blob: 53e27d6cf139e3b336591f6254cb433c6177f5e0 [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"
17#include "CompilandDumper.h"
Zachary Turnere5cb2692015-05-01 20:24:26 +000018#include "ExternalSymbolDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000019#include "FunctionDumper.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000020#include "LLVMOutputStyle.h"
Zachary Turner2d11c202015-02-27 09:15:59 +000021#include "LinePrinter.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000022#include "OutputStyle.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000023#include "TypeDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000024#include "VariableDumper.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 Turnerbac69d32016-07-22 19:56:05 +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 Turner93839cb2016-06-02 05:07:49 +000053#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000054#include "llvm/Support/CommandLine.h"
55#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000056#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000057#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000058#include "llvm/Support/Format.h"
59#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000060#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000061#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000062#include "llvm/Support/Process.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000063#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000064#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000065#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000066
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000067using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000068using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000069using namespace llvm::msf;
Zachary Turner2f09b502016-04-29 17:28:47 +000070using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000071
Zachary Turner1dc9fd32016-06-14 20:48:36 +000072namespace {
73// A simple adapter that acts like a ByteStream but holds ownership over
74// and underlying FileOutputBuffer.
75class FileBufferByteStream : public ByteStream<true> {
76public:
77 FileBufferByteStream(std::unique_ptr<FileOutputBuffer> Buffer)
78 : ByteStream(MutableArrayRef<uint8_t>(Buffer->getBufferStart(),
79 Buffer->getBufferEnd())),
80 FileBuffer(std::move(Buffer)) {}
81
Zachary Turnerab58ae82016-06-30 17:43:00 +000082 Error commit() const override {
83 if (FileBuffer->commit())
84 return llvm::make_error<RawError>(raw_error_code::not_writable);
85 return Error::success();
86 }
87
Zachary Turner1dc9fd32016-06-14 20:48:36 +000088private:
89 std::unique_ptr<FileOutputBuffer> FileBuffer;
90};
91}
92
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000093namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000094
Zachary Turnera30bd1a2016-06-30 17:42:48 +000095cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
96cl::SubCommand
97 PrettySubcommand("pretty",
98 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000099cl::SubCommand
100 YamlToPdbSubcommand("yaml2pdb",
101 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000102cl::SubCommand
103 PdbToYamlSubcommand("pdb2yaml",
104 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000105
Zachary Turner7797c722015-03-02 04:39:56 +0000106cl::OptionCategory TypeCategory("Symbol Type Options");
107cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +0000108cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000109
110namespace pretty {
111cl::list<std::string> InputFilenames(cl::Positional,
112 cl::desc("<input PDB files>"),
113 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000114
115cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000116 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000117cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000118 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000119cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000120 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000121cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000122 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
123cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
124 cl::sub(PrettySubcommand));
125cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
126 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000127cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000128 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000129 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000130
Zachary Turnere5cb2692015-05-01 20:24:26 +0000131cl::opt<uint64_t> LoadAddress(
132 "load-address",
133 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000134 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
135cl::list<std::string> ExcludeTypes(
136 "exclude-types", cl::desc("Exclude types by regular expression"),
137 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
138cl::list<std::string> ExcludeSymbols(
139 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
140 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
141cl::list<std::string> ExcludeCompilands(
142 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
143 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000144
145cl::list<std::string> IncludeTypes(
146 "include-types",
147 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000148 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000149cl::list<std::string> IncludeSymbols(
150 "include-symbols",
151 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000152 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000153cl::list<std::string> IncludeCompilands(
154 "include-compilands",
155 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000156 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000157
Zachary Turner7797c722015-03-02 04:39:56 +0000158cl::opt<bool> ExcludeCompilerGenerated(
159 "no-compiler-generated",
160 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000161 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000162cl::opt<bool>
163 ExcludeSystemLibraries("no-system-libs",
164 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000165 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000166cl::opt<bool> NoClassDefs("no-class-definitions",
167 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000168 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000169cl::opt<bool> NoEnumDefs("no-enum-definitions",
170 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000171 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
172}
173
174namespace raw {
175
176cl::OptionCategory MsfOptions("MSF Container Options");
177cl::OptionCategory TypeOptions("Type Record Options");
178cl::OptionCategory FileOptions("Module & File Options");
179cl::OptionCategory SymbolOptions("Symbol Options");
180cl::OptionCategory MiscOptions("Miscellaneous Options");
181
182// MSF OPTIONS
183cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
184 cl::cat(MsfOptions), cl::sub(RawSubcommand));
185cl::opt<bool> DumpStreamBlocks("stream-blocks",
186 cl::desc("dump PDB stream blocks"),
187 cl::cat(MsfOptions), cl::sub(RawSubcommand));
188cl::opt<bool> DumpStreamSummary("stream-summary",
189 cl::desc("dump summary of the PDB streams"),
190 cl::cat(MsfOptions), cl::sub(RawSubcommand));
191
192// TYPE OPTIONS
193cl::opt<bool>
194 DumpTpiRecords("tpi-records",
195 cl::desc("dump CodeView type records from TPI stream"),
196 cl::cat(TypeOptions), cl::sub(RawSubcommand));
197cl::opt<bool> DumpTpiRecordBytes(
198 "tpi-record-bytes",
199 cl::desc("dump CodeView type record raw bytes from TPI stream"),
200 cl::cat(TypeOptions), cl::sub(RawSubcommand));
201cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
202 cl::cat(TypeOptions), cl::sub(RawSubcommand));
203cl::opt<bool>
204 DumpIpiRecords("ipi-records",
205 cl::desc("dump CodeView type records from IPI stream"),
206 cl::cat(TypeOptions), cl::sub(RawSubcommand));
207cl::opt<bool> DumpIpiRecordBytes(
208 "ipi-record-bytes",
209 cl::desc("dump CodeView type record raw bytes from IPI stream"),
210 cl::cat(TypeOptions), cl::sub(RawSubcommand));
211
212// MODULE & FILE OPTIONS
213cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
214 cl::cat(FileOptions), cl::sub(RawSubcommand));
215cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
216 cl::cat(FileOptions), cl::sub(RawSubcommand));
217cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000218 cl::desc("dump file and line information"),
219 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000220
221// SYMBOL OPTIONS
222cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
223 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
224cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
225 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
226cl::opt<bool>
227 DumpSymRecordBytes("sym-record-bytes",
228 cl::desc("dump CodeView symbol record raw bytes"),
229 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
230
231// MISCELLANEOUS OPTIONS
232cl::opt<bool> DumpSectionContribs("section-contribs",
233 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000234 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000235cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
236 cl::cat(MiscOptions), cl::sub(RawSubcommand));
237cl::opt<bool> DumpSectionHeaders("section-headers",
238 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000239 cl::cat(MiscOptions), cl::sub(RawSubcommand));
240cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
241 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000242
243cl::opt<std::string> DumpStreamDataIdx("stream", cl::desc("dump stream data"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000244 cl::cat(MiscOptions),
245 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000246cl::opt<std::string> DumpStreamDataName("stream-name",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000247 cl::desc("dump stream data"),
248 cl::cat(MiscOptions),
249 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000250
Zachary Turnerab58ae82016-06-30 17:43:00 +0000251cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
252 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000253
254cl::list<std::string> InputFilenames(cl::Positional,
255 cl::desc("<input PDB files>"),
256 cl::OneOrMore, cl::sub(RawSubcommand));
257}
258
259namespace yaml2pdb {
260cl::opt<std::string>
261 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
262 cl::sub(YamlToPdbSubcommand));
263
264cl::list<std::string> InputFilename(cl::Positional,
265 cl::desc("<input YAML file>"), cl::Required,
266 cl::sub(YamlToPdbSubcommand));
267}
268
269namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000270cl::opt<bool>
271 NoFileHeaders("no-file-headers",
272 cl::desc("Do not dump MSF file headers (you will not be able "
273 "to generate a fresh PDB from the resulting YAML)"),
274 cl::sub(PdbToYamlSubcommand), cl::init(false));
275
Zachary Turnerab58ae82016-06-30 17:43:00 +0000276cl::opt<bool> StreamMetadata(
277 "stream-metadata",
278 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000279 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000280cl::opt<bool> StreamDirectory(
281 "stream-directory",
282 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000283 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000284cl::opt<bool> PdbStream("pdb-stream",
285 cl::desc("Dump the PDB Stream (Stream 1)"),
286 cl::sub(PdbToYamlSubcommand), cl::init(false));
287cl::opt<bool> DbiStream("dbi-stream",
288 cl::desc("Dump the DBI Stream (Stream 2)"),
289 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000290cl::opt<bool>
291 DbiModuleInfo("dbi-module-info",
292 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
293 cl::sub(PdbToYamlSubcommand), cl::init(false));
294
295cl::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
301cl::list<std::string> InputFilename(cl::Positional,
302 cl::desc("<input PDB file>"), cl::Required,
303 cl::sub(PdbToYamlSubcommand));
304}
Zachary Turner49693b42015-01-28 01:22:33 +0000305}
306
David Majnemerc165c882016-05-28 18:25:15 +0000307static ExitOnError ExitOnErr;
308
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000309static void yamlToPdb(StringRef Path) {
310 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
311 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
312 /*RequiresNullTerminator=*/false);
313
314 if (ErrorOrBuffer.getError()) {
315 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
316 }
317
318 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
319
320 llvm::yaml::Input In(Buffer->getBuffer());
321 pdb::yaml::PdbObject YamlObj;
322 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000323 if (!YamlObj.Headers.hasValue())
324 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
325 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000326
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000327 auto OutFileOrError = FileOutputBuffer::create(
Zachary Turnerf6b93822016-07-11 21:45:09 +0000328 opts::yaml2pdb::YamlPdbOutputFile, YamlObj.Headers->FileSize);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000329 if (OutFileOrError.getError())
330 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path,
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000331 opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000332
333 auto FileByteStream =
334 llvm::make_unique<FileBufferByteStream>(std::move(*OutFileOrError));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000335 PDBFileBuilder Builder(std::move(FileByteStream));
336
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000337 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock));
338 ExitOnErr(Builder.getMsfBuilder().setDirectoryBlocksHint(
339 YamlObj.Headers->DirectoryBlocks));
340 if (!YamlObj.StreamSizes.hasValue()) {
341 ExitOnErr(make_error<GenericError>(
342 generic_error_code::unspecified,
343 "Cannot generate a PDB when stream sizes are not known"));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000344 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000345
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000346 if (YamlObj.StreamMap.hasValue()) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000347 if (YamlObj.StreamMap->size() != YamlObj.StreamSizes->size()) {
348 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
349 "YAML specifies different number of "
350 "streams in stream sizes and stream "
351 "map"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000352 }
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000353
354 auto &Sizes = *YamlObj.StreamSizes;
355 auto &Map = *YamlObj.StreamMap;
356 for (uint32_t I = 0; I < Sizes.size(); ++I) {
357 uint32_t Size = Sizes[I];
358 std::vector<uint32_t> Blocks;
359 for (auto E : Map[I].Blocks)
360 Blocks.push_back(E);
361 ExitOnErr(Builder.getMsfBuilder().addStream(Size, Blocks));
362 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000363 } else {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000364 auto &Sizes = *YamlObj.StreamSizes;
365 for (auto S : Sizes) {
366 ExitOnErr(Builder.getMsfBuilder().addStream(S));
367 }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000368 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000369
370 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000371 auto &InfoBuilder = Builder.getInfoBuilder();
372 InfoBuilder.setAge(YamlObj.PdbStream->Age);
373 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
374 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
375 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner5e534c72016-07-15 22:17:08 +0000376 for (auto &NM : YamlObj.PdbStream->NamedStreams)
377 InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
378 NM.StreamNumber);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000379 }
380
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000381 if (YamlObj.DbiStream.hasValue()) {
382 auto &DbiBuilder = Builder.getDbiBuilder();
383 DbiBuilder.setAge(YamlObj.DbiStream->Age);
384 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
385 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
386 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
387 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
388 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
389 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000390 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
391 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
392 for (auto S : MI.SourceFiles)
393 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
394 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000395 }
396
397 auto Pdb = Builder.build();
398 ExitOnErr(Pdb.takeError());
399
400 auto &PdbFile = *Pdb;
401 ExitOnErr(PdbFile->commit());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000402}
403
Zachary Turner8848a7a2016-07-06 18:05:57 +0000404static void pdb2Yaml(StringRef Path) {
405 std::unique_ptr<IPDBSession> Session;
406 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
407
408 RawSession *RS = static_cast<RawSession *>(Session.get());
409 PDBFile &File = RS->getPDBFile();
410 auto O = llvm::make_unique<YAMLOutputStyle>(File);
411 O = llvm::make_unique<YAMLOutputStyle>(File);
412
413 ExitOnErr(O->dump());
414}
415
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000416static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000417 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000418 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000419
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000420 RawSession *RS = static_cast<RawSession *>(Session.get());
421 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000422 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000423
424 ExitOnErr(O->dump());
425}
426
427static void dumpPretty(StringRef Path) {
428 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000429
David Majnemerc165c882016-05-28 18:25:15 +0000430 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000431
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000432 if (opts::pretty::LoadAddress)
433 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000434
Zachary Turner2d11c202015-02-27 09:15:59 +0000435 LinePrinter Printer(2, outs());
436
Zachary Turnera5549172015-02-10 22:43:25 +0000437 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000438 std::string FileName(GlobalScope->getSymbolsFileName());
439
Zachary Turner2d11c202015-02-27 09:15:59 +0000440 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
441 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
442 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000443 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000444
Zachary Turner2d11c202015-02-27 09:15:59 +0000445 Printer.NewLine();
446 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000447 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000448 Printer << ": " << FileSize << " bytes";
449 } else {
450 Printer << ": (Unable to obtain file size)";
451 }
452
453 Printer.NewLine();
454 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
455 Printer << ": " << GlobalScope->getGuid();
456
457 Printer.NewLine();
458 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
459 Printer << ": " << GlobalScope->getAge();
460
461 Printer.NewLine();
462 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
463 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000464 if (GlobalScope->hasCTypes())
465 outs() << "HasCTypes ";
466 if (GlobalScope->hasPrivateSymbols())
467 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000468 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000469
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000470 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000471 Printer.NewLine();
472 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
473 << "---COMPILANDS---";
474 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000475 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000476 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000477 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000478 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000479 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000480 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000481 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000482 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000483 }
484
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000485 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000486 Printer.NewLine();
487 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
488 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000489 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000490 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000491 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000492 }
493
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000494 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000495 Printer.NewLine();
496 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
497 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000498 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000499 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000500 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000501 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000502 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000503 }
504
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000505 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000506 Printer.NewLine();
507 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
508 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000509 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000510 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000511 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000512 while (auto Function = Functions->getNext()) {
513 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000514 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000515 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000516 }
517 {
518 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000519 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000520 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000521 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000522 }
523 {
524 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000525 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000526 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000527 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000528 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000529 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000530 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000531 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000532 Printer.NewLine();
533 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
534 Printer.Indent();
535 ExternalSymbolDumper Dumper(Printer);
536 Dumper.start(*GlobalScope);
537 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000538 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000539 Printer.NewLine();
540 }
Zachary Turnera5549172015-02-10 22:43:25 +0000541 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000542}
543
544int main(int argc_, const char *argv_[]) {
545 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000546 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000547 PrettyStackTraceProgram X(argc_, argv_);
548
David Majnemerc165c882016-05-28 18:25:15 +0000549 ExitOnErr.setBanner("llvm-pdbdump: ");
550
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000551 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000552 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000553 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
554 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000555
556 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
557
558 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000559
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000560 // These options are shared by two subcommands.
561 if ((opts::PdbToYamlSubcommand || opts::RawSubcommand) && opts::raw::RawAll) {
562 opts::raw::DumpHeaders = true;
563 opts::raw::DumpModules = true;
564 opts::raw::DumpModuleFiles = true;
565 opts::raw::DumpModuleSyms = true;
566 opts::raw::DumpPublics = true;
567 opts::raw::DumpSectionHeaders = true;
568 opts::raw::DumpStreamSummary = true;
569 opts::raw::DumpStreamBlocks = true;
570 opts::raw::DumpTpiRecords = true;
571 opts::raw::DumpTpiHash = true;
572 opts::raw::DumpIpiRecords = true;
573 opts::raw::DumpSectionMap = true;
574 opts::raw::DumpSectionContribs = true;
575 opts::raw::DumpLineInfo = true;
576 opts::raw::DumpFpo = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000577 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000578
Zachary Turner93839cb2016-06-02 05:07:49 +0000579 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000580
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000581 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000582 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000583 } else if (opts::YamlToPdbSubcommand) {
584 yamlToPdb(opts::yaml2pdb::InputFilename.front());
585 } else if (opts::PrettySubcommand) {
586 if (opts::pretty::Lines)
587 opts::pretty::Compilands = true;
588
589 if (opts::pretty::All) {
590 opts::pretty::Compilands = true;
591 opts::pretty::Symbols = true;
592 opts::pretty::Globals = true;
593 opts::pretty::Types = true;
594 opts::pretty::Externals = true;
595 opts::pretty::Lines = true;
596 }
597
598 // When adding filters for excluded compilands and types, we need to
599 // remember
600 // that these are regexes. So special characters such as * and \ need to be
601 // escaped in the regex. In the case of a literal \, this means it needs to
602 // be escaped again in the C++. So matching a single \ in the input
603 // requires
604 // 4 \es in the C++.
605 if (opts::pretty::ExcludeCompilerGenerated) {
606 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
607 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
608 }
609 if (opts::pretty::ExcludeSystemLibraries) {
610 opts::pretty::ExcludeCompilands.push_back(
611 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
612 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
613 opts::pretty::ExcludeCompilands.push_back(
614 "d:\\\\th.obj.x86fre\\\\minkernel");
615 }
616 std::for_each(opts::pretty::InputFilenames.begin(),
617 opts::pretty::InputFilenames.end(), dumpPretty);
618 } else if (opts::RawSubcommand) {
619 std::for_each(opts::raw::InputFilenames.begin(),
620 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000621 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000622
Zachary Turner819e77d2016-05-06 20:51:57 +0000623 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000624 return 0;
625}