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
13 // - source/line/col information
14 // - original variable name
15 // - original type name
17 //===----------------------------------------------------------------------===//
19 #include "llvm/Pass.h"
20 #include "llvm/Function.h"
21 #include "llvm/IntrinsicInst.h"
22 #include "llvm/Metadata.h"
23 #include "llvm/Module.h"
24 #include "llvm/Assembly/Writer.h"
25 #include "llvm/Analysis/DebugInfo.h"
26 #include "llvm/Analysis/Passes.h"
27 #include "llvm/Support/CFG.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/raw_ostream.h"
34 PrintDirectory("print-fullpath",
35 cl::desc("Print fullpath when printing debug info"),
39 class PrintDbgInfo
: public FunctionPass
{
41 void printVariableDeclaration(const Value
*V
);
43 static char ID
; // Pass identification
44 PrintDbgInfo() : FunctionPass(ID
), Out(errs()) {
45 initializePrintDbgInfoPass(*PassRegistry::getPassRegistry());
48 virtual bool runOnFunction(Function
&F
);
49 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
53 char PrintDbgInfo::ID
= 0;
56 INITIALIZE_PASS(PrintDbgInfo
, "print-dbginfo",
57 "Print debug info in human readable form", false, false)
59 FunctionPass
*llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
61 /// Find the debug info descriptor corresponding to this global variable.
62 static Value
*findDbgGlobalDeclare(GlobalVariable
*V
) {
63 const Module
*M
= V
->getParent();
64 NamedMDNode
*NMD
= M
->getNamedMetadata("llvm.dbg.gv");
68 for (unsigned i
= 0, e
= NMD
->getNumOperands(); i
!= e
; ++i
) {
69 DIDescriptor
DIG(cast
<MDNode
>(NMD
->getOperand(i
)));
70 if (!DIG
.isGlobalVariable())
72 if (DIGlobalVariable(DIG
).getGlobal() == V
)
78 /// Find the debug info descriptor corresponding to this function.
79 static Value
*findDbgSubprogramDeclare(Function
*V
) {
80 const Module
*M
= V
->getParent();
81 NamedMDNode
*NMD
= M
->getNamedMetadata("llvm.dbg.sp");
85 for (unsigned i
= 0, e
= NMD
->getNumOperands(); i
!= e
; ++i
) {
86 DIDescriptor
DIG(cast
<MDNode
>(NMD
->getOperand(i
)));
87 if (!DIG
.isSubprogram())
89 if (DISubprogram(DIG
).getFunction() == V
)
95 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
96 /// It looks through pointer casts too.
97 static const DbgDeclareInst
*findDbgDeclare(const Value
*V
) {
98 V
= V
->stripPointerCasts();
100 if (!isa
<Instruction
>(V
) && !isa
<Argument
>(V
))
103 const Function
*F
= NULL
;
104 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
))
105 F
= I
->getParent()->getParent();
106 else if (const Argument
*A
= dyn_cast
<Argument
>(V
))
109 for (Function::const_iterator FI
= F
->begin(), FE
= F
->end(); FI
!= FE
; ++FI
)
110 for (BasicBlock::const_iterator BI
= (*FI
).begin(), BE
= (*FI
).end();
112 if (const DbgDeclareInst
*DDI
= dyn_cast
<DbgDeclareInst
>(BI
))
113 if (DDI
->getAddress() == V
)
119 static bool getLocationInfo(const Value
*V
, std::string
&DisplayName
,
120 std::string
&Type
, unsigned &LineNo
,
121 std::string
&File
, std::string
&Dir
) {
125 if (GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(const_cast<Value
*>(V
))) {
126 Value
*DIGV
= findDbgGlobalDeclare(GV
);
127 if (!DIGV
) return false;
128 DIGlobalVariable
Var(cast
<MDNode
>(DIGV
));
130 StringRef D
= Var
.getDisplayName();
133 LineNo
= Var
.getLineNumber();
134 Unit
= Var
.getCompileUnit();
135 TypeD
= Var
.getType();
136 } else if (Function
*F
= dyn_cast
<Function
>(const_cast<Value
*>(V
))){
137 Value
*DIF
= findDbgSubprogramDeclare(F
);
138 if (!DIF
) return false;
139 DISubprogram
Var(cast
<MDNode
>(DIF
));
141 StringRef D
= Var
.getDisplayName();
144 LineNo
= Var
.getLineNumber();
145 Unit
= Var
.getCompileUnit();
146 TypeD
= Var
.getType();
148 const DbgDeclareInst
*DDI
= findDbgDeclare(V
);
149 if (!DDI
) return false;
150 DIVariable
Var(cast
<MDNode
>(DDI
->getVariable()));
152 StringRef D
= Var
.getName();
155 LineNo
= Var
.getLineNumber();
156 Unit
= Var
.getCompileUnit();
157 TypeD
= Var
.getType();
160 StringRef T
= TypeD
.getName();
163 StringRef F
= Unit
.getFilename();
166 StringRef D
= Unit
.getDirectory();
172 void PrintDbgInfo::printVariableDeclaration(const Value
*V
) {
173 std::string DisplayName
, File
, Directory
, Type
;
176 if (!getLocationInfo(V
, DisplayName
, Type
, LineNo
, File
, Directory
))
180 WriteAsOperand(Out
, V
, false, 0);
181 if (isa
<Function
>(V
))
182 Out
<< " is function " << DisplayName
183 << " of type " << Type
<< " declared at ";
185 Out
<< " is variable " << DisplayName
186 << " of type " << Type
<< " declared at ";
189 Out
<< Directory
<< "/";
191 Out
<< File
<< ":" << LineNo
<< "\n";
194 bool PrintDbgInfo::runOnFunction(Function
&F
) {
195 if (F
.isDeclaration())
198 Out
<< "function " << F
.getName() << "\n\n";
200 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
) {
203 if (I
!= F
.begin() && (pred_begin(BB
) == pred_end(BB
)))
207 Out
<< BB
->getName();
212 for (BasicBlock::const_iterator i
= BB
->begin(), e
= BB
->end();
215 printVariableDeclaration(i
);
217 if (const User
*U
= dyn_cast
<User
>(i
)) {
218 for(unsigned i
=0;i
<U
->getNumOperands();i
++)
219 printVariableDeclaration(U
->getOperand(i
));