blob: 164592d10b981b284919571a620fff75c7e80af3 [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"
18#include "lldb/Core/ValueObjectList.h"
19#include "lldb/Core/Value.h"
20
Greg Clayton1f746072012-08-29 21:13:06 +000021#include "lldb/Symbol/Function.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000022#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
Greg Clayton644247c2011-07-07 01:59:51 +000024#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Symbol/Type.h"
26#include "lldb/Symbol/Variable.h"
27
28#include "lldb/Target/ExecutionContext.h"
29#include "lldb/Target/Process.h"
30#include "lldb/Target/RegisterContext.h"
31#include "lldb/Target/Target.h"
32#include "lldb/Target/Thread.h"
33
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034
35using namespace lldb_private;
36
Jim Ingham58b59f92011-04-22 23:53:53 +000037lldb::ValueObjectSP
38ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
39{
40 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
41}
42
Jim Ingham6035b672011-03-31 00:19:25 +000043ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
44 ValueObject(exe_scope),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045 m_variable_sp(var_sp)
46{
47 // Do not attempt to construct one of these objects with no variable!
48 assert (m_variable_sp.get() != NULL);
49 m_name = var_sp->GetName();
50}
51
52ValueObjectVariable::~ValueObjectVariable()
53{
54}
55
Greg Clayton6beaaa62011-01-17 03:46:26 +000056lldb::clang_type_t
Sean Callanan72772842012-02-22 23:57:45 +000057ValueObjectVariable::GetClangTypeImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058{
59 Type *var_type = m_variable_sp->GetType();
60 if (var_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +000061 return var_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 return NULL;
63}
64
65ConstString
66ValueObjectVariable::GetTypeName()
67{
68 Type * var_type = m_variable_sp->GetType();
69 if (var_type)
70 return var_type->GetName();
Greg Clayton84db9102012-03-26 23:03:23 +000071 return ConstString();
72}
73
74ConstString
75ValueObjectVariable::GetQualifiedTypeName()
76{
77 Type * var_type = m_variable_sp->GetType();
78 if (var_type)
79 return var_type->GetQualifiedName();
80 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000081}
82
Greg Claytonc7bece562013-01-25 18:06:21 +000083size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084ValueObjectVariable::CalculateNumChildren()
Sean Callanan72772842012-02-22 23:57:45 +000085{
86 ClangASTType type(GetClangAST(),
87 GetClangType());
88
89 if (!type.IsValid())
90 return 0;
91
92 const bool omit_empty_base_classes = true;
93 return ClangASTContext::GetNumChildren(type.GetASTContext(), type.GetOpaqueQualType(), omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094}
95
96clang::ASTContext *
Sean Callanan72772842012-02-22 23:57:45 +000097ValueObjectVariable::GetClangASTImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098{
Jim Ingham56bbb882011-10-31 23:06:45 +000099 Type *var_type = m_variable_sp->GetType();
100 if (var_type)
101 return var_type->GetClangAST();
102 return 0;
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 Claytonfaac1112013-03-14 18:31:44 +0000108 ClangASTType type(GetClangAST(), GetClangType());
Sean Callanan72772842012-02-22 23:57:45 +0000109
110 if (!type.IsValid())
111 return 0;
112
Greg Claytonfaac1112013-03-14 18:31:44 +0000113 return type.GetClangTypeByteSize();
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");
Greg Claytonf5fb4272010-09-18 04:00:06 +0000141 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000142 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000143 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000144 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000145 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton007d5be2011-05-30 00:49:24 +0000146
Greg Claytonc14ee322011-09-22 04:58:26 +0000147 Target *target = exe_ctx.GetTargetPtr();
148 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000150 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
151 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000152 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153
Greg Clayton007d5be2011-05-30 00:49:24 +0000154 if (expr.IsLocationList())
155 {
156 SymbolContext sc;
157 variable->CalculateSymbolContext (&sc);
158 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000159 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000160 }
161 Value old_value(m_value);
162 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
163 {
164 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165
Greg Clayton007d5be2011-05-30 00:49:24 +0000166 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata9128ee22011-09-06 19:20:51 +0000167
168 switch (value_type)
169 {
170 case Value::eValueTypeFileAddress:
171 SetAddressTypeOfChildren(eAddressTypeFile);
172 break;
173 case Value::eValueTypeHostAddress:
174 SetAddressTypeOfChildren(eAddressTypeHost);
175 break;
176 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000177 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000178 case Value::eValueTypeVector:
Greg Claytonbdf31622011-10-01 01:53:20 +0000179 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000180 break;
181 }
Greg Claytona134cc12010-09-13 02:37:44 +0000182
Greg Clayton007d5be2011-05-30 00:49:24 +0000183 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000184 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000185 default:
186 assert(!"Unhandled expression result value kind...");
187 break;
188
189 case Value::eValueTypeScalar:
190 // The variable value is in the Scalar value inside the m_value.
191 // We can point our m_data right to it.
Greg Claytone72dfb32012-02-24 01:59:29 +0000192 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000193 break;
194
195 case Value::eValueTypeFileAddress:
196 case Value::eValueTypeLoadAddress:
197 case Value::eValueTypeHostAddress:
198 // The DWARF expression result was an address in the inferior
199 // process. If this variable is an aggregate type, we just need
200 // the address as the main value as all child variable objects
201 // will rely upon this location and add an offset and then read
202 // their own values as needed. If this variable is a simple
203 // type, we read all data for it into m_data.
204 // Make sure this type has a value before we try and read it
205
206 // If we have a file address, convert it to a load address if we can.
Greg Claytonc14ee322011-09-22 04:58:26 +0000207 Process *process = exe_ctx.GetProcessPtr();
208 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000209 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000210 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
211 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000212 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000213 SymbolContext var_sc;
214 variable->CalculateSymbolContext(&var_sc);
215 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000216 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000217 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
218 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000219 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000220 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000221 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000222 if (load_addr != LLDB_INVALID_ADDRESS)
223 {
224 m_value.SetValueType(Value::eValueTypeLoadAddress);
225 m_value.GetScalar() = load_addr;
226 }
Greg Claytona134cc12010-09-13 02:37:44 +0000227 }
228 }
229 }
230 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000231
232 if (ClangASTContext::IsAggregateType (GetClangType()))
233 {
234 // this value object represents an aggregate type whose
235 // children have values, but this object does not. So we
236 // say we are changed if our location has changed.
237 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
238 }
239 else
240 {
241 // Copy the Value and set the context to use our Variable
242 // so it can extract read its value into m_data appropriately
243 Value value(m_value);
244 value.SetContext(Value::eContextTypeVariable, variable);
Greg Claytone72dfb32012-02-24 01:59:29 +0000245 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000246 }
247 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000248 }
249
Greg Clayton007d5be2011-05-30 00:49:24 +0000250 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000252 }
Jim Ingham6035b672011-03-31 00:19:25 +0000253 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
256
257
258bool
Jim Ingham6035b672011-03-31 00:19:25 +0000259ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000261 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
262 if (exe_ctx_ref.HasFrameRef())
263 {
264 ExecutionContext exe_ctx (exe_ctx_ref);
265 StackFrame *frame = exe_ctx.GetFramePtr();
266 if (frame)
267 {
268 return m_variable_sp->IsInScope (frame);
269 }
270 else
271 {
272 // This ValueObject had a frame at one time, but now we
273 // can't locate it, so return false since we probably aren't
274 // in scope.
275 return false;
276 }
277 }
278 // We have a variable that wasn't tied to a frame, which
279 // means it is a global and is always in scope.
280 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000281
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
Greg Claytone72dfb32012-02-24 01:59:29 +0000284lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000285ValueObjectVariable::GetModule()
286{
287 if (m_variable_sp)
288 {
289 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
290 if (sc_scope)
291 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000292 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000293 }
294 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000295 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000296}
297
Enrico Granata9128ee22011-09-06 19:20:51 +0000298SymbolContextScope *
299ValueObjectVariable::GetSymbolContextScope()
300{
301 if (m_variable_sp)
302 return m_variable_sp->GetSymbolContextScope();
303 return NULL;
304}
Greg Clayton81e871e2012-02-04 02:27:34 +0000305
306bool
307ValueObjectVariable::GetDeclaration (Declaration &decl)
308{
309 if (m_variable_sp)
310 {
311 decl = m_variable_sp->GetDeclaration();
312 return true;
313 }
314 return false;
315}