blob: 127f931b6edd183fd9960faa6aa37aa8520be538 [file] [log] [blame]
Torok Edwin6e681062008-12-16 09:09:19 +00001//===- 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"
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"
29using namespace llvm;
30
31static cl::opt<bool>
32PrintDirectory("print-fullpath", cl::desc("Print fullpath when printing debug info"), cl::Hidden);
33
34namespace {
35 struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
36 private:
37 raw_ostream &Out;
38 void printStopPoint(const DbgStopPointInst *DSI);
39 void printFuncStart(const DbgFuncStartInst *FS);
40 void printVariableDeclaration(const Value *V);
41 public:
42 static char ID; // Pass identification
43 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
44
45 virtual bool runOnFunction(Function &F);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49
50 };
51 char PrintDbgInfo::ID = 0;
52 static RegisterPass<PrintDbgInfo> X("print-dbginfo",
53 "Print debug info in human readable form");
54}
55
56FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
57
58void PrintDbgInfo::printVariableDeclaration(const Value *V)
59{
60 if(const DbgDeclareInst* DDI = findDbgDeclare(V)) {
61 DIVariable Var(cast<GlobalVariable>(DDI->getVariable()));
Bill Wendlingccbdc7a2009-03-09 05:04:40 +000062 std::string Res1, Res2;
63 Out << "; variable " << Var.getName(Res1)
64 << " of type " << Var.getType().getName(Res2)
Torok Edwin6e681062008-12-16 09:09:19 +000065 << " at line " << Var.getLineNumber() << "\n";
66 }
67}
68
69void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI)
70{
71 if (PrintDirectory) {
72 std::string dir;
73 GetConstantStringInfo(DSI->getDirectory(), dir);
74 Out << dir << "/";
75 }
76 std::string file;
77 GetConstantStringInfo(DSI->getFileName(), file);
78 Out << file << ":" << DSI->getLine();
79 if (unsigned Col = DSI->getColumn()) {
80 Out << ":" << Col;
81 }
82}
83
84void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS)
85{
86 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
Bill Wendlingccbdc7a2009-03-09 05:04:40 +000087 std::string Res1, Res2;
88 Out << ";fully qualified function name: " << Subprogram.getDisplayName(Res1)
89 << " return type: " << Subprogram.getType().getName(Res2)
Torok Edwin6e681062008-12-16 09:09:19 +000090 << " at line " << Subprogram.getLineNumber()
91 << "\n\n";
92}
93
94bool PrintDbgInfo::runOnFunction(Function &F)
95{
96 if (F.isDeclaration())
97 return false;
98 Out << "function " << F.getName() << "\n\n";
99 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
100 BasicBlock *BB = I;
101 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
102 // Skip dead blocks.
103 continue;
104 const DbgStopPointInst *DSI = findBBStopPoint(BB);
105 Out << BB->getName();
106 Out << ":";
107 if (DSI) {
108 Out << "; (";
109 printStopPoint(DSI);
110 Out << ")";
111 }
112 Out << "\n";
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))) {
119
120 if (DSI->getContext() == LastDSP->getContext() &&
121 DSI->getLineValue() == LastDSP->getLineValue() &&
122 DSI->getColumnValue() == LastDSP->getColumnValue()) {
123 // Don't print same location twice.
124 continue;
125 }
126 LastDSP = cast<DbgStopPointInst>(i);
127 // Don't print consecutive stoppoints, use a flag
128 // to know which one we printed.
129 printed = false;
130
131 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
132 printFuncStart(FS);
133 }
134 } else {
135 if (!printed && LastDSP) {
136 Out << "; ";
137 printStopPoint(LastDSP);
138 Out << "\n";
139 printed = true;
140 }
141 Out << *i;
142 printVariableDeclaration(i);
143 }
144 }
145 }
146 return false;
147}