2003-10-13 03:32:08 +00:00
|
|
|
//===- TraceValues.cpp - Value Tracing for debugging ----------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-12-14 16:26:05 +00:00
|
|
|
//
|
2002-04-14 06:15:24 +00:00
|
|
|
// Support for inserting LLVM code to print values at basic block and function
|
2001-12-14 16:26:05 +00:00
|
|
|
// exits.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-14 23:18:45 +00:00
|
|
|
|
2003-01-14 22:39:29 +00:00
|
|
|
#include "llvm/Transforms/Instrumentation.h"
|
2002-04-28 19:55:58 +00:00
|
|
|
#include "llvm/Constants.h"
|
2001-10-14 23:18:45 +00:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2001-10-14 23:18:45 +00:00
|
|
|
#include "llvm/Module.h"
|
2002-02-26 21:46:54 +00:00
|
|
|
#include "llvm/Pass.h"
|
2001-10-18 05:28:08 +00:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2002-05-20 21:43:59 +00:00
|
|
|
#include <algorithm>
|
2001-10-18 20:06:03 +00:00
|
|
|
#include <sstream>
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
|
2003-04-13 03:50:14 +00:00
|
|
|
cl::desc("Disable pointer hashing in the -trace or -tracem "
|
|
|
|
"passes"));
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
static cl::list<std::string>
|
2003-04-13 03:50:14 +00:00
|
|
|
TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
|
|
|
|
"-trace or -tracem passes"),
|
2003-01-13 00:52:14 +00:00
|
|
|
cl::value_desc("function"), cl::Hidden);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-07-23 18:04:15 +00:00
|
|
|
static void TraceValuesAtBBExit(BasicBlock *BB,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum,
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Instruction*> *valuesStoredInFunction);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
|
|
|
// We trace a particular function if no functions to trace were specified
|
|
|
|
// or if the function is in the specified list.
|
|
|
|
//
|
2002-07-23 18:04:15 +00:00
|
|
|
inline static bool
|
2003-01-13 00:52:14 +00:00
|
|
|
TraceThisFunction(Function &F)
|
2002-05-19 15:39:02 +00:00
|
|
|
{
|
2003-01-13 00:52:14 +00:00
|
|
|
if (TraceFuncNames.empty()) return true;
|
2002-05-20 21:43:59 +00:00
|
|
|
|
2003-01-13 00:52:14 +00:00
|
|
|
return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
|
|
|
|
!= TraceFuncNames.end();
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-26 21:46:54 +00:00
|
|
|
namespace {
|
2002-05-20 21:43:59 +00:00
|
|
|
struct ExternalFuncs {
|
2002-05-19 15:39:02 +00:00
|
|
|
Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
|
|
|
|
Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
|
2002-06-25 16:13:24 +00:00
|
|
|
void doInitialization(Module &M); // Add prototypes for external functions
|
2002-05-19 15:39:02 +00:00
|
|
|
};
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
class InsertTraceCode : public FunctionPass {
|
2002-07-23 18:04:15 +00:00
|
|
|
protected:
|
2002-05-19 15:39:02 +00:00
|
|
|
ExternalFuncs externalFuncs;
|
2002-02-26 21:46:54 +00:00
|
|
|
public:
|
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
// Add a prototype for runtime functions not already in the program.
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool doInitialization(Module &M);
|
2002-02-26 21:46:54 +00:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// Function InsertCodeToTraceValues
|
|
|
|
//
|
2002-04-14 06:15:24 +00:00
|
|
|
// Inserts tracing code for all live values at basic block and/or function
|
2002-03-26 18:01:55 +00:00
|
|
|
// exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-07-23 18:04:15 +00:00
|
|
|
bool doit(Function *M);
|
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) = 0;
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-04-14 06:15:24 +00:00
|
|
|
// runOnFunction - This method does the work.
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-07-23 18:04:15 +00:00
|
|
|
bool runOnFunction(Function &F);
|
2002-04-28 21:27:06 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-21 20:00:28 +00:00
|
|
|
AU.setPreservesCFG();
|
2002-04-28 21:27:06 +00:00
|
|
|
}
|
2002-02-26 21:46:54 +00:00
|
|
|
};
|
2002-07-23 18:04:15 +00:00
|
|
|
|
|
|
|
struct FunctionTracer : public InsertTraceCode {
|
|
|
|
// Ignore basic blocks here...
|
2003-10-27 21:44:09 +00:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) {}
|
2002-07-23 18:04:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BasicBlockTracer : public InsertTraceCode {
|
|
|
|
// Trace basic blocks here...
|
2003-10-27 21:44:09 +00:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) {
|
2002-07-23 18:04:15 +00:00
|
|
|
TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc, &VI);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the passes...
|
2002-07-26 21:12:46 +00:00
|
|
|
RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
|
|
|
|
RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
|
2002-02-26 21:46:54 +00:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2004-09-30 20:14:18 +00:00
|
|
|
/// Just trace functions
|
|
|
|
FunctionPass *llvm::createTraceValuesPassForFunction() {
|
2002-07-23 18:04:15 +00:00
|
|
|
return new FunctionTracer();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2004-09-30 20:14:18 +00:00
|
|
|
/// Trace BB's and functions
|
|
|
|
FunctionPass *llvm::createTraceValuesPassForBasicBlocks() {
|
2002-07-23 18:04:15 +00:00
|
|
|
return new BasicBlockTracer();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
// Add a prototype for external functions used by the tracing code.
|
2001-12-14 16:26:05 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
void ExternalFuncs::doInitialization(Module &M) {
|
2001-12-14 16:26:05 +00:00
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-04-04 22:19:18 +00:00
|
|
|
const FunctionType *MTy =
|
2003-10-27 21:44:09 +00:00
|
|
|
FunctionType::get(Type::IntTy, std::vector<const Type*>(1, SBP), true);
|
2002-06-25 16:13:24 +00:00
|
|
|
PrintfFunc = M.getOrInsertFunction("printf", MTy);
|
2002-05-20 21:43:59 +00:00
|
|
|
|
|
|
|
// uint (sbyte*)
|
2003-08-31 00:20:36 +00:00
|
|
|
HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", Type::UIntTy, SBP,
|
|
|
|
0);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-05-20 21:43:59 +00:00
|
|
|
// void (sbyte*)
|
2003-08-31 00:20:36 +00:00
|
|
|
ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum",
|
|
|
|
Type::VoidTy, SBP, 0);
|
|
|
|
RecordPtrFunc = M.getOrInsertFunction("RecordPointer",
|
|
|
|
Type::VoidTy, SBP, 0);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2003-08-31 00:20:36 +00:00
|
|
|
PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", Type::VoidTy, 0);
|
2002-06-25 16:13:24 +00:00
|
|
|
ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
|
2003-08-31 00:20:36 +00:00
|
|
|
Type::VoidTy, 0);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add a prototype for external functions used by the tracing code.
|
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool InsertTraceCode::doInitialization(Module &M) {
|
2002-05-19 15:39:02 +00:00
|
|
|
externalFuncs.doInitialization(M);
|
2002-03-29 03:43:24 +00:00
|
|
|
return false;
|
2001-10-28 21:37:25 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 16:26:05 +00:00
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
static inline GlobalVariable *getStringRef(Module *M, const std::string &str) {
|
2001-12-14 16:26:05 +00:00
|
|
|
// Create a constant internal string reference...
|
|
|
|
Constant *Init = ConstantArray::get(str);
|
2002-03-18 03:40:25 +00:00
|
|
|
|
|
|
|
// Create the global variable and record it in the module
|
|
|
|
// The GV will be renamed to a unique name if needed.
|
2003-04-16 20:28:45 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
|
|
|
|
GlobalValue::InternalLinkage, Init,
|
2001-12-14 16:26:05 +00:00
|
|
|
"trstr");
|
2001-10-18 20:06:03 +00:00
|
|
|
M->getGlobalList().push_back(GV);
|
|
|
|
return GV;
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2001-10-18 13:49:22 +00:00
|
|
|
|
2001-10-18 18:16:11 +00:00
|
|
|
//
|
2001-10-28 21:37:25 +00:00
|
|
|
// Check if this instruction has any uses outside its basic block,
|
2003-07-11 21:57:43 +00:00
|
|
|
// or if it used by either a Call or Return instruction (ditto).
|
|
|
|
// (Values stored to memory within this BB are live at end of BB but are
|
|
|
|
// traced at the store instruction, not where they are computed.)
|
2001-10-18 18:16:11 +00:00
|
|
|
//
|
2001-12-14 16:26:05 +00:00
|
|
|
static inline bool LiveAtBBExit(const Instruction* I) {
|
|
|
|
const BasicBlock *BB = I->getParent();
|
2001-10-18 18:16:11 +00:00
|
|
|
for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
|
2001-12-14 16:26:05 +00:00
|
|
|
if (const Instruction *UI = dyn_cast<Instruction>(*U))
|
|
|
|
if (UI->getParent() != BB || isa<ReturnInst>(UI))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2001-10-18 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
2001-10-14 23:18:45 +00:00
|
|
|
|
2001-12-14 16:26:05 +00:00
|
|
|
static inline bool TraceThisOpCode(unsigned opCode) {
|
|
|
|
// Explicitly test for opCodes *not* to trace so that any new opcodes will
|
|
|
|
// be traced by default (VoidTy's are already excluded)
|
|
|
|
//
|
2002-10-13 19:39:16 +00:00
|
|
|
return (opCode < Instruction::OtherOpsBegin &&
|
2001-12-14 16:26:05 +00:00
|
|
|
opCode != Instruction::Alloca &&
|
2003-10-19 21:34:28 +00:00
|
|
|
opCode != Instruction::PHI &&
|
2001-12-14 16:26:05 +00:00
|
|
|
opCode != Instruction::Cast);
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2001-10-18 20:06:03 +00:00
|
|
|
|
2003-07-11 21:57:43 +00:00
|
|
|
// Trace a value computed by an instruction if it is non-void, it is computed
|
|
|
|
// by a real computation, not just a copy (see TraceThisOpCode), and
|
|
|
|
// -- it is a load instruction: we want to check values read from memory
|
|
|
|
// -- or it is live at exit from the basic block (i.e., ignore local temps)
|
|
|
|
//
|
2001-12-14 16:26:05 +00:00
|
|
|
static bool ShouldTraceValue(const Instruction *I) {
|
|
|
|
return
|
2003-07-11 21:57:43 +00:00
|
|
|
I->getType() != Type::VoidTy &&
|
|
|
|
TraceThisOpCode(I->getOpcode()) &&
|
|
|
|
(isa<LoadInst>(I) || LiveAtBBExit(I));
|
2001-10-18 13:49:22 +00:00
|
|
|
}
|
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
static std::string getPrintfCodeFor(const Value *V) {
|
2001-12-14 16:26:05 +00:00
|
|
|
if (V == 0) return "";
|
2002-04-14 06:15:24 +00:00
|
|
|
if (V->getType()->isFloatingPoint())
|
2001-12-14 16:26:05 +00:00
|
|
|
return "%g";
|
2002-05-19 15:39:02 +00:00
|
|
|
else if (V->getType() == Type::LabelTy)
|
2002-04-14 06:15:24 +00:00
|
|
|
return "0x%p";
|
2002-05-19 15:39:02 +00:00
|
|
|
else if (isa<PointerType>(V->getType()))
|
2002-05-20 21:43:59 +00:00
|
|
|
return DisablePtrHashing ? "0x%p" : "%d";
|
2002-09-03 01:07:35 +00:00
|
|
|
else if (V->getType()->isIntegral())
|
2002-04-14 06:15:24 +00:00
|
|
|
return "%d";
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-04-14 06:15:24 +00:00
|
|
|
assert(0 && "Illegal value to print out...");
|
|
|
|
return "";
|
2001-10-18 13:49:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
|
2003-10-27 21:44:09 +00:00
|
|
|
std::string Message,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function *Printf, Function* HashPtrToSeqNum) {
|
2001-12-14 16:26:05 +00:00
|
|
|
// Escape Message by replacing all % characters with %% chars.
|
2003-10-27 21:44:09 +00:00
|
|
|
std::string Tmp;
|
2002-10-17 16:22:08 +00:00
|
|
|
std::swap(Tmp, Message);
|
2003-10-27 21:44:09 +00:00
|
|
|
std::string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
|
2002-10-17 16:22:08 +00:00
|
|
|
while (I != Tmp.end()) {
|
|
|
|
Message.append(Tmp.begin(), I);
|
|
|
|
Message += "%%";
|
|
|
|
++I; // Make sure to erase the % as well...
|
|
|
|
Tmp.erase(Tmp.begin(), I);
|
|
|
|
I = std::find(Tmp.begin(), Tmp.end(), '%');
|
2001-12-14 16:26:05 +00:00
|
|
|
}
|
2003-01-13 00:52:14 +00:00
|
|
|
Message += Tmp;
|
2001-12-14 16:26:05 +00:00
|
|
|
Module *Mod = BB->getParent()->getParent();
|
2001-10-18 05:28:08 +00:00
|
|
|
|
2001-10-18 06:03:05 +00:00
|
|
|
// Turn the marker string into a global variable...
|
2001-12-14 16:26:05 +00:00
|
|
|
GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
|
|
|
|
|
|
|
|
// Turn the format string into an sbyte *
|
2004-07-18 00:44:37 +00:00
|
|
|
Constant *GEP=ConstantExpr::getGetElementPtr(fmtVal,
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
|
2001-10-18 06:03:05 +00:00
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
// Insert a call to the hash function if this is a pointer value
|
2002-05-20 21:43:59 +00:00
|
|
|
if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
|
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-09-10 17:04:02 +00:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
|
2002-05-20 21:43:59 +00:00
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Value*> HashArgs(1, V);
|
2002-09-10 17:04:02 +00:00
|
|
|
V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
2001-10-18 06:03:05 +00:00
|
|
|
// Insert the first print instruction to print the string flag:
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Value*> PrintArgs;
|
2001-12-14 16:26:05 +00:00
|
|
|
PrintArgs.push_back(GEP);
|
|
|
|
if (V) PrintArgs.push_back(V);
|
2002-09-10 17:04:02 +00:00
|
|
|
new CallInst(Printf, PrintArgs, "trace", InsertBefore);
|
2001-10-18 05:28:08 +00:00
|
|
|
}
|
2001-12-14 16:26:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
|
2002-09-10 17:04:02 +00:00
|
|
|
Instruction *InsertBefore,
|
2003-10-27 21:44:09 +00:00
|
|
|
const std::string &Message, Function *Printf,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function* HashPtrToSeqNum) {
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostringstream OutStr;
|
2001-12-14 16:26:05 +00:00
|
|
|
if (V) WriteAsOperand(OutStr, V);
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
|
2002-05-19 15:39:02 +00:00
|
|
|
Printf, HashPtrToSeqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertReleaseInst(Value *V, BasicBlock *BB,
|
2002-09-10 17:04:02 +00:00
|
|
|
Instruction *InsertBefore,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function* ReleasePtrFunc) {
|
2002-05-20 21:43:59 +00:00
|
|
|
|
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-09-10 17:04:02 +00:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
|
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Value*> releaseArgs(1, V);
|
2002-09-10 17:04:02 +00:00
|
|
|
new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertRecordInst(Value *V, BasicBlock *BB,
|
2002-09-10 17:04:02 +00:00
|
|
|
Instruction *InsertBefore,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function* RecordPtrFunc) {
|
2002-05-20 21:43:59 +00:00
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-09-10 17:04:02 +00:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "RP_cast", InsertBefore);
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Value*> releaseArgs(1, V);
|
2002-09-10 17:04:02 +00:00
|
|
|
new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Look for alloca and free instructions. These are the ptrs to release.
|
|
|
|
// Release the free'd pointers immediately. Record the alloca'd pointers
|
|
|
|
// to be released on return from the current function.
|
|
|
|
//
|
|
|
|
static void
|
|
|
|
ReleasePtrSeqNumbers(BasicBlock *BB,
|
|
|
|
ExternalFuncs& externalFuncs) {
|
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
|
2003-04-23 16:37:45 +00:00
|
|
|
if (FreeInst *FI = dyn_cast<FreeInst>(II))
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
|
2003-04-23 16:37:45 +00:00
|
|
|
else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
2001-10-14 23:18:45 +00:00
|
|
|
|
2002-07-08 23:37:07 +00:00
|
|
|
// Insert print instructions at the end of basic block BB for each value
|
|
|
|
// computed in BB that is live at the end of BB,
|
|
|
|
// or that is stored to memory in BB.
|
|
|
|
// If the value is stored to memory, we load it back before printing it
|
2002-03-26 18:01:55 +00:00
|
|
|
// We also return all such loaded values in the vector valuesStoredInFunction
|
2002-04-14 06:15:24 +00:00
|
|
|
// for printing at the exit from the function. (Note that in each invocation
|
|
|
|
// of the function, this will only get the last value stored for each static
|
2001-10-18 18:16:11 +00:00
|
|
|
// store instruction).
|
2001-10-14 23:18:45 +00:00
|
|
|
//
|
2002-05-19 15:39:02 +00:00
|
|
|
static void TraceValuesAtBBExit(BasicBlock *BB,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum,
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Instruction*> *valuesStoredInFunction) {
|
2001-10-28 21:37:25 +00:00
|
|
|
// Get an iterator to point to the insertion location, which is
|
|
|
|
// just before the terminator instruction.
|
2001-10-14 23:18:45 +00:00
|
|
|
//
|
2002-09-10 17:04:02 +00:00
|
|
|
TerminatorInst *InsertPos = BB->getTerminator();
|
2001-10-14 23:18:45 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostringstream OutStr;
|
2001-12-14 16:26:05 +00:00
|
|
|
WriteAsOperand(OutStr, BB, false);
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
|
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2002-07-08 23:37:07 +00:00
|
|
|
// Insert a print instruction for each instruction preceding InsertPos.
|
|
|
|
// The print instructions must go before InsertPos, so we use the
|
|
|
|
// instruction *preceding* InsertPos to check when to terminate the loop.
|
2001-12-14 16:26:05 +00:00
|
|
|
//
|
2002-09-10 17:04:02 +00:00
|
|
|
for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
|
2003-04-23 16:37:45 +00:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
|
2003-07-11 21:57:43 +00:00
|
|
|
// Trace the stored value and address
|
|
|
|
InsertVerbosePrintInst(SI->getOperand(0), BB, InsertPos,
|
|
|
|
" (store value) ", Printf, HashPtrToSeqNum);
|
|
|
|
InsertVerbosePrintInst(SI->getOperand(1), BB, InsertPos,
|
|
|
|
" (store addr ) ", Printf, HashPtrToSeqNum);
|
2002-09-10 17:04:02 +00:00
|
|
|
}
|
2003-07-11 21:57:43 +00:00
|
|
|
else if (ShouldTraceValue(II))
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
|
2001-12-14 16:26:05 +00:00
|
|
|
}
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2002-07-23 18:04:15 +00:00
|
|
|
static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function* HashPtrToSeqNum){
|
2001-10-18 18:16:11 +00:00
|
|
|
// Get an iterator to point to the insertion location
|
2003-09-20 14:39:18 +00:00
|
|
|
BasicBlock &BB = F.getEntryBlock();
|
2002-09-10 17:04:02 +00:00
|
|
|
Instruction *InsertPos = BB.begin();
|
2001-12-14 16:26:05 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostringstream OutStr;
|
2003-01-13 00:52:14 +00:00
|
|
|
WriteAsOperand(OutStr, &F);
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
|
2002-05-19 15:39:02 +00:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-12-14 16:26:05 +00:00
|
|
|
|
2001-11-15 15:00:16 +00:00
|
|
|
// Now print all the incoming arguments
|
2001-12-14 16:26:05 +00:00
|
|
|
unsigned ArgNo = 0;
|
2002-07-23 18:04:15 +00:00
|
|
|
for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertVerbosePrintInst(I, &BB, InsertPos,
|
2002-05-20 21:43:59 +00:00
|
|
|
" Arg #" + utostr(ArgNo) + ": ", Printf,
|
|
|
|
HashPtrToSeqNum);
|
2001-12-14 16:26:05 +00:00
|
|
|
}
|
2001-10-18 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
|
2002-05-19 15:39:02 +00:00
|
|
|
Function *Printf,
|
|
|
|
Function* HashPtrToSeqNum) {
|
2001-10-18 18:16:11 +00:00
|
|
|
// Get an iterator to point to the insertion location
|
2002-09-10 17:04:02 +00:00
|
|
|
ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostringstream OutStr;
|
2001-12-14 16:26:05 +00:00
|
|
|
WriteAsOperand(OutStr, BB->getParent(), true);
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
|
2002-05-19 15:39:02 +00:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2001-11-15 15:00:16 +00:00
|
|
|
// print the return value, if any
|
2001-12-14 16:26:05 +00:00
|
|
|
if (BB->getParent()->getReturnType() != Type::VoidTy)
|
2002-09-10 17:04:02 +00:00
|
|
|
InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
|
2002-05-19 15:39:02 +00:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-23 18:04:15 +00:00
|
|
|
bool InsertTraceCode::runOnFunction(Function &F) {
|
|
|
|
if (!TraceThisFunction(F))
|
2002-05-19 15:39:02 +00:00
|
|
|
return false;
|
|
|
|
|
2003-10-27 21:44:09 +00:00
|
|
|
std::vector<Instruction*> valuesStoredInFunction;
|
|
|
|
std::vector<BasicBlock*> exitBlocks;
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
// Insert code to trace values at function entry
|
2002-07-23 18:04:15 +00:00
|
|
|
InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
|
|
|
// Push a pointer set for recording alloca'd pointers at entry.
|
2002-05-20 21:43:59 +00:00
|
|
|
if (!DisablePtrHashing)
|
2003-10-27 21:44:09 +00:00
|
|
|
new CallInst(externalFuncs.PushOnEntryFunc, std::vector<Value*>(), "",
|
2003-09-20 14:39:18 +00:00
|
|
|
F.getEntryBlock().begin());
|
2002-09-10 17:04:02 +00:00
|
|
|
|
2002-07-23 18:04:15 +00:00
|
|
|
for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
|
2001-12-14 16:26:05 +00:00
|
|
|
if (isa<ReturnInst>(BB->getTerminator()))
|
|
|
|
exitBlocks.push_back(BB); // record this as an exit block
|
2002-07-23 18:04:15 +00:00
|
|
|
|
|
|
|
// Insert trace code if this basic block is interesting...
|
|
|
|
handleBasicBlock(BB, valuesStoredInFunction);
|
|
|
|
|
2002-05-20 21:43:59 +00:00
|
|
|
if (!DisablePtrHashing) // release seq. numbers on free/ret
|
2002-05-19 15:39:02 +00:00
|
|
|
ReleasePtrSeqNumbers(BB, externalFuncs);
|
2001-12-14 16:26:05 +00:00
|
|
|
}
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-07-23 18:04:15 +00:00
|
|
|
for (unsigned i=0; i != exitBlocks.size(); ++i)
|
2002-05-19 15:39:02 +00:00
|
|
|
{
|
|
|
|
// Insert code to trace values at function exit
|
2002-07-23 18:04:15 +00:00
|
|
|
InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
|
|
|
// Release all recorded pointers before RETURN. Do this LAST!
|
2002-05-20 21:43:59 +00:00
|
|
|
if (!DisablePtrHashing)
|
2003-10-27 21:44:09 +00:00
|
|
|
new CallInst(externalFuncs.ReleaseOnReturnFunc, std::vector<Value*>(),
|
|
|
|
"", exitBlocks[i]->getTerminator());
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2001-10-18 05:28:08 +00:00
|
|
|
return true;
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|