1 //===-- ValueObjectDynamicValue.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "lldb/Core/ValueObjectDynamicValue.h"
15 // Other libraries and framework includes
17 #include "lldb/Core/Log.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/ValueObjectList.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/ValueObject.h"
23 #include "lldb/Symbol/ClangASTType.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/SymbolContext.h"
26 #include "lldb/Symbol/Type.h"
27 #include "lldb/Symbol/Variable.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/LanguageRuntime.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/RegisterContext.h"
33 #include "lldb/Target/Target.h"
34 #include "lldb/Target/Thread.h"
36 using namespace lldb_private;
38 ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) :
41 m_dynamic_type_info(),
42 m_use_dynamic (use_dynamic)
44 SetName (parent.GetName());
47 ValueObjectDynamicValue::~ValueObjectDynamicValue()
49 m_owning_valobj_sp.reset();
53 ValueObjectDynamicValue::GetClangTypeImpl ()
55 if (m_dynamic_type_info.HasTypeSP())
56 return m_value.GetClangType();
58 return m_parent->GetClangType();
62 ValueObjectDynamicValue::GetTypeName()
64 const bool success = UpdateValueIfNeeded(false);
67 if (m_dynamic_type_info.HasTypeSP())
68 return GetClangType().GetConstTypeName();
69 if (m_dynamic_type_info.HasName())
70 return m_dynamic_type_info.GetName();
72 return m_parent->GetTypeName();
76 ValueObjectDynamicValue::GetQualifiedTypeName()
78 const bool success = UpdateValueIfNeeded(false);
81 if (m_dynamic_type_info.HasTypeSP())
82 return GetClangType().GetConstQualifiedTypeName ();
83 if (m_dynamic_type_info.HasName())
84 return m_dynamic_type_info.GetName();
86 return m_parent->GetTypeName();
90 ValueObjectDynamicValue::CalculateNumChildren()
92 const bool success = UpdateValueIfNeeded(false);
93 if (success && m_dynamic_type_info.HasTypeSP())
94 return GetClangType().GetNumChildren (true);
96 return m_parent->GetNumChildren();
100 ValueObjectDynamicValue::GetByteSize()
102 const bool success = UpdateValueIfNeeded(false);
103 if (success && m_dynamic_type_info.HasTypeSP())
104 return m_value.GetValueByteSize(NULL);
106 return m_parent->GetByteSize();
110 ValueObjectDynamicValue::GetValueType() const
112 return m_parent->GetValueType();
116 ValueObjectDynamicValue::UpdateValue ()
118 SetValueIsValid (false);
121 if (!m_parent->UpdateValueIfNeeded(false))
123 // The dynamic value failed to get an error, pass the error along
124 if (m_error.Success() && m_parent->GetError().Fail())
125 m_error = m_parent->GetError();
129 // Setting our type_sp to NULL will route everything back through our
130 // parent which is equivalent to not using dynamic values.
131 if (m_use_dynamic == lldb::eNoDynamicValues)
133 m_dynamic_type_info.Clear();
137 ExecutionContext exe_ctx (GetExecutionContextRef());
138 Target *target = exe_ctx.GetTargetPtr();
141 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
142 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
145 // First make sure our Type and/or Address haven't changed:
146 Process *process = exe_ctx.GetProcessPtr();
150 TypeAndOrName class_type_or_name;
151 Address dynamic_address;
152 bool found_dynamic_type = false;
154 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
155 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
157 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
159 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
163 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
165 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
167 if (!found_dynamic_type)
169 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
171 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
175 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
178 m_update_point.SetUpdated();
180 // If we don't have a dynamic type, then make ourselves just a echo of our parent.
181 // Or we could return false, and make ourselves an echo of our parent?
182 if (!found_dynamic_type)
184 if (m_dynamic_type_info)
185 SetValueDidChange(true);
186 ClearDynamicTypeInformation();
187 m_dynamic_type_info.Clear();
188 m_value = m_parent->GetValue();
189 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
190 return m_error.Success();
193 Value old_value(m_value);
195 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
197 bool has_changed_type = false;
199 if (!m_dynamic_type_info)
201 m_dynamic_type_info = class_type_or_name;
202 has_changed_type = true;
204 else if (class_type_or_name != m_dynamic_type_info)
206 // We are another type, we need to tear down our children...
207 m_dynamic_type_info = class_type_or_name;
208 SetValueDidChange (true);
209 has_changed_type = true;
212 if (has_changed_type)
213 ClearDynamicTypeInformation ();
215 if (!m_address.IsValid() || m_address != dynamic_address)
217 if (m_address.IsValid())
218 SetValueDidChange (true);
220 // We've moved, so we should be fine...
221 m_address = dynamic_address;
222 lldb::TargetSP target_sp (GetTargetSP());
223 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
224 m_value.GetScalar() = load_address;
227 ClangASTType corrected_type;
228 if (m_dynamic_type_info.HasTypeSP())
230 // The type will always be the type of the dynamic object. If our parent's type was a pointer,
231 // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type
233 ClangASTType orig_type = m_dynamic_type_info.GetTypeSP()->GetClangForwardType();
234 corrected_type = orig_type;
235 if (m_parent->IsPointerType())
236 corrected_type = orig_type.GetPointerType ();
237 else if (m_parent->IsPointerOrReferenceType())
238 corrected_type = orig_type.GetLValueReferenceType ();
240 else /*if (m_dynamic_type_info.HasName())*/
242 // If we are here we need to adjust our dynamic type name to include the correct & or * symbol
243 std::string type_name_buf (m_dynamic_type_info.GetName().GetCString());
244 if (m_parent->IsPointerType())
245 type_name_buf.append(" *");
246 else if (m_parent->IsPointerOrReferenceType())
247 type_name_buf.append(" &");
248 corrected_type = m_parent->GetClangType();
249 m_dynamic_type_info.SetName(type_name_buf.c_str());
252 //m_value.SetContext (Value::eContextTypeClangType, corrected_type);
253 m_value.SetClangType (corrected_type);
255 // Our address is the location of the dynamic type stored in memory. It isn't a load address,
256 // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
257 m_value.SetValueType(Value::eValueTypeScalar);
259 if (has_changed_type && log)
260 log->Printf("[%s %p] has a new dynamic type %s",
261 GetName().GetCString(),
263 GetTypeName().GetCString());
265 if (m_address.IsValid() && m_dynamic_type_info)
267 // The variable value is in the Scalar value inside the m_value.
268 // We can point our m_data right to it.
269 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
270 if (m_error.Success())
272 if (GetClangType().IsAggregateType ())
274 // this value object represents an aggregate type whose
275 // children have values, but this object does not. So we
276 // say we are changed if our location has changed.
277 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
280 SetValueIsValid (true);
285 // We get here if we've failed above...
286 SetValueIsValid (false);
293 ValueObjectDynamicValue::IsInScope ()
295 return m_parent->IsInScope();
299 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
301 if (!UpdateValueIfNeeded(false))
303 error.SetErrorString("unable to read value");
307 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
308 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
310 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
312 error.SetErrorString("unable to read value");
316 // if we are at an offset from our parent, in order to set ourselves correctly we would need
317 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
318 // with that - if anything more than a value overwrite is required, you should be using the
319 // expression parser instead of the value editing facility
320 if (my_value != parent_value)
322 // but NULL'ing out a value should always be allowed
323 if (strcmp(value_str,"0"))
325 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
330 bool ret_val = m_parent->SetValueFromCString(value_str,error);
336 ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error)
338 if (!UpdateValueIfNeeded(false))
340 error.SetErrorString("unable to read value");
344 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
345 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
347 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
349 error.SetErrorString("unable to read value");
353 // if we are at an offset from our parent, in order to set ourselves correctly we would need
354 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
355 // with that - if anything more than a value overwrite is required, you should be using the
356 // expression parser instead of the value editing facility
357 if (my_value != parent_value)
359 // but NULL'ing out a value should always be allowed
360 lldb::offset_t offset = 0;
362 if (data.GetPointer(&offset) != 0)
364 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
369 bool ret_val = m_parent->SetData(data, error);