blob: c77d39e099b5dfc244658a6e383e1bbd9a0e9626 [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
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
9#include "llvm/GlobalVariable.h"
Chris Lattnerca142372002-04-28 19:55:58 +000010#include "llvm/Constants.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Chris Lattnerd92b01c2002-04-09 18:37:46 +000015#include "llvm/BasicBlock.h"
Chris Lattner57698e22002-03-26 18:01:55 +000016#include "llvm/Function.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000017#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Vikram S. Adve47f37c32002-05-19 15:39:02 +000020#include "Support/CommandLine.h"
Chris Lattner5de22042001-11-27 00:03:19 +000021#include "Support/StringExtras.h"
Chris Lattnere2c61262001-10-18 20:06:03 +000022#include <sstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000023using std::vector;
24using std::string;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000025
Vikram S. Adve47f37c32002-05-19 15:39:02 +000026
27enum TraceHashPtrOpt {
28 HashToSeqNum, NoHash
29};
30
31static cl::Enum<enum TraceHashPtrOpt> TraceHashPtrs("tracehash", cl::NoFlags,
32 "Hash pointer values when tracing",
33 clEnumValN(HashToSeqNum, "on", "Hash pointers to sequence number"),
34 clEnumValN(NoHash , "off","Disable hashing of pointers"), 0);
35
36
37static cl::StringList TraceFuncName ("tracefunc", "trace these functions", cl::NoFlags);
38
39
40// We trace a particular function if no functions to trace were specified
41// or if the function is in the specified list.
42//
43inline bool
44TraceThisFunction(Function* func)
45{
46 if (TraceFuncName.getNumOccurances() == 0)
47 return true;
48
49 for (std::vector<std::string>::const_iterator SI=TraceFuncName.begin(),
50 SE=TraceFuncName.end(); SI != SE; ++SI)
51 if (func->getName() == *SI)
52 return true;
53
54 return false;
55}
56
57
Chris Lattner04805fa2002-02-26 21:46:54 +000058namespace {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000059 class ExternalFuncs {
60 public:
61 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
62 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
63 void doInitialization(Module *M); // Add prototypes for external functions
64 };
65
Chris Lattnerc8e66542002-04-27 06:56:12 +000066 class InsertTraceCode : public FunctionPass {
Chris Lattner57698e22002-03-26 18:01:55 +000067 bool TraceBasicBlockExits, TraceFunctionExits;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000068 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000069 public:
Chris Lattner57698e22002-03-26 18:01:55 +000070 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattner04805fa2002-02-26 21:46:54 +000071 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner57698e22002-03-26 18:01:55 +000072 TraceFunctionExits(traceFunctionExits) {}
Chris Lattner37104aa2002-04-29 14:57:45 +000073
74 const char *getPassName() const { return "Trace Code Insertion"; }
Chris Lattner04805fa2002-02-26 21:46:54 +000075
Vikram S. Adve47f37c32002-05-19 15:39:02 +000076 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000077 //
78 bool doInitialization(Module *M);
79
80 //--------------------------------------------------------------------------
81 // Function InsertCodeToTraceValues
82 //
Chris Lattner7e358902002-04-14 06:15:24 +000083 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000084 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000085 //
Chris Lattner57698e22002-03-26 18:01:55 +000086 static bool doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve47f37c32002-05-19 15:39:02 +000087 bool traceFunctionExits, ExternalFuncs& externalFuncs);
88
Chris Lattner7e358902002-04-14 06:15:24 +000089 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000090 //
Chris Lattnerc8e66542002-04-27 06:56:12 +000091 bool runOnFunction(Function *F) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000092 return doit(F, TraceBasicBlockExits, TraceFunctionExits, externalFuncs);
Chris Lattner04805fa2002-02-26 21:46:54 +000093 }
Chris Lattnerf12cc842002-04-28 21:27:06 +000094
95 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
96 AU.preservesCFG();
97 }
Chris Lattner04805fa2002-02-26 21:46:54 +000098 };
99} // end anonymous namespace
100
101
Chris Lattnerc8e66542002-04-27 06:56:12 +0000102Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner04805fa2002-02-26 21:46:54 +0000103 return new InsertTraceCode(false, true);
104}
105
Chris Lattner7e358902002-04-14 06:15:24 +0000106Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner04805fa2002-02-26 21:46:54 +0000107 return new InsertTraceCode(true, true);
108}
109
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000110// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000111//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000112void ExternalFuncs::doInitialization(Module *M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000113 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000114 const FunctionType *MTy =
115 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000116 PrintfFunc = M->getOrInsertFunction("printf", MTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000117
118 // Use varargs functions with no args instead of func(sbyte*) so that
119 // we don't have to generate cast instructions below :-)
120 //
121 const FunctionType *hashFuncTy =
122 FunctionType::get(Type::UIntTy, vector<const Type*>(), true);
123 HashPtrFunc = M->getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
124
125 // varargs again, same reason.
126 const FunctionType *voidVAFuncTy =
127 FunctionType::get(Type::VoidTy, vector<const Type*>(), true);
128
129 ReleasePtrFunc =M->getOrInsertFunction("ReleasePointerSeqNum", voidVAFuncTy);
130 RecordPtrFunc = M->getOrInsertFunction("RecordPointer", voidVAFuncTy);
131
132 const FunctionType *voidvoidFuncTy =
133 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
134
135 PushOnEntryFunc = M->getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
136 ReleaseOnReturnFunc = M->getOrInsertFunction("ReleasePointersPopSet",
137 voidvoidFuncTy);
138}
139
140
141// Add a prototype for external functions used by the tracing code.
142//
143bool InsertTraceCode::doInitialization(Module *M) {
144 externalFuncs.doInitialization(M);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000145 return false;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000146}
147
Chris Lattnerf1197b02001-12-14 16:26:05 +0000148
149static inline GlobalVariable *getStringRef(Module *M, const string &str) {
150 // Create a constant internal string reference...
151 Constant *Init = ConstantArray::get(str);
Vikram S. Adve9f129ff2002-03-18 03:40:25 +0000152
153 // Create the global variable and record it in the module
154 // The GV will be renamed to a unique name if needed.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000155 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
156 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +0000157 M->getGlobalList().push_back(GV);
158 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000159}
160
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000161
Chris Lattnerf1197b02001-12-14 16:26:05 +0000162//
163// Check if this instruction has any uses outside its basic block,
164// or if it used by either a Call or Return instruction.
165//
166static inline bool LiveAtBBExit(const Instruction* I) {
167 const BasicBlock *BB = I->getParent();
168 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
169 if (const Instruction *UI = dyn_cast<Instruction>(*U))
170 if (UI->getParent() != BB || isa<ReturnInst>(UI))
171 return true;
172
173 return false;
174}
175
176
177static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000178 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000179 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000180 //
181 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000182 opCode != Instruction::Alloca &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000183 opCode != Instruction::PHINode &&
184 opCode != Instruction::Cast);
185}
186
Chris Lattnerf1197b02001-12-14 16:26:05 +0000187
188static bool ShouldTraceValue(const Instruction *I) {
189 return
190 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
191 TraceThisOpCode(I->getOpcode());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000192}
193
Chris Lattnerf1197b02001-12-14 16:26:05 +0000194static string getPrintfCodeFor(const Value *V) {
195 if (V == 0) return "";
Chris Lattner7e358902002-04-14 06:15:24 +0000196 if (V->getType()->isFloatingPoint())
197 return "%g";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000198 else if (V->getType() == Type::LabelTy)
Chris Lattner7e358902002-04-14 06:15:24 +0000199 return "0x%p";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000200 else if (isa<PointerType>(V->getType()))
201 return (TraceHashPtrs == NoHash)? "0x%p" : "%d";
Chris Lattner7e358902002-04-14 06:15:24 +0000202 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattnerf1197b02001-12-14 16:26:05 +0000203 return "%d";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000204
Chris Lattner7e358902002-04-14 06:15:24 +0000205 assert(0 && "Illegal value to print out...");
206 return "";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000207}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000208
209
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000210static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
211 string Message,
212 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000213 // Escape Message by replacing all % characters with %% chars.
214 unsigned Offset = 0;
215 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner7e358902002-04-14 06:15:24 +0000216 Message.replace(Offset, 1, "%%");
Chris Lattnerf1197b02001-12-14 16:26:05 +0000217 Offset += 2; // Skip over the new %'s
218 }
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000219
Chris Lattnerf1197b02001-12-14 16:26:05 +0000220 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000221
Chris Lattner5309e102001-10-18 06:03:05 +0000222 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000223 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
224
225 // Turn the format string into an sbyte *
226 Instruction *GEP =
227 new GetElementPtrInst(fmtVal,
228 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
229 "trstr");
230 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner5309e102001-10-18 06:03:05 +0000231
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000232 // Insert a call to the hash function if this is a pointer value
233 if (V && isa<PointerType>(V->getType()) && TraceHashPtrs == HashToSeqNum) {
234 vector<Value*> HashArgs;
235 HashArgs.push_back(V);
236 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
237 BBI = BB->getInstList().insert(BBI, cast<Instruction>(V))+1;
238 }
239
Chris Lattner5309e102001-10-18 06:03:05 +0000240 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000241 vector<Value*> PrintArgs;
242 PrintArgs.push_back(GEP);
243 if (V) PrintArgs.push_back(V);
244 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner5309e102001-10-18 06:03:05 +0000245 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000246}
247
Chris Lattner5309e102001-10-18 06:03:05 +0000248
Chris Lattnerf1197b02001-12-14 16:26:05 +0000249static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
250 BasicBlock::iterator &BBI,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000251 const string &Message, Function *Printf,
252 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000253 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000254 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000255 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
256 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000257}
258
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000259static void
260InsertReleaseInst(Value *V, BasicBlock *BB,
261 BasicBlock::iterator &BBI,
262 Function* ReleasePtrFunc) {
263 vector<Value*> releaseArgs;
264 releaseArgs.push_back(V);
265 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
266 BBI = BB->getInstList().insert(BBI, I)+1;
267}
268
269static void
270InsertRecordInst(Value *V, BasicBlock *BB,
271 BasicBlock::iterator &BBI,
272 Function* RecordPtrFunc) {
273 vector<Value*> releaseArgs;
274 releaseArgs.push_back(V);
275 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
276 BBI = BB->getInstList().insert(BBI, I)+1;
277}
278
279static void
280InsertPushOnEntryFunc(Function *M,
281 Function* PushOnEntryFunc) {
282 // Get an iterator to point to the insertion location
283 BasicBlock *BB = M->getEntryNode();
284 BB->getInstList().insert(BB->begin(), new CallInst(PushOnEntryFunc,
285 vector<Value*> ()));
286}
287
288static void
289InsertReleaseRecordedInst(BasicBlock *BB,
290 Function* ReleaseOnReturnFunc) {
291 BasicBlock::iterator BBI = BB->end()-1;
292 BBI = 1 + BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
293 vector<Value*>()));
294}
295
296// Look for alloca and free instructions. These are the ptrs to release.
297// Release the free'd pointers immediately. Record the alloca'd pointers
298// to be released on return from the current function.
299//
300static void
301ReleasePtrSeqNumbers(BasicBlock *BB,
302 ExternalFuncs& externalFuncs) {
303
304 for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
305 if (FreeInst *FI = dyn_cast<FreeInst>(*II))
306 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
307 else if (AllocaInst *AI = dyn_cast<AllocaInst>(*II))
308 {
309 BasicBlock::iterator nextI = II+1;
310 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
311 II = nextI - 1;
312 }
313 }
314}
315
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000316
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000317// Insert print instructions at the end of the basic block *bb
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000318// for each value in valueVec[] that is live at the end of that basic block,
319// or that is stored to memory in this basic block.
320// If the value is stored to memory, we load it back before printing
Chris Lattner57698e22002-03-26 18:01:55 +0000321// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-04-14 06:15:24 +0000322// for printing at the exit from the function. (Note that in each invocation
323// of the function, this will only get the last value stored for each static
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000324// store instruction).
325// *bb must be the block in which the value is computed;
326// this is not checked here.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000327//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000328static void TraceValuesAtBBExit(BasicBlock *BB,
329 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner57698e22002-03-26 18:01:55 +0000330 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000331 // Get an iterator to point to the insertion location, which is
332 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000333 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000334 BasicBlock::iterator InsertPos = BB->end()-1;
335 assert((*InsertPos)->isTerminator());
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000336
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000337 // If the terminator is a conditional branch, insert the trace code just
338 // before the instruction that computes the branch condition (just to
339 // avoid putting a call between the CC-setting instruction and the branch).
340 // Use laterInstrSet to mark instructions that come after the setCC instr
341 // because those cannot be traced at the location we choose.
342 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000343 Instruction *SetCC = 0;
344 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
345 if (!Branch->isUnconditional())
346 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
347 if (I->getParent() == BB) {
348 SetCC = I;
349 while (*InsertPos != SetCC)
350 --InsertPos; // Back up until we can insert before the setcc
351 }
352
353 // Copy all of the instructions into a vector to avoid problems with Setcc
354 const vector<Instruction*> Insts(BB->begin(), InsertPos);
355
Chris Lattner7f74a562002-01-20 22:54:45 +0000356 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000357 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000358 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
359 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000360
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000361 // Insert a print instruction for each value.
362 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000363 for (vector<Instruction*>::const_iterator II = Insts.begin(),
364 IE = Insts.end(); II != IE; ++II) {
365 Instruction *I = *II;
366 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000367 assert(valuesStoredInFunction &&
Chris Lattner7e358902002-04-14 06:15:24 +0000368 "Should not be printing a store instruction at function exit");
Chris Lattnerf1197b02001-12-14 16:26:05 +0000369 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
370 "reload");
371 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner57698e22002-03-26 18:01:55 +0000372 valuesStoredInFunction->push_back(LI);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000373 }
Chris Lattnerf1197b02001-12-14 16:26:05 +0000374 if (ShouldTraceValue(I))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000375 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000376 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000377}
378
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000379static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf,
380 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000381 // Get an iterator to point to the insertion location
Chris Lattnerf1197b02001-12-14 16:26:05 +0000382 BasicBlock *BB = M->getEntryNode();
383 BasicBlock::iterator BBI = BB->begin();
384
Chris Lattner7f74a562002-01-20 22:54:45 +0000385 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000386 WriteAsOperand(OutStr, M, true);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000387 InsertPrintInst(0, BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
388 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000389
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000390 // Now print all the incoming arguments
Chris Lattner57698e22002-03-26 18:01:55 +0000391 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000392 unsigned ArgNo = 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000393 for (Function::ArgumentListType::const_iterator
Chris Lattnerf1197b02001-12-14 16:26:05 +0000394 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
Chris Lattner2e9fa6d2002-04-09 19:48:49 +0000395 InsertVerbosePrintInst((Value*)*I, BB, BBI,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000396 " Arg #" + utostr(ArgNo), Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000397 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000398}
399
400
Chris Lattner57698e22002-03-26 18:01:55 +0000401static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000402 Function *Printf,
403 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000404 // Get an iterator to point to the insertion location
Chris Lattnerf1197b02001-12-14 16:26:05 +0000405 BasicBlock::iterator BBI = BB->end()-1;
406 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000407
Chris Lattner7f74a562002-01-20 22:54:45 +0000408 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000409 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000410 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
411 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000412
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000413 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000414 if (BB->getParent()->getReturnType() != Type::VoidTy)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000415 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ",
416 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000417}
418
419
Chris Lattner57698e22002-03-26 18:01:55 +0000420bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000421 bool traceFunctionEvents,
422 ExternalFuncs& externalFuncs) {
Chris Lattner57698e22002-03-26 18:01:55 +0000423 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000424 return false;
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000425
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000426 if (!TraceThisFunction(M))
427 return false;
428
Chris Lattner57698e22002-03-26 18:01:55 +0000429 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000430 vector<BasicBlock*> exitBlocks;
431
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000432 // Insert code to trace values at function entry
Chris Lattner57698e22002-03-26 18:01:55 +0000433 if (traceFunctionEvents)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000434 InsertCodeToShowFunctionEntry(M, externalFuncs.PrintfFunc,
435 externalFuncs.HashPtrFunc);
436
437 // Push a pointer set for recording alloca'd pointers at entry.
438 if (TraceHashPtrs == HashToSeqNum)
439 InsertPushOnEntryFunc(M, externalFuncs.PushOnEntryFunc);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000440
Chris Lattner57698e22002-03-26 18:01:55 +0000441 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000442 BasicBlock *BB = *BI;
443 if (isa<ReturnInst>(BB->getTerminator()))
444 exitBlocks.push_back(BB); // record this as an exit block
445
446 if (traceBasicBlockExits)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000447 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
448 externalFuncs.HashPtrFunc, &valuesStoredInFunction);
449
450 if (TraceHashPtrs == HashToSeqNum) // release seq. numbers on free/ret
451 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000452 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000453
454 for (unsigned i=0; i < exitBlocks.size(); ++i)
455 {
456 // Insert code to trace values at function exit
457 if (traceFunctionEvents)
458 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
459 externalFuncs.HashPtrFunc);
460
461 // Release all recorded pointers before RETURN. Do this LAST!
462 if (TraceHashPtrs == HashToSeqNum)
463 InsertReleaseRecordedInst(exitBlocks[i],
464 externalFuncs.ReleaseOnReturnFunc);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000465 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000466
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000467 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000468}