blob: aef79787715b9d05d4e63b24967cf8fe7c9bac0d [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
43 PrintDbgInfo() : FunctionPass(&ID), Out(outs()) {}
Torok Edwin6e681062008-12-16 09:09:19 +000044
Bill Wendlinge66b2912009-05-14 18:16:46 +000045 virtual bool runOnFunction(Function &F);
46 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47 AU.setPreservesAll();
48 }
49 };
50 char PrintDbgInfo::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000051 INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
52 "Print debug info in human readable form", false, false);
Torok Edwin6e681062008-12-16 09:09:19 +000053}
54
55FunctionPass *llvm::createDbgInfoPrinterPass() { return new PrintDbgInfo(); }
56
Bill Wendlinge66b2912009-05-14 18:16:46 +000057void PrintDbgInfo::printVariableDeclaration(const Value *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +000058 std::string DisplayName, File, Directory, Type;
59 unsigned LineNo;
Bill Wendlinge66b2912009-05-14 18:16:46 +000060
61 if (!getLocationInfo(V, DisplayName, Type, LineNo, File, Directory))
62 return;
63
64 Out << "; ";
65 WriteAsOperand(Out, V, false, 0);
66 Out << " is variable " << DisplayName
Torok Edwinff7d0e92009-03-10 13:41:26 +000067 << " of type " << Type << " declared at ";
Bill Wendlinge66b2912009-05-14 18:16:46 +000068
69 if (PrintDirectory)
70 Out << Directory << "/";
71
72 Out << File << ":" << LineNo << "\n";
Torok Edwin6e681062008-12-16 09:09:19 +000073}
74
Bill Wendlinge66b2912009-05-14 18:16:46 +000075bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +000076 if (F.isDeclaration())
77 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +000078
Torok Edwin6e681062008-12-16 09:09:19 +000079 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000080
Torok Edwin6e681062008-12-16 09:09:19 +000081 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
82 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +000083
Torok Edwin6e681062008-12-16 09:09:19 +000084 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
85 // Skip dead blocks.
86 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +000087
Torok Edwin6e681062008-12-16 09:09:19 +000088 Out << BB->getName();
89 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +000090
Torok Edwin6e681062008-12-16 09:09:19 +000091 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000092
Bill Wendlinge66b2912009-05-14 18:16:46 +000093 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
94 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +000095
Torok Edwin6e681062008-12-16 09:09:19 +000096 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +000097
Torok Edwinff7d0e92009-03-10 13:41:26 +000098 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +000099 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000100 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000101 }
Torok Edwin6e681062008-12-16 09:09:19 +0000102 }
103 }
Torok Edwin6e681062008-12-16 09:09:19 +0000104 return false;
105}