blob: 9bbf7e580eac39e8bb046477768d52c3b9a96aaa [file] [log] [blame]
Chris Lattnerf1197b02001-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. Advea200a6c2001-10-14 23:18:45 +00007
8#include "llvm/Transforms/Instrumentation/TraceValues.h"
9#include "llvm/GlobalVariable.h"
Chris Lattner3462ae32001-12-03 22:26:30 +000010#include "llvm/ConstantVals.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000011#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000012#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000013#include "llvm/iTerminators.h"
14#include "llvm/iOther.h"
Chris Lattner57698e22002-03-26 18:01:55 +000015#include "llvm/Function.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000016#include "llvm/Module.h"
17#include "llvm/SymbolTable.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000018#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000019#include "llvm/Assembly/Writer.h"
Chris Lattner5de22042001-11-27 00:03:19 +000020#include "Support/StringExtras.h"
Chris Lattnere2c61262001-10-18 20:06:03 +000021#include <sstream>
Chris Lattner7f74a562002-01-20 22:54:45 +000022using std::vector;
23using std::string;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000024
Chris Lattner04805fa2002-02-26 21:46:54 +000025namespace {
26 class InsertTraceCode : public MethodPass {
Chris Lattner57698e22002-03-26 18:01:55 +000027 bool TraceBasicBlockExits, TraceFunctionExits;
28 Function *PrintfFunc;
Chris Lattner04805fa2002-02-26 21:46:54 +000029 public:
Chris Lattner57698e22002-03-26 18:01:55 +000030 InsertTraceCode(bool traceBasicBlockExits, bool traceFunctionExits)
Chris Lattner04805fa2002-02-26 21:46:54 +000031 : TraceBasicBlockExits(traceBasicBlockExits),
Chris Lattner57698e22002-03-26 18:01:55 +000032 TraceFunctionExits(traceFunctionExits) {}
Chris Lattner04805fa2002-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 //
41 // Inserts tracing code for all live values at basic block and/or method
Chris Lattner57698e22002-03-26 18:01:55 +000042 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000043 //
Chris Lattner57698e22002-03-26 18:01:55 +000044 static bool doit(Function *M, bool traceBasicBlockExits,
45 bool traceFunctionExits, Function *Printf);
Chris Lattner04805fa2002-02-26 21:46:54 +000046
47 // runOnMethod - This method does the work. Always successful.
48 //
Chris Lattner57698e22002-03-26 18:01:55 +000049 bool runOnMethod(Function *F) {
50 return doit(F, TraceBasicBlockExits, TraceFunctionExits, PrintfFunc);
Chris Lattner04805fa2002-02-26 21:46:54 +000051 }
52 };
53} // end anonymous namespace
54
55
56Pass *createTraceValuesPassForMethod() { // Just trace methods
57 return new InsertTraceCode(false, true);
58}
59
60Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and methods
61 return new InsertTraceCode(true, true);
62}
63
64
65
66
Chris Lattnerf1197b02001-12-14 16:26:05 +000067// Add a prototype for printf if it is not already in the program.
68//
Chris Lattner0686e432002-01-21 07:31:50 +000069bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +000070 SymbolTable *ST = M->getSymbolTable();
71 const Type *SBP = PointerType::get(Type::SByteTy);
72 const MethodType *MTy =
73 MethodType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000074
Chris Lattner57698e22002-03-26 18:01:55 +000075 if (Value *Func = ST->lookup(PointerType::get(MTy), "printf")) {
76 PrintfFunc = cast<Function>(Func);
Chris Lattnerf1197b02001-12-14 16:26:05 +000077 return false;
78 }
79
80 // Create a new method and add it to the module
Chris Lattner57698e22002-03-26 18:01:55 +000081 PrintfFunc = new Function(MTy, false, "printf");
82 M->getFunctionList().push_back(PrintfFunc);
Chris Lattnerf1197b02001-12-14 16:26:05 +000083 return true;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000084}
85
Chris Lattnerf1197b02001-12-14 16:26:05 +000086
87static inline GlobalVariable *getStringRef(Module *M, const string &str) {
88 // Create a constant internal string reference...
89 Constant *Init = ConstantArray::get(str);
Vikram S. Adve9f129ff2002-03-18 03:40:25 +000090
91 // Create the global variable and record it in the module
92 // The GV will be renamed to a unique name if needed.
Chris Lattnerf1197b02001-12-14 16:26:05 +000093 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
94 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +000095 M->getGlobalList().push_back(GV);
96 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +000097}
98
Vikram S. Adve7ac553a2001-10-18 13:49:22 +000099
Chris Lattnerf1197b02001-12-14 16:26:05 +0000100//
101// Check if this instruction has any uses outside its basic block,
102// or if it used by either a Call or Return instruction.
103//
104static inline bool LiveAtBBExit(const Instruction* I) {
105 const BasicBlock *BB = I->getParent();
106 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
107 if (const Instruction *UI = dyn_cast<Instruction>(*U))
108 if (UI->getParent() != BB || isa<ReturnInst>(UI))
109 return true;
110
111 return false;
112}
113
114
115static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000116 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000117 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000118 //
119 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000120 opCode != Instruction::Alloca &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000121 opCode != Instruction::PHINode &&
122 opCode != Instruction::Cast);
123}
124
Chris Lattnerf1197b02001-12-14 16:26:05 +0000125
126static bool ShouldTraceValue(const Instruction *I) {
127 return
128 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
129 TraceThisOpCode(I->getOpcode());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000130}
131
Chris Lattnerf1197b02001-12-14 16:26:05 +0000132static string getPrintfCodeFor(const Value *V) {
133 if (V == 0) return "";
134 switch (V->getType()->getPrimitiveID()) {
135 case Type::BoolTyID:
136 case Type::UByteTyID: case Type::UShortTyID:
137 case Type::UIntTyID: case Type::ULongTyID:
138 case Type::SByteTyID: case Type::ShortTyID:
139 case Type::IntTyID: case Type::LongTyID:
140 return "%d";
Chris Lattnere2c61262001-10-18 20:06:03 +0000141
Chris Lattnerf1197b02001-12-14 16:26:05 +0000142 case Type::FloatTyID: case Type::DoubleTyID:
143 return "%g";
Chris Lattnere2c61262001-10-18 20:06:03 +0000144
Chris Lattnerf1197b02001-12-14 16:26:05 +0000145 case Type::LabelTyID: case Type::PointerTyID:
146 return "%p";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000147
Chris Lattnerf1197b02001-12-14 16:26:05 +0000148 default:
149 assert(0 && "Illegal value to print out...");
150 return "";
151 }
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000152}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000153
154
Chris Lattnerf1197b02001-12-14 16:26:05 +0000155static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
Chris Lattner57698e22002-03-26 18:01:55 +0000156 string Message, Function *Printf) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000157 // Escape Message by replacing all % characters with %% chars.
158 unsigned Offset = 0;
159 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000160 Message.replace(Offset, 2, "%%");
161 Offset += 2; // Skip over the new %'s
162 }
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000163
Chris Lattnerf1197b02001-12-14 16:26:05 +0000164 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000165
Chris Lattner5309e102001-10-18 06:03:05 +0000166 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000167 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
168
169 // Turn the format string into an sbyte *
170 Instruction *GEP =
171 new GetElementPtrInst(fmtVal,
172 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
173 "trstr");
174 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner5309e102001-10-18 06:03:05 +0000175
176 // Insert the first print instruction to print the string flag:
Chris Lattnerf1197b02001-12-14 16:26:05 +0000177 vector<Value*> PrintArgs;
178 PrintArgs.push_back(GEP);
179 if (V) PrintArgs.push_back(V);
180 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner5309e102001-10-18 06:03:05 +0000181 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000182}
183
Chris Lattner5309e102001-10-18 06:03:05 +0000184
Chris Lattnerf1197b02001-12-14 16:26:05 +0000185static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
186 BasicBlock::iterator &BBI,
Chris Lattner57698e22002-03-26 18:01:55 +0000187 const string &Message, Function *Printf) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000188 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000189 if (V) WriteAsOperand(OutStr, V);
190 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000191}
192
193
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000194// Insert print instructions at the end of the basic block *bb
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000195// for each value in valueVec[] that is live at the end of that basic block,
196// or that is stored to memory in this basic block.
197// If the value is stored to memory, we load it back before printing
Chris Lattner57698e22002-03-26 18:01:55 +0000198// We also return all such loaded values in the vector valuesStoredInFunction
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000199// for printing at the exit from the method. (Note that in each invocation
200// of the method, this will only get the last value stored for each static
201// store instruction).
202// *bb must be the block in which the value is computed;
203// this is not checked here.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000204//
Chris Lattner57698e22002-03-26 18:01:55 +0000205static void TraceValuesAtBBExit(BasicBlock *BB, Function *Printf,
206 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000207 // Get an iterator to point to the insertion location, which is
208 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000209 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000210 BasicBlock::iterator InsertPos = BB->end()-1;
211 assert((*InsertPos)->isTerminator());
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000212
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000213 // If the terminator is a conditional branch, insert the trace code just
214 // before the instruction that computes the branch condition (just to
215 // avoid putting a call between the CC-setting instruction and the branch).
216 // Use laterInstrSet to mark instructions that come after the setCC instr
217 // because those cannot be traced at the location we choose.
218 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000219 Instruction *SetCC = 0;
220 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
221 if (!Branch->isUnconditional())
222 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
223 if (I->getParent() == BB) {
224 SetCC = I;
225 while (*InsertPos != SetCC)
226 --InsertPos; // Back up until we can insert before the setcc
227 }
228
229 // Copy all of the instructions into a vector to avoid problems with Setcc
230 const vector<Instruction*> Insts(BB->begin(), InsertPos);
231
Chris Lattner7f74a562002-01-20 22:54:45 +0000232 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000233 WriteAsOperand(OutStr, BB, false);
234 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
235
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000236 // Insert a print instruction for each value.
237 //
Chris Lattnerf1197b02001-12-14 16:26:05 +0000238 for (vector<Instruction*>::const_iterator II = Insts.begin(),
239 IE = Insts.end(); II != IE; ++II) {
240 Instruction *I = *II;
241 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner57698e22002-03-26 18:01:55 +0000242 assert(valuesStoredInFunction &&
Chris Lattnerf1197b02001-12-14 16:26:05 +0000243 "Should not be printing a store instruction at method exit");
244 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
245 "reload");
246 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner57698e22002-03-26 18:01:55 +0000247 valuesStoredInFunction->push_back(LI);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000248 }
Chris Lattnerf1197b02001-12-14 16:26:05 +0000249 if (ShouldTraceValue(I))
250 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
251 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000252}
253
Chris Lattner57698e22002-03-26 18:01:55 +0000254static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000255 // Get an iterator to point to the insertion location
Chris Lattnerf1197b02001-12-14 16:26:05 +0000256 BasicBlock *BB = M->getEntryNode();
257 BasicBlock::iterator BBI = BB->begin();
258
Chris Lattner7f74a562002-01-20 22:54:45 +0000259 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000260 WriteAsOperand(OutStr, M, true);
261 InsertPrintInst(0, BB, BBI, "ENTERING METHOD: " + OutStr.str(), Printf);
262
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000263 // Now print all the incoming arguments
Chris Lattner57698e22002-03-26 18:01:55 +0000264 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000265 unsigned ArgNo = 0;
Chris Lattner57698e22002-03-26 18:01:55 +0000266 for (Function::ArgumentListType::const_iterator
Chris Lattnerf1197b02001-12-14 16:26:05 +0000267 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
268 InsertVerbosePrintInst(*I, BB, BBI,
269 " Arg #" + utostr(ArgNo), Printf);
270 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000271}
272
273
Chris Lattner57698e22002-03-26 18:01:55 +0000274static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
275 Function *Printf) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000276 // Get an iterator to point to the insertion location
Chris Lattnerf1197b02001-12-14 16:26:05 +0000277 BasicBlock::iterator BBI = BB->end()-1;
278 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000279
Chris Lattner7f74a562002-01-20 22:54:45 +0000280 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000281 WriteAsOperand(OutStr, BB->getParent(), true);
282 InsertPrintInst(0, BB, BBI, "LEAVING METHOD: " + OutStr.str(), Printf);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000283
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000284 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000285 if (BB->getParent()->getReturnType() != Type::VoidTy)
286 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000287}
288
289
Chris Lattner57698e22002-03-26 18:01:55 +0000290bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
291 bool traceFunctionEvents, Function *Printf) {
292 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000293 return false;
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000294
Chris Lattner57698e22002-03-26 18:01:55 +0000295 vector<Instruction*> valuesStoredInFunction;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000296 vector<BasicBlock*> exitBlocks;
297
Chris Lattner57698e22002-03-26 18:01:55 +0000298 if (traceFunctionEvents)
299 InsertCodeToShowFunctionEntry(M, Printf);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000300
Chris Lattner57698e22002-03-26 18:01:55 +0000301 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000302 BasicBlock *BB = *BI;
303 if (isa<ReturnInst>(BB->getTerminator()))
304 exitBlocks.push_back(BB); // record this as an exit block
305
306 if (traceBasicBlockExits)
Chris Lattner57698e22002-03-26 18:01:55 +0000307 TraceValuesAtBBExit(BB, Printf, &valuesStoredInFunction);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000308 }
309
Chris Lattner57698e22002-03-26 18:01:55 +0000310 if (traceFunctionEvents)
Chris Lattnerf1197b02001-12-14 16:26:05 +0000311 for (unsigned i=0; i < exitBlocks.size(); ++i) {
312#if 0
Chris Lattner57698e22002-03-26 18:01:55 +0000313 TraceValuesAtBBExit(valuesStoredInFunction, exitBlocks[i], module,
314 /*indent*/ 0, /*isFunctionExit*/ true,
315 /*valuesStoredInFunction*/ NULL);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000316#endif
Chris Lattner57698e22002-03-26 18:01:55 +0000317 InsertCodeToShowFunctionExit(exitBlocks[i], Printf);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000318 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000319
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000320 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000321}