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