blob: e6c7d485b9a39acd4e13d729f621f0749e283bb1 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- ValueObject.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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/Core/ValueObject.h"
13
14// C Includes
Greg Claytonf5e56de2010-09-14 23:36:40 +000015#include <stdlib.h>
16
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017// C++ Includes
18// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "llvm/Support/raw_ostream.h"
Jim Ingham5a369122010-09-28 01:25:32 +000020#include "clang/AST/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
22// Project includes
23#include "lldb/Core/DataBufferHeap.h"
Enrico Granata4becb372011-06-29 22:27:15 +000024#include "lldb/Core/Debugger.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000025#include "lldb/Core/Log.h"
Greg Clayton1f746072012-08-29 21:13:06 +000026#include "lldb/Core/Module.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/StreamString.h"
Enrico Granata21fd13f2012-10-27 02:05:48 +000028#include "lldb/Core/ValueObjectCast.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000030#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000031#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Core/ValueObjectList.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000033#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000034#include "lldb/Core/ValueObjectSyntheticFilter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035
Enrico Granata5548cb52013-01-28 23:47:25 +000036#include "lldb/DataFormatters/DataVisualization.h"
Enrico Granata2206b482014-10-30 18:27:31 +000037#include "lldb/DataFormatters/StringPrinter.h"
Enrico Granata4d93b8c2013-09-30 19:11:51 +000038#include "lldb/DataFormatters/ValueObjectPrinter.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000039
Greg Clayton7fb56d02011-02-01 01:31:41 +000040#include "lldb/Host/Endian.h"
41
Enrico Granata61a80ba2011-08-12 16:42:31 +000042#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granataf2bbf712011-07-15 02:26:42 +000043#include "lldb/Interpreter/ScriptInterpreterPython.h"
44
Greg Claytone1a916a2010-07-21 22:12:05 +000045#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Symbol/ClangASTContext.h"
47#include "lldb/Symbol/Type.h"
48
Jim Ingham53c47f12010-09-10 23:12:17 +000049#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000050#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000051#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "lldb/Target/Process.h"
53#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000054#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000055#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58using namespace lldb;
59using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000060using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
Greg Claytonafacd142011-09-02 01:15:17 +000062static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063
64//----------------------------------------------------------------------
65// ValueObject constructor
66//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000067ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000069 m_parent (&parent),
Enrico Granata4873e522013-04-11 22:48:58 +000070 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000071 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072 m_name (),
73 m_data (),
74 m_value (),
75 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000076 m_value_str (),
77 m_old_value_str (),
78 m_location_str (),
79 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000080 m_object_desc_str (),
Enrico Granata744794a2014-09-05 21:46:22 +000081 m_validation_result(),
Jim Ingham58b59f92011-04-22 23:53:53 +000082 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000083 m_children (),
84 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000085 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000086 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000087 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000088 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +000089 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000090 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +000091 m_type_summary_sp(),
92 m_type_format_sp(),
93 m_synthetic_children_sp(),
Enrico Granata744794a2014-09-05 21:46:22 +000094 m_type_validator_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +000095 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000096 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
Greg Clayton288bdf92010-09-02 02:59:18 +000097 m_value_is_valid (false),
98 m_value_did_change (false),
99 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000100 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000101 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000102 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000103 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000104 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000105 m_is_getting_summary(false),
106 m_did_calculate_complete_objc_class_type(false)
Jim Ingham6035b672011-03-31 00:19:25 +0000107{
Jim Ingham58b59f92011-04-22 23:53:53 +0000108 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000109}
110
111//----------------------------------------------------------------------
112// ValueObject constructor
113//----------------------------------------------------------------------
Enrico Granata9128ee22011-09-06 19:20:51 +0000114ValueObject::ValueObject (ExecutionContextScope *exe_scope,
115 AddressType child_ptr_or_ref_addr_type) :
Jim Ingham6035b672011-03-31 00:19:25 +0000116 UserID (++g_value_obj_uid), // Unique identifier for every value object
117 m_parent (NULL),
Enrico Granata4873e522013-04-11 22:48:58 +0000118 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000119 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000120 m_name (),
121 m_data (),
122 m_value (),
123 m_error (),
124 m_value_str (),
125 m_old_value_str (),
126 m_location_str (),
127 m_summary_str (),
128 m_object_desc_str (),
Enrico Granata744794a2014-09-05 21:46:22 +0000129 m_validation_result(),
Jim Ingham58b59f92011-04-22 23:53:53 +0000130 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000131 m_children (),
132 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000133 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000134 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000135 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000136 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +0000137 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000138 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +0000139 m_type_summary_sp(),
140 m_type_format_sp(),
141 m_synthetic_children_sp(),
Enrico Granata744794a2014-09-05 21:46:22 +0000142 m_type_validator_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +0000143 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000144 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
Jim Ingham6035b672011-03-31 00:19:25 +0000145 m_value_is_valid (false),
146 m_value_did_change (false),
147 m_children_count_valid (false),
148 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000149 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000150 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000151 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000152 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000153 m_is_getting_summary(false),
154 m_did_calculate_complete_objc_class_type(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000155{
Jim Ingham58b59f92011-04-22 23:53:53 +0000156 m_manager = new ValueObjectManager();
157 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
160//----------------------------------------------------------------------
161// Destructor
162//----------------------------------------------------------------------
163ValueObject::~ValueObject ()
164{
165}
166
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000167bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000168ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169{
Enrico Granata4becb372011-06-29 22:27:15 +0000170
Enrico Granata9128ee22011-09-06 19:20:51 +0000171 bool did_change_formats = false;
172
Enrico Granata0a3958e2011-07-02 00:25:22 +0000173 if (update_format)
Enrico Granata5548cb52013-01-28 23:47:25 +0000174 did_change_formats = UpdateFormatsIfNeeded();
Enrico Granata4becb372011-06-29 22:27:15 +0000175
Greg Claytonb71f3842010-10-05 03:13:51 +0000176 // If this is a constant value, then our success is predicated on whether
177 // we have an error or not
178 if (GetIsConstant())
Enrico Granata9128ee22011-09-06 19:20:51 +0000179 {
Enrico Granatab1c6c482013-10-09 00:33:55 +0000180 // if you are constant, things might still have changed behind your back
181 // (e.g. you are a frozen object and things have changed deeper than you cared to freeze-dry yourself)
182 // in this case, your value has not changed, but "computed" entries might have, so you might now have
183 // a different summary, or a different object description. clear these so we will recompute them
Enrico Granata9128ee22011-09-06 19:20:51 +0000184 if (update_format && !did_change_formats)
Enrico Granatab1c6c482013-10-09 00:33:55 +0000185 ClearUserVisibleData(eClearUserVisibleDataItemsSummary | eClearUserVisibleDataItemsDescription);
Greg Claytonb71f3842010-10-05 03:13:51 +0000186 return m_error.Success();
Enrico Granata9128ee22011-09-06 19:20:51 +0000187 }
Greg Claytonb71f3842010-10-05 03:13:51 +0000188
Jim Ingham6035b672011-03-31 00:19:25 +0000189 bool first_update = m_update_point.IsFirstEvaluation();
190
191 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Jim Ingham6035b672011-03-31 00:19:25 +0000193 m_update_point.SetUpdated();
194
195 // Save the old value using swap to avoid a string copy which
196 // also will clear our m_value_str
197 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000198 {
Jim Ingham6035b672011-03-31 00:19:25 +0000199 m_old_value_valid = false;
200 }
201 else
202 {
203 m_old_value_valid = true;
204 m_old_value_str.swap (m_value_str);
Enrico Granata86cc9822012-03-19 22:58:49 +0000205 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham6035b672011-03-31 00:19:25 +0000206 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207
Enrico Granataf2bbf712011-07-15 02:26:42 +0000208 ClearUserVisibleData();
209
Greg Claytonefbc7d22012-03-09 04:23:44 +0000210 if (IsInScope())
Jim Ingham6035b672011-03-31 00:19:25 +0000211 {
Greg Claytonefbc7d22012-03-09 04:23:44 +0000212 const bool value_was_valid = GetValueIsValid();
213 SetValueDidChange (false);
214
215 m_error.Clear();
216
217 // Call the pure virtual function to update the value
218 bool success = UpdateValue ();
219
220 SetValueIsValid (success);
221
222 if (first_update)
223 SetValueDidChange (false);
224 else if (!m_value_did_change && success == false)
225 {
226 // The value wasn't gotten successfully, so we mark this
227 // as changed if the value used to be valid and now isn't
228 SetValueDidChange (value_was_valid);
229 }
230 }
231 else
232 {
233 m_error.SetErrorString("out of scope");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000234 }
235 }
236 return m_error.Success();
237}
238
Enrico Granata9128ee22011-09-06 19:20:51 +0000239bool
Enrico Granata5548cb52013-01-28 23:47:25 +0000240ValueObject::UpdateFormatsIfNeeded()
Enrico Granata4becb372011-06-29 22:27:15 +0000241{
Greg Clayton5160ce52013-03-27 23:08:40 +0000242 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata6f3533f2011-07-29 19:53:35 +0000243 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +0000244 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000245 GetName().GetCString(), static_cast<void*>(this),
246 m_last_format_mgr_revision,
247 DataVisualization::GetCurrentRevision());
248
Enrico Granata9128ee22011-09-06 19:20:51 +0000249 bool any_change = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000250
Enrico Granata5548cb52013-01-28 23:47:25 +0000251 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()))
Enrico Granata4becb372011-06-29 22:27:15 +0000252 {
Enrico Granataa0db6ed2014-04-09 21:06:11 +0000253 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
254 any_change = true;
255
Enrico Granata852cc952013-10-08 19:03:22 +0000256 SetValueFormat(DataVisualization::GetFormat (*this, eNoDynamicValues));
Enrico Granata5548cb52013-01-28 23:47:25 +0000257 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000258#ifndef LLDB_DISABLE_PYTHON
Enrico Granata5548cb52013-01-28 23:47:25 +0000259 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000260#endif
Enrico Granata744794a2014-09-05 21:46:22 +0000261 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
Enrico Granata4becb372011-06-29 22:27:15 +0000262 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000263
Enrico Granata9128ee22011-09-06 19:20:51 +0000264 return any_change;
Enrico Granata4becb372011-06-29 22:27:15 +0000265}
266
Jim Ingham16e0c682011-08-12 23:34:31 +0000267void
268ValueObject::SetNeedsUpdate ()
269{
270 m_update_point.SetNeedsUpdate();
271 // We have to clear the value string here so ConstResult children will notice if their values are
272 // changed by hand (i.e. with SetValueAsCString).
Enrico Granata86cc9822012-03-19 22:58:49 +0000273 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000274}
275
Enrico Granata13ac0e22012-10-17 19:03:34 +0000276void
Enrico Granatae3e91512012-10-22 18:18:36 +0000277ValueObject::ClearDynamicTypeInformation ()
Enrico Granata13ac0e22012-10-17 19:03:34 +0000278{
Enrico Granata38c54632013-10-30 00:04:29 +0000279 m_children_count_valid = false;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000280 m_did_calculate_complete_objc_class_type = false;
Enrico Granatae3e91512012-10-22 18:18:36 +0000281 m_last_format_mgr_revision = 0;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000282 m_override_type = ClangASTType();
Enrico Granatae3e91512012-10-22 18:18:36 +0000283 SetValueFormat(lldb::TypeFormatImplSP());
284 SetSummaryFormat(lldb::TypeSummaryImplSP());
285 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000286}
287
Sean Callanan72772842012-02-22 23:57:45 +0000288ClangASTType
289ValueObject::MaybeCalculateCompleteType ()
290{
Greg Clayton57ee3062013-07-11 22:46:58 +0000291 ClangASTType clang_type(GetClangTypeImpl());
Sean Callanan356e17c2012-03-30 02:04:38 +0000292
Sean Callanan72772842012-02-22 23:57:45 +0000293 if (m_did_calculate_complete_objc_class_type)
294 {
295 if (m_override_type.IsValid())
296 return m_override_type;
297 else
Greg Clayton57ee3062013-07-11 22:46:58 +0000298 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000299 }
300
Greg Clayton57ee3062013-07-11 22:46:58 +0000301 ClangASTType class_type;
302 bool is_pointer_type = false;
Sean Callanan72772842012-02-22 23:57:45 +0000303
Greg Clayton57ee3062013-07-11 22:46:58 +0000304 if (clang_type.IsObjCObjectPointerType(&class_type))
Sean Callanan72772842012-02-22 23:57:45 +0000305 {
306 is_pointer_type = true;
307 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000308 else if (clang_type.IsObjCObjectOrInterfaceType())
Sean Callanan72772842012-02-22 23:57:45 +0000309 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000310 class_type = clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000311 }
312 else
313 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000314 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000315 }
316
317 m_did_calculate_complete_objc_class_type = true;
318
Greg Clayton57ee3062013-07-11 22:46:58 +0000319 if (class_type)
Sean Callanan72772842012-02-22 23:57:45 +0000320 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000321 ConstString class_name (class_type.GetConstTypeName());
Sean Callanan72772842012-02-22 23:57:45 +0000322
Greg Clayton57ee3062013-07-11 22:46:58 +0000323 if (class_name)
324 {
325 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
326
327 if (process_sp)
328 {
329 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
330
331 if (objc_language_runtime)
332 {
333 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name);
334
335 if (complete_objc_class_type_sp)
336 {
337 ClangASTType complete_class(complete_objc_class_type_sp->GetClangFullType());
338
339 if (complete_class.GetCompleteType())
340 {
341 if (is_pointer_type)
342 {
343 m_override_type = complete_class.GetPointerType();
344 }
345 else
346 {
347 m_override_type = complete_class;
348 }
349
350 if (m_override_type.IsValid())
351 return m_override_type;
352 }
353 }
354 }
355 }
356 }
Sean Callanan72772842012-02-22 23:57:45 +0000357 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000358 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000359}
360
Greg Clayton57ee3062013-07-11 22:46:58 +0000361ClangASTType
Sean Callanan72772842012-02-22 23:57:45 +0000362ValueObject::GetClangType ()
363{
Greg Clayton57ee3062013-07-11 22:46:58 +0000364 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000365}
366
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000367TypeImpl
368ValueObject::GetTypeImpl ()
369{
370 return TypeImpl(GetClangType());
371}
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373DataExtractor &
374ValueObject::GetDataExtractor ()
375{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000376 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 return m_data;
378}
379
380const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000381ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000383 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 return m_error;
385}
386
387const ConstString &
388ValueObject::GetName() const
389{
390 return m_name;
391}
392
393const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000394ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395{
Enrico Granata82fabf82013-04-30 20:45:04 +0000396 return GetLocationAsCStringImpl(m_value,
397 m_data);
398}
399
400const char *
401ValueObject::GetLocationAsCStringImpl (const Value& value,
402 const DataExtractor& data)
403{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000404 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 {
406 if (m_location_str.empty())
407 {
408 StreamString sstr;
Enrico Granata82fabf82013-04-30 20:45:04 +0000409
410 Value::ValueType value_type = value.GetValueType();
411
412 switch (value_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000415 case Value::eValueTypeVector:
Enrico Granata82fabf82013-04-30 20:45:04 +0000416 if (value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000418 RegisterInfo *reg_info = value.GetRegisterInfo();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000419 if (reg_info)
420 {
421 if (reg_info->name)
422 m_location_str = reg_info->name;
423 else if (reg_info->alt_name)
424 m_location_str = reg_info->alt_name;
Enrico Granata82fabf82013-04-30 20:45:04 +0000425 if (m_location_str.empty())
426 m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 }
428 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000429 if (m_location_str.empty())
430 m_location_str = (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 break;
432
433 case Value::eValueTypeLoadAddress:
434 case Value::eValueTypeFileAddress:
435 case Value::eValueTypeHostAddress:
436 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000437 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
438 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 m_location_str.swap(sstr.GetString());
440 }
441 break;
442 }
443 }
444 }
445 return m_location_str.c_str();
446}
447
448Value &
449ValueObject::GetValue()
450{
451 return m_value;
452}
453
454const Value &
455ValueObject::GetValue() const
456{
457 return m_value;
458}
459
460bool
Jim Ingham6035b672011-03-31 00:19:25 +0000461ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000462{
Enrico Granata6fd87d52011-08-04 01:41:02 +0000463 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
464 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000465 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Ingham16e0c682011-08-12 23:34:31 +0000466 Value tmp_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000467 scalar = tmp_value.ResolveValue(&exe_ctx);
Greg Claytondcad5022011-12-29 01:26:56 +0000468 if (scalar.IsValid())
469 {
470 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
471 if (bitfield_bit_size)
472 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset());
473 return true;
474 }
Enrico Granata6fd87d52011-08-04 01:41:02 +0000475 }
Greg Claytondcad5022011-12-29 01:26:56 +0000476 return false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000477}
478
479bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000480ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481{
Greg Clayton288bdf92010-09-02 02:59:18 +0000482 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483}
484
485
486void
487ValueObject::SetValueIsValid (bool b)
488{
Greg Clayton288bdf92010-09-02 02:59:18 +0000489 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490}
491
492bool
Jim Ingham6035b672011-03-31 00:19:25 +0000493ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000494{
Jim Ingham6035b672011-03-31 00:19:25 +0000495 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000496 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000497}
498
499void
500ValueObject::SetValueDidChange (bool value_changed)
501{
Greg Clayton288bdf92010-09-02 02:59:18 +0000502 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503}
504
505ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000506ValueObject::GetChildAtIndex (size_t idx, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000507{
508 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000509 // We may need to update our value if we are dynamic
510 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000511 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000512 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000514 // Check if we have already made the child value object?
Enrico Granata9d60f602012-03-09 03:09:58 +0000515 if (can_create && !m_children.HasChildAtIndex(idx))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000517 // No we haven't created the child at this index, so lets have our
518 // subclass do it and cache the result for quick future access.
Enrico Granata9d60f602012-03-09 03:09:58 +0000519 m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0));
Jim Ingham78a685a2011-04-16 00:01:13 +0000520 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000521
Enrico Granata9d60f602012-03-09 03:09:58 +0000522 ValueObject* child = m_children.GetChildAtIndex(idx);
523 if (child != NULL)
524 return child->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 }
526 return child_sp;
527}
528
Enrico Granata3309d882013-01-12 01:00:22 +0000529ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000530ValueObject::GetChildAtIndexPath (const std::initializer_list<size_t>& idxs,
531 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000532{
533 if (idxs.size() == 0)
534 return GetSP();
535 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000536 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000537 {
538 root = root->GetChildAtIndex(idx, true);
539 if (!root)
540 {
541 if (index_of_error)
542 *index_of_error = idx;
543 return root;
544 }
545 }
546 return root;
547}
548
549ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000550ValueObject::GetChildAtIndexPath (const std::initializer_list< std::pair<size_t, bool> >& idxs,
551 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000552{
553 if (idxs.size() == 0)
554 return GetSP();
555 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000556 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000557 {
558 root = root->GetChildAtIndex(idx.first, idx.second);
559 if (!root)
560 {
561 if (index_of_error)
562 *index_of_error = idx.first;
563 return root;
564 }
565 }
566 return root;
567}
568
569lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000570ValueObject::GetChildAtIndexPath (const std::vector<size_t> &idxs,
571 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000572{
573 if (idxs.size() == 0)
574 return GetSP();
575 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000576 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000577 {
578 root = root->GetChildAtIndex(idx, true);
579 if (!root)
580 {
581 if (index_of_error)
582 *index_of_error = idx;
583 return root;
584 }
585 }
586 return root;
587}
588
589lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000590ValueObject::GetChildAtIndexPath (const std::vector< std::pair<size_t, bool> > &idxs,
591 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000592{
593 if (idxs.size() == 0)
594 return GetSP();
595 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000596 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000597 {
598 root = root->GetChildAtIndex(idx.first, idx.second);
599 if (!root)
600 {
601 if (index_of_error)
602 *index_of_error = idx.first;
603 return root;
604 }
605 }
606 return root;
607}
608
Enrico Granatae2e220a2013-09-12 00:48:47 +0000609lldb::ValueObjectSP
610ValueObject::GetChildAtNamePath (const std::initializer_list<ConstString> &names,
611 ConstString* name_of_error)
612{
613 if (names.size() == 0)
614 return GetSP();
615 ValueObjectSP root(GetSP());
616 for (ConstString name : names)
617 {
618 root = root->GetChildMemberWithName(name, true);
619 if (!root)
620 {
621 if (name_of_error)
622 *name_of_error = name;
623 return root;
624 }
625 }
626 return root;
627}
628
629lldb::ValueObjectSP
630ValueObject::GetChildAtNamePath (const std::vector<ConstString> &names,
631 ConstString* name_of_error)
632{
633 if (names.size() == 0)
634 return GetSP();
635 ValueObjectSP root(GetSP());
636 for (ConstString name : names)
637 {
638 root = root->GetChildMemberWithName(name, true);
639 if (!root)
640 {
641 if (name_of_error)
642 *name_of_error = name;
643 return root;
644 }
645 }
646 return root;
647}
648
649lldb::ValueObjectSP
650ValueObject::GetChildAtNamePath (const std::initializer_list< std::pair<ConstString, bool> > &names,
651 ConstString* name_of_error)
652{
653 if (names.size() == 0)
654 return GetSP();
655 ValueObjectSP root(GetSP());
656 for (std::pair<ConstString, bool> name : names)
657 {
658 root = root->GetChildMemberWithName(name.first, name.second);
659 if (!root)
660 {
661 if (name_of_error)
662 *name_of_error = name.first;
663 return root;
664 }
665 }
666 return root;
667}
668
669lldb::ValueObjectSP
670ValueObject::GetChildAtNamePath (const std::vector< std::pair<ConstString, bool> > &names,
671 ConstString* name_of_error)
672{
673 if (names.size() == 0)
674 return GetSP();
675 ValueObjectSP root(GetSP());
676 for (std::pair<ConstString, bool> name : names)
677 {
678 root = root->GetChildMemberWithName(name.first, name.second);
679 if (!root)
680 {
681 if (name_of_error)
682 *name_of_error = name.first;
683 return root;
684 }
685 }
686 return root;
687}
688
Greg Claytonc7bece562013-01-25 18:06:21 +0000689size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690ValueObject::GetIndexOfChildWithName (const ConstString &name)
691{
692 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000693 return GetClangType().GetIndexOfChildWithName (name.GetCString(), omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694}
695
696ValueObjectSP
697ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
698{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000699 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000700 // classes (which really aren't part of the expression path), so we
701 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000703
Greg Claytondea8cb42011-06-29 22:09:02 +0000704 // We may need to update our value if we are dynamic
705 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000706 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000707
708 std::vector<uint32_t> child_indexes;
Greg Claytondea8cb42011-06-29 22:09:02 +0000709 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000710 const size_t num_child_indexes = GetClangType().GetIndexOfChildMemberWithName (name.GetCString(),
711 omit_empty_base_classes,
712 child_indexes);
Greg Claytondea8cb42011-06-29 22:09:02 +0000713 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000714 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000715 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
716 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
717
718 child_sp = GetChildAtIndex(*pos, can_create);
719 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000721 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000722 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000723 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
724 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000725 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000726 else
727 {
728 child_sp.reset();
729 }
730
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 }
732 }
733 return child_sp;
734}
735
736
Greg Claytonc7bece562013-01-25 18:06:21 +0000737size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738ValueObject::GetNumChildren ()
739{
Enrico Granatac5bc4122012-03-27 02:35:13 +0000740 UpdateValueIfNeeded();
Greg Clayton288bdf92010-09-02 02:59:18 +0000741 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 {
743 SetNumChildren (CalculateNumChildren());
744 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000745 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746}
Greg Clayton4a792072012-10-23 01:50:10 +0000747
748bool
749ValueObject::MightHaveChildren()
750{
Enrico Granatadb8142b2012-10-23 02:07:54 +0000751 bool has_children = false;
Greg Clayton2452ab72013-02-08 22:02:02 +0000752 const uint32_t type_info = GetTypeInfo();
753 if (type_info)
Greg Clayton4a792072012-10-23 01:50:10 +0000754 {
Enrico Granata622be232014-10-21 20:52:14 +0000755 if (type_info & (eTypeHasChildren |
756 eTypeIsPointer |
757 eTypeIsReference))
Greg Clayton4a792072012-10-23 01:50:10 +0000758 has_children = true;
759 }
760 else
761 {
762 has_children = GetNumChildren () > 0;
763 }
764 return has_children;
765}
766
767// Should only be called by ValueObject::GetNumChildren()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768void
Greg Claytonc7bece562013-01-25 18:06:21 +0000769ValueObject::SetNumChildren (size_t num_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770{
Greg Clayton288bdf92010-09-02 02:59:18 +0000771 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000772 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773}
774
775void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776ValueObject::SetName (const ConstString &name)
777{
778 m_name = name;
779}
780
Jim Ingham58b59f92011-04-22 23:53:53 +0000781ValueObject *
Greg Claytonc7bece562013-01-25 18:06:21 +0000782ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000783{
Jim Ingham2eec4872011-05-07 00:10:58 +0000784 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000785
Greg Claytondea8cb42011-06-29 22:09:02 +0000786 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000787 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000788 std::string child_name_str;
789 uint32_t child_byte_size = 0;
790 int32_t child_byte_offset = 0;
791 uint32_t child_bitfield_bit_size = 0;
792 uint32_t child_bitfield_bit_offset = 0;
793 bool child_is_base_class = false;
794 bool child_is_deref_of_parent = false;
795
796 const bool transparent_pointers = synthetic_array_member == false;
Greg Clayton57ee3062013-07-11 22:46:58 +0000797 ClangASTType child_clang_type;
Greg Claytondea8cb42011-06-29 22:09:02 +0000798
Greg Claytoncc4d0142012-02-17 07:49:44 +0000799 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000800
Greg Clayton57ee3062013-07-11 22:46:58 +0000801 child_clang_type = GetClangType().GetChildClangTypeAtIndex (&exe_ctx,
Greg Clayton57ee3062013-07-11 22:46:58 +0000802 idx,
803 transparent_pointers,
804 omit_empty_base_classes,
805 ignore_array_bounds,
806 child_name_str,
807 child_byte_size,
808 child_byte_offset,
809 child_bitfield_bit_size,
810 child_bitfield_bit_offset,
811 child_is_base_class,
Greg Clayton759e7442014-07-19 00:12:57 +0000812 child_is_deref_of_parent,
813 this);
Greg Clayton4ef877f2012-12-06 02:33:54 +0000814 if (child_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000815 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000816 if (synthetic_index)
817 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818
Greg Claytondea8cb42011-06-29 22:09:02 +0000819 ConstString child_name;
820 if (!child_name_str.empty())
821 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822
Greg Claytondea8cb42011-06-29 22:09:02 +0000823 valobj = new ValueObjectChild (*this,
Greg Claytondea8cb42011-06-29 22:09:02 +0000824 child_clang_type,
825 child_name,
826 child_byte_size,
827 child_byte_offset,
828 child_bitfield_bit_size,
829 child_bitfield_bit_offset,
830 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000831 child_is_deref_of_parent,
832 eAddressTypeInvalid);
833 //if (valobj)
834 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
835 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000836
Jim Ingham58b59f92011-04-22 23:53:53 +0000837 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838}
839
Enrico Granata0c489f52012-03-01 04:24:26 +0000840bool
841ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
842 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843{
Enrico Granata0c489f52012-03-01 04:24:26 +0000844 destination.clear();
845
846 // ideally we would like to bail out if passing NULL, but if we do so
847 // we end up not providing the summary for function pointers anymore
848 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
849 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000850
851 m_is_getting_summary = true;
Enrico Granataf18c03e2012-04-04 17:34:10 +0000852
853 // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other
854 // information that we might care to see in a crash log. might be useful in very specific situations though.
855 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s",
856 GetTypeName().GetCString(),
857 GetName().GetCString(),
858 summary_ptr->GetDescription().c_str());*/
859
Enrico Granata0c489f52012-03-01 04:24:26 +0000860 if (UpdateValueIfNeeded (false))
861 {
862 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000864 if (HasSyntheticValue())
865 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
866 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000867 }
868 else
869 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000870 ClangASTType clang_type = GetClangType();
Enrico Granata0c489f52012-03-01 04:24:26 +0000871
872 // Do some default printout for function pointers
873 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000874 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000875 if (clang_type.IsFunctionPointerType ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000877 StreamString sstr;
Enrico Granata0c489f52012-03-01 04:24:26 +0000878 AddressType func_ptr_address_type = eAddressTypeInvalid;
879 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
880 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000881 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000882 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000883 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000884 case eAddressTypeInvalid:
885 case eAddressTypeFile:
886 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000887
Greg Claytoncc4d0142012-02-17 07:49:44 +0000888 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000889 {
890 ExecutionContext exe_ctx (GetExecutionContextRef());
891
892 Address so_addr;
893 Target *target = exe_ctx.GetTargetPtr();
894 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000895 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000896 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000897 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000898 so_addr.Dump (&sstr,
899 exe_ctx.GetBestExecutionContextScope(),
900 Address::DumpStyleResolvedDescription,
901 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000902 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000903 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000904 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000905 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000906
Greg Claytoncc4d0142012-02-17 07:49:44 +0000907 case eAddressTypeHost:
908 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000909 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000910 }
911 if (sstr.GetSize() > 0)
912 {
913 destination.assign (1, '(');
914 destination.append (sstr.GetData(), sstr.GetSize());
915 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000916 }
917 }
918 }
919 }
920 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000921 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000922 return !destination.empty();
923}
924
925const char *
926ValueObject::GetSummaryAsCString ()
927{
928 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
929 {
930 GetSummaryAsCString(GetSummaryFormat().get(),
931 m_summary_str);
932 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000933 if (m_summary_str.empty())
934 return NULL;
935 return m_summary_str.c_str();
936}
937
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000938bool
939ValueObject::IsCStringContainer(bool check_pointer)
940{
Greg Clayton57ee3062013-07-11 22:46:58 +0000941 ClangASTType pointee_or_element_clang_type;
942 const Flags type_flags (GetTypeInfo (&pointee_or_element_clang_type));
Enrico Granata622be232014-10-21 20:52:14 +0000943 bool is_char_arr_ptr (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000944 pointee_or_element_clang_type.IsCharType ());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000945 if (!is_char_arr_ptr)
946 return false;
947 if (!check_pointer)
948 return true;
Enrico Granata622be232014-10-21 20:52:14 +0000949 if (type_flags.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000950 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000951 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000952 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000953 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000954 return (cstr_address != LLDB_INVALID_ADDRESS);
955}
956
Enrico Granata9128ee22011-09-06 19:20:51 +0000957size_t
958ValueObject::GetPointeeData (DataExtractor& data,
959 uint32_t item_idx,
960 uint32_t item_count)
961{
Greg Clayton57ee3062013-07-11 22:46:58 +0000962 ClangASTType pointee_or_element_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000963 const uint32_t type_info = GetTypeInfo (&pointee_or_element_clang_type);
Enrico Granata622be232014-10-21 20:52:14 +0000964 const bool is_pointer_type = type_info & eTypeIsPointer;
965 const bool is_array_type = type_info & eTypeIsArray;
Greg Clayton2452ab72013-02-08 22:02:02 +0000966 if (!(is_pointer_type || is_array_type))
Enrico Granata9128ee22011-09-06 19:20:51 +0000967 return 0;
968
969 if (item_count == 0)
970 return 0;
971
Greg Clayton57ee3062013-07-11 22:46:58 +0000972 const uint64_t item_type_size = pointee_or_element_clang_type.GetByteSize();
Enrico Granata9128ee22011-09-06 19:20:51 +0000973 const uint64_t bytes = item_count * item_type_size;
Enrico Granata9128ee22011-09-06 19:20:51 +0000974 const uint64_t offset = item_idx * item_type_size;
975
976 if (item_idx == 0 && item_count == 1) // simply a deref
977 {
Greg Clayton2452ab72013-02-08 22:02:02 +0000978 if (is_pointer_type)
Enrico Granata9128ee22011-09-06 19:20:51 +0000979 {
980 Error error;
981 ValueObjectSP pointee_sp = Dereference(error);
982 if (error.Fail() || pointee_sp.get() == NULL)
983 return 0;
Sean Callanan866e91c2014-02-28 22:27:53 +0000984 return pointee_sp->GetData(data, error);
Enrico Granata9128ee22011-09-06 19:20:51 +0000985 }
986 else
987 {
988 ValueObjectSP child_sp = GetChildAtIndex(0, true);
989 if (child_sp.get() == NULL)
990 return 0;
Sean Callanan866e91c2014-02-28 22:27:53 +0000991 Error error;
992 return child_sp->GetData(data, error);
Enrico Granata9128ee22011-09-06 19:20:51 +0000993 }
994 return true;
995 }
996 else /* (items > 1) */
997 {
998 Error error;
999 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
1000 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
1001
1002 AddressType addr_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001003 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
Enrico Granata9128ee22011-09-06 19:20:51 +00001004
Enrico Granata9128ee22011-09-06 19:20:51 +00001005 switch (addr_type)
1006 {
1007 case eAddressTypeFile:
1008 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001009 ModuleSP module_sp (GetModule());
1010 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +00001011 {
Enrico Granata9c2efe32012-08-07 01:49:34 +00001012 addr = addr + offset;
Enrico Granata9128ee22011-09-06 19:20:51 +00001013 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +00001014 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001015 ExecutionContext exe_ctx (GetExecutionContextRef());
1016 Target* target = exe_ctx.GetTargetPtr();
1017 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +00001018 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001019 heap_buf_ptr->SetByteSize(bytes);
1020 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
1021 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +00001022 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001023 data.SetData(data_sp);
1024 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001025 }
1026 }
1027 }
1028 }
1029 break;
1030 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +00001031 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001032 ExecutionContext exe_ctx (GetExecutionContextRef());
1033 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +00001034 if (process)
1035 {
1036 heap_buf_ptr->SetByteSize(bytes);
1037 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
Enrico Granata5e1480c2013-10-30 17:52:44 +00001038 if (error.Success() || bytes_read > 0)
Enrico Granata9128ee22011-09-06 19:20:51 +00001039 {
1040 data.SetData(data_sp);
1041 return bytes_read;
1042 }
1043 }
1044 }
1045 break;
1046 case eAddressTypeHost:
1047 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001048 const uint64_t max_bytes = GetClangType().GetByteSize();
Greg Clayton2452ab72013-02-08 22:02:02 +00001049 if (max_bytes > offset)
1050 {
1051 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
1052 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
1053 data.SetData(data_sp);
1054 return bytes_read;
1055 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001056 }
1057 break;
1058 case eAddressTypeInvalid:
Enrico Granata9128ee22011-09-06 19:20:51 +00001059 break;
1060 }
1061 }
1062 return 0;
1063}
1064
Greg Claytonfaac1112013-03-14 18:31:44 +00001065uint64_t
Sean Callanan866e91c2014-02-28 22:27:53 +00001066ValueObject::GetData (DataExtractor& data, Error &error)
Enrico Granata9128ee22011-09-06 19:20:51 +00001067{
1068 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001069 ExecutionContext exe_ctx (GetExecutionContextRef());
Sean Callanan866e91c2014-02-28 22:27:53 +00001070 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +00001071 if (error.Fail())
Sean Callananed185ab2013-04-19 19:47:32 +00001072 {
1073 if (m_data.GetByteSize())
1074 {
1075 data = m_data;
1076 return data.GetByteSize();
1077 }
1078 else
1079 {
1080 return 0;
1081 }
1082 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001083 data.SetAddressByteSize(m_data.GetAddressByteSize());
1084 data.SetByteOrder(m_data.GetByteOrder());
1085 return data.GetByteSize();
1086}
1087
Sean Callanan389823e2013-04-13 01:21:23 +00001088bool
1089ValueObject::SetData (DataExtractor &data, Error &error)
1090{
1091 error.Clear();
1092 // Make sure our value is up to date first so that our location and location
1093 // type is valid.
1094 if (!UpdateValueIfNeeded(false))
1095 {
1096 error.SetErrorString("unable to read value");
1097 return false;
1098 }
1099
1100 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001101 const Encoding encoding = GetClangType().GetEncoding(count);
Sean Callanan389823e2013-04-13 01:21:23 +00001102
1103 const size_t byte_size = GetByteSize();
1104
1105 Value::ValueType value_type = m_value.GetValueType();
1106
1107 switch (value_type)
1108 {
1109 case Value::eValueTypeScalar:
1110 {
1111 Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1112
1113 if (!set_error.Success())
1114 {
1115 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1116 return false;
1117 }
1118 }
1119 break;
1120 case Value::eValueTypeLoadAddress:
1121 {
1122 // If it is a load address, then the scalar value is the storage location
1123 // of the data, and we have to shove this value down to that load location.
1124 ExecutionContext exe_ctx (GetExecutionContextRef());
1125 Process *process = exe_ctx.GetProcessPtr();
1126 if (process)
1127 {
1128 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1129 size_t bytes_written = process->WriteMemory(target_addr,
1130 data.GetDataStart(),
1131 byte_size,
1132 error);
1133 if (!error.Success())
1134 return false;
1135 if (bytes_written != byte_size)
1136 {
1137 error.SetErrorString("unable to write value to memory");
1138 return false;
1139 }
1140 }
1141 }
1142 break;
1143 case Value::eValueTypeHostAddress:
1144 {
1145 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1146 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1147 m_data.SetData(buffer_sp, 0);
1148 data.CopyByteOrderedData (0,
1149 byte_size,
1150 const_cast<uint8_t *>(m_data.GetDataStart()),
1151 byte_size,
1152 m_data.GetByteOrder());
1153 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1154 }
1155 break;
1156 case Value::eValueTypeFileAddress:
1157 case Value::eValueTypeVector:
1158 break;
1159 }
1160
1161 // If we have reached this point, then we have successfully changed the value.
1162 SetNeedsUpdate();
1163 return true;
1164}
1165
Enrico Granata9128ee22011-09-06 19:20:51 +00001166// will compute strlen(str), but without consuming more than
1167// maxlen bytes out of str (this serves the purpose of reading
1168// chunks of a string without having to worry about
1169// missing NULL terminators in the chunk)
1170// of course, if strlen(str) > maxlen, the function will return
1171// maxlen_value (which should be != maxlen, because that allows you
1172// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1173static uint32_t
1174strlen_or_inf (const char* str,
1175 uint32_t maxlen,
1176 uint32_t maxlen_value)
1177{
1178 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +00001179 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +00001180 {
Greg Clayton8dd5c172011-10-05 22:19:51 +00001181 while(*str)
1182 {
1183 len++;str++;
Greg Clayton2452ab72013-02-08 22:02:02 +00001184 if (len >= maxlen)
Greg Clayton8dd5c172011-10-05 22:19:51 +00001185 return maxlen_value;
1186 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001187 }
1188 return len;
1189}
1190
Enrico Granata2206b482014-10-30 18:27:31 +00001191static bool
1192CopyStringDataToBufferSP(const StreamString& source,
1193 lldb::DataBufferSP& destination)
1194{
1195 destination.reset(new DataBufferHeap(source.GetSize()+1,0));
1196 memcpy(destination->GetBytes(), source.GetString().c_str(), source.GetSize());
1197 return true;
1198}
1199
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001200size_t
Enrico Granata2206b482014-10-30 18:27:31 +00001201ValueObject::ReadPointedString (lldb::DataBufferSP& buffer_sp,
Greg Claytoncc4d0142012-02-17 07:49:44 +00001202 Error& error,
1203 uint32_t max_length,
1204 bool honor_array,
1205 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001206{
Enrico Granata2206b482014-10-30 18:27:31 +00001207 StreamString s;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001208 ExecutionContext exe_ctx (GetExecutionContextRef());
1209 Target* target = exe_ctx.GetTargetPtr();
Enrico Granata2206b482014-10-30 18:27:31 +00001210
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001211 if (!target)
1212 {
1213 s << "<no target to read from>";
1214 error.SetErrorString("no target to read from");
Enrico Granata2206b482014-10-30 18:27:31 +00001215 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001216 return 0;
1217 }
1218
1219 if (max_length == 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001220 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001221
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001222 size_t bytes_read = 0;
1223 size_t total_bytes_read = 0;
1224
Greg Clayton57ee3062013-07-11 22:46:58 +00001225 ClangASTType clang_type = GetClangType();
1226 ClangASTType elem_or_pointee_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001227 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
Enrico Granata622be232014-10-21 20:52:14 +00001228 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
Greg Clayton57ee3062013-07-11 22:46:58 +00001229 elem_or_pointee_clang_type.IsCharType ())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001230 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001231 addr_t cstr_address = LLDB_INVALID_ADDRESS;
1232 AddressType cstr_address_type = eAddressTypeInvalid;
1233
1234 size_t cstr_len = 0;
1235 bool capped_data = false;
Enrico Granata622be232014-10-21 20:52:14 +00001236 if (type_flags.Test (eTypeIsArray))
Greg Claytoncc4d0142012-02-17 07:49:44 +00001237 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001238 // We have an array
Greg Clayton57ee3062013-07-11 22:46:58 +00001239 uint64_t array_size = 0;
1240 if (clang_type.IsArrayType(NULL, &array_size, NULL))
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001241 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001242 cstr_len = array_size;
1243 if (cstr_len > max_length)
1244 {
1245 capped_data = true;
1246 cstr_len = max_length;
1247 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001248 }
1249 cstr_address = GetAddressOf (true, &cstr_address_type);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001250 }
1251 else
1252 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001253 // We have a pointer
1254 cstr_address = GetPointerValue (&cstr_address_type);
1255 }
1256
1257 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1258 {
1259 s << "<invalid address>";
1260 error.SetErrorString("invalid address");
Enrico Granata2206b482014-10-30 18:27:31 +00001261 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001262 return 0;
1263 }
Enrico Granata2206b482014-10-30 18:27:31 +00001264
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001265 Address cstr_so_addr (cstr_address);
1266 DataExtractor data;
1267 if (cstr_len > 0 && honor_array)
1268 {
1269 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1270 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1271 GetPointeeData(data, 0, cstr_len);
Enrico Granata2206b482014-10-30 18:27:31 +00001272
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001273 if ((bytes_read = data.GetByteSize()) > 0)
1274 {
1275 total_bytes_read = bytes_read;
Enrico Granata2206b482014-10-30 18:27:31 +00001276 for (size_t offset = 0; offset < bytes_read; offset++)
1277 s.Printf("%c", *data.PeekData(offset, 1));
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001278 if (capped_data)
1279 s << "...";
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001280 }
1281 }
1282 else
1283 {
1284 cstr_len = max_length;
1285 const size_t k_max_buf_size = 64;
Enrico Granata2206b482014-10-30 18:27:31 +00001286
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001287 size_t offset = 0;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001288
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001289 int cstr_len_displayed = -1;
1290 bool capped_cstr = false;
1291 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1292 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1293 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001294 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001295 total_bytes_read += bytes_read;
1296 const char *cstr = data.PeekCStr(0);
1297 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1298 if (len > k_max_buf_size)
1299 len = k_max_buf_size;
Enrico Granata2206b482014-10-30 18:27:31 +00001300
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001301 if (cstr_len_displayed < 0)
1302 cstr_len_displayed = len;
Enrico Granata2206b482014-10-30 18:27:31 +00001303
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001304 if (len == 0)
1305 break;
1306 cstr_len_displayed += len;
1307 if (len > bytes_read)
1308 len = bytes_read;
1309 if (len > cstr_len)
1310 len = cstr_len;
1311
Enrico Granata2206b482014-10-30 18:27:31 +00001312 for (size_t offset = 0; offset < bytes_read; offset++)
1313 s.Printf("%c", *data.PeekData(offset, 1));
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001314
1315 if (len < k_max_buf_size)
1316 break;
1317
1318 if (len >= cstr_len)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001319 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001320 capped_cstr = true;
1321 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001322 }
Enrico Granata2206b482014-10-30 18:27:31 +00001323
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001324 cstr_len -= len;
1325 offset += len;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001326 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001327
1328 if (cstr_len_displayed >= 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001329 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001330 if (capped_cstr)
1331 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001332 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001333 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001334 }
1335 else
1336 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001337 error.SetErrorString("not a string object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001338 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001339 }
Enrico Granata2206b482014-10-30 18:27:31 +00001340 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001341 return total_bytes_read;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001342}
1343
Enrico Granata744794a2014-09-05 21:46:22 +00001344std::pair<TypeValidatorResult, std::string>
1345ValueObject::GetValidationStatus ()
1346{
1347 if (!UpdateValueIfNeeded(true))
1348 return {TypeValidatorResult::Success,""}; // not the validator's job to discuss update problems
1349
1350 if (m_validation_result.hasValue())
1351 return m_validation_result.getValue();
1352
1353 if (!m_type_validator_sp)
1354 return {TypeValidatorResult::Success,""}; // no validator no failure
1355
1356 auto outcome = m_type_validator_sp->FormatObject(this);
1357
1358 return (m_validation_result = {outcome.m_result,outcome.m_message}).getValue();
1359}
1360
Jim Ingham53c47f12010-09-10 23:12:17 +00001361const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001362ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001363{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001364
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001365 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001366 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001367
1368 if (!m_object_desc_str.empty())
1369 return m_object_desc_str.c_str();
1370
Greg Claytoncc4d0142012-02-17 07:49:44 +00001371 ExecutionContext exe_ctx (GetExecutionContextRef());
1372 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001373 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001374 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001375
Jim Ingham53c47f12010-09-10 23:12:17 +00001376 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001377
Greg Claytonafacd142011-09-02 01:15:17 +00001378 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001379 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1380
Jim Inghama2cf2632010-12-23 02:29:54 +00001381 if (runtime == NULL)
1382 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001383 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Greg Clayton57ee3062013-07-11 22:46:58 +00001384 ClangASTType clang_type = GetClangType();
1385 if (clang_type)
Jim Inghama2cf2632010-12-23 02:29:54 +00001386 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001387 bool is_signed;
Greg Clayton57ee3062013-07-11 22:46:58 +00001388 if (clang_type.IsIntegerType (is_signed) || clang_type.IsPointerType ())
Jim Inghamb7603bb2011-03-18 00:05:18 +00001389 {
Greg Claytonafacd142011-09-02 01:15:17 +00001390 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001391 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001392 }
1393 }
1394
Jim Ingham8d543de2011-03-31 23:01:21 +00001395 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001396 {
1397 m_object_desc_str.append (s.GetData());
1398 }
Sean Callanan672ad942010-10-23 00:18:49 +00001399
1400 if (m_object_desc_str.empty())
1401 return NULL;
1402 else
1403 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001404}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405
Enrico Granata0c489f52012-03-01 04:24:26 +00001406bool
Enrico Granata4939b982013-12-22 09:24:22 +00001407ValueObject::GetValueAsCString (const lldb_private::TypeFormatImpl& format,
1408 std::string& destination)
1409{
1410 if (UpdateValueIfNeeded(false))
1411 return format.FormatObject(this,destination);
1412 else
1413 return false;
1414}
1415
1416bool
Enrico Granata0c489f52012-03-01 04:24:26 +00001417ValueObject::GetValueAsCString (lldb::Format format,
1418 std::string& destination)
1419{
Enrico Granata30f287f2013-12-28 08:44:02 +00001420 return GetValueAsCString(TypeFormatImpl_Format(format),destination);
Enrico Granata0c489f52012-03-01 04:24:26 +00001421}
1422
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001424ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001425{
Enrico Granatab294fd22013-05-31 19:18:19 +00001426 if (UpdateValueIfNeeded(true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 {
Enrico Granata4939b982013-12-22 09:24:22 +00001428 lldb::TypeFormatImplSP format_sp;
Enrico Granata0c489f52012-03-01 04:24:26 +00001429 lldb::Format my_format = GetFormat();
Enrico Granatac953a6a2012-12-11 02:17:22 +00001430 if (my_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001432 if (m_type_format_sp)
Enrico Granata4939b982013-12-22 09:24:22 +00001433 format_sp = m_type_format_sp;
Enrico Granata0c489f52012-03-01 04:24:26 +00001434 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001435 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001436 if (m_is_bitfield_for_scalar)
1437 my_format = eFormatUnsigned;
1438 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001440 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441 {
1442 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1443 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001444 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001445 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001446 else
1447 {
Enrico Granatad07cfd32014-10-08 18:27:36 +00001448 my_format = GetValue().GetClangType().GetFormat();
Enrico Granata0c489f52012-03-01 04:24:26 +00001449 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001450 }
1451 }
1452 }
Enrico Granatab294fd22013-05-31 19:18:19 +00001453 if (my_format != m_last_format || m_value_str.empty())
Enrico Granata297e69f2012-03-06 23:21:16 +00001454 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001455 m_last_format = my_format;
Enrico Granata4939b982013-12-22 09:24:22 +00001456 if (!format_sp)
Enrico Granata30f287f2013-12-28 08:44:02 +00001457 format_sp.reset(new TypeFormatImpl_Format(my_format));
Enrico Granata4939b982013-12-22 09:24:22 +00001458 if (GetValueAsCString(*format_sp.get(), m_value_str))
Enrico Granata297e69f2012-03-06 23:21:16 +00001459 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001460 if (!m_value_did_change && m_old_value_valid)
1461 {
1462 // The value was gotten successfully, so we consider the
1463 // value as changed if the value string differs
1464 SetValueDidChange (m_old_value_str != m_value_str);
1465 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001466 }
1467 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 }
1469 if (m_value_str.empty())
1470 return NULL;
1471 return m_value_str.c_str();
1472}
1473
Enrico Granatac3e320a2011-08-02 17:27:39 +00001474// if > 8bytes, 0 is returned. this method should mostly be used
1475// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001476uint64_t
Johnny Chen3f476c42012-06-05 19:37:43 +00001477ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001478{
1479 // If our byte size is zero this is an aggregate type that has children
Enrico Granatad07cfd32014-10-08 18:27:36 +00001480 if (CanProvideValue())
Enrico Granatac3e320a2011-08-02 17:27:39 +00001481 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001482 Scalar scalar;
1483 if (ResolveValue (scalar))
Johnny Chen3f476c42012-06-05 19:37:43 +00001484 {
1485 if (success)
1486 *success = true;
Enrico Granata48ea80f2012-10-24 20:24:39 +00001487 return scalar.ULongLong(fail_value);
Johnny Chen3f476c42012-06-05 19:37:43 +00001488 }
1489 // fallthrough, otherwise...
Enrico Granatac3e320a2011-08-02 17:27:39 +00001490 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001491
1492 if (success)
1493 *success = false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001494 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001495}
1496
Enrico Granatad7373f62013-10-31 18:57:50 +00001497int64_t
1498ValueObject::GetValueAsSigned (int64_t fail_value, bool *success)
1499{
1500 // If our byte size is zero this is an aggregate type that has children
Enrico Granatad07cfd32014-10-08 18:27:36 +00001501 if (CanProvideValue())
Enrico Granatad7373f62013-10-31 18:57:50 +00001502 {
1503 Scalar scalar;
1504 if (ResolveValue (scalar))
1505 {
1506 if (success)
1507 *success = true;
1508 return scalar.SLongLong(fail_value);
1509 }
1510 // fallthrough, otherwise...
1511 }
1512
1513 if (success)
1514 *success = false;
1515 return fail_value;
1516}
1517
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001518// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1519// this call up to date by returning true for your new special cases. We will eventually move
1520// to checking this call result before trying to display special cases
1521bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001522ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1523 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001524{
Greg Clayton57ee3062013-07-11 22:46:58 +00001525 Flags flags(GetTypeInfo());
Enrico Granata622be232014-10-21 20:52:14 +00001526 if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001527 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001528 {
1529 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001530 (custom_format == eFormatCString ||
1531 custom_format == eFormatCharArray ||
1532 custom_format == eFormatChar ||
1533 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001534 return true;
1535
Enrico Granata622be232014-10-21 20:52:14 +00001536 if (flags.Test(eTypeIsArray))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001537 {
Greg Claytonafacd142011-09-02 01:15:17 +00001538 if ((custom_format == eFormatBytes) ||
1539 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001540 return true;
1541
Greg Claytonafacd142011-09-02 01:15:17 +00001542 if ((custom_format == eFormatVectorOfChar) ||
1543 (custom_format == eFormatVectorOfFloat32) ||
1544 (custom_format == eFormatVectorOfFloat64) ||
1545 (custom_format == eFormatVectorOfSInt16) ||
1546 (custom_format == eFormatVectorOfSInt32) ||
1547 (custom_format == eFormatVectorOfSInt64) ||
1548 (custom_format == eFormatVectorOfSInt8) ||
1549 (custom_format == eFormatVectorOfUInt128) ||
1550 (custom_format == eFormatVectorOfUInt16) ||
1551 (custom_format == eFormatVectorOfUInt32) ||
1552 (custom_format == eFormatVectorOfUInt64) ||
1553 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001554 return true;
1555 }
1556 }
1557 return false;
1558}
1559
Enrico Granata9fc19442011-07-06 02:13:41 +00001560bool
1561ValueObject::DumpPrintableRepresentation(Stream& s,
1562 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001563 Format custom_format,
Enrico Granata0dba9b32014-01-08 01:36:59 +00001564 PrintableRepresentationSpecialCases special,
1565 bool do_dump_error)
Enrico Granata9fc19442011-07-06 02:13:41 +00001566{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001567
Greg Clayton57ee3062013-07-11 22:46:58 +00001568 Flags flags(GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001569
Enrico Granata86cc9822012-03-19 22:58:49 +00001570 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1571 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1572
1573 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001574 {
Enrico Granata622be232014-10-21 20:52:14 +00001575 if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001576 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001577 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001578 // when being asked to get a printable display an array or pointer type directly,
1579 // try to "do the right thing"
1580
1581 if (IsCStringContainer(true) &&
1582 (custom_format == eFormatCString ||
1583 custom_format == eFormatCharArray ||
1584 custom_format == eFormatChar ||
1585 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001586 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001587 Error error;
Enrico Granata2206b482014-10-30 18:27:31 +00001588 lldb::DataBufferSP buffer_sp;
1589 ReadPointedString(buffer_sp,
Enrico Granata86cc9822012-03-19 22:58:49 +00001590 error,
1591 0,
1592 (custom_format == eFormatVectorOfChar) ||
1593 (custom_format == eFormatCharArray));
Enrico Granataebdc1ac2014-11-05 21:20:48 +00001594 lldb_private::formatters::ReadBufferAndDumpToStreamOptions options(*this);
Enrico Granata2206b482014-10-30 18:27:31 +00001595 options.SetData(DataExtractor(buffer_sp, lldb::eByteOrderInvalid, 8)); // none of this matters for a string - pass some defaults
1596 options.SetStream(&s);
1597 options.SetPrefixToken(0);
1598 options.SetQuote('"');
1599 options.SetSourceSize(buffer_sp->GetByteSize());
Enrico Granata2206b482014-10-30 18:27:31 +00001600 lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
Enrico Granata86cc9822012-03-19 22:58:49 +00001601 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001602 }
1603
Enrico Granata86cc9822012-03-19 22:58:49 +00001604 if (custom_format == eFormatEnum)
1605 return false;
1606
1607 // this only works for arrays, because I have no way to know when
1608 // the pointed memory ends, and no special \0 end of data marker
Enrico Granata622be232014-10-21 20:52:14 +00001609 if (flags.Test(eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001610 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001611 if ((custom_format == eFormatBytes) ||
1612 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001613 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001614 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001615
1616 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001617 for (size_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001618 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001619
1620 if (low)
1621 s << ',';
1622
1623 ValueObjectSP child = GetChildAtIndex(low,true);
1624 if (!child.get())
1625 {
1626 s << "<invalid child>";
1627 continue;
1628 }
1629 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1630 }
1631
1632 s << ']';
1633
1634 return true;
1635 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001636
Enrico Granata86cc9822012-03-19 22:58:49 +00001637 if ((custom_format == eFormatVectorOfChar) ||
1638 (custom_format == eFormatVectorOfFloat32) ||
1639 (custom_format == eFormatVectorOfFloat64) ||
1640 (custom_format == eFormatVectorOfSInt16) ||
1641 (custom_format == eFormatVectorOfSInt32) ||
1642 (custom_format == eFormatVectorOfSInt64) ||
1643 (custom_format == eFormatVectorOfSInt8) ||
1644 (custom_format == eFormatVectorOfUInt128) ||
1645 (custom_format == eFormatVectorOfUInt16) ||
1646 (custom_format == eFormatVectorOfUInt32) ||
1647 (custom_format == eFormatVectorOfUInt64) ||
1648 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1649 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001650 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001651
1652 Format format = FormatManager::GetSingleItemFormat(custom_format);
1653
1654 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001655 for (size_t low = 0; low < count; low++)
Enrico Granata86cc9822012-03-19 22:58:49 +00001656 {
1657
1658 if (low)
1659 s << ',';
1660
1661 ValueObjectSP child = GetChildAtIndex(low,true);
1662 if (!child.get())
1663 {
1664 s << "<invalid child>";
1665 continue;
1666 }
1667 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1668 }
1669
1670 s << ']';
1671
1672 return true;
1673 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001674 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001675
1676 if ((custom_format == eFormatBoolean) ||
1677 (custom_format == eFormatBinary) ||
1678 (custom_format == eFormatChar) ||
1679 (custom_format == eFormatCharPrintable) ||
1680 (custom_format == eFormatComplexFloat) ||
1681 (custom_format == eFormatDecimal) ||
1682 (custom_format == eFormatHex) ||
Enrico Granata7ec18e32012-08-09 19:33:34 +00001683 (custom_format == eFormatHexUppercase) ||
Enrico Granata86cc9822012-03-19 22:58:49 +00001684 (custom_format == eFormatFloat) ||
1685 (custom_format == eFormatOctal) ||
1686 (custom_format == eFormatOSType) ||
1687 (custom_format == eFormatUnicode16) ||
1688 (custom_format == eFormatUnicode32) ||
1689 (custom_format == eFormatUnsigned) ||
1690 (custom_format == eFormatPointer) ||
1691 (custom_format == eFormatComplexInteger) ||
1692 (custom_format == eFormatComplex) ||
1693 (custom_format == eFormatDefault)) // use the [] operator
1694 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001695 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001696 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001697
1698 if (only_special)
1699 return false;
1700
Enrico Granata86cc9822012-03-19 22:58:49 +00001701 bool var_success = false;
1702
1703 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001704 const char *cstr = NULL;
Enrico Granata2c75f112013-06-21 00:04:51 +00001705
1706 // this is a local stream that we are using to ensure that the data pointed to by cstr survives
1707 // long enough for us to copy it to its destination - it is necessary to have this temporary storage
1708 // area for cases where our desired output is not backed by some other longer-term storage
Greg Claytonc7bece562013-01-25 18:06:21 +00001709 StreamString strm;
Enrico Granata86cc9822012-03-19 22:58:49 +00001710
Enrico Granata465f4bc2014-02-15 01:24:44 +00001711 if (custom_format != eFormatInvalid)
Enrico Granata86cc9822012-03-19 22:58:49 +00001712 SetFormat(custom_format);
1713
1714 switch(val_obj_display)
1715 {
1716 case eValueObjectRepresentationStyleValue:
Greg Claytonc7bece562013-01-25 18:06:21 +00001717 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001718 break;
1719
1720 case eValueObjectRepresentationStyleSummary:
Greg Claytonc7bece562013-01-25 18:06:21 +00001721 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001722 break;
1723
1724 case eValueObjectRepresentationStyleLanguageSpecific:
Greg Claytonc7bece562013-01-25 18:06:21 +00001725 cstr = GetObjectDescription();
Enrico Granata86cc9822012-03-19 22:58:49 +00001726 break;
1727
1728 case eValueObjectRepresentationStyleLocation:
Greg Claytonc7bece562013-01-25 18:06:21 +00001729 cstr = GetLocationAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001730 break;
1731
1732 case eValueObjectRepresentationStyleChildrenCount:
Deepak Panickal99fbc072014-03-03 15:39:47 +00001733 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
Greg Claytonc7bece562013-01-25 18:06:21 +00001734 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001735 break;
1736
1737 case eValueObjectRepresentationStyleType:
Greg Claytonc7bece562013-01-25 18:06:21 +00001738 cstr = GetTypeName().AsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001739 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001740
1741 case eValueObjectRepresentationStyleName:
1742 cstr = GetName().AsCString();
1743 break;
1744
1745 case eValueObjectRepresentationStyleExpressionPath:
1746 GetExpressionPath(strm, false);
1747 cstr = strm.GetString().c_str();
1748 break;
Enrico Granata86cc9822012-03-19 22:58:49 +00001749 }
1750
Greg Claytonc7bece562013-01-25 18:06:21 +00001751 if (!cstr)
Enrico Granata86cc9822012-03-19 22:58:49 +00001752 {
1753 if (val_obj_display == eValueObjectRepresentationStyleValue)
Greg Claytonc7bece562013-01-25 18:06:21 +00001754 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001755 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1756 {
Enrico Granatad07cfd32014-10-08 18:27:36 +00001757 if (!CanProvideValue())
Enrico Granata86cc9822012-03-19 22:58:49 +00001758 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001759 strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1760 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001761 }
1762 else
Greg Claytonc7bece562013-01-25 18:06:21 +00001763 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001764 }
1765 }
1766
Greg Claytonc7bece562013-01-25 18:06:21 +00001767 if (cstr)
1768 s.PutCString(cstr);
Enrico Granata86cc9822012-03-19 22:58:49 +00001769 else
1770 {
1771 if (m_error.Fail())
Enrico Granata0dba9b32014-01-08 01:36:59 +00001772 {
1773 if (do_dump_error)
1774 s.Printf("<%s>", m_error.AsCString());
1775 else
1776 return false;
1777 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001778 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1779 s.PutCString("<no summary available>");
1780 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1781 s.PutCString("<no value available>");
1782 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1783 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1784 else
1785 s.PutCString("<no printable representation>");
1786 }
1787
1788 // we should only return false here if we could not do *anything*
1789 // even if we have an error message as output, that's a success
1790 // from our callers' perspective, so return true
1791 var_success = true;
Enrico Granata465f4bc2014-02-15 01:24:44 +00001792
1793 if (custom_format != eFormatInvalid)
1794 SetFormat(eFormatDefault);
Enrico Granata86cc9822012-03-19 22:58:49 +00001795 }
1796
Enrico Granataf4efecd2011-07-12 22:56:10 +00001797 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001798}
1799
Greg Clayton737b9322010-09-13 03:32:57 +00001800addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001801ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001802{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001803 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001804 return LLDB_INVALID_ADDRESS;
1805
Greg Clayton73b472d2010-10-27 03:32:59 +00001806 switch (m_value.GetValueType())
1807 {
1808 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001809 case Value::eValueTypeVector:
Greg Clayton73b472d2010-10-27 03:32:59 +00001810 if (scalar_is_load_address)
1811 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001812 if(address_type)
1813 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001814 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1815 }
1816 break;
1817
1818 case Value::eValueTypeLoadAddress:
1819 case Value::eValueTypeFileAddress:
1820 case Value::eValueTypeHostAddress:
1821 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001822 if(address_type)
1823 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001824 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1825 }
1826 break;
1827 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001828 if (address_type)
1829 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001830 return LLDB_INVALID_ADDRESS;
1831}
1832
1833addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001834ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001835{
Greg Claytonafacd142011-09-02 01:15:17 +00001836 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001837 if(address_type)
1838 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001839
Enrico Granatac3e320a2011-08-02 17:27:39 +00001840 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001841 return address;
1842
Greg Clayton73b472d2010-10-27 03:32:59 +00001843 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001844 {
1845 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001846 case Value::eValueTypeVector:
Enrico Granata9128ee22011-09-06 19:20:51 +00001847 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001848 break;
1849
Enrico Granata9128ee22011-09-06 19:20:51 +00001850 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001851 case Value::eValueTypeLoadAddress:
1852 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001853 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001854 lldb::offset_t data_offset = 0;
Greg Clayton737b9322010-09-13 03:32:57 +00001855 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001856 }
1857 break;
1858 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001859
Enrico Granata9128ee22011-09-06 19:20:51 +00001860 if (address_type)
1861 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001862
Greg Clayton737b9322010-09-13 03:32:57 +00001863 return address;
1864}
1865
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866bool
Enrico Granata07a4ac22012-05-08 21:25:06 +00001867ValueObject::SetValueFromCString (const char *value_str, Error& error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001868{
Enrico Granata07a4ac22012-05-08 21:25:06 +00001869 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001870 // Make sure our value is up to date first so that our location and location
1871 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001872 if (!UpdateValueIfNeeded(false))
Enrico Granata07a4ac22012-05-08 21:25:06 +00001873 {
1874 error.SetErrorString("unable to read value");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 return false;
Enrico Granata07a4ac22012-05-08 21:25:06 +00001876 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877
Greg Claytonfaac1112013-03-14 18:31:44 +00001878 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001879 const Encoding encoding = GetClangType().GetEncoding (count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880
Greg Claytonb1320972010-07-14 00:18:15 +00001881 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882
Jim Ingham16e0c682011-08-12 23:34:31 +00001883 Value::ValueType value_type = m_value.GetValueType();
1884
1885 if (value_type == Value::eValueTypeScalar)
1886 {
1887 // If the value is already a scalar, then let the scalar change itself:
1888 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1889 }
1890 else if (byte_size <= Scalar::GetMaxByteSize())
1891 {
1892 // If the value fits in a scalar, then make a new scalar and again let the
1893 // scalar code do the conversion, then figure out where to put the new value.
1894 Scalar new_scalar;
Jim Ingham16e0c682011-08-12 23:34:31 +00001895 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1896 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001897 {
Jim Ingham4b536182011-08-09 02:12:22 +00001898 switch (value_type)
1899 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001900 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001901 {
1902 // If it is a load address, then the scalar value is the storage location
1903 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001904 ExecutionContext exe_ctx (GetExecutionContextRef());
1905 Process *process = exe_ctx.GetProcessPtr();
1906 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001907 {
Enrico Granata48ea80f2012-10-24 20:24:39 +00001908 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001909 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1910 new_scalar,
1911 byte_size,
1912 error);
Enrico Granata07a4ac22012-05-08 21:25:06 +00001913 if (!error.Success())
1914 return false;
1915 if (bytes_written != byte_size)
1916 {
1917 error.SetErrorString("unable to write value to memory");
1918 return false;
1919 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001920 }
1921 }
Jim Ingham4b536182011-08-09 02:12:22 +00001922 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001923 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001924 {
1925 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1926 DataExtractor new_data;
1927 new_data.SetByteOrder (m_data.GetByteOrder());
1928
1929 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1930 m_data.SetData(buffer_sp, 0);
1931 bool success = new_scalar.GetData(new_data);
1932 if (success)
1933 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001934 new_data.CopyByteOrderedData (0,
1935 byte_size,
1936 const_cast<uint8_t *>(m_data.GetDataStart()),
1937 byte_size,
1938 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001939 }
1940 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1941
1942 }
Jim Ingham4b536182011-08-09 02:12:22 +00001943 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001944 case Value::eValueTypeFileAddress:
1945 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001946 case Value::eValueTypeVector:
1947 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001948 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001949 }
1950 else
1951 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001952 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001953 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001954 }
1955 else
1956 {
1957 // We don't support setting things bigger than a scalar at present.
Enrico Granata07a4ac22012-05-08 21:25:06 +00001958 error.SetErrorString("unable to write aggregate data type");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001959 return false;
1960 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001961
1962 // If we have reached this point, then we have successfully changed the value.
1963 SetNeedsUpdate();
1964 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
Greg Clayton81e871e2012-02-04 02:27:34 +00001967bool
1968ValueObject::GetDeclaration (Declaration &decl)
1969{
1970 decl.Clear();
1971 return false;
1972}
1973
Greg Clayton84db9102012-03-26 23:03:23 +00001974ConstString
1975ValueObject::GetTypeName()
1976{
Greg Clayton57ee3062013-07-11 22:46:58 +00001977 return GetClangType().GetConstTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001978}
1979
1980ConstString
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001981ValueObject::GetDisplayTypeName()
1982{
1983 return GetTypeName();
1984}
1985
1986ConstString
Greg Clayton84db9102012-03-26 23:03:23 +00001987ValueObject::GetQualifiedTypeName()
1988{
Greg Clayton57ee3062013-07-11 22:46:58 +00001989 return GetClangType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001990}
1991
1992
Greg Claytonafacd142011-09-02 01:15:17 +00001993LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00001994ValueObject::GetObjectRuntimeLanguage ()
1995{
Greg Clayton57ee3062013-07-11 22:46:58 +00001996 return GetClangType().GetMinimumLanguage ();
Jim Ingham5a369122010-09-28 01:25:32 +00001997}
1998
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999void
Jim Ingham58b59f92011-04-22 23:53:53 +00002000ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001{
Jim Ingham58b59f92011-04-22 23:53:53 +00002002 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003}
2004
2005ValueObjectSP
2006ValueObject::GetSyntheticChild (const ConstString &key) const
2007{
2008 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00002009 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00002011 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002012 return synthetic_child_sp;
2013}
2014
Greg Clayton2452ab72013-02-08 22:02:02 +00002015uint32_t
Greg Clayton57ee3062013-07-11 22:46:58 +00002016ValueObject::GetTypeInfo (ClangASTType *pointee_or_element_clang_type)
Greg Clayton2452ab72013-02-08 22:02:02 +00002017{
Greg Clayton57ee3062013-07-11 22:46:58 +00002018 return GetClangType().GetTypeInfo (pointee_or_element_clang_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00002019}
2020
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002021bool
2022ValueObject::IsPointerType ()
2023{
Greg Clayton57ee3062013-07-11 22:46:58 +00002024 return GetClangType().IsPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025}
2026
Jim Inghamb7603bb2011-03-18 00:05:18 +00002027bool
Greg Claytondaf515f2011-07-09 20:12:33 +00002028ValueObject::IsArrayType ()
2029{
Greg Clayton57ee3062013-07-11 22:46:58 +00002030 return GetClangType().IsArrayType (NULL, NULL, NULL);
Greg Claytondaf515f2011-07-09 20:12:33 +00002031}
2032
2033bool
Enrico Granata9fc19442011-07-06 02:13:41 +00002034ValueObject::IsScalarType ()
2035{
Greg Clayton57ee3062013-07-11 22:46:58 +00002036 return GetClangType().IsScalarType ();
Enrico Granata9fc19442011-07-06 02:13:41 +00002037}
2038
2039bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00002040ValueObject::IsIntegerType (bool &is_signed)
2041{
Greg Clayton57ee3062013-07-11 22:46:58 +00002042 return GetClangType().IsIntegerType (is_signed);
Jim Inghamb7603bb2011-03-18 00:05:18 +00002043}
Greg Clayton73b472d2010-10-27 03:32:59 +00002044
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045bool
2046ValueObject::IsPointerOrReferenceType ()
2047{
Greg Clayton57ee3062013-07-11 22:46:58 +00002048 return GetClangType().IsPointerOrReferenceType ();
Greg Clayton007d5be2011-05-30 00:49:24 +00002049}
2050
2051bool
Greg Claytondea8cb42011-06-29 22:09:02 +00002052ValueObject::IsPossibleDynamicType ()
2053{
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002054 ExecutionContext exe_ctx (GetExecutionContextRef());
2055 Process *process = exe_ctx.GetProcessPtr();
2056 if (process)
2057 return process->IsPossibleDynamicValue(*this);
2058 else
Greg Clayton57ee3062013-07-11 22:46:58 +00002059 return GetClangType().IsPossibleDynamicType (NULL, true, true);
Greg Claytondea8cb42011-06-29 22:09:02 +00002060}
2061
Enrico Granata9e7b3882012-12-13 23:50:33 +00002062bool
2063ValueObject::IsObjCNil ()
2064{
Enrico Granata622be232014-10-21 20:52:14 +00002065 const uint32_t mask = eTypeIsObjC | eTypeIsPointer;
Greg Clayton57ee3062013-07-11 22:46:58 +00002066 bool isObjCpointer = (((GetClangType().GetTypeInfo(NULL)) & mask) == mask);
Enrico Granata7277d202013-03-15 23:33:15 +00002067 if (!isObjCpointer)
2068 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002069 bool canReadValue = true;
2070 bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
Enrico Granata7277d202013-03-15 23:33:15 +00002071 return canReadValue && isZero;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002072}
2073
Greg Claytonafacd142011-09-02 01:15:17 +00002074ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002075ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002076{
Greg Clayton2452ab72013-02-08 22:02:02 +00002077 const uint32_t type_info = GetTypeInfo ();
Enrico Granata622be232014-10-21 20:52:14 +00002078 if (type_info & eTypeIsArray)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002079 return GetSyntheticArrayMemberFromArray(index, can_create);
2080
Enrico Granata622be232014-10-21 20:52:14 +00002081 if (type_info & eTypeIsPointer)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002082 return GetSyntheticArrayMemberFromPointer(index, can_create);
2083
2084 return ValueObjectSP();
2085
2086}
2087
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002088ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002089ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002090{
2091 ValueObjectSP synthetic_child_sp;
2092 if (IsPointerType ())
2093 {
2094 char index_str[64];
Deepak Panickal99fbc072014-03-03 15:39:47 +00002095 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002096 ConstString index_const_str(index_str);
2097 // Check if we have already created a synthetic array member in this
2098 // valid object. If we have we will re-use it.
2099 synthetic_child_sp = GetSyntheticChild (index_const_str);
2100 if (!synthetic_child_sp)
2101 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002102 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002103 // We haven't made a synthetic array member for INDEX yet, so
2104 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00002105 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002106
2107 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00002108 if (synthetic_child)
2109 {
2110 AddSyntheticChild(index_const_str, synthetic_child);
2111 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002112 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00002113 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00002114 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002115 }
2116 }
2117 return synthetic_child_sp;
2118}
Jim Ingham22777012010-09-23 02:01:19 +00002119
Greg Claytondaf515f2011-07-09 20:12:33 +00002120// This allows you to create an array member using and index
2121// that doesn't not fall in the normal bounds of the array.
2122// Many times structure can be defined as:
2123// struct Collection
2124// {
2125// uint32_t item_count;
2126// Item item_array[0];
2127// };
2128// The size of the "item_array" is 1, but many times in practice
2129// there are more items in "item_array".
2130
2131ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002132ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create)
Greg Claytondaf515f2011-07-09 20:12:33 +00002133{
2134 ValueObjectSP synthetic_child_sp;
2135 if (IsArrayType ())
2136 {
2137 char index_str[64];
Deepak Panickal99fbc072014-03-03 15:39:47 +00002138 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
Greg Claytondaf515f2011-07-09 20:12:33 +00002139 ConstString index_const_str(index_str);
2140 // Check if we have already created a synthetic array member in this
2141 // valid object. If we have we will re-use it.
2142 synthetic_child_sp = GetSyntheticChild (index_const_str);
2143 if (!synthetic_child_sp)
2144 {
2145 ValueObject *synthetic_child;
2146 // We haven't made a synthetic array member for INDEX yet, so
2147 // lets make one and cache it for any future reference.
2148 synthetic_child = CreateChildAtIndex(0, true, index);
2149
2150 // Cache the value if we got one back...
2151 if (synthetic_child)
2152 {
2153 AddSyntheticChild(index_const_str, synthetic_child);
2154 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002155 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00002156 synthetic_child_sp->m_is_array_item_for_pointer = true;
2157 }
2158 }
2159 }
2160 return synthetic_child_sp;
2161}
2162
Enrico Granata9fc19442011-07-06 02:13:41 +00002163ValueObjectSP
2164ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2165{
2166 ValueObjectSP synthetic_child_sp;
2167 if (IsScalarType ())
2168 {
2169 char index_str[64];
2170 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2171 ConstString index_const_str(index_str);
2172 // Check if we have already created a synthetic array member in this
2173 // valid object. If we have we will re-use it.
2174 synthetic_child_sp = GetSyntheticChild (index_const_str);
2175 if (!synthetic_child_sp)
2176 {
Enrico Granata9fc19442011-07-06 02:13:41 +00002177 // We haven't made a synthetic array member for INDEX yet, so
2178 // lets make one and cache it for any future reference.
Greg Clayton57ee3062013-07-11 22:46:58 +00002179 ValueObjectChild *synthetic_child = new ValueObjectChild (*this,
2180 GetClangType(),
2181 index_const_str,
2182 GetByteSize(),
2183 0,
2184 to-from+1,
2185 from,
2186 false,
2187 false,
2188 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00002189
2190 // Cache the value if we got one back...
2191 if (synthetic_child)
2192 {
2193 AddSyntheticChild(index_const_str, synthetic_child);
2194 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002195 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00002196 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2197 }
2198 }
2199 }
2200 return synthetic_child_sp;
2201}
2202
Greg Claytonafacd142011-09-02 01:15:17 +00002203ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00002204ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
2205{
2206
2207 ValueObjectSP synthetic_child_sp;
2208
2209 char name_str[64];
2210 snprintf(name_str, sizeof(name_str), "@%i", offset);
2211 ConstString name_const_str(name_str);
2212
2213 // Check if we have already created a synthetic array member in this
2214 // valid object. If we have we will re-use it.
2215 synthetic_child_sp = GetSyntheticChild (name_const_str);
2216
2217 if (synthetic_child_sp.get())
2218 return synthetic_child_sp;
2219
2220 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00002221 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002222
2223 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
Greg Clayton57ee3062013-07-11 22:46:58 +00002224 type,
Enrico Granata6f3533f2011-07-29 19:53:35 +00002225 name_const_str,
Greg Clayton57ee3062013-07-11 22:46:58 +00002226 type.GetByteSize(),
Enrico Granata6f3533f2011-07-29 19:53:35 +00002227 offset,
2228 0,
2229 0,
2230 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00002231 false,
2232 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002233 if (synthetic_child)
2234 {
2235 AddSyntheticChild(name_const_str, synthetic_child);
2236 synthetic_child_sp = synthetic_child->GetSP();
2237 synthetic_child_sp->SetName(name_const_str);
2238 synthetic_child_sp->m_is_child_at_offset = true;
2239 }
2240 return synthetic_child_sp;
2241}
2242
Enrico Granata32556cd2014-08-26 20:54:04 +00002243ValueObjectSP
Enrico Granata59953f02014-08-26 21:35:30 +00002244ValueObject::GetSyntheticBase (uint32_t offset, const ClangASTType& type, bool can_create)
Enrico Granata32556cd2014-08-26 20:54:04 +00002245{
2246 ValueObjectSP synthetic_child_sp;
2247
2248 char name_str[64];
2249 snprintf(name_str, sizeof(name_str), "%s", type.GetTypeName().AsCString("<unknown>"));
2250 ConstString name_const_str(name_str);
2251
2252 // Check if we have already created a synthetic array member in this
2253 // valid object. If we have we will re-use it.
2254 synthetic_child_sp = GetSyntheticChild (name_const_str);
2255
2256 if (synthetic_child_sp.get())
2257 return synthetic_child_sp;
2258
2259 if (!can_create)
2260 return ValueObjectSP();
2261
Enrico Granata32556cd2014-08-26 20:54:04 +00002262 const bool is_base_class = true;
2263
2264 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2265 type,
2266 name_const_str,
2267 type.GetByteSize(),
2268 offset,
2269 0,
2270 0,
2271 is_base_class,
2272 false,
2273 eAddressTypeInvalid);
2274 if (synthetic_child)
2275 {
2276 AddSyntheticChild(name_const_str, synthetic_child);
2277 synthetic_child_sp = synthetic_child->GetSP();
2278 synthetic_child_sp->SetName(name_const_str);
2279 }
2280 return synthetic_child_sp;
2281}
2282
2283
Enrico Granatad55546b2011-07-22 00:16:08 +00002284// your expression path needs to have a leading . or ->
2285// (unless it somehow "looks like" an array, in which case it has
2286// a leading [ symbol). while the [ is meaningful and should be shown
2287// to the user, . and -> are just parser design, but by no means
2288// added information for the user.. strip them off
2289static const char*
2290SkipLeadingExpressionPathSeparators(const char* expression)
2291{
2292 if (!expression || !expression[0])
2293 return expression;
2294 if (expression[0] == '.')
2295 return expression+1;
2296 if (expression[0] == '-' && expression[1] == '>')
2297 return expression+2;
2298 return expression;
2299}
2300
Greg Claytonafacd142011-09-02 01:15:17 +00002301ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00002302ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2303{
2304 ValueObjectSP synthetic_child_sp;
2305 ConstString name_const_string(expression);
2306 // Check if we have already created a synthetic array member in this
2307 // valid object. If we have we will re-use it.
2308 synthetic_child_sp = GetSyntheticChild (name_const_string);
2309 if (!synthetic_child_sp)
2310 {
2311 // We haven't made a synthetic array member for expression yet, so
2312 // lets make one and cache it for any future reference.
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002313 synthetic_child_sp = GetValueForExpressionPath(expression,
2314 NULL, NULL, NULL,
2315 GetValueForExpressionPathOptions().DontAllowSyntheticChildren());
Enrico Granatad55546b2011-07-22 00:16:08 +00002316
2317 // Cache the value if we got one back...
2318 if (synthetic_child_sp.get())
2319 {
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002320 // FIXME: this causes a "real" child to end up with its name changed to the contents of expression
Enrico Granatad55546b2011-07-22 00:16:08 +00002321 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00002322 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002323 }
2324 }
2325 return synthetic_child_sp;
2326}
2327
2328void
Enrico Granata86cc9822012-03-19 22:58:49 +00002329ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002330{
Enrico Granata86cc9822012-03-19 22:58:49 +00002331 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002332 return;
2333
Enrico Granatac5bc4122012-03-27 02:35:13 +00002334 TargetSP target_sp(GetTargetSP());
Enrico Granata5d5f60c2013-09-24 22:58:37 +00002335 if (target_sp && target_sp->GetEnableSyntheticValue() == false)
Enrico Granatac5bc4122012-03-27 02:35:13 +00002336 {
2337 m_synthetic_value = NULL;
2338 return;
2339 }
2340
Enrico Granatae3e91512012-10-22 18:18:36 +00002341 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2342
Enrico Granata5548cb52013-01-28 23:47:25 +00002343 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
Enrico Granata86cc9822012-03-19 22:58:49 +00002344 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002345
Enrico Granata0c489f52012-03-01 04:24:26 +00002346 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002347 return;
2348
Enrico Granatae3e91512012-10-22 18:18:36 +00002349 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2350 return;
2351
Enrico Granata86cc9822012-03-19 22:58:49 +00002352 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002353}
2354
Jim Ingham78a685a2011-04-16 00:01:13 +00002355void
Greg Claytonafacd142011-09-02 01:15:17 +00002356ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002357{
Greg Claytonafacd142011-09-02 01:15:17 +00002358 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002359 return;
2360
Jim Ingham58b59f92011-04-22 23:53:53 +00002361 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002362 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002363 ExecutionContext exe_ctx (GetExecutionContextRef());
2364 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002365 if (process && process->IsPossibleDynamicValue(*this))
Enrico Granatae3e91512012-10-22 18:18:36 +00002366 {
2367 ClearDynamicTypeInformation ();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002368 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Enrico Granatae3e91512012-10-22 18:18:36 +00002369 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002370 }
2371}
2372
Jim Ingham58b59f92011-04-22 23:53:53 +00002373ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002374ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002375{
Greg Claytonafacd142011-09-02 01:15:17 +00002376 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002377 return ValueObjectSP();
2378
2379 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002380 {
Jim Ingham2837b762011-05-04 03:43:18 +00002381 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002382 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002383 if (m_dynamic_value)
2384 return m_dynamic_value->GetSP();
2385 else
2386 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002387}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002388
Jim Ingham60dbabb2011-12-08 19:44:08 +00002389ValueObjectSP
2390ValueObject::GetStaticValue()
2391{
2392 return GetSP();
2393}
2394
Enrico Granata886147f2012-05-08 18:47:08 +00002395lldb::ValueObjectSP
2396ValueObject::GetNonSyntheticValue ()
2397{
2398 return GetSP();
2399}
2400
Enrico Granatad55546b2011-07-22 00:16:08 +00002401ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002402ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002403{
Enrico Granata86cc9822012-03-19 22:58:49 +00002404 if (use_synthetic == false)
2405 return ValueObjectSP();
2406
Enrico Granatad55546b2011-07-22 00:16:08 +00002407 CalculateSyntheticValue(use_synthetic);
2408
2409 if (m_synthetic_value)
2410 return m_synthetic_value->GetSP();
2411 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002412 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002413}
2414
Greg Claytone221f822011-01-21 01:59:00 +00002415bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002416ValueObject::HasSyntheticValue()
2417{
Enrico Granata5548cb52013-01-28 23:47:25 +00002418 UpdateFormatsIfNeeded();
Enrico Granata27b625e2011-08-09 01:04:56 +00002419
Enrico Granata0c489f52012-03-01 04:24:26 +00002420 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002421 return false;
2422
Enrico Granata86cc9822012-03-19 22:58:49 +00002423 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002424
2425 if (m_synthetic_value)
2426 return true;
2427 else
2428 return false;
2429}
2430
2431bool
Greg Claytone221f822011-01-21 01:59:00 +00002432ValueObject::GetBaseClassPath (Stream &s)
2433{
2434 if (IsBaseClass())
2435 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002436 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Clayton57ee3062013-07-11 22:46:58 +00002437 ClangASTType clang_type = GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002438 std::string cxx_class_name;
Greg Clayton57ee3062013-07-11 22:46:58 +00002439 bool this_had_base_class = clang_type.GetCXXClassName (cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002440 if (this_had_base_class)
2441 {
2442 if (parent_had_base_class)
2443 s.PutCString("::");
2444 s.PutCString(cxx_class_name.c_str());
2445 }
2446 return parent_had_base_class || this_had_base_class;
2447 }
2448 return false;
2449}
2450
2451
2452ValueObject *
2453ValueObject::GetNonBaseClassParent()
2454{
Jim Ingham78a685a2011-04-16 00:01:13 +00002455 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002456 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002457 if (GetParent()->IsBaseClass())
2458 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002459 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002460 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002461 }
2462 return NULL;
2463}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002464
Enrico Granataa3c8f042014-08-19 22:29:08 +00002465
2466bool
2467ValueObject::IsBaseClass (uint32_t& depth)
2468{
2469 if (!IsBaseClass())
2470 {
2471 depth = 0;
2472 return false;
2473 }
2474 if (GetParent())
2475 {
2476 GetParent()->IsBaseClass(depth);
2477 depth = depth + 1;
2478 return true;
2479 }
2480 // TODO: a base of no parent? weird..
2481 depth = 1;
2482 return true;
2483}
2484
Greg Clayton1d3afba2010-10-05 00:00:42 +00002485void
Enrico Granata4becb372011-06-29 22:27:15 +00002486ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002487{
Greg Claytone221f822011-01-21 01:59:00 +00002488 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002489
Enrico Granata86cc9822012-03-19 22:58:49 +00002490 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002491 {
Enrico Granata4becb372011-06-29 22:27:15 +00002492 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2493 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2494 // the eHonorPointers mode is meant to produce strings in this latter format
2495 s.PutCString("*(");
2496 }
Greg Claytone221f822011-01-21 01:59:00 +00002497
Enrico Granata4becb372011-06-29 22:27:15 +00002498 ValueObject* parent = GetParent();
2499
2500 if (parent)
2501 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002502
2503 // if we are a deref_of_parent just because we are synthetic array
2504 // members made up to allow ptr[%d] syntax to work in variable
2505 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002506 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002507 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002508
Greg Claytone221f822011-01-21 01:59:00 +00002509 if (!IsBaseClass())
2510 {
2511 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002512 {
Greg Claytone221f822011-01-21 01:59:00 +00002513 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2514 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002515 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002516 ClangASTType non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002517 if (non_base_class_parent_clang_type)
2518 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002519 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002520 {
2521 s.PutCString("->");
2522 }
Enrico Granata4becb372011-06-29 22:27:15 +00002523 else
2524 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002525 const uint32_t non_base_class_parent_type_info = non_base_class_parent_clang_type.GetTypeInfo();
2526
Enrico Granata622be232014-10-21 20:52:14 +00002527 if (non_base_class_parent_type_info & eTypeIsPointer)
Enrico Granata4becb372011-06-29 22:27:15 +00002528 {
2529 s.PutCString("->");
2530 }
Enrico Granata622be232014-10-21 20:52:14 +00002531 else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2532 !(non_base_class_parent_type_info & eTypeIsArray))
Enrico Granata4becb372011-06-29 22:27:15 +00002533 {
2534 s.PutChar('.');
2535 }
Greg Claytone221f822011-01-21 01:59:00 +00002536 }
2537 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002538 }
Greg Claytone221f822011-01-21 01:59:00 +00002539
2540 const char *name = GetName().GetCString();
2541 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002542 {
Greg Claytone221f822011-01-21 01:59:00 +00002543 if (qualify_cxx_base_classes)
2544 {
2545 if (GetBaseClassPath (s))
2546 s.PutCString("::");
2547 }
2548 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002549 }
2550 }
2551 }
2552
Enrico Granata86cc9822012-03-19 22:58:49 +00002553 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002554 {
Greg Claytone221f822011-01-21 01:59:00 +00002555 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002556 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002557}
2558
Greg Claytonafacd142011-09-02 01:15:17 +00002559ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002560ValueObject::GetValueForExpressionPath(const char* expression,
2561 const char** first_unparsed,
2562 ExpressionPathScanEndReason* reason_to_stop,
2563 ExpressionPathEndResultType* final_value_type,
2564 const GetValueForExpressionPathOptions& options,
2565 ExpressionPathAftermath* final_task_on_target)
2566{
2567
2568 const char* dummy_first_unparsed;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002569 ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2570 ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata86cc9822012-03-19 22:58:49 +00002571 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002572
2573 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2574 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2575 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2576 final_value_type ? final_value_type : &dummy_final_value_type,
2577 options,
2578 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2579
Enrico Granata86cc9822012-03-19 22:58:49 +00002580 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002581 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002582
Enrico Granata86cc9822012-03-19 22:58:49 +00002583 if (ret_val.get() && ((final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain)) // I can only deref and takeaddress of plain objects
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002584 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002585 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002586 {
2587 Error error;
2588 ValueObjectSP final_value = ret_val->Dereference(error);
2589 if (error.Fail() || !final_value.get())
2590 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002591 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002592 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002593 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002594 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002595 return ValueObjectSP();
2596 }
2597 else
2598 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002599 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002600 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002601 return final_value;
2602 }
2603 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002604 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002605 {
2606 Error error;
2607 ValueObjectSP final_value = ret_val->AddressOf(error);
2608 if (error.Fail() || !final_value.get())
2609 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002610 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002611 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002612 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002613 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002614 return ValueObjectSP();
2615 }
2616 else
2617 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002618 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002619 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002620 return final_value;
2621 }
2622 }
2623 }
2624 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2625}
2626
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002627int
2628ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002629 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002630 const char** first_unparsed,
2631 ExpressionPathScanEndReason* reason_to_stop,
2632 ExpressionPathEndResultType* final_value_type,
2633 const GetValueForExpressionPathOptions& options,
2634 ExpressionPathAftermath* final_task_on_target)
2635{
2636 const char* dummy_first_unparsed;
2637 ExpressionPathScanEndReason dummy_reason_to_stop;
2638 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002639 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002640
2641 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2642 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2643 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2644 final_value_type ? final_value_type : &dummy_final_value_type,
2645 options,
2646 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2647
2648 if (!ret_val.get()) // if there are errors, I add nothing to the list
2649 return 0;
2650
Enrico Granata86ea8d82012-03-29 01:34:34 +00002651 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002652 {
2653 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002654 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002655 {
2656 list->Append(ret_val);
2657 return 1;
2658 }
Enrico Granata86ea8d82012-03-29 01:34:34 +00002659 if (ret_val.get() && (final_value_type ? *final_value_type : dummy_final_value_type) == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002660 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002661 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002662 {
2663 Error error;
2664 ValueObjectSP final_value = ret_val->Dereference(error);
2665 if (error.Fail() || !final_value.get())
2666 {
Greg Clayton23f59502012-07-17 03:23:13 +00002667 if (reason_to_stop)
2668 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2669 if (final_value_type)
2670 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002671 return 0;
2672 }
2673 else
2674 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002675 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002676 list->Append(final_value);
2677 return 1;
2678 }
2679 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002680 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002681 {
2682 Error error;
2683 ValueObjectSP final_value = ret_val->AddressOf(error);
2684 if (error.Fail() || !final_value.get())
2685 {
Greg Clayton23f59502012-07-17 03:23:13 +00002686 if (reason_to_stop)
2687 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2688 if (final_value_type)
2689 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002690 return 0;
2691 }
2692 else
2693 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002694 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002695 list->Append(final_value);
2696 return 1;
2697 }
2698 }
2699 }
2700 }
2701 else
2702 {
2703 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2704 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2705 ret_val,
2706 list,
2707 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2708 final_value_type ? final_value_type : &dummy_final_value_type,
2709 options,
2710 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2711 }
2712 // in any non-covered case, just do the obviously right thing
2713 list->Append(ret_val);
2714 return 1;
2715}
2716
Greg Claytonafacd142011-09-02 01:15:17 +00002717ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002718ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2719 const char** first_unparsed,
2720 ExpressionPathScanEndReason* reason_to_stop,
2721 ExpressionPathEndResultType* final_result,
2722 const GetValueForExpressionPathOptions& options,
2723 ExpressionPathAftermath* what_next)
2724{
2725 ValueObjectSP root = GetSP();
2726
2727 if (!root.get())
2728 return ValueObjectSP();
2729
2730 *first_unparsed = expression_cstr;
2731
2732 while (true)
2733 {
2734
2735 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2736
Greg Clayton57ee3062013-07-11 22:46:58 +00002737 ClangASTType root_clang_type = root->GetClangType();
2738 ClangASTType pointee_clang_type;
2739 Flags pointee_clang_type_info;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002740
Greg Clayton57ee3062013-07-11 22:46:58 +00002741 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002742 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00002743 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002744
2745 if (!expression_cstr || *expression_cstr == '\0')
2746 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002747 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002748 return root;
2749 }
2750
2751 switch (*expression_cstr)
2752 {
2753 case '-':
2754 {
2755 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granata622be232014-10-21 20:52:14 +00002756 root_clang_type_info.Test(eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002757 {
2758 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002759 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2760 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002761 return ValueObjectSP();
2762 }
Enrico Granata622be232014-10-21 20:52:14 +00002763 if (root_clang_type_info.Test(eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2764 root_clang_type_info.Test(eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002765 options.m_no_fragile_ivar)
2766 {
2767 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002768 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2769 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002770 return ValueObjectSP();
2771 }
2772 if (expression_cstr[1] != '>')
2773 {
2774 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002775 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2776 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002777 return ValueObjectSP();
2778 }
2779 expression_cstr++; // skip the -
2780 }
2781 case '.': // or fallthrough from ->
2782 {
2783 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granata622be232014-10-21 20:52:14 +00002784 root_clang_type_info.Test(eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002785 {
2786 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002787 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2788 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002789 return ValueObjectSP();
2790 }
2791 expression_cstr++; // skip .
2792 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2793 ConstString child_name;
2794 if (!next_separator) // if no other separator just expand this last layer
2795 {
2796 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002797 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2798
2799 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002800 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002801 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002802 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2803 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002804 return child_valobj_sp;
2805 }
2806 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2807 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002808 if (root->IsSynthetic())
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002809 {
2810 *first_unparsed = expression_cstr;
Enrico Granatad07cfd32014-10-08 18:27:36 +00002811 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchSyntheticChild;
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002812 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2813 return ValueObjectSP();
2814 }
2815
2816 child_valobj_sp = root->GetSyntheticValue();
Enrico Granata86cc9822012-03-19 22:58:49 +00002817 if (child_valobj_sp.get())
2818 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002819 }
2820
2821 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2822 // so we hit the "else" branch, and return an error
2823 if(child_valobj_sp.get()) // if it worked, just return
2824 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002825 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002826 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2827 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002828 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002829 }
2830 else
2831 {
2832 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002833 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2834 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002835 return ValueObjectSP();
2836 }
2837 }
2838 else // other layers do expand
2839 {
2840 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002841 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2842 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002843 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002844 root = child_valobj_sp;
2845 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002846 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002847 continue;
2848 }
2849 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2850 {
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002851 if (root->IsSynthetic())
2852 {
2853 *first_unparsed = expression_cstr;
2854 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2855 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2856 return ValueObjectSP();
2857 }
2858
Enrico Granata86cc9822012-03-19 22:58:49 +00002859 child_valobj_sp = root->GetSyntheticValue(true);
2860 if (child_valobj_sp)
2861 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002862 }
2863
2864 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2865 // so we hit the "else" branch, and return an error
2866 if(child_valobj_sp.get()) // if it worked, move on
2867 {
2868 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002869 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002870 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002871 continue;
2872 }
2873 else
2874 {
2875 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002876 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2877 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002878 return ValueObjectSP();
2879 }
2880 }
2881 break;
2882 }
2883 case '[':
2884 {
Enrico Granata622be232014-10-21 20:52:14 +00002885 if (!root_clang_type_info.Test(eTypeIsArray) && !root_clang_type_info.Test(eTypeIsPointer) && !root_clang_type_info.Test(eTypeIsVector)) // if this is not a T[] nor a T*
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002886 {
Enrico Granata622be232014-10-21 20:52:14 +00002887 if (!root_clang_type_info.Test(eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002888 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002889 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2890 {
2891 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002892 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2893 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002894 return ValueObjectSP();
2895 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002896 }
2897 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2898 {
2899 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002900 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2901 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002902 return ValueObjectSP();
2903 }
2904 }
2905 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2906 {
Enrico Granata622be232014-10-21 20:52:14 +00002907 if (!root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002908 {
2909 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002910 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2911 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002912 return ValueObjectSP();
2913 }
2914 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2915 {
2916 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002917 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2918 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002919 return root;
2920 }
2921 }
2922 const char *separator_position = ::strchr(expression_cstr+1,'-');
2923 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2924 if (!close_bracket_position) // if there is no ], this is a syntax error
2925 {
2926 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002927 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2928 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002929 return ValueObjectSP();
2930 }
2931 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2932 {
2933 char *end = NULL;
2934 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2935 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2936 {
2937 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002938 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2939 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002940 return ValueObjectSP();
2941 }
2942 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2943 {
Enrico Granata622be232014-10-21 20:52:14 +00002944 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002945 {
2946 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002947 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2948 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002949 return root;
2950 }
2951 else
2952 {
2953 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002954 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2955 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002956 return ValueObjectSP();
2957 }
2958 }
2959 // from here on we do have a valid index
Enrico Granata622be232014-10-21 20:52:14 +00002960 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002961 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002962 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2963 if (!child_valobj_sp)
2964 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002965 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002966 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2967 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002968 if (child_valobj_sp)
2969 {
2970 root = child_valobj_sp;
2971 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002972 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002973 continue;
2974 }
2975 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002976 {
2977 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002978 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2979 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002980 return ValueObjectSP();
2981 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002982 }
Enrico Granata622be232014-10-21 20:52:14 +00002983 else if (root_clang_type_info.Test(eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002984 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002985 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granata622be232014-10-21 20:52:14 +00002986 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002987 {
2988 Error error;
2989 root = root->Dereference(error);
2990 if (error.Fail() || !root.get())
2991 {
2992 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002993 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2994 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002995 return ValueObjectSP();
2996 }
2997 else
2998 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002999 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003000 continue;
3001 }
3002 }
3003 else
3004 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003005 if (root->GetClangType().GetMinimumLanguage() == eLanguageTypeObjC
Enrico Granata622be232014-10-21 20:52:14 +00003006 && pointee_clang_type_info.AllClear(eTypeIsPointer)
Greg Clayton84db9102012-03-26 23:03:23 +00003007 && root->HasSyntheticValue()
3008 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00003009 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003010 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00003011 }
3012 else
3013 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003014 if (!root.get())
3015 {
3016 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003017 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3018 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003019 return ValueObjectSP();
3020 }
3021 else
3022 {
3023 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003024 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003025 continue;
3026 }
3027 }
3028 }
Enrico Granata622be232014-10-21 20:52:14 +00003029 else if (root_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003030 {
3031 root = root->GetSyntheticBitFieldChild(index, index, true);
3032 if (!root.get())
3033 {
3034 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003035 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3036 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003037 return ValueObjectSP();
3038 }
3039 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3040 {
3041 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003042 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3043 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003044 return root;
3045 }
3046 }
Enrico Granata622be232014-10-21 20:52:14 +00003047 else if (root_clang_type_info.Test(eTypeIsVector))
Enrico Granata08a1bb82013-06-19 00:00:45 +00003048 {
3049 root = root->GetChildAtIndex(index, true);
3050 if (!root.get())
3051 {
3052 *first_unparsed = expression_cstr;
3053 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3054 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3055 return ValueObjectSP();
3056 }
3057 else
3058 {
3059 *first_unparsed = end+1; // skip ]
3060 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3061 continue;
3062 }
3063 }
Enrico Granata86cc9822012-03-19 22:58:49 +00003064 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00003065 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003066 if (root->HasSyntheticValue())
3067 root = root->GetSyntheticValue();
3068 else if (!root->IsSynthetic())
3069 {
3070 *first_unparsed = expression_cstr;
3071 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3072 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3073 return ValueObjectSP();
3074 }
3075 // if we are here, then root itself is a synthetic VO.. should be good to go
3076
Enrico Granata27b625e2011-08-09 01:04:56 +00003077 if (!root.get())
3078 {
3079 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003080 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3081 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3082 return ValueObjectSP();
3083 }
3084 root = root->GetChildAtIndex(index, true);
3085 if (!root.get())
3086 {
3087 *first_unparsed = expression_cstr;
3088 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3089 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003090 return ValueObjectSP();
3091 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00003092 else
3093 {
3094 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003095 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00003096 continue;
3097 }
Enrico Granata27b625e2011-08-09 01:04:56 +00003098 }
3099 else
3100 {
3101 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003102 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3103 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003104 return ValueObjectSP();
3105 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003106 }
3107 else // we have a low and a high index
3108 {
3109 char *end = NULL;
3110 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3111 if (!end || end != separator_position) // if something weird is in our way return an error
3112 {
3113 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003114 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3115 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003116 return ValueObjectSP();
3117 }
3118 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3119 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3120 {
3121 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003122 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3123 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003124 return ValueObjectSP();
3125 }
3126 if (index_lower > index_higher) // swap indices if required
3127 {
3128 unsigned long temp = index_lower;
3129 index_lower = index_higher;
3130 index_higher = temp;
3131 }
Enrico Granata622be232014-10-21 20:52:14 +00003132 if (root_clang_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003133 {
3134 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3135 if (!root.get())
3136 {
3137 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003138 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3139 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003140 return ValueObjectSP();
3141 }
3142 else
3143 {
3144 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003145 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3146 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003147 return root;
3148 }
3149 }
Enrico Granata622be232014-10-21 20:52:14 +00003150 else if (root_clang_type_info.Test(eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granata86cc9822012-03-19 22:58:49 +00003151 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granata622be232014-10-21 20:52:14 +00003152 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003153 {
3154 Error error;
3155 root = root->Dereference(error);
3156 if (error.Fail() || !root.get())
3157 {
3158 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003159 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3160 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003161 return ValueObjectSP();
3162 }
3163 else
3164 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003165 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003166 continue;
3167 }
3168 }
3169 else
3170 {
3171 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003172 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3173 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003174 return root;
3175 }
3176 }
3177 break;
3178 }
3179 default: // some non-separator is in the way
3180 {
3181 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003182 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3183 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003184 return ValueObjectSP();
3185 break;
3186 }
3187 }
3188 }
3189}
3190
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003191int
3192ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3193 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00003194 ValueObjectSP root,
3195 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003196 ExpressionPathScanEndReason* reason_to_stop,
3197 ExpressionPathEndResultType* final_result,
3198 const GetValueForExpressionPathOptions& options,
3199 ExpressionPathAftermath* what_next)
3200{
3201 if (!root.get())
3202 return 0;
3203
3204 *first_unparsed = expression_cstr;
3205
3206 while (true)
3207 {
3208
3209 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3210
Greg Clayton57ee3062013-07-11 22:46:58 +00003211 ClangASTType root_clang_type = root->GetClangType();
3212 ClangASTType pointee_clang_type;
3213 Flags pointee_clang_type_info;
3214 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003215 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00003216 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003217
3218 if (!expression_cstr || *expression_cstr == '\0')
3219 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003220 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003221 list->Append(root);
3222 return 1;
3223 }
3224
3225 switch (*expression_cstr)
3226 {
3227 case '[':
3228 {
Enrico Granata622be232014-10-21 20:52:14 +00003229 if (!root_clang_type_info.Test(eTypeIsArray) && !root_clang_type_info.Test(eTypeIsPointer)) // if this is not a T[] nor a T*
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003230 {
Enrico Granata622be232014-10-21 20:52:14 +00003231 if (!root_clang_type_info.Test(eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003232 {
3233 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003234 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3235 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003236 return 0;
3237 }
3238 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3239 {
3240 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003241 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3242 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003243 return 0;
3244 }
3245 }
3246 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3247 {
Enrico Granata622be232014-10-21 20:52:14 +00003248 if (!root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003249 {
3250 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003251 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3252 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003253 return 0;
3254 }
3255 else // expand this into list
3256 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003257 const size_t max_index = root->GetNumChildren() - 1;
3258 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003259 {
3260 ValueObjectSP child =
3261 root->GetChildAtIndex(index, true);
3262 list->Append(child);
3263 }
3264 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003265 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3266 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003267 return max_index; // tell me number of items I added to the VOList
3268 }
3269 }
3270 const char *separator_position = ::strchr(expression_cstr+1,'-');
3271 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3272 if (!close_bracket_position) // if there is no ], this is a syntax error
3273 {
3274 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003275 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3276 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003277 return 0;
3278 }
3279 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3280 {
3281 char *end = NULL;
3282 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3283 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3284 {
3285 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003286 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3287 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003288 return 0;
3289 }
3290 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3291 {
Enrico Granata622be232014-10-21 20:52:14 +00003292 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003293 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003294 const size_t max_index = root->GetNumChildren() - 1;
3295 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003296 {
3297 ValueObjectSP child =
3298 root->GetChildAtIndex(index, true);
3299 list->Append(child);
3300 }
3301 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003302 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3303 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003304 return max_index; // tell me number of items I added to the VOList
3305 }
3306 else
3307 {
3308 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003309 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3310 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003311 return 0;
3312 }
3313 }
3314 // from here on we do have a valid index
Enrico Granata622be232014-10-21 20:52:14 +00003315 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003316 {
3317 root = root->GetChildAtIndex(index, true);
3318 if (!root.get())
3319 {
3320 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003321 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3322 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003323 return 0;
3324 }
3325 else
3326 {
3327 list->Append(root);
3328 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003329 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3330 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003331 return 1;
3332 }
3333 }
Enrico Granata622be232014-10-21 20:52:14 +00003334 else if (root_clang_type_info.Test(eTypeIsPointer))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003335 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003336 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granata622be232014-10-21 20:52:14 +00003337 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003338 {
3339 Error error;
3340 root = root->Dereference(error);
3341 if (error.Fail() || !root.get())
3342 {
3343 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003344 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3345 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003346 return 0;
3347 }
3348 else
3349 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003350 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003351 continue;
3352 }
3353 }
3354 else
3355 {
3356 root = root->GetSyntheticArrayMemberFromPointer(index, true);
3357 if (!root.get())
3358 {
3359 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003360 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3361 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003362 return 0;
3363 }
3364 else
3365 {
3366 list->Append(root);
3367 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003368 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3369 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003370 return 1;
3371 }
3372 }
3373 }
3374 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3375 {
3376 root = root->GetSyntheticBitFieldChild(index, index, true);
3377 if (!root.get())
3378 {
3379 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003380 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3381 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003382 return 0;
3383 }
3384 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3385 {
3386 list->Append(root);
3387 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003388 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3389 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003390 return 1;
3391 }
3392 }
3393 }
3394 else // we have a low and a high index
3395 {
3396 char *end = NULL;
3397 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3398 if (!end || end != separator_position) // if something weird is in our way return an error
3399 {
3400 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003401 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3402 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003403 return 0;
3404 }
3405 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3406 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3407 {
3408 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003409 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3410 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003411 return 0;
3412 }
3413 if (index_lower > index_higher) // swap indices if required
3414 {
3415 unsigned long temp = index_lower;
3416 index_lower = index_higher;
3417 index_higher = temp;
3418 }
Enrico Granata622be232014-10-21 20:52:14 +00003419 if (root_clang_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003420 {
3421 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3422 if (!root.get())
3423 {
3424 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003425 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3426 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003427 return 0;
3428 }
3429 else
3430 {
3431 list->Append(root);
3432 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003433 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3434 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003435 return 1;
3436 }
3437 }
Enrico Granata622be232014-10-21 20:52:14 +00003438 else if (root_clang_type_info.Test(eTypeIsPointer) && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granata86cc9822012-03-19 22:58:49 +00003439 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granata622be232014-10-21 20:52:14 +00003440 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003441 {
3442 Error error;
3443 root = root->Dereference(error);
3444 if (error.Fail() || !root.get())
3445 {
3446 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003447 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3448 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003449 return 0;
3450 }
3451 else
3452 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003453 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003454 continue;
3455 }
3456 }
3457 else
3458 {
Johnny Chen44805302011-07-19 19:48:13 +00003459 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003460 index <= index_higher; index++)
3461 {
3462 ValueObjectSP child =
3463 root->GetChildAtIndex(index, true);
3464 list->Append(child);
3465 }
3466 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003467 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3468 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003469 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3470 }
3471 }
3472 break;
3473 }
3474 default: // some non-[ separator, or something entirely wrong, is in the way
3475 {
3476 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003477 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3478 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003479 return 0;
3480 break;
3481 }
3482 }
3483 }
3484}
3485
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003486void
3487ValueObject::LogValueObject (Log *log)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003488{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003489 if (log)
3490 return LogValueObject (log, DumpValueObjectOptions::DefaultOptions());
Greg Clayton1d3afba2010-10-05 00:00:42 +00003491}
3492
Enrico Granata0c489f52012-03-01 04:24:26 +00003493void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003494ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003495{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003496 if (log)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003497 {
3498 StreamString s;
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003499 Dump (s, options);
Greg Claytonf830dbb2012-03-22 18:15:37 +00003500 if (s.GetSize())
3501 log->PutCString(s.GetData());
3502 }
3503}
3504
3505void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003506ValueObject::Dump (Stream &s)
Enrico Granata0c489f52012-03-01 04:24:26 +00003507{
3508
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003509 ValueObjectPrinter printer(this,&s,DumpValueObjectOptions::DefaultOptions());
3510 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003511}
3512
3513void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003514ValueObject::Dump (Stream &s,
3515 const DumpValueObjectOptions& options)
Enrico Granata0c489f52012-03-01 04:24:26 +00003516{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003517 ValueObjectPrinter printer(this,&s,options);
3518 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003519}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003520
3521ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003522ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003523{
3524 ValueObjectSP valobj_sp;
3525
Enrico Granatac3e320a2011-08-02 17:27:39 +00003526 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003527 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003528 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003529
3530 DataExtractor data;
3531 data.SetByteOrder (m_data.GetByteOrder());
3532 data.SetAddressByteSize(m_data.GetAddressByteSize());
3533
Enrico Granata9f1e2042012-04-24 22:15:37 +00003534 if (IsBitfield())
3535 {
3536 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
Greg Clayton57ee3062013-07-11 22:46:58 +00003537 m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Enrico Granata9f1e2042012-04-24 22:15:37 +00003538 }
3539 else
Greg Clayton57ee3062013-07-11 22:46:58 +00003540 m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003541
3542 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003543 GetClangType(),
3544 name,
3545 data,
3546 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003547 }
Jim Ingham6035b672011-03-31 00:19:25 +00003548
3549 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003550 {
Greg Claytoneeb15652013-12-10 23:16:40 +00003551 ExecutionContext exe_ctx (GetExecutionContextRef());
3552 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003553 }
3554 return valobj_sp;
3555}
3556
Enrico Granata538a88a2014-10-09 18:24:30 +00003557ValueObjectSP
3558ValueObject::GetQualifiedRepresentationIfAvailable (lldb::DynamicValueType dynValue,
3559 bool synthValue)
3560{
3561 ValueObjectSP result_sp(GetSP());
3562
3563 switch (dynValue)
3564 {
3565 case lldb::eDynamicCanRunTarget:
3566 case lldb::eDynamicDontRunTarget:
3567 {
3568 if (!result_sp->IsDynamic())
3569 {
3570 if (result_sp->GetDynamicValue(dynValue))
3571 result_sp = result_sp->GetDynamicValue(dynValue);
3572 }
3573 }
3574 break;
3575 case lldb::eNoDynamicValues:
Enrico Granata538a88a2014-10-09 18:24:30 +00003576 {
3577 if (result_sp->IsDynamic())
3578 {
3579 if (result_sp->GetStaticValue())
3580 result_sp = result_sp->GetStaticValue();
3581 }
3582 }
3583 break;
3584 }
3585
3586 if (synthValue)
3587 {
3588 if (!result_sp->IsSynthetic())
3589 {
3590 if (result_sp->GetSyntheticValue())
3591 result_sp = result_sp->GetSyntheticValue();
3592 }
3593 }
3594 else
3595 {
3596 if (result_sp->IsSynthetic())
3597 {
3598 if (result_sp->GetNonSyntheticValue())
3599 result_sp = result_sp->GetNonSyntheticValue();
3600 }
3601 }
3602
3603 return result_sp;
3604}
3605
Greg Clayton759e7442014-07-19 00:12:57 +00003606lldb::addr_t
3607ValueObject::GetCPPVTableAddress (AddressType &address_type)
3608{
3609 ClangASTType pointee_type;
3610 ClangASTType this_type(GetClangType());
3611 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3612 if (type_info)
3613 {
3614 bool ptr_or_ref = false;
Enrico Granata622be232014-10-21 20:52:14 +00003615 if (type_info & (eTypeIsPointer | eTypeIsReference))
Greg Clayton759e7442014-07-19 00:12:57 +00003616 {
3617 ptr_or_ref = true;
3618 type_info = pointee_type.GetTypeInfo();
3619 }
3620
Enrico Granata622be232014-10-21 20:52:14 +00003621 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
Greg Clayton759e7442014-07-19 00:12:57 +00003622 if ((type_info & cpp_class) == cpp_class)
3623 {
3624 if (ptr_or_ref)
3625 {
3626 address_type = GetAddressTypeOfChildren();
3627 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3628 }
3629 else
3630 return GetAddressOf (false, &address_type);
3631 }
3632 }
3633
3634 address_type = eAddressTypeInvalid;
3635 return LLDB_INVALID_ADDRESS;
3636}
3637
Greg Claytonafacd142011-09-02 01:15:17 +00003638ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003639ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003640{
Jim Ingham58b59f92011-04-22 23:53:53 +00003641 if (m_deref_valobj)
3642 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003643
Greg Clayton54979cd2010-12-15 05:08:08 +00003644 const bool is_pointer_type = IsPointerType();
3645 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003646 {
3647 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003648 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003649
3650 std::string child_name_str;
3651 uint32_t child_byte_size = 0;
3652 int32_t child_byte_offset = 0;
3653 uint32_t child_bitfield_bit_size = 0;
3654 uint32_t child_bitfield_bit_offset = 0;
3655 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003656 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003657 const bool transparent_pointers = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00003658 ClangASTType clang_type = GetClangType();
3659 ClangASTType child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003660
Greg Claytoncc4d0142012-02-17 07:49:44 +00003661 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003662
Greg Clayton57ee3062013-07-11 22:46:58 +00003663 child_clang_type = clang_type.GetChildClangTypeAtIndex (&exe_ctx,
Greg Clayton57ee3062013-07-11 22:46:58 +00003664 0,
3665 transparent_pointers,
3666 omit_empty_base_classes,
3667 ignore_array_bounds,
3668 child_name_str,
3669 child_byte_size,
3670 child_byte_offset,
3671 child_bitfield_bit_size,
3672 child_bitfield_bit_offset,
3673 child_is_base_class,
Greg Clayton759e7442014-07-19 00:12:57 +00003674 child_is_deref_of_parent,
3675 this);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003676 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003677 {
3678 ConstString child_name;
3679 if (!child_name_str.empty())
3680 child_name.SetCString (child_name_str.c_str());
3681
Jim Ingham58b59f92011-04-22 23:53:53 +00003682 m_deref_valobj = new ValueObjectChild (*this,
Jim Ingham58b59f92011-04-22 23:53:53 +00003683 child_clang_type,
3684 child_name,
3685 child_byte_size,
3686 child_byte_offset,
3687 child_bitfield_bit_size,
3688 child_bitfield_bit_offset,
3689 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003690 child_is_deref_of_parent,
3691 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003692 }
3693 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003694
Jim Ingham58b59f92011-04-22 23:53:53 +00003695 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003696 {
3697 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003698 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003699 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003700 else
3701 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003702 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003703 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003704
3705 if (is_pointer_type)
3706 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3707 else
3708 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003709 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003710 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003711}
3712
Greg Claytonafacd142011-09-02 01:15:17 +00003713ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003714ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003715{
Jim Ingham78a685a2011-04-16 00:01:13 +00003716 if (m_addr_of_valobj_sp)
3717 return m_addr_of_valobj_sp;
3718
Greg Claytone0d378b2011-03-24 21:19:54 +00003719 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003720 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003721 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003722 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003723 if (addr != LLDB_INVALID_ADDRESS)
3724 {
3725 switch (address_type)
3726 {
3727 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003728 {
3729 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003730 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003731 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3732 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003733 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003734
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003735 case eAddressTypeFile:
3736 case eAddressTypeLoad:
3737 case eAddressTypeHost:
3738 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003739 ClangASTType clang_type = GetClangType();
3740 if (clang_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003741 {
3742 std::string name (1, '&');
3743 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003744 ExecutionContext exe_ctx (GetExecutionContextRef());
3745 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00003746 clang_type.GetPointerType(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003747 ConstString (name.c_str()),
3748 addr,
3749 eAddressTypeInvalid,
3750 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003751 }
3752 }
3753 break;
3754 }
3755 }
Sean Callananed185ab2013-04-19 19:47:32 +00003756 else
3757 {
3758 StreamString expr_path_strm;
3759 GetExpressionPath(expr_path_strm, true);
3760 error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str());
3761 }
3762
Jim Ingham78a685a2011-04-16 00:01:13 +00003763 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003764}
3765
Greg Clayton9a142cf2012-02-03 05:34:10 +00003766ValueObjectSP
3767ValueObject::Cast (const ClangASTType &clang_ast_type)
3768{
Greg Clayton81e871e2012-02-04 02:27:34 +00003769 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003770}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003771
Greg Claytonafacd142011-09-02 01:15:17 +00003772ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003773ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3774{
Greg Claytonafacd142011-09-02 01:15:17 +00003775 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003776 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003777 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003778
3779 if (ptr_value != LLDB_INVALID_ADDRESS)
3780 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003781 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003782 ExecutionContext exe_ctx (GetExecutionContextRef());
3783 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003784 name,
3785 ptr_addr,
3786 clang_ast_type);
3787 }
3788 return valobj_sp;
3789}
3790
Greg Claytonafacd142011-09-02 01:15:17 +00003791ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003792ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3793{
Greg Claytonafacd142011-09-02 01:15:17 +00003794 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003795 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003796 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003797
3798 if (ptr_value != LLDB_INVALID_ADDRESS)
3799 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003800 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003801 ExecutionContext exe_ctx (GetExecutionContextRef());
3802 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003803 name,
3804 ptr_addr,
3805 type_sp);
3806 }
3807 return valobj_sp;
3808}
3809
Jim Ingham6035b672011-03-31 00:19:25 +00003810ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003811 m_mod_id(),
3812 m_exe_ctx_ref(),
3813 m_needs_update (true),
3814 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003815{
3816}
3817
3818ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003819 m_mod_id(),
3820 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003821 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003822 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003823{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003824 ExecutionContext exe_ctx(exe_scope);
3825 TargetSP target_sp (exe_ctx.GetTargetSP());
3826 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003827 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003828 m_exe_ctx_ref.SetTargetSP (target_sp);
3829 ProcessSP process_sp (exe_ctx.GetProcessSP());
3830 if (!process_sp)
3831 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003832
Greg Claytoncc4d0142012-02-17 07:49:44 +00003833 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003834 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003835 m_mod_id = process_sp->GetModID();
3836 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003837
Greg Claytoncc4d0142012-02-17 07:49:44 +00003838 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003839
Greg Claytoncc4d0142012-02-17 07:49:44 +00003840 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003841 {
3842 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003843 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003844 }
Jim Ingham6035b672011-03-31 00:19:25 +00003845
Greg Claytoncc4d0142012-02-17 07:49:44 +00003846 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003847 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003848 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003849
Jason Molendab57e4a12013-11-04 09:33:30 +00003850 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003851 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003852 {
3853 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003854 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003855 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003856 if (frame_sp)
3857 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003858 }
3859 }
3860 }
Jim Ingham6035b672011-03-31 00:19:25 +00003861}
3862
3863ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003864 m_mod_id(),
3865 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3866 m_needs_update (true),
3867 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003868{
3869}
3870
3871ValueObject::EvaluationPoint::~EvaluationPoint ()
3872{
3873}
3874
Jim Ingham6035b672011-03-31 00:19:25 +00003875// This function checks the EvaluationPoint against the current process state. If the current
3876// state matches the evaluation point, or the evaluation point is already invalid, then we return
3877// false, meaning "no change". If the current state is different, we update our state, and return
3878// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3879// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003880// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003881
3882bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003883ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003884{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003885
3886 // Start with the target, if it is NULL, then we're obviously not going to get any further:
Greg Clayton44d93782014-01-27 23:43:24 +00003887 const bool thread_and_frame_only_if_stopped = true;
3888 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
Jim Ingham73ca05a2011-12-17 01:35:57 +00003889
Greg Claytoncc4d0142012-02-17 07:49:44 +00003890 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003891 return false;
3892
Jim Ingham6035b672011-03-31 00:19:25 +00003893 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003894 Process *process = exe_ctx.GetProcessPtr();
3895 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003896 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003897
Jim Ingham6035b672011-03-31 00:19:25 +00003898 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003899 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003900
Jim Ingham78a685a2011-04-16 00:01:13 +00003901 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3902 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003903 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003904 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003905
Greg Clayton23f59502012-07-17 03:23:13 +00003906 bool changed = false;
3907 const bool was_valid = m_mod_id.IsValid();
3908 if (was_valid)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003909 {
3910 if (m_mod_id == current_mod_id)
3911 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003912 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003913 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003914 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003915 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003916 else
3917 {
3918 m_mod_id = current_mod_id;
3919 m_needs_update = true;
3920 changed = true;
3921 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003922 }
Jim Ingham6035b672011-03-31 00:19:25 +00003923
Jim Ingham73ca05a2011-12-17 01:35:57 +00003924 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3925 // That way we'll be sure to return a valid exe_scope.
3926 // If we used to have a thread or a frame but can't find it anymore, then mark ourselves as invalid.
Jim Ingham6035b672011-03-31 00:19:25 +00003927
Greg Claytoncc4d0142012-02-17 07:49:44 +00003928 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003929 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003930 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3931 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003932 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003933 if (m_exe_ctx_ref.HasFrameRef())
3934 {
Jason Molendab57e4a12013-11-04 09:33:30 +00003935 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003936 if (!frame_sp)
3937 {
3938 // We used to have a frame, but now it is gone
3939 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003940 changed = was_valid;
Greg Claytoncc4d0142012-02-17 07:49:44 +00003941 }
3942 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003943 }
Jim Ingham6035b672011-03-31 00:19:25 +00003944 else
3945 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003946 // We used to have a thread, but now it is gone
3947 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003948 changed = was_valid;
Jim Ingham6035b672011-03-31 00:19:25 +00003949 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003950
Jim Ingham6035b672011-03-31 00:19:25 +00003951 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003952 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003953}
3954
Jim Ingham61be0902011-05-02 18:13:59 +00003955void
3956ValueObject::EvaluationPoint::SetUpdated ()
3957{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003958 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3959 if (process_sp)
3960 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003961 m_first_update = false;
3962 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003963}
3964
3965
Enrico Granataf2bbf712011-07-15 02:26:42 +00003966
3967void
Enrico Granata86cc9822012-03-19 22:58:49 +00003968ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003969{
Enrico Granata86cc9822012-03-19 22:58:49 +00003970 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3971 m_value_str.clear();
3972
3973 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3974 m_location_str.clear();
3975
3976 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
Enrico Granata86cc9822012-03-19 22:58:49 +00003977 m_summary_str.clear();
Enrico Granata86cc9822012-03-19 22:58:49 +00003978
3979 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3980 m_object_desc_str.clear();
3981
3982 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3983 {
3984 if (m_synthetic_value)
3985 m_synthetic_value = NULL;
3986 }
Enrico Granata744794a2014-09-05 21:46:22 +00003987
3988 if ((clear_mask & eClearUserVisibleDataItemsValidator) == eClearUserVisibleDataItemsValidator)
3989 m_validation_result.reset();
Johnny Chen44805302011-07-19 19:48:13 +00003990}
Enrico Granata9128ee22011-09-06 19:20:51 +00003991
3992SymbolContextScope *
3993ValueObject::GetSymbolContextScope()
3994{
3995 if (m_parent)
3996 {
3997 if (!m_parent->IsPointerOrReferenceType())
3998 return m_parent->GetSymbolContextScope();
3999 }
4000 return NULL;
4001}
Enrico Granatab2698cd2012-09-13 18:27:09 +00004002
4003lldb::ValueObjectSP
4004ValueObject::CreateValueObjectFromExpression (const char* name,
4005 const char* expression,
4006 const ExecutionContext& exe_ctx)
4007{
4008 lldb::ValueObjectSP retval_sp;
4009 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
4010 if (!target_sp)
4011 return retval_sp;
4012 if (!expression || !*expression)
4013 return retval_sp;
4014 target_sp->EvaluateExpression (expression,
4015 exe_ctx.GetFrameSP().get(),
4016 retval_sp);
4017 if (retval_sp && name && *name)
4018 retval_sp->SetName(ConstString(name));
4019 return retval_sp;
4020}
4021
4022lldb::ValueObjectSP
4023ValueObject::CreateValueObjectFromAddress (const char* name,
4024 uint64_t address,
4025 const ExecutionContext& exe_ctx,
4026 ClangASTType type)
4027{
Greg Clayton57ee3062013-07-11 22:46:58 +00004028 if (type)
Enrico Granatab2698cd2012-09-13 18:27:09 +00004029 {
Greg Clayton57ee3062013-07-11 22:46:58 +00004030 ClangASTType pointer_type(type.GetPointerType());
4031 if (pointer_type)
4032 {
4033 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
4034 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4035 pointer_type,
4036 ConstString(name),
4037 buffer,
4038 lldb::endian::InlHostByteOrder(),
4039 exe_ctx.GetAddressByteSize()));
4040 if (ptr_result_valobj_sp)
4041 {
4042 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
4043 Error err;
4044 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
4045 if (ptr_result_valobj_sp && name && *name)
4046 ptr_result_valobj_sp->SetName(ConstString(name));
4047 }
4048 return ptr_result_valobj_sp;
4049 }
Enrico Granatab2698cd2012-09-13 18:27:09 +00004050 }
Greg Clayton57ee3062013-07-11 22:46:58 +00004051 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00004052}
4053
4054lldb::ValueObjectSP
4055ValueObject::CreateValueObjectFromData (const char* name,
Enrico Granata7ca1c762014-03-31 23:02:25 +00004056 const DataExtractor& data,
Enrico Granatab2698cd2012-09-13 18:27:09 +00004057 const ExecutionContext& exe_ctx,
4058 ClangASTType type)
4059{
4060 lldb::ValueObjectSP new_value_sp;
4061 new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00004062 type,
Enrico Granatab2698cd2012-09-13 18:27:09 +00004063 ConstString(name),
4064 data,
4065 LLDB_INVALID_ADDRESS);
4066 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
4067 if (new_value_sp && name && *name)
4068 new_value_sp->SetName(ConstString(name));
4069 return new_value_sp;
4070}
Enrico Granata4873e522013-04-11 22:48:58 +00004071
4072ModuleSP
4073ValueObject::GetModule ()
4074{
4075 ValueObject* root(GetRoot());
4076 if (root != this)
4077 return root->GetModule();
4078 return lldb::ModuleSP();
4079}
4080
4081ValueObject*
4082ValueObject::GetRoot ()
4083{
4084 if (m_root)
4085 return m_root;
4086 ValueObject* parent = m_parent;
4087 if (!parent)
4088 return (m_root = this);
4089 while (parent->m_parent)
4090 {
4091 if (parent->m_root)
4092 return (m_root = parent->m_root);
4093 parent = parent->m_parent;
4094 }
4095 return (m_root = parent);
4096}
4097
4098AddressType
4099ValueObject::GetAddressTypeOfChildren()
4100{
4101 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
4102 {
4103 ValueObject* root(GetRoot());
4104 if (root != this)
4105 return root->GetAddressTypeOfChildren();
4106 }
4107 return m_address_type_of_ptr_or_ref_children;
4108}
4109
4110lldb::DynamicValueType
4111ValueObject::GetDynamicValueType ()
4112{
4113 ValueObject* with_dv_info = this;
4114 while (with_dv_info)
4115 {
4116 if (with_dv_info->HasDynamicValueTypeInfo())
4117 return with_dv_info->GetDynamicValueTypeImpl();
4118 with_dv_info = with_dv_info->m_parent;
4119 }
4120 return lldb::eNoDynamicValues;
4121}
Enrico Granata39d51412013-05-31 17:43:40 +00004122
Enrico Granata4873e522013-04-11 22:48:58 +00004123lldb::Format
4124ValueObject::GetFormat () const
4125{
4126 const ValueObject* with_fmt_info = this;
4127 while (with_fmt_info)
4128 {
4129 if (with_fmt_info->m_format != lldb::eFormatDefault)
4130 return with_fmt_info->m_format;
4131 with_fmt_info = with_fmt_info->m_parent;
4132 }
4133 return m_format;
4134}
Enrico Granatad07cfd32014-10-08 18:27:36 +00004135
4136bool
4137ValueObject::CanProvideValue ()
4138{
4139 return (false == GetClangType().IsAggregateType());
4140}