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