blob: 78a987244602327ae43ebdc97bda03a2fe9239cf [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 Turner44a643c2017-01-12 22:28:15 +000012#include "CompactTypeDumpVisitor.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000013#include "llvm-pdbdump.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000014
Zachary Turner629cb7d2017-01-11 23:24:22 +000015#include "llvm/DebugInfo/CodeView/CVTypeDumper.h"
16#include "llvm/DebugInfo/CodeView/CVTypeVisitor.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000017#include "llvm/DebugInfo/CodeView/EnumTables.h"
18#include "llvm/DebugInfo/CodeView/ModuleSubstreamVisitor.h"
19#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
Zachary Turner629cb7d2017-01-11 23:24:22 +000020#include "llvm/DebugInfo/CodeView/TypeDatabaseVisitor.h"
21#include "llvm/DebugInfo/CodeView/TypeDeserializer.h"
22#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
23#include "llvm/DebugInfo/CodeView/TypeVisitorCallbackPipeline.h"
Zachary Turnera3225b02016-07-29 20:56:36 +000024#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
25#include "llvm/DebugInfo/MSF/StreamReader.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000026#include "llvm/DebugInfo/PDB/PDBExtras.h"
27#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
28#include "llvm/DebugInfo/PDB/Raw/EnumTables.h"
Bob Haarman653baa22016-10-21 19:43:19 +000029#include "llvm/DebugInfo/PDB/Raw/GlobalsStream.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000030#include "llvm/DebugInfo/PDB/Raw/ISectionContribVisitor.h"
31#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
32#include "llvm/DebugInfo/PDB/Raw/ModInfo.h"
33#include "llvm/DebugInfo/PDB/Raw/ModStream.h"
34#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
35#include "llvm/DebugInfo/PDB/Raw/PublicsStream.h"
36#include "llvm/DebugInfo/PDB/Raw/RawError.h"
37#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
38#include "llvm/Object/COFF.h"
Zachary Turner44a643c2017-01-12 22:28:15 +000039#include "llvm/Support/FormatVariadic.h"
Zachary Turnerd3117392016-06-03 19:28:33 +000040
41#include <unordered_map>
42
43using namespace llvm;
44using namespace llvm::codeview;
Zachary Turnerbac69d32016-07-22 19:56:05 +000045using namespace llvm::msf;
Zachary Turnerd3117392016-06-03 19:28:33 +000046using namespace llvm::pdb;
47
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +000048namespace {
49struct PageStats {
50 explicit PageStats(const BitVector &FreePages)
51 : Upm(FreePages), ActualUsedPages(FreePages.size()),
52 MultiUsePages(FreePages.size()), UseAfterFreePages(FreePages.size()) {
53 const_cast<BitVector &>(Upm).flip();
54 // To calculate orphaned pages, we start with the set of pages that the
55 // MSF thinks are used. Each time we find one that actually *is* used,
56 // we unset it. Whichever bits remain set at the end are orphaned.
57 OrphanedPages = Upm;
58 }
59
60 // The inverse of the MSF File's copy of the Fpm. The basis for which we
61 // determine the allocation status of each page.
62 const BitVector Upm;
63
64 // Pages which are marked as used in the FPM and are used at least once.
65 BitVector ActualUsedPages;
66
67 // Pages which are marked as used in the FPM but are used more than once.
68 BitVector MultiUsePages;
69
70 // Pages which are marked as used in the FPM but are not used at all.
71 BitVector OrphanedPages;
72
73 // Pages which are marked free in the FPM but are used.
74 BitVector UseAfterFreePages;
75};
76}
77
78static void recordKnownUsedPage(PageStats &Stats, uint32_t UsedIndex) {
79 if (Stats.Upm.test(UsedIndex)) {
80 if (Stats.ActualUsedPages.test(UsedIndex))
81 Stats.MultiUsePages.set(UsedIndex);
82 Stats.ActualUsedPages.set(UsedIndex);
83 Stats.OrphanedPages.reset(UsedIndex);
84 } else {
85 // The MSF doesn't think this page is used, but it is.
86 Stats.UseAfterFreePages.set(UsedIndex);
87 }
88}
89
Zachary Turnerd3117392016-06-03 19:28:33 +000090static void printSectionOffset(llvm::raw_ostream &OS,
91 const SectionOffset &Off) {
92 OS << Off.Off << ", " << Off.Isect;
93}
94
Zachary Turner629cb7d2017-01-11 23:24:22 +000095LLVMOutputStyle::LLVMOutputStyle(PDBFile &File) : File(File), P(outs()) {}
Zachary Turnerd3117392016-06-03 19:28:33 +000096
Zachary Turnera30bd1a2016-06-30 17:42:48 +000097Error LLVMOutputStyle::dump() {
98 if (auto EC = dumpFileHeaders())
99 return EC;
100
101 if (auto EC = dumpStreamSummary())
102 return EC;
103
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000104 if (auto EC = dumpFreePageMap())
105 return EC;
106
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000107 if (auto EC = dumpStreamBlocks())
108 return EC;
109
Zachary Turner72c5b642016-09-09 18:17:52 +0000110 if (auto EC = dumpBlockRanges())
111 return EC;
112
113 if (auto EC = dumpStreamBytes())
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000114 return EC;
115
Zachary Turner760ad4d2017-01-20 22:42:09 +0000116 if (auto EC = dumpStringTable())
117 return EC;
118
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000119 if (auto EC = dumpInfoStream())
120 return EC;
121
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000122 if (auto EC = dumpTpiStream(StreamTPI))
123 return EC;
124
125 if (auto EC = dumpTpiStream(StreamIPI))
126 return EC;
127
128 if (auto EC = dumpDbiStream())
129 return EC;
130
131 if (auto EC = dumpSectionContribs())
132 return EC;
133
134 if (auto EC = dumpSectionMap())
135 return EC;
136
Bob Haarman653baa22016-10-21 19:43:19 +0000137 if (auto EC = dumpGlobalsStream())
138 return EC;
139
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000140 if (auto EC = dumpPublicsStream())
141 return EC;
142
143 if (auto EC = dumpSectionHeaders())
144 return EC;
145
146 if (auto EC = dumpFpoStream())
147 return EC;
148
149 flush();
150
151 return Error::success();
152}
153
Zachary Turnerd3117392016-06-03 19:28:33 +0000154Error LLVMOutputStyle::dumpFileHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000155 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000156 return Error::success();
157
158 DictScope D(P, "FileHeaders");
159 P.printNumber("BlockSize", File.getBlockSize());
Zachary Turnerb927e022016-07-15 22:17:19 +0000160 P.printNumber("FreeBlockMap", File.getFreeBlockMapBlock());
Zachary Turnerd3117392016-06-03 19:28:33 +0000161 P.printNumber("NumBlocks", File.getBlockCount());
162 P.printNumber("NumDirectoryBytes", File.getNumDirectoryBytes());
163 P.printNumber("Unknown1", File.getUnknown1());
164 P.printNumber("BlockMapAddr", File.getBlockMapIndex());
165 P.printNumber("NumDirectoryBlocks", File.getNumDirectoryBlocks());
Zachary Turnerd3117392016-06-03 19:28:33 +0000166
167 // The directory is not contiguous. Instead, the block map contains a
168 // contiguous list of block numbers whose contents, when concatenated in
169 // order, make up the directory.
170 P.printList("DirectoryBlocks", File.getDirectoryBlockArray());
171 P.printNumber("NumStreams", File.getNumStreams());
172 return Error::success();
173}
174
Zachary Turner36efbfa2016-09-09 19:00:49 +0000175void LLVMOutputStyle::discoverStreamPurposes() {
176 if (!StreamPurposes.empty())
177 return;
Zachary Turnerd3117392016-06-03 19:28:33 +0000178
Reid Kleckner11582c52016-06-17 20:38:01 +0000179 // It's OK if we fail to load some of these streams, we still attempt to print
180 // what we can.
Zachary Turnera1657a92016-06-08 17:26:39 +0000181 auto Dbi = File.getPDBDbiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000182 auto Tpi = File.getPDBTpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000183 auto Ipi = File.getPDBIpiStream();
Zachary Turnera1657a92016-06-08 17:26:39 +0000184 auto Info = File.getPDBInfoStream();
Zachary Turnerd3117392016-06-03 19:28:33 +0000185
Zachary Turnerd3117392016-06-03 19:28:33 +0000186 uint32_t StreamCount = File.getNumStreams();
187 std::unordered_map<uint16_t, const ModuleInfoEx *> ModStreams;
188 std::unordered_map<uint16_t, std::string> NamedStreams;
189
Reid Kleckner11582c52016-06-17 20:38:01 +0000190 if (Dbi) {
191 for (auto &ModI : Dbi->modules()) {
192 uint16_t SN = ModI.Info.getModuleStreamIndex();
193 ModStreams[SN] = &ModI;
194 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000195 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000196 if (Info) {
197 for (auto &NSE : Info->named_streams()) {
198 NamedStreams[NSE.second] = NSE.first();
199 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000200 }
201
Zachary Turner36efbfa2016-09-09 19:00:49 +0000202 StreamPurposes.resize(StreamCount);
Zachary Turnerd3117392016-06-03 19:28:33 +0000203 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000204 std::string Value;
205 if (StreamIdx == OldMSFDirectory)
206 Value = "Old MSF Directory";
207 else if (StreamIdx == StreamPDB)
208 Value = "PDB Stream";
209 else if (StreamIdx == StreamDBI)
210 Value = "DBI Stream";
211 else if (StreamIdx == StreamTPI)
212 Value = "TPI Stream";
213 else if (StreamIdx == StreamIPI)
214 Value = "IPI Stream";
Reid Kleckner11582c52016-06-17 20:38:01 +0000215 else if (Dbi && StreamIdx == Dbi->getGlobalSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000216 Value = "Global Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000217 else if (Dbi && StreamIdx == Dbi->getPublicSymbolStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000218 Value = "Public Symbol Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000219 else if (Dbi && StreamIdx == Dbi->getSymRecordStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000220 Value = "Public Symbol Records";
Reid Kleckner11582c52016-06-17 20:38:01 +0000221 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000222 Value = "TPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000223 else if (Tpi && StreamIdx == Tpi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000224 Value = "TPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000225 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000226 Value = "IPI Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000227 else if (Ipi && StreamIdx == Ipi->getTypeHashStreamAuxIndex())
Zachary Turnerd3117392016-06-03 19:28:33 +0000228 Value = "IPI Aux Hash";
Reid Kleckner11582c52016-06-17 20:38:01 +0000229 else if (Dbi &&
230 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Exception))
Zachary Turnerd3117392016-06-03 19:28:33 +0000231 Value = "Exception Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000232 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Fixup))
Zachary Turnerd3117392016-06-03 19:28:33 +0000233 Value = "Fixup Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000234 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::FPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000235 Value = "FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000236 else if (Dbi &&
237 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::NewFPO))
Zachary Turnerd3117392016-06-03 19:28:33 +0000238 Value = "New FPO Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000239 else if (Dbi &&
240 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapFromSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000241 Value = "Omap From Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000242 else if (Dbi &&
243 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::OmapToSrc))
Zachary Turnerd3117392016-06-03 19:28:33 +0000244 Value = "Omap To Source Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000245 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Pdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000246 Value = "Pdata";
Reid Kleckner11582c52016-06-17 20:38:01 +0000247 else if (Dbi &&
248 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdr))
Zachary Turnerd3117392016-06-03 19:28:33 +0000249 Value = "Section Header Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000250 else if (Dbi &&
251 StreamIdx ==
252 Dbi->getDebugStreamIndex(DbgHeaderType::SectionHdrOrig))
Zachary Turnerd3117392016-06-03 19:28:33 +0000253 Value = "Section Header Original Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000254 else if (Dbi &&
255 StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::TokenRidMap))
Zachary Turnerd3117392016-06-03 19:28:33 +0000256 Value = "Token Rid Data";
Reid Kleckner11582c52016-06-17 20:38:01 +0000257 else if (Dbi && StreamIdx == Dbi->getDebugStreamIndex(DbgHeaderType::Xdata))
Zachary Turnerd3117392016-06-03 19:28:33 +0000258 Value = "Xdata";
259 else {
260 auto ModIter = ModStreams.find(StreamIdx);
261 auto NSIter = NamedStreams.find(StreamIdx);
262 if (ModIter != ModStreams.end()) {
263 Value = "Module \"";
264 Value += ModIter->second->Info.getModuleName().str();
265 Value += "\"";
266 } else if (NSIter != NamedStreams.end()) {
267 Value = "Named Stream \"";
268 Value += NSIter->second;
269 Value += "\"";
270 } else {
271 Value = "???";
272 }
273 }
Zachary Turner36efbfa2016-09-09 19:00:49 +0000274 StreamPurposes[StreamIdx] = Value;
Zachary Turnerd3117392016-06-03 19:28:33 +0000275 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000276
277 // Consume errors from missing streams.
278 if (!Dbi)
279 consumeError(Dbi.takeError());
280 if (!Tpi)
281 consumeError(Tpi.takeError());
282 if (!Ipi)
283 consumeError(Ipi.takeError());
284 if (!Info)
285 consumeError(Info.takeError());
Zachary Turner36efbfa2016-09-09 19:00:49 +0000286}
287
288Error LLVMOutputStyle::dumpStreamSummary() {
289 if (!opts::raw::DumpStreamSummary)
290 return Error::success();
291
292 discoverStreamPurposes();
293
294 uint32_t StreamCount = File.getNumStreams();
295
296 ListScope L(P, "Streams");
297 for (uint16_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
298 std::string Label("Stream ");
299 Label += to_string(StreamIdx);
300
301 std::string Value = "[" + StreamPurposes[StreamIdx] + "] (";
302 Value += to_string(File.getStreamByteSize(StreamIdx));
303 Value += " bytes)";
304
305 P.printString(Label, Value);
306 }
Reid Kleckner11582c52016-06-17 20:38:01 +0000307
Zachary Turnerd3117392016-06-03 19:28:33 +0000308 P.flush();
309 return Error::success();
310}
311
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000312Error LLVMOutputStyle::dumpFreePageMap() {
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000313 if (!opts::raw::DumpPageStats)
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000314 return Error::success();
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000315
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000316 // Start with used pages instead of free pages because
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000317 // the number of free pages is far larger than used pages.
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000318 BitVector FPM = File.getMsfLayout().FreePageMap;
319
320 PageStats PS(FPM);
321
322 recordKnownUsedPage(PS, 0); // MSF Super Block
323
Zachary Turner8cf51c32016-08-03 16:53:21 +0000324 uint32_t BlocksPerSection = msf::getFpmIntervalLength(File.getMsfLayout());
325 uint32_t NumSections = msf::getNumFpmIntervals(File.getMsfLayout());
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000326 for (uint32_t I = 0; I < NumSections; ++I) {
327 uint32_t Fpm0 = 1 + BlocksPerSection * I;
328 // 2 Fpm blocks spaced at `getBlockSize()` block intervals
329 recordKnownUsedPage(PS, Fpm0);
330 recordKnownUsedPage(PS, Fpm0 + 1);
331 }
332
333 recordKnownUsedPage(PS, File.getBlockMapIndex()); // Stream Table
334
Rui Ueyama22e67382016-08-02 23:22:46 +0000335 for (auto DB : File.getDirectoryBlockArray())
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000336 recordKnownUsedPage(PS, DB);
Rui Ueyama22e67382016-08-02 23:22:46 +0000337
338 // Record pages used by streams. Note that pages for stream 0
339 // are considered being unused because that's what MSVC tools do.
340 // Stream 0 doesn't contain actual data, so it makes some sense,
341 // though it's a bit confusing to us.
342 for (auto &SE : File.getStreamMap().drop_front(1))
343 for (auto &S : SE)
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000344 recordKnownUsedPage(PS, S);
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000345
346 dumpBitVector("Msf Free Pages", FPM);
347 dumpBitVector("Orphaned Pages", PS.OrphanedPages);
348 dumpBitVector("Multiply Used Pages", PS.MultiUsePages);
349 dumpBitVector("Use After Free Pages", PS.UseAfterFreePages);
Rui Ueyama7a5cdc62016-07-29 21:38:00 +0000350 return Error::success();
351}
352
Zachary Turnerd3c7b8e2016-08-01 21:19:45 +0000353void LLVMOutputStyle::dumpBitVector(StringRef Name, const BitVector &V) {
354 std::vector<uint32_t> Vec;
355 for (uint32_t I = 0, E = V.size(); I != E; ++I)
356 if (V[I])
357 Vec.push_back(I);
358 P.printList(Name, Vec);
359}
360
Bob Haarman653baa22016-10-21 19:43:19 +0000361Error LLVMOutputStyle::dumpGlobalsStream() {
362 if (!opts::raw::DumpGlobals)
363 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000364 if (!File.hasPDBGlobalsStream()) {
365 P.printString("Globals Stream not present");
366 return Error::success();
367 }
Bob Haarman653baa22016-10-21 19:43:19 +0000368
Bob Haarman653baa22016-10-21 19:43:19 +0000369 auto Globals = File.getPDBGlobalsStream();
370 if (!Globals)
Bob Haarman312fd0e2016-12-06 00:55:55 +0000371 return Globals.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000372 DictScope D(P, "Globals Stream");
Bob Haarman653baa22016-10-21 19:43:19 +0000373
374 auto Dbi = File.getPDBDbiStream();
375 if (!Dbi)
376 return Dbi.takeError();
377
378 P.printNumber("Stream number", Dbi->getGlobalSymbolStreamIndex());
379 P.printNumber("Number of buckets", Globals->getNumBuckets());
380 P.printList("Hash Buckets", Globals->getHashBuckets());
381
382 return Error::success();
383}
384
Zachary Turnerd3117392016-06-03 19:28:33 +0000385Error LLVMOutputStyle::dumpStreamBlocks() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000386 if (!opts::raw::DumpStreamBlocks)
Zachary Turnerd3117392016-06-03 19:28:33 +0000387 return Error::success();
388
389 ListScope L(P, "StreamBlocks");
390 uint32_t StreamCount = File.getNumStreams();
391 for (uint32_t StreamIdx = 0; StreamIdx < StreamCount; ++StreamIdx) {
392 std::string Name("Stream ");
393 Name += to_string(StreamIdx);
394 auto StreamBlocks = File.getStreamBlockList(StreamIdx);
395 P.printList(Name, StreamBlocks);
396 }
397 return Error::success();
398}
399
Zachary Turner72c5b642016-09-09 18:17:52 +0000400Error LLVMOutputStyle::dumpBlockRanges() {
401 if (!opts::raw::DumpBlockRange.hasValue())
402 return Error::success();
403 auto &R = *opts::raw::DumpBlockRange;
404 uint32_t Max = R.Max.getValueOr(R.Min);
405
406 if (Max < R.Min)
407 return make_error<StringError>(
408 "Invalid block range specified. Max < Min",
409 std::make_error_code(std::errc::bad_address));
410 if (Max >= File.getBlockCount())
411 return make_error<StringError>(
412 "Invalid block range specified. Requested block out of bounds",
413 std::make_error_code(std::errc::bad_address));
414
415 DictScope D(P, "Block Data");
416 for (uint32_t I = R.Min; I <= Max; ++I) {
417 auto ExpectedData = File.getBlockData(I, File.getBlockSize());
418 if (!ExpectedData)
419 return ExpectedData.takeError();
420 std::string Label;
421 llvm::raw_string_ostream S(Label);
422 S << "Block " << I;
423 S.flush();
424 P.printBinaryBlock(Label, *ExpectedData);
425 }
426
427 return Error::success();
428}
429
430Error LLVMOutputStyle::dumpStreamBytes() {
431 if (opts::raw::DumpStreamData.empty())
Zachary Turnerd3117392016-06-03 19:28:33 +0000432 return Error::success();
433
Zachary Turner36efbfa2016-09-09 19:00:49 +0000434 discoverStreamPurposes();
435
Zachary Turner72c5b642016-09-09 18:17:52 +0000436 DictScope D(P, "Stream Data");
437 for (uint32_t SI : opts::raw::DumpStreamData) {
438 if (SI >= File.getNumStreams())
439 return make_error<RawError>(raw_error_code::no_stream);
Zachary Turnerd2b2bfe2016-06-08 00:25:08 +0000440
Zachary Turner72c5b642016-09-09 18:17:52 +0000441 auto S = MappedBlockStream::createIndexedStream(File.getMsfLayout(),
442 File.getMsfBuffer(), SI);
443 if (!S)
444 continue;
Zachary Turner36efbfa2016-09-09 19:00:49 +0000445 DictScope DD(P, "Stream");
446
447 P.printNumber("Index", SI);
448 P.printString("Type", StreamPurposes[SI]);
449 P.printNumber("Size", S->getLength());
450 auto Blocks = File.getMsfLayout().StreamMap[SI];
451 P.printList("Blocks", Blocks);
452
Zachary Turner72c5b642016-09-09 18:17:52 +0000453 StreamReader R(*S);
454 ArrayRef<uint8_t> StreamData;
455 if (auto EC = R.readBytes(StreamData, S->getLength()))
Zachary Turnerd3117392016-06-03 19:28:33 +0000456 return EC;
Zachary Turner36efbfa2016-09-09 19:00:49 +0000457 P.printBinaryBlock("Data", StreamData);
Zachary Turnerd3117392016-06-03 19:28:33 +0000458 }
459 return Error::success();
460}
461
Zachary Turner760ad4d2017-01-20 22:42:09 +0000462Error LLVMOutputStyle::dumpStringTable() {
463 if (!opts::raw::DumpStringTable)
464 return Error::success();
465
466 auto IS = File.getStringTable();
467 if (!IS)
468 return IS.takeError();
469
470 DictScope D(P, "String Table");
471 for (uint32_t I : IS->name_ids()) {
472 StringRef S = IS->getStringForID(I);
473 if (!S.empty()) {
474 llvm::SmallString<32> Str;
475 Str.append("'");
476 Str.append(S);
477 Str.append("'");
478 P.printString(Str);
479 }
480 }
481 return Error::success();
482}
483
Zachary Turnerd3117392016-06-03 19:28:33 +0000484Error LLVMOutputStyle::dumpInfoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000485 if (!opts::raw::DumpHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000486 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000487 if (!File.hasPDBInfoStream()) {
488 P.printString("PDB Stream not present");
489 return Error::success();
490 }
Zachary Turnera1657a92016-06-08 17:26:39 +0000491 auto IS = File.getPDBInfoStream();
492 if (!IS)
493 return IS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000494
495 DictScope D(P, "PDB Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000496 P.printNumber("Version", IS->getVersion());
497 P.printHex("Signature", IS->getSignature());
498 P.printNumber("Age", IS->getAge());
499 P.printObject("Guid", IS->getGuid());
Zachary Turner760ad4d2017-01-20 22:42:09 +0000500 {
501 DictScope DD(P, "Named Streams");
502 for (const auto &S : IS->getNamedStreams().entries())
503 P.printObject(S.getKey(), S.getValue());
504 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000505 return Error::success();
506}
507
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000508static void printTypeIndexOffset(raw_ostream &OS,
509 const TypeIndexOffset &TIOff) {
510 OS << "{" << TIOff.Type.getIndex() << ", " << TIOff.Offset << "}";
511}
512
Zachary Turner29da5db2017-01-25 21:17:40 +0000513namespace {
514class RecordBytesVisitor : public TypeVisitorCallbacks {
515public:
516 explicit RecordBytesVisitor(ScopedPrinter &P) : P(P) {}
517
518 Error visitTypeEnd(CVType &Record) override {
519 P.printBinaryBlock("Bytes", Record.content());
520 return Error::success();
521 }
522
523private:
524 ScopedPrinter &P;
525};
Rui Ueyamafd97bf12016-06-03 20:48:51 +0000526}
527
Zachary Turnerd3117392016-06-03 19:28:33 +0000528Error LLVMOutputStyle::dumpTpiStream(uint32_t StreamIdx) {
529 assert(StreamIdx == StreamTPI || StreamIdx == StreamIPI);
530
531 bool DumpRecordBytes = false;
532 bool DumpRecords = false;
Zachary Turner29da5db2017-01-25 21:17:40 +0000533 bool DumpTpiHash = false;
Zachary Turnerd3117392016-06-03 19:28:33 +0000534 StringRef Label;
535 StringRef VerLabel;
536 if (StreamIdx == StreamTPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000537 if (!File.hasPDBTpiStream()) {
538 P.printString("Type Info Stream (TPI) not present");
539 return Error::success();
540 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000541 DumpRecordBytes = opts::raw::DumpTpiRecordBytes;
542 DumpRecords = opts::raw::DumpTpiRecords;
Zachary Turner29da5db2017-01-25 21:17:40 +0000543 DumpTpiHash = opts::raw::DumpTpiHash;
Zachary Turnerd3117392016-06-03 19:28:33 +0000544 Label = "Type Info Stream (TPI)";
545 VerLabel = "TPI Version";
546 } else if (StreamIdx == StreamIPI) {
Bob Haarmana5b43582016-12-05 22:44:00 +0000547 if (!File.hasPDBIpiStream()) {
548 P.printString("Type Info Stream (IPI) not present");
549 return Error::success();
550 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000551 DumpRecordBytes = opts::raw::DumpIpiRecordBytes;
552 DumpRecords = opts::raw::DumpIpiRecords;
Zachary Turnerd3117392016-06-03 19:28:33 +0000553 Label = "Type Info Stream (IPI)";
554 VerLabel = "IPI Version";
555 }
Zachary Turner29da5db2017-01-25 21:17:40 +0000556 if (!DumpRecordBytes && !DumpRecords && !DumpTpiHash &&
557 !opts::raw::DumpModuleSyms)
Zachary Turnerd3117392016-06-03 19:28:33 +0000558 return Error::success();
559
Zachary Turner29da5db2017-01-25 21:17:40 +0000560 bool IsSilentDatabaseBuild = !DumpRecordBytes && !DumpRecords && !DumpTpiHash;
561
Zachary Turnera1657a92016-06-08 17:26:39 +0000562 auto Tpi = (StreamIdx == StreamTPI) ? File.getPDBTpiStream()
563 : File.getPDBIpiStream();
564 if (!Tpi)
565 return Tpi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000566
Zachary Turner29da5db2017-01-25 21:17:40 +0000567 std::unique_ptr<DictScope> StreamScope;
568 std::unique_ptr<ListScope> RecordScope;
569
570 if (!IsSilentDatabaseBuild) {
571 StreamScope = llvm::make_unique<DictScope>(P, Label);
572 P.printNumber(VerLabel, Tpi->getTpiVersion());
573 P.printNumber("Record count", Tpi->NumTypeRecords());
574 }
575
Zachary Turner44a643c2017-01-12 22:28:15 +0000576 TypeDatabaseVisitor DBV(TypeDB);
577 CompactTypeDumpVisitor CTDV(TypeDB, &P);
578 TypeDumpVisitor TDV(TypeDB, &P, false);
Zachary Turner29da5db2017-01-25 21:17:40 +0000579 RecordBytesVisitor RBV(P);
Zachary Turner44a643c2017-01-12 22:28:15 +0000580 TypeDeserializer Deserializer;
Zachary Turner29da5db2017-01-25 21:17:40 +0000581
582 // We always need to deserialize and add it to the type database. This is
583 // true if even if we're not dumping anything, because we could need the
584 // type database for the purposes of dumping symbols.
Zachary Turner44a643c2017-01-12 22:28:15 +0000585 TypeVisitorCallbackPipeline Pipeline;
586 Pipeline.addCallbackToPipeline(Deserializer);
587 Pipeline.addCallbackToPipeline(DBV);
588
Zachary Turner29da5db2017-01-25 21:17:40 +0000589 // If we're in dump mode, add a dumper with the appropriate detail level.
590 if (DumpRecords) {
Zachary Turner44a643c2017-01-12 22:28:15 +0000591 if (opts::raw::CompactRecords)
592 Pipeline.addCallbackToPipeline(CTDV);
593 else
594 Pipeline.addCallbackToPipeline(TDV);
Zachary Turnerd3117392016-06-03 19:28:33 +0000595 }
Zachary Turner29da5db2017-01-25 21:17:40 +0000596 if (DumpRecordBytes)
597 Pipeline.addCallbackToPipeline(RBV);
598
599 CVTypeVisitor Visitor(Pipeline);
600
601 if (DumpRecords || DumpRecordBytes)
602 RecordScope = llvm::make_unique<ListScope>(P, "Records");
603
604 bool HadError = false;
605
606 TypeIndex T(TypeIndex::FirstNonSimpleIndex);
607 for (auto Type : Tpi->types(&HadError)) {
608 std::unique_ptr<DictScope> OneRecordScope;
609
610 if ((DumpRecords || DumpRecordBytes) && !opts::raw::CompactRecords)
611 OneRecordScope = llvm::make_unique<DictScope>(P, "");
612
613 if (auto EC = Visitor.visitTypeRecord(Type))
614 return EC;
615 }
616 if (HadError)
617 return make_error<RawError>(raw_error_code::corrupt_file,
618 "TPI stream contained corrupt record");
619
620 if (DumpTpiHash) {
621 DictScope DD(P, "Hash");
622 P.printNumber("Number of Hash Buckets", Tpi->NumHashBuckets());
623 P.printNumber("Hash Key Size", Tpi->getHashKeySize());
624 P.printList("Values", Tpi->getHashValues());
625
626 ListScope LHA(P, "Adjusters");
627 auto ExpectedST = File.getStringTable();
628 if (!ExpectedST)
629 return ExpectedST.takeError();
630 const auto &ST = *ExpectedST;
631 for (const auto &E : Tpi->getHashAdjusters()) {
632 DictScope DHA(P);
633 StringRef Name = ST.getStringForID(E.first);
634 P.printString("Type", Name);
635 P.printHex("TI", E.second);
636 }
637 }
638
639 if (!IsSilentDatabaseBuild) {
640 ListScope L(P, "TypeIndexOffsets");
641 for (const auto &IO : Tpi->getTypeIndexOffsets()) {
642 P.printString(formatv("Index: {0:x}, Offset: {1:N}", IO.Type.getIndex(),
643 (uint32_t)IO.Offset)
644 .str());
645 }
646 }
647
Zachary Turnerd3117392016-06-03 19:28:33 +0000648 P.flush();
649 return Error::success();
650}
651
652Error LLVMOutputStyle::dumpDbiStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000653 bool DumpModules = opts::raw::DumpModules || opts::raw::DumpModuleSyms ||
654 opts::raw::DumpModuleFiles || opts::raw::DumpLineInfo;
655 if (!opts::raw::DumpHeaders && !DumpModules)
Zachary Turnerd3117392016-06-03 19:28:33 +0000656 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000657 if (!File.hasPDBDbiStream()) {
658 P.printString("DBI Stream not present");
659 return Error::success();
660 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000661
Zachary Turnera1657a92016-06-08 17:26:39 +0000662 auto DS = File.getPDBDbiStream();
663 if (!DS)
664 return DS.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000665
666 DictScope D(P, "DBI Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000667 P.printNumber("Dbi Version", DS->getDbiVersion());
668 P.printNumber("Age", DS->getAge());
669 P.printBoolean("Incremental Linking", DS->isIncrementallyLinked());
670 P.printBoolean("Has CTypes", DS->hasCTypes());
671 P.printBoolean("Is Stripped", DS->isStripped());
672 P.printObject("Machine Type", DS->getMachineType());
673 P.printNumber("Symbol Record Stream Index", DS->getSymRecordStreamIndex());
674 P.printNumber("Public Symbol Stream Index", DS->getPublicSymbolStreamIndex());
675 P.printNumber("Global Symbol Stream Index", DS->getGlobalSymbolStreamIndex());
Zachary Turnerd3117392016-06-03 19:28:33 +0000676
Zachary Turnera1657a92016-06-08 17:26:39 +0000677 uint16_t Major = DS->getBuildMajorVersion();
678 uint16_t Minor = DS->getBuildMinorVersion();
Zachary Turnerd3117392016-06-03 19:28:33 +0000679 P.printVersion("Toolchain Version", Major, Minor);
680
681 std::string DllName;
682 raw_string_ostream DllStream(DllName);
683 DllStream << "mspdb" << Major << Minor << ".dll version";
684 DllStream.flush();
Zachary Turnera1657a92016-06-08 17:26:39 +0000685 P.printVersion(DllName, Major, Minor, DS->getPdbDllVersion());
Zachary Turnerd3117392016-06-03 19:28:33 +0000686
687 if (DumpModules) {
688 ListScope L(P, "Modules");
Zachary Turnera1657a92016-06-08 17:26:39 +0000689 for (auto &Modi : DS->modules()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000690 DictScope DD(P);
691 P.printString("Name", Modi.Info.getModuleName().str());
692 P.printNumber("Debug Stream Index", Modi.Info.getModuleStreamIndex());
693 P.printString("Object File Name", Modi.Info.getObjFileName().str());
694 P.printNumber("Num Files", Modi.Info.getNumberOfFiles());
695 P.printNumber("Source File Name Idx", Modi.Info.getSourceFileNameIndex());
696 P.printNumber("Pdb File Name Idx", Modi.Info.getPdbFilePathNameIndex());
697 P.printNumber("Line Info Byte Size", Modi.Info.getLineInfoByteSize());
698 P.printNumber("C13 Line Info Byte Size",
699 Modi.Info.getC13LineInfoByteSize());
700 P.printNumber("Symbol Byte Size", Modi.Info.getSymbolDebugInfoByteSize());
701 P.printNumber("Type Server Index", Modi.Info.getTypeServerIndex());
702 P.printBoolean("Has EC Info", Modi.Info.hasECInfo());
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000703 if (opts::raw::DumpModuleFiles) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000704 std::string FileListName =
705 to_string(Modi.SourceFiles.size()) + " Contributing Source Files";
706 ListScope LL(P, FileListName);
707 for (auto File : Modi.SourceFiles)
708 P.printString(File.str());
709 }
710 bool HasModuleDI =
711 (Modi.Info.getModuleStreamIndex() < File.getNumStreams());
712 bool ShouldDumpSymbols =
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000713 (opts::raw::DumpModuleSyms || opts::raw::DumpSymRecordBytes);
714 if (HasModuleDI && (ShouldDumpSymbols || opts::raw::DumpLineInfo)) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000715 auto ModStreamData = MappedBlockStream::createIndexedStream(
Zachary Turnerd66889c2016-07-28 19:12:28 +0000716 File.getMsfLayout(), File.getMsfBuffer(),
717 Modi.Info.getModuleStreamIndex());
718
719 ModStream ModS(Modi.Info, std::move(ModStreamData));
Zachary Turnerd3117392016-06-03 19:28:33 +0000720 if (auto EC = ModS.reload())
721 return EC;
722
723 if (ShouldDumpSymbols) {
724 ListScope SS(P, "Symbols");
Zachary Turner629cb7d2017-01-11 23:24:22 +0000725 codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000726 bool HadError = false;
Zachary Turner0d840742016-10-07 21:34:46 +0000727 for (auto S : ModS.symbols(&HadError)) {
728 DictScope LL(P, "");
729 if (opts::raw::DumpModuleSyms) {
730 if (auto EC = SD.dump(S)) {
731 llvm::consumeError(std::move(EC));
732 HadError = true;
733 break;
734 }
735 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000736 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000737 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000738 }
739 if (HadError)
740 return make_error<RawError>(
741 raw_error_code::corrupt_file,
742 "DBI stream contained corrupt symbol record");
743 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000744 if (opts::raw::DumpLineInfo) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000745 ListScope SS(P, "LineInfo");
746 bool HadError = false;
747 // Define a locally scoped visitor to print the different
748 // substream types types.
749 class RecordVisitor : public codeview::IModuleSubstreamVisitor {
750 public:
751 RecordVisitor(ScopedPrinter &P, PDBFile &F) : P(P), F(F) {}
752 Error visitUnknown(ModuleSubstreamKind Kind,
Zachary Turnerd66889c2016-07-28 19:12:28 +0000753 ReadableStreamRef Stream) override {
Zachary Turnerd3117392016-06-03 19:28:33 +0000754 DictScope DD(P, "Unknown");
755 ArrayRef<uint8_t> Data;
756 StreamReader R(Stream);
757 if (auto EC = R.readBytes(Data, R.bytesRemaining())) {
758 return make_error<RawError>(
759 raw_error_code::corrupt_file,
760 "DBI stream contained corrupt line info record");
761 }
762 P.printBinaryBlock("Data", Data);
763 return Error::success();
764 }
765 Error
Zachary Turnerd66889c2016-07-28 19:12:28 +0000766 visitFileChecksums(ReadableStreamRef Data,
Zachary Turnerd3117392016-06-03 19:28:33 +0000767 const FileChecksumArray &Checksums) override {
768 DictScope DD(P, "FileChecksums");
769 for (const auto &C : Checksums) {
770 DictScope DDD(P, "Checksum");
771 if (auto Result = getFileNameForOffset(C.FileNameOffset))
772 P.printString("FileName", Result.get());
773 else
774 return Result.takeError();
775 P.flush();
776 P.printEnum("Kind", uint8_t(C.Kind), getFileChecksumNames());
777 P.printBinaryBlock("Checksum", C.Checksum);
778 }
779 return Error::success();
780 }
781
Zachary Turnerd66889c2016-07-28 19:12:28 +0000782 Error visitLines(ReadableStreamRef Data,
783 const LineSubstreamHeader *Header,
Zachary Turnerd3117392016-06-03 19:28:33 +0000784 const LineInfoArray &Lines) override {
785 DictScope DD(P, "Lines");
786 for (const auto &L : Lines) {
787 if (auto Result = getFileNameForOffset2(L.NameIndex))
788 P.printString("FileName", Result.get());
789 else
790 return Result.takeError();
791 P.flush();
792 for (const auto &N : L.LineNumbers) {
793 DictScope DDD(P, "Line");
794 LineInfo LI(N.Flags);
795 P.printNumber("Offset", N.Offset);
796 if (LI.isAlwaysStepInto())
797 P.printString("StepInto", StringRef("Always"));
798 else if (LI.isNeverStepInto())
799 P.printString("StepInto", StringRef("Never"));
800 else
801 P.printNumber("LineNumberStart", LI.getStartLine());
802 P.printNumber("EndDelta", LI.getLineDelta());
803 P.printBoolean("IsStatement", LI.isStatement());
804 }
805 for (const auto &C : L.Columns) {
806 DictScope DDD(P, "Column");
807 P.printNumber("Start", C.StartColumn);
808 P.printNumber("End", C.EndColumn);
809 }
810 }
811 return Error::success();
812 }
813
814 private:
815 Expected<StringRef> getFileNameForOffset(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000816 auto ST = F.getStringTable();
817 if (!ST)
818 return ST.takeError();
819
820 return ST->getStringForID(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000821 }
822 Expected<StringRef> getFileNameForOffset2(uint32_t Offset) {
Zachary Turnera1657a92016-06-08 17:26:39 +0000823 auto DS = F.getPDBDbiStream();
824 if (!DS)
825 return DS.takeError();
826 return DS->getFileNameForIndex(Offset);
Zachary Turnerd3117392016-06-03 19:28:33 +0000827 }
828 ScopedPrinter &P;
829 PDBFile &F;
830 };
831
832 RecordVisitor V(P, File);
833 for (const auto &L : ModS.lines(&HadError)) {
834 if (auto EC = codeview::visitModuleSubstream(L, V))
835 return EC;
836 }
837 }
838 }
839 }
840 }
841 return Error::success();
842}
843
844Error LLVMOutputStyle::dumpSectionContribs() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000845 if (!opts::raw::DumpSectionContribs)
Zachary Turnerd3117392016-06-03 19:28:33 +0000846 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000847 if (!File.hasPDBDbiStream()) {
848 P.printString("DBI Stream not present");
849 return Error::success();
850 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000851
Zachary Turnera1657a92016-06-08 17:26:39 +0000852 auto Dbi = File.getPDBDbiStream();
853 if (!Dbi)
854 return Dbi.takeError();
855
Zachary Turnerd3117392016-06-03 19:28:33 +0000856 ListScope L(P, "Section Contributions");
857 class Visitor : public ISectionContribVisitor {
858 public:
859 Visitor(ScopedPrinter &P, DbiStream &DS) : P(P), DS(DS) {}
860 void visit(const SectionContrib &SC) override {
861 DictScope D(P, "Contribution");
862 P.printNumber("ISect", SC.ISect);
863 P.printNumber("Off", SC.Off);
864 P.printNumber("Size", SC.Size);
865 P.printFlags("Characteristics", SC.Characteristics,
866 codeview::getImageSectionCharacteristicNames(),
867 COFF::SectionCharacteristics(0x00F00000));
868 {
869 DictScope DD(P, "Module");
870 P.printNumber("Index", SC.Imod);
871 auto M = DS.modules();
872 if (M.size() > SC.Imod) {
873 P.printString("Name", M[SC.Imod].Info.getModuleName());
874 }
875 }
876 P.printNumber("Data CRC", SC.DataCrc);
877 P.printNumber("Reloc CRC", SC.RelocCrc);
878 P.flush();
879 }
880 void visit(const SectionContrib2 &SC) override {
881 visit(SC.Base);
882 P.printNumber("ISect Coff", SC.ISectCoff);
883 P.flush();
884 }
885
886 private:
887 ScopedPrinter &P;
888 DbiStream &DS;
889 };
Zachary Turnera1657a92016-06-08 17:26:39 +0000890 Visitor V(P, *Dbi);
891 Dbi->visitSectionContributions(V);
Zachary Turnerd3117392016-06-03 19:28:33 +0000892 return Error::success();
893}
894
895Error LLVMOutputStyle::dumpSectionMap() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000896 if (!opts::raw::DumpSectionMap)
Zachary Turnerd3117392016-06-03 19:28:33 +0000897 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000898 if (!File.hasPDBDbiStream()) {
899 P.printString("DBI Stream not present");
900 return Error::success();
901 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000902
Zachary Turnera1657a92016-06-08 17:26:39 +0000903 auto Dbi = File.getPDBDbiStream();
904 if (!Dbi)
905 return Dbi.takeError();
906
Zachary Turnerd3117392016-06-03 19:28:33 +0000907 ListScope L(P, "Section Map");
Zachary Turnera1657a92016-06-08 17:26:39 +0000908 for (auto &M : Dbi->getSectionMap()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000909 DictScope D(P, "Entry");
910 P.printFlags("Flags", M.Flags, getOMFSegMapDescFlagNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000911 P.printNumber("Ovl", M.Ovl);
912 P.printNumber("Group", M.Group);
913 P.printNumber("Frame", M.Frame);
914 P.printNumber("SecName", M.SecName);
915 P.printNumber("ClassName", M.ClassName);
916 P.printNumber("Offset", M.Offset);
917 P.printNumber("SecByteLength", M.SecByteLength);
918 P.flush();
919 }
920 return Error::success();
921}
922
923Error LLVMOutputStyle::dumpPublicsStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000924 if (!opts::raw::DumpPublics)
Zachary Turnerd3117392016-06-03 19:28:33 +0000925 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000926 if (!File.hasPDBPublicsStream()) {
927 P.printString("Publics Stream not present");
928 return Error::success();
929 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000930
Zachary Turnera1657a92016-06-08 17:26:39 +0000931 auto Publics = File.getPDBPublicsStream();
932 if (!Publics)
933 return Publics.takeError();
Bob Haarmana5b43582016-12-05 22:44:00 +0000934 DictScope D(P, "Publics Stream");
Zachary Turnera1657a92016-06-08 17:26:39 +0000935
936 auto Dbi = File.getPDBDbiStream();
937 if (!Dbi)
938 return Dbi.takeError();
939
940 P.printNumber("Stream number", Dbi->getPublicSymbolStreamIndex());
941 P.printNumber("SymHash", Publics->getSymHash());
942 P.printNumber("AddrMap", Publics->getAddrMap());
943 P.printNumber("Number of buckets", Publics->getNumBuckets());
944 P.printList("Hash Buckets", Publics->getHashBuckets());
945 P.printList("Address Map", Publics->getAddressMap());
946 P.printList("Thunk Map", Publics->getThunkMap());
947 P.printList("Section Offsets", Publics->getSectionOffsets(),
Zachary Turnerd3117392016-06-03 19:28:33 +0000948 printSectionOffset);
949 ListScope L(P, "Symbols");
Zachary Turner629cb7d2017-01-11 23:24:22 +0000950 codeview::CVSymbolDumper SD(P, TypeDB, nullptr, false);
Zachary Turnerd3117392016-06-03 19:28:33 +0000951 bool HadError = false;
Zachary Turnera1657a92016-06-08 17:26:39 +0000952 for (auto S : Publics->getSymbols(&HadError)) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000953 DictScope DD(P, "");
954
Zachary Turner0d840742016-10-07 21:34:46 +0000955 if (auto EC = SD.dump(S)) {
956 HadError = true;
957 break;
958 }
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000959 if (opts::raw::DumpSymRecordBytes)
Zachary Turnerc67b00c2016-09-14 23:00:16 +0000960 P.printBinaryBlock("Bytes", S.content());
Zachary Turnerd3117392016-06-03 19:28:33 +0000961 }
962 if (HadError)
963 return make_error<RawError>(
964 raw_error_code::corrupt_file,
965 "Public symbol stream contained corrupt record");
966
967 return Error::success();
968}
969
970Error LLVMOutputStyle::dumpSectionHeaders() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +0000971 if (!opts::raw::DumpSectionHeaders)
Zachary Turnerd3117392016-06-03 19:28:33 +0000972 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +0000973 if (!File.hasPDBDbiStream()) {
974 P.printString("DBI Stream not present");
975 return Error::success();
976 }
Zachary Turnerd3117392016-06-03 19:28:33 +0000977
Zachary Turnera1657a92016-06-08 17:26:39 +0000978 auto Dbi = File.getPDBDbiStream();
979 if (!Dbi)
980 return Dbi.takeError();
Zachary Turnerd3117392016-06-03 19:28:33 +0000981
982 ListScope D(P, "Section Headers");
Zachary Turnera1657a92016-06-08 17:26:39 +0000983 for (const object::coff_section &Section : Dbi->getSectionHeaders()) {
Zachary Turnerd3117392016-06-03 19:28:33 +0000984 DictScope DD(P, "");
985
986 // If a name is 8 characters long, there is no NUL character at end.
987 StringRef Name(Section.Name, strnlen(Section.Name, sizeof(Section.Name)));
988 P.printString("Name", Name);
989 P.printNumber("Virtual Size", Section.VirtualSize);
990 P.printNumber("Virtual Address", Section.VirtualAddress);
991 P.printNumber("Size of Raw Data", Section.SizeOfRawData);
992 P.printNumber("File Pointer to Raw Data", Section.PointerToRawData);
993 P.printNumber("File Pointer to Relocations", Section.PointerToRelocations);
994 P.printNumber("File Pointer to Linenumbers", Section.PointerToLinenumbers);
995 P.printNumber("Number of Relocations", Section.NumberOfRelocations);
996 P.printNumber("Number of Linenumbers", Section.NumberOfLinenumbers);
Rui Ueyama2c5384a2016-06-06 21:34:55 +0000997 P.printFlags("Characteristics", Section.Characteristics,
998 getImageSectionCharacteristicNames());
Zachary Turnerd3117392016-06-03 19:28:33 +0000999 }
1000 return Error::success();
1001}
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001002
1003Error LLVMOutputStyle::dumpFpoStream() {
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001004 if (!opts::raw::DumpFpo)
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001005 return Error::success();
Bob Haarmana5b43582016-12-05 22:44:00 +00001006 if (!File.hasPDBDbiStream()) {
1007 P.printString("DBI Stream not present");
1008 return Error::success();
1009 }
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001010
Zachary Turnera1657a92016-06-08 17:26:39 +00001011 auto Dbi = File.getPDBDbiStream();
1012 if (!Dbi)
1013 return Dbi.takeError();
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001014
1015 ListScope D(P, "New FPO");
Zachary Turnera1657a92016-06-08 17:26:39 +00001016 for (const object::FpoData &Fpo : Dbi->getFpoRecords()) {
Rui Ueyamaef2b4882016-06-06 18:39:21 +00001017 DictScope DD(P, "");
1018 P.printNumber("Offset", Fpo.Offset);
1019 P.printNumber("Size", Fpo.Size);
1020 P.printNumber("Number of locals", Fpo.NumLocals);
1021 P.printNumber("Number of params", Fpo.NumParams);
1022 P.printNumber("Size of Prolog", Fpo.getPrologSize());
1023 P.printNumber("Number of Saved Registers", Fpo.getNumSavedRegs());
1024 P.printBoolean("Has SEH", Fpo.hasSEH());
1025 P.printBoolean("Use BP", Fpo.useBP());
1026 P.printNumber("Frame Pointer", Fpo.getFP());
1027 }
1028 return Error::success();
1029}
Zachary Turnera30bd1a2016-06-30 17:42:48 +00001030
Zachary Turner7120a472016-06-06 20:37:05 +00001031void LLVMOutputStyle::flush() { P.flush(); }