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