1 //===-- ValueObject.cpp -----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Core/Address.h" // for Address
13 #include "lldb/Core/Module.h"
14 #include "lldb/Core/Scalar.h" // for Scalar
15 #include "lldb/Core/ValueObjectCast.h"
16 #include "lldb/Core/ValueObjectChild.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Core/ValueObjectDynamicValue.h"
19 #include "lldb/Core/ValueObjectMemory.h"
20 #include "lldb/Core/ValueObjectSyntheticFilter.h"
21 #include "lldb/DataFormatters/DataVisualization.h"
22 #include "lldb/DataFormatters/DumpValueObjectOptions.h" // for DumpValueObj...
23 #include "lldb/DataFormatters/FormatManager.h" // for FormatManager
24 #include "lldb/DataFormatters/StringPrinter.h"
25 #include "lldb/DataFormatters/TypeFormat.h" // for TypeFormatImpl_F...
26 #include "lldb/DataFormatters/TypeSummary.h" // for TypeSummaryOptions
27 #include "lldb/DataFormatters/TypeValidator.h" // for TypeValidatorImp...
28 #include "lldb/DataFormatters/ValueObjectPrinter.h"
29 #include "lldb/Expression/ExpressionVariable.h" // for ExpressionVariable
30 #include "lldb/Symbol/ClangASTContext.h"
31 #include "lldb/Symbol/CompileUnit.h"
32 #include "lldb/Symbol/CompilerType.h"
33 #include "lldb/Symbol/Declaration.h" // for Declaration
34 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
35 #include "lldb/Symbol/Type.h"
36 #include "lldb/Target/ExecutionContext.h"
37 #include "lldb/Target/Language.h"
38 #include "lldb/Target/LanguageRuntime.h"
39 #include "lldb/Target/ObjCLanguageRuntime.h"
40 #include "lldb/Target/Process.h"
41 #include "lldb/Target/StackFrame.h" // for StackFrame
42 #include "lldb/Target/Target.h"
43 #include "lldb/Target/Thread.h"
44 #include "lldb/Target/ThreadList.h" // for ThreadList
45 #include "lldb/Utility/DataBuffer.h" // for DataBuffer
46 #include "lldb/Utility/DataBufferHeap.h"
47 #include "lldb/Utility/Flags.h" // for Flags
48 #include "lldb/Utility/Log.h"
49 #include "lldb/Utility/Logging.h" // for GetLogIfAllCateg...
50 #include "lldb/Utility/SharingPtr.h" // for SharingPtr
51 #include "lldb/Utility/Stream.h" // for Stream
52 #include "lldb/Utility/StreamString.h"
53 #include "lldb/lldb-private-types.h" // for RegisterInfo
55 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
57 #include <algorithm> // for min
58 #include <cstdint> // for uint32_t, uint64_t
59 #include <cstdlib> // for size_t, NULL
60 #include <memory> // for shared_ptr, oper...
61 #include <tuple> // for tie, tuple
63 #include <assert.h> // for assert
64 #include <inttypes.h> // for PRIu64, PRIx64
65 #include <stdio.h> // for snprintf
66 #include <string.h> // for memcpy, memcmp
68 namespace lldb_private {
69 class ExecutionContextScope;
71 namespace lldb_private {
72 class SymbolContextScope;
76 using namespace lldb_private;
77 using namespace lldb_utility;
79 static user_id_t g_value_obj_uid = 0;
81 //----------------------------------------------------------------------
82 // ValueObject constructor
83 //----------------------------------------------------------------------
84 ValueObject::ValueObject(ValueObject &parent)
85 : UserID(++g_value_obj_uid), // Unique identifier for every value object
86 m_parent(&parent), m_root(NULL), m_update_point(parent.GetUpdatePoint()),
87 m_name(), m_data(), m_value(), m_error(), m_value_str(),
88 m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(),
89 m_validation_result(), m_manager(parent.GetManager()), m_children(),
90 m_synthetic_children(), m_dynamic_value(NULL), m_synthetic_value(NULL),
91 m_deref_valobj(NULL), m_format(eFormatDefault),
92 m_last_format(eFormatDefault), m_last_format_mgr_revision(0),
93 m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(),
94 m_type_validator_sp(), m_user_id_of_forced_summary(),
95 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
97 m_preferred_display_language(lldb::eLanguageTypeUnknown),
98 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
99 m_children_count_valid(false), m_old_value_valid(false),
100 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
101 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
102 m_is_getting_summary(false),
103 m_did_calculate_complete_objc_class_type(false),
104 m_is_synthetic_children_generated(
105 parent.m_is_synthetic_children_generated) {
106 m_manager->ManageObject(this);
109 //----------------------------------------------------------------------
110 // ValueObject constructor
111 //----------------------------------------------------------------------
112 ValueObject::ValueObject(ExecutionContextScope *exe_scope,
113 AddressType child_ptr_or_ref_addr_type)
114 : UserID(++g_value_obj_uid), // Unique identifier for every value object
115 m_parent(NULL), m_root(NULL), m_update_point(exe_scope), m_name(),
116 m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(),
117 m_location_str(), m_summary_str(), m_object_desc_str(),
118 m_validation_result(), m_manager(), m_children(), m_synthetic_children(),
119 m_dynamic_value(NULL), m_synthetic_value(NULL), m_deref_valobj(NULL),
120 m_format(eFormatDefault), m_last_format(eFormatDefault),
121 m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(),
122 m_synthetic_children_sp(), m_type_validator_sp(),
123 m_user_id_of_forced_summary(),
124 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
126 m_preferred_display_language(lldb::eLanguageTypeUnknown),
127 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
128 m_children_count_valid(false), m_old_value_valid(false),
129 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
130 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
131 m_is_getting_summary(false),
132 m_did_calculate_complete_objc_class_type(false),
133 m_is_synthetic_children_generated(false) {
134 m_manager = new ValueObjectManager();
135 m_manager->ManageObject(this);
138 //----------------------------------------------------------------------
140 //----------------------------------------------------------------------
141 ValueObject::~ValueObject() {}
143 bool ValueObject::UpdateValueIfNeeded(bool update_format) {
145 bool did_change_formats = false;
148 did_change_formats = UpdateFormatsIfNeeded();
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
159 if (update_format && !did_change_formats)
160 ClearUserVisibleData(eClearUserVisibleDataItemsSummary |
161 eClearUserVisibleDataItemsDescription);
162 return m_error.Success();
165 bool first_update = IsChecksumEmpty();
167 if (NeedsUpdating()) {
168 m_update_point.SetUpdated();
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;
175 m_old_value_valid = true;
176 m_old_value_str.swap(m_value_str);
177 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
180 ClearUserVisibleData();
183 const bool value_was_valid = GetValueIsValid();
184 SetValueDidChange(false);
188 // Call the pure virtual function to update the value
190 bool need_compare_checksums = false;
191 llvm::SmallVector<uint8_t, 16> old_checksum;
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());
200 bool success = UpdateValue();
202 SetValueIsValid(success);
205 const uint64_t max_checksum_size = 128;
206 m_data.Checksum(m_value_checksum, max_checksum_size);
208 need_compare_checksums = false;
209 m_value_checksum.clear();
212 assert(!need_compare_checksums ||
213 (!old_checksum.empty() && !m_value_checksum.empty()));
216 SetValueDidChange(false);
217 else if (!m_value_did_change && success == false) {
218 // The value wasn't gotten successfully, so we mark this as changed if
219 // the value used to be valid and now isn't
220 SetValueDidChange(value_was_valid);
221 } else if (need_compare_checksums) {
222 SetValueDidChange(memcmp(&old_checksum[0], &m_value_checksum[0],
223 m_value_checksum.size()));
227 m_error.SetErrorString("out of scope");
230 return m_error.Success();
233 bool ValueObject::UpdateFormatsIfNeeded() {
234 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_DATAFORMATTERS));
236 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject "
237 "rev: %d - Global rev: %d",
238 GetName().GetCString(), static_cast<void *>(this),
239 m_last_format_mgr_revision,
240 DataVisualization::GetCurrentRevision());
242 bool any_change = false;
244 if ((m_last_format_mgr_revision != DataVisualization::GetCurrentRevision())) {
245 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
248 SetValueFormat(DataVisualization::GetFormat(*this, eNoDynamicValues));
250 DataVisualization::GetSummaryFormat(*this, GetDynamicValueType()));
251 #ifndef LLDB_DISABLE_PYTHON
252 SetSyntheticChildren(
253 DataVisualization::GetSyntheticChildren(*this, GetDynamicValueType()));
255 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
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);
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());
278 CompilerType ValueObject::MaybeCalculateCompleteType() {
279 CompilerType compiler_type(GetCompilerTypeImpl());
281 if (m_did_calculate_complete_objc_class_type) {
282 if (m_override_type.IsValid())
283 return m_override_type;
285 return compiler_type;
288 CompilerType class_type;
289 bool is_pointer_type = false;
291 if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type)) {
292 is_pointer_type = true;
293 } else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type)) {
294 class_type = compiler_type;
296 return compiler_type;
299 m_did_calculate_complete_objc_class_type = true;
302 ConstString class_name(class_type.GetConstTypeName());
305 ProcessSP process_sp(
306 GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
309 ObjCLanguageRuntime *objc_language_runtime(
310 process_sp->GetObjCLanguageRuntime());
312 if (objc_language_runtime) {
313 TypeSP complete_objc_class_type_sp =
314 objc_language_runtime->LookupInCompleteClassCache(class_name);
316 if (complete_objc_class_type_sp) {
317 CompilerType complete_class(
318 complete_objc_class_type_sp->GetFullCompilerType());
320 if (complete_class.GetCompleteType()) {
321 if (is_pointer_type) {
322 m_override_type = complete_class.GetPointerType();
324 m_override_type = complete_class;
327 if (m_override_type.IsValid())
328 return m_override_type;
335 return compiler_type;
338 CompilerType ValueObject::GetCompilerType() {
339 return MaybeCalculateCompleteType();
342 TypeImpl ValueObject::GetTypeImpl() { return TypeImpl(GetCompilerType()); }
344 DataExtractor &ValueObject::GetDataExtractor() {
345 UpdateValueIfNeeded(false);
349 const Status &ValueObject::GetError() {
350 UpdateValueIfNeeded(false);
354 const ConstString &ValueObject::GetName() const { return m_name; }
356 const char *ValueObject::GetLocationAsCString() {
357 return GetLocationAsCStringImpl(m_value, m_data);
360 const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
361 const DataExtractor &data) {
362 if (UpdateValueIfNeeded(false)) {
363 if (m_location_str.empty()) {
366 Value::ValueType value_type = value.GetValueType();
368 switch (value_type) {
369 case Value::eValueTypeScalar:
370 case Value::eValueTypeVector:
371 if (value.GetContextType() == Value::eContextTypeRegisterInfo) {
372 RegisterInfo *reg_info = value.GetRegisterInfo();
375 m_location_str = reg_info->name;
376 else if (reg_info->alt_name)
377 m_location_str = reg_info->alt_name;
378 if (m_location_str.empty())
379 m_location_str = (reg_info->encoding == lldb::eEncodingVector)
384 if (m_location_str.empty())
386 (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
389 case Value::eValueTypeLoadAddress:
390 case Value::eValueTypeFileAddress:
391 case Value::eValueTypeHostAddress: {
392 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
393 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size,
394 value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
395 m_location_str = sstr.GetString();
400 return m_location_str.c_str();
403 Value &ValueObject::GetValue() { return m_value; }
405 const Value &ValueObject::GetValue() const { return m_value; }
407 bool ValueObject::ResolveValue(Scalar &scalar) {
408 if (UpdateValueIfNeeded(
409 false)) // make sure that you are up to date before returning anything
411 ExecutionContext exe_ctx(GetExecutionContextRef());
412 Value tmp_value(m_value);
413 scalar = tmp_value.ResolveValue(&exe_ctx);
414 if (scalar.IsValid()) {
415 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
416 if (bitfield_bit_size)
417 return scalar.ExtractBitfield(bitfield_bit_size,
418 GetBitfieldBitOffset());
425 bool ValueObject::IsLogicalTrue(Status &error) {
426 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
427 LazyBool is_logical_true = language->IsLogicalTrue(*this, error);
428 switch (is_logical_true) {
431 return (is_logical_true == true);
432 case eLazyBoolCalculate:
439 if (!ResolveValue(scalar_value)) {
440 error.SetErrorString("failed to get a scalar result");
445 if (scalar_value.ULongLong(1) == 0)
453 bool ValueObject::GetValueIsValid() const { return m_value_is_valid; }
455 void ValueObject::SetValueIsValid(bool b) { m_value_is_valid = b; }
457 bool ValueObject::GetValueDidChange() { return m_value_did_change; }
459 void ValueObject::SetValueDidChange(bool value_changed) {
460 m_value_did_change = value_changed;
463 ValueObjectSP ValueObject::GetChildAtIndex(size_t idx, bool can_create) {
464 ValueObjectSP child_sp;
465 // We may need to update our value if we are dynamic
466 if (IsPossibleDynamicType())
467 UpdateValueIfNeeded(false);
468 if (idx < GetNumChildren()) {
469 // Check if we have already made the child value object?
470 if (can_create && !m_children.HasChildAtIndex(idx)) {
471 // No we haven't created the child at this index, so lets have our
472 // subclass do it and cache the result for quick future access.
473 m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
476 ValueObject *child = m_children.GetChildAtIndex(idx);
478 return child->GetSP();
484 ValueObject::GetChildAtIndexPath(llvm::ArrayRef<size_t> idxs,
485 size_t *index_of_error) {
486 if (idxs.size() == 0)
488 ValueObjectSP root(GetSP());
489 for (size_t idx : idxs) {
490 root = root->GetChildAtIndex(idx, true);
493 *index_of_error = idx;
500 lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
501 llvm::ArrayRef<std::pair<size_t, bool>> idxs, size_t *index_of_error) {
502 if (idxs.size() == 0)
504 ValueObjectSP root(GetSP());
505 for (std::pair<size_t, bool> idx : idxs) {
506 root = root->GetChildAtIndex(idx.first, idx.second);
509 *index_of_error = idx.first;
517 ValueObject::GetChildAtNamePath(llvm::ArrayRef<ConstString> names,
518 ConstString *name_of_error) {
519 if (names.size() == 0)
521 ValueObjectSP root(GetSP());
522 for (ConstString name : names) {
523 root = root->GetChildMemberWithName(name, true);
526 *name_of_error = name;
533 lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
534 llvm::ArrayRef<std::pair<ConstString, bool>> names,
535 ConstString *name_of_error) {
536 if (names.size() == 0)
538 ValueObjectSP root(GetSP());
539 for (std::pair<ConstString, bool> name : names) {
540 root = root->GetChildMemberWithName(name.first, name.second);
543 *name_of_error = name.first;
550 size_t ValueObject::GetIndexOfChildWithName(const ConstString &name) {
551 bool omit_empty_base_classes = true;
552 return GetCompilerType().GetIndexOfChildWithName(name.GetCString(),
553 omit_empty_base_classes);
556 ValueObjectSP ValueObject::GetChildMemberWithName(const ConstString &name,
558 // when getting a child by name, it could be buried inside some base classes
559 // (which really aren't part of the expression path), so we need a vector of
560 // indexes that can get us down to the correct child
561 ValueObjectSP child_sp;
563 // We may need to update our value if we are dynamic
564 if (IsPossibleDynamicType())
565 UpdateValueIfNeeded(false);
567 std::vector<uint32_t> child_indexes;
568 bool omit_empty_base_classes = true;
569 const size_t num_child_indexes =
570 GetCompilerType().GetIndexOfChildMemberWithName(
571 name.GetCString(), omit_empty_base_classes, child_indexes);
572 if (num_child_indexes > 0) {
573 std::vector<uint32_t>::const_iterator pos = child_indexes.begin();
574 std::vector<uint32_t>::const_iterator end = child_indexes.end();
576 child_sp = GetChildAtIndex(*pos, can_create);
577 for (++pos; pos != end; ++pos) {
579 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex(*pos, can_create));
580 child_sp = new_child_sp;
589 size_t ValueObject::GetNumChildren(uint32_t max) {
590 UpdateValueIfNeeded();
592 if (max < UINT32_MAX) {
593 if (m_children_count_valid) {
594 size_t children_count = m_children.GetChildrenCount();
595 return children_count <= max ? children_count : max;
597 return CalculateNumChildren(max);
600 if (!m_children_count_valid) {
601 SetNumChildren(CalculateNumChildren());
603 return m_children.GetChildrenCount();
606 bool ValueObject::MightHaveChildren() {
607 bool has_children = false;
608 const uint32_t type_info = GetTypeInfo();
610 if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
613 has_children = GetNumChildren() > 0;
618 // Should only be called by ValueObject::GetNumChildren()
619 void ValueObject::SetNumChildren(size_t num_children) {
620 m_children_count_valid = true;
621 m_children.SetChildrenCount(num_children);
624 void ValueObject::SetName(const ConstString &name) { m_name = name; }
626 ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
627 bool synthetic_array_member,
628 int32_t synthetic_index) {
629 ValueObject *valobj = NULL;
631 bool omit_empty_base_classes = true;
632 bool ignore_array_bounds = synthetic_array_member;
633 std::string child_name_str;
634 uint32_t child_byte_size = 0;
635 int32_t child_byte_offset = 0;
636 uint32_t child_bitfield_bit_size = 0;
637 uint32_t child_bitfield_bit_offset = 0;
638 bool child_is_base_class = false;
639 bool child_is_deref_of_parent = false;
640 uint64_t language_flags = 0;
642 const bool transparent_pointers = synthetic_array_member == false;
643 CompilerType child_compiler_type;
645 ExecutionContext exe_ctx(GetExecutionContextRef());
647 child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
648 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
649 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
650 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
651 child_is_deref_of_parent, this, language_flags);
652 if (child_compiler_type) {
654 child_byte_offset += child_byte_size * synthetic_index;
656 ConstString child_name;
657 if (!child_name_str.empty())
658 child_name.SetCString(child_name_str.c_str());
660 valobj = new ValueObjectChild(
661 *this, child_compiler_type, child_name, child_byte_size,
662 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
663 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
666 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
672 bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
673 std::string &destination,
674 lldb::LanguageType lang) {
675 return GetSummaryAsCString(summary_ptr, destination,
676 TypeSummaryOptions().SetLanguage(lang));
679 bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
680 std::string &destination,
681 const TypeSummaryOptions &options) {
684 // ideally we would like to bail out if passing NULL, but if we do so we end
685 // up not providing the summary for function pointers anymore
686 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
689 m_is_getting_summary = true;
691 TypeSummaryOptions actual_options(options);
693 if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
694 actual_options.SetLanguage(GetPreferredDisplayLanguage());
696 // this is a hot path in code and we prefer to avoid setting this string all
697 // too often also clearing out other information that we might care to see in
698 // a crash log. might be useful in very specific situations though.
699 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s.
700 Summary provider's description is %s",
701 GetTypeName().GetCString(),
702 GetName().GetCString(),
703 summary_ptr->GetDescription().c_str());*/
705 if (UpdateValueIfNeeded(false) && summary_ptr) {
706 if (HasSyntheticValue())
707 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on
708 // the synthetic children being
709 // up-to-date (e.g. ${svar%#})
710 summary_ptr->FormatObject(this, destination, actual_options);
712 m_is_getting_summary = false;
713 return !destination.empty();
716 const char *ValueObject::GetSummaryAsCString(lldb::LanguageType lang) {
717 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) {
718 TypeSummaryOptions summary_options;
719 summary_options.SetLanguage(lang);
720 GetSummaryAsCString(GetSummaryFormat().get(), m_summary_str,
723 if (m_summary_str.empty())
725 return m_summary_str.c_str();
728 bool ValueObject::GetSummaryAsCString(std::string &destination,
729 const TypeSummaryOptions &options) {
730 return GetSummaryAsCString(GetSummaryFormat().get(), destination, options);
733 bool ValueObject::IsCStringContainer(bool check_pointer) {
734 CompilerType pointee_or_element_compiler_type;
735 const Flags type_flags(GetTypeInfo(&pointee_or_element_compiler_type));
736 bool is_char_arr_ptr(type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
737 pointee_or_element_compiler_type.IsCharType());
738 if (!is_char_arr_ptr)
742 if (type_flags.Test(eTypeIsArray))
744 addr_t cstr_address = LLDB_INVALID_ADDRESS;
745 AddressType cstr_address_type = eAddressTypeInvalid;
746 cstr_address = GetAddressOf(true, &cstr_address_type);
747 return (cstr_address != LLDB_INVALID_ADDRESS);
750 size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
751 uint32_t item_count) {
752 CompilerType pointee_or_element_compiler_type;
753 const uint32_t type_info = GetTypeInfo(&pointee_or_element_compiler_type);
754 const bool is_pointer_type = type_info & eTypeIsPointer;
755 const bool is_array_type = type_info & eTypeIsArray;
756 if (!(is_pointer_type || is_array_type))
762 ExecutionContext exe_ctx(GetExecutionContextRef());
764 const uint64_t item_type_size = pointee_or_element_compiler_type.GetByteSize(
765 exe_ctx.GetBestExecutionContextScope());
766 const uint64_t bytes = item_count * item_type_size;
767 const uint64_t offset = item_idx * item_type_size;
769 if (item_idx == 0 && item_count == 1) // simply a deref
771 if (is_pointer_type) {
773 ValueObjectSP pointee_sp = Dereference(error);
774 if (error.Fail() || pointee_sp.get() == NULL)
776 return pointee_sp->GetData(data, error);
778 ValueObjectSP child_sp = GetChildAtIndex(0, true);
779 if (child_sp.get() == NULL)
782 return child_sp->GetData(data, error);
785 } else /* (items > 1) */
788 lldb_private::DataBufferHeap *heap_buf_ptr = NULL;
789 lldb::DataBufferSP data_sp(heap_buf_ptr =
790 new lldb_private::DataBufferHeap());
792 AddressType addr_type;
793 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type)
794 : GetAddressOf(true, &addr_type);
797 case eAddressTypeFile: {
798 ModuleSP module_sp(GetModule());
800 addr = addr + offset;
802 module_sp->ResolveFileAddress(addr, so_addr);
803 ExecutionContext exe_ctx(GetExecutionContextRef());
804 Target *target = exe_ctx.GetTargetPtr();
806 heap_buf_ptr->SetByteSize(bytes);
807 size_t bytes_read = target->ReadMemory(
808 so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
809 if (error.Success()) {
810 data.SetData(data_sp);
816 case eAddressTypeLoad: {
817 ExecutionContext exe_ctx(GetExecutionContextRef());
818 Process *process = exe_ctx.GetProcessPtr();
820 heap_buf_ptr->SetByteSize(bytes);
821 size_t bytes_read = process->ReadMemory(
822 addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
823 if (error.Success() || bytes_read > 0) {
824 data.SetData(data_sp);
829 case eAddressTypeHost: {
830 const uint64_t max_bytes =
831 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
832 if (max_bytes > offset) {
833 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
834 addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
835 if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
837 heap_buf_ptr->CopyData((uint8_t *)(addr + offset), bytes_read);
838 data.SetData(data_sp);
842 case eAddressTypeInvalid:
849 uint64_t ValueObject::GetData(DataExtractor &data, Status &error) {
850 UpdateValueIfNeeded(false);
851 ExecutionContext exe_ctx(GetExecutionContextRef());
852 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
854 if (m_data.GetByteSize()) {
857 return data.GetByteSize();
862 data.SetAddressByteSize(m_data.GetAddressByteSize());
863 data.SetByteOrder(m_data.GetByteOrder());
864 return data.GetByteSize();
867 bool ValueObject::SetData(DataExtractor &data, Status &error) {
869 // Make sure our value is up to date first so that our location and location
871 if (!UpdateValueIfNeeded(false)) {
872 error.SetErrorString("unable to read value");
877 const Encoding encoding = GetCompilerType().GetEncoding(count);
879 const size_t byte_size = GetByteSize();
881 Value::ValueType value_type = m_value.GetValueType();
883 switch (value_type) {
884 case Value::eValueTypeScalar: {
886 m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
888 if (!set_error.Success()) {
889 error.SetErrorStringWithFormat("unable to set scalar value: %s",
890 set_error.AsCString());
894 case Value::eValueTypeLoadAddress: {
895 // If it is a load address, then the scalar value is the storage location
896 // of the data, and we have to shove this value down to that load location.
897 ExecutionContext exe_ctx(GetExecutionContextRef());
898 Process *process = exe_ctx.GetProcessPtr();
900 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
901 size_t bytes_written = process->WriteMemory(
902 target_addr, data.GetDataStart(), byte_size, error);
903 if (!error.Success())
905 if (bytes_written != byte_size) {
906 error.SetErrorString("unable to write value to memory");
911 case Value::eValueTypeHostAddress: {
912 // If it is a host address, then we stuff the scalar as a DataBuffer into
914 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
915 m_data.SetData(buffer_sp, 0);
916 data.CopyByteOrderedData(0, byte_size,
917 const_cast<uint8_t *>(m_data.GetDataStart()),
918 byte_size, m_data.GetByteOrder());
919 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
921 case Value::eValueTypeFileAddress:
922 case Value::eValueTypeVector:
926 // If we have reached this point, then we have successfully changed the
932 static bool CopyStringDataToBufferSP(const StreamString &source,
933 lldb::DataBufferSP &destination) {
934 destination.reset(new DataBufferHeap(source.GetSize() + 1, 0));
935 memcpy(destination->GetBytes(), source.GetString().data(), source.GetSize());
939 std::pair<size_t, bool>
940 ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Status &error,
941 uint32_t max_length, bool honor_array,
942 Format item_format) {
943 bool was_capped = false;
945 ExecutionContext exe_ctx(GetExecutionContextRef());
946 Target *target = exe_ctx.GetTargetPtr();
949 s << "<no target to read from>";
950 error.SetErrorString("no target to read from");
951 CopyStringDataToBufferSP(s, buffer_sp);
952 return {0, was_capped};
956 max_length = target->GetMaximumSizeOfStringSummary();
958 size_t bytes_read = 0;
959 size_t total_bytes_read = 0;
961 CompilerType compiler_type = GetCompilerType();
962 CompilerType elem_or_pointee_compiler_type;
963 const Flags type_flags(GetTypeInfo(&elem_or_pointee_compiler_type));
964 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
965 elem_or_pointee_compiler_type.IsCharType()) {
966 addr_t cstr_address = LLDB_INVALID_ADDRESS;
967 AddressType cstr_address_type = eAddressTypeInvalid;
970 bool capped_data = false;
971 const bool is_array = type_flags.Test(eTypeIsArray);
974 uint64_t array_size = 0;
975 if (compiler_type.IsArrayType(NULL, &array_size, NULL)) {
976 cstr_len = array_size;
977 if (cstr_len > max_length) {
979 cstr_len = max_length;
982 cstr_address = GetAddressOf(true, &cstr_address_type);
985 cstr_address = GetPointerValue(&cstr_address_type);
988 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) {
989 if (cstr_address_type == eAddressTypeHost && is_array) {
990 const char *cstr = GetDataExtractor().PeekCStr(0);
991 if (cstr == nullptr) {
992 s << "<invalid address>";
993 error.SetErrorString("invalid address");
994 CopyStringDataToBufferSP(s, buffer_sp);
995 return {0, was_capped};
997 buffer_sp.reset(new DataBufferHeap(cstr_len, 0));
998 memcpy(buffer_sp->GetBytes(), cstr, cstr_len);
999 return {cstr_len, was_capped};
1001 s << "<invalid address>";
1002 error.SetErrorString("invalid address");
1003 CopyStringDataToBufferSP(s, buffer_sp);
1004 return {0, was_capped};
1008 Address cstr_so_addr(cstr_address);
1010 if (cstr_len > 0 && honor_array) {
1011 // I am using GetPointeeData() here to abstract the fact that some
1012 // ValueObjects are actually frozen pointers in the host but the pointed-
1013 // to data lives in the debuggee, and GetPointeeData() automatically
1014 // takes care of this
1015 GetPointeeData(data, 0, cstr_len);
1017 if ((bytes_read = data.GetByteSize()) > 0) {
1018 total_bytes_read = bytes_read;
1019 for (size_t offset = 0; offset < bytes_read; offset++)
1020 s.Printf("%c", *data.PeekData(offset, 1));
1025 cstr_len = max_length;
1026 const size_t k_max_buf_size = 64;
1030 int cstr_len_displayed = -1;
1031 bool capped_cstr = false;
1032 // I am using GetPointeeData() here to abstract the fact that some
1033 // ValueObjects are actually frozen pointers in the host but the pointed-
1034 // to data lives in the debuggee, and GetPointeeData() automatically
1035 // takes care of this
1036 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) {
1037 total_bytes_read += bytes_read;
1038 const char *cstr = data.PeekCStr(0);
1039 size_t len = strnlen(cstr, k_max_buf_size);
1040 if (cstr_len_displayed < 0)
1041 cstr_len_displayed = len;
1045 cstr_len_displayed += len;
1046 if (len > bytes_read)
1051 for (size_t offset = 0; offset < bytes_read; offset++)
1052 s.Printf("%c", *data.PeekData(offset, 1));
1054 if (len < k_max_buf_size)
1057 if (len >= cstr_len) {
1066 if (cstr_len_displayed >= 0) {
1072 error.SetErrorString("not a string object");
1073 s << "<not a string object>";
1075 CopyStringDataToBufferSP(s, buffer_sp);
1076 return {total_bytes_read, was_capped};
1079 std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() {
1080 if (!UpdateValueIfNeeded(true))
1081 return {TypeValidatorResult::Success,
1082 ""}; // not the validator's job to discuss update problems
1084 if (m_validation_result.hasValue())
1085 return m_validation_result.getValue();
1087 if (!m_type_validator_sp)
1088 return {TypeValidatorResult::Success, ""}; // no validator no failure
1090 auto outcome = m_type_validator_sp->FormatObject(this);
1092 return (m_validation_result = {outcome.m_result, outcome.m_message})
1096 const char *ValueObject::GetObjectDescription() {
1098 if (!UpdateValueIfNeeded(true))
1101 if (!m_object_desc_str.empty())
1102 return m_object_desc_str.c_str();
1104 ExecutionContext exe_ctx(GetExecutionContextRef());
1105 Process *process = exe_ctx.GetProcessPtr();
1106 if (process == NULL)
1111 LanguageType language = GetObjectRuntimeLanguage();
1112 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1114 if (runtime == NULL) {
1115 // Aw, hell, if the things a pointer, or even just an integer, let's try
1117 CompilerType compiler_type = GetCompilerType();
1118 if (compiler_type) {
1120 if (compiler_type.IsIntegerType(is_signed) ||
1121 compiler_type.IsPointerType()) {
1122 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1127 if (runtime && runtime->GetObjectDescription(s, *this)) {
1128 m_object_desc_str.append(s.GetString());
1131 if (m_object_desc_str.empty())
1134 return m_object_desc_str.c_str();
1137 bool ValueObject::GetValueAsCString(const lldb_private::TypeFormatImpl &format,
1138 std::string &destination) {
1139 if (UpdateValueIfNeeded(false))
1140 return format.FormatObject(this, destination);
1145 bool ValueObject::GetValueAsCString(lldb::Format format,
1146 std::string &destination) {
1147 return GetValueAsCString(TypeFormatImpl_Format(format), destination);
1150 const char *ValueObject::GetValueAsCString() {
1151 if (UpdateValueIfNeeded(true)) {
1152 lldb::TypeFormatImplSP format_sp;
1153 lldb::Format my_format = GetFormat();
1154 if (my_format == lldb::eFormatDefault) {
1155 if (m_type_format_sp)
1156 format_sp = m_type_format_sp;
1158 if (m_is_bitfield_for_scalar)
1159 my_format = eFormatUnsigned;
1161 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) {
1162 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1164 my_format = reg_info->format;
1166 my_format = GetValue().GetCompilerType().GetFormat();
1171 if (my_format != m_last_format || m_value_str.empty()) {
1172 m_last_format = my_format;
1174 format_sp.reset(new TypeFormatImpl_Format(my_format));
1175 if (GetValueAsCString(*format_sp.get(), m_value_str)) {
1176 if (!m_value_did_change && m_old_value_valid) {
1177 // The value was gotten successfully, so we consider the value as
1178 // changed if the value string differs
1179 SetValueDidChange(m_old_value_str != m_value_str);
1184 if (m_value_str.empty())
1186 return m_value_str.c_str();
1189 // if > 8bytes, 0 is returned. this method should mostly be used to read
1190 // address values out of pointers
1191 uint64_t ValueObject::GetValueAsUnsigned(uint64_t fail_value, bool *success) {
1192 // If our byte size is zero this is an aggregate type that has children
1193 if (CanProvideValue()) {
1195 if (ResolveValue(scalar)) {
1198 return scalar.ULongLong(fail_value);
1200 // fallthrough, otherwise...
1208 int64_t ValueObject::GetValueAsSigned(int64_t fail_value, bool *success) {
1209 // If our byte size is zero this is an aggregate type that has children
1210 if (CanProvideValue()) {
1212 if (ResolveValue(scalar)) {
1215 return scalar.SLongLong(fail_value);
1217 // fallthrough, otherwise...
1225 // if any more "special cases" are added to
1226 // ValueObject::DumpPrintableRepresentation() please keep this call up to date
1227 // by returning true for your new special cases. We will eventually move to
1228 // checking this call result before trying to display special cases
1229 bool ValueObject::HasSpecialPrintableRepresentation(
1230 ValueObjectRepresentationStyle val_obj_display, Format custom_format) {
1231 Flags flags(GetTypeInfo());
1232 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1233 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1234 if (IsCStringContainer(true) &&
1235 (custom_format == eFormatCString || custom_format == eFormatCharArray ||
1236 custom_format == eFormatChar || custom_format == eFormatVectorOfChar))
1239 if (flags.Test(eTypeIsArray)) {
1240 if ((custom_format == eFormatBytes) ||
1241 (custom_format == eFormatBytesWithASCII))
1244 if ((custom_format == eFormatVectorOfChar) ||
1245 (custom_format == eFormatVectorOfFloat32) ||
1246 (custom_format == eFormatVectorOfFloat64) ||
1247 (custom_format == eFormatVectorOfSInt16) ||
1248 (custom_format == eFormatVectorOfSInt32) ||
1249 (custom_format == eFormatVectorOfSInt64) ||
1250 (custom_format == eFormatVectorOfSInt8) ||
1251 (custom_format == eFormatVectorOfUInt128) ||
1252 (custom_format == eFormatVectorOfUInt16) ||
1253 (custom_format == eFormatVectorOfUInt32) ||
1254 (custom_format == eFormatVectorOfUInt64) ||
1255 (custom_format == eFormatVectorOfUInt8))
1262 bool ValueObject::DumpPrintableRepresentation(
1263 Stream &s, ValueObjectRepresentationStyle val_obj_display,
1264 Format custom_format, PrintableRepresentationSpecialCases special,
1265 bool do_dump_error) {
1267 Flags flags(GetTypeInfo());
1269 bool allow_special =
1270 (special == ValueObject::PrintableRepresentationSpecialCases::eAllow);
1271 const bool only_special = false;
1273 if (allow_special) {
1274 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1275 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1276 // when being asked to get a printable display an array or pointer type
1277 // directly, try to "do the right thing"
1279 if (IsCStringContainer(true) &&
1280 (custom_format == eFormatCString ||
1281 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1283 eFormatVectorOfChar)) // print char[] & char* directly
1286 lldb::DataBufferSP buffer_sp;
1287 std::pair<size_t, bool> read_string = ReadPointedString(
1288 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1289 (custom_format == eFormatCharArray));
1290 lldb_private::formatters::StringPrinter::
1291 ReadBufferAndDumpToStreamOptions options(*this);
1292 options.SetData(DataExtractor(
1293 buffer_sp, lldb::eByteOrderInvalid,
1294 8)); // none of this matters for a string - pass some defaults
1295 options.SetStream(&s);
1296 options.SetPrefixToken(0);
1297 options.SetQuote('"');
1298 options.SetSourceSize(buffer_sp->GetByteSize());
1299 options.SetIsTruncated(read_string.second);
1300 formatters::StringPrinter::ReadBufferAndDumpToStream<
1301 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1303 return !error.Fail();
1306 if (custom_format == eFormatEnum)
1309 // this only works for arrays, because I have no way to know when the
1310 // pointed memory ends, and no special \0 end of data marker
1311 if (flags.Test(eTypeIsArray)) {
1312 if ((custom_format == eFormatBytes) ||
1313 (custom_format == eFormatBytesWithASCII)) {
1314 const size_t count = GetNumChildren();
1317 for (size_t low = 0; low < count; low++) {
1322 ValueObjectSP child = GetChildAtIndex(low, true);
1324 s << "<invalid child>";
1327 child->DumpPrintableRepresentation(
1328 s, ValueObject::eValueObjectRepresentationStyleValue,
1337 if ((custom_format == eFormatVectorOfChar) ||
1338 (custom_format == eFormatVectorOfFloat32) ||
1339 (custom_format == eFormatVectorOfFloat64) ||
1340 (custom_format == eFormatVectorOfSInt16) ||
1341 (custom_format == eFormatVectorOfSInt32) ||
1342 (custom_format == eFormatVectorOfSInt64) ||
1343 (custom_format == eFormatVectorOfSInt8) ||
1344 (custom_format == eFormatVectorOfUInt128) ||
1345 (custom_format == eFormatVectorOfUInt16) ||
1346 (custom_format == eFormatVectorOfUInt32) ||
1347 (custom_format == eFormatVectorOfUInt64) ||
1348 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1349 // with ASCII or any vector
1350 // format should be printed
1353 const size_t count = GetNumChildren();
1355 Format format = FormatManager::GetSingleItemFormat(custom_format);
1358 for (size_t low = 0; low < count; low++) {
1363 ValueObjectSP child = GetChildAtIndex(low, true);
1365 s << "<invalid child>";
1368 child->DumpPrintableRepresentation(
1369 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1378 if ((custom_format == eFormatBoolean) ||
1379 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1380 (custom_format == eFormatCharPrintable) ||
1381 (custom_format == eFormatComplexFloat) ||
1382 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1383 (custom_format == eFormatHexUppercase) ||
1384 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1385 (custom_format == eFormatOSType) ||
1386 (custom_format == eFormatUnicode16) ||
1387 (custom_format == eFormatUnicode32) ||
1388 (custom_format == eFormatUnsigned) ||
1389 (custom_format == eFormatPointer) ||
1390 (custom_format == eFormatComplexInteger) ||
1391 (custom_format == eFormatComplex) ||
1392 (custom_format == eFormatDefault)) // use the [] operator
1400 bool var_success = false;
1403 llvm::StringRef str;
1405 // this is a local stream that we are using to ensure that the data pointed
1406 // to by cstr survives long enough for us to copy it to its destination -
1407 // it is necessary to have this temporary storage area for cases where our
1408 // desired output is not backed by some other longer-term storage
1411 if (custom_format != eFormatInvalid)
1412 SetFormat(custom_format);
1414 switch (val_obj_display) {
1415 case eValueObjectRepresentationStyleValue:
1416 str = GetValueAsCString();
1419 case eValueObjectRepresentationStyleSummary:
1420 str = GetSummaryAsCString();
1423 case eValueObjectRepresentationStyleLanguageSpecific:
1424 str = GetObjectDescription();
1427 case eValueObjectRepresentationStyleLocation:
1428 str = GetLocationAsCString();
1431 case eValueObjectRepresentationStyleChildrenCount:
1432 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1433 str = strm.GetString();
1436 case eValueObjectRepresentationStyleType:
1437 str = GetTypeName().GetStringRef();
1440 case eValueObjectRepresentationStyleName:
1441 str = GetName().GetStringRef();
1444 case eValueObjectRepresentationStyleExpressionPath:
1445 GetExpressionPath(strm, false);
1446 str = strm.GetString();
1451 if (val_obj_display == eValueObjectRepresentationStyleValue)
1452 str = GetSummaryAsCString();
1453 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1454 if (!CanProvideValue()) {
1455 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1456 GetLocationAsCString());
1457 str = strm.GetString();
1459 str = GetValueAsCString();
1466 if (m_error.Fail()) {
1468 s.Printf("<%s>", m_error.AsCString());
1471 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1472 s.PutCString("<no summary available>");
1473 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1474 s.PutCString("<no value available>");
1475 else if (val_obj_display ==
1476 eValueObjectRepresentationStyleLanguageSpecific)
1477 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1478 // have other runtimes
1482 s.PutCString("<no printable representation>");
1485 // we should only return false here if we could not do *anything* even if
1486 // we have an error message as output, that's a success from our callers'
1487 // perspective, so return true
1490 if (custom_format != eFormatInvalid)
1491 SetFormat(eFormatDefault);
1497 addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1498 AddressType *address_type) {
1499 // Can't take address of a bitfield
1501 return LLDB_INVALID_ADDRESS;
1503 if (!UpdateValueIfNeeded(false))
1504 return LLDB_INVALID_ADDRESS;
1506 switch (m_value.GetValueType()) {
1507 case Value::eValueTypeScalar:
1508 case Value::eValueTypeVector:
1509 if (scalar_is_load_address) {
1511 *address_type = eAddressTypeLoad;
1512 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1516 case Value::eValueTypeLoadAddress:
1517 case Value::eValueTypeFileAddress: {
1519 *address_type = m_value.GetValueAddressType();
1520 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1522 case Value::eValueTypeHostAddress: {
1524 *address_type = m_value.GetValueAddressType();
1525 return LLDB_INVALID_ADDRESS;
1529 *address_type = eAddressTypeInvalid;
1530 return LLDB_INVALID_ADDRESS;
1533 addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1534 addr_t address = LLDB_INVALID_ADDRESS;
1536 *address_type = eAddressTypeInvalid;
1538 if (!UpdateValueIfNeeded(false))
1541 switch (m_value.GetValueType()) {
1542 case Value::eValueTypeScalar:
1543 case Value::eValueTypeVector:
1544 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1547 case Value::eValueTypeHostAddress:
1548 case Value::eValueTypeLoadAddress:
1549 case Value::eValueTypeFileAddress: {
1550 lldb::offset_t data_offset = 0;
1551 address = m_data.GetPointer(&data_offset);
1556 *address_type = GetAddressTypeOfChildren();
1561 bool ValueObject::SetValueFromCString(const char *value_str, Status &error) {
1563 // Make sure our value is up to date first so that our location and location
1565 if (!UpdateValueIfNeeded(false)) {
1566 error.SetErrorString("unable to read value");
1571 const Encoding encoding = GetCompilerType().GetEncoding(count);
1573 const size_t byte_size = GetByteSize();
1575 Value::ValueType value_type = m_value.GetValueType();
1577 if (value_type == Value::eValueTypeScalar) {
1578 // If the value is already a scalar, then let the scalar change itself:
1579 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1580 } else if (byte_size <= 16) {
1581 // If the value fits in a scalar, then make a new scalar and again let the
1582 // scalar code do the conversion, then figure out where to put the new
1585 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1586 if (error.Success()) {
1587 switch (value_type) {
1588 case Value::eValueTypeLoadAddress: {
1589 // If it is a load address, then the scalar value is the storage
1590 // location of the data, and we have to shove this value down to that
1592 ExecutionContext exe_ctx(GetExecutionContextRef());
1593 Process *process = exe_ctx.GetProcessPtr();
1595 addr_t target_addr =
1596 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1597 size_t bytes_written = process->WriteScalarToMemory(
1598 target_addr, new_scalar, byte_size, error);
1599 if (!error.Success())
1601 if (bytes_written != byte_size) {
1602 error.SetErrorString("unable to write value to memory");
1607 case Value::eValueTypeHostAddress: {
1608 // If it is a host address, then we stuff the scalar as a DataBuffer
1609 // into the Value's data.
1610 DataExtractor new_data;
1611 new_data.SetByteOrder(m_data.GetByteOrder());
1613 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1614 m_data.SetData(buffer_sp, 0);
1615 bool success = new_scalar.GetData(new_data);
1617 new_data.CopyByteOrderedData(
1618 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1619 byte_size, m_data.GetByteOrder());
1621 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1624 case Value::eValueTypeFileAddress:
1625 case Value::eValueTypeScalar:
1626 case Value::eValueTypeVector:
1633 // We don't support setting things bigger than a scalar at present.
1634 error.SetErrorString("unable to write aggregate data type");
1638 // If we have reached this point, then we have successfully changed the
1644 bool ValueObject::GetDeclaration(Declaration &decl) {
1649 ConstString ValueObject::GetTypeName() {
1650 return GetCompilerType().GetConstTypeName();
1653 ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1655 ConstString ValueObject::GetQualifiedTypeName() {
1656 return GetCompilerType().GetConstQualifiedTypeName();
1659 LanguageType ValueObject::GetObjectRuntimeLanguage() {
1660 return GetCompilerType().GetMinimumLanguage();
1663 void ValueObject::AddSyntheticChild(const ConstString &key,
1664 ValueObject *valobj) {
1665 m_synthetic_children[key] = valobj;
1668 ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1669 ValueObjectSP synthetic_child_sp;
1670 std::map<ConstString, ValueObject *>::const_iterator pos =
1671 m_synthetic_children.find(key);
1672 if (pos != m_synthetic_children.end())
1673 synthetic_child_sp = pos->second->GetSP();
1674 return synthetic_child_sp;
1678 ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1679 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
1682 bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1684 bool ValueObject::IsArrayType() {
1685 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
1688 bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1690 bool ValueObject::IsIntegerType(bool &is_signed) {
1691 return GetCompilerType().IsIntegerType(is_signed);
1694 bool ValueObject::IsPointerOrReferenceType() {
1695 return GetCompilerType().IsPointerOrReferenceType();
1698 bool ValueObject::IsPossibleDynamicType() {
1699 ExecutionContext exe_ctx(GetExecutionContextRef());
1700 Process *process = exe_ctx.GetProcessPtr();
1702 return process->IsPossibleDynamicValue(*this);
1704 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
1707 bool ValueObject::IsRuntimeSupportValue() {
1708 Process *process(GetProcessSP().get());
1710 LanguageRuntime *runtime =
1711 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1713 runtime = process->GetObjCLanguageRuntime();
1715 return runtime->IsRuntimeSupportValue(*this);
1720 bool ValueObject::IsNilReference() {
1721 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1722 return language->IsNilReference(*this);
1727 bool ValueObject::IsUninitializedReference() {
1728 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1729 return language->IsUninitializedReference(*this);
1734 // This allows you to create an array member using and index that doesn't not
1735 // fall in the normal bounds of the array. Many times structure can be defined
1736 // as: struct Collection {
1737 // uint32_t item_count;
1738 // Item item_array[0];
1740 // The size of the "item_array" is 1, but many times in practice there are more
1741 // items in "item_array".
1743 ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1745 ValueObjectSP synthetic_child_sp;
1746 if (IsPointerType() || IsArrayType()) {
1748 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1749 ConstString index_const_str(index_str);
1750 // Check if we have already created a synthetic array member in this valid
1751 // object. If we have we will re-use it.
1752 synthetic_child_sp = GetSyntheticChild(index_const_str);
1753 if (!synthetic_child_sp) {
1754 ValueObject *synthetic_child;
1755 // We haven't made a synthetic array member for INDEX yet, so lets make
1756 // one and cache it for any future reference.
1757 synthetic_child = CreateChildAtIndex(0, true, index);
1759 // Cache the value if we got one back...
1760 if (synthetic_child) {
1761 AddSyntheticChild(index_const_str, synthetic_child);
1762 synthetic_child_sp = synthetic_child->GetSP();
1763 synthetic_child_sp->SetName(ConstString(index_str));
1764 synthetic_child_sp->m_is_array_item_for_pointer = true;
1768 return synthetic_child_sp;
1771 ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1773 ValueObjectSP synthetic_child_sp;
1774 if (IsScalarType()) {
1776 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1777 ConstString index_const_str(index_str);
1778 // Check if we have already created a synthetic array member in this valid
1779 // object. If we have we will re-use it.
1780 synthetic_child_sp = GetSyntheticChild(index_const_str);
1781 if (!synthetic_child_sp) {
1782 uint32_t bit_field_size = to - from + 1;
1783 uint32_t bit_field_offset = from;
1784 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1786 GetByteSize() * 8 - bit_field_size - bit_field_offset;
1787 // We haven't made a synthetic array member for INDEX yet, so lets make
1788 // one and cache it for any future reference.
1789 ValueObjectChild *synthetic_child = new ValueObjectChild(
1790 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1791 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1794 // Cache the value if we got one back...
1795 if (synthetic_child) {
1796 AddSyntheticChild(index_const_str, synthetic_child);
1797 synthetic_child_sp = synthetic_child->GetSP();
1798 synthetic_child_sp->SetName(ConstString(index_str));
1799 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1803 return synthetic_child_sp;
1806 ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1807 uint32_t offset, const CompilerType &type, bool can_create,
1808 ConstString name_const_str) {
1810 ValueObjectSP synthetic_child_sp;
1812 if (name_const_str.IsEmpty()) {
1814 snprintf(name_str, sizeof(name_str), "@%i", offset);
1815 name_const_str.SetCString(name_str);
1818 // Check if we have already created a synthetic array member in this valid
1819 // object. If we have we will re-use it.
1820 synthetic_child_sp = GetSyntheticChild(name_const_str);
1822 if (synthetic_child_sp.get())
1823 return synthetic_child_sp;
1826 return ValueObjectSP();
1828 ExecutionContext exe_ctx(GetExecutionContextRef());
1830 ValueObjectChild *synthetic_child = new ValueObjectChild(
1831 *this, type, name_const_str,
1832 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1833 false, false, eAddressTypeInvalid, 0);
1834 if (synthetic_child) {
1835 AddSyntheticChild(name_const_str, synthetic_child);
1836 synthetic_child_sp = synthetic_child->GetSP();
1837 synthetic_child_sp->SetName(name_const_str);
1838 synthetic_child_sp->m_is_child_at_offset = true;
1840 return synthetic_child_sp;
1843 ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1844 const CompilerType &type,
1846 ConstString name_const_str) {
1847 ValueObjectSP synthetic_child_sp;
1849 if (name_const_str.IsEmpty()) {
1851 snprintf(name_str, sizeof(name_str), "base%s@%i",
1852 type.GetTypeName().AsCString("<unknown>"), offset);
1853 name_const_str.SetCString(name_str);
1856 // Check if we have already created a synthetic array member in this valid
1857 // object. If we have we will re-use it.
1858 synthetic_child_sp = GetSyntheticChild(name_const_str);
1860 if (synthetic_child_sp.get())
1861 return synthetic_child_sp;
1864 return ValueObjectSP();
1866 const bool is_base_class = true;
1868 ExecutionContext exe_ctx(GetExecutionContextRef());
1870 ValueObjectChild *synthetic_child = new ValueObjectChild(
1871 *this, type, name_const_str,
1872 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1873 is_base_class, false, eAddressTypeInvalid, 0);
1874 if (synthetic_child) {
1875 AddSyntheticChild(name_const_str, synthetic_child);
1876 synthetic_child_sp = synthetic_child->GetSP();
1877 synthetic_child_sp->SetName(name_const_str);
1879 return synthetic_child_sp;
1882 // your expression path needs to have a leading . or -> (unless it somehow
1883 // "looks like" an array, in which case it has a leading [ symbol). while the [
1884 // is meaningful and should be shown to the user, . and -> are just parser
1885 // design, but by no means added information for the user.. strip them off
1886 static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1887 if (!expression || !expression[0])
1889 if (expression[0] == '.')
1890 return expression + 1;
1891 if (expression[0] == '-' && expression[1] == '>')
1892 return expression + 2;
1897 ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1899 ValueObjectSP synthetic_child_sp;
1900 ConstString name_const_string(expression);
1901 // Check if we have already created a synthetic array member in this valid
1902 // object. If we have we will re-use it.
1903 synthetic_child_sp = GetSyntheticChild(name_const_string);
1904 if (!synthetic_child_sp) {
1905 // We haven't made a synthetic array member for expression yet, so lets
1906 // make one and cache it for any future reference.
1907 synthetic_child_sp = GetValueForExpressionPath(
1908 expression, NULL, NULL,
1909 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1910 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1913 // Cache the value if we got one back...
1914 if (synthetic_child_sp.get()) {
1915 // FIXME: this causes a "real" child to end up with its name changed to
1916 // the contents of expression
1917 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1918 synthetic_child_sp->SetName(
1919 ConstString(SkipLeadingExpressionPathSeparators(expression)));
1922 return synthetic_child_sp;
1925 void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
1926 if (use_synthetic == false)
1929 TargetSP target_sp(GetTargetSP());
1930 if (target_sp && target_sp->GetEnableSyntheticValue() == false) {
1931 m_synthetic_value = NULL;
1935 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1937 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1940 if (m_synthetic_children_sp.get() == NULL)
1943 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1946 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1949 void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1950 if (use_dynamic == eNoDynamicValues)
1953 if (!m_dynamic_value && !IsDynamic()) {
1954 ExecutionContext exe_ctx(GetExecutionContextRef());
1955 Process *process = exe_ctx.GetProcessPtr();
1956 if (process && process->IsPossibleDynamicValue(*this)) {
1957 ClearDynamicTypeInformation();
1958 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
1963 ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1964 if (use_dynamic == eNoDynamicValues)
1965 return ValueObjectSP();
1967 if (!IsDynamic() && m_dynamic_value == NULL) {
1968 CalculateDynamicValue(use_dynamic);
1970 if (m_dynamic_value)
1971 return m_dynamic_value->GetSP();
1973 return ValueObjectSP();
1976 ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1978 lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1980 ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
1981 if (use_synthetic == false)
1982 return ValueObjectSP();
1984 CalculateSyntheticValue(use_synthetic);
1986 if (m_synthetic_value)
1987 return m_synthetic_value->GetSP();
1989 return ValueObjectSP();
1992 bool ValueObject::HasSyntheticValue() {
1993 UpdateFormatsIfNeeded();
1995 if (m_synthetic_children_sp.get() == NULL)
1998 CalculateSyntheticValue(true);
2000 if (m_synthetic_value)
2006 bool ValueObject::GetBaseClassPath(Stream &s) {
2007 if (IsBaseClass()) {
2008 bool parent_had_base_class =
2009 GetParent() && GetParent()->GetBaseClassPath(s);
2010 CompilerType compiler_type = GetCompilerType();
2011 std::string cxx_class_name;
2012 bool this_had_base_class =
2013 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2014 if (this_had_base_class) {
2015 if (parent_had_base_class)
2017 s.PutCString(cxx_class_name);
2019 return parent_had_base_class || this_had_base_class;
2024 ValueObject *ValueObject::GetNonBaseClassParent() {
2026 if (GetParent()->IsBaseClass())
2027 return GetParent()->GetNonBaseClassParent();
2034 bool ValueObject::IsBaseClass(uint32_t &depth) {
2035 if (!IsBaseClass()) {
2040 GetParent()->IsBaseClass(depth);
2044 // TODO: a base of no parent? weird..
2049 void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2050 GetExpressionPathFormat epformat) {
2051 // synthetic children do not actually "exist" as part of the hierarchy, and
2052 // sometimes they are consed up in ways that don't make sense from an
2053 // underlying language/API standpoint. So, use a special code path here to
2054 // return something that can hopefully be used in expression
2055 if (m_is_synthetic_children_generated) {
2056 UpdateValueIfNeeded();
2058 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2059 if (IsPointerOrReferenceType()) {
2060 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2061 GetValueAsUnsigned(0));
2064 uint64_t load_addr =
2065 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2066 if (load_addr != LLDB_INVALID_ADDRESS) {
2067 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2074 if (CanProvideValue()) {
2075 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2076 GetValueAsCString());
2083 const bool is_deref_of_parent = IsDereferenceOfParent();
2085 if (is_deref_of_parent &&
2086 epformat == eGetExpressionPathFormatDereferencePointers) {
2087 // this is the original format of GetExpressionPath() producing code like
2088 // *(a_ptr).memberName, which is entirely fine, until you put this into
2089 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
2090 // a_ptr->memberName. the eHonorPointers mode is meant to produce strings
2091 // in this latter format
2095 ValueObject *parent = GetParent();
2098 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2100 // if we are a deref_of_parent just because we are synthetic array members
2101 // made up to allow ptr[%d] syntax to work in variable printing, then add our
2102 // name ([%d]) to the expression path
2103 if (m_is_array_item_for_pointer &&
2104 epformat == eGetExpressionPathFormatHonorPointers)
2105 s.PutCString(m_name.AsCString());
2107 if (!IsBaseClass()) {
2108 if (!is_deref_of_parent) {
2109 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2110 if (non_base_class_parent &&
2111 !non_base_class_parent->GetName().IsEmpty()) {
2112 CompilerType non_base_class_parent_compiler_type =
2113 non_base_class_parent->GetCompilerType();
2114 if (non_base_class_parent_compiler_type) {
2115 if (parent && parent->IsDereferenceOfParent() &&
2116 epformat == eGetExpressionPathFormatHonorPointers) {
2119 const uint32_t non_base_class_parent_type_info =
2120 non_base_class_parent_compiler_type.GetTypeInfo();
2122 if (non_base_class_parent_type_info & eTypeIsPointer) {
2124 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2125 !(non_base_class_parent_type_info & eTypeIsArray)) {
2132 const char *name = GetName().GetCString();
2134 if (qualify_cxx_base_classes) {
2135 if (GetBaseClassPath(s))
2143 if (is_deref_of_parent &&
2144 epformat == eGetExpressionPathFormatDereferencePointers) {
2149 ValueObjectSP ValueObject::GetValueForExpressionPath(
2150 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
2151 ExpressionPathEndResultType *final_value_type,
2152 const GetValueForExpressionPathOptions &options,
2153 ExpressionPathAftermath *final_task_on_target) {
2155 ExpressionPathScanEndReason dummy_reason_to_stop =
2156 ValueObject::eExpressionPathScanEndReasonUnknown;
2157 ExpressionPathEndResultType dummy_final_value_type =
2158 ValueObject::eExpressionPathEndResultTypeInvalid;
2159 ExpressionPathAftermath dummy_final_task_on_target =
2160 ValueObject::eExpressionPathAftermathNothing;
2162 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2163 expression, reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2164 final_value_type ? final_value_type : &dummy_final_value_type, options,
2165 final_task_on_target ? final_task_on_target
2166 : &dummy_final_task_on_target);
2168 if (!final_task_on_target ||
2169 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2172 if (ret_val.get() &&
2173 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2174 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2177 if ((final_task_on_target ? *final_task_on_target
2178 : dummy_final_task_on_target) ==
2179 ValueObject::eExpressionPathAftermathDereference) {
2181 ValueObjectSP final_value = ret_val->Dereference(error);
2182 if (error.Fail() || !final_value.get()) {
2185 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2186 if (final_value_type)
2187 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2188 return ValueObjectSP();
2190 if (final_task_on_target)
2191 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2195 if (*final_task_on_target ==
2196 ValueObject::eExpressionPathAftermathTakeAddress) {
2198 ValueObjectSP final_value = ret_val->AddressOf(error);
2199 if (error.Fail() || !final_value.get()) {
2202 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2203 if (final_value_type)
2204 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2205 return ValueObjectSP();
2207 if (final_task_on_target)
2208 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2213 return ret_val; // final_task_on_target will still have its original value, so
2214 // you know I did not do it
2217 ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
2218 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
2219 ExpressionPathEndResultType *final_result,
2220 const GetValueForExpressionPathOptions &options,
2221 ExpressionPathAftermath *what_next) {
2222 ValueObjectSP root = GetSP();
2227 llvm::StringRef remainder = expression;
2230 llvm::StringRef temp_expression = remainder;
2232 CompilerType root_compiler_type = root->GetCompilerType();
2233 CompilerType pointee_compiler_type;
2234 Flags pointee_compiler_type_info;
2236 Flags root_compiler_type_info(
2237 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2238 if (pointee_compiler_type)
2239 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2241 if (temp_expression.empty()) {
2242 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2246 switch (temp_expression.front()) {
2248 temp_expression = temp_expression.drop_front();
2249 if (options.m_check_dot_vs_arrow_syntax &&
2250 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2252 // non-pointer and I
2253 // must catch the error
2256 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2257 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2258 return ValueObjectSP();
2260 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2261 // extract an ObjC IVar
2262 // when this is forbidden
2263 root_compiler_type_info.Test(eTypeIsPointer) &&
2264 options.m_no_fragile_ivar) {
2266 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2267 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2268 return ValueObjectSP();
2270 if (!temp_expression.startswith(">")) {
2272 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2273 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2274 return ValueObjectSP();
2278 case '.': // or fallthrough from ->
2280 if (options.m_check_dot_vs_arrow_syntax &&
2281 temp_expression.front() == '.' &&
2282 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2283 // use . on a pointer
2284 // and I must catch the
2288 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2289 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2292 temp_expression = temp_expression.drop_front(); // skip . or >
2294 size_t next_sep_pos = temp_expression.find_first_of("-.[", 1);
2295 ConstString child_name;
2296 if (next_sep_pos == llvm::StringRef::npos) // if no other separator just
2297 // expand this last layer
2299 child_name.SetString(temp_expression);
2300 ValueObjectSP child_valobj_sp =
2301 root->GetChildMemberWithName(child_name, true);
2303 if (child_valobj_sp.get()) // we know we are done, so just return
2306 ValueObject::eExpressionPathScanEndReasonEndOfString;
2307 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2308 return child_valobj_sp;
2310 switch (options.m_synthetic_children_traversal) {
2311 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2314 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2316 if (root->IsSynthetic()) {
2317 child_valobj_sp = root->GetNonSyntheticValue();
2318 if (child_valobj_sp.get())
2320 child_valobj_sp->GetChildMemberWithName(child_name, true);
2323 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2325 if (!root->IsSynthetic()) {
2326 child_valobj_sp = root->GetSyntheticValue();
2327 if (child_valobj_sp.get())
2329 child_valobj_sp->GetChildMemberWithName(child_name, true);
2332 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2334 if (root->IsSynthetic()) {
2335 child_valobj_sp = root->GetNonSyntheticValue();
2336 if (child_valobj_sp.get())
2338 child_valobj_sp->GetChildMemberWithName(child_name, true);
2340 child_valobj_sp = root->GetSyntheticValue();
2341 if (child_valobj_sp.get())
2343 child_valobj_sp->GetChildMemberWithName(child_name, true);
2349 // if we are here and options.m_no_synthetic_children is true,
2350 // child_valobj_sp is going to be a NULL SP, so we hit the "else"
2351 // branch, and return an error
2352 if (child_valobj_sp.get()) // if it worked, just return
2355 ValueObject::eExpressionPathScanEndReasonEndOfString;
2356 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2357 return child_valobj_sp;
2360 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2361 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2364 } else // other layers do expand
2366 llvm::StringRef next_separator = temp_expression.substr(next_sep_pos);
2368 child_name.SetString(temp_expression.slice(0, next_sep_pos));
2370 ValueObjectSP child_valobj_sp =
2371 root->GetChildMemberWithName(child_name, true);
2372 if (child_valobj_sp.get()) // store the new root and move on
2374 root = child_valobj_sp;
2375 remainder = next_separator;
2376 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2379 switch (options.m_synthetic_children_traversal) {
2380 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2383 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2385 if (root->IsSynthetic()) {
2386 child_valobj_sp = root->GetNonSyntheticValue();
2387 if (child_valobj_sp.get())
2389 child_valobj_sp->GetChildMemberWithName(child_name, true);
2392 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2394 if (!root->IsSynthetic()) {
2395 child_valobj_sp = root->GetSyntheticValue();
2396 if (child_valobj_sp.get())
2398 child_valobj_sp->GetChildMemberWithName(child_name, true);
2401 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2403 if (root->IsSynthetic()) {
2404 child_valobj_sp = root->GetNonSyntheticValue();
2405 if (child_valobj_sp.get())
2407 child_valobj_sp->GetChildMemberWithName(child_name, true);
2409 child_valobj_sp = root->GetSyntheticValue();
2410 if (child_valobj_sp.get())
2412 child_valobj_sp->GetChildMemberWithName(child_name, true);
2418 // if we are here and options.m_no_synthetic_children is true,
2419 // child_valobj_sp is going to be a NULL SP, so we hit the "else"
2420 // branch, and return an error
2421 if (child_valobj_sp.get()) // if it worked, move on
2423 root = child_valobj_sp;
2424 remainder = next_separator;
2425 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2429 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2430 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2437 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2438 !root_compiler_type_info.Test(eTypeIsPointer) &&
2439 !root_compiler_type_info.Test(
2440 eTypeIsVector)) // if this is not a T[] nor a T*
2442 if (!root_compiler_type_info.Test(
2443 eTypeIsScalar)) // if this is not even a scalar...
2445 if (options.m_synthetic_children_traversal ==
2446 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2447 None) // ...only chance left is synthetic
2450 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2451 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2452 return ValueObjectSP();
2454 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2455 // check that we can
2459 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2460 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2461 return ValueObjectSP();
2464 if (temp_expression[1] ==
2465 ']') // if this is an unbounded range it only works for arrays
2467 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2469 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2470 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2472 } else // even if something follows, we cannot expand unbounded ranges,
2473 // just let the caller do it
2476 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2478 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2483 size_t close_bracket_position = temp_expression.find(']', 1);
2484 if (close_bracket_position ==
2485 llvm::StringRef::npos) // if there is no ], this is a syntax error
2488 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2489 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2493 llvm::StringRef bracket_expr =
2494 temp_expression.slice(1, close_bracket_position);
2496 // If this was an empty expression it would have been caught by the if
2498 assert(!bracket_expr.empty());
2500 if (!bracket_expr.contains('-')) {
2501 // if no separator, this is of the form [N]. Note that this cannot be
2502 // an unbounded range of the form [], because that case was handled
2503 // above with an unconditional return.
2504 unsigned long index = 0;
2505 if (bracket_expr.getAsInteger(0, index)) {
2507 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2508 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2512 // from here on we do have a valid index
2513 if (root_compiler_type_info.Test(eTypeIsArray)) {
2514 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2515 if (!child_valobj_sp)
2516 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2517 if (!child_valobj_sp)
2518 if (root->HasSyntheticValue() &&
2519 root->GetSyntheticValue()->GetNumChildren() > index)
2521 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2522 if (child_valobj_sp) {
2523 root = child_valobj_sp;
2525 temp_expression.substr(close_bracket_position + 1); // skip ]
2526 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2530 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2531 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2534 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2537 eExpressionPathAftermathDereference && // if this is a
2546 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2548 root = root->Dereference(error);
2549 if (error.Fail() || !root) {
2551 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2552 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2555 *what_next = eExpressionPathAftermathNothing;
2559 if (root->GetCompilerType().GetMinimumLanguage() ==
2560 eLanguageTypeObjC &&
2561 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2562 root->HasSyntheticValue() &&
2563 (options.m_synthetic_children_traversal ==
2564 GetValueForExpressionPathOptions::
2565 SyntheticChildrenTraversal::ToSynthetic ||
2566 options.m_synthetic_children_traversal ==
2567 GetValueForExpressionPathOptions::
2568 SyntheticChildrenTraversal::Both)) {
2569 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2571 root = root->GetSyntheticArrayMember(index, true);
2574 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2575 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2579 temp_expression.substr(close_bracket_position + 1); // skip ]
2580 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2584 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2585 root = root->GetSyntheticBitFieldChild(index, index, true);
2588 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2589 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2591 } else // we do not know how to expand members of bitfields, so we
2592 // just return and let the caller do any further processing
2594 *reason_to_stop = ValueObject::
2595 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2596 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2599 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2600 root = root->GetChildAtIndex(index, true);
2603 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2604 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2605 return ValueObjectSP();
2608 temp_expression.substr(close_bracket_position + 1); // skip ]
2609 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2612 } else if (options.m_synthetic_children_traversal ==
2613 GetValueForExpressionPathOptions::
2614 SyntheticChildrenTraversal::ToSynthetic ||
2615 options.m_synthetic_children_traversal ==
2616 GetValueForExpressionPathOptions::
2617 SyntheticChildrenTraversal::Both) {
2618 if (root->HasSyntheticValue())
2619 root = root->GetSyntheticValue();
2620 else if (!root->IsSynthetic()) {
2622 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2623 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2626 // if we are here, then root itself is a synthetic VO.. should be
2631 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2632 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2635 root = root->GetChildAtIndex(index, true);
2638 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2639 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2643 temp_expression.substr(close_bracket_position + 1); // skip ]
2644 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2649 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2650 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2654 // we have a low and a high index
2655 llvm::StringRef sleft, sright;
2656 unsigned long low_index, high_index;
2657 std::tie(sleft, sright) = bracket_expr.split('-');
2658 if (sleft.getAsInteger(0, low_index) ||
2659 sright.getAsInteger(0, high_index)) {
2661 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2662 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2666 if (low_index > high_index) // swap indices if required
2667 std::swap(low_index, high_index);
2669 if (root_compiler_type_info.Test(
2670 eTypeIsScalar)) // expansion only works for scalars
2672 root = root->GetSyntheticBitFieldChild(low_index, high_index, true);
2675 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2676 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2679 *reason_to_stop = ValueObject::
2680 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2681 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2684 } else if (root_compiler_type_info.Test(
2685 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2686 // accessing it by index and I would
2687 // have deref'ed anyway, then do it
2688 // now and use this as a bitfield
2690 ValueObject::eExpressionPathAftermathDereference &&
2691 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2693 root = root->Dereference(error);
2694 if (error.Fail() || !root) {
2696 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2697 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2700 *what_next = ValueObject::eExpressionPathAftermathNothing;
2705 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2706 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2712 default: // some non-separator is in the way
2715 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2716 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2723 void ValueObject::LogValueObject(Log *log) {
2725 return LogValueObject(log, DumpValueObjectOptions(*this));
2728 void ValueObject::LogValueObject(Log *log,
2729 const DumpValueObjectOptions &options) {
2734 log->PutCString(s.GetData());
2738 void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
2740 void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
2741 ValueObjectPrinter printer(this, &s, options);
2742 printer.PrintValueObject();
2745 ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
2746 ValueObjectSP valobj_sp;
2748 if (UpdateValueIfNeeded(false) && m_error.Success()) {
2749 ExecutionContext exe_ctx(GetExecutionContextRef());
2752 data.SetByteOrder(m_data.GetByteOrder());
2753 data.SetAddressByteSize(m_data.GetAddressByteSize());
2756 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
2757 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2759 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2761 valobj_sp = ValueObjectConstResult::Create(
2762 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
2767 ExecutionContext exe_ctx(GetExecutionContextRef());
2768 valobj_sp = ValueObjectConstResult::Create(
2769 exe_ctx.GetBestExecutionContextScope(), m_error);
2774 ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
2775 lldb::DynamicValueType dynValue, bool synthValue) {
2776 ValueObjectSP result_sp(GetSP());
2779 case lldb::eDynamicCanRunTarget:
2780 case lldb::eDynamicDontRunTarget: {
2781 if (!result_sp->IsDynamic()) {
2782 if (result_sp->GetDynamicValue(dynValue))
2783 result_sp = result_sp->GetDynamicValue(dynValue);
2786 case lldb::eNoDynamicValues: {
2787 if (result_sp->IsDynamic()) {
2788 if (result_sp->GetStaticValue())
2789 result_sp = result_sp->GetStaticValue();
2795 if (!result_sp->IsSynthetic()) {
2796 if (result_sp->GetSyntheticValue())
2797 result_sp = result_sp->GetSyntheticValue();
2800 if (result_sp->IsSynthetic()) {
2801 if (result_sp->GetNonSyntheticValue())
2802 result_sp = result_sp->GetNonSyntheticValue();
2809 lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
2810 CompilerType pointee_type;
2811 CompilerType this_type(GetCompilerType());
2812 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
2814 bool ptr_or_ref = false;
2815 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
2817 type_info = pointee_type.GetTypeInfo();
2820 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
2821 if ((type_info & cpp_class) == cpp_class) {
2823 address_type = GetAddressTypeOfChildren();
2824 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
2826 return GetAddressOf(false, &address_type);
2830 address_type = eAddressTypeInvalid;
2831 return LLDB_INVALID_ADDRESS;
2834 ValueObjectSP ValueObject::Dereference(Status &error) {
2836 return m_deref_valobj->GetSP();
2838 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
2839 if (is_pointer_or_reference_type) {
2840 bool omit_empty_base_classes = true;
2841 bool ignore_array_bounds = false;
2843 std::string child_name_str;
2844 uint32_t child_byte_size = 0;
2845 int32_t child_byte_offset = 0;
2846 uint32_t child_bitfield_bit_size = 0;
2847 uint32_t child_bitfield_bit_offset = 0;
2848 bool child_is_base_class = false;
2849 bool child_is_deref_of_parent = false;
2850 const bool transparent_pointers = false;
2851 CompilerType compiler_type = GetCompilerType();
2852 CompilerType child_compiler_type;
2853 uint64_t language_flags;
2855 ExecutionContext exe_ctx(GetExecutionContextRef());
2857 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
2858 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
2859 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
2860 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
2861 child_is_deref_of_parent, this, language_flags);
2862 if (child_compiler_type && child_byte_size) {
2863 ConstString child_name;
2864 if (!child_name_str.empty())
2865 child_name.SetCString(child_name_str.c_str());
2867 m_deref_valobj = new ValueObjectChild(
2868 *this, child_compiler_type, child_name, child_byte_size,
2869 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
2870 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
2873 } else if (HasSyntheticValue()) {
2876 ->GetChildMemberWithName(ConstString("$$dereference$$"), true)
2880 if (m_deref_valobj) {
2882 return m_deref_valobj->GetSP();
2885 GetExpressionPath(strm, true);
2887 if (is_pointer_or_reference_type)
2888 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
2889 GetTypeName().AsCString("<invalid type>"),
2892 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
2893 GetTypeName().AsCString("<invalid type>"),
2895 return ValueObjectSP();
2899 ValueObjectSP ValueObject::AddressOf(Status &error) {
2900 if (m_addr_of_valobj_sp)
2901 return m_addr_of_valobj_sp;
2903 AddressType address_type = eAddressTypeInvalid;
2904 const bool scalar_is_load_address = false;
2905 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
2907 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
2908 switch (address_type) {
2909 case eAddressTypeInvalid: {
2910 StreamString expr_path_strm;
2911 GetExpressionPath(expr_path_strm, true);
2912 error.SetErrorStringWithFormat("'%s' is not in memory",
2913 expr_path_strm.GetData());
2916 case eAddressTypeFile:
2917 case eAddressTypeLoad: {
2918 CompilerType compiler_type = GetCompilerType();
2919 if (compiler_type) {
2920 std::string name(1, '&');
2921 name.append(m_name.AsCString(""));
2922 ExecutionContext exe_ctx(GetExecutionContextRef());
2923 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
2924 exe_ctx.GetBestExecutionContextScope(),
2925 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
2926 eAddressTypeInvalid, m_data.GetAddressByteSize());
2933 StreamString expr_path_strm;
2934 GetExpressionPath(expr_path_strm, true);
2935 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
2936 expr_path_strm.GetData());
2939 return m_addr_of_valobj_sp;
2942 ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
2943 return ValueObjectCast::Create(*this, GetName(), compiler_type);
2946 lldb::ValueObjectSP ValueObject::Clone(const ConstString &new_name) {
2947 return ValueObjectCast::Create(*this, new_name, GetCompilerType());
2950 ValueObjectSP ValueObject::CastPointerType(const char *name,
2951 CompilerType &compiler_type) {
2952 ValueObjectSP valobj_sp;
2953 AddressType address_type;
2954 addr_t ptr_value = GetPointerValue(&address_type);
2956 if (ptr_value != LLDB_INVALID_ADDRESS) {
2957 Address ptr_addr(ptr_value);
2958 ExecutionContext exe_ctx(GetExecutionContextRef());
2959 valobj_sp = ValueObjectMemory::Create(
2960 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
2965 ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
2966 ValueObjectSP valobj_sp;
2967 AddressType address_type;
2968 addr_t ptr_value = GetPointerValue(&address_type);
2970 if (ptr_value != LLDB_INVALID_ADDRESS) {
2971 Address ptr_addr(ptr_value);
2972 ExecutionContext exe_ctx(GetExecutionContextRef());
2973 valobj_sp = ValueObjectMemory::Create(
2974 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
2979 ValueObject::EvaluationPoint::EvaluationPoint()
2980 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
2982 ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
2984 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
2985 ExecutionContext exe_ctx(exe_scope);
2986 TargetSP target_sp(exe_ctx.GetTargetSP());
2988 m_exe_ctx_ref.SetTargetSP(target_sp);
2989 ProcessSP process_sp(exe_ctx.GetProcessSP());
2991 process_sp = target_sp->GetProcessSP();
2994 m_mod_id = process_sp->GetModID();
2995 m_exe_ctx_ref.SetProcessSP(process_sp);
2997 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3001 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3005 m_exe_ctx_ref.SetThreadSP(thread_sp);
3007 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3010 frame_sp = thread_sp->GetSelectedFrame();
3013 m_exe_ctx_ref.SetFrameSP(frame_sp);
3019 ValueObject::EvaluationPoint::EvaluationPoint(
3020 const ValueObject::EvaluationPoint &rhs)
3021 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
3023 ValueObject::EvaluationPoint::~EvaluationPoint() {}
3025 // This function checks the EvaluationPoint against the current process state.
3026 // If the current state matches the evaluation point, or the evaluation point
3027 // is already invalid, then we return false, meaning "no change". If the
3028 // current state is different, we update our state, and return true meaning
3029 // "yes, change". If we did see a change, we also set m_needs_update to true,
3030 // so future calls to NeedsUpdate will return true. exe_scope will be set to
3031 // the current execution context scope.
3033 bool ValueObject::EvaluationPoint::SyncWithProcessState(
3034 bool accept_invalid_exe_ctx) {
3035 // Start with the target, if it is NULL, then we're obviously not going to
3037 const bool thread_and_frame_only_if_stopped = true;
3038 ExecutionContext exe_ctx(
3039 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3041 if (exe_ctx.GetTargetPtr() == NULL)
3044 // If we don't have a process nothing can change.
3045 Process *process = exe_ctx.GetProcessPtr();
3046 if (process == NULL)
3049 // If our stop id is the current stop ID, nothing has changed:
3050 ProcessModID current_mod_id = process->GetModID();
3052 // If the current stop id is 0, either we haven't run yet, or the process
3053 // state has been cleared. In either case, we aren't going to be able to sync
3054 // with the process state.
3055 if (current_mod_id.GetStopID() == 0)
3058 bool changed = false;
3059 const bool was_valid = m_mod_id.IsValid();
3061 if (m_mod_id == current_mod_id) {
3062 // Everything is already up to date in this object, no need to update the
3063 // execution context scope.
3066 m_mod_id = current_mod_id;
3067 m_needs_update = true;
3072 // Now re-look up the thread and frame in case the underlying objects have
3073 // gone away & been recreated. That way we'll be sure to return a valid
3074 // exe_scope. If we used to have a thread or a frame but can't find it
3075 // anymore, then mark ourselves as invalid.
3077 if (!accept_invalid_exe_ctx) {
3078 if (m_exe_ctx_ref.HasThreadRef()) {
3079 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3081 if (m_exe_ctx_ref.HasFrameRef()) {
3082 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3084 // We used to have a frame, but now it is gone
3086 changed = was_valid;
3090 // We used to have a thread, but now it is gone
3092 changed = was_valid;
3100 void ValueObject::EvaluationPoint::SetUpdated() {
3101 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3103 m_mod_id = process_sp->GetModID();
3104 m_needs_update = false;
3107 void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3108 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3109 eClearUserVisibleDataItemsValue)
3110 m_value_str.clear();
3112 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3113 eClearUserVisibleDataItemsLocation)
3114 m_location_str.clear();
3116 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3117 eClearUserVisibleDataItemsSummary)
3118 m_summary_str.clear();
3120 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3121 eClearUserVisibleDataItemsDescription)
3122 m_object_desc_str.clear();
3124 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3125 eClearUserVisibleDataItemsSyntheticChildren) {
3126 if (m_synthetic_value)
3127 m_synthetic_value = NULL;
3130 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3131 eClearUserVisibleDataItemsValidator)
3132 m_validation_result.reset();
3135 SymbolContextScope *ValueObject::GetSymbolContextScope() {
3137 if (!m_parent->IsPointerOrReferenceType())
3138 return m_parent->GetSymbolContextScope();
3144 ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3145 llvm::StringRef expression,
3146 const ExecutionContext &exe_ctx) {
3147 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3148 EvaluateExpressionOptions());
3151 lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
3152 llvm::StringRef name, llvm::StringRef expression,
3153 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
3154 lldb::ValueObjectSP retval_sp;
3155 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3158 if (expression.empty())
3160 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3161 retval_sp, options);
3162 if (retval_sp && !name.empty())
3163 retval_sp->SetName(ConstString(name));
3167 lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3168 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3169 CompilerType type) {
3171 CompilerType pointer_type(type.GetPointerType());
3173 lldb::DataBufferSP buffer(
3174 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3175 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3176 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3177 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3178 exe_ctx.GetAddressByteSize()));
3179 if (ptr_result_valobj_sp) {
3180 ptr_result_valobj_sp->GetValue().SetValueType(
3181 Value::eValueTypeLoadAddress);
3183 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
3184 if (ptr_result_valobj_sp && !name.empty())
3185 ptr_result_valobj_sp->SetName(ConstString(name));
3187 return ptr_result_valobj_sp;
3190 return lldb::ValueObjectSP();
3193 lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
3194 llvm::StringRef name, const DataExtractor &data,
3195 const ExecutionContext &exe_ctx, CompilerType type) {
3196 lldb::ValueObjectSP new_value_sp;
3197 new_value_sp = ValueObjectConstResult::Create(
3198 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3199 LLDB_INVALID_ADDRESS);
3200 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
3201 if (new_value_sp && !name.empty())
3202 new_value_sp->SetName(ConstString(name));
3203 return new_value_sp;
3206 ModuleSP ValueObject::GetModule() {
3207 ValueObject *root(GetRoot());
3209 return root->GetModule();
3210 return lldb::ModuleSP();
3213 ValueObject *ValueObject::GetRoot() {
3216 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3217 return (vo->m_parent != nullptr);
3222 ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3223 ValueObject *vo = this;
3232 AddressType ValueObject::GetAddressTypeOfChildren() {
3233 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3234 ValueObject *root(GetRoot());
3236 return root->GetAddressTypeOfChildren();
3238 return m_address_type_of_ptr_or_ref_children;
3241 lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3242 ValueObject *with_dv_info = this;
3243 while (with_dv_info) {
3244 if (with_dv_info->HasDynamicValueTypeInfo())
3245 return with_dv_info->GetDynamicValueTypeImpl();
3246 with_dv_info = with_dv_info->m_parent;
3248 return lldb::eNoDynamicValues;
3251 lldb::Format ValueObject::GetFormat() const {
3252 const ValueObject *with_fmt_info = this;
3253 while (with_fmt_info) {
3254 if (with_fmt_info->m_format != lldb::eFormatDefault)
3255 return with_fmt_info->m_format;
3256 with_fmt_info = with_fmt_info->m_parent;
3261 lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3262 lldb::LanguageType type = m_preferred_display_language;
3263 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3265 if (GetRoot() == this) {
3266 if (StackFrameSP frame_sp = GetFrameSP()) {
3267 const SymbolContext &sc(
3268 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3269 if (CompileUnit *cu = sc.comp_unit)
3270 type = cu->GetLanguage();
3273 type = GetRoot()->GetPreferredDisplayLanguage();
3277 return (m_preferred_display_language = type); // only compute it once
3280 void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3281 m_preferred_display_language = lt;
3284 void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3285 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3286 SetPreferredDisplayLanguage(lt);
3289 bool ValueObject::CanProvideValue() {
3290 // we need to support invalid types as providers of values because some bare-
3291 // board debugging scenarios have no notion of types, but still manage to
3292 // have raw numeric values for things like registers. sigh.
3293 const CompilerType &type(GetCompilerType());
3294 return (false == type.IsValid()) ||
3295 (0 != (type.GetTypeInfo() & eTypeHasValue));
3298 bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3300 ValueObjectSP ValueObject::Persist() {
3301 if (!UpdateValueIfNeeded())
3304 TargetSP target_sp(GetTargetSP());
3308 PersistentExpressionState *persistent_state =
3309 target_sp->GetPersistentExpressionStateForLanguage(
3310 GetPreferredDisplayLanguage());
3312 if (!persistent_state)
3315 auto prefix = persistent_state->GetPersistentVariablePrefix();
3317 persistent_state->GetNextPersistentVariableName(*target_sp, prefix);
3319 ValueObjectSP const_result_sp =
3320 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3322 ExpressionVariableSP clang_var_sp =
3323 persistent_state->CreatePersistentVariable(const_result_sp);
3324 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3325 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3327 return clang_var_sp->GetValueObject();
3330 bool ValueObject::IsSyntheticChildrenGenerated() {
3331 return m_is_synthetic_children_generated;
3334 void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3335 m_is_synthetic_children_generated = b;
3338 uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
3340 void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }
3342 ValueObjectManager::ValueObjectManager(lldb::ValueObjectSP in_valobj_sp,
3343 lldb::DynamicValueType use_dynamic,
3344 bool use_synthetic) : m_root_valobj_sp(),
3345 m_user_valobj_sp(), m_use_dynamic(use_dynamic), m_stop_id(UINT32_MAX),
3346 m_use_synthetic(use_synthetic) {
3349 // If the user passes in a value object that is dynamic or synthetic, then
3350 // water it down to the static type.
3351 m_root_valobj_sp = in_valobj_sp->GetQualifiedRepresentationIfAvailable(lldb::eNoDynamicValues, false);
3354 bool ValueObjectManager::IsValid() const {
3355 if (!m_root_valobj_sp)
3357 lldb::TargetSP target_sp = GetTargetSP();
3359 return target_sp->IsValid();
3363 lldb::ValueObjectSP ValueObjectManager::GetSP() {
3364 lldb::ProcessSP process_sp = GetProcessSP();
3366 return lldb::ValueObjectSP();
3368 const uint32_t current_stop_id = process_sp->GetLastNaturalStopID();
3369 if (current_stop_id == m_stop_id)
3370 return m_user_valobj_sp;
3372 m_stop_id = current_stop_id;
3374 if (!m_root_valobj_sp) {
3375 m_user_valobj_sp.reset();
3376 return m_root_valobj_sp;
3379 m_user_valobj_sp = m_root_valobj_sp;
3381 if (m_use_dynamic != lldb::eNoDynamicValues) {
3382 lldb::ValueObjectSP dynamic_sp = m_user_valobj_sp->GetDynamicValue(m_use_dynamic);
3384 m_user_valobj_sp = dynamic_sp;
3387 if (m_use_synthetic) {
3388 lldb::ValueObjectSP synthetic_sp = m_user_valobj_sp->GetSyntheticValue(m_use_synthetic);
3390 m_user_valobj_sp = synthetic_sp;
3393 return m_user_valobj_sp;
3396 void ValueObjectManager::SetUseDynamic(lldb::DynamicValueType use_dynamic) {
3397 if (use_dynamic != m_use_dynamic) {
3398 m_use_dynamic = use_dynamic;
3399 m_user_valobj_sp.reset();
3400 m_stop_id = UINT32_MAX;
3404 void ValueObjectManager::SetUseSynthetic(bool use_synthetic) {
3405 if (m_use_synthetic != use_synthetic) {
3406 m_use_synthetic = use_synthetic;
3407 m_user_valobj_sp.reset();
3408 m_stop_id = UINT32_MAX;
3412 lldb::TargetSP ValueObjectManager::GetTargetSP() const {
3413 if (!m_root_valobj_sp)
3414 return m_root_valobj_sp->GetTargetSP();
3415 return lldb::TargetSP();
3418 lldb::ProcessSP ValueObjectManager::GetProcessSP() const {
3419 if (m_root_valobj_sp)
3420 return m_root_valobj_sp->GetProcessSP();
3421 return lldb::ProcessSP();
3424 lldb::ThreadSP ValueObjectManager::GetThreadSP() const {
3425 if (m_root_valobj_sp)
3426 return m_root_valobj_sp->GetThreadSP();
3427 return lldb::ThreadSP();
3430 lldb::StackFrameSP ValueObjectManager::GetFrameSP() const {
3431 if (m_root_valobj_sp)
3432 return m_root_valobj_sp->GetFrameSP();
3433 return lldb::StackFrameSP();