1ac7ddfbfSEd Maste //===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
2ac7ddfbfSEd Maste //
3ac7ddfbfSEd Maste //                     The LLVM Compiler Infrastructure
4ac7ddfbfSEd Maste //
5ac7ddfbfSEd Maste // This file is distributed under the University of Illinois Open Source
6ac7ddfbfSEd Maste // License. See LICENSE.TXT for details.
7ac7ddfbfSEd Maste //
8ac7ddfbfSEd Maste //===----------------------------------------------------------------------===//
9ac7ddfbfSEd Maste 
10ac7ddfbfSEd Maste #include "lldb/Core/ValueObjectVariable.h"
11ac7ddfbfSEd Maste 
12*b5893f02SDimitry Andric #include "lldb/Core/Address.h"
13*b5893f02SDimitry Andric #include "lldb/Core/AddressRange.h"
14ac7ddfbfSEd Maste #include "lldb/Core/Module.h"
15ac7ddfbfSEd Maste #include "lldb/Core/Value.h"
16*b5893f02SDimitry Andric #include "lldb/Expression/DWARFExpression.h"
17*b5893f02SDimitry Andric #include "lldb/Symbol/Declaration.h"
18ac7ddfbfSEd Maste #include "lldb/Symbol/Function.h"
19ac7ddfbfSEd Maste #include "lldb/Symbol/ObjectFile.h"
20ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolContext.h"
21ac7ddfbfSEd Maste #include "lldb/Symbol/SymbolContextScope.h"
22ac7ddfbfSEd Maste #include "lldb/Symbol/Type.h"
23ac7ddfbfSEd Maste #include "lldb/Symbol/Variable.h"
24ac7ddfbfSEd Maste #include "lldb/Target/ExecutionContext.h"
25ac7ddfbfSEd Maste #include "lldb/Target/Process.h"
26ac7ddfbfSEd Maste #include "lldb/Target/RegisterContext.h"
27ac7ddfbfSEd Maste #include "lldb/Target/Target.h"
28*b5893f02SDimitry Andric #include "lldb/Utility/DataExtractor.h"
29*b5893f02SDimitry Andric #include "lldb/Utility/RegisterValue.h"
30*b5893f02SDimitry Andric #include "lldb/Utility/Scalar.h"
31*b5893f02SDimitry Andric #include "lldb/Utility/Status.h"
32*b5893f02SDimitry Andric #include "lldb/lldb-private-enumerations.h"
33*b5893f02SDimitry Andric #include "lldb/lldb-types.h"
34ac7ddfbfSEd Maste 
35*b5893f02SDimitry Andric #include "llvm/ADT/StringRef.h"
36f678e45dSDimitry Andric 
37*b5893f02SDimitry Andric #include <assert.h>
38*b5893f02SDimitry Andric #include <memory>
39f678e45dSDimitry Andric 
40f678e45dSDimitry Andric namespace lldb_private {
41f678e45dSDimitry Andric class ExecutionContextScope;
42f678e45dSDimitry Andric }
43f678e45dSDimitry Andric namespace lldb_private {
44f678e45dSDimitry Andric class StackFrame;
45f678e45dSDimitry Andric }
46f678e45dSDimitry Andric namespace lldb_private {
47f678e45dSDimitry Andric struct RegisterInfo;
48f678e45dSDimitry Andric }
49ac7ddfbfSEd Maste using namespace lldb_private;
50ac7ddfbfSEd Maste 
51ac7ddfbfSEd Maste lldb::ValueObjectSP
Create(ExecutionContextScope * exe_scope,const lldb::VariableSP & var_sp)52435933ddSDimitry Andric ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
53435933ddSDimitry Andric                             const lldb::VariableSP &var_sp) {
54ac7ddfbfSEd Maste   return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
55ac7ddfbfSEd Maste }
56ac7ddfbfSEd Maste 
ValueObjectVariable(ExecutionContextScope * exe_scope,const lldb::VariableSP & var_sp)57435933ddSDimitry Andric ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
58435933ddSDimitry Andric                                          const lldb::VariableSP &var_sp)
59435933ddSDimitry Andric     : ValueObject(exe_scope), m_variable_sp(var_sp) {
60ac7ddfbfSEd Maste   // Do not attempt to construct one of these objects with no variable!
61ac7ddfbfSEd Maste   assert(m_variable_sp.get() != NULL);
62ac7ddfbfSEd Maste   m_name = var_sp->GetName();
63ac7ddfbfSEd Maste }
64ac7ddfbfSEd Maste 
~ValueObjectVariable()65435933ddSDimitry Andric ValueObjectVariable::~ValueObjectVariable() {}
66ac7ddfbfSEd Maste 
GetCompilerTypeImpl()67435933ddSDimitry Andric CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
68ac7ddfbfSEd Maste   Type *var_type = m_variable_sp->GetType();
69ac7ddfbfSEd Maste   if (var_type)
709f2f44ceSEd Maste     return var_type->GetForwardCompilerType();
719f2f44ceSEd Maste   return CompilerType();
72ac7ddfbfSEd Maste }
73ac7ddfbfSEd Maste 
GetTypeName()74435933ddSDimitry Andric ConstString ValueObjectVariable::GetTypeName() {
75ac7ddfbfSEd Maste   Type *var_type = m_variable_sp->GetType();
76ac7ddfbfSEd Maste   if (var_type)
77ac7ddfbfSEd Maste     return var_type->GetName();
78ac7ddfbfSEd Maste   return ConstString();
79ac7ddfbfSEd Maste }
80ac7ddfbfSEd Maste 
GetDisplayTypeName()81435933ddSDimitry Andric ConstString ValueObjectVariable::GetDisplayTypeName() {
820127ef0fSEd Maste   Type *var_type = m_variable_sp->GetType();
830127ef0fSEd Maste   if (var_type)
849f2f44ceSEd Maste     return var_type->GetForwardCompilerType().GetDisplayTypeName();
850127ef0fSEd Maste   return ConstString();
860127ef0fSEd Maste }
870127ef0fSEd Maste 
GetQualifiedTypeName()88435933ddSDimitry Andric ConstString ValueObjectVariable::GetQualifiedTypeName() {
89ac7ddfbfSEd Maste   Type *var_type = m_variable_sp->GetType();
90ac7ddfbfSEd Maste   if (var_type)
91ac7ddfbfSEd Maste     return var_type->GetQualifiedName();
92ac7ddfbfSEd Maste   return ConstString();
93ac7ddfbfSEd Maste }
94ac7ddfbfSEd Maste 
CalculateNumChildren(uint32_t max)95435933ddSDimitry Andric size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
969f2f44ceSEd Maste   CompilerType type(GetCompilerType());
97ac7ddfbfSEd Maste 
98ac7ddfbfSEd Maste   if (!type.IsValid())
99ac7ddfbfSEd Maste     return 0;
100ac7ddfbfSEd Maste 
101*b5893f02SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
102ac7ddfbfSEd Maste   const bool omit_empty_base_classes = true;
103*b5893f02SDimitry Andric   auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
1049f2f44ceSEd Maste   return child_count <= max ? child_count : max;
105ac7ddfbfSEd Maste }
106ac7ddfbfSEd Maste 
GetByteSize()107435933ddSDimitry Andric uint64_t ValueObjectVariable::GetByteSize() {
1081c3bbb01SEd Maste   ExecutionContext exe_ctx(GetExecutionContextRef());
1091c3bbb01SEd Maste 
1109f2f44ceSEd Maste   CompilerType type(GetCompilerType());
111ac7ddfbfSEd Maste 
112ac7ddfbfSEd Maste   if (!type.IsValid())
113ac7ddfbfSEd Maste     return 0;
114ac7ddfbfSEd Maste 
115*b5893f02SDimitry Andric   return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()).getValueOr(0);
116ac7ddfbfSEd Maste }
117ac7ddfbfSEd Maste 
GetValueType() const118435933ddSDimitry Andric lldb::ValueType ValueObjectVariable::GetValueType() const {
119ac7ddfbfSEd Maste   if (m_variable_sp)
120ac7ddfbfSEd Maste     return m_variable_sp->GetScope();
121ac7ddfbfSEd Maste   return lldb::eValueTypeInvalid;
122ac7ddfbfSEd Maste }
123ac7ddfbfSEd Maste 
UpdateValue()124435933ddSDimitry Andric bool ValueObjectVariable::UpdateValue() {
125ac7ddfbfSEd Maste   SetValueIsValid(false);
126ac7ddfbfSEd Maste   m_error.Clear();
127ac7ddfbfSEd Maste 
128ac7ddfbfSEd Maste   Variable *variable = m_variable_sp.get();
129ac7ddfbfSEd Maste   DWARFExpression &expr = variable->LocationExpression();
130ac7ddfbfSEd Maste 
131435933ddSDimitry Andric   if (variable->GetLocationIsConstantValueData()) {
132ac7ddfbfSEd Maste     // expr doesn't contain DWARF bytes, it contains the constant variable
133ac7ddfbfSEd Maste     // value bytes themselves...
134ac7ddfbfSEd Maste     if (expr.GetExpressionData(m_data))
135ac7ddfbfSEd Maste       m_value.SetContext(Value::eContextTypeVariable, variable);
136ac7ddfbfSEd Maste     else
137ac7ddfbfSEd Maste       m_error.SetErrorString("empty constant data");
138ac7ddfbfSEd Maste     // constant bytes can't be edited - sorry
139ac7ddfbfSEd Maste     m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
140435933ddSDimitry Andric   } else {
141ac7ddfbfSEd Maste     lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
142ac7ddfbfSEd Maste     ExecutionContext exe_ctx(GetExecutionContextRef());
143ac7ddfbfSEd Maste 
144ac7ddfbfSEd Maste     Target *target = exe_ctx.GetTargetPtr();
145435933ddSDimitry Andric     if (target) {
146ac7ddfbfSEd Maste       m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
147ac7ddfbfSEd Maste       m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
148ac7ddfbfSEd Maste     }
149ac7ddfbfSEd Maste 
150435933ddSDimitry Andric     if (expr.IsLocationList()) {
151ac7ddfbfSEd Maste       SymbolContext sc;
152ac7ddfbfSEd Maste       variable->CalculateSymbolContext(&sc);
153ac7ddfbfSEd Maste       if (sc.function)
154435933ddSDimitry Andric         loclist_base_load_addr =
155435933ddSDimitry Andric             sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
156435933ddSDimitry Andric                 target);
157ac7ddfbfSEd Maste     }
158ac7ddfbfSEd Maste     Value old_value(m_value);
159acac075bSDimitry Andric     if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
160acac075bSDimitry Andric                       nullptr, m_value, &m_error)) {
161ac7ddfbfSEd Maste       m_resolved_value = m_value;
162ac7ddfbfSEd Maste       m_value.SetContext(Value::eContextTypeVariable, variable);
16335617911SEd Maste 
1649f2f44ceSEd Maste       CompilerType compiler_type = GetCompilerType();
1659f2f44ceSEd Maste       if (compiler_type.IsValid())
1669f2f44ceSEd Maste         m_value.SetCompilerType(compiler_type);
167ac7ddfbfSEd Maste 
168ac7ddfbfSEd Maste       Value::ValueType value_type = m_value.GetValueType();
169ac7ddfbfSEd Maste 
1707aa51b79SEd Maste       Process *process = exe_ctx.GetProcessPtr();
1717aa51b79SEd Maste       const bool process_is_alive = process && process->IsAlive();
1729f2f44ceSEd Maste       const uint32_t type_info = compiler_type.GetTypeInfo();
173435933ddSDimitry Andric       const bool is_pointer_or_ref =
174435933ddSDimitry Andric           (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
1757aa51b79SEd Maste 
176435933ddSDimitry Andric       switch (value_type) {
177ac7ddfbfSEd Maste       case Value::eValueTypeFileAddress:
178435933ddSDimitry Andric         // If this type is a pointer, then its children will be considered load
1794ba319b5SDimitry Andric         // addresses if the pointer or reference is dereferenced, but only if
1804ba319b5SDimitry Andric         // the process is alive.
1817aa51b79SEd Maste         //
1827aa51b79SEd Maste         // There could be global variables like in the following code:
1837aa51b79SEd Maste         // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
1847aa51b79SEd Maste         // Foo g_foo1;
1857aa51b79SEd Maste         // Foo g_foo2;
1867aa51b79SEd Maste         // LinkedListNode g_second_node = { &g_foo2, NULL };
1877aa51b79SEd Maste         // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
1887aa51b79SEd Maste         //
189435933ddSDimitry Andric         // When we aren't running, we should be able to look at these variables
1904ba319b5SDimitry Andric         // using the "target variable" command. Children of the "g_first_node"
1914ba319b5SDimitry Andric         // always will be of the same address type as the parent. But children
1924ba319b5SDimitry Andric         // of the "next" member of LinkedListNode will become load addresses if
1934ba319b5SDimitry Andric         // we have a live process, or remain what a file address if it what a
1944ba319b5SDimitry Andric         // file address.
1957aa51b79SEd Maste         if (process_is_alive && is_pointer_or_ref)
1967aa51b79SEd Maste           SetAddressTypeOfChildren(eAddressTypeLoad);
1977aa51b79SEd Maste         else
198ac7ddfbfSEd Maste           SetAddressTypeOfChildren(eAddressTypeFile);
199ac7ddfbfSEd Maste         break;
200ac7ddfbfSEd Maste       case Value::eValueTypeHostAddress:
201435933ddSDimitry Andric         // Same as above for load addresses, except children of pointer or refs
2024ba319b5SDimitry Andric         // are always load addresses. Host addresses are used to store freeze
2034ba319b5SDimitry Andric         // dried variables. If this type is a struct, the entire struct
2044ba319b5SDimitry Andric         // contents will be copied into the heap of the
2054ba319b5SDimitry Andric         // LLDB process, but we do not currently follow any pointers.
2067aa51b79SEd Maste         if (is_pointer_or_ref)
2077aa51b79SEd Maste           SetAddressTypeOfChildren(eAddressTypeLoad);
2087aa51b79SEd Maste         else
209ac7ddfbfSEd Maste           SetAddressTypeOfChildren(eAddressTypeHost);
210ac7ddfbfSEd Maste         break;
211ac7ddfbfSEd Maste       case Value::eValueTypeLoadAddress:
212ac7ddfbfSEd Maste       case Value::eValueTypeScalar:
213ac7ddfbfSEd Maste       case Value::eValueTypeVector:
214ac7ddfbfSEd Maste         SetAddressTypeOfChildren(eAddressTypeLoad);
215ac7ddfbfSEd Maste         break;
216ac7ddfbfSEd Maste       }
217ac7ddfbfSEd Maste 
218435933ddSDimitry Andric       switch (value_type) {
219ac7ddfbfSEd Maste       case Value::eValueTypeVector:
220ac7ddfbfSEd Maste       // fall through
221ac7ddfbfSEd Maste       case Value::eValueTypeScalar:
2224ba319b5SDimitry Andric         // The variable value is in the Scalar value inside the m_value. We can
2234ba319b5SDimitry Andric         // point our m_data right to it.
224435933ddSDimitry Andric         m_error =
225435933ddSDimitry Andric             m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
226ac7ddfbfSEd Maste         break;
227ac7ddfbfSEd Maste 
228ac7ddfbfSEd Maste       case Value::eValueTypeFileAddress:
229ac7ddfbfSEd Maste       case Value::eValueTypeLoadAddress:
230ac7ddfbfSEd Maste       case Value::eValueTypeHostAddress:
2314ba319b5SDimitry Andric         // The DWARF expression result was an address in the inferior process.
2324ba319b5SDimitry Andric         // If this variable is an aggregate type, we just need the address as
2334ba319b5SDimitry Andric         // the main value as all child variable objects will rely upon this
2344ba319b5SDimitry Andric         // location and add an offset and then read their own values as needed.
2354ba319b5SDimitry Andric         // If this variable is a simple type, we read all data for it into
2364ba319b5SDimitry Andric         // m_data. Make sure this type has a value before we try and read it
237ac7ddfbfSEd Maste 
238ac7ddfbfSEd Maste         // If we have a file address, convert it to a load address if we can.
2394ba319b5SDimitry Andric         if (value_type == Value::eValueTypeFileAddress && process_is_alive)
2404ba319b5SDimitry Andric           m_value.ConvertToLoadAddress(GetModule().get(), target);
241ac7ddfbfSEd Maste 
242435933ddSDimitry Andric         if (!CanProvideValue()) {
2434ba319b5SDimitry Andric           // this value object represents an aggregate type whose children have
2444ba319b5SDimitry Andric           // values, but this object does not. So we say we are changed if our
2454ba319b5SDimitry Andric           // location has changed.
246435933ddSDimitry Andric           SetValueDidChange(value_type != old_value.GetValueType() ||
247435933ddSDimitry Andric                             m_value.GetScalar() != old_value.GetScalar());
248435933ddSDimitry Andric         } else {
2494ba319b5SDimitry Andric           // Copy the Value and set the context to use our Variable so it can
2504ba319b5SDimitry Andric           // extract read its value into m_data appropriately
251ac7ddfbfSEd Maste           Value value(m_value);
252ac7ddfbfSEd Maste           value.SetContext(Value::eContextTypeVariable, variable);
253435933ddSDimitry Andric           m_error =
254435933ddSDimitry Andric               value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
2557aa51b79SEd Maste 
256435933ddSDimitry Andric           SetValueDidChange(value_type != old_value.GetValueType() ||
257435933ddSDimitry Andric                             m_value.GetScalar() != old_value.GetScalar());
258ac7ddfbfSEd Maste         }
259ac7ddfbfSEd Maste         break;
260ac7ddfbfSEd Maste       }
261ac7ddfbfSEd Maste 
262ac7ddfbfSEd Maste       SetValueIsValid(m_error.Success());
263435933ddSDimitry Andric     } else {
264ac7ddfbfSEd Maste       // could not find location, won't allow editing
265ac7ddfbfSEd Maste       m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
266ac7ddfbfSEd Maste     }
267ac7ddfbfSEd Maste   }
268ac7ddfbfSEd Maste   return m_error.Success();
269ac7ddfbfSEd Maste }
270ac7ddfbfSEd Maste 
IsInScope()271435933ddSDimitry Andric bool ValueObjectVariable::IsInScope() {
272ac7ddfbfSEd Maste   const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
273435933ddSDimitry Andric   if (exe_ctx_ref.HasFrameRef()) {
274ac7ddfbfSEd Maste     ExecutionContext exe_ctx(exe_ctx_ref);
275ac7ddfbfSEd Maste     StackFrame *frame = exe_ctx.GetFramePtr();
276435933ddSDimitry Andric     if (frame) {
277ac7ddfbfSEd Maste       return m_variable_sp->IsInScope(frame);
278435933ddSDimitry Andric     } else {
2794ba319b5SDimitry Andric       // This ValueObject had a frame at one time, but now we can't locate it,
2804ba319b5SDimitry Andric       // so return false since we probably aren't in scope.
281ac7ddfbfSEd Maste       return false;
282ac7ddfbfSEd Maste     }
283ac7ddfbfSEd Maste   }
2844ba319b5SDimitry Andric   // We have a variable that wasn't tied to a frame, which means it is a global
2854ba319b5SDimitry Andric   // and is always in scope.
286ac7ddfbfSEd Maste   return true;
287ac7ddfbfSEd Maste }
288ac7ddfbfSEd Maste 
GetModule()289435933ddSDimitry Andric lldb::ModuleSP ValueObjectVariable::GetModule() {
290435933ddSDimitry Andric   if (m_variable_sp) {
291ac7ddfbfSEd Maste     SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
292435933ddSDimitry Andric     if (sc_scope) {
293ac7ddfbfSEd Maste       return sc_scope->CalculateSymbolContextModule();
294ac7ddfbfSEd Maste     }
295ac7ddfbfSEd Maste   }
296ac7ddfbfSEd Maste   return lldb::ModuleSP();
297ac7ddfbfSEd Maste }
298ac7ddfbfSEd Maste 
GetSymbolContextScope()299435933ddSDimitry Andric SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
300ac7ddfbfSEd Maste   if (m_variable_sp)
301ac7ddfbfSEd Maste     return m_variable_sp->GetSymbolContextScope();
302ac7ddfbfSEd Maste   return NULL;
303ac7ddfbfSEd Maste }
304ac7ddfbfSEd Maste 
GetDeclaration(Declaration & decl)305435933ddSDimitry Andric bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
306435933ddSDimitry Andric   if (m_variable_sp) {
307ac7ddfbfSEd Maste     decl = m_variable_sp->GetDeclaration();
308ac7ddfbfSEd Maste     return true;
309ac7ddfbfSEd Maste   }
310ac7ddfbfSEd Maste   return false;
311ac7ddfbfSEd Maste }
312ac7ddfbfSEd Maste 
GetLocationAsCString()313435933ddSDimitry Andric const char *ValueObjectVariable::GetLocationAsCString() {
314ac7ddfbfSEd Maste   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
315435933ddSDimitry Andric     return GetLocationAsCStringImpl(m_resolved_value, m_data);
316ac7ddfbfSEd Maste   else
317ac7ddfbfSEd Maste     return ValueObject::GetLocationAsCString();
318ac7ddfbfSEd Maste }
319ac7ddfbfSEd Maste 
SetValueFromCString(const char * value_str,Status & error)320435933ddSDimitry Andric bool ValueObjectVariable::SetValueFromCString(const char *value_str,
3215517e702SDimitry Andric                                               Status &error) {
322435933ddSDimitry Andric   if (!UpdateValueIfNeeded()) {
32312b93ac6SEd Maste     error.SetErrorString("unable to update value before writing");
32412b93ac6SEd Maste     return false;
32512b93ac6SEd Maste   }
32612b93ac6SEd Maste 
327435933ddSDimitry Andric   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
328ac7ddfbfSEd Maste     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
329ac7ddfbfSEd Maste     ExecutionContext exe_ctx(GetExecutionContextRef());
330ac7ddfbfSEd Maste     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
331ac7ddfbfSEd Maste     RegisterValue reg_value;
332435933ddSDimitry Andric     if (!reg_info || !reg_ctx) {
333ac7ddfbfSEd Maste       error.SetErrorString("unable to retrieve register info");
334ac7ddfbfSEd Maste       return false;
335ac7ddfbfSEd Maste     }
336435933ddSDimitry Andric     error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
337ac7ddfbfSEd Maste     if (error.Fail())
338ac7ddfbfSEd Maste       return false;
339435933ddSDimitry Andric     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
340ac7ddfbfSEd Maste       SetNeedsUpdate();
341ac7ddfbfSEd Maste       return true;
342435933ddSDimitry Andric     } else {
343ac7ddfbfSEd Maste       error.SetErrorString("unable to write back to register");
344ac7ddfbfSEd Maste       return false;
345ac7ddfbfSEd Maste     }
346435933ddSDimitry Andric   } else
347ac7ddfbfSEd Maste     return ValueObject::SetValueFromCString(value_str, error);
348ac7ddfbfSEd Maste }
349ac7ddfbfSEd Maste 
SetData(DataExtractor & data,Status & error)3505517e702SDimitry Andric bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
351435933ddSDimitry Andric   if (!UpdateValueIfNeeded()) {
35212b93ac6SEd Maste     error.SetErrorString("unable to update value before writing");
35312b93ac6SEd Maste     return false;
35412b93ac6SEd Maste   }
35512b93ac6SEd Maste 
356435933ddSDimitry Andric   if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
357ac7ddfbfSEd Maste     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
358ac7ddfbfSEd Maste     ExecutionContext exe_ctx(GetExecutionContextRef());
359ac7ddfbfSEd Maste     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
360ac7ddfbfSEd Maste     RegisterValue reg_value;
361435933ddSDimitry Andric     if (!reg_info || !reg_ctx) {
362ac7ddfbfSEd Maste       error.SetErrorString("unable to retrieve register info");
363ac7ddfbfSEd Maste       return false;
364ac7ddfbfSEd Maste     }
36512b93ac6SEd Maste     error = reg_value.SetValueFromData(reg_info, data, 0, true);
366ac7ddfbfSEd Maste     if (error.Fail())
367ac7ddfbfSEd Maste       return false;
368435933ddSDimitry Andric     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
369ac7ddfbfSEd Maste       SetNeedsUpdate();
370ac7ddfbfSEd Maste       return true;
371435933ddSDimitry Andric     } else {
372ac7ddfbfSEd Maste       error.SetErrorString("unable to write back to register");
373ac7ddfbfSEd Maste       return false;
374ac7ddfbfSEd Maste     }
375435933ddSDimitry Andric   } else
376ac7ddfbfSEd Maste     return ValueObject::SetData(data, error);
377ac7ddfbfSEd Maste }
378