32 AllocationMap::iterator iter;
38 if (iter->second.m_leak)
41 Free(iter->first, err);
62 const bool process_is_alive = process_sp && process_sp->IsAlive();
68 if (process_is_alive && process_sp->CanJIT()) {
71 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72 lldb::ePermissionsWritable,
91 size_t alloc_size = back->second.m_size;
92 ret = llvm::alignTo(addr + alloc_size, 4096);
95 uint64_t end_of_memory;
98 end_of_memory = 0xffffull;
101 end_of_memory = 0xffffffffull;
104 end_of_memory = 0xffffffffffffffffull;
118 if (process_is_alive && process_sp->CanJIT()) {
120 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
146 err = process_sp->GetMemoryRegionInfo(
149 lldbassert(0 &&
"GetMemoryRegionInfo() succeeded, then failed");
161 uint64_t alloc_address = target_sp->GetExprAllocAddress();
162 if (alloc_address > 0) {
163 if (alloc_address >= end_of_memory) {
164 lldbassert(0 &&
"The allocation address for expression evaluation must "
165 "be within process address space");
172 switch (address_byte_size) {
180 ret = 0xdead0fff00000000ull;
191 size_t alloc_size = back->second.m_size;
192 uint64_t align = target_sp->GetExprAllocAlign();
195 ret = llvm::alignTo(addr + alloc_size, align);
201IRMemoryMap::AllocationMap::iterator
206 AllocationMap::iterator iter =
m_allocations.lower_bound(addr);
214 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
224 AllocationMap::const_iterator iter =
m_allocations.lower_bound(addr);
235 iter->second.m_size))
242 iter->second.m_size))
261 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
268 return process_sp->GetByteOrder();
273 return target_sp->GetArchitecture().GetByteOrder();
282 return process_sp->GetAddressByteSize();
287 return target_sp->GetArchitecture().GetAddressByteSize();
296 return process_sp.get();
301 return target_sp.get();
308 uint32_t permissions, uint8_t alignment,
315 llvm_unreachable(
"Invalid AllocationPolicy");
325llvm::Expected<lldb::addr_t>
335 size_t allocation_size;
340 allocation_size = alignment;
343 allocation_size = llvm::alignTo(size, alignment);
348 allocation_size += alignment - 1;
353 return llvm::createStringError(
354 llvm::inconvertibleErrorCode(),
355 "Couldn't malloc: invalid allocation policy");
357 allocation_address =
FindSpace(allocation_size);
359 return llvm::createStringError(llvm::inconvertibleErrorCode(),
360 "Couldn't malloc: address space is full");
365 "IRMemoryMap::%s process_sp=0x%" PRIxPTR
366 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
367 __FUNCTION__,
reinterpret_cast<uintptr_t
>(process_sp.get()),
368 process_sp && process_sp->CanJIT() ?
"true" :
"false",
369 process_sp && process_sp->IsAlive() ?
"true" :
"false");
370 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
374 process_sp->AllocateMemory(allocation_size, permissions,
error);
377 process_sp->CallocateMemory(allocation_size, permissions,
error);
379 if (!
error.Success())
380 return error.takeError();
383 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
384 "due to failed condition (see previous expr log message)",
387 allocation_address =
FindSpace(allocation_size);
389 return llvm::createStringError(
390 llvm::inconvertibleErrorCode(),
391 "Couldn't malloc: address space is full");
397 if (process_sp->CanJIT() && process_sp->IsAlive()) {
401 process_sp->AllocateMemory(allocation_size, permissions,
error);
404 process_sp->CallocateMemory(allocation_size, permissions,
error);
406 if (!
error.Success())
407 return error.takeError();
409 return llvm::createStringError(
410 llvm::inconvertibleErrorCode(),
411 "Couldn't malloc: process doesn't support allocating memory");
414 return llvm::createStringError(llvm::inconvertibleErrorCode(),
415 "Couldn't malloc: process doesn't exist, "
416 "and this memory must be in the process");
422 aligned_address = (allocation_address + mask) & (~mask);
425 std::piecewise_construct, std::forward_as_tuple(aligned_address),
426 std::forward_as_tuple(allocation_address, aligned_address,
427 allocation_size, permissions, alignment, policy));
431 std::vector<uint8_t> zero_buf(size, 0);
432 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
436 const char *policy_string;
440 policy_string =
"<invalid policy>";
443 policy_string =
"eAllocationPolicyHostOnly";
446 policy_string =
"eAllocationPolicyProcessOnly";
449 policy_string =
"eAllocationPolicyMirror";
454 "IRMemoryMap::Malloc (%" PRIu64
", 0x%" PRIx64
", 0x%" PRIx64
455 ", %s) -> 0x%" PRIx64,
456 (uint64_t)allocation_size, (uint64_t)alignment,
457 (uint64_t)permissions, policy_string, aligned_address);
461 *used_policy = policy;
463 return aligned_address;
469 AllocationMap::iterator iter =
m_allocations.find(process_address);
484 AllocationMap::iterator iter =
m_allocations.find(process_address);
498 if (process_sp->CanJIT() && process_sp->IsAlive())
499 process_sp->DeallocateMemory(
515 "IRMemoryMap::Free (0x%" PRIx64
") freed [0x%" PRIx64
517 (uint64_t)process_address, iter->second.m_process_start,
518 iter->second.m_process_start + iter->second.m_size);
547 const uint8_t *bytes,
size_t size,
551 AllocationMap::iterator iter =
FindAllocation(process_address, size);
557 process_sp->WriteMemory(process_address, bytes, size,
error);
562 "Couldn't write: no allocation contains the target "
563 "range and the process doesn't exist");
593 process_sp->WriteMemory(process_address, bytes, size,
error);
594 if (!
error.Success())
601 process_sp->WriteMemory(process_address, bytes, size,
error);
602 if (!
error.Success())
610 "IRMemoryMap::WriteMemory (0x%" PRIx64
", 0x%" PRIxPTR
611 ", 0x%" PRId64
") went to [0x%" PRIx64
"..0x%" PRIx64
")",
612 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
615 (uint64_t)allocation.
m_size);
620 Scalar &scalar,
size_t size,
629 const size_t mem_size =
635 "Couldn't write scalar: failed to get scalar as memory data");
653 pointer = process_sp->FixAnyAddress(pointer);
664 AllocationMap::iterator iter =
FindAllocation(process_address, size);
670 process_sp->ReadMemory(process_address, bytes, size,
error);
677 Address absolute_address(process_address);
678 target_sp->ReadMemory(absolute_address, bytes, size,
error,
true);
683 "Couldn't read: no allocation contains the target "
684 "range, and neither the process nor the target exist");
692 if (offset > allocation.
m_size) {
720 process_sp->ReadMemory(process_address, bytes, size,
error);
721 if (!
error.Success())
734 process_sp->ReadMemory(process_address, bytes, size,
error);
735 if (!
error.Success())
743 "IRMemoryMap::ReadMemory (0x%" PRIx64
", 0x%" PRIxPTR
744 ", 0x%" PRId64
") came from [0x%" PRIx64
"..0x%" PRIx64
")",
745 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
748 (uint64_t)allocation.
m_size);
761 if (!
error.Success())
772 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
775 scalar = extractor.
GetU8(&offset);
778 scalar = extractor.
GetU16(&offset);
781 scalar = extractor.
GetU32(&offset);
784 scalar = extractor.
GetU64(&offset);
801 if (!
error.Success())
813 AllocationMap::iterator iter =
FindAllocation(process_address, size);
817 "Couldn't find an allocation containing [0x%" PRIx64
"..0x%" PRIx64
819 process_address, process_address + size);
828 "Couldn't get memory data: invalid allocation policy");
832 "Couldn't get memory data: memory is only in the target");
839 "Couldn't get memory data: data buffer is empty");
846 if (!
error.Success())
857 "Couldn't get memory data: data buffer is empty");
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
A section + offset based address class.
A subclass of DataBuffer that stores a data buffer on the heap.
lldb::offset_t GetByteSize() const override
Get the number of bytes in the data buffer.
"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
bool IntersectsAllocation(lldb::addr_t addr, size_t size) const
ExecutionContextScope * GetBestExecutionContextScope() const
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()
AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size)
uint32_t GetAddressByteSize()
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.
@ eAllocationPolicyHostOnly
This allocation was created in the host and will never make it into the process.
@ eAllocationPolicyMirror
The intent is that this allocation exist both in the host and the process and have the same content i...
LazyBool GetReadable() const
LazyBool GetExecutable() const
LazyBool GetWritable() const
size_t GetByteSize() const
unsigned long long ULongLong(unsigned long long fail_value=0) const
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
void Clear()
Clear the object state.
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
static Status FromErrorString(const char *str)
bool Fail() const
Test for error condition.
bool Success() const
Test for success condition.
uint8_t * GetBytes()
Get a pointer to the data.
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
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.
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.
uint8_t m_alignment
The alignment of the requested allocation.
BaseType GetRangeBase() const
BaseType GetRangeEnd() const