blob: 714bf7637d7202aa5f054b5fbe84a7136ed165fb [file] [log] [blame]
Chris Lattner29c14732001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner649f5dd2002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattner29c14732001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advedf1892f2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
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,
26 cl::desc("Disable pointer hashing"));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000027
Chris Lattner5ff62e92002-07-22 02:10:13 +000028static cl::list<string>
29TraceFuncName("tracefunc", cl::desc("trace only specific functions"),
Chris Lattner46897282002-07-22 02:17:27 +000030 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000031
Chris Lattner11982662002-07-23 18:04:15 +000032static void TraceValuesAtBBExit(BasicBlock *BB,
33 Function *Printf, Function* HashPtrToSeqNum,
34 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve1e2ddcf2002-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 Lattner11982662002-07-23 18:04:15 +000039inline static bool
40TraceThisFunction(Function &func)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000041{
Chris Lattner5ff62e92002-07-22 02:10:13 +000042 if (TraceFuncName.size() == 0)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000043 return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000044
Chris Lattner11982662002-07-23 18:04:15 +000045 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func.getName())
Chris Lattner2e0769e2002-05-20 21:43:59 +000046 != TraceFuncName.end();
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000047}
48
49
Chris Lattnerbd0ef772002-02-26 21:46:54 +000050namespace {
Chris Lattner2e0769e2002-05-20 21:43:59 +000051 struct ExternalFuncs {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000052 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
53 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner7e708292002-06-25 16:13:24 +000054 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000055 };
56
Chris Lattnerf57b8452002-04-27 06:56:12 +000057 class InsertTraceCode : public FunctionPass {
Chris Lattner11982662002-07-23 18:04:15 +000058 protected:
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000059 ExternalFuncs externalFuncs;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000060 public:
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000062 // Add a prototype for runtime functions not already in the program.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000063 //
Chris Lattner7e708292002-06-25 16:13:24 +000064 bool doInitialization(Module &M);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000065
66 //--------------------------------------------------------------------------
67 // Function InsertCodeToTraceValues
68 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000069 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000070 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000071 //
Chris Lattner11982662002-07-23 18:04:15 +000072 bool doit(Function *M);
73
74 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) = 0;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000075
Chris Lattner649f5dd2002-04-14 06:15:24 +000076 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000077 //
Chris Lattner11982662002-07-23 18:04:15 +000078 bool runOnFunction(Function &F);
Chris Lattner97e52e42002-04-28 21:27:06 +000079
80 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
81 AU.preservesCFG();
82 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000083 };
Chris Lattner11982662002-07-23 18:04:15 +000084
85 struct FunctionTracer : public InsertTraceCode {
86 // Ignore basic blocks here...
87 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {}
88 };
89
90 struct BasicBlockTracer : public InsertTraceCode {
91 // Trace basic blocks here...
92 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) {
93 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
94 externalFuncs.HashPtrFunc, &VI);
95 }
96 };
97
98 // Register the passes...
Chris Lattnera6275cc2002-07-26 21:12:46 +000099 RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
100 RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000101} // end anonymous namespace
102
103
Chris Lattnerf57b8452002-04-27 06:56:12 +0000104Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner11982662002-07-23 18:04:15 +0000105 return new FunctionTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000106}
107
Chris Lattner649f5dd2002-04-14 06:15:24 +0000108Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner11982662002-07-23 18:04:15 +0000109 return new BasicBlockTracer();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000110}
111
Chris Lattner11982662002-07-23 18:04:15 +0000112
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000113// Add a prototype for external functions used by the tracing code.
Chris Lattner29c14732001-12-14 16:26:05 +0000114//
Chris Lattner7e708292002-06-25 16:13:24 +0000115void ExternalFuncs::doInitialization(Module &M) {
Chris Lattner29c14732001-12-14 16:26:05 +0000116 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +0000117 const FunctionType *MTy =
118 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner7e708292002-06-25 16:13:24 +0000119 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000120
121 // uint (sbyte*)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000122 const FunctionType *hashFuncTy =
Chris Lattner2e0769e2002-05-20 21:43:59 +0000123 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner7e708292002-06-25 16:13:24 +0000124 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000125
Chris Lattner2e0769e2002-05-20 21:43:59 +0000126 // void (sbyte*)
127 const FunctionType *voidSBPFuncTy =
128 FunctionType::get(Type::VoidTy, vector<const Type*>(1, SBP), false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000129
Chris Lattner7e708292002-06-25 16:13:24 +0000130 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
131 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000132
133 const FunctionType *voidvoidFuncTy =
134 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
135
Chris Lattner7e708292002-06-25 16:13:24 +0000136 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
137 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000138 voidvoidFuncTy);
139}
140
141
142// Add a prototype for external functions used by the tracing code.
143//
Chris Lattner7e708292002-06-25 16:13:24 +0000144bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000145 externalFuncs.doInitialization(M);
Chris Lattner89851072002-03-29 03:43:24 +0000146 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +0000147}
148
Chris Lattner29c14732001-12-14 16:26:05 +0000149
150static inline GlobalVariable *getStringRef(Module *M, const string &str) {
151 // Create a constant internal string reference...
152 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +0000153
154 // Create the global variable and record it in the module
155 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +0000156 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
157 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000158 M->getGlobalList().push_back(GV);
159 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000160}
161
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000162
Chris Lattner29c14732001-12-14 16:26:05 +0000163//
164// Check if this instruction has any uses outside its basic block,
165// or if it used by either a Call or Return instruction.
166//
167static inline bool LiveAtBBExit(const Instruction* I) {
168 const BasicBlock *BB = I->getParent();
169 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
170 if (const Instruction *UI = dyn_cast<Instruction>(*U))
171 if (UI->getParent() != BB || isa<ReturnInst>(UI))
172 return true;
173
174 return false;
175}
176
177
178static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000179 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000180 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000181 //
182 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000183 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000184 opCode != Instruction::PHINode &&
185 opCode != Instruction::Cast);
186}
187
Chris Lattner29c14732001-12-14 16:26:05 +0000188
189static bool ShouldTraceValue(const Instruction *I) {
190 return
191 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
192 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000193}
194
Chris Lattner29c14732001-12-14 16:26:05 +0000195static string getPrintfCodeFor(const Value *V) {
196 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000197 if (V->getType()->isFloatingPoint())
198 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000199 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000200 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000201 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000202 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000203 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000204 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000205
Chris Lattner649f5dd2002-04-14 06:15:24 +0000206 assert(0 && "Illegal value to print out...");
207 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000208}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000209
210
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000211static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
212 string Message,
213 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000214 // Escape Message by replacing all % characters with %% chars.
215 unsigned Offset = 0;
216 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner649f5dd2002-04-14 06:15:24 +0000217 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000218 Offset += 2; // Skip over the new %'s
219 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000220
Chris Lattner29c14732001-12-14 16:26:05 +0000221 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000222
Chris Lattner44571632001-10-18 06:03:05 +0000223 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000224 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
225
226 // Turn the format string into an sbyte *
227 Instruction *GEP =
228 new GetElementPtrInst(fmtVal,
229 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
230 "trstr");
Chris Lattner7e708292002-06-25 16:13:24 +0000231 BBI = ++BB->getInstList().insert(BBI, GEP);
Chris Lattner44571632001-10-18 06:03:05 +0000232
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000233 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000234 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
235 const Type *SBP = PointerType::get(Type::SByteTy);
236 if (V->getType() != SBP) { // Cast pointer to be sbyte*
237 Instruction *I = new CastInst(V, SBP, "Hash_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000238 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000239 V = I;
240 }
241
242 vector<Value*> HashArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000243 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
Chris Lattner7e708292002-06-25 16:13:24 +0000244 BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
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);
251 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner7e708292002-06-25 16:13:24 +0000252 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner29c14732001-12-14 16:26:05 +0000253}
254
Chris Lattner44571632001-10-18 06:03:05 +0000255
Chris Lattner29c14732001-12-14 16:26:05 +0000256static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
257 BasicBlock::iterator &BBI,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000258 const string &Message, Function *Printf,
259 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000260 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000261 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000262 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
263 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000264}
265
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000266static void
267InsertReleaseInst(Value *V, BasicBlock *BB,
268 BasicBlock::iterator &BBI,
269 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000270
271 const Type *SBP = PointerType::get(Type::SByteTy);
272 if (V->getType() != SBP) { // Cast pointer to be sbyte*
273 Instruction *I = new CastInst(V, SBP, "RPSN_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000274 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000275 V = I;
276 }
277 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000278 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000279 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000280}
281
282static void
283InsertRecordInst(Value *V, BasicBlock *BB,
284 BasicBlock::iterator &BBI,
285 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000286 const Type *SBP = PointerType::get(Type::SByteTy);
287 if (V->getType() != SBP) { // Cast pointer to be sbyte*
288 Instruction *I = new CastInst(V, SBP, "RP_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000289 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000290 V = I;
291 }
292 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000293 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000294 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000295}
296
297static void
298InsertPushOnEntryFunc(Function *M,
299 Function* PushOnEntryFunc) {
300 // Get an iterator to point to the insertion location
Chris Lattner7e708292002-06-25 16:13:24 +0000301 BasicBlock &BB = M->getEntryNode();
302 BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
303 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000304}
305
306static void
307InsertReleaseRecordedInst(BasicBlock *BB,
308 Function* ReleaseOnReturnFunc) {
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000309 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000310 BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
311 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000312}
313
314// Look for alloca and free instructions. These are the ptrs to release.
315// Release the free'd pointers immediately. Record the alloca'd pointers
316// to be released on return from the current function.
317//
318static void
319ReleasePtrSeqNumbers(BasicBlock *BB,
320 ExternalFuncs& externalFuncs) {
321
322 for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
Chris Lattner7e708292002-06-25 16:13:24 +0000323 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000324 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000325 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000326 {
Chris Lattner7e708292002-06-25 16:13:24 +0000327 BasicBlock::iterator nextI = ++II;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000328 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000329 II = --nextI;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000330 }
331 }
332}
333
Chris Lattner8d9e3772001-10-18 05:28:08 +0000334
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000335// Insert print instructions at the end of basic block BB for each value
336// computed in BB that is live at the end of BB,
337// or that is stored to memory in BB.
338// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000339// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000340// for printing at the exit from the function. (Note that in each invocation
341// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000342// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000343//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000344static void TraceValuesAtBBExit(BasicBlock *BB,
345 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000346 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000347 // Get an iterator to point to the insertion location, which is
348 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000349 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000350 BasicBlock::iterator InsertPos = --BB->end();
351 assert(InsertPos->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000352
Chris Lattner697954c2002-01-20 22:54:45 +0000353 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000354 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000355 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
356 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000357
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000358 // Insert a print instruction for each instruction preceding InsertPos.
359 // The print instructions must go before InsertPos, so we use the
360 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000361 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000362 if (InsertPos != BB->begin()) { // there's at least one instr before InsertPos
363 BasicBlock::iterator II = BB->begin(), IEincl = InsertPos;
364 --IEincl;
365 do { // do from II up to IEincl, inclusive
366 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
367 assert(valuesStoredInFunction &&
368 "Should not be printing a store instruction at function exit");
Chris Lattner8e4fc252002-08-22 22:48:32 +0000369 LoadInst *LI = new LoadInst(SI->getPointerOperand(), "reload." +
370 SI->getPointerOperand()->getName());
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000371 InsertPos = ++BB->getInstList().insert(InsertPos, LI);
372 valuesStoredInFunction->push_back(LI);
373 }
374 if (ShouldTraceValue(II))
375 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf,HashPtrToSeqNum);
376 } while (II++ != IEincl);
Chris Lattner29c14732001-12-14 16:26:05 +0000377 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000378}
379
Chris Lattner11982662002-07-23 18:04:15 +0000380static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000381 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000382 // Get an iterator to point to the insertion location
Chris Lattner11982662002-07-23 18:04:15 +0000383 BasicBlock &BB = F.getEntryNode();
Chris Lattner7e708292002-06-25 16:13:24 +0000384 BasicBlock::iterator BBI = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000385
Chris Lattner697954c2002-01-20 22:54:45 +0000386 std::ostringstream OutStr;
Chris Lattner11982662002-07-23 18:04:15 +0000387 WriteAsOperand(OutStr, &F, true);
Chris Lattner7e708292002-06-25 16:13:24 +0000388 InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000389 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000390
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000391 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000392 unsigned ArgNo = 0;
Chris Lattner11982662002-07-23 18:04:15 +0000393 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner7e708292002-06-25 16:13:24 +0000394 InsertVerbosePrintInst(I, &BB, BBI,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000395 " Arg #" + utostr(ArgNo) + ": ", Printf,
396 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000397 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000398}
399
400
Chris Lattner79df7c02002-03-26 18:01:55 +0000401static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000402 Function *Printf,
403 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000404 // Get an iterator to point to the insertion location
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000405 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000406 ReturnInst &Ret = cast<ReturnInst>(BB->back());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000407
Chris Lattner697954c2002-01-20 22:54:45 +0000408 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000409 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000410 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
411 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000412
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000413 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000414 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner7e708292002-06-25 16:13:24 +0000415 InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000416 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000417}
418
419
Chris Lattner11982662002-07-23 18:04:15 +0000420bool InsertTraceCode::runOnFunction(Function &F) {
421 if (!TraceThisFunction(F))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000422 return false;
423
Chris Lattner79df7c02002-03-26 18:01:55 +0000424 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000425 vector<BasicBlock*> exitBlocks;
426
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000427 // Insert code to trace values at function entry
Chris Lattner11982662002-07-23 18:04:15 +0000428 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
429 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000430
431 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000432 if (!DisablePtrHashing)
Chris Lattner11982662002-07-23 18:04:15 +0000433 InsertPushOnEntryFunc(&F, externalFuncs.PushOnEntryFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000434
Chris Lattner11982662002-07-23 18:04:15 +0000435 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000436 if (isa<ReturnInst>(BB->getTerminator()))
437 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner11982662002-07-23 18:04:15 +0000438
439 // Insert trace code if this basic block is interesting...
440 handleBasicBlock(BB, valuesStoredInFunction);
441
Chris Lattner2e0769e2002-05-20 21:43:59 +0000442 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000443 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000444 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000445
Chris Lattner11982662002-07-23 18:04:15 +0000446 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000447 {
448 // Insert code to trace values at function exit
Chris Lattner11982662002-07-23 18:04:15 +0000449 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
450 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000451
452 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000453 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000454 InsertReleaseRecordedInst(exitBlocks[i],
455 externalFuncs.ReleaseOnReturnFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000456 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000457
Chris Lattner8d9e3772001-10-18 05:28:08 +0000458 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000459}