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
68
69
71public:
72 AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
73 uint32_t chunk_size);
74
76
77 lldb::addr_t ReserveBlock(uint32_t size);
78
79 bool FreeBlock(lldb::addr_t addr);
80
82
83 uint32_t GetByteSize() const { return m_range.GetByteSize(); }
84
85 uint32_t GetPermissions() const { return m_permissions; }
86
87 uint32_t GetChunkSize() const { return m_chunk_size; }
88
89 bool Contains(lldb::addr_t addr) const {
90 return m_range.Contains(addr);
91 }
92
93protected:
94 uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
95
96 uint32_t CalculateChunksNeededForSize(uint32_t size) const {
97 return (size + m_chunk_size - 1) / m_chunk_size;
98 }
99 // Base address of this block of memory 4GB of chunk should be enough.
101 // Permissions for this memory (logical OR of lldb::Permissions bits)
102 const uint32_t m_permissions;
103 // The size of chunks that the memory at m_addr is divied up into.
104 const uint32_t m_chunk_size;
105 // A sorted list of free address ranges.
107 // A sorted list of reserved address.
109};
110
111// A class that can track allocated memory and give out allocated memory
112// without us having to make an allocate/deallocate call every time we need
113// some memory in a process that is being debugged.
115public:
116 // Constructors and Destructors
118
120
121 void Clear(bool deallocate_memory);
122
123 lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
124 Status &error);
125
127
128protected:
129 typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
130
131 AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
132 uint32_t chunk_size, Status &error);
133
134 // Classes that inherit from MemoryCache can see and modify these
136 std::recursive_mutex m_mutex;
137 typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
139
140private:
143};
144
145} // namespace lldb_private
146
147#endif // LLDB_TARGET_MEMORY_H
static llvm::raw_ostream & error(Stream &strm)
uint32_t CalculateChunksNeededForSize(uint32_t size) const
Definition: Memory.h:96
bool FreeBlock(lldb::addr_t addr)
Definition: Memory.cpp:330
uint32_t GetPermissions() const
Definition: Memory.h:85
lldb::addr_t GetBaseAddress() const
Definition: Memory.h:81
lldb::addr_t ReserveBlock(uint32_t size)
Definition: Memory.cpp:279
const uint32_t m_permissions
Definition: Memory.h:102
Range< lldb::addr_t, uint32_t > m_range
Definition: Memory.h:100
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
Definition: Memory.h:106
uint32_t TotalChunks() const
Definition: Memory.h:94
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
Definition: Memory.h:108
uint32_t GetChunkSize() const
Definition: Memory.h:87
const uint32_t m_chunk_size
Definition: Memory.h:104
uint32_t GetByteSize() const
Definition: Memory.h:83
bool Contains(lldb::addr_t addr) const
Definition: Memory.h:89
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
Definition: Memory.cpp:386
std::multimap< uint32_t, AllocatedBlockSP > PermissionsToBlockMap
Definition: Memory.h:137
const AllocatedMemoryCache & operator=(const AllocatedMemoryCache &)=delete
std::recursive_mutex m_mutex
Definition: Memory.h:136
void Clear(bool deallocate_memory)
Definition: Memory.cpp:349
bool DeallocateMemory(lldb::addr_t ptr)
Definition: Memory.cpp:417
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
Definition: Memory.h:129
PermissionsToBlockMap m_memory_map
Definition: Memory.h:138
AllocatedMemoryCache(const AllocatedMemoryCache &)=delete
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
Definition: Memory.cpp:360
uint32_t GetMemoryCacheLineSize() const
Definition: Memory.h:34
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:154
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:126
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:341
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:334
uint64_t addr_t
Definition: lldb-types.h:80
bool Contains(BaseType r) const
Definition: RangeMap.h:93
BaseType GetRangeBase() const
Definition: RangeMap.h:45
SizeType GetByteSize() const
Definition: RangeMap.h:87