10b57cec5SDimitry Andric //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass decodes the debug info metadata in a module and prints in a 100b57cec5SDimitry Andric // (sufficiently-prepared-) human-readable form. 110b57cec5SDimitry Andric // 120b57cec5SDimitry Andric // For example, run this pass from opt along with the -analyze option, and 130b57cec5SDimitry Andric // it'll print to standard output. 140b57cec5SDimitry Andric // 150b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 160b57cec5SDimitry Andric 17e8d8bef9SDimitry Andric #include "llvm/Analysis/ModuleDebugInfoPrinter.h" 180b57cec5SDimitry Andric #include "llvm/Analysis/Passes.h" 19*81ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h" 200b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h" 21e8d8bef9SDimitry Andric #include "llvm/IR/PassManager.h" 22480093f4SDimitry Andric #include "llvm/InitializePasses.h" 230b57cec5SDimitry Andric #include "llvm/Pass.h" 240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 250b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 260b57cec5SDimitry Andric using namespace llvm; 270b57cec5SDimitry Andric 280b57cec5SDimitry Andric namespace { 29e8d8bef9SDimitry Andric class ModuleDebugInfoLegacyPrinter : public ModulePass { 300b57cec5SDimitry Andric DebugInfoFinder Finder; 31e8d8bef9SDimitry Andric 320b57cec5SDimitry Andric public: 330b57cec5SDimitry Andric static char ID; // Pass identification, replacement for typeid 34e8d8bef9SDimitry Andric ModuleDebugInfoLegacyPrinter() : ModulePass(ID) { 35e8d8bef9SDimitry Andric initializeModuleDebugInfoLegacyPrinterPass( 36e8d8bef9SDimitry Andric *PassRegistry::getPassRegistry()); 370b57cec5SDimitry Andric } 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric bool runOnModule(Module &M) override; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 420b57cec5SDimitry Andric AU.setPreservesAll(); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric void print(raw_ostream &O, const Module *M) const override; 450b57cec5SDimitry Andric }; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric 48e8d8bef9SDimitry Andric char ModuleDebugInfoLegacyPrinter::ID = 0; 49e8d8bef9SDimitry Andric INITIALIZE_PASS(ModuleDebugInfoLegacyPrinter, "module-debuginfo", 500b57cec5SDimitry Andric "Decodes module-level debug info", false, true) 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric ModulePass *llvm::createModuleDebugInfoPrinterPass() { 53e8d8bef9SDimitry Andric return new ModuleDebugInfoLegacyPrinter(); 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 56e8d8bef9SDimitry Andric bool ModuleDebugInfoLegacyPrinter::runOnModule(Module &M) { 570b57cec5SDimitry Andric Finder.processModule(M); 580b57cec5SDimitry Andric return false; 590b57cec5SDimitry Andric } 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, 620b57cec5SDimitry Andric unsigned Line = 0) { 630b57cec5SDimitry Andric if (Filename.empty()) 640b57cec5SDimitry Andric return; 650b57cec5SDimitry Andric 660b57cec5SDimitry Andric O << " from "; 670b57cec5SDimitry Andric if (!Directory.empty()) 680b57cec5SDimitry Andric O << Directory << "/"; 690b57cec5SDimitry Andric O << Filename; 700b57cec5SDimitry Andric if (Line) 710b57cec5SDimitry Andric O << ":" << Line; 720b57cec5SDimitry Andric } 730b57cec5SDimitry Andric 74e8d8bef9SDimitry Andric static void printModuleDebugInfo(raw_ostream &O, const Module *M, 75e8d8bef9SDimitry Andric const DebugInfoFinder &Finder) { 760b57cec5SDimitry Andric // Printing the nodes directly isn't particularly helpful (since they 770b57cec5SDimitry Andric // reference other nodes that won't be printed, particularly for the 780b57cec5SDimitry Andric // filenames), so just print a few useful things. 790b57cec5SDimitry Andric for (DICompileUnit *CU : Finder.compile_units()) { 800b57cec5SDimitry Andric O << "Compile unit: "; 810b57cec5SDimitry Andric auto Lang = dwarf::LanguageString(CU->getSourceLanguage()); 820b57cec5SDimitry Andric if (!Lang.empty()) 830b57cec5SDimitry Andric O << Lang; 840b57cec5SDimitry Andric else 850b57cec5SDimitry Andric O << "unknown-language(" << CU->getSourceLanguage() << ")"; 860b57cec5SDimitry Andric printFile(O, CU->getFilename(), CU->getDirectory()); 870b57cec5SDimitry Andric O << '\n'; 880b57cec5SDimitry Andric } 890b57cec5SDimitry Andric 900b57cec5SDimitry Andric for (DISubprogram *S : Finder.subprograms()) { 910b57cec5SDimitry Andric O << "Subprogram: " << S->getName(); 920b57cec5SDimitry Andric printFile(O, S->getFilename(), S->getDirectory(), S->getLine()); 930b57cec5SDimitry Andric if (!S->getLinkageName().empty()) 940b57cec5SDimitry Andric O << " ('" << S->getLinkageName() << "')"; 950b57cec5SDimitry Andric O << '\n'; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric for (auto GVU : Finder.global_variables()) { 990b57cec5SDimitry Andric const auto *GV = GVU->getVariable(); 1000b57cec5SDimitry Andric O << "Global variable: " << GV->getName(); 1010b57cec5SDimitry Andric printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine()); 1020b57cec5SDimitry Andric if (!GV->getLinkageName().empty()) 1030b57cec5SDimitry Andric O << " ('" << GV->getLinkageName() << "')"; 1040b57cec5SDimitry Andric O << '\n'; 1050b57cec5SDimitry Andric } 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric for (const DIType *T : Finder.types()) { 1080b57cec5SDimitry Andric O << "Type:"; 1090b57cec5SDimitry Andric if (!T->getName().empty()) 1100b57cec5SDimitry Andric O << ' ' << T->getName(); 1110b57cec5SDimitry Andric printFile(O, T->getFilename(), T->getDirectory(), T->getLine()); 1120b57cec5SDimitry Andric if (auto *BT = dyn_cast<DIBasicType>(T)) { 1130b57cec5SDimitry Andric O << " "; 1140b57cec5SDimitry Andric auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding()); 1150b57cec5SDimitry Andric if (!Encoding.empty()) 1160b57cec5SDimitry Andric O << Encoding; 1170b57cec5SDimitry Andric else 1180b57cec5SDimitry Andric O << "unknown-encoding(" << BT->getEncoding() << ')'; 1190b57cec5SDimitry Andric } else { 1200b57cec5SDimitry Andric O << ' '; 1210b57cec5SDimitry Andric auto Tag = dwarf::TagString(T->getTag()); 1220b57cec5SDimitry Andric if (!Tag.empty()) 1230b57cec5SDimitry Andric O << Tag; 1240b57cec5SDimitry Andric else 1250b57cec5SDimitry Andric O << "unknown-tag(" << T->getTag() << ")"; 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric if (auto *CT = dyn_cast<DICompositeType>(T)) { 1280b57cec5SDimitry Andric if (auto *S = CT->getRawIdentifier()) 1290b57cec5SDimitry Andric O << " (identifier: '" << S->getString() << "')"; 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric O << '\n'; 1320b57cec5SDimitry Andric } 1330b57cec5SDimitry Andric } 134e8d8bef9SDimitry Andric 135e8d8bef9SDimitry Andric void ModuleDebugInfoLegacyPrinter::print(raw_ostream &O, 136e8d8bef9SDimitry Andric const Module *M) const { 137e8d8bef9SDimitry Andric printModuleDebugInfo(O, M, Finder); 138e8d8bef9SDimitry Andric } 139e8d8bef9SDimitry Andric 140e8d8bef9SDimitry Andric ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS) 141e8d8bef9SDimitry Andric : OS(OS) {} 142e8d8bef9SDimitry Andric 143e8d8bef9SDimitry Andric PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M, 144e8d8bef9SDimitry Andric ModuleAnalysisManager &AM) { 145e8d8bef9SDimitry Andric Finder.processModule(M); 146e8d8bef9SDimitry Andric printModuleDebugInfo(OS, &M, Finder); 147e8d8bef9SDimitry Andric return PreservedAnalyses::all(); 148e8d8bef9SDimitry Andric } 149