LLDB  mainline
IRMemoryMap.h
Go to the documentation of this file.
1 //===-- IRMemoryMap.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_EXPRESSION_IRMEMORYMAP_H
10 #define LLDB_EXPRESSION_IRMEMORYMAP_H
11 
13 #include "lldb/Utility/UserID.h"
14 #include "lldb/lldb-public.h"
15 
16 #include <map>
17 
18 namespace lldb_private {
19 
20 /// \class IRMemoryMap IRMemoryMap.h "lldb/Expression/IRMemoryMap.h"
21 /// Encapsulates memory that may exist in the process but must
22 /// also be available in the host process.
23 ///
24 /// This class encapsulates a group of memory objects that must be readable or
25 /// writable from the host process regardless of whether the process exists.
26 /// This allows the IR interpreter as well as JITted code to access the same
27 /// memory. All allocations made by this class are represented as disjoint
28 /// intervals.
29 ///
30 /// Point queries against this group of memory objects can be made by the
31 /// address in the tar at which they reside. If the inferior does not exist,
32 /// allocations still get made-up addresses. If an inferior appears at some
33 /// point, then those addresses need to be re-mapped.
34 class IRMemoryMap {
35 public:
36  IRMemoryMap(lldb::TargetSP target_sp);
37  ~IRMemoryMap();
38 
39  enum AllocationPolicy : uint8_t {
41  0, ///< It is an error for an allocation to have this policy.
42  eAllocationPolicyHostOnly, ///< This allocation was created in the host and
43  ///will never make it into the process.
44  ///< It is an error to create other types of allocations while such
45  ///allocations exist.
46  eAllocationPolicyMirror, ///< The intent is that this allocation exist both
47  ///in the host and the process and have
48  ///< the same content in both.
49  eAllocationPolicyProcessOnly ///< The intent is that this allocation exist
50  ///only in the process.
51  };
52 
53  lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions,
54  AllocationPolicy policy, bool zero_memory, Status &error);
55  void Leak(lldb::addr_t process_address, Status &error);
56  void Free(lldb::addr_t process_address, Status &error);
57 
58  void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes,
59  size_t size, Status &error);
60  void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar,
61  size_t size, Status &error);
62  void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address,
63  Status &error);
64  void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size,
65  Status &error);
66  void ReadScalarFromMemory(Scalar &scalar, lldb::addr_t process_address,
67  size_t size, Status &error);
68  void ReadPointerFromMemory(lldb::addr_t *address,
69  lldb::addr_t process_address, Status &error);
70  bool GetAllocSize(lldb::addr_t address, size_t &size);
71  void GetMemoryData(DataExtractor &extractor, lldb::addr_t process_address,
72  size_t size, Status &error);
73 
76 
77  // This function can return NULL.
79 
80  lldb::TargetSP GetTarget() { return m_target_wp.lock(); }
81 
82 protected:
83  // This function should only be used if you know you are using the JIT. Any
84  // other cases should use GetBestExecutionContextScope().
85 
86  lldb::ProcessWP &GetProcessWP() { return m_process_wp; }
87 
88 private:
89  struct Allocation {
91  m_process_alloc; ///< The (unaligned) base for the remote allocation.
93  m_process_start; ///< The base address of the allocation in the process.
94  size_t m_size; ///< The size of the requested allocation.
96 
97  /// Flags. Keep these grouped together to avoid structure padding.
99  bool m_leak;
100  uint8_t m_permissions; ///< The access permissions on the memory in the
101  /// process. In the host, the memory is always
102  /// read/write.
103  uint8_t m_alignment; ///< The alignment of the requested allocation.
104 
105  public:
106  Allocation(lldb::addr_t process_alloc, lldb::addr_t process_start,
107  size_t size, uint32_t permissions, uint8_t alignment,
109 
110  Allocation(const Allocation &) = delete;
111  const Allocation &operator=(const Allocation &) = delete;
112  };
113 
114  static_assert(sizeof(Allocation) <=
115  (4 * sizeof(lldb::addr_t)) + sizeof(DataBufferHeap),
116  "IRMemoryMap::Allocation is larger than expected");
117 
118  lldb::ProcessWP m_process_wp;
119  lldb::TargetWP m_target_wp;
120  typedef std::map<lldb::addr_t, Allocation> AllocationMap;
122 
123  lldb::addr_t FindSpace(size_t size);
125  AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size);
126 
127  // Returns true if the given allocation intersects any allocation in the
128  // memory map.
129  bool IntersectsAllocation(lldb::addr_t addr, size_t size) const;
130 
131  // Returns true if the two given allocations intersect each other.
132  static bool AllocationsIntersect(lldb::addr_t addr1, size_t size1,
133  lldb::addr_t addr2, size_t size2);
134 };
135 }
136 
137 #endif
lldb-public.h
lldb_private::IRMemoryMap::ContainsHostOnlyAllocations
bool ContainsHostOnlyAllocations()
lldb_private::IRMemoryMap::AllocationPolicy
AllocationPolicy
Definition: IRMemoryMap.h:39
lldb_private::IRMemoryMap::Allocation::m_permissions
uint8_t m_permissions
The access permissions on the memory in the process.
Definition: IRMemoryMap.h:100
lldb_private::IRMemoryMap::Allocation::m_alignment
uint8_t m_alignment
The alignment of the requested allocation.
Definition: IRMemoryMap.h:103
lldb_private::Scalar
Definition: Scalar.h:34
lldb_private::IRMemoryMap::Malloc
lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Status &error)
Definition: IRMemoryMap.cpp:288
lldb_private::IRMemoryMap::FindAllocation
AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size)
Definition: IRMemoryMap.cpp:165
lldb_private::IRMemoryMap::GetProcessWP
lldb::ProcessWP & GetProcessWP()
Definition: IRMemoryMap.h:86
lldb_private::IRMemoryMap::eAllocationPolicyHostOnly
@ eAllocationPolicyHostOnly
This allocation was created in the host and will never make it into the process.
Definition: IRMemoryMap.h:42
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::IRMemoryMap::AllocationsIntersect
static bool AllocationsIntersect(lldb::addr_t addr1, size_t size1, lldb::addr_t addr2, size_t size2)
Definition: IRMemoryMap.cpp:212
lldb_private::IRMemoryMap::GetTarget
lldb::TargetSP GetTarget()
Definition: IRMemoryMap.h:80
lldb_private::IRMemoryMap::FindSpace
lldb::addr_t FindSpace(size_t size)
Definition: IRMemoryMap.cpp:45
lldb_private::IRMemoryMap::WriteScalarToMemory
void WriteScalarToMemory(lldb::addr_t process_address, Scalar &scalar, size_t size, Status &error)
Definition: IRMemoryMap.cpp:589
lldb_private::IRMemoryMap::Free
void Free(lldb::addr_t process_address, Status &error)
Definition: IRMemoryMap.cpp:446
lldb_private::IRMemoryMap::Allocation::m_size
size_t m_size
The size of the requested allocation.
Definition: IRMemoryMap.h:94
lldb_private::IRMemoryMap::Allocation::operator=
const Allocation & operator=(const Allocation &)=delete
lldb_private::IRMemoryMap::GetByteOrder
lldb::ByteOrder GetByteOrder()
Definition: IRMemoryMap.cpp:227
lldb_private::IRMemoryMap::Allocation::m_data
DataBufferHeap m_data
Definition: IRMemoryMap.h:95
error
static llvm::raw_ostream & error(Stream &strm)
Definition: CommandReturnObject.cpp:17
lldb_private::DataExtractor
Definition: DataExtractor.h:48
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::IRMemoryMap::IRMemoryMap
IRMemoryMap(lldb::TargetSP target_sp)
Definition: IRMemoryMap.cpp:22
lldb_private::IRMemoryMap::eAllocationPolicyMirror
@ eAllocationPolicyMirror
The intent is that this allocation exist both in the host and the process and have the same content i...
Definition: IRMemoryMap.h:46
lldb_private::IRMemoryMap::Leak
void Leak(lldb::addr_t process_address, Status &error)
Definition: IRMemoryMap.cpp:430
lldb_private::IRMemoryMap::GetAddressByteSize
uint32_t GetAddressByteSize()
Definition: IRMemoryMap.cpp:241
lldb_private::IRMemoryMap::ReadScalarFromMemory
void ReadScalarFromMemory(Scalar &scalar, lldb::addr_t process_address, size_t size, Status &error)
Definition: IRMemoryMap.cpp:720
lldb_private::IRMemoryMap::Allocation::m_process_alloc
lldb::addr_t m_process_alloc
The (unaligned) base for the remote allocation.
Definition: IRMemoryMap.h:91
lldb_private::IRMemoryMap::~IRMemoryMap
~IRMemoryMap()
Definition: IRMemoryMap.cpp:27
lldb_private::IRMemoryMap::Allocation::m_policy
AllocationPolicy m_policy
Flags. Keep these grouped together to avoid structure padding.
Definition: IRMemoryMap.h:98
lldb_private::IRMemoryMap::eAllocationPolicyInvalid
@ eAllocationPolicyInvalid
It is an error for an allocation to have this policy.
Definition: IRMemoryMap.h:40
lldb_private::IRMemoryMap::Allocation::m_leak
bool m_leak
Definition: IRMemoryMap.h:99
UserID.h
lldb_private::IRMemoryMap::ReadMemory
void ReadMemory(uint8_t *bytes, lldb::addr_t process_address, size_t size, Status &error)
Definition: IRMemoryMap.cpp:624
lldb_private::IRMemoryMap::Allocation::m_process_start
lldb::addr_t m_process_start
The base address of the allocation in the process.
Definition: IRMemoryMap.h:93
lldb_private::Status
Definition: Status.h:44
lldb_private::IRMemoryMap::WriteMemory
void WriteMemory(lldb::addr_t process_address, const uint8_t *bytes, size_t size, Status &error)
Definition: IRMemoryMap.cpp:513
uint32_t
lldb_private::IRMemoryMap::GetMemoryData
void GetMemoryData(DataExtractor &extractor, lldb::addr_t process_address, size_t size, Status &error)
Definition: IRMemoryMap.cpp:780
lldb_private::IRMemoryMap::AllocationMap
std::map< lldb::addr_t, Allocation > AllocationMap
Definition: IRMemoryMap.h:120
lldb_private::IRMemoryMap::WritePointerToMemory
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address, Status &error)
Definition: IRMemoryMap.cpp:615
lldb_private::IRMemoryMap::Allocation
Definition: IRMemoryMap.h:89
lldb_private::IRMemoryMap::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: IRMemoryMap.cpp:255
lldb_private::IRMemoryMap::m_allocations
AllocationMap m_allocations
Definition: IRMemoryMap.h:121
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::IRMemoryMap::Allocation::Allocation
Allocation(lldb::addr_t process_alloc, lldb::addr_t process_start, size_t size, uint32_t permissions, uint8_t alignment, AllocationPolicy m_policy)
Definition: IRMemoryMap.cpp:269
lldb_private::IRMemoryMap::ReadPointerFromMemory
void ReadPointerFromMemory(lldb::addr_t *address, lldb::addr_t process_address, Status &error)
Definition: IRMemoryMap.cpp:763
lldb_private::IRMemoryMap::eAllocationPolicyProcessOnly
@ eAllocationPolicyProcessOnly
The intent is that this allocation exist only in the process.
Definition: IRMemoryMap.h:49
lldb_private::IRMemoryMap::GetAllocSize
bool GetAllocSize(lldb::addr_t address, size_t &size)
Definition: IRMemoryMap.cpp:491
lldb_private::DataBufferHeap
Definition: DataBufferHeap.h:30
lldb_private::IRMemoryMap::m_target_wp
lldb::TargetWP m_target_wp
Definition: IRMemoryMap.h:119
lldb_private::IRMemoryMap
Definition: IRMemoryMap.h:34
DataBufferHeap.h
lldb_private::IRMemoryMap::m_process_wp
lldb::ProcessWP m_process_wp
Definition: IRMemoryMap.h:116
lldb_private::IRMemoryMap::IntersectsAllocation
bool IntersectsAllocation(lldb::addr_t addr, size_t size) const
Definition: IRMemoryMap.cpp:183
lldb::ByteOrder
ByteOrder
Byte ordering definitions.
Definition: lldb-enumerations.h:138