11022Sache//===- llvm-pdbutil.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
278858Sjoerg//
31022Sache// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41022Sache// See https://llvm.org/LICENSE.txt for license information.
51022Sache// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61022Sache//
71022Sache//===----------------------------------------------------------------------===//
81022Sache//
91022Sache// Dumps debug information present in PDB files.
101022Sache//
111022Sache//===----------------------------------------------------------------------===//
121022Sache
131022Sache#include "llvm-pdbutil.h"
141533Sjoerg
151533Sjoerg#include "BytesOutputStyle.h"
161533Sjoerg#include "DumpOutputStyle.h"
171533Sjoerg#include "ExplainOutputStyle.h"
181533Sjoerg#include "OutputStyle.h"
191533Sjoerg#include "PrettyClassDefinitionDumper.h"
201533Sjoerg#include "PrettyCompilandDumper.h"
211533Sjoerg#include "PrettyEnumDumper.h"
221533Sjoerg#include "PrettyExternalSymbolDumper.h"
231533Sjoerg#include "PrettyFunctionDumper.h"
241533Sjoerg#include "PrettyTypeDumper.h"
2555541Skato#include "PrettyTypedefDumper.h"
2655541Skato#include "PrettyVariableDumper.h"
271022Sache#include "YAMLOutputStyle.h"
281022Sache
2987992Sjoerg#include "llvm/ADT/ArrayRef.h"
3087992Sjoerg#include "llvm/ADT/STLExtras.h"
3187992Sjoerg#include "llvm/ADT/StringExtras.h"
321022Sache#include "llvm/BinaryFormat/Magic.h"
3329531Scharnier#include "llvm/Config/config.h"
3429531Scharnier#include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
3579111Sjoerg#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
3629531Scharnier#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
3769793Sobrien#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
381022Sache#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
391022Sache#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
4029531Scharnier#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
4187992Sjoerg#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
421022Sache#include "llvm/DebugInfo/MSF/MSFBuilder.h"
431022Sache#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
4479111Sjoerg#include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
4579111Sjoerg#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
461022Sache#include "llvm/DebugInfo/PDB/IPDBInjectedSource.h"
471022Sache#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
4887992Sjoerg#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
4987992Sjoerg#include "llvm/DebugInfo/PDB/IPDBSession.h"
501022Sache#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
511022Sache#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
5287992Sjoerg#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
531022Sache#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
5487992Sjoerg#include "llvm/DebugInfo/PDB/Native/InputFile.h"
5587992Sjoerg#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
5687992Sjoerg#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
5787992Sjoerg#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
5887992Sjoerg#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
591137Sache#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
601137Sache#include "llvm/DebugInfo/PDB/Native/RawError.h"
611137Sache#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
621022Sache#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
631022Sache#include "llvm/DebugInfo/PDB/PDB.h"
641022Sache#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
651022Sache#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
661022Sache#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
671022Sache#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
681022Sache#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
691022Sache#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
701022Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeBuiltin.h"
711022Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
721022Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
731022Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
741137Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
751022Sache#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
761022Sache#include "llvm/Support/BinaryByteStream.h"
7729531Scharnier#include "llvm/Support/COM.h"
7887992Sjoerg#include "llvm/Support/CommandLine.h"
791022Sache#include "llvm/Support/ConvertUTF.h"
801022Sache#include "llvm/Support/FileOutputBuffer.h"
811022Sache#include "llvm/Support/FileSystem.h"
821022Sache#include "llvm/Support/Format.h"
831022Sache#include "llvm/Support/InitLLVM.h"
8487992Sjoerg#include "llvm/Support/LineIterator.h"
8587992Sjoerg#include "llvm/Support/MemoryBuffer.h"
861533Sjoerg#include "llvm/Support/Path.h"
871022Sache#include "llvm/Support/PrettyStackTrace.h"
881533Sjoerg#include "llvm/Support/Process.h"
8929531Scharnier#include "llvm/Support/Regex.h"
901533Sjoerg#include "llvm/Support/ScopedPrinter.h"
911533Sjoerg#include "llvm/Support/Signals.h"
921533Sjoerg#include "llvm/Support/raw_ostream.h"
931533Sjoerg
941533Sjoergusing namespace llvm;
958857Srgrimesusing namespace llvm::codeview;
9687992Sjoergusing namespace llvm::msf;
9787992Sjoergusing namespace llvm::pdb;
9887992Sjoerg
9987992Sjoergnamespace opts {
10087992Sjoerg
1011533Sjoergcl::SubCommand DumpSubcommand("dump", "Dump MSF and CodeView debug info");
1021533Sjoergcl::SubCommand BytesSubcommand("bytes", "Dump raw bytes from the PDB file");
1031533Sjoerg
1041533Sjoergcl::SubCommand DiaDumpSubcommand("diadump",
1051533Sjoerg                                 "Dump debug information using a DIA-like API");
10687992Sjoerg
1071533Sjoergcl::SubCommand
1081533Sjoerg    PrettySubcommand("pretty",
1091022Sache                     "Dump semantic information about types and symbols");
1101022Sache
1111022Sachecl::SubCommand
1121533Sjoerg    YamlToPdbSubcommand("yaml2pdb",
1131022Sache                        "Generate a PDB file from a YAML description");
11487992Sjoergcl::SubCommand
11587992Sjoerg    PdbToYamlSubcommand("pdb2yaml",
1161022Sache                        "Generate a detailed YAML description of a PDB File");
1171022Sache
1181022Sachecl::SubCommand MergeSubcommand("merge",
1191533Sjoerg                               "Merge multiple PDBs into a single PDB");
1201022Sache
12187992Sjoergcl::SubCommand ExplainSubcommand("explain",
1221022Sache                                 "Explain the meaning of a file offset");
12387992Sjoerg
1241022Sachecl::SubCommand ExportSubcommand("export",
1251022Sache                                "Write binary data from a stream to a file");
12687992Sjoerg
1271022Sachecl::OptionCategory TypeCategory("Symbol Type Options");
1281022Sachecl::OptionCategory FilterCategory("Filtering and Sorting Options");
1291022Sachecl::OptionCategory OtherOptions("Other Options");
1301022Sache
1311022Sachecl::ValuesClass ChunkValues = cl::values(
1321022Sache    clEnumValN(ModuleSubsection::CrossScopeExports, "cme",
1331022Sache               "Cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
1341022Sache    clEnumValN(ModuleSubsection::CrossScopeImports, "cmi",
1351022Sache               "Cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
1361022Sache    clEnumValN(ModuleSubsection::FileChecksums, "fc",
1371022Sache               "File checksums (DEBUG_S_CHECKSUMS subsection)"),
1381022Sache    clEnumValN(ModuleSubsection::InlineeLines, "ilines",
1391022Sache               "Inlinee lines (DEBUG_S_INLINEELINES subsection)"),
1401022Sache    clEnumValN(ModuleSubsection::Lines, "lines",
14187992Sjoerg               "Lines (DEBUG_S_LINES subsection)"),
14287992Sjoerg    clEnumValN(ModuleSubsection::StringTable, "strings",
14387992Sjoerg               "String Table (DEBUG_S_STRINGTABLE subsection) (not "
14479111Sjoerg               "typically present in PDB file)"),
14579111Sjoerg    clEnumValN(ModuleSubsection::FrameData, "frames",
14687992Sjoerg               "Frame Data (DEBUG_S_FRAMEDATA subsection)"),
14787992Sjoerg    clEnumValN(ModuleSubsection::Symbols, "symbols",
14887992Sjoerg               "Symbols (DEBUG_S_SYMBOLS subsection) (not typically "
14987992Sjoerg               "present in PDB file)"),
15087992Sjoerg    clEnumValN(ModuleSubsection::CoffSymbolRVAs, "rvas",
1511022Sache               "COFF Symbol RVAs (DEBUG_S_COFF_SYMBOL_RVA subsection)"),
15287992Sjoerg    clEnumValN(ModuleSubsection::Unknown, "unknown",
15387992Sjoerg               "Any subsection not covered by another option"),
15487992Sjoerg    clEnumValN(ModuleSubsection::All, "all", "All known subsections"));
15587992Sjoerg
15687992Sjoergnamespace diadump {
15787992Sjoergcl::list<std::string> InputFilenames(cl::Positional,
1581022Sache                                     cl::desc("<input PDB files>"),
15987992Sjoerg                                     cl::OneOrMore, cl::sub(DiaDumpSubcommand));
16087992Sjoerg
16187992Sjoergcl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
16287992Sjoerg                     cl::sub(DiaDumpSubcommand));
16387992Sjoerg
16487992Sjoergstatic cl::opt<bool>
16587992Sjoerg    ShowClassHierarchy("hierarchy", cl::desc("Show lexical and class parents"),
1661022Sache                       cl::sub(DiaDumpSubcommand));
1671022Sachestatic cl::opt<bool> NoSymIndexIds(
16887992Sjoerg    "no-ids",
16987992Sjoerg    cl::desc("Don't show any SymIndexId fields (overrides -hierarchy)"),
17087992Sjoerg    cl::sub(DiaDumpSubcommand));
17187992Sjoerg
17287992Sjoergstatic cl::opt<bool>
17387992Sjoerg    Recurse("recurse",
17487992Sjoerg            cl::desc("When dumping a SymIndexId, dump the full details of the "
1751022Sache                     "corresponding record"),
1761022Sache            cl::sub(DiaDumpSubcommand));
17787992Sjoerg
17887992Sjoergstatic cl::opt<bool> Enums("enums", cl::desc("Dump enum types"),
1791022Sache                           cl::sub(DiaDumpSubcommand));
1801022Sachestatic cl::opt<bool> Pointers("pointers", cl::desc("Dump enum types"),
18187992Sjoerg                              cl::sub(DiaDumpSubcommand));
18287992Sjoergstatic cl::opt<bool> UDTs("udts", cl::desc("Dump udt types"),
1831022Sache                          cl::sub(DiaDumpSubcommand));
1841022Sachestatic cl::opt<bool> Compilands("compilands",
18587992Sjoerg                                cl::desc("Dump compiland information"),
18687992Sjoerg                                cl::sub(DiaDumpSubcommand));
1871022Sachestatic cl::opt<bool> Funcsigs("funcsigs",
1881022Sache                              cl::desc("Dump function signature information"),
18987992Sjoerg                              cl::sub(DiaDumpSubcommand));
19087992Sjoergstatic cl::opt<bool> Arrays("arrays", cl::desc("Dump array types"),
19187992Sjoerg                            cl::sub(DiaDumpSubcommand));
1921022Sachestatic cl::opt<bool> VTShapes("vtshapes", cl::desc("Dump virtual table shapes"),
1931022Sache                              cl::sub(DiaDumpSubcommand));
19487992Sjoergstatic cl::opt<bool> Typedefs("typedefs", cl::desc("Dump typedefs"),
19561154Sphk                              cl::sub(DiaDumpSubcommand));
19661154Sphk} // namespace diadump
19761154Sphk
19887992SjoergFilterOptions Filters;
1991022Sache
2001022Sachenamespace pretty {
2011022Sachecl::list<std::string> InputFilenames(cl::Positional,
2021022Sache                                     cl::desc("<input PDB files>"),
2031022Sache                                     cl::OneOrMore, cl::sub(PrettySubcommand));
2041022Sache
20587992Sjoergcl::opt<bool> InjectedSources("injected-sources",
20687992Sjoerg                              cl::desc("Display injected sources"),
20787992Sjoerg                              cl::cat(OtherOptions), cl::sub(PrettySubcommand));
20887992Sjoergcl::opt<bool> ShowInjectedSourceContent(
20987992Sjoerg    "injected-source-content",
21087992Sjoerg    cl::desc("When displaying an injected source, display the file content"),
21187992Sjoerg    cl::cat(OtherOptions), cl::sub(PrettySubcommand));
21287992Sjoerg
21387992Sjoergcl::list<std::string> WithName(
21487992Sjoerg    "with-name",
21587992Sjoerg    cl::desc("Display any symbol or type with the specified exact name"),
21687992Sjoerg    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
21787992Sjoerg
2181022Sachecl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
2191022Sache                         cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22087992Sjoergcl::opt<bool> Symbols("module-syms",
22187992Sjoerg                      cl::desc("Display symbols for each compiland"),
2221022Sache                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22387992Sjoergcl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
22487992Sjoerg                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22587992Sjoergcl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
22687992Sjoerg                        cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22787992Sjoergcl::list<SymLevel> SymTypes(
22887992Sjoerg    "sym-types", cl::desc("Type of symbols to dump (default all)"),
22987992Sjoerg    cl::cat(TypeCategory), cl::sub(PrettySubcommand),
23087992Sjoerg    cl::values(
23187992Sjoerg        clEnumValN(SymLevel::Thunks, "thunks", "Display thunk symbols"),
23287992Sjoerg        clEnumValN(SymLevel::Data, "data", "Display data symbols"),
23387992Sjoerg        clEnumValN(SymLevel::Functions, "funcs", "Display function symbols"),
23487992Sjoerg        clEnumValN(SymLevel::All, "all", "Display all symbols (default)")));
23587992Sjoerg
23687992Sjoergcl::opt<bool>
23787992Sjoerg    Types("types",
23887992Sjoerg          cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
23987992Sjoerg          cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24087992Sjoergcl::opt<bool> Classes("classes", cl::desc("Display class types"),
24187992Sjoerg                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24287992Sjoergcl::opt<bool> Enums("enums", cl::desc("Display enum types"),
24387992Sjoerg                    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24487992Sjoergcl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
24587992Sjoerg                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24687992Sjoergcl::opt<bool> Funcsigs("funcsigs", cl::desc("Display function signatures"),
24787992Sjoerg                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24829531Scharniercl::opt<bool> Pointers("pointers", cl::desc("Display pointer types"),
24987992Sjoerg                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
25087992Sjoergcl::opt<bool> Arrays("arrays", cl::desc("Display arrays"),
25187992Sjoerg                     cl::cat(TypeCategory), cl::sub(PrettySubcommand));
25287992Sjoergcl::opt<bool> VTShapes("vtshapes", cl::desc("Display vftable shapes"),
25387992Sjoerg                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
25487992Sjoerg
25578858Sjoergcl::opt<SymbolSortMode> SymbolOrder(
25687992Sjoerg    "symbol-order", cl::desc("symbol sort order"),
25787992Sjoerg    cl::init(SymbolSortMode::None),
25887992Sjoerg    cl::values(clEnumValN(SymbolSortMode::None, "none",
25987992Sjoerg                          "Undefined / no particular sort order"),
26087992Sjoerg               clEnumValN(SymbolSortMode::Name, "name", "Sort symbols by name"),
26187992Sjoerg               clEnumValN(SymbolSortMode::Size, "size",
26287992Sjoerg                          "Sort symbols by size")),
26387992Sjoerg    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
26487992Sjoerg
2651022Sachecl::opt<ClassSortMode> ClassOrder(
26687992Sjoerg    "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
26787992Sjoerg    cl::values(
26887992Sjoerg        clEnumValN(ClassSortMode::None, "none",
26987992Sjoerg                   "Undefined / no particular sort order"),
27087992Sjoerg        clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
27187992Sjoerg        clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
27287992Sjoerg        clEnumValN(ClassSortMode::Padding, "padding",
27387992Sjoerg                   "Sort classes by amount of padding"),
27487992Sjoerg        clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
27587992Sjoerg                   "Sort classes by percentage of space consumed by padding"),
27687992Sjoerg        clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
27787992Sjoerg                   "Sort classes by amount of immediate padding"),
27887992Sjoerg        clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
27987992Sjoerg                   "Sort classes by percentage of space consumed by immediate "
28087992Sjoerg                   "padding")),
28187992Sjoerg    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
2821022Sache
28387992Sjoergcl::opt<ClassDefinitionFormat> ClassFormat(
2841022Sache    "class-definitions", cl::desc("Class definition format"),
28555541Skato    cl::init(ClassDefinitionFormat::All),
28655541Skato    cl::values(
28755541Skato        clEnumValN(ClassDefinitionFormat::All, "all",
2881022Sache                   "Display all class members including data, constants, "
2891022Sache                   "typedefs, functions, etc"),
2901022Sache        clEnumValN(ClassDefinitionFormat::Layout, "layout",
2911022Sache                   "Only display members that contribute to class size."),
29279161Sjoerg        clEnumValN(ClassDefinitionFormat::None, "none",
2931022Sache                   "Don't display class definitions")),
29461154Sphk    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
2951022Sachecl::opt<uint32_t> ClassRecursionDepth(
2961022Sache    "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
29779161Sjoerg    cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
29887992Sjoerg
2991022Sachecl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
30087992Sjoerg                    cl::sub(PrettySubcommand));
3011022Sachecl::opt<bool>
3021022Sache    All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
3031022Sache        cl::cat(TypeCategory), cl::sub(PrettySubcommand));
3041022Sache
3051022Sachecl::opt<uint64_t> LoadAddress(
3061022Sache    "load-address",
3071022Sache    cl::desc("Assume the module is loaded at the specified address"),
3081022Sache    cl::cat(OtherOptions), cl::sub(PrettySubcommand));
30955541Skatocl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
31055541Skato                     cl::cat(OtherOptions), cl::sub(PrettySubcommand));
31155541Skatocl::opt<cl::boolOrDefault>
3121022Sache    ColorOutput("color-output",
3131022Sache                cl::desc("Override use of color (default = isatty)"),
3141022Sache                cl::cat(OtherOptions), cl::sub(PrettySubcommand));
3151022Sachecl::list<std::string>
3161022Sache    ExcludeTypes("exclude-types",
3171022Sache                 cl::desc("Exclude types by regular expression"),
3181022Sache                 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3191183Srgrimescl::list<std::string>
3201183Srgrimes    ExcludeSymbols("exclude-symbols",
32187992Sjoerg                   cl::desc("Exclude symbols by regular expression"),
32287992Sjoerg                   cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3231022Sachecl::list<std::string>
3241022Sache    ExcludeCompilands("exclude-compilands",
3251022Sache                      cl::desc("Exclude compilands by regular expression"),
3261022Sache                      cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3271022Sache
3281022Sachecl::list<std::string> IncludeTypes(
32979111Sjoerg    "include-types",
33079111Sjoerg    cl::desc("Include only types which match a regular expression"),
33179111Sjoerg    cl::cat(FilterCategory), cl::sub(PrettySubcommand));
33279111Sjoergcl::list<std::string> IncludeSymbols(
33379111Sjoerg    "include-symbols",
33487992Sjoerg    cl::desc("Include only symbols which match a regular expression"),
33579111Sjoerg    cl::cat(FilterCategory), cl::sub(PrettySubcommand));
33679111Sjoergcl::list<std::string> IncludeCompilands(
33779111Sjoerg    "include-compilands",
33879111Sjoerg    cl::desc("Include only compilands those which match a regular expression"),
3391022Sache    cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3401022Sachecl::opt<uint32_t> SizeThreshold(
3411022Sache    "min-type-size", cl::desc("Displays only those types which are greater "
3421022Sache                              "than or equal to the specified size."),
3431022Sache    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3441022Sachecl::opt<uint32_t> PaddingThreshold(
3451022Sache    "min-class-padding", cl::desc("Displays only those classes which have at "
3461022Sache                                  "least the specified amount of padding."),
3471022Sache    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3481022Sachecl::opt<uint32_t> ImmediatePaddingThreshold(
3491022Sache    "min-class-padding-imm",
3501022Sache    cl::desc("Displays only those classes which have at least the specified "
3511022Sache             "amount of immediate padding, ignoring padding internal to bases "
3521022Sache             "and aggregates."),
3531022Sache    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
3541022Sache
35579111Sjoergcl::opt<bool> ExcludeCompilerGenerated(
35679111Sjoerg    "no-compiler-generated",
35779111Sjoerg    cl::desc("Don't show compiler generated types and symbols"),
35879111Sjoerg    cl::cat(FilterCategory), cl::sub(PrettySubcommand));
35979111Sjoergcl::opt<bool>
36079111Sjoerg    ExcludeSystemLibraries("no-system-libs",
36179111Sjoerg                           cl::desc("Don't show symbols from system libraries"),
36279111Sjoerg                           cl::cat(FilterCategory), cl::sub(PrettySubcommand));
36379111Sjoerg
36479111Sjoergcl::opt<bool> NoEnumDefs("no-enum-definitions",
36579111Sjoerg                         cl::desc("Don't display full enum definitions"),
36679111Sjoerg                         cl::cat(FilterCategory), cl::sub(PrettySubcommand));
36779111Sjoerg}
36879111Sjoerg
36979111Sjoergcl::OptionCategory FileOptions("Module & File Options");
3701022Sache
371namespace bytes {
372cl::OptionCategory MsfBytes("MSF File Options");
373cl::OptionCategory DbiBytes("Dbi Stream Options");
374cl::OptionCategory PdbBytes("PDB Stream Options");
375cl::OptionCategory Types("Type Options");
376cl::OptionCategory ModuleCategory("Module Options");
377
378std::optional<NumberRange> DumpBlockRange;
379std::optional<NumberRange> DumpByteRange;
380
381cl::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
386cl::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
391cl::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
397cl::opt<bool> NameMap("name-map", cl::desc("Dump bytes of PDB Name Map"),
398                      cl::sub(BytesSubcommand), cl::cat(PdbBytes));
399cl::opt<bool> Fpm("fpm", cl::desc("Dump free page map"),
400                  cl::sub(BytesSubcommand), cl::cat(MsfBytes));
401
402cl::opt<bool> SectionContributions("sc", cl::desc("Dump section contributions"),
403                                   cl::sub(BytesSubcommand), cl::cat(DbiBytes));
404cl::opt<bool> SectionMap("sm", cl::desc("Dump section map"),
405                         cl::sub(BytesSubcommand), cl::cat(DbiBytes));
406cl::opt<bool> ModuleInfos("modi", cl::desc("Dump module info"),
407                          cl::sub(BytesSubcommand), cl::cat(DbiBytes));
408cl::opt<bool> FileInfo("files", cl::desc("Dump source file info"),
409                       cl::sub(BytesSubcommand), cl::cat(DbiBytes));
410cl::opt<bool> TypeServerMap("type-server", cl::desc("Dump type server map"),
411                            cl::sub(BytesSubcommand), cl::cat(DbiBytes));
412cl::opt<bool> ECData("ec", cl::desc("Dump edit and continue map"),
413                     cl::sub(BytesSubcommand), cl::cat(DbiBytes));
414
415cl::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));
418cl::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
423cl::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));
428cl::opt<bool> ModuleSyms("syms", cl::desc("Dump symbol record substream"),
429                         cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
430cl::opt<bool> ModuleC11("c11-chunks", cl::Hidden,
431                        cl::desc("Dump C11 CodeView debug chunks"),
432                        cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
433cl::opt<bool> ModuleC13("chunks",
434                        cl::desc("Dump C13 CodeView debug chunk subsection"),
435                        cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
436cl::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));
441cl::list<std::string> InputFilenames(cl::Positional,
442                                     cl::desc("<input PDB files>"),
443                                     cl::OneOrMore, cl::sub(BytesSubcommand));
444
445} // namespace bytes
446
447namespace dump {
448
449cl::OptionCategory MsfOptions("MSF Container Options");
450cl::OptionCategory TypeOptions("Type Record Options");
451cl::OptionCategory SymbolOptions("Symbol Options");
452cl::OptionCategory MiscOptions("Miscellaneous Options");
453
454// MSF OPTIONS
455cl::opt<bool> DumpSummary("summary", cl::desc("dump file summary"),
456                          cl::cat(MsfOptions), cl::sub(DumpSubcommand));
457cl::opt<bool> DumpStreams("streams",
458                          cl::desc("dump summary of the PDB streams"),
459                          cl::cat(MsfOptions), cl::sub(DumpSubcommand));
460cl::opt<bool> DumpStreamBlocks(
461    "stream-blocks",
462    cl::desc("Add block information to the output of -streams"),
463    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
464cl::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));
468cl::opt<bool> DumpTypeStats(
469    "type-stats",
470    cl::desc("Dump a detailed breakdown of type usage/size"),
471    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
472cl::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));
476cl::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
482cl::opt<bool> DumpTypes("types",
483                        cl::desc("dump CodeView type records from TPI stream"),
484                        cl::cat(TypeOptions), cl::sub(DumpSubcommand));
485cl::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));
489cl::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
495cl::opt<bool> DumpTypeExtras("type-extras",
496                             cl::desc("dump type hashes and index offsets"),
497                             cl::cat(TypeOptions), cl::sub(DumpSubcommand));
498
499cl::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
505cl::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
510cl::opt<bool> DumpIds("ids",
511                      cl::desc("dump CodeView type records from IPI stream"),
512                      cl::cat(TypeOptions), cl::sub(DumpSubcommand));
513cl::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
518cl::opt<bool> DumpIdExtras("id-extras",
519                           cl::desc("dump id hashes and index offsets"),
520                           cl::cat(TypeOptions), cl::sub(DumpSubcommand));
521cl::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
526cl::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
534cl::opt<bool> DumpGlobals("globals", cl::desc("dump Globals symbol records"),
535                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
536cl::opt<bool> DumpGlobalExtras("global-extras", cl::desc("dump Globals hashes"),
537                               cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
538cl::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));
543cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
544                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
545cl::opt<bool> DumpPublicExtras("public-extras",
546                               cl::desc("dump Publics hashes and address maps"),
547                               cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
548cl::opt<bool>
549    DumpGSIRecords("gsi-records",
550                   cl::desc("dump public / global common record stream"),
551                   cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
552cl::opt<bool> DumpSymbols("symbols", cl::desc("dump module symbols"),
553                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
554
555cl::opt<bool>
556    DumpSymRecordBytes("sym-data",
557                       cl::desc("dump CodeView symbol record raw bytes"),
558                       cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
559
560cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"),
561                      cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
562
563cl::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));
567cl::opt<bool> DumpParents("show-parents",
568                          cl::desc("dump the symbols record's all parents."),
569                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
570cl::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));
575cl::opt<bool> DumpChildren("show-children",
576                           cl::desc("dump the symbols record's all children."),
577                           cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
578cl::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
585cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
586                          cl::cat(FileOptions), cl::sub(DumpSubcommand));
587cl::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));
591cl::opt<bool> DumpLines(
592    "l",
593    cl::desc("dump source file/line information (DEBUG_S_LINES subsection)"),
594    cl::cat(FileOptions), cl::sub(DumpSubcommand));
595cl::opt<bool> DumpInlineeLines(
596    "il",
597    cl::desc("dump inlinee line information (DEBUG_S_INLINEELINES subsection)"),
598    cl::cat(FileOptions), cl::sub(DumpSubcommand));
599cl::opt<bool> DumpXmi(
600    "xmi",
601    cl::desc(
602        "dump cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
603    cl::cat(FileOptions), cl::sub(DumpSubcommand));
604cl::opt<bool> DumpXme(
605    "xme",
606    cl::desc(
607        "dump cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
608    cl::cat(FileOptions), cl::sub(DumpSubcommand));
609cl::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));
613cl::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
619cl::opt<bool> DumpNamedStreams("named-streams",
620                               cl::desc("dump PDB named stream table"),
621                               cl::cat(MiscOptions), cl::sub(DumpSubcommand));
622
623cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
624                              cl::cat(MiscOptions), cl::sub(DumpSubcommand));
625cl::opt<bool> DumpStringTableDetails("string-table-details",
626                                     cl::desc("dump PDB String Table Details"),
627                                     cl::cat(MiscOptions),
628                                     cl::sub(DumpSubcommand));
629
630cl::opt<bool> DumpSectionContribs("section-contribs",
631                                  cl::desc("dump section contributions"),
632                                  cl::cat(MiscOptions),
633                                  cl::sub(DumpSubcommand));
634cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
635                             cl::cat(MiscOptions), cl::sub(DumpSubcommand));
636cl::opt<bool> DumpSectionHeaders("section-headers",
637                                 cl::desc("Dump image section headers"),
638                                 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
639
640cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
641                     cl::cat(MiscOptions), cl::sub(DumpSubcommand));
642
643cl::list<std::string> InputFilenames(cl::Positional,
644                                     cl::desc("<input PDB files>"),
645                                     cl::OneOrMore, cl::sub(DumpSubcommand));
646}
647
648namespace yaml2pdb {
649cl::opt<std::string>
650    YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
651                      cl::sub(YamlToPdbSubcommand));
652
653cl::opt<std::string> InputFilename(cl::Positional,
654                                   cl::desc("<input YAML file>"), cl::Required,
655                                   cl::sub(YamlToPdbSubcommand));
656}
657
658namespace pdb2yaml {
659cl::opt<bool> All("all",
660                  cl::desc("Dump everything we know how to dump."),
661                  cl::sub(PdbToYamlSubcommand), cl::init(false));
662cl::opt<bool> NoFileHeaders("no-file-headers",
663                            cl::desc("Do not dump MSF file headers"),
664                            cl::sub(PdbToYamlSubcommand), cl::init(false));
665cl::opt<bool> Minimal("minimal",
666                      cl::desc("Don't write fields with default values"),
667                      cl::sub(PdbToYamlSubcommand), cl::init(false));
668
669cl::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));
673cl::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));
677cl::opt<bool> PdbStream("pdb-stream",
678                        cl::desc("Dump the PDB Stream (Stream 1)"),
679                        cl::sub(PdbToYamlSubcommand), cl::init(false));
680
681cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
682                          cl::sub(PdbToYamlSubcommand), cl::init(false));
683
684cl::opt<bool> DbiStream("dbi-stream",
685                        cl::desc("Dump the DBI Stream Headers (Stream 2)"),
686                        cl::sub(PdbToYamlSubcommand), cl::init(false));
687
688cl::opt<bool> TpiStream("tpi-stream",
689                        cl::desc("Dump the TPI Stream (Stream 3)"),
690                        cl::sub(PdbToYamlSubcommand), cl::init(false));
691
692cl::opt<bool> IpiStream("ipi-stream",
693                        cl::desc("Dump the IPI Stream (Stream 5)"),
694                        cl::sub(PdbToYamlSubcommand), cl::init(false));
695
696cl::opt<bool> PublicsStream("publics-stream",
697                            cl::desc("Dump the Publics Stream"),
698                            cl::sub(PdbToYamlSubcommand), cl::init(false));
699
700// MODULE & FILE OPTIONS
701cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
702                          cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
703cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
704                              cl::cat(FileOptions),
705                              cl::sub(PdbToYamlSubcommand));
706cl::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));
710cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
711                             cl::cat(FileOptions),
712                             cl::sub(PdbToYamlSubcommand));
713
714cl::list<std::string> InputFilename(cl::Positional,
715                                    cl::desc("<input PDB file>"), cl::Required,
716                                    cl::sub(PdbToYamlSubcommand));
717} // namespace pdb2yaml
718
719namespace merge {
720cl::list<std::string> InputFilenames(cl::Positional,
721                                     cl::desc("<input PDB files>"),
722                                     cl::OneOrMore, cl::sub(MergeSubcommand));
723cl::opt<std::string>
724    PdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
725                  cl::sub(MergeSubcommand));
726}
727
728namespace explain {
729cl::list<std::string> InputFilename(cl::Positional,
730                                    cl::desc("<input PDB file>"), cl::Required,
731                                    cl::sub(ExplainSubcommand));
732
733cl::list<uint64_t> Offsets("offset", cl::desc("The file offset to explain"),
734                           cl::sub(ExplainSubcommand), cl::OneOrMore);
735
736cl::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
751namespace exportstream {
752cl::list<std::string> InputFilename(cl::Positional,
753                                    cl::desc("<input PDB file>"), cl::Required,
754                                    cl::sub(ExportSubcommand));
755cl::opt<std::string> OutputFile("out",
756                                cl::desc("The file to write the stream to"),
757                                cl::Required, cl::sub(ExportSubcommand));
758cl::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));
762cl::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
770static ExitOnError ExitOnErr;
771
772static 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
883static 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
890static 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
899static 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
906static 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
915bool 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
925uint32_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
932bool 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
945bool 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
958static std::string stringOr(std::string Str, std::string IfEmpty) {
959  return (Str.empty()) ? IfEmpty : Str;
960}
961
962static 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
1018template <typename OuterT, typename ChildT>
1019void 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
1033static 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
1081static 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
1320static 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
1371static 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
1383static 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
1422static 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
1444static 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
1453int 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