Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 1 | //===- 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" |
| 11 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 15 | using namespace llvm::pdb; |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 16 | |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame^] | 17 | MappedBlockStream::MappedBlockStream(uint32_t StreamIdx, const IPDBFile &File) |
| 18 | : Pdb(File) { |
Zachary Turner | 96e60f7 | 2016-05-24 20:31:48 +0000 | [diff] [blame] | 19 | if (StreamIdx >= Pdb.getNumStreams()) { |
| 20 | StreamLength = 0; |
| 21 | } else { |
| 22 | StreamLength = Pdb.getStreamByteSize(StreamIdx); |
| 23 | BlockList = Pdb.getStreamBlockList(StreamIdx); |
| 24 | } |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 27 | Error MappedBlockStream::readBytes(uint32_t Offset, uint32_t Size, |
| 28 | ArrayRef<uint8_t> &Buffer) const { |
| 29 | // Make sure we aren't trying to read beyond the end of the stream. |
Zachary Turner | 7dd4259 | 2016-05-27 20:17:33 +0000 | [diff] [blame] | 30 | if (Size > StreamLength) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 31 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 7dd4259 | 2016-05-27 20:17:33 +0000 | [diff] [blame] | 32 | if (Offset > StreamLength - Size) |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 33 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
| 34 | |
| 35 | if (tryReadContiguously(Offset, Size, Buffer)) |
| 36 | return Error::success(); |
| 37 | |
| 38 | auto CacheIter = CacheMap.find(Offset); |
| 39 | if (CacheIter != CacheMap.end()) { |
| 40 | // In a more general solution, we would need to guarantee that the |
| 41 | // cached allocation is at least the requested size. In practice, since |
| 42 | // these are CodeView / PDB records, we know they are always formatted |
| 43 | // the same way and never change, so we should never be requesting two |
| 44 | // allocations from the same address with different sizes. |
| 45 | Buffer = ArrayRef<uint8_t>(CacheIter->second, Size); |
| 46 | return Error::success(); |
| 47 | } |
| 48 | |
| 49 | // Otherwise allocate a large enough buffer in the pool, memcpy the data |
| 50 | // into it, and return an ArrayRef to that. |
| 51 | uint8_t *WriteBuffer = Pool.Allocate<uint8_t>(Size); |
| 52 | |
| 53 | if (auto EC = readBytes(Offset, MutableArrayRef<uint8_t>(WriteBuffer, Size))) |
| 54 | return EC; |
| 55 | CacheMap.insert(std::make_pair(Offset, WriteBuffer)); |
| 56 | Buffer = ArrayRef<uint8_t>(WriteBuffer, Size); |
| 57 | return Error::success(); |
| 58 | } |
| 59 | |
| 60 | bool MappedBlockStream::tryReadContiguously(uint32_t Offset, uint32_t Size, |
| 61 | ArrayRef<uint8_t> &Buffer) const { |
| 62 | // Attempt to fulfill the request with a reference directly into the stream. |
| 63 | // This can work even if the request crosses a block boundary, provided that |
| 64 | // all subsequent blocks are contiguous. For example, a 10k read with a 4k |
| 65 | // block size can be filled with a reference if, from the starting offset, |
| 66 | // 3 blocks in a row are contiguous. |
| 67 | uint32_t BlockNum = Offset / Pdb.getBlockSize(); |
| 68 | uint32_t OffsetInBlock = Offset % Pdb.getBlockSize(); |
| 69 | uint32_t BytesFromFirstBlock = |
| 70 | std::min(Size, Pdb.getBlockSize() - OffsetInBlock); |
| 71 | uint32_t NumAdditionalBlocks = |
| 72 | llvm::alignTo(Size - BytesFromFirstBlock, Pdb.getBlockSize()) / |
| 73 | Pdb.getBlockSize(); |
| 74 | |
| 75 | uint32_t RequiredContiguousBlocks = NumAdditionalBlocks + 1; |
| 76 | uint32_t E = BlockList[BlockNum]; |
| 77 | for (uint32_t I = 0; I < RequiredContiguousBlocks; ++I, ++E) { |
| 78 | if (BlockList[I + BlockNum] != E) |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | uint32_t FirstBlockAddr = BlockList[BlockNum]; |
| 83 | StringRef Str = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize()); |
| 84 | Str = Str.drop_front(OffsetInBlock); |
| 85 | Buffer = |
| 86 | ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(Str.data()), Size); |
| 87 | return true; |
| 88 | } |
| 89 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 90 | Error MappedBlockStream::readBytes(uint32_t Offset, |
| 91 | MutableArrayRef<uint8_t> Buffer) const { |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 92 | uint32_t BlockNum = Offset / Pdb.getBlockSize(); |
| 93 | uint32_t OffsetInBlock = Offset % Pdb.getBlockSize(); |
| 94 | |
| 95 | // Make sure we aren't trying to read beyond the end of the stream. |
| 96 | if (Buffer.size() > StreamLength) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 97 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 98 | if (Offset > StreamLength - Buffer.size()) |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 99 | return make_error<RawError>(raw_error_code::insufficient_buffer); |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 100 | |
| 101 | uint32_t BytesLeft = Buffer.size(); |
| 102 | uint32_t BytesWritten = 0; |
| 103 | uint8_t *WriteBuffer = Buffer.data(); |
| 104 | while (BytesLeft > 0) { |
| 105 | uint32_t StreamBlockAddr = BlockList[BlockNum]; |
| 106 | |
| 107 | StringRef Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize()); |
| 108 | |
| 109 | const char *ChunkStart = Data.data() + OffsetInBlock; |
| 110 | uint32_t BytesInChunk = |
| 111 | std::min(BytesLeft, Pdb.getBlockSize() - OffsetInBlock); |
| 112 | ::memcpy(WriteBuffer + BytesWritten, ChunkStart, BytesInChunk); |
| 113 | |
| 114 | BytesWritten += BytesInChunk; |
| 115 | BytesLeft -= BytesInChunk; |
| 116 | ++BlockNum; |
| 117 | OffsetInBlock = 0; |
| 118 | } |
| 119 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 120 | return Error::success(); |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 121 | |
Zachary Turner | f5c5965 | 2016-05-03 00:28:21 +0000 | [diff] [blame] | 122 | } |
Zachary Turner | 90b8b8d | 2016-05-31 22:41:52 +0000 | [diff] [blame^] | 123 | |
| 124 | uint32_t MappedBlockStream::getNumBytesCopied() const { |
| 125 | return static_cast<uint32_t>(Pool.getBytesAllocated()); |
| 126 | } |