1 //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass decodes the debug info metadata in a module and prints in a
11 // (sufficiently-prepared-) human-readable form.
13 // For example, run this pass from opt along with the -analyze option, and
14 // it'll print to standard output.
16 //===----------------------------------------------------------------------===//
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"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
28 class ModuleDebugInfoPrinter : public ModulePass {
29 DebugInfoFinder Finder;
31 static char ID; // Pass identification, replacement for typeid
32 ModuleDebugInfoPrinter() : ModulePass(ID) {
33 initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry());
36 bool runOnModule(Module &M) override;
38 void getAnalysisUsage(AnalysisUsage &AU) const override {
41 void print(raw_ostream &O, const Module *M) const override;
45 char ModuleDebugInfoPrinter::ID = 0;
46 INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo",
47 "Decodes module-level debug info", false, true)
49 ModulePass *llvm::createModuleDebugInfoPrinterPass() {
50 return new ModuleDebugInfoPrinter();
53 bool ModuleDebugInfoPrinter::runOnModule(Module &M) {
54 Finder.processModule(M);
58 static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
64 if (!Directory.empty())
65 O << Directory << "/";
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());
81 O << "unknown-language(" << CU->getSourceLanguage() << ")";
82 printFile(O, CU->getFilename(), CU->getDirectory());
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() << "')";
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() << "')";
103 for (const DIType *T : Finder.types()) {
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)) {
110 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
111 if (!Encoding.empty())
114 O << "unknown-encoding(" << BT->getEncoding() << ')';
117 auto Tag = dwarf::TagString(T->getTag());
121 O << "unknown-tag(" << T->getTag() << ")";
123 if (auto *CT = dyn_cast<DICompositeType>(T)) {
124 if (auto *S = CT->getRawIdentifier())
125 O << " (identifier: '" << S->getString() << "')";