blob: c802f736046c6e5144d394acecda893e85019c82 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===- TraceValues.cpp - Value Tracing for debugging ----------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerf1197b02001-12-14 16:26:05 +00009//
Chris Lattner7e358902002-04-14 06:15:24 +000010// Support for inserting LLVM code to print values at basic block and function
Chris Lattnerf1197b02001-12-14 16:26:05 +000011// exits.
12//
13//===----------------------------------------------------------------------===//
Vikram S. Advea200a6c2001-10-14 23:18:45 +000014
Chris Lattner57fd3072003-01-14 22:39:29 +000015#include "llvm/Transforms/Instrumentation.h"
Chris Lattnerca142372002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000017#include "llvm/DerivedTypes.h"
Vikram S. Advea0db1c92001-10-18 18:16:11 +000018#include "llvm/iMemory.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000019#include "llvm/iTerminators.h"
20#include "llvm/iOther.h"
Vikram S. Advea200a6c2001-10-14 23:18:45 +000021#include "llvm/Module.h"
Chris Lattner04805fa2002-02-26 21:46:54 +000022#include "llvm/Pass.h"
Chris Lattnera0a8b5b2001-10-18 05:28:08 +000023#include "llvm/Assembly/Writer.h"
Vikram S. Adve47f37c32002-05-19 15:39:02 +000024#include "Support/CommandLine.h"
Chris Lattner5de22042001-11-27 00:03:19 +000025#include "Support/StringExtras.h"
Chris Lattnerace7b8d2002-05-20 21:43:59 +000026#include <algorithm>
Chris Lattnere2c61262001-10-18 20:06:03 +000027#include <sstream>
Vikram S. Adve96f6ac92001-10-28 21:37:25 +000028
Brian Gaeke960707c2003-11-11 22:41:34 +000029namespace llvm {
30
Chris Lattnerf5cad152002-07-22 02:10:13 +000031static cl::opt<bool>
32DisablePtrHashing("tracedisablehashdisable", cl::Hidden,
Chris Lattner01587d42003-04-13 03:50:14 +000033 cl::desc("Disable pointer hashing in the -trace or -tracem "
34 "passes"));
Vikram S. Adve47f37c32002-05-19 15:39:02 +000035
Chris Lattner23398932003-10-27 21:44:09 +000036static cl::list<std::string>
Chris Lattner01587d42003-04-13 03:50:14 +000037TraceFuncNames("tracefunc", cl::desc("Only trace specific functions in the "
38 "-trace or -tracem passes"),
Chris Lattnerb9636a72003-01-13 00:52:14 +000039 cl::value_desc("function"), cl::Hidden);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000040
Chris Lattner6d216fd2002-07-23 18:04:15 +000041static void TraceValuesAtBBExit(BasicBlock *BB,
42 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner23398932003-10-27 21:44:09 +000043 std::vector<Instruction*> *valuesStoredInFunction);
Vikram S. Adve47f37c32002-05-19 15:39:02 +000044
45// We trace a particular function if no functions to trace were specified
46// or if the function is in the specified list.
47//
Chris Lattner6d216fd2002-07-23 18:04:15 +000048inline static bool
Chris Lattnerb9636a72003-01-13 00:52:14 +000049TraceThisFunction(Function &F)
Vikram S. Adve47f37c32002-05-19 15:39:02 +000050{
Chris Lattnerb9636a72003-01-13 00:52:14 +000051 if (TraceFuncNames.empty()) return true;
Chris Lattnerace7b8d2002-05-20 21:43:59 +000052
Chris Lattnerb9636a72003-01-13 00:52:14 +000053 return std::find(TraceFuncNames.begin(), TraceFuncNames.end(), F.getName())
54 != TraceFuncNames.end();
Vikram S. Adve47f37c32002-05-19 15:39:02 +000055}
56
57
Chris Lattner04805fa2002-02-26 21:46:54 +000058namespace {
Chris Lattnerace7b8d2002-05-20 21:43:59 +000059 struct ExternalFuncs {
Vikram S. Adve47f37c32002-05-19 15:39:02 +000060 Function *PrintfFunc, *HashPtrFunc, *ReleasePtrFunc;
61 Function *RecordPtrFunc, *PushOnEntryFunc, *ReleaseOnReturnFunc;
Chris Lattner113f4f42002-06-25 16:13:24 +000062 void doInitialization(Module &M); // Add prototypes for external functions
Vikram S. Adve47f37c32002-05-19 15:39:02 +000063 };
64
Chris Lattnerc8e66542002-04-27 06:56:12 +000065 class InsertTraceCode : public FunctionPass {
Chris Lattner6d216fd2002-07-23 18:04:15 +000066 protected:
Vikram S. Adve47f37c32002-05-19 15:39:02 +000067 ExternalFuncs externalFuncs;
Chris Lattner04805fa2002-02-26 21:46:54 +000068 public:
Chris Lattner04805fa2002-02-26 21:46:54 +000069
Vikram S. Adve47f37c32002-05-19 15:39:02 +000070 // Add a prototype for runtime functions not already in the program.
Chris Lattner04805fa2002-02-26 21:46:54 +000071 //
Chris Lattner113f4f42002-06-25 16:13:24 +000072 bool doInitialization(Module &M);
Chris Lattner04805fa2002-02-26 21:46:54 +000073
74 //--------------------------------------------------------------------------
75 // Function InsertCodeToTraceValues
76 //
Chris Lattner7e358902002-04-14 06:15:24 +000077 // Inserts tracing code for all live values at basic block and/or function
Chris Lattner57698e22002-03-26 18:01:55 +000078 // exits as specified by `traceBasicBlockExits' and `traceFunctionExits'.
Chris Lattner04805fa2002-02-26 21:46:54 +000079 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000080 bool doit(Function *M);
81
Chris Lattner23398932003-10-27 21:44:09 +000082 virtual void handleBasicBlock(BasicBlock *BB,
83 std::vector<Instruction*> &VI) = 0;
Vikram S. Adve47f37c32002-05-19 15:39:02 +000084
Chris Lattner7e358902002-04-14 06:15:24 +000085 // runOnFunction - This method does the work.
Chris Lattner04805fa2002-02-26 21:46:54 +000086 //
Chris Lattner6d216fd2002-07-23 18:04:15 +000087 bool runOnFunction(Function &F);
Chris Lattnerf12cc842002-04-28 21:27:06 +000088
89 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000090 AU.setPreservesCFG();
Chris Lattnerf12cc842002-04-28 21:27:06 +000091 }
Chris Lattner04805fa2002-02-26 21:46:54 +000092 };
Chris Lattner6d216fd2002-07-23 18:04:15 +000093
94 struct FunctionTracer : public InsertTraceCode {
95 // Ignore basic blocks here...
Chris Lattner23398932003-10-27 21:44:09 +000096 virtual void handleBasicBlock(BasicBlock *BB,
97 std::vector<Instruction*> &VI) {}
Chris Lattner6d216fd2002-07-23 18:04:15 +000098 };
99
100 struct BasicBlockTracer : public InsertTraceCode {
101 // Trace basic blocks here...
Chris Lattner23398932003-10-27 21:44:09 +0000102 virtual void handleBasicBlock(BasicBlock *BB,
103 std::vector<Instruction*> &VI) {
Chris Lattner6d216fd2002-07-23 18:04:15 +0000104 TraceValuesAtBBExit(BB, externalFuncs.PrintfFunc,
105 externalFuncs.HashPtrFunc, &VI);
106 }
107 };
108
109 // Register the passes...
Chris Lattnerc8b70922002-07-26 21:12:46 +0000110 RegisterOpt<FunctionTracer> X("tracem","Insert Function trace code only");
111 RegisterOpt<BasicBlockTracer> Y("trace","Insert BB and Function trace code");
Chris Lattner04805fa2002-02-26 21:46:54 +0000112} // end anonymous namespace
113
114
Chris Lattnerc8e66542002-04-27 06:56:12 +0000115Pass *createTraceValuesPassForFunction() { // Just trace functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000116 return new FunctionTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000117}
118
Chris Lattner7e358902002-04-14 06:15:24 +0000119Pass *createTraceValuesPassForBasicBlocks() { // Trace BB's and functions
Chris Lattner6d216fd2002-07-23 18:04:15 +0000120 return new BasicBlockTracer();
Chris Lattner04805fa2002-02-26 21:46:54 +0000121}
122
Chris Lattner6d216fd2002-07-23 18:04:15 +0000123
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000124// Add a prototype for external functions used by the tracing code.
Chris Lattnerf1197b02001-12-14 16:26:05 +0000125//
Chris Lattner113f4f42002-06-25 16:13:24 +0000126void ExternalFuncs::doInitialization(Module &M) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000127 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattnere2f2f542002-04-04 22:19:18 +0000128 const FunctionType *MTy =
Chris Lattner23398932003-10-27 21:44:09 +0000129 FunctionType::get(Type::IntTy, std::vector<const Type*>(1, SBP), true);
Chris Lattner113f4f42002-06-25 16:13:24 +0000130 PrintfFunc = M.getOrInsertFunction("printf", MTy);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000131
132 // uint (sbyte*)
Chris Lattner8e6ac1b2003-08-31 00:20:36 +0000133 HashPtrFunc = M.getOrInsertFunction("HashPointerToSeqNum", Type::UIntTy, SBP,
134 0);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000135
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000136 // void (sbyte*)
Chris Lattner8e6ac1b2003-08-31 00:20:36 +0000137 ReleasePtrFunc = M.getOrInsertFunction("ReleasePointerSeqNum",
138 Type::VoidTy, SBP, 0);
139 RecordPtrFunc = M.getOrInsertFunction("RecordPointer",
140 Type::VoidTy, SBP, 0);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000141
Chris Lattner8e6ac1b2003-08-31 00:20:36 +0000142 PushOnEntryFunc = M.getOrInsertFunction("PushPointerSet", Type::VoidTy, 0);
Chris Lattner113f4f42002-06-25 16:13:24 +0000143 ReleaseOnReturnFunc = M.getOrInsertFunction("ReleasePointersPopSet",
Chris Lattner8e6ac1b2003-08-31 00:20:36 +0000144 Type::VoidTy, 0);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000145}
146
147
148// Add a prototype for external functions used by the tracing code.
149//
Chris Lattner113f4f42002-06-25 16:13:24 +0000150bool InsertTraceCode::doInitialization(Module &M) {
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000151 externalFuncs.doInitialization(M);
Chris Lattnerc46dcca2002-03-29 03:43:24 +0000152 return false;
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000153}
154
Chris Lattnerf1197b02001-12-14 16:26:05 +0000155
Chris Lattner23398932003-10-27 21:44:09 +0000156static inline GlobalVariable *getStringRef(Module *M, const std::string &str) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000157 // Create a constant internal string reference...
158 Constant *Init = ConstantArray::get(str);
Vikram S. Adve9f129ff2002-03-18 03:40:25 +0000159
160 // Create the global variable and record it in the module
161 // The GV will be renamed to a unique name if needed.
Chris Lattner379a8d22003-04-16 20:28:45 +0000162 GlobalVariable *GV = new GlobalVariable(Init->getType(), true,
163 GlobalValue::InternalLinkage, Init,
Chris Lattnerf1197b02001-12-14 16:26:05 +0000164 "trstr");
Chris Lattnere2c61262001-10-18 20:06:03 +0000165 M->getGlobalList().push_back(GV);
166 return GV;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000167}
168
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000169
Chris Lattnerf1197b02001-12-14 16:26:05 +0000170//
171// Check if this instruction has any uses outside its basic block,
Vikram S. Adveac83df72003-07-11 21:57:43 +0000172// or if it used by either a Call or Return instruction (ditto).
173// (Values stored to memory within this BB are live at end of BB but are
174// traced at the store instruction, not where they are computed.)
Chris Lattnerf1197b02001-12-14 16:26:05 +0000175//
176static inline bool LiveAtBBExit(const Instruction* I) {
177 const BasicBlock *BB = I->getParent();
178 for (Value::use_const_iterator U = I->use_begin(); U != I->use_end(); ++U)
179 if (const Instruction *UI = dyn_cast<Instruction>(*U))
180 if (UI->getParent() != BB || isa<ReturnInst>(UI))
181 return true;
182
183 return false;
184}
185
186
187static inline bool TraceThisOpCode(unsigned opCode) {
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000188 // Explicitly test for opCodes *not* to trace so that any new opcodes will
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000189 // be traced by default (VoidTy's are already excluded)
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000190 //
Chris Lattner69ce8672002-10-13 19:39:16 +0000191 return (opCode < Instruction::OtherOpsBegin &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000192 opCode != Instruction::Alloca &&
Chris Lattnerb94550e2003-10-19 21:34:28 +0000193 opCode != Instruction::PHI &&
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000194 opCode != Instruction::Cast);
195}
196
Chris Lattnerf1197b02001-12-14 16:26:05 +0000197
Vikram S. Adveac83df72003-07-11 21:57:43 +0000198// Trace a value computed by an instruction if it is non-void, it is computed
199// by a real computation, not just a copy (see TraceThisOpCode), and
200// -- it is a load instruction: we want to check values read from memory
201// -- or it is live at exit from the basic block (i.e., ignore local temps)
202//
Chris Lattnerf1197b02001-12-14 16:26:05 +0000203static bool ShouldTraceValue(const Instruction *I) {
204 return
Vikram S. Adveac83df72003-07-11 21:57:43 +0000205 I->getType() != Type::VoidTy &&
206 TraceThisOpCode(I->getOpcode()) &&
207 (isa<LoadInst>(I) || LiveAtBBExit(I));
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000208}
209
Chris Lattner23398932003-10-27 21:44:09 +0000210static std::string getPrintfCodeFor(const Value *V) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000211 if (V == 0) return "";
Chris Lattner7e358902002-04-14 06:15:24 +0000212 if (V->getType()->isFloatingPoint())
213 return "%g";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000214 else if (V->getType() == Type::LabelTy)
Chris Lattner7e358902002-04-14 06:15:24 +0000215 return "0x%p";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000216 else if (isa<PointerType>(V->getType()))
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000217 return DisablePtrHashing ? "0x%p" : "%d";
Chris Lattnerbc6bdc22002-09-03 01:07:35 +0000218 else if (V->getType()->isIntegral())
Chris Lattnerf1197b02001-12-14 16:26:05 +0000219 return "%d";
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000220
Chris Lattner7e358902002-04-14 06:15:24 +0000221 assert(0 && "Illegal value to print out...");
222 return "";
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000223}
Vikram S. Adve7ac553a2001-10-18 13:49:22 +0000224
225
Chris Lattner28a8d242002-09-10 17:04:02 +0000226static void InsertPrintInst(Value *V, BasicBlock *BB, Instruction *InsertBefore,
Chris Lattner23398932003-10-27 21:44:09 +0000227 std::string Message,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000228 Function *Printf, Function* HashPtrToSeqNum) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000229 // Escape Message by replacing all % characters with %% chars.
Chris Lattner23398932003-10-27 21:44:09 +0000230 std::string Tmp;
Chris Lattner26783a52002-10-17 16:22:08 +0000231 std::swap(Tmp, Message);
Chris Lattner23398932003-10-27 21:44:09 +0000232 std::string::iterator I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattner26783a52002-10-17 16:22:08 +0000233 while (I != Tmp.end()) {
234 Message.append(Tmp.begin(), I);
235 Message += "%%";
236 ++I; // Make sure to erase the % as well...
237 Tmp.erase(Tmp.begin(), I);
238 I = std::find(Tmp.begin(), Tmp.end(), '%');
Chris Lattnerf1197b02001-12-14 16:26:05 +0000239 }
Chris Lattnerb9636a72003-01-13 00:52:14 +0000240 Message += Tmp;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000241 Module *Mod = BB->getParent()->getParent();
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000242
Chris Lattner5309e102001-10-18 06:03:05 +0000243 // Turn the marker string into a global variable...
Chris Lattnerf1197b02001-12-14 16:26:05 +0000244 GlobalVariable *fmtVal = getStringRef(Mod, Message+getPrintfCodeFor(V)+"\n");
245
246 // Turn the format string into an sbyte *
Chris Lattner3cc30182003-06-05 04:48:18 +0000247 Constant *GEP =ConstantExpr::getGetElementPtr(ConstantPointerRef::get(fmtVal),
Chris Lattner23398932003-10-27 21:44:09 +0000248 std::vector<Constant*>(2,Constant::getNullValue(Type::LongTy)));
Chris Lattner5309e102001-10-18 06:03:05 +0000249
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000250 // Insert a call to the hash function if this is a pointer value
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000251 if (V && isa<PointerType>(V->getType()) && !DisablePtrHashing) {
252 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000253 if (V->getType() != SBP) // Cast pointer to be sbyte*
254 V = new CastInst(V, SBP, "Hash_cast", InsertBefore);
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000255
Chris Lattner23398932003-10-27 21:44:09 +0000256 std::vector<Value*> HashArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000257 V = new CallInst(HashPtrToSeqNum, HashArgs, "ptrSeqNum", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000258 }
259
Chris Lattner5309e102001-10-18 06:03:05 +0000260 // Insert the first print instruction to print the string flag:
Chris Lattner23398932003-10-27 21:44:09 +0000261 std::vector<Value*> PrintArgs;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000262 PrintArgs.push_back(GEP);
263 if (V) PrintArgs.push_back(V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000264 new CallInst(Printf, PrintArgs, "trace", InsertBefore);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000265}
266
Chris Lattner5309e102001-10-18 06:03:05 +0000267
Chris Lattnerf1197b02001-12-14 16:26:05 +0000268static void InsertVerbosePrintInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000269 Instruction *InsertBefore,
Chris Lattner23398932003-10-27 21:44:09 +0000270 const std::string &Message, Function *Printf,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000271 Function* HashPtrToSeqNum) {
Chris Lattner7f74a562002-01-20 22:54:45 +0000272 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000273 if (V) WriteAsOperand(OutStr, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000274 InsertPrintInst(V, BB, InsertBefore, Message+OutStr.str()+" = ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000275 Printf, HashPtrToSeqNum);
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000276}
277
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000278static void
279InsertReleaseInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000280 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000281 Function* ReleasePtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000282
283 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000284 if (V->getType() != SBP) // Cast pointer to be sbyte*
285 V = new CastInst(V, SBP, "RPSN_cast", InsertBefore);
286
Chris Lattner23398932003-10-27 21:44:09 +0000287 std::vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000288 new CallInst(ReleasePtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000289}
290
291static void
292InsertRecordInst(Value *V, BasicBlock *BB,
Chris Lattner28a8d242002-09-10 17:04:02 +0000293 Instruction *InsertBefore,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000294 Function* RecordPtrFunc) {
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000295 const Type *SBP = PointerType::get(Type::SByteTy);
Chris Lattner28a8d242002-09-10 17:04:02 +0000296 if (V->getType() != SBP) // Cast pointer to be sbyte*
297 V = new CastInst(V, SBP, "RP_cast", InsertBefore);
298
Chris Lattner23398932003-10-27 21:44:09 +0000299 std::vector<Value*> releaseArgs(1, V);
Chris Lattner28a8d242002-09-10 17:04:02 +0000300 new CallInst(RecordPtrFunc, releaseArgs, "", InsertBefore);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000301}
302
303// Look for alloca and free instructions. These are the ptrs to release.
304// Release the free'd pointers immediately. Record the alloca'd pointers
305// to be released on return from the current function.
306//
307static void
308ReleasePtrSeqNumbers(BasicBlock *BB,
309 ExternalFuncs& externalFuncs) {
310
Chris Lattner28a8d242002-09-10 17:04:02 +0000311 for (BasicBlock::iterator II=BB->begin(), IE = BB->end(); II != IE; ++II)
Chris Lattner889f6202003-04-23 16:37:45 +0000312 if (FreeInst *FI = dyn_cast<FreeInst>(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000313 InsertReleaseInst(FI->getOperand(0), BB, FI,externalFuncs.ReleasePtrFunc);
Chris Lattner889f6202003-04-23 16:37:45 +0000314 else if (AllocaInst *AI = dyn_cast<AllocaInst>(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000315 InsertRecordInst(AI, BB, AI->getNext(), externalFuncs.RecordPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000316}
317
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000318
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000319// Insert print instructions at the end of basic block BB for each value
320// computed in BB that is live at the end of BB,
321// or that is stored to memory in BB.
322// If the value is stored to memory, we load it back before printing it
Chris Lattner57698e22002-03-26 18:01:55 +0000323// We also return all such loaded values in the vector valuesStoredInFunction
Chris Lattner7e358902002-04-14 06:15:24 +0000324// for printing at the exit from the function. (Note that in each invocation
325// of the function, this will only get the last value stored for each static
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000326// store instruction).
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000327//
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000328static void TraceValuesAtBBExit(BasicBlock *BB,
329 Function *Printf, Function* HashPtrToSeqNum,
Chris Lattner23398932003-10-27 21:44:09 +0000330 std::vector<Instruction*> *valuesStoredInFunction) {
Vikram S. Adve96f6ac92001-10-28 21:37:25 +0000331 // Get an iterator to point to the insertion location, which is
332 // just before the terminator instruction.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000333 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000334 TerminatorInst *InsertPos = BB->getTerminator();
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000335
Chris Lattner7f74a562002-01-20 22:54:45 +0000336 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000337 WriteAsOperand(OutStr, BB, false);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000338 InsertPrintInst(0, BB, InsertPos, "LEAVING BB:" + OutStr.str(),
339 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000340
Vikram S. Adve4b581be2002-07-08 23:37:07 +0000341 // Insert a print instruction for each instruction preceding InsertPos.
342 // The print instructions must go before InsertPos, so we use the
343 // instruction *preceding* InsertPos to check when to terminate the loop.
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000344 //
Chris Lattner28a8d242002-09-10 17:04:02 +0000345 for (BasicBlock::iterator II = BB->begin(); &*II != InsertPos; ++II) {
Chris Lattner889f6202003-04-23 16:37:45 +0000346 if (StoreInst *SI = dyn_cast<StoreInst>(II)) {
Vikram S. Adveac83df72003-07-11 21:57:43 +0000347 // Trace the stored value and address
348 InsertVerbosePrintInst(SI->getOperand(0), BB, InsertPos,
349 " (store value) ", Printf, HashPtrToSeqNum);
350 InsertVerbosePrintInst(SI->getOperand(1), BB, InsertPos,
351 " (store addr ) ", Printf, HashPtrToSeqNum);
Chris Lattner28a8d242002-09-10 17:04:02 +0000352 }
Vikram S. Adveac83df72003-07-11 21:57:43 +0000353 else if (ShouldTraceValue(II))
Chris Lattner28a8d242002-09-10 17:04:02 +0000354 InsertVerbosePrintInst(II, BB, InsertPos, " ", Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000355 }
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000356}
357
Chris Lattner6d216fd2002-07-23 18:04:15 +0000358static inline void InsertCodeToShowFunctionEntry(Function &F, Function *Printf,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000359 Function* HashPtrToSeqNum){
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000360 // Get an iterator to point to the insertion location
Chris Lattner5dac64f2003-09-20 14:39:18 +0000361 BasicBlock &BB = F.getEntryBlock();
Chris Lattner28a8d242002-09-10 17:04:02 +0000362 Instruction *InsertPos = BB.begin();
Chris Lattnerf1197b02001-12-14 16:26:05 +0000363
Chris Lattner7f74a562002-01-20 22:54:45 +0000364 std::ostringstream OutStr;
Chris Lattnerb9636a72003-01-13 00:52:14 +0000365 WriteAsOperand(OutStr, &F);
Chris Lattner28a8d242002-09-10 17:04:02 +0000366 InsertPrintInst(0, &BB, InsertPos, "ENTERING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000367 Printf, HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000368
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000369 // Now print all the incoming arguments
Chris Lattnerf1197b02001-12-14 16:26:05 +0000370 unsigned ArgNo = 0;
Chris Lattner6d216fd2002-07-23 18:04:15 +0000371 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++ArgNo){
Chris Lattner28a8d242002-09-10 17:04:02 +0000372 InsertVerbosePrintInst(I, &BB, InsertPos,
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000373 " Arg #" + utostr(ArgNo) + ": ", Printf,
374 HashPtrToSeqNum);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000375 }
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000376}
377
378
Chris Lattner57698e22002-03-26 18:01:55 +0000379static inline void InsertCodeToShowFunctionExit(BasicBlock *BB,
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000380 Function *Printf,
381 Function* HashPtrToSeqNum) {
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000382 // Get an iterator to point to the insertion location
Chris Lattner28a8d242002-09-10 17:04:02 +0000383 ReturnInst *Ret = cast<ReturnInst>(BB->getTerminator());
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000384
Chris Lattner7f74a562002-01-20 22:54:45 +0000385 std::ostringstream OutStr;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000386 WriteAsOperand(OutStr, BB->getParent(), true);
Chris Lattner28a8d242002-09-10 17:04:02 +0000387 InsertPrintInst(0, BB, Ret, "LEAVING FUNCTION: " + OutStr.str(),
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000388 Printf, HashPtrToSeqNum);
Vikram S. Advea0db1c92001-10-18 18:16:11 +0000389
Vikram S. Adveb601fda2001-11-15 15:00:16 +0000390 // print the return value, if any
Chris Lattnerf1197b02001-12-14 16:26:05 +0000391 if (BB->getParent()->getReturnType() != Type::VoidTy)
Chris Lattner28a8d242002-09-10 17:04:02 +0000392 InsertPrintInst(Ret->getReturnValue(), BB, Ret, " Returning: ",
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000393 Printf, HashPtrToSeqNum);
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000394}
395
396
Chris Lattner6d216fd2002-07-23 18:04:15 +0000397bool InsertTraceCode::runOnFunction(Function &F) {
398 if (!TraceThisFunction(F))
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000399 return false;
400
Chris Lattner23398932003-10-27 21:44:09 +0000401 std::vector<Instruction*> valuesStoredInFunction;
402 std::vector<BasicBlock*> exitBlocks;
Chris Lattnerf1197b02001-12-14 16:26:05 +0000403
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000404 // Insert code to trace values at function entry
Chris Lattner6d216fd2002-07-23 18:04:15 +0000405 InsertCodeToShowFunctionEntry(F, externalFuncs.PrintfFunc,
406 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000407
408 // Push a pointer set for recording alloca'd pointers at entry.
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000409 if (!DisablePtrHashing)
Chris Lattner23398932003-10-27 21:44:09 +0000410 new CallInst(externalFuncs.PushOnEntryFunc, std::vector<Value*>(), "",
Chris Lattner5dac64f2003-09-20 14:39:18 +0000411 F.getEntryBlock().begin());
Chris Lattner28a8d242002-09-10 17:04:02 +0000412
Chris Lattner6d216fd2002-07-23 18:04:15 +0000413 for (Function::iterator BB = F.begin(); BB != F.end(); ++BB) {
Chris Lattnerf1197b02001-12-14 16:26:05 +0000414 if (isa<ReturnInst>(BB->getTerminator()))
415 exitBlocks.push_back(BB); // record this as an exit block
Chris Lattner6d216fd2002-07-23 18:04:15 +0000416
417 // Insert trace code if this basic block is interesting...
418 handleBasicBlock(BB, valuesStoredInFunction);
419
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000420 if (!DisablePtrHashing) // release seq. numbers on free/ret
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000421 ReleasePtrSeqNumbers(BB, externalFuncs);
Chris Lattnerf1197b02001-12-14 16:26:05 +0000422 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000423
Chris Lattner6d216fd2002-07-23 18:04:15 +0000424 for (unsigned i=0; i != exitBlocks.size(); ++i)
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000425 {
426 // Insert code to trace values at function exit
Chris Lattner6d216fd2002-07-23 18:04:15 +0000427 InsertCodeToShowFunctionExit(exitBlocks[i], externalFuncs.PrintfFunc,
428 externalFuncs.HashPtrFunc);
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000429
430 // Release all recorded pointers before RETURN. Do this LAST!
Chris Lattnerace7b8d2002-05-20 21:43:59 +0000431 if (!DisablePtrHashing)
Chris Lattner23398932003-10-27 21:44:09 +0000432 new CallInst(externalFuncs.ReleaseOnReturnFunc, std::vector<Value*>(),
433 "", exitBlocks[i]->getTerminator());
Chris Lattnerf1197b02001-12-14 16:26:05 +0000434 }
Vikram S. Adve47f37c32002-05-19 15:39:02 +0000435
Chris Lattnera0a8b5b2001-10-18 05:28:08 +0000436 return true;
Vikram S. Advea200a6c2001-10-14 23:18:45 +0000437}
Brian Gaeke960707c2003-11-11 22:41:34 +0000438
439} // End llvm namespace