blob: 9b9e51a0abb87b7a32111a8ca4f7781f1bf4136a [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ValueObjectVariable.cpp ---------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chris Lattner30fdc8d2010-06-08 16:52:24 +000010#include "lldb/Core/ValueObjectVariable.h"
11
Zachary Turner2f3df612017-04-06 21:28:29 +000012#include "lldb/Core/Address.h" // for Address
13#include "lldb/Core/AddressRange.h" // for AddressRange
14#include "lldb/Core/ArchSpec.h" // for ArchSpec
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Module.h"
Enrico Granata82fabf82013-04-30 20:45:04 +000016#include "lldb/Core/RegisterValue.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000017#include "lldb/Core/Scalar.h" // for Scalar, operator!=
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018#include "lldb/Core/Value.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000019#include "lldb/Expression/DWARFExpression.h" // for DWARFExpression
20#include "lldb/Symbol/Declaration.h" // for Declaration
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
Greg Clayton644247c2011-07-07 01:59:51 +000024#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Symbol/Type.h"
26#include "lldb/Symbol/Variable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000031#include "lldb/Utility/DataExtractor.h" // for DataExtractor
Zachary Turner97206d52017-05-12 04:51:55 +000032#include "lldb/Utility/Status.h" // for Status
Zachary Turner2f3df612017-04-06 21:28:29 +000033#include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy...
34#include "lldb/lldb-types.h" // for addr_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Zachary Turner2f3df612017-04-06 21:28:29 +000036#include "llvm/ADT/StringRef.h" // for StringRef
37
38#include <assert.h> // for assert
39#include <memory> // for shared_ptr
40
41namespace lldb_private {
42class ExecutionContextScope;
43}
44namespace lldb_private {
45class StackFrame;
46}
47namespace lldb_private {
48struct RegisterInfo;
49}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050using namespace lldb_private;
51
Jim Ingham58b59f92011-04-22 23:53:53 +000052lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +000053ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
54 const lldb::VariableSP &var_sp) {
55 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
Jim Ingham58b59f92011-04-22 23:53:53 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058ValueObjectVariable::ValueObjectVariable(ExecutionContextScope *exe_scope,
59 const lldb::VariableSP &var_sp)
60 : ValueObject(exe_scope), m_variable_sp(var_sp) {
61 // Do not attempt to construct one of these objects with no variable!
62 assert(m_variable_sp.get() != NULL);
63 m_name = var_sp->GetName();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Kate Stoneb9c1b512016-09-06 20:57:50 +000066ValueObjectVariable::~ValueObjectVariable() {}
67
68CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
69 Type *var_type = m_variable_sp->GetType();
70 if (var_type)
71 return var_type->GetForwardCompilerType();
72 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073}
74
Kate Stoneb9c1b512016-09-06 20:57:50 +000075ConstString ValueObjectVariable::GetTypeName() {
76 Type *var_type = m_variable_sp->GetType();
77 if (var_type)
78 return var_type->GetName();
79 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
Kate Stoneb9c1b512016-09-06 20:57:50 +000082ConstString ValueObjectVariable::GetDisplayTypeName() {
83 Type *var_type = m_variable_sp->GetType();
84 if (var_type)
85 return var_type->GetForwardCompilerType().GetDisplayTypeName();
86 return ConstString();
Greg Clayton84db9102012-03-26 23:03:23 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089ConstString ValueObjectVariable::GetQualifiedTypeName() {
90 Type *var_type = m_variable_sp->GetType();
91 if (var_type)
92 return var_type->GetQualifiedName();
93 return ConstString();
Enrico Granatae8daa2f2014-05-17 19:14:17 +000094}
95
Kate Stoneb9c1b512016-09-06 20:57:50 +000096size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
97 CompilerType type(GetCompilerType());
98
99 if (!type.IsValid())
100 return 0;
101
102 const bool omit_empty_base_classes = true;
103 auto child_count = type.GetNumChildren(omit_empty_base_classes);
104 return child_count <= max ? child_count : max;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
Kate Stoneb9c1b512016-09-06 20:57:50 +0000107uint64_t ValueObjectVariable::GetByteSize() {
108 ExecutionContext exe_ctx(GetExecutionContextRef());
109
110 CompilerType type(GetCompilerType());
111
112 if (!type.IsValid())
113 return 0;
114
115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
Kate Stoneb9c1b512016-09-06 20:57:50 +0000118lldb::ValueType ValueObjectVariable::GetValueType() const {
119 if (m_variable_sp)
120 return m_variable_sp->GetScope();
121 return lldb::eValueTypeInvalid;
122}
123
124bool ValueObjectVariable::UpdateValue() {
125 SetValueIsValid(false);
126 m_error.Clear();
127
128 Variable *variable = m_variable_sp.get();
129 DWARFExpression &expr = variable->LocationExpression();
130
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);
136 else
137 m_error.SetErrorString("empty constant data");
138 // constant bytes can't be edited - sorry
139 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
140 } else {
141 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Enrico Granata951bdd52015-01-28 01:09:45 +0000142 ExecutionContext exe_ctx(GetExecutionContextRef());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000143
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 Target *target = exe_ctx.GetTargetPtr();
145 if (target) {
146 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
147 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Claytonf5fb4272010-09-18 04:00:06 +0000148 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 if (expr.IsLocationList()) {
151 SymbolContext sc;
152 variable->CalculateSymbolContext(&sc);
153 if (sc.function)
154 loclist_base_load_addr =
155 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
156 target);
157 }
158 Value old_value(m_value);
159 if (expr.Evaluate(&exe_ctx, nullptr, nullptr, nullptr,
160 loclist_base_load_addr, nullptr, nullptr, m_value,
161 &m_error)) {
162 m_resolved_value = m_value;
163 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164
Kate Stoneb9c1b512016-09-06 20:57:50 +0000165 CompilerType compiler_type = GetCompilerType();
166 if (compiler_type.IsValid())
167 m_value.SetCompilerType(compiler_type);
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 Value::ValueType value_type = m_value.GetValueType();
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000170
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 Process *process = exe_ctx.GetProcessPtr();
172 const bool process_is_alive = process && process->IsAlive();
173 const uint32_t type_info = compiler_type.GetTypeInfo();
174 const bool is_pointer_or_ref =
175 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
176
177 switch (value_type) {
178 case Value::eValueTypeFileAddress:
179 // If this type is a pointer, then its children will be considered load
180 // addresses
181 // if the pointer or reference is dereferenced, but only if the process
182 // is alive.
183 //
184 // There could be global variables like in the following code:
185 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
186 // Foo g_foo1;
187 // Foo g_foo2;
188 // LinkedListNode g_second_node = { &g_foo2, NULL };
189 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
190 //
191 // When we aren't running, we should be able to look at these variables
192 // using
193 // the "target variable" command. Children of the "g_first_node" always
194 // will
195 // be of the same address type as the parent. But children of the "next"
196 // member of
197 // LinkedListNode will become load addresses if we have a live process,
198 // or remain
199 // what a file address if it what a file address.
200 if (process_is_alive && is_pointer_or_ref)
201 SetAddressTypeOfChildren(eAddressTypeLoad);
202 else
203 SetAddressTypeOfChildren(eAddressTypeFile);
204 break;
205 case Value::eValueTypeHostAddress:
206 // Same as above for load addresses, except children of pointer or refs
207 // are always
208 // load addresses. Host addresses are used to store freeze dried
209 // variables. If this
210 // type is a struct, the entire struct contents will be copied into the
211 // heap of the
212 // LLDB process, but we do not currrently follow any pointers.
213 if (is_pointer_or_ref)
214 SetAddressTypeOfChildren(eAddressTypeLoad);
215 else
216 SetAddressTypeOfChildren(eAddressTypeHost);
217 break;
218 case Value::eValueTypeLoadAddress:
219 case Value::eValueTypeScalar:
220 case Value::eValueTypeVector:
221 SetAddressTypeOfChildren(eAddressTypeLoad);
222 break;
223 }
224
225 switch (value_type) {
226 case Value::eValueTypeVector:
227 // fall through
228 case Value::eValueTypeScalar:
229 // The variable value is in the Scalar value inside the m_value.
230 // We can point our m_data right to it.
231 m_error =
232 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
233 break;
234
235 case Value::eValueTypeFileAddress:
236 case Value::eValueTypeLoadAddress:
237 case Value::eValueTypeHostAddress:
238 // The DWARF expression result was an address in the inferior
239 // process. If this variable is an aggregate type, we just need
240 // the address as the main value as all child variable objects
241 // will rely upon this location and add an offset and then read
242 // their own values as needed. If this variable is a simple
243 // type, we read all data for it into m_data.
244 // Make sure this type has a value before we try and read it
245
246 // If we have a file address, convert it to a load address if we can.
247 if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
248 lldb::addr_t file_addr =
249 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
250 if (file_addr != LLDB_INVALID_ADDRESS) {
251 SymbolContext var_sc;
252 variable->CalculateSymbolContext(&var_sc);
253 if (var_sc.module_sp) {
254 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
255 if (objfile) {
256 Address so_addr(file_addr, objfile->GetSectionList());
257 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
258 if (load_addr != LLDB_INVALID_ADDRESS) {
259 m_value.SetValueType(Value::eValueTypeLoadAddress);
260 m_value.GetScalar() = load_addr;
261 }
262 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000263 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000266
267 if (!CanProvideValue()) {
268 // this value object represents an aggregate type whose
269 // children have values, but this object does not. So we
270 // say we are changed if our location has changed.
271 SetValueDidChange(value_type != old_value.GetValueType() ||
272 m_value.GetScalar() != old_value.GetScalar());
273 } else {
274 // Copy the Value and set the context to use our Variable
275 // so it can extract read its value into m_data appropriately
276 Value value(m_value);
277 value.SetContext(Value::eContextTypeVariable, variable);
278 m_error =
279 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
280
281 SetValueDidChange(value_type != old_value.GetValueType() ||
282 m_value.GetScalar() != old_value.GetScalar());
Enrico Granata82fabf82013-04-30 20:45:04 +0000283 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 break;
285 }
286
287 SetValueIsValid(m_error.Success());
288 } else {
289 // could not find location, won't allow editing
290 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292 }
293 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
Kate Stoneb9c1b512016-09-06 20:57:50 +0000296bool ValueObjectVariable::IsInScope() {
297 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
298 if (exe_ctx_ref.HasFrameRef()) {
299 ExecutionContext exe_ctx(exe_ctx_ref);
300 StackFrame *frame = exe_ctx.GetFramePtr();
301 if (frame) {
302 return m_variable_sp->IsInScope(frame);
303 } else {
304 // This ValueObject had a frame at one time, but now we
305 // can't locate it, so return false since we probably aren't
306 // in scope.
307 return false;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000308 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000309 }
310 // We have a variable that wasn't tied to a frame, which
311 // means it is a global and is always in scope.
312 return true;
313}
314
315lldb::ModuleSP ValueObjectVariable::GetModule() {
316 if (m_variable_sp) {
317 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
318 if (sc_scope) {
319 return sc_scope->CalculateSymbolContextModule();
320 }
321 }
322 return lldb::ModuleSP();
323}
324
325SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
326 if (m_variable_sp)
327 return m_variable_sp->GetSymbolContextScope();
328 return NULL;
329}
330
331bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
332 if (m_variable_sp) {
333 decl = m_variable_sp->GetDeclaration();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000334 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 }
336 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337}
338
Kate Stoneb9c1b512016-09-06 20:57:50 +0000339const char *ValueObjectVariable::GetLocationAsCString() {
340 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
341 return GetLocationAsCStringImpl(m_resolved_value, m_data);
342 else
343 return ValueObject::GetLocationAsCString();
Greg Clayton644247c2011-07-07 01:59:51 +0000344}
345
Kate Stoneb9c1b512016-09-06 20:57:50 +0000346bool ValueObjectVariable::SetValueFromCString(const char *value_str,
Zachary Turner97206d52017-05-12 04:51:55 +0000347 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 if (!UpdateValueIfNeeded()) {
349 error.SetErrorString("unable to update value before writing");
Greg Clayton81e871e2012-02-04 02:27:34 +0000350 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351 }
352
353 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
354 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
355 ExecutionContext exe_ctx(GetExecutionContextRef());
356 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
357 RegisterValue reg_value;
358 if (!reg_info || !reg_ctx) {
359 error.SetErrorString("unable to retrieve register info");
360 return false;
361 }
Zachary Turnerac96f662016-11-17 23:47:31 +0000362 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 if (error.Fail())
364 return false;
365 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
366 SetNeedsUpdate();
367 return true;
368 } else {
369 error.SetErrorString("unable to write back to register");
370 return false;
371 }
372 } else
373 return ValueObject::SetValueFromCString(value_str, error);
Greg Clayton81e871e2012-02-04 02:27:34 +0000374}
Enrico Granata82fabf82013-04-30 20:45:04 +0000375
Zachary Turner97206d52017-05-12 04:51:55 +0000376bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000377 if (!UpdateValueIfNeeded()) {
378 error.SetErrorString("unable to update value before writing");
379 return false;
380 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000381
Kate Stoneb9c1b512016-09-06 20:57:50 +0000382 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
383 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
384 ExecutionContext exe_ctx(GetExecutionContextRef());
385 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
386 RegisterValue reg_value;
387 if (!reg_info || !reg_ctx) {
388 error.SetErrorString("unable to retrieve register info");
389 return false;
Sean Callanan6826d222014-01-18 01:13:50 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 error = reg_value.SetValueFromData(reg_info, data, 0, true);
392 if (error.Fail())
393 return false;
394 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
395 SetNeedsUpdate();
396 return true;
397 } else {
398 error.SetErrorString("unable to write back to register");
399 return false;
Enrico Granata82fabf82013-04-30 20:45:04 +0000400 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000401 } else
402 return ValueObject::SetData(data, error);
Enrico Granata82fabf82013-04-30 20:45:04 +0000403}