blob: 80c16c84ca41ad9cae1f31af53bbe84ba73b2aa1 [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
Enrico Granatad2284832012-10-17 22:23:56 +000017#include "lldb/Core/Log.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000018#include "lldb/Core/Module.h"
19#include "lldb/Core/ValueObjectList.h"
20#include "lldb/Core/Value.h"
21#include "lldb/Core/ValueObject.h"
22
Greg Claytona1e5dc82015-08-11 22:53:00 +000023#include "lldb/Symbol/CompilerType.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000024#include "lldb/Symbol/ObjectFile.h"
25#include "lldb/Symbol/SymbolContext.h"
26#include "lldb/Symbol/Type.h"
27#include "lldb/Symbol/Variable.h"
28
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"
35
Jim Ingham78a685a2011-04-16 00:01:13 +000036using namespace lldb_private;
37
Jim Ingham2837b762011-05-04 03:43:18 +000038ValueObjectDynamicValue::ValueObjectDynamicValue (ValueObject &parent, lldb::DynamicValueType use_dynamic) :
Jim Ingham78a685a2011-04-16 00:01:13 +000039 ValueObject(parent),
40 m_address (),
Enrico Granataf7b1a342013-01-23 01:17:27 +000041 m_dynamic_type_info(),
Jim Ingham2837b762011-05-04 03:43:18 +000042 m_use_dynamic (use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +000043{
Enrico Granata6f3533f2011-07-29 19:53:35 +000044 SetName (parent.GetName());
Jim Ingham78a685a2011-04-16 00:01:13 +000045}
46
47ValueObjectDynamicValue::~ValueObjectDynamicValue()
48{
49 m_owning_valobj_sp.reset();
50}
51
Greg Claytona1e5dc82015-08-11 22:53:00 +000052CompilerType
Sean Callanan72772842012-02-22 23:57:45 +000053ValueObjectDynamicValue::GetClangTypeImpl ()
Jim Ingham78a685a2011-04-16 00:01:13 +000054{
Enrico Granatadc4db5a2013-10-29 00:28:35 +000055 const bool success = UpdateValueIfNeeded(false);
56 if (success)
57 {
58 if (m_dynamic_type_info.HasType())
59 return m_value.GetClangType();
60 else
61 return m_parent->GetClangType();
62 }
63 return m_parent->GetClangType();
Jim Ingham78a685a2011-04-16 00:01:13 +000064}
65
66ConstString
67ValueObjectDynamicValue::GetTypeName()
68{
Enrico Granatac3e320a2011-08-02 17:27:39 +000069 const bool success = UpdateValueIfNeeded(false);
Enrico Granataf7b1a342013-01-23 01:17:27 +000070 if (success)
71 {
Enrico Granataf7b1a342013-01-23 01:17:27 +000072 if (m_dynamic_type_info.HasName())
73 return m_dynamic_type_info.GetName();
74 }
75 return m_parent->GetTypeName();
76}
77
Enrico Granatadc4db5a2013-10-29 00:28:35 +000078TypeImpl
79ValueObjectDynamicValue::GetTypeImpl ()
80{
81 const bool success = UpdateValueIfNeeded(false);
Enrico Granatadf7c7f92013-10-29 17:42:02 +000082 if (success && m_type_impl.IsValid())
Enrico Granatadc4db5a2013-10-29 00:28:35 +000083 {
84 return m_type_impl;
85 }
86 return m_parent->GetTypeImpl();
87}
88
Enrico Granataf7b1a342013-01-23 01:17:27 +000089ConstString
90ValueObjectDynamicValue::GetQualifiedTypeName()
91{
92 const bool success = UpdateValueIfNeeded(false);
93 if (success)
94 {
Enrico Granataf7b1a342013-01-23 01:17:27 +000095 if (m_dynamic_type_info.HasName())
96 return m_dynamic_type_info.GetName();
97 }
Enrico Granatae8daa2f2014-05-17 19:14:17 +000098 return m_parent->GetQualifiedTypeName();
99}
100
101ConstString
102ValueObjectDynamicValue::GetDisplayTypeName()
103{
104 const bool success = UpdateValueIfNeeded(false);
105 if (success)
106 {
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();
111 }
112 return m_parent->GetDisplayTypeName();
Jim Ingham78a685a2011-04-16 00:01:13 +0000113}
114
Greg Claytonc7bece562013-01-25 18:06:21 +0000115size_t
Jim Ingham78a685a2011-04-16 00:01:13 +0000116ValueObjectDynamicValue::CalculateNumChildren()
117{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000118 const bool success = UpdateValueIfNeeded(false);
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000119 if (success && m_dynamic_type_info.HasType())
Greg Clayton57ee3062013-07-11 22:46:58 +0000120 return GetClangType().GetNumChildren (true);
Jim Ingham78a685a2011-04-16 00:01:13 +0000121 else
122 return m_parent->GetNumChildren();
123}
124
Greg Claytonfaac1112013-03-14 18:31:44 +0000125uint64_t
Jim Ingham78a685a2011-04-16 00:01:13 +0000126ValueObjectDynamicValue::GetByteSize()
127{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000128 const bool success = UpdateValueIfNeeded(false);
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000129 if (success && m_dynamic_type_info.HasType())
Enrico Granata951bdd52015-01-28 01:09:45 +0000130 return m_value.GetValueByteSize(nullptr);
Jim Ingham78a685a2011-04-16 00:01:13 +0000131 else
132 return m_parent->GetByteSize();
133}
134
135lldb::ValueType
136ValueObjectDynamicValue::GetValueType() const
137{
138 return m_parent->GetValueType();
139}
140
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000141
142static TypeAndOrName
143FixupTypeAndOrName (const TypeAndOrName& type_andor_name,
144 ValueObject& parent)
145{
146 TypeAndOrName ret(type_andor_name);
147 if (type_andor_name.HasType())
148 {
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
151 // should be okay...
Greg Claytona1e5dc82015-08-11 22:53:00 +0000152 CompilerType orig_type = type_andor_name.GetCompilerType();
153 CompilerType corrected_type = orig_type;
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000154 if (parent.IsPointerType())
155 corrected_type = orig_type.GetPointerType ();
156 else if (parent.IsPointerOrReferenceType())
Greg Claytond8d4a572015-08-11 21:38:15 +0000157 corrected_type = ClangASTContext::GetLValueReferenceType(orig_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +0000158 ret.SetCompilerType(corrected_type);
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000159 }
160 else /*if (m_dynamic_type_info.HasName())*/
161 {
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(" &");
Enrico Granatafcf0c4e2013-10-31 22:42:00 +0000168 // the parent type should be a correctly pointer'ed or referenc'ed type
Greg Claytona1e5dc82015-08-11 22:53:00 +0000169 ret.SetCompilerType(parent.GetClangType());
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000170 ret.SetName(corrected_name.c_str());
171 }
172 return ret;
173}
174
Jim Ingham78a685a2011-04-16 00:01:13 +0000175bool
176ValueObjectDynamicValue::UpdateValue ()
177{
178 SetValueIsValid (false);
179 m_error.Clear();
180
Enrico Granatac3e320a2011-08-02 17:27:39 +0000181 if (!m_parent->UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +0000182 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000183 // 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();
Jim Ingham78a685a2011-04-16 00:01:13 +0000186 return false;
187 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000188
Jim Ingham2837b762011-05-04 03:43:18 +0000189 // 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)
192 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000193 m_dynamic_type_info.Clear();
Jim Ingham2837b762011-05-04 03:43:18 +0000194 return true;
195 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000196
Greg Claytoncc4d0142012-02-17 07:49:44 +0000197 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytonc14ee322011-09-22 04:58:26 +0000198 Target *target = exe_ctx.GetTargetPtr();
199 if (target)
Jim Ingham78a685a2011-04-16 00:01:13 +0000200 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000201 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
202 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Jim Ingham78a685a2011-04-16 00:01:13 +0000203 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000204
Jim Ingham78a685a2011-04-16 00:01:13 +0000205 // First make sure our Type and/or Address haven't changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +0000206 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham78a685a2011-04-16 00:01:13 +0000207 if (!process)
208 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000209
Jim Ingham61be0902011-05-02 18:13:59 +0000210 TypeAndOrName class_type_or_name;
Jim Ingham78a685a2011-04-16 00:01:13 +0000211 Address dynamic_address;
212 bool found_dynamic_type = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000213
Jim Ingham78a685a2011-04-16 00:01:13 +0000214 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
215 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
216 {
217 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
218 if (runtime)
Jim Ingham2837b762011-05-04 03:43:18 +0000219 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
Jim Ingham78a685a2011-04-16 00:01:13 +0000220 }
221 else
222 {
223 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
224 if (cpp_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +0000225 found_dynamic_type = cpp_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000226
Jim Ingham78a685a2011-04-16 00:01:13 +0000227 if (!found_dynamic_type)
228 {
229 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
230 if (objc_runtime)
Enrico Granata9910bc82011-08-03 02:18:51 +0000231 found_dynamic_type = objc_runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address);
Jim Ingham78a685a2011-04-16 00:01:13 +0000232 }
233 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000234
Jim Ingham61be0902011-05-02 18:13:59 +0000235 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
236 // don't...
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000237
Jim Ingham61be0902011-05-02 18:13:59 +0000238 m_update_point.SetUpdated();
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000239
Enrico Granatadf7c7f92013-10-29 17:42:02 +0000240 if (found_dynamic_type)
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000241 {
Enrico Granatadf7c7f92013-10-29 17:42:02 +0000242 if (class_type_or_name.HasType())
243 {
Greg Claytona1e5dc82015-08-11 22:53:00 +0000244 m_type_impl = TypeImpl(m_parent->GetClangType(),FixupTypeAndOrName(class_type_or_name, *m_parent).GetCompilerType());
Enrico Granatadf7c7f92013-10-29 17:42:02 +0000245 }
246 else
247 {
248 m_type_impl.Clear();
249 }
250 }
251 else
252 {
253 m_type_impl.Clear();
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000254 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000255
Jim Ingham78a685a2011-04-16 00:01:13 +0000256 // 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)
259 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000260 if (m_dynamic_type_info)
Enrico Granata75badc42012-11-27 23:50:00 +0000261 SetValueDidChange(true);
Enrico Granatabd83b872012-11-27 23:28:32 +0000262 ClearDynamicTypeInformation();
Enrico Granataf7b1a342013-01-23 01:17:27 +0000263 m_dynamic_type_info.Clear();
Jim Ingham78a685a2011-04-16 00:01:13 +0000264 m_value = m_parent->GetValue();
Greg Clayton57ee3062013-07-11 22:46:58 +0000265 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000266 return m_error.Success();
267 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000268
Jim Ingham78a685a2011-04-16 00:01:13 +0000269 Value old_value(m_value);
270
Greg Clayton5160ce52013-03-27 23:08:40 +0000271 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000272
Enrico Granatae3e91512012-10-22 18:18:36 +0000273 bool has_changed_type = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000274
Enrico Granataf7b1a342013-01-23 01:17:27 +0000275 if (!m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000276 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000277 m_dynamic_type_info = class_type_or_name;
Enrico Granatae3e91512012-10-22 18:18:36 +0000278 has_changed_type = true;
Jim Ingham78a685a2011-04-16 00:01:13 +0000279 }
Enrico Granataf7b1a342013-01-23 01:17:27 +0000280 else if (class_type_or_name != m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000281 {
282 // We are another type, we need to tear down our children...
Enrico Granataf7b1a342013-01-23 01:17:27 +0000283 m_dynamic_type_info = class_type_or_name;
Jim Ingham78a685a2011-04-16 00:01:13 +0000284 SetValueDidChange (true);
Enrico Granatae3e91512012-10-22 18:18:36 +0000285 has_changed_type = true;
Jim Ingham78a685a2011-04-16 00:01:13 +0000286 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000287
Enrico Granatae3e91512012-10-22 18:18:36 +0000288 if (has_changed_type)
289 ClearDynamicTypeInformation ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000290
Jim Ingham78a685a2011-04-16 00:01:13 +0000291 if (!m_address.IsValid() || m_address != dynamic_address)
292 {
293 if (m_address.IsValid())
294 SetValueDidChange (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000295
Jim Ingham78a685a2011-04-16 00:01:13 +0000296 // We've moved, so we should be fine...
297 m_address = dynamic_address;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000298 lldb::TargetSP target_sp (GetTargetSP());
299 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000300 m_value.GetScalar() = load_address;
301 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000302
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000303 m_dynamic_type_info = FixupTypeAndOrName(m_dynamic_type_info, *m_parent);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000304
Greg Clayton57ee3062013-07-11 22:46:58 +0000305 //m_value.SetContext (Value::eContextTypeClangType, corrected_type);
Greg Claytona1e5dc82015-08-11 22:53:00 +0000306 m_value.SetClangType (m_dynamic_type_info.GetCompilerType());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000307
Jim Ingham78a685a2011-04-16 00:01:13 +0000308 // 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);
311
Enrico Granatae3e91512012-10-22 18:18:36 +0000312 if (has_changed_type && log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000313 log->Printf("[%s %p] has a new dynamic type %s", GetName().GetCString(),
314 static_cast<void*>(this), GetTypeName().GetCString());
315
Enrico Granataf7b1a342013-01-23 01:17:27 +0000316 if (m_address.IsValid() && m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000317 {
318 // The variable value is in the Scalar value inside the m_value.
319 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000320 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000321 if (m_error.Success())
322 {
Enrico Granatad07cfd32014-10-08 18:27:36 +0000323 if (!CanProvideValue())
Jim Ingham78a685a2011-04-16 00:01:13 +0000324 {
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());
329 }
330
331 SetValueIsValid (true);
332 return true;
333 }
334 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000335
Jim Ingham78a685a2011-04-16 00:01:13 +0000336 // We get here if we've failed above...
337 SetValueIsValid (false);
338 return false;
339}
340
341
342
343bool
344ValueObjectDynamicValue::IsInScope ()
345{
346 return m_parent->IsInScope();
347}
348
Enrico Granata07a4ac22012-05-08 21:25:06 +0000349bool
350ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
351{
352 if (!UpdateValueIfNeeded(false))
353 {
354 error.SetErrorString("unable to read value");
355 return false;
356 }
357
358 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
359 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
360
361 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
362 {
363 error.SetErrorString("unable to read value");
364 return false;
365 }
366
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)
372 {
373 // but NULL'ing out a value should always be allowed
374 if (strcmp(value_str,"0"))
375 {
376 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
377 return false;
378 }
379 }
380
381 bool ret_val = m_parent->SetValueFromCString(value_str,error);
382 SetNeedsUpdate();
383 return ret_val;
384}
Sean Callanan389823e2013-04-13 01:21:23 +0000385
386bool
387ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error)
388{
389 if (!UpdateValueIfNeeded(false))
390 {
391 error.SetErrorString("unable to read value");
392 return false;
393 }
394
395 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
396 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
397
398 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
399 {
400 error.SetErrorString("unable to read value");
401 return false;
402 }
403
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)
409 {
410 // but NULL'ing out a value should always be allowed
411 lldb::offset_t offset = 0;
412
413 if (data.GetPointer(&offset) != 0)
414 {
415 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
416 return false;
417 }
418 }
419
420 bool ret_val = m_parent->SetData(data, error);
421 SetNeedsUpdate();
422 return ret_val;
423}