blob: 618466a403091b38b7014025f2cd737fb7827ef2 [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
Chris Lattnerc56d2392003-01-14 22:39:29 +00008#include "llvm/Transforms/Instrumentation.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000010#include "llvm/DerivedTypes.h"
Vikram S. Adve631b9a32001-10-18 18:16:11 +000011#include "llvm/iMemory.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000012#include "llvm/iTerminators.h"
13#include "llvm/iOther.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000014#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000016#include "llvm/Assembly/Writer.h"
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000017#include "Support/CommandLine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000018#include "Support/StringExtras.h"
Chris Lattner2e0769e2002-05-20 21:43:59 +000019#include <algorithm>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000020#include <sstream>
Chris Lattner697954c2002-01-20 22:54:45 +000021using std::vector;
22using std::string;
Vikram S. Adved8893302001-10-28 21:37:25 +000023
Chris Lattner5ff62e92002-07-22 02:10:13 +000024static cl::opt<bool>
25DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
Chris Lattneraf7ccd92003-04-13 03:50:14 +000026 cl::desc("Disable pointer hashing in the -trace or -tracem "
27 "passes"));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000028
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::list<string>
Chris Lattneraf7ccd92003-04-13 03:50:14 +000030TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
31 "-trace or -tracem passes"),
Chris Lattner80e5ed92003-01-13 00:52:14 +000032 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000033
Chris Lattner11982662002-07-23 18:04:15 +000034static void TraceValuesAtBBExit(BasicBlock *BB,
35 Function *Printf, Function* HashPtrToSeqNum,
36 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve1e2ddcf2002-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 Lattner11982662002-07-23 18:04:15 +000041inline static bool
Chris Lattner80e5ed92003-01-13 00:52:14 +000042TraceThisFunction(Function &F)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000043{
Chris Lattner80e5ed92003-01-13 00:52:14 +000044 if (TraceFuncNames.empty()) return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000045
Chris Lattner80e5ed92003-01-13 00:52:14 +000046 return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
47 != TraceFuncNames.end();
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000048}
49
50
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051namespace {
Chris Lattner2e0769e2002-05-20 21:43:59 +000052 struct ExternalFuncs {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000053 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
54 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner7e708292002-06-25 16:13:24 +000055 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000056 };
57
Chris Lattnerf57b8452002-04-27 06:56:12 +000058 class InsertTraceCode : public FunctionPass {
Chris Lattner11982662002-07-23 18:04:15 +000059 protected:
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000060 ExternalFuncs externalFuncs;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061 public:
Chris Lattnerbd0ef772002-02-26 21:46:54 +000062
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000063 // Add a prototype for runtime functions not already in the program.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000064 //
Chris Lattner7e708292002-06-25 16:13:24 +000065 bool doInitialization(Module &M);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066
67 //--------------------------------------------------------------------------
68 // Function InsertCodeToTraceValues
69 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000070 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000071 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000072 //
Chris Lattner11982662002-07-23 18:04:15 +000073 bool doit(Function *M);
74
75 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) = 0;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000076
Chris Lattner649f5dd2002-04-14 06:15:24 +000077 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000078 //
Chris Lattner11982662002-07-23 18:04:15 +000079 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000080
81 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnercb2610e2002-10-21 20:00:28 +000082 AU.setPreservesCFG();
Chris Lattner97e52e42002-04-28 21:27:06 +000083 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000084 };
Chris Lattner11982662002-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 Lattnera6275cc2002-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 Lattnerbd0ef772002-02-26 21:46:54 +0000102} // end anonymous namespace
103
104
Chris Lattnerf57b8452002-04-27 06:56:12 +0000105Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner11982662002-07-23 18:04:15 +0000106 return new FunctionTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000107}
108
Chris Lattner649f5dd2002-04-14 06:15:24 +0000109Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner11982662002-07-23 18:04:15 +0000110 return new BasicBlockTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000111}
112
Chris Lattner11982662002-07-23 18:04:15 +0000113
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000114// Add a prototype for external functions used by the tracing code.
Chris Lattner29c14732001-12-14 16:26:05 +0000115//
Chris Lattner7e708292002-06-25 16:13:24 +0000116void ExternalFuncs::doInitialization(Module &M) {
Chris Lattner29c14732001-12-14 16:26:05 +0000117 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000118 const FunctionType *MTy =
119 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner7e708292002-06-25 16:13:24 +0000120 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000121
122 // uint (sbyte*)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000123 const FunctionType *hashFuncTy =
Chris Lattner2e0769e2002-05-20 21:43:59 +0000124 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner7e708292002-06-25 16:13:24 +0000125 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000126
Chris Lattner2e0769e2002-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. Adve1e2ddcf2002-05-19 15:39:02 +0000130
Chris Lattner7e708292002-06-25 16:13:24 +0000131 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
132 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000133
134 const FunctionType *voidvoidFuncTy =
135 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
136
Chris Lattner7e708292002-06-25 16:13:24 +0000137 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
138 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000139 voidvoidFuncTy);
140}
141
142
143// Add a prototype for external functions used by the tracing code.
144//
Chris Lattner7e708292002-06-25 16:13:24 +0000145bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000146 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000147 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000148}
149
Chris Lattner29c14732001-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. Adve524185a2002-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 Lattner4ad02e72003-04-16 20:28:45 +0000157 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
158 GlobalValue::InternalLinkage, Init,
Chris Lattner29c14732001-12-14 16:26:05 +0000159 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000160 M->getGlobalList().push_back(GV);
161 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000162}
163
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000164
Chris Lattner29c14732001-12-14 16:26:05 +0000165//
166// Check if this instruction has any uses outside its basic block,
167// or if it used by either a Call or Return instruction.
168//
169static inline bool LiveAtBBExit(const Instruction* I) {
170 const BasicBlock *BB = I->getParent();
171 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
172 if (const Instruction *UI = dyn_cast<Instruction>(*U))
173 if (UI->getParent() != BB || isa<ReturnInst>(UI))
174 return true;
175
176 return false;
177}
178
179
180static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000181 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000182 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000183 //
Chris Lattner0b16ae22002-10-13 19:39:16 +0000184 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000185 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000186 opCode != Instruction::PHINode &&
187 opCode != Instruction::Cast);
188}
189
Chris Lattner29c14732001-12-14 16:26:05 +0000190
191static bool ShouldTraceValue(const Instruction *I) {
192 return
193 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
194 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000195}
196
Chris Lattner29c14732001-12-14 16:26:05 +0000197static string getPrintfCodeFor(const Value *V) {
198 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000199 if (V->getType()->isFloatingPoint())
200 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000201 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000202 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000203 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000204 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner65ad3722002-09-03 01:07:35 +0000205 else if (V->getType()->isIntegral())
Chris Lattner29c14732001-12-14 16:26:05 +0000206 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000207
Chris Lattner649f5dd2002-04-14 06:15:24 +0000208 assert(0 && "Illegal value to print out...");
209 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000210}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000211
212
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000213static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000214 string Message,
215 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000216 // Escape Message by replacing all % characters with %% chars.
Chris Lattner1a33e312002-10-17 16:22:08 +0000217 string Tmp;
218 std::swap(Tmp, Message);
219 string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
220 while (I != Tmp.end()) {
221 Message.append(Tmp.begin(), I);
222 Message += "%%";
223 ++I; // Make sure to erase the % as well...
224 Tmp.erase(Tmp.begin(), I);
225 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattner29c14732001-12-14 16:26:05 +0000226 }
Chris Lattner80e5ed92003-01-13 00:52:14 +0000227 Message += Tmp;
Chris Lattner29c14732001-12-14 16:26:05 +0000228 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000229
Chris Lattner44571632001-10-18 06:03:05 +0000230 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000231 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
232
233 // Turn the format string into an sbyte *
Chris Lattnerd3646f12003-06-05 04:48:18 +0000234 Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
235 vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
Chris Lattner44571632001-10-18 06:03:05 +0000236
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000237 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000238 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
239 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000240 if (V->getType() != SBP) // Cast pointer to be sbyte*
241 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000242
243 vector<Value*> HashArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000244 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000245 }
246
Chris Lattner44571632001-10-18 06:03:05 +0000247 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000248 vector<Value*> PrintArgs;
249 PrintArgs.push_back(GEP);
250 if (V) PrintArgs.push_back(V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000251 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattner29c14732001-12-14 16:26:05 +0000252}
253
Chris Lattner44571632001-10-18 06:03:05 +0000254
Chris Lattner29c14732001-12-14 16:26:05 +0000255static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000256 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000257 const string &Message, Function *Printf,
258 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000259 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000260 if (V) WriteAsOperand(OutStr, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000261 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000262 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000263}
264
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000265static void
266InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000267 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000268 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000269
270 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000271 if (V->getType() != SBP) // Cast pointer to be sbyte*
272 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
273
Chris Lattner2e0769e2002-05-20 21:43:59 +0000274 vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000275 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000276}
277
278static void
279InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000280 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000281 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000282 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000283 if (V->getType() != SBP) // Cast pointer to be sbyte*
284 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
285
Chris Lattner2e0769e2002-05-20 21:43:59 +0000286 vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000287 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000288}
289
290// Look for alloca and free instructions. These are the ptrs to release.
291// Release the free'd pointers immediately. Record the alloca'd pointers
292// to be released on return from the current function.
293//
294static void
295ReleasePtrSeqNumbers(BasicBlock *BB,
296 ExternalFuncs& externalFuncs) {
297
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000298 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattnere408e252003-04-23 16:37:45 +0000299 if (FreeInst *FI = dyn_cast<FreeInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000300 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattnere408e252003-04-23 16:37:45 +0000301 else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000302 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000303}
304
Chris Lattner8d9e3772001-10-18 05:28:08 +0000305
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000306// Insert print instructions at the end of basic block BB for each value
307// computed in BB that is live at the end of BB,
308// or that is stored to memory in BB.
309// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000310// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000311// for printing at the exit from the function. (Note that in each invocation
312// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000313// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000314//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000315static void TraceValuesAtBBExit(BasicBlock *BB,
316 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000317 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000318 // Get an iterator to point to the insertion location, which is
319 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000320 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000321 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000322
Chris Lattner697954c2002-01-20 22:54:45 +0000323 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000324 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000325 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
326 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000327
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000328 // Insert a print instruction for each instruction preceding InsertPos.
329 // The print instructions must go before InsertPos, so we use the
330 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000331 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000332 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
Chris Lattnere408e252003-04-23 16:37:45 +0000333 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000334 assert(valuesStoredInFunction &&
335 "Should not be printing a store instruction at function exit");
336 LoadInst *LI = new LoadInst(SI->getPointerOperand(), "reload." +
337 SI->getPointerOperand()->getName(),
338 InsertPos);
339 valuesStoredInFunction->push_back(LI);
340 }
341 if (ShouldTraceValue(II))
342 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000343 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000344}
345
Chris Lattner11982662002-07-23 18:04:15 +0000346static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000347 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000348 // Get an iterator to point to the insertion location
Chris Lattner11982662002-07-23 18:04:15 +0000349 BasicBlock &BB = F.getEntryNode();
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000350 Instruction *InsertPos = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000351
Chris Lattner697954c2002-01-20 22:54:45 +0000352 std::ostringstream OutStr;
Chris Lattner80e5ed92003-01-13 00:52:14 +0000353 WriteAsOperand(OutStr, &F);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000354 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000355 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000356
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000357 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000358 unsigned ArgNo = 0;
Chris Lattner11982662002-07-23 18:04:15 +0000359 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000360 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000361 " Arg #" + utostr(ArgNo) + ": ", Printf,
362 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000363 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000364}
365
366
Chris Lattner79df7c02002-03-26 18:01:55 +0000367static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000368 Function *Printf,
369 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000370 // Get an iterator to point to the insertion location
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000371 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000372
Chris Lattner697954c2002-01-20 22:54:45 +0000373 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000374 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000375 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000376 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000377
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000378 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000379 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000380 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000381 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000382}
383
384
Chris Lattner11982662002-07-23 18:04:15 +0000385bool InsertTraceCode::runOnFunction(Function &F) {
386 if (!TraceThisFunction(F))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000387 return false;
388
Chris Lattner79df7c02002-03-26 18:01:55 +0000389 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000390 vector<BasicBlock*> exitBlocks;
391
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000392 // Insert code to trace values at function entry
Chris Lattner11982662002-07-23 18:04:15 +0000393 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
394 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000395
396 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000397 if (!DisablePtrHashing)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000398 new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
399 F.getEntryNode().begin());
400
Chris Lattner11982662002-07-23 18:04:15 +0000401 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000402 if (isa<ReturnInst>(BB->getTerminator()))
403 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner11982662002-07-23 18:04:15 +0000404
405 // Insert trace code if this basic block is interesting...
406 handleBasicBlock(BB, valuesStoredInFunction);
407
Chris Lattner2e0769e2002-05-20 21:43:59 +0000408 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000409 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000410 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000411
Chris Lattner11982662002-07-23 18:04:15 +0000412 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000413 {
414 // Insert code to trace values at function exit
Chris Lattner11982662002-07-23 18:04:15 +0000415 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
416 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000417
418 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000419 if (!DisablePtrHashing)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000420 new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
421 exitBlocks[i]->getTerminator());
Chris Lattner29c14732001-12-14 16:26:05 +0000422 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000423
Chris Lattner8d9e3772001-10-18 05:28:08 +0000424 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000425}