LLDB mainline
IRMemoryMap.cpp
Go to the documentation of this file.
1//===-- IRMemoryMap.cpp ---------------------------------------------------===//
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
11#include "lldb/Target/Process.h"
12#include "lldb/Target/Target.h"
17#include "lldb/Utility/Log.h"
18#include "lldb/Utility/Scalar.h"
19#include "lldb/Utility/Status.h"
20
21using namespace lldb_private;
22
24 if (target_sp)
25 m_process_wp = target_sp->GetProcessSP();
26}
27
29 lldb::ProcessSP process_sp = m_process_wp.lock();
30
31 if (process_sp) {
32 AllocationMap::iterator iter;
33
34 Status err;
35
36 while ((iter = m_allocations.begin()) != m_allocations.end()) {
37 err.Clear();
38 if (iter->second.m_leak)
39 m_allocations.erase(iter);
40 else
41 Free(iter->first, err);
42 }
43 }
44}
45
47 // The FindSpace algorithm's job is to find a region of memory that the
48 // underlying process is unlikely to be using.
49 //
50 // The memory returned by this function will never be written to. The only
51 // point is that it should not shadow process memory if possible, so that
52 // expressions processing real values from the process do not use the wrong
53 // data.
54 //
55 // If the process can in fact allocate memory (CanJIT() lets us know this)
56 // then this can be accomplished just be allocating memory in the inferior.
57 // Then no guessing is required.
58
59 lldb::TargetSP target_sp = m_target_wp.lock();
60 lldb::ProcessSP process_sp = m_process_wp.lock();
61
62 const bool process_is_alive = process_sp && process_sp->IsAlive();
63
65 if (size == 0)
66 return ret;
67
68 if (process_is_alive && process_sp->CanJIT()) {
69 Status alloc_error;
70
71 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable |
72 lldb::ePermissionsWritable,
73 alloc_error);
74
75 if (!alloc_error.Success())
77 else
78 return ret;
79 }
80
81 // At this point we know that we need to hunt.
82 //
83 // First, go to the end of the existing allocations we've made if there are
84 // any allocations. Otherwise start at the beginning of memory.
85
86 if (m_allocations.empty()) {
87 ret = 0;
88 } else {
89 auto back = m_allocations.rbegin();
90 lldb::addr_t addr = back->first;
91 size_t alloc_size = back->second.m_size;
92 ret = llvm::alignTo(addr + alloc_size, 4096);
93 }
94
95 uint64_t end_of_memory;
96 switch (GetAddressByteSize()) {
97 case 2:
98 end_of_memory = 0xffffull;
99 break;
100 case 4:
101 end_of_memory = 0xffffffffull;
102 break;
103 case 8:
104 end_of_memory = 0xffffffffffffffffull;
105 break;
106 default:
107 lldbassert(false && "Invalid address size.");
109 }
110
111 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped
112 // regions, walk forward through memory until a region is found that has
113 // adequate space for our allocation.
114 //
115 // Skip this when the process can't JIT. In that case, allocations are
116 // host-only and never written to process memory, so there's no need to probe
117 // the process's memory map.
118 if (process_is_alive && process_sp->CanJIT()) {
119 MemoryRegionInfo region_info;
120 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
121 if (err.Success()) {
122 while (true) {
123 if (region_info.GetRange().GetRangeBase() == 0 &&
124 region_info.GetRange().GetRangeEnd() < end_of_memory) {
125 // Don't use a region that starts at address 0,
126 // it can make it harder to debug null dereference crashes
127 // in the inferior.
128 ret = region_info.GetRange().GetRangeEnd();
129 } else if (region_info.GetReadable() != eLazyBoolNo ||
130 region_info.GetWritable() != eLazyBoolNo ||
131 region_info.GetExecutable() != eLazyBoolNo) {
132 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
134 break;
135 } else {
136 ret = region_info.GetRange().GetRangeEnd();
137 }
138 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
139 return ret;
140 } else {
141 // ret stays the same. We just need to walk a bit further.
142 }
143
144 // FIXME: When we're able to JIT WebAssembly, this strategy won't work
145 // because we might probe beyond its linear memory.
146 err = process_sp->GetMemoryRegionInfo(
147 region_info.GetRange().GetRangeEnd(), region_info);
148 if (err.Fail()) {
149 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
151 break;
152 }
153 }
154 }
155 }
156
157 // We've tried our algorithm, and it didn't work. Now we have to reset back
158 // to the end of the allocations we've already reported, or use a 'sensible'
159 // default if this is our first allocation.
160 if (m_allocations.empty()) {
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");
167 }
168 ret = alloc_address;
169 } else {
170 uint32_t address_byte_size = GetAddressByteSize();
171 if (address_byte_size != UINT32_MAX) {
172 switch (address_byte_size) {
173 case 2:
174 ret = 0x8000ull;
175 break;
176 case 4:
177 ret = 0xee000000ull;
178 break;
179 case 8:
180 ret = 0xdead0fff00000000ull;
181 break;
182 default:
183 lldbassert(false && "Invalid address size.");
185 }
186 }
187 }
188 } else {
189 auto back = m_allocations.rbegin();
190 lldb::addr_t addr = back->first;
191 size_t alloc_size = back->second.m_size;
192 uint64_t align = target_sp->GetExprAllocAlign();
193 if (align == 0)
194 align = 4096;
195 ret = llvm::alignTo(addr + alloc_size, align);
196 }
197
198 return ret;
199}
200
201IRMemoryMap::AllocationMap::iterator
203 if (addr == LLDB_INVALID_ADDRESS)
204 return m_allocations.end();
205
206 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
207
208 if (iter == m_allocations.end() || iter->first > addr) {
209 if (iter == m_allocations.begin())
210 return m_allocations.end();
211 iter--;
212 }
213
214 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
215 return iter;
216
217 return m_allocations.end();
218}
219
220bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
221 if (addr == LLDB_INVALID_ADDRESS)
222 return false;
223
224 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
225
226 // Since we only know that the returned interval begins at a location greater
227 // than or equal to where the given interval begins, it's possible that the
228 // given interval intersects either the returned interval or the previous
229 // interval. Thus, we need to check both. Note that we only need to check
230 // these two intervals. Since all intervals are disjoint it is not possible
231 // that an adjacent interval does not intersect, but a non-adjacent interval
232 // does intersect.
233 if (iter != m_allocations.end()) {
234 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
235 iter->second.m_size))
236 return true;
237 }
238
239 if (iter != m_allocations.begin()) {
240 --iter;
241 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
242 iter->second.m_size))
243 return true;
244 }
245
246 return false;
247}
248
250 lldb::addr_t addr2, size_t size2) {
251 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
252 // that satisfy A<B and X<Y are the following:
253 // A B X Y
254 // A X B Y (intersects)
255 // A X Y B (intersects)
256 // X A B Y (intersects)
257 // X A Y B (intersects)
258 // X Y A B
259 // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
260 // || Y <= A)), or (X < B && A < Y)
261 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
262}
263
265 lldb::ProcessSP process_sp = m_process_wp.lock();
266
267 if (process_sp)
268 return process_sp->GetByteOrder();
269
270 lldb::TargetSP target_sp = m_target_wp.lock();
271
272 if (target_sp)
273 return target_sp->GetArchitecture().GetByteOrder();
274
276}
277
279 lldb::ProcessSP process_sp = m_process_wp.lock();
280
281 if (process_sp)
282 return process_sp->GetAddressByteSize();
283
284 lldb::TargetSP target_sp = m_target_wp.lock();
285
286 if (target_sp)
287 return target_sp->GetArchitecture().GetAddressByteSize();
288
289 return UINT32_MAX;
290}
291
293 lldb::ProcessSP process_sp = m_process_wp.lock();
294
295 if (process_sp)
296 return process_sp.get();
297
298 lldb::TargetSP target_sp = m_target_wp.lock();
299
300 if (target_sp)
301 return target_sp.get();
302
303 return nullptr;
304}
305
307 lldb::addr_t process_start, size_t size,
308 uint32_t permissions, uint8_t alignment,
309 AllocationPolicy policy)
310 : m_process_alloc(process_alloc), m_process_start(process_start),
311 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
312 m_alignment(alignment) {
313 switch (policy) {
314 default:
315 llvm_unreachable("Invalid AllocationPolicy");
318 m_data.SetByteSize(size);
319 break;
321 break;
322 }
323}
324
325llvm::Expected<lldb::addr_t>
326IRMemoryMap::Malloc(size_t size, uint8_t alignment, uint32_t permissions,
327 AllocationPolicy policy, bool zero_memory,
328 AllocationPolicy *used_policy) {
330
331 lldb::ProcessSP process_sp;
332 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
333 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
334
335 size_t allocation_size;
336
337 if (size == 0) {
338 // FIXME: Malloc(0) should either return an invalid address or assert, in
339 // order to cut down on unnecessary allocations.
340 allocation_size = alignment;
341 } else {
342 // Round up the requested size to an aligned value.
343 allocation_size = llvm::alignTo(size, alignment);
344
345 // The process page cache does not see the requested alignment. We can't
346 // assume its result will be any more than 1-byte aligned. To work around
347 // this, request `alignment - 1` additional bytes.
348 allocation_size += alignment - 1;
349 }
350
351 switch (policy) {
352 default:
353 return llvm::createStringError(
354 llvm::inconvertibleErrorCode(),
355 "Couldn't malloc: invalid allocation policy");
357 allocation_address = FindSpace(allocation_size);
358 if (allocation_address == LLDB_INVALID_ADDRESS)
359 return llvm::createStringError(llvm::inconvertibleErrorCode(),
360 "Couldn't malloc: address space is full");
361 break;
363 process_sp = m_process_wp.lock();
364 LLDB_LOGF(log,
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()) {
372 if (!zero_memory)
373 allocation_address =
374 process_sp->AllocateMemory(allocation_size, permissions, error);
375 else
376 allocation_address =
377 process_sp->CallocateMemory(allocation_size, permissions, error);
378
379 if (!error.Success())
380 return error.takeError();
381 } else {
382 LLDB_LOGF(log,
383 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
384 "due to failed condition (see previous expr log message)",
385 __FUNCTION__);
387 allocation_address = FindSpace(allocation_size);
388 if (allocation_address == LLDB_INVALID_ADDRESS)
389 return llvm::createStringError(
390 llvm::inconvertibleErrorCode(),
391 "Couldn't malloc: address space is full");
392 }
393 break;
395 process_sp = m_process_wp.lock();
396 if (process_sp) {
397 if (process_sp->CanJIT() && process_sp->IsAlive()) {
399 if (!zero_memory)
400 allocation_address =
401 process_sp->AllocateMemory(allocation_size, permissions, error);
402 else
403 allocation_address =
404 process_sp->CallocateMemory(allocation_size, permissions, error);
405
406 if (!error.Success())
407 return error.takeError();
408 } else {
409 return llvm::createStringError(
410 llvm::inconvertibleErrorCode(),
411 "Couldn't malloc: process doesn't support allocating memory");
412 }
413 } else {
414 return llvm::createStringError(llvm::inconvertibleErrorCode(),
415 "Couldn't malloc: process doesn't exist, "
416 "and this memory must be in the process");
417 }
418 break;
419 }
420
421 lldb::addr_t mask = alignment - 1;
422 aligned_address = (allocation_address + mask) & (~mask);
423
424 m_allocations.emplace(
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));
428
429 if (zero_memory) {
430 Status write_error;
431 std::vector<uint8_t> zero_buf(size, 0);
432 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
433 }
434
435 if (log) {
436 const char *policy_string;
437
438 switch (policy) {
439 default:
440 policy_string = "<invalid policy>";
441 break;
443 policy_string = "eAllocationPolicyHostOnly";
444 break;
446 policy_string = "eAllocationPolicyProcessOnly";
447 break;
449 policy_string = "eAllocationPolicyMirror";
450 break;
451 }
452
453 LLDB_LOGF(log,
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);
458 }
459
460 if (used_policy)
461 *used_policy = policy;
462
463 return aligned_address;
464}
465
467 error.Clear();
468
469 AllocationMap::iterator iter = m_allocations.find(process_address);
470
471 if (iter == m_allocations.end()) {
472 error = Status::FromErrorString("Couldn't leak: allocation doesn't exist");
473 return;
474 }
475
476 Allocation &allocation = iter->second;
477
478 allocation.m_leak = true;
479}
480
482 error.Clear();
483
484 AllocationMap::iterator iter = m_allocations.find(process_address);
485
486 if (iter == m_allocations.end()) {
487 error = Status::FromErrorString("Couldn't free: allocation doesn't exist");
488 return;
489 }
490
491 Allocation &allocation = iter->second;
492
493 switch (allocation.m_policy) {
494 default:
496 lldb::ProcessSP process_sp = m_process_wp.lock();
497 if (process_sp) {
498 if (process_sp->CanJIT() && process_sp->IsAlive())
499 process_sp->DeallocateMemory(
500 allocation.m_process_alloc); // FindSpace allocated this for real
501 }
502
503 break;
504 }
507 lldb::ProcessSP process_sp = m_process_wp.lock();
508 if (process_sp)
509 process_sp->DeallocateMemory(allocation.m_process_alloc);
510 }
511 }
512
514 LLDB_LOGF(log,
515 "IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
516 "..0x%" PRIx64 ")",
517 (uint64_t)process_address, iter->second.m_process_start,
518 iter->second.m_process_start + iter->second.m_size);
519 }
520
521 m_allocations.erase(iter);
522}
523
524bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
525 AllocationMap::iterator iter = FindAllocation(address, size);
526 if (iter == m_allocations.end())
527 return false;
528
529 Allocation &al = iter->second;
530
531 if (address > (al.m_process_start + al.m_size)) {
532 size = 0;
533 return false;
534 }
535
536 if (address > al.m_process_start) {
537 int dif = address - al.m_process_start;
538 size = al.m_size - dif;
539 return true;
540 }
541
542 size = al.m_size;
543 return true;
544}
545
547 const uint8_t *bytes, size_t size,
548 Status &error) {
549 error.Clear();
550
551 AllocationMap::iterator iter = FindAllocation(process_address, size);
552
553 if (iter == m_allocations.end()) {
554 lldb::ProcessSP process_sp = m_process_wp.lock();
555
556 if (process_sp) {
557 process_sp->WriteMemory(process_address, bytes, size, error);
558 return;
559 }
560
562 "Couldn't write: no allocation contains the target "
563 "range and the process doesn't exist");
564 return;
565 }
566
567 Allocation &allocation = iter->second;
568
569 uint64_t offset = process_address - allocation.m_process_start;
570
571 lldb::ProcessSP process_sp;
572
573 switch (allocation.m_policy) {
574 default:
575 error =
576 Status::FromErrorString("Couldn't write: invalid allocation policy");
577 return;
579 if (!allocation.m_data.GetByteSize()) {
580 error = Status::FromErrorString("Couldn't write: data buffer is empty");
581 return;
582 }
583 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
584 break;
586 if (!allocation.m_data.GetByteSize()) {
587 error = Status::FromErrorString("Couldn't write: data buffer is empty");
588 return;
589 }
590 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
591 process_sp = m_process_wp.lock();
592 if (process_sp) {
593 process_sp->WriteMemory(process_address, bytes, size, error);
594 if (!error.Success())
595 return;
596 }
597 break;
599 process_sp = m_process_wp.lock();
600 if (process_sp) {
601 process_sp->WriteMemory(process_address, bytes, size, error);
602 if (!error.Success())
603 return;
604 }
605 break;
606 }
607
609 LLDB_LOGF(log,
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,
613 (uint64_t)allocation.m_process_start,
614 (uint64_t)allocation.m_process_start +
615 (uint64_t)allocation.m_size);
616 }
617}
618
620 Scalar &scalar, size_t size,
621 Status &error) {
622 error.Clear();
623
624 if (size == UINT32_MAX)
625 size = scalar.GetByteSize();
626
627 if (size > 0) {
628 uint8_t buf[32];
629 const size_t mem_size =
630 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
631 if (mem_size > 0) {
632 return WriteMemory(process_address, buf, mem_size, error);
633 } else {
635 "Couldn't write scalar: failed to get scalar as memory data");
636 }
637 } else {
638 error = Status::FromErrorString("Couldn't write scalar: its size was zero");
639 }
640}
641
643 lldb::addr_t pointer, Status &error) {
644 error.Clear();
645
646 /// Only ask the Process to fix `pointer` if the address belongs to the
647 /// process. An address belongs to the process if the Allocation policy is not
648 /// eAllocationPolicyHostOnly.
649 auto it = FindAllocation(pointer, 1);
650 if (it == m_allocations.end() ||
651 it->second.m_policy != AllocationPolicy::eAllocationPolicyHostOnly)
652 if (auto process_sp = GetProcessWP().lock())
653 pointer = process_sp->FixAnyAddress(pointer);
654
655 Scalar scalar(pointer);
656
657 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
658}
659
660void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
661 size_t size, Status &error) {
662 error.Clear();
663
664 AllocationMap::iterator iter = FindAllocation(process_address, size);
665
666 if (iter == m_allocations.end()) {
667 lldb::ProcessSP process_sp = m_process_wp.lock();
668
669 if (process_sp) {
670 process_sp->ReadMemory(process_address, bytes, size, error);
671 return;
672 }
673
674 lldb::TargetSP target_sp = m_target_wp.lock();
675
676 if (target_sp) {
677 Address absolute_address(process_address);
678 target_sp->ReadMemory(absolute_address, bytes, size, error, true);
679 return;
680 }
681
683 "Couldn't read: no allocation contains the target "
684 "range, and neither the process nor the target exist");
685 return;
686 }
687
688 Allocation &allocation = iter->second;
689
690 uint64_t offset = process_address - allocation.m_process_start;
691
692 if (offset > allocation.m_size) {
693 error =
694 Status::FromErrorString("Couldn't read: data is not in the allocation");
695 return;
696 }
697
698 lldb::ProcessSP process_sp;
699
700 switch (allocation.m_policy) {
701 default:
702 error = Status::FromErrorString("Couldn't read: invalid allocation policy");
703 return;
705 if (!allocation.m_data.GetByteSize()) {
706 error = Status::FromErrorString("Couldn't read: data buffer is empty");
707 return;
708 }
709 if (allocation.m_data.GetByteSize() < offset + size) {
710 error =
711 Status::FromErrorString("Couldn't read: not enough underlying data");
712 return;
713 }
714
715 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
716 break;
718 process_sp = m_process_wp.lock();
719 if (process_sp) {
720 process_sp->ReadMemory(process_address, bytes, size, error);
721 if (!error.Success())
722 return;
723 } else {
724 if (!allocation.m_data.GetByteSize()) {
725 error = Status::FromErrorString("Couldn't read: data buffer is empty");
726 return;
727 }
728 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
729 }
730 break;
732 process_sp = m_process_wp.lock();
733 if (process_sp) {
734 process_sp->ReadMemory(process_address, bytes, size, error);
735 if (!error.Success())
736 return;
737 }
738 break;
739 }
740
742 LLDB_LOGF(log,
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,
746 (uint64_t)allocation.m_process_start,
747 (uint64_t)allocation.m_process_start +
748 (uint64_t)allocation.m_size);
749 }
750}
751
753 lldb::addr_t process_address,
754 size_t size, Status &error) {
755 error.Clear();
756
757 if (size > 0) {
758 DataBufferHeap buf(size, 0);
759 ReadMemory(buf.GetBytes(), process_address, size, error);
760
761 if (!error.Success())
762 return;
763
764 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
766
767 lldb::offset_t offset = 0;
768
769 switch (size) {
770 default:
772 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
773 return;
774 case 1:
775 scalar = extractor.GetU8(&offset);
776 break;
777 case 2:
778 scalar = extractor.GetU16(&offset);
779 break;
780 case 4:
781 scalar = extractor.GetU32(&offset);
782 break;
783 case 8:
784 scalar = extractor.GetU64(&offset);
785 break;
786 }
787 } else {
788 error = Status::FromErrorString("Couldn't read scalar: its size was zero");
789 }
790}
791
793 lldb::addr_t process_address,
794 Status &error) {
795 error.Clear();
796
797 Scalar pointer_scalar;
798 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
799 error);
800
801 if (!error.Success())
802 return;
803
804 *address = pointer_scalar.ULongLong();
805}
806
808 lldb::addr_t process_address, size_t size,
809 Status &error) {
810 error.Clear();
811
812 if (size > 0) {
813 AllocationMap::iterator iter = FindAllocation(process_address, size);
814
815 if (iter == m_allocations.end()) {
817 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
818 ")",
819 process_address, process_address + size);
820 return;
821 }
822
823 Allocation &allocation = iter->second;
824
825 switch (allocation.m_policy) {
826 default:
828 "Couldn't get memory data: invalid allocation policy");
829 return;
832 "Couldn't get memory data: memory is only in the target");
833 return;
835 lldb::ProcessSP process_sp = m_process_wp.lock();
836
837 if (!allocation.m_data.GetByteSize()) {
839 "Couldn't get memory data: data buffer is empty");
840 return;
841 }
842 if (process_sp) {
843 process_sp->ReadMemory(allocation.m_process_start,
844 allocation.m_data.GetBytes(),
845 allocation.m_data.GetByteSize(), error);
846 if (!error.Success())
847 return;
848 uint64_t offset = process_address - allocation.m_process_start;
849 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
851 return;
852 }
853 } break;
855 if (!allocation.m_data.GetByteSize()) {
857 "Couldn't get memory data: data buffer is empty");
858 return;
859 }
860 uint64_t offset = process_address - allocation.m_process_start;
861 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
863 return;
864 }
865 } else {
866 error =
867 Status::FromErrorString("Couldn't get memory data: its size was zero");
868 return;
869 }
870}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOGF(log,...)
Definition Log.h:378
A section + offset based address class.
Definition Address.h:62
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.
An data extractor class.
uint64_t GetU64(lldb::offset_t *offset_ptr) const
Extract a uint64_t value from *offset_ptr.
uint32_t GetU32(lldb::offset_t *offset_ptr) const
Extract a uint32_t value from *offset_ptr.
uint16_t GetU16(lldb::offset_t *offset_ptr) const
Extract a uint16_t value from *offset_ptr.
uint8_t GetU8(lldb::offset_t *offset_ptr) const
Extract a uint8_t value from *offset_ptr.
"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()
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
size_t GetByteSize() const
Definition Scalar.cpp:162
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition Scalar.cpp:365
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition Scalar.cpp:788
An error handling class.
Definition Status.h:118
void Clear()
Clear the object state.
Definition Status.cpp:214
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition Status.cpp:106
static Status FromErrorString(const char *str)
Definition Status.h:141
bool Fail() const
Test for error condition.
Definition Status.cpp:293
bool Success() const
Test for success condition.
Definition Status.cpp:303
uint8_t * GetBytes()
Get a pointer to the data.
Definition DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
#define UINT32_MAX
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.
Definition Log.h:327
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
ByteOrder
Byte ordering definitions.
uint64_t addr_t
Definition lldb-types.h:80
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
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
uint8_t m_alignment
The alignment of the requested allocation.
BaseType GetRangeBase() const
Definition RangeMap.h:45
BaseType GetRangeEnd() const
Definition RangeMap.h:78