1 //===-- SBValue.cpp -------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBValue.h"
10 #include "lldb/Utility/Instrumentation.h"
12 #include "lldb/API/SBDeclaration.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBTypeFilter.h"
15 #include "lldb/API/SBTypeFormat.h"
16 #include "lldb/API/SBTypeSummary.h"
17 #include "lldb/API/SBTypeSynthetic.h"
19 #include "lldb/Breakpoint/Watchpoint.h"
20 #include "lldb/Core/Declaration.h"
21 #include "lldb/Core/Module.h"
22 #include "lldb/Core/Section.h"
23 #include "lldb/Core/Value.h"
24 #include "lldb/DataFormatters/DataVisualization.h"
25 #include "lldb/DataFormatters/DumpValueObjectOptions.h"
26 #include "lldb/Symbol/Block.h"
27 #include "lldb/Symbol/ObjectFile.h"
28 #include "lldb/Symbol/Type.h"
29 #include "lldb/Symbol/Variable.h"
30 #include "lldb/Symbol/VariableList.h"
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/StackFrame.h"
34 #include "lldb/Target/Target.h"
35 #include "lldb/Target/Thread.h"
36 #include "lldb/Utility/DataExtractor.h"
37 #include "lldb/Utility/Scalar.h"
38 #include "lldb/Utility/Stream.h"
39 #include "lldb/ValueObject/ValueObject.h"
40 #include "lldb/ValueObject/ValueObjectConstResult.h"
42 #include "lldb/API/SBDebugger.h"
43 #include "lldb/API/SBExpressionOptions.h"
44 #include "lldb/API/SBFrame.h"
45 #include "lldb/API/SBProcess.h"
46 #include "lldb/API/SBTarget.h"
47 #include "lldb/API/SBThread.h"
52 using namespace lldb_private
;
56 ValueImpl() = default;
58 ValueImpl(lldb::ValueObjectSP in_valobj_sp
,
59 lldb::DynamicValueType use_dynamic
, bool use_synthetic
,
60 const char *name
= nullptr)
61 : m_use_dynamic(use_dynamic
), m_use_synthetic(use_synthetic
),
64 if ((m_valobj_sp
= in_valobj_sp
->GetQualifiedRepresentationIfAvailable(
65 lldb::eNoDynamicValues
, false))) {
66 if (!m_name
.IsEmpty())
67 m_valobj_sp
->SetName(m_name
);
72 ValueImpl(const ValueImpl
&rhs
) = default;
74 ValueImpl
&operator=(const ValueImpl
&rhs
) {
76 m_valobj_sp
= rhs
.m_valobj_sp
;
77 m_use_dynamic
= rhs
.m_use_dynamic
;
78 m_use_synthetic
= rhs
.m_use_synthetic
;
85 if (m_valobj_sp
.get() == nullptr)
88 // FIXME: This check is necessary but not sufficient. We for sure don't
89 // want to touch SBValues whose owning
90 // targets have gone away. This check is a little weak in that it
91 // enforces that restriction when you call IsValid, but since IsValid
92 // doesn't lock the target, you have no guarantee that the SBValue won't
93 // go invalid after you call this... Also, an SBValue could depend on
94 // data from one of the modules in the target, and those could go away
95 // independently of the target, for instance if a module is unloaded.
96 // But right now, neither SBValues nor ValueObjects know which modules
97 // they depend on. So I have no good way to make that check without
98 // tracking that in all the ValueObject subclasses.
99 TargetSP target_sp
= m_valobj_sp
->GetTargetSP();
100 return target_sp
&& target_sp
->IsValid();
104 lldb::ValueObjectSP
GetRootSP() { return m_valobj_sp
; }
106 lldb::ValueObjectSP
GetSP(Process::StopLocker
&stop_locker
,
107 std::unique_lock
<std::recursive_mutex
> &lock
,
110 error
= Status::FromErrorString("invalid value object");
114 lldb::ValueObjectSP value_sp
= m_valobj_sp
;
116 Target
*target
= value_sp
->GetTargetSP().get();
117 // If this ValueObject holds an error, then it is valuable for that.
118 if (value_sp
->GetError().Fail())
122 return ValueObjectSP();
124 lock
= std::unique_lock
<std::recursive_mutex
>(target
->GetAPIMutex());
126 ProcessSP
process_sp(value_sp
->GetProcessSP());
127 if (process_sp
&& !stop_locker
.TryLock(&process_sp
->GetRunLock())) {
128 // We don't allow people to play around with ValueObject if the process
129 // is running. If you want to look at values, pause the process, then
131 error
= Status::FromErrorString("process must be stopped.");
132 return ValueObjectSP();
135 if (m_use_dynamic
!= eNoDynamicValues
) {
136 ValueObjectSP dynamic_sp
= value_sp
->GetDynamicValue(m_use_dynamic
);
138 value_sp
= dynamic_sp
;
141 if (m_use_synthetic
) {
142 ValueObjectSP synthetic_sp
= value_sp
->GetSyntheticValue();
144 value_sp
= synthetic_sp
;
148 error
= Status::FromErrorString("invalid value object");
149 if (!m_name
.IsEmpty())
150 value_sp
->SetName(m_name
);
155 void SetUseDynamic(lldb::DynamicValueType use_dynamic
) {
156 m_use_dynamic
= use_dynamic
;
159 void SetUseSynthetic(bool use_synthetic
) { m_use_synthetic
= use_synthetic
; }
161 lldb::DynamicValueType
GetUseDynamic() { return m_use_dynamic
; }
163 bool GetUseSynthetic() { return m_use_synthetic
; }
165 // All the derived values that we would make from the m_valobj_sp will share
166 // the ExecutionContext with m_valobj_sp, so we don't need to do the
167 // calculations in GetSP to return the Target, Process, Thread or Frame. It
168 // is convenient to provide simple accessors for these, which I do here.
169 TargetSP
GetTargetSP() {
171 return m_valobj_sp
->GetTargetSP();
176 ProcessSP
GetProcessSP() {
178 return m_valobj_sp
->GetProcessSP();
183 ThreadSP
GetThreadSP() {
185 return m_valobj_sp
->GetThreadSP();
190 StackFrameSP
GetFrameSP() {
192 return m_valobj_sp
->GetFrameSP();
194 return StackFrameSP();
198 lldb::ValueObjectSP m_valobj_sp
;
199 lldb::DynamicValueType m_use_dynamic
;
200 bool m_use_synthetic
;
206 ValueLocker() = default;
208 ValueObjectSP
GetLockedSP(ValueImpl
&in_value
) {
209 return in_value
.GetSP(m_stop_locker
, m_lock
, m_lock_error
);
212 Status
&GetError() { return m_lock_error
; }
215 Process::StopLocker m_stop_locker
;
216 std::unique_lock
<std::recursive_mutex
> m_lock
;
220 SBValue::SBValue() { LLDB_INSTRUMENT_VA(this); }
222 SBValue::SBValue(const lldb::ValueObjectSP
&value_sp
) {
223 LLDB_INSTRUMENT_VA(this, value_sp
);
228 SBValue::SBValue(const SBValue
&rhs
) {
229 LLDB_INSTRUMENT_VA(this, rhs
);
231 SetSP(rhs
.m_opaque_sp
);
234 SBValue
&SBValue::operator=(const SBValue
&rhs
) {
235 LLDB_INSTRUMENT_VA(this, rhs
);
238 SetSP(rhs
.m_opaque_sp
);
243 SBValue::~SBValue() = default;
245 bool SBValue::IsValid() {
246 LLDB_INSTRUMENT_VA(this);
247 return this->operator bool();
249 SBValue::operator bool() const {
250 LLDB_INSTRUMENT_VA(this);
252 // If this function ever changes to anything that does more than just check
253 // if the opaque shared pointer is non NULL, then we need to update all "if
254 // (m_opaque_sp)" code in this file.
255 return m_opaque_sp
.get() != nullptr && m_opaque_sp
->IsValid() &&
256 m_opaque_sp
->GetRootSP().get() != nullptr;
259 void SBValue::Clear() {
260 LLDB_INSTRUMENT_VA(this);
265 SBError
SBValue::GetError() {
266 LLDB_INSTRUMENT_VA(this);
271 lldb::ValueObjectSP
value_sp(GetSP(locker
));
273 sb_error
.SetError(value_sp
->GetError().Clone());
275 sb_error
= Status::FromErrorStringWithFormat("error: %s",
276 locker
.GetError().AsCString());
281 user_id_t
SBValue::GetID() {
282 LLDB_INSTRUMENT_VA(this);
285 lldb::ValueObjectSP
value_sp(GetSP(locker
));
287 return value_sp
->GetID();
288 return LLDB_INVALID_UID
;
291 const char *SBValue::GetName() {
292 LLDB_INSTRUMENT_VA(this);
295 lldb::ValueObjectSP
value_sp(GetSP(locker
));
299 return value_sp
->GetName().GetCString();
302 const char *SBValue::GetTypeName() {
303 LLDB_INSTRUMENT_VA(this);
306 lldb::ValueObjectSP
value_sp(GetSP(locker
));
310 return value_sp
->GetQualifiedTypeName().GetCString();
313 const char *SBValue::GetDisplayTypeName() {
314 LLDB_INSTRUMENT_VA(this);
317 lldb::ValueObjectSP
value_sp(GetSP(locker
));
321 return value_sp
->GetDisplayTypeName().GetCString();
324 size_t SBValue::GetByteSize() {
325 LLDB_INSTRUMENT_VA(this);
330 lldb::ValueObjectSP
value_sp(GetSP(locker
));
332 result
= value_sp
->GetByteSize().value_or(0);
338 bool SBValue::IsInScope() {
339 LLDB_INSTRUMENT_VA(this);
344 lldb::ValueObjectSP
value_sp(GetSP(locker
));
346 result
= value_sp
->IsInScope();
352 const char *SBValue::GetValue() {
353 LLDB_INSTRUMENT_VA(this);
356 lldb::ValueObjectSP
value_sp(GetSP(locker
));
359 return ConstString(value_sp
->GetValueAsCString()).GetCString();
362 ValueType
SBValue::GetValueType() {
363 LLDB_INSTRUMENT_VA(this);
365 ValueType result
= eValueTypeInvalid
;
367 lldb::ValueObjectSP
value_sp(GetSP(locker
));
369 result
= value_sp
->GetValueType();
374 const char *SBValue::GetObjectDescription() {
375 LLDB_INSTRUMENT_VA(this);
378 lldb::ValueObjectSP
value_sp(GetSP(locker
));
382 llvm::Expected
<std::string
> str
= value_sp
->GetObjectDescription();
384 llvm::consumeError(str
.takeError());
387 return ConstString(*str
).AsCString();
390 SBType
SBValue::GetType() {
391 LLDB_INSTRUMENT_VA(this);
395 lldb::ValueObjectSP
value_sp(GetSP(locker
));
398 type_sp
= std::make_shared
<TypeImpl
>(value_sp
->GetTypeImpl());
399 sb_type
.SetSP(type_sp
);
405 bool SBValue::GetValueDidChange() {
406 LLDB_INSTRUMENT_VA(this);
410 lldb::ValueObjectSP
value_sp(GetSP(locker
));
412 if (value_sp
->UpdateValueIfNeeded(false))
413 result
= value_sp
->GetValueDidChange();
419 const char *SBValue::GetSummary() {
420 LLDB_INSTRUMENT_VA(this);
423 lldb::ValueObjectSP
value_sp(GetSP(locker
));
427 return ConstString(value_sp
->GetSummaryAsCString()).GetCString();
430 const char *SBValue::GetSummary(lldb::SBStream
&stream
,
431 lldb::SBTypeSummaryOptions
&options
) {
432 LLDB_INSTRUMENT_VA(this, stream
, options
);
435 lldb::ValueObjectSP
value_sp(GetSP(locker
));
438 if (value_sp
->GetSummaryAsCString(buffer
, options
.ref()) && !buffer
.empty())
439 stream
.Printf("%s", buffer
.c_str());
441 return ConstString(stream
.GetData()).GetCString();
444 const char *SBValue::GetLocation() {
445 LLDB_INSTRUMENT_VA(this);
448 lldb::ValueObjectSP
value_sp(GetSP(locker
));
452 return ConstString(value_sp
->GetLocationAsCString()).GetCString();
455 // Deprecated - use the one that takes an lldb::SBError
456 bool SBValue::SetValueFromCString(const char *value_str
) {
457 LLDB_INSTRUMENT_VA(this, value_str
);
460 return SetValueFromCString(value_str
, dummy
);
463 bool SBValue::SetValueFromCString(const char *value_str
, lldb::SBError
&error
) {
464 LLDB_INSTRUMENT_VA(this, value_str
, error
);
466 bool success
= false;
468 lldb::ValueObjectSP
value_sp(GetSP(locker
));
470 success
= value_sp
->SetValueFromCString(value_str
, error
.ref());
472 error
= Status::FromErrorStringWithFormat("Could not get value: %s",
473 locker
.GetError().AsCString());
478 lldb::SBTypeFormat
SBValue::GetTypeFormat() {
479 LLDB_INSTRUMENT_VA(this);
481 lldb::SBTypeFormat format
;
483 lldb::ValueObjectSP
value_sp(GetSP(locker
));
485 if (value_sp
->UpdateValueIfNeeded(true)) {
486 lldb::TypeFormatImplSP format_sp
= value_sp
->GetValueFormat();
488 format
.SetSP(format_sp
);
494 lldb::SBTypeSummary
SBValue::GetTypeSummary() {
495 LLDB_INSTRUMENT_VA(this);
497 lldb::SBTypeSummary summary
;
499 lldb::ValueObjectSP
value_sp(GetSP(locker
));
501 if (value_sp
->UpdateValueIfNeeded(true)) {
502 lldb::TypeSummaryImplSP summary_sp
= value_sp
->GetSummaryFormat();
504 summary
.SetSP(summary_sp
);
510 lldb::SBTypeFilter
SBValue::GetTypeFilter() {
511 LLDB_INSTRUMENT_VA(this);
513 lldb::SBTypeFilter filter
;
515 lldb::ValueObjectSP
value_sp(GetSP(locker
));
517 if (value_sp
->UpdateValueIfNeeded(true)) {
518 lldb::SyntheticChildrenSP synthetic_sp
= value_sp
->GetSyntheticChildren();
520 if (synthetic_sp
&& !synthetic_sp
->IsScripted()) {
521 TypeFilterImplSP filter_sp
=
522 std::static_pointer_cast
<TypeFilterImpl
>(synthetic_sp
);
523 filter
.SetSP(filter_sp
);
530 lldb::SBTypeSynthetic
SBValue::GetTypeSynthetic() {
531 LLDB_INSTRUMENT_VA(this);
533 lldb::SBTypeSynthetic synthetic
;
535 lldb::ValueObjectSP
value_sp(GetSP(locker
));
537 if (value_sp
->UpdateValueIfNeeded(true)) {
538 lldb::SyntheticChildrenSP children_sp
= value_sp
->GetSyntheticChildren();
540 if (children_sp
&& children_sp
->IsScripted()) {
541 ScriptedSyntheticChildrenSP synth_sp
=
542 std::static_pointer_cast
<ScriptedSyntheticChildren
>(children_sp
);
543 synthetic
.SetSP(synth_sp
);
550 lldb::SBValue
SBValue::CreateChildAtOffset(const char *name
, uint32_t offset
,
552 LLDB_INSTRUMENT_VA(this, name
, offset
, type
);
554 lldb::SBValue sb_value
;
556 lldb::ValueObjectSP
value_sp(GetSP(locker
));
557 lldb::ValueObjectSP new_value_sp
;
559 TypeImplSP
type_sp(type
.GetSP());
560 if (type
.IsValid()) {
561 sb_value
.SetSP(value_sp
->GetSyntheticChildAtOffset(
562 offset
, type_sp
->GetCompilerType(false), true),
563 GetPreferDynamicValue(), GetPreferSyntheticValue(), name
);
569 lldb::SBValue
SBValue::Cast(SBType type
) {
570 LLDB_INSTRUMENT_VA(this, type
);
572 lldb::SBValue sb_value
;
574 lldb::ValueObjectSP
value_sp(GetSP(locker
));
575 TypeImplSP
type_sp(type
.GetSP());
576 if (value_sp
&& type_sp
)
577 sb_value
.SetSP(value_sp
->Cast(type_sp
->GetCompilerType(false)),
578 GetPreferDynamicValue(), GetPreferSyntheticValue());
582 lldb::SBValue
SBValue::CreateValueFromExpression(const char *name
,
583 const char *expression
) {
584 LLDB_INSTRUMENT_VA(this, name
, expression
);
586 SBExpressionOptions options
;
587 options
.ref().SetKeepInMemory(true);
588 return CreateValueFromExpression(name
, expression
, options
);
591 lldb::SBValue
SBValue::CreateValueFromExpression(const char *name
,
592 const char *expression
,
593 SBExpressionOptions
&options
) {
594 LLDB_INSTRUMENT_VA(this, name
, expression
, options
);
596 lldb::SBValue sb_value
;
598 lldb::ValueObjectSP
value_sp(GetSP(locker
));
599 lldb::ValueObjectSP new_value_sp
;
601 ExecutionContext
exe_ctx(value_sp
->GetExecutionContextRef());
602 new_value_sp
= ValueObject::CreateValueObjectFromExpression(
603 name
, expression
, exe_ctx
, options
.ref());
605 new_value_sp
->SetName(ConstString(name
));
607 sb_value
.SetSP(new_value_sp
);
611 lldb::SBValue
SBValue::CreateValueFromAddress(const char *name
,
612 lldb::addr_t address
,
614 LLDB_INSTRUMENT_VA(this, name
, address
, sb_type
);
616 lldb::SBValue sb_value
;
618 lldb::ValueObjectSP
value_sp(GetSP(locker
));
619 lldb::ValueObjectSP new_value_sp
;
620 lldb::TypeImplSP
type_impl_sp(sb_type
.GetSP());
621 if (value_sp
&& type_impl_sp
) {
622 CompilerType
ast_type(type_impl_sp
->GetCompilerType(true));
623 ExecutionContext
exe_ctx(value_sp
->GetExecutionContextRef());
624 new_value_sp
= ValueObject::CreateValueObjectFromAddress(name
, address
,
627 sb_value
.SetSP(new_value_sp
);
631 lldb::SBValue
SBValue::CreateValueFromData(const char *name
, SBData data
,
633 LLDB_INSTRUMENT_VA(this, name
, data
, sb_type
);
635 lldb::SBValue sb_value
;
636 lldb::ValueObjectSP new_value_sp
;
638 lldb::ValueObjectSP
value_sp(GetSP(locker
));
639 lldb::TypeImplSP
type_impl_sp(sb_type
.GetSP());
640 if (value_sp
&& type_impl_sp
) {
641 ExecutionContext
exe_ctx(value_sp
->GetExecutionContextRef());
642 new_value_sp
= ValueObject::CreateValueObjectFromData(
643 name
, **data
, exe_ctx
, type_impl_sp
->GetCompilerType(true));
644 new_value_sp
->SetAddressTypeOfChildren(eAddressTypeLoad
);
646 sb_value
.SetSP(new_value_sp
);
650 lldb::SBValue
SBValue::CreateBoolValue(const char *name
, bool value
) {
651 LLDB_INSTRUMENT_VA(this, name
);
653 lldb::SBValue sb_value
;
654 lldb::ValueObjectSP new_value_sp
;
656 lldb::ValueObjectSP
value_sp(GetSP(locker
));
657 lldb::TargetSP target_sp
= m_opaque_sp
->GetTargetSP();
658 if (value_sp
&& target_sp
) {
660 ValueObject::CreateValueObjectFromBool(target_sp
, value
, name
);
662 sb_value
.SetSP(new_value_sp
);
666 SBValue
SBValue::GetChildAtIndex(uint32_t idx
) {
667 LLDB_INSTRUMENT_VA(this, idx
);
669 const bool can_create_synthetic
= false;
670 lldb::DynamicValueType use_dynamic
= eNoDynamicValues
;
673 target_sp
= m_opaque_sp
->GetTargetSP();
676 use_dynamic
= target_sp
->GetPreferDynamicValue();
678 return GetChildAtIndex(idx
, use_dynamic
, can_create_synthetic
);
681 SBValue
SBValue::GetChildAtIndex(uint32_t idx
,
682 lldb::DynamicValueType use_dynamic
,
683 bool can_create_synthetic
) {
684 LLDB_INSTRUMENT_VA(this, idx
, use_dynamic
, can_create_synthetic
);
686 lldb::ValueObjectSP child_sp
;
689 lldb::ValueObjectSP
value_sp(GetSP(locker
));
691 const bool can_create
= true;
692 child_sp
= value_sp
->GetChildAtIndex(idx
);
693 if (can_create_synthetic
&& !child_sp
) {
694 child_sp
= value_sp
->GetSyntheticArrayMember(idx
, can_create
);
699 sb_value
.SetSP(child_sp
, use_dynamic
, GetPreferSyntheticValue());
704 uint32_t SBValue::GetIndexOfChildWithName(const char *name
) {
705 LLDB_INSTRUMENT_VA(this, name
);
707 uint32_t idx
= UINT32_MAX
;
709 lldb::ValueObjectSP
value_sp(GetSP(locker
));
711 idx
= value_sp
->GetIndexOfChildWithName(name
);
716 SBValue
SBValue::GetChildMemberWithName(const char *name
) {
717 LLDB_INSTRUMENT_VA(this, name
);
719 lldb::DynamicValueType use_dynamic_value
= eNoDynamicValues
;
722 target_sp
= m_opaque_sp
->GetTargetSP();
725 use_dynamic_value
= target_sp
->GetPreferDynamicValue();
726 return GetChildMemberWithName(name
, use_dynamic_value
);
730 SBValue::GetChildMemberWithName(const char *name
,
731 lldb::DynamicValueType use_dynamic_value
) {
732 LLDB_INSTRUMENT_VA(this, name
, use_dynamic_value
);
734 lldb::ValueObjectSP child_sp
;
737 lldb::ValueObjectSP
value_sp(GetSP(locker
));
739 child_sp
= value_sp
->GetChildMemberWithName(name
);
743 sb_value
.SetSP(child_sp
, use_dynamic_value
, GetPreferSyntheticValue());
748 lldb::SBValue
SBValue::GetDynamicValue(lldb::DynamicValueType use_dynamic
) {
749 LLDB_INSTRUMENT_VA(this, use_dynamic
);
753 ValueImplSP
proxy_sp(new ValueImpl(m_opaque_sp
->GetRootSP(), use_dynamic
,
754 m_opaque_sp
->GetUseSynthetic()));
755 value_sb
.SetSP(proxy_sp
);
760 lldb::SBValue
SBValue::GetStaticValue() {
761 LLDB_INSTRUMENT_VA(this);
765 ValueImplSP
proxy_sp(new ValueImpl(m_opaque_sp
->GetRootSP(),
767 m_opaque_sp
->GetUseSynthetic()));
768 value_sb
.SetSP(proxy_sp
);
773 lldb::SBValue
SBValue::GetNonSyntheticValue() {
774 LLDB_INSTRUMENT_VA(this);
778 ValueImplSP
proxy_sp(new ValueImpl(m_opaque_sp
->GetRootSP(),
779 m_opaque_sp
->GetUseDynamic(), false));
780 value_sb
.SetSP(proxy_sp
);
785 lldb::SBValue
SBValue::GetSyntheticValue() {
786 LLDB_INSTRUMENT_VA(this);
790 ValueImplSP
proxy_sp(new ValueImpl(m_opaque_sp
->GetRootSP(),
791 m_opaque_sp
->GetUseDynamic(), true));
792 value_sb
.SetSP(proxy_sp
);
793 if (!value_sb
.IsSynthetic()) {
800 lldb::DynamicValueType
SBValue::GetPreferDynamicValue() {
801 LLDB_INSTRUMENT_VA(this);
804 return eNoDynamicValues
;
805 return m_opaque_sp
->GetUseDynamic();
808 void SBValue::SetPreferDynamicValue(lldb::DynamicValueType use_dynamic
) {
809 LLDB_INSTRUMENT_VA(this, use_dynamic
);
812 return m_opaque_sp
->SetUseDynamic(use_dynamic
);
815 bool SBValue::GetPreferSyntheticValue() {
816 LLDB_INSTRUMENT_VA(this);
820 return m_opaque_sp
->GetUseSynthetic();
823 void SBValue::SetPreferSyntheticValue(bool use_synthetic
) {
824 LLDB_INSTRUMENT_VA(this, use_synthetic
);
827 return m_opaque_sp
->SetUseSynthetic(use_synthetic
);
830 bool SBValue::IsDynamic() {
831 LLDB_INSTRUMENT_VA(this);
834 lldb::ValueObjectSP
value_sp(GetSP(locker
));
836 return value_sp
->IsDynamic();
840 bool SBValue::IsSynthetic() {
841 LLDB_INSTRUMENT_VA(this);
844 lldb::ValueObjectSP
value_sp(GetSP(locker
));
846 return value_sp
->IsSynthetic();
850 bool SBValue::IsSyntheticChildrenGenerated() {
851 LLDB_INSTRUMENT_VA(this);
854 lldb::ValueObjectSP
value_sp(GetSP(locker
));
856 return value_sp
->IsSyntheticChildrenGenerated();
860 void SBValue::SetSyntheticChildrenGenerated(bool is
) {
861 LLDB_INSTRUMENT_VA(this, is
);
864 lldb::ValueObjectSP
value_sp(GetSP(locker
));
866 return value_sp
->SetSyntheticChildrenGenerated(is
);
869 lldb::SBValue
SBValue::GetValueForExpressionPath(const char *expr_path
) {
870 LLDB_INSTRUMENT_VA(this, expr_path
);
872 lldb::ValueObjectSP child_sp
;
874 lldb::ValueObjectSP
value_sp(GetSP(locker
));
876 // using default values for all the fancy options, just do it if you can
877 child_sp
= value_sp
->GetValueForExpressionPath(expr_path
);
881 sb_value
.SetSP(child_sp
, GetPreferDynamicValue(), GetPreferSyntheticValue());
886 int64_t SBValue::GetValueAsSigned(SBError
&error
, int64_t fail_value
) {
887 LLDB_INSTRUMENT_VA(this, error
, fail_value
);
891 lldb::ValueObjectSP
value_sp(GetSP(locker
));
894 uint64_t ret_val
= fail_value
;
895 ret_val
= value_sp
->GetValueAsSigned(fail_value
, &success
);
897 error
= Status::FromErrorString("could not resolve value");
900 error
= Status::FromErrorStringWithFormat("could not get SBValue: %s",
901 locker
.GetError().AsCString());
906 uint64_t SBValue::GetValueAsUnsigned(SBError
&error
, uint64_t fail_value
) {
907 LLDB_INSTRUMENT_VA(this, error
, fail_value
);
911 lldb::ValueObjectSP
value_sp(GetSP(locker
));
914 uint64_t ret_val
= fail_value
;
915 ret_val
= value_sp
->GetValueAsUnsigned(fail_value
, &success
);
917 error
= Status::FromErrorString("could not resolve value");
920 error
= Status::FromErrorStringWithFormat("could not get SBValue: %s",
921 locker
.GetError().AsCString());
926 int64_t SBValue::GetValueAsSigned(int64_t fail_value
) {
927 LLDB_INSTRUMENT_VA(this, fail_value
);
930 lldb::ValueObjectSP
value_sp(GetSP(locker
));
932 return value_sp
->GetValueAsSigned(fail_value
);
937 uint64_t SBValue::GetValueAsUnsigned(uint64_t fail_value
) {
938 LLDB_INSTRUMENT_VA(this, fail_value
);
941 lldb::ValueObjectSP
value_sp(GetSP(locker
));
943 return value_sp
->GetValueAsUnsigned(fail_value
);
948 lldb::addr_t
SBValue::GetValueAsAddress() {
949 addr_t fail_value
= LLDB_INVALID_ADDRESS
;
951 lldb::ValueObjectSP
value_sp(GetSP(locker
));
954 uint64_t ret_val
= fail_value
;
955 ret_val
= value_sp
->GetValueAsUnsigned(fail_value
, &success
);
958 ProcessSP process_sp
= m_opaque_sp
->GetProcessSP();
961 return process_sp
->FixDataAddress(ret_val
);
967 bool SBValue::MightHaveChildren() {
968 LLDB_INSTRUMENT_VA(this);
970 bool has_children
= false;
972 lldb::ValueObjectSP
value_sp(GetSP(locker
));
974 has_children
= value_sp
->MightHaveChildren();
979 bool SBValue::IsRuntimeSupportValue() {
980 LLDB_INSTRUMENT_VA(this);
982 bool is_support
= false;
984 lldb::ValueObjectSP
value_sp(GetSP(locker
));
986 is_support
= value_sp
->IsRuntimeSupportValue();
991 uint32_t SBValue::GetNumChildren() {
992 LLDB_INSTRUMENT_VA(this);
994 return GetNumChildren(UINT32_MAX
);
997 uint32_t SBValue::GetNumChildren(uint32_t max
) {
998 LLDB_INSTRUMENT_VA(this, max
);
1000 uint32_t num_children
= 0;
1003 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1005 num_children
= value_sp
->GetNumChildrenIgnoringErrors(max
);
1007 return num_children
;
1010 SBValue
SBValue::Dereference() {
1011 LLDB_INSTRUMENT_VA(this);
1015 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1018 sb_value
= value_sp
->Dereference(error
);
1024 // Deprecated - please use GetType().IsPointerType() instead.
1025 bool SBValue::TypeIsPointerType() {
1026 LLDB_INSTRUMENT_VA(this);
1028 return GetType().IsPointerType();
1031 void *SBValue::GetOpaqueType() {
1032 LLDB_INSTRUMENT_VA(this);
1035 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1037 return value_sp
->GetCompilerType().GetOpaqueQualType();
1041 lldb::SBTarget
SBValue::GetTarget() {
1042 LLDB_INSTRUMENT_VA(this);
1047 target_sp
= m_opaque_sp
->GetTargetSP();
1048 sb_target
.SetSP(target_sp
);
1054 lldb::SBProcess
SBValue::GetProcess() {
1055 LLDB_INSTRUMENT_VA(this);
1057 SBProcess sb_process
;
1058 ProcessSP process_sp
;
1060 process_sp
= m_opaque_sp
->GetProcessSP();
1061 sb_process
.SetSP(process_sp
);
1067 lldb::SBThread
SBValue::GetThread() {
1068 LLDB_INSTRUMENT_VA(this);
1073 thread_sp
= m_opaque_sp
->GetThreadSP();
1074 sb_thread
.SetThread(thread_sp
);
1080 lldb::SBFrame
SBValue::GetFrame() {
1081 LLDB_INSTRUMENT_VA(this);
1084 StackFrameSP frame_sp
;
1086 frame_sp
= m_opaque_sp
->GetFrameSP();
1087 sb_frame
.SetFrameSP(frame_sp
);
1093 lldb::ValueObjectSP
SBValue::GetSP(ValueLocker
&locker
) const {
1094 // IsValid means that the SBValue has a value in it. But that's not the
1095 // only time that ValueObjects are useful. We also want to return the value
1096 // if there's an error state in it.
1097 if (!m_opaque_sp
|| (!m_opaque_sp
->IsValid()
1098 && (m_opaque_sp
->GetRootSP()
1099 && !m_opaque_sp
->GetRootSP()->GetError().Fail()))) {
1100 locker
.GetError() = Status::FromErrorString("No value");
1101 return ValueObjectSP();
1103 return locker
.GetLockedSP(*m_opaque_sp
.get());
1106 lldb::ValueObjectSP
SBValue::GetSP() const {
1107 LLDB_INSTRUMENT_VA(this);
1110 return GetSP(locker
);
1113 void SBValue::SetSP(ValueImplSP impl_sp
) { m_opaque_sp
= impl_sp
; }
1115 void SBValue::SetSP(const lldb::ValueObjectSP
&sp
) {
1117 lldb::TargetSP
target_sp(sp
->GetTargetSP());
1119 lldb::DynamicValueType use_dynamic
= target_sp
->GetPreferDynamicValue();
1120 bool use_synthetic
=
1121 target_sp
->TargetProperties::GetEnableSyntheticValue();
1122 m_opaque_sp
= ValueImplSP(new ValueImpl(sp
, use_dynamic
, use_synthetic
));
1124 m_opaque_sp
= ValueImplSP(new ValueImpl(sp
, eNoDynamicValues
, true));
1126 m_opaque_sp
= ValueImplSP(new ValueImpl(sp
, eNoDynamicValues
, false));
1129 void SBValue::SetSP(const lldb::ValueObjectSP
&sp
,
1130 lldb::DynamicValueType use_dynamic
) {
1132 lldb::TargetSP
target_sp(sp
->GetTargetSP());
1134 bool use_synthetic
=
1135 target_sp
->TargetProperties::GetEnableSyntheticValue();
1136 SetSP(sp
, use_dynamic
, use_synthetic
);
1138 SetSP(sp
, use_dynamic
, true);
1140 SetSP(sp
, use_dynamic
, false);
1143 void SBValue::SetSP(const lldb::ValueObjectSP
&sp
, bool use_synthetic
) {
1145 lldb::TargetSP
target_sp(sp
->GetTargetSP());
1147 lldb::DynamicValueType use_dynamic
= target_sp
->GetPreferDynamicValue();
1148 SetSP(sp
, use_dynamic
, use_synthetic
);
1150 SetSP(sp
, eNoDynamicValues
, use_synthetic
);
1152 SetSP(sp
, eNoDynamicValues
, use_synthetic
);
1155 void SBValue::SetSP(const lldb::ValueObjectSP
&sp
,
1156 lldb::DynamicValueType use_dynamic
, bool use_synthetic
) {
1157 m_opaque_sp
= ValueImplSP(new ValueImpl(sp
, use_dynamic
, use_synthetic
));
1160 void SBValue::SetSP(const lldb::ValueObjectSP
&sp
,
1161 lldb::DynamicValueType use_dynamic
, bool use_synthetic
,
1164 ValueImplSP(new ValueImpl(sp
, use_dynamic
, use_synthetic
, name
));
1167 bool SBValue::GetExpressionPath(SBStream
&description
) {
1168 LLDB_INSTRUMENT_VA(this, description
);
1171 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1173 value_sp
->GetExpressionPath(description
.ref());
1179 bool SBValue::GetExpressionPath(SBStream
&description
,
1180 bool qualify_cxx_base_classes
) {
1181 LLDB_INSTRUMENT_VA(this, description
, qualify_cxx_base_classes
);
1184 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1186 value_sp
->GetExpressionPath(description
.ref());
1192 lldb::SBValue
SBValue::EvaluateExpression(const char *expr
) const {
1193 LLDB_INSTRUMENT_VA(this, expr
);
1196 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1200 lldb::TargetSP target_sp
= value_sp
->GetTargetSP();
1204 lldb::SBExpressionOptions options
;
1205 options
.SetFetchDynamicValue(target_sp
->GetPreferDynamicValue());
1206 options
.SetUnwindOnError(true);
1207 options
.SetIgnoreBreakpoints(true);
1209 return EvaluateExpression(expr
, options
, nullptr);
1213 SBValue::EvaluateExpression(const char *expr
,
1214 const SBExpressionOptions
&options
) const {
1215 LLDB_INSTRUMENT_VA(this, expr
, options
);
1217 return EvaluateExpression(expr
, options
, nullptr);
1220 lldb::SBValue
SBValue::EvaluateExpression(const char *expr
,
1221 const SBExpressionOptions
&options
,
1222 const char *name
) const {
1223 LLDB_INSTRUMENT_VA(this, expr
, options
, name
);
1225 if (!expr
|| expr
[0] == '\0') {
1231 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1236 lldb::TargetSP target_sp
= value_sp
->GetTargetSP();
1241 std::lock_guard
<std::recursive_mutex
> guard(target_sp
->GetAPIMutex());
1242 ExecutionContext
exe_ctx(target_sp
.get());
1244 StackFrame
*frame
= exe_ctx
.GetFramePtr();
1249 ValueObjectSP res_val_sp
;
1250 target_sp
->EvaluateExpression(expr
, frame
, res_val_sp
, options
.ref(), nullptr,
1254 res_val_sp
->SetName(ConstString(name
));
1257 result
.SetSP(res_val_sp
, options
.GetFetchDynamicValue());
1261 bool SBValue::GetDescription(SBStream
&description
) {
1262 LLDB_INSTRUMENT_VA(this, description
);
1264 Stream
&strm
= description
.ref();
1267 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1269 DumpValueObjectOptions options
;
1270 options
.SetUseDynamicType(m_opaque_sp
->GetUseDynamic());
1271 options
.SetUseSyntheticValue(m_opaque_sp
->GetUseSynthetic());
1272 if (llvm::Error error
= value_sp
->Dump(strm
, options
)) {
1273 strm
<< "error: " << toString(std::move(error
));
1277 strm
.PutCString("No value");
1283 lldb::Format
SBValue::GetFormat() {
1284 LLDB_INSTRUMENT_VA(this);
1287 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1289 return value_sp
->GetFormat();
1290 return eFormatDefault
;
1293 void SBValue::SetFormat(lldb::Format format
) {
1294 LLDB_INSTRUMENT_VA(this, format
);
1297 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1299 value_sp
->SetFormat(format
);
1302 lldb::SBValue
SBValue::AddressOf() {
1303 LLDB_INSTRUMENT_VA(this);
1307 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1310 sb_value
.SetSP(value_sp
->AddressOf(error
), GetPreferDynamicValue(),
1311 GetPreferSyntheticValue());
1317 lldb::addr_t
SBValue::GetLoadAddress() {
1318 LLDB_INSTRUMENT_VA(this);
1320 lldb::addr_t value
= LLDB_INVALID_ADDRESS
;
1322 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1324 return value_sp
->GetLoadAddress();
1329 lldb::SBAddress
SBValue::GetAddress() {
1330 LLDB_INSTRUMENT_VA(this);
1334 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1336 TargetSP
target_sp(value_sp
->GetTargetSP());
1338 lldb::addr_t value
= LLDB_INVALID_ADDRESS
;
1339 const bool scalar_is_load_address
= true;
1340 AddressType addr_type
;
1341 value
= value_sp
->GetAddressOf(scalar_is_load_address
, &addr_type
);
1342 if (addr_type
== eAddressTypeFile
) {
1343 ModuleSP
module_sp(value_sp
->GetModule());
1345 module_sp
->ResolveFileAddress(value
, addr
);
1346 } else if (addr_type
== eAddressTypeLoad
) {
1347 // no need to check the return value on this.. if it can actually do
1348 // the resolve addr will be in the form (section,offset), otherwise it
1349 // will simply be returned as (NULL, value)
1350 addr
.SetLoadAddress(value
, target_sp
.get());
1355 return SBAddress(addr
);
1358 lldb::SBData
SBValue::GetPointeeData(uint32_t item_idx
, uint32_t item_count
) {
1359 LLDB_INSTRUMENT_VA(this, item_idx
, item_count
);
1361 lldb::SBData sb_data
;
1363 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1365 TargetSP
target_sp(value_sp
->GetTargetSP());
1367 DataExtractorSP
data_sp(new DataExtractor());
1368 value_sp
->GetPointeeData(*data_sp
, item_idx
, item_count
);
1369 if (data_sp
->GetByteSize() > 0)
1377 lldb::SBData
SBValue::GetData() {
1378 LLDB_INSTRUMENT_VA(this);
1380 lldb::SBData sb_data
;
1382 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1384 DataExtractorSP
data_sp(new DataExtractor());
1386 value_sp
->GetData(*data_sp
, error
);
1387 if (error
.Success())
1394 bool SBValue::SetData(lldb::SBData
&data
, SBError
&error
) {
1395 LLDB_INSTRUMENT_VA(this, data
, error
);
1398 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1402 DataExtractor
*data_extractor
= data
.get();
1404 if (!data_extractor
) {
1405 error
= Status::FromErrorString("No data to set");
1410 value_sp
->SetData(*data_extractor
, set_error
);
1412 if (!set_error
.Success()) {
1413 error
= Status::FromErrorStringWithFormat("Couldn't set data: %s",
1414 set_error
.AsCString());
1419 error
= Status::FromErrorStringWithFormat(
1420 "Couldn't set data: could not get SBValue: %s",
1421 locker
.GetError().AsCString());
1428 lldb::SBValue
SBValue::Clone(const char *new_name
) {
1429 LLDB_INSTRUMENT_VA(this, new_name
);
1432 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1435 return lldb::SBValue(value_sp
->Clone(ConstString(new_name
)));
1437 return lldb::SBValue();
1440 lldb::SBDeclaration
SBValue::GetDeclaration() {
1441 LLDB_INSTRUMENT_VA(this);
1444 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1445 SBDeclaration decl_sb
;
1448 if (value_sp
->GetDeclaration(decl
))
1449 decl_sb
.SetDeclaration(decl
);
1454 lldb::SBWatchpoint
SBValue::Watch(bool resolve_location
, bool read
, bool write
,
1456 LLDB_INSTRUMENT_VA(this, resolve_location
, read
, write
, error
);
1458 SBWatchpoint sb_watchpoint
;
1460 // If the SBValue is not valid, there's no point in even trying to watch it.
1462 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1463 TargetSP
target_sp(GetTarget().GetSP());
1464 if (value_sp
&& target_sp
) {
1465 // Read and Write cannot both be false.
1466 if (!read
&& !write
)
1467 return sb_watchpoint
;
1469 // If the value is not in scope, don't try and watch and invalid value
1471 return sb_watchpoint
;
1473 addr_t addr
= GetLoadAddress();
1474 if (addr
== LLDB_INVALID_ADDRESS
)
1475 return sb_watchpoint
;
1476 size_t byte_size
= GetByteSize();
1478 return sb_watchpoint
;
1480 uint32_t watch_type
= 0;
1482 watch_type
|= LLDB_WATCH_TYPE_READ
;
1483 // read + write, the most likely intention
1484 // is to catch all writes to this, not just
1485 // value modifications.
1487 watch_type
|= LLDB_WATCH_TYPE_WRITE
;
1490 watch_type
|= LLDB_WATCH_TYPE_MODIFY
;
1494 CompilerType
type(value_sp
->GetCompilerType());
1495 WatchpointSP watchpoint_sp
=
1496 target_sp
->CreateWatchpoint(addr
, byte_size
, &type
, watch_type
, rc
);
1497 error
.SetError(std::move(rc
));
1499 if (watchpoint_sp
) {
1500 sb_watchpoint
.SetSP(watchpoint_sp
);
1502 if (value_sp
->GetDeclaration(decl
)) {
1503 if (decl
.GetFile()) {
1505 // True to show fullpath for declaration file.
1506 decl
.DumpStopContext(&ss
, true);
1507 watchpoint_sp
->SetDeclInfo(std::string(ss
.GetString()));
1511 } else if (target_sp
) {
1512 error
= Status::FromErrorStringWithFormat("could not get SBValue: %s",
1513 locker
.GetError().AsCString());
1515 error
= Status::FromErrorString(
1516 "could not set watchpoint, a target is required");
1519 return sb_watchpoint
;
1522 // FIXME: Remove this method impl (as well as the decl in .h) once it is no
1524 // Backward compatibility fix in the interim.
1525 lldb::SBWatchpoint
SBValue::Watch(bool resolve_location
, bool read
,
1527 LLDB_INSTRUMENT_VA(this, resolve_location
, read
, write
);
1530 return Watch(resolve_location
, read
, write
, error
);
1533 lldb::SBWatchpoint
SBValue::WatchPointee(bool resolve_location
, bool read
,
1534 bool write
, SBError
&error
) {
1535 LLDB_INSTRUMENT_VA(this, resolve_location
, read
, write
, error
);
1537 SBWatchpoint sb_watchpoint
;
1538 if (IsInScope() && GetType().IsPointerType())
1539 sb_watchpoint
= Dereference().Watch(resolve_location
, read
, write
, error
);
1540 return sb_watchpoint
;
1543 lldb::SBValue
SBValue::Persist() {
1544 LLDB_INSTRUMENT_VA(this);
1547 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1548 SBValue persisted_sb
;
1550 persisted_sb
.SetSP(value_sp
->Persist());
1552 return persisted_sb
;
1555 lldb::SBValue
SBValue::GetVTable() {
1558 lldb::ValueObjectSP
value_sp(GetSP(locker
));
1562 vtable_sb
.SetSP(value_sp
->GetVTable());