2001-12-14 16:26:05 +00:00
|
|
|
//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
|
|
|
|
//
|
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"
|
2001-10-18 18:16:11 +00:00
|
|
|
#include "llvm/iMemory.h"
|
2001-10-14 23:18:45 +00:00
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#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"
|
2002-05-19 15:39:02 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2001-11-27 00:03:19 +00:00
|
|
|
#include "Support/StringExtras.h"
|
2002-05-20 21:43:59 +00:00
|
|
|
#include <algorithm>
|
2001-10-18 20:06:03 +00:00
|
|
|
#include <sstream>
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2001-10-28 21:37:25 +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
|
|
|
|
2002-07-22 02:10:13 +00:00
|
|
|
static cl::list<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,
|
|
|
|
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);
|
|
|
|
|
|
|
|
virtual void handleBasicBlock(BasicBlock *BB, 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...
|
|
|
|
virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct BasicBlockTracer : public InsertTraceCode {
|
|
|
|
// Trace basic blocks here...
|
|
|
|
virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2002-04-27 06:56:12 +00:00
|
|
|
Pass *createTraceValuesPassForFunction() { // Just trace functions
|
2002-07-23 18:04:15 +00:00
|
|
|
return new FunctionTracer();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-04-14 06:15:24 +00:00
|
|
|
Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
|
2002-07-23 18:04:15 +00:00
|
|
|
return new BasicBlockTracer();
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-23 18:04:15 +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 =
|
|
|
|
FunctionType::get(Type::IntTy, 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*)
|
2002-05-19 15:39:02 +00:00
|
|
|
const FunctionType *hashFuncTy =
|
2002-05-20 21:43:59 +00:00
|
|
|
FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
|
2002-06-25 16:13:24 +00:00
|
|
|
HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-05-20 21:43:59 +00:00
|
|
|
// void (sbyte*)
|
|
|
|
const FunctionType *voidSBPFuncTy =
|
|
|
|
FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
|
|
|
|
RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
|
|
|
const FunctionType *voidvoidFuncTy =
|
|
|
|
FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
|
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
|
|
|
|
ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
|
2002-05-19 15:39:02 +00:00
|
|
|
voidvoidFuncTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
static inline GlobalVariable *getStringRef(Module *M, const string &str) {
|
|
|
|
// 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,
|
|
|
|
// or if it used by either a Call or Return instruction.
|
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 &&
|
|
|
|
opCode != Instruction::PHINode &&
|
|
|
|
opCode != Instruction::Cast);
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2001-10-18 20:06:03 +00:00
|
|
|
|
2001-12-14 16:26:05 +00:00
|
|
|
static bool ShouldTraceValue(const Instruction *I) {
|
|
|
|
return
|
|
|
|
I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
|
|
|
|
TraceThisOpCode(I->getOpcode());
|
2001-10-18 13:49:22 +00:00
|
|
|
}
|
|
|
|
|
2001-12-14 16:26:05 +00:00
|
|
|
static string getPrintfCodeFor(const Value *V) {
|
|
|
|
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,
|
2002-05-19 15:39:02 +00:00
|
|
|
string Message,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum) {
|
2001-12-14 16:26:05 +00:00
|
|
|
// Escape Message by replacing all % characters with %% chars.
|
2002-10-17 16:22:08 +00:00
|
|
|
string Tmp;
|
|
|
|
std::swap(Tmp, Message);
|
|
|
|
string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
|
|
|
|
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 *
|
2003-06-05 04:48:18 +00:00
|
|
|
Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
|
|
|
|
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
|
|
|
|
|
|
|
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:
|
2001-12-14 16:26:05 +00:00
|
|
|
vector<Value*> PrintArgs;
|
|
|
|
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,
|
2002-05-19 15:39:02 +00:00
|
|
|
const string &Message, Function *Printf,
|
|
|
|
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);
|
|
|
|
|
2002-05-20 21:43:59 +00:00
|
|
|
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
|
|
|
|
2002-09-10 17:04:02 +00:00
|
|
|
vector<Value*> releaseArgs(1, V);
|
|
|
|
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,
|
2002-03-26 18:01:55 +00:00
|
|
|
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)) {
|
2002-09-10 17:04:02 +00:00
|
|
|
assert(valuesStoredInFunction &&
|
|
|
|
"Should not be printing a store instruction at function exit");
|
|
|
|
LoadInst *LI = new LoadInst(SI->getPointerOperand(), "reload." +
|
|
|
|
SI->getPointerOperand()->getName(),
|
|
|
|
InsertPos);
|
|
|
|
valuesStoredInFunction->push_back(LI);
|
|
|
|
}
|
|
|
|
if (ShouldTraceValue(II))
|
|
|
|
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
|
2002-07-23 18:04:15 +00:00
|
|
|
BasicBlock &BB = F.getEntryNode();
|
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;
|
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
vector<Instruction*> valuesStoredInFunction;
|
2001-12-14 16:26:05 +00:00
|
|
|
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)
|
2002-09-10 17:04:02 +00:00
|
|
|
new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
|
|
|
|
F.getEntryNode().begin());
|
|
|
|
|
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)
|
2002-09-10 17:04:02 +00:00
|
|
|
new CallInst(externalFuncs.ReleaseOnReturnFunc, 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
|
|
|
}
|