LLVM 22.0.0git
ModuleDebugInfoPrinter.cpp
Go to the documentation of this file.
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
19#include "llvm/IR/DebugInfo.h"
20#include "llvm/IR/PassManager.h"
21#include "llvm/Pass.h"
24using namespace llvm;
25
26static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory,
27 unsigned Line = 0) {
28 if (Filename.empty())
29 return;
30
31 O << " from ";
32 if (!Directory.empty())
33 O << Directory << "/";
34 O << Filename;
35 if (Line)
36 O << ":" << Line;
37}
38
39static void printModuleDebugInfo(raw_ostream &O, const Module *M,
40 const DebugInfoFinder &Finder) {
41 // Printing the nodes directly isn't particularly helpful (since they
42 // reference other nodes that won't be printed, particularly for the
43 // filenames), so just print a few useful things.
44 for (DICompileUnit *CU : Finder.compile_units()) {
45 O << "Compile unit: ";
46 auto Lang =
47 dwarf::LanguageString(CU->getSourceLanguage().getUnversionedName());
48 if (!Lang.empty())
49 O << Lang;
50 else
51 O << "unknown-language(" << CU->getSourceLanguage().getUnversionedName()
52 << ")";
53 printFile(O, CU->getFilename(), CU->getDirectory());
54 O << '\n';
55 }
56
57 for (DISubprogram *S : Finder.subprograms()) {
58 O << "Subprogram: " << S->getName();
59 printFile(O, S->getFilename(), S->getDirectory(), S->getLine());
60 if (!S->getLinkageName().empty())
61 O << " ('" << S->getLinkageName() << "')";
62 O << '\n';
63 }
64
65 for (auto *GVU : Finder.global_variables()) {
66 const auto *GV = GVU->getVariable();
67 O << "Global variable: " << GV->getName();
68 printFile(O, GV->getFilename(), GV->getDirectory(), GV->getLine());
69 if (!GV->getLinkageName().empty())
70 O << " ('" << GV->getLinkageName() << "')";
71 O << '\n';
72 }
73
74 for (const DIType *T : Finder.types()) {
75 O << "Type:";
76 if (!T->getName().empty())
77 O << ' ' << T->getName();
78 printFile(O, T->getFilename(), T->getDirectory(), T->getLine());
79 if (auto *BT = dyn_cast<DIBasicType>(T)) {
80 O << " ";
81 auto Encoding = dwarf::AttributeEncodingString(BT->getEncoding());
82 if (!Encoding.empty())
83 O << Encoding;
84 else
85 O << "unknown-encoding(" << BT->getEncoding() << ')';
86 } else {
87 O << ' ';
88 auto Tag = dwarf::TagString(T->getTag());
89 if (!Tag.empty())
90 O << Tag;
91 else
92 O << "unknown-tag(" << T->getTag() << ")";
93 }
94 if (auto *CT = dyn_cast<DICompositeType>(T)) {
95 if (auto *S = CT->getRawIdentifier())
96 O << " (identifier: '" << S->getString() << "')";
97 }
98 O << '\n';
99 }
100}
101
104
107 Finder.processModule(M);
108 printModuleDebugInfo(OS, &M, Finder);
109 return PreservedAnalyses::all();
110}
BitTracker BT
This file contains constants used for implementing Dwarf debug support.
This header defines various interfaces for pass management in LLVM.
#define T
static void printModuleDebugInfo(raw_ostream &O, const Module *M, const DebugInfoFinder &Finder)
static void printFile(raw_ostream &O, StringRef Filename, StringRef Directory, unsigned Line=0)
Subprogram description. Uses SubclassData1.
Base class for types.
Utility to find all debug info in a module.
Definition DebugInfo.h:103
iterator_range< global_variable_expression_iterator > global_variables() const
Definition DebugInfo.h:151
iterator_range< subprogram_iterator > subprograms() const
Definition DebugInfo.h:147
iterator_range< type_iterator > types() const
Definition DebugInfo.h:155
iterator_range< compile_unit_iterator > compile_units() const
Definition DebugInfo.h:143
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI StringRef LanguageString(unsigned Language)
Definition Dwarf.cpp:412
LLVM_ABI StringRef AttributeEncodingString(unsigned Encoding)
Definition Dwarf.cpp:263
LLVM_ABI StringRef TagString(unsigned Tag)
Definition Dwarf.cpp:21
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39