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/lldb-python.h" 11 12 #include "lldb/Core/ValueObject.h" 13 14 // C Includes 15 #include <stdlib.h> 16 17 // C++ Includes 18 // Other libraries and framework includes 19 #include "llvm/Support/raw_ostream.h" 20 #include "clang/AST/Type.h" 21 22 // Project includes 23 #include "lldb/Core/DataBufferHeap.h" 24 #include "lldb/Core/Debugger.h" 25 #include "lldb/Core/Log.h" 26 #include "lldb/Core/Module.h" 27 #include "lldb/Core/StreamString.h" 28 #include "lldb/Core/ValueObjectCast.h" 29 #include "lldb/Core/ValueObjectChild.h" 30 #include "lldb/Core/ValueObjectConstResult.h" 31 #include "lldb/Core/ValueObjectDynamicValue.h" 32 #include "lldb/Core/ValueObjectList.h" 33 #include "lldb/Core/ValueObjectMemory.h" 34 #include "lldb/Core/ValueObjectSyntheticFilter.h" 35 36 #include "lldb/DataFormatters/DataVisualization.h" 37 #include "lldb/DataFormatters/ValueObjectPrinter.h" 38 39 #include "lldb/Host/Endian.h" 40 41 #include "lldb/Interpreter/CommandInterpreter.h" 42 #include "lldb/Interpreter/ScriptInterpreterPython.h" 43 44 #include "lldb/Symbol/ClangASTType.h" 45 #include "lldb/Symbol/ClangASTContext.h" 46 #include "lldb/Symbol/Type.h" 47 48 #include "lldb/Target/ExecutionContext.h" 49 #include "lldb/Target/LanguageRuntime.h" 50 #include "lldb/Target/ObjCLanguageRuntime.h" 51 #include "lldb/Target/Process.h" 52 #include "lldb/Target/RegisterContext.h" 53 #include "lldb/Target/SectionLoadList.h" 54 #include "lldb/Target/Target.h" 55 #include "lldb/Target/Thread.h" 56 57 using namespace lldb; 58 using namespace lldb_private; 59 using namespace lldb_utility; 60 61 static user_id_t g_value_obj_uid = 0; 62 63 //---------------------------------------------------------------------- 64 // ValueObject constructor 65 //---------------------------------------------------------------------- 66 ValueObject::ValueObject (ValueObject &parent) : 67 UserID (++g_value_obj_uid), // Unique identifier for every value object 68 m_parent (&parent), 69 m_root (NULL), 70 m_update_point (parent.GetUpdatePoint ()), 71 m_name (), 72 m_data (), 73 m_value (), 74 m_error (), 75 m_value_str (), 76 m_old_value_str (), 77 m_location_str (), 78 m_summary_str (), 79 m_object_desc_str (), 80 m_manager(parent.GetManager()), 81 m_children (), 82 m_synthetic_children (), 83 m_dynamic_value (NULL), 84 m_synthetic_value(NULL), 85 m_deref_valobj(NULL), 86 m_format (eFormatDefault), 87 m_last_format (eFormatDefault), 88 m_last_format_mgr_revision(0), 89 m_type_summary_sp(), 90 m_type_format_sp(), 91 m_synthetic_children_sp(), 92 m_user_id_of_forced_summary(), 93 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid), 94 m_value_is_valid (false), 95 m_value_did_change (false), 96 m_children_count_valid (false), 97 m_old_value_valid (false), 98 m_is_deref_of_parent (false), 99 m_is_array_item_for_pointer(false), 100 m_is_bitfield_for_scalar(false), 101 m_is_child_at_offset(false), 102 m_is_getting_summary(false), 103 m_did_calculate_complete_objc_class_type(false) 104 { 105 m_manager->ManageObject(this); 106 } 107 108 //---------------------------------------------------------------------- 109 // ValueObject constructor 110 //---------------------------------------------------------------------- 111 ValueObject::ValueObject (ExecutionContextScope *exe_scope, 112 AddressType child_ptr_or_ref_addr_type) : 113 UserID (++g_value_obj_uid), // Unique identifier for every value object 114 m_parent (NULL), 115 m_root (NULL), 116 m_update_point (exe_scope), 117 m_name (), 118 m_data (), 119 m_value (), 120 m_error (), 121 m_value_str (), 122 m_old_value_str (), 123 m_location_str (), 124 m_summary_str (), 125 m_object_desc_str (), 126 m_manager(), 127 m_children (), 128 m_synthetic_children (), 129 m_dynamic_value (NULL), 130 m_synthetic_value(NULL), 131 m_deref_valobj(NULL), 132 m_format (eFormatDefault), 133 m_last_format (eFormatDefault), 134 m_last_format_mgr_revision(0), 135 m_type_summary_sp(), 136 m_type_format_sp(), 137 m_synthetic_children_sp(), 138 m_user_id_of_forced_summary(), 139 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type), 140 m_value_is_valid (false), 141 m_value_did_change (false), 142 m_children_count_valid (false), 143 m_old_value_valid (false), 144 m_is_deref_of_parent (false), 145 m_is_array_item_for_pointer(false), 146 m_is_bitfield_for_scalar(false), 147 m_is_child_at_offset(false), 148 m_is_getting_summary(false), 149 m_did_calculate_complete_objc_class_type(false) 150 { 151 m_manager = new ValueObjectManager(); 152 m_manager->ManageObject (this); 153 } 154 155 //---------------------------------------------------------------------- 156 // Destructor 157 //---------------------------------------------------------------------- 158 ValueObject::~ValueObject () 159 { 160 } 161 162 bool 163 ValueObject::UpdateValueIfNeeded (bool update_format) 164 { 165 166 bool did_change_formats = false; 167 168 if (update_format) 169 did_change_formats = UpdateFormatsIfNeeded(); 170 171 // If this is a constant value, then our success is predicated on whether 172 // we have an error or not 173 if (GetIsConstant()) 174 { 175 // if you are constant, things might still have changed behind your back 176 // (e.g. you are a frozen object and things have changed deeper than you cared to freeze-dry yourself) 177 // in this case, your value has not changed, but "computed" entries might have, so you might now have 178 // a different summary, or a different object description. clear these so we will recompute them 179 if (update_format && !did_change_formats) 180 ClearUserVisibleData(eClearUserVisibleDataItemsSummary | eClearUserVisibleDataItemsDescription); 181 return m_error.Success(); 182 } 183 184 bool first_update = m_update_point.IsFirstEvaluation(); 185 186 if (m_update_point.NeedsUpdating()) 187 { 188 m_update_point.SetUpdated(); 189 190 // Save the old value using swap to avoid a string copy which 191 // also will clear our m_value_str 192 if (m_value_str.empty()) 193 { 194 m_old_value_valid = false; 195 } 196 else 197 { 198 m_old_value_valid = true; 199 m_old_value_str.swap (m_value_str); 200 ClearUserVisibleData(eClearUserVisibleDataItemsValue); 201 } 202 203 ClearUserVisibleData(); 204 205 if (IsInScope()) 206 { 207 const bool value_was_valid = GetValueIsValid(); 208 SetValueDidChange (false); 209 210 m_error.Clear(); 211 212 // Call the pure virtual function to update the value 213 bool success = UpdateValue (); 214 215 SetValueIsValid (success); 216 217 if (first_update) 218 SetValueDidChange (false); 219 else if (!m_value_did_change && success == false) 220 { 221 // The value wasn't gotten successfully, so we mark this 222 // as changed if the value used to be valid and now isn't 223 SetValueDidChange (value_was_valid); 224 } 225 } 226 else 227 { 228 m_error.SetErrorString("out of scope"); 229 } 230 } 231 return m_error.Success(); 232 } 233 234 bool 235 ValueObject::UpdateFormatsIfNeeded() 236 { 237 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES)); 238 if (log) 239 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d", 240 GetName().GetCString(), 241 this, 242 m_last_format_mgr_revision, 243 DataVisualization::GetCurrentRevision()); 244 245 bool any_change = false; 246 247 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision())) 248 { 249 SetValueFormat(DataVisualization::GetFormat (*this, eNoDynamicValues)); 250 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType())); 251 #ifndef LLDB_DISABLE_PYTHON 252 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType())); 253 #endif 254 255 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision(); 256 257 any_change = true; 258 } 259 260 return any_change; 261 262 } 263 264 void 265 ValueObject::SetNeedsUpdate () 266 { 267 m_update_point.SetNeedsUpdate(); 268 // We have to clear the value string here so ConstResult children will notice if their values are 269 // changed by hand (i.e. with SetValueAsCString). 270 ClearUserVisibleData(eClearUserVisibleDataItemsValue); 271 } 272 273 void 274 ValueObject::ClearDynamicTypeInformation () 275 { 276 m_children_count_valid = false; 277 m_did_calculate_complete_objc_class_type = false; 278 m_last_format_mgr_revision = 0; 279 m_override_type = ClangASTType(); 280 SetValueFormat(lldb::TypeFormatImplSP()); 281 SetSummaryFormat(lldb::TypeSummaryImplSP()); 282 SetSyntheticChildren(lldb::SyntheticChildrenSP()); 283 } 284 285 ClangASTType 286 ValueObject::MaybeCalculateCompleteType () 287 { 288 ClangASTType clang_type(GetClangTypeImpl()); 289 290 if (m_did_calculate_complete_objc_class_type) 291 { 292 if (m_override_type.IsValid()) 293 return m_override_type; 294 else 295 return clang_type; 296 } 297 298 ClangASTType class_type; 299 bool is_pointer_type = false; 300 301 if (clang_type.IsObjCObjectPointerType(&class_type)) 302 { 303 is_pointer_type = true; 304 } 305 else if (clang_type.IsObjCObjectOrInterfaceType()) 306 { 307 class_type = clang_type; 308 } 309 else 310 { 311 return clang_type; 312 } 313 314 m_did_calculate_complete_objc_class_type = true; 315 316 if (class_type) 317 { 318 ConstString class_name (class_type.GetConstTypeName()); 319 320 if (class_name) 321 { 322 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP()); 323 324 if (process_sp) 325 { 326 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime()); 327 328 if (objc_language_runtime) 329 { 330 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name); 331 332 if (complete_objc_class_type_sp) 333 { 334 ClangASTType complete_class(complete_objc_class_type_sp->GetClangFullType()); 335 336 if (complete_class.GetCompleteType()) 337 { 338 if (is_pointer_type) 339 { 340 m_override_type = complete_class.GetPointerType(); 341 } 342 else 343 { 344 m_override_type = complete_class; 345 } 346 347 if (m_override_type.IsValid()) 348 return m_override_type; 349 } 350 } 351 } 352 } 353 } 354 } 355 return clang_type; 356 } 357 358 ClangASTType 359 ValueObject::GetClangType () 360 { 361 return MaybeCalculateCompleteType(); 362 } 363 364 TypeImpl 365 ValueObject::GetTypeImpl () 366 { 367 return TypeImpl(GetClangType()); 368 } 369 370 DataExtractor & 371 ValueObject::GetDataExtractor () 372 { 373 UpdateValueIfNeeded(false); 374 return m_data; 375 } 376 377 const Error & 378 ValueObject::GetError() 379 { 380 UpdateValueIfNeeded(false); 381 return m_error; 382 } 383 384 const ConstString & 385 ValueObject::GetName() const 386 { 387 return m_name; 388 } 389 390 const char * 391 ValueObject::GetLocationAsCString () 392 { 393 return GetLocationAsCStringImpl(m_value, 394 m_data); 395 } 396 397 const char * 398 ValueObject::GetLocationAsCStringImpl (const Value& value, 399 const DataExtractor& data) 400 { 401 if (UpdateValueIfNeeded(false)) 402 { 403 if (m_location_str.empty()) 404 { 405 StreamString sstr; 406 407 Value::ValueType value_type = value.GetValueType(); 408 409 switch (value_type) 410 { 411 case Value::eValueTypeScalar: 412 case Value::eValueTypeVector: 413 if (value.GetContextType() == Value::eContextTypeRegisterInfo) 414 { 415 RegisterInfo *reg_info = value.GetRegisterInfo(); 416 if (reg_info) 417 { 418 if (reg_info->name) 419 m_location_str = reg_info->name; 420 else if (reg_info->alt_name) 421 m_location_str = reg_info->alt_name; 422 if (m_location_str.empty()) 423 m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar"; 424 } 425 } 426 if (m_location_str.empty()) 427 m_location_str = (value_type == Value::eValueTypeVector) ? "vector" : "scalar"; 428 break; 429 430 case Value::eValueTypeLoadAddress: 431 case Value::eValueTypeFileAddress: 432 case Value::eValueTypeHostAddress: 433 { 434 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2; 435 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS)); 436 m_location_str.swap(sstr.GetString()); 437 } 438 break; 439 } 440 } 441 } 442 return m_location_str.c_str(); 443 } 444 445 Value & 446 ValueObject::GetValue() 447 { 448 return m_value; 449 } 450 451 const Value & 452 ValueObject::GetValue() const 453 { 454 return m_value; 455 } 456 457 bool 458 ValueObject::ResolveValue (Scalar &scalar) 459 { 460 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything 461 { 462 ExecutionContext exe_ctx (GetExecutionContextRef()); 463 Value tmp_value(m_value); 464 scalar = tmp_value.ResolveValue(&exe_ctx); 465 if (scalar.IsValid()) 466 { 467 const uint32_t bitfield_bit_size = GetBitfieldBitSize(); 468 if (bitfield_bit_size) 469 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset()); 470 return true; 471 } 472 } 473 return false; 474 } 475 476 bool 477 ValueObject::GetValueIsValid () const 478 { 479 return m_value_is_valid; 480 } 481 482 483 void 484 ValueObject::SetValueIsValid (bool b) 485 { 486 m_value_is_valid = b; 487 } 488 489 bool 490 ValueObject::GetValueDidChange () 491 { 492 GetValueAsCString (); 493 return m_value_did_change; 494 } 495 496 void 497 ValueObject::SetValueDidChange (bool value_changed) 498 { 499 m_value_did_change = value_changed; 500 } 501 502 ValueObjectSP 503 ValueObject::GetChildAtIndex (size_t idx, bool can_create) 504 { 505 ValueObjectSP child_sp; 506 // We may need to update our value if we are dynamic 507 if (IsPossibleDynamicType ()) 508 UpdateValueIfNeeded(false); 509 if (idx < GetNumChildren()) 510 { 511 // Check if we have already made the child value object? 512 if (can_create && !m_children.HasChildAtIndex(idx)) 513 { 514 // No we haven't created the child at this index, so lets have our 515 // subclass do it and cache the result for quick future access. 516 m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0)); 517 } 518 519 ValueObject* child = m_children.GetChildAtIndex(idx); 520 if (child != NULL) 521 return child->GetSP(); 522 } 523 return child_sp; 524 } 525 526 ValueObjectSP 527 ValueObject::GetChildAtIndexPath (const std::initializer_list<size_t>& idxs, 528 size_t* index_of_error) 529 { 530 if (idxs.size() == 0) 531 return GetSP(); 532 ValueObjectSP root(GetSP()); 533 for (size_t idx : idxs) 534 { 535 root = root->GetChildAtIndex(idx, true); 536 if (!root) 537 { 538 if (index_of_error) 539 *index_of_error = idx; 540 return root; 541 } 542 } 543 return root; 544 } 545 546 ValueObjectSP 547 ValueObject::GetChildAtIndexPath (const std::initializer_list< std::pair<size_t, bool> >& idxs, 548 size_t* index_of_error) 549 { 550 if (idxs.size() == 0) 551 return GetSP(); 552 ValueObjectSP root(GetSP()); 553 for (std::pair<size_t, bool> idx : idxs) 554 { 555 root = root->GetChildAtIndex(idx.first, idx.second); 556 if (!root) 557 { 558 if (index_of_error) 559 *index_of_error = idx.first; 560 return root; 561 } 562 } 563 return root; 564 } 565 566 lldb::ValueObjectSP 567 ValueObject::GetChildAtIndexPath (const std::vector<size_t> &idxs, 568 size_t* index_of_error) 569 { 570 if (idxs.size() == 0) 571 return GetSP(); 572 ValueObjectSP root(GetSP()); 573 for (size_t idx : idxs) 574 { 575 root = root->GetChildAtIndex(idx, true); 576 if (!root) 577 { 578 if (index_of_error) 579 *index_of_error = idx; 580 return root; 581 } 582 } 583 return root; 584 } 585 586 lldb::ValueObjectSP 587 ValueObject::GetChildAtIndexPath (const std::vector< std::pair<size_t, bool> > &idxs, 588 size_t* index_of_error) 589 { 590 if (idxs.size() == 0) 591 return GetSP(); 592 ValueObjectSP root(GetSP()); 593 for (std::pair<size_t, bool> idx : idxs) 594 { 595 root = root->GetChildAtIndex(idx.first, idx.second); 596 if (!root) 597 { 598 if (index_of_error) 599 *index_of_error = idx.first; 600 return root; 601 } 602 } 603 return root; 604 } 605 606 lldb::ValueObjectSP 607 ValueObject::GetChildAtNamePath (const std::initializer_list<ConstString> &names, 608 ConstString* name_of_error) 609 { 610 if (names.size() == 0) 611 return GetSP(); 612 ValueObjectSP root(GetSP()); 613 for (ConstString name : names) 614 { 615 root = root->GetChildMemberWithName(name, true); 616 if (!root) 617 { 618 if (name_of_error) 619 *name_of_error = name; 620 return root; 621 } 622 } 623 return root; 624 } 625 626 lldb::ValueObjectSP 627 ValueObject::GetChildAtNamePath (const std::vector<ConstString> &names, 628 ConstString* name_of_error) 629 { 630 if (names.size() == 0) 631 return GetSP(); 632 ValueObjectSP root(GetSP()); 633 for (ConstString name : names) 634 { 635 root = root->GetChildMemberWithName(name, true); 636 if (!root) 637 { 638 if (name_of_error) 639 *name_of_error = name; 640 return root; 641 } 642 } 643 return root; 644 } 645 646 lldb::ValueObjectSP 647 ValueObject::GetChildAtNamePath (const std::initializer_list< std::pair<ConstString, bool> > &names, 648 ConstString* name_of_error) 649 { 650 if (names.size() == 0) 651 return GetSP(); 652 ValueObjectSP root(GetSP()); 653 for (std::pair<ConstString, bool> name : names) 654 { 655 root = root->GetChildMemberWithName(name.first, name.second); 656 if (!root) 657 { 658 if (name_of_error) 659 *name_of_error = name.first; 660 return root; 661 } 662 } 663 return root; 664 } 665 666 lldb::ValueObjectSP 667 ValueObject::GetChildAtNamePath (const std::vector< std::pair<ConstString, bool> > &names, 668 ConstString* name_of_error) 669 { 670 if (names.size() == 0) 671 return GetSP(); 672 ValueObjectSP root(GetSP()); 673 for (std::pair<ConstString, bool> name : names) 674 { 675 root = root->GetChildMemberWithName(name.first, name.second); 676 if (!root) 677 { 678 if (name_of_error) 679 *name_of_error = name.first; 680 return root; 681 } 682 } 683 return root; 684 } 685 686 size_t 687 ValueObject::GetIndexOfChildWithName (const ConstString &name) 688 { 689 bool omit_empty_base_classes = true; 690 return GetClangType().GetIndexOfChildWithName (name.GetCString(), omit_empty_base_classes); 691 } 692 693 ValueObjectSP 694 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create) 695 { 696 // when getting a child by name, it could be buried inside some base 697 // classes (which really aren't part of the expression path), so we 698 // need a vector of indexes that can get us down to the correct child 699 ValueObjectSP child_sp; 700 701 // We may need to update our value if we are dynamic 702 if (IsPossibleDynamicType ()) 703 UpdateValueIfNeeded(false); 704 705 std::vector<uint32_t> child_indexes; 706 bool omit_empty_base_classes = true; 707 const size_t num_child_indexes = GetClangType().GetIndexOfChildMemberWithName (name.GetCString(), 708 omit_empty_base_classes, 709 child_indexes); 710 if (num_child_indexes > 0) 711 { 712 std::vector<uint32_t>::const_iterator pos = child_indexes.begin (); 713 std::vector<uint32_t>::const_iterator end = child_indexes.end (); 714 715 child_sp = GetChildAtIndex(*pos, can_create); 716 for (++pos; pos != end; ++pos) 717 { 718 if (child_sp) 719 { 720 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create)); 721 child_sp = new_child_sp; 722 } 723 else 724 { 725 child_sp.reset(); 726 } 727 728 } 729 } 730 return child_sp; 731 } 732 733 734 size_t 735 ValueObject::GetNumChildren () 736 { 737 UpdateValueIfNeeded(); 738 if (!m_children_count_valid) 739 { 740 SetNumChildren (CalculateNumChildren()); 741 } 742 return m_children.GetChildrenCount(); 743 } 744 745 bool 746 ValueObject::MightHaveChildren() 747 { 748 bool has_children = false; 749 const uint32_t type_info = GetTypeInfo(); 750 if (type_info) 751 { 752 if (type_info & (ClangASTType::eTypeHasChildren | 753 ClangASTType::eTypeIsPointer | 754 ClangASTType::eTypeIsReference)) 755 has_children = true; 756 } 757 else 758 { 759 has_children = GetNumChildren () > 0; 760 } 761 return has_children; 762 } 763 764 // Should only be called by ValueObject::GetNumChildren() 765 void 766 ValueObject::SetNumChildren (size_t num_children) 767 { 768 m_children_count_valid = true; 769 m_children.SetChildrenCount(num_children); 770 } 771 772 void 773 ValueObject::SetName (const ConstString &name) 774 { 775 m_name = name; 776 } 777 778 ValueObject * 779 ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index) 780 { 781 ValueObject *valobj = NULL; 782 783 bool omit_empty_base_classes = true; 784 bool ignore_array_bounds = synthetic_array_member; 785 std::string child_name_str; 786 uint32_t child_byte_size = 0; 787 int32_t child_byte_offset = 0; 788 uint32_t child_bitfield_bit_size = 0; 789 uint32_t child_bitfield_bit_offset = 0; 790 bool child_is_base_class = false; 791 bool child_is_deref_of_parent = false; 792 793 const bool transparent_pointers = synthetic_array_member == false; 794 ClangASTType child_clang_type; 795 796 ExecutionContext exe_ctx (GetExecutionContextRef()); 797 798 child_clang_type = GetClangType().GetChildClangTypeAtIndex (&exe_ctx, 799 GetName().GetCString(), 800 idx, 801 transparent_pointers, 802 omit_empty_base_classes, 803 ignore_array_bounds, 804 child_name_str, 805 child_byte_size, 806 child_byte_offset, 807 child_bitfield_bit_size, 808 child_bitfield_bit_offset, 809 child_is_base_class, 810 child_is_deref_of_parent); 811 if (child_clang_type) 812 { 813 if (synthetic_index) 814 child_byte_offset += child_byte_size * synthetic_index; 815 816 ConstString child_name; 817 if (!child_name_str.empty()) 818 child_name.SetCString (child_name_str.c_str()); 819 820 valobj = new ValueObjectChild (*this, 821 child_clang_type, 822 child_name, 823 child_byte_size, 824 child_byte_offset, 825 child_bitfield_bit_size, 826 child_bitfield_bit_offset, 827 child_is_base_class, 828 child_is_deref_of_parent, 829 eAddressTypeInvalid); 830 //if (valobj) 831 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid); 832 } 833 834 return valobj; 835 } 836 837 bool 838 ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr, 839 std::string& destination) 840 { 841 destination.clear(); 842 843 // ideally we would like to bail out if passing NULL, but if we do so 844 // we end up not providing the summary for function pointers anymore 845 if (/*summary_ptr == NULL ||*/ m_is_getting_summary) 846 return false; 847 848 m_is_getting_summary = true; 849 850 // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other 851 // information that we might care to see in a crash log. might be useful in very specific situations though. 852 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s", 853 GetTypeName().GetCString(), 854 GetName().GetCString(), 855 summary_ptr->GetDescription().c_str());*/ 856 857 if (UpdateValueIfNeeded (false)) 858 { 859 if (summary_ptr) 860 { 861 if (HasSyntheticValue()) 862 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#}) 863 summary_ptr->FormatObject(this, destination); 864 } 865 else 866 { 867 ClangASTType clang_type = GetClangType(); 868 869 // Do some default printout for function pointers 870 if (clang_type) 871 { 872 if (clang_type.IsFunctionPointerType ()) 873 { 874 StreamString sstr; 875 AddressType func_ptr_address_type = eAddressTypeInvalid; 876 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type); 877 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS) 878 { 879 switch (func_ptr_address_type) 880 { 881 case eAddressTypeInvalid: 882 case eAddressTypeFile: 883 break; 884 885 case eAddressTypeLoad: 886 { 887 ExecutionContext exe_ctx (GetExecutionContextRef()); 888 889 Address so_addr; 890 Target *target = exe_ctx.GetTargetPtr(); 891 if (target && target->GetSectionLoadList().IsEmpty() == false) 892 { 893 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr)) 894 { 895 so_addr.Dump (&sstr, 896 exe_ctx.GetBestExecutionContextScope(), 897 Address::DumpStyleResolvedDescription, 898 Address::DumpStyleSectionNameOffset); 899 } 900 } 901 } 902 break; 903 904 case eAddressTypeHost: 905 break; 906 } 907 } 908 if (sstr.GetSize() > 0) 909 { 910 destination.assign (1, '('); 911 destination.append (sstr.GetData(), sstr.GetSize()); 912 destination.append (1, ')'); 913 } 914 } 915 } 916 } 917 } 918 m_is_getting_summary = false; 919 return !destination.empty(); 920 } 921 922 const char * 923 ValueObject::GetSummaryAsCString () 924 { 925 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) 926 { 927 GetSummaryAsCString(GetSummaryFormat().get(), 928 m_summary_str); 929 } 930 if (m_summary_str.empty()) 931 return NULL; 932 return m_summary_str.c_str(); 933 } 934 935 bool 936 ValueObject::IsCStringContainer(bool check_pointer) 937 { 938 ClangASTType pointee_or_element_clang_type; 939 const Flags type_flags (GetTypeInfo (&pointee_or_element_clang_type)); 940 bool is_char_arr_ptr (type_flags.AnySet (ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) && 941 pointee_or_element_clang_type.IsCharType ()); 942 if (!is_char_arr_ptr) 943 return false; 944 if (!check_pointer) 945 return true; 946 if (type_flags.Test(ClangASTType::eTypeIsArray)) 947 return true; 948 addr_t cstr_address = LLDB_INVALID_ADDRESS; 949 AddressType cstr_address_type = eAddressTypeInvalid; 950 cstr_address = GetAddressOf (true, &cstr_address_type); 951 return (cstr_address != LLDB_INVALID_ADDRESS); 952 } 953 954 size_t 955 ValueObject::GetPointeeData (DataExtractor& data, 956 uint32_t item_idx, 957 uint32_t item_count) 958 { 959 ClangASTType pointee_or_element_clang_type; 960 const uint32_t type_info = GetTypeInfo (&pointee_or_element_clang_type); 961 const bool is_pointer_type = type_info & ClangASTType::eTypeIsPointer; 962 const bool is_array_type = type_info & ClangASTType::eTypeIsArray; 963 if (!(is_pointer_type || is_array_type)) 964 return 0; 965 966 if (item_count == 0) 967 return 0; 968 969 const uint64_t item_type_size = pointee_or_element_clang_type.GetByteSize(); 970 const uint64_t bytes = item_count * item_type_size; 971 const uint64_t offset = item_idx * item_type_size; 972 973 if (item_idx == 0 && item_count == 1) // simply a deref 974 { 975 if (is_pointer_type) 976 { 977 Error error; 978 ValueObjectSP pointee_sp = Dereference(error); 979 if (error.Fail() || pointee_sp.get() == NULL) 980 return 0; 981 return pointee_sp->GetDataExtractor().Copy(data); 982 } 983 else 984 { 985 ValueObjectSP child_sp = GetChildAtIndex(0, true); 986 if (child_sp.get() == NULL) 987 return 0; 988 return child_sp->GetDataExtractor().Copy(data); 989 } 990 return true; 991 } 992 else /* (items > 1) */ 993 { 994 Error error; 995 lldb_private::DataBufferHeap* heap_buf_ptr = NULL; 996 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap()); 997 998 AddressType addr_type; 999 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type); 1000 1001 switch (addr_type) 1002 { 1003 case eAddressTypeFile: 1004 { 1005 ModuleSP module_sp (GetModule()); 1006 if (module_sp) 1007 { 1008 addr = addr + offset; 1009 Address so_addr; 1010 module_sp->ResolveFileAddress(addr, so_addr); 1011 ExecutionContext exe_ctx (GetExecutionContextRef()); 1012 Target* target = exe_ctx.GetTargetPtr(); 1013 if (target) 1014 { 1015 heap_buf_ptr->SetByteSize(bytes); 1016 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error); 1017 if (error.Success()) 1018 { 1019 data.SetData(data_sp); 1020 return bytes_read; 1021 } 1022 } 1023 } 1024 } 1025 break; 1026 case eAddressTypeLoad: 1027 { 1028 ExecutionContext exe_ctx (GetExecutionContextRef()); 1029 Process *process = exe_ctx.GetProcessPtr(); 1030 if (process) 1031 { 1032 heap_buf_ptr->SetByteSize(bytes); 1033 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error); 1034 if (error.Success() || bytes_read > 0) 1035 { 1036 data.SetData(data_sp); 1037 return bytes_read; 1038 } 1039 } 1040 } 1041 break; 1042 case eAddressTypeHost: 1043 { 1044 const uint64_t max_bytes = GetClangType().GetByteSize(); 1045 if (max_bytes > offset) 1046 { 1047 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes); 1048 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read); 1049 data.SetData(data_sp); 1050 return bytes_read; 1051 } 1052 } 1053 break; 1054 case eAddressTypeInvalid: 1055 break; 1056 } 1057 } 1058 return 0; 1059 } 1060 1061 uint64_t 1062 ValueObject::GetData (DataExtractor& data) 1063 { 1064 UpdateValueIfNeeded(false); 1065 ExecutionContext exe_ctx (GetExecutionContextRef()); 1066 Error error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get()); 1067 if (error.Fail()) 1068 { 1069 if (m_data.GetByteSize()) 1070 { 1071 data = m_data; 1072 return data.GetByteSize(); 1073 } 1074 else 1075 { 1076 return 0; 1077 } 1078 } 1079 data.SetAddressByteSize(m_data.GetAddressByteSize()); 1080 data.SetByteOrder(m_data.GetByteOrder()); 1081 return data.GetByteSize(); 1082 } 1083 1084 bool 1085 ValueObject::SetData (DataExtractor &data, Error &error) 1086 { 1087 error.Clear(); 1088 // Make sure our value is up to date first so that our location and location 1089 // type is valid. 1090 if (!UpdateValueIfNeeded(false)) 1091 { 1092 error.SetErrorString("unable to read value"); 1093 return false; 1094 } 1095 1096 uint64_t count = 0; 1097 const Encoding encoding = GetClangType().GetEncoding(count); 1098 1099 const size_t byte_size = GetByteSize(); 1100 1101 Value::ValueType value_type = m_value.GetValueType(); 1102 1103 switch (value_type) 1104 { 1105 case Value::eValueTypeScalar: 1106 { 1107 Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size); 1108 1109 if (!set_error.Success()) 1110 { 1111 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString()); 1112 return false; 1113 } 1114 } 1115 break; 1116 case Value::eValueTypeLoadAddress: 1117 { 1118 // If it is a load address, then the scalar value is the storage location 1119 // of the data, and we have to shove this value down to that load location. 1120 ExecutionContext exe_ctx (GetExecutionContextRef()); 1121 Process *process = exe_ctx.GetProcessPtr(); 1122 if (process) 1123 { 1124 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1125 size_t bytes_written = process->WriteMemory(target_addr, 1126 data.GetDataStart(), 1127 byte_size, 1128 error); 1129 if (!error.Success()) 1130 return false; 1131 if (bytes_written != byte_size) 1132 { 1133 error.SetErrorString("unable to write value to memory"); 1134 return false; 1135 } 1136 } 1137 } 1138 break; 1139 case Value::eValueTypeHostAddress: 1140 { 1141 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data. 1142 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0)); 1143 m_data.SetData(buffer_sp, 0); 1144 data.CopyByteOrderedData (0, 1145 byte_size, 1146 const_cast<uint8_t *>(m_data.GetDataStart()), 1147 byte_size, 1148 m_data.GetByteOrder()); 1149 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart(); 1150 } 1151 break; 1152 case Value::eValueTypeFileAddress: 1153 case Value::eValueTypeVector: 1154 break; 1155 } 1156 1157 // If we have reached this point, then we have successfully changed the value. 1158 SetNeedsUpdate(); 1159 return true; 1160 } 1161 1162 // will compute strlen(str), but without consuming more than 1163 // maxlen bytes out of str (this serves the purpose of reading 1164 // chunks of a string without having to worry about 1165 // missing NULL terminators in the chunk) 1166 // of course, if strlen(str) > maxlen, the function will return 1167 // maxlen_value (which should be != maxlen, because that allows you 1168 // to know whether strlen(str) == maxlen or strlen(str) > maxlen) 1169 static uint32_t 1170 strlen_or_inf (const char* str, 1171 uint32_t maxlen, 1172 uint32_t maxlen_value) 1173 { 1174 uint32_t len = 0; 1175 if (str) 1176 { 1177 while(*str) 1178 { 1179 len++;str++; 1180 if (len >= maxlen) 1181 return maxlen_value; 1182 } 1183 } 1184 return len; 1185 } 1186 1187 size_t 1188 ValueObject::ReadPointedString (Stream& s, 1189 Error& error, 1190 uint32_t max_length, 1191 bool honor_array, 1192 Format item_format) 1193 { 1194 ExecutionContext exe_ctx (GetExecutionContextRef()); 1195 Target* target = exe_ctx.GetTargetPtr(); 1196 1197 if (!target) 1198 { 1199 s << "<no target to read from>"; 1200 error.SetErrorString("no target to read from"); 1201 return 0; 1202 } 1203 1204 if (max_length == 0) 1205 max_length = target->GetMaximumSizeOfStringSummary(); 1206 1207 size_t bytes_read = 0; 1208 size_t total_bytes_read = 0; 1209 1210 ClangASTType clang_type = GetClangType(); 1211 ClangASTType elem_or_pointee_clang_type; 1212 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type)); 1213 if (type_flags.AnySet (ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) && 1214 elem_or_pointee_clang_type.IsCharType ()) 1215 { 1216 addr_t cstr_address = LLDB_INVALID_ADDRESS; 1217 AddressType cstr_address_type = eAddressTypeInvalid; 1218 1219 size_t cstr_len = 0; 1220 bool capped_data = false; 1221 if (type_flags.Test (ClangASTType::eTypeIsArray)) 1222 { 1223 // We have an array 1224 uint64_t array_size = 0; 1225 if (clang_type.IsArrayType(NULL, &array_size, NULL)) 1226 { 1227 cstr_len = array_size; 1228 if (cstr_len > max_length) 1229 { 1230 capped_data = true; 1231 cstr_len = max_length; 1232 } 1233 } 1234 cstr_address = GetAddressOf (true, &cstr_address_type); 1235 } 1236 else 1237 { 1238 // We have a pointer 1239 cstr_address = GetPointerValue (&cstr_address_type); 1240 } 1241 1242 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) 1243 { 1244 s << "<invalid address>"; 1245 error.SetErrorString("invalid address"); 1246 return 0; 1247 } 1248 1249 Address cstr_so_addr (cstr_address); 1250 DataExtractor data; 1251 if (cstr_len > 0 && honor_array) 1252 { 1253 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host 1254 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this 1255 GetPointeeData(data, 0, cstr_len); 1256 1257 if ((bytes_read = data.GetByteSize()) > 0) 1258 { 1259 total_bytes_read = bytes_read; 1260 s << '"'; 1261 data.Dump (&s, 1262 0, // Start offset in "data" 1263 item_format, 1264 1, // Size of item (1 byte for a char!) 1265 bytes_read, // How many bytes to print? 1266 UINT32_MAX, // num per line 1267 LLDB_INVALID_ADDRESS,// base address 1268 0, // bitfield bit size 1269 0); // bitfield bit offset 1270 if (capped_data) 1271 s << "..."; 1272 s << '"'; 1273 } 1274 } 1275 else 1276 { 1277 cstr_len = max_length; 1278 const size_t k_max_buf_size = 64; 1279 1280 size_t offset = 0; 1281 1282 int cstr_len_displayed = -1; 1283 bool capped_cstr = false; 1284 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host 1285 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this 1286 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) 1287 { 1288 total_bytes_read += bytes_read; 1289 const char *cstr = data.PeekCStr(0); 1290 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1); 1291 if (len > k_max_buf_size) 1292 len = k_max_buf_size; 1293 if (cstr && cstr_len_displayed < 0) 1294 s << '"'; 1295 1296 if (cstr_len_displayed < 0) 1297 cstr_len_displayed = len; 1298 1299 if (len == 0) 1300 break; 1301 cstr_len_displayed += len; 1302 if (len > bytes_read) 1303 len = bytes_read; 1304 if (len > cstr_len) 1305 len = cstr_len; 1306 1307 data.Dump (&s, 1308 0, // Start offset in "data" 1309 item_format, 1310 1, // Size of item (1 byte for a char!) 1311 len, // How many bytes to print? 1312 UINT32_MAX, // num per line 1313 LLDB_INVALID_ADDRESS,// base address 1314 0, // bitfield bit size 1315 0); // bitfield bit offset 1316 1317 if (len < k_max_buf_size) 1318 break; 1319 1320 if (len >= cstr_len) 1321 { 1322 capped_cstr = true; 1323 break; 1324 } 1325 1326 cstr_len -= len; 1327 offset += len; 1328 } 1329 1330 if (cstr_len_displayed >= 0) 1331 { 1332 s << '"'; 1333 if (capped_cstr) 1334 s << "..."; 1335 } 1336 } 1337 } 1338 else 1339 { 1340 error.SetErrorString("not a string object"); 1341 s << "<not a string object>"; 1342 } 1343 return total_bytes_read; 1344 } 1345 1346 const char * 1347 ValueObject::GetObjectDescription () 1348 { 1349 1350 if (!UpdateValueIfNeeded (true)) 1351 return NULL; 1352 1353 if (!m_object_desc_str.empty()) 1354 return m_object_desc_str.c_str(); 1355 1356 ExecutionContext exe_ctx (GetExecutionContextRef()); 1357 Process *process = exe_ctx.GetProcessPtr(); 1358 if (process == NULL) 1359 return NULL; 1360 1361 StreamString s; 1362 1363 LanguageType language = GetObjectRuntimeLanguage(); 1364 LanguageRuntime *runtime = process->GetLanguageRuntime(language); 1365 1366 if (runtime == NULL) 1367 { 1368 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway... 1369 ClangASTType clang_type = GetClangType(); 1370 if (clang_type) 1371 { 1372 bool is_signed; 1373 if (clang_type.IsIntegerType (is_signed) || clang_type.IsPointerType ()) 1374 { 1375 runtime = process->GetLanguageRuntime(eLanguageTypeObjC); 1376 } 1377 } 1378 } 1379 1380 if (runtime && runtime->GetObjectDescription(s, *this)) 1381 { 1382 m_object_desc_str.append (s.GetData()); 1383 } 1384 1385 if (m_object_desc_str.empty()) 1386 return NULL; 1387 else 1388 return m_object_desc_str.c_str(); 1389 } 1390 1391 bool 1392 ValueObject::GetValueAsCString (lldb::Format format, 1393 std::string& destination) 1394 { 1395 if (GetClangType().IsAggregateType () == false && UpdateValueIfNeeded(false)) 1396 { 1397 const Value::ContextType context_type = m_value.GetContextType(); 1398 1399 if (context_type == Value::eContextTypeRegisterInfo) 1400 { 1401 const RegisterInfo *reg_info = m_value.GetRegisterInfo(); 1402 if (reg_info) 1403 { 1404 ExecutionContext exe_ctx (GetExecutionContextRef()); 1405 1406 StreamString reg_sstr; 1407 m_data.Dump (®_sstr, 1408 0, 1409 format, 1410 reg_info->byte_size, 1411 1, 1412 UINT32_MAX, 1413 LLDB_INVALID_ADDRESS, 1414 0, 1415 0, 1416 exe_ctx.GetBestExecutionContextScope()); 1417 destination.swap(reg_sstr.GetString()); 1418 } 1419 } 1420 else 1421 { 1422 ClangASTType clang_type = GetClangType (); 1423 if (clang_type) 1424 { 1425 // put custom bytes to display in this DataExtractor to override the default value logic 1426 lldb_private::DataExtractor special_format_data; 1427 if (format == eFormatCString) 1428 { 1429 Flags type_flags(clang_type.GetTypeInfo(NULL)); 1430 if (type_flags.Test(ClangASTType::eTypeIsPointer) && !type_flags.Test(ClangASTType::eTypeIsObjC)) 1431 { 1432 // if we are dumping a pointer as a c-string, get the pointee data as a string 1433 TargetSP target_sp(GetTargetSP()); 1434 if (target_sp) 1435 { 1436 size_t max_len = target_sp->GetMaximumSizeOfStringSummary(); 1437 Error error; 1438 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0)); 1439 Address address(GetPointerValue()); 1440 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success()) 1441 special_format_data.SetData(buffer_sp); 1442 } 1443 } 1444 } 1445 1446 StreamString sstr; 1447 ExecutionContext exe_ctx (GetExecutionContextRef()); 1448 clang_type.DumpTypeValue (&sstr, // The stream to use for display 1449 format, // Format to display this type with 1450 special_format_data.GetByteSize() ? 1451 special_format_data: m_data, // Data to extract from 1452 0, // Byte offset into "m_data" 1453 GetByteSize(), // Byte size of item in "m_data" 1454 GetBitfieldBitSize(), // Bitfield bit size 1455 GetBitfieldBitOffset(), // Bitfield bit offset 1456 exe_ctx.GetBestExecutionContextScope()); 1457 // Don't set the m_error to anything here otherwise 1458 // we won't be able to re-format as anything else. The 1459 // code for ClangASTType::DumpTypeValue() should always 1460 // return something, even if that something contains 1461 // an error messsage. "m_error" is used to detect errors 1462 // when reading the valid object, not for formatting errors. 1463 if (sstr.GetString().empty()) 1464 destination.clear(); 1465 else 1466 destination.swap(sstr.GetString()); 1467 } 1468 } 1469 return !destination.empty(); 1470 } 1471 else 1472 return false; 1473 } 1474 1475 const char * 1476 ValueObject::GetValueAsCString () 1477 { 1478 if (UpdateValueIfNeeded(true)) 1479 { 1480 lldb::Format my_format = GetFormat(); 1481 if (my_format == lldb::eFormatDefault) 1482 { 1483 if (m_type_format_sp) 1484 my_format = m_type_format_sp->GetFormat(); 1485 else 1486 { 1487 if (m_is_bitfield_for_scalar) 1488 my_format = eFormatUnsigned; 1489 else 1490 { 1491 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) 1492 { 1493 const RegisterInfo *reg_info = m_value.GetRegisterInfo(); 1494 if (reg_info) 1495 my_format = reg_info->format; 1496 } 1497 else 1498 { 1499 my_format = GetClangType().GetFormat(); 1500 } 1501 } 1502 } 1503 } 1504 if (my_format != m_last_format || m_value_str.empty()) 1505 { 1506 m_last_format = my_format; 1507 if (GetValueAsCString(my_format, m_value_str)) 1508 { 1509 if (!m_value_did_change && m_old_value_valid) 1510 { 1511 // The value was gotten successfully, so we consider the 1512 // value as changed if the value string differs 1513 SetValueDidChange (m_old_value_str != m_value_str); 1514 } 1515 } 1516 } 1517 } 1518 if (m_value_str.empty()) 1519 return NULL; 1520 return m_value_str.c_str(); 1521 } 1522 1523 // if > 8bytes, 0 is returned. this method should mostly be used 1524 // to read address values out of pointers 1525 uint64_t 1526 ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success) 1527 { 1528 // If our byte size is zero this is an aggregate type that has children 1529 if (!GetClangType().IsAggregateType()) 1530 { 1531 Scalar scalar; 1532 if (ResolveValue (scalar)) 1533 { 1534 if (success) 1535 *success = true; 1536 return scalar.ULongLong(fail_value); 1537 } 1538 // fallthrough, otherwise... 1539 } 1540 1541 if (success) 1542 *success = false; 1543 return fail_value; 1544 } 1545 1546 int64_t 1547 ValueObject::GetValueAsSigned (int64_t fail_value, bool *success) 1548 { 1549 // If our byte size is zero this is an aggregate type that has children 1550 if (!GetClangType().IsAggregateType()) 1551 { 1552 Scalar scalar; 1553 if (ResolveValue (scalar)) 1554 { 1555 if (success) 1556 *success = true; 1557 return scalar.SLongLong(fail_value); 1558 } 1559 // fallthrough, otherwise... 1560 } 1561 1562 if (success) 1563 *success = false; 1564 return fail_value; 1565 } 1566 1567 // if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep 1568 // this call up to date by returning true for your new special cases. We will eventually move 1569 // to checking this call result before trying to display special cases 1570 bool 1571 ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display, 1572 Format custom_format) 1573 { 1574 Flags flags(GetTypeInfo()); 1575 if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) 1576 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) 1577 { 1578 if (IsCStringContainer(true) && 1579 (custom_format == eFormatCString || 1580 custom_format == eFormatCharArray || 1581 custom_format == eFormatChar || 1582 custom_format == eFormatVectorOfChar)) 1583 return true; 1584 1585 if (flags.Test(ClangASTType::eTypeIsArray)) 1586 { 1587 if ((custom_format == eFormatBytes) || 1588 (custom_format == eFormatBytesWithASCII)) 1589 return true; 1590 1591 if ((custom_format == eFormatVectorOfChar) || 1592 (custom_format == eFormatVectorOfFloat32) || 1593 (custom_format == eFormatVectorOfFloat64) || 1594 (custom_format == eFormatVectorOfSInt16) || 1595 (custom_format == eFormatVectorOfSInt32) || 1596 (custom_format == eFormatVectorOfSInt64) || 1597 (custom_format == eFormatVectorOfSInt8) || 1598 (custom_format == eFormatVectorOfUInt128) || 1599 (custom_format == eFormatVectorOfUInt16) || 1600 (custom_format == eFormatVectorOfUInt32) || 1601 (custom_format == eFormatVectorOfUInt64) || 1602 (custom_format == eFormatVectorOfUInt8)) 1603 return true; 1604 } 1605 } 1606 return false; 1607 } 1608 1609 bool 1610 ValueObject::DumpPrintableRepresentation(Stream& s, 1611 ValueObjectRepresentationStyle val_obj_display, 1612 Format custom_format, 1613 PrintableRepresentationSpecialCases special) 1614 { 1615 1616 Flags flags(GetTypeInfo()); 1617 1618 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow); 1619 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly); 1620 1621 if (allow_special) 1622 { 1623 if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) 1624 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) 1625 { 1626 // when being asked to get a printable display an array or pointer type directly, 1627 // try to "do the right thing" 1628 1629 if (IsCStringContainer(true) && 1630 (custom_format == eFormatCString || 1631 custom_format == eFormatCharArray || 1632 custom_format == eFormatChar || 1633 custom_format == eFormatVectorOfChar)) // print char[] & char* directly 1634 { 1635 Error error; 1636 ReadPointedString(s, 1637 error, 1638 0, 1639 (custom_format == eFormatVectorOfChar) || 1640 (custom_format == eFormatCharArray)); 1641 return !error.Fail(); 1642 } 1643 1644 if (custom_format == eFormatEnum) 1645 return false; 1646 1647 // this only works for arrays, because I have no way to know when 1648 // the pointed memory ends, and no special \0 end of data marker 1649 if (flags.Test(ClangASTType::eTypeIsArray)) 1650 { 1651 if ((custom_format == eFormatBytes) || 1652 (custom_format == eFormatBytesWithASCII)) 1653 { 1654 const size_t count = GetNumChildren(); 1655 1656 s << '['; 1657 for (size_t low = 0; low < count; low++) 1658 { 1659 1660 if (low) 1661 s << ','; 1662 1663 ValueObjectSP child = GetChildAtIndex(low,true); 1664 if (!child.get()) 1665 { 1666 s << "<invalid child>"; 1667 continue; 1668 } 1669 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format); 1670 } 1671 1672 s << ']'; 1673 1674 return true; 1675 } 1676 1677 if ((custom_format == eFormatVectorOfChar) || 1678 (custom_format == eFormatVectorOfFloat32) || 1679 (custom_format == eFormatVectorOfFloat64) || 1680 (custom_format == eFormatVectorOfSInt16) || 1681 (custom_format == eFormatVectorOfSInt32) || 1682 (custom_format == eFormatVectorOfSInt64) || 1683 (custom_format == eFormatVectorOfSInt8) || 1684 (custom_format == eFormatVectorOfUInt128) || 1685 (custom_format == eFormatVectorOfUInt16) || 1686 (custom_format == eFormatVectorOfUInt32) || 1687 (custom_format == eFormatVectorOfUInt64) || 1688 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly 1689 { 1690 const size_t count = GetNumChildren(); 1691 1692 Format format = FormatManager::GetSingleItemFormat(custom_format); 1693 1694 s << '['; 1695 for (size_t low = 0; low < count; low++) 1696 { 1697 1698 if (low) 1699 s << ','; 1700 1701 ValueObjectSP child = GetChildAtIndex(low,true); 1702 if (!child.get()) 1703 { 1704 s << "<invalid child>"; 1705 continue; 1706 } 1707 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format); 1708 } 1709 1710 s << ']'; 1711 1712 return true; 1713 } 1714 } 1715 1716 if ((custom_format == eFormatBoolean) || 1717 (custom_format == eFormatBinary) || 1718 (custom_format == eFormatChar) || 1719 (custom_format == eFormatCharPrintable) || 1720 (custom_format == eFormatComplexFloat) || 1721 (custom_format == eFormatDecimal) || 1722 (custom_format == eFormatHex) || 1723 (custom_format == eFormatHexUppercase) || 1724 (custom_format == eFormatFloat) || 1725 (custom_format == eFormatOctal) || 1726 (custom_format == eFormatOSType) || 1727 (custom_format == eFormatUnicode16) || 1728 (custom_format == eFormatUnicode32) || 1729 (custom_format == eFormatUnsigned) || 1730 (custom_format == eFormatPointer) || 1731 (custom_format == eFormatComplexInteger) || 1732 (custom_format == eFormatComplex) || 1733 (custom_format == eFormatDefault)) // use the [] operator 1734 return false; 1735 } 1736 } 1737 1738 if (only_special) 1739 return false; 1740 1741 bool var_success = false; 1742 1743 { 1744 const char *cstr = NULL; 1745 1746 // this is a local stream that we are using to ensure that the data pointed to by cstr survives 1747 // long enough for us to copy it to its destination - it is necessary to have this temporary storage 1748 // area for cases where our desired output is not backed by some other longer-term storage 1749 StreamString strm; 1750 1751 if (custom_format != eFormatInvalid) 1752 SetFormat(custom_format); 1753 1754 switch(val_obj_display) 1755 { 1756 case eValueObjectRepresentationStyleValue: 1757 cstr = GetValueAsCString(); 1758 break; 1759 1760 case eValueObjectRepresentationStyleSummary: 1761 cstr = GetSummaryAsCString(); 1762 break; 1763 1764 case eValueObjectRepresentationStyleLanguageSpecific: 1765 cstr = GetObjectDescription(); 1766 break; 1767 1768 case eValueObjectRepresentationStyleLocation: 1769 cstr = GetLocationAsCString(); 1770 break; 1771 1772 case eValueObjectRepresentationStyleChildrenCount: 1773 strm.Printf("%zu", GetNumChildren()); 1774 cstr = strm.GetString().c_str(); 1775 break; 1776 1777 case eValueObjectRepresentationStyleType: 1778 cstr = GetTypeName().AsCString(); 1779 break; 1780 1781 case eValueObjectRepresentationStyleName: 1782 cstr = GetName().AsCString(); 1783 break; 1784 1785 case eValueObjectRepresentationStyleExpressionPath: 1786 GetExpressionPath(strm, false); 1787 cstr = strm.GetString().c_str(); 1788 break; 1789 } 1790 1791 if (!cstr) 1792 { 1793 if (val_obj_display == eValueObjectRepresentationStyleValue) 1794 cstr = GetSummaryAsCString(); 1795 else if (val_obj_display == eValueObjectRepresentationStyleSummary) 1796 { 1797 if (GetClangType().IsAggregateType()) 1798 { 1799 strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString()); 1800 cstr = strm.GetString().c_str(); 1801 } 1802 else 1803 cstr = GetValueAsCString(); 1804 } 1805 } 1806 1807 if (cstr) 1808 s.PutCString(cstr); 1809 else 1810 { 1811 if (m_error.Fail()) 1812 s.Printf("<%s>", m_error.AsCString()); 1813 else if (val_obj_display == eValueObjectRepresentationStyleSummary) 1814 s.PutCString("<no summary available>"); 1815 else if (val_obj_display == eValueObjectRepresentationStyleValue) 1816 s.PutCString("<no value available>"); 1817 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific) 1818 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description 1819 else 1820 s.PutCString("<no printable representation>"); 1821 } 1822 1823 // we should only return false here if we could not do *anything* 1824 // even if we have an error message as output, that's a success 1825 // from our callers' perspective, so return true 1826 var_success = true; 1827 1828 if (custom_format != eFormatInvalid) 1829 SetFormat(eFormatDefault); 1830 } 1831 1832 return var_success; 1833 } 1834 1835 addr_t 1836 ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type) 1837 { 1838 if (!UpdateValueIfNeeded(false)) 1839 return LLDB_INVALID_ADDRESS; 1840 1841 switch (m_value.GetValueType()) 1842 { 1843 case Value::eValueTypeScalar: 1844 case Value::eValueTypeVector: 1845 if (scalar_is_load_address) 1846 { 1847 if(address_type) 1848 *address_type = eAddressTypeLoad; 1849 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1850 } 1851 break; 1852 1853 case Value::eValueTypeLoadAddress: 1854 case Value::eValueTypeFileAddress: 1855 case Value::eValueTypeHostAddress: 1856 { 1857 if(address_type) 1858 *address_type = m_value.GetValueAddressType (); 1859 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1860 } 1861 break; 1862 } 1863 if (address_type) 1864 *address_type = eAddressTypeInvalid; 1865 return LLDB_INVALID_ADDRESS; 1866 } 1867 1868 addr_t 1869 ValueObject::GetPointerValue (AddressType *address_type) 1870 { 1871 addr_t address = LLDB_INVALID_ADDRESS; 1872 if(address_type) 1873 *address_type = eAddressTypeInvalid; 1874 1875 if (!UpdateValueIfNeeded(false)) 1876 return address; 1877 1878 switch (m_value.GetValueType()) 1879 { 1880 case Value::eValueTypeScalar: 1881 case Value::eValueTypeVector: 1882 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1883 break; 1884 1885 case Value::eValueTypeHostAddress: 1886 case Value::eValueTypeLoadAddress: 1887 case Value::eValueTypeFileAddress: 1888 { 1889 lldb::offset_t data_offset = 0; 1890 address = m_data.GetPointer(&data_offset); 1891 } 1892 break; 1893 } 1894 1895 if (address_type) 1896 *address_type = GetAddressTypeOfChildren(); 1897 1898 return address; 1899 } 1900 1901 bool 1902 ValueObject::SetValueFromCString (const char *value_str, Error& error) 1903 { 1904 error.Clear(); 1905 // Make sure our value is up to date first so that our location and location 1906 // type is valid. 1907 if (!UpdateValueIfNeeded(false)) 1908 { 1909 error.SetErrorString("unable to read value"); 1910 return false; 1911 } 1912 1913 uint64_t count = 0; 1914 const Encoding encoding = GetClangType().GetEncoding (count); 1915 1916 const size_t byte_size = GetByteSize(); 1917 1918 Value::ValueType value_type = m_value.GetValueType(); 1919 1920 if (value_type == Value::eValueTypeScalar) 1921 { 1922 // If the value is already a scalar, then let the scalar change itself: 1923 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size); 1924 } 1925 else if (byte_size <= Scalar::GetMaxByteSize()) 1926 { 1927 // If the value fits in a scalar, then make a new scalar and again let the 1928 // scalar code do the conversion, then figure out where to put the new value. 1929 Scalar new_scalar; 1930 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size); 1931 if (error.Success()) 1932 { 1933 switch (value_type) 1934 { 1935 case Value::eValueTypeLoadAddress: 1936 { 1937 // If it is a load address, then the scalar value is the storage location 1938 // of the data, and we have to shove this value down to that load location. 1939 ExecutionContext exe_ctx (GetExecutionContextRef()); 1940 Process *process = exe_ctx.GetProcessPtr(); 1941 if (process) 1942 { 1943 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS); 1944 size_t bytes_written = process->WriteScalarToMemory (target_addr, 1945 new_scalar, 1946 byte_size, 1947 error); 1948 if (!error.Success()) 1949 return false; 1950 if (bytes_written != byte_size) 1951 { 1952 error.SetErrorString("unable to write value to memory"); 1953 return false; 1954 } 1955 } 1956 } 1957 break; 1958 case Value::eValueTypeHostAddress: 1959 { 1960 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data. 1961 DataExtractor new_data; 1962 new_data.SetByteOrder (m_data.GetByteOrder()); 1963 1964 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0)); 1965 m_data.SetData(buffer_sp, 0); 1966 bool success = new_scalar.GetData(new_data); 1967 if (success) 1968 { 1969 new_data.CopyByteOrderedData (0, 1970 byte_size, 1971 const_cast<uint8_t *>(m_data.GetDataStart()), 1972 byte_size, 1973 m_data.GetByteOrder()); 1974 } 1975 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart(); 1976 1977 } 1978 break; 1979 case Value::eValueTypeFileAddress: 1980 case Value::eValueTypeScalar: 1981 case Value::eValueTypeVector: 1982 break; 1983 } 1984 } 1985 else 1986 { 1987 return false; 1988 } 1989 } 1990 else 1991 { 1992 // We don't support setting things bigger than a scalar at present. 1993 error.SetErrorString("unable to write aggregate data type"); 1994 return false; 1995 } 1996 1997 // If we have reached this point, then we have successfully changed the value. 1998 SetNeedsUpdate(); 1999 return true; 2000 } 2001 2002 bool 2003 ValueObject::GetDeclaration (Declaration &decl) 2004 { 2005 decl.Clear(); 2006 return false; 2007 } 2008 2009 ConstString 2010 ValueObject::GetTypeName() 2011 { 2012 return GetClangType().GetConstTypeName(); 2013 } 2014 2015 ConstString 2016 ValueObject::GetQualifiedTypeName() 2017 { 2018 return GetClangType().GetConstQualifiedTypeName(); 2019 } 2020 2021 2022 LanguageType 2023 ValueObject::GetObjectRuntimeLanguage () 2024 { 2025 return GetClangType().GetMinimumLanguage (); 2026 } 2027 2028 void 2029 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj) 2030 { 2031 m_synthetic_children[key] = valobj; 2032 } 2033 2034 ValueObjectSP 2035 ValueObject::GetSyntheticChild (const ConstString &key) const 2036 { 2037 ValueObjectSP synthetic_child_sp; 2038 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key); 2039 if (pos != m_synthetic_children.end()) 2040 synthetic_child_sp = pos->second->GetSP(); 2041 return synthetic_child_sp; 2042 } 2043 2044 uint32_t 2045 ValueObject::GetTypeInfo (ClangASTType *pointee_or_element_clang_type) 2046 { 2047 return GetClangType().GetTypeInfo (pointee_or_element_clang_type); 2048 } 2049 2050 bool 2051 ValueObject::IsPointerType () 2052 { 2053 return GetClangType().IsPointerType(); 2054 } 2055 2056 bool 2057 ValueObject::IsArrayType () 2058 { 2059 return GetClangType().IsArrayType (NULL, NULL, NULL); 2060 } 2061 2062 bool 2063 ValueObject::IsScalarType () 2064 { 2065 return GetClangType().IsScalarType (); 2066 } 2067 2068 bool 2069 ValueObject::IsIntegerType (bool &is_signed) 2070 { 2071 return GetClangType().IsIntegerType (is_signed); 2072 } 2073 2074 bool 2075 ValueObject::IsPointerOrReferenceType () 2076 { 2077 return GetClangType().IsPointerOrReferenceType (); 2078 } 2079 2080 bool 2081 ValueObject::IsPossibleDynamicType () 2082 { 2083 ExecutionContext exe_ctx (GetExecutionContextRef()); 2084 Process *process = exe_ctx.GetProcessPtr(); 2085 if (process) 2086 return process->IsPossibleDynamicValue(*this); 2087 else 2088 return GetClangType().IsPossibleDynamicType (NULL, true, true); 2089 } 2090 2091 bool 2092 ValueObject::IsObjCNil () 2093 { 2094 const uint32_t mask = ClangASTType::eTypeIsObjC | ClangASTType::eTypeIsPointer; 2095 bool isObjCpointer = (((GetClangType().GetTypeInfo(NULL)) & mask) == mask); 2096 if (!isObjCpointer) 2097 return false; 2098 bool canReadValue = true; 2099 bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0; 2100 return canReadValue && isZero; 2101 } 2102 2103 ValueObjectSP 2104 ValueObject::GetSyntheticArrayMember (size_t index, bool can_create) 2105 { 2106 const uint32_t type_info = GetTypeInfo (); 2107 if (type_info & ClangASTType::eTypeIsArray) 2108 return GetSyntheticArrayMemberFromArray(index, can_create); 2109 2110 if (type_info & ClangASTType::eTypeIsPointer) 2111 return GetSyntheticArrayMemberFromPointer(index, can_create); 2112 2113 return ValueObjectSP(); 2114 2115 } 2116 2117 ValueObjectSP 2118 ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create) 2119 { 2120 ValueObjectSP synthetic_child_sp; 2121 if (IsPointerType ()) 2122 { 2123 char index_str[64]; 2124 snprintf(index_str, sizeof(index_str), "[%zu]", index); 2125 ConstString index_const_str(index_str); 2126 // Check if we have already created a synthetic array member in this 2127 // valid object. If we have we will re-use it. 2128 synthetic_child_sp = GetSyntheticChild (index_const_str); 2129 if (!synthetic_child_sp) 2130 { 2131 ValueObject *synthetic_child; 2132 // We haven't made a synthetic array member for INDEX yet, so 2133 // lets make one and cache it for any future reference. 2134 synthetic_child = CreateChildAtIndex(0, true, index); 2135 2136 // Cache the value if we got one back... 2137 if (synthetic_child) 2138 { 2139 AddSyntheticChild(index_const_str, synthetic_child); 2140 synthetic_child_sp = synthetic_child->GetSP(); 2141 synthetic_child_sp->SetName(ConstString(index_str)); 2142 synthetic_child_sp->m_is_array_item_for_pointer = true; 2143 } 2144 } 2145 } 2146 return synthetic_child_sp; 2147 } 2148 2149 // This allows you to create an array member using and index 2150 // that doesn't not fall in the normal bounds of the array. 2151 // Many times structure can be defined as: 2152 // struct Collection 2153 // { 2154 // uint32_t item_count; 2155 // Item item_array[0]; 2156 // }; 2157 // The size of the "item_array" is 1, but many times in practice 2158 // there are more items in "item_array". 2159 2160 ValueObjectSP 2161 ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create) 2162 { 2163 ValueObjectSP synthetic_child_sp; 2164 if (IsArrayType ()) 2165 { 2166 char index_str[64]; 2167 snprintf(index_str, sizeof(index_str), "[%zu]", index); 2168 ConstString index_const_str(index_str); 2169 // Check if we have already created a synthetic array member in this 2170 // valid object. If we have we will re-use it. 2171 synthetic_child_sp = GetSyntheticChild (index_const_str); 2172 if (!synthetic_child_sp) 2173 { 2174 ValueObject *synthetic_child; 2175 // We haven't made a synthetic array member for INDEX yet, so 2176 // lets make one and cache it for any future reference. 2177 synthetic_child = CreateChildAtIndex(0, true, index); 2178 2179 // Cache the value if we got one back... 2180 if (synthetic_child) 2181 { 2182 AddSyntheticChild(index_const_str, synthetic_child); 2183 synthetic_child_sp = synthetic_child->GetSP(); 2184 synthetic_child_sp->SetName(ConstString(index_str)); 2185 synthetic_child_sp->m_is_array_item_for_pointer = true; 2186 } 2187 } 2188 } 2189 return synthetic_child_sp; 2190 } 2191 2192 ValueObjectSP 2193 ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create) 2194 { 2195 ValueObjectSP synthetic_child_sp; 2196 if (IsScalarType ()) 2197 { 2198 char index_str[64]; 2199 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to); 2200 ConstString index_const_str(index_str); 2201 // Check if we have already created a synthetic array member in this 2202 // valid object. If we have we will re-use it. 2203 synthetic_child_sp = GetSyntheticChild (index_const_str); 2204 if (!synthetic_child_sp) 2205 { 2206 // We haven't made a synthetic array member for INDEX yet, so 2207 // lets make one and cache it for any future reference. 2208 ValueObjectChild *synthetic_child = new ValueObjectChild (*this, 2209 GetClangType(), 2210 index_const_str, 2211 GetByteSize(), 2212 0, 2213 to-from+1, 2214 from, 2215 false, 2216 false, 2217 eAddressTypeInvalid); 2218 2219 // Cache the value if we got one back... 2220 if (synthetic_child) 2221 { 2222 AddSyntheticChild(index_const_str, synthetic_child); 2223 synthetic_child_sp = synthetic_child->GetSP(); 2224 synthetic_child_sp->SetName(ConstString(index_str)); 2225 synthetic_child_sp->m_is_bitfield_for_scalar = true; 2226 } 2227 } 2228 } 2229 return synthetic_child_sp; 2230 } 2231 2232 ValueObjectSP 2233 ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create) 2234 { 2235 2236 ValueObjectSP synthetic_child_sp; 2237 2238 char name_str[64]; 2239 snprintf(name_str, sizeof(name_str), "@%i", offset); 2240 ConstString name_const_str(name_str); 2241 2242 // Check if we have already created a synthetic array member in this 2243 // valid object. If we have we will re-use it. 2244 synthetic_child_sp = GetSyntheticChild (name_const_str); 2245 2246 if (synthetic_child_sp.get()) 2247 return synthetic_child_sp; 2248 2249 if (!can_create) 2250 return ValueObjectSP(); 2251 2252 ValueObjectChild *synthetic_child = new ValueObjectChild(*this, 2253 type, 2254 name_const_str, 2255 type.GetByteSize(), 2256 offset, 2257 0, 2258 0, 2259 false, 2260 false, 2261 eAddressTypeInvalid); 2262 if (synthetic_child) 2263 { 2264 AddSyntheticChild(name_const_str, synthetic_child); 2265 synthetic_child_sp = synthetic_child->GetSP(); 2266 synthetic_child_sp->SetName(name_const_str); 2267 synthetic_child_sp->m_is_child_at_offset = true; 2268 } 2269 return synthetic_child_sp; 2270 } 2271 2272 // your expression path needs to have a leading . or -> 2273 // (unless it somehow "looks like" an array, in which case it has 2274 // a leading [ symbol). while the [ is meaningful and should be shown 2275 // to the user, . and -> are just parser design, but by no means 2276 // added information for the user.. strip them off 2277 static const char* 2278 SkipLeadingExpressionPathSeparators(const char* expression) 2279 { 2280 if (!expression || !expression[0]) 2281 return expression; 2282 if (expression[0] == '.') 2283 return expression+1; 2284 if (expression[0] == '-' && expression[1] == '>') 2285 return expression+2; 2286 return expression; 2287 } 2288 2289 ValueObjectSP 2290 ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create) 2291 { 2292 ValueObjectSP synthetic_child_sp; 2293 ConstString name_const_string(expression); 2294 // Check if we have already created a synthetic array member in this 2295 // valid object. If we have we will re-use it. 2296 synthetic_child_sp = GetSyntheticChild (name_const_string); 2297 if (!synthetic_child_sp) 2298 { 2299 // We haven't made a synthetic array member for expression yet, so 2300 // lets make one and cache it for any future reference. 2301 synthetic_child_sp = GetValueForExpressionPath(expression, 2302 NULL, NULL, NULL, 2303 GetValueForExpressionPathOptions().DontAllowSyntheticChildren()); 2304 2305 // Cache the value if we got one back... 2306 if (synthetic_child_sp.get()) 2307 { 2308 // FIXME: this causes a "real" child to end up with its name changed to the contents of expression 2309 AddSyntheticChild(name_const_string, synthetic_child_sp.get()); 2310 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression))); 2311 } 2312 } 2313 return synthetic_child_sp; 2314 } 2315 2316 void 2317 ValueObject::CalculateSyntheticValue (bool use_synthetic) 2318 { 2319 if (use_synthetic == false) 2320 return; 2321 2322 TargetSP target_sp(GetTargetSP()); 2323 if (target_sp && target_sp->GetEnableSyntheticValue() == false) 2324 { 2325 m_synthetic_value = NULL; 2326 return; 2327 } 2328 2329 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp); 2330 2331 if (!UpdateFormatsIfNeeded() && m_synthetic_value) 2332 return; 2333 2334 if (m_synthetic_children_sp.get() == NULL) 2335 return; 2336 2337 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value) 2338 return; 2339 2340 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp); 2341 } 2342 2343 void 2344 ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic) 2345 { 2346 if (use_dynamic == eNoDynamicValues) 2347 return; 2348 2349 if (!m_dynamic_value && !IsDynamic()) 2350 { 2351 ExecutionContext exe_ctx (GetExecutionContextRef()); 2352 Process *process = exe_ctx.GetProcessPtr(); 2353 if (process && process->IsPossibleDynamicValue(*this)) 2354 { 2355 ClearDynamicTypeInformation (); 2356 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic); 2357 } 2358 } 2359 } 2360 2361 ValueObjectSP 2362 ValueObject::GetDynamicValue (DynamicValueType use_dynamic) 2363 { 2364 if (use_dynamic == eNoDynamicValues) 2365 return ValueObjectSP(); 2366 2367 if (!IsDynamic() && m_dynamic_value == NULL) 2368 { 2369 CalculateDynamicValue(use_dynamic); 2370 } 2371 if (m_dynamic_value) 2372 return m_dynamic_value->GetSP(); 2373 else 2374 return ValueObjectSP(); 2375 } 2376 2377 ValueObjectSP 2378 ValueObject::GetStaticValue() 2379 { 2380 return GetSP(); 2381 } 2382 2383 lldb::ValueObjectSP 2384 ValueObject::GetNonSyntheticValue () 2385 { 2386 return GetSP(); 2387 } 2388 2389 ValueObjectSP 2390 ValueObject::GetSyntheticValue (bool use_synthetic) 2391 { 2392 if (use_synthetic == false) 2393 return ValueObjectSP(); 2394 2395 CalculateSyntheticValue(use_synthetic); 2396 2397 if (m_synthetic_value) 2398 return m_synthetic_value->GetSP(); 2399 else 2400 return ValueObjectSP(); 2401 } 2402 2403 bool 2404 ValueObject::HasSyntheticValue() 2405 { 2406 UpdateFormatsIfNeeded(); 2407 2408 if (m_synthetic_children_sp.get() == NULL) 2409 return false; 2410 2411 CalculateSyntheticValue(true); 2412 2413 if (m_synthetic_value) 2414 return true; 2415 else 2416 return false; 2417 } 2418 2419 bool 2420 ValueObject::GetBaseClassPath (Stream &s) 2421 { 2422 if (IsBaseClass()) 2423 { 2424 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s); 2425 ClangASTType clang_type = GetClangType(); 2426 std::string cxx_class_name; 2427 bool this_had_base_class = clang_type.GetCXXClassName (cxx_class_name); 2428 if (this_had_base_class) 2429 { 2430 if (parent_had_base_class) 2431 s.PutCString("::"); 2432 s.PutCString(cxx_class_name.c_str()); 2433 } 2434 return parent_had_base_class || this_had_base_class; 2435 } 2436 return false; 2437 } 2438 2439 2440 ValueObject * 2441 ValueObject::GetNonBaseClassParent() 2442 { 2443 if (GetParent()) 2444 { 2445 if (GetParent()->IsBaseClass()) 2446 return GetParent()->GetNonBaseClassParent(); 2447 else 2448 return GetParent(); 2449 } 2450 return NULL; 2451 } 2452 2453 void 2454 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat) 2455 { 2456 const bool is_deref_of_parent = IsDereferenceOfParent (); 2457 2458 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers) 2459 { 2460 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely 2461 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName. 2462 // the eHonorPointers mode is meant to produce strings in this latter format 2463 s.PutCString("*("); 2464 } 2465 2466 ValueObject* parent = GetParent(); 2467 2468 if (parent) 2469 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat); 2470 2471 // if we are a deref_of_parent just because we are synthetic array 2472 // members made up to allow ptr[%d] syntax to work in variable 2473 // printing, then add our name ([%d]) to the expression path 2474 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers) 2475 s.PutCString(m_name.AsCString()); 2476 2477 if (!IsBaseClass()) 2478 { 2479 if (!is_deref_of_parent) 2480 { 2481 ValueObject *non_base_class_parent = GetNonBaseClassParent(); 2482 if (non_base_class_parent) 2483 { 2484 ClangASTType non_base_class_parent_clang_type = non_base_class_parent->GetClangType(); 2485 if (non_base_class_parent_clang_type) 2486 { 2487 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers) 2488 { 2489 s.PutCString("->"); 2490 } 2491 else 2492 { 2493 const uint32_t non_base_class_parent_type_info = non_base_class_parent_clang_type.GetTypeInfo(); 2494 2495 if (non_base_class_parent_type_info & ClangASTType::eTypeIsPointer) 2496 { 2497 s.PutCString("->"); 2498 } 2499 else if ((non_base_class_parent_type_info & ClangASTType::eTypeHasChildren) && 2500 !(non_base_class_parent_type_info & ClangASTType::eTypeIsArray)) 2501 { 2502 s.PutChar('.'); 2503 } 2504 } 2505 } 2506 } 2507 2508 const char *name = GetName().GetCString(); 2509 if (name) 2510 { 2511 if (qualify_cxx_base_classes) 2512 { 2513 if (GetBaseClassPath (s)) 2514 s.PutCString("::"); 2515 } 2516 s.PutCString(name); 2517 } 2518 } 2519 } 2520 2521 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers) 2522 { 2523 s.PutChar(')'); 2524 } 2525 } 2526 2527 ValueObjectSP 2528 ValueObject::GetValueForExpressionPath(const char* expression, 2529 const char** first_unparsed, 2530 ExpressionPathScanEndReason* reason_to_stop, 2531 ExpressionPathEndResultType* final_value_type, 2532 const GetValueForExpressionPathOptions& options, 2533 ExpressionPathAftermath* final_task_on_target) 2534 { 2535 2536 const char* dummy_first_unparsed; 2537 ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown; 2538 ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid; 2539 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2540 2541 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression, 2542 first_unparsed ? first_unparsed : &dummy_first_unparsed, 2543 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop, 2544 final_value_type ? final_value_type : &dummy_final_value_type, 2545 options, 2546 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target); 2547 2548 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing) 2549 return ret_val; 2550 2551 if (ret_val.get() && ((final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress of plain objects 2552 { 2553 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference) 2554 { 2555 Error error; 2556 ValueObjectSP final_value = ret_val->Dereference(error); 2557 if (error.Fail() || !final_value.get()) 2558 { 2559 if (reason_to_stop) 2560 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 2561 if (final_value_type) 2562 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid; 2563 return ValueObjectSP(); 2564 } 2565 else 2566 { 2567 if (final_task_on_target) 2568 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2569 return final_value; 2570 } 2571 } 2572 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress) 2573 { 2574 Error error; 2575 ValueObjectSP final_value = ret_val->AddressOf(error); 2576 if (error.Fail() || !final_value.get()) 2577 { 2578 if (reason_to_stop) 2579 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed; 2580 if (final_value_type) 2581 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid; 2582 return ValueObjectSP(); 2583 } 2584 else 2585 { 2586 if (final_task_on_target) 2587 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2588 return final_value; 2589 } 2590 } 2591 } 2592 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it 2593 } 2594 2595 int 2596 ValueObject::GetValuesForExpressionPath(const char* expression, 2597 ValueObjectListSP& list, 2598 const char** first_unparsed, 2599 ExpressionPathScanEndReason* reason_to_stop, 2600 ExpressionPathEndResultType* final_value_type, 2601 const GetValueForExpressionPathOptions& options, 2602 ExpressionPathAftermath* final_task_on_target) 2603 { 2604 const char* dummy_first_unparsed; 2605 ExpressionPathScanEndReason dummy_reason_to_stop; 2606 ExpressionPathEndResultType dummy_final_value_type; 2607 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2608 2609 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression, 2610 first_unparsed ? first_unparsed : &dummy_first_unparsed, 2611 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop, 2612 final_value_type ? final_value_type : &dummy_final_value_type, 2613 options, 2614 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target); 2615 2616 if (!ret_val.get()) // if there are errors, I add nothing to the list 2617 return 0; 2618 2619 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet) 2620 { 2621 // I need not expand a range, just post-process the final value and return 2622 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing) 2623 { 2624 list->Append(ret_val); 2625 return 1; 2626 } 2627 if (ret_val.get() && (final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects 2628 { 2629 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference) 2630 { 2631 Error error; 2632 ValueObjectSP final_value = ret_val->Dereference(error); 2633 if (error.Fail() || !final_value.get()) 2634 { 2635 if (reason_to_stop) 2636 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 2637 if (final_value_type) 2638 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid; 2639 return 0; 2640 } 2641 else 2642 { 2643 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2644 list->Append(final_value); 2645 return 1; 2646 } 2647 } 2648 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress) 2649 { 2650 Error error; 2651 ValueObjectSP final_value = ret_val->AddressOf(error); 2652 if (error.Fail() || !final_value.get()) 2653 { 2654 if (reason_to_stop) 2655 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed; 2656 if (final_value_type) 2657 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid; 2658 return 0; 2659 } 2660 else 2661 { 2662 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing; 2663 list->Append(final_value); 2664 return 1; 2665 } 2666 } 2667 } 2668 } 2669 else 2670 { 2671 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed, 2672 first_unparsed ? first_unparsed : &dummy_first_unparsed, 2673 ret_val, 2674 list, 2675 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop, 2676 final_value_type ? final_value_type : &dummy_final_value_type, 2677 options, 2678 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target); 2679 } 2680 // in any non-covered case, just do the obviously right thing 2681 list->Append(ret_val); 2682 return 1; 2683 } 2684 2685 ValueObjectSP 2686 ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr, 2687 const char** first_unparsed, 2688 ExpressionPathScanEndReason* reason_to_stop, 2689 ExpressionPathEndResultType* final_result, 2690 const GetValueForExpressionPathOptions& options, 2691 ExpressionPathAftermath* what_next) 2692 { 2693 ValueObjectSP root = GetSP(); 2694 2695 if (!root.get()) 2696 return ValueObjectSP(); 2697 2698 *first_unparsed = expression_cstr; 2699 2700 while (true) 2701 { 2702 2703 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr 2704 2705 ClangASTType root_clang_type = root->GetClangType(); 2706 ClangASTType pointee_clang_type; 2707 Flags pointee_clang_type_info; 2708 2709 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type)); 2710 if (pointee_clang_type) 2711 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo()); 2712 2713 if (!expression_cstr || *expression_cstr == '\0') 2714 { 2715 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 2716 return root; 2717 } 2718 2719 switch (*expression_cstr) 2720 { 2721 case '-': 2722 { 2723 if (options.m_check_dot_vs_arrow_syntax && 2724 root_clang_type_info.Test(ClangASTType::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error 2725 { 2726 *first_unparsed = expression_cstr; 2727 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot; 2728 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2729 return ValueObjectSP(); 2730 } 2731 if (root_clang_type_info.Test(ClangASTType::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden 2732 root_clang_type_info.Test(ClangASTType::eTypeIsPointer) && 2733 options.m_no_fragile_ivar) 2734 { 2735 *first_unparsed = expression_cstr; 2736 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed; 2737 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2738 return ValueObjectSP(); 2739 } 2740 if (expression_cstr[1] != '>') 2741 { 2742 *first_unparsed = expression_cstr; 2743 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 2744 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2745 return ValueObjectSP(); 2746 } 2747 expression_cstr++; // skip the - 2748 } 2749 case '.': // or fallthrough from -> 2750 { 2751 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' && 2752 root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error 2753 { 2754 *first_unparsed = expression_cstr; 2755 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow; 2756 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2757 return ValueObjectSP(); 2758 } 2759 expression_cstr++; // skip . 2760 const char *next_separator = strpbrk(expression_cstr+1,"-.["); 2761 ConstString child_name; 2762 if (!next_separator) // if no other separator just expand this last layer 2763 { 2764 child_name.SetCString (expression_cstr); 2765 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true); 2766 2767 if (child_valobj_sp.get()) // we know we are done, so just return 2768 { 2769 *first_unparsed = ""; 2770 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 2771 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2772 return child_valobj_sp; 2773 } 2774 else if (options.m_no_synthetic_children == false) // let's try with synthetic children 2775 { 2776 if (root->IsSynthetic()) 2777 { 2778 *first_unparsed = expression_cstr; 2779 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2780 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2781 return ValueObjectSP(); 2782 } 2783 2784 child_valobj_sp = root->GetSyntheticValue(); 2785 if (child_valobj_sp.get()) 2786 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true); 2787 } 2788 2789 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP, 2790 // so we hit the "else" branch, and return an error 2791 if(child_valobj_sp.get()) // if it worked, just return 2792 { 2793 *first_unparsed = ""; 2794 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 2795 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2796 return child_valobj_sp; 2797 } 2798 else 2799 { 2800 *first_unparsed = expression_cstr; 2801 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2802 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2803 return ValueObjectSP(); 2804 } 2805 } 2806 else // other layers do expand 2807 { 2808 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr); 2809 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true); 2810 if (child_valobj_sp.get()) // store the new root and move on 2811 { 2812 root = child_valobj_sp; 2813 *first_unparsed = next_separator; 2814 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2815 continue; 2816 } 2817 else if (options.m_no_synthetic_children == false) // let's try with synthetic children 2818 { 2819 if (root->IsSynthetic()) 2820 { 2821 *first_unparsed = expression_cstr; 2822 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2823 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2824 return ValueObjectSP(); 2825 } 2826 2827 child_valobj_sp = root->GetSyntheticValue(true); 2828 if (child_valobj_sp) 2829 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true); 2830 } 2831 2832 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP, 2833 // so we hit the "else" branch, and return an error 2834 if(child_valobj_sp.get()) // if it worked, move on 2835 { 2836 root = child_valobj_sp; 2837 *first_unparsed = next_separator; 2838 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2839 continue; 2840 } 2841 else 2842 { 2843 *first_unparsed = expression_cstr; 2844 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2845 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2846 return ValueObjectSP(); 2847 } 2848 } 2849 break; 2850 } 2851 case '[': 2852 { 2853 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray) && !root_clang_type_info.Test(ClangASTType::eTypeIsPointer) && !root_clang_type_info.Test(ClangASTType::eTypeIsVector)) // if this is not a T[] nor a T* 2854 { 2855 if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar... 2856 { 2857 if (options.m_no_synthetic_children) // ...only chance left is synthetic 2858 { 2859 *first_unparsed = expression_cstr; 2860 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid; 2861 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2862 return ValueObjectSP(); 2863 } 2864 } 2865 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields 2866 { 2867 *first_unparsed = expression_cstr; 2868 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed; 2869 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2870 return ValueObjectSP(); 2871 } 2872 } 2873 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays 2874 { 2875 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 2876 { 2877 *first_unparsed = expression_cstr; 2878 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed; 2879 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2880 return ValueObjectSP(); 2881 } 2882 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it 2883 { 2884 *first_unparsed = expression_cstr+2; 2885 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet; 2886 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange; 2887 return root; 2888 } 2889 } 2890 const char *separator_position = ::strchr(expression_cstr+1,'-'); 2891 const char *close_bracket_position = ::strchr(expression_cstr+1,']'); 2892 if (!close_bracket_position) // if there is no ], this is a syntax error 2893 { 2894 *first_unparsed = expression_cstr; 2895 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 2896 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2897 return ValueObjectSP(); 2898 } 2899 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N] 2900 { 2901 char *end = NULL; 2902 unsigned long index = ::strtoul (expression_cstr+1, &end, 0); 2903 if (!end || end != close_bracket_position) // if something weird is in our way return an error 2904 { 2905 *first_unparsed = expression_cstr; 2906 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 2907 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2908 return ValueObjectSP(); 2909 } 2910 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays 2911 { 2912 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 2913 { 2914 *first_unparsed = expression_cstr+2; 2915 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet; 2916 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange; 2917 return root; 2918 } 2919 else 2920 { 2921 *first_unparsed = expression_cstr; 2922 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed; 2923 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2924 return ValueObjectSP(); 2925 } 2926 } 2927 // from here on we do have a valid index 2928 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 2929 { 2930 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true); 2931 if (!child_valobj_sp) 2932 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true); 2933 if (!child_valobj_sp) 2934 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index) 2935 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true); 2936 if (child_valobj_sp) 2937 { 2938 root = child_valobj_sp; 2939 *first_unparsed = end+1; // skip ] 2940 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2941 continue; 2942 } 2943 else 2944 { 2945 *first_unparsed = expression_cstr; 2946 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2947 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2948 return ValueObjectSP(); 2949 } 2950 } 2951 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) 2952 { 2953 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield 2954 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar)) 2955 { 2956 Error error; 2957 root = root->Dereference(error); 2958 if (error.Fail() || !root.get()) 2959 { 2960 *first_unparsed = expression_cstr; 2961 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 2962 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2963 return ValueObjectSP(); 2964 } 2965 else 2966 { 2967 *what_next = eExpressionPathAftermathNothing; 2968 continue; 2969 } 2970 } 2971 else 2972 { 2973 if (root->GetClangType().GetMinimumLanguage() == eLanguageTypeObjC 2974 && pointee_clang_type_info.AllClear(ClangASTType::eTypeIsPointer) 2975 && root->HasSyntheticValue() 2976 && options.m_no_synthetic_children == false) 2977 { 2978 root = root->GetSyntheticValue()->GetChildAtIndex(index, true); 2979 } 2980 else 2981 root = root->GetSyntheticArrayMemberFromPointer(index, true); 2982 if (!root.get()) 2983 { 2984 *first_unparsed = expression_cstr; 2985 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 2986 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 2987 return ValueObjectSP(); 2988 } 2989 else 2990 { 2991 *first_unparsed = end+1; // skip ] 2992 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 2993 continue; 2994 } 2995 } 2996 } 2997 else if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) 2998 { 2999 root = root->GetSyntheticBitFieldChild(index, index, true); 3000 if (!root.get()) 3001 { 3002 *first_unparsed = expression_cstr; 3003 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3004 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3005 return ValueObjectSP(); 3006 } 3007 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing 3008 { 3009 *first_unparsed = end+1; // skip ] 3010 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet; 3011 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield; 3012 return root; 3013 } 3014 } 3015 else if (root_clang_type_info.Test(ClangASTType::eTypeIsVector)) 3016 { 3017 root = root->GetChildAtIndex(index, true); 3018 if (!root.get()) 3019 { 3020 *first_unparsed = expression_cstr; 3021 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3022 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3023 return ValueObjectSP(); 3024 } 3025 else 3026 { 3027 *first_unparsed = end+1; // skip ] 3028 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 3029 continue; 3030 } 3031 } 3032 else if (options.m_no_synthetic_children == false) 3033 { 3034 if (root->HasSyntheticValue()) 3035 root = root->GetSyntheticValue(); 3036 else if (!root->IsSynthetic()) 3037 { 3038 *first_unparsed = expression_cstr; 3039 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing; 3040 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3041 return ValueObjectSP(); 3042 } 3043 // if we are here, then root itself is a synthetic VO.. should be good to go 3044 3045 if (!root.get()) 3046 { 3047 *first_unparsed = expression_cstr; 3048 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing; 3049 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3050 return ValueObjectSP(); 3051 } 3052 root = root->GetChildAtIndex(index, true); 3053 if (!root.get()) 3054 { 3055 *first_unparsed = expression_cstr; 3056 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3057 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3058 return ValueObjectSP(); 3059 } 3060 else 3061 { 3062 *first_unparsed = end+1; // skip ] 3063 *final_result = ValueObject::eExpressionPathEndResultTypePlain; 3064 continue; 3065 } 3066 } 3067 else 3068 { 3069 *first_unparsed = expression_cstr; 3070 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3071 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3072 return ValueObjectSP(); 3073 } 3074 } 3075 else // we have a low and a high index 3076 { 3077 char *end = NULL; 3078 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0); 3079 if (!end || end != separator_position) // if something weird is in our way return an error 3080 { 3081 *first_unparsed = expression_cstr; 3082 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3083 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3084 return ValueObjectSP(); 3085 } 3086 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0); 3087 if (!end || end != close_bracket_position) // if something weird is in our way return an error 3088 { 3089 *first_unparsed = expression_cstr; 3090 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3091 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3092 return ValueObjectSP(); 3093 } 3094 if (index_lower > index_higher) // swap indices if required 3095 { 3096 unsigned long temp = index_lower; 3097 index_lower = index_higher; 3098 index_higher = temp; 3099 } 3100 if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars 3101 { 3102 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true); 3103 if (!root.get()) 3104 { 3105 *first_unparsed = expression_cstr; 3106 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3107 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3108 return ValueObjectSP(); 3109 } 3110 else 3111 { 3112 *first_unparsed = end+1; // skip ] 3113 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet; 3114 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield; 3115 return root; 3116 } 3117 } 3118 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield 3119 *what_next == ValueObject::eExpressionPathAftermathDereference && 3120 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar)) 3121 { 3122 Error error; 3123 root = root->Dereference(error); 3124 if (error.Fail() || !root.get()) 3125 { 3126 *first_unparsed = expression_cstr; 3127 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 3128 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3129 return ValueObjectSP(); 3130 } 3131 else 3132 { 3133 *what_next = ValueObject::eExpressionPathAftermathNothing; 3134 continue; 3135 } 3136 } 3137 else 3138 { 3139 *first_unparsed = expression_cstr; 3140 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet; 3141 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange; 3142 return root; 3143 } 3144 } 3145 break; 3146 } 3147 default: // some non-separator is in the way 3148 { 3149 *first_unparsed = expression_cstr; 3150 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3151 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3152 return ValueObjectSP(); 3153 break; 3154 } 3155 } 3156 } 3157 } 3158 3159 int 3160 ValueObject::ExpandArraySliceExpression(const char* expression_cstr, 3161 const char** first_unparsed, 3162 ValueObjectSP root, 3163 ValueObjectListSP& list, 3164 ExpressionPathScanEndReason* reason_to_stop, 3165 ExpressionPathEndResultType* final_result, 3166 const GetValueForExpressionPathOptions& options, 3167 ExpressionPathAftermath* what_next) 3168 { 3169 if (!root.get()) 3170 return 0; 3171 3172 *first_unparsed = expression_cstr; 3173 3174 while (true) 3175 { 3176 3177 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr 3178 3179 ClangASTType root_clang_type = root->GetClangType(); 3180 ClangASTType pointee_clang_type; 3181 Flags pointee_clang_type_info; 3182 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type)); 3183 if (pointee_clang_type) 3184 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo()); 3185 3186 if (!expression_cstr || *expression_cstr == '\0') 3187 { 3188 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString; 3189 list->Append(root); 3190 return 1; 3191 } 3192 3193 switch (*expression_cstr) 3194 { 3195 case '[': 3196 { 3197 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray) && !root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if this is not a T[] nor a T* 3198 { 3199 if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong! 3200 { 3201 *first_unparsed = expression_cstr; 3202 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid; 3203 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3204 return 0; 3205 } 3206 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields 3207 { 3208 *first_unparsed = expression_cstr; 3209 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed; 3210 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3211 return 0; 3212 } 3213 } 3214 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays 3215 { 3216 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 3217 { 3218 *first_unparsed = expression_cstr; 3219 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed; 3220 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3221 return 0; 3222 } 3223 else // expand this into list 3224 { 3225 const size_t max_index = root->GetNumChildren() - 1; 3226 for (size_t index = 0; index < max_index; index++) 3227 { 3228 ValueObjectSP child = 3229 root->GetChildAtIndex(index, true); 3230 list->Append(child); 3231 } 3232 *first_unparsed = expression_cstr+2; 3233 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3234 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3235 return max_index; // tell me number of items I added to the VOList 3236 } 3237 } 3238 const char *separator_position = ::strchr(expression_cstr+1,'-'); 3239 const char *close_bracket_position = ::strchr(expression_cstr+1,']'); 3240 if (!close_bracket_position) // if there is no ], this is a syntax error 3241 { 3242 *first_unparsed = expression_cstr; 3243 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3244 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3245 return 0; 3246 } 3247 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N] 3248 { 3249 char *end = NULL; 3250 unsigned long index = ::strtoul (expression_cstr+1, &end, 0); 3251 if (!end || end != close_bracket_position) // if something weird is in our way return an error 3252 { 3253 *first_unparsed = expression_cstr; 3254 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3255 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3256 return 0; 3257 } 3258 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays 3259 { 3260 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 3261 { 3262 const size_t max_index = root->GetNumChildren() - 1; 3263 for (size_t index = 0; index < max_index; index++) 3264 { 3265 ValueObjectSP child = 3266 root->GetChildAtIndex(index, true); 3267 list->Append(child); 3268 } 3269 *first_unparsed = expression_cstr+2; 3270 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3271 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3272 return max_index; // tell me number of items I added to the VOList 3273 } 3274 else 3275 { 3276 *first_unparsed = expression_cstr; 3277 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed; 3278 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3279 return 0; 3280 } 3281 } 3282 // from here on we do have a valid index 3283 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray)) 3284 { 3285 root = root->GetChildAtIndex(index, true); 3286 if (!root.get()) 3287 { 3288 *first_unparsed = expression_cstr; 3289 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3290 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3291 return 0; 3292 } 3293 else 3294 { 3295 list->Append(root); 3296 *first_unparsed = end+1; // skip ] 3297 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3298 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3299 return 1; 3300 } 3301 } 3302 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) 3303 { 3304 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield 3305 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar)) 3306 { 3307 Error error; 3308 root = root->Dereference(error); 3309 if (error.Fail() || !root.get()) 3310 { 3311 *first_unparsed = expression_cstr; 3312 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 3313 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3314 return 0; 3315 } 3316 else 3317 { 3318 *what_next = eExpressionPathAftermathNothing; 3319 continue; 3320 } 3321 } 3322 else 3323 { 3324 root = root->GetSyntheticArrayMemberFromPointer(index, true); 3325 if (!root.get()) 3326 { 3327 *first_unparsed = expression_cstr; 3328 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3329 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3330 return 0; 3331 } 3332 else 3333 { 3334 list->Append(root); 3335 *first_unparsed = end+1; // skip ] 3336 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3337 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3338 return 1; 3339 } 3340 } 3341 } 3342 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/ 3343 { 3344 root = root->GetSyntheticBitFieldChild(index, index, true); 3345 if (!root.get()) 3346 { 3347 *first_unparsed = expression_cstr; 3348 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3349 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3350 return 0; 3351 } 3352 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing 3353 { 3354 list->Append(root); 3355 *first_unparsed = end+1; // skip ] 3356 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3357 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3358 return 1; 3359 } 3360 } 3361 } 3362 else // we have a low and a high index 3363 { 3364 char *end = NULL; 3365 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0); 3366 if (!end || end != separator_position) // if something weird is in our way return an error 3367 { 3368 *first_unparsed = expression_cstr; 3369 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3370 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3371 return 0; 3372 } 3373 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0); 3374 if (!end || end != close_bracket_position) // if something weird is in our way return an error 3375 { 3376 *first_unparsed = expression_cstr; 3377 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3378 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3379 return 0; 3380 } 3381 if (index_lower > index_higher) // swap indices if required 3382 { 3383 unsigned long temp = index_lower; 3384 index_lower = index_higher; 3385 index_higher = temp; 3386 } 3387 if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars 3388 { 3389 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true); 3390 if (!root.get()) 3391 { 3392 *first_unparsed = expression_cstr; 3393 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild; 3394 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3395 return 0; 3396 } 3397 else 3398 { 3399 list->Append(root); 3400 *first_unparsed = end+1; // skip ] 3401 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3402 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3403 return 1; 3404 } 3405 } 3406 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield 3407 *what_next == ValueObject::eExpressionPathAftermathDereference && 3408 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar)) 3409 { 3410 Error error; 3411 root = root->Dereference(error); 3412 if (error.Fail() || !root.get()) 3413 { 3414 *first_unparsed = expression_cstr; 3415 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed; 3416 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3417 return 0; 3418 } 3419 else 3420 { 3421 *what_next = ValueObject::eExpressionPathAftermathNothing; 3422 continue; 3423 } 3424 } 3425 else 3426 { 3427 for (unsigned long index = index_lower; 3428 index <= index_higher; index++) 3429 { 3430 ValueObjectSP child = 3431 root->GetChildAtIndex(index, true); 3432 list->Append(child); 3433 } 3434 *first_unparsed = end+1; 3435 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded; 3436 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList; 3437 return index_higher-index_lower+1; // tell me number of items I added to the VOList 3438 } 3439 } 3440 break; 3441 } 3442 default: // some non-[ separator, or something entirely wrong, is in the way 3443 { 3444 *first_unparsed = expression_cstr; 3445 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol; 3446 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid; 3447 return 0; 3448 break; 3449 } 3450 } 3451 } 3452 } 3453 3454 void 3455 ValueObject::LogValueObject (Log *log) 3456 { 3457 if (log) 3458 return LogValueObject (log, DumpValueObjectOptions::DefaultOptions()); 3459 } 3460 3461 void 3462 ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options) 3463 { 3464 if (log) 3465 { 3466 StreamString s; 3467 Dump (s, options); 3468 if (s.GetSize()) 3469 log->PutCString(s.GetData()); 3470 } 3471 } 3472 3473 void 3474 ValueObject::Dump (Stream &s) 3475 { 3476 3477 ValueObjectPrinter printer(this,&s,DumpValueObjectOptions::DefaultOptions()); 3478 printer.PrintValueObject(); 3479 } 3480 3481 void 3482 ValueObject::Dump (Stream &s, 3483 const DumpValueObjectOptions& options) 3484 { 3485 ValueObjectPrinter printer(this,&s,options); 3486 printer.PrintValueObject(); 3487 } 3488 3489 ValueObjectSP 3490 ValueObject::CreateConstantValue (const ConstString &name) 3491 { 3492 ValueObjectSP valobj_sp; 3493 3494 if (UpdateValueIfNeeded(false) && m_error.Success()) 3495 { 3496 ExecutionContext exe_ctx (GetExecutionContextRef()); 3497 3498 DataExtractor data; 3499 data.SetByteOrder (m_data.GetByteOrder()); 3500 data.SetAddressByteSize(m_data.GetAddressByteSize()); 3501 3502 if (IsBitfield()) 3503 { 3504 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX))); 3505 m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get()); 3506 } 3507 else 3508 m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get()); 3509 3510 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), 3511 GetClangType(), 3512 name, 3513 data, 3514 GetAddressOf()); 3515 } 3516 3517 if (!valobj_sp) 3518 { 3519 ExecutionContext exe_ctx (GetExecutionContextRef()); 3520 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), m_error); 3521 } 3522 return valobj_sp; 3523 } 3524 3525 ValueObjectSP 3526 ValueObject::Dereference (Error &error) 3527 { 3528 if (m_deref_valobj) 3529 return m_deref_valobj->GetSP(); 3530 3531 const bool is_pointer_type = IsPointerType(); 3532 if (is_pointer_type) 3533 { 3534 bool omit_empty_base_classes = true; 3535 bool ignore_array_bounds = false; 3536 3537 std::string child_name_str; 3538 uint32_t child_byte_size = 0; 3539 int32_t child_byte_offset = 0; 3540 uint32_t child_bitfield_bit_size = 0; 3541 uint32_t child_bitfield_bit_offset = 0; 3542 bool child_is_base_class = false; 3543 bool child_is_deref_of_parent = false; 3544 const bool transparent_pointers = false; 3545 ClangASTType clang_type = GetClangType(); 3546 ClangASTType child_clang_type; 3547 3548 ExecutionContext exe_ctx (GetExecutionContextRef()); 3549 3550 child_clang_type = clang_type.GetChildClangTypeAtIndex (&exe_ctx, 3551 GetName().GetCString(), 3552 0, 3553 transparent_pointers, 3554 omit_empty_base_classes, 3555 ignore_array_bounds, 3556 child_name_str, 3557 child_byte_size, 3558 child_byte_offset, 3559 child_bitfield_bit_size, 3560 child_bitfield_bit_offset, 3561 child_is_base_class, 3562 child_is_deref_of_parent); 3563 if (child_clang_type && child_byte_size) 3564 { 3565 ConstString child_name; 3566 if (!child_name_str.empty()) 3567 child_name.SetCString (child_name_str.c_str()); 3568 3569 m_deref_valobj = new ValueObjectChild (*this, 3570 child_clang_type, 3571 child_name, 3572 child_byte_size, 3573 child_byte_offset, 3574 child_bitfield_bit_size, 3575 child_bitfield_bit_offset, 3576 child_is_base_class, 3577 child_is_deref_of_parent, 3578 eAddressTypeInvalid); 3579 } 3580 } 3581 3582 if (m_deref_valobj) 3583 { 3584 error.Clear(); 3585 return m_deref_valobj->GetSP(); 3586 } 3587 else 3588 { 3589 StreamString strm; 3590 GetExpressionPath(strm, true); 3591 3592 if (is_pointer_type) 3593 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str()); 3594 else 3595 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str()); 3596 return ValueObjectSP(); 3597 } 3598 } 3599 3600 ValueObjectSP 3601 ValueObject::AddressOf (Error &error) 3602 { 3603 if (m_addr_of_valobj_sp) 3604 return m_addr_of_valobj_sp; 3605 3606 AddressType address_type = eAddressTypeInvalid; 3607 const bool scalar_is_load_address = false; 3608 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type); 3609 error.Clear(); 3610 if (addr != LLDB_INVALID_ADDRESS) 3611 { 3612 switch (address_type) 3613 { 3614 case eAddressTypeInvalid: 3615 { 3616 StreamString expr_path_strm; 3617 GetExpressionPath(expr_path_strm, true); 3618 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str()); 3619 } 3620 break; 3621 3622 case eAddressTypeFile: 3623 case eAddressTypeLoad: 3624 case eAddressTypeHost: 3625 { 3626 ClangASTType clang_type = GetClangType(); 3627 if (clang_type) 3628 { 3629 std::string name (1, '&'); 3630 name.append (m_name.AsCString("")); 3631 ExecutionContext exe_ctx (GetExecutionContextRef()); 3632 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), 3633 clang_type.GetPointerType(), 3634 ConstString (name.c_str()), 3635 addr, 3636 eAddressTypeInvalid, 3637 m_data.GetAddressByteSize()); 3638 } 3639 } 3640 break; 3641 } 3642 } 3643 else 3644 { 3645 StreamString expr_path_strm; 3646 GetExpressionPath(expr_path_strm, true); 3647 error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str()); 3648 } 3649 3650 return m_addr_of_valobj_sp; 3651 } 3652 3653 ValueObjectSP 3654 ValueObject::Cast (const ClangASTType &clang_ast_type) 3655 { 3656 return ValueObjectCast::Create (*this, GetName(), clang_ast_type); 3657 } 3658 3659 ValueObjectSP 3660 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type) 3661 { 3662 ValueObjectSP valobj_sp; 3663 AddressType address_type; 3664 addr_t ptr_value = GetPointerValue (&address_type); 3665 3666 if (ptr_value != LLDB_INVALID_ADDRESS) 3667 { 3668 Address ptr_addr (ptr_value); 3669 ExecutionContext exe_ctx (GetExecutionContextRef()); 3670 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(), 3671 name, 3672 ptr_addr, 3673 clang_ast_type); 3674 } 3675 return valobj_sp; 3676 } 3677 3678 ValueObjectSP 3679 ValueObject::CastPointerType (const char *name, TypeSP &type_sp) 3680 { 3681 ValueObjectSP valobj_sp; 3682 AddressType address_type; 3683 addr_t ptr_value = GetPointerValue (&address_type); 3684 3685 if (ptr_value != LLDB_INVALID_ADDRESS) 3686 { 3687 Address ptr_addr (ptr_value); 3688 ExecutionContext exe_ctx (GetExecutionContextRef()); 3689 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(), 3690 name, 3691 ptr_addr, 3692 type_sp); 3693 } 3694 return valobj_sp; 3695 } 3696 3697 ValueObject::EvaluationPoint::EvaluationPoint () : 3698 m_mod_id(), 3699 m_exe_ctx_ref(), 3700 m_needs_update (true), 3701 m_first_update (true) 3702 { 3703 } 3704 3705 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected): 3706 m_mod_id(), 3707 m_exe_ctx_ref(), 3708 m_needs_update (true), 3709 m_first_update (true) 3710 { 3711 ExecutionContext exe_ctx(exe_scope); 3712 TargetSP target_sp (exe_ctx.GetTargetSP()); 3713 if (target_sp) 3714 { 3715 m_exe_ctx_ref.SetTargetSP (target_sp); 3716 ProcessSP process_sp (exe_ctx.GetProcessSP()); 3717 if (!process_sp) 3718 process_sp = target_sp->GetProcessSP(); 3719 3720 if (process_sp) 3721 { 3722 m_mod_id = process_sp->GetModID(); 3723 m_exe_ctx_ref.SetProcessSP (process_sp); 3724 3725 ThreadSP thread_sp (exe_ctx.GetThreadSP()); 3726 3727 if (!thread_sp) 3728 { 3729 if (use_selected) 3730 thread_sp = process_sp->GetThreadList().GetSelectedThread(); 3731 } 3732 3733 if (thread_sp) 3734 { 3735 m_exe_ctx_ref.SetThreadSP(thread_sp); 3736 3737 StackFrameSP frame_sp (exe_ctx.GetFrameSP()); 3738 if (!frame_sp) 3739 { 3740 if (use_selected) 3741 frame_sp = thread_sp->GetSelectedFrame(); 3742 } 3743 if (frame_sp) 3744 m_exe_ctx_ref.SetFrameSP(frame_sp); 3745 } 3746 } 3747 } 3748 } 3749 3750 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) : 3751 m_mod_id(), 3752 m_exe_ctx_ref(rhs.m_exe_ctx_ref), 3753 m_needs_update (true), 3754 m_first_update (true) 3755 { 3756 } 3757 3758 ValueObject::EvaluationPoint::~EvaluationPoint () 3759 { 3760 } 3761 3762 // This function checks the EvaluationPoint against the current process state. If the current 3763 // state matches the evaluation point, or the evaluation point is already invalid, then we return 3764 // false, meaning "no change". If the current state is different, we update our state, and return 3765 // true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so 3766 // future calls to NeedsUpdate will return true. 3767 // exe_scope will be set to the current execution context scope. 3768 3769 bool 3770 ValueObject::EvaluationPoint::SyncWithProcessState() 3771 { 3772 3773 // Start with the target, if it is NULL, then we're obviously not going to get any further: 3774 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock()); 3775 3776 if (exe_ctx.GetTargetPtr() == NULL) 3777 return false; 3778 3779 // If we don't have a process nothing can change. 3780 Process *process = exe_ctx.GetProcessPtr(); 3781 if (process == NULL) 3782 return false; 3783 3784 // If our stop id is the current stop ID, nothing has changed: 3785 ProcessModID current_mod_id = process->GetModID(); 3786 3787 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared. 3788 // In either case, we aren't going to be able to sync with the process state. 3789 if (current_mod_id.GetStopID() == 0) 3790 return false; 3791 3792 bool changed = false; 3793 const bool was_valid = m_mod_id.IsValid(); 3794 if (was_valid) 3795 { 3796 if (m_mod_id == current_mod_id) 3797 { 3798 // Everything is already up to date in this object, no need to 3799 // update the execution context scope. 3800 changed = false; 3801 } 3802 else 3803 { 3804 m_mod_id = current_mod_id; 3805 m_needs_update = true; 3806 changed = true; 3807 } 3808 } 3809 3810 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated. 3811 // That way we'll be sure to return a valid exe_scope. 3812 // If we used to have a thread or a frame but can't find it anymore, then mark ourselves as invalid. 3813 3814 if (m_exe_ctx_ref.HasThreadRef()) 3815 { 3816 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP()); 3817 if (thread_sp) 3818 { 3819 if (m_exe_ctx_ref.HasFrameRef()) 3820 { 3821 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP()); 3822 if (!frame_sp) 3823 { 3824 // We used to have a frame, but now it is gone 3825 SetInvalid(); 3826 changed = was_valid; 3827 } 3828 } 3829 } 3830 else 3831 { 3832 // We used to have a thread, but now it is gone 3833 SetInvalid(); 3834 changed = was_valid; 3835 } 3836 3837 } 3838 return changed; 3839 } 3840 3841 void 3842 ValueObject::EvaluationPoint::SetUpdated () 3843 { 3844 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP()); 3845 if (process_sp) 3846 m_mod_id = process_sp->GetModID(); 3847 m_first_update = false; 3848 m_needs_update = false; 3849 } 3850 3851 3852 3853 void 3854 ValueObject::ClearUserVisibleData(uint32_t clear_mask) 3855 { 3856 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue) 3857 m_value_str.clear(); 3858 3859 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation) 3860 m_location_str.clear(); 3861 3862 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary) 3863 { 3864 m_summary_str.clear(); 3865 } 3866 3867 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription) 3868 m_object_desc_str.clear(); 3869 3870 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren) 3871 { 3872 if (m_synthetic_value) 3873 m_synthetic_value = NULL; 3874 } 3875 } 3876 3877 SymbolContextScope * 3878 ValueObject::GetSymbolContextScope() 3879 { 3880 if (m_parent) 3881 { 3882 if (!m_parent->IsPointerOrReferenceType()) 3883 return m_parent->GetSymbolContextScope(); 3884 } 3885 return NULL; 3886 } 3887 3888 lldb::ValueObjectSP 3889 ValueObject::CreateValueObjectFromExpression (const char* name, 3890 const char* expression, 3891 const ExecutionContext& exe_ctx) 3892 { 3893 lldb::ValueObjectSP retval_sp; 3894 lldb::TargetSP target_sp(exe_ctx.GetTargetSP()); 3895 if (!target_sp) 3896 return retval_sp; 3897 if (!expression || !*expression) 3898 return retval_sp; 3899 target_sp->EvaluateExpression (expression, 3900 exe_ctx.GetFrameSP().get(), 3901 retval_sp); 3902 if (retval_sp && name && *name) 3903 retval_sp->SetName(ConstString(name)); 3904 return retval_sp; 3905 } 3906 3907 lldb::ValueObjectSP 3908 ValueObject::CreateValueObjectFromAddress (const char* name, 3909 uint64_t address, 3910 const ExecutionContext& exe_ctx, 3911 ClangASTType type) 3912 { 3913 if (type) 3914 { 3915 ClangASTType pointer_type(type.GetPointerType()); 3916 if (pointer_type) 3917 { 3918 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t))); 3919 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), 3920 pointer_type, 3921 ConstString(name), 3922 buffer, 3923 lldb::endian::InlHostByteOrder(), 3924 exe_ctx.GetAddressByteSize())); 3925 if (ptr_result_valobj_sp) 3926 { 3927 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress); 3928 Error err; 3929 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err); 3930 if (ptr_result_valobj_sp && name && *name) 3931 ptr_result_valobj_sp->SetName(ConstString(name)); 3932 } 3933 return ptr_result_valobj_sp; 3934 } 3935 } 3936 return lldb::ValueObjectSP(); 3937 } 3938 3939 lldb::ValueObjectSP 3940 ValueObject::CreateValueObjectFromData (const char* name, 3941 DataExtractor& data, 3942 const ExecutionContext& exe_ctx, 3943 ClangASTType type) 3944 { 3945 lldb::ValueObjectSP new_value_sp; 3946 new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), 3947 type, 3948 ConstString(name), 3949 data, 3950 LLDB_INVALID_ADDRESS); 3951 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad); 3952 if (new_value_sp && name && *name) 3953 new_value_sp->SetName(ConstString(name)); 3954 return new_value_sp; 3955 } 3956 3957 ModuleSP 3958 ValueObject::GetModule () 3959 { 3960 ValueObject* root(GetRoot()); 3961 if (root != this) 3962 return root->GetModule(); 3963 return lldb::ModuleSP(); 3964 } 3965 3966 ValueObject* 3967 ValueObject::GetRoot () 3968 { 3969 if (m_root) 3970 return m_root; 3971 ValueObject* parent = m_parent; 3972 if (!parent) 3973 return (m_root = this); 3974 while (parent->m_parent) 3975 { 3976 if (parent->m_root) 3977 return (m_root = parent->m_root); 3978 parent = parent->m_parent; 3979 } 3980 return (m_root = parent); 3981 } 3982 3983 AddressType 3984 ValueObject::GetAddressTypeOfChildren() 3985 { 3986 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) 3987 { 3988 ValueObject* root(GetRoot()); 3989 if (root != this) 3990 return root->GetAddressTypeOfChildren(); 3991 } 3992 return m_address_type_of_ptr_or_ref_children; 3993 } 3994 3995 lldb::DynamicValueType 3996 ValueObject::GetDynamicValueType () 3997 { 3998 ValueObject* with_dv_info = this; 3999 while (with_dv_info) 4000 { 4001 if (with_dv_info->HasDynamicValueTypeInfo()) 4002 return with_dv_info->GetDynamicValueTypeImpl(); 4003 with_dv_info = with_dv_info->m_parent; 4004 } 4005 return lldb::eNoDynamicValues; 4006 } 4007 4008 lldb::Format 4009 ValueObject::GetFormat () const 4010 { 4011 const ValueObject* with_fmt_info = this; 4012 while (with_fmt_info) 4013 { 4014 if (with_fmt_info->m_format != lldb::eFormatDefault) 4015 return with_fmt_info->m_format; 4016 with_fmt_info = with_fmt_info->m_parent; 4017 } 4018 return m_format; 4019 } 4020