blob: e09049d03915ec3886f4d8b6ebd8949b913b698d [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"
Enrico Granatac1247f52014-11-06 21:23:20 +000047#include "lldb/Symbol/CompileUnit.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Symbol/Type.h"
49
Jim Ingham53c47f12010-09-10 23:12:17 +000050#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000051#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000052#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Process.h"
54#include "lldb/Target/RegisterContext.h"
Greg Claytond5944cd2013-12-06 01:12:00 +000055#include "lldb/Target/SectionLoadList.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000056#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
59using namespace lldb;
60using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000061using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062
Greg Claytonafacd142011-09-02 01:15:17 +000063static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064
65//----------------------------------------------------------------------
66// ValueObject constructor
67//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000068ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000070 m_parent (&parent),
Enrico Granata4873e522013-04-11 22:48:58 +000071 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000072 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000073 m_name (),
74 m_data (),
75 m_value (),
76 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000077 m_value_str (),
78 m_old_value_str (),
79 m_location_str (),
80 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000081 m_object_desc_str (),
Enrico Granata744794a2014-09-05 21:46:22 +000082 m_validation_result(),
Jim Ingham58b59f92011-04-22 23:53:53 +000083 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000084 m_children (),
85 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000086 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000087 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000088 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000089 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +000090 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000091 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +000092 m_type_summary_sp(),
93 m_type_format_sp(),
94 m_synthetic_children_sp(),
Enrico Granata744794a2014-09-05 21:46:22 +000095 m_type_validator_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +000096 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000097 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
Greg Clayton288bdf92010-09-02 02:59:18 +000098 m_value_is_valid (false),
99 m_value_did_change (false),
100 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000101 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000102 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000103 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000104 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000105 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000106 m_is_getting_summary(false),
107 m_did_calculate_complete_objc_class_type(false)
Jim Ingham6035b672011-03-31 00:19:25 +0000108{
Jim Ingham58b59f92011-04-22 23:53:53 +0000109 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000110}
111
112//----------------------------------------------------------------------
113// ValueObject constructor
114//----------------------------------------------------------------------
Enrico Granata9128ee22011-09-06 19:20:51 +0000115ValueObject::ValueObject (ExecutionContextScope *exe_scope,
116 AddressType child_ptr_or_ref_addr_type) :
Jim Ingham6035b672011-03-31 00:19:25 +0000117 UserID (++g_value_obj_uid), // Unique identifier for every value object
118 m_parent (NULL),
Enrico Granata4873e522013-04-11 22:48:58 +0000119 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000120 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000121 m_name (),
122 m_data (),
123 m_value (),
124 m_error (),
125 m_value_str (),
126 m_old_value_str (),
127 m_location_str (),
128 m_summary_str (),
129 m_object_desc_str (),
Enrico Granata744794a2014-09-05 21:46:22 +0000130 m_validation_result(),
Jim Ingham58b59f92011-04-22 23:53:53 +0000131 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000132 m_children (),
133 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000134 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000135 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000136 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000137 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +0000138 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000139 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +0000140 m_type_summary_sp(),
141 m_type_format_sp(),
142 m_synthetic_children_sp(),
Enrico Granata744794a2014-09-05 21:46:22 +0000143 m_type_validator_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +0000144 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000145 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
Jim Ingham6035b672011-03-31 00:19:25 +0000146 m_value_is_valid (false),
147 m_value_did_change (false),
148 m_children_count_valid (false),
149 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000150 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000151 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000152 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000153 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000154 m_is_getting_summary(false),
155 m_did_calculate_complete_objc_class_type(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156{
Jim Ingham58b59f92011-04-22 23:53:53 +0000157 m_manager = new ValueObjectManager();
158 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
161//----------------------------------------------------------------------
162// Destructor
163//----------------------------------------------------------------------
164ValueObject::~ValueObject ()
165{
166}
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000169ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170{
Enrico Granata4becb372011-06-29 22:27:15 +0000171
Enrico Granata9128ee22011-09-06 19:20:51 +0000172 bool did_change_formats = false;
173
Enrico Granata0a3958e2011-07-02 00:25:22 +0000174 if (update_format)
Enrico Granata5548cb52013-01-28 23:47:25 +0000175 did_change_formats = UpdateFormatsIfNeeded();
Enrico Granata4becb372011-06-29 22:27:15 +0000176
Greg Claytonb71f3842010-10-05 03:13:51 +0000177 // If this is a constant value, then our success is predicated on whether
178 // we have an error or not
179 if (GetIsConstant())
Enrico Granata9128ee22011-09-06 19:20:51 +0000180 {
Enrico Granatab1c6c482013-10-09 00:33:55 +0000181 // if you are constant, things might still have changed behind your back
182 // (e.g. you are a frozen object and things have changed deeper than you cared to freeze-dry yourself)
183 // in this case, your value has not changed, but "computed" entries might have, so you might now have
184 // a different summary, or a different object description. clear these so we will recompute them
Enrico Granata9128ee22011-09-06 19:20:51 +0000185 if (update_format && !did_change_formats)
Enrico Granatab1c6c482013-10-09 00:33:55 +0000186 ClearUserVisibleData(eClearUserVisibleDataItemsSummary | eClearUserVisibleDataItemsDescription);
Greg Claytonb71f3842010-10-05 03:13:51 +0000187 return m_error.Success();
Enrico Granata9128ee22011-09-06 19:20:51 +0000188 }
Greg Claytonb71f3842010-10-05 03:13:51 +0000189
Jim Ingham6035b672011-03-31 00:19:25 +0000190 bool first_update = m_update_point.IsFirstEvaluation();
191
192 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 {
Jim Ingham6035b672011-03-31 00:19:25 +0000194 m_update_point.SetUpdated();
195
196 // Save the old value using swap to avoid a string copy which
197 // also will clear our m_value_str
198 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199 {
Jim Ingham6035b672011-03-31 00:19:25 +0000200 m_old_value_valid = false;
201 }
202 else
203 {
204 m_old_value_valid = true;
205 m_old_value_str.swap (m_value_str);
Enrico Granata86cc9822012-03-19 22:58:49 +0000206 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham6035b672011-03-31 00:19:25 +0000207 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000208
Enrico Granataf2bbf712011-07-15 02:26:42 +0000209 ClearUserVisibleData();
210
Greg Claytonefbc7d22012-03-09 04:23:44 +0000211 if (IsInScope())
Jim Ingham6035b672011-03-31 00:19:25 +0000212 {
Greg Claytonefbc7d22012-03-09 04:23:44 +0000213 const bool value_was_valid = GetValueIsValid();
214 SetValueDidChange (false);
215
216 m_error.Clear();
217
218 // Call the pure virtual function to update the value
219 bool success = UpdateValue ();
220
221 SetValueIsValid (success);
222
223 if (first_update)
224 SetValueDidChange (false);
225 else if (!m_value_did_change && success == false)
226 {
227 // The value wasn't gotten successfully, so we mark this
228 // as changed if the value used to be valid and now isn't
229 SetValueDidChange (value_was_valid);
230 }
231 }
232 else
233 {
234 m_error.SetErrorString("out of scope");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235 }
236 }
237 return m_error.Success();
238}
239
Enrico Granata9128ee22011-09-06 19:20:51 +0000240bool
Enrico Granata5548cb52013-01-28 23:47:25 +0000241ValueObject::UpdateFormatsIfNeeded()
Enrico Granata4becb372011-06-29 22:27:15 +0000242{
Greg Clayton5160ce52013-03-27 23:08:40 +0000243 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata6f3533f2011-07-29 19:53:35 +0000244 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +0000245 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d",
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000246 GetName().GetCString(), static_cast<void*>(this),
247 m_last_format_mgr_revision,
248 DataVisualization::GetCurrentRevision());
249
Enrico Granata9128ee22011-09-06 19:20:51 +0000250 bool any_change = false;
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000251
Enrico Granata5548cb52013-01-28 23:47:25 +0000252 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()))
Enrico Granata4becb372011-06-29 22:27:15 +0000253 {
Enrico Granataa0db6ed2014-04-09 21:06:11 +0000254 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
255 any_change = true;
256
Enrico Granata852cc952013-10-08 19:03:22 +0000257 SetValueFormat(DataVisualization::GetFormat (*this, eNoDynamicValues));
Enrico Granata5548cb52013-01-28 23:47:25 +0000258 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000259#ifndef LLDB_DISABLE_PYTHON
Enrico Granata5548cb52013-01-28 23:47:25 +0000260 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000261#endif
Enrico Granata744794a2014-09-05 21:46:22 +0000262 SetValidator(DataVisualization::GetValidator(*this, GetDynamicValueType()));
Enrico Granata4becb372011-06-29 22:27:15 +0000263 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000264
Enrico Granata9128ee22011-09-06 19:20:51 +0000265 return any_change;
Enrico Granata4becb372011-06-29 22:27:15 +0000266}
267
Jim Ingham16e0c682011-08-12 23:34:31 +0000268void
269ValueObject::SetNeedsUpdate ()
270{
271 m_update_point.SetNeedsUpdate();
272 // We have to clear the value string here so ConstResult children will notice if their values are
273 // changed by hand (i.e. with SetValueAsCString).
Enrico Granata86cc9822012-03-19 22:58:49 +0000274 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000275}
276
Enrico Granata13ac0e22012-10-17 19:03:34 +0000277void
Enrico Granatae3e91512012-10-22 18:18:36 +0000278ValueObject::ClearDynamicTypeInformation ()
Enrico Granata13ac0e22012-10-17 19:03:34 +0000279{
Enrico Granata38c54632013-10-30 00:04:29 +0000280 m_children_count_valid = false;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000281 m_did_calculate_complete_objc_class_type = false;
Enrico Granatae3e91512012-10-22 18:18:36 +0000282 m_last_format_mgr_revision = 0;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000283 m_override_type = ClangASTType();
Enrico Granatae3e91512012-10-22 18:18:36 +0000284 SetValueFormat(lldb::TypeFormatImplSP());
285 SetSummaryFormat(lldb::TypeSummaryImplSP());
286 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000287}
288
Sean Callanan72772842012-02-22 23:57:45 +0000289ClangASTType
290ValueObject::MaybeCalculateCompleteType ()
291{
Greg Clayton57ee3062013-07-11 22:46:58 +0000292 ClangASTType clang_type(GetClangTypeImpl());
Sean Callanan356e17c2012-03-30 02:04:38 +0000293
Sean Callanan72772842012-02-22 23:57:45 +0000294 if (m_did_calculate_complete_objc_class_type)
295 {
296 if (m_override_type.IsValid())
297 return m_override_type;
298 else
Greg Clayton57ee3062013-07-11 22:46:58 +0000299 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000300 }
301
Greg Clayton57ee3062013-07-11 22:46:58 +0000302 ClangASTType class_type;
303 bool is_pointer_type = false;
Sean Callanan72772842012-02-22 23:57:45 +0000304
Greg Clayton57ee3062013-07-11 22:46:58 +0000305 if (clang_type.IsObjCObjectPointerType(&class_type))
Sean Callanan72772842012-02-22 23:57:45 +0000306 {
307 is_pointer_type = true;
308 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000309 else if (clang_type.IsObjCObjectOrInterfaceType())
Sean Callanan72772842012-02-22 23:57:45 +0000310 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000311 class_type = clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000312 }
313 else
314 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000315 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000316 }
317
318 m_did_calculate_complete_objc_class_type = true;
319
Greg Clayton57ee3062013-07-11 22:46:58 +0000320 if (class_type)
Sean Callanan72772842012-02-22 23:57:45 +0000321 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000322 ConstString class_name (class_type.GetConstTypeName());
Sean Callanan72772842012-02-22 23:57:45 +0000323
Greg Clayton57ee3062013-07-11 22:46:58 +0000324 if (class_name)
325 {
326 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
327
328 if (process_sp)
329 {
330 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
331
332 if (objc_language_runtime)
333 {
334 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name);
335
336 if (complete_objc_class_type_sp)
337 {
338 ClangASTType complete_class(complete_objc_class_type_sp->GetClangFullType());
339
340 if (complete_class.GetCompleteType())
341 {
342 if (is_pointer_type)
343 {
344 m_override_type = complete_class.GetPointerType();
345 }
346 else
347 {
348 m_override_type = complete_class;
349 }
350
351 if (m_override_type.IsValid())
352 return m_override_type;
353 }
354 }
355 }
356 }
357 }
Sean Callanan72772842012-02-22 23:57:45 +0000358 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000359 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000360}
361
Greg Clayton57ee3062013-07-11 22:46:58 +0000362ClangASTType
Sean Callanan72772842012-02-22 23:57:45 +0000363ValueObject::GetClangType ()
364{
Greg Clayton57ee3062013-07-11 22:46:58 +0000365 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000366}
367
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000368TypeImpl
369ValueObject::GetTypeImpl ()
370{
371 return TypeImpl(GetClangType());
372}
373
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374DataExtractor &
375ValueObject::GetDataExtractor ()
376{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000377 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378 return m_data;
379}
380
381const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000382ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000383{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000384 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 return m_error;
386}
387
388const ConstString &
389ValueObject::GetName() const
390{
391 return m_name;
392}
393
394const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000395ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396{
Enrico Granata82fabf82013-04-30 20:45:04 +0000397 return GetLocationAsCStringImpl(m_value,
398 m_data);
399}
400
401const char *
402ValueObject::GetLocationAsCStringImpl (const Value& value,
403 const DataExtractor& data)
404{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000405 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 {
407 if (m_location_str.empty())
408 {
409 StreamString sstr;
Enrico Granata82fabf82013-04-30 20:45:04 +0000410
411 Value::ValueType value_type = value.GetValueType();
412
413 switch (value_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000416 case Value::eValueTypeVector:
Enrico Granata82fabf82013-04-30 20:45:04 +0000417 if (value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000419 RegisterInfo *reg_info = value.GetRegisterInfo();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 if (reg_info)
421 {
422 if (reg_info->name)
423 m_location_str = reg_info->name;
424 else if (reg_info->alt_name)
425 m_location_str = reg_info->alt_name;
Enrico Granata82fabf82013-04-30 20:45:04 +0000426 if (m_location_str.empty())
427 m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 }
429 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000430 if (m_location_str.empty())
431 m_location_str = (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432 break;
433
434 case Value::eValueTypeLoadAddress:
435 case Value::eValueTypeFileAddress:
436 case Value::eValueTypeHostAddress:
437 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000438 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
439 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 m_location_str.swap(sstr.GetString());
441 }
442 break;
443 }
444 }
445 }
446 return m_location_str.c_str();
447}
448
449Value &
450ValueObject::GetValue()
451{
452 return m_value;
453}
454
455const Value &
456ValueObject::GetValue() const
457{
458 return m_value;
459}
460
461bool
Jim Ingham6035b672011-03-31 00:19:25 +0000462ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000463{
Enrico Granata6fd87d52011-08-04 01:41:02 +0000464 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
465 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000466 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Ingham16e0c682011-08-12 23:34:31 +0000467 Value tmp_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000468 scalar = tmp_value.ResolveValue(&exe_ctx);
Greg Claytondcad5022011-12-29 01:26:56 +0000469 if (scalar.IsValid())
470 {
471 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
472 if (bitfield_bit_size)
473 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset());
474 return true;
475 }
Enrico Granata6fd87d52011-08-04 01:41:02 +0000476 }
Greg Claytondcad5022011-12-29 01:26:56 +0000477 return false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000478}
479
480bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000481ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482{
Greg Clayton288bdf92010-09-02 02:59:18 +0000483 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000484}
485
486
487void
488ValueObject::SetValueIsValid (bool b)
489{
Greg Clayton288bdf92010-09-02 02:59:18 +0000490 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
493bool
Jim Ingham6035b672011-03-31 00:19:25 +0000494ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495{
Jim Ingham6035b672011-03-31 00:19:25 +0000496 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000497 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000498}
499
500void
501ValueObject::SetValueDidChange (bool value_changed)
502{
Greg Clayton288bdf92010-09-02 02:59:18 +0000503 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504}
505
506ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000507ValueObject::GetChildAtIndex (size_t idx, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000508{
509 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000510 // We may need to update our value if we are dynamic
511 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000512 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000513 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000515 // Check if we have already made the child value object?
Enrico Granata9d60f602012-03-09 03:09:58 +0000516 if (can_create && !m_children.HasChildAtIndex(idx))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000517 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000518 // No we haven't created the child at this index, so lets have our
519 // subclass do it and cache the result for quick future access.
Enrico Granata9d60f602012-03-09 03:09:58 +0000520 m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0));
Jim Ingham78a685a2011-04-16 00:01:13 +0000521 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000522
Enrico Granata9d60f602012-03-09 03:09:58 +0000523 ValueObject* child = m_children.GetChildAtIndex(idx);
524 if (child != NULL)
525 return child->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000526 }
527 return child_sp;
528}
529
Enrico Granata3309d882013-01-12 01:00:22 +0000530ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000531ValueObject::GetChildAtIndexPath (const std::initializer_list<size_t>& idxs,
532 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000533{
534 if (idxs.size() == 0)
535 return GetSP();
536 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000537 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000538 {
539 root = root->GetChildAtIndex(idx, true);
540 if (!root)
541 {
542 if (index_of_error)
543 *index_of_error = idx;
544 return root;
545 }
546 }
547 return root;
548}
549
550ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000551ValueObject::GetChildAtIndexPath (const std::initializer_list< std::pair<size_t, bool> >& idxs,
552 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000553{
554 if (idxs.size() == 0)
555 return GetSP();
556 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000557 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000558 {
559 root = root->GetChildAtIndex(idx.first, idx.second);
560 if (!root)
561 {
562 if (index_of_error)
563 *index_of_error = idx.first;
564 return root;
565 }
566 }
567 return root;
568}
569
570lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000571ValueObject::GetChildAtIndexPath (const std::vector<size_t> &idxs,
572 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000573{
574 if (idxs.size() == 0)
575 return GetSP();
576 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000577 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000578 {
579 root = root->GetChildAtIndex(idx, true);
580 if (!root)
581 {
582 if (index_of_error)
583 *index_of_error = idx;
584 return root;
585 }
586 }
587 return root;
588}
589
590lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000591ValueObject::GetChildAtIndexPath (const std::vector< std::pair<size_t, bool> > &idxs,
592 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000593{
594 if (idxs.size() == 0)
595 return GetSP();
596 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000597 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000598 {
599 root = root->GetChildAtIndex(idx.first, idx.second);
600 if (!root)
601 {
602 if (index_of_error)
603 *index_of_error = idx.first;
604 return root;
605 }
606 }
607 return root;
608}
609
Enrico Granatae2e220a2013-09-12 00:48:47 +0000610lldb::ValueObjectSP
611ValueObject::GetChildAtNamePath (const std::initializer_list<ConstString> &names,
612 ConstString* name_of_error)
613{
614 if (names.size() == 0)
615 return GetSP();
616 ValueObjectSP root(GetSP());
617 for (ConstString name : names)
618 {
619 root = root->GetChildMemberWithName(name, true);
620 if (!root)
621 {
622 if (name_of_error)
623 *name_of_error = name;
624 return root;
625 }
626 }
627 return root;
628}
629
630lldb::ValueObjectSP
631ValueObject::GetChildAtNamePath (const std::vector<ConstString> &names,
632 ConstString* name_of_error)
633{
634 if (names.size() == 0)
635 return GetSP();
636 ValueObjectSP root(GetSP());
637 for (ConstString name : names)
638 {
639 root = root->GetChildMemberWithName(name, true);
640 if (!root)
641 {
642 if (name_of_error)
643 *name_of_error = name;
644 return root;
645 }
646 }
647 return root;
648}
649
650lldb::ValueObjectSP
651ValueObject::GetChildAtNamePath (const std::initializer_list< std::pair<ConstString, bool> > &names,
652 ConstString* name_of_error)
653{
654 if (names.size() == 0)
655 return GetSP();
656 ValueObjectSP root(GetSP());
657 for (std::pair<ConstString, bool> name : names)
658 {
659 root = root->GetChildMemberWithName(name.first, name.second);
660 if (!root)
661 {
662 if (name_of_error)
663 *name_of_error = name.first;
664 return root;
665 }
666 }
667 return root;
668}
669
670lldb::ValueObjectSP
671ValueObject::GetChildAtNamePath (const std::vector< std::pair<ConstString, bool> > &names,
672 ConstString* name_of_error)
673{
674 if (names.size() == 0)
675 return GetSP();
676 ValueObjectSP root(GetSP());
677 for (std::pair<ConstString, bool> name : names)
678 {
679 root = root->GetChildMemberWithName(name.first, name.second);
680 if (!root)
681 {
682 if (name_of_error)
683 *name_of_error = name.first;
684 return root;
685 }
686 }
687 return root;
688}
689
Greg Claytonc7bece562013-01-25 18:06:21 +0000690size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691ValueObject::GetIndexOfChildWithName (const ConstString &name)
692{
693 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000694 return GetClangType().GetIndexOfChildWithName (name.GetCString(), omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695}
696
697ValueObjectSP
698ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
699{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000700 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000701 // classes (which really aren't part of the expression path), so we
702 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000704
Greg Claytondea8cb42011-06-29 22:09:02 +0000705 // We may need to update our value if we are dynamic
706 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000707 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000708
709 std::vector<uint32_t> child_indexes;
Greg Claytondea8cb42011-06-29 22:09:02 +0000710 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000711 const size_t num_child_indexes = GetClangType().GetIndexOfChildMemberWithName (name.GetCString(),
712 omit_empty_base_classes,
713 child_indexes);
Greg Claytondea8cb42011-06-29 22:09:02 +0000714 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000716 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
717 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
718
719 child_sp = GetChildAtIndex(*pos, can_create);
720 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000722 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000723 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000724 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
725 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000726 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000727 else
728 {
729 child_sp.reset();
730 }
731
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
733 }
734 return child_sp;
735}
736
737
Greg Claytonc7bece562013-01-25 18:06:21 +0000738size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739ValueObject::GetNumChildren ()
740{
Enrico Granatac5bc4122012-03-27 02:35:13 +0000741 UpdateValueIfNeeded();
Greg Clayton288bdf92010-09-02 02:59:18 +0000742 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 {
744 SetNumChildren (CalculateNumChildren());
745 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000746 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747}
Greg Clayton4a792072012-10-23 01:50:10 +0000748
749bool
750ValueObject::MightHaveChildren()
751{
Enrico Granatadb8142b2012-10-23 02:07:54 +0000752 bool has_children = false;
Greg Clayton2452ab72013-02-08 22:02:02 +0000753 const uint32_t type_info = GetTypeInfo();
754 if (type_info)
Greg Clayton4a792072012-10-23 01:50:10 +0000755 {
Enrico Granata622be232014-10-21 20:52:14 +0000756 if (type_info & (eTypeHasChildren |
757 eTypeIsPointer |
758 eTypeIsReference))
Greg Clayton4a792072012-10-23 01:50:10 +0000759 has_children = true;
760 }
761 else
762 {
763 has_children = GetNumChildren () > 0;
764 }
765 return has_children;
766}
767
768// Should only be called by ValueObject::GetNumChildren()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769void
Greg Claytonc7bece562013-01-25 18:06:21 +0000770ValueObject::SetNumChildren (size_t num_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771{
Greg Clayton288bdf92010-09-02 02:59:18 +0000772 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000773 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000774}
775
776void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777ValueObject::SetName (const ConstString &name)
778{
779 m_name = name;
780}
781
Jim Ingham58b59f92011-04-22 23:53:53 +0000782ValueObject *
Greg Claytonc7bece562013-01-25 18:06:21 +0000783ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784{
Jim Ingham2eec4872011-05-07 00:10:58 +0000785 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000786
Greg Claytondea8cb42011-06-29 22:09:02 +0000787 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000788 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000789 std::string child_name_str;
790 uint32_t child_byte_size = 0;
791 int32_t child_byte_offset = 0;
792 uint32_t child_bitfield_bit_size = 0;
793 uint32_t child_bitfield_bit_offset = 0;
794 bool child_is_base_class = false;
795 bool child_is_deref_of_parent = false;
796
797 const bool transparent_pointers = synthetic_array_member == false;
Greg Clayton57ee3062013-07-11 22:46:58 +0000798 ClangASTType child_clang_type;
Greg Claytondea8cb42011-06-29 22:09:02 +0000799
Greg Claytoncc4d0142012-02-17 07:49:44 +0000800 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000801
Greg Clayton57ee3062013-07-11 22:46:58 +0000802 child_clang_type = GetClangType().GetChildClangTypeAtIndex (&exe_ctx,
Greg Clayton57ee3062013-07-11 22:46:58 +0000803 idx,
804 transparent_pointers,
805 omit_empty_base_classes,
806 ignore_array_bounds,
807 child_name_str,
808 child_byte_size,
809 child_byte_offset,
810 child_bitfield_bit_size,
811 child_bitfield_bit_offset,
812 child_is_base_class,
Greg Clayton759e7442014-07-19 00:12:57 +0000813 child_is_deref_of_parent,
814 this);
Greg Clayton4ef877f2012-12-06 02:33:54 +0000815 if (child_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000816 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000817 if (synthetic_index)
818 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000819
Greg Claytondea8cb42011-06-29 22:09:02 +0000820 ConstString child_name;
821 if (!child_name_str.empty())
822 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000823
Greg Claytondea8cb42011-06-29 22:09:02 +0000824 valobj = new ValueObjectChild (*this,
Greg Claytondea8cb42011-06-29 22:09:02 +0000825 child_clang_type,
826 child_name,
827 child_byte_size,
828 child_byte_offset,
829 child_bitfield_bit_size,
830 child_bitfield_bit_offset,
831 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000832 child_is_deref_of_parent,
833 eAddressTypeInvalid);
834 //if (valobj)
835 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
836 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000837
Jim Ingham58b59f92011-04-22 23:53:53 +0000838 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839}
840
Enrico Granata0c489f52012-03-01 04:24:26 +0000841bool
842ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
843 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000844{
Enrico Granatac1247f52014-11-06 21:23:20 +0000845 return GetSummaryAsCString(summary_ptr, destination, TypeSummaryOptions());
846}
847
848bool
849ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
850 std::string& destination,
851 const TypeSummaryOptions& options)
852{
Enrico Granata0c489f52012-03-01 04:24:26 +0000853 destination.clear();
854
855 // ideally we would like to bail out if passing NULL, but if we do so
856 // we end up not providing the summary for function pointers anymore
857 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
858 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000859
860 m_is_getting_summary = true;
Enrico Granataf18c03e2012-04-04 17:34:10 +0000861
862 // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other
863 // information that we might care to see in a crash log. might be useful in very specific situations though.
864 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s",
865 GetTypeName().GetCString(),
866 GetName().GetCString(),
867 summary_ptr->GetDescription().c_str());*/
868
Enrico Granata0c489f52012-03-01 04:24:26 +0000869 if (UpdateValueIfNeeded (false))
870 {
871 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000873 if (HasSyntheticValue())
874 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
875 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000876 }
877 else
878 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000879 ClangASTType clang_type = GetClangType();
Enrico Granata0c489f52012-03-01 04:24:26 +0000880
881 // Do some default printout for function pointers
882 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000883 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000884 if (clang_type.IsFunctionPointerType ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000886 StreamString sstr;
Enrico Granata0c489f52012-03-01 04:24:26 +0000887 AddressType func_ptr_address_type = eAddressTypeInvalid;
888 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
889 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000890 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000891 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000892 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000893 case eAddressTypeInvalid:
894 case eAddressTypeFile:
895 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000896
Greg Claytoncc4d0142012-02-17 07:49:44 +0000897 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000898 {
899 ExecutionContext exe_ctx (GetExecutionContextRef());
900
901 Address so_addr;
902 Target *target = exe_ctx.GetTargetPtr();
903 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000904 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000905 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000906 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000907 so_addr.Dump (&sstr,
908 exe_ctx.GetBestExecutionContextScope(),
909 Address::DumpStyleResolvedDescription,
910 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000911 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000912 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000913 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000914 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000915
Greg Claytoncc4d0142012-02-17 07:49:44 +0000916 case eAddressTypeHost:
917 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000918 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000919 }
920 if (sstr.GetSize() > 0)
921 {
922 destination.assign (1, '(');
923 destination.append (sstr.GetData(), sstr.GetSize());
924 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925 }
926 }
927 }
928 }
929 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000930 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000931 return !destination.empty();
932}
933
934const char *
935ValueObject::GetSummaryAsCString ()
936{
Enrico Granatac1247f52014-11-06 21:23:20 +0000937 return GetSummaryAsCString(TypeSummaryOptions());
938}
939
940const char *
941ValueObject::GetSummaryAsCString (const TypeSummaryOptions& options)
942{
Enrico Granata0c489f52012-03-01 04:24:26 +0000943 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
944 {
945 GetSummaryAsCString(GetSummaryFormat().get(),
Enrico Granatac1247f52014-11-06 21:23:20 +0000946 m_summary_str,
947 options);
Enrico Granata0c489f52012-03-01 04:24:26 +0000948 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 if (m_summary_str.empty())
950 return NULL;
951 return m_summary_str.c_str();
952}
953
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000954bool
955ValueObject::IsCStringContainer(bool check_pointer)
956{
Greg Clayton57ee3062013-07-11 22:46:58 +0000957 ClangASTType pointee_or_element_clang_type;
958 const Flags type_flags (GetTypeInfo (&pointee_or_element_clang_type));
Enrico Granata622be232014-10-21 20:52:14 +0000959 bool is_char_arr_ptr (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
Greg Clayton57ee3062013-07-11 22:46:58 +0000960 pointee_or_element_clang_type.IsCharType ());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000961 if (!is_char_arr_ptr)
962 return false;
963 if (!check_pointer)
964 return true;
Enrico Granata622be232014-10-21 20:52:14 +0000965 if (type_flags.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000966 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000967 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000968 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000969 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000970 return (cstr_address != LLDB_INVALID_ADDRESS);
971}
972
Enrico Granata9128ee22011-09-06 19:20:51 +0000973size_t
974ValueObject::GetPointeeData (DataExtractor& data,
975 uint32_t item_idx,
976 uint32_t item_count)
977{
Greg Clayton57ee3062013-07-11 22:46:58 +0000978 ClangASTType pointee_or_element_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000979 const uint32_t type_info = GetTypeInfo (&pointee_or_element_clang_type);
Enrico Granata622be232014-10-21 20:52:14 +0000980 const bool is_pointer_type = type_info & eTypeIsPointer;
981 const bool is_array_type = type_info & eTypeIsArray;
Greg Clayton2452ab72013-02-08 22:02:02 +0000982 if (!(is_pointer_type || is_array_type))
Enrico Granata9128ee22011-09-06 19:20:51 +0000983 return 0;
984
985 if (item_count == 0)
986 return 0;
987
Greg Clayton57ee3062013-07-11 22:46:58 +0000988 const uint64_t item_type_size = pointee_or_element_clang_type.GetByteSize();
Enrico Granata9128ee22011-09-06 19:20:51 +0000989 const uint64_t bytes = item_count * item_type_size;
Enrico Granata9128ee22011-09-06 19:20:51 +0000990 const uint64_t offset = item_idx * item_type_size;
991
992 if (item_idx == 0 && item_count == 1) // simply a deref
993 {
Greg Clayton2452ab72013-02-08 22:02:02 +0000994 if (is_pointer_type)
Enrico Granata9128ee22011-09-06 19:20:51 +0000995 {
996 Error error;
997 ValueObjectSP pointee_sp = Dereference(error);
998 if (error.Fail() || pointee_sp.get() == NULL)
999 return 0;
Sean Callanan866e91c2014-02-28 22:27:53 +00001000 return pointee_sp->GetData(data, error);
Enrico Granata9128ee22011-09-06 19:20:51 +00001001 }
1002 else
1003 {
1004 ValueObjectSP child_sp = GetChildAtIndex(0, true);
1005 if (child_sp.get() == NULL)
1006 return 0;
Sean Callanan866e91c2014-02-28 22:27:53 +00001007 Error error;
1008 return child_sp->GetData(data, error);
Enrico Granata9128ee22011-09-06 19:20:51 +00001009 }
1010 return true;
1011 }
1012 else /* (items > 1) */
1013 {
1014 Error error;
1015 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
1016 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
1017
1018 AddressType addr_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001019 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
Enrico Granata9128ee22011-09-06 19:20:51 +00001020
Enrico Granata9128ee22011-09-06 19:20:51 +00001021 switch (addr_type)
1022 {
1023 case eAddressTypeFile:
1024 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001025 ModuleSP module_sp (GetModule());
1026 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +00001027 {
Enrico Granata9c2efe32012-08-07 01:49:34 +00001028 addr = addr + offset;
Enrico Granata9128ee22011-09-06 19:20:51 +00001029 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +00001030 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001031 ExecutionContext exe_ctx (GetExecutionContextRef());
1032 Target* target = exe_ctx.GetTargetPtr();
1033 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +00001034 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001035 heap_buf_ptr->SetByteSize(bytes);
1036 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
1037 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +00001038 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001039 data.SetData(data_sp);
1040 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001041 }
1042 }
1043 }
1044 }
1045 break;
1046 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +00001047 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001048 ExecutionContext exe_ctx (GetExecutionContextRef());
1049 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +00001050 if (process)
1051 {
1052 heap_buf_ptr->SetByteSize(bytes);
1053 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
Enrico Granata5e1480c2013-10-30 17:52:44 +00001054 if (error.Success() || bytes_read > 0)
Enrico Granata9128ee22011-09-06 19:20:51 +00001055 {
1056 data.SetData(data_sp);
1057 return bytes_read;
1058 }
1059 }
1060 }
1061 break;
1062 case eAddressTypeHost:
1063 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001064 const uint64_t max_bytes = GetClangType().GetByteSize();
Greg Clayton2452ab72013-02-08 22:02:02 +00001065 if (max_bytes > offset)
1066 {
1067 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
1068 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
1069 data.SetData(data_sp);
1070 return bytes_read;
1071 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001072 }
1073 break;
1074 case eAddressTypeInvalid:
Enrico Granata9128ee22011-09-06 19:20:51 +00001075 break;
1076 }
1077 }
1078 return 0;
1079}
1080
Greg Claytonfaac1112013-03-14 18:31:44 +00001081uint64_t
Sean Callanan866e91c2014-02-28 22:27:53 +00001082ValueObject::GetData (DataExtractor& data, Error &error)
Enrico Granata9128ee22011-09-06 19:20:51 +00001083{
1084 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001085 ExecutionContext exe_ctx (GetExecutionContextRef());
Sean Callanan866e91c2014-02-28 22:27:53 +00001086 error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +00001087 if (error.Fail())
Sean Callananed185ab2013-04-19 19:47:32 +00001088 {
1089 if (m_data.GetByteSize())
1090 {
1091 data = m_data;
1092 return data.GetByteSize();
1093 }
1094 else
1095 {
1096 return 0;
1097 }
1098 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001099 data.SetAddressByteSize(m_data.GetAddressByteSize());
1100 data.SetByteOrder(m_data.GetByteOrder());
1101 return data.GetByteSize();
1102}
1103
Sean Callanan389823e2013-04-13 01:21:23 +00001104bool
1105ValueObject::SetData (DataExtractor &data, Error &error)
1106{
1107 error.Clear();
1108 // Make sure our value is up to date first so that our location and location
1109 // type is valid.
1110 if (!UpdateValueIfNeeded(false))
1111 {
1112 error.SetErrorString("unable to read value");
1113 return false;
1114 }
1115
1116 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001117 const Encoding encoding = GetClangType().GetEncoding(count);
Sean Callanan389823e2013-04-13 01:21:23 +00001118
1119 const size_t byte_size = GetByteSize();
1120
1121 Value::ValueType value_type = m_value.GetValueType();
1122
1123 switch (value_type)
1124 {
1125 case Value::eValueTypeScalar:
1126 {
1127 Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1128
1129 if (!set_error.Success())
1130 {
1131 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1132 return false;
1133 }
1134 }
1135 break;
1136 case Value::eValueTypeLoadAddress:
1137 {
1138 // If it is a load address, then the scalar value is the storage location
1139 // of the data, and we have to shove this value down to that load location.
1140 ExecutionContext exe_ctx (GetExecutionContextRef());
1141 Process *process = exe_ctx.GetProcessPtr();
1142 if (process)
1143 {
1144 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1145 size_t bytes_written = process->WriteMemory(target_addr,
1146 data.GetDataStart(),
1147 byte_size,
1148 error);
1149 if (!error.Success())
1150 return false;
1151 if (bytes_written != byte_size)
1152 {
1153 error.SetErrorString("unable to write value to memory");
1154 return false;
1155 }
1156 }
1157 }
1158 break;
1159 case Value::eValueTypeHostAddress:
1160 {
1161 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1162 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1163 m_data.SetData(buffer_sp, 0);
1164 data.CopyByteOrderedData (0,
1165 byte_size,
1166 const_cast<uint8_t *>(m_data.GetDataStart()),
1167 byte_size,
1168 m_data.GetByteOrder());
1169 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1170 }
1171 break;
1172 case Value::eValueTypeFileAddress:
1173 case Value::eValueTypeVector:
1174 break;
1175 }
1176
1177 // If we have reached this point, then we have successfully changed the value.
1178 SetNeedsUpdate();
1179 return true;
1180}
1181
Enrico Granata9128ee22011-09-06 19:20:51 +00001182// will compute strlen(str), but without consuming more than
1183// maxlen bytes out of str (this serves the purpose of reading
1184// chunks of a string without having to worry about
1185// missing NULL terminators in the chunk)
1186// of course, if strlen(str) > maxlen, the function will return
1187// maxlen_value (which should be != maxlen, because that allows you
1188// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1189static uint32_t
1190strlen_or_inf (const char* str,
1191 uint32_t maxlen,
1192 uint32_t maxlen_value)
1193{
1194 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +00001195 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +00001196 {
Greg Clayton8dd5c172011-10-05 22:19:51 +00001197 while(*str)
1198 {
1199 len++;str++;
Greg Clayton2452ab72013-02-08 22:02:02 +00001200 if (len >= maxlen)
Greg Clayton8dd5c172011-10-05 22:19:51 +00001201 return maxlen_value;
1202 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001203 }
1204 return len;
1205}
1206
Enrico Granata2206b482014-10-30 18:27:31 +00001207static bool
1208CopyStringDataToBufferSP(const StreamString& source,
1209 lldb::DataBufferSP& destination)
1210{
1211 destination.reset(new DataBufferHeap(source.GetSize()+1,0));
1212 memcpy(destination->GetBytes(), source.GetString().c_str(), source.GetSize());
1213 return true;
1214}
1215
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001216size_t
Enrico Granata2206b482014-10-30 18:27:31 +00001217ValueObject::ReadPointedString (lldb::DataBufferSP& buffer_sp,
Greg Claytoncc4d0142012-02-17 07:49:44 +00001218 Error& error,
1219 uint32_t max_length,
1220 bool honor_array,
1221 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001222{
Enrico Granata2206b482014-10-30 18:27:31 +00001223 StreamString s;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001224 ExecutionContext exe_ctx (GetExecutionContextRef());
1225 Target* target = exe_ctx.GetTargetPtr();
Enrico Granata2206b482014-10-30 18:27:31 +00001226
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001227 if (!target)
1228 {
1229 s << "<no target to read from>";
1230 error.SetErrorString("no target to read from");
Enrico Granata2206b482014-10-30 18:27:31 +00001231 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001232 return 0;
1233 }
1234
1235 if (max_length == 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001236 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001237
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001238 size_t bytes_read = 0;
1239 size_t total_bytes_read = 0;
1240
Greg Clayton57ee3062013-07-11 22:46:58 +00001241 ClangASTType clang_type = GetClangType();
1242 ClangASTType elem_or_pointee_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001243 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
Enrico Granata622be232014-10-21 20:52:14 +00001244 if (type_flags.AnySet (eTypeIsArray | eTypeIsPointer) &&
Greg Clayton57ee3062013-07-11 22:46:58 +00001245 elem_or_pointee_clang_type.IsCharType ())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001246 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001247 addr_t cstr_address = LLDB_INVALID_ADDRESS;
1248 AddressType cstr_address_type = eAddressTypeInvalid;
1249
1250 size_t cstr_len = 0;
1251 bool capped_data = false;
Enrico Granata622be232014-10-21 20:52:14 +00001252 if (type_flags.Test (eTypeIsArray))
Greg Claytoncc4d0142012-02-17 07:49:44 +00001253 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001254 // We have an array
Greg Clayton57ee3062013-07-11 22:46:58 +00001255 uint64_t array_size = 0;
1256 if (clang_type.IsArrayType(NULL, &array_size, NULL))
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001257 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001258 cstr_len = array_size;
1259 if (cstr_len > max_length)
1260 {
1261 capped_data = true;
1262 cstr_len = max_length;
1263 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001264 }
1265 cstr_address = GetAddressOf (true, &cstr_address_type);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001266 }
1267 else
1268 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001269 // We have a pointer
1270 cstr_address = GetPointerValue (&cstr_address_type);
1271 }
1272
1273 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1274 {
1275 s << "<invalid address>";
1276 error.SetErrorString("invalid address");
Enrico Granata2206b482014-10-30 18:27:31 +00001277 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001278 return 0;
1279 }
Enrico Granata2206b482014-10-30 18:27:31 +00001280
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001281 Address cstr_so_addr (cstr_address);
1282 DataExtractor data;
1283 if (cstr_len > 0 && honor_array)
1284 {
1285 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1286 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1287 GetPointeeData(data, 0, cstr_len);
Enrico Granata2206b482014-10-30 18:27:31 +00001288
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001289 if ((bytes_read = data.GetByteSize()) > 0)
1290 {
1291 total_bytes_read = bytes_read;
Enrico Granata2206b482014-10-30 18:27:31 +00001292 for (size_t offset = 0; offset < bytes_read; offset++)
1293 s.Printf("%c", *data.PeekData(offset, 1));
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001294 if (capped_data)
1295 s << "...";
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001296 }
1297 }
1298 else
1299 {
1300 cstr_len = max_length;
1301 const size_t k_max_buf_size = 64;
Enrico Granata2206b482014-10-30 18:27:31 +00001302
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001303 size_t offset = 0;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001304
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001305 int cstr_len_displayed = -1;
1306 bool capped_cstr = false;
1307 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1308 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1309 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001310 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001311 total_bytes_read += bytes_read;
1312 const char *cstr = data.PeekCStr(0);
1313 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1314 if (len > k_max_buf_size)
1315 len = k_max_buf_size;
Enrico Granata2206b482014-10-30 18:27:31 +00001316
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001317 if (cstr_len_displayed < 0)
1318 cstr_len_displayed = len;
Enrico Granata2206b482014-10-30 18:27:31 +00001319
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001320 if (len == 0)
1321 break;
1322 cstr_len_displayed += len;
1323 if (len > bytes_read)
1324 len = bytes_read;
1325 if (len > cstr_len)
1326 len = cstr_len;
1327
Enrico Granata2206b482014-10-30 18:27:31 +00001328 for (size_t offset = 0; offset < bytes_read; offset++)
1329 s.Printf("%c", *data.PeekData(offset, 1));
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001330
1331 if (len < k_max_buf_size)
1332 break;
1333
1334 if (len >= cstr_len)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001335 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001336 capped_cstr = true;
1337 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001338 }
Enrico Granata2206b482014-10-30 18:27:31 +00001339
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001340 cstr_len -= len;
1341 offset += len;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001342 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001343
1344 if (cstr_len_displayed >= 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001345 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001346 if (capped_cstr)
1347 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001348 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001349 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001350 }
1351 else
1352 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001353 error.SetErrorString("not a string object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001354 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001355 }
Enrico Granata2206b482014-10-30 18:27:31 +00001356 CopyStringDataToBufferSP(s, buffer_sp);
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001357 return total_bytes_read;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001358}
1359
Enrico Granata744794a2014-09-05 21:46:22 +00001360std::pair<TypeValidatorResult, std::string>
1361ValueObject::GetValidationStatus ()
1362{
1363 if (!UpdateValueIfNeeded(true))
1364 return {TypeValidatorResult::Success,""}; // not the validator's job to discuss update problems
1365
1366 if (m_validation_result.hasValue())
1367 return m_validation_result.getValue();
1368
1369 if (!m_type_validator_sp)
1370 return {TypeValidatorResult::Success,""}; // no validator no failure
1371
1372 auto outcome = m_type_validator_sp->FormatObject(this);
1373
1374 return (m_validation_result = {outcome.m_result,outcome.m_message}).getValue();
1375}
1376
Jim Ingham53c47f12010-09-10 23:12:17 +00001377const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001378ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001379{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001380
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001381 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001382 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001383
1384 if (!m_object_desc_str.empty())
1385 return m_object_desc_str.c_str();
1386
Greg Claytoncc4d0142012-02-17 07:49:44 +00001387 ExecutionContext exe_ctx (GetExecutionContextRef());
1388 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001389 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001390 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001391
Jim Ingham53c47f12010-09-10 23:12:17 +00001392 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001393
Greg Claytonafacd142011-09-02 01:15:17 +00001394 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001395 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1396
Jim Inghama2cf2632010-12-23 02:29:54 +00001397 if (runtime == NULL)
1398 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001399 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Greg Clayton57ee3062013-07-11 22:46:58 +00001400 ClangASTType clang_type = GetClangType();
1401 if (clang_type)
Jim Inghama2cf2632010-12-23 02:29:54 +00001402 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001403 bool is_signed;
Greg Clayton57ee3062013-07-11 22:46:58 +00001404 if (clang_type.IsIntegerType (is_signed) || clang_type.IsPointerType ())
Jim Inghamb7603bb2011-03-18 00:05:18 +00001405 {
Greg Claytonafacd142011-09-02 01:15:17 +00001406 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001407 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001408 }
1409 }
1410
Jim Ingham8d543de2011-03-31 23:01:21 +00001411 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001412 {
1413 m_object_desc_str.append (s.GetData());
1414 }
Sean Callanan672ad942010-10-23 00:18:49 +00001415
1416 if (m_object_desc_str.empty())
1417 return NULL;
1418 else
1419 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001420}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421
Enrico Granata0c489f52012-03-01 04:24:26 +00001422bool
Enrico Granata4939b982013-12-22 09:24:22 +00001423ValueObject::GetValueAsCString (const lldb_private::TypeFormatImpl& format,
1424 std::string& destination)
1425{
1426 if (UpdateValueIfNeeded(false))
1427 return format.FormatObject(this,destination);
1428 else
1429 return false;
1430}
1431
1432bool
Enrico Granata0c489f52012-03-01 04:24:26 +00001433ValueObject::GetValueAsCString (lldb::Format format,
1434 std::string& destination)
1435{
Enrico Granata30f287f2013-12-28 08:44:02 +00001436 return GetValueAsCString(TypeFormatImpl_Format(format),destination);
Enrico Granata0c489f52012-03-01 04:24:26 +00001437}
1438
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001439const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001440ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001441{
Enrico Granatab294fd22013-05-31 19:18:19 +00001442 if (UpdateValueIfNeeded(true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443 {
Enrico Granata4939b982013-12-22 09:24:22 +00001444 lldb::TypeFormatImplSP format_sp;
Enrico Granata0c489f52012-03-01 04:24:26 +00001445 lldb::Format my_format = GetFormat();
Enrico Granatac953a6a2012-12-11 02:17:22 +00001446 if (my_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001448 if (m_type_format_sp)
Enrico Granata4939b982013-12-22 09:24:22 +00001449 format_sp = m_type_format_sp;
Enrico Granata0c489f52012-03-01 04:24:26 +00001450 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001451 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001452 if (m_is_bitfield_for_scalar)
1453 my_format = eFormatUnsigned;
1454 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001455 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001456 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001457 {
1458 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1459 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001460 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001461 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001462 else
1463 {
Enrico Granatad07cfd32014-10-08 18:27:36 +00001464 my_format = GetValue().GetClangType().GetFormat();
Enrico Granata0c489f52012-03-01 04:24:26 +00001465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001466 }
1467 }
1468 }
Enrico Granatab294fd22013-05-31 19:18:19 +00001469 if (my_format != m_last_format || m_value_str.empty())
Enrico Granata297e69f2012-03-06 23:21:16 +00001470 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001471 m_last_format = my_format;
Enrico Granata4939b982013-12-22 09:24:22 +00001472 if (!format_sp)
Enrico Granata30f287f2013-12-28 08:44:02 +00001473 format_sp.reset(new TypeFormatImpl_Format(my_format));
Enrico Granata4939b982013-12-22 09:24:22 +00001474 if (GetValueAsCString(*format_sp.get(), m_value_str))
Enrico Granata297e69f2012-03-06 23:21:16 +00001475 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001476 if (!m_value_did_change && m_old_value_valid)
1477 {
1478 // The value was gotten successfully, so we consider the
1479 // value as changed if the value string differs
1480 SetValueDidChange (m_old_value_str != m_value_str);
1481 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001482 }
1483 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001484 }
1485 if (m_value_str.empty())
1486 return NULL;
1487 return m_value_str.c_str();
1488}
1489
Enrico Granatac3e320a2011-08-02 17:27:39 +00001490// if > 8bytes, 0 is returned. this method should mostly be used
1491// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001492uint64_t
Johnny Chen3f476c42012-06-05 19:37:43 +00001493ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001494{
1495 // If our byte size is zero this is an aggregate type that has children
Enrico Granatad07cfd32014-10-08 18:27:36 +00001496 if (CanProvideValue())
Enrico Granatac3e320a2011-08-02 17:27:39 +00001497 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001498 Scalar scalar;
1499 if (ResolveValue (scalar))
Johnny Chen3f476c42012-06-05 19:37:43 +00001500 {
1501 if (success)
1502 *success = true;
Enrico Granata48ea80f2012-10-24 20:24:39 +00001503 return scalar.ULongLong(fail_value);
Johnny Chen3f476c42012-06-05 19:37:43 +00001504 }
1505 // fallthrough, otherwise...
Enrico Granatac3e320a2011-08-02 17:27:39 +00001506 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001507
1508 if (success)
1509 *success = false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001510 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001511}
1512
Enrico Granatad7373f62013-10-31 18:57:50 +00001513int64_t
1514ValueObject::GetValueAsSigned (int64_t fail_value, bool *success)
1515{
1516 // If our byte size is zero this is an aggregate type that has children
Enrico Granatad07cfd32014-10-08 18:27:36 +00001517 if (CanProvideValue())
Enrico Granatad7373f62013-10-31 18:57:50 +00001518 {
1519 Scalar scalar;
1520 if (ResolveValue (scalar))
1521 {
1522 if (success)
1523 *success = true;
1524 return scalar.SLongLong(fail_value);
1525 }
1526 // fallthrough, otherwise...
1527 }
1528
1529 if (success)
1530 *success = false;
1531 return fail_value;
1532}
1533
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001534// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1535// this call up to date by returning true for your new special cases. We will eventually move
1536// to checking this call result before trying to display special cases
1537bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001538ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1539 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001540{
Greg Clayton57ee3062013-07-11 22:46:58 +00001541 Flags flags(GetTypeInfo());
Enrico Granata622be232014-10-21 20:52:14 +00001542 if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001543 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001544 {
1545 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001546 (custom_format == eFormatCString ||
1547 custom_format == eFormatCharArray ||
1548 custom_format == eFormatChar ||
1549 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001550 return true;
1551
Enrico Granata622be232014-10-21 20:52:14 +00001552 if (flags.Test(eTypeIsArray))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001553 {
Greg Claytonafacd142011-09-02 01:15:17 +00001554 if ((custom_format == eFormatBytes) ||
1555 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001556 return true;
1557
Greg Claytonafacd142011-09-02 01:15:17 +00001558 if ((custom_format == eFormatVectorOfChar) ||
1559 (custom_format == eFormatVectorOfFloat32) ||
1560 (custom_format == eFormatVectorOfFloat64) ||
1561 (custom_format == eFormatVectorOfSInt16) ||
1562 (custom_format == eFormatVectorOfSInt32) ||
1563 (custom_format == eFormatVectorOfSInt64) ||
1564 (custom_format == eFormatVectorOfSInt8) ||
1565 (custom_format == eFormatVectorOfUInt128) ||
1566 (custom_format == eFormatVectorOfUInt16) ||
1567 (custom_format == eFormatVectorOfUInt32) ||
1568 (custom_format == eFormatVectorOfUInt64) ||
1569 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001570 return true;
1571 }
1572 }
1573 return false;
1574}
1575
Enrico Granata9fc19442011-07-06 02:13:41 +00001576bool
1577ValueObject::DumpPrintableRepresentation(Stream& s,
1578 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001579 Format custom_format,
Enrico Granata0dba9b32014-01-08 01:36:59 +00001580 PrintableRepresentationSpecialCases special,
1581 bool do_dump_error)
Enrico Granata9fc19442011-07-06 02:13:41 +00001582{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001583
Greg Clayton57ee3062013-07-11 22:46:58 +00001584 Flags flags(GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001585
Enrico Granata86cc9822012-03-19 22:58:49 +00001586 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1587 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1588
1589 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001590 {
Enrico Granata622be232014-10-21 20:52:14 +00001591 if (flags.AnySet(eTypeIsArray | eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001592 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001593 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001594 // when being asked to get a printable display an array or pointer type directly,
1595 // try to "do the right thing"
1596
1597 if (IsCStringContainer(true) &&
1598 (custom_format == eFormatCString ||
1599 custom_format == eFormatCharArray ||
1600 custom_format == eFormatChar ||
1601 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001602 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001603 Error error;
Enrico Granata2206b482014-10-30 18:27:31 +00001604 lldb::DataBufferSP buffer_sp;
1605 ReadPointedString(buffer_sp,
Enrico Granata86cc9822012-03-19 22:58:49 +00001606 error,
1607 0,
1608 (custom_format == eFormatVectorOfChar) ||
1609 (custom_format == eFormatCharArray));
Enrico Granataebdc1ac2014-11-05 21:20:48 +00001610 lldb_private::formatters::ReadBufferAndDumpToStreamOptions options(*this);
Enrico Granata2206b482014-10-30 18:27:31 +00001611 options.SetData(DataExtractor(buffer_sp, lldb::eByteOrderInvalid, 8)); // none of this matters for a string - pass some defaults
1612 options.SetStream(&s);
1613 options.SetPrefixToken(0);
1614 options.SetQuote('"');
1615 options.SetSourceSize(buffer_sp->GetByteSize());
Enrico Granata2206b482014-10-30 18:27:31 +00001616 lldb_private::formatters::ReadBufferAndDumpToStream<lldb_private::formatters::StringElementType::ASCII>(options);
Enrico Granata86cc9822012-03-19 22:58:49 +00001617 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001618 }
1619
Enrico Granata86cc9822012-03-19 22:58:49 +00001620 if (custom_format == eFormatEnum)
1621 return false;
1622
1623 // this only works for arrays, because I have no way to know when
1624 // the pointed memory ends, and no special \0 end of data marker
Enrico Granata622be232014-10-21 20:52:14 +00001625 if (flags.Test(eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001626 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001627 if ((custom_format == eFormatBytes) ||
1628 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001629 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001630 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001631
1632 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001633 for (size_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001634 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001635
1636 if (low)
1637 s << ',';
1638
1639 ValueObjectSP child = GetChildAtIndex(low,true);
1640 if (!child.get())
1641 {
1642 s << "<invalid child>";
1643 continue;
1644 }
1645 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1646 }
1647
1648 s << ']';
1649
1650 return true;
1651 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001652
Enrico Granata86cc9822012-03-19 22:58:49 +00001653 if ((custom_format == eFormatVectorOfChar) ||
1654 (custom_format == eFormatVectorOfFloat32) ||
1655 (custom_format == eFormatVectorOfFloat64) ||
1656 (custom_format == eFormatVectorOfSInt16) ||
1657 (custom_format == eFormatVectorOfSInt32) ||
1658 (custom_format == eFormatVectorOfSInt64) ||
1659 (custom_format == eFormatVectorOfSInt8) ||
1660 (custom_format == eFormatVectorOfUInt128) ||
1661 (custom_format == eFormatVectorOfUInt16) ||
1662 (custom_format == eFormatVectorOfUInt32) ||
1663 (custom_format == eFormatVectorOfUInt64) ||
1664 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1665 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001666 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001667
1668 Format format = FormatManager::GetSingleItemFormat(custom_format);
1669
1670 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001671 for (size_t low = 0; low < count; low++)
Enrico Granata86cc9822012-03-19 22:58:49 +00001672 {
1673
1674 if (low)
1675 s << ',';
1676
1677 ValueObjectSP child = GetChildAtIndex(low,true);
1678 if (!child.get())
1679 {
1680 s << "<invalid child>";
1681 continue;
1682 }
1683 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1684 }
1685
1686 s << ']';
1687
1688 return true;
1689 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001690 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001691
1692 if ((custom_format == eFormatBoolean) ||
1693 (custom_format == eFormatBinary) ||
1694 (custom_format == eFormatChar) ||
1695 (custom_format == eFormatCharPrintable) ||
1696 (custom_format == eFormatComplexFloat) ||
1697 (custom_format == eFormatDecimal) ||
1698 (custom_format == eFormatHex) ||
Enrico Granata7ec18e32012-08-09 19:33:34 +00001699 (custom_format == eFormatHexUppercase) ||
Enrico Granata86cc9822012-03-19 22:58:49 +00001700 (custom_format == eFormatFloat) ||
1701 (custom_format == eFormatOctal) ||
1702 (custom_format == eFormatOSType) ||
1703 (custom_format == eFormatUnicode16) ||
1704 (custom_format == eFormatUnicode32) ||
1705 (custom_format == eFormatUnsigned) ||
1706 (custom_format == eFormatPointer) ||
1707 (custom_format == eFormatComplexInteger) ||
1708 (custom_format == eFormatComplex) ||
1709 (custom_format == eFormatDefault)) // use the [] operator
1710 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001711 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001712 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001713
1714 if (only_special)
1715 return false;
1716
Enrico Granata86cc9822012-03-19 22:58:49 +00001717 bool var_success = false;
1718
1719 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001720 const char *cstr = NULL;
Enrico Granata2c75f112013-06-21 00:04:51 +00001721
1722 // this is a local stream that we are using to ensure that the data pointed to by cstr survives
1723 // long enough for us to copy it to its destination - it is necessary to have this temporary storage
1724 // area for cases where our desired output is not backed by some other longer-term storage
Greg Claytonc7bece562013-01-25 18:06:21 +00001725 StreamString strm;
Enrico Granata86cc9822012-03-19 22:58:49 +00001726
Enrico Granata465f4bc2014-02-15 01:24:44 +00001727 if (custom_format != eFormatInvalid)
Enrico Granata86cc9822012-03-19 22:58:49 +00001728 SetFormat(custom_format);
1729
1730 switch(val_obj_display)
1731 {
1732 case eValueObjectRepresentationStyleValue:
Greg Claytonc7bece562013-01-25 18:06:21 +00001733 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001734 break;
1735
1736 case eValueObjectRepresentationStyleSummary:
Greg Claytonc7bece562013-01-25 18:06:21 +00001737 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001738 break;
1739
1740 case eValueObjectRepresentationStyleLanguageSpecific:
Greg Claytonc7bece562013-01-25 18:06:21 +00001741 cstr = GetObjectDescription();
Enrico Granata86cc9822012-03-19 22:58:49 +00001742 break;
1743
1744 case eValueObjectRepresentationStyleLocation:
Greg Claytonc7bece562013-01-25 18:06:21 +00001745 cstr = GetLocationAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001746 break;
1747
1748 case eValueObjectRepresentationStyleChildrenCount:
Deepak Panickal99fbc072014-03-03 15:39:47 +00001749 strm.Printf("%" PRIu64 "", (uint64_t)GetNumChildren());
Greg Claytonc7bece562013-01-25 18:06:21 +00001750 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001751 break;
1752
1753 case eValueObjectRepresentationStyleType:
Greg Claytonc7bece562013-01-25 18:06:21 +00001754 cstr = GetTypeName().AsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001755 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001756
1757 case eValueObjectRepresentationStyleName:
1758 cstr = GetName().AsCString();
1759 break;
1760
1761 case eValueObjectRepresentationStyleExpressionPath:
1762 GetExpressionPath(strm, false);
1763 cstr = strm.GetString().c_str();
1764 break;
Enrico Granata86cc9822012-03-19 22:58:49 +00001765 }
1766
Greg Claytonc7bece562013-01-25 18:06:21 +00001767 if (!cstr)
Enrico Granata86cc9822012-03-19 22:58:49 +00001768 {
1769 if (val_obj_display == eValueObjectRepresentationStyleValue)
Greg Claytonc7bece562013-01-25 18:06:21 +00001770 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001771 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1772 {
Enrico Granatad07cfd32014-10-08 18:27:36 +00001773 if (!CanProvideValue())
Enrico Granata86cc9822012-03-19 22:58:49 +00001774 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001775 strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1776 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001777 }
1778 else
Greg Claytonc7bece562013-01-25 18:06:21 +00001779 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001780 }
1781 }
1782
Greg Claytonc7bece562013-01-25 18:06:21 +00001783 if (cstr)
1784 s.PutCString(cstr);
Enrico Granata86cc9822012-03-19 22:58:49 +00001785 else
1786 {
1787 if (m_error.Fail())
Enrico Granata0dba9b32014-01-08 01:36:59 +00001788 {
1789 if (do_dump_error)
1790 s.Printf("<%s>", m_error.AsCString());
1791 else
1792 return false;
1793 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001794 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1795 s.PutCString("<no summary available>");
1796 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1797 s.PutCString("<no value available>");
1798 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1799 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1800 else
1801 s.PutCString("<no printable representation>");
1802 }
1803
1804 // we should only return false here if we could not do *anything*
1805 // even if we have an error message as output, that's a success
1806 // from our callers' perspective, so return true
1807 var_success = true;
Enrico Granata465f4bc2014-02-15 01:24:44 +00001808
1809 if (custom_format != eFormatInvalid)
1810 SetFormat(eFormatDefault);
Enrico Granata86cc9822012-03-19 22:58:49 +00001811 }
1812
Enrico Granataf4efecd2011-07-12 22:56:10 +00001813 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001814}
1815
Greg Clayton737b9322010-09-13 03:32:57 +00001816addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001817ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001818{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001819 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001820 return LLDB_INVALID_ADDRESS;
1821
Greg Clayton73b472d2010-10-27 03:32:59 +00001822 switch (m_value.GetValueType())
1823 {
1824 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001825 case Value::eValueTypeVector:
Greg Clayton73b472d2010-10-27 03:32:59 +00001826 if (scalar_is_load_address)
1827 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001828 if(address_type)
1829 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001830 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1831 }
1832 break;
1833
1834 case Value::eValueTypeLoadAddress:
1835 case Value::eValueTypeFileAddress:
1836 case Value::eValueTypeHostAddress:
1837 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001838 if(address_type)
1839 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001840 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1841 }
1842 break;
1843 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001844 if (address_type)
1845 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001846 return LLDB_INVALID_ADDRESS;
1847}
1848
1849addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001850ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001851{
Greg Claytonafacd142011-09-02 01:15:17 +00001852 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001853 if(address_type)
1854 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001855
Enrico Granatac3e320a2011-08-02 17:27:39 +00001856 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001857 return address;
1858
Greg Clayton73b472d2010-10-27 03:32:59 +00001859 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001860 {
1861 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001862 case Value::eValueTypeVector:
Enrico Granata9128ee22011-09-06 19:20:51 +00001863 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001864 break;
1865
Enrico Granata9128ee22011-09-06 19:20:51 +00001866 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001867 case Value::eValueTypeLoadAddress:
1868 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001869 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001870 lldb::offset_t data_offset = 0;
Greg Clayton737b9322010-09-13 03:32:57 +00001871 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001872 }
1873 break;
1874 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001875
Enrico Granata9128ee22011-09-06 19:20:51 +00001876 if (address_type)
1877 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001878
Greg Clayton737b9322010-09-13 03:32:57 +00001879 return address;
1880}
1881
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001882bool
Enrico Granata07a4ac22012-05-08 21:25:06 +00001883ValueObject::SetValueFromCString (const char *value_str, Error& error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001884{
Enrico Granata07a4ac22012-05-08 21:25:06 +00001885 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001886 // Make sure our value is up to date first so that our location and location
1887 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001888 if (!UpdateValueIfNeeded(false))
Enrico Granata07a4ac22012-05-08 21:25:06 +00001889 {
1890 error.SetErrorString("unable to read value");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001891 return false;
Enrico Granata07a4ac22012-05-08 21:25:06 +00001892 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001893
Greg Claytonfaac1112013-03-14 18:31:44 +00001894 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001895 const Encoding encoding = GetClangType().GetEncoding (count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001896
Greg Claytonb1320972010-07-14 00:18:15 +00001897 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898
Jim Ingham16e0c682011-08-12 23:34:31 +00001899 Value::ValueType value_type = m_value.GetValueType();
1900
1901 if (value_type == Value::eValueTypeScalar)
1902 {
1903 // If the value is already a scalar, then let the scalar change itself:
1904 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1905 }
1906 else if (byte_size <= Scalar::GetMaxByteSize())
1907 {
1908 // If the value fits in a scalar, then make a new scalar and again let the
1909 // scalar code do the conversion, then figure out where to put the new value.
1910 Scalar new_scalar;
Jim Ingham16e0c682011-08-12 23:34:31 +00001911 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1912 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913 {
Jim Ingham4b536182011-08-09 02:12:22 +00001914 switch (value_type)
1915 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001916 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001917 {
1918 // If it is a load address, then the scalar value is the storage location
1919 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001920 ExecutionContext exe_ctx (GetExecutionContextRef());
1921 Process *process = exe_ctx.GetProcessPtr();
1922 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001923 {
Enrico Granata48ea80f2012-10-24 20:24:39 +00001924 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001925 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1926 new_scalar,
1927 byte_size,
1928 error);
Enrico Granata07a4ac22012-05-08 21:25:06 +00001929 if (!error.Success())
1930 return false;
1931 if (bytes_written != byte_size)
1932 {
1933 error.SetErrorString("unable to write value to memory");
1934 return false;
1935 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001936 }
1937 }
Jim Ingham4b536182011-08-09 02:12:22 +00001938 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001939 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001940 {
1941 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1942 DataExtractor new_data;
1943 new_data.SetByteOrder (m_data.GetByteOrder());
1944
1945 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1946 m_data.SetData(buffer_sp, 0);
1947 bool success = new_scalar.GetData(new_data);
1948 if (success)
1949 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001950 new_data.CopyByteOrderedData (0,
1951 byte_size,
1952 const_cast<uint8_t *>(m_data.GetDataStart()),
1953 byte_size,
1954 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001955 }
1956 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1957
1958 }
Jim Ingham4b536182011-08-09 02:12:22 +00001959 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001960 case Value::eValueTypeFileAddress:
1961 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001962 case Value::eValueTypeVector:
1963 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001964 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 }
1966 else
1967 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001968 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001969 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001970 }
1971 else
1972 {
1973 // We don't support setting things bigger than a scalar at present.
Enrico Granata07a4ac22012-05-08 21:25:06 +00001974 error.SetErrorString("unable to write aggregate data type");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975 return false;
1976 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001977
1978 // If we have reached this point, then we have successfully changed the value.
1979 SetNeedsUpdate();
1980 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981}
1982
Greg Clayton81e871e2012-02-04 02:27:34 +00001983bool
1984ValueObject::GetDeclaration (Declaration &decl)
1985{
1986 decl.Clear();
1987 return false;
1988}
1989
Greg Clayton84db9102012-03-26 23:03:23 +00001990ConstString
1991ValueObject::GetTypeName()
1992{
Greg Clayton57ee3062013-07-11 22:46:58 +00001993 return GetClangType().GetConstTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00001994}
1995
1996ConstString
Enrico Granatae8daa2f2014-05-17 19:14:17 +00001997ValueObject::GetDisplayTypeName()
1998{
1999 return GetTypeName();
2000}
2001
2002ConstString
Greg Clayton84db9102012-03-26 23:03:23 +00002003ValueObject::GetQualifiedTypeName()
2004{
Greg Clayton57ee3062013-07-11 22:46:58 +00002005 return GetClangType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00002006}
2007
2008
Greg Claytonafacd142011-09-02 01:15:17 +00002009LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00002010ValueObject::GetObjectRuntimeLanguage ()
2011{
Greg Clayton57ee3062013-07-11 22:46:58 +00002012 return GetClangType().GetMinimumLanguage ();
Jim Ingham5a369122010-09-28 01:25:32 +00002013}
2014
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002015void
Jim Ingham58b59f92011-04-22 23:53:53 +00002016ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002017{
Jim Ingham58b59f92011-04-22 23:53:53 +00002018 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019}
2020
2021ValueObjectSP
2022ValueObject::GetSyntheticChild (const ConstString &key) const
2023{
2024 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00002025 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002026 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00002027 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028 return synthetic_child_sp;
2029}
2030
Greg Clayton2452ab72013-02-08 22:02:02 +00002031uint32_t
Greg Clayton57ee3062013-07-11 22:46:58 +00002032ValueObject::GetTypeInfo (ClangASTType *pointee_or_element_clang_type)
Greg Clayton2452ab72013-02-08 22:02:02 +00002033{
Greg Clayton57ee3062013-07-11 22:46:58 +00002034 return GetClangType().GetTypeInfo (pointee_or_element_clang_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00002035}
2036
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037bool
2038ValueObject::IsPointerType ()
2039{
Greg Clayton57ee3062013-07-11 22:46:58 +00002040 return GetClangType().IsPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041}
2042
Jim Inghamb7603bb2011-03-18 00:05:18 +00002043bool
Greg Claytondaf515f2011-07-09 20:12:33 +00002044ValueObject::IsArrayType ()
2045{
Greg Clayton57ee3062013-07-11 22:46:58 +00002046 return GetClangType().IsArrayType (NULL, NULL, NULL);
Greg Claytondaf515f2011-07-09 20:12:33 +00002047}
2048
2049bool
Enrico Granata9fc19442011-07-06 02:13:41 +00002050ValueObject::IsScalarType ()
2051{
Greg Clayton57ee3062013-07-11 22:46:58 +00002052 return GetClangType().IsScalarType ();
Enrico Granata9fc19442011-07-06 02:13:41 +00002053}
2054
2055bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00002056ValueObject::IsIntegerType (bool &is_signed)
2057{
Greg Clayton57ee3062013-07-11 22:46:58 +00002058 return GetClangType().IsIntegerType (is_signed);
Jim Inghamb7603bb2011-03-18 00:05:18 +00002059}
Greg Clayton73b472d2010-10-27 03:32:59 +00002060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002061bool
2062ValueObject::IsPointerOrReferenceType ()
2063{
Greg Clayton57ee3062013-07-11 22:46:58 +00002064 return GetClangType().IsPointerOrReferenceType ();
Greg Clayton007d5be2011-05-30 00:49:24 +00002065}
2066
2067bool
Greg Claytondea8cb42011-06-29 22:09:02 +00002068ValueObject::IsPossibleDynamicType ()
2069{
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002070 ExecutionContext exe_ctx (GetExecutionContextRef());
2071 Process *process = exe_ctx.GetProcessPtr();
2072 if (process)
2073 return process->IsPossibleDynamicValue(*this);
2074 else
Greg Clayton57ee3062013-07-11 22:46:58 +00002075 return GetClangType().IsPossibleDynamicType (NULL, true, true);
Greg Claytondea8cb42011-06-29 22:09:02 +00002076}
2077
Enrico Granata9e7b3882012-12-13 23:50:33 +00002078bool
2079ValueObject::IsObjCNil ()
2080{
Enrico Granata622be232014-10-21 20:52:14 +00002081 const uint32_t mask = eTypeIsObjC | eTypeIsPointer;
Greg Clayton57ee3062013-07-11 22:46:58 +00002082 bool isObjCpointer = (((GetClangType().GetTypeInfo(NULL)) & mask) == mask);
Enrico Granata7277d202013-03-15 23:33:15 +00002083 if (!isObjCpointer)
2084 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002085 bool canReadValue = true;
2086 bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
Enrico Granata7277d202013-03-15 23:33:15 +00002087 return canReadValue && isZero;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002088}
2089
Greg Claytonafacd142011-09-02 01:15:17 +00002090ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002091ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002092{
Greg Clayton2452ab72013-02-08 22:02:02 +00002093 const uint32_t type_info = GetTypeInfo ();
Enrico Granata622be232014-10-21 20:52:14 +00002094 if (type_info & eTypeIsArray)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002095 return GetSyntheticArrayMemberFromArray(index, can_create);
2096
Enrico Granata622be232014-10-21 20:52:14 +00002097 if (type_info & eTypeIsPointer)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002098 return GetSyntheticArrayMemberFromPointer(index, can_create);
2099
2100 return ValueObjectSP();
2101
2102}
2103
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002104ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002105ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002106{
2107 ValueObjectSP synthetic_child_sp;
2108 if (IsPointerType ())
2109 {
2110 char index_str[64];
Deepak Panickal99fbc072014-03-03 15:39:47 +00002111 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002112 ConstString index_const_str(index_str);
2113 // Check if we have already created a synthetic array member in this
2114 // valid object. If we have we will re-use it.
2115 synthetic_child_sp = GetSyntheticChild (index_const_str);
2116 if (!synthetic_child_sp)
2117 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002118 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002119 // We haven't made a synthetic array member for INDEX yet, so
2120 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00002121 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002122
2123 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00002124 if (synthetic_child)
2125 {
2126 AddSyntheticChild(index_const_str, synthetic_child);
2127 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002128 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00002129 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00002130 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 }
2132 }
2133 return synthetic_child_sp;
2134}
Jim Ingham22777012010-09-23 02:01:19 +00002135
Greg Claytondaf515f2011-07-09 20:12:33 +00002136// This allows you to create an array member using and index
2137// that doesn't not fall in the normal bounds of the array.
2138// Many times structure can be defined as:
2139// struct Collection
2140// {
2141// uint32_t item_count;
2142// Item item_array[0];
2143// };
2144// The size of the "item_array" is 1, but many times in practice
2145// there are more items in "item_array".
2146
2147ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002148ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create)
Greg Claytondaf515f2011-07-09 20:12:33 +00002149{
2150 ValueObjectSP synthetic_child_sp;
2151 if (IsArrayType ())
2152 {
2153 char index_str[64];
Deepak Panickal99fbc072014-03-03 15:39:47 +00002154 snprintf(index_str, sizeof(index_str), "[%" PRIu64 "]", (uint64_t)index);
Greg Claytondaf515f2011-07-09 20:12:33 +00002155 ConstString index_const_str(index_str);
2156 // Check if we have already created a synthetic array member in this
2157 // valid object. If we have we will re-use it.
2158 synthetic_child_sp = GetSyntheticChild (index_const_str);
2159 if (!synthetic_child_sp)
2160 {
2161 ValueObject *synthetic_child;
2162 // We haven't made a synthetic array member for INDEX yet, so
2163 // lets make one and cache it for any future reference.
2164 synthetic_child = CreateChildAtIndex(0, true, index);
2165
2166 // Cache the value if we got one back...
2167 if (synthetic_child)
2168 {
2169 AddSyntheticChild(index_const_str, synthetic_child);
2170 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002171 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00002172 synthetic_child_sp->m_is_array_item_for_pointer = true;
2173 }
2174 }
2175 }
2176 return synthetic_child_sp;
2177}
2178
Enrico Granata9fc19442011-07-06 02:13:41 +00002179ValueObjectSP
2180ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2181{
2182 ValueObjectSP synthetic_child_sp;
2183 if (IsScalarType ())
2184 {
2185 char index_str[64];
2186 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2187 ConstString index_const_str(index_str);
2188 // Check if we have already created a synthetic array member in this
2189 // valid object. If we have we will re-use it.
2190 synthetic_child_sp = GetSyntheticChild (index_const_str);
2191 if (!synthetic_child_sp)
2192 {
Enrico Granata9fc19442011-07-06 02:13:41 +00002193 // We haven't made a synthetic array member for INDEX yet, so
2194 // lets make one and cache it for any future reference.
Greg Clayton57ee3062013-07-11 22:46:58 +00002195 ValueObjectChild *synthetic_child = new ValueObjectChild (*this,
2196 GetClangType(),
2197 index_const_str,
2198 GetByteSize(),
2199 0,
2200 to-from+1,
2201 from,
2202 false,
2203 false,
2204 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00002205
2206 // Cache the value if we got one back...
2207 if (synthetic_child)
2208 {
2209 AddSyntheticChild(index_const_str, synthetic_child);
2210 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002211 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00002212 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2213 }
2214 }
2215 }
2216 return synthetic_child_sp;
2217}
2218
Greg Claytonafacd142011-09-02 01:15:17 +00002219ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00002220ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
2221{
2222
2223 ValueObjectSP synthetic_child_sp;
2224
2225 char name_str[64];
2226 snprintf(name_str, sizeof(name_str), "@%i", offset);
2227 ConstString name_const_str(name_str);
2228
2229 // Check if we have already created a synthetic array member in this
2230 // valid object. If we have we will re-use it.
2231 synthetic_child_sp = GetSyntheticChild (name_const_str);
2232
2233 if (synthetic_child_sp.get())
2234 return synthetic_child_sp;
2235
2236 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00002237 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002238
2239 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
Greg Clayton57ee3062013-07-11 22:46:58 +00002240 type,
Enrico Granata6f3533f2011-07-29 19:53:35 +00002241 name_const_str,
Greg Clayton57ee3062013-07-11 22:46:58 +00002242 type.GetByteSize(),
Enrico Granata6f3533f2011-07-29 19:53:35 +00002243 offset,
2244 0,
2245 0,
2246 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00002247 false,
2248 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002249 if (synthetic_child)
2250 {
2251 AddSyntheticChild(name_const_str, synthetic_child);
2252 synthetic_child_sp = synthetic_child->GetSP();
2253 synthetic_child_sp->SetName(name_const_str);
2254 synthetic_child_sp->m_is_child_at_offset = true;
2255 }
2256 return synthetic_child_sp;
2257}
2258
Enrico Granata32556cd2014-08-26 20:54:04 +00002259ValueObjectSP
Enrico Granata59953f02014-08-26 21:35:30 +00002260ValueObject::GetSyntheticBase (uint32_t offset, const ClangASTType& type, bool can_create)
Enrico Granata32556cd2014-08-26 20:54:04 +00002261{
2262 ValueObjectSP synthetic_child_sp;
2263
2264 char name_str[64];
2265 snprintf(name_str, sizeof(name_str), "%s", type.GetTypeName().AsCString("<unknown>"));
2266 ConstString name_const_str(name_str);
2267
2268 // Check if we have already created a synthetic array member in this
2269 // valid object. If we have we will re-use it.
2270 synthetic_child_sp = GetSyntheticChild (name_const_str);
2271
2272 if (synthetic_child_sp.get())
2273 return synthetic_child_sp;
2274
2275 if (!can_create)
2276 return ValueObjectSP();
2277
Enrico Granata32556cd2014-08-26 20:54:04 +00002278 const bool is_base_class = true;
2279
2280 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2281 type,
2282 name_const_str,
2283 type.GetByteSize(),
2284 offset,
2285 0,
2286 0,
2287 is_base_class,
2288 false,
2289 eAddressTypeInvalid);
2290 if (synthetic_child)
2291 {
2292 AddSyntheticChild(name_const_str, synthetic_child);
2293 synthetic_child_sp = synthetic_child->GetSP();
2294 synthetic_child_sp->SetName(name_const_str);
2295 }
2296 return synthetic_child_sp;
2297}
2298
2299
Enrico Granatad55546b2011-07-22 00:16:08 +00002300// your expression path needs to have a leading . or ->
2301// (unless it somehow "looks like" an array, in which case it has
2302// a leading [ symbol). while the [ is meaningful and should be shown
2303// to the user, . and -> are just parser design, but by no means
2304// added information for the user.. strip them off
2305static const char*
2306SkipLeadingExpressionPathSeparators(const char* expression)
2307{
2308 if (!expression || !expression[0])
2309 return expression;
2310 if (expression[0] == '.')
2311 return expression+1;
2312 if (expression[0] == '-' && expression[1] == '>')
2313 return expression+2;
2314 return expression;
2315}
2316
Greg Claytonafacd142011-09-02 01:15:17 +00002317ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00002318ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2319{
2320 ValueObjectSP synthetic_child_sp;
2321 ConstString name_const_string(expression);
2322 // Check if we have already created a synthetic array member in this
2323 // valid object. If we have we will re-use it.
2324 synthetic_child_sp = GetSyntheticChild (name_const_string);
2325 if (!synthetic_child_sp)
2326 {
2327 // We haven't made a synthetic array member for expression yet, so
2328 // lets make one and cache it for any future reference.
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002329 synthetic_child_sp = GetValueForExpressionPath(expression,
2330 NULL, NULL, NULL,
2331 GetValueForExpressionPathOptions().DontAllowSyntheticChildren());
Enrico Granatad55546b2011-07-22 00:16:08 +00002332
2333 // Cache the value if we got one back...
2334 if (synthetic_child_sp.get())
2335 {
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002336 // 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 +00002337 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00002338 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002339 }
2340 }
2341 return synthetic_child_sp;
2342}
2343
2344void
Enrico Granata86cc9822012-03-19 22:58:49 +00002345ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002346{
Enrico Granata86cc9822012-03-19 22:58:49 +00002347 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002348 return;
2349
Enrico Granatac5bc4122012-03-27 02:35:13 +00002350 TargetSP target_sp(GetTargetSP());
Enrico Granata5d5f60c2013-09-24 22:58:37 +00002351 if (target_sp && target_sp->GetEnableSyntheticValue() == false)
Enrico Granatac5bc4122012-03-27 02:35:13 +00002352 {
2353 m_synthetic_value = NULL;
2354 return;
2355 }
2356
Enrico Granatae3e91512012-10-22 18:18:36 +00002357 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2358
Enrico Granata5548cb52013-01-28 23:47:25 +00002359 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
Enrico Granata86cc9822012-03-19 22:58:49 +00002360 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002361
Enrico Granata0c489f52012-03-01 04:24:26 +00002362 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002363 return;
2364
Enrico Granatae3e91512012-10-22 18:18:36 +00002365 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2366 return;
2367
Enrico Granata86cc9822012-03-19 22:58:49 +00002368 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002369}
2370
Jim Ingham78a685a2011-04-16 00:01:13 +00002371void
Greg Claytonafacd142011-09-02 01:15:17 +00002372ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002373{
Greg Claytonafacd142011-09-02 01:15:17 +00002374 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002375 return;
2376
Jim Ingham58b59f92011-04-22 23:53:53 +00002377 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002378 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002379 ExecutionContext exe_ctx (GetExecutionContextRef());
2380 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002381 if (process && process->IsPossibleDynamicValue(*this))
Enrico Granatae3e91512012-10-22 18:18:36 +00002382 {
2383 ClearDynamicTypeInformation ();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002384 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Enrico Granatae3e91512012-10-22 18:18:36 +00002385 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002386 }
2387}
2388
Jim Ingham58b59f92011-04-22 23:53:53 +00002389ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002390ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002391{
Greg Claytonafacd142011-09-02 01:15:17 +00002392 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002393 return ValueObjectSP();
2394
2395 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002396 {
Jim Ingham2837b762011-05-04 03:43:18 +00002397 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002398 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002399 if (m_dynamic_value)
2400 return m_dynamic_value->GetSP();
2401 else
2402 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002403}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002404
Jim Ingham60dbabb2011-12-08 19:44:08 +00002405ValueObjectSP
2406ValueObject::GetStaticValue()
2407{
2408 return GetSP();
2409}
2410
Enrico Granata886147f2012-05-08 18:47:08 +00002411lldb::ValueObjectSP
2412ValueObject::GetNonSyntheticValue ()
2413{
2414 return GetSP();
2415}
2416
Enrico Granatad55546b2011-07-22 00:16:08 +00002417ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002418ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002419{
Enrico Granata86cc9822012-03-19 22:58:49 +00002420 if (use_synthetic == false)
2421 return ValueObjectSP();
2422
Enrico Granatad55546b2011-07-22 00:16:08 +00002423 CalculateSyntheticValue(use_synthetic);
2424
2425 if (m_synthetic_value)
2426 return m_synthetic_value->GetSP();
2427 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002428 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002429}
2430
Greg Claytone221f822011-01-21 01:59:00 +00002431bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002432ValueObject::HasSyntheticValue()
2433{
Enrico Granata5548cb52013-01-28 23:47:25 +00002434 UpdateFormatsIfNeeded();
Enrico Granata27b625e2011-08-09 01:04:56 +00002435
Enrico Granata0c489f52012-03-01 04:24:26 +00002436 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002437 return false;
2438
Enrico Granata86cc9822012-03-19 22:58:49 +00002439 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002440
2441 if (m_synthetic_value)
2442 return true;
2443 else
2444 return false;
2445}
2446
2447bool
Greg Claytone221f822011-01-21 01:59:00 +00002448ValueObject::GetBaseClassPath (Stream &s)
2449{
2450 if (IsBaseClass())
2451 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002452 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Clayton57ee3062013-07-11 22:46:58 +00002453 ClangASTType clang_type = GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002454 std::string cxx_class_name;
Greg Clayton57ee3062013-07-11 22:46:58 +00002455 bool this_had_base_class = clang_type.GetCXXClassName (cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002456 if (this_had_base_class)
2457 {
2458 if (parent_had_base_class)
2459 s.PutCString("::");
2460 s.PutCString(cxx_class_name.c_str());
2461 }
2462 return parent_had_base_class || this_had_base_class;
2463 }
2464 return false;
2465}
2466
2467
2468ValueObject *
2469ValueObject::GetNonBaseClassParent()
2470{
Jim Ingham78a685a2011-04-16 00:01:13 +00002471 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002472 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002473 if (GetParent()->IsBaseClass())
2474 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002475 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002476 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002477 }
2478 return NULL;
2479}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002480
Enrico Granataa3c8f042014-08-19 22:29:08 +00002481
2482bool
2483ValueObject::IsBaseClass (uint32_t& depth)
2484{
2485 if (!IsBaseClass())
2486 {
2487 depth = 0;
2488 return false;
2489 }
2490 if (GetParent())
2491 {
2492 GetParent()->IsBaseClass(depth);
2493 depth = depth + 1;
2494 return true;
2495 }
2496 // TODO: a base of no parent? weird..
2497 depth = 1;
2498 return true;
2499}
2500
Greg Clayton1d3afba2010-10-05 00:00:42 +00002501void
Enrico Granata4becb372011-06-29 22:27:15 +00002502ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002503{
Greg Claytone221f822011-01-21 01:59:00 +00002504 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002505
Enrico Granata86cc9822012-03-19 22:58:49 +00002506 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002507 {
Enrico Granata4becb372011-06-29 22:27:15 +00002508 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2509 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2510 // the eHonorPointers mode is meant to produce strings in this latter format
2511 s.PutCString("*(");
2512 }
Greg Claytone221f822011-01-21 01:59:00 +00002513
Enrico Granata4becb372011-06-29 22:27:15 +00002514 ValueObject* parent = GetParent();
2515
2516 if (parent)
2517 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002518
2519 // if we are a deref_of_parent just because we are synthetic array
2520 // members made up to allow ptr[%d] syntax to work in variable
2521 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002522 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002523 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002524
Greg Claytone221f822011-01-21 01:59:00 +00002525 if (!IsBaseClass())
2526 {
2527 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002528 {
Greg Claytone221f822011-01-21 01:59:00 +00002529 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2530 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002531 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002532 ClangASTType non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002533 if (non_base_class_parent_clang_type)
2534 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002535 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002536 {
2537 s.PutCString("->");
2538 }
Enrico Granata4becb372011-06-29 22:27:15 +00002539 else
2540 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002541 const uint32_t non_base_class_parent_type_info = non_base_class_parent_clang_type.GetTypeInfo();
2542
Enrico Granata622be232014-10-21 20:52:14 +00002543 if (non_base_class_parent_type_info & eTypeIsPointer)
Enrico Granata4becb372011-06-29 22:27:15 +00002544 {
2545 s.PutCString("->");
2546 }
Enrico Granata622be232014-10-21 20:52:14 +00002547 else if ((non_base_class_parent_type_info & eTypeHasChildren) &&
2548 !(non_base_class_parent_type_info & eTypeIsArray))
Enrico Granata4becb372011-06-29 22:27:15 +00002549 {
2550 s.PutChar('.');
2551 }
Greg Claytone221f822011-01-21 01:59:00 +00002552 }
2553 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002554 }
Greg Claytone221f822011-01-21 01:59:00 +00002555
2556 const char *name = GetName().GetCString();
2557 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002558 {
Greg Claytone221f822011-01-21 01:59:00 +00002559 if (qualify_cxx_base_classes)
2560 {
2561 if (GetBaseClassPath (s))
2562 s.PutCString("::");
2563 }
2564 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002565 }
2566 }
2567 }
2568
Enrico Granata86cc9822012-03-19 22:58:49 +00002569 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002570 {
Greg Claytone221f822011-01-21 01:59:00 +00002571 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002572 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002573}
2574
Greg Claytonafacd142011-09-02 01:15:17 +00002575ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002576ValueObject::GetValueForExpressionPath(const char* expression,
2577 const char** first_unparsed,
2578 ExpressionPathScanEndReason* reason_to_stop,
2579 ExpressionPathEndResultType* final_value_type,
2580 const GetValueForExpressionPathOptions& options,
2581 ExpressionPathAftermath* final_task_on_target)
2582{
2583
2584 const char* dummy_first_unparsed;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002585 ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2586 ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata86cc9822012-03-19 22:58:49 +00002587 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002588
2589 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2590 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2591 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2592 final_value_type ? final_value_type : &dummy_final_value_type,
2593 options,
2594 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2595
Enrico Granata86cc9822012-03-19 22:58:49 +00002596 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002597 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002598
Enrico Granata86cc9822012-03-19 22:58:49 +00002599 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 +00002600 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002601 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002602 {
2603 Error error;
2604 ValueObjectSP final_value = ret_val->Dereference(error);
2605 if (error.Fail() || !final_value.get())
2606 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002607 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002608 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002609 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002610 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002611 return ValueObjectSP();
2612 }
2613 else
2614 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002615 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002616 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002617 return final_value;
2618 }
2619 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002620 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002621 {
2622 Error error;
2623 ValueObjectSP final_value = ret_val->AddressOf(error);
2624 if (error.Fail() || !final_value.get())
2625 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002626 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002627 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002628 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002629 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002630 return ValueObjectSP();
2631 }
2632 else
2633 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002634 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002635 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002636 return final_value;
2637 }
2638 }
2639 }
2640 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2641}
2642
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002643int
2644ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002645 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002646 const char** first_unparsed,
2647 ExpressionPathScanEndReason* reason_to_stop,
2648 ExpressionPathEndResultType* final_value_type,
2649 const GetValueForExpressionPathOptions& options,
2650 ExpressionPathAftermath* final_task_on_target)
2651{
2652 const char* dummy_first_unparsed;
2653 ExpressionPathScanEndReason dummy_reason_to_stop;
2654 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002655 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002656
2657 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2658 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2659 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2660 final_value_type ? final_value_type : &dummy_final_value_type,
2661 options,
2662 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2663
2664 if (!ret_val.get()) // if there are errors, I add nothing to the list
2665 return 0;
2666
Enrico Granata86ea8d82012-03-29 01:34:34 +00002667 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002668 {
2669 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002670 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002671 {
2672 list->Append(ret_val);
2673 return 1;
2674 }
Enrico Granata86ea8d82012-03-29 01:34:34 +00002675 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 +00002676 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002677 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002678 {
2679 Error error;
2680 ValueObjectSP final_value = ret_val->Dereference(error);
2681 if (error.Fail() || !final_value.get())
2682 {
Greg Clayton23f59502012-07-17 03:23:13 +00002683 if (reason_to_stop)
2684 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2685 if (final_value_type)
2686 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002687 return 0;
2688 }
2689 else
2690 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002691 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002692 list->Append(final_value);
2693 return 1;
2694 }
2695 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002696 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002697 {
2698 Error error;
2699 ValueObjectSP final_value = ret_val->AddressOf(error);
2700 if (error.Fail() || !final_value.get())
2701 {
Greg Clayton23f59502012-07-17 03:23:13 +00002702 if (reason_to_stop)
2703 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2704 if (final_value_type)
2705 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002706 return 0;
2707 }
2708 else
2709 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002710 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002711 list->Append(final_value);
2712 return 1;
2713 }
2714 }
2715 }
2716 }
2717 else
2718 {
2719 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2720 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2721 ret_val,
2722 list,
2723 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2724 final_value_type ? final_value_type : &dummy_final_value_type,
2725 options,
2726 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2727 }
2728 // in any non-covered case, just do the obviously right thing
2729 list->Append(ret_val);
2730 return 1;
2731}
2732
Greg Claytonafacd142011-09-02 01:15:17 +00002733ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002734ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2735 const char** first_unparsed,
2736 ExpressionPathScanEndReason* reason_to_stop,
2737 ExpressionPathEndResultType* final_result,
2738 const GetValueForExpressionPathOptions& options,
2739 ExpressionPathAftermath* what_next)
2740{
2741 ValueObjectSP root = GetSP();
2742
2743 if (!root.get())
2744 return ValueObjectSP();
2745
2746 *first_unparsed = expression_cstr;
2747
2748 while (true)
2749 {
2750
2751 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2752
Greg Clayton57ee3062013-07-11 22:46:58 +00002753 ClangASTType root_clang_type = root->GetClangType();
2754 ClangASTType pointee_clang_type;
2755 Flags pointee_clang_type_info;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002756
Greg Clayton57ee3062013-07-11 22:46:58 +00002757 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002758 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00002759 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002760
2761 if (!expression_cstr || *expression_cstr == '\0')
2762 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002763 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002764 return root;
2765 }
2766
2767 switch (*expression_cstr)
2768 {
2769 case '-':
2770 {
2771 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granata622be232014-10-21 20:52:14 +00002772 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 +00002773 {
2774 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002775 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2776 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002777 return ValueObjectSP();
2778 }
Enrico Granata622be232014-10-21 20:52:14 +00002779 if (root_clang_type_info.Test(eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2780 root_clang_type_info.Test(eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002781 options.m_no_fragile_ivar)
2782 {
2783 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002784 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2785 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002786 return ValueObjectSP();
2787 }
2788 if (expression_cstr[1] != '>')
2789 {
2790 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002791 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2792 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002793 return ValueObjectSP();
2794 }
2795 expression_cstr++; // skip the -
2796 }
2797 case '.': // or fallthrough from ->
2798 {
2799 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granata622be232014-10-21 20:52:14 +00002800 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 +00002801 {
2802 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002803 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2804 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002805 return ValueObjectSP();
2806 }
2807 expression_cstr++; // skip .
2808 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2809 ConstString child_name;
2810 if (!next_separator) // if no other separator just expand this last layer
2811 {
2812 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002813 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2814
2815 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002816 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002817 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002818 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2819 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002820 return child_valobj_sp;
2821 }
2822 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2823 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002824 if (root->IsSynthetic())
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002825 {
2826 *first_unparsed = expression_cstr;
Enrico Granatad07cfd32014-10-08 18:27:36 +00002827 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchSyntheticChild;
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002828 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2829 return ValueObjectSP();
2830 }
2831
2832 child_valobj_sp = root->GetSyntheticValue();
Enrico Granata86cc9822012-03-19 22:58:49 +00002833 if (child_valobj_sp.get())
2834 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002835 }
2836
2837 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2838 // so we hit the "else" branch, and return an error
2839 if(child_valobj_sp.get()) // if it worked, just return
2840 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002841 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002842 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2843 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002844 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002845 }
2846 else
2847 {
2848 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002849 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2850 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002851 return ValueObjectSP();
2852 }
2853 }
2854 else // other layers do expand
2855 {
2856 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002857 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2858 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002859 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002860 root = child_valobj_sp;
2861 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002862 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002863 continue;
2864 }
2865 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2866 {
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002867 if (root->IsSynthetic())
2868 {
2869 *first_unparsed = expression_cstr;
2870 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2871 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2872 return ValueObjectSP();
2873 }
2874
Enrico Granata86cc9822012-03-19 22:58:49 +00002875 child_valobj_sp = root->GetSyntheticValue(true);
2876 if (child_valobj_sp)
2877 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002878 }
2879
2880 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2881 // so we hit the "else" branch, and return an error
2882 if(child_valobj_sp.get()) // if it worked, move on
2883 {
2884 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002885 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002886 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002887 continue;
2888 }
2889 else
2890 {
2891 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002892 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2893 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002894 return ValueObjectSP();
2895 }
2896 }
2897 break;
2898 }
2899 case '[':
2900 {
Enrico Granata622be232014-10-21 20:52:14 +00002901 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 +00002902 {
Enrico Granata622be232014-10-21 20:52:14 +00002903 if (!root_clang_type_info.Test(eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002904 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002905 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2906 {
2907 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002908 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2909 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002910 return ValueObjectSP();
2911 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002912 }
2913 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2914 {
2915 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002916 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2917 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002918 return ValueObjectSP();
2919 }
2920 }
2921 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2922 {
Enrico Granata622be232014-10-21 20:52:14 +00002923 if (!root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002924 {
2925 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002926 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2927 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002928 return ValueObjectSP();
2929 }
2930 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2931 {
2932 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002933 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2934 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002935 return root;
2936 }
2937 }
2938 const char *separator_position = ::strchr(expression_cstr+1,'-');
2939 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2940 if (!close_bracket_position) // if there is no ], this is a syntax error
2941 {
2942 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002943 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2944 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002945 return ValueObjectSP();
2946 }
2947 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2948 {
2949 char *end = NULL;
2950 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2951 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2952 {
2953 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002954 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2955 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002956 return ValueObjectSP();
2957 }
2958 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2959 {
Enrico Granata622be232014-10-21 20:52:14 +00002960 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002961 {
2962 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002963 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2964 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002965 return root;
2966 }
2967 else
2968 {
2969 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002970 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2971 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002972 return ValueObjectSP();
2973 }
2974 }
2975 // from here on we do have a valid index
Enrico Granata622be232014-10-21 20:52:14 +00002976 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002977 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002978 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2979 if (!child_valobj_sp)
2980 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002981 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002982 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2983 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002984 if (child_valobj_sp)
2985 {
2986 root = child_valobj_sp;
2987 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002988 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002989 continue;
2990 }
2991 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002992 {
2993 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002994 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2995 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002996 return ValueObjectSP();
2997 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002998 }
Enrico Granata622be232014-10-21 20:52:14 +00002999 else if (root_clang_type_info.Test(eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003000 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003001 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 +00003002 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003003 {
3004 Error error;
3005 root = root->Dereference(error);
3006 if (error.Fail() || !root.get())
3007 {
3008 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003009 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3010 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003011 return ValueObjectSP();
3012 }
3013 else
3014 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003015 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003016 continue;
3017 }
3018 }
3019 else
3020 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003021 if (root->GetClangType().GetMinimumLanguage() == eLanguageTypeObjC
Enrico Granata622be232014-10-21 20:52:14 +00003022 && pointee_clang_type_info.AllClear(eTypeIsPointer)
Greg Clayton84db9102012-03-26 23:03:23 +00003023 && root->HasSyntheticValue()
3024 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00003025 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003026 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00003027 }
3028 else
3029 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003030 if (!root.get())
3031 {
3032 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003033 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3034 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003035 return ValueObjectSP();
3036 }
3037 else
3038 {
3039 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003040 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003041 continue;
3042 }
3043 }
3044 }
Enrico Granata622be232014-10-21 20:52:14 +00003045 else if (root_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003046 {
3047 root = root->GetSyntheticBitFieldChild(index, index, true);
3048 if (!root.get())
3049 {
3050 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003051 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3052 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003053 return ValueObjectSP();
3054 }
3055 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3056 {
3057 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003058 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3059 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003060 return root;
3061 }
3062 }
Enrico Granata622be232014-10-21 20:52:14 +00003063 else if (root_clang_type_info.Test(eTypeIsVector))
Enrico Granata08a1bb82013-06-19 00:00:45 +00003064 {
3065 root = root->GetChildAtIndex(index, true);
3066 if (!root.get())
3067 {
3068 *first_unparsed = expression_cstr;
3069 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3070 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3071 return ValueObjectSP();
3072 }
3073 else
3074 {
3075 *first_unparsed = end+1; // skip ]
3076 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3077 continue;
3078 }
3079 }
Enrico Granata86cc9822012-03-19 22:58:49 +00003080 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00003081 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003082 if (root->HasSyntheticValue())
3083 root = root->GetSyntheticValue();
3084 else if (!root->IsSynthetic())
3085 {
3086 *first_unparsed = expression_cstr;
3087 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3088 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3089 return ValueObjectSP();
3090 }
3091 // if we are here, then root itself is a synthetic VO.. should be good to go
3092
Enrico Granata27b625e2011-08-09 01:04:56 +00003093 if (!root.get())
3094 {
3095 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003096 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3097 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3098 return ValueObjectSP();
3099 }
3100 root = root->GetChildAtIndex(index, true);
3101 if (!root.get())
3102 {
3103 *first_unparsed = expression_cstr;
3104 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3105 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003106 return ValueObjectSP();
3107 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00003108 else
3109 {
3110 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003111 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00003112 continue;
3113 }
Enrico Granata27b625e2011-08-09 01:04:56 +00003114 }
3115 else
3116 {
3117 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003118 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3119 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003120 return ValueObjectSP();
3121 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003122 }
3123 else // we have a low and a high index
3124 {
3125 char *end = NULL;
3126 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3127 if (!end || end != separator_position) // if something weird is in our way return an error
3128 {
3129 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003130 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3131 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003132 return ValueObjectSP();
3133 }
3134 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3135 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3136 {
3137 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003138 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3139 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003140 return ValueObjectSP();
3141 }
3142 if (index_lower > index_higher) // swap indices if required
3143 {
3144 unsigned long temp = index_lower;
3145 index_lower = index_higher;
3146 index_higher = temp;
3147 }
Enrico Granata622be232014-10-21 20:52:14 +00003148 if (root_clang_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003149 {
3150 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3151 if (!root.get())
3152 {
3153 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003154 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3155 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003156 return ValueObjectSP();
3157 }
3158 else
3159 {
3160 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003161 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3162 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003163 return root;
3164 }
3165 }
Enrico Granata622be232014-10-21 20:52:14 +00003166 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 +00003167 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granata622be232014-10-21 20:52:14 +00003168 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003169 {
3170 Error error;
3171 root = root->Dereference(error);
3172 if (error.Fail() || !root.get())
3173 {
3174 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003175 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3176 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003177 return ValueObjectSP();
3178 }
3179 else
3180 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003181 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003182 continue;
3183 }
3184 }
3185 else
3186 {
3187 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003188 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3189 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003190 return root;
3191 }
3192 }
3193 break;
3194 }
3195 default: // some non-separator is in the way
3196 {
3197 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003198 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3199 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003200 return ValueObjectSP();
3201 break;
3202 }
3203 }
3204 }
3205}
3206
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003207int
3208ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3209 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00003210 ValueObjectSP root,
3211 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003212 ExpressionPathScanEndReason* reason_to_stop,
3213 ExpressionPathEndResultType* final_result,
3214 const GetValueForExpressionPathOptions& options,
3215 ExpressionPathAftermath* what_next)
3216{
3217 if (!root.get())
3218 return 0;
3219
3220 *first_unparsed = expression_cstr;
3221
3222 while (true)
3223 {
3224
3225 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3226
Greg Clayton57ee3062013-07-11 22:46:58 +00003227 ClangASTType root_clang_type = root->GetClangType();
3228 ClangASTType pointee_clang_type;
3229 Flags pointee_clang_type_info;
3230 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003231 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00003232 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003233
3234 if (!expression_cstr || *expression_cstr == '\0')
3235 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003236 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003237 list->Append(root);
3238 return 1;
3239 }
3240
3241 switch (*expression_cstr)
3242 {
3243 case '[':
3244 {
Enrico Granata622be232014-10-21 20:52:14 +00003245 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 +00003246 {
Enrico Granata622be232014-10-21 20:52:14 +00003247 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 +00003248 {
3249 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003250 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3251 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003252 return 0;
3253 }
3254 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3255 {
3256 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003257 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3258 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003259 return 0;
3260 }
3261 }
3262 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3263 {
Enrico Granata622be232014-10-21 20:52:14 +00003264 if (!root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003265 {
3266 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003267 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3268 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003269 return 0;
3270 }
3271 else // expand this into list
3272 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003273 const size_t max_index = root->GetNumChildren() - 1;
3274 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003275 {
3276 ValueObjectSP child =
3277 root->GetChildAtIndex(index, true);
3278 list->Append(child);
3279 }
3280 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003281 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3282 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003283 return max_index; // tell me number of items I added to the VOList
3284 }
3285 }
3286 const char *separator_position = ::strchr(expression_cstr+1,'-');
3287 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3288 if (!close_bracket_position) // if there is no ], this is a syntax error
3289 {
3290 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003291 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3292 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003293 return 0;
3294 }
3295 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3296 {
3297 char *end = NULL;
3298 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3299 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3300 {
3301 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003302 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3303 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003304 return 0;
3305 }
3306 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3307 {
Enrico Granata622be232014-10-21 20:52:14 +00003308 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003309 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003310 const size_t max_index = root->GetNumChildren() - 1;
3311 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003312 {
3313 ValueObjectSP child =
3314 root->GetChildAtIndex(index, true);
3315 list->Append(child);
3316 }
3317 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003318 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3319 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003320 return max_index; // tell me number of items I added to the VOList
3321 }
3322 else
3323 {
3324 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003325 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3326 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003327 return 0;
3328 }
3329 }
3330 // from here on we do have a valid index
Enrico Granata622be232014-10-21 20:52:14 +00003331 if (root_clang_type_info.Test(eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003332 {
3333 root = root->GetChildAtIndex(index, true);
3334 if (!root.get())
3335 {
3336 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003337 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3338 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003339 return 0;
3340 }
3341 else
3342 {
3343 list->Append(root);
3344 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003345 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3346 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003347 return 1;
3348 }
3349 }
Enrico Granata622be232014-10-21 20:52:14 +00003350 else if (root_clang_type_info.Test(eTypeIsPointer))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003351 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003352 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 +00003353 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003354 {
3355 Error error;
3356 root = root->Dereference(error);
3357 if (error.Fail() || !root.get())
3358 {
3359 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003360 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3361 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003362 return 0;
3363 }
3364 else
3365 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003366 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003367 continue;
3368 }
3369 }
3370 else
3371 {
3372 root = root->GetSyntheticArrayMemberFromPointer(index, true);
3373 if (!root.get())
3374 {
3375 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003376 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3377 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003378 return 0;
3379 }
3380 else
3381 {
3382 list->Append(root);
3383 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003384 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3385 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003386 return 1;
3387 }
3388 }
3389 }
3390 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3391 {
3392 root = root->GetSyntheticBitFieldChild(index, index, true);
3393 if (!root.get())
3394 {
3395 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003396 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3397 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003398 return 0;
3399 }
3400 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3401 {
3402 list->Append(root);
3403 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003404 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3405 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003406 return 1;
3407 }
3408 }
3409 }
3410 else // we have a low and a high index
3411 {
3412 char *end = NULL;
3413 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3414 if (!end || end != separator_position) // if something weird is in our way return an error
3415 {
3416 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003417 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3418 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003419 return 0;
3420 }
3421 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3422 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3423 {
3424 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003425 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3426 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003427 return 0;
3428 }
3429 if (index_lower > index_higher) // swap indices if required
3430 {
3431 unsigned long temp = index_lower;
3432 index_lower = index_higher;
3433 index_higher = temp;
3434 }
Enrico Granata622be232014-10-21 20:52:14 +00003435 if (root_clang_type_info.Test(eTypeIsScalar)) // expansion only works for scalars
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003436 {
3437 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3438 if (!root.get())
3439 {
3440 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003441 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3442 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003443 return 0;
3444 }
3445 else
3446 {
3447 list->Append(root);
3448 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003449 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3450 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003451 return 1;
3452 }
3453 }
Enrico Granata622be232014-10-21 20:52:14 +00003454 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 +00003455 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granata622be232014-10-21 20:52:14 +00003456 pointee_clang_type_info.Test(eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003457 {
3458 Error error;
3459 root = root->Dereference(error);
3460 if (error.Fail() || !root.get())
3461 {
3462 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003463 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3464 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003465 return 0;
3466 }
3467 else
3468 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003469 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003470 continue;
3471 }
3472 }
3473 else
3474 {
Johnny Chen44805302011-07-19 19:48:13 +00003475 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003476 index <= index_higher; index++)
3477 {
3478 ValueObjectSP child =
3479 root->GetChildAtIndex(index, true);
3480 list->Append(child);
3481 }
3482 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003483 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3484 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003485 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3486 }
3487 }
3488 break;
3489 }
3490 default: // some non-[ separator, or something entirely wrong, is in the way
3491 {
3492 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003493 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3494 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003495 return 0;
3496 break;
3497 }
3498 }
3499 }
3500}
3501
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003502void
3503ValueObject::LogValueObject (Log *log)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003504{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003505 if (log)
3506 return LogValueObject (log, DumpValueObjectOptions::DefaultOptions());
Greg Clayton1d3afba2010-10-05 00:00:42 +00003507}
3508
Enrico Granata0c489f52012-03-01 04:24:26 +00003509void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003510ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003511{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003512 if (log)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003513 {
3514 StreamString s;
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003515 Dump (s, options);
Greg Claytonf830dbb2012-03-22 18:15:37 +00003516 if (s.GetSize())
3517 log->PutCString(s.GetData());
3518 }
3519}
3520
3521void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003522ValueObject::Dump (Stream &s)
Enrico Granata0c489f52012-03-01 04:24:26 +00003523{
3524
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003525 ValueObjectPrinter printer(this,&s,DumpValueObjectOptions::DefaultOptions());
3526 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003527}
3528
3529void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003530ValueObject::Dump (Stream &s,
3531 const DumpValueObjectOptions& options)
Enrico Granata0c489f52012-03-01 04:24:26 +00003532{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003533 ValueObjectPrinter printer(this,&s,options);
3534 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003535}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003536
3537ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003538ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003539{
3540 ValueObjectSP valobj_sp;
3541
Enrico Granatac3e320a2011-08-02 17:27:39 +00003542 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003543 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003544 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003545
3546 DataExtractor data;
3547 data.SetByteOrder (m_data.GetByteOrder());
3548 data.SetAddressByteSize(m_data.GetAddressByteSize());
3549
Enrico Granata9f1e2042012-04-24 22:15:37 +00003550 if (IsBitfield())
3551 {
3552 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
Greg Clayton57ee3062013-07-11 22:46:58 +00003553 m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Enrico Granata9f1e2042012-04-24 22:15:37 +00003554 }
3555 else
Greg Clayton57ee3062013-07-11 22:46:58 +00003556 m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003557
3558 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003559 GetClangType(),
3560 name,
3561 data,
3562 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003563 }
Jim Ingham6035b672011-03-31 00:19:25 +00003564
3565 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003566 {
Greg Claytoneeb15652013-12-10 23:16:40 +00003567 ExecutionContext exe_ctx (GetExecutionContextRef());
3568 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(), m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003569 }
3570 return valobj_sp;
3571}
3572
Enrico Granata538a88a2014-10-09 18:24:30 +00003573ValueObjectSP
3574ValueObject::GetQualifiedRepresentationIfAvailable (lldb::DynamicValueType dynValue,
3575 bool synthValue)
3576{
3577 ValueObjectSP result_sp(GetSP());
3578
3579 switch (dynValue)
3580 {
3581 case lldb::eDynamicCanRunTarget:
3582 case lldb::eDynamicDontRunTarget:
3583 {
3584 if (!result_sp->IsDynamic())
3585 {
3586 if (result_sp->GetDynamicValue(dynValue))
3587 result_sp = result_sp->GetDynamicValue(dynValue);
3588 }
3589 }
3590 break;
3591 case lldb::eNoDynamicValues:
Enrico Granata538a88a2014-10-09 18:24:30 +00003592 {
3593 if (result_sp->IsDynamic())
3594 {
3595 if (result_sp->GetStaticValue())
3596 result_sp = result_sp->GetStaticValue();
3597 }
3598 }
3599 break;
3600 }
3601
3602 if (synthValue)
3603 {
3604 if (!result_sp->IsSynthetic())
3605 {
3606 if (result_sp->GetSyntheticValue())
3607 result_sp = result_sp->GetSyntheticValue();
3608 }
3609 }
3610 else
3611 {
3612 if (result_sp->IsSynthetic())
3613 {
3614 if (result_sp->GetNonSyntheticValue())
3615 result_sp = result_sp->GetNonSyntheticValue();
3616 }
3617 }
3618
3619 return result_sp;
3620}
3621
Greg Clayton759e7442014-07-19 00:12:57 +00003622lldb::addr_t
3623ValueObject::GetCPPVTableAddress (AddressType &address_type)
3624{
3625 ClangASTType pointee_type;
3626 ClangASTType this_type(GetClangType());
3627 uint32_t type_info = this_type.GetTypeInfo(&pointee_type);
3628 if (type_info)
3629 {
3630 bool ptr_or_ref = false;
Enrico Granata622be232014-10-21 20:52:14 +00003631 if (type_info & (eTypeIsPointer | eTypeIsReference))
Greg Clayton759e7442014-07-19 00:12:57 +00003632 {
3633 ptr_or_ref = true;
3634 type_info = pointee_type.GetTypeInfo();
3635 }
3636
Enrico Granata622be232014-10-21 20:52:14 +00003637 const uint32_t cpp_class = eTypeIsClass | eTypeIsCPlusPlus;
Greg Clayton759e7442014-07-19 00:12:57 +00003638 if ((type_info & cpp_class) == cpp_class)
3639 {
3640 if (ptr_or_ref)
3641 {
3642 address_type = GetAddressTypeOfChildren();
3643 return GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
3644 }
3645 else
3646 return GetAddressOf (false, &address_type);
3647 }
3648 }
3649
3650 address_type = eAddressTypeInvalid;
3651 return LLDB_INVALID_ADDRESS;
3652}
3653
Greg Claytonafacd142011-09-02 01:15:17 +00003654ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003655ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003656{
Jim Ingham58b59f92011-04-22 23:53:53 +00003657 if (m_deref_valobj)
3658 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003659
Greg Clayton54979cd2010-12-15 05:08:08 +00003660 const bool is_pointer_type = IsPointerType();
3661 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003662 {
3663 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003664 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003665
3666 std::string child_name_str;
3667 uint32_t child_byte_size = 0;
3668 int32_t child_byte_offset = 0;
3669 uint32_t child_bitfield_bit_size = 0;
3670 uint32_t child_bitfield_bit_offset = 0;
3671 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003672 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003673 const bool transparent_pointers = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00003674 ClangASTType clang_type = GetClangType();
3675 ClangASTType child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003676
Greg Claytoncc4d0142012-02-17 07:49:44 +00003677 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003678
Greg Clayton57ee3062013-07-11 22:46:58 +00003679 child_clang_type = clang_type.GetChildClangTypeAtIndex (&exe_ctx,
Greg Clayton57ee3062013-07-11 22:46:58 +00003680 0,
3681 transparent_pointers,
3682 omit_empty_base_classes,
3683 ignore_array_bounds,
3684 child_name_str,
3685 child_byte_size,
3686 child_byte_offset,
3687 child_bitfield_bit_size,
3688 child_bitfield_bit_offset,
3689 child_is_base_class,
Greg Clayton759e7442014-07-19 00:12:57 +00003690 child_is_deref_of_parent,
3691 this);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003692 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003693 {
3694 ConstString child_name;
3695 if (!child_name_str.empty())
3696 child_name.SetCString (child_name_str.c_str());
3697
Jim Ingham58b59f92011-04-22 23:53:53 +00003698 m_deref_valobj = new ValueObjectChild (*this,
Jim Ingham58b59f92011-04-22 23:53:53 +00003699 child_clang_type,
3700 child_name,
3701 child_byte_size,
3702 child_byte_offset,
3703 child_bitfield_bit_size,
3704 child_bitfield_bit_offset,
3705 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003706 child_is_deref_of_parent,
3707 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003708 }
3709 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003710
Jim Ingham58b59f92011-04-22 23:53:53 +00003711 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003712 {
3713 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003714 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003715 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003716 else
3717 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003718 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003719 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003720
3721 if (is_pointer_type)
3722 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3723 else
3724 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003725 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003726 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003727}
3728
Greg Claytonafacd142011-09-02 01:15:17 +00003729ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003730ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003731{
Jim Ingham78a685a2011-04-16 00:01:13 +00003732 if (m_addr_of_valobj_sp)
3733 return m_addr_of_valobj_sp;
3734
Greg Claytone0d378b2011-03-24 21:19:54 +00003735 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003736 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003737 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003738 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003739 if (addr != LLDB_INVALID_ADDRESS)
3740 {
3741 switch (address_type)
3742 {
3743 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003744 {
3745 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003746 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003747 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3748 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003749 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003750
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003751 case eAddressTypeFile:
3752 case eAddressTypeLoad:
3753 case eAddressTypeHost:
3754 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003755 ClangASTType clang_type = GetClangType();
3756 if (clang_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003757 {
3758 std::string name (1, '&');
3759 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003760 ExecutionContext exe_ctx (GetExecutionContextRef());
3761 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00003762 clang_type.GetPointerType(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003763 ConstString (name.c_str()),
3764 addr,
3765 eAddressTypeInvalid,
3766 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003767 }
3768 }
3769 break;
3770 }
3771 }
Sean Callananed185ab2013-04-19 19:47:32 +00003772 else
3773 {
3774 StreamString expr_path_strm;
3775 GetExpressionPath(expr_path_strm, true);
3776 error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str());
3777 }
3778
Jim Ingham78a685a2011-04-16 00:01:13 +00003779 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003780}
3781
Greg Clayton9a142cf2012-02-03 05:34:10 +00003782ValueObjectSP
3783ValueObject::Cast (const ClangASTType &clang_ast_type)
3784{
Greg Clayton81e871e2012-02-04 02:27:34 +00003785 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003786}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003787
Greg Claytonafacd142011-09-02 01:15:17 +00003788ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003789ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3790{
Greg Claytonafacd142011-09-02 01:15:17 +00003791 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003792 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003793 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003794
3795 if (ptr_value != LLDB_INVALID_ADDRESS)
3796 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003797 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003798 ExecutionContext exe_ctx (GetExecutionContextRef());
3799 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003800 name,
3801 ptr_addr,
3802 clang_ast_type);
3803 }
3804 return valobj_sp;
3805}
3806
Greg Claytonafacd142011-09-02 01:15:17 +00003807ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003808ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3809{
Greg Claytonafacd142011-09-02 01:15:17 +00003810 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003811 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003812 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003813
3814 if (ptr_value != LLDB_INVALID_ADDRESS)
3815 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003816 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003817 ExecutionContext exe_ctx (GetExecutionContextRef());
3818 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003819 name,
3820 ptr_addr,
3821 type_sp);
3822 }
3823 return valobj_sp;
3824}
3825
Jim Ingham6035b672011-03-31 00:19:25 +00003826ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003827 m_mod_id(),
3828 m_exe_ctx_ref(),
3829 m_needs_update (true),
3830 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003831{
3832}
3833
3834ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003835 m_mod_id(),
3836 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003837 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003838 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003839{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003840 ExecutionContext exe_ctx(exe_scope);
3841 TargetSP target_sp (exe_ctx.GetTargetSP());
3842 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003843 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003844 m_exe_ctx_ref.SetTargetSP (target_sp);
3845 ProcessSP process_sp (exe_ctx.GetProcessSP());
3846 if (!process_sp)
3847 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003848
Greg Claytoncc4d0142012-02-17 07:49:44 +00003849 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003850 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003851 m_mod_id = process_sp->GetModID();
3852 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003853
Greg Claytoncc4d0142012-02-17 07:49:44 +00003854 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003855
Greg Claytoncc4d0142012-02-17 07:49:44 +00003856 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003857 {
3858 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003859 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003860 }
Jim Ingham6035b672011-03-31 00:19:25 +00003861
Greg Claytoncc4d0142012-02-17 07:49:44 +00003862 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003863 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003864 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003865
Jason Molendab57e4a12013-11-04 09:33:30 +00003866 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003867 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003868 {
3869 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003870 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003871 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003872 if (frame_sp)
3873 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003874 }
3875 }
3876 }
Jim Ingham6035b672011-03-31 00:19:25 +00003877}
3878
3879ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003880 m_mod_id(),
3881 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3882 m_needs_update (true),
3883 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003884{
3885}
3886
3887ValueObject::EvaluationPoint::~EvaluationPoint ()
3888{
3889}
3890
Jim Ingham6035b672011-03-31 00:19:25 +00003891// This function checks the EvaluationPoint against the current process state. If the current
3892// state matches the evaluation point, or the evaluation point is already invalid, then we return
3893// false, meaning "no change". If the current state is different, we update our state, and return
3894// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3895// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003896// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003897
3898bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003899ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003900{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003901
3902 // 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 +00003903 const bool thread_and_frame_only_if_stopped = true;
3904 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock(thread_and_frame_only_if_stopped));
Jim Ingham73ca05a2011-12-17 01:35:57 +00003905
Greg Claytoncc4d0142012-02-17 07:49:44 +00003906 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003907 return false;
3908
Jim Ingham6035b672011-03-31 00:19:25 +00003909 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003910 Process *process = exe_ctx.GetProcessPtr();
3911 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003912 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003913
Jim Ingham6035b672011-03-31 00:19:25 +00003914 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003915 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003916
Jim Ingham78a685a2011-04-16 00:01:13 +00003917 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3918 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003919 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003920 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003921
Greg Clayton23f59502012-07-17 03:23:13 +00003922 bool changed = false;
3923 const bool was_valid = m_mod_id.IsValid();
3924 if (was_valid)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003925 {
3926 if (m_mod_id == current_mod_id)
3927 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003928 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003929 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003930 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003931 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003932 else
3933 {
3934 m_mod_id = current_mod_id;
3935 m_needs_update = true;
3936 changed = true;
3937 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003938 }
Jim Ingham6035b672011-03-31 00:19:25 +00003939
Jim Ingham73ca05a2011-12-17 01:35:57 +00003940 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3941 // That way we'll be sure to return a valid exe_scope.
3942 // 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 +00003943
Greg Claytoncc4d0142012-02-17 07:49:44 +00003944 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003945 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003946 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3947 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003948 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003949 if (m_exe_ctx_ref.HasFrameRef())
3950 {
Jason Molendab57e4a12013-11-04 09:33:30 +00003951 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003952 if (!frame_sp)
3953 {
3954 // We used to have a frame, but now it is gone
3955 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003956 changed = was_valid;
Greg Claytoncc4d0142012-02-17 07:49:44 +00003957 }
3958 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003959 }
Jim Ingham6035b672011-03-31 00:19:25 +00003960 else
3961 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003962 // We used to have a thread, but now it is gone
3963 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003964 changed = was_valid;
Jim Ingham6035b672011-03-31 00:19:25 +00003965 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003966
Jim Ingham6035b672011-03-31 00:19:25 +00003967 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003968 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003969}
3970
Jim Ingham61be0902011-05-02 18:13:59 +00003971void
3972ValueObject::EvaluationPoint::SetUpdated ()
3973{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003974 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3975 if (process_sp)
3976 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003977 m_first_update = false;
3978 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003979}
3980
3981
Enrico Granataf2bbf712011-07-15 02:26:42 +00003982
3983void
Enrico Granata86cc9822012-03-19 22:58:49 +00003984ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003985{
Enrico Granata86cc9822012-03-19 22:58:49 +00003986 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3987 m_value_str.clear();
3988
3989 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3990 m_location_str.clear();
3991
3992 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
Enrico Granata86cc9822012-03-19 22:58:49 +00003993 m_summary_str.clear();
Enrico Granata86cc9822012-03-19 22:58:49 +00003994
3995 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3996 m_object_desc_str.clear();
3997
3998 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3999 {
4000 if (m_synthetic_value)
4001 m_synthetic_value = NULL;
4002 }
Enrico Granata744794a2014-09-05 21:46:22 +00004003
4004 if ((clear_mask & eClearUserVisibleDataItemsValidator) == eClearUserVisibleDataItemsValidator)
4005 m_validation_result.reset();
Johnny Chen44805302011-07-19 19:48:13 +00004006}
Enrico Granata9128ee22011-09-06 19:20:51 +00004007
4008SymbolContextScope *
4009ValueObject::GetSymbolContextScope()
4010{
4011 if (m_parent)
4012 {
4013 if (!m_parent->IsPointerOrReferenceType())
4014 return m_parent->GetSymbolContextScope();
4015 }
4016 return NULL;
4017}
Enrico Granatab2698cd2012-09-13 18:27:09 +00004018
4019lldb::ValueObjectSP
4020ValueObject::CreateValueObjectFromExpression (const char* name,
4021 const char* expression,
4022 const ExecutionContext& exe_ctx)
4023{
4024 lldb::ValueObjectSP retval_sp;
4025 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
4026 if (!target_sp)
4027 return retval_sp;
4028 if (!expression || !*expression)
4029 return retval_sp;
4030 target_sp->EvaluateExpression (expression,
4031 exe_ctx.GetFrameSP().get(),
4032 retval_sp);
4033 if (retval_sp && name && *name)
4034 retval_sp->SetName(ConstString(name));
4035 return retval_sp;
4036}
4037
4038lldb::ValueObjectSP
4039ValueObject::CreateValueObjectFromAddress (const char* name,
4040 uint64_t address,
4041 const ExecutionContext& exe_ctx,
4042 ClangASTType type)
4043{
Greg Clayton57ee3062013-07-11 22:46:58 +00004044 if (type)
Enrico Granatab2698cd2012-09-13 18:27:09 +00004045 {
Greg Clayton57ee3062013-07-11 22:46:58 +00004046 ClangASTType pointer_type(type.GetPointerType());
4047 if (pointer_type)
4048 {
4049 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
4050 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4051 pointer_type,
4052 ConstString(name),
4053 buffer,
4054 lldb::endian::InlHostByteOrder(),
4055 exe_ctx.GetAddressByteSize()));
4056 if (ptr_result_valobj_sp)
4057 {
4058 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
4059 Error err;
4060 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
4061 if (ptr_result_valobj_sp && name && *name)
4062 ptr_result_valobj_sp->SetName(ConstString(name));
4063 }
4064 return ptr_result_valobj_sp;
4065 }
Enrico Granatab2698cd2012-09-13 18:27:09 +00004066 }
Greg Clayton57ee3062013-07-11 22:46:58 +00004067 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00004068}
4069
4070lldb::ValueObjectSP
4071ValueObject::CreateValueObjectFromData (const char* name,
Enrico Granata7ca1c762014-03-31 23:02:25 +00004072 const DataExtractor& data,
Enrico Granatab2698cd2012-09-13 18:27:09 +00004073 const ExecutionContext& exe_ctx,
4074 ClangASTType type)
4075{
4076 lldb::ValueObjectSP new_value_sp;
4077 new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00004078 type,
Enrico Granatab2698cd2012-09-13 18:27:09 +00004079 ConstString(name),
4080 data,
4081 LLDB_INVALID_ADDRESS);
4082 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
4083 if (new_value_sp && name && *name)
4084 new_value_sp->SetName(ConstString(name));
4085 return new_value_sp;
4086}
Enrico Granata4873e522013-04-11 22:48:58 +00004087
4088ModuleSP
4089ValueObject::GetModule ()
4090{
4091 ValueObject* root(GetRoot());
4092 if (root != this)
4093 return root->GetModule();
4094 return lldb::ModuleSP();
4095}
4096
4097ValueObject*
4098ValueObject::GetRoot ()
4099{
4100 if (m_root)
4101 return m_root;
4102 ValueObject* parent = m_parent;
4103 if (!parent)
4104 return (m_root = this);
4105 while (parent->m_parent)
4106 {
4107 if (parent->m_root)
4108 return (m_root = parent->m_root);
4109 parent = parent->m_parent;
4110 }
4111 return (m_root = parent);
4112}
4113
4114AddressType
4115ValueObject::GetAddressTypeOfChildren()
4116{
4117 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
4118 {
4119 ValueObject* root(GetRoot());
4120 if (root != this)
4121 return root->GetAddressTypeOfChildren();
4122 }
4123 return m_address_type_of_ptr_or_ref_children;
4124}
4125
4126lldb::DynamicValueType
4127ValueObject::GetDynamicValueType ()
4128{
4129 ValueObject* with_dv_info = this;
4130 while (with_dv_info)
4131 {
4132 if (with_dv_info->HasDynamicValueTypeInfo())
4133 return with_dv_info->GetDynamicValueTypeImpl();
4134 with_dv_info = with_dv_info->m_parent;
4135 }
4136 return lldb::eNoDynamicValues;
4137}
Enrico Granata39d51412013-05-31 17:43:40 +00004138
Enrico Granata4873e522013-04-11 22:48:58 +00004139lldb::Format
4140ValueObject::GetFormat () const
4141{
4142 const ValueObject* with_fmt_info = this;
4143 while (with_fmt_info)
4144 {
4145 if (with_fmt_info->m_format != lldb::eFormatDefault)
4146 return with_fmt_info->m_format;
4147 with_fmt_info = with_fmt_info->m_parent;
4148 }
4149 return m_format;
4150}
Enrico Granatad07cfd32014-10-08 18:27:36 +00004151
Enrico Granatac1247f52014-11-06 21:23:20 +00004152lldb::LanguageType
4153ValueObject::GetPreferredDisplayLanguage ()
4154{
4155 lldb::LanguageType type = lldb::eLanguageTypeUnknown;
4156 if (GetRoot())
4157 {
4158 if (GetRoot() == this)
4159 {
4160 if (StackFrameSP frame_sp = GetFrameSP())
4161 {
4162 const SymbolContext& sc(frame_sp->GetSymbolContext(eSymbolContextCompUnit));
4163 if (CompileUnit* cu = sc.comp_unit)
4164 type = cu->GetLanguage();
4165 }
4166 }
4167 else
4168 {
4169 type = GetRoot()->GetPreferredDisplayLanguage();
4170 }
4171 }
4172 return type;
4173}
4174
Enrico Granatad07cfd32014-10-08 18:27:36 +00004175bool
4176ValueObject::CanProvideValue ()
4177{
4178 return (false == GetClangType().IsAggregateType());
4179}