blob: 225dc02c8addbdcebe413d15ca10daa896a8715c [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
Greg Clayton57ee3062013-07-11 22:46:58 +0000237 if (GetClangType().IsAggregateType())
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());
Greg Clayton007d5be2011-05-30 00:49:24 +0000251 }
252 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000253 }
254
Greg Clayton007d5be2011-05-30 00:49:24 +0000255 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000257 else
258 {
259 // could not find location, won't allow editing
260 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
261 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262 }
Jim Ingham6035b672011-03-31 00:19:25 +0000263 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000264}
265
266
267
268bool
Jim Ingham6035b672011-03-31 00:19:25 +0000269ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000270{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000271 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
272 if (exe_ctx_ref.HasFrameRef())
273 {
274 ExecutionContext exe_ctx (exe_ctx_ref);
Jason Molendab57e4a12013-11-04 09:33:30 +0000275 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000276 if (frame)
277 {
278 return m_variable_sp->IsInScope (frame);
279 }
280 else
281 {
282 // This ValueObject had a frame at one time, but now we
283 // can't locate it, so return false since we probably aren't
284 // in scope.
285 return false;
286 }
287 }
288 // We have a variable that wasn't tied to a frame, which
289 // means it is a global and is always in scope.
290 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000291
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000292}
293
Greg Claytone72dfb32012-02-24 01:59:29 +0000294lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000295ValueObjectVariable::GetModule()
296{
297 if (m_variable_sp)
298 {
299 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
300 if (sc_scope)
301 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000302 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000303 }
304 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000305 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000306}
307
Enrico Granata9128ee22011-09-06 19:20:51 +0000308SymbolContextScope *
309ValueObjectVariable::GetSymbolContextScope()
310{
311 if (m_variable_sp)
312 return m_variable_sp->GetSymbolContextScope();
313 return NULL;
314}
Greg Clayton81e871e2012-02-04 02:27:34 +0000315
316bool
317ValueObjectVariable::GetDeclaration (Declaration &decl)
318{
319 if (m_variable_sp)
320 {
321 decl = m_variable_sp->GetDeclaration();
322 return true;
323 }
324 return false;
325}
Enrico Granata82fabf82013-04-30 20:45:04 +0000326
327const char *
328ValueObjectVariable::GetLocationAsCString ()
329{
Enrico Granata3880c4c2013-05-03 23:28:47 +0000330 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
331 return GetLocationAsCStringImpl(m_resolved_value,
332 m_data);
333 else
334 return ValueObject::GetLocationAsCString();
Enrico Granata82fabf82013-04-30 20:45:04 +0000335}
336
337bool
338ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
339{
Sean Callanan6826d222014-01-18 01:13:50 +0000340 if (!UpdateValueIfNeeded())
341 {
342 error.SetErrorString("unable to update value before writing");
343 return false;
344 }
345
Enrico Granata82fabf82013-04-30 20:45:04 +0000346 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
347 {
348 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
349 ExecutionContext exe_ctx(GetExecutionContextRef());
350 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
351 RegisterValue reg_value;
352 if (!reg_info || !reg_ctx)
353 {
354 error.SetErrorString("unable to retrieve register info");
355 return false;
356 }
357 error = reg_value.SetValueFromCString(reg_info, value_str);
358 if (error.Fail())
359 return false;
360 if (reg_ctx->WriteRegister (reg_info, reg_value))
361 {
362 SetNeedsUpdate();
363 return true;
364 }
365 else
366 {
367 error.SetErrorString("unable to write back to register");
368 return false;
369 }
370 }
371 else
372 return ValueObject::SetValueFromCString(value_str, error);
373}
374
375bool
376ValueObjectVariable::SetData (DataExtractor &data, Error &error)
377{
Sean Callanan6826d222014-01-18 01:13:50 +0000378 if (!UpdateValueIfNeeded())
379 {
380 error.SetErrorString("unable to update value before writing");
381 return false;
382 }
383
Enrico Granata82fabf82013-04-30 20:45:04 +0000384 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
385 {
386 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
387 ExecutionContext exe_ctx(GetExecutionContextRef());
388 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
389 RegisterValue reg_value;
390 if (!reg_info || !reg_ctx)
391 {
392 error.SetErrorString("unable to retrieve register info");
393 return false;
394 }
Sean Callanan3d6ae762014-02-07 20:42:44 +0000395 error = reg_value.SetValueFromData(reg_info, data, 0, true);
Enrico Granata82fabf82013-04-30 20:45:04 +0000396 if (error.Fail())
397 return false;
398 if (reg_ctx->WriteRegister (reg_info, reg_value))
399 {
400 SetNeedsUpdate();
401 return true;
402 }
403 else
404 {
405 error.SetErrorString("unable to write back to register");
406 return false;
407 }
408 }
409 else
410 return ValueObject::SetData(data, error);
411}