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