blob: 6c39044dbbd76fb2a1deed6e8fe3fe937922dcc2 [file] [log] [blame]
Zachary Turnerd3117392016-06-03 19:28:33 +00001//===- LLVMOutputStyle.cpp ------------------------------------ *- 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#include "LLVMOutputStyle.h"
11
12#include "llvm-pdbdump.h"
13#include "llvm/DebugInfo/CodeView/EnumTables.h"
14#include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
15#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000016#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17#include "llvm/DebugInfo/MSF/StreamReader.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000018#include "llvm/DebugInfo/PDB/PDBExtras.h"
19#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Raw/EnumTables.h"
21#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
22#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
23#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
24#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
25#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
26#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
27#include "llvm/DebugInfo/PDB/Raw/RawError.h"
28#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
29#include "llvm/Object/COFF.h"
30
31#include <unordered_map>
32
33using namespace llvm;
34using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000035using namespace llvm::msf;
Zachary Turnerd3117392016-06-03 19:28:33 +000036using namespace llvm::pdb;
37
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +000038namespace {
39struct PageStats {
40 explicit PageStats(const BitVector &FreePages)
41 : Upm(FreePages), ActualUsedPages(FreePages.size()),
42 MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) {
43 const_cast<BitVector &>(Upm).flip();
44 // To calculate orphaned pages, we start with the set of pages that the
45 // MSF thinks are used. Each time we find one that actually *is* used,
46 // we unset it. Whichever bits remain set at the end are orphaned.
47 OrphanedPages = Upm;
48 }
49
50 // The inverse of the MSF File's copy of the Fpm. The basis for which we
51 // determine the allocation status of each page.
52 const BitVector Upm;
53
54 // Pages which are marked as used in the FPM and are used at least once.
55 BitVector ActualUsedPages;
56
57 // Pages which are marked as used in the FPM but are used more than once.
58 BitVector MultiUsePages;
59
60 // Pages which are marked as used in the FPM but are not used at all.
61 BitVector OrphanedPages;
62
63 // Pages which are marked free in the FPM but are used.
64 BitVector UseAfterFreePages;
65};
66}
67
68static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) {
69 if (Stats.Upm.test(UsedIndex)) {
70 if (Stats.ActualUsedPages.test(UsedIndex))
71 Stats.MultiUsePages.set(UsedIndex);
72 Stats.ActualUsedPages.set(UsedIndex);
73 Stats.OrphanedPages.reset(UsedIndex);
74 } else {
75 // The MSF doesn't think this page is used, but it is.
76 Stats.UseAfterFreePages.set(UsedIndex);
77 }
78}
79
Zachary Turnerd3117392016-06-03 19:28:33 +000080static void printSectionOffset(llvm::raw_ostream &OS,
81 const SectionOffset &Off) {
82 OS << Off.Off << ", " << Off.Isect;
83}
84
85LLVMOutputStyle::LLVMOutputStyle(PDBFile &File)
86 : File(File), P(outs()), TD(&P, false) {}
87
Zachary Turnera30bd1a2016-06-30 17:42:48 +000088Error LLVMOutputStyle::dump() {
89 if (auto EC = dumpFileHeaders())
90 return EC;
91
92 if (auto EC = dumpStreamSummary())
93 return EC;
94
Rui Ueyama7a5cdc62016-07-29 21:38:00 +000095 if (auto EC = dumpFreePageMap())
96 return EC;
97
Zachary Turnera30bd1a2016-06-30 17:42:48 +000098 if (auto EC = dumpStreamBlocks())
99 return EC;
100
101 if (auto EC = dumpStreamData())
102 return EC;
103
104 if (auto EC = dumpInfoStream())
105 return EC;
106
107 if (auto EC = dumpNamedStream())
108 return EC;
109
110 if (auto EC = dumpTpiStream(StreamTPI))
111 return EC;
112
113 if (auto EC = dumpTpiStream(StreamIPI))
114 return EC;
115
116 if (auto EC = dumpDbiStream())
117 return EC;
118
119 if (auto EC = dumpSectionContribs())
120 return EC;
121
122 if (auto EC = dumpSectionMap())
123 return EC;
124
125 if (auto EC = dumpPublicsStream())
126 return EC;
127
128 if (auto EC = dumpSectionHeaders())
129 return EC;
130
131 if (auto EC = dumpFpoStream())
132 return EC;
133
134 flush();
135
136 return Error::success();
137}
138
Zachary Turnerd3117392016-06-03 19:28:33 +0000139Error LLVMOutputStyle::dumpFileHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000140 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000141 return Error::success();
142
143 DictScope D(P, "FileHeaders");
144 P.printNumber("BlockSize", File.getBlockSize());
Zachary Turnerb927e022016-07-15 22:17:19 +0000145 P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock());
Zachary Turnerd3117392016-06-03 19:28:33 +0000146 P.printNumber("NumBlocks", File.getBlockCount());
147 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
148 P.printNumber("Unknown1", File.getUnknown1());
149 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
150 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
Zachary Turnerd3117392016-06-03 19:28:33 +0000151
152 // The directory is not contiguous. Instead, the block map contains a
153 // contiguous list of block numbers whose contents, when concatenated in
154 // order, make up the directory.
155 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
156 P.printNumber("NumStreams", File.getNumStreams());
157 return Error::success();
158}
159
160Error LLVMOutputStyle::dumpStreamSummary() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000161 if (!opts::raw::DumpStreamSummary)
Zachary Turnerd3117392016-06-03 19:28:33 +0000162 return Error::success();
163
Reid Kleckner11582c52016-06-17 20:38:01 +0000164 // It's OK if we fail to load some of these streams, we still attempt to print
165 // what we can.
Zachary Turnera1657a92016-06-08 17:26:39 +0000166 auto Dbi = File.getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000167 auto Tpi = File.getPDBTpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000168 auto Ipi = File.getPDBIpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000169 auto Info = File.getPDBInfoStream();
Zachary Turnerd3117392016-06-03 19:28:33 +0000170
171 ListScope L(P, "Streams");
172 uint32_t StreamCount = File.getNumStreams();
173 std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams;
174 std::unordered_map<uint16_t, std::string> NamedStreams;
175
Reid Kleckner11582c52016-06-17 20:38:01 +0000176 if (Dbi) {
177 for (auto &ModI : Dbi->modules()) {
178 uint16_t SN = ModI.Info.getModuleStreamIndex();
179 ModStreams[SN] = &ModI;
180 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000181 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000182 if (Info) {
183 for (auto &NSE : Info->named_streams()) {
184 NamedStreams[NSE.second] = NSE.first();
185 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000186 }
187
188 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
189 std::string Label("Stream ");
190 Label += to_string(StreamIdx);
191 std::string Value;
192 if (StreamIdx == OldMSFDirectory)
193 Value = "Old MSF Directory";
194 else if (StreamIdx == StreamPDB)
195 Value = "PDB Stream";
196 else if (StreamIdx == StreamDBI)
197 Value = "DBI Stream";
198 else if (StreamIdx == StreamTPI)
199 Value = "TPI Stream";
200 else if (StreamIdx == StreamIPI)
201 Value = "IPI Stream";
Reid Kleckner11582c52016-06-17 20:38:01 +0000202 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000203 Value = "Global Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000204 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000205 Value = "Public Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000206 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000207 Value = "Public Symbol Records";
Reid Kleckner11582c52016-06-17 20:38:01 +0000208 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000209 Value = "TPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000210 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000211 Value = "TPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000212 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000213 Value = "IPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000214 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000215 Value = "IPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000216 else if (Dbi &&
217 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
Zachary Turnerd3117392016-06-03 19:28:33 +0000218 Value = "Exception Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000219 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
Zachary Turnerd3117392016-06-03 19:28:33 +0000220 Value = "Fixup Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000221 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000222 Value = "FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000223 else if (Dbi &&
224 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000225 Value = "New FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000226 else if (Dbi &&
227 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000228 Value = "Omap From Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000229 else if (Dbi &&
230 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000231 Value = "Omap To Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000232 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000233 Value = "Pdata";
Reid Kleckner11582c52016-06-17 20:38:01 +0000234 else if (Dbi &&
235 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
Zachary Turnerd3117392016-06-03 19:28:33 +0000236 Value = "Section Header Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000237 else if (Dbi &&
238 StreamIdx ==
239 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
Zachary Turnerd3117392016-06-03 19:28:33 +0000240 Value = "Section Header Original Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000241 else if (Dbi &&
242 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
Zachary Turnerd3117392016-06-03 19:28:33 +0000243 Value = "Token Rid Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000244 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000245 Value = "Xdata";
246 else {
247 auto ModIter = ModStreams.find(StreamIdx);
248 auto NSIter = NamedStreams.find(StreamIdx);
249 if (ModIter != ModStreams.end()) {
250 Value = "Module \"";
251 Value += ModIter->second->Info.getModuleName().str();
252 Value += "\"";
253 } else if (NSIter != NamedStreams.end()) {
254 Value = "Named Stream \"";
255 Value += NSIter->second;
256 Value += "\"";
257 } else {
258 Value = "???";
259 }
260 }
261 Value = "[" + Value + "]";
262 Value =
263 Value + " (" + to_string(File.getStreamByteSize(StreamIdx)) + " bytes)";
264
265 P.printString(Label, Value);
266 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000267
268 // Consume errors from missing streams.
269 if (!Dbi)
270 consumeError(Dbi.takeError());
271 if (!Tpi)
272 consumeError(Tpi.takeError());
273 if (!Ipi)
274 consumeError(Ipi.takeError());
275 if (!Info)
276 consumeError(Info.takeError());
277
Zachary Turnerd3117392016-06-03 19:28:33 +0000278 P.flush();
279 return Error::success();
280}
281
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000282Error LLVMOutputStyle::dumpFreePageMap() {
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000283 if (!opts::raw::DumpPageStats)
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000284 return Error::success();
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000285
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000286 // Start with used pages instead of free pages because
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000287 // the number of free pages is far larger than used pages.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000288 BitVector FPM = File.getMsfLayout().FreePageMap;
289
290 PageStats PS(FPM);
291
292 recordKnownUsedPage(PS, 0); // MSF Super Block
293
294 uint32_t BlocksPerSection = File.getBlockSize();
295 uint32_t NumSections =
296 llvm::alignTo(File.getBlockCount(), BlocksPerSection) / BlocksPerSection;
297 for (uint32_t I = 0; I < NumSections; ++I) {
298 uint32_t Fpm0 = 1 + BlocksPerSection * I;
299 // 2 Fpm blocks spaced at `getBlockSize()` block intervals
300 recordKnownUsedPage(PS, Fpm0);
301 recordKnownUsedPage(PS, Fpm0 + 1);
302 }
303
304 recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table
305
306 for (auto DB : File.getDirectoryBlockArray()) {
307 recordKnownUsedPage(PS, DB);
308 }
309 for (auto &SE : File.getStreamMap()) {
310 for (auto &S : SE) {
311 recordKnownUsedPage(PS, S);
312 }
313 }
314
315 dumpBitVector("Msf Free Pages", FPM);
316 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
317 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
318 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000319 return Error::success();
320}
321
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000322void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
323 std::vector<uint32_t> Vec;
324 for (uint32_t I = 0, E = V.size(); I != E; ++I)
325 if (V[I])
326 Vec.push_back(I);
327 P.printList(Name, Vec);
328}
329
Zachary Turnerd3117392016-06-03 19:28:33 +0000330Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000331 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000332 return Error::success();
333
334 ListScope L(P, "StreamBlocks");
335 uint32_t StreamCount = File.getNumStreams();
336 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
337 std::string Name("Stream ");
338 Name += to_string(StreamIdx);
339 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
340 P.printList(Name, StreamBlocks);
341 }
342 return Error::success();
343}
344
345Error LLVMOutputStyle::dumpStreamData() {
346 uint32_t StreamCount = File.getNumStreams();
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000347 StringRef DumpStreamStr = opts::raw::DumpStreamDataIdx;
Zachary Turnerd3117392016-06-03 19:28:33 +0000348 uint32_t DumpStreamNum;
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000349 if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum))
Zachary Turnerd3117392016-06-03 19:28:33 +0000350 return Error::success();
351
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000352 if (DumpStreamNum >= StreamCount)
353 return make_error<RawError>(raw_error_code::no_stream);
354
Zachary Turnerd66889c2016-07-28 19:12:28 +0000355 auto S = MappedBlockStream::createIndexedStream(
356 File.getMsfLayout(), File.getMsfBuffer(), DumpStreamNum);
357 StreamReader R(*S);
Zachary Turnerd3117392016-06-03 19:28:33 +0000358 while (R.bytesRemaining() > 0) {
359 ArrayRef<uint8_t> Data;
360 uint32_t BytesToReadInBlock = std::min(
361 R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize()));
362 if (auto EC = R.readBytes(Data, BytesToReadInBlock))
363 return EC;
364 P.printBinaryBlock(
365 "Data",
366 StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size()));
367 }
368 return Error::success();
369}
370
371Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000372 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000373 return Error::success();
Zachary Turnera1657a92016-06-08 17:26:39 +0000374 auto IS = File.getPDBInfoStream();
375 if (!IS)
376 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000377
378 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000379 P.printNumber("Version", IS->getVersion());
380 P.printHex("Signature", IS->getSignature());
381 P.printNumber("Age", IS->getAge());
382 P.printObject("Guid", IS->getGuid());
Zachary Turnerd3117392016-06-03 19:28:33 +0000383 return Error::success();
384}
385
386Error LLVMOutputStyle::dumpNamedStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000387 if (opts::raw::DumpStreamDataName.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000388 return Error::success();
389
Zachary Turnera1657a92016-06-08 17:26:39 +0000390 auto IS = File.getPDBInfoStream();
391 if (!IS)
392 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000393
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000394 uint32_t NameStreamIndex =
395 IS->getNamedStreamIndex(opts::raw::DumpStreamDataName);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000396 if (NameStreamIndex == 0 || NameStreamIndex >= File.getNumStreams())
397 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000398
399 if (NameStreamIndex != 0) {
400 std::string Name("Stream '");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000401 Name += opts::raw::DumpStreamDataName;
Zachary Turnerd3117392016-06-03 19:28:33 +0000402 Name += "'";
403 DictScope D(P, Name);
404 P.printNumber("Index", NameStreamIndex);
405
Zachary Turnerd66889c2016-07-28 19:12:28 +0000406 auto NameStream = MappedBlockStream::createIndexedStream(
407 File.getMsfLayout(), File.getMsfBuffer(), NameStreamIndex);
408 StreamReader Reader(*NameStream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000409
410 NameHashTable NameTable;
411 if (auto EC = NameTable.load(Reader))
412 return EC;
413
414 P.printHex("Signature", NameTable.getSignature());
415 P.printNumber("Version", NameTable.getHashVersion());
416 P.printNumber("Name Count", NameTable.getNameCount());
417 ListScope L(P, "Names");
418 for (uint32_t ID : NameTable.name_ids()) {
419 StringRef Str = NameTable.getStringForID(ID);
420 if (!Str.empty())
421 P.printString(to_string(ID), Str);
422 }
423 }
424 return Error::success();
425}
426
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000427static void printTypeIndexOffset(raw_ostream &OS,
428 const TypeIndexOffset &TIOff) {
429 OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}";
430}
431
432static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000433 if (!opts::raw::DumpTpiHash)
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000434 return;
435 DictScope DD(P, "Hash");
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000436 P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets());
Rui Ueyamad8339172016-06-07 23:44:27 +0000437 P.printNumber("Hash Key Size", Tpi.getHashKeySize());
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000438 P.printList("Values", Tpi.getHashValues());
439 P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(),
440 printTypeIndexOffset);
441 P.printList("Hash Adjustments", Tpi.getHashAdjustments(),
442 printTypeIndexOffset);
443}
444
Zachary Turnerd3117392016-06-03 19:28:33 +0000445Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
446 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
447
448 bool DumpRecordBytes = false;
449 bool DumpRecords = false;
450 StringRef Label;
451 StringRef VerLabel;
452 if (StreamIdx == StreamTPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000453 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
454 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000455 Label = "Type Info Stream (TPI)";
456 VerLabel = "TPI Version";
457 } else if (StreamIdx == StreamIPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000458 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
459 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000460 Label = "Type Info Stream (IPI)";
461 VerLabel = "IPI Version";
462 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000463 if (!DumpRecordBytes && !DumpRecords && !opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000464 return Error::success();
465
Zachary Turnera1657a92016-06-08 17:26:39 +0000466 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
467 : File.getPDBIpiStream();
468 if (!Tpi)
469 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000470
471 if (DumpRecords || DumpRecordBytes) {
472 DictScope D(P, Label);
473
Zachary Turnera1657a92016-06-08 17:26:39 +0000474 P.printNumber(VerLabel, Tpi->getTpiVersion());
475 P.printNumber("Record count", Tpi->NumTypeRecords());
Zachary Turnerd3117392016-06-03 19:28:33 +0000476
477 ListScope L(P, "Records");
478
479 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000480 for (auto &Type : Tpi->types(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000481 DictScope DD(P, "");
482
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000483 if (DumpRecords) {
484 if (auto EC = TD.dump(Type))
485 return EC;
486 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000487
488 if (DumpRecordBytes)
489 P.printBinaryBlock("Bytes", Type.Data);
490 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000491 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000492 if (HadError)
493 return make_error<RawError>(raw_error_code::corrupt_file,
494 "TPI stream contained corrupt record");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000495 } else if (opts::raw::DumpModuleSyms) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000496 // Even if the user doesn't want to dump type records, we still need to
497 // iterate them in order to build the list of types so that we can print
498 // them when dumping module symbols. So when they want to dump symbols
499 // but not types, use a null output stream.
500 ScopedPrinter *OldP = TD.getPrinter();
501 TD.setPrinter(nullptr);
502
503 bool HadError = false;
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000504 for (auto &Type : Tpi->types(&HadError)) {
505 if (auto EC = TD.dump(Type))
506 return EC;
507 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000508
509 TD.setPrinter(OldP);
Zachary Turnera1657a92016-06-08 17:26:39 +0000510 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000511 if (HadError)
512 return make_error<RawError>(raw_error_code::corrupt_file,
513 "TPI stream contained corrupt record");
514 }
515 P.flush();
516 return Error::success();
517}
518
519Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000520 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
521 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
522 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000523 return Error::success();
524
Zachary Turnera1657a92016-06-08 17:26:39 +0000525 auto DS = File.getPDBDbiStream();
526 if (!DS)
527 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000528
529 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000530 P.printNumber("Dbi Version", DS->getDbiVersion());
531 P.printNumber("Age", DS->getAge());
532 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
533 P.printBoolean("Has CTypes", DS->hasCTypes());
534 P.printBoolean("Is Stripped", DS->isStripped());
535 P.printObject("Machine Type", DS->getMachineType());
536 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
537 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
538 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000539
Zachary Turnera1657a92016-06-08 17:26:39 +0000540 uint16_t Major = DS->getBuildMajorVersion();
541 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000542 P.printVersion("Toolchain Version", Major, Minor);
543
544 std::string DllName;
545 raw_string_ostream DllStream(DllName);
546 DllStream << "mspdb" << Major << Minor << ".dll version";
547 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000548 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000549
550 if (DumpModules) {
551 ListScope L(P, "Modules");
Zachary Turnera1657a92016-06-08 17:26:39 +0000552 for (auto &Modi : DS->modules()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000553 DictScope DD(P);
554 P.printString("Name", Modi.Info.getModuleName().str());
555 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
556 P.printString("Object File Name", Modi.Info.getObjFileName().str());
557 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
558 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
559 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
560 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
561 P.printNumber("C13 Line Info Byte Size",
562 Modi.Info.getC13LineInfoByteSize());
563 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
564 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
565 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000566 if (opts::raw::DumpModuleFiles) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000567 std::string FileListName =
568 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
569 ListScope LL(P, FileListName);
570 for (auto File : Modi.SourceFiles)
571 P.printString(File.str());
572 }
573 bool HasModuleDI =
574 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
575 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000576 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
577 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000578 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000579 File.getMsfLayout(), File.getMsfBuffer(),
580 Modi.Info.getModuleStreamIndex());
581
582 ModStream ModS(Modi.Info, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000583 if (auto EC = ModS.reload())
584 return EC;
585
586 if (ShouldDumpSymbols) {
587 ListScope SS(P, "Symbols");
588 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
589 bool HadError = false;
590 for (const auto &S : ModS.symbols(&HadError)) {
591 DictScope DD(P, "");
592
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000593 if (opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000594 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000595 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000596 P.printBinaryBlock("Bytes", S.Data);
597 }
598 if (HadError)
599 return make_error<RawError>(
600 raw_error_code::corrupt_file,
601 "DBI stream contained corrupt symbol record");
602 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000603 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000604 ListScope SS(P, "LineInfo");
605 bool HadError = false;
606 // Define a locally scoped visitor to print the different
607 // substream types types.
608 class RecordVisitor : public codeview::IModuleSubstreamVisitor {
609 public:
610 RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {}
611 Error visitUnknown(ModuleSubstreamKind Kind,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000612 ReadableStreamRef Stream) override {
Zachary Turnerd3117392016-06-03 19:28:33 +0000613 DictScope DD(P, "Unknown");
614 ArrayRef<uint8_t> Data;
615 StreamReader R(Stream);
616 if (auto EC = R.readBytes(Data, R.bytesRemaining())) {
617 return make_error<RawError>(
618 raw_error_code::corrupt_file,
619 "DBI stream contained corrupt line info record");
620 }
621 P.printBinaryBlock("Data", Data);
622 return Error::success();
623 }
624 Error
Zachary Turnerd66889c2016-07-28 19:12:28 +0000625 visitFileChecksums(ReadableStreamRef Data,
Zachary Turnerd3117392016-06-03 19:28:33 +0000626 const FileChecksumArray &Checksums) override {
627 DictScope DD(P, "FileChecksums");
628 for (const auto &C : Checksums) {
629 DictScope DDD(P, "Checksum");
630 if (auto Result = getFileNameForOffset(C.FileNameOffset))
631 P.printString("FileName", Result.get());
632 else
633 return Result.takeError();
634 P.flush();
635 P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames());
636 P.printBinaryBlock("Checksum", C.Checksum);
637 }
638 return Error::success();
639 }
640
Zachary Turnerd66889c2016-07-28 19:12:28 +0000641 Error visitLines(ReadableStreamRef Data,
642 const LineSubstreamHeader *Header,
Zachary Turnerd3117392016-06-03 19:28:33 +0000643 const LineInfoArray &Lines) override {
644 DictScope DD(P, "Lines");
645 for (const auto &L : Lines) {
646 if (auto Result = getFileNameForOffset2(L.NameIndex))
647 P.printString("FileName", Result.get());
648 else
649 return Result.takeError();
650 P.flush();
651 for (const auto &N : L.LineNumbers) {
652 DictScope DDD(P, "Line");
653 LineInfo LI(N.Flags);
654 P.printNumber("Offset", N.Offset);
655 if (LI.isAlwaysStepInto())
656 P.printString("StepInto", StringRef("Always"));
657 else if (LI.isNeverStepInto())
658 P.printString("StepInto", StringRef("Never"));
659 else
660 P.printNumber("LineNumberStart", LI.getStartLine());
661 P.printNumber("EndDelta", LI.getLineDelta());
662 P.printBoolean("IsStatement", LI.isStatement());
663 }
664 for (const auto &C : L.Columns) {
665 DictScope DDD(P, "Column");
666 P.printNumber("Start", C.StartColumn);
667 P.printNumber("End", C.EndColumn);
668 }
669 }
670 return Error::success();
671 }
672
673 private:
674 Expected<StringRef> getFileNameForOffset(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000675 auto ST = F.getStringTable();
676 if (!ST)
677 return ST.takeError();
678
679 return ST->getStringForID(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000680 }
681 Expected<StringRef> getFileNameForOffset2(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000682 auto DS = F.getPDBDbiStream();
683 if (!DS)
684 return DS.takeError();
685 return DS->getFileNameForIndex(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000686 }
687 ScopedPrinter &P;
688 PDBFile &F;
689 };
690
691 RecordVisitor V(P, File);
692 for (const auto &L : ModS.lines(&HadError)) {
693 if (auto EC = codeview::visitModuleSubstream(L, V))
694 return EC;
695 }
696 }
697 }
698 }
699 }
700 return Error::success();
701}
702
703Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000704 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000705 return Error::success();
706
Zachary Turnera1657a92016-06-08 17:26:39 +0000707 auto Dbi = File.getPDBDbiStream();
708 if (!Dbi)
709 return Dbi.takeError();
710
Zachary Turnerd3117392016-06-03 19:28:33 +0000711 ListScope L(P, "Section Contributions");
712 class Visitor : public ISectionContribVisitor {
713 public:
714 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
715 void visit(const SectionContrib &SC) override {
716 DictScope D(P, "Contribution");
717 P.printNumber("ISect", SC.ISect);
718 P.printNumber("Off", SC.Off);
719 P.printNumber("Size", SC.Size);
720 P.printFlags("Characteristics", SC.Characteristics,
721 codeview::getImageSectionCharacteristicNames(),
722 COFF::SectionCharacteristics(0x00F00000));
723 {
724 DictScope DD(P, "Module");
725 P.printNumber("Index", SC.Imod);
726 auto M = DS.modules();
727 if (M.size() > SC.Imod) {
728 P.printString("Name", M[SC.Imod].Info.getModuleName());
729 }
730 }
731 P.printNumber("Data CRC", SC.DataCrc);
732 P.printNumber("Reloc CRC", SC.RelocCrc);
733 P.flush();
734 }
735 void visit(const SectionContrib2 &SC) override {
736 visit(SC.Base);
737 P.printNumber("ISect Coff", SC.ISectCoff);
738 P.flush();
739 }
740
741 private:
742 ScopedPrinter &P;
743 DbiStream &DS;
744 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000745 Visitor V(P, *Dbi);
746 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000747 return Error::success();
748}
749
750Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000751 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000752 return Error::success();
753
Zachary Turnera1657a92016-06-08 17:26:39 +0000754 auto Dbi = File.getPDBDbiStream();
755 if (!Dbi)
756 return Dbi.takeError();
757
Zachary Turnerd3117392016-06-03 19:28:33 +0000758 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000759 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000760 DictScope D(P, "Entry");
761 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
762 P.printNumber("Flags", M.Flags);
763 P.printNumber("Ovl", M.Ovl);
764 P.printNumber("Group", M.Group);
765 P.printNumber("Frame", M.Frame);
766 P.printNumber("SecName", M.SecName);
767 P.printNumber("ClassName", M.ClassName);
768 P.printNumber("Offset", M.Offset);
769 P.printNumber("SecByteLength", M.SecByteLength);
770 P.flush();
771 }
772 return Error::success();
773}
774
775Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000776 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000777 return Error::success();
778
779 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000780 auto Publics = File.getPDBPublicsStream();
781 if (!Publics)
782 return Publics.takeError();
783
784 auto Dbi = File.getPDBDbiStream();
785 if (!Dbi)
786 return Dbi.takeError();
787
788 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
789 P.printNumber("SymHash", Publics->getSymHash());
790 P.printNumber("AddrMap", Publics->getAddrMap());
791 P.printNumber("Number of buckets", Publics->getNumBuckets());
792 P.printList("Hash Buckets", Publics->getHashBuckets());
793 P.printList("Address Map", Publics->getAddressMap());
794 P.printList("Thunk Map", Publics->getThunkMap());
795 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000796 printSectionOffset);
797 ListScope L(P, "Symbols");
798 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
799 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000800 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000801 DictScope DD(P, "");
802
803 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000804 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000805 P.printBinaryBlock("Bytes", S.Data);
806 }
807 if (HadError)
808 return make_error<RawError>(
809 raw_error_code::corrupt_file,
810 "Public symbol stream contained corrupt record");
811
812 return Error::success();
813}
814
815Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000816 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000817 return Error::success();
818
Zachary Turnera1657a92016-06-08 17:26:39 +0000819 auto Dbi = File.getPDBDbiStream();
820 if (!Dbi)
821 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000822
823 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000824 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000825 DictScope DD(P, "");
826
827 // If a name is 8 characters long, there is no NUL character at end.
828 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
829 P.printString("Name", Name);
830 P.printNumber("Virtual Size", Section.VirtualSize);
831 P.printNumber("Virtual Address", Section.VirtualAddress);
832 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
833 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
834 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
835 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
836 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
837 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +0000838 P.printFlags("Characteristics", Section.Characteristics,
839 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000840 }
841 return Error::success();
842}
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000843
844Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000845 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000846 return Error::success();
847
Zachary Turnera1657a92016-06-08 17:26:39 +0000848 auto Dbi = File.getPDBDbiStream();
849 if (!Dbi)
850 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000851
852 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +0000853 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000854 DictScope DD(P, "");
855 P.printNumber("Offset", Fpo.Offset);
856 P.printNumber("Size", Fpo.Size);
857 P.printNumber("Number of locals", Fpo.NumLocals);
858 P.printNumber("Number of params", Fpo.NumParams);
859 P.printNumber("Size of Prolog", Fpo.getPrologSize());
860 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
861 P.printBoolean("Has SEH", Fpo.hasSEH());
862 P.printBoolean("Use BP", Fpo.useBP());
863 P.printNumber("Frame Pointer", Fpo.getFP());
864 }
865 return Error::success();
866}
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000867
Zachary Turner7120a472016-06-06 20:37:05 +0000868void LLVMOutputStyle::flush() { P.flush(); }