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/FormatManager.h" // for FormatManager
23 #include "lldb/DataFormatters/StringPrinter.h"
24 #include "lldb/DataFormatters/TypeFormat.h" // for TypeFormatImpl_F...
25 #include "lldb/DataFormatters/TypeSummary.h" // for TypeSummaryOptions
26 #include "lldb/DataFormatters/TypeValidator.h" // for TypeValidatorImp...
27 #include "lldb/DataFormatters/ValueObjectPrinter.h"
28 #include "lldb/Expression/ExpressionVariable.h" // for ExpressionVariable
29 #include "lldb/Symbol/ClangASTContext.h"
30 #include "lldb/Symbol/CompileUnit.h"
31 #include "lldb/Symbol/CompilerType.h"
32 #include "lldb/Symbol/Declaration.h" // for Declaration
33 #include "lldb/Symbol/SymbolContext.h" // for SymbolContext
34 #include "lldb/Symbol/Type.h"
35 #include "lldb/Target/ExecutionContext.h"
36 #include "lldb/Target/Language.h"
37 #include "lldb/Target/LanguageRuntime.h"
38 #include "lldb/Target/ObjCLanguageRuntime.h"
39 #include "lldb/Target/Process.h"
40 #include "lldb/Target/StackFrame.h" // for StackFrame
41 #include "lldb/Target/Target.h"
42 #include "lldb/Target/Thread.h"
43 #include "lldb/Target/ThreadList.h" // for ThreadList
44 #include "lldb/Utility/DataBuffer.h" // for DataBuffer
45 #include "lldb/Utility/DataBufferHeap.h"
46 #include "lldb/Utility/Flags.h" // for Flags
47 #include "lldb/Utility/Log.h"
48 #include "lldb/Utility/Logging.h" // for GetLogIfAllCateg...
49 #include "lldb/Utility/SharingPtr.h" // for SharingPtr
50 #include "lldb/Utility/Stream.h" // for Stream
51 #include "lldb/Utility/StreamString.h"
52 #include "lldb/lldb-private-types.h" // for RegisterInfo
54 #include "llvm/Support/Compiler.h" // for LLVM_FALLTHROUGH
56 #include <algorithm> // for min
57 #include <cstdint> // for uint32_t, uint64_t
58 #include <cstdlib> // for size_t, NULL
59 #include <memory> // for shared_ptr, oper...
60 #include <tuple> // for tie, tuple
62 #include <assert.h> // for assert
63 #include <inttypes.h> // for PRIu64, PRIx64
64 #include <stdio.h> // for snprintf
65 #include <string.h> // for memcpy, memcmp
67 namespace lldb_private {
68 class ExecutionContextScope;
70 namespace lldb_private {
71 class SymbolContextScope;
75 using namespace lldb_private;
76 using namespace lldb_utility;
78 static user_id_t g_value_obj_uid = 0;
80 //----------------------------------------------------------------------
81 // ValueObject constructor
82 //----------------------------------------------------------------------
83 ValueObject::ValueObject(ValueObject &parent)
84 : UserID(++g_value_obj_uid), // Unique identifier for every value object
85 m_parent(&parent), m_root(NULL), m_update_point(parent.GetUpdatePoint()),
86 m_name(), m_data(), m_value(), m_error(), m_value_str(),
87 m_old_value_str(), m_location_str(), m_summary_str(), m_object_desc_str(),
88 m_validation_result(), m_manager(parent.GetManager()), m_children(),
89 m_synthetic_children(), m_dynamic_value(NULL), m_synthetic_value(NULL),
90 m_deref_valobj(NULL), m_format(eFormatDefault),
91 m_last_format(eFormatDefault), m_last_format_mgr_revision(0),
92 m_type_summary_sp(), m_type_format_sp(), m_synthetic_children_sp(),
93 m_type_validator_sp(), m_user_id_of_forced_summary(),
94 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
96 m_preferred_display_language(lldb::eLanguageTypeUnknown),
97 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
98 m_children_count_valid(false), m_old_value_valid(false),
99 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
100 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
101 m_is_getting_summary(false),
102 m_did_calculate_complete_objc_class_type(false),
103 m_is_synthetic_children_generated(
104 parent.m_is_synthetic_children_generated) {
105 m_manager->ManageObject(this);
108 //----------------------------------------------------------------------
109 // ValueObject constructor
110 //----------------------------------------------------------------------
111 ValueObject::ValueObject(ExecutionContextScope *exe_scope,
112 AddressType child_ptr_or_ref_addr_type)
113 : UserID(++g_value_obj_uid), // Unique identifier for every value object
114 m_parent(NULL), m_root(NULL), m_update_point(exe_scope), m_name(),
115 m_data(), m_value(), m_error(), m_value_str(), m_old_value_str(),
116 m_location_str(), m_summary_str(), m_object_desc_str(),
117 m_validation_result(), m_manager(), m_children(), m_synthetic_children(),
118 m_dynamic_value(NULL), m_synthetic_value(NULL), m_deref_valobj(NULL),
119 m_format(eFormatDefault), m_last_format(eFormatDefault),
120 m_last_format_mgr_revision(0), m_type_summary_sp(), m_type_format_sp(),
121 m_synthetic_children_sp(), m_type_validator_sp(),
122 m_user_id_of_forced_summary(),
123 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
125 m_preferred_display_language(lldb::eLanguageTypeUnknown),
126 m_language_flags(0), m_value_is_valid(false), m_value_did_change(false),
127 m_children_count_valid(false), m_old_value_valid(false),
128 m_is_deref_of_parent(false), m_is_array_item_for_pointer(false),
129 m_is_bitfield_for_scalar(false), m_is_child_at_offset(false),
130 m_is_getting_summary(false),
131 m_did_calculate_complete_objc_class_type(false),
132 m_is_synthetic_children_generated(false) {
133 m_manager = new ValueObjectManager();
134 m_manager->ManageObject(this);
137 //----------------------------------------------------------------------
139 //----------------------------------------------------------------------
140 ValueObject::~ValueObject() {}
142 bool ValueObject::UpdateValueIfNeeded(bool update_format) {
144 bool did_change_formats = false;
147 did_change_formats = UpdateFormatsIfNeeded();
149 // If this is a constant value, then our success is predicated on whether
150 // we have an error or not
151 if (GetIsConstant()) {
152 // if you are constant, things might still have changed behind your back
153 // (e.g. you are a frozen object and things have changed deeper than you
154 // cared to freeze-dry yourself)
155 // in this case, your value has not changed, but "computed" entries might
156 // have, so you might now have
157 // a different summary, or a different object description. clear these so we
158 // will recompute them
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
171 // also will 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
219 // as changed if 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
265 // changed by hand (i.e. with SetValueAsCString).
266 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
269 void ValueObject::ClearDynamicTypeInformation() {
270 m_children_count_valid = false;
271 m_did_calculate_complete_objc_class_type = false;
272 m_last_format_mgr_revision = 0;
273 m_override_type = CompilerType();
274 SetValueFormat(lldb::TypeFormatImplSP());
275 SetSummaryFormat(lldb::TypeSummaryImplSP());
276 SetSyntheticChildren(lldb::SyntheticChildrenSP());
279 CompilerType ValueObject::MaybeCalculateCompleteType() {
280 CompilerType compiler_type(GetCompilerTypeImpl());
282 if (m_did_calculate_complete_objc_class_type) {
283 if (m_override_type.IsValid())
284 return m_override_type;
286 return compiler_type;
289 CompilerType class_type;
290 bool is_pointer_type = false;
292 if (ClangASTContext::IsObjCObjectPointerType(compiler_type, &class_type)) {
293 is_pointer_type = true;
294 } else if (ClangASTContext::IsObjCObjectOrInterfaceType(compiler_type)) {
295 class_type = compiler_type;
297 return compiler_type;
300 m_did_calculate_complete_objc_class_type = true;
303 ConstString class_name(class_type.GetConstTypeName());
306 ProcessSP process_sp(
307 GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
310 ObjCLanguageRuntime *objc_language_runtime(
311 process_sp->GetObjCLanguageRuntime());
313 if (objc_language_runtime) {
314 TypeSP complete_objc_class_type_sp =
315 objc_language_runtime->LookupInCompleteClassCache(class_name);
317 if (complete_objc_class_type_sp) {
318 CompilerType complete_class(
319 complete_objc_class_type_sp->GetFullCompilerType());
321 if (complete_class.GetCompleteType()) {
322 if (is_pointer_type) {
323 m_override_type = complete_class.GetPointerType();
325 m_override_type = complete_class;
328 if (m_override_type.IsValid())
329 return m_override_type;
336 return compiler_type;
339 CompilerType ValueObject::GetCompilerType() {
340 return MaybeCalculateCompleteType();
343 TypeImpl ValueObject::GetTypeImpl() { return TypeImpl(GetCompilerType()); }
345 DataExtractor &ValueObject::GetDataExtractor() {
346 UpdateValueIfNeeded(false);
350 const Error &ValueObject::GetError() {
351 UpdateValueIfNeeded(false);
355 const ConstString &ValueObject::GetName() const { return m_name; }
357 const char *ValueObject::GetLocationAsCString() {
358 return GetLocationAsCStringImpl(m_value, m_data);
361 const char *ValueObject::GetLocationAsCStringImpl(const Value &value,
362 const DataExtractor &data) {
363 if (UpdateValueIfNeeded(false)) {
364 if (m_location_str.empty()) {
367 Value::ValueType value_type = value.GetValueType();
369 switch (value_type) {
370 case Value::eValueTypeScalar:
371 case Value::eValueTypeVector:
372 if (value.GetContextType() == Value::eContextTypeRegisterInfo) {
373 RegisterInfo *reg_info = value.GetRegisterInfo();
376 m_location_str = reg_info->name;
377 else if (reg_info->alt_name)
378 m_location_str = reg_info->alt_name;
379 if (m_location_str.empty())
380 m_location_str = (reg_info->encoding == lldb::eEncodingVector)
385 if (m_location_str.empty())
387 (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
390 case Value::eValueTypeLoadAddress:
391 case Value::eValueTypeFileAddress:
392 case Value::eValueTypeHostAddress: {
393 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
394 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size,
395 value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
396 m_location_str = sstr.GetString();
401 return m_location_str.c_str();
404 Value &ValueObject::GetValue() { return m_value; }
406 const Value &ValueObject::GetValue() const { return m_value; }
408 bool ValueObject::ResolveValue(Scalar &scalar) {
409 if (UpdateValueIfNeeded(
410 false)) // make sure that you are up to date before returning anything
412 ExecutionContext exe_ctx(GetExecutionContextRef());
413 Value tmp_value(m_value);
414 scalar = tmp_value.ResolveValue(&exe_ctx);
415 if (scalar.IsValid()) {
416 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
417 if (bitfield_bit_size)
418 return scalar.ExtractBitfield(bitfield_bit_size,
419 GetBitfieldBitOffset());
426 bool ValueObject::IsLogicalTrue(Error &error) {
427 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
428 LazyBool is_logical_true = language->IsLogicalTrue(*this, error);
429 switch (is_logical_true) {
432 return (is_logical_true == true);
433 case eLazyBoolCalculate:
440 if (!ResolveValue(scalar_value)) {
441 error.SetErrorString("failed to get a scalar result");
446 if (scalar_value.ULongLong(1) == 0)
454 bool ValueObject::GetValueIsValid() const { return m_value_is_valid; }
456 void ValueObject::SetValueIsValid(bool b) { m_value_is_valid = b; }
458 bool ValueObject::GetValueDidChange() { return m_value_did_change; }
460 void ValueObject::SetValueDidChange(bool value_changed) {
461 m_value_did_change = value_changed;
464 ValueObjectSP ValueObject::GetChildAtIndex(size_t idx, bool can_create) {
465 ValueObjectSP child_sp;
466 // We may need to update our value if we are dynamic
467 if (IsPossibleDynamicType())
468 UpdateValueIfNeeded(false);
469 if (idx < GetNumChildren()) {
470 // Check if we have already made the child value object?
471 if (can_create && !m_children.HasChildAtIndex(idx)) {
472 // No we haven't created the child at this index, so lets have our
473 // subclass do it and cache the result for quick future access.
474 m_children.SetChildAtIndex(idx, CreateChildAtIndex(idx, false, 0));
477 ValueObject *child = m_children.GetChildAtIndex(idx);
479 return child->GetSP();
485 ValueObject::GetChildAtIndexPath(llvm::ArrayRef<size_t> idxs,
486 size_t *index_of_error) {
487 if (idxs.size() == 0)
489 ValueObjectSP root(GetSP());
490 for (size_t idx : idxs) {
491 root = root->GetChildAtIndex(idx, true);
494 *index_of_error = idx;
501 lldb::ValueObjectSP ValueObject::GetChildAtIndexPath(
502 llvm::ArrayRef<std::pair<size_t, bool>> idxs, size_t *index_of_error) {
503 if (idxs.size() == 0)
505 ValueObjectSP root(GetSP());
506 for (std::pair<size_t, bool> idx : idxs) {
507 root = root->GetChildAtIndex(idx.first, idx.second);
510 *index_of_error = idx.first;
518 ValueObject::GetChildAtNamePath(llvm::ArrayRef<ConstString> names,
519 ConstString *name_of_error) {
520 if (names.size() == 0)
522 ValueObjectSP root(GetSP());
523 for (ConstString name : names) {
524 root = root->GetChildMemberWithName(name, true);
527 *name_of_error = name;
534 lldb::ValueObjectSP ValueObject::GetChildAtNamePath(
535 llvm::ArrayRef<std::pair<ConstString, bool>> names,
536 ConstString *name_of_error) {
537 if (names.size() == 0)
539 ValueObjectSP root(GetSP());
540 for (std::pair<ConstString, bool> name : names) {
541 root = root->GetChildMemberWithName(name.first, name.second);
544 *name_of_error = name.first;
551 size_t ValueObject::GetIndexOfChildWithName(const ConstString &name) {
552 bool omit_empty_base_classes = true;
553 return GetCompilerType().GetIndexOfChildWithName(name.GetCString(),
554 omit_empty_base_classes);
557 ValueObjectSP ValueObject::GetChildMemberWithName(const ConstString &name,
559 // when getting a child by name, it could be buried inside some base
560 // classes (which really aren't part of the expression path), so we
561 // need a vector of indexes that can get us down to the correct child
562 ValueObjectSP child_sp;
564 // We may need to update our value if we are dynamic
565 if (IsPossibleDynamicType())
566 UpdateValueIfNeeded(false);
568 std::vector<uint32_t> child_indexes;
569 bool omit_empty_base_classes = true;
570 const size_t num_child_indexes =
571 GetCompilerType().GetIndexOfChildMemberWithName(
572 name.GetCString(), omit_empty_base_classes, child_indexes);
573 if (num_child_indexes > 0) {
574 std::vector<uint32_t>::const_iterator pos = child_indexes.begin();
575 std::vector<uint32_t>::const_iterator end = child_indexes.end();
577 child_sp = GetChildAtIndex(*pos, can_create);
578 for (++pos; pos != end; ++pos) {
580 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex(*pos, can_create));
581 child_sp = new_child_sp;
590 size_t ValueObject::GetNumChildren(uint32_t max) {
591 UpdateValueIfNeeded();
593 if (max < UINT32_MAX) {
594 if (m_children_count_valid) {
595 size_t children_count = m_children.GetChildrenCount();
596 return children_count <= max ? children_count : max;
598 return CalculateNumChildren(max);
601 if (!m_children_count_valid) {
602 SetNumChildren(CalculateNumChildren());
604 return m_children.GetChildrenCount();
607 bool ValueObject::MightHaveChildren() {
608 bool has_children = false;
609 const uint32_t type_info = GetTypeInfo();
611 if (type_info & (eTypeHasChildren | eTypeIsPointer | eTypeIsReference))
614 has_children = GetNumChildren() > 0;
619 // Should only be called by ValueObject::GetNumChildren()
620 void ValueObject::SetNumChildren(size_t num_children) {
621 m_children_count_valid = true;
622 m_children.SetChildrenCount(num_children);
625 void ValueObject::SetName(const ConstString &name) { m_name = name; }
627 ValueObject *ValueObject::CreateChildAtIndex(size_t idx,
628 bool synthetic_array_member,
629 int32_t synthetic_index) {
630 ValueObject *valobj = NULL;
632 bool omit_empty_base_classes = true;
633 bool ignore_array_bounds = synthetic_array_member;
634 std::string child_name_str;
635 uint32_t child_byte_size = 0;
636 int32_t child_byte_offset = 0;
637 uint32_t child_bitfield_bit_size = 0;
638 uint32_t child_bitfield_bit_offset = 0;
639 bool child_is_base_class = false;
640 bool child_is_deref_of_parent = false;
641 uint64_t language_flags = 0;
643 const bool transparent_pointers = synthetic_array_member == false;
644 CompilerType child_compiler_type;
646 ExecutionContext exe_ctx(GetExecutionContextRef());
648 child_compiler_type = GetCompilerType().GetChildCompilerTypeAtIndex(
649 &exe_ctx, idx, transparent_pointers, omit_empty_base_classes,
650 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
651 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
652 child_is_deref_of_parent, this, language_flags);
653 if (child_compiler_type) {
655 child_byte_offset += child_byte_size * synthetic_index;
657 ConstString child_name;
658 if (!child_name_str.empty())
659 child_name.SetCString(child_name_str.c_str());
661 valobj = new ValueObjectChild(
662 *this, child_compiler_type, child_name, child_byte_size,
663 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
664 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
667 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
673 bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
674 std::string &destination,
675 lldb::LanguageType lang) {
676 return GetSummaryAsCString(summary_ptr, destination,
677 TypeSummaryOptions().SetLanguage(lang));
680 bool ValueObject::GetSummaryAsCString(TypeSummaryImpl *summary_ptr,
681 std::string &destination,
682 const TypeSummaryOptions &options) {
685 // ideally we would like to bail out if passing NULL, but if we do so
686 // we end up not providing the summary for function pointers anymore
687 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
690 m_is_getting_summary = true;
692 TypeSummaryOptions actual_options(options);
694 if (actual_options.GetLanguage() == lldb::eLanguageTypeUnknown)
695 actual_options.SetLanguage(GetPreferredDisplayLanguage());
697 // this is a hot path in code and we prefer to avoid setting this string all
698 // too often also clearing out other
699 // information that we might care to see in a crash log. might be useful in
700 // very specific situations though.
701 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s.
702 Summary provider's description is %s",
703 GetTypeName().GetCString(),
704 GetName().GetCString(),
705 summary_ptr->GetDescription().c_str());*/
707 if (UpdateValueIfNeeded(false) && summary_ptr) {
708 if (HasSyntheticValue())
709 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on
710 // the synthetic children being
711 // up-to-date (e.g. ${svar%#})
712 summary_ptr->FormatObject(this, destination, actual_options);
714 m_is_getting_summary = false;
715 return !destination.empty();
718 const char *ValueObject::GetSummaryAsCString(lldb::LanguageType lang) {
719 if (UpdateValueIfNeeded(true) && m_summary_str.empty()) {
720 TypeSummaryOptions summary_options;
721 summary_options.SetLanguage(lang);
722 GetSummaryAsCString(GetSummaryFormat().get(), m_summary_str,
725 if (m_summary_str.empty())
727 return m_summary_str.c_str();
730 bool ValueObject::GetSummaryAsCString(std::string &destination,
731 const TypeSummaryOptions &options) {
732 return GetSummaryAsCString(GetSummaryFormat().get(), destination, options);
735 bool ValueObject::IsCStringContainer(bool check_pointer) {
736 CompilerType pointee_or_element_compiler_type;
737 const Flags type_flags(GetTypeInfo(&pointee_or_element_compiler_type));
738 bool is_char_arr_ptr(type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
739 pointee_or_element_compiler_type.IsCharType());
740 if (!is_char_arr_ptr)
744 if (type_flags.Test(eTypeIsArray))
746 addr_t cstr_address = LLDB_INVALID_ADDRESS;
747 AddressType cstr_address_type = eAddressTypeInvalid;
748 cstr_address = GetAddressOf(true, &cstr_address_type);
749 return (cstr_address != LLDB_INVALID_ADDRESS);
752 size_t ValueObject::GetPointeeData(DataExtractor &data, uint32_t item_idx,
753 uint32_t item_count) {
754 CompilerType pointee_or_element_compiler_type;
755 const uint32_t type_info = GetTypeInfo(&pointee_or_element_compiler_type);
756 const bool is_pointer_type = type_info & eTypeIsPointer;
757 const bool is_array_type = type_info & eTypeIsArray;
758 if (!(is_pointer_type || is_array_type))
764 ExecutionContext exe_ctx(GetExecutionContextRef());
766 const uint64_t item_type_size = pointee_or_element_compiler_type.GetByteSize(
767 exe_ctx.GetBestExecutionContextScope());
768 const uint64_t bytes = item_count * item_type_size;
769 const uint64_t offset = item_idx * item_type_size;
771 if (item_idx == 0 && item_count == 1) // simply a deref
773 if (is_pointer_type) {
775 ValueObjectSP pointee_sp = Dereference(error);
776 if (error.Fail() || pointee_sp.get() == NULL)
778 return pointee_sp->GetData(data, error);
780 ValueObjectSP child_sp = GetChildAtIndex(0, true);
781 if (child_sp.get() == NULL)
784 return child_sp->GetData(data, error);
787 } else /* (items > 1) */
790 lldb_private::DataBufferHeap *heap_buf_ptr = NULL;
791 lldb::DataBufferSP data_sp(heap_buf_ptr =
792 new lldb_private::DataBufferHeap());
794 AddressType addr_type;
795 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type)
796 : GetAddressOf(true, &addr_type);
799 case eAddressTypeFile: {
800 ModuleSP module_sp(GetModule());
802 addr = addr + offset;
804 module_sp->ResolveFileAddress(addr, so_addr);
805 ExecutionContext exe_ctx(GetExecutionContextRef());
806 Target *target = exe_ctx.GetTargetPtr();
808 heap_buf_ptr->SetByteSize(bytes);
809 size_t bytes_read = target->ReadMemory(
810 so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
811 if (error.Success()) {
812 data.SetData(data_sp);
818 case eAddressTypeLoad: {
819 ExecutionContext exe_ctx(GetExecutionContextRef());
820 Process *process = exe_ctx.GetProcessPtr();
822 heap_buf_ptr->SetByteSize(bytes);
823 size_t bytes_read = process->ReadMemory(
824 addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
825 if (error.Success() || bytes_read > 0) {
826 data.SetData(data_sp);
831 case eAddressTypeHost: {
832 const uint64_t max_bytes =
833 GetCompilerType().GetByteSize(exe_ctx.GetBestExecutionContextScope());
834 if (max_bytes > offset) {
835 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
836 addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
837 if (addr == 0 || addr == LLDB_INVALID_ADDRESS)
839 heap_buf_ptr->CopyData((uint8_t *)(addr + offset), bytes_read);
840 data.SetData(data_sp);
844 case eAddressTypeInvalid:
851 uint64_t ValueObject::GetData(DataExtractor &data, Error &error) {
852 UpdateValueIfNeeded(false);
853 ExecutionContext exe_ctx(GetExecutionContextRef());
854 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
856 if (m_data.GetByteSize()) {
859 return data.GetByteSize();
864 data.SetAddressByteSize(m_data.GetAddressByteSize());
865 data.SetByteOrder(m_data.GetByteOrder());
866 return data.GetByteSize();
869 bool ValueObject::SetData(DataExtractor &data, Error &error) {
871 // Make sure our value is up to date first so that our location and location
873 if (!UpdateValueIfNeeded(false)) {
874 error.SetErrorString("unable to read value");
879 const Encoding encoding = GetCompilerType().GetEncoding(count);
881 const size_t byte_size = GetByteSize();
883 Value::ValueType value_type = m_value.GetValueType();
885 switch (value_type) {
886 case Value::eValueTypeScalar: {
888 m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
890 if (!set_error.Success()) {
891 error.SetErrorStringWithFormat("unable to set scalar value: %s",
892 set_error.AsCString());
896 case Value::eValueTypeLoadAddress: {
897 // If it is a load address, then the scalar value is the storage location
898 // of the data, and we have to shove this value down to that load location.
899 ExecutionContext exe_ctx(GetExecutionContextRef());
900 Process *process = exe_ctx.GetProcessPtr();
902 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
903 size_t bytes_written = process->WriteMemory(
904 target_addr, data.GetDataStart(), byte_size, error);
905 if (!error.Success())
907 if (bytes_written != byte_size) {
908 error.SetErrorString("unable to write value to memory");
913 case Value::eValueTypeHostAddress: {
914 // If it is a host address, then we stuff the scalar as a DataBuffer into
916 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
917 m_data.SetData(buffer_sp, 0);
918 data.CopyByteOrderedData(0, byte_size,
919 const_cast<uint8_t *>(m_data.GetDataStart()),
920 byte_size, m_data.GetByteOrder());
921 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
923 case Value::eValueTypeFileAddress:
924 case Value::eValueTypeVector:
928 // If we have reached this point, then we have successfully changed the value.
933 static bool CopyStringDataToBufferSP(const StreamString &source,
934 lldb::DataBufferSP &destination) {
935 destination.reset(new DataBufferHeap(source.GetSize() + 1, 0));
936 memcpy(destination->GetBytes(), source.GetString().data(), source.GetSize());
940 std::pair<size_t, bool>
941 ValueObject::ReadPointedString(lldb::DataBufferSP &buffer_sp, Error &error,
942 uint32_t max_length, bool honor_array,
943 Format item_format) {
944 bool was_capped = false;
946 ExecutionContext exe_ctx(GetExecutionContextRef());
947 Target *target = exe_ctx.GetTargetPtr();
950 s << "<no target to read from>";
951 error.SetErrorString("no target to read from");
952 CopyStringDataToBufferSP(s, buffer_sp);
953 return {0, was_capped};
957 max_length = target->GetMaximumSizeOfStringSummary();
959 size_t bytes_read = 0;
960 size_t total_bytes_read = 0;
962 CompilerType compiler_type = GetCompilerType();
963 CompilerType elem_or_pointee_compiler_type;
964 const Flags type_flags(GetTypeInfo(&elem_or_pointee_compiler_type));
965 if (type_flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
966 elem_or_pointee_compiler_type.IsCharType()) {
967 addr_t cstr_address = LLDB_INVALID_ADDRESS;
968 AddressType cstr_address_type = eAddressTypeInvalid;
971 bool capped_data = false;
972 const bool is_array = type_flags.Test(eTypeIsArray);
975 uint64_t array_size = 0;
976 if (compiler_type.IsArrayType(NULL, &array_size, NULL)) {
977 cstr_len = array_size;
978 if (cstr_len > max_length) {
980 cstr_len = max_length;
983 cstr_address = GetAddressOf(true, &cstr_address_type);
986 cstr_address = GetPointerValue(&cstr_address_type);
989 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS) {
990 if (cstr_address_type == eAddressTypeHost && is_array) {
991 const char *cstr = GetDataExtractor().PeekCStr(0);
992 if (cstr == nullptr) {
993 s << "<invalid address>";
994 error.SetErrorString("invalid address");
995 CopyStringDataToBufferSP(s, buffer_sp);
996 return {0, was_capped};
998 buffer_sp.reset(new DataBufferHeap(cstr_len, 0));
999 memcpy(buffer_sp->GetBytes(), cstr, cstr_len);
1000 return {cstr_len, was_capped};
1002 s << "<invalid address>";
1003 error.SetErrorString("invalid address");
1004 CopyStringDataToBufferSP(s, buffer_sp);
1005 return {0, was_capped};
1009 Address cstr_so_addr(cstr_address);
1011 if (cstr_len > 0 && honor_array) {
1012 // I am using GetPointeeData() here to abstract the fact that some
1013 // ValueObjects are actually frozen pointers in the host
1014 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1015 // automatically takes care of this
1016 GetPointeeData(data, 0, cstr_len);
1018 if ((bytes_read = data.GetByteSize()) > 0) {
1019 total_bytes_read = bytes_read;
1020 for (size_t offset = 0; offset < bytes_read; offset++)
1021 s.Printf("%c", *data.PeekData(offset, 1));
1026 cstr_len = max_length;
1027 const size_t k_max_buf_size = 64;
1031 int cstr_len_displayed = -1;
1032 bool capped_cstr = false;
1033 // I am using GetPointeeData() here to abstract the fact that some
1034 // ValueObjects are actually frozen pointers in the host
1035 // but the pointed-to data lives in the debuggee, and GetPointeeData()
1036 // automatically takes care of this
1037 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0) {
1038 total_bytes_read += bytes_read;
1039 const char *cstr = data.PeekCStr(0);
1040 size_t len = strnlen(cstr, k_max_buf_size);
1041 if (cstr_len_displayed < 0)
1042 cstr_len_displayed = len;
1046 cstr_len_displayed += len;
1047 if (len > bytes_read)
1052 for (size_t offset = 0; offset < bytes_read; offset++)
1053 s.Printf("%c", *data.PeekData(offset, 1));
1055 if (len < k_max_buf_size)
1058 if (len >= cstr_len) {
1067 if (cstr_len_displayed >= 0) {
1073 error.SetErrorString("not a string object");
1074 s << "<not a string object>";
1076 CopyStringDataToBufferSP(s, buffer_sp);
1077 return {total_bytes_read, was_capped};
1080 std::pair<TypeValidatorResult, std::string> ValueObject::GetValidationStatus() {
1081 if (!UpdateValueIfNeeded(true))
1082 return {TypeValidatorResult::Success,
1083 ""}; // not the validator's job to discuss update problems
1085 if (m_validation_result.hasValue())
1086 return m_validation_result.getValue();
1088 if (!m_type_validator_sp)
1089 return {TypeValidatorResult::Success, ""}; // no validator no failure
1091 auto outcome = m_type_validator_sp->FormatObject(this);
1093 return (m_validation_result = {outcome.m_result, outcome.m_message})
1097 const char *ValueObject::GetObjectDescription() {
1099 if (!UpdateValueIfNeeded(true))
1102 if (!m_object_desc_str.empty())
1103 return m_object_desc_str.c_str();
1105 ExecutionContext exe_ctx(GetExecutionContextRef());
1106 Process *process = exe_ctx.GetProcessPtr();
1107 if (process == NULL)
1112 LanguageType language = GetObjectRuntimeLanguage();
1113 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1115 if (runtime == NULL) {
1116 // Aw, hell, if the things a pointer, or even just an integer, let's try
1118 CompilerType compiler_type = GetCompilerType();
1119 if (compiler_type) {
1121 if (compiler_type.IsIntegerType(is_signed) ||
1122 compiler_type.IsPointerType()) {
1123 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
1128 if (runtime && runtime->GetObjectDescription(s, *this)) {
1129 m_object_desc_str.append(s.GetString());
1132 if (m_object_desc_str.empty())
1135 return m_object_desc_str.c_str();
1138 bool ValueObject::GetValueAsCString(const lldb_private::TypeFormatImpl &format,
1139 std::string &destination) {
1140 if (UpdateValueIfNeeded(false))
1141 return format.FormatObject(this, destination);
1146 bool ValueObject::GetValueAsCString(lldb::Format format,
1147 std::string &destination) {
1148 return GetValueAsCString(TypeFormatImpl_Format(format), destination);
1151 const char *ValueObject::GetValueAsCString() {
1152 if (UpdateValueIfNeeded(true)) {
1153 lldb::TypeFormatImplSP format_sp;
1154 lldb::Format my_format = GetFormat();
1155 if (my_format == lldb::eFormatDefault) {
1156 if (m_type_format_sp)
1157 format_sp = m_type_format_sp;
1159 if (m_is_bitfield_for_scalar)
1160 my_format = eFormatUnsigned;
1162 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo) {
1163 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1165 my_format = reg_info->format;
1167 my_format = GetValue().GetCompilerType().GetFormat();
1172 if (my_format != m_last_format || m_value_str.empty()) {
1173 m_last_format = my_format;
1175 format_sp.reset(new TypeFormatImpl_Format(my_format));
1176 if (GetValueAsCString(*format_sp.get(), m_value_str)) {
1177 if (!m_value_did_change && m_old_value_valid) {
1178 // The value was gotten successfully, so we consider the
1179 // value as changed if the value string differs
1180 SetValueDidChange(m_old_value_str != m_value_str);
1185 if (m_value_str.empty())
1187 return m_value_str.c_str();
1190 // if > 8bytes, 0 is returned. this method should mostly be used
1191 // to read address values out of pointers
1192 uint64_t ValueObject::GetValueAsUnsigned(uint64_t fail_value, bool *success) {
1193 // If our byte size is zero this is an aggregate type that has children
1194 if (CanProvideValue()) {
1196 if (ResolveValue(scalar)) {
1199 return scalar.ULongLong(fail_value);
1201 // fallthrough, otherwise...
1209 int64_t ValueObject::GetValueAsSigned(int64_t fail_value, bool *success) {
1210 // If our byte size is zero this is an aggregate type that has children
1211 if (CanProvideValue()) {
1213 if (ResolveValue(scalar)) {
1216 return scalar.SLongLong(fail_value);
1218 // fallthrough, otherwise...
1226 // if any more "special cases" are added to
1227 // ValueObject::DumpPrintableRepresentation() please keep
1228 // this call up to date by returning true for your new special cases. We will
1230 // to checking this call result before trying to display special cases
1231 bool ValueObject::HasSpecialPrintableRepresentation(
1232 ValueObjectRepresentationStyle val_obj_display, Format custom_format) {
1233 Flags flags(GetTypeInfo());
1234 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1235 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1236 if (IsCStringContainer(true) &&
1237 (custom_format == eFormatCString || custom_format == eFormatCharArray ||
1238 custom_format == eFormatChar || custom_format == eFormatVectorOfChar))
1241 if (flags.Test(eTypeIsArray)) {
1242 if ((custom_format == eFormatBytes) ||
1243 (custom_format == eFormatBytesWithASCII))
1246 if ((custom_format == eFormatVectorOfChar) ||
1247 (custom_format == eFormatVectorOfFloat32) ||
1248 (custom_format == eFormatVectorOfFloat64) ||
1249 (custom_format == eFormatVectorOfSInt16) ||
1250 (custom_format == eFormatVectorOfSInt32) ||
1251 (custom_format == eFormatVectorOfSInt64) ||
1252 (custom_format == eFormatVectorOfSInt8) ||
1253 (custom_format == eFormatVectorOfUInt128) ||
1254 (custom_format == eFormatVectorOfUInt16) ||
1255 (custom_format == eFormatVectorOfUInt32) ||
1256 (custom_format == eFormatVectorOfUInt64) ||
1257 (custom_format == eFormatVectorOfUInt8))
1264 bool ValueObject::DumpPrintableRepresentation(
1265 Stream &s, ValueObjectRepresentationStyle val_obj_display,
1266 Format custom_format, PrintableRepresentationSpecialCases special,
1267 bool do_dump_error) {
1269 Flags flags(GetTypeInfo());
1271 bool allow_special =
1272 (special == ValueObject::PrintableRepresentationSpecialCases::eAllow);
1273 const bool only_special = false;
1275 if (allow_special) {
1276 if (flags.AnySet(eTypeIsArray | eTypeIsPointer) &&
1277 val_obj_display == ValueObject::eValueObjectRepresentationStyleValue) {
1278 // when being asked to get a printable display an array or pointer type
1280 // try to "do the right thing"
1282 if (IsCStringContainer(true) &&
1283 (custom_format == eFormatCString ||
1284 custom_format == eFormatCharArray || custom_format == eFormatChar ||
1286 eFormatVectorOfChar)) // print char[] & char* directly
1289 lldb::DataBufferSP buffer_sp;
1290 std::pair<size_t, bool> read_string = ReadPointedString(
1291 buffer_sp, error, 0, (custom_format == eFormatVectorOfChar) ||
1292 (custom_format == eFormatCharArray));
1293 lldb_private::formatters::StringPrinter::
1294 ReadBufferAndDumpToStreamOptions options(*this);
1295 options.SetData(DataExtractor(
1296 buffer_sp, lldb::eByteOrderInvalid,
1297 8)); // none of this matters for a string - pass some defaults
1298 options.SetStream(&s);
1299 options.SetPrefixToken(0);
1300 options.SetQuote('"');
1301 options.SetSourceSize(buffer_sp->GetByteSize());
1302 options.SetIsTruncated(read_string.second);
1303 formatters::StringPrinter::ReadBufferAndDumpToStream<
1304 lldb_private::formatters::StringPrinter::StringElementType::ASCII>(
1306 return !error.Fail();
1309 if (custom_format == eFormatEnum)
1312 // this only works for arrays, because I have no way to know when
1313 // the pointed memory ends, and no special \0 end of data marker
1314 if (flags.Test(eTypeIsArray)) {
1315 if ((custom_format == eFormatBytes) ||
1316 (custom_format == eFormatBytesWithASCII)) {
1317 const size_t count = GetNumChildren();
1320 for (size_t low = 0; low < count; low++) {
1325 ValueObjectSP child = GetChildAtIndex(low, true);
1327 s << "<invalid child>";
1330 child->DumpPrintableRepresentation(
1331 s, ValueObject::eValueObjectRepresentationStyleValue,
1340 if ((custom_format == eFormatVectorOfChar) ||
1341 (custom_format == eFormatVectorOfFloat32) ||
1342 (custom_format == eFormatVectorOfFloat64) ||
1343 (custom_format == eFormatVectorOfSInt16) ||
1344 (custom_format == eFormatVectorOfSInt32) ||
1345 (custom_format == eFormatVectorOfSInt64) ||
1346 (custom_format == eFormatVectorOfSInt8) ||
1347 (custom_format == eFormatVectorOfUInt128) ||
1348 (custom_format == eFormatVectorOfUInt16) ||
1349 (custom_format == eFormatVectorOfUInt32) ||
1350 (custom_format == eFormatVectorOfUInt64) ||
1351 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes
1352 // with ASCII or any vector
1353 // format should be printed
1356 const size_t count = GetNumChildren();
1358 Format format = FormatManager::GetSingleItemFormat(custom_format);
1361 for (size_t low = 0; low < count; low++) {
1366 ValueObjectSP child = GetChildAtIndex(low, true);
1368 s << "<invalid child>";
1371 child->DumpPrintableRepresentation(
1372 s, ValueObject::eValueObjectRepresentationStyleValue, format);
1381 if ((custom_format == eFormatBoolean) ||
1382 (custom_format == eFormatBinary) || (custom_format == eFormatChar) ||
1383 (custom_format == eFormatCharPrintable) ||
1384 (custom_format == eFormatComplexFloat) ||
1385 (custom_format == eFormatDecimal) || (custom_format == eFormatHex) ||
1386 (custom_format == eFormatHexUppercase) ||
1387 (custom_format == eFormatFloat) || (custom_format == eFormatOctal) ||
1388 (custom_format == eFormatOSType) ||
1389 (custom_format == eFormatUnicode16) ||
1390 (custom_format == eFormatUnicode32) ||
1391 (custom_format == eFormatUnsigned) ||
1392 (custom_format == eFormatPointer) ||
1393 (custom_format == eFormatComplexInteger) ||
1394 (custom_format == eFormatComplex) ||
1395 (custom_format == eFormatDefault)) // use the [] operator
1403 bool var_success = false;
1406 llvm::StringRef str;
1408 // this is a local stream that we are using to ensure that the data pointed
1409 // to by cstr survives long enough for us to copy it to its destination - it
1410 // is necessary to have this temporary storage area for cases where our
1411 // desired output is not backed by some other longer-term storage
1414 if (custom_format != eFormatInvalid)
1415 SetFormat(custom_format);
1417 switch (val_obj_display) {
1418 case eValueObjectRepresentationStyleValue:
1419 str = GetValueAsCString();
1422 case eValueObjectRepresentationStyleSummary:
1423 str = GetSummaryAsCString();
1426 case eValueObjectRepresentationStyleLanguageSpecific:
1427 str = GetObjectDescription();
1430 case eValueObjectRepresentationStyleLocation:
1431 str = GetLocationAsCString();
1434 case eValueObjectRepresentationStyleChildrenCount:
1435 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
1436 str = strm.GetString();
1439 case eValueObjectRepresentationStyleType:
1440 str = GetTypeName().GetStringRef();
1443 case eValueObjectRepresentationStyleName:
1444 str = GetName().GetStringRef();
1447 case eValueObjectRepresentationStyleExpressionPath:
1448 GetExpressionPath(strm, false);
1449 str = strm.GetString();
1454 if (val_obj_display == eValueObjectRepresentationStyleValue)
1455 str = GetSummaryAsCString();
1456 else if (val_obj_display == eValueObjectRepresentationStyleSummary) {
1457 if (!CanProvideValue()) {
1458 strm.Printf("%s @ %s", GetTypeName().AsCString(),
1459 GetLocationAsCString());
1460 str = strm.GetString();
1462 str = GetValueAsCString();
1469 if (m_error.Fail()) {
1471 s.Printf("<%s>", m_error.AsCString());
1474 } else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1475 s.PutCString("<no summary available>");
1476 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1477 s.PutCString("<no value available>");
1478 else if (val_obj_display ==
1479 eValueObjectRepresentationStyleLanguageSpecific)
1480 s.PutCString("<not a valid Objective-C object>"); // edit this if we
1481 // have other runtimes
1485 s.PutCString("<no printable representation>");
1488 // we should only return false here if we could not do *anything*
1489 // even if we have an error message as output, that's a success
1490 // from our callers' perspective, so return true
1493 if (custom_format != eFormatInvalid)
1494 SetFormat(eFormatDefault);
1500 addr_t ValueObject::GetAddressOf(bool scalar_is_load_address,
1501 AddressType *address_type) {
1502 // Can't take address of a bitfield
1504 return LLDB_INVALID_ADDRESS;
1506 if (!UpdateValueIfNeeded(false))
1507 return LLDB_INVALID_ADDRESS;
1509 switch (m_value.GetValueType()) {
1510 case Value::eValueTypeScalar:
1511 case Value::eValueTypeVector:
1512 if (scalar_is_load_address) {
1514 *address_type = eAddressTypeLoad;
1515 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1519 case Value::eValueTypeLoadAddress:
1520 case Value::eValueTypeFileAddress: {
1522 *address_type = m_value.GetValueAddressType();
1523 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1525 case Value::eValueTypeHostAddress: {
1527 *address_type = m_value.GetValueAddressType();
1528 return LLDB_INVALID_ADDRESS;
1532 *address_type = eAddressTypeInvalid;
1533 return LLDB_INVALID_ADDRESS;
1536 addr_t ValueObject::GetPointerValue(AddressType *address_type) {
1537 addr_t address = LLDB_INVALID_ADDRESS;
1539 *address_type = eAddressTypeInvalid;
1541 if (!UpdateValueIfNeeded(false))
1544 switch (m_value.GetValueType()) {
1545 case Value::eValueTypeScalar:
1546 case Value::eValueTypeVector:
1547 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1550 case Value::eValueTypeHostAddress:
1551 case Value::eValueTypeLoadAddress:
1552 case Value::eValueTypeFileAddress: {
1553 lldb::offset_t data_offset = 0;
1554 address = m_data.GetPointer(&data_offset);
1559 *address_type = GetAddressTypeOfChildren();
1564 bool ValueObject::SetValueFromCString(const char *value_str, Error &error) {
1566 // Make sure our value is up to date first so that our location and location
1568 if (!UpdateValueIfNeeded(false)) {
1569 error.SetErrorString("unable to read value");
1574 const Encoding encoding = GetCompilerType().GetEncoding(count);
1576 const size_t byte_size = GetByteSize();
1578 Value::ValueType value_type = m_value.GetValueType();
1580 if (value_type == Value::eValueTypeScalar) {
1581 // If the value is already a scalar, then let the scalar change itself:
1582 m_value.GetScalar().SetValueFromCString(value_str, encoding, byte_size);
1583 } else if (byte_size <= 16) {
1584 // If the value fits in a scalar, then make a new scalar and again let the
1585 // scalar code do the conversion, then figure out where to put the new
1588 error = new_scalar.SetValueFromCString(value_str, encoding, byte_size);
1589 if (error.Success()) {
1590 switch (value_type) {
1591 case Value::eValueTypeLoadAddress: {
1592 // If it is a load address, then the scalar value is the storage
1594 // of the data, and we have to shove this value down to that load
1596 ExecutionContext exe_ctx(GetExecutionContextRef());
1597 Process *process = exe_ctx.GetProcessPtr();
1599 addr_t target_addr =
1600 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1601 size_t bytes_written = process->WriteScalarToMemory(
1602 target_addr, new_scalar, byte_size, error);
1603 if (!error.Success())
1605 if (bytes_written != byte_size) {
1606 error.SetErrorString("unable to write value to memory");
1611 case Value::eValueTypeHostAddress: {
1612 // If it is a host address, then we stuff the scalar as a DataBuffer
1613 // into the Value's data.
1614 DataExtractor new_data;
1615 new_data.SetByteOrder(m_data.GetByteOrder());
1617 DataBufferSP buffer_sp(new DataBufferHeap(byte_size, 0));
1618 m_data.SetData(buffer_sp, 0);
1619 bool success = new_scalar.GetData(new_data);
1621 new_data.CopyByteOrderedData(
1622 0, byte_size, const_cast<uint8_t *>(m_data.GetDataStart()),
1623 byte_size, m_data.GetByteOrder());
1625 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1628 case Value::eValueTypeFileAddress:
1629 case Value::eValueTypeScalar:
1630 case Value::eValueTypeVector:
1637 // We don't support setting things bigger than a scalar at present.
1638 error.SetErrorString("unable to write aggregate data type");
1642 // If we have reached this point, then we have successfully changed the value.
1647 bool ValueObject::GetDeclaration(Declaration &decl) {
1652 ConstString ValueObject::GetTypeName() {
1653 return GetCompilerType().GetConstTypeName();
1656 ConstString ValueObject::GetDisplayTypeName() { return GetTypeName(); }
1658 ConstString ValueObject::GetQualifiedTypeName() {
1659 return GetCompilerType().GetConstQualifiedTypeName();
1662 LanguageType ValueObject::GetObjectRuntimeLanguage() {
1663 return GetCompilerType().GetMinimumLanguage();
1666 void ValueObject::AddSyntheticChild(const ConstString &key,
1667 ValueObject *valobj) {
1668 m_synthetic_children[key] = valobj;
1671 ValueObjectSP ValueObject::GetSyntheticChild(const ConstString &key) const {
1672 ValueObjectSP synthetic_child_sp;
1673 std::map<ConstString, ValueObject *>::const_iterator pos =
1674 m_synthetic_children.find(key);
1675 if (pos != m_synthetic_children.end())
1676 synthetic_child_sp = pos->second->GetSP();
1677 return synthetic_child_sp;
1681 ValueObject::GetTypeInfo(CompilerType *pointee_or_element_compiler_type) {
1682 return GetCompilerType().GetTypeInfo(pointee_or_element_compiler_type);
1685 bool ValueObject::IsPointerType() { return GetCompilerType().IsPointerType(); }
1687 bool ValueObject::IsArrayType() {
1688 return GetCompilerType().IsArrayType(NULL, NULL, NULL);
1691 bool ValueObject::IsScalarType() { return GetCompilerType().IsScalarType(); }
1693 bool ValueObject::IsIntegerType(bool &is_signed) {
1694 return GetCompilerType().IsIntegerType(is_signed);
1697 bool ValueObject::IsPointerOrReferenceType() {
1698 return GetCompilerType().IsPointerOrReferenceType();
1701 bool ValueObject::IsPossibleDynamicType() {
1702 ExecutionContext exe_ctx(GetExecutionContextRef());
1703 Process *process = exe_ctx.GetProcessPtr();
1705 return process->IsPossibleDynamicValue(*this);
1707 return GetCompilerType().IsPossibleDynamicType(NULL, true, true);
1710 bool ValueObject::IsRuntimeSupportValue() {
1711 Process *process(GetProcessSP().get());
1713 LanguageRuntime *runtime =
1714 process->GetLanguageRuntime(GetObjectRuntimeLanguage());
1716 runtime = process->GetObjCLanguageRuntime();
1718 return runtime->IsRuntimeSupportValue(*this);
1723 bool ValueObject::IsNilReference() {
1724 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1725 return language->IsNilReference(*this);
1730 bool ValueObject::IsUninitializedReference() {
1731 if (Language *language = Language::FindPlugin(GetObjectRuntimeLanguage())) {
1732 return language->IsUninitializedReference(*this);
1737 // This allows you to create an array member using and index
1738 // that doesn't not fall in the normal bounds of the array.
1739 // Many times structure can be defined as:
1740 // struct Collection
1742 // uint32_t item_count;
1743 // Item item_array[0];
1745 // The size of the "item_array" is 1, but many times in practice
1746 // there are more items in "item_array".
1748 ValueObjectSP ValueObject::GetSyntheticArrayMember(size_t index,
1750 ValueObjectSP synthetic_child_sp;
1751 if (IsPointerType() || IsArrayType()) {
1753 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
1754 ConstString index_const_str(index_str);
1755 // Check if we have already created a synthetic array member in this
1756 // valid object. If we have we will re-use it.
1757 synthetic_child_sp = GetSyntheticChild(index_const_str);
1758 if (!synthetic_child_sp) {
1759 ValueObject *synthetic_child;
1760 // We haven't made a synthetic array member for INDEX yet, so
1761 // lets make one and cache it for any future reference.
1762 synthetic_child = CreateChildAtIndex(0, true, index);
1764 // Cache the value if we got one back...
1765 if (synthetic_child) {
1766 AddSyntheticChild(index_const_str, synthetic_child);
1767 synthetic_child_sp = synthetic_child->GetSP();
1768 synthetic_child_sp->SetName(ConstString(index_str));
1769 synthetic_child_sp->m_is_array_item_for_pointer = true;
1773 return synthetic_child_sp;
1776 ValueObjectSP ValueObject::GetSyntheticBitFieldChild(uint32_t from, uint32_t to,
1778 ValueObjectSP synthetic_child_sp;
1779 if (IsScalarType()) {
1781 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1782 ConstString index_const_str(index_str);
1783 // Check if we have already created a synthetic array member in this
1784 // valid object. If we have we will re-use it.
1785 synthetic_child_sp = GetSyntheticChild(index_const_str);
1786 if (!synthetic_child_sp) {
1787 uint32_t bit_field_size = to - from + 1;
1788 uint32_t bit_field_offset = from;
1789 if (GetDataExtractor().GetByteOrder() == eByteOrderBig)
1791 GetByteSize() * 8 - bit_field_size - bit_field_offset;
1792 // We haven't made a synthetic array member for INDEX yet, so
1793 // lets make one and cache it for any future reference.
1794 ValueObjectChild *synthetic_child = new ValueObjectChild(
1795 *this, GetCompilerType(), index_const_str, GetByteSize(), 0,
1796 bit_field_size, bit_field_offset, false, false, eAddressTypeInvalid,
1799 // Cache the value if we got one back...
1800 if (synthetic_child) {
1801 AddSyntheticChild(index_const_str, synthetic_child);
1802 synthetic_child_sp = synthetic_child->GetSP();
1803 synthetic_child_sp->SetName(ConstString(index_str));
1804 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1808 return synthetic_child_sp;
1811 ValueObjectSP ValueObject::GetSyntheticChildAtOffset(
1812 uint32_t offset, const CompilerType &type, bool can_create,
1813 ConstString name_const_str) {
1815 ValueObjectSP synthetic_child_sp;
1817 if (name_const_str.IsEmpty()) {
1819 snprintf(name_str, sizeof(name_str), "@%i", offset);
1820 name_const_str.SetCString(name_str);
1823 // Check if we have already created a synthetic array member in this
1824 // valid object. If we have we will re-use it.
1825 synthetic_child_sp = GetSyntheticChild(name_const_str);
1827 if (synthetic_child_sp.get())
1828 return synthetic_child_sp;
1831 return ValueObjectSP();
1833 ExecutionContext exe_ctx(GetExecutionContextRef());
1835 ValueObjectChild *synthetic_child = new ValueObjectChild(
1836 *this, type, name_const_str,
1837 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1838 false, false, eAddressTypeInvalid, 0);
1839 if (synthetic_child) {
1840 AddSyntheticChild(name_const_str, synthetic_child);
1841 synthetic_child_sp = synthetic_child->GetSP();
1842 synthetic_child_sp->SetName(name_const_str);
1843 synthetic_child_sp->m_is_child_at_offset = true;
1845 return synthetic_child_sp;
1848 ValueObjectSP ValueObject::GetSyntheticBase(uint32_t offset,
1849 const CompilerType &type,
1851 ConstString name_const_str) {
1852 ValueObjectSP synthetic_child_sp;
1854 if (name_const_str.IsEmpty()) {
1856 snprintf(name_str, sizeof(name_str), "base%s@%i",
1857 type.GetTypeName().AsCString("<unknown>"), offset);
1858 name_const_str.SetCString(name_str);
1861 // Check if we have already created a synthetic array member in this
1862 // valid object. If we have we will re-use it.
1863 synthetic_child_sp = GetSyntheticChild(name_const_str);
1865 if (synthetic_child_sp.get())
1866 return synthetic_child_sp;
1869 return ValueObjectSP();
1871 const bool is_base_class = true;
1873 ExecutionContext exe_ctx(GetExecutionContextRef());
1875 ValueObjectChild *synthetic_child = new ValueObjectChild(
1876 *this, type, name_const_str,
1877 type.GetByteSize(exe_ctx.GetBestExecutionContextScope()), offset, 0, 0,
1878 is_base_class, false, eAddressTypeInvalid, 0);
1879 if (synthetic_child) {
1880 AddSyntheticChild(name_const_str, synthetic_child);
1881 synthetic_child_sp = synthetic_child->GetSP();
1882 synthetic_child_sp->SetName(name_const_str);
1884 return synthetic_child_sp;
1887 // your expression path needs to have a leading . or ->
1888 // (unless it somehow "looks like" an array, in which case it has
1889 // a leading [ symbol). while the [ is meaningful and should be shown
1890 // to the user, . and -> are just parser design, but by no means
1891 // added information for the user.. strip them off
1892 static const char *SkipLeadingExpressionPathSeparators(const char *expression) {
1893 if (!expression || !expression[0])
1895 if (expression[0] == '.')
1896 return expression + 1;
1897 if (expression[0] == '-' && expression[1] == '>')
1898 return expression + 2;
1903 ValueObject::GetSyntheticExpressionPathChild(const char *expression,
1905 ValueObjectSP synthetic_child_sp;
1906 ConstString name_const_string(expression);
1907 // Check if we have already created a synthetic array member in this
1908 // valid object. If we have we will re-use it.
1909 synthetic_child_sp = GetSyntheticChild(name_const_string);
1910 if (!synthetic_child_sp) {
1911 // We haven't made a synthetic array member for expression yet, so
1912 // lets make one and cache it for any future reference.
1913 synthetic_child_sp = GetValueForExpressionPath(
1914 expression, NULL, NULL,
1915 GetValueForExpressionPathOptions().SetSyntheticChildrenTraversal(
1916 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
1919 // Cache the value if we got one back...
1920 if (synthetic_child_sp.get()) {
1921 // FIXME: this causes a "real" child to end up with its name changed to
1922 // the contents of expression
1923 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
1924 synthetic_child_sp->SetName(
1925 ConstString(SkipLeadingExpressionPathSeparators(expression)));
1928 return synthetic_child_sp;
1931 void ValueObject::CalculateSyntheticValue(bool use_synthetic) {
1932 if (use_synthetic == false)
1935 TargetSP target_sp(GetTargetSP());
1936 if (target_sp && target_sp->GetEnableSyntheticValue() == false) {
1937 m_synthetic_value = NULL;
1941 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
1943 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
1946 if (m_synthetic_children_sp.get() == NULL)
1949 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
1952 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
1955 void ValueObject::CalculateDynamicValue(DynamicValueType use_dynamic) {
1956 if (use_dynamic == eNoDynamicValues)
1959 if (!m_dynamic_value && !IsDynamic()) {
1960 ExecutionContext exe_ctx(GetExecutionContextRef());
1961 Process *process = exe_ctx.GetProcessPtr();
1962 if (process && process->IsPossibleDynamicValue(*this)) {
1963 ClearDynamicTypeInformation();
1964 m_dynamic_value = new ValueObjectDynamicValue(*this, use_dynamic);
1969 ValueObjectSP ValueObject::GetDynamicValue(DynamicValueType use_dynamic) {
1970 if (use_dynamic == eNoDynamicValues)
1971 return ValueObjectSP();
1973 if (!IsDynamic() && m_dynamic_value == NULL) {
1974 CalculateDynamicValue(use_dynamic);
1976 if (m_dynamic_value)
1977 return m_dynamic_value->GetSP();
1979 return ValueObjectSP();
1982 ValueObjectSP ValueObject::GetStaticValue() { return GetSP(); }
1984 lldb::ValueObjectSP ValueObject::GetNonSyntheticValue() { return GetSP(); }
1986 ValueObjectSP ValueObject::GetSyntheticValue(bool use_synthetic) {
1987 if (use_synthetic == false)
1988 return ValueObjectSP();
1990 CalculateSyntheticValue(use_synthetic);
1992 if (m_synthetic_value)
1993 return m_synthetic_value->GetSP();
1995 return ValueObjectSP();
1998 bool ValueObject::HasSyntheticValue() {
1999 UpdateFormatsIfNeeded();
2001 if (m_synthetic_children_sp.get() == NULL)
2004 CalculateSyntheticValue(true);
2006 if (m_synthetic_value)
2012 bool ValueObject::GetBaseClassPath(Stream &s) {
2013 if (IsBaseClass()) {
2014 bool parent_had_base_class =
2015 GetParent() && GetParent()->GetBaseClassPath(s);
2016 CompilerType compiler_type = GetCompilerType();
2017 std::string cxx_class_name;
2018 bool this_had_base_class =
2019 ClangASTContext::GetCXXClassName(compiler_type, cxx_class_name);
2020 if (this_had_base_class) {
2021 if (parent_had_base_class)
2023 s.PutCString(cxx_class_name);
2025 return parent_had_base_class || this_had_base_class;
2030 ValueObject *ValueObject::GetNonBaseClassParent() {
2032 if (GetParent()->IsBaseClass())
2033 return GetParent()->GetNonBaseClassParent();
2040 bool ValueObject::IsBaseClass(uint32_t &depth) {
2041 if (!IsBaseClass()) {
2046 GetParent()->IsBaseClass(depth);
2050 // TODO: a base of no parent? weird..
2055 void ValueObject::GetExpressionPath(Stream &s, bool qualify_cxx_base_classes,
2056 GetExpressionPathFormat epformat) {
2057 // synthetic children do not actually "exist" as part of the hierarchy, and
2058 // sometimes they are consed up in ways
2059 // that don't make sense from an underlying language/API standpoint. So, use a
2060 // special code path here to return
2061 // something that can hopefully be used in expression
2062 if (m_is_synthetic_children_generated) {
2063 UpdateValueIfNeeded();
2065 if (m_value.GetValueType() == Value::eValueTypeLoadAddress) {
2066 if (IsPointerOrReferenceType()) {
2067 s.Printf("((%s)0x%" PRIx64 ")", GetTypeName().AsCString("void"),
2068 GetValueAsUnsigned(0));
2071 uint64_t load_addr =
2072 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
2073 if (load_addr != LLDB_INVALID_ADDRESS) {
2074 s.Printf("(*( (%s *)0x%" PRIx64 "))", GetTypeName().AsCString("void"),
2081 if (CanProvideValue()) {
2082 s.Printf("((%s)%s)", GetTypeName().AsCString("void"),
2083 GetValueAsCString());
2090 const bool is_deref_of_parent = IsDereferenceOfParent();
2092 if (is_deref_of_parent &&
2093 epformat == eGetExpressionPathFormatDereferencePointers) {
2094 // this is the original format of GetExpressionPath() producing code like
2095 // *(a_ptr).memberName, which is entirely
2096 // fine, until you put this into
2097 // StackFrame::GetValueForVariableExpressionPath() which prefers to see
2098 // a_ptr->memberName.
2099 // the eHonorPointers mode is meant to produce strings in this latter format
2103 ValueObject *parent = GetParent();
2106 parent->GetExpressionPath(s, qualify_cxx_base_classes, epformat);
2108 // if we are a deref_of_parent just because we are synthetic array
2109 // members made up to allow ptr[%d] syntax to work in variable
2110 // printing, then add our name ([%d]) to the expression path
2111 if (m_is_array_item_for_pointer &&
2112 epformat == eGetExpressionPathFormatHonorPointers)
2113 s.PutCString(m_name.AsCString());
2115 if (!IsBaseClass()) {
2116 if (!is_deref_of_parent) {
2117 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2118 if (non_base_class_parent &&
2119 !non_base_class_parent->GetName().IsEmpty()) {
2120 CompilerType non_base_class_parent_compiler_type =
2121 non_base_class_parent->GetCompilerType();
2122 if (non_base_class_parent_compiler_type) {
2123 if (parent && parent->IsDereferenceOfParent() &&
2124 epformat == eGetExpressionPathFormatHonorPointers) {
2127 const uint32_t non_base_class_parent_type_info =
2128 non_base_class_parent_compiler_type.GetTypeInfo();
2130 if (non_base_class_parent_type_info & eTypeIsPointer) {
2132 } else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2133 !(non_base_class_parent_type_info & eTypeIsArray)) {
2140 const char *name = GetName().GetCString();
2142 if (qualify_cxx_base_classes) {
2143 if (GetBaseClassPath(s))
2151 if (is_deref_of_parent &&
2152 epformat == eGetExpressionPathFormatDereferencePointers) {
2157 ValueObjectSP ValueObject::GetValueForExpressionPath(
2158 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
2159 ExpressionPathEndResultType *final_value_type,
2160 const GetValueForExpressionPathOptions &options,
2161 ExpressionPathAftermath *final_task_on_target) {
2163 ExpressionPathScanEndReason dummy_reason_to_stop =
2164 ValueObject::eExpressionPathScanEndReasonUnknown;
2165 ExpressionPathEndResultType dummy_final_value_type =
2166 ValueObject::eExpressionPathEndResultTypeInvalid;
2167 ExpressionPathAftermath dummy_final_task_on_target =
2168 ValueObject::eExpressionPathAftermathNothing;
2170 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(
2171 expression, reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2172 final_value_type ? final_value_type : &dummy_final_value_type, options,
2173 final_task_on_target ? final_task_on_target
2174 : &dummy_final_task_on_target);
2176 if (!final_task_on_target ||
2177 *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
2180 if (ret_val.get() &&
2181 ((final_value_type ? *final_value_type : dummy_final_value_type) ==
2182 eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress
2185 if ((final_task_on_target ? *final_task_on_target
2186 : dummy_final_task_on_target) ==
2187 ValueObject::eExpressionPathAftermathDereference) {
2189 ValueObjectSP final_value = ret_val->Dereference(error);
2190 if (error.Fail() || !final_value.get()) {
2193 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2194 if (final_value_type)
2195 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2196 return ValueObjectSP();
2198 if (final_task_on_target)
2199 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2203 if (*final_task_on_target ==
2204 ValueObject::eExpressionPathAftermathTakeAddress) {
2206 ValueObjectSP final_value = ret_val->AddressOf(error);
2207 if (error.Fail() || !final_value.get()) {
2210 ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2211 if (final_value_type)
2212 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
2213 return ValueObjectSP();
2215 if (final_task_on_target)
2216 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
2221 return ret_val; // final_task_on_target will still have its original value, so
2222 // you know I did not do it
2225 ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
2226 llvm::StringRef expression, ExpressionPathScanEndReason *reason_to_stop,
2227 ExpressionPathEndResultType *final_result,
2228 const GetValueForExpressionPathOptions &options,
2229 ExpressionPathAftermath *what_next) {
2230 ValueObjectSP root = GetSP();
2235 llvm::StringRef remainder = expression;
2238 llvm::StringRef temp_expression = remainder;
2240 CompilerType root_compiler_type = root->GetCompilerType();
2241 CompilerType pointee_compiler_type;
2242 Flags pointee_compiler_type_info;
2244 Flags root_compiler_type_info(
2245 root_compiler_type.GetTypeInfo(&pointee_compiler_type));
2246 if (pointee_compiler_type)
2247 pointee_compiler_type_info.Reset(pointee_compiler_type.GetTypeInfo());
2249 if (temp_expression.empty()) {
2250 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2254 switch (temp_expression.front()) {
2256 temp_expression = temp_expression.drop_front();
2257 if (options.m_check_dot_vs_arrow_syntax &&
2258 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2260 // non-pointer and I
2261 // must catch the error
2264 ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2265 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2266 return ValueObjectSP();
2268 if (root_compiler_type_info.Test(eTypeIsObjC) && // if yo are trying to
2269 // extract an ObjC IVar
2270 // when this is forbidden
2271 root_compiler_type_info.Test(eTypeIsPointer) &&
2272 options.m_no_fragile_ivar) {
2274 ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2275 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2276 return ValueObjectSP();
2278 if (!temp_expression.startswith(">")) {
2280 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2281 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2282 return ValueObjectSP();
2286 case '.': // or fallthrough from ->
2288 if (options.m_check_dot_vs_arrow_syntax &&
2289 temp_expression.front() == '.' &&
2290 root_compiler_type_info.Test(eTypeIsPointer)) // if you are trying to
2291 // use . on a pointer
2292 // and I must catch the
2296 ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2297 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2300 temp_expression = temp_expression.drop_front(); // skip . or >
2302 size_t next_sep_pos = temp_expression.find_first_of("-.[", 1);
2303 ConstString child_name;
2304 if (next_sep_pos == llvm::StringRef::npos) // if no other separator just
2305 // expand this last layer
2307 child_name.SetString(temp_expression);
2308 ValueObjectSP child_valobj_sp =
2309 root->GetChildMemberWithName(child_name, true);
2311 if (child_valobj_sp.get()) // we know we are done, so just return
2314 ValueObject::eExpressionPathScanEndReasonEndOfString;
2315 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2316 return child_valobj_sp;
2318 switch (options.m_synthetic_children_traversal) {
2319 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2322 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2324 if (root->IsSynthetic()) {
2325 child_valobj_sp = root->GetNonSyntheticValue();
2326 if (child_valobj_sp.get())
2328 child_valobj_sp->GetChildMemberWithName(child_name, true);
2331 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2333 if (!root->IsSynthetic()) {
2334 child_valobj_sp = root->GetSyntheticValue();
2335 if (child_valobj_sp.get())
2337 child_valobj_sp->GetChildMemberWithName(child_name, true);
2340 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2342 if (root->IsSynthetic()) {
2343 child_valobj_sp = root->GetNonSyntheticValue();
2344 if (child_valobj_sp.get())
2346 child_valobj_sp->GetChildMemberWithName(child_name, true);
2348 child_valobj_sp = root->GetSyntheticValue();
2349 if (child_valobj_sp.get())
2351 child_valobj_sp->GetChildMemberWithName(child_name, true);
2357 // if we are here and options.m_no_synthetic_children is true,
2358 // child_valobj_sp is going to be a NULL SP,
2359 // so we hit the "else" branch, and return an error
2360 if (child_valobj_sp.get()) // if it worked, just return
2363 ValueObject::eExpressionPathScanEndReasonEndOfString;
2364 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2365 return child_valobj_sp;
2368 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2369 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2372 } else // other layers do expand
2374 llvm::StringRef next_separator = temp_expression.substr(next_sep_pos);
2376 child_name.SetString(temp_expression.slice(0, next_sep_pos));
2378 ValueObjectSP child_valobj_sp =
2379 root->GetChildMemberWithName(child_name, true);
2380 if (child_valobj_sp.get()) // store the new root and move on
2382 root = child_valobj_sp;
2383 remainder = next_separator;
2384 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2387 switch (options.m_synthetic_children_traversal) {
2388 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2391 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2393 if (root->IsSynthetic()) {
2394 child_valobj_sp = root->GetNonSyntheticValue();
2395 if (child_valobj_sp.get())
2397 child_valobj_sp->GetChildMemberWithName(child_name, true);
2400 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2402 if (!root->IsSynthetic()) {
2403 child_valobj_sp = root->GetSyntheticValue();
2404 if (child_valobj_sp.get())
2406 child_valobj_sp->GetChildMemberWithName(child_name, true);
2409 case GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2411 if (root->IsSynthetic()) {
2412 child_valobj_sp = root->GetNonSyntheticValue();
2413 if (child_valobj_sp.get())
2415 child_valobj_sp->GetChildMemberWithName(child_name, true);
2417 child_valobj_sp = root->GetSyntheticValue();
2418 if (child_valobj_sp.get())
2420 child_valobj_sp->GetChildMemberWithName(child_name, true);
2426 // if we are here and options.m_no_synthetic_children is true,
2427 // child_valobj_sp is going to be a NULL SP,
2428 // so we hit the "else" branch, and return an error
2429 if (child_valobj_sp.get()) // if it worked, move on
2431 root = child_valobj_sp;
2432 remainder = next_separator;
2433 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2437 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2438 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2445 if (!root_compiler_type_info.Test(eTypeIsArray) &&
2446 !root_compiler_type_info.Test(eTypeIsPointer) &&
2447 !root_compiler_type_info.Test(
2448 eTypeIsVector)) // if this is not a T[] nor a T*
2450 if (!root_compiler_type_info.Test(
2451 eTypeIsScalar)) // if this is not even a scalar...
2453 if (options.m_synthetic_children_traversal ==
2454 GetValueForExpressionPathOptions::SyntheticChildrenTraversal::
2455 None) // ...only chance left is synthetic
2458 ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2459 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2460 return ValueObjectSP();
2462 } else if (!options.m_allow_bitfields_syntax) // if this is a scalar,
2463 // check that we can
2467 ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2468 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2469 return ValueObjectSP();
2472 if (temp_expression[1] ==
2473 ']') // if this is an unbounded range it only works for arrays
2475 if (!root_compiler_type_info.Test(eTypeIsArray)) {
2477 ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2478 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2480 } else // even if something follows, we cannot expand unbounded ranges,
2481 // just let the caller do it
2484 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2486 ValueObject::eExpressionPathEndResultTypeUnboundedRange;
2491 size_t close_bracket_position = temp_expression.find(']', 1);
2492 if (close_bracket_position ==
2493 llvm::StringRef::npos) // if there is no ], this is a syntax error
2496 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2497 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2501 llvm::StringRef bracket_expr =
2502 temp_expression.slice(1, close_bracket_position);
2504 // If this was an empty expression it would have been caught by the if
2506 assert(!bracket_expr.empty());
2508 if (!bracket_expr.contains('-')) {
2509 // if no separator, this is of the form [N]. Note that this cannot
2510 // be an unbounded range of the form [], because that case was handled
2511 // above with an unconditional return.
2512 unsigned long index = 0;
2513 if (bracket_expr.getAsInteger(0, index)) {
2515 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2516 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2520 // from here on we do have a valid index
2521 if (root_compiler_type_info.Test(eTypeIsArray)) {
2522 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2523 if (!child_valobj_sp)
2524 child_valobj_sp = root->GetSyntheticArrayMember(index, true);
2525 if (!child_valobj_sp)
2526 if (root->HasSyntheticValue() &&
2527 root->GetSyntheticValue()->GetNumChildren() > index)
2529 root->GetSyntheticValue()->GetChildAtIndex(index, true);
2530 if (child_valobj_sp) {
2531 root = child_valobj_sp;
2533 temp_expression.substr(close_bracket_position + 1); // skip ]
2534 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2538 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2539 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2542 } else if (root_compiler_type_info.Test(eTypeIsPointer)) {
2545 eExpressionPathAftermathDereference && // if this is a
2554 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2556 root = root->Dereference(error);
2557 if (error.Fail() || !root) {
2559 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2560 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2563 *what_next = eExpressionPathAftermathNothing;
2567 if (root->GetCompilerType().GetMinimumLanguage() ==
2568 eLanguageTypeObjC &&
2569 pointee_compiler_type_info.AllClear(eTypeIsPointer) &&
2570 root->HasSyntheticValue() &&
2571 (options.m_synthetic_children_traversal ==
2572 GetValueForExpressionPathOptions::
2573 SyntheticChildrenTraversal::ToSynthetic ||
2574 options.m_synthetic_children_traversal ==
2575 GetValueForExpressionPathOptions::
2576 SyntheticChildrenTraversal::Both)) {
2577 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
2579 root = root->GetSyntheticArrayMember(index, true);
2582 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2583 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2587 temp_expression.substr(close_bracket_position + 1); // skip ]
2588 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2592 } else if (root_compiler_type_info.Test(eTypeIsScalar)) {
2593 root = root->GetSyntheticBitFieldChild(index, index, true);
2596 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2597 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2599 } else // we do not know how to expand members of bitfields, so we
2600 // just return and let the caller do any further processing
2602 *reason_to_stop = ValueObject::
2603 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2604 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2607 } else if (root_compiler_type_info.Test(eTypeIsVector)) {
2608 root = root->GetChildAtIndex(index, true);
2611 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2612 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2613 return ValueObjectSP();
2616 temp_expression.substr(close_bracket_position + 1); // skip ]
2617 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2620 } else if (options.m_synthetic_children_traversal ==
2621 GetValueForExpressionPathOptions::
2622 SyntheticChildrenTraversal::ToSynthetic ||
2623 options.m_synthetic_children_traversal ==
2624 GetValueForExpressionPathOptions::
2625 SyntheticChildrenTraversal::Both) {
2626 if (root->HasSyntheticValue())
2627 root = root->GetSyntheticValue();
2628 else if (!root->IsSynthetic()) {
2630 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2631 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2634 // if we are here, then root itself is a synthetic VO.. should be good
2639 ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2640 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2643 root = root->GetChildAtIndex(index, true);
2646 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2647 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2651 temp_expression.substr(close_bracket_position + 1); // skip ]
2652 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
2657 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2658 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2662 // we have a low and a high index
2663 llvm::StringRef sleft, sright;
2664 unsigned long low_index, high_index;
2665 std::tie(sleft, sright) = bracket_expr.split('-');
2666 if (sleft.getAsInteger(0, low_index) ||
2667 sright.getAsInteger(0, high_index)) {
2669 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2670 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2674 if (low_index > high_index) // swap indices if required
2675 std::swap(low_index, high_index);
2677 if (root_compiler_type_info.Test(
2678 eTypeIsScalar)) // expansion only works for scalars
2680 root = root->GetSyntheticBitFieldChild(low_index, high_index, true);
2683 ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2684 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2687 *reason_to_stop = ValueObject::
2688 eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2689 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
2692 } else if (root_compiler_type_info.Test(
2693 eTypeIsPointer) && // if this is a ptr-to-scalar, I am
2694 // accessing it by index and I would
2695 // have deref'ed anyway, then do it
2696 // now and use this as a bitfield
2698 ValueObject::eExpressionPathAftermathDereference &&
2699 pointee_compiler_type_info.Test(eTypeIsScalar)) {
2701 root = root->Dereference(error);
2702 if (error.Fail() || !root) {
2704 ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2705 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2708 *what_next = ValueObject::eExpressionPathAftermathNothing;
2713 ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2714 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
2720 default: // some non-separator is in the way
2723 ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2724 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2731 void ValueObject::LogValueObject(Log *log) {
2733 return LogValueObject(log, DumpValueObjectOptions(*this));
2736 void ValueObject::LogValueObject(Log *log,
2737 const DumpValueObjectOptions &options) {
2742 log->PutCString(s.GetData());
2746 void ValueObject::Dump(Stream &s) { Dump(s, DumpValueObjectOptions(*this)); }
2748 void ValueObject::Dump(Stream &s, const DumpValueObjectOptions &options) {
2749 ValueObjectPrinter printer(this, &s, options);
2750 printer.PrintValueObject();
2753 ValueObjectSP ValueObject::CreateConstantValue(const ConstString &name) {
2754 ValueObjectSP valobj_sp;
2756 if (UpdateValueIfNeeded(false) && m_error.Success()) {
2757 ExecutionContext exe_ctx(GetExecutionContextRef());
2760 data.SetByteOrder(m_data.GetByteOrder());
2761 data.SetAddressByteSize(m_data.GetAddressByteSize());
2764 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
2765 m_error = v.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2767 m_error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
2769 valobj_sp = ValueObjectConstResult::Create(
2770 exe_ctx.GetBestExecutionContextScope(), GetCompilerType(), name, data,
2775 ExecutionContext exe_ctx(GetExecutionContextRef());
2776 valobj_sp = ValueObjectConstResult::Create(
2777 exe_ctx.GetBestExecutionContextScope(), m_error);
2782 ValueObjectSP ValueObject::GetQualifiedRepresentationIfAvailable(
2783 lldb::DynamicValueType dynValue, bool synthValue) {
2784 ValueObjectSP result_sp(GetSP());
2787 case lldb::eDynamicCanRunTarget:
2788 case lldb::eDynamicDontRunTarget: {
2789 if (!result_sp->IsDynamic()) {
2790 if (result_sp->GetDynamicValue(dynValue))
2791 result_sp = result_sp->GetDynamicValue(dynValue);
2794 case lldb::eNoDynamicValues: {
2795 if (result_sp->IsDynamic()) {
2796 if (result_sp->GetStaticValue())
2797 result_sp = result_sp->GetStaticValue();
2803 if (!result_sp->IsSynthetic()) {
2804 if (result_sp->GetSyntheticValue())
2805 result_sp = result_sp->GetSyntheticValue();
2808 if (result_sp->IsSynthetic()) {
2809 if (result_sp->GetNonSyntheticValue())
2810 result_sp = result_sp->GetNonSyntheticValue();
2817 lldb::addr_t ValueObject::GetCPPVTableAddress(AddressType &address_type) {
2818 CompilerType pointee_type;
2819 CompilerType this_type(GetCompilerType());
2820 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
2822 bool ptr_or_ref = false;
2823 if (type_info & (eTypeIsPointer | eTypeIsReference)) {
2825 type_info = pointee_type.GetTypeInfo();
2828 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
2829 if ((type_info & cpp_class) == cpp_class) {
2831 address_type = GetAddressTypeOfChildren();
2832 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
2834 return GetAddressOf(false, &address_type);
2838 address_type = eAddressTypeInvalid;
2839 return LLDB_INVALID_ADDRESS;
2842 ValueObjectSP ValueObject::Dereference(Error &error) {
2844 return m_deref_valobj->GetSP();
2846 const bool is_pointer_or_reference_type = IsPointerOrReferenceType();
2847 if (is_pointer_or_reference_type) {
2848 bool omit_empty_base_classes = true;
2849 bool ignore_array_bounds = false;
2851 std::string child_name_str;
2852 uint32_t child_byte_size = 0;
2853 int32_t child_byte_offset = 0;
2854 uint32_t child_bitfield_bit_size = 0;
2855 uint32_t child_bitfield_bit_offset = 0;
2856 bool child_is_base_class = false;
2857 bool child_is_deref_of_parent = false;
2858 const bool transparent_pointers = false;
2859 CompilerType compiler_type = GetCompilerType();
2860 CompilerType child_compiler_type;
2861 uint64_t language_flags;
2863 ExecutionContext exe_ctx(GetExecutionContextRef());
2865 child_compiler_type = compiler_type.GetChildCompilerTypeAtIndex(
2866 &exe_ctx, 0, transparent_pointers, omit_empty_base_classes,
2867 ignore_array_bounds, child_name_str, child_byte_size, child_byte_offset,
2868 child_bitfield_bit_size, child_bitfield_bit_offset, child_is_base_class,
2869 child_is_deref_of_parent, this, language_flags);
2870 if (child_compiler_type && child_byte_size) {
2871 ConstString child_name;
2872 if (!child_name_str.empty())
2873 child_name.SetCString(child_name_str.c_str());
2875 m_deref_valobj = new ValueObjectChild(
2876 *this, child_compiler_type, child_name, child_byte_size,
2877 child_byte_offset, child_bitfield_bit_size, child_bitfield_bit_offset,
2878 child_is_base_class, child_is_deref_of_parent, eAddressTypeInvalid,
2881 } else if (HasSyntheticValue()) {
2884 ->GetChildMemberWithName(ConstString("$$dereference$$"), true)
2888 if (m_deref_valobj) {
2890 return m_deref_valobj->GetSP();
2893 GetExpressionPath(strm, true);
2895 if (is_pointer_or_reference_type)
2896 error.SetErrorStringWithFormat("dereference failed: (%s) %s",
2897 GetTypeName().AsCString("<invalid type>"),
2900 error.SetErrorStringWithFormat("not a pointer or reference type: (%s) %s",
2901 GetTypeName().AsCString("<invalid type>"),
2903 return ValueObjectSP();
2907 ValueObjectSP ValueObject::AddressOf(Error &error) {
2908 if (m_addr_of_valobj_sp)
2909 return m_addr_of_valobj_sp;
2911 AddressType address_type = eAddressTypeInvalid;
2912 const bool scalar_is_load_address = false;
2913 addr_t addr = GetAddressOf(scalar_is_load_address, &address_type);
2915 if (addr != LLDB_INVALID_ADDRESS && address_type != eAddressTypeHost) {
2916 switch (address_type) {
2917 case eAddressTypeInvalid: {
2918 StreamString expr_path_strm;
2919 GetExpressionPath(expr_path_strm, true);
2920 error.SetErrorStringWithFormat("'%s' is not in memory",
2921 expr_path_strm.GetData());
2924 case eAddressTypeFile:
2925 case eAddressTypeLoad: {
2926 CompilerType compiler_type = GetCompilerType();
2927 if (compiler_type) {
2928 std::string name(1, '&');
2929 name.append(m_name.AsCString(""));
2930 ExecutionContext exe_ctx(GetExecutionContextRef());
2931 m_addr_of_valobj_sp = ValueObjectConstResult::Create(
2932 exe_ctx.GetBestExecutionContextScope(),
2933 compiler_type.GetPointerType(), ConstString(name.c_str()), addr,
2934 eAddressTypeInvalid, m_data.GetAddressByteSize());
2941 StreamString expr_path_strm;
2942 GetExpressionPath(expr_path_strm, true);
2943 error.SetErrorStringWithFormat("'%s' doesn't have a valid address",
2944 expr_path_strm.GetData());
2947 return m_addr_of_valobj_sp;
2950 ValueObjectSP ValueObject::Cast(const CompilerType &compiler_type) {
2951 return ValueObjectCast::Create(*this, GetName(), compiler_type);
2954 lldb::ValueObjectSP ValueObject::Clone(const ConstString &new_name) {
2955 return ValueObjectCast::Create(*this, new_name, GetCompilerType());
2958 ValueObjectSP ValueObject::CastPointerType(const char *name,
2959 CompilerType &compiler_type) {
2960 ValueObjectSP valobj_sp;
2961 AddressType address_type;
2962 addr_t ptr_value = GetPointerValue(&address_type);
2964 if (ptr_value != LLDB_INVALID_ADDRESS) {
2965 Address ptr_addr(ptr_value);
2966 ExecutionContext exe_ctx(GetExecutionContextRef());
2967 valobj_sp = ValueObjectMemory::Create(
2968 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, compiler_type);
2973 ValueObjectSP ValueObject::CastPointerType(const char *name, TypeSP &type_sp) {
2974 ValueObjectSP valobj_sp;
2975 AddressType address_type;
2976 addr_t ptr_value = GetPointerValue(&address_type);
2978 if (ptr_value != LLDB_INVALID_ADDRESS) {
2979 Address ptr_addr(ptr_value);
2980 ExecutionContext exe_ctx(GetExecutionContextRef());
2981 valobj_sp = ValueObjectMemory::Create(
2982 exe_ctx.GetBestExecutionContextScope(), name, ptr_addr, type_sp);
2987 ValueObject::EvaluationPoint::EvaluationPoint()
2988 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {}
2990 ValueObject::EvaluationPoint::EvaluationPoint(ExecutionContextScope *exe_scope,
2992 : m_mod_id(), m_exe_ctx_ref(), m_needs_update(true) {
2993 ExecutionContext exe_ctx(exe_scope);
2994 TargetSP target_sp(exe_ctx.GetTargetSP());
2996 m_exe_ctx_ref.SetTargetSP(target_sp);
2997 ProcessSP process_sp(exe_ctx.GetProcessSP());
2999 process_sp = target_sp->GetProcessSP();
3002 m_mod_id = process_sp->GetModID();
3003 m_exe_ctx_ref.SetProcessSP(process_sp);
3005 ThreadSP thread_sp(exe_ctx.GetThreadSP());
3009 thread_sp = process_sp->GetThreadList().GetSelectedThread();
3013 m_exe_ctx_ref.SetThreadSP(thread_sp);
3015 StackFrameSP frame_sp(exe_ctx.GetFrameSP());
3018 frame_sp = thread_sp->GetSelectedFrame();
3021 m_exe_ctx_ref.SetFrameSP(frame_sp);
3027 ValueObject::EvaluationPoint::EvaluationPoint(
3028 const ValueObject::EvaluationPoint &rhs)
3029 : m_mod_id(), m_exe_ctx_ref(rhs.m_exe_ctx_ref), m_needs_update(true) {}
3031 ValueObject::EvaluationPoint::~EvaluationPoint() {}
3033 // This function checks the EvaluationPoint against the current process state.
3035 // state matches the evaluation point, or the evaluation point is already
3036 // invalid, then we return
3037 // false, meaning "no change". If the current state is different, we update our
3038 // state, and return
3039 // true meaning "yes, change". If we did see a change, we also set
3040 // m_needs_update to true, so
3041 // future calls to NeedsUpdate will return true.
3042 // exe_scope will be set to the current execution context scope.
3044 bool ValueObject::EvaluationPoint::SyncWithProcessState(
3045 bool accept_invalid_exe_ctx) {
3046 // Start with the target, if it is NULL, then we're obviously not going to get
3048 const bool thread_and_frame_only_if_stopped = true;
3049 ExecutionContext exe_ctx(
3050 m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
3052 if (exe_ctx.GetTargetPtr() == NULL)
3055 // If we don't have a process nothing can change.
3056 Process *process = exe_ctx.GetProcessPtr();
3057 if (process == NULL)
3060 // If our stop id is the current stop ID, nothing has changed:
3061 ProcessModID current_mod_id = process->GetModID();
3063 // If the current stop id is 0, either we haven't run yet, or the process
3064 // state has been cleared.
3065 // In either case, we aren't going to be able to sync with the process state.
3066 if (current_mod_id.GetStopID() == 0)
3069 bool changed = false;
3070 const bool was_valid = m_mod_id.IsValid();
3072 if (m_mod_id == current_mod_id) {
3073 // Everything is already up to date in this object, no need to
3074 // update the execution context scope.
3077 m_mod_id = current_mod_id;
3078 m_needs_update = true;
3083 // Now re-look up the thread and frame in case the underlying objects have
3084 // gone away & been recreated.
3085 // That way we'll be sure to return a valid exe_scope.
3086 // If we used to have a thread or a frame but can't find it anymore, then mark
3087 // ourselves as invalid.
3089 if (!accept_invalid_exe_ctx) {
3090 if (m_exe_ctx_ref.HasThreadRef()) {
3091 ThreadSP thread_sp(m_exe_ctx_ref.GetThreadSP());
3093 if (m_exe_ctx_ref.HasFrameRef()) {
3094 StackFrameSP frame_sp(m_exe_ctx_ref.GetFrameSP());
3096 // We used to have a frame, but now it is gone
3098 changed = was_valid;
3102 // We used to have a thread, but now it is gone
3104 changed = was_valid;
3112 void ValueObject::EvaluationPoint::SetUpdated() {
3113 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3115 m_mod_id = process_sp->GetModID();
3116 m_needs_update = false;
3119 void ValueObject::ClearUserVisibleData(uint32_t clear_mask) {
3120 if ((clear_mask & eClearUserVisibleDataItemsValue) ==
3121 eClearUserVisibleDataItemsValue)
3122 m_value_str.clear();
3124 if ((clear_mask & eClearUserVisibleDataItemsLocation) ==
3125 eClearUserVisibleDataItemsLocation)
3126 m_location_str.clear();
3128 if ((clear_mask & eClearUserVisibleDataItemsSummary) ==
3129 eClearUserVisibleDataItemsSummary)
3130 m_summary_str.clear();
3132 if ((clear_mask & eClearUserVisibleDataItemsDescription) ==
3133 eClearUserVisibleDataItemsDescription)
3134 m_object_desc_str.clear();
3136 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) ==
3137 eClearUserVisibleDataItemsSyntheticChildren) {
3138 if (m_synthetic_value)
3139 m_synthetic_value = NULL;
3142 if ((clear_mask & eClearUserVisibleDataItemsValidator) ==
3143 eClearUserVisibleDataItemsValidator)
3144 m_validation_result.reset();
3147 SymbolContextScope *ValueObject::GetSymbolContextScope() {
3149 if (!m_parent->IsPointerOrReferenceType())
3150 return m_parent->GetSymbolContextScope();
3156 ValueObject::CreateValueObjectFromExpression(llvm::StringRef name,
3157 llvm::StringRef expression,
3158 const ExecutionContext &exe_ctx) {
3159 return CreateValueObjectFromExpression(name, expression, exe_ctx,
3160 EvaluateExpressionOptions());
3163 lldb::ValueObjectSP ValueObject::CreateValueObjectFromExpression(
3164 llvm::StringRef name, llvm::StringRef expression,
3165 const ExecutionContext &exe_ctx, const EvaluateExpressionOptions &options) {
3166 lldb::ValueObjectSP retval_sp;
3167 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3170 if (expression.empty())
3172 target_sp->EvaluateExpression(expression, exe_ctx.GetFrameSP().get(),
3173 retval_sp, options);
3174 if (retval_sp && !name.empty())
3175 retval_sp->SetName(ConstString(name));
3179 lldb::ValueObjectSP ValueObject::CreateValueObjectFromAddress(
3180 llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx,
3181 CompilerType type) {
3183 CompilerType pointer_type(type.GetPointerType());
3185 lldb::DataBufferSP buffer(
3186 new lldb_private::DataBufferHeap(&address, sizeof(lldb::addr_t)));
3187 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create(
3188 exe_ctx.GetBestExecutionContextScope(), pointer_type,
3189 ConstString(name), buffer, exe_ctx.GetByteOrder(),
3190 exe_ctx.GetAddressByteSize()));
3191 if (ptr_result_valobj_sp) {
3192 ptr_result_valobj_sp->GetValue().SetValueType(
3193 Value::eValueTypeLoadAddress);
3195 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
3196 if (ptr_result_valobj_sp && !name.empty())
3197 ptr_result_valobj_sp->SetName(ConstString(name));
3199 return ptr_result_valobj_sp;
3202 return lldb::ValueObjectSP();
3205 lldb::ValueObjectSP ValueObject::CreateValueObjectFromData(
3206 llvm::StringRef name, const DataExtractor &data,
3207 const ExecutionContext &exe_ctx, CompilerType type) {
3208 lldb::ValueObjectSP new_value_sp;
3209 new_value_sp = ValueObjectConstResult::Create(
3210 exe_ctx.GetBestExecutionContextScope(), type, ConstString(name), data,
3211 LLDB_INVALID_ADDRESS);
3212 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
3213 if (new_value_sp && !name.empty())
3214 new_value_sp->SetName(ConstString(name));
3215 return new_value_sp;
3218 ModuleSP ValueObject::GetModule() {
3219 ValueObject *root(GetRoot());
3221 return root->GetModule();
3222 return lldb::ModuleSP();
3225 ValueObject *ValueObject::GetRoot() {
3228 return (m_root = FollowParentChain([](ValueObject *vo) -> bool {
3229 return (vo->m_parent != nullptr);
3234 ValueObject::FollowParentChain(std::function<bool(ValueObject *)> f) {
3235 ValueObject *vo = this;
3244 AddressType ValueObject::GetAddressTypeOfChildren() {
3245 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid) {
3246 ValueObject *root(GetRoot());
3248 return root->GetAddressTypeOfChildren();
3250 return m_address_type_of_ptr_or_ref_children;
3253 lldb::DynamicValueType ValueObject::GetDynamicValueType() {
3254 ValueObject *with_dv_info = this;
3255 while (with_dv_info) {
3256 if (with_dv_info->HasDynamicValueTypeInfo())
3257 return with_dv_info->GetDynamicValueTypeImpl();
3258 with_dv_info = with_dv_info->m_parent;
3260 return lldb::eNoDynamicValues;
3263 lldb::Format ValueObject::GetFormat() const {
3264 const ValueObject *with_fmt_info = this;
3265 while (with_fmt_info) {
3266 if (with_fmt_info->m_format != lldb::eFormatDefault)
3267 return with_fmt_info->m_format;
3268 with_fmt_info = with_fmt_info->m_parent;
3273 lldb::LanguageType ValueObject::GetPreferredDisplayLanguage() {
3274 lldb::LanguageType type = m_preferred_display_language;
3275 if (m_preferred_display_language == lldb::eLanguageTypeUnknown) {
3277 if (GetRoot() == this) {
3278 if (StackFrameSP frame_sp = GetFrameSP()) {
3279 const SymbolContext &sc(
3280 frame_sp->GetSymbolContext(eSymbolContextCompUnit));
3281 if (CompileUnit *cu = sc.comp_unit)
3282 type = cu->GetLanguage();
3285 type = GetRoot()->GetPreferredDisplayLanguage();
3289 return (m_preferred_display_language = type); // only compute it once
3292 void ValueObject::SetPreferredDisplayLanguage(lldb::LanguageType lt) {
3293 m_preferred_display_language = lt;
3296 void ValueObject::SetPreferredDisplayLanguageIfNeeded(lldb::LanguageType lt) {
3297 if (m_preferred_display_language == lldb::eLanguageTypeUnknown)
3298 SetPreferredDisplayLanguage(lt);
3301 bool ValueObject::CanProvideValue() {
3302 // we need to support invalid types as providers of values because some
3304 // debugging scenarios have no notion of types, but still manage to have raw
3306 // values for things like registers. sigh.
3307 const CompilerType &type(GetCompilerType());
3308 return (false == type.IsValid()) ||
3309 (0 != (type.GetTypeInfo() & eTypeHasValue));
3312 bool ValueObject::IsChecksumEmpty() { return m_value_checksum.empty(); }
3314 ValueObjectSP ValueObject::Persist() {
3315 if (!UpdateValueIfNeeded())
3318 TargetSP target_sp(GetTargetSP());
3322 PersistentExpressionState *persistent_state =
3323 target_sp->GetPersistentExpressionStateForLanguage(
3324 GetPreferredDisplayLanguage());
3326 if (!persistent_state)
3329 ConstString name(persistent_state->GetNextPersistentVariableName());
3331 ValueObjectSP const_result_sp =
3332 ValueObjectConstResult::Create(target_sp.get(), GetValue(), name);
3334 ExpressionVariableSP clang_var_sp =
3335 persistent_state->CreatePersistentVariable(const_result_sp);
3336 clang_var_sp->m_live_sp = clang_var_sp->m_frozen_sp;
3337 clang_var_sp->m_flags |= ExpressionVariable::EVIsProgramReference;
3339 return clang_var_sp->GetValueObject();
3342 bool ValueObject::IsSyntheticChildrenGenerated() {
3343 return m_is_synthetic_children_generated;
3346 void ValueObject::SetSyntheticChildrenGenerated(bool b) {
3347 m_is_synthetic_children_generated = b;
3350 uint64_t ValueObject::GetLanguageFlags() { return m_language_flags; }
3352 void ValueObject::SetLanguageFlags(uint64_t flags) { m_language_flags = flags; }
3354 ValueObjectManager::ValueObjectManager(lldb::ValueObjectSP in_valobj_sp,
3355 lldb::DynamicValueType use_dynamic,
3356 bool use_synthetic) : m_root_valobj_sp(),
3357 m_user_valobj_sp(), m_use_dynamic(use_dynamic), m_stop_id(UINT32_MAX),
3358 m_use_synthetic(use_synthetic) {
3361 // If the user passes in a value object that is dynamic or synthetic, then
3362 // water it down to the static type.
3363 m_root_valobj_sp = in_valobj_sp->GetQualifiedRepresentationIfAvailable(lldb::eNoDynamicValues, false);
3366 bool ValueObjectManager::IsValid() const {
3367 if (!m_root_valobj_sp)
3369 lldb::TargetSP target_sp = GetTargetSP();
3371 return target_sp->IsValid();
3375 lldb::ValueObjectSP ValueObjectManager::GetSP() {
3376 lldb::ProcessSP process_sp = GetProcessSP();
3378 return lldb::ValueObjectSP();
3380 const uint32_t current_stop_id = process_sp->GetLastNaturalStopID();
3381 if (current_stop_id == m_stop_id)
3382 return m_user_valobj_sp;
3384 m_stop_id = current_stop_id;
3386 if (!m_root_valobj_sp) {
3387 m_user_valobj_sp.reset();
3388 return m_root_valobj_sp;
3391 m_user_valobj_sp = m_root_valobj_sp;
3393 if (m_use_dynamic != lldb::eNoDynamicValues) {
3394 lldb::ValueObjectSP dynamic_sp = m_user_valobj_sp->GetDynamicValue(m_use_dynamic);
3396 m_user_valobj_sp = dynamic_sp;
3399 if (m_use_synthetic) {
3400 lldb::ValueObjectSP synthetic_sp = m_user_valobj_sp->GetSyntheticValue(m_use_synthetic);
3402 m_user_valobj_sp = synthetic_sp;
3405 return m_user_valobj_sp;
3408 void ValueObjectManager::SetUseDynamic(lldb::DynamicValueType use_dynamic) {
3409 if (use_dynamic != m_use_dynamic) {
3410 m_use_dynamic = use_dynamic;
3411 m_user_valobj_sp.reset();
3412 m_stop_id = UINT32_MAX;
3416 void ValueObjectManager::SetUseSynthetic(bool use_synthetic) {
3417 if (m_use_synthetic != use_synthetic) {
3418 m_use_synthetic = use_synthetic;
3419 m_user_valobj_sp.reset();
3420 m_stop_id = UINT32_MAX;
3424 lldb::TargetSP ValueObjectManager::GetTargetSP() const {
3425 if (!m_root_valobj_sp)
3426 return m_root_valobj_sp->GetTargetSP();
3427 return lldb::TargetSP();
3430 lldb::ProcessSP ValueObjectManager::GetProcessSP() const {
3431 if (m_root_valobj_sp)
3432 return m_root_valobj_sp->GetProcessSP();
3433 return lldb::ProcessSP();
3436 lldb::ThreadSP ValueObjectManager::GetThreadSP() const {
3437 if (m_root_valobj_sp)
3438 return m_root_valobj_sp->GetThreadSP();
3439 return lldb::ThreadSP();
3442 lldb::StackFrameSP ValueObjectManager::GetFrameSP() const {
3443 if (m_root_valobj_sp)
3444 return m_root_valobj_sp->GetFrameSP();
3445 return lldb::StackFrameSP();