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 batching all
37 /// misses through Process::DoReadMemoryRanges. The semantics of the return
38 /// 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
87
88
90public:
91 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
92 uint32_t chunk_size);
93
95
96 lldb::addr_t ReserveBlock(uint32_t size);
97
98 bool FreeBlock(lldb::addr_t addr);
99
100 lldb::addr_t GetBaseAddress() const { return m_range.GetRangeBase(); }
101
102 uint32_t GetByteSize() const { return m_range.GetByteSize(); }
103
104 uint32_t GetPermissions() const { return m_permissions; }
105
106 uint32_t GetChunkSize() const { return m_chunk_size; }
107
108 bool Contains(lldb::addr_t addr) const {
109 return m_range.Contains(addr);
110 }
111
112protected:
113 uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
114
115 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
116 return (size + m_chunk_size - 1) / m_chunk_size;
117 }
118 // Base address of this block of memory 4GB of chunk should be enough.
120 // Permissions for this memory (logical OR of lldb::Permissions bits)
121 const uint32_t m_permissions;
122 // The size of chunks that the memory at m_addr is divied up into.
123 const uint32_t m_chunk_size;
124 // A sorted list of free address ranges.
126 // A sorted list of reserved address.
128};
129
130// A class that can track allocated memory and give out allocated memory
131// without us having to make an allocate/deallocate call every time we need
132// some memory in a process that is being debugged.
134public:
135 // Constructors and Destructors
137
139
140 void Clear(bool deallocate_memory);
141
142 lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
143 Status &error);
144
146
147 bool IsInCache(lldb::addr_t addr) const;
148
149protected:
150 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
151
152 AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
153 uint32_t chunk_size, Status &error);
154
155 // Classes that inherit from MemoryCache can see and modify these
157 mutable std::recursive_mutex m_mutex;
158 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
160
161private:
164};
165
166} // namespace lldb_private
167
168#endif // LLDB_TARGET_MEMORY_H
static llvm::raw_ostream & error(Stream &strm)
uint32_t CalculateChunksNeededForSize(uint32_t size) const
Definition Memory.h:115
bool FreeBlock(lldb::addr_t addr)
Definition Memory.cpp:387
uint32_t GetPermissions() const
Definition Memory.h:104
lldb::addr_t GetBaseAddress() const
Definition Memory.h:100
lldb::addr_t ReserveBlock(uint32_t size)
Definition Memory.cpp:335
const uint32_t m_permissions
Definition Memory.h:121
Range< lldb::addr_t, uint32_t > m_range
Definition Memory.h:119
AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions, uint32_t chunk_size)
Definition Memory.cpp:323
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
Definition Memory.h:125
uint32_t TotalChunks() const
Definition Memory.h:113
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
Definition Memory.h:127
uint32_t GetChunkSize() const
Definition Memory.h:106
const uint32_t m_chunk_size
Definition Memory.h:123
uint32_t GetByteSize() const
Definition Memory.h:102
bool Contains(lldb::addr_t addr) const
Definition Memory.h:108
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
Definition Memory.cpp:441
bool IsInCache(lldb::addr_t addr) const
Definition Memory.cpp:491
std::multimap< uint32_t, AllocatedBlockSP > PermissionsToBlockMap
Definition Memory.h:158
const AllocatedMemoryCache & operator=(const AllocatedMemoryCache &)=delete
AllocatedMemoryCache(Process &process)
Definition Memory.cpp:401
std::recursive_mutex m_mutex
Definition Memory.h:157
void Clear(bool deallocate_memory)
Definition Memory.cpp:406
bool DeallocateMemory(lldb::addr_t ptr)
Definition Memory.cpp:472
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
Definition Memory.h:150
PermissionsToBlockMap m_memory_map
Definition Memory.h:159
AllocatedMemoryCache(const AllocatedMemoryCache &)=delete
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
Definition Memory.cpp:417
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 batching all misses through Process::DoR...
Definition Memory.cpp:275
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
InvalidRanges m_invalid_ranges
Definition Memory.h:71
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:169
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:141
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:357
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