blob: 36c47141a45ff9826aef6eaa80e3ef4c67e86ffb [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.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000075 for (DICompileUnit *CU : Finder.compile_units()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000076 O << "Compile unit: ";
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +000077 if (const char *Lang = dwarf::LanguageString(CU->getSourceLanguage()))
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000078 O << Lang;
79 else
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +000080 O << "unknown-language(" << CU->getSourceLanguage() << ")";
81 printFile(O, CU->getFilename(), CU->getDirectory());
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000082 O << '\n';
83 }
84
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000085 for (DISubprogram *S : Finder.subprograms()) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +000086 O << "Subprogram: " << S->getName();
87 printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
88 if (!S->getLinkageName().empty())
89 O << " ('" << S->getLinkageName() << "')";
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000090 O << '\n';
91 }
92
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093 for (const DIGlobalVariable *GV : Finder.global_variables()) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000094 O << "Global variable: " << GV->getName();
95 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
96 if (!GV->getLinkageName().empty())
97 O << " ('" << GV->getLinkageName() << "')";
Dan Gohmanfb64b5d2010-05-07 16:22:32 +000098 O << '\n';
99 }
100
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000101 for (const DIType *T : Finder.types()) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000102 O << "Type:";
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000103 if (!T->getName().empty())
104 O << ' ' << T->getName();
105 printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000106 if (auto *BT = dyn_cast<DIBasicType>(T)) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000107 O << " ";
108 if (const char *Encoding =
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000109 dwarf::AttributeEncodingString(BT->getEncoding()))
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000110 O << Encoding;
111 else
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000112 O << "unknown-encoding(" << BT->getEncoding() << ')';
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000113 } else {
114 O << ' ';
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000115 if (const char *Tag = dwarf::TagString(T->getTag()))
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000116 O << Tag;
117 else
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000118 O << "unknown-tag(" << T->getTag() << ")";
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000119 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000120 if (auto *CT = dyn_cast<DICompositeType>(T)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000121 if (auto *S = CT->getRawIdentifier())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000122 O << " (identifier: '" << S->getString() << "')";
123 }
Dan Gohmanfb64b5d2010-05-07 16:22:32 +0000124 O << '\n';
125 }
126}