LLVM  14.0.0git
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 
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"
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
34  ModuleDebugInfoLegacyPrinter() : ModulePass(ID) {
37  }
38 
39  bool runOnModule(Module &M) override;
40 
41  void getAnalysisUsage(AnalysisUsage &AU) const override {
42  AU.setPreservesAll();
43  }
44  void print(raw_ostream &O, const Module *M) const override;
45 };
46 }
47 
49 INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo",
50  "Decodes module-level debug info", false, true)
51 
53  return new ModuleDebugInfoLegacyPrinter();
54 }
55 
56 bool ModuleDebugInfoLegacyPrinter::runOnModule(Module &M) {
57  Finder.processModule(M);
58  return false;
59 }
60 
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 
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 
136  const Module *M) const {
137  printModuleDebugInfo(O, M, Finder);
138 }
139 
141  : OS(OS) {}
142 
144  ModuleAnalysisManager &AM) {
145  Finder.processModule(M);
146  printModuleDebugInfo(OS, &M, Finder);
147  return PreservedAnalyses::all();
148 }
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:155
llvm::dwarf::AttributeEncodingString
StringRef AttributeEncodingString(unsigned Encoding)
Definition: Dwarf.cpp:197
printFile
static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, unsigned Line=0)
Definition: ModuleDebugInfoPrinter.cpp:61
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::DIType
Base class for types.
Definition: DebugInfoMetadata.h:662
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::StringRef::empty
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:153
print
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Definition: ArchiveWriter.cpp:147
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:238
Pass.h
Statistic.h
ErrorHandling.h
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::DebugInfoFinder::compile_units
iterator_range< compile_unit_iterator > compile_units() const
Definition: DebugInfo.h:127
llvm::DebugInfoFinder
Utility to find all debug info in a module.
Definition: DebugInfo.h:90
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
llvm::dwarf::LanguageString
StringRef LanguageString(unsigned Language)
Definition: Dwarf.cpp:327
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::BitTracker
Definition: BitTracker.h:35
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
printModuleDebugInfo
static void printModuleDebugInfo(raw_ostream &O, const Module *M, const DebugInfoFinder &Finder)
Definition: ModuleDebugInfoPrinter.cpp:74
llvm::initializeModuleDebugInfoLegacyPrinterPass
void initializeModuleDebugInfoLegacyPrinterPass(PassRegistry &)
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
llvm::createModuleDebugInfoPrinterPass
ModulePass * createModuleDebugInfoPrinterPass()
DebugInfo.h
llvm::DICompileUnit
Compile unit.
Definition: DebugInfoMetadata.h:1335
llvm::DebugInfoFinder::processModule
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:148
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
ModuleDebugInfoPrinter.h
llvm::DebugInfoFinder::global_variables
iterator_range< global_variable_expression_iterator > global_variables() const
Definition: DebugInfo.h:135
llvm::DebugInfoFinder::subprograms
iterator_range< subprogram_iterator > subprograms() const
Definition: DebugInfo.h:131
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
INITIALIZE_PASS
INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo", "Decodes module-level debug info", false, true) ModulePass *llvm
Definition: ModuleDebugInfoPrinter.cpp:49
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:161
PassManager.h
llvm::ModuleDebugInfoPrinterPass::run
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Definition: ModuleDebugInfoPrinter.cpp:143
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1820
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
llvm::dwarf::TagString
StringRef TagString(unsigned Tag)
Definition: Dwarf.cpp:21
raw_ostream.h
llvm::DebugInfoFinder::types
iterator_range< type_iterator > types() const
Definition: DebugInfo.h:139
CU
Definition: AArch64AsmBackend.cpp:501
InitializePasses.h
llvm::ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass
ModuleDebugInfoPrinterPass(raw_ostream &OS)
Definition: ModuleDebugInfoPrinter.cpp:140
Passes.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37