1 //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass decodes the debug info metadata in a module and prints in a
10 // (sufficiently-prepared-) human-readable form.
11 //
12 // For example, run this pass from opt along with the -analyze option, and
13 // it'll print to standard output.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/ModuleDebugInfoPrinter.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/PassManager.h"
22 #include "llvm/InitializePasses.h"
23 #include "llvm/Pass.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace llvm;
27
28 namespace {
29 class ModuleDebugInfoLegacyPrinter : public ModulePass {
30 DebugInfoFinder Finder;
31
32 public:
33 static char ID; // Pass identification, replacement for typeid
ModuleDebugInfoLegacyPrinter()34 ModuleDebugInfoLegacyPrinter() : ModulePass(ID) {
35 initializeModuleDebugInfoLegacyPrinterPass(
36 *PassRegistry::getPassRegistry());
37 }
38
39 bool runOnModule(Module &M) override;
40
getAnalysisUsage(AnalysisUsage & AU) const41 void getAnalysisUsage(AnalysisUsage &AU) const override {
42 AU.setPreservesAll();
43 }
44 void print(raw_ostream &O, const Module *M) const override;
45 };
46 }
47
48 char ModuleDebugInfoLegacyPrinter::ID = 0;
49 INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo",
50 "Decodes module-level debug info", false, true)
51
createModuleDebugInfoPrinterPass()52 ModulePass *llvm::createModuleDebugInfoPrinterPass() {
53 return new ModuleDebugInfoLegacyPrinter();
54 }
55
runOnModule(Module & M)56 bool ModuleDebugInfoLegacyPrinter::runOnModule(Module &M) {
57 Finder.processModule(M);
58 return false;
59 }
60
printFile(raw_ostream & O,StringRef Filename,StringRef Directory,unsigned Line=0)61 static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
62 unsigned Line = 0) {
63 if (Filename.empty())
64 return;
65
66 O << " from ";
67 if (!Directory.empty())
68 O << Directory << "/";
69 O << Filename;
70 if (Line)
71 O << ":" << Line;
72 }
73
printModuleDebugInfo(raw_ostream & O,const Module * M,const DebugInfoFinder & Finder)74 static void printModuleDebugInfo(raw_ostream &O, const Module *M,
75 const DebugInfoFinder &Finder) {
76 // Printing the nodes directly isn't particularly helpful (since they
77 // reference other nodes that won't be printed, particularly for the
78 // filenames), so just print a few useful things.
79 for (DICompileUnit *CU : Finder.compile_units()) {
80 O << "Compile unit: ";
81 auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
82 if (!Lang.empty())
83 O << Lang;
84 else
85 O << "unknown-language(" << CU->getSourceLanguage() << ")";
86 printFile(O, CU->getFilename(), CU->getDirectory());
87 O << '\n';
88 }
89
90 for (DISubprogram *S : Finder.subprograms()) {
91 O << "Subprogram: " << S->getName();
92 printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
93 if (!S->getLinkageName().empty())
94 O << " ('" << S->getLinkageName() << "')";
95 O << '\n';
96 }
97
98 for (auto GVU : Finder.global_variables()) {
99 const auto *GV = GVU->getVariable();
100 O << "Global variable: " << GV->getName();
101 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
102 if (!GV->getLinkageName().empty())
103 O << " ('" << GV->getLinkageName() << "')";
104 O << '\n';
105 }
106
107 for (const DIType *T : Finder.types()) {
108 O << "Type:";
109 if (!T->getName().empty())
110 O << ' ' << T->getName();
111 printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
112 if (auto *BT = dyn_cast<DIBasicType>(T)) {
113 O << " ";
114 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
115 if (!Encoding.empty())
116 O << Encoding;
117 else
118 O << "unknown-encoding(" << BT->getEncoding() << ')';
119 } else {
120 O << ' ';
121 auto Tag = dwarf::TagString(T->getTag());
122 if (!Tag.empty())
123 O << Tag;
124 else
125 O << "unknown-tag(" << T->getTag() << ")";
126 }
127 if (auto *CT = dyn_cast<DICompositeType>(T)) {
128 if (auto *S = CT->getRawIdentifier())
129 O << " (identifier: '" << S->getString() << "')";
130 }
131 O << '\n';
132 }
133 }
134
print(raw_ostream & O,const Module * M) const135 void ModuleDebugInfoLegacyPrinter::print(raw_ostream &O,
136 const Module *M) const {
137 printModuleDebugInfo(O, M, Finder);
138 }
139
ModuleDebugInfoPrinterPass(raw_ostream & OS)140 ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS)
141 : OS(OS) {}
142
run(Module & M,ModuleAnalysisManager & AM)143 PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M,
144 ModuleAnalysisManager &AM) {
145 Finder.processModule(M);
146 printModuleDebugInfo(OS, &M, Finder);
147 return PreservedAnalyses::all();
148 }
149