blob: d16bfa480e1d84dc3ebd3ae155763fe354a6c6f6 [file] [log] [blame]
Zachary Turner0ebcf2f2018-03-29 16:28:20 +00001//===- ExplainOutputStyle.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 "ExplainOutputStyle.h"
11
12#include "FormatUtil.h"
Zachary Turner06e4df32018-04-04 17:29:09 +000013#include "InputFile.h"
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000014#include "StreamUtil.h"
15#include "llvm-pdbutil.h"
16
Zachary Turner21ff13f2018-03-30 17:16:50 +000017#include "llvm/DebugInfo/CodeView/Formatters.h"
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000018#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
Zachary Turner21ff13f2018-03-30 17:16:50 +000019#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000021#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
Zachary Turner21ff13f2018-03-30 17:16:50 +000022#include "llvm/DebugInfo/PDB/Native/RawTypes.h"
Zachary Turner06e4df32018-04-04 17:29:09 +000023#include "llvm/Support/BinaryByteStream.h"
Zachary Turner21ff13f2018-03-30 17:16:50 +000024#include "llvm/Support/BinaryStreamArray.h"
25#include "llvm/Support/Error.h"
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000026
27using namespace llvm;
28using namespace llvm::codeview;
29using namespace llvm::msf;
30using namespace llvm::pdb;
31
Zachary Turner06e4df32018-04-04 17:29:09 +000032ExplainOutputStyle::ExplainOutputStyle(InputFile &File, uint64_t FileOffset)
33 : File(File), FileOffset(FileOffset), P(2, false, outs()) {}
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000034
35Error ExplainOutputStyle::dump() {
36 P.formatLine("Explaining file offset {0} of file '{1}'.", FileOffset,
37 File.getFilePath());
38
Zachary Turner06e4df32018-04-04 17:29:09 +000039 if (File.isPdb())
40 return explainPdbFile();
41
42 return explainBinaryFile();
43}
44
45Error ExplainOutputStyle::explainPdbFile() {
46 bool IsAllocated = explainPdbBlockStatus();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000047 if (!IsAllocated)
48 return Error::success();
49
50 AutoIndent Indent(P);
Zachary Turner06e4df32018-04-04 17:29:09 +000051 if (isPdbSuperBlock())
52 explainPdbSuperBlockOffset();
53 else if (isPdbFpmBlock())
54 explainPdbFpmBlockOffset();
55 else if (isPdbBlockMapBlock())
56 explainPdbBlockMapOffset();
57 else if (isPdbStreamDirectoryBlock())
58 explainPdbStreamDirectoryOffset();
59 else if (auto Index = getPdbBlockStreamIndex())
60 explainPdbStreamOffset(*Index);
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000061 else
Zachary Turner06e4df32018-04-04 17:29:09 +000062 explainPdbUnknownBlock();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000063 return Error::success();
64}
65
Zachary Turner06e4df32018-04-04 17:29:09 +000066Error ExplainOutputStyle::explainBinaryFile() {
67 std::unique_ptr<BinaryByteStream> Stream =
68 llvm::make_unique<BinaryByteStream>(File.unknown().getBuffer(),
69 llvm::support::little);
70 switch (opts::explain::InputType) {
71 case opts::explain::InputFileType::DBIStream: {
72 DbiStream Dbi(std::move(Stream));
73 if (auto EC = Dbi.reload(nullptr))
74 return EC;
75 explainStreamOffset(Dbi, FileOffset);
76 break;
77 }
78 case opts::explain::InputFileType::PDBStream: {
79 InfoStream Info(std::move(Stream));
80 if (auto EC = Info.reload())
81 return EC;
82 explainStreamOffset(Info, FileOffset);
83 break;
84 }
85 default:
86 llvm_unreachable("Invalid input file type!");
87 }
88 return Error::success();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000089}
90
Zachary Turner06e4df32018-04-04 17:29:09 +000091uint32_t ExplainOutputStyle::pdbBlockIndex() const {
92 return FileOffset / File.pdb().getBlockSize();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000093}
94
Zachary Turner06e4df32018-04-04 17:29:09 +000095uint32_t ExplainOutputStyle::pdbBlockOffset() const {
96 uint64_t BlockStart = pdbBlockIndex() * File.pdb().getBlockSize();
97 assert(FileOffset >= BlockStart);
98 return FileOffset - BlockStart;
Zachary Turner0ebcf2f2018-03-29 16:28:20 +000099}
100
Zachary Turner06e4df32018-04-04 17:29:09 +0000101bool ExplainOutputStyle::isPdbSuperBlock() const {
102 return pdbBlockIndex() == 0;
103}
104
105bool ExplainOutputStyle::isPdbFpm1() const {
106 return ((pdbBlockIndex() - 1) % File.pdb().getBlockSize() == 0);
107}
108bool ExplainOutputStyle::isPdbFpm2() const {
109 return ((pdbBlockIndex() - 2) % File.pdb().getBlockSize() == 0);
110}
111
112bool ExplainOutputStyle::isPdbFpmBlock() const {
113 return isPdbFpm1() || isPdbFpm2();
114}
115
116bool ExplainOutputStyle::isPdbBlockMapBlock() const {
117 return pdbBlockIndex() == File.pdb().getBlockMapIndex();
118}
119
120bool ExplainOutputStyle::isPdbStreamDirectoryBlock() const {
121 const auto &Layout = File.pdb().getMsfLayout();
122 return llvm::is_contained(Layout.DirectoryBlocks, pdbBlockIndex());
123}
124
125Optional<uint32_t> ExplainOutputStyle::getPdbBlockStreamIndex() const {
126 const auto &Layout = File.pdb().getMsfLayout();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000127 for (const auto &Entry : enumerate(Layout.StreamMap)) {
Zachary Turner06e4df32018-04-04 17:29:09 +0000128 if (!llvm::is_contained(Entry.value(), pdbBlockIndex()))
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000129 continue;
130 return Entry.index();
131 }
132 return None;
133}
134
Zachary Turner06e4df32018-04-04 17:29:09 +0000135bool ExplainOutputStyle::explainPdbBlockStatus() {
136 if (FileOffset >= File.pdb().getFileSize()) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000137 P.formatLine("Address {0} is not in the file (file size = {1}).",
Zachary Turner06e4df32018-04-04 17:29:09 +0000138 FileOffset, File.pdb().getFileSize());
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000139 return false;
140 }
Zachary Turner06e4df32018-04-04 17:29:09 +0000141 P.formatLine("Block:Offset = {2:X-}:{1:X-4}.", FileOffset, pdbBlockOffset(),
142 pdbBlockIndex());
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000143
Zachary Turner06e4df32018-04-04 17:29:09 +0000144 bool IsFree = File.pdb().getMsfLayout().FreePageMap[pdbBlockIndex()];
145 P.formatLine("Address is in block {0} ({1}allocated).", pdbBlockIndex(),
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000146 IsFree ? "un" : "");
147 return !IsFree;
148}
149
Zachary Turner5551cf22018-03-29 17:11:14 +0000150#define endof(Class, Field) (offsetof(Class, Field) + sizeof(Class::Field))
151
Zachary Turner06e4df32018-04-04 17:29:09 +0000152void ExplainOutputStyle::explainPdbSuperBlockOffset() {
Zachary Turner5551cf22018-03-29 17:11:14 +0000153 P.formatLine("This corresponds to offset {0} of the MSF super block, ",
Zachary Turner06e4df32018-04-04 17:29:09 +0000154 pdbBlockOffset());
155 if (pdbBlockOffset() < endof(SuperBlock, MagicBytes))
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000156 P.printLine("which is part of the MSF file magic.");
Zachary Turner06e4df32018-04-04 17:29:09 +0000157 else if (pdbBlockOffset() < endof(SuperBlock, BlockSize)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000158 P.printLine("which contains the block size of the file.");
Zachary Turner103f6b62018-03-29 17:45:34 +0000159 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000160 uint32_t(File.pdb().getMsfLayout().SB->BlockSize));
161 } else if (pdbBlockOffset() < endof(SuperBlock, FreeBlockMapBlock)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000162 P.printLine("which contains the index of the FPM block (e.g. 1 or 2).");
Zachary Turner103f6b62018-03-29 17:45:34 +0000163 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000164 uint32_t(File.pdb().getMsfLayout().SB->FreeBlockMapBlock));
165 } else if (pdbBlockOffset() < endof(SuperBlock, NumBlocks)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000166 P.printLine("which contains the number of blocks in the file.");
Zachary Turner103f6b62018-03-29 17:45:34 +0000167 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000168 uint32_t(File.pdb().getMsfLayout().SB->NumBlocks));
169 } else if (pdbBlockOffset() < endof(SuperBlock, NumDirectoryBytes)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000170 P.printLine("which contains the number of bytes in the stream directory.");
Zachary Turner103f6b62018-03-29 17:45:34 +0000171 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000172 uint32_t(File.pdb().getMsfLayout().SB->NumDirectoryBytes));
173 } else if (pdbBlockOffset() < endof(SuperBlock, Unknown1)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000174 P.printLine("whose purpose is unknown.");
Zachary Turner103f6b62018-03-29 17:45:34 +0000175 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000176 uint32_t(File.pdb().getMsfLayout().SB->Unknown1));
177 } else if (pdbBlockOffset() < endof(SuperBlock, BlockMapAddr)) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000178 P.printLine("which contains the file offset of the block map.");
Zachary Turner103f6b62018-03-29 17:45:34 +0000179 P.formatLine("The current value is {0}.",
Zachary Turner06e4df32018-04-04 17:29:09 +0000180 uint32_t(File.pdb().getMsfLayout().SB->BlockMapAddr));
Zachary Turner103f6b62018-03-29 17:45:34 +0000181 } else {
Zachary Turner06e4df32018-04-04 17:29:09 +0000182 assert(pdbBlockOffset() > sizeof(SuperBlock));
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000183 P.printLine(
184 "which is outside the range of valid data for the super block.");
185 }
186}
187
Zachary Turner103f6b62018-03-29 17:45:34 +0000188static std::string toBinaryString(uint8_t Byte) {
189 char Result[9] = {0};
190 for (int I = 0; I < 8; ++I) {
191 char C = (Byte & 1) ? '1' : '0';
192 Result[I] = C;
193 Byte >>= 1;
194 }
195 return std::string(Result);
196}
197
Zachary Turner06e4df32018-04-04 17:29:09 +0000198void ExplainOutputStyle::explainPdbFpmBlockOffset() {
199 const MSFLayout &Layout = File.pdb().getMsfLayout();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000200 uint32_t MainFpm = Layout.mainFpmBlock();
201 uint32_t AltFpm = Layout.alternateFpmBlock();
202
Zachary Turner06e4df32018-04-04 17:29:09 +0000203 assert(isPdbFpmBlock());
204 uint32_t Fpm = isPdbFpm1() ? 1 : 2;
205 uint32_t FpmChunk = pdbBlockIndex() / File.pdb().getBlockSize();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000206 assert((Fpm == MainFpm) || (Fpm == AltFpm));
207 (void)AltFpm;
208 bool IsMain = (Fpm == MainFpm);
209 P.formatLine("Address is in FPM{0} ({1} FPM)", Fpm, IsMain ? "Main" : "Alt");
210 uint32_t DescribedBlockStart =
Zachary Turner06e4df32018-04-04 17:29:09 +0000211 8 * (FpmChunk * File.pdb().getBlockSize() + pdbBlockOffset());
212 if (DescribedBlockStart > File.pdb().getBlockCount()) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000213 P.printLine("Address is in extraneous FPM space.");
214 return;
215 }
216
217 P.formatLine("Address describes the allocation status of blocks [{0},{1})",
218 DescribedBlockStart, DescribedBlockStart + 8);
Zachary Turner103f6b62018-03-29 17:45:34 +0000219 ArrayRef<uint8_t> Bytes;
Zachary Turner06e4df32018-04-04 17:29:09 +0000220 cantFail(File.pdb().getMsfBuffer().readBytes(FileOffset, 1, Bytes));
Zachary Turner103f6b62018-03-29 17:45:34 +0000221 P.formatLine("Status = {0} (Note: 0 = allocated, 1 = free)",
222 toBinaryString(Bytes[0]));
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000223}
224
Zachary Turner06e4df32018-04-04 17:29:09 +0000225void ExplainOutputStyle::explainPdbBlockMapOffset() {
226 uint64_t BlockMapOffset = File.pdb().getBlockMapOffset();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000227 uint32_t OffsetInBlock = FileOffset - BlockMapOffset;
228 P.formatLine("Address is at offset {0} of the directory block list",
229 OffsetInBlock);
230}
231
232static uint32_t getOffsetInStream(ArrayRef<support::ulittle32_t> StreamBlocks,
233 uint64_t FileOffset, uint32_t BlockSize) {
234 uint32_t BlockIndex = FileOffset / BlockSize;
235 uint32_t OffsetInBlock = FileOffset - BlockIndex * BlockSize;
236
237 auto Iter = llvm::find(StreamBlocks, BlockIndex);
238 assert(Iter != StreamBlocks.end());
239 uint32_t StreamBlockIndex = std::distance(StreamBlocks.begin(), Iter);
240 return StreamBlockIndex * BlockSize + OffsetInBlock;
241}
242
Zachary Turner06e4df32018-04-04 17:29:09 +0000243void ExplainOutputStyle::explainPdbStreamOffset(uint32_t Stream) {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000244 SmallVector<StreamInfo, 12> Streams;
Zachary Turner06e4df32018-04-04 17:29:09 +0000245 discoverStreamPurposes(File.pdb(), Streams);
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000246
247 assert(Stream <= Streams.size());
248 const StreamInfo &S = Streams[Stream];
Zachary Turner06e4df32018-04-04 17:29:09 +0000249 const auto &Layout = File.pdb().getStreamLayout(Stream);
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000250 uint32_t StreamOff =
Zachary Turner06e4df32018-04-04 17:29:09 +0000251 getOffsetInStream(Layout.Blocks, FileOffset, File.pdb().getBlockSize());
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000252 P.formatLine("Address is at offset {0}/{1} of Stream {2} ({3}){4}.",
253 StreamOff, Layout.Length, Stream, S.getLongName(),
254 (StreamOff > Layout.Length) ? " in unused space" : "");
Zachary Turner21ff13f2018-03-30 17:16:50 +0000255 switch (S.getPurpose()) {
Zachary Turner06e4df32018-04-04 17:29:09 +0000256 case StreamPurpose::DBI: {
257 DbiStream &Dbi = cantFail(File.pdb().getPDBDbiStream());
258 explainStreamOffset(Dbi, StreamOff);
Zachary Turner21ff13f2018-03-30 17:16:50 +0000259 break;
Zachary Turner06e4df32018-04-04 17:29:09 +0000260 }
261 case StreamPurpose::PDB: {
262 InfoStream &Info = cantFail(File.pdb().getPDBInfoStream());
263 explainStreamOffset(Info, StreamOff);
Zachary Turner21ff13f2018-03-30 17:16:50 +0000264 break;
Zachary Turner06e4df32018-04-04 17:29:09 +0000265 }
Zachary Turner21ff13f2018-03-30 17:16:50 +0000266 case StreamPurpose::IPI:
267 case StreamPurpose::TPI:
268 case StreamPurpose::ModuleStream:
269 case StreamPurpose::NamedStream:
270 default:
271 break;
272 }
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000273}
274
Zachary Turner06e4df32018-04-04 17:29:09 +0000275void ExplainOutputStyle::explainPdbStreamDirectoryOffset() {
276 auto DirectoryBlocks = File.pdb().getDirectoryBlockArray();
277 const auto &Layout = File.pdb().getMsfLayout();
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000278 uint32_t StreamOff =
Zachary Turner06e4df32018-04-04 17:29:09 +0000279 getOffsetInStream(DirectoryBlocks, FileOffset, File.pdb().getBlockSize());
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000280 P.formatLine("Address is at offset {0}/{1} of Stream Directory{2}.",
281 StreamOff, uint32_t(Layout.SB->NumDirectoryBytes),
282 uint32_t(StreamOff > Layout.SB->NumDirectoryBytes)
283 ? " in unused space"
284 : "");
285}
286
Zachary Turner06e4df32018-04-04 17:29:09 +0000287void ExplainOutputStyle::explainPdbUnknownBlock() {
Zachary Turner0ebcf2f2018-03-29 16:28:20 +0000288 P.formatLine("Address has unknown purpose.");
289}
Zachary Turner21ff13f2018-03-30 17:16:50 +0000290
291template <typename T>
292static void printStructField(LinePrinter &P, StringRef Label, T Value) {
293 P.formatLine("which contains {0}.", Label);
294 P.formatLine("The current value is {0}.", Value);
295}
296
297static void explainDbiHeaderOffset(LinePrinter &P, DbiStream &Dbi,
298 uint32_t Offset) {
299 const DbiStreamHeader *Header = Dbi.getHeader();
300 assert(Header != nullptr);
301
302 if (Offset < endof(DbiStreamHeader, VersionSignature))
303 printStructField(P, "the DBI Stream Version Signature",
304 int32_t(Header->VersionSignature));
305 else if (Offset < endof(DbiStreamHeader, VersionHeader))
306 printStructField(P, "the DBI Stream Version Header",
307 uint32_t(Header->VersionHeader));
308 else if (Offset < endof(DbiStreamHeader, Age))
309 printStructField(P, "the age of the DBI Stream", uint32_t(Header->Age));
310 else if (Offset < endof(DbiStreamHeader, GlobalSymbolStreamIndex))
311 printStructField(P, "the index of the Global Symbol Stream",
312 uint16_t(Header->GlobalSymbolStreamIndex));
313 else if (Offset < endof(DbiStreamHeader, BuildNumber))
314 printStructField(P, "the build number", uint16_t(Header->BuildNumber));
315 else if (Offset < endof(DbiStreamHeader, PublicSymbolStreamIndex))
316 printStructField(P, "the index of the Public Symbol Stream",
317 uint16_t(Header->PublicSymbolStreamIndex));
318 else if (Offset < endof(DbiStreamHeader, PdbDllVersion))
319 printStructField(P, "the version of mspdb.dll",
320 uint16_t(Header->PdbDllVersion));
321 else if (Offset < endof(DbiStreamHeader, SymRecordStreamIndex))
322 printStructField(P, "the index of the Symbol Record Stream",
323 uint16_t(Header->SymRecordStreamIndex));
324 else if (Offset < endof(DbiStreamHeader, PdbDllRbld))
325 printStructField(P, "the rbld of mspdb.dll", uint16_t(Header->PdbDllRbld));
326 else if (Offset < endof(DbiStreamHeader, ModiSubstreamSize))
327 printStructField(P, "the size of the Module Info Substream",
328 int32_t(Header->ModiSubstreamSize));
329 else if (Offset < endof(DbiStreamHeader, SecContrSubstreamSize))
330 printStructField(P, "the size of the Section Contribution Substream",
331 int32_t(Header->SecContrSubstreamSize));
332 else if (Offset < endof(DbiStreamHeader, SectionMapSize))
333 printStructField(P, "the size of the Section Map Substream",
334 int32_t(Header->SectionMapSize));
335 else if (Offset < endof(DbiStreamHeader, FileInfoSize))
336 printStructField(P, "the size of the File Info Substream",
337 int32_t(Header->FileInfoSize));
338 else if (Offset < endof(DbiStreamHeader, TypeServerSize))
339 printStructField(P, "the size of the Type Server Map",
340 int32_t(Header->TypeServerSize));
341 else if (Offset < endof(DbiStreamHeader, MFCTypeServerIndex))
342 printStructField(P, "the index of the MFC Type Server stream",
343 uint32_t(Header->MFCTypeServerIndex));
344 else if (Offset < endof(DbiStreamHeader, OptionalDbgHdrSize))
345 printStructField(P, "the size of the Optional Debug Stream array",
346 int32_t(Header->OptionalDbgHdrSize));
347 else if (Offset < endof(DbiStreamHeader, ECSubstreamSize))
348 printStructField(P, "the size of the Edit & Continue Substream",
349 int32_t(Header->ECSubstreamSize));
350 else if (Offset < endof(DbiStreamHeader, Flags))
351 printStructField(P, "the DBI Stream flags", uint16_t(Header->Flags));
352 else if (Offset < endof(DbiStreamHeader, MachineType))
353 printStructField(P, "the machine type", uint16_t(Header->MachineType));
354 else if (Offset < endof(DbiStreamHeader, Reserved))
355 printStructField(P, "reserved data", uint32_t(Header->Reserved));
356}
357
358static void explainDbiModiSubstreamOffset(LinePrinter &P, DbiStream &Dbi,
359 uint32_t Offset) {
360 VarStreamArray<DbiModuleDescriptor> ModuleDescriptors;
361 BinaryStreamRef ModiSubstreamData = Dbi.getModiSubstreamData().StreamData;
362 BinaryStreamReader Reader(ModiSubstreamData);
363
364 cantFail(Reader.readArray(ModuleDescriptors, ModiSubstreamData.getLength()));
365 auto Prev = ModuleDescriptors.begin();
366 assert(Prev.offset() == 0);
367 auto Current = Prev;
368 uint32_t Index = 0;
369 while (true) {
370 Prev = Current;
371 ++Current;
372 if (Current == ModuleDescriptors.end() || Offset < Current.offset())
373 break;
374 ++Index;
375 }
376
377 DbiModuleDescriptor &Descriptor = *Prev;
378 P.formatLine("which contains the descriptor for module {0} ({1}).", Index,
379 Descriptor.getModuleName());
380}
381
382template <typename T>
383static void dontExplain(LinePrinter &Printer, T &Stream, uint32_t Offset) {}
384
385template <typename T, typename SubstreamRangeT>
386static void explainSubstreamOffset(LinePrinter &P, uint32_t OffsetInStream,
387 T &Stream,
388 const SubstreamRangeT &Substreams) {
389 uint32_t SubOffset = OffsetInStream;
390 for (const auto &Entry : Substreams) {
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000391 if (Entry.Size <= 0)
Zachary Turner21ff13f2018-03-30 17:16:50 +0000392 continue;
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000393 uint32_t S = static_cast<uint32_t>(Entry.Size);
394 if (SubOffset < S) {
395 P.formatLine("address is at offset {0}/{1} of the {2}.", SubOffset, S,
396 Entry.Label);
Zachary Turner21ff13f2018-03-30 17:16:50 +0000397 Entry.Explain(P, Stream, SubOffset);
398 return;
399 }
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000400 SubOffset -= S;
Zachary Turner21ff13f2018-03-30 17:16:50 +0000401 }
402}
403
Zachary Turner06e4df32018-04-04 17:29:09 +0000404void ExplainOutputStyle::explainStreamOffset(DbiStream &Dbi,
405 uint32_t OffsetInStream) {
Zachary Turner21ff13f2018-03-30 17:16:50 +0000406 P.printLine("Within the DBI stream:");
Zachary Turner21ff13f2018-03-30 17:16:50 +0000407 AutoIndent Indent(P);
408 const DbiStreamHeader *Header = Dbi.getHeader();
409 assert(Header != nullptr);
410
411 struct SubstreamInfo {
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000412 int32_t Size;
Zachary Turner21ff13f2018-03-30 17:16:50 +0000413 StringRef Label;
414 void (*Explain)(LinePrinter &, DbiStream &, uint32_t);
415 } Substreams[] = {
416 {sizeof(DbiStreamHeader), "DBI Stream Header", explainDbiHeaderOffset},
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000417 {int32_t(Header->ModiSubstreamSize), "Module Info Substream",
Zachary Turner21ff13f2018-03-30 17:16:50 +0000418 explainDbiModiSubstreamOffset},
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000419 {int32_t(Header->SecContrSubstreamSize), "Section Contribution Substream",
Zachary Turner21ff13f2018-03-30 17:16:50 +0000420 dontExplain<DbiStream>},
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000421 {int32_t(Header->SectionMapSize), "Section Map", dontExplain<DbiStream>},
422 {int32_t(Header->FileInfoSize), "File Info Substream",
Zachary Turner21ff13f2018-03-30 17:16:50 +0000423 dontExplain<DbiStream>},
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000424 {int32_t(Header->TypeServerSize), "Type Server Map Substream",
Zachary Turner21ff13f2018-03-30 17:16:50 +0000425 dontExplain<DbiStream>},
Zachary Turnerb145b4e2018-03-30 17:28:35 +0000426 {int32_t(Header->ECSubstreamSize), "Edit & Continue Substream",
427 dontExplain<DbiStream>},
428 {int32_t(Header->OptionalDbgHdrSize), "Optional Debug Stream Array",
Zachary Turner21ff13f2018-03-30 17:16:50 +0000429 dontExplain<DbiStream>},
430 };
431
432 explainSubstreamOffset(P, OffsetInStream, Dbi, Substreams);
433}
434
435static void explainPdbStreamHeaderOffset(LinePrinter &P, InfoStream &Info,
436 uint32_t Offset) {
437 const InfoStreamHeader *Header = Info.getHeader();
438 assert(Header != nullptr);
439
440 if (Offset < endof(InfoStreamHeader, Version))
441 printStructField(P, "the PDB Stream Version Signature",
442 uint32_t(Header->Version));
443 else if (Offset < endof(InfoStreamHeader, Signature))
444 printStructField(P, "the signature of the PDB Stream",
445 uint32_t(Header->Signature));
446 else if (Offset < endof(InfoStreamHeader, Age))
447 printStructField(P, "the age of the PDB", uint32_t(Header->Age));
448 else if (Offset < endof(InfoStreamHeader, Guid))
449 printStructField(P, "the guid of the PDB", fmt_guid(Header->Guid.Guid));
450}
451
Zachary Turner06e4df32018-04-04 17:29:09 +0000452void ExplainOutputStyle::explainStreamOffset(InfoStream &Info,
453 uint32_t OffsetInStream) {
Zachary Turner21ff13f2018-03-30 17:16:50 +0000454 P.printLine("Within the PDB stream:");
Zachary Turner21ff13f2018-03-30 17:16:50 +0000455 AutoIndent Indent(P);
456
457 struct SubstreamInfo {
458 uint32_t Size;
459 StringRef Label;
460 void (*Explain)(LinePrinter &, InfoStream &, uint32_t);
461 } Substreams[] = {{sizeof(InfoStreamHeader), "PDB Stream Header",
462 explainPdbStreamHeaderOffset},
463 {Info.getNamedStreamMapByteSize(), "Named Stream Map",
464 dontExplain<InfoStream>},
465 {Info.getStreamSize(), "PDB Feature Signatures",
466 dontExplain<InfoStream>}};
467
468 explainSubstreamOffset(P, OffsetInStream, Info, Substreams);
469}