ModuleDebugInfoPrinter.cpp 4.7 KB

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