blob: 5717406ba20df23965f86edfe817ee3d1eeae254 [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
Greg Clayton1be10fc2010-09-29 01:12:09 +000056ValueObjectVariable::GetClangType ()
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();
70 ConstString empty_type_name;
71 return empty_type_name;
72}
73
74uint32_t
75ValueObjectVariable::CalculateNumChildren()
76{
77 Type *var_type = m_variable_sp->GetType();
78 if (var_type)
79 return var_type->GetNumChildren(true);
80 return 0;
81}
82
83clang::ASTContext *
84ValueObjectVariable::GetClangAST ()
85{
86 return m_variable_sp->GetType()->GetClangAST();
87}
88
89size_t
90ValueObjectVariable::GetByteSize()
91{
Greg Clayton007d5be2011-05-30 00:49:24 +000092 Type *type = m_variable_sp->GetType();
93 if (type)
94 return type->GetByteSize();
95 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000096}
97
98lldb::ValueType
99ValueObjectVariable::GetValueType() const
100{
101 if (m_variable_sp)
102 return m_variable_sp->GetScope();
103 return lldb::eValueTypeInvalid;
104}
105
Jim Ingham6035b672011-03-31 00:19:25 +0000106bool
107ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108{
109 SetValueIsValid (false);
110 m_error.Clear();
111
112 Variable *variable = m_variable_sp.get();
113 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000114
Greg Clayton007d5be2011-05-30 00:49:24 +0000115 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000116 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000117 // expr doesn't contain DWARF bytes, it contains the constant variable
118 // value bytes themselves...
119 if (expr.GetExpressionData(m_data))
120 m_value.SetContext(Value::eContextTypeVariable, variable);
121 else
122 m_error.SetErrorString ("empty constant data");
Greg Claytonf5fb4272010-09-18 04:00:06 +0000123 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000124 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000125 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000126 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
127 ExecutionContext exe_ctx (GetExecutionContextScope());
128
129 if (exe_ctx.target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000131 m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder());
132 m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize());
133 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134
Greg Clayton007d5be2011-05-30 00:49:24 +0000135 if (expr.IsLocationList())
136 {
137 SymbolContext sc;
138 variable->CalculateSymbolContext (&sc);
139 if (sc.function)
140 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.target);
141 }
142 Value old_value(m_value);
143 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
144 {
145 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146
Greg Clayton007d5be2011-05-30 00:49:24 +0000147 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata9128ee22011-09-06 19:20:51 +0000148
149 switch (value_type)
150 {
151 case Value::eValueTypeFileAddress:
152 SetAddressTypeOfChildren(eAddressTypeFile);
153 break;
154 case Value::eValueTypeHostAddress:
155 SetAddressTypeOfChildren(eAddressTypeHost);
156 break;
157 case Value::eValueTypeLoadAddress:
158 SetAddressTypeOfChildren(eAddressTypeLoad);
159 break;
160 case Value::eValueTypeScalar:
161 // TODO: is this the right thing to do?
162 SetAddressTypeOfChildren(eAddressTypeInvalid);
163 break;
164 }
Greg Claytona134cc12010-09-13 02:37:44 +0000165
Greg Clayton007d5be2011-05-30 00:49:24 +0000166 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000167 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000168 default:
169 assert(!"Unhandled expression result value kind...");
170 break;
171
172 case Value::eValueTypeScalar:
173 // The variable value is in the Scalar value inside the m_value.
174 // We can point our m_data right to it.
Greg Clayton644247c2011-07-07 01:59:51 +0000175 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule());
Greg Clayton007d5be2011-05-30 00:49:24 +0000176 break;
177
178 case Value::eValueTypeFileAddress:
179 case Value::eValueTypeLoadAddress:
180 case Value::eValueTypeHostAddress:
181 // The DWARF expression result was an address in the inferior
182 // process. If this variable is an aggregate type, we just need
183 // the address as the main value as all child variable objects
184 // will rely upon this location and add an offset and then read
185 // their own values as needed. If this variable is a simple
186 // type, we read all data for it into m_data.
187 // Make sure this type has a value before we try and read it
188
189 // If we have a file address, convert it to a load address if we can.
Greg Claytondea8cb42011-06-29 22:09:02 +0000190 if (value_type == Value::eValueTypeFileAddress && exe_ctx.process && exe_ctx.process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000191 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000192 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
193 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000194 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000195 SymbolContext var_sc;
196 variable->CalculateSymbolContext(&var_sc);
197 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000198 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000199 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
200 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000201 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000202 Address so_addr(file_addr, objfile->GetSectionList());
203 lldb::addr_t load_addr = so_addr.GetLoadAddress (exe_ctx.target);
204 if (load_addr != LLDB_INVALID_ADDRESS)
205 {
206 m_value.SetValueType(Value::eValueTypeLoadAddress);
207 m_value.GetScalar() = load_addr;
208 }
Greg Claytona134cc12010-09-13 02:37:44 +0000209 }
210 }
211 }
212 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000213
214 if (ClangASTContext::IsAggregateType (GetClangType()))
215 {
216 // this value object represents an aggregate type whose
217 // children have values, but this object does not. So we
218 // say we are changed if our location has changed.
219 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
220 }
221 else
222 {
223 // Copy the Value and set the context to use our Variable
224 // so it can extract read its value into m_data appropriately
225 Value value(m_value);
226 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton644247c2011-07-07 01:59:51 +0000227 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule());
Greg Clayton007d5be2011-05-30 00:49:24 +0000228 }
229 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000230 }
231
Greg Clayton007d5be2011-05-30 00:49:24 +0000232 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 }
Jim Ingham6035b672011-03-31 00:19:25 +0000235 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
238
239
240bool
Jim Ingham6035b672011-03-31 00:19:25 +0000241ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242{
Jim Ingham6035b672011-03-31 00:19:25 +0000243 ExecutionContextScope *exe_scope = GetExecutionContextScope();
244 if (!exe_scope)
245 return true;
246
247 StackFrame *frame = exe_scope->CalculateStackFrame();
248 if (!frame)
249 return true;
250
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251 return m_variable_sp->IsInScope (frame);
252}
253
Greg Clayton644247c2011-07-07 01:59:51 +0000254Module *
255ValueObjectVariable::GetModule()
256{
257 if (m_variable_sp)
258 {
259 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
260 if (sc_scope)
261 {
262 SymbolContext sc;
263 sc_scope->CalculateSymbolContext (&sc);
264 return sc.module_sp.get();
265 }
266 }
267 return NULL;
268}
269
Enrico Granata9128ee22011-09-06 19:20:51 +0000270SymbolContextScope *
271ValueObjectVariable::GetSymbolContextScope()
272{
273 if (m_variable_sp)
274 return m_variable_sp->GetSymbolContextScope();
275 return NULL;
276}