blob: 6346a90a68c7650374cd9d75b15715627e768b36 [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"
Torok Edwinff7d0e92009-03-10 13:41:26 +000022#include "llvm/Assembly/Writer.h"
Torok Edwin6e681062008-12-16 09:09:19 +000023#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/Compiler.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/raw_ostream.h"
30using namespace llvm;
31
32static cl::opt<bool>
33PrintDirectory("print-fullpath", cl::desc("Print fullpath when printing debug info"), cl::Hidden);
34
35namespace {
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000036 struct VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
Torok Edwin6e681062008-12-16 09:09:19 +000037 private:
38 raw_ostream &Out;
39 void printStopPoint(const DbgStopPointInst *DSI);
40 void printFuncStart(const DbgFuncStartInst *FS);
41 void printVariableDeclaration(const Value *V);
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000042 public:
43 static char ID; // Pass identification
44 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
Torok Edwin6e681062008-12-16 09:09:19 +000045
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000046 virtual bool runOnFunction(Function &F);
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.setPreservesAll();
49 }
50 };
51 char PrintDbgInfo::ID = 0;
52 static RegisterPass<PrintDbgInfo> X("print-dbginfo",
53 "Print debug info in human readable form");
Torok Edwin6e681062008-12-16 09:09:19 +000054}
55
56FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
57
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000058void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +000059 std::string DisplayName, File, Directory, Type;
60 unsigned LineNo;
61 if (getLocationInfo(V, DisplayName, Type, LineNo, File, Directory)) {
62 Out << "; ";
63 WriteAsOperand(Out, V, false, 0);
64 Out << " is variable " << DisplayName
65 << " of type " << Type << " declared at ";
66 if (PrintDirectory) {
67 Out << Directory << "/";
68 }
69 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +000070 }
71}
72
73void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI)
74{
75 if (PrintDirectory) {
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000076 const char *Dir = GetConstantStringInfo(DSI->getDirectory());
77 Out << (Dir ? Dir : "") << "/";
Torok Edwin6e681062008-12-16 09:09:19 +000078 }
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000079
80 const char *FN = GetConstantStringInfo(DSI->getFileName());
81 Out << (FN ? FN : "") << ":" << DSI->getLine();
82
83 if (unsigned Col = DSI->getColumn())
Torok Edwin6e681062008-12-16 09:09:19 +000084 Out << ":" << Col;
Torok Edwin6e681062008-12-16 09:09:19 +000085}
86
87void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS)
88{
89 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
Bill Wendlingc7a09ab2009-03-13 04:37:11 +000090 Out << ";fully qualified function name: " << Subprogram.getDisplayName()
91 << " return type: " << Subprogram.getType().getName()
Torok Edwin6e681062008-12-16 09:09:19 +000092 << " at line " << Subprogram.getLineNumber()
93 << "\n\n";
94}
95
96bool PrintDbgInfo::runOnFunction(Function &F)
97{
98 if (F.isDeclaration())
99 return false;
100 Out << "function " << F.getName() << "\n\n";
101 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
102 BasicBlock *BB = I;
103 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
104 // Skip dead blocks.
105 continue;
106 const DbgStopPointInst *DSI = findBBStopPoint(BB);
107 Out << BB->getName();
108 Out << ":";
109 if (DSI) {
110 Out << "; (";
111 printStopPoint(DSI);
112 Out << ")";
113 }
114 Out << "\n";
115 // A dbgstoppoint's information is valid until we encounter a new one.
116 const DbgStopPointInst *LastDSP = DSI;
117 bool printed = DSI != 0;
118 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end(); i != e; ++i) {
119 if (isa<DbgInfoIntrinsic>(i)) {
120 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
121
122 if (DSI->getContext() == LastDSP->getContext() &&
123 DSI->getLineValue() == LastDSP->getLineValue() &&
124 DSI->getColumnValue() == LastDSP->getColumnValue()) {
125 // Don't print same location twice.
126 continue;
127 }
128 LastDSP = cast<DbgStopPointInst>(i);
129 // Don't print consecutive stoppoints, use a flag
130 // to know which one we printed.
131 printed = false;
132
133 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
134 printFuncStart(FS);
135 }
136 } else {
137 if (!printed && LastDSP) {
138 Out << "; ";
139 printStopPoint(LastDSP);
140 Out << "\n";
141 printed = true;
142 }
143 Out << *i;
144 printVariableDeclaration(i);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000145 if (const User *U = dyn_cast<User>(i)) {
146 for(unsigned i=0;i<U->getNumOperands();i++) {
147 printVariableDeclaration(U->getOperand(i));
148 }
149 }
Torok Edwin6e681062008-12-16 09:09:19 +0000150 }
151 }
152 }
153 return false;
154}