blob: cce0a04399774497115ee9a6545ced1fa43c7c3e [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"
38#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000039#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000040#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000041#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000042#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
43#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000044#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
45#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
Zachary Turner8848a7a2016-07-06 18:05:57 +000046#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000047#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000048#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000049#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000050#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Reid Klecknerce5196e2016-05-12 23:26:23 +000051#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000052#include "llvm/DebugInfo/PDB/Raw/RawSession.h"
Zachary Turnerc6d54da2016-09-09 17:46:17 +000053#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
54#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000055#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000056#include "llvm/Support/CommandLine.h"
57#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000058#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000059#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000060#include "llvm/Support/Format.h"
61#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000062#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000063#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000064#include "llvm/Support/Process.h"
Zachary Turner72c5b642016-09-09 18:17:52 +000065#include "llvm/Support/Regex.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000066#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000067#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000068#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000069
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000070using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000071using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000072using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000073using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000074
75namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000076
Zachary Turnera30bd1a2016-06-30 17:42:48 +000077cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
78cl::SubCommand
79 PrettySubcommand("pretty",
80 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000081cl::SubCommand
82 YamlToPdbSubcommand("yaml2pdb",
83 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000084cl::SubCommand
85 PdbToYamlSubcommand("pdb2yaml",
86 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000087
Zachary Turner7797c722015-03-02 04:39:56 +000088cl::OptionCategory TypeCategory("Symbol Type Options");
89cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000090cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000091
92namespace pretty {
93cl::list<std::string> InputFilenames(cl::Positional,
94 cl::desc("<input PDB files>"),
95 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000096
97cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +000098 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000099cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000100 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000101cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000102 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000103cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000104 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
105cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
106 cl::sub(PrettySubcommand));
107cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
108 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000109cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000110 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000111 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000112
Zachary Turnere5cb2692015-05-01 20:24:26 +0000113cl::opt<uint64_t> LoadAddress(
114 "load-address",
115 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000116 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
117cl::list<std::string> ExcludeTypes(
118 "exclude-types", cl::desc("Exclude types by regular expression"),
119 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
120cl::list<std::string> ExcludeSymbols(
121 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
122 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
123cl::list<std::string> ExcludeCompilands(
124 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
125 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000126
127cl::list<std::string> IncludeTypes(
128 "include-types",
129 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000130 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000131cl::list<std::string> IncludeSymbols(
132 "include-symbols",
133 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000134 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000135cl::list<std::string> IncludeCompilands(
136 "include-compilands",
137 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000138 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000139
Zachary Turner7797c722015-03-02 04:39:56 +0000140cl::opt<bool> ExcludeCompilerGenerated(
141 "no-compiler-generated",
142 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000143 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000144cl::opt<bool>
145 ExcludeSystemLibraries("no-system-libs",
146 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000147 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000148cl::opt<bool> NoClassDefs("no-class-definitions",
149 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000150 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000151cl::opt<bool> NoEnumDefs("no-enum-definitions",
152 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000153 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
154}
155
156namespace raw {
157
158cl::OptionCategory MsfOptions("MSF Container Options");
159cl::OptionCategory TypeOptions("Type Record Options");
160cl::OptionCategory FileOptions("Module & File Options");
161cl::OptionCategory SymbolOptions("Symbol Options");
162cl::OptionCategory MiscOptions("Miscellaneous Options");
163
164// MSF OPTIONS
165cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
166 cl::cat(MsfOptions), cl::sub(RawSubcommand));
167cl::opt<bool> DumpStreamBlocks("stream-blocks",
168 cl::desc("dump PDB stream blocks"),
169 cl::cat(MsfOptions), cl::sub(RawSubcommand));
170cl::opt<bool> DumpStreamSummary("stream-summary",
171 cl::desc("dump summary of the PDB streams"),
172 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000173cl::opt<bool> DumpPageStats(
174 "page-stats",
175 cl::desc("dump allocation stats of the pages in the MSF file"),
176 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turner72c5b642016-09-09 18:17:52 +0000177cl::opt<std::string>
178 DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
179 cl::desc("Dump binary data from specified range."),
180 cl::cat(MsfOptions), cl::sub(RawSubcommand));
181llvm::Optional<BlockRange> DumpBlockRange;
182
183cl::list<uint32_t>
184 DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
185 cl::desc("Dump binary data from specified streams."),
186 cl::cat(MsfOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000187
188// TYPE OPTIONS
189cl::opt<bool>
Zachary Turner44a643c2017-01-12 22:28:15 +0000190 CompactRecords("compact-records",
191 cl::desc("Dump type and symbol records with less detail"),
192 cl::cat(TypeOptions), cl::sub(RawSubcommand));
193
194cl::opt<bool>
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000195 DumpTpiRecords("tpi-records",
196 cl::desc("dump CodeView type records from TPI stream"),
197 cl::cat(TypeOptions), cl::sub(RawSubcommand));
198cl::opt<bool> DumpTpiRecordBytes(
199 "tpi-record-bytes",
200 cl::desc("dump CodeView type record raw bytes from TPI stream"),
201 cl::cat(TypeOptions), cl::sub(RawSubcommand));
202cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
203 cl::cat(TypeOptions), cl::sub(RawSubcommand));
204cl::opt<bool>
205 DumpIpiRecords("ipi-records",
206 cl::desc("dump CodeView type records from IPI stream"),
207 cl::cat(TypeOptions), cl::sub(RawSubcommand));
208cl::opt<bool> DumpIpiRecordBytes(
209 "ipi-record-bytes",
210 cl::desc("dump CodeView type record raw bytes from IPI stream"),
211 cl::cat(TypeOptions), cl::sub(RawSubcommand));
212
213// MODULE & FILE OPTIONS
214cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
215 cl::cat(FileOptions), cl::sub(RawSubcommand));
216cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
217 cl::cat(FileOptions), cl::sub(RawSubcommand));
218cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000219 cl::desc("dump file and line information"),
220 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000221
222// SYMBOL OPTIONS
Bob Haarman653baa22016-10-21 19:43:19 +0000223cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
224 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000225cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
226 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
227cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
228 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
229cl::opt<bool>
230 DumpSymRecordBytes("sym-record-bytes",
231 cl::desc("dump CodeView symbol record raw bytes"),
232 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
233
234// MISCELLANEOUS OPTIONS
235cl::opt<bool> DumpSectionContribs("section-contribs",
236 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000237 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000238cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
239 cl::cat(MiscOptions), cl::sub(RawSubcommand));
240cl::opt<bool> DumpSectionHeaders("section-headers",
241 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000242 cl::cat(MiscOptions), cl::sub(RawSubcommand));
243cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
244 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000245
Zachary Turnerab58ae82016-06-30 17:43:00 +0000246cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
247 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000248
249cl::list<std::string> InputFilenames(cl::Positional,
250 cl::desc("<input PDB files>"),
251 cl::OneOrMore, cl::sub(RawSubcommand));
252}
253
254namespace yaml2pdb {
255cl::opt<std::string>
256 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
257 cl::sub(YamlToPdbSubcommand));
258
259cl::list<std::string> InputFilename(cl::Positional,
260 cl::desc("<input YAML file>"), cl::Required,
261 cl::sub(YamlToPdbSubcommand));
262}
263
264namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000265cl::opt<bool>
266 NoFileHeaders("no-file-headers",
267 cl::desc("Do not dump MSF file headers (you will not be able "
268 "to generate a fresh PDB from the resulting YAML)"),
269 cl::sub(PdbToYamlSubcommand), cl::init(false));
270
Zachary Turnerab58ae82016-06-30 17:43:00 +0000271cl::opt<bool> StreamMetadata(
272 "stream-metadata",
273 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000274 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000275cl::opt<bool> StreamDirectory(
276 "stream-directory",
277 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000278 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000279cl::opt<bool> PdbStream("pdb-stream",
280 cl::desc("Dump the PDB Stream (Stream 1)"),
281 cl::sub(PdbToYamlSubcommand), cl::init(false));
282cl::opt<bool> DbiStream("dbi-stream",
283 cl::desc("Dump the DBI Stream (Stream 2)"),
284 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000285cl::opt<bool>
286 DbiModuleInfo("dbi-module-info",
287 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
288 cl::sub(PdbToYamlSubcommand), cl::init(false));
289
Zachary Turner3b147642016-10-08 01:12:01 +0000290cl::opt<bool> DbiModuleSyms(
291 "dbi-module-syms",
292 cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
293 cl::sub(PdbToYamlSubcommand), cl::init(false));
294
Zachary Turnerd218c262016-07-22 15:46:37 +0000295cl::opt<bool> DbiModuleSourceFileInfo(
296 "dbi-module-source-info",
297 cl::desc(
298 "Dump DBI Module Source File Information (implies -dbi-module-info"),
299 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000300
Zachary Turnerac5763e2016-08-18 16:49:29 +0000301cl::opt<bool> TpiStream("tpi-stream",
302 cl::desc("Dump the TPI Stream (Stream 3)"),
303 cl::sub(PdbToYamlSubcommand), cl::init(false));
304
Zachary Turnerde9ba152016-09-15 18:22:31 +0000305cl::opt<bool> IpiStream("ipi-stream",
306 cl::desc("Dump the IPI Stream (Stream 5)"),
307 cl::sub(PdbToYamlSubcommand), cl::init(false));
308
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000309cl::list<std::string> InputFilename(cl::Positional,
310 cl::desc("<input PDB file>"), cl::Required,
311 cl::sub(PdbToYamlSubcommand));
312}
Zachary Turner49693b42015-01-28 01:22:33 +0000313}
314
David Majnemerc165c882016-05-28 18:25:15 +0000315static ExitOnError ExitOnErr;
316
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000317static void yamlToPdb(StringRef Path) {
Zachary Turnere109dc62016-07-22 19:56:26 +0000318 BumpPtrAllocator Allocator;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000319 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
320 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
321 /*RequiresNullTerminator=*/false);
322
323 if (ErrorOrBuffer.getError()) {
324 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
325 }
326
327 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
328
329 llvm::yaml::Input In(Buffer->getBuffer());
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000330 pdb::yaml::PdbObject YamlObj(Allocator);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000331 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000332 if (!YamlObj.Headers.hasValue())
333 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
334 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000335
Zachary Turnere109dc62016-07-22 19:56:26 +0000336 PDBFileBuilder Builder(Allocator);
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000337
Rui Ueyama5d6714e2016-09-30 20:52:12 +0000338 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock.BlockSize));
Zachary Turner620961d2016-09-14 23:00:02 +0000339 // Add each of the reserved streams. We ignore stream metadata in the
340 // yaml, because we will reconstruct our own view of the streams. For
341 // example, the YAML may say that there were 20 streams in the original
342 // PDB, but maybe we only dump a subset of those 20 streams, so we will
343 // have fewer, and the ones we do have may end up with different indices
344 // than the ones in the original PDB. So we just start with a clean slate.
345 for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
346 ExitOnErr(Builder.getMsfBuilder().addStream(0));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000347
348 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000349 auto &InfoBuilder = Builder.getInfoBuilder();
350 InfoBuilder.setAge(YamlObj.PdbStream->Age);
351 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
352 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
353 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner5e534c72016-07-15 22:17:08 +0000354 for (auto &NM : YamlObj.PdbStream->NamedStreams)
355 InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
356 NM.StreamNumber);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000357 }
358
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000359 if (YamlObj.DbiStream.hasValue()) {
360 auto &DbiBuilder = Builder.getDbiBuilder();
361 DbiBuilder.setAge(YamlObj.DbiStream->Age);
362 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
363 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
364 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
365 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
366 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
367 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000368 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
369 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
370 for (auto S : MI.SourceFiles)
371 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
372 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000373 }
374
Zachary Turnerc6d54da2016-09-09 17:46:17 +0000375 if (YamlObj.TpiStream.hasValue()) {
376 auto &TpiBuilder = Builder.getTpiBuilder();
377 TpiBuilder.setVersionHeader(YamlObj.TpiStream->Version);
378 for (const auto &R : YamlObj.TpiStream->Records)
379 TpiBuilder.addTypeRecord(R.Record);
380 }
381
Zachary Turnerde9ba152016-09-15 18:22:31 +0000382 if (YamlObj.IpiStream.hasValue()) {
383 auto &IpiBuilder = Builder.getIpiBuilder();
384 IpiBuilder.setVersionHeader(YamlObj.IpiStream->Version);
385 for (const auto &R : YamlObj.IpiStream->Records)
386 IpiBuilder.addTypeRecord(R.Record);
387 }
388
Rui Ueyamafc22cef2016-09-30 20:34:44 +0000389 ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000390}
391
Zachary Turner8848a7a2016-07-06 18:05:57 +0000392static void pdb2Yaml(StringRef Path) {
393 std::unique_ptr<IPDBSession> Session;
394 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
395
396 RawSession *RS = static_cast<RawSession *>(Session.get());
397 PDBFile &File = RS->getPDBFile();
398 auto O = llvm::make_unique<YAMLOutputStyle>(File);
399 O = llvm::make_unique<YAMLOutputStyle>(File);
400
401 ExitOnErr(O->dump());
402}
403
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000404static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000405 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000406 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000407
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000408 RawSession *RS = static_cast<RawSession *>(Session.get());
409 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000410 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000411
412 ExitOnErr(O->dump());
413}
414
415static void dumpPretty(StringRef Path) {
416 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000417
David Majnemerc165c882016-05-28 18:25:15 +0000418 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000419
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000420 if (opts::pretty::LoadAddress)
421 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000422
Zachary Turner2d11c202015-02-27 09:15:59 +0000423 LinePrinter Printer(2, outs());
424
Zachary Turnera5549172015-02-10 22:43:25 +0000425 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000426 std::string FileName(GlobalScope->getSymbolsFileName());
427
Zachary Turner2d11c202015-02-27 09:15:59 +0000428 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
429 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
430 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000431 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000432
Zachary Turner2d11c202015-02-27 09:15:59 +0000433 Printer.NewLine();
434 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000435 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000436 Printer << ": " << FileSize << " bytes";
437 } else {
438 Printer << ": (Unable to obtain file size)";
439 }
440
441 Printer.NewLine();
442 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
443 Printer << ": " << GlobalScope->getGuid();
444
445 Printer.NewLine();
446 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
447 Printer << ": " << GlobalScope->getAge();
448
449 Printer.NewLine();
450 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
451 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000452 if (GlobalScope->hasCTypes())
453 outs() << "HasCTypes ";
454 if (GlobalScope->hasPrivateSymbols())
455 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000456 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000457
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000458 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000459 Printer.NewLine();
460 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
461 << "---COMPILANDS---";
462 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000463 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000464 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000465 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000466 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000467 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000468 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000469 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000470 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000471 }
472
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000473 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000474 Printer.NewLine();
475 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
476 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000477 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000478 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000479 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000480 }
481
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000482 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000483 Printer.NewLine();
484 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
485 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000486 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000487 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000488 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000489 Dumper.start(*Compiland, true);
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::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000494 Printer.NewLine();
495 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
496 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000497 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000498 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000499 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000500 while (auto Function = Functions->getNext()) {
501 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000502 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000503 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000504 }
505 {
506 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000507 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000508 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000509 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000510 }
511 {
512 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000513 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000514 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000515 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000516 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000517 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000518 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000519 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000520 Printer.NewLine();
521 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
522 Printer.Indent();
523 ExternalSymbolDumper Dumper(Printer);
524 Dumper.start(*GlobalScope);
525 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000526 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000527 Printer.NewLine();
528 }
Zachary Turnera5549172015-02-10 22:43:25 +0000529 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000530}
531
532int main(int argc_, const char *argv_[]) {
533 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000534 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000535 PrettyStackTraceProgram X(argc_, argv_);
536
David Majnemerc165c882016-05-28 18:25:15 +0000537 ExitOnErr.setBanner("llvm-pdbdump: ");
538
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000539 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000540 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000541 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
542 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000543
544 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
545
546 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turner72c5b642016-09-09 18:17:52 +0000547 if (!opts::raw::DumpBlockRangeOpt.empty()) {
548 llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
549 llvm::SmallVector<llvm::StringRef, 2> Matches;
550 if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
551 errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
552 << "' invalid format.\n";
553 errs().flush();
554 exit(1);
555 }
556 opts::raw::DumpBlockRange.emplace();
557 Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
558 if (!Matches[3].empty()) {
559 opts::raw::DumpBlockRange->Max.emplace();
560 Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
561 }
562 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000563
Zachary Turner44a643c2017-01-12 22:28:15 +0000564 if (opts::RawSubcommand) {
565 if (opts::raw::RawAll) {
566 opts::raw::DumpHeaders = true;
567 opts::raw::DumpModules = true;
568 opts::raw::DumpModuleFiles = true;
569 opts::raw::DumpModuleSyms = true;
570 opts::raw::DumpGlobals = true;
571 opts::raw::DumpPublics = true;
572 opts::raw::DumpSectionHeaders = true;
573 opts::raw::DumpStreamSummary = true;
574 opts::raw::DumpPageStats = true;
575 opts::raw::DumpStreamBlocks = true;
576 opts::raw::DumpTpiRecords = true;
577 opts::raw::DumpTpiHash = true;
578 opts::raw::DumpIpiRecords = true;
579 opts::raw::DumpSectionMap = true;
580 opts::raw::DumpSectionContribs = true;
581 opts::raw::DumpLineInfo = true;
582 opts::raw::DumpFpo = true;
583 }
584
585 if (opts::raw::CompactRecords &&
586 (opts::raw::DumpTpiRecordBytes || opts::raw::DumpIpiRecordBytes)) {
587 errs() << "-compact-records is incompatible with -tpi-record-bytes and "
588 "-ipi-record-bytes.\n";
589 exit(1);
590 }
Zachary Turner7797c722015-03-02 04:39:56 +0000591 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000592
Zachary Turner93839cb2016-06-02 05:07:49 +0000593 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000594
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000595 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000596 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000597 } else if (opts::YamlToPdbSubcommand) {
598 yamlToPdb(opts::yaml2pdb::InputFilename.front());
599 } else if (opts::PrettySubcommand) {
600 if (opts::pretty::Lines)
601 opts::pretty::Compilands = true;
602
603 if (opts::pretty::All) {
604 opts::pretty::Compilands = true;
605 opts::pretty::Symbols = true;
606 opts::pretty::Globals = true;
607 opts::pretty::Types = true;
608 opts::pretty::Externals = true;
609 opts::pretty::Lines = true;
610 }
611
612 // When adding filters for excluded compilands and types, we need to
Zachary Turner72c5b642016-09-09 18:17:52 +0000613 // remember that these are regexes. So special characters such as * and \
614 // need to be escaped in the regex. In the case of a literal \, this means
615 // it needs to be escaped again in the C++. So matching a single \ in the
616 // input requires 4 \es in the C++.
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000617 if (opts::pretty::ExcludeCompilerGenerated) {
618 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
619 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
620 }
621 if (opts::pretty::ExcludeSystemLibraries) {
622 opts::pretty::ExcludeCompilands.push_back(
623 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
624 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
625 opts::pretty::ExcludeCompilands.push_back(
626 "d:\\\\th.obj.x86fre\\\\minkernel");
627 }
628 std::for_each(opts::pretty::InputFilenames.begin(),
629 opts::pretty::InputFilenames.end(), dumpPretty);
630 } else if (opts::RawSubcommand) {
631 std::for_each(opts::raw::InputFilenames.begin(),
632 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000633 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000634
Zachary Turner819e77d2016-05-06 20:51:57 +0000635 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000636 return 0;
637}