blob: 19dfdee5b2a0da6b4922f5c26755231578936cac [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"
37
Greg Clayton7fb56d02011-02-01 01:31:41 +000038#include "lldb/Host/Endian.h"
39
Enrico Granata61a80ba2011-08-12 16:42:31 +000040#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granataf2bbf712011-07-15 02:26:42 +000041#include "lldb/Interpreter/ScriptInterpreterPython.h"
42
Greg Claytone1a916a2010-07-21 22:12:05 +000043#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Symbol/ClangASTContext.h"
45#include "lldb/Symbol/Type.h"
46
Jim Ingham53c47f12010-09-10 23:12:17 +000047#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000048#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Target/Process.h"
51#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000052#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054
55using namespace lldb;
56using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000057using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000058
Greg Claytonafacd142011-09-02 01:15:17 +000059static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060
61//----------------------------------------------------------------------
62// ValueObject constructor
63//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000064ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000066 m_parent (&parent),
Enrico Granata4873e522013-04-11 22:48:58 +000067 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000068 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069 m_name (),
70 m_data (),
71 m_value (),
72 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000073 m_value_str (),
74 m_old_value_str (),
75 m_location_str (),
76 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000077 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000078 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000079 m_children (),
80 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000081 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000082 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000083 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000084 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000085 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +000086 m_type_summary_sp(),
87 m_type_format_sp(),
88 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +000089 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000090 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
Greg Clayton288bdf92010-09-02 02:59:18 +000091 m_value_is_valid (false),
92 m_value_did_change (false),
93 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000094 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +000095 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +000096 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +000097 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +000098 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +000099 m_is_getting_summary(false),
100 m_did_calculate_complete_objc_class_type(false)
Jim Ingham6035b672011-03-31 00:19:25 +0000101{
Jim Ingham58b59f92011-04-22 23:53:53 +0000102 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000103}
104
105//----------------------------------------------------------------------
106// ValueObject constructor
107//----------------------------------------------------------------------
Enrico Granata9128ee22011-09-06 19:20:51 +0000108ValueObject::ValueObject (ExecutionContextScope *exe_scope,
109 AddressType child_ptr_or_ref_addr_type) :
Jim Ingham6035b672011-03-31 00:19:25 +0000110 UserID (++g_value_obj_uid), // Unique identifier for every value object
111 m_parent (NULL),
Enrico Granata4873e522013-04-11 22:48:58 +0000112 m_root (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000113 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000114 m_name (),
115 m_data (),
116 m_value (),
117 m_error (),
118 m_value_str (),
119 m_old_value_str (),
120 m_location_str (),
121 m_summary_str (),
122 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000123 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000124 m_children (),
125 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000126 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000127 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000128 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000129 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000130 m_last_format_mgr_revision(0),
Enrico Granata0c489f52012-03-01 04:24:26 +0000131 m_type_summary_sp(),
132 m_type_format_sp(),
133 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +0000134 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000135 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
Jim Ingham6035b672011-03-31 00:19:25 +0000136 m_value_is_valid (false),
137 m_value_did_change (false),
138 m_children_count_valid (false),
139 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000140 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000141 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000142 m_is_bitfield_for_scalar(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000143 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000144 m_is_getting_summary(false),
145 m_did_calculate_complete_objc_class_type(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000146{
Jim Ingham58b59f92011-04-22 23:53:53 +0000147 m_manager = new ValueObjectManager();
148 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000149}
150
151//----------------------------------------------------------------------
152// Destructor
153//----------------------------------------------------------------------
154ValueObject::~ValueObject ()
155{
156}
157
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000159ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160{
Enrico Granata4becb372011-06-29 22:27:15 +0000161
Enrico Granata9128ee22011-09-06 19:20:51 +0000162 bool did_change_formats = false;
163
Enrico Granata0a3958e2011-07-02 00:25:22 +0000164 if (update_format)
Enrico Granata5548cb52013-01-28 23:47:25 +0000165 did_change_formats = UpdateFormatsIfNeeded();
Enrico Granata4becb372011-06-29 22:27:15 +0000166
Greg Claytonb71f3842010-10-05 03:13:51 +0000167 // If this is a constant value, then our success is predicated on whether
168 // we have an error or not
169 if (GetIsConstant())
Enrico Granata9128ee22011-09-06 19:20:51 +0000170 {
171 // if you were asked to update your formatters, but did not get a chance to do it
172 // clear your own values (this serves the purpose of faking a stop-id for frozen
173 // objects (which are regarded as constant, but could have changes behind their backs
174 // because of the frozen-pointer depth limit)
175 // TODO: decouple summary from value and then remove this code and only force-clear the summary
176 if (update_format && !did_change_formats)
Enrico Granata86cc9822012-03-19 22:58:49 +0000177 ClearUserVisibleData(eClearUserVisibleDataItemsSummary);
Greg Claytonb71f3842010-10-05 03:13:51 +0000178 return m_error.Success();
Enrico Granata9128ee22011-09-06 19:20:51 +0000179 }
Greg Claytonb71f3842010-10-05 03:13:51 +0000180
Jim Ingham6035b672011-03-31 00:19:25 +0000181 bool first_update = m_update_point.IsFirstEvaluation();
182
183 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 {
Jim Ingham6035b672011-03-31 00:19:25 +0000185 m_update_point.SetUpdated();
186
187 // Save the old value using swap to avoid a string copy which
188 // also will clear our m_value_str
189 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000190 {
Jim Ingham6035b672011-03-31 00:19:25 +0000191 m_old_value_valid = false;
192 }
193 else
194 {
195 m_old_value_valid = true;
196 m_old_value_str.swap (m_value_str);
Enrico Granata86cc9822012-03-19 22:58:49 +0000197 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham6035b672011-03-31 00:19:25 +0000198 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000199
Enrico Granataf2bbf712011-07-15 02:26:42 +0000200 ClearUserVisibleData();
201
Greg Claytonefbc7d22012-03-09 04:23:44 +0000202 if (IsInScope())
Jim Ingham6035b672011-03-31 00:19:25 +0000203 {
Greg Claytonefbc7d22012-03-09 04:23:44 +0000204 const bool value_was_valid = GetValueIsValid();
205 SetValueDidChange (false);
206
207 m_error.Clear();
208
209 // Call the pure virtual function to update the value
210 bool success = UpdateValue ();
211
212 SetValueIsValid (success);
213
214 if (first_update)
215 SetValueDidChange (false);
216 else if (!m_value_did_change && success == false)
217 {
218 // The value wasn't gotten successfully, so we mark this
219 // as changed if the value used to be valid and now isn't
220 SetValueDidChange (value_was_valid);
221 }
222 }
223 else
224 {
225 m_error.SetErrorString("out of scope");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226 }
227 }
228 return m_error.Success();
229}
230
Enrico Granata9128ee22011-09-06 19:20:51 +0000231bool
Enrico Granata5548cb52013-01-28 23:47:25 +0000232ValueObject::UpdateFormatsIfNeeded()
Enrico Granata4becb372011-06-29 22:27:15 +0000233{
Greg Clayton5160ce52013-03-27 23:08:40 +0000234 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
Enrico Granata6f3533f2011-07-29 19:53:35 +0000235 if (log)
Enrico Granatad2284832012-10-17 22:23:56 +0000236 log->Printf("[%s %p] checking for FormatManager revisions. ValueObject rev: %d - Global rev: %d",
Enrico Granata6f3533f2011-07-29 19:53:35 +0000237 GetName().GetCString(),
Enrico Granatad2284832012-10-17 22:23:56 +0000238 this,
Enrico Granata4becb372011-06-29 22:27:15 +0000239 m_last_format_mgr_revision,
Enrico Granata85933ed2011-08-18 16:38:26 +0000240 DataVisualization::GetCurrentRevision());
Enrico Granata9128ee22011-09-06 19:20:51 +0000241
242 bool any_change = false;
243
Enrico Granata5548cb52013-01-28 23:47:25 +0000244 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()))
Enrico Granata4becb372011-06-29 22:27:15 +0000245 {
Enrico Granata78d06382011-09-09 23:33:14 +0000246 SetValueFormat(DataVisualization::ValueFormats::GetFormat (*this, eNoDynamicValues));
Enrico Granata5548cb52013-01-28 23:47:25 +0000247 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000248#ifndef LLDB_DISABLE_PYTHON
Enrico Granata5548cb52013-01-28 23:47:25 +0000249 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, GetDynamicValueType()));
Jason Molenda7a9a72b2012-05-16 00:38:08 +0000250#endif
Enrico Granata1490c6f2011-07-19 02:34:21 +0000251
Enrico Granata85933ed2011-08-18 16:38:26 +0000252 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
Enrico Granata855cd902011-09-06 22:59:55 +0000253
254 any_change = true;
Enrico Granata4becb372011-06-29 22:27:15 +0000255 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000256
257 return any_change;
258
Enrico Granata4becb372011-06-29 22:27:15 +0000259}
260
Jim Ingham16e0c682011-08-12 23:34:31 +0000261void
262ValueObject::SetNeedsUpdate ()
263{
264 m_update_point.SetNeedsUpdate();
265 // We have to clear the value string here so ConstResult children will notice if their values are
266 // changed by hand (i.e. with SetValueAsCString).
Enrico Granata86cc9822012-03-19 22:58:49 +0000267 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000268}
269
Enrico Granata13ac0e22012-10-17 19:03:34 +0000270void
Enrico Granatae3e91512012-10-22 18:18:36 +0000271ValueObject::ClearDynamicTypeInformation ()
Enrico Granata13ac0e22012-10-17 19:03:34 +0000272{
273 m_did_calculate_complete_objc_class_type = false;
Enrico Granatae3e91512012-10-22 18:18:36 +0000274 m_last_format_mgr_revision = 0;
Enrico Granata13ac0e22012-10-17 19:03:34 +0000275 m_override_type = ClangASTType();
Enrico Granatae3e91512012-10-22 18:18:36 +0000276 SetValueFormat(lldb::TypeFormatImplSP());
277 SetSummaryFormat(lldb::TypeSummaryImplSP());
278 SetSyntheticChildren(lldb::SyntheticChildrenSP());
Enrico Granata13ac0e22012-10-17 19:03:34 +0000279}
280
Sean Callanan72772842012-02-22 23:57:45 +0000281ClangASTType
282ValueObject::MaybeCalculateCompleteType ()
283{
284 ClangASTType ret(GetClangASTImpl(), GetClangTypeImpl());
Sean Callanan356e17c2012-03-30 02:04:38 +0000285
Sean Callanan72772842012-02-22 23:57:45 +0000286 if (m_did_calculate_complete_objc_class_type)
287 {
288 if (m_override_type.IsValid())
289 return m_override_type;
290 else
291 return ret;
292 }
293
294 clang_type_t ast_type(GetClangTypeImpl());
295 clang_type_t class_type;
296 bool is_pointer_type;
297
298 if (ClangASTContext::IsObjCObjectPointerType(ast_type, &class_type))
299 {
300 is_pointer_type = true;
301 }
302 else if (ClangASTContext::IsObjCClassType(ast_type))
303 {
304 is_pointer_type = false;
305 class_type = ast_type;
306 }
307 else
308 {
309 return ret;
310 }
311
312 m_did_calculate_complete_objc_class_type = true;
313
314 if (!class_type)
315 return ret;
316
317 std::string class_name;
318
319 if (!ClangASTContext::GetObjCClassName(class_type, class_name))
320 return ret;
321
322 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
323
324 if (!process_sp)
325 return ret;
326
327 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
328
329 if (!objc_language_runtime)
330 return ret;
331
332 ConstString class_name_cs(class_name.c_str());
333
334 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name_cs);
335
336 if (!complete_objc_class_type_sp)
337 return ret;
338
339 ClangASTType complete_class(complete_objc_class_type_sp->GetClangAST(),
340 complete_objc_class_type_sp->GetClangFullType());
341
342 if (!ClangASTContext::GetCompleteType(complete_class.GetASTContext(),
343 complete_class.GetOpaqueQualType()))
344 return ret;
345
346 if (is_pointer_type)
347 {
348 clang_type_t pointer_type = ClangASTContext::CreatePointerType(complete_class.GetASTContext(),
349 complete_class.GetOpaqueQualType());
350
351 m_override_type = ClangASTType(complete_class.GetASTContext(),
352 pointer_type);
353 }
354 else
355 {
356 m_override_type = complete_class;
357 }
358
Sean Callanan356e17c2012-03-30 02:04:38 +0000359 if (m_override_type.IsValid())
360 return m_override_type;
361 else
362 return ret;
Sean Callanan72772842012-02-22 23:57:45 +0000363}
364
365clang::ASTContext *
366ValueObject::GetClangAST ()
367{
368 ClangASTType type = MaybeCalculateCompleteType();
369
370 return type.GetASTContext();
371}
372
373lldb::clang_type_t
374ValueObject::GetClangType ()
375{
376 ClangASTType type = MaybeCalculateCompleteType();
377
378 return type.GetOpaqueQualType();
379}
380
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381DataExtractor &
382ValueObject::GetDataExtractor ()
383{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000384 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000385 return m_data;
386}
387
388const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000389ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000391 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 return m_error;
393}
394
395const ConstString &
396ValueObject::GetName() const
397{
398 return m_name;
399}
400
401const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000402ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000404 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405 {
406 if (m_location_str.empty())
407 {
408 StreamString sstr;
409
410 switch (m_value.GetValueType())
411 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +0000413 case Value::eValueTypeVector:
Greg Clayton526e5af2010-11-13 03:52:47 +0000414 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415 {
416 RegisterInfo *reg_info = m_value.GetRegisterInfo();
417 if (reg_info)
418 {
419 if (reg_info->name)
420 m_location_str = reg_info->name;
421 else if (reg_info->alt_name)
422 m_location_str = reg_info->alt_name;
Greg Clayton0665a0f2012-10-30 18:18:43 +0000423
424 m_location_str = (reg_info->encoding == lldb::eEncodingVector) ? "vector" : "scalar";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 }
426 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 break;
428
429 case Value::eValueTypeLoadAddress:
430 case Value::eValueTypeFileAddress:
431 case Value::eValueTypeHostAddress:
432 {
433 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
434 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
435 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);
463 scalar = tmp_value.ResolveValue(&exe_ctx, GetClangAST ());
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
Greg Claytonc7bece562013-01-25 18:06:21 +0000605size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000606ValueObject::GetIndexOfChildWithName (const ConstString &name)
607{
608 bool omit_empty_base_classes = true;
609 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000610 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000611 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000612 omit_empty_base_classes);
613}
614
615ValueObjectSP
616ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
617{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000618 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000619 // classes (which really aren't part of the expression path), so we
620 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000622
Greg Claytondea8cb42011-06-29 22:09:02 +0000623 // We may need to update our value if we are dynamic
624 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000625 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000626
627 std::vector<uint32_t> child_indexes;
628 clang::ASTContext *clang_ast = GetClangAST();
629 void *clang_type = GetClangType();
630 bool omit_empty_base_classes = true;
631 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
632 clang_type,
633 name.GetCString(),
634 omit_empty_base_classes,
635 child_indexes);
636 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000638 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
639 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
640
641 child_sp = GetChildAtIndex(*pos, can_create);
642 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000643 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000644 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000645 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000646 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
647 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000648 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000649 else
650 {
651 child_sp.reset();
652 }
653
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 }
655 }
656 return child_sp;
657}
658
659
Greg Claytonc7bece562013-01-25 18:06:21 +0000660size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661ValueObject::GetNumChildren ()
662{
Enrico Granatac5bc4122012-03-27 02:35:13 +0000663 UpdateValueIfNeeded();
Greg Clayton288bdf92010-09-02 02:59:18 +0000664 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 {
666 SetNumChildren (CalculateNumChildren());
667 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000668 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000669}
Greg Clayton4a792072012-10-23 01:50:10 +0000670
671bool
672ValueObject::MightHaveChildren()
673{
Enrico Granatadb8142b2012-10-23 02:07:54 +0000674 bool has_children = false;
Greg Clayton2452ab72013-02-08 22:02:02 +0000675 const uint32_t type_info = GetTypeInfo();
676 if (type_info)
Greg Clayton4a792072012-10-23 01:50:10 +0000677 {
Greg Clayton4a792072012-10-23 01:50:10 +0000678 if (type_info & (ClangASTContext::eTypeHasChildren |
679 ClangASTContext::eTypeIsPointer |
680 ClangASTContext::eTypeIsReference))
681 has_children = true;
682 }
683 else
684 {
685 has_children = GetNumChildren () > 0;
686 }
687 return has_children;
688}
689
690// Should only be called by ValueObject::GetNumChildren()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691void
Greg Claytonc7bece562013-01-25 18:06:21 +0000692ValueObject::SetNumChildren (size_t num_children)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693{
Greg Clayton288bdf92010-09-02 02:59:18 +0000694 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000695 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000696}
697
698void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699ValueObject::SetName (const ConstString &name)
700{
701 m_name = name;
702}
703
Jim Ingham58b59f92011-04-22 23:53:53 +0000704ValueObject *
Greg Claytonc7bece562013-01-25 18:06:21 +0000705ValueObject::CreateChildAtIndex (size_t idx, bool synthetic_array_member, int32_t synthetic_index)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000706{
Jim Ingham2eec4872011-05-07 00:10:58 +0000707 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000708
Greg Claytondea8cb42011-06-29 22:09:02 +0000709 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000710 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000711 std::string child_name_str;
712 uint32_t child_byte_size = 0;
713 int32_t child_byte_offset = 0;
714 uint32_t child_bitfield_bit_size = 0;
715 uint32_t child_bitfield_bit_offset = 0;
716 bool child_is_base_class = false;
717 bool child_is_deref_of_parent = false;
718
719 const bool transparent_pointers = synthetic_array_member == false;
720 clang::ASTContext *clang_ast = GetClangAST();
721 clang_type_t clang_type = GetClangType();
722 clang_type_t child_clang_type;
723
Greg Claytoncc4d0142012-02-17 07:49:44 +0000724 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000725
726 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
727 clang_ast,
728 GetName().GetCString(),
729 clang_type,
730 idx,
731 transparent_pointers,
732 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +0000733 ignore_array_bounds,
Greg Claytondea8cb42011-06-29 22:09:02 +0000734 child_name_str,
735 child_byte_size,
736 child_byte_offset,
737 child_bitfield_bit_size,
738 child_bitfield_bit_offset,
739 child_is_base_class,
740 child_is_deref_of_parent);
Greg Clayton4ef877f2012-12-06 02:33:54 +0000741 if (child_clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000742 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000743 if (synthetic_index)
744 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745
Greg Claytondea8cb42011-06-29 22:09:02 +0000746 ConstString child_name;
747 if (!child_name_str.empty())
748 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749
Greg Claytondea8cb42011-06-29 22:09:02 +0000750 valobj = new ValueObjectChild (*this,
751 clang_ast,
752 child_clang_type,
753 child_name,
754 child_byte_size,
755 child_byte_offset,
756 child_bitfield_bit_size,
757 child_bitfield_bit_offset,
758 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000759 child_is_deref_of_parent,
760 eAddressTypeInvalid);
761 //if (valobj)
762 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
763 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000764
Jim Ingham58b59f92011-04-22 23:53:53 +0000765 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766}
767
Enrico Granata0c489f52012-03-01 04:24:26 +0000768bool
769ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
770 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771{
Enrico Granata0c489f52012-03-01 04:24:26 +0000772 destination.clear();
773
774 // ideally we would like to bail out if passing NULL, but if we do so
775 // we end up not providing the summary for function pointers anymore
776 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
777 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000778
779 m_is_getting_summary = true;
Enrico Granataf18c03e2012-04-04 17:34:10 +0000780
781 // this is a hot path in code and we prefer to avoid setting this string all too often also clearing out other
782 // information that we might care to see in a crash log. might be useful in very specific situations though.
783 /*Host::SetCrashDescriptionWithFormat("Trying to fetch a summary for %s %s. Summary provider's description is %s",
784 GetTypeName().GetCString(),
785 GetName().GetCString(),
786 summary_ptr->GetDescription().c_str());*/
787
Enrico Granata0c489f52012-03-01 04:24:26 +0000788 if (UpdateValueIfNeeded (false))
789 {
790 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000792 if (HasSyntheticValue())
793 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
794 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000795 }
796 else
797 {
798 clang_type_t clang_type = GetClangType();
799
800 // Do some default printout for function pointers
801 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000802 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000803 StreamString sstr;
804 clang_type_t elem_or_pointee_clang_type;
805 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
806 GetClangAST(),
807 &elem_or_pointee_clang_type));
808
809 if (ClangASTContext::IsFunctionPointerType (clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000811 AddressType func_ptr_address_type = eAddressTypeInvalid;
812 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
813 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000814 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000815 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000816 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000817 case eAddressTypeInvalid:
818 case eAddressTypeFile:
819 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000820
Greg Claytoncc4d0142012-02-17 07:49:44 +0000821 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000822 {
823 ExecutionContext exe_ctx (GetExecutionContextRef());
824
825 Address so_addr;
826 Target *target = exe_ctx.GetTargetPtr();
827 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000828 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000829 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000830 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000831 so_addr.Dump (&sstr,
832 exe_ctx.GetBestExecutionContextScope(),
833 Address::DumpStyleResolvedDescription,
834 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000835 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000836 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000837 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000838 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000839
Greg Claytoncc4d0142012-02-17 07:49:44 +0000840 case eAddressTypeHost:
841 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000842 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000843 }
844 if (sstr.GetSize() > 0)
845 {
846 destination.assign (1, '(');
847 destination.append (sstr.GetData(), sstr.GetSize());
848 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 }
850 }
851 }
852 }
853 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000854 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000855 return !destination.empty();
856}
857
858const char *
859ValueObject::GetSummaryAsCString ()
860{
861 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
862 {
863 GetSummaryAsCString(GetSummaryFormat().get(),
864 m_summary_str);
865 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 if (m_summary_str.empty())
867 return NULL;
868 return m_summary_str.c_str();
869}
870
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000871bool
872ValueObject::IsCStringContainer(bool check_pointer)
873{
874 clang_type_t elem_or_pointee_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000875 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000876 bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
Greg Clayton2452ab72013-02-08 22:02:02 +0000877 ClangASTContext::IsCharType (elem_or_pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000878 if (!is_char_arr_ptr)
879 return false;
880 if (!check_pointer)
881 return true;
882 if (type_flags.Test(ClangASTContext::eTypeIsArray))
883 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000884 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000885 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000886 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000887 return (cstr_address != LLDB_INVALID_ADDRESS);
888}
889
Enrico Granata9128ee22011-09-06 19:20:51 +0000890size_t
891ValueObject::GetPointeeData (DataExtractor& data,
892 uint32_t item_idx,
893 uint32_t item_count)
894{
Greg Clayton2452ab72013-02-08 22:02:02 +0000895 clang_type_t pointee_or_element_clang_type;
896 const uint32_t type_info = GetTypeInfo (&pointee_or_element_clang_type);
897 const bool is_pointer_type = type_info & ClangASTContext::eTypeIsPointer;
898 const bool is_array_type = type_info & ClangASTContext::eTypeIsArray;
899 if (!(is_pointer_type || is_array_type))
Enrico Granata9128ee22011-09-06 19:20:51 +0000900 return 0;
901
902 if (item_count == 0)
903 return 0;
904
Greg Clayton2452ab72013-02-08 22:02:02 +0000905 clang::ASTContext *ast = GetClangAST();
906 ClangASTType pointee_or_element_type(ast, pointee_or_element_clang_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000907
Greg Clayton2452ab72013-02-08 22:02:02 +0000908 const uint64_t item_type_size = pointee_or_element_type.GetClangTypeByteSize();
Enrico Granata9128ee22011-09-06 19:20:51 +0000909
910 const uint64_t bytes = item_count * item_type_size;
911
912 const uint64_t offset = item_idx * item_type_size;
913
914 if (item_idx == 0 && item_count == 1) // simply a deref
915 {
Greg Clayton2452ab72013-02-08 22:02:02 +0000916 if (is_pointer_type)
Enrico Granata9128ee22011-09-06 19:20:51 +0000917 {
918 Error error;
919 ValueObjectSP pointee_sp = Dereference(error);
920 if (error.Fail() || pointee_sp.get() == NULL)
921 return 0;
922 return pointee_sp->GetDataExtractor().Copy(data);
923 }
924 else
925 {
926 ValueObjectSP child_sp = GetChildAtIndex(0, true);
927 if (child_sp.get() == NULL)
928 return 0;
929 return child_sp->GetDataExtractor().Copy(data);
930 }
931 return true;
932 }
933 else /* (items > 1) */
934 {
935 Error error;
936 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
937 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
938
939 AddressType addr_type;
Greg Clayton2452ab72013-02-08 22:02:02 +0000940 lldb::addr_t addr = is_pointer_type ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
Enrico Granata9128ee22011-09-06 19:20:51 +0000941
Enrico Granata9128ee22011-09-06 19:20:51 +0000942 switch (addr_type)
943 {
944 case eAddressTypeFile:
945 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000946 ModuleSP module_sp (GetModule());
947 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +0000948 {
Enrico Granata9c2efe32012-08-07 01:49:34 +0000949 addr = addr + offset;
Enrico Granata9128ee22011-09-06 19:20:51 +0000950 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +0000951 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000952 ExecutionContext exe_ctx (GetExecutionContextRef());
953 Target* target = exe_ctx.GetTargetPtr();
954 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +0000955 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000956 heap_buf_ptr->SetByteSize(bytes);
957 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
958 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +0000959 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000960 data.SetData(data_sp);
961 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +0000962 }
963 }
964 }
965 }
966 break;
967 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +0000968 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000969 ExecutionContext exe_ctx (GetExecutionContextRef());
970 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +0000971 if (process)
972 {
973 heap_buf_ptr->SetByteSize(bytes);
974 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
975 if (error.Success())
976 {
977 data.SetData(data_sp);
978 return bytes_read;
979 }
980 }
981 }
982 break;
983 case eAddressTypeHost:
984 {
Greg Clayton2452ab72013-02-08 22:02:02 +0000985 ClangASTType valobj_type(ast, GetClangType());
986 uint64_t max_bytes = valobj_type.GetClangTypeByteSize();
987 if (max_bytes > offset)
988 {
989 size_t bytes_read = std::min<uint64_t>(max_bytes - offset, bytes);
990 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes_read);
991 data.SetData(data_sp);
992 return bytes_read;
993 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000994 }
995 break;
996 case eAddressTypeInvalid:
Enrico Granata9128ee22011-09-06 19:20:51 +0000997 break;
998 }
999 }
1000 return 0;
1001}
1002
Greg Claytonfaac1112013-03-14 18:31:44 +00001003uint64_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001004ValueObject::GetData (DataExtractor& data)
1005{
1006 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001007 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytone72dfb32012-02-24 01:59:29 +00001008 Error error = m_value.GetValueAsData(&exe_ctx, GetClangAST(), data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +00001009 if (error.Fail())
1010 return 0;
1011 data.SetAddressByteSize(m_data.GetAddressByteSize());
1012 data.SetByteOrder(m_data.GetByteOrder());
1013 return data.GetByteSize();
1014}
1015
Sean Callanan389823e2013-04-13 01:21:23 +00001016bool
1017ValueObject::SetData (DataExtractor &data, Error &error)
1018{
1019 error.Clear();
1020 // Make sure our value is up to date first so that our location and location
1021 // type is valid.
1022 if (!UpdateValueIfNeeded(false))
1023 {
1024 error.SetErrorString("unable to read value");
1025 return false;
1026 }
1027
1028 uint64_t count = 0;
1029 Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
1030
1031 const size_t byte_size = GetByteSize();
1032
1033 Value::ValueType value_type = m_value.GetValueType();
1034
1035 switch (value_type)
1036 {
1037 case Value::eValueTypeScalar:
1038 {
1039 Error set_error = m_value.GetScalar().SetValueFromData(data, encoding, byte_size);
1040
1041 if (!set_error.Success())
1042 {
1043 error.SetErrorStringWithFormat("unable to set scalar value: %s", set_error.AsCString());
1044 return false;
1045 }
1046 }
1047 break;
1048 case Value::eValueTypeLoadAddress:
1049 {
1050 // If it is a load address, then the scalar value is the storage location
1051 // of the data, and we have to shove this value down to that load location.
1052 ExecutionContext exe_ctx (GetExecutionContextRef());
1053 Process *process = exe_ctx.GetProcessPtr();
1054 if (process)
1055 {
1056 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1057 size_t bytes_written = process->WriteMemory(target_addr,
1058 data.GetDataStart(),
1059 byte_size,
1060 error);
1061 if (!error.Success())
1062 return false;
1063 if (bytes_written != byte_size)
1064 {
1065 error.SetErrorString("unable to write value to memory");
1066 return false;
1067 }
1068 }
1069 }
1070 break;
1071 case Value::eValueTypeHostAddress:
1072 {
1073 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1074 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1075 m_data.SetData(buffer_sp, 0);
1076 data.CopyByteOrderedData (0,
1077 byte_size,
1078 const_cast<uint8_t *>(m_data.GetDataStart()),
1079 byte_size,
1080 m_data.GetByteOrder());
1081 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1082 }
1083 break;
1084 case Value::eValueTypeFileAddress:
1085 case Value::eValueTypeVector:
1086 break;
1087 }
1088
1089 // If we have reached this point, then we have successfully changed the value.
1090 SetNeedsUpdate();
1091 return true;
1092}
1093
Enrico Granata9128ee22011-09-06 19:20:51 +00001094// will compute strlen(str), but without consuming more than
1095// maxlen bytes out of str (this serves the purpose of reading
1096// chunks of a string without having to worry about
1097// missing NULL terminators in the chunk)
1098// of course, if strlen(str) > maxlen, the function will return
1099// maxlen_value (which should be != maxlen, because that allows you
1100// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
1101static uint32_t
1102strlen_or_inf (const char* str,
1103 uint32_t maxlen,
1104 uint32_t maxlen_value)
1105{
1106 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +00001107 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +00001108 {
Greg Clayton8dd5c172011-10-05 22:19:51 +00001109 while(*str)
1110 {
1111 len++;str++;
Greg Clayton2452ab72013-02-08 22:02:02 +00001112 if (len >= maxlen)
Greg Clayton8dd5c172011-10-05 22:19:51 +00001113 return maxlen_value;
1114 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001115 }
1116 return len;
1117}
1118
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001119size_t
Greg Claytoncc4d0142012-02-17 07:49:44 +00001120ValueObject::ReadPointedString (Stream& s,
1121 Error& error,
1122 uint32_t max_length,
1123 bool honor_array,
1124 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001125{
Greg Claytoncc4d0142012-02-17 07:49:44 +00001126 ExecutionContext exe_ctx (GetExecutionContextRef());
1127 Target* target = exe_ctx.GetTargetPtr();
1128
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001129 if (!target)
1130 {
1131 s << "<no target to read from>";
1132 error.SetErrorString("no target to read from");
1133 return 0;
1134 }
1135
1136 if (max_length == 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001137 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001138
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001139 size_t bytes_read = 0;
1140 size_t total_bytes_read = 0;
1141
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001142 clang_type_t clang_type = GetClangType();
1143 clang_type_t elem_or_pointee_clang_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001144 const Flags type_flags (GetTypeInfo (&elem_or_pointee_clang_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001145 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
1146 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
1147 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001148 addr_t cstr_address = LLDB_INVALID_ADDRESS;
1149 AddressType cstr_address_type = eAddressTypeInvalid;
1150
1151 size_t cstr_len = 0;
1152 bool capped_data = false;
1153 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Greg Claytoncc4d0142012-02-17 07:49:44 +00001154 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001155 // We have an array
1156 cstr_len = ClangASTContext::GetArraySize (clang_type);
1157 if (cstr_len > max_length)
1158 {
1159 capped_data = true;
1160 cstr_len = max_length;
1161 }
1162 cstr_address = GetAddressOf (true, &cstr_address_type);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001163 }
1164 else
1165 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001166 // We have a pointer
1167 cstr_address = GetPointerValue (&cstr_address_type);
1168 }
1169
1170 if (cstr_address == 0 || cstr_address == LLDB_INVALID_ADDRESS)
1171 {
1172 s << "<invalid address>";
1173 error.SetErrorString("invalid address");
1174 return 0;
1175 }
1176
1177 Address cstr_so_addr (cstr_address);
1178 DataExtractor data;
1179 if (cstr_len > 0 && honor_array)
1180 {
1181 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1182 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1183 GetPointeeData(data, 0, cstr_len);
1184
1185 if ((bytes_read = data.GetByteSize()) > 0)
1186 {
1187 total_bytes_read = bytes_read;
1188 s << '"';
1189 data.Dump (&s,
1190 0, // Start offset in "data"
1191 item_format,
1192 1, // Size of item (1 byte for a char!)
1193 bytes_read, // How many bytes to print?
1194 UINT32_MAX, // num per line
1195 LLDB_INVALID_ADDRESS,// base address
1196 0, // bitfield bit size
1197 0); // bitfield bit offset
1198 if (capped_data)
1199 s << "...";
1200 s << '"';
1201 }
1202 }
1203 else
1204 {
1205 cstr_len = max_length;
1206 const size_t k_max_buf_size = 64;
1207
1208 size_t offset = 0;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001209
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001210 int cstr_len_displayed = -1;
1211 bool capped_cstr = false;
1212 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1213 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1214 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001215 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001216 total_bytes_read += bytes_read;
1217 const char *cstr = data.PeekCStr(0);
1218 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1219 if (len > k_max_buf_size)
1220 len = k_max_buf_size;
1221 if (cstr && cstr_len_displayed < 0)
1222 s << '"';
1223
1224 if (cstr_len_displayed < 0)
1225 cstr_len_displayed = len;
1226
1227 if (len == 0)
1228 break;
1229 cstr_len_displayed += len;
1230 if (len > bytes_read)
1231 len = bytes_read;
1232 if (len > cstr_len)
1233 len = cstr_len;
1234
1235 data.Dump (&s,
1236 0, // Start offset in "data"
1237 item_format,
1238 1, // Size of item (1 byte for a char!)
1239 len, // How many bytes to print?
1240 UINT32_MAX, // num per line
1241 LLDB_INVALID_ADDRESS,// base address
1242 0, // bitfield bit size
1243 0); // bitfield bit offset
1244
1245 if (len < k_max_buf_size)
1246 break;
1247
1248 if (len >= cstr_len)
Enrico Granata6f3533f2011-07-29 19:53:35 +00001249 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001250 capped_cstr = true;
1251 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001252 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001253
1254 cstr_len -= len;
1255 offset += len;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001256 }
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001257
1258 if (cstr_len_displayed >= 0)
Greg Claytoncc4d0142012-02-17 07:49:44 +00001259 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001260 s << '"';
1261 if (capped_cstr)
1262 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001263 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001264 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001265 }
1266 else
1267 {
Enrico Granata7e0db2a2013-02-28 22:01:33 +00001268 error.SetErrorString("not a string object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001269 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001270 }
Enrico Granataea2bc0f2013-02-21 19:57:10 +00001271 return total_bytes_read;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001272}
1273
Jim Ingham53c47f12010-09-10 23:12:17 +00001274const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001275ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001276{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001277
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001278 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001279 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001280
1281 if (!m_object_desc_str.empty())
1282 return m_object_desc_str.c_str();
1283
Greg Claytoncc4d0142012-02-17 07:49:44 +00001284 ExecutionContext exe_ctx (GetExecutionContextRef());
1285 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001286 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001287 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001288
Jim Ingham53c47f12010-09-10 23:12:17 +00001289 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001290
Greg Claytonafacd142011-09-02 01:15:17 +00001291 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001292 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1293
Jim Inghama2cf2632010-12-23 02:29:54 +00001294 if (runtime == NULL)
1295 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001296 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +00001297 clang_type_t opaque_qual_type = GetClangType();
1298 if (opaque_qual_type != NULL)
1299 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001300 bool is_signed;
1301 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
1302 || ClangASTContext::IsPointerType (opaque_qual_type))
1303 {
Greg Claytonafacd142011-09-02 01:15:17 +00001304 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001305 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001306 }
1307 }
1308
Jim Ingham8d543de2011-03-31 23:01:21 +00001309 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001310 {
1311 m_object_desc_str.append (s.GetData());
1312 }
Sean Callanan672ad942010-10-23 00:18:49 +00001313
1314 if (m_object_desc_str.empty())
1315 return NULL;
1316 else
1317 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001318}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319
Enrico Granata0c489f52012-03-01 04:24:26 +00001320bool
1321ValueObject::GetValueAsCString (lldb::Format format,
1322 std::string& destination)
1323{
1324 if (ClangASTContext::IsAggregateType (GetClangType()) == false &&
1325 UpdateValueIfNeeded(false))
1326 {
1327 const Value::ContextType context_type = m_value.GetContextType();
1328
1329 switch (context_type)
1330 {
1331 case Value::eContextTypeClangType:
1332 case Value::eContextTypeLLDBType:
1333 case Value::eContextTypeVariable:
1334 {
1335 clang_type_t clang_type = GetClangType ();
1336 if (clang_type)
1337 {
Enrico Granata852cce72013-03-23 01:12:38 +00001338 // put custom bytes to display in this DataExtractor to override the default value logic
1339 lldb_private::DataExtractor special_format_data;
1340 clang::ASTContext* ast = GetClangAST();
Enrico Granata123c39c2013-03-23 01:44:23 +00001341 if (format == eFormatCString)
Enrico Granata852cce72013-03-23 01:12:38 +00001342 {
Enrico Granata9d71afe2013-03-23 01:44:59 +00001343 Flags type_flags(ClangASTContext::GetTypeInfo(clang_type, ast, NULL));
Enrico Granata123c39c2013-03-23 01:44:23 +00001344 if (type_flags.Test(ClangASTContext::eTypeIsPointer) && !type_flags.Test(ClangASTContext::eTypeIsObjC))
Enrico Granata852cce72013-03-23 01:12:38 +00001345 {
1346 // if we are dumping a pointer as a c-string, get the pointee data as a string
1347 TargetSP target_sp(GetTargetSP());
1348 if (target_sp)
1349 {
1350 size_t max_len = target_sp->GetMaximumSizeOfStringSummary();
1351 Error error;
1352 DataBufferSP buffer_sp(new DataBufferHeap(max_len+1,0));
1353 Address address(GetPointerValue());
1354 if (target_sp->ReadCStringFromMemory(address, (char*)buffer_sp->GetBytes(), max_len, error) && error.Success())
1355 special_format_data.SetData(buffer_sp);
1356 }
1357 }
1358 }
1359
Enrico Granata0c489f52012-03-01 04:24:26 +00001360 StreamString sstr;
1361 ExecutionContext exe_ctx (GetExecutionContextRef());
Enrico Granata852cce72013-03-23 01:12:38 +00001362 ClangASTType::DumpTypeValue (ast, // The clang AST
1363 clang_type, // The clang type to display
1364 &sstr, // The stream to use for display
1365 format, // Format to display this type with
1366 special_format_data.GetByteSize() ?
1367 special_format_data: m_data, // Data to extract from
1368 0, // Byte offset into "m_data"
1369 GetByteSize(), // Byte size of item in "m_data"
1370 GetBitfieldBitSize(), // Bitfield bit size
1371 GetBitfieldBitOffset(), // Bitfield bit offset
Enrico Granata0c489f52012-03-01 04:24:26 +00001372 exe_ctx.GetBestExecutionContextScope());
1373 // Don't set the m_error to anything here otherwise
1374 // we won't be able to re-format as anything else. The
1375 // code for ClangASTType::DumpTypeValue() should always
1376 // return something, even if that something contains
1377 // an error messsage. "m_error" is used to detect errors
1378 // when reading the valid object, not for formatting errors.
1379 if (sstr.GetString().empty())
1380 destination.clear();
1381 else
1382 destination.swap(sstr.GetString());
1383 }
1384 }
1385 break;
1386
1387 case Value::eContextTypeRegisterInfo:
1388 {
1389 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1390 if (reg_info)
1391 {
1392 ExecutionContext exe_ctx (GetExecutionContextRef());
1393
1394 StreamString reg_sstr;
1395 m_data.Dump (&reg_sstr,
1396 0,
1397 format,
1398 reg_info->byte_size,
1399 1,
1400 UINT32_MAX,
1401 LLDB_INVALID_ADDRESS,
1402 0,
1403 0,
1404 exe_ctx.GetBestExecutionContextScope());
1405 destination.swap(reg_sstr.GetString());
1406 }
1407 }
1408 break;
1409
1410 default:
1411 break;
1412 }
1413 return !destination.empty();
1414 }
1415 else
1416 return false;
1417}
1418
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001420ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001421{
Enrico Granata0c489f52012-03-01 04:24:26 +00001422 if (UpdateValueIfNeeded(true) && m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001424 lldb::Format my_format = GetFormat();
Enrico Granatac953a6a2012-12-11 02:17:22 +00001425 if (my_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001426 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001427 if (m_type_format_sp)
1428 my_format = m_type_format_sp->GetFormat();
1429 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001430 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001431 if (m_is_bitfield_for_scalar)
1432 my_format = eFormatUnsigned;
1433 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001434 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001435 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001436 {
1437 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1438 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001439 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001440 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001441 else
1442 {
1443 clang_type_t clang_type = GetClangType ();
1444 my_format = ClangASTType::GetFormat(clang_type);
1445 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001446 }
1447 }
1448 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001449 if (GetValueAsCString(my_format, m_value_str))
1450 {
1451 if (!m_value_did_change && m_old_value_valid)
1452 {
1453 // The value was gotten successfully, so we consider the
1454 // value as changed if the value string differs
1455 SetValueDidChange (m_old_value_str != m_value_str);
1456 }
1457 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001458 }
1459 if (m_value_str.empty())
1460 return NULL;
1461 return m_value_str.c_str();
1462}
1463
Enrico Granatac3e320a2011-08-02 17:27:39 +00001464// if > 8bytes, 0 is returned. this method should mostly be used
1465// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001466uint64_t
Johnny Chen3f476c42012-06-05 19:37:43 +00001467ValueObject::GetValueAsUnsigned (uint64_t fail_value, bool *success)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001468{
1469 // If our byte size is zero this is an aggregate type that has children
1470 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
1471 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001472 Scalar scalar;
1473 if (ResolveValue (scalar))
Johnny Chen3f476c42012-06-05 19:37:43 +00001474 {
1475 if (success)
1476 *success = true;
Enrico Granata48ea80f2012-10-24 20:24:39 +00001477 return scalar.ULongLong(fail_value);
Johnny Chen3f476c42012-06-05 19:37:43 +00001478 }
1479 // fallthrough, otherwise...
Enrico Granatac3e320a2011-08-02 17:27:39 +00001480 }
Johnny Chen3f476c42012-06-05 19:37:43 +00001481
1482 if (success)
1483 *success = false;
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001484 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001485}
1486
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001487// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1488// this call up to date by returning true for your new special cases. We will eventually move
1489// to checking this call result before trying to display special cases
1490bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001491ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1492 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001493{
1494 clang_type_t elem_or_pointee_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001495 Flags flags(GetTypeInfo(&elem_or_pointee_type));
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001496
1497 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001498 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001499 {
1500 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001501 (custom_format == eFormatCString ||
1502 custom_format == eFormatCharArray ||
1503 custom_format == eFormatChar ||
1504 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001505 return true;
1506
1507 if (flags.Test(ClangASTContext::eTypeIsArray))
1508 {
Greg Claytonafacd142011-09-02 01:15:17 +00001509 if ((custom_format == eFormatBytes) ||
1510 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001511 return true;
1512
Greg Claytonafacd142011-09-02 01:15:17 +00001513 if ((custom_format == eFormatVectorOfChar) ||
1514 (custom_format == eFormatVectorOfFloat32) ||
1515 (custom_format == eFormatVectorOfFloat64) ||
1516 (custom_format == eFormatVectorOfSInt16) ||
1517 (custom_format == eFormatVectorOfSInt32) ||
1518 (custom_format == eFormatVectorOfSInt64) ||
1519 (custom_format == eFormatVectorOfSInt8) ||
1520 (custom_format == eFormatVectorOfUInt128) ||
1521 (custom_format == eFormatVectorOfUInt16) ||
1522 (custom_format == eFormatVectorOfUInt32) ||
1523 (custom_format == eFormatVectorOfUInt64) ||
1524 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001525 return true;
1526 }
1527 }
1528 return false;
1529}
1530
Enrico Granata9fc19442011-07-06 02:13:41 +00001531bool
1532ValueObject::DumpPrintableRepresentation(Stream& s,
1533 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001534 Format custom_format,
Enrico Granata86cc9822012-03-19 22:58:49 +00001535 PrintableRepresentationSpecialCases special)
Enrico Granata9fc19442011-07-06 02:13:41 +00001536{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001537
1538 clang_type_t elem_or_pointee_type;
Greg Clayton2452ab72013-02-08 22:02:02 +00001539 Flags flags(GetTypeInfo(&elem_or_pointee_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001540
Enrico Granata86cc9822012-03-19 22:58:49 +00001541 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1542 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1543
1544 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001545 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001546 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1547 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001548 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001549 // when being asked to get a printable display an array or pointer type directly,
1550 // try to "do the right thing"
1551
1552 if (IsCStringContainer(true) &&
1553 (custom_format == eFormatCString ||
1554 custom_format == eFormatCharArray ||
1555 custom_format == eFormatChar ||
1556 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001557 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001558 Error error;
1559 ReadPointedString(s,
1560 error,
1561 0,
1562 (custom_format == eFormatVectorOfChar) ||
1563 (custom_format == eFormatCharArray));
1564 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001565 }
1566
Enrico Granata86cc9822012-03-19 22:58:49 +00001567 if (custom_format == eFormatEnum)
1568 return false;
1569
1570 // this only works for arrays, because I have no way to know when
1571 // the pointed memory ends, and no special \0 end of data marker
1572 if (flags.Test(ClangASTContext::eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001573 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001574 if ((custom_format == eFormatBytes) ||
1575 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001576 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001577 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001578
1579 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001580 for (size_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001581 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001582
1583 if (low)
1584 s << ',';
1585
1586 ValueObjectSP child = GetChildAtIndex(low,true);
1587 if (!child.get())
1588 {
1589 s << "<invalid child>";
1590 continue;
1591 }
1592 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1593 }
1594
1595 s << ']';
1596
1597 return true;
1598 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001599
Enrico Granata86cc9822012-03-19 22:58:49 +00001600 if ((custom_format == eFormatVectorOfChar) ||
1601 (custom_format == eFormatVectorOfFloat32) ||
1602 (custom_format == eFormatVectorOfFloat64) ||
1603 (custom_format == eFormatVectorOfSInt16) ||
1604 (custom_format == eFormatVectorOfSInt32) ||
1605 (custom_format == eFormatVectorOfSInt64) ||
1606 (custom_format == eFormatVectorOfSInt8) ||
1607 (custom_format == eFormatVectorOfUInt128) ||
1608 (custom_format == eFormatVectorOfUInt16) ||
1609 (custom_format == eFormatVectorOfUInt32) ||
1610 (custom_format == eFormatVectorOfUInt64) ||
1611 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1612 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001613 const size_t count = GetNumChildren();
Enrico Granata86cc9822012-03-19 22:58:49 +00001614
1615 Format format = FormatManager::GetSingleItemFormat(custom_format);
1616
1617 s << '[';
Greg Claytonc7bece562013-01-25 18:06:21 +00001618 for (size_t low = 0; low < count; low++)
Enrico Granata86cc9822012-03-19 22:58:49 +00001619 {
1620
1621 if (low)
1622 s << ',';
1623
1624 ValueObjectSP child = GetChildAtIndex(low,true);
1625 if (!child.get())
1626 {
1627 s << "<invalid child>";
1628 continue;
1629 }
1630 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1631 }
1632
1633 s << ']';
1634
1635 return true;
1636 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001637 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001638
1639 if ((custom_format == eFormatBoolean) ||
1640 (custom_format == eFormatBinary) ||
1641 (custom_format == eFormatChar) ||
1642 (custom_format == eFormatCharPrintable) ||
1643 (custom_format == eFormatComplexFloat) ||
1644 (custom_format == eFormatDecimal) ||
1645 (custom_format == eFormatHex) ||
Enrico Granata7ec18e32012-08-09 19:33:34 +00001646 (custom_format == eFormatHexUppercase) ||
Enrico Granata86cc9822012-03-19 22:58:49 +00001647 (custom_format == eFormatFloat) ||
1648 (custom_format == eFormatOctal) ||
1649 (custom_format == eFormatOSType) ||
1650 (custom_format == eFormatUnicode16) ||
1651 (custom_format == eFormatUnicode32) ||
1652 (custom_format == eFormatUnsigned) ||
1653 (custom_format == eFormatPointer) ||
1654 (custom_format == eFormatComplexInteger) ||
1655 (custom_format == eFormatComplex) ||
1656 (custom_format == eFormatDefault)) // use the [] operator
1657 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001658 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001659 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001660
1661 if (only_special)
1662 return false;
1663
Enrico Granata86cc9822012-03-19 22:58:49 +00001664 bool var_success = false;
1665
1666 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001667 const char *cstr = NULL;
1668 StreamString strm;
Enrico Granata86cc9822012-03-19 22:58:49 +00001669
1670 if (custom_format != eFormatInvalid)
1671 SetFormat(custom_format);
1672
1673 switch(val_obj_display)
1674 {
1675 case eValueObjectRepresentationStyleValue:
Greg Claytonc7bece562013-01-25 18:06:21 +00001676 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001677 break;
1678
1679 case eValueObjectRepresentationStyleSummary:
Greg Claytonc7bece562013-01-25 18:06:21 +00001680 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001681 break;
1682
1683 case eValueObjectRepresentationStyleLanguageSpecific:
Greg Claytonc7bece562013-01-25 18:06:21 +00001684 cstr = GetObjectDescription();
Enrico Granata86cc9822012-03-19 22:58:49 +00001685 break;
1686
1687 case eValueObjectRepresentationStyleLocation:
Greg Claytonc7bece562013-01-25 18:06:21 +00001688 cstr = GetLocationAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001689 break;
1690
1691 case eValueObjectRepresentationStyleChildrenCount:
Greg Claytonc7bece562013-01-25 18:06:21 +00001692 strm.Printf("%zu", GetNumChildren());
1693 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001694 break;
1695
1696 case eValueObjectRepresentationStyleType:
Greg Claytonc7bece562013-01-25 18:06:21 +00001697 cstr = GetTypeName().AsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001698 break;
Enrico Granata86cc9822012-03-19 22:58:49 +00001699 }
1700
Greg Claytonc7bece562013-01-25 18:06:21 +00001701 if (!cstr)
Enrico Granata86cc9822012-03-19 22:58:49 +00001702 {
1703 if (val_obj_display == eValueObjectRepresentationStyleValue)
Greg Claytonc7bece562013-01-25 18:06:21 +00001704 cstr = GetSummaryAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001705 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1706 {
1707 if (ClangASTContext::IsAggregateType (GetClangType()) == true)
1708 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001709 strm.Printf("%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1710 cstr = strm.GetString().c_str();
Enrico Granata86cc9822012-03-19 22:58:49 +00001711 }
1712 else
Greg Claytonc7bece562013-01-25 18:06:21 +00001713 cstr = GetValueAsCString();
Enrico Granata86cc9822012-03-19 22:58:49 +00001714 }
1715 }
1716
Greg Claytonc7bece562013-01-25 18:06:21 +00001717 if (cstr)
1718 s.PutCString(cstr);
Enrico Granata86cc9822012-03-19 22:58:49 +00001719 else
1720 {
1721 if (m_error.Fail())
1722 s.Printf("<%s>", m_error.AsCString());
1723 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1724 s.PutCString("<no summary available>");
1725 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1726 s.PutCString("<no value available>");
1727 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1728 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1729 else
1730 s.PutCString("<no printable representation>");
1731 }
1732
1733 // we should only return false here if we could not do *anything*
1734 // even if we have an error message as output, that's a success
1735 // from our callers' perspective, so return true
1736 var_success = true;
1737
1738 if (custom_format != eFormatInvalid)
1739 SetFormat(eFormatDefault);
1740 }
1741
Enrico Granataf4efecd2011-07-12 22:56:10 +00001742 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001743}
1744
Greg Clayton737b9322010-09-13 03:32:57 +00001745addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001746ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001747{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001748 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001749 return LLDB_INVALID_ADDRESS;
1750
Greg Clayton73b472d2010-10-27 03:32:59 +00001751 switch (m_value.GetValueType())
1752 {
1753 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001754 case Value::eValueTypeVector:
Greg Clayton73b472d2010-10-27 03:32:59 +00001755 if (scalar_is_load_address)
1756 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001757 if(address_type)
1758 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001759 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1760 }
1761 break;
1762
1763 case Value::eValueTypeLoadAddress:
1764 case Value::eValueTypeFileAddress:
1765 case Value::eValueTypeHostAddress:
1766 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001767 if(address_type)
1768 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001769 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1770 }
1771 break;
1772 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001773 if (address_type)
1774 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001775 return LLDB_INVALID_ADDRESS;
1776}
1777
1778addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001779ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001780{
Greg Claytonafacd142011-09-02 01:15:17 +00001781 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001782 if(address_type)
1783 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001784
Enrico Granatac3e320a2011-08-02 17:27:39 +00001785 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001786 return address;
1787
Greg Clayton73b472d2010-10-27 03:32:59 +00001788 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001789 {
1790 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001791 case Value::eValueTypeVector:
Enrico Granata9128ee22011-09-06 19:20:51 +00001792 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001793 break;
1794
Enrico Granata9128ee22011-09-06 19:20:51 +00001795 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001796 case Value::eValueTypeLoadAddress:
1797 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001798 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001799 lldb::offset_t data_offset = 0;
Greg Clayton737b9322010-09-13 03:32:57 +00001800 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001801 }
1802 break;
1803 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001804
Enrico Granata9128ee22011-09-06 19:20:51 +00001805 if (address_type)
1806 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001807
Greg Clayton737b9322010-09-13 03:32:57 +00001808 return address;
1809}
1810
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001811bool
Enrico Granata07a4ac22012-05-08 21:25:06 +00001812ValueObject::SetValueFromCString (const char *value_str, Error& error)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001813{
Enrico Granata07a4ac22012-05-08 21:25:06 +00001814 error.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001815 // Make sure our value is up to date first so that our location and location
1816 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001817 if (!UpdateValueIfNeeded(false))
Enrico Granata07a4ac22012-05-08 21:25:06 +00001818 {
1819 error.SetErrorString("unable to read value");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001820 return false;
Enrico Granata07a4ac22012-05-08 21:25:06 +00001821 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001822
Greg Claytonfaac1112013-03-14 18:31:44 +00001823 uint64_t count = 0;
Greg Claytonafacd142011-09-02 01:15:17 +00001824 Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001825
Greg Claytonb1320972010-07-14 00:18:15 +00001826 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001827
Jim Ingham16e0c682011-08-12 23:34:31 +00001828 Value::ValueType value_type = m_value.GetValueType();
1829
1830 if (value_type == Value::eValueTypeScalar)
1831 {
1832 // If the value is already a scalar, then let the scalar change itself:
1833 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1834 }
1835 else if (byte_size <= Scalar::GetMaxByteSize())
1836 {
1837 // If the value fits in a scalar, then make a new scalar and again let the
1838 // scalar code do the conversion, then figure out where to put the new value.
1839 Scalar new_scalar;
Jim Ingham16e0c682011-08-12 23:34:31 +00001840 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1841 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001842 {
Jim Ingham4b536182011-08-09 02:12:22 +00001843 switch (value_type)
1844 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001845 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001846 {
1847 // If it is a load address, then the scalar value is the storage location
1848 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001849 ExecutionContext exe_ctx (GetExecutionContextRef());
1850 Process *process = exe_ctx.GetProcessPtr();
1851 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001852 {
Enrico Granata48ea80f2012-10-24 20:24:39 +00001853 addr_t target_addr = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001854 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1855 new_scalar,
1856 byte_size,
1857 error);
Enrico Granata07a4ac22012-05-08 21:25:06 +00001858 if (!error.Success())
1859 return false;
1860 if (bytes_written != byte_size)
1861 {
1862 error.SetErrorString("unable to write value to memory");
1863 return false;
1864 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001865 }
1866 }
Jim Ingham4b536182011-08-09 02:12:22 +00001867 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001868 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001869 {
1870 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1871 DataExtractor new_data;
1872 new_data.SetByteOrder (m_data.GetByteOrder());
1873
1874 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1875 m_data.SetData(buffer_sp, 0);
1876 bool success = new_scalar.GetData(new_data);
1877 if (success)
1878 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001879 new_data.CopyByteOrderedData (0,
1880 byte_size,
1881 const_cast<uint8_t *>(m_data.GetDataStart()),
1882 byte_size,
1883 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001884 }
1885 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1886
1887 }
Jim Ingham4b536182011-08-09 02:12:22 +00001888 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001889 case Value::eValueTypeFileAddress:
1890 case Value::eValueTypeScalar:
Greg Clayton0665a0f2012-10-30 18:18:43 +00001891 case Value::eValueTypeVector:
1892 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001893 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001894 }
1895 else
1896 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001897 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001899 }
1900 else
1901 {
1902 // We don't support setting things bigger than a scalar at present.
Enrico Granata07a4ac22012-05-08 21:25:06 +00001903 error.SetErrorString("unable to write aggregate data type");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001904 return false;
1905 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001906
1907 // If we have reached this point, then we have successfully changed the value.
1908 SetNeedsUpdate();
1909 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001910}
1911
Greg Clayton81e871e2012-02-04 02:27:34 +00001912bool
1913ValueObject::GetDeclaration (Declaration &decl)
1914{
1915 decl.Clear();
1916 return false;
1917}
1918
Greg Clayton84db9102012-03-26 23:03:23 +00001919ConstString
1920ValueObject::GetTypeName()
1921{
1922 return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
1923}
1924
1925ConstString
1926ValueObject::GetQualifiedTypeName()
1927{
1928 return ClangASTType::GetConstQualifiedTypeName (GetClangAST(), GetClangType());
1929}
1930
1931
Greg Claytonafacd142011-09-02 01:15:17 +00001932LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00001933ValueObject::GetObjectRuntimeLanguage ()
1934{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001935 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1936 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001937}
1938
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001939void
Jim Ingham58b59f92011-04-22 23:53:53 +00001940ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001941{
Jim Ingham58b59f92011-04-22 23:53:53 +00001942 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001943}
1944
1945ValueObjectSP
1946ValueObject::GetSyntheticChild (const ConstString &key) const
1947{
1948 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001949 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001951 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001952 return synthetic_child_sp;
1953}
1954
Greg Clayton2452ab72013-02-08 22:02:02 +00001955uint32_t
1956ValueObject::GetTypeInfo (clang_type_t *pointee_or_element_clang_type)
1957{
1958 return ClangASTContext::GetTypeInfo (GetClangType(), GetClangAST(), pointee_or_element_clang_type);
1959}
1960
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001961bool
1962ValueObject::IsPointerType ()
1963{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001964 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965}
1966
Jim Inghamb7603bb2011-03-18 00:05:18 +00001967bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001968ValueObject::IsArrayType ()
1969{
Greg Clayton4ef877f2012-12-06 02:33:54 +00001970 return ClangASTContext::IsArrayType (GetClangType(), NULL, NULL, NULL);
Greg Claytondaf515f2011-07-09 20:12:33 +00001971}
1972
1973bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001974ValueObject::IsScalarType ()
1975{
1976 return ClangASTContext::IsScalarType (GetClangType());
1977}
1978
1979bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001980ValueObject::IsIntegerType (bool &is_signed)
1981{
1982 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1983}
Greg Clayton73b472d2010-10-27 03:32:59 +00001984
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985bool
1986ValueObject::IsPointerOrReferenceType ()
1987{
Greg Clayton007d5be2011-05-30 00:49:24 +00001988 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1989}
1990
1991bool
Greg Claytondea8cb42011-06-29 22:09:02 +00001992ValueObject::IsPossibleDynamicType ()
1993{
Enrico Granatafd4c84e2012-05-21 16:51:35 +00001994 ExecutionContext exe_ctx (GetExecutionContextRef());
1995 Process *process = exe_ctx.GetProcessPtr();
1996 if (process)
1997 return process->IsPossibleDynamicValue(*this);
1998 else
Greg Clayton70364252012-08-31 18:56:24 +00001999 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType(), NULL, true, true);
Greg Claytondea8cb42011-06-29 22:09:02 +00002000}
2001
Enrico Granata9e7b3882012-12-13 23:50:33 +00002002bool
2003ValueObject::IsObjCNil ()
2004{
Enrico Granata7277d202013-03-15 23:33:15 +00002005 const uint32_t mask = ClangASTContext::eTypeIsObjC | ClangASTContext::eTypeIsPointer;
2006 bool isObjCpointer = ( ((ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), NULL)) & mask) == mask);
2007 if (!isObjCpointer)
2008 return false;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002009 bool canReadValue = true;
2010 bool isZero = GetValueAsUnsigned(0,&canReadValue) == 0;
Enrico Granata7277d202013-03-15 23:33:15 +00002011 return canReadValue && isZero;
Enrico Granata9e7b3882012-12-13 23:50:33 +00002012}
2013
Greg Claytonafacd142011-09-02 01:15:17 +00002014ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002015ValueObject::GetSyntheticArrayMember (size_t index, bool can_create)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002016{
Greg Clayton2452ab72013-02-08 22:02:02 +00002017 const uint32_t type_info = GetTypeInfo ();
2018 if (type_info & ClangASTContext::eTypeIsArray)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002019 return GetSyntheticArrayMemberFromArray(index, can_create);
2020
Greg Clayton2452ab72013-02-08 22:02:02 +00002021 if (type_info & ClangASTContext::eTypeIsPointer)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00002022 return GetSyntheticArrayMemberFromPointer(index, can_create);
2023
2024 return ValueObjectSP();
2025
2026}
2027
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002028ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002029ValueObject::GetSyntheticArrayMemberFromPointer (size_t index, bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030{
2031 ValueObjectSP synthetic_child_sp;
2032 if (IsPointerType ())
2033 {
2034 char index_str[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00002035 snprintf(index_str, sizeof(index_str), "[%zu]", index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002036 ConstString index_const_str(index_str);
2037 // Check if we have already created a synthetic array member in this
2038 // valid object. If we have we will re-use it.
2039 synthetic_child_sp = GetSyntheticChild (index_const_str);
2040 if (!synthetic_child_sp)
2041 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002042 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002043 // We haven't made a synthetic array member for INDEX yet, so
2044 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00002045 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002046
2047 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00002048 if (synthetic_child)
2049 {
2050 AddSyntheticChild(index_const_str, synthetic_child);
2051 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002052 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00002053 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00002054 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055 }
2056 }
2057 return synthetic_child_sp;
2058}
Jim Ingham22777012010-09-23 02:01:19 +00002059
Greg Claytondaf515f2011-07-09 20:12:33 +00002060// This allows you to create an array member using and index
2061// that doesn't not fall in the normal bounds of the array.
2062// Many times structure can be defined as:
2063// struct Collection
2064// {
2065// uint32_t item_count;
2066// Item item_array[0];
2067// };
2068// The size of the "item_array" is 1, but many times in practice
2069// there are more items in "item_array".
2070
2071ValueObjectSP
Greg Claytonc7bece562013-01-25 18:06:21 +00002072ValueObject::GetSyntheticArrayMemberFromArray (size_t index, bool can_create)
Greg Claytondaf515f2011-07-09 20:12:33 +00002073{
2074 ValueObjectSP synthetic_child_sp;
2075 if (IsArrayType ())
2076 {
2077 char index_str[64];
Greg Claytonc7bece562013-01-25 18:06:21 +00002078 snprintf(index_str, sizeof(index_str), "[%zu]", index);
Greg Claytondaf515f2011-07-09 20:12:33 +00002079 ConstString index_const_str(index_str);
2080 // Check if we have already created a synthetic array member in this
2081 // valid object. If we have we will re-use it.
2082 synthetic_child_sp = GetSyntheticChild (index_const_str);
2083 if (!synthetic_child_sp)
2084 {
2085 ValueObject *synthetic_child;
2086 // We haven't made a synthetic array member for INDEX yet, so
2087 // lets make one and cache it for any future reference.
2088 synthetic_child = CreateChildAtIndex(0, true, index);
2089
2090 // Cache the value if we got one back...
2091 if (synthetic_child)
2092 {
2093 AddSyntheticChild(index_const_str, synthetic_child);
2094 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002095 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00002096 synthetic_child_sp->m_is_array_item_for_pointer = true;
2097 }
2098 }
2099 }
2100 return synthetic_child_sp;
2101}
2102
Enrico Granata9fc19442011-07-06 02:13:41 +00002103ValueObjectSP
2104ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
2105{
2106 ValueObjectSP synthetic_child_sp;
2107 if (IsScalarType ())
2108 {
2109 char index_str[64];
2110 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
2111 ConstString index_const_str(index_str);
2112 // Check if we have already created a synthetic array member in this
2113 // valid object. If we have we will re-use it.
2114 synthetic_child_sp = GetSyntheticChild (index_const_str);
2115 if (!synthetic_child_sp)
2116 {
2117 ValueObjectChild *synthetic_child;
2118 // We haven't made a synthetic array member for INDEX yet, so
2119 // lets make one and cache it for any future reference.
2120 synthetic_child = new ValueObjectChild(*this,
2121 GetClangAST(),
2122 GetClangType(),
2123 index_const_str,
2124 GetByteSize(),
2125 0,
2126 to-from+1,
2127 from,
2128 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00002129 false,
2130 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00002131
2132 // Cache the value if we got one back...
2133 if (synthetic_child)
2134 {
2135 AddSyntheticChild(index_const_str, synthetic_child);
2136 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002137 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00002138 synthetic_child_sp->m_is_bitfield_for_scalar = true;
2139 }
2140 }
2141 }
2142 return synthetic_child_sp;
2143}
2144
Greg Claytonafacd142011-09-02 01:15:17 +00002145ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00002146ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
2147{
2148
2149 ValueObjectSP synthetic_child_sp;
2150
2151 char name_str[64];
2152 snprintf(name_str, sizeof(name_str), "@%i", offset);
2153 ConstString name_const_str(name_str);
2154
2155 // Check if we have already created a synthetic array member in this
2156 // valid object. If we have we will re-use it.
2157 synthetic_child_sp = GetSyntheticChild (name_const_str);
2158
2159 if (synthetic_child_sp.get())
2160 return synthetic_child_sp;
2161
2162 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00002163 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00002164
2165 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
2166 type.GetASTContext(),
2167 type.GetOpaqueQualType(),
2168 name_const_str,
2169 type.GetTypeByteSize(),
2170 offset,
2171 0,
2172 0,
2173 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00002174 false,
2175 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00002176 if (synthetic_child)
2177 {
2178 AddSyntheticChild(name_const_str, synthetic_child);
2179 synthetic_child_sp = synthetic_child->GetSP();
2180 synthetic_child_sp->SetName(name_const_str);
2181 synthetic_child_sp->m_is_child_at_offset = true;
2182 }
2183 return synthetic_child_sp;
2184}
2185
Enrico Granatad55546b2011-07-22 00:16:08 +00002186// your expression path needs to have a leading . or ->
2187// (unless it somehow "looks like" an array, in which case it has
2188// a leading [ symbol). while the [ is meaningful and should be shown
2189// to the user, . and -> are just parser design, but by no means
2190// added information for the user.. strip them off
2191static const char*
2192SkipLeadingExpressionPathSeparators(const char* expression)
2193{
2194 if (!expression || !expression[0])
2195 return expression;
2196 if (expression[0] == '.')
2197 return expression+1;
2198 if (expression[0] == '-' && expression[1] == '>')
2199 return expression+2;
2200 return expression;
2201}
2202
Greg Claytonafacd142011-09-02 01:15:17 +00002203ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00002204ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
2205{
2206 ValueObjectSP synthetic_child_sp;
2207 ConstString name_const_string(expression);
2208 // Check if we have already created a synthetic array member in this
2209 // valid object. If we have we will re-use it.
2210 synthetic_child_sp = GetSyntheticChild (name_const_string);
2211 if (!synthetic_child_sp)
2212 {
2213 // We haven't made a synthetic array member for expression yet, so
2214 // lets make one and cache it for any future reference.
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002215 synthetic_child_sp = GetValueForExpressionPath(expression,
2216 NULL, NULL, NULL,
2217 GetValueForExpressionPathOptions().DontAllowSyntheticChildren());
Enrico Granatad55546b2011-07-22 00:16:08 +00002218
2219 // Cache the value if we got one back...
2220 if (synthetic_child_sp.get())
2221 {
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002222 // 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 +00002223 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00002224 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002225 }
2226 }
2227 return synthetic_child_sp;
2228}
2229
2230void
Enrico Granata86cc9822012-03-19 22:58:49 +00002231ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002232{
Enrico Granata86cc9822012-03-19 22:58:49 +00002233 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002234 return;
2235
Enrico Granatac5bc4122012-03-27 02:35:13 +00002236 TargetSP target_sp(GetTargetSP());
2237 if (target_sp && (target_sp->GetEnableSyntheticValue() == false || target_sp->GetSuppressSyntheticValue() == true))
2238 {
2239 m_synthetic_value = NULL;
2240 return;
2241 }
2242
Enrico Granatae3e91512012-10-22 18:18:36 +00002243 lldb::SyntheticChildrenSP current_synth_sp(m_synthetic_children_sp);
2244
Enrico Granata5548cb52013-01-28 23:47:25 +00002245 if (!UpdateFormatsIfNeeded() && m_synthetic_value)
Enrico Granata86cc9822012-03-19 22:58:49 +00002246 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002247
Enrico Granata0c489f52012-03-01 04:24:26 +00002248 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002249 return;
2250
Enrico Granatae3e91512012-10-22 18:18:36 +00002251 if (current_synth_sp == m_synthetic_children_sp && m_synthetic_value)
2252 return;
2253
Enrico Granata86cc9822012-03-19 22:58:49 +00002254 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002255}
2256
Jim Ingham78a685a2011-04-16 00:01:13 +00002257void
Greg Claytonafacd142011-09-02 01:15:17 +00002258ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002259{
Greg Claytonafacd142011-09-02 01:15:17 +00002260 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002261 return;
2262
Jim Ingham58b59f92011-04-22 23:53:53 +00002263 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002264 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002265 ExecutionContext exe_ctx (GetExecutionContextRef());
2266 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002267 if (process && process->IsPossibleDynamicValue(*this))
Enrico Granatae3e91512012-10-22 18:18:36 +00002268 {
2269 ClearDynamicTypeInformation ();
Enrico Granatafd4c84e2012-05-21 16:51:35 +00002270 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Enrico Granatae3e91512012-10-22 18:18:36 +00002271 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002272 }
2273}
2274
Jim Ingham58b59f92011-04-22 23:53:53 +00002275ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002276ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002277{
Greg Claytonafacd142011-09-02 01:15:17 +00002278 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002279 return ValueObjectSP();
2280
2281 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002282 {
Jim Ingham2837b762011-05-04 03:43:18 +00002283 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002284 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002285 if (m_dynamic_value)
2286 return m_dynamic_value->GetSP();
2287 else
2288 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002289}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002290
Jim Ingham60dbabb2011-12-08 19:44:08 +00002291ValueObjectSP
2292ValueObject::GetStaticValue()
2293{
2294 return GetSP();
2295}
2296
Enrico Granata886147f2012-05-08 18:47:08 +00002297lldb::ValueObjectSP
2298ValueObject::GetNonSyntheticValue ()
2299{
2300 return GetSP();
2301}
2302
Enrico Granatad55546b2011-07-22 00:16:08 +00002303ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002304ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002305{
Enrico Granata86cc9822012-03-19 22:58:49 +00002306 if (use_synthetic == false)
2307 return ValueObjectSP();
2308
Enrico Granatad55546b2011-07-22 00:16:08 +00002309 CalculateSyntheticValue(use_synthetic);
2310
2311 if (m_synthetic_value)
2312 return m_synthetic_value->GetSP();
2313 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002314 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002315}
2316
Greg Claytone221f822011-01-21 01:59:00 +00002317bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002318ValueObject::HasSyntheticValue()
2319{
Enrico Granata5548cb52013-01-28 23:47:25 +00002320 UpdateFormatsIfNeeded();
Enrico Granata27b625e2011-08-09 01:04:56 +00002321
Enrico Granata0c489f52012-03-01 04:24:26 +00002322 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002323 return false;
2324
Enrico Granata86cc9822012-03-19 22:58:49 +00002325 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002326
2327 if (m_synthetic_value)
2328 return true;
2329 else
2330 return false;
2331}
2332
2333bool
Greg Claytone221f822011-01-21 01:59:00 +00002334ValueObject::GetBaseClassPath (Stream &s)
2335{
2336 if (IsBaseClass())
2337 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002338 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00002339 clang_type_t clang_type = GetClangType();
2340 std::string cxx_class_name;
2341 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
2342 if (this_had_base_class)
2343 {
2344 if (parent_had_base_class)
2345 s.PutCString("::");
2346 s.PutCString(cxx_class_name.c_str());
2347 }
2348 return parent_had_base_class || this_had_base_class;
2349 }
2350 return false;
2351}
2352
2353
2354ValueObject *
2355ValueObject::GetNonBaseClassParent()
2356{
Jim Ingham78a685a2011-04-16 00:01:13 +00002357 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002358 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002359 if (GetParent()->IsBaseClass())
2360 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002361 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002362 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002363 }
2364 return NULL;
2365}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002366
2367void
Enrico Granata4becb372011-06-29 22:27:15 +00002368ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002369{
Greg Claytone221f822011-01-21 01:59:00 +00002370 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002371
Enrico Granata86cc9822012-03-19 22:58:49 +00002372 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002373 {
Enrico Granata4becb372011-06-29 22:27:15 +00002374 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2375 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2376 // the eHonorPointers mode is meant to produce strings in this latter format
2377 s.PutCString("*(");
2378 }
Greg Claytone221f822011-01-21 01:59:00 +00002379
Enrico Granata4becb372011-06-29 22:27:15 +00002380 ValueObject* parent = GetParent();
2381
2382 if (parent)
2383 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002384
2385 // if we are a deref_of_parent just because we are synthetic array
2386 // members made up to allow ptr[%d] syntax to work in variable
2387 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002388 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002389 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002390
Greg Claytone221f822011-01-21 01:59:00 +00002391 if (!IsBaseClass())
2392 {
2393 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002394 {
Greg Claytone221f822011-01-21 01:59:00 +00002395 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2396 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002397 {
Greg Claytone221f822011-01-21 01:59:00 +00002398 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2399 if (non_base_class_parent_clang_type)
2400 {
2401 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
2402
Enrico Granata86cc9822012-03-19 22:58:49 +00002403 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002404 {
2405 s.PutCString("->");
2406 }
Enrico Granata4becb372011-06-29 22:27:15 +00002407 else
2408 {
2409 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
2410 {
2411 s.PutCString("->");
2412 }
2413 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
2414 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
2415 {
2416 s.PutChar('.');
2417 }
Greg Claytone221f822011-01-21 01:59:00 +00002418 }
2419 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002420 }
Greg Claytone221f822011-01-21 01:59:00 +00002421
2422 const char *name = GetName().GetCString();
2423 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002424 {
Greg Claytone221f822011-01-21 01:59:00 +00002425 if (qualify_cxx_base_classes)
2426 {
2427 if (GetBaseClassPath (s))
2428 s.PutCString("::");
2429 }
2430 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002431 }
2432 }
2433 }
2434
Enrico Granata86cc9822012-03-19 22:58:49 +00002435 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002436 {
Greg Claytone221f822011-01-21 01:59:00 +00002437 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002438 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002439}
2440
Greg Claytonafacd142011-09-02 01:15:17 +00002441ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002442ValueObject::GetValueForExpressionPath(const char* expression,
2443 const char** first_unparsed,
2444 ExpressionPathScanEndReason* reason_to_stop,
2445 ExpressionPathEndResultType* final_value_type,
2446 const GetValueForExpressionPathOptions& options,
2447 ExpressionPathAftermath* final_task_on_target)
2448{
2449
2450 const char* dummy_first_unparsed;
Enrico Granataea2bc0f2013-02-21 19:57:10 +00002451 ExpressionPathScanEndReason dummy_reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnknown;
2452 ExpressionPathEndResultType dummy_final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata86cc9822012-03-19 22:58:49 +00002453 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002454
2455 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2456 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2457 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2458 final_value_type ? final_value_type : &dummy_final_value_type,
2459 options,
2460 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2461
Enrico Granata86cc9822012-03-19 22:58:49 +00002462 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002463 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002464
Enrico Granata86cc9822012-03-19 22:58:49 +00002465 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 +00002466 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002467 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002468 {
2469 Error error;
2470 ValueObjectSP final_value = ret_val->Dereference(error);
2471 if (error.Fail() || !final_value.get())
2472 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002473 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002474 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002475 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002476 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002477 return ValueObjectSP();
2478 }
2479 else
2480 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002481 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002482 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002483 return final_value;
2484 }
2485 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002486 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002487 {
2488 Error error;
2489 ValueObjectSP final_value = ret_val->AddressOf(error);
2490 if (error.Fail() || !final_value.get())
2491 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002492 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002493 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002494 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002495 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002496 return ValueObjectSP();
2497 }
2498 else
2499 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002500 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002501 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002502 return final_value;
2503 }
2504 }
2505 }
2506 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2507}
2508
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002509int
2510ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002511 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002512 const char** first_unparsed,
2513 ExpressionPathScanEndReason* reason_to_stop,
2514 ExpressionPathEndResultType* final_value_type,
2515 const GetValueForExpressionPathOptions& options,
2516 ExpressionPathAftermath* final_task_on_target)
2517{
2518 const char* dummy_first_unparsed;
2519 ExpressionPathScanEndReason dummy_reason_to_stop;
2520 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002521 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002522
2523 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2524 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2525 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2526 final_value_type ? final_value_type : &dummy_final_value_type,
2527 options,
2528 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2529
2530 if (!ret_val.get()) // if there are errors, I add nothing to the list
2531 return 0;
2532
Enrico Granata86ea8d82012-03-29 01:34:34 +00002533 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002534 {
2535 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002536 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002537 {
2538 list->Append(ret_val);
2539 return 1;
2540 }
Enrico Granata86ea8d82012-03-29 01:34:34 +00002541 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 +00002542 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002543 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002544 {
2545 Error error;
2546 ValueObjectSP final_value = ret_val->Dereference(error);
2547 if (error.Fail() || !final_value.get())
2548 {
Greg Clayton23f59502012-07-17 03:23:13 +00002549 if (reason_to_stop)
2550 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2551 if (final_value_type)
2552 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002553 return 0;
2554 }
2555 else
2556 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002557 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002558 list->Append(final_value);
2559 return 1;
2560 }
2561 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002562 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002563 {
2564 Error error;
2565 ValueObjectSP final_value = ret_val->AddressOf(error);
2566 if (error.Fail() || !final_value.get())
2567 {
Greg Clayton23f59502012-07-17 03:23:13 +00002568 if (reason_to_stop)
2569 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2570 if (final_value_type)
2571 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002572 return 0;
2573 }
2574 else
2575 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002576 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002577 list->Append(final_value);
2578 return 1;
2579 }
2580 }
2581 }
2582 }
2583 else
2584 {
2585 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2586 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2587 ret_val,
2588 list,
2589 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2590 final_value_type ? final_value_type : &dummy_final_value_type,
2591 options,
2592 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2593 }
2594 // in any non-covered case, just do the obviously right thing
2595 list->Append(ret_val);
2596 return 1;
2597}
2598
Greg Claytonafacd142011-09-02 01:15:17 +00002599ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002600ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2601 const char** first_unparsed,
2602 ExpressionPathScanEndReason* reason_to_stop,
2603 ExpressionPathEndResultType* final_result,
2604 const GetValueForExpressionPathOptions& options,
2605 ExpressionPathAftermath* what_next)
2606{
2607 ValueObjectSP root = GetSP();
2608
2609 if (!root.get())
2610 return ValueObjectSP();
2611
2612 *first_unparsed = expression_cstr;
2613
2614 while (true)
2615 {
2616
2617 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2618
Greg Claytonafacd142011-09-02 01:15:17 +00002619 clang_type_t root_clang_type = root->GetClangType();
2620 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002621 Flags root_clang_type_info,pointee_clang_type_info;
2622
2623 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2624 if (pointee_clang_type)
2625 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002626
2627 if (!expression_cstr || *expression_cstr == '\0')
2628 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002629 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002630 return root;
2631 }
2632
2633 switch (*expression_cstr)
2634 {
2635 case '-':
2636 {
2637 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002638 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) ) // if you are trying to use -> on a non-pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002639 {
2640 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002641 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2642 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002643 return ValueObjectSP();
2644 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002645 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2646 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002647 options.m_no_fragile_ivar)
2648 {
2649 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002650 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2651 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002652 return ValueObjectSP();
2653 }
2654 if (expression_cstr[1] != '>')
2655 {
2656 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002657 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2658 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002659 return ValueObjectSP();
2660 }
2661 expression_cstr++; // skip the -
2662 }
2663 case '.': // or fallthrough from ->
2664 {
2665 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002666 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if you are trying to use . on a pointer and I must catch the error
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002667 {
2668 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002669 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2670 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002671 return ValueObjectSP();
2672 }
2673 expression_cstr++; // skip .
2674 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2675 ConstString child_name;
2676 if (!next_separator) // if no other separator just expand this last layer
2677 {
2678 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002679 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2680
2681 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002682 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002683 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002684 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2685 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002686 return child_valobj_sp;
2687 }
2688 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2689 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002690 if (root->IsSynthetic())
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002691 {
2692 *first_unparsed = expression_cstr;
2693 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2694 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2695 return ValueObjectSP();
2696 }
2697
2698 child_valobj_sp = root->GetSyntheticValue();
Enrico Granata86cc9822012-03-19 22:58:49 +00002699 if (child_valobj_sp.get())
2700 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002701 }
2702
2703 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2704 // so we hit the "else" branch, and return an error
2705 if(child_valobj_sp.get()) // if it worked, just return
2706 {
Daniel Maleaa85e6b62012-12-07 22:21:08 +00002707 *first_unparsed = "";
Enrico Granata86cc9822012-03-19 22:58:49 +00002708 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2709 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002710 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002711 }
2712 else
2713 {
2714 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002715 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2716 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002717 return ValueObjectSP();
2718 }
2719 }
2720 else // other layers do expand
2721 {
2722 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002723 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2724 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002725 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002726 root = child_valobj_sp;
2727 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002728 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002729 continue;
2730 }
2731 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2732 {
Enrico Granatadf31a8a2012-08-02 17:34:05 +00002733 if (root->IsSynthetic())
2734 {
2735 *first_unparsed = expression_cstr;
2736 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2737 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2738 return ValueObjectSP();
2739 }
2740
Enrico Granata86cc9822012-03-19 22:58:49 +00002741 child_valobj_sp = root->GetSyntheticValue(true);
2742 if (child_valobj_sp)
2743 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002744 }
2745
2746 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2747 // so we hit the "else" branch, and return an error
2748 if(child_valobj_sp.get()) // if it worked, move on
2749 {
2750 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002751 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002752 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002753 continue;
2754 }
2755 else
2756 {
2757 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002758 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2759 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002760 return ValueObjectSP();
2761 }
2762 }
2763 break;
2764 }
2765 case '[':
2766 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002767 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002768 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002769 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002770 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002771 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2772 {
2773 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002774 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2775 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002776 return ValueObjectSP();
2777 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002778 }
2779 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2780 {
2781 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002782 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2783 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002784 return ValueObjectSP();
2785 }
2786 }
2787 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2788 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002789 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002790 {
2791 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002792 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2793 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002794 return ValueObjectSP();
2795 }
2796 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2797 {
2798 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002799 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2800 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002801 return root;
2802 }
2803 }
2804 const char *separator_position = ::strchr(expression_cstr+1,'-');
2805 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2806 if (!close_bracket_position) // if there is no ], this is a syntax error
2807 {
2808 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002809 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2810 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002811 return ValueObjectSP();
2812 }
2813 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2814 {
2815 char *end = NULL;
2816 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2817 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2818 {
2819 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002820 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2821 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002822 return ValueObjectSP();
2823 }
2824 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2825 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002826 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002827 {
2828 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002829 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2830 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002831 return root;
2832 }
2833 else
2834 {
2835 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002836 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2837 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002838 return ValueObjectSP();
2839 }
2840 }
2841 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002842 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002843 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002844 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2845 if (!child_valobj_sp)
2846 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002847 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002848 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2849 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002850 if (child_valobj_sp)
2851 {
2852 root = child_valobj_sp;
2853 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002854 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002855 continue;
2856 }
2857 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002858 {
2859 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002860 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2861 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002862 return ValueObjectSP();
2863 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002864 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002865 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002866 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002867 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002868 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002869 {
2870 Error error;
2871 root = root->Dereference(error);
2872 if (error.Fail() || !root.get())
2873 {
2874 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002875 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2876 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002877 return ValueObjectSP();
2878 }
2879 else
2880 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002881 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002882 continue;
2883 }
2884 }
2885 else
2886 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002887 if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
Greg Clayton84db9102012-03-26 23:03:23 +00002888 root->GetClangType()) == eLanguageTypeObjC
2889 && ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2890 && root->HasSyntheticValue()
2891 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002892 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002893 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002894 }
2895 else
2896 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002897 if (!root.get())
2898 {
2899 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002900 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2901 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002902 return ValueObjectSP();
2903 }
2904 else
2905 {
2906 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002907 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002908 continue;
2909 }
2910 }
2911 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002912 else if (ClangASTContext::IsScalarType(root_clang_type))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002913 {
2914 root = root->GetSyntheticBitFieldChild(index, index, true);
2915 if (!root.get())
2916 {
2917 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002918 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2919 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002920 return ValueObjectSP();
2921 }
2922 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2923 {
2924 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002925 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2926 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002927 return root;
2928 }
2929 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002930 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002931 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002932 if (root->HasSyntheticValue())
2933 root = root->GetSyntheticValue();
2934 else if (!root->IsSynthetic())
2935 {
2936 *first_unparsed = expression_cstr;
2937 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2938 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2939 return ValueObjectSP();
2940 }
2941 // if we are here, then root itself is a synthetic VO.. should be good to go
2942
Enrico Granata27b625e2011-08-09 01:04:56 +00002943 if (!root.get())
2944 {
2945 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002946 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2947 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2948 return ValueObjectSP();
2949 }
2950 root = root->GetChildAtIndex(index, true);
2951 if (!root.get())
2952 {
2953 *first_unparsed = expression_cstr;
2954 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2955 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002956 return ValueObjectSP();
2957 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00002958 else
2959 {
2960 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002961 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002962 continue;
2963 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002964 }
2965 else
2966 {
2967 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002968 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2969 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002970 return ValueObjectSP();
2971 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002972 }
2973 else // we have a low and a high index
2974 {
2975 char *end = NULL;
2976 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2977 if (!end || end != separator_position) // if something weird is in our way return an error
2978 {
2979 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002980 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2981 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002982 return ValueObjectSP();
2983 }
2984 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2985 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2986 {
2987 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002988 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2989 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002990 return ValueObjectSP();
2991 }
2992 if (index_lower > index_higher) // swap indices if required
2993 {
2994 unsigned long temp = index_lower;
2995 index_lower = index_higher;
2996 index_higher = temp;
2997 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002998 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002999 {
3000 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3001 if (!root.get())
3002 {
3003 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003004 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3005 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003006 return ValueObjectSP();
3007 }
3008 else
3009 {
3010 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003011 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
3012 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003013 return root;
3014 }
3015 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003016 else if (root_clang_type_info.Test(ClangASTContext::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 +00003017 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003018 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003019 {
3020 Error error;
3021 root = root->Dereference(error);
3022 if (error.Fail() || !root.get())
3023 {
3024 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003025 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3026 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003027 return ValueObjectSP();
3028 }
3029 else
3030 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003031 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003032 continue;
3033 }
3034 }
3035 else
3036 {
3037 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003038 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
3039 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003040 return root;
3041 }
3042 }
3043 break;
3044 }
3045 default: // some non-separator is in the way
3046 {
3047 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003048 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3049 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00003050 return ValueObjectSP();
3051 break;
3052 }
3053 }
3054 }
3055}
3056
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003057int
3058ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
3059 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00003060 ValueObjectSP root,
3061 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003062 ExpressionPathScanEndReason* reason_to_stop,
3063 ExpressionPathEndResultType* final_result,
3064 const GetValueForExpressionPathOptions& options,
3065 ExpressionPathAftermath* what_next)
3066{
3067 if (!root.get())
3068 return 0;
3069
3070 *first_unparsed = expression_cstr;
3071
3072 while (true)
3073 {
3074
3075 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
3076
Greg Claytonafacd142011-09-02 01:15:17 +00003077 clang_type_t root_clang_type = root->GetClangType();
3078 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003079 Flags root_clang_type_info,pointee_clang_type_info;
3080
3081 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
3082 if (pointee_clang_type)
3083 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
3084
3085 if (!expression_cstr || *expression_cstr == '\0')
3086 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003087 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003088 list->Append(root);
3089 return 1;
3090 }
3091
3092 switch (*expression_cstr)
3093 {
3094 case '[':
3095 {
3096 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
3097 {
3098 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
3099 {
3100 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003101 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
3102 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003103 return 0;
3104 }
3105 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
3106 {
3107 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003108 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
3109 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003110 return 0;
3111 }
3112 }
3113 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
3114 {
3115 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
3116 {
3117 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003118 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3119 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003120 return 0;
3121 }
3122 else // expand this into list
3123 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003124 const size_t max_index = root->GetNumChildren() - 1;
3125 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003126 {
3127 ValueObjectSP child =
3128 root->GetChildAtIndex(index, true);
3129 list->Append(child);
3130 }
3131 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003132 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3133 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003134 return max_index; // tell me number of items I added to the VOList
3135 }
3136 }
3137 const char *separator_position = ::strchr(expression_cstr+1,'-');
3138 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
3139 if (!close_bracket_position) // if there is no ], this is a syntax error
3140 {
3141 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003142 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3143 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003144 return 0;
3145 }
3146 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
3147 {
3148 char *end = NULL;
3149 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
3150 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3151 {
3152 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003153 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3154 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003155 return 0;
3156 }
3157 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
3158 {
3159 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
3160 {
Greg Claytonc7bece562013-01-25 18:06:21 +00003161 const size_t max_index = root->GetNumChildren() - 1;
3162 for (size_t index = 0; index < max_index; index++)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003163 {
3164 ValueObjectSP child =
3165 root->GetChildAtIndex(index, true);
3166 list->Append(child);
3167 }
3168 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00003169 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3170 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003171 return max_index; // tell me number of items I added to the VOList
3172 }
3173 else
3174 {
3175 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003176 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
3177 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003178 return 0;
3179 }
3180 }
3181 // from here on we do have a valid index
3182 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
3183 {
3184 root = root->GetChildAtIndex(index, true);
3185 if (!root.get())
3186 {
3187 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003188 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3189 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003190 return 0;
3191 }
3192 else
3193 {
3194 list->Append(root);
3195 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003196 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3197 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003198 return 1;
3199 }
3200 }
3201 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
3202 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003203 if (*what_next == ValueObject::eExpressionPathAftermathDereference && // if this is a ptr-to-scalar, I am accessing it by index and I would have deref'ed anyway, then do it now and use this as a bitfield
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003204 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
3205 {
3206 Error error;
3207 root = root->Dereference(error);
3208 if (error.Fail() || !root.get())
3209 {
3210 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003211 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3212 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003213 return 0;
3214 }
3215 else
3216 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003217 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003218 continue;
3219 }
3220 }
3221 else
3222 {
3223 root = root->GetSyntheticArrayMemberFromPointer(index, true);
3224 if (!root.get())
3225 {
3226 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003227 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3228 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003229 return 0;
3230 }
3231 else
3232 {
3233 list->Append(root);
3234 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003235 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3236 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003237 return 1;
3238 }
3239 }
3240 }
3241 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3242 {
3243 root = root->GetSyntheticBitFieldChild(index, index, true);
3244 if (!root.get())
3245 {
3246 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003247 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3248 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003249 return 0;
3250 }
3251 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3252 {
3253 list->Append(root);
3254 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003255 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3256 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003257 return 1;
3258 }
3259 }
3260 }
3261 else // we have a low and a high index
3262 {
3263 char *end = NULL;
3264 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3265 if (!end || end != separator_position) // if something weird is in our way return an error
3266 {
3267 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003268 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3269 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003270 return 0;
3271 }
3272 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3273 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3274 {
3275 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003276 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3277 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003278 return 0;
3279 }
3280 if (index_lower > index_higher) // swap indices if required
3281 {
3282 unsigned long temp = index_lower;
3283 index_lower = index_higher;
3284 index_higher = temp;
3285 }
3286 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
3287 {
3288 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3289 if (!root.get())
3290 {
3291 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003292 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3293 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003294 return 0;
3295 }
3296 else
3297 {
3298 list->Append(root);
3299 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003300 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3301 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003302 return 1;
3303 }
3304 }
3305 else if (root_clang_type_info.Test(ClangASTContext::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 +00003306 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003307 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
3308 {
3309 Error error;
3310 root = root->Dereference(error);
3311 if (error.Fail() || !root.get())
3312 {
3313 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003314 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3315 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003316 return 0;
3317 }
3318 else
3319 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003320 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003321 continue;
3322 }
3323 }
3324 else
3325 {
Johnny Chen44805302011-07-19 19:48:13 +00003326 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003327 index <= index_higher; index++)
3328 {
3329 ValueObjectSP child =
3330 root->GetChildAtIndex(index, true);
3331 list->Append(child);
3332 }
3333 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003334 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3335 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003336 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3337 }
3338 }
3339 break;
3340 }
3341 default: // some non-[ separator, or something entirely wrong, is in the way
3342 {
3343 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003344 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3345 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003346 return 0;
3347 break;
3348 }
3349 }
3350 }
3351}
3352
Enrico Granata0c489f52012-03-01 04:24:26 +00003353static void
3354DumpValueObject_Impl (Stream &s,
3355 ValueObject *valobj,
3356 const ValueObject::DumpValueObjectOptions& options,
3357 uint32_t ptr_depth,
3358 uint32_t curr_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003359{
Greg Clayton007d5be2011-05-30 00:49:24 +00003360 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003361 {
Enrico Granata5548cb52013-01-28 23:47:25 +00003362 bool update_success = valobj->UpdateValueIfNeeded (true);
Greg Clayton007d5be2011-05-30 00:49:24 +00003363
Enrico Granata0c489f52012-03-01 04:24:26 +00003364 const char *root_valobj_name =
3365 options.m_root_valobj_name.empty() ?
3366 valobj->GetName().AsCString() :
3367 options.m_root_valobj_name.c_str();
3368
3369 if (update_success && options.m_use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00003370 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003371 ValueObject *dynamic_value = valobj->GetDynamicValue(options.m_use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00003372 if (dynamic_value)
3373 valobj = dynamic_value;
3374 }
3375
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003376 clang_type_t clang_type = valobj->GetClangType();
3377
Greg Clayton73b472d2010-10-27 03:32:59 +00003378 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003379 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00003380 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
3381 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003382
Enrico Granata0c489f52012-03-01 04:24:26 +00003383 const bool print_valobj = options.m_flat_output == false || has_value;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003384
3385 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003386 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003387 if (options.m_show_location)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003388 {
Jim Ingham6035b672011-03-31 00:19:25 +00003389 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003390 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003391
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003392 s.Indent();
Enrico Granata2b2631c2012-08-09 16:51:25 +00003393
3394 bool show_type = true;
3395 // if we are at the root-level and been asked to hide the root's type, then hide it
3396 if (curr_depth == 0 && options.m_hide_root_type)
3397 show_type = false;
3398 else
3399 // otherwise decide according to the usual rules (asked to show types - always at the root level)
3400 show_type = options.m_show_types || (curr_depth == 0 && !options.m_flat_output);
3401
3402 if (show_type)
Enrico Granataf7b1a342013-01-23 01:17:27 +00003403 s.Printf("(%s) ", valobj->GetQualifiedTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00003404
Enrico Granata0c489f52012-03-01 04:24:26 +00003405 if (options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003406 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003407 // If we are showing types, also qualify the C++ base classes
Enrico Granata0c489f52012-03-01 04:24:26 +00003408 const bool qualify_cxx_base_classes = options.m_show_types;
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003409 if (!options.m_hide_name)
3410 {
3411 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
3412 s.PutCString(" =");
3413 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003414 }
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003415 else if (!options.m_hide_name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003416 {
3417 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
3418 s.Printf ("%s =", name_cstr);
3419 }
3420
Enrico Granata0c489f52012-03-01 04:24:26 +00003421 if (!options.m_scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003422 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003423 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003424 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003425 }
3426
Enrico Granata0c489f52012-03-01 04:24:26 +00003427 std::string summary_str;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003428 std::string value_str;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003429 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00003430 const char *sum_cstr = NULL;
Enrico Granata0c489f52012-03-01 04:24:26 +00003431 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003432
Enrico Granata0c489f52012-03-01 04:24:26 +00003433 if (options.m_omit_summary_depth > 0)
Enrico Granata0c5ef692011-07-16 01:22:04 +00003434 entry = NULL;
3435
Enrico Granata9e7b3882012-12-13 23:50:33 +00003436 bool is_nil = valobj->IsObjCNil();
3437
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003438 if (err_cstr == NULL)
3439 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003440 if (options.m_format != eFormatDefault && options.m_format != valobj->GetFormat())
Greg Clayton6efba4f2012-01-26 21:08:30 +00003441 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003442 valobj->GetValueAsCString(options.m_format,
3443 value_str);
Greg Clayton6efba4f2012-01-26 21:08:30 +00003444 }
Enrico Granata0c489f52012-03-01 04:24:26 +00003445 else
Greg Clayton6efba4f2012-01-26 21:08:30 +00003446 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003447 val_cstr = valobj->GetValueAsCString();
3448 if (val_cstr)
3449 value_str = val_cstr;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003450 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003451 err_cstr = valobj->GetError().AsCString();
3452 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003453
3454 if (err_cstr)
3455 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003456 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00003457 }
3458 else
3459 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003460 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003461 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003462 {
Enrico Granata9e7b3882012-12-13 23:50:33 +00003463 if (is_nil)
3464 sum_cstr = "nil";
3465 else if (options.m_omit_summary_depth == 0)
Enrico Granata0c489f52012-03-01 04:24:26 +00003466 {
3467 if (options.m_summary_sp)
3468 {
3469 valobj->GetSummaryAsCString(entry, summary_str);
3470 sum_cstr = summary_str.c_str();
3471 }
3472 else
3473 sum_cstr = valobj->GetSummaryAsCString();
3474 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003475
Greg Clayton6efba4f2012-01-26 21:08:30 +00003476 // Make sure we have a value and make sure the summary didn't
Enrico Granata9e7b3882012-12-13 23:50:33 +00003477 // specify that the value should not be printed - and do not print
3478 // the value if this thing is nil
Enrico Granatac2a58d72013-03-25 19:46:48 +00003479 // (but show the value if the user passes a format explicitly)
3480 if (!is_nil && !value_str.empty() && (entry == NULL || (entry->DoesPrintValue() || options.m_format != eFormatDefault) || sum_cstr == NULL) && !options.m_hide_value)
Greg Clayton6efba4f2012-01-26 21:08:30 +00003481 s.Printf(" %s", value_str.c_str());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003482
Enrico Granata9dd75c82011-07-15 23:30:15 +00003483 if (sum_cstr)
Enrico Granata0c489f52012-03-01 04:24:26 +00003484 s.Printf(" %s", sum_cstr);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003485
Enrico Granata9e7b3882012-12-13 23:50:33 +00003486 // let's avoid the overly verbose no description error for a nil thing
3487 if (options.m_use_objc && !is_nil)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003488 {
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003489 if (!options.m_hide_value || !options.m_hide_name)
3490 s.Printf(" ");
Jim Ingham6035b672011-03-31 00:19:25 +00003491 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003492 if (object_desc)
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003493 s.Printf("%s\n", object_desc);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003494 else
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003495 s.Printf ("[no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003496 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00003497 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003498 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003499
Enrico Granata0c489f52012-03-01 04:24:26 +00003500 if (curr_depth < options.m_max_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003501 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003502 // We will show children for all concrete types. We won't show
3503 // pointer contents unless a pointer depth has been specified.
3504 // We won't reference contents unless the reference is the
3505 // root object (depth of zero).
3506 bool print_children = true;
3507
3508 // Use a new temporary pointer depth in case we override the
3509 // current pointer depth below...
3510 uint32_t curr_ptr_depth = ptr_depth;
3511
3512 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
3513 if (is_ptr || is_ref)
3514 {
3515 // We have a pointer or reference whose value is an address.
3516 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00003517 AddressType ptr_address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003518 if (valobj->GetPointerValue (&ptr_address_type) == 0)
Greg Clayton73b472d2010-10-27 03:32:59 +00003519 print_children = false;
3520
3521 else if (is_ref && curr_depth == 0)
3522 {
3523 // If this is the root object (depth is zero) that we are showing
3524 // and it is a reference, and no pointer depth has been supplied
3525 // print out what it references. Don't do this at deeper depths
3526 // otherwise we can end up with infinite recursion...
3527 curr_ptr_depth = 1;
3528 }
3529
3530 if (curr_ptr_depth == 0)
3531 print_children = false;
3532 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003533
Enrico Granata0a3958e2011-07-02 00:25:22 +00003534 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00003535 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003536 ValueObject* synth_valobj;
3537 ValueObjectSP synth_valobj_sp = valobj->GetSyntheticValue (options.m_use_synthetic);
3538 synth_valobj = (synth_valobj_sp ? synth_valobj_sp.get() : valobj);
Enrico Granatac5bc4122012-03-27 02:35:13 +00003539
Greg Claytonc7bece562013-01-25 18:06:21 +00003540 size_t num_children = synth_valobj->GetNumChildren();
Enrico Granata22c55d12011-08-12 02:00:06 +00003541 bool print_dotdotdot = false;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003542 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003543 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003544 if (options.m_flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003545 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003546 if (print_valobj)
3547 s.EOL();
3548 }
3549 else
3550 {
3551 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00003552 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003553 s.IndentMore();
3554 }
Enrico Granata22c55d12011-08-12 02:00:06 +00003555
Greg Claytonc7bece562013-01-25 18:06:21 +00003556 const size_t max_num_children = valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00003557
Enrico Granata0c489f52012-03-01 04:24:26 +00003558 if (num_children > max_num_children && !options.m_ignore_cap)
Enrico Granata22c55d12011-08-12 02:00:06 +00003559 {
3560 num_children = max_num_children;
3561 print_dotdotdot = true;
3562 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003563
Enrico Granata0c489f52012-03-01 04:24:26 +00003564 ValueObject::DumpValueObjectOptions child_options(options);
Enrico Granatac953a6a2012-12-11 02:17:22 +00003565 child_options.SetFormat(options.m_format).SetSummary().SetRootValueObjectName();
Enrico Granata9a31ccb2013-01-29 01:35:01 +00003566 child_options.SetScopeChecked(true).SetHideName(options.m_hide_name).SetHideValue(options.m_hide_value)
Enrico Granata0c489f52012-03-01 04:24:26 +00003567 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
Greg Claytonc7bece562013-01-25 18:06:21 +00003568 for (size_t idx=0; idx<num_children; ++idx)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003569 {
Enrico Granatac482a192011-08-17 22:13:59 +00003570 ValueObjectSP child_sp(synth_valobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003571 if (child_sp.get())
3572 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003573 DumpValueObject_Impl (s,
3574 child_sp.get(),
3575 child_options,
3576 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
3577 curr_depth + 1);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003578 }
3579 }
3580
Enrico Granata0c489f52012-03-01 04:24:26 +00003581 if (!options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003582 {
Enrico Granata22c55d12011-08-12 02:00:06 +00003583 if (print_dotdotdot)
Enrico Granata61a80ba2011-08-12 16:42:31 +00003584 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003585 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3586 Target *target = exe_ctx.GetTargetPtr();
3587 if (target)
3588 target->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
Enrico Granata22c55d12011-08-12 02:00:06 +00003589 s.Indent("...\n");
Enrico Granata61a80ba2011-08-12 16:42:31 +00003590 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003591 s.IndentLess();
3592 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003593 }
3594 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003595 else if (has_children)
3596 {
3597 // Aggregate, no children...
3598 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00003599 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003600 }
3601 else
3602 {
3603 if (print_valobj)
3604 s.EOL();
3605 }
3606
Greg Clayton1d3afba2010-10-05 00:00:42 +00003607 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003608 else
3609 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00003610 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003611 }
3612 }
3613 else
3614 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003615 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003616 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003617 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003618 }
3619 }
3620 }
3621 }
3622}
3623
Enrico Granata0c489f52012-03-01 04:24:26 +00003624void
Greg Claytonf830dbb2012-03-22 18:15:37 +00003625ValueObject::LogValueObject (Log *log,
3626 ValueObject *valobj)
3627{
3628 if (log && valobj)
3629 return LogValueObject (log, valobj, DumpValueObjectOptions::DefaultOptions());
3630}
3631
3632void
3633ValueObject::LogValueObject (Log *log,
3634 ValueObject *valobj,
3635 const DumpValueObjectOptions& options)
3636{
3637 if (log && valobj)
3638 {
3639 StreamString s;
3640 ValueObject::DumpValueObject (s, valobj, options);
3641 if (s.GetSize())
3642 log->PutCString(s.GetData());
3643 }
3644}
3645
3646void
Enrico Granata0c489f52012-03-01 04:24:26 +00003647ValueObject::DumpValueObject (Stream &s,
3648 ValueObject *valobj)
3649{
3650
3651 if (!valobj)
3652 return;
3653
3654 DumpValueObject_Impl(s,
3655 valobj,
3656 DumpValueObjectOptions::DefaultOptions(),
3657 0,
3658 0);
3659}
3660
3661void
3662ValueObject::DumpValueObject (Stream &s,
3663 ValueObject *valobj,
3664 const DumpValueObjectOptions& options)
3665{
3666 DumpValueObject_Impl(s,
3667 valobj,
3668 options,
3669 options.m_max_ptr_depth, // max pointer depth allowed, we will go down from here
3670 0 // current object depth is 0 since we are just starting
3671 );
3672}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003673
3674ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003675ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003676{
3677 ValueObjectSP valobj_sp;
3678
Enrico Granatac3e320a2011-08-02 17:27:39 +00003679 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003680 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003681 ExecutionContext exe_ctx (GetExecutionContextRef());
3682 clang::ASTContext *ast = GetClangAST ();
3683
3684 DataExtractor data;
3685 data.SetByteOrder (m_data.GetByteOrder());
3686 data.SetAddressByteSize(m_data.GetAddressByteSize());
3687
Enrico Granata9f1e2042012-04-24 22:15:37 +00003688 if (IsBitfield())
3689 {
3690 Value v(Scalar(GetValueAsUnsigned(UINT64_MAX)));
3691 m_error = v.GetValueAsData (&exe_ctx, ast, data, 0, GetModule().get());
3692 }
3693 else
3694 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003695
3696 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3697 ast,
3698 GetClangType(),
3699 name,
3700 data,
3701 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003702 }
Jim Ingham6035b672011-03-31 00:19:25 +00003703
3704 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003705 {
Jim Ingham58b59f92011-04-22 23:53:53 +00003706 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003707 }
3708 return valobj_sp;
3709}
3710
Greg Claytonafacd142011-09-02 01:15:17 +00003711ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003712ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003713{
Jim Ingham58b59f92011-04-22 23:53:53 +00003714 if (m_deref_valobj)
3715 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003716
Greg Clayton54979cd2010-12-15 05:08:08 +00003717 const bool is_pointer_type = IsPointerType();
3718 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003719 {
3720 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003721 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003722
3723 std::string child_name_str;
3724 uint32_t child_byte_size = 0;
3725 int32_t child_byte_offset = 0;
3726 uint32_t child_bitfield_bit_size = 0;
3727 uint32_t child_bitfield_bit_offset = 0;
3728 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003729 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003730 const bool transparent_pointers = false;
3731 clang::ASTContext *clang_ast = GetClangAST();
3732 clang_type_t clang_type = GetClangType();
3733 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003734
Greg Claytoncc4d0142012-02-17 07:49:44 +00003735 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003736
3737 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
3738 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003739 GetName().GetCString(),
3740 clang_type,
3741 0,
3742 transparent_pointers,
3743 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003744 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003745 child_name_str,
3746 child_byte_size,
3747 child_byte_offset,
3748 child_bitfield_bit_size,
3749 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003750 child_is_base_class,
3751 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003752 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003753 {
3754 ConstString child_name;
3755 if (!child_name_str.empty())
3756 child_name.SetCString (child_name_str.c_str());
3757
Jim Ingham58b59f92011-04-22 23:53:53 +00003758 m_deref_valobj = new ValueObjectChild (*this,
3759 clang_ast,
3760 child_clang_type,
3761 child_name,
3762 child_byte_size,
3763 child_byte_offset,
3764 child_bitfield_bit_size,
3765 child_bitfield_bit_offset,
3766 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003767 child_is_deref_of_parent,
3768 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003769 }
3770 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003771
Jim Ingham58b59f92011-04-22 23:53:53 +00003772 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003773 {
3774 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003775 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003776 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003777 else
3778 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003779 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003780 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003781
3782 if (is_pointer_type)
3783 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3784 else
3785 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003786 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003787 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003788}
3789
Greg Claytonafacd142011-09-02 01:15:17 +00003790ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003791ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003792{
Jim Ingham78a685a2011-04-16 00:01:13 +00003793 if (m_addr_of_valobj_sp)
3794 return m_addr_of_valobj_sp;
3795
Greg Claytone0d378b2011-03-24 21:19:54 +00003796 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003797 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003798 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003799 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003800 if (addr != LLDB_INVALID_ADDRESS)
3801 {
3802 switch (address_type)
3803 {
3804 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003805 {
3806 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003807 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003808 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3809 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003810 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003811
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003812 case eAddressTypeFile:
3813 case eAddressTypeLoad:
3814 case eAddressTypeHost:
3815 {
3816 clang::ASTContext *ast = GetClangAST();
3817 clang_type_t clang_type = GetClangType();
3818 if (ast && clang_type)
3819 {
3820 std::string name (1, '&');
3821 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003822 ExecutionContext exe_ctx (GetExecutionContextRef());
3823 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003824 ast,
3825 ClangASTContext::CreatePointerType (ast, clang_type),
3826 ConstString (name.c_str()),
3827 addr,
3828 eAddressTypeInvalid,
3829 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003830 }
3831 }
3832 break;
3833 }
3834 }
Jim Ingham78a685a2011-04-16 00:01:13 +00003835 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003836}
3837
Greg Clayton9a142cf2012-02-03 05:34:10 +00003838ValueObjectSP
3839ValueObject::Cast (const ClangASTType &clang_ast_type)
3840{
Greg Clayton81e871e2012-02-04 02:27:34 +00003841 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003842}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003843
Greg Claytonafacd142011-09-02 01:15:17 +00003844ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003845ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3846{
Greg Claytonafacd142011-09-02 01:15:17 +00003847 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003848 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003849 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003850
3851 if (ptr_value != LLDB_INVALID_ADDRESS)
3852 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003853 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003854 ExecutionContext exe_ctx (GetExecutionContextRef());
3855 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003856 name,
3857 ptr_addr,
3858 clang_ast_type);
3859 }
3860 return valobj_sp;
3861}
3862
Greg Claytonafacd142011-09-02 01:15:17 +00003863ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003864ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3865{
Greg Claytonafacd142011-09-02 01:15:17 +00003866 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003867 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003868 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003869
3870 if (ptr_value != LLDB_INVALID_ADDRESS)
3871 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003872 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003873 ExecutionContext exe_ctx (GetExecutionContextRef());
3874 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003875 name,
3876 ptr_addr,
3877 type_sp);
3878 }
3879 return valobj_sp;
3880}
3881
Jim Ingham6035b672011-03-31 00:19:25 +00003882ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003883 m_mod_id(),
3884 m_exe_ctx_ref(),
3885 m_needs_update (true),
3886 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003887{
3888}
3889
3890ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003891 m_mod_id(),
3892 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003893 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003894 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003895{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003896 ExecutionContext exe_ctx(exe_scope);
3897 TargetSP target_sp (exe_ctx.GetTargetSP());
3898 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003899 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003900 m_exe_ctx_ref.SetTargetSP (target_sp);
3901 ProcessSP process_sp (exe_ctx.GetProcessSP());
3902 if (!process_sp)
3903 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003904
Greg Claytoncc4d0142012-02-17 07:49:44 +00003905 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003906 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003907 m_mod_id = process_sp->GetModID();
3908 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003909
Greg Claytoncc4d0142012-02-17 07:49:44 +00003910 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003911
Greg Claytoncc4d0142012-02-17 07:49:44 +00003912 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003913 {
3914 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003915 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003916 }
Jim Ingham6035b672011-03-31 00:19:25 +00003917
Greg Claytoncc4d0142012-02-17 07:49:44 +00003918 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003919 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003920 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003921
Greg Claytoncc4d0142012-02-17 07:49:44 +00003922 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3923 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003924 {
3925 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003926 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003927 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003928 if (frame_sp)
3929 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003930 }
3931 }
3932 }
Jim Ingham6035b672011-03-31 00:19:25 +00003933}
3934
3935ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003936 m_mod_id(),
3937 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3938 m_needs_update (true),
3939 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003940{
3941}
3942
3943ValueObject::EvaluationPoint::~EvaluationPoint ()
3944{
3945}
3946
Jim Ingham6035b672011-03-31 00:19:25 +00003947// This function checks the EvaluationPoint against the current process state. If the current
3948// state matches the evaluation point, or the evaluation point is already invalid, then we return
3949// false, meaning "no change". If the current state is different, we update our state, and return
3950// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3951// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003952// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003953
3954bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003955ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003956{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003957
3958 // 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 +00003959 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock());
Jim Ingham73ca05a2011-12-17 01:35:57 +00003960
Greg Claytoncc4d0142012-02-17 07:49:44 +00003961 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003962 return false;
3963
Jim Ingham6035b672011-03-31 00:19:25 +00003964 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003965 Process *process = exe_ctx.GetProcessPtr();
3966 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003967 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003968
Jim Ingham6035b672011-03-31 00:19:25 +00003969 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003970 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003971
Jim Ingham78a685a2011-04-16 00:01:13 +00003972 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3973 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003974 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003975 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003976
Greg Clayton23f59502012-07-17 03:23:13 +00003977 bool changed = false;
3978 const bool was_valid = m_mod_id.IsValid();
3979 if (was_valid)
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003980 {
3981 if (m_mod_id == current_mod_id)
3982 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003983 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003984 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003985 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003986 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003987 else
3988 {
3989 m_mod_id = current_mod_id;
3990 m_needs_update = true;
3991 changed = true;
3992 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003993 }
Jim Ingham6035b672011-03-31 00:19:25 +00003994
Jim Ingham73ca05a2011-12-17 01:35:57 +00003995 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3996 // That way we'll be sure to return a valid exe_scope.
3997 // 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 +00003998
Greg Claytoncc4d0142012-02-17 07:49:44 +00003999 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00004000 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00004001 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
4002 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00004003 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00004004 if (m_exe_ctx_ref.HasFrameRef())
4005 {
4006 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
4007 if (!frame_sp)
4008 {
4009 // We used to have a frame, but now it is gone
4010 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00004011 changed = was_valid;
Greg Claytoncc4d0142012-02-17 07:49:44 +00004012 }
4013 }
Greg Clayton262f80d2011-07-06 16:49:27 +00004014 }
Jim Ingham6035b672011-03-31 00:19:25 +00004015 else
4016 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00004017 // We used to have a thread, but now it is gone
4018 SetInvalid();
Greg Clayton23f59502012-07-17 03:23:13 +00004019 changed = was_valid;
Jim Ingham6035b672011-03-31 00:19:25 +00004020 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00004021
Jim Ingham6035b672011-03-31 00:19:25 +00004022 }
Jim Ingham9ee01152011-12-10 01:49:43 +00004023 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00004024}
4025
Jim Ingham61be0902011-05-02 18:13:59 +00004026void
4027ValueObject::EvaluationPoint::SetUpdated ()
4028{
Greg Claytoncc4d0142012-02-17 07:49:44 +00004029 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
4030 if (process_sp)
4031 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00004032 m_first_update = false;
4033 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00004034}
4035
4036
Greg Claytoncc4d0142012-02-17 07:49:44 +00004037//bool
4038//ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
4039//{
4040// if (!IsValid())
4041// return false;
4042//
4043// bool needs_update = false;
4044//
4045// // The target has to be non-null, and the
4046// Target *target = exe_scope->CalculateTarget();
4047// if (target != NULL)
4048// {
4049// Target *old_target = m_target_sp.get();
4050// assert (target == old_target);
4051// Process *process = exe_scope->CalculateProcess();
4052// if (process != NULL)
4053// {
4054// // FOR NOW - assume you can't update variable objects across process boundaries.
4055// Process *old_process = m_process_sp.get();
4056// assert (process == old_process);
4057// ProcessModID current_mod_id = process->GetModID();
4058// if (m_mod_id != current_mod_id)
4059// {
4060// needs_update = true;
4061// m_mod_id = current_mod_id;
4062// }
4063// // See if we're switching the thread or stack context. If no thread is given, this is
4064// // being evaluated in a global context.
4065// Thread *thread = exe_scope->CalculateThread();
4066// if (thread != NULL)
4067// {
4068// user_id_t new_thread_index = thread->GetIndexID();
4069// if (new_thread_index != m_thread_id)
4070// {
4071// needs_update = true;
4072// m_thread_id = new_thread_index;
4073// m_stack_id.Clear();
4074// }
4075//
4076// StackFrame *new_frame = exe_scope->CalculateStackFrame();
4077// if (new_frame != NULL)
4078// {
4079// if (new_frame->GetStackID() != m_stack_id)
4080// {
4081// needs_update = true;
4082// m_stack_id = new_frame->GetStackID();
4083// }
4084// }
4085// else
4086// {
4087// m_stack_id.Clear();
4088// needs_update = true;
4089// }
4090// }
4091// else
4092// {
4093// // If this had been given a thread, and now there is none, we should update.
4094// // Otherwise we don't have to do anything.
4095// if (m_thread_id != LLDB_INVALID_UID)
4096// {
4097// m_thread_id = LLDB_INVALID_UID;
4098// m_stack_id.Clear();
4099// needs_update = true;
4100// }
4101// }
4102// }
4103// else
4104// {
4105// // If there is no process, then we don't need to update anything.
4106// // But if we're switching from having a process to not, we should try to update.
4107// if (m_process_sp.get() != NULL)
4108// {
4109// needs_update = true;
4110// m_process_sp.reset();
4111// m_thread_id = LLDB_INVALID_UID;
4112// m_stack_id.Clear();
4113// }
4114// }
4115// }
4116// else
4117// {
4118// // If there's no target, nothing can change so we don't need to update anything.
4119// // But if we're switching from having a target to not, we should try to update.
4120// if (m_target_sp.get() != NULL)
4121// {
4122// needs_update = true;
4123// m_target_sp.reset();
4124// m_process_sp.reset();
4125// m_thread_id = LLDB_INVALID_UID;
4126// m_stack_id.Clear();
4127// }
4128// }
4129// if (!m_needs_update)
4130// m_needs_update = needs_update;
4131//
4132// return needs_update;
4133//}
Enrico Granataf2bbf712011-07-15 02:26:42 +00004134
4135void
Enrico Granata86cc9822012-03-19 22:58:49 +00004136ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00004137{
Enrico Granata86cc9822012-03-19 22:58:49 +00004138 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
4139 m_value_str.clear();
4140
4141 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
4142 m_location_str.clear();
4143
4144 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
4145 {
Enrico Granata86cc9822012-03-19 22:58:49 +00004146 m_summary_str.clear();
4147 }
4148
4149 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
4150 m_object_desc_str.clear();
4151
4152 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
4153 {
4154 if (m_synthetic_value)
4155 m_synthetic_value = NULL;
4156 }
Johnny Chen44805302011-07-19 19:48:13 +00004157}
Enrico Granata9128ee22011-09-06 19:20:51 +00004158
4159SymbolContextScope *
4160ValueObject::GetSymbolContextScope()
4161{
4162 if (m_parent)
4163 {
4164 if (!m_parent->IsPointerOrReferenceType())
4165 return m_parent->GetSymbolContextScope();
4166 }
4167 return NULL;
4168}
Enrico Granatab2698cd2012-09-13 18:27:09 +00004169
4170lldb::ValueObjectSP
4171ValueObject::CreateValueObjectFromExpression (const char* name,
4172 const char* expression,
4173 const ExecutionContext& exe_ctx)
4174{
4175 lldb::ValueObjectSP retval_sp;
4176 lldb::TargetSP target_sp(exe_ctx.GetTargetSP());
4177 if (!target_sp)
4178 return retval_sp;
4179 if (!expression || !*expression)
4180 return retval_sp;
4181 target_sp->EvaluateExpression (expression,
4182 exe_ctx.GetFrameSP().get(),
4183 retval_sp);
4184 if (retval_sp && name && *name)
4185 retval_sp->SetName(ConstString(name));
4186 return retval_sp;
4187}
4188
4189lldb::ValueObjectSP
4190ValueObject::CreateValueObjectFromAddress (const char* name,
4191 uint64_t address,
4192 const ExecutionContext& exe_ctx,
4193 ClangASTType type)
4194{
4195 ClangASTType pointer_type(type.GetASTContext(),type.GetPointerType());
4196 lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
4197 lldb::ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4198 pointer_type.GetASTContext(),
4199 pointer_type.GetOpaqueQualType(),
4200 ConstString(name),
4201 buffer,
4202 lldb::endian::InlHostByteOrder(),
4203 exe_ctx.GetAddressByteSize()));
4204 if (ptr_result_valobj_sp)
4205 {
4206 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
4207 Error err;
4208 ptr_result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
4209 if (ptr_result_valobj_sp && name && *name)
4210 ptr_result_valobj_sp->SetName(ConstString(name));
4211 }
4212 return ptr_result_valobj_sp;
4213}
4214
4215lldb::ValueObjectSP
4216ValueObject::CreateValueObjectFromData (const char* name,
4217 DataExtractor& data,
4218 const ExecutionContext& exe_ctx,
4219 ClangASTType type)
4220{
4221 lldb::ValueObjectSP new_value_sp;
4222 new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
4223 type.GetASTContext() ,
4224 type.GetOpaqueQualType(),
4225 ConstString(name),
4226 data,
4227 LLDB_INVALID_ADDRESS);
4228 new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
4229 if (new_value_sp && name && *name)
4230 new_value_sp->SetName(ConstString(name));
4231 return new_value_sp;
4232}
Enrico Granata4873e522013-04-11 22:48:58 +00004233
4234ModuleSP
4235ValueObject::GetModule ()
4236{
4237 ValueObject* root(GetRoot());
4238 if (root != this)
4239 return root->GetModule();
4240 return lldb::ModuleSP();
4241}
4242
4243ValueObject*
4244ValueObject::GetRoot ()
4245{
4246 if (m_root)
4247 return m_root;
4248 ValueObject* parent = m_parent;
4249 if (!parent)
4250 return (m_root = this);
4251 while (parent->m_parent)
4252 {
4253 if (parent->m_root)
4254 return (m_root = parent->m_root);
4255 parent = parent->m_parent;
4256 }
4257 return (m_root = parent);
4258}
4259
4260AddressType
4261ValueObject::GetAddressTypeOfChildren()
4262{
4263 if (m_address_type_of_ptr_or_ref_children == eAddressTypeInvalid)
4264 {
4265 ValueObject* root(GetRoot());
4266 if (root != this)
4267 return root->GetAddressTypeOfChildren();
4268 }
4269 return m_address_type_of_ptr_or_ref_children;
4270}
4271
4272lldb::DynamicValueType
4273ValueObject::GetDynamicValueType ()
4274{
4275 ValueObject* with_dv_info = this;
4276 while (with_dv_info)
4277 {
4278 if (with_dv_info->HasDynamicValueTypeInfo())
4279 return with_dv_info->GetDynamicValueTypeImpl();
4280 with_dv_info = with_dv_info->m_parent;
4281 }
4282 return lldb::eNoDynamicValues;
4283}
4284lldb::Format
4285ValueObject::GetFormat () const
4286{
4287 const ValueObject* with_fmt_info = this;
4288 while (with_fmt_info)
4289 {
4290 if (with_fmt_info->m_format != lldb::eFormatDefault)
4291 return with_fmt_info->m_format;
4292 with_fmt_info = with_fmt_info->m_parent;
4293 }
4294 return m_format;
4295}