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