blob: e801bd7143425152a9c030e0eb3e646f6fc08d35 [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
Greg Clayton99558cc42015-08-24 23:46:31 +000053ValueObjectDynamicValue::GetCompilerTypeImpl ()
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())
Greg Clayton99558cc42015-08-24 23:46:31 +000059 return m_value.GetCompilerType();
Enrico Granatadc4db5a2013-10-29 00:28:35 +000060 else
Greg Clayton99558cc42015-08-24 23:46:31 +000061 return m_parent->GetCompilerType();
Enrico Granatadc4db5a2013-10-29 00:28:35 +000062 }
Greg Clayton99558cc42015-08-24 23:46:31 +000063 return m_parent->GetCompilerType();
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())
Greg Clayton99558cc42015-08-24 23:46:31 +0000108 return GetCompilerType().GetDisplayTypeName();
Enrico Granatae8daa2f2014-05-17 19:14:17 +0000109 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 Clayton99558cc42015-08-24 23:46:31 +0000120 return GetCompilerType().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
141bool
142ValueObjectDynamicValue::UpdateValue ()
143{
144 SetValueIsValid (false);
145 m_error.Clear();
146
Enrico Granatac3e320a2011-08-02 17:27:39 +0000147 if (!m_parent->UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +0000148 {
Greg Clayton007d5be2011-05-30 00:49:24 +0000149 // The dynamic value failed to get an error, pass the error along
150 if (m_error.Success() && m_parent->GetError().Fail())
151 m_error = m_parent->GetError();
Jim Ingham78a685a2011-04-16 00:01:13 +0000152 return false;
153 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000154
Jim Ingham2837b762011-05-04 03:43:18 +0000155 // Setting our type_sp to NULL will route everything back through our
156 // parent which is equivalent to not using dynamic values.
157 if (m_use_dynamic == lldb::eNoDynamicValues)
158 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000159 m_dynamic_type_info.Clear();
Jim Ingham2837b762011-05-04 03:43:18 +0000160 return true;
161 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000162
Greg Claytoncc4d0142012-02-17 07:49:44 +0000163 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytonc14ee322011-09-22 04:58:26 +0000164 Target *target = exe_ctx.GetTargetPtr();
165 if (target)
Jim Ingham78a685a2011-04-16 00:01:13 +0000166 {
Greg Claytonc14ee322011-09-22 04:58:26 +0000167 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
168 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
Jim Ingham78a685a2011-04-16 00:01:13 +0000169 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000170
Jim Ingham78a685a2011-04-16 00:01:13 +0000171 // First make sure our Type and/or Address haven't changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +0000172 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham78a685a2011-04-16 00:01:13 +0000173 if (!process)
174 return false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000175
Jim Ingham61be0902011-05-02 18:13:59 +0000176 TypeAndOrName class_type_or_name;
Jim Ingham78a685a2011-04-16 00:01:13 +0000177 Address dynamic_address;
178 bool found_dynamic_type = false;
Enrico Granata0b6003f2015-09-17 22:56:38 +0000179 Value::ValueType value_type;
Enrico Granatac74275b2015-09-22 19:45:52 +0000180
181 LanguageRuntime *runtime = nullptr;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000182
Jim Ingham78a685a2011-04-16 00:01:13 +0000183 lldb::LanguageType known_type = m_parent->GetObjectRuntimeLanguage();
184 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
185 {
Enrico Granatac74275b2015-09-22 19:45:52 +0000186 runtime = process->GetLanguageRuntime (known_type);
Jim Ingham78a685a2011-04-16 00:01:13 +0000187 if (runtime)
Enrico Granata0b6003f2015-09-17 22:56:38 +0000188 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address, value_type);
Jim Ingham78a685a2011-04-16 00:01:13 +0000189 }
190 else
191 {
Enrico Granatac74275b2015-09-22 19:45:52 +0000192 runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
193 if (runtime)
194 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address, value_type);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000195
Jim Ingham78a685a2011-04-16 00:01:13 +0000196 if (!found_dynamic_type)
197 {
Enrico Granatac74275b2015-09-22 19:45:52 +0000198 runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
199 if (runtime)
200 found_dynamic_type = runtime->GetDynamicTypeAndAddress (*m_parent, m_use_dynamic, class_type_or_name, dynamic_address, value_type);
Jim Ingham78a685a2011-04-16 00:01:13 +0000201 }
202 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000203
Jim Ingham61be0902011-05-02 18:13:59 +0000204 // Getting the dynamic value may have run the program a bit, and so marked us as needing updating, but we really
205 // don't...
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000206
Jim Ingham61be0902011-05-02 18:13:59 +0000207 m_update_point.SetUpdated();
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000208
Enrico Granatac74275b2015-09-22 19:45:52 +0000209 if (runtime && found_dynamic_type)
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000210 {
Enrico Granatadf7c7f92013-10-29 17:42:02 +0000211 if (class_type_or_name.HasType())
212 {
Enrico Granatac74275b2015-09-22 19:45:52 +0000213 m_type_impl = TypeImpl(m_parent->GetCompilerType(),
Enrico Granata7eed4872015-09-22 19:58:02 +0000214 runtime->FixUpDynamicType(class_type_or_name, *m_parent).GetCompilerType());
Enrico Granatadf7c7f92013-10-29 17:42:02 +0000215 }
216 else
217 {
218 m_type_impl.Clear();
219 }
220 }
221 else
222 {
223 m_type_impl.Clear();
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000224 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000225
Jim Ingham78a685a2011-04-16 00:01:13 +0000226 // If we don't have a dynamic type, then make ourselves just a echo of our parent.
227 // Or we could return false, and make ourselves an echo of our parent?
228 if (!found_dynamic_type)
229 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000230 if (m_dynamic_type_info)
Enrico Granata75badc42012-11-27 23:50:00 +0000231 SetValueDidChange(true);
Enrico Granatabd83b872012-11-27 23:28:32 +0000232 ClearDynamicTypeInformation();
Enrico Granataf7b1a342013-01-23 01:17:27 +0000233 m_dynamic_type_info.Clear();
Jim Ingham78a685a2011-04-16 00:01:13 +0000234 m_value = m_parent->GetValue();
Greg Clayton57ee3062013-07-11 22:46:58 +0000235 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000236 return m_error.Success();
237 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000238
Jim Ingham78a685a2011-04-16 00:01:13 +0000239 Value old_value(m_value);
240
Greg Clayton5160ce52013-03-27 23:08:40 +0000241 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000242
Enrico Granatae3e91512012-10-22 18:18:36 +0000243 bool has_changed_type = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000244
Enrico Granataf7b1a342013-01-23 01:17:27 +0000245 if (!m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000246 {
Enrico Granataf7b1a342013-01-23 01:17:27 +0000247 m_dynamic_type_info = class_type_or_name;
Enrico Granatae3e91512012-10-22 18:18:36 +0000248 has_changed_type = true;
Jim Ingham78a685a2011-04-16 00:01:13 +0000249 }
Enrico Granataf7b1a342013-01-23 01:17:27 +0000250 else if (class_type_or_name != m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000251 {
252 // We are another type, we need to tear down our children...
Enrico Granataf7b1a342013-01-23 01:17:27 +0000253 m_dynamic_type_info = class_type_or_name;
Jim Ingham78a685a2011-04-16 00:01:13 +0000254 SetValueDidChange (true);
Enrico Granatae3e91512012-10-22 18:18:36 +0000255 has_changed_type = true;
Jim Ingham78a685a2011-04-16 00:01:13 +0000256 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000257
Enrico Granatae3e91512012-10-22 18:18:36 +0000258 if (has_changed_type)
259 ClearDynamicTypeInformation ();
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000260
Jim Ingham78a685a2011-04-16 00:01:13 +0000261 if (!m_address.IsValid() || m_address != dynamic_address)
262 {
263 if (m_address.IsValid())
264 SetValueDidChange (true);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000265
Jim Ingham78a685a2011-04-16 00:01:13 +0000266 // We've moved, so we should be fine...
267 m_address = dynamic_address;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000268 lldb::TargetSP target_sp (GetTargetSP());
269 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000270 m_value.GetScalar() = load_address;
271 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000272
Enrico Granatac74275b2015-09-22 19:45:52 +0000273 if (runtime)
Enrico Granata7eed4872015-09-22 19:58:02 +0000274 m_dynamic_type_info = runtime->FixUpDynamicType(m_dynamic_type_info, *m_parent);
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000275
Greg Clayton57ee3062013-07-11 22:46:58 +0000276 //m_value.SetContext (Value::eContextTypeClangType, corrected_type);
Greg Clayton99558cc42015-08-24 23:46:31 +0000277 m_value.SetCompilerType (m_dynamic_type_info.GetCompilerType());
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000278
Enrico Granata0b6003f2015-09-17 22:56:38 +0000279 m_value.SetValueType(value_type);
Jim Ingham78a685a2011-04-16 00:01:13 +0000280
Enrico Granatae3e91512012-10-22 18:18:36 +0000281 if (has_changed_type && log)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000282 log->Printf("[%s %p] has a new dynamic type %s", GetName().GetCString(),
283 static_cast<void*>(this), GetTypeName().GetCString());
284
Enrico Granataf7b1a342013-01-23 01:17:27 +0000285 if (m_address.IsValid() && m_dynamic_type_info)
Jim Ingham78a685a2011-04-16 00:01:13 +0000286 {
287 // The variable value is in the Scalar value inside the m_value.
288 // We can point our m_data right to it.
Greg Clayton57ee3062013-07-11 22:46:58 +0000289 m_error = m_value.GetValueAsData (&exe_ctx, m_data, 0, GetModule().get());
Jim Ingham78a685a2011-04-16 00:01:13 +0000290 if (m_error.Success())
291 {
Enrico Granatad07cfd32014-10-08 18:27:36 +0000292 if (!CanProvideValue())
Jim Ingham78a685a2011-04-16 00:01:13 +0000293 {
294 // this value object represents an aggregate type whose
295 // children have values, but this object does not. So we
296 // say we are changed if our location has changed.
297 SetValueDidChange (m_value.GetValueType() != old_value.GetValueType() || m_value.GetScalar() != old_value.GetScalar());
298 }
299
300 SetValueIsValid (true);
301 return true;
302 }
303 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000304
Jim Ingham78a685a2011-04-16 00:01:13 +0000305 // We get here if we've failed above...
306 SetValueIsValid (false);
307 return false;
308}
309
310
311
312bool
313ValueObjectDynamicValue::IsInScope ()
314{
315 return m_parent->IsInScope();
316}
317
Enrico Granata07a4ac22012-05-08 21:25:06 +0000318bool
319ValueObjectDynamicValue::SetValueFromCString (const char *value_str, Error& error)
320{
321 if (!UpdateValueIfNeeded(false))
322 {
323 error.SetErrorString("unable to read value");
324 return false;
325 }
326
327 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
328 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
329
330 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
331 {
332 error.SetErrorString("unable to read value");
333 return false;
334 }
335
336 // if we are at an offset from our parent, in order to set ourselves correctly we would need
337 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
338 // with that - if anything more than a value overwrite is required, you should be using the
339 // expression parser instead of the value editing facility
340 if (my_value != parent_value)
341 {
342 // but NULL'ing out a value should always be allowed
343 if (strcmp(value_str,"0"))
344 {
345 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
346 return false;
347 }
348 }
349
350 bool ret_val = m_parent->SetValueFromCString(value_str,error);
351 SetNeedsUpdate();
352 return ret_val;
353}
Sean Callanan389823e2013-04-13 01:21:23 +0000354
355bool
356ValueObjectDynamicValue::SetData (DataExtractor &data, Error &error)
357{
358 if (!UpdateValueIfNeeded(false))
359 {
360 error.SetErrorString("unable to read value");
361 return false;
362 }
363
364 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
365 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
366
367 if (my_value == UINT64_MAX || parent_value == UINT64_MAX)
368 {
369 error.SetErrorString("unable to read value");
370 return false;
371 }
372
373 // if we are at an offset from our parent, in order to set ourselves correctly we would need
374 // to change the new value so that it refers to the correct dynamic type. we choose not to deal
375 // with that - if anything more than a value overwrite is required, you should be using the
376 // expression parser instead of the value editing facility
377 if (my_value != parent_value)
378 {
379 // but NULL'ing out a value should always be allowed
380 lldb::offset_t offset = 0;
381
382 if (data.GetPointer(&offset) != 0)
383 {
384 error.SetErrorString("unable to modify dynamic value, use 'expression' command");
385 return false;
386 }
387 }
388
389 bool ret_val = m_parent->SetData(data, error);
390 SetNeedsUpdate();
391 return ret_val;
392}
Siva Chandra9851b1f2015-08-18 17:56:06 +0000393
394bool
395ValueObjectDynamicValue::GetDeclaration (Declaration &decl)
396{
397 if (m_parent)
398 return m_parent->GetDeclaration(decl);
399
400 return ValueObject::GetDeclaration(decl);
401}