blob: 4b94f8b700687028ba6f333c7d6a6ec33a00cb8c [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/CVTypeVisitor.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000018#include "llvm/DebugInfo/CodeView/EnumTables.h"
Zachary Turner0c60f262017-05-18 23:03:06 +000019#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.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"
Zachary Turner629cb7d2017-01-11 23:24:22 +000028#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
29#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000030#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000031#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptor.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000032#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
33#include "llvm/DebugInfo/PDB/Native/EnumTables.h"
34#include "llvm/DebugInfo/PDB/Native/GlobalsStream.h"
35#include "llvm/DebugInfo/PDB/Native/ISectionContribVisitor.h"
36#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner67c56012017-04-27 16:11:19 +000037#include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000038#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
39#include "llvm/DebugInfo/PDB/Native/PublicsStream.h"
40#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerbedc85f2017-05-04 23:53:54 +000041#include "llvm/DebugInfo/PDB/Native/TpiHashing.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000042#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000043#include "llvm/DebugInfo/PDB/PDBExtras.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000044#include "llvm/Object/COFF.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000045#include "llvm/Support/BinaryStreamReader.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000046#include "llvm/Support/FormatVariadic.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000047
48#include <unordered_map>
49
50using namespace llvm;
51using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000052using namespace llvm::msf;
Zachary Turnerd3117392016-06-03 19:28:33 +000053using namespace llvm::pdb;
54
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +000055namespace {
56struct PageStats {
57 explicit PageStats(const BitVector &FreePages)
58 : Upm(FreePages), ActualUsedPages(FreePages.size()),
59 MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) {
60 const_cast<BitVector &>(Upm).flip();
61 // To calculate orphaned pages, we start with the set of pages that the
62 // MSF thinks are used. Each time we find one that actually *is* used,
63 // we unset it. Whichever bits remain set at the end are orphaned.
64 OrphanedPages = Upm;
65 }
66
67 // The inverse of the MSF File's copy of the Fpm. The basis for which we
68 // determine the allocation status of each page.
69 const BitVector Upm;
70
71 // Pages which are marked as used in the FPM and are used at least once.
72 BitVector ActualUsedPages;
73
74 // Pages which are marked as used in the FPM but are used more than once.
75 BitVector MultiUsePages;
76
77 // Pages which are marked as used in the FPM but are not used at all.
78 BitVector OrphanedPages;
79
80 // Pages which are marked free in the FPM but are used.
81 BitVector UseAfterFreePages;
82};
Zachary Turner5b6e4e02017-04-29 01:13:21 +000083
Zachary Turner5b6e4e02017-04-29 01:13:21 +000084class C13RawVisitor : public C13DebugFragmentVisitor {
85public:
Zachary Turner0c60f262017-05-18 23:03:06 +000086 C13RawVisitor(ScopedPrinter &P, PDBFile &F, LazyRandomTypeCollection &IPI)
Zachary Turneredef1452017-05-02 16:56:09 +000087 : C13DebugFragmentVisitor(F), P(P), IPI(IPI) {}
Zachary Turner5b6e4e02017-04-29 01:13:21 +000088
89 Error handleLines() override {
Zachary Turneredef1452017-05-02 16:56:09 +000090 if (Lines.empty())
91 return Error::success();
92
Zachary Turner5b6e4e02017-04-29 01:13:21 +000093 DictScope DD(P, "Lines");
94
95 for (const auto &Fragment : Lines) {
Zachary Turner8a2ebfb2017-05-01 23:27:42 +000096 DictScope DDD(P, "Block");
Zachary Turner5b6e4e02017-04-29 01:13:21 +000097 P.printNumber("RelocSegment", Fragment.header()->RelocSegment);
98 P.printNumber("RelocOffset", Fragment.header()->RelocOffset);
99 P.printNumber("CodeSize", Fragment.header()->CodeSize);
Zachary Turner8a2ebfb2017-05-01 23:27:42 +0000100 P.printBoolean("HasColumns", Fragment.hasColumnInfo());
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000101
102 for (const auto &L : Fragment) {
103 DictScope DDDD(P, "Lines");
104
105 if (auto EC = printFileName("FileName", L.NameIndex))
106 return EC;
107
108 for (const auto &N : L.LineNumbers) {
109 DictScope DDD(P, "Line");
110 LineInfo LI(N.Flags);
111 P.printNumber("Offset", N.Offset);
112 if (LI.isAlwaysStepInto())
113 P.printString("StepInto", StringRef("Always"));
114 else if (LI.isNeverStepInto())
115 P.printString("StepInto", StringRef("Never"));
116 else
117 P.printNumber("LineNumberStart", LI.getStartLine());
118 P.printNumber("EndDelta", LI.getLineDelta());
119 P.printBoolean("IsStatement", LI.isStatement());
120 }
121 for (const auto &C : L.Columns) {
122 DictScope DDD(P, "Column");
123 P.printNumber("Start", C.StartColumn);
124 P.printNumber("End", C.EndColumn);
125 }
126 }
127 }
128
129 return Error::success();
130 }
131
132 Error handleFileChecksums() override {
Zachary Turneredef1452017-05-02 16:56:09 +0000133 if (!Checksums.hasValue())
134 return Error::success();
135
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000136 DictScope DD(P, "FileChecksums");
137 for (const auto &CS : *Checksums) {
138 DictScope DDD(P, "Checksum");
139 if (auto Result = getNameFromStringTable(CS.FileNameOffset))
140 P.printString("FileName", *Result);
141 else
142 return Result.takeError();
143 P.printEnum("Kind", uint8_t(CS.Kind), getFileChecksumNames());
144 P.printBinaryBlock("Checksum", CS.Checksum);
145 }
146 return Error::success();
147 }
148
Zachary Turneredef1452017-05-02 16:56:09 +0000149 Error handleInlineeLines() override {
150 if (InlineeLines.empty())
151 return Error::success();
152
153 DictScope D(P, "InlineeLines");
154 for (const auto &IL : InlineeLines) {
155 P.printBoolean("HasExtraFiles", IL.hasExtraFiles());
156 ListScope LS(P, "Lines");
157 for (const auto &L : IL) {
158 DictScope DDD(P, "Inlinee");
159 if (auto EC = printFileName("FileName", L.Header->FileID))
160 return EC;
161
Zachary Turner0c60f262017-05-18 23:03:06 +0000162 if (auto EC = dumpTypeRecord("Function", L.Header->Inlinee))
Zachary Turneredef1452017-05-02 16:56:09 +0000163 return EC;
164 P.printNumber("SourceLine", L.Header->SourceLineNum);
165 if (IL.hasExtraFiles()) {
166 ListScope DDDD(P, "ExtraFiles");
167 for (const auto &EF : L.ExtraFiles) {
168 if (auto EC = printFileName("File", EF))
169 return EC;
170 }
171 }
172 }
173 }
174 return Error::success();
175 }
176
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000177private:
Zachary Turner0c60f262017-05-18 23:03:06 +0000178 Error dumpTypeRecord(StringRef Label, TypeIndex Index) {
179 CompactTypeDumpVisitor CTDV(IPI, Index, &P);
Zachary Turneredef1452017-05-02 16:56:09 +0000180 DictScope D(P, Label);
Zachary Turner0c60f262017-05-18 23:03:06 +0000181 if (IPI.contains(Index)) {
182 CVType Type = IPI.getType(Index);
Zachary Turner1d795c42017-05-17 16:39:06 +0000183 if (auto EC = codeview::visitTypeRecord(Type, CTDV))
Zachary Turneredef1452017-05-02 16:56:09 +0000184 return EC;
185 } else {
186 P.printString(
187 llvm::formatv("Index: {0:x} (unknown function)", Index.getIndex())
188 .str());
189 }
190 return Error::success();
191 }
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000192 Error printFileName(StringRef Label, uint32_t Offset) {
193 if (auto Result = getNameFromChecksumsBuffer(Offset)) {
194 P.printString(Label, *Result);
195 return Error::success();
196 } else
197 return Result.takeError();
198 }
199
200 ScopedPrinter &P;
Zachary Turner0c60f262017-05-18 23:03:06 +0000201 LazyRandomTypeCollection &IPI;
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000202};
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000203}
204
205static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) {
206 if (Stats.Upm.test(UsedIndex)) {
207 if (Stats.ActualUsedPages.test(UsedIndex))
208 Stats.MultiUsePages.set(UsedIndex);
209 Stats.ActualUsedPages.set(UsedIndex);
210 Stats.OrphanedPages.reset(UsedIndex);
211 } else {
212 // The MSF doesn't think this page is used, but it is.
213 Stats.UseAfterFreePages.set(UsedIndex);
214 }
215}
216
Zachary Turnerd3117392016-06-03 19:28:33 +0000217static void printSectionOffset(llvm::raw_ostream &OS,
218 const SectionOffset &Off) {
219 OS << Off.Off << ", " << Off.Isect;
220}
221
Zachary Turner629cb7d2017-01-11 23:24:22 +0000222LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) : File(File), P(outs()) {}
Zachary Turnerd3117392016-06-03 19:28:33 +0000223
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000224Error LLVMOutputStyle::dump() {
225 if (auto EC = dumpFileHeaders())
226 return EC;
227
228 if (auto EC = dumpStreamSummary())
229 return EC;
230
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000231 if (auto EC = dumpFreePageMap())
232 return EC;
233
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000234 if (auto EC = dumpStreamBlocks())
235 return EC;
236
Zachary Turner72c5b642016-09-09 18:17:52 +0000237 if (auto EC = dumpBlockRanges())
238 return EC;
239
240 if (auto EC = dumpStreamBytes())
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000241 return EC;
242
Zachary Turner760ad4d2017-01-20 22:42:09 +0000243 if (auto EC = dumpStringTable())
244 return EC;
245
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000246 if (auto EC = dumpInfoStream())
247 return EC;
248
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000249 if (auto EC = dumpTpiStream(StreamTPI))
250 return EC;
251
252 if (auto EC = dumpTpiStream(StreamIPI))
253 return EC;
254
255 if (auto EC = dumpDbiStream())
256 return EC;
257
258 if (auto EC = dumpSectionContribs())
259 return EC;
260
261 if (auto EC = dumpSectionMap())
262 return EC;
263
Bob Haarman653baa22016-10-21 19:43:19 +0000264 if (auto EC = dumpGlobalsStream())
265 return EC;
266
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000267 if (auto EC = dumpPublicsStream())
268 return EC;
269
270 if (auto EC = dumpSectionHeaders())
271 return EC;
272
273 if (auto EC = dumpFpoStream())
274 return EC;
275
276 flush();
277
278 return Error::success();
279}
280
Zachary Turnerd3117392016-06-03 19:28:33 +0000281Error LLVMOutputStyle::dumpFileHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000282 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000283 return Error::success();
284
285 DictScope D(P, "FileHeaders");
286 P.printNumber("BlockSize", File.getBlockSize());
Zachary Turnerb927e022016-07-15 22:17:19 +0000287 P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock());
Zachary Turnerd3117392016-06-03 19:28:33 +0000288 P.printNumber("NumBlocks", File.getBlockCount());
289 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
290 P.printNumber("Unknown1", File.getUnknown1());
291 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
292 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
Zachary Turnerd3117392016-06-03 19:28:33 +0000293
294 // The directory is not contiguous. Instead, the block map contains a
295 // contiguous list of block numbers whose contents, when concatenated in
296 // order, make up the directory.
297 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
298 P.printNumber("NumStreams", File.getNumStreams());
299 return Error::success();
300}
301
Zachary Turner36efbfa2016-09-09 19:00:49 +0000302Error LLVMOutputStyle::dumpStreamSummary() {
303 if (!opts::raw::DumpStreamSummary)
304 return Error::success();
305
Zachary Turner6ac232c2017-03-13 23:28:25 +0000306 if (StreamPurposes.empty())
307 discoverStreamPurposes(File, StreamPurposes);
Zachary Turner36efbfa2016-09-09 19:00:49 +0000308
309 uint32_t StreamCount = File.getNumStreams();
310
311 ListScope L(P, "Streams");
312 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
313 std::string Label("Stream ");
314 Label += to_string(StreamIdx);
315
316 std::string Value = "[" + StreamPurposes[StreamIdx] + "] (";
317 Value += to_string(File.getStreamByteSize(StreamIdx));
318 Value += " bytes)";
319
320 P.printString(Label, Value);
321 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000322
Zachary Turnerd3117392016-06-03 19:28:33 +0000323 P.flush();
324 return Error::success();
325}
326
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000327Error LLVMOutputStyle::dumpFreePageMap() {
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000328 if (!opts::raw::DumpPageStats)
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000329 return Error::success();
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000330
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000331 // Start with used pages instead of free pages because
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000332 // the number of free pages is far larger than used pages.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000333 BitVector FPM = File.getMsfLayout().FreePageMap;
334
335 PageStats PS(FPM);
336
337 recordKnownUsedPage(PS, 0); // MSF Super Block
338
Zachary Turner8cf51c32016-08-03 16:53:21 +0000339 uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout());
340 uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout());
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000341 for (uint32_t I = 0; I < NumSections; ++I) {
342 uint32_t Fpm0 = 1 + BlocksPerSection * I;
343 // 2 Fpm blocks spaced at `getBlockSize()` block intervals
344 recordKnownUsedPage(PS, Fpm0);
345 recordKnownUsedPage(PS, Fpm0 + 1);
346 }
347
348 recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table
349
Rui Ueyama22e67382016-08-02 23:22:46 +0000350 for (auto DB : File.getDirectoryBlockArray())
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000351 recordKnownUsedPage(PS, DB);
Rui Ueyama22e67382016-08-02 23:22:46 +0000352
353 // Record pages used by streams. Note that pages for stream 0
354 // are considered being unused because that's what MSVC tools do.
355 // Stream 0 doesn't contain actual data, so it makes some sense,
356 // though it's a bit confusing to us.
357 for (auto &SE : File.getStreamMap().drop_front(1))
358 for (auto &S : SE)
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000359 recordKnownUsedPage(PS, S);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000360
361 dumpBitVector("Msf Free Pages", FPM);
362 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
363 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
364 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000365 return Error::success();
366}
367
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000368void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
369 std::vector<uint32_t> Vec;
370 for (uint32_t I = 0, E = V.size(); I != E; ++I)
371 if (V[I])
372 Vec.push_back(I);
373 P.printList(Name, Vec);
374}
375
Bob Haarman653baa22016-10-21 19:43:19 +0000376Error LLVMOutputStyle::dumpGlobalsStream() {
377 if (!opts::raw::DumpGlobals)
378 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000379 if (!File.hasPDBGlobalsStream()) {
380 P.printString("Globals Stream not present");
381 return Error::success();
382 }
Bob Haarman653baa22016-10-21 19:43:19 +0000383
Bob Haarman653baa22016-10-21 19:43:19 +0000384 auto Globals = File.getPDBGlobalsStream();
385 if (!Globals)
Bob Haarman312fd0e2016-12-06 00:55:55 +0000386 return Globals.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000387 DictScope D(P, "Globals Stream");
Bob Haarman653baa22016-10-21 19:43:19 +0000388
389 auto Dbi = File.getPDBDbiStream();
390 if (!Dbi)
391 return Dbi.takeError();
392
393 P.printNumber("Stream number", Dbi->getGlobalSymbolStreamIndex());
394 P.printNumber("Number of buckets", Globals->getNumBuckets());
395 P.printList("Hash Buckets", Globals->getHashBuckets());
396
397 return Error::success();
398}
399
Zachary Turnerd3117392016-06-03 19:28:33 +0000400Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000401 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000402 return Error::success();
403
404 ListScope L(P, "StreamBlocks");
405 uint32_t StreamCount = File.getNumStreams();
406 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
407 std::string Name("Stream ");
408 Name += to_string(StreamIdx);
409 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
410 P.printList(Name, StreamBlocks);
411 }
412 return Error::success();
413}
414
Zachary Turner72c5b642016-09-09 18:17:52 +0000415Error LLVMOutputStyle::dumpBlockRanges() {
416 if (!opts::raw::DumpBlockRange.hasValue())
417 return Error::success();
418 auto &R = *opts::raw::DumpBlockRange;
419 uint32_t Max = R.Max.getValueOr(R.Min);
420
421 if (Max < R.Min)
422 return make_error<StringError>(
423 "Invalid block range specified. Max < Min",
424 std::make_error_code(std::errc::bad_address));
425 if (Max >= File.getBlockCount())
426 return make_error<StringError>(
427 "Invalid block range specified. Requested block out of bounds",
428 std::make_error_code(std::errc::bad_address));
429
430 DictScope D(P, "Block Data");
431 for (uint32_t I = R.Min; I <= Max; ++I) {
432 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
433 if (!ExpectedData)
434 return ExpectedData.takeError();
435 std::string Label;
436 llvm::raw_string_ostream S(Label);
437 S << "Block " << I;
438 S.flush();
439 P.printBinaryBlock(Label, *ExpectedData);
440 }
441
442 return Error::success();
443}
444
Zachary Turner7159ab92017-04-28 00:43:38 +0000445static Error parseStreamSpec(StringRef Str, uint32_t &SI, uint32_t &Offset,
446 uint32_t &Size) {
447 if (Str.consumeInteger(0, SI))
448 return make_error<RawError>(raw_error_code::invalid_format,
449 "Invalid Stream Specification");
450 if (Str.consume_front(":")) {
451 if (Str.consumeInteger(0, Offset))
452 return make_error<RawError>(raw_error_code::invalid_format,
453 "Invalid Stream Specification");
454 }
455 if (Str.consume_front("@")) {
456 if (Str.consumeInteger(0, Size))
457 return make_error<RawError>(raw_error_code::invalid_format,
458 "Invalid Stream Specification");
459 }
460 if (!Str.empty())
461 return make_error<RawError>(raw_error_code::invalid_format,
462 "Invalid Stream Specification");
463 return Error::success();
464}
465
Zachary Turner72c5b642016-09-09 18:17:52 +0000466Error LLVMOutputStyle::dumpStreamBytes() {
467 if (opts::raw::DumpStreamData.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000468 return Error::success();
469
Zachary Turner6ac232c2017-03-13 23:28:25 +0000470 if (StreamPurposes.empty())
471 discoverStreamPurposes(File, StreamPurposes);
Zachary Turner36efbfa2016-09-09 19:00:49 +0000472
Zachary Turner72c5b642016-09-09 18:17:52 +0000473 DictScope D(P, "Stream Data");
Zachary Turner7159ab92017-04-28 00:43:38 +0000474 for (auto &Str : opts::raw::DumpStreamData) {
475 uint32_t SI = 0;
476 uint32_t Begin = 0;
477 uint32_t Size = 0;
478 uint32_t End = 0;
479
480 if (auto EC = parseStreamSpec(Str, SI, Begin, Size))
481 return EC;
482
Zachary Turner72c5b642016-09-09 18:17:52 +0000483 if (SI >= File.getNumStreams())
484 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000485
Zachary Turner72c5b642016-09-09 18:17:52 +0000486 auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(),
487 File.getMsfBuffer(), SI);
488 if (!S)
489 continue;
Zachary Turner36efbfa2016-09-09 19:00:49 +0000490 DictScope DD(P, "Stream");
Zachary Turner7159ab92017-04-28 00:43:38 +0000491 if (Size == 0)
492 End = S->getLength();
493 else {
494 End = Begin + Size;
495 if (End >= S->getLength())
496 return make_error<RawError>(raw_error_code::index_out_of_bounds,
497 "Stream is not long enough!");
498 }
Zachary Turner36efbfa2016-09-09 19:00:49 +0000499
500 P.printNumber("Index", SI);
501 P.printString("Type", StreamPurposes[SI]);
502 P.printNumber("Size", S->getLength());
503 auto Blocks = File.getMsfLayout().StreamMap[SI];
504 P.printList("Blocks", Blocks);
505
Zachary Turner120faca2017-02-27 22:11:43 +0000506 BinaryStreamReader R(*S);
Zachary Turner72c5b642016-09-09 18:17:52 +0000507 ArrayRef<uint8_t> StreamData;
508 if (auto EC = R.readBytes(StreamData, S->getLength()))
Zachary Turnerd3117392016-06-03 19:28:33 +0000509 return EC;
Zachary Turner7159ab92017-04-28 00:43:38 +0000510 Size = End - Begin;
511 StreamData = StreamData.slice(Begin, Size);
512 P.printBinaryBlock("Data", StreamData, Begin);
Zachary Turnerd3117392016-06-03 19:28:33 +0000513 }
514 return Error::success();
515}
516
Zachary Turner760ad4d2017-01-20 22:42:09 +0000517Error LLVMOutputStyle::dumpStringTable() {
518 if (!opts::raw::DumpStringTable)
519 return Error::success();
520
521 auto IS = File.getStringTable();
522 if (!IS)
523 return IS.takeError();
524
525 DictScope D(P, "String Table");
526 for (uint32_t I : IS->name_ids()) {
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000527 auto ES = IS->getStringForID(I);
528 if (!ES)
529 return ES.takeError();
530
531 if (ES->empty())
532 continue;
533 llvm::SmallString<32> Str;
534 Str.append("'");
535 Str.append(*ES);
536 Str.append("'");
537 P.printString(Str);
Zachary Turner760ad4d2017-01-20 22:42:09 +0000538 }
539 return Error::success();
540}
541
Zachary Turnerd3117392016-06-03 19:28:33 +0000542Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000543 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000544 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000545 if (!File.hasPDBInfoStream()) {
546 P.printString("PDB Stream not present");
547 return Error::success();
548 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000549 auto IS = File.getPDBInfoStream();
550 if (!IS)
551 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000552
553 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000554 P.printNumber("Version", IS->getVersion());
555 P.printHex("Signature", IS->getSignature());
556 P.printNumber("Age", IS->getAge());
557 P.printObject("Guid", IS->getGuid());
Zachary Turner05d5e612017-03-16 20:19:11 +0000558 P.printHex("Features", IS->getFeatures());
Zachary Turner760ad4d2017-01-20 22:42:09 +0000559 {
560 DictScope DD(P, "Named Streams");
561 for (const auto &S : IS->getNamedStreams().entries())
562 P.printObject(S.getKey(), S.getValue());
563 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000564 return Error::success();
565}
566
Zachary Turner29da5db2017-01-25 21:17:40 +0000567namespace {
568class RecordBytesVisitor : public TypeVisitorCallbacks {
569public:
570 explicit RecordBytesVisitor(ScopedPrinter &P) : P(P) {}
571
572 Error visitTypeEnd(CVType &Record) override {
573 P.printBinaryBlock("Bytes", Record.content());
574 return Error::success();
575 }
576
577private:
578 ScopedPrinter &P;
579};
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000580}
581
Zachary Turnerd3117392016-06-03 19:28:33 +0000582Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
583 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
584
585 bool DumpRecordBytes = false;
586 bool DumpRecords = false;
Zachary Turner29da5db2017-01-25 21:17:40 +0000587 bool DumpTpiHash = false;
Zachary Turnerd3117392016-06-03 19:28:33 +0000588 StringRef Label;
589 StringRef VerLabel;
590 if (StreamIdx == StreamTPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000591 if (!File.hasPDBTpiStream()) {
592 P.printString("Type Info Stream (TPI) not present");
593 return Error::success();
594 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000595 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
596 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turner29da5db2017-01-25 21:17:40 +0000597 DumpTpiHash = opts::raw::DumpTpiHash;
Zachary Turnerd3117392016-06-03 19:28:33 +0000598 Label = "Type Info Stream (TPI)";
599 VerLabel = "TPI Version";
600 } else if (StreamIdx == StreamIPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000601 if (!File.hasPDBIpiStream()) {
602 P.printString("Type Info Stream (IPI) not present");
603 return Error::success();
604 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000605 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
606 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000607 Label = "Type Info Stream (IPI)";
608 VerLabel = "IPI Version";
609 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000610
Zachary Turnera1657a92016-06-08 17:26:39 +0000611 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
612 : File.getPDBIpiStream();
613 if (!Tpi)
614 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000615
Zachary Turner0c60f262017-05-18 23:03:06 +0000616 auto ExpectedTypes = initializeTypeDatabase(StreamIdx);
617 if (!ExpectedTypes)
618 return ExpectedTypes.takeError();
619 auto &Types = *ExpectedTypes;
620
621 if (!DumpRecordBytes && !DumpRecords && !DumpTpiHash)
622 return Error::success();
623
Zachary Turner29da5db2017-01-25 21:17:40 +0000624 std::unique_ptr<DictScope> StreamScope;
625 std::unique_ptr<ListScope> RecordScope;
626
Zachary Turner20d773c2017-05-04 23:53:01 +0000627 StreamScope = llvm::make_unique<DictScope>(P, Label);
628 P.printNumber(VerLabel, Tpi->getTpiVersion());
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000629 P.printNumber("Record count", Tpi->getNumTypeRecords());
Zachary Turner20d773c2017-05-04 23:53:01 +0000630
Zachary Turner20d773c2017-05-04 23:53:01 +0000631 std::vector<std::unique_ptr<TypeVisitorCallbacks>> Visitors;
632
Zachary Turner20d773c2017-05-04 23:53:01 +0000633 // If we're in dump mode, add a dumper with the appropriate detail level.
634 if (DumpRecords) {
635 std::unique_ptr<TypeVisitorCallbacks> Dumper;
636 if (opts::raw::CompactRecords)
Zachary Turner0c60f262017-05-18 23:03:06 +0000637 Dumper = make_unique<CompactTypeDumpVisitor>(Types, &P);
Zachary Turner20d773c2017-05-04 23:53:01 +0000638 else {
Zachary Turner0c60f262017-05-18 23:03:06 +0000639 assert(TpiTypes);
Zachary Turner29da5db2017-01-25 21:17:40 +0000640
Zachary Turner0c60f262017-05-18 23:03:06 +0000641 auto X = make_unique<TypeDumpVisitor>(*TpiTypes, &P, false);
Zachary Turner20d773c2017-05-04 23:53:01 +0000642 if (StreamIdx == StreamIPI)
Zachary Turner0c60f262017-05-18 23:03:06 +0000643 X->setIpiTypes(*IpiTypes);
Zachary Turner20d773c2017-05-04 23:53:01 +0000644 Dumper = std::move(X);
645 }
646 Visitors.push_back(std::move(Dumper));
647 }
648 if (DumpRecordBytes)
649 Visitors.push_back(make_unique<RecordBytesVisitor>(P));
Zachary Turner29da5db2017-01-25 21:17:40 +0000650
651 // We always need to deserialize and add it to the type database. This is
652 // true if even if we're not dumping anything, because we could need the
653 // type database for the purposes of dumping symbols.
Zachary Turner44a643c2017-01-12 22:28:15 +0000654 TypeVisitorCallbackPipeline Pipeline;
Zachary Turner20d773c2017-05-04 23:53:01 +0000655 for (const auto &V : Visitors)
656 Pipeline.addCallbackToPipeline(*V);
Zachary Turner29da5db2017-01-25 21:17:40 +0000657
Zachary Turner29da5db2017-01-25 21:17:40 +0000658 if (DumpRecords || DumpRecordBytes)
659 RecordScope = llvm::make_unique<ListScope>(P, "Records");
660
Zachary Turner0c60f262017-05-18 23:03:06 +0000661 Optional<TypeIndex> I = Types.getFirst();
662 do {
Zachary Turner29da5db2017-01-25 21:17:40 +0000663 std::unique_ptr<DictScope> OneRecordScope;
664
665 if ((DumpRecords || DumpRecordBytes) && !opts::raw::CompactRecords)
666 OneRecordScope = llvm::make_unique<DictScope>(P, "");
667
Zachary Turner0c60f262017-05-18 23:03:06 +0000668 auto T = Types.getType(*I);
669 if (auto EC = codeview::visitTypeRecord(T, *I, Pipeline))
Zachary Turner29da5db2017-01-25 21:17:40 +0000670 return EC;
Zachary Turner0c60f262017-05-18 23:03:06 +0000671 } while (I = Types.getNext(*I));
Zachary Turner29da5db2017-01-25 21:17:40 +0000672
673 if (DumpTpiHash) {
674 DictScope DD(P, "Hash");
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000675 P.printNumber("Number of Hash Buckets", Tpi->getNumHashBuckets());
Zachary Turner29da5db2017-01-25 21:17:40 +0000676 P.printNumber("Hash Key Size", Tpi->getHashKeySize());
677 P.printList("Values", Tpi->getHashValues());
678
679 ListScope LHA(P, "Adjusters");
680 auto ExpectedST = File.getStringTable();
681 if (!ExpectedST)
682 return ExpectedST.takeError();
683 const auto &ST = *ExpectedST;
684 for (const auto &E : Tpi->getHashAdjusters()) {
685 DictScope DHA(P);
Zachary Turner2d5c2cd2017-05-03 17:11:11 +0000686 auto Name = ST.getStringForID(E.first);
687 if (!Name)
688 return Name.takeError();
689
690 P.printString("Type", *Name);
Zachary Turner29da5db2017-01-25 21:17:40 +0000691 P.printHex("TI", E.second);
692 }
693 }
694
Zachary Turner20d773c2017-05-04 23:53:01 +0000695 ListScope L(P, "TypeIndexOffsets");
696 for (const auto &IO : Tpi->getTypeIndexOffsets()) {
697 P.printString(formatv("Index: {0:x}, Offset: {1:N}", IO.Type.getIndex(),
698 (uint32_t)IO.Offset)
699 .str());
Zachary Turner29da5db2017-01-25 21:17:40 +0000700 }
701
Zachary Turnerd3117392016-06-03 19:28:33 +0000702 P.flush();
703 return Error::success();
704}
705
Zachary Turner0c60f262017-05-18 23:03:06 +0000706Expected<codeview::LazyRandomTypeCollection &>
707LLVMOutputStyle::initializeTypeDatabase(uint32_t SN) {
708 auto &TypeCollection = (SN == StreamTPI) ? TpiTypes : IpiTypes;
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000709 auto Tpi =
710 (SN == StreamTPI) ? File.getPDBTpiStream() : File.getPDBIpiStream();
Zachary Turnerbedc85f2017-05-04 23:53:54 +0000711 if (!Tpi)
712 return Tpi.takeError();
713
Zachary Turner0c60f262017-05-18 23:03:06 +0000714 if (!TypeCollection) {
715 // Initialize the type collection, even if we're not going to dump it. This
716 // way if some other part of the dumper decides it wants to use some or all
717 // of the records for whatever purposes, it can still access them lazily.
718 auto &Types = Tpi->typeArray();
719 uint32_t Count = Tpi->getNumTypeRecords();
720 auto Offsets = Tpi->getTypeIndexOffsets();
721 TypeCollection =
722 llvm::make_unique<LazyRandomTypeCollection>(Types, Count, Offsets);
723 }
Zachary Turner8c746732017-05-05 22:02:37 +0000724
Zachary Turner0c60f262017-05-18 23:03:06 +0000725 return *TypeCollection;
Zachary Turner20d773c2017-05-04 23:53:01 +0000726}
727
Zachary Turnerd3117392016-06-03 19:28:33 +0000728Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000729 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
730 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
731 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000732 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000733 if (!File.hasPDBDbiStream()) {
734 P.printString("DBI Stream not present");
735 return Error::success();
736 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000737
Zachary Turnera1657a92016-06-08 17:26:39 +0000738 auto DS = File.getPDBDbiStream();
739 if (!DS)
740 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000741
742 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000743 P.printNumber("Dbi Version", DS->getDbiVersion());
744 P.printNumber("Age", DS->getAge());
745 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
746 P.printBoolean("Has CTypes", DS->hasCTypes());
747 P.printBoolean("Is Stripped", DS->isStripped());
748 P.printObject("Machine Type", DS->getMachineType());
749 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
750 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
751 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000752
Zachary Turnera1657a92016-06-08 17:26:39 +0000753 uint16_t Major = DS->getBuildMajorVersion();
754 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000755 P.printVersion("Toolchain Version", Major, Minor);
756
757 std::string DllName;
758 raw_string_ostream DllStream(DllName);
759 DllStream << "mspdb" << Major << Minor << ".dll version";
760 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000761 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000762
763 if (DumpModules) {
764 ListScope L(P, "Modules");
Zachary Turner1eb9a022017-05-04 23:53:29 +0000765 const DbiModuleList &Modules = DS->modules();
766 for (uint32_t I = 0; I < Modules.getModuleCount(); ++I) {
767 const DbiModuleDescriptor &Modi = Modules.getModuleDescriptor(I);
Zachary Turnerd3117392016-06-03 19:28:33 +0000768 DictScope DD(P);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000769 P.printString("Name", Modi.getModuleName().str());
770 P.printNumber("Debug Stream Index", Modi.getModuleStreamIndex());
771 P.printString("Object File Name", Modi.getObjFileName().str());
772 P.printNumber("Num Files", Modi.getNumberOfFiles());
773 P.printNumber("Source File Name Idx", Modi.getSourceFileNameIndex());
774 P.printNumber("Pdb File Name Idx", Modi.getPdbFilePathNameIndex());
775 P.printNumber("Line Info Byte Size", Modi.getC11LineInfoByteSize());
776 P.printNumber("C13 Line Info Byte Size", Modi.getC13LineInfoByteSize());
777 P.printNumber("Symbol Byte Size", Modi.getSymbolDebugInfoByteSize());
778 P.printNumber("Type Server Index", Modi.getTypeServerIndex());
779 P.printBoolean("Has EC Info", Modi.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000780 if (opts::raw::DumpModuleFiles) {
Zachary Turner1eb9a022017-05-04 23:53:29 +0000781 std::string FileListName = to_string(Modules.getSourceFileCount(I)) +
782 " Contributing Source Files";
Zachary Turnerd3117392016-06-03 19:28:33 +0000783 ListScope LL(P, FileListName);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000784 for (auto File : Modules.source_files(I))
785 P.printString(File);
Zachary Turnerd3117392016-06-03 19:28:33 +0000786 }
Zachary Turner1eb9a022017-05-04 23:53:29 +0000787 bool HasModuleDI = (Modi.getModuleStreamIndex() < File.getNumStreams());
Zachary Turnerd3117392016-06-03 19:28:33 +0000788 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000789 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
790 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000791 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000792 File.getMsfLayout(), File.getMsfBuffer(),
Zachary Turner1eb9a022017-05-04 23:53:29 +0000793 Modi.getModuleStreamIndex());
Zachary Turnerd66889c2016-07-28 19:12:28 +0000794
Zachary Turner1eb9a022017-05-04 23:53:29 +0000795 ModuleDebugStreamRef ModS(Modi, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000796 if (auto EC = ModS.reload())
797 return EC;
798
799 if (ShouldDumpSymbols) {
Zachary Turner0c60f262017-05-18 23:03:06 +0000800 auto ExpectedTypes = initializeTypeDatabase(StreamTPI);
801 if (!ExpectedTypes)
802 return ExpectedTypes.takeError();
803 auto &Types = *ExpectedTypes;
Zachary Turner20d773c2017-05-04 23:53:01 +0000804
Zachary Turnerd3117392016-06-03 19:28:33 +0000805 ListScope SS(P, "Symbols");
Zachary Turner0c60f262017-05-18 23:03:06 +0000806 codeview::CVSymbolDumper SD(P, Types, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000807 bool HadError = false;
Zachary Turner0d840742016-10-07 21:34:46 +0000808 for (auto S : ModS.symbols(&HadError)) {
809 DictScope LL(P, "");
810 if (opts::raw::DumpModuleSyms) {
811 if (auto EC = SD.dump(S)) {
812 llvm::consumeError(std::move(EC));
813 HadError = true;
814 break;
815 }
816 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000817 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000818 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000819 }
820 if (HadError)
821 return make_error<RawError>(
822 raw_error_code::corrupt_file,
823 "DBI stream contained corrupt symbol record");
824 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000825 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000826 ListScope SS(P, "LineInfo");
Zachary Turner0c60f262017-05-18 23:03:06 +0000827 auto ExpectedTypes = initializeTypeDatabase(StreamIPI);
828 if (!ExpectedTypes)
829 return ExpectedTypes.takeError();
830 auto &IpiItems = *ExpectedTypes;
831 C13RawVisitor V(P, File, IpiItems);
Zachary Turner5b6e4e02017-04-29 01:13:21 +0000832 if (auto EC = codeview::visitModuleDebugFragments(
833 ModS.linesAndChecksums(), V))
834 return EC;
Zachary Turnerd3117392016-06-03 19:28:33 +0000835 }
836 }
837 }
838 }
839 return Error::success();
840}
841
842Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000843 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000844 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000845 if (!File.hasPDBDbiStream()) {
846 P.printString("DBI Stream not present");
847 return Error::success();
848 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000849
Zachary Turnera1657a92016-06-08 17:26:39 +0000850 auto Dbi = File.getPDBDbiStream();
851 if (!Dbi)
852 return Dbi.takeError();
853
Zachary Turnerd3117392016-06-03 19:28:33 +0000854 ListScope L(P, "Section Contributions");
855 class Visitor : public ISectionContribVisitor {
856 public:
857 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
858 void visit(const SectionContrib &SC) override {
859 DictScope D(P, "Contribution");
860 P.printNumber("ISect", SC.ISect);
861 P.printNumber("Off", SC.Off);
862 P.printNumber("Size", SC.Size);
863 P.printFlags("Characteristics", SC.Characteristics,
864 codeview::getImageSectionCharacteristicNames(),
865 COFF::SectionCharacteristics(0x00F00000));
866 {
867 DictScope DD(P, "Module");
868 P.printNumber("Index", SC.Imod);
Zachary Turner1eb9a022017-05-04 23:53:29 +0000869 const DbiModuleList &Modules = DS.modules();
870 if (Modules.getModuleCount() > SC.Imod) {
871 P.printString("Name",
872 Modules.getModuleDescriptor(SC.Imod).getModuleName());
Zachary Turnerd3117392016-06-03 19:28:33 +0000873 }
874 }
875 P.printNumber("Data CRC", SC.DataCrc);
876 P.printNumber("Reloc CRC", SC.RelocCrc);
877 P.flush();
878 }
879 void visit(const SectionContrib2 &SC) override {
880 visit(SC.Base);
881 P.printNumber("ISect Coff", SC.ISectCoff);
882 P.flush();
883 }
884
885 private:
886 ScopedPrinter &P;
887 DbiStream &DS;
888 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000889 Visitor V(P, *Dbi);
890 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000891 return Error::success();
892}
893
894Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000895 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000896 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000897 if (!File.hasPDBDbiStream()) {
898 P.printString("DBI Stream not present");
899 return Error::success();
900 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000901
Zachary Turnera1657a92016-06-08 17:26:39 +0000902 auto Dbi = File.getPDBDbiStream();
903 if (!Dbi)
904 return Dbi.takeError();
905
Zachary Turnerd3117392016-06-03 19:28:33 +0000906 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000907 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000908 DictScope D(P, "Entry");
909 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000910 P.printNumber("Ovl", M.Ovl);
911 P.printNumber("Group", M.Group);
912 P.printNumber("Frame", M.Frame);
913 P.printNumber("SecName", M.SecName);
914 P.printNumber("ClassName", M.ClassName);
915 P.printNumber("Offset", M.Offset);
916 P.printNumber("SecByteLength", M.SecByteLength);
917 P.flush();
918 }
919 return Error::success();
920}
921
922Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000923 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000924 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000925 if (!File.hasPDBPublicsStream()) {
926 P.printString("Publics Stream not present");
927 return Error::success();
928 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000929
Zachary Turnera1657a92016-06-08 17:26:39 +0000930 auto Publics = File.getPDBPublicsStream();
931 if (!Publics)
932 return Publics.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000933 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000934
935 auto Dbi = File.getPDBDbiStream();
936 if (!Dbi)
937 return Dbi.takeError();
938
939 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
940 P.printNumber("SymHash", Publics->getSymHash());
941 P.printNumber("AddrMap", Publics->getAddrMap());
942 P.printNumber("Number of buckets", Publics->getNumBuckets());
943 P.printList("Hash Buckets", Publics->getHashBuckets());
944 P.printList("Address Map", Publics->getAddressMap());
945 P.printList("Thunk Map", Publics->getThunkMap());
946 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000947 printSectionOffset);
948 ListScope L(P, "Symbols");
Zachary Turner0c60f262017-05-18 23:03:06 +0000949 auto ExpectedTypes = initializeTypeDatabase(StreamTPI);
950 if (!ExpectedTypes)
951 return ExpectedTypes.takeError();
952 auto &Tpi = *ExpectedTypes;
Zachary Turner20d773c2017-05-04 23:53:01 +0000953
Zachary Turner0c60f262017-05-18 23:03:06 +0000954 codeview::CVSymbolDumper SD(P, Tpi, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000955 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000956 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000957 DictScope DD(P, "");
958
Zachary Turner0d840742016-10-07 21:34:46 +0000959 if (auto EC = SD.dump(S)) {
960 HadError = true;
961 break;
962 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000963 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000964 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000965 }
966 if (HadError)
967 return make_error<RawError>(
968 raw_error_code::corrupt_file,
969 "Public symbol stream contained corrupt record");
970
971 return Error::success();
972}
973
974Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000975 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000976 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000977 if (!File.hasPDBDbiStream()) {
978 P.printString("DBI Stream not present");
979 return Error::success();
980 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000981
Zachary Turnera1657a92016-06-08 17:26:39 +0000982 auto Dbi = File.getPDBDbiStream();
983 if (!Dbi)
984 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000985
986 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000987 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000988 DictScope DD(P, "");
989
990 // If a name is 8 characters long, there is no NUL character at end.
991 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
992 P.printString("Name", Name);
993 P.printNumber("Virtual Size", Section.VirtualSize);
994 P.printNumber("Virtual Address", Section.VirtualAddress);
995 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
996 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
997 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
998 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
999 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
1000 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +00001001 P.printFlags("Characteristics", Section.Characteristics,
1002 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +00001003 }
1004 return Error::success();
1005}
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001006
1007Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001008 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001009 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +00001010 if (!File.hasPDBDbiStream()) {
1011 P.printString("DBI Stream not present");
1012 return Error::success();
1013 }
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001014
Zachary Turnera1657a92016-06-08 17:26:39 +00001015 auto Dbi = File.getPDBDbiStream();
1016 if (!Dbi)
1017 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001018
1019 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +00001020 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001021 DictScope DD(P, "");
1022 P.printNumber("Offset", Fpo.Offset);
1023 P.printNumber("Size", Fpo.Size);
1024 P.printNumber("Number of locals", Fpo.NumLocals);
1025 P.printNumber("Number of params", Fpo.NumParams);
1026 P.printNumber("Size of Prolog", Fpo.getPrologSize());
1027 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
1028 P.printBoolean("Has SEH", Fpo.hasSEH());
1029 P.printBoolean("Use BP", Fpo.useBP());
1030 P.printNumber("Frame Pointer", Fpo.getFP());
1031 }
1032 return Error::success();
1033}
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001034
Zachary Turner7120a472016-06-06 20:37:05 +00001035void LLVMOutputStyle::flush() { P.flush(); }