• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Analysis/Passes.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.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
ModuleDebugInfoPrinter()32     ModuleDebugInfoPrinter() : ModulePass(ID) {
33       initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
34     }
35 
36     bool runOnModule(Module &M) override;
37 
getAnalysisUsage(AnalysisUsage & AU) const38     void getAnalysisUsage(AnalysisUsage &AU) const override {
39       AU.setPreservesAll();
40     }
41     void print(raw_ostream &O, const Module *M) const override;
42   };
43 }
44 
45 char ModuleDebugInfoPrinter::ID = 0;
46 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
47                 "Decodes module-level debug info", false, true)
48 
createModuleDebugInfoPrinterPass()49 ModulePass *llvm::createModuleDebugInfoPrinterPass() {
50   return new ModuleDebugInfoPrinter();
51 }
52 
runOnModule(Module & M)53 bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
54   Finder.processModule(M);
55   return false;
56 }
57 
print(raw_ostream & O,const Module * M) const58 void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
59   for (DICompileUnit CU : Finder.compile_units()) {
60     O << "Compile Unit: ";
61     CU.print(O);
62     O << '\n';
63   }
64 
65   for (DISubprogram S : Finder.subprograms()) {
66     O << "Subprogram: ";
67     S.print(O);
68     O << '\n';
69   }
70 
71   for (DIGlobalVariable GV : Finder.global_variables()) {
72     O << "GlobalVariable: ";
73     GV.print(O);
74     O << '\n';
75   }
76 
77   for (DIType T : Finder.types()) {
78     O << "Type: ";
79     T.print(O);
80     O << '\n';
81   }
82 }
83