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. |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +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: "; |
| 77 | if (const char *Lang = LanguageString(CU.getLanguage())) |
| 78 | O << Lang; |
| 79 | else |
| 80 | O << "unknown-language(" << CU.getLanguage() << ")"; |
| 81 | printFile(O, CU.getFilename(), CU.getDirectory()); |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 82 | O << '\n'; |
| 83 | } |
| 84 | |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +0000 | [diff] [blame] | 85 | for (DISubprogram S : Finder.subprograms()) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 86 | O << "Subprogram: " << S.getName(); |
| 87 | printFile(O, S.getFilename(), S.getDirectory(), S.getLineNumber()); |
| 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 | |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +0000 | [diff] [blame] | 93 | for (DIGlobalVariable GV : Finder.global_variables()) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 94 | O << "Global variable: " << GV.getName(); |
| 95 | printFile(O, GV.getFilename(), GV.getDirectory(), GV.getLineNumber()); |
| 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 | |
Alon Mishne | ad31215 | 2014-03-18 09:41:07 +0000 | [diff] [blame] | 101 | for (DIType T : Finder.types()) { |
Duncan P. N. Exon Smith | e274180 | 2015-03-03 17:24:31 +0000 | [diff] [blame^] | 102 | O << "Type:"; |
| 103 | if (!T.getName().empty()) |
| 104 | O << ' ' << T.getName(); |
| 105 | printFile(O, T.getFilename(), T.getDirectory(), T.getLineNumber()); |
| 106 | if (T.isBasicType()) { |
| 107 | DIBasicType BT(T.get()); |
| 108 | O << " "; |
| 109 | if (const char *Encoding = |
| 110 | dwarf::AttributeEncodingString(BT.getEncoding())) |
| 111 | O << Encoding; |
| 112 | else |
| 113 | O << "unknown-encoding(" << BT.getEncoding() << ')'; |
| 114 | } else { |
| 115 | O << ' '; |
| 116 | if (const char *Tag = dwarf::TagString(T.getTag())) |
| 117 | O << Tag; |
| 118 | else |
| 119 | O << "unknown-tag(" << T.getTag() << ")"; |
| 120 | } |
| 121 | if (T.isCompositeType()) { |
| 122 | DICompositeType CT(T.get()); |
| 123 | if (auto *S = CT.getIdentifier()) |
| 124 | O << " (identifier: '" << S->getString() << "')"; |
| 125 | } |
Dan Gohman | fb64b5d | 2010-05-07 16:22:32 +0000 | [diff] [blame] | 126 | O << '\n'; |
| 127 | } |
| 128 | } |