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);
63 BlockMap::iterator pos =
m_L1_cache.upper_bound(addr);
68 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 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
151 auto data_buffer_heap_sp =
153 size_t process_bytes_read =
m_process.ReadMemoryFromInferior(
154 line_base_addr, data_buffer_heap_sp->GetBytes(),
155 data_buffer_heap_sp->GetByteSize(),
error);
158 if (process_bytes_read == 0)
163 data_buffer_heap_sp->SetByteSize(process_bytes_read);
165 m_L2_cache[line_base_addr] = data_buffer_heap_sp;
166 return data_buffer_heap_sp;
171 if (!dst || dst_len == 0)
174 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
183 "memory read failed for 0x%" PRIx64, addr);
192 memcpy(dst, l1_data, dst_len);
214 addr_t cache_line_base_addr = addr - cache_line_offset;
218 if (!first_cache_line)
223 if (cache_line_offset >= first_cache_line->GetByteSize())
226 uint8_t *dst_buf = (uint8_t *)dst;
227 size_t bytes_left = dst_len;
228 size_t read_size = first_cache_line->GetByteSize() - cache_line_offset;
229 if (read_size > bytes_left)
230 read_size = bytes_left;
232 memcpy(dst_buf + dst_len - bytes_left,
233 first_cache_line->GetBytes() + cache_line_offset, read_size);
234 bytes_left -= read_size;
240 return dst_len - bytes_left;
243 if (bytes_left > 0) {
251 "memory read failed for 0x%" PRIx64, cache_line_base_addr);
252 return dst_len - bytes_left;
257 if (!second_cache_line)
258 return dst_len - bytes_left;
260 read_size = bytes_left;
261 if (read_size > second_cache_line->GetByteSize())
262 read_size = second_cache_line->GetByteSize();
264 memcpy(dst_buf + dst_len - bytes_left, second_cache_line->GetBytes(),
266 bytes_left -= read_size;
268 return dst_len - bytes_left;
274llvm::SmallVector<llvm::MutableArrayRef<uint8_t>>
276 llvm::MutableArrayRef<uint8_t> buffer) {
277 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
279 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> results;
280 results.reserve(ranges.size());
281 llvm::SmallVector<Range<lldb::addr_t, size_t>> missed_ranges;
284 for (
auto range : ranges) {
286 const size_t len = range.GetByteSize();
289 results.push_back(buffer.take_front(0));
294 results.push_back(buffer.take_front(len));
295 buffer = buffer.drop_front(len);
296 memcpy(results.back().data(), l1_data, len);
301 results.emplace_back(
nullptr,
nullptr);
302 missed_ranges.push_back(range);
305 if (missed_ranges.empty())
308 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>> fetched_buffers_vec =
309 m_process.DoReadMemoryRanges(missed_ranges, buffer);
310 auto fetched_buffers = llvm::ArrayRef(fetched_buffers_vec);
312 for (
auto [missed_range, fetched] : llvm::zip(missed_ranges, fetched_buffers))
316 for (
auto &result : results)
317 if (result.data() ==
nullptr)
318 result = fetched_buffers.consume_front();
324 uint32_t permissions, uint32_t chunk_size)
330 assert(byte_size > chunk_size);
342 for (
size_t i=0; i<free_count; ++i)
345 const lldb::addr_t range_size = free_block.GetByteSize();
346 if (range_size >= size)
351 addr_t addr = free_block.GetRangeBase();
373 free_block.SetRangeBase(reserved_block.
GetRangeEnd());
374 free_block.SetByteSize(bytes_left);
388 bool success =
false;
407 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
408 if (
m_process.IsAlive() && deallocate_memory) {
409 PermissionsToBlockMap::iterator pos, end =
m_memory_map.end();
411 m_process.DoDeallocateMemory(pos->second->GetBaseAddress());
420 const size_t page_size = 4096;
421 const size_t num_pages = (byte_size + page_size - 1) / page_size;
422 const size_t page_byte_size = num_pages * page_size;
428 "Process::DoAllocateMemory (byte_size = 0x%8.8" PRIx32
429 ", permissions = %s) => 0x%16.16" PRIx64,
434 block_sp = std::make_shared<AllocatedBlock>(addr, page_byte_size,
435 permissions, chunk_size);
436 m_memory_map.insert(std::make_pair(permissions, block_sp));
442 uint32_t permissions,
444 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
447 std::pair<PermissionsToBlockMap::iterator, PermissionsToBlockMap::iterator>
450 for (PermissionsToBlockMap::iterator pos = range.first; pos != range.second;
452 addr = (*pos).second->ReserveBlock(byte_size);
461 addr = block_sp->ReserveBlock(byte_size);
465 "AllocatedMemoryCache::AllocateMemory (byte_size = 0x%8.8" PRIx32
466 ", permissions = %s) => 0x%16.16" PRIx64,
473 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
475 PermissionsToBlockMap::iterator pos, end =
m_memory_map.end();
476 bool success =
false;
478 if (pos->second->Contains(addr)) {
479 success = pos->second->FreeBlock(addr);
485 "AllocatedMemoryCache::DeallocateMemory (addr = 0x%16.16" PRIx64
487 (uint64_t)addr, success);
492 std::lock_guard<std::recursive_mutex> guard(
m_mutex);
494 return llvm::any_of(
m_memory_map, [addr](
const auto &block) {
495 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 batching all misses through Process::DoR...
void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
InvalidRanges m_invalid_ranges
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)