blob: a13c84b7c38d5f6b18039601c692e7c7cd15c096 [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
10#include "lldb/Core/ValueObject.h"
11
12// C Includes
Greg Claytonf5e56de2010-09-14 23:36:40 +000013#include <stdlib.h>
14
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015// C++ Includes
16// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017#include "llvm/Support/raw_ostream.h"
Jim Ingham5a369122010-09-28 01:25:32 +000018#include "clang/AST/Type.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019
20// Project includes
21#include "lldb/Core/DataBufferHeap.h"
Enrico Granata0a976142011-08-22 22:03:47 +000022#include "lldb/Core/DataVisualization.h"
Enrico Granata4becb372011-06-29 22:27:15 +000023#include "lldb/Core/Debugger.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000024#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/StreamString.h"
26#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000027#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000028#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/ValueObjectList.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000030#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000031#include "lldb/Core/ValueObjectSyntheticFilter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
Greg Clayton7fb56d02011-02-01 01:31:41 +000033#include "lldb/Host/Endian.h"
34
Enrico Granata61a80ba2011-08-12 16:42:31 +000035#include "lldb/Interpreter/CommandInterpreter.h"
Enrico Granataf2bbf712011-07-15 02:26:42 +000036#include "lldb/Interpreter/ScriptInterpreterPython.h"
37
Greg Claytone1a916a2010-07-21 22:12:05 +000038#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Symbol/ClangASTContext.h"
40#include "lldb/Symbol/Type.h"
41
Jim Ingham53c47f12010-09-10 23:12:17 +000042#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000043#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000044#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Target/Process.h"
46#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000047#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049
Enrico Granataf4efecd2011-07-12 22:56:10 +000050#include "lldb/Utility/RefCounter.h"
51
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052using namespace lldb;
53using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000054using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
Greg Claytonafacd142011-09-02 01:15:17 +000056static user_id_t g_value_obj_uid = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057
58//----------------------------------------------------------------------
59// ValueObject constructor
60//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000061ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000062 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000063 m_parent (&parent),
Stephen Wilson71c21d12011-04-11 19:41:40 +000064 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065 m_name (),
66 m_data (),
67 m_value (),
68 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000069 m_value_str (),
70 m_old_value_str (),
71 m_location_str (),
72 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000073 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000074 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000075 m_children (),
76 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000077 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000078 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000079 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000080 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000081 m_last_format_mgr_revision(0),
Enrico Granatad8b5fce2011-08-02 23:12:24 +000082 m_last_format_mgr_dynamic(parent.m_last_format_mgr_dynamic),
Enrico Granata0c489f52012-03-01 04:24:26 +000083 m_type_summary_sp(),
84 m_type_format_sp(),
85 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +000086 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +000087 m_address_type_of_ptr_or_ref_children(eAddressTypeInvalid),
Greg Clayton288bdf92010-09-02 02:59:18 +000088 m_value_is_valid (false),
89 m_value_did_change (false),
90 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000091 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +000092 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +000093 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +000094 m_is_bitfield_for_scalar(false),
Enrico Granatad55546b2011-07-22 00:16:08 +000095 m_is_expression_path_child(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +000096 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +000097 m_is_getting_summary(false),
98 m_did_calculate_complete_objc_class_type(false)
Jim Ingham6035b672011-03-31 00:19:25 +000099{
Jim Ingham58b59f92011-04-22 23:53:53 +0000100 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000101}
102
103//----------------------------------------------------------------------
104// ValueObject constructor
105//----------------------------------------------------------------------
Enrico Granata9128ee22011-09-06 19:20:51 +0000106ValueObject::ValueObject (ExecutionContextScope *exe_scope,
107 AddressType child_ptr_or_ref_addr_type) :
Jim Ingham6035b672011-03-31 00:19:25 +0000108 UserID (++g_value_obj_uid), // Unique identifier for every value object
109 m_parent (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000110 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000111 m_name (),
112 m_data (),
113 m_value (),
114 m_error (),
115 m_value_str (),
116 m_old_value_str (),
117 m_location_str (),
118 m_summary_str (),
119 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000120 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000121 m_children (),
122 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000123 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000124 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000125 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000126 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000127 m_last_format_mgr_revision(0),
Greg Claytonafacd142011-09-02 01:15:17 +0000128 m_last_format_mgr_dynamic(eNoDynamicValues),
Enrico Granata0c489f52012-03-01 04:24:26 +0000129 m_type_summary_sp(),
130 m_type_format_sp(),
131 m_synthetic_children_sp(),
Jim Ingham4b536182011-08-09 02:12:22 +0000132 m_user_id_of_forced_summary(),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000133 m_address_type_of_ptr_or_ref_children(child_ptr_or_ref_addr_type),
Jim Ingham6035b672011-03-31 00:19:25 +0000134 m_value_is_valid (false),
135 m_value_did_change (false),
136 m_children_count_valid (false),
137 m_old_value_valid (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000138 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000139 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000140 m_is_bitfield_for_scalar(false),
Enrico Granatad55546b2011-07-22 00:16:08 +0000141 m_is_expression_path_child(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000142 m_is_child_at_offset(false),
Sean Callanan72772842012-02-22 23:57:45 +0000143 m_is_getting_summary(false),
144 m_did_calculate_complete_objc_class_type(false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145{
Jim Ingham58b59f92011-04-22 23:53:53 +0000146 m_manager = new ValueObjectManager();
147 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148}
149
150//----------------------------------------------------------------------
151// Destructor
152//----------------------------------------------------------------------
153ValueObject::~ValueObject ()
154{
155}
156
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000158ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000160 return UpdateValueIfNeeded(m_last_format_mgr_dynamic, update_format);
161}
162
163bool
Greg Claytonafacd142011-09-02 01:15:17 +0000164ValueObject::UpdateValueIfNeeded (DynamicValueType use_dynamic, bool update_format)
Enrico Granatac3e320a2011-08-02 17:27:39 +0000165{
Enrico Granata4becb372011-06-29 22:27:15 +0000166
Enrico Granata9128ee22011-09-06 19:20:51 +0000167 bool did_change_formats = false;
168
Enrico Granata0a3958e2011-07-02 00:25:22 +0000169 if (update_format)
Enrico Granata9128ee22011-09-06 19:20:51 +0000170 did_change_formats = UpdateFormatsIfNeeded(use_dynamic);
Enrico Granata4becb372011-06-29 22:27:15 +0000171
Greg Claytonb71f3842010-10-05 03:13:51 +0000172 // If this is a constant value, then our success is predicated on whether
173 // we have an error or not
174 if (GetIsConstant())
Enrico Granata9128ee22011-09-06 19:20:51 +0000175 {
176 // if you were asked to update your formatters, but did not get a chance to do it
177 // clear your own values (this serves the purpose of faking a stop-id for frozen
178 // objects (which are regarded as constant, but could have changes behind their backs
179 // because of the frozen-pointer depth limit)
180 // TODO: decouple summary from value and then remove this code and only force-clear the summary
181 if (update_format && !did_change_formats)
Enrico Granata86cc9822012-03-19 22:58:49 +0000182 ClearUserVisibleData(eClearUserVisibleDataItemsSummary);
Greg Claytonb71f3842010-10-05 03:13:51 +0000183 return m_error.Success();
Enrico Granata9128ee22011-09-06 19:20:51 +0000184 }
Greg Claytonb71f3842010-10-05 03:13:51 +0000185
Jim Ingham6035b672011-03-31 00:19:25 +0000186 bool first_update = m_update_point.IsFirstEvaluation();
187
188 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000189 {
Jim Ingham6035b672011-03-31 00:19:25 +0000190 m_update_point.SetUpdated();
191
192 // Save the old value using swap to avoid a string copy which
193 // also will clear our m_value_str
194 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000195 {
Jim Ingham6035b672011-03-31 00:19:25 +0000196 m_old_value_valid = false;
197 }
198 else
199 {
200 m_old_value_valid = true;
201 m_old_value_str.swap (m_value_str);
Enrico Granata86cc9822012-03-19 22:58:49 +0000202 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham6035b672011-03-31 00:19:25 +0000203 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000204
Enrico Granataf2bbf712011-07-15 02:26:42 +0000205 ClearUserVisibleData();
206
Greg Claytonefbc7d22012-03-09 04:23:44 +0000207 if (IsInScope())
Jim Ingham6035b672011-03-31 00:19:25 +0000208 {
Greg Claytonefbc7d22012-03-09 04:23:44 +0000209 const bool value_was_valid = GetValueIsValid();
210 SetValueDidChange (false);
211
212 m_error.Clear();
213
214 // Call the pure virtual function to update the value
215 bool success = UpdateValue ();
216
217 SetValueIsValid (success);
218
219 if (first_update)
220 SetValueDidChange (false);
221 else if (!m_value_did_change && success == false)
222 {
223 // The value wasn't gotten successfully, so we mark this
224 // as changed if the value used to be valid and now isn't
225 SetValueDidChange (value_was_valid);
226 }
227 }
228 else
229 {
230 m_error.SetErrorString("out of scope");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 }
232 }
233 return m_error.Success();
234}
235
Enrico Granata9128ee22011-09-06 19:20:51 +0000236bool
Greg Claytonafacd142011-09-02 01:15:17 +0000237ValueObject::UpdateFormatsIfNeeded(DynamicValueType use_dynamic)
Enrico Granata4becb372011-06-29 22:27:15 +0000238{
Enrico Granata6f3533f2011-07-29 19:53:35 +0000239 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
240 if (log)
241 log->Printf("checking for FormatManager revisions. VO named %s is at revision %d, while the format manager is at revision %d",
242 GetName().GetCString(),
Enrico Granata4becb372011-06-29 22:27:15 +0000243 m_last_format_mgr_revision,
Enrico Granata85933ed2011-08-18 16:38:26 +0000244 DataVisualization::GetCurrentRevision());
Enrico Granata9128ee22011-09-06 19:20:51 +0000245
246 bool any_change = false;
247
Enrico Granata85933ed2011-08-18 16:38:26 +0000248 if ( (m_last_format_mgr_revision != DataVisualization::GetCurrentRevision()) ||
Enrico Granatac3e320a2011-08-02 17:27:39 +0000249 m_last_format_mgr_dynamic != use_dynamic)
Enrico Granata4becb372011-06-29 22:27:15 +0000250 {
Enrico Granata78d06382011-09-09 23:33:14 +0000251 SetValueFormat(DataVisualization::ValueFormats::GetFormat (*this, eNoDynamicValues));
252 SetSummaryFormat(DataVisualization::GetSummaryFormat (*this, use_dynamic));
253 SetSyntheticChildren(DataVisualization::GetSyntheticChildren (*this, use_dynamic));
Enrico Granata1490c6f2011-07-19 02:34:21 +0000254
Enrico Granata85933ed2011-08-18 16:38:26 +0000255 m_last_format_mgr_revision = DataVisualization::GetCurrentRevision();
Enrico Granatac3e320a2011-08-02 17:27:39 +0000256 m_last_format_mgr_dynamic = use_dynamic;
Enrico Granata855cd902011-09-06 22:59:55 +0000257
258 any_change = true;
Enrico Granata4becb372011-06-29 22:27:15 +0000259 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000260
261 return any_change;
262
Enrico Granata4becb372011-06-29 22:27:15 +0000263}
264
Jim Ingham16e0c682011-08-12 23:34:31 +0000265void
266ValueObject::SetNeedsUpdate ()
267{
268 m_update_point.SetNeedsUpdate();
269 // We have to clear the value string here so ConstResult children will notice if their values are
270 // changed by hand (i.e. with SetValueAsCString).
Enrico Granata86cc9822012-03-19 22:58:49 +0000271 ClearUserVisibleData(eClearUserVisibleDataItemsValue);
Jim Ingham16e0c682011-08-12 23:34:31 +0000272}
273
Sean Callanan72772842012-02-22 23:57:45 +0000274ClangASTType
275ValueObject::MaybeCalculateCompleteType ()
276{
277 ClangASTType ret(GetClangASTImpl(), GetClangTypeImpl());
278
279 if (m_did_calculate_complete_objc_class_type)
280 {
281 if (m_override_type.IsValid())
282 return m_override_type;
283 else
284 return ret;
285 }
286
287 clang_type_t ast_type(GetClangTypeImpl());
288 clang_type_t class_type;
289 bool is_pointer_type;
290
291 if (ClangASTContext::IsObjCObjectPointerType(ast_type, &class_type))
292 {
293 is_pointer_type = true;
294 }
295 else if (ClangASTContext::IsObjCClassType(ast_type))
296 {
297 is_pointer_type = false;
298 class_type = ast_type;
299 }
300 else
301 {
302 return ret;
303 }
304
305 m_did_calculate_complete_objc_class_type = true;
306
307 if (!class_type)
308 return ret;
309
310 std::string class_name;
311
312 if (!ClangASTContext::GetObjCClassName(class_type, class_name))
313 return ret;
314
315 ProcessSP process_sp(GetUpdatePoint().GetExecutionContextRef().GetProcessSP());
316
317 if (!process_sp)
318 return ret;
319
320 ObjCLanguageRuntime *objc_language_runtime(process_sp->GetObjCLanguageRuntime());
321
322 if (!objc_language_runtime)
323 return ret;
324
325 ConstString class_name_cs(class_name.c_str());
326
327 TypeSP complete_objc_class_type_sp = objc_language_runtime->LookupInCompleteClassCache(class_name_cs);
328
329 if (!complete_objc_class_type_sp)
330 return ret;
331
332 ClangASTType complete_class(complete_objc_class_type_sp->GetClangAST(),
333 complete_objc_class_type_sp->GetClangFullType());
334
335 if (!ClangASTContext::GetCompleteType(complete_class.GetASTContext(),
336 complete_class.GetOpaqueQualType()))
337 return ret;
338
339 if (is_pointer_type)
340 {
341 clang_type_t pointer_type = ClangASTContext::CreatePointerType(complete_class.GetASTContext(),
342 complete_class.GetOpaqueQualType());
343
344 m_override_type = ClangASTType(complete_class.GetASTContext(),
345 pointer_type);
346 }
347 else
348 {
349 m_override_type = complete_class;
350 }
351
352 return m_override_type;
353}
354
355clang::ASTContext *
356ValueObject::GetClangAST ()
357{
358 ClangASTType type = MaybeCalculateCompleteType();
359
360 return type.GetASTContext();
361}
362
363lldb::clang_type_t
364ValueObject::GetClangType ()
365{
366 ClangASTType type = MaybeCalculateCompleteType();
367
368 return type.GetOpaqueQualType();
369}
370
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371DataExtractor &
372ValueObject::GetDataExtractor ()
373{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000374 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 return m_data;
376}
377
378const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000379ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000381 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 return m_error;
383}
384
385const ConstString &
386ValueObject::GetName() const
387{
388 return m_name;
389}
390
391const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000392ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000394 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 {
396 if (m_location_str.empty())
397 {
398 StreamString sstr;
399
400 switch (m_value.GetValueType())
401 {
402 default:
403 break;
404
405 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000406 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000407 {
408 RegisterInfo *reg_info = m_value.GetRegisterInfo();
409 if (reg_info)
410 {
411 if (reg_info->name)
412 m_location_str = reg_info->name;
413 else if (reg_info->alt_name)
414 m_location_str = reg_info->alt_name;
415 break;
416 }
417 }
418 m_location_str = "scalar";
419 break;
420
421 case Value::eValueTypeLoadAddress:
422 case Value::eValueTypeFileAddress:
423 case Value::eValueTypeHostAddress:
424 {
425 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
426 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
427 m_location_str.swap(sstr.GetString());
428 }
429 break;
430 }
431 }
432 }
433 return m_location_str.c_str();
434}
435
436Value &
437ValueObject::GetValue()
438{
439 return m_value;
440}
441
442const Value &
443ValueObject::GetValue() const
444{
445 return m_value;
446}
447
448bool
Jim Ingham6035b672011-03-31 00:19:25 +0000449ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000450{
Enrico Granata6fd87d52011-08-04 01:41:02 +0000451 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
452 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000453 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Ingham16e0c682011-08-12 23:34:31 +0000454 Value tmp_value(m_value);
455 scalar = tmp_value.ResolveValue(&exe_ctx, GetClangAST ());
Greg Claytondcad5022011-12-29 01:26:56 +0000456 if (scalar.IsValid())
457 {
458 const uint32_t bitfield_bit_size = GetBitfieldBitSize();
459 if (bitfield_bit_size)
460 return scalar.ExtractBitfield (bitfield_bit_size, GetBitfieldBitOffset());
461 return true;
462 }
Enrico Granata6fd87d52011-08-04 01:41:02 +0000463 }
Greg Claytondcad5022011-12-29 01:26:56 +0000464 return false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000465}
466
467bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000468ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469{
Greg Clayton288bdf92010-09-02 02:59:18 +0000470 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471}
472
473
474void
475ValueObject::SetValueIsValid (bool b)
476{
Greg Clayton288bdf92010-09-02 02:59:18 +0000477 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478}
479
480bool
Jim Ingham6035b672011-03-31 00:19:25 +0000481ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482{
Jim Ingham6035b672011-03-31 00:19:25 +0000483 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000484 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485}
486
487void
488ValueObject::SetValueDidChange (bool value_changed)
489{
Greg Clayton288bdf92010-09-02 02:59:18 +0000490 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000491}
492
493ValueObjectSP
494ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
495{
496 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000497 // We may need to update our value if we are dynamic
498 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000499 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000500 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000501 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000502 // Check if we have already made the child value object?
Enrico Granata9d60f602012-03-09 03:09:58 +0000503 if (can_create && !m_children.HasChildAtIndex(idx))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000505 // No we haven't created the child at this index, so lets have our
506 // subclass do it and cache the result for quick future access.
Enrico Granata9d60f602012-03-09 03:09:58 +0000507 m_children.SetChildAtIndex(idx,CreateChildAtIndex (idx, false, 0));
Jim Ingham78a685a2011-04-16 00:01:13 +0000508 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000509
Enrico Granata9d60f602012-03-09 03:09:58 +0000510 ValueObject* child = m_children.GetChildAtIndex(idx);
511 if (child != NULL)
512 return child->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000513 }
514 return child_sp;
515}
516
517uint32_t
518ValueObject::GetIndexOfChildWithName (const ConstString &name)
519{
520 bool omit_empty_base_classes = true;
521 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000522 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000523 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524 omit_empty_base_classes);
525}
526
527ValueObjectSP
528ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
529{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000530 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531 // classes (which really aren't part of the expression path), so we
532 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000534
Greg Claytondea8cb42011-06-29 22:09:02 +0000535 // We may need to update our value if we are dynamic
536 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000537 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000538
539 std::vector<uint32_t> child_indexes;
540 clang::ASTContext *clang_ast = GetClangAST();
541 void *clang_type = GetClangType();
542 bool omit_empty_base_classes = true;
543 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
544 clang_type,
545 name.GetCString(),
546 omit_empty_base_classes,
547 child_indexes);
548 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000550 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
551 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
552
553 child_sp = GetChildAtIndex(*pos, can_create);
554 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000556 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000557 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000558 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
559 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000560 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000561 else
562 {
563 child_sp.reset();
564 }
565
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566 }
567 }
568 return child_sp;
569}
570
571
572uint32_t
573ValueObject::GetNumChildren ()
574{
Enrico Granatac5bc4122012-03-27 02:35:13 +0000575 UpdateValueIfNeeded();
Greg Clayton288bdf92010-09-02 02:59:18 +0000576 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000577 {
578 SetNumChildren (CalculateNumChildren());
579 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000580 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582void
583ValueObject::SetNumChildren (uint32_t num_children)
584{
Greg Clayton288bdf92010-09-02 02:59:18 +0000585 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000586 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
589void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590ValueObject::SetName (const ConstString &name)
591{
592 m_name = name;
593}
594
Jim Ingham58b59f92011-04-22 23:53:53 +0000595ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
597{
Jim Ingham2eec4872011-05-07 00:10:58 +0000598 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000599
Greg Claytondea8cb42011-06-29 22:09:02 +0000600 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000601 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000602 std::string child_name_str;
603 uint32_t child_byte_size = 0;
604 int32_t child_byte_offset = 0;
605 uint32_t child_bitfield_bit_size = 0;
606 uint32_t child_bitfield_bit_offset = 0;
607 bool child_is_base_class = false;
608 bool child_is_deref_of_parent = false;
609
610 const bool transparent_pointers = synthetic_array_member == false;
611 clang::ASTContext *clang_ast = GetClangAST();
612 clang_type_t clang_type = GetClangType();
613 clang_type_t child_clang_type;
614
Greg Claytoncc4d0142012-02-17 07:49:44 +0000615 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000616
617 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
618 clang_ast,
619 GetName().GetCString(),
620 clang_type,
621 idx,
622 transparent_pointers,
623 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +0000624 ignore_array_bounds,
Greg Claytondea8cb42011-06-29 22:09:02 +0000625 child_name_str,
626 child_byte_size,
627 child_byte_offset,
628 child_bitfield_bit_size,
629 child_bitfield_bit_offset,
630 child_is_base_class,
631 child_is_deref_of_parent);
632 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000633 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000634 if (synthetic_index)
635 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000636
Greg Claytondea8cb42011-06-29 22:09:02 +0000637 ConstString child_name;
638 if (!child_name_str.empty())
639 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640
Greg Claytondea8cb42011-06-29 22:09:02 +0000641 valobj = new ValueObjectChild (*this,
642 clang_ast,
643 child_clang_type,
644 child_name,
645 child_byte_size,
646 child_byte_offset,
647 child_bitfield_bit_size,
648 child_bitfield_bit_offset,
649 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000650 child_is_deref_of_parent,
651 eAddressTypeInvalid);
652 //if (valobj)
653 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
654 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000655
Jim Ingham58b59f92011-04-22 23:53:53 +0000656 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657}
658
Enrico Granata0c489f52012-03-01 04:24:26 +0000659bool
660ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
661 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662{
Enrico Granata0c489f52012-03-01 04:24:26 +0000663 destination.clear();
664
665 // ideally we would like to bail out if passing NULL, but if we do so
666 // we end up not providing the summary for function pointers anymore
667 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
668 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000669
670 m_is_getting_summary = true;
Enrico Granata0c489f52012-03-01 04:24:26 +0000671 if (UpdateValueIfNeeded (false))
672 {
673 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000675 if (HasSyntheticValue())
676 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
677 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000678 }
679 else
680 {
681 clang_type_t clang_type = GetClangType();
682
683 // Do some default printout for function pointers
684 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000685 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000686 StreamString sstr;
687 clang_type_t elem_or_pointee_clang_type;
688 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
689 GetClangAST(),
690 &elem_or_pointee_clang_type));
691
692 if (ClangASTContext::IsFunctionPointerType (clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000693 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000694 AddressType func_ptr_address_type = eAddressTypeInvalid;
695 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
696 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000697 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000698 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000699 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000700 case eAddressTypeInvalid:
701 case eAddressTypeFile:
702 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000703
Greg Claytoncc4d0142012-02-17 07:49:44 +0000704 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000705 {
706 ExecutionContext exe_ctx (GetExecutionContextRef());
707
708 Address so_addr;
709 Target *target = exe_ctx.GetTargetPtr();
710 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000711 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000712 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000713 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000714 so_addr.Dump (&sstr,
715 exe_ctx.GetBestExecutionContextScope(),
716 Address::DumpStyleResolvedDescription,
717 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000718 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000719 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000720 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000721 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000722
Greg Claytoncc4d0142012-02-17 07:49:44 +0000723 case eAddressTypeHost:
724 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000725 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000726 }
727 if (sstr.GetSize() > 0)
728 {
729 destination.assign (1, '(');
730 destination.append (sstr.GetData(), sstr.GetSize());
731 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 }
733 }
734 }
735 }
736 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000737 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000738 return !destination.empty();
739}
740
741const char *
742ValueObject::GetSummaryAsCString ()
743{
744 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
745 {
746 GetSummaryAsCString(GetSummaryFormat().get(),
747 m_summary_str);
748 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000749 if (m_summary_str.empty())
750 return NULL;
751 return m_summary_str.c_str();
752}
753
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000754bool
755ValueObject::IsCStringContainer(bool check_pointer)
756{
757 clang_type_t elem_or_pointee_clang_type;
758 const Flags type_flags (ClangASTContext::GetTypeInfo (GetClangType(),
759 GetClangAST(),
760 &elem_or_pointee_clang_type));
761 bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
762 ClangASTContext::IsCharType (elem_or_pointee_clang_type));
763 if (!is_char_arr_ptr)
764 return false;
765 if (!check_pointer)
766 return true;
767 if (type_flags.Test(ClangASTContext::eTypeIsArray))
768 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000769 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000770 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000771 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000772 return (cstr_address != LLDB_INVALID_ADDRESS);
773}
774
Enrico Granata9128ee22011-09-06 19:20:51 +0000775size_t
776ValueObject::GetPointeeData (DataExtractor& data,
777 uint32_t item_idx,
778 uint32_t item_count)
779{
780 if (!IsPointerType() && !IsArrayType())
781 return 0;
782
783 if (item_count == 0)
784 return 0;
785
786 uint32_t stride = 0;
787
788 ClangASTType type(GetClangAST(),
789 GetClangType());
790
791 const uint64_t item_type_size = (IsPointerType() ? ClangASTType::GetTypeByteSize(GetClangAST(), type.GetPointeeType()) :
792 ClangASTType::GetTypeByteSize(GetClangAST(), type.GetArrayElementType(stride)));
793
794 const uint64_t bytes = item_count * item_type_size;
795
796 const uint64_t offset = item_idx * item_type_size;
797
798 if (item_idx == 0 && item_count == 1) // simply a deref
799 {
800 if (IsPointerType())
801 {
802 Error error;
803 ValueObjectSP pointee_sp = Dereference(error);
804 if (error.Fail() || pointee_sp.get() == NULL)
805 return 0;
806 return pointee_sp->GetDataExtractor().Copy(data);
807 }
808 else
809 {
810 ValueObjectSP child_sp = GetChildAtIndex(0, true);
811 if (child_sp.get() == NULL)
812 return 0;
813 return child_sp->GetDataExtractor().Copy(data);
814 }
815 return true;
816 }
817 else /* (items > 1) */
818 {
819 Error error;
820 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
821 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
822
823 AddressType addr_type;
824 lldb::addr_t addr = IsPointerType() ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
825
Enrico Granata9128ee22011-09-06 19:20:51 +0000826 switch (addr_type)
827 {
828 case eAddressTypeFile:
829 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000830 ModuleSP module_sp (GetModule());
831 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +0000832 {
833 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +0000834 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000835 ExecutionContext exe_ctx (GetExecutionContextRef());
836 Target* target = exe_ctx.GetTargetPtr();
837 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +0000838 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000839 heap_buf_ptr->SetByteSize(bytes);
840 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
841 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +0000842 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000843 data.SetData(data_sp);
844 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +0000845 }
846 }
847 }
848 }
849 break;
850 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +0000851 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000852 ExecutionContext exe_ctx (GetExecutionContextRef());
853 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +0000854 if (process)
855 {
856 heap_buf_ptr->SetByteSize(bytes);
857 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
858 if (error.Success())
859 {
860 data.SetData(data_sp);
861 return bytes_read;
862 }
863 }
864 }
865 break;
866 case eAddressTypeHost:
867 {
868 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes);
869 data.SetData(data_sp);
870 return bytes;
871 }
872 break;
873 case eAddressTypeInvalid:
874 default:
875 break;
876 }
877 }
878 return 0;
879}
880
881size_t
882ValueObject::GetData (DataExtractor& data)
883{
884 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000885 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytone72dfb32012-02-24 01:59:29 +0000886 Error error = m_value.GetValueAsData(&exe_ctx, GetClangAST(), data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +0000887 if (error.Fail())
888 return 0;
889 data.SetAddressByteSize(m_data.GetAddressByteSize());
890 data.SetByteOrder(m_data.GetByteOrder());
891 return data.GetByteSize();
892}
893
894// will compute strlen(str), but without consuming more than
895// maxlen bytes out of str (this serves the purpose of reading
896// chunks of a string without having to worry about
897// missing NULL terminators in the chunk)
898// of course, if strlen(str) > maxlen, the function will return
899// maxlen_value (which should be != maxlen, because that allows you
900// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
901static uint32_t
902strlen_or_inf (const char* str,
903 uint32_t maxlen,
904 uint32_t maxlen_value)
905{
906 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +0000907 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +0000908 {
Greg Clayton8dd5c172011-10-05 22:19:51 +0000909 while(*str)
910 {
911 len++;str++;
912 if (len > maxlen)
913 return maxlen_value;
914 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000915 }
916 return len;
917}
918
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000919void
Greg Claytoncc4d0142012-02-17 07:49:44 +0000920ValueObject::ReadPointedString (Stream& s,
921 Error& error,
922 uint32_t max_length,
923 bool honor_array,
924 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000925{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000926 ExecutionContext exe_ctx (GetExecutionContextRef());
927 Target* target = exe_ctx.GetTargetPtr();
928
929 if (target && max_length == 0)
930 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000931
932 clang_type_t clang_type = GetClangType();
933 clang_type_t elem_or_pointee_clang_type;
934 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
935 GetClangAST(),
936 &elem_or_pointee_clang_type));
937 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
938 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
939 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000940 if (target == NULL)
941 {
942 s << "<no target to read from>";
943 }
944 else
945 {
946 addr_t cstr_address = LLDB_INVALID_ADDRESS;
947 AddressType cstr_address_type = eAddressTypeInvalid;
948
949 size_t cstr_len = 0;
950 bool capped_data = false;
951 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000952 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000953 // We have an array
954 cstr_len = ClangASTContext::GetArraySize (clang_type);
955 if (cstr_len > max_length)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000956 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000957 capped_data = true;
958 cstr_len = max_length;
959 }
960 cstr_address = GetAddressOf (true, &cstr_address_type);
961 }
962 else
963 {
964 // We have a pointer
965 cstr_address = GetPointerValue (&cstr_address_type);
966 }
967 if (cstr_address != 0 && cstr_address != LLDB_INVALID_ADDRESS)
968 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000969 Address cstr_so_addr (cstr_address);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000970 DataExtractor data;
971 size_t bytes_read = 0;
972 if (cstr_len > 0 && honor_array)
973 {
974 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
975 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
976 GetPointeeData(data, 0, cstr_len);
977
978 if ((bytes_read = data.GetByteSize()) > 0)
979 {
980 s << '"';
981 data.Dump (&s,
982 0, // Start offset in "data"
983 item_format,
984 1, // Size of item (1 byte for a char!)
985 bytes_read, // How many bytes to print?
986 UINT32_MAX, // num per line
987 LLDB_INVALID_ADDRESS,// base address
988 0, // bitfield bit size
989 0); // bitfield bit offset
990 if (capped_data)
991 s << "...";
992 s << '"';
993 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000994 }
995 else
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000996 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000997 cstr_len = max_length;
998 const size_t k_max_buf_size = 64;
999
1000 size_t offset = 0;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001001
Greg Claytoncc4d0142012-02-17 07:49:44 +00001002 int cstr_len_displayed = -1;
1003 bool capped_cstr = false;
1004 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1005 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1006 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001007 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001008 const char *cstr = data.PeekCStr(0);
1009 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1010 if (len > k_max_buf_size)
1011 len = k_max_buf_size;
1012 if (cstr && cstr_len_displayed < 0)
1013 s << '"';
1014
1015 if (cstr_len_displayed < 0)
1016 cstr_len_displayed = len;
1017
1018 if (len == 0)
1019 break;
1020 cstr_len_displayed += len;
1021 if (len > bytes_read)
1022 len = bytes_read;
1023 if (len > cstr_len)
1024 len = cstr_len;
1025
1026 data.Dump (&s,
1027 0, // Start offset in "data"
1028 item_format,
1029 1, // Size of item (1 byte for a char!)
1030 len, // How many bytes to print?
1031 UINT32_MAX, // num per line
1032 LLDB_INVALID_ADDRESS,// base address
1033 0, // bitfield bit size
1034 0); // bitfield bit offset
1035
1036 if (len < k_max_buf_size)
1037 break;
1038
1039 if (len >= cstr_len)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001040 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001041 capped_cstr = true;
1042 break;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001043 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001044
1045 cstr_len -= len;
1046 offset += len;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001047 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001048
1049 if (cstr_len_displayed >= 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001050 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001051 s << '"';
1052 if (capped_cstr)
1053 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001054 }
1055 }
1056 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001057 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001058 }
1059 else
1060 {
1061 error.SetErrorString("impossible to read a string from this object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001062 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001063 }
1064}
1065
Jim Ingham53c47f12010-09-10 23:12:17 +00001066const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001067ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001068{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001069
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001070 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001071 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001072
1073 if (!m_object_desc_str.empty())
1074 return m_object_desc_str.c_str();
1075
Greg Claytoncc4d0142012-02-17 07:49:44 +00001076 ExecutionContext exe_ctx (GetExecutionContextRef());
1077 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001078 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001079 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001080
Jim Ingham53c47f12010-09-10 23:12:17 +00001081 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001082
Greg Claytonafacd142011-09-02 01:15:17 +00001083 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001084 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1085
Jim Inghama2cf2632010-12-23 02:29:54 +00001086 if (runtime == NULL)
1087 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001088 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +00001089 clang_type_t opaque_qual_type = GetClangType();
1090 if (opaque_qual_type != NULL)
1091 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001092 bool is_signed;
1093 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
1094 || ClangASTContext::IsPointerType (opaque_qual_type))
1095 {
Greg Claytonafacd142011-09-02 01:15:17 +00001096 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001097 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001098 }
1099 }
1100
Jim Ingham8d543de2011-03-31 23:01:21 +00001101 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001102 {
1103 m_object_desc_str.append (s.GetData());
1104 }
Sean Callanan672ad942010-10-23 00:18:49 +00001105
1106 if (m_object_desc_str.empty())
1107 return NULL;
1108 else
1109 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001110}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111
Enrico Granata0c489f52012-03-01 04:24:26 +00001112bool
1113ValueObject::GetValueAsCString (lldb::Format format,
1114 std::string& destination)
1115{
1116 if (ClangASTContext::IsAggregateType (GetClangType()) == false &&
1117 UpdateValueIfNeeded(false))
1118 {
1119 const Value::ContextType context_type = m_value.GetContextType();
1120
1121 switch (context_type)
1122 {
1123 case Value::eContextTypeClangType:
1124 case Value::eContextTypeLLDBType:
1125 case Value::eContextTypeVariable:
1126 {
1127 clang_type_t clang_type = GetClangType ();
1128 if (clang_type)
1129 {
1130 StreamString sstr;
1131 ExecutionContext exe_ctx (GetExecutionContextRef());
1132 ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
1133 clang_type, // The clang type to display
1134 &sstr,
1135 format, // Format to display this type with
1136 m_data, // Data to extract from
1137 0, // Byte offset into "m_data"
1138 GetByteSize(), // Byte size of item in "m_data"
1139 GetBitfieldBitSize(), // Bitfield bit size
1140 GetBitfieldBitOffset(), // Bitfield bit offset
1141 exe_ctx.GetBestExecutionContextScope());
1142 // Don't set the m_error to anything here otherwise
1143 // we won't be able to re-format as anything else. The
1144 // code for ClangASTType::DumpTypeValue() should always
1145 // return something, even if that something contains
1146 // an error messsage. "m_error" is used to detect errors
1147 // when reading the valid object, not for formatting errors.
1148 if (sstr.GetString().empty())
1149 destination.clear();
1150 else
1151 destination.swap(sstr.GetString());
1152 }
1153 }
1154 break;
1155
1156 case Value::eContextTypeRegisterInfo:
1157 {
1158 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1159 if (reg_info)
1160 {
1161 ExecutionContext exe_ctx (GetExecutionContextRef());
1162
1163 StreamString reg_sstr;
1164 m_data.Dump (&reg_sstr,
1165 0,
1166 format,
1167 reg_info->byte_size,
1168 1,
1169 UINT32_MAX,
1170 LLDB_INVALID_ADDRESS,
1171 0,
1172 0,
1173 exe_ctx.GetBestExecutionContextScope());
1174 destination.swap(reg_sstr.GetString());
1175 }
1176 }
1177 break;
1178
1179 default:
1180 break;
1181 }
1182 return !destination.empty();
1183 }
1184 else
1185 return false;
1186}
1187
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001189ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190{
Enrico Granata0c489f52012-03-01 04:24:26 +00001191 if (UpdateValueIfNeeded(true) && m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001193 lldb::Format my_format = GetFormat();
1194 if (m_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001195 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001196 if (m_type_format_sp)
1197 my_format = m_type_format_sp->GetFormat();
1198 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001199 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001200 if (m_is_bitfield_for_scalar)
1201 my_format = eFormatUnsigned;
1202 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001203 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001204 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001205 {
1206 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1207 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001208 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001210 else
1211 {
1212 clang_type_t clang_type = GetClangType ();
1213 my_format = ClangASTType::GetFormat(clang_type);
1214 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 }
1216 }
1217 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001218 if (GetValueAsCString(my_format, m_value_str))
1219 {
1220 if (!m_value_did_change && m_old_value_valid)
1221 {
1222 // The value was gotten successfully, so we consider the
1223 // value as changed if the value string differs
1224 SetValueDidChange (m_old_value_str != m_value_str);
1225 }
1226 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 }
1228 if (m_value_str.empty())
1229 return NULL;
1230 return m_value_str.c_str();
1231}
1232
Enrico Granatac3e320a2011-08-02 17:27:39 +00001233// if > 8bytes, 0 is returned. this method should mostly be used
1234// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001235uint64_t
1236ValueObject::GetValueAsUnsigned (uint64_t fail_value)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001237{
1238 // If our byte size is zero this is an aggregate type that has children
1239 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
1240 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001241 Scalar scalar;
1242 if (ResolveValue (scalar))
1243 return scalar.GetRawBits64(fail_value);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001244 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001245 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001246}
1247
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001248// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1249// this call up to date by returning true for your new special cases. We will eventually move
1250// to checking this call result before trying to display special cases
1251bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001252ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1253 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001254{
1255 clang_type_t elem_or_pointee_type;
1256 Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
1257
1258 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001259 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001260 {
1261 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001262 (custom_format == eFormatCString ||
1263 custom_format == eFormatCharArray ||
1264 custom_format == eFormatChar ||
1265 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001266 return true;
1267
1268 if (flags.Test(ClangASTContext::eTypeIsArray))
1269 {
Greg Claytonafacd142011-09-02 01:15:17 +00001270 if ((custom_format == eFormatBytes) ||
1271 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001272 return true;
1273
Greg Claytonafacd142011-09-02 01:15:17 +00001274 if ((custom_format == eFormatVectorOfChar) ||
1275 (custom_format == eFormatVectorOfFloat32) ||
1276 (custom_format == eFormatVectorOfFloat64) ||
1277 (custom_format == eFormatVectorOfSInt16) ||
1278 (custom_format == eFormatVectorOfSInt32) ||
1279 (custom_format == eFormatVectorOfSInt64) ||
1280 (custom_format == eFormatVectorOfSInt8) ||
1281 (custom_format == eFormatVectorOfUInt128) ||
1282 (custom_format == eFormatVectorOfUInt16) ||
1283 (custom_format == eFormatVectorOfUInt32) ||
1284 (custom_format == eFormatVectorOfUInt64) ||
1285 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001286 return true;
1287 }
1288 }
1289 return false;
1290}
1291
Enrico Granata9fc19442011-07-06 02:13:41 +00001292bool
1293ValueObject::DumpPrintableRepresentation(Stream& s,
1294 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001295 Format custom_format,
Enrico Granata86cc9822012-03-19 22:58:49 +00001296 PrintableRepresentationSpecialCases special)
Enrico Granata9fc19442011-07-06 02:13:41 +00001297{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001298
1299 clang_type_t elem_or_pointee_type;
1300 Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001301
Enrico Granata86cc9822012-03-19 22:58:49 +00001302 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1303 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1304
1305 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001306 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001307 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1308 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001309 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001310 // when being asked to get a printable display an array or pointer type directly,
1311 // try to "do the right thing"
1312
1313 if (IsCStringContainer(true) &&
1314 (custom_format == eFormatCString ||
1315 custom_format == eFormatCharArray ||
1316 custom_format == eFormatChar ||
1317 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001318 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001319 Error error;
1320 ReadPointedString(s,
1321 error,
1322 0,
1323 (custom_format == eFormatVectorOfChar) ||
1324 (custom_format == eFormatCharArray));
1325 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001326 }
1327
Enrico Granata86cc9822012-03-19 22:58:49 +00001328 if (custom_format == eFormatEnum)
1329 return false;
1330
1331 // this only works for arrays, because I have no way to know when
1332 // the pointed memory ends, and no special \0 end of data marker
1333 if (flags.Test(ClangASTContext::eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001334 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001335 if ((custom_format == eFormatBytes) ||
1336 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001337 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001338 uint32_t count = GetNumChildren();
1339
1340 s << '[';
1341 for (uint32_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001342 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001343
1344 if (low)
1345 s << ',';
1346
1347 ValueObjectSP child = GetChildAtIndex(low,true);
1348 if (!child.get())
1349 {
1350 s << "<invalid child>";
1351 continue;
1352 }
1353 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1354 }
1355
1356 s << ']';
1357
1358 return true;
1359 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001360
Enrico Granata86cc9822012-03-19 22:58:49 +00001361 if ((custom_format == eFormatVectorOfChar) ||
1362 (custom_format == eFormatVectorOfFloat32) ||
1363 (custom_format == eFormatVectorOfFloat64) ||
1364 (custom_format == eFormatVectorOfSInt16) ||
1365 (custom_format == eFormatVectorOfSInt32) ||
1366 (custom_format == eFormatVectorOfSInt64) ||
1367 (custom_format == eFormatVectorOfSInt8) ||
1368 (custom_format == eFormatVectorOfUInt128) ||
1369 (custom_format == eFormatVectorOfUInt16) ||
1370 (custom_format == eFormatVectorOfUInt32) ||
1371 (custom_format == eFormatVectorOfUInt64) ||
1372 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1373 {
1374 uint32_t count = GetNumChildren();
1375
1376 Format format = FormatManager::GetSingleItemFormat(custom_format);
1377
1378 s << '[';
1379 for (uint32_t low = 0; low < count; low++)
1380 {
1381
1382 if (low)
1383 s << ',';
1384
1385 ValueObjectSP child = GetChildAtIndex(low,true);
1386 if (!child.get())
1387 {
1388 s << "<invalid child>";
1389 continue;
1390 }
1391 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1392 }
1393
1394 s << ']';
1395
1396 return true;
1397 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001398 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001399
1400 if ((custom_format == eFormatBoolean) ||
1401 (custom_format == eFormatBinary) ||
1402 (custom_format == eFormatChar) ||
1403 (custom_format == eFormatCharPrintable) ||
1404 (custom_format == eFormatComplexFloat) ||
1405 (custom_format == eFormatDecimal) ||
1406 (custom_format == eFormatHex) ||
1407 (custom_format == eFormatFloat) ||
1408 (custom_format == eFormatOctal) ||
1409 (custom_format == eFormatOSType) ||
1410 (custom_format == eFormatUnicode16) ||
1411 (custom_format == eFormatUnicode32) ||
1412 (custom_format == eFormatUnsigned) ||
1413 (custom_format == eFormatPointer) ||
1414 (custom_format == eFormatComplexInteger) ||
1415 (custom_format == eFormatComplex) ||
1416 (custom_format == eFormatDefault)) // use the [] operator
1417 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001418 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001419 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001420
1421 if (only_special)
1422 return false;
1423
Enrico Granata86cc9822012-03-19 22:58:49 +00001424 bool var_success = false;
1425
1426 {
1427 const char * return_value;
1428 std::string alloc_mem;
1429
1430 if (custom_format != eFormatInvalid)
1431 SetFormat(custom_format);
1432
1433 switch(val_obj_display)
1434 {
1435 case eValueObjectRepresentationStyleValue:
1436 return_value = GetValueAsCString();
1437 break;
1438
1439 case eValueObjectRepresentationStyleSummary:
1440 return_value = GetSummaryAsCString();
1441 break;
1442
1443 case eValueObjectRepresentationStyleLanguageSpecific:
1444 return_value = GetObjectDescription();
1445 break;
1446
1447 case eValueObjectRepresentationStyleLocation:
1448 return_value = GetLocationAsCString();
1449 break;
1450
1451 case eValueObjectRepresentationStyleChildrenCount:
1452 {
1453 alloc_mem.resize(512);
1454 return_value = &alloc_mem[0];
1455 int count = GetNumChildren();
1456 snprintf((char*)return_value, 512, "%d", count);
1457 }
1458 break;
1459
1460 case eValueObjectRepresentationStyleType:
1461 return_value = GetTypeName().AsCString();
1462 break;
1463
1464 default:
1465 break;
1466 }
1467
1468 if (!return_value)
1469 {
1470 if (val_obj_display == eValueObjectRepresentationStyleValue)
1471 return_value = GetSummaryAsCString();
1472 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1473 {
1474 if (ClangASTContext::IsAggregateType (GetClangType()) == true)
1475 {
1476 // this thing has no value, and it seems to have no summary
1477 // some combination of unitialized data and other factors can also
1478 // raise this condition, so let's print a nice generic description
1479 {
1480 alloc_mem.resize(684);
1481 return_value = &alloc_mem[0];
1482 snprintf((char*)return_value, 684, "%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1483 }
1484 }
1485 else
1486 return_value = GetValueAsCString();
1487 }
1488 }
1489
1490 if (return_value)
1491 s.PutCString(return_value);
1492 else
1493 {
1494 if (m_error.Fail())
1495 s.Printf("<%s>", m_error.AsCString());
1496 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1497 s.PutCString("<no summary available>");
1498 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1499 s.PutCString("<no value available>");
1500 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1501 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1502 else
1503 s.PutCString("<no printable representation>");
1504 }
1505
1506 // we should only return false here if we could not do *anything*
1507 // even if we have an error message as output, that's a success
1508 // from our callers' perspective, so return true
1509 var_success = true;
1510
1511 if (custom_format != eFormatInvalid)
1512 SetFormat(eFormatDefault);
1513 }
1514
Enrico Granataf4efecd2011-07-12 22:56:10 +00001515 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001516}
1517
Greg Clayton737b9322010-09-13 03:32:57 +00001518addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001519ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001520{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001521 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001522 return LLDB_INVALID_ADDRESS;
1523
Greg Clayton73b472d2010-10-27 03:32:59 +00001524 switch (m_value.GetValueType())
1525 {
1526 case Value::eValueTypeScalar:
1527 if (scalar_is_load_address)
1528 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001529 if(address_type)
1530 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001531 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1532 }
1533 break;
1534
1535 case Value::eValueTypeLoadAddress:
1536 case Value::eValueTypeFileAddress:
1537 case Value::eValueTypeHostAddress:
1538 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001539 if(address_type)
1540 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001541 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1542 }
1543 break;
1544 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001545 if (address_type)
1546 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001547 return LLDB_INVALID_ADDRESS;
1548}
1549
1550addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001551ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001552{
Greg Claytonafacd142011-09-02 01:15:17 +00001553 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001554 if(address_type)
1555 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001556
Enrico Granatac3e320a2011-08-02 17:27:39 +00001557 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001558 return address;
1559
Greg Clayton73b472d2010-10-27 03:32:59 +00001560 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001561 {
1562 case Value::eValueTypeScalar:
Enrico Granata9128ee22011-09-06 19:20:51 +00001563 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001564 break;
1565
Enrico Granata9128ee22011-09-06 19:20:51 +00001566 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001567 case Value::eValueTypeLoadAddress:
1568 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001569 {
1570 uint32_t data_offset = 0;
1571 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001572 }
1573 break;
1574 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001575
Enrico Granata9128ee22011-09-06 19:20:51 +00001576 if (address_type)
1577 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001578
Greg Clayton737b9322010-09-13 03:32:57 +00001579 return address;
1580}
1581
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001582bool
Jim Ingham6035b672011-03-31 00:19:25 +00001583ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584{
1585 // Make sure our value is up to date first so that our location and location
1586 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001587 if (!UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588 return false;
1589
1590 uint32_t count = 0;
Greg Claytonafacd142011-09-02 01:15:17 +00001591 Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001592
Greg Claytonb1320972010-07-14 00:18:15 +00001593 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001594
Jim Ingham16e0c682011-08-12 23:34:31 +00001595 Value::ValueType value_type = m_value.GetValueType();
1596
1597 if (value_type == Value::eValueTypeScalar)
1598 {
1599 // If the value is already a scalar, then let the scalar change itself:
1600 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1601 }
1602 else if (byte_size <= Scalar::GetMaxByteSize())
1603 {
1604 // If the value fits in a scalar, then make a new scalar and again let the
1605 // scalar code do the conversion, then figure out where to put the new value.
1606 Scalar new_scalar;
1607 Error error;
1608 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1609 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001610 {
Jim Ingham4b536182011-08-09 02:12:22 +00001611 switch (value_type)
1612 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001613 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001614 {
1615 // If it is a load address, then the scalar value is the storage location
1616 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001617 ExecutionContext exe_ctx (GetExecutionContextRef());
1618 Process *process = exe_ctx.GetProcessPtr();
1619 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001620 {
Greg Claytonafacd142011-09-02 01:15:17 +00001621 addr_t target_addr = m_value.GetScalar().GetRawBits64(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001622 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1623 new_scalar,
1624 byte_size,
1625 error);
Jim Ingham16e0c682011-08-12 23:34:31 +00001626 if (!error.Success() || bytes_written != byte_size)
1627 return false;
1628 }
1629 }
Jim Ingham4b536182011-08-09 02:12:22 +00001630 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001631 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001632 {
1633 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1634 DataExtractor new_data;
1635 new_data.SetByteOrder (m_data.GetByteOrder());
1636
1637 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1638 m_data.SetData(buffer_sp, 0);
1639 bool success = new_scalar.GetData(new_data);
1640 if (success)
1641 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001642 new_data.CopyByteOrderedData (0,
1643 byte_size,
1644 const_cast<uint8_t *>(m_data.GetDataStart()),
1645 byte_size,
1646 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001647 }
1648 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1649
1650 }
Jim Ingham4b536182011-08-09 02:12:22 +00001651 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001652 case Value::eValueTypeFileAddress:
1653 case Value::eValueTypeScalar:
Jim Ingham16e0c682011-08-12 23:34:31 +00001654 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001655 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001656 }
1657 else
1658 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001659 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001660 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001661 }
1662 else
1663 {
1664 // We don't support setting things bigger than a scalar at present.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001665 return false;
1666 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001667
1668 // If we have reached this point, then we have successfully changed the value.
1669 SetNeedsUpdate();
1670 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671}
1672
Greg Clayton81e871e2012-02-04 02:27:34 +00001673bool
1674ValueObject::GetDeclaration (Declaration &decl)
1675{
1676 decl.Clear();
1677 return false;
1678}
1679
Greg Clayton84db9102012-03-26 23:03:23 +00001680ConstString
1681ValueObject::GetTypeName()
1682{
1683 return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
1684}
1685
1686ConstString
1687ValueObject::GetQualifiedTypeName()
1688{
1689 return ClangASTType::GetConstQualifiedTypeName (GetClangAST(), GetClangType());
1690}
1691
1692
Greg Claytonafacd142011-09-02 01:15:17 +00001693LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00001694ValueObject::GetObjectRuntimeLanguage ()
1695{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001696 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1697 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001698}
1699
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001700void
Jim Ingham58b59f92011-04-22 23:53:53 +00001701ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702{
Jim Ingham58b59f92011-04-22 23:53:53 +00001703 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001704}
1705
1706ValueObjectSP
1707ValueObject::GetSyntheticChild (const ConstString &key) const
1708{
1709 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001710 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001711 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001712 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 return synthetic_child_sp;
1714}
1715
1716bool
1717ValueObject::IsPointerType ()
1718{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001719 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001720}
1721
Jim Inghamb7603bb2011-03-18 00:05:18 +00001722bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001723ValueObject::IsArrayType ()
1724{
1725 return ClangASTContext::IsArrayType (GetClangType());
1726}
1727
1728bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001729ValueObject::IsScalarType ()
1730{
1731 return ClangASTContext::IsScalarType (GetClangType());
1732}
1733
1734bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001735ValueObject::IsIntegerType (bool &is_signed)
1736{
1737 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1738}
Greg Clayton73b472d2010-10-27 03:32:59 +00001739
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001740bool
1741ValueObject::IsPointerOrReferenceType ()
1742{
Greg Clayton007d5be2011-05-30 00:49:24 +00001743 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1744}
1745
1746bool
1747ValueObject::IsPossibleCPlusPlusDynamicType ()
1748{
1749 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750}
1751
Greg Claytondea8cb42011-06-29 22:09:02 +00001752bool
1753ValueObject::IsPossibleDynamicType ()
1754{
1755 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1756}
1757
Greg Claytonafacd142011-09-02 01:15:17 +00001758ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001759ValueObject::GetSyntheticArrayMember (int32_t index, bool can_create)
1760{
1761 if (IsArrayType())
1762 return GetSyntheticArrayMemberFromArray(index, can_create);
1763
1764 if (IsPointerType())
1765 return GetSyntheticArrayMemberFromPointer(index, can_create);
1766
1767 return ValueObjectSP();
1768
1769}
1770
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001771ValueObjectSP
1772ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1773{
1774 ValueObjectSP synthetic_child_sp;
1775 if (IsPointerType ())
1776 {
1777 char index_str[64];
1778 snprintf(index_str, sizeof(index_str), "[%i]", index);
1779 ConstString index_const_str(index_str);
1780 // Check if we have already created a synthetic array member in this
1781 // valid object. If we have we will re-use it.
1782 synthetic_child_sp = GetSyntheticChild (index_const_str);
1783 if (!synthetic_child_sp)
1784 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001785 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786 // We haven't made a synthetic array member for INDEX yet, so
1787 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001788 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789
1790 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001791 if (synthetic_child)
1792 {
1793 AddSyntheticChild(index_const_str, synthetic_child);
1794 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001795 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00001796 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001797 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001798 }
1799 }
1800 return synthetic_child_sp;
1801}
Jim Ingham22777012010-09-23 02:01:19 +00001802
Greg Claytondaf515f2011-07-09 20:12:33 +00001803// This allows you to create an array member using and index
1804// that doesn't not fall in the normal bounds of the array.
1805// Many times structure can be defined as:
1806// struct Collection
1807// {
1808// uint32_t item_count;
1809// Item item_array[0];
1810// };
1811// The size of the "item_array" is 1, but many times in practice
1812// there are more items in "item_array".
1813
1814ValueObjectSP
1815ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1816{
1817 ValueObjectSP synthetic_child_sp;
1818 if (IsArrayType ())
1819 {
1820 char index_str[64];
1821 snprintf(index_str, sizeof(index_str), "[%i]", index);
1822 ConstString index_const_str(index_str);
1823 // Check if we have already created a synthetic array member in this
1824 // valid object. If we have we will re-use it.
1825 synthetic_child_sp = GetSyntheticChild (index_const_str);
1826 if (!synthetic_child_sp)
1827 {
1828 ValueObject *synthetic_child;
1829 // We haven't made a synthetic array member for INDEX yet, so
1830 // lets make one and cache it for any future reference.
1831 synthetic_child = CreateChildAtIndex(0, true, index);
1832
1833 // Cache the value if we got one back...
1834 if (synthetic_child)
1835 {
1836 AddSyntheticChild(index_const_str, synthetic_child);
1837 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001838 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00001839 synthetic_child_sp->m_is_array_item_for_pointer = true;
1840 }
1841 }
1842 }
1843 return synthetic_child_sp;
1844}
1845
Enrico Granata9fc19442011-07-06 02:13:41 +00001846ValueObjectSP
1847ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1848{
1849 ValueObjectSP synthetic_child_sp;
1850 if (IsScalarType ())
1851 {
1852 char index_str[64];
1853 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1854 ConstString index_const_str(index_str);
1855 // Check if we have already created a synthetic array member in this
1856 // valid object. If we have we will re-use it.
1857 synthetic_child_sp = GetSyntheticChild (index_const_str);
1858 if (!synthetic_child_sp)
1859 {
1860 ValueObjectChild *synthetic_child;
1861 // We haven't made a synthetic array member for INDEX yet, so
1862 // lets make one and cache it for any future reference.
1863 synthetic_child = new ValueObjectChild(*this,
1864 GetClangAST(),
1865 GetClangType(),
1866 index_const_str,
1867 GetByteSize(),
1868 0,
1869 to-from+1,
1870 from,
1871 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001872 false,
1873 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00001874
1875 // Cache the value if we got one back...
1876 if (synthetic_child)
1877 {
1878 AddSyntheticChild(index_const_str, synthetic_child);
1879 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001880 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00001881 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1882 }
1883 }
1884 }
1885 return synthetic_child_sp;
1886}
1887
Greg Claytonafacd142011-09-02 01:15:17 +00001888ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001889ValueObject::GetSyntheticArrayRangeChild (uint32_t from, uint32_t to, bool can_create)
1890{
1891 ValueObjectSP synthetic_child_sp;
1892 if (IsArrayType () || IsPointerType ())
1893 {
1894 char index_str[64];
1895 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1896 ConstString index_const_str(index_str);
1897 // Check if we have already created a synthetic array member in this
1898 // valid object. If we have we will re-use it.
1899 synthetic_child_sp = GetSyntheticChild (index_const_str);
1900 if (!synthetic_child_sp)
1901 {
1902 ValueObjectSynthetic *synthetic_child;
1903
1904 // We haven't made a synthetic array member for INDEX yet, so
1905 // lets make one and cache it for any future reference.
Enrico Granata061858c2012-02-15 02:34:21 +00001906 SyntheticArrayView *view = new SyntheticArrayView(SyntheticChildren::Flags());
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001907 view->AddRange(from,to);
1908 SyntheticChildrenSP view_sp(view);
1909 synthetic_child = new ValueObjectSynthetic(*this, view_sp);
1910
1911 // Cache the value if we got one back...
1912 if (synthetic_child)
1913 {
1914 AddSyntheticChild(index_const_str, synthetic_child);
1915 synthetic_child_sp = synthetic_child->GetSP();
1916 synthetic_child_sp->SetName(ConstString(index_str));
1917 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1918 }
1919 }
1920 }
1921 return synthetic_child_sp;
1922}
1923
Greg Claytonafacd142011-09-02 01:15:17 +00001924ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00001925ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1926{
1927
1928 ValueObjectSP synthetic_child_sp;
1929
1930 char name_str[64];
1931 snprintf(name_str, sizeof(name_str), "@%i", offset);
1932 ConstString name_const_str(name_str);
1933
1934 // Check if we have already created a synthetic array member in this
1935 // valid object. If we have we will re-use it.
1936 synthetic_child_sp = GetSyntheticChild (name_const_str);
1937
1938 if (synthetic_child_sp.get())
1939 return synthetic_child_sp;
1940
1941 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00001942 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001943
1944 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1945 type.GetASTContext(),
1946 type.GetOpaqueQualType(),
1947 name_const_str,
1948 type.GetTypeByteSize(),
1949 offset,
1950 0,
1951 0,
1952 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001953 false,
1954 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001955 if (synthetic_child)
1956 {
1957 AddSyntheticChild(name_const_str, synthetic_child);
1958 synthetic_child_sp = synthetic_child->GetSP();
1959 synthetic_child_sp->SetName(name_const_str);
1960 synthetic_child_sp->m_is_child_at_offset = true;
1961 }
1962 return synthetic_child_sp;
1963}
1964
Enrico Granatad55546b2011-07-22 00:16:08 +00001965// your expression path needs to have a leading . or ->
1966// (unless it somehow "looks like" an array, in which case it has
1967// a leading [ symbol). while the [ is meaningful and should be shown
1968// to the user, . and -> are just parser design, but by no means
1969// added information for the user.. strip them off
1970static const char*
1971SkipLeadingExpressionPathSeparators(const char* expression)
1972{
1973 if (!expression || !expression[0])
1974 return expression;
1975 if (expression[0] == '.')
1976 return expression+1;
1977 if (expression[0] == '-' && expression[1] == '>')
1978 return expression+2;
1979 return expression;
1980}
1981
Greg Claytonafacd142011-09-02 01:15:17 +00001982ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00001983ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1984{
1985 ValueObjectSP synthetic_child_sp;
1986 ConstString name_const_string(expression);
1987 // Check if we have already created a synthetic array member in this
1988 // valid object. If we have we will re-use it.
1989 synthetic_child_sp = GetSyntheticChild (name_const_string);
1990 if (!synthetic_child_sp)
1991 {
1992 // We haven't made a synthetic array member for expression yet, so
1993 // lets make one and cache it for any future reference.
1994 synthetic_child_sp = GetValueForExpressionPath(expression);
1995
1996 // Cache the value if we got one back...
1997 if (synthetic_child_sp.get())
1998 {
1999 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00002000 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002001 synthetic_child_sp->m_is_expression_path_child = true;
2002 }
2003 }
2004 return synthetic_child_sp;
2005}
2006
2007void
Enrico Granata86cc9822012-03-19 22:58:49 +00002008ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002009{
Enrico Granata86cc9822012-03-19 22:58:49 +00002010 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002011 return;
2012
Enrico Granatac5bc4122012-03-27 02:35:13 +00002013 TargetSP target_sp(GetTargetSP());
2014 if (target_sp && (target_sp->GetEnableSyntheticValue() == false || target_sp->GetSuppressSyntheticValue() == true))
2015 {
2016 m_synthetic_value = NULL;
2017 return;
2018 }
2019
Enrico Granata86cc9822012-03-19 22:58:49 +00002020 if (!UpdateFormatsIfNeeded(m_last_format_mgr_dynamic) && m_synthetic_value)
2021 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002022
Enrico Granata0c489f52012-03-01 04:24:26 +00002023 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002024 return;
2025
Enrico Granata86cc9822012-03-19 22:58:49 +00002026 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002027}
2028
Jim Ingham78a685a2011-04-16 00:01:13 +00002029void
Greg Claytonafacd142011-09-02 01:15:17 +00002030ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002031{
Greg Claytonafacd142011-09-02 01:15:17 +00002032 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002033 return;
2034
Jim Ingham58b59f92011-04-22 23:53:53 +00002035 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002036 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002037 ExecutionContext exe_ctx (GetExecutionContextRef());
2038 Process *process = exe_ctx.GetProcessPtr();
2039 if (process)
Jim Ingham78a685a2011-04-16 00:01:13 +00002040 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002041 bool worth_having_dynamic_value = false;
Jim Ingham78a685a2011-04-16 00:01:13 +00002042
Greg Claytoncc4d0142012-02-17 07:49:44 +00002043
2044 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
2045 // hard code this everywhere.
2046 LanguageType known_type = GetObjectRuntimeLanguage();
2047 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
Jim Ingham78a685a2011-04-16 00:01:13 +00002048 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002049 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
2050 if (runtime)
2051 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00002052 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00002053 else
2054 {
2055 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (eLanguageTypeC_plus_plus);
2056 if (cpp_runtime)
2057 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
2058
2059 if (!worth_having_dynamic_value)
2060 {
2061 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (eLanguageTypeObjC);
2062 if (objc_runtime)
2063 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
2064 }
2065 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002066
Greg Claytoncc4d0142012-02-17 07:49:44 +00002067 if (worth_having_dynamic_value)
2068 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
2069 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002070 }
2071}
2072
Jim Ingham58b59f92011-04-22 23:53:53 +00002073ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002074ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002075{
Greg Claytonafacd142011-09-02 01:15:17 +00002076 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002077 return ValueObjectSP();
2078
2079 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002080 {
Jim Ingham2837b762011-05-04 03:43:18 +00002081 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002082 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002083 if (m_dynamic_value)
2084 return m_dynamic_value->GetSP();
2085 else
2086 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002087}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002088
Jim Ingham60dbabb2011-12-08 19:44:08 +00002089ValueObjectSP
2090ValueObject::GetStaticValue()
2091{
2092 return GetSP();
2093}
2094
Enrico Granatad55546b2011-07-22 00:16:08 +00002095ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002096ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002097{
Enrico Granata86cc9822012-03-19 22:58:49 +00002098 if (use_synthetic == false)
2099 return ValueObjectSP();
2100
Enrico Granatad55546b2011-07-22 00:16:08 +00002101 CalculateSyntheticValue(use_synthetic);
2102
2103 if (m_synthetic_value)
2104 return m_synthetic_value->GetSP();
2105 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002106 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002107}
2108
Greg Claytone221f822011-01-21 01:59:00 +00002109bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002110ValueObject::HasSyntheticValue()
2111{
2112 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
2113
Enrico Granata0c489f52012-03-01 04:24:26 +00002114 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002115 return false;
2116
Enrico Granata86cc9822012-03-19 22:58:49 +00002117 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002118
2119 if (m_synthetic_value)
2120 return true;
2121 else
2122 return false;
2123}
2124
2125bool
Greg Claytone221f822011-01-21 01:59:00 +00002126ValueObject::GetBaseClassPath (Stream &s)
2127{
2128 if (IsBaseClass())
2129 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002130 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00002131 clang_type_t clang_type = GetClangType();
2132 std::string cxx_class_name;
2133 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
2134 if (this_had_base_class)
2135 {
2136 if (parent_had_base_class)
2137 s.PutCString("::");
2138 s.PutCString(cxx_class_name.c_str());
2139 }
2140 return parent_had_base_class || this_had_base_class;
2141 }
2142 return false;
2143}
2144
2145
2146ValueObject *
2147ValueObject::GetNonBaseClassParent()
2148{
Jim Ingham78a685a2011-04-16 00:01:13 +00002149 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002150 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002151 if (GetParent()->IsBaseClass())
2152 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002153 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002154 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002155 }
2156 return NULL;
2157}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002158
2159void
Enrico Granata4becb372011-06-29 22:27:15 +00002160ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002161{
Greg Claytone221f822011-01-21 01:59:00 +00002162 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002163
Enrico Granata86cc9822012-03-19 22:58:49 +00002164 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002165 {
Enrico Granata4becb372011-06-29 22:27:15 +00002166 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2167 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2168 // the eHonorPointers mode is meant to produce strings in this latter format
2169 s.PutCString("*(");
2170 }
Greg Claytone221f822011-01-21 01:59:00 +00002171
Enrico Granata4becb372011-06-29 22:27:15 +00002172 ValueObject* parent = GetParent();
2173
2174 if (parent)
2175 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002176
2177 // if we are a deref_of_parent just because we are synthetic array
2178 // members made up to allow ptr[%d] syntax to work in variable
2179 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002180 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002181 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002182
Greg Claytone221f822011-01-21 01:59:00 +00002183 if (!IsBaseClass())
2184 {
2185 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002186 {
Greg Claytone221f822011-01-21 01:59:00 +00002187 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2188 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002189 {
Greg Claytone221f822011-01-21 01:59:00 +00002190 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2191 if (non_base_class_parent_clang_type)
2192 {
2193 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
2194
Enrico Granata86cc9822012-03-19 22:58:49 +00002195 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002196 {
2197 s.PutCString("->");
2198 }
Enrico Granata4becb372011-06-29 22:27:15 +00002199 else
2200 {
2201 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
2202 {
2203 s.PutCString("->");
2204 }
2205 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
2206 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
2207 {
2208 s.PutChar('.');
2209 }
Greg Claytone221f822011-01-21 01:59:00 +00002210 }
2211 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002212 }
Greg Claytone221f822011-01-21 01:59:00 +00002213
2214 const char *name = GetName().GetCString();
2215 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002216 {
Greg Claytone221f822011-01-21 01:59:00 +00002217 if (qualify_cxx_base_classes)
2218 {
2219 if (GetBaseClassPath (s))
2220 s.PutCString("::");
2221 }
2222 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002223 }
2224 }
2225 }
2226
Enrico Granata86cc9822012-03-19 22:58:49 +00002227 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002228 {
Greg Claytone221f822011-01-21 01:59:00 +00002229 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002230 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002231}
2232
Greg Claytonafacd142011-09-02 01:15:17 +00002233ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002234ValueObject::GetValueForExpressionPath(const char* expression,
2235 const char** first_unparsed,
2236 ExpressionPathScanEndReason* reason_to_stop,
2237 ExpressionPathEndResultType* final_value_type,
2238 const GetValueForExpressionPathOptions& options,
2239 ExpressionPathAftermath* final_task_on_target)
2240{
2241
2242 const char* dummy_first_unparsed;
2243 ExpressionPathScanEndReason dummy_reason_to_stop;
2244 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002245 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002246
2247 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2248 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2249 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2250 final_value_type ? final_value_type : &dummy_final_value_type,
2251 options,
2252 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2253
Enrico Granata86cc9822012-03-19 22:58:49 +00002254 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002255 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002256
Enrico Granata86cc9822012-03-19 22:58:49 +00002257 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 +00002258 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002259 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002260 {
2261 Error error;
2262 ValueObjectSP final_value = ret_val->Dereference(error);
2263 if (error.Fail() || !final_value.get())
2264 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002265 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002266 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002267 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002268 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002269 return ValueObjectSP();
2270 }
2271 else
2272 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002273 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002274 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002275 return final_value;
2276 }
2277 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002278 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002279 {
2280 Error error;
2281 ValueObjectSP final_value = ret_val->AddressOf(error);
2282 if (error.Fail() || !final_value.get())
2283 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002284 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002285 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002286 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002287 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002288 return ValueObjectSP();
2289 }
2290 else
2291 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002292 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002293 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002294 return final_value;
2295 }
2296 }
2297 }
2298 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2299}
2300
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002301int
2302ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002303 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002304 const char** first_unparsed,
2305 ExpressionPathScanEndReason* reason_to_stop,
2306 ExpressionPathEndResultType* final_value_type,
2307 const GetValueForExpressionPathOptions& options,
2308 ExpressionPathAftermath* final_task_on_target)
2309{
2310 const char* dummy_first_unparsed;
2311 ExpressionPathScanEndReason dummy_reason_to_stop;
2312 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002313 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002314
2315 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2316 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2317 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2318 final_value_type ? final_value_type : &dummy_final_value_type,
2319 options,
2320 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2321
2322 if (!ret_val.get()) // if there are errors, I add nothing to the list
2323 return 0;
2324
Enrico Granata86ea8d82012-03-29 01:34:34 +00002325 if ( (reason_to_stop ? *reason_to_stop : dummy_reason_to_stop) != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002326 {
2327 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002328 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002329 {
2330 list->Append(ret_val);
2331 return 1;
2332 }
Enrico Granata86ea8d82012-03-29 01:34:34 +00002333 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 +00002334 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002335 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002336 {
2337 Error error;
2338 ValueObjectSP final_value = ret_val->Dereference(error);
2339 if (error.Fail() || !final_value.get())
2340 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002341 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2342 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002343 return 0;
2344 }
2345 else
2346 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002347 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002348 list->Append(final_value);
2349 return 1;
2350 }
2351 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002352 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002353 {
2354 Error error;
2355 ValueObjectSP final_value = ret_val->AddressOf(error);
2356 if (error.Fail() || !final_value.get())
2357 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002358 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2359 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002360 return 0;
2361 }
2362 else
2363 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002364 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002365 list->Append(final_value);
2366 return 1;
2367 }
2368 }
2369 }
2370 }
2371 else
2372 {
2373 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2374 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2375 ret_val,
2376 list,
2377 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2378 final_value_type ? final_value_type : &dummy_final_value_type,
2379 options,
2380 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2381 }
2382 // in any non-covered case, just do the obviously right thing
2383 list->Append(ret_val);
2384 return 1;
2385}
2386
Greg Claytonafacd142011-09-02 01:15:17 +00002387ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002388ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2389 const char** first_unparsed,
2390 ExpressionPathScanEndReason* reason_to_stop,
2391 ExpressionPathEndResultType* final_result,
2392 const GetValueForExpressionPathOptions& options,
2393 ExpressionPathAftermath* what_next)
2394{
2395 ValueObjectSP root = GetSP();
2396
2397 if (!root.get())
2398 return ValueObjectSP();
2399
2400 *first_unparsed = expression_cstr;
2401
2402 while (true)
2403 {
2404
2405 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2406
Greg Claytonafacd142011-09-02 01:15:17 +00002407 clang_type_t root_clang_type = root->GetClangType();
2408 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002409 Flags root_clang_type_info,pointee_clang_type_info;
2410
2411 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2412 if (pointee_clang_type)
2413 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002414
2415 if (!expression_cstr || *expression_cstr == '\0')
2416 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002417 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002418 return root;
2419 }
2420
2421 switch (*expression_cstr)
2422 {
2423 case '-':
2424 {
2425 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002426 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 +00002427 {
2428 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002429 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2430 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002431 return ValueObjectSP();
2432 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002433 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2434 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002435 options.m_no_fragile_ivar)
2436 {
2437 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002438 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2439 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002440 return ValueObjectSP();
2441 }
2442 if (expression_cstr[1] != '>')
2443 {
2444 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002445 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2446 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002447 return ValueObjectSP();
2448 }
2449 expression_cstr++; // skip the -
2450 }
2451 case '.': // or fallthrough from ->
2452 {
2453 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002454 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 +00002455 {
2456 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002457 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2458 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002459 return ValueObjectSP();
2460 }
2461 expression_cstr++; // skip .
2462 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2463 ConstString child_name;
2464 if (!next_separator) // if no other separator just expand this last layer
2465 {
2466 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002467 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2468
2469 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002470 {
2471 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002472 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2473 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002474 return child_valobj_sp;
2475 }
2476 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2477 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002478 if (root->IsSynthetic())
2479 child_valobj_sp = root;
2480 else
2481 child_valobj_sp = root->GetSyntheticValue();
2482
2483 if (child_valobj_sp.get())
2484 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002485 }
2486
2487 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2488 // so we hit the "else" branch, and return an error
2489 if(child_valobj_sp.get()) // if it worked, just return
2490 {
2491 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002492 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2493 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002494 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002495 }
2496 else
2497 {
2498 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002499 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2500 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002501 return ValueObjectSP();
2502 }
2503 }
2504 else // other layers do expand
2505 {
2506 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002507 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2508 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002509 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002510 root = child_valobj_sp;
2511 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002512 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002513 continue;
2514 }
2515 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2516 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002517 child_valobj_sp = root->GetSyntheticValue(true);
2518 if (child_valobj_sp)
2519 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002520 }
2521
2522 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2523 // so we hit the "else" branch, and return an error
2524 if(child_valobj_sp.get()) // if it worked, move on
2525 {
2526 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002527 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002528 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002529 continue;
2530 }
2531 else
2532 {
2533 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002534 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2535 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002536 return ValueObjectSP();
2537 }
2538 }
2539 break;
2540 }
2541 case '[':
2542 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002543 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 +00002544 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002545 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002546 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002547 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2548 {
2549 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002550 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2551 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002552 return ValueObjectSP();
2553 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002554 }
2555 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2556 {
2557 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002558 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2559 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002560 return ValueObjectSP();
2561 }
2562 }
2563 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2564 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002565 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002566 {
2567 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002568 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2569 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002570 return ValueObjectSP();
2571 }
2572 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2573 {
2574 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002575 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2576 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002577 return root;
2578 }
2579 }
2580 const char *separator_position = ::strchr(expression_cstr+1,'-');
2581 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2582 if (!close_bracket_position) // if there is no ], this is a syntax error
2583 {
2584 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002585 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2586 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002587 return ValueObjectSP();
2588 }
2589 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2590 {
2591 char *end = NULL;
2592 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2593 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2594 {
2595 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002596 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2597 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002598 return ValueObjectSP();
2599 }
2600 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2601 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002602 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002603 {
2604 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002605 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2606 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002607 return root;
2608 }
2609 else
2610 {
2611 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002612 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2613 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002614 return ValueObjectSP();
2615 }
2616 }
2617 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002618 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002619 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002620 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2621 if (!child_valobj_sp)
2622 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002623 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002624 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2625 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002626 if (child_valobj_sp)
2627 {
2628 root = child_valobj_sp;
2629 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002630 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002631 continue;
2632 }
2633 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002634 {
2635 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002636 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2637 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002638 return ValueObjectSP();
2639 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002640 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002641 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002642 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002643 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 +00002644 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002645 {
2646 Error error;
2647 root = root->Dereference(error);
2648 if (error.Fail() || !root.get())
2649 {
2650 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002651 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2652 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002653 return ValueObjectSP();
2654 }
2655 else
2656 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002657 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002658 continue;
2659 }
2660 }
2661 else
2662 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002663 if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
Greg Clayton84db9102012-03-26 23:03:23 +00002664 root->GetClangType()) == eLanguageTypeObjC
2665 && ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2666 && root->HasSyntheticValue()
2667 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002668 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002669 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002670 }
2671 else
2672 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002673 if (!root.get())
2674 {
2675 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002676 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2677 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002678 return ValueObjectSP();
2679 }
2680 else
2681 {
2682 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002683 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002684 continue;
2685 }
2686 }
2687 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002688 else if (ClangASTContext::IsScalarType(root_clang_type))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002689 {
2690 root = root->GetSyntheticBitFieldChild(index, index, true);
2691 if (!root.get())
2692 {
2693 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002694 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2695 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002696 return ValueObjectSP();
2697 }
2698 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2699 {
2700 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002701 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2702 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002703 return root;
2704 }
2705 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002706 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002707 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002708 if (root->HasSyntheticValue())
2709 root = root->GetSyntheticValue();
2710 else if (!root->IsSynthetic())
2711 {
2712 *first_unparsed = expression_cstr;
2713 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2714 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2715 return ValueObjectSP();
2716 }
2717 // if we are here, then root itself is a synthetic VO.. should be good to go
2718
Enrico Granata27b625e2011-08-09 01:04:56 +00002719 if (!root.get())
2720 {
2721 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002722 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2723 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2724 return ValueObjectSP();
2725 }
2726 root = root->GetChildAtIndex(index, true);
2727 if (!root.get())
2728 {
2729 *first_unparsed = expression_cstr;
2730 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2731 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002732 return ValueObjectSP();
2733 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00002734 else
2735 {
2736 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002737 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002738 continue;
2739 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002740 }
2741 else
2742 {
2743 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002744 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2745 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002746 return ValueObjectSP();
2747 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002748 }
2749 else // we have a low and a high index
2750 {
2751 char *end = NULL;
2752 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2753 if (!end || end != separator_position) // if something weird is in our way return an error
2754 {
2755 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002756 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2757 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002758 return ValueObjectSP();
2759 }
2760 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2761 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2762 {
2763 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002764 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2765 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002766 return ValueObjectSP();
2767 }
2768 if (index_lower > index_higher) // swap indices if required
2769 {
2770 unsigned long temp = index_lower;
2771 index_lower = index_higher;
2772 index_higher = temp;
2773 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002774 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002775 {
2776 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2777 if (!root.get())
2778 {
2779 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002780 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2781 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002782 return ValueObjectSP();
2783 }
2784 else
2785 {
2786 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002787 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2788 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002789 return root;
2790 }
2791 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002792 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 +00002793 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002794 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002795 {
2796 Error error;
2797 root = root->Dereference(error);
2798 if (error.Fail() || !root.get())
2799 {
2800 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002801 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2802 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002803 return ValueObjectSP();
2804 }
2805 else
2806 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002807 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002808 continue;
2809 }
2810 }
2811 else
2812 {
2813 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002814 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2815 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002816 return root;
2817 }
2818 }
2819 break;
2820 }
2821 default: // some non-separator is in the way
2822 {
2823 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002824 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2825 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002826 return ValueObjectSP();
2827 break;
2828 }
2829 }
2830 }
2831}
2832
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002833int
2834ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2835 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00002836 ValueObjectSP root,
2837 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002838 ExpressionPathScanEndReason* reason_to_stop,
2839 ExpressionPathEndResultType* final_result,
2840 const GetValueForExpressionPathOptions& options,
2841 ExpressionPathAftermath* what_next)
2842{
2843 if (!root.get())
2844 return 0;
2845
2846 *first_unparsed = expression_cstr;
2847
2848 while (true)
2849 {
2850
2851 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2852
Greg Claytonafacd142011-09-02 01:15:17 +00002853 clang_type_t root_clang_type = root->GetClangType();
2854 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002855 Flags root_clang_type_info,pointee_clang_type_info;
2856
2857 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2858 if (pointee_clang_type)
2859 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2860
2861 if (!expression_cstr || *expression_cstr == '\0')
2862 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002863 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002864 list->Append(root);
2865 return 1;
2866 }
2867
2868 switch (*expression_cstr)
2869 {
2870 case '[':
2871 {
2872 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2873 {
2874 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2875 {
2876 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002877 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2878 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002879 return 0;
2880 }
2881 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2882 {
2883 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002884 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2885 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002886 return 0;
2887 }
2888 }
2889 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2890 {
2891 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2892 {
2893 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002894 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2895 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002896 return 0;
2897 }
2898 else // expand this into list
2899 {
2900 int max_index = root->GetNumChildren() - 1;
2901 for (int index = 0; index < max_index; index++)
2902 {
2903 ValueObjectSP child =
2904 root->GetChildAtIndex(index, true);
2905 list->Append(child);
2906 }
2907 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002908 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2909 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002910 return max_index; // tell me number of items I added to the VOList
2911 }
2912 }
2913 const char *separator_position = ::strchr(expression_cstr+1,'-');
2914 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2915 if (!close_bracket_position) // if there is no ], this is a syntax error
2916 {
2917 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002918 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2919 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002920 return 0;
2921 }
2922 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2923 {
2924 char *end = NULL;
2925 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2926 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2927 {
2928 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002929 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2930 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002931 return 0;
2932 }
2933 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2934 {
2935 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2936 {
2937 int max_index = root->GetNumChildren() - 1;
2938 for (int index = 0; index < max_index; index++)
2939 {
2940 ValueObjectSP child =
2941 root->GetChildAtIndex(index, true);
2942 list->Append(child);
2943 }
2944 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002945 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2946 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002947 return max_index; // tell me number of items I added to the VOList
2948 }
2949 else
2950 {
2951 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002952 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2953 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002954 return 0;
2955 }
2956 }
2957 // from here on we do have a valid index
2958 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2959 {
2960 root = root->GetChildAtIndex(index, true);
2961 if (!root.get())
2962 {
2963 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002964 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2965 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002966 return 0;
2967 }
2968 else
2969 {
2970 list->Append(root);
2971 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002972 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2973 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002974 return 1;
2975 }
2976 }
2977 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2978 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002979 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 +00002980 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2981 {
2982 Error error;
2983 root = root->Dereference(error);
2984 if (error.Fail() || !root.get())
2985 {
2986 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002987 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2988 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002989 return 0;
2990 }
2991 else
2992 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002993 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002994 continue;
2995 }
2996 }
2997 else
2998 {
2999 root = root->GetSyntheticArrayMemberFromPointer(index, true);
3000 if (!root.get())
3001 {
3002 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003003 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3004 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003005 return 0;
3006 }
3007 else
3008 {
3009 list->Append(root);
3010 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003011 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3012 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003013 return 1;
3014 }
3015 }
3016 }
3017 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3018 {
3019 root = root->GetSyntheticBitFieldChild(index, index, true);
3020 if (!root.get())
3021 {
3022 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003023 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3024 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003025 return 0;
3026 }
3027 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3028 {
3029 list->Append(root);
3030 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003031 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3032 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003033 return 1;
3034 }
3035 }
3036 }
3037 else // we have a low and a high index
3038 {
3039 char *end = NULL;
3040 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3041 if (!end || end != separator_position) // if something weird is in our way return an error
3042 {
3043 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003044 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3045 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003046 return 0;
3047 }
3048 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3049 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3050 {
3051 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003052 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3053 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003054 return 0;
3055 }
3056 if (index_lower > index_higher) // swap indices if required
3057 {
3058 unsigned long temp = index_lower;
3059 index_lower = index_higher;
3060 index_higher = temp;
3061 }
3062 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
3063 {
3064 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3065 if (!root.get())
3066 {
3067 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003068 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3069 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003070 return 0;
3071 }
3072 else
3073 {
3074 list->Append(root);
3075 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003076 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3077 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003078 return 1;
3079 }
3080 }
3081 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 +00003082 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003083 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
3084 {
3085 Error error;
3086 root = root->Dereference(error);
3087 if (error.Fail() || !root.get())
3088 {
3089 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003090 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3091 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003092 return 0;
3093 }
3094 else
3095 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003096 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003097 continue;
3098 }
3099 }
3100 else
3101 {
Johnny Chen44805302011-07-19 19:48:13 +00003102 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003103 index <= index_higher; index++)
3104 {
3105 ValueObjectSP child =
3106 root->GetChildAtIndex(index, true);
3107 list->Append(child);
3108 }
3109 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003110 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3111 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003112 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3113 }
3114 }
3115 break;
3116 }
3117 default: // some non-[ separator, or something entirely wrong, is in the way
3118 {
3119 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003120 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3121 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003122 return 0;
3123 break;
3124 }
3125 }
3126 }
3127}
3128
Enrico Granata0c489f52012-03-01 04:24:26 +00003129static void
3130DumpValueObject_Impl (Stream &s,
3131 ValueObject *valobj,
3132 const ValueObject::DumpValueObjectOptions& options,
3133 uint32_t ptr_depth,
3134 uint32_t curr_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003135{
Greg Clayton007d5be2011-05-30 00:49:24 +00003136 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003137 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003138 bool update_success = valobj->UpdateValueIfNeeded (options.m_use_dynamic, true);
Greg Clayton007d5be2011-05-30 00:49:24 +00003139
Enrico Granata0c489f52012-03-01 04:24:26 +00003140 const char *root_valobj_name =
3141 options.m_root_valobj_name.empty() ?
3142 valobj->GetName().AsCString() :
3143 options.m_root_valobj_name.c_str();
3144
3145 if (update_success && options.m_use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00003146 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003147 ValueObject *dynamic_value = valobj->GetDynamicValue(options.m_use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00003148 if (dynamic_value)
3149 valobj = dynamic_value;
3150 }
3151
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003152 clang_type_t clang_type = valobj->GetClangType();
3153
Greg Clayton73b472d2010-10-27 03:32:59 +00003154 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003155 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00003156 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
3157 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003158
Enrico Granata0c489f52012-03-01 04:24:26 +00003159 const bool print_valobj = options.m_flat_output == false || has_value;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003160
3161 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003162 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003163 if (options.m_show_location)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003164 {
Jim Ingham6035b672011-03-31 00:19:25 +00003165 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003166 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003167
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003168 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003169
Greg Clayton7c8a9662010-11-02 01:50:16 +00003170 // Always show the type for the top level items.
Enrico Granata0c489f52012-03-01 04:24:26 +00003171 if (options.m_show_types || (curr_depth == 0 && !options.m_flat_output))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003172 {
Greg Clayton84db9102012-03-26 23:03:23 +00003173 const char* typeName = valobj->GetQualifiedTypeName().AsCString("<invalid type>");
3174 //const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
Enrico Granata9910bc82011-08-03 02:18:51 +00003175 s.Printf("(%s", typeName);
3176 // only show dynamic types if the user really wants to see types
Enrico Granata0c489f52012-03-01 04:24:26 +00003177 if (options.m_show_types && options.m_use_dynamic != eNoDynamicValues &&
Enrico Granata9910bc82011-08-03 02:18:51 +00003178 (/*strstr(typeName, "id") == typeName ||*/
Greg Claytonafacd142011-09-02 01:15:17 +00003179 ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == eLanguageTypeObjC))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003180 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003181 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3182 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatac3e320a2011-08-02 17:27:39 +00003183 if (process == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003184 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003185 else
3186 {
3187 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
3188 if (runtime == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003189 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003190 else
3191 {
3192 ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
3193 if (!runtime->IsValidISA(isa))
Enrico Granata9910bc82011-08-03 02:18:51 +00003194 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003195 else
3196 s.Printf(", dynamic type: %s) ",
3197 runtime->GetActualTypeName(isa).GetCString());
3198 }
3199 }
3200 }
3201 else
3202 s.Printf(") ");
3203 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003204
Greg Clayton1d3afba2010-10-05 00:00:42 +00003205
Enrico Granata0c489f52012-03-01 04:24:26 +00003206 if (options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003207 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003208 // If we are showing types, also qualify the C++ base classes
Enrico Granata0c489f52012-03-01 04:24:26 +00003209 const bool qualify_cxx_base_classes = options.m_show_types;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003210 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003211 s.PutCString(" =");
3212 }
3213 else
3214 {
3215 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
3216 s.Printf ("%s =", name_cstr);
3217 }
3218
Enrico Granata0c489f52012-03-01 04:24:26 +00003219 if (!options.m_scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003220 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003221 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003222 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003223 }
3224
Enrico Granata0c489f52012-03-01 04:24:26 +00003225 std::string summary_str;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003226 std::string value_str;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003227 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00003228 const char *sum_cstr = NULL;
Enrico Granata0c489f52012-03-01 04:24:26 +00003229 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003230
Enrico Granata0c489f52012-03-01 04:24:26 +00003231 if (options.m_omit_summary_depth > 0)
Enrico Granata0c5ef692011-07-16 01:22:04 +00003232 entry = NULL;
3233
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003234 if (err_cstr == NULL)
3235 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003236 if (options.m_format != eFormatDefault && options.m_format != valobj->GetFormat())
Greg Clayton6efba4f2012-01-26 21:08:30 +00003237 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003238 valobj->GetValueAsCString(options.m_format,
3239 value_str);
Greg Clayton6efba4f2012-01-26 21:08:30 +00003240 }
Enrico Granata0c489f52012-03-01 04:24:26 +00003241 else
Greg Clayton6efba4f2012-01-26 21:08:30 +00003242 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003243 val_cstr = valobj->GetValueAsCString();
3244 if (val_cstr)
3245 value_str = val_cstr;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003246 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003247 err_cstr = valobj->GetError().AsCString();
3248 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003249
3250 if (err_cstr)
3251 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003252 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00003253 }
3254 else
3255 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003256 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003257 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003258 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003259 if (options.m_omit_summary_depth == 0)
3260 {
3261 if (options.m_summary_sp)
3262 {
3263 valobj->GetSummaryAsCString(entry, summary_str);
3264 sum_cstr = summary_str.c_str();
3265 }
3266 else
3267 sum_cstr = valobj->GetSummaryAsCString();
3268 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003269
Greg Clayton6efba4f2012-01-26 21:08:30 +00003270 // Make sure we have a value and make sure the summary didn't
3271 // specify that the value should not be printed
3272 if (!value_str.empty() && (entry == NULL || entry->DoesPrintValue() || sum_cstr == NULL))
3273 s.Printf(" %s", value_str.c_str());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003274
Enrico Granata9dd75c82011-07-15 23:30:15 +00003275 if (sum_cstr)
Enrico Granata0c489f52012-03-01 04:24:26 +00003276 s.Printf(" %s", sum_cstr);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003277
Enrico Granata0c489f52012-03-01 04:24:26 +00003278 if (options.m_use_objc)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003279 {
Jim Ingham6035b672011-03-31 00:19:25 +00003280 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003281 if (object_desc)
3282 s.Printf(" %s\n", object_desc);
3283 else
Sean Callanan672ad942010-10-23 00:18:49 +00003284 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003285 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00003286 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003287 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003288
Enrico Granata0c489f52012-03-01 04:24:26 +00003289 if (curr_depth < options.m_max_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003290 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003291 // We will show children for all concrete types. We won't show
3292 // pointer contents unless a pointer depth has been specified.
3293 // We won't reference contents unless the reference is the
3294 // root object (depth of zero).
3295 bool print_children = true;
3296
3297 // Use a new temporary pointer depth in case we override the
3298 // current pointer depth below...
3299 uint32_t curr_ptr_depth = ptr_depth;
3300
3301 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
3302 if (is_ptr || is_ref)
3303 {
3304 // We have a pointer or reference whose value is an address.
3305 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00003306 AddressType ptr_address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003307 if (valobj->GetPointerValue (&ptr_address_type) == 0)
Greg Clayton73b472d2010-10-27 03:32:59 +00003308 print_children = false;
3309
3310 else if (is_ref && curr_depth == 0)
3311 {
3312 // If this is the root object (depth is zero) that we are showing
3313 // and it is a reference, and no pointer depth has been supplied
3314 // print out what it references. Don't do this at deeper depths
3315 // otherwise we can end up with infinite recursion...
3316 curr_ptr_depth = 1;
3317 }
3318
3319 if (curr_ptr_depth == 0)
3320 print_children = false;
3321 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003322
Enrico Granata0a3958e2011-07-02 00:25:22 +00003323 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00003324 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003325 ValueObject* synth_valobj;
3326 ValueObjectSP synth_valobj_sp = valobj->GetSyntheticValue (options.m_use_synthetic);
3327 synth_valobj = (synth_valobj_sp ? synth_valobj_sp.get() : valobj);
Enrico Granatac5bc4122012-03-27 02:35:13 +00003328
Enrico Granatac482a192011-08-17 22:13:59 +00003329 uint32_t num_children = synth_valobj->GetNumChildren();
Enrico Granata22c55d12011-08-12 02:00:06 +00003330 bool print_dotdotdot = false;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003331 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003332 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003333 if (options.m_flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003334 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003335 if (print_valobj)
3336 s.EOL();
3337 }
3338 else
3339 {
3340 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00003341 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003342 s.IndentMore();
3343 }
Enrico Granata22c55d12011-08-12 02:00:06 +00003344
Greg Claytoncc4d0142012-02-17 07:49:44 +00003345 uint32_t max_num_children = valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00003346
Enrico Granata0c489f52012-03-01 04:24:26 +00003347 if (num_children > max_num_children && !options.m_ignore_cap)
Enrico Granata22c55d12011-08-12 02:00:06 +00003348 {
3349 num_children = max_num_children;
3350 print_dotdotdot = true;
3351 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003352
Enrico Granata0c489f52012-03-01 04:24:26 +00003353 ValueObject::DumpValueObjectOptions child_options(options);
3354 child_options.SetFormat().SetSummary().SetRootValueObjectName();
3355 child_options.SetScopeChecked(true)
3356 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003357 for (uint32_t idx=0; idx<num_children; ++idx)
3358 {
Enrico Granatac482a192011-08-17 22:13:59 +00003359 ValueObjectSP child_sp(synth_valobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003360 if (child_sp.get())
3361 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003362 DumpValueObject_Impl (s,
3363 child_sp.get(),
3364 child_options,
3365 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
3366 curr_depth + 1);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003367 }
3368 }
3369
Enrico Granata0c489f52012-03-01 04:24:26 +00003370 if (!options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003371 {
Enrico Granata22c55d12011-08-12 02:00:06 +00003372 if (print_dotdotdot)
Enrico Granata61a80ba2011-08-12 16:42:31 +00003373 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003374 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3375 Target *target = exe_ctx.GetTargetPtr();
3376 if (target)
3377 target->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
Enrico Granata22c55d12011-08-12 02:00:06 +00003378 s.Indent("...\n");
Enrico Granata61a80ba2011-08-12 16:42:31 +00003379 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003380 s.IndentLess();
3381 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003382 }
3383 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003384 else if (has_children)
3385 {
3386 // Aggregate, no children...
3387 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00003388 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003389 }
3390 else
3391 {
3392 if (print_valobj)
3393 s.EOL();
3394 }
3395
Greg Clayton1d3afba2010-10-05 00:00:42 +00003396 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003397 else
3398 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00003399 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003400 }
3401 }
3402 else
3403 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003404 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003405 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003406 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003407 }
3408 }
3409 }
3410 }
3411}
3412
Enrico Granata0c489f52012-03-01 04:24:26 +00003413void
Greg Claytonf830dbb2012-03-22 18:15:37 +00003414ValueObject::LogValueObject (Log *log,
3415 ValueObject *valobj)
3416{
3417 if (log && valobj)
3418 return LogValueObject (log, valobj, DumpValueObjectOptions::DefaultOptions());
3419}
3420
3421void
3422ValueObject::LogValueObject (Log *log,
3423 ValueObject *valobj,
3424 const DumpValueObjectOptions& options)
3425{
3426 if (log && valobj)
3427 {
3428 StreamString s;
3429 ValueObject::DumpValueObject (s, valobj, options);
3430 if (s.GetSize())
3431 log->PutCString(s.GetData());
3432 }
3433}
3434
3435void
Enrico Granata0c489f52012-03-01 04:24:26 +00003436ValueObject::DumpValueObject (Stream &s,
3437 ValueObject *valobj)
3438{
3439
3440 if (!valobj)
3441 return;
3442
3443 DumpValueObject_Impl(s,
3444 valobj,
3445 DumpValueObjectOptions::DefaultOptions(),
3446 0,
3447 0);
3448}
3449
3450void
3451ValueObject::DumpValueObject (Stream &s,
3452 ValueObject *valobj,
3453 const DumpValueObjectOptions& options)
3454{
3455 DumpValueObject_Impl(s,
3456 valobj,
3457 options,
3458 options.m_max_ptr_depth, // max pointer depth allowed, we will go down from here
3459 0 // current object depth is 0 since we are just starting
3460 );
3461}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003462
3463ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003464ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003465{
3466 ValueObjectSP valobj_sp;
3467
Enrico Granatac3e320a2011-08-02 17:27:39 +00003468 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003469 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003470 ExecutionContext exe_ctx (GetExecutionContextRef());
3471 clang::ASTContext *ast = GetClangAST ();
3472
3473 DataExtractor data;
3474 data.SetByteOrder (m_data.GetByteOrder());
3475 data.SetAddressByteSize(m_data.GetAddressByteSize());
3476
Greg Claytone72dfb32012-02-24 01:59:29 +00003477 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003478
3479 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3480 ast,
3481 GetClangType(),
3482 name,
3483 data,
3484 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003485 }
Jim Ingham6035b672011-03-31 00:19:25 +00003486
3487 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003488 {
Jim Ingham58b59f92011-04-22 23:53:53 +00003489 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003490 }
3491 return valobj_sp;
3492}
3493
Greg Claytonafacd142011-09-02 01:15:17 +00003494ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003495ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003496{
Jim Ingham58b59f92011-04-22 23:53:53 +00003497 if (m_deref_valobj)
3498 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003499
Greg Clayton54979cd2010-12-15 05:08:08 +00003500 const bool is_pointer_type = IsPointerType();
3501 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003502 {
3503 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003504 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003505
3506 std::string child_name_str;
3507 uint32_t child_byte_size = 0;
3508 int32_t child_byte_offset = 0;
3509 uint32_t child_bitfield_bit_size = 0;
3510 uint32_t child_bitfield_bit_offset = 0;
3511 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003512 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003513 const bool transparent_pointers = false;
3514 clang::ASTContext *clang_ast = GetClangAST();
3515 clang_type_t clang_type = GetClangType();
3516 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003517
Greg Claytoncc4d0142012-02-17 07:49:44 +00003518 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003519
3520 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
3521 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003522 GetName().GetCString(),
3523 clang_type,
3524 0,
3525 transparent_pointers,
3526 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003527 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003528 child_name_str,
3529 child_byte_size,
3530 child_byte_offset,
3531 child_bitfield_bit_size,
3532 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003533 child_is_base_class,
3534 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003535 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003536 {
3537 ConstString child_name;
3538 if (!child_name_str.empty())
3539 child_name.SetCString (child_name_str.c_str());
3540
Jim Ingham58b59f92011-04-22 23:53:53 +00003541 m_deref_valobj = new ValueObjectChild (*this,
3542 clang_ast,
3543 child_clang_type,
3544 child_name,
3545 child_byte_size,
3546 child_byte_offset,
3547 child_bitfield_bit_size,
3548 child_bitfield_bit_offset,
3549 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003550 child_is_deref_of_parent,
3551 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003552 }
3553 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003554
Jim Ingham58b59f92011-04-22 23:53:53 +00003555 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003556 {
3557 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003558 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003559 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003560 else
3561 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003562 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003563 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003564
3565 if (is_pointer_type)
3566 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3567 else
3568 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003569 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003570 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003571}
3572
Greg Claytonafacd142011-09-02 01:15:17 +00003573ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003574ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003575{
Jim Ingham78a685a2011-04-16 00:01:13 +00003576 if (m_addr_of_valobj_sp)
3577 return m_addr_of_valobj_sp;
3578
Greg Claytone0d378b2011-03-24 21:19:54 +00003579 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003580 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003581 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003582 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003583 if (addr != LLDB_INVALID_ADDRESS)
3584 {
3585 switch (address_type)
3586 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003587 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003588 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003589 {
3590 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003591 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003592 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3593 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003594 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003595
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003596 case eAddressTypeFile:
3597 case eAddressTypeLoad:
3598 case eAddressTypeHost:
3599 {
3600 clang::ASTContext *ast = GetClangAST();
3601 clang_type_t clang_type = GetClangType();
3602 if (ast && clang_type)
3603 {
3604 std::string name (1, '&');
3605 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003606 ExecutionContext exe_ctx (GetExecutionContextRef());
3607 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003608 ast,
3609 ClangASTContext::CreatePointerType (ast, clang_type),
3610 ConstString (name.c_str()),
3611 addr,
3612 eAddressTypeInvalid,
3613 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003614 }
3615 }
3616 break;
3617 }
3618 }
Jim Ingham78a685a2011-04-16 00:01:13 +00003619 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003620}
3621
Greg Clayton9a142cf2012-02-03 05:34:10 +00003622ValueObjectSP
3623ValueObject::Cast (const ClangASTType &clang_ast_type)
3624{
Greg Clayton81e871e2012-02-04 02:27:34 +00003625 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003626}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003627
Greg Claytonafacd142011-09-02 01:15:17 +00003628ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003629ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3630{
Greg Claytonafacd142011-09-02 01:15:17 +00003631 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003632 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003633 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003634
3635 if (ptr_value != LLDB_INVALID_ADDRESS)
3636 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003637 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003638 ExecutionContext exe_ctx (GetExecutionContextRef());
3639 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003640 name,
3641 ptr_addr,
3642 clang_ast_type);
3643 }
3644 return valobj_sp;
3645}
3646
Greg Claytonafacd142011-09-02 01:15:17 +00003647ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003648ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3649{
Greg Claytonafacd142011-09-02 01:15:17 +00003650 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003651 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003652 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003653
3654 if (ptr_value != LLDB_INVALID_ADDRESS)
3655 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003656 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003657 ExecutionContext exe_ctx (GetExecutionContextRef());
3658 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003659 name,
3660 ptr_addr,
3661 type_sp);
3662 }
3663 return valobj_sp;
3664}
3665
Jim Ingham6035b672011-03-31 00:19:25 +00003666ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003667 m_mod_id(),
3668 m_exe_ctx_ref(),
3669 m_needs_update (true),
3670 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003671{
3672}
3673
3674ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003675 m_mod_id(),
3676 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003677 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003678 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003679{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003680 ExecutionContext exe_ctx(exe_scope);
3681 TargetSP target_sp (exe_ctx.GetTargetSP());
3682 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003683 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003684 m_exe_ctx_ref.SetTargetSP (target_sp);
3685 ProcessSP process_sp (exe_ctx.GetProcessSP());
3686 if (!process_sp)
3687 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003688
Greg Claytoncc4d0142012-02-17 07:49:44 +00003689 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003690 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003691 m_mod_id = process_sp->GetModID();
3692 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003693
Greg Claytoncc4d0142012-02-17 07:49:44 +00003694 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003695
Greg Claytoncc4d0142012-02-17 07:49:44 +00003696 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003697 {
3698 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003699 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003700 }
Jim Ingham6035b672011-03-31 00:19:25 +00003701
Greg Claytoncc4d0142012-02-17 07:49:44 +00003702 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003703 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003704 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003705
Greg Claytoncc4d0142012-02-17 07:49:44 +00003706 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3707 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003708 {
3709 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003710 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003711 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003712 if (frame_sp)
3713 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003714 }
3715 }
3716 }
Jim Ingham6035b672011-03-31 00:19:25 +00003717}
3718
3719ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003720 m_mod_id(),
3721 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3722 m_needs_update (true),
3723 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003724{
3725}
3726
3727ValueObject::EvaluationPoint::~EvaluationPoint ()
3728{
3729}
3730
Jim Ingham6035b672011-03-31 00:19:25 +00003731// This function checks the EvaluationPoint against the current process state. If the current
3732// state matches the evaluation point, or the evaluation point is already invalid, then we return
3733// false, meaning "no change". If the current state is different, we update our state, and return
3734// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3735// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003736// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003737
3738bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003739ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003740{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003741
3742 // 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 +00003743 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock());
Jim Ingham73ca05a2011-12-17 01:35:57 +00003744
Greg Claytoncc4d0142012-02-17 07:49:44 +00003745 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003746 return false;
3747
Jim Ingham6035b672011-03-31 00:19:25 +00003748 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003749 Process *process = exe_ctx.GetProcessPtr();
3750 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003751 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003752
Jim Ingham6035b672011-03-31 00:19:25 +00003753 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003754 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003755
Jim Ingham78a685a2011-04-16 00:01:13 +00003756 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3757 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003758 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003759 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003760
3761 bool changed;
3762
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003763 if (m_mod_id.IsValid())
3764 {
3765 if (m_mod_id == current_mod_id)
3766 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003767 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003768 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003769 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003770 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003771 else
3772 {
3773 m_mod_id = current_mod_id;
3774 m_needs_update = true;
3775 changed = true;
3776 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003777 }
Jim Ingham6035b672011-03-31 00:19:25 +00003778
Jim Ingham73ca05a2011-12-17 01:35:57 +00003779 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3780 // That way we'll be sure to return a valid exe_scope.
3781 // 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 +00003782
Greg Claytoncc4d0142012-02-17 07:49:44 +00003783 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003784 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003785 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3786 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003787 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003788 if (m_exe_ctx_ref.HasFrameRef())
3789 {
3790 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
3791 if (!frame_sp)
3792 {
3793 // We used to have a frame, but now it is gone
3794 SetInvalid();
3795 }
3796 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003797 }
Jim Ingham6035b672011-03-31 00:19:25 +00003798 else
3799 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003800 // We used to have a thread, but now it is gone
3801 SetInvalid();
Jim Ingham6035b672011-03-31 00:19:25 +00003802 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003803
Jim Ingham6035b672011-03-31 00:19:25 +00003804 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003805 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003806}
3807
Jim Ingham61be0902011-05-02 18:13:59 +00003808void
3809ValueObject::EvaluationPoint::SetUpdated ()
3810{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003811 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3812 if (process_sp)
3813 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003814 m_first_update = false;
3815 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003816}
3817
3818
Greg Claytoncc4d0142012-02-17 07:49:44 +00003819//bool
3820//ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3821//{
3822// if (!IsValid())
3823// return false;
3824//
3825// bool needs_update = false;
3826//
3827// // The target has to be non-null, and the
3828// Target *target = exe_scope->CalculateTarget();
3829// if (target != NULL)
3830// {
3831// Target *old_target = m_target_sp.get();
3832// assert (target == old_target);
3833// Process *process = exe_scope->CalculateProcess();
3834// if (process != NULL)
3835// {
3836// // FOR NOW - assume you can't update variable objects across process boundaries.
3837// Process *old_process = m_process_sp.get();
3838// assert (process == old_process);
3839// ProcessModID current_mod_id = process->GetModID();
3840// if (m_mod_id != current_mod_id)
3841// {
3842// needs_update = true;
3843// m_mod_id = current_mod_id;
3844// }
3845// // See if we're switching the thread or stack context. If no thread is given, this is
3846// // being evaluated in a global context.
3847// Thread *thread = exe_scope->CalculateThread();
3848// if (thread != NULL)
3849// {
3850// user_id_t new_thread_index = thread->GetIndexID();
3851// if (new_thread_index != m_thread_id)
3852// {
3853// needs_update = true;
3854// m_thread_id = new_thread_index;
3855// m_stack_id.Clear();
3856// }
3857//
3858// StackFrame *new_frame = exe_scope->CalculateStackFrame();
3859// if (new_frame != NULL)
3860// {
3861// if (new_frame->GetStackID() != m_stack_id)
3862// {
3863// needs_update = true;
3864// m_stack_id = new_frame->GetStackID();
3865// }
3866// }
3867// else
3868// {
3869// m_stack_id.Clear();
3870// needs_update = true;
3871// }
3872// }
3873// else
3874// {
3875// // If this had been given a thread, and now there is none, we should update.
3876// // Otherwise we don't have to do anything.
3877// if (m_thread_id != LLDB_INVALID_UID)
3878// {
3879// m_thread_id = LLDB_INVALID_UID;
3880// m_stack_id.Clear();
3881// needs_update = true;
3882// }
3883// }
3884// }
3885// else
3886// {
3887// // If there is no process, then we don't need to update anything.
3888// // But if we're switching from having a process to not, we should try to update.
3889// if (m_process_sp.get() != NULL)
3890// {
3891// needs_update = true;
3892// m_process_sp.reset();
3893// m_thread_id = LLDB_INVALID_UID;
3894// m_stack_id.Clear();
3895// }
3896// }
3897// }
3898// else
3899// {
3900// // If there's no target, nothing can change so we don't need to update anything.
3901// // But if we're switching from having a target to not, we should try to update.
3902// if (m_target_sp.get() != NULL)
3903// {
3904// needs_update = true;
3905// m_target_sp.reset();
3906// m_process_sp.reset();
3907// m_thread_id = LLDB_INVALID_UID;
3908// m_stack_id.Clear();
3909// }
3910// }
3911// if (!m_needs_update)
3912// m_needs_update = needs_update;
3913//
3914// return needs_update;
3915//}
Enrico Granataf2bbf712011-07-15 02:26:42 +00003916
3917void
Enrico Granata86cc9822012-03-19 22:58:49 +00003918ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003919{
Enrico Granata86cc9822012-03-19 22:58:49 +00003920 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3921 m_value_str.clear();
3922
3923 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3924 m_location_str.clear();
3925
3926 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
3927 {
3928 m_is_getting_summary = false;
3929 m_summary_str.clear();
3930 }
3931
3932 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3933 m_object_desc_str.clear();
3934
3935 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3936 {
3937 if (m_synthetic_value)
3938 m_synthetic_value = NULL;
3939 }
Johnny Chen44805302011-07-19 19:48:13 +00003940}
Enrico Granata9128ee22011-09-06 19:20:51 +00003941
3942SymbolContextScope *
3943ValueObject::GetSymbolContextScope()
3944{
3945 if (m_parent)
3946 {
3947 if (!m_parent->IsPointerOrReferenceType())
3948 return m_parent->GetSymbolContextScope();
3949 }
3950 return NULL;
3951}