blob: 384516283ca4525eab3297f91ca66eab1394b175 [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"
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 }
Chris Lattner97e52e42002-04-28 21:27:06 +000052
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
54 AU.preservesCFG();
55 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000056 };
57} // end anonymous namespace
58
59
Chris Lattnerf57b8452002-04-27 06:56:12 +000060Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000061 return new InsertTraceCode(false, true);
62}
63
Chris Lattner649f5dd2002-04-14 06:15:24 +000064Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattnerbd0ef772002-02-26 21:46:54 +000065 return new InsertTraceCode(true, true);
66}
67
68
69
70
Chris Lattner29c14732001-12-14 16:26:05 +000071// Add a prototype for printf if it is not already in the program.
72//
Chris Lattnerf4de63f2002-01-21 07:31:50 +000073bool InsertTraceCode::doInitialization(Module *M) {
Chris Lattner29c14732001-12-14 16:26:05 +000074 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner2aac6bf2002-04-04 22:19:18 +000075 const FunctionType *MTy =
76 FunctionType::get(Type::IntTy, vector<const Type*>(1, SBP), true);
Vikram S. Adved8893302001-10-28 21:37:25 +000077
Chris Lattner89851072002-03-29 03:43:24 +000078 PrintfFunc = M->getOrInsertFunction("printf", MTy);
79 return false;
Vikram S. Adved8893302001-10-28 21:37:25 +000080}
81
Chris Lattner29c14732001-12-14 16:26:05 +000082
83static inline GlobalVariable *getStringRef(Module *M, const string &str) {
84 // Create a constant internal string reference...
85 Constant *Init = ConstantArray::get(str);
Vikram S. Adve524185a2002-03-18 03:40:25 +000086
87 // Create the global variable and record it in the module
88 // The GV will be renamed to a unique name if needed.
Chris Lattner29c14732001-12-14 16:26:05 +000089 GlobalVariable *GV = new GlobalVariable(Init->getType(), true, true, Init,
90 "trstr");
Chris Lattnerb81a0bf2001-10-18 20:06:03 +000091 M->getGlobalList().push_back(GV);
92 return GV;
Vikram S. Advedf1892f2001-10-14 23:18:45 +000093}
94
Vikram S. Advebedb00d2001-10-18 13:49:22 +000095
Chris Lattner29c14732001-12-14 16:26:05 +000096//
97// Check if this instruction has any uses outside its basic block,
98// or if it used by either a Call or Return instruction.
99//
100static inline bool LiveAtBBExit(const Instruction* I) {
101 const BasicBlock *BB = I->getParent();
102 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
103 if (const Instruction *UI = dyn_cast<Instruction>(*U))
104 if (UI->getParent() != BB || isa<ReturnInst>(UI))
105 return true;
106
107 return false;
108}
109
110
111static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000112 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattner8d9e3772001-10-18 05:28:08 +0000113 // be traced by default (VoidTy's are already excluded)
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000114 //
115 return (opCode < Instruction::FirstOtherOp &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000116 opCode != Instruction::Alloca &&
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000117 opCode != Instruction::PHINode &&
118 opCode != Instruction::Cast);
119}
120
Chris Lattner29c14732001-12-14 16:26:05 +0000121
122static bool ShouldTraceValue(const Instruction *I) {
123 return
124 I->getType() != Type::VoidTy && LiveAtBBExit(I) &&
125 TraceThisOpCode(I->getOpcode());
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000126}
127
Chris Lattner29c14732001-12-14 16:26:05 +0000128static string getPrintfCodeFor(const Value *V) {
129 if (V == 0) return "";
Chris Lattner649f5dd2002-04-14 06:15:24 +0000130 if (V->getType()->isFloatingPoint())
131 return "%g";
132 else if (V->getType() == Type::LabelTy || isa<PointerType>(V->getType()))
133 return "0x%p";
134 else if (V->getType()->isIntegral() || V->getType() == Type::BoolTy)
Chris Lattner29c14732001-12-14 16:26:05 +0000135 return "%d";
Chris Lattnerb81a0bf2001-10-18 20:06:03 +0000136
Chris Lattner649f5dd2002-04-14 06:15:24 +0000137 assert(0 && "Illegal value to print out...");
138 return "";
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000139}
Vikram S. Advebedb00d2001-10-18 13:49:22 +0000140
141
Chris Lattner29c14732001-12-14 16:26:05 +0000142static void InsertPrintInst(Value *V, BasicBlock *BB, BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000143 string Message, Function *Printf) {
Chris Lattner29c14732001-12-14 16:26:05 +0000144 // Escape Message by replacing all % characters with %% chars.
145 unsigned Offset = 0;
146 while ((Offset = Message.find('%', Offset)) != string::npos) {
Chris Lattner649f5dd2002-04-14 06:15:24 +0000147 Message.replace(Offset, 1, "%%");
Chris Lattner29c14732001-12-14 16:26:05 +0000148 Offset += 2; // Skip over the new %'s
149 }
Chris Lattner8d9e3772001-10-18 05:28:08 +0000150
Chris Lattner29c14732001-12-14 16:26:05 +0000151 Module *Mod = BB->getParent()->getParent();
Chris Lattner8d9e3772001-10-18 05:28:08 +0000152
Chris Lattner44571632001-10-18 06:03:05 +0000153 // Turn the marker string into a global variable...
Chris Lattner29c14732001-12-14 16:26:05 +0000154 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
155
156 // Turn the format string into an sbyte *
157 Instruction *GEP =
158 new GetElementPtrInst(fmtVal,
159 vector<Value*>(2,ConstantUInt::get(Type::UIntTy, 0)),
160 "trstr");
161 BBI = BB->getInstList().insert(BBI, GEP)+1;
Chris Lattner44571632001-10-18 06:03:05 +0000162
163 // Insert the first print instruction to print the string flag:
Chris Lattner29c14732001-12-14 16:26:05 +0000164 vector<Value*> PrintArgs;
165 PrintArgs.push_back(GEP);
166 if (V) PrintArgs.push_back(V);
167 Instruction *I = new CallInst(Printf, PrintArgs, "trace");
Chris Lattner44571632001-10-18 06:03:05 +0000168 BBI = BB->getInstList().insert(BBI, I)+1;
Chris Lattner29c14732001-12-14 16:26:05 +0000169}
170
Chris Lattner44571632001-10-18 06:03:05 +0000171
Chris Lattner29c14732001-12-14 16:26:05 +0000172static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
173 BasicBlock::iterator &BBI,
Chris Lattner79df7c02002-03-26 18:01:55 +0000174 const string &Message, Function *Printf) {
Chris Lattner697954c2002-01-20 22:54:45 +0000175 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000176 if (V) WriteAsOperand(OutStr, V);
177 InsertPrintInst(V, BB, BBI, Message+OutStr.str()+" = ", Printf);
Chris Lattner8d9e3772001-10-18 05:28:08 +0000178}
179
180
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000181// Insert print instructions at the end of the basic block *bb
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000182// for each value in valueVec[] that is live at the end of that basic block,
183// or that is stored to memory in this basic block.
184// If the value is stored to memory, we load it back before printing
Chris Lattner79df7c02002-03-26 18:01:55 +0000185// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner649f5dd2002-04-14 06:15:24 +0000186// for printing at the exit from the function. (Note that in each invocation
187// of the function, this will only get the last value stored for each static
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000188// store instruction).
189// *bb must be the block in which the value is computed;
190// this is not checked here.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000191//
Chris Lattner79df7c02002-03-26 18:01:55 +0000192static void TraceValuesAtBBExit(BasicBlock *BB, Function *Printf,
193 vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adved8893302001-10-28 21:37:25 +0000194 // Get an iterator to point to the insertion location, which is
195 // just before the terminator instruction.
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000196 //
Chris Lattner29c14732001-12-14 16:26:05 +0000197 BasicBlock::iterator InsertPos = BB->end()-1;
198 assert((*InsertPos)->isTerminator());
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000199
Vikram S. Adved8893302001-10-28 21:37:25 +0000200 // If the terminator is a conditional branch, insert the trace code just
201 // before the instruction that computes the branch condition (just to
202 // avoid putting a call between the CC-setting instruction and the branch).
203 // Use laterInstrSet to mark instructions that come after the setCC instr
204 // because those cannot be traced at the location we choose.
205 //
Chris Lattner29c14732001-12-14 16:26:05 +0000206 Instruction *SetCC = 0;
207 if (BranchInst *Branch = dyn_cast<BranchInst>(BB->getTerminator()))
208 if (!Branch->isUnconditional())
209 if (Instruction *I = dyn_cast<Instruction>(Branch->getCondition()))
210 if (I->getParent() == BB) {
211 SetCC = I;
212 while (*InsertPos != SetCC)
213 --InsertPos; // Back up until we can insert before the setcc
214 }
215
216 // Copy all of the instructions into a vector to avoid problems with Setcc
217 const vector<Instruction*> Insts(BB->begin(), InsertPos);
218
Chris Lattner697954c2002-01-20 22:54:45 +0000219 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000220 WriteAsOperand(OutStr, BB, false);
221 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(), Printf);
222
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000223 // Insert a print instruction for each value.
224 //
Chris Lattner29c14732001-12-14 16:26:05 +0000225 for (vector<Instruction*>::const_iterator II = Insts.begin(),
226 IE = Insts.end(); II != IE; ++II) {
227 Instruction *I = *II;
228 if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Chris Lattner79df7c02002-03-26 18:01:55 +0000229 assert(valuesStoredInFunction &&
Chris Lattner649f5dd2002-04-14 06:15:24 +0000230 "Should not be printing a store instruction at function exit");
Chris Lattner29c14732001-12-14 16:26:05 +0000231 LoadInst *LI = new LoadInst(SI->getPointerOperand(), SI->copyIndices(),
232 "reload");
233 InsertPos = BB->getInstList().insert(InsertPos, LI) + 1;
Chris Lattner79df7c02002-03-26 18:01:55 +0000234 valuesStoredInFunction->push_back(LI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000235 }
Chris Lattner29c14732001-12-14 16:26:05 +0000236 if (ShouldTraceValue(I))
237 InsertVerbosePrintInst(I, BB, InsertPos, " ", Printf);
238 }
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000239}
240
Chris Lattner79df7c02002-03-26 18:01:55 +0000241static inline void InsertCodeToShowFunctionEntry(Function *M, Function *Printf){
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000242 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000243 BasicBlock *BB = M->getEntryNode();
244 BasicBlock::iterator BBI = BB->begin();
245
Chris Lattner697954c2002-01-20 22:54:45 +0000246 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000247 WriteAsOperand(OutStr, M, true);
Chris Lattner649f5dd2002-04-14 06:15:24 +0000248 InsertPrintInst(0, BB, BBI, "ENTERING FUNCTION: " + OutStr.str(), Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000249
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000250 // Now print all the incoming arguments
Chris Lattner79df7c02002-03-26 18:01:55 +0000251 const Function::ArgumentListType &argList = M->getArgumentList();
Chris Lattner29c14732001-12-14 16:26:05 +0000252 unsigned ArgNo = 0;
Chris Lattner79df7c02002-03-26 18:01:55 +0000253 for (Function::ArgumentListType::const_iterator
Chris Lattner29c14732001-12-14 16:26:05 +0000254 I = argList.begin(), E = argList.end(); I != E; ++I, ++ArgNo) {
Chris Lattner73e21422002-04-09 19:48:49 +0000255 InsertVerbosePrintInst((Value*)*I, BB, BBI,
Chris Lattner29c14732001-12-14 16:26:05 +0000256 " Arg #" + utostr(ArgNo), Printf);
257 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000258}
259
260
Chris Lattner79df7c02002-03-26 18:01:55 +0000261static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
262 Function *Printf) {
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000263 // Get an iterator to point to the insertion location
Chris Lattner29c14732001-12-14 16:26:05 +0000264 BasicBlock::iterator BBI = BB->end()-1;
265 ReturnInst *Ret = cast<ReturnInst>(*BBI);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000266
Chris Lattner697954c2002-01-20 22:54:45 +0000267 std::ostringstream OutStr;
Chris Lattner29c14732001-12-14 16:26:05 +0000268 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner649f5dd2002-04-14 06:15:24 +0000269 InsertPrintInst(0, BB, BBI, "LEAVING FUNCTION: " + OutStr.str(), Printf);
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000270
Vikram S. Adve2ed5ccd2001-11-15 15:00:16 +0000271 // print the return value, if any
Chris Lattner29c14732001-12-14 16:26:05 +0000272 if (BB->getParent()->getReturnType() != Type::VoidTy)
273 InsertPrintInst(Ret->getReturnValue(), BB, BBI, " Returning: ", Printf);
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000274}
275
276
Chris Lattner79df7c02002-03-26 18:01:55 +0000277bool InsertTraceCode::doit(Function *M, bool traceBasicBlockExits,
278 bool traceFunctionEvents, Function *Printf) {
279 if (!traceBasicBlockExits && !traceFunctionEvents)
Chris Lattner8d9e3772001-10-18 05:28:08 +0000280 return false;
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000281
Chris Lattner79df7c02002-03-26 18:01:55 +0000282 vector<Instruction*> valuesStoredInFunction;
Chris Lattner29c14732001-12-14 16:26:05 +0000283 vector<BasicBlock*> exitBlocks;
284
Chris Lattner79df7c02002-03-26 18:01:55 +0000285 if (traceFunctionEvents)
286 InsertCodeToShowFunctionEntry(M, Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000287
Chris Lattner79df7c02002-03-26 18:01:55 +0000288 for (Function::iterator BI = M->begin(); BI != M->end(); ++BI) {
Chris Lattner29c14732001-12-14 16:26:05 +0000289 BasicBlock *BB = *BI;
290 if (isa<ReturnInst>(BB->getTerminator()))
291 exitBlocks.push_back(BB); // record this as an exit block
292
293 if (traceBasicBlockExits)
Chris Lattner79df7c02002-03-26 18:01:55 +0000294 TraceValuesAtBBExit(BB, Printf, &valuesStoredInFunction);
Chris Lattner29c14732001-12-14 16:26:05 +0000295 }
296
Chris Lattner79df7c02002-03-26 18:01:55 +0000297 if (traceFunctionEvents)
Chris Lattner29c14732001-12-14 16:26:05 +0000298 for (unsigned i=0; i < exitBlocks.size(); ++i) {
299#if 0
Chris Lattner79df7c02002-03-26 18:01:55 +0000300 TraceValuesAtBBExit(valuesStoredInFunction, exitBlocks[i], module,
301 /*indent*/ 0, /*isFunctionExit*/ true,
302 /*valuesStoredInFunction*/ NULL);
Chris Lattner29c14732001-12-14 16:26:05 +0000303#endif
Chris Lattner79df7c02002-03-26 18:01:55 +0000304 InsertCodeToShowFunctionExit(exitBlocks[i], Printf);
Chris Lattner29c14732001-12-14 16:26:05 +0000305 }
Vikram S. Adve631b9a32001-10-18 18:16:11 +0000306
Chris Lattner8d9e3772001-10-18 05:28:08 +0000307 return true;
Vikram S. Advedf1892f2001-10-14 23:18:45 +0000308}