1 //===- ExplainOutputStyle.cpp --------------------------------- *- C++ --*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "ExplainOutputStyle.h"
12 #include "FormatUtil.h"
13 #include "InputFile.h"
14 #include "StreamUtil.h"
15 #include "llvm-pdbutil.h"
17 #include "llvm/DebugInfo/CodeView/Formatters.h"
18 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
19 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
20 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
21 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
22 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
23 #include "llvm/Support/BinaryByteStream.h"
24 #include "llvm/Support/BinaryStreamArray.h"
25 #include "llvm/Support/Error.h"
28 using namespace llvm::codeview;
29 using namespace llvm::msf;
30 using namespace llvm::pdb;
32 ExplainOutputStyle::ExplainOutputStyle(InputFile &File, uint64_t FileOffset)
33 : File(File), FileOffset(FileOffset), P(2, false, outs()) {}
35 Error ExplainOutputStyle::dump() {
36 P.formatLine("Explaining file offset {0} of file '{1}'.", FileOffset,
40 return explainPdbFile();
42 return explainBinaryFile();
45 Error ExplainOutputStyle::explainPdbFile() {
46 bool IsAllocated = explainPdbBlockStatus();
48 return Error::success();
51 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);
62 explainPdbUnknownBlock();
63 return Error::success();
66 Error 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))
75 explainStreamOffset(Dbi, FileOffset);
78 case opts::explain::InputFileType::PDBStream: {
79 InfoStream Info(std::move(Stream));
80 if (auto EC = Info.reload())
82 explainStreamOffset(Info, FileOffset);
86 llvm_unreachable("Invalid input file type!");
88 return Error::success();
91 uint32_t ExplainOutputStyle::pdbBlockIndex() const {
92 return FileOffset / File.pdb().getBlockSize();
95 uint32_t ExplainOutputStyle::pdbBlockOffset() const {
96 uint64_t BlockStart = pdbBlockIndex() * File.pdb().getBlockSize();
97 assert(FileOffset >= BlockStart);
98 return FileOffset - BlockStart;
101 bool ExplainOutputStyle::isPdbSuperBlock() const {
102 return pdbBlockIndex() == 0;
105 bool ExplainOutputStyle::isPdbFpm1() const {
106 return ((pdbBlockIndex() - 1) % File.pdb().getBlockSize() == 0);
108 bool ExplainOutputStyle::isPdbFpm2() const {
109 return ((pdbBlockIndex() - 2) % File.pdb().getBlockSize() == 0);
112 bool ExplainOutputStyle::isPdbFpmBlock() const {
113 return isPdbFpm1() || isPdbFpm2();
116 bool ExplainOutputStyle::isPdbBlockMapBlock() const {
117 return pdbBlockIndex() == File.pdb().getBlockMapIndex();
120 bool ExplainOutputStyle::isPdbStreamDirectoryBlock() const {
121 const auto &Layout = File.pdb().getMsfLayout();
122 return llvm::is_contained(Layout.DirectoryBlocks, pdbBlockIndex());
125 Optional<uint32_t> ExplainOutputStyle::getPdbBlockStreamIndex() const {
126 const auto &Layout = File.pdb().getMsfLayout();
127 for (const auto &Entry : enumerate(Layout.StreamMap)) {
128 if (!llvm::is_contained(Entry.value(), pdbBlockIndex()))
130 return Entry.index();
135 bool ExplainOutputStyle::explainPdbBlockStatus() {
136 if (FileOffset >= File.pdb().getFileSize()) {
137 P.formatLine("Address {0} is not in the file (file size = {1}).",
138 FileOffset, File.pdb().getFileSize());
141 P.formatLine("Block:Offset = {2:X-}:{1:X-4}.", FileOffset, pdbBlockOffset(),
144 bool IsFree = File.pdb().getMsfLayout().FreePageMap[pdbBlockIndex()];
145 P.formatLine("Address is in block {0} ({1}allocated).", pdbBlockIndex(),
150 #define endof(Class, Field) (offsetof(Class, Field) + sizeof(Class::Field))
152 void ExplainOutputStyle::explainPdbSuperBlockOffset() {
153 P.formatLine("This corresponds to offset {0} of the MSF super block, ",
155 if (pdbBlockOffset() < endof(SuperBlock, MagicBytes))
156 P.printLine("which is part of the MSF file magic.");
157 else if (pdbBlockOffset() < endof(SuperBlock, BlockSize)) {
158 P.printLine("which contains the block size of the file.");
159 P.formatLine("The current value is {0}.",
160 uint32_t(File.pdb().getMsfLayout().SB->BlockSize));
161 } else if (pdbBlockOffset() < endof(SuperBlock, FreeBlockMapBlock)) {
162 P.printLine("which contains the index of the FPM block (e.g. 1 or 2).");
163 P.formatLine("The current value is {0}.",
164 uint32_t(File.pdb().getMsfLayout().SB->FreeBlockMapBlock));
165 } else if (pdbBlockOffset() < endof(SuperBlock, NumBlocks)) {
166 P.printLine("which contains the number of blocks in the file.");
167 P.formatLine("The current value is {0}.",
168 uint32_t(File.pdb().getMsfLayout().SB->NumBlocks));
169 } else if (pdbBlockOffset() < endof(SuperBlock, NumDirectoryBytes)) {
170 P.printLine("which contains the number of bytes in the stream directory.");
171 P.formatLine("The current value is {0}.",
172 uint32_t(File.pdb().getMsfLayout().SB->NumDirectoryBytes));
173 } else if (pdbBlockOffset() < endof(SuperBlock, Unknown1)) {
174 P.printLine("whose purpose is unknown.");
175 P.formatLine("The current value is {0}.",
176 uint32_t(File.pdb().getMsfLayout().SB->Unknown1));
177 } else if (pdbBlockOffset() < endof(SuperBlock, BlockMapAddr)) {
178 P.printLine("which contains the file offset of the block map.");
179 P.formatLine("The current value is {0}.",
180 uint32_t(File.pdb().getMsfLayout().SB->BlockMapAddr));
182 assert(pdbBlockOffset() > sizeof(SuperBlock));
184 "which is outside the range of valid data for the super block.");
188 static 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';
195 return std::string(Result);
198 void ExplainOutputStyle::explainPdbFpmBlockOffset() {
199 const MSFLayout &Layout = File.pdb().getMsfLayout();
200 uint32_t MainFpm = Layout.mainFpmBlock();
201 uint32_t AltFpm = Layout.alternateFpmBlock();
203 assert(isPdbFpmBlock());
204 uint32_t Fpm = isPdbFpm1() ? 1 : 2;
205 uint32_t FpmChunk = pdbBlockIndex() / File.pdb().getBlockSize();
206 assert((Fpm == MainFpm) || (Fpm == AltFpm));
208 bool IsMain = (Fpm == MainFpm);
209 P.formatLine("Address is in FPM{0} ({1} FPM)", Fpm, IsMain ? "Main" : "Alt");
210 uint32_t DescribedBlockStart =
211 8 * (FpmChunk * File.pdb().getBlockSize() + pdbBlockOffset());
212 if (DescribedBlockStart > File.pdb().getBlockCount()) {
213 P.printLine("Address is in extraneous FPM space.");
217 P.formatLine("Address describes the allocation status of blocks [{0},{1})",
218 DescribedBlockStart, DescribedBlockStart + 8);
219 ArrayRef<uint8_t> Bytes;
220 cantFail(File.pdb().getMsfBuffer().readBytes(FileOffset, 1, Bytes));
221 P.formatLine("Status = {0} (Note: 0 = allocated, 1 = free)",
222 toBinaryString(Bytes[0]));
225 void ExplainOutputStyle::explainPdbBlockMapOffset() {
226 uint64_t BlockMapOffset = File.pdb().getBlockMapOffset();
227 uint32_t OffsetInBlock = FileOffset - BlockMapOffset;
228 P.formatLine("Address is at offset {0} of the directory block list",
232 static 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;
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;
243 void ExplainOutputStyle::explainPdbStreamOffset(uint32_t Stream) {
244 SmallVector<StreamInfo, 12> Streams;
245 discoverStreamPurposes(File.pdb(), Streams);
247 assert(Stream <= Streams.size());
248 const StreamInfo &S = Streams[Stream];
249 const auto &Layout = File.pdb().getStreamLayout(Stream);
251 getOffsetInStream(Layout.Blocks, FileOffset, File.pdb().getBlockSize());
252 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" : "");
255 switch (S.getPurpose()) {
256 case StreamPurpose::DBI: {
257 DbiStream &Dbi = cantFail(File.pdb().getPDBDbiStream());
258 explainStreamOffset(Dbi, StreamOff);
261 case StreamPurpose::PDB: {
262 InfoStream &Info = cantFail(File.pdb().getPDBInfoStream());
263 explainStreamOffset(Info, StreamOff);
266 case StreamPurpose::IPI:
267 case StreamPurpose::TPI:
268 case StreamPurpose::ModuleStream:
269 case StreamPurpose::NamedStream:
275 void ExplainOutputStyle::explainPdbStreamDirectoryOffset() {
276 auto DirectoryBlocks = File.pdb().getDirectoryBlockArray();
277 const auto &Layout = File.pdb().getMsfLayout();
279 getOffsetInStream(DirectoryBlocks, FileOffset, File.pdb().getBlockSize());
280 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)
287 void ExplainOutputStyle::explainPdbUnknownBlock() {
288 P.formatLine("Address has unknown purpose.");
291 template <typename T>
292 static void printStructField(LinePrinter &P, StringRef Label, T Value) {
293 P.formatLine("which contains {0}.", Label);
294 P.formatLine("The current value is {0}.", Value);
297 static void explainDbiHeaderOffset(LinePrinter &P, DbiStream &Dbi,
299 const DbiStreamHeader *Header = Dbi.getHeader();
300 assert(Header != nullptr);
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));
358 static void explainDbiModiSubstreamOffset(LinePrinter &P, DbiStream &Dbi,
360 VarStreamArray<DbiModuleDescriptor> ModuleDescriptors;
361 BinaryStreamRef ModiSubstreamData = Dbi.getModiSubstreamData().StreamData;
362 BinaryStreamReader Reader(ModiSubstreamData);
364 cantFail(Reader.readArray(ModuleDescriptors, ModiSubstreamData.getLength()));
365 auto Prev = ModuleDescriptors.begin();
366 assert(Prev.offset() == 0);
372 if (Current == ModuleDescriptors.end() || Offset < Current.offset())
377 DbiModuleDescriptor &Descriptor = *Prev;
378 P.formatLine("which contains the descriptor for module {0} ({1}).", Index,
379 Descriptor.getModuleName());
382 template <typename T>
383 static void dontExplain(LinePrinter &Printer, T &Stream, uint32_t Offset) {}
385 template <typename T, typename SubstreamRangeT>
386 static void explainSubstreamOffset(LinePrinter &P, uint32_t OffsetInStream,
388 const SubstreamRangeT &Substreams) {
389 uint32_t SubOffset = OffsetInStream;
390 for (const auto &Entry : Substreams) {
393 uint32_t S = static_cast<uint32_t>(Entry.Size);
395 P.formatLine("address is at offset {0}/{1} of the {2}.", SubOffset, S,
397 Entry.Explain(P, Stream, SubOffset);
404 void ExplainOutputStyle::explainStreamOffset(DbiStream &Dbi,
405 uint32_t OffsetInStream) {
406 P.printLine("Within the DBI stream:");
407 AutoIndent Indent(P);
408 const DbiStreamHeader *Header = Dbi.getHeader();
409 assert(Header != nullptr);
411 struct SubstreamInfo {
414 void (*Explain)(LinePrinter &, DbiStream &, uint32_t);
416 {sizeof(DbiStreamHeader), "DBI Stream Header", explainDbiHeaderOffset},
417 {int32_t(Header->ModiSubstreamSize), "Module Info Substream",
418 explainDbiModiSubstreamOffset},
419 {int32_t(Header->SecContrSubstreamSize), "Section Contribution Substream",
420 dontExplain<DbiStream>},
421 {int32_t(Header->SectionMapSize), "Section Map", dontExplain<DbiStream>},
422 {int32_t(Header->FileInfoSize), "File Info Substream",
423 dontExplain<DbiStream>},
424 {int32_t(Header->TypeServerSize), "Type Server Map Substream",
425 dontExplain<DbiStream>},
426 {int32_t(Header->ECSubstreamSize), "Edit & Continue Substream",
427 dontExplain<DbiStream>},
428 {int32_t(Header->OptionalDbgHdrSize), "Optional Debug Stream Array",
429 dontExplain<DbiStream>},
432 explainSubstreamOffset(P, OffsetInStream, Dbi, Substreams);
435 static void explainPdbStreamHeaderOffset(LinePrinter &P, InfoStream &Info,
437 const InfoStreamHeader *Header = Info.getHeader();
438 assert(Header != nullptr);
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));
452 void ExplainOutputStyle::explainStreamOffset(InfoStream &Info,
453 uint32_t OffsetInStream) {
454 P.printLine("Within the PDB stream:");
455 AutoIndent Indent(P);
457 struct SubstreamInfo {
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>}};
468 explainSubstreamOffset(P, OffsetInStream, Info, Substreams);