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
23IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
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() !=
127 region_info.GetWritable() !=
129 region_info.GetExecutable() !=
131 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) {
133 break;
134 } else {
135 ret = region_info.GetRange().GetRangeEnd();
136 }
137 } else if (ret + size < region_info.GetRange().GetRangeEnd()) {
138 return ret;
139 } else {
140 // ret stays the same. We just need to walk a bit further.
141 }
142
143 err = process_sp->GetMemoryRegionInfo(
144 region_info.GetRange().GetRangeEnd(), region_info);
145 if (err.Fail()) {
146 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
148 break;
149 }
150 }
151 }
152 }
153
154 // We've tried our algorithm, and it didn't work. Now we have to reset back
155 // to the end of the allocations we've already reported, or use a 'sensible'
156 // default if this is our first allocation.
157 if (m_allocations.empty()) {
158 uint64_t alloc_address = target_sp->GetExprAllocAddress();
159 if (alloc_address > 0) {
160 if (alloc_address >= end_of_memory) {
161 lldbassert(0 && "The allocation address for expression evaluation must "
162 "be within process address space");
164 }
165 ret = alloc_address;
166 } else {
167 uint32_t address_byte_size = GetAddressByteSize();
168 if (address_byte_size != UINT32_MAX) {
169 switch (address_byte_size) {
170 case 2:
171 ret = 0x8000ull;
172 break;
173 case 4:
174 ret = 0xee000000ull;
175 break;
176 case 8:
177 ret = 0xdead0fff00000000ull;
178 break;
179 default:
180 lldbassert(false && "Invalid address size.");
182 }
183 }
184 }
185 } else {
186 auto back = m_allocations.rbegin();
187 lldb::addr_t addr = back->first;
188 size_t alloc_size = back->second.m_size;
189 uint64_t align = target_sp->GetExprAllocAlign();
190 if (align == 0)
191 align = 4096;
192 ret = llvm::alignTo(addr + alloc_size, align);
193 }
194
195 return ret;
196}
197
198IRMemoryMap::AllocationMap::iterator
200 if (addr == LLDB_INVALID_ADDRESS)
201 return m_allocations.end();
202
203 AllocationMap::iterator iter = m_allocations.lower_bound(addr);
204
205 if (iter == m_allocations.end() || iter->first > addr) {
206 if (iter == m_allocations.begin())
207 return m_allocations.end();
208 iter--;
209 }
210
211 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
212 return iter;
213
214 return m_allocations.end();
215}
216
217bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
218 if (addr == LLDB_INVALID_ADDRESS)
219 return false;
220
221 AllocationMap::const_iterator iter = m_allocations.lower_bound(addr);
222
223 // Since we only know that the returned interval begins at a location greater
224 // than or equal to where the given interval begins, it's possible that the
225 // given interval intersects either the returned interval or the previous
226 // interval. Thus, we need to check both. Note that we only need to check
227 // these two intervals. Since all intervals are disjoint it is not possible
228 // that an adjacent interval does not intersect, but a non-adjacent interval
229 // does intersect.
230 if (iter != m_allocations.end()) {
231 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
232 iter->second.m_size))
233 return true;
234 }
235
236 if (iter != m_allocations.begin()) {
237 --iter;
238 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
239 iter->second.m_size))
240 return true;
241 }
242
243 return false;
244}
245
247 lldb::addr_t addr2, size_t size2) {
248 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
249 // that satisfy A<B and X<Y are the following:
250 // A B X Y
251 // A X B Y (intersects)
252 // A X Y B (intersects)
253 // X A B Y (intersects)
254 // X A Y B (intersects)
255 // X Y A B
256 // The first is B <= X, and the last is Y <= A. So the condition is !(B <= X
257 // || Y <= A)), or (X < B && A < Y)
258 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
259}
260
262 lldb::ProcessSP process_sp = m_process_wp.lock();
263
264 if (process_sp)
265 return process_sp->GetByteOrder();
266
267 lldb::TargetSP target_sp = m_target_wp.lock();
268
269 if (target_sp)
270 return target_sp->GetArchitecture().GetByteOrder();
271
273}
274
276 lldb::ProcessSP process_sp = m_process_wp.lock();
277
278 if (process_sp)
279 return process_sp->GetAddressByteSize();
280
281 lldb::TargetSP target_sp = m_target_wp.lock();
282
283 if (target_sp)
284 return target_sp->GetArchitecture().GetAddressByteSize();
285
286 return UINT32_MAX;
287}
288
290 lldb::ProcessSP process_sp = m_process_wp.lock();
291
292 if (process_sp)
293 return process_sp.get();
294
295 lldb::TargetSP target_sp = m_target_wp.lock();
296
297 if (target_sp)
298 return target_sp.get();
299
300 return nullptr;
301}
302
304 lldb::addr_t process_start, size_t size,
305 uint32_t permissions, uint8_t alignment,
306 AllocationPolicy policy)
307 : m_process_alloc(process_alloc), m_process_start(process_start),
308 m_size(size), m_policy(policy), m_leak(false), m_permissions(permissions),
309 m_alignment(alignment) {
310 switch (policy) {
311 default:
312 llvm_unreachable("Invalid AllocationPolicy");
315 m_data.SetByteSize(size);
316 break;
318 break;
319 }
320}
321
322lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
323 uint32_t permissions, AllocationPolicy policy,
324 bool zero_memory, Status &error) {
326 error.Clear();
327
328 lldb::ProcessSP process_sp;
329 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
330 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
331
332 size_t allocation_size;
333
334 if (size == 0) {
335 // FIXME: Malloc(0) should either return an invalid address or assert, in
336 // order to cut down on unnecessary allocations.
337 allocation_size = alignment;
338 } else {
339 // Round up the requested size to an aligned value.
340 allocation_size = llvm::alignTo(size, alignment);
341
342 // The process page cache does not see the requested alignment. We can't
343 // assume its result will be any more than 1-byte aligned. To work around
344 // this, request `alignment - 1` additional bytes.
345 allocation_size += alignment - 1;
346 }
347
348 switch (policy) {
349 default:
350 error.SetErrorToGenericError();
351 error.SetErrorString("Couldn't malloc: invalid allocation policy");
354 allocation_address = FindSpace(allocation_size);
355 if (allocation_address == LLDB_INVALID_ADDRESS) {
356 error.SetErrorToGenericError();
357 error.SetErrorString("Couldn't malloc: address space is full");
359 }
360 break;
362 process_sp = m_process_wp.lock();
363 LLDB_LOGF(log,
364 "IRMemoryMap::%s process_sp=0x%" PRIxPTR
365 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
366 __FUNCTION__, reinterpret_cast<uintptr_t>(process_sp.get()),
367 process_sp && process_sp->CanJIT() ? "true" : "false",
368 process_sp && process_sp->IsAlive() ? "true" : "false");
369 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
370 if (!zero_memory)
371 allocation_address =
372 process_sp->AllocateMemory(allocation_size, permissions, error);
373 else
374 allocation_address =
375 process_sp->CallocateMemory(allocation_size, permissions, error);
376
377 if (!error.Success())
379 } else {
380 LLDB_LOGF(log,
381 "IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
382 "due to failed condition (see previous expr log message)",
383 __FUNCTION__);
385 allocation_address = FindSpace(allocation_size);
386 if (allocation_address == LLDB_INVALID_ADDRESS) {
387 error.SetErrorToGenericError();
388 error.SetErrorString("Couldn't malloc: address space is full");
390 }
391 }
392 break;
394 process_sp = m_process_wp.lock();
395 if (process_sp) {
396 if (process_sp->CanJIT() && process_sp->IsAlive()) {
397 if (!zero_memory)
398 allocation_address =
399 process_sp->AllocateMemory(allocation_size, permissions, error);
400 else
401 allocation_address =
402 process_sp->CallocateMemory(allocation_size, permissions, error);
403
404 if (!error.Success())
406 } else {
407 error.SetErrorToGenericError();
408 error.SetErrorString(
409 "Couldn't malloc: process doesn't support allocating memory");
411 }
412 } else {
413 error.SetErrorToGenericError();
414 error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
415 "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 return aligned_address;
461}
462
464 error.Clear();
465
466 AllocationMap::iterator iter = m_allocations.find(process_address);
467
468 if (iter == m_allocations.end()) {
469 error.SetErrorToGenericError();
470 error.SetErrorString("Couldn't leak: allocation doesn't exist");
471 return;
472 }
473
474 Allocation &allocation = iter->second;
475
476 allocation.m_leak = true;
477}
478
480 error.Clear();
481
482 AllocationMap::iterator iter = m_allocations.find(process_address);
483
484 if (iter == m_allocations.end()) {
485 error.SetErrorToGenericError();
486 error.SetErrorString("Couldn't free: allocation doesn't exist");
487 return;
488 }
489
490 Allocation &allocation = iter->second;
491
492 switch (allocation.m_policy) {
493 default:
495 lldb::ProcessSP process_sp = m_process_wp.lock();
496 if (process_sp) {
497 if (process_sp->CanJIT() && process_sp->IsAlive())
498 process_sp->DeallocateMemory(
499 allocation.m_process_alloc); // FindSpace allocated this for real
500 }
501
502 break;
503 }
506 lldb::ProcessSP process_sp = m_process_wp.lock();
507 if (process_sp)
508 process_sp->DeallocateMemory(allocation.m_process_alloc);
509 }
510 }
511
513 LLDB_LOGF(log,
514 "IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
515 "..0x%" PRIx64 ")",
516 (uint64_t)process_address, iter->second.m_process_start,
517 iter->second.m_process_start + iter->second.m_size);
518 }
519
520 m_allocations.erase(iter);
521}
522
523bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
524 AllocationMap::iterator iter = FindAllocation(address, size);
525 if (iter == m_allocations.end())
526 return false;
527
528 Allocation &al = iter->second;
529
530 if (address > (al.m_process_start + al.m_size)) {
531 size = 0;
532 return false;
533 }
534
535 if (address > al.m_process_start) {
536 int dif = address - al.m_process_start;
537 size = al.m_size - dif;
538 return true;
539 }
540
541 size = al.m_size;
542 return true;
543}
544
546 const uint8_t *bytes, size_t size,
547 Status &error) {
548 error.Clear();
549
550 AllocationMap::iterator iter = FindAllocation(process_address, size);
551
552 if (iter == m_allocations.end()) {
553 lldb::ProcessSP process_sp = m_process_wp.lock();
554
555 if (process_sp) {
556 process_sp->WriteMemory(process_address, bytes, size, error);
557 return;
558 }
559
560 error.SetErrorToGenericError();
561 error.SetErrorString("Couldn't write: no allocation contains the target "
562 "range and the process doesn't exist");
563 return;
564 }
565
566 Allocation &allocation = iter->second;
567
568 uint64_t offset = process_address - allocation.m_process_start;
569
570 lldb::ProcessSP process_sp;
571
572 switch (allocation.m_policy) {
573 default:
574 error.SetErrorToGenericError();
575 error.SetErrorString("Couldn't write: invalid allocation policy");
576 return;
578 if (!allocation.m_data.GetByteSize()) {
579 error.SetErrorToGenericError();
580 error.SetErrorString("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.SetErrorToGenericError();
588 error.SetErrorString("Couldn't write: data buffer is empty");
589 return;
590 }
591 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
592 process_sp = m_process_wp.lock();
593 if (process_sp) {
594 process_sp->WriteMemory(process_address, bytes, size, error);
595 if (!error.Success())
596 return;
597 }
598 break;
600 process_sp = m_process_wp.lock();
601 if (process_sp) {
602 process_sp->WriteMemory(process_address, bytes, size, error);
603 if (!error.Success())
604 return;
605 }
606 break;
607 }
608
610 LLDB_LOGF(log,
611 "IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIxPTR
612 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
613 (uint64_t)process_address, reinterpret_cast<uintptr_t>(bytes), (uint64_t)size,
614 (uint64_t)allocation.m_process_start,
615 (uint64_t)allocation.m_process_start +
616 (uint64_t)allocation.m_size);
617 }
618}
619
621 Scalar &scalar, size_t size,
622 Status &error) {
623 error.Clear();
624
625 if (size == UINT32_MAX)
626 size = scalar.GetByteSize();
627
628 if (size > 0) {
629 uint8_t buf[32];
630 const size_t mem_size =
631 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
632 if (mem_size > 0) {
633 return WriteMemory(process_address, buf, mem_size, error);
634 } else {
635 error.SetErrorToGenericError();
636 error.SetErrorString(
637 "Couldn't write scalar: failed to get scalar as memory data");
638 }
639 } else {
640 error.SetErrorToGenericError();
641 error.SetErrorString("Couldn't write scalar: its size was zero");
642 }
643}
644
646 lldb::addr_t address, Status &error) {
647 error.Clear();
648
649 Scalar scalar(address);
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
676 error.SetErrorToGenericError();
677 error.SetErrorString("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.SetErrorToGenericError();
688 error.SetErrorString("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.SetErrorToGenericError();
697 error.SetErrorString("Couldn't read: invalid allocation policy");
698 return;
700 if (!allocation.m_data.GetByteSize()) {
701 error.SetErrorToGenericError();
702 error.SetErrorString("Couldn't read: data buffer is empty");
703 return;
704 }
705 if (allocation.m_data.GetByteSize() < offset + size) {
706 error.SetErrorToGenericError();
707 error.SetErrorString("Couldn't read: not enough underlying data");
708 return;
709 }
710
711 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
712 break;
714 process_sp = m_process_wp.lock();
715 if (process_sp) {
716 process_sp->ReadMemory(process_address, bytes, size, error);
717 if (!error.Success())
718 return;
719 } else {
720 if (!allocation.m_data.GetByteSize()) {
721 error.SetErrorToGenericError();
722 error.SetErrorString("Couldn't read: data buffer is empty");
723 return;
724 }
725 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
726 }
727 break;
729 process_sp = m_process_wp.lock();
730 if (process_sp) {
731 process_sp->ReadMemory(process_address, bytes, size, error);
732 if (!error.Success())
733 return;
734 }
735 break;
736 }
737
739 LLDB_LOGF(log,
740 "IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIxPTR
741 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
742 (uint64_t)process_address, reinterpret_cast<uintptr_t>(bytes), (uint64_t)size,
743 (uint64_t)allocation.m_process_start,
744 (uint64_t)allocation.m_process_start +
745 (uint64_t)allocation.m_size);
746 }
747}
748
750 lldb::addr_t process_address,
751 size_t size, Status &error) {
752 error.Clear();
753
754 if (size > 0) {
755 DataBufferHeap buf(size, 0);
756 ReadMemory(buf.GetBytes(), process_address, size, error);
757
758 if (!error.Success())
759 return;
760
761 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
763
764 lldb::offset_t offset = 0;
765
766 switch (size) {
767 default:
768 error.SetErrorToGenericError();
769 error.SetErrorStringWithFormat(
770 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
771 return;
772 case 1:
773 scalar = extractor.GetU8(&offset);
774 break;
775 case 2:
776 scalar = extractor.GetU16(&offset);
777 break;
778 case 4:
779 scalar = extractor.GetU32(&offset);
780 break;
781 case 8:
782 scalar = extractor.GetU64(&offset);
783 break;
784 }
785 } else {
786 error.SetErrorToGenericError();
787 error.SetErrorString("Couldn't read scalar: its size was zero");
788 }
789}
790
792 lldb::addr_t process_address,
793 Status &error) {
794 error.Clear();
795
796 Scalar pointer_scalar;
797 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
798 error);
799
800 if (!error.Success())
801 return;
802
803 *address = pointer_scalar.ULongLong();
804}
805
807 lldb::addr_t process_address, size_t size,
808 Status &error) {
809 error.Clear();
810
811 if (size > 0) {
812 AllocationMap::iterator iter = FindAllocation(process_address, size);
813
814 if (iter == m_allocations.end()) {
815 error.SetErrorToGenericError();
816 error.SetErrorStringWithFormat(
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:
827 error.SetErrorToGenericError();
828 error.SetErrorString(
829 "Couldn't get memory data: invalid allocation policy");
830 return;
832 error.SetErrorToGenericError();
833 error.SetErrorString(
834 "Couldn't get memory data: memory is only in the target");
835 return;
837 lldb::ProcessSP process_sp = m_process_wp.lock();
838
839 if (!allocation.m_data.GetByteSize()) {
840 error.SetErrorToGenericError();
841 error.SetErrorString("Couldn't get memory data: data buffer is empty");
842 return;
843 }
844 if (process_sp) {
845 process_sp->ReadMemory(allocation.m_process_start,
846 allocation.m_data.GetBytes(),
847 allocation.m_data.GetByteSize(), error);
848 if (!error.Success())
849 return;
850 uint64_t offset = process_address - allocation.m_process_start;
851 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
853 return;
854 }
855 } break;
857 if (!allocation.m_data.GetByteSize()) {
858 error.SetErrorToGenericError();
859 error.SetErrorString("Couldn't get memory data: data buffer is empty");
860 return;
861 }
862 uint64_t offset = process_address - allocation.m_process_start;
863 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
865 return;
866 }
867 } else {
868 error.SetErrorToGenericError();
869 error.SetErrorString("Couldn't get memory data: its size was zero");
870 return;
871 }
872}
static llvm::raw_ostream & error(Stream &strm)
#define lldbassert(x)
Definition: LLDBAssert.h:15
#define LLDB_LOGF(log,...)
Definition: Log.h:366
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.
lldb::offset_t SetByteSize(lldb::offset_t byte_size)
Set the number of bytes in the data buffer.
An data extractor class.
Definition: DataExtractor.h:48
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
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
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)
IRMemoryMap(lldb::TargetSP target_sp)
Definition: IRMemoryMap.cpp:23
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
OptionalBool GetWritable() const
OptionalBool GetReadable() const
OptionalBool GetExecutable() const
size_t GetByteSize() const
Definition: Scalar.cpp:132
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:335
size_t GetAsMemoryData(void *dst, size_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
Definition: Scalar.cpp:772
An error handling class.
Definition: Status.h:44
void Clear()
Clear the object state.
Definition: Status.cpp:166
bool Fail() const
Test for error condition.
Definition: Status.cpp:180
bool Success() const
Test for success condition.
Definition: Status.cpp:278
uint8_t * GetBytes()
Get a pointer to the data.
Definition: DataBuffer.h:108
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
#define UINT32_MAX
Definition: lldb-defines.h:19
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:331
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:387
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid
uint64_t addr_t
Definition: lldb-types.h:80
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
AllocationPolicy m_policy
Flags. Keep these grouped together to avoid structure padding.
Definition: IRMemoryMap.h:98
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:93
BaseType GetRangeBase() const
Definition: RangeMap.h:45
BaseType GetRangeEnd() const
Definition: RangeMap.h:78