blob: c86bece42eb899dfda1625011a4b8b8d1bada67a [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 Clayton57ee3062013-07-11 22:46:58 +000057ClangASTType
Sean Callanan72772842012-02-22 23:57:45 +000058ValueObjectVariable::GetClangTypeImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059{
60 Type *var_type = m_variable_sp->GetType();
61 if (var_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +000062 return var_type->GetClangForwardType();
Greg Clayton57ee3062013-07-11 22:46:58 +000063 return ClangASTType();
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)
80 return var_type->GetClangForwardType().GetDisplayTypeName();
81 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094ValueObjectVariable::CalculateNumChildren()
Sean Callanan72772842012-02-22 23:57:45 +000095{
Greg Clayton57ee3062013-07-11 22:46:58 +000096 ClangASTType type(GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +000097
98 if (!type.IsValid())
99 return 0;
100
101 const bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000102 return type.GetNumChildren(omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103}
104
Greg Claytonfaac1112013-03-14 18:31:44 +0000105uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106ValueObjectVariable::GetByteSize()
107{
Enrico Granata951bdd52015-01-28 01:09:45 +0000108 ExecutionContext exe_ctx(GetExecutionContextRef());
109
Greg Clayton57ee3062013-07-11 22:46:58 +0000110 ClangASTType type(GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +0000111
112 if (!type.IsValid())
113 return 0;
114
Greg Clayton526ae042015-02-12 00:34:25 +0000115 return type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116}
117
118lldb::ValueType
119ValueObjectVariable::GetValueType() const
120{
121 if (m_variable_sp)
122 return m_variable_sp->GetScope();
123 return lldb::eValueTypeInvalid;
124}
125
Jim Ingham6035b672011-03-31 00:19:25 +0000126bool
127ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128{
129 SetValueIsValid (false);
130 m_error.Clear();
131
132 Variable *variable = m_variable_sp.get();
133 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000134
Greg Clayton007d5be2011-05-30 00:49:24 +0000135 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000136 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000137 // expr doesn't contain DWARF bytes, it contains the constant variable
138 // value bytes themselves...
139 if (expr.GetExpressionData(m_data))
140 m_value.SetContext(Value::eContextTypeVariable, variable);
141 else
142 m_error.SetErrorString ("empty constant data");
Enrico Granata82fabf82013-04-30 20:45:04 +0000143 // constant bytes can't be edited - sorry
144 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Greg Claytonf5fb4272010-09-18 04:00:06 +0000145 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000146 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000147 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000148 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000149 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton007d5be2011-05-30 00:49:24 +0000150
Greg Claytonc14ee322011-09-22 04:58:26 +0000151 Target *target = exe_ctx.GetTargetPtr();
152 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000154 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
155 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000156 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157
Greg Clayton007d5be2011-05-30 00:49:24 +0000158 if (expr.IsLocationList())
159 {
160 SymbolContext sc;
161 variable->CalculateSymbolContext (&sc);
162 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000163 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000164 }
165 Value old_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000166 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
Greg Clayton007d5be2011-05-30 00:49:24 +0000167 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000168 m_resolved_value = m_value;
Greg Clayton007d5be2011-05-30 00:49:24 +0000169 m_value.SetContext(Value::eContextTypeVariable, variable);
Sean Callananc707f322013-10-09 02:32:37 +0000170
171 ClangASTType clang_type = GetClangType();
172 if (clang_type.IsValid())
173 m_value.SetClangType(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
Greg Clayton007d5be2011-05-30 00:49:24 +0000175 Value::ValueType value_type = m_value.GetValueType();
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000176
177 Process *process = exe_ctx.GetProcessPtr();
178 const bool process_is_alive = process && process->IsAlive();
179 const uint32_t type_info = clang_type.GetTypeInfo();
180 const bool is_pointer_or_ref = (type_info & (lldb::eTypeIsPointer | lldb::eTypeIsReference)) != 0;
181
Enrico Granata9128ee22011-09-06 19:20:51 +0000182 switch (value_type)
183 {
184 case Value::eValueTypeFileAddress:
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000185 // If this type is a pointer, then its children will be considered load addresses
186 // if the pointer or reference is dereferenced, but only if the process is alive.
187 //
188 // There could be global variables like in the following code:
189 // struct LinkedListNode { Foo* foo; LinkedListNode* next; };
190 // Foo g_foo1;
191 // Foo g_foo2;
192 // LinkedListNode g_second_node = { &g_foo2, NULL };
193 // LinkedListNode g_first_node = { &g_foo1, &g_second_node };
194 //
195 // When we aren't running, we should be able to look at these variables using
196 // the "target variable" command. Children of the "g_first_node" always will
197 // be of the same address type as the parent. But children of the "next" member of
198 // LinkedListNode will become load addresses if we have a live process, 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);
Enrico Granata9128ee22011-09-06 19:20:51 +0000204 break;
205 case Value::eValueTypeHostAddress:
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000206 // Same as above for load addresses, except children of pointer or refs are always
207 // load addresses. Host addresses are used to store freeze dried variables. If this
208 // type is a struct, the entire struct contents will be copied into the heap of the
209 // LLDB process, but we do not currrently follow any pointers.
210 if (is_pointer_or_ref)
211 SetAddressTypeOfChildren(eAddressTypeLoad);
212 else
213 SetAddressTypeOfChildren(eAddressTypeHost);
Enrico Granata9128ee22011-09-06 19:20:51 +0000214 break;
215 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000216 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000217 case Value::eValueTypeVector:
Greg Claytonbdf31622011-10-01 01:53:20 +0000218 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000219 break;
220 }
Greg Claytona134cc12010-09-13 02:37:44 +0000221
Greg Clayton007d5be2011-05-30 00:49:24 +0000222 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000223 {
Enrico Granata68ae91c2013-05-20 22:58:35 +0000224 case Value::eValueTypeVector:
225 // fall through
Greg Clayton007d5be2011-05-30 00:49:24 +0000226 case Value::eValueTypeScalar:
227 // The variable value is in the Scalar value inside the m_value.
228 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000229 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000230 break;
231
232 case Value::eValueTypeFileAddress:
233 case Value::eValueTypeLoadAddress:
234 case Value::eValueTypeHostAddress:
235 // The DWARF expression result was an address in the inferior
236 // process. If this variable is an aggregate type, we just need
237 // the address as the main value as all child variable objects
238 // will rely upon this location and add an offset and then read
239 // their own values as needed. If this variable is a simple
240 // type, we read all data for it into m_data.
241 // Make sure this type has a value before we try and read it
242
243 // If we have a file address, convert it to a load address if we can.
Greg Clayton3a95b5b2014-12-19 01:28:42 +0000244 if (value_type == Value::eValueTypeFileAddress && process_is_alive)
Greg Claytona134cc12010-09-13 02:37:44 +0000245 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000246 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
247 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000248 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000249 SymbolContext var_sc;
250 variable->CalculateSymbolContext(&var_sc);
251 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000252 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000253 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
254 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000255 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000256 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000257 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000258 if (load_addr != LLDB_INVALID_ADDRESS)
259 {
260 m_value.SetValueType(Value::eValueTypeLoadAddress);
261 m_value.GetScalar() = load_addr;
262 }
Greg Claytona134cc12010-09-13 02:37:44 +0000263 }
264 }
265 }
266 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000267
Enrico Granatad07cfd32014-10-08 18:27:36 +0000268 if (!CanProvideValue())
Greg Clayton007d5be2011-05-30 00:49:24 +0000269 {
270 // this value object represents an aggregate type whose
271 // children have values, but this object does not. So we
272 // say we are changed if our location has changed.
273 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
274 }
275 else
276 {
277 // Copy the Value and set the context to use our Variable
278 // so it can extract read its value into m_data appropriately
279 Value value(m_value);
280 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton57ee3062013-07-11 22:46:58 +0000281 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
Enrico Granata0eb0ec22014-11-04 21:28:50 +0000282
283 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
Greg Clayton007d5be2011-05-30 00:49:24 +0000284 }
285 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000286 }
287
Greg Clayton007d5be2011-05-30 00:49:24 +0000288 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000290 else
291 {
292 // could not find location, won't allow editing
293 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
294 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 }
Jim Ingham6035b672011-03-31 00:19:25 +0000296 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297}
298
299
300
301bool
Jim Ingham6035b672011-03-31 00:19:25 +0000302ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000304 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
305 if (exe_ctx_ref.HasFrameRef())
306 {
307 ExecutionContext exe_ctx (exe_ctx_ref);
Jason Molendab57e4a12013-11-04 09:33:30 +0000308 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000309 if (frame)
310 {
311 return m_variable_sp->IsInScope (frame);
312 }
313 else
314 {
315 // This ValueObject had a frame at one time, but now we
316 // can't locate it, so return false since we probably aren't
317 // in scope.
318 return false;
319 }
320 }
321 // We have a variable that wasn't tied to a frame, which
322 // means it is a global and is always in scope.
323 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000324
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325}
326
Greg Claytone72dfb32012-02-24 01:59:29 +0000327lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000328ValueObjectVariable::GetModule()
329{
330 if (m_variable_sp)
331 {
332 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
333 if (sc_scope)
334 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000335 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000336 }
337 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000338 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000339}
340
Enrico Granata9128ee22011-09-06 19:20:51 +0000341SymbolContextScope *
342ValueObjectVariable::GetSymbolContextScope()
343{
344 if (m_variable_sp)
345 return m_variable_sp->GetSymbolContextScope();
346 return NULL;
347}
Greg Clayton81e871e2012-02-04 02:27:34 +0000348
349bool
350ValueObjectVariable::GetDeclaration (Declaration &decl)
351{
352 if (m_variable_sp)
353 {
354 decl = m_variable_sp->GetDeclaration();
355 return true;
356 }
357 return false;
358}
Enrico Granata82fabf82013-04-30 20:45:04 +0000359
360const char *
361ValueObjectVariable::GetLocationAsCString ()
362{
Enrico Granata3880c4c2013-05-03 23:28:47 +0000363 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
364 return GetLocationAsCStringImpl(m_resolved_value,
365 m_data);
366 else
367 return ValueObject::GetLocationAsCString();
Enrico Granata82fabf82013-04-30 20:45:04 +0000368}
369
370bool
371ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
372{
Sean Callanan6826d222014-01-18 01:13:50 +0000373 if (!UpdateValueIfNeeded())
374 {
375 error.SetErrorString("unable to update value before writing");
376 return false;
377 }
378
Enrico Granata82fabf82013-04-30 20:45:04 +0000379 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
380 {
381 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
382 ExecutionContext exe_ctx(GetExecutionContextRef());
383 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
384 RegisterValue reg_value;
385 if (!reg_info || !reg_ctx)
386 {
387 error.SetErrorString("unable to retrieve register info");
388 return false;
389 }
390 error = reg_value.SetValueFromCString(reg_info, value_str);
391 if (error.Fail())
392 return false;
393 if (reg_ctx->WriteRegister (reg_info, reg_value))
394 {
395 SetNeedsUpdate();
396 return true;
397 }
398 else
399 {
400 error.SetErrorString("unable to write back to register");
401 return false;
402 }
403 }
404 else
405 return ValueObject::SetValueFromCString(value_str, error);
406}
407
408bool
409ValueObjectVariable::SetData (DataExtractor &data, Error &error)
410{
Sean Callanan6826d222014-01-18 01:13:50 +0000411 if (!UpdateValueIfNeeded())
412 {
413 error.SetErrorString("unable to update value before writing");
414 return false;
415 }
416
Enrico Granata82fabf82013-04-30 20:45:04 +0000417 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
418 {
419 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
420 ExecutionContext exe_ctx(GetExecutionContextRef());
421 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
422 RegisterValue reg_value;
423 if (!reg_info || !reg_ctx)
424 {
425 error.SetErrorString("unable to retrieve register info");
426 return false;
427 }
Sean Callanan3d6ae762014-02-07 20:42:44 +0000428 error = reg_value.SetValueFromData(reg_info, data, 0, true);
Enrico Granata82fabf82013-04-30 20:45:04 +0000429 if (error.Fail())
430 return false;
431 if (reg_ctx->WriteRegister (reg_info, reg_value))
432 {
433 SetNeedsUpdate();
434 return true;
435 }
436 else
437 {
438 error.SetErrorString("unable to write back to register");
439 return false;
440 }
441 }
442 else
443 return ValueObject::SetData(data, error);
444}