blob: d39d21a2a0bf559eefb485a27a7ef10af56d4943 [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 Granata4d93b8c2013-09-30 19:11:51 +000037#include "lldb/DataFormatters/ValueObjectPrinter.h"
Enrico Granata5548cb52013-01-28 23:47:25 +000038
Greg Clayton7fb56d02011-02-01 01:31:41 +000039#include "lldb/Host/Endian.h"
40
Enrico Granata61a80ba2011-08-12 16:42:31 +000041#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granataf2bbf712011-07-15 02:26:42 +000042#include "lldb/Interpreter/ScriptInterpreterPython.h"
43
Greg Claytone1a916a2010-07-21 22:12:05 +000044#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Symbol/ClangASTContext.h"
46#include "lldb/Symbol/Type.h"
47
Jim Ingham53c47f12010-09-10 23:12:17 +000048#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000049#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000050#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051#include "lldb/Target/Process.h"
52#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000053#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56using namespace lldb;
57using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000058using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059
Greg Claytonafacd142011-09-02 01:15:17 +000060static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061
62//----------------------------------------------------------------------
63// ValueObject constructor
64//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000065ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000067 m_parent (&parent),
Enrico Granata4873e522013-04-11 22:48:58 +000068 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000069 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070 m_name (),
71 m_data (),
72 m_value (),
73 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000074 m_value_str (),
75 m_old_value_str (),
76 m_location_str (),
77 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000078 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000079 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000080 m_children (),
81 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000082 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000083 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000084 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000085 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +000086 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000087 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +000088 m_type_summary_sp(),
89 m_type_format_sp(),
90 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +000091 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000092 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
Greg Clayton288bdf92010-09-02 02:59:18 +000093 m_value_is_valid (false),
94 m_value_did_change (false),
95 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000096 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +000097 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +000098 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +000099 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000100 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000101 m_is_getting_summary(false),
102 m_did_calculate_complete_objc_class_type(false)
Jim Ingham6035b672011-03-31 00:19:25 +0000103{
Jim Ingham58b59f92011-04-22 23:53:53 +0000104 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000105}
106
107//----------------------------------------------------------------------
108// ValueObject constructor
109//----------------------------------------------------------------------
Enrico Granata9128ee22011-09-06 19:20:51 +0000110ValueObject::ValueObject (ExecutionContextScope *exe_scope,
111 AddressType child_ptr_or_ref_addr_type) :
Jim Ingham6035b672011-03-31 00:19:25 +0000112 UserID (++g_value_obj_uid), // Unique identifier for every value object
113 m_parent (NULL),
Enrico Granata4873e522013-04-11 22:48:58 +0000114 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000115 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000116 m_name (),
117 m_data (),
118 m_value (),
119 m_error (),
120 m_value_str (),
121 m_old_value_str (),
122 m_location_str (),
123 m_summary_str (),
124 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000125 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000126 m_children (),
127 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000128 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000129 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000130 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000131 m_format (eFormatDefault),
Enrico Granatab294fd22013-05-31 19:18:19 +0000132 m_last_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000133 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +0000134 m_type_summary_sp(),
135 m_type_format_sp(),
136 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +0000137 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000138 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
Jim Ingham6035b672011-03-31 00:19:25 +0000139 m_value_is_valid (false),
140 m_value_did_change (false),
141 m_children_count_valid (false),
142 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000143 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000144 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000145 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000146 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000147 m_is_getting_summary(false),
148 m_did_calculate_complete_objc_class_type(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149{
Jim Ingham58b59f92011-04-22 23:53:53 +0000150 m_manager = new ValueObjectManager();
151 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000152}
153
154//----------------------------------------------------------------------
155// Destructor
156//----------------------------------------------------------------------
157ValueObject::~ValueObject ()
158{
159}
160
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000162ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163{
Enrico Granata4becb372011-06-29 22:27:15 +0000164
Enrico Granata9128ee22011-09-06 19:20:51 +0000165 bool did_change_formats = false;
166
Enrico Granata0a3958e2011-07-02 00:25:22 +0000167 if (update_format)
Enrico Granata5548cb52013-01-28 23:47:25 +0000168 did_change_formats = UpdateFormatsIfNeeded();
Enrico Granata4becb372011-06-29 22:27:15 +0000169
Greg Claytonb71f3842010-10-05 03:13:51 +0000170 // If this is a constant value, then our success is predicated on whether
171 // we have an error or not
172 if (GetIsConstant())
Enrico Granata9128ee22011-09-06 19:20:51 +0000173 {
Enrico Granatab1c6c482013-10-09 00:33:55 +0000174 // if you are constant, things might still have changed behind your back
175 // (e.g. you are a frozen object and things have changed deeper than you cared to freeze-dry yourself)
176 // in this case, your value has not changed, but "computed" entries might have, so you might now have
177 // a different summary, or a different object description. clear these so we will recompute them
Enrico Granata9128ee22011-09-06 19:20:51 +0000178 if (update_format && !did_change_formats)
Enrico Granatab1c6c482013-10-09 00:33:55 +0000179 ClearUserVisibleData(eClearUserVisibleDataItemsSummary | eClearUserVisibleDataItemsDescription);
Greg Claytonb71f3842010-10-05 03:13:51 +0000180 return m_error.Success();
Enrico Granata9128ee22011-09-06 19:20:51 +0000181 }
Greg Claytonb71f3842010-10-05 03:13:51 +0000182
Jim Ingham6035b672011-03-31 00:19:25 +0000183 bool first_update = m_update_point.IsFirstEvaluation();
184
185 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 {
Jim Ingham6035b672011-03-31 00:19:25 +0000187 m_update_point.SetUpdated();
188
189 // Save the old value using swap to avoid a string copy which
190 // also will clear our m_value_str
191 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192 {
Jim Ingham6035b672011-03-31 00:19:25 +0000193 m_old_value_valid = false;
194 }
195 else
196 {
197 m_old_value_valid = true;
198 m_old_value_str.swap (m_value_str);
Enrico Granata86cc9822012-03-19 22:58:49 +0000199 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham6035b672011-03-31 00:19:25 +0000200 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Enrico Granataf2bbf712011-07-15 02:26:42 +0000202 ClearUserVisibleData();
203
Greg Claytonefbc7d22012-03-09 04:23:44 +0000204 if (IsInScope())
Jim Ingham6035b672011-03-31 00:19:25 +0000205 {
Greg Claytonefbc7d22012-03-09 04:23:44 +0000206 const bool value_was_valid = GetValueIsValid();
207 SetValueDidChange (false);
208
209 m_error.Clear();
210
211 // Call the pure virtual function to update the value
212 bool success = UpdateValue ();
213
214 SetValueIsValid (success);
215
216 if (first_update)
217 SetValueDidChange (false);
218 else if (!m_value_did_change && success == false)
219 {
220 // The value wasn't gotten successfully, so we mark this
221 // as changed if the value used to be valid and now isn't
222 SetValueDidChange (value_was_valid);
223 }
224 }
225 else
226 {
227 m_error.SetErrorString("out of scope");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228 }
229 }
230 return m_error.Success();
231}
232
Enrico Granata9128ee22011-09-06 19:20:51 +0000233bool
Enrico Granata5548cb52013-01-28 23:47:25 +0000234ValueObject::UpdateFormatsIfNeeded()
Enrico Granata4becb372011-06-29 22:27:15 +0000235{
Greg Clayton5160ce52013-03-27 23:08:40 +0000236 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata6f3533f2011-07-29 19:53:35 +0000237 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +0000238 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d",
Enrico Granata6f3533f2011-07-29 19:53:35 +0000239 GetName().GetCString(),
Enrico Granatad2284832012-10-17 22:23:56 +0000240 this,
Enrico Granata4becb372011-06-29 22:27:15 +0000241 m_last_format_mgr_revision,
Enrico Granata85933ed2011-08-18 16:38:26 +0000242 DataVisualization::GetCurrentRevision());
Enrico Granata9128ee22011-09-06 19:20:51 +0000243
244 bool any_change = false;
245
Enrico Granata5548cb52013-01-28 23:47:25 +0000246 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()))
Enrico Granata4becb372011-06-29 22:27:15 +0000247 {
Enrico Granata852cc952013-10-08 19:03:22 +0000248 SetValueFormat(DataVisualization::GetFormat (*this, eNoDynamicValues));
Enrico Granata5548cb52013-01-28 23:47:25 +0000249 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000250#ifndef LLDB_DISABLE_PYTHON
Enrico Granata5548cb52013-01-28 23:47:25 +0000251 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000252#endif
Enrico Granata1490c6f2011-07-19 02:34:21 +0000253
Enrico Granata85933ed2011-08-18 16:38:26 +0000254 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
Enrico Granata855cd902011-09-06 22:59:55 +0000255
256 any_change = true;
Enrico Granata4becb372011-06-29 22:27:15 +0000257 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000258
259 return any_change;
260
Enrico Granata4becb372011-06-29 22:27:15 +0000261}
262
Jim Ingham16e0c682011-08-12 23:34:31 +0000263void
264ValueObject::SetNeedsUpdate ()
265{
266 m_update_point.SetNeedsUpdate();
267 // We have to clear the value string here so ConstResult children will notice if their values are
268 // changed by hand (i.e. with SetValueAsCString).
Enrico Granata86cc9822012-03-19 22:58:49 +0000269 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000270}
271
Enrico Granata13ac0e22012-10-17 19:03:34 +0000272void
Enrico Granatae3e91512012-10-22 18:18:36 +0000273ValueObject::ClearDynamicTypeInformation ()
Enrico Granata13ac0e22012-10-17 19:03:34 +0000274{
Enrico Granata38c54632013-10-30 00:04:29 +0000275 m_children_count_valid = false;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000276 m_did_calculate_complete_objc_class_type = false;
Enrico Granatae3e91512012-10-22 18:18:36 +0000277 m_last_format_mgr_revision = 0;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000278 m_override_type = ClangASTType();
Enrico Granatae3e91512012-10-22 18:18:36 +0000279 SetValueFormat(lldb::TypeFormatImplSP());
280 SetSummaryFormat(lldb::TypeSummaryImplSP());
281 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000282}
283
Sean Callanan72772842012-02-22 23:57:45 +0000284ClangASTType
285ValueObject::MaybeCalculateCompleteType ()
286{
Greg Clayton57ee3062013-07-11 22:46:58 +0000287 ClangASTType clang_type(GetClangTypeImpl());
Sean Callanan356e17c2012-03-30 02:04:38 +0000288
Sean Callanan72772842012-02-22 23:57:45 +0000289 if (m_did_calculate_complete_objc_class_type)
290 {
291 if (m_override_type.IsValid())
292 return m_override_type;
293 else
Greg Clayton57ee3062013-07-11 22:46:58 +0000294 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000295 }
296
Greg Clayton57ee3062013-07-11 22:46:58 +0000297 ClangASTType class_type;
298 bool is_pointer_type = false;
Sean Callanan72772842012-02-22 23:57:45 +0000299
Greg Clayton57ee3062013-07-11 22:46:58 +0000300 if (clang_type.IsObjCObjectPointerType(&class_type))
Sean Callanan72772842012-02-22 23:57:45 +0000301 {
302 is_pointer_type = true;
303 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000304 else if (clang_type.IsObjCObjectOrInterfaceType())
Sean Callanan72772842012-02-22 23:57:45 +0000305 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000306 class_type = clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000307 }
308 else
309 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000310 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000311 }
312
313 m_did_calculate_complete_objc_class_type = true;
314
Greg Clayton57ee3062013-07-11 22:46:58 +0000315 if (class_type)
Sean Callanan72772842012-02-22 23:57:45 +0000316 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000317 ConstString class_name (class_type.GetConstTypeName());
Sean Callanan72772842012-02-22 23:57:45 +0000318
Greg Clayton57ee3062013-07-11 22:46:58 +0000319 if (class_name)
320 {
321 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
322
323 if (process_sp)
324 {
325 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
326
327 if (objc_language_runtime)
328 {
329 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name);
330
331 if (complete_objc_class_type_sp)
332 {
333 ClangASTType complete_class(complete_objc_class_type_sp->GetClangFullType());
334
335 if (complete_class.GetCompleteType())
336 {
337 if (is_pointer_type)
338 {
339 m_override_type = complete_class.GetPointerType();
340 }
341 else
342 {
343 m_override_type = complete_class;
344 }
345
346 if (m_override_type.IsValid())
347 return m_override_type;
348 }
349 }
350 }
351 }
352 }
Sean Callanan72772842012-02-22 23:57:45 +0000353 }
Greg Clayton57ee3062013-07-11 22:46:58 +0000354 return clang_type;
Sean Callanan72772842012-02-22 23:57:45 +0000355}
356
Greg Clayton57ee3062013-07-11 22:46:58 +0000357ClangASTType
Sean Callanan72772842012-02-22 23:57:45 +0000358ValueObject::GetClangType ()
359{
Greg Clayton57ee3062013-07-11 22:46:58 +0000360 return MaybeCalculateCompleteType();
Sean Callanan72772842012-02-22 23:57:45 +0000361}
362
Enrico Granatadc4db5a2013-10-29 00:28:35 +0000363TypeImpl
364ValueObject::GetTypeImpl ()
365{
366 return TypeImpl(GetClangType());
367}
368
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000369DataExtractor &
370ValueObject::GetDataExtractor ()
371{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000372 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 return m_data;
374}
375
376const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000377ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000378{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000379 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 return m_error;
381}
382
383const ConstString &
384ValueObject::GetName() const
385{
386 return m_name;
387}
388
389const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000390ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391{
Enrico Granata82fabf82013-04-30 20:45:04 +0000392 return GetLocationAsCStringImpl(m_value,
393 m_data);
394}
395
396const char *
397ValueObject::GetLocationAsCStringImpl (const Value& value,
398 const DataExtractor& data)
399{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000400 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 {
402 if (m_location_str.empty())
403 {
404 StreamString sstr;
Enrico Granata82fabf82013-04-30 20:45:04 +0000405
406 Value::ValueType value_type = value.GetValueType();
407
408 switch (value_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000411 case Value::eValueTypeVector:
Enrico Granata82fabf82013-04-30 20:45:04 +0000412 if (value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000414 RegisterInfo *reg_info = value.GetRegisterInfo();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 if (reg_info)
416 {
417 if (reg_info->name)
418 m_location_str = reg_info->name;
419 else if (reg_info->alt_name)
420 m_location_str = reg_info->alt_name;
Enrico Granata82fabf82013-04-30 20:45:04 +0000421 if (m_location_str.empty())
422 m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 }
424 }
Enrico Granata82fabf82013-04-30 20:45:04 +0000425 if (m_location_str.empty())
426 m_location_str = (value_type == Value::eValueTypeVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 break;
428
429 case Value::eValueTypeLoadAddress:
430 case Value::eValueTypeFileAddress:
431 case Value::eValueTypeHostAddress:
432 {
Enrico Granata82fabf82013-04-30 20:45:04 +0000433 uint32_t addr_nibble_size = data.GetAddressByteSize() * 2;
434 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 m_location_str.swap(sstr.GetString());
436 }
437 break;
438 }
439 }
440 }
441 return m_location_str.c_str();
442}
443
444Value &
445ValueObject::GetValue()
446{
447 return m_value;
448}
449
450const Value &
451ValueObject::GetValue() const
452{
453 return m_value;
454}
455
456bool
Jim Ingham6035b672011-03-31 00:19:25 +0000457ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000458{
Enrico Granata6fd87d52011-08-04 01:41:02 +0000459 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
460 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000461 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Ingham16e0c682011-08-12 23:34:31 +0000462 Value tmp_value(m_value);
Greg Clayton57ee3062013-07-11 22:46:58 +0000463 scalar = tmp_value.ResolveValue(&exe_ctx);
Greg Claytondcad5022011-12-29 01:26:56 +0000464 if (scalar.IsValid())
465 {
466 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
467 if (bitfield_bit_size)
468 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset());
469 return true;
470 }
Enrico Granata6fd87d52011-08-04 01:41:02 +0000471 }
Greg Claytondcad5022011-12-29 01:26:56 +0000472 return false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000473}
474
475bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000476ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477{
Greg Clayton288bdf92010-09-02 02:59:18 +0000478 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479}
480
481
482void
483ValueObject::SetValueIsValid (bool b)
484{
Greg Clayton288bdf92010-09-02 02:59:18 +0000485 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486}
487
488bool
Jim Ingham6035b672011-03-31 00:19:25 +0000489ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490{
Jim Ingham6035b672011-03-31 00:19:25 +0000491 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000492 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493}
494
495void
496ValueObject::SetValueDidChange (bool value_changed)
497{
Greg Clayton288bdf92010-09-02 02:59:18 +0000498 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499}
500
501ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000502ValueObject::GetChildAtIndex (size_t idx, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000503{
504 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000505 // We may need to update our value if we are dynamic
506 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000507 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000508 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000509 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000510 // Check if we have already made the child value object?
Enrico Granata9d60f602012-03-09 03:09:58 +0000511 if (can_create && !m_children.HasChildAtIndex(idx))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000513 // No we haven't created the child at this index, so lets have our
514 // subclass do it and cache the result for quick future access.
Enrico Granata9d60f602012-03-09 03:09:58 +0000515 m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0));
Jim Ingham78a685a2011-04-16 00:01:13 +0000516 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000517
Enrico Granata9d60f602012-03-09 03:09:58 +0000518 ValueObject* child = m_children.GetChildAtIndex(idx);
519 if (child != NULL)
520 return child->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 }
522 return child_sp;
523}
524
Enrico Granata3309d882013-01-12 01:00:22 +0000525ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000526ValueObject::GetChildAtIndexPath (const std::initializer_list<size_t>& idxs,
527 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000528{
529 if (idxs.size() == 0)
530 return GetSP();
531 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000532 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000533 {
534 root = root->GetChildAtIndex(idx, true);
535 if (!root)
536 {
537 if (index_of_error)
538 *index_of_error = idx;
539 return root;
540 }
541 }
542 return root;
543}
544
545ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000546ValueObject::GetChildAtIndexPath (const std::initializer_list< std::pair<size_t, bool> >& idxs,
547 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000548{
549 if (idxs.size() == 0)
550 return GetSP();
551 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000552 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000553 {
554 root = root->GetChildAtIndex(idx.first, idx.second);
555 if (!root)
556 {
557 if (index_of_error)
558 *index_of_error = idx.first;
559 return root;
560 }
561 }
562 return root;
563}
564
565lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000566ValueObject::GetChildAtIndexPath (const std::vector<size_t> &idxs,
567 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000568{
569 if (idxs.size() == 0)
570 return GetSP();
571 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000572 for (size_t idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000573 {
574 root = root->GetChildAtIndex(idx, true);
575 if (!root)
576 {
577 if (index_of_error)
578 *index_of_error = idx;
579 return root;
580 }
581 }
582 return root;
583}
584
585lldb::ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +0000586ValueObject::GetChildAtIndexPath (const std::vector< std::pair<size_t, bool> > &idxs,
587 size_t* index_of_error)
Enrico Granata3309d882013-01-12 01:00:22 +0000588{
589 if (idxs.size() == 0)
590 return GetSP();
591 ValueObjectSP root(GetSP());
Greg Claytonc7bece562013-01-25 18:06:21 +0000592 for (std::pair<size_t, bool> idx : idxs)
Enrico Granata3309d882013-01-12 01:00:22 +0000593 {
594 root = root->GetChildAtIndex(idx.first, idx.second);
595 if (!root)
596 {
597 if (index_of_error)
598 *index_of_error = idx.first;
599 return root;
600 }
601 }
602 return root;
603}
604
Enrico Granatae2e220a2013-09-12 00:48:47 +0000605lldb::ValueObjectSP
606ValueObject::GetChildAtNamePath (const std::initializer_list<ConstString> &names,
607 ConstString* name_of_error)
608{
609 if (names.size() == 0)
610 return GetSP();
611 ValueObjectSP root(GetSP());
612 for (ConstString name : names)
613 {
614 root = root->GetChildMemberWithName(name, true);
615 if (!root)
616 {
617 if (name_of_error)
618 *name_of_error = name;
619 return root;
620 }
621 }
622 return root;
623}
624
625lldb::ValueObjectSP
626ValueObject::GetChildAtNamePath (const std::vector<ConstString> &names,
627 ConstString* name_of_error)
628{
629 if (names.size() == 0)
630 return GetSP();
631 ValueObjectSP root(GetSP());
632 for (ConstString name : names)
633 {
634 root = root->GetChildMemberWithName(name, true);
635 if (!root)
636 {
637 if (name_of_error)
638 *name_of_error = name;
639 return root;
640 }
641 }
642 return root;
643}
644
645lldb::ValueObjectSP
646ValueObject::GetChildAtNamePath (const std::initializer_list< std::pair<ConstString, bool> > &names,
647 ConstString* name_of_error)
648{
649 if (names.size() == 0)
650 return GetSP();
651 ValueObjectSP root(GetSP());
652 for (std::pair<ConstString, bool> name : names)
653 {
654 root = root->GetChildMemberWithName(name.first, name.second);
655 if (!root)
656 {
657 if (name_of_error)
658 *name_of_error = name.first;
659 return root;
660 }
661 }
662 return root;
663}
664
665lldb::ValueObjectSP
666ValueObject::GetChildAtNamePath (const std::vector< std::pair<ConstString, bool> > &names,
667 ConstString* name_of_error)
668{
669 if (names.size() == 0)
670 return GetSP();
671 ValueObjectSP root(GetSP());
672 for (std::pair<ConstString, bool> name : names)
673 {
674 root = root->GetChildMemberWithName(name.first, name.second);
675 if (!root)
676 {
677 if (name_of_error)
678 *name_of_error = name.first;
679 return root;
680 }
681 }
682 return root;
683}
684
Greg Claytonc7bece562013-01-25 18:06:21 +0000685size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686ValueObject::GetIndexOfChildWithName (const ConstString &name)
687{
688 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000689 return GetClangType().GetIndexOfChildWithName (name.GetCString(), omit_empty_base_classes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000690}
691
692ValueObjectSP
693ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
694{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000695 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696 // classes (which really aren't part of the expression path), so we
697 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000698 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000699
Greg Claytondea8cb42011-06-29 22:09:02 +0000700 // We may need to update our value if we are dynamic
701 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000702 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000703
704 std::vector<uint32_t> child_indexes;
Greg Claytondea8cb42011-06-29 22:09:02 +0000705 bool omit_empty_base_classes = true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000706 const size_t num_child_indexes = GetClangType().GetIndexOfChildMemberWithName (name.GetCString(),
707 omit_empty_base_classes,
708 child_indexes);
Greg Claytondea8cb42011-06-29 22:09:02 +0000709 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000711 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
712 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
713
714 child_sp = GetChildAtIndex(*pos, can_create);
715 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000717 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000718 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000719 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
720 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000721 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000722 else
723 {
724 child_sp.reset();
725 }
726
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 }
728 }
729 return child_sp;
730}
731
732
Greg Claytonc7bece562013-01-25 18:06:21 +0000733size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734ValueObject::GetNumChildren ()
735{
Enrico Granatac5bc4122012-03-27 02:35:13 +0000736 UpdateValueIfNeeded();
Greg Clayton288bdf92010-09-02 02:59:18 +0000737 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000738 {
739 SetNumChildren (CalculateNumChildren());
740 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000741 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742}
Greg Clayton4a792072012-10-23 01:50:10 +0000743
744bool
745ValueObject::MightHaveChildren()
746{
Enrico Granatadb8142b2012-10-23 02:07:54 +0000747 bool has_children = false;
Greg Clayton2452ab72013-02-08 22:02:02 +0000748 const uint32_t type_info = GetTypeInfo();
749 if (type_info)
Greg Clayton4a792072012-10-23 01:50:10 +0000750 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000751 if (type_info & (ClangASTType::eTypeHasChildren |
752 ClangASTType::eTypeIsPointer |
753 ClangASTType::eTypeIsReference))
Greg Clayton4a792072012-10-23 01:50:10 +0000754 has_children = true;
755 }
756 else
757 {
758 has_children = GetNumChildren () > 0;
759 }
760 return has_children;
761}
762
763// Should only be called by ValueObject::GetNumChildren()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000764void
Greg Claytonc7bece562013-01-25 18:06:21 +0000765ValueObject::SetNumChildren (size_t num_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766{
Greg Clayton288bdf92010-09-02 02:59:18 +0000767 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000768 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769}
770
771void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772ValueObject::SetName (const ConstString &name)
773{
774 m_name = name;
775}
776
Jim Ingham58b59f92011-04-22 23:53:53 +0000777ValueObject *
Greg Claytonc7bece562013-01-25 18:06:21 +0000778ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000779{
Jim Ingham2eec4872011-05-07 00:10:58 +0000780 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000781
Greg Claytondea8cb42011-06-29 22:09:02 +0000782 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000783 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000784 std::string child_name_str;
785 uint32_t child_byte_size = 0;
786 int32_t child_byte_offset = 0;
787 uint32_t child_bitfield_bit_size = 0;
788 uint32_t child_bitfield_bit_offset = 0;
789 bool child_is_base_class = false;
790 bool child_is_deref_of_parent = false;
791
792 const bool transparent_pointers = synthetic_array_member == false;
Greg Clayton57ee3062013-07-11 22:46:58 +0000793 ClangASTType child_clang_type;
Greg Claytondea8cb42011-06-29 22:09:02 +0000794
Greg Claytoncc4d0142012-02-17 07:49:44 +0000795 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000796
Greg Clayton57ee3062013-07-11 22:46:58 +0000797 child_clang_type = GetClangType().GetChildClangTypeAtIndex (&exe_ctx,
798 GetName().GetCString(),
799 idx,
800 transparent_pointers,
801 omit_empty_base_classes,
802 ignore_array_bounds,
803 child_name_str,
804 child_byte_size,
805 child_byte_offset,
806 child_bitfield_bit_size,
807 child_bitfield_bit_offset,
808 child_is_base_class,
809 child_is_deref_of_parent);
Greg Clayton4ef877f2012-12-06 02:33:54 +0000810 if (child_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000811 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000812 if (synthetic_index)
813 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814
Greg Claytondea8cb42011-06-29 22:09:02 +0000815 ConstString child_name;
816 if (!child_name_str.empty())
817 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818
Greg Claytondea8cb42011-06-29 22:09:02 +0000819 valobj = new ValueObjectChild (*this,
Greg Claytondea8cb42011-06-29 22:09:02 +0000820 child_clang_type,
821 child_name,
822 child_byte_size,
823 child_byte_offset,
824 child_bitfield_bit_size,
825 child_bitfield_bit_offset,
826 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000827 child_is_deref_of_parent,
828 eAddressTypeInvalid);
829 //if (valobj)
830 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
831 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000832
Jim Ingham58b59f92011-04-22 23:53:53 +0000833 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834}
835
Enrico Granata0c489f52012-03-01 04:24:26 +0000836bool
837ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
838 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000839{
Enrico Granata0c489f52012-03-01 04:24:26 +0000840 destination.clear();
841
842 // ideally we would like to bail out if passing NULL, but if we do so
843 // we end up not providing the summary for function pointers anymore
844 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
845 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000846
847 m_is_getting_summary = true;
Enrico Granataf18c03e2012-04-04 17:34:10 +0000848
849 // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other
850 // information that we might care to see in a crash log. might be useful in very specific situations though.
851 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s",
852 GetTypeName().GetCString(),
853 GetName().GetCString(),
854 summary_ptr->GetDescription().c_str());*/
855
Enrico Granata0c489f52012-03-01 04:24:26 +0000856 if (UpdateValueIfNeeded (false))
857 {
858 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000860 if (HasSyntheticValue())
861 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
862 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000863 }
864 else
865 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000866 ClangASTType clang_type = GetClangType();
Enrico Granata0c489f52012-03-01 04:24:26 +0000867
868 // Do some default printout for function pointers
869 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000870 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000871 if (clang_type.IsFunctionPointerType ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872 {
Greg Clayton57ee3062013-07-11 22:46:58 +0000873 StreamString sstr;
Enrico Granata0c489f52012-03-01 04:24:26 +0000874 AddressType func_ptr_address_type = eAddressTypeInvalid;
875 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
876 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000877 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000878 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000879 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000880 case eAddressTypeInvalid:
881 case eAddressTypeFile:
882 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000883
Greg Claytoncc4d0142012-02-17 07:49:44 +0000884 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000885 {
886 ExecutionContext exe_ctx (GetExecutionContextRef());
887
888 Address so_addr;
889 Target *target = exe_ctx.GetTargetPtr();
890 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000891 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000892 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000893 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000894 so_addr.Dump (&sstr,
895 exe_ctx.GetBestExecutionContextScope(),
896 Address::DumpStyleResolvedDescription,
897 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000898 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000899 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000900 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000901 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000902
Greg Claytoncc4d0142012-02-17 07:49:44 +0000903 case eAddressTypeHost:
904 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000905 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000906 }
907 if (sstr.GetSize() > 0)
908 {
909 destination.assign (1, '(');
910 destination.append (sstr.GetData(), sstr.GetSize());
911 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000912 }
913 }
914 }
915 }
916 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000917 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000918 return !destination.empty();
919}
920
921const char *
922ValueObject::GetSummaryAsCString ()
923{
924 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
925 {
926 GetSummaryAsCString(GetSummaryFormat().get(),
927 m_summary_str);
928 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929 if (m_summary_str.empty())
930 return NULL;
931 return m_summary_str.c_str();
932}
933
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000934bool
935ValueObject::IsCStringContainer(bool check_pointer)
936{
Greg Clayton57ee3062013-07-11 22:46:58 +0000937 ClangASTType pointee_or_element_clang_type;
938 const Flags type_flags (GetTypeInfo (&pointee_or_element_clang_type));
939 bool is_char_arr_ptr (type_flags.AnySet (ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) &&
940 pointee_or_element_clang_type.IsCharType ());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000941 if (!is_char_arr_ptr)
942 return false;
943 if (!check_pointer)
944 return true;
Greg Clayton57ee3062013-07-11 22:46:58 +0000945 if (type_flags.Test(ClangASTType::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000946 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000947 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000948 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000949 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000950 return (cstr_address != LLDB_INVALID_ADDRESS);
951}
952
Enrico Granata9128ee22011-09-06 19:20:51 +0000953size_t
954ValueObject::GetPointeeData (DataExtractor& data,
955 uint32_t item_idx,
956 uint32_t item_count)
957{
Greg Clayton57ee3062013-07-11 22:46:58 +0000958 ClangASTType pointee_or_element_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000959 const uint32_t type_info = GetTypeInfo (&pointee_or_element_clang_type);
Greg Clayton57ee3062013-07-11 22:46:58 +0000960 const bool is_pointer_type = type_info & ClangASTType::eTypeIsPointer;
961 const bool is_array_type = type_info & ClangASTType::eTypeIsArray;
Greg Clayton2452ab72013-02-08 22:02:02 +0000962 if (!(is_pointer_type || is_array_type))
Enrico Granata9128ee22011-09-06 19:20:51 +0000963 return 0;
964
965 if (item_count == 0)
966 return 0;
967
Greg Clayton57ee3062013-07-11 22:46:58 +0000968 const uint64_t item_type_size = pointee_or_element_clang_type.GetByteSize();
Enrico Granata9128ee22011-09-06 19:20:51 +0000969 const uint64_t bytes = item_count * item_type_size;
Enrico Granata9128ee22011-09-06 19:20:51 +0000970 const uint64_t offset = item_idx * item_type_size;
971
972 if (item_idx == 0 && item_count == 1) // simply a deref
973 {
Greg Clayton2452ab72013-02-08 22:02:02 +0000974 if (is_pointer_type)
Enrico Granata9128ee22011-09-06 19:20:51 +0000975 {
976 Error error;
977 ValueObjectSP pointee_sp = Dereference(error);
978 if (error.Fail() || pointee_sp.get() == NULL)
979 return 0;
980 return pointee_sp->GetDataExtractor().Copy(data);
981 }
982 else
983 {
984 ValueObjectSP child_sp = GetChildAtIndex(0, true);
985 if (child_sp.get() == NULL)
986 return 0;
987 return child_sp->GetDataExtractor().Copy(data);
988 }
989 return true;
990 }
991 else /* (items > 1) */
992 {
993 Error error;
994 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
995 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
996
997 AddressType addr_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000998 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000999
Enrico Granata9128ee22011-09-06 19:20:51 +00001000 switch (addr_type)
1001 {
1002 case eAddressTypeFile:
1003 {
Greg Claytone72dfb32012-02-24 01:59:29 +00001004 ModuleSP module_sp (GetModule());
1005 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +00001006 {
Enrico Granata9c2efe32012-08-07 01:49:34 +00001007 addr = addr + offset;
Enrico Granata9128ee22011-09-06 19:20:51 +00001008 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +00001009 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001010 ExecutionContext exe_ctx (GetExecutionContextRef());
1011 Target* target = exe_ctx.GetTargetPtr();
1012 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +00001013 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001014 heap_buf_ptr->SetByteSize(bytes);
1015 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
1016 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +00001017 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001018 data.SetData(data_sp);
1019 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +00001020 }
1021 }
1022 }
1023 }
1024 break;
1025 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +00001026 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001027 ExecutionContext exe_ctx (GetExecutionContextRef());
1028 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +00001029 if (process)
1030 {
1031 heap_buf_ptr->SetByteSize(bytes);
1032 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
Enrico Granata5e1480c2013-10-30 17:52:44 +00001033 if (error.Success() || bytes_read > 0)
Enrico Granata9128ee22011-09-06 19:20:51 +00001034 {
1035 data.SetData(data_sp);
1036 return bytes_read;
1037 }
1038 }
1039 }
1040 break;
1041 case eAddressTypeHost:
1042 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001043 const uint64_t max_bytes = GetClangType().GetByteSize();
Greg Clayton2452ab72013-02-08 22:02:02 +00001044 if (max_bytes > offset)
1045 {
1046 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
1047 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
1048 data.SetData(data_sp);
1049 return bytes_read;
1050 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001051 }
1052 break;
1053 case eAddressTypeInvalid:
Enrico Granata9128ee22011-09-06 19:20:51 +00001054 break;
1055 }
1056 }
1057 return 0;
1058}
1059
Greg Claytonfaac1112013-03-14 18:31:44 +00001060uint64_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001061ValueObject::GetData (DataExtractor& data)
1062{
1063 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001064 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Clayton57ee3062013-07-11 22:46:58 +00001065 Error error = m_value.GetValueAsData(&exe_ctx, data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +00001066 if (error.Fail())
Sean Callananed185ab2013-04-19 19:47:32 +00001067 {
1068 if (m_data.GetByteSize())
1069 {
1070 data = m_data;
1071 return data.GetByteSize();
1072 }
1073 else
1074 {
1075 return 0;
1076 }
1077 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001078 data.SetAddressByteSize(m_data.GetAddressByteSize());
1079 data.SetByteOrder(m_data.GetByteOrder());
1080 return data.GetByteSize();
1081}
1082
Sean Callanan389823e2013-04-13 01:21:23 +00001083bool
1084ValueObject::SetData (DataExtractor &data, Error &error)
1085{
1086 error.Clear();
1087 // Make sure our value is up to date first so that our location and location
1088 // type is valid.
1089 if (!UpdateValueIfNeeded(false))
1090 {
1091 error.SetErrorString("unable to read value");
1092 return false;
1093 }
1094
1095 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001096 const Encoding encoding = GetClangType().GetEncoding(count);
Sean Callanan389823e2013-04-13 01:21:23 +00001097
1098 const size_t byte_size = GetByteSize();
1099
1100 Value::ValueType value_type = m_value.GetValueType();
1101
1102 switch (value_type)
1103 {
1104 case Value::eValueTypeScalar:
1105 {
1106 Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1107
1108 if (!set_error.Success())
1109 {
1110 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1111 return false;
1112 }
1113 }
1114 break;
1115 case Value::eValueTypeLoadAddress:
1116 {
1117 // If it is a load address, then the scalar value is the storage location
1118 // of the data, and we have to shove this value down to that load location.
1119 ExecutionContext exe_ctx (GetExecutionContextRef());
1120 Process *process = exe_ctx.GetProcessPtr();
1121 if (process)
1122 {
1123 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1124 size_t bytes_written = process->WriteMemory(target_addr,
1125 data.GetDataStart(),
1126 byte_size,
1127 error);
1128 if (!error.Success())
1129 return false;
1130 if (bytes_written != byte_size)
1131 {
1132 error.SetErrorString("unable to write value to memory");
1133 return false;
1134 }
1135 }
1136 }
1137 break;
1138 case Value::eValueTypeHostAddress:
1139 {
1140 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1141 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1142 m_data.SetData(buffer_sp, 0);
1143 data.CopyByteOrderedData (0,
1144 byte_size,
1145 const_cast<uint8_t *>(m_data.GetDataStart()),
1146 byte_size,
1147 m_data.GetByteOrder());
1148 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1149 }
1150 break;
1151 case Value::eValueTypeFileAddress:
1152 case Value::eValueTypeVector:
1153 break;
1154 }
1155
1156 // If we have reached this point, then we have successfully changed the value.
1157 SetNeedsUpdate();
1158 return true;
1159}
1160
Enrico Granata9128ee22011-09-06 19:20:51 +00001161// will compute strlen(str), but without consuming more than
1162// maxlen bytes out of str (this serves the purpose of reading
1163// chunks of a string without having to worry about
1164// missing NULL terminators in the chunk)
1165// of course, if strlen(str) > maxlen, the function will return
1166// maxlen_value (which should be != maxlen, because that allows you
1167// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1168static uint32_t
1169strlen_or_inf (const char* str,
1170 uint32_t maxlen,
1171 uint32_t maxlen_value)
1172{
1173 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +00001174 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +00001175 {
Greg Clayton8dd5c172011-10-05 22:19:51 +00001176 while(*str)
1177 {
1178 len++;str++;
Greg Clayton2452ab72013-02-08 22:02:02 +00001179 if (len >= maxlen)
Greg Clayton8dd5c172011-10-05 22:19:51 +00001180 return maxlen_value;
1181 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001182 }
1183 return len;
1184}
1185
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001186size_t
Greg Claytoncc4d0142012-02-17 07:49:44 +00001187ValueObject::ReadPointedString (Stream& s,
1188 Error& error,
1189 uint32_t max_length,
1190 bool honor_array,
1191 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001192{
Greg Claytoncc4d0142012-02-17 07:49:44 +00001193 ExecutionContext exe_ctx (GetExecutionContextRef());
1194 Target* target = exe_ctx.GetTargetPtr();
1195
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001196 if (!target)
1197 {
1198 s << "<no target to read from>";
1199 error.SetErrorString("no target to read from");
1200 return 0;
1201 }
1202
1203 if (max_length == 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001204 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001205
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001206 size_t bytes_read = 0;
1207 size_t total_bytes_read = 0;
1208
Greg Clayton57ee3062013-07-11 22:46:58 +00001209 ClangASTType clang_type = GetClangType();
1210 ClangASTType elem_or_pointee_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001211 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
Greg Clayton57ee3062013-07-11 22:46:58 +00001212 if (type_flags.AnySet (ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer) &&
1213 elem_or_pointee_clang_type.IsCharType ())
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001214 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001215 addr_t cstr_address = LLDB_INVALID_ADDRESS;
1216 AddressType cstr_address_type = eAddressTypeInvalid;
1217
1218 size_t cstr_len = 0;
1219 bool capped_data = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00001220 if (type_flags.Test (ClangASTType::eTypeIsArray))
Greg Claytoncc4d0142012-02-17 07:49:44 +00001221 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001222 // We have an array
Greg Clayton57ee3062013-07-11 22:46:58 +00001223 uint64_t array_size = 0;
1224 if (clang_type.IsArrayType(NULL, &array_size, NULL))
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001225 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001226 cstr_len = array_size;
1227 if (cstr_len > max_length)
1228 {
1229 capped_data = true;
1230 cstr_len = max_length;
1231 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001232 }
1233 cstr_address = GetAddressOf (true, &cstr_address_type);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001234 }
1235 else
1236 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001237 // We have a pointer
1238 cstr_address = GetPointerValue (&cstr_address_type);
1239 }
1240
1241 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1242 {
1243 s << "<invalid address>";
1244 error.SetErrorString("invalid address");
1245 return 0;
1246 }
1247
1248 Address cstr_so_addr (cstr_address);
1249 DataExtractor data;
1250 if (cstr_len > 0 && honor_array)
1251 {
1252 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1253 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1254 GetPointeeData(data, 0, cstr_len);
1255
1256 if ((bytes_read = data.GetByteSize()) > 0)
1257 {
1258 total_bytes_read = bytes_read;
1259 s << '"';
1260 data.Dump (&s,
1261 0, // Start offset in "data"
1262 item_format,
1263 1, // Size of item (1 byte for a char!)
1264 bytes_read, // How many bytes to print?
1265 UINT32_MAX, // num per line
1266 LLDB_INVALID_ADDRESS,// base address
1267 0, // bitfield bit size
1268 0); // bitfield bit offset
1269 if (capped_data)
1270 s << "...";
1271 s << '"';
1272 }
1273 }
1274 else
1275 {
1276 cstr_len = max_length;
1277 const size_t k_max_buf_size = 64;
1278
1279 size_t offset = 0;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001280
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001281 int cstr_len_displayed = -1;
1282 bool capped_cstr = false;
1283 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1284 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1285 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001286 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001287 total_bytes_read += bytes_read;
1288 const char *cstr = data.PeekCStr(0);
1289 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1290 if (len > k_max_buf_size)
1291 len = k_max_buf_size;
1292 if (cstr && cstr_len_displayed < 0)
1293 s << '"';
1294
1295 if (cstr_len_displayed < 0)
1296 cstr_len_displayed = len;
1297
1298 if (len == 0)
1299 break;
1300 cstr_len_displayed += len;
1301 if (len > bytes_read)
1302 len = bytes_read;
1303 if (len > cstr_len)
1304 len = cstr_len;
1305
1306 data.Dump (&s,
1307 0, // Start offset in "data"
1308 item_format,
1309 1, // Size of item (1 byte for a char!)
1310 len, // How many bytes to print?
1311 UINT32_MAX, // num per line
1312 LLDB_INVALID_ADDRESS,// base address
1313 0, // bitfield bit size
1314 0); // bitfield bit offset
1315
1316 if (len < k_max_buf_size)
1317 break;
1318
1319 if (len >= cstr_len)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001320 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001321 capped_cstr = true;
1322 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001323 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001324
1325 cstr_len -= len;
1326 offset += len;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001327 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001328
1329 if (cstr_len_displayed >= 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001330 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001331 s << '"';
1332 if (capped_cstr)
1333 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001334 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001335 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001336 }
1337 else
1338 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001339 error.SetErrorString("not a string object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001340 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001341 }
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001342 return total_bytes_read;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001343}
1344
Jim Ingham53c47f12010-09-10 23:12:17 +00001345const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001346ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001347{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001348
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001349 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001350 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001351
1352 if (!m_object_desc_str.empty())
1353 return m_object_desc_str.c_str();
1354
Greg Claytoncc4d0142012-02-17 07:49:44 +00001355 ExecutionContext exe_ctx (GetExecutionContextRef());
1356 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001357 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001358 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001359
Jim Ingham53c47f12010-09-10 23:12:17 +00001360 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001361
Greg Claytonafacd142011-09-02 01:15:17 +00001362 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001363 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1364
Jim Inghama2cf2632010-12-23 02:29:54 +00001365 if (runtime == NULL)
1366 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001367 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Greg Clayton57ee3062013-07-11 22:46:58 +00001368 ClangASTType clang_type = GetClangType();
1369 if (clang_type)
Jim Inghama2cf2632010-12-23 02:29:54 +00001370 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001371 bool is_signed;
Greg Clayton57ee3062013-07-11 22:46:58 +00001372 if (clang_type.IsIntegerType (is_signed) || clang_type.IsPointerType ())
Jim Inghamb7603bb2011-03-18 00:05:18 +00001373 {
Greg Claytonafacd142011-09-02 01:15:17 +00001374 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001375 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001376 }
1377 }
1378
Jim Ingham8d543de2011-03-31 23:01:21 +00001379 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001380 {
1381 m_object_desc_str.append (s.GetData());
1382 }
Sean Callanan672ad942010-10-23 00:18:49 +00001383
1384 if (m_object_desc_str.empty())
1385 return NULL;
1386 else
1387 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001388}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001389
Enrico Granata0c489f52012-03-01 04:24:26 +00001390bool
1391ValueObject::GetValueAsCString (lldb::Format format,
1392 std::string& destination)
1393{
Greg Clayton57ee3062013-07-11 22:46:58 +00001394 if (GetClangType().IsAggregateType () == false && UpdateValueIfNeeded(false))
Enrico Granata0c489f52012-03-01 04:24:26 +00001395 {
1396 const Value::ContextType context_type = m_value.GetContextType();
1397
Greg Clayton57ee3062013-07-11 22:46:58 +00001398 if (context_type == Value::eContextTypeRegisterInfo)
Enrico Granata0c489f52012-03-01 04:24:26 +00001399 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001400 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1401 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001402 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001403 ExecutionContext exe_ctx (GetExecutionContextRef());
1404
1405 StreamString reg_sstr;
1406 m_data.Dump (&reg_sstr,
1407 0,
1408 format,
1409 reg_info->byte_size,
1410 1,
1411 UINT32_MAX,
1412 LLDB_INVALID_ADDRESS,
1413 0,
1414 0,
1415 exe_ctx.GetBestExecutionContextScope());
1416 destination.swap(reg_sstr.GetString());
1417 }
1418 }
1419 else
1420 {
1421 ClangASTType clang_type = GetClangType ();
1422 if (clang_type)
1423 {
1424 // put custom bytes to display in this DataExtractor to override the default value logic
1425 lldb_private::DataExtractor special_format_data;
1426 if (format == eFormatCString)
Enrico Granata0c489f52012-03-01 04:24:26 +00001427 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001428 Flags type_flags(clang_type.GetTypeInfo(NULL));
1429 if (type_flags.Test(ClangASTType::eTypeIsPointer) && !type_flags.Test(ClangASTType::eTypeIsObjC))
Enrico Granata852cce72013-03-23 01:12:38 +00001430 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001431 // if we are dumping a pointer as a c-string, get the pointee data as a string
1432 TargetSP target_sp(GetTargetSP());
1433 if (target_sp)
Enrico Granata852cce72013-03-23 01:12:38 +00001434 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001435 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
1436 Error error;
1437 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
1438 Address address(GetPointerValue());
1439 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
1440 special_format_data.SetData(buffer_sp);
Enrico Granata852cce72013-03-23 01:12:38 +00001441 }
1442 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001443 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001444
Greg Clayton57ee3062013-07-11 22:46:58 +00001445 StreamString sstr;
1446 ExecutionContext exe_ctx (GetExecutionContextRef());
1447 clang_type.DumpTypeValue (&sstr, // The stream to use for display
1448 format, // Format to display this type with
1449 special_format_data.GetByteSize() ?
1450 special_format_data: m_data, // Data to extract from
1451 0, // Byte offset into "m_data"
1452 GetByteSize(), // Byte size of item in "m_data"
1453 GetBitfieldBitSize(), // Bitfield bit size
1454 GetBitfieldBitOffset(), // Bitfield bit offset
1455 exe_ctx.GetBestExecutionContextScope());
1456 // Don't set the m_error to anything here otherwise
1457 // we won't be able to re-format as anything else. The
1458 // code for ClangASTType::DumpTypeValue() should always
1459 // return something, even if that something contains
1460 // an error messsage. "m_error" is used to detect errors
1461 // when reading the valid object, not for formatting errors.
1462 if (sstr.GetString().empty())
1463 destination.clear();
1464 else
1465 destination.swap(sstr.GetString());
Enrico Granata0c489f52012-03-01 04:24:26 +00001466 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001467 }
1468 return !destination.empty();
1469 }
1470 else
1471 return false;
1472}
1473
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001474const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001475ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476{
Enrico Granatab294fd22013-05-31 19:18:19 +00001477 if (UpdateValueIfNeeded(true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001478 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001479 lldb::Format my_format = GetFormat();
Enrico Granatac953a6a2012-12-11 02:17:22 +00001480 if (my_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001481 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001482 if (m_type_format_sp)
1483 my_format = m_type_format_sp->GetFormat();
1484 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001485 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001486 if (m_is_bitfield_for_scalar)
1487 my_format = eFormatUnsigned;
1488 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001489 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001490 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001491 {
1492 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1493 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001494 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001495 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001496 else
1497 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001498 my_format = GetClangType().GetFormat();
Enrico Granata0c489f52012-03-01 04:24:26 +00001499 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001500 }
1501 }
1502 }
Enrico Granatab294fd22013-05-31 19:18:19 +00001503 if (my_format != m_last_format || m_value_str.empty())
Enrico Granata297e69f2012-03-06 23:21:16 +00001504 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001505 m_last_format = my_format;
1506 if (GetValueAsCString(my_format, m_value_str))
Enrico Granata297e69f2012-03-06 23:21:16 +00001507 {
Enrico Granatab294fd22013-05-31 19:18:19 +00001508 if (!m_value_did_change && m_old_value_valid)
1509 {
1510 // The value was gotten successfully, so we consider the
1511 // value as changed if the value string differs
1512 SetValueDidChange (m_old_value_str != m_value_str);
1513 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001514 }
1515 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001516 }
1517 if (m_value_str.empty())
1518 return NULL;
1519 return m_value_str.c_str();
1520}
1521
Enrico Granatac3e320a2011-08-02 17:27:39 +00001522// if > 8bytes, 0 is returned. this method should mostly be used
1523// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001524uint64_t
Johnny Chen3f476c42012-06-05 19:37:43 +00001525ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001526{
1527 // If our byte size is zero this is an aggregate type that has children
Greg Clayton57ee3062013-07-11 22:46:58 +00001528 if (!GetClangType().IsAggregateType())
Enrico Granatac3e320a2011-08-02 17:27:39 +00001529 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001530 Scalar scalar;
1531 if (ResolveValue (scalar))
Johnny Chen3f476c42012-06-05 19:37:43 +00001532 {
1533 if (success)
1534 *success = true;
Enrico Granata48ea80f2012-10-24 20:24:39 +00001535 return scalar.ULongLong(fail_value);
Johnny Chen3f476c42012-06-05 19:37:43 +00001536 }
1537 // fallthrough, otherwise...
Enrico Granatac3e320a2011-08-02 17:27:39 +00001538 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001539
1540 if (success)
1541 *success = false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001542 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001543}
1544
Enrico Granatad7373f62013-10-31 18:57:50 +00001545int64_t
1546ValueObject::GetValueAsSigned (int64_t fail_value, bool *success)
1547{
1548 // If our byte size is zero this is an aggregate type that has children
1549 if (!GetClangType().IsAggregateType())
1550 {
1551 Scalar scalar;
1552 if (ResolveValue (scalar))
1553 {
1554 if (success)
1555 *success = true;
1556 return scalar.SLongLong(fail_value);
1557 }
1558 // fallthrough, otherwise...
1559 }
1560
1561 if (success)
1562 *success = false;
1563 return fail_value;
1564}
1565
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001566// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1567// this call up to date by returning true for your new special cases. We will eventually move
1568// to checking this call result before trying to display special cases
1569bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001570ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1571 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001572{
Greg Clayton57ee3062013-07-11 22:46:58 +00001573 Flags flags(GetTypeInfo());
1574 if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001575 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001576 {
1577 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001578 (custom_format == eFormatCString ||
1579 custom_format == eFormatCharArray ||
1580 custom_format == eFormatChar ||
1581 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001582 return true;
1583
Greg Clayton57ee3062013-07-11 22:46:58 +00001584 if (flags.Test(ClangASTType::eTypeIsArray))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001585 {
Greg Claytonafacd142011-09-02 01:15:17 +00001586 if ((custom_format == eFormatBytes) ||
1587 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001588 return true;
1589
Greg Claytonafacd142011-09-02 01:15:17 +00001590 if ((custom_format == eFormatVectorOfChar) ||
1591 (custom_format == eFormatVectorOfFloat32) ||
1592 (custom_format == eFormatVectorOfFloat64) ||
1593 (custom_format == eFormatVectorOfSInt16) ||
1594 (custom_format == eFormatVectorOfSInt32) ||
1595 (custom_format == eFormatVectorOfSInt64) ||
1596 (custom_format == eFormatVectorOfSInt8) ||
1597 (custom_format == eFormatVectorOfUInt128) ||
1598 (custom_format == eFormatVectorOfUInt16) ||
1599 (custom_format == eFormatVectorOfUInt32) ||
1600 (custom_format == eFormatVectorOfUInt64) ||
1601 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001602 return true;
1603 }
1604 }
1605 return false;
1606}
1607
Enrico Granata9fc19442011-07-06 02:13:41 +00001608bool
1609ValueObject::DumpPrintableRepresentation(Stream& s,
1610 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001611 Format custom_format,
Enrico Granata86cc9822012-03-19 22:58:49 +00001612 PrintableRepresentationSpecialCases special)
Enrico Granata9fc19442011-07-06 02:13:41 +00001613{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001614
Greg Clayton57ee3062013-07-11 22:46:58 +00001615 Flags flags(GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001616
Enrico Granata86cc9822012-03-19 22:58:49 +00001617 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1618 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1619
1620 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001621 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001622 if (flags.AnySet(ClangASTType::eTypeIsArray | ClangASTType::eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001623 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001624 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001625 // when being asked to get a printable display an array or pointer type directly,
1626 // try to "do the right thing"
1627
1628 if (IsCStringContainer(true) &&
1629 (custom_format == eFormatCString ||
1630 custom_format == eFormatCharArray ||
1631 custom_format == eFormatChar ||
1632 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001633 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001634 Error error;
1635 ReadPointedString(s,
1636 error,
1637 0,
1638 (custom_format == eFormatVectorOfChar) ||
1639 (custom_format == eFormatCharArray));
1640 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001641 }
1642
Enrico Granata86cc9822012-03-19 22:58:49 +00001643 if (custom_format == eFormatEnum)
1644 return false;
1645
1646 // this only works for arrays, because I have no way to know when
1647 // the pointed memory ends, and no special \0 end of data marker
Greg Clayton57ee3062013-07-11 22:46:58 +00001648 if (flags.Test(ClangASTType::eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001649 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001650 if ((custom_format == eFormatBytes) ||
1651 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001652 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001653 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001654
1655 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001656 for (size_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001657 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001658
1659 if (low)
1660 s << ',';
1661
1662 ValueObjectSP child = GetChildAtIndex(low,true);
1663 if (!child.get())
1664 {
1665 s << "<invalid child>";
1666 continue;
1667 }
1668 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1669 }
1670
1671 s << ']';
1672
1673 return true;
1674 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001675
Enrico Granata86cc9822012-03-19 22:58:49 +00001676 if ((custom_format == eFormatVectorOfChar) ||
1677 (custom_format == eFormatVectorOfFloat32) ||
1678 (custom_format == eFormatVectorOfFloat64) ||
1679 (custom_format == eFormatVectorOfSInt16) ||
1680 (custom_format == eFormatVectorOfSInt32) ||
1681 (custom_format == eFormatVectorOfSInt64) ||
1682 (custom_format == eFormatVectorOfSInt8) ||
1683 (custom_format == eFormatVectorOfUInt128) ||
1684 (custom_format == eFormatVectorOfUInt16) ||
1685 (custom_format == eFormatVectorOfUInt32) ||
1686 (custom_format == eFormatVectorOfUInt64) ||
1687 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1688 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001689 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001690
1691 Format format = FormatManager::GetSingleItemFormat(custom_format);
1692
1693 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001694 for (size_t low = 0; low < count; low++)
Enrico Granata86cc9822012-03-19 22:58:49 +00001695 {
1696
1697 if (low)
1698 s << ',';
1699
1700 ValueObjectSP child = GetChildAtIndex(low,true);
1701 if (!child.get())
1702 {
1703 s << "<invalid child>";
1704 continue;
1705 }
1706 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1707 }
1708
1709 s << ']';
1710
1711 return true;
1712 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001713 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001714
1715 if ((custom_format == eFormatBoolean) ||
1716 (custom_format == eFormatBinary) ||
1717 (custom_format == eFormatChar) ||
1718 (custom_format == eFormatCharPrintable) ||
1719 (custom_format == eFormatComplexFloat) ||
1720 (custom_format == eFormatDecimal) ||
1721 (custom_format == eFormatHex) ||
Enrico Granata7ec18e32012-08-09 19:33:34 +00001722 (custom_format == eFormatHexUppercase) ||
Enrico Granata86cc9822012-03-19 22:58:49 +00001723 (custom_format == eFormatFloat) ||
1724 (custom_format == eFormatOctal) ||
1725 (custom_format == eFormatOSType) ||
1726 (custom_format == eFormatUnicode16) ||
1727 (custom_format == eFormatUnicode32) ||
1728 (custom_format == eFormatUnsigned) ||
1729 (custom_format == eFormatPointer) ||
1730 (custom_format == eFormatComplexInteger) ||
1731 (custom_format == eFormatComplex) ||
1732 (custom_format == eFormatDefault)) // use the [] operator
1733 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001734 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001735 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001736
1737 if (only_special)
1738 return false;
1739
Enrico Granata86cc9822012-03-19 22:58:49 +00001740 bool var_success = false;
1741
1742 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001743 const char *cstr = NULL;
Enrico Granata2c75f112013-06-21 00:04:51 +00001744
1745 // this is a local stream that we are using to ensure that the data pointed to by cstr survives
1746 // long enough for us to copy it to its destination - it is necessary to have this temporary storage
1747 // area for cases where our desired output is not backed by some other longer-term storage
Greg Claytonc7bece562013-01-25 18:06:21 +00001748 StreamString strm;
Enrico Granata86cc9822012-03-19 22:58:49 +00001749
1750 if (custom_format != eFormatInvalid)
1751 SetFormat(custom_format);
1752
1753 switch(val_obj_display)
1754 {
1755 case eValueObjectRepresentationStyleValue:
Greg Claytonc7bece562013-01-25 18:06:21 +00001756 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001757 break;
1758
1759 case eValueObjectRepresentationStyleSummary:
Greg Claytonc7bece562013-01-25 18:06:21 +00001760 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001761 break;
1762
1763 case eValueObjectRepresentationStyleLanguageSpecific:
Greg Claytonc7bece562013-01-25 18:06:21 +00001764 cstr = GetObjectDescription();
Enrico Granata86cc9822012-03-19 22:58:49 +00001765 break;
1766
1767 case eValueObjectRepresentationStyleLocation:
Greg Claytonc7bece562013-01-25 18:06:21 +00001768 cstr = GetLocationAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001769 break;
1770
1771 case eValueObjectRepresentationStyleChildrenCount:
Greg Claytonc7bece562013-01-25 18:06:21 +00001772 strm.Printf("%zu", GetNumChildren());
1773 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001774 break;
1775
1776 case eValueObjectRepresentationStyleType:
Greg Claytonc7bece562013-01-25 18:06:21 +00001777 cstr = GetTypeName().AsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001778 break;
Enrico Granata2c75f112013-06-21 00:04:51 +00001779
1780 case eValueObjectRepresentationStyleName:
1781 cstr = GetName().AsCString();
1782 break;
1783
1784 case eValueObjectRepresentationStyleExpressionPath:
1785 GetExpressionPath(strm, false);
1786 cstr = strm.GetString().c_str();
1787 break;
Enrico Granata86cc9822012-03-19 22:58:49 +00001788 }
1789
Greg Claytonc7bece562013-01-25 18:06:21 +00001790 if (!cstr)
Enrico Granata86cc9822012-03-19 22:58:49 +00001791 {
1792 if (val_obj_display == eValueObjectRepresentationStyleValue)
Greg Claytonc7bece562013-01-25 18:06:21 +00001793 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001794 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1795 {
Greg Clayton57ee3062013-07-11 22:46:58 +00001796 if (GetClangType().IsAggregateType())
Enrico Granata86cc9822012-03-19 22:58:49 +00001797 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001798 strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1799 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001800 }
1801 else
Greg Claytonc7bece562013-01-25 18:06:21 +00001802 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001803 }
1804 }
1805
Greg Claytonc7bece562013-01-25 18:06:21 +00001806 if (cstr)
1807 s.PutCString(cstr);
Enrico Granata86cc9822012-03-19 22:58:49 +00001808 else
1809 {
1810 if (m_error.Fail())
1811 s.Printf("<%s>", m_error.AsCString());
1812 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1813 s.PutCString("<no summary available>");
1814 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1815 s.PutCString("<no value available>");
1816 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1817 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1818 else
1819 s.PutCString("<no printable representation>");
1820 }
1821
1822 // we should only return false here if we could not do *anything*
1823 // even if we have an error message as output, that's a success
1824 // from our callers' perspective, so return true
1825 var_success = true;
1826
1827 if (custom_format != eFormatInvalid)
1828 SetFormat(eFormatDefault);
1829 }
1830
Enrico Granataf4efecd2011-07-12 22:56:10 +00001831 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001832}
1833
Greg Clayton737b9322010-09-13 03:32:57 +00001834addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001835ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001836{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001837 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001838 return LLDB_INVALID_ADDRESS;
1839
Greg Clayton73b472d2010-10-27 03:32:59 +00001840 switch (m_value.GetValueType())
1841 {
1842 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001843 case Value::eValueTypeVector:
Greg Clayton73b472d2010-10-27 03:32:59 +00001844 if (scalar_is_load_address)
1845 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001846 if(address_type)
1847 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001848 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1849 }
1850 break;
1851
1852 case Value::eValueTypeLoadAddress:
1853 case Value::eValueTypeFileAddress:
1854 case Value::eValueTypeHostAddress:
1855 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001856 if(address_type)
1857 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001858 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1859 }
1860 break;
1861 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001862 if (address_type)
1863 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001864 return LLDB_INVALID_ADDRESS;
1865}
1866
1867addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001868ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001869{
Greg Claytonafacd142011-09-02 01:15:17 +00001870 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001871 if(address_type)
1872 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001873
Enrico Granatac3e320a2011-08-02 17:27:39 +00001874 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001875 return address;
1876
Greg Clayton73b472d2010-10-27 03:32:59 +00001877 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001878 {
1879 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001880 case Value::eValueTypeVector:
Enrico Granata9128ee22011-09-06 19:20:51 +00001881 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001882 break;
1883
Enrico Granata9128ee22011-09-06 19:20:51 +00001884 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001885 case Value::eValueTypeLoadAddress:
1886 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001887 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001888 lldb::offset_t data_offset = 0;
Greg Clayton737b9322010-09-13 03:32:57 +00001889 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001890 }
1891 break;
1892 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001893
Enrico Granata9128ee22011-09-06 19:20:51 +00001894 if (address_type)
1895 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001896
Greg Clayton737b9322010-09-13 03:32:57 +00001897 return address;
1898}
1899
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001900bool
Enrico Granata07a4ac22012-05-08 21:25:06 +00001901ValueObject::SetValueFromCString (const char *value_str, Error& error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001902{
Enrico Granata07a4ac22012-05-08 21:25:06 +00001903 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904 // Make sure our value is up to date first so that our location and location
1905 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001906 if (!UpdateValueIfNeeded(false))
Enrico Granata07a4ac22012-05-08 21:25:06 +00001907 {
1908 error.SetErrorString("unable to read value");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001909 return false;
Enrico Granata07a4ac22012-05-08 21:25:06 +00001910 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001911
Greg Claytonfaac1112013-03-14 18:31:44 +00001912 uint64_t count = 0;
Greg Clayton57ee3062013-07-11 22:46:58 +00001913 const Encoding encoding = GetClangType().GetEncoding (count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001914
Greg Claytonb1320972010-07-14 00:18:15 +00001915 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916
Jim Ingham16e0c682011-08-12 23:34:31 +00001917 Value::ValueType value_type = m_value.GetValueType();
1918
1919 if (value_type == Value::eValueTypeScalar)
1920 {
1921 // If the value is already a scalar, then let the scalar change itself:
1922 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1923 }
1924 else if (byte_size <= Scalar::GetMaxByteSize())
1925 {
1926 // If the value fits in a scalar, then make a new scalar and again let the
1927 // scalar code do the conversion, then figure out where to put the new value.
1928 Scalar new_scalar;
Jim Ingham16e0c682011-08-12 23:34:31 +00001929 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1930 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931 {
Jim Ingham4b536182011-08-09 02:12:22 +00001932 switch (value_type)
1933 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001934 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001935 {
1936 // If it is a load address, then the scalar value is the storage location
1937 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001938 ExecutionContext exe_ctx (GetExecutionContextRef());
1939 Process *process = exe_ctx.GetProcessPtr();
1940 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001941 {
Enrico Granata48ea80f2012-10-24 20:24:39 +00001942 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001943 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1944 new_scalar,
1945 byte_size,
1946 error);
Enrico Granata07a4ac22012-05-08 21:25:06 +00001947 if (!error.Success())
1948 return false;
1949 if (bytes_written != byte_size)
1950 {
1951 error.SetErrorString("unable to write value to memory");
1952 return false;
1953 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001954 }
1955 }
Jim Ingham4b536182011-08-09 02:12:22 +00001956 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001957 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001958 {
1959 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1960 DataExtractor new_data;
1961 new_data.SetByteOrder (m_data.GetByteOrder());
1962
1963 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1964 m_data.SetData(buffer_sp, 0);
1965 bool success = new_scalar.GetData(new_data);
1966 if (success)
1967 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001968 new_data.CopyByteOrderedData (0,
1969 byte_size,
1970 const_cast<uint8_t *>(m_data.GetDataStart()),
1971 byte_size,
1972 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001973 }
1974 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1975
1976 }
Jim Ingham4b536182011-08-09 02:12:22 +00001977 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001978 case Value::eValueTypeFileAddress:
1979 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001980 case Value::eValueTypeVector:
1981 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001982 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001983 }
1984 else
1985 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001986 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001987 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001988 }
1989 else
1990 {
1991 // We don't support setting things bigger than a scalar at present.
Enrico Granata07a4ac22012-05-08 21:25:06 +00001992 error.SetErrorString("unable to write aggregate data type");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001993 return false;
1994 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001995
1996 // If we have reached this point, then we have successfully changed the value.
1997 SetNeedsUpdate();
1998 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001999}
2000
Greg Clayton81e871e2012-02-04 02:27:34 +00002001bool
2002ValueObject::GetDeclaration (Declaration &decl)
2003{
2004 decl.Clear();
2005 return false;
2006}
2007
Greg Clayton84db9102012-03-26 23:03:23 +00002008ConstString
2009ValueObject::GetTypeName()
2010{
Greg Clayton57ee3062013-07-11 22:46:58 +00002011 return GetClangType().GetConstTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00002012}
2013
2014ConstString
2015ValueObject::GetQualifiedTypeName()
2016{
Greg Clayton57ee3062013-07-11 22:46:58 +00002017 return GetClangType().GetConstQualifiedTypeName();
Greg Clayton84db9102012-03-26 23:03:23 +00002018}
2019
2020
Greg Claytonafacd142011-09-02 01:15:17 +00002021LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00002022ValueObject::GetObjectRuntimeLanguage ()
2023{
Greg Clayton57ee3062013-07-11 22:46:58 +00002024 return GetClangType().GetMinimumLanguage ();
Jim Ingham5a369122010-09-28 01:25:32 +00002025}
2026
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002027void
Jim Ingham58b59f92011-04-22 23:53:53 +00002028ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002029{
Jim Ingham58b59f92011-04-22 23:53:53 +00002030 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002031}
2032
2033ValueObjectSP
2034ValueObject::GetSyntheticChild (const ConstString &key) const
2035{
2036 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00002037 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002038 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00002039 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002040 return synthetic_child_sp;
2041}
2042
Greg Clayton2452ab72013-02-08 22:02:02 +00002043uint32_t
Greg Clayton57ee3062013-07-11 22:46:58 +00002044ValueObject::GetTypeInfo (ClangASTType *pointee_or_element_clang_type)
Greg Clayton2452ab72013-02-08 22:02:02 +00002045{
Greg Clayton57ee3062013-07-11 22:46:58 +00002046 return GetClangType().GetTypeInfo (pointee_or_element_clang_type);
Greg Clayton2452ab72013-02-08 22:02:02 +00002047}
2048
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002049bool
2050ValueObject::IsPointerType ()
2051{
Greg Clayton57ee3062013-07-11 22:46:58 +00002052 return GetClangType().IsPointerType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053}
2054
Jim Inghamb7603bb2011-03-18 00:05:18 +00002055bool
Greg Claytondaf515f2011-07-09 20:12:33 +00002056ValueObject::IsArrayType ()
2057{
Greg Clayton57ee3062013-07-11 22:46:58 +00002058 return GetClangType().IsArrayType (NULL, NULL, NULL);
Greg Claytondaf515f2011-07-09 20:12:33 +00002059}
2060
2061bool
Enrico Granata9fc19442011-07-06 02:13:41 +00002062ValueObject::IsScalarType ()
2063{
Greg Clayton57ee3062013-07-11 22:46:58 +00002064 return GetClangType().IsScalarType ();
Enrico Granata9fc19442011-07-06 02:13:41 +00002065}
2066
2067bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00002068ValueObject::IsIntegerType (bool &is_signed)
2069{
Greg Clayton57ee3062013-07-11 22:46:58 +00002070 return GetClangType().IsIntegerType (is_signed);
Jim Inghamb7603bb2011-03-18 00:05:18 +00002071}
Greg Clayton73b472d2010-10-27 03:32:59 +00002072
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002073bool
2074ValueObject::IsPointerOrReferenceType ()
2075{
Greg Clayton57ee3062013-07-11 22:46:58 +00002076 return GetClangType().IsPointerOrReferenceType ();
Greg Clayton007d5be2011-05-30 00:49:24 +00002077}
2078
2079bool
Greg Claytondea8cb42011-06-29 22:09:02 +00002080ValueObject::IsPossibleDynamicType ()
2081{
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002082 ExecutionContext exe_ctx (GetExecutionContextRef());
2083 Process *process = exe_ctx.GetProcessPtr();
2084 if (process)
2085 return process->IsPossibleDynamicValue(*this);
2086 else
Greg Clayton57ee3062013-07-11 22:46:58 +00002087 return GetClangType().IsPossibleDynamicType (NULL, true, true);
Greg Claytondea8cb42011-06-29 22:09:02 +00002088}
2089
Enrico Granata9e7b3882012-12-13 23:50:33 +00002090bool
2091ValueObject::IsObjCNil ()
2092{
Greg Clayton57ee3062013-07-11 22:46:58 +00002093 const uint32_t mask = ClangASTType::eTypeIsObjC | ClangASTType::eTypeIsPointer;
2094 bool isObjCpointer = (((GetClangType().GetTypeInfo(NULL)) & mask) == mask);
Enrico Granata7277d202013-03-15 23:33:15 +00002095 if (!isObjCpointer)
2096 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002097 bool canReadValue = true;
2098 bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
Enrico Granata7277d202013-03-15 23:33:15 +00002099 return canReadValue && isZero;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002100}
2101
Greg Claytonafacd142011-09-02 01:15:17 +00002102ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002103ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002104{
Greg Clayton2452ab72013-02-08 22:02:02 +00002105 const uint32_t type_info = GetTypeInfo ();
Greg Clayton57ee3062013-07-11 22:46:58 +00002106 if (type_info & ClangASTType::eTypeIsArray)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002107 return GetSyntheticArrayMemberFromArray(index, can_create);
2108
Greg Clayton57ee3062013-07-11 22:46:58 +00002109 if (type_info & ClangASTType::eTypeIsPointer)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002110 return GetSyntheticArrayMemberFromPointer(index, can_create);
2111
2112 return ValueObjectSP();
2113
2114}
2115
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002116ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002117ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118{
2119 ValueObjectSP synthetic_child_sp;
2120 if (IsPointerType ())
2121 {
2122 char index_str[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00002123 snprintf(index_str, sizeof(index_str), "[%zu]", index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002124 ConstString index_const_str(index_str);
2125 // Check if we have already created a synthetic array member in this
2126 // valid object. If we have we will re-use it.
2127 synthetic_child_sp = GetSyntheticChild (index_const_str);
2128 if (!synthetic_child_sp)
2129 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002130 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131 // We haven't made a synthetic array member for INDEX yet, so
2132 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00002133 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002134
2135 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00002136 if (synthetic_child)
2137 {
2138 AddSyntheticChild(index_const_str, synthetic_child);
2139 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002140 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00002141 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00002142 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143 }
2144 }
2145 return synthetic_child_sp;
2146}
Jim Ingham22777012010-09-23 02:01:19 +00002147
Greg Claytondaf515f2011-07-09 20:12:33 +00002148// This allows you to create an array member using and index
2149// that doesn't not fall in the normal bounds of the array.
2150// Many times structure can be defined as:
2151// struct Collection
2152// {
2153// uint32_t item_count;
2154// Item item_array[0];
2155// };
2156// The size of the "item_array" is 1, but many times in practice
2157// there are more items in "item_array".
2158
2159ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002160ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create)
Greg Claytondaf515f2011-07-09 20:12:33 +00002161{
2162 ValueObjectSP synthetic_child_sp;
2163 if (IsArrayType ())
2164 {
2165 char index_str[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00002166 snprintf(index_str, sizeof(index_str), "[%zu]", index);
Greg Claytondaf515f2011-07-09 20:12:33 +00002167 ConstString index_const_str(index_str);
2168 // Check if we have already created a synthetic array member in this
2169 // valid object. If we have we will re-use it.
2170 synthetic_child_sp = GetSyntheticChild (index_const_str);
2171 if (!synthetic_child_sp)
2172 {
2173 ValueObject *synthetic_child;
2174 // We haven't made a synthetic array member for INDEX yet, so
2175 // lets make one and cache it for any future reference.
2176 synthetic_child = CreateChildAtIndex(0, true, index);
2177
2178 // Cache the value if we got one back...
2179 if (synthetic_child)
2180 {
2181 AddSyntheticChild(index_const_str, synthetic_child);
2182 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002183 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00002184 synthetic_child_sp->m_is_array_item_for_pointer = true;
2185 }
2186 }
2187 }
2188 return synthetic_child_sp;
2189}
2190
Enrico Granata9fc19442011-07-06 02:13:41 +00002191ValueObjectSP
2192ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2193{
2194 ValueObjectSP synthetic_child_sp;
2195 if (IsScalarType ())
2196 {
2197 char index_str[64];
2198 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2199 ConstString index_const_str(index_str);
2200 // Check if we have already created a synthetic array member in this
2201 // valid object. If we have we will re-use it.
2202 synthetic_child_sp = GetSyntheticChild (index_const_str);
2203 if (!synthetic_child_sp)
2204 {
Enrico Granata9fc19442011-07-06 02:13:41 +00002205 // We haven't made a synthetic array member for INDEX yet, so
2206 // lets make one and cache it for any future reference.
Greg Clayton57ee3062013-07-11 22:46:58 +00002207 ValueObjectChild *synthetic_child = new ValueObjectChild (*this,
2208 GetClangType(),
2209 index_const_str,
2210 GetByteSize(),
2211 0,
2212 to-from+1,
2213 from,
2214 false,
2215 false,
2216 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00002217
2218 // Cache the value if we got one back...
2219 if (synthetic_child)
2220 {
2221 AddSyntheticChild(index_const_str, synthetic_child);
2222 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002223 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00002224 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2225 }
2226 }
2227 }
2228 return synthetic_child_sp;
2229}
2230
Greg Claytonafacd142011-09-02 01:15:17 +00002231ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00002232ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
2233{
2234
2235 ValueObjectSP synthetic_child_sp;
2236
2237 char name_str[64];
2238 snprintf(name_str, sizeof(name_str), "@%i", offset);
2239 ConstString name_const_str(name_str);
2240
2241 // Check if we have already created a synthetic array member in this
2242 // valid object. If we have we will re-use it.
2243 synthetic_child_sp = GetSyntheticChild (name_const_str);
2244
2245 if (synthetic_child_sp.get())
2246 return synthetic_child_sp;
2247
2248 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00002249 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002250
2251 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
Greg Clayton57ee3062013-07-11 22:46:58 +00002252 type,
Enrico Granata6f3533f2011-07-29 19:53:35 +00002253 name_const_str,
Greg Clayton57ee3062013-07-11 22:46:58 +00002254 type.GetByteSize(),
Enrico Granata6f3533f2011-07-29 19:53:35 +00002255 offset,
2256 0,
2257 0,
2258 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00002259 false,
2260 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002261 if (synthetic_child)
2262 {
2263 AddSyntheticChild(name_const_str, synthetic_child);
2264 synthetic_child_sp = synthetic_child->GetSP();
2265 synthetic_child_sp->SetName(name_const_str);
2266 synthetic_child_sp->m_is_child_at_offset = true;
2267 }
2268 return synthetic_child_sp;
2269}
2270
Enrico Granatad55546b2011-07-22 00:16:08 +00002271// your expression path needs to have a leading . or ->
2272// (unless it somehow "looks like" an array, in which case it has
2273// a leading [ symbol). while the [ is meaningful and should be shown
2274// to the user, . and -> are just parser design, but by no means
2275// added information for the user.. strip them off
2276static const char*
2277SkipLeadingExpressionPathSeparators(const char* expression)
2278{
2279 if (!expression || !expression[0])
2280 return expression;
2281 if (expression[0] == '.')
2282 return expression+1;
2283 if (expression[0] == '-' && expression[1] == '>')
2284 return expression+2;
2285 return expression;
2286}
2287
Greg Claytonafacd142011-09-02 01:15:17 +00002288ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00002289ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2290{
2291 ValueObjectSP synthetic_child_sp;
2292 ConstString name_const_string(expression);
2293 // Check if we have already created a synthetic array member in this
2294 // valid object. If we have we will re-use it.
2295 synthetic_child_sp = GetSyntheticChild (name_const_string);
2296 if (!synthetic_child_sp)
2297 {
2298 // We haven't made a synthetic array member for expression yet, so
2299 // lets make one and cache it for any future reference.
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002300 synthetic_child_sp = GetValueForExpressionPath(expression,
2301 NULL, NULL, NULL,
2302 GetValueForExpressionPathOptions().DontAllowSyntheticChildren());
Enrico Granatad55546b2011-07-22 00:16:08 +00002303
2304 // Cache the value if we got one back...
2305 if (synthetic_child_sp.get())
2306 {
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002307 // 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 +00002308 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00002309 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002310 }
2311 }
2312 return synthetic_child_sp;
2313}
2314
2315void
Enrico Granata86cc9822012-03-19 22:58:49 +00002316ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002317{
Enrico Granata86cc9822012-03-19 22:58:49 +00002318 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002319 return;
2320
Enrico Granatac5bc4122012-03-27 02:35:13 +00002321 TargetSP target_sp(GetTargetSP());
Enrico Granata5d5f60c2013-09-24 22:58:37 +00002322 if (target_sp && target_sp->GetEnableSyntheticValue() == false)
Enrico Granatac5bc4122012-03-27 02:35:13 +00002323 {
2324 m_synthetic_value = NULL;
2325 return;
2326 }
2327
Enrico Granatae3e91512012-10-22 18:18:36 +00002328 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2329
Enrico Granata5548cb52013-01-28 23:47:25 +00002330 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
Enrico Granata86cc9822012-03-19 22:58:49 +00002331 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002332
Enrico Granata0c489f52012-03-01 04:24:26 +00002333 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002334 return;
2335
Enrico Granatae3e91512012-10-22 18:18:36 +00002336 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2337 return;
2338
Enrico Granata86cc9822012-03-19 22:58:49 +00002339 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002340}
2341
Jim Ingham78a685a2011-04-16 00:01:13 +00002342void
Greg Claytonafacd142011-09-02 01:15:17 +00002343ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002344{
Greg Claytonafacd142011-09-02 01:15:17 +00002345 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002346 return;
2347
Jim Ingham58b59f92011-04-22 23:53:53 +00002348 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002349 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002350 ExecutionContext exe_ctx (GetExecutionContextRef());
2351 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002352 if (process && process->IsPossibleDynamicValue(*this))
Enrico Granatae3e91512012-10-22 18:18:36 +00002353 {
2354 ClearDynamicTypeInformation ();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002355 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Enrico Granatae3e91512012-10-22 18:18:36 +00002356 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002357 }
2358}
2359
Jim Ingham58b59f92011-04-22 23:53:53 +00002360ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002361ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002362{
Greg Claytonafacd142011-09-02 01:15:17 +00002363 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002364 return ValueObjectSP();
2365
2366 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002367 {
Jim Ingham2837b762011-05-04 03:43:18 +00002368 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002369 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002370 if (m_dynamic_value)
2371 return m_dynamic_value->GetSP();
2372 else
2373 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002374}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002375
Jim Ingham60dbabb2011-12-08 19:44:08 +00002376ValueObjectSP
2377ValueObject::GetStaticValue()
2378{
2379 return GetSP();
2380}
2381
Enrico Granata886147f2012-05-08 18:47:08 +00002382lldb::ValueObjectSP
2383ValueObject::GetNonSyntheticValue ()
2384{
2385 return GetSP();
2386}
2387
Enrico Granatad55546b2011-07-22 00:16:08 +00002388ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002389ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002390{
Enrico Granata86cc9822012-03-19 22:58:49 +00002391 if (use_synthetic == false)
2392 return ValueObjectSP();
2393
Enrico Granatad55546b2011-07-22 00:16:08 +00002394 CalculateSyntheticValue(use_synthetic);
2395
2396 if (m_synthetic_value)
2397 return m_synthetic_value->GetSP();
2398 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002399 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002400}
2401
Greg Claytone221f822011-01-21 01:59:00 +00002402bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002403ValueObject::HasSyntheticValue()
2404{
Enrico Granata5548cb52013-01-28 23:47:25 +00002405 UpdateFormatsIfNeeded();
Enrico Granata27b625e2011-08-09 01:04:56 +00002406
Enrico Granata0c489f52012-03-01 04:24:26 +00002407 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002408 return false;
2409
Enrico Granata86cc9822012-03-19 22:58:49 +00002410 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002411
2412 if (m_synthetic_value)
2413 return true;
2414 else
2415 return false;
2416}
2417
2418bool
Greg Claytone221f822011-01-21 01:59:00 +00002419ValueObject::GetBaseClassPath (Stream &s)
2420{
2421 if (IsBaseClass())
2422 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002423 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Clayton57ee3062013-07-11 22:46:58 +00002424 ClangASTType clang_type = GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002425 std::string cxx_class_name;
Greg Clayton57ee3062013-07-11 22:46:58 +00002426 bool this_had_base_class = clang_type.GetCXXClassName (cxx_class_name);
Greg Claytone221f822011-01-21 01:59:00 +00002427 if (this_had_base_class)
2428 {
2429 if (parent_had_base_class)
2430 s.PutCString("::");
2431 s.PutCString(cxx_class_name.c_str());
2432 }
2433 return parent_had_base_class || this_had_base_class;
2434 }
2435 return false;
2436}
2437
2438
2439ValueObject *
2440ValueObject::GetNonBaseClassParent()
2441{
Jim Ingham78a685a2011-04-16 00:01:13 +00002442 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002443 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002444 if (GetParent()->IsBaseClass())
2445 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002446 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002447 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002448 }
2449 return NULL;
2450}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002451
2452void
Enrico Granata4becb372011-06-29 22:27:15 +00002453ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002454{
Greg Claytone221f822011-01-21 01:59:00 +00002455 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002456
Enrico Granata86cc9822012-03-19 22:58:49 +00002457 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002458 {
Enrico Granata4becb372011-06-29 22:27:15 +00002459 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2460 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2461 // the eHonorPointers mode is meant to produce strings in this latter format
2462 s.PutCString("*(");
2463 }
Greg Claytone221f822011-01-21 01:59:00 +00002464
Enrico Granata4becb372011-06-29 22:27:15 +00002465 ValueObject* parent = GetParent();
2466
2467 if (parent)
2468 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002469
2470 // if we are a deref_of_parent just because we are synthetic array
2471 // members made up to allow ptr[%d] syntax to work in variable
2472 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002473 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002474 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002475
Greg Claytone221f822011-01-21 01:59:00 +00002476 if (!IsBaseClass())
2477 {
2478 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002479 {
Greg Claytone221f822011-01-21 01:59:00 +00002480 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2481 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002482 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002483 ClangASTType non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
Greg Claytone221f822011-01-21 01:59:00 +00002484 if (non_base_class_parent_clang_type)
2485 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002486 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002487 {
2488 s.PutCString("->");
2489 }
Enrico Granata4becb372011-06-29 22:27:15 +00002490 else
2491 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002492 const uint32_t non_base_class_parent_type_info = non_base_class_parent_clang_type.GetTypeInfo();
2493
2494 if (non_base_class_parent_type_info & ClangASTType::eTypeIsPointer)
Enrico Granata4becb372011-06-29 22:27:15 +00002495 {
2496 s.PutCString("->");
2497 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002498 else if ((non_base_class_parent_type_info & ClangASTType::eTypeHasChildren) &&
2499 !(non_base_class_parent_type_info & ClangASTType::eTypeIsArray))
Enrico Granata4becb372011-06-29 22:27:15 +00002500 {
2501 s.PutChar('.');
2502 }
Greg Claytone221f822011-01-21 01:59:00 +00002503 }
2504 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002505 }
Greg Claytone221f822011-01-21 01:59:00 +00002506
2507 const char *name = GetName().GetCString();
2508 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002509 {
Greg Claytone221f822011-01-21 01:59:00 +00002510 if (qualify_cxx_base_classes)
2511 {
2512 if (GetBaseClassPath (s))
2513 s.PutCString("::");
2514 }
2515 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002516 }
2517 }
2518 }
2519
Enrico Granata86cc9822012-03-19 22:58:49 +00002520 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002521 {
Greg Claytone221f822011-01-21 01:59:00 +00002522 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002523 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002524}
2525
Greg Claytonafacd142011-09-02 01:15:17 +00002526ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002527ValueObject::GetValueForExpressionPath(const char* expression,
2528 const char** first_unparsed,
2529 ExpressionPathScanEndReason* reason_to_stop,
2530 ExpressionPathEndResultType* final_value_type,
2531 const GetValueForExpressionPathOptions& options,
2532 ExpressionPathAftermath* final_task_on_target)
2533{
2534
2535 const char* dummy_first_unparsed;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002536 ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2537 ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata86cc9822012-03-19 22:58:49 +00002538 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002539
2540 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2541 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2542 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2543 final_value_type ? final_value_type : &dummy_final_value_type,
2544 options,
2545 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2546
Enrico Granata86cc9822012-03-19 22:58:49 +00002547 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002548 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002549
Enrico Granata86cc9822012-03-19 22:58:49 +00002550 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 +00002551 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002552 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002553 {
2554 Error error;
2555 ValueObjectSP final_value = ret_val->Dereference(error);
2556 if (error.Fail() || !final_value.get())
2557 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002558 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002559 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002560 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002561 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002562 return ValueObjectSP();
2563 }
2564 else
2565 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002566 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002567 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002568 return final_value;
2569 }
2570 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002571 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002572 {
2573 Error error;
2574 ValueObjectSP final_value = ret_val->AddressOf(error);
2575 if (error.Fail() || !final_value.get())
2576 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002577 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002578 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002579 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002580 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002581 return ValueObjectSP();
2582 }
2583 else
2584 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002585 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002586 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002587 return final_value;
2588 }
2589 }
2590 }
2591 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2592}
2593
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002594int
2595ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002596 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002597 const char** first_unparsed,
2598 ExpressionPathScanEndReason* reason_to_stop,
2599 ExpressionPathEndResultType* final_value_type,
2600 const GetValueForExpressionPathOptions& options,
2601 ExpressionPathAftermath* final_task_on_target)
2602{
2603 const char* dummy_first_unparsed;
2604 ExpressionPathScanEndReason dummy_reason_to_stop;
2605 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002606 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002607
2608 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2609 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2610 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2611 final_value_type ? final_value_type : &dummy_final_value_type,
2612 options,
2613 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2614
2615 if (!ret_val.get()) // if there are errors, I add nothing to the list
2616 return 0;
2617
Enrico Granata86ea8d82012-03-29 01:34:34 +00002618 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002619 {
2620 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002621 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002622 {
2623 list->Append(ret_val);
2624 return 1;
2625 }
Enrico Granata86ea8d82012-03-29 01:34:34 +00002626 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 +00002627 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002628 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002629 {
2630 Error error;
2631 ValueObjectSP final_value = ret_val->Dereference(error);
2632 if (error.Fail() || !final_value.get())
2633 {
Greg Clayton23f59502012-07-17 03:23:13 +00002634 if (reason_to_stop)
2635 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2636 if (final_value_type)
2637 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002638 return 0;
2639 }
2640 else
2641 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002642 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002643 list->Append(final_value);
2644 return 1;
2645 }
2646 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002647 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002648 {
2649 Error error;
2650 ValueObjectSP final_value = ret_val->AddressOf(error);
2651 if (error.Fail() || !final_value.get())
2652 {
Greg Clayton23f59502012-07-17 03:23:13 +00002653 if (reason_to_stop)
2654 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2655 if (final_value_type)
2656 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002657 return 0;
2658 }
2659 else
2660 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002661 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002662 list->Append(final_value);
2663 return 1;
2664 }
2665 }
2666 }
2667 }
2668 else
2669 {
2670 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2671 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2672 ret_val,
2673 list,
2674 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2675 final_value_type ? final_value_type : &dummy_final_value_type,
2676 options,
2677 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2678 }
2679 // in any non-covered case, just do the obviously right thing
2680 list->Append(ret_val);
2681 return 1;
2682}
2683
Greg Claytonafacd142011-09-02 01:15:17 +00002684ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002685ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2686 const char** first_unparsed,
2687 ExpressionPathScanEndReason* reason_to_stop,
2688 ExpressionPathEndResultType* final_result,
2689 const GetValueForExpressionPathOptions& options,
2690 ExpressionPathAftermath* what_next)
2691{
2692 ValueObjectSP root = GetSP();
2693
2694 if (!root.get())
2695 return ValueObjectSP();
2696
2697 *first_unparsed = expression_cstr;
2698
2699 while (true)
2700 {
2701
2702 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2703
Greg Clayton57ee3062013-07-11 22:46:58 +00002704 ClangASTType root_clang_type = root->GetClangType();
2705 ClangASTType pointee_clang_type;
2706 Flags pointee_clang_type_info;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002707
Greg Clayton57ee3062013-07-11 22:46:58 +00002708 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002709 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00002710 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002711
2712 if (!expression_cstr || *expression_cstr == '\0')
2713 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002714 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002715 return root;
2716 }
2717
2718 switch (*expression_cstr)
2719 {
2720 case '-':
2721 {
2722 if (options.m_check_dot_vs_arrow_syntax &&
Greg Clayton57ee3062013-07-11 22:46:58 +00002723 root_clang_type_info.Test(ClangASTType::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002724 {
2725 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002726 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2727 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002728 return ValueObjectSP();
2729 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002730 if (root_clang_type_info.Test(ClangASTType::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2731 root_clang_type_info.Test(ClangASTType::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002732 options.m_no_fragile_ivar)
2733 {
2734 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002735 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2736 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002737 return ValueObjectSP();
2738 }
2739 if (expression_cstr[1] != '>')
2740 {
2741 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002742 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2743 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002744 return ValueObjectSP();
2745 }
2746 expression_cstr++; // skip the -
2747 }
2748 case '.': // or fallthrough from ->
2749 {
2750 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Greg Clayton57ee3062013-07-11 22:46:58 +00002751 root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002752 {
2753 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002754 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2755 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002756 return ValueObjectSP();
2757 }
2758 expression_cstr++; // skip .
2759 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2760 ConstString child_name;
2761 if (!next_separator) // if no other separator just expand this last layer
2762 {
2763 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002764 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2765
2766 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002767 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002768 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002769 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2770 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002771 return child_valobj_sp;
2772 }
2773 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2774 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002775 if (root->IsSynthetic())
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002776 {
2777 *first_unparsed = expression_cstr;
2778 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2779 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2780 return ValueObjectSP();
2781 }
2782
2783 child_valobj_sp = root->GetSyntheticValue();
Enrico Granata86cc9822012-03-19 22:58:49 +00002784 if (child_valobj_sp.get())
2785 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002786 }
2787
2788 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2789 // so we hit the "else" branch, and return an error
2790 if(child_valobj_sp.get()) // if it worked, just return
2791 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002792 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002793 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2794 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002795 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002796 }
2797 else
2798 {
2799 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002800 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2801 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002802 return ValueObjectSP();
2803 }
2804 }
2805 else // other layers do expand
2806 {
2807 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002808 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2809 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002810 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002811 root = child_valobj_sp;
2812 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002813 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002814 continue;
2815 }
2816 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2817 {
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002818 if (root->IsSynthetic())
2819 {
2820 *first_unparsed = expression_cstr;
2821 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2822 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2823 return ValueObjectSP();
2824 }
2825
Enrico Granata86cc9822012-03-19 22:58:49 +00002826 child_valobj_sp = root->GetSyntheticValue(true);
2827 if (child_valobj_sp)
2828 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002829 }
2830
2831 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2832 // so we hit the "else" branch, and return an error
2833 if(child_valobj_sp.get()) // if it worked, move on
2834 {
2835 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002836 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002837 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002838 continue;
2839 }
2840 else
2841 {
2842 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002843 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2844 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002845 return ValueObjectSP();
2846 }
2847 }
2848 break;
2849 }
2850 case '[':
2851 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002852 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray) && !root_clang_type_info.Test(ClangASTType::eTypeIsPointer) && !root_clang_type_info.Test(ClangASTType::eTypeIsVector)) // if this is not a T[] nor a T*
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002853 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002854 if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002855 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002856 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2857 {
2858 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002859 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2860 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002861 return ValueObjectSP();
2862 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002863 }
2864 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2865 {
2866 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002867 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2868 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002869 return ValueObjectSP();
2870 }
2871 }
2872 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2873 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002874 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002875 {
2876 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002877 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2878 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002879 return ValueObjectSP();
2880 }
2881 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2882 {
2883 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002884 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2885 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002886 return root;
2887 }
2888 }
2889 const char *separator_position = ::strchr(expression_cstr+1,'-');
2890 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2891 if (!close_bracket_position) // if there is no ], this is a syntax error
2892 {
2893 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002894 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2895 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002896 return ValueObjectSP();
2897 }
2898 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2899 {
2900 char *end = NULL;
2901 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2902 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2903 {
2904 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002905 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2906 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002907 return ValueObjectSP();
2908 }
2909 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2910 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002911 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002912 {
2913 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002914 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2915 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002916 return root;
2917 }
2918 else
2919 {
2920 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002921 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2922 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002923 return ValueObjectSP();
2924 }
2925 }
2926 // from here on we do have a valid index
Greg Clayton57ee3062013-07-11 22:46:58 +00002927 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002928 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002929 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2930 if (!child_valobj_sp)
2931 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002932 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002933 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2934 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002935 if (child_valobj_sp)
2936 {
2937 root = child_valobj_sp;
2938 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002939 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002940 continue;
2941 }
2942 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002943 {
2944 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002945 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2946 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002947 return ValueObjectSP();
2948 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002949 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002950 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002951 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002952 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
Greg Clayton57ee3062013-07-11 22:46:58 +00002953 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002954 {
2955 Error error;
2956 root = root->Dereference(error);
2957 if (error.Fail() || !root.get())
2958 {
2959 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002960 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2961 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002962 return ValueObjectSP();
2963 }
2964 else
2965 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002966 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002967 continue;
2968 }
2969 }
2970 else
2971 {
Greg Clayton57ee3062013-07-11 22:46:58 +00002972 if (root->GetClangType().GetMinimumLanguage() == eLanguageTypeObjC
2973 && pointee_clang_type_info.AllClear(ClangASTType::eTypeIsPointer)
Greg Clayton84db9102012-03-26 23:03:23 +00002974 && root->HasSyntheticValue()
2975 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002976 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002977 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002978 }
2979 else
2980 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002981 if (!root.get())
2982 {
2983 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002984 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2985 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002986 return ValueObjectSP();
2987 }
2988 else
2989 {
2990 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002991 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002992 continue;
2993 }
2994 }
2995 }
Greg Clayton57ee3062013-07-11 22:46:58 +00002996 else if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002997 {
2998 root = root->GetSyntheticBitFieldChild(index, index, true);
2999 if (!root.get())
3000 {
3001 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003002 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3003 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003004 return ValueObjectSP();
3005 }
3006 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3007 {
3008 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003009 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3010 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003011 return root;
3012 }
3013 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003014 else if (root_clang_type_info.Test(ClangASTType::eTypeIsVector))
Enrico Granata08a1bb82013-06-19 00:00:45 +00003015 {
3016 root = root->GetChildAtIndex(index, true);
3017 if (!root.get())
3018 {
3019 *first_unparsed = expression_cstr;
3020 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3021 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3022 return ValueObjectSP();
3023 }
3024 else
3025 {
3026 *first_unparsed = end+1; // skip ]
3027 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
3028 continue;
3029 }
3030 }
Enrico Granata86cc9822012-03-19 22:58:49 +00003031 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00003032 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003033 if (root->HasSyntheticValue())
3034 root = root->GetSyntheticValue();
3035 else if (!root->IsSynthetic())
3036 {
3037 *first_unparsed = expression_cstr;
3038 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3039 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3040 return ValueObjectSP();
3041 }
3042 // if we are here, then root itself is a synthetic VO.. should be good to go
3043
Enrico Granata27b625e2011-08-09 01:04:56 +00003044 if (!root.get())
3045 {
3046 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003047 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
3048 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
3049 return ValueObjectSP();
3050 }
3051 root = root->GetChildAtIndex(index, true);
3052 if (!root.get())
3053 {
3054 *first_unparsed = expression_cstr;
3055 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3056 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003057 return ValueObjectSP();
3058 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00003059 else
3060 {
3061 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003062 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00003063 continue;
3064 }
Enrico Granata27b625e2011-08-09 01:04:56 +00003065 }
3066 else
3067 {
3068 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003069 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3070 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00003071 return ValueObjectSP();
3072 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003073 }
3074 else // we have a low and a high index
3075 {
3076 char *end = NULL;
3077 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3078 if (!end || end != separator_position) // if something weird is in our way return an error
3079 {
3080 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003081 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3082 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003083 return ValueObjectSP();
3084 }
3085 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3086 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3087 {
3088 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003089 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3090 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003091 return ValueObjectSP();
3092 }
3093 if (index_lower > index_higher) // swap indices if required
3094 {
3095 unsigned long temp = index_lower;
3096 index_lower = index_higher;
3097 index_higher = temp;
3098 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003099 if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003100 {
3101 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3102 if (!root.get())
3103 {
3104 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003105 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3106 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003107 return ValueObjectSP();
3108 }
3109 else
3110 {
3111 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003112 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3113 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003114 return root;
3115 }
3116 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003117 else if (root_clang_type_info.Test(ClangASTType::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 +00003118 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Greg Clayton57ee3062013-07-11 22:46:58 +00003119 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003120 {
3121 Error error;
3122 root = root->Dereference(error);
3123 if (error.Fail() || !root.get())
3124 {
3125 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003126 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3127 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003128 return ValueObjectSP();
3129 }
3130 else
3131 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003132 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003133 continue;
3134 }
3135 }
3136 else
3137 {
3138 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003139 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3140 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003141 return root;
3142 }
3143 }
3144 break;
3145 }
3146 default: // some non-separator is in the way
3147 {
3148 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003149 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3150 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003151 return ValueObjectSP();
3152 break;
3153 }
3154 }
3155 }
3156}
3157
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003158int
3159ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3160 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00003161 ValueObjectSP root,
3162 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003163 ExpressionPathScanEndReason* reason_to_stop,
3164 ExpressionPathEndResultType* final_result,
3165 const GetValueForExpressionPathOptions& options,
3166 ExpressionPathAftermath* what_next)
3167{
3168 if (!root.get())
3169 return 0;
3170
3171 *first_unparsed = expression_cstr;
3172
3173 while (true)
3174 {
3175
3176 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3177
Greg Clayton57ee3062013-07-11 22:46:58 +00003178 ClangASTType root_clang_type = root->GetClangType();
3179 ClangASTType pointee_clang_type;
3180 Flags pointee_clang_type_info;
3181 Flags root_clang_type_info(root_clang_type.GetTypeInfo(&pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003182 if (pointee_clang_type)
Greg Clayton57ee3062013-07-11 22:46:58 +00003183 pointee_clang_type_info.Reset(pointee_clang_type.GetTypeInfo());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003184
3185 if (!expression_cstr || *expression_cstr == '\0')
3186 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003187 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003188 list->Append(root);
3189 return 1;
3190 }
3191
3192 switch (*expression_cstr)
3193 {
3194 case '[':
3195 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003196 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray) && !root_clang_type_info.Test(ClangASTType::eTypeIsPointer)) // if this is not a T[] nor a T*
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003197 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003198 if (!root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003199 {
3200 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003201 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3202 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003203 return 0;
3204 }
3205 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3206 {
3207 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003208 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3209 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003210 return 0;
3211 }
3212 }
3213 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3214 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003215 if (!root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003216 {
3217 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003218 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3219 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003220 return 0;
3221 }
3222 else // expand this into list
3223 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003224 const size_t max_index = root->GetNumChildren() - 1;
3225 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003226 {
3227 ValueObjectSP child =
3228 root->GetChildAtIndex(index, true);
3229 list->Append(child);
3230 }
3231 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003232 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3233 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003234 return max_index; // tell me number of items I added to the VOList
3235 }
3236 }
3237 const char *separator_position = ::strchr(expression_cstr+1,'-');
3238 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3239 if (!close_bracket_position) // if there is no ], this is a syntax error
3240 {
3241 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003242 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3243 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003244 return 0;
3245 }
3246 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3247 {
3248 char *end = NULL;
3249 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3250 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3251 {
3252 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003253 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3254 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003255 return 0;
3256 }
3257 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3258 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003259 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003260 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003261 const size_t max_index = root->GetNumChildren() - 1;
3262 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003263 {
3264 ValueObjectSP child =
3265 root->GetChildAtIndex(index, true);
3266 list->Append(child);
3267 }
3268 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003269 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3270 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003271 return max_index; // tell me number of items I added to the VOList
3272 }
3273 else
3274 {
3275 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003276 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3277 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003278 return 0;
3279 }
3280 }
3281 // from here on we do have a valid index
Greg Clayton57ee3062013-07-11 22:46:58 +00003282 if (root_clang_type_info.Test(ClangASTType::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003283 {
3284 root = root->GetChildAtIndex(index, true);
3285 if (!root.get())
3286 {
3287 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003288 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3289 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003290 return 0;
3291 }
3292 else
3293 {
3294 list->Append(root);
3295 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003296 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3297 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003298 return 1;
3299 }
3300 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003301 else if (root_clang_type_info.Test(ClangASTType::eTypeIsPointer))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003302 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003303 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
Greg Clayton57ee3062013-07-11 22:46:58 +00003304 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003305 {
3306 Error error;
3307 root = root->Dereference(error);
3308 if (error.Fail() || !root.get())
3309 {
3310 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003311 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3312 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003313 return 0;
3314 }
3315 else
3316 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003317 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003318 continue;
3319 }
3320 }
3321 else
3322 {
3323 root = root->GetSyntheticArrayMemberFromPointer(index, true);
3324 if (!root.get())
3325 {
3326 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003327 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3328 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003329 return 0;
3330 }
3331 else
3332 {
3333 list->Append(root);
3334 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003335 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3336 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003337 return 1;
3338 }
3339 }
3340 }
3341 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3342 {
3343 root = root->GetSyntheticBitFieldChild(index, index, true);
3344 if (!root.get())
3345 {
3346 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003347 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3348 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003349 return 0;
3350 }
3351 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3352 {
3353 list->Append(root);
3354 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003355 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3356 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003357 return 1;
3358 }
3359 }
3360 }
3361 else // we have a low and a high index
3362 {
3363 char *end = NULL;
3364 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3365 if (!end || end != separator_position) // if something weird is in our way return an error
3366 {
3367 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003368 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3369 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003370 return 0;
3371 }
3372 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3373 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3374 {
3375 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003376 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3377 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003378 return 0;
3379 }
3380 if (index_lower > index_higher) // swap indices if required
3381 {
3382 unsigned long temp = index_lower;
3383 index_lower = index_higher;
3384 index_higher = temp;
3385 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003386 if (root_clang_type_info.Test(ClangASTType::eTypeIsScalar)) // expansion only works for scalars
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003387 {
3388 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3389 if (!root.get())
3390 {
3391 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003392 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3393 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003394 return 0;
3395 }
3396 else
3397 {
3398 list->Append(root);
3399 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003400 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3401 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003402 return 1;
3403 }
3404 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003405 else if (root_clang_type_info.Test(ClangASTType::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 +00003406 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Greg Clayton57ee3062013-07-11 22:46:58 +00003407 pointee_clang_type_info.Test(ClangASTType::eTypeIsScalar))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003408 {
3409 Error error;
3410 root = root->Dereference(error);
3411 if (error.Fail() || !root.get())
3412 {
3413 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003414 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3415 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003416 return 0;
3417 }
3418 else
3419 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003420 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003421 continue;
3422 }
3423 }
3424 else
3425 {
Johnny Chen44805302011-07-19 19:48:13 +00003426 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003427 index <= index_higher; index++)
3428 {
3429 ValueObjectSP child =
3430 root->GetChildAtIndex(index, true);
3431 list->Append(child);
3432 }
3433 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003434 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3435 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003436 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3437 }
3438 }
3439 break;
3440 }
3441 default: // some non-[ separator, or something entirely wrong, is in the way
3442 {
3443 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003444 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3445 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003446 return 0;
3447 break;
3448 }
3449 }
3450 }
3451}
3452
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003453void
3454ValueObject::LogValueObject (Log *log)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003455{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003456 if (log)
3457 return LogValueObject (log, DumpValueObjectOptions::DefaultOptions());
Greg Clayton1d3afba2010-10-05 00:00:42 +00003458}
3459
Enrico Granata0c489f52012-03-01 04:24:26 +00003460void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003461ValueObject::LogValueObject (Log *log, const DumpValueObjectOptions& options)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003462{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003463 if (log)
Greg Claytonf830dbb2012-03-22 18:15:37 +00003464 {
3465 StreamString s;
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003466 Dump (s, options);
Greg Claytonf830dbb2012-03-22 18:15:37 +00003467 if (s.GetSize())
3468 log->PutCString(s.GetData());
3469 }
3470}
3471
3472void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003473ValueObject::Dump (Stream &s)
Enrico Granata0c489f52012-03-01 04:24:26 +00003474{
3475
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003476 ValueObjectPrinter printer(this,&s,DumpValueObjectOptions::DefaultOptions());
3477 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003478}
3479
3480void
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003481ValueObject::Dump (Stream &s,
3482 const DumpValueObjectOptions& options)
Enrico Granata0c489f52012-03-01 04:24:26 +00003483{
Enrico Granata4d93b8c2013-09-30 19:11:51 +00003484 ValueObjectPrinter printer(this,&s,options);
3485 printer.PrintValueObject();
Enrico Granata0c489f52012-03-01 04:24:26 +00003486}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003487
3488ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003489ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003490{
3491 ValueObjectSP valobj_sp;
3492
Enrico Granatac3e320a2011-08-02 17:27:39 +00003493 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003494 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003495 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003496
3497 DataExtractor data;
3498 data.SetByteOrder (m_data.GetByteOrder());
3499 data.SetAddressByteSize(m_data.GetAddressByteSize());
3500
Enrico Granata9f1e2042012-04-24 22:15:37 +00003501 if (IsBitfield())
3502 {
3503 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
Greg Clayton57ee3062013-07-11 22:46:58 +00003504 m_error = v.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Enrico Granata9f1e2042012-04-24 22:15:37 +00003505 }
3506 else
Greg Clayton57ee3062013-07-11 22:46:58 +00003507 m_error = m_value.GetValueAsData (&exe_ctx, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003508
3509 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003510 GetClangType(),
3511 name,
3512 data,
3513 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003514 }
Jim Ingham6035b672011-03-31 00:19:25 +00003515
3516 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003517 {
Jim Ingham58b59f92011-04-22 23:53:53 +00003518 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003519 }
3520 return valobj_sp;
3521}
3522
Greg Claytonafacd142011-09-02 01:15:17 +00003523ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003524ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003525{
Jim Ingham58b59f92011-04-22 23:53:53 +00003526 if (m_deref_valobj)
3527 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003528
Greg Clayton54979cd2010-12-15 05:08:08 +00003529 const bool is_pointer_type = IsPointerType();
3530 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003531 {
3532 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003533 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003534
3535 std::string child_name_str;
3536 uint32_t child_byte_size = 0;
3537 int32_t child_byte_offset = 0;
3538 uint32_t child_bitfield_bit_size = 0;
3539 uint32_t child_bitfield_bit_offset = 0;
3540 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003541 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003542 const bool transparent_pointers = false;
Greg Clayton57ee3062013-07-11 22:46:58 +00003543 ClangASTType clang_type = GetClangType();
3544 ClangASTType child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003545
Greg Claytoncc4d0142012-02-17 07:49:44 +00003546 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003547
Greg Clayton57ee3062013-07-11 22:46:58 +00003548 child_clang_type = clang_type.GetChildClangTypeAtIndex (&exe_ctx,
3549 GetName().GetCString(),
3550 0,
3551 transparent_pointers,
3552 omit_empty_base_classes,
3553 ignore_array_bounds,
3554 child_name_str,
3555 child_byte_size,
3556 child_byte_offset,
3557 child_bitfield_bit_size,
3558 child_bitfield_bit_offset,
3559 child_is_base_class,
3560 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003561 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003562 {
3563 ConstString child_name;
3564 if (!child_name_str.empty())
3565 child_name.SetCString (child_name_str.c_str());
3566
Jim Ingham58b59f92011-04-22 23:53:53 +00003567 m_deref_valobj = new ValueObjectChild (*this,
Jim Ingham58b59f92011-04-22 23:53:53 +00003568 child_clang_type,
3569 child_name,
3570 child_byte_size,
3571 child_byte_offset,
3572 child_bitfield_bit_size,
3573 child_bitfield_bit_offset,
3574 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003575 child_is_deref_of_parent,
3576 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003577 }
3578 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003579
Jim Ingham58b59f92011-04-22 23:53:53 +00003580 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003581 {
3582 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003583 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003584 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003585 else
3586 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003587 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003588 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003589
3590 if (is_pointer_type)
3591 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3592 else
3593 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003594 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003595 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003596}
3597
Greg Claytonafacd142011-09-02 01:15:17 +00003598ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003599ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003600{
Jim Ingham78a685a2011-04-16 00:01:13 +00003601 if (m_addr_of_valobj_sp)
3602 return m_addr_of_valobj_sp;
3603
Greg Claytone0d378b2011-03-24 21:19:54 +00003604 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003605 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003606 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003607 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003608 if (addr != LLDB_INVALID_ADDRESS)
3609 {
3610 switch (address_type)
3611 {
3612 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003613 {
3614 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003615 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003616 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3617 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003618 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003619
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003620 case eAddressTypeFile:
3621 case eAddressTypeLoad:
3622 case eAddressTypeHost:
3623 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003624 ClangASTType clang_type = GetClangType();
3625 if (clang_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003626 {
3627 std::string name (1, '&');
3628 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003629 ExecutionContext exe_ctx (GetExecutionContextRef());
3630 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00003631 clang_type.GetPointerType(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003632 ConstString (name.c_str()),
3633 addr,
3634 eAddressTypeInvalid,
3635 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003636 }
3637 }
3638 break;
3639 }
3640 }
Sean Callananed185ab2013-04-19 19:47:32 +00003641 else
3642 {
3643 StreamString expr_path_strm;
3644 GetExpressionPath(expr_path_strm, true);
3645 error.SetErrorStringWithFormat("'%s' doesn't have a valid address", expr_path_strm.GetString().c_str());
3646 }
3647
Jim Ingham78a685a2011-04-16 00:01:13 +00003648 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003649}
3650
Greg Clayton9a142cf2012-02-03 05:34:10 +00003651ValueObjectSP
3652ValueObject::Cast (const ClangASTType &clang_ast_type)
3653{
Greg Clayton81e871e2012-02-04 02:27:34 +00003654 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003655}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003656
Greg Claytonafacd142011-09-02 01:15:17 +00003657ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003658ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3659{
Greg Claytonafacd142011-09-02 01:15:17 +00003660 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003661 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003662 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003663
3664 if (ptr_value != LLDB_INVALID_ADDRESS)
3665 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003666 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003667 ExecutionContext exe_ctx (GetExecutionContextRef());
3668 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003669 name,
3670 ptr_addr,
3671 clang_ast_type);
3672 }
3673 return valobj_sp;
3674}
3675
Greg Claytonafacd142011-09-02 01:15:17 +00003676ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003677ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3678{
Greg Claytonafacd142011-09-02 01:15:17 +00003679 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003680 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003681 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003682
3683 if (ptr_value != LLDB_INVALID_ADDRESS)
3684 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003685 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003686 ExecutionContext exe_ctx (GetExecutionContextRef());
3687 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003688 name,
3689 ptr_addr,
3690 type_sp);
3691 }
3692 return valobj_sp;
3693}
3694
Jim Ingham6035b672011-03-31 00:19:25 +00003695ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003696 m_mod_id(),
3697 m_exe_ctx_ref(),
3698 m_needs_update (true),
3699 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003700{
3701}
3702
3703ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003704 m_mod_id(),
3705 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003706 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003707 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003708{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003709 ExecutionContext exe_ctx(exe_scope);
3710 TargetSP target_sp (exe_ctx.GetTargetSP());
3711 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003712 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003713 m_exe_ctx_ref.SetTargetSP (target_sp);
3714 ProcessSP process_sp (exe_ctx.GetProcessSP());
3715 if (!process_sp)
3716 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003717
Greg Claytoncc4d0142012-02-17 07:49:44 +00003718 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003719 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003720 m_mod_id = process_sp->GetModID();
3721 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003722
Greg Claytoncc4d0142012-02-17 07:49:44 +00003723 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003724
Greg Claytoncc4d0142012-02-17 07:49:44 +00003725 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003726 {
3727 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003728 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003729 }
Jim Ingham6035b672011-03-31 00:19:25 +00003730
Greg Claytoncc4d0142012-02-17 07:49:44 +00003731 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003732 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003733 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003734
Jason Molendab57e4a12013-11-04 09:33:30 +00003735 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003736 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003737 {
3738 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003739 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003740 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003741 if (frame_sp)
3742 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003743 }
3744 }
3745 }
Jim Ingham6035b672011-03-31 00:19:25 +00003746}
3747
3748ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003749 m_mod_id(),
3750 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3751 m_needs_update (true),
3752 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003753{
3754}
3755
3756ValueObject::EvaluationPoint::~EvaluationPoint ()
3757{
3758}
3759
Jim Ingham6035b672011-03-31 00:19:25 +00003760// This function checks the EvaluationPoint against the current process state. If the current
3761// state matches the evaluation point, or the evaluation point is already invalid, then we return
3762// false, meaning "no change". If the current state is different, we update our state, and return
3763// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3764// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003765// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003766
3767bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003768ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003769{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003770
3771 // Start with the target, if it is NULL, then we're obviously not going to get any further:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003772 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock());
Jim Ingham73ca05a2011-12-17 01:35:57 +00003773
Greg Claytoncc4d0142012-02-17 07:49:44 +00003774 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003775 return false;
3776
Jim Ingham6035b672011-03-31 00:19:25 +00003777 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003778 Process *process = exe_ctx.GetProcessPtr();
3779 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003780 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003781
Jim Ingham6035b672011-03-31 00:19:25 +00003782 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003783 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003784
Jim Ingham78a685a2011-04-16 00:01:13 +00003785 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3786 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003787 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003788 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003789
Greg Clayton23f59502012-07-17 03:23:13 +00003790 bool changed = false;
3791 const bool was_valid = m_mod_id.IsValid();
3792 if (was_valid)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003793 {
3794 if (m_mod_id == current_mod_id)
3795 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003796 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003797 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003798 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003799 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003800 else
3801 {
3802 m_mod_id = current_mod_id;
3803 m_needs_update = true;
3804 changed = true;
3805 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003806 }
Jim Ingham6035b672011-03-31 00:19:25 +00003807
Jim Ingham73ca05a2011-12-17 01:35:57 +00003808 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3809 // That way we'll be sure to return a valid exe_scope.
3810 // 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 +00003811
Greg Claytoncc4d0142012-02-17 07:49:44 +00003812 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003813 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003814 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3815 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003816 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003817 if (m_exe_ctx_ref.HasFrameRef())
3818 {
Jason Molendab57e4a12013-11-04 09:33:30 +00003819 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003820 if (!frame_sp)
3821 {
3822 // We used to have a frame, but now it is gone
3823 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003824 changed = was_valid;
Greg Claytoncc4d0142012-02-17 07:49:44 +00003825 }
3826 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003827 }
Jim Ingham6035b672011-03-31 00:19:25 +00003828 else
3829 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003830 // We used to have a thread, but now it is gone
3831 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00003832 changed = was_valid;
Jim Ingham6035b672011-03-31 00:19:25 +00003833 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003834
Jim Ingham6035b672011-03-31 00:19:25 +00003835 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003836 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003837}
3838
Jim Ingham61be0902011-05-02 18:13:59 +00003839void
3840ValueObject::EvaluationPoint::SetUpdated ()
3841{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003842 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3843 if (process_sp)
3844 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003845 m_first_update = false;
3846 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003847}
3848
3849
Enrico Granataf2bbf712011-07-15 02:26:42 +00003850
3851void
Enrico Granata86cc9822012-03-19 22:58:49 +00003852ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003853{
Enrico Granata86cc9822012-03-19 22:58:49 +00003854 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3855 m_value_str.clear();
3856
3857 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3858 m_location_str.clear();
3859
3860 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
3861 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003862 m_summary_str.clear();
3863 }
3864
3865 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3866 m_object_desc_str.clear();
3867
3868 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3869 {
3870 if (m_synthetic_value)
3871 m_synthetic_value = NULL;
3872 }
Johnny Chen44805302011-07-19 19:48:13 +00003873}
Enrico Granata9128ee22011-09-06 19:20:51 +00003874
3875SymbolContextScope *
3876ValueObject::GetSymbolContextScope()
3877{
3878 if (m_parent)
3879 {
3880 if (!m_parent->IsPointerOrReferenceType())
3881 return m_parent->GetSymbolContextScope();
3882 }
3883 return NULL;
3884}
Enrico Granatab2698cd2012-09-13 18:27:09 +00003885
3886lldb::ValueObjectSP
3887ValueObject::CreateValueObjectFromExpression (const char* name,
3888 const char* expression,
3889 const ExecutionContext& exe_ctx)
3890{
3891 lldb::ValueObjectSP retval_sp;
3892 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
3893 if (!target_sp)
3894 return retval_sp;
3895 if (!expression || !*expression)
3896 return retval_sp;
3897 target_sp->EvaluateExpression (expression,
3898 exe_ctx.GetFrameSP().get(),
3899 retval_sp);
3900 if (retval_sp && name && *name)
3901 retval_sp->SetName(ConstString(name));
3902 return retval_sp;
3903}
3904
3905lldb::ValueObjectSP
3906ValueObject::CreateValueObjectFromAddress (const char* name,
3907 uint64_t address,
3908 const ExecutionContext& exe_ctx,
3909 ClangASTType type)
3910{
Greg Clayton57ee3062013-07-11 22:46:58 +00003911 if (type)
Enrico Granatab2698cd2012-09-13 18:27:09 +00003912 {
Greg Clayton57ee3062013-07-11 22:46:58 +00003913 ClangASTType pointer_type(type.GetPointerType());
3914 if (pointer_type)
3915 {
3916 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
3917 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3918 pointer_type,
3919 ConstString(name),
3920 buffer,
3921 lldb::endian::InlHostByteOrder(),
3922 exe_ctx.GetAddressByteSize()));
3923 if (ptr_result_valobj_sp)
3924 {
3925 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
3926 Error err;
3927 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
3928 if (ptr_result_valobj_sp && name && *name)
3929 ptr_result_valobj_sp->SetName(ConstString(name));
3930 }
3931 return ptr_result_valobj_sp;
3932 }
Enrico Granatab2698cd2012-09-13 18:27:09 +00003933 }
Greg Clayton57ee3062013-07-11 22:46:58 +00003934 return lldb::ValueObjectSP();
Enrico Granatab2698cd2012-09-13 18:27:09 +00003935}
3936
3937lldb::ValueObjectSP
3938ValueObject::CreateValueObjectFromData (const char* name,
3939 DataExtractor& data,
3940 const ExecutionContext& exe_ctx,
3941 ClangASTType type)
3942{
3943 lldb::ValueObjectSP new_value_sp;
3944 new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Clayton57ee3062013-07-11 22:46:58 +00003945 type,
Enrico Granatab2698cd2012-09-13 18:27:09 +00003946 ConstString(name),
3947 data,
3948 LLDB_INVALID_ADDRESS);
3949 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
3950 if (new_value_sp && name && *name)
3951 new_value_sp->SetName(ConstString(name));
3952 return new_value_sp;
3953}
Enrico Granata4873e522013-04-11 22:48:58 +00003954
3955ModuleSP
3956ValueObject::GetModule ()
3957{
3958 ValueObject* root(GetRoot());
3959 if (root != this)
3960 return root->GetModule();
3961 return lldb::ModuleSP();
3962}
3963
3964ValueObject*
3965ValueObject::GetRoot ()
3966{
3967 if (m_root)
3968 return m_root;
3969 ValueObject* parent = m_parent;
3970 if (!parent)
3971 return (m_root = this);
3972 while (parent->m_parent)
3973 {
3974 if (parent->m_root)
3975 return (m_root = parent->m_root);
3976 parent = parent->m_parent;
3977 }
3978 return (m_root = parent);
3979}
3980
3981AddressType
3982ValueObject::GetAddressTypeOfChildren()
3983{
3984 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
3985 {
3986 ValueObject* root(GetRoot());
3987 if (root != this)
3988 return root->GetAddressTypeOfChildren();
3989 }
3990 return m_address_type_of_ptr_or_ref_children;
3991}
3992
3993lldb::DynamicValueType
3994ValueObject::GetDynamicValueType ()
3995{
3996 ValueObject* with_dv_info = this;
3997 while (with_dv_info)
3998 {
3999 if (with_dv_info->HasDynamicValueTypeInfo())
4000 return with_dv_info->GetDynamicValueTypeImpl();
4001 with_dv_info = with_dv_info->m_parent;
4002 }
4003 return lldb::eNoDynamicValues;
4004}
Enrico Granata39d51412013-05-31 17:43:40 +00004005
Enrico Granata4873e522013-04-11 22:48:58 +00004006lldb::Format
4007ValueObject::GetFormat () const
4008{
4009 const ValueObject* with_fmt_info = this;
4010 while (with_fmt_info)
4011 {
4012 if (with_fmt_info->m_format != lldb::eFormatDefault)
4013 return with_fmt_info->m_format;
4014 with_fmt_info = with_fmt_info->m_parent;
4015 }
4016 return m_format;
4017}