blob: 2dd4ef0fb30de45899e4885db0c9be5015396ef2 [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
Zachary Turner5b6e4e02017-04-29 01:13:21 +000012#include "C13DebugFragmentVisitor.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000013#include "CompactTypeDumpVisitor.h"
Zachary Turner6ac232c2017-03-13 23:28:25 +000014#include "StreamUtil.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000015#include "llvm-pdbdump.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000016
Zachary Turner629cb7d2017-01-11 23:24:22 +000017#include "llvm/DebugInfo/CodeView/CVTypeDumper.h"
18#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000019#include "llvm/DebugInfo/CodeView/EnumTables.h"
Zachary Turner5b6e4e02017-04-29 01:13:21 +000020#include "llvm/DebugInfo/CodeView/Line.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000021#include "llvm/DebugInfo/CodeView/ModuleDebugFileChecksumFragment.h"
Zachary Turner67c56012017-04-27 16:11:19 +000022#include "llvm/DebugInfo/CodeView/ModuleDebugFragmentVisitor.h"
Zachary Turneredef1452017-05-02 16:56:09 +000023#include "llvm/DebugInfo/CodeView/ModuleDebugInlineeLinesFragment.h"
Zachary Turnerc37cb0c2017-04-27 16:12:16 +000024#include "llvm/DebugInfo/CodeView/ModuleDebugLineFragment.h"
25#include "llvm/DebugInfo/CodeView/ModuleDebugUnknownFragment.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000026#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000027#include "llvm/DebugInfo/CodeView/TypeDatabaseVisitor.h"
28#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
29#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
30#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000031#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000032#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000033#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
34#include "llvm/DebugInfo/PDB/Native/EnumTables.h"
35#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
36#include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
37#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000038#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000039#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
40#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
41#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerbedc85f2017-05-04 23:53:54 +000042#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000043#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000044#include "llvm/DebugInfo/PDB/PDBExtras.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000045#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000046#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000047#include "llvm/Support/FormatVariadic.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000048
49#include <unordered_map>
50
51using namespace llvm;
52using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000053using namespace llvm::msf;
Zachary Turnerd3117392016-06-03 19:28:33 +000054using namespace llvm::pdb;
55
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +000056namespace {
57struct PageStats {
58 explicit PageStats(const BitVector &FreePages)
59 : Upm(FreePages), ActualUsedPages(FreePages.size()),
60 MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) {
61 const_cast<BitVector &>(Upm).flip();
62 // To calculate orphaned pages, we start with the set of pages that the
63 // MSF thinks are used. Each time we find one that actually *is* used,
64 // we unset it. Whichever bits remain set at the end are orphaned.
65 OrphanedPages = Upm;
66 }
67
68 // The inverse of the MSF File's copy of the Fpm. The basis for which we
69 // determine the allocation status of each page.
70 const BitVector Upm;
71
72 // Pages which are marked as used in the FPM and are used at least once.
73 BitVector ActualUsedPages;
74
75 // Pages which are marked as used in the FPM but are used more than once.
76 BitVector MultiUsePages;
77
78 // Pages which are marked as used in the FPM but are not used at all.
79 BitVector OrphanedPages;
80
81 // Pages which are marked free in the FPM but are used.
82 BitVector UseAfterFreePages;
83};
Zachary Turner5b6e4e02017-04-29 01:13:21 +000084
Zachary Turner5b6e4e02017-04-29 01:13:21 +000085class C13RawVisitor : public C13DebugFragmentVisitor {
86public:
Zachary Turneredef1452017-05-02 16:56:09 +000087 C13RawVisitor(ScopedPrinter &P, PDBFile &F, TypeDatabase &IPI)
88 : C13DebugFragmentVisitor(F), P(P), IPI(IPI) {}
Zachary Turner5b6e4e02017-04-29 01:13:21 +000089
90 Error handleLines() override {
Zachary Turneredef1452017-05-02 16:56:09 +000091 if (Lines.empty())
92 return Error::success();
93
Zachary Turner5b6e4e02017-04-29 01:13:21 +000094 DictScope DD(P, "Lines");
95
96 for (const auto &Fragment : Lines) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000097 DictScope DDD(P, "Block");
Zachary Turner5b6e4e02017-04-29 01:13:21 +000098 P.printNumber("RelocSegment", Fragment.header()->RelocSegment);
99 P.printNumber("RelocOffset", Fragment.header()->RelocOffset);
100 P.printNumber("CodeSize", Fragment.header()->CodeSize);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000101 P.printBoolean("HasColumns", Fragment.hasColumnInfo());
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000102
103 for (const auto &L : Fragment) {
104 DictScope DDDD(P, "Lines");
105
106 if (auto EC = printFileName("FileName", L.NameIndex))
107 return EC;
108
109 for (const auto &N : L.LineNumbers) {
110 DictScope DDD(P, "Line");
111 LineInfo LI(N.Flags);
112 P.printNumber("Offset", N.Offset);
113 if (LI.isAlwaysStepInto())
114 P.printString("StepInto", StringRef("Always"));
115 else if (LI.isNeverStepInto())
116 P.printString("StepInto", StringRef("Never"));
117 else
118 P.printNumber("LineNumberStart", LI.getStartLine());
119 P.printNumber("EndDelta", LI.getLineDelta());
120 P.printBoolean("IsStatement", LI.isStatement());
121 }
122 for (const auto &C : L.Columns) {
123 DictScope DDD(P, "Column");
124 P.printNumber("Start", C.StartColumn);
125 P.printNumber("End", C.EndColumn);
126 }
127 }
128 }
129
130 return Error::success();
131 }
132
133 Error handleFileChecksums() override {
Zachary Turneredef1452017-05-02 16:56:09 +0000134 if (!Checksums.hasValue())
135 return Error::success();
136
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000137 DictScope DD(P, "FileChecksums");
138 for (const auto &CS : *Checksums) {
139 DictScope DDD(P, "Checksum");
140 if (auto Result = getNameFromStringTable(CS.FileNameOffset))
141 P.printString("FileName", *Result);
142 else
143 return Result.takeError();
144 P.printEnum("Kind", uint8_t(CS.Kind), getFileChecksumNames());
145 P.printBinaryBlock("Checksum", CS.Checksum);
146 }
147 return Error::success();
148 }
149
Zachary Turneredef1452017-05-02 16:56:09 +0000150 Error handleInlineeLines() override {
151 if (InlineeLines.empty())
152 return Error::success();
153
154 DictScope D(P, "InlineeLines");
155 for (const auto &IL : InlineeLines) {
156 P.printBoolean("HasExtraFiles", IL.hasExtraFiles());
157 ListScope LS(P, "Lines");
158 for (const auto &L : IL) {
159 DictScope DDD(P, "Inlinee");
160 if (auto EC = printFileName("FileName", L.Header->FileID))
161 return EC;
162
163 if (auto EC = dumpTypeRecord("Function", IPI, L.Header->Inlinee))
164 return EC;
165 P.printNumber("SourceLine", L.Header->SourceLineNum);
166 if (IL.hasExtraFiles()) {
167 ListScope DDDD(P, "ExtraFiles");
168 for (const auto &EF : L.ExtraFiles) {
169 if (auto EC = printFileName("File", EF))
170 return EC;
171 }
172 }
173 }
174 }
175 return Error::success();
176 }
177
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000178private:
Zachary Turneredef1452017-05-02 16:56:09 +0000179 Error dumpTypeRecord(StringRef Label, TypeDatabase &DB, TypeIndex Index) {
180 CompactTypeDumpVisitor CTDV(DB, Index, &P);
181 CVTypeVisitor Visitor(CTDV);
182 DictScope D(P, Label);
183 if (DB.containsTypeIndex(Index)) {
184 CVType &Type = DB.getTypeRecord(Index);
185 if (auto EC = Visitor.visitTypeRecord(Type))
186 return EC;
187 } else {
188 P.printString(
189 llvm::formatv("Index: {0:x} (unknown function)", Index.getIndex())
190 .str());
191 }
192 return Error::success();
193 }
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000194 Error printFileName(StringRef Label, uint32_t Offset) {
195 if (auto Result = getNameFromChecksumsBuffer(Offset)) {
196 P.printString(Label, *Result);
197 return Error::success();
198 } else
199 return Result.takeError();
200 }
201
202 ScopedPrinter &P;
Zachary Turneredef1452017-05-02 16:56:09 +0000203 TypeDatabase &IPI;
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000204};
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000205}
206
207static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) {
208 if (Stats.Upm.test(UsedIndex)) {
209 if (Stats.ActualUsedPages.test(UsedIndex))
210 Stats.MultiUsePages.set(UsedIndex);
211 Stats.ActualUsedPages.set(UsedIndex);
212 Stats.OrphanedPages.reset(UsedIndex);
213 } else {
214 // The MSF doesn't think this page is used, but it is.
215 Stats.UseAfterFreePages.set(UsedIndex);
216 }
217}
218
Zachary Turnerd3117392016-06-03 19:28:33 +0000219static void printSectionOffset(llvm::raw_ostream &OS,
220 const SectionOffset &Off) {
221 OS << Off.Off << ", " << Off.Isect;
222}
223
Zachary Turner629cb7d2017-01-11 23:24:22 +0000224LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) : File(File), P(outs()) {}
Zachary Turnerd3117392016-06-03 19:28:33 +0000225
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000226Error LLVMOutputStyle::dump() {
227 if (auto EC = dumpFileHeaders())
228 return EC;
229
230 if (auto EC = dumpStreamSummary())
231 return EC;
232
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000233 if (auto EC = dumpFreePageMap())
234 return EC;
235
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000236 if (auto EC = dumpStreamBlocks())
237 return EC;
238
Zachary Turner72c5b642016-09-09 18:17:52 +0000239 if (auto EC = dumpBlockRanges())
240 return EC;
241
242 if (auto EC = dumpStreamBytes())
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000243 return EC;
244
Zachary Turner760ad4d2017-01-20 22:42:09 +0000245 if (auto EC = dumpStringTable())
246 return EC;
247
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000248 if (auto EC = dumpInfoStream())
249 return EC;
250
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000251 if (auto EC = dumpTpiStream(StreamTPI))
252 return EC;
253
254 if (auto EC = dumpTpiStream(StreamIPI))
255 return EC;
256
257 if (auto EC = dumpDbiStream())
258 return EC;
259
260 if (auto EC = dumpSectionContribs())
261 return EC;
262
263 if (auto EC = dumpSectionMap())
264 return EC;
265
Bob Haarman653baa22016-10-21 19:43:19 +0000266 if (auto EC = dumpGlobalsStream())
267 return EC;
268
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000269 if (auto EC = dumpPublicsStream())
270 return EC;
271
272 if (auto EC = dumpSectionHeaders())
273 return EC;
274
275 if (auto EC = dumpFpoStream())
276 return EC;
277
278 flush();
279
280 return Error::success();
281}
282
Zachary Turnerd3117392016-06-03 19:28:33 +0000283Error LLVMOutputStyle::dumpFileHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000284 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000285 return Error::success();
286
287 DictScope D(P, "FileHeaders");
288 P.printNumber("BlockSize", File.getBlockSize());
Zachary Turnerb927e022016-07-15 22:17:19 +0000289 P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock());
Zachary Turnerd3117392016-06-03 19:28:33 +0000290 P.printNumber("NumBlocks", File.getBlockCount());
291 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
292 P.printNumber("Unknown1", File.getUnknown1());
293 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
294 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
Zachary Turnerd3117392016-06-03 19:28:33 +0000295
296 // The directory is not contiguous. Instead, the block map contains a
297 // contiguous list of block numbers whose contents, when concatenated in
298 // order, make up the directory.
299 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
300 P.printNumber("NumStreams", File.getNumStreams());
301 return Error::success();
302}
303
Zachary Turner36efbfa2016-09-09 19:00:49 +0000304Error LLVMOutputStyle::dumpStreamSummary() {
305 if (!opts::raw::DumpStreamSummary)
306 return Error::success();
307
Zachary Turner6ac232c2017-03-13 23:28:25 +0000308 if (StreamPurposes.empty())
309 discoverStreamPurposes(File, StreamPurposes);
Zachary Turner36efbfa2016-09-09 19:00:49 +0000310
311 uint32_t StreamCount = File.getNumStreams();
312
313 ListScope L(P, "Streams");
314 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
315 std::string Label("Stream ");
316 Label += to_string(StreamIdx);
317
318 std::string Value = "[" + StreamPurposes[StreamIdx] + "] (";
319 Value += to_string(File.getStreamByteSize(StreamIdx));
320 Value += " bytes)";
321
322 P.printString(Label, Value);
323 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000324
Zachary Turnerd3117392016-06-03 19:28:33 +0000325 P.flush();
326 return Error::success();
327}
328
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000329Error LLVMOutputStyle::dumpFreePageMap() {
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000330 if (!opts::raw::DumpPageStats)
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000331 return Error::success();
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000332
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000333 // Start with used pages instead of free pages because
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000334 // the number of free pages is far larger than used pages.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000335 BitVector FPM = File.getMsfLayout().FreePageMap;
336
337 PageStats PS(FPM);
338
339 recordKnownUsedPage(PS, 0); // MSF Super Block
340
Zachary Turner8cf51c32016-08-03 16:53:21 +0000341 uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout());
342 uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout());
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000343 for (uint32_t I = 0; I < NumSections; ++I) {
344 uint32_t Fpm0 = 1 + BlocksPerSection * I;
345 // 2 Fpm blocks spaced at `getBlockSize()` block intervals
346 recordKnownUsedPage(PS, Fpm0);
347 recordKnownUsedPage(PS, Fpm0 + 1);
348 }
349
350 recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table
351
Rui Ueyama22e67382016-08-02 23:22:46 +0000352 for (auto DB : File.getDirectoryBlockArray())
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000353 recordKnownUsedPage(PS, DB);
Rui Ueyama22e67382016-08-02 23:22:46 +0000354
355 // Record pages used by streams. Note that pages for stream 0
356 // are considered being unused because that's what MSVC tools do.
357 // Stream 0 doesn't contain actual data, so it makes some sense,
358 // though it's a bit confusing to us.
359 for (auto &SE : File.getStreamMap().drop_front(1))
360 for (auto &S : SE)
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000361 recordKnownUsedPage(PS, S);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000362
363 dumpBitVector("Msf Free Pages", FPM);
364 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
365 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
366 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000367 return Error::success();
368}
369
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000370void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
371 std::vector<uint32_t> Vec;
372 for (uint32_t I = 0, E = V.size(); I != E; ++I)
373 if (V[I])
374 Vec.push_back(I);
375 P.printList(Name, Vec);
376}
377
Bob Haarman653baa22016-10-21 19:43:19 +0000378Error LLVMOutputStyle::dumpGlobalsStream() {
379 if (!opts::raw::DumpGlobals)
380 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000381 if (!File.hasPDBGlobalsStream()) {
382 P.printString("Globals Stream not present");
383 return Error::success();
384 }
Bob Haarman653baa22016-10-21 19:43:19 +0000385
Bob Haarman653baa22016-10-21 19:43:19 +0000386 auto Globals = File.getPDBGlobalsStream();
387 if (!Globals)
Bob Haarman312fd0e2016-12-06 00:55:55 +0000388 return Globals.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000389 DictScope D(P, "Globals Stream");
Bob Haarman653baa22016-10-21 19:43:19 +0000390
391 auto Dbi = File.getPDBDbiStream();
392 if (!Dbi)
393 return Dbi.takeError();
394
395 P.printNumber("Stream number", Dbi->getGlobalSymbolStreamIndex());
396 P.printNumber("Number of buckets", Globals->getNumBuckets());
397 P.printList("Hash Buckets", Globals->getHashBuckets());
398
399 return Error::success();
400}
401
Zachary Turnerd3117392016-06-03 19:28:33 +0000402Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000403 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000404 return Error::success();
405
406 ListScope L(P, "StreamBlocks");
407 uint32_t StreamCount = File.getNumStreams();
408 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
409 std::string Name("Stream ");
410 Name += to_string(StreamIdx);
411 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
412 P.printList(Name, StreamBlocks);
413 }
414 return Error::success();
415}
416
Zachary Turner72c5b642016-09-09 18:17:52 +0000417Error LLVMOutputStyle::dumpBlockRanges() {
418 if (!opts::raw::DumpBlockRange.hasValue())
419 return Error::success();
420 auto &R = *opts::raw::DumpBlockRange;
421 uint32_t Max = R.Max.getValueOr(R.Min);
422
423 if (Max < R.Min)
424 return make_error<StringError>(
425 "Invalid block range specified. Max < Min",
426 std::make_error_code(std::errc::bad_address));
427 if (Max >= File.getBlockCount())
428 return make_error<StringError>(
429 "Invalid block range specified. Requested block out of bounds",
430 std::make_error_code(std::errc::bad_address));
431
432 DictScope D(P, "Block Data");
433 for (uint32_t I = R.Min; I <= Max; ++I) {
434 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
435 if (!ExpectedData)
436 return ExpectedData.takeError();
437 std::string Label;
438 llvm::raw_string_ostream S(Label);
439 S << "Block " << I;
440 S.flush();
441 P.printBinaryBlock(Label, *ExpectedData);
442 }
443
444 return Error::success();
445}
446
Zachary Turner7159ab92017-04-28 00:43:38 +0000447static Error parseStreamSpec(StringRef Str, uint32_t &SI, uint32_t &Offset,
448 uint32_t &Size) {
449 if (Str.consumeInteger(0, SI))
450 return make_error<RawError>(raw_error_code::invalid_format,
451 "Invalid Stream Specification");
452 if (Str.consume_front(":")) {
453 if (Str.consumeInteger(0, Offset))
454 return make_error<RawError>(raw_error_code::invalid_format,
455 "Invalid Stream Specification");
456 }
457 if (Str.consume_front("@")) {
458 if (Str.consumeInteger(0, Size))
459 return make_error<RawError>(raw_error_code::invalid_format,
460 "Invalid Stream Specification");
461 }
462 if (!Str.empty())
463 return make_error<RawError>(raw_error_code::invalid_format,
464 "Invalid Stream Specification");
465 return Error::success();
466}
467
Zachary Turner72c5b642016-09-09 18:17:52 +0000468Error LLVMOutputStyle::dumpStreamBytes() {
469 if (opts::raw::DumpStreamData.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000470 return Error::success();
471
Zachary Turner6ac232c2017-03-13 23:28:25 +0000472 if (StreamPurposes.empty())
473 discoverStreamPurposes(File, StreamPurposes);
Zachary Turner36efbfa2016-09-09 19:00:49 +0000474
Zachary Turner72c5b642016-09-09 18:17:52 +0000475 DictScope D(P, "Stream Data");
Zachary Turner7159ab92017-04-28 00:43:38 +0000476 for (auto &Str : opts::raw::DumpStreamData) {
477 uint32_t SI = 0;
478 uint32_t Begin = 0;
479 uint32_t Size = 0;
480 uint32_t End = 0;
481
482 if (auto EC = parseStreamSpec(Str, SI, Begin, Size))
483 return EC;
484
Zachary Turner72c5b642016-09-09 18:17:52 +0000485 if (SI >= File.getNumStreams())
486 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000487
Zachary Turner72c5b642016-09-09 18:17:52 +0000488 auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(),
489 File.getMsfBuffer(), SI);
490 if (!S)
491 continue;
Zachary Turner36efbfa2016-09-09 19:00:49 +0000492 DictScope DD(P, "Stream");
Zachary Turner7159ab92017-04-28 00:43:38 +0000493 if (Size == 0)
494 End = S->getLength();
495 else {
496 End = Begin + Size;
497 if (End >= S->getLength())
498 return make_error<RawError>(raw_error_code::index_out_of_bounds,
499 "Stream is not long enough!");
500 }
Zachary Turner36efbfa2016-09-09 19:00:49 +0000501
502 P.printNumber("Index", SI);
503 P.printString("Type", StreamPurposes[SI]);
504 P.printNumber("Size", S->getLength());
505 auto Blocks = File.getMsfLayout().StreamMap[SI];
506 P.printList("Blocks", Blocks);
507
Zachary Turner120faca2017-02-27 22:11:43 +0000508 BinaryStreamReader R(*S);
Zachary Turner72c5b642016-09-09 18:17:52 +0000509 ArrayRef<uint8_t> StreamData;
510 if (auto EC = R.readBytes(StreamData, S->getLength()))
Zachary Turnerd3117392016-06-03 19:28:33 +0000511 return EC;
Zachary Turner7159ab92017-04-28 00:43:38 +0000512 Size = End - Begin;
513 StreamData = StreamData.slice(Begin, Size);
514 P.printBinaryBlock("Data", StreamData, Begin);
Zachary Turnerd3117392016-06-03 19:28:33 +0000515 }
516 return Error::success();
517}
518
Zachary Turner760ad4d2017-01-20 22:42:09 +0000519Error LLVMOutputStyle::dumpStringTable() {
520 if (!opts::raw::DumpStringTable)
521 return Error::success();
522
523 auto IS = File.getStringTable();
524 if (!IS)
525 return IS.takeError();
526
527 DictScope D(P, "String Table");
528 for (uint32_t I : IS->name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000529 auto ES = IS->getStringForID(I);
530 if (!ES)
531 return ES.takeError();
532
533 if (ES->empty())
534 continue;
535 llvm::SmallString<32> Str;
536 Str.append("'");
537 Str.append(*ES);
538 Str.append("'");
539 P.printString(Str);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000540 }
541 return Error::success();
542}
543
Zachary Turnerd3117392016-06-03 19:28:33 +0000544Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000545 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000546 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000547 if (!File.hasPDBInfoStream()) {
548 P.printString("PDB Stream not present");
549 return Error::success();
550 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000551 auto IS = File.getPDBInfoStream();
552 if (!IS)
553 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000554
555 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000556 P.printNumber("Version", IS->getVersion());
557 P.printHex("Signature", IS->getSignature());
558 P.printNumber("Age", IS->getAge());
559 P.printObject("Guid", IS->getGuid());
Zachary Turner05d5e612017-03-16 20:19:11 +0000560 P.printHex("Features", IS->getFeatures());
Zachary Turner760ad4d2017-01-20 22:42:09 +0000561 {
562 DictScope DD(P, "Named Streams");
563 for (const auto &S : IS->getNamedStreams().entries())
564 P.printObject(S.getKey(), S.getValue());
565 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000566 return Error::success();
567}
568
Zachary Turner29da5db2017-01-25 21:17:40 +0000569namespace {
570class RecordBytesVisitor : public TypeVisitorCallbacks {
571public:
572 explicit RecordBytesVisitor(ScopedPrinter &P) : P(P) {}
573
574 Error visitTypeEnd(CVType &Record) override {
575 P.printBinaryBlock("Bytes", Record.content());
576 return Error::success();
577 }
578
579private:
580 ScopedPrinter &P;
581};
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000582}
583
Zachary Turnerd3117392016-06-03 19:28:33 +0000584Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
585 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
586
587 bool DumpRecordBytes = false;
588 bool DumpRecords = false;
Zachary Turner29da5db2017-01-25 21:17:40 +0000589 bool DumpTpiHash = false;
Zachary Turnerd3117392016-06-03 19:28:33 +0000590 StringRef Label;
591 StringRef VerLabel;
592 if (StreamIdx == StreamTPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000593 if (!File.hasPDBTpiStream()) {
594 P.printString("Type Info Stream (TPI) not present");
595 return Error::success();
596 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000597 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
598 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turner29da5db2017-01-25 21:17:40 +0000599 DumpTpiHash = opts::raw::DumpTpiHash;
Zachary Turnerd3117392016-06-03 19:28:33 +0000600 Label = "Type Info Stream (TPI)";
601 VerLabel = "TPI Version";
602 } else if (StreamIdx == StreamIPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000603 if (!File.hasPDBIpiStream()) {
604 P.printString("Type Info Stream (IPI) not present");
605 return Error::success();
606 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000607 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
608 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000609 Label = "Type Info Stream (IPI)";
610 VerLabel = "IPI Version";
611 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000612
Zachary Turner20d773c2017-05-04 23:53:01 +0000613 if (!DumpRecordBytes && !DumpRecords && !DumpTpiHash)
614 return Error::success();
Zachary Turner29da5db2017-01-25 21:17:40 +0000615
Zachary Turnera1657a92016-06-08 17:26:39 +0000616 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
617 : File.getPDBIpiStream();
618 if (!Tpi)
619 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000620
Zachary Turner29da5db2017-01-25 21:17:40 +0000621 std::unique_ptr<DictScope> StreamScope;
622 std::unique_ptr<ListScope> RecordScope;
623
Zachary Turner20d773c2017-05-04 23:53:01 +0000624 StreamScope = llvm::make_unique<DictScope>(P, Label);
625 P.printNumber(VerLabel, Tpi->getTpiVersion());
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000626 P.printNumber("Record count", Tpi->getNumTypeRecords());
Zachary Turner20d773c2017-05-04 23:53:01 +0000627
628 Optional<TypeDatabase> &StreamDB = (StreamIdx == StreamTPI) ? TypeDB : ItemDB;
629
630 std::vector<std::unique_ptr<TypeVisitorCallbacks>> Visitors;
631
632 Visitors.push_back(make_unique<TypeDeserializer>());
633 if (!StreamDB.hasValue()) {
Zachary Turner8c746732017-05-05 22:02:37 +0000634 StreamDB.emplace(Tpi->getNumTypeRecords());
Zachary Turner20d773c2017-05-04 23:53:01 +0000635 Visitors.push_back(make_unique<TypeDatabaseVisitor>(*StreamDB));
Zachary Turner29da5db2017-01-25 21:17:40 +0000636 }
Zachary Turner20d773c2017-05-04 23:53:01 +0000637 // If we're in dump mode, add a dumper with the appropriate detail level.
638 if (DumpRecords) {
639 std::unique_ptr<TypeVisitorCallbacks> Dumper;
640 if (opts::raw::CompactRecords)
641 Dumper = make_unique<CompactTypeDumpVisitor>(*StreamDB, &P);
642 else {
643 assert(TypeDB.hasValue());
Zachary Turner29da5db2017-01-25 21:17:40 +0000644
Zachary Turner20d773c2017-05-04 23:53:01 +0000645 auto X = make_unique<TypeDumpVisitor>(*TypeDB, &P, false);
646 if (StreamIdx == StreamIPI)
647 X->setItemDB(*ItemDB);
648 Dumper = std::move(X);
649 }
650 Visitors.push_back(std::move(Dumper));
651 }
652 if (DumpRecordBytes)
653 Visitors.push_back(make_unique<RecordBytesVisitor>(P));
Zachary Turner29da5db2017-01-25 21:17:40 +0000654
655 // We always need to deserialize and add it to the type database. This is
656 // true if even if we're not dumping anything, because we could need the
657 // type database for the purposes of dumping symbols.
Zachary Turner44a643c2017-01-12 22:28:15 +0000658 TypeVisitorCallbackPipeline Pipeline;
Zachary Turner20d773c2017-05-04 23:53:01 +0000659 for (const auto &V : Visitors)
660 Pipeline.addCallbackToPipeline(*V);
Zachary Turner29da5db2017-01-25 21:17:40 +0000661
662 CVTypeVisitor Visitor(Pipeline);
663
664 if (DumpRecords || DumpRecordBytes)
665 RecordScope = llvm::make_unique<ListScope>(P, "Records");
666
667 bool HadError = false;
668
669 TypeIndex T(TypeIndex::FirstNonSimpleIndex);
670 for (auto Type : Tpi->types(&HadError)) {
671 std::unique_ptr<DictScope> OneRecordScope;
672
673 if ((DumpRecords || DumpRecordBytes) && !opts::raw::CompactRecords)
674 OneRecordScope = llvm::make_unique<DictScope>(P, "");
675
676 if (auto EC = Visitor.visitTypeRecord(Type))
677 return EC;
Zachary Turneredef1452017-05-02 16:56:09 +0000678 T.setIndex(T.getIndex() + 1);
Zachary Turner29da5db2017-01-25 21:17:40 +0000679 }
680 if (HadError)
681 return make_error<RawError>(raw_error_code::corrupt_file,
682 "TPI stream contained corrupt record");
683
684 if (DumpTpiHash) {
685 DictScope DD(P, "Hash");
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000686 P.printNumber("Number of Hash Buckets", Tpi->getNumHashBuckets());
Zachary Turner29da5db2017-01-25 21:17:40 +0000687 P.printNumber("Hash Key Size", Tpi->getHashKeySize());
688 P.printList("Values", Tpi->getHashValues());
689
690 ListScope LHA(P, "Adjusters");
691 auto ExpectedST = File.getStringTable();
692 if (!ExpectedST)
693 return ExpectedST.takeError();
694 const auto &ST = *ExpectedST;
695 for (const auto &E : Tpi->getHashAdjusters()) {
696 DictScope DHA(P);
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000697 auto Name = ST.getStringForID(E.first);
698 if (!Name)
699 return Name.takeError();
700
701 P.printString("Type", *Name);
Zachary Turner29da5db2017-01-25 21:17:40 +0000702 P.printHex("TI", E.second);
703 }
704 }
705
Zachary Turner20d773c2017-05-04 23:53:01 +0000706 ListScope L(P, "TypeIndexOffsets");
707 for (const auto &IO : Tpi->getTypeIndexOffsets()) {
708 P.printString(formatv("Index: {0:x}, Offset: {1:N}", IO.Type.getIndex(),
709 (uint32_t)IO.Offset)
710 .str());
Zachary Turner29da5db2017-01-25 21:17:40 +0000711 }
712
Zachary Turnerd3117392016-06-03 19:28:33 +0000713 P.flush();
714 return Error::success();
715}
716
Zachary Turner20d773c2017-05-04 23:53:01 +0000717Error LLVMOutputStyle::buildTypeDatabase(uint32_t SN) {
718 assert(SN == StreamIPI || SN == StreamTPI);
719
720 auto &DB = (SN == StreamIPI) ? ItemDB : TypeDB;
721
722 if (DB.hasValue())
723 return Error::success();
724
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000725 auto Tpi =
726 (SN == StreamTPI) ? File.getPDBTpiStream() : File.getPDBIpiStream();
727
728 if (!Tpi)
729 return Tpi.takeError();
730
Zachary Turner8c746732017-05-05 22:02:37 +0000731 DB.emplace(Tpi->getNumTypeRecords());
732
Zachary Turner20d773c2017-05-04 23:53:01 +0000733 TypeVisitorCallbackPipeline Pipeline;
734 TypeDeserializer Deserializer;
735 TypeDatabaseVisitor DBV(*DB);
736 Pipeline.addCallbackToPipeline(Deserializer);
737 Pipeline.addCallbackToPipeline(DBV);
738
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000739 auto HashValues = Tpi->getHashValues();
740 std::unique_ptr<TpiHashVerifier> HashVerifier;
741 if (!HashValues.empty()) {
742 HashVerifier =
743 make_unique<TpiHashVerifier>(HashValues, Tpi->getNumHashBuckets());
744 Pipeline.addCallbackToPipeline(*HashVerifier);
745 }
Zachary Turner20d773c2017-05-04 23:53:01 +0000746
747 CVTypeVisitor Visitor(Pipeline);
748 return Visitor.visitTypeStream(Tpi->types(nullptr));
749}
750
Zachary Turnerd3117392016-06-03 19:28:33 +0000751Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000752 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
753 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
754 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000755 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000756 if (!File.hasPDBDbiStream()) {
757 P.printString("DBI Stream not present");
758 return Error::success();
759 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000760
Zachary Turnera1657a92016-06-08 17:26:39 +0000761 auto DS = File.getPDBDbiStream();
762 if (!DS)
763 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000764
765 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000766 P.printNumber("Dbi Version", DS->getDbiVersion());
767 P.printNumber("Age", DS->getAge());
768 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
769 P.printBoolean("Has CTypes", DS->hasCTypes());
770 P.printBoolean("Is Stripped", DS->isStripped());
771 P.printObject("Machine Type", DS->getMachineType());
772 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
773 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
774 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000775
Zachary Turnera1657a92016-06-08 17:26:39 +0000776 uint16_t Major = DS->getBuildMajorVersion();
777 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000778 P.printVersion("Toolchain Version", Major, Minor);
779
780 std::string DllName;
781 raw_string_ostream DllStream(DllName);
782 DllStream << "mspdb" << Major << Minor << ".dll version";
783 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000784 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000785
786 if (DumpModules) {
787 ListScope L(P, "Modules");
Zachary Turner1eb9a022017-05-04 23:53:29 +0000788 const DbiModuleList &Modules = DS->modules();
789 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
790 const DbiModuleDescriptor &Modi = Modules.getModuleDescriptor(I);
Zachary Turnerd3117392016-06-03 19:28:33 +0000791 DictScope DD(P);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000792 P.printString("Name", Modi.getModuleName().str());
793 P.printNumber("Debug Stream Index", Modi.getModuleStreamIndex());
794 P.printString("Object File Name", Modi.getObjFileName().str());
795 P.printNumber("Num Files", Modi.getNumberOfFiles());
796 P.printNumber("Source File Name Idx", Modi.getSourceFileNameIndex());
797 P.printNumber("Pdb File Name Idx", Modi.getPdbFilePathNameIndex());
798 P.printNumber("Line Info Byte Size", Modi.getC11LineInfoByteSize());
799 P.printNumber("C13 Line Info Byte Size", Modi.getC13LineInfoByteSize());
800 P.printNumber("Symbol Byte Size", Modi.getSymbolDebugInfoByteSize());
801 P.printNumber("Type Server Index", Modi.getTypeServerIndex());
802 P.printBoolean("Has EC Info", Modi.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000803 if (opts::raw::DumpModuleFiles) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000804 std::string FileListName = to_string(Modules.getSourceFileCount(I)) +
805 " Contributing Source Files";
Zachary Turnerd3117392016-06-03 19:28:33 +0000806 ListScope LL(P, FileListName);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000807 for (auto File : Modules.source_files(I))
808 P.printString(File);
Zachary Turnerd3117392016-06-03 19:28:33 +0000809 }
Zachary Turner1eb9a022017-05-04 23:53:29 +0000810 bool HasModuleDI = (Modi.getModuleStreamIndex() < File.getNumStreams());
Zachary Turnerd3117392016-06-03 19:28:33 +0000811 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000812 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
813 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000814 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000815 File.getMsfLayout(), File.getMsfBuffer(),
Zachary Turner1eb9a022017-05-04 23:53:29 +0000816 Modi.getModuleStreamIndex());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000817
Zachary Turner1eb9a022017-05-04 23:53:29 +0000818 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000819 if (auto EC = ModS.reload())
820 return EC;
821
822 if (ShouldDumpSymbols) {
Zachary Turner20d773c2017-05-04 23:53:01 +0000823 if (auto EC = buildTypeDatabase(StreamTPI))
824 return EC;
825
Zachary Turnerd3117392016-06-03 19:28:33 +0000826 ListScope SS(P, "Symbols");
Zachary Turner20d773c2017-05-04 23:53:01 +0000827 codeview::CVSymbolDumper SD(P, *TypeDB, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000828 bool HadError = false;
Zachary Turner0d840742016-10-07 21:34:46 +0000829 for (auto S : ModS.symbols(&HadError)) {
830 DictScope LL(P, "");
831 if (opts::raw::DumpModuleSyms) {
832 if (auto EC = SD.dump(S)) {
833 llvm::consumeError(std::move(EC));
834 HadError = true;
835 break;
836 }
837 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000838 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000839 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000840 }
841 if (HadError)
842 return make_error<RawError>(
843 raw_error_code::corrupt_file,
844 "DBI stream contained corrupt symbol record");
845 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000846 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000847 ListScope SS(P, "LineInfo");
Zachary Turner20d773c2017-05-04 23:53:01 +0000848 if (auto EC = buildTypeDatabase(StreamIPI))
849 return EC;
Zachary Turnerd3117392016-06-03 19:28:33 +0000850
Zachary Turner20d773c2017-05-04 23:53:01 +0000851 C13RawVisitor V(P, File, *ItemDB);
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000852 if (auto EC = codeview::visitModuleDebugFragments(
853 ModS.linesAndChecksums(), V))
854 return EC;
Zachary Turnerd3117392016-06-03 19:28:33 +0000855 }
856 }
857 }
858 }
859 return Error::success();
860}
861
862Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000863 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000864 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000865 if (!File.hasPDBDbiStream()) {
866 P.printString("DBI Stream not present");
867 return Error::success();
868 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000869
Zachary Turnera1657a92016-06-08 17:26:39 +0000870 auto Dbi = File.getPDBDbiStream();
871 if (!Dbi)
872 return Dbi.takeError();
873
Zachary Turnerd3117392016-06-03 19:28:33 +0000874 ListScope L(P, "Section Contributions");
875 class Visitor : public ISectionContribVisitor {
876 public:
877 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
878 void visit(const SectionContrib &SC) override {
879 DictScope D(P, "Contribution");
880 P.printNumber("ISect", SC.ISect);
881 P.printNumber("Off", SC.Off);
882 P.printNumber("Size", SC.Size);
883 P.printFlags("Characteristics", SC.Characteristics,
884 codeview::getImageSectionCharacteristicNames(),
885 COFF::SectionCharacteristics(0x00F00000));
886 {
887 DictScope DD(P, "Module");
888 P.printNumber("Index", SC.Imod);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000889 const DbiModuleList &Modules = DS.modules();
890 if (Modules.getModuleCount() > SC.Imod) {
891 P.printString("Name",
892 Modules.getModuleDescriptor(SC.Imod).getModuleName());
Zachary Turnerd3117392016-06-03 19:28:33 +0000893 }
894 }
895 P.printNumber("Data CRC", SC.DataCrc);
896 P.printNumber("Reloc CRC", SC.RelocCrc);
897 P.flush();
898 }
899 void visit(const SectionContrib2 &SC) override {
900 visit(SC.Base);
901 P.printNumber("ISect Coff", SC.ISectCoff);
902 P.flush();
903 }
904
905 private:
906 ScopedPrinter &P;
907 DbiStream &DS;
908 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000909 Visitor V(P, *Dbi);
910 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000911 return Error::success();
912}
913
914Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000915 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000916 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000917 if (!File.hasPDBDbiStream()) {
918 P.printString("DBI Stream not present");
919 return Error::success();
920 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000921
Zachary Turnera1657a92016-06-08 17:26:39 +0000922 auto Dbi = File.getPDBDbiStream();
923 if (!Dbi)
924 return Dbi.takeError();
925
Zachary Turnerd3117392016-06-03 19:28:33 +0000926 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000927 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000928 DictScope D(P, "Entry");
929 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000930 P.printNumber("Ovl", M.Ovl);
931 P.printNumber("Group", M.Group);
932 P.printNumber("Frame", M.Frame);
933 P.printNumber("SecName", M.SecName);
934 P.printNumber("ClassName", M.ClassName);
935 P.printNumber("Offset", M.Offset);
936 P.printNumber("SecByteLength", M.SecByteLength);
937 P.flush();
938 }
939 return Error::success();
940}
941
942Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000943 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000944 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000945 if (!File.hasPDBPublicsStream()) {
946 P.printString("Publics Stream not present");
947 return Error::success();
948 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000949
Zachary Turnera1657a92016-06-08 17:26:39 +0000950 auto Publics = File.getPDBPublicsStream();
951 if (!Publics)
952 return Publics.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000953 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000954
955 auto Dbi = File.getPDBDbiStream();
956 if (!Dbi)
957 return Dbi.takeError();
958
959 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
960 P.printNumber("SymHash", Publics->getSymHash());
961 P.printNumber("AddrMap", Publics->getAddrMap());
962 P.printNumber("Number of buckets", Publics->getNumBuckets());
963 P.printList("Hash Buckets", Publics->getHashBuckets());
964 P.printList("Address Map", Publics->getAddressMap());
965 P.printList("Thunk Map", Publics->getThunkMap());
966 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000967 printSectionOffset);
968 ListScope L(P, "Symbols");
Zachary Turner20d773c2017-05-04 23:53:01 +0000969 if (auto EC = buildTypeDatabase(StreamTPI))
970 return EC;
971
972 codeview::CVSymbolDumper SD(P, *TypeDB, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000973 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000974 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000975 DictScope DD(P, "");
976
Zachary Turner0d840742016-10-07 21:34:46 +0000977 if (auto EC = SD.dump(S)) {
978 HadError = true;
979 break;
980 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000981 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000982 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000983 }
984 if (HadError)
985 return make_error<RawError>(
986 raw_error_code::corrupt_file,
987 "Public symbol stream contained corrupt record");
988
989 return Error::success();
990}
991
992Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000993 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000994 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000995 if (!File.hasPDBDbiStream()) {
996 P.printString("DBI Stream not present");
997 return Error::success();
998 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000999
Zachary Turnera1657a92016-06-08 17:26:39 +00001000 auto Dbi = File.getPDBDbiStream();
1001 if (!Dbi)
1002 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +00001003
1004 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +00001005 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +00001006 DictScope DD(P, "");
1007
1008 // If a name is 8 characters long, there is no NUL character at end.
1009 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
1010 P.printString("Name", Name);
1011 P.printNumber("Virtual Size", Section.VirtualSize);
1012 P.printNumber("Virtual Address", Section.VirtualAddress);
1013 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
1014 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
1015 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
1016 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
1017 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
1018 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +00001019 P.printFlags("Characteristics", Section.Characteristics,
1020 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +00001021 }
1022 return Error::success();
1023}
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001024
1025Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001026 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001027 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +00001028 if (!File.hasPDBDbiStream()) {
1029 P.printString("DBI Stream not present");
1030 return Error::success();
1031 }
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001032
Zachary Turnera1657a92016-06-08 17:26:39 +00001033 auto Dbi = File.getPDBDbiStream();
1034 if (!Dbi)
1035 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001036
1037 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +00001038 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001039 DictScope DD(P, "");
1040 P.printNumber("Offset", Fpo.Offset);
1041 P.printNumber("Size", Fpo.Size);
1042 P.printNumber("Number of locals", Fpo.NumLocals);
1043 P.printNumber("Number of params", Fpo.NumParams);
1044 P.printNumber("Size of Prolog", Fpo.getPrologSize());
1045 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
1046 P.printBoolean("Has SEH", Fpo.hasSEH());
1047 P.printBoolean("Use BP", Fpo.useBP());
1048 P.printNumber("Frame Pointer", Fpo.getFP());
1049 }
1050 return Error::success();
1051}
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001052
Zachary Turner7120a472016-06-06 20:37:05 +00001053void LLVMOutputStyle::flush() { P.flush(); }