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/Assembly/Writer.h"
23 #include "llvm/Analysis/DebugInfo.h"
24 #include "llvm/Analysis/Passes.h"
25 #include "llvm/Analysis/ValueTracking.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/raw_ostream.h"
33 PrintDirectory("print-fullpath",
34 cl::desc("Print fullpath when printing debug info"),
38 class VISIBILITY_HIDDEN PrintDbgInfo
: public FunctionPass
{
40 void printStopPoint(const DbgStopPointInst
*DSI
);
41 void printFuncStart(const DbgFuncStartInst
*FS
);
42 void printVariableDeclaration(const Value
*V
);
44 static char ID
; // Pass identification
45 PrintDbgInfo() : FunctionPass(&ID
), Out(outs()) {}
47 virtual bool runOnFunction(Function
&F
);
48 virtual void getAnalysisUsage(AnalysisUsage
&AU
) const {
52 char PrintDbgInfo::ID
= 0;
53 static RegisterPass
<PrintDbgInfo
> X("print-dbginfo",
54 "Print debug info in human readable form");
57 FunctionPass
*llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
59 void PrintDbgInfo::printVariableDeclaration(const Value
*V
) {
60 std::string DisplayName
, File
, Directory
, Type
;
63 if (!getLocationInfo(V
, DisplayName
, Type
, LineNo
, File
, Directory
))
67 WriteAsOperand(Out
, V
, false, 0);
68 Out
<< " is variable " << DisplayName
69 << " of type " << Type
<< " declared at ";
72 Out
<< Directory
<< "/";
74 Out
<< File
<< ":" << LineNo
<< "\n";
77 void PrintDbgInfo::printStopPoint(const DbgStopPointInst
*DSI
) {
80 GetConstantStringInfo(DSI
->getDirectory(), dir
);
85 GetConstantStringInfo(DSI
->getFileName(), file
);
86 Out
<< file
<< ":" << DSI
->getLine();
88 if (unsigned Col
= DSI
->getColumn())
92 void PrintDbgInfo::printFuncStart(const DbgFuncStartInst
*FS
) {
93 DISubprogram
Subprogram(FS
->getSubprogram());
94 std::string Res1
, Res2
;
95 Out
<< "; fully qualified function name: " << Subprogram
.getDisplayName(Res1
)
96 << " return type: " << Subprogram
.getReturnTypeName(Res2
)
97 << " at line " << Subprogram
.getLineNumber()
101 bool PrintDbgInfo::runOnFunction(Function
&F
) {
102 if (F
.isDeclaration())
105 Out
<< "function " << F
.getName() << "\n\n";
107 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
) {
110 if (I
!= F
.begin() && (pred_begin(BB
) == pred_end(BB
)))
114 const DbgStopPointInst
*DSI
= findBBStopPoint(BB
);
115 Out
<< BB
->getName();
126 // A dbgstoppoint's information is valid until we encounter a new one.
127 const DbgStopPointInst
*LastDSP
= DSI
;
128 bool Printed
= DSI
!= 0;
129 for (BasicBlock::const_iterator i
= BB
->begin(), e
= BB
->end();
131 if (isa
<DbgInfoIntrinsic
>(i
)) {
132 if ((DSI
= dyn_cast
<DbgStopPointInst
>(i
))) {
133 if (DSI
->getContext() == LastDSP
->getContext() &&
134 DSI
->getLineValue() == LastDSP
->getLineValue() &&
135 DSI
->getColumnValue() == LastDSP
->getColumnValue())
136 // Don't print same location twice.
139 LastDSP
= cast
<DbgStopPointInst
>(i
);
141 // Don't print consecutive stoppoints, use a flag to know which one we
144 } else if (const DbgFuncStartInst
*FS
= dyn_cast
<DbgFuncStartInst
>(i
)) {
148 if (!Printed
&& LastDSP
) {
150 printStopPoint(LastDSP
);
156 printVariableDeclaration(i
);
158 if (const User
*U
= dyn_cast
<User
>(i
)) {
159 for(unsigned i
=0;i
<U
->getNumOperands();i
++)
160 printVariableDeclaration(U
->getOperand(i
));