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