blob: d05d33dc721befae04f46c09d2e2b3f5a0d466b5 [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 Lattnere9bb2df2001-12-03 22:26:30 +000010#include "llvm/ConstantVals.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"
Chris Lattner42a41272002-04-09 18:37:46 +000015#include "llvm/BasicBlock.h"
Chris Lattner79df7c02002-03-26 18:01:55 +000016#include "llvm/Function.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000017#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
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 Lattnerbd0ef772002-02-26 21:46:54 +000025namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000026 class InsertTraceCode : public FunctionPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000027 bool TraceBasicBlockExits, TraceFunctionExits;
28 Function *PrintfFunc;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000029 public:
Chris Lattner79df7c02002-03-26 18:01:55 +000030 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattnerbd0ef772002-02-26 21:46:54 +000031 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner79df7c02002-03-26 18:01:55 +000032 TraceFunctionExits(traceFunctionExits) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000033
34 // Add a prototype for printf if it is not already in the program.
35 //
36 bool doInitialization(Module *M);
37
38 //--------------------------------------------------------------------------
39 // Function InsertCodeToTraceValues
40 //
Chris Lattner649f5dd2002-04-14 06:15:24 +000041 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner79df7c02002-03-26 18:01:55 +000042 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000043 //
Chris Lattner79df7c02002-03-26 18:01:55 +000044 static bool doit(Function *M, bool traceBasicBlockExits,
45 bool traceFunctionExits, Function *Printf);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000046
Chris Lattner649f5dd2002-04-14 06:15:24 +000047 // runOnFunction - This method does the work.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000048 //
Chris Lattnerf57b8452002-04-27 06:56:12 +000049 bool runOnFunction(Function *F) {
Chris Lattner79df7c02002-03-26 18:01:55 +000050 return doit(F, TraceBasicBlockExits, TraceFunctionExits, PrintfFunc);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000051 }
52 };
53} // end anonymous namespace
54
55
Chris Lattnerf57b8452002-04-27 06:56:12 +000056Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000057 return new InsertTraceCode(false, true);
58}
59
Chris Lattner649f5dd2002-04-14 06:15:24 +000060Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061 return new InsertTraceCode(true, true);
62}
63
64
65
66
Chris Lattner29c14732001-12-14 16:26:05 +000067// Add a prototype for printf if it is not already in the program.
68//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000069bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +000070 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +000071 const FunctionType *MTy =
72 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adved8893302001-10-28 21:37:25 +000073
Chris Lattner89851072002-03-29 03:43:24 +000074 PrintfFunc = M->getOrInsertFunction("printf", MTy);
75 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +000076}
77
Chris Lattner29c14732001-12-14 16:26:05 +000078
79static inline GlobalVariable *getStringRef(Module *M, const string &str) {
80 // Create a constant internal string reference...
81 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +000082
83 // Create the global variable and record it in the module
84 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +000085 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
86 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000087 M->getGlobalList().push_back(GV);
88 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000089}
90
Vikram S. Advebedb00d2001-10-18 13:49:22 +000091
Chris Lattner29c14732001-12-14 16:26:05 +000092//
93// Check if this instruction has any uses outside its basic block,
94// or if it used by either a Call or Return instruction.
95//
96static inline bool LiveAtBBExit(const Instruction* I) {
97 const BasicBlock *BB = I->getParent();
98 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
99 if (const Instruction *UI = dyn_cast<Instruction>(*U))
100 if (UI->getParent() != BB || isa<ReturnInst>(UI))
101 return true;
102
103 return false;
104}
105
106
107static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000108 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000109 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000110 //
111 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000112 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000113 opCode != Instruction::PHINode &&
114 opCode != Instruction::Cast);
115}
116
Chris Lattner29c14732001-12-14 16:26:05 +0000117
118static bool ShouldTraceValue(const Instruction *I) {
119 return
120 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
121 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000122}
123
Chris Lattner29c14732001-12-14 16:26:05 +0000124static string getPrintfCodeFor(const Value *V) {
125 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000126 if (V->getType()->isFloatingPoint())
127 return "%g";
128 else if (V->getType() == Type::LabelTy || isa<PointerType>(V->getType()))
129 return "0x%p";
130 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000131 return "%d";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000132
Chris Lattner649f5dd2002-04-14 06:15:24 +0000133 assert(0 && "Illegal value to print out...");
134 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000135}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000136
137
Chris Lattner29c14732001-12-14 16:26:05 +0000138static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000139 string Message, Function *Printf) {
Chris Lattner29c14732001-12-14 16:26:05 +0000140 // Escape Message by replacing all % characters with %% chars.
141 unsigned Offset = 0;
142 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner649f5dd2002-04-14 06:15:24 +0000143 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000144 Offset += 2; // Skip over the new %'s
145 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000146
Chris Lattner29c14732001-12-14 16:26:05 +0000147 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000148
Chris Lattner44571632001-10-18 06:03:05 +0000149 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000150 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
151
152 // Turn the format string into an sbyte *
153 Instruction *GEP =
154 new GetElementPtrInst(fmtVal,
155 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
156 "trstr");
157 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000158
159 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000160 vector<Value*> PrintArgs;
161 PrintArgs.push_back(GEP);
162 if (V) PrintArgs.push_back(V);
163 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000164 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000165}
166
Chris Lattner44571632001-10-18 06:03:05 +0000167
Chris Lattner29c14732001-12-14 16:26:05 +0000168static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
169 BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000170 const string &Message, Function *Printf) {
Chris Lattner697954c2002-01-20 22:54:45 +0000171 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000172 if (V) WriteAsOperand(OutStr, V);
173 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000174}
175
176
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000177// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000178// for each value in valueVec[] that is live at the end of that basic block,
179// or that is stored to memory in this basic block.
180// If the value is stored to memory, we load it back before printing
Chris Lattner79df7c02002-03-26 18:01:55 +0000181// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000182// for printing at the exit from the function. (Note that in each invocation
183// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000184// store instruction).
185// *bb must be the block in which the value is computed;
186// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000187//
Chris Lattner79df7c02002-03-26 18:01:55 +0000188static void TraceValuesAtBBExit(BasicBlock *BB, Function *Printf,
189 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000190 // Get an iterator to point to the insertion location, which is
191 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000192 //
Chris Lattner29c14732001-12-14 16:26:05 +0000193 BasicBlock::iterator InsertPos = BB->end()-1;
194 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000195
Vikram S. Adved8893302001-10-28 21:37:25 +0000196 // If the terminator is a conditional branch, insert the trace code just
197 // before the instruction that computes the branch condition (just to
198 // avoid putting a call between the CC-setting instruction and the branch).
199 // Use laterInstrSet to mark instructions that come after the setCC instr
200 // because those cannot be traced at the location we choose.
201 //
Chris Lattner29c14732001-12-14 16:26:05 +0000202 Instruction *SetCC = 0;
203 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
204 if (!Branch->isUnconditional())
205 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
206 if (I->getParent() == BB) {
207 SetCC = I;
208 while (*InsertPos != SetCC)
209 --InsertPos; // Back up until we can insert before the setcc
210 }
211
212 // Copy all of the instructions into a vector to avoid problems with Setcc
213 const vector<Instruction*> Insts(BB->begin(), InsertPos);
214
Chris Lattner697954c2002-01-20 22:54:45 +0000215 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000216 WriteAsOperand(OutStr, BB, false);
217 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
218
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000219 // Insert a print instruction for each value.
220 //
Chris Lattner29c14732001-12-14 16:26:05 +0000221 for (vector<Instruction*>::const_iterator II = Insts.begin(),
222 IE = Insts.end(); II != IE; ++II) {
223 Instruction *I = *II;
224 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000225 assert(valuesStoredInFunction &&
Chris Lattner649f5dd2002-04-14 06:15:24 +0000226 "Should not be printing a store instruction at function exit");
Chris Lattner29c14732001-12-14 16:26:05 +0000227 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
228 "reload");
229 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner79df7c02002-03-26 18:01:55 +0000230 valuesStoredInFunction->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000231 }
Chris Lattner29c14732001-12-14 16:26:05 +0000232 if (ShouldTraceValue(I))
233 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
234 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000235}
236
Chris Lattner79df7c02002-03-26 18:01:55 +0000237static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000238 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000239 BasicBlock *BB = M->getEntryNode();
240 BasicBlock::iterator BBI = BB->begin();
241
Chris Lattner697954c2002-01-20 22:54:45 +0000242 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000243 WriteAsOperand(OutStr, M, true);
Chris Lattner649f5dd2002-04-14 06:15:24 +0000244 InsertPrintInst(0, BB, BBI, "ENTERING FUNCTION: " + OutStr.str(), Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000245
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000246 // Now print all the incoming arguments
Chris Lattner79df7c02002-03-26 18:01:55 +0000247 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattner29c14732001-12-14 16:26:05 +0000248 unsigned ArgNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000249 for (Function::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000250 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
Chris Lattner73e21422002-04-09 19:48:49 +0000251 InsertVerbosePrintInst((Value*)*I, BB, BBI,
Chris Lattner29c14732001-12-14 16:26:05 +0000252 " Arg #" + utostr(ArgNo), Printf);
253 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000254}
255
256
Chris Lattner79df7c02002-03-26 18:01:55 +0000257static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
258 Function *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000259 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000260 BasicBlock::iterator BBI = BB->end()-1;
261 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000262
Chris Lattner697954c2002-01-20 22:54:45 +0000263 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000264 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner649f5dd2002-04-14 06:15:24 +0000265 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(), Printf);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000266
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000267 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000268 if (BB->getParent()->getReturnType() != Type::VoidTy)
269 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000270}
271
272
Chris Lattner79df7c02002-03-26 18:01:55 +0000273bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
274 bool traceFunctionEvents, Function *Printf) {
275 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000276 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000277
Chris Lattner79df7c02002-03-26 18:01:55 +0000278 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000279 vector<BasicBlock*> exitBlocks;
280
Chris Lattner79df7c02002-03-26 18:01:55 +0000281 if (traceFunctionEvents)
282 InsertCodeToShowFunctionEntry(M, Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000283
Chris Lattner79df7c02002-03-26 18:01:55 +0000284 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattner29c14732001-12-14 16:26:05 +0000285 BasicBlock *BB = *BI;
286 if (isa<ReturnInst>(BB->getTerminator()))
287 exitBlocks.push_back(BB); // record this as an exit block
288
289 if (traceBasicBlockExits)
Chris Lattner79df7c02002-03-26 18:01:55 +0000290 TraceValuesAtBBExit(BB, Printf, &valuesStoredInFunction);
Chris Lattner29c14732001-12-14 16:26:05 +0000291 }
292
Chris Lattner79df7c02002-03-26 18:01:55 +0000293 if (traceFunctionEvents)
Chris Lattner29c14732001-12-14 16:26:05 +0000294 for (unsigned i=0; i < exitBlocks.size(); ++i) {
295#if 0
Chris Lattner79df7c02002-03-26 18:01:55 +0000296 TraceValuesAtBBExit(valuesStoredInFunction, exitBlocks[i], module,
297 /*indent*/ 0, /*isFunctionExit*/ true,
298 /*valuesStoredInFunction*/ NULL);
Chris Lattner29c14732001-12-14 16:26:05 +0000299#endif
Chris Lattner79df7c02002-03-26 18:01:55 +0000300 InsertCodeToShowFunctionExit(exitBlocks[i], Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000301 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000302
Chris Lattner8d9e3772001-10-18 05:28:08 +0000303 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000304}