blob: e072b491652362305fedbccc68e948a9930d8bdc [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{
Greg Clayton288bdf92010-09-02 02:59:18 +0000575 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 {
577 SetNumChildren (CalculateNumChildren());
578 }
Enrico Granata9d60f602012-03-09 03:09:58 +0000579 return m_children.GetChildrenCount();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000580}
581void
582ValueObject::SetNumChildren (uint32_t num_children)
583{
Greg Clayton288bdf92010-09-02 02:59:18 +0000584 m_children_count_valid = true;
Enrico Granata9d60f602012-03-09 03:09:58 +0000585 m_children.SetChildrenCount(num_children);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000586}
587
588void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000589ValueObject::SetName (const ConstString &name)
590{
591 m_name = name;
592}
593
Jim Ingham58b59f92011-04-22 23:53:53 +0000594ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000595ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
596{
Jim Ingham2eec4872011-05-07 00:10:58 +0000597 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000598
Greg Claytondea8cb42011-06-29 22:09:02 +0000599 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000600 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000601 std::string child_name_str;
602 uint32_t child_byte_size = 0;
603 int32_t child_byte_offset = 0;
604 uint32_t child_bitfield_bit_size = 0;
605 uint32_t child_bitfield_bit_offset = 0;
606 bool child_is_base_class = false;
607 bool child_is_deref_of_parent = false;
608
609 const bool transparent_pointers = synthetic_array_member == false;
610 clang::ASTContext *clang_ast = GetClangAST();
611 clang_type_t clang_type = GetClangType();
612 clang_type_t child_clang_type;
613
Greg Claytoncc4d0142012-02-17 07:49:44 +0000614 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytondea8cb42011-06-29 22:09:02 +0000615
616 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
617 clang_ast,
618 GetName().GetCString(),
619 clang_type,
620 idx,
621 transparent_pointers,
622 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +0000623 ignore_array_bounds,
Greg Claytondea8cb42011-06-29 22:09:02 +0000624 child_name_str,
625 child_byte_size,
626 child_byte_offset,
627 child_bitfield_bit_size,
628 child_bitfield_bit_offset,
629 child_is_base_class,
630 child_is_deref_of_parent);
631 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000633 if (synthetic_index)
634 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635
Greg Claytondea8cb42011-06-29 22:09:02 +0000636 ConstString child_name;
637 if (!child_name_str.empty())
638 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000639
Greg Claytondea8cb42011-06-29 22:09:02 +0000640 valobj = new ValueObjectChild (*this,
641 clang_ast,
642 child_clang_type,
643 child_name,
644 child_byte_size,
645 child_byte_offset,
646 child_bitfield_bit_size,
647 child_bitfield_bit_offset,
648 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +0000649 child_is_deref_of_parent,
650 eAddressTypeInvalid);
651 //if (valobj)
652 // valobj->SetAddressTypeOfChildren(eAddressTypeInvalid);
653 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000654
Jim Ingham58b59f92011-04-22 23:53:53 +0000655 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000656}
657
Enrico Granata0c489f52012-03-01 04:24:26 +0000658bool
659ValueObject::GetSummaryAsCString (TypeSummaryImpl* summary_ptr,
660 std::string& destination)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661{
Enrico Granata0c489f52012-03-01 04:24:26 +0000662 destination.clear();
663
664 // ideally we would like to bail out if passing NULL, but if we do so
665 // we end up not providing the summary for function pointers anymore
666 if (/*summary_ptr == NULL ||*/ m_is_getting_summary)
667 return false;
Greg Clayton48ca8b82012-01-07 20:58:07 +0000668
669 m_is_getting_summary = true;
Enrico Granata0c489f52012-03-01 04:24:26 +0000670 if (UpdateValueIfNeeded (false))
671 {
672 if (summary_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 {
Enrico Granata86cc9822012-03-19 22:58:49 +0000674 if (HasSyntheticValue())
675 m_synthetic_value->UpdateValueIfNeeded(); // the summary might depend on the synthetic children being up-to-date (e.g. ${svar%#})
676 summary_ptr->FormatObject(this, destination);
Enrico Granata0c489f52012-03-01 04:24:26 +0000677 }
678 else
679 {
680 clang_type_t clang_type = GetClangType();
681
682 // Do some default printout for function pointers
683 if (clang_type)
Enrico Granata4becb372011-06-29 22:27:15 +0000684 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000685 StreamString sstr;
686 clang_type_t elem_or_pointee_clang_type;
687 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
688 GetClangAST(),
689 &elem_or_pointee_clang_type));
690
691 if (ClangASTContext::IsFunctionPointerType (clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000692 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000693 AddressType func_ptr_address_type = eAddressTypeInvalid;
694 addr_t func_ptr_address = GetPointerValue (&func_ptr_address_type);
695 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
Enrico Granataf2bbf712011-07-15 02:26:42 +0000696 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000697 switch (func_ptr_address_type)
Jim Ingham6035b672011-03-31 00:19:25 +0000698 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000699 case eAddressTypeInvalid:
700 case eAddressTypeFile:
701 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000702
Greg Claytoncc4d0142012-02-17 07:49:44 +0000703 case eAddressTypeLoad:
Enrico Granata0c489f52012-03-01 04:24:26 +0000704 {
705 ExecutionContext exe_ctx (GetExecutionContextRef());
706
707 Address so_addr;
708 Target *target = exe_ctx.GetTargetPtr();
709 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Claytoncc4d0142012-02-17 07:49:44 +0000710 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000711 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Enrico Granataf2bbf712011-07-15 02:26:42 +0000712 {
Enrico Granata0c489f52012-03-01 04:24:26 +0000713 so_addr.Dump (&sstr,
714 exe_ctx.GetBestExecutionContextScope(),
715 Address::DumpStyleResolvedDescription,
716 Address::DumpStyleSectionNameOffset);
Enrico Granataf2bbf712011-07-15 02:26:42 +0000717 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000718 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000719 }
Greg Claytoncc4d0142012-02-17 07:49:44 +0000720 break;
Enrico Granata0c489f52012-03-01 04:24:26 +0000721
Greg Claytoncc4d0142012-02-17 07:49:44 +0000722 case eAddressTypeHost:
723 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +0000724 }
Enrico Granata0c489f52012-03-01 04:24:26 +0000725 }
726 if (sstr.GetSize() > 0)
727 {
728 destination.assign (1, '(');
729 destination.append (sstr.GetData(), sstr.GetSize());
730 destination.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 }
732 }
733 }
734 }
735 }
Greg Clayton48ca8b82012-01-07 20:58:07 +0000736 m_is_getting_summary = false;
Enrico Granata0c489f52012-03-01 04:24:26 +0000737 return !destination.empty();
738}
739
740const char *
741ValueObject::GetSummaryAsCString ()
742{
743 if (UpdateValueIfNeeded(true) && m_summary_str.empty())
744 {
745 GetSummaryAsCString(GetSummaryFormat().get(),
746 m_summary_str);
747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 if (m_summary_str.empty())
749 return NULL;
750 return m_summary_str.c_str();
751}
752
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000753bool
754ValueObject::IsCStringContainer(bool check_pointer)
755{
756 clang_type_t elem_or_pointee_clang_type;
757 const Flags type_flags (ClangASTContext::GetTypeInfo (GetClangType(),
758 GetClangAST(),
759 &elem_or_pointee_clang_type));
760 bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
761 ClangASTContext::IsCharType (elem_or_pointee_clang_type));
762 if (!is_char_arr_ptr)
763 return false;
764 if (!check_pointer)
765 return true;
766 if (type_flags.Test(ClangASTContext::eTypeIsArray))
767 return true;
Greg Claytonafacd142011-09-02 01:15:17 +0000768 addr_t cstr_address = LLDB_INVALID_ADDRESS;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000769 AddressType cstr_address_type = eAddressTypeInvalid;
Enrico Granata9128ee22011-09-06 19:20:51 +0000770 cstr_address = GetAddressOf (true, &cstr_address_type);
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000771 return (cstr_address != LLDB_INVALID_ADDRESS);
772}
773
Enrico Granata9128ee22011-09-06 19:20:51 +0000774size_t
775ValueObject::GetPointeeData (DataExtractor& data,
776 uint32_t item_idx,
777 uint32_t item_count)
778{
779 if (!IsPointerType() && !IsArrayType())
780 return 0;
781
782 if (item_count == 0)
783 return 0;
784
785 uint32_t stride = 0;
786
787 ClangASTType type(GetClangAST(),
788 GetClangType());
789
790 const uint64_t item_type_size = (IsPointerType() ? ClangASTType::GetTypeByteSize(GetClangAST(), type.GetPointeeType()) :
791 ClangASTType::GetTypeByteSize(GetClangAST(), type.GetArrayElementType(stride)));
792
793 const uint64_t bytes = item_count * item_type_size;
794
795 const uint64_t offset = item_idx * item_type_size;
796
797 if (item_idx == 0 && item_count == 1) // simply a deref
798 {
799 if (IsPointerType())
800 {
801 Error error;
802 ValueObjectSP pointee_sp = Dereference(error);
803 if (error.Fail() || pointee_sp.get() == NULL)
804 return 0;
805 return pointee_sp->GetDataExtractor().Copy(data);
806 }
807 else
808 {
809 ValueObjectSP child_sp = GetChildAtIndex(0, true);
810 if (child_sp.get() == NULL)
811 return 0;
812 return child_sp->GetDataExtractor().Copy(data);
813 }
814 return true;
815 }
816 else /* (items > 1) */
817 {
818 Error error;
819 lldb_private::DataBufferHeap* heap_buf_ptr = NULL;
820 lldb::DataBufferSP data_sp(heap_buf_ptr = new lldb_private::DataBufferHeap());
821
822 AddressType addr_type;
823 lldb::addr_t addr = IsPointerType() ? GetPointerValue(&addr_type) : GetAddressOf(true, &addr_type);
824
Enrico Granata9128ee22011-09-06 19:20:51 +0000825 switch (addr_type)
826 {
827 case eAddressTypeFile:
828 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000829 ModuleSP module_sp (GetModule());
830 if (module_sp)
Enrico Granata9128ee22011-09-06 19:20:51 +0000831 {
832 Address so_addr;
Greg Claytone72dfb32012-02-24 01:59:29 +0000833 module_sp->ResolveFileAddress(addr, so_addr);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000834 ExecutionContext exe_ctx (GetExecutionContextRef());
835 Target* target = exe_ctx.GetTargetPtr();
836 if (target)
Enrico Granata9128ee22011-09-06 19:20:51 +0000837 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000838 heap_buf_ptr->SetByteSize(bytes);
839 size_t bytes_read = target->ReadMemory(so_addr, false, heap_buf_ptr->GetBytes(), bytes, error);
840 if (error.Success())
Enrico Granata9128ee22011-09-06 19:20:51 +0000841 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000842 data.SetData(data_sp);
843 return bytes_read;
Enrico Granata9128ee22011-09-06 19:20:51 +0000844 }
845 }
846 }
847 }
848 break;
849 case eAddressTypeLoad:
Enrico Granata9128ee22011-09-06 19:20:51 +0000850 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000851 ExecutionContext exe_ctx (GetExecutionContextRef());
852 Process *process = exe_ctx.GetProcessPtr();
Enrico Granata9128ee22011-09-06 19:20:51 +0000853 if (process)
854 {
855 heap_buf_ptr->SetByteSize(bytes);
856 size_t bytes_read = process->ReadMemory(addr + offset, heap_buf_ptr->GetBytes(), bytes, error);
857 if (error.Success())
858 {
859 data.SetData(data_sp);
860 return bytes_read;
861 }
862 }
863 }
864 break;
865 case eAddressTypeHost:
866 {
867 heap_buf_ptr->CopyData((uint8_t*)(addr + offset), bytes);
868 data.SetData(data_sp);
869 return bytes;
870 }
871 break;
872 case eAddressTypeInvalid:
873 default:
874 break;
875 }
876 }
877 return 0;
878}
879
880size_t
881ValueObject::GetData (DataExtractor& data)
882{
883 UpdateValueIfNeeded(false);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000884 ExecutionContext exe_ctx (GetExecutionContextRef());
Greg Claytone72dfb32012-02-24 01:59:29 +0000885 Error error = m_value.GetValueAsData(&exe_ctx, GetClangAST(), data, 0, GetModule().get());
Enrico Granata9128ee22011-09-06 19:20:51 +0000886 if (error.Fail())
887 return 0;
888 data.SetAddressByteSize(m_data.GetAddressByteSize());
889 data.SetByteOrder(m_data.GetByteOrder());
890 return data.GetByteSize();
891}
892
893// will compute strlen(str), but without consuming more than
894// maxlen bytes out of str (this serves the purpose of reading
895// chunks of a string without having to worry about
896// missing NULL terminators in the chunk)
897// of course, if strlen(str) > maxlen, the function will return
898// maxlen_value (which should be != maxlen, because that allows you
899// to know whether strlen(str) == maxlen or strlen(str) > maxlen)
900static uint32_t
901strlen_or_inf (const char* str,
902 uint32_t maxlen,
903 uint32_t maxlen_value)
904{
905 uint32_t len = 0;
Greg Clayton8dd5c172011-10-05 22:19:51 +0000906 if (str)
Enrico Granata9128ee22011-09-06 19:20:51 +0000907 {
Greg Clayton8dd5c172011-10-05 22:19:51 +0000908 while(*str)
909 {
910 len++;str++;
911 if (len > maxlen)
912 return maxlen_value;
913 }
Enrico Granata9128ee22011-09-06 19:20:51 +0000914 }
915 return len;
916}
917
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000918void
Greg Claytoncc4d0142012-02-17 07:49:44 +0000919ValueObject::ReadPointedString (Stream& s,
920 Error& error,
921 uint32_t max_length,
922 bool honor_array,
923 Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000924{
Greg Claytoncc4d0142012-02-17 07:49:44 +0000925 ExecutionContext exe_ctx (GetExecutionContextRef());
926 Target* target = exe_ctx.GetTargetPtr();
927
928 if (target && max_length == 0)
929 max_length = target->GetMaximumSizeOfStringSummary();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000930
931 clang_type_t clang_type = GetClangType();
932 clang_type_t elem_or_pointee_clang_type;
933 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
934 GetClangAST(),
935 &elem_or_pointee_clang_type));
936 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
937 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
938 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000939 if (target == NULL)
940 {
941 s << "<no target to read from>";
942 }
943 else
944 {
945 addr_t cstr_address = LLDB_INVALID_ADDRESS;
946 AddressType cstr_address_type = eAddressTypeInvalid;
947
948 size_t cstr_len = 0;
949 bool capped_data = false;
950 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000951 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000952 // We have an array
953 cstr_len = ClangASTContext::GetArraySize (clang_type);
954 if (cstr_len > max_length)
Enrico Granata6f3533f2011-07-29 19:53:35 +0000955 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000956 capped_data = true;
957 cstr_len = max_length;
958 }
959 cstr_address = GetAddressOf (true, &cstr_address_type);
960 }
961 else
962 {
963 // We have a pointer
964 cstr_address = GetPointerValue (&cstr_address_type);
965 }
966 if (cstr_address != 0 && cstr_address != LLDB_INVALID_ADDRESS)
967 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000968 Address cstr_so_addr (cstr_address);
Greg Claytoncc4d0142012-02-17 07:49:44 +0000969 DataExtractor data;
970 size_t bytes_read = 0;
971 if (cstr_len > 0 && honor_array)
972 {
973 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
974 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
975 GetPointeeData(data, 0, cstr_len);
976
977 if ((bytes_read = data.GetByteSize()) > 0)
978 {
979 s << '"';
980 data.Dump (&s,
981 0, // Start offset in "data"
982 item_format,
983 1, // Size of item (1 byte for a char!)
984 bytes_read, // How many bytes to print?
985 UINT32_MAX, // num per line
986 LLDB_INVALID_ADDRESS,// base address
987 0, // bitfield bit size
988 0); // bitfield bit offset
989 if (capped_data)
990 s << "...";
991 s << '"';
992 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000993 }
994 else
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000995 {
Greg Claytoncc4d0142012-02-17 07:49:44 +0000996 cstr_len = max_length;
997 const size_t k_max_buf_size = 64;
998
999 size_t offset = 0;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001000
Greg Claytoncc4d0142012-02-17 07:49:44 +00001001 int cstr_len_displayed = -1;
1002 bool capped_cstr = false;
1003 // I am using GetPointeeData() here to abstract the fact that some ValueObjects are actually frozen pointers in the host
1004 // but the pointed-to data lives in the debuggee, and GetPointeeData() automatically takes care of this
1005 while ((bytes_read = GetPointeeData(data, offset, k_max_buf_size)) > 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001006 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001007 const char *cstr = data.PeekCStr(0);
1008 size_t len = strlen_or_inf (cstr, k_max_buf_size, k_max_buf_size+1);
1009 if (len > k_max_buf_size)
1010 len = k_max_buf_size;
1011 if (cstr && cstr_len_displayed < 0)
1012 s << '"';
1013
1014 if (cstr_len_displayed < 0)
1015 cstr_len_displayed = len;
1016
1017 if (len == 0)
1018 break;
1019 cstr_len_displayed += len;
1020 if (len > bytes_read)
1021 len = bytes_read;
1022 if (len > cstr_len)
1023 len = cstr_len;
1024
1025 data.Dump (&s,
1026 0, // Start offset in "data"
1027 item_format,
1028 1, // Size of item (1 byte for a char!)
1029 len, // How many bytes to print?
1030 UINT32_MAX, // num per line
1031 LLDB_INVALID_ADDRESS,// base address
1032 0, // bitfield bit size
1033 0); // bitfield bit offset
1034
1035 if (len < k_max_buf_size)
1036 break;
1037
1038 if (len >= cstr_len)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001039 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001040 capped_cstr = true;
1041 break;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001042 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001043
1044 cstr_len -= len;
1045 offset += len;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001046 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001047
1048 if (cstr_len_displayed >= 0)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001049 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001050 s << '"';
1051 if (capped_cstr)
1052 s << "...";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001053 }
1054 }
1055 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00001056 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001057 }
1058 else
1059 {
1060 error.SetErrorString("impossible to read a string from this object");
Enrico Granata6f3533f2011-07-29 19:53:35 +00001061 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001062 }
1063}
1064
Jim Ingham53c47f12010-09-10 23:12:17 +00001065const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001066ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +00001067{
Enrico Granata0a3958e2011-07-02 00:25:22 +00001068
Enrico Granatad8b5fce2011-08-02 23:12:24 +00001069 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +00001070 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001071
1072 if (!m_object_desc_str.empty())
1073 return m_object_desc_str.c_str();
1074
Greg Claytoncc4d0142012-02-17 07:49:44 +00001075 ExecutionContext exe_ctx (GetExecutionContextRef());
1076 Process *process = exe_ctx.GetProcessPtr();
Jim Ingham5a369122010-09-28 01:25:32 +00001077 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +00001078 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +00001079
Jim Ingham53c47f12010-09-10 23:12:17 +00001080 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +00001081
Greg Claytonafacd142011-09-02 01:15:17 +00001082 LanguageType language = GetObjectRuntimeLanguage();
Jim Ingham5a369122010-09-28 01:25:32 +00001083 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
1084
Jim Inghama2cf2632010-12-23 02:29:54 +00001085 if (runtime == NULL)
1086 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001087 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +00001088 clang_type_t opaque_qual_type = GetClangType();
1089 if (opaque_qual_type != NULL)
1090 {
Jim Inghamb7603bb2011-03-18 00:05:18 +00001091 bool is_signed;
1092 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
1093 || ClangASTContext::IsPointerType (opaque_qual_type))
1094 {
Greg Claytonafacd142011-09-02 01:15:17 +00001095 runtime = process->GetLanguageRuntime(eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +00001096 }
Jim Inghama2cf2632010-12-23 02:29:54 +00001097 }
1098 }
1099
Jim Ingham8d543de2011-03-31 23:01:21 +00001100 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +00001101 {
1102 m_object_desc_str.append (s.GetData());
1103 }
Sean Callanan672ad942010-10-23 00:18:49 +00001104
1105 if (m_object_desc_str.empty())
1106 return NULL;
1107 else
1108 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +00001109}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110
Enrico Granata0c489f52012-03-01 04:24:26 +00001111bool
1112ValueObject::GetValueAsCString (lldb::Format format,
1113 std::string& destination)
1114{
1115 if (ClangASTContext::IsAggregateType (GetClangType()) == false &&
1116 UpdateValueIfNeeded(false))
1117 {
1118 const Value::ContextType context_type = m_value.GetContextType();
1119
1120 switch (context_type)
1121 {
1122 case Value::eContextTypeClangType:
1123 case Value::eContextTypeLLDBType:
1124 case Value::eContextTypeVariable:
1125 {
1126 clang_type_t clang_type = GetClangType ();
1127 if (clang_type)
1128 {
1129 StreamString sstr;
1130 ExecutionContext exe_ctx (GetExecutionContextRef());
1131 ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
1132 clang_type, // The clang type to display
1133 &sstr,
1134 format, // Format to display this type with
1135 m_data, // Data to extract from
1136 0, // Byte offset into "m_data"
1137 GetByteSize(), // Byte size of item in "m_data"
1138 GetBitfieldBitSize(), // Bitfield bit size
1139 GetBitfieldBitOffset(), // Bitfield bit offset
1140 exe_ctx.GetBestExecutionContextScope());
1141 // Don't set the m_error to anything here otherwise
1142 // we won't be able to re-format as anything else. The
1143 // code for ClangASTType::DumpTypeValue() should always
1144 // return something, even if that something contains
1145 // an error messsage. "m_error" is used to detect errors
1146 // when reading the valid object, not for formatting errors.
1147 if (sstr.GetString().empty())
1148 destination.clear();
1149 else
1150 destination.swap(sstr.GetString());
1151 }
1152 }
1153 break;
1154
1155 case Value::eContextTypeRegisterInfo:
1156 {
1157 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1158 if (reg_info)
1159 {
1160 ExecutionContext exe_ctx (GetExecutionContextRef());
1161
1162 StreamString reg_sstr;
1163 m_data.Dump (&reg_sstr,
1164 0,
1165 format,
1166 reg_info->byte_size,
1167 1,
1168 UINT32_MAX,
1169 LLDB_INVALID_ADDRESS,
1170 0,
1171 0,
1172 exe_ctx.GetBestExecutionContextScope());
1173 destination.swap(reg_sstr.GetString());
1174 }
1175 }
1176 break;
1177
1178 default:
1179 break;
1180 }
1181 return !destination.empty();
1182 }
1183 else
1184 return false;
1185}
1186
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001187const char *
Jim Ingham6035b672011-03-31 00:19:25 +00001188ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001189{
Enrico Granata0c489f52012-03-01 04:24:26 +00001190 if (UpdateValueIfNeeded(true) && m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001191 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001192 lldb::Format my_format = GetFormat();
1193 if (m_format == lldb::eFormatDefault)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001195 if (m_type_format_sp)
1196 my_format = m_type_format_sp->GetFormat();
1197 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001198 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001199 if (m_is_bitfield_for_scalar)
1200 my_format = eFormatUnsigned;
1201 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 {
Enrico Granata0c489f52012-03-01 04:24:26 +00001203 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001204 {
1205 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
1206 if (reg_info)
Enrico Granata0c489f52012-03-01 04:24:26 +00001207 my_format = reg_info->format;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001208 }
Enrico Granata0c489f52012-03-01 04:24:26 +00001209 else
1210 {
1211 clang_type_t clang_type = GetClangType ();
1212 my_format = ClangASTType::GetFormat(clang_type);
1213 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214 }
1215 }
1216 }
Enrico Granata297e69f2012-03-06 23:21:16 +00001217 if (GetValueAsCString(my_format, m_value_str))
1218 {
1219 if (!m_value_did_change && m_old_value_valid)
1220 {
1221 // The value was gotten successfully, so we consider the
1222 // value as changed if the value string differs
1223 SetValueDidChange (m_old_value_str != m_value_str);
1224 }
1225 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226 }
1227 if (m_value_str.empty())
1228 return NULL;
1229 return m_value_str.c_str();
1230}
1231
Enrico Granatac3e320a2011-08-02 17:27:39 +00001232// if > 8bytes, 0 is returned. this method should mostly be used
1233// to read address values out of pointers
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001234uint64_t
1235ValueObject::GetValueAsUnsigned (uint64_t fail_value)
Enrico Granatac3e320a2011-08-02 17:27:39 +00001236{
1237 // If our byte size is zero this is an aggregate type that has children
1238 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
1239 {
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001240 Scalar scalar;
1241 if (ResolveValue (scalar))
1242 return scalar.GetRawBits64(fail_value);
Enrico Granatac3e320a2011-08-02 17:27:39 +00001243 }
Greg Clayton56d9a1b2011-08-22 02:49:39 +00001244 return fail_value;
Enrico Granatac3e320a2011-08-02 17:27:39 +00001245}
1246
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001247// if any more "special cases" are added to ValueObject::DumpPrintableRepresentation() please keep
1248// this call up to date by returning true for your new special cases. We will eventually move
1249// to checking this call result before trying to display special cases
1250bool
Enrico Granata86cc9822012-03-19 22:58:49 +00001251ValueObject::HasSpecialPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
1252 Format custom_format)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001253{
1254 clang_type_t elem_or_pointee_type;
1255 Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
1256
1257 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
Enrico Granata86cc9822012-03-19 22:58:49 +00001258 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001259 {
1260 if (IsCStringContainer(true) &&
Greg Claytonafacd142011-09-02 01:15:17 +00001261 (custom_format == eFormatCString ||
1262 custom_format == eFormatCharArray ||
1263 custom_format == eFormatChar ||
1264 custom_format == eFormatVectorOfChar))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001265 return true;
1266
1267 if (flags.Test(ClangASTContext::eTypeIsArray))
1268 {
Greg Claytonafacd142011-09-02 01:15:17 +00001269 if ((custom_format == eFormatBytes) ||
1270 (custom_format == eFormatBytesWithASCII))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001271 return true;
1272
Greg Claytonafacd142011-09-02 01:15:17 +00001273 if ((custom_format == eFormatVectorOfChar) ||
1274 (custom_format == eFormatVectorOfFloat32) ||
1275 (custom_format == eFormatVectorOfFloat64) ||
1276 (custom_format == eFormatVectorOfSInt16) ||
1277 (custom_format == eFormatVectorOfSInt32) ||
1278 (custom_format == eFormatVectorOfSInt64) ||
1279 (custom_format == eFormatVectorOfSInt8) ||
1280 (custom_format == eFormatVectorOfUInt128) ||
1281 (custom_format == eFormatVectorOfUInt16) ||
1282 (custom_format == eFormatVectorOfUInt32) ||
1283 (custom_format == eFormatVectorOfUInt64) ||
1284 (custom_format == eFormatVectorOfUInt8))
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001285 return true;
1286 }
1287 }
1288 return false;
1289}
1290
Enrico Granata9fc19442011-07-06 02:13:41 +00001291bool
1292ValueObject::DumpPrintableRepresentation(Stream& s,
1293 ValueObjectRepresentationStyle val_obj_display,
Greg Claytonafacd142011-09-02 01:15:17 +00001294 Format custom_format,
Enrico Granata86cc9822012-03-19 22:58:49 +00001295 PrintableRepresentationSpecialCases special)
Enrico Granata9fc19442011-07-06 02:13:41 +00001296{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001297
1298 clang_type_t elem_or_pointee_type;
1299 Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001300
Enrico Granata86cc9822012-03-19 22:58:49 +00001301 bool allow_special = ((special & ePrintableRepresentationSpecialCasesAllow) == ePrintableRepresentationSpecialCasesAllow);
1302 bool only_special = ((special & ePrintableRepresentationSpecialCasesOnly) == ePrintableRepresentationSpecialCasesOnly);
1303
1304 if (allow_special)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001305 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001306 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1307 && val_obj_display == ValueObject::eValueObjectRepresentationStyleValue)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001308 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001309 // when being asked to get a printable display an array or pointer type directly,
1310 // try to "do the right thing"
1311
1312 if (IsCStringContainer(true) &&
1313 (custom_format == eFormatCString ||
1314 custom_format == eFormatCharArray ||
1315 custom_format == eFormatChar ||
1316 custom_format == eFormatVectorOfChar)) // print char[] & char* directly
Enrico Granataf4efecd2011-07-12 22:56:10 +00001317 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001318 Error error;
1319 ReadPointedString(s,
1320 error,
1321 0,
1322 (custom_format == eFormatVectorOfChar) ||
1323 (custom_format == eFormatCharArray));
1324 return !error.Fail();
Enrico Granataf4efecd2011-07-12 22:56:10 +00001325 }
1326
Enrico Granata86cc9822012-03-19 22:58:49 +00001327 if (custom_format == eFormatEnum)
1328 return false;
1329
1330 // this only works for arrays, because I have no way to know when
1331 // the pointed memory ends, and no special \0 end of data marker
1332 if (flags.Test(ClangASTContext::eTypeIsArray))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001333 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001334 if ((custom_format == eFormatBytes) ||
1335 (custom_format == eFormatBytesWithASCII))
Enrico Granataf4efecd2011-07-12 22:56:10 +00001336 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001337 uint32_t count = GetNumChildren();
1338
1339 s << '[';
1340 for (uint32_t low = 0; low < count; low++)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001341 {
Enrico Granata86cc9822012-03-19 22:58:49 +00001342
1343 if (low)
1344 s << ',';
1345
1346 ValueObjectSP child = GetChildAtIndex(low,true);
1347 if (!child.get())
1348 {
1349 s << "<invalid child>";
1350 continue;
1351 }
1352 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, custom_format);
1353 }
1354
1355 s << ']';
1356
1357 return true;
1358 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001359
Enrico Granata86cc9822012-03-19 22:58:49 +00001360 if ((custom_format == eFormatVectorOfChar) ||
1361 (custom_format == eFormatVectorOfFloat32) ||
1362 (custom_format == eFormatVectorOfFloat64) ||
1363 (custom_format == eFormatVectorOfSInt16) ||
1364 (custom_format == eFormatVectorOfSInt32) ||
1365 (custom_format == eFormatVectorOfSInt64) ||
1366 (custom_format == eFormatVectorOfSInt8) ||
1367 (custom_format == eFormatVectorOfUInt128) ||
1368 (custom_format == eFormatVectorOfUInt16) ||
1369 (custom_format == eFormatVectorOfUInt32) ||
1370 (custom_format == eFormatVectorOfUInt64) ||
1371 (custom_format == eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1372 {
1373 uint32_t count = GetNumChildren();
1374
1375 Format format = FormatManager::GetSingleItemFormat(custom_format);
1376
1377 s << '[';
1378 for (uint32_t low = 0; low < count; low++)
1379 {
1380
1381 if (low)
1382 s << ',';
1383
1384 ValueObjectSP child = GetChildAtIndex(low,true);
1385 if (!child.get())
1386 {
1387 s << "<invalid child>";
1388 continue;
1389 }
1390 child->DumpPrintableRepresentation(s, ValueObject::eValueObjectRepresentationStyleValue, format);
1391 }
1392
1393 s << ']';
1394
1395 return true;
1396 }
Enrico Granataf4efecd2011-07-12 22:56:10 +00001397 }
Enrico Granata86cc9822012-03-19 22:58:49 +00001398
1399 if ((custom_format == eFormatBoolean) ||
1400 (custom_format == eFormatBinary) ||
1401 (custom_format == eFormatChar) ||
1402 (custom_format == eFormatCharPrintable) ||
1403 (custom_format == eFormatComplexFloat) ||
1404 (custom_format == eFormatDecimal) ||
1405 (custom_format == eFormatHex) ||
1406 (custom_format == eFormatFloat) ||
1407 (custom_format == eFormatOctal) ||
1408 (custom_format == eFormatOSType) ||
1409 (custom_format == eFormatUnicode16) ||
1410 (custom_format == eFormatUnicode32) ||
1411 (custom_format == eFormatUnsigned) ||
1412 (custom_format == eFormatPointer) ||
1413 (custom_format == eFormatComplexInteger) ||
1414 (custom_format == eFormatComplex) ||
1415 (custom_format == eFormatDefault)) // use the [] operator
1416 return false;
Enrico Granataf4efecd2011-07-12 22:56:10 +00001417 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001418 }
Enrico Granata85933ed2011-08-18 16:38:26 +00001419
1420 if (only_special)
1421 return false;
1422
Enrico Granata86cc9822012-03-19 22:58:49 +00001423 bool var_success = false;
1424
1425 {
1426 const char * return_value;
1427 std::string alloc_mem;
1428
1429 if (custom_format != eFormatInvalid)
1430 SetFormat(custom_format);
1431
1432 switch(val_obj_display)
1433 {
1434 case eValueObjectRepresentationStyleValue:
1435 return_value = GetValueAsCString();
1436 break;
1437
1438 case eValueObjectRepresentationStyleSummary:
1439 return_value = GetSummaryAsCString();
1440 break;
1441
1442 case eValueObjectRepresentationStyleLanguageSpecific:
1443 return_value = GetObjectDescription();
1444 break;
1445
1446 case eValueObjectRepresentationStyleLocation:
1447 return_value = GetLocationAsCString();
1448 break;
1449
1450 case eValueObjectRepresentationStyleChildrenCount:
1451 {
1452 alloc_mem.resize(512);
1453 return_value = &alloc_mem[0];
1454 int count = GetNumChildren();
1455 snprintf((char*)return_value, 512, "%d", count);
1456 }
1457 break;
1458
1459 case eValueObjectRepresentationStyleType:
1460 return_value = GetTypeName().AsCString();
1461 break;
1462
1463 default:
1464 break;
1465 }
1466
1467 if (!return_value)
1468 {
1469 if (val_obj_display == eValueObjectRepresentationStyleValue)
1470 return_value = GetSummaryAsCString();
1471 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1472 {
1473 if (ClangASTContext::IsAggregateType (GetClangType()) == true)
1474 {
1475 // this thing has no value, and it seems to have no summary
1476 // some combination of unitialized data and other factors can also
1477 // raise this condition, so let's print a nice generic description
1478 {
1479 alloc_mem.resize(684);
1480 return_value = &alloc_mem[0];
1481 snprintf((char*)return_value, 684, "%s @ %s", GetTypeName().AsCString(), GetLocationAsCString());
1482 }
1483 }
1484 else
1485 return_value = GetValueAsCString();
1486 }
1487 }
1488
1489 if (return_value)
1490 s.PutCString(return_value);
1491 else
1492 {
1493 if (m_error.Fail())
1494 s.Printf("<%s>", m_error.AsCString());
1495 else if (val_obj_display == eValueObjectRepresentationStyleSummary)
1496 s.PutCString("<no summary available>");
1497 else if (val_obj_display == eValueObjectRepresentationStyleValue)
1498 s.PutCString("<no value available>");
1499 else if (val_obj_display == eValueObjectRepresentationStyleLanguageSpecific)
1500 s.PutCString("<not a valid Objective-C object>"); // edit this if we have other runtimes that support a description
1501 else
1502 s.PutCString("<no printable representation>");
1503 }
1504
1505 // we should only return false here if we could not do *anything*
1506 // even if we have an error message as output, that's a success
1507 // from our callers' perspective, so return true
1508 var_success = true;
1509
1510 if (custom_format != eFormatInvalid)
1511 SetFormat(eFormatDefault);
1512 }
1513
Enrico Granataf4efecd2011-07-12 22:56:10 +00001514 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001515}
1516
Greg Clayton737b9322010-09-13 03:32:57 +00001517addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001518ValueObject::GetAddressOf (bool scalar_is_load_address, AddressType *address_type)
Greg Clayton73b472d2010-10-27 03:32:59 +00001519{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001520 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001521 return LLDB_INVALID_ADDRESS;
1522
Greg Clayton73b472d2010-10-27 03:32:59 +00001523 switch (m_value.GetValueType())
1524 {
1525 case Value::eValueTypeScalar:
1526 if (scalar_is_load_address)
1527 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001528 if(address_type)
1529 *address_type = eAddressTypeLoad;
Greg Clayton73b472d2010-10-27 03:32:59 +00001530 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1531 }
1532 break;
1533
1534 case Value::eValueTypeLoadAddress:
1535 case Value::eValueTypeFileAddress:
1536 case Value::eValueTypeHostAddress:
1537 {
Enrico Granata9128ee22011-09-06 19:20:51 +00001538 if(address_type)
1539 *address_type = m_value.GetValueAddressType ();
Greg Clayton73b472d2010-10-27 03:32:59 +00001540 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1541 }
1542 break;
1543 }
Enrico Granata9128ee22011-09-06 19:20:51 +00001544 if (address_type)
1545 *address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +00001546 return LLDB_INVALID_ADDRESS;
1547}
1548
1549addr_t
Enrico Granata9128ee22011-09-06 19:20:51 +00001550ValueObject::GetPointerValue (AddressType *address_type)
Greg Clayton737b9322010-09-13 03:32:57 +00001551{
Greg Claytonafacd142011-09-02 01:15:17 +00001552 addr_t address = LLDB_INVALID_ADDRESS;
Enrico Granata9128ee22011-09-06 19:20:51 +00001553 if(address_type)
1554 *address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001555
Enrico Granatac3e320a2011-08-02 17:27:39 +00001556 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001557 return address;
1558
Greg Clayton73b472d2010-10-27 03:32:59 +00001559 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001560 {
1561 case Value::eValueTypeScalar:
Enrico Granata9128ee22011-09-06 19:20:51 +00001562 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
Greg Clayton737b9322010-09-13 03:32:57 +00001563 break;
1564
Enrico Granata9128ee22011-09-06 19:20:51 +00001565 case Value::eValueTypeHostAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001566 case Value::eValueTypeLoadAddress:
1567 case Value::eValueTypeFileAddress:
Greg Clayton737b9322010-09-13 03:32:57 +00001568 {
1569 uint32_t data_offset = 0;
1570 address = m_data.GetPointer(&data_offset);
Greg Clayton737b9322010-09-13 03:32:57 +00001571 }
1572 break;
1573 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001574
Enrico Granata9128ee22011-09-06 19:20:51 +00001575 if (address_type)
1576 *address_type = GetAddressTypeOfChildren();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001577
Greg Clayton737b9322010-09-13 03:32:57 +00001578 return address;
1579}
1580
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581bool
Jim Ingham6035b672011-03-31 00:19:25 +00001582ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001583{
1584 // Make sure our value is up to date first so that our location and location
1585 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001586 if (!UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001587 return false;
1588
1589 uint32_t count = 0;
Greg Claytonafacd142011-09-02 01:15:17 +00001590 Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001591
Greg Claytonb1320972010-07-14 00:18:15 +00001592 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001593
Jim Ingham16e0c682011-08-12 23:34:31 +00001594 Value::ValueType value_type = m_value.GetValueType();
1595
1596 if (value_type == Value::eValueTypeScalar)
1597 {
1598 // If the value is already a scalar, then let the scalar change itself:
1599 m_value.GetScalar().SetValueFromCString (value_str, encoding, byte_size);
1600 }
1601 else if (byte_size <= Scalar::GetMaxByteSize())
1602 {
1603 // If the value fits in a scalar, then make a new scalar and again let the
1604 // scalar code do the conversion, then figure out where to put the new value.
1605 Scalar new_scalar;
1606 Error error;
1607 error = new_scalar.SetValueFromCString (value_str, encoding, byte_size);
1608 if (error.Success())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001609 {
Jim Ingham4b536182011-08-09 02:12:22 +00001610 switch (value_type)
1611 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001612 case Value::eValueTypeLoadAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001613 {
1614 // If it is a load address, then the scalar value is the storage location
1615 // of the data, and we have to shove this value down to that load location.
Greg Claytoncc4d0142012-02-17 07:49:44 +00001616 ExecutionContext exe_ctx (GetExecutionContextRef());
1617 Process *process = exe_ctx.GetProcessPtr();
1618 if (process)
Jim Ingham16e0c682011-08-12 23:34:31 +00001619 {
Greg Claytonafacd142011-09-02 01:15:17 +00001620 addr_t target_addr = m_value.GetScalar().GetRawBits64(LLDB_INVALID_ADDRESS);
Greg Claytoncc4d0142012-02-17 07:49:44 +00001621 size_t bytes_written = process->WriteScalarToMemory (target_addr,
1622 new_scalar,
1623 byte_size,
1624 error);
Jim Ingham16e0c682011-08-12 23:34:31 +00001625 if (!error.Success() || bytes_written != byte_size)
1626 return false;
1627 }
1628 }
Jim Ingham4b536182011-08-09 02:12:22 +00001629 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001630 case Value::eValueTypeHostAddress:
Jim Ingham16e0c682011-08-12 23:34:31 +00001631 {
1632 // If it is a host address, then we stuff the scalar as a DataBuffer into the Value's data.
1633 DataExtractor new_data;
1634 new_data.SetByteOrder (m_data.GetByteOrder());
1635
1636 DataBufferSP buffer_sp (new DataBufferHeap(byte_size, 0));
1637 m_data.SetData(buffer_sp, 0);
1638 bool success = new_scalar.GetData(new_data);
1639 if (success)
1640 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00001641 new_data.CopyByteOrderedData (0,
1642 byte_size,
1643 const_cast<uint8_t *>(m_data.GetDataStart()),
1644 byte_size,
1645 m_data.GetByteOrder());
Jim Ingham16e0c682011-08-12 23:34:31 +00001646 }
1647 m_value.GetScalar() = (uintptr_t)m_data.GetDataStart();
1648
1649 }
Jim Ingham4b536182011-08-09 02:12:22 +00001650 break;
Greg Claytoncc4d0142012-02-17 07:49:44 +00001651 case Value::eValueTypeFileAddress:
1652 case Value::eValueTypeScalar:
Jim Ingham16e0c682011-08-12 23:34:31 +00001653 break;
Jim Ingham4b536182011-08-09 02:12:22 +00001654 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 }
1656 else
1657 {
Jim Ingham16e0c682011-08-12 23:34:31 +00001658 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001660 }
1661 else
1662 {
1663 // We don't support setting things bigger than a scalar at present.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664 return false;
1665 }
Jim Ingham16e0c682011-08-12 23:34:31 +00001666
1667 // If we have reached this point, then we have successfully changed the value.
1668 SetNeedsUpdate();
1669 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001670}
1671
Greg Clayton81e871e2012-02-04 02:27:34 +00001672bool
1673ValueObject::GetDeclaration (Declaration &decl)
1674{
1675 decl.Clear();
1676 return false;
1677}
1678
Greg Clayton84db9102012-03-26 23:03:23 +00001679ConstString
1680ValueObject::GetTypeName()
1681{
1682 return ClangASTType::GetConstTypeName (GetClangAST(), GetClangType());
1683}
1684
1685ConstString
1686ValueObject::GetQualifiedTypeName()
1687{
1688 return ClangASTType::GetConstQualifiedTypeName (GetClangAST(), GetClangType());
1689}
1690
1691
Greg Claytonafacd142011-09-02 01:15:17 +00001692LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00001693ValueObject::GetObjectRuntimeLanguage ()
1694{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001695 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1696 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001697}
1698
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699void
Jim Ingham58b59f92011-04-22 23:53:53 +00001700ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701{
Jim Ingham58b59f92011-04-22 23:53:53 +00001702 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001703}
1704
1705ValueObjectSP
1706ValueObject::GetSyntheticChild (const ConstString &key) const
1707{
1708 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001709 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001710 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001711 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 return synthetic_child_sp;
1713}
1714
1715bool
1716ValueObject::IsPointerType ()
1717{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001718 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719}
1720
Jim Inghamb7603bb2011-03-18 00:05:18 +00001721bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001722ValueObject::IsArrayType ()
1723{
1724 return ClangASTContext::IsArrayType (GetClangType());
1725}
1726
1727bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001728ValueObject::IsScalarType ()
1729{
1730 return ClangASTContext::IsScalarType (GetClangType());
1731}
1732
1733bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001734ValueObject::IsIntegerType (bool &is_signed)
1735{
1736 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1737}
Greg Clayton73b472d2010-10-27 03:32:59 +00001738
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001739bool
1740ValueObject::IsPointerOrReferenceType ()
1741{
Greg Clayton007d5be2011-05-30 00:49:24 +00001742 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1743}
1744
1745bool
1746ValueObject::IsPossibleCPlusPlusDynamicType ()
1747{
1748 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749}
1750
Greg Claytondea8cb42011-06-29 22:09:02 +00001751bool
1752ValueObject::IsPossibleDynamicType ()
1753{
1754 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1755}
1756
Greg Claytonafacd142011-09-02 01:15:17 +00001757ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001758ValueObject::GetSyntheticArrayMember (int32_t index, bool can_create)
1759{
1760 if (IsArrayType())
1761 return GetSyntheticArrayMemberFromArray(index, can_create);
1762
1763 if (IsPointerType())
1764 return GetSyntheticArrayMemberFromPointer(index, can_create);
1765
1766 return ValueObjectSP();
1767
1768}
1769
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770ValueObjectSP
1771ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1772{
1773 ValueObjectSP synthetic_child_sp;
1774 if (IsPointerType ())
1775 {
1776 char index_str[64];
1777 snprintf(index_str, sizeof(index_str), "[%i]", index);
1778 ConstString index_const_str(index_str);
1779 // Check if we have already created a synthetic array member in this
1780 // valid object. If we have we will re-use it.
1781 synthetic_child_sp = GetSyntheticChild (index_const_str);
1782 if (!synthetic_child_sp)
1783 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001784 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785 // We haven't made a synthetic array member for INDEX yet, so
1786 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001787 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788
1789 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001790 if (synthetic_child)
1791 {
1792 AddSyntheticChild(index_const_str, synthetic_child);
1793 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001794 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00001795 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001796 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797 }
1798 }
1799 return synthetic_child_sp;
1800}
Jim Ingham22777012010-09-23 02:01:19 +00001801
Greg Claytondaf515f2011-07-09 20:12:33 +00001802// This allows you to create an array member using and index
1803// that doesn't not fall in the normal bounds of the array.
1804// Many times structure can be defined as:
1805// struct Collection
1806// {
1807// uint32_t item_count;
1808// Item item_array[0];
1809// };
1810// The size of the "item_array" is 1, but many times in practice
1811// there are more items in "item_array".
1812
1813ValueObjectSP
1814ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1815{
1816 ValueObjectSP synthetic_child_sp;
1817 if (IsArrayType ())
1818 {
1819 char index_str[64];
1820 snprintf(index_str, sizeof(index_str), "[%i]", index);
1821 ConstString index_const_str(index_str);
1822 // Check if we have already created a synthetic array member in this
1823 // valid object. If we have we will re-use it.
1824 synthetic_child_sp = GetSyntheticChild (index_const_str);
1825 if (!synthetic_child_sp)
1826 {
1827 ValueObject *synthetic_child;
1828 // We haven't made a synthetic array member for INDEX yet, so
1829 // lets make one and cache it for any future reference.
1830 synthetic_child = CreateChildAtIndex(0, true, index);
1831
1832 // Cache the value if we got one back...
1833 if (synthetic_child)
1834 {
1835 AddSyntheticChild(index_const_str, synthetic_child);
1836 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001837 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00001838 synthetic_child_sp->m_is_array_item_for_pointer = true;
1839 }
1840 }
1841 }
1842 return synthetic_child_sp;
1843}
1844
Enrico Granata9fc19442011-07-06 02:13:41 +00001845ValueObjectSP
1846ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1847{
1848 ValueObjectSP synthetic_child_sp;
1849 if (IsScalarType ())
1850 {
1851 char index_str[64];
1852 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1853 ConstString index_const_str(index_str);
1854 // Check if we have already created a synthetic array member in this
1855 // valid object. If we have we will re-use it.
1856 synthetic_child_sp = GetSyntheticChild (index_const_str);
1857 if (!synthetic_child_sp)
1858 {
1859 ValueObjectChild *synthetic_child;
1860 // We haven't made a synthetic array member for INDEX yet, so
1861 // lets make one and cache it for any future reference.
1862 synthetic_child = new ValueObjectChild(*this,
1863 GetClangAST(),
1864 GetClangType(),
1865 index_const_str,
1866 GetByteSize(),
1867 0,
1868 to-from+1,
1869 from,
1870 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001871 false,
1872 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00001873
1874 // Cache the value if we got one back...
1875 if (synthetic_child)
1876 {
1877 AddSyntheticChild(index_const_str, synthetic_child);
1878 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001879 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00001880 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1881 }
1882 }
1883 }
1884 return synthetic_child_sp;
1885}
1886
Greg Claytonafacd142011-09-02 01:15:17 +00001887ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001888ValueObject::GetSyntheticArrayRangeChild (uint32_t from, uint32_t to, bool can_create)
1889{
1890 ValueObjectSP synthetic_child_sp;
1891 if (IsArrayType () || IsPointerType ())
1892 {
1893 char index_str[64];
1894 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1895 ConstString index_const_str(index_str);
1896 // Check if we have already created a synthetic array member in this
1897 // valid object. If we have we will re-use it.
1898 synthetic_child_sp = GetSyntheticChild (index_const_str);
1899 if (!synthetic_child_sp)
1900 {
1901 ValueObjectSynthetic *synthetic_child;
1902
1903 // We haven't made a synthetic array member for INDEX yet, so
1904 // lets make one and cache it for any future reference.
Enrico Granata061858c2012-02-15 02:34:21 +00001905 SyntheticArrayView *view = new SyntheticArrayView(SyntheticChildren::Flags());
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001906 view->AddRange(from,to);
1907 SyntheticChildrenSP view_sp(view);
1908 synthetic_child = new ValueObjectSynthetic(*this, view_sp);
1909
1910 // Cache the value if we got one back...
1911 if (synthetic_child)
1912 {
1913 AddSyntheticChild(index_const_str, synthetic_child);
1914 synthetic_child_sp = synthetic_child->GetSP();
1915 synthetic_child_sp->SetName(ConstString(index_str));
1916 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1917 }
1918 }
1919 }
1920 return synthetic_child_sp;
1921}
1922
Greg Claytonafacd142011-09-02 01:15:17 +00001923ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00001924ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1925{
1926
1927 ValueObjectSP synthetic_child_sp;
1928
1929 char name_str[64];
1930 snprintf(name_str, sizeof(name_str), "@%i", offset);
1931 ConstString name_const_str(name_str);
1932
1933 // Check if we have already created a synthetic array member in this
1934 // valid object. If we have we will re-use it.
1935 synthetic_child_sp = GetSyntheticChild (name_const_str);
1936
1937 if (synthetic_child_sp.get())
1938 return synthetic_child_sp;
1939
1940 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00001941 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001942
1943 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1944 type.GetASTContext(),
1945 type.GetOpaqueQualType(),
1946 name_const_str,
1947 type.GetTypeByteSize(),
1948 offset,
1949 0,
1950 0,
1951 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001952 false,
1953 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001954 if (synthetic_child)
1955 {
1956 AddSyntheticChild(name_const_str, synthetic_child);
1957 synthetic_child_sp = synthetic_child->GetSP();
1958 synthetic_child_sp->SetName(name_const_str);
1959 synthetic_child_sp->m_is_child_at_offset = true;
1960 }
1961 return synthetic_child_sp;
1962}
1963
Enrico Granatad55546b2011-07-22 00:16:08 +00001964// your expression path needs to have a leading . or ->
1965// (unless it somehow "looks like" an array, in which case it has
1966// a leading [ symbol). while the [ is meaningful and should be shown
1967// to the user, . and -> are just parser design, but by no means
1968// added information for the user.. strip them off
1969static const char*
1970SkipLeadingExpressionPathSeparators(const char* expression)
1971{
1972 if (!expression || !expression[0])
1973 return expression;
1974 if (expression[0] == '.')
1975 return expression+1;
1976 if (expression[0] == '-' && expression[1] == '>')
1977 return expression+2;
1978 return expression;
1979}
1980
Greg Claytonafacd142011-09-02 01:15:17 +00001981ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00001982ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1983{
1984 ValueObjectSP synthetic_child_sp;
1985 ConstString name_const_string(expression);
1986 // Check if we have already created a synthetic array member in this
1987 // valid object. If we have we will re-use it.
1988 synthetic_child_sp = GetSyntheticChild (name_const_string);
1989 if (!synthetic_child_sp)
1990 {
1991 // We haven't made a synthetic array member for expression yet, so
1992 // lets make one and cache it for any future reference.
1993 synthetic_child_sp = GetValueForExpressionPath(expression);
1994
1995 // Cache the value if we got one back...
1996 if (synthetic_child_sp.get())
1997 {
1998 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00001999 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00002000 synthetic_child_sp->m_is_expression_path_child = true;
2001 }
2002 }
2003 return synthetic_child_sp;
2004}
2005
2006void
Enrico Granata86cc9822012-03-19 22:58:49 +00002007ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002008{
Enrico Granata86cc9822012-03-19 22:58:49 +00002009 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00002010 return;
2011
Enrico Granata86cc9822012-03-19 22:58:49 +00002012 if (!UpdateFormatsIfNeeded(m_last_format_mgr_dynamic) && m_synthetic_value)
2013 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002014
Enrico Granata0c489f52012-03-01 04:24:26 +00002015 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002016 return;
2017
Enrico Granata86cc9822012-03-19 22:58:49 +00002018 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002019}
2020
Jim Ingham78a685a2011-04-16 00:01:13 +00002021void
Greg Claytonafacd142011-09-02 01:15:17 +00002022ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002023{
Greg Claytonafacd142011-09-02 01:15:17 +00002024 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002025 return;
2026
Jim Ingham58b59f92011-04-22 23:53:53 +00002027 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002028 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002029 ExecutionContext exe_ctx (GetExecutionContextRef());
2030 Process *process = exe_ctx.GetProcessPtr();
2031 if (process)
Jim Ingham78a685a2011-04-16 00:01:13 +00002032 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002033 bool worth_having_dynamic_value = false;
Jim Ingham78a685a2011-04-16 00:01:13 +00002034
Greg Claytoncc4d0142012-02-17 07:49:44 +00002035
2036 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
2037 // hard code this everywhere.
2038 LanguageType known_type = GetObjectRuntimeLanguage();
2039 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
Jim Ingham78a685a2011-04-16 00:01:13 +00002040 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002041 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
2042 if (runtime)
2043 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00002044 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00002045 else
2046 {
2047 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (eLanguageTypeC_plus_plus);
2048 if (cpp_runtime)
2049 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
2050
2051 if (!worth_having_dynamic_value)
2052 {
2053 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (eLanguageTypeObjC);
2054 if (objc_runtime)
2055 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
2056 }
2057 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002058
Greg Claytoncc4d0142012-02-17 07:49:44 +00002059 if (worth_having_dynamic_value)
2060 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
2061 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002062 }
2063}
2064
Jim Ingham58b59f92011-04-22 23:53:53 +00002065ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002066ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002067{
Greg Claytonafacd142011-09-02 01:15:17 +00002068 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002069 return ValueObjectSP();
2070
2071 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002072 {
Jim Ingham2837b762011-05-04 03:43:18 +00002073 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002074 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002075 if (m_dynamic_value)
2076 return m_dynamic_value->GetSP();
2077 else
2078 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002079}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002080
Jim Ingham60dbabb2011-12-08 19:44:08 +00002081ValueObjectSP
2082ValueObject::GetStaticValue()
2083{
2084 return GetSP();
2085}
2086
Enrico Granatad55546b2011-07-22 00:16:08 +00002087ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002088ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002089{
Enrico Granata86cc9822012-03-19 22:58:49 +00002090 if (use_synthetic == false)
2091 return ValueObjectSP();
2092
Enrico Granatad55546b2011-07-22 00:16:08 +00002093 CalculateSyntheticValue(use_synthetic);
2094
2095 if (m_synthetic_value)
2096 return m_synthetic_value->GetSP();
2097 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002098 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002099}
2100
Greg Claytone221f822011-01-21 01:59:00 +00002101bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002102ValueObject::HasSyntheticValue()
2103{
2104 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
2105
Enrico Granata0c489f52012-03-01 04:24:26 +00002106 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002107 return false;
2108
Enrico Granata86cc9822012-03-19 22:58:49 +00002109 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002110
2111 if (m_synthetic_value)
2112 return true;
2113 else
2114 return false;
2115}
2116
2117bool
Greg Claytone221f822011-01-21 01:59:00 +00002118ValueObject::GetBaseClassPath (Stream &s)
2119{
2120 if (IsBaseClass())
2121 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002122 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00002123 clang_type_t clang_type = GetClangType();
2124 std::string cxx_class_name;
2125 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
2126 if (this_had_base_class)
2127 {
2128 if (parent_had_base_class)
2129 s.PutCString("::");
2130 s.PutCString(cxx_class_name.c_str());
2131 }
2132 return parent_had_base_class || this_had_base_class;
2133 }
2134 return false;
2135}
2136
2137
2138ValueObject *
2139ValueObject::GetNonBaseClassParent()
2140{
Jim Ingham78a685a2011-04-16 00:01:13 +00002141 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002142 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002143 if (GetParent()->IsBaseClass())
2144 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002145 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002146 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002147 }
2148 return NULL;
2149}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002150
2151void
Enrico Granata4becb372011-06-29 22:27:15 +00002152ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002153{
Greg Claytone221f822011-01-21 01:59:00 +00002154 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002155
Enrico Granata86cc9822012-03-19 22:58:49 +00002156 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002157 {
Enrico Granata4becb372011-06-29 22:27:15 +00002158 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2159 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2160 // the eHonorPointers mode is meant to produce strings in this latter format
2161 s.PutCString("*(");
2162 }
Greg Claytone221f822011-01-21 01:59:00 +00002163
Enrico Granata4becb372011-06-29 22:27:15 +00002164 ValueObject* parent = GetParent();
2165
2166 if (parent)
2167 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002168
2169 // if we are a deref_of_parent just because we are synthetic array
2170 // members made up to allow ptr[%d] syntax to work in variable
2171 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002172 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002173 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002174
Greg Claytone221f822011-01-21 01:59:00 +00002175 if (!IsBaseClass())
2176 {
2177 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002178 {
Greg Claytone221f822011-01-21 01:59:00 +00002179 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2180 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002181 {
Greg Claytone221f822011-01-21 01:59:00 +00002182 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2183 if (non_base_class_parent_clang_type)
2184 {
2185 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
2186
Enrico Granata86cc9822012-03-19 22:58:49 +00002187 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002188 {
2189 s.PutCString("->");
2190 }
Enrico Granata4becb372011-06-29 22:27:15 +00002191 else
2192 {
2193 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
2194 {
2195 s.PutCString("->");
2196 }
2197 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
2198 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
2199 {
2200 s.PutChar('.');
2201 }
Greg Claytone221f822011-01-21 01:59:00 +00002202 }
2203 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002204 }
Greg Claytone221f822011-01-21 01:59:00 +00002205
2206 const char *name = GetName().GetCString();
2207 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002208 {
Greg Claytone221f822011-01-21 01:59:00 +00002209 if (qualify_cxx_base_classes)
2210 {
2211 if (GetBaseClassPath (s))
2212 s.PutCString("::");
2213 }
2214 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002215 }
2216 }
2217 }
2218
Enrico Granata86cc9822012-03-19 22:58:49 +00002219 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002220 {
Greg Claytone221f822011-01-21 01:59:00 +00002221 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002222 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002223}
2224
Greg Claytonafacd142011-09-02 01:15:17 +00002225ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002226ValueObject::GetValueForExpressionPath(const char* expression,
2227 const char** first_unparsed,
2228 ExpressionPathScanEndReason* reason_to_stop,
2229 ExpressionPathEndResultType* final_value_type,
2230 const GetValueForExpressionPathOptions& options,
2231 ExpressionPathAftermath* final_task_on_target)
2232{
2233
2234 const char* dummy_first_unparsed;
2235 ExpressionPathScanEndReason dummy_reason_to_stop;
2236 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002237 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002238
2239 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2240 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2241 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2242 final_value_type ? final_value_type : &dummy_final_value_type,
2243 options,
2244 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2245
Enrico Granata86cc9822012-03-19 22:58:49 +00002246 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002247 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002248
Enrico Granata86cc9822012-03-19 22:58:49 +00002249 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 +00002250 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002251 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002252 {
2253 Error error;
2254 ValueObjectSP final_value = ret_val->Dereference(error);
2255 if (error.Fail() || !final_value.get())
2256 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002257 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002258 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002259 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002260 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002261 return ValueObjectSP();
2262 }
2263 else
2264 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002265 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002266 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002267 return final_value;
2268 }
2269 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002270 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002271 {
2272 Error error;
2273 ValueObjectSP final_value = ret_val->AddressOf(error);
2274 if (error.Fail() || !final_value.get())
2275 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002276 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002277 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002278 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002279 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002280 return ValueObjectSP();
2281 }
2282 else
2283 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002284 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002285 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002286 return final_value;
2287 }
2288 }
2289 }
2290 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2291}
2292
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002293int
2294ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002295 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002296 const char** first_unparsed,
2297 ExpressionPathScanEndReason* reason_to_stop,
2298 ExpressionPathEndResultType* final_value_type,
2299 const GetValueForExpressionPathOptions& options,
2300 ExpressionPathAftermath* final_task_on_target)
2301{
2302 const char* dummy_first_unparsed;
2303 ExpressionPathScanEndReason dummy_reason_to_stop;
2304 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002305 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002306
2307 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2308 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2309 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2310 final_value_type ? final_value_type : &dummy_final_value_type,
2311 options,
2312 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2313
2314 if (!ret_val.get()) // if there are errors, I add nothing to the list
2315 return 0;
2316
Enrico Granata86cc9822012-03-19 22:58:49 +00002317 if (*reason_to_stop != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002318 {
2319 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002320 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002321 {
2322 list->Append(ret_val);
2323 return 1;
2324 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002325 if (ret_val.get() && *final_value_type == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002326 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002327 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002328 {
2329 Error error;
2330 ValueObjectSP final_value = ret_val->Dereference(error);
2331 if (error.Fail() || !final_value.get())
2332 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002333 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2334 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002335 return 0;
2336 }
2337 else
2338 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002339 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002340 list->Append(final_value);
2341 return 1;
2342 }
2343 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002344 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002345 {
2346 Error error;
2347 ValueObjectSP final_value = ret_val->AddressOf(error);
2348 if (error.Fail() || !final_value.get())
2349 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002350 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2351 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002352 return 0;
2353 }
2354 else
2355 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002356 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002357 list->Append(final_value);
2358 return 1;
2359 }
2360 }
2361 }
2362 }
2363 else
2364 {
2365 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2366 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2367 ret_val,
2368 list,
2369 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2370 final_value_type ? final_value_type : &dummy_final_value_type,
2371 options,
2372 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2373 }
2374 // in any non-covered case, just do the obviously right thing
2375 list->Append(ret_val);
2376 return 1;
2377}
2378
Greg Claytonafacd142011-09-02 01:15:17 +00002379ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002380ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2381 const char** first_unparsed,
2382 ExpressionPathScanEndReason* reason_to_stop,
2383 ExpressionPathEndResultType* final_result,
2384 const GetValueForExpressionPathOptions& options,
2385 ExpressionPathAftermath* what_next)
2386{
2387 ValueObjectSP root = GetSP();
2388
2389 if (!root.get())
2390 return ValueObjectSP();
2391
2392 *first_unparsed = expression_cstr;
2393
2394 while (true)
2395 {
2396
2397 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2398
Greg Claytonafacd142011-09-02 01:15:17 +00002399 clang_type_t root_clang_type = root->GetClangType();
2400 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002401 Flags root_clang_type_info,pointee_clang_type_info;
2402
2403 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2404 if (pointee_clang_type)
2405 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002406
2407 if (!expression_cstr || *expression_cstr == '\0')
2408 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002409 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002410 return root;
2411 }
2412
2413 switch (*expression_cstr)
2414 {
2415 case '-':
2416 {
2417 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002418 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 +00002419 {
2420 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002421 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2422 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002423 return ValueObjectSP();
2424 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002425 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2426 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002427 options.m_no_fragile_ivar)
2428 {
2429 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002430 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2431 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002432 return ValueObjectSP();
2433 }
2434 if (expression_cstr[1] != '>')
2435 {
2436 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002437 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2438 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002439 return ValueObjectSP();
2440 }
2441 expression_cstr++; // skip the -
2442 }
2443 case '.': // or fallthrough from ->
2444 {
2445 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002446 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 +00002447 {
2448 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002449 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2450 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002451 return ValueObjectSP();
2452 }
2453 expression_cstr++; // skip .
2454 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2455 ConstString child_name;
2456 if (!next_separator) // if no other separator just expand this last layer
2457 {
2458 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002459 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2460
2461 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002462 {
2463 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002464 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2465 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002466 return child_valobj_sp;
2467 }
2468 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2469 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002470 if (root->IsSynthetic())
2471 child_valobj_sp = root;
2472 else
2473 child_valobj_sp = root->GetSyntheticValue();
2474
2475 if (child_valobj_sp.get())
2476 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002477 }
2478
2479 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2480 // so we hit the "else" branch, and return an error
2481 if(child_valobj_sp.get()) // if it worked, just return
2482 {
2483 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002484 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2485 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002486 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002487 }
2488 else
2489 {
2490 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002491 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2492 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002493 return ValueObjectSP();
2494 }
2495 }
2496 else // other layers do expand
2497 {
2498 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002499 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2500 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002501 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002502 root = child_valobj_sp;
2503 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002504 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002505 continue;
2506 }
2507 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2508 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002509 child_valobj_sp = root->GetSyntheticValue(true);
2510 if (child_valobj_sp)
2511 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002512 }
2513
2514 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2515 // so we hit the "else" branch, and return an error
2516 if(child_valobj_sp.get()) // if it worked, move on
2517 {
2518 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002519 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002520 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002521 continue;
2522 }
2523 else
2524 {
2525 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002526 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2527 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002528 return ValueObjectSP();
2529 }
2530 }
2531 break;
2532 }
2533 case '[':
2534 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002535 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 +00002536 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002537 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002538 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002539 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2540 {
2541 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002542 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2543 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002544 return ValueObjectSP();
2545 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002546 }
2547 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2548 {
2549 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002550 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2551 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002552 return ValueObjectSP();
2553 }
2554 }
2555 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2556 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002557 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002558 {
2559 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002560 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2561 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002562 return ValueObjectSP();
2563 }
2564 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2565 {
2566 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002567 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2568 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002569 return root;
2570 }
2571 }
2572 const char *separator_position = ::strchr(expression_cstr+1,'-');
2573 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2574 if (!close_bracket_position) // if there is no ], this is a syntax error
2575 {
2576 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002577 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2578 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002579 return ValueObjectSP();
2580 }
2581 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2582 {
2583 char *end = NULL;
2584 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2585 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2586 {
2587 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002588 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2589 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002590 return ValueObjectSP();
2591 }
2592 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2593 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002594 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002595 {
2596 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002597 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2598 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002599 return root;
2600 }
2601 else
2602 {
2603 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002604 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2605 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002606 return ValueObjectSP();
2607 }
2608 }
2609 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002610 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002611 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002612 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2613 if (!child_valobj_sp)
2614 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002615 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002616 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2617 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002618 if (child_valobj_sp)
2619 {
2620 root = child_valobj_sp;
2621 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002622 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002623 continue;
2624 }
2625 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002626 {
2627 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002628 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2629 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002630 return ValueObjectSP();
2631 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002632 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002633 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002634 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002635 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 +00002636 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002637 {
2638 Error error;
2639 root = root->Dereference(error);
2640 if (error.Fail() || !root.get())
2641 {
2642 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002643 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2644 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002645 return ValueObjectSP();
2646 }
2647 else
2648 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002649 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002650 continue;
2651 }
2652 }
2653 else
2654 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002655 if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
Greg Clayton84db9102012-03-26 23:03:23 +00002656 root->GetClangType()) == eLanguageTypeObjC
2657 && ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2658 && root->HasSyntheticValue()
2659 && options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002660 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002661 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002662 }
2663 else
2664 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002665 if (!root.get())
2666 {
2667 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002668 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2669 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002670 return ValueObjectSP();
2671 }
2672 else
2673 {
2674 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002675 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002676 continue;
2677 }
2678 }
2679 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002680 else if (ClangASTContext::IsScalarType(root_clang_type))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002681 {
2682 root = root->GetSyntheticBitFieldChild(index, index, true);
2683 if (!root.get())
2684 {
2685 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002686 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2687 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002688 return ValueObjectSP();
2689 }
2690 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2691 {
2692 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002693 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2694 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002695 return root;
2696 }
2697 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002698 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002699 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002700 if (root->HasSyntheticValue())
2701 root = root->GetSyntheticValue();
2702 else if (!root->IsSynthetic())
2703 {
2704 *first_unparsed = expression_cstr;
2705 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2706 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2707 return ValueObjectSP();
2708 }
2709 // if we are here, then root itself is a synthetic VO.. should be good to go
2710
Enrico Granata27b625e2011-08-09 01:04:56 +00002711 if (!root.get())
2712 {
2713 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002714 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2715 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2716 return ValueObjectSP();
2717 }
2718 root = root->GetChildAtIndex(index, true);
2719 if (!root.get())
2720 {
2721 *first_unparsed = expression_cstr;
2722 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2723 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002724 return ValueObjectSP();
2725 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00002726 else
2727 {
2728 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002729 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002730 continue;
2731 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002732 }
2733 else
2734 {
2735 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002736 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2737 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002738 return ValueObjectSP();
2739 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002740 }
2741 else // we have a low and a high index
2742 {
2743 char *end = NULL;
2744 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2745 if (!end || end != separator_position) // if something weird is in our way return an error
2746 {
2747 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002748 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2749 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002750 return ValueObjectSP();
2751 }
2752 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2753 if (!end || end != close_bracket_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 if (index_lower > index_higher) // swap indices if required
2761 {
2762 unsigned long temp = index_lower;
2763 index_lower = index_higher;
2764 index_higher = temp;
2765 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002766 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002767 {
2768 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2769 if (!root.get())
2770 {
2771 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002772 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2773 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002774 return ValueObjectSP();
2775 }
2776 else
2777 {
2778 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002779 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2780 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002781 return root;
2782 }
2783 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002784 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 +00002785 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002786 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002787 {
2788 Error error;
2789 root = root->Dereference(error);
2790 if (error.Fail() || !root.get())
2791 {
2792 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002793 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2794 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002795 return ValueObjectSP();
2796 }
2797 else
2798 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002799 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002800 continue;
2801 }
2802 }
2803 else
2804 {
2805 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002806 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2807 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002808 return root;
2809 }
2810 }
2811 break;
2812 }
2813 default: // some non-separator is in the way
2814 {
2815 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002816 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2817 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002818 return ValueObjectSP();
2819 break;
2820 }
2821 }
2822 }
2823}
2824
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002825int
2826ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2827 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00002828 ValueObjectSP root,
2829 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002830 ExpressionPathScanEndReason* reason_to_stop,
2831 ExpressionPathEndResultType* final_result,
2832 const GetValueForExpressionPathOptions& options,
2833 ExpressionPathAftermath* what_next)
2834{
2835 if (!root.get())
2836 return 0;
2837
2838 *first_unparsed = expression_cstr;
2839
2840 while (true)
2841 {
2842
2843 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2844
Greg Claytonafacd142011-09-02 01:15:17 +00002845 clang_type_t root_clang_type = root->GetClangType();
2846 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002847 Flags root_clang_type_info,pointee_clang_type_info;
2848
2849 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2850 if (pointee_clang_type)
2851 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2852
2853 if (!expression_cstr || *expression_cstr == '\0')
2854 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002855 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002856 list->Append(root);
2857 return 1;
2858 }
2859
2860 switch (*expression_cstr)
2861 {
2862 case '[':
2863 {
2864 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2865 {
2866 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2867 {
2868 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002869 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2870 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002871 return 0;
2872 }
2873 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2874 {
2875 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002876 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2877 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002878 return 0;
2879 }
2880 }
2881 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2882 {
2883 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2884 {
2885 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002886 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2887 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002888 return 0;
2889 }
2890 else // expand this into list
2891 {
2892 int max_index = root->GetNumChildren() - 1;
2893 for (int index = 0; index < max_index; index++)
2894 {
2895 ValueObjectSP child =
2896 root->GetChildAtIndex(index, true);
2897 list->Append(child);
2898 }
2899 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002900 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2901 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002902 return max_index; // tell me number of items I added to the VOList
2903 }
2904 }
2905 const char *separator_position = ::strchr(expression_cstr+1,'-');
2906 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2907 if (!close_bracket_position) // if there is no ], this is a syntax error
2908 {
2909 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002910 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2911 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002912 return 0;
2913 }
2914 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2915 {
2916 char *end = NULL;
2917 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2918 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2919 {
2920 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002921 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2922 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002923 return 0;
2924 }
2925 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2926 {
2927 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2928 {
2929 int max_index = root->GetNumChildren() - 1;
2930 for (int index = 0; index < max_index; index++)
2931 {
2932 ValueObjectSP child =
2933 root->GetChildAtIndex(index, true);
2934 list->Append(child);
2935 }
2936 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002937 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2938 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002939 return max_index; // tell me number of items I added to the VOList
2940 }
2941 else
2942 {
2943 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002944 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2945 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002946 return 0;
2947 }
2948 }
2949 // from here on we do have a valid index
2950 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2951 {
2952 root = root->GetChildAtIndex(index, true);
2953 if (!root.get())
2954 {
2955 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002956 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2957 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002958 return 0;
2959 }
2960 else
2961 {
2962 list->Append(root);
2963 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002964 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2965 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002966 return 1;
2967 }
2968 }
2969 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2970 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002971 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 +00002972 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2973 {
2974 Error error;
2975 root = root->Dereference(error);
2976 if (error.Fail() || !root.get())
2977 {
2978 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002979 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2980 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002981 return 0;
2982 }
2983 else
2984 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002985 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002986 continue;
2987 }
2988 }
2989 else
2990 {
2991 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2992 if (!root.get())
2993 {
2994 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002995 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2996 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002997 return 0;
2998 }
2999 else
3000 {
3001 list->Append(root);
3002 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003003 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3004 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003005 return 1;
3006 }
3007 }
3008 }
3009 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3010 {
3011 root = root->GetSyntheticBitFieldChild(index, index, true);
3012 if (!root.get())
3013 {
3014 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003015 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3016 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003017 return 0;
3018 }
3019 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3020 {
3021 list->Append(root);
3022 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003023 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3024 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003025 return 1;
3026 }
3027 }
3028 }
3029 else // we have a low and a high index
3030 {
3031 char *end = NULL;
3032 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3033 if (!end || end != separator_position) // if something weird is in our way return an error
3034 {
3035 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003036 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3037 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003038 return 0;
3039 }
3040 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3041 if (!end || end != close_bracket_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 if (index_lower > index_higher) // swap indices if required
3049 {
3050 unsigned long temp = index_lower;
3051 index_lower = index_higher;
3052 index_higher = temp;
3053 }
3054 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
3055 {
3056 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3057 if (!root.get())
3058 {
3059 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003060 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3061 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003062 return 0;
3063 }
3064 else
3065 {
3066 list->Append(root);
3067 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003068 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3069 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003070 return 1;
3071 }
3072 }
3073 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 +00003074 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003075 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
3076 {
3077 Error error;
3078 root = root->Dereference(error);
3079 if (error.Fail() || !root.get())
3080 {
3081 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003082 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3083 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003084 return 0;
3085 }
3086 else
3087 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003088 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003089 continue;
3090 }
3091 }
3092 else
3093 {
Johnny Chen44805302011-07-19 19:48:13 +00003094 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003095 index <= index_higher; index++)
3096 {
3097 ValueObjectSP child =
3098 root->GetChildAtIndex(index, true);
3099 list->Append(child);
3100 }
3101 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003102 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3103 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003104 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3105 }
3106 }
3107 break;
3108 }
3109 default: // some non-[ separator, or something entirely wrong, is in the way
3110 {
3111 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003112 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3113 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003114 return 0;
3115 break;
3116 }
3117 }
3118 }
3119}
3120
Enrico Granata0c489f52012-03-01 04:24:26 +00003121static void
3122DumpValueObject_Impl (Stream &s,
3123 ValueObject *valobj,
3124 const ValueObject::DumpValueObjectOptions& options,
3125 uint32_t ptr_depth,
3126 uint32_t curr_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003127{
Greg Clayton007d5be2011-05-30 00:49:24 +00003128 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003129 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003130 bool update_success = valobj->UpdateValueIfNeeded (options.m_use_dynamic, true);
Greg Clayton007d5be2011-05-30 00:49:24 +00003131
Enrico Granata0c489f52012-03-01 04:24:26 +00003132 const char *root_valobj_name =
3133 options.m_root_valobj_name.empty() ?
3134 valobj->GetName().AsCString() :
3135 options.m_root_valobj_name.c_str();
3136
3137 if (update_success && options.m_use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00003138 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003139 ValueObject *dynamic_value = valobj->GetDynamicValue(options.m_use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00003140 if (dynamic_value)
3141 valobj = dynamic_value;
3142 }
3143
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003144 clang_type_t clang_type = valobj->GetClangType();
3145
Greg Clayton73b472d2010-10-27 03:32:59 +00003146 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003147 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00003148 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
3149 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003150
Enrico Granata0c489f52012-03-01 04:24:26 +00003151 const bool print_valobj = options.m_flat_output == false || has_value;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003152
3153 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003154 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003155 if (options.m_show_location)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003156 {
Jim Ingham6035b672011-03-31 00:19:25 +00003157 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003158 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003159
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003160 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003161
Greg Clayton7c8a9662010-11-02 01:50:16 +00003162 // Always show the type for the top level items.
Enrico Granata0c489f52012-03-01 04:24:26 +00003163 if (options.m_show_types || (curr_depth == 0 && !options.m_flat_output))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003164 {
Greg Clayton84db9102012-03-26 23:03:23 +00003165 const char* typeName = valobj->GetQualifiedTypeName().AsCString("<invalid type>");
3166 //const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
Enrico Granata9910bc82011-08-03 02:18:51 +00003167 s.Printf("(%s", typeName);
3168 // only show dynamic types if the user really wants to see types
Enrico Granata0c489f52012-03-01 04:24:26 +00003169 if (options.m_show_types && options.m_use_dynamic != eNoDynamicValues &&
Enrico Granata9910bc82011-08-03 02:18:51 +00003170 (/*strstr(typeName, "id") == typeName ||*/
Greg Claytonafacd142011-09-02 01:15:17 +00003171 ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == eLanguageTypeObjC))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003172 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003173 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3174 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatac3e320a2011-08-02 17:27:39 +00003175 if (process == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003176 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003177 else
3178 {
3179 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
3180 if (runtime == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003181 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003182 else
3183 {
3184 ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
3185 if (!runtime->IsValidISA(isa))
Enrico Granata9910bc82011-08-03 02:18:51 +00003186 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003187 else
3188 s.Printf(", dynamic type: %s) ",
3189 runtime->GetActualTypeName(isa).GetCString());
3190 }
3191 }
3192 }
3193 else
3194 s.Printf(") ");
3195 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003196
Greg Clayton1d3afba2010-10-05 00:00:42 +00003197
Enrico Granata0c489f52012-03-01 04:24:26 +00003198 if (options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003199 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003200 // If we are showing types, also qualify the C++ base classes
Enrico Granata0c489f52012-03-01 04:24:26 +00003201 const bool qualify_cxx_base_classes = options.m_show_types;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003202 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003203 s.PutCString(" =");
3204 }
3205 else
3206 {
3207 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
3208 s.Printf ("%s =", name_cstr);
3209 }
3210
Enrico Granata0c489f52012-03-01 04:24:26 +00003211 if (!options.m_scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003212 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003213 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003214 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003215 }
3216
Enrico Granata0c489f52012-03-01 04:24:26 +00003217 std::string summary_str;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003218 std::string value_str;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003219 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00003220 const char *sum_cstr = NULL;
Enrico Granata0c489f52012-03-01 04:24:26 +00003221 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003222
Enrico Granata0c489f52012-03-01 04:24:26 +00003223 if (options.m_omit_summary_depth > 0)
Enrico Granata0c5ef692011-07-16 01:22:04 +00003224 entry = NULL;
3225
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003226 if (err_cstr == NULL)
3227 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003228 if (options.m_format != eFormatDefault && options.m_format != valobj->GetFormat())
Greg Clayton6efba4f2012-01-26 21:08:30 +00003229 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003230 valobj->GetValueAsCString(options.m_format,
3231 value_str);
Greg Clayton6efba4f2012-01-26 21:08:30 +00003232 }
Enrico Granata0c489f52012-03-01 04:24:26 +00003233 else
Greg Clayton6efba4f2012-01-26 21:08:30 +00003234 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003235 val_cstr = valobj->GetValueAsCString();
3236 if (val_cstr)
3237 value_str = val_cstr;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003238 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003239 err_cstr = valobj->GetError().AsCString();
3240 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003241
3242 if (err_cstr)
3243 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003244 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00003245 }
3246 else
3247 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003248 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003249 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003250 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003251 if (options.m_omit_summary_depth == 0)
3252 {
3253 if (options.m_summary_sp)
3254 {
3255 valobj->GetSummaryAsCString(entry, summary_str);
3256 sum_cstr = summary_str.c_str();
3257 }
3258 else
3259 sum_cstr = valobj->GetSummaryAsCString();
3260 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003261
Greg Clayton6efba4f2012-01-26 21:08:30 +00003262 // Make sure we have a value and make sure the summary didn't
3263 // specify that the value should not be printed
3264 if (!value_str.empty() && (entry == NULL || entry->DoesPrintValue() || sum_cstr == NULL))
3265 s.Printf(" %s", value_str.c_str());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003266
Enrico Granata9dd75c82011-07-15 23:30:15 +00003267 if (sum_cstr)
Enrico Granata0c489f52012-03-01 04:24:26 +00003268 s.Printf(" %s", sum_cstr);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003269
Enrico Granata0c489f52012-03-01 04:24:26 +00003270 if (options.m_use_objc)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003271 {
Jim Ingham6035b672011-03-31 00:19:25 +00003272 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003273 if (object_desc)
3274 s.Printf(" %s\n", object_desc);
3275 else
Sean Callanan672ad942010-10-23 00:18:49 +00003276 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003277 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00003278 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003279 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003280
Enrico Granata0c489f52012-03-01 04:24:26 +00003281 if (curr_depth < options.m_max_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003282 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003283 // We will show children for all concrete types. We won't show
3284 // pointer contents unless a pointer depth has been specified.
3285 // We won't reference contents unless the reference is the
3286 // root object (depth of zero).
3287 bool print_children = true;
3288
3289 // Use a new temporary pointer depth in case we override the
3290 // current pointer depth below...
3291 uint32_t curr_ptr_depth = ptr_depth;
3292
3293 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
3294 if (is_ptr || is_ref)
3295 {
3296 // We have a pointer or reference whose value is an address.
3297 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00003298 AddressType ptr_address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003299 if (valobj->GetPointerValue (&ptr_address_type) == 0)
Greg Clayton73b472d2010-10-27 03:32:59 +00003300 print_children = false;
3301
3302 else if (is_ref && curr_depth == 0)
3303 {
3304 // If this is the root object (depth is zero) that we are showing
3305 // and it is a reference, and no pointer depth has been supplied
3306 // print out what it references. Don't do this at deeper depths
3307 // otherwise we can end up with infinite recursion...
3308 curr_ptr_depth = 1;
3309 }
3310
3311 if (curr_ptr_depth == 0)
3312 print_children = false;
3313 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003314
Enrico Granata0a3958e2011-07-02 00:25:22 +00003315 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00003316 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003317 ValueObject* synth_valobj;
3318 ValueObjectSP synth_valobj_sp = valobj->GetSyntheticValue (options.m_use_synthetic);
3319 synth_valobj = (synth_valobj_sp ? synth_valobj_sp.get() : valobj);
Enrico Granatac482a192011-08-17 22:13:59 +00003320 uint32_t num_children = synth_valobj->GetNumChildren();
Enrico Granata22c55d12011-08-12 02:00:06 +00003321 bool print_dotdotdot = false;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003322 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003323 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003324 if (options.m_flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003325 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003326 if (print_valobj)
3327 s.EOL();
3328 }
3329 else
3330 {
3331 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00003332 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003333 s.IndentMore();
3334 }
Enrico Granata22c55d12011-08-12 02:00:06 +00003335
Greg Claytoncc4d0142012-02-17 07:49:44 +00003336 uint32_t max_num_children = valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00003337
Enrico Granata0c489f52012-03-01 04:24:26 +00003338 if (num_children > max_num_children && !options.m_ignore_cap)
Enrico Granata22c55d12011-08-12 02:00:06 +00003339 {
3340 num_children = max_num_children;
3341 print_dotdotdot = true;
3342 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003343
Enrico Granata0c489f52012-03-01 04:24:26 +00003344 ValueObject::DumpValueObjectOptions child_options(options);
3345 child_options.SetFormat().SetSummary().SetRootValueObjectName();
3346 child_options.SetScopeChecked(true)
3347 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003348 for (uint32_t idx=0; idx<num_children; ++idx)
3349 {
Enrico Granatac482a192011-08-17 22:13:59 +00003350 ValueObjectSP child_sp(synth_valobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003351 if (child_sp.get())
3352 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003353 DumpValueObject_Impl (s,
3354 child_sp.get(),
3355 child_options,
3356 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
3357 curr_depth + 1);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003358 }
3359 }
3360
Enrico Granata0c489f52012-03-01 04:24:26 +00003361 if (!options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003362 {
Enrico Granata22c55d12011-08-12 02:00:06 +00003363 if (print_dotdotdot)
Enrico Granata61a80ba2011-08-12 16:42:31 +00003364 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003365 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3366 Target *target = exe_ctx.GetTargetPtr();
3367 if (target)
3368 target->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
Enrico Granata22c55d12011-08-12 02:00:06 +00003369 s.Indent("...\n");
Enrico Granata61a80ba2011-08-12 16:42:31 +00003370 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003371 s.IndentLess();
3372 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003373 }
3374 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003375 else if (has_children)
3376 {
3377 // Aggregate, no children...
3378 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00003379 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003380 }
3381 else
3382 {
3383 if (print_valobj)
3384 s.EOL();
3385 }
3386
Greg Clayton1d3afba2010-10-05 00:00:42 +00003387 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003388 else
3389 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00003390 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003391 }
3392 }
3393 else
3394 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003395 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003396 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003397 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003398 }
3399 }
3400 }
3401 }
3402}
3403
Enrico Granata0c489f52012-03-01 04:24:26 +00003404void
Greg Claytonf830dbb2012-03-22 18:15:37 +00003405ValueObject::LogValueObject (Log *log,
3406 ValueObject *valobj)
3407{
3408 if (log && valobj)
3409 return LogValueObject (log, valobj, DumpValueObjectOptions::DefaultOptions());
3410}
3411
3412void
3413ValueObject::LogValueObject (Log *log,
3414 ValueObject *valobj,
3415 const DumpValueObjectOptions& options)
3416{
3417 if (log && valobj)
3418 {
3419 StreamString s;
3420 ValueObject::DumpValueObject (s, valobj, options);
3421 if (s.GetSize())
3422 log->PutCString(s.GetData());
3423 }
3424}
3425
3426void
Enrico Granata0c489f52012-03-01 04:24:26 +00003427ValueObject::DumpValueObject (Stream &s,
3428 ValueObject *valobj)
3429{
3430
3431 if (!valobj)
3432 return;
3433
3434 DumpValueObject_Impl(s,
3435 valobj,
3436 DumpValueObjectOptions::DefaultOptions(),
3437 0,
3438 0);
3439}
3440
3441void
3442ValueObject::DumpValueObject (Stream &s,
3443 ValueObject *valobj,
3444 const DumpValueObjectOptions& options)
3445{
3446 DumpValueObject_Impl(s,
3447 valobj,
3448 options,
3449 options.m_max_ptr_depth, // max pointer depth allowed, we will go down from here
3450 0 // current object depth is 0 since we are just starting
3451 );
3452}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003453
3454ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003455ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003456{
3457 ValueObjectSP valobj_sp;
3458
Enrico Granatac3e320a2011-08-02 17:27:39 +00003459 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003460 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003461 ExecutionContext exe_ctx (GetExecutionContextRef());
3462 clang::ASTContext *ast = GetClangAST ();
3463
3464 DataExtractor data;
3465 data.SetByteOrder (m_data.GetByteOrder());
3466 data.SetAddressByteSize(m_data.GetAddressByteSize());
3467
Greg Claytone72dfb32012-02-24 01:59:29 +00003468 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003469
3470 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3471 ast,
3472 GetClangType(),
3473 name,
3474 data,
3475 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003476 }
Jim Ingham6035b672011-03-31 00:19:25 +00003477
3478 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003479 {
Jim Ingham58b59f92011-04-22 23:53:53 +00003480 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003481 }
3482 return valobj_sp;
3483}
3484
Greg Claytonafacd142011-09-02 01:15:17 +00003485ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003486ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003487{
Jim Ingham58b59f92011-04-22 23:53:53 +00003488 if (m_deref_valobj)
3489 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003490
Greg Clayton54979cd2010-12-15 05:08:08 +00003491 const bool is_pointer_type = IsPointerType();
3492 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003493 {
3494 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003495 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003496
3497 std::string child_name_str;
3498 uint32_t child_byte_size = 0;
3499 int32_t child_byte_offset = 0;
3500 uint32_t child_bitfield_bit_size = 0;
3501 uint32_t child_bitfield_bit_offset = 0;
3502 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003503 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003504 const bool transparent_pointers = false;
3505 clang::ASTContext *clang_ast = GetClangAST();
3506 clang_type_t clang_type = GetClangType();
3507 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003508
Greg Claytoncc4d0142012-02-17 07:49:44 +00003509 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003510
3511 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
3512 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003513 GetName().GetCString(),
3514 clang_type,
3515 0,
3516 transparent_pointers,
3517 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003518 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003519 child_name_str,
3520 child_byte_size,
3521 child_byte_offset,
3522 child_bitfield_bit_size,
3523 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003524 child_is_base_class,
3525 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003526 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003527 {
3528 ConstString child_name;
3529 if (!child_name_str.empty())
3530 child_name.SetCString (child_name_str.c_str());
3531
Jim Ingham58b59f92011-04-22 23:53:53 +00003532 m_deref_valobj = new ValueObjectChild (*this,
3533 clang_ast,
3534 child_clang_type,
3535 child_name,
3536 child_byte_size,
3537 child_byte_offset,
3538 child_bitfield_bit_size,
3539 child_bitfield_bit_offset,
3540 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003541 child_is_deref_of_parent,
3542 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003543 }
3544 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003545
Jim Ingham58b59f92011-04-22 23:53:53 +00003546 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003547 {
3548 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003549 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003550 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003551 else
3552 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003553 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003554 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003555
3556 if (is_pointer_type)
3557 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3558 else
3559 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003560 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003561 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003562}
3563
Greg Claytonafacd142011-09-02 01:15:17 +00003564ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003565ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003566{
Jim Ingham78a685a2011-04-16 00:01:13 +00003567 if (m_addr_of_valobj_sp)
3568 return m_addr_of_valobj_sp;
3569
Greg Claytone0d378b2011-03-24 21:19:54 +00003570 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003571 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003572 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003573 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003574 if (addr != LLDB_INVALID_ADDRESS)
3575 {
3576 switch (address_type)
3577 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003578 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003579 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003580 {
3581 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003582 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003583 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3584 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003585 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003586
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003587 case eAddressTypeFile:
3588 case eAddressTypeLoad:
3589 case eAddressTypeHost:
3590 {
3591 clang::ASTContext *ast = GetClangAST();
3592 clang_type_t clang_type = GetClangType();
3593 if (ast && clang_type)
3594 {
3595 std::string name (1, '&');
3596 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003597 ExecutionContext exe_ctx (GetExecutionContextRef());
3598 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003599 ast,
3600 ClangASTContext::CreatePointerType (ast, clang_type),
3601 ConstString (name.c_str()),
3602 addr,
3603 eAddressTypeInvalid,
3604 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003605 }
3606 }
3607 break;
3608 }
3609 }
Jim Ingham78a685a2011-04-16 00:01:13 +00003610 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003611}
3612
Greg Clayton9a142cf2012-02-03 05:34:10 +00003613ValueObjectSP
3614ValueObject::Cast (const ClangASTType &clang_ast_type)
3615{
Greg Clayton81e871e2012-02-04 02:27:34 +00003616 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003617}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003618
Greg Claytonafacd142011-09-02 01:15:17 +00003619ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003620ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3621{
Greg Claytonafacd142011-09-02 01:15:17 +00003622 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003623 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003624 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003625
3626 if (ptr_value != LLDB_INVALID_ADDRESS)
3627 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003628 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003629 ExecutionContext exe_ctx (GetExecutionContextRef());
3630 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003631 name,
3632 ptr_addr,
3633 clang_ast_type);
3634 }
3635 return valobj_sp;
3636}
3637
Greg Claytonafacd142011-09-02 01:15:17 +00003638ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003639ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3640{
Greg Claytonafacd142011-09-02 01:15:17 +00003641 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003642 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003643 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003644
3645 if (ptr_value != LLDB_INVALID_ADDRESS)
3646 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003647 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003648 ExecutionContext exe_ctx (GetExecutionContextRef());
3649 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003650 name,
3651 ptr_addr,
3652 type_sp);
3653 }
3654 return valobj_sp;
3655}
3656
Jim Ingham6035b672011-03-31 00:19:25 +00003657ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003658 m_mod_id(),
3659 m_exe_ctx_ref(),
3660 m_needs_update (true),
3661 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003662{
3663}
3664
3665ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003666 m_mod_id(),
3667 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003668 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003669 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003670{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003671 ExecutionContext exe_ctx(exe_scope);
3672 TargetSP target_sp (exe_ctx.GetTargetSP());
3673 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003674 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003675 m_exe_ctx_ref.SetTargetSP (target_sp);
3676 ProcessSP process_sp (exe_ctx.GetProcessSP());
3677 if (!process_sp)
3678 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003679
Greg Claytoncc4d0142012-02-17 07:49:44 +00003680 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003681 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003682 m_mod_id = process_sp->GetModID();
3683 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003684
Greg Claytoncc4d0142012-02-17 07:49:44 +00003685 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003686
Greg Claytoncc4d0142012-02-17 07:49:44 +00003687 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003688 {
3689 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003690 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003691 }
Jim Ingham6035b672011-03-31 00:19:25 +00003692
Greg Claytoncc4d0142012-02-17 07:49:44 +00003693 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003694 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003695 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003696
Greg Claytoncc4d0142012-02-17 07:49:44 +00003697 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3698 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003699 {
3700 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003701 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003702 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003703 if (frame_sp)
3704 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003705 }
3706 }
3707 }
Jim Ingham6035b672011-03-31 00:19:25 +00003708}
3709
3710ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003711 m_mod_id(),
3712 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3713 m_needs_update (true),
3714 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003715{
3716}
3717
3718ValueObject::EvaluationPoint::~EvaluationPoint ()
3719{
3720}
3721
Jim Ingham6035b672011-03-31 00:19:25 +00003722// This function checks the EvaluationPoint against the current process state. If the current
3723// state matches the evaluation point, or the evaluation point is already invalid, then we return
3724// false, meaning "no change". If the current state is different, we update our state, and return
3725// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3726// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003727// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003728
3729bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003730ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003731{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003732
3733 // 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 +00003734 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock());
Jim Ingham73ca05a2011-12-17 01:35:57 +00003735
Greg Claytoncc4d0142012-02-17 07:49:44 +00003736 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003737 return false;
3738
Jim Ingham6035b672011-03-31 00:19:25 +00003739 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003740 Process *process = exe_ctx.GetProcessPtr();
3741 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003742 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003743
Jim Ingham6035b672011-03-31 00:19:25 +00003744 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003745 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003746
Jim Ingham78a685a2011-04-16 00:01:13 +00003747 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3748 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003749 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003750 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003751
3752 bool changed;
3753
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003754 if (m_mod_id.IsValid())
3755 {
3756 if (m_mod_id == current_mod_id)
3757 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003758 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003759 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003760 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003761 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003762 else
3763 {
3764 m_mod_id = current_mod_id;
3765 m_needs_update = true;
3766 changed = true;
3767 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003768 }
Jim Ingham6035b672011-03-31 00:19:25 +00003769
Jim Ingham73ca05a2011-12-17 01:35:57 +00003770 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3771 // That way we'll be sure to return a valid exe_scope.
3772 // 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 +00003773
Greg Claytoncc4d0142012-02-17 07:49:44 +00003774 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003775 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003776 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3777 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003778 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003779 if (m_exe_ctx_ref.HasFrameRef())
3780 {
3781 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
3782 if (!frame_sp)
3783 {
3784 // We used to have a frame, but now it is gone
3785 SetInvalid();
3786 }
3787 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003788 }
Jim Ingham6035b672011-03-31 00:19:25 +00003789 else
3790 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003791 // We used to have a thread, but now it is gone
3792 SetInvalid();
Jim Ingham6035b672011-03-31 00:19:25 +00003793 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003794
Jim Ingham6035b672011-03-31 00:19:25 +00003795 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003796 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003797}
3798
Jim Ingham61be0902011-05-02 18:13:59 +00003799void
3800ValueObject::EvaluationPoint::SetUpdated ()
3801{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003802 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3803 if (process_sp)
3804 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003805 m_first_update = false;
3806 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003807}
3808
3809
Greg Claytoncc4d0142012-02-17 07:49:44 +00003810//bool
3811//ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3812//{
3813// if (!IsValid())
3814// return false;
3815//
3816// bool needs_update = false;
3817//
3818// // The target has to be non-null, and the
3819// Target *target = exe_scope->CalculateTarget();
3820// if (target != NULL)
3821// {
3822// Target *old_target = m_target_sp.get();
3823// assert (target == old_target);
3824// Process *process = exe_scope->CalculateProcess();
3825// if (process != NULL)
3826// {
3827// // FOR NOW - assume you can't update variable objects across process boundaries.
3828// Process *old_process = m_process_sp.get();
3829// assert (process == old_process);
3830// ProcessModID current_mod_id = process->GetModID();
3831// if (m_mod_id != current_mod_id)
3832// {
3833// needs_update = true;
3834// m_mod_id = current_mod_id;
3835// }
3836// // See if we're switching the thread or stack context. If no thread is given, this is
3837// // being evaluated in a global context.
3838// Thread *thread = exe_scope->CalculateThread();
3839// if (thread != NULL)
3840// {
3841// user_id_t new_thread_index = thread->GetIndexID();
3842// if (new_thread_index != m_thread_id)
3843// {
3844// needs_update = true;
3845// m_thread_id = new_thread_index;
3846// m_stack_id.Clear();
3847// }
3848//
3849// StackFrame *new_frame = exe_scope->CalculateStackFrame();
3850// if (new_frame != NULL)
3851// {
3852// if (new_frame->GetStackID() != m_stack_id)
3853// {
3854// needs_update = true;
3855// m_stack_id = new_frame->GetStackID();
3856// }
3857// }
3858// else
3859// {
3860// m_stack_id.Clear();
3861// needs_update = true;
3862// }
3863// }
3864// else
3865// {
3866// // If this had been given a thread, and now there is none, we should update.
3867// // Otherwise we don't have to do anything.
3868// if (m_thread_id != LLDB_INVALID_UID)
3869// {
3870// m_thread_id = LLDB_INVALID_UID;
3871// m_stack_id.Clear();
3872// needs_update = true;
3873// }
3874// }
3875// }
3876// else
3877// {
3878// // If there is no process, then we don't need to update anything.
3879// // But if we're switching from having a process to not, we should try to update.
3880// if (m_process_sp.get() != NULL)
3881// {
3882// needs_update = true;
3883// m_process_sp.reset();
3884// m_thread_id = LLDB_INVALID_UID;
3885// m_stack_id.Clear();
3886// }
3887// }
3888// }
3889// else
3890// {
3891// // If there's no target, nothing can change so we don't need to update anything.
3892// // But if we're switching from having a target to not, we should try to update.
3893// if (m_target_sp.get() != NULL)
3894// {
3895// needs_update = true;
3896// m_target_sp.reset();
3897// m_process_sp.reset();
3898// m_thread_id = LLDB_INVALID_UID;
3899// m_stack_id.Clear();
3900// }
3901// }
3902// if (!m_needs_update)
3903// m_needs_update = needs_update;
3904//
3905// return needs_update;
3906//}
Enrico Granataf2bbf712011-07-15 02:26:42 +00003907
3908void
Enrico Granata86cc9822012-03-19 22:58:49 +00003909ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003910{
Enrico Granata86cc9822012-03-19 22:58:49 +00003911 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3912 m_value_str.clear();
3913
3914 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3915 m_location_str.clear();
3916
3917 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
3918 {
3919 m_is_getting_summary = false;
3920 m_summary_str.clear();
3921 }
3922
3923 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3924 m_object_desc_str.clear();
3925
3926 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3927 {
3928 if (m_synthetic_value)
3929 m_synthetic_value = NULL;
3930 }
Johnny Chen44805302011-07-19 19:48:13 +00003931}
Enrico Granata9128ee22011-09-06 19:20:51 +00003932
3933SymbolContextScope *
3934ValueObject::GetSymbolContextScope()
3935{
3936 if (m_parent)
3937 {
3938 if (!m_parent->IsPointerOrReferenceType())
3939 return m_parent->GetSymbolContextScope();
3940 }
3941 return NULL;
3942}