2001-12-15 00:26:05 +08:00
|
|
|
//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
|
|
|
|
//
|
2002-04-14 14:15:24 +08:00
|
|
|
// Support for inserting LLVM code to print values at basic block and function
|
2001-12-15 00:26:05 +08:00
|
|
|
// exits.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-10-15 07:18:45 +08:00
|
|
|
|
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
|
|
|
#include "llvm/GlobalVariable.h"
|
2002-04-29 03:55:58 +08:00
|
|
|
#include "llvm/Constants.h"
|
2001-10-15 07:18:45 +08:00
|
|
|
#include "llvm/DerivedTypes.h"
|
2001-10-19 02:16:11 +08:00
|
|
|
#include "llvm/iMemory.h"
|
2001-10-15 07:18:45 +08:00
|
|
|
#include "llvm/iTerminators.h"
|
|
|
|
#include "llvm/iOther.h"
|
2002-04-10 02:37:46 +08:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-03-27 02:01:55 +08:00
|
|
|
#include "llvm/Function.h"
|
2001-10-15 07:18:45 +08:00
|
|
|
#include "llvm/Module.h"
|
2002-02-27 05:46:54 +08:00
|
|
|
#include "llvm/Pass.h"
|
2001-10-18 13:28:08 +08:00
|
|
|
#include "llvm/Assembly/Writer.h"
|
2002-05-19 23:39:02 +08:00
|
|
|
#include "Support/CommandLine.h"
|
2001-11-27 08:03:19 +08:00
|
|
|
#include "Support/StringExtras.h"
|
2002-05-21 05:43:59 +08:00
|
|
|
#include <algorithm>
|
2001-10-19 04:06:03 +08:00
|
|
|
#include <sstream>
|
2002-01-21 06:54:45 +08:00
|
|
|
using std::vector;
|
|
|
|
using std::string;
|
2001-10-29 05:37:25 +08:00
|
|
|
|
2002-05-21 05:43:59 +08:00
|
|
|
static cl::Flag DisablePtrHashing("tracedisablehashdisable",
|
|
|
|
"Disable pointer hashing", cl::NoFlags);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-05-21 05:43:59 +08:00
|
|
|
static cl::StringList TraceFuncName ("tracefunc", "trace only specific funct"
|
|
|
|
"ions", cl::NoFlags);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
// We trace a particular function if no functions to trace were specified
|
|
|
|
// or if the function is in the specified list.
|
|
|
|
//
|
|
|
|
inline bool
|
|
|
|
TraceThisFunction(Function* func)
|
|
|
|
{
|
|
|
|
if (TraceFuncName.getNumOccurances() == 0)
|
|
|
|
return true;
|
2002-05-21 05:43:59 +08:00
|
|
|
|
|
|
|
return std::find(TraceFuncName.begin(), TraceFuncName.end(), func->getName())
|
|
|
|
!= TraceFuncName.end();
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-27 05:46:54 +08:00
|
|
|
namespace {
|
2002-05-21 05:43:59 +08:00
|
|
|
struct ExternalFuncs {
|
2002-05-19 23:39:02 +08:00
|
|
|
Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
|
|
|
|
Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
|
2002-06-26 00:13:24 +08:00
|
|
|
void doInitialization(Module &M); // Add prototypes for external functions
|
2002-05-19 23:39:02 +08:00
|
|
|
};
|
|
|
|
|
2002-04-27 14:56:12 +08:00
|
|
|
class InsertTraceCode : public FunctionPass {
|
2002-03-27 02:01:55 +08:00
|
|
|
bool TraceBasicBlockExits, TraceFunctionExits;
|
2002-05-19 23:39:02 +08:00
|
|
|
ExternalFuncs externalFuncs;
|
2002-02-27 05:46:54 +08:00
|
|
|
public:
|
2002-03-27 02:01:55 +08:00
|
|
|
InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
|
2002-02-27 05:46:54 +08:00
|
|
|
: TraceBasicBlockExits(traceBasicBlockExits),
|
2002-03-27 02:01:55 +08:00
|
|
|
TraceFunctionExits(traceFunctionExits) {}
|
2002-04-29 22:57:45 +08:00
|
|
|
|
|
|
|
const char *getPassName() const { return "Trace Code Insertion"; }
|
2002-02-27 05:46:54 +08:00
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
// Add a prototype for runtime functions not already in the program.
|
2002-02-27 05:46:54 +08:00
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool doInitialization(Module &M);
|
2002-02-27 05:46:54 +08:00
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// Function InsertCodeToTraceValues
|
|
|
|
//
|
2002-04-14 14:15:24 +08:00
|
|
|
// Inserts tracing code for all live values at basic block and/or function
|
2002-03-27 02:01:55 +08:00
|
|
|
// exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
|
2002-02-27 05:46:54 +08:00
|
|
|
//
|
2002-03-27 02:01:55 +08:00
|
|
|
static bool doit(Function *M, bool traceBasicBlockExits,
|
2002-05-19 23:39:02 +08:00
|
|
|
bool traceFunctionExits, ExternalFuncs& externalFuncs);
|
|
|
|
|
2002-04-14 14:15:24 +08:00
|
|
|
// runOnFunction - This method does the work.
|
2002-02-27 05:46:54 +08:00
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
return doit(&F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
|
2002-02-27 05:46:54 +08:00
|
|
|
}
|
2002-04-29 05:27:06 +08:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.preservesCFG();
|
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2002-04-27 14:56:12 +08:00
|
|
|
Pass *createTraceValuesPassForFunction() { // Just trace functions
|
2002-02-27 05:46:54 +08:00
|
|
|
return new InsertTraceCode(false, true);
|
|
|
|
}
|
|
|
|
|
2002-04-14 14:15:24 +08:00
|
|
|
Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
|
2002-02-27 05:46:54 +08:00
|
|
|
return new InsertTraceCode(true, true);
|
|
|
|
}
|
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
// Add a prototype for external functions used by the tracing code.
|
2001-12-15 00:26:05 +08:00
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
void ExternalFuncs::doInitialization(Module &M) {
|
2001-12-15 00:26:05 +08:00
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-04-05 06:19:18 +08:00
|
|
|
const FunctionType *MTy =
|
|
|
|
FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
|
2002-06-26 00:13:24 +08:00
|
|
|
PrintfFunc = M.getOrInsertFunction("printf", MTy);
|
2002-05-21 05:43:59 +08:00
|
|
|
|
|
|
|
// uint (sbyte*)
|
2002-05-19 23:39:02 +08:00
|
|
|
const FunctionType *hashFuncTy =
|
2002-05-21 05:43:59 +08:00
|
|
|
FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
|
2002-06-26 00:13:24 +08:00
|
|
|
HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-05-21 05:43:59 +08:00
|
|
|
// void (sbyte*)
|
|
|
|
const FunctionType *voidSBPFuncTy =
|
|
|
|
FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
|
|
|
|
RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
|
|
|
const FunctionType *voidvoidFuncTy =
|
|
|
|
FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
|
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
|
|
|
|
ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
|
2002-05-19 23:39:02 +08:00
|
|
|
voidvoidFuncTy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Add a prototype for external functions used by the tracing code.
|
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
bool InsertTraceCode::doInitialization(Module &M) {
|
2002-05-19 23:39:02 +08:00
|
|
|
externalFuncs.doInitialization(M);
|
2002-03-29 11:43:24 +08:00
|
|
|
return false;
|
2001-10-29 05:37:25 +08:00
|
|
|
}
|
|
|
|
|
2001-12-15 00:26:05 +08:00
|
|
|
|
|
|
|
static inline GlobalVariable *getStringRef(Module *M, const string &str) {
|
|
|
|
// Create a constant internal string reference...
|
|
|
|
Constant *Init = ConstantArray::get(str);
|
2002-03-18 11:40:25 +08:00
|
|
|
|
|
|
|
// Create the global variable and record it in the module
|
|
|
|
// The GV will be renamed to a unique name if needed.
|
2001-12-15 00:26:05 +08:00
|
|
|
GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
|
|
|
|
"trstr");
|
2001-10-19 04:06:03 +08:00
|
|
|
M->getGlobalList().push_back(GV);
|
|
|
|
return GV;
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
2001-10-18 21:49:22 +08:00
|
|
|
|
2001-10-19 02:16:11 +08:00
|
|
|
//
|
2001-10-29 05:37:25 +08: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-19 02:16:11 +08:00
|
|
|
//
|
2001-12-15 00:26:05 +08:00
|
|
|
static inline bool LiveAtBBExit(const Instruction* I) {
|
|
|
|
const BasicBlock *BB = I->getParent();
|
2001-10-19 02:16:11 +08:00
|
|
|
for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
|
2001-12-15 00:26:05 +08:00
|
|
|
if (const Instruction *UI = dyn_cast<Instruction>(*U))
|
|
|
|
if (UI->getParent() != BB || isa<ReturnInst>(UI))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
2001-10-19 02:16:11 +08:00
|
|
|
}
|
|
|
|
|
2001-10-15 07:18:45 +08:00
|
|
|
|
2001-12-15 00:26:05 +08: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)
|
|
|
|
//
|
|
|
|
return (opCode < Instruction::FirstOtherOp &&
|
|
|
|
opCode != Instruction::Alloca &&
|
|
|
|
opCode != Instruction::PHINode &&
|
|
|
|
opCode != Instruction::Cast);
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
2001-10-19 04:06:03 +08:00
|
|
|
|
2001-12-15 00:26:05 +08:00
|
|
|
static bool ShouldTraceValue(const Instruction *I) {
|
|
|
|
return
|
|
|
|
I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
|
|
|
|
TraceThisOpCode(I->getOpcode());
|
2001-10-18 21:49:22 +08:00
|
|
|
}
|
|
|
|
|
2001-12-15 00:26:05 +08:00
|
|
|
static string getPrintfCodeFor(const Value *V) {
|
|
|
|
if (V == 0) return "";
|
2002-04-14 14:15:24 +08:00
|
|
|
if (V->getType()->isFloatingPoint())
|
2001-12-15 00:26:05 +08:00
|
|
|
return "%g";
|
2002-05-19 23:39:02 +08:00
|
|
|
else if (V->getType() == Type::LabelTy)
|
2002-04-14 14:15:24 +08:00
|
|
|
return "0x%p";
|
2002-05-19 23:39:02 +08:00
|
|
|
else if (isa<PointerType>(V->getType()))
|
2002-05-21 05:43:59 +08:00
|
|
|
return DisablePtrHashing ? "0x%p" : "%d";
|
2002-04-14 14:15:24 +08:00
|
|
|
else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
|
|
|
|
return "%d";
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-04-14 14:15:24 +08:00
|
|
|
assert(0 && "Illegal value to print out...");
|
|
|
|
return "";
|
2001-10-18 21:49:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
|
|
|
|
string Message,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum) {
|
2001-12-15 00:26:05 +08:00
|
|
|
// Escape Message by replacing all % characters with %% chars.
|
|
|
|
unsigned Offset = 0;
|
|
|
|
while ((Offset = Message.find('%', Offset)) != string::npos) {
|
2002-04-14 14:15:24 +08:00
|
|
|
Message.replace(Offset, 1, "%%");
|
2001-12-15 00:26:05 +08:00
|
|
|
Offset += 2; // Skip over the new %'s
|
|
|
|
}
|
2001-10-18 13:28:08 +08:00
|
|
|
|
2001-12-15 00:26:05 +08:00
|
|
|
Module *Mod = BB->getParent()->getParent();
|
2001-10-18 13:28:08 +08:00
|
|
|
|
2001-10-18 14:03:05 +08:00
|
|
|
// Turn the marker string into a global variable...
|
2001-12-15 00:26:05 +08:00
|
|
|
GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
|
|
|
|
|
|
|
|
// Turn the format string into an sbyte *
|
|
|
|
Instruction *GEP =
|
|
|
|
new GetElementPtrInst(fmtVal,
|
|
|
|
vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
|
|
|
|
"trstr");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, GEP);
|
2001-10-18 14:03:05 +08:00
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
// Insert a call to the hash function if this is a pointer value
|
2002-05-21 05:43:59 +08:00
|
|
|
if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
|
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
|
|
|
if (V->getType() != SBP) { // Cast pointer to be sbyte*
|
|
|
|
Instruction *I = new CastInst(V, SBP, "Hash_cast");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-21 05:43:59 +08:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Value*> HashArgs(1, V);
|
2002-05-19 23:39:02 +08:00
|
|
|
V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
2001-10-18 14:03:05 +08:00
|
|
|
// Insert the first print instruction to print the string flag:
|
2001-12-15 00:26:05 +08:00
|
|
|
vector<Value*> PrintArgs;
|
|
|
|
PrintArgs.push_back(GEP);
|
|
|
|
if (V) PrintArgs.push_back(V);
|
|
|
|
Instruction *I = new CallInst(Printf, PrintArgs, "trace");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2001-10-18 13:28:08 +08:00
|
|
|
}
|
2001-12-15 00:26:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
2002-05-19 23:39:02 +08:00
|
|
|
const string &Message, Function *Printf,
|
|
|
|
Function* HashPtrToSeqNum) {
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostringstream OutStr;
|
2001-12-15 00:26:05 +08:00
|
|
|
if (V) WriteAsOperand(OutStr, V);
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
|
|
|
|
Printf, HashPtrToSeqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertReleaseInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
|
|
|
Function* ReleasePtrFunc) {
|
2002-05-21 05:43:59 +08:00
|
|
|
|
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
|
|
|
if (V->getType() != SBP) { // Cast pointer to be sbyte*
|
|
|
|
Instruction *I = new CastInst(V, SBP, "RPSN_cast");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-21 05:43:59 +08:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
vector<Value*> releaseArgs(1, V);
|
2002-05-19 23:39:02 +08:00
|
|
|
Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertRecordInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
|
|
|
Function* RecordPtrFunc) {
|
2002-05-21 05:43:59 +08:00
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
|
|
|
if (V->getType() != SBP) { // Cast pointer to be sbyte*
|
|
|
|
Instruction *I = new CastInst(V, SBP, "RP_cast");
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-21 05:43:59 +08:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
vector<Value*> releaseArgs(1, V);
|
2002-05-19 23:39:02 +08:00
|
|
|
Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
|
2002-06-26 00:13:24 +08:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertPushOnEntryFunc(Function *M,
|
|
|
|
Function* PushOnEntryFunc) {
|
|
|
|
// Get an iterator to point to the insertion location
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock &BB = M->getEntryNode();
|
|
|
|
BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
|
|
|
|
vector<Value*>()));
|
2001-10-19 02:16:11 +08:00
|
|
|
}
|
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
static void
|
|
|
|
InsertReleaseRecordedInst(BasicBlock *BB,
|
|
|
|
Function* ReleaseOnReturnFunc) {
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock::iterator BBI = BB->end()--;
|
|
|
|
BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
|
|
|
|
vector<Value*>()));
|
2002-05-19 23:39:02 +08: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) {
|
|
|
|
|
|
|
|
for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
|
2002-06-26 00:13:24 +08:00
|
|
|
if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
|
2002-06-26 00:13:24 +08:00
|
|
|
else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
|
2002-05-19 23:39:02 +08:00
|
|
|
{
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock::iterator nextI = ++II;
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
|
2002-06-26 00:13:24 +08:00
|
|
|
II = --nextI;
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-15 07:18:45 +08:00
|
|
|
|
|
|
|
// Insert print instructions at the end of the basic block *bb
|
2001-10-19 02:16:11 +08:00
|
|
|
// for each value in valueVec[] that is live at the end of that basic block,
|
|
|
|
// or that is stored to memory in this basic block.
|
|
|
|
// If the value is stored to memory, we load it back before printing
|
2002-03-27 02:01:55 +08:00
|
|
|
// We also return all such loaded values in the vector valuesStoredInFunction
|
2002-04-14 14:15:24 +08: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-19 02:16:11 +08:00
|
|
|
// store instruction).
|
|
|
|
// *bb must be the block in which the value is computed;
|
|
|
|
// this is not checked here.
|
2001-10-15 07:18:45 +08:00
|
|
|
//
|
2002-05-19 23:39:02 +08:00
|
|
|
static void TraceValuesAtBBExit(BasicBlock *BB,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum,
|
2002-03-27 02:01:55 +08:00
|
|
|
vector<Instruction*> *valuesStoredInFunction) {
|
2001-10-29 05:37:25 +08:00
|
|
|
// Get an iterator to point to the insertion location, which is
|
|
|
|
// just before the terminator instruction.
|
2001-10-15 07:18:45 +08:00
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock::iterator InsertPos = BB->end()--;
|
|
|
|
assert(BB->back().isTerminator());
|
2001-10-15 07:18:45 +08:00
|
|
|
|
2001-10-29 05:37:25 +08:00
|
|
|
// If the terminator is a conditional branch, insert the trace code just
|
|
|
|
// before the instruction that computes the branch condition (just to
|
|
|
|
// avoid putting a call between the CC-setting instruction and the branch).
|
|
|
|
// Use laterInstrSet to mark instructions that come after the setCC instr
|
|
|
|
// because those cannot be traced at the location we choose.
|
|
|
|
//
|
2001-12-15 00:26:05 +08:00
|
|
|
Instruction *SetCC = 0;
|
|
|
|
if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
|
|
|
|
if (!Branch->isUnconditional())
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
|
|
|
|
if (I->getParent() == BB) {
|
2002-06-26 00:13:24 +08:00
|
|
|
InsertPos = SetCC = I; // Back up until we can insert before the setcc
|
2001-10-19 02:16:11 +08:00
|
|
|
}
|
2001-10-18 21:49:22 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostringstream OutStr;
|
2001-12-15 00:26:05 +08:00
|
|
|
WriteAsOperand(OutStr, BB, false);
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
|
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2001-12-15 00:26:05 +08:00
|
|
|
// Insert a print instruction for each value.
|
|
|
|
//
|
2002-06-26 00:13:24 +08:00
|
|
|
for (BasicBlock::iterator II = BB->begin(), IE = InsertPos++; II != IE; ++II){
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
|
2002-03-27 02:01:55 +08:00
|
|
|
assert(valuesStoredInFunction &&
|
2002-04-14 14:15:24 +08:00
|
|
|
"Should not be printing a store instruction at function exit");
|
2001-12-15 00:26:05 +08:00
|
|
|
LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
|
2002-06-26 00:13:24 +08:00
|
|
|
"reload."+SI->getPointerOperand()->getName());
|
|
|
|
InsertPos = ++BB->getInstList().insert(InsertPos, LI);
|
2002-03-27 02:01:55 +08:00
|
|
|
valuesStoredInFunction->push_back(LI);
|
2001-12-15 00:26:05 +08:00
|
|
|
}
|
2002-06-26 00:13:24 +08:00
|
|
|
if (ShouldTraceValue(II))
|
|
|
|
InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
|
2001-12-15 00:26:05 +08:00
|
|
|
}
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
|
|
|
|
Function* HashPtrToSeqNum){
|
2001-10-19 02:16:11 +08:00
|
|
|
// Get an iterator to point to the insertion location
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock &BB = M->getEntryNode();
|
|
|
|
BasicBlock::iterator BBI = BB.begin();
|
2001-12-15 00:26:05 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostringstream OutStr;
|
2001-12-15 00:26:05 +08:00
|
|
|
WriteAsOperand(OutStr, M, true);
|
2002-06-26 00:13:24 +08:00
|
|
|
InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
|
2002-05-19 23:39:02 +08:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-12-15 00:26:05 +08:00
|
|
|
|
2001-11-15 23:00:16 +08:00
|
|
|
// Now print all the incoming arguments
|
2001-12-15 00:26:05 +08:00
|
|
|
unsigned ArgNo = 0;
|
2002-06-26 00:13:24 +08:00
|
|
|
for (Function::aiterator I = M->abegin(), E = M->aend(); I != E; ++I,++ArgNo){
|
|
|
|
InsertVerbosePrintInst(I, &BB, BBI,
|
2002-05-21 05:43:59 +08:00
|
|
|
" Arg #" + utostr(ArgNo) + ": ", Printf,
|
|
|
|
HashPtrToSeqNum);
|
2001-12-15 00:26:05 +08:00
|
|
|
}
|
2001-10-19 02:16:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-27 02:01:55 +08:00
|
|
|
static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
|
2002-05-19 23:39:02 +08:00
|
|
|
Function *Printf,
|
|
|
|
Function* HashPtrToSeqNum) {
|
2001-10-19 02:16:11 +08:00
|
|
|
// Get an iterator to point to the insertion location
|
2002-06-26 00:13:24 +08:00
|
|
|
BasicBlock::iterator BBI = BB->end()--;
|
|
|
|
ReturnInst &Ret = cast<ReturnInst>(BB->back());
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostringstream OutStr;
|
2001-12-15 00:26:05 +08:00
|
|
|
WriteAsOperand(OutStr, BB->getParent(), true);
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
|
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2001-11-15 23:00:16 +08:00
|
|
|
// print the return value, if any
|
2001-12-15 00:26:05 +08:00
|
|
|
if (BB->getParent()->getReturnType() != Type::VoidTy)
|
2002-06-26 00:13:24 +08:00
|
|
|
InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
|
2002-05-19 23:39:02 +08:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-27 02:01:55 +08:00
|
|
|
bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
|
2002-05-19 23:39:02 +08:00
|
|
|
bool traceFunctionEvents,
|
|
|
|
ExternalFuncs& externalFuncs) {
|
2002-03-27 02:01:55 +08:00
|
|
|
if (!traceBasicBlockExits && !traceFunctionEvents)
|
2001-12-15 00:26:05 +08:00
|
|
|
return false;
|
2001-10-18 21:49:22 +08:00
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
if (!TraceThisFunction(M))
|
|
|
|
return false;
|
|
|
|
|
2002-03-27 02:01:55 +08:00
|
|
|
vector<Instruction*> valuesStoredInFunction;
|
2001-12-15 00:26:05 +08:00
|
|
|
vector<BasicBlock*> exitBlocks;
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2002-05-19 23:39:02 +08:00
|
|
|
// Insert code to trace values at function entry
|
2002-03-27 02:01:55 +08:00
|
|
|
if (traceFunctionEvents)
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
|
|
|
|
|
|
|
// Push a pointer set for recording alloca'd pointers at entry.
|
2002-05-21 05:43:59 +08:00
|
|
|
if (!DisablePtrHashing)
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2002-06-26 00:13:24 +08:00
|
|
|
for (Function::iterator BB = M->begin(); BB != M->end(); ++BB) {
|
2001-12-15 00:26:05 +08:00
|
|
|
if (isa<ReturnInst>(BB->getTerminator()))
|
|
|
|
exitBlocks.push_back(BB); // record this as an exit block
|
|
|
|
|
|
|
|
if (traceBasicBlockExits)
|
2002-05-19 23:39:02 +08:00
|
|
|
TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc, &valuesStoredInFunction);
|
|
|
|
|
2002-05-21 05:43:59 +08:00
|
|
|
if (!DisablePtrHashing) // release seq. numbers on free/ret
|
2002-05-19 23:39:02 +08:00
|
|
|
ReleasePtrSeqNumbers(BB, externalFuncs);
|
2001-12-15 00:26:05 +08:00
|
|
|
}
|
2002-05-19 23:39:02 +08:00
|
|
|
|
|
|
|
for (unsigned i=0; i < exitBlocks.size(); ++i)
|
|
|
|
{
|
|
|
|
// Insert code to trace values at function exit
|
|
|
|
if (traceFunctionEvents)
|
|
|
|
InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
|
|
|
|
|
|
|
// Release all recorded pointers before RETURN. Do this LAST!
|
2002-05-21 05:43:59 +08:00
|
|
|
if (!DisablePtrHashing)
|
2002-05-19 23:39:02 +08:00
|
|
|
InsertReleaseRecordedInst(exitBlocks[i],
|
|
|
|
externalFuncs.ReleaseOnReturnFunc);
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2001-10-18 13:28:08 +08:00
|
|
|
return true;
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|