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 <map>
15#include <mutex>
16#include <vector>
17
18namespace lldb_private {
19// A class to track memory that was read from a live process between
20// runs.
22public:
23 // Constructors and Destructors
24 MemoryCache(Process &process);
25
27
28 void Clear(bool clear_invalid_ranges = false);
29
30 void Flush(lldb::addr_t addr, size_t size);
31
32 size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error);
33
35
36 void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
37
38 bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size);
39
40 // Allow external sources to populate data into the L1 memory cache
41 void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len);
42
44 const lldb::DataBufferSP &data_buffer_sp);
45
46protected:
47 typedef std::map<lldb::addr_t, lldb::DataBufferSP> BlockMap;
50 // Classes that inherit from MemoryCache can see and modify these
51 std::recursive_mutex m_mutex;
52 BlockMap m_L1_cache; // A first level memory cache whose chunk sizes vary that
53 // will be used only if the memory read fits entirely in
54 // a chunk
55 BlockMap m_L2_cache; // A memory cache of fixed size chinks
56 // (m_L2_cache_line_byte_size bytes in size each)
60
61private:
62 MemoryCache(const MemoryCache &) = delete;
63 const MemoryCache &operator=(const MemoryCache &) = delete;
64
66
67 // If the entire range [addr, addr+len) is covered by a single L1 entry,
68 // returns a pointer into that entry's data at the correct offset. Returns
69 // nullptr on a miss. Caller must hold m_mutex.
70 const uint8_t *FindL1CacheEntry(lldb::addr_t addr, size_t len) const;
71};
72
73
74
76public:
77 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
78 uint32_t chunk_size);
79
81
82 lldb::addr_t ReserveBlock(uint32_t size);
83
84 bool FreeBlock(lldb::addr_t addr);
85
86 lldb::addr_t GetBaseAddress() const { return m_range.GetRangeBase(); }
87
88 uint32_t GetByteSize() const { return m_range.GetByteSize(); }
89
90 uint32_t GetPermissions() const { return m_permissions; }
91
92 uint32_t GetChunkSize() const { return m_chunk_size; }
93
94 bool Contains(lldb::addr_t addr) const {
95 return m_range.Contains(addr);
96 }
97
98protected:
99 uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
100
101 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
102 return (size + m_chunk_size - 1) / m_chunk_size;
103 }
104 // Base address of this block of memory 4GB of chunk should be enough.
106 // Permissions for this memory (logical OR of lldb::Permissions bits)
107 const uint32_t m_permissions;
108 // The size of chunks that the memory at m_addr is divied up into.
109 const uint32_t m_chunk_size;
110 // A sorted list of free address ranges.
112 // A sorted list of reserved address.
114};
115
116// A class that can track allocated memory and give out allocated memory
117// without us having to make an allocate/deallocate call every time we need
118// some memory in a process that is being debugged.
120public:
121 // Constructors and Destructors
123
125
126 void Clear(bool deallocate_memory);
127
128 lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
129 Status &error);
130
132
133 bool IsInCache(lldb::addr_t addr) const;
134
135protected:
136 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
137
138 AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
139 uint32_t chunk_size, Status &error);
140
141 // Classes that inherit from MemoryCache can see and modify these
143 mutable std::recursive_mutex m_mutex;
144 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
146
147private:
150};
151
152} // namespace lldb_private
153
154#endif // LLDB_TARGET_MEMORY_H
static llvm::raw_ostream & error(Stream &strm)
uint32_t CalculateChunksNeededForSize(uint32_t size) const
Definition Memory.h:101
bool FreeBlock(lldb::addr_t addr)
Definition Memory.cpp:337
uint32_t GetPermissions() const
Definition Memory.h:90
lldb::addr_t GetBaseAddress() const
Definition Memory.h:86
lldb::addr_t ReserveBlock(uint32_t size)
Definition Memory.cpp:285
const uint32_t m_permissions
Definition Memory.h:107
Range< lldb::addr_t, uint32_t > m_range
Definition Memory.h:105
AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions, uint32_t chunk_size)
Definition Memory.cpp:273
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
Definition Memory.h:111
uint32_t TotalChunks() const
Definition Memory.h:99
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
Definition Memory.h:113
uint32_t GetChunkSize() const
Definition Memory.h:92
const uint32_t m_chunk_size
Definition Memory.h:109
uint32_t GetByteSize() const
Definition Memory.h:88
bool Contains(lldb::addr_t addr) const
Definition Memory.h:94
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
Definition Memory.cpp:391
bool IsInCache(lldb::addr_t addr) const
Definition Memory.cpp:441
std::multimap< uint32_t, AllocatedBlockSP > PermissionsToBlockMap
Definition Memory.h:144
const AllocatedMemoryCache & operator=(const AllocatedMemoryCache &)=delete
AllocatedMemoryCache(Process &process)
Definition Memory.cpp:351
std::recursive_mutex m_mutex
Definition Memory.h:143
void Clear(bool deallocate_memory)
Definition Memory.cpp:356
bool DeallocateMemory(lldb::addr_t ptr)
Definition Memory.cpp:422
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
Definition Memory.h:136
PermissionsToBlockMap m_memory_map
Definition Memory.h:145
AllocatedMemoryCache(const AllocatedMemoryCache &)=delete
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
Definition Memory.cpp:367
uint32_t GetMemoryCacheLineSize() const
Definition Memory.h:34
const uint8_t * FindL1CacheEntry(lldb::addr_t addr, size_t len) const
Definition Memory.cpp:126
MemoryCache(Process &process)
Definition Memory.cpp:24
std::recursive_mutex m_mutex
Definition Memory.h:51
bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition Memory.cpp:111
void Flush(lldb::addr_t addr, size_t size)
Definition Memory.cpp:53
std::map< lldb::addr_t, lldb::DataBufferSP > BlockMap
Definition Memory.h:47
void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
Definition Memory.cpp:41
InvalidRanges m_invalid_ranges
Definition Memory.h:57
void Clear(bool clear_invalid_ranges=false)
Definition Memory.cpp:32
RangeVector< lldb::addr_t, lldb::addr_t, 4 > InvalidRanges
Definition Memory.h:48
MemoryCache(const MemoryCache &)=delete
size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error)
Definition Memory.cpp:168
void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition Memory.cpp:101
lldb::DataBufferSP GetL2CacheLine(lldb::addr_t addr, Status &error)
Definition Memory.cpp:140
const MemoryCache & operator=(const MemoryCache &)=delete
Range< lldb::addr_t, lldb::addr_t > AddrRange
Definition Memory.h:49
uint32_t m_L2_cache_line_byte_size
Definition Memory.h:59
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