1 //===-- Materializer.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/Materializer.h" 11 #include "lldb/Core/DumpDataExtractor.h" 12 #include "lldb/Core/ValueObjectConstResult.h" 13 #include "lldb/Core/ValueObjectVariable.h" 14 #include "lldb/Expression/ExpressionVariable.h" 15 #include "lldb/Symbol/ClangASTContext.h" 16 #include "lldb/Symbol/Symbol.h" 17 #include "lldb/Symbol/Type.h" 18 #include "lldb/Symbol/Variable.h" 19 #include "lldb/Target/ExecutionContext.h" 20 #include "lldb/Target/RegisterContext.h" 21 #include "lldb/Target/StackFrame.h" 22 #include "lldb/Target/Target.h" 23 #include "lldb/Target/Thread.h" 24 #include "lldb/Utility/Log.h" 25 #include "lldb/Utility/RegisterValue.h" 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(), 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