1ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
2ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
3ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//                     The LLVM Compiler Infrastructure
4ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
5ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// This file is distributed under the University of Illinois Open Source
6ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// License. See LICENSE.TXT for details.
7ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
8ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//===----------------------------------------------------------------------===//
9ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
10ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// This pass decodes the debug info metadata in a module and prints in a
11ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// (sufficiently-prepared-) human-readable form.
12ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
13ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// For example, run this pass from opt along with the -analyze option, and
14ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman// it'll print to standard output.
15ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//
16ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman//===----------------------------------------------------------------------===//
17ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
18ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman#include "llvm/Analysis/Passes.h"
19d04a8d4b33ff316ca4cf961e06c9e312eff8e64fChandler Carruth#include "llvm/ADT/Statistic.h"
2036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/DebugInfo.h"
210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Function.h"
220bcbd1df7a204e1e512f1a27066d725309de1b13Bill Wendling#include "llvm/Pass.h"
23ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman#include "llvm/Support/ErrorHandling.h"
24ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman#include "llvm/Support/raw_ostream.h"
25ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohmanusing namespace llvm;
26ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
27ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohmannamespace {
28ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  class ModuleDebugInfoPrinter : public ModulePass {
29ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    DebugInfoFinder Finder;
30ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  public:
31ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    static char ID; // Pass identification, replacement for typeid
32081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    ModuleDebugInfoPrinter() : ModulePass(ID) {
33081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson      initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
34081c34b725980f995be9080eaec24cd3dfaaf065Owen Anderson    }
35ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
3636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    bool runOnModule(Module &M) override;
37ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
3836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    void getAnalysisUsage(AnalysisUsage &AU) const override {
39ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman      AU.setPreservesAll();
40ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    }
4136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    void print(raw_ostream &O, const Module *M) const override;
42ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  };
43ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman}
44ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
45ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohmanchar ModuleDebugInfoPrinter::ID = 0;
46d13db2c59cc94162d6cf0a04187d408bfef6d4a7Owen AndersonINITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
47ce665bd2e2b581ab0858d1afe359192bac96b868Owen Anderson                "Decodes module-level debug info", false, true)
48ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
49ef0b14593bb8dd5651606925584adb1ac1096ba5Dan GohmanModulePass *llvm::createModuleDebugInfoPrinterPass() {
50ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  return new ModuleDebugInfoPrinter();
51ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman}
52ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
53ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohmanbool ModuleDebugInfoPrinter::runOnModule(Module &M) {
54ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  Finder.processModule(M);
55ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  return false;
56ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman}
57ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
58ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohmanvoid ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const {
5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (DICompileUnit CU : Finder.compile_units()) {
60ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << "Compile Unit: ";
6136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    CU.print(O);
62ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << '\n';
63ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  }
64ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
6536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (DISubprogram S : Finder.subprograms()) {
66ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << "Subprogram: ";
6736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    S.print(O);
68ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << '\n';
69ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  }
70ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
7136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (DIGlobalVariable GV : Finder.global_variables()) {
72ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << "GlobalVariable: ";
7336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    GV.print(O);
74ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << '\n';
75ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  }
76ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman
7736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines  for (DIType T : Finder.types()) {
78ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << "Type: ";
7936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    T.print(O);
80ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman    O << '\n';
81ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman  }
82ef0b14593bb8dd5651606925584adb1ac1096ba5Dan Gohman}
83