blob: cbc4700dd1a959a16b609dc43a1c92c58d57ec96 [file] [log] [blame]
Dan Gohmanfb64b5d2010-05-07 16:22:32 +00001//===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
2//
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 pass decodes the debug info metadata in a module and prints in a
11// (sufficiently-prepared-) human-readable form.
12//
13// For example, run this pass from opt along with the -analyze option, and
14// it'll print to standard output.
15//
16//===----------------------------------------------------------------------===//
17
18#include "llvm/Analysis/Passes.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/ADT/Statistic.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000020#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/Function.h"
Bill Wendlinge38859d2012-06-28 00:05:13 +000022#include "llvm/Pass.h"
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000025using namespace llvm;
26
27namespace {
28 class ModuleDebugInfoPrinter : public ModulePass {
29 DebugInfoFinder Finder;
30 public:
31 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000032 ModuleDebugInfoPrinter() : ModulePass(ID) {
33 initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
34 }
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000035
Craig Toppere9ba7592014-03-05 07:30:04 +000036 bool runOnModule(Module &M) override;
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000037
Craig Toppere9ba7592014-03-05 07:30:04 +000038 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000039 AU.setPreservesAll();
40 }
Craig Toppere9ba7592014-03-05 07:30:04 +000041 void print(raw_ostream &O, const Module *M) const override;
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000042 };
43}
44
45char ModuleDebugInfoPrinter::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000046INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
Owen Andersondf7a4f22010-10-07 22:25:06 +000047 "Decodes module-level debug info", false, true)
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000048
49ModulePass *llvm::createModuleDebugInfoPrinterPass() {
50 return new ModuleDebugInfoPrinter();
51}
52
53bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
54 Finder.processModule(M);
55 return false;
56}
57
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000058static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
59 unsigned Line = 0) {
60 if (Filename.empty())
61 return;
62
63 O << " from ";
64 if (!Directory.empty())
65 O << Directory << "/";
66 O << Filename;
67 if (Line)
68 O << ":" << Line;
69}
70
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000071void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000072 // Printing the nodes directly isn't particularly helpful (since they
73 // reference other nodes that won't be printed, particularly for the
74 // filenames), so just print a few useful things.
Alon Mishnead312152014-03-18 09:41:07 +000075 for (DICompileUnit CU : Finder.compile_units()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000076 O << "Compile unit: ";
77 if (const char *Lang = LanguageString(CU.getLanguage()))
78 O << Lang;
79 else
80 O << "unknown-language(" << CU.getLanguage() << ")";
81 printFile(O, CU.getFilename(), CU.getDirectory());
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000082 O << '\n';
83 }
84
Alon Mishnead312152014-03-18 09:41:07 +000085 for (DISubprogram S : Finder.subprograms()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000086 O << "Subprogram: " << S.getName();
87 printFile(O, S.getFilename(), S.getDirectory(), S.getLineNumber());
88 if (!S.getLinkageName().empty())
89 O << " ('" << S.getLinkageName() << "')";
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000090 O << '\n';
91 }
92
Alon Mishnead312152014-03-18 09:41:07 +000093 for (DIGlobalVariable GV : Finder.global_variables()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000094 O << "Global variable: " << GV.getName();
95 printFile(O, GV.getFilename(), GV.getDirectory(), GV.getLineNumber());
96 if (!GV.getLinkageName().empty())
97 O << " ('" << GV.getLinkageName() << "')";
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000098 O << '\n';
99 }
100
Alon Mishnead312152014-03-18 09:41:07 +0000101 for (DIType T : Finder.types()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000102 O << "Type:";
103 if (!T.getName().empty())
104 O << ' ' << T.getName();
105 printFile(O, T.getFilename(), T.getDirectory(), T.getLineNumber());
106 if (T.isBasicType()) {
107 DIBasicType BT(T.get());
108 O << " ";
109 if (const char *Encoding =
110 dwarf::AttributeEncodingString(BT.getEncoding()))
111 O << Encoding;
112 else
113 O << "unknown-encoding(" << BT.getEncoding() << ')';
114 } else {
115 O << ' ';
116 if (const char *Tag = dwarf::TagString(T.getTag()))
117 O << Tag;
118 else
119 O << "unknown-tag(" << T.getTag() << ")";
120 }
121 if (T.isCompositeType()) {
122 DICompositeType CT(T.get());
123 if (auto *S = CT.getIdentifier())
124 O << " (identifier: '" << S->getString() << "')";
125 }
Dan Gohmanfb64b5d2010-05-07 16:22:32 +0000126 O << '\n';
127 }
128}