blob: f0e88e62c0f5b3a347220fdb095694f03a78f958 [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
76ValueObjectVariable::GetQualifiedTypeName()
77{
78 Type * var_type = m_variable_sp->GetType();
79 if (var_type)
80 return var_type->GetQualifiedName();
81 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
Greg Claytonc7bece562013-01-25 18:06:21 +000084size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000085ValueObjectVariable::CalculateNumChildren()
Sean Callanan72772842012-02-22 23:57:45 +000086{
Greg Clayton57ee3062013-07-11 22:46:58 +000087 ClangASTType type(GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +000088
89 if (!type.IsValid())
90 return 0;
91
92 const bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +000093 return type.GetNumChildren(omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
Greg Claytonfaac1112013-03-14 18:31:44 +000096uint64_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097ValueObjectVariable::GetByteSize()
98{
Greg Clayton57ee3062013-07-11 22:46:58 +000099 ClangASTType type(GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +0000100
101 if (!type.IsValid())
102 return 0;
103
Greg Clayton57ee3062013-07-11 22:46:58 +0000104 return type.GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000105}
106
107lldb::ValueType
108ValueObjectVariable::GetValueType() const
109{
110 if (m_variable_sp)
111 return m_variable_sp->GetScope();
112 return lldb::eValueTypeInvalid;
113}
114
Jim Ingham6035b672011-03-31 00:19:25 +0000115bool
116ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117{
118 SetValueIsValid (false);
119 m_error.Clear();
120
121 Variable *variable = m_variable_sp.get();
122 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000123
Greg Clayton007d5be2011-05-30 00:49:24 +0000124 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000125 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000126 // expr doesn't contain DWARF bytes, it contains the constant variable
127 // value bytes themselves...
128 if (expr.GetExpressionData(m_data))
129 m_value.SetContext(Value::eContextTypeVariable, variable);
130 else
131 m_error.SetErrorString ("empty constant data");
Enrico Granata82fabf82013-04-30 20:45:04 +0000132 // constant bytes can't be edited - sorry
133 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
Greg Claytonf5fb4272010-09-18 04:00:06 +0000134 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000135 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000136 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000137 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000138 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton007d5be2011-05-30 00:49:24 +0000139
Greg Claytonc14ee322011-09-22 04:58:26 +0000140 Target *target = exe_ctx.GetTargetPtr();
141 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000143 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
144 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000145 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Greg Clayton007d5be2011-05-30 00:49:24 +0000147 if (expr.IsLocationList())
148 {
149 SymbolContext sc;
150 variable->CalculateSymbolContext (&sc);
151 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000152 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000153 }
154 Value old_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000155 if (expr.Evaluate (&exe_ctx, NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
Greg Clayton007d5be2011-05-30 00:49:24 +0000156 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000157 m_resolved_value = m_value;
Greg Clayton007d5be2011-05-30 00:49:24 +0000158 m_value.SetContext(Value::eContextTypeVariable, variable);
Sean Callananc707f322013-10-09 02:32:37 +0000159
160 ClangASTType clang_type = GetClangType();
161 if (clang_type.IsValid())
162 m_value.SetClangType(clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163
Greg Clayton007d5be2011-05-30 00:49:24 +0000164 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata9128ee22011-09-06 19:20:51 +0000165
166 switch (value_type)
167 {
168 case Value::eValueTypeFileAddress:
169 SetAddressTypeOfChildren(eAddressTypeFile);
170 break;
171 case Value::eValueTypeHostAddress:
172 SetAddressTypeOfChildren(eAddressTypeHost);
173 break;
174 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000175 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000176 case Value::eValueTypeVector:
Greg Claytonbdf31622011-10-01 01:53:20 +0000177 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000178 break;
179 }
Greg Claytona134cc12010-09-13 02:37:44 +0000180
Greg Clayton007d5be2011-05-30 00:49:24 +0000181 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000182 {
Enrico Granata68ae91c2013-05-20 22:58:35 +0000183 case Value::eValueTypeVector:
184 // fall through
Greg Clayton007d5be2011-05-30 00:49:24 +0000185 case Value::eValueTypeScalar:
186 // The variable value is in the Scalar value inside the m_value.
187 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000188 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000189 break;
190
191 case Value::eValueTypeFileAddress:
192 case Value::eValueTypeLoadAddress:
193 case Value::eValueTypeHostAddress:
194 // The DWARF expression result was an address in the inferior
195 // process. If this variable is an aggregate type, we just need
196 // the address as the main value as all child variable objects
197 // will rely upon this location and add an offset and then read
198 // their own values as needed. If this variable is a simple
199 // type, we read all data for it into m_data.
200 // Make sure this type has a value before we try and read it
201
202 // If we have a file address, convert it to a load address if we can.
Greg Claytonc14ee322011-09-22 04:58:26 +0000203 Process *process = exe_ctx.GetProcessPtr();
204 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000205 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000206 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
207 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000208 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000209 SymbolContext var_sc;
210 variable->CalculateSymbolContext(&var_sc);
211 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000212 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000213 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
214 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000215 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000216 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000217 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000218 if (load_addr != LLDB_INVALID_ADDRESS)
219 {
220 m_value.SetValueType(Value::eValueTypeLoadAddress);
221 m_value.GetScalar() = load_addr;
222 }
Greg Claytona134cc12010-09-13 02:37:44 +0000223 }
224 }
225 }
226 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000227
Greg Clayton57ee3062013-07-11 22:46:58 +0000228 if (GetClangType().IsAggregateType())
Greg Clayton007d5be2011-05-30 00:49:24 +0000229 {
230 // this value object represents an aggregate type whose
231 // children have values, but this object does not. So we
232 // say we are changed if our location has changed.
233 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
234 }
235 else
236 {
237 // Copy the Value and set the context to use our Variable
238 // so it can extract read its value into m_data appropriately
239 Value value(m_value);
240 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton57ee3062013-07-11 22:46:58 +0000241 m_error = value.GetValueAsData(&exe_ctx, m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000242 }
243 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000244 }
245
Greg Clayton007d5be2011-05-30 00:49:24 +0000246 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000248 else
249 {
250 // could not find location, won't allow editing
251 m_resolved_value.SetContext(Value::eContextTypeInvalid, NULL);
252 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253 }
Jim Ingham6035b672011-03-31 00:19:25 +0000254 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255}
256
257
258
259bool
Jim Ingham6035b672011-03-31 00:19:25 +0000260ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000262 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
263 if (exe_ctx_ref.HasFrameRef())
264 {
265 ExecutionContext exe_ctx (exe_ctx_ref);
Jason Molendaf23bf742013-11-02 02:23:02 +0000266 Frame *frame = exe_ctx.GetFramePtr();
Greg Claytoncc4d0142012-02-17 07:49:44 +0000267 if (frame)
268 {
269 return m_variable_sp->IsInScope (frame);
270 }
271 else
272 {
273 // This ValueObject had a frame at one time, but now we
274 // can't locate it, so return false since we probably aren't
275 // in scope.
276 return false;
277 }
278 }
279 // We have a variable that wasn't tied to a frame, which
280 // means it is a global and is always in scope.
281 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000282
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000283}
284
Greg Claytone72dfb32012-02-24 01:59:29 +0000285lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000286ValueObjectVariable::GetModule()
287{
288 if (m_variable_sp)
289 {
290 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
291 if (sc_scope)
292 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000293 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000294 }
295 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000296 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000297}
298
Enrico Granata9128ee22011-09-06 19:20:51 +0000299SymbolContextScope *
300ValueObjectVariable::GetSymbolContextScope()
301{
302 if (m_variable_sp)
303 return m_variable_sp->GetSymbolContextScope();
304 return NULL;
305}
Greg Clayton81e871e2012-02-04 02:27:34 +0000306
307bool
308ValueObjectVariable::GetDeclaration (Declaration &decl)
309{
310 if (m_variable_sp)
311 {
312 decl = m_variable_sp->GetDeclaration();
313 return true;
314 }
315 return false;
316}
Enrico Granata82fabf82013-04-30 20:45:04 +0000317
318const char *
319ValueObjectVariable::GetLocationAsCString ()
320{
Enrico Granata3880c4c2013-05-03 23:28:47 +0000321 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
322 return GetLocationAsCStringImpl(m_resolved_value,
323 m_data);
324 else
325 return ValueObject::GetLocationAsCString();
Enrico Granata82fabf82013-04-30 20:45:04 +0000326}
327
328bool
329ValueObjectVariable::SetValueFromCString (const char *value_str, Error& error)
330{
331 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
332 {
333 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
334 ExecutionContext exe_ctx(GetExecutionContextRef());
335 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
336 RegisterValue reg_value;
337 if (!reg_info || !reg_ctx)
338 {
339 error.SetErrorString("unable to retrieve register info");
340 return false;
341 }
342 error = reg_value.SetValueFromCString(reg_info, value_str);
343 if (error.Fail())
344 return false;
345 if (reg_ctx->WriteRegister (reg_info, reg_value))
346 {
347 SetNeedsUpdate();
348 return true;
349 }
350 else
351 {
352 error.SetErrorString("unable to write back to register");
353 return false;
354 }
355 }
356 else
357 return ValueObject::SetValueFromCString(value_str, error);
358}
359
360bool
361ValueObjectVariable::SetData (DataExtractor &data, Error &error)
362{
363 if (m_resolved_value.GetContextType() == Value::eContextTypeRegisterInfo)
364 {
365 RegisterInfo *reg_info = m_resolved_value.GetRegisterInfo();
366 ExecutionContext exe_ctx(GetExecutionContextRef());
367 RegisterContext *reg_ctx = exe_ctx.GetRegisterContext();
368 RegisterValue reg_value;
369 if (!reg_info || !reg_ctx)
370 {
371 error.SetErrorString("unable to retrieve register info");
372 return false;
373 }
374 error = reg_value.SetValueFromData(reg_info, data, 0, false);
375 if (error.Fail())
376 return false;
377 if (reg_ctx->WriteRegister (reg_info, reg_value))
378 {
379 SetNeedsUpdate();
380 return true;
381 }
382 else
383 {
384 error.SetErrorString("unable to write back to register");
385 return false;
386 }
387 }
388 else
389 return ValueObject::SetData(data, error);
390}