blob: 0600bcd952c2fd9f43d3dd0877c9018bd0f173d2 [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 Turner1dc9fd32016-06-14 20:48:36 +000032#include "llvm/DebugInfo/CodeView/ByteStream.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000033#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000034#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000035#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000036#include "llvm/DebugInfo/PDB/IPDBSession.h"
37#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000038#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000039#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000040#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000041#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
42#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000043#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
44#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
Zachary Turner8848a7a2016-07-06 18:05:57 +000045#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000046#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000047#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turnerdbeaea72016-07-11 21:45:26 +000048#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000049#include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
Reid Klecknerce5196e2016-05-12 23:26:23 +000050#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000051#include "llvm/DebugInfo/PDB/Raw/RawSession.h"
Zachary Turner93839cb2016-06-02 05:07:49 +000052#include "llvm/Support/COM.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000053#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/ConvertUTF.h"
Zachary Turner1dc9fd32016-06-14 20:48:36 +000055#include "llvm/Support/FileOutputBuffer.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000056#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000057#include "llvm/Support/Format.h"
58#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000059#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000060#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000061#include "llvm/Support/Process.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000062#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000063#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000064#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000065
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000066using namespace llvm;
Zachary Turnera96cce62016-06-03 03:25:59 +000067using namespace llvm::codeview;
Zachary Turner2f09b502016-04-29 17:28:47 +000068using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000069
Zachary Turner1dc9fd32016-06-14 20:48:36 +000070namespace {
71// A simple adapter that acts like a ByteStream but holds ownership over
72// and underlying FileOutputBuffer.
73class FileBufferByteStream : public ByteStream<true> {
74public:
75 FileBufferByteStream(std::unique_ptr<FileOutputBuffer> Buffer)
76 : ByteStream(MutableArrayRef<uint8_t>(Buffer->getBufferStart(),
77 Buffer->getBufferEnd())),
78 FileBuffer(std::move(Buffer)) {}
79
Zachary Turnerab58ae82016-06-30 17:43:00 +000080 Error commit() const override {
81 if (FileBuffer->commit())
82 return llvm::make_error<RawError>(raw_error_code::not_writable);
83 return Error::success();
84 }
85
Zachary Turner1dc9fd32016-06-14 20:48:36 +000086private:
87 std::unique_ptr<FileOutputBuffer> FileBuffer;
88};
89}
90
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000091namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000092
Zachary Turnera30bd1a2016-06-30 17:42:48 +000093cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
94cl::SubCommand
95 PrettySubcommand("pretty",
96 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000097cl::SubCommand
98 YamlToPdbSubcommand("yaml2pdb",
99 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000100cl::SubCommand
101 PdbToYamlSubcommand("pdb2yaml",
102 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000103
Zachary Turner7797c722015-03-02 04:39:56 +0000104cl::OptionCategory TypeCategory("Symbol Type Options");
105cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +0000106cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000107
108namespace pretty {
109cl::list<std::string> InputFilenames(cl::Positional,
110 cl::desc("<input PDB files>"),
111 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000112
113cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000114 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000115cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000116 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000117cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000118 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000119cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000120 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
121cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
122 cl::sub(PrettySubcommand));
123cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
124 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000125cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000126 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000127 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000128
Zachary Turnere5cb2692015-05-01 20:24:26 +0000129cl::opt<uint64_t> LoadAddress(
130 "load-address",
131 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000132 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
133cl::list<std::string> ExcludeTypes(
134 "exclude-types", cl::desc("Exclude types by regular expression"),
135 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
136cl::list<std::string> ExcludeSymbols(
137 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
138 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
139cl::list<std::string> ExcludeCompilands(
140 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
141 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000142
143cl::list<std::string> IncludeTypes(
144 "include-types",
145 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000146 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000147cl::list<std::string> IncludeSymbols(
148 "include-symbols",
149 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000150 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000151cl::list<std::string> IncludeCompilands(
152 "include-compilands",
153 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000154 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000155
Zachary Turner7797c722015-03-02 04:39:56 +0000156cl::opt<bool> ExcludeCompilerGenerated(
157 "no-compiler-generated",
158 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000159 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000160cl::opt<bool>
161 ExcludeSystemLibraries("no-system-libs",
162 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000163 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000164cl::opt<bool> NoClassDefs("no-class-definitions",
165 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000166 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000167cl::opt<bool> NoEnumDefs("no-enum-definitions",
168 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000169 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
170}
171
172namespace raw {
173
174cl::OptionCategory MsfOptions("MSF Container Options");
175cl::OptionCategory TypeOptions("Type Record Options");
176cl::OptionCategory FileOptions("Module & File Options");
177cl::OptionCategory SymbolOptions("Symbol Options");
178cl::OptionCategory MiscOptions("Miscellaneous Options");
179
180// MSF OPTIONS
181cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
182 cl::cat(MsfOptions), cl::sub(RawSubcommand));
183cl::opt<bool> DumpStreamBlocks("stream-blocks",
184 cl::desc("dump PDB stream blocks"),
185 cl::cat(MsfOptions), cl::sub(RawSubcommand));
186cl::opt<bool> DumpStreamSummary("stream-summary",
187 cl::desc("dump summary of the PDB streams"),
188 cl::cat(MsfOptions), cl::sub(RawSubcommand));
189
190// TYPE OPTIONS
191cl::opt<bool>
192 DumpTpiRecords("tpi-records",
193 cl::desc("dump CodeView type records from TPI stream"),
194 cl::cat(TypeOptions), cl::sub(RawSubcommand));
195cl::opt<bool> DumpTpiRecordBytes(
196 "tpi-record-bytes",
197 cl::desc("dump CodeView type record raw bytes from TPI stream"),
198 cl::cat(TypeOptions), cl::sub(RawSubcommand));
199cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
200 cl::cat(TypeOptions), cl::sub(RawSubcommand));
201cl::opt<bool>
202 DumpIpiRecords("ipi-records",
203 cl::desc("dump CodeView type records from IPI stream"),
204 cl::cat(TypeOptions), cl::sub(RawSubcommand));
205cl::opt<bool> DumpIpiRecordBytes(
206 "ipi-record-bytes",
207 cl::desc("dump CodeView type record raw bytes from IPI stream"),
208 cl::cat(TypeOptions), cl::sub(RawSubcommand));
209
210// MODULE & FILE OPTIONS
211cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
212 cl::cat(FileOptions), cl::sub(RawSubcommand));
213cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
214 cl::cat(FileOptions), cl::sub(RawSubcommand));
215cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000216 cl::desc("dump file and line information"),
217 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000218
219// SYMBOL OPTIONS
220cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
221 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
222cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
223 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
224cl::opt<bool>
225 DumpSymRecordBytes("sym-record-bytes",
226 cl::desc("dump CodeView symbol record raw bytes"),
227 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
228
229// MISCELLANEOUS OPTIONS
230cl::opt<bool> DumpSectionContribs("section-contribs",
231 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000232 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000233cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
234 cl::cat(MiscOptions), cl::sub(RawSubcommand));
235cl::opt<bool> DumpSectionHeaders("section-headers",
236 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000237 cl::cat(MiscOptions), cl::sub(RawSubcommand));
238cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
239 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000240
241cl::opt<std::string> DumpStreamDataIdx("stream", cl::desc("dump stream data"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000242 cl::cat(MiscOptions),
243 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000244cl::opt<std::string> DumpStreamDataName("stream-name",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000245 cl::desc("dump stream data"),
246 cl::cat(MiscOptions),
247 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000248
Zachary Turnerab58ae82016-06-30 17:43:00 +0000249cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
250 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000251
252cl::list<std::string> InputFilenames(cl::Positional,
253 cl::desc("<input PDB files>"),
254 cl::OneOrMore, cl::sub(RawSubcommand));
255}
256
257namespace yaml2pdb {
258cl::opt<std::string>
259 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
260 cl::sub(YamlToPdbSubcommand));
261
262cl::list<std::string> InputFilename(cl::Positional,
263 cl::desc("<input YAML file>"), cl::Required,
264 cl::sub(YamlToPdbSubcommand));
265}
266
267namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000268cl::opt<bool>
269 NoFileHeaders("no-file-headers",
270 cl::desc("Do not dump MSF file headers (you will not be able "
271 "to generate a fresh PDB from the resulting YAML)"),
272 cl::sub(PdbToYamlSubcommand), cl::init(false));
273
Zachary Turnerab58ae82016-06-30 17:43:00 +0000274cl::opt<bool> StreamMetadata(
275 "stream-metadata",
276 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000277 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000278cl::opt<bool> StreamDirectory(
279 "stream-directory",
280 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000281 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000282cl::opt<bool> PdbStream("pdb-stream",
283 cl::desc("Dump the PDB Stream (Stream 1)"),
284 cl::sub(PdbToYamlSubcommand), cl::init(false));
285cl::opt<bool> DbiStream("dbi-stream",
286 cl::desc("Dump the DBI Stream (Stream 2)"),
287 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000288cl::opt<bool>
289 DbiModuleInfo("dbi-module-info",
290 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
291 cl::sub(PdbToYamlSubcommand), cl::init(false));
292
293cl::opt<bool> DbiModuleSourceFileInfo(
294 "dbi-module-source-info",
295 cl::desc(
296 "Dump DBI Module Source File Information (implies -dbi-module-info"),
297 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000298
299cl::list<std::string> InputFilename(cl::Positional,
300 cl::desc("<input PDB file>"), cl::Required,
301 cl::sub(PdbToYamlSubcommand));
302}
Zachary Turner49693b42015-01-28 01:22:33 +0000303}
304
David Majnemerc165c882016-05-28 18:25:15 +0000305static ExitOnError ExitOnErr;
306
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000307static void yamlToPdb(StringRef Path) {
308 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
309 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
310 /*RequiresNullTerminator=*/false);
311
312 if (ErrorOrBuffer.getError()) {
313 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
314 }
315
316 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
317
318 llvm::yaml::Input In(Buffer->getBuffer());
319 pdb::yaml::PdbObject YamlObj;
320 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000321 if (!YamlObj.Headers.hasValue())
322 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
323 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000324
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000325 auto OutFileOrError = FileOutputBuffer::create(
Zachary Turnerf6b93822016-07-11 21:45:09 +0000326 opts::yaml2pdb::YamlPdbOutputFile, YamlObj.Headers->FileSize);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000327 if (OutFileOrError.getError())
328 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path,
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000329 opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000330
331 auto FileByteStream =
332 llvm::make_unique<FileBufferByteStream>(std::move(*OutFileOrError));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000333 PDBFileBuilder Builder(std::move(FileByteStream));
334
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000335 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock));
336 ExitOnErr(Builder.getMsfBuilder().setDirectoryBlocksHint(
337 YamlObj.Headers->DirectoryBlocks));
338 if (!YamlObj.StreamSizes.hasValue()) {
339 ExitOnErr(make_error<GenericError>(
340 generic_error_code::unspecified,
341 "Cannot generate a PDB when stream sizes are not known"));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000342 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000343
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000344 if (YamlObj.StreamMap.hasValue()) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000345 if (YamlObj.StreamMap->size() != YamlObj.StreamSizes->size()) {
346 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
347 "YAML specifies different number of "
348 "streams in stream sizes and stream "
349 "map"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000350 }
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000351
352 auto &Sizes = *YamlObj.StreamSizes;
353 auto &Map = *YamlObj.StreamMap;
354 for (uint32_t I = 0; I < Sizes.size(); ++I) {
355 uint32_t Size = Sizes[I];
356 std::vector<uint32_t> Blocks;
357 for (auto E : Map[I].Blocks)
358 Blocks.push_back(E);
359 ExitOnErr(Builder.getMsfBuilder().addStream(Size, Blocks));
360 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000361 } else {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000362 auto &Sizes = *YamlObj.StreamSizes;
363 for (auto S : Sizes) {
364 ExitOnErr(Builder.getMsfBuilder().addStream(S));
365 }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000366 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000367
368 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000369 auto &InfoBuilder = Builder.getInfoBuilder();
370 InfoBuilder.setAge(YamlObj.PdbStream->Age);
371 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
372 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
373 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner5e534c72016-07-15 22:17:08 +0000374 for (auto &NM : YamlObj.PdbStream->NamedStreams)
375 InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
376 NM.StreamNumber);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000377 }
378
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000379 if (YamlObj.DbiStream.hasValue()) {
380 auto &DbiBuilder = Builder.getDbiBuilder();
381 DbiBuilder.setAge(YamlObj.DbiStream->Age);
382 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
383 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
384 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
385 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
386 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
387 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000388 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
389 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
390 for (auto S : MI.SourceFiles)
391 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
392 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000393 }
394
395 auto Pdb = Builder.build();
396 ExitOnErr(Pdb.takeError());
397
398 auto &PdbFile = *Pdb;
399 ExitOnErr(PdbFile->commit());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000400}
401
Zachary Turner8848a7a2016-07-06 18:05:57 +0000402static void pdb2Yaml(StringRef Path) {
403 std::unique_ptr<IPDBSession> Session;
404 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
405
406 RawSession *RS = static_cast<RawSession *>(Session.get());
407 PDBFile &File = RS->getPDBFile();
408 auto O = llvm::make_unique<YAMLOutputStyle>(File);
409 O = llvm::make_unique<YAMLOutputStyle>(File);
410
411 ExitOnErr(O->dump());
412}
413
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000414static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000415 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000416 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000417
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000418 RawSession *RS = static_cast<RawSession *>(Session.get());
419 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000420 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000421
422 ExitOnErr(O->dump());
423}
424
425static void dumpPretty(StringRef Path) {
426 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000427
David Majnemerc165c882016-05-28 18:25:15 +0000428 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000429
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000430 if (opts::pretty::LoadAddress)
431 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000432
Zachary Turner2d11c202015-02-27 09:15:59 +0000433 LinePrinter Printer(2, outs());
434
Zachary Turnera5549172015-02-10 22:43:25 +0000435 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000436 std::string FileName(GlobalScope->getSymbolsFileName());
437
Zachary Turner2d11c202015-02-27 09:15:59 +0000438 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
439 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
440 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000441 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000442
Zachary Turner2d11c202015-02-27 09:15:59 +0000443 Printer.NewLine();
444 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000445 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000446 Printer << ": " << FileSize << " bytes";
447 } else {
448 Printer << ": (Unable to obtain file size)";
449 }
450
451 Printer.NewLine();
452 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
453 Printer << ": " << GlobalScope->getGuid();
454
455 Printer.NewLine();
456 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
457 Printer << ": " << GlobalScope->getAge();
458
459 Printer.NewLine();
460 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
461 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000462 if (GlobalScope->hasCTypes())
463 outs() << "HasCTypes ";
464 if (GlobalScope->hasPrivateSymbols())
465 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000466 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000467
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000468 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000469 Printer.NewLine();
470 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
471 << "---COMPILANDS---";
472 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000473 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000474 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000475 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000476 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000477 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000478 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000479 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000480 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000481 }
482
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000483 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000484 Printer.NewLine();
485 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
486 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000487 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000488 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000489 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000490 }
491
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000492 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000493 Printer.NewLine();
494 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
495 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000496 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000497 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000498 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000499 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000500 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000501 }
502
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000503 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000504 Printer.NewLine();
505 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
506 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000507 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000508 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000509 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000510 while (auto Function = Functions->getNext()) {
511 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000512 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000513 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000514 }
515 {
516 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000517 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000518 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000519 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000520 }
521 {
522 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000523 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000524 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000525 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000526 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000527 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000528 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000529 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000530 Printer.NewLine();
531 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
532 Printer.Indent();
533 ExternalSymbolDumper Dumper(Printer);
534 Dumper.start(*GlobalScope);
535 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000536 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000537 Printer.NewLine();
538 }
Zachary Turnera5549172015-02-10 22:43:25 +0000539 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000540}
541
542int main(int argc_, const char *argv_[]) {
543 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000544 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000545 PrettyStackTraceProgram X(argc_, argv_);
546
David Majnemerc165c882016-05-28 18:25:15 +0000547 ExitOnErr.setBanner("llvm-pdbdump: ");
548
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000549 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000550 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000551 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
552 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000553
554 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
555
556 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000557
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000558 // These options are shared by two subcommands.
559 if ((opts::PdbToYamlSubcommand || opts::RawSubcommand) && opts::raw::RawAll) {
560 opts::raw::DumpHeaders = true;
561 opts::raw::DumpModules = true;
562 opts::raw::DumpModuleFiles = true;
563 opts::raw::DumpModuleSyms = true;
564 opts::raw::DumpPublics = true;
565 opts::raw::DumpSectionHeaders = true;
566 opts::raw::DumpStreamSummary = true;
567 opts::raw::DumpStreamBlocks = true;
568 opts::raw::DumpTpiRecords = true;
569 opts::raw::DumpTpiHash = true;
570 opts::raw::DumpIpiRecords = true;
571 opts::raw::DumpSectionMap = true;
572 opts::raw::DumpSectionContribs = true;
573 opts::raw::DumpLineInfo = true;
574 opts::raw::DumpFpo = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000575 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000576
Zachary Turner93839cb2016-06-02 05:07:49 +0000577 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000578
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000579 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000580 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000581 } else if (opts::YamlToPdbSubcommand) {
582 yamlToPdb(opts::yaml2pdb::InputFilename.front());
583 } else if (opts::PrettySubcommand) {
584 if (opts::pretty::Lines)
585 opts::pretty::Compilands = true;
586
587 if (opts::pretty::All) {
588 opts::pretty::Compilands = true;
589 opts::pretty::Symbols = true;
590 opts::pretty::Globals = true;
591 opts::pretty::Types = true;
592 opts::pretty::Externals = true;
593 opts::pretty::Lines = true;
594 }
595
596 // When adding filters for excluded compilands and types, we need to
597 // remember
598 // that these are regexes. So special characters such as * and \ need to be
599 // escaped in the regex. In the case of a literal \, this means it needs to
600 // be escaped again in the C++. So matching a single \ in the input
601 // requires
602 // 4 \es in the C++.
603 if (opts::pretty::ExcludeCompilerGenerated) {
604 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
605 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
606 }
607 if (opts::pretty::ExcludeSystemLibraries) {
608 opts::pretty::ExcludeCompilands.push_back(
609 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
610 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
611 opts::pretty::ExcludeCompilands.push_back(
612 "d:\\\\th.obj.x86fre\\\\minkernel");
613 }
614 std::for_each(opts::pretty::InputFilenames.begin(),
615 opts::pretty::InputFilenames.end(), dumpPretty);
616 } else if (opts::RawSubcommand) {
617 std::for_each(opts::raw::InputFilenames.begin(),
618 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000619 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000620
Zachary Turner819e77d2016-05-06 20:51:57 +0000621 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000622 return 0;
623}