LLDB mainline
Memory.h
Go to the documentation of this file.
1//===-- Memory.h ------------------------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_TARGET_MEMORY_H
10#define LLDB_TARGET_MEMORY_H
11
13#include "lldb/lldb-private.h"
14#include "llvm/ADT/ArrayRef.h"
15#include "llvm/ADT/SmallVector.h"
16#include <map>
17#include <mutex>
18#include <vector>
19
20namespace lldb_private {
21// A class to track memory that was read from a live process between
22// runs.
24public:
25 // Constructors and Destructors
26 MemoryCache(Process &process);
27
29
30 void Clear(bool clear_invalid_ranges = false);
31
32 void Flush(lldb::addr_t addr, size_t size);
33
34 size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error);
35
36 /// Reads multiple memory ranges, serving cache hits from L1 and L2 and
37 /// batching all misses through Process::DoReadMemoryRanges. The semantics of
38 /// the return value match Process::ReadMemoryRanges.
39 llvm::SmallVector<llvm::MutableArrayRef<uint8_t>>
40 ReadRanges(llvm::ArrayRef<Range<lldb::addr_t, size_t>> ranges,
41 llvm::MutableArrayRef<uint8_t> buffer);
42
44
45 void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
46
47 bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
48
49 // Allow external sources to populate data into the L1 memory cache
50 void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
51
52 void AddL1CacheData(lldb::addr_t addr, llvm::ArrayRef<uint8_t> src) {
53 if (!src.empty())
54 AddL1CacheData(addr, src.data(), src.size());
55 }
56
58 const lldb::DataBufferSP &data_buffer_sp);
59
60protected:
61 typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
64 // Classes that inherit from MemoryCache can see and modify these
65 std::recursive_mutex m_mutex;
66 BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that
67 // will be used only if the memory read fits entirely in
68 // a chunk
69 BlockMap m_L2_cache; // A memory cache of fixed size chinks
70 // (m_L2_cache_line_byte_size bytes in size each)
74
75private:
76 MemoryCache(const MemoryCache &) = delete;
77 const MemoryCache &operator=(const MemoryCache &) = delete;
78
80
81 // If the entire range [addr, addr+len) is covered by a single L1 entry,
82 // returns a pointer into that entry's data at the correct offset. Returns
83 // nullptr on a miss. Caller must hold m_mutex.
84 const uint8_t *FindL1CacheEntry(lldb::addr_t addr, size_t len) const;
85
86 // If the entire range [addr, addr+len) is covered by a single cache line
87 // that is already in L2, returns a pointer into that line's data at the
88 // correct offset. Never reads from the inferior. Returns nullptr on a miss.
89 // Caller must hold m_mutex.
90 const uint8_t *FindL2CacheEntry(lldb::addr_t addr, size_t len) const;
91
92 // Looks the range [addr, addr+len) up in L1 and then L2, returning a pointer
93 // into the data of whichever entry covers it. Returns nullptr on a miss.
94 // Caller must hold m_mutex.
95 const uint8_t *FindCacheEntry(lldb::addr_t addr, size_t len) const;
96};
97
98
99
101public:
102 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
103 uint32_t chunk_size);
104
106
107 lldb::addr_t ReserveBlock(uint32_t size);
108
109 bool FreeBlock(lldb::addr_t addr);
110
111 lldb::addr_t GetBaseAddress() const { return m_range.GetRangeBase(); }
112
113 uint32_t GetByteSize() const { return m_range.GetByteSize(); }
114
115 uint32_t GetPermissions() const { return m_permissions; }
116
117 uint32_t GetChunkSize() const { return m_chunk_size; }
118
119 bool Contains(lldb::addr_t addr) const {
120 return m_range.Contains(addr);
121 }
122
123protected:
124 uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
125
126 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
127 return (size + m_chunk_size - 1) / m_chunk_size;
128 }
129 // Base address of this block of memory 4GB of chunk should be enough.
131 // Permissions for this memory (logical OR of lldb::Permissions bits)
132 const uint32_t m_permissions;
133 // The size of chunks that the memory at m_addr is divied up into.
134 const uint32_t m_chunk_size;
135 // A sorted list of free address ranges.
137 // A sorted list of reserved address.
139};
140
141// A class that can track allocated memory and give out allocated memory
142// without us having to make an allocate/deallocate call every time we need
143// some memory in a process that is being debugged.
145public:
146 // Constructors and Destructors
148
150
151 void Clear(bool deallocate_memory);
152
153 lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
154 Status &error);
155
157
158 bool IsInCache(lldb::addr_t addr) const;
159
160protected:
161 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
162
163 AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
164 uint32_t chunk_size, Status &error);
165
166 // Classes that inherit from MemoryCache can see and modify these
168 mutable std::recursive_mutex m_mutex;
169 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
171
172private:
175};
176
177} // namespace lldb_private
178
179#endif // LLDB_TARGET_MEMORY_H
static llvm::raw_ostream & error(Stream &strm)
uint32_t CalculateChunksNeededForSize(uint32_t size) const
Definition Memory.h:126
bool FreeBlock(lldb::addr_t addr)
Definition Memory.cpp:409
uint32_t GetPermissions() const
Definition Memory.h:115
lldb::addr_t GetBaseAddress() const
Definition Memory.h:111
lldb::addr_t ReserveBlock(uint32_t size)
Definition Memory.cpp:357
const uint32_t m_permissions
Definition Memory.h:132
Range< lldb::addr_t, uint32_t > m_range
Definition Memory.h:130
AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions, uint32_t chunk_size)
Definition Memory.cpp:345
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
Definition Memory.h:136
uint32_t TotalChunks() const
Definition Memory.h:124
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
Definition Memory.h:138
uint32_t GetChunkSize() const
Definition Memory.h:117
const uint32_t m_chunk_size
Definition Memory.h:134
uint32_t GetByteSize() const
Definition Memory.h:113
bool Contains(lldb::addr_t addr) const
Definition Memory.h:119
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
Definition Memory.cpp:463
bool IsInCache(lldb::addr_t addr) const
Definition Memory.cpp:513
std::multimap< uint32_t, AllocatedBlockSP > PermissionsToBlockMap
Definition Memory.h:169
const AllocatedMemoryCache & operator=(const AllocatedMemoryCache &)=delete
AllocatedMemoryCache(Process &process)
Definition Memory.cpp:423
std::recursive_mutex m_mutex
Definition Memory.h:168
void Clear(bool deallocate_memory)
Definition Memory.cpp:428
bool DeallocateMemory(lldb::addr_t ptr)
Definition Memory.cpp:494
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
Definition Memory.h:161
PermissionsToBlockMap m_memory_map
Definition Memory.h:170
AllocatedMemoryCache(const AllocatedMemoryCache &)=delete
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
Definition Memory.cpp:439
void AddL1CacheData(lldb::addr_t addr, llvm::ArrayRef< uint8_t > src)
Definition Memory.h:52
uint32_t GetMemoryCacheLineSize() const
Definition Memory.h:43
const uint8_t * FindL1CacheEntry(lldb::addr_t addr, size_t len) const
Definition Memory.cpp:127
MemoryCache(Process &process)
Definition Memory.cpp:26
std::recursive_mutex m_mutex
Definition Memory.h:65
bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition Memory.cpp:112
void Flush(lldb::addr_t addr, size_t size)
Definition Memory.cpp:54
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...
Definition Memory.cpp:296
std::map< lldb::addr_t, lldb::DataBufferSP > BlockMap
Definition Memory.h:61
void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
Definition Memory.cpp:43
const uint8_t * FindL2CacheEntry(lldb::addr_t addr, size_t len) const
Definition Memory.cpp:141
InvalidRanges m_invalid_ranges
Definition Memory.h:71
const uint8_t * FindCacheEntry(lldb::addr_t addr, size_t len) const
Definition Memory.cpp:154
void Clear(bool clear_invalid_ranges=false)
Definition Memory.cpp:34
RangeVector< lldb::addr_t, lldb::addr_t, 4 > InvalidRanges
Definition Memory.h:62
MemoryCache(const MemoryCache &)=delete
size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error)
Definition Memory.cpp:190
void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition Memory.cpp:102
lldb::DataBufferSP GetL2CacheLine(lldb::addr_t addr, Status &error)
Definition Memory.cpp:162
const MemoryCache & operator=(const MemoryCache &)=delete
Range< lldb::addr_t, lldb::addr_t > AddrRange
Definition Memory.h:63
uint32_t m_L2_cache_line_byte_size
Definition Memory.h:73
A plug-in interface definition class for debugging a process.
Definition Process.h:360
An error handling class.
Definition Status.h:118
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
uint64_t addr_t
Definition lldb-types.h:80