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 17 #include "llvm/Analysis/ModuleDebugInfoPrinter.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/Analysis/Passes.h" 20 #include "llvm/BinaryFormat/Dwarf.h" 21 #include "llvm/IR/DebugInfo.h" 22 #include "llvm/IR/PassManager.h" 23 #include "llvm/InitializePasses.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Support/ErrorHandling.h" 26 #include "llvm/Support/raw_ostream.h" 27 using namespace llvm; 28 29 namespace { 30 class ModuleDebugInfoLegacyPrinter : public ModulePass { 31 DebugInfoFinder Finder; 32 33 public: 34 static char ID; // Pass identification, replacement for typeid 35 ModuleDebugInfoLegacyPrinter() : ModulePass(ID) { 36 initializeModuleDebugInfoLegacyPrinterPass( 37 *PassRegistry::getPassRegistry()); 38 } 39 40 bool runOnModule(Module &M) override; 41 42 void getAnalysisUsage(AnalysisUsage &AU) const override { 43 AU.setPreservesAll(); 44 } 45 void print(raw_ostream &O, const Module *M) const override; 46 }; 47 } 48 49 char ModuleDebugInfoLegacyPrinter::ID = 0; 50 INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo", 51 "Decodes module-level debug info", false, true) 52 53 ModulePass *llvm::createModuleDebugInfoPrinterPass() { 54 return new ModuleDebugInfoLegacyPrinter(); 55 } 56 57 bool ModuleDebugInfoLegacyPrinter::runOnModule(Module &M) { 58 Finder.processModule(M); 59 return false; 60 } 61 62 static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, 63 unsigned Line = 0) { 64 if (Filename.empty()) 65 return; 66 67 O << " from "; 68 if (!Directory.empty()) 69 O << Directory << "/"; 70 O << Filename; 71 if (Line) 72 O << ":" << Line; 73 } 74 75 static void printModuleDebugInfo(raw_ostream &O, const Module *M, 76 const DebugInfoFinder &Finder) { 77 // Printing the nodes directly isn't particularly helpful (since they 78 // reference other nodes that won't be printed, particularly for the 79 // filenames), so just print a few useful things. 80 for (DICompileUnit *CU : Finder.compile_units()) { 81 O << "Compile unit: "; 82 auto Lang = dwarf::LanguageString(CU->getSourceLanguage()); 83 if (!Lang.empty()) 84 O << Lang; 85 else 86 O << "unknown-language(" << CU->getSourceLanguage() << ")"; 87 printFile(O, CU->getFilename(), CU->getDirectory()); 88 O << '\n'; 89 } 90 91 for (DISubprogram *S : Finder.subprograms()) { 92 O << "Subprogram: " << S->getName(); 93 printFile(O, S->getFilename(), S->getDirectory(), S->getLine()); 94 if (!S->getLinkageName().empty()) 95 O << " ('" << S->getLinkageName() << "')"; 96 O << '\n'; 97 } 98 99 for (auto GVU : Finder.global_variables()) { 100 const auto *GV = GVU->getVariable(); 101 O << "Global variable: " << GV->getName(); 102 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine()); 103 if (!GV->getLinkageName().empty()) 104 O << " ('" << GV->getLinkageName() << "')"; 105 O << '\n'; 106 } 107 108 for (const DIType *T : Finder.types()) { 109 O << "Type:"; 110 if (!T->getName().empty()) 111 O << ' ' << T->getName(); 112 printFile(O, T->getFilename(), T->getDirectory(), T->getLine()); 113 if (auto *BT = dyn_cast<DIBasicType>(T)) { 114 O << " "; 115 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding()); 116 if (!Encoding.empty()) 117 O << Encoding; 118 else 119 O << "unknown-encoding(" << BT->getEncoding() << ')'; 120 } else { 121 O << ' '; 122 auto Tag = dwarf::TagString(T->getTag()); 123 if (!Tag.empty()) 124 O << Tag; 125 else 126 O << "unknown-tag(" << T->getTag() << ")"; 127 } 128 if (auto *CT = dyn_cast<DICompositeType>(T)) { 129 if (auto *S = CT->getRawIdentifier()) 130 O << " (identifier: '" << S->getString() << "')"; 131 } 132 O << '\n'; 133 } 134 } 135 136 void ModuleDebugInfoLegacyPrinter::print(raw_ostream &O, 137 const Module *M) const { 138 printModuleDebugInfo(O, M, Finder); 139 } 140 141 ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS) 142 : OS(OS) {} 143 144 PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M, 145 ModuleAnalysisManager &AM) { 146 Finder.processModule(M); 147 printModuleDebugInfo(OS, &M, Finder); 148 return PreservedAnalyses::all(); 149 } 150