blob: a460777b0e639cea056e86c61972104ec9af1663 [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;
Bill Wendlinge66b2912009-05-14 18:16:46 +000040 void printVariableDeclaration(const Value *V);
41 public:
42 static char ID; // Pass identification
Owen Anderson081c34b2010-10-19 17:21:58 +000043 PrintDbgInfo() : FunctionPass(ID), Out(errs()) {
44 initializePrintDbgInfoPass(*PassRegistry::getPassRegistry());
45 }
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;
Torok Edwin6e681062008-12-16 09:09:19 +000053}
54
Owen Anderson71802342010-10-07 04:13:08 +000055INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
Owen Andersonce665bd2010-10-07 22:25:06 +000056 "Print debug info in human readable form", false, false)
Owen Anderson71802342010-10-07 04:13:08 +000057
Torok Edwin6e681062008-12-16 09:09:19 +000058FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
59
Bill Wendlinge66b2912009-05-14 18:16:46 +000060void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +000061 std::string DisplayName, File, Directory, Type;
62 unsigned LineNo;
Bill Wendlinge66b2912009-05-14 18:16:46 +000063
64 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
65 return;
66
67 Out << "; ";
68 WriteAsOperand(Out, V, false, 0);
69 Out << " is variable " << DisplayName
Torok Edwinff7d0e92009-03-10 13:41:26 +000070 << " of type " << Type << " declared at ";
Bill Wendlinge66b2912009-05-14 18:16:46 +000071
72 if (PrintDirectory)
73 Out << Directory << "/";
74
75 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +000076}
77
Bill Wendlinge66b2912009-05-14 18:16:46 +000078bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +000079 if (F.isDeclaration())
80 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +000081
Torok Edwin6e681062008-12-16 09:09:19 +000082 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000083
Torok Edwin6e681062008-12-16 09:09:19 +000084 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
85 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +000086
Torok Edwin6e681062008-12-16 09:09:19 +000087 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
88 // Skip dead blocks.
89 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +000090
Torok Edwin6e681062008-12-16 09:09:19 +000091 Out << BB->getName();
92 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +000093
Torok Edwin6e681062008-12-16 09:09:19 +000094 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000095
Bill Wendlinge66b2912009-05-14 18:16:46 +000096 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
97 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +000098
Torok Edwin6e681062008-12-16 09:09:19 +000099 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +0000100
Torok Edwinff7d0e92009-03-10 13:41:26 +0000101 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000102 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000103 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000104 }
Torok Edwin6e681062008-12-16 09:09:19 +0000105 }
106 }
Torok Edwin6e681062008-12-16 09:09:19 +0000107 return false;
108}