blob: 3018a70d4c0e9da5f1f6682f87a4196c5e8f8a4b [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
Rui Ueyama22e67382016-08-02 23:22:46 +0000306 for (auto DB : File.getDirectoryBlockArray())
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000307 recordKnownUsedPage(PS, DB);
Rui Ueyama22e67382016-08-02 23:22:46 +0000308
309 // Record pages used by streams. Note that pages for stream 0
310 // are considered being unused because that's what MSVC tools do.
311 // Stream 0 doesn't contain actual data, so it makes some sense,
312 // though it's a bit confusing to us.
313 for (auto &SE : File.getStreamMap().drop_front(1))
314 for (auto &S : SE)
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000315 recordKnownUsedPage(PS, S);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000316
317 dumpBitVector("Msf Free Pages", FPM);
318 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
319 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
320 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000321 return Error::success();
322}
323
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000324void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
325 std::vector<uint32_t> Vec;
326 for (uint32_t I = 0, E = V.size(); I != E; ++I)
327 if (V[I])
328 Vec.push_back(I);
329 P.printList(Name, Vec);
330}
331
Zachary Turnerd3117392016-06-03 19:28:33 +0000332Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000333 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000334 return Error::success();
335
336 ListScope L(P, "StreamBlocks");
337 uint32_t StreamCount = File.getNumStreams();
338 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
339 std::string Name("Stream ");
340 Name += to_string(StreamIdx);
341 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
342 P.printList(Name, StreamBlocks);
343 }
344 return Error::success();
345}
346
347Error LLVMOutputStyle::dumpStreamData() {
348 uint32_t StreamCount = File.getNumStreams();
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000349 StringRef DumpStreamStr = opts::raw::DumpStreamDataIdx;
Zachary Turnerd3117392016-06-03 19:28:33 +0000350 uint32_t DumpStreamNum;
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000351 if (DumpStreamStr.getAsInteger(/*Radix=*/0U, DumpStreamNum))
Zachary Turnerd3117392016-06-03 19:28:33 +0000352 return Error::success();
353
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000354 if (DumpStreamNum >= StreamCount)
355 return make_error<RawError>(raw_error_code::no_stream);
356
Zachary Turnerd66889c2016-07-28 19:12:28 +0000357 auto S = MappedBlockStream::createIndexedStream(
358 File.getMsfLayout(), File.getMsfBuffer(), DumpStreamNum);
359 StreamReader R(*S);
Zachary Turnerd3117392016-06-03 19:28:33 +0000360 while (R.bytesRemaining() > 0) {
361 ArrayRef<uint8_t> Data;
362 uint32_t BytesToReadInBlock = std::min(
363 R.bytesRemaining(), static_cast<uint32_t>(File.getBlockSize()));
364 if (auto EC = R.readBytes(Data, BytesToReadInBlock))
365 return EC;
366 P.printBinaryBlock(
367 "Data",
368 StringRef(reinterpret_cast<const char *>(Data.begin()), Data.size()));
369 }
370 return Error::success();
371}
372
373Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000374 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000375 return Error::success();
Zachary Turnera1657a92016-06-08 17:26:39 +0000376 auto IS = File.getPDBInfoStream();
377 if (!IS)
378 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000379
380 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000381 P.printNumber("Version", IS->getVersion());
382 P.printHex("Signature", IS->getSignature());
383 P.printNumber("Age", IS->getAge());
384 P.printObject("Guid", IS->getGuid());
Zachary Turnerd3117392016-06-03 19:28:33 +0000385 return Error::success();
386}
387
388Error LLVMOutputStyle::dumpNamedStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000389 if (opts::raw::DumpStreamDataName.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000390 return Error::success();
391
Zachary Turnera1657a92016-06-08 17:26:39 +0000392 auto IS = File.getPDBInfoStream();
393 if (!IS)
394 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000395
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000396 uint32_t NameStreamIndex =
397 IS->getNamedStreamIndex(opts::raw::DumpStreamDataName);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000398 if (NameStreamIndex == 0 || NameStreamIndex >= File.getNumStreams())
399 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000400
401 if (NameStreamIndex != 0) {
402 std::string Name("Stream '");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000403 Name += opts::raw::DumpStreamDataName;
Zachary Turnerd3117392016-06-03 19:28:33 +0000404 Name += "'";
405 DictScope D(P, Name);
406 P.printNumber("Index", NameStreamIndex);
407
Zachary Turnerd66889c2016-07-28 19:12:28 +0000408 auto NameStream = MappedBlockStream::createIndexedStream(
409 File.getMsfLayout(), File.getMsfBuffer(), NameStreamIndex);
410 StreamReader Reader(*NameStream);
Zachary Turnerd3117392016-06-03 19:28:33 +0000411
412 NameHashTable NameTable;
413 if (auto EC = NameTable.load(Reader))
414 return EC;
415
416 P.printHex("Signature", NameTable.getSignature());
417 P.printNumber("Version", NameTable.getHashVersion());
418 P.printNumber("Name Count", NameTable.getNameCount());
419 ListScope L(P, "Names");
420 for (uint32_t ID : NameTable.name_ids()) {
421 StringRef Str = NameTable.getStringForID(ID);
422 if (!Str.empty())
423 P.printString(to_string(ID), Str);
424 }
425 }
426 return Error::success();
427}
428
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000429static void printTypeIndexOffset(raw_ostream &OS,
430 const TypeIndexOffset &TIOff) {
431 OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}";
432}
433
434static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000435 if (!opts::raw::DumpTpiHash)
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000436 return;
437 DictScope DD(P, "Hash");
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000438 P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets());
Rui Ueyamad8339172016-06-07 23:44:27 +0000439 P.printNumber("Hash Key Size", Tpi.getHashKeySize());
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000440 P.printList("Values", Tpi.getHashValues());
441 P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(),
442 printTypeIndexOffset);
443 P.printList("Hash Adjustments", Tpi.getHashAdjustments(),
444 printTypeIndexOffset);
445}
446
Zachary Turnerd3117392016-06-03 19:28:33 +0000447Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
448 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
449
450 bool DumpRecordBytes = false;
451 bool DumpRecords = false;
452 StringRef Label;
453 StringRef VerLabel;
454 if (StreamIdx == StreamTPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000455 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
456 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000457 Label = "Type Info Stream (TPI)";
458 VerLabel = "TPI Version";
459 } else if (StreamIdx == StreamIPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000460 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
461 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000462 Label = "Type Info Stream (IPI)";
463 VerLabel = "IPI Version";
464 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000465 if (!DumpRecordBytes && !DumpRecords && !opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000466 return Error::success();
467
Zachary Turnera1657a92016-06-08 17:26:39 +0000468 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
469 : File.getPDBIpiStream();
470 if (!Tpi)
471 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000472
473 if (DumpRecords || DumpRecordBytes) {
474 DictScope D(P, Label);
475
Zachary Turnera1657a92016-06-08 17:26:39 +0000476 P.printNumber(VerLabel, Tpi->getTpiVersion());
477 P.printNumber("Record count", Tpi->NumTypeRecords());
Zachary Turnerd3117392016-06-03 19:28:33 +0000478
479 ListScope L(P, "Records");
480
481 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000482 for (auto &Type : Tpi->types(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000483 DictScope DD(P, "");
484
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000485 if (DumpRecords) {
486 if (auto EC = TD.dump(Type))
487 return EC;
488 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000489
490 if (DumpRecordBytes)
491 P.printBinaryBlock("Bytes", Type.Data);
492 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000493 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000494 if (HadError)
495 return make_error<RawError>(raw_error_code::corrupt_file,
496 "TPI stream contained corrupt record");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000497 } else if (opts::raw::DumpModuleSyms) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000498 // Even if the user doesn't want to dump type records, we still need to
499 // iterate them in order to build the list of types so that we can print
500 // them when dumping module symbols. So when they want to dump symbols
501 // but not types, use a null output stream.
502 ScopedPrinter *OldP = TD.getPrinter();
503 TD.setPrinter(nullptr);
504
505 bool HadError = false;
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000506 for (auto &Type : Tpi->types(&HadError)) {
507 if (auto EC = TD.dump(Type))
508 return EC;
509 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000510
511 TD.setPrinter(OldP);
Zachary Turnera1657a92016-06-08 17:26:39 +0000512 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000513 if (HadError)
514 return make_error<RawError>(raw_error_code::corrupt_file,
515 "TPI stream contained corrupt record");
516 }
517 P.flush();
518 return Error::success();
519}
520
521Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000522 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
523 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
524 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000525 return Error::success();
526
Zachary Turnera1657a92016-06-08 17:26:39 +0000527 auto DS = File.getPDBDbiStream();
528 if (!DS)
529 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000530
531 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000532 P.printNumber("Dbi Version", DS->getDbiVersion());
533 P.printNumber("Age", DS->getAge());
534 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
535 P.printBoolean("Has CTypes", DS->hasCTypes());
536 P.printBoolean("Is Stripped", DS->isStripped());
537 P.printObject("Machine Type", DS->getMachineType());
538 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
539 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
540 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000541
Zachary Turnera1657a92016-06-08 17:26:39 +0000542 uint16_t Major = DS->getBuildMajorVersion();
543 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000544 P.printVersion("Toolchain Version", Major, Minor);
545
546 std::string DllName;
547 raw_string_ostream DllStream(DllName);
548 DllStream << "mspdb" << Major << Minor << ".dll version";
549 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000550 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000551
552 if (DumpModules) {
553 ListScope L(P, "Modules");
Zachary Turnera1657a92016-06-08 17:26:39 +0000554 for (auto &Modi : DS->modules()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000555 DictScope DD(P);
556 P.printString("Name", Modi.Info.getModuleName().str());
557 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
558 P.printString("Object File Name", Modi.Info.getObjFileName().str());
559 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
560 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
561 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
562 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
563 P.printNumber("C13 Line Info Byte Size",
564 Modi.Info.getC13LineInfoByteSize());
565 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
566 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
567 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000568 if (opts::raw::DumpModuleFiles) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000569 std::string FileListName =
570 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
571 ListScope LL(P, FileListName);
572 for (auto File : Modi.SourceFiles)
573 P.printString(File.str());
574 }
575 bool HasModuleDI =
576 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
577 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000578 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
579 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000580 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000581 File.getMsfLayout(), File.getMsfBuffer(),
582 Modi.Info.getModuleStreamIndex());
583
584 ModStream ModS(Modi.Info, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000585 if (auto EC = ModS.reload())
586 return EC;
587
588 if (ShouldDumpSymbols) {
589 ListScope SS(P, "Symbols");
590 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
591 bool HadError = false;
592 for (const auto &S : ModS.symbols(&HadError)) {
593 DictScope DD(P, "");
594
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000595 if (opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000596 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000597 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000598 P.printBinaryBlock("Bytes", S.Data);
599 }
600 if (HadError)
601 return make_error<RawError>(
602 raw_error_code::corrupt_file,
603 "DBI stream contained corrupt symbol record");
604 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000605 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000606 ListScope SS(P, "LineInfo");
607 bool HadError = false;
608 // Define a locally scoped visitor to print the different
609 // substream types types.
610 class RecordVisitor : public codeview::IModuleSubstreamVisitor {
611 public:
612 RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {}
613 Error visitUnknown(ModuleSubstreamKind Kind,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000614 ReadableStreamRef Stream) override {
Zachary Turnerd3117392016-06-03 19:28:33 +0000615 DictScope DD(P, "Unknown");
616 ArrayRef<uint8_t> Data;
617 StreamReader R(Stream);
618 if (auto EC = R.readBytes(Data, R.bytesRemaining())) {
619 return make_error<RawError>(
620 raw_error_code::corrupt_file,
621 "DBI stream contained corrupt line info record");
622 }
623 P.printBinaryBlock("Data", Data);
624 return Error::success();
625 }
626 Error
Zachary Turnerd66889c2016-07-28 19:12:28 +0000627 visitFileChecksums(ReadableStreamRef Data,
Zachary Turnerd3117392016-06-03 19:28:33 +0000628 const FileChecksumArray &Checksums) override {
629 DictScope DD(P, "FileChecksums");
630 for (const auto &C : Checksums) {
631 DictScope DDD(P, "Checksum");
632 if (auto Result = getFileNameForOffset(C.FileNameOffset))
633 P.printString("FileName", Result.get());
634 else
635 return Result.takeError();
636 P.flush();
637 P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames());
638 P.printBinaryBlock("Checksum", C.Checksum);
639 }
640 return Error::success();
641 }
642
Zachary Turnerd66889c2016-07-28 19:12:28 +0000643 Error visitLines(ReadableStreamRef Data,
644 const LineSubstreamHeader *Header,
Zachary Turnerd3117392016-06-03 19:28:33 +0000645 const LineInfoArray &Lines) override {
646 DictScope DD(P, "Lines");
647 for (const auto &L : Lines) {
648 if (auto Result = getFileNameForOffset2(L.NameIndex))
649 P.printString("FileName", Result.get());
650 else
651 return Result.takeError();
652 P.flush();
653 for (const auto &N : L.LineNumbers) {
654 DictScope DDD(P, "Line");
655 LineInfo LI(N.Flags);
656 P.printNumber("Offset", N.Offset);
657 if (LI.isAlwaysStepInto())
658 P.printString("StepInto", StringRef("Always"));
659 else if (LI.isNeverStepInto())
660 P.printString("StepInto", StringRef("Never"));
661 else
662 P.printNumber("LineNumberStart", LI.getStartLine());
663 P.printNumber("EndDelta", LI.getLineDelta());
664 P.printBoolean("IsStatement", LI.isStatement());
665 }
666 for (const auto &C : L.Columns) {
667 DictScope DDD(P, "Column");
668 P.printNumber("Start", C.StartColumn);
669 P.printNumber("End", C.EndColumn);
670 }
671 }
672 return Error::success();
673 }
674
675 private:
676 Expected<StringRef> getFileNameForOffset(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000677 auto ST = F.getStringTable();
678 if (!ST)
679 return ST.takeError();
680
681 return ST->getStringForID(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000682 }
683 Expected<StringRef> getFileNameForOffset2(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000684 auto DS = F.getPDBDbiStream();
685 if (!DS)
686 return DS.takeError();
687 return DS->getFileNameForIndex(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000688 }
689 ScopedPrinter &P;
690 PDBFile &F;
691 };
692
693 RecordVisitor V(P, File);
694 for (const auto &L : ModS.lines(&HadError)) {
695 if (auto EC = codeview::visitModuleSubstream(L, V))
696 return EC;
697 }
698 }
699 }
700 }
701 }
702 return Error::success();
703}
704
705Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000706 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000707 return Error::success();
708
Zachary Turnera1657a92016-06-08 17:26:39 +0000709 auto Dbi = File.getPDBDbiStream();
710 if (!Dbi)
711 return Dbi.takeError();
712
Zachary Turnerd3117392016-06-03 19:28:33 +0000713 ListScope L(P, "Section Contributions");
714 class Visitor : public ISectionContribVisitor {
715 public:
716 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
717 void visit(const SectionContrib &SC) override {
718 DictScope D(P, "Contribution");
719 P.printNumber("ISect", SC.ISect);
720 P.printNumber("Off", SC.Off);
721 P.printNumber("Size", SC.Size);
722 P.printFlags("Characteristics", SC.Characteristics,
723 codeview::getImageSectionCharacteristicNames(),
724 COFF::SectionCharacteristics(0x00F00000));
725 {
726 DictScope DD(P, "Module");
727 P.printNumber("Index", SC.Imod);
728 auto M = DS.modules();
729 if (M.size() > SC.Imod) {
730 P.printString("Name", M[SC.Imod].Info.getModuleName());
731 }
732 }
733 P.printNumber("Data CRC", SC.DataCrc);
734 P.printNumber("Reloc CRC", SC.RelocCrc);
735 P.flush();
736 }
737 void visit(const SectionContrib2 &SC) override {
738 visit(SC.Base);
739 P.printNumber("ISect Coff", SC.ISectCoff);
740 P.flush();
741 }
742
743 private:
744 ScopedPrinter &P;
745 DbiStream &DS;
746 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000747 Visitor V(P, *Dbi);
748 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000749 return Error::success();
750}
751
752Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000753 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000754 return Error::success();
755
Zachary Turnera1657a92016-06-08 17:26:39 +0000756 auto Dbi = File.getPDBDbiStream();
757 if (!Dbi)
758 return Dbi.takeError();
759
Zachary Turnerd3117392016-06-03 19:28:33 +0000760 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000761 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000762 DictScope D(P, "Entry");
763 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
764 P.printNumber("Flags", M.Flags);
765 P.printNumber("Ovl", M.Ovl);
766 P.printNumber("Group", M.Group);
767 P.printNumber("Frame", M.Frame);
768 P.printNumber("SecName", M.SecName);
769 P.printNumber("ClassName", M.ClassName);
770 P.printNumber("Offset", M.Offset);
771 P.printNumber("SecByteLength", M.SecByteLength);
772 P.flush();
773 }
774 return Error::success();
775}
776
777Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000778 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000779 return Error::success();
780
781 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000782 auto Publics = File.getPDBPublicsStream();
783 if (!Publics)
784 return Publics.takeError();
785
786 auto Dbi = File.getPDBDbiStream();
787 if (!Dbi)
788 return Dbi.takeError();
789
790 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
791 P.printNumber("SymHash", Publics->getSymHash());
792 P.printNumber("AddrMap", Publics->getAddrMap());
793 P.printNumber("Number of buckets", Publics->getNumBuckets());
794 P.printList("Hash Buckets", Publics->getHashBuckets());
795 P.printList("Address Map", Publics->getAddressMap());
796 P.printList("Thunk Map", Publics->getThunkMap());
797 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000798 printSectionOffset);
799 ListScope L(P, "Symbols");
800 codeview::CVSymbolDumper SD(P, TD, nullptr, false);
801 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000802 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000803 DictScope DD(P, "");
804
805 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000806 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000807 P.printBinaryBlock("Bytes", S.Data);
808 }
809 if (HadError)
810 return make_error<RawError>(
811 raw_error_code::corrupt_file,
812 "Public symbol stream contained corrupt record");
813
814 return Error::success();
815}
816
817Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000818 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000819 return Error::success();
820
Zachary Turnera1657a92016-06-08 17:26:39 +0000821 auto Dbi = File.getPDBDbiStream();
822 if (!Dbi)
823 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000824
825 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000826 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000827 DictScope DD(P, "");
828
829 // If a name is 8 characters long, there is no NUL character at end.
830 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
831 P.printString("Name", Name);
832 P.printNumber("Virtual Size", Section.VirtualSize);
833 P.printNumber("Virtual Address", Section.VirtualAddress);
834 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
835 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
836 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
837 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
838 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
839 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +0000840 P.printFlags("Characteristics", Section.Characteristics,
841 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000842 }
843 return Error::success();
844}
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000845
846Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000847 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000848 return Error::success();
849
Zachary Turnera1657a92016-06-08 17:26:39 +0000850 auto Dbi = File.getPDBDbiStream();
851 if (!Dbi)
852 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000853
854 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +0000855 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000856 DictScope DD(P, "");
857 P.printNumber("Offset", Fpo.Offset);
858 P.printNumber("Size", Fpo.Size);
859 P.printNumber("Number of locals", Fpo.NumLocals);
860 P.printNumber("Number of params", Fpo.NumParams);
861 P.printNumber("Size of Prolog", Fpo.getPrologSize());
862 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
863 P.printBoolean("Has SEH", Fpo.hasSEH());
864 P.printBoolean("Use BP", Fpo.useBP());
865 P.printNumber("Frame Pointer", Fpo.getFP());
866 }
867 return Error::success();
868}
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000869
Zachary Turner7120a472016-06-06 20:37:05 +0000870void LLVMOutputStyle::flush() { P.flush(); }