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