blob: c4fbde22e755277bd2a70d3874e1b6d89fb49277 [file] [log] [blame]
Zachary Turnerea40f402018-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"
13#include "StreamUtil.h"
14#include "llvm-pdbutil.h"
15
16#include "llvm/DebugInfo/MSF/MappedBlockStream.h"
17#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
18
19using namespace llvm;
20using namespace llvm::codeview;
21using namespace llvm::msf;
22using namespace llvm::pdb;
23
24ExplainOutputStyle::ExplainOutputStyle(PDBFile &File, uint64_t FileOffset)
25 : File(File), FileOffset(FileOffset),
26 BlockIndex(FileOffset / File.getBlockSize()),
27 OffsetInBlock(FileOffset - BlockIndex * File.getBlockSize()),
28 P(2, false, outs()) {}
29
30Error ExplainOutputStyle::dump() {
31 P.formatLine("Explaining file offset {0} of file '{1}'.", FileOffset,
32 File.getFilePath());
33
34 bool IsAllocated = explainBlockStatus();
35 if (!IsAllocated)
36 return Error::success();
37
38 AutoIndent Indent(P);
39 if (isSuperBlock())
40 explainSuperBlockOffset();
41 else if (isFpmBlock())
42 explainFpmBlockOffset();
43 else if (isBlockMapBlock())
44 explainBlockMapOffset();
45 else if (isStreamDirectoryBlock())
46 explainStreamDirectoryOffset();
47 else if (auto Index = getBlockStreamIndex())
48 explainStreamOffset(*Index);
49 else
50 explainUnknownBlock();
51 return Error::success();
52}
53
54bool ExplainOutputStyle::isSuperBlock() const { return BlockIndex == 0; }
55
56bool ExplainOutputStyle::isFpm1() const {
57 return ((BlockIndex - 1) % File.getBlockSize() == 0);
58}
59bool ExplainOutputStyle::isFpm2() const {
60 return ((BlockIndex - 2) % File.getBlockSize() == 0);
61}
62
63bool ExplainOutputStyle::isFpmBlock() const { return isFpm1() || isFpm2(); }
64
65bool ExplainOutputStyle::isBlockMapBlock() const {
66 return BlockIndex == File.getBlockMapIndex();
67}
68
69bool ExplainOutputStyle::isStreamDirectoryBlock() const {
70 const auto &Layout = File.getMsfLayout();
71 return llvm::is_contained(Layout.DirectoryBlocks, BlockIndex);
72}
73
74Optional<uint32_t> ExplainOutputStyle::getBlockStreamIndex() const {
75 const auto &Layout = File.getMsfLayout();
76 for (const auto &Entry : enumerate(Layout.StreamMap)) {
77 if (!llvm::is_contained(Entry.value(), BlockIndex))
78 continue;
79 return Entry.index();
80 }
81 return None;
82}
83
84bool ExplainOutputStyle::explainBlockStatus() {
85 if (FileOffset >= File.getFileSize()) {
86 P.formatLine("Address {0} is not in the file (file size = {1}).",
87 FileOffset, File.getFileSize());
88 return false;
89 }
90 P.formatLine("Block:Offset = {2:X-}:{1:X-4}.", FileOffset, OffsetInBlock,
91 BlockIndex);
92
93 bool IsFree = File.getMsfLayout().FreePageMap[BlockIndex];
94 P.formatLine("Address is in block {0} ({1}allocated).", BlockIndex,
95 IsFree ? "un" : "");
96 return !IsFree;
97}
98
Zachary Turner1b204162018-03-29 17:11:14 +000099#define endof(Class, Field) (offsetof(Class, Field) + sizeof(Class::Field))
100
Zachary Turnerea40f402018-03-29 16:28:20 +0000101void ExplainOutputStyle::explainSuperBlockOffset() {
Zachary Turner1b204162018-03-29 17:11:14 +0000102 P.formatLine("This corresponds to offset {0} of the MSF super block, ",
Zachary Turnerea40f402018-03-29 16:28:20 +0000103 OffsetInBlock);
Zachary Turner1b204162018-03-29 17:11:14 +0000104 if (OffsetInBlock < endof(SuperBlock, MagicBytes))
Zachary Turnerea40f402018-03-29 16:28:20 +0000105 P.printLine("which is part of the MSF file magic.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000106 else if (OffsetInBlock < endof(SuperBlock, BlockSize)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000107 P.printLine("which contains the block size of the file.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000108 P.formatLine("The current value is {0}.",
109 uint32_t(File.getMsfLayout().SB->BlockSize));
110 } else if (OffsetInBlock < endof(SuperBlock, FreeBlockMapBlock)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000111 P.printLine("which contains the index of the FPM block (e.g. 1 or 2).");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000112 P.formatLine("The current value is {0}.",
113 uint32_t(File.getMsfLayout().SB->FreeBlockMapBlock));
114 } else if (OffsetInBlock < endof(SuperBlock, NumBlocks)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000115 P.printLine("which contains the number of blocks in the file.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000116 P.formatLine("The current value is {0}.",
117 uint32_t(File.getMsfLayout().SB->NumBlocks));
118 } else if (OffsetInBlock < endof(SuperBlock, NumDirectoryBytes)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000119 P.printLine("which contains the number of bytes in the stream directory.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000120 P.formatLine("The current value is {0}.",
121 uint32_t(File.getMsfLayout().SB->NumDirectoryBytes));
122 } else if (OffsetInBlock < endof(SuperBlock, Unknown1)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000123 P.printLine("whose purpose is unknown.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000124 P.formatLine("The current value is {0}.",
125 uint32_t(File.getMsfLayout().SB->Unknown1));
126 } else if (OffsetInBlock < endof(SuperBlock, BlockMapAddr)) {
Zachary Turnerea40f402018-03-29 16:28:20 +0000127 P.printLine("which contains the file offset of the block map.");
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000128 P.formatLine("The current value is {0}.",
129 uint32_t(File.getMsfLayout().SB->BlockMapAddr));
130 } else {
Zachary Turnerea40f402018-03-29 16:28:20 +0000131 assert(OffsetInBlock > sizeof(SuperBlock));
132 P.printLine(
133 "which is outside the range of valid data for the super block.");
134 }
135}
136
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000137static std::string toBinaryString(uint8_t Byte) {
138 char Result[9] = {0};
139 for (int I = 0; I < 8; ++I) {
140 char C = (Byte & 1) ? '1' : '0';
141 Result[I] = C;
142 Byte >>= 1;
143 }
144 return std::string(Result);
145}
146
Zachary Turnerea40f402018-03-29 16:28:20 +0000147void ExplainOutputStyle::explainFpmBlockOffset() {
148 const MSFLayout &Layout = File.getMsfLayout();
149 uint32_t MainFpm = Layout.mainFpmBlock();
150 uint32_t AltFpm = Layout.alternateFpmBlock();
151
152 assert(isFpmBlock());
153 uint32_t Fpm = isFpm1() ? 1 : 2;
154 uint32_t FpmChunk = BlockIndex / File.getBlockSize();
155 assert((Fpm == MainFpm) || (Fpm == AltFpm));
156 (void)AltFpm;
157 bool IsMain = (Fpm == MainFpm);
158 P.formatLine("Address is in FPM{0} ({1} FPM)", Fpm, IsMain ? "Main" : "Alt");
159 uint32_t DescribedBlockStart =
160 8 * (FpmChunk * File.getBlockSize() + OffsetInBlock);
161 if (DescribedBlockStart > File.getBlockCount()) {
162 P.printLine("Address is in extraneous FPM space.");
163 return;
164 }
165
166 P.formatLine("Address describes the allocation status of blocks [{0},{1})",
167 DescribedBlockStart, DescribedBlockStart + 8);
Zachary Turnerf4b6dcf2018-03-29 17:45:34 +0000168 ArrayRef<uint8_t> Bytes;
169 cantFail(File.getMsfBuffer().readBytes(FileOffset, 1, Bytes));
170 P.formatLine("Status = {0} (Note: 0 = allocated, 1 = free)",
171 toBinaryString(Bytes[0]));
Zachary Turnerea40f402018-03-29 16:28:20 +0000172}
173
Zachary Turnerea40f402018-03-29 16:28:20 +0000174void ExplainOutputStyle::explainBlockMapOffset() {
Zachary Turnerea40f402018-03-29 16:28:20 +0000175 uint64_t BlockMapOffset = File.getBlockMapOffset();
176 uint32_t OffsetInBlock = FileOffset - BlockMapOffset;
177 P.formatLine("Address is at offset {0} of the directory block list",
178 OffsetInBlock);
179}
180
181static uint32_t getOffsetInStream(ArrayRef<support::ulittle32_t> StreamBlocks,
182 uint64_t FileOffset, uint32_t BlockSize) {
183 uint32_t BlockIndex = FileOffset / BlockSize;
184 uint32_t OffsetInBlock = FileOffset - BlockIndex * BlockSize;
185
186 auto Iter = llvm::find(StreamBlocks, BlockIndex);
187 assert(Iter != StreamBlocks.end());
188 uint32_t StreamBlockIndex = std::distance(StreamBlocks.begin(), Iter);
189 return StreamBlockIndex * BlockSize + OffsetInBlock;
190}
191
192void ExplainOutputStyle::explainStreamOffset(uint32_t Stream) {
193 SmallVector<StreamInfo, 12> Streams;
194 discoverStreamPurposes(File, Streams);
195
196 assert(Stream <= Streams.size());
197 const StreamInfo &S = Streams[Stream];
198 const auto &Layout = File.getStreamLayout(Stream);
199 uint32_t StreamOff =
200 getOffsetInStream(Layout.Blocks, FileOffset, File.getBlockSize());
201 P.formatLine("Address is at offset {0}/{1} of Stream {2} ({3}){4}.",
202 StreamOff, Layout.Length, Stream, S.getLongName(),
203 (StreamOff > Layout.Length) ? " in unused space" : "");
204}
205
206void ExplainOutputStyle::explainStreamDirectoryOffset() {
207 auto DirectoryBlocks = File.getDirectoryBlockArray();
208 const auto &Layout = File.getMsfLayout();
209 uint32_t StreamOff =
210 getOffsetInStream(DirectoryBlocks, FileOffset, File.getBlockSize());
211 P.formatLine("Address is at offset {0}/{1} of Stream Directory{2}.",
212 StreamOff, uint32_t(Layout.SB->NumDirectoryBytes),
213 uint32_t(StreamOff > Layout.SB->NumDirectoryBytes)
214 ? " in unused space"
215 : "");
216}
217
218void ExplainOutputStyle::explainUnknownBlock() {
219 P.formatLine("Address has unknown purpose.");
220}