blob: cd832abeba51e85829bc29120d0f91b88bb3ad60 [file] [log] [blame]
Bill Wendlinge66b2912009-05-14 18:16:46 +00001//===- DbgInfoPrinter.cpp - Print debug info in a human readable form ------==//
Torok Edwin6e681062008-12-16 09:09:19 +00002//
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:
Bill Wendlinge66b2912009-05-14 18:16:46 +000012//
Torok Edwin6e681062008-12-16 09:09:19 +000013// - source/line/col information
14// - original variable name
15// - original type name
16//
17//===----------------------------------------------------------------------===//
Bill Wendlinge66b2912009-05-14 18:16:46 +000018
Torok Edwin6e681062008-12-16 09:09:19 +000019#include "llvm/Pass.h"
20#include "llvm/Function.h"
Torok Edwin6e681062008-12-16 09:09:19 +000021#include "llvm/IntrinsicInst.h"
Chris Lattnerf0908a32009-12-31 03:02:08 +000022#include "llvm/Metadata.h"
Devang Patelfc0569e2011-02-15 17:36:11 +000023#include "llvm/Module.h"
Torok Edwinff7d0e92009-03-10 13:41:26 +000024#include "llvm/Assembly/Writer.h"
Torok Edwin6e681062008-12-16 09:09:19 +000025#include "llvm/Analysis/DebugInfo.h"
26#include "llvm/Analysis/Passes.h"
Torok Edwin6e681062008-12-16 09:09:19 +000027#include "llvm/Support/CFG.h"
Torok Edwin6e681062008-12-16 09:09:19 +000028#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/raw_ostream.h"
Bill Wendlinge66b2912009-05-14 18:16:46 +000030
Torok Edwin6e681062008-12-16 09:09:19 +000031using namespace llvm;
32
33static cl::opt<bool>
Bill Wendlinge66b2912009-05-14 18:16:46 +000034PrintDirectory("print-fullpath",
35 cl::desc("Print fullpath when printing debug info"),
36 cl::Hidden);
Torok Edwin6e681062008-12-16 09:09:19 +000037
38namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000039 class PrintDbgInfo : public FunctionPass {
Bill Wendlinge66b2912009-05-14 18:16:46 +000040 raw_ostream &Out;
Bill Wendlinge66b2912009-05-14 18:16:46 +000041 void printVariableDeclaration(const Value *V);
42 public:
43 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000044 PrintDbgInfo() : FunctionPass(ID), Out(errs()) {
45 initializePrintDbgInfoPass(*PassRegistry::getPassRegistry());
46 }
Torok Edwin6e681062008-12-16 09:09:19 +000047
Bill Wendlinge66b2912009-05-14 18:16:46 +000048 virtual bool runOnFunction(Function &F);
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.setPreservesAll();
51 }
52 };
53 char PrintDbgInfo::ID = 0;
Torok Edwin6e681062008-12-16 09:09:19 +000054}
55
Owen Anderson71802342010-10-07 04:13:08 +000056INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
Owen Andersonce665bd2010-10-07 22:25:06 +000057 "Print debug info in human readable form", false, false)
Owen Anderson71802342010-10-07 04:13:08 +000058
Torok Edwin6e681062008-12-16 09:09:19 +000059FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
60
Devang Patelfc0569e2011-02-15 17:36:11 +000061/// Find the debug info descriptor corresponding to this global variable.
62static Value *findDbgGlobalDeclare(GlobalVariable *V) {
63 const Module *M = V->getParent();
64 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
65 if (!NMD)
66 return 0;
67
68 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
69 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
70 if (!DIG.isGlobalVariable())
71 continue;
72 if (DIGlobalVariable(DIG).getGlobal() == V)
73 return DIG;
74 }
75 return 0;
76}
77
78/// Find the debug info descriptor corresponding to this function.
79static Value *findDbgSubprogramDeclare(Function *V) {
80 const Module *M = V->getParent();
81 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.sp");
82 if (!NMD)
83 return 0;
84
85 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
86 DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
87 if (!DIG.isSubprogram())
88 continue;
89 if (DISubprogram(DIG).getFunction() == V)
90 return DIG;
91 }
92 return 0;
93}
94
95/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
96/// It looks through pointer casts too.
97static const DbgDeclareInst *findDbgDeclare(const Value *V) {
98 V = V->stripPointerCasts();
99
100 if (!isa<Instruction>(V) && !isa<Argument>(V))
101 return 0;
102
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))
107 F = A->getParent();
108
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();
111 BI != BE; ++BI)
112 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
113 if (DDI->getAddress() == V)
114 return DDI;
115
116 return 0;
117}
118
119static bool getLocationInfo(const Value *V, std::string &DisplayName,
120 std::string &Type, unsigned &LineNo,
121 std::string &File, std::string &Dir) {
122 DICompileUnit Unit;
123 DIType TypeD;
124
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));
129
130 StringRef D = Var.getDisplayName();
131 if (!D.empty())
132 DisplayName = D;
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));
140
141 StringRef D = Var.getDisplayName();
142 if (!D.empty())
143 DisplayName = D;
144 LineNo = Var.getLineNumber();
145 Unit = Var.getCompileUnit();
146 TypeD = Var.getType();
147 } else {
148 const DbgDeclareInst *DDI = findDbgDeclare(V);
149 if (!DDI) return false;
150 DIVariable Var(cast<MDNode>(DDI->getVariable()));
151
152 StringRef D = Var.getName();
153 if (!D.empty())
154 DisplayName = D;
155 LineNo = Var.getLineNumber();
156 Unit = Var.getCompileUnit();
157 TypeD = Var.getType();
158 }
159
160 StringRef T = TypeD.getName();
161 if (!T.empty())
162 Type = T;
163 StringRef F = Unit.getFilename();
164 if (!F.empty())
165 File = F;
166 StringRef D = Unit.getDirectory();
167 if (!D.empty())
168 Dir = D;
169 return true;
170}
171
Bill Wendlinge66b2912009-05-14 18:16:46 +0000172void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +0000173 std::string DisplayName, File, Directory, Type;
Galina Kistanovaef01f322011-09-21 23:34:23 +0000174 unsigned LineNo = 0;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000175
176 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
177 return;
178
179 Out << "; ";
180 WriteAsOperand(Out, V, false, 0);
Devang Patel497acb92011-02-15 17:24:56 +0000181 if (isa<Function>(V))
182 Out << " is function " << DisplayName
183 << " of type " << Type << " declared at ";
184 else
185 Out << " is variable " << DisplayName
186 << " of type " << Type << " declared at ";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000187
188 if (PrintDirectory)
189 Out << Directory << "/";
190
191 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +0000192}
193
Bill Wendlinge66b2912009-05-14 18:16:46 +0000194bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +0000195 if (F.isDeclaration())
196 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000197
Torok Edwin6e681062008-12-16 09:09:19 +0000198 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000199
Torok Edwin6e681062008-12-16 09:09:19 +0000200 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
201 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000202
Torok Edwin6e681062008-12-16 09:09:19 +0000203 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
204 // Skip dead blocks.
205 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000206
Torok Edwin6e681062008-12-16 09:09:19 +0000207 Out << BB->getName();
208 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000209
Torok Edwin6e681062008-12-16 09:09:19 +0000210 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000211
Bill Wendlinge66b2912009-05-14 18:16:46 +0000212 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
213 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +0000214
Torok Edwin6e681062008-12-16 09:09:19 +0000215 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +0000216
Torok Edwinff7d0e92009-03-10 13:41:26 +0000217 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000218 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000219 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000220 }
Torok Edwin6e681062008-12-16 09:09:19 +0000221 }
222 }
Torok Edwin6e681062008-12-16 09:09:19 +0000223 return false;
224}