1 //===-- ValueObject.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/Core/ValueObject.h" 11 12 // C Includes 13 #include <stdlib.h> 14 15 // C++ Includes 16 // Other libraries and framework includes 17 #include "llvm/Support/raw_ostream.h" 18 #include "clang/AST/Type.h" 19 20 // Project includes 21 #include "lldb/Core/DataBufferHeap.h" 22 #include "lldb/Core/StreamString.h" 23 #include "lldb/Core/ValueObjectChild.h" 24 #include "lldb/Core/ValueObjectConstResult.h" 25 #include "lldb/Core/ValueObjectDynamicValue.h" 26 #include "lldb/Core/ValueObjectList.h" 27 28 #include "lldb/Host/Endian.h" 29 30 #include "lldb/Symbol/ClangASTType.h" 31 #include "lldb/Symbol/ClangASTContext.h" 32 #include "lldb/Symbol/Type.h" 33 34 #include "lldb/Target/ExecutionContext.h" 35 #include "lldb/Target/LanguageRuntime.h" 36 #include "lldb/Target/Process.h" 37 #include "lldb/Target/RegisterContext.h" 38 #include "lldb/Target/Target.h" 39 #include "lldb/Target/Thread.h" 40 41 using namespace lldb; 42 using namespace lldb_private; 43 44 static lldb::user_id_t g_value_obj_uid = 0; 45 46 //---------------------------------------------------------------------- 47 // ValueObject constructor 48 //---------------------------------------------------------------------- 49 ValueObject::ValueObject (ValueObject &parent) : 50 UserID (++g_value_obj_uid), // Unique identifier for every value object 51 m_parent (&parent), 52 m_update_point (parent.GetUpdatePoint ()), 53 m_name (), 54 m_data (), 55 m_value (), 56 m_error (), 57 m_value_str (), 58 m_old_value_str (), 59 m_location_str (), 60 m_summary_str (), 61 m_object_desc_str (), 62 m_manager(parent.GetManager()), 63 m_children (), 64 m_synthetic_children (), 65 m_dynamic_value (NULL), 66 m_deref_valobj(NULL), 67 m_format (eFormatDefault), 68 m_value_is_valid (false), 69 m_value_did_change (false), 70 m_children_count_valid (false), 71 m_old_value_valid (false), 72 m_pointers_point_to_load_addrs (false), 73 m_is_deref_of_parent (false) 74 { 75 m_manager->ManageObject(this); 76 } 77 78 //---------------------------------------------------------------------- 79 // ValueObject constructor 80 //---------------------------------------------------------------------- 81 ValueObject::ValueObject (ExecutionContextScope *exe_scope) : 82 UserID (++g_value_obj_uid), // Unique identifier for every value object 83 m_parent (NULL), 84 m_update_point (exe_scope), 85 m_name (), 86 m_data (), 87 m_value (), 88 m_error (), 89 m_value_str (), 90 m_old_value_str (), 91 m_location_str (), 92 m_summary_str (), 93 m_object_desc_str (), 94 m_manager(), 95 m_children (), 96 m_synthetic_children (), 97 m_dynamic_value (NULL), 98 m_deref_valobj(NULL), 99 m_format (eFormatDefault), 100 m_value_is_valid (false), 101 m_value_did_change (false), 102 m_children_count_valid (false), 103 m_old_value_valid (false), 104 m_pointers_point_to_load_addrs (false), 105 m_is_deref_of_parent (false) 106 { 107 m_manager = new ValueObjectManager(); 108 m_manager->ManageObject (this); 109 } 110 111 //---------------------------------------------------------------------- 112 // Destructor 113 //---------------------------------------------------------------------- 114 ValueObject::~ValueObject () 115 { 116 } 117 118 bool 119 ValueObject::UpdateValueIfNeeded () 120 { 121 // If this is a constant value, then our success is predicated on whether 122 // we have an error or not 123 if (GetIsConstant()) 124 return m_error.Success(); 125 126 bool first_update = m_update_point.IsFirstEvaluation(); 127 128 if (m_update_point.NeedsUpdating()) 129 { 130 m_update_point.SetUpdated(); 131 132 // Save the old value using swap to avoid a string copy which 133 // also will clear our m_value_str 134 if (m_value_str.empty()) 135 { 136 m_old_value_valid = false; 137 } 138 else 139 { 140 m_old_value_valid = true; 141 m_old_value_str.swap (m_value_str); 142 m_value_str.clear(); 143 } 144 m_location_str.clear(); 145 m_summary_str.clear(); 146 m_object_desc_str.clear(); 147 148 const bool value_was_valid = GetValueIsValid(); 149 SetValueDidChange (false); 150 151 m_error.Clear(); 152 153 // Call the pure virtual function to update the value 154 bool success = UpdateValue (); 155 156 SetValueIsValid (success); 157 158 if (first_update) 159 SetValueDidChange (false); 160 else if (!m_value_did_change && success == false) 161 { 162 // The value wasn't gotten successfully, so we mark this 163 // as changed if the value used to be valid and now isn't 164 SetValueDidChange (value_was_valid); 165 } 166 } 167 return m_error.Success(); 168 } 169 170 DataExtractor & 171 ValueObject::GetDataExtractor () 172 { 173 UpdateValueIfNeeded(); 174 return m_data; 175 } 176 177 const Error & 178 ValueObject::GetError() const 179 { 180 return m_error; 181 } 182 183 const ConstString & 184 ValueObject::GetName() const 185 { 186 return m_name; 187 } 188 189 const char * 190 ValueObject::GetLocationAsCString () 191 { 192 if (UpdateValueIfNeeded()) 193 { 194 if (m_location_str.empty()) 195 { 196 StreamString sstr; 197 198 switch (m_value.GetValueType()) 199 { 200 default: 201 break; 202 203 case Value::eValueTypeScalar: 204 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) 205 { 206 RegisterInfo *reg_info = m_value.GetRegisterInfo(); 207 if (reg_info) 208 { 209 if (reg_info->name) 210 m_location_str = reg_info->name; 211 else if (reg_info->alt_name) 212 m_location_str = reg_info->alt_name; 213 break; 214 } 215 } 216 m_location_str = "scalar"; 217 break; 218 219 case Value::eValueTypeLoadAddress: 220 case Value::eValueTypeFileAddress: 221 case Value::eValueTypeHostAddress: 222 { 223 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2; 224 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS)); 225 m_location_str.swap(sstr.GetString()); 226 } 227 break; 228 } 229 } 230 } 231 return m_location_str.c_str(); 232 } 233 234 Value & 235 ValueObject::GetValue() 236 { 237 return m_value; 238 } 239 240 const Value & 241 ValueObject::GetValue() const 242 { 243 return m_value; 244 } 245 246 bool 247 ValueObject::ResolveValue (Scalar &scalar) 248 { 249 ExecutionContext exe_ctx; 250 ExecutionContextScope *exe_scope = GetExecutionContextScope(); 251 if (exe_scope) 252 exe_scope->CalculateExecutionContext(exe_ctx); 253 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ()); 254 return scalar.IsValid(); 255 } 256 257 bool 258 ValueObject::GetValueIsValid () const 259 { 260 return m_value_is_valid; 261 } 262 263 264 void 265 ValueObject::SetValueIsValid (bool b) 266 { 267 m_value_is_valid = b; 268 } 269 270 bool 271 ValueObject::GetValueDidChange () 272 { 273 GetValueAsCString (); 274 return m_value_did_change; 275 } 276 277 void 278 ValueObject::SetValueDidChange (bool value_changed) 279 { 280 m_value_did_change = value_changed; 281 } 282 283 ValueObjectSP 284 ValueObject::GetChildAtIndex (uint32_t idx, bool can_create) 285 { 286 ValueObjectSP child_sp; 287 if (UpdateValueIfNeeded()) 288 { 289 if (idx < GetNumChildren()) 290 { 291 // Check if we have already made the child value object? 292 if (can_create && m_children[idx] == NULL) 293 { 294 // No we haven't created the child at this index, so lets have our 295 // subclass do it and cache the result for quick future access. 296 m_children[idx] = CreateChildAtIndex (idx, false, 0); 297 } 298 299 if (m_children[idx] != NULL) 300 return m_children[idx]->GetSP(); 301 } 302 } 303 return child_sp; 304 } 305 306 uint32_t 307 ValueObject::GetIndexOfChildWithName (const ConstString &name) 308 { 309 bool omit_empty_base_classes = true; 310 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(), 311 GetClangType(), 312 name.GetCString(), 313 omit_empty_base_classes); 314 } 315 316 ValueObjectSP 317 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create) 318 { 319 // when getting a child by name, it could be buried inside some base 320 // classes (which really aren't part of the expression path), so we 321 // need a vector of indexes that can get us down to the correct child 322 ValueObjectSP child_sp; 323 324 if (UpdateValueIfNeeded()) 325 { 326 std::vector<uint32_t> child_indexes; 327 clang::ASTContext *clang_ast = GetClangAST(); 328 void *clang_type = GetClangType(); 329 bool omit_empty_base_classes = true; 330 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast, 331 clang_type, 332 name.GetCString(), 333 omit_empty_base_classes, 334 child_indexes); 335 if (num_child_indexes > 0) 336 { 337 std::vector<uint32_t>::const_iterator pos = child_indexes.begin (); 338 std::vector<uint32_t>::const_iterator end = child_indexes.end (); 339 340 child_sp = GetChildAtIndex(*pos, can_create); 341 for (++pos; pos != end; ++pos) 342 { 343 if (child_sp) 344 { 345 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create)); 346 child_sp = new_child_sp; 347 } 348 else 349 { 350 child_sp.reset(); 351 } 352 353 } 354 } 355 } 356 return child_sp; 357 } 358 359 360 uint32_t 361 ValueObject::GetNumChildren () 362 { 363 if (!m_children_count_valid) 364 { 365 SetNumChildren (CalculateNumChildren()); 366 } 367 return m_children.size(); 368 } 369 void 370 ValueObject::SetNumChildren (uint32_t num_children) 371 { 372 m_children_count_valid = true; 373 m_children.resize(num_children); 374 } 375 376 void 377 ValueObject::SetName (const char *name) 378 { 379 m_name.SetCString(name); 380 } 381 382 void 383 ValueObject::SetName (const ConstString &name) 384 { 385 m_name = name; 386 } 387 388 ValueObject * 389 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index) 390 { 391 ValueObject *valobj; 392 393 if (UpdateValueIfNeeded()) 394 { 395 bool omit_empty_base_classes = true; 396 397 std::string child_name_str; 398 uint32_t child_byte_size = 0; 399 int32_t child_byte_offset = 0; 400 uint32_t child_bitfield_bit_size = 0; 401 uint32_t child_bitfield_bit_offset = 0; 402 bool child_is_base_class = false; 403 bool child_is_deref_of_parent = false; 404 405 const bool transparent_pointers = synthetic_array_member == false; 406 clang::ASTContext *clang_ast = GetClangAST(); 407 clang_type_t clang_type = GetClangType(); 408 clang_type_t child_clang_type; 409 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast, 410 GetName().GetCString(), 411 clang_type, 412 idx, 413 transparent_pointers, 414 omit_empty_base_classes, 415 child_name_str, 416 child_byte_size, 417 child_byte_offset, 418 child_bitfield_bit_size, 419 child_bitfield_bit_offset, 420 child_is_base_class, 421 child_is_deref_of_parent); 422 if (child_clang_type && child_byte_size) 423 { 424 if (synthetic_index) 425 child_byte_offset += child_byte_size * synthetic_index; 426 427 ConstString child_name; 428 if (!child_name_str.empty()) 429 child_name.SetCString (child_name_str.c_str()); 430 431 valobj = new ValueObjectChild (*this, 432 clang_ast, 433 child_clang_type, 434 child_name, 435 child_byte_size, 436 child_byte_offset, 437 child_bitfield_bit_size, 438 child_bitfield_bit_offset, 439 child_is_base_class, 440 child_is_deref_of_parent); 441 if (m_pointers_point_to_load_addrs) 442 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs); 443 } 444 } 445 446 return valobj; 447 } 448 449 const char * 450 ValueObject::GetSummaryAsCString () 451 { 452 if (UpdateValueIfNeeded ()) 453 { 454 if (m_summary_str.empty()) 455 { 456 clang_type_t clang_type = GetClangType(); 457 458 // See if this is a pointer to a C string? 459 if (clang_type) 460 { 461 StreamString sstr; 462 clang_type_t elem_or_pointee_clang_type; 463 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, 464 GetClangAST(), 465 &elem_or_pointee_clang_type)); 466 467 ExecutionContextScope *exe_scope = GetExecutionContextScope(); 468 if (exe_scope) 469 { 470 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) && 471 ClangASTContext::IsCharType (elem_or_pointee_clang_type)) 472 { 473 Process *process = exe_scope->CalculateProcess(); 474 if (process != NULL) 475 { 476 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS; 477 AddressType cstr_address_type = eAddressTypeInvalid; 478 479 size_t cstr_len = 0; 480 if (type_flags.Test (ClangASTContext::eTypeIsArray)) 481 { 482 // We have an array 483 cstr_len = ClangASTContext::GetArraySize (clang_type); 484 cstr_address = GetAddressOf (cstr_address_type, true); 485 } 486 else 487 { 488 // We have a pointer 489 cstr_address = GetPointerValue (cstr_address_type, true); 490 } 491 if (cstr_address != LLDB_INVALID_ADDRESS) 492 { 493 DataExtractor data; 494 size_t bytes_read = 0; 495 std::vector<char> data_buffer; 496 Error error; 497 if (cstr_len > 0) 498 { 499 data_buffer.resize(cstr_len); 500 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder()); 501 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error); 502 if (bytes_read > 0) 503 { 504 sstr << '"'; 505 data.Dump (&sstr, 506 0, // Start offset in "data" 507 eFormatChar, // Print as characters 508 1, // Size of item (1 byte for a char!) 509 bytes_read, // How many bytes to print? 510 UINT32_MAX, // num per line 511 LLDB_INVALID_ADDRESS,// base address 512 0, // bitfield bit size 513 0); // bitfield bit offset 514 sstr << '"'; 515 } 516 } 517 else 518 { 519 const size_t k_max_buf_size = 256; 520 data_buffer.resize (k_max_buf_size + 1); 521 // NULL terminate in case we don't get the entire C string 522 data_buffer.back() = '\0'; 523 524 sstr << '"'; 525 526 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder()); 527 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0) 528 { 529 size_t len = strlen(&data_buffer.front()); 530 if (len == 0) 531 break; 532 if (len > bytes_read) 533 len = bytes_read; 534 535 data.Dump (&sstr, 536 0, // Start offset in "data" 537 eFormatChar, // Print as characters 538 1, // Size of item (1 byte for a char!) 539 len, // How many bytes to print? 540 UINT32_MAX, // num per line 541 LLDB_INVALID_ADDRESS,// base address 542 0, // bitfield bit size 543 0); // bitfield bit offset 544 545 if (len < k_max_buf_size) 546 break; 547 cstr_address += k_max_buf_size; 548 } 549 sstr << '"'; 550 } 551 } 552 } 553 554 if (sstr.GetSize() > 0) 555 m_summary_str.assign (sstr.GetData(), sstr.GetSize()); 556 } 557 else if (ClangASTContext::IsFunctionPointerType (clang_type)) 558 { 559 AddressType func_ptr_address_type = eAddressTypeInvalid; 560 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true); 561 562 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS) 563 { 564 switch (func_ptr_address_type) 565 { 566 case eAddressTypeInvalid: 567 case eAddressTypeFile: 568 break; 569 570 case eAddressTypeLoad: 571 { 572 Address so_addr; 573 Target *target = exe_scope->CalculateTarget(); 574 if (target && target->GetSectionLoadList().IsEmpty() == false) 575 { 576 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr)) 577 { 578 so_addr.Dump (&sstr, 579 exe_scope, 580 Address::DumpStyleResolvedDescription, 581 Address::DumpStyleSectionNameOffset); 582 } 583 } 584 } 585 break; 586 587 case eAddressTypeHost: 588 break; 589 } 590 } 591 if (sstr.GetSize() > 0) 592 { 593 m_summary_str.assign (1, '('); 594 m_summary_str.append (sstr.GetData(), sstr.GetSize()); 595 m_summary_str.append (1, ')'); 596 } 597 } 598 } 599 } 600 } 601 } 602 if (m_summary_str.empty()) 603 return NULL; 604 return m_summary_str.c_str(); 605 } 606 607 const char * 608 ValueObject::GetObjectDescription () 609 { 610 if (!m_object_desc_str.empty()) 611 return m_object_desc_str.c_str(); 612 613 if (!UpdateValueIfNeeded ()) 614 return NULL; 615 616 ExecutionContextScope *exe_scope = GetExecutionContextScope(); 617 if (exe_scope == NULL) 618 return NULL; 619 620 Process *process = exe_scope->CalculateProcess(); 621 if (process == NULL) 622 return NULL; 623 624 StreamString s; 625 626 lldb::LanguageType language = GetObjectRuntimeLanguage(); 627 LanguageRuntime *runtime = process->GetLanguageRuntime(language); 628 629 if (runtime == NULL) 630 { 631 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway... 632 clang_type_t opaque_qual_type = GetClangType(); 633 if (opaque_qual_type != NULL) 634 { 635 bool is_signed; 636 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed) 637 || ClangASTContext::IsPointerType (opaque_qual_type)) 638 { 639 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC); 640 } 641 } 642 } 643 644 if (runtime && runtime->GetObjectDescription(s, *this)) 645 { 646 m_object_desc_str.append (s.GetData()); 647 } 648 649 if (m_object_desc_str.empty()) 650 return NULL; 651 else 652 return m_object_desc_str.c_str(); 653 } 654 655 const char * 656 ValueObject::GetValueAsCString () 657 { 658 // If our byte size is zero this is an aggregate type that has children 659 if (ClangASTContext::IsAggregateType (GetClangType()) == false) 660 { 661 if (UpdateValueIfNeeded()) 662 { 663 if (m_value_str.empty()) 664 { 665 const Value::ContextType context_type = m_value.GetContextType(); 666 667 switch (context_type) 668 { 669 case Value::eContextTypeClangType: 670 case Value::eContextTypeLLDBType: 671 case Value::eContextTypeVariable: 672 { 673 clang_type_t clang_type = GetClangType (); 674 if (clang_type) 675 { 676 StreamString sstr; 677 Format format = GetFormat(); 678 if (format == eFormatDefault) 679 format = ClangASTType::GetFormat(clang_type); 680 681 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST 682 clang_type, // The clang type to display 683 &sstr, 684 format, // Format to display this type with 685 m_data, // Data to extract from 686 0, // Byte offset into "m_data" 687 GetByteSize(), // Byte size of item in "m_data" 688 GetBitfieldBitSize(), // Bitfield bit size 689 GetBitfieldBitOffset())) // Bitfield bit offset 690 m_value_str.swap(sstr.GetString()); 691 else 692 m_value_str.clear(); 693 } 694 } 695 break; 696 697 case Value::eContextTypeRegisterInfo: 698 { 699 const RegisterInfo *reg_info = m_value.GetRegisterInfo(); 700 if (reg_info) 701 { 702 StreamString reg_sstr; 703 m_data.Dump(®_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0); 704 m_value_str.swap(reg_sstr.GetString()); 705 } 706 } 707 break; 708 709 default: 710 break; 711 } 712 } 713 714 if (!m_value_did_change && m_old_value_valid) 715 { 716 // The value was gotten successfully, so we consider the 717 // value as changed if the value string differs 718 SetValueDidChange (m_old_value_str != m_value_str); 719 } 720 } 721 } 722 if (m_value_str.empty()) 723 return NULL; 724 return m_value_str.c_str(); 725 } 726 727 addr_t 728 ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address) 729 { 730 if (!UpdateValueIfNeeded()) 731 return LLDB_INVALID_ADDRESS; 732 733 switch (m_value.GetValueType()) 734 { 735 case Value::eValueTypeScalar: 736 if (scalar_is_load_address) 737 { 738 address_type = eAddressTypeLoad; 739 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 740 } 741 break; 742 743 case Value::eValueTypeLoadAddress: 744 case Value::eValueTypeFileAddress: 745 case Value::eValueTypeHostAddress: 746 { 747 address_type = m_value.GetValueAddressType (); 748 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 749 } 750 break; 751 } 752 address_type = eAddressTypeInvalid; 753 return LLDB_INVALID_ADDRESS; 754 } 755 756 addr_t 757 ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address) 758 { 759 lldb::addr_t address = LLDB_INVALID_ADDRESS; 760 address_type = eAddressTypeInvalid; 761 762 if (!UpdateValueIfNeeded()) 763 return address; 764 765 switch (m_value.GetValueType()) 766 { 767 case Value::eValueTypeScalar: 768 if (scalar_is_load_address) 769 { 770 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 771 address_type = eAddressTypeLoad; 772 } 773 break; 774 775 case Value::eValueTypeLoadAddress: 776 case Value::eValueTypeFileAddress: 777 case Value::eValueTypeHostAddress: 778 { 779 uint32_t data_offset = 0; 780 address = m_data.GetPointer(&data_offset); 781 address_type = m_value.GetValueAddressType(); 782 if (address_type == eAddressTypeInvalid) 783 address_type = eAddressTypeLoad; 784 } 785 break; 786 } 787 788 if (m_pointers_point_to_load_addrs) 789 address_type = eAddressTypeLoad; 790 791 return address; 792 } 793 794 bool 795 ValueObject::SetValueFromCString (const char *value_str) 796 { 797 // Make sure our value is up to date first so that our location and location 798 // type is valid. 799 if (!UpdateValueIfNeeded()) 800 return false; 801 802 uint32_t count = 0; 803 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count); 804 805 char *end = NULL; 806 const size_t byte_size = GetByteSize(); 807 switch (encoding) 808 { 809 case eEncodingInvalid: 810 return false; 811 812 case eEncodingUint: 813 if (byte_size > sizeof(unsigned long long)) 814 { 815 return false; 816 } 817 else 818 { 819 unsigned long long ull_val = strtoull(value_str, &end, 0); 820 if (end && *end != '\0') 821 return false; 822 m_value = ull_val; 823 // Limit the bytes in our m_data appropriately. 824 m_value.GetScalar().GetData (m_data, byte_size); 825 } 826 break; 827 828 case eEncodingSint: 829 if (byte_size > sizeof(long long)) 830 { 831 return false; 832 } 833 else 834 { 835 long long sll_val = strtoll(value_str, &end, 0); 836 if (end && *end != '\0') 837 return false; 838 m_value = sll_val; 839 // Limit the bytes in our m_data appropriately. 840 m_value.GetScalar().GetData (m_data, byte_size); 841 } 842 break; 843 844 case eEncodingIEEE754: 845 { 846 const off_t byte_offset = GetByteOffset(); 847 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size)); 848 if (dst != NULL) 849 { 850 // We are decoding a float into host byte order below, so make 851 // sure m_data knows what it contains. 852 m_data.SetByteOrder(lldb::endian::InlHostByteOrder()); 853 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue ( 854 GetClangAST(), 855 GetClangType(), 856 value_str, 857 dst, 858 byte_size); 859 860 if (converted_byte_size == byte_size) 861 { 862 } 863 } 864 } 865 break; 866 867 case eEncodingVector: 868 return false; 869 870 default: 871 return false; 872 } 873 874 // If we have made it here the value is in m_data and we should write it 875 // out to the target 876 return Write (); 877 } 878 879 bool 880 ValueObject::Write () 881 { 882 // Clear the update ID so the next time we try and read the value 883 // we try and read it again. 884 m_update_point.SetNeedsUpdate(); 885 886 // TODO: when Value has a method to write a value back, call it from here. 887 return false; 888 889 } 890 891 lldb::LanguageType 892 ValueObject::GetObjectRuntimeLanguage () 893 { 894 clang_type_t opaque_qual_type = GetClangType(); 895 if (opaque_qual_type == NULL) 896 return lldb::eLanguageTypeC; 897 898 // If the type is a reference, then resolve it to what it refers to first: 899 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType()); 900 if (qual_type->isAnyPointerType()) 901 { 902 if (qual_type->isObjCObjectPointerType()) 903 return lldb::eLanguageTypeObjC; 904 905 clang::QualType pointee_type (qual_type->getPointeeType()); 906 if (pointee_type->getCXXRecordDeclForPointerType() != NULL) 907 return lldb::eLanguageTypeC_plus_plus; 908 if (pointee_type->isObjCObjectOrInterfaceType()) 909 return lldb::eLanguageTypeObjC; 910 if (pointee_type->isObjCClassType()) 911 return lldb::eLanguageTypeObjC; 912 } 913 else 914 { 915 if (ClangASTContext::IsObjCClassType (opaque_qual_type)) 916 return lldb::eLanguageTypeObjC; 917 if (ClangASTContext::IsCXXClassType (opaque_qual_type)) 918 return lldb::eLanguageTypeC_plus_plus; 919 } 920 921 return lldb::eLanguageTypeC; 922 } 923 924 void 925 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj) 926 { 927 m_synthetic_children[key] = valobj; 928 } 929 930 ValueObjectSP 931 ValueObject::GetSyntheticChild (const ConstString &key) const 932 { 933 ValueObjectSP synthetic_child_sp; 934 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key); 935 if (pos != m_synthetic_children.end()) 936 synthetic_child_sp = pos->second->GetSP(); 937 return synthetic_child_sp; 938 } 939 940 bool 941 ValueObject::IsPointerType () 942 { 943 return ClangASTContext::IsPointerType (GetClangType()); 944 } 945 946 bool 947 ValueObject::IsIntegerType (bool &is_signed) 948 { 949 return ClangASTContext::IsIntegerType (GetClangType(), is_signed); 950 } 951 952 bool 953 ValueObject::IsPointerOrReferenceType () 954 { 955 return ClangASTContext::IsPointerOrReferenceType(GetClangType()); 956 } 957 958 ValueObjectSP 959 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create) 960 { 961 ValueObjectSP synthetic_child_sp; 962 if (IsPointerType ()) 963 { 964 char index_str[64]; 965 snprintf(index_str, sizeof(index_str), "[%i]", index); 966 ConstString index_const_str(index_str); 967 // Check if we have already created a synthetic array member in this 968 // valid object. If we have we will re-use it. 969 synthetic_child_sp = GetSyntheticChild (index_const_str); 970 if (!synthetic_child_sp) 971 { 972 ValueObject *synthetic_child; 973 // We haven't made a synthetic array member for INDEX yet, so 974 // lets make one and cache it for any future reference. 975 synthetic_child = CreateChildAtIndex(0, true, index); 976 977 // Cache the value if we got one back... 978 if (synthetic_child) 979 { 980 AddSyntheticChild(index_const_str, synthetic_child); 981 synthetic_child_sp = synthetic_child->GetSP(); 982 } 983 } 984 } 985 return synthetic_child_sp; 986 } 987 988 void 989 ValueObject::CalculateDynamicValue () 990 { 991 if (!m_dynamic_value && !IsDynamic()) 992 { 993 Process *process = m_update_point.GetProcess(); 994 bool worth_having_dynamic_value = false; 995 996 997 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to 998 // hard code this everywhere. 999 lldb::LanguageType known_type = GetObjectRuntimeLanguage(); 1000 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC) 1001 { 1002 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type); 1003 if (runtime) 1004 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this); 1005 } 1006 else 1007 { 1008 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus); 1009 if (cpp_runtime) 1010 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this); 1011 1012 if (!worth_having_dynamic_value) 1013 { 1014 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC); 1015 if (objc_runtime) 1016 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this); 1017 } 1018 } 1019 1020 if (worth_having_dynamic_value) 1021 m_dynamic_value = new ValueObjectDynamicValue (*this); 1022 1023 // if (worth_having_dynamic_value) 1024 // printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager); 1025 1026 } 1027 } 1028 1029 ValueObjectSP 1030 ValueObject::GetDynamicValue (bool can_create) 1031 { 1032 if (!IsDynamic() && m_dynamic_value == NULL && can_create) 1033 { 1034 CalculateDynamicValue(); 1035 } 1036 if (m_dynamic_value) 1037 return m_dynamic_value->GetSP(); 1038 else 1039 return ValueObjectSP(); 1040 } 1041 1042 bool 1043 ValueObject::GetBaseClassPath (Stream &s) 1044 { 1045 if (IsBaseClass()) 1046 { 1047 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s); 1048 clang_type_t clang_type = GetClangType(); 1049 std::string cxx_class_name; 1050 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name); 1051 if (this_had_base_class) 1052 { 1053 if (parent_had_base_class) 1054 s.PutCString("::"); 1055 s.PutCString(cxx_class_name.c_str()); 1056 } 1057 return parent_had_base_class || this_had_base_class; 1058 } 1059 return false; 1060 } 1061 1062 1063 ValueObject * 1064 ValueObject::GetNonBaseClassParent() 1065 { 1066 if (GetParent()) 1067 { 1068 if (GetParent()->IsBaseClass()) 1069 return GetParent()->GetNonBaseClassParent(); 1070 else 1071 return GetParent(); 1072 } 1073 return NULL; 1074 } 1075 1076 void 1077 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes) 1078 { 1079 const bool is_deref_of_parent = IsDereferenceOfParent (); 1080 1081 if (is_deref_of_parent) 1082 s.PutCString("*("); 1083 1084 if (GetParent()) 1085 GetParent()->GetExpressionPath (s, qualify_cxx_base_classes); 1086 1087 if (!IsBaseClass()) 1088 { 1089 if (!is_deref_of_parent) 1090 { 1091 ValueObject *non_base_class_parent = GetNonBaseClassParent(); 1092 if (non_base_class_parent) 1093 { 1094 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType(); 1095 if (non_base_class_parent_clang_type) 1096 { 1097 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL); 1098 1099 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer) 1100 { 1101 s.PutCString("->"); 1102 } 1103 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) && 1104 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray)) 1105 { 1106 s.PutChar('.'); 1107 } 1108 } 1109 } 1110 1111 const char *name = GetName().GetCString(); 1112 if (name) 1113 { 1114 if (qualify_cxx_base_classes) 1115 { 1116 if (GetBaseClassPath (s)) 1117 s.PutCString("::"); 1118 } 1119 s.PutCString(name); 1120 } 1121 } 1122 } 1123 1124 if (is_deref_of_parent) 1125 s.PutChar(')'); 1126 } 1127 1128 void 1129 ValueObject::DumpValueObject 1130 ( 1131 Stream &s, 1132 ValueObject *valobj, 1133 const char *root_valobj_name, 1134 uint32_t ptr_depth, 1135 uint32_t curr_depth, 1136 uint32_t max_depth, 1137 bool show_types, 1138 bool show_location, 1139 bool use_objc, 1140 bool use_dynamic, 1141 bool scope_already_checked, 1142 bool flat_output 1143 ) 1144 { 1145 if (valobj && valobj->UpdateValueIfNeeded ()) 1146 { 1147 if (use_dynamic) 1148 { 1149 ValueObject *dynamic_value = valobj->GetDynamicValue(true).get(); 1150 if (dynamic_value) 1151 valobj = dynamic_value; 1152 } 1153 1154 clang_type_t clang_type = valobj->GetClangType(); 1155 1156 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL)); 1157 const char *err_cstr = NULL; 1158 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren); 1159 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue); 1160 1161 const bool print_valobj = flat_output == false || has_value; 1162 1163 if (print_valobj) 1164 { 1165 if (show_location) 1166 { 1167 s.Printf("%s: ", valobj->GetLocationAsCString()); 1168 } 1169 1170 s.Indent(); 1171 1172 // Always show the type for the top level items. 1173 if (show_types || (curr_depth == 0 && !flat_output)) 1174 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>")); 1175 1176 1177 if (flat_output) 1178 { 1179 // If we are showing types, also qualify the C++ base classes 1180 const bool qualify_cxx_base_classes = show_types; 1181 valobj->GetExpressionPath(s, qualify_cxx_base_classes); 1182 s.PutCString(" ="); 1183 } 1184 else 1185 { 1186 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString(""); 1187 s.Printf ("%s =", name_cstr); 1188 } 1189 1190 if (!scope_already_checked && !valobj->IsInScope()) 1191 { 1192 err_cstr = "error: out of scope"; 1193 } 1194 } 1195 1196 const char *val_cstr = NULL; 1197 1198 if (err_cstr == NULL) 1199 { 1200 val_cstr = valobj->GetValueAsCString(); 1201 err_cstr = valobj->GetError().AsCString(); 1202 } 1203 1204 if (err_cstr) 1205 { 1206 s.Printf (" error: %s\n", err_cstr); 1207 } 1208 else 1209 { 1210 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference); 1211 if (print_valobj) 1212 { 1213 const char *sum_cstr = valobj->GetSummaryAsCString(); 1214 1215 if (val_cstr) 1216 s.Printf(" %s", val_cstr); 1217 1218 if (sum_cstr) 1219 s.Printf(" %s", sum_cstr); 1220 1221 if (use_objc) 1222 { 1223 const char *object_desc = valobj->GetObjectDescription(); 1224 if (object_desc) 1225 s.Printf(" %s\n", object_desc); 1226 else 1227 s.Printf (" [no Objective-C description available]\n"); 1228 return; 1229 } 1230 } 1231 1232 if (curr_depth < max_depth) 1233 { 1234 // We will show children for all concrete types. We won't show 1235 // pointer contents unless a pointer depth has been specified. 1236 // We won't reference contents unless the reference is the 1237 // root object (depth of zero). 1238 bool print_children = true; 1239 1240 // Use a new temporary pointer depth in case we override the 1241 // current pointer depth below... 1242 uint32_t curr_ptr_depth = ptr_depth; 1243 1244 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer); 1245 if (is_ptr || is_ref) 1246 { 1247 // We have a pointer or reference whose value is an address. 1248 // Make sure that address is not NULL 1249 AddressType ptr_address_type; 1250 if (valobj->GetPointerValue (ptr_address_type, true) == 0) 1251 print_children = false; 1252 1253 else if (is_ref && curr_depth == 0) 1254 { 1255 // If this is the root object (depth is zero) that we are showing 1256 // and it is a reference, and no pointer depth has been supplied 1257 // print out what it references. Don't do this at deeper depths 1258 // otherwise we can end up with infinite recursion... 1259 curr_ptr_depth = 1; 1260 } 1261 1262 if (curr_ptr_depth == 0) 1263 print_children = false; 1264 } 1265 1266 if (print_children) 1267 { 1268 const uint32_t num_children = valobj->GetNumChildren(); 1269 if (num_children) 1270 { 1271 if (flat_output) 1272 { 1273 if (print_valobj) 1274 s.EOL(); 1275 } 1276 else 1277 { 1278 if (print_valobj) 1279 s.PutCString(is_ref ? ": {\n" : " {\n"); 1280 s.IndentMore(); 1281 } 1282 1283 for (uint32_t idx=0; idx<num_children; ++idx) 1284 { 1285 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true)); 1286 if (child_sp.get()) 1287 { 1288 DumpValueObject (s, 1289 child_sp.get(), 1290 NULL, 1291 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth, 1292 curr_depth + 1, 1293 max_depth, 1294 show_types, 1295 show_location, 1296 false, 1297 use_dynamic, 1298 true, 1299 flat_output); 1300 } 1301 } 1302 1303 if (!flat_output) 1304 { 1305 s.IndentLess(); 1306 s.Indent("}\n"); 1307 } 1308 } 1309 else if (has_children) 1310 { 1311 // Aggregate, no children... 1312 if (print_valobj) 1313 s.PutCString(" {}\n"); 1314 } 1315 else 1316 { 1317 if (print_valobj) 1318 s.EOL(); 1319 } 1320 1321 } 1322 else 1323 { 1324 s.EOL(); 1325 } 1326 } 1327 else 1328 { 1329 if (has_children && print_valobj) 1330 { 1331 s.PutCString("{...}\n"); 1332 } 1333 } 1334 } 1335 } 1336 } 1337 1338 1339 ValueObjectSP 1340 ValueObject::CreateConstantValue (const ConstString &name) 1341 { 1342 ValueObjectSP valobj_sp; 1343 1344 if (UpdateValueIfNeeded() && m_error.Success()) 1345 { 1346 ExecutionContextScope *exe_scope = GetExecutionContextScope(); 1347 if (exe_scope) 1348 { 1349 ExecutionContext exe_ctx; 1350 exe_scope->CalculateExecutionContext(exe_ctx); 1351 1352 clang::ASTContext *ast = GetClangAST (); 1353 1354 DataExtractor data; 1355 data.SetByteOrder (m_data.GetByteOrder()); 1356 data.SetAddressByteSize(m_data.GetAddressByteSize()); 1357 1358 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0); 1359 1360 valobj_sp = ValueObjectConstResult::Create (exe_scope, 1361 ast, 1362 GetClangType(), 1363 name, 1364 data); 1365 } 1366 } 1367 1368 if (!valobj_sp) 1369 { 1370 valobj_sp = ValueObjectConstResult::Create (NULL, m_error); 1371 } 1372 return valobj_sp; 1373 } 1374 1375 lldb::ValueObjectSP 1376 ValueObject::Dereference (Error &error) 1377 { 1378 if (m_deref_valobj) 1379 return m_deref_valobj->GetSP(); 1380 1381 const bool is_pointer_type = IsPointerType(); 1382 if (is_pointer_type) 1383 { 1384 bool omit_empty_base_classes = true; 1385 1386 std::string child_name_str; 1387 uint32_t child_byte_size = 0; 1388 int32_t child_byte_offset = 0; 1389 uint32_t child_bitfield_bit_size = 0; 1390 uint32_t child_bitfield_bit_offset = 0; 1391 bool child_is_base_class = false; 1392 bool child_is_deref_of_parent = false; 1393 const bool transparent_pointers = false; 1394 clang::ASTContext *clang_ast = GetClangAST(); 1395 clang_type_t clang_type = GetClangType(); 1396 clang_type_t child_clang_type; 1397 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast, 1398 GetName().GetCString(), 1399 clang_type, 1400 0, 1401 transparent_pointers, 1402 omit_empty_base_classes, 1403 child_name_str, 1404 child_byte_size, 1405 child_byte_offset, 1406 child_bitfield_bit_size, 1407 child_bitfield_bit_offset, 1408 child_is_base_class, 1409 child_is_deref_of_parent); 1410 if (child_clang_type && child_byte_size) 1411 { 1412 ConstString child_name; 1413 if (!child_name_str.empty()) 1414 child_name.SetCString (child_name_str.c_str()); 1415 1416 m_deref_valobj = new ValueObjectChild (*this, 1417 clang_ast, 1418 child_clang_type, 1419 child_name, 1420 child_byte_size, 1421 child_byte_offset, 1422 child_bitfield_bit_size, 1423 child_bitfield_bit_offset, 1424 child_is_base_class, 1425 child_is_deref_of_parent); 1426 } 1427 } 1428 1429 if (m_deref_valobj) 1430 { 1431 error.Clear(); 1432 return m_deref_valobj->GetSP(); 1433 } 1434 else 1435 { 1436 StreamString strm; 1437 GetExpressionPath(strm, true); 1438 1439 if (is_pointer_type) 1440 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str()); 1441 else 1442 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str()); 1443 return ValueObjectSP(); 1444 } 1445 } 1446 1447 lldb::ValueObjectSP 1448 ValueObject::AddressOf (Error &error) 1449 { 1450 if (m_addr_of_valobj_sp) 1451 return m_addr_of_valobj_sp; 1452 1453 AddressType address_type = eAddressTypeInvalid; 1454 const bool scalar_is_load_address = false; 1455 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address); 1456 error.Clear(); 1457 if (addr != LLDB_INVALID_ADDRESS) 1458 { 1459 switch (address_type) 1460 { 1461 default: 1462 case eAddressTypeInvalid: 1463 { 1464 StreamString expr_path_strm; 1465 GetExpressionPath(expr_path_strm, true); 1466 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str()); 1467 } 1468 break; 1469 1470 case eAddressTypeFile: 1471 case eAddressTypeLoad: 1472 case eAddressTypeHost: 1473 { 1474 clang::ASTContext *ast = GetClangAST(); 1475 clang_type_t clang_type = GetClangType(); 1476 if (ast && clang_type) 1477 { 1478 std::string name (1, '&'); 1479 name.append (m_name.AsCString("")); 1480 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(), 1481 ast, 1482 ClangASTContext::CreatePointerType (ast, clang_type), 1483 ConstString (name.c_str()), 1484 addr, 1485 eAddressTypeInvalid, 1486 m_data.GetAddressByteSize()); 1487 } 1488 } 1489 break; 1490 } 1491 } 1492 return m_addr_of_valobj_sp; 1493 } 1494 1495 ValueObject::EvaluationPoint::EvaluationPoint () : 1496 m_thread_id (LLDB_INVALID_UID), 1497 m_stop_id (0) 1498 { 1499 } 1500 1501 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected): 1502 m_needs_update (true), 1503 m_first_update (true), 1504 m_thread_id (LLDB_INVALID_UID), 1505 m_stop_id (0) 1506 1507 { 1508 ExecutionContext exe_ctx; 1509 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope, 1510 // and if so we want to cache that not the original. 1511 if (exe_scope) 1512 exe_scope->CalculateExecutionContext(exe_ctx); 1513 if (exe_ctx.target != NULL) 1514 { 1515 m_target_sp = exe_ctx.target->GetSP(); 1516 1517 if (exe_ctx.process == NULL) 1518 m_process_sp = exe_ctx.target->GetProcessSP(); 1519 else 1520 m_process_sp = exe_ctx.process->GetSP(); 1521 1522 if (m_process_sp != NULL) 1523 { 1524 m_stop_id = m_process_sp->GetStopID(); 1525 Thread *thread = NULL; 1526 1527 if (exe_ctx.thread == NULL) 1528 { 1529 if (use_selected) 1530 { 1531 thread = m_process_sp->GetThreadList().GetSelectedThread().get(); 1532 if (thread) 1533 computed_exe_scope = thread; 1534 } 1535 } 1536 else 1537 thread = exe_ctx.thread; 1538 1539 if (thread != NULL) 1540 { 1541 m_thread_id = thread->GetIndexID(); 1542 if (exe_ctx.frame == NULL) 1543 { 1544 if (use_selected) 1545 { 1546 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get(); 1547 if (frame) 1548 { 1549 m_stack_id = frame->GetStackID(); 1550 computed_exe_scope = frame; 1551 } 1552 } 1553 } 1554 else 1555 m_stack_id = exe_ctx.frame->GetStackID(); 1556 } 1557 } 1558 } 1559 m_exe_scope = computed_exe_scope; 1560 } 1561 1562 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) : 1563 m_exe_scope (rhs.m_exe_scope), 1564 m_needs_update(true), 1565 m_first_update(true), 1566 m_target_sp (rhs.m_target_sp), 1567 m_process_sp (rhs.m_process_sp), 1568 m_thread_id (rhs.m_thread_id), 1569 m_stack_id (rhs.m_stack_id), 1570 m_stop_id (0) 1571 { 1572 } 1573 1574 ValueObject::EvaluationPoint::~EvaluationPoint () 1575 { 1576 } 1577 1578 ExecutionContextScope * 1579 ValueObject::EvaluationPoint::GetExecutionContextScope () 1580 { 1581 // We have to update before giving out the scope, or we could be handing out stale pointers. 1582 SyncWithProcessState(); 1583 1584 return m_exe_scope; 1585 } 1586 1587 // This function checks the EvaluationPoint against the current process state. If the current 1588 // state matches the evaluation point, or the evaluation point is already invalid, then we return 1589 // false, meaning "no change". If the current state is different, we update our state, and return 1590 // true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so 1591 // future calls to NeedsUpdate will return true. 1592 1593 bool 1594 ValueObject::EvaluationPoint::SyncWithProcessState() 1595 { 1596 // If we're already invalid, we don't need to do anything, and nothing has changed: 1597 if (m_stop_id == LLDB_INVALID_UID) 1598 { 1599 // Can't update with an invalid state. 1600 m_needs_update = false; 1601 return false; 1602 } 1603 1604 // If we don't have a process nothing can change. 1605 if (!m_process_sp) 1606 return false; 1607 1608 // If our stop id is the current stop ID, nothing has changed: 1609 uint32_t cur_stop_id = m_process_sp->GetStopID(); 1610 if (m_stop_id == cur_stop_id) 1611 return false; 1612 1613 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared. 1614 // In either case, we aren't going to be able to sync with the process state. 1615 if (cur_stop_id == 0) 1616 return false; 1617 1618 m_stop_id = cur_stop_id; 1619 m_needs_update = true; 1620 m_exe_scope = m_process_sp.get(); 1621 1622 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either 1623 // doesn't, mark ourselves as invalid. 1624 1625 if (m_thread_id != LLDB_INVALID_THREAD_ID) 1626 { 1627 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get(); 1628 if (our_thread == NULL) 1629 SetInvalid(); 1630 else 1631 { 1632 m_exe_scope = our_thread; 1633 1634 if (m_stack_id.IsValid()) 1635 { 1636 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get(); 1637 if (our_frame == NULL) 1638 SetInvalid(); 1639 else 1640 m_exe_scope = our_frame; 1641 } 1642 } 1643 } 1644 return true; 1645 } 1646 1647 bool 1648 ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope) 1649 { 1650 if (!IsValid()) 1651 return false; 1652 1653 bool needs_update = false; 1654 m_exe_scope = NULL; 1655 1656 // The target has to be non-null, and the 1657 Target *target = exe_scope->CalculateTarget(); 1658 if (target != NULL) 1659 { 1660 Target *old_target = m_target_sp.get(); 1661 assert (target == old_target); 1662 Process *process = exe_scope->CalculateProcess(); 1663 if (process != NULL) 1664 { 1665 // FOR NOW - assume you can't update variable objects across process boundaries. 1666 Process *old_process = m_process_sp.get(); 1667 assert (process == old_process); 1668 1669 lldb::user_id_t stop_id = process->GetStopID(); 1670 if (stop_id != m_stop_id) 1671 { 1672 needs_update = true; 1673 m_stop_id = stop_id; 1674 } 1675 // See if we're switching the thread or stack context. If no thread is given, this is 1676 // being evaluated in a global context. 1677 Thread *thread = exe_scope->CalculateThread(); 1678 if (thread != NULL) 1679 { 1680 lldb::user_id_t new_thread_index = thread->GetIndexID(); 1681 if (new_thread_index != m_thread_id) 1682 { 1683 needs_update = true; 1684 m_thread_id = new_thread_index; 1685 m_stack_id.Clear(); 1686 } 1687 1688 StackFrame *new_frame = exe_scope->CalculateStackFrame(); 1689 if (new_frame != NULL) 1690 { 1691 if (new_frame->GetStackID() != m_stack_id) 1692 { 1693 needs_update = true; 1694 m_stack_id = new_frame->GetStackID(); 1695 } 1696 } 1697 else 1698 { 1699 m_stack_id.Clear(); 1700 needs_update = true; 1701 } 1702 } 1703 else 1704 { 1705 // If this had been given a thread, and now there is none, we should update. 1706 // Otherwise we don't have to do anything. 1707 if (m_thread_id != LLDB_INVALID_UID) 1708 { 1709 m_thread_id = LLDB_INVALID_UID; 1710 m_stack_id.Clear(); 1711 needs_update = true; 1712 } 1713 } 1714 } 1715 else 1716 { 1717 // If there is no process, then we don't need to update anything. 1718 // But if we're switching from having a process to not, we should try to update. 1719 if (m_process_sp.get() != NULL) 1720 { 1721 needs_update = true; 1722 m_process_sp.reset(); 1723 m_thread_id = LLDB_INVALID_UID; 1724 m_stack_id.Clear(); 1725 } 1726 } 1727 } 1728 else 1729 { 1730 // If there's no target, nothing can change so we don't need to update anything. 1731 // But if we're switching from having a target to not, we should try to update. 1732 if (m_target_sp.get() != NULL) 1733 { 1734 needs_update = true; 1735 m_target_sp.reset(); 1736 m_process_sp.reset(); 1737 m_thread_id = LLDB_INVALID_UID; 1738 m_stack_id.Clear(); 1739 } 1740 } 1741 if (!m_needs_update) 1742 m_needs_update = needs_update; 1743 1744 return needs_update; 1745 } 1746