1cfca06d7SDimitry Andric //===-- ValueObjectVariable.cpp -------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste
9f034231aSEd Maste #include "lldb/Core/ValueObjectVariable.h"
10f034231aSEd Maste
1194994d37SDimitry Andric #include "lldb/Core/Address.h"
1294994d37SDimitry Andric #include "lldb/Core/AddressRange.h"
13344a3780SDimitry Andric #include "lldb/Core/Declaration.h"
14f034231aSEd Maste #include "lldb/Core/Module.h"
15f034231aSEd Maste #include "lldb/Core/Value.h"
161f917f69SDimitry Andric #include "lldb/Expression/DWARFExpressionList.h"
17f034231aSEd Maste #include "lldb/Symbol/Function.h"
18f034231aSEd Maste #include "lldb/Symbol/ObjectFile.h"
19f034231aSEd Maste #include "lldb/Symbol/SymbolContext.h"
20f034231aSEd Maste #include "lldb/Symbol/SymbolContextScope.h"
21f034231aSEd Maste #include "lldb/Symbol/Type.h"
22f034231aSEd Maste #include "lldb/Symbol/Variable.h"
23f034231aSEd Maste #include "lldb/Target/ExecutionContext.h"
24f034231aSEd Maste #include "lldb/Target/Process.h"
25f034231aSEd Maste #include "lldb/Target/RegisterContext.h"
26f034231aSEd Maste #include "lldb/Target/Target.h"
2794994d37SDimitry Andric #include "lldb/Utility/DataExtractor.h"
2894994d37SDimitry Andric #include "lldb/Utility/RegisterValue.h"
2994994d37SDimitry Andric #include "lldb/Utility/Scalar.h"
3094994d37SDimitry Andric #include "lldb/Utility/Status.h"
3194994d37SDimitry Andric #include "lldb/lldb-private-enumerations.h"
3294994d37SDimitry Andric #include "lldb/lldb-types.h"
33f034231aSEd Maste
3494994d37SDimitry Andric #include "llvm/ADT/StringRef.h"
3574a628f7SDimitry Andric
36344a3780SDimitry Andric #include <cassert>
3794994d37SDimitry Andric #include <memory>
38e3b55780SDimitry Andric #include <optional>
3974a628f7SDimitry Andric
4074a628f7SDimitry Andric namespace lldb_private {
4174a628f7SDimitry Andric class ExecutionContextScope;
4274a628f7SDimitry Andric }
4374a628f7SDimitry Andric namespace lldb_private {
4474a628f7SDimitry Andric class StackFrame;
4574a628f7SDimitry Andric }
4674a628f7SDimitry Andric namespace lldb_private {
4774a628f7SDimitry Andric struct RegisterInfo;
4874a628f7SDimitry Andric }
49f034231aSEd Maste using namespace lldb_private;
50f034231aSEd Maste
51f034231aSEd Maste lldb::ValueObjectSP
Create(ExecutionContextScope * exe_scope,const lldb::VariableSP & var_sp)5214f1b3e8SDimitry Andric ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
5314f1b3e8SDimitry Andric const lldb::VariableSP &var_sp) {
54cfca06d7SDimitry Andric auto manager_sp = ValueObjectManager::Create();
55cfca06d7SDimitry Andric return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP();
56f034231aSEd Maste }
57f034231aSEd Maste
ValueObjectVariable(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const lldb::VariableSP & var_sp)5814f1b3e8SDimitry Andric ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
59cfca06d7SDimitry Andric ValueObjectManager &manager,
6014f1b3e8SDimitry Andric const lldb::VariableSP &var_sp)
61cfca06d7SDimitry Andric : ValueObject(exe_scope, manager), m_variable_sp(var_sp) {
62f034231aSEd Maste // Do not attempt to construct one of these objects with no variable!
635f29bb8aSDimitry Andric assert(m_variable_sp.get() != nullptr);
64f034231aSEd Maste m_name = var_sp->GetName();
65f034231aSEd Maste }
66f034231aSEd Maste
67344a3780SDimitry Andric ValueObjectVariable::~ValueObjectVariable() = default;
68f034231aSEd Maste
GetCompilerTypeImpl()6914f1b3e8SDimitry Andric CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
70f034231aSEd Maste Type *var_type = m_variable_sp->GetType();
71f034231aSEd Maste if (var_type)
72e81d9d49SDimitry Andric return var_type->GetForwardCompilerType();
73e81d9d49SDimitry Andric return CompilerType();
74f034231aSEd Maste }
75f034231aSEd Maste
GetTypeName()7614f1b3e8SDimitry Andric ConstString ValueObjectVariable::GetTypeName() {
77f034231aSEd Maste Type *var_type = m_variable_sp->GetType();
78f034231aSEd Maste if (var_type)
79f034231aSEd Maste return var_type->GetName();
80f034231aSEd Maste return ConstString();
81f034231aSEd Maste }
82f034231aSEd Maste
GetDisplayTypeName()8314f1b3e8SDimitry Andric ConstString ValueObjectVariable::GetDisplayTypeName() {
840cac4ca3SEd Maste Type *var_type = m_variable_sp->GetType();
850cac4ca3SEd Maste if (var_type)
86e81d9d49SDimitry Andric return var_type->GetForwardCompilerType().GetDisplayTypeName();
870cac4ca3SEd Maste return ConstString();
880cac4ca3SEd Maste }
890cac4ca3SEd Maste
GetQualifiedTypeName()9014f1b3e8SDimitry Andric ConstString ValueObjectVariable::GetQualifiedTypeName() {
91f034231aSEd Maste Type *var_type = m_variable_sp->GetType();
92f034231aSEd Maste if (var_type)
93f034231aSEd Maste return var_type->GetQualifiedName();
94f034231aSEd Maste return ConstString();
95f034231aSEd Maste }
96f034231aSEd Maste
97ac9a064cSDimitry Andric llvm::Expected<uint32_t>
CalculateNumChildren(uint32_t max)98ac9a064cSDimitry Andric ValueObjectVariable::CalculateNumChildren(uint32_t max) {
99e81d9d49SDimitry Andric CompilerType type(GetCompilerType());
100f034231aSEd Maste
101f034231aSEd Maste if (!type.IsValid())
102ac9a064cSDimitry Andric return llvm::make_error<llvm::StringError>("invalid type",
103ac9a064cSDimitry Andric llvm::inconvertibleErrorCode());
104f034231aSEd Maste
10594994d37SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
106f034231aSEd Maste const bool omit_empty_base_classes = true;
10794994d37SDimitry Andric auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
108ac9a064cSDimitry Andric if (!child_count)
109ac9a064cSDimitry Andric return child_count;
110ac9a064cSDimitry Andric return *child_count <= max ? *child_count : max;
111f034231aSEd Maste }
112f034231aSEd Maste
GetByteSize()113e3b55780SDimitry Andric std::optional<uint64_t> ValueObjectVariable::GetByteSize() {
11412bd4897SEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
11512bd4897SEd Maste
116e81d9d49SDimitry Andric CompilerType type(GetCompilerType());
117f034231aSEd Maste
118f034231aSEd Maste if (!type.IsValid())
119b60736ecSDimitry Andric return {};
120f034231aSEd Maste
121b60736ecSDimitry Andric return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
122f034231aSEd Maste }
123f034231aSEd Maste
GetValueType() const12414f1b3e8SDimitry Andric lldb::ValueType ValueObjectVariable::GetValueType() const {
125f034231aSEd Maste if (m_variable_sp)
126f034231aSEd Maste return m_variable_sp->GetScope();
127f034231aSEd Maste return lldb::eValueTypeInvalid;
128f034231aSEd Maste }
129f034231aSEd Maste
UpdateValue()13014f1b3e8SDimitry Andric bool ValueObjectVariable::UpdateValue() {
131f034231aSEd Maste SetValueIsValid(false);
132f034231aSEd Maste m_error.Clear();
133f034231aSEd Maste
134f034231aSEd Maste Variable *variable = m_variable_sp.get();
1351f917f69SDimitry Andric DWARFExpressionList &expr_list = variable->LocationExpressionList();
136f034231aSEd Maste
13714f1b3e8SDimitry Andric if (variable->GetLocationIsConstantValueData()) {
138f034231aSEd Maste // expr doesn't contain DWARF bytes, it contains the constant variable
139f034231aSEd Maste // value bytes themselves...
1401f917f69SDimitry Andric if (expr_list.GetExpressionData(m_data)) {
141b60736ecSDimitry Andric if (m_data.GetDataStart() && m_data.GetByteSize())
142b60736ecSDimitry Andric m_value.SetBytes(m_data.GetDataStart(), m_data.GetByteSize());
143344a3780SDimitry Andric m_value.SetContext(Value::ContextType::Variable, variable);
1441f917f69SDimitry Andric } else
145f034231aSEd Maste m_error.SetErrorString("empty constant data");
146f034231aSEd Maste // constant bytes can't be edited - sorry
147344a3780SDimitry Andric m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
14814f1b3e8SDimitry Andric } else {
149f034231aSEd Maste lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
150f034231aSEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
151f034231aSEd Maste
152f034231aSEd Maste Target *target = exe_ctx.GetTargetPtr();
15314f1b3e8SDimitry Andric if (target) {
154f034231aSEd Maste m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
155f034231aSEd Maste m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
156f034231aSEd Maste }
157f034231aSEd Maste
1581f917f69SDimitry Andric if (!expr_list.IsAlwaysValidSingleExpr()) {
159f034231aSEd Maste SymbolContext sc;
160f034231aSEd Maste variable->CalculateSymbolContext(&sc);
161f034231aSEd Maste if (sc.function)
16214f1b3e8SDimitry Andric loclist_base_load_addr =
16314f1b3e8SDimitry Andric sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
16414f1b3e8SDimitry Andric target);
165f034231aSEd Maste }
166f034231aSEd Maste Value old_value(m_value);
167ac9a064cSDimitry Andric llvm::Expected<Value> maybe_value = expr_list.Evaluate(
168ac9a064cSDimitry Andric &exe_ctx, nullptr, loclist_base_load_addr, nullptr, nullptr);
169ac9a064cSDimitry Andric
170ac9a064cSDimitry Andric if (maybe_value) {
171ac9a064cSDimitry Andric m_value = *maybe_value;
172f034231aSEd Maste m_resolved_value = m_value;
173344a3780SDimitry Andric m_value.SetContext(Value::ContextType::Variable, variable);
174f21a844fSEd Maste
175e81d9d49SDimitry Andric CompilerType compiler_type = GetCompilerType();
176e81d9d49SDimitry Andric if (compiler_type.IsValid())
177e81d9d49SDimitry Andric m_value.SetCompilerType(compiler_type);
178f034231aSEd Maste
179f034231aSEd Maste Value::ValueType value_type = m_value.GetValueType();
180f034231aSEd Maste
181cfca06d7SDimitry Andric // The size of the buffer within m_value can be less than the size
182cfca06d7SDimitry Andric // prescribed by its type. E.g. this can happen when an expression only
183cfca06d7SDimitry Andric // partially describes an object (say, because it contains DW_OP_piece).
184cfca06d7SDimitry Andric //
185cfca06d7SDimitry Andric // In this case, grow m_value to the expected size. An alternative way to
186cfca06d7SDimitry Andric // handle this is to teach Value::GetValueAsData() and ValueObjectChild
187cfca06d7SDimitry Andric // not to read past the end of a host buffer, but this gets impractically
188cfca06d7SDimitry Andric // complicated as a Value's host buffer may be shared with a distant
189cfca06d7SDimitry Andric // ancestor or sibling in the ValueObject hierarchy.
190cfca06d7SDimitry Andric //
191cfca06d7SDimitry Andric // FIXME: When we grow m_value, we should represent the added bits as
192cfca06d7SDimitry Andric // undefined somehow instead of as 0's.
193344a3780SDimitry Andric if (value_type == Value::ValueType::HostAddress &&
194cfca06d7SDimitry Andric compiler_type.IsValid()) {
195cfca06d7SDimitry Andric if (size_t value_buf_size = m_value.GetBuffer().GetByteSize()) {
196cfca06d7SDimitry Andric size_t value_size = m_value.GetValueByteSize(&m_error, &exe_ctx);
197cfca06d7SDimitry Andric if (m_error.Success() && value_buf_size < value_size)
198cfca06d7SDimitry Andric m_value.ResizeData(value_size);
199cfca06d7SDimitry Andric }
200cfca06d7SDimitry Andric }
201cfca06d7SDimitry Andric
202205afe67SEd Maste Process *process = exe_ctx.GetProcessPtr();
203205afe67SEd Maste const bool process_is_alive = process && process->IsAlive();
204f034231aSEd Maste
20514f1b3e8SDimitry Andric switch (value_type) {
206344a3780SDimitry Andric case Value::ValueType::Invalid:
207344a3780SDimitry Andric m_error.SetErrorString("invalid value");
208344a3780SDimitry Andric break;
209344a3780SDimitry Andric case Value::ValueType::Scalar:
210f73363f1SDimitry Andric // The variable value is in the Scalar value inside the m_value. We can
211f73363f1SDimitry Andric // point our m_data right to it.
21214f1b3e8SDimitry Andric m_error =
213ead24645SDimitry Andric m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
214f034231aSEd Maste break;
215f034231aSEd Maste
216344a3780SDimitry Andric case Value::ValueType::FileAddress:
217344a3780SDimitry Andric case Value::ValueType::LoadAddress:
218344a3780SDimitry Andric case Value::ValueType::HostAddress:
219f73363f1SDimitry Andric // The DWARF expression result was an address in the inferior process.
220f73363f1SDimitry Andric // If this variable is an aggregate type, we just need the address as
221f73363f1SDimitry Andric // the main value as all child variable objects will rely upon this
222f73363f1SDimitry Andric // location and add an offset and then read their own values as needed.
223f73363f1SDimitry Andric // If this variable is a simple type, we read all data for it into
224f73363f1SDimitry Andric // m_data. Make sure this type has a value before we try and read it
225f034231aSEd Maste
226f034231aSEd Maste // If we have a file address, convert it to a load address if we can.
227344a3780SDimitry Andric if (value_type == Value::ValueType::FileAddress && process_is_alive)
228f73363f1SDimitry Andric m_value.ConvertToLoadAddress(GetModule().get(), target);
229f034231aSEd Maste
23014f1b3e8SDimitry Andric if (!CanProvideValue()) {
231f73363f1SDimitry Andric // this value object represents an aggregate type whose children have
232f73363f1SDimitry Andric // values, but this object does not. So we say we are changed if our
233f73363f1SDimitry Andric // location has changed.
23414f1b3e8SDimitry Andric SetValueDidChange(value_type != old_value.GetValueType() ||
23514f1b3e8SDimitry Andric m_value.GetScalar() != old_value.GetScalar());
23614f1b3e8SDimitry Andric } else {
237f73363f1SDimitry Andric // Copy the Value and set the context to use our Variable so it can
238f73363f1SDimitry Andric // extract read its value into m_data appropriately
239f034231aSEd Maste Value value(m_value);
240344a3780SDimitry Andric value.SetContext(Value::ContextType::Variable, variable);
24114f1b3e8SDimitry Andric m_error =
242ead24645SDimitry Andric value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
243205afe67SEd Maste
24414f1b3e8SDimitry Andric SetValueDidChange(value_type != old_value.GetValueType() ||
24514f1b3e8SDimitry Andric m_value.GetScalar() != old_value.GetScalar());
246f034231aSEd Maste }
247f034231aSEd Maste break;
248f034231aSEd Maste }
249f034231aSEd Maste
250f034231aSEd Maste SetValueIsValid(m_error.Success());
25114f1b3e8SDimitry Andric } else {
252ac9a064cSDimitry Andric m_error = maybe_value.takeError();
253f034231aSEd Maste // could not find location, won't allow editing
254344a3780SDimitry Andric m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
255f034231aSEd Maste }
256f034231aSEd Maste }
257cfca06d7SDimitry Andric
258f034231aSEd Maste return m_error.Success();
259f034231aSEd Maste }
260f034231aSEd Maste
DoUpdateChildrenAddressType(ValueObject & valobj)261cfca06d7SDimitry Andric void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject &valobj) {
262cfca06d7SDimitry Andric Value::ValueType value_type = valobj.GetValue().GetValueType();
263cfca06d7SDimitry Andric ExecutionContext exe_ctx(GetExecutionContextRef());
264cfca06d7SDimitry Andric Process *process = exe_ctx.GetProcessPtr();
265cfca06d7SDimitry Andric const bool process_is_alive = process && process->IsAlive();
266cfca06d7SDimitry Andric const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo();
267cfca06d7SDimitry Andric const bool is_pointer_or_ref =
268cfca06d7SDimitry Andric (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
269cfca06d7SDimitry Andric
270cfca06d7SDimitry Andric switch (value_type) {
271344a3780SDimitry Andric case Value::ValueType::Invalid:
272344a3780SDimitry Andric break;
273344a3780SDimitry Andric case Value::ValueType::FileAddress:
274cfca06d7SDimitry Andric // If this type is a pointer, then its children will be considered load
275cfca06d7SDimitry Andric // addresses if the pointer or reference is dereferenced, but only if
276cfca06d7SDimitry Andric // the process is alive.
277cfca06d7SDimitry Andric //
278cfca06d7SDimitry Andric // There could be global variables like in the following code:
279cfca06d7SDimitry Andric // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
280cfca06d7SDimitry Andric // Foo g_foo1;
281cfca06d7SDimitry Andric // Foo g_foo2;
282cfca06d7SDimitry Andric // LinkedListNode g_second_node = { &g_foo2, NULL };
283cfca06d7SDimitry Andric // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
284cfca06d7SDimitry Andric //
285cfca06d7SDimitry Andric // When we aren't running, we should be able to look at these variables
286cfca06d7SDimitry Andric // using the "target variable" command. Children of the "g_first_node"
287cfca06d7SDimitry Andric // always will be of the same address type as the parent. But children
288cfca06d7SDimitry Andric // of the "next" member of LinkedListNode will become load addresses if
289cfca06d7SDimitry Andric // we have a live process, or remain a file address if it was a file
290cfca06d7SDimitry Andric // address.
291cfca06d7SDimitry Andric if (process_is_alive && is_pointer_or_ref)
292cfca06d7SDimitry Andric valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
293cfca06d7SDimitry Andric else
294cfca06d7SDimitry Andric valobj.SetAddressTypeOfChildren(eAddressTypeFile);
295cfca06d7SDimitry Andric break;
296344a3780SDimitry Andric case Value::ValueType::HostAddress:
297cfca06d7SDimitry Andric // Same as above for load addresses, except children of pointer or refs
298cfca06d7SDimitry Andric // are always load addresses. Host addresses are used to store freeze
299cfca06d7SDimitry Andric // dried variables. If this type is a struct, the entire struct
300cfca06d7SDimitry Andric // contents will be copied into the heap of the
301cfca06d7SDimitry Andric // LLDB process, but we do not currently follow any pointers.
302cfca06d7SDimitry Andric if (is_pointer_or_ref)
303cfca06d7SDimitry Andric valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
304cfca06d7SDimitry Andric else
305cfca06d7SDimitry Andric valobj.SetAddressTypeOfChildren(eAddressTypeHost);
306cfca06d7SDimitry Andric break;
307344a3780SDimitry Andric case Value::ValueType::LoadAddress:
308344a3780SDimitry Andric case Value::ValueType::Scalar:
309cfca06d7SDimitry Andric valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
310cfca06d7SDimitry Andric break;
311cfca06d7SDimitry Andric }
312cfca06d7SDimitry Andric }
313cfca06d7SDimitry Andric
314cfca06d7SDimitry Andric
315cfca06d7SDimitry Andric
IsInScope()31614f1b3e8SDimitry Andric bool ValueObjectVariable::IsInScope() {
317f034231aSEd Maste const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
31814f1b3e8SDimitry Andric if (exe_ctx_ref.HasFrameRef()) {
319f034231aSEd Maste ExecutionContext exe_ctx(exe_ctx_ref);
320f034231aSEd Maste StackFrame *frame = exe_ctx.GetFramePtr();
32114f1b3e8SDimitry Andric if (frame) {
322f034231aSEd Maste return m_variable_sp->IsInScope(frame);
32314f1b3e8SDimitry Andric } else {
324f73363f1SDimitry Andric // This ValueObject had a frame at one time, but now we can't locate it,
325f73363f1SDimitry Andric // so return false since we probably aren't in scope.
326f034231aSEd Maste return false;
327f034231aSEd Maste }
328f034231aSEd Maste }
329f73363f1SDimitry Andric // We have a variable that wasn't tied to a frame, which means it is a global
330f73363f1SDimitry Andric // and is always in scope.
331f034231aSEd Maste return true;
332f034231aSEd Maste }
333f034231aSEd Maste
GetModule()33414f1b3e8SDimitry Andric lldb::ModuleSP ValueObjectVariable::GetModule() {
33514f1b3e8SDimitry Andric if (m_variable_sp) {
336f034231aSEd Maste SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
33714f1b3e8SDimitry Andric if (sc_scope) {
338f034231aSEd Maste return sc_scope->CalculateSymbolContextModule();
339f034231aSEd Maste }
340f034231aSEd Maste }
341f034231aSEd Maste return lldb::ModuleSP();
342f034231aSEd Maste }
343f034231aSEd Maste
GetSymbolContextScope()34414f1b3e8SDimitry Andric SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
345f034231aSEd Maste if (m_variable_sp)
346f034231aSEd Maste return m_variable_sp->GetSymbolContextScope();
3475f29bb8aSDimitry Andric return nullptr;
348f034231aSEd Maste }
349f034231aSEd Maste
GetDeclaration(Declaration & decl)35014f1b3e8SDimitry Andric bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
35114f1b3e8SDimitry Andric if (m_variable_sp) {
352f034231aSEd Maste decl = m_variable_sp->GetDeclaration();
353f034231aSEd Maste return true;
354f034231aSEd Maste }
355f034231aSEd Maste return false;
356f034231aSEd Maste }
357f034231aSEd Maste
GetLocationAsCString()35814f1b3e8SDimitry Andric const char *ValueObjectVariable::GetLocationAsCString() {
359344a3780SDimitry Andric if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo)
36014f1b3e8SDimitry Andric return GetLocationAsCStringImpl(m_resolved_value, m_data);
361f034231aSEd Maste else
362f034231aSEd Maste return ValueObject::GetLocationAsCString();
363f034231aSEd Maste }
364f034231aSEd Maste
SetValueFromCString(const char * value_str,Status & error)36514f1b3e8SDimitry Andric bool ValueObjectVariable::SetValueFromCString(const char *value_str,
366b76161e4SDimitry Andric Status &error) {
36714f1b3e8SDimitry Andric if (!UpdateValueIfNeeded()) {
368866dcdacSEd Maste error.SetErrorString("unable to update value before writing");
369866dcdacSEd Maste return false;
370866dcdacSEd Maste }
371866dcdacSEd Maste
372344a3780SDimitry Andric if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
373f034231aSEd Maste RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
374f034231aSEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
375f034231aSEd Maste RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
376f034231aSEd Maste RegisterValue reg_value;
37714f1b3e8SDimitry Andric if (!reg_info || !reg_ctx) {
378f034231aSEd Maste error.SetErrorString("unable to retrieve register info");
379f034231aSEd Maste return false;
380f034231aSEd Maste }
38114f1b3e8SDimitry Andric error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
382f034231aSEd Maste if (error.Fail())
383f034231aSEd Maste return false;
38414f1b3e8SDimitry Andric if (reg_ctx->WriteRegister(reg_info, reg_value)) {
385f034231aSEd Maste SetNeedsUpdate();
386f034231aSEd Maste return true;
38714f1b3e8SDimitry Andric } else {
388f034231aSEd Maste error.SetErrorString("unable to write back to register");
389f034231aSEd Maste return false;
390f034231aSEd Maste }
39114f1b3e8SDimitry Andric } else
392f034231aSEd Maste return ValueObject::SetValueFromCString(value_str, error);
393f034231aSEd Maste }
394f034231aSEd Maste
SetData(DataExtractor & data,Status & error)395b76161e4SDimitry Andric bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
39614f1b3e8SDimitry Andric if (!UpdateValueIfNeeded()) {
397866dcdacSEd Maste error.SetErrorString("unable to update value before writing");
398866dcdacSEd Maste return false;
399866dcdacSEd Maste }
400866dcdacSEd Maste
401344a3780SDimitry Andric if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
402f034231aSEd Maste RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
403f034231aSEd Maste ExecutionContext exe_ctx(GetExecutionContextRef());
404f034231aSEd Maste RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
405f034231aSEd Maste RegisterValue reg_value;
40614f1b3e8SDimitry Andric if (!reg_info || !reg_ctx) {
407f034231aSEd Maste error.SetErrorString("unable to retrieve register info");
408f034231aSEd Maste return false;
409f034231aSEd Maste }
410e3b55780SDimitry Andric error = reg_value.SetValueFromData(*reg_info, data, 0, true);
411f034231aSEd Maste if (error.Fail())
412f034231aSEd Maste return false;
41314f1b3e8SDimitry Andric if (reg_ctx->WriteRegister(reg_info, reg_value)) {
414f034231aSEd Maste SetNeedsUpdate();
415f034231aSEd Maste return true;
41614f1b3e8SDimitry Andric } else {
417f034231aSEd Maste error.SetErrorString("unable to write back to register");
418f034231aSEd Maste return false;
419f034231aSEd Maste }
42014f1b3e8SDimitry Andric } else
421f034231aSEd Maste return ValueObject::SetData(data, error);
422f034231aSEd Maste }
423