LLVM  9.0.0svn
ModuleDebugInfoPrinter.cpp
Go to the documentation of this file.
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/Pass.h"
23 using namespace llvm;
24 
25 namespace {
26  class ModuleDebugInfoPrinter : public ModulePass {
27  DebugInfoFinder Finder;
28  public:
29  static char ID; // Pass identification, replacement for typeid
30  ModuleDebugInfoPrinter() : ModulePass(ID) {
32  }
33 
34  bool runOnModule(Module &M) override;
35 
36  void getAnalysisUsage(AnalysisUsage &AU) const override {
37  AU.setPreservesAll();
38  }
39  void print(raw_ostream &O, const Module *M) const override;
40  };
41 }
42 
44 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
45  "Decodes module-level debug info", false, true)
46 
48  return new ModuleDebugInfoPrinter();
49 }
50 
51 bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
52  Finder.processModule(M);
53  return false;
54 }
55 
56 static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
57  unsigned Line = 0) {
58  if (Filename.empty())
59  return;
60 
61  O << " from ";
62  if (!Directory.empty())
63  O << Directory << "/";
64  O << Filename;
65  if (Line)
66  O << ":" << Line;
67 }
68 
69 void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
70  // Printing the nodes directly isn't particularly helpful (since they
71  // reference other nodes that won't be printed, particularly for the
72  // filenames), so just print a few useful things.
73  for (DICompileUnit *CU : Finder.compile_units()) {
74  O << "Compile unit: ";
75  auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
76  if (!Lang.empty())
77  O << Lang;
78  else
79  O << "unknown-language(" << CU->getSourceLanguage() << ")";
80  printFile(O, CU->getFilename(), CU->getDirectory());
81  O << '\n';
82  }
83 
84  for (DISubprogram *S : Finder.subprograms()) {
85  O << "Subprogram: " << S->getName();
86  printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
87  if (!S->getLinkageName().empty())
88  O << " ('" << S->getLinkageName() << "')";
89  O << '\n';
90  }
91 
92  for (auto GVU : Finder.global_variables()) {
93  const auto *GV = GVU->getVariable();
94  O << "Global variable: " << GV->getName();
95  printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
96  if (!GV->getLinkageName().empty())
97  O << " ('" << GV->getLinkageName() << "')";
98  O << '\n';
99  }
100 
101  for (const DIType *T : Finder.types()) {
102  O << "Type:";
103  if (!T->getName().empty())
104  O << ' ' << T->getName();
105  printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
106  if (auto *BT = dyn_cast<DIBasicType>(T)) {
107  O << " ";
108  auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
109  if (!Encoding.empty())
110  O << Encoding;
111  else
112  O << "unknown-encoding(" << BT->getEncoding() << ')';
113  } else {
114  O << ' ';
115  auto Tag = dwarf::TagString(T->getTag());
116  if (!Tag.empty())
117  O << Tag;
118  else
119  O << "unknown-tag(" << T->getTag() << ")";
120  }
121  if (auto *CT = dyn_cast<DICompositeType>(T)) {
122  if (auto *S = CT->getRawIdentifier())
123  O << " (identifier: '" << S->getString() << "')";
124  }
125  O << '\n';
126  }
127 }
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:62
Utility to find all debug info in a module.
Definition: DebugInfo.h:64
static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, unsigned Line=0)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:126
Subprogram description.
StringRef LanguageString(unsigned Language)
Definition: Dwarf.cpp:299
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:115
INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo", "Decodes module-level debug info", false, true) ModulePass *llvm
void initializeModuleDebugInfoPrinterPass(PassRegistry &)
StringRef AttributeEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:182
Represent the analysis usage information of a pass.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
iterator_range< compile_unit_iterator > compile_units() const
Definition: DebugInfo.h:103
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:111
Base class for types.
void setPreservesAll()
Set by analyses that do not transform their input at all.
ModulePass * createModuleDebugInfoPrinterPass()
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:224
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:107
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48