blob: 07ac65e2675827218a5d57e95e0a035ecc6411b5 [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//
10// Dumps debug information present in PDB files. This utility makes use of
11// the Microsoft Windows SDK, so will not compile or run on non-Windows
12// platforms.
13//
14//===----------------------------------------------------------------------===//
15
Zachary Turner9a818ad2015-02-22 22:03:38 +000016#include "llvm-pdbdump.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000017#include "LLVMOutputStyle.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000018#include "LinePrinter.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000019#include "OutputStyle.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000020#include "PrettyCompilandDumper.h"
21#include "PrettyExternalSymbolDumper.h"
22#include "PrettyFunctionDumper.h"
23#include "PrettyTypeDumper.h"
24#include "PrettyVariableDumper.h"
Zachary Turner7120a472016-06-06 20:37:05 +000025#include "YAMLOutputStyle.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000026
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000027#include "llvm/ADT/ArrayRef.h"
David Majnemer6e081262015-10-15 01:27:19 +000028#include "llvm/ADT/BitVector.h"
29#include "llvm/ADT/DenseMap.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000030#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000031#include "llvm/Config/config.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000032#include "llvm/DebugInfo/MSF/ByteStream.h"
33#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000034#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000035#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000036#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000037#include "llvm/DebugInfo/PDB/IPDBSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000038#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
39#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
40#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
41#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
42#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
43#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
44#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
45#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
46#include "llvm/DebugInfo/PDB/Native/RawError.h"
47#include "llvm/DebugInfo/PDB/Native/StringTableBuilder.h"
48#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
49#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000050#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000051#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000052#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000053#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000054#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
55#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000056#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000057#include "llvm/Support/CommandLine.h"
58#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000059#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000060#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000061#include "llvm/Support/Format.h"
62#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000063#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000064#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000065#include "llvm/Support/Process.h"
Zachary Turner72c5b642016-09-09 18:17:52 +000066#include "llvm/Support/Regex.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000067#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000068#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000069#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000070
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000071using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000072using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000073using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000074using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000075
76namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000077
Zachary Turnera30bd1a2016-06-30 17:42:48 +000078cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
79cl::SubCommand
80 PrettySubcommand("pretty",
81 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000082cl::SubCommand
83 YamlToPdbSubcommand("yaml2pdb",
84 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000085cl::SubCommand
86 PdbToYamlSubcommand("pdb2yaml",
87 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000088
Zachary Turner7797c722015-03-02 04:39:56 +000089cl::OptionCategory TypeCategory("Symbol Type Options");
90cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000091cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000092
93namespace pretty {
94cl::list<std::string> InputFilenames(cl::Positional,
95 cl::desc("<input PDB files>"),
96 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000097
98cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +000099 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000100cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000101 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000102cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000103 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000104cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000105 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
106cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
107 cl::sub(PrettySubcommand));
108cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
109 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000110cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000111 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000112 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000113
Zachary Turnere5cb2692015-05-01 20:24:26 +0000114cl::opt<uint64_t> LoadAddress(
115 "load-address",
116 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000117 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
118cl::list<std::string> ExcludeTypes(
119 "exclude-types", cl::desc("Exclude types by regular expression"),
120 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
121cl::list<std::string> ExcludeSymbols(
122 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
123 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
124cl::list<std::string> ExcludeCompilands(
125 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
126 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000127
128cl::list<std::string> IncludeTypes(
129 "include-types",
130 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000131 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000132cl::list<std::string> IncludeSymbols(
133 "include-symbols",
134 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000135 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000136cl::list<std::string> IncludeCompilands(
137 "include-compilands",
138 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000139 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000140
Zachary Turner7797c722015-03-02 04:39:56 +0000141cl::opt<bool> ExcludeCompilerGenerated(
142 "no-compiler-generated",
143 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000144 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000145cl::opt<bool>
146 ExcludeSystemLibraries("no-system-libs",
147 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000148 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000149cl::opt<bool> NoClassDefs("no-class-definitions",
150 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000151 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000152cl::opt<bool> NoEnumDefs("no-enum-definitions",
153 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000154 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
155}
156
157namespace raw {
158
159cl::OptionCategory MsfOptions("MSF Container Options");
160cl::OptionCategory TypeOptions("Type Record Options");
161cl::OptionCategory FileOptions("Module & File Options");
162cl::OptionCategory SymbolOptions("Symbol Options");
163cl::OptionCategory MiscOptions("Miscellaneous Options");
164
165// MSF OPTIONS
166cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
167 cl::cat(MsfOptions), cl::sub(RawSubcommand));
168cl::opt<bool> DumpStreamBlocks("stream-blocks",
169 cl::desc("dump PDB stream blocks"),
170 cl::cat(MsfOptions), cl::sub(RawSubcommand));
171cl::opt<bool> DumpStreamSummary("stream-summary",
172 cl::desc("dump summary of the PDB streams"),
173 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000174cl::opt<bool> DumpPageStats(
175 "page-stats",
176 cl::desc("dump allocation stats of the pages in the MSF file"),
177 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turner72c5b642016-09-09 18:17:52 +0000178cl::opt<std::string>
179 DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
180 cl::desc("Dump binary data from specified range."),
181 cl::cat(MsfOptions), cl::sub(RawSubcommand));
182llvm::Optional<BlockRange> DumpBlockRange;
183
184cl::list<uint32_t>
185 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
186 cl::desc("Dump binary data from specified streams."),
187 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000188
189// TYPE OPTIONS
190cl::opt<bool>
Zachary Turner44a643c2017-01-12 22:28:15 +0000191 CompactRecords("compact-records",
192 cl::desc("Dump type and symbol records with less detail"),
193 cl::cat(TypeOptions), cl::sub(RawSubcommand));
194
195cl::opt<bool>
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000196 DumpTpiRecords("tpi-records",
197 cl::desc("dump CodeView type records from TPI stream"),
198 cl::cat(TypeOptions), cl::sub(RawSubcommand));
199cl::opt<bool> DumpTpiRecordBytes(
200 "tpi-record-bytes",
201 cl::desc("dump CodeView type record raw bytes from TPI stream"),
202 cl::cat(TypeOptions), cl::sub(RawSubcommand));
203cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
204 cl::cat(TypeOptions), cl::sub(RawSubcommand));
205cl::opt<bool>
206 DumpIpiRecords("ipi-records",
207 cl::desc("dump CodeView type records from IPI stream"),
208 cl::cat(TypeOptions), cl::sub(RawSubcommand));
209cl::opt<bool> DumpIpiRecordBytes(
210 "ipi-record-bytes",
211 cl::desc("dump CodeView type record raw bytes from IPI stream"),
212 cl::cat(TypeOptions), cl::sub(RawSubcommand));
213
214// MODULE & FILE OPTIONS
215cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
216 cl::cat(FileOptions), cl::sub(RawSubcommand));
217cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
218 cl::cat(FileOptions), cl::sub(RawSubcommand));
219cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000220 cl::desc("dump file and line information"),
221 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000222
223// SYMBOL OPTIONS
Bob Haarman653baa22016-10-21 19:43:19 +0000224cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
225 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000226cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
227 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
228cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
229 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
230cl::opt<bool>
231 DumpSymRecordBytes("sym-record-bytes",
232 cl::desc("dump CodeView symbol record raw bytes"),
233 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
234
235// MISCELLANEOUS OPTIONS
Zachary Turner760ad4d2017-01-20 22:42:09 +0000236cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
237 cl::cat(MiscOptions), cl::sub(RawSubcommand));
238
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000239cl::opt<bool> DumpSectionContribs("section-contribs",
240 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000241 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000242cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
243 cl::cat(MiscOptions), cl::sub(RawSubcommand));
244cl::opt<bool> DumpSectionHeaders("section-headers",
245 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000246 cl::cat(MiscOptions), cl::sub(RawSubcommand));
247cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
248 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000249
Zachary Turnerab58ae82016-06-30 17:43:00 +0000250cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
251 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000252
253cl::list<std::string> InputFilenames(cl::Positional,
254 cl::desc("<input PDB files>"),
255 cl::OneOrMore, cl::sub(RawSubcommand));
256}
257
258namespace yaml2pdb {
259cl::opt<std::string>
260 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
261 cl::sub(YamlToPdbSubcommand));
262
263cl::list<std::string> InputFilename(cl::Positional,
264 cl::desc("<input YAML file>"), cl::Required,
265 cl::sub(YamlToPdbSubcommand));
266}
267
268namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000269cl::opt<bool>
270 NoFileHeaders("no-file-headers",
271 cl::desc("Do not dump MSF file headers (you will not be able "
272 "to generate a fresh PDB from the resulting YAML)"),
273 cl::sub(PdbToYamlSubcommand), cl::init(false));
274
Zachary Turnerab58ae82016-06-30 17:43:00 +0000275cl::opt<bool> StreamMetadata(
276 "stream-metadata",
277 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000278 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000279cl::opt<bool> StreamDirectory(
280 "stream-directory",
281 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000282 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000283cl::opt<bool> PdbStream("pdb-stream",
284 cl::desc("Dump the PDB Stream (Stream 1)"),
285 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turner760ad4d2017-01-20 22:42:09 +0000286
287cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
288 cl::sub(PdbToYamlSubcommand), cl::init(false));
289
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000290cl::opt<bool> DbiStream("dbi-stream",
291 cl::desc("Dump the DBI Stream (Stream 2)"),
292 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000293cl::opt<bool>
294 DbiModuleInfo("dbi-module-info",
295 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
296 cl::sub(PdbToYamlSubcommand), cl::init(false));
297
Zachary Turner3b147642016-10-08 01:12:01 +0000298cl::opt<bool> DbiModuleSyms(
299 "dbi-module-syms",
300 cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
301 cl::sub(PdbToYamlSubcommand), cl::init(false));
302
Zachary Turnerd218c262016-07-22 15:46:37 +0000303cl::opt<bool> DbiModuleSourceFileInfo(
304 "dbi-module-source-info",
305 cl::desc(
306 "Dump DBI Module Source File Information (implies -dbi-module-info"),
307 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000308
Zachary Turnerac5763e2016-08-18 16:49:29 +0000309cl::opt<bool> TpiStream("tpi-stream",
310 cl::desc("Dump the TPI Stream (Stream 3)"),
311 cl::sub(PdbToYamlSubcommand), cl::init(false));
312
Zachary Turnerde9ba152016-09-15 18:22:31 +0000313cl::opt<bool> IpiStream("ipi-stream",
314 cl::desc("Dump the IPI Stream (Stream 5)"),
315 cl::sub(PdbToYamlSubcommand), cl::init(false));
316
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000317cl::list<std::string> InputFilename(cl::Positional,
318 cl::desc("<input PDB file>"), cl::Required,
319 cl::sub(PdbToYamlSubcommand));
320}
Zachary Turner49693b42015-01-28 01:22:33 +0000321}
322
David Majnemerc165c882016-05-28 18:25:15 +0000323static ExitOnError ExitOnErr;
324
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000325static void yamlToPdb(StringRef Path) {
Zachary Turnere109dc62016-07-22 19:56:26 +0000326 BumpPtrAllocator Allocator;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000327 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
328 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
329 /*RequiresNullTerminator=*/false);
330
331 if (ErrorOrBuffer.getError()) {
332 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
333 }
334
335 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
336
337 llvm::yaml::Input In(Buffer->getBuffer());
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000338 pdb::yaml::PdbObject YamlObj(Allocator);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000339 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000340 if (!YamlObj.Headers.hasValue())
341 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
342 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000343
Zachary Turnere109dc62016-07-22 19:56:26 +0000344 PDBFileBuilder Builder(Allocator);
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000345
Rui Ueyama5d6714e2016-09-30 20:52:12 +0000346 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock.BlockSize));
Zachary Turner620961d2016-09-14 23:00:02 +0000347 // Add each of the reserved streams. We ignore stream metadata in the
348 // yaml, because we will reconstruct our own view of the streams. For
349 // example, the YAML may say that there were 20 streams in the original
350 // PDB, but maybe we only dump a subset of those 20 streams, so we will
351 // have fewer, and the ones we do have may end up with different indices
352 // than the ones in the original PDB. So we just start with a clean slate.
353 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
354 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000355
Zachary Turner760ad4d2017-01-20 22:42:09 +0000356 if (YamlObj.StringTable.hasValue()) {
357 auto &Strings = Builder.getStringTableBuilder();
358 for (auto S : *YamlObj.StringTable)
359 Strings.insert(S);
360 }
361
Zachary Turner8848a7a2016-07-06 18:05:57 +0000362 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000363 auto &InfoBuilder = Builder.getInfoBuilder();
364 InfoBuilder.setAge(YamlObj.PdbStream->Age);
365 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
366 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
367 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000368 }
369
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000370 if (YamlObj.DbiStream.hasValue()) {
371 auto &DbiBuilder = Builder.getDbiBuilder();
372 DbiBuilder.setAge(YamlObj.DbiStream->Age);
373 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
374 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
375 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
376 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
377 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
378 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000379 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
380 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
381 for (auto S : MI.SourceFiles)
382 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
383 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000384 }
385
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000386 if (YamlObj.TpiStream.hasValue()) {
387 auto &TpiBuilder = Builder.getTpiBuilder();
388 TpiBuilder.setVersionHeader(YamlObj.TpiStream->Version);
389 for (const auto &R : YamlObj.TpiStream->Records)
390 TpiBuilder.addTypeRecord(R.Record);
391 }
392
Zachary Turnerde9ba152016-09-15 18:22:31 +0000393 if (YamlObj.IpiStream.hasValue()) {
394 auto &IpiBuilder = Builder.getIpiBuilder();
395 IpiBuilder.setVersionHeader(YamlObj.IpiStream->Version);
396 for (const auto &R : YamlObj.IpiStream->Records)
397 IpiBuilder.addTypeRecord(R.Record);
398 }
399
Rui Ueyamafc22cef2016-09-30 20:34:44 +0000400 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000401}
402
Zachary Turner8848a7a2016-07-06 18:05:57 +0000403static void pdb2Yaml(StringRef Path) {
404 std::unique_ptr<IPDBSession> Session;
405 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
406
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000407 NativeSession *RS = static_cast<NativeSession *>(Session.get());
Zachary Turner8848a7a2016-07-06 18:05:57 +0000408 PDBFile &File = RS->getPDBFile();
409 auto O = llvm::make_unique<YAMLOutputStyle>(File);
410 O = llvm::make_unique<YAMLOutputStyle>(File);
411
412 ExitOnErr(O->dump());
413}
414
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000415static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000416 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000417 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000418
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000419 NativeSession *RS = static_cast<NativeSession *>(Session.get());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000420 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000421 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000422
423 ExitOnErr(O->dump());
424}
425
426static void dumpPretty(StringRef Path) {
427 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000428
David Majnemerc165c882016-05-28 18:25:15 +0000429 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000430
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000431 if (opts::pretty::LoadAddress)
432 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000433
Zachary Turner2d11c202015-02-27 09:15:59 +0000434 LinePrinter Printer(2, outs());
435
Zachary Turnera5549172015-02-10 22:43:25 +0000436 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000437 std::string FileName(GlobalScope->getSymbolsFileName());
438
Zachary Turner2d11c202015-02-27 09:15:59 +0000439 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
440 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
441 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000442 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000443
Zachary Turner2d11c202015-02-27 09:15:59 +0000444 Printer.NewLine();
445 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000446 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000447 Printer << ": " << FileSize << " bytes";
448 } else {
449 Printer << ": (Unable to obtain file size)";
450 }
451
452 Printer.NewLine();
453 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
454 Printer << ": " << GlobalScope->getGuid();
455
456 Printer.NewLine();
457 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
458 Printer << ": " << GlobalScope->getAge();
459
460 Printer.NewLine();
461 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
462 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000463 if (GlobalScope->hasCTypes())
464 outs() << "HasCTypes ";
465 if (GlobalScope->hasPrivateSymbols())
466 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000467 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000468
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000469 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000470 Printer.NewLine();
471 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
472 << "---COMPILANDS---";
473 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000474 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000475 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000476 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000477 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000478 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000479 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000480 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000481 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000482 }
483
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000484 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000485 Printer.NewLine();
486 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
487 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000488 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000489 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000490 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000491 }
492
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000493 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000494 Printer.NewLine();
495 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
496 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000497 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000498 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000499 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000500 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000501 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000502 }
503
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000504 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000505 Printer.NewLine();
506 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
507 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000508 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000509 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000510 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000511 while (auto Function = Functions->getNext()) {
512 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000513 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000514 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000515 }
516 {
517 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000518 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000519 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000520 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000521 }
522 {
523 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000524 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000525 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000526 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000527 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000528 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000529 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000530 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000531 Printer.NewLine();
532 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
533 Printer.Indent();
534 ExternalSymbolDumper Dumper(Printer);
535 Dumper.start(*GlobalScope);
536 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000537 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000538 Printer.NewLine();
539 }
Zachary Turnera5549172015-02-10 22:43:25 +0000540 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000541}
542
543int main(int argc_, const char *argv_[]) {
544 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000545 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000546 PrettyStackTraceProgram X(argc_, argv_);
547
David Majnemerc165c882016-05-28 18:25:15 +0000548 ExitOnErr.setBanner("llvm-pdbdump: ");
549
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000550 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000551 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000552 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
553 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000554
555 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
556
557 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner72c5b642016-09-09 18:17:52 +0000558 if (!opts::raw::DumpBlockRangeOpt.empty()) {
559 llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
560 llvm::SmallVector<llvm::StringRef, 2> Matches;
561 if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
562 errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
563 << "' invalid format.\n";
564 errs().flush();
565 exit(1);
566 }
567 opts::raw::DumpBlockRange.emplace();
568 Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
569 if (!Matches[3].empty()) {
570 opts::raw::DumpBlockRange->Max.emplace();
571 Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
572 }
573 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000574
Zachary Turner44a643c2017-01-12 22:28:15 +0000575 if (opts::RawSubcommand) {
576 if (opts::raw::RawAll) {
577 opts::raw::DumpHeaders = true;
578 opts::raw::DumpModules = true;
579 opts::raw::DumpModuleFiles = true;
580 opts::raw::DumpModuleSyms = true;
581 opts::raw::DumpGlobals = true;
582 opts::raw::DumpPublics = true;
583 opts::raw::DumpSectionHeaders = true;
584 opts::raw::DumpStreamSummary = true;
585 opts::raw::DumpPageStats = true;
586 opts::raw::DumpStreamBlocks = true;
587 opts::raw::DumpTpiRecords = true;
588 opts::raw::DumpTpiHash = true;
589 opts::raw::DumpIpiRecords = true;
590 opts::raw::DumpSectionMap = true;
591 opts::raw::DumpSectionContribs = true;
592 opts::raw::DumpLineInfo = true;
593 opts::raw::DumpFpo = true;
Zachary Turner760ad4d2017-01-20 22:42:09 +0000594 opts::raw::DumpStringTable = true;
Zachary Turner44a643c2017-01-12 22:28:15 +0000595 }
596
597 if (opts::raw::CompactRecords &&
598 (opts::raw::DumpTpiRecordBytes || opts::raw::DumpIpiRecordBytes)) {
599 errs() << "-compact-records is incompatible with -tpi-record-bytes and "
600 "-ipi-record-bytes.\n";
601 exit(1);
602 }
Zachary Turner7797c722015-03-02 04:39:56 +0000603 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000604
Zachary Turner93839cb2016-06-02 05:07:49 +0000605 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000606
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000607 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000608 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000609 } else if (opts::YamlToPdbSubcommand) {
610 yamlToPdb(opts::yaml2pdb::InputFilename.front());
611 } else if (opts::PrettySubcommand) {
612 if (opts::pretty::Lines)
613 opts::pretty::Compilands = true;
614
615 if (opts::pretty::All) {
616 opts::pretty::Compilands = true;
617 opts::pretty::Symbols = true;
618 opts::pretty::Globals = true;
619 opts::pretty::Types = true;
620 opts::pretty::Externals = true;
621 opts::pretty::Lines = true;
622 }
623
624 // When adding filters for excluded compilands and types, we need to
Zachary Turner72c5b642016-09-09 18:17:52 +0000625 // remember that these are regexes. So special characters such as * and \
626 // need to be escaped in the regex. In the case of a literal \, this means
627 // it needs to be escaped again in the C++. So matching a single \ in the
628 // input requires 4 \es in the C++.
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000629 if (opts::pretty::ExcludeCompilerGenerated) {
630 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
631 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
632 }
633 if (opts::pretty::ExcludeSystemLibraries) {
634 opts::pretty::ExcludeCompilands.push_back(
635 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
636 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
637 opts::pretty::ExcludeCompilands.push_back(
638 "d:\\\\th.obj.x86fre\\\\minkernel");
639 }
640 std::for_each(opts::pretty::InputFilenames.begin(),
641 opts::pretty::InputFilenames.end(), dumpPretty);
642 } else if (opts::RawSubcommand) {
643 std::for_each(opts::raw::InputFilenames.begin(),
644 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000645 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000646
Zachary Turner819e77d2016-05-06 20:51:57 +0000647 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000648 return 0;
649}