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