blob: 079edced6410d343ad9e086eaed48c4dd73044cb [file] [log] [blame]
Chris Lattner29c14732001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner649f5dd2002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattner29c14732001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advedf1892f2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
9#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000010#include "llvm/Constants.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Adve631b9a32001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Chris Lattner42a41272002-04-09 18:37:46 +000015#include "llvm/BasicBlock.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000016#include "llvm/Function.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000017#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000020#include "Support/CommandLine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000021#include "Support/StringExtras.h"
Chris Lattner2e0769e2002-05-20 21:43:59 +000022#include <algorithm>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000023#include <sstream>
Chris Lattner697954c2002-01-20 22:54:45 +000024using std::vector;
25using std::string;
Vikram S. Adved8893302001-10-28 21:37:25 +000026
Chris Lattner2e0769e2002-05-20 21:43:59 +000027static cl::Flag DisablePtrHashing("tracedisablehashdisable",
28 "Disable pointer hashing", cl::NoFlags);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000029
Chris Lattner2e0769e2002-05-20 21:43:59 +000030static cl::StringList TraceFuncName ("tracefunc", "trace only specific funct"
31 "ions", cl::NoFlags);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000032
33
34// We trace a particular function if no functions to trace were specified
35// or if the function is in the specified list.
36//
37inline bool
38TraceThisFunction(Function* func)
39{
40 if (TraceFuncName.getNumOccurances() == 0)
41 return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000042
43 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func->getName())
44 != TraceFuncName.end();
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000045}
46
47
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048namespace {
Chris Lattner2e0769e2002-05-20 21:43:59 +000049 struct ExternalFuncs {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000050 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
51 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
52 void doInitialization(Module *M); // Add prototypes for external functions
53 };
54
Chris Lattnerf57b8452002-04-27 06:56:12 +000055 class InsertTraceCode : public FunctionPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000056 bool TraceBasicBlockExits, TraceFunctionExits;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000057 ExternalFuncs externalFuncs;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000058 public:
Chris Lattner79df7c02002-03-26 18:01:55 +000059 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattnerbd0ef772002-02-26 21:46:54 +000060 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner79df7c02002-03-26 18:01:55 +000061 TraceFunctionExits(traceFunctionExits) {}
Chris Lattner96c466b2002-04-29 14:57:45 +000062
63 const char *getPassName() const { return "Trace Code Insertion"; }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000065 // Add a prototype for runtime functions not already in the program.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066 //
67 bool doInitialization(Module *M);
68
69 //--------------------------------------------------------------------------
70 // Function InsertCodeToTraceValues
71 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000072 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000073 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000074 //
Chris Lattner79df7c02002-03-26 18:01:55 +000075 static bool doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000076 bool traceFunctionExits, ExternalFuncs& externalFuncs);
77
Chris Lattner649f5dd2002-04-14 06:15:24 +000078 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000079 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000080 bool runOnFunction(Function *F) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000081 return doit(F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000082 }
Chris Lattner97e52e42002-04-28 21:27:06 +000083
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
85 AU.preservesCFG();
86 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000087 };
88} // end anonymous namespace
89
90
Chris Lattnerf57b8452002-04-27 06:56:12 +000091Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000092 return new InsertTraceCode(false, true);
93}
94
Chris Lattner649f5dd2002-04-14 06:15:24 +000095Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000096 return new InsertTraceCode(true, true);
97}
98
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000099// Add a prototype for external functions used by the tracing code.
Chris Lattner29c14732001-12-14 16:26:05 +0000100//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000101void ExternalFuncs::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +0000102 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000103 const FunctionType *MTy =
104 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner89851072002-03-29 03:43:24 +0000105 PrintfFunc = M->getOrInsertFunction("printf", MTy);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000106
107 // uint (sbyte*)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000108 const FunctionType *hashFuncTy =
Chris Lattner2e0769e2002-05-20 21:43:59 +0000109 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000110 HashPtrFunc = M->getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
111
Chris Lattner2e0769e2002-05-20 21:43:59 +0000112 // void (sbyte*)
113 const FunctionType *voidSBPFuncTy =
114 FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000115
Chris Lattner2e0769e2002-05-20 21:43:59 +0000116 ReleasePtrFunc =M->getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
117 RecordPtrFunc = M->getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000118
119 const FunctionType *voidvoidFuncTy =
120 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
121
122 PushOnEntryFunc = M->getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
123 ReleaseOnReturnFunc = M->getOrInsertFunction("ReleasePointersPopSet",
124 voidvoidFuncTy);
125}
126
127
128// Add a prototype for external functions used by the tracing code.
129//
130bool InsertTraceCode::doInitialization(Module *M) {
131 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000132 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000133}
134
Chris Lattner29c14732001-12-14 16:26:05 +0000135
136static inline GlobalVariable *getStringRef(Module *M, const string &str) {
137 // Create a constant internal string reference...
138 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +0000139
140 // Create the global variable and record it in the module
141 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +0000142 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
143 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000144 M->getGlobalList().push_back(GV);
145 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000146}
147
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000148
Chris Lattner29c14732001-12-14 16:26:05 +0000149//
150// Check if this instruction has any uses outside its basic block,
151// or if it used by either a Call or Return instruction.
152//
153static inline bool LiveAtBBExit(const Instruction* I) {
154 const BasicBlock *BB = I->getParent();
155 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
156 if (const Instruction *UI = dyn_cast<Instruction>(*U))
157 if (UI->getParent() != BB || isa<ReturnInst>(UI))
158 return true;
159
160 return false;
161}
162
163
164static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000165 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000166 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000167 //
168 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000169 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000170 opCode != Instruction::PHINode &&
171 opCode != Instruction::Cast);
172}
173
Chris Lattner29c14732001-12-14 16:26:05 +0000174
175static bool ShouldTraceValue(const Instruction *I) {
176 return
177 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
178 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000179}
180
Chris Lattner29c14732001-12-14 16:26:05 +0000181static string getPrintfCodeFor(const Value *V) {
182 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000183 if (V->getType()->isFloatingPoint())
184 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000185 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000186 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000187 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000188 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000189 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000190 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000191
Chris Lattner649f5dd2002-04-14 06:15:24 +0000192 assert(0 && "Illegal value to print out...");
193 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000194}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000195
196
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000197static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
198 string Message,
199 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000200 // Escape Message by replacing all % characters with %% chars.
201 unsigned Offset = 0;
202 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner649f5dd2002-04-14 06:15:24 +0000203 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000204 Offset += 2; // Skip over the new %'s
205 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000206
Chris Lattner29c14732001-12-14 16:26:05 +0000207 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000208
Chris Lattner44571632001-10-18 06:03:05 +0000209 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000210 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
211
212 // Turn the format string into an sbyte *
213 Instruction *GEP =
214 new GetElementPtrInst(fmtVal,
215 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
216 "trstr");
217 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000218
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000219 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000220 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
221 const Type *SBP = PointerType::get(Type::SByteTy);
222 if (V->getType() != SBP) { // Cast pointer to be sbyte*
223 Instruction *I = new CastInst(V, SBP, "Hash_cast");
224 BBI = BB->getInstList().insert(BBI, I)+1;
225 V = I;
226 }
227
228 vector<Value*> HashArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000229 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
230 BBI = BB->getInstList().insert(BBI, cast<Instruction>(V))+1;
231 }
232
Chris Lattner44571632001-10-18 06:03:05 +0000233 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000234 vector<Value*> PrintArgs;
235 PrintArgs.push_back(GEP);
236 if (V) PrintArgs.push_back(V);
237 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000238 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000239}
240
Chris Lattner44571632001-10-18 06:03:05 +0000241
Chris Lattner29c14732001-12-14 16:26:05 +0000242static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
243 BasicBlock::iterator &BBI,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000244 const string &Message, Function *Printf,
245 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000246 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000247 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000248 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
249 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000250}
251
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000252static void
253InsertReleaseInst(Value *V, BasicBlock *BB,
254 BasicBlock::iterator &BBI,
255 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000256
257 const Type *SBP = PointerType::get(Type::SByteTy);
258 if (V->getType() != SBP) { // Cast pointer to be sbyte*
259 Instruction *I = new CastInst(V, SBP, "RPSN_cast");
260 BBI = BB->getInstList().insert(BBI, I)+1;
261 V = I;
262 }
263 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000264 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
265 BBI = BB->getInstList().insert(BBI, I)+1;
266}
267
268static void
269InsertRecordInst(Value *V, BasicBlock *BB,
270 BasicBlock::iterator &BBI,
271 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000272 const Type *SBP = PointerType::get(Type::SByteTy);
273 if (V->getType() != SBP) { // Cast pointer to be sbyte*
274 Instruction *I = new CastInst(V, SBP, "RP_cast");
275 BBI = BB->getInstList().insert(BBI, I)+1;
276 V = I;
277 }
278 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000279 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
280 BBI = BB->getInstList().insert(BBI, I)+1;
281}
282
283static void
284InsertPushOnEntryFunc(Function *M,
285 Function* PushOnEntryFunc) {
286 // Get an iterator to point to the insertion location
287 BasicBlock *BB = M->getEntryNode();
288 BB->getInstList().insert(BB->begin(), new CallInst(PushOnEntryFunc,
289 vector<Value*> ()));
290}
291
292static void
293InsertReleaseRecordedInst(BasicBlock *BB,
294 Function* ReleaseOnReturnFunc) {
295 BasicBlock::iterator BBI = BB->end()-1;
296 BBI = 1 + BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
297 vector<Value*>()));
298}
299
300// Look for alloca and free instructions. These are the ptrs to release.
301// Release the free'd pointers immediately. Record the alloca'd pointers
302// to be released on return from the current function.
303//
304static void
305ReleasePtrSeqNumbers(BasicBlock *BB,
306 ExternalFuncs& externalFuncs) {
307
308 for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
309 if (FreeInst *FI = dyn_cast<FreeInst>(*II))
310 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
311 else if (AllocaInst *AI = dyn_cast<AllocaInst>(*II))
312 {
313 BasicBlock::iterator nextI = II+1;
314 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
315 II = nextI - 1;
316 }
317 }
318}
319
Chris Lattner8d9e3772001-10-18 05:28:08 +0000320
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000321// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000322// for each value in valueVec[] that is live at the end of that basic block,
323// or that is stored to memory in this basic block.
324// If the value is stored to memory, we load it back before printing
Chris Lattner79df7c02002-03-26 18:01:55 +0000325// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000326// for printing at the exit from the function. (Note that in each invocation
327// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000328// store instruction).
329// *bb must be the block in which the value is computed;
330// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000331//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000332static void TraceValuesAtBBExit(BasicBlock *BB,
333 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000334 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000335 // Get an iterator to point to the insertion location, which is
336 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000337 //
Chris Lattner29c14732001-12-14 16:26:05 +0000338 BasicBlock::iterator InsertPos = BB->end()-1;
339 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000340
Vikram S. Adved8893302001-10-28 21:37:25 +0000341 // If the terminator is a conditional branch, insert the trace code just
342 // before the instruction that computes the branch condition (just to
343 // avoid putting a call between the CC-setting instruction and the branch).
344 // Use laterInstrSet to mark instructions that come after the setCC instr
345 // because those cannot be traced at the location we choose.
346 //
Chris Lattner29c14732001-12-14 16:26:05 +0000347 Instruction *SetCC = 0;
348 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
349 if (!Branch->isUnconditional())
350 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
351 if (I->getParent() == BB) {
352 SetCC = I;
353 while (*InsertPos != SetCC)
354 --InsertPos; // Back up until we can insert before the setcc
355 }
356
357 // Copy all of the instructions into a vector to avoid problems with Setcc
358 const vector<Instruction*> Insts(BB->begin(), InsertPos);
359
Chris Lattner697954c2002-01-20 22:54:45 +0000360 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000361 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000362 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
363 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000364
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000365 // Insert a print instruction for each value.
366 //
Chris Lattner29c14732001-12-14 16:26:05 +0000367 for (vector<Instruction*>::const_iterator II = Insts.begin(),
368 IE = Insts.end(); II != IE; ++II) {
369 Instruction *I = *II;
370 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000371 assert(valuesStoredInFunction &&
Chris Lattner649f5dd2002-04-14 06:15:24 +0000372 "Should not be printing a store instruction at function exit");
Chris Lattner29c14732001-12-14 16:26:05 +0000373 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
374 "reload");
375 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner79df7c02002-03-26 18:01:55 +0000376 valuesStoredInFunction->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000377 }
Chris Lattner29c14732001-12-14 16:26:05 +0000378 if (ShouldTraceValue(I))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000379 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000380 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000381}
382
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000383static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
384 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000385 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000386 BasicBlock *BB = M->getEntryNode();
387 BasicBlock::iterator BBI = BB->begin();
388
Chris Lattner697954c2002-01-20 22:54:45 +0000389 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000390 WriteAsOperand(OutStr, M, true);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000391 InsertPrintInst(0, BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
392 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000393
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000394 // Now print all the incoming arguments
Chris Lattner79df7c02002-03-26 18:01:55 +0000395 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattner29c14732001-12-14 16:26:05 +0000396 unsigned ArgNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000397 for (Function::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000398 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
Chris Lattner73e21422002-04-09 19:48:49 +0000399 InsertVerbosePrintInst((Value*)*I, BB, BBI,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000400 " Arg #" + utostr(ArgNo) + ": ", Printf,
401 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000402 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000403}
404
405
Chris Lattner79df7c02002-03-26 18:01:55 +0000406static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000407 Function *Printf,
408 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000409 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000410 BasicBlock::iterator BBI = BB->end()-1;
411 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000412
Chris Lattner697954c2002-01-20 22:54:45 +0000413 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000414 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000415 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
416 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000417
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000418 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000419 if (BB->getParent()->getReturnType() != Type::VoidTy)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000420 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ",
421 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000422}
423
424
Chris Lattner79df7c02002-03-26 18:01:55 +0000425bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000426 bool traceFunctionEvents,
427 ExternalFuncs& externalFuncs) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000428 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000429 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000430
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000431 if (!TraceThisFunction(M))
432 return false;
433
Chris Lattner79df7c02002-03-26 18:01:55 +0000434 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000435 vector<BasicBlock*> exitBlocks;
436
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000437 // Insert code to trace values at function entry
Chris Lattner79df7c02002-03-26 18:01:55 +0000438 if (traceFunctionEvents)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000439 InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
440 externalFuncs.HashPtrFunc);
441
442 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000443 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000444 InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000445
Chris Lattner79df7c02002-03-26 18:01:55 +0000446 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattner29c14732001-12-14 16:26:05 +0000447 BasicBlock *BB = *BI;
448 if (isa<ReturnInst>(BB->getTerminator()))
449 exitBlocks.push_back(BB); // record this as an exit block
450
451 if (traceBasicBlockExits)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000452 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
453 externalFuncs.HashPtrFunc, &valuesStoredInFunction);
454
Chris Lattner2e0769e2002-05-20 21:43:59 +0000455 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000456 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000457 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000458
459 for (unsigned i=0; i < exitBlocks.size(); ++i)
460 {
461 // Insert code to trace values at function exit
462 if (traceFunctionEvents)
463 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
464 externalFuncs.HashPtrFunc);
465
466 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000467 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000468 InsertReleaseRecordedInst(exitBlocks[i],
469 externalFuncs.ReleaseOnReturnFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000470 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000471
Chris Lattner8d9e3772001-10-18 05:28:08 +0000472 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000473}