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 
12 #include "lldb/Utility/RangeMap.h"
13 #include "lldb/lldb-private.h"
14 #include <map>
15 #include <mutex>
16 #include <vector>
17 
18 namespace lldb_private {
19 // A class to track memory that was read from a live process between
20 // runs.
21 class MemoryCache {
22 public:
23  // Constructors and Destructors
24  MemoryCache(Process &process);
25 
26  ~MemoryCache();
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 
43  void AddL1CacheData(lldb::addr_t addr,
44  const lldb::DataBufferSP &data_buffer_sp);
45 
46 protected:
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 
61 private:
62  MemoryCache(const MemoryCache &) = delete;
63  const MemoryCache &operator=(const MemoryCache &) = delete;
64 };
65 
66 
67 
69 public:
70  AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions,
71  uint32_t chunk_size);
72 
74 
76 
77  bool FreeBlock(lldb::addr_t addr);
78 
80 
81  uint32_t GetByteSize() const { return m_range.GetByteSize(); }
82 
84 
85  uint32_t GetChunkSize() const { return m_chunk_size; }
86 
87  bool Contains(lldb::addr_t addr) const {
88  return m_range.Contains(addr);
89  }
90 
91 protected:
92  uint32_t TotalChunks() const { return GetByteSize() / GetChunkSize(); }
93 
95  return (size + m_chunk_size - 1) / m_chunk_size;
96  }
97  // Base address of this block of memory 4GB of chunk should be enough.
99  // Permissions for this memory (logical OR of lldb::Permissions bits)
101  // The size of chunks that the memory at m_addr is divied up into.
103  // A sorted list of free address ranges.
105  // A sorted list of reserved address.
107 };
108 
109 // A class that can track allocated memory and give out allocated memory
110 // without us having to make an allocate/deallocate call every time we need
111 // some memory in a process that is being debugged.
113 public:
114  // Constructors and Destructors
115  AllocatedMemoryCache(Process &process);
116 
118 
119  void Clear();
120 
121  lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions,
122  Status &error);
123 
124  bool DeallocateMemory(lldb::addr_t ptr);
125 
126 protected:
127  typedef std::shared_ptr<AllocatedBlock> AllocatedBlockSP;
128 
129  AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions,
130  uint32_t chunk_size, Status &error);
131 
132  // Classes that inherit from MemoryCache can see and modify these
134  std::recursive_mutex m_mutex;
135  typedef std::multimap<uint32_t, AllocatedBlockSP> PermissionsToBlockMap;
137 
138 private:
139  AllocatedMemoryCache(const AllocatedMemoryCache &) = delete;
140  const AllocatedMemoryCache &operator=(const AllocatedMemoryCache &) = delete;
141 };
142 
143 } // namespace lldb_private
144 
145 #endif // LLDB_TARGET_MEMORY_H
lldb_private::AllocatedBlock::ReserveBlock
lldb::addr_t ReserveBlock(uint32_t size)
Definition: Memory.cpp:264
lldb_private::Range::GetRangeBase
BaseType GetRangeBase() const
Definition: RangeMap.h:46
lldb_private::AllocatedMemoryCache::~AllocatedMemoryCache
~AllocatedMemoryCache()
lldb_private::MemoryCache::BlockMap
std::map< lldb::addr_t, lldb::DataBufferSP > BlockMap
Definition: Memory.h:47
lldb_private::MemoryCache::AddrRange
Range< lldb::addr_t, lldb::addr_t > AddrRange
Definition: Memory.h:49
lldb_private::MemoryCache::m_L1_cache
BlockMap m_L1_cache
Definition: Memory.h:52
lldb_private::MemoryCache::m_process
Process & m_process
Definition: Memory.h:58
lldb_private::AllocatedMemoryCache::PermissionsToBlockMap
std::multimap< uint32_t, AllocatedBlockSP > PermissionsToBlockMap
Definition: Memory.h:135
lldb_private::AllocatedBlock::AllocatedBlock
AllocatedBlock(lldb::addr_t addr, uint32_t byte_size, uint32_t permissions, uint32_t chunk_size)
Definition: Memory.cpp:252
lldb_private::MemoryCache::RemoveInvalidRange
bool RemoveInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition: Memory.cpp:110
lldb_private::Process
Definition: Process.h:341
lldb_private::AllocatedBlock::m_chunk_size
const uint32_t m_chunk_size
Definition: Memory.h:102
lldb_private::AllocatedBlock::Contains
bool Contains(lldb::addr_t addr) const
Definition: Memory.h:87
lldb_private::AllocatedMemoryCache::DeallocateMemory
bool DeallocateMemory(lldb::addr_t ptr)
Definition: Memory.cpp:402
lldb_private::AllocatedBlock
Definition: Memory.h:68
lldb_private::AllocatedMemoryCache::AllocatedMemoryCache
AllocatedMemoryCache(Process &process)
Definition: Memory.cpp:329
lldb_private::AllocatedBlock::m_reserved_blocks
RangeVector< lldb::addr_t, uint32_t > m_reserved_blocks
Definition: Memory.h:106
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
RangeMap.h
lldb_private::MemoryCache::Flush
void Flush(lldb::addr_t addr, size_t size)
Definition: Memory.cpp:52
lldb_private::AllocatedBlock::TotalChunks
uint32_t TotalChunks() const
Definition: Memory.h:92
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::AllocatedBlock::m_permissions
const uint32_t m_permissions
Definition: Memory.h:100
lldb_private::MemoryCache::m_mutex
std::recursive_mutex m_mutex
Definition: Memory.h:51
lldb_private::MemoryCache::GetMemoryCacheLineSize
uint32_t GetMemoryCacheLineSize() const
Definition: Memory.h:34
lldb_private::AllocatedMemoryCache::m_process
Process & m_process
Definition: Memory.h:133
lldb_private::AllocatedBlock::CalculateChunksNeededForSize
uint32_t CalculateChunksNeededForSize(uint32_t size) const
Definition: Memory.h:94
lldb_private::Range< lldb::addr_t, lldb::addr_t >
lldb_private::AllocatedBlock::m_range
Range< lldb::addr_t, uint32_t > m_range
Definition: Memory.h:98
lldb_private::MemoryCache::operator=
const MemoryCache & operator=(const MemoryCache &)=delete
lldb_private::MemoryCache::AddInvalidRange
void AddInvalidRange(lldb::addr_t base_addr, lldb::addr_t byte_size)
Definition: Memory.cpp:100
lldb_private::RangeVector< lldb::addr_t, lldb::addr_t, 4 >
lldb_private::AllocatedMemoryCache::m_mutex
std::recursive_mutex m_mutex
Definition: Memory.h:134
lldb_private::AllocatedBlock::FreeBlock
bool FreeBlock(lldb::addr_t addr)
Definition: Memory.cpp:315
lldb-private.h
lldb_private::AllocatedMemoryCache::AllocateMemory
lldb::addr_t AllocateMemory(size_t byte_size, uint32_t permissions, Status &error)
Definition: Memory.cpp:371
lldb_private::MemoryCache::MemoryCache
MemoryCache(Process &process)
Definition: Memory.cpp:23
lldb_private::AllocatedMemoryCache::operator=
const AllocatedMemoryCache & operator=(const AllocatedMemoryCache &)=delete
lldb_private::MemoryCache::m_invalid_ranges
InvalidRanges m_invalid_ranges
Definition: Memory.h:57
lldb_private::AllocatedBlock::GetChunkSize
uint32_t GetChunkSize() const
Definition: Memory.h:85
lldb_private::Status
Definition: Status.h:44
lldb_private::AllocatedBlock::GetPermissions
uint32_t GetPermissions() const
Definition: Memory.h:83
lldb_private::MemoryCache::~MemoryCache
~MemoryCache()
uint32_t
lldb_private::AllocatedMemoryCache
Definition: Memory.h:112
lldb_private::AllocatedMemoryCache::AllocatePage
AllocatedBlockSP AllocatePage(uint32_t byte_size, uint32_t permissions, uint32_t chunk_size, Status &error)
Definition: Memory.cpp:345
lldb_private::MemoryCache::Read
size_t Read(lldb::addr_t addr, void *dst, size_t dst_len, Status &error)
Definition: Memory.cpp:125
lldb_private::AllocatedMemoryCache::m_memory_map
PermissionsToBlockMap m_memory_map
Definition: Memory.h:136
lldb_private::MemoryCache::m_L2_cache_line_byte_size
uint32_t m_L2_cache_line_byte_size
Definition: Memory.h:59
lldb_private::MemoryCache
Definition: Memory.h:21
lldb_private::MemoryCache::m_L2_cache
BlockMap m_L2_cache
Definition: Memory.h:55
lldb_private::Range::GetByteSize
SizeType GetByteSize() const
Definition: RangeMap.h:71
lldb_private::MemoryCache::AddL1CacheData
void AddL1CacheData(lldb::addr_t addr, const void *src, size_t src_len)
Definition: Memory.cpp:40
lldb_private::AllocatedBlock::GetByteSize
uint32_t GetByteSize() const
Definition: Memory.h:81
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::AllocatedBlock::m_free_blocks
RangeVector< lldb::addr_t, uint32_t > m_free_blocks
Definition: Memory.h:104
lldb_private::AllocatedMemoryCache::Clear
void Clear()
Definition: Memory.cpp:334
lldb_private::AllocatedBlock::GetBaseAddress
lldb::addr_t GetBaseAddress() const
Definition: Memory.h:79
lldb_private::AllocatedBlock::~AllocatedBlock
~AllocatedBlock()
lldb_private::MemoryCache::InvalidRanges
RangeVector< lldb::addr_t, lldb::addr_t, 4 > InvalidRanges
Definition: Memory.h:48
lldb_private::MemoryCache::Clear
void Clear(bool clear_invalid_ranges=false)
Definition: Memory.cpp:31
lldb_private::Range::Contains
bool Contains(BaseType r) const
Definition: RangeMap.h:77
lldb_private::AllocatedMemoryCache::AllocatedBlockSP
std::shared_ptr< AllocatedBlock > AllocatedBlockSP
Definition: Memory.h:127