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