1 //===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/ValueObjectVariable.h"
12 #include "lldb/Core/Address.h"
13 #include "lldb/Core/AddressRange.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/Value.h"
16 #include "lldb/Expression/DWARFExpression.h"
17 #include "lldb/Symbol/Declaration.h"
18 #include "lldb/Symbol/Function.h"
19 #include "lldb/Symbol/ObjectFile.h"
20 #include "lldb/Symbol/SymbolContext.h"
21 #include "lldb/Symbol/SymbolContextScope.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/Process.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/DataExtractor.h"
29 #include "lldb/Utility/RegisterValue.h"
30 #include "lldb/Utility/Scalar.h"
31 #include "lldb/Utility/Status.h"
32 #include "lldb/lldb-private-enumerations.h"
33 #include "lldb/lldb-types.h"
35 #include "llvm/ADT/StringRef.h"
40 namespace lldb_private {
41 class ExecutionContextScope;
43 namespace lldb_private {
46 namespace lldb_private {
49 using namespace lldb_private;
52 ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
53 const lldb::VariableSP &var_sp) {
54 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
57 ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
58 const lldb::VariableSP &var_sp)
59 : ValueObject(exe_scope), m_variable_sp(var_sp) {
60 // Do not attempt to construct one of these objects with no variable!
61 assert(m_variable_sp.get() != NULL);
62 m_name = var_sp->GetName();
65 ValueObjectVariable::~ValueObjectVariable() {}
67 CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
68 Type *var_type = m_variable_sp->GetType();
70 return var_type->GetForwardCompilerType();
71 return CompilerType();
74 ConstString ValueObjectVariable::GetTypeName() {
75 Type *var_type = m_variable_sp->GetType();
77 return var_type->GetName();
81 ConstString ValueObjectVariable::GetDisplayTypeName() {
82 Type *var_type = m_variable_sp->GetType();
84 return var_type->GetForwardCompilerType().GetDisplayTypeName();
88 ConstString ValueObjectVariable::GetQualifiedTypeName() {
89 Type *var_type = m_variable_sp->GetType();
91 return var_type->GetQualifiedName();
95 size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
96 CompilerType type(GetCompilerType());
101 ExecutionContext exe_ctx(GetExecutionContextRef());
102 const bool omit_empty_base_classes = true;
103 auto child_count = type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
104 return child_count <= max ? child_count : max;
107 uint64_t ValueObjectVariable::GetByteSize() {
108 ExecutionContext exe_ctx(GetExecutionContextRef());
110 CompilerType type(GetCompilerType());
115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope()).getValueOr(0);
118 lldb::ValueType ValueObjectVariable::GetValueType() const {
120 return m_variable_sp->GetScope();
121 return lldb::eValueTypeInvalid;
124 bool ValueObjectVariable::UpdateValue() {
125 SetValueIsValid(false);
128 Variable *variable = m_variable_sp.get();
129 DWARFExpression &expr = variable->LocationExpression();
131 if (variable->GetLocationIsConstantValueData()) {
132 // expr doesn't contain DWARF bytes, it contains the constant variable
133 // value bytes themselves...
134 if (expr.GetExpressionData(m_data))
135 m_value.SetContext(Value::eContextTypeVariable, variable);
137 m_error.SetErrorString("empty constant data");
138 // constant bytes can't be edited - sorry
139 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
141 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
142 ExecutionContext exe_ctx(GetExecutionContextRef());
144 Target *target = exe_ctx.GetTargetPtr();
146 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
147 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
150 if (expr.IsLocationList()) {
152 variable->CalculateSymbolContext(&sc);
154 loclist_base_load_addr =
155 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
158 Value old_value(m_value);
159 if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
160 nullptr, m_value, &m_error)) {
161 m_resolved_value = m_value;
162 m_value.SetContext(Value::eContextTypeVariable, variable);
164 CompilerType compiler_type = GetCompilerType();
165 if (compiler_type.IsValid())
166 m_value.SetCompilerType(compiler_type);
168 Value::ValueType value_type = m_value.GetValueType();
170 Process *process = exe_ctx.GetProcessPtr();
171 const bool process_is_alive = process && process->IsAlive();
172 const uint32_t type_info = compiler_type.GetTypeInfo();
173 const bool is_pointer_or_ref =
174 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
176 switch (value_type) {
177 case Value::eValueTypeFileAddress:
178 // If this type is a pointer, then its children will be considered load
179 // addresses if the pointer or reference is dereferenced, but only if
180 // the process is alive.
182 // There could be global variables like in the following code:
183 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
186 // LinkedListNode g_second_node = { &g_foo2, NULL };
187 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
189 // When we aren't running, we should be able to look at these variables
190 // using the "target variable" command. Children of the "g_first_node"
191 // always will be of the same address type as the parent. But children
192 // of the "next" member of LinkedListNode will become load addresses if
193 // we have a live process, or remain what a file address if it what a
195 if (process_is_alive && is_pointer_or_ref)
196 SetAddressTypeOfChildren(eAddressTypeLoad);
198 SetAddressTypeOfChildren(eAddressTypeFile);
200 case Value::eValueTypeHostAddress:
201 // Same as above for load addresses, except children of pointer or refs
202 // are always load addresses. Host addresses are used to store freeze
203 // dried variables. If this type is a struct, the entire struct
204 // contents will be copied into the heap of the
205 // LLDB process, but we do not currently follow any pointers.
206 if (is_pointer_or_ref)
207 SetAddressTypeOfChildren(eAddressTypeLoad);
209 SetAddressTypeOfChildren(eAddressTypeHost);
211 case Value::eValueTypeLoadAddress:
212 case Value::eValueTypeScalar:
213 case Value::eValueTypeVector:
214 SetAddressTypeOfChildren(eAddressTypeLoad);
218 switch (value_type) {
219 case Value::eValueTypeVector:
221 case Value::eValueTypeScalar:
222 // The variable value is in the Scalar value inside the m_value. We can
223 // point our m_data right to it.
225 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
228 case Value::eValueTypeFileAddress:
229 case Value::eValueTypeLoadAddress:
230 case Value::eValueTypeHostAddress:
231 // The DWARF expression result was an address in the inferior process.
232 // If this variable is an aggregate type, we just need the address as
233 // the main value as all child variable objects will rely upon this
234 // location and add an offset and then read their own values as needed.
235 // If this variable is a simple type, we read all data for it into
236 // m_data. Make sure this type has a value before we try and read it
238 // If we have a file address, convert it to a load address if we can.
239 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
240 m_value.ConvertToLoadAddress(GetModule().get(), target);
242 if (!CanProvideValue()) {
243 // this value object represents an aggregate type whose children have
244 // values, but this object does not. So we say we are changed if our
245 // location has changed.
246 SetValueDidChange(value_type != old_value.GetValueType() ||
247 m_value.GetScalar() != old_value.GetScalar());
249 // Copy the Value and set the context to use our Variable so it can
250 // extract read its value into m_data appropriately
251 Value value(m_value);
252 value.SetContext(Value::eContextTypeVariable, variable);
254 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
256 SetValueDidChange(value_type != old_value.GetValueType() ||
257 m_value.GetScalar() != old_value.GetScalar());
262 SetValueIsValid(m_error.Success());
264 // could not find location, won't allow editing
265 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
268 return m_error.Success();
271 bool ValueObjectVariable::IsInScope() {
272 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
273 if (exe_ctx_ref.HasFrameRef()) {
274 ExecutionContext exe_ctx(exe_ctx_ref);
275 StackFrame *frame = exe_ctx.GetFramePtr();
277 return m_variable_sp->IsInScope(frame);
279 // This ValueObject had a frame at one time, but now we can't locate it,
280 // so return false since we probably aren't in scope.
284 // We have a variable that wasn't tied to a frame, which means it is a global
285 // and is always in scope.
289 lldb::ModuleSP ValueObjectVariable::GetModule() {
291 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
293 return sc_scope->CalculateSymbolContextModule();
296 return lldb::ModuleSP();
299 SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
301 return m_variable_sp->GetSymbolContextScope();
305 bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
307 decl = m_variable_sp->GetDeclaration();
313 const char *ValueObjectVariable::GetLocationAsCString() {
314 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
315 return GetLocationAsCStringImpl(m_resolved_value, m_data);
317 return ValueObject::GetLocationAsCString();
320 bool ValueObjectVariable::SetValueFromCString(const char *value_str,
322 if (!UpdateValueIfNeeded()) {
323 error.SetErrorString("unable to update value before writing");
327 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
328 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
329 ExecutionContext exe_ctx(GetExecutionContextRef());
330 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
331 RegisterValue reg_value;
332 if (!reg_info || !reg_ctx) {
333 error.SetErrorString("unable to retrieve register info");
336 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
339 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
343 error.SetErrorString("unable to write back to register");
347 return ValueObject::SetValueFromCString(value_str, error);
350 bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
351 if (!UpdateValueIfNeeded()) {
352 error.SetErrorString("unable to update value before writing");
356 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
357 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
358 ExecutionContext exe_ctx(GetExecutionContextRef());
359 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
360 RegisterValue reg_value;
361 if (!reg_info || !reg_ctx) {
362 error.SetErrorString("unable to retrieve register info");
365 error = reg_value.SetValueFromData(reg_info, data, 0, true);
368 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
372 error.SetErrorString("unable to write back to register");
376 return ValueObject::SetData(data, error);