blob: 431b9bb83ddc11c6a5ae554e72795a74686b343f [file] [log] [blame]
Chris Lattnerf1197b02001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
Chris Lattner7e358902002-04-14 06:15:24 +00003// Support for inserting LLVM code to print values at basic block and function
Chris Lattnerf1197b02001-12-14 16:26:05 +00004// exits.
5//
6//===----------------------------------------------------------------------===//
Vikram S. Advea200a6c2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
Chris Lattnerca142372002-04-28 19:55:58 +00009#include "llvm/Constants.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000010#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000011#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000012#include "llvm/iTerminators.h"
13#include "llvm/iOther.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000014#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000015#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000016#include "llvm/Assembly/Writer.h"
Vikram S. Adve47f37c32002-05-19 15:39:02 +000017#include "Support/CommandLine.h"
Chris Lattner5de22042001-11-27 00:03:19 +000018#include "Support/StringExtras.h"
Chris Lattnerace7b8d2002-05-20 21:43:59 +000019#include <algorithm>
Chris Lattnere2c61262001-10-18 20:06:03 +000020#include <sstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000021using std::vector;
22using std::string;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000023
Chris Lattnerf5cad152002-07-22 02:10:13 +000024static cl::opt<bool>
25DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
26 cl::desc("Disable pointer hashing"));
Vikram S. Adve47f37c32002-05-19 15:39:02 +000027
Chris Lattnerf5cad152002-07-22 02:10:13 +000028static cl::list<string>
29TraceFuncName("tracefunc", cl::desc("trace only specific functions"),
Chris Lattnerdf7633f2002-07-22 02:17:27 +000030 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000031
Chris Lattner6d216fd2002-07-23 18:04:15 +000032static void TraceValuesAtBBExit(BasicBlock *BB,
33 Function *Printf, Function* HashPtrToSeqNum,
34 vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000035
36// We trace a particular function if no functions to trace were specified
37// or if the function is in the specified list.
38//
Chris Lattner6d216fd2002-07-23 18:04:15 +000039inline static bool
40TraceThisFunction(Function &func)
Vikram S. Adve47f37c32002-05-19 15:39:02 +000041{
Chris Lattnerf5cad152002-07-22 02:10:13 +000042 if (TraceFuncName.size() == 0)
Vikram S. Adve47f37c32002-05-19 15:39:02 +000043 return true;
Chris Lattnerace7b8d2002-05-20 21:43:59 +000044
Chris Lattner6d216fd2002-07-23 18:04:15 +000045 return std::find(TraceFuncName.begin(), TraceFuncName.end(), func.getName())
Chris Lattnerace7b8d2002-05-20 21:43:59 +000046 != TraceFuncName.end();
Vikram S. Adve47f37c32002-05-19 15:39:02 +000047}
48
49
Chris Lattner04805fa2002-02-26 21:46:54 +000050namespace {
Chris Lattnerace7b8d2002-05-20 21:43:59 +000051 struct ExternalFuncs {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000052 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
53 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner113f4f42002-06-25 16:13:24 +000054 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve47f37c32002-05-19 15:39:02 +000055 };
56
Chris Lattnerc8e66542002-04-27 06:56:12 +000057 class InsertTraceCode : public FunctionPass {
Chris Lattner6d216fd2002-07-23 18:04:15 +000058 protected:
Vikram S. Adve47f37c32002-05-19 15:39:02 +000059 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000060 public:
Chris Lattner04805fa2002-02-26 21:46:54 +000061
Vikram S. Adve47f37c32002-05-19 15:39:02 +000062 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000063 //
Chris Lattner113f4f42002-06-25 16:13:24 +000064 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000065
66 //--------------------------------------------------------------------------
67 // Function InsertCodeToTraceValues
68 //
Chris Lattner7e358902002-04-14 06:15:24 +000069 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000070 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000071 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000072 bool doit(Function *M);
73
74 virtual void handleBasicBlock(BasicBlock *BB, vector<Instruction*> &VI) = 0;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000075
Chris Lattner7e358902002-04-14 06:15:24 +000076 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000077 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000078 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000079
80 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000081 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000082 }
Chris Lattner04805fa2002-02-26 21:46:54 +000083 };
Chris Lattner6d216fd2002-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 Lattnerc8b70922002-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 Lattner04805fa2002-02-26 21:46:54 +0000101} // end anonymous namespace
102
103
Chris Lattnerc8e66542002-04-27 06:56:12 +0000104Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000105 return new FunctionTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000106}
107
Chris Lattner7e358902002-04-14 06:15:24 +0000108Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000109 return new BasicBlockTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000110}
111
Chris Lattner6d216fd2002-07-23 18:04:15 +0000112
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000113// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000114//
Chris Lattner113f4f42002-06-25 16:13:24 +0000115void ExternalFuncs::doInitialization(Module &M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000116 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000117 const FunctionType *MTy =
118 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000119 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000120
121 // uint (sbyte*)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000122 const FunctionType *hashFuncTy =
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000123 FunctionType::get(Type::UIntTy, vector<const Type*>(1, SBP), false);
Chris Lattner113f4f42002-06-25 16:13:24 +0000124 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", hashFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000125
Chris Lattnerace7b8d2002-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. Adve47f37c32002-05-19 15:39:02 +0000129
Chris Lattner113f4f42002-06-25 16:13:24 +0000130 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum", voidSBPFuncTy);
131 RecordPtrFunc = M.getOrInsertFunction("RecordPointer", voidSBPFuncTy);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000132
133 const FunctionType *voidvoidFuncTy =
134 FunctionType::get(Type::VoidTy, vector<const Type*>(), false);
135
Chris Lattner113f4f42002-06-25 16:13:24 +0000136 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", voidvoidFuncTy);
137 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000138 voidvoidFuncTy);
139}
140
141
142// Add a prototype for external functions used by the tracing code.
143//
Chris Lattner113f4f42002-06-25 16:13:24 +0000144bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000145 externalFuncs.doInitialization(M);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000146 return false;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000147}
148
Chris Lattnerf1197b02001-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. Adve9f129ff2002-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 Lattnerf1197b02001-12-14 16:26:05 +0000156 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
157 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +0000158 M->getGlobalList().push_back(GV);
159 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000160}
161
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000162
Chris Lattnerf1197b02001-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. Advea200a6c2001-10-14 23:18:45 +0000179 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000180 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000181 //
Chris Lattner69ce8672002-10-13 19:39:16 +0000182 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000183 opCode != Instruction::Alloca &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000184 opCode != Instruction::PHINode &&
185 opCode != Instruction::Cast);
186}
187
Chris Lattnerf1197b02001-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. Advea0db1c92001-10-18 18:16:11 +0000193}
194
Chris Lattnerf1197b02001-12-14 16:26:05 +0000195static string getPrintfCodeFor(const Value *V) {
196 if (V == 0) return "";
Chris Lattner7e358902002-04-14 06:15:24 +0000197 if (V->getType()->isFloatingPoint())
198 return "%g";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000199 else if (V->getType() == Type::LabelTy)
Chris Lattner7e358902002-04-14 06:15:24 +0000200 return "0x%p";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000201 else if (isa<PointerType>(V->getType()))
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000202 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattnerbc6bdc22002-09-03 01:07:35 +0000203 else if (V->getType()->isIntegral())
Chris Lattnerf1197b02001-12-14 16:26:05 +0000204 return "%d";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000205
Chris Lattner7e358902002-04-14 06:15:24 +0000206 assert(0 && "Illegal value to print out...");
207 return "";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000208}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000209
210
Chris Lattner28a8d242002-09-10 17:04:02 +0000211static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000212 string Message,
213 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000214 // Escape Message by replacing all % characters with %% chars.
Chris Lattner26783a52002-10-17 16:22:08 +0000215 string Tmp;
216 std::swap(Tmp, Message);
217 string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
218 while (I != Tmp.end()) {
219 Message.append(Tmp.begin(), I);
220 Message += "%%";
221 ++I; // Make sure to erase the % as well...
222 Tmp.erase(Tmp.begin(), I);
223 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattnerf1197b02001-12-14 16:26:05 +0000224 }
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000225
Chris Lattnerf1197b02001-12-14 16:26:05 +0000226 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000227
Chris Lattner5309e102001-10-18 06:03:05 +0000228 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000229 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
230
231 // Turn the format string into an sbyte *
232 Instruction *GEP =
233 new GetElementPtrInst(fmtVal,
Chris Lattnerb9d9e0f2002-09-11 01:21:29 +0000234 vector<Value*>(2,ConstantSInt::get(Type::LongTy, 0)),
Chris Lattner28a8d242002-09-10 17:04:02 +0000235 "trstr", InsertBefore);
Chris Lattner5309e102001-10-18 06:03:05 +0000236
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000237 // Insert a call to the hash function if this is a pointer value
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000238 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
239 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-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 Lattnerace7b8d2002-05-20 21:43:59 +0000242
243 vector<Value*> HashArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000244 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000245 }
246
Chris Lattner5309e102001-10-18 06:03:05 +0000247 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000248 vector<Value*> PrintArgs;
249 PrintArgs.push_back(GEP);
250 if (V) PrintArgs.push_back(V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000251 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000252}
253
Chris Lattner5309e102001-10-18 06:03:05 +0000254
Chris Lattnerf1197b02001-12-14 16:26:05 +0000255static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000256 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000257 const string &Message, Function *Printf,
258 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000259 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000260 if (V) WriteAsOperand(OutStr, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000261 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000262 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000263}
264
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000265static void
266InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000267 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000268 Function* ReleasePtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000269
270 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-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 Lattnerace7b8d2002-05-20 21:43:59 +0000274 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000275 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000276}
277
278static void
279InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000280 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000281 Function* RecordPtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000282 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-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 Lattnerace7b8d2002-05-20 21:43:59 +0000286 vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000287 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-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 Lattner28a8d242002-09-10 17:04:02 +0000298 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattner113f4f42002-06-25 16:13:24 +0000299 if (FreeInst *FI = dyn_cast<FreeInst>(&*II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000300 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattner113f4f42002-06-25 16:13:24 +0000301 else if (AllocaInst *AI = dyn_cast<AllocaInst>(&*II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000302 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000303}
304
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000305
Vikram S. Adve4b581be2002-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 Lattner57698e22002-03-26 18:01:55 +0000310// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-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. Advea0db1c92001-10-18 18:16:11 +0000313// store instruction).
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000314//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000315static void TraceValuesAtBBExit(BasicBlock *BB,
316 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner57698e22002-03-26 18:01:55 +0000317 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-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. Advea200a6c2001-10-14 23:18:45 +0000320 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000321 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000322
Chris Lattner7f74a562002-01-20 22:54:45 +0000323 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000324 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000325 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
326 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000327
Vikram S. Adve4b581be2002-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. Advea200a6c2001-10-14 23:18:45 +0000331 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000332 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
333 if (StoreInst *SI = dyn_cast<StoreInst>(&*II)) {
334 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 Lattnerf1197b02001-12-14 16:26:05 +0000343 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000344}
345
Chris Lattner6d216fd2002-07-23 18:04:15 +0000346static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000347 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000348 // Get an iterator to point to the insertion location
Chris Lattner6d216fd2002-07-23 18:04:15 +0000349 BasicBlock &BB = F.getEntryNode();
Chris Lattner28a8d242002-09-10 17:04:02 +0000350 Instruction *InsertPos = BB.begin();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000351
Chris Lattner7f74a562002-01-20 22:54:45 +0000352 std::ostringstream OutStr;
Chris Lattner6d216fd2002-07-23 18:04:15 +0000353 WriteAsOperand(OutStr, &F, true);
Chris Lattner28a8d242002-09-10 17:04:02 +0000354 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000355 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000356
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000357 // Now print all the incoming arguments
Chris Lattnerf1197b02001-12-14 16:26:05 +0000358 unsigned ArgNo = 0;
Chris Lattner6d216fd2002-07-23 18:04:15 +0000359 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner28a8d242002-09-10 17:04:02 +0000360 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000361 " Arg #" + utostr(ArgNo) + ": ", Printf,
362 HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000363 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000364}
365
366
Chris Lattner57698e22002-03-26 18:01:55 +0000367static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000368 Function *Printf,
369 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000370 // Get an iterator to point to the insertion location
Chris Lattner28a8d242002-09-10 17:04:02 +0000371 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000372
Chris Lattner7f74a562002-01-20 22:54:45 +0000373 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000374 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner28a8d242002-09-10 17:04:02 +0000375 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000376 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000377
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000378 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000379 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner28a8d242002-09-10 17:04:02 +0000380 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000381 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000382}
383
384
Chris Lattner6d216fd2002-07-23 18:04:15 +0000385bool InsertTraceCode::runOnFunction(Function &F) {
386 if (!TraceThisFunction(F))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000387 return false;
388
Chris Lattner57698e22002-03-26 18:01:55 +0000389 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000390 vector<BasicBlock*> exitBlocks;
391
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000392 // Insert code to trace values at function entry
Chris Lattner6d216fd2002-07-23 18:04:15 +0000393 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
394 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000395
396 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000397 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000398 new CallInst(externalFuncs.PushOnEntryFunc, vector<Value*>(), "",
399 F.getEntryNode().begin());
400
Chris Lattner6d216fd2002-07-23 18:04:15 +0000401 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000402 if (isa<ReturnInst>(BB->getTerminator()))
403 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner6d216fd2002-07-23 18:04:15 +0000404
405 // Insert trace code if this basic block is interesting...
406 handleBasicBlock(BB, valuesStoredInFunction);
407
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000408 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000409 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000410 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000411
Chris Lattner6d216fd2002-07-23 18:04:15 +0000412 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000413 {
414 // Insert code to trace values at function exit
Chris Lattner6d216fd2002-07-23 18:04:15 +0000415 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
416 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000417
418 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000419 if (!DisablePtrHashing)
Chris Lattner28a8d242002-09-10 17:04:02 +0000420 new CallInst(externalFuncs.ReleaseOnReturnFunc, vector<Value*>(), "",
421 exitBlocks[i]->getTerminator());
Chris Lattnerf1197b02001-12-14 16:26:05 +0000422 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000423
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000424 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000425}