annotate llvm/lib/Analysis/ModuleDebugInfoPrinter.cpp @ 220:42394fc6a535

Added tag llvm12 for changeset 0572611fdcc8
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 15 Jun 2021 19:13:43 +0900
parents 1d019706d866
children 2e18cbf3894f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This pass decodes the debug info metadata in a module and prints in a
anatofuz
parents:
diff changeset
10 // (sufficiently-prepared-) human-readable form.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 // For example, run this pass from opt along with the -analyze option, and
anatofuz
parents:
diff changeset
13 // it'll print to standard output.
anatofuz
parents:
diff changeset
14 //
anatofuz
parents:
diff changeset
15 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
16
anatofuz
parents:
diff changeset
17 #include "llvm/ADT/Statistic.h"
anatofuz
parents:
diff changeset
18 #include "llvm/Analysis/Passes.h"
anatofuz
parents:
diff changeset
19 #include "llvm/IR/DebugInfo.h"
anatofuz
parents:
diff changeset
20 #include "llvm/InitializePasses.h"
anatofuz
parents:
diff changeset
21 #include "llvm/Pass.h"
anatofuz
parents:
diff changeset
22 #include "llvm/Support/ErrorHandling.h"
anatofuz
parents:
diff changeset
23 #include "llvm/Support/raw_ostream.h"
anatofuz
parents:
diff changeset
24 using namespace llvm;
anatofuz
parents:
diff changeset
25
anatofuz
parents:
diff changeset
26 namespace {
anatofuz
parents:
diff changeset
27 class ModuleDebugInfoPrinter : public ModulePass {
anatofuz
parents:
diff changeset
28 DebugInfoFinder Finder;
anatofuz
parents:
diff changeset
29 public:
anatofuz
parents:
diff changeset
30 static char ID; // Pass identification, replacement for typeid
anatofuz
parents:
diff changeset
31 ModuleDebugInfoPrinter() : ModulePass(ID) {
anatofuz
parents:
diff changeset
32 initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
anatofuz
parents:
diff changeset
33 }
anatofuz
parents:
diff changeset
34
anatofuz
parents:
diff changeset
35 bool runOnModule(Module &M) override;
anatofuz
parents:
diff changeset
36
anatofuz
parents:
diff changeset
37 void getAnalysisUsage(AnalysisUsage &AU) const override {
anatofuz
parents:
diff changeset
38 AU.setPreservesAll();
anatofuz
parents:
diff changeset
39 }
anatofuz
parents:
diff changeset
40 void print(raw_ostream &O, const Module *M) const override;
anatofuz
parents:
diff changeset
41 };
anatofuz
parents:
diff changeset
42 }
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 char ModuleDebugInfoPrinter::ID = 0;
anatofuz
parents:
diff changeset
45 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
anatofuz
parents:
diff changeset
46 "Decodes module-level debug info", false, true)
anatofuz
parents:
diff changeset
47
anatofuz
parents:
diff changeset
48 ModulePass *llvm::createModuleDebugInfoPrinterPass() {
anatofuz
parents:
diff changeset
49 return new ModuleDebugInfoPrinter();
anatofuz
parents:
diff changeset
50 }
anatofuz
parents:
diff changeset
51
anatofuz
parents:
diff changeset
52 bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
anatofuz
parents:
diff changeset
53 Finder.processModule(M);
anatofuz
parents:
diff changeset
54 return false;
anatofuz
parents:
diff changeset
55 }
anatofuz
parents:
diff changeset
56
anatofuz
parents:
diff changeset
57 static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
anatofuz
parents:
diff changeset
58 unsigned Line = 0) {
anatofuz
parents:
diff changeset
59 if (Filename.empty())
anatofuz
parents:
diff changeset
60 return;
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 O << " from ";
anatofuz
parents:
diff changeset
63 if (!Directory.empty())
anatofuz
parents:
diff changeset
64 O << Directory << "/";
anatofuz
parents:
diff changeset
65 O << Filename;
anatofuz
parents:
diff changeset
66 if (Line)
anatofuz
parents:
diff changeset
67 O << ":" << Line;
anatofuz
parents:
diff changeset
68 }
anatofuz
parents:
diff changeset
69
anatofuz
parents:
diff changeset
70 void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
anatofuz
parents:
diff changeset
71 // Printing the nodes directly isn't particularly helpful (since they
anatofuz
parents:
diff changeset
72 // reference other nodes that won't be printed, particularly for the
anatofuz
parents:
diff changeset
73 // filenames), so just print a few useful things.
anatofuz
parents:
diff changeset
74 for (DICompileUnit *CU : Finder.compile_units()) {
anatofuz
parents:
diff changeset
75 O << "Compile unit: ";
anatofuz
parents:
diff changeset
76 auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
anatofuz
parents:
diff changeset
77 if (!Lang.empty())
anatofuz
parents:
diff changeset
78 O << Lang;
anatofuz
parents:
diff changeset
79 else
anatofuz
parents:
diff changeset
80 O << "unknown-language(" << CU->getSourceLanguage() << ")";
anatofuz
parents:
diff changeset
81 printFile(O, CU->getFilename(), CU->getDirectory());
anatofuz
parents:
diff changeset
82 O << '\n';
anatofuz
parents:
diff changeset
83 }
anatofuz
parents:
diff changeset
84
anatofuz
parents:
diff changeset
85 for (DISubprogram *S : Finder.subprograms()) {
anatofuz
parents:
diff changeset
86 O << "Subprogram: " << S->getName();
anatofuz
parents:
diff changeset
87 printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
anatofuz
parents:
diff changeset
88 if (!S->getLinkageName().empty())
anatofuz
parents:
diff changeset
89 O << " ('" << S->getLinkageName() << "')";
anatofuz
parents:
diff changeset
90 O << '\n';
anatofuz
parents:
diff changeset
91 }
anatofuz
parents:
diff changeset
92
anatofuz
parents:
diff changeset
93 for (auto GVU : Finder.global_variables()) {
anatofuz
parents:
diff changeset
94 const auto *GV = GVU->getVariable();
anatofuz
parents:
diff changeset
95 O << "Global variable: " << GV->getName();
anatofuz
parents:
diff changeset
96 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
anatofuz
parents:
diff changeset
97 if (!GV->getLinkageName().empty())
anatofuz
parents:
diff changeset
98 O << " ('" << GV->getLinkageName() << "')";
anatofuz
parents:
diff changeset
99 O << '\n';
anatofuz
parents:
diff changeset
100 }
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 for (const DIType *T : Finder.types()) {
anatofuz
parents:
diff changeset
103 O << "Type:";
anatofuz
parents:
diff changeset
104 if (!T->getName().empty())
anatofuz
parents:
diff changeset
105 O << ' ' << T->getName();
anatofuz
parents:
diff changeset
106 printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
anatofuz
parents:
diff changeset
107 if (auto *BT = dyn_cast<DIBasicType>(T)) {
anatofuz
parents:
diff changeset
108 O << " ";
anatofuz
parents:
diff changeset
109 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
anatofuz
parents:
diff changeset
110 if (!Encoding.empty())
anatofuz
parents:
diff changeset
111 O << Encoding;
anatofuz
parents:
diff changeset
112 else
anatofuz
parents:
diff changeset
113 O << "unknown-encoding(" << BT->getEncoding() << ')';
anatofuz
parents:
diff changeset
114 } else {
anatofuz
parents:
diff changeset
115 O << ' ';
anatofuz
parents:
diff changeset
116 auto Tag = dwarf::TagString(T->getTag());
anatofuz
parents:
diff changeset
117 if (!Tag.empty())
anatofuz
parents:
diff changeset
118 O << Tag;
anatofuz
parents:
diff changeset
119 else
anatofuz
parents:
diff changeset
120 O << "unknown-tag(" << T->getTag() << ")";
anatofuz
parents:
diff changeset
121 }
anatofuz
parents:
diff changeset
122 if (auto *CT = dyn_cast<DICompositeType>(T)) {
anatofuz
parents:
diff changeset
123 if (auto *S = CT->getRawIdentifier())
anatofuz
parents:
diff changeset
124 O << " (identifier: '" << S->getString() << "')";
anatofuz
parents:
diff changeset
125 }
anatofuz
parents:
diff changeset
126 O << '\n';
anatofuz
parents:
diff changeset
127 }
anatofuz
parents:
diff changeset
128 }