blob: e08963210ec426498494d3998c4407ede1534629 [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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Module.h"
Enrico Granata82fabf82013-04-30 20:45:04 +000015#include "lldb/Core/RegisterValue.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000016#include "lldb/Core/Scalar.h" // for Scalar, operator!=
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "lldb/Core/Value.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000018#include "lldb/Expression/DWARFExpression.h" // for DWARFExpression
19#include "lldb/Symbol/Declaration.h" // for Declaration
Greg Clayton1f746072012-08-29 21:13:06 +000020#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
Greg Clayton644247c2011-07-07 01:59:51 +000023#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Target/Target.h"
Zachary Turner2f3df612017-04-06 21:28:29 +000030#include "lldb/Utility/DataExtractor.h" // for DataExtractor
Zachary Turner97206d52017-05-12 04:51:55 +000031#include "lldb/Utility/Status.h" // for Status
Zachary Turner2f3df612017-04-06 21:28:29 +000032#include "lldb/lldb-private-enumerations.h" // for AddressType::eAddressTy...
33#include "lldb/lldb-types.h" // for addr_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
Zachary Turner2f3df612017-04-06 21:28:29 +000035#include "llvm/ADT/StringRef.h" // for StringRef
36
37#include <assert.h> // for assert
38#include <memory> // for shared_ptr
39
40namespace lldb_private {
41class ExecutionContextScope;
42}
43namespace lldb_private {
44class StackFrame;
45}
46namespace lldb_private {
47struct RegisterInfo;
48}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049using namespace lldb_private;
50
Jim Ingham58b59f92011-04-22 23:53:53 +000051lldb::ValueObjectSP
Kate Stoneb9c1b512016-09-06 20:57:50 +000052ValueObjectVariable::Create(ExecutionContextScope *exe_scope,
53 const lldb::VariableSP &var_sp) {
54 return (new ValueObjectVariable(exe_scope, var_sp))->GetSP();
Jim Ingham58b59f92011-04-22 23:53:53 +000055}
56
Kate Stoneb9c1b512016-09-06 20:57:50 +000057ValueObjectVariable::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();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063}
64
Kate Stoneb9c1b512016-09-06 20:57:50 +000065ValueObjectVariable::~ValueObjectVariable() {}
66
67CompilerType ValueObjectVariable::GetCompilerTypeImpl() {
68 Type *var_type = m_variable_sp->GetType();
69 if (var_type)
70 return var_type->GetForwardCompilerType();
71 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072}
73
Kate Stoneb9c1b512016-09-06 20:57:50 +000074ConstString ValueObjectVariable::GetTypeName() {
75 Type *var_type = m_variable_sp->GetType();
76 if (var_type)
77 return var_type->GetName();
78 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079}
80
Kate Stoneb9c1b512016-09-06 20:57:50 +000081ConstString ValueObjectVariable::GetDisplayTypeName() {
82 Type *var_type = m_variable_sp->GetType();
83 if (var_type)
84 return var_type->GetForwardCompilerType().GetDisplayTypeName();
85 return ConstString();
Greg Clayton84db9102012-03-26 23:03:23 +000086}
87
Kate Stoneb9c1b512016-09-06 20:57:50 +000088ConstString ValueObjectVariable::GetQualifiedTypeName() {
89 Type *var_type = m_variable_sp->GetType();
90 if (var_type)
91 return var_type->GetQualifiedName();
92 return ConstString();
Enrico Granatae8daa2f2014-05-17 19:14:17 +000093}
94
Kate Stoneb9c1b512016-09-06 20:57:50 +000095size_t ValueObjectVariable::CalculateNumChildren(uint32_t max) {
96 CompilerType type(GetCompilerType());
97
98 if (!type.IsValid())
99 return 0;
100
101 const bool omit_empty_base_classes = true;
102 auto child_count = type.GetNumChildren(omit_empty_base_classes);
103 return child_count <= max ? child_count : max;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104}
105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106uint64_t ValueObjectVariable::GetByteSize() {
107 ExecutionContext exe_ctx(GetExecutionContextRef());
108
109 CompilerType type(GetCompilerType());
110
111 if (!type.IsValid())
112 return 0;
113
114 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115}
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117lldb::ValueType ValueObjectVariable::GetValueType() const {
118 if (m_variable_sp)
119 return m_variable_sp->GetScope();
120 return lldb::eValueTypeInvalid;
121}
122
123bool ValueObjectVariable::UpdateValue() {
124 SetValueIsValid(false);
125 m_error.Clear();
126
127 Variable *variable = m_variable_sp.get();
128 DWARFExpression &expr = variable->LocationExpression();
129
130 if (variable->GetLocationIsConstantValueData()) {
131 // expr doesn't contain DWARF bytes, it contains the constant variable
132 // value bytes themselves...
133 if (expr.GetExpressionData(m_data))
134 m_value.SetContext(Value::eContextTypeVariable, variable);
135 else
136 m_error.SetErrorString("empty constant data");
137 // constant bytes can't be edited - sorry
138 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
139 } else {
140 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Enrico Granata951bdd52015-01-28 01:09:45 +0000141 ExecutionContext exe_ctx(GetExecutionContextRef());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 Target *target = exe_ctx.GetTargetPtr();
144 if (target) {
145 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
146 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Claytonf5fb4272010-09-18 04:00:06 +0000147 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149 if (expr.IsLocationList()) {
150 SymbolContext sc;
151 variable->CalculateSymbolContext(&sc);
152 if (sc.function)
153 loclist_base_load_addr =
154 sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(
155 target);
156 }
157 Value old_value(m_value);
Tamas Berghammerbba2c832017-08-16 11:45:10 +0000158 if (expr.Evaluate(&exe_ctx, nullptr, loclist_base_load_addr, nullptr,
159 nullptr, m_value, &m_error)) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 m_resolved_value = m_value;
161 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 CompilerType compiler_type = GetCompilerType();
164 if (compiler_type.IsValid())
165 m_value.SetCompilerType(compiler_type);
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167 Value::ValueType value_type = m_value.GetValueType();
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 Process *process = exe_ctx.GetProcessPtr();
170 const bool process_is_alive = process && process->IsAlive();
171 const uint32_t type_info = compiler_type.GetTypeInfo();
172 const bool is_pointer_or_ref =
173 (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
174
175 switch (value_type) {
176 case Value::eValueTypeFileAddress:
177 // If this type is a pointer, then its children will be considered load
Adrian Prantl05097242018-04-30 16:49:04 +0000178 // addresses if the pointer or reference is dereferenced, but only if
179 // the process is alive.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180 //
181 // There could be global variables like in the following code:
182 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
183 // Foo g_foo1;
184 // Foo g_foo2;
185 // LinkedListNode g_second_node = { &g_foo2, NULL };
186 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
187 //
188 // When we aren't running, we should be able to look at these variables
Adrian Prantl05097242018-04-30 16:49:04 +0000189 // using the "target variable" command. Children of the "g_first_node"
190 // always will be of the same address type as the parent. But children
191 // of the "next" member of LinkedListNode will become load addresses if
192 // we have a live process, or remain what a file address if it what a
193 // file address.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194 if (process_is_alive && is_pointer_or_ref)
195 SetAddressTypeOfChildren(eAddressTypeLoad);
196 else
197 SetAddressTypeOfChildren(eAddressTypeFile);
198 break;
199 case Value::eValueTypeHostAddress:
200 // Same as above for load addresses, except children of pointer or refs
Adrian Prantl05097242018-04-30 16:49:04 +0000201 // are always load addresses. Host addresses are used to store freeze
202 // dried variables. If this type is a struct, the entire struct
203 // contents will be copied into the heap of the
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 // LLDB process, but we do not currrently follow any pointers.
205 if (is_pointer_or_ref)
206 SetAddressTypeOfChildren(eAddressTypeLoad);
207 else
208 SetAddressTypeOfChildren(eAddressTypeHost);
209 break;
210 case Value::eValueTypeLoadAddress:
211 case Value::eValueTypeScalar:
212 case Value::eValueTypeVector:
213 SetAddressTypeOfChildren(eAddressTypeLoad);
214 break;
215 }
216
217 switch (value_type) {
218 case Value::eValueTypeVector:
219 // fall through
220 case Value::eValueTypeScalar:
Adrian Prantl05097242018-04-30 16:49:04 +0000221 // The variable value is in the Scalar value inside the m_value. We can
222 // point our m_data right to it.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223 m_error =
224 m_value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
225 break;
226
227 case Value::eValueTypeFileAddress:
228 case Value::eValueTypeLoadAddress:
229 case Value::eValueTypeHostAddress:
Adrian Prantl05097242018-04-30 16:49:04 +0000230 // The DWARF expression result was an address in the inferior process.
231 // If this variable is an aggregate type, we just need the address as
232 // the main value as all child variable objects will rely upon this
233 // location and add an offset and then read their own values as needed.
234 // If this variable is a simple type, we read all data for it into
235 // m_data. Make sure this type has a value before we try and read it
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236
237 // If we have a file address, convert it to a load address if we can.
238 if (value_type == Value::eValueTypeFileAddress && process_is_alive) {
239 lldb::addr_t file_addr =
240 m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
241 if (file_addr != LLDB_INVALID_ADDRESS) {
242 SymbolContext var_sc;
243 variable->CalculateSymbolContext(&var_sc);
244 if (var_sc.module_sp) {
245 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
246 if (objfile) {
247 Address so_addr(file_addr, objfile->GetSectionList());
248 lldb::addr_t load_addr = so_addr.GetLoadAddress(target);
249 if (load_addr != LLDB_INVALID_ADDRESS) {
250 m_value.SetValueType(Value::eValueTypeLoadAddress);
251 m_value.GetScalar() = load_addr;
252 }
253 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000254 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257
258 if (!CanProvideValue()) {
Adrian Prantl05097242018-04-30 16:49:04 +0000259 // this value object represents an aggregate type whose children have
260 // values, but this object does not. So we say we are changed if our
261 // location has changed.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000262 SetValueDidChange(value_type != old_value.GetValueType() ||
263 m_value.GetScalar() != old_value.GetScalar());
264 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000265 // Copy the Value and set the context to use our Variable so it can
266 // extract read its value into m_data appropriately
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 Value value(m_value);
268 value.SetContext(Value::eContextTypeVariable, variable);
269 m_error =
270 value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
271
272 SetValueDidChange(value_type != old_value.GetValueType() ||
273 m_value.GetScalar() != old_value.GetScalar());
Enrico Granata82fabf82013-04-30 20:45:04 +0000274 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275 break;
276 }
277
278 SetValueIsValid(m_error.Success());
279 } else {
280 // could not find location, won't allow editing
281 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283 }
284 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000285}
286
Kate Stoneb9c1b512016-09-06 20:57:50 +0000287bool ValueObjectVariable::IsInScope() {
288 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
289 if (exe_ctx_ref.HasFrameRef()) {
290 ExecutionContext exe_ctx(exe_ctx_ref);
291 StackFrame *frame = exe_ctx.GetFramePtr();
292 if (frame) {
293 return m_variable_sp->IsInScope(frame);
294 } else {
Adrian Prantl05097242018-04-30 16:49:04 +0000295 // This ValueObject had a frame at one time, but now we can't locate it,
296 // so return false since we probably aren't in scope.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000297 return false;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000298 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000299 }
Adrian Prantl05097242018-04-30 16:49:04 +0000300 // We have a variable that wasn't tied to a frame, which means it is a global
301 // and is always in scope.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 return true;
303}
304
305lldb::ModuleSP ValueObjectVariable::GetModule() {
306 if (m_variable_sp) {
307 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
308 if (sc_scope) {
309 return sc_scope->CalculateSymbolContextModule();
310 }
311 }
312 return lldb::ModuleSP();
313}
314
315SymbolContextScope *ValueObjectVariable::GetSymbolContextScope() {
316 if (m_variable_sp)
317 return m_variable_sp->GetSymbolContextScope();
318 return NULL;
319}
320
321bool ValueObjectVariable::GetDeclaration(Declaration &decl) {
322 if (m_variable_sp) {
323 decl = m_variable_sp->GetDeclaration();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000324 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000325 }
326 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327}
328
Kate Stoneb9c1b512016-09-06 20:57:50 +0000329const char *ValueObjectVariable::GetLocationAsCString() {
330 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
331 return GetLocationAsCStringImpl(m_resolved_value, m_data);
332 else
333 return ValueObject::GetLocationAsCString();
Greg Clayton644247c2011-07-07 01:59:51 +0000334}
335
Kate Stoneb9c1b512016-09-06 20:57:50 +0000336bool ValueObjectVariable::SetValueFromCString(const char *value_str,
Zachary Turner97206d52017-05-12 04:51:55 +0000337 Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000338 if (!UpdateValueIfNeeded()) {
339 error.SetErrorString("unable to update value before writing");
Greg Clayton81e871e2012-02-04 02:27:34 +0000340 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000341 }
342
343 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
344 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
345 ExecutionContext exe_ctx(GetExecutionContextRef());
346 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
347 RegisterValue reg_value;
348 if (!reg_info || !reg_ctx) {
349 error.SetErrorString("unable to retrieve register info");
350 return false;
351 }
Zachary Turnerac96f662016-11-17 23:47:31 +0000352 error = reg_value.SetValueFromString(reg_info, llvm::StringRef(value_str));
Kate Stoneb9c1b512016-09-06 20:57:50 +0000353 if (error.Fail())
354 return false;
355 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
356 SetNeedsUpdate();
357 return true;
358 } else {
359 error.SetErrorString("unable to write back to register");
360 return false;
361 }
362 } else
363 return ValueObject::SetValueFromCString(value_str, error);
Greg Clayton81e871e2012-02-04 02:27:34 +0000364}
Enrico Granata82fabf82013-04-30 20:45:04 +0000365
Zachary Turner97206d52017-05-12 04:51:55 +0000366bool ValueObjectVariable::SetData(DataExtractor &data, Status &error) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000367 if (!UpdateValueIfNeeded()) {
368 error.SetErrorString("unable to update value before writing");
369 return false;
370 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000371
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo) {
373 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
374 ExecutionContext exe_ctx(GetExecutionContextRef());
375 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
376 RegisterValue reg_value;
377 if (!reg_info || !reg_ctx) {
378 error.SetErrorString("unable to retrieve register info");
379 return false;
Sean Callanan6826d222014-01-18 01:13:50 +0000380 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000381 error = reg_value.SetValueFromData(reg_info, data, 0, true);
382 if (error.Fail())
383 return false;
384 if (reg_ctx->WriteRegister(reg_info, reg_value)) {
385 SetNeedsUpdate();
386 return true;
387 } else {
388 error.SetErrorString("unable to write back to register");
389 return false;
Enrico Granata82fabf82013-04-30 20:45:04 +0000390 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000391 } else
392 return ValueObject::SetData(data, error);
Enrico Granata82fabf82013-04-30 20:45:04 +0000393}