blob: 8c44d4d28ca88df6ee5567bc17b1f02b927fdbbc [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,
26 cl::desc("Disable pointer hashing"));
Vikram S. Adve47f37c32002-05-19 15:39:02 +000027
Chris Lattnerf5cad152002-07-22 02:10:13 +000028static cl::list<string>
Chris Lattnerb9636a72003-01-13 00:52:14 +000029TraceFuncNames("tracefunc", cl::desc("trace only specific functions"),
30 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000031
Chris Lattner6d216fd2002-07-23 18:04:15 +000032static void TraceValuesAtBBExit(BasicBlock *BB,
33 Function *Printf, Function* HashPtrToSeqNum,
34 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000035
36// We trace a particular function if no functions to trace were specified
37// or if the function is in the specified list.
38//
Chris Lattner6d216fd2002-07-23 18:04:15 +000039inline static bool
Chris Lattnerb9636a72003-01-13 00:52:14 +000040TraceThisFunction(Function &F)
Vikram S. Adve47f37c32002-05-19 15:39:02 +000041{
Chris Lattnerb9636a72003-01-13 00:52:14 +000042 if (TraceFuncNames.empty()) return true;
Chris Lattnerace7b8d2002-05-20 21:43:59 +000043
Chris Lattnerb9636a72003-01-13 00:52:14 +000044 return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
45 != TraceFuncNames.end();
Vikram S. Adve47f37c32002-05-19 15:39:02 +000046}
47
48
Chris Lattner04805fa2002-02-26 21:46:54 +000049namespace {
Chris Lattnerace7b8d2002-05-20 21:43:59 +000050 struct ExternalFuncs {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000051 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
52 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner113f4f42002-06-25 16:13:24 +000053 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve47f37c32002-05-19 15:39:02 +000054 };
55
Chris Lattnerc8e66542002-04-27 06:56:12 +000056 class InsertTraceCode : public FunctionPass {
Chris Lattner6d216fd2002-07-23 18:04:15 +000057 protected:
Vikram S. Adve47f37c32002-05-19 15:39:02 +000058 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000059 public:
Chris Lattner04805fa2002-02-26 21:46:54 +000060
Vikram S. Adve47f37c32002-05-19 15:39:02 +000061 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000062 //
Chris Lattner113f4f42002-06-25 16:13:24 +000063 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000064
65 //--------------------------------------------------------------------------
66 // Function InsertCodeToTraceValues
67 //
Chris Lattner7e358902002-04-14 06:15:24 +000068 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000069 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000070 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000071 bool doit(Function *M);
72
73 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) = 0;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000074
Chris Lattner7e358902002-04-14 06:15:24 +000075 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000076 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000077 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000078
79 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000080 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000081 }
Chris Lattner04805fa2002-02-26 21:46:54 +000082 };
Chris Lattner6d216fd2002-07-23 18:04:15 +000083
84 struct FunctionTracer : public InsertTraceCode {
85 // Ignore basic blocks here...
86 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {}
87 };
88
89 struct BasicBlockTracer : public InsertTraceCode {
90 // Trace basic blocks here...
91 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {
92 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
93 externalFuncs.HashPtrFunc, &VI);
94 }
95 };
96
97 // Register the passes...
Chris Lattnerc8b70922002-07-26 21:12:46 +000098 RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
99 RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
Chris Lattner04805fa2002-02-26 21:46:54 +0000100} // end anonymous namespace
101
102
Chris Lattnerc8e66542002-04-27 06:56:12 +0000103Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000104 return new FunctionTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000105}
106
Chris Lattner7e358902002-04-14 06:15:24 +0000107Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000108 return new BasicBlockTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000109}
110
Chris Lattner6d216fd2002-07-23 18:04:15 +0000111
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000112// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000113//
Chris Lattner113f4f42002-06-25 16:13:24 +0000114void ExternalFuncs::doInitialization(Module &M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000115 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000116 const FunctionType *MTy =
117 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000118 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000119
120 // uint (sbyte*)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000121 const FunctionType *hashFuncTy =
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000122 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner113f4f42002-06-25 16:13:24 +0000123 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000124
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000125 // void (sbyte*)
126 const FunctionType *voidSBPFuncTy =
127 FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000128
Chris Lattner113f4f42002-06-25 16:13:24 +0000129 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
130 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000131
132 const FunctionType *voidvoidFuncTy =
133 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
134
Chris Lattner113f4f42002-06-25 16:13:24 +0000135 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
136 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000137 voidvoidFuncTy);
138}
139
140
141// Add a prototype for external functions used by the tracing code.
142//
Chris Lattner113f4f42002-06-25 16:13:24 +0000143bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000144 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 //
Chris Lattner69ce8672002-10-13 19:39:16 +0000181 return (opCode < Instruction::OtherOpsBegin &&
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()))
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000201 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattnerbc6bdc22002-09-03 01:07:35 +0000202 else if (V->getType()->isIntegral())
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
Chris Lattner28a8d242002-09-10 17:04:02 +0000210static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000211 string Message,
212 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000213 // Escape Message by replacing all % characters with %% chars.
Chris Lattner26783a52002-10-17 16:22:08 +0000214 string Tmp;
215 std::swap(Tmp, Message);
216 string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
217 while (I != Tmp.end()) {
218 Message.append(Tmp.begin(), I);
219 Message += "%%";
220 ++I; // Make sure to erase the % as well...
221 Tmp.erase(Tmp.begin(), I);
222 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattnerf1197b02001-12-14 16:26:05 +0000223 }
Chris Lattnerb9636a72003-01-13 00:52:14 +0000224 Message += Tmp;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000225 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000226
Chris Lattner5309e102001-10-18 06:03:05 +0000227 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000228 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
229
230 // Turn the format string into an sbyte *
231 Instruction *GEP =
232 new GetElementPtrInst(fmtVal,
Chris Lattnerb9d9e0f2002-09-11 01:21:29 +0000233 vector<Value*>(2,ConstantSInt::get(Type::LongTy, 0)),
Chris Lattnerb9636a72003-01-13 00:52:14 +0000234 "trstrp", InsertBefore);
Chris Lattner5309e102001-10-18 06:03:05 +0000235
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000236 // Insert a call to the hash function if this is a pointer value
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000237 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
238 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000239 if (V->getType() != SBP) // Cast pointer to be sbyte*
240 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000241
242 vector<Value*> HashArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000243 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000244 }
245
Chris Lattner5309e102001-10-18 06:03:05 +0000246 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000247 vector<Value*> PrintArgs;
248 PrintArgs.push_back(GEP);
249 if (V) PrintArgs.push_back(V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000250 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000251}
252
Chris Lattner5309e102001-10-18 06:03:05 +0000253
Chris Lattnerf1197b02001-12-14 16:26:05 +0000254static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000255 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000256 const string &Message, Function *Printf,
257 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000258 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000259 if (V) WriteAsOperand(OutStr, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000260 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000261 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000262}
263
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000264static void
265InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000266 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000267 Function* ReleasePtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000268
269 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000270 if (V->getType() != SBP) // Cast pointer to be sbyte*
271 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
272
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000273 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000274 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000275}
276
277static void
278InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000279 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000280 Function* RecordPtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000281 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000282 if (V->getType() != SBP) // Cast pointer to be sbyte*
283 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
284
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000285 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000286 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000287}
288
289// Look for alloca and free instructions. These are the ptrs to release.
290// Release the free'd pointers immediately. Record the alloca'd pointers
291// to be released on return from the current function.
292//
293static void
294ReleasePtrSeqNumbers(BasicBlock *BB,
295 ExternalFuncs& externalFuncs) {
296
Chris Lattner28a8d242002-09-10 17:04:02 +0000297 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattner113f4f42002-06-25 16:13:24 +0000298 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000299 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattner113f4f42002-06-25 16:13:24 +0000300 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000301 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000302}
303
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000304
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000305// Insert print instructions at the end of basic block BB for each value
306// computed in BB that is live at the end of BB,
307// or that is stored to memory in BB.
308// If the value is stored to memory, we load it back before printing it
Chris Lattner57698e22002-03-26 18:01:55 +0000309// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-04-14 06:15:24 +0000310// for printing at the exit from the function. (Note that in each invocation
311// of the function, this will only get the last value stored for each static
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000312// store instruction).
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000313//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000314static void TraceValuesAtBBExit(BasicBlock *BB,
315 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner57698e22002-03-26 18:01:55 +0000316 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000317 // Get an iterator to point to the insertion location, which is
318 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000319 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000320 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000321
Chris Lattner7f74a562002-01-20 22:54:45 +0000322 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000323 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000324 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
325 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000326
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000327 // Insert a print instruction for each instruction preceding InsertPos.
328 // The print instructions must go before InsertPos, so we use the
329 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000330 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000331 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
332 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
333 assert(valuesStoredInFunction &&
334 "Should not be printing a store instruction at function exit");
335 LoadInst *LI = new LoadInst(SI->getPointerOperand(), "reload." +
336 SI->getPointerOperand()->getName(),
337 InsertPos);
338 valuesStoredInFunction->push_back(LI);
339 }
340 if (ShouldTraceValue(II))
341 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000342 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000343}
344
Chris Lattner6d216fd2002-07-23 18:04:15 +0000345static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000346 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000347 // Get an iterator to point to the insertion location
Chris Lattner6d216fd2002-07-23 18:04:15 +0000348 BasicBlock &BB = F.getEntryNode();
Chris Lattner28a8d242002-09-10 17:04:02 +0000349 Instruction *InsertPos = BB.begin();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000350
Chris Lattner7f74a562002-01-20 22:54:45 +0000351 std::ostringstream OutStr;
Chris Lattnerb9636a72003-01-13 00:52:14 +0000352 WriteAsOperand(OutStr, &F);
Chris Lattner28a8d242002-09-10 17:04:02 +0000353 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000354 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000355
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000356 // Now print all the incoming arguments
Chris Lattnerf1197b02001-12-14 16:26:05 +0000357 unsigned ArgNo = 0;
Chris Lattner6d216fd2002-07-23 18:04:15 +0000358 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner28a8d242002-09-10 17:04:02 +0000359 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000360 " Arg #" + utostr(ArgNo) + ": ", Printf,
361 HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000362 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000363}
364
365
Chris Lattner57698e22002-03-26 18:01:55 +0000366static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000367 Function *Printf,
368 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000369 // Get an iterator to point to the insertion location
Chris Lattner28a8d242002-09-10 17:04:02 +0000370 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000371
Chris Lattner7f74a562002-01-20 22:54:45 +0000372 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000373 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner28a8d242002-09-10 17:04:02 +0000374 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000375 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000376
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000377 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000378 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner28a8d242002-09-10 17:04:02 +0000379 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000380 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000381}
382
383
Chris Lattner6d216fd2002-07-23 18:04:15 +0000384bool InsertTraceCode::runOnFunction(Function &F) {
385 if (!TraceThisFunction(F))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000386 return false;
387
Chris Lattner57698e22002-03-26 18:01:55 +0000388 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000389 vector<BasicBlock*> exitBlocks;
390
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000391 // Insert code to trace values at function entry
Chris Lattner6d216fd2002-07-23 18:04:15 +0000392 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
393 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000394
395 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000396 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000397 new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
398 F.getEntryNode().begin());
399
Chris Lattner6d216fd2002-07-23 18:04:15 +0000400 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000401 if (isa<ReturnInst>(BB->getTerminator()))
402 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner6d216fd2002-07-23 18:04:15 +0000403
404 // Insert trace code if this basic block is interesting...
405 handleBasicBlock(BB, valuesStoredInFunction);
406
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000407 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000408 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000409 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000410
Chris Lattner6d216fd2002-07-23 18:04:15 +0000411 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000412 {
413 // Insert code to trace values at function exit
Chris Lattner6d216fd2002-07-23 18:04:15 +0000414 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
415 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000416
417 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000418 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000419 new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
420 exitBlocks[i]->getTerminator());
Chris Lattnerf1197b02001-12-14 16:26:05 +0000421 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000422
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000423 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000424}