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 if (process_is_alive) {
115 MemoryRegionInfo region_info;
116 Status err = process_sp->GetMemoryRegionInfo(ret, region_info);
117 if (err.Success()) {
118 while (true) {
119 if (region_info.GetRange().GetRangeBase() == 0 &&
120 region_info.GetRange().GetRangeEnd() < end_of_memory) {
121 // Don't use a region that starts at address 0,
122 // it can make it harder to debug null dereference crashes
123 // in the inferior.
124 ret = region_info.GetRange().GetRangeEnd();
125 } else if (region_info.GetReadable() != eLazyBoolNo ||
126 region_info.GetWritable() != eLazyBoolNo ||
127 region_info.GetExecutable() != eLazyBoolNo) {
128 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
130 break;
131 } else {
132 ret = region_info.GetRange().GetRangeEnd();
133 }
134 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
135 return ret;
136 } else {
137 // ret stays the same. We just need to walk a bit further.
138 }
139
140 err = process_sp->GetMemoryRegionInfo(
141 region_info.GetRange().GetRangeEnd(), region_info);
142 if (err.Fail()) {
143 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
145 break;
146 }
147 }
148 }
149 }
150
151 // We've tried our algorithm, and it didn't work. Now we have to reset back
152 // to the end of the allocations we've already reported, or use a 'sensible'
153 // default if this is our first allocation.
154 if (m_allocations.empty()) {
155 uint64_t alloc_address = target_sp->GetExprAllocAddress();
156 if (alloc_address > 0) {
157 if (alloc_address >= end_of_memory) {
158 lldbassert(0 && "The allocation address for expression evaluation must "
159 "be within process address space");
161 }
162 ret = alloc_address;
163 } else {
164 uint32_t address_byte_size = GetAddressByteSize();
165 if (address_byte_size != UINT32_MAX) {
166 switch (address_byte_size) {
167 case 2:
168 ret = 0x8000ull;
169 break;
170 case 4:
171 ret = 0xee000000ull;
172 break;
173 case 8:
174 ret = 0xdead0fff00000000ull;
175 break;
176 default:
177 lldbassert(false && "Invalid address size.");
179 }
180 }
181 }
182 } else {
183 auto back = m_allocations.rbegin();
184 lldb::addr_t addr = back->first;
185 size_t alloc_size = back->second.m_size;
186 uint64_t align = target_sp->GetExprAllocAlign();
187 if (align == 0)
188 align = 4096;
189 ret = llvm::alignTo(addr + alloc_size, align);
190 }
191
192 return ret;
193}
194
195IRMemoryMap::AllocationMap::iterator
197 if (addr == LLDB_INVALID_ADDRESS)
198 return m_allocations.end();
199
200 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
201
202 if (iter == m_allocations.end() || iter->first > addr) {
203 if (iter == m_allocations.begin())
204 return m_allocations.end();
205 iter--;
206 }
207
208 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
209 return iter;
210
211 return m_allocations.end();
212}
213
214bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
215 if (addr == LLDB_INVALID_ADDRESS)
216 return false;
217
218 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
219
220 // Since we only know that the returned interval begins at a location greater
221 // than or equal to where the given interval begins, it's possible that the
222 // given interval intersects either the returned interval or the previous
223 // interval. Thus, we need to check both. Note that we only need to check
224 // these two intervals. Since all intervals are disjoint it is not possible
225 // that an adjacent interval does not intersect, but a non-adjacent interval
226 // does intersect.
227 if (iter != m_allocations.end()) {
228 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
229 iter->second.m_size))
230 return true;
231 }
232
233 if (iter != m_allocations.begin()) {
234 --iter;
235 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
236 iter->second.m_size))
237 return true;
238 }
239
240 return false;
241}
242
244 lldb::addr_t addr2, size_t size2) {
245 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
246 // that satisfy A<B and X<Y are the following:
247 // A B X Y
248 // A X B Y (intersects)
249 // A X Y B (intersects)
250 // X A B Y (intersects)
251 // X A Y B (intersects)
252 // X Y A B
253 // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
254 // || Y <= A)), or (X < B && A < Y)
255 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
256}
257
259 lldb::ProcessSP process_sp = m_process_wp.lock();
260
261 if (process_sp)
262 return process_sp->GetByteOrder();
263
264 lldb::TargetSP target_sp = m_target_wp.lock();
265
266 if (target_sp)
267 return target_sp->GetArchitecture().GetByteOrder();
268
270}
271
273 lldb::ProcessSP process_sp = m_process_wp.lock();
274
275 if (process_sp)
276 return process_sp->GetAddressByteSize();
277
278 lldb::TargetSP target_sp = m_target_wp.lock();
279
280 if (target_sp)
281 return target_sp->GetArchitecture().GetAddressByteSize();
282
283 return UINT32_MAX;
284}
285
287 lldb::ProcessSP process_sp = m_process_wp.lock();
288
289 if (process_sp)
290 return process_sp.get();
291
292 lldb::TargetSP target_sp = m_target_wp.lock();
293
294 if (target_sp)
295 return target_sp.get();
296
297 return nullptr;
298}
299
301 lldb::addr_t process_start, size_t size,
302 uint32_t permissions, uint8_t alignment,
303 AllocationPolicy policy)
304 : m_process_alloc(process_alloc), m_process_start(process_start),
305 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
306 m_alignment(alignment) {
307 switch (policy) {
308 default:
309 llvm_unreachable("Invalid AllocationPolicy");
312 m_data.SetByteSize(size);
313 break;
315 break;
316 }
317}
318
319llvm::Expected<lldb::addr_t>
320IRMemoryMap::Malloc(size_t size, uint8_t alignment, uint32_t permissions,
321 AllocationPolicy policy, bool zero_memory,
322 AllocationPolicy *used_policy) {
324
325 lldb::ProcessSP process_sp;
326 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
327 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
328
329 size_t allocation_size;
330
331 if (size == 0) {
332 // FIXME: Malloc(0) should either return an invalid address or assert, in
333 // order to cut down on unnecessary allocations.
334 allocation_size = alignment;
335 } else {
336 // Round up the requested size to an aligned value.
337 allocation_size = llvm::alignTo(size, alignment);
338
339 // The process page cache does not see the requested alignment. We can't
340 // assume its result will be any more than 1-byte aligned. To work around
341 // this, request `alignment - 1` additional bytes.
342 allocation_size += alignment - 1;
343 }
344
345 switch (policy) {
346 default:
347 return llvm::createStringError(
348 llvm::inconvertibleErrorCode(),
349 "Couldn't malloc: invalid allocation policy");
351 allocation_address = FindSpace(allocation_size);
352 if (allocation_address == LLDB_INVALID_ADDRESS)
353 return llvm::createStringError(llvm::inconvertibleErrorCode(),
354 "Couldn't malloc: address space is full");
355 break;
357 process_sp = m_process_wp.lock();
358 LLDB_LOGF(log,
359 "IRMemoryMap::%s process_sp=0x%" PRIxPTR
360 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
361 __FUNCTION__, reinterpret_cast<uintptr_t>(process_sp.get()),
362 process_sp && process_sp->CanJIT() ? "true" : "false",
363 process_sp && process_sp->IsAlive() ? "true" : "false");
364 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
366 if (!zero_memory)
367 allocation_address =
368 process_sp->AllocateMemory(allocation_size, permissions, error);
369 else
370 allocation_address =
371 process_sp->CallocateMemory(allocation_size, permissions, error);
372
373 if (!error.Success())
374 return error.takeError();
375 } else {
376 LLDB_LOGF(log,
377 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
378 "due to failed condition (see previous expr log message)",
379 __FUNCTION__);
381 allocation_address = FindSpace(allocation_size);
382 if (allocation_address == LLDB_INVALID_ADDRESS)
383 return llvm::createStringError(
384 llvm::inconvertibleErrorCode(),
385 "Couldn't malloc: address space is full");
386 }
387 break;
389 process_sp = m_process_wp.lock();
390 if (process_sp) {
391 if (process_sp->CanJIT() && process_sp->IsAlive()) {
393 if (!zero_memory)
394 allocation_address =
395 process_sp->AllocateMemory(allocation_size, permissions, error);
396 else
397 allocation_address =
398 process_sp->CallocateMemory(allocation_size, permissions, error);
399
400 if (!error.Success())
401 return error.takeError();
402 } else {
403 return llvm::createStringError(
404 llvm::inconvertibleErrorCode(),
405 "Couldn't malloc: process doesn't support allocating memory");
406 }
407 } else {
408 return llvm::createStringError(llvm::inconvertibleErrorCode(),
409 "Couldn't malloc: process doesn't exist, "
410 "and this memory must be in the process");
411 }
412 break;
413 }
414
415 lldb::addr_t mask = alignment - 1;
416 aligned_address = (allocation_address + mask) & (~mask);
417
418 m_allocations.emplace(
419 std::piecewise_construct, std::forward_as_tuple(aligned_address),
420 std::forward_as_tuple(allocation_address, aligned_address,
421 allocation_size, permissions, alignment, policy));
422
423 if (zero_memory) {
424 Status write_error;
425 std::vector<uint8_t> zero_buf(size, 0);
426 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
427 }
428
429 if (log) {
430 const char *policy_string;
431
432 switch (policy) {
433 default:
434 policy_string = "<invalid policy>";
435 break;
437 policy_string = "eAllocationPolicyHostOnly";
438 break;
440 policy_string = "eAllocationPolicyProcessOnly";
441 break;
443 policy_string = "eAllocationPolicyMirror";
444 break;
445 }
446
447 LLDB_LOGF(log,
448 "IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64
449 ", %s) -> 0x%" PRIx64,
450 (uint64_t)allocation_size, (uint64_t)alignment,
451 (uint64_t)permissions, policy_string, aligned_address);
452 }
453
454 if (used_policy)
455 *used_policy = policy;
456
457 return aligned_address;
458}
459
461 error.Clear();
462
463 AllocationMap::iterator iter = m_allocations.find(process_address);
464
465 if (iter == m_allocations.end()) {
466 error = Status::FromErrorString("Couldn't leak: allocation doesn't exist");
467 return;
468 }
469
470 Allocation &allocation = iter->second;
471
472 allocation.m_leak = true;
473}
474
476 error.Clear();
477
478 AllocationMap::iterator iter = m_allocations.find(process_address);
479
480 if (iter == m_allocations.end()) {
481 error = Status::FromErrorString("Couldn't free: allocation doesn't exist");
482 return;
483 }
484
485 Allocation &allocation = iter->second;
486
487 switch (allocation.m_policy) {
488 default:
490 lldb::ProcessSP process_sp = m_process_wp.lock();
491 if (process_sp) {
492 if (process_sp->CanJIT() && process_sp->IsAlive())
493 process_sp->DeallocateMemory(
494 allocation.m_process_alloc); // FindSpace allocated this for real
495 }
496
497 break;
498 }
501 lldb::ProcessSP process_sp = m_process_wp.lock();
502 if (process_sp)
503 process_sp->DeallocateMemory(allocation.m_process_alloc);
504 }
505 }
506
508 LLDB_LOGF(log,
509 "IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
510 "..0x%" PRIx64 ")",
511 (uint64_t)process_address, iter->second.m_process_start,
512 iter->second.m_process_start + iter->second.m_size);
513 }
514
515 m_allocations.erase(iter);
516}
517
518bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
519 AllocationMap::iterator iter = FindAllocation(address, size);
520 if (iter == m_allocations.end())
521 return false;
522
523 Allocation &al = iter->second;
524
525 if (address > (al.m_process_start + al.m_size)) {
526 size = 0;
527 return false;
528 }
529
530 if (address > al.m_process_start) {
531 int dif = address - al.m_process_start;
532 size = al.m_size - dif;
533 return true;
534 }
535
536 size = al.m_size;
537 return true;
538}
539
541 const uint8_t *bytes, size_t size,
542 Status &error) {
543 error.Clear();
544
545 AllocationMap::iterator iter = FindAllocation(process_address, size);
546
547 if (iter == m_allocations.end()) {
548 lldb::ProcessSP process_sp = m_process_wp.lock();
549
550 if (process_sp) {
551 process_sp->WriteMemory(process_address, bytes, size, error);
552 return;
553 }
554
556 "Couldn't write: no allocation contains the target "
557 "range and the process doesn't exist");
558 return;
559 }
560
561 Allocation &allocation = iter->second;
562
563 uint64_t offset = process_address - allocation.m_process_start;
564
565 lldb::ProcessSP process_sp;
566
567 switch (allocation.m_policy) {
568 default:
569 error =
570 Status::FromErrorString("Couldn't write: invalid allocation policy");
571 return;
573 if (!allocation.m_data.GetByteSize()) {
574 error = Status::FromErrorString("Couldn't write: data buffer is empty");
575 return;
576 }
577 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
578 break;
580 if (!allocation.m_data.GetByteSize()) {
581 error = Status::FromErrorString("Couldn't write: data buffer is empty");
582 return;
583 }
584 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
585 process_sp = m_process_wp.lock();
586 if (process_sp) {
587 process_sp->WriteMemory(process_address, bytes, size, error);
588 if (!error.Success())
589 return;
590 }
591 break;
593 process_sp = m_process_wp.lock();
594 if (process_sp) {
595 process_sp->WriteMemory(process_address, bytes, size, error);
596 if (!error.Success())
597 return;
598 }
599 break;
600 }
601
603 LLDB_LOGF(log,
604 "IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIxPTR
605 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
606 (uint64_t)process_address, reinterpret_cast<uintptr_t>(bytes), (uint64_t)size,
607 (uint64_t)allocation.m_process_start,
608 (uint64_t)allocation.m_process_start +
609 (uint64_t)allocation.m_size);
610 }
611}
612
614 Scalar &scalar, size_t size,
615 Status &error) {
616 error.Clear();
617
618 if (size == UINT32_MAX)
619 size = scalar.GetByteSize();
620
621 if (size > 0) {
622 uint8_t buf[32];
623 const size_t mem_size =
624 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
625 if (mem_size > 0) {
626 return WriteMemory(process_address, buf, mem_size, error);
627 } else {
629 "Couldn't write scalar: failed to get scalar as memory data");
630 }
631 } else {
632 error = Status::FromErrorString("Couldn't write scalar: its size was zero");
633 }
634}
635
637 lldb::addr_t pointer, Status &error) {
638 error.Clear();
639
640 /// Only ask the Process to fix `pointer` if the address belongs to the
641 /// process. An address belongs to the process if the Allocation policy is not
642 /// eAllocationPolicyHostOnly.
643 auto it = FindAllocation(pointer, 1);
644 if (it == m_allocations.end() ||
645 it->second.m_policy != AllocationPolicy::eAllocationPolicyHostOnly)
646 if (auto process_sp = GetProcessWP().lock())
647 pointer = process_sp->FixAnyAddress(pointer);
648
649 Scalar scalar(pointer);
650
651 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
652}
653
654void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
655 size_t size, Status &error) {
656 error.Clear();
657
658 AllocationMap::iterator iter = FindAllocation(process_address, size);
659
660 if (iter == m_allocations.end()) {
661 lldb::ProcessSP process_sp = m_process_wp.lock();
662
663 if (process_sp) {
664 process_sp->ReadMemory(process_address, bytes, size, error);
665 return;
666 }
667
668 lldb::TargetSP target_sp = m_target_wp.lock();
669
670 if (target_sp) {
671 Address absolute_address(process_address);
672 target_sp->ReadMemory(absolute_address, bytes, size, error, true);
673 return;
674 }
675
677 "Couldn't read: no allocation contains the target "
678 "range, and neither the process nor the target exist");
679 return;
680 }
681
682 Allocation &allocation = iter->second;
683
684 uint64_t offset = process_address - allocation.m_process_start;
685
686 if (offset > allocation.m_size) {
687 error =
688 Status::FromErrorString("Couldn't read: data is not in the allocation");
689 return;
690 }
691
692 lldb::ProcessSP process_sp;
693
694 switch (allocation.m_policy) {
695 default:
696 error = Status::FromErrorString("Couldn't read: invalid allocation policy");
697 return;
699 if (!allocation.m_data.GetByteSize()) {
700 error = Status::FromErrorString("Couldn't read: data buffer is empty");
701 return;
702 }
703 if (allocation.m_data.GetByteSize() < offset + size) {
704 error =
705 Status::FromErrorString("Couldn't read: not enough underlying data");
706 return;
707 }
708
709 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
710 break;
712 process_sp = m_process_wp.lock();
713 if (process_sp) {
714 process_sp->ReadMemory(process_address, bytes, size, error);
715 if (!error.Success())
716 return;
717 } else {
718 if (!allocation.m_data.GetByteSize()) {
719 error = Status::FromErrorString("Couldn't read: data buffer is empty");
720 return;
721 }
722 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
723 }
724 break;
726 process_sp = m_process_wp.lock();
727 if (process_sp) {
728 process_sp->ReadMemory(process_address, bytes, size, error);
729 if (!error.Success())
730 return;
731 }
732 break;
733 }
734
736 LLDB_LOGF(log,
737 "IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIxPTR
738 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
739 (uint64_t)process_address, reinterpret_cast<uintptr_t>(bytes), (uint64_t)size,
740 (uint64_t)allocation.m_process_start,
741 (uint64_t)allocation.m_process_start +
742 (uint64_t)allocation.m_size);
743 }
744}
745
747 lldb::addr_t process_address,
748 size_t size, Status &error) {
749 error.Clear();
750
751 if (size > 0) {
752 DataBufferHeap buf(size, 0);
753 ReadMemory(buf.GetBytes(), process_address, size, error);
754
755 if (!error.Success())
756 return;
757
758 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
760
761 lldb::offset_t offset = 0;
762
763 switch (size) {
764 default:
766 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
767 return;
768 case 1:
769 scalar = extractor.GetU8(&offset);
770 break;
771 case 2:
772 scalar = extractor.GetU16(&offset);
773 break;
774 case 4:
775 scalar = extractor.GetU32(&offset);
776 break;
777 case 8:
778 scalar = extractor.GetU64(&offset);
779 break;
780 }
781 } else {
782 error = Status::FromErrorString("Couldn't read scalar: its size was zero");
783 }
784}
785
787 lldb::addr_t process_address,
788 Status &error) {
789 error.Clear();
790
791 Scalar pointer_scalar;
792 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
793 error);
794
795 if (!error.Success())
796 return;
797
798 *address = pointer_scalar.ULongLong();
799}
800
802 lldb::addr_t process_address, size_t size,
803 Status &error) {
804 error.Clear();
805
806 if (size > 0) {
807 AllocationMap::iterator iter = FindAllocation(process_address, size);
808
809 if (iter == m_allocations.end()) {
811 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
812 ")",
813 process_address, process_address + size);
814 return;
815 }
816
817 Allocation &allocation = iter->second;
818
819 switch (allocation.m_policy) {
820 default:
822 "Couldn't get memory data: invalid allocation policy");
823 return;
826 "Couldn't get memory data: memory is only in the target");
827 return;
829 lldb::ProcessSP process_sp = m_process_wp.lock();
830
831 if (!allocation.m_data.GetByteSize()) {
833 "Couldn't get memory data: data buffer is empty");
834 return;
835 }
836 if (process_sp) {
837 process_sp->ReadMemory(allocation.m_process_start,
838 allocation.m_data.GetBytes(),
839 allocation.m_data.GetByteSize(), error);
840 if (!error.Success())
841 return;
842 uint64_t offset = process_address - allocation.m_process_start;
843 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
845 return;
846 }
847 } break;
849 if (!allocation.m_data.GetByteSize()) {
851 "Couldn't get memory data: data buffer is empty");
852 return;
853 }
854 uint64_t offset = process_address - allocation.m_process_start;
855 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
857 return;
858 }
859 } else {
860 error =
861 Status::FromErrorString("Couldn't get memory data: its size was zero");
862 return;
863 }
864}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition LLDBAssert.h:16
#define LLDB_LOGF(log,...)
Definition Log.h:383
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:332
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