Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 1 | //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass decodes the debug info metadata in a module and prints in a |
| 11 | // (sufficiently-prepared-) human-readable form. |
| 12 | // |
| 13 | // For example, run this pass from opt along with the -analyze option, and |
| 14 | // it'll print to standard output. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | f4ec8bf | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DebugInfo.h" |
Bill Wendling | 0bcbd1d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
| 27 | class ModuleDebugInfoPrinter : public ModulePass { |
| 28 | DebugInfoFinder Finder; |
| 29 | public: |
| 30 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 31 | ModuleDebugInfoPrinter() : ModulePass(ID) { |
| 32 | initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry()); |
| 33 | } |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 34 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 35 | bool runOnModule(Module &M) override; |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 36 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 37 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 38 | AU.setPreservesAll(); |
| 39 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 40 | void print(raw_ostream &O, const Module *M) const override; |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 41 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 42 | } |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 43 | |
| 44 | char ModuleDebugInfoPrinter::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 45 | INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 46 | "Decodes module-level debug info", false, true) |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 47 | |
| 48 | ModulePass *llvm::createModuleDebugInfoPrinterPass() { |
| 49 | return new ModuleDebugInfoPrinter(); |
| 50 | } |
| 51 | |
| 52 | bool ModuleDebugInfoPrinter::runOnModule(Module &M) { |
| 53 | Finder.processModule(M); |
| 54 | return false; |
| 55 | } |
| 56 | |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 57 | static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, |
| 58 | unsigned Line = 0) { |
| 59 | if (Filename.empty()) |
| 60 | return; |
| 61 | |
| 62 | O << " from "; |
| 63 | if (!Directory.empty()) |
| 64 | O << Directory << "/"; |
| 65 | O << Filename; |
| 66 | if (Line) |
| 67 | O << ":" << Line; |
| 68 | } |
| 69 | |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 70 | void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 71 | // Printing the nodes directly isn't particularly helpful (since they |
| 72 | // reference other nodes that won't be printed, particularly for the |
| 73 | // filenames), so just print a few useful things. |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 74 | for (DICompileUnit *CU : Finder.compile_units()) { |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 75 | O << "Compile unit: "; |
Mehdi Amini | f9bb6bc | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 76 | auto Lang = dwarf::LanguageString(CU->getSourceLanguage()); |
| 77 | if (!Lang.empty()) |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 78 | O << Lang; |
| 79 | else |
Duncan P. N. Exon Smith | ed0e117 | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 80 | O << "unknown-language(" << CU->getSourceLanguage() << ")"; |
| 81 | printFile(O, CU->getFilename(), CU->getDirectory()); |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 82 | O << '\n'; |
| 83 | } |
| 84 | |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 85 | for (DISubprogram *S : Finder.subprograms()) { |
Duncan P. N. Exon Smith | 125e3d3 | 2015-04-14 03:40:37 +0000 | [diff] [blame] | 86 | O << "Subprogram: " << S->getName(); |
| 87 | printFile(O, S->getFilename(), S->getDirectory(), S->getLine()); |
| 88 | if (!S->getLinkageName().empty()) |
| 89 | O << " ('" << S->getLinkageName() << "')"; |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 90 | O << '\n'; |
| 91 | } |
| 92 | |
Adrian Prantl | 7b500b4 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 93 | for (auto GVU : Finder.global_variables()) { |
| 94 | const auto *GV = GVU->getVariable(); |
Duncan P. N. Exon Smith | 355ec00 | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 95 | O << "Global variable: " << GV->getName(); |
| 96 | printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine()); |
| 97 | if (!GV->getLinkageName().empty()) |
| 98 | O << " ('" << GV->getLinkageName() << "')"; |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 99 | O << '\n'; |
| 100 | } |
| 101 | |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 102 | for (const DIType *T : Finder.types()) { |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 103 | O << "Type:"; |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 104 | if (!T->getName().empty()) |
| 105 | O << ' ' << T->getName(); |
| 106 | printFile(O, T->getFilename(), T->getDirectory(), T->getLine()); |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 107 | if (auto *BT = dyn_cast<DIBasicType>(T)) { |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 108 | O << " "; |
Mehdi Amini | f9bb6bc | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 109 | auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding()); |
| 110 | if (!Encoding.empty()) |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 111 | O << Encoding; |
| 112 | else |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 113 | O << "unknown-encoding(" << BT->getEncoding() << ')'; |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 114 | } else { |
| 115 | O << ' '; |
Mehdi Amini | f9bb6bc | 2016-10-05 05:59:29 +0000 | [diff] [blame] | 116 | auto Tag = dwarf::TagString(T->getTag()); |
| 117 | if (!Tag.empty()) |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 118 | O << Tag; |
| 119 | else |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 120 | O << "unknown-tag(" << T->getTag() << ")"; |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 121 | } |
Duncan P. N. Exon Smith | e56023a | 2015-04-29 16:38:44 +0000 | [diff] [blame] | 122 | if (auto *CT = dyn_cast<DICompositeType>(T)) { |
Duncan P. N. Exon Smith | 7f76d29 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 123 | if (auto *S = CT->getRawIdentifier()) |
Duncan P. N. Exon Smith | b056aa7 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 124 | O << " (identifier: '" << S->getString() << "')"; |
| 125 | } |
Dan Gohman | ef0b145 | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 126 | O << '\n'; |
| 127 | } |
| 128 | } |