Dan Gohman | fb64b5d | 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 | |
| 18 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9a4c9e5 | 2014-03-06 00:46:21 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" |
Bill Wendling | e38859d | 2012-06-28 00:05:13 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
| 27 | namespace { |
| 28 | class ModuleDebugInfoPrinter : public ModulePass { |
| 29 | DebugInfoFinder Finder; |
| 30 | public: |
| 31 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 32 | ModuleDebugInfoPrinter() : ModulePass(ID) { |
| 33 | initializeModuleDebugInfoPrinterPass(*PassRegistry::getPassRegistry()); |
| 34 | } |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 35 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 36 | bool runOnModule(Module &M) override; |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 37 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 38 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 39 | AU.setPreservesAll(); |
| 40 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 41 | void print(raw_ostream &O, const Module *M) const override; |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 42 | }; |
| 43 | } |
| 44 | |
| 45 | char ModuleDebugInfoPrinter::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS(ModuleDebugInfoPrinter, "module-debuginfo", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 47 | "Decodes module-level debug info", false, true) |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 48 | |
| 49 | ModulePass *llvm::createModuleDebugInfoPrinterPass() { |
| 50 | return new ModuleDebugInfoPrinter(); |
| 51 | } |
| 52 | |
| 53 | bool ModuleDebugInfoPrinter::runOnModule(Module &M) { |
| 54 | Finder.processModule(M); |
| 55 | return false; |
| 56 | } |
| 57 | |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 58 | static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, |
| 59 | unsigned Line = 0) { |
| 60 | if (Filename.empty()) |
| 61 | return; |
| 62 | |
| 63 | O << " from "; |
| 64 | if (!Directory.empty()) |
| 65 | O << Directory << "/"; |
| 66 | O << Filename; |
| 67 | if (Line) |
| 68 | O << ":" << Line; |
| 69 | } |
| 70 | |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 71 | void ModuleDebugInfoPrinter::print(raw_ostream &O, const Module *M) const { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 72 | // Printing the nodes directly isn't particularly helpful (since they |
| 73 | // reference other nodes that won't be printed, particularly for the |
| 74 | // filenames), so just print a few useful things. |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 75 | for (DICompileUnit *CU : Finder.compile_units()) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 76 | O << "Compile unit: "; |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 77 | if (const char *Lang = dwarf::LanguageString(CU->getSourceLanguage())) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 78 | O << Lang; |
| 79 | else |
Duncan P. N. Exon Smith | 35ef22c | 2015-04-15 23:19:27 +0000 | [diff] [blame] | 80 | O << "unknown-language(" << CU->getSourceLanguage() << ")"; |
| 81 | printFile(O, CU->getFilename(), CU->getDirectory()); |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 82 | O << '\n'; |
| 83 | } |
| 84 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 85 | for (DISubprogram *S : Finder.subprograms()) { |
Duncan P. N. Exon Smith | 537b4a8 | 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 | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 90 | O << '\n'; |
| 91 | } |
| 92 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 93 | for (const DIGlobalVariable *GV : Finder.global_variables()) { |
Duncan P. N. Exon Smith | 7348dda | 2015-04-14 02:22:36 +0000 | [diff] [blame] | 94 | O << "Global variable: " << GV->getName(); |
| 95 | printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine()); |
| 96 | if (!GV->getLinkageName().empty()) |
| 97 | O << " ('" << GV->getLinkageName() << "')"; |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 98 | O << '\n'; |
| 99 | } |
| 100 | |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 101 | for (const DIType *T : Finder.types()) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 102 | O << "Type:"; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 103 | if (!T->getName().empty()) |
| 104 | O << ' ' << T->getName(); |
| 105 | printFile(O, T->getFilename(), T->getDirectory(), T->getLine()); |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 106 | if (auto *BT = dyn_cast<DIBasicType>(T)) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 107 | O << " "; |
| 108 | if (const char *Encoding = |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 109 | dwarf::AttributeEncodingString(BT->getEncoding())) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 110 | O << Encoding; |
| 111 | else |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 112 | O << "unknown-encoding(" << BT->getEncoding() << ')'; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 113 | } else { |
| 114 | O << ' '; |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 115 | if (const char *Tag = dwarf::TagString(T->getTag())) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 116 | O << Tag; |
| 117 | else |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 118 | O << "unknown-tag(" << T->getTag() << ")"; |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 119 | } |
Duncan P. N. Exon Smith | a9308c4 | 2015-04-29 16:38:44 +0000 | [diff] [blame^] | 120 | if (auto *CT = dyn_cast<DICompositeType>(T)) { |
Duncan P. N. Exon Smith | b105564 | 2015-04-16 01:01:28 +0000 | [diff] [blame] | 121 | if (auto *S = CT->getRawIdentifier()) |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame] | 122 | O << " (identifier: '" << S->getString() << "')"; |
| 123 | } |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 124 | O << '\n'; |
| 125 | } |
| 126 | } |