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 const bool success = UpdateValueIfNeeded(false);
58 if (m_dynamic_type_info.HasType())
59 return m_value.GetClangType();
61 return m_parent->GetClangType();
63 return m_parent->GetClangType();
67 ValueObjectDynamicValue::GetTypeName()
69 const bool success = UpdateValueIfNeeded(false);
72 if (m_dynamic_type_info.HasName())
73 return m_dynamic_type_info.GetName();
74 if (m_dynamic_type_info.HasType())
75 return GetClangType().GetConstTypeName();
77 return m_parent->GetTypeName();
81 ValueObjectDynamicValue::GetTypeImpl ()
83 const bool success = UpdateValueIfNeeded(false);
84 if (success && m_type_impl.IsValid())
88 return m_parent->GetTypeImpl();
92 ValueObjectDynamicValue::GetQualifiedTypeName()
94 const bool success = UpdateValueIfNeeded(false);
97 if (m_dynamic_type_info.HasName())
98 return m_dynamic_type_info.GetName();
99 if (m_dynamic_type_info.HasType())
100 return GetClangType().GetConstQualifiedTypeName ();
102 return m_parent->GetTypeName();
106 ValueObjectDynamicValue::CalculateNumChildren()
108 const bool success = UpdateValueIfNeeded(false);
109 if (success && m_dynamic_type_info.HasType())
110 return GetClangType().GetNumChildren (true);
112 return m_parent->GetNumChildren();
116 ValueObjectDynamicValue::GetByteSize()
118 const bool success = UpdateValueIfNeeded(false);
119 if (success && m_dynamic_type_info.HasType())
120 return m_value.GetValueByteSize(NULL);
122 return m_parent->GetByteSize();
126 ValueObjectDynamicValue::GetValueType() const
128 return m_parent->GetValueType();
133 FixupTypeAndOrName (const TypeAndOrName& type_andor_name,
136 TypeAndOrName ret(type_andor_name);
137 if (type_andor_name.HasType())
139 // The type will always be the type of the dynamic object. If our parent's type was a pointer,
140 // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type
142 ClangASTType orig_type = type_andor_name.GetClangASTType();
143 ClangASTType corrected_type = orig_type;
144 if (parent.IsPointerType())
145 corrected_type = orig_type.GetPointerType ();
146 else if (parent.IsPointerOrReferenceType())
147 corrected_type = orig_type.GetLValueReferenceType ();
148 ret.SetClangASTType(corrected_type);
150 else /*if (m_dynamic_type_info.HasName())*/
152 // If we are here we need to adjust our dynamic type name to include the correct & or * symbol
153 std::string corrected_name (type_andor_name.GetName().GetCString());
154 if (parent.IsPointerType())
155 corrected_name.append(" *");
156 else if (parent.IsPointerOrReferenceType())
157 corrected_name.append(" &");
158 // the parent type should be a correctly pointer'ed or referenc'ed type
159 ret.SetClangASTType(parent.GetClangType());
160 ret.SetName(corrected_name.c_str());
166 ValueObjectDynamicValue::UpdateValue ()
168 SetValueIsValid (false);
171 if (!m_parent->UpdateValueIfNeeded(false))
173 // The dynamic value failed to get an error, pass the error along
174 if (m_error.Success() && m_parent->GetError().Fail())
175 m_error = m_parent->GetError();
179 // Setting our type_sp to NULL will route everything back through our
180 // parent which is equivalent to not using dynamic values.
181 if (m_use_dynamic == lldb::eNoDynamicValues)
183 m_dynamic_type_info.Clear();
187 ExecutionContext exe_ctx (GetExecutionContextRef());
188 Target *target = exe_ctx.GetTargetPtr();
191 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
192 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
195 // First make sure our Type and/or Address haven't changed:
196 Process *process = exe_ctx.GetProcessPtr();
200 TypeAndOrName class_type_or_name;
201 Address dynamic_address;
202 bool found_dynamic_type = false;
204 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
205 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
207 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
209 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
213 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
215 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
217 if (!found_dynamic_type)
219 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
221 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
225 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
228 m_update_point.SetUpdated();
230 if (found_dynamic_type)
232 if (class_type_or_name.HasType())
234 // TypeSP are always generated from debug info
235 if (!class_type_or_name.HasTypeSP() && class_type_or_name.GetClangASTType().IsRuntimeGeneratedType())
237 m_type_impl = TypeImpl(m_parent->GetClangType(),FixupTypeAndOrName(class_type_or_name, *m_parent).GetClangASTType());
238 class_type_or_name.SetClangASTType(ClangASTType());
242 m_type_impl = TypeImpl(FixupTypeAndOrName(class_type_or_name, *m_parent).GetClangASTType());
255 // If we don't have a dynamic type, then make ourselves just a echo of our parent.
256 // Or we could return false, and make ourselves an echo of our parent?
257 if (!found_dynamic_type)
259 if (m_dynamic_type_info)
260 SetValueDidChange(true);
261 ClearDynamicTypeInformation();
262 m_dynamic_type_info.Clear();
263 m_value = m_parent->GetValue();
264 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
265 return m_error.Success();
268 Value old_value(m_value);
270 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
272 bool has_changed_type = false;
274 if (!m_dynamic_type_info)
276 m_dynamic_type_info = class_type_or_name;
277 has_changed_type = true;
279 else if (class_type_or_name != m_dynamic_type_info)
281 // We are another type, we need to tear down our children...
282 m_dynamic_type_info = class_type_or_name;
283 SetValueDidChange (true);
284 has_changed_type = true;
287 if (has_changed_type)
288 ClearDynamicTypeInformation ();
290 if (!m_address.IsValid() || m_address != dynamic_address)
292 if (m_address.IsValid())
293 SetValueDidChange (true);
295 // We've moved, so we should be fine...
296 m_address = dynamic_address;
297 lldb::TargetSP target_sp (GetTargetSP());
298 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
299 m_value.GetScalar() = load_address;
302 m_dynamic_type_info = FixupTypeAndOrName(m_dynamic_type_info, *m_parent);
304 //m_value.SetContext (Value::eContextTypeClangType, corrected_type);
305 m_value.SetClangType (m_dynamic_type_info.GetClangASTType());
307 // Our address is the location of the dynamic type stored in memory. It isn't a load address,
308 // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
309 m_value.SetValueType(Value::eValueTypeScalar);
311 if (has_changed_type && log)
312 log->Printf("[%s %p] has a new dynamic type %s",
313 GetName().GetCString(),
315 GetTypeName().GetCString());
317 if (m_address.IsValid() && m_dynamic_type_info)
319 // The variable value is in the Scalar value inside the m_value.
320 // We can point our m_data right to it.
321 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
322 if (m_error.Success())
324 if (GetClangType().IsAggregateType ())
326 // this value object represents an aggregate type whose
327 // children have values, but this object does not. So we
328 // say we are changed if our location has changed.
329 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
332 SetValueIsValid (true);
337 // We get here if we've failed above...
338 SetValueIsValid (false);
345 ValueObjectDynamicValue::IsInScope ()
347 return m_parent->IsInScope();
351 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
353 if (!UpdateValueIfNeeded(false))
355 error.SetErrorString("unable to read value");
359 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
360 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
362 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
364 error.SetErrorString("unable to read value");
368 // if we are at an offset from our parent, in order to set ourselves correctly we would need
369 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
370 // with that - if anything more than a value overwrite is required, you should be using the
371 // expression parser instead of the value editing facility
372 if (my_value != parent_value)
374 // but NULL'ing out a value should always be allowed
375 if (strcmp(value_str,"0"))
377 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
382 bool ret_val = m_parent->SetValueFromCString(value_str,error);
388 ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error)
390 if (!UpdateValueIfNeeded(false))
392 error.SetErrorString("unable to read value");
396 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
397 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
399 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
401 error.SetErrorString("unable to read value");
405 // if we are at an offset from our parent, in order to set ourselves correctly we would need
406 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
407 // with that - if anything more than a value overwrite is required, you should be using the
408 // expression parser instead of the value editing facility
409 if (my_value != parent_value)
411 // but NULL'ing out a value should always be allowed
412 lldb::offset_t offset = 0;
414 if (data.GetPointer(&offset) != 0)
416 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
421 bool ret_val = m_parent->SetData(data, error);