1 //===- ExplainOutputStyle.cpp --------------------------------- *- C++ --*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "ExplainOutputStyle.h"
11 #include "StreamUtil.h"
12 #include "llvm-pdbutil.h"
14 #include "llvm/DebugInfo/CodeView/Formatters.h"
15 #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
16 #include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17 #include "llvm/DebugInfo/PDB/Native/DbiStream.h"
18 #include "llvm/DebugInfo/PDB/Native/FormatUtil.h"
19 #include "llvm/DebugInfo/PDB/Native/InfoStream.h"
20 #include "llvm/DebugInfo/PDB/Native/InputFile.h"
21 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
22 #include "llvm/DebugInfo/PDB/Native/PDBFile.h"
23 #include "llvm/DebugInfo/PDB/Native/RawTypes.h"
24 #include "llvm/Object/COFF.h"
25 #include "llvm/Support/BinaryByteStream.h"
26 #include "llvm/Support/BinaryStreamArray.h"
27 #include "llvm/Support/Error.h"
30 using namespace llvm::codeview
;
31 using namespace llvm::msf
;
32 using namespace llvm::pdb
;
34 ExplainOutputStyle::ExplainOutputStyle(InputFile
&File
, uint64_t FileOffset
)
35 : File(File
), FileOffset(FileOffset
), P(2, false, outs(), opts::Filters
) {}
37 Error
ExplainOutputStyle::dump() {
38 P
.formatLine("Explaining file offset {0} of file '{1}'.", FileOffset
,
42 return explainPdbFile();
44 return explainBinaryFile();
47 Error
ExplainOutputStyle::explainPdbFile() {
48 bool IsAllocated
= explainPdbBlockStatus();
50 return Error::success();
53 if (isPdbSuperBlock())
54 explainPdbSuperBlockOffset();
55 else if (isPdbFpmBlock())
56 explainPdbFpmBlockOffset();
57 else if (isPdbBlockMapBlock())
58 explainPdbBlockMapOffset();
59 else if (isPdbStreamDirectoryBlock())
60 explainPdbStreamDirectoryOffset();
61 else if (auto Index
= getPdbBlockStreamIndex())
62 explainPdbStreamOffset(*Index
);
64 explainPdbUnknownBlock();
65 return Error::success();
68 Error
ExplainOutputStyle::explainBinaryFile() {
69 std::unique_ptr
<BinaryByteStream
> Stream
=
70 std::make_unique
<BinaryByteStream
>(File
.unknown().getBuffer(),
71 llvm::support::little
);
72 switch (opts::explain::InputType
) {
73 case opts::explain::InputFileType::DBIStream
: {
74 DbiStream
Dbi(std::move(Stream
));
75 if (auto EC
= Dbi
.reload(nullptr))
77 explainStreamOffset(Dbi
, FileOffset
);
80 case opts::explain::InputFileType::PDBStream
: {
81 InfoStream
Info(std::move(Stream
));
82 if (auto EC
= Info
.reload())
84 explainStreamOffset(Info
, FileOffset
);
88 llvm_unreachable("Invalid input file type!");
90 return Error::success();
93 uint32_t ExplainOutputStyle::pdbBlockIndex() const {
94 return FileOffset
/ File
.pdb().getBlockSize();
97 uint32_t ExplainOutputStyle::pdbBlockOffset() const {
98 uint64_t BlockStart
= pdbBlockIndex() * File
.pdb().getBlockSize();
99 assert(FileOffset
>= BlockStart
);
100 return FileOffset
- BlockStart
;
103 bool ExplainOutputStyle::isPdbSuperBlock() const {
104 return pdbBlockIndex() == 0;
107 bool ExplainOutputStyle::isPdbFpm1() const {
108 return ((pdbBlockIndex() - 1) % File
.pdb().getBlockSize() == 0);
110 bool ExplainOutputStyle::isPdbFpm2() const {
111 return ((pdbBlockIndex() - 2) % File
.pdb().getBlockSize() == 0);
114 bool ExplainOutputStyle::isPdbFpmBlock() const {
115 return isPdbFpm1() || isPdbFpm2();
118 bool ExplainOutputStyle::isPdbBlockMapBlock() const {
119 return pdbBlockIndex() == File
.pdb().getBlockMapIndex();
122 bool ExplainOutputStyle::isPdbStreamDirectoryBlock() const {
123 const auto &Layout
= File
.pdb().getMsfLayout();
124 return llvm::is_contained(Layout
.DirectoryBlocks
, pdbBlockIndex());
127 std::optional
<uint32_t> ExplainOutputStyle::getPdbBlockStreamIndex() const {
128 const auto &Layout
= File
.pdb().getMsfLayout();
129 for (const auto &Entry
: enumerate(Layout
.StreamMap
)) {
130 if (!llvm::is_contained(Entry
.value(), pdbBlockIndex()))
132 return Entry
.index();
137 bool ExplainOutputStyle::explainPdbBlockStatus() {
138 if (FileOffset
>= File
.pdb().getFileSize()) {
139 P
.formatLine("Address {0} is not in the file (file size = {1}).",
140 FileOffset
, File
.pdb().getFileSize());
143 P
.formatLine("Block:Offset = {2:X-}:{1:X-4}.", FileOffset
, pdbBlockOffset(),
146 bool IsFree
= File
.pdb().getMsfLayout().FreePageMap
[pdbBlockIndex()];
147 P
.formatLine("Address is in block {0} ({1}allocated).", pdbBlockIndex(),
152 #define endof(Class, Field) (offsetof(Class, Field) + sizeof(Class::Field))
154 void ExplainOutputStyle::explainPdbSuperBlockOffset() {
155 P
.formatLine("This corresponds to offset {0} of the MSF super block, ",
157 if (pdbBlockOffset() < endof(SuperBlock
, MagicBytes
))
158 P
.printLine("which is part of the MSF file magic.");
159 else if (pdbBlockOffset() < endof(SuperBlock
, BlockSize
)) {
160 P
.printLine("which contains the block size of the file.");
161 P
.formatLine("The current value is {0}.",
162 uint32_t(File
.pdb().getMsfLayout().SB
->BlockSize
));
163 } else if (pdbBlockOffset() < endof(SuperBlock
, FreeBlockMapBlock
)) {
164 P
.printLine("which contains the index of the FPM block (e.g. 1 or 2).");
165 P
.formatLine("The current value is {0}.",
166 uint32_t(File
.pdb().getMsfLayout().SB
->FreeBlockMapBlock
));
167 } else if (pdbBlockOffset() < endof(SuperBlock
, NumBlocks
)) {
168 P
.printLine("which contains the number of blocks in the file.");
169 P
.formatLine("The current value is {0}.",
170 uint32_t(File
.pdb().getMsfLayout().SB
->NumBlocks
));
171 } else if (pdbBlockOffset() < endof(SuperBlock
, NumDirectoryBytes
)) {
172 P
.printLine("which contains the number of bytes in the stream directory.");
173 P
.formatLine("The current value is {0}.",
174 uint32_t(File
.pdb().getMsfLayout().SB
->NumDirectoryBytes
));
175 } else if (pdbBlockOffset() < endof(SuperBlock
, Unknown1
)) {
176 P
.printLine("whose purpose is unknown.");
177 P
.formatLine("The current value is {0}.",
178 uint32_t(File
.pdb().getMsfLayout().SB
->Unknown1
));
179 } else if (pdbBlockOffset() < endof(SuperBlock
, BlockMapAddr
)) {
180 P
.printLine("which contains the file offset of the block map.");
181 P
.formatLine("The current value is {0}.",
182 uint32_t(File
.pdb().getMsfLayout().SB
->BlockMapAddr
));
184 assert(pdbBlockOffset() > sizeof(SuperBlock
));
186 "which is outside the range of valid data for the super block.");
190 static std::string
toBinaryString(uint8_t Byte
) {
191 char Result
[9] = {0};
192 for (int I
= 0; I
< 8; ++I
) {
193 char C
= (Byte
& 1) ? '1' : '0';
197 return std::string(Result
);
200 void ExplainOutputStyle::explainPdbFpmBlockOffset() {
201 const MSFLayout
&Layout
= File
.pdb().getMsfLayout();
202 uint32_t MainFpm
= Layout
.mainFpmBlock();
203 uint32_t AltFpm
= Layout
.alternateFpmBlock();
205 assert(isPdbFpmBlock());
206 uint32_t Fpm
= isPdbFpm1() ? 1 : 2;
207 uint32_t FpmChunk
= pdbBlockIndex() / File
.pdb().getBlockSize();
208 assert((Fpm
== MainFpm
) || (Fpm
== AltFpm
));
210 bool IsMain
= (Fpm
== MainFpm
);
211 P
.formatLine("Address is in FPM{0} ({1} FPM)", Fpm
, IsMain
? "Main" : "Alt");
212 uint32_t DescribedBlockStart
=
213 8 * (FpmChunk
* File
.pdb().getBlockSize() + pdbBlockOffset());
214 if (DescribedBlockStart
> File
.pdb().getBlockCount()) {
215 P
.printLine("Address is in extraneous FPM space.");
219 P
.formatLine("Address describes the allocation status of blocks [{0},{1})",
220 DescribedBlockStart
, DescribedBlockStart
+ 8);
221 ArrayRef
<uint8_t> Bytes
;
222 cantFail(File
.pdb().getMsfBuffer().readBytes(FileOffset
, 1, Bytes
));
223 P
.formatLine("Status = {0} (Note: 0 = allocated, 1 = free)",
224 toBinaryString(Bytes
[0]));
227 void ExplainOutputStyle::explainPdbBlockMapOffset() {
228 uint64_t BlockMapOffset
= File
.pdb().getBlockMapOffset();
229 uint32_t OffsetInBlock
= FileOffset
- BlockMapOffset
;
230 P
.formatLine("Address is at offset {0} of the directory block list",
234 static uint32_t getOffsetInStream(ArrayRef
<support::ulittle32_t
> StreamBlocks
,
235 uint64_t FileOffset
, uint32_t BlockSize
) {
236 uint32_t BlockIndex
= FileOffset
/ BlockSize
;
237 uint32_t OffsetInBlock
= FileOffset
- BlockIndex
* BlockSize
;
239 auto Iter
= llvm::find(StreamBlocks
, BlockIndex
);
240 assert(Iter
!= StreamBlocks
.end());
241 uint32_t StreamBlockIndex
= std::distance(StreamBlocks
.begin(), Iter
);
242 return StreamBlockIndex
* BlockSize
+ OffsetInBlock
;
245 void ExplainOutputStyle::explainPdbStreamOffset(uint32_t Stream
) {
246 SmallVector
<StreamInfo
, 12> Streams
;
247 discoverStreamPurposes(File
.pdb(), Streams
);
249 assert(Stream
<= Streams
.size());
250 const StreamInfo
&S
= Streams
[Stream
];
251 const auto &Layout
= File
.pdb().getStreamLayout(Stream
);
253 getOffsetInStream(Layout
.Blocks
, FileOffset
, File
.pdb().getBlockSize());
254 P
.formatLine("Address is at offset {0}/{1} of Stream {2} ({3}){4}.",
255 StreamOff
, Layout
.Length
, Stream
, S
.getLongName(),
256 (StreamOff
> Layout
.Length
) ? " in unused space" : "");
257 switch (S
.getPurpose()) {
258 case StreamPurpose::DBI
: {
259 DbiStream
&Dbi
= cantFail(File
.pdb().getPDBDbiStream());
260 explainStreamOffset(Dbi
, StreamOff
);
263 case StreamPurpose::PDB
: {
264 InfoStream
&Info
= cantFail(File
.pdb().getPDBInfoStream());
265 explainStreamOffset(Info
, StreamOff
);
268 case StreamPurpose::IPI
:
269 case StreamPurpose::TPI
:
270 case StreamPurpose::ModuleStream
:
271 case StreamPurpose::NamedStream
:
277 void ExplainOutputStyle::explainPdbStreamDirectoryOffset() {
278 auto DirectoryBlocks
= File
.pdb().getDirectoryBlockArray();
279 const auto &Layout
= File
.pdb().getMsfLayout();
281 getOffsetInStream(DirectoryBlocks
, FileOffset
, File
.pdb().getBlockSize());
282 P
.formatLine("Address is at offset {0}/{1} of Stream Directory{2}.",
283 StreamOff
, uint32_t(Layout
.SB
->NumDirectoryBytes
),
284 uint32_t(StreamOff
> Layout
.SB
->NumDirectoryBytes
)
289 void ExplainOutputStyle::explainPdbUnknownBlock() {
290 P
.formatLine("Address has unknown purpose.");
293 template <typename T
>
294 static void printStructField(LinePrinter
&P
, StringRef Label
, T Value
) {
295 P
.formatLine("which contains {0}.", Label
);
296 P
.formatLine("The current value is {0}.", Value
);
299 static void explainDbiHeaderOffset(LinePrinter
&P
, DbiStream
&Dbi
,
301 const DbiStreamHeader
*Header
= Dbi
.getHeader();
302 assert(Header
!= nullptr);
304 if (Offset
< endof(DbiStreamHeader
, VersionSignature
))
305 printStructField(P
, "the DBI Stream Version Signature",
306 int32_t(Header
->VersionSignature
));
307 else if (Offset
< endof(DbiStreamHeader
, VersionHeader
))
308 printStructField(P
, "the DBI Stream Version Header",
309 uint32_t(Header
->VersionHeader
));
310 else if (Offset
< endof(DbiStreamHeader
, Age
))
311 printStructField(P
, "the age of the DBI Stream", uint32_t(Header
->Age
));
312 else if (Offset
< endof(DbiStreamHeader
, GlobalSymbolStreamIndex
))
313 printStructField(P
, "the index of the Global Symbol Stream",
314 uint16_t(Header
->GlobalSymbolStreamIndex
));
315 else if (Offset
< endof(DbiStreamHeader
, BuildNumber
))
316 printStructField(P
, "the build number", uint16_t(Header
->BuildNumber
));
317 else if (Offset
< endof(DbiStreamHeader
, PublicSymbolStreamIndex
))
318 printStructField(P
, "the index of the Public Symbol Stream",
319 uint16_t(Header
->PublicSymbolStreamIndex
));
320 else if (Offset
< endof(DbiStreamHeader
, PdbDllVersion
))
321 printStructField(P
, "the version of mspdb.dll",
322 uint16_t(Header
->PdbDllVersion
));
323 else if (Offset
< endof(DbiStreamHeader
, SymRecordStreamIndex
))
324 printStructField(P
, "the index of the Symbol Record Stream",
325 uint16_t(Header
->SymRecordStreamIndex
));
326 else if (Offset
< endof(DbiStreamHeader
, PdbDllRbld
))
327 printStructField(P
, "the rbld of mspdb.dll", uint16_t(Header
->PdbDllRbld
));
328 else if (Offset
< endof(DbiStreamHeader
, ModiSubstreamSize
))
329 printStructField(P
, "the size of the Module Info Substream",
330 int32_t(Header
->ModiSubstreamSize
));
331 else if (Offset
< endof(DbiStreamHeader
, SecContrSubstreamSize
))
332 printStructField(P
, "the size of the Section Contribution Substream",
333 int32_t(Header
->SecContrSubstreamSize
));
334 else if (Offset
< endof(DbiStreamHeader
, SectionMapSize
))
335 printStructField(P
, "the size of the Section Map Substream",
336 int32_t(Header
->SectionMapSize
));
337 else if (Offset
< endof(DbiStreamHeader
, FileInfoSize
))
338 printStructField(P
, "the size of the File Info Substream",
339 int32_t(Header
->FileInfoSize
));
340 else if (Offset
< endof(DbiStreamHeader
, TypeServerSize
))
341 printStructField(P
, "the size of the Type Server Map",
342 int32_t(Header
->TypeServerSize
));
343 else if (Offset
< endof(DbiStreamHeader
, MFCTypeServerIndex
))
344 printStructField(P
, "the index of the MFC Type Server stream",
345 uint32_t(Header
->MFCTypeServerIndex
));
346 else if (Offset
< endof(DbiStreamHeader
, OptionalDbgHdrSize
))
347 printStructField(P
, "the size of the Optional Debug Stream array",
348 int32_t(Header
->OptionalDbgHdrSize
));
349 else if (Offset
< endof(DbiStreamHeader
, ECSubstreamSize
))
350 printStructField(P
, "the size of the Edit & Continue Substream",
351 int32_t(Header
->ECSubstreamSize
));
352 else if (Offset
< endof(DbiStreamHeader
, Flags
))
353 printStructField(P
, "the DBI Stream flags", uint16_t(Header
->Flags
));
354 else if (Offset
< endof(DbiStreamHeader
, MachineType
))
355 printStructField(P
, "the machine type", uint16_t(Header
->MachineType
));
356 else if (Offset
< endof(DbiStreamHeader
, Reserved
))
357 printStructField(P
, "reserved data", uint32_t(Header
->Reserved
));
360 static void explainDbiModiSubstreamOffset(LinePrinter
&P
, DbiStream
&Dbi
,
362 VarStreamArray
<DbiModuleDescriptor
> ModuleDescriptors
;
363 BinaryStreamRef ModiSubstreamData
= Dbi
.getModiSubstreamData().StreamData
;
364 BinaryStreamReader
Reader(ModiSubstreamData
);
366 cantFail(Reader
.readArray(ModuleDescriptors
, ModiSubstreamData
.getLength()));
367 auto Prev
= ModuleDescriptors
.begin();
368 assert(Prev
.offset() == 0);
374 if (Current
== ModuleDescriptors
.end() || Offset
< Current
.offset())
379 const DbiModuleDescriptor
&Descriptor
= *Prev
;
380 P
.formatLine("which contains the descriptor for module {0} ({1}).", Index
,
381 Descriptor
.getModuleName());
384 template <typename T
>
385 static void dontExplain(LinePrinter
&Printer
, T
&Stream
, uint32_t Offset
) {}
387 template <typename T
, typename SubstreamRangeT
>
388 static void explainSubstreamOffset(LinePrinter
&P
, uint32_t OffsetInStream
,
390 const SubstreamRangeT
&Substreams
) {
391 uint32_t SubOffset
= OffsetInStream
;
392 for (const auto &Entry
: Substreams
) {
395 uint32_t S
= static_cast<uint32_t>(Entry
.Size
);
397 P
.formatLine("address is at offset {0}/{1} of the {2}.", SubOffset
, S
,
399 Entry
.Explain(P
, Stream
, SubOffset
);
406 void ExplainOutputStyle::explainStreamOffset(DbiStream
&Dbi
,
407 uint32_t OffsetInStream
) {
408 P
.printLine("Within the DBI stream:");
409 AutoIndent
Indent(P
);
410 const DbiStreamHeader
*Header
= Dbi
.getHeader();
411 assert(Header
!= nullptr);
413 struct SubstreamInfo
{
416 void (*Explain
)(LinePrinter
&, DbiStream
&, uint32_t);
418 {sizeof(DbiStreamHeader
), "DBI Stream Header", explainDbiHeaderOffset
},
419 {int32_t(Header
->ModiSubstreamSize
), "Module Info Substream",
420 explainDbiModiSubstreamOffset
},
421 {int32_t(Header
->SecContrSubstreamSize
), "Section Contribution Substream",
422 dontExplain
<DbiStream
>},
423 {int32_t(Header
->SectionMapSize
), "Section Map", dontExplain
<DbiStream
>},
424 {int32_t(Header
->FileInfoSize
), "File Info Substream",
425 dontExplain
<DbiStream
>},
426 {int32_t(Header
->TypeServerSize
), "Type Server Map Substream",
427 dontExplain
<DbiStream
>},
428 {int32_t(Header
->ECSubstreamSize
), "Edit & Continue Substream",
429 dontExplain
<DbiStream
>},
430 {int32_t(Header
->OptionalDbgHdrSize
), "Optional Debug Stream Array",
431 dontExplain
<DbiStream
>},
434 explainSubstreamOffset(P
, OffsetInStream
, Dbi
, Substreams
);
437 static void explainPdbStreamHeaderOffset(LinePrinter
&P
, InfoStream
&Info
,
439 const InfoStreamHeader
*Header
= Info
.getHeader();
440 assert(Header
!= nullptr);
442 if (Offset
< endof(InfoStreamHeader
, Version
))
443 printStructField(P
, "the PDB Stream Version Signature",
444 uint32_t(Header
->Version
));
445 else if (Offset
< endof(InfoStreamHeader
, Signature
))
446 printStructField(P
, "the signature of the PDB Stream",
447 uint32_t(Header
->Signature
));
448 else if (Offset
< endof(InfoStreamHeader
, Age
))
449 printStructField(P
, "the age of the PDB", uint32_t(Header
->Age
));
450 else if (Offset
< endof(InfoStreamHeader
, Guid
))
451 printStructField(P
, "the guid of the PDB", fmt_guid(Header
->Guid
.Guid
));
454 void ExplainOutputStyle::explainStreamOffset(InfoStream
&Info
,
455 uint32_t OffsetInStream
) {
456 P
.printLine("Within the PDB stream:");
457 AutoIndent
Indent(P
);
459 struct SubstreamInfo
{
462 void (*Explain
)(LinePrinter
&, InfoStream
&, uint32_t);
463 } Substreams
[] = {{sizeof(InfoStreamHeader
), "PDB Stream Header",
464 explainPdbStreamHeaderOffset
},
465 {Info
.getNamedStreamMapByteSize(), "Named Stream Map",
466 dontExplain
<InfoStream
>},
467 {Info
.getStreamSize(), "PDB Feature Signatures",
468 dontExplain
<InfoStream
>}};
470 explainSubstreamOffset(P
, OffsetInStream
, Info
, Substreams
);