blob: 62f215ddc8aef4f7a6bbd5b822a2fec45084eaa4 [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 Turnera30bd1a2016-06-30 17:42:48 +0000288
289cl::list<std::string> InputFilename(cl::Positional,
290 cl::desc("<input PDB file>"), cl::Required,
291 cl::sub(PdbToYamlSubcommand));
292}
Zachary Turner49693b42015-01-28 01:22:33 +0000293}
294
David Majnemerc165c882016-05-28 18:25:15 +0000295static ExitOnError ExitOnErr;
296
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000297static void yamlToPdb(StringRef Path) {
298 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
299 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
300 /*RequiresNullTerminator=*/false);
301
302 if (ErrorOrBuffer.getError()) {
303 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
304 }
305
306 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
307
308 llvm::yaml::Input In(Buffer->getBuffer());
309 pdb::yaml::PdbObject YamlObj;
310 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000311 if (!YamlObj.Headers.hasValue())
312 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
313 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000314
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000315 auto OutFileOrError = FileOutputBuffer::create(
Zachary Turnerf6b93822016-07-11 21:45:09 +0000316 opts::yaml2pdb::YamlPdbOutputFile, YamlObj.Headers->FileSize);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000317 if (OutFileOrError.getError())
318 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path,
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000319 opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000320
321 auto FileByteStream =
322 llvm::make_unique<FileBufferByteStream>(std::move(*OutFileOrError));
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000323 PDBFileBuilder Builder(std::move(FileByteStream));
324
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000325 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock));
326 ExitOnErr(Builder.getMsfBuilder().setDirectoryBlocksHint(
327 YamlObj.Headers->DirectoryBlocks));
328 if (!YamlObj.StreamSizes.hasValue()) {
329 ExitOnErr(make_error<GenericError>(
330 generic_error_code::unspecified,
331 "Cannot generate a PDB when stream sizes are not known"));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000332 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000333
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000334 if (YamlObj.StreamMap.hasValue()) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000335 if (YamlObj.StreamMap->size() != YamlObj.StreamSizes->size()) {
336 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
337 "YAML specifies different number of "
338 "streams in stream sizes and stream "
339 "map"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000340 }
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000341
342 auto &Sizes = *YamlObj.StreamSizes;
343 auto &Map = *YamlObj.StreamMap;
344 for (uint32_t I = 0; I < Sizes.size(); ++I) {
345 uint32_t Size = Sizes[I];
346 std::vector<uint32_t> Blocks;
347 for (auto E : Map[I].Blocks)
348 Blocks.push_back(E);
349 ExitOnErr(Builder.getMsfBuilder().addStream(Size, Blocks));
350 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000351 } else {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000352 auto &Sizes = *YamlObj.StreamSizes;
353 for (auto S : Sizes) {
354 ExitOnErr(Builder.getMsfBuilder().addStream(S));
355 }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000356 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000357
358 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000359 auto &InfoBuilder = Builder.getInfoBuilder();
360 InfoBuilder.setAge(YamlObj.PdbStream->Age);
361 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
362 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
363 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner5e534c72016-07-15 22:17:08 +0000364 for (auto &NM : YamlObj.PdbStream->NamedStreams)
365 InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
366 NM.StreamNumber);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000367 }
368
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000369 if (YamlObj.DbiStream.hasValue()) {
370 auto &DbiBuilder = Builder.getDbiBuilder();
371 DbiBuilder.setAge(YamlObj.DbiStream->Age);
372 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
373 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
374 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
375 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
376 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
377 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
378 }
379
380 auto Pdb = Builder.build();
381 ExitOnErr(Pdb.takeError());
382
383 auto &PdbFile = *Pdb;
384 ExitOnErr(PdbFile->commit());
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000385}
386
Zachary Turner8848a7a2016-07-06 18:05:57 +0000387static void pdb2Yaml(StringRef Path) {
388 std::unique_ptr<IPDBSession> Session;
389 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
390
391 RawSession *RS = static_cast<RawSession *>(Session.get());
392 PDBFile &File = RS->getPDBFile();
393 auto O = llvm::make_unique<YAMLOutputStyle>(File);
394 O = llvm::make_unique<YAMLOutputStyle>(File);
395
396 ExitOnErr(O->dump());
397}
398
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000399static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000400 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000401 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000402
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000403 RawSession *RS = static_cast<RawSession *>(Session.get());
404 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000405 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000406
407 ExitOnErr(O->dump());
408}
409
410static void dumpPretty(StringRef Path) {
411 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000412
David Majnemerc165c882016-05-28 18:25:15 +0000413 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000414
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000415 if (opts::pretty::LoadAddress)
416 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000417
Zachary Turner2d11c202015-02-27 09:15:59 +0000418 LinePrinter Printer(2, outs());
419
Zachary Turnera5549172015-02-10 22:43:25 +0000420 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000421 std::string FileName(GlobalScope->getSymbolsFileName());
422
Zachary Turner2d11c202015-02-27 09:15:59 +0000423 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
424 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
425 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000426 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000427
Zachary Turner2d11c202015-02-27 09:15:59 +0000428 Printer.NewLine();
429 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000430 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000431 Printer << ": " << FileSize << " bytes";
432 } else {
433 Printer << ": (Unable to obtain file size)";
434 }
435
436 Printer.NewLine();
437 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
438 Printer << ": " << GlobalScope->getGuid();
439
440 Printer.NewLine();
441 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
442 Printer << ": " << GlobalScope->getAge();
443
444 Printer.NewLine();
445 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
446 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000447 if (GlobalScope->hasCTypes())
448 outs() << "HasCTypes ";
449 if (GlobalScope->hasPrivateSymbols())
450 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000451 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000452
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000453 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000454 Printer.NewLine();
455 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
456 << "---COMPILANDS---";
457 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000458 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000459 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000460 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000461 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000462 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000463 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000464 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000465 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000466 }
467
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000468 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000469 Printer.NewLine();
470 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
471 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000472 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000473 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000474 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000475 }
476
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000477 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000478 Printer.NewLine();
479 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
480 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000481 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000482 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000483 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000484 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000485 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000486 }
487
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000488 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000489 Printer.NewLine();
490 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
491 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000492 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000493 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000494 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000495 while (auto Function = Functions->getNext()) {
496 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000497 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000498 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000499 }
500 {
501 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000502 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000503 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000504 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000505 }
506 {
507 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000508 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000509 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000510 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000511 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000512 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000513 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000514 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000515 Printer.NewLine();
516 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
517 Printer.Indent();
518 ExternalSymbolDumper Dumper(Printer);
519 Dumper.start(*GlobalScope);
520 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000521 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000522 Printer.NewLine();
523 }
Zachary Turnera5549172015-02-10 22:43:25 +0000524 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000525}
526
527int main(int argc_, const char *argv_[]) {
528 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000529 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000530 PrettyStackTraceProgram X(argc_, argv_);
531
David Majnemerc165c882016-05-28 18:25:15 +0000532 ExitOnErr.setBanner("llvm-pdbdump: ");
533
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000534 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000535 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000536 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
537 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000538
539 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
540
541 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000542
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000543 // These options are shared by two subcommands.
544 if ((opts::PdbToYamlSubcommand || opts::RawSubcommand) && opts::raw::RawAll) {
545 opts::raw::DumpHeaders = true;
546 opts::raw::DumpModules = true;
547 opts::raw::DumpModuleFiles = true;
548 opts::raw::DumpModuleSyms = true;
549 opts::raw::DumpPublics = true;
550 opts::raw::DumpSectionHeaders = true;
551 opts::raw::DumpStreamSummary = true;
552 opts::raw::DumpStreamBlocks = true;
553 opts::raw::DumpTpiRecords = true;
554 opts::raw::DumpTpiHash = true;
555 opts::raw::DumpIpiRecords = true;
556 opts::raw::DumpSectionMap = true;
557 opts::raw::DumpSectionContribs = true;
558 opts::raw::DumpLineInfo = true;
559 opts::raw::DumpFpo = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000560 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000561
Zachary Turner93839cb2016-06-02 05:07:49 +0000562 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000563
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000564 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000565 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000566 } else if (opts::YamlToPdbSubcommand) {
567 yamlToPdb(opts::yaml2pdb::InputFilename.front());
568 } else if (opts::PrettySubcommand) {
569 if (opts::pretty::Lines)
570 opts::pretty::Compilands = true;
571
572 if (opts::pretty::All) {
573 opts::pretty::Compilands = true;
574 opts::pretty::Symbols = true;
575 opts::pretty::Globals = true;
576 opts::pretty::Types = true;
577 opts::pretty::Externals = true;
578 opts::pretty::Lines = true;
579 }
580
581 // When adding filters for excluded compilands and types, we need to
582 // remember
583 // that these are regexes. So special characters such as * and \ need to be
584 // escaped in the regex. In the case of a literal \, this means it needs to
585 // be escaped again in the C++. So matching a single \ in the input
586 // requires
587 // 4 \es in the C++.
588 if (opts::pretty::ExcludeCompilerGenerated) {
589 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
590 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
591 }
592 if (opts::pretty::ExcludeSystemLibraries) {
593 opts::pretty::ExcludeCompilands.push_back(
594 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
595 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
596 opts::pretty::ExcludeCompilands.push_back(
597 "d:\\\\th.obj.x86fre\\\\minkernel");
598 }
599 std::for_each(opts::pretty::InputFilenames.begin(),
600 opts::pretty::InputFilenames.end(), dumpPretty);
601 } else if (opts::RawSubcommand) {
602 std::for_each(opts::raw::InputFilenames.begin(),
603 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000604 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000605
Zachary Turner819e77d2016-05-06 20:51:57 +0000606 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000607 return 0;
608}