blob: 896c9cee557cab74c4bbbc9f177fac0a56d95104 [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 Turnera010f5c2016-07-29 18:24:26 +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
72namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000073
Zachary Turnera30bd1a2016-06-30 17:42:48 +000074cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
75cl::SubCommand
76 PrettySubcommand("pretty",
77 "Dump semantic information about types and symbols");
Zachary Turnerab58ae82016-06-30 17:43:00 +000078cl::SubCommand
79 YamlToPdbSubcommand("yaml2pdb",
80 "Generate a PDB file from a YAML description");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000081cl::SubCommand
82 PdbToYamlSubcommand("pdb2yaml",
83 "Generate a detailed YAML description of a PDB File");
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000084
Zachary Turner7797c722015-03-02 04:39:56 +000085cl::OptionCategory TypeCategory("Symbol Type Options");
86cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000087cl::OptionCategory OtherOptions("Other Options");
Zachary Turnera30bd1a2016-06-30 17:42:48 +000088
89namespace pretty {
90cl::list<std::string> InputFilenames(cl::Positional,
91 cl::desc("<input PDB files>"),
92 cl::OneOrMore, cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000093
94cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +000095 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000096cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +000097 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +000098cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +000099 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnere5cb2692015-05-01 20:24:26 +0000100cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000101 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
102cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
103 cl::sub(PrettySubcommand));
104cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
105 cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000106cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +0000107 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000108 cl::cat(TypeCategory), cl::sub(PrettySubcommand));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000109
Zachary Turnere5cb2692015-05-01 20:24:26 +0000110cl::opt<uint64_t> LoadAddress(
111 "load-address",
112 cl::desc("Assume the module is loaded at the specified address"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000113 cl::cat(OtherOptions), cl::sub(PrettySubcommand));
114cl::list<std::string> ExcludeTypes(
115 "exclude-types", cl::desc("Exclude types by regular expression"),
116 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
117cl::list<std::string> ExcludeSymbols(
118 "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
119 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
120cl::list<std::string> ExcludeCompilands(
121 "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
122 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000123
124cl::list<std::string> IncludeTypes(
125 "include-types",
126 cl::desc("Include only types which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000127 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000128cl::list<std::string> IncludeSymbols(
129 "include-symbols",
130 cl::desc("Include only symbols which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000131 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000132cl::list<std::string> IncludeCompilands(
133 "include-compilands",
134 cl::desc("Include only compilands those which match a regular expression"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000135 cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000136
Zachary Turner7797c722015-03-02 04:39:56 +0000137cl::opt<bool> ExcludeCompilerGenerated(
138 "no-compiler-generated",
139 cl::desc("Don't show compiler generated types and symbols"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000140 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner7797c722015-03-02 04:39:56 +0000141cl::opt<bool>
142 ExcludeSystemLibraries("no-system-libs",
143 cl::desc("Don't show symbols from system libraries"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000144 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000145cl::opt<bool> NoClassDefs("no-class-definitions",
146 cl::desc("Don't display full class definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000147 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
Zachary Turner65323652015-03-04 06:09:53 +0000148cl::opt<bool> NoEnumDefs("no-enum-definitions",
149 cl::desc("Don't display full enum definitions"),
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000150 cl::cat(FilterCategory), cl::sub(PrettySubcommand));
151}
152
153namespace raw {
154
155cl::OptionCategory MsfOptions("MSF Container Options");
156cl::OptionCategory TypeOptions("Type Record Options");
157cl::OptionCategory FileOptions("Module & File Options");
158cl::OptionCategory SymbolOptions("Symbol Options");
159cl::OptionCategory MiscOptions("Miscellaneous Options");
160
161// MSF OPTIONS
162cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
163 cl::cat(MsfOptions), cl::sub(RawSubcommand));
164cl::opt<bool> DumpStreamBlocks("stream-blocks",
165 cl::desc("dump PDB stream blocks"),
166 cl::cat(MsfOptions), cl::sub(RawSubcommand));
167cl::opt<bool> DumpStreamSummary("stream-summary",
168 cl::desc("dump summary of the PDB streams"),
169 cl::cat(MsfOptions), cl::sub(RawSubcommand));
170
171// TYPE OPTIONS
172cl::opt<bool>
173 DumpTpiRecords("tpi-records",
174 cl::desc("dump CodeView type records from TPI stream"),
175 cl::cat(TypeOptions), cl::sub(RawSubcommand));
176cl::opt<bool> DumpTpiRecordBytes(
177 "tpi-record-bytes",
178 cl::desc("dump CodeView type record raw bytes from TPI stream"),
179 cl::cat(TypeOptions), cl::sub(RawSubcommand));
180cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
181 cl::cat(TypeOptions), cl::sub(RawSubcommand));
182cl::opt<bool>
183 DumpIpiRecords("ipi-records",
184 cl::desc("dump CodeView type records from IPI stream"),
185 cl::cat(TypeOptions), cl::sub(RawSubcommand));
186cl::opt<bool> DumpIpiRecordBytes(
187 "ipi-record-bytes",
188 cl::desc("dump CodeView type record raw bytes from IPI stream"),
189 cl::cat(TypeOptions), cl::sub(RawSubcommand));
190
191// MODULE & FILE OPTIONS
192cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
193 cl::cat(FileOptions), cl::sub(RawSubcommand));
194cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
195 cl::cat(FileOptions), cl::sub(RawSubcommand));
196cl::opt<bool> DumpLineInfo("line-info",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000197 cl::desc("dump file and line information"),
198 cl::cat(FileOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000199
200// SYMBOL OPTIONS
201cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
202 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
203cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
204 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
205cl::opt<bool>
206 DumpSymRecordBytes("sym-record-bytes",
207 cl::desc("dump CodeView symbol record raw bytes"),
208 cl::cat(SymbolOptions), cl::sub(RawSubcommand));
209
210// MISCELLANEOUS OPTIONS
211cl::opt<bool> DumpSectionContribs("section-contribs",
212 cl::desc("dump section contributions"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000213 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000214cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
215 cl::cat(MiscOptions), cl::sub(RawSubcommand));
216cl::opt<bool> DumpSectionHeaders("section-headers",
217 cl::desc("dump section headers"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000218 cl::cat(MiscOptions), cl::sub(RawSubcommand));
219cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
220 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000221
222cl::opt<std::string> DumpStreamDataIdx("stream", cl::desc("dump stream data"),
Zachary Turnerab58ae82016-06-30 17:43:00 +0000223 cl::cat(MiscOptions),
224 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000225cl::opt<std::string> DumpStreamDataName("stream-name",
Zachary Turnerab58ae82016-06-30 17:43:00 +0000226 cl::desc("dump stream data"),
227 cl::cat(MiscOptions),
228 cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000229
Zachary Turnerab58ae82016-06-30 17:43:00 +0000230cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
231 cl::cat(MiscOptions), cl::sub(RawSubcommand));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000232
233cl::list<std::string> InputFilenames(cl::Positional,
234 cl::desc("<input PDB files>"),
235 cl::OneOrMore, cl::sub(RawSubcommand));
236}
237
238namespace yaml2pdb {
239cl::opt<std::string>
240 YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
241 cl::sub(YamlToPdbSubcommand));
242
243cl::list<std::string> InputFilename(cl::Positional,
244 cl::desc("<input YAML file>"), cl::Required,
245 cl::sub(YamlToPdbSubcommand));
246}
247
248namespace pdb2yaml {
Zachary Turnerf6b93822016-07-11 21:45:09 +0000249cl::opt<bool>
250 NoFileHeaders("no-file-headers",
251 cl::desc("Do not dump MSF file headers (you will not be able "
252 "to generate a fresh PDB from the resulting YAML)"),
253 cl::sub(PdbToYamlSubcommand), cl::init(false));
254
Zachary Turnerab58ae82016-06-30 17:43:00 +0000255cl::opt<bool> StreamMetadata(
256 "stream-metadata",
257 cl::desc("Dump the number of streams and each stream's size"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000258 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerab58ae82016-06-30 17:43:00 +0000259cl::opt<bool> StreamDirectory(
260 "stream-directory",
261 cl::desc("Dump each stream's block map (implies -stream-metadata)"),
Zachary Turnerf6b93822016-07-11 21:45:09 +0000262 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000263cl::opt<bool> PdbStream("pdb-stream",
264 cl::desc("Dump the PDB Stream (Stream 1)"),
265 cl::sub(PdbToYamlSubcommand), cl::init(false));
266cl::opt<bool> DbiStream("dbi-stream",
267 cl::desc("Dump the DBI Stream (Stream 2)"),
268 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnerd218c262016-07-22 15:46:37 +0000269cl::opt<bool>
270 DbiModuleInfo("dbi-module-info",
271 cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
272 cl::sub(PdbToYamlSubcommand), cl::init(false));
273
274cl::opt<bool> DbiModuleSourceFileInfo(
275 "dbi-module-source-info",
276 cl::desc(
277 "Dump DBI Module Source File Information (implies -dbi-module-info"),
278 cl::sub(PdbToYamlSubcommand), cl::init(false));
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000279
280cl::list<std::string> InputFilename(cl::Positional,
281 cl::desc("<input PDB file>"), cl::Required,
282 cl::sub(PdbToYamlSubcommand));
283}
Zachary Turner49693b42015-01-28 01:22:33 +0000284}
285
David Majnemerc165c882016-05-28 18:25:15 +0000286static ExitOnError ExitOnErr;
287
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000288static void yamlToPdb(StringRef Path) {
Zachary Turnere109dc62016-07-22 19:56:26 +0000289 BumpPtrAllocator Allocator;
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000290 ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
291 MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
292 /*RequiresNullTerminator=*/false);
293
294 if (ErrorOrBuffer.getError()) {
295 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
296 }
297
298 std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
299
300 llvm::yaml::Input In(Buffer->getBuffer());
301 pdb::yaml::PdbObject YamlObj;
302 In >> YamlObj;
Zachary Turnerf6b93822016-07-11 21:45:09 +0000303 if (!YamlObj.Headers.hasValue())
304 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
305 "Yaml does not contain MSF headers"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000306
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000307 auto OutFileOrError = FileOutputBuffer::create(
Zachary Turnerf6b93822016-07-11 21:45:09 +0000308 opts::yaml2pdb::YamlPdbOutputFile, YamlObj.Headers->FileSize);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000309 if (OutFileOrError.getError())
310 ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path,
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000311 opts::yaml2pdb::YamlPdbOutputFile));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000312
313 auto FileByteStream =
314 llvm::make_unique<FileBufferByteStream>(std::move(*OutFileOrError));
Zachary Turnere109dc62016-07-22 19:56:26 +0000315 PDBFileBuilder Builder(Allocator);
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000316
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000317 ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock));
318 ExitOnErr(Builder.getMsfBuilder().setDirectoryBlocksHint(
319 YamlObj.Headers->DirectoryBlocks));
320 if (!YamlObj.StreamSizes.hasValue()) {
321 ExitOnErr(make_error<GenericError>(
322 generic_error_code::unspecified,
323 "Cannot generate a PDB when stream sizes are not known"));
Zachary Turner8848a7a2016-07-06 18:05:57 +0000324 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000325
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000326 if (YamlObj.StreamMap.hasValue()) {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000327 if (YamlObj.StreamMap->size() != YamlObj.StreamSizes->size()) {
328 ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
329 "YAML specifies different number of "
330 "streams in stream sizes and stream "
331 "map"));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000332 }
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000333
334 auto &Sizes = *YamlObj.StreamSizes;
335 auto &Map = *YamlObj.StreamMap;
336 for (uint32_t I = 0; I < Sizes.size(); ++I) {
337 uint32_t Size = Sizes[I];
338 std::vector<uint32_t> Blocks;
339 for (auto E : Map[I].Blocks)
340 Blocks.push_back(E);
341 ExitOnErr(Builder.getMsfBuilder().addStream(Size, Blocks));
342 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000343 } else {
Zachary Turnerfaa554b2016-07-15 22:16:56 +0000344 auto &Sizes = *YamlObj.StreamSizes;
345 for (auto S : Sizes) {
346 ExitOnErr(Builder.getMsfBuilder().addStream(S));
347 }
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000348 }
Zachary Turner8848a7a2016-07-06 18:05:57 +0000349
350 if (YamlObj.PdbStream.hasValue()) {
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000351 auto &InfoBuilder = Builder.getInfoBuilder();
352 InfoBuilder.setAge(YamlObj.PdbStream->Age);
353 InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
354 InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
355 InfoBuilder.setVersion(YamlObj.PdbStream->Version);
Zachary Turner5e534c72016-07-15 22:17:08 +0000356 for (auto &NM : YamlObj.PdbStream->NamedStreams)
357 InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
358 NM.StreamNumber);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000359 }
360
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000361 if (YamlObj.DbiStream.hasValue()) {
362 auto &DbiBuilder = Builder.getDbiBuilder();
363 DbiBuilder.setAge(YamlObj.DbiStream->Age);
364 DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
365 DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
366 DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
367 DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
368 DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
369 DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
Zachary Turnerd218c262016-07-22 15:46:37 +0000370 for (const auto &MI : YamlObj.DbiStream->ModInfos) {
371 ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
372 for (auto S : MI.SourceFiles)
373 ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
374 }
Zachary Turnerdbeaea72016-07-11 21:45:26 +0000375 }
376
Zachary Turnerd66889c2016-07-28 19:12:28 +0000377 ExitOnErr(Builder.commit(*FileByteStream));
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000378}
379
Zachary Turner8848a7a2016-07-06 18:05:57 +0000380static void pdb2Yaml(StringRef Path) {
381 std::unique_ptr<IPDBSession> Session;
382 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
383
384 RawSession *RS = static_cast<RawSession *>(Session.get());
385 PDBFile &File = RS->getPDBFile();
386 auto O = llvm::make_unique<YAMLOutputStyle>(File);
387 O = llvm::make_unique<YAMLOutputStyle>(File);
388
389 ExitOnErr(O->dump());
390}
391
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000392static void dumpRaw(StringRef Path) {
David Majnemer1573b242016-04-28 23:47:27 +0000393 std::unique_ptr<IPDBSession> Session;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000394 ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000395
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000396 RawSession *RS = static_cast<RawSession *>(Session.get());
397 PDBFile &File = RS->getPDBFile();
Zachary Turner8848a7a2016-07-06 18:05:57 +0000398 auto O = llvm::make_unique<LLVMOutputStyle>(File);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000399
400 ExitOnErr(O->dump());
401}
402
403static void dumpPretty(StringRef Path) {
404 std::unique_ptr<IPDBSession> Session;
David Majnemer1573b242016-04-28 23:47:27 +0000405
David Majnemerc165c882016-05-28 18:25:15 +0000406 ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
David Majnemer1573b242016-04-28 23:47:27 +0000407
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000408 if (opts::pretty::LoadAddress)
409 Session->setLoadAddress(opts::pretty::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000410
Zachary Turner2d11c202015-02-27 09:15:59 +0000411 LinePrinter Printer(2, outs());
412
Zachary Turnera5549172015-02-10 22:43:25 +0000413 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000414 std::string FileName(GlobalScope->getSymbolsFileName());
415
Zachary Turner2d11c202015-02-27 09:15:59 +0000416 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
417 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
418 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000419 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000420
Zachary Turner2d11c202015-02-27 09:15:59 +0000421 Printer.NewLine();
422 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000423 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000424 Printer << ": " << FileSize << " bytes";
425 } else {
426 Printer << ": (Unable to obtain file size)";
427 }
428
429 Printer.NewLine();
430 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
431 Printer << ": " << GlobalScope->getGuid();
432
433 Printer.NewLine();
434 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
435 Printer << ": " << GlobalScope->getAge();
436
437 Printer.NewLine();
438 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
439 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000440 if (GlobalScope->hasCTypes())
441 outs() << "HasCTypes ";
442 if (GlobalScope->hasPrivateSymbols())
443 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000444 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000445
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000446 if (opts::pretty::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000447 Printer.NewLine();
448 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
449 << "---COMPILANDS---";
450 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000451 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000452 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000453 CompilandDumpFlags options = CompilandDumper::Flags::None;
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000454 if (opts::pretty::Lines)
Zachary Turnera99000d2016-03-08 21:42:24 +0000455 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000456 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000457 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000458 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000459 }
460
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000461 if (opts::pretty::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000462 Printer.NewLine();
463 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
464 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000465 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000466 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000467 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000468 }
469
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000470 if (opts::pretty::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000471 Printer.NewLine();
472 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
473 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000474 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000475 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000476 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000477 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000478 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000479 }
480
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000481 if (opts::pretty::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000482 Printer.NewLine();
483 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
484 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000485 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000486 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000487 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000488 while (auto Function = Functions->getNext()) {
489 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000490 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000491 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000492 }
493 {
494 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000495 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000496 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000497 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000498 }
499 {
500 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000501 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000502 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000503 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000504 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000505 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000506 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000507 if (opts::pretty::Externals) {
Zachary Turnere5cb2692015-05-01 20:24:26 +0000508 Printer.NewLine();
509 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
510 Printer.Indent();
511 ExternalSymbolDumper Dumper(Printer);
512 Dumper.start(*GlobalScope);
513 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000514 if (opts::pretty::Lines) {
Zachary Turnera99000d2016-03-08 21:42:24 +0000515 Printer.NewLine();
516 }
Zachary Turnera5549172015-02-10 22:43:25 +0000517 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000518}
519
520int main(int argc_, const char *argv_[]) {
521 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000522 sys::PrintStackTraceOnErrorSignal(argv_[0]);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000523 PrettyStackTraceProgram X(argc_, argv_);
524
David Majnemerc165c882016-05-28 18:25:15 +0000525 ExitOnErr.setBanner("llvm-pdbdump: ");
526
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000527 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000528 SpecificBumpPtrAllocator<char> ArgAllocator;
David Majnemerc165c882016-05-28 18:25:15 +0000529 ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
530 argv, makeArrayRef(argv_, argc_), ArgAllocator)));
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000531
532 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
533
534 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000535
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000536 // These options are shared by two subcommands.
537 if ((opts::PdbToYamlSubcommand || opts::RawSubcommand) && opts::raw::RawAll) {
538 opts::raw::DumpHeaders = true;
539 opts::raw::DumpModules = true;
540 opts::raw::DumpModuleFiles = true;
541 opts::raw::DumpModuleSyms = true;
542 opts::raw::DumpPublics = true;
543 opts::raw::DumpSectionHeaders = true;
544 opts::raw::DumpStreamSummary = true;
545 opts::raw::DumpStreamBlocks = true;
546 opts::raw::DumpTpiRecords = true;
547 opts::raw::DumpTpiHash = true;
548 opts::raw::DumpIpiRecords = true;
549 opts::raw::DumpSectionMap = true;
550 opts::raw::DumpSectionContribs = true;
551 opts::raw::DumpLineInfo = true;
552 opts::raw::DumpFpo = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000553 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000554
Zachary Turner93839cb2016-06-02 05:07:49 +0000555 llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000556
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000557 if (opts::PdbToYamlSubcommand) {
Zachary Turner8848a7a2016-07-06 18:05:57 +0000558 pdb2Yaml(opts::pdb2yaml::InputFilename.front());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000559 } else if (opts::YamlToPdbSubcommand) {
560 yamlToPdb(opts::yaml2pdb::InputFilename.front());
561 } else if (opts::PrettySubcommand) {
562 if (opts::pretty::Lines)
563 opts::pretty::Compilands = true;
564
565 if (opts::pretty::All) {
566 opts::pretty::Compilands = true;
567 opts::pretty::Symbols = true;
568 opts::pretty::Globals = true;
569 opts::pretty::Types = true;
570 opts::pretty::Externals = true;
571 opts::pretty::Lines = true;
572 }
573
574 // When adding filters for excluded compilands and types, we need to
575 // remember
576 // that these are regexes. So special characters such as * and \ need to be
577 // escaped in the regex. In the case of a literal \, this means it needs to
578 // be escaped again in the C++. So matching a single \ in the input
579 // requires
580 // 4 \es in the C++.
581 if (opts::pretty::ExcludeCompilerGenerated) {
582 opts::pretty::ExcludeTypes.push_back("__vc_attributes");
583 opts::pretty::ExcludeCompilands.push_back("\\* Linker \\*");
584 }
585 if (opts::pretty::ExcludeSystemLibraries) {
586 opts::pretty::ExcludeCompilands.push_back(
587 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
588 opts::pretty::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
589 opts::pretty::ExcludeCompilands.push_back(
590 "d:\\\\th.obj.x86fre\\\\minkernel");
591 }
592 std::for_each(opts::pretty::InputFilenames.begin(),
593 opts::pretty::InputFilenames.end(), dumpPretty);
594 } else if (opts::RawSubcommand) {
595 std::for_each(opts::raw::InputFilenames.begin(),
596 opts::raw::InputFilenames.end(), dumpRaw);
Zachary Turner1dc9fd32016-06-14 20:48:36 +0000597 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000598
Zachary Turner819e77d2016-05-06 20:51:57 +0000599 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000600 return 0;
601}