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