blob: a9706857a129de786df3b27fa0fa1b91af1bf068 [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)
Zachary Turner5e3e4bb2016-08-05 21:45:34 +000086 : File(File), P(outs()), Dumper(&P, false) {}
Zachary Turnerd3117392016-06-03 19:28:33 +000087
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
Zachary Turner72c5b642016-09-09 18:17:52 +0000101 if (auto EC = dumpBlockRanges())
102 return EC;
103
104 if (auto EC = dumpStreamBytes())
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000105 return EC;
106
107 if (auto EC = dumpInfoStream())
108 return EC;
109
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000110 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
Zachary Turner8cf51c32016-08-03 16:53:21 +0000294 uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout());
295 uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout());
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000296 for (uint32_t I = 0; I < NumSections; ++I) {
297 uint32_t Fpm0 = 1 + BlocksPerSection * I;
298 // 2 Fpm blocks spaced at `getBlockSize()` block intervals
299 recordKnownUsedPage(PS, Fpm0);
300 recordKnownUsedPage(PS, Fpm0 + 1);
301 }
302
303 recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table
304
Rui Ueyama22e67382016-08-02 23:22:46 +0000305 for (auto DB : File.getDirectoryBlockArray())
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000306 recordKnownUsedPage(PS, DB);
Rui Ueyama22e67382016-08-02 23:22:46 +0000307
308 // Record pages used by streams. Note that pages for stream 0
309 // are considered being unused because that's what MSVC tools do.
310 // Stream 0 doesn't contain actual data, so it makes some sense,
311 // though it's a bit confusing to us.
312 for (auto &SE : File.getStreamMap().drop_front(1))
313 for (auto &S : SE)
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000314 recordKnownUsedPage(PS, S);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000315
316 dumpBitVector("Msf Free Pages", FPM);
317 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
318 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
319 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000320 return Error::success();
321}
322
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000323void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
324 std::vector<uint32_t> Vec;
325 for (uint32_t I = 0, E = V.size(); I != E; ++I)
326 if (V[I])
327 Vec.push_back(I);
328 P.printList(Name, Vec);
329}
330
Zachary Turnerd3117392016-06-03 19:28:33 +0000331Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000332 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000333 return Error::success();
334
335 ListScope L(P, "StreamBlocks");
336 uint32_t StreamCount = File.getNumStreams();
337 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
338 std::string Name("Stream ");
339 Name += to_string(StreamIdx);
340 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
341 P.printList(Name, StreamBlocks);
342 }
343 return Error::success();
344}
345
Zachary Turner72c5b642016-09-09 18:17:52 +0000346Error LLVMOutputStyle::dumpBlockRanges() {
347 if (!opts::raw::DumpBlockRange.hasValue())
348 return Error::success();
349 auto &R = *opts::raw::DumpBlockRange;
350 uint32_t Max = R.Max.getValueOr(R.Min);
351
352 if (Max < R.Min)
353 return make_error<StringError>(
354 "Invalid block range specified. Max < Min",
355 std::make_error_code(std::errc::bad_address));
356 if (Max >= File.getBlockCount())
357 return make_error<StringError>(
358 "Invalid block range specified. Requested block out of bounds",
359 std::make_error_code(std::errc::bad_address));
360
361 DictScope D(P, "Block Data");
362 for (uint32_t I = R.Min; I <= Max; ++I) {
363 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
364 if (!ExpectedData)
365 return ExpectedData.takeError();
366 std::string Label;
367 llvm::raw_string_ostream S(Label);
368 S << "Block " << I;
369 S.flush();
370 P.printBinaryBlock(Label, *ExpectedData);
371 }
372
373 return Error::success();
374}
375
376Error LLVMOutputStyle::dumpStreamBytes() {
377 if (opts::raw::DumpStreamData.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000378 return Error::success();
379
Zachary Turner72c5b642016-09-09 18:17:52 +0000380 DictScope D(P, "Stream Data");
381 for (uint32_t SI : opts::raw::DumpStreamData) {
382 if (SI >= File.getNumStreams())
383 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000384
Zachary Turner72c5b642016-09-09 18:17:52 +0000385 auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(),
386 File.getMsfBuffer(), SI);
387 if (!S)
388 continue;
389 StreamReader R(*S);
390 ArrayRef<uint8_t> StreamData;
391 if (auto EC = R.readBytes(StreamData, S->getLength()))
Zachary Turnerd3117392016-06-03 19:28:33 +0000392 return EC;
Zachary Turner72c5b642016-09-09 18:17:52 +0000393 std::string Label;
394 llvm::raw_string_ostream Stream(Label);
395 Stream << "Stream " << SI;
396 Stream.flush();
397 P.printBinaryBlock(Label, StreamData);
Zachary Turnerd3117392016-06-03 19:28:33 +0000398 }
399 return Error::success();
400}
401
402Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000403 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000404 return Error::success();
Zachary Turnera1657a92016-06-08 17:26:39 +0000405 auto IS = File.getPDBInfoStream();
406 if (!IS)
407 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000408
409 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000410 P.printNumber("Version", IS->getVersion());
411 P.printHex("Signature", IS->getSignature());
412 P.printNumber("Age", IS->getAge());
413 P.printObject("Guid", IS->getGuid());
Zachary Turnerd3117392016-06-03 19:28:33 +0000414 return Error::success();
415}
416
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000417static void printTypeIndexOffset(raw_ostream &OS,
418 const TypeIndexOffset &TIOff) {
419 OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}";
420}
421
422static void dumpTpiHash(ScopedPrinter &P, TpiStream &Tpi) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000423 if (!opts::raw::DumpTpiHash)
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000424 return;
425 DictScope DD(P, "Hash");
Rui Ueyamaf14a74c2016-06-07 23:53:43 +0000426 P.printNumber("Number of Hash Buckets", Tpi.NumHashBuckets());
Rui Ueyamad8339172016-06-07 23:44:27 +0000427 P.printNumber("Hash Key Size", Tpi.getHashKeySize());
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000428 P.printList("Values", Tpi.getHashValues());
429 P.printList("Type Index Offsets", Tpi.getTypeIndexOffsets(),
430 printTypeIndexOffset);
431 P.printList("Hash Adjustments", Tpi.getHashAdjustments(),
432 printTypeIndexOffset);
433}
434
Zachary Turnerd3117392016-06-03 19:28:33 +0000435Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
436 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
437
438 bool DumpRecordBytes = false;
439 bool DumpRecords = false;
440 StringRef Label;
441 StringRef VerLabel;
442 if (StreamIdx == StreamTPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000443 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
444 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000445 Label = "Type Info Stream (TPI)";
446 VerLabel = "TPI Version";
447 } else if (StreamIdx == StreamIPI) {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000448 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
449 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000450 Label = "Type Info Stream (IPI)";
451 VerLabel = "IPI Version";
452 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000453 if (!DumpRecordBytes && !DumpRecords && !opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000454 return Error::success();
455
Zachary Turnera1657a92016-06-08 17:26:39 +0000456 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
457 : File.getPDBIpiStream();
458 if (!Tpi)
459 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000460
461 if (DumpRecords || DumpRecordBytes) {
462 DictScope D(P, Label);
463
Zachary Turnera1657a92016-06-08 17:26:39 +0000464 P.printNumber(VerLabel, Tpi->getTpiVersion());
465 P.printNumber("Record count", Tpi->NumTypeRecords());
Zachary Turnerd3117392016-06-03 19:28:33 +0000466
467 ListScope L(P, "Records");
468
469 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000470 for (auto &Type : Tpi->types(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000471 DictScope DD(P, "");
472
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000473 if (DumpRecords) {
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000474 if (auto EC = Dumper.dump(Type))
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000475 return EC;
476 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000477
478 if (DumpRecordBytes)
479 P.printBinaryBlock("Bytes", Type.Data);
480 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000481 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000482 if (HadError)
483 return make_error<RawError>(raw_error_code::corrupt_file,
484 "TPI stream contained corrupt record");
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000485 } else if (opts::raw::DumpModuleSyms) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000486 // Even if the user doesn't want to dump type records, we still need to
487 // iterate them in order to build the list of types so that we can print
488 // them when dumping module symbols. So when they want to dump symbols
489 // but not types, use a null output stream.
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000490 ScopedPrinter *OldP = Dumper.getPrinter();
491 Dumper.setPrinter(nullptr);
Zachary Turnerd3117392016-06-03 19:28:33 +0000492
493 bool HadError = false;
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000494 for (auto &Type : Tpi->types(&HadError)) {
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000495 if (auto EC = Dumper.dump(Type))
Zachary Turner01ee3dae2016-06-16 18:22:27 +0000496 return EC;
497 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000498
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000499 Dumper.setPrinter(OldP);
Zachary Turnera1657a92016-06-08 17:26:39 +0000500 dumpTpiHash(P, *Tpi);
Zachary Turnerd3117392016-06-03 19:28:33 +0000501 if (HadError)
502 return make_error<RawError>(raw_error_code::corrupt_file,
503 "TPI stream contained corrupt record");
504 }
505 P.flush();
506 return Error::success();
507}
508
509Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000510 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
511 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
512 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000513 return Error::success();
514
Zachary Turnera1657a92016-06-08 17:26:39 +0000515 auto DS = File.getPDBDbiStream();
516 if (!DS)
517 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000518
519 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000520 P.printNumber("Dbi Version", DS->getDbiVersion());
521 P.printNumber("Age", DS->getAge());
522 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
523 P.printBoolean("Has CTypes", DS->hasCTypes());
524 P.printBoolean("Is Stripped", DS->isStripped());
525 P.printObject("Machine Type", DS->getMachineType());
526 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
527 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
528 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000529
Zachary Turnera1657a92016-06-08 17:26:39 +0000530 uint16_t Major = DS->getBuildMajorVersion();
531 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000532 P.printVersion("Toolchain Version", Major, Minor);
533
534 std::string DllName;
535 raw_string_ostream DllStream(DllName);
536 DllStream << "mspdb" << Major << Minor << ".dll version";
537 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000538 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000539
540 if (DumpModules) {
541 ListScope L(P, "Modules");
Zachary Turnera1657a92016-06-08 17:26:39 +0000542 for (auto &Modi : DS->modules()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000543 DictScope DD(P);
544 P.printString("Name", Modi.Info.getModuleName().str());
545 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
546 P.printString("Object File Name", Modi.Info.getObjFileName().str());
547 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
548 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
549 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
550 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
551 P.printNumber("C13 Line Info Byte Size",
552 Modi.Info.getC13LineInfoByteSize());
553 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
554 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
555 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000556 if (opts::raw::DumpModuleFiles) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000557 std::string FileListName =
558 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
559 ListScope LL(P, FileListName);
560 for (auto File : Modi.SourceFiles)
561 P.printString(File.str());
562 }
563 bool HasModuleDI =
564 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
565 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000566 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
567 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000568 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000569 File.getMsfLayout(), File.getMsfBuffer(),
570 Modi.Info.getModuleStreamIndex());
571
572 ModStream ModS(Modi.Info, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000573 if (auto EC = ModS.reload())
574 return EC;
575
576 if (ShouldDumpSymbols) {
577 ListScope SS(P, "Symbols");
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000578 codeview::CVSymbolDumper SD(P, Dumper, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000579 bool HadError = false;
580 for (const auto &S : ModS.symbols(&HadError)) {
581 DictScope DD(P, "");
582
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000583 if (opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000584 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000585 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000586 P.printBinaryBlock("Bytes", S.Data);
587 }
588 if (HadError)
589 return make_error<RawError>(
590 raw_error_code::corrupt_file,
591 "DBI stream contained corrupt symbol record");
592 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000593 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000594 ListScope SS(P, "LineInfo");
595 bool HadError = false;
596 // Define a locally scoped visitor to print the different
597 // substream types types.
598 class RecordVisitor : public codeview::IModuleSubstreamVisitor {
599 public:
600 RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {}
601 Error visitUnknown(ModuleSubstreamKind Kind,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000602 ReadableStreamRef Stream) override {
Zachary Turnerd3117392016-06-03 19:28:33 +0000603 DictScope DD(P, "Unknown");
604 ArrayRef<uint8_t> Data;
605 StreamReader R(Stream);
606 if (auto EC = R.readBytes(Data, R.bytesRemaining())) {
607 return make_error<RawError>(
608 raw_error_code::corrupt_file,
609 "DBI stream contained corrupt line info record");
610 }
611 P.printBinaryBlock("Data", Data);
612 return Error::success();
613 }
614 Error
Zachary Turnerd66889c2016-07-28 19:12:28 +0000615 visitFileChecksums(ReadableStreamRef Data,
Zachary Turnerd3117392016-06-03 19:28:33 +0000616 const FileChecksumArray &Checksums) override {
617 DictScope DD(P, "FileChecksums");
618 for (const auto &C : Checksums) {
619 DictScope DDD(P, "Checksum");
620 if (auto Result = getFileNameForOffset(C.FileNameOffset))
621 P.printString("FileName", Result.get());
622 else
623 return Result.takeError();
624 P.flush();
625 P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames());
626 P.printBinaryBlock("Checksum", C.Checksum);
627 }
628 return Error::success();
629 }
630
Zachary Turnerd66889c2016-07-28 19:12:28 +0000631 Error visitLines(ReadableStreamRef Data,
632 const LineSubstreamHeader *Header,
Zachary Turnerd3117392016-06-03 19:28:33 +0000633 const LineInfoArray &Lines) override {
634 DictScope DD(P, "Lines");
635 for (const auto &L : Lines) {
636 if (auto Result = getFileNameForOffset2(L.NameIndex))
637 P.printString("FileName", Result.get());
638 else
639 return Result.takeError();
640 P.flush();
641 for (const auto &N : L.LineNumbers) {
642 DictScope DDD(P, "Line");
643 LineInfo LI(N.Flags);
644 P.printNumber("Offset", N.Offset);
645 if (LI.isAlwaysStepInto())
646 P.printString("StepInto", StringRef("Always"));
647 else if (LI.isNeverStepInto())
648 P.printString("StepInto", StringRef("Never"));
649 else
650 P.printNumber("LineNumberStart", LI.getStartLine());
651 P.printNumber("EndDelta", LI.getLineDelta());
652 P.printBoolean("IsStatement", LI.isStatement());
653 }
654 for (const auto &C : L.Columns) {
655 DictScope DDD(P, "Column");
656 P.printNumber("Start", C.StartColumn);
657 P.printNumber("End", C.EndColumn);
658 }
659 }
660 return Error::success();
661 }
662
663 private:
664 Expected<StringRef> getFileNameForOffset(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000665 auto ST = F.getStringTable();
666 if (!ST)
667 return ST.takeError();
668
669 return ST->getStringForID(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000670 }
671 Expected<StringRef> getFileNameForOffset2(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000672 auto DS = F.getPDBDbiStream();
673 if (!DS)
674 return DS.takeError();
675 return DS->getFileNameForIndex(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000676 }
677 ScopedPrinter &P;
678 PDBFile &F;
679 };
680
681 RecordVisitor V(P, File);
682 for (const auto &L : ModS.lines(&HadError)) {
683 if (auto EC = codeview::visitModuleSubstream(L, V))
684 return EC;
685 }
686 }
687 }
688 }
689 }
690 return Error::success();
691}
692
693Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000694 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000695 return Error::success();
696
Zachary Turnera1657a92016-06-08 17:26:39 +0000697 auto Dbi = File.getPDBDbiStream();
698 if (!Dbi)
699 return Dbi.takeError();
700
Zachary Turnerd3117392016-06-03 19:28:33 +0000701 ListScope L(P, "Section Contributions");
702 class Visitor : public ISectionContribVisitor {
703 public:
704 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
705 void visit(const SectionContrib &SC) override {
706 DictScope D(P, "Contribution");
707 P.printNumber("ISect", SC.ISect);
708 P.printNumber("Off", SC.Off);
709 P.printNumber("Size", SC.Size);
710 P.printFlags("Characteristics", SC.Characteristics,
711 codeview::getImageSectionCharacteristicNames(),
712 COFF::SectionCharacteristics(0x00F00000));
713 {
714 DictScope DD(P, "Module");
715 P.printNumber("Index", SC.Imod);
716 auto M = DS.modules();
717 if (M.size() > SC.Imod) {
718 P.printString("Name", M[SC.Imod].Info.getModuleName());
719 }
720 }
721 P.printNumber("Data CRC", SC.DataCrc);
722 P.printNumber("Reloc CRC", SC.RelocCrc);
723 P.flush();
724 }
725 void visit(const SectionContrib2 &SC) override {
726 visit(SC.Base);
727 P.printNumber("ISect Coff", SC.ISectCoff);
728 P.flush();
729 }
730
731 private:
732 ScopedPrinter &P;
733 DbiStream &DS;
734 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000735 Visitor V(P, *Dbi);
736 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000737 return Error::success();
738}
739
740Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000741 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000742 return Error::success();
743
Zachary Turnera1657a92016-06-08 17:26:39 +0000744 auto Dbi = File.getPDBDbiStream();
745 if (!Dbi)
746 return Dbi.takeError();
747
Zachary Turnerd3117392016-06-03 19:28:33 +0000748 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000749 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000750 DictScope D(P, "Entry");
751 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
752 P.printNumber("Flags", M.Flags);
753 P.printNumber("Ovl", M.Ovl);
754 P.printNumber("Group", M.Group);
755 P.printNumber("Frame", M.Frame);
756 P.printNumber("SecName", M.SecName);
757 P.printNumber("ClassName", M.ClassName);
758 P.printNumber("Offset", M.Offset);
759 P.printNumber("SecByteLength", M.SecByteLength);
760 P.flush();
761 }
762 return Error::success();
763}
764
765Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000766 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000767 return Error::success();
768
769 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000770 auto Publics = File.getPDBPublicsStream();
771 if (!Publics)
772 return Publics.takeError();
773
774 auto Dbi = File.getPDBDbiStream();
775 if (!Dbi)
776 return Dbi.takeError();
777
778 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
779 P.printNumber("SymHash", Publics->getSymHash());
780 P.printNumber("AddrMap", Publics->getAddrMap());
781 P.printNumber("Number of buckets", Publics->getNumBuckets());
782 P.printList("Hash Buckets", Publics->getHashBuckets());
783 P.printList("Address Map", Publics->getAddressMap());
784 P.printList("Thunk Map", Publics->getThunkMap());
785 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000786 printSectionOffset);
787 ListScope L(P, "Symbols");
Zachary Turner5e3e4bb2016-08-05 21:45:34 +0000788 codeview::CVSymbolDumper SD(P, Dumper, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000789 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000790 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000791 DictScope DD(P, "");
792
793 SD.dump(S);
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000794 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerd3117392016-06-03 19:28:33 +0000795 P.printBinaryBlock("Bytes", S.Data);
796 }
797 if (HadError)
798 return make_error<RawError>(
799 raw_error_code::corrupt_file,
800 "Public symbol stream contained corrupt record");
801
802 return Error::success();
803}
804
805Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000806 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000807 return Error::success();
808
Zachary Turnera1657a92016-06-08 17:26:39 +0000809 auto Dbi = File.getPDBDbiStream();
810 if (!Dbi)
811 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000812
813 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000814 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000815 DictScope DD(P, "");
816
817 // If a name is 8 characters long, there is no NUL character at end.
818 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
819 P.printString("Name", Name);
820 P.printNumber("Virtual Size", Section.VirtualSize);
821 P.printNumber("Virtual Address", Section.VirtualAddress);
822 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
823 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
824 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
825 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
826 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
827 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +0000828 P.printFlags("Characteristics", Section.Characteristics,
829 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000830 }
831 return Error::success();
832}
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000833
834Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000835 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000836 return Error::success();
837
Zachary Turnera1657a92016-06-08 17:26:39 +0000838 auto Dbi = File.getPDBDbiStream();
839 if (!Dbi)
840 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000841
842 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +0000843 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +0000844 DictScope DD(P, "");
845 P.printNumber("Offset", Fpo.Offset);
846 P.printNumber("Size", Fpo.Size);
847 P.printNumber("Number of locals", Fpo.NumLocals);
848 P.printNumber("Number of params", Fpo.NumParams);
849 P.printNumber("Size of Prolog", Fpo.getPrologSize());
850 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
851 P.printBoolean("Has SEH", Fpo.hasSEH());
852 P.printBoolean("Use BP", Fpo.useBP());
853 P.printNumber("Frame Pointer", Fpo.getFP());
854 }
855 return Error::success();
856}
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000857
Zachary Turner7120a472016-06-06 20:37:05 +0000858void LLVMOutputStyle::flush() { P.flush(); }