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