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;
114 if (process_is_alive) {
116 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
135 err = process_sp->GetMemoryRegionInfo(
138 lldbassert(0 &&
"GetMemoryRegionInfo() succeeded, then failed");
150 uint64_t alloc_address = target_sp->GetExprAllocAddress();
151 if (alloc_address > 0) {
152 if (alloc_address >= end_of_memory) {
153 lldbassert(0 &&
"The allocation address for expression evaluation must "
154 "be within process address space");
161 switch (address_byte_size) {
169 ret = 0xdead0fff00000000ull;
180 size_t alloc_size = back->second.m_size;
181 uint64_t align = target_sp->GetExprAllocAlign();
184 ret = llvm::alignTo(addr + alloc_size, align);
190IRMemoryMap::AllocationMap::iterator
195 AllocationMap::iterator iter =
m_allocations.lower_bound(addr);
203 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
213 AllocationMap::const_iterator iter =
m_allocations.lower_bound(addr);
224 iter->second.m_size))
231 iter->second.m_size))
250 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
257 return process_sp->GetByteOrder();
262 return target_sp->GetArchitecture().GetByteOrder();
271 return process_sp->GetAddressByteSize();
276 return target_sp->GetArchitecture().GetAddressByteSize();
285 return process_sp.get();
290 return target_sp.get();
297 uint32_t permissions, uint8_t alignment,
299 : m_process_alloc(process_alloc), m_process_start(process_start),
300 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
301 m_alignment(alignment) {
304 llvm_unreachable(
"Invalid AllocationPolicy");
324 size_t allocation_size;
329 allocation_size = alignment;
332 allocation_size = llvm::alignTo(size, alignment);
337 allocation_size += alignment - 1;
342 error.SetErrorToGenericError();
343 error.SetErrorString(
"Couldn't malloc: invalid allocation policy");
346 allocation_address =
FindSpace(allocation_size);
348 error.SetErrorToGenericError();
349 error.SetErrorString(
"Couldn't malloc: address space is full");
356 "IRMemoryMap::%s process_sp=0x%" PRIxPTR
357 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
358 __FUNCTION__,
reinterpret_cast<uintptr_t
>(process_sp.get()),
359 process_sp && process_sp->CanJIT() ?
"true" :
"false",
360 process_sp && process_sp->IsAlive() ?
"true" :
"false");
361 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
364 process_sp->AllocateMemory(allocation_size, permissions,
error);
367 process_sp->CallocateMemory(allocation_size, permissions,
error);
369 if (!
error.Success())
373 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
374 "due to failed condition (see previous expr log message)",
377 allocation_address =
FindSpace(allocation_size);
379 error.SetErrorToGenericError();
380 error.SetErrorString(
"Couldn't malloc: address space is full");
388 if (process_sp->CanJIT() && process_sp->IsAlive()) {
391 process_sp->AllocateMemory(allocation_size, permissions,
error);
394 process_sp->CallocateMemory(allocation_size, permissions,
error);
396 if (!
error.Success())
399 error.SetErrorToGenericError();
400 error.SetErrorString(
401 "Couldn't malloc: process doesn't support allocating memory");
405 error.SetErrorToGenericError();
406 error.SetErrorString(
"Couldn't malloc: process doesn't exist, and this "
407 "memory must be in the process");
414 aligned_address = (allocation_address + mask) & (~mask);
417 std::piecewise_construct, std::forward_as_tuple(aligned_address),
418 std::forward_as_tuple(allocation_address, aligned_address,
419 allocation_size, permissions, alignment, policy));
423 std::vector<uint8_t> zero_buf(size, 0);
424 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
428 const char *policy_string;
432 policy_string =
"<invalid policy>";
435 policy_string =
"eAllocationPolicyHostOnly";
438 policy_string =
"eAllocationPolicyProcessOnly";
441 policy_string =
"eAllocationPolicyMirror";
446 "IRMemoryMap::Malloc (%" PRIu64
", 0x%" PRIx64
", 0x%" PRIx64
447 ", %s) -> 0x%" PRIx64,
448 (uint64_t)allocation_size, (uint64_t)alignment,
449 (uint64_t)permissions, policy_string, aligned_address);
452 return aligned_address;
458 AllocationMap::iterator iter =
m_allocations.find(process_address);
461 error.SetErrorToGenericError();
462 error.SetErrorString(
"Couldn't leak: allocation doesn't exist");
474 AllocationMap::iterator iter =
m_allocations.find(process_address);
477 error.SetErrorToGenericError();
478 error.SetErrorString(
"Couldn't free: allocation doesn't exist");
489 if (process_sp->CanJIT() && process_sp->IsAlive())
490 process_sp->DeallocateMemory(
506 "IRMemoryMap::Free (0x%" PRIx64
") freed [0x%" PRIx64
508 (uint64_t)process_address, iter->second.m_process_start,
509 iter->second.m_process_start + iter->second.m_size);
538 const uint8_t *bytes,
size_t size,
542 AllocationMap::iterator iter =
FindAllocation(process_address, size);
548 process_sp->WriteMemory(process_address, bytes, size,
error);
552 error.SetErrorToGenericError();
553 error.SetErrorString(
"Couldn't write: no allocation contains the target "
554 "range and the process doesn't exist");
566 error.SetErrorToGenericError();
567 error.SetErrorString(
"Couldn't write: invalid allocation policy");
571 error.SetErrorToGenericError();
572 error.SetErrorString(
"Couldn't write: data buffer is empty");
579 error.SetErrorToGenericError();
580 error.SetErrorString(
"Couldn't write: data buffer is empty");
586 process_sp->WriteMemory(process_address, bytes, size,
error);
587 if (!
error.Success())
594 process_sp->WriteMemory(process_address, bytes, size,
error);
595 if (!
error.Success())
603 "IRMemoryMap::WriteMemory (0x%" PRIx64
", 0x%" PRIxPTR
604 ", 0x%" PRId64
") went to [0x%" PRIx64
"..0x%" PRIx64
")",
605 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
608 (uint64_t)allocation.
m_size);
613 Scalar &scalar,
size_t size,
622 const size_t mem_size =
627 error.SetErrorToGenericError();
628 error.SetErrorString(
629 "Couldn't write scalar: failed to get scalar as memory data");
632 error.SetErrorToGenericError();
633 error.SetErrorString(
"Couldn't write scalar: its size was zero");
650 AllocationMap::iterator iter =
FindAllocation(process_address, size);
656 process_sp->ReadMemory(process_address, bytes, size,
error);
663 Address absolute_address(process_address);
664 target_sp->ReadMemory(absolute_address, bytes, size,
error,
true);
668 error.SetErrorToGenericError();
669 error.SetErrorString(
"Couldn't read: no allocation contains the target "
670 "range, and neither the process nor the target exist");
678 if (offset > allocation.
m_size) {
679 error.SetErrorToGenericError();
680 error.SetErrorString(
"Couldn't read: data is not in the allocation");
688 error.SetErrorToGenericError();
689 error.SetErrorString(
"Couldn't read: invalid allocation policy");
693 error.SetErrorToGenericError();
694 error.SetErrorString(
"Couldn't read: data buffer is empty");
698 error.SetErrorToGenericError();
699 error.SetErrorString(
"Couldn't read: not enough underlying data");
708 process_sp->ReadMemory(process_address, bytes, size,
error);
709 if (!
error.Success())
713 error.SetErrorToGenericError();
714 error.SetErrorString(
"Couldn't read: data buffer is empty");
723 process_sp->ReadMemory(process_address, bytes, size,
error);
724 if (!
error.Success())
732 "IRMemoryMap::ReadMemory (0x%" PRIx64
", 0x%" PRIxPTR
733 ", 0x%" PRId64
") came from [0x%" PRIx64
"..0x%" PRIx64
")",
734 (uint64_t)process_address,
reinterpret_cast<uintptr_t
>(bytes), (uint64_t)size,
737 (uint64_t)allocation.
m_size);
750 if (!
error.Success())
760 error.SetErrorToGenericError();
761 error.SetErrorStringWithFormat(
762 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
765 scalar = extractor.
GetU8(&offset);
768 scalar = extractor.
GetU16(&offset);
771 scalar = extractor.
GetU32(&offset);
774 scalar = extractor.
GetU64(&offset);
778 error.SetErrorToGenericError();
779 error.SetErrorString(
"Couldn't read scalar: its size was zero");
792 if (!
error.Success())
804 AllocationMap::iterator iter =
FindAllocation(process_address, size);
807 error.SetErrorToGenericError();
808 error.SetErrorStringWithFormat(
809 "Couldn't find an allocation containing [0x%" PRIx64
"..0x%" PRIx64
811 process_address, process_address + size);
819 error.SetErrorToGenericError();
820 error.SetErrorString(
821 "Couldn't get memory data: invalid allocation policy");
824 error.SetErrorToGenericError();
825 error.SetErrorString(
826 "Couldn't get memory data: memory is only in the target");
832 error.SetErrorToGenericError();
833 error.SetErrorString(
"Couldn't get memory data: data buffer is empty");
840 if (!
error.Success())
850 error.SetErrorToGenericError();
851 error.SetErrorString(
"Couldn't get memory data: data buffer is empty");
860 error.SetErrorToGenericError();
861 error.SetErrorString(
"Couldn't get memory data: its size was zero");
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::offset_t SetByteSize(lldb::offset_t byte_size)
Set 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()
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
lldb::addr_t Malloc(size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Status &error)
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)
void WritePointerToMemory(lldb::addr_t process_address, lldb::addr_t address, Status &error)
AllocationMap::iterator FindAllocation(lldb::addr_t addr, size_t size)
uint32_t GetAddressByteSize()
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...
OptionalBool GetWritable() const
OptionalBool GetReadable() const
OptionalBool GetExecutable() 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.
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.
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.
BaseType GetRangeEnd() const