blob: 7ba991a8f74282048a2f1cab14b23ae79ecc2cd0 [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 Turner2d11c202015-02-27 09:15:59 +000020#include "LinePrinter.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000021#include "TypeDumper.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000022#include "VariableDumper.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000023
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000024#include "llvm/ADT/ArrayRef.h"
David Majnemer6e081262015-10-15 01:27:19 +000025#include "llvm/ADT/BitVector.h"
26#include "llvm/ADT/DenseMap.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000027#include "llvm/ADT/StringExtras.h"
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000028#include "llvm/Config/config.h"
Zachary Turnerd5d37dc2016-05-25 20:37:03 +000029#include "llvm/DebugInfo/CodeView/StreamReader.h"
Zachary Turnercac29ae2016-05-24 17:30:25 +000030#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner5a1b5ef2016-05-06 22:15:42 +000031#include "llvm/DebugInfo/CodeView/TypeDumper.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000032#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000033#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000034#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000035#include "llvm/DebugInfo/PDB/IPDBSession.h"
36#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000037#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000038#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000039#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000040#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
41#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000042#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
43#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000044#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000045#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner06c2b4b2016-05-09 17:45:21 +000046#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000047#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000048#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000049#include "llvm/DebugInfo/PDB/Raw/PublicsStream.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 Turnerf5c59652016-05-03 00:28:21 +000052#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000053#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/ConvertUTF.h"
Zachary Turner9a818ad2015-02-22 22:03:38 +000055#include "llvm/Support/FileSystem.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000056#include "llvm/Support/Format.h"
57#include "llvm/Support/ManagedStatic.h"
David Majnemer6e081262015-10-15 01:27:19 +000058#include "llvm/Support/MemoryBuffer.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000059#include "llvm/Support/PrettyStackTrace.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000060#include "llvm/Support/Process.h"
Reid Klecknerb0345262016-05-04 16:09:04 +000061#include "llvm/Support/ScopedPrinter.h"
Daniel Sandersd41718e2016-04-22 12:04:42 +000062#include "llvm/Support/Signals.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000063#include "llvm/Support/raw_ostream.h"
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000064
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000065#if defined(HAVE_DIA_SDK)
Benjamin Kramer4b4a9362015-10-15 09:38:45 +000066#ifndef NOMINMAX
67#define NOMINMAX
68#endif
Zachary Turnera5549172015-02-10 22:43:25 +000069#include <Windows.h>
Zachary Turner8d7fa9b2015-02-10 22:47:14 +000070#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000071
72using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000073using namespace llvm::pdb;
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000074
75namespace opts {
Zachary Turnerc0acf682015-02-15 20:27:53 +000076
77enum class PDB_DumpType { ByType, ByObjFile, Both };
78
Zachary Turnerfcb14ad2015-01-27 20:46:21 +000079cl::list<std::string> InputFilenames(cl::Positional,
80 cl::desc("<input PDB files>"),
81 cl::OneOrMore);
82
Zachary Turner7797c722015-03-02 04:39:56 +000083cl::OptionCategory TypeCategory("Symbol Type Options");
84cl::OptionCategory FilterCategory("Filtering Options");
Zachary Turnere5cb2692015-05-01 20:24:26 +000085cl::OptionCategory OtherOptions("Other Options");
Rui Ueyama18695f92016-05-26 23:01:05 +000086cl::OptionCategory NativeOptions("Native Options");
Zachary Turner7797c722015-03-02 04:39:56 +000087
88cl::opt<bool> Compilands("compilands", cl::desc("Display compilands"),
89 cl::cat(TypeCategory));
90cl::opt<bool> Symbols("symbols", cl::desc("Display symbols for each compiland"),
91 cl::cat(TypeCategory));
92cl::opt<bool> Globals("globals", cl::desc("Dump global symbols"),
93 cl::cat(TypeCategory));
Zachary Turnere5cb2692015-05-01 20:24:26 +000094cl::opt<bool> Externals("externals", cl::desc("Dump external symbols"),
95 cl::cat(TypeCategory));
Zachary Turner7797c722015-03-02 04:39:56 +000096cl::opt<bool> Types("types", cl::desc("Display types"), cl::cat(TypeCategory));
Zachary Turnera99000d2016-03-08 21:42:24 +000097cl::opt<bool> Lines("lines", cl::desc("Line tables"), cl::cat(TypeCategory));
Zachary Turner7797c722015-03-02 04:39:56 +000098cl::opt<bool>
Zachary Turner7797c722015-03-02 04:39:56 +000099 All("all", cl::desc("Implies all other options in 'Symbol Types' category"),
100 cl::cat(TypeCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000101
Zachary Turnere5cb2692015-05-01 20:24:26 +0000102cl::opt<uint64_t> LoadAddress(
103 "load-address",
104 cl::desc("Assume the module is loaded at the specified address"),
105 cl::cat(OtherOptions));
106
Zachary Turner96e60f72016-05-24 20:31:48 +0000107cl::opt<bool> DumpHeaders("raw-headers", cl::desc("dump PDB headers"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000108 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000109cl::opt<bool> DumpStreamBlocks("raw-stream-blocks",
David Majnemer6e081262015-10-15 01:27:19 +0000110 cl::desc("dump PDB stream blocks"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000111 cl::cat(NativeOptions));
Zachary Turner85ed80b2016-05-25 03:43:17 +0000112cl::opt<bool> DumpStreamSummary("raw-stream-summary",
113 cl::desc("dump summary of the PDB streams"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000114 cl::cat(NativeOptions));
Reid Klecknerb0345262016-05-04 16:09:04 +0000115cl::opt<bool>
Zachary Turnerc9972c62016-05-25 04:35:22 +0000116 DumpTpiRecords("raw-tpi-records",
117 cl::desc("dump CodeView type records from TPI stream"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000118 cl::cat(NativeOptions));
Zachary Turnerc9972c62016-05-25 04:35:22 +0000119cl::opt<bool> DumpTpiRecordBytes(
120 "raw-tpi-record-bytes",
121 cl::desc("dump CodeView type record raw bytes from TPI stream"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000122 cl::cat(NativeOptions));
Zachary Turnerc9972c62016-05-25 04:35:22 +0000123cl::opt<bool>
124 DumpIpiRecords("raw-ipi-records",
125 cl::desc("dump CodeView type records from IPI stream"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000126 cl::cat(NativeOptions));
Zachary Turnerc9972c62016-05-25 04:35:22 +0000127cl::opt<bool> DumpIpiRecordBytes(
128 "raw-ipi-record-bytes",
129 cl::desc("dump CodeView type record raw bytes from IPI stream"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000130 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000131cl::opt<std::string> DumpStreamDataIdx("raw-stream",
132 cl::desc("dump stream data"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000133 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000134cl::opt<std::string> DumpStreamDataName("raw-stream-name",
135 cl::desc("dump stream data"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000136 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000137cl::opt<bool> DumpModules("raw-modules", cl::desc("dump compiland information"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000138 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000139cl::opt<bool> DumpModuleFiles("raw-module-files",
140 cl::desc("dump file information"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000141 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000142cl::opt<bool> DumpModuleSyms("raw-module-syms", cl::desc("dump module symbols"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000143 cl::cat(NativeOptions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000144cl::opt<bool> DumpPublics("raw-publics", cl::desc("dump Publics stream data"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000145 cl::cat(NativeOptions));
Zachary Turnercac29ae2016-05-24 17:30:25 +0000146cl::opt<bool>
Zachary Turner96e60f72016-05-24 20:31:48 +0000147 DumpSymRecordBytes("raw-sym-record-bytes",
Zachary Turnercac29ae2016-05-24 17:30:25 +0000148 cl::desc("dump CodeView symbol record raw bytes"),
Rui Ueyama18695f92016-05-26 23:01:05 +0000149 cl::cat(NativeOptions));
Rui Ueyama9dc034d2016-05-26 23:26:55 +0000150cl::opt<bool>
151 RawAll("raw-all",
152 cl::desc("Implies most other options in 'Native Options' category"),
153 cl::cat(NativeOptions));
David Majnemer6e081262015-10-15 01:27:19 +0000154
Zachary Turnerf5abda22015-03-01 06:49:49 +0000155cl::list<std::string>
156 ExcludeTypes("exclude-types",
157 cl::desc("Exclude types by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +0000158 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000159cl::list<std::string>
160 ExcludeSymbols("exclude-symbols",
161 cl::desc("Exclude symbols by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +0000162 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000163cl::list<std::string>
164 ExcludeCompilands("exclude-compilands",
165 cl::desc("Exclude compilands by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +0000166 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000167
168cl::list<std::string> IncludeTypes(
169 "include-types",
170 cl::desc("Include only types which match a regular expression"),
171 cl::ZeroOrMore, cl::cat(FilterCategory));
172cl::list<std::string> IncludeSymbols(
173 "include-symbols",
174 cl::desc("Include only symbols which match a regular expression"),
175 cl::ZeroOrMore, cl::cat(FilterCategory));
176cl::list<std::string> IncludeCompilands(
177 "include-compilands",
178 cl::desc("Include only compilands those which match a regular expression"),
179 cl::ZeroOrMore, cl::cat(FilterCategory));
180
Zachary Turner7797c722015-03-02 04:39:56 +0000181cl::opt<bool> ExcludeCompilerGenerated(
182 "no-compiler-generated",
183 cl::desc("Don't show compiler generated types and symbols"),
184 cl::cat(FilterCategory));
185cl::opt<bool>
186 ExcludeSystemLibraries("no-system-libs",
187 cl::desc("Don't show symbols from system libraries"),
188 cl::cat(FilterCategory));
Zachary Turner65323652015-03-04 06:09:53 +0000189cl::opt<bool> NoClassDefs("no-class-definitions",
190 cl::desc("Don't display full class definitions"),
191 cl::cat(FilterCategory));
192cl::opt<bool> NoEnumDefs("no-enum-definitions",
193 cl::desc("Don't display full enum definitions"),
194 cl::cat(FilterCategory));
Zachary Turner49693b42015-01-28 01:22:33 +0000195}
196
Zachary Turner819e77d2016-05-06 20:51:57 +0000197static Error dumpFileHeaders(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000198 if (!opts::DumpHeaders)
Zachary Turner819e77d2016-05-06 20:51:57 +0000199 return Error::success();
200
Reid Klecknerb0345262016-05-04 16:09:04 +0000201 DictScope D(P, "FileHeaders");
202 P.printNumber("BlockSize", File.getBlockSize());
203 P.printNumber("Unknown0", File.getUnknown0());
204 P.printNumber("NumBlocks", File.getBlockCount());
205 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
206 P.printNumber("Unknown1", File.getUnknown1());
207 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
208 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
209 P.printNumber("BlockMapOffset", File.getBlockMapOffset());
Zachary Turnercdd313c2016-05-04 15:05:12 +0000210
Chad Rosier20dbbf32016-05-04 15:25:06 +0000211 // The directory is not contiguous. Instead, the block map contains a
212 // contiguous list of block numbers whose contents, when concatenated in
213 // order, make up the directory.
Reid Klecknerb0345262016-05-04 16:09:04 +0000214 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
215 P.printNumber("NumStreams", File.getNumStreams());
Zachary Turner819e77d2016-05-06 20:51:57 +0000216 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000217}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000218
Zachary Turner85ed80b2016-05-25 03:43:17 +0000219static Error dumpStreamSummary(ScopedPrinter &P, PDBFile &File) {
220 if (!opts::DumpStreamSummary)
Zachary Turner819e77d2016-05-06 20:51:57 +0000221 return Error::success();
Zachary Turnercdd313c2016-05-04 15:05:12 +0000222
Zachary Turner85ed80b2016-05-25 03:43:17 +0000223 auto DbiS = File.getPDBDbiStream();
224 if (auto EC = DbiS.takeError())
225 return EC;
226 auto TpiS = File.getPDBTpiStream();
227 if (auto EC = TpiS.takeError())
228 return EC;
Zachary Turnerc9972c62016-05-25 04:35:22 +0000229 auto IpiS = File.getPDBIpiStream();
230 if (auto EC = IpiS.takeError())
231 return EC;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000232 auto InfoS = File.getPDBInfoStream();
233 if (auto EC = InfoS.takeError())
234 return EC;
235 DbiStream &DS = DbiS.get();
236 TpiStream &TS = TpiS.get();
Zachary Turnerc9972c62016-05-25 04:35:22 +0000237 TpiStream &TIS = IpiS.get();
Zachary Turner85ed80b2016-05-25 03:43:17 +0000238 InfoStream &IS = InfoS.get();
239
240 ListScope L(P, "Streams");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000241 uint32_t StreamCount = File.getNumStreams();
Zachary Turner85ed80b2016-05-25 03:43:17 +0000242 std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams;
243 std::unordered_map<uint16_t, std::string> NamedStreams;
244
245 for (auto &ModI : DS.modules()) {
246 uint16_t SN = ModI.Info.getModuleStreamIndex();
247 ModStreams[SN] = &ModI;
Chad Rosier20dbbf32016-05-04 15:25:06 +0000248 }
Zachary Turner85ed80b2016-05-25 03:43:17 +0000249 for (auto &NSE : IS.named_streams()) {
250 NamedStreams[NSE.second] = NSE.first();
251 }
252
253 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
254 std::string Label("Stream ");
255 Label += to_string(StreamIdx);
256 std::string Value;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000257 if (StreamIdx == StreamPDB)
Zachary Turner85ed80b2016-05-25 03:43:17 +0000258 Value = "PDB Stream";
259 else if (StreamIdx == StreamDBI)
260 Value = "DBI Stream";
261 else if (StreamIdx == StreamTPI)
262 Value = "TPI Stream";
263 else if (StreamIdx == StreamIPI)
264 Value = "IPI Stream";
265 else if (StreamIdx == DS.getGlobalSymbolStreamIndex())
266 Value = "Global Symbol Hash";
267 else if (StreamIdx == DS.getPublicSymbolStreamIndex())
268 Value = "Public Symbol Hash";
269 else if (StreamIdx == DS.getSymRecordStreamIndex())
270 Value = "Public Symbol Records";
271 else if (StreamIdx == TS.getTypeHashStreamIndex())
272 Value = "TPI Hash";
273 else if (StreamIdx == TS.getTypeHashStreamAuxIndex())
274 Value = "TPI Aux Hash";
Zachary Turnerc9972c62016-05-25 04:35:22 +0000275 else if (StreamIdx == TIS.getTypeHashStreamIndex())
276 Value = "IPI Hash";
277 else if (StreamIdx == TIS.getTypeHashStreamAuxIndex())
278 Value = "IPI Aux Hash";
Zachary Turnerd3076ab2016-05-25 05:49:48 +0000279 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Exception))
280 Value = "Exception Data";
281 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Fixup))
282 Value = "Fixup Data";
283 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::FPO))
284 Value = "FPO Data";
285 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::NewFPO))
286 Value = "New FPO Data";
287 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
288 Value = "Omap From Source Data";
289 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::OmapToSrc))
290 Value = "Omap To Source Data";
291 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Pdata))
292 Value = "Pdata";
293 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::SectionHdr))
294 Value = "Section Header Data";
295 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
296 Value = "Section Header Original Data";
297 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::TokenRidMap))
298 Value = "Token Rid Data";
299 else if (StreamIdx == DS.getDebugStreamIndex(DbgHeaderType::Xdata))
300 Value = "Xdata";
Zachary Turner85ed80b2016-05-25 03:43:17 +0000301 else {
302 auto ModIter = ModStreams.find(StreamIdx);
303 auto NSIter = NamedStreams.find(StreamIdx);
304 if (ModIter != ModStreams.end()) {
305 Value = "Module \"";
306 Value += ModIter->second->Info.getModuleName();
307 Value += "\"";
308 } else if (NSIter != NamedStreams.end()) {
309 Value = "Named Stream \"";
310 Value += NSIter->second;
311 Value += "\"";
312 } else {
313 Value = "???";
314 }
315 }
316 Value = "[" + Value + "]";
317 Value =
318 Value + " (" + to_string(File.getStreamByteSize(StreamIdx)) + " bytes)";
319
320 P.printString(Label, Value);
321 }
322 P.flush();
Zachary Turner819e77d2016-05-06 20:51:57 +0000323 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000324}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000325
Zachary Turner819e77d2016-05-06 20:51:57 +0000326static Error dumpStreamBlocks(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000327 if (!opts::DumpStreamBlocks)
Zachary Turner819e77d2016-05-06 20:51:57 +0000328 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000329
330 ListScope L(P, "StreamBlocks");
331 uint32_t StreamCount = File.getNumStreams();
332 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
333 std::string Name("Stream ");
334 Name += to_string(StreamIdx);
335 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
336 P.printList(Name, StreamBlocks);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000337 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000338 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000339}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000340
Zachary Turner819e77d2016-05-06 20:51:57 +0000341static Error dumpStreamData(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000342 uint32_t StreamCount = File.getNumStreams();
Zachary Turner96e60f72016-05-24 20:31:48 +0000343 StringRef DumpStreamStr = opts::DumpStreamDataIdx;
Chad Rosier20dbbf32016-05-04 15:25:06 +0000344 uint32_t DumpStreamNum;
Reid Klecknerb0345262016-05-04 16:09:04 +0000345 if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum) ||
346 DumpStreamNum >= StreamCount)
Zachary Turner819e77d2016-05-06 20:51:57 +0000347 return Error::success();
Chad Rosier20dbbf32016-05-04 15:25:06 +0000348
Zachary Turner96e60f72016-05-24 20:31:48 +0000349 MappedBlockStream S(DumpStreamNum, File);
Zachary Turnerd5d37dc2016-05-25 20:37:03 +0000350 codeview::StreamReader R(S);
Zachary Turner96e60f72016-05-24 20:31:48 +0000351 while (R.bytesRemaining() > 0) {
352 ArrayRef<uint8_t> Data;
Reid Klecknerb0345262016-05-04 16:09:04 +0000353 uint32_t BytesToReadInBlock = std::min(
Zachary Turner96e60f72016-05-24 20:31:48 +0000354 R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize()));
355 if (auto EC = R.getArrayRef(Data, BytesToReadInBlock))
356 return EC;
Zachary Turnerd8ca0522016-05-25 18:32:07 +0000357 P.printBinaryBlock(
358 "Data",
359 StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size()));
Chad Rosier20dbbf32016-05-04 15:25:06 +0000360 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000361 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000362}
Chad Rosier20dbbf32016-05-04 15:25:06 +0000363
Zachary Turner819e77d2016-05-06 20:51:57 +0000364static Error dumpInfoStream(ScopedPrinter &P, PDBFile &File) {
Zachary Turnerc59261c2016-05-25 03:53:16 +0000365 if (!opts::DumpHeaders)
366 return Error::success();
Zachary Turner819e77d2016-05-06 20:51:57 +0000367 auto InfoS = File.getPDBInfoStream();
368 if (auto EC = InfoS.takeError())
369 return EC;
370
371 InfoStream &IS = InfoS.get();
Chad Rosier20dbbf32016-05-04 15:25:06 +0000372
Reid Klecknerb0345262016-05-04 16:09:04 +0000373 DictScope D(P, "PDB Stream");
374 P.printNumber("Version", IS.getVersion());
375 P.printHex("Signature", IS.getSignature());
376 P.printNumber("Age", IS.getAge());
377 P.printObject("Guid", IS.getGuid());
Zachary Turner819e77d2016-05-06 20:51:57 +0000378 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000379}
380
Zachary Turner96e60f72016-05-24 20:31:48 +0000381static Error dumpNamedStream(ScopedPrinter &P, PDBFile &File) {
382 if (opts::DumpStreamDataName.empty())
383 return Error::success();
384
Zachary Turner819e77d2016-05-06 20:51:57 +0000385 auto InfoS = File.getPDBInfoStream();
386 if (auto EC = InfoS.takeError())
387 return EC;
388 InfoStream &IS = InfoS.get();
389
Zachary Turner96e60f72016-05-24 20:31:48 +0000390 uint32_t NameStreamIndex = IS.getNamedStreamIndex(opts::DumpStreamDataName);
Reid Klecknerb0345262016-05-04 16:09:04 +0000391
Chad Rosier20dbbf32016-05-04 15:25:06 +0000392 if (NameStreamIndex != 0) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000393 std::string Name("Stream '");
Zachary Turner96e60f72016-05-24 20:31:48 +0000394 Name += opts::DumpStreamDataName;
Reid Klecknerb0345262016-05-04 16:09:04 +0000395 Name += "'";
396 DictScope D(P, Name);
397 P.printNumber("Index", NameStreamIndex);
398
Chad Rosier20dbbf32016-05-04 15:25:06 +0000399 MappedBlockStream NameStream(NameStreamIndex, File);
Zachary Turnerd5d37dc2016-05-25 20:37:03 +0000400 codeview::StreamReader Reader(NameStream);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000401
Chad Rosier20dbbf32016-05-04 15:25:06 +0000402 NameHashTable NameTable;
Zachary Turner819e77d2016-05-06 20:51:57 +0000403 if (auto EC = NameTable.load(Reader))
404 return EC;
405
Reid Klecknerb0345262016-05-04 16:09:04 +0000406 P.printHex("Signature", NameTable.getSignature());
407 P.printNumber("Version", NameTable.getHashVersion());
408 P.printNumber("Name Count", NameTable.getNameCount());
409 ListScope L(P, "Names");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000410 for (uint32_t ID : NameTable.name_ids()) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000411 StringRef Str = NameTable.getStringForID(ID);
412 if (!Str.empty())
413 P.printString(Str);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000414 }
415 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000416 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000417}
Chad Rosier20dbbf32016-05-04 15:25:06 +0000418
Zachary Turnercac29ae2016-05-24 17:30:25 +0000419static Error dumpDbiStream(ScopedPrinter &P, PDBFile &File,
420 codeview::CVTypeDumper &TD) {
Zachary Turnerc59261c2016-05-25 03:53:16 +0000421 bool DumpModules =
422 opts::DumpModules || opts::DumpModuleSyms || opts::DumpModuleFiles;
423 if (!opts::DumpHeaders && !DumpModules)
424 return Error::success();
425
Zachary Turner819e77d2016-05-06 20:51:57 +0000426 auto DbiS = File.getPDBDbiStream();
427 if (auto EC = DbiS.takeError())
428 return EC;
429 DbiStream &DS = DbiS.get();
Reid Klecknerb0345262016-05-04 16:09:04 +0000430
431 DictScope D(P, "DBI Stream");
432 P.printNumber("Dbi Version", DS.getDbiVersion());
433 P.printNumber("Age", DS.getAge());
434 P.printBoolean("Incremental Linking", DS.isIncrementallyLinked());
435 P.printBoolean("Has CTypes", DS.hasCTypes());
436 P.printBoolean("Is Stripped", DS.isStripped());
437 P.printObject("Machine Type", DS.getMachineType());
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000438 P.printNumber("Symbol Record Stream Index", DS.getSymRecordStreamIndex());
Zachary Turner96e60f72016-05-24 20:31:48 +0000439 P.printNumber("Public Symbol Stream Index", DS.getPublicSymbolStreamIndex());
440 P.printNumber("Global Symbol Stream Index", DS.getGlobalSymbolStreamIndex());
Chad Rosier20dbbf32016-05-04 15:25:06 +0000441
442 uint16_t Major = DS.getBuildMajorVersion();
443 uint16_t Minor = DS.getBuildMinorVersion();
Reid Klecknerb0345262016-05-04 16:09:04 +0000444 P.printVersion("Toolchain Version", Major, Minor);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000445
Reid Klecknerb0345262016-05-04 16:09:04 +0000446 std::string DllName;
447 raw_string_ostream DllStream(DllName);
448 DllStream << "mspdb" << Major << Minor << ".dll version";
449 DllStream.flush();
450 P.printVersion(DllName, Major, Minor, DS.getPdbDllVersion());
451
Zachary Turnerc59261c2016-05-25 03:53:16 +0000452 if (DumpModules) {
Zachary Turner96e60f72016-05-24 20:31:48 +0000453 ListScope L(P, "Modules");
454 for (auto &Modi : DS.modules()) {
455 DictScope DD(P);
456 P.printString("Name", Modi.Info.getModuleName());
457 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
458 P.printString("Object File Name", Modi.Info.getObjFileName());
459 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
460 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
461 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
462 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
463 P.printNumber("C13 Line Info Byte Size",
464 Modi.Info.getC13LineInfoByteSize());
465 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
466 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
467 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
468 if (opts::DumpModuleFiles) {
469 std::string FileListName =
470 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
471 ListScope LL(P, FileListName);
472 for (auto File : Modi.SourceFiles)
473 P.printString(File);
474 }
475 bool HasModuleDI =
476 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
477 bool ShouldDumpSymbols =
478 (opts::DumpModuleSyms || opts::DumpSymRecordBytes);
479 if (HasModuleDI && ShouldDumpSymbols) {
480 ListScope SS(P, "Symbols");
481 ModStream ModS(File, Modi.Info);
482 if (auto EC = ModS.reload())
483 return EC;
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000484
Zachary Turner96e60f72016-05-24 20:31:48 +0000485 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
486 for (auto &S : ModS.symbols()) {
487 DictScope DD(P, "");
Zachary Turnercac29ae2016-05-24 17:30:25 +0000488
Zachary Turner96e60f72016-05-24 20:31:48 +0000489 if (opts::DumpModuleSyms)
490 SD.dump(S);
491 if (opts::DumpSymRecordBytes)
492 P.printBinaryBlock("Bytes", S.Data);
493 }
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000494 }
495 }
Chad Rosier20dbbf32016-05-04 15:25:06 +0000496 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000497 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000498}
499
Zachary Turnercac29ae2016-05-24 17:30:25 +0000500static Error dumpTpiStream(ScopedPrinter &P, PDBFile &File,
Zachary Turnerc9972c62016-05-25 04:35:22 +0000501 codeview::CVTypeDumper &TD, uint32_t StreamIdx) {
502 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
Reid Klecknerb0345262016-05-04 16:09:04 +0000503
Zachary Turnerc9972c62016-05-25 04:35:22 +0000504 bool DumpRecordBytes = false;
505 bool DumpRecords = false;
506 StringRef Label;
507 StringRef VerLabel;
508 if (StreamIdx == StreamTPI) {
509 DumpRecordBytes = opts::DumpTpiRecordBytes;
510 DumpRecords = opts::DumpTpiRecordBytes;
511 Label = "Type Info Stream (TPI)";
512 VerLabel = "TPI Version";
513 } else if (StreamIdx == StreamIPI) {
514 DumpRecordBytes = opts::DumpIpiRecordBytes;
515 DumpRecords = opts::DumpIpiRecords;
516 Label = "Type Info Stream (IPI)";
517 VerLabel = "IPI Version";
518 }
519 if (!DumpRecordBytes && !DumpRecords && !opts::DumpModuleSyms)
520 return Error::success();
Zachary Turnercac29ae2016-05-24 17:30:25 +0000521
Zachary Turnerc9972c62016-05-25 04:35:22 +0000522 auto TpiS = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
523 : File.getPDBIpiStream();
Zachary Turnercac29ae2016-05-24 17:30:25 +0000524 if (auto EC = TpiS.takeError())
525 return EC;
526 TpiStream &Tpi = TpiS.get();
527
Zachary Turnerc9972c62016-05-25 04:35:22 +0000528 if (DumpRecords || DumpRecordBytes) {
529 DictScope D(P, Label);
530
531 P.printNumber(VerLabel, Tpi.getTpiVersion());
Zachary Turnercac29ae2016-05-24 17:30:25 +0000532 P.printNumber("Record count", Tpi.NumTypeRecords());
533
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000534 ListScope L(P, "Records");
Reid Klecknerb0345262016-05-04 16:09:04 +0000535
Reid Klecknerce5196e2016-05-12 23:26:23 +0000536 bool HadError = false;
537 for (auto &Type : Tpi.types(&HadError)) {
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000538 DictScope DD(P, "");
539
Zachary Turnerc9972c62016-05-25 04:35:22 +0000540 if (DumpRecords)
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000541 TD.dump(Type);
542
Zachary Turnerc9972c62016-05-25 04:35:22 +0000543 if (DumpRecordBytes)
Zachary Turner9073ed62016-05-09 17:44:58 +0000544 P.printBinaryBlock("Bytes", Type.Data);
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000545 }
Reid Klecknerce5196e2016-05-12 23:26:23 +0000546 if (HadError)
547 return make_error<RawError>(raw_error_code::corrupt_file,
548 "TPI stream contained corrupt record");
Zachary Turnercac29ae2016-05-24 17:30:25 +0000549 } else if (opts::DumpModuleSyms) {
550 // Even if the user doesn't want to dump type records, we still need to
551 // iterate them in order to build the list of types so that we can print
552 // them when dumping module symbols. So when they want to dump symbols
553 // but not types, use a null output stream.
554 ScopedPrinter *OldP = TD.getPrinter();
555 TD.setPrinter(nullptr);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000556
Zachary Turnercac29ae2016-05-24 17:30:25 +0000557 bool HadError = false;
558 for (auto &Type : Tpi.types(&HadError))
559 TD.dump(Type);
560
561 TD.setPrinter(OldP);
562 if (HadError)
563 return make_error<RawError>(raw_error_code::corrupt_file,
564 "TPI stream contained corrupt record");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000565 }
Zachary Turnerc9972c62016-05-25 04:35:22 +0000566 P.flush();
Zachary Turner819e77d2016-05-06 20:51:57 +0000567 return Error::success();
Zachary Turnercdd313c2016-05-04 15:05:12 +0000568}
569
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000570static Error dumpPublicsStream(ScopedPrinter &P, PDBFile &File,
571 codeview::CVTypeDumper &TD) {
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000572 if (!opts::DumpPublics)
573 return Error::success();
574
575 DictScope D(P, "Publics Stream");
576 auto PublicsS = File.getPDBPublicsStream();
577 if (auto EC = PublicsS.takeError())
578 return EC;
579 PublicsStream &Publics = PublicsS.get();
580 P.printNumber("Stream number", Publics.getStreamNum());
581 P.printNumber("SymHash", Publics.getSymHash());
582 P.printNumber("AddrMap", Publics.getAddrMap());
583 P.printNumber("Number of buckets", Publics.getNumBuckets());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000584 P.printList("Hash Buckets", Publics.getHashBuckets());
585 P.printList("Address Map", Publics.getAddressMap());
586 P.printList("Thunk Map", Publics.getThunkMap());
Rui Ueyama350b2982016-05-18 16:24:16 +0000587 P.printList("Section Offsets", Publics.getSectionOffsets());
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000588 ListScope L(P, "Symbols");
589 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
590 for (auto S : Publics.getSymbols()) {
591 DictScope DD(P, "");
592
593 SD.dump(S);
594 if (opts::DumpSymRecordBytes)
595 P.printBinaryBlock("Bytes", S.Data);
596 }
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000597 return Error::success();
598}
599
Zachary Turner819e77d2016-05-06 20:51:57 +0000600static Error dumpStructure(RawSession &RS) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000601 PDBFile &File = RS.getPDBFile();
602 ScopedPrinter P(outs());
603
Zachary Turner819e77d2016-05-06 20:51:57 +0000604 if (auto EC = dumpFileHeaders(P, File))
605 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000606
Zachary Turner85ed80b2016-05-25 03:43:17 +0000607 if (auto EC = dumpStreamSummary(P, File))
Zachary Turner819e77d2016-05-06 20:51:57 +0000608 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000609
Zachary Turner819e77d2016-05-06 20:51:57 +0000610 if (auto EC = dumpStreamBlocks(P, File))
611 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000612
Zachary Turner819e77d2016-05-06 20:51:57 +0000613 if (auto EC = dumpStreamData(P, File))
614 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000615
Zachary Turner819e77d2016-05-06 20:51:57 +0000616 if (auto EC = dumpInfoStream(P, File))
617 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000618
Zachary Turner96e60f72016-05-24 20:31:48 +0000619 if (auto EC = dumpNamedStream(P, File))
Zachary Turner819e77d2016-05-06 20:51:57 +0000620 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000621
Zachary Turnercac29ae2016-05-24 17:30:25 +0000622 codeview::CVTypeDumper TD(P, false);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000623 if (auto EC = dumpTpiStream(P, File, TD, StreamTPI))
624 return EC;
625 if (auto EC = dumpTpiStream(P, File, TD, StreamIPI))
Zachary Turner819e77d2016-05-06 20:51:57 +0000626 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000627
Zachary Turnercac29ae2016-05-24 17:30:25 +0000628 if (auto EC = dumpDbiStream(P, File, TD))
Zachary Turner819e77d2016-05-06 20:51:57 +0000629 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000630
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000631 if (auto EC = dumpPublicsStream(P, File, TD))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000632 return EC;
Zachary Turner96e60f72016-05-24 20:31:48 +0000633 return Error::success();
634}
635
636bool isRawDumpEnabled() {
637 if (opts::DumpHeaders)
638 return true;
639 if (opts::DumpModules)
640 return true;
641 if (opts::DumpModuleFiles)
642 return true;
643 if (opts::DumpModuleSyms)
644 return true;
645 if (!opts::DumpStreamDataIdx.empty())
646 return true;
647 if (!opts::DumpStreamDataName.empty())
648 return true;
649 if (opts::DumpPublics)
650 return true;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000651 if (opts::DumpStreamSummary)
Zachary Turner96e60f72016-05-24 20:31:48 +0000652 return true;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000653 if (opts::DumpStreamBlocks)
Zachary Turner96e60f72016-05-24 20:31:48 +0000654 return true;
655 if (opts::DumpSymRecordBytes)
656 return true;
657 if (opts::DumpTpiRecordBytes)
658 return true;
659 if (opts::DumpTpiRecords)
660 return true;
Zachary Turnerc9972c62016-05-25 04:35:22 +0000661 if (opts::DumpIpiRecords)
662 return true;
663 if (opts::DumpIpiRecordBytes)
664 return true;
Zachary Turner96e60f72016-05-24 20:31:48 +0000665 return false;
David Majnemer1573b242016-04-28 23:47:27 +0000666}
667
668static void dumpInput(StringRef Path) {
669 std::unique_ptr<IPDBSession> Session;
Zachary Turner96e60f72016-05-24 20:31:48 +0000670 if (isRawDumpEnabled()) {
Zachary Turner819e77d2016-05-06 20:51:57 +0000671 auto E = loadDataForPDB(PDB_ReaderType::Raw, Path, Session);
672 if (!E) {
David Majnemer1573b242016-04-28 23:47:27 +0000673 RawSession *RS = static_cast<RawSession *>(Session.get());
Zachary Turner819e77d2016-05-06 20:51:57 +0000674 E = dumpStructure(*RS);
David Majnemer1573b242016-04-28 23:47:27 +0000675 }
676
Zachary Turner819e77d2016-05-06 20:51:57 +0000677 if (E)
678 logAllUnhandledErrors(std::move(E), outs(), "");
679
David Majnemer1573b242016-04-28 23:47:27 +0000680 return;
681 }
682
Zachary Turner819e77d2016-05-06 20:51:57 +0000683 Error E = loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
684 if (E) {
685 logAllUnhandledErrors(std::move(E), outs(), "");
David Majnemer1573b242016-04-28 23:47:27 +0000686 return;
687 }
688
Zachary Turnere5cb2692015-05-01 20:24:26 +0000689 if (opts::LoadAddress)
690 Session->setLoadAddress(opts::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000691
Zachary Turner2d11c202015-02-27 09:15:59 +0000692 LinePrinter Printer(2, outs());
693
Zachary Turnera5549172015-02-10 22:43:25 +0000694 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000695 std::string FileName(GlobalScope->getSymbolsFileName());
696
Zachary Turner2d11c202015-02-27 09:15:59 +0000697 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
698 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
699 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000700 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000701
Zachary Turner2d11c202015-02-27 09:15:59 +0000702 Printer.NewLine();
703 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000704 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000705 Printer << ": " << FileSize << " bytes";
706 } else {
707 Printer << ": (Unable to obtain file size)";
708 }
709
710 Printer.NewLine();
711 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
712 Printer << ": " << GlobalScope->getGuid();
713
714 Printer.NewLine();
715 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
716 Printer << ": " << GlobalScope->getAge();
717
718 Printer.NewLine();
719 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
720 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000721 if (GlobalScope->hasCTypes())
722 outs() << "HasCTypes ";
723 if (GlobalScope->hasPrivateSymbols())
724 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000725 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000726
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000727 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000728 Printer.NewLine();
729 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
730 << "---COMPILANDS---";
731 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000732 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000733 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000734 CompilandDumpFlags options = CompilandDumper::Flags::None;
735 if (opts::Lines)
736 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000737 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000738 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000739 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000740 }
741
742 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000743 Printer.NewLine();
744 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
745 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000746 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000747 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000748 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000749 }
750
751 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000752 Printer.NewLine();
753 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
754 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000755 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000756 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000757 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000758 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000759 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000760 }
761
762 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000763 Printer.NewLine();
764 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
765 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000766 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000767 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000768 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000769 while (auto Function = Functions->getNext()) {
770 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000771 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000772 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000773 }
774 {
775 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000776 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000777 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000778 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000779 }
780 {
781 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000782 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000783 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000784 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000785 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000786 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000787 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000788 if (opts::Externals) {
789 Printer.NewLine();
790 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
791 Printer.Indent();
792 ExternalSymbolDumper Dumper(Printer);
793 Dumper.start(*GlobalScope);
794 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000795 if (opts::Lines) {
796 Printer.NewLine();
797 }
Zachary Turnera5549172015-02-10 22:43:25 +0000798 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000799}
800
801int main(int argc_, const char *argv_[]) {
802 // Print a stack trace if we signal out.
803 sys::PrintStackTraceOnErrorSignal();
804 PrettyStackTraceProgram X(argc_, argv_);
805
806 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000807 SpecificBumpPtrAllocator<char> ArgAllocator;
808 std::error_code EC = sys::Process::GetArgumentVector(
809 argv, makeArrayRef(argv_, argc_), ArgAllocator);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000810 if (EC) {
David Majnemer6e081262015-10-15 01:27:19 +0000811 errs() << "error: couldn't get arguments: " << EC.message() << '\n';
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000812 return 1;
813 }
814
815 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
816
817 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000818 if (opts::Lines)
819 opts::Compilands = true;
820
Zachary Turner7797c722015-03-02 04:39:56 +0000821 if (opts::All) {
822 opts::Compilands = true;
823 opts::Symbols = true;
824 opts::Globals = true;
825 opts::Types = true;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000826 opts::Externals = true;
Zachary Turnera99000d2016-03-08 21:42:24 +0000827 opts::Lines = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000828 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000829
Rui Ueyama9dc034d2016-05-26 23:26:55 +0000830 if (opts::RawAll) {
831 opts::DumpHeaders = true;
832 opts::DumpModules = true;
833 opts::DumpModuleFiles = true;
834 opts::DumpModuleSyms = true;
835 opts::DumpPublics = true;
836 opts::DumpStreamSummary = true;
837 opts::DumpStreamBlocks = true;
838 opts::DumpTpiRecords = true;
839 opts::DumpIpiRecords = true;
840 }
841
Zachary Turnera99000d2016-03-08 21:42:24 +0000842 // When adding filters for excluded compilands and types, we need to remember
843 // that these are regexes. So special characters such as * and \ need to be
844 // escaped in the regex. In the case of a literal \, this means it needs to
845 // be escaped again in the C++. So matching a single \ in the input requires
846 // 4 \es in the C++.
Zachary Turner7797c722015-03-02 04:39:56 +0000847 if (opts::ExcludeCompilerGenerated) {
848 opts::ExcludeTypes.push_back("__vc_attributes");
Zachary Turnera99000d2016-03-08 21:42:24 +0000849 opts::ExcludeCompilands.push_back("\\* Linker \\*");
Zachary Turner7797c722015-03-02 04:39:56 +0000850 }
851 if (opts::ExcludeSystemLibraries) {
852 opts::ExcludeCompilands.push_back(
Zachary Turnera99000d2016-03-08 21:42:24 +0000853 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
854 opts::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
855 opts::ExcludeCompilands.push_back("d:\\\\th.obj.x86fre\\\\minkernel");
Zachary Turner7797c722015-03-02 04:39:56 +0000856 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000857
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000858#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000859 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000860#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000861
862 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
863 dumpInput);
864
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000865#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000866 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000867#endif
Zachary Turner819e77d2016-05-06 20:51:57 +0000868 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000869 return 0;
870}