blob: 1e321f17d59fa32d139db380a4fdb33e2162377e [file] [log] [blame]
Dan Gohmanef0b1452010-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
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/ADT/Statistic.h"
Chandler Carruthe3e43d92017-06-06 11:49:48 +000019#include "llvm/Analysis/Passes.h"
Chandler Carruthf4ec8bf2014-03-06 00:46:21 +000020#include "llvm/IR/DebugInfo.h"
Bill Wendling0bcbd1d2012-06-28 00:05:13 +000021#include "llvm/Pass.h"
Dan Gohmanef0b1452010-05-07 16:22:32 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Dan Gohmanef0b1452010-05-07 16:22:32 +000024using namespace llvm;
25
26namespace {
27 class ModuleDebugInfoPrinter : public ModulePass {
28 DebugInfoFinder Finder;
29 public:
30 static char ID; // Pass identification, replacement for typeid
Owen Anderson081c34b2010-10-19 17:21:58 +000031 ModuleDebugInfoPrinter() : ModulePass(ID) {
32 initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
33 }
Dan Gohmanef0b1452010-05-07 16:22:32 +000034
Craig Topperc37e6c02014-03-05 07:30:04 +000035 bool runOnModule(Module &M) override;
Dan Gohmanef0b1452010-05-07 16:22:32 +000036
Craig Topperc37e6c02014-03-05 07:30:04 +000037 void getAnalysisUsage(AnalysisUsage &AU) const override {
Dan Gohmanef0b1452010-05-07 16:22:32 +000038 AU.setPreservesAll();
39 }
Craig Topperc37e6c02014-03-05 07:30:04 +000040 void print(raw_ostream &O, const Module *M) const override;
Dan Gohmanef0b1452010-05-07 16:22:32 +000041 };
Alexander Kornienkocd52a7a2015-06-23 09:49:53 +000042}
Dan Gohmanef0b1452010-05-07 16:22:32 +000043
44char ModuleDebugInfoPrinter::ID = 0;
Owen Andersond13db2c2010-07-21 22:09:45 +000045INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
Owen Andersonce665bd2010-10-07 22:25:06 +000046 "Decodes module-level debug info", false, true)
Dan Gohmanef0b1452010-05-07 16:22:32 +000047
48ModulePass *llvm::createModuleDebugInfoPrinterPass() {
49 return new ModuleDebugInfoPrinter();
50}
51
52bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
53 Finder.processModule(M);
54 return false;
55}
56
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +000057static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
58 unsigned Line = 0) {
59 if (Filename.empty())
60 return;
61
62 O << " from ";
63 if (!Directory.empty())
64 O << Directory << "/";
65 O << Filename;
66 if (Line)
67 O << ":" << Line;
68}
69
Dan Gohmanef0b1452010-05-07 16:22:32 +000070void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +000071 // Printing the nodes directly isn't particularly helpful (since they
72 // reference other nodes that won't be printed, particularly for the
73 // filenames), so just print a few useful things.
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000074 for (DICompileUnit *CU : Finder.compile_units()) {
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +000075 O << "Compile unit: ";
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +000076 auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
77 if (!Lang.empty())
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +000078 O << Lang;
79 else
Duncan P. N. Exon Smithed0e1172015-04-15 23:19:27 +000080 O << "unknown-language(" << CU->getSourceLanguage() << ")";
81 printFile(O, CU->getFilename(), CU->getDirectory());
Dan Gohmanef0b1452010-05-07 16:22:32 +000082 O << '\n';
83 }
84
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +000085 for (DISubprogram *S : Finder.subprograms()) {
Duncan P. N. Exon Smith125e3d32015-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 Gohmanef0b1452010-05-07 16:22:32 +000090 O << '\n';
91 }
92
Adrian Prantl7b500b42016-12-20 02:09:43 +000093 for (auto GVU : Finder.global_variables()) {
94 const auto *GV = GVU->getVariable();
Duncan P. N. Exon Smith355ec002015-04-14 02:22:36 +000095 O << "Global variable: " << GV->getName();
96 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
97 if (!GV->getLinkageName().empty())
98 O << " ('" << GV->getLinkageName() << "')";
Dan Gohmanef0b1452010-05-07 16:22:32 +000099 O << '\n';
100 }
101
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000102 for (const DIType *T : Finder.types()) {
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000103 O << "Type:";
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +0000104 if (!T->getName().empty())
105 O << ' ' << T->getName();
106 printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000107 if (auto *BT = dyn_cast<DIBasicType>(T)) {
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000108 O << " ";
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +0000109 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
110 if (!Encoding.empty())
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000111 O << Encoding;
112 else
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +0000113 O << "unknown-encoding(" << BT->getEncoding() << ')';
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000114 } else {
115 O << ' ';
Mehdi Aminif9bb6bc2016-10-05 05:59:29 +0000116 auto Tag = dwarf::TagString(T->getTag());
117 if (!Tag.empty())
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000118 O << Tag;
119 else
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +0000120 O << "unknown-tag(" << T->getTag() << ")";
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000121 }
Duncan P. N. Exon Smithe56023a2015-04-29 16:38:44 +0000122 if (auto *CT = dyn_cast<DICompositeType>(T)) {
Duncan P. N. Exon Smith7f76d292015-04-16 01:01:28 +0000123 if (auto *S = CT->getRawIdentifier())
Duncan P. N. Exon Smithb056aa72015-03-03 17:24:31 +0000124 O << " (identifier: '" << S->getString() << "')";
125 }
Dan Gohmanef0b1452010-05-07 16:22:32 +0000126 O << '\n';
127 }
128}