blob: ddff1a2006c46f2153b9f5e206e930cc934c4202 [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{
Greg Clayton57ee3062013-07-11 22:46:58 +0000108 ClangASTType type(GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +0000109
110 if (!type.IsValid())
111 return 0;
112
Greg Clayton57ee3062013-07-11 22:46:58 +0000113 return type.GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114}
115
116lldb::ValueType
117ValueObjectVariable::GetValueType() const
118{
119 if (m_variable_sp)
120 return m_variable_sp->GetScope();
121 return lldb::eValueTypeInvalid;
122}
123
Jim Ingham6035b672011-03-31 00:19:25 +0000124bool
125ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126{
127 SetValueIsValid (false);
128 m_error.Clear();
129
130 Variable *variable = m_variable_sp.get();
131 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000132
Greg Clayton007d5be2011-05-30 00:49:24 +0000133 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000134 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000135 // expr doesn't contain DWARF bytes, it contains the constant variable
136 // value bytes themselves...
137 if (expr.GetExpressionData(m_data))
138 m_value.SetContext(Value::eContextTypeVariable, variable);
139 else
140 m_error.SetErrorString ("empty constant data");
Enrico Granata82fabf82013-04-30 20:45:04 +0000141 // constant bytes can't be edited - sorry
142 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Greg Claytonf5fb4272010-09-18 04:00:06 +0000143 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000144 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000145 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000146 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000147 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton007d5be2011-05-30 00:49:24 +0000148
Greg Claytonc14ee322011-09-22 04:58:26 +0000149 Target *target = exe_ctx.GetTargetPtr();
150 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000152 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
153 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000154 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155
Greg Clayton007d5be2011-05-30 00:49:24 +0000156 if (expr.IsLocationList())
157 {
158 SymbolContext sc;
159 variable->CalculateSymbolContext (&sc);
160 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000161 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000162 }
163 Value old_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000164 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
Greg Clayton007d5be2011-05-30 00:49:24 +0000165 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000166 m_resolved_value = m_value;
Greg Clayton007d5be2011-05-30 00:49:24 +0000167 m_value.SetContext(Value::eContextTypeVariable, variable);
Sean Callananc707f322013-10-09 02:32:37 +0000168
169 ClangASTType clang_type = GetClangType();
170 if (clang_type.IsValid())
171 m_value.SetClangType(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000172
Greg Clayton007d5be2011-05-30 00:49:24 +0000173 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata9128ee22011-09-06 19:20:51 +0000174
175 switch (value_type)
176 {
177 case Value::eValueTypeFileAddress:
178 SetAddressTypeOfChildren(eAddressTypeFile);
179 break;
180 case Value::eValueTypeHostAddress:
181 SetAddressTypeOfChildren(eAddressTypeHost);
182 break;
183 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000184 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000185 case Value::eValueTypeVector:
Greg Claytonbdf31622011-10-01 01:53:20 +0000186 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000187 break;
188 }
Greg Claytona134cc12010-09-13 02:37:44 +0000189
Greg Clayton007d5be2011-05-30 00:49:24 +0000190 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000191 {
Enrico Granata68ae91c2013-05-20 22:58:35 +0000192 case Value::eValueTypeVector:
193 // fall through
Greg Clayton007d5be2011-05-30 00:49:24 +0000194 case Value::eValueTypeScalar:
195 // The variable value is in the Scalar value inside the m_value.
196 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000197 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000198 break;
199
200 case Value::eValueTypeFileAddress:
201 case Value::eValueTypeLoadAddress:
202 case Value::eValueTypeHostAddress:
203 // The DWARF expression result was an address in the inferior
204 // process. If this variable is an aggregate type, we just need
205 // the address as the main value as all child variable objects
206 // will rely upon this location and add an offset and then read
207 // their own values as needed. If this variable is a simple
208 // type, we read all data for it into m_data.
209 // Make sure this type has a value before we try and read it
210
211 // If we have a file address, convert it to a load address if we can.
Greg Claytonc14ee322011-09-22 04:58:26 +0000212 Process *process = exe_ctx.GetProcessPtr();
213 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000214 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000215 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
216 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000217 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000218 SymbolContext var_sc;
219 variable->CalculateSymbolContext(&var_sc);
220 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000221 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000222 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
223 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000224 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000225 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000226 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000227 if (load_addr != LLDB_INVALID_ADDRESS)
228 {
229 m_value.SetValueType(Value::eValueTypeLoadAddress);
230 m_value.GetScalar() = load_addr;
231 }
Greg Claytona134cc12010-09-13 02:37:44 +0000232 }
233 }
234 }
235 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000236
Enrico Granatad07cfd32014-10-08 18:27:36 +0000237 if (!CanProvideValue())
Greg Clayton007d5be2011-05-30 00:49:24 +0000238 {
239 // this value object represents an aggregate type whose
240 // children have values, but this object does not. So we
241 // say we are changed if our location has changed.
242 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
243 }
244 else
245 {
246 // Copy the Value and set the context to use our Variable
247 // so it can extract read its value into m_data appropriately
248 Value value(m_value);
249 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton57ee3062013-07-11 22:46:58 +0000250 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
Enrico Granata0eb0ec22014-11-04 21:28:50 +0000251
252 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
Greg Clayton007d5be2011-05-30 00:49:24 +0000253 }
254 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000255 }
256
Greg Clayton007d5be2011-05-30 00:49:24 +0000257 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000259 else
260 {
261 // could not find location, won't allow editing
262 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
263 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264 }
Jim Ingham6035b672011-03-31 00:19:25 +0000265 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000266}
267
268
269
270bool
Jim Ingham6035b672011-03-31 00:19:25 +0000271ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000273 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
274 if (exe_ctx_ref.HasFrameRef())
275 {
276 ExecutionContext exe_ctx (exe_ctx_ref);
Jason Molendab57e4a12013-11-04 09:33:30 +0000277 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000278 if (frame)
279 {
280 return m_variable_sp->IsInScope (frame);
281 }
282 else
283 {
284 // This ValueObject had a frame at one time, but now we
285 // can't locate it, so return false since we probably aren't
286 // in scope.
287 return false;
288 }
289 }
290 // We have a variable that wasn't tied to a frame, which
291 // means it is a global and is always in scope.
292 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000293
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000294}
295
Greg Claytone72dfb32012-02-24 01:59:29 +0000296lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000297ValueObjectVariable::GetModule()
298{
299 if (m_variable_sp)
300 {
301 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
302 if (sc_scope)
303 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000304 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000305 }
306 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000307 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000308}
309
Enrico Granata9128ee22011-09-06 19:20:51 +0000310SymbolContextScope *
311ValueObjectVariable::GetSymbolContextScope()
312{
313 if (m_variable_sp)
314 return m_variable_sp->GetSymbolContextScope();
315 return NULL;
316}
Greg Clayton81e871e2012-02-04 02:27:34 +0000317
318bool
319ValueObjectVariable::GetDeclaration (Declaration &decl)
320{
321 if (m_variable_sp)
322 {
323 decl = m_variable_sp->GetDeclaration();
324 return true;
325 }
326 return false;
327}
Enrico Granata82fabf82013-04-30 20:45:04 +0000328
329const char *
330ValueObjectVariable::GetLocationAsCString ()
331{
Enrico Granata3880c4c2013-05-03 23:28:47 +0000332 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
333 return GetLocationAsCStringImpl(m_resolved_value,
334 m_data);
335 else
336 return ValueObject::GetLocationAsCString();
Enrico Granata82fabf82013-04-30 20:45:04 +0000337}
338
339bool
340ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
341{
Sean Callanan6826d222014-01-18 01:13:50 +0000342 if (!UpdateValueIfNeeded())
343 {
344 error.SetErrorString("unable to update value before writing");
345 return false;
346 }
347
Enrico Granata82fabf82013-04-30 20:45:04 +0000348 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
349 {
350 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
351 ExecutionContext exe_ctx(GetExecutionContextRef());
352 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
353 RegisterValue reg_value;
354 if (!reg_info || !reg_ctx)
355 {
356 error.SetErrorString("unable to retrieve register info");
357 return false;
358 }
359 error = reg_value.SetValueFromCString(reg_info, value_str);
360 if (error.Fail())
361 return false;
362 if (reg_ctx->WriteRegister (reg_info, reg_value))
363 {
364 SetNeedsUpdate();
365 return true;
366 }
367 else
368 {
369 error.SetErrorString("unable to write back to register");
370 return false;
371 }
372 }
373 else
374 return ValueObject::SetValueFromCString(value_str, error);
375}
376
377bool
378ValueObjectVariable::SetData (DataExtractor &data, Error &error)
379{
Sean Callanan6826d222014-01-18 01:13:50 +0000380 if (!UpdateValueIfNeeded())
381 {
382 error.SetErrorString("unable to update value before writing");
383 return false;
384 }
385
Enrico Granata82fabf82013-04-30 20:45:04 +0000386 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
387 {
388 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
389 ExecutionContext exe_ctx(GetExecutionContextRef());
390 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
391 RegisterValue reg_value;
392 if (!reg_info || !reg_ctx)
393 {
394 error.SetErrorString("unable to retrieve register info");
395 return false;
396 }
Sean Callanan3d6ae762014-02-07 20:42:44 +0000397 error = reg_value.SetValueFromData(reg_info, data, 0, true);
Enrico Granata82fabf82013-04-30 20:45:04 +0000398 if (error.Fail())
399 return false;
400 if (reg_ctx->WriteRegister (reg_info, reg_value))
401 {
402 SetNeedsUpdate();
403 return true;
404 }
405 else
406 {
407 error.SetErrorString("unable to write back to register");
408 return false;
409 }
410 }
411 else
412 return ValueObject::SetData(data, error);
413}