blob: e31aeb8185b37d6c9d5f7d6163f42a41877e5c80 [file] [log] [blame]
Chris Lattner29c14732001-12-14 16:26:05 +00001//===- TraceValues.cpp - Value Tracing for debugging -------------*- C++ -*--=//
2//
3// Support for inserting LLVM code to print values at basic block and method
4// 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 Lattner79df7c02002-03-26 18:01:55 +000015#include "llvm/Function.h"
Vikram S. Advedf1892f2001-10-14 23:18:45 +000016#include "llvm/Module.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000017#include "llvm/Pass.h"
Chris Lattner8d9e3772001-10-18 05:28:08 +000018#include "llvm/Assembly/Writer.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000019#include "Support/StringExtras.h"
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 Lattnerbd0ef772002-02-26 21:46:54 +000024namespace {
25 class InsertTraceCode : public MethodPass {
Chris Lattner79df7c02002-03-26 18:01:55 +000026 bool TraceBasicBlockExits, TraceFunctionExits;
27 Function *PrintfFunc;
Chris Lattnerbd0ef772002-02-26 21:46:54 +000028 public:
Chris Lattner79df7c02002-03-26 18:01:55 +000029 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattnerbd0ef772002-02-26 21:46:54 +000030 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner79df7c02002-03-26 18:01:55 +000031 TraceFunctionExits(traceFunctionExits) {}
Chris Lattnerbd0ef772002-02-26 21:46:54 +000032
33 // Add a prototype for printf if it is not already in the program.
34 //
35 bool doInitialization(Module *M);
36
37 //--------------------------------------------------------------------------
38 // Function InsertCodeToTraceValues
39 //
40 // Inserts tracing code for all live values at basic block and/or method
Chris Lattner79df7c02002-03-26 18:01:55 +000041 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattnerbd0ef772002-02-26 21:46:54 +000042 //
Chris Lattner79df7c02002-03-26 18:01:55 +000043 static bool doit(Function *M, bool traceBasicBlockExits,
44 bool traceFunctionExits, Function *Printf);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000045
46 // runOnMethod - This method does the work. Always successful.
47 //
Chris Lattner79df7c02002-03-26 18:01:55 +000048 bool runOnMethod(Function *F) {
49 return doit(F, TraceBasicBlockExits, TraceFunctionExits, PrintfFunc);
Chris Lattnerbd0ef772002-02-26 21:46:54 +000050 }
51 };
52} // end anonymous namespace
53
54
55Pass *createTraceValuesPassForMethod() { // Just trace methods
56 return new InsertTraceCode(false, true);
57}
58
59Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and methods
60 return new InsertTraceCode(true, true);
61}
62
63
64
65
Chris Lattner29c14732001-12-14 16:26:05 +000066// Add a prototype for printf if it is not already in the program.
67//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000068bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +000069 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +000070 const FunctionType *MTy =
71 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adved8893302001-10-28 21:37:25 +000072
Chris Lattner89851072002-03-29 03:43:24 +000073 PrintfFunc = M->getOrInsertFunction("printf", MTy);
74 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +000075}
76
Chris Lattner29c14732001-12-14 16:26:05 +000077
78static inline GlobalVariable *getStringRef(Module *M, const string &str) {
79 // Create a constant internal string reference...
80 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +000081
82 // Create the global variable and record it in the module
83 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +000084 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
85 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000086 M->getGlobalList().push_back(GV);
87 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000088}
89
Vikram S. Advebedb00d2001-10-18 13:49:22 +000090
Chris Lattner29c14732001-12-14 16:26:05 +000091//
92// Check if this instruction has any uses outside its basic block,
93// or if it used by either a Call or Return instruction.
94//
95static inline bool LiveAtBBExit(const Instruction* I) {
96 const BasicBlock *BB = I->getParent();
97 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
98 if (const Instruction *UI = dyn_cast<Instruction>(*U))
99 if (UI->getParent() != BB || isa<ReturnInst>(UI))
100 return true;
101
102 return false;
103}
104
105
106static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000107 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000108 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000109 //
110 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000111 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000112 opCode != Instruction::PHINode &&
113 opCode != Instruction::Cast);
114}
115
Chris Lattner29c14732001-12-14 16:26:05 +0000116
117static bool ShouldTraceValue(const Instruction *I) {
118 return
119 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
120 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000121}
122
Chris Lattner29c14732001-12-14 16:26:05 +0000123static string getPrintfCodeFor(const Value *V) {
124 if (V == 0) return "";
125 switch (V->getType()->getPrimitiveID()) {
126 case Type::BoolTyID:
127 case Type::UByteTyID: case Type::UShortTyID:
128 case Type::UIntTyID: case Type::ULongTyID:
129 case Type::SByteTyID: case Type::ShortTyID:
130 case Type::IntTyID: case Type::LongTyID:
131 return "%d";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000132
Chris Lattner29c14732001-12-14 16:26:05 +0000133 case Type::FloatTyID: case Type::DoubleTyID:
134 return "%g";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000135
Chris Lattner29c14732001-12-14 16:26:05 +0000136 case Type::LabelTyID: case Type::PointerTyID:
137 return "%p";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000138
Chris Lattner29c14732001-12-14 16:26:05 +0000139 default:
140 assert(0 && "Illegal value to print out...");
141 return "";
142 }
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000143}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000144
145
Chris Lattner29c14732001-12-14 16:26:05 +0000146static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000147 string Message, Function *Printf) {
Chris Lattner29c14732001-12-14 16:26:05 +0000148 // Escape Message by replacing all % characters with %% chars.
149 unsigned Offset = 0;
150 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner29c14732001-12-14 16:26:05 +0000151 Message.replace(Offset, 2, "%%");
152 Offset += 2; // Skip over the new %'s
153 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000154
Chris Lattner29c14732001-12-14 16:26:05 +0000155 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000156
Chris Lattner44571632001-10-18 06:03:05 +0000157 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000158 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
159
160 // Turn the format string into an sbyte *
161 Instruction *GEP =
162 new GetElementPtrInst(fmtVal,
163 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
164 "trstr");
165 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000166
167 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000168 vector<Value*> PrintArgs;
169 PrintArgs.push_back(GEP);
170 if (V) PrintArgs.push_back(V);
171 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000172 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000173}
174
Chris Lattner44571632001-10-18 06:03:05 +0000175
Chris Lattner29c14732001-12-14 16:26:05 +0000176static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
177 BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000178 const string &Message, Function *Printf) {
Chris Lattner697954c2002-01-20 22:54:45 +0000179 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000180 if (V) WriteAsOperand(OutStr, V);
181 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000182}
183
184
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000185// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000186// for each value in valueVec[] that is live at the end of that basic block,
187// or that is stored to memory in this basic block.
188// If the value is stored to memory, we load it back before printing
Chris Lattner79df7c02002-03-26 18:01:55 +0000189// We also return all such loaded values in the vector valuesStoredInFunction
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000190// for printing at the exit from the method. (Note that in each invocation
191// of the method, this will only get the last value stored for each static
192// store instruction).
193// *bb must be the block in which the value is computed;
194// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000195//
Chris Lattner79df7c02002-03-26 18:01:55 +0000196static void TraceValuesAtBBExit(BasicBlock *BB, Function *Printf,
197 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000198 // Get an iterator to point to the insertion location, which is
199 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000200 //
Chris Lattner29c14732001-12-14 16:26:05 +0000201 BasicBlock::iterator InsertPos = BB->end()-1;
202 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000203
Vikram S. Adved8893302001-10-28 21:37:25 +0000204 // If the terminator is a conditional branch, insert the trace code just
205 // before the instruction that computes the branch condition (just to
206 // avoid putting a call between the CC-setting instruction and the branch).
207 // Use laterInstrSet to mark instructions that come after the setCC instr
208 // because those cannot be traced at the location we choose.
209 //
Chris Lattner29c14732001-12-14 16:26:05 +0000210 Instruction *SetCC = 0;
211 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
212 if (!Branch->isUnconditional())
213 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
214 if (I->getParent() == BB) {
215 SetCC = I;
216 while (*InsertPos != SetCC)
217 --InsertPos; // Back up until we can insert before the setcc
218 }
219
220 // Copy all of the instructions into a vector to avoid problems with Setcc
221 const vector<Instruction*> Insts(BB->begin(), InsertPos);
222
Chris Lattner697954c2002-01-20 22:54:45 +0000223 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000224 WriteAsOperand(OutStr, BB, false);
225 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
226
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000227 // Insert a print instruction for each value.
228 //
Chris Lattner29c14732001-12-14 16:26:05 +0000229 for (vector<Instruction*>::const_iterator II = Insts.begin(),
230 IE = Insts.end(); II != IE; ++II) {
231 Instruction *I = *II;
232 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000233 assert(valuesStoredInFunction &&
Chris Lattner29c14732001-12-14 16:26:05 +0000234 "Should not be printing a store instruction at method exit");
235 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
236 "reload");
237 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner79df7c02002-03-26 18:01:55 +0000238 valuesStoredInFunction->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000239 }
Chris Lattner29c14732001-12-14 16:26:05 +0000240 if (ShouldTraceValue(I))
241 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
242 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000243}
244
Chris Lattner79df7c02002-03-26 18:01:55 +0000245static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000246 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000247 BasicBlock *BB = M->getEntryNode();
248 BasicBlock::iterator BBI = BB->begin();
249
Chris Lattner697954c2002-01-20 22:54:45 +0000250 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000251 WriteAsOperand(OutStr, M, true);
252 InsertPrintInst(0, BB, BBI, "ENTERING METHOD: " + OutStr.str(), Printf);
253
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000254 // Now print all the incoming arguments
Chris Lattner79df7c02002-03-26 18:01:55 +0000255 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattner29c14732001-12-14 16:26:05 +0000256 unsigned ArgNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000257 for (Function::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000258 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
259 InsertVerbosePrintInst(*I, BB, BBI,
260 " Arg #" + utostr(ArgNo), Printf);
261 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000262}
263
264
Chris Lattner79df7c02002-03-26 18:01:55 +0000265static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
266 Function *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000267 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000268 BasicBlock::iterator BBI = BB->end()-1;
269 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000270
Chris Lattner697954c2002-01-20 22:54:45 +0000271 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000272 WriteAsOperand(OutStr, BB->getParent(), true);
273 InsertPrintInst(0, BB, BBI, "LEAVING METHOD: " + OutStr.str(), Printf);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000274
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000275 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000276 if (BB->getParent()->getReturnType() != Type::VoidTy)
277 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000278}
279
280
Chris Lattner79df7c02002-03-26 18:01:55 +0000281bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
282 bool traceFunctionEvents, Function *Printf) {
283 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000284 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000285
Chris Lattner79df7c02002-03-26 18:01:55 +0000286 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000287 vector<BasicBlock*> exitBlocks;
288
Chris Lattner79df7c02002-03-26 18:01:55 +0000289 if (traceFunctionEvents)
290 InsertCodeToShowFunctionEntry(M, Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000291
Chris Lattner79df7c02002-03-26 18:01:55 +0000292 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattner29c14732001-12-14 16:26:05 +0000293 BasicBlock *BB = *BI;
294 if (isa<ReturnInst>(BB->getTerminator()))
295 exitBlocks.push_back(BB); // record this as an exit block
296
297 if (traceBasicBlockExits)
Chris Lattner79df7c02002-03-26 18:01:55 +0000298 TraceValuesAtBBExit(BB, Printf, &valuesStoredInFunction);
Chris Lattner29c14732001-12-14 16:26:05 +0000299 }
300
Chris Lattner79df7c02002-03-26 18:01:55 +0000301 if (traceFunctionEvents)
Chris Lattner29c14732001-12-14 16:26:05 +0000302 for (unsigned i=0; i < exitBlocks.size(); ++i) {
303#if 0
Chris Lattner79df7c02002-03-26 18:01:55 +0000304 TraceValuesAtBBExit(valuesStoredInFunction, exitBlocks[i], module,
305 /*indent*/ 0, /*isFunctionExit*/ true,
306 /*valuesStoredInFunction*/ NULL);
Chris Lattner29c14732001-12-14 16:26:05 +0000307#endif
Chris Lattner79df7c02002-03-26 18:01:55 +0000308 InsertCodeToShowFunctionExit(exitBlocks[i], Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000309 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000310
Chris Lattner8d9e3772001-10-18 05:28:08 +0000311 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000312}