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