blob: 3ff37e9deacf58af39001c6bba45a568e83dd4e7 [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton49ce8962012-08-29 21:13:06 +000021#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
Greg Clayton801417e2011-07-07 01:59:51 +000024#include "lldb/Symbol/SymbolContextScope.h"
Chris Lattner24943d22010-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 Lattner24943d22010-06-08 16:52:24 +000034
35using namespace lldb_private;
36
Jim Ingham47da8102011-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 Inghamfa3a16a2011-03-31 00:19:25 +000043ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
44 ValueObject(exe_scope),
Chris Lattner24943d22010-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 Claytonb01000f2011-01-17 03:46:26 +000056lldb::clang_type_t
Sean Callanan931acec2012-02-22 23:57:45 +000057ValueObjectVariable::GetClangTypeImpl ()
Chris Lattner24943d22010-06-08 16:52:24 +000058{
59 Type *var_type = m_variable_sp->GetType();
60 if (var_type)
Greg Claytonb01000f2011-01-17 03:46:26 +000061 return var_type->GetClangForwardType();
Chris Lattner24943d22010-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 Claytondc0a38c2012-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 Lattner24943d22010-06-08 16:52:24 +000081}
82
83uint32_t
84ValueObjectVariable::CalculateNumChildren()
Sean Callanan931acec2012-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 Lattner24943d22010-06-08 16:52:24 +000094}
95
96clang::ASTContext *
Sean Callanan931acec2012-02-22 23:57:45 +000097ValueObjectVariable::GetClangASTImpl ()
Chris Lattner24943d22010-06-08 16:52:24 +000098{
Jim Inghambdc85ee2011-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 Lattner24943d22010-06-08 16:52:24 +0000103}
104
105size_t
106ValueObjectVariable::GetByteSize()
107{
Sean Callanan931acec2012-02-22 23:57:45 +0000108 ClangASTType type(GetClangAST(),
109 GetClangType());
110
111 if (!type.IsValid())
112 return 0;
113
114 return (ClangASTType::GetClangTypeBitWidth(type.GetASTContext(), type.GetOpaqueQualType()) + 7) / 8;
Chris Lattner24943d22010-06-08 16:52:24 +0000115}
116
117lldb::ValueType
118ValueObjectVariable::GetValueType() const
119{
120 if (m_variable_sp)
121 return m_variable_sp->GetScope();
122 return lldb::eValueTypeInvalid;
123}
124
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000125bool
126ValueObjectVariable::UpdateValue ()
Chris Lattner24943d22010-06-08 16:52:24 +0000127{
128 SetValueIsValid (false);
129 m_error.Clear();
130
131 Variable *variable = m_variable_sp.get();
132 DWARFExpression &expr = variable->LocationExpression();
Greg Clayton1e30afd2010-09-18 04:00:06 +0000133
Greg Clayton82f07462011-05-30 00:49:24 +0000134 if (variable->GetLocationIsConstantValueData())
Greg Clayton1e30afd2010-09-18 04:00:06 +0000135 {
Greg Clayton82f07462011-05-30 00:49:24 +0000136 // expr doesn't contain DWARF bytes, it contains the constant variable
137 // value bytes themselves...
138 if (expr.GetExpressionData(m_data))
139 m_value.SetContext(Value::eContextTypeVariable, variable);
140 else
141 m_error.SetErrorString ("empty constant data");
Greg Clayton1e30afd2010-09-18 04:00:06 +0000142 }
Greg Clayton82f07462011-05-30 00:49:24 +0000143 else
Greg Clayton178710c2010-09-14 02:20:48 +0000144 {
Greg Clayton82f07462011-05-30 00:49:24 +0000145 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Greg Claytonb4d7fc02012-02-17 07:49:44 +0000146 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton82f07462011-05-30 00:49:24 +0000147
Greg Clayton567e7f32011-09-22 04:58:26 +0000148 Target *target = exe_ctx.GetTargetPtr();
149 if (target)
Chris Lattner24943d22010-06-08 16:52:24 +0000150 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000151 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
152 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton82f07462011-05-30 00:49:24 +0000153 }
Chris Lattner24943d22010-06-08 16:52:24 +0000154
Greg Clayton82f07462011-05-30 00:49:24 +0000155 if (expr.IsLocationList())
156 {
157 SymbolContext sc;
158 variable->CalculateSymbolContext (&sc);
159 if (sc.function)
Greg Clayton567e7f32011-09-22 04:58:26 +0000160 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton82f07462011-05-30 00:49:24 +0000161 }
162 Value old_value(m_value);
163 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
164 {
165 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner24943d22010-06-08 16:52:24 +0000166
Greg Clayton82f07462011-05-30 00:49:24 +0000167 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata91544802011-09-06 19:20:51 +0000168
169 switch (value_type)
170 {
171 case Value::eValueTypeFileAddress:
172 SetAddressTypeOfChildren(eAddressTypeFile);
173 break;
174 case Value::eValueTypeHostAddress:
175 SetAddressTypeOfChildren(eAddressTypeHost);
176 break;
177 case Value::eValueTypeLoadAddress:
Enrico Granata91544802011-09-06 19:20:51 +0000178 case Value::eValueTypeScalar:
Greg Clayton7c5e22f2012-10-30 18:18:43 +0000179 case Value::eValueTypeVector:
Greg Clayton05e3b9e2011-10-01 01:53:20 +0000180 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata91544802011-09-06 19:20:51 +0000181 break;
182 }
Greg Claytonaed58812010-09-13 02:37:44 +0000183
Greg Clayton82f07462011-05-30 00:49:24 +0000184 switch (value_type)
Greg Claytonaed58812010-09-13 02:37:44 +0000185 {
Greg Clayton82f07462011-05-30 00:49:24 +0000186 default:
187 assert(!"Unhandled expression result value kind...");
188 break;
189
190 case Value::eValueTypeScalar:
191 // The variable value is in the Scalar value inside the m_value.
192 // We can point our m_data right to it.
Greg Clayton3508c382012-02-24 01:59:29 +0000193 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton82f07462011-05-30 00:49:24 +0000194 break;
195
196 case Value::eValueTypeFileAddress:
197 case Value::eValueTypeLoadAddress:
198 case Value::eValueTypeHostAddress:
199 // The DWARF expression result was an address in the inferior
200 // process. If this variable is an aggregate type, we just need
201 // the address as the main value as all child variable objects
202 // will rely upon this location and add an offset and then read
203 // their own values as needed. If this variable is a simple
204 // type, we read all data for it into m_data.
205 // Make sure this type has a value before we try and read it
206
207 // If we have a file address, convert it to a load address if we can.
Greg Clayton567e7f32011-09-22 04:58:26 +0000208 Process *process = exe_ctx.GetProcessPtr();
209 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytonaed58812010-09-13 02:37:44 +0000210 {
Greg Clayton82f07462011-05-30 00:49:24 +0000211 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
212 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytonaed58812010-09-13 02:37:44 +0000213 {
Greg Clayton82f07462011-05-30 00:49:24 +0000214 SymbolContext var_sc;
215 variable->CalculateSymbolContext(&var_sc);
216 if (var_sc.module_sp)
Greg Claytonaed58812010-09-13 02:37:44 +0000217 {
Greg Clayton82f07462011-05-30 00:49:24 +0000218 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
219 if (objfile)
Greg Claytonaed58812010-09-13 02:37:44 +0000220 {
Greg Clayton82f07462011-05-30 00:49:24 +0000221 Address so_addr(file_addr, objfile->GetSectionList());
Greg Clayton567e7f32011-09-22 04:58:26 +0000222 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton82f07462011-05-30 00:49:24 +0000223 if (load_addr != LLDB_INVALID_ADDRESS)
224 {
225 m_value.SetValueType(Value::eValueTypeLoadAddress);
226 m_value.GetScalar() = load_addr;
227 }
Greg Claytonaed58812010-09-13 02:37:44 +0000228 }
229 }
230 }
231 }
Greg Clayton82f07462011-05-30 00:49:24 +0000232
233 if (ClangASTContext::IsAggregateType (GetClangType()))
234 {
235 // this value object represents an aggregate type whose
236 // children have values, but this object does not. So we
237 // say we are changed if our location has changed.
238 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
239 }
240 else
241 {
242 // Copy the Value and set the context to use our Variable
243 // so it can extract read its value into m_data appropriately
244 Value value(m_value);
245 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton3508c382012-02-24 01:59:29 +0000246 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule().get());
Greg Clayton82f07462011-05-30 00:49:24 +0000247 }
248 break;
Greg Claytonaed58812010-09-13 02:37:44 +0000249 }
250
Greg Clayton82f07462011-05-30 00:49:24 +0000251 SetValueIsValid (m_error.Success());
Chris Lattner24943d22010-06-08 16:52:24 +0000252 }
Chris Lattner24943d22010-06-08 16:52:24 +0000253 }
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000254 return m_error.Success();
Chris Lattner24943d22010-06-08 16:52:24 +0000255}
256
257
258
259bool
Jim Inghamfa3a16a2011-03-31 00:19:25 +0000260ValueObjectVariable::IsInScope ()
Chris Lattner24943d22010-06-08 16:52:24 +0000261{
Greg Claytonb4d7fc02012-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);
266 StackFrame *frame = exe_ctx.GetFramePtr();
267 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 Inghamfa3a16a2011-03-31 00:19:25 +0000282
Chris Lattner24943d22010-06-08 16:52:24 +0000283}
284
Greg Clayton3508c382012-02-24 01:59:29 +0000285lldb::ModuleSP
Greg Clayton801417e2011-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 Clayton3508c382012-02-24 01:59:29 +0000293 return sc_scope->CalculateSymbolContextModule();
Greg Clayton801417e2011-07-07 01:59:51 +0000294 }
295 }
Greg Clayton3508c382012-02-24 01:59:29 +0000296 return lldb::ModuleSP();
Greg Clayton801417e2011-07-07 01:59:51 +0000297}
298
Enrico Granata91544802011-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 Clayton0a19a1b2012-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}