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
|
|
|
|
|
|
|
#include "llvm/Transforms/Instrumentation/TraceValues.h"
|
|
|
|
#include "llvm/GlobalVariable.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"
|
2002-04-09 18:37:46 +00:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-03-26 18:01:55 +00:00
|
|
|
#include "llvm/Function.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"
|
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-05-20 21:43:59 +00:00
|
|
|
static cl::Flag DisablePtrHashing("tracedisablehashdisable",
|
|
|
|
"Disable pointer hashing", cl::NoFlags);
|
2002-05-19 15:39:02 +00:00
|
|
|
|
2002-05-20 21:43:59 +00:00
|
|
|
static cl::StringList TraceFuncName ("tracefunc", "trace only specific funct"
|
|
|
|
"ions", cl::NoFlags);
|
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.
|
|
|
|
//
|
|
|
|
inline bool
|
|
|
|
TraceThisFunction(Function* func)
|
|
|
|
{
|
|
|
|
if (TraceFuncName.getNumOccurances() == 0)
|
|
|
|
return true;
|
2002-05-20 21:43:59 +00:00
|
|
|
|
|
|
|
return std::find(TraceFuncName.begin(), TraceFuncName.end(), func->getName())
|
|
|
|
!= TraceFuncName.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-03-26 18:01:55 +00:00
|
|
|
bool TraceBasicBlockExits, TraceFunctionExits;
|
2002-05-19 15:39:02 +00:00
|
|
|
ExternalFuncs externalFuncs;
|
2002-02-26 21:46:54 +00:00
|
|
|
public:
|
2002-03-26 18:01:55 +00:00
|
|
|
InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
|
2002-02-26 21:46:54 +00:00
|
|
|
: TraceBasicBlockExits(traceBasicBlockExits),
|
2002-03-26 18:01:55 +00:00
|
|
|
TraceFunctionExits(traceFunctionExits) {}
|
2002-04-29 14:57:45 +00:00
|
|
|
|
|
|
|
const char *getPassName() const { return "Trace Code Insertion"; }
|
2002-02-26 21:46:54 +00:00
|
|
|
|
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-03-26 18:01:55 +00:00
|
|
|
static bool doit(Function *M, bool traceBasicBlockExits,
|
2002-05-19 15:39:02 +00:00
|
|
|
bool traceFunctionExits, ExternalFuncs& externalFuncs);
|
|
|
|
|
2002-04-14 06:15:24 +00:00
|
|
|
// runOnFunction - This method does the work.
|
2002-02-26 21:46:54 +00:00
|
|
|
//
|
2002-06-25 16:13:24 +00:00
|
|
|
bool runOnFunction(Function &F) {
|
|
|
|
return doit(&F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
|
2002-02-26 21:46:54 +00:00
|
|
|
}
|
2002-04-28 21:27:06 +00:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.preservesCFG();
|
|
|
|
}
|
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-02-26 21:46:54 +00:00
|
|
|
return new InsertTraceCode(false, true);
|
|
|
|
}
|
|
|
|
|
2002-04-14 06:15:24 +00:00
|
|
|
Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
|
2002-02-26 21:46:54 +00:00
|
|
|
return new InsertTraceCode(true, true);
|
|
|
|
}
|
|
|
|
|
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.
|
2001-12-14 16:26:05 +00:00
|
|
|
GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
|
|
|
|
"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)
|
|
|
|
//
|
|
|
|
return (opCode < Instruction::FirstOtherOp &&
|
|
|
|
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-04-14 06:15:24 +00:00
|
|
|
else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
|
|
|
|
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-05-19 15:39:02 +00:00
|
|
|
static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
|
|
|
|
string Message,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum) {
|
2001-12-14 16:26:05 +00:00
|
|
|
// Escape Message by replacing all % characters with %% chars.
|
|
|
|
unsigned Offset = 0;
|
|
|
|
while ((Offset = Message.find('%', Offset)) != string::npos) {
|
2002-04-14 06:15:24 +00:00
|
|
|
Message.replace(Offset, 1, "%%");
|
2001-12-14 16:26:05 +00:00
|
|
|
Offset += 2; // Skip over the new %'s
|
|
|
|
}
|
2001-10-18 05:28:08 +00:00
|
|
|
|
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 *
|
|
|
|
Instruction *GEP =
|
|
|
|
new GetElementPtrInst(fmtVal,
|
|
|
|
vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
|
|
|
|
"trstr");
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, GEP);
|
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);
|
|
|
|
if (V->getType() != SBP) { // Cast pointer to be sbyte*
|
|
|
|
Instruction *I = new CastInst(V, SBP, "Hash_cast");
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-20 21:43:59 +00:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<Value*> HashArgs(1, V);
|
2002-05-19 15:39:02 +00:00
|
|
|
V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
|
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);
|
|
|
|
Instruction *I = new CallInst(Printf, PrintArgs, "trace");
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2001-10-18 05:28:08 +00:00
|
|
|
}
|
2001-12-14 16:26:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
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-05-19 15:39:02 +00:00
|
|
|
InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
|
|
|
|
Printf, HashPtrToSeqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertReleaseInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
|
|
|
Function* ReleasePtrFunc) {
|
2002-05-20 21:43:59 +00: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-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-20 21:43:59 +00:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
vector<Value*> releaseArgs(1, V);
|
2002-05-19 15:39:02 +00:00
|
|
|
Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertRecordInst(Value *V, BasicBlock *BB,
|
|
|
|
BasicBlock::iterator &BBI,
|
|
|
|
Function* RecordPtrFunc) {
|
2002-05-20 21:43:59 +00: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-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-20 21:43:59 +00:00
|
|
|
V = I;
|
|
|
|
}
|
|
|
|
vector<Value*> releaseArgs(1, V);
|
2002-05-19 15:39:02 +00:00
|
|
|
Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, I);
|
2002-05-19 15:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertPushOnEntryFunc(Function *M,
|
|
|
|
Function* PushOnEntryFunc) {
|
|
|
|
// Get an iterator to point to the insertion location
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock &BB = M->getEntryNode();
|
|
|
|
BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
|
|
|
|
vector<Value*>()));
|
2001-10-18 18:16:11 +00:00
|
|
|
}
|
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
static void
|
|
|
|
InsertReleaseRecordedInst(BasicBlock *BB,
|
|
|
|
Function* ReleaseOnReturnFunc) {
|
2002-07-08 23:37:07 +00:00
|
|
|
BasicBlock::iterator BBI = --BB->end();
|
2002-06-25 16:13:24 +00:00
|
|
|
BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
|
|
|
|
vector<Value*>()));
|
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) {
|
|
|
|
|
|
|
|
for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
|
2002-06-25 16:13:24 +00:00
|
|
|
if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
|
2002-06-25 16:13:24 +00:00
|
|
|
else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
|
2002-05-19 15:39:02 +00:00
|
|
|
{
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock::iterator nextI = ++II;
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
|
2002-06-25 16:13:24 +00:00
|
|
|
II = --nextI;
|
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-07-08 23:37:07 +00:00
|
|
|
BasicBlock::iterator InsertPos = --BB->end();
|
|
|
|
assert(InsertPos->isTerminator());
|
2001-10-14 23:18:45 +00:00
|
|
|
|
2002-07-08 23:37:07 +00:00
|
|
|
#undef CANNOT_SAVE_CCR_ACROSS_CALLS
|
|
|
|
#ifdef CANNOT_SAVE_CCR_ACROSS_CALLS
|
|
|
|
//
|
|
|
|
// *** DISABLING THIS BECAUSE SAVING %CCR ACROSS CALLS WORKS NOW.
|
|
|
|
// *** DELETE THIS CODE AFTER SOME TESTING.
|
|
|
|
// *** NOTE: THIS CODE IS BROKEN ANYWAY WHEN THE SETCC IS NOT JUST
|
|
|
|
// *** BEFORE THE BRANCH.
|
|
|
|
// -- Vikram Adve, 7/7/02.
|
|
|
|
//
|
2001-10-28 21:37:25 +00: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-14 16:26:05 +00: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-25 16:13:24 +00:00
|
|
|
InsertPos = SetCC = I; // Back up until we can insert before the setcc
|
2001-10-18 18:16:11 +00:00
|
|
|
}
|
2002-07-08 23:37:07 +00:00
|
|
|
#endif CANNOT_SAVE_CCR_ACROSS_CALLS
|
2001-10-18 13:49:22 +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-07-08 23:37:07 +00:00
|
|
|
if (InsertPos != BB->begin()) { // there's at least one instr before InsertPos
|
|
|
|
BasicBlock::iterator II = BB->begin(), IEincl = InsertPos;
|
|
|
|
--IEincl;
|
|
|
|
do { // do from II up to IEincl, inclusive
|
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
|
|
|
|
assert(valuesStoredInFunction &&
|
|
|
|
"Should not be printing a store instruction at function exit");
|
|
|
|
LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
|
2002-06-25 16:13:24 +00:00
|
|
|
"reload."+SI->getPointerOperand()->getName());
|
2002-07-08 23:37:07 +00:00
|
|
|
InsertPos = ++BB->getInstList().insert(InsertPos, LI);
|
|
|
|
valuesStoredInFunction->push_back(LI);
|
|
|
|
}
|
|
|
|
if (ShouldTraceValue(II))
|
|
|
|
InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf,HashPtrToSeqNum);
|
|
|
|
} while (II++ != IEincl);
|
2001-12-14 16:26:05 +00:00
|
|
|
}
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
|
|
|
|
Function* HashPtrToSeqNum){
|
2001-10-18 18:16:11 +00:00
|
|
|
// Get an iterator to point to the insertion location
|
2002-06-25 16:13:24 +00:00
|
|
|
BasicBlock &BB = M->getEntryNode();
|
|
|
|
BasicBlock::iterator BBI = BB.begin();
|
2001-12-14 16:26:05 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
std::ostringstream OutStr;
|
2001-12-14 16:26:05 +00:00
|
|
|
WriteAsOperand(OutStr, M, true);
|
2002-06-25 16:13:24 +00:00
|
|
|
InsertPrintInst(0, &BB, BBI, "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-06-25 16:13:24 +00:00
|
|
|
for (Function::aiterator I = M->abegin(), E = M->aend(); I != E; ++I,++ArgNo){
|
|
|
|
InsertVerbosePrintInst(I, &BB, BBI,
|
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-07-08 23:37:07 +00:00
|
|
|
BasicBlock::iterator BBI = --BB->end();
|
2002-06-25 16:13:24 +00:00
|
|
|
ReturnInst &Ret = cast<ReturnInst>(BB->back());
|
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-05-19 15:39:02 +00:00
|
|
|
InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
|
|
|
|
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-06-25 16:13:24 +00:00
|
|
|
InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
|
2002-05-19 15:39:02 +00:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-14 23:18:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-26 18:01:55 +00:00
|
|
|
bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
|
2002-05-19 15:39:02 +00:00
|
|
|
bool traceFunctionEvents,
|
|
|
|
ExternalFuncs& externalFuncs) {
|
2002-03-26 18:01:55 +00:00
|
|
|
if (!traceBasicBlockExits && !traceFunctionEvents)
|
2001-12-14 16:26:05 +00:00
|
|
|
return false;
|
2001-10-18 13:49:22 +00:00
|
|
|
|
2002-05-19 15:39:02 +00:00
|
|
|
if (!TraceThisFunction(M))
|
|
|
|
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-03-26 18:01:55 +00:00
|
|
|
if (traceFunctionEvents)
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
|
|
|
|
|
|
|
// Push a pointer set for recording alloca'd pointers at entry.
|
2002-05-20 21:43:59 +00:00
|
|
|
if (!DisablePtrHashing)
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
|
2001-10-18 18:16:11 +00:00
|
|
|
|
2002-06-25 16:13:24 +00:00
|
|
|
for (Function::iterator BB = M->begin(); BB != M->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
|
|
|
|
|
|
|
|
if (traceBasicBlockExits)
|
2002-05-19 15:39:02 +00:00
|
|
|
TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc, &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
|
|
|
|
|
|
|
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-20 21:43:59 +00:00
|
|
|
if (!DisablePtrHashing)
|
2002-05-19 15:39:02 +00:00
|
|
|
InsertReleaseRecordedInst(exitBlocks[i],
|
|
|
|
externalFuncs.ReleaseOnReturnFunc);
|
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
|
|
|
}
|