1 //===- DbgInfoPrinter.cpp - Print debug info in a human readable form -----==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a pass that prints instructions, and associated debug
12 // - source/line/col information
13 // - original variable name
14 // - original type name
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"
22 #include "llvm/Analysis/DebugInfo.h"
23 #include "llvm/Analysis/Passes.h"
24 #include "llvm/Analysis/ValueTracking.h"
25 #include "llvm/Support/CFG.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/raw_ostream.h"
32 PrintDirectory("print-fullpath", cl::desc("Print fullpath when printing debug info"), cl::Hidden);
35 struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
38 void printStopPoint(const DbgStopPointInst *DSI);
39 void printFuncStart(const DbgFuncStartInst *FS);
40 void printVariableDeclaration(const Value *V);
42 static char ID; // Pass identification
43 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
45 virtual bool runOnFunction(Function &F);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51 char PrintDbgInfo::ID = 0;
52 static RegisterPass<PrintDbgInfo> X("print-dbginfo",
53 "Print debug info in human readable form");
56 FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
58 void PrintDbgInfo::printVariableDeclaration(const Value *V)
60 if(const DbgDeclareInst* DDI = findDbgDeclare(V)) {
61 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
62 std::string Res1, Res2;
63 Out << "; variable " << Var.getName(Res1)
64 << " of type " << Var.getType().getName(Res2)
65 << " at line " << Var.getLineNumber() << "\n";
69 void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI)
73 GetConstantStringInfo(DSI->getDirectory(), dir);
77 GetConstantStringInfo(DSI->getFileName(), file);
78 Out << file << ":" << DSI->getLine();
79 if (unsigned Col = DSI->getColumn()) {
84 void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS)
86 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
87 std::string Res1, Res2;
88 Out << ";fully qualified function name: " << Subprogram.getDisplayName(Res1)
89 << " return type: " << Subprogram.getType().getName(Res2)
90 << " at line " << Subprogram.getLineNumber()
94 bool PrintDbgInfo::runOnFunction(Function &F)
96 if (F.isDeclaration())
98 Out << "function " << F.getName() << "\n\n";
99 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
101 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
104 const DbgStopPointInst *DSI = findBBStopPoint(BB);
105 Out << BB->getName();
113 // A dbgstoppoint's information is valid until we encounter a new one.
114 const DbgStopPointInst *LastDSP = DSI;
115 bool printed = DSI != 0;
116 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
117 if (isa<DbgInfoIntrinsic>(i)) {
118 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
120 if (DSI->getContext() == LastDSP->getContext() &&
121 DSI->getLineValue() == LastDSP->getLineValue() &&
122 DSI->getColumnValue() == LastDSP->getColumnValue()) {
123 // Don't print same location twice.
126 LastDSP = cast<DbgStopPointInst>(i);
127 // Don't print consecutive stoppoints, use a flag
128 // to know which one we printed.
131 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
135 if (!printed && LastDSP) {
137 printStopPoint(LastDSP);
142 printVariableDeclaration(i);