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