blob: 6c549e6345e649a17bab1c893b40c9ff96d0ac85 [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"
25#include "llvm/Analysis/ValueTracking.h"
26#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 {
Bill Wendlinge66b2912009-05-14 18:16:46 +000038 class VISIBILITY_HIDDEN PrintDbgInfo : public FunctionPass {
39 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) {
Torok Edwin6e681062008-12-16 09:09:19 +000078 if (PrintDirectory) {
Bill Wendling0582ae92009-03-13 04:39:26 +000079 std::string dir;
Bill Wendlinge66b2912009-05-14 18:16:46 +000080 GetConstantStringInfo(DSI->getDirectory(), dir);
Bill Wendling0582ae92009-03-13 04:39:26 +000081 Out << dir << "/";
Torok Edwin6e681062008-12-16 09:09:19 +000082 }
Bill Wendlinge66b2912009-05-14 18:16:46 +000083
Bill Wendling0582ae92009-03-13 04:39:26 +000084 std::string file;
85 GetConstantStringInfo(DSI->getFileName(), file);
86 Out << file << ":" << DSI->getLine();
Bill Wendlinge66b2912009-05-14 18:16:46 +000087
88 if (unsigned Col = DSI->getColumn())
Torok Edwin6e681062008-12-16 09:09:19 +000089 Out << ":" << Col;
Torok Edwin6e681062008-12-16 09:09:19 +000090}
91
Bill Wendlinge66b2912009-05-14 18:16:46 +000092void PrintDbgInfo::printFuncStart(const DbgFuncStartInst *FS) {
Torok Edwin6e681062008-12-16 09:09:19 +000093 DISubprogram Subprogram(cast<GlobalVariable>(FS->getSubprogram()));
Bill Wendling0582ae92009-03-13 04:39:26 +000094 std::string Res1, Res2;
Bill Wendlinge66b2912009-05-14 18:16:46 +000095 Out << "; fully qualified function name: " << Subprogram.getDisplayName(Res1)
Devang Pateld9338032009-06-23 22:23:13 +000096 << " return type: " << Subprogram.getReturnTypeName(Res2)
Bill Wendlinge66b2912009-05-14 18:16:46 +000097 << " at line " << Subprogram.getLineNumber()
98 << "\n\n";
Torok Edwin6e681062008-12-16 09:09:19 +000099}
100
Bill Wendlinge66b2912009-05-14 18:16:46 +0000101bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +0000102 if (F.isDeclaration())
103 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000104
Torok Edwin6e681062008-12-16 09:09:19 +0000105 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000106
Torok Edwin6e681062008-12-16 09:09:19 +0000107 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
108 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000109
Torok Edwin6e681062008-12-16 09:09:19 +0000110 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
111 // Skip dead blocks.
112 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000113
Torok Edwin6e681062008-12-16 09:09:19 +0000114 const DbgStopPointInst *DSI = findBBStopPoint(BB);
115 Out << BB->getName();
116 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000117
Torok Edwin6e681062008-12-16 09:09:19 +0000118 if (DSI) {
119 Out << "; (";
120 printStopPoint(DSI);
121 Out << ")";
122 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000123
Torok Edwin6e681062008-12-16 09:09:19 +0000124 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000125
Torok Edwin6e681062008-12-16 09:09:19 +0000126 // A dbgstoppoint's information is valid until we encounter a new one.
127 const DbgStopPointInst *LastDSP = DSI;
Bill Wendlinge66b2912009-05-14 18:16:46 +0000128 bool Printed = DSI != 0;
129 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
130 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +0000131 if (isa<DbgInfoIntrinsic>(i)) {
132 if ((DSI = dyn_cast<DbgStopPointInst>(i))) {
Torok Edwin6e681062008-12-16 09:09:19 +0000133 if (DSI->getContext() == LastDSP->getContext() &&
134 DSI->getLineValue() == LastDSP->getLineValue() &&
Bill Wendlinge66b2912009-05-14 18:16:46 +0000135 DSI->getColumnValue() == LastDSP->getColumnValue())
Torok Edwin6e681062008-12-16 09:09:19 +0000136 // Don't print same location twice.
137 continue;
Torok Edwin6e681062008-12-16 09:09:19 +0000138
Bill Wendlinge66b2912009-05-14 18:16:46 +0000139 LastDSP = cast<DbgStopPointInst>(i);
140
141 // Don't print consecutive stoppoints, use a flag to know which one we
142 // printed.
143 Printed = false;
Torok Edwin6e681062008-12-16 09:09:19 +0000144 } else if (const DbgFuncStartInst *FS = dyn_cast<DbgFuncStartInst>(i)) {
145 printFuncStart(FS);
146 }
147 } else {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000148 if (!Printed && LastDSP) {
Torok Edwin6e681062008-12-16 09:09:19 +0000149 Out << "; ";
150 printStopPoint(LastDSP);
151 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +0000152 Printed = true;
Torok Edwin6e681062008-12-16 09:09:19 +0000153 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000154
Torok Edwin6e681062008-12-16 09:09:19 +0000155 Out << *i;
156 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +0000157
Torok Edwinff7d0e92009-03-10 13:41:26 +0000158 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000159 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000160 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000161 }
Torok Edwin6e681062008-12-16 09:09:19 +0000162 }
163 }
164 }
Bill Wendlinge66b2912009-05-14 18:16:46 +0000165
Torok Edwin6e681062008-12-16 09:09:19 +0000166 return false;
167}