blob: 71431c2700e2075315071b6438bc05c90564befe [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
Dan Gohman73166382010-08-20 18:03:05 +000043 PrintDbgInfo() : FunctionPass(ID), Out(errs()) {}
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;
Torok Edwin6e681062008-12-16 09:09:19 +000051}
52
Owen Anderson71802342010-10-07 04:13:08 +000053INITIALIZE_PASS(PrintDbgInfo, "print-dbginfo",
Owen Andersonce665bd2010-10-07 22:25:06 +000054 "Print debug info in human readable form", false, false)
Owen Anderson71802342010-10-07 04:13:08 +000055
Torok Edwin6e681062008-12-16 09:09:19 +000056FunctionPass *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 +000076bool PrintDbgInfo::runOnFunction(Function &F) {
Torok Edwin6e681062008-12-16 09:09:19 +000077 if (F.isDeclaration())
78 return false;
Bill Wendlinge66b2912009-05-14 18:16:46 +000079
Torok Edwin6e681062008-12-16 09:09:19 +000080 Out << "function " << F.getName() << "\n\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000081
Torok Edwin6e681062008-12-16 09:09:19 +000082 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
83 BasicBlock *BB = I;
Bill Wendlinge66b2912009-05-14 18:16:46 +000084
Torok Edwin6e681062008-12-16 09:09:19 +000085 if (I != F.begin() && (pred_begin(BB) == pred_end(BB)))
86 // Skip dead blocks.
87 continue;
Bill Wendlinge66b2912009-05-14 18:16:46 +000088
Torok Edwin6e681062008-12-16 09:09:19 +000089 Out << BB->getName();
90 Out << ":";
Bill Wendlinge66b2912009-05-14 18:16:46 +000091
Torok Edwin6e681062008-12-16 09:09:19 +000092 Out << "\n";
Bill Wendlinge66b2912009-05-14 18:16:46 +000093
Bill Wendlinge66b2912009-05-14 18:16:46 +000094 for (BasicBlock::const_iterator i = BB->begin(), e = BB->end();
95 i != e; ++i) {
Torok Edwin6e681062008-12-16 09:09:19 +000096
Torok Edwin6e681062008-12-16 09:09:19 +000097 printVariableDeclaration(i);
Bill Wendlinge66b2912009-05-14 18:16:46 +000098
Torok Edwinff7d0e92009-03-10 13:41:26 +000099 if (const User *U = dyn_cast<User>(i)) {
Bill Wendlinge66b2912009-05-14 18:16:46 +0000100 for(unsigned i=0;i<U->getNumOperands();i++)
Torok Edwinff7d0e92009-03-10 13:41:26 +0000101 printVariableDeclaration(U->getOperand(i));
Torok Edwinff7d0e92009-03-10 13:41:26 +0000102 }
Torok Edwin6e681062008-12-16 09:09:19 +0000103 }
104 }
Torok Edwin6e681062008-12-16 09:09:19 +0000105 return false;
106}