xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
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/ADT/Statistic.h"
18 #include "llvm/Analysis/Passes.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/raw_ostream.h"
24 using namespace llvm;
25 
26 namespace {
27   class ModuleDebugInfoPrinter : public ModulePass {
28     DebugInfoFinder Finder;
29   public:
30     static char ID; // Pass identification, replacement for typeid
31     ModuleDebugInfoPrinter() : ModulePass(ID) {
32       initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
33     }
34 
35     bool runOnModule(Module &M) override;
36 
37     void getAnalysisUsage(AnalysisUsage &AU) const override {
38       AU.setPreservesAll();
39     }
40     void print(raw_ostream &O, const Module *M) const override;
41   };
42 }
43 
44 char ModuleDebugInfoPrinter::ID = 0;
45 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
46                 "Decodes module-level debug info", false, true)
47 
48 ModulePass *llvm::createModuleDebugInfoPrinterPass() {
49   return new ModuleDebugInfoPrinter();
50 }
51 
52 bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
53   Finder.processModule(M);
54   return false;
55 }
56 
57 static 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 
70 void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
71   // 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.
74   for (DICompileUnit *CU : Finder.compile_units()) {
75     O << "Compile unit: ";
76     auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
77     if (!Lang.empty())
78       O << Lang;
79     else
80       O << "unknown-language(" << CU->getSourceLanguage() << ")";
81     printFile(O, CU->getFilename(), CU->getDirectory());
82     O << '\n';
83   }
84 
85   for (DISubprogram *S : Finder.subprograms()) {
86     O << "Subprogram: " << S->getName();
87     printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
88     if (!S->getLinkageName().empty())
89       O << " ('" << S->getLinkageName() << "')";
90     O << '\n';
91   }
92 
93   for (auto GVU : Finder.global_variables()) {
94     const auto *GV = GVU->getVariable();
95     O << "Global variable: " << GV->getName();
96     printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
97     if (!GV->getLinkageName().empty())
98       O << " ('" << GV->getLinkageName() << "')";
99     O << '\n';
100   }
101 
102   for (const DIType *T : Finder.types()) {
103     O << "Type:";
104     if (!T->getName().empty())
105       O << ' ' << T->getName();
106     printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
107     if (auto *BT = dyn_cast<DIBasicType>(T)) {
108       O << " ";
109       auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
110       if (!Encoding.empty())
111         O << Encoding;
112       else
113         O << "unknown-encoding(" << BT->getEncoding() << ')';
114     } else {
115       O << ' ';
116       auto Tag = dwarf::TagString(T->getTag());
117       if (!Tag.empty())
118         O << Tag;
119       else
120         O << "unknown-tag(" << T->getTag() << ")";
121     }
122     if (auto *CT = dyn_cast<DICompositeType>(T)) {
123       if (auto *S = CT->getRawIdentifier())
124         O << " (identifier: '" << S->getString() << "')";
125     }
126     O << '\n';
127   }
128 }
129