• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- 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"
13 #include "InputFile.h"
14 #include "StreamUtil.h"
15 #include "llvm-pdbutil.h"
16 
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"
26 
27 using namespace llvm;
28 using namespace llvm::codeview;
29 using namespace llvm::msf;
30 using namespace llvm::pdb;
31 
ExplainOutputStyle(InputFile & File,uint64_t FileOffset)32 ExplainOutputStyle::ExplainOutputStyle(InputFile &File, uint64_t FileOffset)
33     : File(File), FileOffset(FileOffset), P(2, false, outs()) {}
34 
dump()35 Error ExplainOutputStyle::dump() {
36   P.formatLine("Explaining file offset {0} of file '{1}'.", FileOffset,
37                File.getFilePath());
38 
39   if (File.isPdb())
40     return explainPdbFile();
41 
42   return explainBinaryFile();
43 }
44 
explainPdbFile()45 Error ExplainOutputStyle::explainPdbFile() {
46   bool IsAllocated = explainPdbBlockStatus();
47   if (!IsAllocated)
48     return Error::success();
49 
50   AutoIndent Indent(P);
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);
61   else
62     explainPdbUnknownBlock();
63   return Error::success();
64 }
65 
explainBinaryFile()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))
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();
89 }
90 
pdbBlockIndex() const91 uint32_t ExplainOutputStyle::pdbBlockIndex() const {
92   return FileOffset / File.pdb().getBlockSize();
93 }
94 
pdbBlockOffset() const95 uint32_t ExplainOutputStyle::pdbBlockOffset() const {
96   uint64_t BlockStart = pdbBlockIndex() * File.pdb().getBlockSize();
97   assert(FileOffset >= BlockStart);
98   return FileOffset - BlockStart;
99 }
100 
isPdbSuperBlock() const101 bool ExplainOutputStyle::isPdbSuperBlock() const {
102   return pdbBlockIndex() == 0;
103 }
104 
isPdbFpm1() const105 bool ExplainOutputStyle::isPdbFpm1() const {
106   return ((pdbBlockIndex() - 1) % File.pdb().getBlockSize() == 0);
107 }
isPdbFpm2() const108 bool ExplainOutputStyle::isPdbFpm2() const {
109   return ((pdbBlockIndex() - 2) % File.pdb().getBlockSize() == 0);
110 }
111 
isPdbFpmBlock() const112 bool ExplainOutputStyle::isPdbFpmBlock() const {
113   return isPdbFpm1() || isPdbFpm2();
114 }
115 
isPdbBlockMapBlock() const116 bool ExplainOutputStyle::isPdbBlockMapBlock() const {
117   return pdbBlockIndex() == File.pdb().getBlockMapIndex();
118 }
119 
isPdbStreamDirectoryBlock() const120 bool ExplainOutputStyle::isPdbStreamDirectoryBlock() const {
121   const auto &Layout = File.pdb().getMsfLayout();
122   return llvm::is_contained(Layout.DirectoryBlocks, pdbBlockIndex());
123 }
124 
getPdbBlockStreamIndex() const125 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()))
129       continue;
130     return Entry.index();
131   }
132   return None;
133 }
134 
explainPdbBlockStatus()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());
139     return false;
140   }
141   P.formatLine("Block:Offset = {2:X-}:{1:X-4}.", FileOffset, pdbBlockOffset(),
142                pdbBlockIndex());
143 
144   bool IsFree = File.pdb().getMsfLayout().FreePageMap[pdbBlockIndex()];
145   P.formatLine("Address is in block {0} ({1}allocated).", pdbBlockIndex(),
146                IsFree ? "un" : "");
147   return !IsFree;
148 }
149 
150 #define endof(Class, Field) (offsetof(Class, Field) + sizeof(Class::Field))
151 
explainPdbSuperBlockOffset()152 void ExplainOutputStyle::explainPdbSuperBlockOffset() {
153   P.formatLine("This corresponds to offset {0} of the MSF super block, ",
154                pdbBlockOffset());
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));
181   } else {
182     assert(pdbBlockOffset() > sizeof(SuperBlock));
183     P.printLine(
184         "which is outside the range of valid data for the super block.");
185   }
186 }
187 
toBinaryString(uint8_t Byte)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';
192     Result[I] = C;
193     Byte >>= 1;
194   }
195   return std::string(Result);
196 }
197 
explainPdbFpmBlockOffset()198 void ExplainOutputStyle::explainPdbFpmBlockOffset() {
199   const MSFLayout &Layout = File.pdb().getMsfLayout();
200   uint32_t MainFpm = Layout.mainFpmBlock();
201   uint32_t AltFpm = Layout.alternateFpmBlock();
202 
203   assert(isPdbFpmBlock());
204   uint32_t Fpm = isPdbFpm1() ? 1 : 2;
205   uint32_t FpmChunk = pdbBlockIndex() / File.pdb().getBlockSize();
206   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 =
211       8 * (FpmChunk * File.pdb().getBlockSize() + pdbBlockOffset());
212   if (DescribedBlockStart > File.pdb().getBlockCount()) {
213     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);
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]));
223 }
224 
explainPdbBlockMapOffset()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",
229                OffsetInBlock);
230 }
231 
getOffsetInStream(ArrayRef<support::ulittle32_t> StreamBlocks,uint64_t FileOffset,uint32_t BlockSize)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;
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 
explainPdbStreamOffset(uint32_t Stream)243 void ExplainOutputStyle::explainPdbStreamOffset(uint32_t Stream) {
244   SmallVector<StreamInfo, 12> Streams;
245   discoverStreamPurposes(File.pdb(), Streams);
246 
247   assert(Stream <= Streams.size());
248   const StreamInfo &S = Streams[Stream];
249   const auto &Layout = File.pdb().getStreamLayout(Stream);
250   uint32_t StreamOff =
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);
259     break;
260   }
261   case StreamPurpose::PDB: {
262     InfoStream &Info = cantFail(File.pdb().getPDBInfoStream());
263     explainStreamOffset(Info, StreamOff);
264     break;
265   }
266   case StreamPurpose::IPI:
267   case StreamPurpose::TPI:
268   case StreamPurpose::ModuleStream:
269   case StreamPurpose::NamedStream:
270   default:
271     break;
272   }
273 }
274 
explainPdbStreamDirectoryOffset()275 void ExplainOutputStyle::explainPdbStreamDirectoryOffset() {
276   auto DirectoryBlocks = File.pdb().getDirectoryBlockArray();
277   const auto &Layout = File.pdb().getMsfLayout();
278   uint32_t StreamOff =
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)
283                    ? " in unused space"
284                    : "");
285 }
286 
explainPdbUnknownBlock()287 void ExplainOutputStyle::explainPdbUnknownBlock() {
288   P.formatLine("Address has unknown purpose.");
289 }
290 
291 template <typename T>
printStructField(LinePrinter & P,StringRef Label,T Value)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);
295 }
296 
explainDbiHeaderOffset(LinePrinter & P,DbiStream & Dbi,uint32_t Offset)297 static 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 
explainDbiModiSubstreamOffset(LinePrinter & P,DbiStream & Dbi,uint32_t Offset)358 static 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 
382 template <typename T>
dontExplain(LinePrinter & Printer,T & Stream,uint32_t Offset)383 static void dontExplain(LinePrinter &Printer, T &Stream, uint32_t Offset) {}
384 
385 template <typename T, typename SubstreamRangeT>
explainSubstreamOffset(LinePrinter & P,uint32_t OffsetInStream,T & Stream,const SubstreamRangeT & Substreams)386 static 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) {
391     if (Entry.Size <= 0)
392       continue;
393     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);
397       Entry.Explain(P, Stream, SubOffset);
398       return;
399     }
400     SubOffset -= S;
401   }
402 }
403 
explainStreamOffset(DbiStream & Dbi,uint32_t OffsetInStream)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);
410 
411   struct SubstreamInfo {
412     int32_t Size;
413     StringRef Label;
414     void (*Explain)(LinePrinter &, DbiStream &, uint32_t);
415   } Substreams[] = {
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>},
430   };
431 
432   explainSubstreamOffset(P, OffsetInStream, Dbi, Substreams);
433 }
434 
explainPdbStreamHeaderOffset(LinePrinter & P,InfoStream & Info,uint32_t Offset)435 static 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 
explainStreamOffset(InfoStream & Info,uint32_t OffsetInStream)452 void ExplainOutputStyle::explainStreamOffset(InfoStream &Info,
453                                              uint32_t OffsetInStream) {
454   P.printLine("Within the PDB stream:");
455   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 }
470