Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 1 | //===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==// |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a pass that prints instructions, and associated debug |
| 11 | // info: |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 12 | // |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 13 | // - source/line/col information |
| 14 | // - original variable name |
| 15 | // - original type name |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 18 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Function.h" |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 21 | #include "llvm/IntrinsicInst.h" |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 22 | #include "llvm/Assembly/Writer.h" |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/DebugInfo.h" |
| 24 | #include "llvm/Analysis/Passes.h" |
| 25 | #include "llvm/Analysis/ValueTracking.h" |
| 26 | #include "llvm/Support/CFG.h" |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 27 | #include "llvm/Support/CommandLine.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 29 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
| 32 | static cl::opt<bool> |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 33 | PrintDirectory("print-fullpath", |
| 34 | cl::desc("Print fullpath when printing debug info"), |
| 35 | cl::Hidden); |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 36 | |
| 37 | namespace { |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 38 | class VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass { |
| 39 | raw_ostream &Out; |
| 40 | void printStopPoint(const DbgStopPointInst *DSI); |
| 41 | void printFuncStart(const DbgFuncStartInst *FS); |
| 42 | void printVariableDeclaration(const Value *V); |
| 43 | public: |
| 44 | static char ID; // Pass identification |
| 45 | PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {} |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 46 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 47 | virtual bool runOnFunction(Function &F); |
| 48 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 49 | AU.setPreservesAll(); |
| 50 | } |
| 51 | }; |
| 52 | char PrintDbgInfo::ID = 0; |
| 53 | static RegisterPass<PrintDbgInfo> X("print-dbginfo", |
| 54 | "Print debug info in human readable form"); |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); } |
| 58 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 59 | void PrintDbgInfo::printVariableDeclaration(const Value *V) { |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 60 | std::string DisplayName, File, Directory, Type; |
| 61 | unsigned LineNo; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 62 | |
| 63 | if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) |
| 64 | return; |
| 65 | |
| 66 | Out << "; "; |
| 67 | WriteAsOperand(Out, V, false, 0); |
| 68 | Out << " is variable " << DisplayName |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 69 | << " of type " << Type << " declared at "; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 70 | |
| 71 | if (PrintDirectory) |
| 72 | Out << Directory << "/"; |
| 73 | |
| 74 | Out << File << ":" << LineNo << "\n"; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 77 | void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 78 | if (PrintDirectory) { |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 79 | std::string dir; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 80 | GetConstantStringInfo(DSI->getDirectory(), dir); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 81 | Out << dir << "/"; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 82 | } |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 83 | |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 84 | std::string file; |
| 85 | GetConstantStringInfo(DSI->getFileName(), file); |
| 86 | Out << file << ":" << DSI->getLine(); |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 87 | |
| 88 | if (unsigned Col = DSI->getColumn()) |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 89 | Out << ":" << Col; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 92 | void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 93 | DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram())); |
Bill Wendling | 0582ae9 | 2009-03-13 04:39:26 +0000 | [diff] [blame] | 94 | std::string Res1, Res2; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 95 | Out << "; fully qualified function name: " << Subprogram.getDisplayName(Res1) |
Devang Patel | d933803 | 2009-06-23 22:23:13 +0000 | [diff] [blame] | 96 | << " return type: " << Subprogram.getReturnTypeName(Res2) |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 97 | << " at line " << Subprogram.getLineNumber() |
| 98 | << "\n\n"; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 101 | bool PrintDbgInfo::runOnFunction(Function &F) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 102 | if (F.isDeclaration()) |
| 103 | return false; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 104 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 105 | Out << "function " << F.getName() << "\n\n"; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 106 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 107 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 108 | BasicBlock *BB = I; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 109 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 110 | if (I != F.begin() && (pred_begin(BB) == pred_end(BB))) |
| 111 | // Skip dead blocks. |
| 112 | continue; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 113 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 114 | const DbgStopPointInst *DSI = findBBStopPoint(BB); |
| 115 | Out << BB->getName(); |
| 116 | Out << ":"; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 117 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 118 | if (DSI) { |
| 119 | Out << "; ("; |
| 120 | printStopPoint(DSI); |
| 121 | Out << ")"; |
| 122 | } |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 123 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 124 | Out << "\n"; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 125 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 126 | // A dbgstoppoint's information is valid until we encounter a new one. |
| 127 | const DbgStopPointInst *LastDSP = DSI; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 128 | bool Printed = DSI != 0; |
| 129 | for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); |
| 130 | i != e; ++i) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 131 | if (isa<DbgInfoIntrinsic>(i)) { |
| 132 | if ((DSI = dyn_cast<DbgStopPointInst>(i))) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 133 | if (DSI->getContext() == LastDSP->getContext() && |
| 134 | DSI->getLineValue() == LastDSP->getLineValue() && |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 135 | DSI->getColumnValue() == LastDSP->getColumnValue()) |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 136 | // Don't print same location twice. |
| 137 | continue; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 138 | |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 139 | LastDSP = cast<DbgStopPointInst>(i); |
| 140 | |
| 141 | // Don't print consecutive stoppoints, use a flag to know which one we |
| 142 | // printed. |
| 143 | Printed = false; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 144 | } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) { |
| 145 | printFuncStart(FS); |
| 146 | } |
| 147 | } else { |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 148 | if (!Printed && LastDSP) { |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 149 | Out << "; "; |
| 150 | printStopPoint(LastDSP); |
| 151 | Out << "\n"; |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 152 | Printed = true; |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 153 | } |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 154 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 155 | Out << *i; |
| 156 | printVariableDeclaration(i); |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 157 | |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 158 | if (const User *U = dyn_cast<User>(i)) { |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 159 | for(unsigned i=0;i<U->getNumOperands();i++) |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 160 | printVariableDeclaration(U->getOperand(i)); |
Torok Edwin | ff7d0e9 | 2009-03-10 13:41:26 +0000 | [diff] [blame] | 161 | } |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
Bill Wendling | e66b291 | 2009-05-14 18:16:46 +0000 | [diff] [blame] | 165 | |
Torok Edwin | 6e68106 | 2008-12-16 09:09:19 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |