blob: d71ec97ce3cf04b2265f8de9a44e5480a05fe99d [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
21#include "lldb/Symbol/ObjectFile.h"
22#include "lldb/Symbol/SymbolContext.h"
Greg Clayton644247c2011-07-07 01:59:51 +000023#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
26
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Target/Target.h"
31#include "lldb/Target/Thread.h"
32
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033
34using namespace lldb_private;
35
Jim Ingham58b59f92011-04-22 23:53:53 +000036lldb::ValueObjectSP
37ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
38{
39 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
40}
41
Jim Ingham6035b672011-03-31 00:19:25 +000042ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
43 ValueObject(exe_scope),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044 m_variable_sp(var_sp)
45{
46 // Do not attempt to construct one of these objects with no variable!
47 assert (m_variable_sp.get() != NULL);
48 m_name = var_sp->GetName();
49}
50
51ValueObjectVariable::~ValueObjectVariable()
52{
53}
54
Greg Clayton6beaaa62011-01-17 03:46:26 +000055lldb::clang_type_t
Sean Callanan72772842012-02-22 23:57:45 +000056ValueObjectVariable::GetClangTypeImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057{
58 Type *var_type = m_variable_sp->GetType();
59 if (var_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +000060 return var_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 return NULL;
62}
63
64ConstString
65ValueObjectVariable::GetTypeName()
66{
67 Type * var_type = m_variable_sp->GetType();
68 if (var_type)
69 return var_type->GetName();
Greg Clayton84db9102012-03-26 23:03:23 +000070 return ConstString();
71}
72
73ConstString
74ValueObjectVariable::GetQualifiedTypeName()
75{
76 Type * var_type = m_variable_sp->GetType();
77 if (var_type)
78 return var_type->GetQualifiedName();
79 return ConstString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000080}
81
82uint32_t
83ValueObjectVariable::CalculateNumChildren()
Sean Callanan72772842012-02-22 23:57:45 +000084{
85 ClangASTType type(GetClangAST(),
86 GetClangType());
87
88 if (!type.IsValid())
89 return 0;
90
91 const bool omit_empty_base_classes = true;
92 return ClangASTContext::GetNumChildren(type.GetASTContext(), type.GetOpaqueQualType(), omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093}
94
95clang::ASTContext *
Sean Callanan72772842012-02-22 23:57:45 +000096ValueObjectVariable::GetClangASTImpl ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097{
Jim Ingham56bbb882011-10-31 23:06:45 +000098 Type *var_type = m_variable_sp->GetType();
99 if (var_type)
100 return var_type->GetClangAST();
101 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102}
103
104size_t
105ValueObjectVariable::GetByteSize()
106{
Sean Callanan72772842012-02-22 23:57:45 +0000107 ClangASTType type(GetClangAST(),
108 GetClangType());
109
110 if (!type.IsValid())
111 return 0;
112
113 return (ClangASTType::GetClangTypeBitWidth(type.GetASTContext(), type.GetOpaqueQualType()) + 7) / 8;
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 Claytonbdf31622011-10-01 01:53:20 +0000178 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000179 break;
180 }
Greg Claytona134cc12010-09-13 02:37:44 +0000181
Greg Clayton007d5be2011-05-30 00:49:24 +0000182 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000183 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000184 default:
185 assert(!"Unhandled expression result value kind...");
186 break;
187
188 case Value::eValueTypeScalar:
189 // The variable value is in the Scalar value inside the m_value.
190 // We can point our m_data right to it.
Greg Claytone72dfb32012-02-24 01:59:29 +0000191 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000192 break;
193
194 case Value::eValueTypeFileAddress:
195 case Value::eValueTypeLoadAddress:
196 case Value::eValueTypeHostAddress:
197 // The DWARF expression result was an address in the inferior
198 // process. If this variable is an aggregate type, we just need
199 // the address as the main value as all child variable objects
200 // will rely upon this location and add an offset and then read
201 // their own values as needed. If this variable is a simple
202 // type, we read all data for it into m_data.
203 // Make sure this type has a value before we try and read it
204
205 // If we have a file address, convert it to a load address if we can.
Greg Claytonc14ee322011-09-22 04:58:26 +0000206 Process *process = exe_ctx.GetProcessPtr();
207 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000208 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000209 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
210 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000211 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000212 SymbolContext var_sc;
213 variable->CalculateSymbolContext(&var_sc);
214 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000215 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000216 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
217 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000218 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000219 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000220 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000221 if (load_addr != LLDB_INVALID_ADDRESS)
222 {
223 m_value.SetValueType(Value::eValueTypeLoadAddress);
224 m_value.GetScalar() = load_addr;
225 }
Greg Claytona134cc12010-09-13 02:37:44 +0000226 }
227 }
228 }
229 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000230
231 if (ClangASTContext::IsAggregateType (GetClangType()))
232 {
233 // this value object represents an aggregate type whose
234 // children have values, but this object does not. So we
235 // say we are changed if our location has changed.
236 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
237 }
238 else
239 {
240 // Copy the Value and set the context to use our Variable
241 // so it can extract read its value into m_data appropriately
242 Value value(m_value);
243 value.SetContext(Value::eContextTypeVariable, variable);
Greg Claytone72dfb32012-02-24 01:59:29 +0000244 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton007d5be2011-05-30 00:49:24 +0000245 }
246 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000247 }
248
Greg Clayton007d5be2011-05-30 00:49:24 +0000249 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 }
Jim Ingham6035b672011-03-31 00:19:25 +0000252 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253}
254
255
256
257bool
Jim Ingham6035b672011-03-31 00:19:25 +0000258ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000260 const ExecutionContextRef &exe_ctx_ref = GetExecutionContextRef();
261 if (exe_ctx_ref.HasFrameRef())
262 {
263 ExecutionContext exe_ctx (exe_ctx_ref);
264 StackFrame *frame = exe_ctx.GetFramePtr();
265 if (frame)
266 {
267 return m_variable_sp->IsInScope (frame);
268 }
269 else
270 {
271 // This ValueObject had a frame at one time, but now we
272 // can't locate it, so return false since we probably aren't
273 // in scope.
274 return false;
275 }
276 }
277 // We have a variable that wasn't tied to a frame, which
278 // means it is a global and is always in scope.
279 return true;
Jim Ingham6035b672011-03-31 00:19:25 +0000280
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281}
282
Greg Claytone72dfb32012-02-24 01:59:29 +0000283lldb::ModuleSP
Greg Clayton644247c2011-07-07 01:59:51 +0000284ValueObjectVariable::GetModule()
285{
286 if (m_variable_sp)
287 {
288 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
289 if (sc_scope)
290 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000291 return sc_scope->CalculateSymbolContextModule();
Greg Clayton644247c2011-07-07 01:59:51 +0000292 }
293 }
Greg Claytone72dfb32012-02-24 01:59:29 +0000294 return lldb::ModuleSP();
Greg Clayton644247c2011-07-07 01:59:51 +0000295}
296
Enrico Granata9128ee22011-09-06 19:20:51 +0000297SymbolContextScope *
298ValueObjectVariable::GetSymbolContextScope()
299{
300 if (m_variable_sp)
301 return m_variable_sp->GetSymbolContextScope();
302 return NULL;
303}
Greg Clayton81e871e2012-02-04 02:27:34 +0000304
305bool
306ValueObjectVariable::GetDeclaration (Declaration &decl)
307{
308 if (m_variable_sp)
309 {
310 decl = m_variable_sp->GetDeclaration();
311 return true;
312 }
313 return false;
314}