blob: a29d858d169f18e4a2543b8b4f9a7ea590a684ae [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
10
11#include "lldb/Core/ValueObjectVariable.h"
12
13// C Includes
14// C++ Includes
15// Other libraries and framework includes
16// Project includes
17#include "lldb/Core/Module.h"
Enrico Granata82fabf82013-04-30 20:45:04 +000018#include "lldb/Core/RegisterValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "lldb/Core/ValueObjectList.h"
20#include "lldb/Core/Value.h"
21
Greg Clayton1f746072012-08-29 21:13:06 +000022#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Symbol/ObjectFile.h"
24#include "lldb/Symbol/SymbolContext.h"
Greg Clayton644247c2011-07-07 01:59:51 +000025#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Symbol/Type.h"
27#include "lldb/Symbol/Variable.h"
28
29#include "lldb/Target/ExecutionContext.h"
30#include "lldb/Target/Process.h"
31#include "lldb/Target/RegisterContext.h"
32#include "lldb/Target/Target.h"
33#include "lldb/Target/Thread.h"
34
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
36using namespace lldb_private;
37
Jim Ingham58b59f92011-04-22 23:53:53 +000038lldb::ValueObjectSP
39ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
40{
41 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
42}
43
Jim Ingham6035b672011-03-31 00:19:25 +000044ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
45 ValueObject(exe_scope),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 m_variable_sp(var_sp)
47{
48 // Do not attempt to construct one of these objects with no variable!
49 assert (m_variable_sp.get() != NULL);
50 m_name = var_sp->GetName();
51}
52
53ValueObjectVariable::~ValueObjectVariable()
54{
55}
56
Greg Claytona1e5dc82015-08-11 22:53:00 +000057CompilerType
Greg Clayton99558cc42015-08-24 23:46:31 +000058ValueObjectVariable::GetCompilerTypeImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059{
60 Type *var_type = m_variable_sp->GetType();
61 if (var_type)
Greg Clayton99558cc42015-08-24 23:46:31 +000062 return var_type->GetForwardCompilerType ();
Greg Claytona1e5dc82015-08-11 22:53:00 +000063 return CompilerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
66ConstString
67ValueObjectVariable::GetTypeName()
68{
69 Type * var_type = m_variable_sp->GetType();
70 if (var_type)
71 return var_type->GetName();
Greg Clayton84db9102012-03-26 23:03:23 +000072 return ConstString();
73}
74
75ConstString
Enrico Granatae8daa2f2014-05-17 19:14:17 +000076ValueObjectVariable::GetDisplayTypeName()
77{
78 Type * var_type = m_variable_sp->GetType();
79 if (var_type)
Greg Clayton99558cc42015-08-24 23:46:31 +000080 return var_type->GetForwardCompilerType ().GetDisplayTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +000081 return ConstString();
82}
83
84ConstString
Greg Clayton84db9102012-03-26 23:03:23 +000085ValueObjectVariable::GetQualifiedTypeName()
86{
87 Type * var_type = m_variable_sp->GetType();
88 if (var_type)
89 return var_type->GetQualifiedName();
90 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091}
92
Greg Claytonc7bece562013-01-25 18:06:21 +000093size_t
Siva Chandra9ac7a6c2015-10-21 19:28:08 +000094ValueObjectVariable::CalculateNumChildren(uint32_t max)
Sean Callanan72772842012-02-22 23:57:45 +000095{
Greg Clayton99558cc42015-08-24 23:46:31 +000096 CompilerType type(GetCompilerType());
Sean Callanan72772842012-02-22 23:57:45 +000097
98 if (!type.IsValid())
99 return 0;
100
101 const bool omit_empty_base_classes = true;
Siva Chandra9ac7a6c2015-10-21 19:28:08 +0000102 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
Greg Claytonfaac1112013-03-14 18:31:44 +0000106uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107ValueObjectVariable::GetByteSize()
108{
Enrico Granata951bdd52015-01-28 01:09:45 +0000109 ExecutionContext exe_ctx(GetExecutionContextRef());
110
Greg Clayton99558cc42015-08-24 23:46:31 +0000111 CompilerType type(GetCompilerType());
Sean Callanan72772842012-02-22 23:57:45 +0000112
113 if (!type.IsValid())
114 return 0;
115
Greg Clayton526ae042015-02-12 00:34:25 +0000116 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117}
118
119lldb::ValueType
120ValueObjectVariable::GetValueType() const
121{
122 if (m_variable_sp)
123 return m_variable_sp->GetScope();
124 return lldb::eValueTypeInvalid;
125}
126
Jim Ingham6035b672011-03-31 00:19:25 +0000127bool
128ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129{
130 SetValueIsValid (false);
131 m_error.Clear();
132
133 Variable *variable = m_variable_sp.get();
134 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000135
Greg Clayton007d5be2011-05-30 00:49:24 +0000136 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000137 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000138 // expr doesn't contain DWARF bytes, it contains the constant variable
139 // value bytes themselves...
140 if (expr.GetExpressionData(m_data))
141 m_value.SetContext(Value::eContextTypeVariable, variable);
142 else
143 m_error.SetErrorString ("empty constant data");
Enrico Granata82fabf82013-04-30 20:45:04 +0000144 // constant bytes can't be edited - sorry
145 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Greg Claytonf5fb4272010-09-18 04:00:06 +0000146 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000147 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000148 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000149 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000150 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton007d5be2011-05-30 00:49:24 +0000151
Greg Claytonc14ee322011-09-22 04:58:26 +0000152 Target *target = exe_ctx.GetTargetPtr();
153 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000154 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000155 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
156 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000157 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158
Greg Clayton007d5be2011-05-30 00:49:24 +0000159 if (expr.IsLocationList())
160 {
161 SymbolContext sc;
162 variable->CalculateSymbolContext (&sc);
163 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000164 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000165 }
166 Value old_value(m_value);
Tamas Berghammer5b42c7a2016-02-26 14:21:10 +0000167 if (expr.Evaluate (&exe_ctx,
168 nullptr,
169 nullptr,
170 nullptr,
171 loclist_base_load_addr,
172 nullptr,
173 nullptr,
174 m_value,
175 &m_error))
Greg Clayton007d5be2011-05-30 00:49:24 +0000176 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000177 m_resolved_value = m_value;
Greg Clayton007d5be2011-05-30 00:49:24 +0000178 m_value.SetContext(Value::eContextTypeVariable, variable);
Sean Callananc707f322013-10-09 02:32:37 +0000179
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000180 CompilerType compiler_type = GetCompilerType();
181 if (compiler_type.IsValid())
182 m_value.SetCompilerType(compiler_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183
Greg Clayton007d5be2011-05-30 00:49:24 +0000184 Value::ValueType value_type = m_value.GetValueType();
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000185
186 Process *process = exe_ctx.GetProcessPtr();
187 const bool process_is_alive = process && process->IsAlive();
Bruce Mitchener3ad353f2015-09-24 03:54:50 +0000188 const uint32_t type_info = compiler_type.GetTypeInfo();
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000189 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
190
Enrico Granata9128ee22011-09-06 19:20:51 +0000191 switch (value_type)
192 {
193 case Value::eValueTypeFileAddress:
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000194 // If this type is a pointer, then its children will be considered load addresses
195 // if the pointer or reference is dereferenced, but only if the process is alive.
196 //
197 // There could be global variables like in the following code:
198 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
199 // Foo g_foo1;
200 // Foo g_foo2;
201 // LinkedListNode g_second_node = { &g_foo2, NULL };
202 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
203 //
204 // When we aren't running, we should be able to look at these variables using
205 // the "target variable" command. Children of the "g_first_node" always will
206 // be of the same address type as the parent. But children of the "next" member of
207 // LinkedListNode will become load addresses if we have a live process, or remain
208 // what a file address if it what a file address.
209 if (process_is_alive && is_pointer_or_ref)
210 SetAddressTypeOfChildren(eAddressTypeLoad);
211 else
212 SetAddressTypeOfChildren(eAddressTypeFile);
Enrico Granata9128ee22011-09-06 19:20:51 +0000213 break;
214 case Value::eValueTypeHostAddress:
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000215 // Same as above for load addresses, except children of pointer or refs are always
216 // load addresses. Host addresses are used to store freeze dried variables. If this
217 // type is a struct, the entire struct contents will be copied into the heap of the
218 // LLDB process, but we do not currrently follow any pointers.
219 if (is_pointer_or_ref)
220 SetAddressTypeOfChildren(eAddressTypeLoad);
221 else
222 SetAddressTypeOfChildren(eAddressTypeHost);
Enrico Granata9128ee22011-09-06 19:20:51 +0000223 break;
224 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000225 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000226 case Value::eValueTypeVector:
Greg Claytonbdf31622011-10-01 01:53:20 +0000227 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000228 break;
229 }
Greg Claytona134cc12010-09-13 02:37:44 +0000230
Greg Clayton007d5be2011-05-30 00:49:24 +0000231 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000232 {
Enrico Granata68ae91c2013-05-20 22:58:35 +0000233 case Value::eValueTypeVector:
234 // fall through
Greg Clayton007d5be2011-05-30 00:49:24 +0000235 case Value::eValueTypeScalar:
236 // The variable value is in the Scalar value inside the m_value.
237 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000238 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000239 break;
240
241 case Value::eValueTypeFileAddress:
242 case Value::eValueTypeLoadAddress:
243 case Value::eValueTypeHostAddress:
244 // The DWARF expression result was an address in the inferior
245 // process. If this variable is an aggregate type, we just need
246 // the address as the main value as all child variable objects
247 // will rely upon this location and add an offset and then read
248 // their own values as needed. If this variable is a simple
249 // type, we read all data for it into m_data.
250 // Make sure this type has a value before we try and read it
251
252 // If we have a file address, convert it to a load address if we can.
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000253 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
Greg Claytona134cc12010-09-13 02:37:44 +0000254 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000255 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
256 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000257 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000258 SymbolContext var_sc;
259 variable->CalculateSymbolContext(&var_sc);
260 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000261 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000262 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
263 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000264 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000265 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000266 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000267 if (load_addr != LLDB_INVALID_ADDRESS)
268 {
269 m_value.SetValueType(Value::eValueTypeLoadAddress);
270 m_value.GetScalar() = load_addr;
271 }
Greg Claytona134cc12010-09-13 02:37:44 +0000272 }
273 }
274 }
275 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000276
Enrico Granatad07cfd32014-10-08 18:27:36 +0000277 if (!CanProvideValue())
Greg Clayton007d5be2011-05-30 00:49:24 +0000278 {
279 // this value object represents an aggregate type whose
280 // children have values, but this object does not. So we
281 // say we are changed if our location has changed.
282 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
283 }
284 else
285 {
286 // Copy the Value and set the context to use our Variable
287 // so it can extract read its value into m_data appropriately
288 Value value(m_value);
289 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton57ee3062013-07-11 22:46:58 +0000290 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
Enrico Granata0eb0ec22014-11-04 21:28:50 +0000291
292 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
Greg Clayton007d5be2011-05-30 00:49:24 +0000293 }
294 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000295 }
296
Greg Clayton007d5be2011-05-30 00:49:24 +0000297 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000299 else
300 {
301 // could not find location, won't allow editing
302 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
303 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 }
Jim Ingham6035b672011-03-31 00:19:25 +0000305 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
308
309
310bool
Jim Ingham6035b672011-03-31 00:19:25 +0000311ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000312{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000313 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
314 if (exe_ctx_ref.HasFrameRef())
315 {
316 ExecutionContext exe_ctx (exe_ctx_ref);
Jason Molendab57e4a12013-11-04 09:33:30 +0000317 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000318 if (frame)
319 {
320 return m_variable_sp->IsInScope (frame);
321 }
322 else
323 {
324 // This ValueObject had a frame at one time, but now we
325 // can't locate it, so return false since we probably aren't
326 // in scope.
327 return false;
328 }
329 }
330 // We have a variable that wasn't tied to a frame, which
331 // means it is a global and is always in scope.
332 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000333
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334}
335
Greg Claytone72dfb32012-02-24 01:59:29 +0000336lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000337ValueObjectVariable::GetModule()
338{
339 if (m_variable_sp)
340 {
341 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
342 if (sc_scope)
343 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000344 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000345 }
346 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000347 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000348}
349
Enrico Granata9128ee22011-09-06 19:20:51 +0000350SymbolContextScope *
351ValueObjectVariable::GetSymbolContextScope()
352{
353 if (m_variable_sp)
354 return m_variable_sp->GetSymbolContextScope();
355 return NULL;
356}
Greg Clayton81e871e2012-02-04 02:27:34 +0000357
358bool
359ValueObjectVariable::GetDeclaration (Declaration &decl)
360{
361 if (m_variable_sp)
362 {
363 decl = m_variable_sp->GetDeclaration();
364 return true;
365 }
366 return false;
367}
Enrico Granata82fabf82013-04-30 20:45:04 +0000368
369const char *
370ValueObjectVariable::GetLocationAsCString ()
371{
Enrico Granata3880c4c2013-05-03 23:28:47 +0000372 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
373 return GetLocationAsCStringImpl(m_resolved_value,
374 m_data);
375 else
376 return ValueObject::GetLocationAsCString();
Enrico Granata82fabf82013-04-30 20:45:04 +0000377}
378
379bool
380ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
381{
Sean Callanan6826d222014-01-18 01:13:50 +0000382 if (!UpdateValueIfNeeded())
383 {
384 error.SetErrorString("unable to update value before writing");
385 return false;
386 }
387
Enrico Granata82fabf82013-04-30 20:45:04 +0000388 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
389 {
390 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
391 ExecutionContext exe_ctx(GetExecutionContextRef());
392 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
393 RegisterValue reg_value;
394 if (!reg_info || !reg_ctx)
395 {
396 error.SetErrorString("unable to retrieve register info");
397 return false;
398 }
399 error = reg_value.SetValueFromCString(reg_info, value_str);
400 if (error.Fail())
401 return false;
402 if (reg_ctx->WriteRegister (reg_info, reg_value))
403 {
404 SetNeedsUpdate();
405 return true;
406 }
407 else
408 {
409 error.SetErrorString("unable to write back to register");
410 return false;
411 }
412 }
413 else
414 return ValueObject::SetValueFromCString(value_str, error);
415}
416
417bool
418ValueObjectVariable::SetData (DataExtractor &data, Error &error)
419{
Sean Callanan6826d222014-01-18 01:13:50 +0000420 if (!UpdateValueIfNeeded())
421 {
422 error.SetErrorString("unable to update value before writing");
423 return false;
424 }
425
Enrico Granata82fabf82013-04-30 20:45:04 +0000426 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
427 {
428 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
429 ExecutionContext exe_ctx(GetExecutionContextRef());
430 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
431 RegisterValue reg_value;
432 if (!reg_info || !reg_ctx)
433 {
434 error.SetErrorString("unable to retrieve register info");
435 return false;
436 }
Sean Callanan3d6ae762014-02-07 20:42:44 +0000437 error = reg_value.SetValueFromData(reg_info, data, 0, true);
Enrico Granata82fabf82013-04-30 20:45:04 +0000438 if (error.Fail())
439 return false;
440 if (reg_ctx->WriteRegister (reg_info, reg_value))
441 {
442 SetNeedsUpdate();
443 return true;
444 }
445 else
446 {
447 error.SetErrorString("unable to write back to register");
448 return false;
449 }
450 }
451 else
452 return ValueObject::SetData(data, error);
453}