1 //===- llvm-pdbutil.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
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 // Dumps debug information present in PDB files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm-pdbutil.h"
15
16 #include "Analyze.h"
17 #include "BytesOutputStyle.h"
18 #include "DumpOutputStyle.h"
19 #include "ExplainOutputStyle.h"
20 #include "InputFile.h"
21 #include "LinePrinter.h"
22 #include "OutputStyle.h"
23 #include "PrettyClassDefinitionDumper.h"
24 #include "PrettyCompilandDumper.h"
25 #include "PrettyEnumDumper.h"
26 #include "PrettyExternalSymbolDumper.h"
27 #include "PrettyFunctionDumper.h"
28 #include "PrettyTypeDumper.h"
29 #include "PrettyTypedefDumper.h"
30 #include "PrettyVariableDumper.h"
31 #include "YAMLOutputStyle.h"
32
33 #include "llvm/ADT/ArrayRef.h"
34 #include "llvm/ADT/BitVector.h"
35 #include "llvm/ADT/DenseMap.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/ADT/StringExtras.h"
38 #include "llvm/BinaryFormat/Magic.h"
39 #include "llvm/Config/config.h"
40 #include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
41 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
42 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
43 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
44 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
45 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
46 #include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
47 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
48 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
49 #include "llvm/DebugInfo/PDB/GenericError.h"
50 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
51 #include "llvm/DebugInfo/PDB/IPDBInjectedSource.h"
52 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
53 #include "llvm/DebugInfo/PDB/IPDBSession.h"
54 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
55 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
56 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
57 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
58 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
59 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
60 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
61 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
62 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
63 #include "llvm/DebugInfo/PDB/Native/RawError.h"
64 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
65 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
66 #include "llvm/DebugInfo/PDB/PDB.h"
67 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
68 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
69 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
70 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
71 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
72 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
73 #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
74 #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
75 #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
76 #include "llvm/Support/BinaryByteStream.h"
77 #include "llvm/Support/COM.h"
78 #include "llvm/Support/CommandLine.h"
79 #include "llvm/Support/ConvertUTF.h"
80 #include "llvm/Support/FileOutputBuffer.h"
81 #include "llvm/Support/FileSystem.h"
82 #include "llvm/Support/Format.h"
83 #include "llvm/Support/InitLLVM.h"
84 #include "llvm/Support/LineIterator.h"
85 #include "llvm/Support/ManagedStatic.h"
86 #include "llvm/Support/MemoryBuffer.h"
87 #include "llvm/Support/Path.h"
88 #include "llvm/Support/PrettyStackTrace.h"
89 #include "llvm/Support/Process.h"
90 #include "llvm/Support/Regex.h"
91 #include "llvm/Support/ScopedPrinter.h"
92 #include "llvm/Support/Signals.h"
93 #include "llvm/Support/raw_ostream.h"
94
95 using namespace llvm;
96 using namespace llvm::codeview;
97 using namespace llvm::msf;
98 using namespace llvm::pdb;
99
100 namespace opts {
101
102 cl::SubCommand DumpSubcommand("dump", "Dump MSF and CodeView debug info");
103 cl::SubCommand BytesSubcommand("bytes", "Dump raw bytes from the PDB file");
104
105 cl::SubCommand
106 PrettySubcommand("pretty",
107 "Dump semantic information about types and symbols");
108
109 cl::SubCommand
110 YamlToPdbSubcommand("yaml2pdb",
111 "Generate a PDB file from a YAML description");
112 cl::SubCommand
113 PdbToYamlSubcommand("pdb2yaml",
114 "Generate a detailed YAML description of a PDB File");
115
116 cl::SubCommand
117 AnalyzeSubcommand("analyze",
118 "Analyze various aspects of a PDB's structure");
119
120 cl::SubCommand MergeSubcommand("merge",
121 "Merge multiple PDBs into a single PDB");
122
123 cl::SubCommand ExplainSubcommand("explain",
124 "Explain the meaning of a file offset");
125
126 cl::SubCommand ExportSubcommand("export",
127 "Write binary data from a stream to a file");
128
129 cl::OptionCategory TypeCategory("Symbol Type Options");
130 cl::OptionCategory FilterCategory("Filtering and Sorting Options");
131 cl::OptionCategory OtherOptions("Other Options");
132
133 cl::ValuesClass ChunkValues = cl::values(
134 clEnumValN(ModuleSubsection::CrossScopeExports, "cme",
135 "Cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
136 clEnumValN(ModuleSubsection::CrossScopeImports, "cmi",
137 "Cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
138 clEnumValN(ModuleSubsection::FileChecksums, "fc",
139 "File checksums (DEBUG_S_CHECKSUMS subsection)"),
140 clEnumValN(ModuleSubsection::InlineeLines, "ilines",
141 "Inlinee lines (DEBUG_S_INLINEELINES subsection)"),
142 clEnumValN(ModuleSubsection::Lines, "lines",
143 "Lines (DEBUG_S_LINES subsection)"),
144 clEnumValN(ModuleSubsection::StringTable, "strings",
145 "String Table (DEBUG_S_STRINGTABLE subsection) (not "
146 "typically present in PDB file)"),
147 clEnumValN(ModuleSubsection::FrameData, "frames",
148 "Frame Data (DEBUG_S_FRAMEDATA subsection)"),
149 clEnumValN(ModuleSubsection::Symbols, "symbols",
150 "Symbols (DEBUG_S_SYMBOLS subsection) (not typically "
151 "present in PDB file)"),
152 clEnumValN(ModuleSubsection::CoffSymbolRVAs, "rvas",
153 "COFF Symbol RVAs (DEBUG_S_COFF_SYMBOL_RVA subsection)"),
154 clEnumValN(ModuleSubsection::Unknown, "unknown",
155 "Any subsection not covered by another option"),
156 clEnumValN(ModuleSubsection::All, "all", "All known subsections"));
157
158 namespace pretty {
159 cl::list<std::string> InputFilenames(cl::Positional,
160 cl::desc("<input PDB files>"),
161 cl::OneOrMore, cl::sub(PrettySubcommand));
162
163 cl::opt<bool> InjectedSources("injected-sources",
164 cl::desc("Display injected sources"),
165 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
166 cl::opt<bool> ShowInjectedSourceContent(
167 "injected-source-content",
168 cl::desc("When displaying an injected source, display the file content"),
169 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
170
171 cl::list<std::string> WithName(
172 "with-name",
173 cl::desc("Display any symbol or type with the specified exact name"),
174 cl::cat(TypeCategory), cl::ZeroOrMore, cl::sub(PrettySubcommand));
175
176 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
177 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
178 cl::opt<bool> Symbols("module-syms",
179 cl::desc("Display symbols for each compiland"),
180 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
181 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
182 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
183 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
184 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
185 cl::list<SymLevel> SymTypes(
186 "sym-types", cl::desc("Type of symbols to dump (default all)"),
187 cl::cat(TypeCategory), cl::sub(PrettySubcommand), cl::ZeroOrMore,
188 cl::values(
189 clEnumValN(SymLevel::Thunks, "thunks", "Display thunk symbols"),
190 clEnumValN(SymLevel::Data, "data", "Display data symbols"),
191 clEnumValN(SymLevel::Functions, "funcs", "Display function symbols"),
192 clEnumValN(SymLevel::All, "all", "Display all symbols (default)")));
193
194 cl::opt<bool>
195 Types("types",
196 cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
197 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
198 cl::opt<bool> Classes("classes", cl::desc("Display class types"),
199 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
200 cl::opt<bool> Enums("enums", cl::desc("Display enum types"),
201 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
202 cl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
203 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
204 cl::opt<SymbolSortMode> SymbolOrder(
205 "symbol-order", cl::desc("symbol sort order"),
206 cl::init(SymbolSortMode::None),
207 cl::values(clEnumValN(SymbolSortMode::None, "none",
208 "Undefined / no particular sort order"),
209 clEnumValN(SymbolSortMode::Name, "name", "Sort symbols by name"),
210 clEnumValN(SymbolSortMode::Size, "size",
211 "Sort symbols by size")),
212 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
213
214 cl::opt<ClassSortMode> ClassOrder(
215 "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
216 cl::values(
217 clEnumValN(ClassSortMode::None, "none",
218 "Undefined / no particular sort order"),
219 clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
220 clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
221 clEnumValN(ClassSortMode::Padding, "padding",
222 "Sort classes by amount of padding"),
223 clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
224 "Sort classes by percentage of space consumed by padding"),
225 clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
226 "Sort classes by amount of immediate padding"),
227 clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
228 "Sort classes by percentage of space consumed by immediate "
229 "padding")),
230 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
231
232 cl::opt<ClassDefinitionFormat> ClassFormat(
233 "class-definitions", cl::desc("Class definition format"),
234 cl::init(ClassDefinitionFormat::All),
235 cl::values(
236 clEnumValN(ClassDefinitionFormat::All, "all",
237 "Display all class members including data, constants, "
238 "typedefs, functions, etc"),
239 clEnumValN(ClassDefinitionFormat::Layout, "layout",
240 "Only display members that contribute to class size."),
241 clEnumValN(ClassDefinitionFormat::None, "none",
242 "Don't display class definitions")),
243 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
244 cl::opt<uint32_t> ClassRecursionDepth(
245 "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
246 cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
247
248 cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
249 cl::sub(PrettySubcommand));
250 cl::opt<bool>
251 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
252 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
253
254 cl::opt<uint64_t> LoadAddress(
255 "load-address",
256 cl::desc("Assume the module is loaded at the specified address"),
257 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
258 cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
259 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
260 cl::opt<cl::boolOrDefault>
261 ColorOutput("color-output",
262 cl::desc("Override use of color (default = isatty)"),
263 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
264 cl::list<std::string> ExcludeTypes(
265 "exclude-types", cl::desc("Exclude types by regular expression"),
266 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
267 cl::list<std::string> ExcludeSymbols(
268 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
269 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
270 cl::list<std::string> ExcludeCompilands(
271 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
272 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
273
274 cl::list<std::string> IncludeTypes(
275 "include-types",
276 cl::desc("Include only types which match a regular expression"),
277 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
278 cl::list<std::string> IncludeSymbols(
279 "include-symbols",
280 cl::desc("Include only symbols which match a regular expression"),
281 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
282 cl::list<std::string> IncludeCompilands(
283 "include-compilands",
284 cl::desc("Include only compilands those which match a regular expression"),
285 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
286 cl::opt<uint32_t> SizeThreshold(
287 "min-type-size", cl::desc("Displays only those types which are greater "
288 "than or equal to the specified size."),
289 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
290 cl::opt<uint32_t> PaddingThreshold(
291 "min-class-padding", cl::desc("Displays only those classes which have at "
292 "least the specified amount of padding."),
293 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
294 cl::opt<uint32_t> ImmediatePaddingThreshold(
295 "min-class-padding-imm",
296 cl::desc("Displays only those classes which have at least the specified "
297 "amount of immediate padding, ignoring padding internal to bases "
298 "and aggregates."),
299 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
300
301 cl::opt<bool> ExcludeCompilerGenerated(
302 "no-compiler-generated",
303 cl::desc("Don't show compiler generated types and symbols"),
304 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
305 cl::opt<bool>
306 ExcludeSystemLibraries("no-system-libs",
307 cl::desc("Don't show symbols from system libraries"),
308 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
309
310 cl::opt<bool> NoEnumDefs("no-enum-definitions",
311 cl::desc("Don't display full enum definitions"),
312 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
313 }
314
315 cl::OptionCategory FileOptions("Module & File Options");
316
317 namespace bytes {
318 cl::OptionCategory MsfBytes("MSF File Options");
319 cl::OptionCategory DbiBytes("Dbi Stream Options");
320 cl::OptionCategory PdbBytes("PDB Stream Options");
321 cl::OptionCategory Types("Type Options");
322 cl::OptionCategory ModuleCategory("Module Options");
323
324 llvm::Optional<NumberRange> DumpBlockRange;
325 llvm::Optional<NumberRange> DumpByteRange;
326
327 cl::opt<std::string> DumpBlockRangeOpt(
328 "block-range", cl::value_desc("start[-end]"),
329 cl::desc("Dump binary data from specified range of blocks."),
330 cl::sub(BytesSubcommand), cl::cat(MsfBytes));
331
332 cl::opt<std::string>
333 DumpByteRangeOpt("byte-range", cl::value_desc("start[-end]"),
334 cl::desc("Dump binary data from specified range of bytes"),
335 cl::sub(BytesSubcommand), cl::cat(MsfBytes));
336
337 cl::list<std::string>
338 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
339 cl::desc("Dump binary data from specified streams. Format "
340 "is SN[:Start][@Size]"),
341 cl::sub(BytesSubcommand), cl::cat(MsfBytes));
342
343 cl::opt<bool> NameMap("name-map", cl::desc("Dump bytes of PDB Name Map"),
344 cl::sub(BytesSubcommand), cl::cat(PdbBytes));
345 cl::opt<bool> Fpm("fpm", cl::desc("Dump free page map"),
346 cl::sub(BytesSubcommand), cl::cat(MsfBytes));
347
348 cl::opt<bool> SectionContributions("sc", cl::desc("Dump section contributions"),
349 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
350 cl::opt<bool> SectionMap("sm", cl::desc("Dump section map"),
351 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
352 cl::opt<bool> ModuleInfos("modi", cl::desc("Dump module info"),
353 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
354 cl::opt<bool> FileInfo("files", cl::desc("Dump source file info"),
355 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
356 cl::opt<bool> TypeServerMap("type-server", cl::desc("Dump type server map"),
357 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
358 cl::opt<bool> ECData("ec", cl::desc("Dump edit and continue map"),
359 cl::sub(BytesSubcommand), cl::cat(DbiBytes));
360
361 cl::list<uint32_t>
362 TypeIndex("type",
363 cl::desc("Dump the type record with the given type index"),
364 cl::ZeroOrMore, cl::CommaSeparated, cl::sub(BytesSubcommand),
365 cl::cat(TypeCategory));
366 cl::list<uint32_t>
367 IdIndex("id", cl::desc("Dump the id record with the given type index"),
368 cl::ZeroOrMore, cl::CommaSeparated, cl::sub(BytesSubcommand),
369 cl::cat(TypeCategory));
370
371 cl::opt<uint32_t> ModuleIndex(
372 "mod",
373 cl::desc(
374 "Limit options in the Modules category to the specified module index"),
375 cl::Optional, cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
376 cl::opt<bool> ModuleSyms("syms", cl::desc("Dump symbol record substream"),
377 cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
378 cl::opt<bool> ModuleC11("c11-chunks", cl::Hidden,
379 cl::desc("Dump C11 CodeView debug chunks"),
380 cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
381 cl::opt<bool> ModuleC13("chunks",
382 cl::desc("Dump C13 CodeView debug chunk subsection"),
383 cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
384 cl::opt<bool> SplitChunks(
385 "split-chunks",
386 cl::desc(
387 "When dumping debug chunks, show a different section for each chunk"),
388 cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
389 cl::list<std::string> InputFilenames(cl::Positional,
390 cl::desc("<input PDB files>"),
391 cl::OneOrMore, cl::sub(BytesSubcommand));
392
393 } // namespace bytes
394
395 namespace dump {
396
397 cl::OptionCategory MsfOptions("MSF Container Options");
398 cl::OptionCategory TypeOptions("Type Record Options");
399 cl::OptionCategory SymbolOptions("Symbol Options");
400 cl::OptionCategory MiscOptions("Miscellaneous Options");
401
402 // MSF OPTIONS
403 cl::opt<bool> DumpSummary("summary", cl::desc("dump file summary"),
404 cl::cat(MsfOptions), cl::sub(DumpSubcommand));
405 cl::opt<bool> DumpStreams("streams",
406 cl::desc("dump summary of the PDB streams"),
407 cl::cat(MsfOptions), cl::sub(DumpSubcommand));
408 cl::opt<bool> DumpStreamBlocks(
409 "stream-blocks",
410 cl::desc("Add block information to the output of -streams"),
411 cl::cat(MsfOptions), cl::sub(DumpSubcommand));
412 cl::opt<bool> DumpSymbolStats(
413 "sym-stats",
414 cl::desc("Dump a detailed breakdown of symbol usage/size for each module"),
415 cl::cat(MsfOptions), cl::sub(DumpSubcommand));
416
417 cl::opt<bool> DumpUdtStats(
418 "udt-stats",
419 cl::desc("Dump a detailed breakdown of S_UDT record usage / stats"),
420 cl::cat(MsfOptions), cl::sub(DumpSubcommand));
421
422 // TYPE OPTIONS
423 cl::opt<bool> DumpTypes("types",
424 cl::desc("dump CodeView type records from TPI stream"),
425 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
426 cl::opt<bool> DumpTypeData(
427 "type-data",
428 cl::desc("dump CodeView type record raw bytes from TPI stream"),
429 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
430
431 cl::opt<bool> DumpTypeExtras("type-extras",
432 cl::desc("dump type hashes and index offsets"),
433 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
434
435 cl::list<uint32_t> DumpTypeIndex(
436 "type-index", cl::ZeroOrMore, cl::CommaSeparated,
437 cl::desc("only dump types with the specified hexadecimal type index"),
438 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
439
440 cl::opt<bool> DumpIds("ids",
441 cl::desc("dump CodeView type records from IPI stream"),
442 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
443 cl::opt<bool>
444 DumpIdData("id-data",
445 cl::desc("dump CodeView type record raw bytes from IPI stream"),
446 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
447
448 cl::opt<bool> DumpIdExtras("id-extras",
449 cl::desc("dump id hashes and index offsets"),
450 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
451 cl::list<uint32_t> DumpIdIndex(
452 "id-index", cl::ZeroOrMore, cl::CommaSeparated,
453 cl::desc("only dump ids with the specified hexadecimal type index"),
454 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
455
456 cl::opt<bool> DumpTypeDependents(
457 "dependents",
458 cl::desc("In conjunection with -type-index and -id-index, dumps the entire "
459 "dependency graph for the specified index instead of "
460 "just the single record with the specified index"),
461 cl::cat(TypeOptions), cl::sub(DumpSubcommand));
462
463 // SYMBOL OPTIONS
464 cl::opt<bool> DumpGlobals("globals", cl::desc("dump Globals symbol records"),
465 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
466 cl::opt<bool> DumpGlobalExtras("global-extras", cl::desc("dump Globals hashes"),
467 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
468 cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
469 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
470 cl::opt<bool> DumpPublicExtras("public-extras",
471 cl::desc("dump Publics hashes and address maps"),
472 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
473 cl::opt<bool>
474 DumpGSIRecords("gsi-records",
475 cl::desc("dump public / global common record stream"),
476 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
477 cl::opt<bool> DumpSymbols("symbols", cl::desc("dump module symbols"),
478 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
479
480 cl::opt<bool>
481 DumpSymRecordBytes("sym-data",
482 cl::desc("dump CodeView symbol record raw bytes"),
483 cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
484
485 // MODULE & FILE OPTIONS
486 cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
487 cl::cat(FileOptions), cl::sub(DumpSubcommand));
488 cl::opt<bool> DumpModuleFiles(
489 "files",
490 cl::desc("Dump the source files that contribute to each module's."),
491 cl::cat(FileOptions), cl::sub(DumpSubcommand));
492 cl::opt<bool> DumpLines(
493 "l",
494 cl::desc("dump source file/line information (DEBUG_S_LINES subsection)"),
495 cl::cat(FileOptions), cl::sub(DumpSubcommand));
496 cl::opt<bool> DumpInlineeLines(
497 "il",
498 cl::desc("dump inlinee line information (DEBUG_S_INLINEELINES subsection)"),
499 cl::cat(FileOptions), cl::sub(DumpSubcommand));
500 cl::opt<bool> DumpXmi(
501 "xmi",
502 cl::desc(
503 "dump cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
504 cl::cat(FileOptions), cl::sub(DumpSubcommand));
505 cl::opt<bool> DumpXme(
506 "xme",
507 cl::desc(
508 "dump cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
509 cl::cat(FileOptions), cl::sub(DumpSubcommand));
510 cl::opt<uint32_t> DumpModi("modi", cl::Optional,
511 cl::desc("For all options that iterate over "
512 "modules, limit to the specified module"),
513 cl::cat(FileOptions), cl::sub(DumpSubcommand));
514 cl::opt<bool> JustMyCode("jmc", cl::Optional,
515 cl::desc("For all options that iterate over modules, "
516 "ignore modules from system libraries"),
517 cl::cat(FileOptions), cl::sub(DumpSubcommand));
518
519 // MISCELLANEOUS OPTIONS
520 cl::opt<bool> DumpNamedStreams("named-streams",
521 cl::desc("dump PDB named stream table"),
522 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
523
524 cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
525 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
526 cl::opt<bool> DumpStringTableDetails("string-table-details",
527 cl::desc("dump PDB String Table Details"),
528 cl::cat(MiscOptions),
529 cl::sub(DumpSubcommand));
530
531 cl::opt<bool> DumpSectionContribs("section-contribs",
532 cl::desc("dump section contributions"),
533 cl::cat(MiscOptions),
534 cl::sub(DumpSubcommand));
535 cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
536 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
537 cl::opt<bool> DumpSectionHeaders("section-headers",
538 cl::desc("Dump image section headers"),
539 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
540
541 cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
542 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
543
544 cl::list<std::string> InputFilenames(cl::Positional,
545 cl::desc("<input PDB files>"),
546 cl::OneOrMore, cl::sub(DumpSubcommand));
547 }
548
549 namespace yaml2pdb {
550 cl::opt<std::string>
551 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
552 cl::sub(YamlToPdbSubcommand));
553
554 cl::opt<std::string> InputFilename(cl::Positional,
555 cl::desc("<input YAML file>"), cl::Required,
556 cl::sub(YamlToPdbSubcommand));
557 }
558
559 namespace pdb2yaml {
560 cl::opt<bool> All("all",
561 cl::desc("Dump everything we know how to dump."),
562 cl::sub(PdbToYamlSubcommand), cl::init(false));
563 cl::opt<bool> NoFileHeaders("no-file-headers",
564 cl::desc("Do not dump MSF file headers"),
565 cl::sub(PdbToYamlSubcommand), cl::init(false));
566 cl::opt<bool> Minimal("minimal",
567 cl::desc("Don't write fields with default values"),
568 cl::sub(PdbToYamlSubcommand), cl::init(false));
569
570 cl::opt<bool> StreamMetadata(
571 "stream-metadata",
572 cl::desc("Dump the number of streams and each stream's size"),
573 cl::sub(PdbToYamlSubcommand), cl::init(false));
574 cl::opt<bool> StreamDirectory(
575 "stream-directory",
576 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
577 cl::sub(PdbToYamlSubcommand), cl::init(false));
578 cl::opt<bool> PdbStream("pdb-stream",
579 cl::desc("Dump the PDB Stream (Stream 1)"),
580 cl::sub(PdbToYamlSubcommand), cl::init(false));
581
582 cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
583 cl::sub(PdbToYamlSubcommand), cl::init(false));
584
585 cl::opt<bool> DbiStream("dbi-stream",
586 cl::desc("Dump the DBI Stream Headers (Stream 2)"),
587 cl::sub(PdbToYamlSubcommand), cl::init(false));
588
589 cl::opt<bool> TpiStream("tpi-stream",
590 cl::desc("Dump the TPI Stream (Stream 3)"),
591 cl::sub(PdbToYamlSubcommand), cl::init(false));
592
593 cl::opt<bool> IpiStream("ipi-stream",
594 cl::desc("Dump the IPI Stream (Stream 5)"),
595 cl::sub(PdbToYamlSubcommand), cl::init(false));
596
597 // MODULE & FILE OPTIONS
598 cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
599 cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
600 cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
601 cl::cat(FileOptions),
602 cl::sub(PdbToYamlSubcommand));
603 cl::list<ModuleSubsection> DumpModuleSubsections(
604 "subsections", cl::ZeroOrMore, cl::CommaSeparated,
605 cl::desc("dump subsections from each module's debug stream"), ChunkValues,
606 cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
607 cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
608 cl::cat(FileOptions),
609 cl::sub(PdbToYamlSubcommand));
610
611 cl::list<std::string> InputFilename(cl::Positional,
612 cl::desc("<input PDB file>"), cl::Required,
613 cl::sub(PdbToYamlSubcommand));
614 } // namespace pdb2yaml
615
616 namespace analyze {
617 cl::opt<bool> StringTable("hash-collisions", cl::desc("Find hash collisions"),
618 cl::sub(AnalyzeSubcommand), cl::init(false));
619 cl::list<std::string> InputFilename(cl::Positional,
620 cl::desc("<input PDB file>"), cl::Required,
621 cl::sub(AnalyzeSubcommand));
622 }
623
624 namespace merge {
625 cl::list<std::string> InputFilenames(cl::Positional,
626 cl::desc("<input PDB files>"),
627 cl::OneOrMore, cl::sub(MergeSubcommand));
628 cl::opt<std::string>
629 PdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
630 cl::sub(MergeSubcommand));
631 }
632
633 namespace explain {
634 cl::list<std::string> InputFilename(cl::Positional,
635 cl::desc("<input PDB file>"), cl::Required,
636 cl::sub(ExplainSubcommand));
637
638 cl::list<uint64_t> Offsets("offset", cl::desc("The file offset to explain"),
639 cl::sub(ExplainSubcommand), cl::OneOrMore);
640
641 cl::opt<InputFileType> InputType(
642 "input-type", cl::desc("Specify how to interpret the input file"),
643 cl::init(InputFileType::PDBFile), cl::Optional, cl::sub(ExplainSubcommand),
644 cl::values(clEnumValN(InputFileType::PDBFile, "pdb-file",
645 "Treat input as a PDB file (default)"),
646 clEnumValN(InputFileType::PDBStream, "pdb-stream",
647 "Treat input as raw contents of PDB stream"),
648 clEnumValN(InputFileType::DBIStream, "dbi-stream",
649 "Treat input as raw contents of DBI stream"),
650 clEnumValN(InputFileType::Names, "names-stream",
651 "Treat input as raw contents of /names named stream"),
652 clEnumValN(InputFileType::ModuleStream, "mod-stream",
653 "Treat input as raw contents of a module stream")));
654 } // namespace explain
655
656 namespace exportstream {
657 cl::list<std::string> InputFilename(cl::Positional,
658 cl::desc("<input PDB file>"), cl::Required,
659 cl::sub(ExportSubcommand));
660 cl::opt<std::string> OutputFile("out",
661 cl::desc("The file to write the stream to"),
662 cl::Required, cl::sub(ExportSubcommand));
663 cl::opt<std::string>
664 Stream("stream", cl::Required,
665 cl::desc("The index or name of the stream whose contents to export"),
666 cl::sub(ExportSubcommand));
667 cl::opt<bool> ForceName("name",
668 cl::desc("Force the interpretation of -stream as a "
669 "string, even if it is a valid integer"),
670 cl::sub(ExportSubcommand), cl::Optional,
671 cl::init(false));
672 } // namespace exportstream
673 }
674
675 static ExitOnError ExitOnErr;
676
yamlToPdb(StringRef Path)677 static void yamlToPdb(StringRef Path) {
678 BumpPtrAllocator Allocator;
679 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
680 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
681 /*RequiresNullTerminator=*/false);
682
683 if (ErrorOrBuffer.getError()) {
684 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
685 }
686
687 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
688
689 llvm::yaml::Input In(Buffer->getBuffer());
690 pdb::yaml::PdbObject YamlObj(Allocator);
691 In >> YamlObj;
692
693 PDBFileBuilder Builder(Allocator);
694
695 uint32_t BlockSize = 4096;
696 if (YamlObj.Headers.hasValue())
697 BlockSize = YamlObj.Headers->SuperBlock.BlockSize;
698 ExitOnErr(Builder.initialize(BlockSize));
699 // Add each of the reserved streams. We ignore stream metadata in the
700 // yaml, because we will reconstruct our own view of the streams. For
701 // example, the YAML may say that there were 20 streams in the original
702 // PDB, but maybe we only dump a subset of those 20 streams, so we will
703 // have fewer, and the ones we do have may end up with different indices
704 // than the ones in the original PDB. So we just start with a clean slate.
705 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
706 ExitOnErr(Builder.getMsfBuilder().addStream(0));
707
708 StringsAndChecksums Strings;
709 Strings.setStrings(std::make_shared<DebugStringTableSubsection>());
710
711 if (YamlObj.StringTable.hasValue()) {
712 for (auto S : *YamlObj.StringTable)
713 Strings.strings()->insert(S);
714 }
715
716 pdb::yaml::PdbInfoStream DefaultInfoStream;
717 pdb::yaml::PdbDbiStream DefaultDbiStream;
718 pdb::yaml::PdbTpiStream DefaultTpiStream;
719 pdb::yaml::PdbTpiStream DefaultIpiStream;
720
721 const auto &Info = YamlObj.PdbStream.getValueOr(DefaultInfoStream);
722
723 auto &InfoBuilder = Builder.getInfoBuilder();
724 InfoBuilder.setAge(Info.Age);
725 InfoBuilder.setGuid(Info.Guid);
726 InfoBuilder.setSignature(Info.Signature);
727 InfoBuilder.setVersion(Info.Version);
728 for (auto F : Info.Features)
729 InfoBuilder.addFeature(F);
730
731 const auto &Dbi = YamlObj.DbiStream.getValueOr(DefaultDbiStream);
732 auto &DbiBuilder = Builder.getDbiBuilder();
733 DbiBuilder.setAge(Dbi.Age);
734 DbiBuilder.setBuildNumber(Dbi.BuildNumber);
735 DbiBuilder.setFlags(Dbi.Flags);
736 DbiBuilder.setMachineType(Dbi.MachineType);
737 DbiBuilder.setPdbDllRbld(Dbi.PdbDllRbld);
738 DbiBuilder.setPdbDllVersion(Dbi.PdbDllVersion);
739 DbiBuilder.setVersionHeader(Dbi.VerHeader);
740 for (const auto &MI : Dbi.ModInfos) {
741 auto &ModiBuilder = ExitOnErr(DbiBuilder.addModuleInfo(MI.Mod));
742 ModiBuilder.setObjFileName(MI.Obj);
743
744 for (auto S : MI.SourceFiles)
745 ExitOnErr(DbiBuilder.addModuleSourceFile(ModiBuilder, S));
746 if (MI.Modi.hasValue()) {
747 const auto &ModiStream = *MI.Modi;
748 for (auto Symbol : ModiStream.Symbols) {
749 ModiBuilder.addSymbol(
750 Symbol.toCodeViewSymbol(Allocator, CodeViewContainer::Pdb));
751 }
752 }
753
754 // Each module has its own checksum subsection, so scan for it every time.
755 Strings.setChecksums(nullptr);
756 CodeViewYAML::initializeStringsAndChecksums(MI.Subsections, Strings);
757
758 auto CodeViewSubsections = ExitOnErr(CodeViewYAML::toCodeViewSubsectionList(
759 Allocator, MI.Subsections, Strings));
760 for (auto &SS : CodeViewSubsections) {
761 ModiBuilder.addDebugSubsection(SS);
762 }
763 }
764
765 auto &TpiBuilder = Builder.getTpiBuilder();
766 const auto &Tpi = YamlObj.TpiStream.getValueOr(DefaultTpiStream);
767 TpiBuilder.setVersionHeader(Tpi.Version);
768 AppendingTypeTableBuilder TS(Allocator);
769 for (const auto &R : Tpi.Records) {
770 CVType Type = R.toCodeViewRecord(TS);
771 TpiBuilder.addTypeRecord(Type.RecordData, None);
772 }
773
774 const auto &Ipi = YamlObj.IpiStream.getValueOr(DefaultIpiStream);
775 auto &IpiBuilder = Builder.getIpiBuilder();
776 IpiBuilder.setVersionHeader(Ipi.Version);
777 for (const auto &R : Ipi.Records) {
778 CVType Type = R.toCodeViewRecord(TS);
779 IpiBuilder.addTypeRecord(Type.RecordData, None);
780 }
781
782 Builder.getStringTableBuilder().setStrings(*Strings.strings());
783
784 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
785 }
786
loadPDB(StringRef Path,std::unique_ptr<IPDBSession> & Session)787 static PDBFile &loadPDB(StringRef Path, std::unique_ptr<IPDBSession> &Session) {
788 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
789
790 NativeSession *NS = static_cast<NativeSession *>(Session.get());
791 return NS->getPDBFile();
792 }
793
pdb2Yaml(StringRef Path)794 static void pdb2Yaml(StringRef Path) {
795 std::unique_ptr<IPDBSession> Session;
796 auto &File = loadPDB(Path, Session);
797
798 auto O = llvm::make_unique<YAMLOutputStyle>(File);
799 O = llvm::make_unique<YAMLOutputStyle>(File);
800
801 ExitOnErr(O->dump());
802 }
803
dumpRaw(StringRef Path)804 static void dumpRaw(StringRef Path) {
805 InputFile IF = ExitOnErr(InputFile::open(Path));
806
807 auto O = llvm::make_unique<DumpOutputStyle>(IF);
808 ExitOnErr(O->dump());
809 }
810
dumpBytes(StringRef Path)811 static void dumpBytes(StringRef Path) {
812 std::unique_ptr<IPDBSession> Session;
813 auto &File = loadPDB(Path, Session);
814
815 auto O = llvm::make_unique<BytesOutputStyle>(File);
816
817 ExitOnErr(O->dump());
818 }
819
dumpAnalysis(StringRef Path)820 static void dumpAnalysis(StringRef Path) {
821 std::unique_ptr<IPDBSession> Session;
822 auto &File = loadPDB(Path, Session);
823 auto O = llvm::make_unique<AnalysisStyle>(File);
824
825 ExitOnErr(O->dump());
826 }
827
shouldDumpSymLevel(SymLevel Search)828 bool opts::pretty::shouldDumpSymLevel(SymLevel Search) {
829 if (SymTypes.empty())
830 return true;
831 if (llvm::find(SymTypes, Search) != SymTypes.end())
832 return true;
833 if (llvm::find(SymTypes, SymLevel::All) != SymTypes.end())
834 return true;
835 return false;
836 }
837
getTypeLength(const PDBSymbolData & Symbol)838 uint32_t llvm::pdb::getTypeLength(const PDBSymbolData &Symbol) {
839 auto SymbolType = Symbol.getType();
840 const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
841
842 return RawType.getLength();
843 }
844
compareFunctionSymbols(const std::unique_ptr<PDBSymbolFunc> & F1,const std::unique_ptr<PDBSymbolFunc> & F2)845 bool opts::pretty::compareFunctionSymbols(
846 const std::unique_ptr<PDBSymbolFunc> &F1,
847 const std::unique_ptr<PDBSymbolFunc> &F2) {
848 assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
849
850 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
851 return F1->getName() < F2->getName();
852
853 // Note that we intentionally sort in descending order on length, since
854 // long functions are more interesting than short functions.
855 return F1->getLength() > F2->getLength();
856 }
857
compareDataSymbols(const std::unique_ptr<PDBSymbolData> & F1,const std::unique_ptr<PDBSymbolData> & F2)858 bool opts::pretty::compareDataSymbols(
859 const std::unique_ptr<PDBSymbolData> &F1,
860 const std::unique_ptr<PDBSymbolData> &F2) {
861 assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
862
863 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
864 return F1->getName() < F2->getName();
865
866 // Note that we intentionally sort in descending order on length, since
867 // large types are more interesting than short ones.
868 return getTypeLength(*F1) > getTypeLength(*F2);
869 }
870
stringOr(std::string Str,std::string IfEmpty)871 static std::string stringOr(std::string Str, std::string IfEmpty) {
872 return (Str.empty()) ? IfEmpty : Str;
873 }
874
dumpInjectedSources(LinePrinter & Printer,IPDBSession & Session)875 static void dumpInjectedSources(LinePrinter &Printer, IPDBSession &Session) {
876 auto Sources = Session.getInjectedSources();
877 if (0 == Sources->getChildCount()) {
878 Printer.printLine("There are no injected sources.");
879 return;
880 }
881
882 while (auto IS = Sources->getNext()) {
883 Printer.NewLine();
884 std::string File = stringOr(IS->getFileName(), "<null>");
885 uint64_t Size = IS->getCodeByteSize();
886 std::string Obj = stringOr(IS->getObjectFileName(), "<null>");
887 std::string VFName = stringOr(IS->getVirtualFileName(), "<null>");
888 uint32_t CRC = IS->getCrc32();
889
890 std::string CompressionStr;
891 llvm::raw_string_ostream Stream(CompressionStr);
892 Stream << IS->getCompression();
893 WithColor(Printer, PDB_ColorItem::Path).get() << File;
894 Printer << " (";
895 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Size;
896 Printer << " bytes): ";
897 WithColor(Printer, PDB_ColorItem::Keyword).get() << "obj";
898 Printer << "=";
899 WithColor(Printer, PDB_ColorItem::Path).get() << Obj;
900 Printer << ", ";
901 WithColor(Printer, PDB_ColorItem::Keyword).get() << "vname";
902 Printer << "=";
903 WithColor(Printer, PDB_ColorItem::Path).get() << VFName;
904 Printer << ", ";
905 WithColor(Printer, PDB_ColorItem::Keyword).get() << "crc";
906 Printer << "=";
907 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << CRC;
908 Printer << ", ";
909 WithColor(Printer, PDB_ColorItem::Keyword).get() << "compression";
910 Printer << "=";
911 WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Stream.str();
912
913 if (!opts::pretty::ShowInjectedSourceContent)
914 continue;
915
916 // Set the indent level to 0 when printing file content.
917 int Indent = Printer.getIndentLevel();
918 Printer.Unindent(Indent);
919
920 Printer.printLine(IS->getCode());
921
922 // Re-indent back to the original level.
923 Printer.Indent(Indent);
924 }
925 }
926
dumpPretty(StringRef Path)927 static void dumpPretty(StringRef Path) {
928 std::unique_ptr<IPDBSession> Session;
929
930 const auto ReaderType =
931 opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
932 ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
933
934 if (opts::pretty::LoadAddress)
935 Session->setLoadAddress(opts::pretty::LoadAddress);
936
937 auto &Stream = outs();
938 const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
939 ? Stream.has_colors()
940 : opts::pretty::ColorOutput == cl::BOU_TRUE;
941 LinePrinter Printer(2, UseColor, Stream);
942
943 auto GlobalScope(Session->getGlobalScope());
944 if (!GlobalScope)
945 return;
946 std::string FileName(GlobalScope->getSymbolsFileName());
947
948 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
949 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
950 Printer.Indent();
951 uint64_t FileSize = 0;
952
953 Printer.NewLine();
954 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
955 if (!sys::fs::file_size(FileName, FileSize)) {
956 Printer << ": " << FileSize << " bytes";
957 } else {
958 Printer << ": (Unable to obtain file size)";
959 }
960
961 Printer.NewLine();
962 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
963 Printer << ": " << GlobalScope->getGuid();
964
965 Printer.NewLine();
966 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
967 Printer << ": " << GlobalScope->getAge();
968
969 Printer.NewLine();
970 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
971 Printer << ": ";
972 if (GlobalScope->hasCTypes())
973 outs() << "HasCTypes ";
974 if (GlobalScope->hasPrivateSymbols())
975 outs() << "HasPrivateSymbols ";
976 Printer.Unindent();
977
978 if (!opts::pretty::WithName.empty()) {
979 Printer.NewLine();
980 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
981 << "---SYMBOLS & TYPES BY NAME---";
982
983 for (StringRef Name : opts::pretty::WithName) {
984 auto Symbols = GlobalScope->findChildren(
985 PDB_SymType::None, Name, PDB_NameSearchFlags::NS_CaseSensitive);
986 if (!Symbols || Symbols->getChildCount() == 0) {
987 Printer.formatLine("[not found] - {0}", Name);
988 continue;
989 }
990 Printer.formatLine("[{0} occurrences] - {1}", Symbols->getChildCount(),
991 Name);
992
993 AutoIndent Indent(Printer);
994 Printer.NewLine();
995
996 while (auto Symbol = Symbols->getNext()) {
997 switch (Symbol->getSymTag()) {
998 case PDB_SymType::Typedef: {
999 TypedefDumper TD(Printer);
1000 std::unique_ptr<PDBSymbolTypeTypedef> T =
1001 llvm::unique_dyn_cast<PDBSymbolTypeTypedef>(std::move(Symbol));
1002 TD.start(*T);
1003 break;
1004 }
1005 case PDB_SymType::Enum: {
1006 EnumDumper ED(Printer);
1007 std::unique_ptr<PDBSymbolTypeEnum> E =
1008 llvm::unique_dyn_cast<PDBSymbolTypeEnum>(std::move(Symbol));
1009 ED.start(*E);
1010 break;
1011 }
1012 case PDB_SymType::UDT: {
1013 ClassDefinitionDumper CD(Printer);
1014 std::unique_ptr<PDBSymbolTypeUDT> C =
1015 llvm::unique_dyn_cast<PDBSymbolTypeUDT>(std::move(Symbol));
1016 CD.start(*C);
1017 break;
1018 }
1019 case PDB_SymType::BaseClass:
1020 case PDB_SymType::Friend: {
1021 TypeDumper TD(Printer);
1022 Symbol->dump(TD);
1023 break;
1024 }
1025 case PDB_SymType::Function: {
1026 FunctionDumper FD(Printer);
1027 std::unique_ptr<PDBSymbolFunc> F =
1028 llvm::unique_dyn_cast<PDBSymbolFunc>(std::move(Symbol));
1029 FD.start(*F, FunctionDumper::PointerType::None);
1030 break;
1031 }
1032 case PDB_SymType::Data: {
1033 VariableDumper VD(Printer);
1034 std::unique_ptr<PDBSymbolData> D =
1035 llvm::unique_dyn_cast<PDBSymbolData>(std::move(Symbol));
1036 VD.start(*D);
1037 break;
1038 }
1039 case PDB_SymType::PublicSymbol: {
1040 ExternalSymbolDumper ED(Printer);
1041 std::unique_ptr<PDBSymbolPublicSymbol> PS =
1042 llvm::unique_dyn_cast<PDBSymbolPublicSymbol>(std::move(Symbol));
1043 ED.dump(*PS);
1044 break;
1045 }
1046 default:
1047 llvm_unreachable("Unexpected symbol tag!");
1048 }
1049 }
1050 }
1051 llvm::outs().flush();
1052 }
1053
1054 if (opts::pretty::Compilands) {
1055 Printer.NewLine();
1056 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
1057 << "---COMPILANDS---";
1058 if (auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>()) {
1059 Printer.Indent();
1060 CompilandDumper Dumper(Printer);
1061 CompilandDumpFlags options = CompilandDumper::Flags::None;
1062 if (opts::pretty::Lines)
1063 options = options | CompilandDumper::Flags::Lines;
1064 while (auto Compiland = Compilands->getNext())
1065 Dumper.start(*Compiland, options);
1066 Printer.Unindent();
1067 }
1068 }
1069
1070 if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs) {
1071 Printer.NewLine();
1072 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
1073 Printer.Indent();
1074 TypeDumper Dumper(Printer);
1075 Dumper.start(*GlobalScope);
1076 Printer.Unindent();
1077 }
1078
1079 if (opts::pretty::Symbols) {
1080 Printer.NewLine();
1081 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
1082 if (auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>()) {
1083 Printer.Indent();
1084 CompilandDumper Dumper(Printer);
1085 while (auto Compiland = Compilands->getNext())
1086 Dumper.start(*Compiland, true);
1087 Printer.Unindent();
1088 }
1089 }
1090
1091 if (opts::pretty::Globals) {
1092 Printer.NewLine();
1093 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
1094 Printer.Indent();
1095 if (shouldDumpSymLevel(opts::pretty::SymLevel::Functions)) {
1096 if (auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>()) {
1097 FunctionDumper Dumper(Printer);
1098 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
1099 while (auto Function = Functions->getNext()) {
1100 Printer.NewLine();
1101 Dumper.start(*Function, FunctionDumper::PointerType::None);
1102 }
1103 } else {
1104 std::vector<std::unique_ptr<PDBSymbolFunc>> Funcs;
1105 while (auto Func = Functions->getNext())
1106 Funcs.push_back(std::move(Func));
1107 llvm::sort(Funcs.begin(), Funcs.end(),
1108 opts::pretty::compareFunctionSymbols);
1109 for (const auto &Func : Funcs) {
1110 Printer.NewLine();
1111 Dumper.start(*Func, FunctionDumper::PointerType::None);
1112 }
1113 }
1114 }
1115 }
1116 if (shouldDumpSymLevel(opts::pretty::SymLevel::Data)) {
1117 if (auto Vars = GlobalScope->findAllChildren<PDBSymbolData>()) {
1118 VariableDumper Dumper(Printer);
1119 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
1120 while (auto Var = Vars->getNext())
1121 Dumper.start(*Var);
1122 } else {
1123 std::vector<std::unique_ptr<PDBSymbolData>> Datas;
1124 while (auto Var = Vars->getNext())
1125 Datas.push_back(std::move(Var));
1126 llvm::sort(Datas.begin(), Datas.end(),
1127 opts::pretty::compareDataSymbols);
1128 for (const auto &Var : Datas)
1129 Dumper.start(*Var);
1130 }
1131 }
1132 }
1133 if (shouldDumpSymLevel(opts::pretty::SymLevel::Thunks)) {
1134 if (auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>()) {
1135 CompilandDumper Dumper(Printer);
1136 while (auto Thunk = Thunks->getNext())
1137 Dumper.dump(*Thunk);
1138 }
1139 }
1140 Printer.Unindent();
1141 }
1142 if (opts::pretty::Externals) {
1143 Printer.NewLine();
1144 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
1145 Printer.Indent();
1146 ExternalSymbolDumper Dumper(Printer);
1147 Dumper.start(*GlobalScope);
1148 }
1149 if (opts::pretty::Lines) {
1150 Printer.NewLine();
1151 }
1152 if (opts::pretty::InjectedSources) {
1153 Printer.NewLine();
1154 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
1155 << "---INJECTED SOURCES---";
1156 AutoIndent Indent1(Printer);
1157
1158 if (ReaderType == PDB_ReaderType::Native)
1159 Printer.printLine(
1160 "Injected sources are not supported with the native reader.");
1161 else
1162 dumpInjectedSources(Printer, *Session);
1163 }
1164
1165 outs().flush();
1166 }
1167
mergePdbs()1168 static void mergePdbs() {
1169 BumpPtrAllocator Allocator;
1170 MergingTypeTableBuilder MergedTpi(Allocator);
1171 MergingTypeTableBuilder MergedIpi(Allocator);
1172
1173 // Create a Tpi and Ipi type table with all types from all input files.
1174 for (const auto &Path : opts::merge::InputFilenames) {
1175 std::unique_ptr<IPDBSession> Session;
1176 auto &File = loadPDB(Path, Session);
1177 SmallVector<TypeIndex, 128> TypeMap;
1178 SmallVector<TypeIndex, 128> IdMap;
1179 if (File.hasPDBTpiStream()) {
1180 auto &Tpi = ExitOnErr(File.getPDBTpiStream());
1181 ExitOnErr(
1182 codeview::mergeTypeRecords(MergedTpi, TypeMap, Tpi.typeArray()));
1183 }
1184 if (File.hasPDBIpiStream()) {
1185 auto &Ipi = ExitOnErr(File.getPDBIpiStream());
1186 ExitOnErr(codeview::mergeIdRecords(MergedIpi, TypeMap, IdMap,
1187 Ipi.typeArray()));
1188 }
1189 }
1190
1191 // Then write the PDB.
1192 PDBFileBuilder Builder(Allocator);
1193 ExitOnErr(Builder.initialize(4096));
1194 // Add each of the reserved streams. We might not put any data in them,
1195 // but at least they have to be present.
1196 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
1197 ExitOnErr(Builder.getMsfBuilder().addStream(0));
1198
1199 auto &DestTpi = Builder.getTpiBuilder();
1200 auto &DestIpi = Builder.getIpiBuilder();
1201 MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
1202 DestTpi.addTypeRecord(Type.RecordData, None);
1203 });
1204 MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
1205 DestIpi.addTypeRecord(Type.RecordData, None);
1206 });
1207 Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
1208
1209 SmallString<64> OutFile(opts::merge::PdbOutputFile);
1210 if (OutFile.empty()) {
1211 OutFile = opts::merge::InputFilenames[0];
1212 llvm::sys::path::replace_extension(OutFile, "merged.pdb");
1213 }
1214 ExitOnErr(Builder.commit(OutFile));
1215 }
1216
explain()1217 static void explain() {
1218 std::unique_ptr<IPDBSession> Session;
1219 InputFile IF =
1220 ExitOnErr(InputFile::open(opts::explain::InputFilename.front(), true));
1221
1222 for (uint64_t Off : opts::explain::Offsets) {
1223 auto O = llvm::make_unique<ExplainOutputStyle>(IF, Off);
1224
1225 ExitOnErr(O->dump());
1226 }
1227 }
1228
exportStream()1229 static void exportStream() {
1230 std::unique_ptr<IPDBSession> Session;
1231 PDBFile &File = loadPDB(opts::exportstream::InputFilename.front(), Session);
1232
1233 std::unique_ptr<MappedBlockStream> SourceStream;
1234 uint32_t Index = 0;
1235 bool Success = false;
1236 std::string OutFileName = opts::exportstream::OutputFile;
1237
1238 if (!opts::exportstream::ForceName) {
1239 // First try to parse it as an integer, if it fails fall back to treating it
1240 // as a named stream.
1241 if (to_integer(opts::exportstream::Stream, Index)) {
1242 if (Index >= File.getNumStreams()) {
1243 errs() << "Error: " << Index << " is not a valid stream index.\n";
1244 exit(1);
1245 }
1246 Success = true;
1247 outs() << "Dumping contents of stream index " << Index << " to file "
1248 << OutFileName << ".\n";
1249 }
1250 }
1251
1252 if (!Success) {
1253 InfoStream &IS = cantFail(File.getPDBInfoStream());
1254 Index = ExitOnErr(IS.getNamedStreamIndex(opts::exportstream::Stream));
1255 outs() << "Dumping contents of stream '" << opts::exportstream::Stream
1256 << "' (index " << Index << ") to file " << OutFileName << ".\n";
1257 }
1258
1259 SourceStream = MappedBlockStream::createIndexedStream(
1260 File.getMsfLayout(), File.getMsfBuffer(), Index, File.getAllocator());
1261 auto OutFile = ExitOnErr(
1262 FileOutputBuffer::create(OutFileName, SourceStream->getLength()));
1263 FileBufferByteStream DestStream(std::move(OutFile), llvm::support::little);
1264 BinaryStreamWriter Writer(DestStream);
1265 ExitOnErr(Writer.writeStreamRef(*SourceStream));
1266 ExitOnErr(DestStream.commit());
1267 }
1268
parseRange(StringRef Str,Optional<opts::bytes::NumberRange> & Parsed)1269 static bool parseRange(StringRef Str,
1270 Optional<opts::bytes::NumberRange> &Parsed) {
1271 if (Str.empty())
1272 return true;
1273
1274 llvm::Regex R("^([^-]+)(-([^-]+))?$");
1275 llvm::SmallVector<llvm::StringRef, 2> Matches;
1276 if (!R.match(Str, &Matches))
1277 return false;
1278
1279 Parsed.emplace();
1280 if (!to_integer(Matches[1], Parsed->Min))
1281 return false;
1282
1283 if (!Matches[3].empty()) {
1284 Parsed->Max.emplace();
1285 if (!to_integer(Matches[3], *Parsed->Max))
1286 return false;
1287 }
1288 return true;
1289 }
1290
simplifyChunkList(llvm::cl::list<opts::ModuleSubsection> & Chunks)1291 static void simplifyChunkList(llvm::cl::list<opts::ModuleSubsection> &Chunks) {
1292 // If this list contains "All" plus some other stuff, remove the other stuff
1293 // and just keep "All" in the list.
1294 if (!llvm::is_contained(Chunks, opts::ModuleSubsection::All))
1295 return;
1296 Chunks.reset();
1297 Chunks.push_back(opts::ModuleSubsection::All);
1298 }
1299
main(int Argc,const char ** Argv)1300 int main(int Argc, const char **Argv) {
1301 InitLLVM X(Argc, Argv);
1302 ExitOnErr.setBanner("llvm-pdbutil: ");
1303
1304 cl::ParseCommandLineOptions(Argc, Argv, "LLVM PDB Dumper\n");
1305
1306 if (opts::BytesSubcommand) {
1307 if (!parseRange(opts::bytes::DumpBlockRangeOpt,
1308 opts::bytes::DumpBlockRange)) {
1309 errs() << "Argument '" << opts::bytes::DumpBlockRangeOpt
1310 << "' invalid format.\n";
1311 errs().flush();
1312 exit(1);
1313 }
1314 if (!parseRange(opts::bytes::DumpByteRangeOpt,
1315 opts::bytes::DumpByteRange)) {
1316 errs() << "Argument '" << opts::bytes::DumpByteRangeOpt
1317 << "' invalid format.\n";
1318 errs().flush();
1319 exit(1);
1320 }
1321 }
1322
1323 if (opts::DumpSubcommand) {
1324 if (opts::dump::RawAll) {
1325 opts::dump::DumpGlobals = true;
1326 opts::dump::DumpInlineeLines = true;
1327 opts::dump::DumpIds = true;
1328 opts::dump::DumpIdExtras = true;
1329 opts::dump::DumpLines = true;
1330 opts::dump::DumpModules = true;
1331 opts::dump::DumpModuleFiles = true;
1332 opts::dump::DumpPublics = true;
1333 opts::dump::DumpSectionContribs = true;
1334 opts::dump::DumpSectionHeaders = true;
1335 opts::dump::DumpSectionMap = true;
1336 opts::dump::DumpStreams = true;
1337 opts::dump::DumpStreamBlocks = true;
1338 opts::dump::DumpStringTable = true;
1339 opts::dump::DumpStringTableDetails = true;
1340 opts::dump::DumpSummary = true;
1341 opts::dump::DumpSymbols = true;
1342 opts::dump::DumpSymbolStats = true;
1343 opts::dump::DumpTypes = true;
1344 opts::dump::DumpTypeExtras = true;
1345 opts::dump::DumpUdtStats = true;
1346 opts::dump::DumpXme = true;
1347 opts::dump::DumpXmi = true;
1348 }
1349 }
1350 if (opts::PdbToYamlSubcommand) {
1351 if (opts::pdb2yaml::All) {
1352 opts::pdb2yaml::StreamMetadata = true;
1353 opts::pdb2yaml::StreamDirectory = true;
1354 opts::pdb2yaml::PdbStream = true;
1355 opts::pdb2yaml::StringTable = true;
1356 opts::pdb2yaml::DbiStream = true;
1357 opts::pdb2yaml::TpiStream = true;
1358 opts::pdb2yaml::IpiStream = true;
1359 opts::pdb2yaml::DumpModules = true;
1360 opts::pdb2yaml::DumpModuleFiles = true;
1361 opts::pdb2yaml::DumpModuleSyms = true;
1362 opts::pdb2yaml::DumpModuleSubsections.push_back(
1363 opts::ModuleSubsection::All);
1364 }
1365 simplifyChunkList(opts::pdb2yaml::DumpModuleSubsections);
1366
1367 if (opts::pdb2yaml::DumpModuleSyms || opts::pdb2yaml::DumpModuleFiles)
1368 opts::pdb2yaml::DumpModules = true;
1369
1370 if (opts::pdb2yaml::DumpModules)
1371 opts::pdb2yaml::DbiStream = true;
1372 }
1373
1374 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
1375
1376 if (opts::PdbToYamlSubcommand) {
1377 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
1378 } else if (opts::YamlToPdbSubcommand) {
1379 if (opts::yaml2pdb::YamlPdbOutputFile.empty()) {
1380 SmallString<16> OutputFilename(opts::yaml2pdb::InputFilename.getValue());
1381 sys::path::replace_extension(OutputFilename, ".pdb");
1382 opts::yaml2pdb::YamlPdbOutputFile = OutputFilename.str();
1383 }
1384 yamlToPdb(opts::yaml2pdb::InputFilename);
1385 } else if (opts::AnalyzeSubcommand) {
1386 dumpAnalysis(opts::analyze::InputFilename.front());
1387 } else if (opts::PrettySubcommand) {
1388 if (opts::pretty::Lines)
1389 opts::pretty::Compilands = true;
1390
1391 if (opts::pretty::All) {
1392 opts::pretty::Compilands = true;
1393 opts::pretty::Symbols = true;
1394 opts::pretty::Globals = true;
1395 opts::pretty::Types = true;
1396 opts::pretty::Externals = true;
1397 opts::pretty::Lines = true;
1398 }
1399
1400 if (opts::pretty::Types) {
1401 opts::pretty::Classes = true;
1402 opts::pretty::Typedefs = true;
1403 opts::pretty::Enums = true;
1404 }
1405
1406 // When adding filters for excluded compilands and types, we need to
1407 // remember that these are regexes. So special characters such as * and \
1408 // need to be escaped in the regex. In the case of a literal \, this means
1409 // it needs to be escaped again in the C++. So matching a single \ in the
1410 // input requires 4 \es in the C++.
1411 if (opts::pretty::ExcludeCompilerGenerated) {
1412 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
1413 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
1414 }
1415 if (opts::pretty::ExcludeSystemLibraries) {
1416 opts::pretty::ExcludeCompilands.push_back(
1417 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
1418 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
1419 opts::pretty::ExcludeCompilands.push_back(
1420 "d:\\\\th.obj.x86fre\\\\minkernel");
1421 }
1422 llvm::for_each(opts::pretty::InputFilenames, dumpPretty);
1423 } else if (opts::DumpSubcommand) {
1424 llvm::for_each(opts::dump::InputFilenames, dumpRaw);
1425 } else if (opts::BytesSubcommand) {
1426 llvm::for_each(opts::bytes::InputFilenames, dumpBytes);
1427 } else if (opts::MergeSubcommand) {
1428 if (opts::merge::InputFilenames.size() < 2) {
1429 errs() << "merge subcommand requires at least 2 input files.\n";
1430 exit(1);
1431 }
1432 mergePdbs();
1433 } else if (opts::ExplainSubcommand) {
1434 explain();
1435 } else if (opts::ExportSubcommand) {
1436 exportStream();
1437 }
1438
1439 outs().flush();
1440 return 0;
1441 }
1442