blob: 783d3cfb743c9d924f48118d182491e292f60681 [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 Turnerd50c0132017-02-01 18:30:22 +000017
18#include "Analyze.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000019#include "LLVMOutputStyle.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000020#include "LinePrinter.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000021#include "OutputStyle.h"
Zachary Turnera9054dd2017-01-11 00:35:43 +000022#include "PrettyCompilandDumper.h"
23#include "PrettyExternalSymbolDumper.h"
24#include "PrettyFunctionDumper.h"
25#include "PrettyTypeDumper.h"
26#include "PrettyVariableDumper.h"
Zachary Turner7120a472016-06-06 20:37:05 +000027#include "YAMLOutputStyle.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000028
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000029#include "llvm/ADT/ArrayRef.h"
David Majnemer6e081262015-10-15 01:27:19 +000030#include "llvm/ADT/BitVector.h"
31#include "llvm/ADT/DenseMap.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 Turnera3225b02016-07-29 20:56:36 +000034#include "llvm/DebugInfo/MSF/MSFBuilder.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000035#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000036#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000037#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000038#include "llvm/DebugInfo/PDB/IPDBSession.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000039#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
40#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
41#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
42#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
43#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
44#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
45#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
46#include "llvm/DebugInfo/PDB/Native/RawConstants.h"
47#include "llvm/DebugInfo/PDB/Native/RawError.h"
48#include "llvm/DebugInfo/PDB/Native/StringTableBuilder.h"
49#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
50#include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000051#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000052#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000053#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000054#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000055#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
56#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000057#include "llvm/Support/BinaryByteStream.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000058#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000059#include "llvm/Support/CommandLine.h"
60#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000061#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000062#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000063#include "llvm/Support/Format.h"
64#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000065#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000066#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000067#include "llvm/Support/Process.h"
Zachary Turner72c5b642016-09-09 18:17:52 +000068#include "llvm/Support/Regex.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000069#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000070#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000071#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000072
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000073using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000074using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000075using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000076using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000077
78namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000079
Zachary Turnera30bd1a2016-06-30 17:42:48 +000080cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
81cl::SubCommand
82 PrettySubcommand("pretty",
83 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000084cl::SubCommand
85 YamlToPdbSubcommand("yaml2pdb",
86 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000087cl::SubCommand
88 PdbToYamlSubcommand("pdb2yaml",
89 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000090
Zachary Turnerd50c0132017-02-01 18:30:22 +000091cl::SubCommand
92 AnalyzeSubcommand("analyze",
93 "Analyze various aspects of a PDB's structure");
94
Zachary Turner7797c722015-03-02 04:39:56 +000095cl::OptionCategory TypeCategory("Symbol Type Options");
96cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000097cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000098
99namespace pretty {
100cl::list<std::string> InputFilenames(cl::Positional,
101 cl::desc("<input PDB files>"),
102 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000103
104cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000105 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000106cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000107 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000108cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000109 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000110cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000111 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
112cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
113 cl::sub(PrettySubcommand));
114cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
115 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000116cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000117 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000118 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000119
Zachary Turnere5cb2692015-05-01 20:24:26 +0000120cl::opt<uint64_t> LoadAddress(
121 "load-address",
122 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000123 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
124cl::list<std::string> ExcludeTypes(
125 "exclude-types", cl::desc("Exclude types by regular expression"),
126 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
127cl::list<std::string> ExcludeSymbols(
128 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
129 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
130cl::list<std::string> ExcludeCompilands(
131 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
132 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000133
134cl::list<std::string> IncludeTypes(
135 "include-types",
136 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000137 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000138cl::list<std::string> IncludeSymbols(
139 "include-symbols",
140 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000141 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000142cl::list<std::string> IncludeCompilands(
143 "include-compilands",
144 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000145 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000146
Zachary Turner7797c722015-03-02 04:39:56 +0000147cl::opt<bool> ExcludeCompilerGenerated(
148 "no-compiler-generated",
149 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000150 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000151cl::opt<bool>
152 ExcludeSystemLibraries("no-system-libs",
153 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000154 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000155cl::opt<bool> NoClassDefs("no-class-definitions",
156 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000157 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000158cl::opt<bool> NoEnumDefs("no-enum-definitions",
159 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000160 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
161}
162
163namespace raw {
164
165cl::OptionCategory MsfOptions("MSF Container Options");
166cl::OptionCategory TypeOptions("Type Record Options");
167cl::OptionCategory FileOptions("Module & File Options");
168cl::OptionCategory SymbolOptions("Symbol Options");
169cl::OptionCategory MiscOptions("Miscellaneous Options");
170
171// MSF OPTIONS
172cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
173 cl::cat(MsfOptions), cl::sub(RawSubcommand));
174cl::opt<bool> DumpStreamBlocks("stream-blocks",
175 cl::desc("dump PDB stream blocks"),
176 cl::cat(MsfOptions), cl::sub(RawSubcommand));
177cl::opt<bool> DumpStreamSummary("stream-summary",
178 cl::desc("dump summary of the PDB streams"),
179 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000180cl::opt<bool> DumpPageStats(
181 "page-stats",
182 cl::desc("dump allocation stats of the pages in the MSF file"),
183 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turner72c5b642016-09-09 18:17:52 +0000184cl::opt<std::string>
185 DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
186 cl::desc("Dump binary data from specified range."),
187 cl::cat(MsfOptions), cl::sub(RawSubcommand));
188llvm::Optional<BlockRange> DumpBlockRange;
189
190cl::list<uint32_t>
191 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
192 cl::desc("Dump binary data from specified streams."),
193 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000194
195// TYPE OPTIONS
196cl::opt<bool>
Zachary Turner44a643c2017-01-12 22:28:15 +0000197 CompactRecords("compact-records",
198 cl::desc("Dump type and symbol records with less detail"),
199 cl::cat(TypeOptions), cl::sub(RawSubcommand));
200
201cl::opt<bool>
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000202 DumpTpiRecords("tpi-records",
203 cl::desc("dump CodeView type records from TPI stream"),
204 cl::cat(TypeOptions), cl::sub(RawSubcommand));
205cl::opt<bool> DumpTpiRecordBytes(
206 "tpi-record-bytes",
207 cl::desc("dump CodeView type record raw bytes from TPI stream"),
208 cl::cat(TypeOptions), cl::sub(RawSubcommand));
209cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
210 cl::cat(TypeOptions), cl::sub(RawSubcommand));
211cl::opt<bool>
212 DumpIpiRecords("ipi-records",
213 cl::desc("dump CodeView type records from IPI stream"),
214 cl::cat(TypeOptions), cl::sub(RawSubcommand));
215cl::opt<bool> DumpIpiRecordBytes(
216 "ipi-record-bytes",
217 cl::desc("dump CodeView type record raw bytes from IPI stream"),
218 cl::cat(TypeOptions), cl::sub(RawSubcommand));
219
220// MODULE & FILE OPTIONS
221cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
222 cl::cat(FileOptions), cl::sub(RawSubcommand));
223cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
224 cl::cat(FileOptions), cl::sub(RawSubcommand));
225cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000226 cl::desc("dump file and line information"),
227 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000228
229// SYMBOL OPTIONS
Bob Haarman653baa22016-10-21 19:43:19 +0000230cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
231 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000232cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
233 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
234cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
235 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
236cl::opt<bool>
237 DumpSymRecordBytes("sym-record-bytes",
238 cl::desc("dump CodeView symbol record raw bytes"),
239 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
240
241// MISCELLANEOUS OPTIONS
Zachary Turner760ad4d2017-01-20 22:42:09 +0000242cl::opt<bool> DumpStringTable("string-table", cl::desc("dump PDB String Table"),
243 cl::cat(MiscOptions), cl::sub(RawSubcommand));
244
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000245cl::opt<bool> DumpSectionContribs("section-contribs",
246 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000247 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000248cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
249 cl::cat(MiscOptions), cl::sub(RawSubcommand));
250cl::opt<bool> DumpSectionHeaders("section-headers",
251 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000252 cl::cat(MiscOptions), cl::sub(RawSubcommand));
253cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
254 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000255
Zachary Turnerab58ae82016-06-30 17:43:00 +0000256cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
257 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000258
259cl::list<std::string> InputFilenames(cl::Positional,
260 cl::desc("<input PDB files>"),
261 cl::OneOrMore, cl::sub(RawSubcommand));
262}
263
264namespace yaml2pdb {
265cl::opt<std::string>
266 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
267 cl::sub(YamlToPdbSubcommand));
268
269cl::list<std::string> InputFilename(cl::Positional,
270 cl::desc("<input YAML file>"), cl::Required,
271 cl::sub(YamlToPdbSubcommand));
272}
273
274namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000275cl::opt<bool>
276 NoFileHeaders("no-file-headers",
277 cl::desc("Do not dump MSF file headers (you will not be able "
278 "to generate a fresh PDB from the resulting YAML)"),
279 cl::sub(PdbToYamlSubcommand), cl::init(false));
280
Zachary Turnerab58ae82016-06-30 17:43:00 +0000281cl::opt<bool> StreamMetadata(
282 "stream-metadata",
283 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000284 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000285cl::opt<bool> StreamDirectory(
286 "stream-directory",
287 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000288 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000289cl::opt<bool> PdbStream("pdb-stream",
290 cl::desc("Dump the PDB Stream (Stream 1)"),
291 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turner760ad4d2017-01-20 22:42:09 +0000292
293cl::opt<bool> StringTable("string-table", cl::desc("Dump the PDB String Table"),
294 cl::sub(PdbToYamlSubcommand), cl::init(false));
295
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000296cl::opt<bool> DbiStream("dbi-stream",
297 cl::desc("Dump the DBI Stream (Stream 2)"),
298 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000299cl::opt<bool>
300 DbiModuleInfo("dbi-module-info",
301 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
302 cl::sub(PdbToYamlSubcommand), cl::init(false));
303
Zachary Turner3b147642016-10-08 01:12:01 +0000304cl::opt<bool> DbiModuleSyms(
305 "dbi-module-syms",
306 cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
307 cl::sub(PdbToYamlSubcommand), cl::init(false));
308
Zachary Turnerd218c262016-07-22 15:46:37 +0000309cl::opt<bool> DbiModuleSourceFileInfo(
310 "dbi-module-source-info",
311 cl::desc(
312 "Dump DBI Module Source File Information (implies -dbi-module-info"),
313 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000314
Zachary Turnerac5763e2016-08-18 16:49:29 +0000315cl::opt<bool> TpiStream("tpi-stream",
316 cl::desc("Dump the TPI Stream (Stream 3)"),
317 cl::sub(PdbToYamlSubcommand), cl::init(false));
318
Zachary Turnerde9ba152016-09-15 18:22:31 +0000319cl::opt<bool> IpiStream("ipi-stream",
320 cl::desc("Dump the IPI Stream (Stream 5)"),
321 cl::sub(PdbToYamlSubcommand), cl::init(false));
322
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000323cl::list<std::string> InputFilename(cl::Positional,
324 cl::desc("<input PDB file>"), cl::Required,
325 cl::sub(PdbToYamlSubcommand));
326}
Zachary Turnerd50c0132017-02-01 18:30:22 +0000327
328namespace analyze {
329cl::opt<bool> StringTable("hash-collisions", cl::desc("Find hash collisions"),
330 cl::sub(AnalyzeSubcommand), cl::init(false));
331cl::list<std::string> InputFilename(cl::Positional,
332 cl::desc("<input PDB file>"), cl::Required,
333 cl::sub(AnalyzeSubcommand));
334}
Zachary Turner49693b42015-01-28 01:22:33 +0000335}
336
David Majnemerc165c882016-05-28 18:25:15 +0000337static ExitOnError ExitOnErr;
338
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000339static void yamlToPdb(StringRef Path) {
Zachary Turnere109dc62016-07-22 19:56:26 +0000340 BumpPtrAllocator Allocator;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000341 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
342 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
343 /*RequiresNullTerminator=*/false);
344
345 if (ErrorOrBuffer.getError()) {
346 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
347 }
348
349 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
350
351 llvm::yaml::Input In(Buffer->getBuffer());
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000352 pdb::yaml::PdbObject YamlObj(Allocator);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000353 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000354 if (!YamlObj.Headers.hasValue())
355 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
356 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000357
Zachary Turnere109dc62016-07-22 19:56:26 +0000358 PDBFileBuilder Builder(Allocator);
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000359
Rui Ueyama5d6714e2016-09-30 20:52:12 +0000360 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock.BlockSize));
Zachary Turner620961d2016-09-14 23:00:02 +0000361 // Add each of the reserved streams. We ignore stream metadata in the
362 // yaml, because we will reconstruct our own view of the streams. For
363 // example, the YAML may say that there were 20 streams in the original
364 // PDB, but maybe we only dump a subset of those 20 streams, so we will
365 // have fewer, and the ones we do have may end up with different indices
366 // than the ones in the original PDB. So we just start with a clean slate.
367 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
368 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000369
Zachary Turner760ad4d2017-01-20 22:42:09 +0000370 if (YamlObj.StringTable.hasValue()) {
371 auto &Strings = Builder.getStringTableBuilder();
372 for (auto S : *YamlObj.StringTable)
373 Strings.insert(S);
374 }
375
Zachary Turner8848a7a2016-07-06 18:05:57 +0000376 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000377 auto &InfoBuilder = Builder.getInfoBuilder();
378 InfoBuilder.setAge(YamlObj.PdbStream->Age);
379 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
380 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
381 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000382 }
383
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000384 if (YamlObj.DbiStream.hasValue()) {
385 auto &DbiBuilder = Builder.getDbiBuilder();
386 DbiBuilder.setAge(YamlObj.DbiStream->Age);
387 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
388 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
389 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
390 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
391 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
392 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000393 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
394 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
395 for (auto S : MI.SourceFiles)
396 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
397 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000398 }
399
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000400 if (YamlObj.TpiStream.hasValue()) {
401 auto &TpiBuilder = Builder.getTpiBuilder();
402 TpiBuilder.setVersionHeader(YamlObj.TpiStream->Version);
403 for (const auto &R : YamlObj.TpiStream->Records)
404 TpiBuilder.addTypeRecord(R.Record);
405 }
406
Zachary Turnerde9ba152016-09-15 18:22:31 +0000407 if (YamlObj.IpiStream.hasValue()) {
408 auto &IpiBuilder = Builder.getIpiBuilder();
409 IpiBuilder.setVersionHeader(YamlObj.IpiStream->Version);
410 for (const auto &R : YamlObj.IpiStream->Records)
411 IpiBuilder.addTypeRecord(R.Record);
412 }
413
Rui Ueyamafc22cef2016-09-30 20:34:44 +0000414 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000415}
416
Zachary Turner8848a7a2016-07-06 18:05:57 +0000417static void pdb2Yaml(StringRef Path) {
418 std::unique_ptr<IPDBSession> Session;
Adrian McCarthy8f713192017-01-27 00:01:55 +0000419 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000420
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000421 NativeSession *RS = static_cast<NativeSession *>(Session.get());
Zachary Turner8848a7a2016-07-06 18:05:57 +0000422 PDBFile &File = RS->getPDBFile();
423 auto O = llvm::make_unique<YAMLOutputStyle>(File);
424 O = llvm::make_unique<YAMLOutputStyle>(File);
425
426 ExitOnErr(O->dump());
427}
428
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000429static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000430 std::unique_ptr<IPDBSession> Session;
Adrian McCarthy8f713192017-01-27 00:01:55 +0000431 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000432
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +0000433 NativeSession *RS = static_cast<NativeSession *>(Session.get());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000434 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000435 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000436
437 ExitOnErr(O->dump());
438}
439
Zachary Turnerd50c0132017-02-01 18:30:22 +0000440static void dumpAnalysis(StringRef Path) {
441 std::unique_ptr<IPDBSession> Session;
442 ExitOnErr(loadDataForPDB(PDB_ReaderType::Native, Path, Session));
443
444 NativeSession *NS = static_cast<NativeSession *>(Session.get());
445 PDBFile &File = NS->getPDBFile();
446 auto O = llvm::make_unique<AnalysisStyle>(File);
447
448 ExitOnErr(O->dump());
449}
450
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000451static void dumpPretty(StringRef Path) {
452 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000453
David Majnemerc165c882016-05-28 18:25:15 +0000454 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000455
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000456 if (opts::pretty::LoadAddress)
457 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000458
Zachary Turner2d11c202015-02-27 09:15:59 +0000459 LinePrinter Printer(2, outs());
460
Zachary Turnera5549172015-02-10 22:43:25 +0000461 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000462 std::string FileName(GlobalScope->getSymbolsFileName());
463
Zachary Turner2d11c202015-02-27 09:15:59 +0000464 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
465 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
466 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000467 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000468
Zachary Turner2d11c202015-02-27 09:15:59 +0000469 Printer.NewLine();
470 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000471 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000472 Printer << ": " << FileSize << " bytes";
473 } else {
474 Printer << ": (Unable to obtain file size)";
475 }
476
477 Printer.NewLine();
478 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
479 Printer << ": " << GlobalScope->getGuid();
480
481 Printer.NewLine();
482 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
483 Printer << ": " << GlobalScope->getAge();
484
485 Printer.NewLine();
486 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
487 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000488 if (GlobalScope->hasCTypes())
489 outs() << "HasCTypes ";
490 if (GlobalScope->hasPrivateSymbols())
491 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000492 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000493
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000494 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000495 Printer.NewLine();
496 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
497 << "---COMPILANDS---";
498 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000499 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000500 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000501 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000502 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000503 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000504 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000505 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000506 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000507 }
508
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000509 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000510 Printer.NewLine();
511 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
512 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000513 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000514 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000515 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000516 }
517
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000518 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000519 Printer.NewLine();
520 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
521 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000522 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000523 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000524 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000525 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000526 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000527 }
528
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000529 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000530 Printer.NewLine();
531 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
532 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000533 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000534 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000535 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000536 while (auto Function = Functions->getNext()) {
537 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000538 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000539 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000540 }
541 {
542 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000543 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000544 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000545 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000546 }
547 {
548 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000549 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000550 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000551 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000552 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000553 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000554 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000555 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000556 Printer.NewLine();
557 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
558 Printer.Indent();
559 ExternalSymbolDumper Dumper(Printer);
560 Dumper.start(*GlobalScope);
561 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000562 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000563 Printer.NewLine();
564 }
Zachary Turnera5549172015-02-10 22:43:25 +0000565 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000566}
567
568int main(int argc_, const char *argv_[]) {
569 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000570 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000571 PrettyStackTraceProgram X(argc_, argv_);
572
David Majnemerc165c882016-05-28 18:25:15 +0000573 ExitOnErr.setBanner("llvm-pdbdump: ");
574
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000575 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000576 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000577 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
578 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000579
580 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
581
582 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner72c5b642016-09-09 18:17:52 +0000583 if (!opts::raw::DumpBlockRangeOpt.empty()) {
584 llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
585 llvm::SmallVector<llvm::StringRef, 2> Matches;
586 if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
587 errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
588 << "' invalid format.\n";
589 errs().flush();
590 exit(1);
591 }
592 opts::raw::DumpBlockRange.emplace();
593 Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
594 if (!Matches[3].empty()) {
595 opts::raw::DumpBlockRange->Max.emplace();
596 Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
597 }
598 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000599
Zachary Turner44a643c2017-01-12 22:28:15 +0000600 if (opts::RawSubcommand) {
601 if (opts::raw::RawAll) {
602 opts::raw::DumpHeaders = true;
603 opts::raw::DumpModules = true;
604 opts::raw::DumpModuleFiles = true;
605 opts::raw::DumpModuleSyms = true;
606 opts::raw::DumpGlobals = true;
607 opts::raw::DumpPublics = true;
608 opts::raw::DumpSectionHeaders = true;
609 opts::raw::DumpStreamSummary = true;
610 opts::raw::DumpPageStats = true;
611 opts::raw::DumpStreamBlocks = true;
612 opts::raw::DumpTpiRecords = true;
613 opts::raw::DumpTpiHash = true;
614 opts::raw::DumpIpiRecords = true;
615 opts::raw::DumpSectionMap = true;
616 opts::raw::DumpSectionContribs = true;
617 opts::raw::DumpLineInfo = true;
618 opts::raw::DumpFpo = true;
Zachary Turner760ad4d2017-01-20 22:42:09 +0000619 opts::raw::DumpStringTable = true;
Zachary Turner44a643c2017-01-12 22:28:15 +0000620 }
621
622 if (opts::raw::CompactRecords &&
623 (opts::raw::DumpTpiRecordBytes || opts::raw::DumpIpiRecordBytes)) {
624 errs() << "-compact-records is incompatible with -tpi-record-bytes and "
625 "-ipi-record-bytes.\n";
626 exit(1);
627 }
Zachary Turner7797c722015-03-02 04:39:56 +0000628 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000629
Zachary Turner93839cb2016-06-02 05:07:49 +0000630 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000631
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000632 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000633 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000634 } else if (opts::YamlToPdbSubcommand) {
635 yamlToPdb(opts::yaml2pdb::InputFilename.front());
Zachary Turnerd50c0132017-02-01 18:30:22 +0000636 } else if (opts::AnalyzeSubcommand) {
637 dumpAnalysis(opts::analyze::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000638 } else if (opts::PrettySubcommand) {
639 if (opts::pretty::Lines)
640 opts::pretty::Compilands = true;
641
642 if (opts::pretty::All) {
643 opts::pretty::Compilands = true;
644 opts::pretty::Symbols = true;
645 opts::pretty::Globals = true;
646 opts::pretty::Types = true;
647 opts::pretty::Externals = true;
648 opts::pretty::Lines = true;
649 }
650
651 // When adding filters for excluded compilands and types, we need to
Zachary Turner72c5b642016-09-09 18:17:52 +0000652 // remember that these are regexes. So special characters such as * and \
653 // need to be escaped in the regex. In the case of a literal \, this means
654 // it needs to be escaped again in the C++. So matching a single \ in the
655 // input requires 4 \es in the C++.
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000656 if (opts::pretty::ExcludeCompilerGenerated) {
657 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
658 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
659 }
660 if (opts::pretty::ExcludeSystemLibraries) {
661 opts::pretty::ExcludeCompilands.push_back(
662 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
663 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
664 opts::pretty::ExcludeCompilands.push_back(
665 "d:\\\\th.obj.x86fre\\\\minkernel");
666 }
667 std::for_each(opts::pretty::InputFilenames.begin(),
668 opts::pretty::InputFilenames.end(), dumpPretty);
669 } else if (opts::RawSubcommand) {
670 std::for_each(opts::raw::InputFilenames.begin(),
671 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000672 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000673
Zachary Turner819e77d2016-05-06 20:51:57 +0000674 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000675 return 0;
676}