17#include "llvm/ADT/STLExtras.h"
35 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
38 if (clear_invalid_ranges)
45 AddL1CacheData(addr, std::make_shared<DataBufferHeap>(src, src_len));
50 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
58 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
67 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
77 const addr_t end_addr = (addr + size - 1);
78 const addr_t first_cache_line_addr = addr - (addr % cache_line_byte_size);
79 const addr_t last_cache_line_addr =
80 end_addr - (end_addr % cache_line_byte_size);
83 uint32_t num_cache_lines;
84 if (last_cache_line_addr >= first_cache_line_addr)
85 num_cache_lines = ((last_cache_line_addr - first_cache_line_addr) /
86 cache_line_byte_size) +
90 (
UINT64_MAX - first_cache_line_addr + 1) / cache_line_byte_size;
92 uint32_t cache_idx = 0;
93 for (
addr_t curr_addr = first_cache_line_addr; cache_idx < num_cache_lines;
94 curr_addr += cache_line_byte_size, ++cache_idx) {
95 BlockMap::iterator pos =
m_L2_cache.find(curr_addr);
105 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
115 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
116 const uint32_t idx =
m_invalid_ranges.FindEntryIndexThatContains(base_addr);
132 BlockMap::const_iterator pos =
m_L1_cache.upper_bound(addr);
135 AddrRange chunk_range(pos->first, pos->second->GetByteSize());
136 if (!chunk_range.
Contains(read_range))
138 return pos->second->GetBytes() + (addr - chunk_range.
GetRangeBase());
146 BlockMap::const_iterator pos =
m_L2_cache.find(addr - line_offset);
149 if (line_offset + len > pos->second->GetByteSize())
151 return pos->second->GetBytes() + line_offset;
167 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
172 auto data_buffer_heap_sp =
174 size_t process_bytes_read =
m_process.ReadMemoryFromInferior(
175 line_base_addr, data_buffer_heap_sp->GetBytes(),
176 data_buffer_heap_sp->GetByteSize(),
error);
179 if (process_bytes_read == 0)
184 data_buffer_heap_sp->SetByteSize(process_bytes_read);
186 m_L2_cache[line_base_addr] = data_buffer_heap_sp;
187 return data_buffer_heap_sp;
192 if (!dst || dst_len == 0)
195 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
200 const addr_t invalid_addr = invalid->GetRangeBase();
202 "memory read failed for 0x%" PRIx64, invalid_addr);
203 if (invalid_addr <= addr)
205 dst_len = invalid_addr - addr;
213 memcpy(dst, l1_data, dst_len);
235 addr_t cache_line_base_addr = addr - cache_line_offset;
239 if (!first_cache_line)
244 if (cache_line_offset >= first_cache_line->GetByteSize())
247 uint8_t *dst_buf = (uint8_t *)dst;
248 size_t bytes_left = dst_len;
249 size_t read_size = first_cache_line->GetByteSize() - cache_line_offset;
250 if (read_size > bytes_left)
251 read_size = bytes_left;
253 memcpy(dst_buf + dst_len - bytes_left,
254 first_cache_line->GetBytes() + cache_line_offset, read_size);
255 bytes_left -= read_size;
261 return dst_len - bytes_left;
264 if (bytes_left > 0) {
272 "memory read failed for 0x%" PRIx64, cache_line_base_addr);
273 return dst_len - bytes_left;
278 if (!second_cache_line)
279 return dst_len - bytes_left;
281 read_size = bytes_left;
282 if (read_size > second_cache_line->GetByteSize())
283 read_size = second_cache_line->GetByteSize();
285 memcpy(dst_buf + dst_len - bytes_left, second_cache_line->GetBytes(),
287 bytes_left -= read_size;
289 return dst_len - bytes_left;
295llvm::SmallVector<llvm::MutableArrayRef<uint8_t>>
297 llvm::MutableArrayRef<uint8_t> buffer) {
300 auto total_ranges_len = llvm::sum_of(
301 llvm::map_range(ranges, [](
auto range) {
return range.size; }));
302 assert(buffer.size() >= total_ranges_len &&
303 "MemoryCache::ReadRanges: provided buffer is too short");
304 if (buffer.size() < total_ranges_len) {
305 llvm::MutableArrayRef<uint8_t> empty;
306 return {ranges.size(), empty};
309 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
311 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> results;
312 results.reserve(ranges.size());
313 llvm::SmallVector<Range<lldb::addr_t, size_t>> missed_ranges;
316 for (
auto range : ranges) {
318 const size_t len = range.GetByteSize();
321 results.push_back(buffer.take_front(0));
327 results.push_back(buffer.take_front(len));
328 buffer = buffer.drop_front(len);
329 memcpy(results.back().data(), cached, len);
334 results.emplace_back(
nullptr,
nullptr);
335 missed_ranges.push_back(range);
338 if (missed_ranges.empty())
341 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> fetched_buffers_vec =
342 m_process.DoReadMemoryRanges(missed_ranges, buffer);
343 auto fetched_buffers = llvm::ArrayRef(fetched_buffers_vec);
345 for (
auto [missed_range, fetched] : llvm::zip(missed_ranges, fetched_buffers))
349 for (
auto &result : results)
350 if (result.data() ==
nullptr)
351 result = fetched_buffers.consume_front();
357 uint32_t permissions, uint32_t chunk_size)
363 assert(byte_size > chunk_size);
375 for (
size_t i=0; i<free_count; ++i)
378 const lldb::addr_t range_size = free_block.GetByteSize();
379 if (range_size >= size)
384 addr_t addr = free_block.GetRangeBase();
406 free_block.SetRangeBase(reserved_block.
GetRangeEnd());
407 free_block.SetByteSize(bytes_left);
421 bool success =
false;
440 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
441 if (
m_process.IsAlive() && deallocate_memory) {
442 PermissionsToBlockMap::iterator pos, end =
m_memory_map.end();
444 m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
453 const size_t page_size = 4096;
454 const size_t num_pages = (byte_size + page_size - 1) / page_size;
455 const size_t page_byte_size = num_pages * page_size;
461 "Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32
462 ", permissions = %s) => 0x%16.16" PRIx64,
467 block_sp = std::make_shared<AllocatedBlock>(addr, page_byte_size,
468 permissions, chunk_size);
469 m_memory_map.insert(std::make_pair(permissions, block_sp));
475 uint32_t permissions,
477 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
480 std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator>
483 for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second;
485 addr = (*pos).second->ReserveBlock(byte_size);
494 addr = block_sp->ReserveBlock(byte_size);
498 "AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32
499 ", permissions = %s) => 0x%16.16" PRIx64,
506 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
508 PermissionsToBlockMap::iterator pos, end =
m_memory_map.end();
509 bool success =
false;
511 if (pos->second->Contains(addr)) {
512 success = pos->second->FreeBlock(addr);
518 "AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64
520 (uint64_t)addr, success);
525 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
527 return llvm::any_of(
m_memory_map, [addr](
const auto &block) {
528 return block.second->Contains(addr);
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_LOG_VERBOSE(log,...)
uint32_t CalculateChunksNeededForSize(uint32_t size) const
bool FreeBlock(lldb::addr_t addr)
lldb::addr_t ReserveBlock(uint32_t size)
const uint32_t m_permissions
Range< lldb::addr_t, uint32_t > m_range
AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions, uint32_t chunk_size)
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
const uint32_t m_chunk_size
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
bool IsInCache(lldb::addr_t addr) const
AllocatedMemoryCache(Process &process)
std::recursive_mutex m_mutex
void Clear(bool deallocate_memory)
bool DeallocateMemory(lldb::addr_t ptr)
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
PermissionsToBlockMap m_memory_map
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
uint32_t GetMemoryCacheLineSize() const
const uint8_t * FindL1CacheEntry(lldb::addr_t addr, size_t len) const
MemoryCache(Process &process)
std::recursive_mutex m_mutex
bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
void Flush(lldb::addr_t addr, size_t size)
llvm::SmallVector< llvm::MutableArrayRef< uint8_t > > ReadRanges(llvm::ArrayRef< Range< lldb::addr_t, size_t > > ranges, llvm::MutableArrayRef< uint8_t > buffer)
Reads multiple memory ranges, serving cache hits from L1 and L2 and batching all misses through Proce...
void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
const uint8_t * FindL2CacheEntry(lldb::addr_t addr, size_t len) const
InvalidRanges m_invalid_ranges
const uint8_t * FindCacheEntry(lldb::addr_t addr, size_t len) const
void Clear(bool clear_invalid_ranges=false)
size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error)
void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
lldb::DataBufferSP GetL2CacheLine(lldb::addr_t addr, Status &error)
Range< lldb::addr_t, lldb::addr_t > AddrRange
uint32_t m_L2_cache_line_byte_size
A plug-in interface definition class for debugging a process.
Range< lldb::addr_t, lldb::addr_t > Entry
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
const char * GetPermissionsAsCString(uint32_t permissions)
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
bool Contains(BaseType r) const
BaseType GetRangeBase() const
SizeType GetByteSize() const
BaseType GetRangeEnd() const
bool DoesIntersect(const Range &rhs) const
void SetByteSize(SizeType s)