llvm-pdbutil.cpp revision 353358
155682Smarkm//===- llvm-pdbutil.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
255682Smarkm//
355682Smarkm// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
455682Smarkm// See https://llvm.org/LICENSE.txt for license information.
555682Smarkm// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
655682Smarkm//
755682Smarkm//===----------------------------------------------------------------------===//
855682Smarkm//
955682Smarkm// Dumps debug information present in PDB files.
1055682Smarkm//
1155682Smarkm//===----------------------------------------------------------------------===//
1255682Smarkm
1355682Smarkm#include "llvm-pdbutil.h"
1455682Smarkm
1555682Smarkm#include "BytesOutputStyle.h"
1655682Smarkm#include "DumpOutputStyle.h"
1755682Smarkm#include "ExplainOutputStyle.h"
1855682Smarkm#include "InputFile.h"
1955682Smarkm#include "LinePrinter.h"
2055682Smarkm#include "OutputStyle.h"
2155682Smarkm#include "PrettyClassDefinitionDumper.h"
2255682Smarkm#include "PrettyCompilandDumper.h"
2355682Smarkm#include "PrettyEnumDumper.h"
2455682Smarkm#include "PrettyExternalSymbolDumper.h"
2555682Smarkm#include "PrettyFunctionDumper.h"
2655682Smarkm#include "PrettyTypeDumper.h"
2755682Smarkm#include "PrettyTypedefDumper.h"
2855682Smarkm#include "PrettyVariableDumper.h"
2955682Smarkm#include "YAMLOutputStyle.h"
3055682Smarkm
3155682Smarkm#include "llvm/ADT/ArrayRef.h"
3255682Smarkm#include "llvm/ADT/BitVector.h"
3355682Smarkm#include "llvm/ADT/DenseMap.h"
3455682Smarkm#include "llvm/ADT/STLExtras.h"
3555682Smarkm#include "llvm/ADT/StringExtras.h"
3655682Smarkm#include "llvm/BinaryFormat/Magic.h"
3755682Smarkm#include "llvm/Config/config.h"
3855682Smarkm#include "llvm/DebugInfo/CodeView/AppendingTypeTableBuilder.h"
3955682Smarkm#include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
4055682Smarkm#include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
4155682Smarkm#include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
4255682Smarkm#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
4355682Smarkm#include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
4455682Smarkm#include "llvm/DebugInfo/CodeView/StringsAndChecksums.h"
4555682Smarkm#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
4655682Smarkm#include "llvm/DebugInfo/MSF/MSFBuilder.h"
4755682Smarkm#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
4855682Smarkm#include "llvm/DebugInfo/PDB/IPDBInjectedSource.h"
4955682Smarkm#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
5055682Smarkm#include "llvm/DebugInfo/PDB/IPDBSession.h"
5155682Smarkm#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
5255682Smarkm#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
5355682Smarkm#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
5455682Smarkm#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
5555682Smarkm#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
5655682Smarkm#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
5755682Smarkm#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
5855682Smarkm#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
5955682Smarkm#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
6055682Smarkm#include "llvm/DebugInfo/PDB/Native/RawError.h"
6155682Smarkm#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
6255682Smarkm#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
6355682Smarkm#include "llvm/DebugInfo/PDB/PDB.h"
6455682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
6555682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
6655682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
6755682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
6855682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
6955682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
7055682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
7155682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionArg.h"
7255682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
7355682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
7455682Smarkm#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
7555682Smarkm#include "llvm/Support/BinaryByteStream.h"
7655682Smarkm#include "llvm/Support/COM.h"
7755682Smarkm#include "llvm/Support/CommandLine.h"
7855682Smarkm#include "llvm/Support/ConvertUTF.h"
7955682Smarkm#include "llvm/Support/FileOutputBuffer.h"
8055682Smarkm#include "llvm/Support/FileSystem.h"
8155682Smarkm#include "llvm/Support/Format.h"
8255682Smarkm#include "llvm/Support/InitLLVM.h"
8355682Smarkm#include "llvm/Support/LineIterator.h"
8455682Smarkm#include "llvm/Support/ManagedStatic.h"
8555682Smarkm#include "llvm/Support/MemoryBuffer.h"
8655682Smarkm#include "llvm/Support/Path.h"
8755682Smarkm#include "llvm/Support/PrettyStackTrace.h"
8855682Smarkm#include "llvm/Support/Process.h"
8955682Smarkm#include "llvm/Support/Regex.h"
9055682Smarkm#include "llvm/Support/ScopedPrinter.h"
9155682Smarkm#include "llvm/Support/Signals.h"
9255682Smarkm#include "llvm/Support/raw_ostream.h"
9355682Smarkm
9455682Smarkmusing namespace llvm;
9555682Smarkmusing namespace llvm::codeview;
9655682Smarkmusing namespace llvm::msf;
9755682Smarkmusing namespace llvm::pdb;
9855682Smarkm
9955682Smarkmnamespace opts {
10055682Smarkm
10155682Smarkmcl::SubCommand DumpSubcommand("dump", "Dump MSF and CodeView debug info");
10255682Smarkmcl::SubCommand BytesSubcommand("bytes", "Dump raw bytes from the PDB file");
10355682Smarkm
10455682Smarkmcl::SubCommand DiaDumpSubcommand("diadump",
10555682Smarkm                                 "Dump debug information using a DIA-like API");
10655682Smarkm
10755682Smarkmcl::SubCommand
10855682Smarkm    PrettySubcommand("pretty",
10955682Smarkm                     "Dump semantic information about types and symbols");
11055682Smarkm
11155682Smarkmcl::SubCommand
11255682Smarkm    YamlToPdbSubcommand("yaml2pdb",
11355682Smarkm                        "Generate a PDB file from a YAML description");
11455682Smarkmcl::SubCommand
11555682Smarkm    PdbToYamlSubcommand("pdb2yaml",
11655682Smarkm                        "Generate a detailed YAML description of a PDB File");
11755682Smarkm
11855682Smarkmcl::SubCommand MergeSubcommand("merge",
11955682Smarkm                               "Merge multiple PDBs into a single PDB");
12055682Smarkm
12155682Smarkmcl::SubCommand ExplainSubcommand("explain",
12255682Smarkm                                 "Explain the meaning of a file offset");
12355682Smarkm
12455682Smarkmcl::SubCommand ExportSubcommand("export",
12555682Smarkm                                "Write binary data from a stream to a file");
12655682Smarkm
12755682Smarkmcl::OptionCategory TypeCategory("Symbol Type Options");
12855682Smarkmcl::OptionCategory FilterCategory("Filtering and Sorting Options");
12955682Smarkmcl::OptionCategory OtherOptions("Other Options");
13055682Smarkm
13155682Smarkmcl::ValuesClass ChunkValues = cl::values(
13255682Smarkm    clEnumValN(ModuleSubsection::CrossScopeExports, "cme",
13355682Smarkm               "Cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
13455682Smarkm    clEnumValN(ModuleSubsection::CrossScopeImports, "cmi",
13555682Smarkm               "Cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
13655682Smarkm    clEnumValN(ModuleSubsection::FileChecksums, "fc",
13755682Smarkm               "File checksums (DEBUG_S_CHECKSUMS subsection)"),
13855682Smarkm    clEnumValN(ModuleSubsection::InlineeLines, "ilines",
13955682Smarkm               "Inlinee lines (DEBUG_S_INLINEELINES subsection)"),
14055682Smarkm    clEnumValN(ModuleSubsection::Lines, "lines",
14155682Smarkm               "Lines (DEBUG_S_LINES subsection)"),
14255682Smarkm    clEnumValN(ModuleSubsection::StringTable, "strings",
14355682Smarkm               "String Table (DEBUG_S_STRINGTABLE subsection) (not "
14455682Smarkm               "typically present in PDB file)"),
14555682Smarkm    clEnumValN(ModuleSubsection::FrameData, "frames",
14655682Smarkm               "Frame Data (DEBUG_S_FRAMEDATA subsection)"),
14755682Smarkm    clEnumValN(ModuleSubsection::Symbols, "symbols",
14855682Smarkm               "Symbols (DEBUG_S_SYMBOLS subsection) (not typically "
14955682Smarkm               "present in PDB file)"),
15055682Smarkm    clEnumValN(ModuleSubsection::CoffSymbolRVAs, "rvas",
15155682Smarkm               "COFF Symbol RVAs (DEBUG_S_COFF_SYMBOL_RVA subsection)"),
15255682Smarkm    clEnumValN(ModuleSubsection::Unknown, "unknown",
15355682Smarkm               "Any subsection not covered by another option"),
15455682Smarkm    clEnumValN(ModuleSubsection::All, "all", "All known subsections"));
15555682Smarkm
15655682Smarkmnamespace diadump {
15755682Smarkmcl::list<std::string> InputFilenames(cl::Positional,
15855682Smarkm                                     cl::desc("<input PDB files>"),
15955682Smarkm                                     cl::OneOrMore, cl::sub(DiaDumpSubcommand));
16055682Smarkm
16155682Smarkmcl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
16255682Smarkm                     cl::sub(DiaDumpSubcommand));
16355682Smarkm
16455682Smarkmstatic cl::opt<bool>
16555682Smarkm    ShowClassHierarchy("hierarchy", cl::desc("Show lexical and class parents"),
16655682Smarkm                       cl::sub(DiaDumpSubcommand));
16755682Smarkmstatic cl::opt<bool> NoSymIndexIds(
16855682Smarkm    "no-ids",
16955682Smarkm    cl::desc("Don't show any SymIndexId fields (overrides -hierarchy)"),
17055682Smarkm    cl::sub(DiaDumpSubcommand));
17155682Smarkm
17255682Smarkmstatic cl::opt<bool>
17355682Smarkm    Recurse("recurse",
17455682Smarkm            cl::desc("When dumping a SymIndexId, dump the full details of the "
17555682Smarkm                     "corresponding record"),
17655682Smarkm            cl::sub(DiaDumpSubcommand));
17755682Smarkm
17855682Smarkmstatic cl::opt<bool> Enums("enums", cl::desc("Dump enum types"),
17955682Smarkm                           cl::sub(DiaDumpSubcommand));
18055682Smarkmstatic cl::opt<bool> Pointers("pointers", cl::desc("Dump enum types"),
18155682Smarkm                              cl::sub(DiaDumpSubcommand));
18255682Smarkmstatic cl::opt<bool> UDTs("udts", cl::desc("Dump udt types"),
18355682Smarkm                          cl::sub(DiaDumpSubcommand));
18455682Smarkmstatic cl::opt<bool> Compilands("compilands",
18555682Smarkm                                cl::desc("Dump compiland information"),
18655682Smarkm                                cl::sub(DiaDumpSubcommand));
18755682Smarkmstatic cl::opt<bool> Funcsigs("funcsigs",
18855682Smarkm                              cl::desc("Dump function signature information"),
18955682Smarkm                              cl::sub(DiaDumpSubcommand));
19055682Smarkmstatic cl::opt<bool> Arrays("arrays", cl::desc("Dump array types"),
19155682Smarkm                            cl::sub(DiaDumpSubcommand));
19255682Smarkmstatic cl::opt<bool> VTShapes("vtshapes", cl::desc("Dump virtual table shapes"),
19355682Smarkm                              cl::sub(DiaDumpSubcommand));
19455682Smarkmstatic cl::opt<bool> Typedefs("typedefs", cl::desc("Dump typedefs"),
19555682Smarkm                              cl::sub(DiaDumpSubcommand));
19655682Smarkm} // namespace diadump
19755682Smarkm
19855682Smarkmnamespace pretty {
19955682Smarkmcl::list<std::string> InputFilenames(cl::Positional,
20055682Smarkm                                     cl::desc("<input PDB files>"),
20155682Smarkm                                     cl::OneOrMore, cl::sub(PrettySubcommand));
20255682Smarkm
20355682Smarkmcl::opt<bool> InjectedSources("injected-sources",
20455682Smarkm                              cl::desc("Display injected sources"),
20555682Smarkm                              cl::cat(OtherOptions), cl::sub(PrettySubcommand));
20655682Smarkmcl::opt<bool> ShowInjectedSourceContent(
20755682Smarkm    "injected-source-content",
20855682Smarkm    cl::desc("When displaying an injected source, display the file content"),
20955682Smarkm    cl::cat(OtherOptions), cl::sub(PrettySubcommand));
21055682Smarkm
21155682Smarkmcl::list<std::string> WithName(
21255682Smarkm    "with-name",
21355682Smarkm    cl::desc("Display any symbol or type with the specified exact name"),
21455682Smarkm    cl::cat(TypeCategory), cl::ZeroOrMore, cl::sub(PrettySubcommand));
21555682Smarkm
21655682Smarkmcl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
21755682Smarkm                         cl::cat(TypeCategory), cl::sub(PrettySubcommand));
21855682Smarkmcl::opt<bool> Symbols("module-syms",
21955682Smarkm                      cl::desc("Display symbols for each compiland"),
22055682Smarkm                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22155682Smarkmcl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
22255682Smarkm                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22355682Smarkmcl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
22455682Smarkm                        cl::cat(TypeCategory), cl::sub(PrettySubcommand));
22555682Smarkmcl::list<SymLevel> SymTypes(
22655682Smarkm    "sym-types", cl::desc("Type of symbols to dump (default all)"),
22755682Smarkm    cl::cat(TypeCategory), cl::sub(PrettySubcommand), cl::ZeroOrMore,
22855682Smarkm    cl::values(
22955682Smarkm        clEnumValN(SymLevel::Thunks, "thunks", "Display thunk symbols"),
23055682Smarkm        clEnumValN(SymLevel::Data, "data", "Display data symbols"),
23155682Smarkm        clEnumValN(SymLevel::Functions, "funcs", "Display function symbols"),
23255682Smarkm        clEnumValN(SymLevel::All, "all", "Display all symbols (default)")));
23355682Smarkm
23455682Smarkmcl::opt<bool>
23555682Smarkm    Types("types",
23655682Smarkm          cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
23755682Smarkm          cl::cat(TypeCategory), cl::sub(PrettySubcommand));
23855682Smarkmcl::opt<bool> Classes("classes", cl::desc("Display class types"),
23955682Smarkm                      cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24055682Smarkmcl::opt<bool> Enums("enums", cl::desc("Display enum types"),
24155682Smarkm                    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24255682Smarkmcl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
24355682Smarkm                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24455682Smarkmcl::opt<bool> Funcsigs("funcsigs", cl::desc("Display function signatures"),
24555682Smarkm                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24655682Smarkmcl::opt<bool> Pointers("pointers", cl::desc("Display pointer types"),
24755682Smarkm                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
24855682Smarkmcl::opt<bool> Arrays("arrays", cl::desc("Display arrays"),
24955682Smarkm                     cl::cat(TypeCategory), cl::sub(PrettySubcommand));
25055682Smarkmcl::opt<bool> VTShapes("vtshapes", cl::desc("Display vftable shapes"),
25155682Smarkm                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
25255682Smarkm
25355682Smarkmcl::opt<SymbolSortMode> SymbolOrder(
25455682Smarkm    "symbol-order", cl::desc("symbol sort order"),
25555682Smarkm    cl::init(SymbolSortMode::None),
25655682Smarkm    cl::values(clEnumValN(SymbolSortMode::None, "none",
25755682Smarkm                          "Undefined / no particular sort order"),
25855682Smarkm               clEnumValN(SymbolSortMode::Name, "name", "Sort symbols by name"),
25955682Smarkm               clEnumValN(SymbolSortMode::Size, "size",
26055682Smarkm                          "Sort symbols by size")),
26155682Smarkm    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
26255682Smarkm
26355682Smarkmcl::opt<ClassSortMode> ClassOrder(
26455682Smarkm    "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
26555682Smarkm    cl::values(
26655682Smarkm        clEnumValN(ClassSortMode::None, "none",
26755682Smarkm                   "Undefined / no particular sort order"),
26855682Smarkm        clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
26955682Smarkm        clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
27055682Smarkm        clEnumValN(ClassSortMode::Padding, "padding",
27155682Smarkm                   "Sort classes by amount of padding"),
27255682Smarkm        clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
27355682Smarkm                   "Sort classes by percentage of space consumed by padding"),
27455682Smarkm        clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
27555682Smarkm                   "Sort classes by amount of immediate padding"),
27655682Smarkm        clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
27755682Smarkm                   "Sort classes by percentage of space consumed by immediate "
27855682Smarkm                   "padding")),
27955682Smarkm    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
28055682Smarkm
28155682Smarkmcl::opt<ClassDefinitionFormat> ClassFormat(
28255682Smarkm    "class-definitions", cl::desc("Class definition format"),
28355682Smarkm    cl::init(ClassDefinitionFormat::All),
28455682Smarkm    cl::values(
28555682Smarkm        clEnumValN(ClassDefinitionFormat::All, "all",
28655682Smarkm                   "Display all class members including data, constants, "
28755682Smarkm                   "typedefs, functions, etc"),
28855682Smarkm        clEnumValN(ClassDefinitionFormat::Layout, "layout",
28955682Smarkm                   "Only display members that contribute to class size."),
29055682Smarkm        clEnumValN(ClassDefinitionFormat::None, "none",
29155682Smarkm                   "Don't display class definitions")),
29255682Smarkm    cl::cat(TypeCategory), cl::sub(PrettySubcommand));
29355682Smarkmcl::opt<uint32_t> ClassRecursionDepth(
29455682Smarkm    "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
29555682Smarkm    cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
29655682Smarkm
29755682Smarkmcl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
29855682Smarkm                    cl::sub(PrettySubcommand));
29955682Smarkmcl::opt<bool>
30055682Smarkm    All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
30155682Smarkm        cl::cat(TypeCategory), cl::sub(PrettySubcommand));
30255682Smarkm
30355682Smarkmcl::opt<uint64_t> LoadAddress(
30455682Smarkm    "load-address",
30555682Smarkm    cl::desc("Assume the module is loaded at the specified address"),
30655682Smarkm    cl::cat(OtherOptions), cl::sub(PrettySubcommand));
30755682Smarkmcl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
30855682Smarkm                     cl::cat(OtherOptions), cl::sub(PrettySubcommand));
30955682Smarkmcl::opt<cl::boolOrDefault>
31055682Smarkm    ColorOutput("color-output",
31155682Smarkm                cl::desc("Override use of color (default = isatty)"),
31255682Smarkm                cl::cat(OtherOptions), cl::sub(PrettySubcommand));
31355682Smarkmcl::list<std::string> ExcludeTypes(
31455682Smarkm    "exclude-types", cl::desc("Exclude types by regular expression"),
31555682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
31655682Smarkmcl::list<std::string> ExcludeSymbols(
31755682Smarkm    "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
31855682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
31955682Smarkmcl::list<std::string> ExcludeCompilands(
32055682Smarkm    "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
32155682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
32255682Smarkm
32355682Smarkmcl::list<std::string> IncludeTypes(
32455682Smarkm    "include-types",
32555682Smarkm    cl::desc("Include only types which match a regular expression"),
32655682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
32755682Smarkmcl::list<std::string> IncludeSymbols(
32855682Smarkm    "include-symbols",
32955682Smarkm    cl::desc("Include only symbols which match a regular expression"),
33055682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
33155682Smarkmcl::list<std::string> IncludeCompilands(
33255682Smarkm    "include-compilands",
33355682Smarkm    cl::desc("Include only compilands those which match a regular expression"),
33455682Smarkm    cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
33555682Smarkmcl::opt<uint32_t> SizeThreshold(
33655682Smarkm    "min-type-size", cl::desc("Displays only those types which are greater "
33755682Smarkm                              "than or equal to the specified size."),
33855682Smarkm    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
33955682Smarkmcl::opt<uint32_t> PaddingThreshold(
34055682Smarkm    "min-class-padding", cl::desc("Displays only those classes which have at "
34155682Smarkm                                  "least the specified amount of padding."),
34255682Smarkm    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
34355682Smarkmcl::opt<uint32_t> ImmediatePaddingThreshold(
34455682Smarkm    "min-class-padding-imm",
34555682Smarkm    cl::desc("Displays only those classes which have at least the specified "
34655682Smarkm             "amount of immediate padding, ignoring padding internal to bases "
34755682Smarkm             "and aggregates."),
34855682Smarkm    cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
34955682Smarkm
35055682Smarkmcl::opt<bool> ExcludeCompilerGenerated(
35155682Smarkm    "no-compiler-generated",
35255682Smarkm    cl::desc("Don't show compiler generated types and symbols"),
35355682Smarkm    cl::cat(FilterCategory), cl::sub(PrettySubcommand));
35455682Smarkmcl::opt<bool>
35555682Smarkm    ExcludeSystemLibraries("no-system-libs",
35655682Smarkm                           cl::desc("Don't show symbols from system libraries"),
35755682Smarkm                           cl::cat(FilterCategory), cl::sub(PrettySubcommand));
35855682Smarkm
35955682Smarkmcl::opt<bool> NoEnumDefs("no-enum-definitions",
36055682Smarkm                         cl::desc("Don't display full enum definitions"),
36155682Smarkm                         cl::cat(FilterCategory), cl::sub(PrettySubcommand));
36255682Smarkm}
36355682Smarkm
36455682Smarkmcl::OptionCategory FileOptions("Module & File Options");
36555682Smarkm
36655682Smarkmnamespace bytes {
36755682Smarkmcl::OptionCategory MsfBytes("MSF File Options");
36855682Smarkmcl::OptionCategory DbiBytes("Dbi Stream Options");
36955682Smarkmcl::OptionCategory PdbBytes("PDB Stream Options");
37055682Smarkmcl::OptionCategory Types("Type Options");
37155682Smarkmcl::OptionCategory ModuleCategory("Module Options");
37255682Smarkm
37355682Smarkmllvm::Optional<NumberRange> DumpBlockRange;
37455682Smarkmllvm::Optional<NumberRange> DumpByteRange;
37555682Smarkm
37655682Smarkmcl::opt<std::string> DumpBlockRangeOpt(
37755682Smarkm    "block-range", cl::value_desc("start[-end]"),
37855682Smarkm    cl::desc("Dump binary data from specified range of blocks."),
37955682Smarkm    cl::sub(BytesSubcommand), cl::cat(MsfBytes));
38055682Smarkm
38155682Smarkmcl::opt<std::string>
38255682Smarkm    DumpByteRangeOpt("byte-range", cl::value_desc("start[-end]"),
38355682Smarkm                     cl::desc("Dump binary data from specified range of bytes"),
38455682Smarkm                     cl::sub(BytesSubcommand), cl::cat(MsfBytes));
38555682Smarkm
38655682Smarkmcl::list<std::string>
38755682Smarkm    DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
38855682Smarkm                   cl::desc("Dump binary data from specified streams.  Format "
38955682Smarkm                            "is SN[:Start][@Size]"),
39055682Smarkm                   cl::sub(BytesSubcommand), cl::cat(MsfBytes));
39155682Smarkm
39255682Smarkmcl::opt<bool> NameMap("name-map", cl::desc("Dump bytes of PDB Name Map"),
39355682Smarkm                      cl::sub(BytesSubcommand), cl::cat(PdbBytes));
39455682Smarkmcl::opt<bool> Fpm("fpm", cl::desc("Dump free page map"),
39555682Smarkm                  cl::sub(BytesSubcommand), cl::cat(MsfBytes));
39655682Smarkm
39755682Smarkmcl::opt<bool> SectionContributions("sc", cl::desc("Dump section contributions"),
39855682Smarkm                                   cl::sub(BytesSubcommand), cl::cat(DbiBytes));
39955682Smarkmcl::opt<bool> SectionMap("sm", cl::desc("Dump section map"),
40055682Smarkm                         cl::sub(BytesSubcommand), cl::cat(DbiBytes));
40155682Smarkmcl::opt<bool> ModuleInfos("modi", cl::desc("Dump module info"),
40255682Smarkm                          cl::sub(BytesSubcommand), cl::cat(DbiBytes));
40355682Smarkmcl::opt<bool> FileInfo("files", cl::desc("Dump source file info"),
40455682Smarkm                       cl::sub(BytesSubcommand), cl::cat(DbiBytes));
40555682Smarkmcl::opt<bool> TypeServerMap("type-server", cl::desc("Dump type server map"),
40655682Smarkm                            cl::sub(BytesSubcommand), cl::cat(DbiBytes));
40755682Smarkmcl::opt<bool> ECData("ec", cl::desc("Dump edit and continue map"),
40855682Smarkm                     cl::sub(BytesSubcommand), cl::cat(DbiBytes));
40955682Smarkm
41055682Smarkmcl::list<uint32_t>
41155682Smarkm    TypeIndex("type",
41255682Smarkm              cl::desc("Dump the type record with the given type index"),
41355682Smarkm              cl::ZeroOrMore, cl::CommaSeparated, cl::sub(BytesSubcommand),
41455682Smarkm              cl::cat(TypeCategory));
41555682Smarkmcl::list<uint32_t>
41655682Smarkm    IdIndex("id", cl::desc("Dump the id record with the given type index"),
41755682Smarkm            cl::ZeroOrMore, cl::CommaSeparated, cl::sub(BytesSubcommand),
41855682Smarkm            cl::cat(TypeCategory));
41955682Smarkm
42055682Smarkmcl::opt<uint32_t> ModuleIndex(
42155682Smarkm    "mod",
42255682Smarkm    cl::desc(
42355682Smarkm        "Limit options in the Modules category to the specified module index"),
42455682Smarkm    cl::Optional, cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
42555682Smarkmcl::opt<bool> ModuleSyms("syms", cl::desc("Dump symbol record substream"),
42655682Smarkm                         cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
42755682Smarkmcl::opt<bool> ModuleC11("c11-chunks", cl::Hidden,
42855682Smarkm                        cl::desc("Dump C11 CodeView debug chunks"),
42955682Smarkm                        cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
43055682Smarkmcl::opt<bool> ModuleC13("chunks",
43155682Smarkm                        cl::desc("Dump C13 CodeView debug chunk subsection"),
43255682Smarkm                        cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
43355682Smarkmcl::opt<bool> SplitChunks(
43455682Smarkm    "split-chunks",
43555682Smarkm    cl::desc(
43655682Smarkm        "When dumping debug chunks, show a different section for each chunk"),
43755682Smarkm    cl::sub(BytesSubcommand), cl::cat(ModuleCategory));
43855682Smarkmcl::list<std::string> InputFilenames(cl::Positional,
43955682Smarkm                                     cl::desc("<input PDB files>"),
44055682Smarkm                                     cl::OneOrMore, cl::sub(BytesSubcommand));
44155682Smarkm
44255682Smarkm} // namespace bytes
44355682Smarkm
44455682Smarkmnamespace dump {
44555682Smarkm
44655682Smarkmcl::OptionCategory MsfOptions("MSF Container Options");
44755682Smarkmcl::OptionCategory TypeOptions("Type Record Options");
44855682Smarkmcl::OptionCategory SymbolOptions("Symbol Options");
44955682Smarkmcl::OptionCategory MiscOptions("Miscellaneous Options");
45055682Smarkm
45155682Smarkm// MSF OPTIONS
45255682Smarkmcl::opt<bool> DumpSummary("summary", cl::desc("dump file summary"),
45355682Smarkm                          cl::cat(MsfOptions), cl::sub(DumpSubcommand));
45455682Smarkmcl::opt<bool> DumpStreams("streams",
45555682Smarkm                          cl::desc("dump summary of the PDB streams"),
45655682Smarkm                          cl::cat(MsfOptions), cl::sub(DumpSubcommand));
45755682Smarkmcl::opt<bool> DumpStreamBlocks(
45855682Smarkm    "stream-blocks",
45955682Smarkm    cl::desc("Add block information to the output of -streams"),
46055682Smarkm    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
46155682Smarkmcl::opt<bool> DumpSymbolStats(
46255682Smarkm    "sym-stats",
46355682Smarkm    cl::desc("Dump a detailed breakdown of symbol usage/size for each module"),
46455682Smarkm    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
46555682Smarkmcl::opt<bool> DumpTypeStats(
46655682Smarkm    "type-stats",
46755682Smarkm    cl::desc("Dump a detailed breakdown of type usage/size"),
46855682Smarkm    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
46955682Smarkmcl::opt<bool> DumpUdtStats(
47055682Smarkm    "udt-stats",
47155682Smarkm    cl::desc("Dump a detailed breakdown of S_UDT record usage / stats"),
47255682Smarkm    cl::cat(MsfOptions), cl::sub(DumpSubcommand));
47355682Smarkm
47455682Smarkm// TYPE OPTIONS
47555682Smarkmcl::opt<bool> DumpTypes("types",
47655682Smarkm                        cl::desc("dump CodeView type records from TPI stream"),
47755682Smarkm                        cl::cat(TypeOptions), cl::sub(DumpSubcommand));
47855682Smarkmcl::opt<bool> DumpTypeData(
47955682Smarkm    "type-data",
48055682Smarkm    cl::desc("dump CodeView type record raw bytes from TPI stream"),
48155682Smarkm    cl::cat(TypeOptions), cl::sub(DumpSubcommand));
48255682Smarkmcl::opt<bool>
48355682Smarkm    DumpTypeRefStats("type-ref-stats",
48455682Smarkm                     cl::desc("dump statistics on the number and size of types "
48555682Smarkm                              "transitively referenced by symbol records"),
48655682Smarkm                     cl::cat(TypeOptions), cl::sub(DumpSubcommand));
48755682Smarkm
48855682Smarkmcl::opt<bool> DumpTypeExtras("type-extras",
48955682Smarkm                             cl::desc("dump type hashes and index offsets"),
49055682Smarkm                             cl::cat(TypeOptions), cl::sub(DumpSubcommand));
49155682Smarkm
49255682Smarkmcl::opt<bool> DontResolveForwardRefs(
49355682Smarkm    "dont-resolve-forward-refs",
49455682Smarkm    cl::desc("When dumping type records for classes, unions, enums, and "
49555682Smarkm             "structs, don't try to resolve forward references"),
49655682Smarkm    cl::cat(TypeOptions), cl::sub(DumpSubcommand));
49755682Smarkm
49855682Smarkmcl::list<uint32_t> DumpTypeIndex(
49955682Smarkm    "type-index", cl::ZeroOrMore, cl::CommaSeparated,
50055682Smarkm    cl::desc("only dump types with the specified hexadecimal type index"),
50155682Smarkm    cl::cat(TypeOptions), cl::sub(DumpSubcommand));
50255682Smarkm
50355682Smarkmcl::opt<bool> DumpIds("ids",
50455682Smarkm                      cl::desc("dump CodeView type records from IPI stream"),
50555682Smarkm                      cl::cat(TypeOptions), cl::sub(DumpSubcommand));
50655682Smarkmcl::opt<bool>
50755682Smarkm    DumpIdData("id-data",
50855682Smarkm               cl::desc("dump CodeView type record raw bytes from IPI stream"),
50955682Smarkm               cl::cat(TypeOptions), cl::sub(DumpSubcommand));
51055682Smarkm
51155682Smarkmcl::opt<bool> DumpIdExtras("id-extras",
51255682Smarkm                           cl::desc("dump id hashes and index offsets"),
51355682Smarkm                           cl::cat(TypeOptions), cl::sub(DumpSubcommand));
51455682Smarkmcl::list<uint32_t> DumpIdIndex(
51555682Smarkm    "id-index", cl::ZeroOrMore, cl::CommaSeparated,
51655682Smarkm    cl::desc("only dump ids with the specified hexadecimal type index"),
51755682Smarkm    cl::cat(TypeOptions), cl::sub(DumpSubcommand));
51855682Smarkm
51955682Smarkmcl::opt<bool> DumpTypeDependents(
52055682Smarkm    "dependents",
52155682Smarkm    cl::desc("In conjunection with -type-index and -id-index, dumps the entire "
52255682Smarkm             "dependency graph for the specified index instead of "
52355682Smarkm             "just the single record with the specified index"),
52455682Smarkm    cl::cat(TypeOptions), cl::sub(DumpSubcommand));
52555682Smarkm
52655682Smarkm// SYMBOL OPTIONS
52755682Smarkmcl::opt<bool> DumpGlobals("globals", cl::desc("dump Globals symbol records"),
52855682Smarkm                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
52955682Smarkmcl::opt<bool> DumpGlobalExtras("global-extras", cl::desc("dump Globals hashes"),
53055682Smarkm                               cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
53155682Smarkmcl::list<std::string> DumpGlobalNames(
53255682Smarkm    "global-name",
53355682Smarkm    cl::desc(
53455682Smarkm        "With -globals, only dump globals whose name matches the given value"),
53555682Smarkm    cl::cat(SymbolOptions), cl::sub(DumpSubcommand), cl::ZeroOrMore);
53655682Smarkmcl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
53755682Smarkm                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
53855682Smarkmcl::opt<bool> DumpPublicExtras("public-extras",
53955682Smarkm                               cl::desc("dump Publics hashes and address maps"),
54055682Smarkm                               cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
54155682Smarkmcl::opt<bool>
54255682Smarkm    DumpGSIRecords("gsi-records",
54355682Smarkm                   cl::desc("dump public / global common record stream"),
54455682Smarkm                   cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
54555682Smarkmcl::opt<bool> DumpSymbols("symbols", cl::desc("dump module symbols"),
54655682Smarkm                          cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
54755682Smarkm
54855682Smarkmcl::opt<bool>
54955682Smarkm    DumpSymRecordBytes("sym-data",
55055682Smarkm                       cl::desc("dump CodeView symbol record raw bytes"),
55155682Smarkm                       cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
55255682Smarkm
55355682Smarkmcl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"),
55455682Smarkm                      cl::cat(SymbolOptions), cl::sub(DumpSubcommand));
55555682Smarkm
55655682Smarkm// MODULE & FILE OPTIONS
55755682Smarkmcl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
55855682Smarkm                          cl::cat(FileOptions), cl::sub(DumpSubcommand));
55955682Smarkmcl::opt<bool> DumpModuleFiles(
56055682Smarkm    "files",
56155682Smarkm    cl::desc("Dump the source files that contribute to each module's."),
56255682Smarkm    cl::cat(FileOptions), cl::sub(DumpSubcommand));
56355682Smarkmcl::opt<bool> DumpLines(
56455682Smarkm    "l",
56555682Smarkm    cl::desc("dump source file/line information (DEBUG_S_LINES subsection)"),
56655682Smarkm    cl::cat(FileOptions), cl::sub(DumpSubcommand));
56755682Smarkmcl::opt<bool> DumpInlineeLines(
56855682Smarkm    "il",
56955682Smarkm    cl::desc("dump inlinee line information (DEBUG_S_INLINEELINES subsection)"),
57055682Smarkm    cl::cat(FileOptions), cl::sub(DumpSubcommand));
57155682Smarkmcl::opt<bool> DumpXmi(
57255682Smarkm    "xmi",
57355682Smarkm    cl::desc(
57455682Smarkm        "dump cross module imports (DEBUG_S_CROSSSCOPEIMPORTS subsection)"),
57555682Smarkm    cl::cat(FileOptions), cl::sub(DumpSubcommand));
57655682Smarkmcl::opt<bool> DumpXme(
57755682Smarkm    "xme",
57855682Smarkm    cl::desc(
57955682Smarkm        "dump cross module exports (DEBUG_S_CROSSSCOPEEXPORTS subsection)"),
58055682Smarkm    cl::cat(FileOptions), cl::sub(DumpSubcommand));
58155682Smarkmcl::opt<uint32_t> DumpModi("modi", cl::Optional,
58255682Smarkm                           cl::desc("For all options that iterate over "
58355682Smarkm                                    "modules, limit to the specified module"),
58455682Smarkm                           cl::cat(FileOptions), cl::sub(DumpSubcommand));
58555682Smarkmcl::opt<bool> JustMyCode("jmc", cl::Optional,
58655682Smarkm                         cl::desc("For all options that iterate over modules, "
58755682Smarkm                                  "ignore modules from system libraries"),
58855682Smarkm                         cl::cat(FileOptions), cl::sub(DumpSubcommand));
58955682Smarkm
59055682Smarkm// MISCELLANEOUS OPTIONS
59155682Smarkmcl::opt<bool> DumpNamedStreams("named-streams",
59255682Smarkm                               cl::desc("dump PDB named stream table"),
59355682Smarkm                               cl::cat(MiscOptions), cl::sub(DumpSubcommand));
59455682Smarkm
59555682Smarkmcl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
59655682Smarkm                              cl::cat(MiscOptions), cl::sub(DumpSubcommand));
59755682Smarkmcl::opt<bool> DumpStringTableDetails("string-table-details",
59855682Smarkm                                     cl::desc("dump PDB String Table Details"),
59955682Smarkm                                     cl::cat(MiscOptions),
60055682Smarkm                                     cl::sub(DumpSubcommand));
60155682Smarkm
60255682Smarkmcl::opt<bool> DumpSectionContribs("section-contribs",
60355682Smarkm                                  cl::desc("dump section contributions"),
60455682Smarkm                                  cl::cat(MiscOptions),
60555682Smarkm                                  cl::sub(DumpSubcommand));
60655682Smarkmcl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
60755682Smarkm                             cl::cat(MiscOptions), cl::sub(DumpSubcommand));
60855682Smarkmcl::opt<bool> DumpSectionHeaders("section-headers",
60955682Smarkm                                 cl::desc("Dump image section headers"),
61055682Smarkm                                 cl::cat(MiscOptions), cl::sub(DumpSubcommand));
61155682Smarkm
61255682Smarkmcl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
61355682Smarkm                     cl::cat(MiscOptions), cl::sub(DumpSubcommand));
61455682Smarkm
61555682Smarkmcl::list<std::string> InputFilenames(cl::Positional,
61655682Smarkm                                     cl::desc("<input PDB files>"),
61755682Smarkm                                     cl::OneOrMore, cl::sub(DumpSubcommand));
61855682Smarkm}
61955682Smarkm
62055682Smarkmnamespace yaml2pdb {
62155682Smarkmcl::opt<std::string>
62255682Smarkm    YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
62355682Smarkm                      cl::sub(YamlToPdbSubcommand));
62455682Smarkm
62555682Smarkmcl::opt<std::string> InputFilename(cl::Positional,
62655682Smarkm                                   cl::desc("<input YAML file>"), cl::Required,
62755682Smarkm                                   cl::sub(YamlToPdbSubcommand));
62855682Smarkm}
62955682Smarkm
63055682Smarkmnamespace pdb2yaml {
63155682Smarkmcl::opt<bool> All("all",
63255682Smarkm                  cl::desc("Dump everything we know how to dump."),
63355682Smarkm                  cl::sub(PdbToYamlSubcommand), cl::init(false));
63455682Smarkmcl::opt<bool> NoFileHeaders("no-file-headers",
63555682Smarkm                            cl::desc("Do not dump MSF file headers"),
63655682Smarkm                            cl::sub(PdbToYamlSubcommand), cl::init(false));
63755682Smarkmcl::opt<bool> Minimal("minimal",
63855682Smarkm                      cl::desc("Don't write fields with default values"),
63955682Smarkm                      cl::sub(PdbToYamlSubcommand), cl::init(false));
64055682Smarkm
64155682Smarkmcl::opt<bool> StreamMetadata(
64255682Smarkm    "stream-metadata",
64355682Smarkm    cl::desc("Dump the number of streams and each stream's size"),
64455682Smarkm    cl::sub(PdbToYamlSubcommand), cl::init(false));
64555682Smarkmcl::opt<bool> StreamDirectory(
64655682Smarkm    "stream-directory",
64755682Smarkm    cl::desc("Dump each stream's block map (implies -stream-metadata)"),
64855682Smarkm    cl::sub(PdbToYamlSubcommand), cl::init(false));
64955682Smarkmcl::opt<bool> PdbStream("pdb-stream",
65055682Smarkm                        cl::desc("Dump the PDB Stream (Stream 1)"),
65155682Smarkm                        cl::sub(PdbToYamlSubcommand), cl::init(false));
65255682Smarkm
65355682Smarkmcl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
65455682Smarkm                          cl::sub(PdbToYamlSubcommand), cl::init(false));
65555682Smarkm
65655682Smarkmcl::opt<bool> DbiStream("dbi-stream",
65755682Smarkm                        cl::desc("Dump the DBI Stream Headers (Stream 2)"),
65855682Smarkm                        cl::sub(PdbToYamlSubcommand), cl::init(false));
65955682Smarkm
66055682Smarkmcl::opt<bool> TpiStream("tpi-stream",
66155682Smarkm                        cl::desc("Dump the TPI Stream (Stream 3)"),
66255682Smarkm                        cl::sub(PdbToYamlSubcommand), cl::init(false));
66355682Smarkm
66455682Smarkmcl::opt<bool> IpiStream("ipi-stream",
66555682Smarkm                        cl::desc("Dump the IPI Stream (Stream 5)"),
66655682Smarkm                        cl::sub(PdbToYamlSubcommand), cl::init(false));
66755682Smarkm
66855682Smarkmcl::opt<bool> PublicsStream("publics-stream",
66955682Smarkm                            cl::desc("Dump the Publics Stream"),
67055682Smarkm                            cl::sub(PdbToYamlSubcommand), cl::init(false));
67155682Smarkm
67255682Smarkm// MODULE & FILE OPTIONS
67355682Smarkmcl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
67455682Smarkm                          cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
67555682Smarkmcl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
67655682Smarkm                              cl::cat(FileOptions),
67755682Smarkm                              cl::sub(PdbToYamlSubcommand));
67855682Smarkmcl::list<ModuleSubsection> DumpModuleSubsections(
67955682Smarkm    "subsections", cl::ZeroOrMore, cl::CommaSeparated,
68055682Smarkm    cl::desc("dump subsections from each module's debug stream"), ChunkValues,
68155682Smarkm    cl::cat(FileOptions), cl::sub(PdbToYamlSubcommand));
68255682Smarkmcl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
68355682Smarkm                             cl::cat(FileOptions),
68455682Smarkm                             cl::sub(PdbToYamlSubcommand));
68555682Smarkm
68655682Smarkmcl::list<std::string> InputFilename(cl::Positional,
68755682Smarkm                                    cl::desc("<input PDB file>"), cl::Required,
68855682Smarkm                                    cl::sub(PdbToYamlSubcommand));
68955682Smarkm} // namespace pdb2yaml
69055682Smarkm
69155682Smarkmnamespace merge {
69255682Smarkmcl::list<std::string> InputFilenames(cl::Positional,
69355682Smarkm                                     cl::desc("<input PDB files>"),
69455682Smarkm                                     cl::OneOrMore, cl::sub(MergeSubcommand));
69555682Smarkmcl::opt<std::string>
69655682Smarkm    PdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
69755682Smarkm                  cl::sub(MergeSubcommand));
69855682Smarkm}
69955682Smarkm
70055682Smarkmnamespace explain {
70155682Smarkmcl::list<std::string> InputFilename(cl::Positional,
70255682Smarkm                                    cl::desc("<input PDB file>"), cl::Required,
70355682Smarkm                                    cl::sub(ExplainSubcommand));
70455682Smarkm
70555682Smarkmcl::list<uint64_t> Offsets("offset", cl::desc("The file offset to explain"),
70655682Smarkm                           cl::sub(ExplainSubcommand), cl::OneOrMore);
70755682Smarkm
70855682Smarkmcl::opt<InputFileType> InputType(
70955682Smarkm    "input-type", cl::desc("Specify how to interpret the input file"),
71055682Smarkm    cl::init(InputFileType::PDBFile), cl::Optional, cl::sub(ExplainSubcommand),
71155682Smarkm    cl::values(clEnumValN(InputFileType::PDBFile, "pdb-file",
71255682Smarkm                          "Treat input as a PDB file (default)"),
71355682Smarkm               clEnumValN(InputFileType::PDBStream, "pdb-stream",
71455682Smarkm                          "Treat input as raw contents of PDB stream"),
71555682Smarkm               clEnumValN(InputFileType::DBIStream, "dbi-stream",
71655682Smarkm                          "Treat input as raw contents of DBI stream"),
71755682Smarkm               clEnumValN(InputFileType::Names, "names-stream",
71855682Smarkm                          "Treat input as raw contents of /names named stream"),
71955682Smarkm               clEnumValN(InputFileType::ModuleStream, "mod-stream",
72055682Smarkm                          "Treat input as raw contents of a module stream")));
72155682Smarkm} // namespace explain
72255682Smarkm
72355682Smarkmnamespace exportstream {
72455682Smarkmcl::list<std::string> InputFilename(cl::Positional,
72555682Smarkm                                    cl::desc("<input PDB file>"), cl::Required,
72655682Smarkm                                    cl::sub(ExportSubcommand));
72755682Smarkmcl::opt<std::string> OutputFile("out",
72855682Smarkm                                cl::desc("The file to write the stream to"),
72955682Smarkm                                cl::Required, cl::sub(ExportSubcommand));
73055682Smarkmcl::opt<std::string>
73155682Smarkm    Stream("stream", cl::Required,
73255682Smarkm           cl::desc("The index or name of the stream whose contents to export"),
73355682Smarkm           cl::sub(ExportSubcommand));
73455682Smarkmcl::opt<bool> ForceName("name",
73555682Smarkm                        cl::desc("Force the interpretation of -stream as a "
73655682Smarkm                                 "string, even if it is a valid integer"),
73755682Smarkm                        cl::sub(ExportSubcommand), cl::Optional,
73855682Smarkm                        cl::init(false));
73955682Smarkm} // namespace exportstream
74055682Smarkm}
74155682Smarkm
74255682Smarkmstatic ExitOnError ExitOnErr;
74355682Smarkm
74455682Smarkmstatic void yamlToPdb(StringRef Path) {
74555682Smarkm  BumpPtrAllocator Allocator;
74655682Smarkm  ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
74755682Smarkm      MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
74855682Smarkm                                   /*RequiresNullTerminator=*/false);
74955682Smarkm
75055682Smarkm  if (ErrorOrBuffer.getError()) {
75155682Smarkm    ExitOnErr(createFileError(Path, errorCodeToError(ErrorOrBuffer.getError())));
75255682Smarkm  }
75355682Smarkm
75455682Smarkm  std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
75555682Smarkm
75655682Smarkm  llvm::yaml::Input In(Buffer->getBuffer());
75755682Smarkm  pdb::yaml::PdbObject YamlObj(Allocator);
75855682Smarkm  In >> YamlObj;
75955682Smarkm
76055682Smarkm  PDBFileBuilder Builder(Allocator);
76155682Smarkm
76255682Smarkm  uint32_t BlockSize = 4096;
76355682Smarkm  if (YamlObj.Headers.hasValue())
76455682Smarkm    BlockSize = YamlObj.Headers->SuperBlock.BlockSize;
76555682Smarkm  ExitOnErr(Builder.initialize(BlockSize));
76655682Smarkm  // Add each of the reserved streams.  We ignore stream metadata in the
76755682Smarkm  // yaml, because we will reconstruct our own view of the streams.  For
76855682Smarkm  // example, the YAML may say that there were 20 streams in the original
76955682Smarkm  // PDB, but maybe we only dump a subset of those 20 streams, so we will
77055682Smarkm  // have fewer, and the ones we do have may end up with different indices
77155682Smarkm  // than the ones in the original PDB.  So we just start with a clean slate.
77255682Smarkm  for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
77355682Smarkm    ExitOnErr(Builder.getMsfBuilder().addStream(0));
77455682Smarkm
77555682Smarkm  StringsAndChecksums Strings;
77655682Smarkm  Strings.setStrings(std::make_shared<DebugStringTableSubsection>());
77755682Smarkm
77855682Smarkm  if (YamlObj.StringTable.hasValue()) {
77955682Smarkm    for (auto S : *YamlObj.StringTable)
78055682Smarkm      Strings.strings()->insert(S);
78155682Smarkm  }
78255682Smarkm
78355682Smarkm  pdb::yaml::PdbInfoStream DefaultInfoStream;
78455682Smarkm  pdb::yaml::PdbDbiStream DefaultDbiStream;
78555682Smarkm  pdb::yaml::PdbTpiStream DefaultTpiStream;
78655682Smarkm  pdb::yaml::PdbTpiStream DefaultIpiStream;
78755682Smarkm
78855682Smarkm  const auto &Info = YamlObj.PdbStream.getValueOr(DefaultInfoStream);
78955682Smarkm
79055682Smarkm  auto &InfoBuilder = Builder.getInfoBuilder();
79155682Smarkm  InfoBuilder.setAge(Info.Age);
79255682Smarkm  InfoBuilder.setGuid(Info.Guid);
79355682Smarkm  InfoBuilder.setSignature(Info.Signature);
79455682Smarkm  InfoBuilder.setVersion(Info.Version);
79555682Smarkm  for (auto F : Info.Features)
79655682Smarkm    InfoBuilder.addFeature(F);
79755682Smarkm
79855682Smarkm  const auto &Dbi = YamlObj.DbiStream.getValueOr(DefaultDbiStream);
79955682Smarkm  auto &DbiBuilder = Builder.getDbiBuilder();
80055682Smarkm  DbiBuilder.setAge(Dbi.Age);
80155682Smarkm  DbiBuilder.setBuildNumber(Dbi.BuildNumber);
80255682Smarkm  DbiBuilder.setFlags(Dbi.Flags);
80355682Smarkm  DbiBuilder.setMachineType(Dbi.MachineType);
80455682Smarkm  DbiBuilder.setPdbDllRbld(Dbi.PdbDllRbld);
80555682Smarkm  DbiBuilder.setPdbDllVersion(Dbi.PdbDllVersion);
80655682Smarkm  DbiBuilder.setVersionHeader(Dbi.VerHeader);
80755682Smarkm  for (const auto &MI : Dbi.ModInfos) {
80855682Smarkm    auto &ModiBuilder = ExitOnErr(DbiBuilder.addModuleInfo(MI.Mod));
80955682Smarkm    ModiBuilder.setObjFileName(MI.Obj);
81055682Smarkm
81155682Smarkm    for (auto S : MI.SourceFiles)
81255682Smarkm      ExitOnErr(DbiBuilder.addModuleSourceFile(ModiBuilder, S));
81355682Smarkm    if (MI.Modi.hasValue()) {
81455682Smarkm      const auto &ModiStream = *MI.Modi;
81555682Smarkm      for (auto Symbol : ModiStream.Symbols) {
81655682Smarkm        ModiBuilder.addSymbol(
81755682Smarkm            Symbol.toCodeViewSymbol(Allocator, CodeViewContainer::Pdb));
81855682Smarkm      }
81955682Smarkm    }
82055682Smarkm
82155682Smarkm    // Each module has its own checksum subsection, so scan for it every time.
82255682Smarkm    Strings.setChecksums(nullptr);
82355682Smarkm    CodeViewYAML::initializeStringsAndChecksums(MI.Subsections, Strings);
82455682Smarkm
82555682Smarkm    auto CodeViewSubsections = ExitOnErr(CodeViewYAML::toCodeViewSubsectionList(
82655682Smarkm        Allocator, MI.Subsections, Strings));
82755682Smarkm    for (auto &SS : CodeViewSubsections) {
82855682Smarkm      ModiBuilder.addDebugSubsection(SS);
82955682Smarkm    }
83055682Smarkm  }
83155682Smarkm
83255682Smarkm  auto &TpiBuilder = Builder.getTpiBuilder();
83355682Smarkm  const auto &Tpi = YamlObj.TpiStream.getValueOr(DefaultTpiStream);
83455682Smarkm  TpiBuilder.setVersionHeader(Tpi.Version);
83555682Smarkm  AppendingTypeTableBuilder TS(Allocator);
83655682Smarkm  for (const auto &R : Tpi.Records) {
83755682Smarkm    CVType Type = R.toCodeViewRecord(TS);
83855682Smarkm    TpiBuilder.addTypeRecord(Type.RecordData, None);
83955682Smarkm  }
84055682Smarkm
84155682Smarkm  const auto &Ipi = YamlObj.IpiStream.getValueOr(DefaultIpiStream);
84255682Smarkm  auto &IpiBuilder = Builder.getIpiBuilder();
84355682Smarkm  IpiBuilder.setVersionHeader(Ipi.Version);
84455682Smarkm  for (const auto &R : Ipi.Records) {
84555682Smarkm    CVType Type = R.toCodeViewRecord(TS);
84655682Smarkm    IpiBuilder.addTypeRecord(Type.RecordData, None);
84755682Smarkm  }
84855682Smarkm
84955682Smarkm  Builder.getStringTableBuilder().setStrings(*Strings.strings());
85055682Smarkm
85155682Smarkm  codeview::GUID IgnoredOutGuid;
85255682Smarkm  ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile, &IgnoredOutGuid));
85355682Smarkm}
85455682Smarkm
85555682Smarkmstatic PDBFile &loadPDB(StringRef Path, std::unique_ptr<IPDBSession> &Session) {
85655682Smarkm  ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
85755682Smarkm
85855682Smarkm  NativeSession *NS = static_cast<NativeSession *>(Session.get());
85955682Smarkm  return NS->getPDBFile();
86055682Smarkm}
86155682Smarkm
86255682Smarkmstatic void pdb2Yaml(StringRef Path) {
86355682Smarkm  std::unique_ptr<IPDBSession> Session;
86455682Smarkm  auto &File = loadPDB(Path, Session);
86555682Smarkm
86655682Smarkm  auto O = llvm::make_unique<YAMLOutputStyle>(File);
86755682Smarkm  O = llvm::make_unique<YAMLOutputStyle>(File);
86855682Smarkm
86955682Smarkm  ExitOnErr(O->dump());
87055682Smarkm}
87155682Smarkm
87255682Smarkmstatic void dumpRaw(StringRef Path) {
87355682Smarkm  InputFile IF = ExitOnErr(InputFile::open(Path));
87455682Smarkm
87555682Smarkm  auto O = llvm::make_unique<DumpOutputStyle>(IF);
87655682Smarkm  ExitOnErr(O->dump());
87755682Smarkm}
87855682Smarkm
87955682Smarkmstatic void dumpBytes(StringRef Path) {
88055682Smarkm  std::unique_ptr<IPDBSession> Session;
88155682Smarkm  auto &File = loadPDB(Path, Session);
88255682Smarkm
88355682Smarkm  auto O = llvm::make_unique<BytesOutputStyle>(File);
88455682Smarkm
88555682Smarkm  ExitOnErr(O->dump());
88655682Smarkm}
88755682Smarkm
88855682Smarkmbool opts::pretty::shouldDumpSymLevel(SymLevel Search) {
88955682Smarkm  if (SymTypes.empty())
89055682Smarkm    return true;
89155682Smarkm  if (llvm::find(SymTypes, Search) != SymTypes.end())
89255682Smarkm    return true;
89355682Smarkm  if (llvm::find(SymTypes, SymLevel::All) != SymTypes.end())
89455682Smarkm    return true;
89555682Smarkm  return false;
89655682Smarkm}
89755682Smarkm
89855682Smarkmuint32_t llvm::pdb::getTypeLength(const PDBSymbolData &Symbol) {
89955682Smarkm  auto SymbolType = Symbol.getType();
90055682Smarkm  const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
90155682Smarkm
90255682Smarkm  return RawType.getLength();
90355682Smarkm}
90455682Smarkm
90555682Smarkmbool opts::pretty::compareFunctionSymbols(
90655682Smarkm    const std::unique_ptr<PDBSymbolFunc> &F1,
90755682Smarkm    const std::unique_ptr<PDBSymbolFunc> &F2) {
90855682Smarkm  assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
90955682Smarkm
91055682Smarkm  if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
91155682Smarkm    return F1->getName() < F2->getName();
91255682Smarkm
91355682Smarkm  // Note that we intentionally sort in descending order on length, since
91455682Smarkm  // long functions are more interesting than short functions.
91555682Smarkm  return F1->getLength() > F2->getLength();
91655682Smarkm}
91755682Smarkm
91855682Smarkmbool opts::pretty::compareDataSymbols(
91955682Smarkm    const std::unique_ptr<PDBSymbolData> &F1,
92055682Smarkm    const std::unique_ptr<PDBSymbolData> &F2) {
92155682Smarkm  assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
92255682Smarkm
92355682Smarkm  if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
92455682Smarkm    return F1->getName() < F2->getName();
92555682Smarkm
92655682Smarkm  // Note that we intentionally sort in descending order on length, since
92755682Smarkm  // large types are more interesting than short ones.
92855682Smarkm  return getTypeLength(*F1) > getTypeLength(*F2);
92955682Smarkm}
93055682Smarkm
93155682Smarkmstatic std::string stringOr(std::string Str, std::string IfEmpty) {
93255682Smarkm  return (Str.empty()) ? IfEmpty : Str;
93355682Smarkm}
93455682Smarkm
93555682Smarkmstatic void dumpInjectedSources(LinePrinter &Printer, IPDBSession &Session) {
93655682Smarkm  auto Sources = Session.getInjectedSources();
93755682Smarkm  if (!Sources || !Sources->getChildCount()) {
93855682Smarkm    Printer.printLine("There are no injected sources.");
93955682Smarkm    return;
94055682Smarkm  }
94155682Smarkm
94255682Smarkm  while (auto IS = Sources->getNext()) {
94355682Smarkm    Printer.NewLine();
94455682Smarkm    std::string File = stringOr(IS->getFileName(), "<null>");
94555682Smarkm    uint64_t Size = IS->getCodeByteSize();
94655682Smarkm    std::string Obj = stringOr(IS->getObjectFileName(), "<null>");
94755682Smarkm    std::string VFName = stringOr(IS->getVirtualFileName(), "<null>");
94855682Smarkm    uint32_t CRC = IS->getCrc32();
94955682Smarkm
95055682Smarkm    WithColor(Printer, PDB_ColorItem::Path).get() << File;
95155682Smarkm    Printer << " (";
95255682Smarkm    WithColor(Printer, PDB_ColorItem::LiteralValue).get() << Size;
95355682Smarkm    Printer << " bytes): ";
95455682Smarkm    WithColor(Printer, PDB_ColorItem::Keyword).get() << "obj";
95555682Smarkm    Printer << "=";
95655682Smarkm    WithColor(Printer, PDB_ColorItem::Path).get() << Obj;
95755682Smarkm    Printer << ", ";
95855682Smarkm    WithColor(Printer, PDB_ColorItem::Keyword).get() << "vname";
95955682Smarkm    Printer << "=";
96055682Smarkm    WithColor(Printer, PDB_ColorItem::Path).get() << VFName;
96155682Smarkm    Printer << ", ";
96255682Smarkm    WithColor(Printer, PDB_ColorItem::Keyword).get() << "crc";
96355682Smarkm    Printer << "=";
96455682Smarkm    WithColor(Printer, PDB_ColorItem::LiteralValue).get() << CRC;
96555682Smarkm    Printer << ", ";
96655682Smarkm    WithColor(Printer, PDB_ColorItem::Keyword).get() << "compression";
96755682Smarkm    Printer << "=";
96855682Smarkm    dumpPDBSourceCompression(
96955682Smarkm        WithColor(Printer, PDB_ColorItem::LiteralValue).get(),
97055682Smarkm        IS->getCompression());
97155682Smarkm
97255682Smarkm    if (!opts::pretty::ShowInjectedSourceContent)
97355682Smarkm      continue;
97455682Smarkm
97555682Smarkm    // Set the indent level to 0 when printing file content.
97655682Smarkm    int Indent = Printer.getIndentLevel();
97755682Smarkm    Printer.Unindent(Indent);
97855682Smarkm
97955682Smarkm    if (IS->getCompression() == PDB_SourceCompression::None)
98055682Smarkm      Printer.printLine(IS->getCode());
98155682Smarkm    else
98255682Smarkm      Printer.formatBinary("Compressed data",
98355682Smarkm                           arrayRefFromStringRef(IS->getCode()),
98455682Smarkm                           /*StartOffset=*/0);
98555682Smarkm
98655682Smarkm    // Re-indent back to the original level.
98755682Smarkm    Printer.Indent(Indent);
98855682Smarkm  }
98955682Smarkm}
99055682Smarkm
99155682Smarkmtemplate <typename OuterT, typename ChildT>
99255682Smarkmvoid diaDumpChildren(PDBSymbol &Outer, PdbSymbolIdField Ids,
99355682Smarkm                     PdbSymbolIdField Recurse) {
99455682Smarkm  OuterT *ConcreteOuter = dyn_cast<OuterT>(&Outer);
99555682Smarkm  if (!ConcreteOuter)
99655682Smarkm    return;
99755682Smarkm
99855682Smarkm  auto Children = ConcreteOuter->template findAllChildren<ChildT>();
99955682Smarkm  while (auto Child = Children->getNext()) {
100055682Smarkm    outs() << "  {";
100155682Smarkm    Child->defaultDump(outs(), 4, Ids, Recurse);
100255682Smarkm    outs() << "\n  }\n";
100355682Smarkm  }
100455682Smarkm}
100555682Smarkm
100655682Smarkmstatic void dumpDia(StringRef Path) {
100755682Smarkm  std::unique_ptr<IPDBSession> Session;
100855682Smarkm
100955682Smarkm  const auto ReaderType =
101055682Smarkm      opts::diadump::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
101155682Smarkm  ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
101255682Smarkm
101355682Smarkm  auto GlobalScope = Session->getGlobalScope();
101455682Smarkm
101555682Smarkm  std::vector<PDB_SymType> SymTypes;
101655682Smarkm
101755682Smarkm  if (opts::diadump::Compilands)
101855682Smarkm    SymTypes.push_back(PDB_SymType::Compiland);
101955682Smarkm  if (opts::diadump::Enums)
102055682Smarkm    SymTypes.push_back(PDB_SymType::Enum);
102155682Smarkm  if (opts::diadump::Pointers)
102255682Smarkm    SymTypes.push_back(PDB_SymType::PointerType);
102355682Smarkm  if (opts::diadump::UDTs)
102455682Smarkm    SymTypes.push_back(PDB_SymType::UDT);
102555682Smarkm  if (opts::diadump::Funcsigs)
102655682Smarkm    SymTypes.push_back(PDB_SymType::FunctionSig);
102755682Smarkm  if (opts::diadump::Arrays)
102855682Smarkm    SymTypes.push_back(PDB_SymType::ArrayType);
102955682Smarkm  if (opts::diadump::VTShapes)
103055682Smarkm    SymTypes.push_back(PDB_SymType::VTableShape);
103155682Smarkm  if (opts::diadump::Typedefs)
103255682Smarkm    SymTypes.push_back(PDB_SymType::Typedef);
103355682Smarkm  PdbSymbolIdField Ids = opts::diadump::NoSymIndexIds ? PdbSymbolIdField::None
103455682Smarkm                                                      : PdbSymbolIdField::All;
103555682Smarkm
103655682Smarkm  PdbSymbolIdField Recurse = PdbSymbolIdField::None;
103755682Smarkm  if (opts::diadump::Recurse)
103855682Smarkm    Recurse = PdbSymbolIdField::All;
103955682Smarkm  if (!opts::diadump::ShowClassHierarchy)
104055682Smarkm    Ids &= ~(PdbSymbolIdField::ClassParent | PdbSymbolIdField::LexicalParent);
104155682Smarkm
104255682Smarkm  for (PDB_SymType ST : SymTypes) {
104355682Smarkm    auto Children = GlobalScope->findAllChildren(ST);
104455682Smarkm    while (auto Child = Children->getNext()) {
104555682Smarkm      outs() << "{";
104655682Smarkm      Child->defaultDump(outs(), 2, Ids, Recurse);
104755682Smarkm
104855682Smarkm      diaDumpChildren<PDBSymbolTypeEnum, PDBSymbolData>(*Child, Ids, Recurse);
104955682Smarkm      outs() << "\n}\n";
105055682Smarkm    }
105155682Smarkm  }
105255682Smarkm}
105355682Smarkm
105455682Smarkmstatic void dumpPretty(StringRef Path) {
105555682Smarkm  std::unique_ptr<IPDBSession> Session;
105655682Smarkm
105755682Smarkm  const auto ReaderType =
105855682Smarkm      opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
105955682Smarkm  ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
106055682Smarkm
106155682Smarkm  if (opts::pretty::LoadAddress)
106255682Smarkm    Session->setLoadAddress(opts::pretty::LoadAddress);
106355682Smarkm
106455682Smarkm  auto &Stream = outs();
106555682Smarkm  const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
106655682Smarkm                            ? Stream.has_colors()
106755682Smarkm                            : opts::pretty::ColorOutput == cl::BOU_TRUE;
106855682Smarkm  LinePrinter Printer(2, UseColor, Stream);
106955682Smarkm
107055682Smarkm  auto GlobalScope(Session->getGlobalScope());
107155682Smarkm  if (!GlobalScope)
107255682Smarkm    return;
107355682Smarkm  std::string FileName(GlobalScope->getSymbolsFileName());
107455682Smarkm
107555682Smarkm  WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
107655682Smarkm  WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
107755682Smarkm  Printer.Indent();
107855682Smarkm  uint64_t FileSize = 0;
107955682Smarkm
108055682Smarkm  Printer.NewLine();
108155682Smarkm  WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
108255682Smarkm  if (!sys::fs::file_size(FileName, FileSize)) {
108355682Smarkm    Printer << ": " << FileSize << " bytes";
108455682Smarkm  } else {
108555682Smarkm    Printer << ": (Unable to obtain file size)";
108655682Smarkm  }
108755682Smarkm
108855682Smarkm  Printer.NewLine();
108955682Smarkm  WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
109055682Smarkm  Printer << ": " << GlobalScope->getGuid();
109155682Smarkm
109255682Smarkm  Printer.NewLine();
109355682Smarkm  WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
109455682Smarkm  Printer << ": " << GlobalScope->getAge();
109555682Smarkm
109655682Smarkm  Printer.NewLine();
109755682Smarkm  WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
109855682Smarkm  Printer << ": ";
109955682Smarkm  if (GlobalScope->hasCTypes())
110055682Smarkm    outs() << "HasCTypes ";
110155682Smarkm  if (GlobalScope->hasPrivateSymbols())
110255682Smarkm    outs() << "HasPrivateSymbols ";
110355682Smarkm  Printer.Unindent();
110455682Smarkm
110555682Smarkm  if (!opts::pretty::WithName.empty()) {
110655682Smarkm    Printer.NewLine();
110755682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get()
110855682Smarkm        << "---SYMBOLS & TYPES BY NAME---";
110955682Smarkm
111055682Smarkm    for (StringRef Name : opts::pretty::WithName) {
111155682Smarkm      auto Symbols = GlobalScope->findChildren(
111255682Smarkm          PDB_SymType::None, Name, PDB_NameSearchFlags::NS_CaseSensitive);
111355682Smarkm      if (!Symbols || Symbols->getChildCount() == 0) {
111455682Smarkm        Printer.formatLine("[not found] - {0}", Name);
111555682Smarkm        continue;
111655682Smarkm      }
111755682Smarkm      Printer.formatLine("[{0} occurrences] - {1}", Symbols->getChildCount(),
111855682Smarkm                         Name);
111955682Smarkm
112055682Smarkm      AutoIndent Indent(Printer);
112155682Smarkm      Printer.NewLine();
112255682Smarkm
112355682Smarkm      while (auto Symbol = Symbols->getNext()) {
112455682Smarkm        switch (Symbol->getSymTag()) {
112555682Smarkm        case PDB_SymType::Typedef: {
112655682Smarkm          TypedefDumper TD(Printer);
112755682Smarkm          std::unique_ptr<PDBSymbolTypeTypedef> T =
112855682Smarkm              llvm::unique_dyn_cast<PDBSymbolTypeTypedef>(std::move(Symbol));
112955682Smarkm          TD.start(*T);
113055682Smarkm          break;
113155682Smarkm        }
113255682Smarkm        case PDB_SymType::Enum: {
113355682Smarkm          EnumDumper ED(Printer);
113455682Smarkm          std::unique_ptr<PDBSymbolTypeEnum> E =
113555682Smarkm              llvm::unique_dyn_cast<PDBSymbolTypeEnum>(std::move(Symbol));
113655682Smarkm          ED.start(*E);
113755682Smarkm          break;
113855682Smarkm        }
113955682Smarkm        case PDB_SymType::UDT: {
114055682Smarkm          ClassDefinitionDumper CD(Printer);
114155682Smarkm          std::unique_ptr<PDBSymbolTypeUDT> C =
114255682Smarkm              llvm::unique_dyn_cast<PDBSymbolTypeUDT>(std::move(Symbol));
114355682Smarkm          CD.start(*C);
114455682Smarkm          break;
114555682Smarkm        }
114655682Smarkm        case PDB_SymType::BaseClass:
114755682Smarkm        case PDB_SymType::Friend: {
114855682Smarkm          TypeDumper TD(Printer);
114955682Smarkm          Symbol->dump(TD);
115055682Smarkm          break;
115155682Smarkm        }
115255682Smarkm        case PDB_SymType::Function: {
115355682Smarkm          FunctionDumper FD(Printer);
115455682Smarkm          std::unique_ptr<PDBSymbolFunc> F =
115555682Smarkm              llvm::unique_dyn_cast<PDBSymbolFunc>(std::move(Symbol));
115655682Smarkm          FD.start(*F, FunctionDumper::PointerType::None);
115755682Smarkm          break;
115855682Smarkm        }
115955682Smarkm        case PDB_SymType::Data: {
116055682Smarkm          VariableDumper VD(Printer);
116155682Smarkm          std::unique_ptr<PDBSymbolData> D =
116255682Smarkm              llvm::unique_dyn_cast<PDBSymbolData>(std::move(Symbol));
116355682Smarkm          VD.start(*D);
116455682Smarkm          break;
116555682Smarkm        }
116655682Smarkm        case PDB_SymType::PublicSymbol: {
116755682Smarkm          ExternalSymbolDumper ED(Printer);
116855682Smarkm          std::unique_ptr<PDBSymbolPublicSymbol> PS =
116955682Smarkm              llvm::unique_dyn_cast<PDBSymbolPublicSymbol>(std::move(Symbol));
117055682Smarkm          ED.dump(*PS);
117155682Smarkm          break;
117255682Smarkm        }
117355682Smarkm        default:
117455682Smarkm          llvm_unreachable("Unexpected symbol tag!");
117555682Smarkm        }
117655682Smarkm      }
117755682Smarkm    }
117855682Smarkm    llvm::outs().flush();
117955682Smarkm  }
118055682Smarkm
118155682Smarkm  if (opts::pretty::Compilands) {
118255682Smarkm    Printer.NewLine();
118355682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get()
118455682Smarkm        << "---COMPILANDS---";
118555682Smarkm    auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
118655682Smarkm
118755682Smarkm    if (Compilands) {
118855682Smarkm      Printer.Indent();
118955682Smarkm      CompilandDumper Dumper(Printer);
119055682Smarkm      CompilandDumpFlags options = CompilandDumper::Flags::None;
119155682Smarkm      if (opts::pretty::Lines)
119255682Smarkm        options = options | CompilandDumper::Flags::Lines;
119355682Smarkm      while (auto Compiland = Compilands->getNext())
119455682Smarkm        Dumper.start(*Compiland, options);
119555682Smarkm      Printer.Unindent();
119655682Smarkm    }
119755682Smarkm  }
119855682Smarkm
119955682Smarkm  if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs ||
120055682Smarkm      opts::pretty::Funcsigs || opts::pretty::Pointers ||
120155682Smarkm      opts::pretty::Arrays || opts::pretty::VTShapes) {
120255682Smarkm    Printer.NewLine();
120355682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
120455682Smarkm    Printer.Indent();
120555682Smarkm    TypeDumper Dumper(Printer);
120655682Smarkm    Dumper.start(*GlobalScope);
120755682Smarkm    Printer.Unindent();
120855682Smarkm  }
120955682Smarkm
121055682Smarkm  if (opts::pretty::Symbols) {
121155682Smarkm    Printer.NewLine();
121255682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
121355682Smarkm    if (auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>()) {
121455682Smarkm      Printer.Indent();
121555682Smarkm      CompilandDumper Dumper(Printer);
121655682Smarkm      while (auto Compiland = Compilands->getNext())
121755682Smarkm        Dumper.start(*Compiland, true);
121855682Smarkm      Printer.Unindent();
121955682Smarkm    }
122055682Smarkm  }
122155682Smarkm
122255682Smarkm  if (opts::pretty::Globals) {
122355682Smarkm    Printer.NewLine();
122455682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
122555682Smarkm    Printer.Indent();
122655682Smarkm    if (shouldDumpSymLevel(opts::pretty::SymLevel::Functions)) {
122755682Smarkm      if (auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>()) {
122855682Smarkm        FunctionDumper Dumper(Printer);
122955682Smarkm        if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
123055682Smarkm          while (auto Function = Functions->getNext()) {
123155682Smarkm            Printer.NewLine();
123255682Smarkm            Dumper.start(*Function, FunctionDumper::PointerType::None);
123355682Smarkm          }
123455682Smarkm        } else {
123555682Smarkm          std::vector<std::unique_ptr<PDBSymbolFunc>> Funcs;
123655682Smarkm          while (auto Func = Functions->getNext())
123755682Smarkm            Funcs.push_back(std::move(Func));
123855682Smarkm          llvm::sort(Funcs, opts::pretty::compareFunctionSymbols);
123955682Smarkm          for (const auto &Func : Funcs) {
124055682Smarkm            Printer.NewLine();
124155682Smarkm            Dumper.start(*Func, FunctionDumper::PointerType::None);
124255682Smarkm          }
124355682Smarkm        }
124455682Smarkm      }
124555682Smarkm    }
124655682Smarkm    if (shouldDumpSymLevel(opts::pretty::SymLevel::Data)) {
124755682Smarkm      if (auto Vars = GlobalScope->findAllChildren<PDBSymbolData>()) {
124855682Smarkm        VariableDumper Dumper(Printer);
124955682Smarkm        if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
125055682Smarkm          while (auto Var = Vars->getNext())
125155682Smarkm            Dumper.start(*Var);
125255682Smarkm        } else {
125355682Smarkm          std::vector<std::unique_ptr<PDBSymbolData>> Datas;
125455682Smarkm          while (auto Var = Vars->getNext())
125555682Smarkm            Datas.push_back(std::move(Var));
125655682Smarkm          llvm::sort(Datas, opts::pretty::compareDataSymbols);
125755682Smarkm          for (const auto &Var : Datas)
125855682Smarkm            Dumper.start(*Var);
125955682Smarkm        }
126055682Smarkm      }
126155682Smarkm    }
126255682Smarkm    if (shouldDumpSymLevel(opts::pretty::SymLevel::Thunks)) {
126355682Smarkm      if (auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>()) {
126455682Smarkm        CompilandDumper Dumper(Printer);
126555682Smarkm        while (auto Thunk = Thunks->getNext())
126655682Smarkm          Dumper.dump(*Thunk);
126755682Smarkm      }
126855682Smarkm    }
126955682Smarkm    Printer.Unindent();
127055682Smarkm  }
127155682Smarkm  if (opts::pretty::Externals) {
127255682Smarkm    Printer.NewLine();
127355682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
127455682Smarkm    Printer.Indent();
127555682Smarkm    ExternalSymbolDumper Dumper(Printer);
127655682Smarkm    Dumper.start(*GlobalScope);
127755682Smarkm  }
127855682Smarkm  if (opts::pretty::Lines) {
127955682Smarkm    Printer.NewLine();
128055682Smarkm  }
128155682Smarkm  if (opts::pretty::InjectedSources) {
128255682Smarkm    Printer.NewLine();
128355682Smarkm    WithColor(Printer, PDB_ColorItem::SectionHeader).get()
128455682Smarkm        << "---INJECTED SOURCES---";
128555682Smarkm    AutoIndent Indent1(Printer);
128655682Smarkm    dumpInjectedSources(Printer, *Session);
128755682Smarkm  }
128855682Smarkm
128955682Smarkm  Printer.NewLine();
129055682Smarkm  outs().flush();
129155682Smarkm}
129255682Smarkm
129355682Smarkmstatic void mergePdbs() {
129455682Smarkm  BumpPtrAllocator Allocator;
129555682Smarkm  MergingTypeTableBuilder MergedTpi(Allocator);
129655682Smarkm  MergingTypeTableBuilder MergedIpi(Allocator);
129755682Smarkm
129855682Smarkm  // Create a Tpi and Ipi type table with all types from all input files.
129955682Smarkm  for (const auto &Path : opts::merge::InputFilenames) {
130055682Smarkm    std::unique_ptr<IPDBSession> Session;
130155682Smarkm    auto &File = loadPDB(Path, Session);
130255682Smarkm    SmallVector<TypeIndex, 128> TypeMap;
130355682Smarkm    SmallVector<TypeIndex, 128> IdMap;
130455682Smarkm    if (File.hasPDBTpiStream()) {
130555682Smarkm      auto &Tpi = ExitOnErr(File.getPDBTpiStream());
130655682Smarkm      ExitOnErr(
130755682Smarkm          codeview::mergeTypeRecords(MergedTpi, TypeMap, Tpi.typeArray()));
130855682Smarkm    }
130955682Smarkm    if (File.hasPDBIpiStream()) {
131055682Smarkm      auto &Ipi = ExitOnErr(File.getPDBIpiStream());
131155682Smarkm      ExitOnErr(codeview::mergeIdRecords(MergedIpi, TypeMap, IdMap,
131255682Smarkm                                         Ipi.typeArray()));
131355682Smarkm    }
131455682Smarkm  }
131555682Smarkm
131655682Smarkm  // Then write the PDB.
131755682Smarkm  PDBFileBuilder Builder(Allocator);
131855682Smarkm  ExitOnErr(Builder.initialize(4096));
131955682Smarkm  // Add each of the reserved streams.  We might not put any data in them,
132055682Smarkm  // but at least they have to be present.
132155682Smarkm  for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
132255682Smarkm    ExitOnErr(Builder.getMsfBuilder().addStream(0));
132355682Smarkm
132455682Smarkm  auto &DestTpi = Builder.getTpiBuilder();
132555682Smarkm  auto &DestIpi = Builder.getIpiBuilder();
132655682Smarkm  MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, const CVType &Type) {
132755682Smarkm    DestTpi.addTypeRecord(Type.RecordData, None);
132855682Smarkm  });
132955682Smarkm  MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, const CVType &Type) {
133055682Smarkm    DestIpi.addTypeRecord(Type.RecordData, None);
133155682Smarkm  });
133255682Smarkm  Builder.getInfoBuilder().addFeature(PdbRaw_FeatureSig::VC140);
133355682Smarkm
133455682Smarkm  SmallString<64> OutFile(opts::merge::PdbOutputFile);
133555682Smarkm  if (OutFile.empty()) {
133655682Smarkm    OutFile = opts::merge::InputFilenames[0];
133755682Smarkm    llvm::sys::path::replace_extension(OutFile, "merged.pdb");
133855682Smarkm  }
133955682Smarkm
134055682Smarkm  codeview::GUID IgnoredOutGuid;
134155682Smarkm  ExitOnErr(Builder.commit(OutFile, &IgnoredOutGuid));
134255682Smarkm}
134355682Smarkm
134455682Smarkmstatic void explain() {
134555682Smarkm  std::unique_ptr<IPDBSession> Session;
134655682Smarkm  InputFile IF =
134755682Smarkm      ExitOnErr(InputFile::open(opts::explain::InputFilename.front(), true));
134855682Smarkm
134955682Smarkm  for (uint64_t Off : opts::explain::Offsets) {
135055682Smarkm    auto O = llvm::make_unique<ExplainOutputStyle>(IF, Off);
135155682Smarkm
135255682Smarkm    ExitOnErr(O->dump());
135355682Smarkm  }
135455682Smarkm}
135555682Smarkm
135655682Smarkmstatic void exportStream() {
135755682Smarkm  std::unique_ptr<IPDBSession> Session;
135855682Smarkm  PDBFile &File = loadPDB(opts::exportstream::InputFilename.front(), Session);
135955682Smarkm
136055682Smarkm  std::unique_ptr<MappedBlockStream> SourceStream;
136155682Smarkm  uint32_t Index = 0;
136255682Smarkm  bool Success = false;
136355682Smarkm  std::string OutFileName = opts::exportstream::OutputFile;
136455682Smarkm
136555682Smarkm  if (!opts::exportstream::ForceName) {
136655682Smarkm    // First try to parse it as an integer, if it fails fall back to treating it
136755682Smarkm    // as a named stream.
136855682Smarkm    if (to_integer(opts::exportstream::Stream, Index)) {
136955682Smarkm      if (Index >= File.getNumStreams()) {
137055682Smarkm        errs() << "Error: " << Index << " is not a valid stream index.\n";
137155682Smarkm        exit(1);
137255682Smarkm      }
137355682Smarkm      Success = true;
137455682Smarkm      outs() << "Dumping contents of stream index " << Index << " to file "
137555682Smarkm             << OutFileName << ".\n";
137655682Smarkm    }
137755682Smarkm  }
137855682Smarkm
137955682Smarkm  if (!Success) {
138055682Smarkm    InfoStream &IS = cantFail(File.getPDBInfoStream());
138155682Smarkm    Index = ExitOnErr(IS.getNamedStreamIndex(opts::exportstream::Stream));
138255682Smarkm    outs() << "Dumping contents of stream '" << opts::exportstream::Stream
138355682Smarkm           << "' (index " << Index << ") to file " << OutFileName << ".\n";
138455682Smarkm  }
138555682Smarkm
138655682Smarkm  SourceStream = File.createIndexedStream(Index);
138755682Smarkm  auto OutFile = ExitOnErr(
138855682Smarkm      FileOutputBuffer::create(OutFileName, SourceStream->getLength()));
138955682Smarkm  FileBufferByteStream DestStream(std::move(OutFile), llvm::support::little);
139055682Smarkm  BinaryStreamWriter Writer(DestStream);
139155682Smarkm  ExitOnErr(Writer.writeStreamRef(*SourceStream));
139255682Smarkm  ExitOnErr(DestStream.commit());
139355682Smarkm}
139455682Smarkm
139555682Smarkmstatic bool parseRange(StringRef Str,
139655682Smarkm                       Optional<opts::bytes::NumberRange> &Parsed) {
139755682Smarkm  if (Str.empty())
139855682Smarkm    return true;
139955682Smarkm
140055682Smarkm  llvm::Regex R("^([^-]+)(-([^-]+))?$");
140155682Smarkm  llvm::SmallVector<llvm::StringRef, 2> Matches;
140255682Smarkm  if (!R.match(Str, &Matches))
140355682Smarkm    return false;
140455682Smarkm
140555682Smarkm  Parsed.emplace();
140655682Smarkm  if (!to_integer(Matches[1], Parsed->Min))
140755682Smarkm    return false;
140855682Smarkm
140955682Smarkm  if (!Matches[3].empty()) {
141055682Smarkm    Parsed->Max.emplace();
141155682Smarkm    if (!to_integer(Matches[3], *Parsed->Max))
141255682Smarkm      return false;
141355682Smarkm  }
141455682Smarkm  return true;
141555682Smarkm}
141655682Smarkm
141755682Smarkmstatic void simplifyChunkList(llvm::cl::list<opts::ModuleSubsection> &Chunks) {
141855682Smarkm  // If this list contains "All" plus some other stuff, remove the other stuff
141955682Smarkm  // and just keep "All" in the list.
142055682Smarkm  if (!llvm::is_contained(Chunks, opts::ModuleSubsection::All))
142155682Smarkm    return;
142255682Smarkm  Chunks.reset();
142355682Smarkm  Chunks.push_back(opts::ModuleSubsection::All);
142455682Smarkm}
142555682Smarkm
142655682Smarkmint main(int Argc, const char **Argv) {
142755682Smarkm  InitLLVM X(Argc, Argv);
142855682Smarkm  ExitOnErr.setBanner("llvm-pdbutil: ");
142955682Smarkm
143055682Smarkm  cl::ParseCommandLineOptions(Argc, Argv, "LLVM PDB Dumper\n");
143155682Smarkm
143255682Smarkm  if (opts::BytesSubcommand) {
143355682Smarkm    if (!parseRange(opts::bytes::DumpBlockRangeOpt,
143455682Smarkm                    opts::bytes::DumpBlockRange)) {
143555682Smarkm      errs() << "Argument '" << opts::bytes::DumpBlockRangeOpt
143655682Smarkm             << "' invalid format.\n";
143755682Smarkm      errs().flush();
143855682Smarkm      exit(1);
143955682Smarkm    }
144055682Smarkm    if (!parseRange(opts::bytes::DumpByteRangeOpt,
144155682Smarkm                    opts::bytes::DumpByteRange)) {
144255682Smarkm      errs() << "Argument '" << opts::bytes::DumpByteRangeOpt
144355682Smarkm             << "' invalid format.\n";
144455682Smarkm      errs().flush();
144555682Smarkm      exit(1);
144655682Smarkm    }
144755682Smarkm  }
144855682Smarkm
144955682Smarkm  if (opts::DumpSubcommand) {
145055682Smarkm    if (opts::dump::RawAll) {
145155682Smarkm      opts::dump::DumpGlobals = true;
145255682Smarkm      opts::dump::DumpFpo = true;
145355682Smarkm      opts::dump::DumpInlineeLines = true;
145455682Smarkm      opts::dump::DumpIds = true;
145555682Smarkm      opts::dump::DumpIdExtras = true;
145655682Smarkm      opts::dump::DumpLines = true;
145755682Smarkm      opts::dump::DumpModules = true;
145855682Smarkm      opts::dump::DumpModuleFiles = true;
145955682Smarkm      opts::dump::DumpPublics = true;
146055682Smarkm      opts::dump::DumpSectionContribs = true;
146155682Smarkm      opts::dump::DumpSectionHeaders = true;
146255682Smarkm      opts::dump::DumpSectionMap = true;
146355682Smarkm      opts::dump::DumpStreams = true;
146455682Smarkm      opts::dump::DumpStreamBlocks = true;
146555682Smarkm      opts::dump::DumpStringTable = true;
146655682Smarkm      opts::dump::DumpStringTableDetails = true;
146755682Smarkm      opts::dump::DumpSummary = true;
146855682Smarkm      opts::dump::DumpSymbols = true;
146955682Smarkm      opts::dump::DumpSymbolStats = true;
147055682Smarkm      opts::dump::DumpTypes = true;
147155682Smarkm      opts::dump::DumpTypeExtras = true;
147255682Smarkm      opts::dump::DumpUdtStats = true;
147355682Smarkm      opts::dump::DumpXme = true;
147455682Smarkm      opts::dump::DumpXmi = true;
147555682Smarkm    }
147655682Smarkm  }
147755682Smarkm  if (opts::PdbToYamlSubcommand) {
147855682Smarkm    if (opts::pdb2yaml::All) {
147955682Smarkm      opts::pdb2yaml::StreamMetadata = true;
148055682Smarkm      opts::pdb2yaml::StreamDirectory = true;
148155682Smarkm      opts::pdb2yaml::PdbStream = true;
148255682Smarkm      opts::pdb2yaml::StringTable = true;
148355682Smarkm      opts::pdb2yaml::DbiStream = true;
148455682Smarkm      opts::pdb2yaml::TpiStream = true;
148555682Smarkm      opts::pdb2yaml::IpiStream = true;
148655682Smarkm      opts::pdb2yaml::PublicsStream = true;
148755682Smarkm      opts::pdb2yaml::DumpModules = true;
148855682Smarkm      opts::pdb2yaml::DumpModuleFiles = true;
148955682Smarkm      opts::pdb2yaml::DumpModuleSyms = true;
149055682Smarkm      opts::pdb2yaml::DumpModuleSubsections.push_back(
149155682Smarkm          opts::ModuleSubsection::All);
149255682Smarkm    }
149355682Smarkm    simplifyChunkList(opts::pdb2yaml::DumpModuleSubsections);
149455682Smarkm
149555682Smarkm    if (opts::pdb2yaml::DumpModuleSyms || opts::pdb2yaml::DumpModuleFiles)
149655682Smarkm      opts::pdb2yaml::DumpModules = true;
149755682Smarkm
149855682Smarkm    if (opts::pdb2yaml::DumpModules)
149955682Smarkm      opts::pdb2yaml::DbiStream = true;
150055682Smarkm  }
150155682Smarkm
150255682Smarkm  llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
150355682Smarkm
150455682Smarkm  if (opts::PdbToYamlSubcommand) {
150555682Smarkm    pdb2Yaml(opts::pdb2yaml::InputFilename.front());
150655682Smarkm  } else if (opts::YamlToPdbSubcommand) {
150755682Smarkm    if (opts::yaml2pdb::YamlPdbOutputFile.empty()) {
150855682Smarkm      SmallString<16> OutputFilename(opts::yaml2pdb::InputFilename.getValue());
150955682Smarkm      sys::path::replace_extension(OutputFilename, ".pdb");
151055682Smarkm      opts::yaml2pdb::YamlPdbOutputFile = OutputFilename.str();
151155682Smarkm    }
151255682Smarkm    yamlToPdb(opts::yaml2pdb::InputFilename);
151355682Smarkm  } else if (opts::DiaDumpSubcommand) {
151455682Smarkm    llvm::for_each(opts::diadump::InputFilenames, dumpDia);
151555682Smarkm  } else if (opts::PrettySubcommand) {
151655682Smarkm    if (opts::pretty::Lines)
151755682Smarkm      opts::pretty::Compilands = true;
151855682Smarkm
151955682Smarkm    if (opts::pretty::All) {
152055682Smarkm      opts::pretty::Compilands = true;
152155682Smarkm      opts::pretty::Symbols = true;
152255682Smarkm      opts::pretty::Globals = true;
152355682Smarkm      opts::pretty::Types = true;
152455682Smarkm      opts::pretty::Externals = true;
152555682Smarkm      opts::pretty::Lines = true;
152655682Smarkm    }
152755682Smarkm
152855682Smarkm    if (opts::pretty::Types) {
152955682Smarkm      opts::pretty::Classes = true;
153055682Smarkm      opts::pretty::Typedefs = true;
153155682Smarkm      opts::pretty::Enums = true;
153255682Smarkm      opts::pretty::Pointers = true;
153355682Smarkm      opts::pretty::Funcsigs = true;
153455682Smarkm    }
153555682Smarkm
153655682Smarkm    // When adding filters for excluded compilands and types, we need to
153755682Smarkm    // remember that these are regexes.  So special characters such as * and \
153855682Smarkm    // need to be escaped in the regex.  In the case of a literal \, this means
153955682Smarkm    // it needs to be escaped again in the C++.  So matching a single \ in the
154055682Smarkm    // input requires 4 \es in the C++.
154155682Smarkm    if (opts::pretty::ExcludeCompilerGenerated) {
154255682Smarkm      opts::pretty::ExcludeTypes.push_back("__vc_attributes");
154355682Smarkm      opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
154455682Smarkm    }
154555682Smarkm    if (opts::pretty::ExcludeSystemLibraries) {
154655682Smarkm      opts::pretty::ExcludeCompilands.push_back(
154755682Smarkm          "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
154855682Smarkm      opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
154955682Smarkm      opts::pretty::ExcludeCompilands.push_back(
155055682Smarkm          "d:\\\\th.obj.x86fre\\\\minkernel");
155155682Smarkm    }
155255682Smarkm    llvm::for_each(opts::pretty::InputFilenames, dumpPretty);
155355682Smarkm  } else if (opts::DumpSubcommand) {
155455682Smarkm    llvm::for_each(opts::dump::InputFilenames, dumpRaw);
155555682Smarkm  } else if (opts::BytesSubcommand) {
155655682Smarkm    llvm::for_each(opts::bytes::InputFilenames, dumpBytes);
155755682Smarkm  } else if (opts::MergeSubcommand) {
155855682Smarkm    if (opts::merge::InputFilenames.size() < 2) {
155955682Smarkm      errs() << "merge subcommand requires at least 2 input files.\n";
156055682Smarkm      exit(1);
156155682Smarkm    }
156255682Smarkm    mergePdbs();
156355682Smarkm  } else if (opts::ExplainSubcommand) {
156455682Smarkm    explain();
156555682Smarkm  } else if (opts::ExportSubcommand) {
156655682Smarkm    exportStream();
156755682Smarkm  }
156855682Smarkm
156955682Smarkm  outs().flush();
157055682Smarkm  return 0;
157155682Smarkm}
157255682Smarkm