LLVM  8.0.1
ModuleDebugInfoPrinter.cpp
Go to the documentation of this file.
1 //===-- 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/ADT/Statistic.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/Pass.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) {
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 
45 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
46  "Decodes module-level debug info", false, true)
47 
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 }
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:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:63
Utility to find all debug info in a module.
Definition: DebugInfo.h:65
static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, unsigned Line=0)
Subprogram description.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
StringRef LanguageString(unsigned Language)
Definition: Dwarf.cpp:300
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:116
INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo", "Decodes module-level debug info", false, true) ModulePass *llvm
void initializeModuleDebugInfoPrinterPass(PassRegistry &)
StringRef AttributeEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:183
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:104
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:112
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:22
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:225
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:108
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49