blob: ba030c4e35a9f29fbea6c8b3b2644584bb5b8bea [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(),
Enrico Granata9df29e32011-07-19 20:57:44 +000085 m_user_id_of_forced_summary(0),
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(),
Enrico Granata9df29e32011-07-19 20:57:44 +0000131 m_user_id_of_forced_summary(0),
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());
Enrico Granataf9fa6ee2011-07-12 00:18:11 +0000226 if (HasCustomSummaryFormat() && m_update_point.GetUpdateID() != m_user_id_of_forced_summary)
227 {
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;
Greg Clayton644247c2011-07-07 01:59:51 +00001239 m_value.GetScalar() = ull_val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240 // Limit the bytes in our m_data appropriately.
1241 m_value.GetScalar().GetData (m_data, byte_size);
1242 }
1243 break;
1244
1245 case eEncodingSint:
1246 if (byte_size > sizeof(long long))
1247 {
1248 return false;
1249 }
1250 else
1251 {
1252 long long sll_val = strtoll(value_str, &end, 0);
1253 if (end && *end != '\0')
1254 return false;
Greg Clayton644247c2011-07-07 01:59:51 +00001255 m_value.GetScalar() = sll_val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001256 // Limit the bytes in our m_data appropriately.
1257 m_value.GetScalar().GetData (m_data, byte_size);
1258 }
1259 break;
1260
1261 case eEncodingIEEE754:
1262 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001263 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +00001264 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001265 if (dst != NULL)
1266 {
1267 // We are decoding a float into host byte order below, so make
1268 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +00001269 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001270 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
1271 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +00001272 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001273 value_str,
1274 dst,
1275 byte_size);
1276
1277 if (converted_byte_size == byte_size)
1278 {
1279 }
1280 }
1281 }
1282 break;
1283
1284 case eEncodingVector:
1285 return false;
1286
1287 default:
1288 return false;
1289 }
1290
1291 // If we have made it here the value is in m_data and we should write it
1292 // out to the target
1293 return Write ();
1294}
1295
1296bool
1297ValueObject::Write ()
1298{
1299 // Clear the update ID so the next time we try and read the value
1300 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +00001301 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001302
1303 // TODO: when Value has a method to write a value back, call it from here.
1304 return false;
1305
1306}
1307
Jim Ingham5a369122010-09-28 01:25:32 +00001308lldb::LanguageType
1309ValueObject::GetObjectRuntimeLanguage ()
1310{
Enrico Granatac3e320a2011-08-02 17:27:39 +00001311 return ClangASTType::GetMinimumLanguage (GetClangAST(),
1312 GetClangType());
Jim Ingham5a369122010-09-28 01:25:32 +00001313}
1314
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001315void
Jim Ingham58b59f92011-04-22 23:53:53 +00001316ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001317{
Jim Ingham58b59f92011-04-22 23:53:53 +00001318 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319}
1320
1321ValueObjectSP
1322ValueObject::GetSyntheticChild (const ConstString &key) const
1323{
1324 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001325 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001326 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001327 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001328 return synthetic_child_sp;
1329}
1330
1331bool
1332ValueObject::IsPointerType ()
1333{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001334 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001335}
1336
Jim Inghamb7603bb2011-03-18 00:05:18 +00001337bool
Greg Claytondaf515f2011-07-09 20:12:33 +00001338ValueObject::IsArrayType ()
1339{
1340 return ClangASTContext::IsArrayType (GetClangType());
1341}
1342
1343bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001344ValueObject::IsScalarType ()
1345{
1346 return ClangASTContext::IsScalarType (GetClangType());
1347}
1348
1349bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001350ValueObject::IsIntegerType (bool &is_signed)
1351{
1352 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1353}
Greg Clayton73b472d2010-10-27 03:32:59 +00001354
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001355bool
1356ValueObject::IsPointerOrReferenceType ()
1357{
Greg Clayton007d5be2011-05-30 00:49:24 +00001358 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1359}
1360
1361bool
1362ValueObject::IsPossibleCPlusPlusDynamicType ()
1363{
1364 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001365}
1366
Greg Claytondea8cb42011-06-29 22:09:02 +00001367bool
1368ValueObject::IsPossibleDynamicType ()
1369{
1370 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1371}
1372
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001373ValueObjectSP
1374ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1375{
1376 ValueObjectSP synthetic_child_sp;
1377 if (IsPointerType ())
1378 {
1379 char index_str[64];
1380 snprintf(index_str, sizeof(index_str), "[%i]", index);
1381 ConstString index_const_str(index_str);
1382 // Check if we have already created a synthetic array member in this
1383 // valid object. If we have we will re-use it.
1384 synthetic_child_sp = GetSyntheticChild (index_const_str);
1385 if (!synthetic_child_sp)
1386 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001387 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001388 // We haven't made a synthetic array member for INDEX yet, so
1389 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001390 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001391
1392 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001393 if (synthetic_child)
1394 {
1395 AddSyntheticChild(index_const_str, synthetic_child);
1396 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001397 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata0a3958e2011-07-02 00:25:22 +00001398 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001399 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001400 }
1401 }
1402 return synthetic_child_sp;
1403}
Jim Ingham22777012010-09-23 02:01:19 +00001404
Greg Claytondaf515f2011-07-09 20:12:33 +00001405// This allows you to create an array member using and index
1406// that doesn't not fall in the normal bounds of the array.
1407// Many times structure can be defined as:
1408// struct Collection
1409// {
1410// uint32_t item_count;
1411// Item item_array[0];
1412// };
1413// The size of the "item_array" is 1, but many times in practice
1414// there are more items in "item_array".
1415
1416ValueObjectSP
1417ValueObject::GetSyntheticArrayMemberFromArray (int32_t index, bool can_create)
1418{
1419 ValueObjectSP synthetic_child_sp;
1420 if (IsArrayType ())
1421 {
1422 char index_str[64];
1423 snprintf(index_str, sizeof(index_str), "[%i]", index);
1424 ConstString index_const_str(index_str);
1425 // Check if we have already created a synthetic array member in this
1426 // valid object. If we have we will re-use it.
1427 synthetic_child_sp = GetSyntheticChild (index_const_str);
1428 if (!synthetic_child_sp)
1429 {
1430 ValueObject *synthetic_child;
1431 // We haven't made a synthetic array member for INDEX yet, so
1432 // lets make one and cache it for any future reference.
1433 synthetic_child = CreateChildAtIndex(0, true, index);
1434
1435 // Cache the value if we got one back...
1436 if (synthetic_child)
1437 {
1438 AddSyntheticChild(index_const_str, synthetic_child);
1439 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001440 synthetic_child_sp->SetName(ConstString(index_str));
Greg Claytondaf515f2011-07-09 20:12:33 +00001441 synthetic_child_sp->m_is_array_item_for_pointer = true;
1442 }
1443 }
1444 }
1445 return synthetic_child_sp;
1446}
1447
Enrico Granata9fc19442011-07-06 02:13:41 +00001448ValueObjectSP
1449ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1450{
1451 ValueObjectSP synthetic_child_sp;
1452 if (IsScalarType ())
1453 {
1454 char index_str[64];
1455 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1456 ConstString index_const_str(index_str);
1457 // Check if we have already created a synthetic array member in this
1458 // valid object. If we have we will re-use it.
1459 synthetic_child_sp = GetSyntheticChild (index_const_str);
1460 if (!synthetic_child_sp)
1461 {
1462 ValueObjectChild *synthetic_child;
1463 // We haven't made a synthetic array member for INDEX yet, so
1464 // lets make one and cache it for any future reference.
1465 synthetic_child = new ValueObjectChild(*this,
1466 GetClangAST(),
1467 GetClangType(),
1468 index_const_str,
1469 GetByteSize(),
1470 0,
1471 to-from+1,
1472 from,
1473 false,
1474 false);
1475
1476 // Cache the value if we got one back...
1477 if (synthetic_child)
1478 {
1479 AddSyntheticChild(index_const_str, synthetic_child);
1480 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata6f3533f2011-07-29 19:53:35 +00001481 synthetic_child_sp->SetName(ConstString(index_str));
Enrico Granata9fc19442011-07-06 02:13:41 +00001482 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1483 }
1484 }
1485 }
1486 return synthetic_child_sp;
1487}
1488
Enrico Granata6f3533f2011-07-29 19:53:35 +00001489lldb::ValueObjectSP
1490ValueObject::GetSyntheticChildAtOffset(uint32_t offset, const ClangASTType& type, bool can_create)
1491{
1492
1493 ValueObjectSP synthetic_child_sp;
1494
1495 char name_str[64];
1496 snprintf(name_str, sizeof(name_str), "@%i", offset);
1497 ConstString name_const_str(name_str);
1498
1499 // Check if we have already created a synthetic array member in this
1500 // valid object. If we have we will re-use it.
1501 synthetic_child_sp = GetSyntheticChild (name_const_str);
1502
1503 if (synthetic_child_sp.get())
1504 return synthetic_child_sp;
1505
1506 if (!can_create)
1507 return lldb::ValueObjectSP();
1508
1509 ValueObjectChild *synthetic_child = new ValueObjectChild(*this,
1510 type.GetASTContext(),
1511 type.GetOpaqueQualType(),
1512 name_const_str,
1513 type.GetTypeByteSize(),
1514 offset,
1515 0,
1516 0,
1517 false,
1518 false);
1519 if (synthetic_child)
1520 {
1521 AddSyntheticChild(name_const_str, synthetic_child);
1522 synthetic_child_sp = synthetic_child->GetSP();
1523 synthetic_child_sp->SetName(name_const_str);
1524 synthetic_child_sp->m_is_child_at_offset = true;
1525 }
1526 return synthetic_child_sp;
1527}
1528
Enrico Granatad55546b2011-07-22 00:16:08 +00001529// your expression path needs to have a leading . or ->
1530// (unless it somehow "looks like" an array, in which case it has
1531// a leading [ symbol). while the [ is meaningful and should be shown
1532// to the user, . and -> are just parser design, but by no means
1533// added information for the user.. strip them off
1534static const char*
1535SkipLeadingExpressionPathSeparators(const char* expression)
1536{
1537 if (!expression || !expression[0])
1538 return expression;
1539 if (expression[0] == '.')
1540 return expression+1;
1541 if (expression[0] == '-' && expression[1] == '>')
1542 return expression+2;
1543 return expression;
1544}
1545
1546lldb::ValueObjectSP
1547ValueObject::GetSyntheticExpressionPathChild(const char* expression, bool can_create)
1548{
1549 ValueObjectSP synthetic_child_sp;
1550 ConstString name_const_string(expression);
1551 // Check if we have already created a synthetic array member in this
1552 // valid object. If we have we will re-use it.
1553 synthetic_child_sp = GetSyntheticChild (name_const_string);
1554 if (!synthetic_child_sp)
1555 {
1556 // We haven't made a synthetic array member for expression yet, so
1557 // lets make one and cache it for any future reference.
1558 synthetic_child_sp = GetValueForExpressionPath(expression);
1559
1560 // Cache the value if we got one back...
1561 if (synthetic_child_sp.get())
1562 {
1563 AddSyntheticChild(name_const_string, synthetic_child_sp.get());
Enrico Granata6f3533f2011-07-29 19:53:35 +00001564 synthetic_child_sp->SetName(ConstString(SkipLeadingExpressionPathSeparators(expression)));
Enrico Granatad55546b2011-07-22 00:16:08 +00001565 synthetic_child_sp->m_is_expression_path_child = true;
1566 }
1567 }
1568 return synthetic_child_sp;
1569}
1570
1571void
1572ValueObject::CalculateSyntheticValue (lldb::SyntheticValueType use_synthetic)
1573{
1574 if (use_synthetic == lldb::eNoSyntheticFilter)
1575 return;
1576
Enrico Granatac3e320a2011-08-02 17:27:39 +00001577 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
Enrico Granatad55546b2011-07-22 00:16:08 +00001578
1579 if (m_last_synthetic_filter.get() == NULL)
1580 return;
1581
Enrico Granataa37a0652011-07-24 00:14:56 +00001582 if (m_synthetic_value == NULL)
1583 m_synthetic_value = new ValueObjectSynthetic(*this, m_last_synthetic_filter);
Enrico Granatad55546b2011-07-22 00:16:08 +00001584
1585}
1586
Jim Ingham78a685a2011-04-16 00:01:13 +00001587void
Jim Ingham2837b762011-05-04 03:43:18 +00001588ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00001589{
Jim Ingham2837b762011-05-04 03:43:18 +00001590 if (use_dynamic == lldb::eNoDynamicValues)
1591 return;
1592
Jim Ingham58b59f92011-04-22 23:53:53 +00001593 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00001594 {
Enrico Granata6f3533f2011-07-29 19:53:35 +00001595 Process *process = m_update_point.GetProcessSP().get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001596 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +00001597
Jim Ingham78a685a2011-04-16 00:01:13 +00001598
1599 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1600 // hard code this everywhere.
1601 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1602 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1603 {
1604 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1605 if (runtime)
1606 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1607 }
1608 else
1609 {
1610 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1611 if (cpp_runtime)
1612 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1613
1614 if (!worth_having_dynamic_value)
1615 {
1616 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1617 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001618 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001619 }
1620 }
1621
1622 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001623 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001624
1625// if (worth_having_dynamic_value)
1626// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1627
Jim Ingham78a685a2011-04-16 00:01:13 +00001628 }
1629}
1630
Jim Ingham58b59f92011-04-22 23:53:53 +00001631ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001632ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001633{
Jim Ingham2837b762011-05-04 03:43:18 +00001634 if (use_dynamic == lldb::eNoDynamicValues)
1635 return ValueObjectSP();
1636
1637 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001638 {
Jim Ingham2837b762011-05-04 03:43:18 +00001639 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001640 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001641 if (m_dynamic_value)
1642 return m_dynamic_value->GetSP();
1643 else
1644 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001645}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001646
Enrico Granatad55546b2011-07-22 00:16:08 +00001647// GetDynamicValue() returns a NULL SharedPointer if the object is not dynamic
1648// or we do not really want a dynamic VO. this method instead returns this object
1649// itself when making it synthetic has no meaning. this makes it much simpler
1650// to replace the SyntheticValue for the ValueObject
1651ValueObjectSP
1652ValueObject::GetSyntheticValue (SyntheticValueType use_synthetic)
1653{
1654 if (use_synthetic == lldb::eNoSyntheticFilter)
1655 return GetSP();
1656
Enrico Granatac3e320a2011-08-02 17:27:39 +00001657 UpdateFormatsIfNeeded(m_last_format_mgr_dynamic);
Enrico Granatad55546b2011-07-22 00:16:08 +00001658
1659 if (m_last_synthetic_filter.get() == NULL)
1660 return GetSP();
1661
1662 CalculateSyntheticValue(use_synthetic);
1663
1664 if (m_synthetic_value)
1665 return m_synthetic_value->GetSP();
1666 else
1667 return GetSP();
1668}
1669
Greg Claytone221f822011-01-21 01:59:00 +00001670bool
1671ValueObject::GetBaseClassPath (Stream &s)
1672{
1673 if (IsBaseClass())
1674 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001675 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001676 clang_type_t clang_type = GetClangType();
1677 std::string cxx_class_name;
1678 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1679 if (this_had_base_class)
1680 {
1681 if (parent_had_base_class)
1682 s.PutCString("::");
1683 s.PutCString(cxx_class_name.c_str());
1684 }
1685 return parent_had_base_class || this_had_base_class;
1686 }
1687 return false;
1688}
1689
1690
1691ValueObject *
1692ValueObject::GetNonBaseClassParent()
1693{
Jim Ingham78a685a2011-04-16 00:01:13 +00001694 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001695 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001696 if (GetParent()->IsBaseClass())
1697 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001698 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001699 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001700 }
1701 return NULL;
1702}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001703
1704void
Enrico Granata4becb372011-06-29 22:27:15 +00001705ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001706{
Greg Claytone221f822011-01-21 01:59:00 +00001707 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00001708
Enrico Granata9dd75c82011-07-15 23:30:15 +00001709 if (is_deref_of_parent && epformat == eDereferencePointers) {
Enrico Granata4becb372011-06-29 22:27:15 +00001710 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
1711 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
1712 // the eHonorPointers mode is meant to produce strings in this latter format
1713 s.PutCString("*(");
1714 }
Greg Claytone221f822011-01-21 01:59:00 +00001715
Enrico Granata4becb372011-06-29 22:27:15 +00001716 ValueObject* parent = GetParent();
1717
1718 if (parent)
1719 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001720
1721 // if we are a deref_of_parent just because we are synthetic array
1722 // members made up to allow ptr[%d] syntax to work in variable
1723 // printing, then add our name ([%d]) to the expression path
Enrico Granata9dd75c82011-07-15 23:30:15 +00001724 if (m_is_array_item_for_pointer && epformat == eHonorPointers)
Enrico Granata0a3958e2011-07-02 00:25:22 +00001725 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00001726
Greg Claytone221f822011-01-21 01:59:00 +00001727 if (!IsBaseClass())
1728 {
1729 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001730 {
Greg Claytone221f822011-01-21 01:59:00 +00001731 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1732 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001733 {
Greg Claytone221f822011-01-21 01:59:00 +00001734 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1735 if (non_base_class_parent_clang_type)
1736 {
1737 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1738
Enrico Granata9dd75c82011-07-15 23:30:15 +00001739 if (parent && parent->IsDereferenceOfParent() && epformat == eHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00001740 {
1741 s.PutCString("->");
1742 }
Enrico Granata4becb372011-06-29 22:27:15 +00001743 else
1744 {
1745 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1746 {
1747 s.PutCString("->");
1748 }
1749 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1750 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1751 {
1752 s.PutChar('.');
1753 }
Greg Claytone221f822011-01-21 01:59:00 +00001754 }
1755 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001756 }
Greg Claytone221f822011-01-21 01:59:00 +00001757
1758 const char *name = GetName().GetCString();
1759 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001760 {
Greg Claytone221f822011-01-21 01:59:00 +00001761 if (qualify_cxx_base_classes)
1762 {
1763 if (GetBaseClassPath (s))
1764 s.PutCString("::");
1765 }
1766 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001767 }
1768 }
1769 }
1770
Enrico Granata4becb372011-06-29 22:27:15 +00001771 if (is_deref_of_parent && epformat == eDereferencePointers) {
Greg Claytone221f822011-01-21 01:59:00 +00001772 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00001773 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001774}
1775
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001776lldb::ValueObjectSP
1777ValueObject::GetValueForExpressionPath(const char* expression,
1778 const char** first_unparsed,
1779 ExpressionPathScanEndReason* reason_to_stop,
1780 ExpressionPathEndResultType* final_value_type,
1781 const GetValueForExpressionPathOptions& options,
1782 ExpressionPathAftermath* final_task_on_target)
1783{
1784
1785 const char* dummy_first_unparsed;
1786 ExpressionPathScanEndReason dummy_reason_to_stop;
1787 ExpressionPathEndResultType dummy_final_value_type;
1788 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1789
1790 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1791 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1792 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1793 final_value_type ? final_value_type : &dummy_final_value_type,
1794 options,
1795 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1796
1797 if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1798 {
1799 return ret_val;
1800 }
1801 if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1802 {
1803 if (*final_task_on_target == ValueObject::eDereference)
1804 {
1805 Error error;
1806 ValueObjectSP final_value = ret_val->Dereference(error);
1807 if (error.Fail() || !final_value.get())
1808 {
1809 *reason_to_stop = ValueObject::eDereferencingFailed;
1810 *final_value_type = ValueObject::eInvalid;
1811 return ValueObjectSP();
1812 }
1813 else
1814 {
1815 *final_task_on_target = ValueObject::eNothing;
1816 return final_value;
1817 }
1818 }
1819 if (*final_task_on_target == ValueObject::eTakeAddress)
1820 {
1821 Error error;
1822 ValueObjectSP final_value = ret_val->AddressOf(error);
1823 if (error.Fail() || !final_value.get())
1824 {
1825 *reason_to_stop = ValueObject::eTakingAddressFailed;
1826 *final_value_type = ValueObject::eInvalid;
1827 return ValueObjectSP();
1828 }
1829 else
1830 {
1831 *final_task_on_target = ValueObject::eNothing;
1832 return final_value;
1833 }
1834 }
1835 }
1836 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
1837}
1838
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001839int
1840ValueObject::GetValuesForExpressionPath(const char* expression,
1841 lldb::ValueObjectListSP& list,
1842 const char** first_unparsed,
1843 ExpressionPathScanEndReason* reason_to_stop,
1844 ExpressionPathEndResultType* final_value_type,
1845 const GetValueForExpressionPathOptions& options,
1846 ExpressionPathAftermath* final_task_on_target)
1847{
1848 const char* dummy_first_unparsed;
1849 ExpressionPathScanEndReason dummy_reason_to_stop;
1850 ExpressionPathEndResultType dummy_final_value_type;
1851 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1852
1853 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1854 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1855 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1856 final_value_type ? final_value_type : &dummy_final_value_type,
1857 options,
1858 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1859
1860 if (!ret_val.get()) // if there are errors, I add nothing to the list
1861 return 0;
1862
1863 if (*reason_to_stop != eArrayRangeOperatorMet)
1864 {
1865 // I need not expand a range, just post-process the final value and return
1866 if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1867 {
1868 list->Append(ret_val);
1869 return 1;
1870 }
1871 if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1872 {
1873 if (*final_task_on_target == ValueObject::eDereference)
1874 {
1875 Error error;
1876 ValueObjectSP final_value = ret_val->Dereference(error);
1877 if (error.Fail() || !final_value.get())
1878 {
1879 *reason_to_stop = ValueObject::eDereferencingFailed;
1880 *final_value_type = ValueObject::eInvalid;
1881 return 0;
1882 }
1883 else
1884 {
1885 *final_task_on_target = ValueObject::eNothing;
1886 list->Append(final_value);
1887 return 1;
1888 }
1889 }
1890 if (*final_task_on_target == ValueObject::eTakeAddress)
1891 {
1892 Error error;
1893 ValueObjectSP final_value = ret_val->AddressOf(error);
1894 if (error.Fail() || !final_value.get())
1895 {
1896 *reason_to_stop = ValueObject::eTakingAddressFailed;
1897 *final_value_type = ValueObject::eInvalid;
1898 return 0;
1899 }
1900 else
1901 {
1902 *final_task_on_target = ValueObject::eNothing;
1903 list->Append(final_value);
1904 return 1;
1905 }
1906 }
1907 }
1908 }
1909 else
1910 {
1911 return ExpandArraySliceExpression(first_unparsed ? *first_unparsed : dummy_first_unparsed,
1912 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1913 ret_val,
1914 list,
1915 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1916 final_value_type ? final_value_type : &dummy_final_value_type,
1917 options,
1918 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1919 }
1920 // in any non-covered case, just do the obviously right thing
1921 list->Append(ret_val);
1922 return 1;
1923}
1924
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001925lldb::ValueObjectSP
1926ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
1927 const char** first_unparsed,
1928 ExpressionPathScanEndReason* reason_to_stop,
1929 ExpressionPathEndResultType* final_result,
1930 const GetValueForExpressionPathOptions& options,
1931 ExpressionPathAftermath* what_next)
1932{
1933 ValueObjectSP root = GetSP();
1934
1935 if (!root.get())
1936 return ValueObjectSP();
1937
1938 *first_unparsed = expression_cstr;
1939
1940 while (true)
1941 {
1942
1943 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
1944
1945 lldb::clang_type_t root_clang_type = root->GetClangType();
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001946 lldb::clang_type_t pointee_clang_type;
1947 Flags root_clang_type_info,pointee_clang_type_info;
1948
1949 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
1950 if (pointee_clang_type)
1951 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001952
1953 if (!expression_cstr || *expression_cstr == '\0')
1954 {
1955 *reason_to_stop = ValueObject::eEndOfString;
1956 return root;
1957 }
1958
1959 switch (*expression_cstr)
1960 {
1961 case '-':
1962 {
1963 if (options.m_check_dot_vs_arrow_syntax &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001964 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 +00001965 {
1966 *first_unparsed = expression_cstr;
1967 *reason_to_stop = ValueObject::eArrowInsteadOfDot;
1968 *final_result = ValueObject::eInvalid;
1969 return ValueObjectSP();
1970 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001971 if (root_clang_type_info.Test(ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
1972 root_clang_type_info.Test(ClangASTContext::eTypeIsPointer) &&
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001973 options.m_no_fragile_ivar)
1974 {
1975 *first_unparsed = expression_cstr;
1976 *reason_to_stop = ValueObject::eFragileIVarNotAllowed;
1977 *final_result = ValueObject::eInvalid;
1978 return ValueObjectSP();
1979 }
1980 if (expression_cstr[1] != '>')
1981 {
1982 *first_unparsed = expression_cstr;
1983 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1984 *final_result = ValueObject::eInvalid;
1985 return ValueObjectSP();
1986 }
1987 expression_cstr++; // skip the -
1988 }
1989 case '.': // or fallthrough from ->
1990 {
1991 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001992 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 +00001993 {
1994 *first_unparsed = expression_cstr;
1995 *reason_to_stop = ValueObject::eDotInsteadOfArrow;
1996 *final_result = ValueObject::eInvalid;
1997 return ValueObjectSP();
1998 }
1999 expression_cstr++; // skip .
2000 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
2001 ConstString child_name;
2002 if (!next_separator) // if no other separator just expand this last layer
2003 {
2004 child_name.SetCString (expression_cstr);
2005 root = root->GetChildMemberWithName(child_name, true);
2006 if (root.get()) // we know we are done, so just return
2007 {
2008 *first_unparsed = '\0';
2009 *reason_to_stop = ValueObject::eEndOfString;
2010 *final_result = ValueObject::ePlain;
2011 return root;
2012 }
2013 else
2014 {
2015 *first_unparsed = expression_cstr;
2016 *reason_to_stop = ValueObject::eNoSuchChild;
2017 *final_result = ValueObject::eInvalid;
2018 return ValueObjectSP();
2019 }
2020 }
2021 else // other layers do expand
2022 {
2023 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
2024 root = root->GetChildMemberWithName(child_name, true);
2025 if (root.get()) // store the new root and move on
2026 {
2027 *first_unparsed = next_separator;
2028 *final_result = ValueObject::ePlain;
2029 continue;
2030 }
2031 else
2032 {
2033 *first_unparsed = expression_cstr;
2034 *reason_to_stop = ValueObject::eNoSuchChild;
2035 *final_result = ValueObject::eInvalid;
2036 return ValueObjectSP();
2037 }
2038 }
2039 break;
2040 }
2041 case '[':
2042 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002043 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 +00002044 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002045 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002046 {
2047 *first_unparsed = expression_cstr;
2048 *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2049 *final_result = ValueObject::eInvalid;
2050 return ValueObjectSP();
2051 }
2052 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2053 {
2054 *first_unparsed = expression_cstr;
2055 *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2056 *final_result = ValueObject::eInvalid;
2057 return ValueObjectSP();
2058 }
2059 }
2060 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2061 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002062 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002063 {
2064 *first_unparsed = expression_cstr;
2065 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2066 *final_result = ValueObject::eInvalid;
2067 return ValueObjectSP();
2068 }
2069 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
2070 {
2071 *first_unparsed = expression_cstr+2;
2072 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2073 *final_result = ValueObject::eUnboundedRange;
2074 return root;
2075 }
2076 }
2077 const char *separator_position = ::strchr(expression_cstr+1,'-');
2078 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2079 if (!close_bracket_position) // if there is no ], this is a syntax error
2080 {
2081 *first_unparsed = expression_cstr;
2082 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2083 *final_result = ValueObject::eInvalid;
2084 return ValueObjectSP();
2085 }
2086 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2087 {
2088 char *end = NULL;
2089 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2090 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2091 {
2092 *first_unparsed = expression_cstr;
2093 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2094 *final_result = ValueObject::eInvalid;
2095 return ValueObjectSP();
2096 }
2097 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2098 {
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002099 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002100 {
2101 *first_unparsed = expression_cstr+2;
2102 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2103 *final_result = ValueObject::eUnboundedRange;
2104 return root;
2105 }
2106 else
2107 {
2108 *first_unparsed = expression_cstr;
2109 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2110 *final_result = ValueObject::eInvalid;
2111 return ValueObjectSP();
2112 }
2113 }
2114 // from here on we do have a valid index
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002115 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002116 {
Greg Claytondaf515f2011-07-09 20:12:33 +00002117 ValueObjectSP child_valobj_sp = root->GetChildAtIndex(index, true);
2118 if (!child_valobj_sp)
2119 child_valobj_sp = root->GetSyntheticArrayMemberFromArray(index, true);
2120 if (child_valobj_sp)
2121 {
2122 root = child_valobj_sp;
2123 *first_unparsed = end+1; // skip ]
2124 *final_result = ValueObject::ePlain;
2125 continue;
2126 }
2127 else
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002128 {
2129 *first_unparsed = expression_cstr;
2130 *reason_to_stop = ValueObject::eNoSuchChild;
2131 *final_result = ValueObject::eInvalid;
2132 return ValueObjectSP();
2133 }
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002134 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002135 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002136 {
2137 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 +00002138 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002139 {
2140 Error error;
2141 root = root->Dereference(error);
2142 if (error.Fail() || !root.get())
2143 {
2144 *first_unparsed = expression_cstr;
2145 *reason_to_stop = ValueObject::eDereferencingFailed;
2146 *final_result = ValueObject::eInvalid;
2147 return ValueObjectSP();
2148 }
2149 else
2150 {
2151 *what_next = eNothing;
2152 continue;
2153 }
2154 }
2155 else
2156 {
2157 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2158 if (!root.get())
2159 {
2160 *first_unparsed = expression_cstr;
2161 *reason_to_stop = ValueObject::eNoSuchChild;
2162 *final_result = ValueObject::eInvalid;
2163 return ValueObjectSP();
2164 }
2165 else
2166 {
2167 *first_unparsed = end+1; // skip ]
2168 *final_result = ValueObject::ePlain;
2169 continue;
2170 }
2171 }
2172 }
2173 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2174 {
2175 root = root->GetSyntheticBitFieldChild(index, index, true);
2176 if (!root.get())
2177 {
2178 *first_unparsed = expression_cstr;
2179 *reason_to_stop = ValueObject::eNoSuchChild;
2180 *final_result = ValueObject::eInvalid;
2181 return ValueObjectSP();
2182 }
2183 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2184 {
2185 *first_unparsed = end+1; // skip ]
2186 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2187 *final_result = ValueObject::eBitfield;
2188 return root;
2189 }
2190 }
2191 }
2192 else // we have a low and a high index
2193 {
2194 char *end = NULL;
2195 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2196 if (!end || end != separator_position) // if something weird is in our way return an error
2197 {
2198 *first_unparsed = expression_cstr;
2199 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2200 *final_result = ValueObject::eInvalid;
2201 return ValueObjectSP();
2202 }
2203 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2204 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2205 {
2206 *first_unparsed = expression_cstr;
2207 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2208 *final_result = ValueObject::eInvalid;
2209 return ValueObjectSP();
2210 }
2211 if (index_lower > index_higher) // swap indices if required
2212 {
2213 unsigned long temp = index_lower;
2214 index_lower = index_higher;
2215 index_higher = temp;
2216 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002217 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002218 {
2219 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2220 if (!root.get())
2221 {
2222 *first_unparsed = expression_cstr;
2223 *reason_to_stop = ValueObject::eNoSuchChild;
2224 *final_result = ValueObject::eInvalid;
2225 return ValueObjectSP();
2226 }
2227 else
2228 {
2229 *first_unparsed = end+1; // skip ]
2230 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
2231 *final_result = ValueObject::eBitfield;
2232 return root;
2233 }
2234 }
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002235 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 +00002236 *what_next == ValueObject::eDereference &&
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002237 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
Enrico Granatafc7a7f32011-07-08 02:51:01 +00002238 {
2239 Error error;
2240 root = root->Dereference(error);
2241 if (error.Fail() || !root.get())
2242 {
2243 *first_unparsed = expression_cstr;
2244 *reason_to_stop = ValueObject::eDereferencingFailed;
2245 *final_result = ValueObject::eInvalid;
2246 return ValueObjectSP();
2247 }
2248 else
2249 {
2250 *what_next = ValueObject::eNothing;
2251 continue;
2252 }
2253 }
2254 else
2255 {
2256 *first_unparsed = expression_cstr;
2257 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
2258 *final_result = ValueObject::eBoundedRange;
2259 return root;
2260 }
2261 }
2262 break;
2263 }
2264 default: // some non-separator is in the way
2265 {
2266 *first_unparsed = expression_cstr;
2267 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2268 *final_result = ValueObject::eInvalid;
2269 return ValueObjectSP();
2270 break;
2271 }
2272 }
2273 }
2274}
2275
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002276int
2277ValueObject::ExpandArraySliceExpression(const char* expression_cstr,
2278 const char** first_unparsed,
2279 lldb::ValueObjectSP root,
2280 lldb::ValueObjectListSP& list,
2281 ExpressionPathScanEndReason* reason_to_stop,
2282 ExpressionPathEndResultType* final_result,
2283 const GetValueForExpressionPathOptions& options,
2284 ExpressionPathAftermath* what_next)
2285{
2286 if (!root.get())
2287 return 0;
2288
2289 *first_unparsed = expression_cstr;
2290
2291 while (true)
2292 {
2293
2294 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
2295
2296 lldb::clang_type_t root_clang_type = root->GetClangType();
2297 lldb::clang_type_t pointee_clang_type;
2298 Flags root_clang_type_info,pointee_clang_type_info;
2299
2300 root_clang_type_info = Flags(ClangASTContext::GetTypeInfo(root_clang_type, GetClangAST(), &pointee_clang_type));
2301 if (pointee_clang_type)
2302 pointee_clang_type_info = Flags(ClangASTContext::GetTypeInfo(pointee_clang_type, GetClangAST(), NULL));
2303
2304 if (!expression_cstr || *expression_cstr == '\0')
2305 {
2306 *reason_to_stop = ValueObject::eEndOfString;
2307 list->Append(root);
2308 return 1;
2309 }
2310
2311 switch (*expression_cstr)
2312 {
2313 case '[':
2314 {
2315 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray) && !root_clang_type_info.Test(ClangASTContext::eTypeIsPointer)) // if this is not a T[] nor a T*
2316 {
2317 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // if this is not even a scalar, this syntax is just plain wrong!
2318 {
2319 *first_unparsed = expression_cstr;
2320 *reason_to_stop = ValueObject::eRangeOperatorInvalid;
2321 *final_result = ValueObject::eInvalid;
2322 return 0;
2323 }
2324 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
2325 {
2326 *first_unparsed = expression_cstr;
2327 *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
2328 *final_result = ValueObject::eInvalid;
2329 return 0;
2330 }
2331 }
2332 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
2333 {
2334 if (!root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2335 {
2336 *first_unparsed = expression_cstr;
2337 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2338 *final_result = ValueObject::eInvalid;
2339 return 0;
2340 }
2341 else // expand this into list
2342 {
2343 int max_index = root->GetNumChildren() - 1;
2344 for (int index = 0; index < max_index; index++)
2345 {
2346 ValueObjectSP child =
2347 root->GetChildAtIndex(index, true);
2348 list->Append(child);
2349 }
2350 *first_unparsed = expression_cstr+2;
2351 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2352 *final_result = ValueObject::eValueObjectList;
2353 return max_index; // tell me number of items I added to the VOList
2354 }
2355 }
2356 const char *separator_position = ::strchr(expression_cstr+1,'-');
2357 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
2358 if (!close_bracket_position) // if there is no ], this is a syntax error
2359 {
2360 *first_unparsed = expression_cstr;
2361 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2362 *final_result = ValueObject::eInvalid;
2363 return 0;
2364 }
2365 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
2366 {
2367 char *end = NULL;
2368 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
2369 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2370 {
2371 *first_unparsed = expression_cstr;
2372 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2373 *final_result = ValueObject::eInvalid;
2374 return 0;
2375 }
2376 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
2377 {
2378 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2379 {
2380 int max_index = root->GetNumChildren() - 1;
2381 for (int index = 0; index < max_index; index++)
2382 {
2383 ValueObjectSP child =
2384 root->GetChildAtIndex(index, true);
2385 list->Append(child);
2386 }
2387 *first_unparsed = expression_cstr+2;
2388 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2389 *final_result = ValueObject::eValueObjectList;
2390 return max_index; // tell me number of items I added to the VOList
2391 }
2392 else
2393 {
2394 *first_unparsed = expression_cstr;
2395 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
2396 *final_result = ValueObject::eInvalid;
2397 return 0;
2398 }
2399 }
2400 // from here on we do have a valid index
2401 if (root_clang_type_info.Test(ClangASTContext::eTypeIsArray))
2402 {
2403 root = root->GetChildAtIndex(index, true);
2404 if (!root.get())
2405 {
2406 *first_unparsed = expression_cstr;
2407 *reason_to_stop = ValueObject::eNoSuchChild;
2408 *final_result = ValueObject::eInvalid;
2409 return 0;
2410 }
2411 else
2412 {
2413 list->Append(root);
2414 *first_unparsed = end+1; // skip ]
2415 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2416 *final_result = ValueObject::eValueObjectList;
2417 return 1;
2418 }
2419 }
2420 else if (root_clang_type_info.Test(ClangASTContext::eTypeIsPointer))
2421 {
2422 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
2423 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2424 {
2425 Error error;
2426 root = root->Dereference(error);
2427 if (error.Fail() || !root.get())
2428 {
2429 *first_unparsed = expression_cstr;
2430 *reason_to_stop = ValueObject::eDereferencingFailed;
2431 *final_result = ValueObject::eInvalid;
2432 return 0;
2433 }
2434 else
2435 {
2436 *what_next = eNothing;
2437 continue;
2438 }
2439 }
2440 else
2441 {
2442 root = root->GetSyntheticArrayMemberFromPointer(index, true);
2443 if (!root.get())
2444 {
2445 *first_unparsed = expression_cstr;
2446 *reason_to_stop = ValueObject::eNoSuchChild;
2447 *final_result = ValueObject::eInvalid;
2448 return 0;
2449 }
2450 else
2451 {
2452 list->Append(root);
2453 *first_unparsed = end+1; // skip ]
2454 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2455 *final_result = ValueObject::eValueObjectList;
2456 return 1;
2457 }
2458 }
2459 }
2460 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
2461 {
2462 root = root->GetSyntheticBitFieldChild(index, index, true);
2463 if (!root.get())
2464 {
2465 *first_unparsed = expression_cstr;
2466 *reason_to_stop = ValueObject::eNoSuchChild;
2467 *final_result = ValueObject::eInvalid;
2468 return 0;
2469 }
2470 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
2471 {
2472 list->Append(root);
2473 *first_unparsed = end+1; // skip ]
2474 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2475 *final_result = ValueObject::eValueObjectList;
2476 return 1;
2477 }
2478 }
2479 }
2480 else // we have a low and a high index
2481 {
2482 char *end = NULL;
2483 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
2484 if (!end || end != separator_position) // if something weird is in our way return an error
2485 {
2486 *first_unparsed = expression_cstr;
2487 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2488 *final_result = ValueObject::eInvalid;
2489 return 0;
2490 }
2491 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
2492 if (!end || end != close_bracket_position) // if something weird is in our way return an error
2493 {
2494 *first_unparsed = expression_cstr;
2495 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2496 *final_result = ValueObject::eInvalid;
2497 return 0;
2498 }
2499 if (index_lower > index_higher) // swap indices if required
2500 {
2501 unsigned long temp = index_lower;
2502 index_lower = index_higher;
2503 index_higher = temp;
2504 }
2505 if (root_clang_type_info.Test(ClangASTContext::eTypeIsScalar)) // expansion only works for scalars
2506 {
2507 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
2508 if (!root.get())
2509 {
2510 *first_unparsed = expression_cstr;
2511 *reason_to_stop = ValueObject::eNoSuchChild;
2512 *final_result = ValueObject::eInvalid;
2513 return 0;
2514 }
2515 else
2516 {
2517 list->Append(root);
2518 *first_unparsed = end+1; // skip ]
2519 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2520 *final_result = ValueObject::eValueObjectList;
2521 return 1;
2522 }
2523 }
2524 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
2525 *what_next == ValueObject::eDereference &&
2526 pointee_clang_type_info.Test(ClangASTContext::eTypeIsScalar))
2527 {
2528 Error error;
2529 root = root->Dereference(error);
2530 if (error.Fail() || !root.get())
2531 {
2532 *first_unparsed = expression_cstr;
2533 *reason_to_stop = ValueObject::eDereferencingFailed;
2534 *final_result = ValueObject::eInvalid;
2535 return 0;
2536 }
2537 else
2538 {
2539 *what_next = ValueObject::eNothing;
2540 continue;
2541 }
2542 }
2543 else
2544 {
Johnny Chen44805302011-07-19 19:48:13 +00002545 for (unsigned long index = index_lower;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002546 index <= index_higher; index++)
2547 {
2548 ValueObjectSP child =
2549 root->GetChildAtIndex(index, true);
2550 list->Append(child);
2551 }
2552 *first_unparsed = end+1;
2553 *reason_to_stop = ValueObject::eRangeOperatorExpanded;
2554 *final_result = ValueObject::eValueObjectList;
2555 return index_higher-index_lower+1; // tell me number of items I added to the VOList
2556 }
2557 }
2558 break;
2559 }
2560 default: // some non-[ separator, or something entirely wrong, is in the way
2561 {
2562 *first_unparsed = expression_cstr;
2563 *reason_to_stop = ValueObject::eUnexpectedSymbol;
2564 *final_result = ValueObject::eInvalid;
2565 return 0;
2566 break;
2567 }
2568 }
2569 }
2570}
2571
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002572void
Greg Clayton1d3afba2010-10-05 00:00:42 +00002573ValueObject::DumpValueObject
2574(
2575 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00002576 ValueObject *valobj,
2577 const char *root_valobj_name,
2578 uint32_t ptr_depth,
2579 uint32_t curr_depth,
2580 uint32_t max_depth,
2581 bool show_types,
2582 bool show_location,
2583 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00002584 lldb::DynamicValueType use_dynamic,
Enrico Granatad55546b2011-07-22 00:16:08 +00002585 bool use_synth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002586 bool scope_already_checked,
Enrico Granata0c5ef692011-07-16 01:22:04 +00002587 bool flat_output,
2588 uint32_t omit_summary_depth
Greg Clayton1d3afba2010-10-05 00:00:42 +00002589)
2590{
Greg Clayton007d5be2011-05-30 00:49:24 +00002591 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002592 {
Enrico Granatac3e320a2011-08-02 17:27:39 +00002593 bool update_success = valobj->UpdateValueIfNeeded (use_dynamic, true);
Greg Clayton007d5be2011-05-30 00:49:24 +00002594
2595 if (update_success && use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00002596 {
Jim Ingham2837b762011-05-04 03:43:18 +00002597 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00002598 if (dynamic_value)
2599 valobj = dynamic_value;
2600 }
2601
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002602 clang_type_t clang_type = valobj->GetClangType();
2603
Greg Clayton73b472d2010-10-27 03:32:59 +00002604 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002605 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00002606 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
2607 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002608
2609 const bool print_valobj = flat_output == false || has_value;
2610
2611 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002612 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002613 if (show_location)
2614 {
Jim Ingham6035b672011-03-31 00:19:25 +00002615 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002616 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002617
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002618 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00002619
Greg Clayton7c8a9662010-11-02 01:50:16 +00002620 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00002621 if (show_types || (curr_depth == 0 && !flat_output))
Enrico Granatac3e320a2011-08-02 17:27:39 +00002622 {
Enrico Granata9910bc82011-08-03 02:18:51 +00002623 const char* typeName = valobj->GetTypeName().AsCString("<invalid type>");
2624 s.Printf("(%s", typeName);
2625 // only show dynamic types if the user really wants to see types
2626 if (show_types && use_dynamic != lldb::eNoDynamicValues &&
2627 (/*strstr(typeName, "id") == typeName ||*/
2628 ClangASTType::GetMinimumLanguage(valobj->GetClangAST(), valobj->GetClangType()) == lldb::eLanguageTypeObjC))
Enrico Granatac3e320a2011-08-02 17:27:39 +00002629 {
2630 Process* process = valobj->GetUpdatePoint().GetProcessSP().get();
2631 if (process == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00002632 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002633 else
2634 {
2635 ObjCLanguageRuntime *runtime = process->GetObjCLanguageRuntime();
2636 if (runtime == NULL)
Enrico Granata9910bc82011-08-03 02:18:51 +00002637 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002638 else
2639 {
2640 ObjCLanguageRuntime::ObjCISA isa = runtime->GetISA(*valobj);
2641 if (!runtime->IsValidISA(isa))
Enrico Granata9910bc82011-08-03 02:18:51 +00002642 s.Printf(", dynamic type: unknown) ");
Enrico Granatac3e320a2011-08-02 17:27:39 +00002643 else
2644 s.Printf(", dynamic type: %s) ",
2645 runtime->GetActualTypeName(isa).GetCString());
2646 }
2647 }
2648 }
2649 else
2650 s.Printf(") ");
2651 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002652
Greg Clayton1d3afba2010-10-05 00:00:42 +00002653
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002654 if (flat_output)
2655 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00002656 // If we are showing types, also qualify the C++ base classes
2657 const bool qualify_cxx_base_classes = show_types;
2658 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002659 s.PutCString(" =");
2660 }
2661 else
2662 {
2663 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
2664 s.Printf ("%s =", name_cstr);
2665 }
2666
Jim Ingham6035b672011-03-31 00:19:25 +00002667 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002668 {
Greg Clayton007d5be2011-05-30 00:49:24 +00002669 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002670 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002671 }
2672
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002673 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00002674 const char *sum_cstr = NULL;
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00002675 SummaryFormat* entry = valobj->GetSummaryFormat().get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002676
Enrico Granata0c5ef692011-07-16 01:22:04 +00002677 if (omit_summary_depth > 0)
2678 entry = NULL;
2679
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002680 if (err_cstr == NULL)
2681 {
Jim Ingham6035b672011-03-31 00:19:25 +00002682 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002683 err_cstr = valobj->GetError().AsCString();
2684 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002685
2686 if (err_cstr)
2687 {
Greg Clayton007d5be2011-05-30 00:49:24 +00002688 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00002689 }
2690 else
2691 {
Greg Clayton73b472d2010-10-27 03:32:59 +00002692 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002693 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002694 {
Enrico Granata4becb372011-06-29 22:27:15 +00002695
Enrico Granata0c5ef692011-07-16 01:22:04 +00002696 sum_cstr = (omit_summary_depth == 0) ? valobj->GetSummaryAsCString() : NULL;
Greg Clayton1d3afba2010-10-05 00:00:42 +00002697
Enrico Granata4becb372011-06-29 22:27:15 +00002698 // We must calculate this value in realtime because entry might alter this variable's value
2699 // (e.g. by saying ${var%fmt}) and render precached values useless
2700 if (val_cstr && (!entry || entry->DoesPrintValue() || !sum_cstr))
2701 s.Printf(" %s", valobj->GetValueAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002702
Enrico Granata9dd75c82011-07-15 23:30:15 +00002703 if (sum_cstr)
Enrico Granata0a3958e2011-07-02 00:25:22 +00002704 {
2705 // for some reason, using %@ (ObjC description) in a summary string, makes
2706 // us believe we need to reset ourselves, thus invalidating the content of
2707 // sum_cstr. Thus, IF we had a valid sum_cstr before, but it is now empty
2708 // let us recalculate it!
2709 if (sum_cstr[0] == '\0')
2710 s.Printf(" %s", valobj->GetSummaryAsCString());
2711 else
2712 s.Printf(" %s", sum_cstr);
2713 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002714
2715 if (use_objc)
2716 {
Jim Ingham6035b672011-03-31 00:19:25 +00002717 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002718 if (object_desc)
2719 s.Printf(" %s\n", object_desc);
2720 else
Sean Callanan672ad942010-10-23 00:18:49 +00002721 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002722 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00002723 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002724 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002725
2726 if (curr_depth < max_depth)
2727 {
Greg Clayton73b472d2010-10-27 03:32:59 +00002728 // We will show children for all concrete types. We won't show
2729 // pointer contents unless a pointer depth has been specified.
2730 // We won't reference contents unless the reference is the
2731 // root object (depth of zero).
2732 bool print_children = true;
2733
2734 // Use a new temporary pointer depth in case we override the
2735 // current pointer depth below...
2736 uint32_t curr_ptr_depth = ptr_depth;
2737
2738 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
2739 if (is_ptr || is_ref)
2740 {
2741 // We have a pointer or reference whose value is an address.
2742 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00002743 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00002744 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
2745 print_children = false;
2746
2747 else if (is_ref && curr_depth == 0)
2748 {
2749 // If this is the root object (depth is zero) that we are showing
2750 // and it is a reference, and no pointer depth has been supplied
2751 // print out what it references. Don't do this at deeper depths
2752 // otherwise we can end up with infinite recursion...
2753 curr_ptr_depth = 1;
2754 }
2755
2756 if (curr_ptr_depth == 0)
2757 print_children = false;
2758 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00002759
Enrico Granata0a3958e2011-07-02 00:25:22 +00002760 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00002761 {
Enrico Granatad55546b2011-07-22 00:16:08 +00002762 ValueObjectSP synth_vobj = valobj->GetSyntheticValue(use_synth ?
2763 lldb::eUseSyntheticFilter :
2764 lldb::eNoSyntheticFilter);
2765 const uint32_t num_children = synth_vobj->GetNumChildren();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002766 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002767 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002768 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002769 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002770 if (print_valobj)
2771 s.EOL();
2772 }
2773 else
2774 {
2775 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00002776 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002777 s.IndentMore();
2778 }
2779
2780 for (uint32_t idx=0; idx<num_children; ++idx)
2781 {
Enrico Granatad55546b2011-07-22 00:16:08 +00002782 ValueObjectSP child_sp(synth_vobj->GetChildAtIndex(idx, true));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002783 if (child_sp.get())
2784 {
2785 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002786 child_sp.get(),
2787 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00002788 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002789 curr_depth + 1,
2790 max_depth,
2791 show_types,
2792 show_location,
2793 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00002794 use_dynamic,
Enrico Granatad55546b2011-07-22 00:16:08 +00002795 use_synth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002796 true,
Enrico Granata0c5ef692011-07-16 01:22:04 +00002797 flat_output,
2798 omit_summary_depth > 1 ? omit_summary_depth - 1 : 0);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002799 }
2800 }
2801
2802 if (!flat_output)
2803 {
2804 s.IndentLess();
2805 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00002806 }
2807 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002808 else if (has_children)
2809 {
2810 // Aggregate, no children...
2811 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00002812 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002813 }
2814 else
2815 {
2816 if (print_valobj)
2817 s.EOL();
2818 }
2819
Greg Clayton1d3afba2010-10-05 00:00:42 +00002820 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002821 else
2822 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00002823 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00002824 }
2825 }
2826 else
2827 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002828 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002829 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002830 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00002831 }
2832 }
2833 }
2834 }
2835}
2836
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002837
2838ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00002839ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002840{
2841 ValueObjectSP valobj_sp;
2842
Enrico Granatac3e320a2011-08-02 17:27:39 +00002843 if (UpdateValueIfNeeded(false) && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002844 {
Jim Ingham6035b672011-03-31 00:19:25 +00002845 ExecutionContextScope *exe_scope = GetExecutionContextScope();
2846 if (exe_scope)
2847 {
2848 ExecutionContext exe_ctx;
2849 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002850
Jim Ingham6035b672011-03-31 00:19:25 +00002851 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002852
Jim Ingham6035b672011-03-31 00:19:25 +00002853 DataExtractor data;
2854 data.SetByteOrder (m_data.GetByteOrder());
2855 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002856
Greg Clayton644247c2011-07-07 01:59:51 +00002857 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002858
Jim Ingham58b59f92011-04-22 23:53:53 +00002859 valobj_sp = ValueObjectConstResult::Create (exe_scope,
2860 ast,
2861 GetClangType(),
2862 name,
2863 data);
Jim Ingham6035b672011-03-31 00:19:25 +00002864 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002865 }
Jim Ingham6035b672011-03-31 00:19:25 +00002866
2867 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002868 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002869 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002870 }
2871 return valobj_sp;
2872}
2873
2874lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00002875ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002876{
Jim Ingham58b59f92011-04-22 23:53:53 +00002877 if (m_deref_valobj)
2878 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00002879
Greg Clayton54979cd2010-12-15 05:08:08 +00002880 const bool is_pointer_type = IsPointerType();
2881 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002882 {
2883 bool omit_empty_base_classes = true;
Greg Claytondaf515f2011-07-09 20:12:33 +00002884 bool ignore_array_bounds = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002885
2886 std::string child_name_str;
2887 uint32_t child_byte_size = 0;
2888 int32_t child_byte_offset = 0;
2889 uint32_t child_bitfield_bit_size = 0;
2890 uint32_t child_bitfield_bit_offset = 0;
2891 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00002892 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002893 const bool transparent_pointers = false;
2894 clang::ASTContext *clang_ast = GetClangAST();
2895 clang_type_t clang_type = GetClangType();
2896 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00002897
2898 ExecutionContext exe_ctx;
2899 GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
2900
2901 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
2902 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002903 GetName().GetCString(),
2904 clang_type,
2905 0,
2906 transparent_pointers,
2907 omit_empty_base_classes,
Greg Claytondaf515f2011-07-09 20:12:33 +00002908 ignore_array_bounds,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002909 child_name_str,
2910 child_byte_size,
2911 child_byte_offset,
2912 child_bitfield_bit_size,
2913 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00002914 child_is_base_class,
2915 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00002916 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002917 {
2918 ConstString child_name;
2919 if (!child_name_str.empty())
2920 child_name.SetCString (child_name_str.c_str());
2921
Jim Ingham58b59f92011-04-22 23:53:53 +00002922 m_deref_valobj = new ValueObjectChild (*this,
2923 clang_ast,
2924 child_clang_type,
2925 child_name,
2926 child_byte_size,
2927 child_byte_offset,
2928 child_bitfield_bit_size,
2929 child_bitfield_bit_offset,
2930 child_is_base_class,
2931 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002932 }
2933 }
Greg Clayton54979cd2010-12-15 05:08:08 +00002934
Jim Ingham58b59f92011-04-22 23:53:53 +00002935 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00002936 {
2937 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00002938 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00002939 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002940 else
2941 {
Greg Clayton54979cd2010-12-15 05:08:08 +00002942 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002943 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002944
2945 if (is_pointer_type)
2946 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
2947 else
2948 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00002949 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002950 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002951}
2952
Jim Ingham78a685a2011-04-16 00:01:13 +00002953lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00002954ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002955{
Jim Ingham78a685a2011-04-16 00:01:13 +00002956 if (m_addr_of_valobj_sp)
2957 return m_addr_of_valobj_sp;
2958
Greg Claytone0d378b2011-03-24 21:19:54 +00002959 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002960 const bool scalar_is_load_address = false;
2961 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00002962 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002963 if (addr != LLDB_INVALID_ADDRESS)
2964 {
2965 switch (address_type)
2966 {
Greg Clayton54979cd2010-12-15 05:08:08 +00002967 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002968 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00002969 {
2970 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002971 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002972 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
2973 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002974 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00002975
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002976 case eAddressTypeFile:
2977 case eAddressTypeLoad:
2978 case eAddressTypeHost:
2979 {
2980 clang::ASTContext *ast = GetClangAST();
2981 clang_type_t clang_type = GetClangType();
2982 if (ast && clang_type)
2983 {
2984 std::string name (1, '&');
2985 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00002986 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
2987 ast,
2988 ClangASTContext::CreatePointerType (ast, clang_type),
2989 ConstString (name.c_str()),
2990 addr,
2991 eAddressTypeInvalid,
2992 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002993 }
2994 }
2995 break;
2996 }
2997 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002998 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002999}
3000
Greg Claytonb2dcc362011-05-05 23:32:56 +00003001
3002lldb::ValueObjectSP
3003ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
3004{
3005 lldb::ValueObjectSP valobj_sp;
3006 AddressType address_type;
3007 const bool scalar_is_load_address = true;
3008 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
3009
3010 if (ptr_value != LLDB_INVALID_ADDRESS)
3011 {
3012 Address ptr_addr (NULL, ptr_value);
3013
3014 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3015 name,
3016 ptr_addr,
3017 clang_ast_type);
3018 }
3019 return valobj_sp;
3020}
3021
3022lldb::ValueObjectSP
3023ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
3024{
3025 lldb::ValueObjectSP valobj_sp;
3026 AddressType address_type;
3027 const bool scalar_is_load_address = true;
3028 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
3029
3030 if (ptr_value != LLDB_INVALID_ADDRESS)
3031 {
3032 Address ptr_addr (NULL, ptr_value);
3033
3034 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
3035 name,
3036 ptr_addr,
3037 type_sp);
3038 }
3039 return valobj_sp;
3040}
3041
Jim Ingham6035b672011-03-31 00:19:25 +00003042ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00003043 m_thread_id (LLDB_INVALID_UID),
3044 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00003045{
3046}
3047
3048ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00003049 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003050 m_first_update (true),
Jim Ingham89b61092011-07-06 17:42:14 +00003051 m_thread_id (LLDB_INVALID_THREAD_ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003052 m_stop_id (0)
3053
Jim Ingham6035b672011-03-31 00:19:25 +00003054{
3055 ExecutionContext exe_ctx;
3056 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
3057 // and if so we want to cache that not the original.
3058 if (exe_scope)
3059 exe_scope->CalculateExecutionContext(exe_ctx);
3060 if (exe_ctx.target != NULL)
3061 {
3062 m_target_sp = exe_ctx.target->GetSP();
3063
3064 if (exe_ctx.process == NULL)
3065 m_process_sp = exe_ctx.target->GetProcessSP();
3066 else
3067 m_process_sp = exe_ctx.process->GetSP();
3068
3069 if (m_process_sp != NULL)
3070 {
3071 m_stop_id = m_process_sp->GetStopID();
3072 Thread *thread = NULL;
3073
3074 if (exe_ctx.thread == NULL)
3075 {
3076 if (use_selected)
3077 {
3078 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
3079 if (thread)
3080 computed_exe_scope = thread;
3081 }
3082 }
3083 else
3084 thread = exe_ctx.thread;
3085
3086 if (thread != NULL)
3087 {
3088 m_thread_id = thread->GetIndexID();
3089 if (exe_ctx.frame == NULL)
3090 {
3091 if (use_selected)
3092 {
3093 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
3094 if (frame)
3095 {
3096 m_stack_id = frame->GetStackID();
3097 computed_exe_scope = frame;
3098 }
3099 }
3100 }
3101 else
3102 m_stack_id = exe_ctx.frame->GetStackID();
3103 }
3104 }
3105 }
3106 m_exe_scope = computed_exe_scope;
3107}
3108
3109ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
3110 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00003111 m_needs_update(true),
3112 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00003113 m_target_sp (rhs.m_target_sp),
3114 m_process_sp (rhs.m_process_sp),
3115 m_thread_id (rhs.m_thread_id),
3116 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00003117 m_stop_id (0)
3118{
3119}
3120
3121ValueObject::EvaluationPoint::~EvaluationPoint ()
3122{
3123}
3124
3125ExecutionContextScope *
3126ValueObject::EvaluationPoint::GetExecutionContextScope ()
3127{
3128 // We have to update before giving out the scope, or we could be handing out stale pointers.
3129 SyncWithProcessState();
3130
3131 return m_exe_scope;
3132}
3133
3134// This function checks the EvaluationPoint against the current process state. If the current
3135// state matches the evaluation point, or the evaluation point is already invalid, then we return
3136// false, meaning "no change". If the current state is different, we update our state, and return
3137// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
3138// future calls to NeedsUpdate will return true.
3139
3140bool
3141ValueObject::EvaluationPoint::SyncWithProcessState()
3142{
3143 // If we're already invalid, we don't need to do anything, and nothing has changed:
3144 if (m_stop_id == LLDB_INVALID_UID)
3145 {
3146 // Can't update with an invalid state.
3147 m_needs_update = false;
3148 return false;
3149 }
3150
3151 // If we don't have a process nothing can change.
3152 if (!m_process_sp)
3153 return false;
3154
3155 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00003156 uint32_t cur_stop_id = m_process_sp->GetStopID();
3157 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00003158 return false;
3159
Jim Ingham78a685a2011-04-16 00:01:13 +00003160 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
3161 // In either case, we aren't going to be able to sync with the process state.
3162 if (cur_stop_id == 0)
3163 return false;
3164
3165 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00003166 m_needs_update = true;
3167 m_exe_scope = m_process_sp.get();
3168
3169 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
3170 // doesn't, mark ourselves as invalid.
3171
3172 if (m_thread_id != LLDB_INVALID_THREAD_ID)
3173 {
3174 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
3175 if (our_thread == NULL)
Greg Clayton262f80d2011-07-06 16:49:27 +00003176 {
Jim Ingham89b61092011-07-06 17:42:14 +00003177 SetInvalid();
Greg Clayton262f80d2011-07-06 16:49:27 +00003178 }
Jim Ingham6035b672011-03-31 00:19:25 +00003179 else
3180 {
3181 m_exe_scope = our_thread;
3182
3183 if (m_stack_id.IsValid())
3184 {
3185 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
3186 if (our_frame == NULL)
3187 SetInvalid();
3188 else
3189 m_exe_scope = our_frame;
3190 }
3191 }
3192 }
3193 return true;
3194}
3195
Jim Ingham61be0902011-05-02 18:13:59 +00003196void
3197ValueObject::EvaluationPoint::SetUpdated ()
3198{
3199 m_first_update = false;
3200 m_needs_update = false;
3201 if (m_process_sp)
3202 m_stop_id = m_process_sp->GetStopID();
3203}
3204
3205
Jim Ingham6035b672011-03-31 00:19:25 +00003206bool
3207ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
3208{
3209 if (!IsValid())
3210 return false;
3211
3212 bool needs_update = false;
3213 m_exe_scope = NULL;
3214
3215 // The target has to be non-null, and the
3216 Target *target = exe_scope->CalculateTarget();
3217 if (target != NULL)
3218 {
3219 Target *old_target = m_target_sp.get();
3220 assert (target == old_target);
3221 Process *process = exe_scope->CalculateProcess();
3222 if (process != NULL)
3223 {
3224 // FOR NOW - assume you can't update variable objects across process boundaries.
3225 Process *old_process = m_process_sp.get();
3226 assert (process == old_process);
3227
3228 lldb::user_id_t stop_id = process->GetStopID();
3229 if (stop_id != m_stop_id)
3230 {
3231 needs_update = true;
3232 m_stop_id = stop_id;
3233 }
3234 // See if we're switching the thread or stack context. If no thread is given, this is
3235 // being evaluated in a global context.
3236 Thread *thread = exe_scope->CalculateThread();
3237 if (thread != NULL)
3238 {
3239 lldb::user_id_t new_thread_index = thread->GetIndexID();
3240 if (new_thread_index != m_thread_id)
3241 {
3242 needs_update = true;
3243 m_thread_id = new_thread_index;
3244 m_stack_id.Clear();
3245 }
3246
3247 StackFrame *new_frame = exe_scope->CalculateStackFrame();
3248 if (new_frame != NULL)
3249 {
3250 if (new_frame->GetStackID() != m_stack_id)
3251 {
3252 needs_update = true;
3253 m_stack_id = new_frame->GetStackID();
3254 }
3255 }
3256 else
3257 {
3258 m_stack_id.Clear();
3259 needs_update = true;
3260 }
3261 }
3262 else
3263 {
3264 // If this had been given a thread, and now there is none, we should update.
3265 // Otherwise we don't have to do anything.
3266 if (m_thread_id != LLDB_INVALID_UID)
3267 {
3268 m_thread_id = LLDB_INVALID_UID;
3269 m_stack_id.Clear();
3270 needs_update = true;
3271 }
3272 }
3273 }
3274 else
3275 {
3276 // If there is no process, then we don't need to update anything.
3277 // But if we're switching from having a process to not, we should try to update.
3278 if (m_process_sp.get() != NULL)
3279 {
3280 needs_update = true;
3281 m_process_sp.reset();
3282 m_thread_id = LLDB_INVALID_UID;
3283 m_stack_id.Clear();
3284 }
3285 }
3286 }
3287 else
3288 {
3289 // If there's no target, nothing can change so we don't need to update anything.
3290 // But if we're switching from having a target to not, we should try to update.
3291 if (m_target_sp.get() != NULL)
3292 {
3293 needs_update = true;
3294 m_target_sp.reset();
3295 m_process_sp.reset();
3296 m_thread_id = LLDB_INVALID_UID;
3297 m_stack_id.Clear();
3298 }
3299 }
3300 if (!m_needs_update)
3301 m_needs_update = needs_update;
3302
3303 return needs_update;
3304}
Enrico Granataf2bbf712011-07-15 02:26:42 +00003305
3306void
3307ValueObject::ClearUserVisibleData()
3308{
3309 m_location_str.clear();
3310 m_value_str.clear();
3311 m_summary_str.clear();
3312 m_object_desc_str.clear();
Johnny Chen44805302011-07-19 19:48:13 +00003313}