blob: 882b7021f498f9ba5a99506766f40a8c6fe01b02 [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{
91 return m_variable_sp->GetType()->GetByteSize();
92}
93
94lldb::ValueType
95ValueObjectVariable::GetValueType() const
96{
97 if (m_variable_sp)
98 return m_variable_sp->GetScope();
99 return lldb::eValueTypeInvalid;
100}
101
Jim Ingham6035b672011-03-31 00:19:25 +0000102bool
103ValueObjectVariable::UpdateValue ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104{
105 SetValueIsValid (false);
106 m_error.Clear();
107
108 Variable *variable = m_variable_sp.get();
109 DWARFExpression &expr = variable->LocationExpression();
Greg Clayton016a95e2010-09-14 02:20:48 +0000110 lldb::addr_t loclist_base_load_addr = LLDB_INVALID_ADDRESS;
Jim Ingham6035b672011-03-31 00:19:25 +0000111 ExecutionContext exe_ctx (GetExecutionContextScope());
Greg Claytonf5fb4272010-09-18 04:00:06 +0000112
Greg Clayton514487e2011-02-15 21:59:32 +0000113 if (exe_ctx.target)
Greg Claytonf5fb4272010-09-18 04:00:06 +0000114 {
Greg Clayton514487e2011-02-15 21:59:32 +0000115 m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder());
116 m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize());
Greg Claytonf5fb4272010-09-18 04:00:06 +0000117 }
118
Greg Clayton016a95e2010-09-14 02:20:48 +0000119 if (expr.IsLocationList())
120 {
121 SymbolContext sc;
122 variable->CalculateSymbolContext (&sc);
123 if (sc.function)
Greg Claytonf5e56de2010-09-14 23:36:40 +0000124 loclist_base_load_addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress (exe_ctx.target);
Greg Clayton016a95e2010-09-14 02:20:48 +0000125 }
126 Value old_value(m_value);
Greg Clayton1a65ae12011-01-25 23:55:37 +0000127 if (expr.Evaluate (&exe_ctx, GetClangAST(), NULL, NULL, NULL, loclist_base_load_addr, NULL, m_value, &m_error))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000128 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000129 m_value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130
131 Value::ValueType value_type = m_value.GetValueType();
132
133 switch (value_type)
134 {
135 default:
136 assert(!"Unhandled expression result value kind...");
137 break;
138
139 case Value::eValueTypeScalar:
140 // The variable value is in the Scalar value inside the m_value.
141 // We can point our m_data right to it.
142 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0);
143 break;
144
145 case Value::eValueTypeFileAddress:
146 case Value::eValueTypeLoadAddress:
147 case Value::eValueTypeHostAddress:
148 // The DWARF expression result was an address in the inferior
149 // process. If this variable is an aggregate type, we just need
150 // the address as the main value as all child variable objects
151 // will rely upon this location and add an offset and then read
152 // their own values as needed. If this variable is a simple
153 // type, we read all data for it into m_data.
154 // Make sure this type has a value before we try and read it
Greg Claytona134cc12010-09-13 02:37:44 +0000155
156 // If we have a file address, convert it to a load address if we can.
157 if (value_type == Value::eValueTypeFileAddress && exe_ctx.process)
158 {
159 lldb::addr_t file_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
160 if (file_addr != LLDB_INVALID_ADDRESS)
161 {
162 SymbolContext var_sc;
163 variable->CalculateSymbolContext(&var_sc);
164 if (var_sc.module_sp)
165 {
166 ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
167 if (objfile)
168 {
169 Address so_addr(file_addr, objfile->GetSectionList());
Greg Claytonf5e56de2010-09-14 23:36:40 +0000170 lldb::addr_t load_addr = so_addr.GetLoadAddress (exe_ctx.target);
Greg Claytona134cc12010-09-13 02:37:44 +0000171 if (load_addr != LLDB_INVALID_ADDRESS)
172 {
173 m_value.SetValueType(Value::eValueTypeLoadAddress);
174 m_value.GetScalar() = load_addr;
175 }
176 }
177 }
178 }
179 }
180
Greg Clayton1be10fc2010-09-29 01:12:09 +0000181 if (ClangASTContext::IsAggregateType (GetClangType()))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000182 {
183 // this value object represents an aggregate type whose
184 // children have values, but this object does not. So we
185 // say we are changed if our location has changed.
186 SetValueDidChange (value_type != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
187 }
188 else
189 {
190 // Copy the Value and set the context to use our Variable
191 // so it can extract read its value into m_data appropriately
192 Value value(m_value);
Greg Clayton526e5af2010-11-13 03:52:47 +0000193 value.SetContext(Value::eContextTypeVariable, variable);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 m_error = value.GetValueAsData(&exe_ctx, GetClangAST(), m_data, 0);
195 }
196 break;
197 }
198
199 SetValueIsValid (m_error.Success());
200 }
Jim Ingham6035b672011-03-31 00:19:25 +0000201 return m_error.Success();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000202}
203
204
205
206bool
Jim Ingham6035b672011-03-31 00:19:25 +0000207ValueObjectVariable::IsInScope ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208{
Jim Ingham6035b672011-03-31 00:19:25 +0000209 ExecutionContextScope *exe_scope = GetExecutionContextScope();
210 if (!exe_scope)
211 return true;
212
213 StackFrame *frame = exe_scope->CalculateStackFrame();
214 if (!frame)
215 return true;
216
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217 return m_variable_sp->IsInScope (frame);
218}
219