blob: 36f8ead090ae9e866573b6f2b4118aa3513b5420 [file] [log] [blame]
Zachary Turner6ba65de2016-04-29 17:22:58 +00001//===- MappedBlockStream.cpp - Reads stream data from a PDBFile -----------===//
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 "llvm/DebugInfo/PDB/Raw/MappedBlockStream.h"
Zachary Turnera1657a92016-06-08 17:26:39 +000011#include "llvm/DebugInfo/PDB/Raw/DirectoryStreamData.h"
Zachary Turnerd8447992016-06-07 05:28:55 +000012#include "llvm/DebugInfo/PDB/Raw/IPDBStreamData.h"
Zachary Turnera1657a92016-06-08 17:26:39 +000013#include "llvm/DebugInfo/PDB/Raw/IndexedStreamData.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000014#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
Zachary Turner819e77d2016-05-06 20:51:57 +000015#include "llvm/DebugInfo/PDB/Raw/RawError.h"
Zachary Turner6ba65de2016-04-29 17:22:58 +000016
17using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000018using namespace llvm::pdb;
Zachary Turner6ba65de2016-04-29 17:22:58 +000019
Zachary Turnera1657a92016-06-08 17:26:39 +000020namespace {
21// This exists so that we can use make_unique while still keeping the
22// constructor of MappedBlockStream private, forcing users to go through
23// the `create` interface.
24class MappedBlockStreamImpl : public MappedBlockStream {
25public:
26 MappedBlockStreamImpl(std::unique_ptr<IPDBStreamData> Data,
27 const IPDBFile &File)
28 : MappedBlockStream(std::move(Data), File) {}
29};
30}
31
Zachary Turner5acb4ac2016-06-10 05:09:12 +000032typedef std::pair<uint32_t, uint32_t> Interval;
33static Interval intersect(const Interval &I1, const Interval &I2) {
34 return std::make_pair(std::max(I1.first, I2.first),
35 std::min(I1.second, I2.second));
36}
37
Zachary Turnerd8447992016-06-07 05:28:55 +000038MappedBlockStream::MappedBlockStream(std::unique_ptr<IPDBStreamData> Data,
39 const IPDBFile &Pdb)
40 : Pdb(Pdb), Data(std::move(Data)) {}
Zachary Turner6ba65de2016-04-29 17:22:58 +000041
Zachary Turner8dbe3622016-05-27 01:54:44 +000042Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size,
43 ArrayRef<uint8_t> &Buffer) const {
44 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd8447992016-06-07 05:28:55 +000045 if (Size > Data->getLength())
Zachary Turner8dbe3622016-05-27 01:54:44 +000046 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +000047 if (Offset > Data->getLength() - Size)
Zachary Turner8dbe3622016-05-27 01:54:44 +000048 return make_error<RawError>(raw_error_code::insufficient_buffer);
49
50 if (tryReadContiguously(Offset, Size, Buffer))
51 return Error::success();
52
53 auto CacheIter = CacheMap.find(Offset);
54 if (CacheIter != CacheMap.end()) {
Zachary Turner5acb4ac2016-06-10 05:09:12 +000055 // Try to find an alloc that was large enough for this request.
56 for (auto &Entry : CacheIter->second) {
57 if (Entry.size() >= Size) {
58 Buffer = Entry.slice(0, Size);
59 return Error::success();
60 }
61 }
62 }
63
64 // We couldn't find a buffer that started at the correct offset (the most
65 // common scenario). Try to see if there is a buffer that starts at some
66 // other offset but overlaps the desired range.
67 for (auto &CacheItem : CacheMap) {
68 Interval RequestExtent = std::make_pair(Offset, Offset + Size);
69
70 // We already checked this one on the fast path above.
71 if (CacheItem.first == Offset)
72 continue;
73 // If the initial extent of the cached item is beyond the ending extent
74 // of the request, there is no overlap.
75 if (CacheItem.first >= Offset + Size)
76 continue;
77
78 // We really only have to check the last item in the list, since we append
79 // in order of increasing length.
80 if (CacheItem.second.empty())
81 continue;
82
83 auto CachedAlloc = CacheItem.second.back();
84 // If the initial extent of the request is beyond the ending extent of
85 // the cached item, there is no overlap.
86 Interval CachedExtent =
87 std::make_pair(CacheItem.first, CacheItem.first + CachedAlloc.size());
88 if (RequestExtent.first >= CachedExtent.first + CachedExtent.second)
89 continue;
90
91 Interval Intersection = intersect(CachedExtent, RequestExtent);
92 // Only use this if the entire request extent is contained in the cached
93 // extent.
94 if (Intersection != RequestExtent)
95 continue;
96
97 uint32_t CacheRangeOffset =
98 AbsoluteDifference(CachedExtent.first, Intersection.first);
99 Buffer = CachedAlloc.slice(CacheRangeOffset, Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000100 return Error::success();
101 }
102
103 // Otherwise allocate a large enough buffer in the pool, memcpy the data
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000104 // into it, and return an ArrayRef to that. Do not touch existing pool
105 // allocations, as existing clients may be holding a pointer which must
106 // not be invalidated.
Zachary Turner97609bb2016-06-10 21:47:26 +0000107 uint8_t *WriteBuffer = static_cast<uint8_t *>(Pool.Allocate(Size, 8));
Zachary Turner8dbe3622016-05-27 01:54:44 +0000108 if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size)))
109 return EC;
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000110
111 if (CacheIter != CacheMap.end()) {
112 CacheIter->second.emplace_back(WriteBuffer, Size);
113 } else {
114 std::vector<CacheEntry> List;
115 List.emplace_back(WriteBuffer, Size);
116 CacheMap.insert(std::make_pair(Offset, List));
117 }
Zachary Turner8dbe3622016-05-27 01:54:44 +0000118 Buffer = ArrayRef<uint8_t>(WriteBuffer, Size);
119 return Error::success();
120}
121
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000122Error MappedBlockStream::readLongestContiguousChunk(
123 uint32_t Offset, ArrayRef<uint8_t> &Buffer) const {
124 // Make sure we aren't trying to read beyond the end of the stream.
125 if (Offset >= Data->getLength())
126 return make_error<RawError>(raw_error_code::insufficient_buffer);
127 uint32_t First = Offset / Pdb.getBlockSize();
128 uint32_t Last = First;
129
130 auto BlockList = Data->getStreamBlocks();
131 while (Last < Pdb.getBlockCount() - 1) {
132 if (BlockList[Last] != BlockList[Last + 1] - 1)
133 break;
134 ++Last;
135 }
136
137 uint32_t OffsetInFirstBlock = Offset % Pdb.getBlockSize();
138 uint32_t BytesFromFirstBlock = Pdb.getBlockSize() - OffsetInFirstBlock;
139 uint32_t BlockSpan = Last - First + 1;
140 uint32_t ByteSpan =
141 BytesFromFirstBlock + (BlockSpan - 1) * Pdb.getBlockSize();
142 Buffer = Pdb.getBlockData(BlockList[First], Pdb.getBlockSize());
143 Buffer = Buffer.drop_front(OffsetInFirstBlock);
144 Buffer = ArrayRef<uint8_t>(Buffer.data(), ByteSpan);
145 return Error::success();
146}
147
Zachary Turnerd8447992016-06-07 05:28:55 +0000148uint32_t MappedBlockStream::getLength() const { return Data->getLength(); }
149
Zachary Turner8dbe3622016-05-27 01:54:44 +0000150bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size,
151 ArrayRef<uint8_t> &Buffer) const {
152 // Attempt to fulfill the request with a reference directly into the stream.
153 // This can work even if the request crosses a block boundary, provided that
154 // all subsequent blocks are contiguous. For example, a 10k read with a 4k
155 // block size can be filled with a reference if, from the starting offset,
156 // 3 blocks in a row are contiguous.
157 uint32_t BlockNum = Offset / Pdb.getBlockSize();
158 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
159 uint32_t BytesFromFirstBlock =
160 std::min(Size, Pdb.getBlockSize() - OffsetInBlock);
161 uint32_t NumAdditionalBlocks =
162 llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) /
163 Pdb.getBlockSize();
164
Zachary Turnerd8447992016-06-07 05:28:55 +0000165 auto BlockList = Data->getStreamBlocks();
Zachary Turner8dbe3622016-05-27 01:54:44 +0000166 uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1;
167 uint32_t E = BlockList[BlockNum];
168 for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) {
169 if (BlockList[I + BlockNum] != E)
170 return false;
171 }
172
173 uint32_t FirstBlockAddr = BlockList[BlockNum];
Zachary Turnere6fee882016-06-07 20:38:37 +0000174 auto Data = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize());
175 Data = Data.drop_front(OffsetInBlock);
176 Buffer = ArrayRef<uint8_t>(Data.data(), Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +0000177 return true;
178}
179
Zachary Turner819e77d2016-05-06 20:51:57 +0000180Error MappedBlockStream::readBytes(uint32_t Offset,
181 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turner6ba65de2016-04-29 17:22:58 +0000182 uint32_t BlockNum = Offset / Pdb.getBlockSize();
183 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
184
185 // Make sure we aren't trying to read beyond the end of the stream.
Zachary Turnerd8447992016-06-07 05:28:55 +0000186 if (Buffer.size() > Data->getLength())
Zachary Turner819e77d2016-05-06 20:51:57 +0000187 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +0000188 if (Offset > Data->getLength() - Buffer.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000189 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000190
191 uint32_t BytesLeft = Buffer.size();
192 uint32_t BytesWritten = 0;
193 uint8_t *WriteBuffer = Buffer.data();
Zachary Turnerd8447992016-06-07 05:28:55 +0000194 auto BlockList = Data->getStreamBlocks();
Zachary Turner6ba65de2016-04-29 17:22:58 +0000195 while (BytesLeft > 0) {
196 uint32_t StreamBlockAddr = BlockList[BlockNum];
197
Zachary Turnere6fee882016-06-07 20:38:37 +0000198 auto Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
Zachary Turner6ba65de2016-04-29 17:22:58 +0000199
Zachary Turnere6fee882016-06-07 20:38:37 +0000200 const uint8_t *ChunkStart = Data.data() + OffsetInBlock;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000201 uint32_t BytesInChunk =
202 std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
203 ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk);
204
205 BytesWritten += BytesInChunk;
206 BytesLeft -= BytesInChunk;
207 ++BlockNum;
208 OffsetInBlock = 0;
209 }
210
Zachary Turner819e77d2016-05-06 20:51:57 +0000211 return Error::success();
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000212}
Zachary Turnerf5c59652016-05-03 00:28:21 +0000213
Zachary Turner5acb4ac2016-06-10 05:09:12 +0000214Error MappedBlockStream::writeBytes(uint32_t Offset,
215 ArrayRef<uint8_t> Buffer) const {
216 // Make sure we aren't trying to write beyond the end of the stream.
217 if (Buffer.size() > Data->getLength())
218 return make_error<RawError>(raw_error_code::insufficient_buffer);
219
220 if (Offset > Data->getLength() - Buffer.size())
221 return make_error<RawError>(raw_error_code::insufficient_buffer);
222
223 uint32_t BlockNum = Offset / Pdb.getBlockSize();
224 uint32_t OffsetInBlock = Offset % Pdb.getBlockSize();
225
226 uint32_t BytesLeft = Buffer.size();
227 auto BlockList = Data->getStreamBlocks();
228 uint32_t BytesWritten = 0;
229 while (BytesLeft > 0) {
230 uint32_t StreamBlockAddr = BlockList[BlockNum];
231 uint32_t BytesToWriteInChunk =
232 std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock);
233
234 const uint8_t *Chunk = Buffer.data() + BytesWritten;
235 ArrayRef<uint8_t> ChunkData(Chunk, BytesToWriteInChunk);
236 if (auto EC = Pdb.setBlockData(StreamBlockAddr, OffsetInBlock, ChunkData))
237 return EC;
238
239 BytesLeft -= BytesToWriteInChunk;
240 BytesWritten += BytesToWriteInChunk;
241 ++BlockNum;
242 OffsetInBlock = 0;
243 }
244
245 // If this write overlapped a read which previously came from the pool,
246 // someone may still be holding a pointer to that alloc which is now invalid.
247 // Compute the overlapping range and update the cache entry, so any
248 // outstanding buffers are automatically updated.
249 for (const auto &MapEntry : CacheMap) {
250 // If the end of the written extent precedes the beginning of the cached
251 // extent, ignore this map entry.
252 if (Offset + BytesWritten < MapEntry.first)
253 continue;
254 for (const auto &Alloc : MapEntry.second) {
255 // If the end of the cached extent precedes the beginning of the written
256 // extent, ignore this alloc.
257 if (MapEntry.first + Alloc.size() < Offset)
258 continue;
259
260 // If we get here, they are guaranteed to overlap.
261 Interval WriteInterval = std::make_pair(Offset, Offset + BytesWritten);
262 Interval CachedInterval =
263 std::make_pair(MapEntry.first, MapEntry.first + Alloc.size());
264 // If they overlap, we need to write the new data into the overlapping
265 // range.
266 auto Intersection = intersect(WriteInterval, CachedInterval);
267 assert(Intersection.first <= Intersection.second);
268
269 uint32_t Length = Intersection.second - Intersection.first;
270 uint32_t SrcOffset =
271 AbsoluteDifference(WriteInterval.first, Intersection.first);
272 uint32_t DestOffset =
273 AbsoluteDifference(CachedInterval.first, Intersection.first);
274 ::memcpy(Alloc.data() + DestOffset, Buffer.data() + SrcOffset, Length);
275 }
276 }
277
278 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000279}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000280
281uint32_t MappedBlockStream::getNumBytesCopied() const {
282 return static_cast<uint32_t>(Pool.getBytesAllocated());
283}
Zachary Turnera1657a92016-06-08 17:26:39 +0000284
285Expected<std::unique_ptr<MappedBlockStream>>
286MappedBlockStream::createIndexedStream(uint32_t StreamIdx,
287 const IPDBFile &File) {
288 if (StreamIdx >= File.getNumStreams())
289 return make_error<RawError>(raw_error_code::no_stream);
290
291 auto Data = llvm::make_unique<IndexedStreamData>(StreamIdx, File);
292 return llvm::make_unique<MappedBlockStreamImpl>(std::move(Data), File);
293}
294
295Expected<std::unique_ptr<MappedBlockStream>>
296MappedBlockStream::createDirectoryStream(const PDBFile &File) {
297 auto Data = llvm::make_unique<DirectoryStreamData>(File);
298 return llvm::make_unique<MappedBlockStreamImpl>(std::move(Data), File);
299}