blob: 7d72b383a5e009a7b1c42924735dbdaaac81331b [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"
Torok Edwinff7d0e92009-03-10 13:41:26 +000023#include "llvm/Assembly/Writer.h"
Torok Edwin6e681062008-12-16 09:09:19 +000024#include "llvm/Analysis/DebugInfo.h"
25#include "llvm/Analysis/Passes.h"
Torok Edwin6e681062008-12-16 09:09:19 +000026#include "llvm/Support/CFG.h"
Torok Edwin6e681062008-12-16 09:09:19 +000027#include "llvm/Support/CommandLine.h"
28#include "llvm/Support/raw_ostream.h"
Bill Wendlinge66b2912009-05-14 18:16:46 +000029
Torok Edwin6e681062008-12-16 09:09:19 +000030using namespace llvm;
31
32static cl::opt<bool>
Bill Wendlinge66b2912009-05-14 18:16:46 +000033PrintDirectory("print-fullpath",
34 cl::desc("Print fullpath when printing debug info"),
35 cl::Hidden);
Torok Edwin6e681062008-12-16 09:09:19 +000036
37namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000038 class PrintDbgInfo : public FunctionPass {
Bill Wendlinge66b2912009-05-14 18:16:46 +000039 raw_ostream &Out;
40 void printStopPoint(const DbgStopPointInst *DSI);
41 void printFuncStart(const DbgFuncStartInst *FS);
42 void printVariableDeclaration(const Value *V);
43 public:
44 static char ID; // Pass identification
45 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
Torok Edwin6e681062008-12-16 09:09:19 +000046
Bill Wendlinge66b2912009-05-14 18:16:46 +000047 virtual bool runOnFunction(Function &F);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
50 }
51 };
52 char PrintDbgInfo::ID = 0;
53 static RegisterPass<PrintDbgInfo> X("print-dbginfo",
54 "Print debug info in human readable form");
Torok Edwin6e681062008-12-16 09:09:19 +000055}
56
57FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
58
Bill Wendlinge66b2912009-05-14 18:16:46 +000059void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +000060 std::string DisplayName, File, Directory, Type;
61 unsigned LineNo;
Bill Wendlinge66b2912009-05-14 18:16:46 +000062
63 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
64 return;
65
66 Out << "; ";
67 WriteAsOperand(Out, V, false, 0);
68 Out << " is variable " << DisplayName
Torok Edwinff7d0e92009-03-10 13:41:26 +000069 << " of type " << Type << " declared at ";
Bill Wendlinge66b2912009-05-14 18:16:46 +000070
71 if (PrintDirectory)
72 Out << Directory << "/";
73
74 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +000075}
76
Bill Wendlinge66b2912009-05-14 18:16:46 +000077void PrintDbgInfo::printStopPoint(const DbgStopPointInst *DSI) {
Chris Lattner524528e2009-12-15 19:34:20 +000078 if (PrintDirectory)
79 if (MDString *Str = dyn_cast<MDString>(DSI->getDirectory()))
80 Out << Str->getString() << '/';
Bill Wendlinge66b2912009-05-14 18:16:46 +000081
Chris Lattner524528e2009-12-15 19:34:20 +000082 if (MDString *Str = dyn_cast<MDString>(DSI->getFileName()))
83 Out << Str->getString();
84 Out << ':' << DSI->getLine();
Bill Wendlinge66b2912009-05-14 18:16:46 +000085
86 if (unsigned Col = DSI->getColumn())
Chris Lattner524528e2009-12-15 19:34:20 +000087 Out << ':' << Col;
Torok Edwin6e681062008-12-16 09:09:19 +000088}
89
Bill Wendlinge66b2912009-05-14 18:16:46 +000090void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
Devang Patele4b27562009-08-28 23:24:31 +000091 DISubprogram Subprogram(FS->getSubprogram());
Devang Patel5ccdd102009-09-29 18:40:58 +000092 Out << "; fully qualified function name: " << Subprogram.getDisplayName()
93 << " return type: " << Subprogram.getReturnTypeName()
Bill Wendlinge66b2912009-05-14 18:16:46 +000094 << " at line " << Subprogram.getLineNumber()
95 << "\n\n";
Torok Edwin6e681062008-12-16 09:09:19 +000096}
97
Bill Wendlinge66b2912009-05-14 18:16:46 +000098bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +000099 if (F.isDeclaration())
100 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000101
Torok Edwin6e681062008-12-16 09:09:19 +0000102 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000103
Torok Edwin6e681062008-12-16 09:09:19 +0000104 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
105 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000106
Torok Edwin6e681062008-12-16 09:09:19 +0000107 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
108 // Skip dead blocks.
109 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000110
Torok Edwin6e681062008-12-16 09:09:19 +0000111 const DbgStopPointInst *DSI = findBBStopPoint(BB);
112 Out << BB->getName();
113 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000114
Torok Edwin6e681062008-12-16 09:09:19 +0000115 if (DSI) {
116 Out << "; (";
117 printStopPoint(DSI);
118 Out << ")";
119 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000120
Torok Edwin6e681062008-12-16 09:09:19 +0000121 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000122
Torok Edwin6e681062008-12-16 09:09:19 +0000123 // A dbgstoppoint's information is valid until we encounter a new one.
124 const DbgStopPointInst *LastDSP = DSI;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000125 bool Printed = DSI != 0;
126 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
127 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +0000128 if (isa<DbgInfoIntrinsic>(i)) {
129 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
Torok Edwin6e681062008-12-16 09:09:19 +0000130 if (DSI->getContext() == LastDSP->getContext() &&
131 DSI->getLineValue() == LastDSP->getLineValue() &&
Bill Wendlinge66b2912009-05-14 18:16:46 +0000132 DSI->getColumnValue() == LastDSP->getColumnValue())
Torok Edwin6e681062008-12-16 09:09:19 +0000133 // Don't print same location twice.
134 continue;
Torok Edwin6e681062008-12-16 09:09:19 +0000135
Bill Wendlinge66b2912009-05-14 18:16:46 +0000136 LastDSP = cast<DbgStopPointInst>(i);
137
138 // Don't print consecutive stoppoints, use a flag to know which one we
139 // printed.
140 Printed = false;
Torok Edwin6e681062008-12-16 09:09:19 +0000141 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
142 printFuncStart(FS);
143 }
144 } else {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000145 if (!Printed && LastDSP) {
Torok Edwin6e681062008-12-16 09:09:19 +0000146 Out << "; ";
147 printStopPoint(LastDSP);
148 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000149 Printed = true;
Torok Edwin6e681062008-12-16 09:09:19 +0000150 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000151
Dan Gohman0c0932f2009-08-12 16:48:27 +0000152 Out << *i << '\n';
Torok Edwin6e681062008-12-16 09:09:19 +0000153 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +0000154
Torok Edwinff7d0e92009-03-10 13:41:26 +0000155 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000156 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000157 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000158 }
Torok Edwin6e681062008-12-16 09:09:19 +0000159 }
160 }
161 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000162
Torok Edwin6e681062008-12-16 09:09:19 +0000163 return false;
164}