blob: b90a996d820c9b5d984093473073611a45175833 [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"
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"
Torok Edwin6e681062008-12-16 09:09:19 +000025#include "llvm/Support/CFG.h"
Torok Edwin6e681062008-12-16 09:09:19 +000026#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/raw_ostream.h"
Bill Wendlinge66b2912009-05-14 18:16:46 +000028
Torok Edwin6e681062008-12-16 09:09:19 +000029using namespace llvm;
30
31static cl::opt<bool>
Bill Wendlinge66b2912009-05-14 18:16:46 +000032PrintDirectory("print-fullpath",
33 cl::desc("Print fullpath when printing debug info"),
34 cl::Hidden);
Torok Edwin6e681062008-12-16 09:09:19 +000035
36namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000037 class PrintDbgInfo : public FunctionPass {
Bill Wendlinge66b2912009-05-14 18:16:46 +000038 raw_ostream &Out;
39 void printStopPoint(const DbgStopPointInst *DSI);
40 void printFuncStart(const DbgFuncStartInst *FS);
41 void printVariableDeclaration(const Value *V);
42 public:
43 static char ID; // Pass identification
44 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
Torok Edwin6e681062008-12-16 09:09:19 +000045
Bill Wendlinge66b2912009-05-14 18:16:46 +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 Wendlinge66b2912009-05-14 18:16:46 +000058void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +000059 std::string DisplayName, File, Directory, Type;
60 unsigned LineNo;
Bill Wendlinge66b2912009-05-14 18:16:46 +000061
62 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
63 return;
64
65 Out << "; ";
66 WriteAsOperand(Out, V, false, 0);
67 Out << " is variable " << DisplayName
Torok Edwinff7d0e92009-03-10 13:41:26 +000068 << " of type " << Type << " declared at ";
Bill Wendlinge66b2912009-05-14 18:16:46 +000069
70 if (PrintDirectory)
71 Out << Directory << "/";
72
73 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +000074}
75
Bill Wendlinge66b2912009-05-14 18:16:46 +000076void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) {
Chris Lattner524528e2009-12-15 19:34:20 +000077 if (PrintDirectory)
78 if (MDString *Str = dyn_cast<MDString>(DSI->getDirectory()))
79 Out << Str->getString() << '/';
Bill Wendlinge66b2912009-05-14 18:16:46 +000080
Chris Lattner524528e2009-12-15 19:34:20 +000081 if (MDString *Str = dyn_cast<MDString>(DSI->getFileName()))
82 Out << Str->getString();
83 Out << ':' << DSI->getLine();
Bill Wendlinge66b2912009-05-14 18:16:46 +000084
85 if (unsigned Col = DSI->getColumn())
Chris Lattner524528e2009-12-15 19:34:20 +000086 Out << ':' << Col;
Torok Edwin6e681062008-12-16 09:09:19 +000087}
88
Bill Wendlinge66b2912009-05-14 18:16:46 +000089void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
Devang Patele4b27562009-08-28 23:24:31 +000090 DISubprogram Subprogram(FS->getSubprogram());
Devang Patel5ccdd102009-09-29 18:40:58 +000091 Out << "; fully qualified function name: " << Subprogram.getDisplayName()
92 << " return type: " << Subprogram.getReturnTypeName()
Bill Wendlinge66b2912009-05-14 18:16:46 +000093 << " at line " << Subprogram.getLineNumber()
94 << "\n\n";
Torok Edwin6e681062008-12-16 09:09:19 +000095}
96
Bill Wendlinge66b2912009-05-14 18:16:46 +000097bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +000098 if (F.isDeclaration())
99 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000100
Torok Edwin6e681062008-12-16 09:09:19 +0000101 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000102
Torok Edwin6e681062008-12-16 09:09:19 +0000103 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
104 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000105
Torok Edwin6e681062008-12-16 09:09:19 +0000106 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
107 // Skip dead blocks.
108 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000109
Torok Edwin6e681062008-12-16 09:09:19 +0000110 const DbgStopPointInst *DSI = findBBStopPoint(BB);
111 Out << BB->getName();
112 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000113
Torok Edwin6e681062008-12-16 09:09:19 +0000114 if (DSI) {
115 Out << "; (";
116 printStopPoint(DSI);
117 Out << ")";
118 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000119
Torok Edwin6e681062008-12-16 09:09:19 +0000120 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000121
Torok Edwin6e681062008-12-16 09:09:19 +0000122 // A dbgstoppoint's information is valid until we encounter a new one.
123 const DbgStopPointInst *LastDSP = DSI;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000124 bool Printed = DSI != 0;
125 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
126 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +0000127 if (isa<DbgInfoIntrinsic>(i)) {
128 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
Torok Edwin6e681062008-12-16 09:09:19 +0000129 if (DSI->getContext() == LastDSP->getContext() &&
130 DSI->getLineValue() == LastDSP->getLineValue() &&
Bill Wendlinge66b2912009-05-14 18:16:46 +0000131 DSI->getColumnValue() == LastDSP->getColumnValue())
Torok Edwin6e681062008-12-16 09:09:19 +0000132 // Don't print same location twice.
133 continue;
Torok Edwin6e681062008-12-16 09:09:19 +0000134
Bill Wendlinge66b2912009-05-14 18:16:46 +0000135 LastDSP = cast<DbgStopPointInst>(i);
136
137 // Don't print consecutive stoppoints, use a flag to know which one we
138 // printed.
139 Printed = false;
Torok Edwin6e681062008-12-16 09:09:19 +0000140 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
141 printFuncStart(FS);
142 }
143 } else {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000144 if (!Printed && LastDSP) {
Torok Edwin6e681062008-12-16 09:09:19 +0000145 Out << "; ";
146 printStopPoint(LastDSP);
147 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000148 Printed = true;
Torok Edwin6e681062008-12-16 09:09:19 +0000149 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000150
Dan Gohman0c0932f2009-08-12 16:48:27 +0000151 Out << *i << '\n';
Torok Edwin6e681062008-12-16 09:09:19 +0000152 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +0000153
Torok Edwinff7d0e92009-03-10 13:41:26 +0000154 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000155 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000156 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000157 }
Torok Edwin6e681062008-12-16 09:09:19 +0000158 }
159 }
160 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000161
Torok Edwin6e681062008-12-16 09:09:19 +0000162 return false;
163}