LLVM API Documentation
00001 //===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a pass that prints instructions, and associated debug 00011 // info: 00012 // 00013 // - source/line/col information 00014 // - original variable name 00015 // - original type name 00016 // 00017 //===----------------------------------------------------------------------===// 00018 00019 #include "llvm/Pass.h" 00020 #include "llvm/Function.h" 00021 #include "llvm/IntrinsicInst.h" 00022 #include "llvm/Assembly/Writer.h" 00023 #include "llvm/Analysis/DebugInfo.h" 00024 #include "llvm/Analysis/Passes.h" 00025 #include "llvm/Analysis/ValueTracking.h" 00026 #include "llvm/Support/CFG.h" 00027 #include "llvm/Support/CommandLine.h" 00028 #include "llvm/Support/raw_ostream.h" 00029 00030 using namespace llvm; 00031 00032 static cl::opt<bool> 00033 PrintDirectory("print-fullpath", 00034 cl::desc("Print fullpath when printing debug info"), 00035 cl::Hidden); 00036 00037 namespace { 00038 class VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass { 00039 raw_ostream &Out; 00040 void printStopPoint(const DbgStopPointInst *DSI); 00041 void printFuncStart(const DbgFuncStartInst *FS); 00042 void printVariableDeclaration(const Value *V); 00043 public: 00044 static char ID; // Pass identification 00045 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {} 00046 00047 virtual bool runOnFunction(Function &F); 00048 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00049 AU.setPreservesAll(); 00050 } 00051 }; 00052 char PrintDbgInfo::ID = 0; 00053 static RegisterPass<PrintDbgInfo> X("print-dbginfo", 00054 "Print debug info in human readable form"); 00055 } 00056 00057 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); } 00058 00059 void PrintDbgInfo::printVariableDeclaration(const Value *V) { 00060 std::string DisplayName, File, Directory, Type; 00061 unsigned LineNo; 00062 00063 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) 00064 return; 00065 00066 Out << "; "; 00067 WriteAsOperand(Out, V, false, 0); 00068 Out << " is variable " << DisplayName 00069 << " of type " << Type << " declared at "; 00070 00071 if (PrintDirectory) 00072 Out << Directory << "/"; 00073 00074 Out << File << ":" << LineNo << "\n"; 00075 } 00076 00077 void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) { 00078 if (PrintDirectory) { 00079 std::string dir; 00080 GetConstantStringInfo(DSI->getDirectory(), dir); 00081 Out << dir << "/"; 00082 } 00083 00084 std::string file; 00085 GetConstantStringInfo(DSI->getFileName(), file); 00086 Out << file << ":" << DSI->getLine(); 00087 00088 if (unsigned Col = DSI->getColumn()) 00089 Out << ":" << Col; 00090 } 00091 00092 void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) { 00093 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram())); 00094 std::string Res1, Res2; 00095 Out << "; fully qualified function name: " << Subprogram.getDisplayName(Res1) 00096 << " return type: " << Subprogram.getReturnTypeName(Res2) 00097 << " at line " << Subprogram.getLineNumber() 00098 << "\n\n"; 00099 } 00100 00101 bool PrintDbgInfo::runOnFunction(Function &F) { 00102 if (F.isDeclaration()) 00103 return false; 00104 00105 Out << "function " << F.getName() << "\n\n"; 00106 00107 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00108 BasicBlock *BB = I; 00109 00110 if (I != F.begin() && (pred_begin(BB) == pred_end(BB))) 00111 // Skip dead blocks. 00112 continue; 00113 00114 const DbgStopPointInst *DSI = findBBStopPoint(BB); 00115 Out << BB->getName(); 00116 Out << ":"; 00117 00118 if (DSI) { 00119 Out << "; ("; 00120 printStopPoint(DSI); 00121 Out << ")"; 00122 } 00123 00124 Out << "\n"; 00125 00126 // A dbgstoppoint's information is valid until we encounter a new one. 00127 const DbgStopPointInst *LastDSP = DSI; 00128 bool Printed = DSI != 0; 00129 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); 00130 i != e; ++i) { 00131 if (isa<DbgInfoIntrinsic>(i)) { 00132 if ((DSI = dyn_cast<DbgStopPointInst>(i))) { 00133 if (DSI->getContext() == LastDSP->getContext() && 00134 DSI->getLineValue() == LastDSP->getLineValue() && 00135 DSI->getColumnValue() == LastDSP->getColumnValue()) 00136 // Don't print same location twice. 00137 continue; 00138 00139 LastDSP = cast<DbgStopPointInst>(i); 00140 00141 // Don't print consecutive stoppoints, use a flag to know which one we 00142 // printed. 00143 Printed = false; 00144 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) { 00145 printFuncStart(FS); 00146 } 00147 } else { 00148 if (!Printed && LastDSP) { 00149 Out << "; "; 00150 printStopPoint(LastDSP); 00151 Out << "\n"; 00152 Printed = true; 00153 } 00154 00155 Out << *i << '\n'; 00156 printVariableDeclaration(i); 00157 00158 if (const User *U = dyn_cast<User>(i)) { 00159 for(unsigned i=0;i<U->getNumOperands();i++) 00160 printVariableDeclaration(U->getOperand(i)); 00161 } 00162 } 00163 } 00164 } 00165 00166 return false; 00167 }