1//===- PrettyCompilandDumper.cpp - llvm-pdbutil compiland dumper -*- C++ *-===//
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#include "PrettyCompilandDumper.h"
10
11#include "PrettyFunctionDumper.h"
12#include "llvm-pdbutil.h"
13
14#include "llvm/ADT/StringExtras.h"
15#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
16#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
17#include "llvm/DebugInfo/PDB/IPDBSession.h"
18#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
19#include "llvm/DebugInfo/PDB/PDBExtras.h"
20#include "llvm/DebugInfo/PDB/PDBSymbol.h"
21#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
22#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
23#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
24#include "llvm/DebugInfo/PDB/PDBSymbolLabel.h"
25#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
26#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
27#include "llvm/DebugInfo/PDB/PDBSymbolUnknown.h"
28#include "llvm/DebugInfo/PDB/PDBSymbolUsingNamespace.h"
29#include "llvm/Support/Format.h"
30#include "llvm/Support/raw_ostream.h"
31
32using namespace llvm;
33using namespace llvm::pdb;
34
35CompilandDumper::CompilandDumper(LinePrinter &P)
36 : PDBSymDumper(true), Printer(P) {}
37
38void CompilandDumper::dump(const PDBSymbolCompilandDetails &Symbol) {}
39
40void CompilandDumper::dump(const PDBSymbolCompilandEnv &Symbol) {}
41
42void CompilandDumper::start(const PDBSymbolCompiland &Symbol,
43 CompilandDumpFlags opts) {
44 std::string FullName = Symbol.getName();
45 if (Printer.IsCompilandExcluded(CompilandName: FullName))
46 return;
47
48 Printer.NewLine();
49 WithColor(Printer, PDB_ColorItem::Path).get() << FullName;
50
51 if (opts & Flags::Lines) {
52 const IPDBSession &Session = Symbol.getSession();
53 if (auto Files = Session.getSourceFilesForCompiland(Compiland: Symbol)) {
54 Printer.Indent();
55 while (auto File = Files->getNext()) {
56 Printer.NewLine();
57 WithColor(Printer, PDB_ColorItem::Path).get() << File->getFileName();
58 if (File->getChecksumType() != PDB_Checksum::None) {
59 auto ChecksumType = File->getChecksumType();
60 auto ChecksumHexString = toHex(Input: File->getChecksum());
61 WithColor(Printer, PDB_ColorItem::Comment).get()
62 << " (" << ChecksumType << ": " << ChecksumHexString << ")";
63 }
64
65 auto Lines = Session.findLineNumbers(Compiland: Symbol, File: *File);
66 if (!Lines)
67 continue;
68
69 Printer.Indent();
70 while (auto Line = Lines->getNext()) {
71 Printer.NewLine();
72 uint32_t LineStart = Line->getLineNumber();
73 uint32_t LineEnd = Line->getLineNumberEnd();
74
75 Printer << "Line ";
76 PDB_ColorItem StatementColor = Line->isStatement()
77 ? PDB_ColorItem::Keyword
78 : PDB_ColorItem::LiteralValue;
79 WithColor(Printer, StatementColor).get() << LineStart;
80 if (LineStart != LineEnd)
81 WithColor(Printer, StatementColor).get() << " - " << LineEnd;
82
83 uint32_t ColumnStart = Line->getColumnNumber();
84 uint32_t ColumnEnd = Line->getColumnNumberEnd();
85 if (ColumnStart != 0 || ColumnEnd != 0) {
86 Printer << ", Column: ";
87 WithColor(Printer, StatementColor).get() << ColumnStart;
88 if (ColumnEnd != ColumnStart)
89 WithColor(Printer, StatementColor).get() << " - " << ColumnEnd;
90 }
91
92 Printer << ", Address: ";
93 if (Line->getLength() > 0) {
94 uint64_t AddrStart = Line->getVirtualAddress();
95 uint64_t AddrEnd = AddrStart + Line->getLength() - 1;
96 WithColor(Printer, PDB_ColorItem::Address).get()
97 << "[" << format_hex(N: AddrStart, Width: 10) << " - "
98 << format_hex(N: AddrEnd, Width: 10) << "]";
99 Printer << " (" << Line->getLength() << " bytes)";
100 } else {
101 uint64_t AddrStart = Line->getVirtualAddress();
102 WithColor(Printer, PDB_ColorItem::Address).get()
103 << "[" << format_hex(N: AddrStart, Width: 10) << "] ";
104 Printer << "(0 bytes)";
105 }
106 }
107 Printer.Unindent();
108 }
109 Printer.Unindent();
110 }
111 }
112
113 if (opts & Flags::Children) {
114 if (auto ChildrenEnum = Symbol.findAllChildren()) {
115 Printer.Indent();
116 while (auto Child = ChildrenEnum->getNext())
117 Child->dump(Dumper&: *this);
118 Printer.Unindent();
119 }
120 }
121}
122
123void CompilandDumper::dump(const PDBSymbolData &Symbol) {
124 if (!shouldDumpSymLevel(Level: opts::pretty::SymLevel::Data))
125 return;
126 if (Printer.IsSymbolExcluded(SymbolName: Symbol.getName()))
127 return;
128
129 Printer.NewLine();
130
131 switch (auto LocType = Symbol.getLocationType()) {
132 case PDB_LocType::Static:
133 Printer << "data: ";
134 WithColor(Printer, PDB_ColorItem::Address).get()
135 << "[" << format_hex(N: Symbol.getVirtualAddress(), Width: 10) << "]";
136
137 WithColor(Printer, PDB_ColorItem::Comment).get()
138 << " [sizeof = " << getTypeLength(Symbol) << "]";
139
140 break;
141 case PDB_LocType::Constant:
142 Printer << "constant: ";
143 WithColor(Printer, PDB_ColorItem::LiteralValue).get()
144 << "[" << Symbol.getValue() << "]";
145 WithColor(Printer, PDB_ColorItem::Comment).get()
146 << " [sizeof = " << getTypeLength(Symbol) << "]";
147 break;
148 default:
149 Printer << "data(unexpected type=" << LocType << ")";
150 }
151
152 Printer << " ";
153 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
154}
155
156void CompilandDumper::dump(const PDBSymbolFunc &Symbol) {
157 if (!shouldDumpSymLevel(Level: opts::pretty::SymLevel::Functions))
158 return;
159 if (Symbol.getLength() == 0)
160 return;
161 if (Printer.IsSymbolExcluded(SymbolName: Symbol.getName()))
162 return;
163
164 Printer.NewLine();
165 FunctionDumper Dumper(Printer);
166 Dumper.start(Symbol, Pointer: FunctionDumper::PointerType::None);
167}
168
169void CompilandDumper::dump(const PDBSymbolLabel &Symbol) {
170 if (Printer.IsSymbolExcluded(SymbolName: Symbol.getName()))
171 return;
172
173 Printer.NewLine();
174 Printer << "label ";
175 WithColor(Printer, PDB_ColorItem::Address).get()
176 << "[" << format_hex(N: Symbol.getVirtualAddress(), Width: 10) << "] ";
177 WithColor(Printer, PDB_ColorItem::Identifier).get() << Symbol.getName();
178}
179
180void CompilandDumper::dump(const PDBSymbolThunk &Symbol) {
181 if (!shouldDumpSymLevel(Level: opts::pretty::SymLevel::Thunks))
182 return;
183 if (Printer.IsSymbolExcluded(SymbolName: Symbol.getName()))
184 return;
185
186 Printer.NewLine();
187 Printer << "thunk ";
188 codeview::ThunkOrdinal Ordinal = Symbol.getThunkOrdinal();
189 uint64_t VA = Symbol.getVirtualAddress();
190 if (Ordinal == codeview::ThunkOrdinal::TrampIncremental) {
191 uint64_t Target = Symbol.getTargetVirtualAddress();
192 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(N: VA, Width: 10);
193 Printer << " -> ";
194 WithColor(Printer, PDB_ColorItem::Address).get() << format_hex(N: Target, Width: 10);
195 } else {
196 WithColor(Printer, PDB_ColorItem::Address).get()
197 << "[" << format_hex(N: VA, Width: 10) << " - "
198 << format_hex(N: VA + Symbol.getLength(), Width: 10) << "]";
199 }
200 Printer << " (";
201 WithColor(Printer, PDB_ColorItem::Register).get() << Ordinal;
202 Printer << ") ";
203 std::string Name = Symbol.getName();
204 if (!Name.empty())
205 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
206}
207
208void CompilandDumper::dump(const PDBSymbolTypeTypedef &Symbol) {}
209
210void CompilandDumper::dump(const PDBSymbolUnknown &Symbol) {
211 Printer.NewLine();
212 Printer << "unknown (" << Symbol.getSymTag() << ")";
213}
214
215void CompilandDumper::dump(const PDBSymbolUsingNamespace &Symbol) {
216 if (Printer.IsSymbolExcluded(SymbolName: Symbol.getName()))
217 return;
218
219 Printer.NewLine();
220 Printer << "using namespace ";
221 std::string Name = Symbol.getName();
222 WithColor(Printer, PDB_ColorItem::Identifier).get() << Name;
223}
224