blob: 58e37bdfc9ef613d70e525d3fae22d13a05fabd1 [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
Greg Claytonc14ee322011-09-22 04:58:26 +0000129 Target *target = exe_ctx.GetTargetPtr();
130 if (target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000132 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
133 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Greg Clayton007d5be2011-05-30 00:49:24 +0000134 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135
Greg Clayton007d5be2011-05-30 00:49:24 +0000136 if (expr.IsLocationList())
137 {
138 SymbolContext sc;
139 variable->CalculateSymbolContext (&sc);
140 if (sc.function)
Greg Claytonc14ee322011-09-22 04:58:26 +0000141 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000142 }
143 Value old_value(m_value);
144 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
145 {
146 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Greg Clayton007d5be2011-05-30 00:49:24 +0000148 Value::ValueType value_type = m_value.GetValueType();
Enrico Granata9128ee22011-09-06 19:20:51 +0000149
150 switch (value_type)
151 {
152 case Value::eValueTypeFileAddress:
153 SetAddressTypeOfChildren(eAddressTypeFile);
154 break;
155 case Value::eValueTypeHostAddress:
156 SetAddressTypeOfChildren(eAddressTypeHost);
157 break;
158 case Value::eValueTypeLoadAddress:
Enrico Granata9128ee22011-09-06 19:20:51 +0000159 case Value::eValueTypeScalar:
Greg Claytonbdf31622011-10-01 01:53:20 +0000160 SetAddressTypeOfChildren(eAddressTypeLoad);
Enrico Granata9128ee22011-09-06 19:20:51 +0000161 break;
162 }
Greg Claytona134cc12010-09-13 02:37:44 +0000163
Greg Clayton007d5be2011-05-30 00:49:24 +0000164 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000165 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000166 default:
167 assert(!"Unhandled expression result value kind...");
168 break;
169
170 case Value::eValueTypeScalar:
171 // The variable value is in the Scalar value inside the m_value.
172 // We can point our m_data right to it.
Greg Clayton644247c2011-07-07 01:59:51 +0000173 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0, GetModule());
Greg Clayton007d5be2011-05-30 00:49:24 +0000174 break;
175
176 case Value::eValueTypeFileAddress:
177 case Value::eValueTypeLoadAddress:
178 case Value::eValueTypeHostAddress:
179 // The DWARF expression result was an address in the inferior
180 // process. If this variable is an aggregate type, we just need
181 // the address as the main value as all child variable objects
182 // will rely upon this location and add an offset and then read
183 // their own values as needed. If this variable is a simple
184 // type, we read all data for it into m_data.
185 // Make sure this type has a value before we try and read it
186
187 // If we have a file address, convert it to a load address if we can.
Greg Claytonc14ee322011-09-22 04:58:26 +0000188 Process *process = exe_ctx.GetProcessPtr();
189 if (value_type == Value::eValueTypeFileAddress && process && process->IsAlive())
Greg Claytona134cc12010-09-13 02:37:44 +0000190 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000191 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
192 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000193 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000194 SymbolContext var_sc;
195 variable->CalculateSymbolContext(&var_sc);
196 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000197 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000198 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
199 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000200 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000201 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonc14ee322011-09-22 04:58:26 +0000202 lldb::addr_t load_addr = so_addr.GetLoadAddress (target);
Greg Clayton007d5be2011-05-30 00:49:24 +0000203 if (load_addr != LLDB_INVALID_ADDRESS)
204 {
205 m_value.SetValueType(Value::eValueTypeLoadAddress);
206 m_value.GetScalar() = load_addr;
207 }
Greg Claytona134cc12010-09-13 02:37:44 +0000208 }
209 }
210 }
211 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000212
213 if (ClangASTContext::IsAggregateType (GetClangType()))
214 {
215 // this value object represents an aggregate type whose
216 // children have values, but this object does not. So we
217 // say we are changed if our location has changed.
218 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
219 }
220 else
221 {
222 // Copy the Value and set the context to use our Variable
223 // so it can extract read its value into m_data appropriately
224 Value value(m_value);
225 value.SetContext(Value::eContextTypeVariable, variable);
Greg Clayton644247c2011-07-07 01:59:51 +0000226 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0, GetModule());
Greg Clayton007d5be2011-05-30 00:49:24 +0000227 }
228 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000229 }
230
Greg Clayton007d5be2011-05-30 00:49:24 +0000231 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000232 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 }
Jim Ingham6035b672011-03-31 00:19:25 +0000234 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
237
238
239bool
Jim Ingham6035b672011-03-31 00:19:25 +0000240ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000241{
Jim Ingham6035b672011-03-31 00:19:25 +0000242 ExecutionContextScope *exe_scope = GetExecutionContextScope();
243 if (!exe_scope)
244 return true;
245
246 StackFrame *frame = exe_scope->CalculateStackFrame();
247 if (!frame)
248 return true;
249
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250 return m_variable_sp->IsInScope (frame);
251}
252
Greg Clayton644247c2011-07-07 01:59:51 +0000253Module *
254ValueObjectVariable::GetModule()
255{
256 if (m_variable_sp)
257 {
258 SymbolContextScope *sc_scope = m_variable_sp->GetSymbolContextScope();
259 if (sc_scope)
260 {
261 SymbolContext sc;
262 sc_scope->CalculateSymbolContext (&sc);
263 return sc.module_sp.get();
264 }
265 }
266 return NULL;
267}
268
Enrico Granata9128ee22011-09-06 19:20:51 +0000269SymbolContextScope *
270ValueObjectVariable::GetSymbolContextScope()
271{
272 if (m_variable_sp)
273 return m_variable_sp->GetSymbolContextScope();
274 return NULL;
275}