blob: 423ff7b6745225695b8f93d9153766e9012b1490 [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 Turnercac29ae2016-05-24 17:30:25 +000029#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner5a1b5ef2016-05-06 22:15:42 +000030#include "llvm/DebugInfo/CodeView/TypeDumper.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000031#include "llvm/DebugInfo/PDB/GenericError.h"
Zachary Turnera5549172015-02-10 22:43:25 +000032#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
Zachary Turnera5549172015-02-10 22:43:25 +000033#include "llvm/DebugInfo/PDB/IPDBRawSymbol.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000034#include "llvm/DebugInfo/PDB/IPDBSession.h"
35#include "llvm/DebugInfo/PDB/PDB.h"
Zachary Turnera5549172015-02-10 22:43:25 +000036#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000037#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
Chandler Carruth71f308a2015-02-13 09:09:03 +000038#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
Zachary Turnerdb18f5c2015-02-27 09:15:18 +000039#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
40#include "llvm/DebugInfo/PDB/PDBSymbolThunk.h"
Zachary Turner2f09b502016-04-29 17:28:47 +000041#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
42#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000043#include "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Zachary Turner1822af542016-04-27 23:41:42 +000044#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
Zachary Turner06c2b4b2016-05-09 17:45:21 +000045#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
Zachary Turner0eace0b2016-05-02 18:09:14 +000046#include "llvm/DebugInfo/PDB/Raw/NameHashTable.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000047#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Rui Ueyama1f6b6e22016-05-13 21:21:53 +000048#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
Reid Klecknerce5196e2016-05-12 23:26:23 +000049#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner0a43efe2016-04-25 17:38:08 +000050#include "llvm/DebugInfo/PDB/Raw/RawSession.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000051#include "llvm/DebugInfo/PDB/Raw/StreamReader.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");
Zachary Turner06c2b4b2016-05-09 17:45:21 +000086cl::OptionCategory NativeOtions("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"),
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000108 cl::cat(NativeOtions));
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"),
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000111 cl::cat(NativeOtions));
Zachary Turner85ed80b2016-05-25 03:43:17 +0000112cl::opt<bool> DumpStreamSummary("raw-stream-summary",
113 cl::desc("dump summary of the PDB streams"),
114 cl::cat(NativeOtions));
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"),
118 cl::cat(NativeOtions));
119cl::opt<bool> DumpTpiRecordBytes(
120 "raw-tpi-record-bytes",
121 cl::desc("dump CodeView type record raw bytes from TPI stream"),
122 cl::cat(NativeOtions));
123cl::opt<bool>
124 DumpIpiRecords("raw-ipi-records",
125 cl::desc("dump CodeView type records from IPI stream"),
126 cl::cat(NativeOtions));
127cl::opt<bool> DumpIpiRecordBytes(
128 "raw-ipi-record-bytes",
129 cl::desc("dump CodeView type record raw bytes from IPI stream"),
130 cl::cat(NativeOtions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000131cl::opt<std::string> DumpStreamDataIdx("raw-stream",
132 cl::desc("dump stream data"),
133 cl::cat(NativeOtions));
134cl::opt<std::string> DumpStreamDataName("raw-stream-name",
135 cl::desc("dump stream data"),
136 cl::cat(NativeOtions));
137cl::opt<bool> DumpModules("raw-modules", cl::desc("dump compiland information"),
138 cl::cat(NativeOtions));
139cl::opt<bool> DumpModuleFiles("raw-module-files",
140 cl::desc("dump file information"),
141 cl::cat(NativeOtions));
142cl::opt<bool> DumpModuleSyms("raw-module-syms", cl::desc("dump module symbols"),
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000143 cl::cat(NativeOtions));
Zachary Turner96e60f72016-05-24 20:31:48 +0000144cl::opt<bool> DumpPublics("raw-publics", cl::desc("dump Publics stream data"),
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000145 cl::cat(NativeOtions));
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"),
149 cl::cat(NativeOtions));
David Majnemer6e081262015-10-15 01:27:19 +0000150
Zachary Turnerf5abda22015-03-01 06:49:49 +0000151cl::list<std::string>
152 ExcludeTypes("exclude-types",
153 cl::desc("Exclude types by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +0000154 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turnerf5abda22015-03-01 06:49:49 +0000155cl::list<std::string>
156 ExcludeSymbols("exclude-symbols",
157 cl::desc("Exclude symbols 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 ExcludeCompilands("exclude-compilands",
161 cl::desc("Exclude compilands by regular expression"),
Zachary Turner7797c722015-03-02 04:39:56 +0000162 cl::ZeroOrMore, cl::cat(FilterCategory));
Zachary Turner4dddcc62015-09-29 19:49:06 +0000163
164cl::list<std::string> IncludeTypes(
165 "include-types",
166 cl::desc("Include only types which match a regular expression"),
167 cl::ZeroOrMore, cl::cat(FilterCategory));
168cl::list<std::string> IncludeSymbols(
169 "include-symbols",
170 cl::desc("Include only symbols which match a regular expression"),
171 cl::ZeroOrMore, cl::cat(FilterCategory));
172cl::list<std::string> IncludeCompilands(
173 "include-compilands",
174 cl::desc("Include only compilands those which match a regular expression"),
175 cl::ZeroOrMore, cl::cat(FilterCategory));
176
Zachary Turner7797c722015-03-02 04:39:56 +0000177cl::opt<bool> ExcludeCompilerGenerated(
178 "no-compiler-generated",
179 cl::desc("Don't show compiler generated types and symbols"),
180 cl::cat(FilterCategory));
181cl::opt<bool>
182 ExcludeSystemLibraries("no-system-libs",
183 cl::desc("Don't show symbols from system libraries"),
184 cl::cat(FilterCategory));
Zachary Turner65323652015-03-04 06:09:53 +0000185cl::opt<bool> NoClassDefs("no-class-definitions",
186 cl::desc("Don't display full class definitions"),
187 cl::cat(FilterCategory));
188cl::opt<bool> NoEnumDefs("no-enum-definitions",
189 cl::desc("Don't display full enum definitions"),
190 cl::cat(FilterCategory));
Zachary Turner49693b42015-01-28 01:22:33 +0000191}
192
Zachary Turner819e77d2016-05-06 20:51:57 +0000193static Error dumpFileHeaders(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000194 if (!opts::DumpHeaders)
Zachary Turner819e77d2016-05-06 20:51:57 +0000195 return Error::success();
196
Reid Klecknerb0345262016-05-04 16:09:04 +0000197 DictScope D(P, "FileHeaders");
198 P.printNumber("BlockSize", File.getBlockSize());
199 P.printNumber("Unknown0", File.getUnknown0());
200 P.printNumber("NumBlocks", File.getBlockCount());
201 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
202 P.printNumber("Unknown1", File.getUnknown1());
203 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
204 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
205 P.printNumber("BlockMapOffset", File.getBlockMapOffset());
Zachary Turnercdd313c2016-05-04 15:05:12 +0000206
Chad Rosier20dbbf32016-05-04 15:25:06 +0000207 // The directory is not contiguous. Instead, the block map contains a
208 // contiguous list of block numbers whose contents, when concatenated in
209 // order, make up the directory.
Reid Klecknerb0345262016-05-04 16:09:04 +0000210 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
211 P.printNumber("NumStreams", File.getNumStreams());
Zachary Turner819e77d2016-05-06 20:51:57 +0000212 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000213}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000214
Zachary Turner85ed80b2016-05-25 03:43:17 +0000215static Error dumpStreamSummary(ScopedPrinter &P, PDBFile &File) {
216 if (!opts::DumpStreamSummary)
Zachary Turner819e77d2016-05-06 20:51:57 +0000217 return Error::success();
Zachary Turnercdd313c2016-05-04 15:05:12 +0000218
Zachary Turner85ed80b2016-05-25 03:43:17 +0000219 auto DbiS = File.getPDBDbiStream();
220 if (auto EC = DbiS.takeError())
221 return EC;
222 auto TpiS = File.getPDBTpiStream();
223 if (auto EC = TpiS.takeError())
224 return EC;
Zachary Turnerc9972c62016-05-25 04:35:22 +0000225 auto IpiS = File.getPDBIpiStream();
226 if (auto EC = IpiS.takeError())
227 return EC;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000228 auto InfoS = File.getPDBInfoStream();
229 if (auto EC = InfoS.takeError())
230 return EC;
231 DbiStream &DS = DbiS.get();
232 TpiStream &TS = TpiS.get();
Zachary Turnerc9972c62016-05-25 04:35:22 +0000233 TpiStream &TIS = IpiS.get();
Zachary Turner85ed80b2016-05-25 03:43:17 +0000234 InfoStream &IS = InfoS.get();
235
236 ListScope L(P, "Streams");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000237 uint32_t StreamCount = File.getNumStreams();
Zachary Turner85ed80b2016-05-25 03:43:17 +0000238 std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams;
239 std::unordered_map<uint16_t, std::string> NamedStreams;
240
241 for (auto &ModI : DS.modules()) {
242 uint16_t SN = ModI.Info.getModuleStreamIndex();
243 ModStreams[SN] = &ModI;
Chad Rosier20dbbf32016-05-04 15:25:06 +0000244 }
Zachary Turner85ed80b2016-05-25 03:43:17 +0000245 for (auto &NSE : IS.named_streams()) {
246 NamedStreams[NSE.second] = NSE.first();
247 }
248
249 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
250 std::string Label("Stream ");
251 Label += to_string(StreamIdx);
252 std::string Value;
Zachary Turnerc59261c2016-05-25 03:53:16 +0000253 if (StreamIdx == StreamPDB)
Zachary Turner85ed80b2016-05-25 03:43:17 +0000254 Value = "PDB Stream";
255 else if (StreamIdx == StreamDBI)
256 Value = "DBI Stream";
257 else if (StreamIdx == StreamTPI)
258 Value = "TPI Stream";
259 else if (StreamIdx == StreamIPI)
260 Value = "IPI Stream";
261 else if (StreamIdx == DS.getGlobalSymbolStreamIndex())
262 Value = "Global Symbol Hash";
263 else if (StreamIdx == DS.getPublicSymbolStreamIndex())
264 Value = "Public Symbol Hash";
265 else if (StreamIdx == DS.getSymRecordStreamIndex())
266 Value = "Public Symbol Records";
267 else if (StreamIdx == TS.getTypeHashStreamIndex())
268 Value = "TPI Hash";
269 else if (StreamIdx == TS.getTypeHashStreamAuxIndex())
270 Value = "TPI Aux Hash";
Zachary Turnerc9972c62016-05-25 04:35:22 +0000271 else if (StreamIdx == TIS.getTypeHashStreamIndex())
272 Value = "IPI Hash";
273 else if (StreamIdx == TIS.getTypeHashStreamAuxIndex())
274 Value = "IPI Aux Hash";
Zachary Turner85ed80b2016-05-25 03:43:17 +0000275 else {
276 auto ModIter = ModStreams.find(StreamIdx);
277 auto NSIter = NamedStreams.find(StreamIdx);
278 if (ModIter != ModStreams.end()) {
279 Value = "Module \"";
280 Value += ModIter->second->Info.getModuleName();
281 Value += "\"";
282 } else if (NSIter != NamedStreams.end()) {
283 Value = "Named Stream \"";
284 Value += NSIter->second;
285 Value += "\"";
286 } else {
287 Value = "???";
288 }
289 }
290 Value = "[" + Value + "]";
291 Value =
292 Value + " (" + to_string(File.getStreamByteSize(StreamIdx)) + " bytes)";
293
294 P.printString(Label, Value);
295 }
296 P.flush();
Zachary Turner819e77d2016-05-06 20:51:57 +0000297 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000298}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000299
Zachary Turner819e77d2016-05-06 20:51:57 +0000300static Error dumpStreamBlocks(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000301 if (!opts::DumpStreamBlocks)
Zachary Turner819e77d2016-05-06 20:51:57 +0000302 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000303
304 ListScope L(P, "StreamBlocks");
305 uint32_t StreamCount = File.getNumStreams();
306 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
307 std::string Name("Stream ");
308 Name += to_string(StreamIdx);
309 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
310 P.printList(Name, StreamBlocks);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000311 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000312 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000313}
Zachary Turnercdd313c2016-05-04 15:05:12 +0000314
Zachary Turner819e77d2016-05-06 20:51:57 +0000315static Error dumpStreamData(ScopedPrinter &P, PDBFile &File) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000316 uint32_t StreamCount = File.getNumStreams();
Zachary Turner96e60f72016-05-24 20:31:48 +0000317 StringRef DumpStreamStr = opts::DumpStreamDataIdx;
Chad Rosier20dbbf32016-05-04 15:25:06 +0000318 uint32_t DumpStreamNum;
Reid Klecknerb0345262016-05-04 16:09:04 +0000319 if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum) ||
320 DumpStreamNum >= StreamCount)
Zachary Turner819e77d2016-05-06 20:51:57 +0000321 return Error::success();
Chad Rosier20dbbf32016-05-04 15:25:06 +0000322
Zachary Turner96e60f72016-05-24 20:31:48 +0000323 MappedBlockStream S(DumpStreamNum, File);
324 StreamReader R(S);
325 while (R.bytesRemaining() > 0) {
326 ArrayRef<uint8_t> Data;
Reid Klecknerb0345262016-05-04 16:09:04 +0000327 uint32_t BytesToReadInBlock = std::min(
Zachary Turner96e60f72016-05-24 20:31:48 +0000328 R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize()));
329 if (auto EC = R.getArrayRef(Data, BytesToReadInBlock))
330 return EC;
331 outs() << StringRef(reinterpret_cast<const char *>(Data.begin()),
332 Data.size());
Chad Rosier20dbbf32016-05-04 15:25:06 +0000333 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000334 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000335}
Chad Rosier20dbbf32016-05-04 15:25:06 +0000336
Zachary Turner819e77d2016-05-06 20:51:57 +0000337static Error dumpInfoStream(ScopedPrinter &P, PDBFile &File) {
Zachary Turnerc59261c2016-05-25 03:53:16 +0000338 if (!opts::DumpHeaders)
339 return Error::success();
Zachary Turner819e77d2016-05-06 20:51:57 +0000340 auto InfoS = File.getPDBInfoStream();
341 if (auto EC = InfoS.takeError())
342 return EC;
343
344 InfoStream &IS = InfoS.get();
Chad Rosier20dbbf32016-05-04 15:25:06 +0000345
Reid Klecknerb0345262016-05-04 16:09:04 +0000346 DictScope D(P, "PDB Stream");
347 P.printNumber("Version", IS.getVersion());
348 P.printHex("Signature", IS.getSignature());
349 P.printNumber("Age", IS.getAge());
350 P.printObject("Guid", IS.getGuid());
Zachary Turner819e77d2016-05-06 20:51:57 +0000351 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000352}
353
Zachary Turner96e60f72016-05-24 20:31:48 +0000354static Error dumpNamedStream(ScopedPrinter &P, PDBFile &File) {
355 if (opts::DumpStreamDataName.empty())
356 return Error::success();
357
Zachary Turner819e77d2016-05-06 20:51:57 +0000358 auto InfoS = File.getPDBInfoStream();
359 if (auto EC = InfoS.takeError())
360 return EC;
361 InfoStream &IS = InfoS.get();
362
Zachary Turner96e60f72016-05-24 20:31:48 +0000363 uint32_t NameStreamIndex = IS.getNamedStreamIndex(opts::DumpStreamDataName);
Reid Klecknerb0345262016-05-04 16:09:04 +0000364
Chad Rosier20dbbf32016-05-04 15:25:06 +0000365 if (NameStreamIndex != 0) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000366 std::string Name("Stream '");
Zachary Turner96e60f72016-05-24 20:31:48 +0000367 Name += opts::DumpStreamDataName;
Reid Klecknerb0345262016-05-04 16:09:04 +0000368 Name += "'";
369 DictScope D(P, Name);
370 P.printNumber("Index", NameStreamIndex);
371
Chad Rosier20dbbf32016-05-04 15:25:06 +0000372 MappedBlockStream NameStream(NameStreamIndex, File);
373 StreamReader Reader(NameStream);
374
Chad Rosier20dbbf32016-05-04 15:25:06 +0000375 NameHashTable NameTable;
Zachary Turner819e77d2016-05-06 20:51:57 +0000376 if (auto EC = NameTable.load(Reader))
377 return EC;
378
Reid Klecknerb0345262016-05-04 16:09:04 +0000379 P.printHex("Signature", NameTable.getSignature());
380 P.printNumber("Version", NameTable.getHashVersion());
381 P.printNumber("Name Count", NameTable.getNameCount());
382 ListScope L(P, "Names");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000383 for (uint32_t ID : NameTable.name_ids()) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000384 StringRef Str = NameTable.getStringForID(ID);
385 if (!Str.empty())
386 P.printString(Str);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000387 }
388 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000389 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000390}
Chad Rosier20dbbf32016-05-04 15:25:06 +0000391
Zachary Turnercac29ae2016-05-24 17:30:25 +0000392static Error dumpDbiStream(ScopedPrinter &P, PDBFile &File,
393 codeview::CVTypeDumper &TD) {
Zachary Turnerc59261c2016-05-25 03:53:16 +0000394 bool DumpModules =
395 opts::DumpModules || opts::DumpModuleSyms || opts::DumpModuleFiles;
396 if (!opts::DumpHeaders && !DumpModules)
397 return Error::success();
398
Zachary Turner819e77d2016-05-06 20:51:57 +0000399 auto DbiS = File.getPDBDbiStream();
400 if (auto EC = DbiS.takeError())
401 return EC;
402 DbiStream &DS = DbiS.get();
Reid Klecknerb0345262016-05-04 16:09:04 +0000403
404 DictScope D(P, "DBI Stream");
405 P.printNumber("Dbi Version", DS.getDbiVersion());
406 P.printNumber("Age", DS.getAge());
407 P.printBoolean("Incremental Linking", DS.isIncrementallyLinked());
408 P.printBoolean("Has CTypes", DS.hasCTypes());
409 P.printBoolean("Is Stripped", DS.isStripped());
410 P.printObject("Machine Type", DS.getMachineType());
Rui Ueyama0376b1a2016-05-19 18:05:58 +0000411 P.printNumber("Symbol Record Stream Index", DS.getSymRecordStreamIndex());
Zachary Turner96e60f72016-05-24 20:31:48 +0000412 P.printNumber("Public Symbol Stream Index", DS.getPublicSymbolStreamIndex());
413 P.printNumber("Global Symbol Stream Index", DS.getGlobalSymbolStreamIndex());
Chad Rosier20dbbf32016-05-04 15:25:06 +0000414
415 uint16_t Major = DS.getBuildMajorVersion();
416 uint16_t Minor = DS.getBuildMinorVersion();
Reid Klecknerb0345262016-05-04 16:09:04 +0000417 P.printVersion("Toolchain Version", Major, Minor);
Chad Rosier20dbbf32016-05-04 15:25:06 +0000418
Reid Klecknerb0345262016-05-04 16:09:04 +0000419 std::string DllName;
420 raw_string_ostream DllStream(DllName);
421 DllStream << "mspdb" << Major << Minor << ".dll version";
422 DllStream.flush();
423 P.printVersion(DllName, Major, Minor, DS.getPdbDllVersion());
424
Zachary Turnerc59261c2016-05-25 03:53:16 +0000425 if (DumpModules) {
Zachary Turner96e60f72016-05-24 20:31:48 +0000426 ListScope L(P, "Modules");
427 for (auto &Modi : DS.modules()) {
428 DictScope DD(P);
429 P.printString("Name", Modi.Info.getModuleName());
430 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
431 P.printString("Object File Name", Modi.Info.getObjFileName());
432 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
433 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
434 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
435 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
436 P.printNumber("C13 Line Info Byte Size",
437 Modi.Info.getC13LineInfoByteSize());
438 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
439 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
440 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
441 if (opts::DumpModuleFiles) {
442 std::string FileListName =
443 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
444 ListScope LL(P, FileListName);
445 for (auto File : Modi.SourceFiles)
446 P.printString(File);
447 }
448 bool HasModuleDI =
449 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
450 bool ShouldDumpSymbols =
451 (opts::DumpModuleSyms || opts::DumpSymRecordBytes);
452 if (HasModuleDI && ShouldDumpSymbols) {
453 ListScope SS(P, "Symbols");
454 ModStream ModS(File, Modi.Info);
455 if (auto EC = ModS.reload())
456 return EC;
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000457
Zachary Turner96e60f72016-05-24 20:31:48 +0000458 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
459 for (auto &S : ModS.symbols()) {
460 DictScope DD(P, "");
Zachary Turnercac29ae2016-05-24 17:30:25 +0000461
Zachary Turner96e60f72016-05-24 20:31:48 +0000462 if (opts::DumpModuleSyms)
463 SD.dump(S);
464 if (opts::DumpSymRecordBytes)
465 P.printBinaryBlock("Bytes", S.Data);
466 }
Zachary Turner06c2b4b2016-05-09 17:45:21 +0000467 }
468 }
Chad Rosier20dbbf32016-05-04 15:25:06 +0000469 }
Zachary Turner819e77d2016-05-06 20:51:57 +0000470 return Error::success();
Reid Klecknerb0345262016-05-04 16:09:04 +0000471}
472
Zachary Turnercac29ae2016-05-24 17:30:25 +0000473static Error dumpTpiStream(ScopedPrinter &P, PDBFile &File,
Zachary Turnerc9972c62016-05-25 04:35:22 +0000474 codeview::CVTypeDumper &TD, uint32_t StreamIdx) {
475 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
Reid Klecknerb0345262016-05-04 16:09:04 +0000476
Zachary Turnerc9972c62016-05-25 04:35:22 +0000477 bool DumpRecordBytes = false;
478 bool DumpRecords = false;
479 StringRef Label;
480 StringRef VerLabel;
481 if (StreamIdx == StreamTPI) {
482 DumpRecordBytes = opts::DumpTpiRecordBytes;
483 DumpRecords = opts::DumpTpiRecordBytes;
484 Label = "Type Info Stream (TPI)";
485 VerLabel = "TPI Version";
486 } else if (StreamIdx == StreamIPI) {
487 DumpRecordBytes = opts::DumpIpiRecordBytes;
488 DumpRecords = opts::DumpIpiRecords;
489 Label = "Type Info Stream (IPI)";
490 VerLabel = "IPI Version";
491 }
492 if (!DumpRecordBytes && !DumpRecords && !opts::DumpModuleSyms)
493 return Error::success();
Zachary Turnercac29ae2016-05-24 17:30:25 +0000494
Zachary Turnerc9972c62016-05-25 04:35:22 +0000495 auto TpiS = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
496 : File.getPDBIpiStream();
Zachary Turnercac29ae2016-05-24 17:30:25 +0000497 if (auto EC = TpiS.takeError())
498 return EC;
499 TpiStream &Tpi = TpiS.get();
500
Zachary Turnerc9972c62016-05-25 04:35:22 +0000501 if (DumpRecords || DumpRecordBytes) {
502 DictScope D(P, Label);
503
504 P.printNumber(VerLabel, Tpi.getTpiVersion());
Zachary Turnercac29ae2016-05-24 17:30:25 +0000505 P.printNumber("Record count", Tpi.NumTypeRecords());
506
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000507 ListScope L(P, "Records");
Reid Klecknerb0345262016-05-04 16:09:04 +0000508
Reid Klecknerce5196e2016-05-12 23:26:23 +0000509 bool HadError = false;
510 for (auto &Type : Tpi.types(&HadError)) {
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000511 DictScope DD(P, "");
512
Zachary Turnerc9972c62016-05-25 04:35:22 +0000513 if (DumpRecords)
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000514 TD.dump(Type);
515
Zachary Turnerc9972c62016-05-25 04:35:22 +0000516 if (DumpRecordBytes)
Zachary Turner9073ed62016-05-09 17:44:58 +0000517 P.printBinaryBlock("Bytes", Type.Data);
Zachary Turner5a1b5ef2016-05-06 22:15:42 +0000518 }
Reid Klecknerce5196e2016-05-12 23:26:23 +0000519 if (HadError)
520 return make_error<RawError>(raw_error_code::corrupt_file,
521 "TPI stream contained corrupt record");
Zachary Turnercac29ae2016-05-24 17:30:25 +0000522 } else if (opts::DumpModuleSyms) {
523 // Even if the user doesn't want to dump type records, we still need to
524 // iterate them in order to build the list of types so that we can print
525 // them when dumping module symbols. So when they want to dump symbols
526 // but not types, use a null output stream.
527 ScopedPrinter *OldP = TD.getPrinter();
528 TD.setPrinter(nullptr);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000529
Zachary Turnercac29ae2016-05-24 17:30:25 +0000530 bool HadError = false;
531 for (auto &Type : Tpi.types(&HadError))
532 TD.dump(Type);
533
534 TD.setPrinter(OldP);
535 if (HadError)
536 return make_error<RawError>(raw_error_code::corrupt_file,
537 "TPI stream contained corrupt record");
Chad Rosier20dbbf32016-05-04 15:25:06 +0000538 }
Zachary Turnerc9972c62016-05-25 04:35:22 +0000539 P.flush();
Zachary Turner819e77d2016-05-06 20:51:57 +0000540 return Error::success();
Zachary Turnercdd313c2016-05-04 15:05:12 +0000541}
542
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000543static Error dumpPublicsStream(ScopedPrinter &P, PDBFile &File,
544 codeview::CVTypeDumper &TD) {
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000545 if (!opts::DumpPublics)
546 return Error::success();
547
548 DictScope D(P, "Publics Stream");
549 auto PublicsS = File.getPDBPublicsStream();
550 if (auto EC = PublicsS.takeError())
551 return EC;
552 PublicsStream &Publics = PublicsS.get();
553 P.printNumber("Stream number", Publics.getStreamNum());
554 P.printNumber("SymHash", Publics.getSymHash());
555 P.printNumber("AddrMap", Publics.getAddrMap());
556 P.printNumber("Number of buckets", Publics.getNumBuckets());
Rui Ueyama8dc18c52016-05-17 23:07:48 +0000557 P.printList("Hash Buckets", Publics.getHashBuckets());
558 P.printList("Address Map", Publics.getAddressMap());
559 P.printList("Thunk Map", Publics.getThunkMap());
Rui Ueyama350b2982016-05-18 16:24:16 +0000560 P.printList("Section Offsets", Publics.getSectionOffsets());
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000561 ListScope L(P, "Symbols");
562 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
563 for (auto S : Publics.getSymbols()) {
564 DictScope DD(P, "");
565
566 SD.dump(S);
567 if (opts::DumpSymRecordBytes)
568 P.printBinaryBlock("Bytes", S.Data);
569 }
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000570 return Error::success();
571}
572
Zachary Turner819e77d2016-05-06 20:51:57 +0000573static Error dumpStructure(RawSession &RS) {
Reid Klecknerb0345262016-05-04 16:09:04 +0000574 PDBFile &File = RS.getPDBFile();
575 ScopedPrinter P(outs());
576
Zachary Turner819e77d2016-05-06 20:51:57 +0000577 if (auto EC = dumpFileHeaders(P, File))
578 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000579
Zachary Turner85ed80b2016-05-25 03:43:17 +0000580 if (auto EC = dumpStreamSummary(P, File))
Zachary Turner819e77d2016-05-06 20:51:57 +0000581 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000582
Zachary Turner819e77d2016-05-06 20:51:57 +0000583 if (auto EC = dumpStreamBlocks(P, File))
584 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000585
Zachary Turner819e77d2016-05-06 20:51:57 +0000586 if (auto EC = dumpStreamData(P, File))
587 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000588
Zachary Turner819e77d2016-05-06 20:51:57 +0000589 if (auto EC = dumpInfoStream(P, File))
590 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000591
Zachary Turner96e60f72016-05-24 20:31:48 +0000592 if (auto EC = dumpNamedStream(P, File))
Zachary Turner819e77d2016-05-06 20:51:57 +0000593 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000594
Zachary Turnercac29ae2016-05-24 17:30:25 +0000595 codeview::CVTypeDumper TD(P, false);
Zachary Turnerc9972c62016-05-25 04:35:22 +0000596 if (auto EC = dumpTpiStream(P, File, TD, StreamTPI))
597 return EC;
598 if (auto EC = dumpTpiStream(P, File, TD, StreamIPI))
Zachary Turner819e77d2016-05-06 20:51:57 +0000599 return EC;
Reid Klecknerb0345262016-05-04 16:09:04 +0000600
Zachary Turnercac29ae2016-05-24 17:30:25 +0000601 if (auto EC = dumpDbiStream(P, File, TD))
Zachary Turner819e77d2016-05-06 20:51:57 +0000602 return EC;
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000603
Zachary Turner9e33e6f2016-05-24 18:55:14 +0000604 if (auto EC = dumpPublicsStream(P, File, TD))
Rui Ueyama1f6b6e22016-05-13 21:21:53 +0000605 return EC;
Zachary Turner96e60f72016-05-24 20:31:48 +0000606 return Error::success();
607}
608
609bool isRawDumpEnabled() {
610 if (opts::DumpHeaders)
611 return true;
612 if (opts::DumpModules)
613 return true;
614 if (opts::DumpModuleFiles)
615 return true;
616 if (opts::DumpModuleSyms)
617 return true;
618 if (!opts::DumpStreamDataIdx.empty())
619 return true;
620 if (!opts::DumpStreamDataName.empty())
621 return true;
622 if (opts::DumpPublics)
623 return true;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000624 if (opts::DumpStreamSummary)
Zachary Turner96e60f72016-05-24 20:31:48 +0000625 return true;
Zachary Turner85ed80b2016-05-25 03:43:17 +0000626 if (opts::DumpStreamBlocks)
Zachary Turner96e60f72016-05-24 20:31:48 +0000627 return true;
628 if (opts::DumpSymRecordBytes)
629 return true;
630 if (opts::DumpTpiRecordBytes)
631 return true;
632 if (opts::DumpTpiRecords)
633 return true;
Zachary Turnerc9972c62016-05-25 04:35:22 +0000634 if (opts::DumpIpiRecords)
635 return true;
636 if (opts::DumpIpiRecordBytes)
637 return true;
Zachary Turner96e60f72016-05-24 20:31:48 +0000638 return false;
David Majnemer1573b242016-04-28 23:47:27 +0000639}
640
641static void dumpInput(StringRef Path) {
642 std::unique_ptr<IPDBSession> Session;
Zachary Turner96e60f72016-05-24 20:31:48 +0000643 if (isRawDumpEnabled()) {
Zachary Turner819e77d2016-05-06 20:51:57 +0000644 auto E = loadDataForPDB(PDB_ReaderType::Raw, Path, Session);
645 if (!E) {
David Majnemer1573b242016-04-28 23:47:27 +0000646 RawSession *RS = static_cast<RawSession *>(Session.get());
Zachary Turner819e77d2016-05-06 20:51:57 +0000647 E = dumpStructure(*RS);
David Majnemer1573b242016-04-28 23:47:27 +0000648 }
649
Zachary Turner819e77d2016-05-06 20:51:57 +0000650 if (E)
651 logAllUnhandledErrors(std::move(E), outs(), "");
652
David Majnemer1573b242016-04-28 23:47:27 +0000653 return;
654 }
655
Zachary Turner819e77d2016-05-06 20:51:57 +0000656 Error E = loadDataForPDB(PDB_ReaderType::DIA, Path, Session);
657 if (E) {
658 logAllUnhandledErrors(std::move(E), outs(), "");
David Majnemer1573b242016-04-28 23:47:27 +0000659 return;
660 }
661
Zachary Turnere5cb2692015-05-01 20:24:26 +0000662 if (opts::LoadAddress)
663 Session->setLoadAddress(opts::LoadAddress);
Zachary Turner7058dfc2015-01-27 22:40:14 +0000664
Zachary Turner2d11c202015-02-27 09:15:59 +0000665 LinePrinter Printer(2, outs());
666
Zachary Turnera5549172015-02-10 22:43:25 +0000667 auto GlobalScope(Session->getGlobalScope());
Zachary Turner9a818ad2015-02-22 22:03:38 +0000668 std::string FileName(GlobalScope->getSymbolsFileName());
669
Zachary Turner2d11c202015-02-27 09:15:59 +0000670 WithColor(Printer, PDB_ColorItem::None).get() << "Summary for ";
671 WithColor(Printer, PDB_ColorItem::Path).get() << FileName;
672 Printer.Indent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000673 uint64_t FileSize = 0;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000674
Zachary Turner2d11c202015-02-27 09:15:59 +0000675 Printer.NewLine();
676 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Size";
David Majnemer6e081262015-10-15 01:27:19 +0000677 if (!sys::fs::file_size(FileName, FileSize)) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000678 Printer << ": " << FileSize << " bytes";
679 } else {
680 Printer << ": (Unable to obtain file size)";
681 }
682
683 Printer.NewLine();
684 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Guid";
685 Printer << ": " << GlobalScope->getGuid();
686
687 Printer.NewLine();
688 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Age";
689 Printer << ": " << GlobalScope->getAge();
690
691 Printer.NewLine();
692 WithColor(Printer, PDB_ColorItem::Identifier).get() << "Attributes";
693 Printer << ": ";
Zachary Turner9a818ad2015-02-22 22:03:38 +0000694 if (GlobalScope->hasCTypes())
695 outs() << "HasCTypes ";
696 if (GlobalScope->hasPrivateSymbols())
697 outs() << "HasPrivateSymbols ";
Zachary Turner2d11c202015-02-27 09:15:59 +0000698 Printer.Unindent();
Zachary Turner9a818ad2015-02-22 22:03:38 +0000699
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000700 if (opts::Compilands) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000701 Printer.NewLine();
702 WithColor(Printer, PDB_ColorItem::SectionHeader).get()
703 << "---COMPILANDS---";
704 Printer.Indent();
Zachary Turnerc074de02015-02-12 21:09:24 +0000705 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000706 CompilandDumper Dumper(Printer);
Zachary Turnera99000d2016-03-08 21:42:24 +0000707 CompilandDumpFlags options = CompilandDumper::Flags::None;
708 if (opts::Lines)
709 options = options | CompilandDumper::Flags::Lines;
Zachary Turner9a818ad2015-02-22 22:03:38 +0000710 while (auto Compiland = Compilands->getNext())
Zachary Turnera99000d2016-03-08 21:42:24 +0000711 Dumper.start(*Compiland, options);
Zachary Turner2d11c202015-02-27 09:15:59 +0000712 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000713 }
714
715 if (opts::Types) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000716 Printer.NewLine();
717 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---TYPES---";
718 Printer.Indent();
Zachary Turner65323652015-03-04 06:09:53 +0000719 TypeDumper Dumper(Printer);
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000720 Dumper.start(*GlobalScope);
Zachary Turner2d11c202015-02-27 09:15:59 +0000721 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000722 }
723
724 if (opts::Symbols) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000725 Printer.NewLine();
726 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---SYMBOLS---";
727 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000728 auto Compilands = GlobalScope->findAllChildren<PDBSymbolCompiland>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000729 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000730 while (auto Compiland = Compilands->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000731 Dumper.start(*Compiland, true);
Zachary Turner2d11c202015-02-27 09:15:59 +0000732 Printer.Unindent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000733 }
734
735 if (opts::Globals) {
Zachary Turner2d11c202015-02-27 09:15:59 +0000736 Printer.NewLine();
737 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---GLOBALS---";
738 Printer.Indent();
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000739 {
Zachary Turner2d11c202015-02-27 09:15:59 +0000740 FunctionDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000741 auto Functions = GlobalScope->findAllChildren<PDBSymbolFunc>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000742 while (auto Function = Functions->getNext()) {
743 Printer.NewLine();
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000744 Dumper.start(*Function, FunctionDumper::PointerType::None);
Zachary Turner2d11c202015-02-27 09:15:59 +0000745 }
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000746 }
747 {
748 auto Vars = GlobalScope->findAllChildren<PDBSymbolData>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000749 VariableDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000750 while (auto Var = Vars->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000751 Dumper.start(*Var);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000752 }
753 {
754 auto Thunks = GlobalScope->findAllChildren<PDBSymbolThunk>();
Zachary Turner2d11c202015-02-27 09:15:59 +0000755 CompilandDumper Dumper(Printer);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000756 while (auto Thunk = Thunks->getNext())
Zachary Turnerb52d08d2015-03-01 06:51:29 +0000757 Dumper.dump(*Thunk);
Zachary Turnerdb18f5c2015-02-27 09:15:18 +0000758 }
Zachary Turner2d11c202015-02-27 09:15:59 +0000759 Printer.Unindent();
Zachary Turner7058dfc2015-01-27 22:40:14 +0000760 }
Zachary Turnere5cb2692015-05-01 20:24:26 +0000761 if (opts::Externals) {
762 Printer.NewLine();
763 WithColor(Printer, PDB_ColorItem::SectionHeader).get() << "---EXTERNALS---";
764 Printer.Indent();
765 ExternalSymbolDumper Dumper(Printer);
766 Dumper.start(*GlobalScope);
767 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000768 if (opts::Lines) {
769 Printer.NewLine();
770 }
Zachary Turnera5549172015-02-10 22:43:25 +0000771 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000772}
773
774int main(int argc_, const char *argv_[]) {
775 // Print a stack trace if we signal out.
776 sys::PrintStackTraceOnErrorSignal();
777 PrettyStackTraceProgram X(argc_, argv_);
778
779 SmallVector<const char *, 256> argv;
David Majnemer6e081262015-10-15 01:27:19 +0000780 SpecificBumpPtrAllocator<char> ArgAllocator;
781 std::error_code EC = sys::Process::GetArgumentVector(
782 argv, makeArrayRef(argv_, argc_), ArgAllocator);
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000783 if (EC) {
David Majnemer6e081262015-10-15 01:27:19 +0000784 errs() << "error: couldn't get arguments: " << EC.message() << '\n';
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000785 return 1;
786 }
787
788 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
789
790 cl::ParseCommandLineOptions(argv.size(), argv.data(), "LLVM PDB Dumper\n");
Zachary Turnera99000d2016-03-08 21:42:24 +0000791 if (opts::Lines)
792 opts::Compilands = true;
793
Zachary Turner7797c722015-03-02 04:39:56 +0000794 if (opts::All) {
795 opts::Compilands = true;
796 opts::Symbols = true;
797 opts::Globals = true;
798 opts::Types = true;
Zachary Turnere5cb2692015-05-01 20:24:26 +0000799 opts::Externals = true;
Zachary Turnera99000d2016-03-08 21:42:24 +0000800 opts::Lines = true;
Zachary Turner7797c722015-03-02 04:39:56 +0000801 }
Zachary Turnera99000d2016-03-08 21:42:24 +0000802
803 // When adding filters for excluded compilands and types, we need to remember
804 // that these are regexes. So special characters such as * and \ need to be
805 // escaped in the regex. In the case of a literal \, this means it needs to
806 // be escaped again in the C++. So matching a single \ in the input requires
807 // 4 \es in the C++.
Zachary Turner7797c722015-03-02 04:39:56 +0000808 if (opts::ExcludeCompilerGenerated) {
809 opts::ExcludeTypes.push_back("__vc_attributes");
Zachary Turnera99000d2016-03-08 21:42:24 +0000810 opts::ExcludeCompilands.push_back("\\* Linker \\*");
Zachary Turner7797c722015-03-02 04:39:56 +0000811 }
812 if (opts::ExcludeSystemLibraries) {
813 opts::ExcludeCompilands.push_back(
Zachary Turnera99000d2016-03-08 21:42:24 +0000814 "f:\\\\binaries\\\\Intermediate\\\\vctools\\\\crt_bld");
815 opts::ExcludeCompilands.push_back("f:\\\\dd\\\\vctools\\\\crt");
816 opts::ExcludeCompilands.push_back("d:\\\\th.obj.x86fre\\\\minkernel");
Zachary Turner7797c722015-03-02 04:39:56 +0000817 }
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000818
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000819#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000820 CoInitializeEx(nullptr, COINIT_MULTITHREADED);
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000821#endif
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000822
823 std::for_each(opts::InputFilenames.begin(), opts::InputFilenames.end(),
824 dumpInput);
825
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000826#if defined(HAVE_DIA_SDK)
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000827 CoUninitialize();
Zachary Turner8d7fa9b2015-02-10 22:47:14 +0000828#endif
Zachary Turner819e77d2016-05-06 20:51:57 +0000829 outs().flush();
Zachary Turnerfcb14ad2015-01-27 20:46:21 +0000830 return 0;
831}