blob: 522e98ff01bb24fb293483ec66dacfc8cb52df10 [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()));
62 Out << "; variable " << Var.getName()
63 << " of type " << Var.getType().getName()
64 << " at line " << Var.getLineNumber() << "\n";
65 }
66}
67
68void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI)
69{
70 if (PrintDirectory) {
71 std::string dir;
72 GetConstantStringInfo(DSI->getDirectory(), dir);
73 Out << dir << "/";
74 }
75 std::string file;
76 GetConstantStringInfo(DSI->getFileName(), file);
77 Out << file << ":" << DSI->getLine();
78 if (unsigned Col = DSI->getColumn()) {
79 Out << ":" << Col;
80 }
81}
82
83void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS)
84{
85 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
86 Out << ";fully qualified function name: " << Subprogram.getDisplayName()
87 << " return type: " << Subprogram.getType().getName()
88 << " at line " << Subprogram.getLineNumber()
89 << "\n\n";
90}
91
92bool PrintDbgInfo::runOnFunction(Function &F)
93{
94 if (F.isDeclaration())
95 return false;
96 Out << "function " << F.getName() << "\n\n";
97 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
98 BasicBlock *BB = I;
99 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
100 // Skip dead blocks.
101 continue;
102 const DbgStopPointInst *DSI = findBBStopPoint(BB);
103 Out << BB->getName();
104 Out << ":";
105 if (DSI) {
106 Out << "; (";
107 printStopPoint(DSI);
108 Out << ")";
109 }
110 Out << "\n";
111 // A dbgstoppoint's information is valid until we encounter a new one.
112 const DbgStopPointInst *LastDSP = DSI;
113 bool printed = DSI != 0;
114 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
115 if (isa<DbgInfoIntrinsic>(i)) {
116 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
117
118 if (DSI->getContext() == LastDSP->getContext() &&
119 DSI->getLineValue() == LastDSP->getLineValue() &&
120 DSI->getColumnValue() == LastDSP->getColumnValue()) {
121 // Don't print same location twice.
122 continue;
123 }
124 LastDSP = cast<DbgStopPointInst>(i);
125 // Don't print consecutive stoppoints, use a flag
126 // to know which one we printed.
127 printed = false;
128
129 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
130 printFuncStart(FS);
131 }
132 } else {
133 if (!printed && LastDSP) {
134 Out << "; ";
135 printStopPoint(LastDSP);
136 Out << "\n";
137 printed = true;
138 }
139 Out << *i;
140 printVariableDeclaration(i);
141 }
142 }
143 }
144 return false;
145}