]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/llvm-pdbdump/llvm-pdbdump.cpp
Vendor import of llvm release_40 branch r292009:
[FreeBSD/FreeBSD.git] / tools / llvm-pdbdump / llvm-pdbdump.cpp
1 //===- 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
16 #include "llvm-pdbdump.h"
17 #include "LLVMOutputStyle.h"
18 #include "LinePrinter.h"
19 #include "OutputStyle.h"
20 #include "PrettyCompilandDumper.h"
21 #include "PrettyExternalSymbolDumper.h"
22 #include "PrettyFunctionDumper.h"
23 #include "PrettyTypeDumper.h"
24 #include "PrettyVariableDumper.h"
25 #include "YAMLOutputStyle.h"
26
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/BitVector.h"
29 #include "llvm/ADT/DenseMap.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/Config/config.h"
32 #include "llvm/DebugInfo/MSF/ByteStream.h"
33 #include "llvm/DebugInfo/MSF/MSFBuilder.h"
34 #include "llvm/DebugInfo/PDB/GenericError.h"
35 #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
36 #include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
37 #include "llvm/DebugInfo/PDB/IPDBSession.h"
38 #include "llvm/DebugInfo/PDB/PDB.h"
39 #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
40 #include "llvm/DebugInfo/PDB/PDBSymbolData.h"
41 #include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
42 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
43 #include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
44 #include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
45 #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
46 #include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
47 #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
48 #include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
49 #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
50 #include "llvm/DebugInfo/PDB/Raw/RawConstants.h"
51 #include "llvm/DebugInfo/PDB/Raw/RawError.h"
52 #include "llvm/DebugInfo/PDB/Raw/RawSession.h"
53 #include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
54 #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
55 #include "llvm/Support/COM.h"
56 #include "llvm/Support/CommandLine.h"
57 #include "llvm/Support/ConvertUTF.h"
58 #include "llvm/Support/FileOutputBuffer.h"
59 #include "llvm/Support/FileSystem.h"
60 #include "llvm/Support/Format.h"
61 #include "llvm/Support/ManagedStatic.h"
62 #include "llvm/Support/MemoryBuffer.h"
63 #include "llvm/Support/PrettyStackTrace.h"
64 #include "llvm/Support/Process.h"
65 #include "llvm/Support/Regex.h"
66 #include "llvm/Support/ScopedPrinter.h"
67 #include "llvm/Support/Signals.h"
68 #include "llvm/Support/raw_ostream.h"
69
70 using namespace llvm;
71 using namespace llvm::codeview;
72 using namespace llvm::msf;
73 using namespace llvm::pdb;
74
75 namespace opts {
76
77 cl::SubCommand RawSubcommand("raw", "Dump raw structure of the PDB file");
78 cl::SubCommand
79     PrettySubcommand("pretty",
80                      "Dump semantic information about types and symbols");
81 cl::SubCommand
82     YamlToPdbSubcommand("yaml2pdb",
83                         "Generate a PDB file from a YAML description");
84 cl::SubCommand
85     PdbToYamlSubcommand("pdb2yaml",
86                         "Generate a detailed YAML description of a PDB File");
87
88 cl::OptionCategory TypeCategory("Symbol Type Options");
89 cl::OptionCategory FilterCategory("Filtering Options");
90 cl::OptionCategory OtherOptions("Other Options");
91
92 namespace pretty {
93 cl::list<std::string> InputFilenames(cl::Positional,
94                                      cl::desc("<input PDB files>"),
95                                      cl::OneOrMore, cl::sub(PrettySubcommand));
96
97 cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
98                          cl::cat(TypeCategory), cl::sub(PrettySubcommand));
99 cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
100                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
101 cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
102                       cl::cat(TypeCategory), cl::sub(PrettySubcommand));
103 cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
104                         cl::cat(TypeCategory), cl::sub(PrettySubcommand));
105 cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory),
106                     cl::sub(PrettySubcommand));
107 cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory),
108                     cl::sub(PrettySubcommand));
109 cl::opt<bool>
110     All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
111         cl::cat(TypeCategory), cl::sub(PrettySubcommand));
112
113 cl::opt<uint64_t> LoadAddress(
114     "load-address",
115     cl::desc("Assume the module is loaded at the specified address"),
116     cl::cat(OtherOptions), cl::sub(PrettySubcommand));
117 cl::list<std::string> ExcludeTypes(
118     "exclude-types", cl::desc("Exclude types by regular expression"),
119     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
120 cl::list<std::string> ExcludeSymbols(
121     "exclude-symbols", cl::desc("Exclude symbols by regular expression"),
122     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
123 cl::list<std::string> ExcludeCompilands(
124     "exclude-compilands", cl::desc("Exclude compilands by regular expression"),
125     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
126
127 cl::list<std::string> IncludeTypes(
128     "include-types",
129     cl::desc("Include only types which match a regular expression"),
130     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
131 cl::list<std::string> IncludeSymbols(
132     "include-symbols",
133     cl::desc("Include only symbols which match a regular expression"),
134     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
135 cl::list<std::string> IncludeCompilands(
136     "include-compilands",
137     cl::desc("Include only compilands those which match a regular expression"),
138     cl::ZeroOrMore, cl::cat(FilterCategory), cl::sub(PrettySubcommand));
139
140 cl::opt<bool> ExcludeCompilerGenerated(
141     "no-compiler-generated",
142     cl::desc("Don't show compiler generated types and symbols"),
143     cl::cat(FilterCategory), cl::sub(PrettySubcommand));
144 cl::opt<bool>
145     ExcludeSystemLibraries("no-system-libs",
146                            cl::desc("Don't show symbols from system libraries"),
147                            cl::cat(FilterCategory), cl::sub(PrettySubcommand));
148 cl::opt<bool> NoClassDefs("no-class-definitions",
149                           cl::desc("Don't display full class definitions"),
150                           cl::cat(FilterCategory), cl::sub(PrettySubcommand));
151 cl::opt<bool> NoEnumDefs("no-enum-definitions",
152                          cl::desc("Don't display full enum definitions"),
153                          cl::cat(FilterCategory), cl::sub(PrettySubcommand));
154 }
155
156 namespace raw {
157
158 cl::OptionCategory MsfOptions("MSF Container Options");
159 cl::OptionCategory TypeOptions("Type Record Options");
160 cl::OptionCategory FileOptions("Module & File Options");
161 cl::OptionCategory SymbolOptions("Symbol Options");
162 cl::OptionCategory MiscOptions("Miscellaneous Options");
163
164 // MSF OPTIONS
165 cl::opt<bool> DumpHeaders("headers", cl::desc("dump PDB headers"),
166                           cl::cat(MsfOptions), cl::sub(RawSubcommand));
167 cl::opt<bool> DumpStreamBlocks("stream-blocks",
168                                cl::desc("dump PDB stream blocks"),
169                                cl::cat(MsfOptions), cl::sub(RawSubcommand));
170 cl::opt<bool> DumpStreamSummary("stream-summary",
171                                 cl::desc("dump summary of the PDB streams"),
172                                 cl::cat(MsfOptions), cl::sub(RawSubcommand));
173 cl::opt<bool> DumpPageStats(
174     "page-stats",
175     cl::desc("dump allocation stats of the pages in the MSF file"),
176     cl::cat(MsfOptions), cl::sub(RawSubcommand));
177 cl::opt<std::string>
178     DumpBlockRangeOpt("block-data", cl::value_desc("start[-end]"),
179                       cl::desc("Dump binary data from specified range."),
180                       cl::cat(MsfOptions), cl::sub(RawSubcommand));
181 llvm::Optional<BlockRange> DumpBlockRange;
182
183 cl::list<uint32_t>
184     DumpStreamData("stream-data", cl::CommaSeparated, cl::ZeroOrMore,
185                    cl::desc("Dump binary data from specified streams."),
186                    cl::cat(MsfOptions), cl::sub(RawSubcommand));
187
188 // TYPE OPTIONS
189 cl::opt<bool>
190     DumpTpiRecords("tpi-records",
191                    cl::desc("dump CodeView type records from TPI stream"),
192                    cl::cat(TypeOptions), cl::sub(RawSubcommand));
193 cl::opt<bool> DumpTpiRecordBytes(
194     "tpi-record-bytes",
195     cl::desc("dump CodeView type record raw bytes from TPI stream"),
196     cl::cat(TypeOptions), cl::sub(RawSubcommand));
197 cl::opt<bool> DumpTpiHash("tpi-hash", cl::desc("dump CodeView TPI hash stream"),
198                           cl::cat(TypeOptions), cl::sub(RawSubcommand));
199 cl::opt<bool>
200     DumpIpiRecords("ipi-records",
201                    cl::desc("dump CodeView type records from IPI stream"),
202                    cl::cat(TypeOptions), cl::sub(RawSubcommand));
203 cl::opt<bool> DumpIpiRecordBytes(
204     "ipi-record-bytes",
205     cl::desc("dump CodeView type record raw bytes from IPI stream"),
206     cl::cat(TypeOptions), cl::sub(RawSubcommand));
207
208 // MODULE & FILE OPTIONS
209 cl::opt<bool> DumpModules("modules", cl::desc("dump compiland information"),
210                           cl::cat(FileOptions), cl::sub(RawSubcommand));
211 cl::opt<bool> DumpModuleFiles("module-files", cl::desc("dump file information"),
212                               cl::cat(FileOptions), cl::sub(RawSubcommand));
213 cl::opt<bool> DumpLineInfo("line-info",
214                            cl::desc("dump file and line information"),
215                            cl::cat(FileOptions), cl::sub(RawSubcommand));
216
217 // SYMBOL OPTIONS
218 cl::opt<bool> DumpGlobals("globals", cl::desc("dump globals stream data"),
219                           cl::cat(SymbolOptions), cl::sub(RawSubcommand));
220 cl::opt<bool> DumpModuleSyms("module-syms", cl::desc("dump module symbols"),
221                              cl::cat(SymbolOptions), cl::sub(RawSubcommand));
222 cl::opt<bool> DumpPublics("publics", cl::desc("dump Publics stream data"),
223                           cl::cat(SymbolOptions), cl::sub(RawSubcommand));
224 cl::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
230 cl::opt<bool> DumpSectionContribs("section-contribs",
231                                   cl::desc("dump section contributions"),
232                                   cl::cat(MiscOptions), cl::sub(RawSubcommand));
233 cl::opt<bool> DumpSectionMap("section-map", cl::desc("dump section map"),
234                              cl::cat(MiscOptions), cl::sub(RawSubcommand));
235 cl::opt<bool> DumpSectionHeaders("section-headers",
236                                  cl::desc("dump section headers"),
237                                  cl::cat(MiscOptions), cl::sub(RawSubcommand));
238 cl::opt<bool> DumpFpo("fpo", cl::desc("dump FPO records"), cl::cat(MiscOptions),
239                       cl::sub(RawSubcommand));
240
241 cl::opt<bool> RawAll("all", cl::desc("Implies most other options."),
242                      cl::cat(MiscOptions), cl::sub(RawSubcommand));
243
244 cl::list<std::string> InputFilenames(cl::Positional,
245                                      cl::desc("<input PDB files>"),
246                                      cl::OneOrMore, cl::sub(RawSubcommand));
247 }
248
249 namespace yaml2pdb {
250 cl::opt<std::string>
251     YamlPdbOutputFile("pdb", cl::desc("the name of the PDB file to write"),
252                       cl::sub(YamlToPdbSubcommand));
253
254 cl::list<std::string> InputFilename(cl::Positional,
255                                     cl::desc("<input YAML file>"), cl::Required,
256                                     cl::sub(YamlToPdbSubcommand));
257 }
258
259 namespace pdb2yaml {
260 cl::opt<bool>
261     NoFileHeaders("no-file-headers",
262                   cl::desc("Do not dump MSF file headers (you will not be able "
263                            "to generate a fresh PDB from the resulting YAML)"),
264                   cl::sub(PdbToYamlSubcommand), cl::init(false));
265
266 cl::opt<bool> StreamMetadata(
267     "stream-metadata",
268     cl::desc("Dump the number of streams and each stream's size"),
269     cl::sub(PdbToYamlSubcommand), cl::init(false));
270 cl::opt<bool> StreamDirectory(
271     "stream-directory",
272     cl::desc("Dump each stream's block map (implies -stream-metadata)"),
273     cl::sub(PdbToYamlSubcommand), cl::init(false));
274 cl::opt<bool> PdbStream("pdb-stream",
275                         cl::desc("Dump the PDB Stream (Stream 1)"),
276                         cl::sub(PdbToYamlSubcommand), cl::init(false));
277 cl::opt<bool> DbiStream("dbi-stream",
278                         cl::desc("Dump the DBI Stream (Stream 2)"),
279                         cl::sub(PdbToYamlSubcommand), cl::init(false));
280 cl::opt<bool>
281     DbiModuleInfo("dbi-module-info",
282                   cl::desc("Dump DBI Module Information (implies -dbi-stream)"),
283                   cl::sub(PdbToYamlSubcommand), cl::init(false));
284
285 cl::opt<bool> DbiModuleSyms(
286     "dbi-module-syms",
287     cl::desc("Dump DBI Module Information (implies -dbi-module-info)"),
288     cl::sub(PdbToYamlSubcommand), cl::init(false));
289
290 cl::opt<bool> DbiModuleSourceFileInfo(
291     "dbi-module-source-info",
292     cl::desc(
293         "Dump DBI Module Source File Information (implies -dbi-module-info"),
294     cl::sub(PdbToYamlSubcommand), cl::init(false));
295
296 cl::opt<bool> TpiStream("tpi-stream",
297                         cl::desc("Dump the TPI Stream (Stream 3)"),
298                         cl::sub(PdbToYamlSubcommand), cl::init(false));
299
300 cl::opt<bool> IpiStream("ipi-stream",
301                         cl::desc("Dump the IPI Stream (Stream 5)"),
302                         cl::sub(PdbToYamlSubcommand), cl::init(false));
303
304 cl::list<std::string> InputFilename(cl::Positional,
305                                     cl::desc("<input PDB file>"), cl::Required,
306                                     cl::sub(PdbToYamlSubcommand));
307 }
308 }
309
310 static ExitOnError ExitOnErr;
311
312 static void yamlToPdb(StringRef Path) {
313   BumpPtrAllocator Allocator;
314   ErrorOr<std::unique_ptr<MemoryBuffer>> ErrorOrBuffer =
315       MemoryBuffer::getFileOrSTDIN(Path, /*FileSize=*/-1,
316                                    /*RequiresNullTerminator=*/false);
317
318   if (ErrorOrBuffer.getError()) {
319     ExitOnErr(make_error<GenericError>(generic_error_code::invalid_path, Path));
320   }
321
322   std::unique_ptr<MemoryBuffer> &Buffer = ErrorOrBuffer.get();
323
324   llvm::yaml::Input In(Buffer->getBuffer());
325   pdb::yaml::PdbObject YamlObj(Allocator);
326   In >> YamlObj;
327   if (!YamlObj.Headers.hasValue())
328     ExitOnErr(make_error<GenericError>(generic_error_code::unspecified,
329                                        "Yaml does not contain MSF headers"));
330
331   PDBFileBuilder Builder(Allocator);
332
333   ExitOnErr(Builder.initialize(YamlObj.Headers->SuperBlock.BlockSize));
334   // Add each of the reserved streams.  We ignore stream metadata in the
335   // yaml, because we will reconstruct our own view of the streams.  For
336   // example, the YAML may say that there were 20 streams in the original
337   // PDB, but maybe we only dump a subset of those 20 streams, so we will
338   // have fewer, and the ones we do have may end up with different indices
339   // than the ones in the original PDB.  So we just start with a clean slate.
340   for (uint32_t I = 0; I < kSpecialStreamCount; ++I)
341     ExitOnErr(Builder.getMsfBuilder().addStream(0));
342
343   if (YamlObj.PdbStream.hasValue()) {
344     auto &InfoBuilder = Builder.getInfoBuilder();
345     InfoBuilder.setAge(YamlObj.PdbStream->Age);
346     InfoBuilder.setGuid(YamlObj.PdbStream->Guid);
347     InfoBuilder.setSignature(YamlObj.PdbStream->Signature);
348     InfoBuilder.setVersion(YamlObj.PdbStream->Version);
349     for (auto &NM : YamlObj.PdbStream->NamedStreams)
350       InfoBuilder.getNamedStreamsBuilder().addMapping(NM.StreamName,
351                                                       NM.StreamNumber);
352   }
353
354   if (YamlObj.DbiStream.hasValue()) {
355     auto &DbiBuilder = Builder.getDbiBuilder();
356     DbiBuilder.setAge(YamlObj.DbiStream->Age);
357     DbiBuilder.setBuildNumber(YamlObj.DbiStream->BuildNumber);
358     DbiBuilder.setFlags(YamlObj.DbiStream->Flags);
359     DbiBuilder.setMachineType(YamlObj.DbiStream->MachineType);
360     DbiBuilder.setPdbDllRbld(YamlObj.DbiStream->PdbDllRbld);
361     DbiBuilder.setPdbDllVersion(YamlObj.DbiStream->PdbDllVersion);
362     DbiBuilder.setVersionHeader(YamlObj.DbiStream->VerHeader);
363     for (const auto &MI : YamlObj.DbiStream->ModInfos) {
364       ExitOnErr(DbiBuilder.addModuleInfo(MI.Obj, MI.Mod));
365       for (auto S : MI.SourceFiles)
366         ExitOnErr(DbiBuilder.addModuleSourceFile(MI.Mod, S));
367     }
368   }
369
370   if (YamlObj.TpiStream.hasValue()) {
371     auto &TpiBuilder = Builder.getTpiBuilder();
372     TpiBuilder.setVersionHeader(YamlObj.TpiStream->Version);
373     for (const auto &R : YamlObj.TpiStream->Records)
374       TpiBuilder.addTypeRecord(R.Record);
375   }
376
377   if (YamlObj.IpiStream.hasValue()) {
378     auto &IpiBuilder = Builder.getIpiBuilder();
379     IpiBuilder.setVersionHeader(YamlObj.IpiStream->Version);
380     for (const auto &R : YamlObj.IpiStream->Records)
381       IpiBuilder.addTypeRecord(R.Record);
382   }
383
384   ExitOnErr(Builder.commit(opts::yaml2pdb::YamlPdbOutputFile));
385 }
386
387 static 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
399 static void dumpRaw(StringRef Path) {
400   std::unique_ptr<IPDBSession> Session;
401   ExitOnErr(loadDataForPDB(PDB_ReaderType::Raw, Path, Session));
402
403   RawSession *RS = static_cast<RawSession *>(Session.get());
404   PDBFile &File = RS->getPDBFile();
405   auto O = llvm::make_unique<LLVMOutputStyle>(File);
406
407   ExitOnErr(O->dump());
408 }
409
410 static void dumpPretty(StringRef Path) {
411   std::unique_ptr<IPDBSession> Session;
412
413   ExitOnErr(loadDataForPDB(PDB_ReaderType::DIA, Path, Session));
414
415   if (opts::pretty::LoadAddress)
416     Session->setLoadAddress(opts::pretty::LoadAddress);
417
418   LinePrinter Printer(2, outs());
419
420   auto GlobalScope(Session->getGlobalScope());
421   std::string FileName(GlobalScope->getSymbolsFileName());
422
423   WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
424   WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
425   Printer.Indent();
426   uint64_t FileSize = 0;
427
428   Printer.NewLine();
429   WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
430   if (!sys::fs::file_size(FileName, FileSize)) {
431     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 << ": ";
447   if (GlobalScope->hasCTypes())
448     outs() << "HasCTypes ";
449   if (GlobalScope->hasPrivateSymbols())
450     outs() << "HasPrivateSymbols ";
451   Printer.Unindent();
452
453   if (opts::pretty::Compilands) {
454     Printer.NewLine();
455     WithColor(Printer, PDB_ColorItem::SectionHeader).get()
456         << "---COMPILANDS---";
457     Printer.Indent();
458     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
459     CompilandDumper Dumper(Printer);
460     CompilandDumpFlags options = CompilandDumper::Flags::None;
461     if (opts::pretty::Lines)
462       options = options | CompilandDumper::Flags::Lines;
463     while (auto Compiland = Compilands->getNext())
464       Dumper.start(*Compiland, options);
465     Printer.Unindent();
466   }
467
468   if (opts::pretty::Types) {
469     Printer.NewLine();
470     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
471     Printer.Indent();
472     TypeDumper Dumper(Printer);
473     Dumper.start(*GlobalScope);
474     Printer.Unindent();
475   }
476
477   if (opts::pretty::Symbols) {
478     Printer.NewLine();
479     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
480     Printer.Indent();
481     auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
482     CompilandDumper Dumper(Printer);
483     while (auto Compiland = Compilands->getNext())
484       Dumper.start(*Compiland, true);
485     Printer.Unindent();
486   }
487
488   if (opts::pretty::Globals) {
489     Printer.NewLine();
490     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
491     Printer.Indent();
492     {
493       FunctionDumper Dumper(Printer);
494       auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
495       while (auto Function = Functions->getNext()) {
496         Printer.NewLine();
497         Dumper.start(*Function, FunctionDumper::PointerType::None);
498       }
499     }
500     {
501       auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
502       VariableDumper Dumper(Printer);
503       while (auto Var = Vars->getNext())
504         Dumper.start(*Var);
505     }
506     {
507       auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
508       CompilandDumper Dumper(Printer);
509       while (auto Thunk = Thunks->getNext())
510         Dumper.dump(*Thunk);
511     }
512     Printer.Unindent();
513   }
514   if (opts::pretty::Externals) {
515     Printer.NewLine();
516     WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
517     Printer.Indent();
518     ExternalSymbolDumper Dumper(Printer);
519     Dumper.start(*GlobalScope);
520   }
521   if (opts::pretty::Lines) {
522     Printer.NewLine();
523   }
524   outs().flush();
525 }
526
527 int main(int argc_, const char *argv_[]) {
528   // Print a stack trace if we signal out.
529   sys::PrintStackTraceOnErrorSignal(argv_[0]);
530   PrettyStackTraceProgram X(argc_, argv_);
531
532   ExitOnErr.setBanner("llvm-pdbdump: ");
533
534   SmallVector<const char *, 256> argv;
535   SpecificBumpPtrAllocator<char> ArgAllocator;
536   ExitOnErr(errorCodeToError(sys::Process::GetArgumentVector(
537       argv, makeArrayRef(argv_, argc_), ArgAllocator)));
538
539   llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
540
541   cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
542   if (!opts::raw::DumpBlockRangeOpt.empty()) {
543     llvm::Regex R("^([0-9]+)(-([0-9]+))?$");
544     llvm::SmallVector<llvm::StringRef, 2> Matches;
545     if (!R.match(opts::raw::DumpBlockRangeOpt, &Matches)) {
546       errs() << "Argument '" << opts::raw::DumpBlockRangeOpt
547              << "' invalid format.\n";
548       errs().flush();
549       exit(1);
550     }
551     opts::raw::DumpBlockRange.emplace();
552     Matches[1].getAsInteger(10, opts::raw::DumpBlockRange->Min);
553     if (!Matches[3].empty()) {
554       opts::raw::DumpBlockRange->Max.emplace();
555       Matches[3].getAsInteger(10, *opts::raw::DumpBlockRange->Max);
556     }
557   }
558
559   if (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::DumpGlobals = true;
565     opts::raw::DumpPublics = true;
566     opts::raw::DumpSectionHeaders = true;
567     opts::raw::DumpStreamSummary = true;
568     opts::raw::DumpPageStats = true;
569     opts::raw::DumpStreamBlocks = true;
570     opts::raw::DumpTpiRecords = true;
571     opts::raw::DumpTpiHash = true;
572     opts::raw::DumpIpiRecords = true;
573     opts::raw::DumpSectionMap = true;
574     opts::raw::DumpSectionContribs = true;
575     opts::raw::DumpLineInfo = true;
576     opts::raw::DumpFpo = true;
577   }
578
579   llvm::sys::InitializeCOMRAII COM(llvm::sys::COMThreadingMode::MultiThreaded);
580
581   if (opts::PdbToYamlSubcommand) {
582     pdb2Yaml(opts::pdb2yaml::InputFilename.front());
583   } else if (opts::YamlToPdbSubcommand) {
584     yamlToPdb(opts::yaml2pdb::InputFilename.front());
585   } else if (opts::PrettySubcommand) {
586     if (opts::pretty::Lines)
587       opts::pretty::Compilands = true;
588
589     if (opts::pretty::All) {
590       opts::pretty::Compilands = true;
591       opts::pretty::Symbols = true;
592       opts::pretty::Globals = true;
593       opts::pretty::Types = true;
594       opts::pretty::Externals = true;
595       opts::pretty::Lines = true;
596     }
597
598     // When adding filters for excluded compilands and types, we need to
599     // remember that these are regexes.  So special characters such as * and \
600     // need to be escaped in the regex.  In the case of a literal \, this means
601     // it needs to be escaped again in the C++.  So matching a single \ in the
602     // input requires 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);
619   }
620
621   outs().flush();
622   return 0;
623 }