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
18namespace 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.
35public:
36 IRMemoryMap(lldb::TargetSP target_sp);
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);
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
75 uint32_t GetAddressByteSize();
76
77 // This function can return NULL.
79
81
82protected:
83 // This function should only be used if you know you are using the JIT. Any
84 // other cases should use GetBestExecutionContextScope().
85
87
88private:
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
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
static llvm::raw_ostream & error(Stream &strm)
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Definition: DataExtractor.h:48
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
Encapsulates memory that may exist in the process but must also be available in the host process.
Definition: IRMemoryMap.h:34
lldb::TargetWP m_target_wp
Definition: IRMemoryMap.h:119
void Free(lldb::addr_t process_address, Status &error)
lldb::ByteOrder GetByteOrder()
void ReadPointerFromMemory(lldb::addr_t *address, lldb::addr_t process_address, Status &error)
lldb::ProcessWP m_process_wp
Definition: IRMemoryMap.h:118
AllocationMap m_allocations
Definition: IRMemoryMap.h:121
lldb::TargetSP GetTarget()
Definition: IRMemoryMap.h:80
bool IntersectsAllocation(lldb::addr_t addr, size_t size) const
ExecutionContextScope * GetBestExecutionContextScope() const
lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Status &error)
std::map< lldb::addr_t, Allocation > AllocationMap
Definition: IRMemoryMap.h:120
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:86
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address, Status &error)
AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size)
void ReadScalarFromMemory(Scalar &scalar, lldb::addr_t process_address, size_t size, Status &error)
lldb::addr_t FindSpace(size_t size)
Definition: IRMemoryMap.cpp:46
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:49
@ eAllocationPolicyHostOnly
This allocation was created in the host and will never make it into the process.
Definition: IRMemoryMap.h:42
@ 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
@ eAllocationPolicyInvalid
It is an error for an allocation to have this policy.
Definition: IRMemoryMap.h:40
An error handling class.
Definition: Status.h:44
A class that represents a running process on the host machine.
ByteOrder
Byte ordering definitions.
std::weak_ptr< lldb_private::Process > ProcessWP
Definition: lldb-forward.h:390
uint64_t addr_t
Definition: lldb-types.h:80
std::weak_ptr< lldb_private::Target > TargetWP
Definition: lldb-forward.h:445
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:444
size_t m_size
The size of the requested allocation.
Definition: IRMemoryMap.h:94
lldb::addr_t m_process_alloc
The (unaligned) base for the remote allocation.
Definition: IRMemoryMap.h:91
Allocation(const Allocation &)=delete
AllocationPolicy m_policy
Flags. Keep these grouped together to avoid structure padding.
Definition: IRMemoryMap.h:98
uint8_t m_permissions
The access permissions on the memory in the process.
Definition: IRMemoryMap.h:100
lldb::addr_t m_process_start
The base address of the allocation in the process.
Definition: IRMemoryMap.h:93
const Allocation & operator=(const Allocation &)=delete
uint8_t m_alignment
The alignment of the requested allocation.
Definition: IRMemoryMap.h:103