blob: 80c8d5acd0f12dd0cb5dddb8bb8a911fea4a15ac [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 Granata4becb372011-06-29 22:27:15 +000022#include "lldb/Core/Debugger.h"
Enrico Granata6f3533f2011-07-29 19:53:35 +000023#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024#include "lldb/Core/StreamString.h"
25#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000026#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000027#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Core/ValueObjectList.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000029#include "lldb/Core/ValueObjectMemory.h"
Enrico Granatad55546b2011-07-22 00:16:08 +000030#include "lldb/Core/ValueObjectSyntheticFilter.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031
Greg Clayton7fb56d02011-02-01 01:31:41 +000032#include "lldb/Host/Endian.h"
33
Enrico Granataf2bbf712011-07-15 02:26:42 +000034#include "lldb/Interpreter/ScriptInterpreterPython.h"
35
Greg Claytone1a916a2010-07-21 22:12:05 +000036#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Symbol/ClangASTContext.h"
38#include "lldb/Symbol/Type.h"
39
Jim Ingham53c47f12010-09-10 23:12:17 +000040#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000041#include "lldb/Target/LanguageRuntime.h"
Enrico Granatac3e320a2011-08-02 17:27:39 +000042#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Target/Process.h"
44#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000045#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047
Enrico Granataf4efecd2011-07-12 22:56:10 +000048#include "lldb/Utility/RefCounter.h"
49
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050using namespace lldb;
51using namespace lldb_private;
Enrico Granataf4efecd2011-07-12 22:56:10 +000052using namespace lldb_utility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053
54static lldb::user_id_t g_value_obj_uid = 0;
55
56//----------------------------------------------------------------------
57// ValueObject constructor
58//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000059ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000060 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000061 m_parent (&parent),
Stephen Wilson71c21d12011-04-11 19:41:40 +000062 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000063 m_name (),
64 m_data (),
65 m_value (),
66 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000067 m_value_str (),
68 m_old_value_str (),
69 m_location_str (),
70 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000071 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000072 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000073 m_children (),
74 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000075 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +000076 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +000077 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000078 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +000079 m_last_format_mgr_revision(0),
Enrico Granatad8b5fce2011-08-02 23:12:24 +000080 m_last_format_mgr_dynamic(parent.m_last_format_mgr_dynamic),
Enrico Granata9df29e32011-07-19 20:57:44 +000081 m_last_summary_format(),
82 m_forced_summary_format(),
83 m_last_value_format(),
Enrico Granatad55546b2011-07-22 00:16:08 +000084 m_last_synthetic_filter(),
Jim Ingham4b536182011-08-09 02:12:22 +000085 m_user_id_of_forced_summary(),
Greg Clayton288bdf92010-09-02 02:59:18 +000086 m_value_is_valid (false),
87 m_value_did_change (false),
88 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000089 m_old_value_valid (false),
Greg Claytone221f822011-01-21 01:59:00 +000090 m_pointers_point_to_load_addrs (false),
Enrico Granata4becb372011-06-29 22:27:15 +000091 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +000092 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +000093 m_is_bitfield_for_scalar(false),
Enrico Granatad55546b2011-07-22 00:16:08 +000094 m_is_expression_path_child(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +000095 m_is_child_at_offset(false),
Enrico Granata9910bc82011-08-03 02:18:51 +000096 m_is_expression_result(parent.m_is_expression_result),
Enrico Granataf4efecd2011-07-12 22:56:10 +000097 m_dump_printable_counter(0)
Jim Ingham6035b672011-03-31 00:19:25 +000098{
Jim Ingham58b59f92011-04-22 23:53:53 +000099 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +0000100}
101
102//----------------------------------------------------------------------
103// ValueObject constructor
104//----------------------------------------------------------------------
105ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
106 UserID (++g_value_obj_uid), // Unique identifier for every value object
107 m_parent (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000108 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +0000109 m_name (),
110 m_data (),
111 m_value (),
112 m_error (),
113 m_value_str (),
114 m_old_value_str (),
115 m_location_str (),
116 m_summary_str (),
117 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000118 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000119 m_children (),
120 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000121 m_dynamic_value (NULL),
Enrico Granatad55546b2011-07-22 00:16:08 +0000122 m_synthetic_value(NULL),
Jim Ingham58b59f92011-04-22 23:53:53 +0000123 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000124 m_format (eFormatDefault),
Enrico Granata9df29e32011-07-19 20:57:44 +0000125 m_last_format_mgr_revision(0),
Enrico Granatac3e320a2011-08-02 17:27:39 +0000126 m_last_format_mgr_dynamic(lldb::eNoDynamicValues),
Enrico Granata9df29e32011-07-19 20:57:44 +0000127 m_last_summary_format(),
128 m_forced_summary_format(),
129 m_last_value_format(),
Enrico Granatad55546b2011-07-22 00:16:08 +0000130 m_last_synthetic_filter(),
Jim Ingham4b536182011-08-09 02:12:22 +0000131 m_user_id_of_forced_summary(),
Jim Ingham6035b672011-03-31 00:19:25 +0000132 m_value_is_valid (false),
133 m_value_did_change (false),
134 m_children_count_valid (false),
135 m_old_value_valid (false),
136 m_pointers_point_to_load_addrs (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000137 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000138 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000139 m_is_bitfield_for_scalar(false),
Enrico Granatad55546b2011-07-22 00:16:08 +0000140 m_is_expression_path_child(false),
Enrico Granata6f3533f2011-07-29 19:53:35 +0000141 m_is_child_at_offset(false),
Enrico Granatac3e320a2011-08-02 17:27:39 +0000142 m_is_expression_result(false),
Enrico Granataf4efecd2011-07-12 22:56:10 +0000143 m_dump_printable_counter(0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144{
Jim Ingham58b59f92011-04-22 23:53:53 +0000145 m_manager = new ValueObjectManager();
146 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
149//----------------------------------------------------------------------
150// Destructor
151//----------------------------------------------------------------------
152ValueObject::~ValueObject ()
153{
154}
155
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000157ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000159 return UpdateValueIfNeeded(m_last_format_mgr_dynamic, update_format);
160}
161
162bool
163ValueObject::UpdateValueIfNeeded (lldb::DynamicValueType use_dynamic, bool update_format)
164{
Enrico Granata4becb372011-06-29 22:27:15 +0000165
Enrico Granata0a3958e2011-07-02 00:25:22 +0000166 if (update_format)
Enrico Granatac3e320a2011-08-02 17:27:39 +0000167 UpdateFormatsIfNeeded(use_dynamic);
Enrico Granata4becb372011-06-29 22:27:15 +0000168
Greg Claytonb71f3842010-10-05 03:13:51 +0000169 // If this is a constant value, then our success is predicated on whether
170 // we have an error or not
171 if (GetIsConstant())
172 return m_error.Success();
173
Jim Ingham6035b672011-03-31 00:19:25 +0000174 bool first_update = m_update_point.IsFirstEvaluation();
175
176 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000177 {
Jim Ingham6035b672011-03-31 00:19:25 +0000178 m_update_point.SetUpdated();
179
180 // Save the old value using swap to avoid a string copy which
181 // also will clear our m_value_str
182 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000183 {
Jim Ingham6035b672011-03-31 00:19:25 +0000184 m_old_value_valid = false;
185 }
186 else
187 {
188 m_old_value_valid = true;
189 m_old_value_str.swap (m_value_str);
190 m_value_str.clear();
191 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192
Enrico Granataf2bbf712011-07-15 02:26:42 +0000193 ClearUserVisibleData();
194
Jim Ingham6035b672011-03-31 00:19:25 +0000195 const bool value_was_valid = GetValueIsValid();
196 SetValueDidChange (false);
Greg Clayton73b953b2010-08-28 00:08:07 +0000197
Jim Ingham6035b672011-03-31 00:19:25 +0000198 m_error.Clear();
Greg Clayton73b953b2010-08-28 00:08:07 +0000199
Jim Ingham6035b672011-03-31 00:19:25 +0000200 // Call the pure virtual function to update the value
201 bool success = UpdateValue ();
202
203 SetValueIsValid (success);
204
205 if (first_update)
206 SetValueDidChange (false);
207 else if (!m_value_did_change && success == false)
208 {
209 // The value wasn't gotten successfully, so we mark this
210 // as changed if the value used to be valid and now isn't
211 SetValueDidChange (value_was_valid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 }
213 }
214 return m_error.Success();
215}
216
Enrico Granata4becb372011-06-29 22:27:15 +0000217void
Enrico Granatac3e320a2011-08-02 17:27:39 +0000218ValueObject::UpdateFormatsIfNeeded(lldb::DynamicValueType use_dynamic)
Enrico Granata4becb372011-06-29 22:27:15 +0000219{
Enrico Granata6f3533f2011-07-29 19:53:35 +0000220 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
221 if (log)
222 log->Printf("checking for FormatManager revisions. VO named %s is at revision %d, while the format manager is at revision %d",
223 GetName().GetCString(),
Enrico Granata4becb372011-06-29 22:27:15 +0000224 m_last_format_mgr_revision,
Enrico Granata6f3533f2011-07-29 19:53:35 +0000225 Debugger::Formatting::ValueFormats::GetCurrentRevision());
Jim Ingham4b536182011-08-09 02:12:22 +0000226 if (HasCustomSummaryFormat() && m_update_point.GetModID() != m_user_id_of_forced_summary)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000227 {
228 ClearCustomSummaryFormat();
229 m_summary_str.clear();
230 }
Enrico Granatac3e320a2011-08-02 17:27:39 +0000231 if ( (m_last_format_mgr_revision != Debugger::Formatting::ValueFormats::GetCurrentRevision()) ||
232 m_last_format_mgr_dynamic != use_dynamic)
Enrico Granata4becb372011-06-29 22:27:15 +0000233 {
234 if (m_last_summary_format.get())
Enrico Granataf2bbf712011-07-15 02:26:42 +0000235 m_last_summary_format.reset((StringSummaryFormat*)NULL);
Enrico Granata4becb372011-06-29 22:27:15 +0000236 if (m_last_value_format.get())
Enrico Granatad55546b2011-07-22 00:16:08 +0000237 m_last_value_format.reset(/*(ValueFormat*)NULL*/);
238 if (m_last_synthetic_filter.get())
239 m_last_synthetic_filter.reset(/*(SyntheticFilter*)NULL*/);
Enrico Granataa37a0652011-07-24 00:14:56 +0000240
241 m_synthetic_value = NULL;
Enrico Granata1490c6f2011-07-19 02:34:21 +0000242
Enrico Granata9910bc82011-08-03 02:18:51 +0000243 Debugger::Formatting::ValueFormats::Get(*this, lldb::eNoDynamicValues, m_last_value_format);
Enrico Granatac3e320a2011-08-02 17:27:39 +0000244 Debugger::Formatting::GetSummaryFormat(*this, use_dynamic, m_last_summary_format);
245 Debugger::Formatting::GetSyntheticFilter(*this, use_dynamic, m_last_synthetic_filter);
Enrico Granata1490c6f2011-07-19 02:34:21 +0000246
247 m_last_format_mgr_revision = Debugger::Formatting::ValueFormats::GetCurrentRevision();
Enrico Granatac3e320a2011-08-02 17:27:39 +0000248 m_last_format_mgr_dynamic = use_dynamic;
Enrico Granataf2bbf712011-07-15 02:26:42 +0000249
250 ClearUserVisibleData();
Enrico Granata4becb372011-06-29 22:27:15 +0000251 }
252}
253
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254DataExtractor &
255ValueObject::GetDataExtractor ()
256{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000257 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000258 return m_data;
259}
260
261const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000262ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000263{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000264 UpdateValueIfNeeded(false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000265 return m_error;
266}
267
268const ConstString &
269ValueObject::GetName() const
270{
271 return m_name;
272}
273
274const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000275ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276{
Enrico Granatac3e320a2011-08-02 17:27:39 +0000277 if (UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000278 {
279 if (m_location_str.empty())
280 {
281 StreamString sstr;
282
283 switch (m_value.GetValueType())
284 {
285 default:
286 break;
287
288 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000289 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 {
291 RegisterInfo *reg_info = m_value.GetRegisterInfo();
292 if (reg_info)
293 {
294 if (reg_info->name)
295 m_location_str = reg_info->name;
296 else if (reg_info->alt_name)
297 m_location_str = reg_info->alt_name;
298 break;
299 }
300 }
301 m_location_str = "scalar";
302 break;
303
304 case Value::eValueTypeLoadAddress:
305 case Value::eValueTypeFileAddress:
306 case Value::eValueTypeHostAddress:
307 {
308 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
309 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
310 m_location_str.swap(sstr.GetString());
311 }
312 break;
313 }
314 }
315 }
316 return m_location_str.c_str();
317}
318
319Value &
320ValueObject::GetValue()
321{
322 return m_value;
323}
324
325const Value &
326ValueObject::GetValue() const
327{
328 return m_value;
329}
330
331bool
Jim Ingham6035b672011-03-31 00:19:25 +0000332ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000333{
Enrico Granata6fd87d52011-08-04 01:41:02 +0000334 if (UpdateValueIfNeeded(false)) // make sure that you are up to date before returning anything
335 {
336 ExecutionContext exe_ctx;
337 ExecutionContextScope *exe_scope = GetExecutionContextScope();
338 if (exe_scope)
339 exe_scope->CalculateExecutionContext(exe_ctx);
340 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
341 return scalar.IsValid();
342 }
343 else
344 return false;
Greg Clayton8f343b02010-11-04 01:54:29 +0000345}
346
347bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000348ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000349{
Greg Clayton288bdf92010-09-02 02:59:18 +0000350 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351}
352
353
354void
355ValueObject::SetValueIsValid (bool b)
356{
Greg Clayton288bdf92010-09-02 02:59:18 +0000357 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358}
359
360bool
Jim Ingham6035b672011-03-31 00:19:25 +0000361ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000362{
Jim Ingham6035b672011-03-31 00:19:25 +0000363 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000364 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365}
366
367void
368ValueObject::SetValueDidChange (bool value_changed)
369{
Greg Clayton288bdf92010-09-02 02:59:18 +0000370 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000371}
372
373ValueObjectSP
374ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
375{
376 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000377 // We may need to update our value if we are dynamic
378 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000379 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000380 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000382 // Check if we have already made the child value object?
383 if (can_create && m_children[idx] == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000385 // No we haven't created the child at this index, so lets have our
386 // subclass do it and cache the result for quick future access.
387 m_children[idx] = CreateChildAtIndex (idx, false, 0);
Jim Ingham78a685a2011-04-16 00:01:13 +0000388 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000389
390 if (m_children[idx] != NULL)
391 return m_children[idx]->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000392 }
393 return child_sp;
394}
395
396uint32_t
397ValueObject::GetIndexOfChildWithName (const ConstString &name)
398{
399 bool omit_empty_base_classes = true;
400 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000401 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000402 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 omit_empty_base_classes);
404}
405
406ValueObjectSP
407ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
408{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000409 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000410 // classes (which really aren't part of the expression path), so we
411 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000413
Greg Claytondea8cb42011-06-29 22:09:02 +0000414 // We may need to update our value if we are dynamic
415 if (IsPossibleDynamicType ())
Enrico Granatac3e320a2011-08-02 17:27:39 +0000416 UpdateValueIfNeeded(false);
Greg Claytondea8cb42011-06-29 22:09:02 +0000417
418 std::vector<uint32_t> child_indexes;
419 clang::ASTContext *clang_ast = GetClangAST();
420 void *clang_type = GetClangType();
421 bool omit_empty_base_classes = true;
422 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
423 clang_type,
424 name.GetCString(),
425 omit_empty_base_classes,
426 child_indexes);
427 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000429 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
430 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
431
432 child_sp = GetChildAtIndex(*pos, can_create);
433 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000435 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000436 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000437 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
438 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000439 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000440 else
441 {
442 child_sp.reset();
443 }
444
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 }
446 }
447 return child_sp;
448}
449
450
451uint32_t
452ValueObject::GetNumChildren ()
453{
Greg Clayton288bdf92010-09-02 02:59:18 +0000454 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 {
456 SetNumChildren (CalculateNumChildren());
457 }
458 return m_children.size();
459}
460void
461ValueObject::SetNumChildren (uint32_t num_children)
462{
Greg Clayton288bdf92010-09-02 02:59:18 +0000463 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 m_children.resize(num_children);
465}
466
467void
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468ValueObject::SetName (const ConstString &name)
469{
470 m_name = name;
471}
472
Jim Ingham58b59f92011-04-22 23:53:53 +0000473ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000474ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
475{
Jim Ingham2eec4872011-05-07 00:10:58 +0000476 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000477
Greg Claytondea8cb42011-06-29 22:09:02 +0000478 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +0000479 bool ignore_array_bounds = synthetic_array_member;
Greg Claytondea8cb42011-06-29 22:09:02 +0000480 std::string child_name_str;
481 uint32_t child_byte_size = 0;
482 int32_t child_byte_offset = 0;
483 uint32_t child_bitfield_bit_size = 0;
484 uint32_t child_bitfield_bit_offset = 0;
485 bool child_is_base_class = false;
486 bool child_is_deref_of_parent = false;
487
488 const bool transparent_pointers = synthetic_array_member == false;
489 clang::ASTContext *clang_ast = GetClangAST();
490 clang_type_t clang_type = GetClangType();
491 clang_type_t child_clang_type;
492
493 ExecutionContext exe_ctx;
494 GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
495
496 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
497 clang_ast,
498 GetName().GetCString(),
499 clang_type,
500 idx,
501 transparent_pointers,
502 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +0000503 ignore_array_bounds,
Greg Claytondea8cb42011-06-29 22:09:02 +0000504 child_name_str,
505 child_byte_size,
506 child_byte_offset,
507 child_bitfield_bit_size,
508 child_bitfield_bit_offset,
509 child_is_base_class,
510 child_is_deref_of_parent);
511 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000512 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000513 if (synthetic_index)
514 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000515
Greg Claytondea8cb42011-06-29 22:09:02 +0000516 ConstString child_name;
517 if (!child_name_str.empty())
518 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000519
Greg Claytondea8cb42011-06-29 22:09:02 +0000520 valobj = new ValueObjectChild (*this,
521 clang_ast,
522 child_clang_type,
523 child_name,
524 child_byte_size,
525 child_byte_offset,
526 child_bitfield_bit_size,
527 child_bitfield_bit_offset,
528 child_is_base_class,
529 child_is_deref_of_parent);
530 if (m_pointers_point_to_load_addrs)
531 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000532 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000533
Jim Ingham58b59f92011-04-22 23:53:53 +0000534 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000535}
536
537const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000538ValueObject::GetSummaryAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000539{
Enrico Granatad8b5fce2011-08-02 23:12:24 +0000540 if (UpdateValueIfNeeded (true))
Enrico Granata4becb372011-06-29 22:27:15 +0000541 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000542 if (m_summary_str.empty())
543 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000544 SummaryFormat *summary_format = GetSummaryFormat().get();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000545
546 if (summary_format)
Enrico Granata4becb372011-06-29 22:27:15 +0000547 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000548 m_summary_str = summary_format->FormatObject(GetSP());
Enrico Granata4becb372011-06-29 22:27:15 +0000549 }
Enrico Granataf2bbf712011-07-15 02:26:42 +0000550 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000551 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000552 clang_type_t clang_type = GetClangType();
Greg Clayton737b9322010-09-13 03:32:57 +0000553
Enrico Granata9dd75c82011-07-15 23:30:15 +0000554 // Do some default printout for function pointers
Enrico Granataf2bbf712011-07-15 02:26:42 +0000555 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000556 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000557 StreamString sstr;
558 clang_type_t elem_or_pointee_clang_type;
559 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
560 GetClangAST(),
561 &elem_or_pointee_clang_type));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562
Enrico Granataf2bbf712011-07-15 02:26:42 +0000563 ExecutionContextScope *exe_scope = GetExecutionContextScope();
564 if (exe_scope)
565 {
Enrico Granata9dd75c82011-07-15 23:30:15 +0000566 if (ClangASTContext::IsFunctionPointerType (clang_type))
Jim Ingham6035b672011-03-31 00:19:25 +0000567 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000568 AddressType func_ptr_address_type = eAddressTypeInvalid;
569 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
570
571 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
572 {
573 switch (func_ptr_address_type)
574 {
575 case eAddressTypeInvalid:
576 case eAddressTypeFile:
577 break;
578
579 case eAddressTypeLoad:
580 {
581 Address so_addr;
582 Target *target = exe_scope->CalculateTarget();
583 if (target && target->GetSectionLoadList().IsEmpty() == false)
584 {
585 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
586 {
587 so_addr.Dump (&sstr,
588 exe_scope,
589 Address::DumpStyleResolvedDescription,
590 Address::DumpStyleSectionNameOffset);
591 }
592 }
593 }
594 break;
595
596 case eAddressTypeHost:
597 break;
598 }
599 }
600 if (sstr.GetSize() > 0)
601 {
602 m_summary_str.assign (1, '(');
603 m_summary_str.append (sstr.GetData(), sstr.GetSize());
604 m_summary_str.append (1, ')');
605 }
Jim Ingham6035b672011-03-31 00:19:25 +0000606 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 }
608 }
609 }
610 }
611 }
612 if (m_summary_str.empty())
613 return NULL;
614 return m_summary_str.c_str();
615}
616
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000617bool
618ValueObject::IsCStringContainer(bool check_pointer)
619{
620 clang_type_t elem_or_pointee_clang_type;
621 const Flags type_flags (ClangASTContext::GetTypeInfo (GetClangType(),
622 GetClangAST(),
623 &elem_or_pointee_clang_type));
624 bool is_char_arr_ptr (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
625 ClangASTContext::IsCharType (elem_or_pointee_clang_type));
626 if (!is_char_arr_ptr)
627 return false;
628 if (!check_pointer)
629 return true;
630 if (type_flags.Test(ClangASTContext::eTypeIsArray))
631 return true;
632 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
633 AddressType cstr_address_type = eAddressTypeInvalid;
634 cstr_address = GetAddressOf (cstr_address_type, true);
635 return (cstr_address != LLDB_INVALID_ADDRESS);
636}
637
638void
639ValueObject::ReadPointedString(Stream& s,
640 Error& error,
Enrico Granataf4efecd2011-07-12 22:56:10 +0000641 uint32_t max_length,
642 bool honor_array,
643 lldb::Format item_format)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000644{
645
646 if (max_length == 0)
Enrico Granataf4efecd2011-07-12 22:56:10 +0000647 max_length = 128; // FIXME this should be a setting, or a formatting parameter
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000648
649 clang_type_t clang_type = GetClangType();
650 clang_type_t elem_or_pointee_clang_type;
651 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
652 GetClangAST(),
653 &elem_or_pointee_clang_type));
654 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
655 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
656 {
657 ExecutionContextScope *exe_scope = GetExecutionContextScope();
658 if (exe_scope)
659 {
660 Target *target = exe_scope->CalculateTarget();
Enrico Granata6f3533f2011-07-29 19:53:35 +0000661 if (target == NULL)
662 {
663 s << "<no target to read from>";
664 }
665 else
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000666 {
667 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
668 AddressType cstr_address_type = eAddressTypeInvalid;
669
670 size_t cstr_len = 0;
671 bool capped_data = false;
672 if (type_flags.Test (ClangASTContext::eTypeIsArray))
673 {
674 // We have an array
675 cstr_len = ClangASTContext::GetArraySize (clang_type);
Enrico Granataf4efecd2011-07-12 22:56:10 +0000676 if (cstr_len > max_length)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000677 {
Enrico Granataf4efecd2011-07-12 22:56:10 +0000678 capped_data = true;
679 cstr_len = max_length;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000680 }
681 cstr_address = GetAddressOf (cstr_address_type, true);
682 }
683 else
684 {
685 // We have a pointer
686 cstr_address = GetPointerValue (cstr_address_type, true);
687 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000688 if (cstr_address == LLDB_INVALID_ADDRESS)
689 {
690 s << "<invalid address for data>";
691 }
692 else
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000693 {
694 Address cstr_so_addr (NULL, cstr_address);
695 DataExtractor data;
696 size_t bytes_read = 0;
697 std::vector<char> data_buffer;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000698 bool prefer_file_cache = false;
Enrico Granataf4efecd2011-07-12 22:56:10 +0000699 if (cstr_len > 0 && honor_array)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000700 {
701 data_buffer.resize(cstr_len);
702 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
703 bytes_read = target->ReadMemory (cstr_so_addr,
704 prefer_file_cache,
705 &data_buffer.front(),
706 cstr_len,
707 error);
708 if (bytes_read > 0)
709 {
710 s << '"';
711 data.Dump (&s,
712 0, // Start offset in "data"
Enrico Granataf4efecd2011-07-12 22:56:10 +0000713 item_format,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000714 1, // Size of item (1 byte for a char!)
715 bytes_read, // How many bytes to print?
716 UINT32_MAX, // num per line
717 LLDB_INVALID_ADDRESS,// base address
718 0, // bitfield bit size
719 0); // bitfield bit offset
720 if (capped_data)
721 s << "...";
722 s << '"';
723 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000724 else
725 s << "\"<data not available>\"";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000726 }
727 else
728 {
Enrico Granataf4efecd2011-07-12 22:56:10 +0000729 cstr_len = max_length;
730 const size_t k_max_buf_size = 64;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000731 data_buffer.resize (k_max_buf_size + 1);
732 // NULL terminate in case we don't get the entire C string
733 data_buffer.back() = '\0';
734
735 s << '"';
736
Enrico Granata6f3533f2011-07-29 19:53:35 +0000737 bool any_data = false;
738
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000739 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
740 while ((bytes_read = target->ReadMemory (cstr_so_addr,
741 prefer_file_cache,
742 &data_buffer.front(),
743 k_max_buf_size,
744 error)) > 0)
745 {
Enrico Granata6f3533f2011-07-29 19:53:35 +0000746 any_data = true;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000747 size_t len = strlen(&data_buffer.front());
748 if (len == 0)
749 break;
750 if (len > bytes_read)
751 len = bytes_read;
Enrico Granataf4efecd2011-07-12 22:56:10 +0000752 if (len > cstr_len)
753 len = cstr_len;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000754
755 data.Dump (&s,
756 0, // Start offset in "data"
Enrico Granataf4efecd2011-07-12 22:56:10 +0000757 item_format,
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000758 1, // Size of item (1 byte for a char!)
759 len, // How many bytes to print?
760 UINT32_MAX, // num per line
761 LLDB_INVALID_ADDRESS,// base address
762 0, // bitfield bit size
763 0); // bitfield bit offset
764
765 if (len < k_max_buf_size)
766 break;
Enrico Granataf4efecd2011-07-12 22:56:10 +0000767 if (len >= cstr_len)
768 {
769 s << "...";
770 break;
771 }
772 cstr_len -= len;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000773 cstr_so_addr.Slide (k_max_buf_size);
774 }
Enrico Granata6f3533f2011-07-29 19:53:35 +0000775
776 if (any_data == false)
777 s << "<data not available>";
778
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000779 s << '"';
780 }
781 }
782 }
783 }
784 }
785 else
786 {
787 error.SetErrorString("impossible to read a string from this object");
Enrico Granata6f3533f2011-07-29 19:53:35 +0000788 s << "<not a string object>";
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000789 }
790}
791
Jim Ingham53c47f12010-09-10 23:12:17 +0000792const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000793ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +0000794{
Enrico Granata0a3958e2011-07-02 00:25:22 +0000795
Enrico Granatad8b5fce2011-08-02 23:12:24 +0000796 if (!UpdateValueIfNeeded (true))
Jim Ingham53c47f12010-09-10 23:12:17 +0000797 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000798
799 if (!m_object_desc_str.empty())
800 return m_object_desc_str.c_str();
801
Jim Ingham6035b672011-03-31 00:19:25 +0000802 ExecutionContextScope *exe_scope = GetExecutionContextScope();
803 if (exe_scope == NULL)
804 return NULL;
805
Jim Ingham53c47f12010-09-10 23:12:17 +0000806 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000807 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000808 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000809
Jim Ingham53c47f12010-09-10 23:12:17 +0000810 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000811
812 lldb::LanguageType language = GetObjectRuntimeLanguage();
813 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
814
Jim Inghama2cf2632010-12-23 02:29:54 +0000815 if (runtime == NULL)
816 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000817 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +0000818 clang_type_t opaque_qual_type = GetClangType();
819 if (opaque_qual_type != NULL)
820 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000821 bool is_signed;
822 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
823 || ClangASTContext::IsPointerType (opaque_qual_type))
824 {
Jim Inghama2cf2632010-12-23 02:29:54 +0000825 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +0000826 }
Jim Inghama2cf2632010-12-23 02:29:54 +0000827 }
828 }
829
Jim Ingham8d543de2011-03-31 23:01:21 +0000830 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +0000831 {
832 m_object_desc_str.append (s.GetData());
833 }
Sean Callanan672ad942010-10-23 00:18:49 +0000834
835 if (m_object_desc_str.empty())
836 return NULL;
837 else
838 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000839}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000840
841const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000842ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000843{
844 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000845 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000846 {
Enrico Granatac3e320a2011-08-02 17:27:39 +0000847 if (UpdateValueIfNeeded(true))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848 {
849 if (m_value_str.empty())
850 {
851 const Value::ContextType context_type = m_value.GetContextType();
852
853 switch (context_type)
854 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000855 case Value::eContextTypeClangType:
856 case Value::eContextTypeLLDBType:
857 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000859 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000860 if (clang_type)
861 {
Enrico Granatac3e320a2011-08-02 17:27:39 +0000862 if (m_format == lldb::eFormatDefault && m_last_value_format)
Enrico Granata4becb372011-06-29 22:27:15 +0000863 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000864 m_value_str = m_last_value_format->FormatObject(GetSP());
Enrico Granata4becb372011-06-29 22:27:15 +0000865 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000866 else
Greg Clayton007d5be2011-05-30 00:49:24 +0000867 {
Enrico Granataf2bbf712011-07-15 02:26:42 +0000868 StreamString sstr;
869 Format format = GetFormat();
870 if (format == eFormatDefault)
871 format = (m_is_bitfield_for_scalar ? eFormatUnsigned :
872 ClangASTType::GetFormat(clang_type));
873
874 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
875 clang_type, // The clang type to display
876 &sstr,
877 format, // Format to display this type with
878 m_data, // Data to extract from
879 0, // Byte offset into "m_data"
880 GetByteSize(), // Byte size of item in "m_data"
881 GetBitfieldBitSize(), // Bitfield bit size
882 GetBitfieldBitOffset())) // Bitfield bit offset
883 m_value_str.swap(sstr.GetString());
884 else
885 {
886 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
887 m_data.GetByteSize(),
888 GetByteSize());
889 m_value_str.clear();
890 }
Greg Clayton007d5be2011-05-30 00:49:24 +0000891 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000892 }
893 }
894 break;
895
Greg Clayton526e5af2010-11-13 03:52:47 +0000896 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 {
898 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
899 if (reg_info)
900 {
901 StreamString reg_sstr;
902 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
903 m_value_str.swap(reg_sstr.GetString());
904 }
905 }
906 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000907
908 default:
909 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000910 }
911 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000912
913 if (!m_value_did_change && m_old_value_valid)
914 {
915 // The value was gotten successfully, so we consider the
916 // value as changed if the value string differs
917 SetValueDidChange (m_old_value_str != m_value_str);
918 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919 }
920 }
921 if (m_value_str.empty())
922 return NULL;
923 return m_value_str.c_str();
924}
925
Enrico Granatac3e320a2011-08-02 17:27:39 +0000926// if > 8bytes, 0 is returned. this method should mostly be used
927// to read address values out of pointers
928unsigned long long
929ValueObject::GetValueAsUnsigned()
930{
931 // If our byte size is zero this is an aggregate type that has children
932 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
933 {
934 if (UpdateValueIfNeeded(true))
935 {
936 uint32_t offset = 0;
937 return m_data.GetMaxU64(&offset,
938 m_data.GetByteSize());
939 }
940 }
941 return 0;
942}
943
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000944bool
945ValueObject::GetPrintableRepresentation(Stream& s,
946 ValueObjectRepresentationStyle val_obj_display,
Enrico Granata0a3958e2011-07-02 00:25:22 +0000947 lldb::Format custom_format)
948{
Enrico Granataf4efecd2011-07-12 22:56:10 +0000949
950 RefCounter ref(&m_dump_printable_counter);
951
Enrico Granata9dd75c82011-07-15 23:30:15 +0000952 if (custom_format != lldb::eFormatInvalid)
Enrico Granata0a3958e2011-07-02 00:25:22 +0000953 SetFormat(custom_format);
954
955 const char * return_value;
Enrico Granatacd1c0232011-08-04 23:37:18 +0000956 std::string alloc_mem;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000957
958 switch(val_obj_display)
959 {
960 case eDisplayValue:
961 return_value = GetValueAsCString();
962 break;
963 case eDisplaySummary:
964 return_value = GetSummaryAsCString();
965 break;
966 case eDisplayLanguageSpecific:
967 return_value = GetObjectDescription();
968 break;
Enrico Granataf2bbf712011-07-15 02:26:42 +0000969 case eDisplayLocation:
970 return_value = GetLocationAsCString();
971 break;
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000972 case eDisplayChildrenCount:
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000973 {
Enrico Granatacd1c0232011-08-04 23:37:18 +0000974 alloc_mem.resize(512);
975 return_value = &alloc_mem[0];
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000976 int count = GetNumChildren();
Enrico Granatacd1c0232011-08-04 23:37:18 +0000977 snprintf((char*)return_value, 512, "%d", count);
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000978 break;
979 }
980 default:
981 break;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000982 }
983
Enrico Granataf4efecd2011-07-12 22:56:10 +0000984 // this code snippet might lead to endless recursion, thus we use a RefCounter here to
985 // check that we are not looping endlessly
986 if (!return_value && (m_dump_printable_counter < 3))
Enrico Granata9fc19442011-07-06 02:13:41 +0000987 {
988 // try to pick the other choice
989 if (val_obj_display == eDisplayValue)
990 return_value = GetSummaryAsCString();
991 else if (val_obj_display == eDisplaySummary)
Enrico Granatae992a082011-07-22 17:03:19 +0000992 {
993 if (ClangASTContext::IsAggregateType (GetClangType()) == true)
994 {
Enrico Granata5dfd49c2011-08-04 02:34:29 +0000995 // this thing has no value, and it seems to have no summary
996 // some combination of unitialized data and other factors can also
997 // raise this condition, so let's print a nice generic error message
998 return_value = "<no available summary>";
Enrico Granatae992a082011-07-22 17:03:19 +0000999 }
1000 else
1001 return_value = GetValueAsCString();
1002 }
Enrico Granata9fc19442011-07-06 02:13:41 +00001003 }
Enrico Granata0a3958e2011-07-02 00:25:22 +00001004
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001005 if (return_value)
1006 s.PutCString(return_value);
1007 else
1008 s.PutCString("<no printable representation>");
1009
1010 // we should only return false here if we could not do *anything*
1011 // even if we have an error message as output, that's a success
1012 // from our callers' perspective, so return true
1013 return true;
1014
Enrico Granata0a3958e2011-07-02 00:25:22 +00001015}
1016
Enrico Granata9fc19442011-07-06 02:13:41 +00001017bool
1018ValueObject::DumpPrintableRepresentation(Stream& s,
1019 ValueObjectRepresentationStyle val_obj_display,
1020 lldb::Format custom_format)
1021{
Enrico Granataf4efecd2011-07-12 22:56:10 +00001022
1023 clang_type_t elem_or_pointee_type;
1024 Flags flags(ClangASTContext::GetTypeInfo(GetClangType(), GetClangAST(), &elem_or_pointee_type));
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001025
Enrico Granataf4efecd2011-07-12 22:56:10 +00001026 if (flags.AnySet(ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer)
1027 && val_obj_display == ValueObject::eDisplayValue)
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001028 {
Enrico Granataf4efecd2011-07-12 22:56:10 +00001029 // when being asked to get a printable display an array or pointer type directly,
1030 // try to "do the right thing"
1031
1032 if (IsCStringContainer(true) &&
1033 (custom_format == lldb::eFormatCString ||
1034 custom_format == lldb::eFormatCharArray ||
1035 custom_format == lldb::eFormatChar ||
1036 custom_format == lldb::eFormatVectorOfChar)) // print char[] & char* directly
1037 {
1038 Error error;
1039 ReadPointedString(s,
1040 error,
1041 0,
1042 (custom_format == lldb::eFormatVectorOfChar) ||
1043 (custom_format == lldb::eFormatCharArray));
1044 return !error.Fail();
1045 }
1046
1047 if (custom_format == lldb::eFormatEnum)
1048 return false;
1049
1050 // this only works for arrays, because I have no way to know when
1051 // the pointed memory ends, and no special \0 end of data marker
1052 if (flags.Test(ClangASTContext::eTypeIsArray))
1053 {
1054 if ((custom_format == lldb::eFormatBytes) ||
1055 (custom_format == lldb::eFormatBytesWithASCII))
1056 {
1057 uint32_t count = GetNumChildren();
1058
1059 s << '[';
1060 for (uint32_t low = 0; low < count; low++)
1061 {
1062
1063 if (low)
1064 s << ',';
1065
1066 ValueObjectSP child = GetChildAtIndex(low,true);
1067 if (!child.get())
1068 {
Enrico Granatae992a082011-07-22 17:03:19 +00001069 s << "<invalid child>";
Enrico Granataf4efecd2011-07-12 22:56:10 +00001070 continue;
1071 }
1072 child->DumpPrintableRepresentation(s, ValueObject::eDisplayValue, custom_format);
1073 }
1074
1075 s << ']';
1076
1077 return true;
1078 }
1079
1080 if ((custom_format == lldb::eFormatVectorOfChar) ||
1081 (custom_format == lldb::eFormatVectorOfFloat32) ||
1082 (custom_format == lldb::eFormatVectorOfFloat64) ||
1083 (custom_format == lldb::eFormatVectorOfSInt16) ||
1084 (custom_format == lldb::eFormatVectorOfSInt32) ||
1085 (custom_format == lldb::eFormatVectorOfSInt64) ||
1086 (custom_format == lldb::eFormatVectorOfSInt8) ||
1087 (custom_format == lldb::eFormatVectorOfUInt128) ||
1088 (custom_format == lldb::eFormatVectorOfUInt16) ||
1089 (custom_format == lldb::eFormatVectorOfUInt32) ||
1090 (custom_format == lldb::eFormatVectorOfUInt64) ||
1091 (custom_format == lldb::eFormatVectorOfUInt8)) // arrays of bytes, bytes with ASCII or any vector format should be printed directly
1092 {
1093 uint32_t count = GetNumChildren();
1094
1095 lldb::Format format = FormatManager::GetSingleItemFormat(custom_format);
1096
1097 s << '[';
1098 for (uint32_t low = 0; low < count; low++)
1099 {
1100
1101 if (low)
1102 s << ',';
1103
1104 ValueObjectSP child = GetChildAtIndex(low,true);
1105 if (!child.get())
1106 {
Enrico Granatae992a082011-07-22 17:03:19 +00001107 s << "<invalid child>";
Enrico Granataf4efecd2011-07-12 22:56:10 +00001108 continue;
1109 }
1110 child->DumpPrintableRepresentation(s, ValueObject::eDisplayValue, format);
1111 }
1112
1113 s << ']';
1114
1115 return true;
1116 }
1117 }
1118
1119 if ((custom_format == lldb::eFormatBoolean) ||
1120 (custom_format == lldb::eFormatBinary) ||
1121 (custom_format == lldb::eFormatChar) ||
1122 (custom_format == lldb::eFormatCharPrintable) ||
1123 (custom_format == lldb::eFormatComplexFloat) ||
1124 (custom_format == lldb::eFormatDecimal) ||
1125 (custom_format == lldb::eFormatHex) ||
1126 (custom_format == lldb::eFormatFloat) ||
1127 (custom_format == lldb::eFormatOctal) ||
1128 (custom_format == lldb::eFormatOSType) ||
1129 (custom_format == lldb::eFormatUnicode16) ||
1130 (custom_format == lldb::eFormatUnicode32) ||
1131 (custom_format == lldb::eFormatUnsigned) ||
1132 (custom_format == lldb::eFormatPointer) ||
1133 (custom_format == lldb::eFormatComplexInteger) ||
1134 (custom_format == lldb::eFormatComplex) ||
1135 (custom_format == lldb::eFormatDefault)) // use the [] operator
1136 return false;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001137 }
Enrico Granata5dfd49c2011-08-04 02:34:29 +00001138 bool var_success = GetPrintableRepresentation(s, val_obj_display, custom_format);
Enrico Granata9dd75c82011-07-15 23:30:15 +00001139 if (custom_format != eFormatInvalid)
Enrico Granataf4efecd2011-07-12 22:56:10 +00001140 SetFormat(eFormatDefault);
1141 return var_success;
Enrico Granata9fc19442011-07-06 02:13:41 +00001142}
1143
Greg Clayton737b9322010-09-13 03:32:57 +00001144addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +00001145ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +00001146{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001147 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001148 return LLDB_INVALID_ADDRESS;
1149
Greg Clayton73b472d2010-10-27 03:32:59 +00001150 switch (m_value.GetValueType())
1151 {
1152 case Value::eValueTypeScalar:
1153 if (scalar_is_load_address)
1154 {
1155 address_type = eAddressTypeLoad;
1156 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1157 }
1158 break;
1159
1160 case Value::eValueTypeLoadAddress:
1161 case Value::eValueTypeFileAddress:
1162 case Value::eValueTypeHostAddress:
1163 {
1164 address_type = m_value.GetValueAddressType ();
1165 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1166 }
1167 break;
1168 }
1169 address_type = eAddressTypeInvalid;
1170 return LLDB_INVALID_ADDRESS;
1171}
1172
1173addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +00001174ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +00001175{
1176 lldb::addr_t address = LLDB_INVALID_ADDRESS;
1177 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +00001178
Enrico Granatac3e320a2011-08-02 17:27:39 +00001179 if (!UpdateValueIfNeeded(false))
Jim Ingham78a685a2011-04-16 00:01:13 +00001180 return address;
1181
Greg Clayton73b472d2010-10-27 03:32:59 +00001182 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +00001183 {
1184 case Value::eValueTypeScalar:
1185 if (scalar_is_load_address)
1186 {
1187 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
1188 address_type = eAddressTypeLoad;
1189 }
1190 break;
1191
1192 case Value::eValueTypeLoadAddress:
1193 case Value::eValueTypeFileAddress:
1194 case Value::eValueTypeHostAddress:
1195 {
1196 uint32_t data_offset = 0;
1197 address = m_data.GetPointer(&data_offset);
1198 address_type = m_value.GetValueAddressType();
1199 if (address_type == eAddressTypeInvalid)
1200 address_type = eAddressTypeLoad;
1201 }
1202 break;
1203 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001204
1205 if (m_pointers_point_to_load_addrs)
1206 address_type = eAddressTypeLoad;
1207
Greg Clayton737b9322010-09-13 03:32:57 +00001208 return address;
1209}
1210
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211bool
Jim Ingham6035b672011-03-31 00:19:25 +00001212ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001213{
1214 // Make sure our value is up to date first so that our location and location
1215 // type is valid.
Enrico Granatac3e320a2011-08-02 17:27:39 +00001216 if (!UpdateValueIfNeeded(false))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217 return false;
1218
1219 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001220 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001221
1222 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +00001223 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001224 switch (encoding)
1225 {
1226 case eEncodingInvalid:
1227 return false;
1228
1229 case eEncodingUint:
1230 if (byte_size > sizeof(unsigned long long))
1231 {
1232 return false;
1233 }
1234 else
1235 {
1236 unsigned long long ull_val = strtoull(value_str, &end, 0);
1237 if (end && *end != '\0')
1238 return false;
Jim Ingham4b536182011-08-09 02:12:22 +00001239 Value::ValueType value_type = m_value.GetValueType();
1240 switch (value_type)
1241 {
1242 case Value::eValueTypeLoadAddress:
1243 case Value::eValueTypeHostAddress:
1244 // The value in these cases lives in the data. So update the data:
1245
1246 break;
1247 case Value::eValueTypeScalar:
1248 m_value.GetScalar() = ull_val;
1249 break;
1250 case Value::eValueTypeFileAddress:
1251 // Try to convert the file address to a load address and then write the new value there.
1252 break;
1253 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001254 // Limit the bytes in our m_data appropriately.
1255 m_value.GetScalar().GetData (m_data, byte_size);
1256 }
1257 break;
1258
1259 case eEncodingSint:
1260 if (byte_size > sizeof(long long))
1261 {
1262 return false;
1263 }
1264 else
1265 {
1266 long long sll_val = strtoll(value_str, &end, 0);
1267 if (end && *end != '\0')
1268 return false;
Greg Clayton644247c2011-07-07 01:59:51 +00001269 m_value.GetScalar() = sll_val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 // Limit the bytes in our m_data appropriately.
1271 m_value.GetScalar().GetData (m_data, byte_size);
1272 }
1273 break;
1274
1275 case eEncodingIEEE754:
1276 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001277 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +00001278 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001279 if (dst != NULL)
1280 {
1281 // We are decoding a float into host byte order below, so make
1282 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +00001283 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001284 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
1285 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +00001286 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287 value_str,
1288 dst,
1289 byte_size);
1290
1291 if (converted_byte_size == byte_size)
1292 {
1293 }
1294 }
1295 }
1296 break;
1297
1298 case eEncodingVector:
1299 return false;
1300
1301 default:
1302 return false;
1303 }
1304
1305 // If we have made it here the value is in m_data and we should write it
1306 // out to the target
1307 return Write ();
1308}
1309
1310bool
1311ValueObject::Write ()
1312{
1313 // Clear the update ID so the next time we try and read the value
1314 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +00001315 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001316
1317 // TODO: when Value has a method to write a value back, call it from here.
1318 return false;
1319
1320}
1321
Jim Ingham5a369122010-09-28 01:25:32 +00001322lldb::LanguageType
1323ValueObject::GetObjectRuntimeLanguage ()
1324{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001325 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1326 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001327}
1328
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329void
Jim Ingham58b59f92011-04-22 23:53:53 +00001330ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001331{
Jim Ingham58b59f92011-04-22 23:53:53 +00001332 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001333}
1334
1335ValueObjectSP
1336ValueObject::GetSyntheticChild (const ConstString &key) const
1337{
1338 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001339 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001340 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001341 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001342 return synthetic_child_sp;
1343}
1344
1345bool
1346ValueObject::IsPointerType ()
1347{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001348 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001349}
1350
Jim Inghamb7603bb2011-03-18 00:05:18 +00001351bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001352ValueObject::IsArrayType ()
1353{
1354 return ClangASTContext::IsArrayType (GetClangType());
1355}
1356
1357bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001358ValueObject::IsScalarType ()
1359{
1360 return ClangASTContext::IsScalarType (GetClangType());
1361}
1362
1363bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001364ValueObject::IsIntegerType (bool &is_signed)
1365{
1366 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1367}
Greg Clayton73b472d2010-10-27 03:32:59 +00001368
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001369bool
1370ValueObject::IsPointerOrReferenceType ()
1371{
Greg Clayton007d5be2011-05-30 00:49:24 +00001372 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1373}
1374
1375bool
1376ValueObject::IsPossibleCPlusPlusDynamicType ()
1377{
1378 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379}
1380
Greg Claytondea8cb42011-06-29 22:09:02 +00001381bool
1382ValueObject::IsPossibleDynamicType ()
1383{
1384 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1385}
1386
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001387ValueObjectSP
1388ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1389{
1390 ValueObjectSP synthetic_child_sp;
1391 if (IsPointerType ())
1392 {
1393 char index_str[64];
1394 snprintf(index_str, sizeof(index_str), "[%i]", index);
1395 ConstString index_const_str(index_str);
1396 // Check if we have already created a synthetic array member in this
1397 // valid object. If we have we will re-use it.
1398 synthetic_child_sp = GetSyntheticChild (index_const_str);
1399 if (!synthetic_child_sp)
1400 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001401 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001402 // We haven't made a synthetic array member for INDEX yet, so
1403 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001404 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001405
1406 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001407 if (synthetic_child)
1408 {
1409 AddSyntheticChild(index_const_str, synthetic_child);
1410 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001411 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00001412 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001413 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001414 }
1415 }
1416 return synthetic_child_sp;
1417}
Jim Ingham22777012010-09-23 02:01:19 +00001418
Greg Claytondaf515f2011-07-09 20:12:33 +00001419// This allows you to create an array member using and index
1420// that doesn't not fall in the normal bounds of the array.
1421// Many times structure can be defined as:
1422// struct Collection
1423// {
1424// uint32_t item_count;
1425// Item item_array[0];
1426// };
1427// The size of the "item_array" is 1, but many times in practice
1428// there are more items in "item_array".
1429
1430ValueObjectSP
1431ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1432{
1433 ValueObjectSP synthetic_child_sp;
1434 if (IsArrayType ())
1435 {
1436 char index_str[64];
1437 snprintf(index_str, sizeof(index_str), "[%i]", index);
1438 ConstString index_const_str(index_str);
1439 // Check if we have already created a synthetic array member in this
1440 // valid object. If we have we will re-use it.
1441 synthetic_child_sp = GetSyntheticChild (index_const_str);
1442 if (!synthetic_child_sp)
1443 {
1444 ValueObject *synthetic_child;
1445 // We haven't made a synthetic array member for INDEX yet, so
1446 // lets make one and cache it for any future reference.
1447 synthetic_child = CreateChildAtIndex(0, true, index);
1448
1449 // Cache the value if we got one back...
1450 if (synthetic_child)
1451 {
1452 AddSyntheticChild(index_const_str, synthetic_child);
1453 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001454 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00001455 synthetic_child_sp->m_is_array_item_for_pointer = true;
1456 }
1457 }
1458 }
1459 return synthetic_child_sp;
1460}
1461
Enrico Granata9fc19442011-07-06 02:13:41 +00001462ValueObjectSP
1463ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1464{
1465 ValueObjectSP synthetic_child_sp;
1466 if (IsScalarType ())
1467 {
1468 char index_str[64];
1469 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1470 ConstString index_const_str(index_str);
1471 // Check if we have already created a synthetic array member in this
1472 // valid object. If we have we will re-use it.
1473 synthetic_child_sp = GetSyntheticChild (index_const_str);
1474 if (!synthetic_child_sp)
1475 {
1476 ValueObjectChild *synthetic_child;
1477 // We haven't made a synthetic array member for INDEX yet, so
1478 // lets make one and cache it for any future reference.
1479 synthetic_child = new ValueObjectChild(*this,
1480 GetClangAST(),
1481 GetClangType(),
1482 index_const_str,
1483 GetByteSize(),
1484 0,
1485 to-from+1,
1486 from,
1487 false,
1488 false);
1489
1490 // Cache the value if we got one back...
1491 if (synthetic_child)
1492 {
1493 AddSyntheticChild(index_const_str, synthetic_child);
1494 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001495 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00001496 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1497 }
1498 }
1499 }
1500 return synthetic_child_sp;
1501}
1502
Enrico Granata6f3533f2011-07-29 19:53:35 +00001503lldb::ValueObjectSP
1504ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1505{
1506
1507 ValueObjectSP synthetic_child_sp;
1508
1509 char name_str[64];
1510 snprintf(name_str, sizeof(name_str), "@%i", offset);
1511 ConstString name_const_str(name_str);
1512
1513 // Check if we have already created a synthetic array member in this
1514 // valid object. If we have we will re-use it.
1515 synthetic_child_sp = GetSyntheticChild (name_const_str);
1516
1517 if (synthetic_child_sp.get())
1518 return synthetic_child_sp;
1519
1520 if (!can_create)
1521 return lldb::ValueObjectSP();
1522
1523 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1524 type.GetASTContext(),
1525 type.GetOpaqueQualType(),
1526 name_const_str,
1527 type.GetTypeByteSize(),
1528 offset,
1529 0,
1530 0,
1531 false,
1532 false);
1533 if (synthetic_child)
1534 {
1535 AddSyntheticChild(name_const_str, synthetic_child);
1536 synthetic_child_sp = synthetic_child->GetSP();
1537 synthetic_child_sp->SetName(name_const_str);
1538 synthetic_child_sp->m_is_child_at_offset = true;
1539 }
1540 return synthetic_child_sp;
1541}
1542
Enrico Granatad55546b2011-07-22 00:16:08 +00001543// your expression path needs to have a leading . or ->
1544// (unless it somehow "looks like" an array, in which case it has
1545// a leading [ symbol). while the [ is meaningful and should be shown
1546// to the user, . and -> are just parser design, but by no means
1547// added information for the user.. strip them off
1548static const char*
1549SkipLeadingExpressionPathSeparators(const char* expression)
1550{
1551 if (!expression || !expression[0])
1552 return expression;
1553 if (expression[0] == '.')
1554 return expression+1;
1555 if (expression[0] == '-' && expression[1] == '>')
1556 return expression+2;
1557 return expression;
1558}
1559
1560lldb::ValueObjectSP
1561ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1562{
1563 ValueObjectSP synthetic_child_sp;
1564 ConstString name_const_string(expression);
1565 // Check if we have already created a synthetic array member in this
1566 // valid object. If we have we will re-use it.
1567 synthetic_child_sp = GetSyntheticChild (name_const_string);
1568 if (!synthetic_child_sp)
1569 {
1570 // We haven't made a synthetic array member for expression yet, so
1571 // lets make one and cache it for any future reference.
1572 synthetic_child_sp = GetValueForExpressionPath(expression);
1573
1574 // Cache the value if we got one back...
1575 if (synthetic_child_sp.get())
1576 {
1577 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00001578 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001579 synthetic_child_sp->m_is_expression_path_child = true;
1580 }
1581 }
1582 return synthetic_child_sp;
1583}
1584
1585void
1586ValueObject::CalculateSyntheticValue (lldb::SyntheticValueType use_synthetic)
1587{
1588 if (use_synthetic == lldb::eNoSyntheticFilter)
1589 return;
1590
Enrico Granatac3e320a2011-08-02 17:27:39 +00001591 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
Enrico Granatad55546b2011-07-22 00:16:08 +00001592
1593 if (m_last_synthetic_filter.get() == NULL)
1594 return;
1595
Enrico Granataa37a0652011-07-24 00:14:56 +00001596 if (m_synthetic_value == NULL)
1597 m_synthetic_value = new ValueObjectSynthetic(*this, m_last_synthetic_filter);
Enrico Granatad55546b2011-07-22 00:16:08 +00001598
1599}
1600
Jim Ingham78a685a2011-04-16 00:01:13 +00001601void
Jim Ingham2837b762011-05-04 03:43:18 +00001602ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00001603{
Jim Ingham2837b762011-05-04 03:43:18 +00001604 if (use_dynamic == lldb::eNoDynamicValues)
1605 return;
1606
Jim Ingham58b59f92011-04-22 23:53:53 +00001607 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00001608 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00001609 Process *process = m_update_point.GetProcessSP().get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001610 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +00001611
Jim Ingham78a685a2011-04-16 00:01:13 +00001612
1613 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1614 // hard code this everywhere.
1615 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1616 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1617 {
1618 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1619 if (runtime)
1620 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1621 }
1622 else
1623 {
1624 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1625 if (cpp_runtime)
1626 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1627
1628 if (!worth_having_dynamic_value)
1629 {
1630 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1631 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001632 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001633 }
1634 }
1635
1636 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001637 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001638
1639// if (worth_having_dynamic_value)
1640// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1641
Jim Ingham78a685a2011-04-16 00:01:13 +00001642 }
1643}
1644
Jim Ingham58b59f92011-04-22 23:53:53 +00001645ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001646ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001647{
Jim Ingham2837b762011-05-04 03:43:18 +00001648 if (use_dynamic == lldb::eNoDynamicValues)
1649 return ValueObjectSP();
1650
1651 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001652 {
Jim Ingham2837b762011-05-04 03:43:18 +00001653 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001654 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001655 if (m_dynamic_value)
1656 return m_dynamic_value->GetSP();
1657 else
1658 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001659}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001660
Enrico Granatad55546b2011-07-22 00:16:08 +00001661// GetDynamicValue() returns a NULL SharedPointer if the object is not dynamic
1662// or we do not really want a dynamic VO. this method instead returns this object
1663// itself when making it synthetic has no meaning. this makes it much simpler
1664// to replace the SyntheticValue for the ValueObject
1665ValueObjectSP
1666ValueObject::GetSyntheticValue (SyntheticValueType use_synthetic)
1667{
1668 if (use_synthetic == lldb::eNoSyntheticFilter)
1669 return GetSP();
1670
Enrico Granatac3e320a2011-08-02 17:27:39 +00001671 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
Enrico Granatad55546b2011-07-22 00:16:08 +00001672
1673 if (m_last_synthetic_filter.get() == NULL)
1674 return GetSP();
1675
1676 CalculateSyntheticValue(use_synthetic);
1677
1678 if (m_synthetic_value)
1679 return m_synthetic_value->GetSP();
1680 else
1681 return GetSP();
1682}
1683
Greg Claytone221f822011-01-21 01:59:00 +00001684bool
Enrico Granata27b625e2011-08-09 01:04:56 +00001685ValueObject::HasSyntheticValue()
1686{
1687 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
1688
1689 if (m_last_synthetic_filter.get() == NULL)
1690 return false;
1691
1692 CalculateSyntheticValue(lldb::eUseSyntheticFilter);
1693
1694 if (m_synthetic_value)
1695 return true;
1696 else
1697 return false;
1698}
1699
1700bool
Greg Claytone221f822011-01-21 01:59:00 +00001701ValueObject::GetBaseClassPath (Stream &s)
1702{
1703 if (IsBaseClass())
1704 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001705 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001706 clang_type_t clang_type = GetClangType();
1707 std::string cxx_class_name;
1708 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1709 if (this_had_base_class)
1710 {
1711 if (parent_had_base_class)
1712 s.PutCString("::");
1713 s.PutCString(cxx_class_name.c_str());
1714 }
1715 return parent_had_base_class || this_had_base_class;
1716 }
1717 return false;
1718}
1719
1720
1721ValueObject *
1722ValueObject::GetNonBaseClassParent()
1723{
Jim Ingham78a685a2011-04-16 00:01:13 +00001724 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001725 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001726 if (GetParent()->IsBaseClass())
1727 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001728 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001729 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001730 }
1731 return NULL;
1732}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001733
1734void
Enrico Granata4becb372011-06-29 22:27:15 +00001735ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001736{
Greg Claytone221f822011-01-21 01:59:00 +00001737 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00001738
Enrico Granata9dd75c82011-07-15 23:30:15 +00001739 if (is_deref_of_parent && epformat == eDereferencePointers) {
Enrico Granata4becb372011-06-29 22:27:15 +00001740 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
1741 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
1742 // the eHonorPointers mode is meant to produce strings in this latter format
1743 s.PutCString("*(");
1744 }
Greg Claytone221f822011-01-21 01:59:00 +00001745
Enrico Granata4becb372011-06-29 22:27:15 +00001746 ValueObject* parent = GetParent();
1747
1748 if (parent)
1749 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001750
1751 // if we are a deref_of_parent just because we are synthetic array
1752 // members made up to allow ptr[%d] syntax to work in variable
1753 // printing, then add our name ([%d]) to the expression path
Enrico Granata9dd75c82011-07-15 23:30:15 +00001754 if (m_is_array_item_for_pointer && epformat == eHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00001755 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00001756
Greg Claytone221f822011-01-21 01:59:00 +00001757 if (!IsBaseClass())
1758 {
1759 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001760 {
Greg Claytone221f822011-01-21 01:59:00 +00001761 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1762 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001763 {
Greg Claytone221f822011-01-21 01:59:00 +00001764 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1765 if (non_base_class_parent_clang_type)
1766 {
1767 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1768
Enrico Granata9dd75c82011-07-15 23:30:15 +00001769 if (parent && parent->IsDereferenceOfParent() && epformat == eHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00001770 {
1771 s.PutCString("->");
1772 }
Enrico Granata4becb372011-06-29 22:27:15 +00001773 else
1774 {
1775 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1776 {
1777 s.PutCString("->");
1778 }
1779 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1780 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1781 {
1782 s.PutChar('.');
1783 }
Greg Claytone221f822011-01-21 01:59:00 +00001784 }
1785 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001786 }
Greg Claytone221f822011-01-21 01:59:00 +00001787
1788 const char *name = GetName().GetCString();
1789 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001790 {
Greg Claytone221f822011-01-21 01:59:00 +00001791 if (qualify_cxx_base_classes)
1792 {
1793 if (GetBaseClassPath (s))
1794 s.PutCString("::");
1795 }
1796 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001797 }
1798 }
1799 }
1800
Enrico Granata4becb372011-06-29 22:27:15 +00001801 if (is_deref_of_parent && epformat == eDereferencePointers) {
Greg Claytone221f822011-01-21 01:59:00 +00001802 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00001803 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001804}
1805
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001806lldb::ValueObjectSP
1807ValueObject::GetValueForExpressionPath(const char* expression,
1808 const char** first_unparsed,
1809 ExpressionPathScanEndReason* reason_to_stop,
1810 ExpressionPathEndResultType* final_value_type,
1811 const GetValueForExpressionPathOptions& options,
1812 ExpressionPathAftermath* final_task_on_target)
1813{
1814
1815 const char* dummy_first_unparsed;
1816 ExpressionPathScanEndReason dummy_reason_to_stop;
1817 ExpressionPathEndResultType dummy_final_value_type;
1818 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1819
1820 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1821 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1822 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1823 final_value_type ? final_value_type : &dummy_final_value_type,
1824 options,
1825 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1826
1827 if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1828 {
1829 return ret_val;
1830 }
1831 if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1832 {
1833 if (*final_task_on_target == ValueObject::eDereference)
1834 {
1835 Error error;
1836 ValueObjectSP final_value = ret_val->Dereference(error);
1837 if (error.Fail() || !final_value.get())
1838 {
1839 *reason_to_stop = ValueObject::eDereferencingFailed;
1840 *final_value_type = ValueObject::eInvalid;
1841 return ValueObjectSP();
1842 }
1843 else
1844 {
1845 *final_task_on_target = ValueObject::eNothing;
1846 return final_value;
1847 }
1848 }
1849 if (*final_task_on_target == ValueObject::eTakeAddress)
1850 {
1851 Error error;
1852 ValueObjectSP final_value = ret_val->AddressOf(error);
1853 if (error.Fail() || !final_value.get())
1854 {
1855 *reason_to_stop = ValueObject::eTakingAddressFailed;
1856 *final_value_type = ValueObject::eInvalid;
1857 return ValueObjectSP();
1858 }
1859 else
1860 {
1861 *final_task_on_target = ValueObject::eNothing;
1862 return final_value;
1863 }
1864 }
1865 }
1866 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
1867}
1868
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001869int
1870ValueObject::GetValuesForExpressionPath(const char* expression,
1871 lldb::ValueObjectListSP& list,
1872 const char** first_unparsed,
1873 ExpressionPathScanEndReason* reason_to_stop,
1874 ExpressionPathEndResultType* final_value_type,
1875 const GetValueForExpressionPathOptions& options,
1876 ExpressionPathAftermath* final_task_on_target)
1877{
1878 const char* dummy_first_unparsed;
1879 ExpressionPathScanEndReason dummy_reason_to_stop;
1880 ExpressionPathEndResultType dummy_final_value_type;
1881 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1882
1883 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1884 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1885 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1886 final_value_type ? final_value_type : &dummy_final_value_type,
1887 options,
1888 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1889
1890 if (!ret_val.get()) // if there are errors, I add nothing to the list
1891 return 0;
1892
1893 if (*reason_to_stop != eArrayRangeOperatorMet)
1894 {
1895 // I need not expand a range, just post-process the final value and return
1896 if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1897 {
1898 list->Append(ret_val);
1899 return 1;
1900 }
1901 if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1902 {
1903 if (*final_task_on_target == ValueObject::eDereference)
1904 {
1905 Error error;
1906 ValueObjectSP final_value = ret_val->Dereference(error);
1907 if (error.Fail() || !final_value.get())
1908 {
1909 *reason_to_stop = ValueObject::eDereferencingFailed;
1910 *final_value_type = ValueObject::eInvalid;
1911 return 0;
1912 }
1913 else
1914 {
1915 *final_task_on_target = ValueObject::eNothing;
1916 list->Append(final_value);
1917 return 1;
1918 }
1919 }
1920 if (*final_task_on_target == ValueObject::eTakeAddress)
1921 {
1922 Error error;
1923 ValueObjectSP final_value = ret_val->AddressOf(error);
1924 if (error.Fail() || !final_value.get())
1925 {
1926 *reason_to_stop = ValueObject::eTakingAddressFailed;
1927 *final_value_type = ValueObject::eInvalid;
1928 return 0;
1929 }
1930 else
1931 {
1932 *final_task_on_target = ValueObject::eNothing;
1933 list->Append(final_value);
1934 return 1;
1935 }
1936 }
1937 }
1938 }
1939 else
1940 {
1941 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
1942 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1943 ret_val,
1944 list,
1945 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1946 final_value_type ? final_value_type : &dummy_final_value_type,
1947 options,
1948 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1949 }
1950 // in any non-covered case, just do the obviously right thing
1951 list->Append(ret_val);
1952 return 1;
1953}
1954
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001955lldb::ValueObjectSP
1956ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
1957 const char** first_unparsed,
1958 ExpressionPathScanEndReason* reason_to_stop,
1959 ExpressionPathEndResultType* final_result,
1960 const GetValueForExpressionPathOptions& options,
1961 ExpressionPathAftermath* what_next)
1962{
1963 ValueObjectSP root = GetSP();
1964
1965 if (!root.get())
1966 return ValueObjectSP();
1967
1968 *first_unparsed = expression_cstr;
1969
1970 while (true)
1971 {
1972
1973 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
1974
1975 lldb::clang_type_t root_clang_type = root->GetClangType();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001976 lldb::clang_type_t pointee_clang_type;
1977 Flags root_clang_type_info,pointee_clang_type_info;
1978
1979 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
1980 if (pointee_clang_type)
1981 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001982
1983 if (!expression_cstr || *expression_cstr == '\0')
1984 {
1985 *reason_to_stop = ValueObject::eEndOfString;
1986 return root;
1987 }
1988
1989 switch (*expression_cstr)
1990 {
1991 case '-':
1992 {
1993 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001994 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 +00001995 {
1996 *first_unparsed = expression_cstr;
1997 *reason_to_stop = ValueObject::eArrowInsteadOfDot;
1998 *final_result = ValueObject::eInvalid;
1999 return ValueObjectSP();
2000 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002001 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
2002 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002003 options.m_no_fragile_ivar)
2004 {
2005 *first_unparsed = expression_cstr;
2006 *reason_to_stop = ValueObject::eFragileIVarNotAllowed;
2007 *final_result = ValueObject::eInvalid;
2008 return ValueObjectSP();
2009 }
2010 if (expression_cstr[1] != '>')
2011 {
2012 *first_unparsed = expression_cstr;
2013 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2014 *final_result = ValueObject::eInvalid;
2015 return ValueObjectSP();
2016 }
2017 expression_cstr++; // skip the -
2018 }
2019 case '.': // or fallthrough from ->
2020 {
2021 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002022 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 +00002023 {
2024 *first_unparsed = expression_cstr;
2025 *reason_to_stop = ValueObject::eDotInsteadOfArrow;
2026 *final_result = ValueObject::eInvalid;
2027 return ValueObjectSP();
2028 }
2029 expression_cstr++; // skip .
2030 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2031 ConstString child_name;
2032 if (!next_separator) // if no other separator just expand this last layer
2033 {
2034 child_name.SetCString (expression_cstr);
2035 root = root->GetChildMemberWithName(child_name, true);
2036 if (root.get()) // we know we are done, so just return
2037 {
2038 *first_unparsed = '\0';
2039 *reason_to_stop = ValueObject::eEndOfString;
2040 *final_result = ValueObject::ePlain;
2041 return root;
2042 }
2043 else
2044 {
2045 *first_unparsed = expression_cstr;
2046 *reason_to_stop = ValueObject::eNoSuchChild;
2047 *final_result = ValueObject::eInvalid;
2048 return ValueObjectSP();
2049 }
2050 }
2051 else // other layers do expand
2052 {
2053 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
2054 root = root->GetChildMemberWithName(child_name, true);
2055 if (root.get()) // store the new root and move on
2056 {
2057 *first_unparsed = next_separator;
2058 *final_result = ValueObject::ePlain;
2059 continue;
2060 }
2061 else
2062 {
2063 *first_unparsed = expression_cstr;
2064 *reason_to_stop = ValueObject::eNoSuchChild;
2065 *final_result = ValueObject::eInvalid;
2066 return ValueObjectSP();
2067 }
2068 }
2069 break;
2070 }
2071 case '[':
2072 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002073 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 +00002074 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002075 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar...
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002076 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002077 if (options.m_no_synthetic_children) // ...only chance left is synthetic
2078 {
2079 *first_unparsed = expression_cstr;
2080 *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2081 *final_result = ValueObject::eInvalid;
2082 return ValueObjectSP();
2083 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002084 }
2085 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2086 {
2087 *first_unparsed = expression_cstr;
2088 *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2089 *final_result = ValueObject::eInvalid;
2090 return ValueObjectSP();
2091 }
2092 }
2093 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2094 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002095 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002096 {
2097 *first_unparsed = expression_cstr;
2098 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2099 *final_result = ValueObject::eInvalid;
2100 return ValueObjectSP();
2101 }
2102 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2103 {
2104 *first_unparsed = expression_cstr+2;
2105 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2106 *final_result = ValueObject::eUnboundedRange;
2107 return root;
2108 }
2109 }
2110 const char *separator_position = ::strchr(expression_cstr+1,'-');
2111 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2112 if (!close_bracket_position) // if there is no ], this is a syntax error
2113 {
2114 *first_unparsed = expression_cstr;
2115 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2116 *final_result = ValueObject::eInvalid;
2117 return ValueObjectSP();
2118 }
2119 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2120 {
2121 char *end = NULL;
2122 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2123 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2124 {
2125 *first_unparsed = expression_cstr;
2126 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2127 *final_result = ValueObject::eInvalid;
2128 return ValueObjectSP();
2129 }
2130 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2131 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002132 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002133 {
2134 *first_unparsed = expression_cstr+2;
2135 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2136 *final_result = ValueObject::eUnboundedRange;
2137 return root;
2138 }
2139 else
2140 {
2141 *first_unparsed = expression_cstr;
2142 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2143 *final_result = ValueObject::eInvalid;
2144 return ValueObjectSP();
2145 }
2146 }
2147 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002148 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002149 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002150 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2151 if (!child_valobj_sp)
2152 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
Enrico Granata27b625e2011-08-09 01:04:56 +00002153 if (!child_valobj_sp)
2154 if (root->HasSyntheticValue() && root->GetSyntheticValue(lldb::eUseSyntheticFilter)->GetNumChildren() > index)
2155 child_valobj_sp = root->GetSyntheticValue(lldb::eUseSyntheticFilter)->GetChildAtIndex(index, true);
Greg Claytondaf515f2011-07-09 20:12:33 +00002156 if (child_valobj_sp)
2157 {
2158 root = child_valobj_sp;
2159 *first_unparsed = end+1; // skip ]
2160 *final_result = ValueObject::ePlain;
2161 continue;
2162 }
2163 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002164 {
2165 *first_unparsed = expression_cstr;
2166 *reason_to_stop = ValueObject::eNoSuchChild;
2167 *final_result = ValueObject::eInvalid;
2168 return ValueObjectSP();
2169 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002170 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002171 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002172 {
2173 if (*what_next == ValueObject::eDereference && // 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 +00002174 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002175 {
2176 Error error;
2177 root = root->Dereference(error);
2178 if (error.Fail() || !root.get())
2179 {
2180 *first_unparsed = expression_cstr;
2181 *reason_to_stop = ValueObject::eDereferencingFailed;
2182 *final_result = ValueObject::eInvalid;
2183 return ValueObjectSP();
2184 }
2185 else
2186 {
2187 *what_next = eNothing;
2188 continue;
2189 }
2190 }
2191 else
2192 {
Enrico Granata27b625e2011-08-09 01:04:56 +00002193 if (ClangASTType::GetMinimumLanguage(root->GetClangAST(),
2194 root->GetClangType()) == lldb::eLanguageTypeObjC
2195 &&
2196 ClangASTContext::IsPointerType(ClangASTType::GetPointeeType(root->GetClangType())) == false
2197 &&
2198 root->HasSyntheticValue()
2199 &&
2200 options.m_no_synthetic_children == false)
2201 {
2202 root = root->GetSyntheticValue(lldb::eUseSyntheticFilter)->GetChildAtIndex(index, true);
2203 }
2204 else
2205 root = root->GetSyntheticArrayMemberFromPointer(index, true);
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002206 if (!root.get())
2207 {
2208 *first_unparsed = expression_cstr;
2209 *reason_to_stop = ValueObject::eNoSuchChild;
2210 *final_result = ValueObject::eInvalid;
2211 return ValueObjectSP();
2212 }
2213 else
2214 {
2215 *first_unparsed = end+1; // skip ]
2216 *final_result = ValueObject::ePlain;
2217 continue;
2218 }
2219 }
2220 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002221 else if (ClangASTContext::IsScalarType(root_clang_type))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002222 {
2223 root = root->GetSyntheticBitFieldChild(index, index, true);
2224 if (!root.get())
2225 {
2226 *first_unparsed = expression_cstr;
2227 *reason_to_stop = ValueObject::eNoSuchChild;
2228 *final_result = ValueObject::eInvalid;
2229 return ValueObjectSP();
2230 }
2231 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2232 {
2233 *first_unparsed = end+1; // skip ]
2234 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2235 *final_result = ValueObject::eBitfield;
2236 return root;
2237 }
2238 }
Enrico Granata27b625e2011-08-09 01:04:56 +00002239 else if (root->HasSyntheticValue() && options.m_no_synthetic_children)
2240 {
2241 root = root->GetSyntheticValue(lldb::eUseSyntheticFilter)->GetChildAtIndex(index, true);
2242 if (!root.get())
2243 {
2244 *first_unparsed = expression_cstr;
2245 *reason_to_stop = ValueObject::eNoSuchChild;
2246 *final_result = ValueObject::eInvalid;
2247 return ValueObjectSP();
2248 }
2249 }
2250 else
2251 {
2252 *first_unparsed = expression_cstr;
2253 *reason_to_stop = ValueObject::eNoSuchChild;
2254 *final_result = ValueObject::eInvalid;
2255 return ValueObjectSP();
2256 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002257 }
2258 else // we have a low and a high index
2259 {
2260 char *end = NULL;
2261 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2262 if (!end || end != separator_position) // if something weird is in our way return an error
2263 {
2264 *first_unparsed = expression_cstr;
2265 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2266 *final_result = ValueObject::eInvalid;
2267 return ValueObjectSP();
2268 }
2269 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2270 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2271 {
2272 *first_unparsed = expression_cstr;
2273 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2274 *final_result = ValueObject::eInvalid;
2275 return ValueObjectSP();
2276 }
2277 if (index_lower > index_higher) // swap indices if required
2278 {
2279 unsigned long temp = index_lower;
2280 index_lower = index_higher;
2281 index_higher = temp;
2282 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002283 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002284 {
2285 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2286 if (!root.get())
2287 {
2288 *first_unparsed = expression_cstr;
2289 *reason_to_stop = ValueObject::eNoSuchChild;
2290 *final_result = ValueObject::eInvalid;
2291 return ValueObjectSP();
2292 }
2293 else
2294 {
2295 *first_unparsed = end+1; // skip ]
2296 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2297 *final_result = ValueObject::eBitfield;
2298 return root;
2299 }
2300 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002301 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 Granatafc7a7f32011-07-08 02:51:01 +00002302 *what_next == ValueObject::eDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002303 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002304 {
2305 Error error;
2306 root = root->Dereference(error);
2307 if (error.Fail() || !root.get())
2308 {
2309 *first_unparsed = expression_cstr;
2310 *reason_to_stop = ValueObject::eDereferencingFailed;
2311 *final_result = ValueObject::eInvalid;
2312 return ValueObjectSP();
2313 }
2314 else
2315 {
2316 *what_next = ValueObject::eNothing;
2317 continue;
2318 }
2319 }
2320 else
2321 {
2322 *first_unparsed = expression_cstr;
2323 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2324 *final_result = ValueObject::eBoundedRange;
2325 return root;
2326 }
2327 }
2328 break;
2329 }
2330 default: // some non-separator is in the way
2331 {
2332 *first_unparsed = expression_cstr;
2333 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2334 *final_result = ValueObject::eInvalid;
2335 return ValueObjectSP();
2336 break;
2337 }
2338 }
2339 }
2340}
2341
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002342int
2343ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2344 const char** first_unparsed,
2345 lldb::ValueObjectSP root,
2346 lldb::ValueObjectListSP& list,
2347 ExpressionPathScanEndReason* reason_to_stop,
2348 ExpressionPathEndResultType* final_result,
2349 const GetValueForExpressionPathOptions& options,
2350 ExpressionPathAftermath* what_next)
2351{
2352 if (!root.get())
2353 return 0;
2354
2355 *first_unparsed = expression_cstr;
2356
2357 while (true)
2358 {
2359
2360 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2361
2362 lldb::clang_type_t root_clang_type = root->GetClangType();
2363 lldb::clang_type_t pointee_clang_type;
2364 Flags root_clang_type_info,pointee_clang_type_info;
2365
2366 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2367 if (pointee_clang_type)
2368 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2369
2370 if (!expression_cstr || *expression_cstr == '\0')
2371 {
2372 *reason_to_stop = ValueObject::eEndOfString;
2373 list->Append(root);
2374 return 1;
2375 }
2376
2377 switch (*expression_cstr)
2378 {
2379 case '[':
2380 {
2381 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2382 {
2383 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2384 {
2385 *first_unparsed = expression_cstr;
2386 *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2387 *final_result = ValueObject::eInvalid;
2388 return 0;
2389 }
2390 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2391 {
2392 *first_unparsed = expression_cstr;
2393 *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2394 *final_result = ValueObject::eInvalid;
2395 return 0;
2396 }
2397 }
2398 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2399 {
2400 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2401 {
2402 *first_unparsed = expression_cstr;
2403 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2404 *final_result = ValueObject::eInvalid;
2405 return 0;
2406 }
2407 else // expand this into list
2408 {
2409 int max_index = root->GetNumChildren() - 1;
2410 for (int index = 0; index < max_index; index++)
2411 {
2412 ValueObjectSP child =
2413 root->GetChildAtIndex(index, true);
2414 list->Append(child);
2415 }
2416 *first_unparsed = expression_cstr+2;
2417 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2418 *final_result = ValueObject::eValueObjectList;
2419 return max_index; // tell me number of items I added to the VOList
2420 }
2421 }
2422 const char *separator_position = ::strchr(expression_cstr+1,'-');
2423 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2424 if (!close_bracket_position) // if there is no ], this is a syntax error
2425 {
2426 *first_unparsed = expression_cstr;
2427 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2428 *final_result = ValueObject::eInvalid;
2429 return 0;
2430 }
2431 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2432 {
2433 char *end = NULL;
2434 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2435 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2436 {
2437 *first_unparsed = expression_cstr;
2438 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2439 *final_result = ValueObject::eInvalid;
2440 return 0;
2441 }
2442 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2443 {
2444 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2445 {
2446 int max_index = root->GetNumChildren() - 1;
2447 for (int index = 0; index < max_index; index++)
2448 {
2449 ValueObjectSP child =
2450 root->GetChildAtIndex(index, true);
2451 list->Append(child);
2452 }
2453 *first_unparsed = expression_cstr+2;
2454 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2455 *final_result = ValueObject::eValueObjectList;
2456 return max_index; // tell me number of items I added to the VOList
2457 }
2458 else
2459 {
2460 *first_unparsed = expression_cstr;
2461 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2462 *final_result = ValueObject::eInvalid;
2463 return 0;
2464 }
2465 }
2466 // from here on we do have a valid index
2467 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2468 {
2469 root = root->GetChildAtIndex(index, true);
2470 if (!root.get())
2471 {
2472 *first_unparsed = expression_cstr;
2473 *reason_to_stop = ValueObject::eNoSuchChild;
2474 *final_result = ValueObject::eInvalid;
2475 return 0;
2476 }
2477 else
2478 {
2479 list->Append(root);
2480 *first_unparsed = end+1; // skip ]
2481 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2482 *final_result = ValueObject::eValueObjectList;
2483 return 1;
2484 }
2485 }
2486 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2487 {
2488 if (*what_next == ValueObject::eDereference && // 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
2489 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2490 {
2491 Error error;
2492 root = root->Dereference(error);
2493 if (error.Fail() || !root.get())
2494 {
2495 *first_unparsed = expression_cstr;
2496 *reason_to_stop = ValueObject::eDereferencingFailed;
2497 *final_result = ValueObject::eInvalid;
2498 return 0;
2499 }
2500 else
2501 {
2502 *what_next = eNothing;
2503 continue;
2504 }
2505 }
2506 else
2507 {
2508 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2509 if (!root.get())
2510 {
2511 *first_unparsed = expression_cstr;
2512 *reason_to_stop = ValueObject::eNoSuchChild;
2513 *final_result = ValueObject::eInvalid;
2514 return 0;
2515 }
2516 else
2517 {
2518 list->Append(root);
2519 *first_unparsed = end+1; // skip ]
2520 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2521 *final_result = ValueObject::eValueObjectList;
2522 return 1;
2523 }
2524 }
2525 }
2526 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2527 {
2528 root = root->GetSyntheticBitFieldChild(index, index, true);
2529 if (!root.get())
2530 {
2531 *first_unparsed = expression_cstr;
2532 *reason_to_stop = ValueObject::eNoSuchChild;
2533 *final_result = ValueObject::eInvalid;
2534 return 0;
2535 }
2536 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2537 {
2538 list->Append(root);
2539 *first_unparsed = end+1; // skip ]
2540 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2541 *final_result = ValueObject::eValueObjectList;
2542 return 1;
2543 }
2544 }
2545 }
2546 else // we have a low and a high index
2547 {
2548 char *end = NULL;
2549 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2550 if (!end || end != separator_position) // if something weird is in our way return an error
2551 {
2552 *first_unparsed = expression_cstr;
2553 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2554 *final_result = ValueObject::eInvalid;
2555 return 0;
2556 }
2557 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2558 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2559 {
2560 *first_unparsed = expression_cstr;
2561 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2562 *final_result = ValueObject::eInvalid;
2563 return 0;
2564 }
2565 if (index_lower > index_higher) // swap indices if required
2566 {
2567 unsigned long temp = index_lower;
2568 index_lower = index_higher;
2569 index_higher = temp;
2570 }
2571 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2572 {
2573 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2574 if (!root.get())
2575 {
2576 *first_unparsed = expression_cstr;
2577 *reason_to_stop = ValueObject::eNoSuchChild;
2578 *final_result = ValueObject::eInvalid;
2579 return 0;
2580 }
2581 else
2582 {
2583 list->Append(root);
2584 *first_unparsed = end+1; // skip ]
2585 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2586 *final_result = ValueObject::eValueObjectList;
2587 return 1;
2588 }
2589 }
2590 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
2591 *what_next == ValueObject::eDereference &&
2592 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2593 {
2594 Error error;
2595 root = root->Dereference(error);
2596 if (error.Fail() || !root.get())
2597 {
2598 *first_unparsed = expression_cstr;
2599 *reason_to_stop = ValueObject::eDereferencingFailed;
2600 *final_result = ValueObject::eInvalid;
2601 return 0;
2602 }
2603 else
2604 {
2605 *what_next = ValueObject::eNothing;
2606 continue;
2607 }
2608 }
2609 else
2610 {
Johnny Chen44805302011-07-19 19:48:13 +00002611 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002612 index <= index_higher; index++)
2613 {
2614 ValueObjectSP child =
2615 root->GetChildAtIndex(index, true);
2616 list->Append(child);
2617 }
2618 *first_unparsed = end+1;
2619 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2620 *final_result = ValueObject::eValueObjectList;
2621 return index_higher-index_lower+1; // tell me number of items I added to the VOList
2622 }
2623 }
2624 break;
2625 }
2626 default: // some non-[ separator, or something entirely wrong, is in the way
2627 {
2628 *first_unparsed = expression_cstr;
2629 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2630 *final_result = ValueObject::eInvalid;
2631 return 0;
2632 break;
2633 }
2634 }
2635 }
2636}
2637
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002638void
Greg Clayton1d3afba2010-10-05 00:00:42 +00002639ValueObject::DumpValueObject
2640(
2641 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00002642 ValueObject *valobj,
2643 const char *root_valobj_name,
2644 uint32_t ptr_depth,
2645 uint32_t curr_depth,
2646 uint32_t max_depth,
2647 bool show_types,
2648 bool show_location,
2649 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00002650 lldb::DynamicValueType use_dynamic,
Enrico Granatad55546b2011-07-22 00:16:08 +00002651 bool use_synth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002652 bool scope_already_checked,
Enrico Granata0c5ef692011-07-16 01:22:04 +00002653 bool flat_output,
2654 uint32_t omit_summary_depth
Greg Clayton1d3afba2010-10-05 00:00:42 +00002655)
2656{
Greg Clayton007d5be2011-05-30 00:49:24 +00002657 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002658 {
Enrico Granatac3e320a2011-08-02 17:27:39 +00002659 bool update_success = valobj->UpdateValueIfNeeded (use_dynamic, true);
Greg Clayton007d5be2011-05-30 00:49:24 +00002660
2661 if (update_success && use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00002662 {
Jim Ingham2837b762011-05-04 03:43:18 +00002663 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00002664 if (dynamic_value)
2665 valobj = dynamic_value;
2666 }
2667
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002668 clang_type_t clang_type = valobj->GetClangType();
2669
Greg Clayton73b472d2010-10-27 03:32:59 +00002670 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002671 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00002672 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
2673 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002674
2675 const bool print_valobj = flat_output == false || has_value;
2676
2677 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002678 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002679 if (show_location)
2680 {
Jim Ingham6035b672011-03-31 00:19:25 +00002681 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002682 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002683
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002684 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00002685
Greg Clayton7c8a9662010-11-02 01:50:16 +00002686 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00002687 if (show_types || (curr_depth == 0 && !flat_output))
Enrico Granatac3e320a2011-08-02 17:27:39 +00002688 {
Enrico Granata9910bc82011-08-03 02:18:51 +00002689 const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
2690 s.Printf("(%s", typeName);
2691 // only show dynamic types if the user really wants to see types
2692 if (show_types && use_dynamic != lldb::eNoDynamicValues &&
2693 (/*strstr(typeName, "id") == typeName ||*/
2694 ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == lldb::eLanguageTypeObjC))
Enrico Granatac3e320a2011-08-02 17:27:39 +00002695 {
2696 Process* process = valobj->GetUpdatePoint().GetProcessSP().get();
2697 if (process == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00002698 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002699 else
2700 {
2701 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
2702 if (runtime == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00002703 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002704 else
2705 {
2706 ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
2707 if (!runtime->IsValidISA(isa))
Enrico Granata9910bc82011-08-03 02:18:51 +00002708 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002709 else
2710 s.Printf(", dynamic type: %s) ",
2711 runtime->GetActualTypeName(isa).GetCString());
2712 }
2713 }
2714 }
2715 else
2716 s.Printf(") ");
2717 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002718
Greg Clayton1d3afba2010-10-05 00:00:42 +00002719
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002720 if (flat_output)
2721 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002722 // If we are showing types, also qualify the C++ base classes
2723 const bool qualify_cxx_base_classes = show_types;
2724 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002725 s.PutCString(" =");
2726 }
2727 else
2728 {
2729 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
2730 s.Printf ("%s =", name_cstr);
2731 }
2732
Jim Ingham6035b672011-03-31 00:19:25 +00002733 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002734 {
Greg Clayton007d5be2011-05-30 00:49:24 +00002735 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002736 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002737 }
2738
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002739 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00002740 const char *sum_cstr = NULL;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002741 SummaryFormat* entry = valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002742
Enrico Granata0c5ef692011-07-16 01:22:04 +00002743 if (omit_summary_depth > 0)
2744 entry = NULL;
2745
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002746 if (err_cstr == NULL)
2747 {
Jim Ingham6035b672011-03-31 00:19:25 +00002748 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002749 err_cstr = valobj->GetError().AsCString();
2750 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002751
2752 if (err_cstr)
2753 {
Greg Clayton007d5be2011-05-30 00:49:24 +00002754 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00002755 }
2756 else
2757 {
Greg Clayton73b472d2010-10-27 03:32:59 +00002758 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002759 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002760 {
Enrico Granata4becb372011-06-29 22:27:15 +00002761
Enrico Granata0c5ef692011-07-16 01:22:04 +00002762 sum_cstr = (omit_summary_depth == 0) ? valobj->GetSummaryAsCString() : NULL;
Greg Clayton1d3afba2010-10-05 00:00:42 +00002763
Enrico Granata4becb372011-06-29 22:27:15 +00002764 // We must calculate this value in realtime because entry might alter this variable's value
2765 // (e.g. by saying ${var%fmt}) and render precached values useless
2766 if (val_cstr && (!entry || entry->DoesPrintValue() || !sum_cstr))
2767 s.Printf(" %s", valobj->GetValueAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002768
Enrico Granata9dd75c82011-07-15 23:30:15 +00002769 if (sum_cstr)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002770 {
2771 // for some reason, using %@ (ObjC description) in a summary string, makes
2772 // us believe we need to reset ourselves, thus invalidating the content of
2773 // sum_cstr. Thus, IF we had a valid sum_cstr before, but it is now empty
2774 // let us recalculate it!
2775 if (sum_cstr[0] == '\0')
2776 s.Printf(" %s", valobj->GetSummaryAsCString());
2777 else
2778 s.Printf(" %s", sum_cstr);
2779 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002780
2781 if (use_objc)
2782 {
Jim Ingham6035b672011-03-31 00:19:25 +00002783 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002784 if (object_desc)
2785 s.Printf(" %s\n", object_desc);
2786 else
Sean Callanan672ad942010-10-23 00:18:49 +00002787 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002788 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00002789 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002790 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002791
2792 if (curr_depth < max_depth)
2793 {
Greg Clayton73b472d2010-10-27 03:32:59 +00002794 // We will show children for all concrete types. We won't show
2795 // pointer contents unless a pointer depth has been specified.
2796 // We won't reference contents unless the reference is the
2797 // root object (depth of zero).
2798 bool print_children = true;
2799
2800 // Use a new temporary pointer depth in case we override the
2801 // current pointer depth below...
2802 uint32_t curr_ptr_depth = ptr_depth;
2803
2804 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
2805 if (is_ptr || is_ref)
2806 {
2807 // We have a pointer or reference whose value is an address.
2808 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00002809 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00002810 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
2811 print_children = false;
2812
2813 else if (is_ref && curr_depth == 0)
2814 {
2815 // If this is the root object (depth is zero) that we are showing
2816 // and it is a reference, and no pointer depth has been supplied
2817 // print out what it references. Don't do this at deeper depths
2818 // otherwise we can end up with infinite recursion...
2819 curr_ptr_depth = 1;
2820 }
2821
2822 if (curr_ptr_depth == 0)
2823 print_children = false;
2824 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002825
Enrico Granata0a3958e2011-07-02 00:25:22 +00002826 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00002827 {
Enrico Granatad55546b2011-07-22 00:16:08 +00002828 ValueObjectSP synth_vobj = valobj->GetSyntheticValue(use_synth ?
2829 lldb::eUseSyntheticFilter :
2830 lldb::eNoSyntheticFilter);
2831 const uint32_t num_children = synth_vobj->GetNumChildren();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002832 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002833 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002834 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002835 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002836 if (print_valobj)
2837 s.EOL();
2838 }
2839 else
2840 {
2841 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002842 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002843 s.IndentMore();
2844 }
2845
2846 for (uint32_t idx=0; idx<num_children; ++idx)
2847 {
Enrico Granatad55546b2011-07-22 00:16:08 +00002848 ValueObjectSP child_sp(synth_vobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002849 if (child_sp.get())
2850 {
2851 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002852 child_sp.get(),
2853 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00002854 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002855 curr_depth + 1,
2856 max_depth,
2857 show_types,
2858 show_location,
2859 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00002860 use_dynamic,
Enrico Granatad55546b2011-07-22 00:16:08 +00002861 use_synth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002862 true,
Enrico Granata0c5ef692011-07-16 01:22:04 +00002863 flat_output,
2864 omit_summary_depth > 1 ? omit_summary_depth - 1 : 0);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002865 }
2866 }
2867
2868 if (!flat_output)
2869 {
2870 s.IndentLess();
2871 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00002872 }
2873 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002874 else if (has_children)
2875 {
2876 // Aggregate, no children...
2877 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00002878 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002879 }
2880 else
2881 {
2882 if (print_valobj)
2883 s.EOL();
2884 }
2885
Greg Clayton1d3afba2010-10-05 00:00:42 +00002886 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002887 else
2888 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00002889 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00002890 }
2891 }
2892 else
2893 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002894 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002895 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002896 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00002897 }
2898 }
2899 }
2900 }
2901}
2902
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002903
2904ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00002905ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002906{
2907 ValueObjectSP valobj_sp;
2908
Enrico Granatac3e320a2011-08-02 17:27:39 +00002909 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002910 {
Jim Ingham6035b672011-03-31 00:19:25 +00002911 ExecutionContextScope *exe_scope = GetExecutionContextScope();
2912 if (exe_scope)
2913 {
2914 ExecutionContext exe_ctx;
2915 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002916
Jim Ingham6035b672011-03-31 00:19:25 +00002917 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002918
Jim Ingham6035b672011-03-31 00:19:25 +00002919 DataExtractor data;
2920 data.SetByteOrder (m_data.GetByteOrder());
2921 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002922
Greg Clayton644247c2011-07-07 01:59:51 +00002923 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002924
Jim Ingham58b59f92011-04-22 23:53:53 +00002925 valobj_sp = ValueObjectConstResult::Create (exe_scope,
2926 ast,
2927 GetClangType(),
2928 name,
2929 data);
Jim Ingham6035b672011-03-31 00:19:25 +00002930 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002931 }
Jim Ingham6035b672011-03-31 00:19:25 +00002932
2933 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002934 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002935 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002936 }
2937 return valobj_sp;
2938}
2939
2940lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00002941ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002942{
Jim Ingham58b59f92011-04-22 23:53:53 +00002943 if (m_deref_valobj)
2944 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00002945
Greg Clayton54979cd2010-12-15 05:08:08 +00002946 const bool is_pointer_type = IsPointerType();
2947 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002948 {
2949 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00002950 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002951
2952 std::string child_name_str;
2953 uint32_t child_byte_size = 0;
2954 int32_t child_byte_offset = 0;
2955 uint32_t child_bitfield_bit_size = 0;
2956 uint32_t child_bitfield_bit_offset = 0;
2957 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00002958 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002959 const bool transparent_pointers = false;
2960 clang::ASTContext *clang_ast = GetClangAST();
2961 clang_type_t clang_type = GetClangType();
2962 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00002963
2964 ExecutionContext exe_ctx;
2965 GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
2966
2967 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
2968 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002969 GetName().GetCString(),
2970 clang_type,
2971 0,
2972 transparent_pointers,
2973 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00002974 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002975 child_name_str,
2976 child_byte_size,
2977 child_byte_offset,
2978 child_bitfield_bit_size,
2979 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00002980 child_is_base_class,
2981 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00002982 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002983 {
2984 ConstString child_name;
2985 if (!child_name_str.empty())
2986 child_name.SetCString (child_name_str.c_str());
2987
Jim Ingham58b59f92011-04-22 23:53:53 +00002988 m_deref_valobj = new ValueObjectChild (*this,
2989 clang_ast,
2990 child_clang_type,
2991 child_name,
2992 child_byte_size,
2993 child_byte_offset,
2994 child_bitfield_bit_size,
2995 child_bitfield_bit_offset,
2996 child_is_base_class,
2997 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002998 }
2999 }
Greg Clayton54979cd2010-12-15 05:08:08 +00003000
Jim Ingham58b59f92011-04-22 23:53:53 +00003001 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00003002 {
3003 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00003004 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00003005 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003006 else
3007 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003008 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003009 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003010
3011 if (is_pointer_type)
3012 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
3013 else
3014 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00003015 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003016 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003017}
3018
Jim Ingham78a685a2011-04-16 00:01:13 +00003019lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00003020ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003021{
Jim Ingham78a685a2011-04-16 00:01:13 +00003022 if (m_addr_of_valobj_sp)
3023 return m_addr_of_valobj_sp;
3024
Greg Claytone0d378b2011-03-24 21:19:54 +00003025 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003026 const bool scalar_is_load_address = false;
3027 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00003028 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003029 if (addr != LLDB_INVALID_ADDRESS)
3030 {
3031 switch (address_type)
3032 {
Greg Clayton54979cd2010-12-15 05:08:08 +00003033 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003034 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00003035 {
3036 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003037 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00003038 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
3039 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003040 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00003041
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003042 case eAddressTypeFile:
3043 case eAddressTypeLoad:
3044 case eAddressTypeHost:
3045 {
3046 clang::ASTContext *ast = GetClangAST();
3047 clang_type_t clang_type = GetClangType();
3048 if (ast && clang_type)
3049 {
3050 std::string name (1, '&');
3051 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00003052 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
3053 ast,
3054 ClangASTContext::CreatePointerType (ast, clang_type),
3055 ConstString (name.c_str()),
3056 addr,
3057 eAddressTypeInvalid,
3058 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003059 }
3060 }
3061 break;
3062 }
3063 }
Jim Ingham78a685a2011-04-16 00:01:13 +00003064 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00003065}
3066
Greg Claytonb2dcc362011-05-05 23:32:56 +00003067
3068lldb::ValueObjectSP
3069ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3070{
3071 lldb::ValueObjectSP valobj_sp;
3072 AddressType address_type;
3073 const bool scalar_is_load_address = true;
3074 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
3075
3076 if (ptr_value != LLDB_INVALID_ADDRESS)
3077 {
3078 Address ptr_addr (NULL, ptr_value);
3079
3080 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3081 name,
3082 ptr_addr,
3083 clang_ast_type);
3084 }
3085 return valobj_sp;
3086}
3087
3088lldb::ValueObjectSP
3089ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3090{
3091 lldb::ValueObjectSP valobj_sp;
3092 AddressType address_type;
3093 const bool scalar_is_load_address = true;
3094 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
3095
3096 if (ptr_value != LLDB_INVALID_ADDRESS)
3097 {
3098 Address ptr_addr (NULL, ptr_value);
3099
3100 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3101 name,
3102 ptr_addr,
3103 type_sp);
3104 }
3105 return valobj_sp;
3106}
3107
Jim Ingham6035b672011-03-31 00:19:25 +00003108ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00003109 m_thread_id (LLDB_INVALID_UID),
Jim Ingham4b536182011-08-09 02:12:22 +00003110 m_mod_id ()
Jim Ingham6035b672011-03-31 00:19:25 +00003111{
3112}
3113
3114ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00003115 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003116 m_first_update (true),
Jim Ingham89b61092011-07-06 17:42:14 +00003117 m_thread_id (LLDB_INVALID_THREAD_ID),
Jim Ingham4b536182011-08-09 02:12:22 +00003118 m_mod_id ()
Stephen Wilson71c21d12011-04-11 19:41:40 +00003119
Jim Ingham6035b672011-03-31 00:19:25 +00003120{
3121 ExecutionContext exe_ctx;
3122 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
3123 // and if so we want to cache that not the original.
3124 if (exe_scope)
3125 exe_scope->CalculateExecutionContext(exe_ctx);
3126 if (exe_ctx.target != NULL)
3127 {
3128 m_target_sp = exe_ctx.target->GetSP();
3129
3130 if (exe_ctx.process == NULL)
3131 m_process_sp = exe_ctx.target->GetProcessSP();
3132 else
3133 m_process_sp = exe_ctx.process->GetSP();
3134
3135 if (m_process_sp != NULL)
3136 {
Jim Ingham4b536182011-08-09 02:12:22 +00003137 m_mod_id = m_process_sp->GetModID();
3138
Jim Ingham6035b672011-03-31 00:19:25 +00003139 Thread *thread = NULL;
3140
3141 if (exe_ctx.thread == NULL)
3142 {
3143 if (use_selected)
3144 {
3145 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
3146 if (thread)
3147 computed_exe_scope = thread;
3148 }
3149 }
3150 else
3151 thread = exe_ctx.thread;
3152
3153 if (thread != NULL)
3154 {
3155 m_thread_id = thread->GetIndexID();
3156 if (exe_ctx.frame == NULL)
3157 {
3158 if (use_selected)
3159 {
3160 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
3161 if (frame)
3162 {
3163 m_stack_id = frame->GetStackID();
3164 computed_exe_scope = frame;
3165 }
3166 }
3167 }
3168 else
3169 m_stack_id = exe_ctx.frame->GetStackID();
3170 }
3171 }
3172 }
3173 m_exe_scope = computed_exe_scope;
3174}
3175
3176ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
3177 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003178 m_needs_update(true),
3179 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00003180 m_target_sp (rhs.m_target_sp),
3181 m_process_sp (rhs.m_process_sp),
3182 m_thread_id (rhs.m_thread_id),
3183 m_stack_id (rhs.m_stack_id),
Jim Ingham4b536182011-08-09 02:12:22 +00003184 m_mod_id ()
Jim Ingham6035b672011-03-31 00:19:25 +00003185{
3186}
3187
3188ValueObject::EvaluationPoint::~EvaluationPoint ()
3189{
3190}
3191
3192ExecutionContextScope *
3193ValueObject::EvaluationPoint::GetExecutionContextScope ()
3194{
3195 // We have to update before giving out the scope, or we could be handing out stale pointers.
3196 SyncWithProcessState();
3197
3198 return m_exe_scope;
3199}
3200
3201// This function checks the EvaluationPoint against the current process state. If the current
3202// state matches the evaluation point, or the evaluation point is already invalid, then we return
3203// false, meaning "no change". If the current state is different, we update our state, and return
3204// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3205// future calls to NeedsUpdate will return true.
3206
3207bool
3208ValueObject::EvaluationPoint::SyncWithProcessState()
3209{
3210 // If we're already invalid, we don't need to do anything, and nothing has changed:
Jim Ingham4b536182011-08-09 02:12:22 +00003211 if (!m_mod_id.IsValid())
Jim Ingham6035b672011-03-31 00:19:25 +00003212 {
3213 // Can't update with an invalid state.
3214 m_needs_update = false;
3215 return false;
3216 }
3217
3218 // If we don't have a process nothing can change.
3219 if (!m_process_sp)
3220 return false;
3221
3222 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham4b536182011-08-09 02:12:22 +00003223 ProcessModID current_mod_id = m_process_sp->GetModID();
3224
3225 if (m_mod_id == current_mod_id)
Jim Ingham6035b672011-03-31 00:19:25 +00003226 return false;
3227
Jim Ingham78a685a2011-04-16 00:01:13 +00003228 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3229 // In either case, we aren't going to be able to sync with the process state.
Jim Ingham4b536182011-08-09 02:12:22 +00003230 if (current_mod_id.GetStopID() == 0)
Jim Ingham78a685a2011-04-16 00:01:13 +00003231 return false;
3232
Jim Ingham4b536182011-08-09 02:12:22 +00003233 m_mod_id = current_mod_id;
Jim Ingham6035b672011-03-31 00:19:25 +00003234 m_needs_update = true;
3235 m_exe_scope = m_process_sp.get();
3236
3237 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
3238 // doesn't, mark ourselves as invalid.
3239
3240 if (m_thread_id != LLDB_INVALID_THREAD_ID)
3241 {
3242 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
3243 if (our_thread == NULL)
Greg Clayton262f80d2011-07-06 16:49:27 +00003244 {
Jim Ingham89b61092011-07-06 17:42:14 +00003245 SetInvalid();
Greg Clayton262f80d2011-07-06 16:49:27 +00003246 }
Jim Ingham6035b672011-03-31 00:19:25 +00003247 else
3248 {
3249 m_exe_scope = our_thread;
3250
3251 if (m_stack_id.IsValid())
3252 {
3253 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
3254 if (our_frame == NULL)
3255 SetInvalid();
3256 else
3257 m_exe_scope = our_frame;
3258 }
3259 }
3260 }
3261 return true;
3262}
3263
Jim Ingham61be0902011-05-02 18:13:59 +00003264void
3265ValueObject::EvaluationPoint::SetUpdated ()
3266{
3267 m_first_update = false;
3268 m_needs_update = false;
3269 if (m_process_sp)
Jim Ingham4b536182011-08-09 02:12:22 +00003270 {
3271 m_mod_id = m_process_sp->GetModID();
3272 }
Jim Ingham61be0902011-05-02 18:13:59 +00003273}
3274
3275
Jim Ingham6035b672011-03-31 00:19:25 +00003276bool
3277ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3278{
3279 if (!IsValid())
3280 return false;
3281
3282 bool needs_update = false;
3283 m_exe_scope = NULL;
3284
3285 // The target has to be non-null, and the
3286 Target *target = exe_scope->CalculateTarget();
3287 if (target != NULL)
3288 {
3289 Target *old_target = m_target_sp.get();
3290 assert (target == old_target);
3291 Process *process = exe_scope->CalculateProcess();
3292 if (process != NULL)
3293 {
3294 // FOR NOW - assume you can't update variable objects across process boundaries.
3295 Process *old_process = m_process_sp.get();
3296 assert (process == old_process);
Jim Ingham4b536182011-08-09 02:12:22 +00003297 ProcessModID current_mod_id = process->GetModID();
3298 if (m_mod_id != current_mod_id)
Jim Ingham6035b672011-03-31 00:19:25 +00003299 {
3300 needs_update = true;
Jim Ingham4b536182011-08-09 02:12:22 +00003301 m_mod_id = current_mod_id;
Jim Ingham6035b672011-03-31 00:19:25 +00003302 }
3303 // See if we're switching the thread or stack context. If no thread is given, this is
3304 // being evaluated in a global context.
3305 Thread *thread = exe_scope->CalculateThread();
3306 if (thread != NULL)
3307 {
3308 lldb::user_id_t new_thread_index = thread->GetIndexID();
3309 if (new_thread_index != m_thread_id)
3310 {
3311 needs_update = true;
3312 m_thread_id = new_thread_index;
3313 m_stack_id.Clear();
3314 }
3315
3316 StackFrame *new_frame = exe_scope->CalculateStackFrame();
3317 if (new_frame != NULL)
3318 {
3319 if (new_frame->GetStackID() != m_stack_id)
3320 {
3321 needs_update = true;
3322 m_stack_id = new_frame->GetStackID();
3323 }
3324 }
3325 else
3326 {
3327 m_stack_id.Clear();
3328 needs_update = true;
3329 }
3330 }
3331 else
3332 {
3333 // If this had been given a thread, and now there is none, we should update.
3334 // Otherwise we don't have to do anything.
3335 if (m_thread_id != LLDB_INVALID_UID)
3336 {
3337 m_thread_id = LLDB_INVALID_UID;
3338 m_stack_id.Clear();
3339 needs_update = true;
3340 }
3341 }
3342 }
3343 else
3344 {
3345 // If there is no process, then we don't need to update anything.
3346 // But if we're switching from having a process to not, we should try to update.
3347 if (m_process_sp.get() != NULL)
3348 {
3349 needs_update = true;
3350 m_process_sp.reset();
3351 m_thread_id = LLDB_INVALID_UID;
3352 m_stack_id.Clear();
3353 }
3354 }
3355 }
3356 else
3357 {
3358 // If there's no target, nothing can change so we don't need to update anything.
3359 // But if we're switching from having a target to not, we should try to update.
3360 if (m_target_sp.get() != NULL)
3361 {
3362 needs_update = true;
3363 m_target_sp.reset();
3364 m_process_sp.reset();
3365 m_thread_id = LLDB_INVALID_UID;
3366 m_stack_id.Clear();
3367 }
3368 }
3369 if (!m_needs_update)
3370 m_needs_update = needs_update;
3371
3372 return needs_update;
3373}
Enrico Granataf2bbf712011-07-15 02:26:42 +00003374
3375void
3376ValueObject::ClearUserVisibleData()
3377{
3378 m_location_str.clear();
3379 m_value_str.clear();
3380 m_summary_str.clear();
3381 m_object_desc_str.clear();
Johnny Chen44805302011-07-19 19:48:13 +00003382}