blob: adc739b034ac2fee2cb84b8940dba77b40d60a3a [file] [log] [blame]
Zachary Turnerfcb14ad2015-01-27 20:46:21 +00001//===- llvm-pdbdump.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//
Zachary Turner6ac232c2017-03-13 23:28:25 +000010// Dumps debug information present in PDB files.
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000011//
12//===----------------------------------------------------------------------===//
13
Zachary Turner9a818ad2015-02-22 22:03:38 +000014#include "llvm-pdbdump.h"
Zachary Turnerd50c0132017-02-01 18:30:22 +000015
16#include "Analyze.h"
Zachary Turner6ac232c2017-03-13 23:28:25 +000017#include "Diff.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000018#include "LLVMOutputStyle.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000019#include "LinePrinter.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000020#include "OutputStyle.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000021#include "PrettyCompilandDumper.h"
22#include "PrettyExternalSymbolDumper.h"
23#include "PrettyFunctionDumper.h"
24#include "PrettyTypeDumper.h"
25#include "PrettyVariableDumper.h"
Zachary Turner7120a472016-06-06 20:37:05 +000026#include "YAMLOutputStyle.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000027
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000028#include "llvm/ADT/ArrayRef.h"
David Majnemer6e081262015-10-15 01:27:19 +000029#include "llvm/ADT/BitVector.h"
30#include "llvm/ADT/DenseMap.h"
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000031#include "llvm/ADT/STLExtras.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000032#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000033#include "llvm/Config/config.h"
Zachary Turneredef1452017-05-02 16:56:09 +000034#include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h"
35#include "llvm/DebugInfo/CodeView/ModuleDebugInlineeLinesFragment.h"
36#include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000037#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000038#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000039#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000040#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000041#include "llvm/DebugInfo/PDB/IPDBSession.h"
Zachary Turner67c56012017-04-27 16:11:19 +000042#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000043#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
44#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
45#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
46#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
47#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
48#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
49#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
Zachary Turnere204a6c2017-05-02 18:00:13 +000050#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000051#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
52#include "llvm/DebugInfo/PDB/Native/RawError.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000053#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
54#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000055#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000056#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000057#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000058#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000059#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
60#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000061#include "llvm/Support/BinaryByteStream.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000062#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000063#include "llvm/Support/CommandLine.h"
64#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000065#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000066#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000067#include "llvm/Support/Format.h"
68#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000069#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000070#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000071#include "llvm/Support/Process.h"
Zachary Turner72c5b642016-09-09 18:17:52 +000072#include "llvm/Support/Regex.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000073#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000074#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000075#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000076
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000077using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000078using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000079using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000080using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000081
82namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000083
Zachary Turnera30bd1a2016-06-30 17:42:48 +000084cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
85cl::SubCommand
86 PrettySubcommand("pretty",
87 "Dump semantic information about types and symbols");
Zachary Turner6ac232c2017-03-13 23:28:25 +000088
89cl::SubCommand DiffSubcommand("diff", "Diff the contents of 2 PDB files");
90
Zachary Turnerab58ae82016-06-30 17:43:00 +000091cl::SubCommand
92 YamlToPdbSubcommand("yaml2pdb",
93 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000094cl::SubCommand
95 PdbToYamlSubcommand("pdb2yaml",
96 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000097
Zachary Turnerd50c0132017-02-01 18:30:22 +000098cl::SubCommand
99 AnalyzeSubcommand("analyze",
100 "Analyze various aspects of a PDB's structure");
101
Zachary Turner7797c722015-03-02 04:39:56 +0000102cl::OptionCategory TypeCategory("Symbol Type Options");
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000103cl::OptionCategory FilterCategory("Filtering and Sorting Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +0000104cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000105
106namespace pretty {
107cl::list<std::string> InputFilenames(cl::Positional,
108 cl::desc("<input PDB files>"),
109 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000110
111cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000112 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000113cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000114 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000115cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000116 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000117cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000118 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner63230a4e2017-04-06 23:43:12 +0000119cl::opt<bool>
120 Types("types",
121 cl::desc("Display all types (implies -classes, -enums, -typedefs)"),
122 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
123cl::opt<bool> Classes("classes", cl::desc("Display class types"),
124 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
125cl::opt<bool> Enums("enums", cl::desc("Display enum types"),
126 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
127cl::opt<bool> Typedefs("typedefs", cl::desc("Display typedef types"),
128 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner4dc4f012017-04-13 21:11:00 +0000129cl::opt<ClassSortMode> ClassOrder(
130 "class-order", cl::desc("Class sort order"), cl::init(ClassSortMode::None),
Zachary Turner16901642017-04-24 17:47:24 +0000131 cl::values(
132 clEnumValN(ClassSortMode::None, "none",
133 "Undefined / no particular sort order"),
134 clEnumValN(ClassSortMode::Name, "name", "Sort classes by name"),
135 clEnumValN(ClassSortMode::Size, "size", "Sort classes by size"),
136 clEnumValN(ClassSortMode::Padding, "padding",
137 "Sort classes by amount of padding"),
138 clEnumValN(ClassSortMode::PaddingPct, "padding-pct",
Zachary Turnerda307b62017-04-25 20:22:29 +0000139 "Sort classes by percentage of space consumed by padding"),
140 clEnumValN(ClassSortMode::PaddingImmediate, "padding-imm",
141 "Sort classes by amount of immediate padding"),
142 clEnumValN(ClassSortMode::PaddingPctImmediate, "padding-pct-imm",
143 "Sort classes by percentage of space consumed by immediate "
144 "padding")),
Zachary Turner4dc4f012017-04-13 21:11:00 +0000145 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
146
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000147cl::opt<ClassDefinitionFormat> ClassFormat(
148 "class-definitions", cl::desc("Class definition format"),
Zachary Turnerda949c12017-04-24 17:47:52 +0000149 cl::init(ClassDefinitionFormat::All),
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000150 cl::values(
Zachary Turnerda949c12017-04-24 17:47:52 +0000151 clEnumValN(ClassDefinitionFormat::All, "all",
Zachary Turner9e7dda32017-04-12 23:18:51 +0000152 "Display all class members including data, constants, "
Zachary Turner4dc4f012017-04-13 21:11:00 +0000153 "typedefs, functions, etc"),
Zachary Turnerda949c12017-04-24 17:47:52 +0000154 clEnumValN(ClassDefinitionFormat::Layout, "layout",
Zachary Turnerc883a8c2017-04-12 23:18:21 +0000155 "Only display members that contribute to class size."),
156 clEnumValN(ClassDefinitionFormat::None, "none",
157 "Don't display class definitions")),
158 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerda949c12017-04-24 17:47:52 +0000159cl::opt<uint32_t> ClassRecursionDepth(
160 "class-recurse-depth", cl::desc("Class recursion depth (0=no limit)"),
161 cl::init(0), cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner63230a4e2017-04-06 23:43:12 +0000162
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000163cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
164 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000165cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000166 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000167 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000168
Zachary Turnere5cb2692015-05-01 20:24:26 +0000169cl::opt<uint64_t> LoadAddress(
170 "load-address",
171 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000172 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
Adrian McCarthy65d26882017-03-15 20:17:58 +0000173cl::opt<bool> Native("native", cl::desc("Use native PDB reader instead of DIA"),
Adrian McCarthy1aa207d2017-03-23 15:28:15 +0000174 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
175cl::opt<cl::boolOrDefault>
176 ColorOutput("color-output",
177 cl::desc("Override use of color (default = isatty)"),
178 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000179cl::list<std::string> ExcludeTypes(
180 "exclude-types", cl::desc("Exclude types by regular expression"),
181 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
182cl::list<std::string> ExcludeSymbols(
183 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
184 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
185cl::list<std::string> ExcludeCompilands(
186 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
187 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000188
189cl::list<std::string> IncludeTypes(
190 "include-types",
191 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000192 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000193cl::list<std::string> IncludeSymbols(
194 "include-symbols",
195 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000196 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000197cl::list<std::string> IncludeCompilands(
198 "include-compilands",
199 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000200 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dc4f012017-04-13 21:11:00 +0000201cl::opt<uint32_t> SizeThreshold(
202 "min-type-size", cl::desc("Displays only those types which are greater "
203 "than or equal to the specified size."),
204 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
205cl::opt<uint32_t> PaddingThreshold(
206 "min-class-padding", cl::desc("Displays only those classes which have at "
207 "least the specified amount of padding."),
208 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turnerda307b62017-04-25 20:22:29 +0000209cl::opt<uint32_t> ImmediatePaddingThreshold(
210 "min-class-padding-imm",
211 cl::desc("Displays only those classes which have at least the specified "
212 "amount of immediate padding, ignoring padding internal to bases "
213 "and aggregates."),
214 cl::init(0), cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000215
Zachary Turner7797c722015-03-02 04:39:56 +0000216cl::opt<bool> ExcludeCompilerGenerated(
217 "no-compiler-generated",
218 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000219 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000220cl::opt<bool>
221 ExcludeSystemLibraries("no-system-libs",
222 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000223 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner10169b62017-04-06 23:43:39 +0000224
Zachary Turner65323652015-03-04 06:09:53 +0000225cl::opt<bool> NoEnumDefs("no-enum-definitions",
226 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000227 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
228}
229
Zachary Turner6ac232c2017-03-13 23:28:25 +0000230namespace diff {
231cl::opt<bool> Pedantic("pedantic",
232 cl::desc("Finds all differences (even structural ones "
233 "that produce otherwise identical PDBs)"),
234 cl::sub(DiffSubcommand));
235
236cl::list<std::string> InputFilenames(cl::Positional,
237 cl::desc("<first> <second>"),
238 cl::OneOrMore, cl::sub(DiffSubcommand));
239}
240
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000241namespace raw {
242
243cl::OptionCategory MsfOptions("MSF Container Options");
244cl::OptionCategory TypeOptions("Type Record Options");
245cl::OptionCategory FileOptions("Module & File Options");
246cl::OptionCategory SymbolOptions("Symbol Options");
247cl::OptionCategory MiscOptions("Miscellaneous Options");
248
249// MSF OPTIONS
250cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
251 cl::cat(MsfOptions), cl::sub(RawSubcommand));
252cl::opt<bool> DumpStreamBlocks("stream-blocks",
253 cl::desc("dump PDB stream blocks"),
254 cl::cat(MsfOptions), cl::sub(RawSubcommand));
255cl::opt<bool> DumpStreamSummary("stream-summary",
256 cl::desc("dump summary of the PDB streams"),
257 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000258cl::opt<bool> DumpPageStats(
259 "page-stats",
260 cl::desc("dump allocation stats of the pages in the MSF file"),
261 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turner72c5b642016-09-09 18:17:52 +0000262cl::opt<std::string>
263 DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
264 cl::desc("Dump binary data from specified range."),
265 cl::cat(MsfOptions), cl::sub(RawSubcommand));
266llvm::Optional<BlockRange> DumpBlockRange;
267
Zachary Turner7159ab92017-04-28 00:43:38 +0000268cl::list<std::string>
Zachary Turner72c5b642016-09-09 18:17:52 +0000269 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
Zachary Turner7159ab92017-04-28 00:43:38 +0000270 cl::desc("Dump binary data from specified streams. Format "
271 "is SN[:Start][@Size]"),
Zachary Turner72c5b642016-09-09 18:17:52 +0000272 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000273
274// TYPE OPTIONS
275cl::opt<bool>
Zachary Turner44a643c2017-01-12 22:28:15 +0000276 CompactRecords("compact-records",
277 cl::desc("Dump type and symbol records with less detail"),
278 cl::cat(TypeOptions), cl::sub(RawSubcommand));
279
280cl::opt<bool>
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000281 DumpTpiRecords("tpi-records",
282 cl::desc("dump CodeView type records from TPI stream"),
283 cl::cat(TypeOptions), cl::sub(RawSubcommand));
284cl::opt<bool> DumpTpiRecordBytes(
285 "tpi-record-bytes",
286 cl::desc("dump CodeView type record raw bytes from TPI stream"),
287 cl::cat(TypeOptions), cl::sub(RawSubcommand));
288cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
289 cl::cat(TypeOptions), cl::sub(RawSubcommand));
290cl::opt<bool>
291 DumpIpiRecords("ipi-records",
292 cl::desc("dump CodeView type records from IPI stream"),
293 cl::cat(TypeOptions), cl::sub(RawSubcommand));
294cl::opt<bool> DumpIpiRecordBytes(
295 "ipi-record-bytes",
296 cl::desc("dump CodeView type record raw bytes from IPI stream"),
297 cl::cat(TypeOptions), cl::sub(RawSubcommand));
298
299// MODULE & FILE OPTIONS
300cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
301 cl::cat(FileOptions), cl::sub(RawSubcommand));
302cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
303 cl::cat(FileOptions), cl::sub(RawSubcommand));
304cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000305 cl::desc("dump file and line information"),
306 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000307
308// SYMBOL OPTIONS
Bob Haarman653baa22016-10-21 19:43:19 +0000309cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
310 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000311cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
312 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
313cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
314 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
315cl::opt<bool>
316 DumpSymRecordBytes("sym-record-bytes",
317 cl::desc("dump CodeView symbol record raw bytes"),
318 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
319
320// MISCELLANEOUS OPTIONS
Zachary Turner760ad4d2017-01-20 22:42:09 +0000321cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
322 cl::cat(MiscOptions), cl::sub(RawSubcommand));
323
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000324cl::opt<bool> DumpSectionContribs("section-contribs",
325 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000326 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000327cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
328 cl::cat(MiscOptions), cl::sub(RawSubcommand));
329cl::opt<bool> DumpSectionHeaders("section-headers",
330 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000331 cl::cat(MiscOptions), cl::sub(RawSubcommand));
332cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
333 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000334
Zachary Turnerab58ae82016-06-30 17:43:00 +0000335cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
336 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000337
338cl::list<std::string> InputFilenames(cl::Positional,
339 cl::desc("<input PDB files>"),
340 cl::OneOrMore, cl::sub(RawSubcommand));
341}
342
343namespace yaml2pdb {
344cl::opt<std::string>
345 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
346 cl::sub(YamlToPdbSubcommand));
347
348cl::list<std::string> InputFilename(cl::Positional,
349 cl::desc("<input YAML file>"), cl::Required,
350 cl::sub(YamlToPdbSubcommand));
351}
352
353namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000354cl::opt<bool>
355 NoFileHeaders("no-file-headers",
356 cl::desc("Do not dump MSF file headers (you will not be able "
357 "to generate a fresh PDB from the resulting YAML)"),
358 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000359cl::opt<bool> Minimal("minimal",
360 cl::desc("Don't write fields with default values"),
361 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerf6b93822016-07-11 21:45:09 +0000362
Zachary Turnerab58ae82016-06-30 17:43:00 +0000363cl::opt<bool> StreamMetadata(
364 "stream-metadata",
365 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000366 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000367cl::opt<bool> StreamDirectory(
368 "stream-directory",
369 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000370 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000371cl::opt<bool> PdbStream("pdb-stream",
372 cl::desc("Dump the PDB Stream (Stream 1)"),
373 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turner760ad4d2017-01-20 22:42:09 +0000374
375cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
376 cl::sub(PdbToYamlSubcommand), cl::init(false));
377
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000378cl::opt<bool> DbiStream("dbi-stream",
379 cl::desc("Dump the DBI Stream (Stream 2)"),
380 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000381cl::opt<bool>
382 DbiModuleInfo("dbi-module-info",
383 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
384 cl::sub(PdbToYamlSubcommand), cl::init(false));
385
Zachary Turner3b147642016-10-08 01:12:01 +0000386cl::opt<bool> DbiModuleSyms(
387 "dbi-module-syms",
388 cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
389 cl::sub(PdbToYamlSubcommand), cl::init(false));
390
Zachary Turnerd218c262016-07-22 15:46:37 +0000391cl::opt<bool> DbiModuleSourceFileInfo(
392 "dbi-module-source-info",
393 cl::desc(
Zachary Turneree3b9c22017-04-25 20:22:02 +0000394 "Dump DBI Module Source File Information (implies -dbi-module-info)"),
Zachary Turnerd218c262016-07-22 15:46:37 +0000395 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000396
Zachary Turneree3b9c22017-04-25 20:22:02 +0000397cl::opt<bool>
398 DbiModuleSourceLineInfo("dbi-module-lines",
399 cl::desc("Dump DBI Module Source Line Information "
400 "(implies -dbi-module-source-info)"),
401 cl::sub(PdbToYamlSubcommand), cl::init(false));
402
Zachary Turnerac5763e2016-08-18 16:49:29 +0000403cl::opt<bool> TpiStream("tpi-stream",
404 cl::desc("Dump the TPI Stream (Stream 3)"),
405 cl::sub(PdbToYamlSubcommand), cl::init(false));
406
Zachary Turnerde9ba152016-09-15 18:22:31 +0000407cl::opt<bool> IpiStream("ipi-stream",
408 cl::desc("Dump the IPI Stream (Stream 5)"),
409 cl::sub(PdbToYamlSubcommand), cl::init(false));
410
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000411cl::list<std::string> InputFilename(cl::Positional,
412 cl::desc("<input PDB file>"), cl::Required,
413 cl::sub(PdbToYamlSubcommand));
414}
Zachary Turnerd50c0132017-02-01 18:30:22 +0000415
416namespace analyze {
417cl::opt<bool> StringTable("hash-collisions", cl::desc("Find hash collisions"),
418 cl::sub(AnalyzeSubcommand), cl::init(false));
419cl::list<std::string> InputFilename(cl::Positional,
420 cl::desc("<input PDB file>"), cl::Required,
421 cl::sub(AnalyzeSubcommand));
422}
Zachary Turner49693b42015-01-28 01:22:33 +0000423}
424
David Majnemerc165c882016-05-28 18:25:15 +0000425static ExitOnError ExitOnErr;
426
Zachary Turneredef1452017-05-02 16:56:09 +0000427static uint32_t
428getFileChecksumOffset(StringRef FileName,
429 ModuleDebugFileChecksumFragment &Checksums,
Zachary Turnere204a6c2017-05-02 18:00:13 +0000430 PDBStringTableBuilder &Strings) {
Zachary Turneredef1452017-05-02 16:56:09 +0000431 // The offset in the line info record is the offset of the checksum
432 // entry for the corresponding file. That entry then contains an
433 // offset into the global string table of the file name. So to
434 // compute the proper offset to write into the line info record, we
435 // must first get its offset in the global string table, then ask the
436 // checksum builder to find the offset in its serialized buffer that
437 // it mapped that filename string table offset to.
438 uint32_t StringOffset = Strings.insert(FileName);
439 return Checksums.mapChecksumOffset(StringOffset);
440}
441
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000442static void yamlToPdb(StringRef Path) {
Zachary Turnere109dc62016-07-22 19:56:26 +0000443 BumpPtrAllocator Allocator;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000444 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
445 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
446 /*RequiresNullTerminator=*/false);
447
448 if (ErrorOrBuffer.getError()) {
449 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
450 }
451
452 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
453
454 llvm::yaml::Input In(Buffer->getBuffer());
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000455 pdb::yaml::PdbObject YamlObj(Allocator);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000456 In >> YamlObj;
457
Zachary Turnere109dc62016-07-22 19:56:26 +0000458 PDBFileBuilder Builder(Allocator);
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000459
Zachary Turnerea4e6072017-03-15 22:18:53 +0000460 uint32_t BlockSize = 4096;
461 if (YamlObj.Headers.hasValue())
462 BlockSize = YamlObj.Headers->SuperBlock.BlockSize;
463 ExitOnErr(Builder.initialize(BlockSize));
Zachary Turner620961d2016-09-14 23:00:02 +0000464 // Add each of the reserved streams. We ignore stream metadata in the
465 // yaml, because we will reconstruct our own view of the streams. For
466 // example, the YAML may say that there were 20 streams in the original
467 // PDB, but maybe we only dump a subset of those 20 streams, so we will
468 // have fewer, and the ones we do have may end up with different indices
469 // than the ones in the original PDB. So we just start with a clean slate.
470 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
471 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000472
Zachary Turner760ad4d2017-01-20 22:42:09 +0000473 if (YamlObj.StringTable.hasValue()) {
474 auto &Strings = Builder.getStringTableBuilder();
475 for (auto S : *YamlObj.StringTable)
476 Strings.insert(S);
477 }
478
Zachary Turnerea4e6072017-03-15 22:18:53 +0000479 pdb::yaml::PdbInfoStream DefaultInfoStream;
480 pdb::yaml::PdbDbiStream DefaultDbiStream;
481 pdb::yaml::PdbTpiStream DefaultTpiStream;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000482
Zachary Turnerea4e6072017-03-15 22:18:53 +0000483 const auto &Info = YamlObj.PdbStream.getValueOr(DefaultInfoStream);
484
485 auto &InfoBuilder = Builder.getInfoBuilder();
486 InfoBuilder.setAge(Info.Age);
487 InfoBuilder.setGuid(Info.Guid);
488 InfoBuilder.setSignature(Info.Signature);
489 InfoBuilder.setVersion(Info.Version);
Zachary Turner05d5e612017-03-16 20:19:11 +0000490 for (auto F : Info.Features)
491 InfoBuilder.addFeature(F);
Zachary Turnerea4e6072017-03-15 22:18:53 +0000492
493 const auto &Dbi = YamlObj.DbiStream.getValueOr(DefaultDbiStream);
494 auto &DbiBuilder = Builder.getDbiBuilder();
495 DbiBuilder.setAge(Dbi.Age);
496 DbiBuilder.setBuildNumber(Dbi.BuildNumber);
497 DbiBuilder.setFlags(Dbi.Flags);
498 DbiBuilder.setMachineType(Dbi.MachineType);
499 DbiBuilder.setPdbDllRbld(Dbi.PdbDllRbld);
500 DbiBuilder.setPdbDllVersion(Dbi.PdbDllVersion);
501 DbiBuilder.setVersionHeader(Dbi.VerHeader);
502 for (const auto &MI : Dbi.ModInfos) {
Zachary Turner05d5e612017-03-16 20:19:11 +0000503 auto &ModiBuilder = ExitOnErr(DbiBuilder.addModuleInfo(MI.Mod));
Zachary Turnerea4e6072017-03-15 22:18:53 +0000504
505 for (auto S : MI.SourceFiles)
506 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
507 if (MI.Modi.hasValue()) {
508 const auto &ModiStream = *MI.Modi;
509 ModiBuilder.setObjFileName(MI.Obj);
510 for (auto Symbol : ModiStream.Symbols)
511 ModiBuilder.addSymbol(Symbol.Record);
Zachary Turnerd218c262016-07-22 15:46:37 +0000512 }
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000513 if (MI.FileLineInfo.hasValue()) {
514 const auto &FLI = *MI.FileLineInfo;
515
516 // File Checksums must be emitted before line information, because line
517 // info records use offsets into the checksum buffer to reference a file's
518 // source file name.
519 auto Checksums = llvm::make_unique<ModuleDebugFileChecksumFragment>();
520 auto &ChecksumRef = *Checksums;
521 if (!FLI.FileChecksums.empty()) {
522 auto &Strings = Builder.getStringTableBuilder();
523 for (auto &FC : FLI.FileChecksums) {
Zachary Turneredef1452017-05-02 16:56:09 +0000524 uint32_t STOffset = Strings.insert(FC.FileName);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000525 Checksums->addChecksum(STOffset, FC.Kind, FC.ChecksumBytes.Bytes);
526 }
527 }
528 ModiBuilder.setC13FileChecksums(std::move(Checksums));
529
Zachary Turneredef1452017-05-02 16:56:09 +0000530 // FIXME: StringTable / StringTableBuilder should really be in
531 // DebugInfoCodeView. This would allow us to construct the
532 // ModuleDebugLineFragment with a reference to the string table,
533 // and we could just pass strings around rather than having to
534 // remember how to calculate the right offset.
535 auto &Strings = Builder.getStringTableBuilder();
536
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000537 for (const auto &Fragment : FLI.LineFragments) {
538 auto Lines = llvm::make_unique<ModuleDebugLineFragment>();
539 Lines->setCodeSize(Fragment.CodeSize);
540 Lines->setRelocationAddress(Fragment.RelocSegment,
541 Fragment.RelocOffset);
542 Lines->setFlags(Fragment.Flags);
543 for (const auto &LC : Fragment.Blocks) {
Zachary Turneredef1452017-05-02 16:56:09 +0000544 uint32_t ChecksumOffset =
545 getFileChecksumOffset(LC.FileName, ChecksumRef, Strings);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000546
547 Lines->createBlock(ChecksumOffset);
548 if (Lines->hasColumnInfo()) {
549 for (const auto &Item : zip(LC.Lines, LC.Columns)) {
550 auto &L = std::get<0>(Item);
551 auto &C = std::get<1>(Item);
552 uint32_t LE = L.LineStart + L.EndDelta;
553 Lines->addLineAndColumnInfo(
554 L.Offset, LineInfo(L.LineStart, LE, L.IsStatement),
555 C.StartColumn, C.EndColumn);
556 }
557 } else {
558 for (const auto &L : LC.Lines) {
559 uint32_t LE = L.LineStart + L.EndDelta;
560 Lines->addLineInfo(L.Offset,
561 LineInfo(L.LineStart, LE, L.IsStatement));
562 }
563 }
564 }
Zachary Turneredef1452017-05-02 16:56:09 +0000565 ModiBuilder.addC13Fragment(std::move(Lines));
566 }
567
568 for (const auto &Inlinee : FLI.Inlinees) {
569 auto Inlinees = llvm::make_unique<ModuleDebugInlineeLineFragment>(
570 Inlinee.HasExtraFiles);
571 for (const auto &Site : Inlinee.Sites) {
572 uint32_t FileOff =
573 getFileChecksumOffset(Site.FileName, ChecksumRef, Strings);
574
575 Inlinees->addInlineSite(Site.Inlinee, FileOff, Site.SourceLineNum);
576 if (!Inlinee.HasExtraFiles)
577 continue;
578
579 for (auto EF : Site.ExtraFiles) {
580 FileOff = getFileChecksumOffset(EF, ChecksumRef, Strings);
581 Inlinees->addExtraFile(FileOff);
582 }
583 }
584 ModiBuilder.addC13Fragment(std::move(Inlinees));
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000585 }
586 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000587 }
588
Zachary Turnerea4e6072017-03-15 22:18:53 +0000589 auto &TpiBuilder = Builder.getTpiBuilder();
590 const auto &Tpi = YamlObj.TpiStream.getValueOr(DefaultTpiStream);
591 TpiBuilder.setVersionHeader(Tpi.Version);
592 for (const auto &R : Tpi.Records)
Reid Kleckner13fc4112017-04-04 00:56:34 +0000593 TpiBuilder.addTypeRecord(R.Record.data(), R.Record.Hash);
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000594
Zachary Turnerea4e6072017-03-15 22:18:53 +0000595 const auto &Ipi = YamlObj.IpiStream.getValueOr(DefaultTpiStream);
596 auto &IpiBuilder = Builder.getIpiBuilder();
597 IpiBuilder.setVersionHeader(Ipi.Version);
598 for (const auto &R : Ipi.Records)
Reid Kleckner13fc4112017-04-04 00:56:34 +0000599 TpiBuilder.addTypeRecord(R.Record.data(), R.Record.Hash);
Zachary Turnerde9ba152016-09-15 18:22:31 +0000600
Rui Ueyamafc22cef2016-09-30 20:34:44 +0000601 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000602}
603
Zachary Turner6ac232c2017-03-13 23:28:25 +0000604static PDBFile &loadPDB(StringRef Path, std::unique_ptr<IPDBSession> &Session) {
Adrian McCarthy8f713192017-01-27 00:01:55 +0000605 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000606
Zachary Turner6ac232c2017-03-13 23:28:25 +0000607 NativeSession *NS = static_cast<NativeSession *>(Session.get());
608 return NS->getPDBFile();
609}
610
611static void pdb2Yaml(StringRef Path) {
612 std::unique_ptr<IPDBSession> Session;
613 auto &File = loadPDB(Path, Session);
614
Zachary Turner8848a7a2016-07-06 18:05:57 +0000615 auto O = llvm::make_unique<YAMLOutputStyle>(File);
616 O = llvm::make_unique<YAMLOutputStyle>(File);
617
618 ExitOnErr(O->dump());
619}
620
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000621static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000622 std::unique_ptr<IPDBSession> Session;
Zachary Turner6ac232c2017-03-13 23:28:25 +0000623 auto &File = loadPDB(Path, Session);
David Majnemer1573b242016-04-28 23:47:27 +0000624
Zachary Turner8848a7a2016-07-06 18:05:57 +0000625 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000626
627 ExitOnErr(O->dump());
628}
629
Zachary Turnerd50c0132017-02-01 18:30:22 +0000630static void dumpAnalysis(StringRef Path) {
631 std::unique_ptr<IPDBSession> Session;
Zachary Turner6ac232c2017-03-13 23:28:25 +0000632 auto &File = loadPDB(Path, Session);
Zachary Turnerd50c0132017-02-01 18:30:22 +0000633 auto O = llvm::make_unique<AnalysisStyle>(File);
634
635 ExitOnErr(O->dump());
636}
637
Zachary Turner6ac232c2017-03-13 23:28:25 +0000638static void diff(StringRef Path1, StringRef Path2) {
639 std::unique_ptr<IPDBSession> Session1;
640 std::unique_ptr<IPDBSession> Session2;
641
642 auto &File1 = loadPDB(Path1, Session1);
643 auto &File2 = loadPDB(Path2, Session2);
644
645 auto O = llvm::make_unique<DiffStyle>(File1, File2);
646
647 ExitOnErr(O->dump());
648}
649
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000650static void dumpPretty(StringRef Path) {
651 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000652
Adrian McCarthy65d26882017-03-15 20:17:58 +0000653 const auto ReaderType =
654 opts::pretty::Native ? PDB_ReaderType::Native : PDB_ReaderType::DIA;
655 ExitOnErr(loadDataForPDB(ReaderType, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000656
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000657 if (opts::pretty::LoadAddress)
658 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000659
Adrian McCarthy1aa207d2017-03-23 15:28:15 +0000660 auto &Stream = outs();
661 const bool UseColor = opts::pretty::ColorOutput == cl::BOU_UNSET
662 ? Stream.has_colors()
663 : opts::pretty::ColorOutput == cl::BOU_TRUE;
664 LinePrinter Printer(2, UseColor, Stream);
Zachary Turner2d11c202015-02-27 09:15:59 +0000665
Zachary Turnera5549172015-02-10 22:43:25 +0000666 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000667 std::string FileName(GlobalScope->getSymbolsFileName());
668
Zachary Turner2d11c202015-02-27 09:15:59 +0000669 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
670 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
671 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000672 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000673
Zachary Turner2d11c202015-02-27 09:15:59 +0000674 Printer.NewLine();
675 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000676 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000677 Printer << ": " << FileSize << " bytes";
678 } else {
679 Printer << ": (Unable to obtain file size)";
680 }
681
682 Printer.NewLine();
683 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
684 Printer << ": " << GlobalScope->getGuid();
685
686 Printer.NewLine();
687 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
688 Printer << ": " << GlobalScope->getAge();
689
690 Printer.NewLine();
691 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
692 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000693 if (GlobalScope->hasCTypes())
694 outs() << "HasCTypes ";
695 if (GlobalScope->hasPrivateSymbols())
696 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000697 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000698
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000699 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000700 Printer.NewLine();
701 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
702 << "---COMPILANDS---";
703 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000704 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000705 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000706 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000707 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000708 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000709 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000710 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000711 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000712 }
713
Zachary Turner63230a4e2017-04-06 23:43:12 +0000714 if (opts::pretty::Classes || opts::pretty::Enums || opts::pretty::Typedefs) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000715 Printer.NewLine();
716 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
717 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000718 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000719 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000720 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000721 }
722
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000723 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000724 Printer.NewLine();
725 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
726 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000727 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000728 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000729 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000730 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000731 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000732 }
733
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000734 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000735 Printer.NewLine();
736 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
737 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000738 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000739 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000740 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000741 while (auto Function = Functions->getNext()) {
742 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000743 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000744 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000745 }
746 {
747 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000748 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000749 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000750 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000751 }
752 {
753 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000754 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000755 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000756 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000757 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000758 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000759 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000760 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000761 Printer.NewLine();
762 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
763 Printer.Indent();
764 ExternalSymbolDumper Dumper(Printer);
765 Dumper.start(*GlobalScope);
766 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000767 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000768 Printer.NewLine();
769 }
Zachary Turnera5549172015-02-10 22:43:25 +0000770 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000771}
772
773int main(int argc_, const char *argv_[]) {
774 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000775 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000776 PrettyStackTraceProgram X(argc_, argv_);
777
David Majnemerc165c882016-05-28 18:25:15 +0000778 ExitOnErr.setBanner("llvm-pdbdump: ");
779
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000780 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000781 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000782 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
783 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000784
785 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
786
787 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner72c5b642016-09-09 18:17:52 +0000788 if (!opts::raw::DumpBlockRangeOpt.empty()) {
789 llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
790 llvm::SmallVector<llvm::StringRef, 2> Matches;
791 if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
792 errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
793 << "' invalid format.\n";
794 errs().flush();
795 exit(1);
796 }
797 opts::raw::DumpBlockRange.emplace();
798 Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
799 if (!Matches[3].empty()) {
800 opts::raw::DumpBlockRange->Max.emplace();
801 Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
802 }
803 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000804
Zachary Turner44a643c2017-01-12 22:28:15 +0000805 if (opts::RawSubcommand) {
806 if (opts::raw::RawAll) {
807 opts::raw::DumpHeaders = true;
808 opts::raw::DumpModules = true;
809 opts::raw::DumpModuleFiles = true;
810 opts::raw::DumpModuleSyms = true;
811 opts::raw::DumpGlobals = true;
812 opts::raw::DumpPublics = true;
813 opts::raw::DumpSectionHeaders = true;
814 opts::raw::DumpStreamSummary = true;
815 opts::raw::DumpPageStats = true;
816 opts::raw::DumpStreamBlocks = true;
817 opts::raw::DumpTpiRecords = true;
818 opts::raw::DumpTpiHash = true;
819 opts::raw::DumpIpiRecords = true;
820 opts::raw::DumpSectionMap = true;
821 opts::raw::DumpSectionContribs = true;
822 opts::raw::DumpLineInfo = true;
823 opts::raw::DumpFpo = true;
Zachary Turner760ad4d2017-01-20 22:42:09 +0000824 opts::raw::DumpStringTable = true;
Zachary Turner44a643c2017-01-12 22:28:15 +0000825 }
826
827 if (opts::raw::CompactRecords &&
828 (opts::raw::DumpTpiRecordBytes || opts::raw::DumpIpiRecordBytes)) {
829 errs() << "-compact-records is incompatible with -tpi-record-bytes and "
830 "-ipi-record-bytes.\n";
831 exit(1);
832 }
Zachary Turner7797c722015-03-02 04:39:56 +0000833 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000834
Zachary Turner93839cb2016-06-02 05:07:49 +0000835 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000836
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000837 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000838 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000839 } else if (opts::YamlToPdbSubcommand) {
840 yamlToPdb(opts::yaml2pdb::InputFilename.front());
Zachary Turnerd50c0132017-02-01 18:30:22 +0000841 } else if (opts::AnalyzeSubcommand) {
842 dumpAnalysis(opts::analyze::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000843 } else if (opts::PrettySubcommand) {
844 if (opts::pretty::Lines)
845 opts::pretty::Compilands = true;
846
847 if (opts::pretty::All) {
848 opts::pretty::Compilands = true;
849 opts::pretty::Symbols = true;
850 opts::pretty::Globals = true;
851 opts::pretty::Types = true;
852 opts::pretty::Externals = true;
853 opts::pretty::Lines = true;
854 }
855
Zachary Turner63230a4e2017-04-06 23:43:12 +0000856 if (opts::pretty::Types) {
857 opts::pretty::Classes = true;
858 opts::pretty::Typedefs = true;
859 opts::pretty::Enums = true;
860 }
861
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000862 // When adding filters for excluded compilands and types, we need to
Zachary Turner72c5b642016-09-09 18:17:52 +0000863 // remember that these are regexes. So special characters such as * and \
864 // need to be escaped in the regex. In the case of a literal \, this means
865 // it needs to be escaped again in the C++. So matching a single \ in the
866 // input requires 4 \es in the C++.
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000867 if (opts::pretty::ExcludeCompilerGenerated) {
868 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
869 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
870 }
871 if (opts::pretty::ExcludeSystemLibraries) {
872 opts::pretty::ExcludeCompilands.push_back(
873 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
874 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
875 opts::pretty::ExcludeCompilands.push_back(
876 "d:\\\\th.obj.x86fre\\\\minkernel");
877 }
878 std::for_each(opts::pretty::InputFilenames.begin(),
879 opts::pretty::InputFilenames.end(), dumpPretty);
880 } else if (opts::RawSubcommand) {
881 std::for_each(opts::raw::InputFilenames.begin(),
882 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner6ac232c2017-03-13 23:28:25 +0000883 } else if (opts::DiffSubcommand) {
884 if (opts::diff::InputFilenames.size() != 2) {
885 errs() << "diff subcommand expects exactly 2 arguments.\n";
886 exit(1);
887 }
888 diff(opts::diff::InputFilenames[0], opts::diff::InputFilenames[1]);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000889 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000890
Zachary Turner819e77d2016-05-06 20:51:57 +0000891 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000892 return 0;
893}