1 //===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/DataBufferHeap.h" 11 #include "lldb/Core/DataExtractor.h" 12 #include "lldb/Core/Error.h" 13 #include "lldb/Core/Log.h" 14 #include "lldb/Core/Scalar.h" 15 #include "lldb/Expression/IRMemoryMap.h" 16 #include "lldb/Target/MemoryRegionInfo.h" 17 #include "lldb/Target/Process.h" 18 #include "lldb/Target/Target.h" 19 #include "lldb/Utility/LLDBAssert.h" 20 21 using namespace lldb_private; 22 23 IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) : 24 m_target_wp(target_sp) 25 { 26 if (target_sp) 27 m_process_wp = target_sp->GetProcessSP(); 28 } 29 30 IRMemoryMap::~IRMemoryMap () 31 { 32 lldb::ProcessSP process_sp = m_process_wp.lock(); 33 34 if (process_sp) 35 { 36 AllocationMap::iterator iter; 37 38 Error err; 39 40 while ((iter = m_allocations.begin()) != m_allocations.end()) 41 { 42 err.Clear(); 43 if (iter->second.m_leak) 44 m_allocations.erase(iter); 45 else 46 Free(iter->first, err); 47 } 48 } 49 } 50 51 lldb::addr_t 52 IRMemoryMap::FindSpace (size_t size) 53 { 54 // The FindSpace algorithm's job is to find a region of memory that the 55 // underlying process is unlikely to be using. 56 // 57 // The memory returned by this function will never be written to. The only 58 // point is that it should not shadow process memory if possible, so that 59 // expressions processing real values from the process do not use the 60 // wrong data. 61 // 62 // If the process can in fact allocate memory (CanJIT() lets us know this) 63 // then this can be accomplished just be allocating memory in the inferior. 64 // Then no guessing is required. 65 66 lldb::TargetSP target_sp = m_target_wp.lock(); 67 lldb::ProcessSP process_sp = m_process_wp.lock(); 68 69 const bool process_is_alive = process_sp && process_sp->IsAlive(); 70 71 lldb::addr_t ret = LLDB_INVALID_ADDRESS; 72 if (size == 0) 73 return ret; 74 75 if (process_is_alive && process_sp->CanJIT()) 76 { 77 Error alloc_error; 78 79 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable | lldb::ePermissionsWritable, alloc_error); 80 81 if (!alloc_error.Success()) 82 return LLDB_INVALID_ADDRESS; 83 else 84 return ret; 85 } 86 87 // At this point we know that we need to hunt. 88 // 89 // First, go to the end of the existing allocations we've made if there are 90 // any allocations. Otherwise start at the beginning of memory. 91 92 if (m_allocations.empty()) 93 { 94 ret = 0x0; 95 } 96 else 97 { 98 auto back = m_allocations.rbegin(); 99 lldb::addr_t addr = back->first; 100 size_t alloc_size = back->second.m_size; 101 ret = llvm::alignTo(addr+alloc_size, 4096); 102 } 103 104 // Now, if it's possible to use the GetMemoryRegionInfo API to detect mapped 105 // regions, walk forward through memory until a region is found that 106 // has adequate space for our allocation. 107 if (process_is_alive) 108 { 109 const uint64_t end_of_memory = process_sp->GetAddressByteSize() == 8 ? 110 0xffffffffffffffffull : 0xffffffffull; 111 112 lldbassert(process_sp->GetAddressByteSize() == 4 || end_of_memory != 0xffffffffull); 113 114 MemoryRegionInfo region_info; 115 Error err = process_sp->GetMemoryRegionInfo(ret, region_info); 116 if (err.Success()) 117 { 118 while (true) 119 { 120 if (region_info.GetReadable() != MemoryRegionInfo::OptionalBool::eNo || 121 region_info.GetWritable() != MemoryRegionInfo::OptionalBool::eNo || 122 region_info.GetExecutable() != MemoryRegionInfo::OptionalBool::eNo) 123 { 124 if (region_info.GetRange().GetRangeEnd() - 1 >= end_of_memory) 125 { 126 ret = LLDB_INVALID_ADDRESS; 127 break; 128 } 129 else 130 { 131 ret = region_info.GetRange().GetRangeEnd(); 132 } 133 } 134 else if (ret + size < region_info.GetRange().GetRangeEnd()) 135 { 136 return ret; 137 } 138 else 139 { 140 // ret stays the same. We just need to walk a bit further. 141 } 142 143 err = process_sp->GetMemoryRegionInfo(region_info.GetRange().GetRangeEnd(), region_info); 144 if (err.Fail()) 145 { 146 lldbassert(!"GetMemoryRegionInfo() succeeded, then failed"); 147 ret = LLDB_INVALID_ADDRESS; 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 158 if (m_allocations.empty()) 159 { 160 uint32_t address_byte_size = GetAddressByteSize(); 161 if (address_byte_size != UINT32_MAX) 162 { 163 switch (address_byte_size) 164 { 165 case 8: 166 ret = 0xffffffff00000000ull; 167 break; 168 case 4: 169 ret = 0xee000000ull; 170 break; 171 default: 172 break; 173 } 174 } 175 } 176 else 177 { 178 auto back = m_allocations.rbegin(); 179 lldb::addr_t addr = back->first; 180 size_t alloc_size = back->second.m_size; 181 ret = llvm::alignTo(addr+alloc_size, 4096); 182 } 183 184 return ret; 185 } 186 187 IRMemoryMap::AllocationMap::iterator 188 IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size) 189 { 190 if (addr == LLDB_INVALID_ADDRESS) 191 return m_allocations.end(); 192 193 AllocationMap::iterator iter = m_allocations.lower_bound (addr); 194 195 if (iter == m_allocations.end() || 196 iter->first > addr) 197 { 198 if (iter == m_allocations.begin()) 199 return m_allocations.end(); 200 iter--; 201 } 202 203 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size) 204 return iter; 205 206 return m_allocations.end(); 207 } 208 209 bool 210 IRMemoryMap::IntersectsAllocation (lldb::addr_t addr, size_t size) const 211 { 212 if (addr == LLDB_INVALID_ADDRESS) 213 return false; 214 215 AllocationMap::const_iterator iter = m_allocations.lower_bound (addr); 216 217 // Since we only know that the returned interval begins at a location greater than or 218 // equal to where the given interval begins, it's possible that the given interval 219 // intersects either the returned interval or the previous interval. Thus, we need to 220 // check both. Note that we only need to check these two intervals. Since all intervals 221 // are disjoint it is not possible that an adjacent interval does not intersect, but a 222 // non-adjacent interval does intersect. 223 if (iter != m_allocations.end()) { 224 if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size)) 225 return true; 226 } 227 228 if (iter != m_allocations.begin()) { 229 --iter; 230 if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size)) 231 return true; 232 } 233 234 return false; 235 } 236 237 bool 238 IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1, lldb::addr_t addr2, size_t size2) { 239 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations that satisfy 240 // A<B and X<Y are the following: 241 // A B X Y 242 // A X B Y (intersects) 243 // A X Y B (intersects) 244 // X A B Y (intersects) 245 // X A Y B (intersects) 246 // X Y A B 247 // The first is B <= X, and the last is Y <= A. 248 // So the condition is !(B <= X || Y <= A)), or (X < B && A < Y) 249 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2)); 250 } 251 252 lldb::ByteOrder 253 IRMemoryMap::GetByteOrder() 254 { 255 lldb::ProcessSP process_sp = m_process_wp.lock(); 256 257 if (process_sp) 258 return process_sp->GetByteOrder(); 259 260 lldb::TargetSP target_sp = m_target_wp.lock(); 261 262 if (target_sp) 263 return target_sp->GetArchitecture().GetByteOrder(); 264 265 return lldb::eByteOrderInvalid; 266 } 267 268 uint32_t 269 IRMemoryMap::GetAddressByteSize() 270 { 271 lldb::ProcessSP process_sp = m_process_wp.lock(); 272 273 if (process_sp) 274 return process_sp->GetAddressByteSize(); 275 276 lldb::TargetSP target_sp = m_target_wp.lock(); 277 278 if (target_sp) 279 return target_sp->GetArchitecture().GetAddressByteSize(); 280 281 return UINT32_MAX; 282 } 283 284 ExecutionContextScope * 285 IRMemoryMap::GetBestExecutionContextScope() const 286 { 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 NULL; 298 } 299 300 IRMemoryMap::Allocation::Allocation (lldb::addr_t process_alloc, 301 lldb::addr_t process_start, 302 size_t size, 303 uint32_t permissions, 304 uint8_t alignment, 305 AllocationPolicy policy) : 306 m_process_alloc (process_alloc), 307 m_process_start (process_start), 308 m_size (size), 309 m_permissions (permissions), 310 m_alignment (alignment), 311 m_policy (policy), 312 m_leak (false) 313 { 314 switch (policy) 315 { 316 default: 317 assert (0 && "We cannot reach this!"); 318 case eAllocationPolicyHostOnly: 319 m_data.SetByteSize(size); 320 memset(m_data.GetBytes(), 0, size); 321 break; 322 case eAllocationPolicyProcessOnly: 323 break; 324 case eAllocationPolicyMirror: 325 m_data.SetByteSize(size); 326 memset(m_data.GetBytes(), 0, size); 327 break; 328 } 329 } 330 331 lldb::addr_t 332 IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Error &error) 333 { 334 lldb_private::Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); 335 error.Clear(); 336 337 lldb::ProcessSP process_sp; 338 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS; 339 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS; 340 341 size_t alignment_mask = alignment - 1; 342 size_t allocation_size; 343 344 if (size == 0) 345 allocation_size = alignment; 346 else 347 allocation_size = (size & alignment_mask) ? ((size + alignment) & (~alignment_mask)) : size; 348 349 switch (policy) 350 { 351 default: 352 error.SetErrorToGenericError(); 353 error.SetErrorString("Couldn't malloc: invalid allocation policy"); 354 return LLDB_INVALID_ADDRESS; 355 case eAllocationPolicyHostOnly: 356 allocation_address = FindSpace(allocation_size); 357 if (allocation_address == LLDB_INVALID_ADDRESS) 358 { 359 error.SetErrorToGenericError(); 360 error.SetErrorString("Couldn't malloc: address space is full"); 361 return LLDB_INVALID_ADDRESS; 362 } 363 break; 364 case eAllocationPolicyMirror: 365 process_sp = m_process_wp.lock(); 366 if (log) 367 log->Printf ("IRMemoryMap::%s process_sp=0x%" PRIx64 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s", __FUNCTION__, (lldb::addr_t) process_sp.get (), process_sp && process_sp->CanJIT () ? "true" : "false", process_sp && process_sp->IsAlive () ? "true" : "false"); 368 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) 369 { 370 if (!zero_memory) 371 allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); 372 else 373 allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error); 374 375 if (!error.Success()) 376 return LLDB_INVALID_ADDRESS; 377 } 378 else 379 { 380 if (log) 381 log->Printf ("IRMemoryMap::%s switching to eAllocationPolicyHostOnly due to failed condition (see previous expr log message)", __FUNCTION__); 382 policy = eAllocationPolicyHostOnly; 383 allocation_address = FindSpace(allocation_size); 384 if (allocation_address == LLDB_INVALID_ADDRESS) 385 { 386 error.SetErrorToGenericError(); 387 error.SetErrorString("Couldn't malloc: address space is full"); 388 return LLDB_INVALID_ADDRESS; 389 } 390 } 391 break; 392 case eAllocationPolicyProcessOnly: 393 process_sp = m_process_wp.lock(); 394 if (process_sp) 395 { 396 if (process_sp->CanJIT() && process_sp->IsAlive()) 397 { 398 if (!zero_memory) 399 allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); 400 else 401 allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error); 402 403 if (!error.Success()) 404 return LLDB_INVALID_ADDRESS; 405 } 406 else 407 { 408 error.SetErrorToGenericError(); 409 error.SetErrorString("Couldn't malloc: process doesn't support allocating memory"); 410 return LLDB_INVALID_ADDRESS; 411 } 412 } 413 else 414 { 415 error.SetErrorToGenericError(); 416 error.SetErrorString("Couldn't malloc: process doesn't exist, and this memory must be in the process"); 417 return LLDB_INVALID_ADDRESS; 418 } 419 break; 420 } 421 422 423 lldb::addr_t mask = alignment - 1; 424 aligned_address = (allocation_address + mask) & (~mask); 425 426 m_allocations[aligned_address] = Allocation(allocation_address, 427 aligned_address, 428 allocation_size, 429 permissions, 430 alignment, 431 policy); 432 433 if (log) 434 { 435 const char * policy_string; 436 437 switch (policy) 438 { 439 default: 440 policy_string = "<invalid policy>"; 441 break; 442 case eAllocationPolicyHostOnly: 443 policy_string = "eAllocationPolicyHostOnly"; 444 break; 445 case eAllocationPolicyProcessOnly: 446 policy_string = "eAllocationPolicyProcessOnly"; 447 break; 448 case eAllocationPolicyMirror: 449 policy_string = "eAllocationPolicyMirror"; 450 break; 451 } 452 453 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", %s) -> 0x%" PRIx64, 454 (uint64_t)allocation_size, 455 (uint64_t)alignment, 456 (uint64_t)permissions, 457 policy_string, 458 aligned_address); 459 } 460 461 return aligned_address; 462 } 463 464 void 465 IRMemoryMap::Leak (lldb::addr_t process_address, Error &error) 466 { 467 error.Clear(); 468 469 AllocationMap::iterator iter = m_allocations.find(process_address); 470 471 if (iter == m_allocations.end()) 472 { 473 error.SetErrorToGenericError(); 474 error.SetErrorString("Couldn't leak: allocation doesn't exist"); 475 return; 476 } 477 478 Allocation &allocation = iter->second; 479 480 allocation.m_leak = true; 481 } 482 483 void 484 IRMemoryMap::Free (lldb::addr_t process_address, Error &error) 485 { 486 error.Clear(); 487 488 AllocationMap::iterator iter = m_allocations.find(process_address); 489 490 if (iter == m_allocations.end()) 491 { 492 error.SetErrorToGenericError(); 493 error.SetErrorString("Couldn't free: allocation doesn't exist"); 494 return; 495 } 496 497 Allocation &allocation = iter->second; 498 499 switch (allocation.m_policy) 500 { 501 default: 502 case eAllocationPolicyHostOnly: 503 { 504 lldb::ProcessSP process_sp = m_process_wp.lock(); 505 if (process_sp) 506 { 507 if (process_sp->CanJIT() && process_sp->IsAlive()) 508 process_sp->DeallocateMemory(allocation.m_process_alloc); // FindSpace allocated this for real 509 } 510 511 break; 512 } 513 case eAllocationPolicyMirror: 514 case eAllocationPolicyProcessOnly: 515 { 516 lldb::ProcessSP process_sp = m_process_wp.lock(); 517 if (process_sp) 518 process_sp->DeallocateMemory(allocation.m_process_alloc); 519 } 520 } 521 522 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) 523 { 524 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64 "..0x%" PRIx64 ")", 525 (uint64_t)process_address, 526 iter->second.m_process_start, 527 iter->second.m_process_start + iter->second.m_size); 528 } 529 530 m_allocations.erase(iter); 531 } 532 533 bool 534 IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) 535 { 536 AllocationMap::iterator iter = FindAllocation(address, size); 537 if (iter == m_allocations.end()) 538 return false; 539 540 Allocation &al = iter->second; 541 542 if (address > (al.m_process_start + al.m_size)) 543 { 544 size = 0; 545 return false; 546 } 547 548 if (address > al.m_process_start) 549 { 550 int dif = address - al.m_process_start; 551 size = al.m_size - dif; 552 return true; 553 } 554 555 size = al.m_size; 556 return true; 557 } 558 559 void 560 IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error) 561 { 562 error.Clear(); 563 564 AllocationMap::iterator iter = FindAllocation(process_address, size); 565 566 if (iter == m_allocations.end()) 567 { 568 lldb::ProcessSP process_sp = m_process_wp.lock(); 569 570 if (process_sp) 571 { 572 process_sp->WriteMemory(process_address, bytes, size, error); 573 return; 574 } 575 576 error.SetErrorToGenericError(); 577 error.SetErrorString("Couldn't write: no allocation contains the target range and the process doesn't exist"); 578 return; 579 } 580 581 Allocation &allocation = iter->second; 582 583 uint64_t offset = process_address - allocation.m_process_start; 584 585 lldb::ProcessSP process_sp; 586 587 switch (allocation.m_policy) 588 { 589 default: 590 error.SetErrorToGenericError(); 591 error.SetErrorString("Couldn't write: invalid allocation policy"); 592 return; 593 case eAllocationPolicyHostOnly: 594 if (!allocation.m_data.GetByteSize()) 595 { 596 error.SetErrorToGenericError(); 597 error.SetErrorString("Couldn't write: data buffer is empty"); 598 return; 599 } 600 ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size); 601 break; 602 case eAllocationPolicyMirror: 603 if (!allocation.m_data.GetByteSize()) 604 { 605 error.SetErrorToGenericError(); 606 error.SetErrorString("Couldn't write: data buffer is empty"); 607 return; 608 } 609 ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size); 610 process_sp = m_process_wp.lock(); 611 if (process_sp) 612 { 613 process_sp->WriteMemory(process_address, bytes, size, error); 614 if (!error.Success()) 615 return; 616 } 617 break; 618 case eAllocationPolicyProcessOnly: 619 process_sp = m_process_wp.lock(); 620 if (process_sp) 621 { 622 process_sp->WriteMemory(process_address, bytes, size, error); 623 if (!error.Success()) 624 return; 625 } 626 break; 627 } 628 629 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) 630 { 631 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")", 632 (uint64_t)process_address, 633 (uint64_t)bytes, 634 (uint64_t)size, 635 (uint64_t)allocation.m_process_start, 636 (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); 637 } 638 } 639 640 void 641 IRMemoryMap::WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error) 642 { 643 error.Clear(); 644 645 if (size == UINT32_MAX) 646 size = scalar.GetByteSize(); 647 648 if (size > 0) 649 { 650 uint8_t buf[32]; 651 const size_t mem_size = scalar.GetAsMemoryData (buf, size, GetByteOrder(), error); 652 if (mem_size > 0) 653 { 654 return WriteMemory(process_address, buf, mem_size, error); 655 } 656 else 657 { 658 error.SetErrorToGenericError(); 659 error.SetErrorString ("Couldn't write scalar: failed to get scalar as memory data"); 660 } 661 } 662 else 663 { 664 error.SetErrorToGenericError(); 665 error.SetErrorString ("Couldn't write scalar: its size was zero"); 666 } 667 return; 668 } 669 670 void 671 IRMemoryMap::WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error) 672 { 673 error.Clear(); 674 675 Scalar scalar(address); 676 677 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error); 678 } 679 680 void 681 IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error) 682 { 683 error.Clear(); 684 685 AllocationMap::iterator iter = FindAllocation(process_address, size); 686 687 if (iter == m_allocations.end()) 688 { 689 lldb::ProcessSP process_sp = m_process_wp.lock(); 690 691 if (process_sp) 692 { 693 process_sp->ReadMemory(process_address, bytes, size, error); 694 return; 695 } 696 697 lldb::TargetSP target_sp = m_target_wp.lock(); 698 699 if (target_sp) 700 { 701 Address absolute_address(process_address); 702 target_sp->ReadMemory(absolute_address, false, bytes, size, error); 703 return; 704 } 705 706 error.SetErrorToGenericError(); 707 error.SetErrorString("Couldn't read: no allocation contains the target range, and neither the process nor the target exist"); 708 return; 709 } 710 711 Allocation &allocation = iter->second; 712 713 uint64_t offset = process_address - allocation.m_process_start; 714 715 if (offset > allocation.m_size) 716 { 717 error.SetErrorToGenericError(); 718 error.SetErrorString("Couldn't read: data is not in the allocation"); 719 return; 720 } 721 722 lldb::ProcessSP process_sp; 723 724 switch (allocation.m_policy) 725 { 726 default: 727 error.SetErrorToGenericError(); 728 error.SetErrorString("Couldn't read: invalid allocation policy"); 729 return; 730 case eAllocationPolicyHostOnly: 731 if (!allocation.m_data.GetByteSize()) 732 { 733 error.SetErrorToGenericError(); 734 error.SetErrorString("Couldn't read: data buffer is empty"); 735 return; 736 } 737 if (allocation.m_data.GetByteSize() < offset + size) 738 { 739 error.SetErrorToGenericError(); 740 error.SetErrorString("Couldn't read: not enough underlying data"); 741 return; 742 } 743 744 ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size); 745 break; 746 case eAllocationPolicyMirror: 747 process_sp = m_process_wp.lock(); 748 if (process_sp) 749 { 750 process_sp->ReadMemory(process_address, bytes, size, error); 751 if (!error.Success()) 752 return; 753 } 754 else 755 { 756 if (!allocation.m_data.GetByteSize()) 757 { 758 error.SetErrorToGenericError(); 759 error.SetErrorString("Couldn't read: data buffer is empty"); 760 return; 761 } 762 ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size); 763 } 764 break; 765 case eAllocationPolicyProcessOnly: 766 process_sp = m_process_wp.lock(); 767 if (process_sp) 768 { 769 process_sp->ReadMemory(process_address, bytes, size, error); 770 if (!error.Success()) 771 return; 772 } 773 break; 774 } 775 776 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) 777 { 778 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")", 779 (uint64_t)process_address, 780 (uint64_t)bytes, 781 (uint64_t)size, 782 (uint64_t)allocation.m_process_start, 783 (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); 784 } 785 } 786 787 void 788 IRMemoryMap::ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error) 789 { 790 error.Clear(); 791 792 if (size > 0) 793 { 794 DataBufferHeap buf(size, 0); 795 ReadMemory(buf.GetBytes(), process_address, size, error); 796 797 if (!error.Success()) 798 return; 799 800 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize()); 801 802 lldb::offset_t offset = 0; 803 804 switch (size) 805 { 806 default: 807 error.SetErrorToGenericError(); 808 error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size); 809 return; 810 case 1: scalar = extractor.GetU8(&offset); break; 811 case 2: scalar = extractor.GetU16(&offset); break; 812 case 4: scalar = extractor.GetU32(&offset); break; 813 case 8: scalar = extractor.GetU64(&offset); break; 814 } 815 } 816 else 817 { 818 error.SetErrorToGenericError(); 819 error.SetErrorString ("Couldn't read scalar: its size was zero"); 820 } 821 return; 822 } 823 824 void 825 IRMemoryMap::ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error) 826 { 827 error.Clear(); 828 829 Scalar pointer_scalar; 830 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(), error); 831 832 if (!error.Success()) 833 return; 834 835 *address = pointer_scalar.ULongLong(); 836 837 return; 838 } 839 840 void 841 IRMemoryMap::GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error) 842 { 843 error.Clear(); 844 845 if (size > 0) 846 { 847 AllocationMap::iterator iter = FindAllocation(process_address, size); 848 849 if (iter == m_allocations.end()) 850 { 851 error.SetErrorToGenericError(); 852 error.SetErrorStringWithFormat("Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64 ")", process_address, process_address + size); 853 return; 854 } 855 856 Allocation &allocation = iter->second; 857 858 switch (allocation.m_policy) 859 { 860 default: 861 error.SetErrorToGenericError(); 862 error.SetErrorString("Couldn't get memory data: invalid allocation policy"); 863 return; 864 case eAllocationPolicyProcessOnly: 865 error.SetErrorToGenericError(); 866 error.SetErrorString("Couldn't get memory data: memory is only in the target"); 867 return; 868 case eAllocationPolicyMirror: 869 { 870 lldb::ProcessSP process_sp = m_process_wp.lock(); 871 872 if (!allocation.m_data.GetByteSize()) 873 { 874 error.SetErrorToGenericError(); 875 error.SetErrorString("Couldn't get memory data: data buffer is empty"); 876 return; 877 } 878 if (process_sp) 879 { 880 process_sp->ReadMemory(allocation.m_process_start, allocation.m_data.GetBytes(), allocation.m_data.GetByteSize(), error); 881 if (!error.Success()) 882 return; 883 uint64_t offset = process_address - allocation.m_process_start; 884 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); 885 return; 886 } 887 } 888 break; 889 case eAllocationPolicyHostOnly: 890 if (!allocation.m_data.GetByteSize()) 891 { 892 error.SetErrorToGenericError(); 893 error.SetErrorString("Couldn't get memory data: data buffer is empty"); 894 return; 895 } 896 uint64_t offset = process_address - allocation.m_process_start; 897 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); 898 return; 899 } 900 } 901 else 902 { 903 error.SetErrorToGenericError(); 904 error.SetErrorString ("Couldn't get memory data: its size was zero"); 905 return; 906 } 907 } 908