blob: 3239e73c98183c3234b62ad31e04c13bceecba3b [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"
9#include "llvm/GlobalVariable.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000010#include "llvm/Constants.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Adve631b9a32001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000015#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000016#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000017#include "llvm/Assembly/Writer.h"
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000018#include "Support/CommandLine.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/StringExtras.h"
Chris Lattner2e0769e2002-05-20 21:43:59 +000020#include <algorithm>
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000021#include <sstream>
Chris Lattner697954c2002-01-20 22:54:45 +000022using std::vector;
23using std::string;
Vikram S. Adved8893302001-10-28 21:37:25 +000024
Chris Lattner5ff62e92002-07-22 02:10:13 +000025static cl::opt<bool>
26DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
27 cl::desc("Disable pointer hashing"));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000028
Chris Lattner5ff62e92002-07-22 02:10:13 +000029static cl::list<string>
30TraceFuncName("tracefunc", cl::desc("trace only specific functions"),
Chris Lattner46897282002-07-22 02:17:27 +000031 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000032
Chris Lattner11982662002-07-23 18:04:15 +000033static void TraceValuesAtBBExit(BasicBlock *BB,
34 Function *Printf, Function* HashPtrToSeqNum,
35 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000036
37// We trace a particular function if no functions to trace were specified
38// or if the function is in the specified list.
39//
Chris Lattner11982662002-07-23 18:04:15 +000040inline static bool
41TraceThisFunction(Function &func)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000042{
Chris Lattner5ff62e92002-07-22 02:10:13 +000043 if (TraceFuncName.size() == 0)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +000044 return true;
Chris Lattner2e0769e2002-05-20 21:43:59 +000045
Chris Lattner11982662002-07-23 18:04:15 +000046 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func.getName())
Chris Lattner2e0769e2002-05-20 21:43:59 +000047 != TraceFuncName.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 {
82 AU.preservesCFG();
83 }
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 Lattner29c14732001-12-14 16:26:05 +0000157 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
158 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000159 M->getGlobalList().push_back(GV);
160 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000161}
162
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000163
Chris Lattner29c14732001-12-14 16:26:05 +0000164//
165// Check if this instruction has any uses outside its basic block,
166// or if it used by either a Call or Return instruction.
167//
168static inline bool LiveAtBBExit(const Instruction* I) {
169 const BasicBlock *BB = I->getParent();
170 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
171 if (const Instruction *UI = dyn_cast<Instruction>(*U))
172 if (UI->getParent() != BB || isa<ReturnInst>(UI))
173 return true;
174
175 return false;
176}
177
178
179static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000180 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000181 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000182 //
183 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000184 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000185 opCode != Instruction::PHINode &&
186 opCode != Instruction::Cast);
187}
188
Chris Lattner29c14732001-12-14 16:26:05 +0000189
190static bool ShouldTraceValue(const Instruction *I) {
191 return
192 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
193 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000194}
195
Chris Lattner29c14732001-12-14 16:26:05 +0000196static string getPrintfCodeFor(const Value *V) {
197 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000198 if (V->getType()->isFloatingPoint())
199 return "%g";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000200 else if (V->getType() == Type::LabelTy)
Chris Lattner649f5dd2002-04-14 06:15:24 +0000201 return "0x%p";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000202 else if (isa<PointerType>(V->getType()))
Chris Lattner2e0769e2002-05-20 21:43:59 +0000203 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000204 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000205 return "%d";
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000206
Chris Lattner649f5dd2002-04-14 06:15:24 +0000207 assert(0 && "Illegal value to print out...");
208 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000209}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000210
211
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000212static void InsertPrintInst(Value *V,BasicBlock *BB, BasicBlock::iterator &BBI,
213 string Message,
214 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattner29c14732001-12-14 16:26:05 +0000215 // Escape Message by replacing all % characters with %% chars.
216 unsigned Offset = 0;
217 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner649f5dd2002-04-14 06:15:24 +0000218 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000219 Offset += 2; // Skip over the new %'s
220 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000221
Chris Lattner29c14732001-12-14 16:26:05 +0000222 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000223
Chris Lattner44571632001-10-18 06:03:05 +0000224 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000225 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
226
227 // Turn the format string into an sbyte *
228 Instruction *GEP =
229 new GetElementPtrInst(fmtVal,
230 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
231 "trstr");
Chris Lattner7e708292002-06-25 16:13:24 +0000232 BBI = ++BB->getInstList().insert(BBI, GEP);
Chris Lattner44571632001-10-18 06:03:05 +0000233
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000234 // Insert a call to the hash function if this is a pointer value
Chris Lattner2e0769e2002-05-20 21:43:59 +0000235 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
236 const Type *SBP = PointerType::get(Type::SByteTy);
237 if (V->getType() != SBP) { // Cast pointer to be sbyte*
238 Instruction *I = new CastInst(V, SBP, "Hash_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000239 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000240 V = I;
241 }
242
243 vector<Value*> HashArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000244 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum");
Chris Lattner7e708292002-06-25 16:13:24 +0000245 BBI = ++BB->getInstList().insert(BBI, cast<Instruction>(V));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000246 }
247
Chris Lattner44571632001-10-18 06:03:05 +0000248 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000249 vector<Value*> PrintArgs;
250 PrintArgs.push_back(GEP);
251 if (V) PrintArgs.push_back(V);
252 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner7e708292002-06-25 16:13:24 +0000253 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner29c14732001-12-14 16:26:05 +0000254}
255
Chris Lattner44571632001-10-18 06:03:05 +0000256
Chris Lattner29c14732001-12-14 16:26:05 +0000257static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
258 BasicBlock::iterator &BBI,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000259 const string &Message, Function *Printf,
260 Function* HashPtrToSeqNum) {
Chris Lattner697954c2002-01-20 22:54:45 +0000261 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000262 if (V) WriteAsOperand(OutStr, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000263 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ",
264 Printf, HashPtrToSeqNum);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000265}
266
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000267static void
268InsertReleaseInst(Value *V, BasicBlock *BB,
269 BasicBlock::iterator &BBI,
270 Function* ReleasePtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000271
272 const Type *SBP = PointerType::get(Type::SByteTy);
273 if (V->getType() != SBP) { // Cast pointer to be sbyte*
274 Instruction *I = new CastInst(V, SBP, "RPSN_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000275 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000276 V = I;
277 }
278 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000279 Instruction *I = new CallInst(ReleasePtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000280 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000281}
282
283static void
284InsertRecordInst(Value *V, BasicBlock *BB,
285 BasicBlock::iterator &BBI,
286 Function* RecordPtrFunc) {
Chris Lattner2e0769e2002-05-20 21:43:59 +0000287 const Type *SBP = PointerType::get(Type::SByteTy);
288 if (V->getType() != SBP) { // Cast pointer to be sbyte*
289 Instruction *I = new CastInst(V, SBP, "RP_cast");
Chris Lattner7e708292002-06-25 16:13:24 +0000290 BBI = ++BB->getInstList().insert(BBI, I);
Chris Lattner2e0769e2002-05-20 21:43:59 +0000291 V = I;
292 }
293 vector<Value*> releaseArgs(1, V);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000294 Instruction *I = new CallInst(RecordPtrFunc, releaseArgs);
Chris Lattner7e708292002-06-25 16:13:24 +0000295 BBI = ++BB->getInstList().insert(BBI, I);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000296}
297
298static void
299InsertPushOnEntryFunc(Function *M,
300 Function* PushOnEntryFunc) {
301 // Get an iterator to point to the insertion location
Chris Lattner7e708292002-06-25 16:13:24 +0000302 BasicBlock &BB = M->getEntryNode();
303 BB.getInstList().insert(BB.begin(), new CallInst(PushOnEntryFunc,
304 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000305}
306
307static void
308InsertReleaseRecordedInst(BasicBlock *BB,
309 Function* ReleaseOnReturnFunc) {
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000310 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000311 BBI = ++BB->getInstList().insert(BBI, new CallInst(ReleaseOnReturnFunc,
312 vector<Value*>()));
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000313}
314
315// Look for alloca and free instructions. These are the ptrs to release.
316// Release the free'd pointers immediately. Record the alloca'd pointers
317// to be released on return from the current function.
318//
319static void
320ReleasePtrSeqNumbers(BasicBlock *BB,
321 ExternalFuncs& externalFuncs) {
322
323 for (BasicBlock::iterator II=BB->begin(); II != BB->end(); ++II) {
Chris Lattner7e708292002-06-25 16:13:24 +0000324 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000325 InsertReleaseInst(FI->getOperand(0), BB,II,externalFuncs.ReleasePtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000326 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000327 {
Chris Lattner7e708292002-06-25 16:13:24 +0000328 BasicBlock::iterator nextI = ++II;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000329 InsertRecordInst(AI, BB, nextI, externalFuncs.RecordPtrFunc);
Chris Lattner7e708292002-06-25 16:13:24 +0000330 II = --nextI;
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000331 }
332 }
333}
334
Chris Lattner8d9e3772001-10-18 05:28:08 +0000335
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000336// Insert print instructions at the end of basic block BB for each value
337// computed in BB that is live at the end of BB,
338// or that is stored to memory in BB.
339// If the value is stored to memory, we load it back before printing it
Chris Lattner79df7c02002-03-26 18:01:55 +0000340// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000341// for printing at the exit from the function. (Note that in each invocation
342// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000343// store instruction).
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000344//
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000345static void TraceValuesAtBBExit(BasicBlock *BB,
346 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner79df7c02002-03-26 18:01:55 +0000347 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000348 // Get an iterator to point to the insertion location, which is
349 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000350 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000351 BasicBlock::iterator InsertPos = --BB->end();
352 assert(InsertPos->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000353
Chris Lattner697954c2002-01-20 22:54:45 +0000354 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000355 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000356 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
357 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000358
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000359 // Insert a print instruction for each instruction preceding InsertPos.
360 // The print instructions must go before InsertPos, so we use the
361 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000362 //
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000363 if (InsertPos != BB->begin()) { // there's at least one instr before InsertPos
364 BasicBlock::iterator II = BB->begin(), IEincl = InsertPos;
365 --IEincl;
366 do { // do from II up to IEincl, inclusive
367 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
368 assert(valuesStoredInFunction &&
369 "Should not be printing a store instruction at function exit");
370 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
Chris Lattner7e708292002-06-25 16:13:24 +0000371 "reload."+SI->getPointerOperand()->getName());
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000372 InsertPos = ++BB->getInstList().insert(InsertPos, LI);
373 valuesStoredInFunction->push_back(LI);
374 }
375 if (ShouldTraceValue(II))
376 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf,HashPtrToSeqNum);
377 } while (II++ != IEincl);
Chris Lattner29c14732001-12-14 16:26:05 +0000378 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000379}
380
Chris Lattner11982662002-07-23 18:04:15 +0000381static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000382 Function* HashPtrToSeqNum){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000383 // Get an iterator to point to the insertion location
Chris Lattner11982662002-07-23 18:04:15 +0000384 BasicBlock &BB = F.getEntryNode();
Chris Lattner7e708292002-06-25 16:13:24 +0000385 BasicBlock::iterator BBI = BB.begin();
Chris Lattner29c14732001-12-14 16:26:05 +0000386
Chris Lattner697954c2002-01-20 22:54:45 +0000387 std::ostringstream OutStr;
Chris Lattner11982662002-07-23 18:04:15 +0000388 WriteAsOperand(OutStr, &F, true);
Chris Lattner7e708292002-06-25 16:13:24 +0000389 InsertPrintInst(0, &BB, BBI, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000390 Printf, HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000391
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000392 // Now print all the incoming arguments
Chris Lattner29c14732001-12-14 16:26:05 +0000393 unsigned ArgNo = 0;
Chris Lattner11982662002-07-23 18:04:15 +0000394 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner7e708292002-06-25 16:13:24 +0000395 InsertVerbosePrintInst(I, &BB, BBI,
Chris Lattner2e0769e2002-05-20 21:43:59 +0000396 " Arg #" + utostr(ArgNo) + ": ", Printf,
397 HashPtrToSeqNum);
Chris Lattner29c14732001-12-14 16:26:05 +0000398 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000399}
400
401
Chris Lattner79df7c02002-03-26 18:01:55 +0000402static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000403 Function *Printf,
404 Function* HashPtrToSeqNum) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000405 // Get an iterator to point to the insertion location
Vikram S. Advef86b4c12002-07-08 23:37:07 +0000406 BasicBlock::iterator BBI = --BB->end();
Chris Lattner7e708292002-06-25 16:13:24 +0000407 ReturnInst &Ret = cast<ReturnInst>(BB->back());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000408
Chris Lattner697954c2002-01-20 22:54:45 +0000409 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000410 WriteAsOperand(OutStr, BB->getParent(), true);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000411 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(),
412 Printf, HashPtrToSeqNum);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000413
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000414 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000415 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner7e708292002-06-25 16:13:24 +0000416 InsertPrintInst(Ret.getReturnValue(), BB, BBI, " Returning: ",
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000417 Printf, HashPtrToSeqNum);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000418}
419
420
Chris Lattner11982662002-07-23 18:04:15 +0000421bool InsertTraceCode::runOnFunction(Function &F) {
422 if (!TraceThisFunction(F))
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000423 return false;
424
Chris Lattner79df7c02002-03-26 18:01:55 +0000425 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000426 vector<BasicBlock*> exitBlocks;
427
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000428 // Insert code to trace values at function entry
Chris Lattner11982662002-07-23 18:04:15 +0000429 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
430 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000431
432 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattner2e0769e2002-05-20 21:43:59 +0000433 if (!DisablePtrHashing)
Chris Lattner11982662002-07-23 18:04:15 +0000434 InsertPushOnEntryFunc(&F, externalFuncs.PushOnEntryFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000435
Chris Lattner11982662002-07-23 18:04:15 +0000436 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattner29c14732001-12-14 16:26:05 +0000437 if (isa<ReturnInst>(BB->getTerminator()))
438 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner11982662002-07-23 18:04:15 +0000439
440 // Insert trace code if this basic block is interesting...
441 handleBasicBlock(BB, valuesStoredInFunction);
442
Chris Lattner2e0769e2002-05-20 21:43:59 +0000443 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000444 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattner29c14732001-12-14 16:26:05 +0000445 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000446
Chris Lattner11982662002-07-23 18:04:15 +0000447 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000448 {
449 // Insert code to trace values at function exit
Chris Lattner11982662002-07-23 18:04:15 +0000450 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
451 externalFuncs.HashPtrFunc);
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000452
453 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattner2e0769e2002-05-20 21:43:59 +0000454 if (!DisablePtrHashing)
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000455 InsertReleaseRecordedInst(exitBlocks[i],
456 externalFuncs.ReleaseOnReturnFunc);
Chris Lattner29c14732001-12-14 16:26:05 +0000457 }
Vikram S. Adve1e2ddcf2002-05-19 15:39:02 +0000458
Chris Lattner8d9e3772001-10-18 05:28:08 +0000459 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000460}