#include "lldb/Core/ValueObjectVariable.h"
#include "lldb/Core/Address.h"
#include "lldb/Core/AddressRange.h"
#include "lldb/Core/Declaration.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Expression/DWARFExpressionList.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/SymbolContextScope.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/RegisterContext.h"
#include "lldb/Target/Target.h"
#include "lldb/Utility/DataExtractor.h"
#include "lldb/Utility/RegisterValue.h"
#include "lldb/Utility/Scalar.h"
#include "lldb/Utility/Status.h"
#include "lldb/lldb-private-enumerations.h"
#include "lldb/lldb-types.h"
#include "llvm/ADT/StringRef.h"
#include <cassert>
#include <memory>
#include <optional>
namespace lldb_private {
class ExecutionContextScope;
}
namespace lldb_private {
class StackFrame;
}
namespace lldb_private {
struct RegisterInfo;
}
using namespace lldb_private;
lldb::ValueObjectSP
ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
const lldb::VariableSP &var_sp) {
auto manager_sp = ValueObjectManager::Create();
return (new ValueObjectVariable(exe_scope, *manager_sp, var_sp))->GetSP();
}
ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
ValueObjectManager &manager,
const lldb::VariableSP &var_sp)
: ValueObject(exe_scope, manager), m_variable_sp(var_sp) {
assert(m_variable_sp.get() != nullptr);
m_name = var_sp->GetName();
}
ValueObjectVariable::~ValueObjectVariable() = default;
CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetForwardCompilerType();
return CompilerType();
}
ConstString ValueObjectVariable::GetTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetName();
return ConstString();
}
ConstString ValueObjectVariable::GetDisplayTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetForwardCompilerType().GetDisplayTypeName();
return ConstString();
}
ConstString ValueObjectVariable::GetQualifiedTypeName() {
Type *var_type = m_variable_sp->GetType();
if (var_type)
return var_type->GetQualifiedName();
return ConstString();
}
llvm::Expected<uint32_t>
ValueObjectVariable::CalculateNumChildren(uint32_t max) {
CompilerType type(GetCompilerType());
if (!type.IsValid())
return llvm::make_error<llvm::StringError>("invalid type",
llvm::inconvertibleErrorCode());
ExecutionContext exe_ctx(GetExecutionContextRef());
const bool omit_empty_base_classes = true;
auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
if (!child_count)
return child_count;
return *child_count <= max ? *child_count : max;
}
std::optional<uint64_t> ValueObjectVariable::GetByteSize() {
ExecutionContext exe_ctx(GetExecutionContextRef());
CompilerType type(GetCompilerType());
if (!type.IsValid())
return {};
return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
}
lldb::ValueType ValueObjectVariable::GetValueType() const {
if (m_variable_sp)
return m_variable_sp->GetScope();
return lldb::eValueTypeInvalid;
}
bool ValueObjectVariable::UpdateValue() {
SetValueIsValid(false);
m_error.Clear();
Variable *variable = m_variable_sp.get();
DWARFExpressionList &expr_list = variable->LocationExpressionList();
if (variable->GetLocationIsConstantValueData()) {
if (expr_list.GetExpressionData(m_data)) {
if (m_data.GetDataStart() && m_data.GetByteSize())
m_value.SetBytes(m_data.GetDataStart(), m_data.GetByteSize());
m_value.SetContext(Value::ContextType::Variable, variable);
} else
m_error.SetErrorString("empty constant data");
m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
} else {
lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
ExecutionContext exe_ctx(GetExecutionContextRef());
Target *target = exe_ctx.GetTargetPtr();
if (target) {
m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
}
if (!expr_list.IsAlwaysValidSingleExpr()) {
SymbolContext sc;
variable->CalculateSymbolContext(&sc);
if (sc.function)
loclist_base_load_addr =
sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
target);
}
Value old_value(m_value);
llvm::Expected<Value> maybe_value = expr_list.Evaluate(
&exe_ctx, nullptr, loclist_base_load_addr, nullptr, nullptr);
if (maybe_value) {
m_value = *maybe_value;
m_resolved_value = m_value;
m_value.SetContext(Value::ContextType::Variable, variable);
CompilerType compiler_type = GetCompilerType();
if (compiler_type.IsValid())
m_value.SetCompilerType(compiler_type);
Value::ValueType value_type = m_value.GetValueType();
if (value_type == Value::ValueType::HostAddress &&
compiler_type.IsValid()) {
if (size_t value_buf_size = m_value.GetBuffer().GetByteSize()) {
size_t value_size = m_value.GetValueByteSize(&m_error, &exe_ctx);
if (m_error.Success() && value_buf_size < value_size)
m_value.ResizeData(value_size);
}
}
Process *process = exe_ctx.GetProcessPtr();
const bool process_is_alive = process && process->IsAlive();
switch (value_type) {
case Value::ValueType::Invalid:
m_error.SetErrorString("invalid value");
break;
case Value::ValueType::Scalar:
m_error =
m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
break;
case Value::ValueType::FileAddress:
case Value::ValueType::LoadAddress:
case Value::ValueType::HostAddress:
if (value_type == Value::ValueType::FileAddress && process_is_alive)
m_value.ConvertToLoadAddress(GetModule().get(), target);
if (!CanProvideValue()) {
SetValueDidChange(value_type != old_value.GetValueType() ||
m_value.GetScalar() != old_value.GetScalar());
} else {
Value value(m_value);
value.SetContext(Value::ContextType::Variable, variable);
m_error =
value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
SetValueDidChange(value_type != old_value.GetValueType() ||
m_value.GetScalar() != old_value.GetScalar());
}
break;
}
SetValueIsValid(m_error.Success());
} else {
m_error = maybe_value.takeError();
m_resolved_value.SetContext(Value::ContextType::Invalid, nullptr);
}
}
return m_error.Success();
}
void ValueObjectVariable::DoUpdateChildrenAddressType(ValueObject &valobj) {
Value::ValueType value_type = valobj.GetValue().GetValueType();
ExecutionContext exe_ctx(GetExecutionContextRef());
Process *process = exe_ctx.GetProcessPtr();
const bool process_is_alive = process && process->IsAlive();
const uint32_t type_info = valobj.GetCompilerType().GetTypeInfo();
const bool is_pointer_or_ref =
(type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
switch (value_type) {
case Value::ValueType::Invalid:
break;
case Value::ValueType::FileAddress:
if (process_is_alive && is_pointer_or_ref)
valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
else
valobj.SetAddressTypeOfChildren(eAddressTypeFile);
break;
case Value::ValueType::HostAddress:
if (is_pointer_or_ref)
valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
else
valobj.SetAddressTypeOfChildren(eAddressTypeHost);
break;
case Value::ValueType::LoadAddress:
case Value::ValueType::Scalar:
valobj.SetAddressTypeOfChildren(eAddressTypeLoad);
break;
}
}
bool ValueObjectVariable::IsInScope() {
const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
if (exe_ctx_ref.HasFrameRef()) {
ExecutionContext exe_ctx(exe_ctx_ref);
StackFrame *frame = exe_ctx.GetFramePtr();
if (frame) {
return m_variable_sp->IsInScope(frame);
} else {
return false;
}
}
return true;
}
lldb::ModuleSP ValueObjectVariable::GetModule() {
if (m_variable_sp) {
SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
if (sc_scope) {
return sc_scope->CalculateSymbolContextModule();
}
}
return lldb::ModuleSP();
}
SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
if (m_variable_sp)
return m_variable_sp->GetSymbolContextScope();
return nullptr;
}
bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
if (m_variable_sp) {
decl = m_variable_sp->GetDeclaration();
return true;
}
return false;
}
const char *ValueObjectVariable::GetLocationAsCString() {
if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo)
return GetLocationAsCStringImpl(m_resolved_value, m_data);
else
return ValueObject::GetLocationAsCString();
}
bool ValueObjectVariable::SetValueFromCString(const char *value_str,
Status &error) {
if (!UpdateValueIfNeeded()) {
error.SetErrorString("unable to update value before writing");
return false;
}
if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
ExecutionContext exe_ctx(GetExecutionContextRef());
RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
RegisterValue reg_value;
if (!reg_info || !reg_ctx) {
error.SetErrorString("unable to retrieve register info");
return false;
}
error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
if (error.Fail())
return false;
if (reg_ctx->WriteRegister(reg_info, reg_value)) {
SetNeedsUpdate();
return true;
} else {
error.SetErrorString("unable to write back to register");
return false;
}
} else
return ValueObject::SetValueFromCString(value_str, error);
}
bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
if (!UpdateValueIfNeeded()) {
error.SetErrorString("unable to update value before writing");
return false;
}
if (m_resolved_value.GetContextType() == Value::ContextType::RegisterInfo) {
RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
ExecutionContext exe_ctx(GetExecutionContextRef());
RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
RegisterValue reg_value;
if (!reg_info || !reg_ctx) {
error.SetErrorString("unable to retrieve register info");
return false;
}
error = reg_value.SetValueFromData(*reg_info, data, 0, true);
if (error.Fail())
return false;
if (reg_ctx->WriteRegister(reg_info, reg_value)) {
SetNeedsUpdate();
return true;
} else {
error.SetErrorString("unable to write back to register");
return false;
}
} else
return ValueObject::SetData(data, error);
}