blob: 4ec7088c780a6989cc71d87cdcf1341292a49ba9 [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 Claytonafacd142011-09-02 01:15:17 +00001679LanguageType
Jim Ingham5a369122010-09-28 01:25:32 +00001680ValueObject::GetObjectRuntimeLanguage ()
1681{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001682 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1683 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001684}
1685
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001686void
Jim Ingham58b59f92011-04-22 23:53:53 +00001687ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001688{
Jim Ingham58b59f92011-04-22 23:53:53 +00001689 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001690}
1691
1692ValueObjectSP
1693ValueObject::GetSyntheticChild (const ConstString &key) const
1694{
1695 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001696 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001697 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001698 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001699 return synthetic_child_sp;
1700}
1701
1702bool
1703ValueObject::IsPointerType ()
1704{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001705 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706}
1707
Jim Inghamb7603bb2011-03-18 00:05:18 +00001708bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001709ValueObject::IsArrayType ()
1710{
1711 return ClangASTContext::IsArrayType (GetClangType());
1712}
1713
1714bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001715ValueObject::IsScalarType ()
1716{
1717 return ClangASTContext::IsScalarType (GetClangType());
1718}
1719
1720bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001721ValueObject::IsIntegerType (bool &is_signed)
1722{
1723 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1724}
Greg Clayton73b472d2010-10-27 03:32:59 +00001725
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001726bool
1727ValueObject::IsPointerOrReferenceType ()
1728{
Greg Clayton007d5be2011-05-30 00:49:24 +00001729 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1730}
1731
1732bool
1733ValueObject::IsPossibleCPlusPlusDynamicType ()
1734{
1735 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001736}
1737
Greg Claytondea8cb42011-06-29 22:09:02 +00001738bool
1739ValueObject::IsPossibleDynamicType ()
1740{
1741 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1742}
1743
Greg Claytonafacd142011-09-02 01:15:17 +00001744ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001745ValueObject::GetSyntheticArrayMember (int32_t index, bool can_create)
1746{
1747 if (IsArrayType())
1748 return GetSyntheticArrayMemberFromArray(index, can_create);
1749
1750 if (IsPointerType())
1751 return GetSyntheticArrayMemberFromPointer(index, can_create);
1752
1753 return ValueObjectSP();
1754
1755}
1756
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001757ValueObjectSP
1758ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1759{
1760 ValueObjectSP synthetic_child_sp;
1761 if (IsPointerType ())
1762 {
1763 char index_str[64];
1764 snprintf(index_str, sizeof(index_str), "[%i]", index);
1765 ConstString index_const_str(index_str);
1766 // Check if we have already created a synthetic array member in this
1767 // valid object. If we have we will re-use it.
1768 synthetic_child_sp = GetSyntheticChild (index_const_str);
1769 if (!synthetic_child_sp)
1770 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001771 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001772 // We haven't made a synthetic array member for INDEX yet, so
1773 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001774 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001775
1776 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001777 if (synthetic_child)
1778 {
1779 AddSyntheticChild(index_const_str, synthetic_child);
1780 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001781 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00001782 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001784 }
1785 }
1786 return synthetic_child_sp;
1787}
Jim Ingham22777012010-09-23 02:01:19 +00001788
Greg Claytondaf515f2011-07-09 20:12:33 +00001789// This allows you to create an array member using and index
1790// that doesn't not fall in the normal bounds of the array.
1791// Many times structure can be defined as:
1792// struct Collection
1793// {
1794// uint32_t item_count;
1795// Item item_array[0];
1796// };
1797// The size of the "item_array" is 1, but many times in practice
1798// there are more items in "item_array".
1799
1800ValueObjectSP
1801ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1802{
1803 ValueObjectSP synthetic_child_sp;
1804 if (IsArrayType ())
1805 {
1806 char index_str[64];
1807 snprintf(index_str, sizeof(index_str), "[%i]", index);
1808 ConstString index_const_str(index_str);
1809 // Check if we have already created a synthetic array member in this
1810 // valid object. If we have we will re-use it.
1811 synthetic_child_sp = GetSyntheticChild (index_const_str);
1812 if (!synthetic_child_sp)
1813 {
1814 ValueObject *synthetic_child;
1815 // We haven't made a synthetic array member for INDEX yet, so
1816 // lets make one and cache it for any future reference.
1817 synthetic_child = CreateChildAtIndex(0, true, index);
1818
1819 // Cache the value if we got one back...
1820 if (synthetic_child)
1821 {
1822 AddSyntheticChild(index_const_str, synthetic_child);
1823 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001824 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00001825 synthetic_child_sp->m_is_array_item_for_pointer = true;
1826 }
1827 }
1828 }
1829 return synthetic_child_sp;
1830}
1831
Enrico Granata9fc19442011-07-06 02:13:41 +00001832ValueObjectSP
1833ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1834{
1835 ValueObjectSP synthetic_child_sp;
1836 if (IsScalarType ())
1837 {
1838 char index_str[64];
1839 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1840 ConstString index_const_str(index_str);
1841 // Check if we have already created a synthetic array member in this
1842 // valid object. If we have we will re-use it.
1843 synthetic_child_sp = GetSyntheticChild (index_const_str);
1844 if (!synthetic_child_sp)
1845 {
1846 ValueObjectChild *synthetic_child;
1847 // We haven't made a synthetic array member for INDEX yet, so
1848 // lets make one and cache it for any future reference.
1849 synthetic_child = new ValueObjectChild(*this,
1850 GetClangAST(),
1851 GetClangType(),
1852 index_const_str,
1853 GetByteSize(),
1854 0,
1855 to-from+1,
1856 from,
1857 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001858 false,
1859 eAddressTypeInvalid);
Enrico Granata9fc19442011-07-06 02:13:41 +00001860
1861 // Cache the value if we got one back...
1862 if (synthetic_child)
1863 {
1864 AddSyntheticChild(index_const_str, synthetic_child);
1865 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001866 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00001867 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1868 }
1869 }
1870 }
1871 return synthetic_child_sp;
1872}
1873
Greg Claytonafacd142011-09-02 01:15:17 +00001874ValueObjectSP
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001875ValueObject::GetSyntheticArrayRangeChild (uint32_t from, uint32_t to, bool can_create)
1876{
1877 ValueObjectSP synthetic_child_sp;
1878 if (IsArrayType () || IsPointerType ())
1879 {
1880 char index_str[64];
1881 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1882 ConstString index_const_str(index_str);
1883 // Check if we have already created a synthetic array member in this
1884 // valid object. If we have we will re-use it.
1885 synthetic_child_sp = GetSyntheticChild (index_const_str);
1886 if (!synthetic_child_sp)
1887 {
1888 ValueObjectSynthetic *synthetic_child;
1889
1890 // We haven't made a synthetic array member for INDEX yet, so
1891 // lets make one and cache it for any future reference.
Enrico Granata061858c2012-02-15 02:34:21 +00001892 SyntheticArrayView *view = new SyntheticArrayView(SyntheticChildren::Flags());
Enrico Granatad64d0bc2011-08-19 21:13:46 +00001893 view->AddRange(from,to);
1894 SyntheticChildrenSP view_sp(view);
1895 synthetic_child = new ValueObjectSynthetic(*this, view_sp);
1896
1897 // Cache the value if we got one back...
1898 if (synthetic_child)
1899 {
1900 AddSyntheticChild(index_const_str, synthetic_child);
1901 synthetic_child_sp = synthetic_child->GetSP();
1902 synthetic_child_sp->SetName(ConstString(index_str));
1903 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1904 }
1905 }
1906 }
1907 return synthetic_child_sp;
1908}
1909
Greg Claytonafacd142011-09-02 01:15:17 +00001910ValueObjectSP
Enrico Granata6f3533f2011-07-29 19:53:35 +00001911ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1912{
1913
1914 ValueObjectSP synthetic_child_sp;
1915
1916 char name_str[64];
1917 snprintf(name_str, sizeof(name_str), "@%i", offset);
1918 ConstString name_const_str(name_str);
1919
1920 // Check if we have already created a synthetic array member in this
1921 // valid object. If we have we will re-use it.
1922 synthetic_child_sp = GetSyntheticChild (name_const_str);
1923
1924 if (synthetic_child_sp.get())
1925 return synthetic_child_sp;
1926
1927 if (!can_create)
Greg Claytonafacd142011-09-02 01:15:17 +00001928 return ValueObjectSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001929
1930 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1931 type.GetASTContext(),
1932 type.GetOpaqueQualType(),
1933 name_const_str,
1934 type.GetTypeByteSize(),
1935 offset,
1936 0,
1937 0,
1938 false,
Enrico Granata9128ee22011-09-06 19:20:51 +00001939 false,
1940 eAddressTypeInvalid);
Enrico Granata6f3533f2011-07-29 19:53:35 +00001941 if (synthetic_child)
1942 {
1943 AddSyntheticChild(name_const_str, synthetic_child);
1944 synthetic_child_sp = synthetic_child->GetSP();
1945 synthetic_child_sp->SetName(name_const_str);
1946 synthetic_child_sp->m_is_child_at_offset = true;
1947 }
1948 return synthetic_child_sp;
1949}
1950
Enrico Granatad55546b2011-07-22 00:16:08 +00001951// your expression path needs to have a leading . or ->
1952// (unless it somehow "looks like" an array, in which case it has
1953// a leading [ symbol). while the [ is meaningful and should be shown
1954// to the user, . and -> are just parser design, but by no means
1955// added information for the user.. strip them off
1956static const char*
1957SkipLeadingExpressionPathSeparators(const char* expression)
1958{
1959 if (!expression || !expression[0])
1960 return expression;
1961 if (expression[0] == '.')
1962 return expression+1;
1963 if (expression[0] == '-' && expression[1] == '>')
1964 return expression+2;
1965 return expression;
1966}
1967
Greg Claytonafacd142011-09-02 01:15:17 +00001968ValueObjectSP
Enrico Granatad55546b2011-07-22 00:16:08 +00001969ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1970{
1971 ValueObjectSP synthetic_child_sp;
1972 ConstString name_const_string(expression);
1973 // Check if we have already created a synthetic array member in this
1974 // valid object. If we have we will re-use it.
1975 synthetic_child_sp = GetSyntheticChild (name_const_string);
1976 if (!synthetic_child_sp)
1977 {
1978 // We haven't made a synthetic array member for expression yet, so
1979 // lets make one and cache it for any future reference.
1980 synthetic_child_sp = GetValueForExpressionPath(expression);
1981
1982 // Cache the value if we got one back...
1983 if (synthetic_child_sp.get())
1984 {
1985 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00001986 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001987 synthetic_child_sp->m_is_expression_path_child = true;
1988 }
1989 }
1990 return synthetic_child_sp;
1991}
1992
1993void
Enrico Granata86cc9822012-03-19 22:58:49 +00001994ValueObject::CalculateSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00001995{
Enrico Granata86cc9822012-03-19 22:58:49 +00001996 if (use_synthetic == false)
Enrico Granatad55546b2011-07-22 00:16:08 +00001997 return;
1998
Enrico Granata86cc9822012-03-19 22:58:49 +00001999 if (!UpdateFormatsIfNeeded(m_last_format_mgr_dynamic) && m_synthetic_value)
2000 return;
Enrico Granatad55546b2011-07-22 00:16:08 +00002001
Enrico Granata0c489f52012-03-01 04:24:26 +00002002 if (m_synthetic_children_sp.get() == NULL)
Enrico Granatad55546b2011-07-22 00:16:08 +00002003 return;
2004
Enrico Granata86cc9822012-03-19 22:58:49 +00002005 m_synthetic_value = new ValueObjectSynthetic(*this, m_synthetic_children_sp);
Enrico Granatad55546b2011-07-22 00:16:08 +00002006}
2007
Jim Ingham78a685a2011-04-16 00:01:13 +00002008void
Greg Claytonafacd142011-09-02 01:15:17 +00002009ValueObject::CalculateDynamicValue (DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00002010{
Greg Claytonafacd142011-09-02 01:15:17 +00002011 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002012 return;
2013
Jim Ingham58b59f92011-04-22 23:53:53 +00002014 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00002015 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002016 ExecutionContext exe_ctx (GetExecutionContextRef());
2017 Process *process = exe_ctx.GetProcessPtr();
2018 if (process)
Jim Ingham78a685a2011-04-16 00:01:13 +00002019 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002020 bool worth_having_dynamic_value = false;
Jim Ingham78a685a2011-04-16 00:01:13 +00002021
Greg Claytoncc4d0142012-02-17 07:49:44 +00002022
2023 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
2024 // hard code this everywhere.
2025 LanguageType known_type = GetObjectRuntimeLanguage();
2026 if (known_type != eLanguageTypeUnknown && known_type != eLanguageTypeC)
Jim Ingham78a685a2011-04-16 00:01:13 +00002027 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00002028 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
2029 if (runtime)
2030 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00002031 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00002032 else
2033 {
2034 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (eLanguageTypeC_plus_plus);
2035 if (cpp_runtime)
2036 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
2037
2038 if (!worth_having_dynamic_value)
2039 {
2040 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (eLanguageTypeObjC);
2041 if (objc_runtime)
2042 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
2043 }
2044 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002045
Greg Claytoncc4d0142012-02-17 07:49:44 +00002046 if (worth_having_dynamic_value)
2047 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
2048 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002049 }
2050}
2051
Jim Ingham58b59f92011-04-22 23:53:53 +00002052ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00002053ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00002054{
Greg Claytonafacd142011-09-02 01:15:17 +00002055 if (use_dynamic == eNoDynamicValues)
Jim Ingham2837b762011-05-04 03:43:18 +00002056 return ValueObjectSP();
2057
2058 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00002059 {
Jim Ingham2837b762011-05-04 03:43:18 +00002060 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00002061 }
Jim Ingham58b59f92011-04-22 23:53:53 +00002062 if (m_dynamic_value)
2063 return m_dynamic_value->GetSP();
2064 else
2065 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00002066}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002067
Jim Ingham60dbabb2011-12-08 19:44:08 +00002068ValueObjectSP
2069ValueObject::GetStaticValue()
2070{
2071 return GetSP();
2072}
2073
Enrico Granatad55546b2011-07-22 00:16:08 +00002074ValueObjectSP
Enrico Granata86cc9822012-03-19 22:58:49 +00002075ValueObject::GetSyntheticValue (bool use_synthetic)
Enrico Granatad55546b2011-07-22 00:16:08 +00002076{
Enrico Granata86cc9822012-03-19 22:58:49 +00002077 if (use_synthetic == false)
2078 return ValueObjectSP();
2079
Enrico Granatad55546b2011-07-22 00:16:08 +00002080 CalculateSyntheticValue(use_synthetic);
2081
2082 if (m_synthetic_value)
2083 return m_synthetic_value->GetSP();
2084 else
Enrico Granata86cc9822012-03-19 22:58:49 +00002085 return ValueObjectSP();
Enrico Granatad55546b2011-07-22 00:16:08 +00002086}
2087
Greg Claytone221f822011-01-21 01:59:00 +00002088bool
Enrico Granata27b625e2011-08-09 01:04:56 +00002089ValueObject::HasSyntheticValue()
2090{
2091 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
2092
Enrico Granata0c489f52012-03-01 04:24:26 +00002093 if (m_synthetic_children_sp.get() == NULL)
Enrico Granata27b625e2011-08-09 01:04:56 +00002094 return false;
2095
Enrico Granata86cc9822012-03-19 22:58:49 +00002096 CalculateSyntheticValue(true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002097
2098 if (m_synthetic_value)
2099 return true;
2100 else
2101 return false;
2102}
2103
2104bool
Greg Claytone221f822011-01-21 01:59:00 +00002105ValueObject::GetBaseClassPath (Stream &s)
2106{
2107 if (IsBaseClass())
2108 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002109 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00002110 clang_type_t clang_type = GetClangType();
2111 std::string cxx_class_name;
2112 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
2113 if (this_had_base_class)
2114 {
2115 if (parent_had_base_class)
2116 s.PutCString("::");
2117 s.PutCString(cxx_class_name.c_str());
2118 }
2119 return parent_had_base_class || this_had_base_class;
2120 }
2121 return false;
2122}
2123
2124
2125ValueObject *
2126ValueObject::GetNonBaseClassParent()
2127{
Jim Ingham78a685a2011-04-16 00:01:13 +00002128 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00002129 {
Jim Ingham78a685a2011-04-16 00:01:13 +00002130 if (GetParent()->IsBaseClass())
2131 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00002132 else
Jim Ingham78a685a2011-04-16 00:01:13 +00002133 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00002134 }
2135 return NULL;
2136}
Greg Clayton1d3afba2010-10-05 00:00:42 +00002137
2138void
Enrico Granata4becb372011-06-29 22:27:15 +00002139ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002140{
Greg Claytone221f822011-01-21 01:59:00 +00002141 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00002142
Enrico Granata86cc9822012-03-19 22:58:49 +00002143 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002144 {
Enrico Granata4becb372011-06-29 22:27:15 +00002145 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
2146 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
2147 // the eHonorPointers mode is meant to produce strings in this latter format
2148 s.PutCString("*(");
2149 }
Greg Claytone221f822011-01-21 01:59:00 +00002150
Enrico Granata4becb372011-06-29 22:27:15 +00002151 ValueObject* parent = GetParent();
2152
2153 if (parent)
2154 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00002155
2156 // if we are a deref_of_parent just because we are synthetic array
2157 // members made up to allow ptr[%d] syntax to work in variable
2158 // printing, then add our name ([%d]) to the expression path
Enrico Granata86cc9822012-03-19 22:58:49 +00002159 if (m_is_array_item_for_pointer && epformat == eGetExpressionPathFormatHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002160 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00002161
Greg Claytone221f822011-01-21 01:59:00 +00002162 if (!IsBaseClass())
2163 {
2164 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002165 {
Greg Claytone221f822011-01-21 01:59:00 +00002166 ValueObject *non_base_class_parent = GetNonBaseClassParent();
2167 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002168 {
Greg Claytone221f822011-01-21 01:59:00 +00002169 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
2170 if (non_base_class_parent_clang_type)
2171 {
2172 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
2173
Enrico Granata86cc9822012-03-19 22:58:49 +00002174 if (parent && parent->IsDereferenceOfParent() && epformat == eGetExpressionPathFormatHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00002175 {
2176 s.PutCString("->");
2177 }
Enrico Granata4becb372011-06-29 22:27:15 +00002178 else
2179 {
2180 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
2181 {
2182 s.PutCString("->");
2183 }
2184 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
2185 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
2186 {
2187 s.PutChar('.');
2188 }
Greg Claytone221f822011-01-21 01:59:00 +00002189 }
2190 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002191 }
Greg Claytone221f822011-01-21 01:59:00 +00002192
2193 const char *name = GetName().GetCString();
2194 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002195 {
Greg Claytone221f822011-01-21 01:59:00 +00002196 if (qualify_cxx_base_classes)
2197 {
2198 if (GetBaseClassPath (s))
2199 s.PutCString("::");
2200 }
2201 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002202 }
2203 }
2204 }
2205
Enrico Granata86cc9822012-03-19 22:58:49 +00002206 if (is_deref_of_parent && epformat == eGetExpressionPathFormatDereferencePointers)
Enrico Granata85933ed2011-08-18 16:38:26 +00002207 {
Greg Claytone221f822011-01-21 01:59:00 +00002208 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00002209 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002210}
2211
Greg Claytonafacd142011-09-02 01:15:17 +00002212ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002213ValueObject::GetValueForExpressionPath(const char* expression,
2214 const char** first_unparsed,
2215 ExpressionPathScanEndReason* reason_to_stop,
2216 ExpressionPathEndResultType* final_value_type,
2217 const GetValueForExpressionPathOptions& options,
2218 ExpressionPathAftermath* final_task_on_target)
2219{
2220
2221 const char* dummy_first_unparsed;
2222 ExpressionPathScanEndReason dummy_reason_to_stop;
2223 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002224 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002225
2226 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2227 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2228 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2229 final_value_type ? final_value_type : &dummy_final_value_type,
2230 options,
2231 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2232
Enrico Granata86cc9822012-03-19 22:58:49 +00002233 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002234 return ret_val;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002235
Enrico Granata86cc9822012-03-19 22:58:49 +00002236 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 +00002237 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002238 if ( (final_task_on_target ? *final_task_on_target : dummy_final_task_on_target) == ValueObject::eExpressionPathAftermathDereference)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002239 {
2240 Error error;
2241 ValueObjectSP final_value = ret_val->Dereference(error);
2242 if (error.Fail() || !final_value.get())
2243 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002244 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002245 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002246 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002247 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002248 return ValueObjectSP();
2249 }
2250 else
2251 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002252 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002253 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002254 return final_value;
2255 }
2256 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002257 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002258 {
2259 Error error;
2260 ValueObjectSP final_value = ret_val->AddressOf(error);
2261 if (error.Fail() || !final_value.get())
2262 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002263 if (reason_to_stop)
Enrico Granata86cc9822012-03-19 22:58:49 +00002264 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
Enrico Granata385ad4e2012-03-03 00:45:57 +00002265 if (final_value_type)
Enrico Granata86cc9822012-03-19 22:58:49 +00002266 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002267 return ValueObjectSP();
2268 }
2269 else
2270 {
Enrico Granata385ad4e2012-03-03 00:45:57 +00002271 if (final_task_on_target)
Enrico Granata86cc9822012-03-19 22:58:49 +00002272 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002273 return final_value;
2274 }
2275 }
2276 }
2277 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
2278}
2279
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002280int
2281ValueObject::GetValuesForExpressionPath(const char* expression,
Greg Claytonafacd142011-09-02 01:15:17 +00002282 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002283 const char** first_unparsed,
2284 ExpressionPathScanEndReason* reason_to_stop,
2285 ExpressionPathEndResultType* final_value_type,
2286 const GetValueForExpressionPathOptions& options,
2287 ExpressionPathAftermath* final_task_on_target)
2288{
2289 const char* dummy_first_unparsed;
2290 ExpressionPathScanEndReason dummy_reason_to_stop;
2291 ExpressionPathEndResultType dummy_final_value_type;
Enrico Granata86cc9822012-03-19 22:58:49 +00002292 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002293
2294 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
2295 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2296 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2297 final_value_type ? final_value_type : &dummy_final_value_type,
2298 options,
2299 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2300
2301 if (!ret_val.get()) // if there are errors, I add nothing to the list
2302 return 0;
2303
Enrico Granata86cc9822012-03-19 22:58:49 +00002304 if (*reason_to_stop != eExpressionPathScanEndReasonArrayRangeOperatorMet)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002305 {
2306 // I need not expand a range, just post-process the final value and return
Enrico Granata86cc9822012-03-19 22:58:49 +00002307 if (!final_task_on_target || *final_task_on_target == ValueObject::eExpressionPathAftermathNothing)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002308 {
2309 list->Append(ret_val);
2310 return 1;
2311 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002312 if (ret_val.get() && *final_value_type == eExpressionPathEndResultTypePlain) // I can only deref and takeaddress of plain objects
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002313 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002314 if (*final_task_on_target == ValueObject::eExpressionPathAftermathDereference)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002315 {
2316 Error error;
2317 ValueObjectSP final_value = ret_val->Dereference(error);
2318 if (error.Fail() || !final_value.get())
2319 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002320 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2321 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002322 return 0;
2323 }
2324 else
2325 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002326 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002327 list->Append(final_value);
2328 return 1;
2329 }
2330 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002331 if (*final_task_on_target == ValueObject::eExpressionPathAftermathTakeAddress)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002332 {
2333 Error error;
2334 ValueObjectSP final_value = ret_val->AddressOf(error);
2335 if (error.Fail() || !final_value.get())
2336 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002337 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonTakingAddressFailed;
2338 *final_value_type = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002339 return 0;
2340 }
2341 else
2342 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002343 *final_task_on_target = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002344 list->Append(final_value);
2345 return 1;
2346 }
2347 }
2348 }
2349 }
2350 else
2351 {
2352 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
2353 first_unparsed ? first_unparsed : &dummy_first_unparsed,
2354 ret_val,
2355 list,
2356 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
2357 final_value_type ? final_value_type : &dummy_final_value_type,
2358 options,
2359 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
2360 }
2361 // in any non-covered case, just do the obviously right thing
2362 list->Append(ret_val);
2363 return 1;
2364}
2365
Greg Claytonafacd142011-09-02 01:15:17 +00002366ValueObjectSP
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002367ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
2368 const char** first_unparsed,
2369 ExpressionPathScanEndReason* reason_to_stop,
2370 ExpressionPathEndResultType* final_result,
2371 const GetValueForExpressionPathOptions& options,
2372 ExpressionPathAftermath* what_next)
2373{
2374 ValueObjectSP root = GetSP();
2375
2376 if (!root.get())
2377 return ValueObjectSP();
2378
2379 *first_unparsed = expression_cstr;
2380
2381 while (true)
2382 {
2383
2384 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2385
Greg Claytonafacd142011-09-02 01:15:17 +00002386 clang_type_t root_clang_type = root->GetClangType();
2387 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002388 Flags root_clang_type_info,pointee_clang_type_info;
2389
2390 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2391 if (pointee_clang_type)
2392 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002393
2394 if (!expression_cstr || *expression_cstr == '\0')
2395 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002396 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002397 return root;
2398 }
2399
2400 switch (*expression_cstr)
2401 {
2402 case '-':
2403 {
2404 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002405 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 +00002406 {
2407 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002408 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrowInsteadOfDot;
2409 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002410 return ValueObjectSP();
2411 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002412 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2413 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002414 options.m_no_fragile_ivar)
2415 {
2416 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002417 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonFragileIVarNotAllowed;
2418 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002419 return ValueObjectSP();
2420 }
2421 if (expression_cstr[1] != '>')
2422 {
2423 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002424 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2425 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002426 return ValueObjectSP();
2427 }
2428 expression_cstr++; // skip the -
2429 }
2430 case '.': // or fallthrough from ->
2431 {
2432 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002433 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 +00002434 {
2435 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002436 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDotInsteadOfArrow;
2437 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002438 return ValueObjectSP();
2439 }
2440 expression_cstr++; // skip .
2441 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2442 ConstString child_name;
2443 if (!next_separator) // if no other separator just expand this last layer
2444 {
2445 child_name.SetCString (expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002446 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2447
2448 if (child_valobj_sp.get()) // we know we are done, so just return
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002449 {
2450 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002451 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2452 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002453 return child_valobj_sp;
2454 }
2455 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2456 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002457 if (root->IsSynthetic())
2458 child_valobj_sp = root;
2459 else
2460 child_valobj_sp = root->GetSyntheticValue();
2461
2462 if (child_valobj_sp.get())
2463 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002464 }
2465
2466 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2467 // so we hit the "else" branch, and return an error
2468 if(child_valobj_sp.get()) // if it worked, just return
2469 {
2470 *first_unparsed = '\0';
Enrico Granata86cc9822012-03-19 22:58:49 +00002471 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
2472 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002473 return child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002474 }
2475 else
2476 {
2477 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002478 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2479 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002480 return ValueObjectSP();
2481 }
2482 }
2483 else // other layers do expand
2484 {
2485 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002486 ValueObjectSP child_valobj_sp = root->GetChildMemberWithName(child_name, true);
2487 if (child_valobj_sp.get()) // store the new root and move on
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002488 {
Enrico Granata8c9d3562011-08-11 17:08:01 +00002489 root = child_valobj_sp;
2490 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002491 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002492 continue;
2493 }
2494 else if (options.m_no_synthetic_children == false) // let's try with synthetic children
2495 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002496 child_valobj_sp = root->GetSyntheticValue(true);
2497 if (child_valobj_sp)
2498 child_valobj_sp = child_valobj_sp->GetChildMemberWithName(child_name, true);
Enrico Granata8c9d3562011-08-11 17:08:01 +00002499 }
2500
2501 // if we are here and options.m_no_synthetic_children is true, child_valobj_sp is going to be a NULL SP,
2502 // so we hit the "else" branch, and return an error
2503 if(child_valobj_sp.get()) // if it worked, move on
2504 {
2505 root = child_valobj_sp;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002506 *first_unparsed = next_separator;
Enrico Granata86cc9822012-03-19 22:58:49 +00002507 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002508 continue;
2509 }
2510 else
2511 {
2512 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002513 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2514 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002515 return ValueObjectSP();
2516 }
2517 }
2518 break;
2519 }
2520 case '[':
2521 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002522 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 +00002523 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002524 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002525 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002526 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2527 {
2528 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002529 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2530 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002531 return ValueObjectSP();
2532 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002533 }
2534 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2535 {
2536 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002537 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2538 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002539 return ValueObjectSP();
2540 }
2541 }
2542 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2543 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002544 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002545 {
2546 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002547 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2548 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002549 return ValueObjectSP();
2550 }
2551 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2552 {
2553 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002554 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2555 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002556 return root;
2557 }
2558 }
2559 const char *separator_position = ::strchr(expression_cstr+1,'-');
2560 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2561 if (!close_bracket_position) // if there is no ], this is a syntax error
2562 {
2563 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002564 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2565 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002566 return ValueObjectSP();
2567 }
2568 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2569 {
2570 char *end = NULL;
2571 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2572 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2573 {
2574 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002575 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2576 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002577 return ValueObjectSP();
2578 }
2579 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2580 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002581 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002582 {
2583 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002584 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2585 *final_result = ValueObject::eExpressionPathEndResultTypeUnboundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002586 return root;
2587 }
2588 else
2589 {
2590 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002591 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2592 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002593 return ValueObjectSP();
2594 }
2595 }
2596 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002597 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002598 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002599 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2600 if (!child_valobj_sp)
2601 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002602 if (!child_valobj_sp)
Enrico Granata86cc9822012-03-19 22:58:49 +00002603 if (root->HasSyntheticValue() && root->GetSyntheticValue()->GetNumChildren() > index)
2604 child_valobj_sp = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002605 if (child_valobj_sp)
2606 {
2607 root = child_valobj_sp;
2608 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002609 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Greg Claytondaf515f2011-07-09 20:12:33 +00002610 continue;
2611 }
2612 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002613 {
2614 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002615 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2616 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002617 return ValueObjectSP();
2618 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002619 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002620 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002621 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002622 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 +00002623 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002624 {
2625 Error error;
2626 root = root->Dereference(error);
2627 if (error.Fail() || !root.get())
2628 {
2629 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002630 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2631 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002632 return ValueObjectSP();
2633 }
2634 else
2635 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002636 *what_next = eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002637 continue;
2638 }
2639 }
2640 else
2641 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002642 if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
Greg Claytonafacd142011-09-02 01:15:17 +00002643 root->GetClangType()) == eLanguageTypeObjC
Enrico Granata27b625e2011-08-09 01:04:56 +00002644 &&
2645 ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2646 &&
2647 root->HasSyntheticValue()
2648 &&
2649 options.m_no_synthetic_children == false)
2650 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002651 root = root->GetSyntheticValue()->GetChildAtIndex(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002652 }
2653 else
2654 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002655 if (!root.get())
2656 {
2657 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002658 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2659 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002660 return ValueObjectSP();
2661 }
2662 else
2663 {
2664 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002665 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002666 continue;
2667 }
2668 }
2669 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002670 else if (ClangASTContext::IsScalarType(root_clang_type))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002671 {
2672 root = root->GetSyntheticBitFieldChild(index, index, true);
2673 if (!root.get())
2674 {
2675 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002676 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2677 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002678 return ValueObjectSP();
2679 }
2680 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2681 {
2682 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002683 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2684 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002685 return root;
2686 }
2687 }
Enrico Granata86cc9822012-03-19 22:58:49 +00002688 else if (options.m_no_synthetic_children == false)
Enrico Granata27b625e2011-08-09 01:04:56 +00002689 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002690 if (root->HasSyntheticValue())
2691 root = root->GetSyntheticValue();
2692 else if (!root->IsSynthetic())
2693 {
2694 *first_unparsed = expression_cstr;
2695 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2696 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2697 return ValueObjectSP();
2698 }
2699 // if we are here, then root itself is a synthetic VO.. should be good to go
2700
Enrico Granata27b625e2011-08-09 01:04:56 +00002701 if (!root.get())
2702 {
2703 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002704 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonSyntheticValueMissing;
2705 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
2706 return ValueObjectSP();
2707 }
2708 root = root->GetChildAtIndex(index, true);
2709 if (!root.get())
2710 {
2711 *first_unparsed = expression_cstr;
2712 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2713 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002714 return ValueObjectSP();
2715 }
Enrico Granata8c9d3562011-08-11 17:08:01 +00002716 else
2717 {
2718 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002719 *final_result = ValueObject::eExpressionPathEndResultTypePlain;
Enrico Granata8c9d3562011-08-11 17:08:01 +00002720 continue;
2721 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002722 }
2723 else
2724 {
2725 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002726 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2727 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granata27b625e2011-08-09 01:04:56 +00002728 return ValueObjectSP();
2729 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002730 }
2731 else // we have a low and a high index
2732 {
2733 char *end = NULL;
2734 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2735 if (!end || end != separator_position) // if something weird is in our way return an error
2736 {
2737 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002738 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2739 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002740 return ValueObjectSP();
2741 }
2742 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2743 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2744 {
2745 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002746 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2747 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002748 return ValueObjectSP();
2749 }
2750 if (index_lower > index_higher) // swap indices if required
2751 {
2752 unsigned long temp = index_lower;
2753 index_lower = index_higher;
2754 index_higher = temp;
2755 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002756 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002757 {
2758 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2759 if (!root.get())
2760 {
2761 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002762 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2763 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002764 return ValueObjectSP();
2765 }
2766 else
2767 {
2768 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002769 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonBitfieldRangeOperatorMet;
2770 *final_result = ValueObject::eExpressionPathEndResultTypeBitfield;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002771 return root;
2772 }
2773 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002774 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 +00002775 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002776 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002777 {
2778 Error error;
2779 root = root->Dereference(error);
2780 if (error.Fail() || !root.get())
2781 {
2782 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002783 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2784 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002785 return ValueObjectSP();
2786 }
2787 else
2788 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002789 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002790 continue;
2791 }
2792 }
2793 else
2794 {
2795 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002796 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonArrayRangeOperatorMet;
2797 *final_result = ValueObject::eExpressionPathEndResultTypeBoundedRange;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002798 return root;
2799 }
2800 }
2801 break;
2802 }
2803 default: // some non-separator is in the way
2804 {
2805 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002806 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2807 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002808 return ValueObjectSP();
2809 break;
2810 }
2811 }
2812 }
2813}
2814
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002815int
2816ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2817 const char** first_unparsed,
Greg Claytonafacd142011-09-02 01:15:17 +00002818 ValueObjectSP root,
2819 ValueObjectListSP& list,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002820 ExpressionPathScanEndReason* reason_to_stop,
2821 ExpressionPathEndResultType* final_result,
2822 const GetValueForExpressionPathOptions& options,
2823 ExpressionPathAftermath* what_next)
2824{
2825 if (!root.get())
2826 return 0;
2827
2828 *first_unparsed = expression_cstr;
2829
2830 while (true)
2831 {
2832
2833 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2834
Greg Claytonafacd142011-09-02 01:15:17 +00002835 clang_type_t root_clang_type = root->GetClangType();
2836 clang_type_t pointee_clang_type;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002837 Flags root_clang_type_info,pointee_clang_type_info;
2838
2839 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2840 if (pointee_clang_type)
2841 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2842
2843 if (!expression_cstr || *expression_cstr == '\0')
2844 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002845 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEndOfString;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002846 list->Append(root);
2847 return 1;
2848 }
2849
2850 switch (*expression_cstr)
2851 {
2852 case '[':
2853 {
2854 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2855 {
2856 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2857 {
2858 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002859 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorInvalid;
2860 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002861 return 0;
2862 }
2863 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2864 {
2865 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002866 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorNotAllowed;
2867 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002868 return 0;
2869 }
2870 }
2871 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2872 {
2873 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2874 {
2875 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002876 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2877 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002878 return 0;
2879 }
2880 else // expand this into list
2881 {
2882 int max_index = root->GetNumChildren() - 1;
2883 for (int index = 0; index < max_index; index++)
2884 {
2885 ValueObjectSP child =
2886 root->GetChildAtIndex(index, true);
2887 list->Append(child);
2888 }
2889 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002890 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2891 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002892 return max_index; // tell me number of items I added to the VOList
2893 }
2894 }
2895 const char *separator_position = ::strchr(expression_cstr+1,'-');
2896 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2897 if (!close_bracket_position) // if there is no ], this is a syntax error
2898 {
2899 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002900 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2901 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002902 return 0;
2903 }
2904 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2905 {
2906 char *end = NULL;
2907 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2908 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2909 {
2910 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002911 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
2912 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002913 return 0;
2914 }
2915 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2916 {
2917 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2918 {
2919 int max_index = root->GetNumChildren() - 1;
2920 for (int index = 0; index < max_index; index++)
2921 {
2922 ValueObjectSP child =
2923 root->GetChildAtIndex(index, true);
2924 list->Append(child);
2925 }
2926 *first_unparsed = expression_cstr+2;
Enrico Granata86cc9822012-03-19 22:58:49 +00002927 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2928 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002929 return max_index; // tell me number of items I added to the VOList
2930 }
2931 else
2932 {
2933 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002934 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonEmptyRangeNotAllowed;
2935 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002936 return 0;
2937 }
2938 }
2939 // from here on we do have a valid index
2940 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2941 {
2942 root = root->GetChildAtIndex(index, true);
2943 if (!root.get())
2944 {
2945 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002946 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2947 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002948 return 0;
2949 }
2950 else
2951 {
2952 list->Append(root);
2953 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002954 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2955 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002956 return 1;
2957 }
2958 }
2959 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2960 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002961 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 +00002962 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2963 {
2964 Error error;
2965 root = root->Dereference(error);
2966 if (error.Fail() || !root.get())
2967 {
2968 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002969 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
2970 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002971 return 0;
2972 }
2973 else
2974 {
Enrico Granata86cc9822012-03-19 22:58:49 +00002975 *what_next = eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002976 continue;
2977 }
2978 }
2979 else
2980 {
2981 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2982 if (!root.get())
2983 {
2984 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00002985 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
2986 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002987 return 0;
2988 }
2989 else
2990 {
2991 list->Append(root);
2992 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00002993 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
2994 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002995 return 1;
2996 }
2997 }
2998 }
2999 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
3000 {
3001 root = root->GetSyntheticBitFieldChild(index, index, true);
3002 if (!root.get())
3003 {
3004 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003005 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3006 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003007 return 0;
3008 }
3009 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
3010 {
3011 list->Append(root);
3012 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003013 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3014 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003015 return 1;
3016 }
3017 }
3018 }
3019 else // we have a low and a high index
3020 {
3021 char *end = NULL;
3022 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
3023 if (!end || end != separator_position) // if something weird is in our way return an error
3024 {
3025 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003026 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3027 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003028 return 0;
3029 }
3030 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
3031 if (!end || end != close_bracket_position) // if something weird is in our way return an error
3032 {
3033 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003034 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3035 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003036 return 0;
3037 }
3038 if (index_lower > index_higher) // swap indices if required
3039 {
3040 unsigned long temp = index_lower;
3041 index_lower = index_higher;
3042 index_higher = temp;
3043 }
3044 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
3045 {
3046 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
3047 if (!root.get())
3048 {
3049 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003050 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonNoSuchChild;
3051 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003052 return 0;
3053 }
3054 else
3055 {
3056 list->Append(root);
3057 *first_unparsed = end+1; // skip ]
Enrico Granata86cc9822012-03-19 22:58:49 +00003058 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3059 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003060 return 1;
3061 }
3062 }
3063 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 +00003064 *what_next == ValueObject::eExpressionPathAftermathDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003065 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
3066 {
3067 Error error;
3068 root = root->Dereference(error);
3069 if (error.Fail() || !root.get())
3070 {
3071 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003072 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonDereferencingFailed;
3073 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003074 return 0;
3075 }
3076 else
3077 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003078 *what_next = ValueObject::eExpressionPathAftermathNothing;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003079 continue;
3080 }
3081 }
3082 else
3083 {
Johnny Chen44805302011-07-19 19:48:13 +00003084 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003085 index <= index_higher; index++)
3086 {
3087 ValueObjectSP child =
3088 root->GetChildAtIndex(index, true);
3089 list->Append(child);
3090 }
3091 *first_unparsed = end+1;
Enrico Granata86cc9822012-03-19 22:58:49 +00003092 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonRangeOperatorExpanded;
3093 *final_result = ValueObject::eExpressionPathEndResultTypeValueObjectList;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003094 return index_higher-index_lower+1; // tell me number of items I added to the VOList
3095 }
3096 }
3097 break;
3098 }
3099 default: // some non-[ separator, or something entirely wrong, is in the way
3100 {
3101 *first_unparsed = expression_cstr;
Enrico Granata86cc9822012-03-19 22:58:49 +00003102 *reason_to_stop = ValueObject::eExpressionPathScanEndReasonUnexpectedSymbol;
3103 *final_result = ValueObject::eExpressionPathEndResultTypeInvalid;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00003104 return 0;
3105 break;
3106 }
3107 }
3108 }
3109}
3110
Enrico Granata0c489f52012-03-01 04:24:26 +00003111static void
3112DumpValueObject_Impl (Stream &s,
3113 ValueObject *valobj,
3114 const ValueObject::DumpValueObjectOptions& options,
3115 uint32_t ptr_depth,
3116 uint32_t curr_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003117{
Greg Clayton007d5be2011-05-30 00:49:24 +00003118 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003119 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003120 bool update_success = valobj->UpdateValueIfNeeded (options.m_use_dynamic, true);
Greg Clayton007d5be2011-05-30 00:49:24 +00003121
Enrico Granata0c489f52012-03-01 04:24:26 +00003122 const char *root_valobj_name =
3123 options.m_root_valobj_name.empty() ?
3124 valobj->GetName().AsCString() :
3125 options.m_root_valobj_name.c_str();
3126
3127 if (update_success && options.m_use_dynamic != eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00003128 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003129 ValueObject *dynamic_value = valobj->GetDynamicValue(options.m_use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00003130 if (dynamic_value)
3131 valobj = dynamic_value;
3132 }
3133
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003134 clang_type_t clang_type = valobj->GetClangType();
3135
Greg Clayton73b472d2010-10-27 03:32:59 +00003136 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003137 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00003138 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
3139 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003140
Enrico Granata0c489f52012-03-01 04:24:26 +00003141 const bool print_valobj = options.m_flat_output == false || has_value;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003142
3143 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003144 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003145 if (options.m_show_location)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003146 {
Jim Ingham6035b672011-03-31 00:19:25 +00003147 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003148 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003149
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003150 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003151
Greg Clayton7c8a9662010-11-02 01:50:16 +00003152 // Always show the type for the top level items.
Enrico Granata0c489f52012-03-01 04:24:26 +00003153 if (options.m_show_types || (curr_depth == 0 && !options.m_flat_output))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003154 {
Enrico Granata9910bc82011-08-03 02:18:51 +00003155 const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
3156 s.Printf("(%s", typeName);
3157 // only show dynamic types if the user really wants to see types
Enrico Granata0c489f52012-03-01 04:24:26 +00003158 if (options.m_show_types && options.m_use_dynamic != eNoDynamicValues &&
Enrico Granata9910bc82011-08-03 02:18:51 +00003159 (/*strstr(typeName, "id") == typeName ||*/
Greg Claytonafacd142011-09-02 01:15:17 +00003160 ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == eLanguageTypeObjC))
Enrico Granatac3e320a2011-08-02 17:27:39 +00003161 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003162 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3163 Process *process = exe_ctx.GetProcessPtr();
Enrico Granatac3e320a2011-08-02 17:27:39 +00003164 if (process == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003165 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003166 else
3167 {
3168 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
3169 if (runtime == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00003170 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003171 else
3172 {
3173 ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
3174 if (!runtime->IsValidISA(isa))
Enrico Granata9910bc82011-08-03 02:18:51 +00003175 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00003176 else
3177 s.Printf(", dynamic type: %s) ",
3178 runtime->GetActualTypeName(isa).GetCString());
3179 }
3180 }
3181 }
3182 else
3183 s.Printf(") ");
3184 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003185
Greg Clayton1d3afba2010-10-05 00:00:42 +00003186
Enrico Granata0c489f52012-03-01 04:24:26 +00003187 if (options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003188 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00003189 // If we are showing types, also qualify the C++ base classes
Enrico Granata0c489f52012-03-01 04:24:26 +00003190 const bool qualify_cxx_base_classes = options.m_show_types;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003191 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003192 s.PutCString(" =");
3193 }
3194 else
3195 {
3196 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
3197 s.Printf ("%s =", name_cstr);
3198 }
3199
Enrico Granata0c489f52012-03-01 04:24:26 +00003200 if (!options.m_scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003201 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003202 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003203 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003204 }
3205
Enrico Granata0c489f52012-03-01 04:24:26 +00003206 std::string summary_str;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003207 std::string value_str;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003208 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00003209 const char *sum_cstr = NULL;
Enrico Granata0c489f52012-03-01 04:24:26 +00003210 TypeSummaryImpl* entry = options.m_summary_sp ? options.m_summary_sp.get() : valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003211
Enrico Granata0c489f52012-03-01 04:24:26 +00003212 if (options.m_omit_summary_depth > 0)
Enrico Granata0c5ef692011-07-16 01:22:04 +00003213 entry = NULL;
3214
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003215 if (err_cstr == NULL)
3216 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003217 if (options.m_format != eFormatDefault && options.m_format != valobj->GetFormat())
Greg Clayton6efba4f2012-01-26 21:08:30 +00003218 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003219 valobj->GetValueAsCString(options.m_format,
3220 value_str);
Greg Clayton6efba4f2012-01-26 21:08:30 +00003221 }
Enrico Granata0c489f52012-03-01 04:24:26 +00003222 else
Greg Clayton6efba4f2012-01-26 21:08:30 +00003223 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003224 val_cstr = valobj->GetValueAsCString();
3225 if (val_cstr)
3226 value_str = val_cstr;
Greg Clayton6efba4f2012-01-26 21:08:30 +00003227 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003228 err_cstr = valobj->GetError().AsCString();
3229 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003230
3231 if (err_cstr)
3232 {
Greg Clayton007d5be2011-05-30 00:49:24 +00003233 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00003234 }
3235 else
3236 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003237 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003238 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003239 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003240 if (options.m_omit_summary_depth == 0)
3241 {
3242 if (options.m_summary_sp)
3243 {
3244 valobj->GetSummaryAsCString(entry, summary_str);
3245 sum_cstr = summary_str.c_str();
3246 }
3247 else
3248 sum_cstr = valobj->GetSummaryAsCString();
3249 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003250
Greg Clayton6efba4f2012-01-26 21:08:30 +00003251 // Make sure we have a value and make sure the summary didn't
3252 // specify that the value should not be printed
3253 if (!value_str.empty() && (entry == NULL || entry->DoesPrintValue() || sum_cstr == NULL))
3254 s.Printf(" %s", value_str.c_str());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003255
Enrico Granata9dd75c82011-07-15 23:30:15 +00003256 if (sum_cstr)
Enrico Granata0c489f52012-03-01 04:24:26 +00003257 s.Printf(" %s", sum_cstr);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003258
Enrico Granata0c489f52012-03-01 04:24:26 +00003259 if (options.m_use_objc)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003260 {
Jim Ingham6035b672011-03-31 00:19:25 +00003261 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003262 if (object_desc)
3263 s.Printf(" %s\n", object_desc);
3264 else
Sean Callanan672ad942010-10-23 00:18:49 +00003265 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003266 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00003267 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003268 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003269
Enrico Granata0c489f52012-03-01 04:24:26 +00003270 if (curr_depth < options.m_max_depth)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003271 {
Greg Clayton73b472d2010-10-27 03:32:59 +00003272 // We will show children for all concrete types. We won't show
3273 // pointer contents unless a pointer depth has been specified.
3274 // We won't reference contents unless the reference is the
3275 // root object (depth of zero).
3276 bool print_children = true;
3277
3278 // Use a new temporary pointer depth in case we override the
3279 // current pointer depth below...
3280 uint32_t curr_ptr_depth = ptr_depth;
3281
3282 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
3283 if (is_ptr || is_ref)
3284 {
3285 // We have a pointer or reference whose value is an address.
3286 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00003287 AddressType ptr_address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003288 if (valobj->GetPointerValue (&ptr_address_type) == 0)
Greg Clayton73b472d2010-10-27 03:32:59 +00003289 print_children = false;
3290
3291 else if (is_ref && curr_depth == 0)
3292 {
3293 // If this is the root object (depth is zero) that we are showing
3294 // and it is a reference, and no pointer depth has been supplied
3295 // print out what it references. Don't do this at deeper depths
3296 // otherwise we can end up with infinite recursion...
3297 curr_ptr_depth = 1;
3298 }
3299
3300 if (curr_ptr_depth == 0)
3301 print_children = false;
3302 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00003303
Enrico Granata0a3958e2011-07-02 00:25:22 +00003304 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00003305 {
Enrico Granata86cc9822012-03-19 22:58:49 +00003306 ValueObject* synth_valobj;
3307 ValueObjectSP synth_valobj_sp = valobj->GetSyntheticValue (options.m_use_synthetic);
3308 synth_valobj = (synth_valobj_sp ? synth_valobj_sp.get() : valobj);
Enrico Granatac482a192011-08-17 22:13:59 +00003309 uint32_t num_children = synth_valobj->GetNumChildren();
Enrico Granata22c55d12011-08-12 02:00:06 +00003310 bool print_dotdotdot = false;
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003311 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003312 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003313 if (options.m_flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003314 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003315 if (print_valobj)
3316 s.EOL();
3317 }
3318 else
3319 {
3320 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00003321 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003322 s.IndentMore();
3323 }
Enrico Granata22c55d12011-08-12 02:00:06 +00003324
Greg Claytoncc4d0142012-02-17 07:49:44 +00003325 uint32_t max_num_children = valobj->GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
Enrico Granata22c55d12011-08-12 02:00:06 +00003326
Enrico Granata0c489f52012-03-01 04:24:26 +00003327 if (num_children > max_num_children && !options.m_ignore_cap)
Enrico Granata22c55d12011-08-12 02:00:06 +00003328 {
3329 num_children = max_num_children;
3330 print_dotdotdot = true;
3331 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003332
Enrico Granata0c489f52012-03-01 04:24:26 +00003333 ValueObject::DumpValueObjectOptions child_options(options);
3334 child_options.SetFormat().SetSummary().SetRootValueObjectName();
3335 child_options.SetScopeChecked(true)
3336 .SetOmitSummaryDepth(child_options.m_omit_summary_depth > 1 ? child_options.m_omit_summary_depth - 1 : 0);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003337 for (uint32_t idx=0; idx<num_children; ++idx)
3338 {
Enrico Granatac482a192011-08-17 22:13:59 +00003339 ValueObjectSP child_sp(synth_valobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003340 if (child_sp.get())
3341 {
Enrico Granata0c489f52012-03-01 04:24:26 +00003342 DumpValueObject_Impl (s,
3343 child_sp.get(),
3344 child_options,
3345 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
3346 curr_depth + 1);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003347 }
3348 }
3349
Enrico Granata0c489f52012-03-01 04:24:26 +00003350 if (!options.m_flat_output)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003351 {
Enrico Granata22c55d12011-08-12 02:00:06 +00003352 if (print_dotdotdot)
Enrico Granata61a80ba2011-08-12 16:42:31 +00003353 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003354 ExecutionContext exe_ctx (valobj->GetExecutionContextRef());
3355 Target *target = exe_ctx.GetTargetPtr();
3356 if (target)
3357 target->GetDebugger().GetCommandInterpreter().ChildrenTruncated();
Enrico Granata22c55d12011-08-12 02:00:06 +00003358 s.Indent("...\n");
Enrico Granata61a80ba2011-08-12 16:42:31 +00003359 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003360 s.IndentLess();
3361 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003362 }
3363 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003364 else if (has_children)
3365 {
3366 // Aggregate, no children...
3367 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00003368 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003369 }
3370 else
3371 {
3372 if (print_valobj)
3373 s.EOL();
3374 }
3375
Greg Clayton1d3afba2010-10-05 00:00:42 +00003376 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003377 else
3378 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00003379 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00003380 }
3381 }
3382 else
3383 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003384 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00003385 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00003386 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00003387 }
3388 }
3389 }
3390 }
3391}
3392
Enrico Granata0c489f52012-03-01 04:24:26 +00003393void
3394ValueObject::DumpValueObject (Stream &s,
3395 ValueObject *valobj)
3396{
3397
3398 if (!valobj)
3399 return;
3400
3401 DumpValueObject_Impl(s,
3402 valobj,
3403 DumpValueObjectOptions::DefaultOptions(),
3404 0,
3405 0);
3406}
3407
3408void
3409ValueObject::DumpValueObject (Stream &s,
3410 ValueObject *valobj,
3411 const DumpValueObjectOptions& options)
3412{
3413 DumpValueObject_Impl(s,
3414 valobj,
3415 options,
3416 options.m_max_ptr_depth, // max pointer depth allowed, we will go down from here
3417 0 // current object depth is 0 since we are just starting
3418 );
3419}
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003420
3421ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00003422ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003423{
3424 ValueObjectSP valobj_sp;
3425
Enrico Granatac3e320a2011-08-02 17:27:39 +00003426 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003427 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003428 ExecutionContext exe_ctx (GetExecutionContextRef());
3429 clang::ASTContext *ast = GetClangAST ();
3430
3431 DataExtractor data;
3432 data.SetByteOrder (m_data.GetByteOrder());
3433 data.SetAddressByteSize(m_data.GetAddressByteSize());
3434
Greg Claytone72dfb32012-02-24 01:59:29 +00003435 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule().get());
Greg Claytoncc4d0142012-02-17 07:49:44 +00003436
3437 valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
3438 ast,
3439 GetClangType(),
3440 name,
3441 data,
3442 GetAddressOf());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003443 }
Jim Ingham6035b672011-03-31 00:19:25 +00003444
3445 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003446 {
Jim Ingham58b59f92011-04-22 23:53:53 +00003447 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003448 }
3449 return valobj_sp;
3450}
3451
Greg Claytonafacd142011-09-02 01:15:17 +00003452ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00003453ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003454{
Jim Ingham58b59f92011-04-22 23:53:53 +00003455 if (m_deref_valobj)
3456 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00003457
Greg Clayton54979cd2010-12-15 05:08:08 +00003458 const bool is_pointer_type = IsPointerType();
3459 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003460 {
3461 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00003462 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003463
3464 std::string child_name_str;
3465 uint32_t child_byte_size = 0;
3466 int32_t child_byte_offset = 0;
3467 uint32_t child_bitfield_bit_size = 0;
3468 uint32_t child_bitfield_bit_offset = 0;
3469 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00003470 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003471 const bool transparent_pointers = false;
3472 clang::ASTContext *clang_ast = GetClangAST();
3473 clang_type_t clang_type = GetClangType();
3474 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00003475
Greg Claytoncc4d0142012-02-17 07:49:44 +00003476 ExecutionContext exe_ctx (GetExecutionContextRef());
Jim Inghamd555bac2011-06-24 22:03:24 +00003477
3478 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
3479 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003480 GetName().GetCString(),
3481 clang_type,
3482 0,
3483 transparent_pointers,
3484 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00003485 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003486 child_name_str,
3487 child_byte_size,
3488 child_byte_offset,
3489 child_bitfield_bit_size,
3490 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00003491 child_is_base_class,
3492 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00003493 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003494 {
3495 ConstString child_name;
3496 if (!child_name_str.empty())
3497 child_name.SetCString (child_name_str.c_str());
3498
Jim Ingham58b59f92011-04-22 23:53:53 +00003499 m_deref_valobj = new ValueObjectChild (*this,
3500 clang_ast,
3501 child_clang_type,
3502 child_name,
3503 child_byte_size,
3504 child_byte_offset,
3505 child_bitfield_bit_size,
3506 child_bitfield_bit_offset,
3507 child_is_base_class,
Enrico Granata9128ee22011-09-06 19:20:51 +00003508 child_is_deref_of_parent,
3509 eAddressTypeInvalid);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003510 }
3511 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003512
Jim Ingham58b59f92011-04-22 23:53:53 +00003513 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003514 {
3515 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003516 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003517 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003518 else
3519 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003520 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003521 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003522
3523 if (is_pointer_type)
3524 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3525 else
3526 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003527 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003528 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003529}
3530
Greg Claytonafacd142011-09-02 01:15:17 +00003531ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003532ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003533{
Jim Ingham78a685a2011-04-16 00:01:13 +00003534 if (m_addr_of_valobj_sp)
3535 return m_addr_of_valobj_sp;
3536
Greg Claytone0d378b2011-03-24 21:19:54 +00003537 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003538 const bool scalar_is_load_address = false;
Enrico Granata9128ee22011-09-06 19:20:51 +00003539 addr_t addr = GetAddressOf (scalar_is_load_address, &address_type);
Greg Clayton54979cd2010-12-15 05:08:08 +00003540 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003541 if (addr != LLDB_INVALID_ADDRESS)
3542 {
3543 switch (address_type)
3544 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003545 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003546 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003547 {
3548 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003549 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003550 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3551 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003552 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003553
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003554 case eAddressTypeFile:
3555 case eAddressTypeLoad:
3556 case eAddressTypeHost:
3557 {
3558 clang::ASTContext *ast = GetClangAST();
3559 clang_type_t clang_type = GetClangType();
3560 if (ast && clang_type)
3561 {
3562 std::string name (1, '&');
3563 name.append (m_name.AsCString(""));
Greg Claytoncc4d0142012-02-17 07:49:44 +00003564 ExecutionContext exe_ctx (GetExecutionContextRef());
3565 m_addr_of_valobj_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
Jim Ingham58b59f92011-04-22 23:53:53 +00003566 ast,
3567 ClangASTContext::CreatePointerType (ast, clang_type),
3568 ConstString (name.c_str()),
3569 addr,
3570 eAddressTypeInvalid,
3571 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003572 }
3573 }
3574 break;
3575 }
3576 }
Jim Ingham78a685a2011-04-16 00:01:13 +00003577 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003578}
3579
Greg Clayton9a142cf2012-02-03 05:34:10 +00003580ValueObjectSP
3581ValueObject::Cast (const ClangASTType &clang_ast_type)
3582{
Greg Clayton81e871e2012-02-04 02:27:34 +00003583 return ValueObjectCast::Create (*this, GetName(), clang_ast_type);
Greg Clayton9a142cf2012-02-03 05:34:10 +00003584}
Greg Claytonb2dcc362011-05-05 23:32:56 +00003585
Greg Claytonafacd142011-09-02 01:15:17 +00003586ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003587ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3588{
Greg Claytonafacd142011-09-02 01:15:17 +00003589 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003590 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003591 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003592
3593 if (ptr_value != LLDB_INVALID_ADDRESS)
3594 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003595 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003596 ExecutionContext exe_ctx (GetExecutionContextRef());
3597 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003598 name,
3599 ptr_addr,
3600 clang_ast_type);
3601 }
3602 return valobj_sp;
3603}
3604
Greg Claytonafacd142011-09-02 01:15:17 +00003605ValueObjectSP
Greg Claytonb2dcc362011-05-05 23:32:56 +00003606ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3607{
Greg Claytonafacd142011-09-02 01:15:17 +00003608 ValueObjectSP valobj_sp;
Greg Claytonb2dcc362011-05-05 23:32:56 +00003609 AddressType address_type;
Enrico Granata9128ee22011-09-06 19:20:51 +00003610 addr_t ptr_value = GetPointerValue (&address_type);
Greg Claytonb2dcc362011-05-05 23:32:56 +00003611
3612 if (ptr_value != LLDB_INVALID_ADDRESS)
3613 {
Greg Claytone72dfb32012-02-24 01:59:29 +00003614 Address ptr_addr (ptr_value);
Greg Claytoncc4d0142012-02-17 07:49:44 +00003615 ExecutionContext exe_ctx (GetExecutionContextRef());
3616 valobj_sp = ValueObjectMemory::Create (exe_ctx.GetBestExecutionContextScope(),
Greg Claytonb2dcc362011-05-05 23:32:56 +00003617 name,
3618 ptr_addr,
3619 type_sp);
3620 }
3621 return valobj_sp;
3622}
3623
Jim Ingham6035b672011-03-31 00:19:25 +00003624ValueObject::EvaluationPoint::EvaluationPoint () :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003625 m_mod_id(),
3626 m_exe_ctx_ref(),
3627 m_needs_update (true),
3628 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003629{
3630}
3631
3632ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Greg Claytoncc4d0142012-02-17 07:49:44 +00003633 m_mod_id(),
3634 m_exe_ctx_ref(),
Jim Ingham6035b672011-03-31 00:19:25 +00003635 m_needs_update (true),
Greg Claytoncc4d0142012-02-17 07:49:44 +00003636 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003637{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003638 ExecutionContext exe_ctx(exe_scope);
3639 TargetSP target_sp (exe_ctx.GetTargetSP());
3640 if (target_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003641 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003642 m_exe_ctx_ref.SetTargetSP (target_sp);
3643 ProcessSP process_sp (exe_ctx.GetProcessSP());
3644 if (!process_sp)
3645 process_sp = target_sp->GetProcessSP();
Jim Ingham6035b672011-03-31 00:19:25 +00003646
Greg Claytoncc4d0142012-02-17 07:49:44 +00003647 if (process_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003648 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003649 m_mod_id = process_sp->GetModID();
3650 m_exe_ctx_ref.SetProcessSP (process_sp);
Jim Ingham4b536182011-08-09 02:12:22 +00003651
Greg Claytoncc4d0142012-02-17 07:49:44 +00003652 ThreadSP thread_sp (exe_ctx.GetThreadSP());
Jim Ingham6035b672011-03-31 00:19:25 +00003653
Greg Claytoncc4d0142012-02-17 07:49:44 +00003654 if (!thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003655 {
3656 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003657 thread_sp = process_sp->GetThreadList().GetSelectedThread();
Jim Ingham6035b672011-03-31 00:19:25 +00003658 }
Jim Ingham6035b672011-03-31 00:19:25 +00003659
Greg Claytoncc4d0142012-02-17 07:49:44 +00003660 if (thread_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003661 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003662 m_exe_ctx_ref.SetThreadSP(thread_sp);
Greg Claytonc14ee322011-09-22 04:58:26 +00003663
Greg Claytoncc4d0142012-02-17 07:49:44 +00003664 StackFrameSP frame_sp (exe_ctx.GetFrameSP());
3665 if (!frame_sp)
Jim Ingham6035b672011-03-31 00:19:25 +00003666 {
3667 if (use_selected)
Greg Claytoncc4d0142012-02-17 07:49:44 +00003668 frame_sp = thread_sp->GetSelectedFrame();
Jim Ingham6035b672011-03-31 00:19:25 +00003669 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003670 if (frame_sp)
3671 m_exe_ctx_ref.SetFrameSP(frame_sp);
Jim Ingham6035b672011-03-31 00:19:25 +00003672 }
3673 }
3674 }
Jim Ingham6035b672011-03-31 00:19:25 +00003675}
3676
3677ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
Greg Claytoncc4d0142012-02-17 07:49:44 +00003678 m_mod_id(),
3679 m_exe_ctx_ref(rhs.m_exe_ctx_ref),
3680 m_needs_update (true),
3681 m_first_update (true)
Jim Ingham6035b672011-03-31 00:19:25 +00003682{
3683}
3684
3685ValueObject::EvaluationPoint::~EvaluationPoint ()
3686{
3687}
3688
Jim Ingham6035b672011-03-31 00:19:25 +00003689// This function checks the EvaluationPoint against the current process state. If the current
3690// state matches the evaluation point, or the evaluation point is already invalid, then we return
3691// false, meaning "no change". If the current state is different, we update our state, and return
3692// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3693// future calls to NeedsUpdate will return true.
Jim Ingham9ee01152011-12-10 01:49:43 +00003694// exe_scope will be set to the current execution context scope.
Jim Ingham6035b672011-03-31 00:19:25 +00003695
3696bool
Greg Claytoncc4d0142012-02-17 07:49:44 +00003697ValueObject::EvaluationPoint::SyncWithProcessState()
Jim Ingham6035b672011-03-31 00:19:25 +00003698{
Jim Ingham73ca05a2011-12-17 01:35:57 +00003699
3700 // 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 +00003701 ExecutionContext exe_ctx(m_exe_ctx_ref.Lock());
Jim Ingham73ca05a2011-12-17 01:35:57 +00003702
Greg Claytoncc4d0142012-02-17 07:49:44 +00003703 if (exe_ctx.GetTargetPtr() == NULL)
Jim Ingham73ca05a2011-12-17 01:35:57 +00003704 return false;
3705
Jim Ingham6035b672011-03-31 00:19:25 +00003706 // If we don't have a process nothing can change.
Greg Claytoncc4d0142012-02-17 07:49:44 +00003707 Process *process = exe_ctx.GetProcessPtr();
3708 if (process == NULL)
Jim Ingham6035b672011-03-31 00:19:25 +00003709 return false;
Jim Ingham73ca05a2011-12-17 01:35:57 +00003710
Jim Ingham6035b672011-03-31 00:19:25 +00003711 // If our stop id is the current stop ID, nothing has changed:
Greg Claytoncc4d0142012-02-17 07:49:44 +00003712 ProcessModID current_mod_id = process->GetModID();
Jim Ingham4b536182011-08-09 02:12:22 +00003713
Jim Ingham78a685a2011-04-16 00:01:13 +00003714 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3715 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003716 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003717 return false;
Jim Ingham9ee01152011-12-10 01:49:43 +00003718
3719 bool changed;
3720
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003721 if (m_mod_id.IsValid())
3722 {
3723 if (m_mod_id == current_mod_id)
3724 {
Jim Ingham5cfbe4a2012-01-12 22:42:34 +00003725 // Everything is already up to date in this object, no need to
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003726 // update the execution context scope.
Jim Ingham9ee01152011-12-10 01:49:43 +00003727 changed = false;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003728 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003729 else
3730 {
3731 m_mod_id = current_mod_id;
3732 m_needs_update = true;
3733 changed = true;
3734 }
Greg Clayton7e9b1fd2011-08-12 21:40:01 +00003735 }
Jim Ingham6035b672011-03-31 00:19:25 +00003736
Jim Ingham73ca05a2011-12-17 01:35:57 +00003737 // Now re-look up the thread and frame in case the underlying objects have gone away & been recreated.
3738 // That way we'll be sure to return a valid exe_scope.
3739 // 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 +00003740
Greg Claytoncc4d0142012-02-17 07:49:44 +00003741 if (m_exe_ctx_ref.HasThreadRef())
Jim Ingham6035b672011-03-31 00:19:25 +00003742 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003743 ThreadSP thread_sp (m_exe_ctx_ref.GetThreadSP());
3744 if (thread_sp)
Greg Clayton262f80d2011-07-06 16:49:27 +00003745 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003746 if (m_exe_ctx_ref.HasFrameRef())
3747 {
3748 StackFrameSP frame_sp (m_exe_ctx_ref.GetFrameSP());
3749 if (!frame_sp)
3750 {
3751 // We used to have a frame, but now it is gone
3752 SetInvalid();
3753 }
3754 }
Greg Clayton262f80d2011-07-06 16:49:27 +00003755 }
Jim Ingham6035b672011-03-31 00:19:25 +00003756 else
3757 {
Greg Claytoncc4d0142012-02-17 07:49:44 +00003758 // We used to have a thread, but now it is gone
3759 SetInvalid();
Jim Ingham6035b672011-03-31 00:19:25 +00003760 }
Greg Claytoncc4d0142012-02-17 07:49:44 +00003761
Jim Ingham6035b672011-03-31 00:19:25 +00003762 }
Jim Ingham9ee01152011-12-10 01:49:43 +00003763 return changed;
Jim Ingham6035b672011-03-31 00:19:25 +00003764}
3765
Jim Ingham61be0902011-05-02 18:13:59 +00003766void
3767ValueObject::EvaluationPoint::SetUpdated ()
3768{
Greg Claytoncc4d0142012-02-17 07:49:44 +00003769 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
3770 if (process_sp)
3771 m_mod_id = process_sp->GetModID();
Jim Ingham61be0902011-05-02 18:13:59 +00003772 m_first_update = false;
3773 m_needs_update = false;
Jim Ingham61be0902011-05-02 18:13:59 +00003774}
3775
3776
Greg Claytoncc4d0142012-02-17 07:49:44 +00003777//bool
3778//ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3779//{
3780// if (!IsValid())
3781// return false;
3782//
3783// bool needs_update = false;
3784//
3785// // The target has to be non-null, and the
3786// Target *target = exe_scope->CalculateTarget();
3787// if (target != NULL)
3788// {
3789// Target *old_target = m_target_sp.get();
3790// assert (target == old_target);
3791// Process *process = exe_scope->CalculateProcess();
3792// if (process != NULL)
3793// {
3794// // FOR NOW - assume you can't update variable objects across process boundaries.
3795// Process *old_process = m_process_sp.get();
3796// assert (process == old_process);
3797// ProcessModID current_mod_id = process->GetModID();
3798// if (m_mod_id != current_mod_id)
3799// {
3800// needs_update = true;
3801// m_mod_id = current_mod_id;
3802// }
3803// // See if we're switching the thread or stack context. If no thread is given, this is
3804// // being evaluated in a global context.
3805// Thread *thread = exe_scope->CalculateThread();
3806// if (thread != NULL)
3807// {
3808// user_id_t new_thread_index = thread->GetIndexID();
3809// if (new_thread_index != m_thread_id)
3810// {
3811// needs_update = true;
3812// m_thread_id = new_thread_index;
3813// m_stack_id.Clear();
3814// }
3815//
3816// StackFrame *new_frame = exe_scope->CalculateStackFrame();
3817// if (new_frame != NULL)
3818// {
3819// if (new_frame->GetStackID() != m_stack_id)
3820// {
3821// needs_update = true;
3822// m_stack_id = new_frame->GetStackID();
3823// }
3824// }
3825// else
3826// {
3827// m_stack_id.Clear();
3828// needs_update = true;
3829// }
3830// }
3831// else
3832// {
3833// // If this had been given a thread, and now there is none, we should update.
3834// // Otherwise we don't have to do anything.
3835// if (m_thread_id != LLDB_INVALID_UID)
3836// {
3837// m_thread_id = LLDB_INVALID_UID;
3838// m_stack_id.Clear();
3839// needs_update = true;
3840// }
3841// }
3842// }
3843// else
3844// {
3845// // If there is no process, then we don't need to update anything.
3846// // But if we're switching from having a process to not, we should try to update.
3847// if (m_process_sp.get() != NULL)
3848// {
3849// needs_update = true;
3850// m_process_sp.reset();
3851// m_thread_id = LLDB_INVALID_UID;
3852// m_stack_id.Clear();
3853// }
3854// }
3855// }
3856// else
3857// {
3858// // If there's no target, nothing can change so we don't need to update anything.
3859// // But if we're switching from having a target to not, we should try to update.
3860// if (m_target_sp.get() != NULL)
3861// {
3862// needs_update = true;
3863// m_target_sp.reset();
3864// m_process_sp.reset();
3865// m_thread_id = LLDB_INVALID_UID;
3866// m_stack_id.Clear();
3867// }
3868// }
3869// if (!m_needs_update)
3870// m_needs_update = needs_update;
3871//
3872// return needs_update;
3873//}
Enrico Granataf2bbf712011-07-15 02:26:42 +00003874
3875void
Enrico Granata86cc9822012-03-19 22:58:49 +00003876ValueObject::ClearUserVisibleData(uint32_t clear_mask)
Enrico Granataf2bbf712011-07-15 02:26:42 +00003877{
Enrico Granata86cc9822012-03-19 22:58:49 +00003878 if ((clear_mask & eClearUserVisibleDataItemsValue) == eClearUserVisibleDataItemsValue)
3879 m_value_str.clear();
3880
3881 if ((clear_mask & eClearUserVisibleDataItemsLocation) == eClearUserVisibleDataItemsLocation)
3882 m_location_str.clear();
3883
3884 if ((clear_mask & eClearUserVisibleDataItemsSummary) == eClearUserVisibleDataItemsSummary)
3885 {
3886 m_is_getting_summary = false;
3887 m_summary_str.clear();
3888 }
3889
3890 if ((clear_mask & eClearUserVisibleDataItemsDescription) == eClearUserVisibleDataItemsDescription)
3891 m_object_desc_str.clear();
3892
3893 if ((clear_mask & eClearUserVisibleDataItemsSyntheticChildren) == eClearUserVisibleDataItemsSyntheticChildren)
3894 {
3895 if (m_synthetic_value)
3896 m_synthetic_value = NULL;
3897 }
Johnny Chen44805302011-07-19 19:48:13 +00003898}
Enrico Granata9128ee22011-09-06 19:20:51 +00003899
3900SymbolContextScope *
3901ValueObject::GetSymbolContextScope()
3902{
3903 if (m_parent)
3904 {
3905 if (!m_parent->IsPointerOrReferenceType())
3906 return m_parent->GetSymbolContextScope();
3907 }
3908 return NULL;
3909}