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