blob: ca5087a02073d84c850fe85c353bd2d562197a71 [file] [log] [blame]
Chris Lattnerf1197b02001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner7e358902002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattnerf1197b02001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advea200a6c2001-10-14 23:18:45 +00007
Chris Lattner57fd3072003-01-14 22:39:29 +00008#include "llvm/Transforms/Instrumentation.h"
Chris Lattnerca142372002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000010#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000011#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000012#include "llvm/iTerminators.h"
13#include "llvm/iOther.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000014#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000016#include "llvm/Assembly/Writer.h"
Vikram S. Adve47f37c32002-05-19 15:39:02 +000017#include "Support/CommandLine.h"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include "Support/StringExtras.h"
Chris Lattnerace7b8d2002-05-20 21:43:59 +000019#include <algorithm>
Chris Lattnere2c61262001-10-18 20:06:03 +000020#include <sstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000021using std::vector;
22using std::string;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000023
Chris Lattnerf5cad152002-07-22 02:10:13 +000024static cl::opt<bool>
25DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
Chris Lattner01587d42003-04-13 03:50:14 +000026 cl::desc("Disable pointer hashing in the -trace or -tracem "
27 "passes"));
Vikram S. Adve47f37c32002-05-19 15:39:02 +000028
Chris Lattnerf5cad152002-07-22 02:10:13 +000029static cl::list<string>
Chris Lattner01587d42003-04-13 03:50:14 +000030TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
31 "-trace or -tracem passes"),
Chris Lattnerb9636a72003-01-13 00:52:14 +000032 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000033
Chris Lattner6d216fd2002-07-23 18:04:15 +000034static void TraceValuesAtBBExit(BasicBlock *BB,
35 Function *Printf, Function* HashPtrToSeqNum,
36 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000037
38// We trace a particular function if no functions to trace were specified
39// or if the function is in the specified list.
40//
Chris Lattner6d216fd2002-07-23 18:04:15 +000041inline static bool
Chris Lattnerb9636a72003-01-13 00:52:14 +000042TraceThisFunction(Function &F)
Vikram S. Adve47f37c32002-05-19 15:39:02 +000043{
Chris Lattnerb9636a72003-01-13 00:52:14 +000044 if (TraceFuncNames.empty()) return true;
Chris Lattnerace7b8d2002-05-20 21:43:59 +000045
Chris Lattnerb9636a72003-01-13 00:52:14 +000046 return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
47 != TraceFuncNames.end();
Vikram S. Adve47f37c32002-05-19 15:39:02 +000048}
49
50
Chris Lattner04805fa2002-02-26 21:46:54 +000051namespace {
Chris Lattnerace7b8d2002-05-20 21:43:59 +000052 struct ExternalFuncs {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000053 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
54 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner113f4f42002-06-25 16:13:24 +000055 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve47f37c32002-05-19 15:39:02 +000056 };
57
Chris Lattnerc8e66542002-04-27 06:56:12 +000058 class InsertTraceCode : public FunctionPass {
Chris Lattner6d216fd2002-07-23 18:04:15 +000059 protected:
Vikram S. Adve47f37c32002-05-19 15:39:02 +000060 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000061 public:
Chris Lattner04805fa2002-02-26 21:46:54 +000062
Vikram S. Adve47f37c32002-05-19 15:39:02 +000063 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000064 //
Chris Lattner113f4f42002-06-25 16:13:24 +000065 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000066
67 //--------------------------------------------------------------------------
68 // Function InsertCodeToTraceValues
69 //
Chris Lattner7e358902002-04-14 06:15:24 +000070 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000071 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000072 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000073 bool doit(Function *M);
74
75 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) = 0;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000076
Chris Lattner7e358902002-04-14 06:15:24 +000077 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000078 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000079 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000080
81 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000082 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000083 }
Chris Lattner04805fa2002-02-26 21:46:54 +000084 };
Chris Lattner6d216fd2002-07-23 18:04:15 +000085
86 struct FunctionTracer : public InsertTraceCode {
87 // Ignore basic blocks here...
88 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {}
89 };
90
91 struct BasicBlockTracer : public InsertTraceCode {
92 // Trace basic blocks here...
93 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {
94 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
95 externalFuncs.HashPtrFunc, &VI);
96 }
97 };
98
99 // Register the passes...
Chris Lattnerc8b70922002-07-26 21:12:46 +0000100 RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
101 RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
Chris Lattner04805fa2002-02-26 21:46:54 +0000102} // end anonymous namespace
103
104
Chris Lattnerc8e66542002-04-27 06:56:12 +0000105Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000106 return new FunctionTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000107}
108
Chris Lattner7e358902002-04-14 06:15:24 +0000109Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000110 return new BasicBlockTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000111}
112
Chris Lattner6d216fd2002-07-23 18:04:15 +0000113
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000114// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000115//
Chris Lattner113f4f42002-06-25 16:13:24 +0000116void ExternalFuncs::doInitialization(Module &M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000117 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000118 const FunctionType *MTy =
119 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000120 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000121
122 // uint (sbyte*)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000123 const FunctionType *hashFuncTy =
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000124 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner113f4f42002-06-25 16:13:24 +0000125 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000126
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000127 // void (sbyte*)
128 const FunctionType *voidSBPFuncTy =
129 FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000130
Chris Lattner113f4f42002-06-25 16:13:24 +0000131 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
132 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000133
134 const FunctionType *voidvoidFuncTy =
135 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
136
Chris Lattner113f4f42002-06-25 16:13:24 +0000137 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
138 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000139 voidvoidFuncTy);
140}
141
142
143// Add a prototype for external functions used by the tracing code.
144//
Chris Lattner113f4f42002-06-25 16:13:24 +0000145bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000146 externalFuncs.doInitialization(M);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000147 return false;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000148}
149
Chris Lattnerf1197b02001-12-14 16:26:05 +0000150
151static inline GlobalVariable *getStringRef(Module *M, const string &str) {
152 // Create a constant internal string reference...
153 Constant *Init = ConstantArray::get(str);
Vikram S. Adve9f129ff2002-03-18 03:40:25 +0000154
155 // Create the global variable and record it in the module
156 // The GV will be renamed to a unique name if needed.
Chris Lattner379a8d22003-04-16 20:28:45 +0000157 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
158 GlobalValue::InternalLinkage, Init,
Chris Lattnerf1197b02001-12-14 16:26:05 +0000159 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +0000160 M->getGlobalList().push_back(GV);
161 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000162}
163
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000164
Chris Lattnerf1197b02001-12-14 16:26:05 +0000165//
166// Check if this instruction has any uses outside its basic block,
Vikram S. Adveac83df72003-07-11 21:57:43 +0000167// or if it used by either a Call or Return instruction (ditto).
168// (Values stored to memory within this BB are live at end of BB but are
169// traced at the store instruction, not where they are computed.)
Chris Lattnerf1197b02001-12-14 16:26:05 +0000170//
171static inline bool LiveAtBBExit(const Instruction* I) {
172 const BasicBlock *BB = I->getParent();
173 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
174 if (const Instruction *UI = dyn_cast<Instruction>(*U))
175 if (UI->getParent() != BB || isa<ReturnInst>(UI))
176 return true;
177
178 return false;
179}
180
181
182static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000183 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000184 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000185 //
Chris Lattner69ce8672002-10-13 19:39:16 +0000186 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000187 opCode != Instruction::Alloca &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000188 opCode != Instruction::PHINode &&
189 opCode != Instruction::Cast);
190}
191
Chris Lattnerf1197b02001-12-14 16:26:05 +0000192
Vikram S. Adveac83df72003-07-11 21:57:43 +0000193// Trace a value computed by an instruction if it is non-void, it is computed
194// by a real computation, not just a copy (see TraceThisOpCode), and
195// -- it is a load instruction: we want to check values read from memory
196// -- or it is live at exit from the basic block (i.e., ignore local temps)
197//
Chris Lattnerf1197b02001-12-14 16:26:05 +0000198static bool ShouldTraceValue(const Instruction *I) {
199 return
Vikram S. Adveac83df72003-07-11 21:57:43 +0000200 I->getType() != Type::VoidTy &&
201 TraceThisOpCode(I->getOpcode()) &&
202 (isa<LoadInst>(I) || LiveAtBBExit(I));
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000203}
204
Chris Lattnerf1197b02001-12-14 16:26:05 +0000205static string getPrintfCodeFor(const Value *V) {
206 if (V == 0) return "";
Chris Lattner7e358902002-04-14 06:15:24 +0000207 if (V->getType()->isFloatingPoint())
208 return "%g";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000209 else if (V->getType() == Type::LabelTy)
Chris Lattner7e358902002-04-14 06:15:24 +0000210 return "0x%p";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000211 else if (isa<PointerType>(V->getType()))
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000212 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattnerbc6bdc22002-09-03 01:07:35 +0000213 else if (V->getType()->isIntegral())
Chris Lattnerf1197b02001-12-14 16:26:05 +0000214 return "%d";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000215
Chris Lattner7e358902002-04-14 06:15:24 +0000216 assert(0 && "Illegal value to print out...");
217 return "";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000218}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000219
220
Chris Lattner28a8d242002-09-10 17:04:02 +0000221static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000222 string Message,
223 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000224 // Escape Message by replacing all % characters with %% chars.
Chris Lattner26783a52002-10-17 16:22:08 +0000225 string Tmp;
226 std::swap(Tmp, Message);
227 string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
228 while (I != Tmp.end()) {
229 Message.append(Tmp.begin(), I);
230 Message += "%%";
231 ++I; // Make sure to erase the % as well...
232 Tmp.erase(Tmp.begin(), I);
233 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattnerf1197b02001-12-14 16:26:05 +0000234 }
Chris Lattnerb9636a72003-01-13 00:52:14 +0000235 Message += Tmp;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000236 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000237
Chris Lattner5309e102001-10-18 06:03:05 +0000238 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000239 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
240
241 // Turn the format string into an sbyte *
Chris Lattner3cc30182003-06-05 04:48:18 +0000242 Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
243 vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
Chris Lattner5309e102001-10-18 06:03:05 +0000244
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000245 // Insert a call to the hash function if this is a pointer value
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000246 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
247 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000248 if (V->getType() != SBP) // Cast pointer to be sbyte*
249 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000250
251 vector<Value*> HashArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000252 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000253 }
254
Chris Lattner5309e102001-10-18 06:03:05 +0000255 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000256 vector<Value*> PrintArgs;
257 PrintArgs.push_back(GEP);
258 if (V) PrintArgs.push_back(V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000259 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000260}
261
Chris Lattner5309e102001-10-18 06:03:05 +0000262
Chris Lattnerf1197b02001-12-14 16:26:05 +0000263static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000264 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000265 const string &Message, Function *Printf,
266 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000267 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000268 if (V) WriteAsOperand(OutStr, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000269 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000270 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000271}
272
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000273static void
274InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000275 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000276 Function* ReleasePtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000277
278 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000279 if (V->getType() != SBP) // Cast pointer to be sbyte*
280 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
281
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000282 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000283 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000284}
285
286static void
287InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000288 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000289 Function* RecordPtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000290 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000291 if (V->getType() != SBP) // Cast pointer to be sbyte*
292 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
293
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000294 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000295 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000296}
297
298// Look for alloca and free instructions. These are the ptrs to release.
299// Release the free'd pointers immediately. Record the alloca'd pointers
300// to be released on return from the current function.
301//
302static void
303ReleasePtrSeqNumbers(BasicBlock *BB,
304 ExternalFuncs& externalFuncs) {
305
Chris Lattner28a8d242002-09-10 17:04:02 +0000306 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattner889f6202003-04-23 16:37:45 +0000307 if (FreeInst *FI = dyn_cast<FreeInst>(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000308 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattner889f6202003-04-23 16:37:45 +0000309 else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000310 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000311}
312
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000313
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000314// Insert print instructions at the end of basic block BB for each value
315// computed in BB that is live at the end of BB,
316// or that is stored to memory in BB.
317// If the value is stored to memory, we load it back before printing it
Chris Lattner57698e22002-03-26 18:01:55 +0000318// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-04-14 06:15:24 +0000319// for printing at the exit from the function. (Note that in each invocation
320// of the function, this will only get the last value stored for each static
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000321// store instruction).
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000322//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000323static void TraceValuesAtBBExit(BasicBlock *BB,
324 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner57698e22002-03-26 18:01:55 +0000325 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000326 // Get an iterator to point to the insertion location, which is
327 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000328 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000329 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000330
Chris Lattner7f74a562002-01-20 22:54:45 +0000331 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000332 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000333 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
334 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000335
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000336 // Insert a print instruction for each instruction preceding InsertPos.
337 // The print instructions must go before InsertPos, so we use the
338 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000339 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000340 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
Chris Lattner889f6202003-04-23 16:37:45 +0000341 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Vikram S. Adveac83df72003-07-11 21:57:43 +0000342 // Trace the stored value and address
343 InsertVerbosePrintInst(SI->getOperand(0), BB, InsertPos,
344 " (store value) ", Printf, HashPtrToSeqNum);
345 InsertVerbosePrintInst(SI->getOperand(1), BB, InsertPos,
346 " (store addr ) ", Printf, HashPtrToSeqNum);
Chris Lattner28a8d242002-09-10 17:04:02 +0000347 }
Vikram S. Adveac83df72003-07-11 21:57:43 +0000348 else if (ShouldTraceValue(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000349 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000350 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000351}
352
Chris Lattner6d216fd2002-07-23 18:04:15 +0000353static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000354 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000355 // Get an iterator to point to the insertion location
Chris Lattner6d216fd2002-07-23 18:04:15 +0000356 BasicBlock &BB = F.getEntryNode();
Chris Lattner28a8d242002-09-10 17:04:02 +0000357 Instruction *InsertPos = BB.begin();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000358
Chris Lattner7f74a562002-01-20 22:54:45 +0000359 std::ostringstream OutStr;
Chris Lattnerb9636a72003-01-13 00:52:14 +0000360 WriteAsOperand(OutStr, &F);
Chris Lattner28a8d242002-09-10 17:04:02 +0000361 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000362 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000363
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000364 // Now print all the incoming arguments
Chris Lattnerf1197b02001-12-14 16:26:05 +0000365 unsigned ArgNo = 0;
Chris Lattner6d216fd2002-07-23 18:04:15 +0000366 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner28a8d242002-09-10 17:04:02 +0000367 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000368 " Arg #" + utostr(ArgNo) + ": ", Printf,
369 HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000370 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000371}
372
373
Chris Lattner57698e22002-03-26 18:01:55 +0000374static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000375 Function *Printf,
376 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000377 // Get an iterator to point to the insertion location
Chris Lattner28a8d242002-09-10 17:04:02 +0000378 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000379
Chris Lattner7f74a562002-01-20 22:54:45 +0000380 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000381 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner28a8d242002-09-10 17:04:02 +0000382 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000383 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000384
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000385 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000386 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner28a8d242002-09-10 17:04:02 +0000387 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000388 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000389}
390
391
Chris Lattner6d216fd2002-07-23 18:04:15 +0000392bool InsertTraceCode::runOnFunction(Function &F) {
393 if (!TraceThisFunction(F))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000394 return false;
395
Chris Lattner57698e22002-03-26 18:01:55 +0000396 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000397 vector<BasicBlock*> exitBlocks;
398
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000399 // Insert code to trace values at function entry
Chris Lattner6d216fd2002-07-23 18:04:15 +0000400 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
401 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000402
403 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000404 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000405 new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
406 F.getEntryNode().begin());
407
Chris Lattner6d216fd2002-07-23 18:04:15 +0000408 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000409 if (isa<ReturnInst>(BB->getTerminator()))
410 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner6d216fd2002-07-23 18:04:15 +0000411
412 // Insert trace code if this basic block is interesting...
413 handleBasicBlock(BB, valuesStoredInFunction);
414
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000415 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000416 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000417 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000418
Chris Lattner6d216fd2002-07-23 18:04:15 +0000419 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000420 {
421 // Insert code to trace values at function exit
Chris Lattner6d216fd2002-07-23 18:04:15 +0000422 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
423 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000424
425 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000426 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000427 new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
428 exitBlocks[i]->getTerminator());
Chris Lattnerf1197b02001-12-14 16:26:05 +0000429 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000430
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000431 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000432}