15ffd83dbSDimitry Andric //===-- ValueObjectVariable.cpp -------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Core/ValueObjectVariable.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Core/Address.h"
120b57cec5SDimitry Andric #include "lldb/Core/AddressRange.h"
13*5f7ddb14SDimitry Andric #include "lldb/Core/Declaration.h"
140b57cec5SDimitry Andric #include "lldb/Core/Module.h"
150b57cec5SDimitry Andric #include "lldb/Core/Value.h"
160b57cec5SDimitry Andric #include "lldb/Expression/DWARFExpression.h"
170b57cec5SDimitry Andric #include "lldb/Symbol/Function.h"
180b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
190b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContext.h"
200b57cec5SDimitry Andric #include "lldb/Symbol/SymbolContextScope.h"
210b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
220b57cec5SDimitry Andric #include "lldb/Symbol/Variable.h"
230b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
240b57cec5SDimitry Andric #include "lldb/Target/Process.h"
250b57cec5SDimitry Andric #include "lldb/Target/RegisterContext.h"
260b57cec5SDimitry Andric #include "lldb/Target/Target.h"
270b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
280b57cec5SDimitry Andric #include "lldb/Utility/RegisterValue.h"
290b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
300b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
310b57cec5SDimitry Andric #include "lldb/lldb-private-enumerations.h"
320b57cec5SDimitry Andric #include "lldb/lldb-types.h"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
350b57cec5SDimitry Andric 
36*5f7ddb14SDimitry Andric #include <cassert>
370b57cec5SDimitry Andric #include <memory>
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace lldb_private {
400b57cec5SDimitry Andric class ExecutionContextScope;
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric namespace lldb_private {
430b57cec5SDimitry Andric class StackFrame;
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric namespace lldb_private {
460b57cec5SDimitry Andric struct RegisterInfo;
470b57cec5SDimitry Andric }
480b57cec5SDimitry Andric using namespace lldb_private;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric lldb::ValueObjectSP
Create(ExecutionContextScope * exe_scope,const lldb::VariableSP & var_sp)510b57cec5SDimitry Andric ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
520b57cec5SDimitry Andric                             const lldb::VariableSP &var_sp) {
535ffd83dbSDimitry Andric   auto manager_sp = ValueObjectManager::Create();
545ffd83dbSDimitry Andric   return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP();
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
ValueObjectVariable(ExecutionContextScope * exe_scope,ValueObjectManager & manager,const lldb::VariableSP & var_sp)570b57cec5SDimitry Andric ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
585ffd83dbSDimitry Andric                                          ValueObjectManager &manager,
590b57cec5SDimitry Andric                                          const lldb::VariableSP &var_sp)
605ffd83dbSDimitry Andric     : ValueObject(exe_scope, manager), m_variable_sp(var_sp) {
610b57cec5SDimitry Andric   // Do not attempt to construct one of these objects with no variable!
620b57cec5SDimitry Andric   assert(m_variable_sp.get() != nullptr);
630b57cec5SDimitry Andric   m_name = var_sp->GetName();
640b57cec5SDimitry Andric }
650b57cec5SDimitry Andric 
66*5f7ddb14SDimitry Andric ValueObjectVariable::~ValueObjectVariable() = default;
670b57cec5SDimitry Andric 
GetCompilerTypeImpl()680b57cec5SDimitry Andric CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
690b57cec5SDimitry Andric   Type *var_type = m_variable_sp->GetType();
700b57cec5SDimitry Andric   if (var_type)
710b57cec5SDimitry Andric     return var_type->GetForwardCompilerType();
720b57cec5SDimitry Andric   return CompilerType();
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
GetTypeName()750b57cec5SDimitry Andric ConstString ValueObjectVariable::GetTypeName() {
760b57cec5SDimitry Andric   Type *var_type = m_variable_sp->GetType();
770b57cec5SDimitry Andric   if (var_type)
780b57cec5SDimitry Andric     return var_type->GetName();
790b57cec5SDimitry Andric   return ConstString();
800b57cec5SDimitry Andric }
810b57cec5SDimitry Andric 
GetDisplayTypeName()820b57cec5SDimitry Andric ConstString ValueObjectVariable::GetDisplayTypeName() {
830b57cec5SDimitry Andric   Type *var_type = m_variable_sp->GetType();
840b57cec5SDimitry Andric   if (var_type)
850b57cec5SDimitry Andric     return var_type->GetForwardCompilerType().GetDisplayTypeName();
860b57cec5SDimitry Andric   return ConstString();
870b57cec5SDimitry Andric }
880b57cec5SDimitry Andric 
GetQualifiedTypeName()890b57cec5SDimitry Andric ConstString ValueObjectVariable::GetQualifiedTypeName() {
900b57cec5SDimitry Andric   Type *var_type = m_variable_sp->GetType();
910b57cec5SDimitry Andric   if (var_type)
920b57cec5SDimitry Andric     return var_type->GetQualifiedName();
930b57cec5SDimitry Andric   return ConstString();
940b57cec5SDimitry Andric }
950b57cec5SDimitry Andric 
CalculateNumChildren(uint32_t max)960b57cec5SDimitry Andric size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
970b57cec5SDimitry Andric   CompilerType type(GetCompilerType());
980b57cec5SDimitry Andric 
990b57cec5SDimitry Andric   if (!type.IsValid())
1000b57cec5SDimitry Andric     return 0;
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1030b57cec5SDimitry Andric   const bool omit_empty_base_classes = true;
1040b57cec5SDimitry Andric   auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
1050b57cec5SDimitry Andric   return child_count <= max ? child_count : max;
1060b57cec5SDimitry Andric }
1070b57cec5SDimitry Andric 
GetByteSize()108af732203SDimitry Andric llvm::Optional<uint64_t> ValueObjectVariable::GetByteSize() {
1090b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   CompilerType type(GetCompilerType());
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric   if (!type.IsValid())
114af732203SDimitry Andric     return {};
1150b57cec5SDimitry Andric 
116af732203SDimitry Andric   return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
GetValueType() const1190b57cec5SDimitry Andric lldb::ValueType ValueObjectVariable::GetValueType() const {
1200b57cec5SDimitry Andric   if (m_variable_sp)
1210b57cec5SDimitry Andric     return m_variable_sp->GetScope();
1220b57cec5SDimitry Andric   return lldb::eValueTypeInvalid;
1230b57cec5SDimitry Andric }
1240b57cec5SDimitry Andric 
UpdateValue()1250b57cec5SDimitry Andric bool ValueObjectVariable::UpdateValue() {
1260b57cec5SDimitry Andric   SetValueIsValid(false);
1270b57cec5SDimitry Andric   m_error.Clear();
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   Variable *variable = m_variable_sp.get();
1300b57cec5SDimitry Andric   DWARFExpression &expr = variable->LocationExpression();
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   if (variable->GetLocationIsConstantValueData()) {
1330b57cec5SDimitry Andric     // expr doesn't contain DWARF bytes, it contains the constant variable
1340b57cec5SDimitry Andric     // value bytes themselves...
135af732203SDimitry Andric     if (expr.GetExpressionData(m_data)) {
136af732203SDimitry Andric        if (m_data.GetDataStart() && m_data.GetByteSize())
137af732203SDimitry Andric         m_value.SetBytes(m_data.GetDataStart(), m_data.GetByteSize());
138*5f7ddb14SDimitry Andric       m_value.SetContext(Value::ContextType::Variable, variable);
139af732203SDimitry Andric     }
1400b57cec5SDimitry Andric     else
1410b57cec5SDimitry Andric       m_error.SetErrorString("empty constant data");
1420b57cec5SDimitry Andric     // constant bytes can't be edited - sorry
143*5f7ddb14SDimitry Andric     m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
1440b57cec5SDimitry Andric   } else {
1450b57cec5SDimitry Andric     lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
1460b57cec5SDimitry Andric     ExecutionContext exe_ctx(GetExecutionContextRef());
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric     Target *target = exe_ctx.GetTargetPtr();
1490b57cec5SDimitry Andric     if (target) {
1500b57cec5SDimitry Andric       m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
1510b57cec5SDimitry Andric       m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
1520b57cec5SDimitry Andric     }
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     if (expr.IsLocationList()) {
1550b57cec5SDimitry Andric       SymbolContext sc;
1560b57cec5SDimitry Andric       variable->CalculateSymbolContext(&sc);
1570b57cec5SDimitry Andric       if (sc.function)
1580b57cec5SDimitry Andric         loclist_base_load_addr =
1590b57cec5SDimitry Andric             sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
1600b57cec5SDimitry Andric                 target);
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric     Value old_value(m_value);
1630b57cec5SDimitry Andric     if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
1640b57cec5SDimitry Andric                       nullptr, m_value, &m_error)) {
1650b57cec5SDimitry Andric       m_resolved_value = m_value;
166*5f7ddb14SDimitry Andric       m_value.SetContext(Value::ContextType::Variable, variable);
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric       CompilerType compiler_type = GetCompilerType();
1690b57cec5SDimitry Andric       if (compiler_type.IsValid())
1700b57cec5SDimitry Andric         m_value.SetCompilerType(compiler_type);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric       Value::ValueType value_type = m_value.GetValueType();
1730b57cec5SDimitry Andric 
1745ffd83dbSDimitry Andric       // The size of the buffer within m_value can be less than the size
1755ffd83dbSDimitry Andric       // prescribed by its type. E.g. this can happen when an expression only
1765ffd83dbSDimitry Andric       // partially describes an object (say, because it contains DW_OP_piece).
1775ffd83dbSDimitry Andric       //
1785ffd83dbSDimitry Andric       // In this case, grow m_value to the expected size. An alternative way to
1795ffd83dbSDimitry Andric       // handle this is to teach Value::GetValueAsData() and ValueObjectChild
1805ffd83dbSDimitry Andric       // not to read past the end of a host buffer, but this gets impractically
1815ffd83dbSDimitry Andric       // complicated as a Value's host buffer may be shared with a distant
1825ffd83dbSDimitry Andric       // ancestor or sibling in the ValueObject hierarchy.
1835ffd83dbSDimitry Andric       //
1845ffd83dbSDimitry Andric       // FIXME: When we grow m_value, we should represent the added bits as
1855ffd83dbSDimitry Andric       // undefined somehow instead of as 0's.
186*5f7ddb14SDimitry Andric       if (value_type == Value::ValueType::HostAddress &&
1875ffd83dbSDimitry Andric           compiler_type.IsValid()) {
1885ffd83dbSDimitry Andric         if (size_t value_buf_size = m_value.GetBuffer().GetByteSize()) {
1895ffd83dbSDimitry Andric           size_t value_size = m_value.GetValueByteSize(&m_error, &exe_ctx);
1905ffd83dbSDimitry Andric           if (m_error.Success() && value_buf_size < value_size)
1915ffd83dbSDimitry Andric             m_value.ResizeData(value_size);
1925ffd83dbSDimitry Andric         }
1935ffd83dbSDimitry Andric       }
1945ffd83dbSDimitry Andric 
1950b57cec5SDimitry Andric       Process *process = exe_ctx.GetProcessPtr();
1960b57cec5SDimitry Andric       const bool process_is_alive = process && process->IsAlive();
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric       switch (value_type) {
199*5f7ddb14SDimitry Andric       case Value::ValueType::Invalid:
200*5f7ddb14SDimitry Andric         m_error.SetErrorString("invalid value");
201*5f7ddb14SDimitry Andric         break;
202*5f7ddb14SDimitry Andric       case Value::ValueType::Scalar:
2030b57cec5SDimitry Andric         // The variable value is in the Scalar value inside the m_value. We can
2040b57cec5SDimitry Andric         // point our m_data right to it.
2050b57cec5SDimitry Andric         m_error =
2069dba64beSDimitry Andric             m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
2070b57cec5SDimitry Andric         break;
2080b57cec5SDimitry Andric 
209*5f7ddb14SDimitry Andric       case Value::ValueType::FileAddress:
210*5f7ddb14SDimitry Andric       case Value::ValueType::LoadAddress:
211*5f7ddb14SDimitry Andric       case Value::ValueType::HostAddress:
2120b57cec5SDimitry Andric         // The DWARF expression result was an address in the inferior process.
2130b57cec5SDimitry Andric         // If this variable is an aggregate type, we just need the address as
2140b57cec5SDimitry Andric         // the main value as all child variable objects will rely upon this
2150b57cec5SDimitry Andric         // location and add an offset and then read their own values as needed.
2160b57cec5SDimitry Andric         // If this variable is a simple type, we read all data for it into
2170b57cec5SDimitry Andric         // m_data. Make sure this type has a value before we try and read it
2180b57cec5SDimitry Andric 
2190b57cec5SDimitry Andric         // If we have a file address, convert it to a load address if we can.
220*5f7ddb14SDimitry Andric         if (value_type == Value::ValueType::FileAddress && process_is_alive)
2210b57cec5SDimitry Andric           m_value.ConvertToLoadAddress(GetModule().get(), target);
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric         if (!CanProvideValue()) {
2240b57cec5SDimitry Andric           // this value object represents an aggregate type whose children have
2250b57cec5SDimitry Andric           // values, but this object does not. So we say we are changed if our
2260b57cec5SDimitry Andric           // location has changed.
2270b57cec5SDimitry Andric           SetValueDidChange(value_type != old_value.GetValueType() ||
2280b57cec5SDimitry Andric                             m_value.GetScalar() != old_value.GetScalar());
2290b57cec5SDimitry Andric         } else {
2300b57cec5SDimitry Andric           // Copy the Value and set the context to use our Variable so it can
2310b57cec5SDimitry Andric           // extract read its value into m_data appropriately
2320b57cec5SDimitry Andric           Value value(m_value);
233*5f7ddb14SDimitry Andric           value.SetContext(Value::ContextType::Variable, variable);
2340b57cec5SDimitry Andric           m_error =
2359dba64beSDimitry Andric               value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric           SetValueDidChange(value_type != old_value.GetValueType() ||
2380b57cec5SDimitry Andric                             m_value.GetScalar() != old_value.GetScalar());
2390b57cec5SDimitry Andric         }
2400b57cec5SDimitry Andric         break;
2410b57cec5SDimitry Andric       }
2420b57cec5SDimitry Andric 
2430b57cec5SDimitry Andric       SetValueIsValid(m_error.Success());
2440b57cec5SDimitry Andric     } else {
2450b57cec5SDimitry Andric       // could not find location, won't allow editing
246*5f7ddb14SDimitry Andric       m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
2470b57cec5SDimitry Andric     }
2480b57cec5SDimitry Andric   }
2495ffd83dbSDimitry Andric 
2500b57cec5SDimitry Andric   return m_error.Success();
2510b57cec5SDimitry Andric }
2520b57cec5SDimitry Andric 
DoUpdateChildrenAddressType(ValueObject & valobj)2535ffd83dbSDimitry Andric void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject &valobj) {
2545ffd83dbSDimitry Andric   Value::ValueType value_type = valobj.GetValue().GetValueType();
2555ffd83dbSDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
2565ffd83dbSDimitry Andric   Process *process = exe_ctx.GetProcessPtr();
2575ffd83dbSDimitry Andric   const bool process_is_alive = process && process->IsAlive();
2585ffd83dbSDimitry Andric   const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo();
2595ffd83dbSDimitry Andric   const bool is_pointer_or_ref =
2605ffd83dbSDimitry Andric       (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
2615ffd83dbSDimitry Andric 
2625ffd83dbSDimitry Andric   switch (value_type) {
263*5f7ddb14SDimitry Andric   case Value::ValueType::Invalid:
264*5f7ddb14SDimitry Andric     break;
265*5f7ddb14SDimitry Andric   case Value::ValueType::FileAddress:
2665ffd83dbSDimitry Andric     // If this type is a pointer, then its children will be considered load
2675ffd83dbSDimitry Andric     // addresses if the pointer or reference is dereferenced, but only if
2685ffd83dbSDimitry Andric     // the process is alive.
2695ffd83dbSDimitry Andric     //
2705ffd83dbSDimitry Andric     // There could be global variables like in the following code:
2715ffd83dbSDimitry Andric     // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
2725ffd83dbSDimitry Andric     // Foo g_foo1;
2735ffd83dbSDimitry Andric     // Foo g_foo2;
2745ffd83dbSDimitry Andric     // LinkedListNode g_second_node = { &g_foo2, NULL };
2755ffd83dbSDimitry Andric     // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
2765ffd83dbSDimitry Andric     //
2775ffd83dbSDimitry Andric     // When we aren't running, we should be able to look at these variables
2785ffd83dbSDimitry Andric     // using the "target variable" command. Children of the "g_first_node"
2795ffd83dbSDimitry Andric     // always will be of the same address type as the parent. But children
2805ffd83dbSDimitry Andric     // of the "next" member of LinkedListNode will become load addresses if
2815ffd83dbSDimitry Andric     // we have a live process, or remain a file address if it was a file
2825ffd83dbSDimitry Andric     // address.
2835ffd83dbSDimitry Andric     if (process_is_alive && is_pointer_or_ref)
2845ffd83dbSDimitry Andric       valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
2855ffd83dbSDimitry Andric     else
2865ffd83dbSDimitry Andric       valobj.SetAddressTypeOfChildren(eAddressTypeFile);
2875ffd83dbSDimitry Andric     break;
288*5f7ddb14SDimitry Andric   case Value::ValueType::HostAddress:
2895ffd83dbSDimitry Andric     // Same as above for load addresses, except children of pointer or refs
2905ffd83dbSDimitry Andric     // are always load addresses. Host addresses are used to store freeze
2915ffd83dbSDimitry Andric     // dried variables. If this type is a struct, the entire struct
2925ffd83dbSDimitry Andric     // contents will be copied into the heap of the
2935ffd83dbSDimitry Andric     // LLDB process, but we do not currently follow any pointers.
2945ffd83dbSDimitry Andric     if (is_pointer_or_ref)
2955ffd83dbSDimitry Andric       valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
2965ffd83dbSDimitry Andric     else
2975ffd83dbSDimitry Andric       valobj.SetAddressTypeOfChildren(eAddressTypeHost);
2985ffd83dbSDimitry Andric     break;
299*5f7ddb14SDimitry Andric   case Value::ValueType::LoadAddress:
300*5f7ddb14SDimitry Andric   case Value::ValueType::Scalar:
3015ffd83dbSDimitry Andric     valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
3025ffd83dbSDimitry Andric     break;
3035ffd83dbSDimitry Andric   }
3045ffd83dbSDimitry Andric }
3055ffd83dbSDimitry Andric 
3065ffd83dbSDimitry Andric 
3075ffd83dbSDimitry Andric 
IsInScope()3080b57cec5SDimitry Andric bool ValueObjectVariable::IsInScope() {
3090b57cec5SDimitry Andric   const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
3100b57cec5SDimitry Andric   if (exe_ctx_ref.HasFrameRef()) {
3110b57cec5SDimitry Andric     ExecutionContext exe_ctx(exe_ctx_ref);
3120b57cec5SDimitry Andric     StackFrame *frame = exe_ctx.GetFramePtr();
3130b57cec5SDimitry Andric     if (frame) {
3140b57cec5SDimitry Andric       return m_variable_sp->IsInScope(frame);
3150b57cec5SDimitry Andric     } else {
3160b57cec5SDimitry Andric       // This ValueObject had a frame at one time, but now we can't locate it,
3170b57cec5SDimitry Andric       // so return false since we probably aren't in scope.
3180b57cec5SDimitry Andric       return false;
3190b57cec5SDimitry Andric     }
3200b57cec5SDimitry Andric   }
3210b57cec5SDimitry Andric   // We have a variable that wasn't tied to a frame, which means it is a global
3220b57cec5SDimitry Andric   // and is always in scope.
3230b57cec5SDimitry Andric   return true;
3240b57cec5SDimitry Andric }
3250b57cec5SDimitry Andric 
GetModule()3260b57cec5SDimitry Andric lldb::ModuleSP ValueObjectVariable::GetModule() {
3270b57cec5SDimitry Andric   if (m_variable_sp) {
3280b57cec5SDimitry Andric     SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
3290b57cec5SDimitry Andric     if (sc_scope) {
3300b57cec5SDimitry Andric       return sc_scope->CalculateSymbolContextModule();
3310b57cec5SDimitry Andric     }
3320b57cec5SDimitry Andric   }
3330b57cec5SDimitry Andric   return lldb::ModuleSP();
3340b57cec5SDimitry Andric }
3350b57cec5SDimitry Andric 
GetSymbolContextScope()3360b57cec5SDimitry Andric SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
3370b57cec5SDimitry Andric   if (m_variable_sp)
3380b57cec5SDimitry Andric     return m_variable_sp->GetSymbolContextScope();
3390b57cec5SDimitry Andric   return nullptr;
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
GetDeclaration(Declaration & decl)3420b57cec5SDimitry Andric bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
3430b57cec5SDimitry Andric   if (m_variable_sp) {
3440b57cec5SDimitry Andric     decl = m_variable_sp->GetDeclaration();
3450b57cec5SDimitry Andric     return true;
3460b57cec5SDimitry Andric   }
3470b57cec5SDimitry Andric   return false;
3480b57cec5SDimitry Andric }
3490b57cec5SDimitry Andric 
GetLocationAsCString()3500b57cec5SDimitry Andric const char *ValueObjectVariable::GetLocationAsCString() {
351*5f7ddb14SDimitry Andric   if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo)
3520b57cec5SDimitry Andric     return GetLocationAsCStringImpl(m_resolved_value, m_data);
3530b57cec5SDimitry Andric   else
3540b57cec5SDimitry Andric     return ValueObject::GetLocationAsCString();
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
SetValueFromCString(const char * value_str,Status & error)3570b57cec5SDimitry Andric bool ValueObjectVariable::SetValueFromCString(const char *value_str,
3580b57cec5SDimitry Andric                                               Status &error) {
3590b57cec5SDimitry Andric   if (!UpdateValueIfNeeded()) {
3600b57cec5SDimitry Andric     error.SetErrorString("unable to update value before writing");
3610b57cec5SDimitry Andric     return false;
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric 
364*5f7ddb14SDimitry Andric   if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
3650b57cec5SDimitry Andric     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
3660b57cec5SDimitry Andric     ExecutionContext exe_ctx(GetExecutionContextRef());
3670b57cec5SDimitry Andric     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
3680b57cec5SDimitry Andric     RegisterValue reg_value;
3690b57cec5SDimitry Andric     if (!reg_info || !reg_ctx) {
3700b57cec5SDimitry Andric       error.SetErrorString("unable to retrieve register info");
3710b57cec5SDimitry Andric       return false;
3720b57cec5SDimitry Andric     }
3730b57cec5SDimitry Andric     error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
3740b57cec5SDimitry Andric     if (error.Fail())
3750b57cec5SDimitry Andric       return false;
3760b57cec5SDimitry Andric     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
3770b57cec5SDimitry Andric       SetNeedsUpdate();
3780b57cec5SDimitry Andric       return true;
3790b57cec5SDimitry Andric     } else {
3800b57cec5SDimitry Andric       error.SetErrorString("unable to write back to register");
3810b57cec5SDimitry Andric       return false;
3820b57cec5SDimitry Andric     }
3830b57cec5SDimitry Andric   } else
3840b57cec5SDimitry Andric     return ValueObject::SetValueFromCString(value_str, error);
3850b57cec5SDimitry Andric }
3860b57cec5SDimitry Andric 
SetData(DataExtractor & data,Status & error)3870b57cec5SDimitry Andric bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
3880b57cec5SDimitry Andric   if (!UpdateValueIfNeeded()) {
3890b57cec5SDimitry Andric     error.SetErrorString("unable to update value before writing");
3900b57cec5SDimitry Andric     return false;
3910b57cec5SDimitry Andric   }
3920b57cec5SDimitry Andric 
393*5f7ddb14SDimitry Andric   if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
3940b57cec5SDimitry Andric     RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
3950b57cec5SDimitry Andric     ExecutionContext exe_ctx(GetExecutionContextRef());
3960b57cec5SDimitry Andric     RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
3970b57cec5SDimitry Andric     RegisterValue reg_value;
3980b57cec5SDimitry Andric     if (!reg_info || !reg_ctx) {
3990b57cec5SDimitry Andric       error.SetErrorString("unable to retrieve register info");
4000b57cec5SDimitry Andric       return false;
4010b57cec5SDimitry Andric     }
4020b57cec5SDimitry Andric     error = reg_value.SetValueFromData(reg_info, data, 0, true);
4030b57cec5SDimitry Andric     if (error.Fail())
4040b57cec5SDimitry Andric       return false;
4050b57cec5SDimitry Andric     if (reg_ctx->WriteRegister(reg_info, reg_value)) {
4060b57cec5SDimitry Andric       SetNeedsUpdate();
4070b57cec5SDimitry Andric       return true;
4080b57cec5SDimitry Andric     } else {
4090b57cec5SDimitry Andric       error.SetErrorString("unable to write back to register");
4100b57cec5SDimitry Andric       return false;
4110b57cec5SDimitry Andric     }
4120b57cec5SDimitry Andric   } else
4130b57cec5SDimitry Andric     return ValueObject::SetData(data, error);
4140b57cec5SDimitry Andric }
415