2003-10-13 11:32:08 +08:00
|
|
|
//===- TraceValues.cpp - Value Tracing for debugging ----------------------===//
|
2003-10-21 03:43:21 +08: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-15 00:26:05 +08:00
|
|
|
//
|
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
|
|
|
|
2003-01-15 06:39:29 +08:00
|
|
|
#include "llvm/Transforms/Instrumentation.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"
|
|
|
|
#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>
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2002-07-22 10:10:13 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
|
2003-04-13 11:50:14 +08:00
|
|
|
cl::desc("Disable pointer hashing in the -trace or -tracem "
|
|
|
|
"passes"));
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
static cl::list<std::string>
|
2003-04-13 11:50:14 +08:00
|
|
|
TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
|
|
|
|
"-trace or -tracem passes"),
|
2003-01-13 08:52:14 +08:00
|
|
|
cl::value_desc("function"), cl::Hidden);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
static void TraceValuesAtBBExit(BasicBlock *BB,
|
|
|
|
Function *Printf, Function* HashPtrToSeqNum,
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Instruction*> *valuesStoredInFunction);
|
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.
|
|
|
|
//
|
2002-07-24 02:04:15 +08:00
|
|
|
inline static bool
|
2003-01-13 08:52:14 +08:00
|
|
|
TraceThisFunction(Function &F)
|
2002-05-19 23:39:02 +08:00
|
|
|
{
|
2003-01-13 08:52:14 +08:00
|
|
|
if (TraceFuncNames.empty()) return true;
|
2002-05-21 05:43:59 +08:00
|
|
|
|
2003-01-13 08:52:14 +08:00
|
|
|
return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
|
|
|
|
!= TraceFuncNames.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-07-24 02:04:15 +08:00
|
|
|
protected:
|
2002-05-19 23:39:02 +08:00
|
|
|
ExternalFuncs externalFuncs;
|
2002-02-27 05:46:54 +08:00
|
|
|
public:
|
|
|
|
|
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-07-24 02:04:15 +08:00
|
|
|
bool doit(Function *M);
|
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) = 0;
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-04-14 14:15:24 +08:00
|
|
|
// runOnFunction - This method does the work.
|
2002-02-27 05:46:54 +08:00
|
|
|
//
|
2002-07-24 02:04:15 +08:00
|
|
|
bool runOnFunction(Function &F);
|
2002-04-29 05:27:06 +08:00
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
2002-10-22 04:00:28 +08:00
|
|
|
AU.setPreservesCFG();
|
2002-04-29 05:27:06 +08:00
|
|
|
}
|
2002-02-27 05:46:54 +08:00
|
|
|
};
|
2002-07-24 02:04:15 +08:00
|
|
|
|
|
|
|
struct FunctionTracer : public InsertTraceCode {
|
|
|
|
// Ignore basic blocks here...
|
2003-10-28 05:44:09 +08:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) {}
|
2002-07-24 02:04:15 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct BasicBlockTracer : public InsertTraceCode {
|
|
|
|
// Trace basic blocks here...
|
2003-10-28 05:44:09 +08:00
|
|
|
virtual void handleBasicBlock(BasicBlock *BB,
|
|
|
|
std::vector<Instruction*> &VI) {
|
2002-07-24 02:04:15 +08:00
|
|
|
TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc, &VI);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Register the passes...
|
2002-07-27 05:12:46 +08:00
|
|
|
RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
|
|
|
|
RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
|
2002-02-27 05:46:54 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
|
2004-01-09 14:12:26 +08:00
|
|
|
Pass *llvm::createTraceValuesPassForFunction() { // Just trace functions
|
2002-07-24 02:04:15 +08:00
|
|
|
return new FunctionTracer();
|
2002-02-27 05:46:54 +08:00
|
|
|
}
|
|
|
|
|
2004-01-09 14:12:26 +08:00
|
|
|
Pass *llvm::createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
|
2002-07-24 02:04:15 +08:00
|
|
|
return new BasicBlockTracer();
|
2002-02-27 05:46:54 +08:00
|
|
|
}
|
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
|
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 =
|
2003-10-28 05:44:09 +08:00
|
|
|
FunctionType::get(Type::IntTy, std::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*)
|
2003-08-31 08:20:36 +08:00
|
|
|
HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", Type::UIntTy, SBP,
|
|
|
|
0);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2002-05-21 05:43:59 +08:00
|
|
|
// void (sbyte*)
|
2003-08-31 08:20:36 +08:00
|
|
|
ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum",
|
|
|
|
Type::VoidTy, SBP, 0);
|
|
|
|
RecordPtrFunc = M.getOrInsertFunction("RecordPointer",
|
|
|
|
Type::VoidTy, SBP, 0);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
2003-08-31 08:20:36 +08:00
|
|
|
PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", Type::VoidTy, 0);
|
2002-06-26 00:13:24 +08:00
|
|
|
ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
|
2003-08-31 08:20:36 +08:00
|
|
|
Type::VoidTy, 0);
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
static inline GlobalVariable *getStringRef(Module *M, const std::string &str) {
|
2001-12-15 00:26:05 +08:00
|
|
|
// 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.
|
2003-04-17 04:28:45 +08:00
|
|
|
GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
|
|
|
|
GlobalValue::InternalLinkage, Init,
|
2001-12-15 00:26:05 +08:00
|
|
|
"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,
|
2003-07-12 05:57:43 +08: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-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)
|
|
|
|
//
|
2002-10-14 03:39:16 +08:00
|
|
|
return (opCode < Instruction::OtherOpsBegin &&
|
2001-12-15 00:26:05 +08:00
|
|
|
opCode != Instruction::Alloca &&
|
2003-10-20 05:34:28 +08:00
|
|
|
opCode != Instruction::PHI &&
|
2001-12-15 00:26:05 +08:00
|
|
|
opCode != Instruction::Cast);
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
2001-10-19 04:06:03 +08:00
|
|
|
|
2003-07-12 05:57:43 +08: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-15 00:26:05 +08:00
|
|
|
static bool ShouldTraceValue(const Instruction *I) {
|
|
|
|
return
|
2003-07-12 05:57:43 +08:00
|
|
|
I->getType() != Type::VoidTy &&
|
|
|
|
TraceThisOpCode(I->getOpcode()) &&
|
|
|
|
(isa<LoadInst>(I) || LiveAtBBExit(I));
|
2001-10-18 21:49:22 +08:00
|
|
|
}
|
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
static std::string getPrintfCodeFor(const Value *V) {
|
2001-12-15 00:26:05 +08:00
|
|
|
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-09-03 09:07:35 +08:00
|
|
|
else if (V->getType()->isIntegral())
|
2002-04-14 14:15:24 +08:00
|
|
|
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-09-11 01:04:02 +08:00
|
|
|
static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
|
2003-10-28 05:44:09 +08:00
|
|
|
std::string Message,
|
2002-05-19 23:39:02 +08:00
|
|
|
Function *Printf, Function* HashPtrToSeqNum) {
|
2001-12-15 00:26:05 +08:00
|
|
|
// Escape Message by replacing all % characters with %% chars.
|
2003-10-28 05:44:09 +08:00
|
|
|
std::string Tmp;
|
2002-10-18 00:22:08 +08:00
|
|
|
std::swap(Tmp, Message);
|
2003-10-28 05:44:09 +08:00
|
|
|
std::string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
|
2002-10-18 00:22:08 +08: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-15 00:26:05 +08:00
|
|
|
}
|
2003-01-13 08:52:14 +08:00
|
|
|
Message += Tmp;
|
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 *
|
2003-06-05 12:48:18 +08:00
|
|
|
Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
|
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);
|
2002-09-11 01:04:02 +08:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
|
2002-05-21 05:43:59 +08:00
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Value*> HashArgs(1, V);
|
2002-09-11 01:04:02 +08:00
|
|
|
V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
|
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:
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Value*> PrintArgs;
|
2001-12-15 00:26:05 +08:00
|
|
|
PrintArgs.push_back(GEP);
|
|
|
|
if (V) PrintArgs.push_back(V);
|
2002-09-11 01:04:02 +08:00
|
|
|
new CallInst(Printf, PrintArgs, "trace", InsertBefore);
|
2001-10-18 13:28:08 +08:00
|
|
|
}
|
2001-12-15 00:26:05 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
|
2002-09-11 01:04:02 +08:00
|
|
|
Instruction *InsertBefore,
|
2003-10-28 05:44:09 +08:00
|
|
|
const std::string &Message, Function *Printf,
|
2002-05-19 23:39:02 +08:00
|
|
|
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-09-11 01:04:02 +08:00
|
|
|
InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
|
2002-05-19 23:39:02 +08:00
|
|
|
Printf, HashPtrToSeqNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertReleaseInst(Value *V, BasicBlock *BB,
|
2002-09-11 01:04:02 +08:00
|
|
|
Instruction *InsertBefore,
|
2002-05-19 23:39:02 +08:00
|
|
|
Function* ReleasePtrFunc) {
|
2002-05-21 05:43:59 +08:00
|
|
|
|
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-09-11 01:04:02 +08:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
|
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Value*> releaseArgs(1, V);
|
2002-09-11 01:04:02 +08:00
|
|
|
new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
InsertRecordInst(Value *V, BasicBlock *BB,
|
2002-09-11 01:04:02 +08:00
|
|
|
Instruction *InsertBefore,
|
2002-05-19 23:39:02 +08:00
|
|
|
Function* RecordPtrFunc) {
|
2002-05-21 05:43:59 +08:00
|
|
|
const Type *SBP = PointerType::get(Type::SByteTy);
|
2002-09-11 01:04:02 +08:00
|
|
|
if (V->getType() != SBP) // Cast pointer to be sbyte*
|
|
|
|
V = new CastInst(V, SBP, "RP_cast", InsertBefore);
|
2001-10-19 02:16:11 +08:00
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Value*> releaseArgs(1, V);
|
2002-09-11 01:04:02 +08:00
|
|
|
new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
|
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) {
|
|
|
|
|
2002-09-11 01:04:02 +08:00
|
|
|
for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
|
2003-04-24 00:37:45 +08:00
|
|
|
if (FreeInst *FI = dyn_cast<FreeInst>(II))
|
2002-09-11 01:04:02 +08:00
|
|
|
InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
|
2003-04-24 00:37:45 +08:00
|
|
|
else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
|
2002-09-11 01:04:02 +08:00
|
|
|
InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
|
2002-05-19 23:39:02 +08:00
|
|
|
}
|
|
|
|
|
2001-10-15 07:18:45 +08:00
|
|
|
|
2002-07-09 07:37:07 +08: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-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).
|
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,
|
2003-10-28 05:44:09 +08:00
|
|
|
std::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-09-11 01:04:02 +08:00
|
|
|
TerminatorInst *InsertPos = BB->getTerminator();
|
2001-10-15 07:18:45 +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
|
|
|
|
2002-07-09 07:37:07 +08: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-15 00:26:05 +08:00
|
|
|
//
|
2002-09-11 01:04:02 +08:00
|
|
|
for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
|
2003-04-24 00:37:45 +08:00
|
|
|
if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
|
2003-07-12 05:57:43 +08: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-11 01:04:02 +08:00
|
|
|
}
|
2003-07-12 05:57:43 +08:00
|
|
|
else if (ShouldTraceValue(II))
|
2002-09-11 01:04:02 +08:00
|
|
|
InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
|
2001-12-15 00:26:05 +08:00
|
|
|
}
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
|
2002-05-19 23:39:02 +08:00
|
|
|
Function* HashPtrToSeqNum){
|
2001-10-19 02:16:11 +08:00
|
|
|
// Get an iterator to point to the insertion location
|
2003-09-20 22:39:18 +08:00
|
|
|
BasicBlock &BB = F.getEntryBlock();
|
2002-09-11 01:04:02 +08:00
|
|
|
Instruction *InsertPos = BB.begin();
|
2001-12-15 00:26:05 +08:00
|
|
|
|
2002-01-21 06:54:45 +08:00
|
|
|
std::ostringstream OutStr;
|
2003-01-13 08:52:14 +08:00
|
|
|
WriteAsOperand(OutStr, &F);
|
2002-09-11 01:04:02 +08:00
|
|
|
InsertPrintInst(0, &BB, InsertPos, "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-07-24 02:04:15 +08:00
|
|
|
for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
|
2002-09-11 01:04:02 +08:00
|
|
|
InsertVerbosePrintInst(I, &BB, InsertPos,
|
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-09-11 01:04:02 +08:00
|
|
|
ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
|
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-09-11 01:04:02 +08:00
|
|
|
InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
|
2002-05-19 23:39:02 +08:00
|
|
|
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-09-11 01:04:02 +08:00
|
|
|
InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
|
2002-05-19 23:39:02 +08:00
|
|
|
Printf, HashPtrToSeqNum);
|
2001-10-15 07:18:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
bool InsertTraceCode::runOnFunction(Function &F) {
|
|
|
|
if (!TraceThisFunction(F))
|
2002-05-19 23:39:02 +08:00
|
|
|
return false;
|
|
|
|
|
2003-10-28 05:44:09 +08:00
|
|
|
std::vector<Instruction*> valuesStoredInFunction;
|
|
|
|
std::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-07-24 02:04:15 +08:00
|
|
|
InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
|
|
|
// Push a pointer set for recording alloca'd pointers at entry.
|
2002-05-21 05:43:59 +08:00
|
|
|
if (!DisablePtrHashing)
|
2003-10-28 05:44:09 +08:00
|
|
|
new CallInst(externalFuncs.PushOnEntryFunc, std::vector<Value*>(), "",
|
2003-09-20 22:39:18 +08:00
|
|
|
F.getEntryBlock().begin());
|
2002-09-11 01:04:02 +08:00
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
for (Function::iterator BB = F.begin(); BB != F.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
|
2002-07-24 02:04:15 +08:00
|
|
|
|
|
|
|
// Insert trace code if this basic block is interesting...
|
|
|
|
handleBasicBlock(BB, 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
|
|
|
|
2002-07-24 02:04:15 +08:00
|
|
|
for (unsigned i=0; i != exitBlocks.size(); ++i)
|
2002-05-19 23:39:02 +08:00
|
|
|
{
|
|
|
|
// Insert code to trace values at function exit
|
2002-07-24 02:04:15 +08:00
|
|
|
InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
|
|
|
|
externalFuncs.HashPtrFunc);
|
2002-05-19 23:39:02 +08:00
|
|
|
|
|
|
|
// Release all recorded pointers before RETURN. Do this LAST!
|
2002-05-21 05:43:59 +08:00
|
|
|
if (!DisablePtrHashing)
|
2003-10-28 05:44:09 +08:00
|
|
|
new CallInst(externalFuncs.ReleaseOnReturnFunc, std::vector<Value*>(),
|
|
|
|
"", exitBlocks[i]->getTerminator());
|
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
|
|
|
}
|