blob: e2a281b7f95b2920b1948504e76c353f395b720b [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"
23#include "lldb/Symbol/Type.h"
24#include "lldb/Symbol/Variable.h"
25
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/RegisterContext.h"
29#include "lldb/Target/Target.h"
30#include "lldb/Target/Thread.h"
31
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33using namespace lldb_private;
34
Jim Ingham58b59f92011-04-22 23:53:53 +000035lldb::ValueObjectSP
36ValueObjectVariable::Create (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp)
37{
38 return (new ValueObjectVariable (exe_scope, var_sp))->GetSP();
39}
40
Jim Ingham6035b672011-03-31 00:19:25 +000041ValueObjectVariable::ValueObjectVariable (ExecutionContextScope *exe_scope, const lldb::VariableSP &var_sp) :
42 ValueObject(exe_scope),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043 m_variable_sp(var_sp)
44{
45 // Do not attempt to construct one of these objects with no variable!
46 assert (m_variable_sp.get() != NULL);
47 m_name = var_sp->GetName();
48}
49
50ValueObjectVariable::~ValueObjectVariable()
51{
52}
53
Greg Clayton6beaaa62011-01-17 03:46:26 +000054lldb::clang_type_t
Greg Clayton1be10fc2010-09-29 01:12:09 +000055ValueObjectVariable::GetClangType ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056{
57 Type *var_type = m_variable_sp->GetType();
58 if (var_type)
Greg Clayton6beaaa62011-01-17 03:46:26 +000059 return var_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 return NULL;
61}
62
63ConstString
64ValueObjectVariable::GetTypeName()
65{
66 Type * var_type = m_variable_sp->GetType();
67 if (var_type)
68 return var_type->GetName();
69 ConstString empty_type_name;
70 return empty_type_name;
71}
72
73uint32_t
74ValueObjectVariable::CalculateNumChildren()
75{
76 Type *var_type = m_variable_sp->GetType();
77 if (var_type)
78 return var_type->GetNumChildren(true);
79 return 0;
80}
81
82clang::ASTContext *
83ValueObjectVariable::GetClangAST ()
84{
85 return m_variable_sp->GetType()->GetClangAST();
86}
87
88size_t
89ValueObjectVariable::GetByteSize()
90{
Greg Clayton007d5be2011-05-30 00:49:24 +000091 Type *type = m_variable_sp->GetType();
92 if (type)
93 return type->GetByteSize();
94 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095}
96
97lldb::ValueType
98ValueObjectVariable::GetValueType() const
99{
100 if (m_variable_sp)
101 return m_variable_sp->GetScope();
102 return lldb::eValueTypeInvalid;
103}
104
Jim Ingham6035b672011-03-31 00:19:25 +0000105bool
106ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107{
108 SetValueIsValid (false);
109 m_error.Clear();
110
111 Variable *variable = m_variable_sp.get();
112 DWARFExpression &expr = variable->LocationExpression();
Greg Claytonf5fb4272010-09-18 04:00:06 +0000113
Greg Clayton007d5be2011-05-30 00:49:24 +0000114 if (variable->GetLocationIsConstantValueData())
Greg Claytonf5fb4272010-09-18 04:00:06 +0000115 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000116 // expr doesn't contain DWARF bytes, it contains the constant variable
117 // value bytes themselves...
118 if (expr.GetExpressionData(m_data))
119 m_value.SetContext(Value::eContextTypeVariable, variable);
120 else
121 m_error.SetErrorString ("empty constant data");
Greg Claytonf5fb4272010-09-18 04:00:06 +0000122 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000123 else
Greg Clayton016a95e2010-09-14 02:20:48 +0000124 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000125 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
126 ExecutionContext exe_ctx (GetExecutionContextScope());
127
128 if (exe_ctx.target)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000130 m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder());
131 m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize());
132 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000133
Greg Clayton007d5be2011-05-30 00:49:24 +0000134 if (expr.IsLocationList())
135 {
136 SymbolContext sc;
137 variable->CalculateSymbolContext (&sc);
138 if (sc.function)
139 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.target);
140 }
141 Value old_value(m_value);
142 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
143 {
144 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145
Greg Clayton007d5be2011-05-30 00:49:24 +0000146 Value::ValueType value_type = m_value.GetValueType();
Greg Claytona134cc12010-09-13 02:37:44 +0000147
Greg Clayton007d5be2011-05-30 00:49:24 +0000148 switch (value_type)
Greg Claytona134cc12010-09-13 02:37:44 +0000149 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000150 default:
151 assert(!"Unhandled expression result value kind...");
152 break;
153
154 case Value::eValueTypeScalar:
155 // The variable value is in the Scalar value inside the m_value.
156 // We can point our m_data right to it.
157 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0);
158 break;
159
160 case Value::eValueTypeFileAddress:
161 case Value::eValueTypeLoadAddress:
162 case Value::eValueTypeHostAddress:
163 // The DWARF expression result was an address in the inferior
164 // process. If this variable is an aggregate type, we just need
165 // the address as the main value as all child variable objects
166 // will rely upon this location and add an offset and then read
167 // their own values as needed. If this variable is a simple
168 // type, we read all data for it into m_data.
169 // Make sure this type has a value before we try and read it
170
171 // If we have a file address, convert it to a load address if we can.
172 if (value_type == Value::eValueTypeFileAddress && exe_ctx.process)
Greg Claytona134cc12010-09-13 02:37:44 +0000173 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000174 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
175 if (file_addr != LLDB_INVALID_ADDRESS)
Greg Claytona134cc12010-09-13 02:37:44 +0000176 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000177 SymbolContext var_sc;
178 variable->CalculateSymbolContext(&var_sc);
179 if (var_sc.module_sp)
Greg Claytona134cc12010-09-13 02:37:44 +0000180 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000181 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
182 if (objfile)
Greg Claytona134cc12010-09-13 02:37:44 +0000183 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000184 Address so_addr(file_addr, objfile->GetSectionList());
185 lldb::addr_t load_addr = so_addr.GetLoadAddress (exe_ctx.target);
186 if (load_addr != LLDB_INVALID_ADDRESS)
187 {
188 m_value.SetValueType(Value::eValueTypeLoadAddress);
189 m_value.GetScalar() = load_addr;
190 }
Greg Claytona134cc12010-09-13 02:37:44 +0000191 }
192 }
193 }
194 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000195
196 if (ClangASTContext::IsAggregateType (GetClangType()))
197 {
198 // this value object represents an aggregate type whose
199 // children have values, but this object does not. So we
200 // say we are changed if our location has changed.
201 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
202 }
203 else
204 {
205 // Copy the Value and set the context to use our Variable
206 // so it can extract read its value into m_data appropriately
207 Value value(m_value);
208 value.SetContext(Value::eContextTypeVariable, variable);
209 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0);
210 }
211 break;
Greg Claytona134cc12010-09-13 02:37:44 +0000212 }
213
Greg Clayton007d5be2011-05-30 00:49:24 +0000214 SetValueIsValid (m_error.Success());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216 }
Jim Ingham6035b672011-03-31 00:19:25 +0000217 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218}
219
220
221
222bool
Jim Ingham6035b672011-03-31 00:19:25 +0000223ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224{
Jim Ingham6035b672011-03-31 00:19:25 +0000225 ExecutionContextScope *exe_scope = GetExecutionContextScope();
226 if (!exe_scope)
227 return true;
228
229 StackFrame *frame = exe_scope->CalculateStackFrame();
230 if (!frame)
231 return true;
232
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 return m_variable_sp->IsInScope (frame);
234}
235