blob: cae33768af6fc3cc7a750dfad0dcff2050ef9c94 [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 Turnerd8447992016-06-07 05:28:55 +000032MappedBlockStream::MappedBlockStream(std::unique_ptr<IPDBStreamData> Data,
33 const IPDBFile &Pdb)
34 : Pdb(Pdb), Data(std::move(Data)) {}
Zachary Turner6ba65de2016-04-29 17:22:58 +000035
Zachary Turner8dbe3622016-05-27 01:54:44 +000036Error 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 Turnerd8447992016-06-07 05:28:55 +000039 if (Size > Data->getLength())
Zachary Turner8dbe3622016-05-27 01:54:44 +000040 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +000041 if (Offset > Data->getLength() - Size)
Zachary Turner8dbe3622016-05-27 01:54:44 +000042 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 Turnerd8447992016-06-07 05:28:55 +000069uint32_t MappedBlockStream::getLength() const { return Data->getLength(); }
70
Zachary Turner8dbe3622016-05-27 01:54:44 +000071bool 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 Turnerd8447992016-06-07 05:28:55 +000086 auto BlockList = Data->getStreamBlocks();
Zachary Turner8dbe3622016-05-27 01:54:44 +000087 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 Turnere6fee882016-06-07 20:38:37 +000095 auto Data = Pdb.getBlockData(FirstBlockAddr, Pdb.getBlockSize());
96 Data = Data.drop_front(OffsetInBlock);
97 Buffer = ArrayRef<uint8_t>(Data.data(), Size);
Zachary Turner8dbe3622016-05-27 01:54:44 +000098 return true;
99}
100
Zachary Turner819e77d2016-05-06 20:51:57 +0000101Error MappedBlockStream::readBytes(uint32_t Offset,
102 MutableArrayRef<uint8_t> Buffer) const {
Zachary Turner6ba65de2016-04-29 17:22:58 +0000103 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 Turnerd8447992016-06-07 05:28:55 +0000107 if (Buffer.size() > Data->getLength())
Zachary Turner819e77d2016-05-06 20:51:57 +0000108 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turnerd8447992016-06-07 05:28:55 +0000109 if (Offset > Data->getLength() - Buffer.size())
Zachary Turner819e77d2016-05-06 20:51:57 +0000110 return make_error<RawError>(raw_error_code::insufficient_buffer);
Zachary Turner6ba65de2016-04-29 17:22:58 +0000111
112 uint32_t BytesLeft = Buffer.size();
113 uint32_t BytesWritten = 0;
114 uint8_t *WriteBuffer = Buffer.data();
Zachary Turnerd8447992016-06-07 05:28:55 +0000115 auto BlockList = Data->getStreamBlocks();
Zachary Turner6ba65de2016-04-29 17:22:58 +0000116 while (BytesLeft > 0) {
117 uint32_t StreamBlockAddr = BlockList[BlockNum];
118
Zachary Turnere6fee882016-06-07 20:38:37 +0000119 auto Data = Pdb.getBlockData(StreamBlockAddr, Pdb.getBlockSize());
Zachary Turner6ba65de2016-04-29 17:22:58 +0000120
Zachary Turnere6fee882016-06-07 20:38:37 +0000121 const uint8_t *ChunkStart = Data.data() + OffsetInBlock;
Zachary Turner6ba65de2016-04-29 17:22:58 +0000122 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 Turner819e77d2016-05-06 20:51:57 +0000132 return Error::success();
Zachary Turnerf5c59652016-05-03 00:28:21 +0000133
Zachary Turnerf5c59652016-05-03 00:28:21 +0000134}
Zachary Turner90b8b8d2016-05-31 22:41:52 +0000135
136uint32_t MappedBlockStream::getNumBytesCopied() const {
137 return static_cast<uint32_t>(Pool.getBytesAllocated());
138}
Zachary Turnera1657a92016-06-08 17:26:39 +0000139
140Expected<std::unique_ptr<MappedBlockStream>>
141MappedBlockStream::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
150Expected<std::unique_ptr<MappedBlockStream>>
151MappedBlockStream::createDirectoryStream(const PDBFile &File) {
152 auto Data = llvm::make_unique<DirectoryStreamData>(File);
153 return llvm::make_unique<MappedBlockStreamImpl>(std::move(Data), File);
154}