1 //===-- Materializer.cpp ----------------------------------------*- C++ -*-===// 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 9 #include "lldb/Expression/Materializer.h" 10 #include "lldb/Core/DumpDataExtractor.h" 11 #include "lldb/Core/ValueObjectConstResult.h" 12 #include "lldb/Core/ValueObjectVariable.h" 13 #include "lldb/Expression/ExpressionVariable.h" 14 #include "lldb/Symbol/Symbol.h" 15 #include "lldb/Symbol/Type.h" 16 #include "lldb/Symbol/Variable.h" 17 #include "lldb/Target/ExecutionContext.h" 18 #include "lldb/Target/RegisterContext.h" 19 #include "lldb/Target/StackFrame.h" 20 #include "lldb/Target/Target.h" 21 #include "lldb/Target/Thread.h" 22 #include "lldb/Utility/Log.h" 23 #include "lldb/Utility/RegisterValue.h" 24 25 #include <memory> 26 27 using namespace lldb_private; 28 29 uint32_t Materializer::AddStructMember(Entity &entity) { 30 uint32_t size = entity.GetSize(); 31 uint32_t alignment = entity.GetAlignment(); 32 33 uint32_t ret; 34 35 if (m_current_offset == 0) 36 m_struct_alignment = alignment; 37 38 if (m_current_offset % alignment) 39 m_current_offset += (alignment - (m_current_offset % alignment)); 40 41 ret = m_current_offset; 42 43 m_current_offset += size; 44 45 return ret; 46 } 47 48 void Materializer::Entity::SetSizeAndAlignmentFromType(CompilerType &type) { 49 if (llvm::Optional<uint64_t> size = type.GetByteSize(nullptr)) 50 m_size = *size; 51 52 uint32_t bit_alignment = type.GetTypeBitAlign(); 53 54 if (bit_alignment % 8) { 55 bit_alignment += 8; 56 bit_alignment &= ~((uint32_t)0x111u); 57 } 58 59 m_alignment = bit_alignment / 8; 60 } 61 62 class EntityPersistentVariable : public Materializer::Entity { 63 public: 64 EntityPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp, 65 Materializer::PersistentVariableDelegate *delegate) 66 : Entity(), m_persistent_variable_sp(persistent_variable_sp), 67 m_delegate(delegate) { 68 // Hard-coding to maximum size of a pointer since persistent variables are 69 // materialized by reference 70 m_size = 8; 71 m_alignment = 8; 72 } 73 74 void MakeAllocation(IRMemoryMap &map, Status &err) { 75 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 76 77 // Allocate a spare memory area to store the persistent variable's 78 // contents. 79 80 Status allocate_error; 81 const bool zero_memory = false; 82 83 lldb::addr_t mem = map.Malloc( 84 m_persistent_variable_sp->GetByteSize(), 8, 85 lldb::ePermissionsReadable | lldb::ePermissionsWritable, 86 IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error); 87 88 if (!allocate_error.Success()) { 89 err.SetErrorStringWithFormat( 90 "couldn't allocate a memory area to store %s: %s", 91 m_persistent_variable_sp->GetName().GetCString(), 92 allocate_error.AsCString()); 93 return; 94 } 95 96 if (log) 97 log->Printf("Allocated %s (0x%" PRIx64 ") successfully", 98 m_persistent_variable_sp->GetName().GetCString(), mem); 99 100 // Put the location of the spare memory into the live data of the 101 // ValueObject. 102 103 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create( 104 map.GetBestExecutionContextScope(), 105 m_persistent_variable_sp->GetCompilerType(), 106 m_persistent_variable_sp->GetName(), mem, eAddressTypeLoad, 107 map.GetAddressByteSize()); 108 109 // Clear the flag if the variable will never be deallocated. 110 111 if (m_persistent_variable_sp->m_flags & 112 ExpressionVariable::EVKeepInTarget) { 113 Status leak_error; 114 map.Leak(mem, leak_error); 115 m_persistent_variable_sp->m_flags &= 116 ~ExpressionVariable::EVNeedsAllocation; 117 } 118 119 // Write the contents of the variable to the area. 120 121 Status write_error; 122 123 map.WriteMemory(mem, m_persistent_variable_sp->GetValueBytes(), 124 m_persistent_variable_sp->GetByteSize(), write_error); 125 126 if (!write_error.Success()) { 127 err.SetErrorStringWithFormat( 128 "couldn't write %s to the target: %s", 129 m_persistent_variable_sp->GetName().AsCString(), 130 write_error.AsCString()); 131 return; 132 } 133 } 134 135 void DestroyAllocation(IRMemoryMap &map, Status &err) { 136 Status deallocate_error; 137 138 map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue() 139 .GetScalar() 140 .ULongLong(), 141 deallocate_error); 142 143 m_persistent_variable_sp->m_live_sp.reset(); 144 145 if (!deallocate_error.Success()) { 146 err.SetErrorStringWithFormat( 147 "couldn't deallocate memory for %s: %s", 148 m_persistent_variable_sp->GetName().GetCString(), 149 deallocate_error.AsCString()); 150 } 151 } 152 153 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 154 lldb::addr_t process_address, Status &err) override { 155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 156 157 const lldb::addr_t load_addr = process_address + m_offset; 158 159 if (log) { 160 log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64 161 ", m_name = %s, m_flags = 0x%hx]", 162 (uint64_t)load_addr, 163 m_persistent_variable_sp->GetName().AsCString(), 164 m_persistent_variable_sp->m_flags); 165 } 166 167 if (m_persistent_variable_sp->m_flags & 168 ExpressionVariable::EVNeedsAllocation) { 169 MakeAllocation(map, err); 170 m_persistent_variable_sp->m_flags |= 171 ExpressionVariable::EVIsLLDBAllocated; 172 173 if (!err.Success()) 174 return; 175 } 176 177 if ((m_persistent_variable_sp->m_flags & 178 ExpressionVariable::EVIsProgramReference && 179 m_persistent_variable_sp->m_live_sp) || 180 m_persistent_variable_sp->m_flags & 181 ExpressionVariable::EVIsLLDBAllocated) { 182 Status write_error; 183 184 map.WriteScalarToMemory( 185 load_addr, 186 m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(), 187 map.GetAddressByteSize(), write_error); 188 189 if (!write_error.Success()) { 190 err.SetErrorStringWithFormat( 191 "couldn't write the location of %s to memory: %s", 192 m_persistent_variable_sp->GetName().AsCString(), 193 write_error.AsCString()); 194 } 195 } else { 196 err.SetErrorStringWithFormat( 197 "no materialization happened for persistent variable %s", 198 m_persistent_variable_sp->GetName().AsCString()); 199 return; 200 } 201 } 202 203 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 204 lldb::addr_t process_address, lldb::addr_t frame_top, 205 lldb::addr_t frame_bottom, Status &err) override { 206 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 207 208 const lldb::addr_t load_addr = process_address + m_offset; 209 210 if (log) { 211 log->Printf( 212 "EntityPersistentVariable::Dematerialize [address = 0x%" PRIx64 213 ", m_name = %s, m_flags = 0x%hx]", 214 (uint64_t)process_address + m_offset, 215 m_persistent_variable_sp->GetName().AsCString(), 216 m_persistent_variable_sp->m_flags); 217 } 218 219 if (m_delegate) { 220 m_delegate->DidDematerialize(m_persistent_variable_sp); 221 } 222 223 if ((m_persistent_variable_sp->m_flags & 224 ExpressionVariable::EVIsLLDBAllocated) || 225 (m_persistent_variable_sp->m_flags & 226 ExpressionVariable::EVIsProgramReference)) { 227 if (m_persistent_variable_sp->m_flags & 228 ExpressionVariable::EVIsProgramReference && 229 !m_persistent_variable_sp->m_live_sp) { 230 // If the reference comes from the program, then the 231 // ClangExpressionVariable's live variable data hasn't been set up yet. 232 // Do this now. 233 234 lldb::addr_t location; 235 Status read_error; 236 237 map.ReadPointerFromMemory(&location, load_addr, read_error); 238 239 if (!read_error.Success()) { 240 err.SetErrorStringWithFormat( 241 "couldn't read the address of program-allocated variable %s: %s", 242 m_persistent_variable_sp->GetName().GetCString(), 243 read_error.AsCString()); 244 return; 245 } 246 247 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create( 248 map.GetBestExecutionContextScope(), 249 m_persistent_variable_sp.get()->GetCompilerType(), 250 m_persistent_variable_sp->GetName(), location, eAddressTypeLoad, 251 m_persistent_variable_sp->GetByteSize()); 252 253 if (frame_top != LLDB_INVALID_ADDRESS && 254 frame_bottom != LLDB_INVALID_ADDRESS && location >= frame_bottom && 255 location <= frame_top) { 256 // If the variable is resident in the stack frame created by the 257 // expression, then it cannot be relied upon to stay around. We 258 // treat it as needing reallocation. 259 m_persistent_variable_sp->m_flags |= 260 ExpressionVariable::EVIsLLDBAllocated; 261 m_persistent_variable_sp->m_flags |= 262 ExpressionVariable::EVNeedsAllocation; 263 m_persistent_variable_sp->m_flags |= 264 ExpressionVariable::EVNeedsFreezeDry; 265 m_persistent_variable_sp->m_flags &= 266 ~ExpressionVariable::EVIsProgramReference; 267 } 268 } 269 270 lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue() 271 .GetScalar() 272 .ULongLong(); 273 274 if (!m_persistent_variable_sp->m_live_sp) { 275 err.SetErrorStringWithFormat( 276 "couldn't find the memory area used to store %s", 277 m_persistent_variable_sp->GetName().GetCString()); 278 return; 279 } 280 281 if (m_persistent_variable_sp->m_live_sp->GetValue() 282 .GetValueAddressType() != eAddressTypeLoad) { 283 err.SetErrorStringWithFormat( 284 "the address of the memory area for %s is in an incorrect format", 285 m_persistent_variable_sp->GetName().GetCString()); 286 return; 287 } 288 289 if (m_persistent_variable_sp->m_flags & 290 ExpressionVariable::EVNeedsFreezeDry || 291 m_persistent_variable_sp->m_flags & 292 ExpressionVariable::EVKeepInTarget) { 293 if (log) 294 log->Printf( 295 "Dematerializing %s from 0x%" PRIx64 " (size = %llu)", 296 m_persistent_variable_sp->GetName().GetCString(), (uint64_t)mem, 297 (unsigned long long)m_persistent_variable_sp->GetByteSize()); 298 299 // Read the contents of the spare memory area 300 301 m_persistent_variable_sp->ValueUpdated(); 302 303 Status read_error; 304 305 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(), mem, 306 m_persistent_variable_sp->GetByteSize(), read_error); 307 308 if (!read_error.Success()) { 309 err.SetErrorStringWithFormat( 310 "couldn't read the contents of %s from memory: %s", 311 m_persistent_variable_sp->GetName().GetCString(), 312 read_error.AsCString()); 313 return; 314 } 315 316 m_persistent_variable_sp->m_flags &= 317 ~ExpressionVariable::EVNeedsFreezeDry; 318 } 319 } else { 320 err.SetErrorStringWithFormat( 321 "no dematerialization happened for persistent variable %s", 322 m_persistent_variable_sp->GetName().AsCString()); 323 return; 324 } 325 326 lldb::ProcessSP process_sp = 327 map.GetBestExecutionContextScope()->CalculateProcess(); 328 if (!process_sp || !process_sp->CanJIT()) { 329 // Allocations are not persistent so persistent variables cannot stay 330 // materialized. 331 332 m_persistent_variable_sp->m_flags |= 333 ExpressionVariable::EVNeedsAllocation; 334 335 DestroyAllocation(map, err); 336 if (!err.Success()) 337 return; 338 } else if (m_persistent_variable_sp->m_flags & 339 ExpressionVariable::EVNeedsAllocation && 340 !(m_persistent_variable_sp->m_flags & 341 ExpressionVariable::EVKeepInTarget)) { 342 DestroyAllocation(map, err); 343 if (!err.Success()) 344 return; 345 } 346 } 347 348 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, 349 Log *log) override { 350 StreamString dump_stream; 351 352 Status err; 353 354 const lldb::addr_t load_addr = process_address + m_offset; 355 356 dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n", 357 load_addr, 358 m_persistent_variable_sp->GetName().AsCString()); 359 360 { 361 dump_stream.Printf("Pointer:\n"); 362 363 DataBufferHeap data(m_size, 0); 364 365 map.ReadMemory(data.GetBytes(), load_addr, m_size, err); 366 367 if (!err.Success()) { 368 dump_stream.Printf(" <could not be read>\n"); 369 } else { 370 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 371 load_addr); 372 373 dump_stream.PutChar('\n'); 374 } 375 } 376 377 { 378 dump_stream.Printf("Target:\n"); 379 380 lldb::addr_t target_address; 381 382 map.ReadPointerFromMemory(&target_address, load_addr, err); 383 384 if (!err.Success()) { 385 dump_stream.Printf(" <could not be read>\n"); 386 } else { 387 DataBufferHeap data(m_persistent_variable_sp->GetByteSize(), 0); 388 389 map.ReadMemory(data.GetBytes(), target_address, 390 m_persistent_variable_sp->GetByteSize(), err); 391 392 if (!err.Success()) { 393 dump_stream.Printf(" <could not be read>\n"); 394 } else { 395 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 396 target_address); 397 398 dump_stream.PutChar('\n'); 399 } 400 } 401 } 402 403 log->PutString(dump_stream.GetString()); 404 } 405 406 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {} 407 408 private: 409 lldb::ExpressionVariableSP m_persistent_variable_sp; 410 Materializer::PersistentVariableDelegate *m_delegate; 411 }; 412 413 uint32_t Materializer::AddPersistentVariable( 414 lldb::ExpressionVariableSP &persistent_variable_sp, 415 PersistentVariableDelegate *delegate, Status &err) { 416 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP()); 417 iter->reset(new EntityPersistentVariable(persistent_variable_sp, delegate)); 418 uint32_t ret = AddStructMember(**iter); 419 (*iter)->SetOffset(ret); 420 return ret; 421 } 422 423 class EntityVariable : public Materializer::Entity { 424 public: 425 EntityVariable(lldb::VariableSP &variable_sp) 426 : Entity(), m_variable_sp(variable_sp), m_is_reference(false), 427 m_temporary_allocation(LLDB_INVALID_ADDRESS), 428 m_temporary_allocation_size(0) { 429 // Hard-coding to maximum size of a pointer since all variables are 430 // materialized by reference 431 m_size = 8; 432 m_alignment = 8; 433 m_is_reference = 434 m_variable_sp->GetType()->GetForwardCompilerType().IsReferenceType(); 435 } 436 437 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 438 lldb::addr_t process_address, Status &err) override { 439 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 440 441 const lldb::addr_t load_addr = process_address + m_offset; 442 if (log) { 443 log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64 444 ", m_variable_sp = %s]", 445 (uint64_t)load_addr, m_variable_sp->GetName().AsCString()); 446 } 447 448 ExecutionContextScope *scope = frame_sp.get(); 449 450 if (!scope) 451 scope = map.GetBestExecutionContextScope(); 452 453 lldb::ValueObjectSP valobj_sp = 454 ValueObjectVariable::Create(scope, m_variable_sp); 455 456 if (!valobj_sp) { 457 err.SetErrorStringWithFormat( 458 "couldn't get a value object for variable %s", 459 m_variable_sp->GetName().AsCString()); 460 return; 461 } 462 463 Status valobj_error = valobj_sp->GetError(); 464 465 if (valobj_error.Fail()) { 466 err.SetErrorStringWithFormat("couldn't get the value of variable %s: %s", 467 m_variable_sp->GetName().AsCString(), 468 valobj_error.AsCString()); 469 return; 470 } 471 472 if (m_is_reference) { 473 DataExtractor valobj_extractor; 474 Status extract_error; 475 valobj_sp->GetData(valobj_extractor, extract_error); 476 477 if (!extract_error.Success()) { 478 err.SetErrorStringWithFormat( 479 "couldn't read contents of reference variable %s: %s", 480 m_variable_sp->GetName().AsCString(), extract_error.AsCString()); 481 return; 482 } 483 484 lldb::offset_t offset = 0; 485 lldb::addr_t reference_addr = valobj_extractor.GetAddress(&offset); 486 487 Status write_error; 488 map.WritePointerToMemory(load_addr, reference_addr, write_error); 489 490 if (!write_error.Success()) { 491 err.SetErrorStringWithFormat("couldn't write the contents of reference " 492 "variable %s to memory: %s", 493 m_variable_sp->GetName().AsCString(), 494 write_error.AsCString()); 495 return; 496 } 497 } else { 498 AddressType address_type = eAddressTypeInvalid; 499 const bool scalar_is_load_address = false; 500 lldb::addr_t addr_of_valobj = 501 valobj_sp->GetAddressOf(scalar_is_load_address, &address_type); 502 if (addr_of_valobj != LLDB_INVALID_ADDRESS) { 503 Status write_error; 504 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error); 505 506 if (!write_error.Success()) { 507 err.SetErrorStringWithFormat( 508 "couldn't write the address of variable %s to memory: %s", 509 m_variable_sp->GetName().AsCString(), write_error.AsCString()); 510 return; 511 } 512 } else { 513 DataExtractor data; 514 Status extract_error; 515 valobj_sp->GetData(data, extract_error); 516 if (!extract_error.Success()) { 517 err.SetErrorStringWithFormat("couldn't get the value of %s: %s", 518 m_variable_sp->GetName().AsCString(), 519 extract_error.AsCString()); 520 return; 521 } 522 523 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) { 524 err.SetErrorStringWithFormat( 525 "trying to create a temporary region for %s but one exists", 526 m_variable_sp->GetName().AsCString()); 527 return; 528 } 529 530 if (data.GetByteSize() < m_variable_sp->GetType()->GetByteSize()) { 531 if (data.GetByteSize() == 0 && 532 !m_variable_sp->LocationExpression().IsValid()) { 533 err.SetErrorStringWithFormat("the variable '%s' has no location, " 534 "it may have been optimized out", 535 m_variable_sp->GetName().AsCString()); 536 } else { 537 err.SetErrorStringWithFormat( 538 "size of variable %s (%" PRIu64 539 ") is larger than the ValueObject's size (%" PRIu64 ")", 540 m_variable_sp->GetName().AsCString(), 541 m_variable_sp->GetType()->GetByteSize().getValueOr(0), 542 data.GetByteSize()); 543 } 544 return; 545 } 546 547 size_t bit_align = 548 m_variable_sp->GetType()->GetLayoutCompilerType().GetTypeBitAlign(); 549 size_t byte_align = (bit_align + 7) / 8; 550 551 if (!byte_align) 552 byte_align = 1; 553 554 Status alloc_error; 555 const bool zero_memory = false; 556 557 m_temporary_allocation = map.Malloc( 558 data.GetByteSize(), byte_align, 559 lldb::ePermissionsReadable | lldb::ePermissionsWritable, 560 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error); 561 562 m_temporary_allocation_size = data.GetByteSize(); 563 564 m_original_data = std::make_shared<DataBufferHeap>(data.GetDataStart(), 565 data.GetByteSize()); 566 567 if (!alloc_error.Success()) { 568 err.SetErrorStringWithFormat( 569 "couldn't allocate a temporary region for %s: %s", 570 m_variable_sp->GetName().AsCString(), alloc_error.AsCString()); 571 return; 572 } 573 574 Status write_error; 575 576 map.WriteMemory(m_temporary_allocation, data.GetDataStart(), 577 data.GetByteSize(), write_error); 578 579 if (!write_error.Success()) { 580 err.SetErrorStringWithFormat( 581 "couldn't write to the temporary region for %s: %s", 582 m_variable_sp->GetName().AsCString(), write_error.AsCString()); 583 return; 584 } 585 586 Status pointer_write_error; 587 588 map.WritePointerToMemory(load_addr, m_temporary_allocation, 589 pointer_write_error); 590 591 if (!pointer_write_error.Success()) { 592 err.SetErrorStringWithFormat( 593 "couldn't write the address of the temporary region for %s: %s", 594 m_variable_sp->GetName().AsCString(), 595 pointer_write_error.AsCString()); 596 } 597 } 598 } 599 } 600 601 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 602 lldb::addr_t process_address, lldb::addr_t frame_top, 603 lldb::addr_t frame_bottom, Status &err) override { 604 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 605 606 const lldb::addr_t load_addr = process_address + m_offset; 607 if (log) { 608 log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64 609 ", m_variable_sp = %s]", 610 (uint64_t)load_addr, m_variable_sp->GetName().AsCString()); 611 } 612 613 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) { 614 ExecutionContextScope *scope = frame_sp.get(); 615 616 if (!scope) 617 scope = map.GetBestExecutionContextScope(); 618 619 lldb::ValueObjectSP valobj_sp = 620 ValueObjectVariable::Create(scope, m_variable_sp); 621 622 if (!valobj_sp) { 623 err.SetErrorStringWithFormat( 624 "couldn't get a value object for variable %s", 625 m_variable_sp->GetName().AsCString()); 626 return; 627 } 628 629 lldb_private::DataExtractor data; 630 631 Status extract_error; 632 633 map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(), 634 extract_error); 635 636 if (!extract_error.Success()) { 637 err.SetErrorStringWithFormat("couldn't get the data for variable %s", 638 m_variable_sp->GetName().AsCString()); 639 return; 640 } 641 642 bool actually_write = true; 643 644 if (m_original_data) { 645 if ((data.GetByteSize() == m_original_data->GetByteSize()) && 646 !memcmp(m_original_data->GetBytes(), data.GetDataStart(), 647 data.GetByteSize())) { 648 actually_write = false; 649 } 650 } 651 652 Status set_error; 653 654 if (actually_write) { 655 valobj_sp->SetData(data, set_error); 656 657 if (!set_error.Success()) { 658 err.SetErrorStringWithFormat( 659 "couldn't write the new contents of %s back into the variable", 660 m_variable_sp->GetName().AsCString()); 661 return; 662 } 663 } 664 665 Status free_error; 666 667 map.Free(m_temporary_allocation, free_error); 668 669 if (!free_error.Success()) { 670 err.SetErrorStringWithFormat( 671 "couldn't free the temporary region for %s: %s", 672 m_variable_sp->GetName().AsCString(), free_error.AsCString()); 673 return; 674 } 675 676 m_original_data.reset(); 677 m_temporary_allocation = LLDB_INVALID_ADDRESS; 678 m_temporary_allocation_size = 0; 679 } 680 } 681 682 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, 683 Log *log) override { 684 StreamString dump_stream; 685 686 const lldb::addr_t load_addr = process_address + m_offset; 687 dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr); 688 689 Status err; 690 691 lldb::addr_t ptr = LLDB_INVALID_ADDRESS; 692 693 { 694 dump_stream.Printf("Pointer:\n"); 695 696 DataBufferHeap data(m_size, 0); 697 698 map.ReadMemory(data.GetBytes(), load_addr, m_size, err); 699 700 if (!err.Success()) { 701 dump_stream.Printf(" <could not be read>\n"); 702 } else { 703 DataExtractor extractor(data.GetBytes(), data.GetByteSize(), 704 map.GetByteOrder(), map.GetAddressByteSize()); 705 706 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 707 load_addr); 708 709 lldb::offset_t offset; 710 711 ptr = extractor.GetPointer(&offset); 712 713 dump_stream.PutChar('\n'); 714 } 715 } 716 717 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) { 718 dump_stream.Printf("Points to process memory:\n"); 719 } else { 720 dump_stream.Printf("Temporary allocation:\n"); 721 } 722 723 if (ptr == LLDB_INVALID_ADDRESS) { 724 dump_stream.Printf(" <could not be be found>\n"); 725 } else { 726 DataBufferHeap data(m_temporary_allocation_size, 0); 727 728 map.ReadMemory(data.GetBytes(), m_temporary_allocation, 729 m_temporary_allocation_size, err); 730 731 if (!err.Success()) { 732 dump_stream.Printf(" <could not be read>\n"); 733 } else { 734 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 735 load_addr); 736 737 dump_stream.PutChar('\n'); 738 } 739 } 740 741 log->PutString(dump_stream.GetString()); 742 } 743 744 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override { 745 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) { 746 Status free_error; 747 748 map.Free(m_temporary_allocation, free_error); 749 750 m_temporary_allocation = LLDB_INVALID_ADDRESS; 751 m_temporary_allocation_size = 0; 752 } 753 } 754 755 private: 756 lldb::VariableSP m_variable_sp; 757 bool m_is_reference; 758 lldb::addr_t m_temporary_allocation; 759 size_t m_temporary_allocation_size; 760 lldb::DataBufferSP m_original_data; 761 }; 762 763 uint32_t Materializer::AddVariable(lldb::VariableSP &variable_sp, Status &err) { 764 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP()); 765 iter->reset(new EntityVariable(variable_sp)); 766 uint32_t ret = AddStructMember(**iter); 767 (*iter)->SetOffset(ret); 768 return ret; 769 } 770 771 class EntityResultVariable : public Materializer::Entity { 772 public: 773 EntityResultVariable(const CompilerType &type, bool is_program_reference, 774 bool keep_in_memory, 775 Materializer::PersistentVariableDelegate *delegate) 776 : Entity(), m_type(type), m_is_program_reference(is_program_reference), 777 m_keep_in_memory(keep_in_memory), 778 m_temporary_allocation(LLDB_INVALID_ADDRESS), 779 m_temporary_allocation_size(0), m_delegate(delegate) { 780 // Hard-coding to maximum size of a pointer since all results are 781 // materialized by reference 782 m_size = 8; 783 m_alignment = 8; 784 } 785 786 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 787 lldb::addr_t process_address, Status &err) override { 788 if (!m_is_program_reference) { 789 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) { 790 err.SetErrorString("Trying to create a temporary region for the result " 791 "but one exists"); 792 return; 793 } 794 795 const lldb::addr_t load_addr = process_address + m_offset; 796 797 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope(); 798 799 llvm::Optional<uint64_t> byte_size = m_type.GetByteSize(exe_scope); 800 if (!byte_size) { 801 err.SetErrorString("can't get size of type"); 802 return; 803 } 804 size_t bit_align = m_type.GetTypeBitAlign(); 805 size_t byte_align = (bit_align + 7) / 8; 806 807 if (!byte_align) 808 byte_align = 1; 809 810 Status alloc_error; 811 const bool zero_memory = true; 812 813 m_temporary_allocation = map.Malloc( 814 *byte_size, byte_align, 815 lldb::ePermissionsReadable | lldb::ePermissionsWritable, 816 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error); 817 m_temporary_allocation_size = *byte_size; 818 819 if (!alloc_error.Success()) { 820 err.SetErrorStringWithFormat( 821 "couldn't allocate a temporary region for the result: %s", 822 alloc_error.AsCString()); 823 return; 824 } 825 826 Status pointer_write_error; 827 828 map.WritePointerToMemory(load_addr, m_temporary_allocation, 829 pointer_write_error); 830 831 if (!pointer_write_error.Success()) { 832 err.SetErrorStringWithFormat("couldn't write the address of the " 833 "temporary region for the result: %s", 834 pointer_write_error.AsCString()); 835 } 836 } 837 } 838 839 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 840 lldb::addr_t process_address, lldb::addr_t frame_top, 841 lldb::addr_t frame_bottom, Status &err) override { 842 err.Clear(); 843 844 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope(); 845 846 if (!exe_scope) { 847 err.SetErrorString("Couldn't dematerialize a result variable: invalid " 848 "execution context scope"); 849 return; 850 } 851 852 lldb::addr_t address; 853 Status read_error; 854 const lldb::addr_t load_addr = process_address + m_offset; 855 856 map.ReadPointerFromMemory(&address, load_addr, read_error); 857 858 if (!read_error.Success()) { 859 err.SetErrorString("Couldn't dematerialize a result variable: couldn't " 860 "read its address"); 861 return; 862 } 863 864 lldb::TargetSP target_sp = exe_scope->CalculateTarget(); 865 866 if (!target_sp) { 867 err.SetErrorString("Couldn't dematerialize a result variable: no target"); 868 return; 869 } 870 871 Status type_system_error; 872 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage( 873 &type_system_error, m_type.GetMinimumLanguage()); 874 875 if (!type_system) { 876 err.SetErrorStringWithFormat("Couldn't dematerialize a result variable: " 877 "couldn't get the corresponding type " 878 "system: %s", 879 type_system_error.AsCString()); 880 return; 881 } 882 883 PersistentExpressionState *persistent_state = 884 type_system->GetPersistentExpressionState(); 885 886 if (!persistent_state) { 887 err.SetErrorString("Couldn't dematerialize a result variable: " 888 "corresponding type system doesn't handle persistent " 889 "variables"); 890 return; 891 } 892 893 ConstString name = 894 m_delegate 895 ? m_delegate->GetName() 896 : persistent_state->GetNextPersistentVariableName( 897 *target_sp, persistent_state->GetPersistentVariablePrefix()); 898 899 lldb::ExpressionVariableSP ret = persistent_state->CreatePersistentVariable( 900 exe_scope, name, m_type, map.GetByteOrder(), map.GetAddressByteSize()); 901 902 if (!ret) { 903 err.SetErrorStringWithFormat("couldn't dematerialize a result variable: " 904 "failed to make persistent variable %s", 905 name.AsCString()); 906 return; 907 } 908 909 lldb::ProcessSP process_sp = 910 map.GetBestExecutionContextScope()->CalculateProcess(); 911 912 if (m_delegate) { 913 m_delegate->DidDematerialize(ret); 914 } 915 916 bool can_persist = 917 (m_is_program_reference && process_sp && process_sp->CanJIT() && 918 !(address >= frame_bottom && address < frame_top)); 919 920 if (can_persist && m_keep_in_memory) { 921 ret->m_live_sp = ValueObjectConstResult::Create(exe_scope, m_type, name, 922 address, eAddressTypeLoad, 923 map.GetAddressByteSize()); 924 } 925 926 ret->ValueUpdated(); 927 928 const size_t pvar_byte_size = ret->GetByteSize(); 929 uint8_t *pvar_data = ret->GetValueBytes(); 930 931 map.ReadMemory(pvar_data, address, pvar_byte_size, read_error); 932 933 if (!read_error.Success()) { 934 err.SetErrorString( 935 "Couldn't dematerialize a result variable: couldn't read its memory"); 936 return; 937 } 938 939 if (!can_persist || !m_keep_in_memory) { 940 ret->m_flags |= ExpressionVariable::EVNeedsAllocation; 941 942 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) { 943 Status free_error; 944 map.Free(m_temporary_allocation, free_error); 945 } 946 } else { 947 ret->m_flags |= ExpressionVariable::EVIsLLDBAllocated; 948 } 949 950 m_temporary_allocation = LLDB_INVALID_ADDRESS; 951 m_temporary_allocation_size = 0; 952 } 953 954 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, 955 Log *log) override { 956 StreamString dump_stream; 957 958 const lldb::addr_t load_addr = process_address + m_offset; 959 960 dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr); 961 962 Status err; 963 964 lldb::addr_t ptr = LLDB_INVALID_ADDRESS; 965 966 { 967 dump_stream.Printf("Pointer:\n"); 968 969 DataBufferHeap data(m_size, 0); 970 971 map.ReadMemory(data.GetBytes(), load_addr, m_size, err); 972 973 if (!err.Success()) { 974 dump_stream.Printf(" <could not be read>\n"); 975 } else { 976 DataExtractor extractor(data.GetBytes(), data.GetByteSize(), 977 map.GetByteOrder(), map.GetAddressByteSize()); 978 979 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 980 load_addr); 981 982 lldb::offset_t offset; 983 984 ptr = extractor.GetPointer(&offset); 985 986 dump_stream.PutChar('\n'); 987 } 988 } 989 990 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) { 991 dump_stream.Printf("Points to process memory:\n"); 992 } else { 993 dump_stream.Printf("Temporary allocation:\n"); 994 } 995 996 if (ptr == LLDB_INVALID_ADDRESS) { 997 dump_stream.Printf(" <could not be be found>\n"); 998 } else { 999 DataBufferHeap data(m_temporary_allocation_size, 0); 1000 1001 map.ReadMemory(data.GetBytes(), m_temporary_allocation, 1002 m_temporary_allocation_size, err); 1003 1004 if (!err.Success()) { 1005 dump_stream.Printf(" <could not be read>\n"); 1006 } else { 1007 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 1008 load_addr); 1009 1010 dump_stream.PutChar('\n'); 1011 } 1012 } 1013 1014 log->PutString(dump_stream.GetString()); 1015 } 1016 1017 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override { 1018 if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS) { 1019 Status free_error; 1020 1021 map.Free(m_temporary_allocation, free_error); 1022 } 1023 1024 m_temporary_allocation = LLDB_INVALID_ADDRESS; 1025 m_temporary_allocation_size = 0; 1026 } 1027 1028 private: 1029 CompilerType m_type; 1030 bool m_is_program_reference; 1031 bool m_keep_in_memory; 1032 1033 lldb::addr_t m_temporary_allocation; 1034 size_t m_temporary_allocation_size; 1035 Materializer::PersistentVariableDelegate *m_delegate; 1036 }; 1037 1038 uint32_t Materializer::AddResultVariable(const CompilerType &type, 1039 bool is_program_reference, 1040 bool keep_in_memory, 1041 PersistentVariableDelegate *delegate, 1042 Status &err) { 1043 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP()); 1044 iter->reset(new EntityResultVariable(type, is_program_reference, 1045 keep_in_memory, delegate)); 1046 uint32_t ret = AddStructMember(**iter); 1047 (*iter)->SetOffset(ret); 1048 return ret; 1049 } 1050 1051 class EntitySymbol : public Materializer::Entity { 1052 public: 1053 EntitySymbol(const Symbol &symbol) : Entity(), m_symbol(symbol) { 1054 // Hard-coding to maximum size of a symbol 1055 m_size = 8; 1056 m_alignment = 8; 1057 } 1058 1059 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 1060 lldb::addr_t process_address, Status &err) override { 1061 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1062 1063 const lldb::addr_t load_addr = process_address + m_offset; 1064 1065 if (log) { 1066 log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64 1067 ", m_symbol = %s]", 1068 (uint64_t)load_addr, m_symbol.GetName().AsCString()); 1069 } 1070 1071 const Address sym_address = m_symbol.GetAddress(); 1072 1073 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope(); 1074 1075 lldb::TargetSP target_sp; 1076 1077 if (exe_scope) 1078 target_sp = map.GetBestExecutionContextScope()->CalculateTarget(); 1079 1080 if (!target_sp) { 1081 err.SetErrorStringWithFormat( 1082 "couldn't resolve symbol %s because there is no target", 1083 m_symbol.GetName().AsCString()); 1084 return; 1085 } 1086 1087 lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get()); 1088 1089 if (resolved_address == LLDB_INVALID_ADDRESS) 1090 resolved_address = sym_address.GetFileAddress(); 1091 1092 Status pointer_write_error; 1093 1094 map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error); 1095 1096 if (!pointer_write_error.Success()) { 1097 err.SetErrorStringWithFormat( 1098 "couldn't write the address of symbol %s: %s", 1099 m_symbol.GetName().AsCString(), pointer_write_error.AsCString()); 1100 return; 1101 } 1102 } 1103 1104 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 1105 lldb::addr_t process_address, lldb::addr_t frame_top, 1106 lldb::addr_t frame_bottom, Status &err) override { 1107 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1108 1109 const lldb::addr_t load_addr = process_address + m_offset; 1110 1111 if (log) { 1112 log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64 1113 ", m_symbol = %s]", 1114 (uint64_t)load_addr, m_symbol.GetName().AsCString()); 1115 } 1116 1117 // no work needs to be done 1118 } 1119 1120 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, 1121 Log *log) override { 1122 StreamString dump_stream; 1123 1124 Status err; 1125 1126 const lldb::addr_t load_addr = process_address + m_offset; 1127 1128 dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr, 1129 m_symbol.GetName().AsCString()); 1130 1131 { 1132 dump_stream.Printf("Pointer:\n"); 1133 1134 DataBufferHeap data(m_size, 0); 1135 1136 map.ReadMemory(data.GetBytes(), load_addr, m_size, err); 1137 1138 if (!err.Success()) { 1139 dump_stream.Printf(" <could not be read>\n"); 1140 } else { 1141 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 1142 load_addr); 1143 1144 dump_stream.PutChar('\n'); 1145 } 1146 } 1147 1148 log->PutString(dump_stream.GetString()); 1149 } 1150 1151 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {} 1152 1153 private: 1154 Symbol m_symbol; 1155 }; 1156 1157 uint32_t Materializer::AddSymbol(const Symbol &symbol_sp, Status &err) { 1158 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP()); 1159 iter->reset(new EntitySymbol(symbol_sp)); 1160 uint32_t ret = AddStructMember(**iter); 1161 (*iter)->SetOffset(ret); 1162 return ret; 1163 } 1164 1165 class EntityRegister : public Materializer::Entity { 1166 public: 1167 EntityRegister(const RegisterInfo ®ister_info) 1168 : Entity(), m_register_info(register_info) { 1169 // Hard-coding alignment conservatively 1170 m_size = m_register_info.byte_size; 1171 m_alignment = m_register_info.byte_size; 1172 } 1173 1174 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 1175 lldb::addr_t process_address, Status &err) override { 1176 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1177 1178 const lldb::addr_t load_addr = process_address + m_offset; 1179 1180 if (log) { 1181 log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64 1182 ", m_register_info = %s]", 1183 (uint64_t)load_addr, m_register_info.name); 1184 } 1185 1186 RegisterValue reg_value; 1187 1188 if (!frame_sp.get()) { 1189 err.SetErrorStringWithFormat( 1190 "couldn't materialize register %s without a stack frame", 1191 m_register_info.name); 1192 return; 1193 } 1194 1195 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext(); 1196 1197 if (!reg_context_sp->ReadRegister(&m_register_info, reg_value)) { 1198 err.SetErrorStringWithFormat("couldn't read the value of register %s", 1199 m_register_info.name); 1200 return; 1201 } 1202 1203 DataExtractor register_data; 1204 1205 if (!reg_value.GetData(register_data)) { 1206 err.SetErrorStringWithFormat("couldn't get the data for register %s", 1207 m_register_info.name); 1208 return; 1209 } 1210 1211 if (register_data.GetByteSize() != m_register_info.byte_size) { 1212 err.SetErrorStringWithFormat( 1213 "data for register %s had size %llu but we expected %llu", 1214 m_register_info.name, (unsigned long long)register_data.GetByteSize(), 1215 (unsigned long long)m_register_info.byte_size); 1216 return; 1217 } 1218 1219 m_register_contents = std::make_shared<DataBufferHeap>( 1220 register_data.GetDataStart(), register_data.GetByteSize()); 1221 1222 Status write_error; 1223 1224 map.WriteMemory(load_addr, register_data.GetDataStart(), 1225 register_data.GetByteSize(), write_error); 1226 1227 if (!write_error.Success()) { 1228 err.SetErrorStringWithFormat( 1229 "couldn't write the contents of register %s: %s", 1230 m_register_info.name, write_error.AsCString()); 1231 return; 1232 } 1233 } 1234 1235 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 1236 lldb::addr_t process_address, lldb::addr_t frame_top, 1237 lldb::addr_t frame_bottom, Status &err) override { 1238 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)); 1239 1240 const lldb::addr_t load_addr = process_address + m_offset; 1241 1242 if (log) { 1243 log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64 1244 ", m_register_info = %s]", 1245 (uint64_t)load_addr, m_register_info.name); 1246 } 1247 1248 Status extract_error; 1249 1250 DataExtractor register_data; 1251 1252 if (!frame_sp.get()) { 1253 err.SetErrorStringWithFormat( 1254 "couldn't dematerialize register %s without a stack frame", 1255 m_register_info.name); 1256 return; 1257 } 1258 1259 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext(); 1260 1261 map.GetMemoryData(register_data, load_addr, m_register_info.byte_size, 1262 extract_error); 1263 1264 if (!extract_error.Success()) { 1265 err.SetErrorStringWithFormat("couldn't get the data for register %s: %s", 1266 m_register_info.name, 1267 extract_error.AsCString()); 1268 return; 1269 } 1270 1271 if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(), 1272 register_data.GetByteSize())) { 1273 // No write required, and in particular we avoid errors if the register 1274 // wasn't writable 1275 1276 m_register_contents.reset(); 1277 return; 1278 } 1279 1280 m_register_contents.reset(); 1281 1282 RegisterValue register_value( 1283 const_cast<uint8_t *>(register_data.GetDataStart()), 1284 register_data.GetByteSize(), register_data.GetByteOrder()); 1285 1286 if (!reg_context_sp->WriteRegister(&m_register_info, register_value)) { 1287 err.SetErrorStringWithFormat("couldn't write the value of register %s", 1288 m_register_info.name); 1289 return; 1290 } 1291 } 1292 1293 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, 1294 Log *log) override { 1295 StreamString dump_stream; 1296 1297 Status err; 1298 1299 const lldb::addr_t load_addr = process_address + m_offset; 1300 1301 dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr, 1302 m_register_info.name); 1303 1304 { 1305 dump_stream.Printf("Value:\n"); 1306 1307 DataBufferHeap data(m_size, 0); 1308 1309 map.ReadMemory(data.GetBytes(), load_addr, m_size, err); 1310 1311 if (!err.Success()) { 1312 dump_stream.Printf(" <could not be read>\n"); 1313 } else { 1314 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, 1315 load_addr); 1316 1317 dump_stream.PutChar('\n'); 1318 } 1319 } 1320 1321 log->PutString(dump_stream.GetString()); 1322 } 1323 1324 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {} 1325 1326 private: 1327 RegisterInfo m_register_info; 1328 lldb::DataBufferSP m_register_contents; 1329 }; 1330 1331 uint32_t Materializer::AddRegister(const RegisterInfo ®ister_info, 1332 Status &err) { 1333 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP()); 1334 iter->reset(new EntityRegister(register_info)); 1335 uint32_t ret = AddStructMember(**iter); 1336 (*iter)->SetOffset(ret); 1337 return ret; 1338 } 1339 1340 Materializer::Materializer() 1341 : m_dematerializer_wp(), m_current_offset(0), m_struct_alignment(8) {} 1342 1343 Materializer::~Materializer() { 1344 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock(); 1345 1346 if (dematerializer_sp) 1347 dematerializer_sp->Wipe(); 1348 } 1349 1350 Materializer::DematerializerSP 1351 Materializer::Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, 1352 lldb::addr_t process_address, Status &error) { 1353 ExecutionContextScope *exe_scope = frame_sp.get(); 1354 1355 if (!exe_scope) 1356 exe_scope = map.GetBestExecutionContextScope(); 1357 1358 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock(); 1359 1360 if (dematerializer_sp) { 1361 error.SetErrorToGenericError(); 1362 error.SetErrorString("Couldn't materialize: already materialized"); 1363 } 1364 1365 DematerializerSP ret( 1366 new Dematerializer(*this, frame_sp, map, process_address)); 1367 1368 if (!exe_scope) { 1369 error.SetErrorToGenericError(); 1370 error.SetErrorString("Couldn't materialize: target doesn't exist"); 1371 } 1372 1373 for (EntityUP &entity_up : m_entities) { 1374 entity_up->Materialize(frame_sp, map, process_address, error); 1375 1376 if (!error.Success()) 1377 return DematerializerSP(); 1378 } 1379 1380 if (Log *log = 1381 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) { 1382 log->Printf( 1383 "Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64 1384 ") materialized:", 1385 static_cast<void *>(frame_sp.get()), process_address); 1386 for (EntityUP &entity_up : m_entities) 1387 entity_up->DumpToLog(map, process_address, log); 1388 } 1389 1390 m_dematerializer_wp = ret; 1391 1392 return ret; 1393 } 1394 1395 void Materializer::Dematerializer::Dematerialize(Status &error, 1396 lldb::addr_t frame_bottom, 1397 lldb::addr_t frame_top) { 1398 lldb::StackFrameSP frame_sp; 1399 1400 lldb::ThreadSP thread_sp = m_thread_wp.lock(); 1401 if (thread_sp) 1402 frame_sp = thread_sp->GetFrameWithStackID(m_stack_id); 1403 1404 ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope(); 1405 1406 if (!IsValid()) { 1407 error.SetErrorToGenericError(); 1408 error.SetErrorString("Couldn't dematerialize: invalid dematerializer"); 1409 } 1410 1411 if (!exe_scope) { 1412 error.SetErrorToGenericError(); 1413 error.SetErrorString("Couldn't dematerialize: target is gone"); 1414 } else { 1415 if (Log *log = 1416 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) { 1417 log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address " 1418 "= 0x%" PRIx64 ") about to dematerialize:", 1419 static_cast<void *>(frame_sp.get()), m_process_address); 1420 for (EntityUP &entity_up : m_materializer->m_entities) 1421 entity_up->DumpToLog(*m_map, m_process_address, log); 1422 } 1423 1424 for (EntityUP &entity_up : m_materializer->m_entities) { 1425 entity_up->Dematerialize(frame_sp, *m_map, m_process_address, frame_top, 1426 frame_bottom, error); 1427 1428 if (!error.Success()) 1429 break; 1430 } 1431 } 1432 1433 Wipe(); 1434 } 1435 1436 void Materializer::Dematerializer::Wipe() { 1437 if (!IsValid()) 1438 return; 1439 1440 for (EntityUP &entity_up : m_materializer->m_entities) { 1441 entity_up->Wipe(*m_map, m_process_address); 1442 } 1443 1444 m_materializer = nullptr; 1445 m_map = nullptr; 1446 m_process_address = LLDB_INVALID_ADDRESS; 1447 } 1448 1449 Materializer::PersistentVariableDelegate::~PersistentVariableDelegate() = 1450 default; 1451