1 //===- llvm-pdbdump.cpp - Dump debug info from a PDB file -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Dumps debug information present in PDB files.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-pdbdump.h"
18 #include "LLVMOutputStyle.h"
19 #include "LinePrinter.h"
20 #include "OutputStyle.h"
21 #include "PrettyCompilandDumper.h"
22 #include "PrettyExternalSymbolDumper.h"
23 #include "PrettyFunctionDumper.h"
24 #include "PrettyTypeDumper.h"
25 #include "PrettyVariableDumper.h"
26 #include "YAMLOutputStyle.h"
28 #include "llvm/ADT/ArrayRef.h"
29 #include "llvm/ADT/BitVector.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/StringExtras.h"
33 #include "llvm/Config/config.h"
34 #include "llvm/DebugInfo/CodeView/DebugChecksumsSubsection.h"
35 #include "llvm/DebugInfo/CodeView/DebugInlineeLinesSubsection.h"
36 #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h"
37 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
38 #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
39 #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
40 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
41 #include "llvm/DebugInfo/PDB/GenericError.h"
42 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
43 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
44 #include "llvm/DebugInfo/PDB/IPDBSession.h"
45 #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
46 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
47 #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
48 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
49 #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
50 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
51 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
52 #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
53 #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
54 #include "llvm/DebugInfo/PDB/Native/RawConstants.h"
55 #include "llvm/DebugInfo/PDB/Native/RawError.h"
56 #include "llvm/DebugInfo/PDB/Native/TpiStream.h"
57 #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
58 #include "llvm/DebugInfo/PDB/PDB.h"
59 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
60 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
61 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
62 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
63 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
64 #include "llvm/Support/BinaryByteStream.h"
65 #include "llvm/Support/COM.h"
66 #include "llvm/Support/CommandLine.h"
67 #include "llvm/Support/ConvertUTF.h"
68 #include "llvm/Support/FileOutputBuffer.h"
69 #include "llvm/Support/FileSystem.h"
70 #include "llvm/Support/Format.h"
71 #include "llvm/Support/ManagedStatic.h"
72 #include "llvm/Support/MemoryBuffer.h"
73 #include "llvm/Support/Path.h"
74 #include "llvm/Support/PrettyStackTrace.h"
75 #include "llvm/Support/Process.h"
76 #include "llvm/Support/Regex.h"
77 #include "llvm/Support/ScopedPrinter.h"
78 #include "llvm/Support/Signals.h"
79 #include "llvm/Support/raw_ostream.h"
82 using namespace llvm::codeview;
83 using namespace llvm::msf;
84 using namespace llvm::pdb;
88 cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
90 PrettySubcommand("pretty",
91 "Dump semantic information about types and symbols");
93 cl::SubCommand DiffSubcommand("diff", "Diff the contents of 2 PDB files");
96 YamlToPdbSubcommand("yaml2pdb",
97 "Generate a PDB file from a YAML description");
99 PdbToYamlSubcommand("pdb2yaml",
100 "Generate a detailed YAML description of a PDB File");
103 AnalyzeSubcommand("analyze",
104 "Analyze various aspects of a PDB's structure");
106 cl::SubCommand MergeSubcommand("merge",
107 "Merge multiple PDBs into a single PDB");
109 cl::OptionCategory TypeCategory("Symbol Type Options");
110 cl::OptionCategory FilterCategory("Filtering and Sorting Options");
111 cl::OptionCategory OtherOptions("Other Options");
114 cl::list<std::string> InputFilenames(cl::Positional,
115 cl::desc("<input PDB files>"),
116 cl::OneOrMore, cl::sub(PrettySubcommand));
118 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
119 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
120 cl::opt<bool> Symbols("module-syms",
121 cl::desc("Display symbols for each compiland"),
122 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
123 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
124 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
125 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
126 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
127 cl::list<SymLevel> SymTypes(
128 "sym-types", cl::desc("Type of symbols to dump (default all)"),
129 cl::cat(TypeCategory), cl::sub(PrettySubcommand), cl::ZeroOrMore,
131 clEnumValN(SymLevel::Thunks, "thunks", "Display thunk symbols"),
132 clEnumValN(SymLevel::Data, "data", "Display data symbols"),
133 clEnumValN(SymLevel::Functions, "funcs", "Display function symbols"),
134 clEnumValN(SymLevel::All, "all", "Display all symbols (default)")));
138 cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
139 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
140 cl::opt<bool> Classes("classes", cl::desc("Display class types"),
141 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
142 cl::opt<bool> Enums("enums", cl::desc("Display enum types"),
143 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
144 cl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
145 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
146 cl::opt<SymbolSortMode> SymbolOrder(
147 "symbol-order", cl::desc("symbol sort order"),
148 cl::init(SymbolSortMode::None),
149 cl::values(clEnumValN(SymbolSortMode::None, "none",
150 "Undefined / no particular sort order"),
151 clEnumValN(SymbolSortMode::Name, "name", "Sort symbols by name"),
152 clEnumValN(SymbolSortMode::Size, "size",
153 "Sort symbols by size")),
154 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
156 cl::opt<ClassSortMode> ClassOrder(
157 "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
159 clEnumValN(ClassSortMode::None, "none",
160 "Undefined / no particular sort order"),
161 clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
162 clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
163 clEnumValN(ClassSortMode::Padding, "padding",
164 "Sort classes by amount of padding"),
165 clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
166 "Sort classes by percentage of space consumed by padding"),
167 clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
168 "Sort classes by amount of immediate padding"),
169 clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
170 "Sort classes by percentage of space consumed by immediate "
172 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
174 cl::opt<ClassDefinitionFormat> ClassFormat(
175 "class-definitions", cl::desc("Class definition format"),
176 cl::init(ClassDefinitionFormat::All),
178 clEnumValN(ClassDefinitionFormat::All, "all",
179 "Display all class members including data, constants, "
180 "typedefs, functions, etc"),
181 clEnumValN(ClassDefinitionFormat::Layout, "layout",
182 "Only display members that contribute to class size."),
183 clEnumValN(ClassDefinitionFormat::None, "none",
184 "Don't display class definitions")),
185 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
186 cl::opt<uint32_t> ClassRecursionDepth(
187 "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
188 cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
190 cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
191 cl::sub(PrettySubcommand));
193 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
194 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
196 cl::opt<uint64_t> LoadAddress(
198 cl::desc("Assume the module is loaded at the specified address"),
199 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
200 cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
201 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
202 cl::opt<cl::boolOrDefault>
203 ColorOutput("color-output",
204 cl::desc("Override use of color (default = isatty)"),
205 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
206 cl::list<std::string> ExcludeTypes(
207 "exclude-types", cl::desc("Exclude types by regular expression"),
208 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
209 cl::list<std::string> ExcludeSymbols(
210 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
211 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
212 cl::list<std::string> ExcludeCompilands(
213 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
214 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
216 cl::list<std::string> IncludeTypes(
218 cl::desc("Include only types which match a regular expression"),
219 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
220 cl::list<std::string> IncludeSymbols(
222 cl::desc("Include only symbols which match a regular expression"),
223 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
224 cl::list<std::string> IncludeCompilands(
225 "include-compilands",
226 cl::desc("Include only compilands those which match a regular expression"),
227 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
228 cl::opt<uint32_t> SizeThreshold(
229 "min-type-size", cl::desc("Displays only those types which are greater "
230 "than or equal to the specified size."),
231 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
232 cl::opt<uint32_t> PaddingThreshold(
233 "min-class-padding", cl::desc("Displays only those classes which have at "
234 "least the specified amount of padding."),
235 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
236 cl::opt<uint32_t> ImmediatePaddingThreshold(
237 "min-class-padding-imm",
238 cl::desc("Displays only those classes which have at least the specified "
239 "amount of immediate padding, ignoring padding internal to bases "
241 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
243 cl::opt<bool> ExcludeCompilerGenerated(
244 "no-compiler-generated",
245 cl::desc("Don't show compiler generated types and symbols"),
246 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
248 ExcludeSystemLibraries("no-system-libs",
249 cl::desc("Don't show symbols from system libraries"),
250 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
252 cl::opt<bool> NoEnumDefs("no-enum-definitions",
253 cl::desc("Don't display full enum definitions"),
254 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
258 cl::opt<bool> Pedantic("pedantic",
259 cl::desc("Finds all differences (even structural ones "
260 "that produce otherwise identical PDBs)"),
261 cl::sub(DiffSubcommand));
263 cl::list<std::string> InputFilenames(cl::Positional,
264 cl::desc("<first> <second>"),
265 cl::OneOrMore, cl::sub(DiffSubcommand));
270 cl::OptionCategory MsfOptions("MSF Container Options");
271 cl::OptionCategory TypeOptions("Type Record Options");
272 cl::OptionCategory FileOptions("Module & File Options");
273 cl::OptionCategory SymbolOptions("Symbol Options");
274 cl::OptionCategory MiscOptions("Miscellaneous Options");
277 cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
278 cl::cat(MsfOptions), cl::sub(RawSubcommand));
279 cl::opt<bool> DumpStreamBlocks("stream-blocks",
280 cl::desc("dump PDB stream blocks"),
281 cl::cat(MsfOptions), cl::sub(RawSubcommand));
282 cl::opt<bool> DumpStreamSummary("stream-summary",
283 cl::desc("dump summary of the PDB streams"),
284 cl::cat(MsfOptions), cl::sub(RawSubcommand));
285 cl::opt<bool> DumpPageStats(
287 cl::desc("dump allocation stats of the pages in the MSF file"),
288 cl::cat(MsfOptions), cl::sub(RawSubcommand));
290 DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
291 cl::desc("Dump binary data from specified range."),
292 cl::cat(MsfOptions), cl::sub(RawSubcommand));
293 llvm::Optional<BlockRange> DumpBlockRange;
295 cl::list<std::string>
296 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
297 cl::desc("Dump binary data from specified streams. Format "
298 "is SN[:Start][@Size]"),
299 cl::cat(MsfOptions), cl::sub(RawSubcommand));
303 CompactRecords("compact-records",
304 cl::desc("Dump type and symbol records with less detail"),
305 cl::cat(TypeOptions), cl::sub(RawSubcommand));
308 DumpTpiRecords("tpi-records",
309 cl::desc("dump CodeView type records from TPI stream"),
310 cl::cat(TypeOptions), cl::sub(RawSubcommand));
311 cl::opt<bool> DumpTpiRecordBytes(
313 cl::desc("dump CodeView type record raw bytes from TPI stream"),
314 cl::cat(TypeOptions), cl::sub(RawSubcommand));
315 cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
316 cl::cat(TypeOptions), cl::sub(RawSubcommand));
318 DumpIpiRecords("ipi-records",
319 cl::desc("dump CodeView type records from IPI stream"),
320 cl::cat(TypeOptions), cl::sub(RawSubcommand));
321 cl::opt<bool> DumpIpiRecordBytes(
323 cl::desc("dump CodeView type record raw bytes from IPI stream"),
324 cl::cat(TypeOptions), cl::sub(RawSubcommand));
326 // MODULE & FILE OPTIONS
327 cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
328 cl::cat(FileOptions), cl::sub(RawSubcommand));
329 cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
330 cl::cat(FileOptions), cl::sub(RawSubcommand));
331 cl::opt<bool> DumpLineInfo("line-info",
332 cl::desc("dump file and line information"),
333 cl::cat(FileOptions), cl::sub(RawSubcommand));
336 cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
337 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
338 cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
339 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
340 cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
341 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
343 DumpSymRecordBytes("sym-record-bytes",
344 cl::desc("dump CodeView symbol record raw bytes"),
345 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
347 // MISCELLANEOUS OPTIONS
348 cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
349 cl::cat(MiscOptions), cl::sub(RawSubcommand));
351 cl::opt<bool> DumpSectionContribs("section-contribs",
352 cl::desc("dump section contributions"),
353 cl::cat(MiscOptions), cl::sub(RawSubcommand));
354 cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
355 cl::cat(MiscOptions), cl::sub(RawSubcommand));
356 cl::opt<bool> DumpSectionHeaders("section-headers",
357 cl::desc("dump section headers"),
358 cl::cat(MiscOptions), cl::sub(RawSubcommand));
359 cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
360 cl::sub(RawSubcommand));
362 cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
363 cl::cat(MiscOptions), cl::sub(RawSubcommand));
365 cl::list<std::string> InputFilenames(cl::Positional,
366 cl::desc("<input PDB files>"),
367 cl::OneOrMore, cl::sub(RawSubcommand));
372 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
373 cl::sub(YamlToPdbSubcommand));
375 cl::opt<std::string> InputFilename(cl::Positional,
376 cl::desc("<input YAML file>"), cl::Required,
377 cl::sub(YamlToPdbSubcommand));
381 cl::opt<bool> All("all",
382 cl::desc("Dump everything we know how to dump."),
383 cl::sub(PdbToYamlSubcommand), cl::init(false));
385 NoFileHeaders("no-file-headers",
386 cl::desc("Do not dump MSF file headers (you will not be able "
387 "to generate a fresh PDB from the resulting YAML)"),
388 cl::sub(PdbToYamlSubcommand), cl::init(false));
389 cl::opt<bool> Minimal("minimal",
390 cl::desc("Don't write fields with default values"),
391 cl::sub(PdbToYamlSubcommand), cl::init(false));
393 cl::opt<bool> StreamMetadata(
395 cl::desc("Dump the number of streams and each stream's size"),
396 cl::sub(PdbToYamlSubcommand), cl::init(false));
397 cl::opt<bool> StreamDirectory(
399 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
400 cl::sub(PdbToYamlSubcommand), cl::init(false));
401 cl::opt<bool> PdbStream("pdb-stream",
402 cl::desc("Dump the PDB Stream (Stream 1)"),
403 cl::sub(PdbToYamlSubcommand), cl::init(false));
405 cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
406 cl::sub(PdbToYamlSubcommand), cl::init(false));
408 cl::opt<bool> DbiStream("dbi-stream",
409 cl::desc("Dump the DBI Stream (Stream 2)"),
410 cl::sub(PdbToYamlSubcommand), cl::init(false));
412 DbiModuleInfo("dbi-module-info",
413 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
414 cl::sub(PdbToYamlSubcommand), cl::init(false));
416 cl::opt<bool> DbiModuleSyms(
418 cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
419 cl::sub(PdbToYamlSubcommand), cl::init(false));
421 cl::opt<bool> DbiModuleSourceFileInfo(
422 "dbi-module-source-info",
424 "Dump DBI Module Source File Information (implies -dbi-module-info)"),
425 cl::sub(PdbToYamlSubcommand), cl::init(false));
428 DbiModuleSourceLineInfo("dbi-module-lines",
429 cl::desc("Dump DBI Module Source Line Information "
430 "(implies -dbi-module-source-info)"),
431 cl::sub(PdbToYamlSubcommand), cl::init(false));
433 cl::opt<bool> TpiStream("tpi-stream",
434 cl::desc("Dump the TPI Stream (Stream 3)"),
435 cl::sub(PdbToYamlSubcommand), cl::init(false));
437 cl::opt<bool> IpiStream("ipi-stream",
438 cl::desc("Dump the IPI Stream (Stream 5)"),
439 cl::sub(PdbToYamlSubcommand), cl::init(false));
441 cl::list<std::string> InputFilename(cl::Positional,
442 cl::desc("<input PDB file>"), cl::Required,
443 cl::sub(PdbToYamlSubcommand));
447 cl::opt<bool> StringTable("hash-collisions", cl::desc("Find hash collisions"),
448 cl::sub(AnalyzeSubcommand), cl::init(false));
449 cl::list<std::string> InputFilename(cl::Positional,
450 cl::desc("<input PDB file>"), cl::Required,
451 cl::sub(AnalyzeSubcommand));
455 cl::list<std::string> InputFilenames(cl::Positional,
456 cl::desc("<input PDB files>"),
457 cl::OneOrMore, cl::sub(MergeSubcommand));
459 PdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
460 cl::sub(MergeSubcommand));
464 static ExitOnError ExitOnErr;
466 static void yamlToPdb(StringRef Path) {
467 BumpPtrAllocator Allocator;
468 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
469 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
470 /*RequiresNullTerminator=*/false);
472 if (ErrorOrBuffer.getError()) {
473 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
476 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
478 llvm::yaml::Input In(Buffer->getBuffer());
479 In.setContext(&Allocator);
480 pdb::yaml::PdbObject YamlObj(Allocator);
483 PDBFileBuilder Builder(Allocator);
485 uint32_t BlockSize = 4096;
486 if (YamlObj.Headers.hasValue())
487 BlockSize = YamlObj.Headers->SuperBlock.BlockSize;
488 ExitOnErr(Builder.initialize(BlockSize));
489 // Add each of the reserved streams. We ignore stream metadata in the
490 // yaml, because we will reconstruct our own view of the streams. For
491 // example, the YAML may say that there were 20 streams in the original
492 // PDB, but maybe we only dump a subset of those 20 streams, so we will
493 // have fewer, and the ones we do have may end up with different indices
494 // than the ones in the original PDB. So we just start with a clean slate.
495 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
496 ExitOnErr(Builder.getMsfBuilder().addStream(0));
498 if (YamlObj.StringTable.hasValue()) {
499 auto &Strings = Builder.getStringTableBuilder();
500 for (auto S : *YamlObj.StringTable)
504 pdb::yaml::PdbInfoStream DefaultInfoStream;
505 pdb::yaml::PdbDbiStream DefaultDbiStream;
506 pdb::yaml::PdbTpiStream DefaultTpiStream;
507 pdb::yaml::PdbTpiStream DefaultIpiStream;
509 const auto &Info = YamlObj.PdbStream.getValueOr(DefaultInfoStream);
511 auto &InfoBuilder = Builder.getInfoBuilder();
512 InfoBuilder.setAge(Info.Age);
513 InfoBuilder.setGuid(Info.Guid);
514 InfoBuilder.setSignature(Info.Signature);
515 InfoBuilder.setVersion(Info.Version);
516 for (auto F : Info.Features)
517 InfoBuilder.addFeature(F);
519 auto &Strings = Builder.getStringTableBuilder().getStrings();
521 const auto &Dbi = YamlObj.DbiStream.getValueOr(DefaultDbiStream);
522 auto &DbiBuilder = Builder.getDbiBuilder();
523 DbiBuilder.setAge(Dbi.Age);
524 DbiBuilder.setBuildNumber(Dbi.BuildNumber);
525 DbiBuilder.setFlags(Dbi.Flags);
526 DbiBuilder.setMachineType(Dbi.MachineType);
527 DbiBuilder.setPdbDllRbld(Dbi.PdbDllRbld);
528 DbiBuilder.setPdbDllVersion(Dbi.PdbDllVersion);
529 DbiBuilder.setVersionHeader(Dbi.VerHeader);
530 for (const auto &MI : Dbi.ModInfos) {
531 auto &ModiBuilder = ExitOnErr(DbiBuilder.addModuleInfo(MI.Mod));
532 ModiBuilder.setObjFileName(MI.Obj);
534 for (auto S : MI.SourceFiles)
535 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
536 if (MI.Modi.hasValue()) {
537 const auto &ModiStream = *MI.Modi;
538 for (auto Symbol : ModiStream.Symbols)
539 ModiBuilder.addSymbol(Symbol.toCodeViewSymbol(Allocator));
541 if (MI.FileLineInfo.hasValue()) {
542 const auto &FLI = *MI.FileLineInfo;
544 // File Checksums must be emitted before line information, because line
545 // info records use offsets into the checksum buffer to reference a file's
547 auto Checksums = llvm::make_unique<DebugChecksumsSubsection>(Strings);
548 auto &ChecksumRef = *Checksums;
549 if (!FLI.FileChecksums.empty()) {
550 for (auto &FC : FLI.FileChecksums)
551 Checksums->addChecksum(FC.FileName, FC.Kind, FC.ChecksumBytes.Bytes);
553 ModiBuilder.setC13FileChecksums(std::move(Checksums));
555 for (const auto &Fragment : FLI.LineFragments) {
557 llvm::make_unique<DebugLinesSubsection>(ChecksumRef, Strings);
558 Lines->setCodeSize(Fragment.CodeSize);
559 Lines->setRelocationAddress(Fragment.RelocSegment,
560 Fragment.RelocOffset);
561 Lines->setFlags(Fragment.Flags);
562 for (const auto &LC : Fragment.Blocks) {
563 Lines->createBlock(LC.FileName);
564 if (Lines->hasColumnInfo()) {
565 for (const auto &Item : zip(LC.Lines, LC.Columns)) {
566 auto &L = std::get<0>(Item);
567 auto &C = std::get<1>(Item);
568 uint32_t LE = L.LineStart + L.EndDelta;
569 Lines->addLineAndColumnInfo(
570 L.Offset, LineInfo(L.LineStart, LE, L.IsStatement),
571 C.StartColumn, C.EndColumn);
574 for (const auto &L : LC.Lines) {
575 uint32_t LE = L.LineStart + L.EndDelta;
576 Lines->addLineInfo(L.Offset,
577 LineInfo(L.LineStart, LE, L.IsStatement));
581 ModiBuilder.addC13Fragment(std::move(Lines));
584 for (const auto &Inlinee : FLI.Inlinees) {
585 auto Inlinees = llvm::make_unique<DebugInlineeLinesSubsection>(
586 ChecksumRef, Inlinee.HasExtraFiles);
587 for (const auto &Site : Inlinee.Sites) {
588 Inlinees->addInlineSite(TypeIndex(Site.Inlinee), Site.FileName,
590 if (!Inlinee.HasExtraFiles)
593 for (auto EF : Site.ExtraFiles) {
594 Inlinees->addExtraFile(EF);
597 ModiBuilder.addC13Fragment(std::move(Inlinees));
602 auto &TpiBuilder = Builder.getTpiBuilder();
603 const auto &Tpi = YamlObj.TpiStream.getValueOr(DefaultTpiStream);
604 TpiBuilder.setVersionHeader(Tpi.Version);
605 for (const auto &R : Tpi.Records) {
606 CVType Type = R.toCodeViewRecord(Allocator);
607 TpiBuilder.addTypeRecord(Type.RecordData, None);
610 const auto &Ipi = YamlObj.IpiStream.getValueOr(DefaultIpiStream);
611 auto &IpiBuilder = Builder.getIpiBuilder();
612 IpiBuilder.setVersionHeader(Ipi.Version);
613 for (const auto &R : Ipi.Records) {
614 CVType Type = R.toCodeViewRecord(Allocator);
615 IpiBuilder.addTypeRecord(Type.RecordData, None);
618 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
621 static PDBFile &loadPDB(StringRef Path, std::unique_ptr<IPDBSession> &Session) {
622 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
624 NativeSession *NS = static_cast<NativeSession *>(Session.get());
625 return NS->getPDBFile();
628 static void pdb2Yaml(StringRef Path) {
629 std::unique_ptr<IPDBSession> Session;
630 auto &File = loadPDB(Path, Session);
632 auto O = llvm::make_unique<YAMLOutputStyle>(File);
633 O = llvm::make_unique<YAMLOutputStyle>(File);
635 ExitOnErr(O->dump());
638 static void dumpRaw(StringRef Path) {
639 std::unique_ptr<IPDBSession> Session;
640 auto &File = loadPDB(Path, Session);
642 auto O = llvm::make_unique<LLVMOutputStyle>(File);
644 ExitOnErr(O->dump());
647 static void dumpAnalysis(StringRef Path) {
648 std::unique_ptr<IPDBSession> Session;
649 auto &File = loadPDB(Path, Session);
650 auto O = llvm::make_unique<AnalysisStyle>(File);
652 ExitOnErr(O->dump());
655 static void diff(StringRef Path1, StringRef Path2) {
656 std::unique_ptr<IPDBSession> Session1;
657 std::unique_ptr<IPDBSession> Session2;
659 auto &File1 = loadPDB(Path1, Session1);
660 auto &File2 = loadPDB(Path2, Session2);
662 auto O = llvm::make_unique<DiffStyle>(File1, File2);
664 ExitOnErr(O->dump());
667 bool opts::pretty::shouldDumpSymLevel(SymLevel Search) {
668 if (SymTypes.empty())
670 if (llvm::find(SymTypes, Search) != SymTypes.end())
672 if (llvm::find(SymTypes, SymLevel::All) != SymTypes.end())
677 uint32_t llvm::pdb::getTypeLength(const PDBSymbolData &Symbol) {
678 auto SymbolType = Symbol.getType();
679 const IPDBRawSymbol &RawType = SymbolType->getRawSymbol();
681 return RawType.getLength();
684 bool opts::pretty::compareFunctionSymbols(
685 const std::unique_ptr<PDBSymbolFunc> &F1,
686 const std::unique_ptr<PDBSymbolFunc> &F2) {
687 assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
689 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
690 return F1->getName() < F2->getName();
692 // Note that we intentionally sort in descending order on length, since
693 // long functions are more interesting than short functions.
694 return F1->getLength() > F2->getLength();
697 bool opts::pretty::compareDataSymbols(
698 const std::unique_ptr<PDBSymbolData> &F1,
699 const std::unique_ptr<PDBSymbolData> &F2) {
700 assert(opts::pretty::SymbolOrder != opts::pretty::SymbolSortMode::None);
702 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::Name)
703 return F1->getName() < F2->getName();
705 // Note that we intentionally sort in descending order on length, since
706 // large types are more interesting than short ones.
707 return getTypeLength(*F1) > getTypeLength(*F2);
710 static void dumpPretty(StringRef Path) {
711 std::unique_ptr<IPDBSession> Session;
713 const auto ReaderType =
714 opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
715 ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
717 if (opts::pretty::LoadAddress)
718 Session->setLoadAddress(opts::pretty::LoadAddress);
720 auto &Stream = outs();
721 const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
722 ? Stream.has_colors()
723 : opts::pretty::ColorOutput == cl::BOU_TRUE;
724 LinePrinter Printer(2, UseColor, Stream);
726 auto GlobalScope(Session->getGlobalScope());
727 std::string FileName(GlobalScope->getSymbolsFileName());
729 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
730 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
732 uint64_t FileSize = 0;
735 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
736 if (!sys::fs::file_size(FileName, FileSize)) {
737 Printer << ": " << FileSize << " bytes";
739 Printer << ": (Unable to obtain file size)";
743 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
744 Printer << ": " << GlobalScope->getGuid();
747 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
748 Printer << ": " << GlobalScope->getAge();
751 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
753 if (GlobalScope->hasCTypes())
754 outs() << "HasCTypes ";
755 if (GlobalScope->hasPrivateSymbols())
756 outs() << "HasPrivateSymbols ";
759 if (opts::pretty::Compilands) {
761 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
762 << "---COMPILANDS---";
764 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
765 CompilandDumper Dumper(Printer);
766 CompilandDumpFlags options = CompilandDumper::Flags::None;
767 if (opts::pretty::Lines)
768 options = options | CompilandDumper::Flags::Lines;
769 while (auto Compiland = Compilands->getNext())
770 Dumper.start(*Compiland, options);
774 if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs) {
776 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
778 TypeDumper Dumper(Printer);
779 Dumper.start(*GlobalScope);
783 if (opts::pretty::Symbols) {
785 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
787 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
788 CompilandDumper Dumper(Printer);
789 while (auto Compiland = Compilands->getNext())
790 Dumper.start(*Compiland, true);
794 if (opts::pretty::Globals) {
796 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
798 if (shouldDumpSymLevel(opts::pretty::SymLevel::Functions)) {
799 FunctionDumper Dumper(Printer);
800 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
801 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
802 while (auto Function = Functions->getNext()) {
804 Dumper.start(*Function, FunctionDumper::PointerType::None);
807 std::vector<std::unique_ptr<PDBSymbolFunc>> Funcs;
808 while (auto Func = Functions->getNext())
809 Funcs.push_back(std::move(Func));
810 std::sort(Funcs.begin(), Funcs.end(),
811 opts::pretty::compareFunctionSymbols);
812 for (const auto &Func : Funcs) {
814 Dumper.start(*Func, FunctionDumper::PointerType::None);
818 if (shouldDumpSymLevel(opts::pretty::SymLevel::Data)) {
819 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
820 VariableDumper Dumper(Printer);
821 if (opts::pretty::SymbolOrder == opts::pretty::SymbolSortMode::None) {
822 while (auto Var = Vars->getNext())
825 std::vector<std::unique_ptr<PDBSymbolData>> Datas;
826 while (auto Var = Vars->getNext())
827 Datas.push_back(std::move(Var));
828 std::sort(Datas.begin(), Datas.end(), opts::pretty::compareDataSymbols);
829 for (const auto &Var : Datas)
833 if (shouldDumpSymLevel(opts::pretty::SymLevel::Thunks)) {
834 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
835 CompilandDumper Dumper(Printer);
836 while (auto Thunk = Thunks->getNext())
841 if (opts::pretty::Externals) {
843 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
845 ExternalSymbolDumper Dumper(Printer);
846 Dumper.start(*GlobalScope);
848 if (opts::pretty::Lines) {
854 static void mergePdbs() {
855 BumpPtrAllocator Allocator;
856 TypeTableBuilder MergedTpi(Allocator);
857 TypeTableBuilder MergedIpi(Allocator);
859 // Create a Tpi and Ipi type table with all types from all input files.
860 for (const auto &Path : opts::merge::InputFilenames) {
861 std::unique_ptr<IPDBSession> Session;
862 auto &File = loadPDB(Path, Session);
863 SmallVector<TypeIndex, 128> TypeMap;
864 SmallVector<TypeIndex, 128> IdMap;
865 if (File.hasPDBTpiStream()) {
866 auto &Tpi = ExitOnErr(File.getPDBTpiStream());
867 ExitOnErr(codeview::mergeTypeRecords(MergedTpi, TypeMap, nullptr,
870 if (File.hasPDBIpiStream()) {
871 auto &Ipi = ExitOnErr(File.getPDBIpiStream());
872 ExitOnErr(codeview::mergeIdRecords(MergedIpi, TypeMap, IdMap,
877 // Then write the PDB.
878 PDBFileBuilder Builder(Allocator);
879 ExitOnErr(Builder.initialize(4096));
880 // Add each of the reserved streams. We might not put any data in them,
881 // but at least they have to be present.
882 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
883 ExitOnErr(Builder.getMsfBuilder().addStream(0));
885 auto &DestTpi = Builder.getTpiBuilder();
886 auto &DestIpi = Builder.getIpiBuilder();
887 MergedTpi.ForEachRecord([&DestTpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
888 DestTpi.addTypeRecord(Data, None);
890 MergedIpi.ForEachRecord([&DestIpi](TypeIndex TI, ArrayRef<uint8_t> Data) {
891 DestIpi.addTypeRecord(Data, None);
894 SmallString<64> OutFile(opts::merge::PdbOutputFile);
895 if (OutFile.empty()) {
896 OutFile = opts::merge::InputFilenames[0];
897 llvm::sys::path::replace_extension(OutFile, "merged.pdb");
899 ExitOnErr(Builder.commit(OutFile));
902 int main(int argc_, const char *argv_[]) {
903 // Print a stack trace if we signal out.
904 sys::PrintStackTraceOnErrorSignal(argv_[0]);
905 PrettyStackTraceProgram X(argc_, argv_);
907 ExitOnErr.setBanner("llvm-pdbdump: ");
909 SmallVector<const char *, 256> argv;
910 SpecificBumpPtrAllocator<char> ArgAllocator;
911 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
912 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
914 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
916 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
917 if (!opts::raw::DumpBlockRangeOpt.empty()) {
918 llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
919 llvm::SmallVector<llvm::StringRef, 2> Matches;
920 if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
921 errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
922 << "' invalid format.\n";
926 opts::raw::DumpBlockRange.emplace();
927 Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
928 if (!Matches[3].empty()) {
929 opts::raw::DumpBlockRange->Max.emplace();
930 Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
934 if (opts::RawSubcommand) {
935 if (opts::raw::RawAll) {
936 opts::raw::DumpHeaders = true;
937 opts::raw::DumpModules = true;
938 opts::raw::DumpModuleFiles = true;
939 opts::raw::DumpModuleSyms = true;
940 opts::raw::DumpGlobals = true;
941 opts::raw::DumpPublics = true;
942 opts::raw::DumpSectionHeaders = true;
943 opts::raw::DumpStreamSummary = true;
944 opts::raw::DumpPageStats = true;
945 opts::raw::DumpStreamBlocks = true;
946 opts::raw::DumpTpiRecords = true;
947 opts::raw::DumpTpiHash = true;
948 opts::raw::DumpIpiRecords = true;
949 opts::raw::DumpSectionMap = true;
950 opts::raw::DumpSectionContribs = true;
951 opts::raw::DumpLineInfo = true;
952 opts::raw::DumpFpo = true;
953 opts::raw::DumpStringTable = true;
956 if (opts::raw::CompactRecords &&
957 (opts::raw::DumpTpiRecordBytes || opts::raw::DumpIpiRecordBytes)) {
958 errs() << "-compact-records is incompatible with -tpi-record-bytes and "
959 "-ipi-record-bytes.\n";
964 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
966 if (opts::PdbToYamlSubcommand) {
967 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
968 } else if (opts::YamlToPdbSubcommand) {
969 if (opts::yaml2pdb::YamlPdbOutputFile.empty()) {
970 SmallString<16> OutputFilename(opts::yaml2pdb::InputFilename.getValue());
971 sys::path::replace_extension(OutputFilename, ".pdb");
972 opts::yaml2pdb::YamlPdbOutputFile = OutputFilename.str();
974 yamlToPdb(opts::yaml2pdb::InputFilename);
975 } else if (opts::AnalyzeSubcommand) {
976 dumpAnalysis(opts::analyze::InputFilename.front());
977 } else if (opts::PrettySubcommand) {
978 if (opts::pretty::Lines)
979 opts::pretty::Compilands = true;
981 if (opts::pretty::All) {
982 opts::pretty::Compilands = true;
983 opts::pretty::Symbols = true;
984 opts::pretty::Globals = true;
985 opts::pretty::Types = true;
986 opts::pretty::Externals = true;
987 opts::pretty::Lines = true;
990 if (opts::pretty::Types) {
991 opts::pretty::Classes = true;
992 opts::pretty::Typedefs = true;
993 opts::pretty::Enums = true;
996 // When adding filters for excluded compilands and types, we need to
997 // remember that these are regexes. So special characters such as * and \
998 // need to be escaped in the regex. In the case of a literal \, this means
999 // it needs to be escaped again in the C++. So matching a single \ in the
1000 // input requires 4 \es in the C++.
1001 if (opts::pretty::ExcludeCompilerGenerated) {
1002 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
1003 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
1005 if (opts::pretty::ExcludeSystemLibraries) {
1006 opts::pretty::ExcludeCompilands.push_back(
1007 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
1008 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
1009 opts::pretty::ExcludeCompilands.push_back(
1010 "d:\\\\th.obj.x86fre\\\\minkernel");
1012 std::for_each(opts::pretty::InputFilenames.begin(),
1013 opts::pretty::InputFilenames.end(), dumpPretty);
1014 } else if (opts::RawSubcommand) {
1015 std::for_each(opts::raw::InputFilenames.begin(),
1016 opts::raw::InputFilenames.end(), dumpRaw);
1017 } else if (opts::DiffSubcommand) {
1018 if (opts::diff::InputFilenames.size() != 2) {
1019 errs() << "diff subcommand expects exactly 2 arguments.\n";
1022 diff(opts::diff::InputFilenames[0], opts::diff::InputFilenames[1]);
1023 } else if (opts::MergeSubcommand) {
1024 if (opts::merge::InputFilenames.size() < 2) {
1025 errs() << "merge subcommand requires at least 2 input files.\n";