1*0b57cec5SDimitry Andric //===-- ModuleDebugInfoPrinter.cpp - Prints module debug info metadata ----===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This pass decodes the debug info metadata in a module and prints in a
10*0b57cec5SDimitry Andric // (sufficiently-prepared-) human-readable form.
11*0b57cec5SDimitry Andric //
12*0b57cec5SDimitry Andric // For example, run this pass from opt along with the -analyze option, and
13*0b57cec5SDimitry Andric // it'll print to standard output.
14*0b57cec5SDimitry Andric //
15*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
16*0b57cec5SDimitry Andric 
17e8d8bef9SDimitry Andric #include "llvm/Analysis/ModuleDebugInfoPrinter.h"
18*0b57cec5SDimitry Andric #include "llvm/Analysis/Passes.h"
1981ad6265SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
20*0b57cec5SDimitry Andric #include "llvm/IR/DebugInfo.h"
21e8d8bef9SDimitry Andric #include "llvm/IR/PassManager.h"
22480093f4SDimitry Andric #include "llvm/InitializePasses.h"
23*0b57cec5SDimitry Andric #include "llvm/Pass.h"
24*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
25*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
26*0b57cec5SDimitry Andric using namespace llvm;
27*0b57cec5SDimitry Andric 
printFile(raw_ostream & O,StringRef Filename,StringRef Directory,unsigned Line=0)28*0b57cec5SDimitry Andric static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
29*0b57cec5SDimitry Andric                       unsigned Line = 0) {
30*0b57cec5SDimitry Andric   if (Filename.empty())
31*0b57cec5SDimitry Andric     return;
32*0b57cec5SDimitry Andric 
33*0b57cec5SDimitry Andric   O << " from ";
34*0b57cec5SDimitry Andric   if (!Directory.empty())
35*0b57cec5SDimitry Andric     O << Directory << "/";
36*0b57cec5SDimitry Andric   O << Filename;
37*0b57cec5SDimitry Andric   if (Line)
38*0b57cec5SDimitry Andric     O << ":" << Line;
39*0b57cec5SDimitry Andric }
40*0b57cec5SDimitry Andric 
printModuleDebugInfo(raw_ostream & O,const Module * M,const DebugInfoFinder & Finder)41e8d8bef9SDimitry Andric static void printModuleDebugInfo(raw_ostream &O, const Module *M,
42e8d8bef9SDimitry Andric                                  const DebugInfoFinder &Finder) {
43*0b57cec5SDimitry Andric   // Printing the nodes directly isn't particularly helpful (since they
44*0b57cec5SDimitry Andric   // reference other nodes that won't be printed, particularly for the
45*0b57cec5SDimitry Andric   // filenames), so just print a few useful things.
46*0b57cec5SDimitry Andric   for (DICompileUnit *CU : Finder.compile_units()) {
47*0b57cec5SDimitry Andric     O << "Compile unit: ";
48*0b57cec5SDimitry Andric     auto Lang = dwarf::LanguageString(CU->getSourceLanguage());
49*0b57cec5SDimitry Andric     if (!Lang.empty())
50*0b57cec5SDimitry Andric       O << Lang;
51*0b57cec5SDimitry Andric     else
52*0b57cec5SDimitry Andric       O << "unknown-language(" << CU->getSourceLanguage() << ")";
53*0b57cec5SDimitry Andric     printFile(O, CU->getFilename(), CU->getDirectory());
54*0b57cec5SDimitry Andric     O << '\n';
55*0b57cec5SDimitry Andric   }
56*0b57cec5SDimitry Andric 
57*0b57cec5SDimitry Andric   for (DISubprogram *S : Finder.subprograms()) {
58*0b57cec5SDimitry Andric     O << "Subprogram: " << S->getName();
59*0b57cec5SDimitry Andric     printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
60*0b57cec5SDimitry Andric     if (!S->getLinkageName().empty())
61*0b57cec5SDimitry Andric       O << " ('" << S->getLinkageName() << "')";
62*0b57cec5SDimitry Andric     O << '\n';
63*0b57cec5SDimitry Andric   }
64*0b57cec5SDimitry Andric 
65fcaf7f86SDimitry Andric   for (auto *GVU : Finder.global_variables()) {
66*0b57cec5SDimitry Andric     const auto *GV = GVU->getVariable();
67*0b57cec5SDimitry Andric     O << "Global variable: " << GV->getName();
68*0b57cec5SDimitry Andric     printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
69*0b57cec5SDimitry Andric     if (!GV->getLinkageName().empty())
70*0b57cec5SDimitry Andric       O << " ('" << GV->getLinkageName() << "')";
71*0b57cec5SDimitry Andric     O << '\n';
72*0b57cec5SDimitry Andric   }
73*0b57cec5SDimitry Andric 
74*0b57cec5SDimitry Andric   for (const DIType *T : Finder.types()) {
75*0b57cec5SDimitry Andric     O << "Type:";
76*0b57cec5SDimitry Andric     if (!T->getName().empty())
77*0b57cec5SDimitry Andric       O << ' ' << T->getName();
78*0b57cec5SDimitry Andric     printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
79*0b57cec5SDimitry Andric     if (auto *BT = dyn_cast<DIBasicType>(T)) {
80*0b57cec5SDimitry Andric       O << " ";
81*0b57cec5SDimitry Andric       auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
82*0b57cec5SDimitry Andric       if (!Encoding.empty())
83*0b57cec5SDimitry Andric         O << Encoding;
84*0b57cec5SDimitry Andric       else
85*0b57cec5SDimitry Andric         O << "unknown-encoding(" << BT->getEncoding() << ')';
86*0b57cec5SDimitry Andric     } else {
87*0b57cec5SDimitry Andric       O << ' ';
88*0b57cec5SDimitry Andric       auto Tag = dwarf::TagString(T->getTag());
89*0b57cec5SDimitry Andric       if (!Tag.empty())
90*0b57cec5SDimitry Andric         O << Tag;
91*0b57cec5SDimitry Andric       else
92*0b57cec5SDimitry Andric         O << "unknown-tag(" << T->getTag() << ")";
93*0b57cec5SDimitry Andric     }
94*0b57cec5SDimitry Andric     if (auto *CT = dyn_cast<DICompositeType>(T)) {
95*0b57cec5SDimitry Andric       if (auto *S = CT->getRawIdentifier())
96*0b57cec5SDimitry Andric         O << " (identifier: '" << S->getString() << "')";
97*0b57cec5SDimitry Andric     }
98*0b57cec5SDimitry Andric     O << '\n';
99*0b57cec5SDimitry Andric   }
100*0b57cec5SDimitry Andric }
101e8d8bef9SDimitry Andric 
ModuleDebugInfoPrinterPass(raw_ostream & OS)102e8d8bef9SDimitry Andric ModuleDebugInfoPrinterPass::ModuleDebugInfoPrinterPass(raw_ostream &OS)
103e8d8bef9SDimitry Andric     : OS(OS) {}
104e8d8bef9SDimitry Andric 
run(Module & M,ModuleAnalysisManager & AM)105e8d8bef9SDimitry Andric PreservedAnalyses ModuleDebugInfoPrinterPass::run(Module &M,
106e8d8bef9SDimitry Andric                                                   ModuleAnalysisManager &AM) {
107e8d8bef9SDimitry Andric   Finder.processModule(M);
108e8d8bef9SDimitry Andric   printModuleDebugInfo(OS, &M, Finder);
109e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
110e8d8bef9SDimitry Andric }
111