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();
75 return m_parent->GetTypeName();
79 ValueObjectDynamicValue::GetTypeImpl ()
81 const bool success = UpdateValueIfNeeded(false);
82 if (success && m_type_impl.IsValid())
86 return m_parent->GetTypeImpl();
90 ValueObjectDynamicValue::GetQualifiedTypeName()
92 const bool success = UpdateValueIfNeeded(false);
95 if (m_dynamic_type_info.HasName())
96 return m_dynamic_type_info.GetName();
98 return m_parent->GetQualifiedTypeName();
102 ValueObjectDynamicValue::GetDisplayTypeName()
104 const bool success = UpdateValueIfNeeded(false);
107 if (m_dynamic_type_info.HasType())
108 return GetClangType().GetDisplayTypeName();
109 if (m_dynamic_type_info.HasName())
110 return m_dynamic_type_info.GetName();
112 return m_parent->GetDisplayTypeName();
116 ValueObjectDynamicValue::CalculateNumChildren()
118 const bool success = UpdateValueIfNeeded(false);
119 if (success && m_dynamic_type_info.HasType())
120 return GetClangType().GetNumChildren (true);
122 return m_parent->GetNumChildren();
126 ValueObjectDynamicValue::GetByteSize()
128 const bool success = UpdateValueIfNeeded(false);
129 if (success && m_dynamic_type_info.HasType())
130 return m_value.GetValueByteSize(nullptr);
132 return m_parent->GetByteSize();
136 ValueObjectDynamicValue::GetValueType() const
138 return m_parent->GetValueType();
143 FixupTypeAndOrName (const TypeAndOrName& type_andor_name,
146 TypeAndOrName ret(type_andor_name);
147 if (type_andor_name.HasType())
149 // The type will always be the type of the dynamic object. If our parent's type was a pointer,
150 // then our type should be a pointer to the type of the dynamic object. If a reference, then the original type
152 ClangASTType orig_type = type_andor_name.GetClangASTType();
153 ClangASTType corrected_type = orig_type;
154 if (parent.IsPointerType())
155 corrected_type = orig_type.GetPointerType ();
156 else if (parent.IsPointerOrReferenceType())
157 corrected_type = orig_type.GetLValueReferenceType ();
158 ret.SetClangASTType(corrected_type);
160 else /*if (m_dynamic_type_info.HasName())*/
162 // If we are here we need to adjust our dynamic type name to include the correct & or * symbol
163 std::string corrected_name (type_andor_name.GetName().GetCString());
164 if (parent.IsPointerType())
165 corrected_name.append(" *");
166 else if (parent.IsPointerOrReferenceType())
167 corrected_name.append(" &");
168 // the parent type should be a correctly pointer'ed or referenc'ed type
169 ret.SetClangASTType(parent.GetClangType());
170 ret.SetName(corrected_name.c_str());
176 ValueObjectDynamicValue::UpdateValue ()
178 SetValueIsValid (false);
181 if (!m_parent->UpdateValueIfNeeded(false))
183 // The dynamic value failed to get an error, pass the error along
184 if (m_error.Success() && m_parent->GetError().Fail())
185 m_error = m_parent->GetError();
189 // Setting our type_sp to NULL will route everything back through our
190 // parent which is equivalent to not using dynamic values.
191 if (m_use_dynamic == lldb::eNoDynamicValues)
193 m_dynamic_type_info.Clear();
197 ExecutionContext exe_ctx (GetExecutionContextRef());
198 Target *target = exe_ctx.GetTargetPtr();
201 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
202 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
205 // First make sure our Type and/or Address haven't changed:
206 Process *process = exe_ctx.GetProcessPtr();
210 TypeAndOrName class_type_or_name;
211 Address dynamic_address;
212 bool found_dynamic_type = false;
214 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
215 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
217 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
219 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
223 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
225 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
227 if (!found_dynamic_type)
229 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
231 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
235 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
238 m_update_point.SetUpdated();
240 if (found_dynamic_type)
242 if (class_type_or_name.HasType())
244 m_type_impl = TypeImpl(m_parent->GetClangType(),FixupTypeAndOrName(class_type_or_name, *m_parent).GetClangASTType());
256 // If we don't have a dynamic type, then make ourselves just a echo of our parent.
257 // Or we could return false, and make ourselves an echo of our parent?
258 if (!found_dynamic_type)
260 if (m_dynamic_type_info)
261 SetValueDidChange(true);
262 ClearDynamicTypeInformation();
263 m_dynamic_type_info.Clear();
264 m_value = m_parent->GetValue();
265 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
266 return m_error.Success();
269 Value old_value(m_value);
271 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
273 bool has_changed_type = false;
275 if (!m_dynamic_type_info)
277 m_dynamic_type_info = class_type_or_name;
278 has_changed_type = true;
280 else if (class_type_or_name != m_dynamic_type_info)
282 // We are another type, we need to tear down our children...
283 m_dynamic_type_info = class_type_or_name;
284 SetValueDidChange (true);
285 has_changed_type = true;
288 if (has_changed_type)
289 ClearDynamicTypeInformation ();
291 if (!m_address.IsValid() || m_address != dynamic_address)
293 if (m_address.IsValid())
294 SetValueDidChange (true);
296 // We've moved, so we should be fine...
297 m_address = dynamic_address;
298 lldb::TargetSP target_sp (GetTargetSP());
299 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
300 m_value.GetScalar() = load_address;
303 m_dynamic_type_info = FixupTypeAndOrName(m_dynamic_type_info, *m_parent);
305 //m_value.SetContext (Value::eContextTypeClangType, corrected_type);
306 m_value.SetClangType (m_dynamic_type_info.GetClangASTType());
308 // Our address is the location of the dynamic type stored in memory. It isn't a load address,
309 // because we aren't pointing to the LOCATION that stores the pointer to us, we're pointing to us...
310 m_value.SetValueType(Value::eValueTypeScalar);
312 if (has_changed_type && log)
313 log->Printf("[%s %p] has a new dynamic type %s", GetName().GetCString(),
314 static_cast<void*>(this), GetTypeName().GetCString());
316 if (m_address.IsValid() && m_dynamic_type_info)
318 // The variable value is in the Scalar value inside the m_value.
319 // We can point our m_data right to it.
320 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
321 if (m_error.Success())
323 if (!CanProvideValue())
325 // this value object represents an aggregate type whose
326 // children have values, but this object does not. So we
327 // say we are changed if our location has changed.
328 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
331 SetValueIsValid (true);
336 // We get here if we've failed above...
337 SetValueIsValid (false);
344 ValueObjectDynamicValue::IsInScope ()
346 return m_parent->IsInScope();
350 ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
352 if (!UpdateValueIfNeeded(false))
354 error.SetErrorString("unable to read value");
358 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
359 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
361 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
363 error.SetErrorString("unable to read value");
367 // if we are at an offset from our parent, in order to set ourselves correctly we would need
368 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
369 // with that - if anything more than a value overwrite is required, you should be using the
370 // expression parser instead of the value editing facility
371 if (my_value != parent_value)
373 // but NULL'ing out a value should always be allowed
374 if (strcmp(value_str,"0"))
376 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
381 bool ret_val = m_parent->SetValueFromCString(value_str,error);
387 ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error)
389 if (!UpdateValueIfNeeded(false))
391 error.SetErrorString("unable to read value");
395 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
396 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
398 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
400 error.SetErrorString("unable to read value");
404 // if we are at an offset from our parent, in order to set ourselves correctly we would need
405 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
406 // with that - if anything more than a value overwrite is required, you should be using the
407 // expression parser instead of the value editing facility
408 if (my_value != parent_value)
410 // but NULL'ing out a value should always be allowed
411 lldb::offset_t offset = 0;
413 if (data.GetPointer(&offset) != 0)
415 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
420 bool ret_val = m_parent->SetData(data, error);