blob: 45799727c3f67a882462298af82b60ca8cbff06e [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*)
Chris Lattner5f07c872003-08-31 00:20:36 +0000123 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", Type::UIntTy, SBP,
124 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000125
Chris Lattner2e0769e2002-05-20 21:43:59 +0000126 // void (sbyte*)
Chris Lattner5f07c872003-08-31 00:20:36 +0000127 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum",
128 Type::VoidTy, SBP, 0);
129 RecordPtrFunc = M.getOrInsertFunction("RecordPointer",
130 Type::VoidTy, SBP, 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000131
Chris Lattner5f07c872003-08-31 00:20:36 +0000132 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", Type::VoidTy, 0);
Chris Lattner7e708292002-06-25 16:13:24 +0000133 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Chris Lattner5f07c872003-08-31 00:20:36 +0000134 Type::VoidTy, 0);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000135}
136
137
138// Add a prototype for external functions used by the tracing code.
139//
Chris Lattner7e708292002-06-25 16:13:24 +0000140bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000141 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000142 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000143}
144
Chris Lattner29c14732001-12-14 16:26:05 +0000145
146static inline GlobalVariable *getStringRef(Module *M, const string &str) {
147 // Create a constant internal string reference...
148 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +0000149
150 // Create the global variable and record it in the module
151 // The GV will be renamed to a unique name if needed.
Chris Lattner4ad02e72003-04-16 20:28:45 +0000152 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
153 GlobalValue::InternalLinkage, Init,
Chris Lattner29c14732001-12-14 16:26:05 +0000154 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000155 M->getGlobalList().push_back(GV);
156 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000157}
158
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000159
Chris Lattner29c14732001-12-14 16:26:05 +0000160//
161// Check if this instruction has any uses outside its basic block,
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000162// or if it used by either a Call or Return instruction (ditto).
163// (Values stored to memory within this BB are live at end of BB but are
164// traced at the store instruction, not where they are computed.)
Chris Lattner29c14732001-12-14 16:26:05 +0000165//
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. Advedf1892f2001-10-14 23:18:45 +0000178 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000179 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000180 //
Chris Lattner0b16ae22002-10-13 19:39:16 +0000181 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000182 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000183 opCode != Instruction::PHINode &&
184 opCode != Instruction::Cast);
185}
186
Chris Lattner29c14732001-12-14 16:26:05 +0000187
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000188// Trace a value computed by an instruction if it is non-void, it is computed
189// by a real computation, not just a copy (see TraceThisOpCode), and
190// -- it is a load instruction: we want to check values read from memory
191// -- or it is live at exit from the basic block (i.e., ignore local temps)
192//
Chris Lattner29c14732001-12-14 16:26:05 +0000193static bool ShouldTraceValue(const Instruction *I) {
194 return
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000195 I->getType() != Type::VoidTy &&
196 TraceThisOpCode(I->getOpcode()) &&
197 (isa<LoadInst>(I) || LiveAtBBExit(I));
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000198}
199
Chris Lattner29c14732001-12-14 16:26:05 +0000200static string getPrintfCodeFor(const Value *V) {
201 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000202 if (V->getType()->isFloatingPoint())
203 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000204 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000205 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000206 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000207 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner65ad3722002-09-03 01:07:35 +0000208 else if (V->getType()->isIntegral())
Chris Lattner29c14732001-12-14 16:26:05 +0000209 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000210
Chris Lattner649f5dd2002-04-14 06:15:24 +0000211 assert(0 && "Illegal value to print out...");
212 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000213}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000214
215
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000216static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000217 string Message,
218 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000219 // Escape Message by replacing all % characters with %% chars.
Chris Lattner1a33e312002-10-17 16:22:08 +0000220 string Tmp;
221 std::swap(Tmp, Message);
222 string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
223 while (I != Tmp.end()) {
224 Message.append(Tmp.begin(), I);
225 Message += "%%";
226 ++I; // Make sure to erase the % as well...
227 Tmp.erase(Tmp.begin(), I);
228 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattner29c14732001-12-14 16:26:05 +0000229 }
Chris Lattner80e5ed92003-01-13 00:52:14 +0000230 Message += Tmp;
Chris Lattner29c14732001-12-14 16:26:05 +0000231 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000232
Chris Lattner44571632001-10-18 06:03:05 +0000233 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000234 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
235
236 // Turn the format string into an sbyte *
Chris Lattnerd3646f12003-06-05 04:48:18 +0000237 Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
238 vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
Chris Lattner44571632001-10-18 06:03:05 +0000239
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000240 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000241 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
242 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000243 if (V->getType() != SBP) // Cast pointer to be sbyte*
244 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000245
246 vector<Value*> HashArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000247 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000248 }
249
Chris Lattner44571632001-10-18 06:03:05 +0000250 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000251 vector<Value*> PrintArgs;
252 PrintArgs.push_back(GEP);
253 if (V) PrintArgs.push_back(V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000254 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattner29c14732001-12-14 16:26:05 +0000255}
256
Chris Lattner44571632001-10-18 06:03:05 +0000257
Chris Lattner29c14732001-12-14 16:26:05 +0000258static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000259 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000260 const string &Message, Function *Printf,
261 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000262 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000263 if (V) WriteAsOperand(OutStr, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000264 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000265 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000266}
267
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000268static void
269InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000270 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000271 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000272
273 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000274 if (V->getType() != SBP) // Cast pointer to be sbyte*
275 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
276
Chris Lattner2e0769e2002-05-20 21:43:59 +0000277 vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000278 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000279}
280
281static void
282InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000283 Instruction *InsertBefore,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000284 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000285 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000286 if (V->getType() != SBP) // Cast pointer to be sbyte*
287 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
288
Chris Lattner2e0769e2002-05-20 21:43:59 +0000289 vector<Value*> releaseArgs(1, V);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000290 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000291}
292
293// Look for alloca and free instructions. These are the ptrs to release.
294// Release the free'd pointers immediately. Record the alloca'd pointers
295// to be released on return from the current function.
296//
297static void
298ReleasePtrSeqNumbers(BasicBlock *BB,
299 ExternalFuncs& externalFuncs) {
300
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000301 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattnere408e252003-04-23 16:37:45 +0000302 if (FreeInst *FI = dyn_cast<FreeInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000303 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattnere408e252003-04-23 16:37:45 +0000304 else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000305 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000306}
307
Chris Lattner8d9e3772001-10-18 05:28:08 +0000308
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000309// Insert print instructions at the end of basic block BB for each value
310// computed in BB that is live at the end of BB,
311// or that is stored to memory in BB.
312// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000313// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000314// for printing at the exit from the function. (Note that in each invocation
315// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000316// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000317//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000318static void TraceValuesAtBBExit(BasicBlock *BB,
319 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000320 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000321 // Get an iterator to point to the insertion location, which is
322 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000323 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000324 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000325
Chris Lattner697954c2002-01-20 22:54:45 +0000326 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000327 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000328 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
329 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000330
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000331 // Insert a print instruction for each instruction preceding InsertPos.
332 // The print instructions must go before InsertPos, so we use the
333 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000334 //
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000335 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
Chris Lattnere408e252003-04-23 16:37:45 +0000336 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000337 // Trace the stored value and address
338 InsertVerbosePrintInst(SI->getOperand(0), BB, InsertPos,
339 " (store value) ", Printf, HashPtrToSeqNum);
340 InsertVerbosePrintInst(SI->getOperand(1), BB, InsertPos,
341 " (store addr ) ", Printf, HashPtrToSeqNum);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000342 }
Vikram S. Adve919fc8c2003-07-11 21:57:43 +0000343 else if (ShouldTraceValue(II))
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000344 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000345 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000346}
347
Chris Lattner11982662002-07-23 18:04:15 +0000348static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000349 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000350 // Get an iterator to point to the insertion location
Chris Lattner02a3be02003-09-20 14:39:18 +0000351 BasicBlock &BB = F.getEntryBlock();
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000352 Instruction *InsertPos = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000353
Chris Lattner697954c2002-01-20 22:54:45 +0000354 std::ostringstream OutStr;
Chris Lattner80e5ed92003-01-13 00:52:14 +0000355 WriteAsOperand(OutStr, &F);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000356 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000357 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000358
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000359 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000360 unsigned ArgNo = 0;
Chris Lattner11982662002-07-23 18:04:15 +0000361 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000362 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000363 " Arg #" + utostr(ArgNo) + ": ", Printf,
364 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000365 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000366}
367
368
Chris Lattner79df7c02002-03-26 18:01:55 +0000369static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000370 Function *Printf,
371 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000372 // Get an iterator to point to the insertion location
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000373 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000374
Chris Lattner697954c2002-01-20 22:54:45 +0000375 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000376 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000377 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000378 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000379
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000380 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000381 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000382 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000383 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000384}
385
386
Chris Lattner11982662002-07-23 18:04:15 +0000387bool InsertTraceCode::runOnFunction(Function &F) {
388 if (!TraceThisFunction(F))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000389 return false;
390
Chris Lattner79df7c02002-03-26 18:01:55 +0000391 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000392 vector<BasicBlock*> exitBlocks;
393
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000394 // Insert code to trace values at function entry
Chris Lattner11982662002-07-23 18:04:15 +0000395 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
396 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000397
398 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000399 if (!DisablePtrHashing)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000400 new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
Chris Lattner02a3be02003-09-20 14:39:18 +0000401 F.getEntryBlock().begin());
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000402
Chris Lattner11982662002-07-23 18:04:15 +0000403 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000404 if (isa<ReturnInst>(BB->getTerminator()))
405 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner11982662002-07-23 18:04:15 +0000406
407 // Insert trace code if this basic block is interesting...
408 handleBasicBlock(BB, valuesStoredInFunction);
409
Chris Lattner2e0769e2002-05-20 21:43:59 +0000410 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000411 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000412 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000413
Chris Lattner11982662002-07-23 18:04:15 +0000414 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000415 {
416 // Insert code to trace values at function exit
Chris Lattner11982662002-07-23 18:04:15 +0000417 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
418 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000419
420 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000421 if (!DisablePtrHashing)
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000422 new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
423 exitBlocks[i]->getTerminator());
Chris Lattner29c14732001-12-14 16:26:05 +0000424 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000425
Chris Lattner8d9e3772001-10-18 05:28:08 +0000426 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000427}