blob: b43c6519baf191a8a5ff2eb1474a265fc06590e4 [file] [log] [blame]
Jim Ingham78a685a2011-04-16 00:01:13 +00001//===-- ValueObjectDynamicValue.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/ValueObjectDynamicValue.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#include "lldb/Core/ValueObject.h"
21
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
24#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
26
27#include "lldb/Target/ExecutionContext.h"
28#include "lldb/Target/LanguageRuntime.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
34
35using namespace lldb_private;
36
37ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent) :
38 ValueObject(parent),
39 m_address (),
40 m_type_sp()
41{
Jim Ingham78a685a2011-04-16 00:01:13 +000042 SetName (parent.GetName().AsCString());
43}
44
45ValueObjectDynamicValue::~ValueObjectDynamicValue()
46{
47 m_owning_valobj_sp.reset();
48}
49
50lldb::clang_type_t
51ValueObjectDynamicValue::GetClangType ()
52{
53 if (m_type_sp)
54 return m_value.GetClangType();
55 else
56 return m_parent->GetClangType();
57}
58
59ConstString
60ValueObjectDynamicValue::GetTypeName()
61{
62 // FIXME: Maybe cache the name, but have to clear it out if the type changes...
63 if (!UpdateValueIfNeeded())
64 return ConstString("<unknown type>");
65
66 if (m_type_sp)
67 return ClangASTType::GetClangTypeName (GetClangType());
68 else
69 return m_parent->GetTypeName();
70}
71
72uint32_t
73ValueObjectDynamicValue::CalculateNumChildren()
74{
75 if (!UpdateValueIfNeeded())
76 return 0;
77
78 if (m_type_sp)
79 return ClangASTContext::GetNumChildren (GetClangAST (), GetClangType(), true);
80 else
81 return m_parent->GetNumChildren();
82}
83
84clang::ASTContext *
85ValueObjectDynamicValue::GetClangAST ()
86{
87 if (!UpdateValueIfNeeded())
88 return NULL;
89
90 if (m_type_sp)
91 return m_type_sp->GetClangAST();
92 else
93 return m_parent->GetClangAST ();
94}
95
96size_t
97ValueObjectDynamicValue::GetByteSize()
98{
99 if (!UpdateValueIfNeeded())
100 return 0;
101
102 if (m_type_sp)
103 return m_value.GetValueByteSize(GetClangAST(), NULL);
104 else
105 return m_parent->GetByteSize();
106}
107
108lldb::ValueType
109ValueObjectDynamicValue::GetValueType() const
110{
111 return m_parent->GetValueType();
112}
113
114bool
115ValueObjectDynamicValue::UpdateValue ()
116{
117 SetValueIsValid (false);
118 m_error.Clear();
119
120 if (!m_parent->UpdateValueIfNeeded())
121 {
122 return false;
123 }
124
125 ExecutionContext exe_ctx (GetExecutionContextScope());
126
127 if (exe_ctx.target)
128 {
129 m_data.SetByteOrder(exe_ctx.target->GetArchitecture().GetByteOrder());
130 m_data.SetAddressByteSize(exe_ctx.target->GetArchitecture().GetAddressByteSize());
131 }
132
133 // First make sure our Type and/or Address haven't changed:
134 Process *process = m_update_point.GetProcess();
135 if (!process)
136 return false;
137
Jim Ingham61be0902011-05-02 18:13:59 +0000138 TypeAndOrName class_type_or_name;
Jim Ingham78a685a2011-04-16 00:01:13 +0000139 Address dynamic_address;
140 bool found_dynamic_type = false;
141
142 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
143 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
144 {
145 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
146 if (runtime)
Jim Ingham61be0902011-05-02 18:13:59 +0000147 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, class_type_or_name, dynamic_address);
Jim Ingham78a685a2011-04-16 00:01:13 +0000148 }
149 else
150 {
151 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
152 if (cpp_runtime)
Jim Ingham61be0902011-05-02 18:13:59 +0000153 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, class_type_or_name, dynamic_address);
Jim Ingham78a685a2011-04-16 00:01:13 +0000154
155 if (!found_dynamic_type)
156 {
157 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
158 if (objc_runtime)
Jim Ingham61be0902011-05-02 18:13:59 +0000159 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, class_type_or_name, dynamic_address);
Jim Ingham78a685a2011-04-16 00:01:13 +0000160 }
161 }
162
Jim Ingham61be0902011-05-02 18:13:59 +0000163 lldb::TypeSP dynamic_type_sp = class_type_or_name.GetTypeSP();
164
165 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
166 // don't...
167
168 m_update_point.SetUpdated();
169
Jim Ingham78a685a2011-04-16 00:01:13 +0000170 // If we don't have a dynamic type, then make ourselves just a echo of our parent.
171 // Or we could return false, and make ourselves an echo of our parent?
172 if (!found_dynamic_type)
173 {
174 if (m_type_sp)
175 SetValueDidChange(true);
176 m_value = m_parent->GetValue();
177 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0);
178 return m_error.Success();
179 }
180
181 Value old_value(m_value);
182
183 if (!m_type_sp)
184 {
185 m_type_sp = dynamic_type_sp;
186 }
187 else if (dynamic_type_sp != m_type_sp)
188 {
189 // We are another type, we need to tear down our children...
190 m_type_sp = dynamic_type_sp;
191 SetValueDidChange (true);
192 }
193
194 if (!m_address.IsValid() || m_address != dynamic_address)
195 {
196 if (m_address.IsValid())
197 SetValueDidChange (true);
198
199 // We've moved, so we should be fine...
200 m_address = dynamic_address;
201 lldb::addr_t load_address = m_address.GetLoadAddress(m_update_point.GetTarget());
202 m_value.GetScalar() = load_address;
203 }
204
205 // The type will always be the type of the dynamic object. If our parent's type was a pointer,
206 // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type
207 // should be okay...
208 lldb::clang_type_t orig_type = m_type_sp->GetClangForwardType();
209 lldb::clang_type_t corrected_type = orig_type;
210 if (m_parent->IsPointerType())
211 corrected_type = ClangASTContext::CreatePointerType (m_type_sp->GetClangAST(), orig_type);
212 else if (m_parent->IsPointerOrReferenceType())
213 corrected_type = ClangASTContext::CreateLValueReferenceType (m_type_sp->GetClangAST(), orig_type);
214
215 m_value.SetContext (Value::eContextTypeClangType, corrected_type);
216
217 // Our address is the location of the dynamic type stored in memory. It isn't a load address,
218 // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
219 m_value.SetValueType(Value::eValueTypeScalar);
220
221 if (m_address.IsValid() && m_type_sp)
222 {
223 // The variable value is in the Scalar value inside the m_value.
224 // We can point our m_data right to it.
225 m_error = m_value.GetValueAsData (&exe_ctx, GetClangAST(), m_data, 0);
226 if (m_error.Success())
227 {
228 if (ClangASTContext::IsAggregateType (GetClangType()))
229 {
230 // this value object represents an aggregate type whose
231 // children have values, but this object does not. So we
232 // say we are changed if our location has changed.
233 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
234 }
235
236 SetValueIsValid (true);
237 return true;
238 }
239 }
240
241 // We get here if we've failed above...
242 SetValueIsValid (false);
243 return false;
244}
245
246
247
248bool
249ValueObjectDynamicValue::IsInScope ()
250{
251 return m_parent->IsInScope();
252}
253