blob: aae31dc414f8e51168676e53acb9b70fc7e546d9 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023#include "lldb/Core/StreamString.h"
24#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000025#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000026#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Core/ValueObjectList.h"
Greg Claytonb2dcc362011-05-05 23:32:56 +000028#include "lldb/Core/ValueObjectMemory.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029
Greg Clayton7fb56d02011-02-01 01:31:41 +000030#include "lldb/Host/Endian.h"
31
Greg Claytone1a916a2010-07-21 22:12:05 +000032#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Symbol/ClangASTContext.h"
34#include "lldb/Symbol/Type.h"
35
Jim Ingham53c47f12010-09-10 23:12:17 +000036#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000037#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/Process.h"
39#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000040#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
43using namespace lldb;
44using namespace lldb_private;
45
46static lldb::user_id_t g_value_obj_uid = 0;
47
48//----------------------------------------------------------------------
49// ValueObject constructor
50//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000051ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000053 m_parent (&parent),
Stephen Wilson71c21d12011-04-11 19:41:40 +000054 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055 m_name (),
56 m_data (),
57 m_value (),
58 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000059 m_value_str (),
60 m_old_value_str (),
61 m_location_str (),
62 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000063 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000064 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000065 m_children (),
66 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000067 m_dynamic_value (NULL),
68 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000069 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000070 m_value_is_valid (false),
71 m_value_did_change (false),
72 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000073 m_old_value_valid (false),
Greg Claytone221f822011-01-21 01:59:00 +000074 m_pointers_point_to_load_addrs (false),
Enrico Granata4becb372011-06-29 22:27:15 +000075 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +000076 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +000077 m_is_bitfield_for_scalar(false),
Enrico Granata4becb372011-06-29 22:27:15 +000078 m_last_format_mgr_revision(0),
79 m_last_summary_format(),
80 m_last_value_format()
Jim Ingham6035b672011-03-31 00:19:25 +000081{
Jim Ingham58b59f92011-04-22 23:53:53 +000082 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +000083}
84
85//----------------------------------------------------------------------
86// ValueObject constructor
87//----------------------------------------------------------------------
88ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
89 UserID (++g_value_obj_uid), // Unique identifier for every value object
90 m_parent (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000091 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +000092 m_name (),
93 m_data (),
94 m_value (),
95 m_error (),
96 m_value_str (),
97 m_old_value_str (),
98 m_location_str (),
99 m_summary_str (),
100 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000101 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +0000102 m_children (),
103 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +0000104 m_dynamic_value (NULL),
105 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000106 m_format (eFormatDefault),
107 m_value_is_valid (false),
108 m_value_did_change (false),
109 m_children_count_valid (false),
110 m_old_value_valid (false),
111 m_pointers_point_to_load_addrs (false),
Enrico Granata4becb372011-06-29 22:27:15 +0000112 m_is_deref_of_parent (false),
Enrico Granata0a3958e2011-07-02 00:25:22 +0000113 m_is_array_item_for_pointer(false),
Enrico Granata9fc19442011-07-06 02:13:41 +0000114 m_is_bitfield_for_scalar(false),
Enrico Granata4becb372011-06-29 22:27:15 +0000115 m_last_format_mgr_revision(0),
116 m_last_summary_format(),
117 m_last_value_format()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118{
Jim Ingham58b59f92011-04-22 23:53:53 +0000119 m_manager = new ValueObjectManager();
120 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121}
122
123//----------------------------------------------------------------------
124// Destructor
125//----------------------------------------------------------------------
126ValueObject::~ValueObject ()
127{
128}
129
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130bool
Enrico Granata0a3958e2011-07-02 00:25:22 +0000131ValueObject::UpdateValueIfNeeded (bool update_format)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000132{
Enrico Granata4becb372011-06-29 22:27:15 +0000133
Enrico Granata0a3958e2011-07-02 00:25:22 +0000134 if (update_format)
135 UpdateFormatsIfNeeded();
Enrico Granata4becb372011-06-29 22:27:15 +0000136
Greg Claytonb71f3842010-10-05 03:13:51 +0000137 // If this is a constant value, then our success is predicated on whether
138 // we have an error or not
139 if (GetIsConstant())
140 return m_error.Success();
141
Jim Ingham6035b672011-03-31 00:19:25 +0000142 bool first_update = m_update_point.IsFirstEvaluation();
143
144 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145 {
Jim Ingham6035b672011-03-31 00:19:25 +0000146 m_update_point.SetUpdated();
147
148 // Save the old value using swap to avoid a string copy which
149 // also will clear our m_value_str
150 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000151 {
Jim Ingham6035b672011-03-31 00:19:25 +0000152 m_old_value_valid = false;
153 }
154 else
155 {
156 m_old_value_valid = true;
157 m_old_value_str.swap (m_value_str);
158 m_value_str.clear();
159 }
160 m_location_str.clear();
161 m_summary_str.clear();
162 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000163
Jim Ingham6035b672011-03-31 00:19:25 +0000164 const bool value_was_valid = GetValueIsValid();
165 SetValueDidChange (false);
Greg Clayton73b953b2010-08-28 00:08:07 +0000166
Jim Ingham6035b672011-03-31 00:19:25 +0000167 m_error.Clear();
Greg Clayton73b953b2010-08-28 00:08:07 +0000168
Jim Ingham6035b672011-03-31 00:19:25 +0000169 // Call the pure virtual function to update the value
170 bool success = UpdateValue ();
171
172 SetValueIsValid (success);
173
174 if (first_update)
175 SetValueDidChange (false);
176 else if (!m_value_did_change && success == false)
177 {
178 // The value wasn't gotten successfully, so we mark this
179 // as changed if the value used to be valid and now isn't
180 SetValueDidChange (value_was_valid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000181 }
182 }
183 return m_error.Success();
184}
185
Enrico Granata4becb372011-06-29 22:27:15 +0000186void
187ValueObject::UpdateFormatsIfNeeded()
188{
189 /*printf("CHECKING FOR UPDATES. I am at revision %d, while the format manager is at revision %d\n",
190 m_last_format_mgr_revision,
191 Debugger::ValueFormats::GetCurrentRevision());*/
192 if (m_last_format_mgr_revision != Debugger::ValueFormats::GetCurrentRevision())
193 {
194 if (m_last_summary_format.get())
195 m_last_summary_format.reset((SummaryFormat*)NULL);
196 if (m_last_value_format.get())
197 m_last_value_format.reset((ValueFormat*)NULL);
198 Debugger::ValueFormats::Get(*this, m_last_value_format);
Enrico Granata0a3958e2011-07-02 00:25:22 +0000199 if (!Debugger::SummaryFormats::Get(*this, m_last_summary_format))
200 Debugger::RegexSummaryFormats::Get(*this, m_last_summary_format);
Enrico Granata4becb372011-06-29 22:27:15 +0000201 m_last_format_mgr_revision = Debugger::ValueFormats::GetCurrentRevision();
202 m_value_str.clear();
203 m_summary_str.clear();
204 }
205}
206
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207DataExtractor &
208ValueObject::GetDataExtractor ()
209{
Jim Ingham78a685a2011-04-16 00:01:13 +0000210 UpdateValueIfNeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000211 return m_data;
212}
213
214const Error &
Greg Clayton262f80d2011-07-06 16:49:27 +0000215ValueObject::GetError()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216{
Greg Clayton262f80d2011-07-06 16:49:27 +0000217 UpdateValueIfNeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218 return m_error;
219}
220
221const ConstString &
222ValueObject::GetName() const
223{
224 return m_name;
225}
226
227const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000228ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000229{
Jim Ingham6035b672011-03-31 00:19:25 +0000230 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231 {
232 if (m_location_str.empty())
233 {
234 StreamString sstr;
235
236 switch (m_value.GetValueType())
237 {
238 default:
239 break;
240
241 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000242 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 {
244 RegisterInfo *reg_info = m_value.GetRegisterInfo();
245 if (reg_info)
246 {
247 if (reg_info->name)
248 m_location_str = reg_info->name;
249 else if (reg_info->alt_name)
250 m_location_str = reg_info->alt_name;
251 break;
252 }
253 }
254 m_location_str = "scalar";
255 break;
256
257 case Value::eValueTypeLoadAddress:
258 case Value::eValueTypeFileAddress:
259 case Value::eValueTypeHostAddress:
260 {
261 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
262 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
263 m_location_str.swap(sstr.GetString());
264 }
265 break;
266 }
267 }
268 }
269 return m_location_str.c_str();
270}
271
272Value &
273ValueObject::GetValue()
274{
275 return m_value;
276}
277
278const Value &
279ValueObject::GetValue() const
280{
281 return m_value;
282}
283
284bool
Jim Ingham6035b672011-03-31 00:19:25 +0000285ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000286{
287 ExecutionContext exe_ctx;
Jim Ingham6035b672011-03-31 00:19:25 +0000288 ExecutionContextScope *exe_scope = GetExecutionContextScope();
289 if (exe_scope)
290 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8f343b02010-11-04 01:54:29 +0000291 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
292 return scalar.IsValid();
293}
294
295bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000296ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297{
Greg Clayton288bdf92010-09-02 02:59:18 +0000298 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299}
300
301
302void
303ValueObject::SetValueIsValid (bool b)
304{
Greg Clayton288bdf92010-09-02 02:59:18 +0000305 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306}
307
308bool
Jim Ingham6035b672011-03-31 00:19:25 +0000309ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000310{
Jim Ingham6035b672011-03-31 00:19:25 +0000311 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000312 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313}
314
315void
316ValueObject::SetValueDidChange (bool value_changed)
317{
Greg Clayton288bdf92010-09-02 02:59:18 +0000318 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319}
320
321ValueObjectSP
322ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
323{
324 ValueObjectSP child_sp;
Greg Claytondea8cb42011-06-29 22:09:02 +0000325 // We may need to update our value if we are dynamic
326 if (IsPossibleDynamicType ())
327 UpdateValueIfNeeded();
328 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000330 // Check if we have already made the child value object?
331 if (can_create && m_children[idx] == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000333 // No we haven't created the child at this index, so lets have our
334 // subclass do it and cache the result for quick future access.
335 m_children[idx] = CreateChildAtIndex (idx, false, 0);
Jim Ingham78a685a2011-04-16 00:01:13 +0000336 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000337
338 if (m_children[idx] != NULL)
339 return m_children[idx]->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340 }
341 return child_sp;
342}
343
344uint32_t
345ValueObject::GetIndexOfChildWithName (const ConstString &name)
346{
347 bool omit_empty_base_classes = true;
348 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000349 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000350 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000351 omit_empty_base_classes);
352}
353
354ValueObjectSP
355ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
356{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000357 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000358 // classes (which really aren't part of the expression path), so we
359 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000360 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000361
Greg Claytondea8cb42011-06-29 22:09:02 +0000362 // We may need to update our value if we are dynamic
363 if (IsPossibleDynamicType ())
364 UpdateValueIfNeeded();
365
366 std::vector<uint32_t> child_indexes;
367 clang::ASTContext *clang_ast = GetClangAST();
368 void *clang_type = GetClangType();
369 bool omit_empty_base_classes = true;
370 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
371 clang_type,
372 name.GetCString(),
373 omit_empty_base_classes,
374 child_indexes);
375 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000377 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
378 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
379
380 child_sp = GetChildAtIndex(*pos, can_create);
381 for (++pos; pos != end; ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000383 if (child_sp)
Jim Ingham78a685a2011-04-16 00:01:13 +0000384 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000385 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
386 child_sp = new_child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000387 }
Greg Claytondea8cb42011-06-29 22:09:02 +0000388 else
389 {
390 child_sp.reset();
391 }
392
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 }
394 }
395 return child_sp;
396}
397
398
399uint32_t
400ValueObject::GetNumChildren ()
401{
Greg Clayton288bdf92010-09-02 02:59:18 +0000402 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403 {
404 SetNumChildren (CalculateNumChildren());
405 }
406 return m_children.size();
407}
408void
409ValueObject::SetNumChildren (uint32_t num_children)
410{
Greg Clayton288bdf92010-09-02 02:59:18 +0000411 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000412 m_children.resize(num_children);
413}
414
415void
416ValueObject::SetName (const char *name)
417{
418 m_name.SetCString(name);
419}
420
421void
422ValueObject::SetName (const ConstString &name)
423{
424 m_name = name;
425}
426
Jim Ingham58b59f92011-04-22 23:53:53 +0000427ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
429{
Jim Ingham2eec4872011-05-07 00:10:58 +0000430 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000431
Greg Claytondea8cb42011-06-29 22:09:02 +0000432 bool omit_empty_base_classes = true;
433
434 std::string child_name_str;
435 uint32_t child_byte_size = 0;
436 int32_t child_byte_offset = 0;
437 uint32_t child_bitfield_bit_size = 0;
438 uint32_t child_bitfield_bit_offset = 0;
439 bool child_is_base_class = false;
440 bool child_is_deref_of_parent = false;
441
442 const bool transparent_pointers = synthetic_array_member == false;
443 clang::ASTContext *clang_ast = GetClangAST();
444 clang_type_t clang_type = GetClangType();
445 clang_type_t child_clang_type;
446
447 ExecutionContext exe_ctx;
448 GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
449
450 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
451 clang_ast,
452 GetName().GetCString(),
453 clang_type,
454 idx,
455 transparent_pointers,
456 omit_empty_base_classes,
457 child_name_str,
458 child_byte_size,
459 child_byte_offset,
460 child_bitfield_bit_size,
461 child_bitfield_bit_offset,
462 child_is_base_class,
463 child_is_deref_of_parent);
464 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 {
Greg Claytondea8cb42011-06-29 22:09:02 +0000466 if (synthetic_index)
467 child_byte_offset += child_byte_size * synthetic_index;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468
Greg Claytondea8cb42011-06-29 22:09:02 +0000469 ConstString child_name;
470 if (!child_name_str.empty())
471 child_name.SetCString (child_name_str.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472
Greg Claytondea8cb42011-06-29 22:09:02 +0000473 valobj = new ValueObjectChild (*this,
474 clang_ast,
475 child_clang_type,
476 child_name,
477 child_byte_size,
478 child_byte_offset,
479 child_bitfield_bit_size,
480 child_bitfield_bit_offset,
481 child_is_base_class,
482 child_is_deref_of_parent);
483 if (m_pointers_point_to_load_addrs)
484 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000486
Jim Ingham58b59f92011-04-22 23:53:53 +0000487 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488}
489
490const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000491ValueObject::GetSummaryAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000492{
Jim Ingham6035b672011-03-31 00:19:25 +0000493 if (UpdateValueIfNeeded ())
Enrico Granata4becb372011-06-29 22:27:15 +0000494 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495 if (m_summary_str.empty())
496 {
Enrico Granata4becb372011-06-29 22:27:15 +0000497 if (m_last_summary_format.get())
498 {
499 StreamString s;
500 ExecutionContext exe_ctx;
501 this->GetExecutionContextScope()->CalculateExecutionContext(exe_ctx);
502 SymbolContext sc = exe_ctx.frame->GetSymbolContext(eSymbolContextEverything);
Enrico Granata0a3958e2011-07-02 00:25:22 +0000503
504 if (m_last_summary_format->m_show_members_oneliner)
Enrico Granata4becb372011-06-29 22:27:15 +0000505 {
Enrico Granata0a3958e2011-07-02 00:25:22 +0000506 const uint32_t num_children = GetNumChildren();
507 if (num_children)
508 {
509
510 s.PutChar('(');
511
512 for (uint32_t idx=0; idx<num_children; ++idx)
513 {
514 ValueObjectSP child_sp(GetChildAtIndex(idx, true));
515 if (child_sp.get())
516 {
517 if (idx)
518 s.PutCString(", ");
519 s.PutCString(child_sp.get()->GetName().AsCString());
520 s.PutChar('=');
Enrico Granata9fc19442011-07-06 02:13:41 +0000521 s.PutCString(child_sp.get()->GetPrintableRepresentation());
Enrico Granata0a3958e2011-07-02 00:25:22 +0000522 }
523 }
524
525 s.PutChar(')');
526
527 m_summary_str.swap(s.GetString());
528 return m_summary_str.c_str();
529 }
530 else
531 return "()";
532
Enrico Granata4becb372011-06-29 22:27:15 +0000533 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000534 else
535 {
536 if (Debugger::FormatPrompt(m_last_summary_format->m_format.c_str(), &sc, &exe_ctx, &sc.line_entry.range.GetBaseAddress(), s, NULL, this))
537 {
538 m_summary_str.swap(s.GetString());
539 return m_summary_str.c_str();
540 }
541 else
542 return NULL;
543 }
Enrico Granata4becb372011-06-29 22:27:15 +0000544 }
545
Greg Clayton73b472d2010-10-27 03:32:59 +0000546 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000547
548 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000549 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000550 {
Greg Clayton737b9322010-09-13 03:32:57 +0000551 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000552 clang_type_t elem_or_pointee_clang_type;
553 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000554 GetClangAST(),
555 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000556
Jim Ingham6035b672011-03-31 00:19:25 +0000557 ExecutionContextScope *exe_scope = GetExecutionContextScope();
558 if (exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 {
Jim Ingham6035b672011-03-31 00:19:25 +0000560 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
561 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 {
Jim Ingham6035b672011-03-31 00:19:25 +0000563 Process *process = exe_scope->CalculateProcess();
564 if (process != NULL)
565 {
566 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
567 AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568
Jim Ingham6035b672011-03-31 00:19:25 +0000569 size_t cstr_len = 0;
Greg Clayton5707d222011-07-09 17:17:07 +0000570 bool capped_data = false;
Jim Ingham6035b672011-03-31 00:19:25 +0000571 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 {
Jim Ingham6035b672011-03-31 00:19:25 +0000573 // We have an array
574 cstr_len = ClangASTContext::GetArraySize (clang_type);
Greg Clayton5707d222011-07-09 17:17:07 +0000575 if (cstr_len > 512) // TODO: make cap a setting
576 {
577 cstr_len = ClangASTContext::GetArraySize (clang_type);
578 if (cstr_len > 512) // TODO: make cap a setting
579 {
580 capped_data = true;
581 cstr_len = 512;
582 }
583 }
Jim Ingham6035b672011-03-31 00:19:25 +0000584 cstr_address = GetAddressOf (cstr_address_type, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 }
Greg Clayton737b9322010-09-13 03:32:57 +0000586 else
587 {
Jim Ingham6035b672011-03-31 00:19:25 +0000588 // We have a pointer
589 cstr_address = GetPointerValue (cstr_address_type, true);
Greg Clayton737b9322010-09-13 03:32:57 +0000590 }
Jim Ingham6035b672011-03-31 00:19:25 +0000591 if (cstr_address != LLDB_INVALID_ADDRESS)
Greg Clayton737b9322010-09-13 03:32:57 +0000592 {
Jim Ingham6035b672011-03-31 00:19:25 +0000593 DataExtractor data;
594 size_t bytes_read = 0;
595 std::vector<char> data_buffer;
596 Error error;
597 if (cstr_len > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000598 {
Jim Ingham6035b672011-03-31 00:19:25 +0000599 data_buffer.resize(cstr_len);
600 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
601 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
602 if (bytes_read > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000603 {
Jim Ingham6035b672011-03-31 00:19:25 +0000604 sstr << '"';
605 data.Dump (&sstr,
606 0, // Start offset in "data"
Greg Clayton4e4294b2011-06-17 23:50:44 +0000607 eFormatCharArray, // Print as characters
Jim Ingham6035b672011-03-31 00:19:25 +0000608 1, // Size of item (1 byte for a char!)
609 bytes_read, // How many bytes to print?
610 UINT32_MAX, // num per line
611 LLDB_INVALID_ADDRESS,// base address
612 0, // bitfield bit size
613 0); // bitfield bit offset
Greg Clayton5707d222011-07-09 17:17:07 +0000614 if (capped_data)
615 sstr << "...";
Jim Ingham6035b672011-03-31 00:19:25 +0000616 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000617 }
618 }
Jim Ingham6035b672011-03-31 00:19:25 +0000619 else
620 {
621 const size_t k_max_buf_size = 256;
622 data_buffer.resize (k_max_buf_size + 1);
623 // NULL terminate in case we don't get the entire C string
624 data_buffer.back() = '\0';
Greg Clayton737b9322010-09-13 03:32:57 +0000625
Jim Ingham6035b672011-03-31 00:19:25 +0000626 sstr << '"';
627
628 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
629 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
630 {
631 size_t len = strlen(&data_buffer.front());
632 if (len == 0)
633 break;
634 if (len > bytes_read)
635 len = bytes_read;
636
637 data.Dump (&sstr,
638 0, // Start offset in "data"
Greg Clayton4e4294b2011-06-17 23:50:44 +0000639 eFormatCharArray, // Print as characters
Jim Ingham6035b672011-03-31 00:19:25 +0000640 1, // Size of item (1 byte for a char!)
641 len, // How many bytes to print?
642 UINT32_MAX, // num per line
643 LLDB_INVALID_ADDRESS,// base address
644 0, // bitfield bit size
645 0); // bitfield bit offset
646
647 if (len < k_max_buf_size)
648 break;
649 cstr_address += k_max_buf_size;
650 }
651 sstr << '"';
652 }
653 }
Greg Clayton737b9322010-09-13 03:32:57 +0000654 }
Jim Ingham6035b672011-03-31 00:19:25 +0000655
656 if (sstr.GetSize() > 0)
657 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
Greg Clayton737b9322010-09-13 03:32:57 +0000658 }
Jim Ingham6035b672011-03-31 00:19:25 +0000659 else if (ClangASTContext::IsFunctionPointerType (clang_type))
Greg Clayton737b9322010-09-13 03:32:57 +0000660 {
Jim Ingham6035b672011-03-31 00:19:25 +0000661 AddressType func_ptr_address_type = eAddressTypeInvalid;
662 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
663
664 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
665 {
666 switch (func_ptr_address_type)
667 {
668 case eAddressTypeInvalid:
669 case eAddressTypeFile:
670 break;
671
672 case eAddressTypeLoad:
673 {
674 Address so_addr;
675 Target *target = exe_scope->CalculateTarget();
676 if (target && target->GetSectionLoadList().IsEmpty() == false)
677 {
678 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
679 {
680 so_addr.Dump (&sstr,
681 exe_scope,
682 Address::DumpStyleResolvedDescription,
683 Address::DumpStyleSectionNameOffset);
684 }
685 }
686 }
687 break;
688
689 case eAddressTypeHost:
690 break;
691 }
692 }
693 if (sstr.GetSize() > 0)
694 {
695 m_summary_str.assign (1, '(');
696 m_summary_str.append (sstr.GetData(), sstr.GetSize());
697 m_summary_str.append (1, ')');
698 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 }
700 }
701 }
702 }
703 }
704 if (m_summary_str.empty())
705 return NULL;
706 return m_summary_str.c_str();
707}
708
Jim Ingham53c47f12010-09-10 23:12:17 +0000709const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000710ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +0000711{
Enrico Granata0a3958e2011-07-02 00:25:22 +0000712
Jim Ingham6035b672011-03-31 00:19:25 +0000713 if (!UpdateValueIfNeeded ())
Jim Ingham53c47f12010-09-10 23:12:17 +0000714 return NULL;
Enrico Granata0a3958e2011-07-02 00:25:22 +0000715
716 if (!m_object_desc_str.empty())
717 return m_object_desc_str.c_str();
718
Jim Ingham6035b672011-03-31 00:19:25 +0000719 ExecutionContextScope *exe_scope = GetExecutionContextScope();
720 if (exe_scope == NULL)
721 return NULL;
722
Jim Ingham53c47f12010-09-10 23:12:17 +0000723 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000724 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000725 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000726
Jim Ingham53c47f12010-09-10 23:12:17 +0000727 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000728
729 lldb::LanguageType language = GetObjectRuntimeLanguage();
730 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
731
Jim Inghama2cf2632010-12-23 02:29:54 +0000732 if (runtime == NULL)
733 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000734 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +0000735 clang_type_t opaque_qual_type = GetClangType();
736 if (opaque_qual_type != NULL)
737 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000738 bool is_signed;
739 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
740 || ClangASTContext::IsPointerType (opaque_qual_type))
741 {
Jim Inghama2cf2632010-12-23 02:29:54 +0000742 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +0000743 }
Jim Inghama2cf2632010-12-23 02:29:54 +0000744 }
745 }
746
Jim Ingham8d543de2011-03-31 23:01:21 +0000747 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +0000748 {
749 m_object_desc_str.append (s.GetData());
750 }
Sean Callanan672ad942010-10-23 00:18:49 +0000751
752 if (m_object_desc_str.empty())
753 return NULL;
754 else
755 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000756}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757
758const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000759ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760{
761 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000762 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 {
Jim Ingham6035b672011-03-31 00:19:25 +0000764 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000765 {
766 if (m_value_str.empty())
767 {
768 const Value::ContextType context_type = m_value.GetContextType();
769
770 switch (context_type)
771 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000772 case Value::eContextTypeClangType:
773 case Value::eContextTypeLLDBType:
774 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000775 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000776 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 if (clang_type)
778 {
779 StreamString sstr;
Greg Clayton68ebae62011-04-28 20:55:26 +0000780 Format format = GetFormat();
Enrico Granata4becb372011-06-29 22:27:15 +0000781 if (format == eFormatDefault)
782 {
783 if (m_last_value_format)
784 format = m_last_value_format->m_format;
785 else
Enrico Granata9fc19442011-07-06 02:13:41 +0000786 // force the system into using unsigned integers for bitfields
787 format = (m_is_bitfield_for_scalar ? eFormatUnsigned :
788 ClangASTType::GetFormat(clang_type));
Enrico Granata4becb372011-06-29 22:27:15 +0000789 }
Greg Clayton32c40852010-10-06 03:09:11 +0000790
791 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
792 clang_type, // The clang type to display
793 &sstr,
Enrico Granata4becb372011-06-29 22:27:15 +0000794 format, // Format to display this type with
Greg Clayton32c40852010-10-06 03:09:11 +0000795 m_data, // Data to extract from
796 0, // Byte offset into "m_data"
797 GetByteSize(), // Byte size of item in "m_data"
798 GetBitfieldBitSize(), // Bitfield bit size
799 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 m_value_str.swap(sstr.GetString());
801 else
Greg Clayton007d5be2011-05-30 00:49:24 +0000802 {
803 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
804 m_data.GetByteSize(),
805 GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 m_value_str.clear();
Greg Clayton007d5be2011-05-30 00:49:24 +0000807 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 }
809 }
810 break;
811
Greg Clayton526e5af2010-11-13 03:52:47 +0000812 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 {
814 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
815 if (reg_info)
816 {
817 StreamString reg_sstr;
818 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
819 m_value_str.swap(reg_sstr.GetString());
820 }
821 }
822 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000823
824 default:
825 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
827 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000828
829 if (!m_value_did_change && m_old_value_valid)
830 {
831 // The value was gotten successfully, so we consider the
832 // value as changed if the value string differs
833 SetValueDidChange (m_old_value_str != m_value_str);
834 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 }
836 }
837 if (m_value_str.empty())
838 return NULL;
839 return m_value_str.c_str();
840}
841
Enrico Granata0a3958e2011-07-02 00:25:22 +0000842const char *
843ValueObject::GetPrintableRepresentation(ValueObjectRepresentationStyle val_obj_display,
844 lldb::Format custom_format)
845{
846 if(custom_format != lldb::eFormatInvalid)
847 SetFormat(custom_format);
848
849 const char * return_value;
850
851 switch(val_obj_display)
852 {
853 case eDisplayValue:
854 return_value = GetValueAsCString();
855 break;
856 case eDisplaySummary:
857 return_value = GetSummaryAsCString();
858 break;
859 case eDisplayLanguageSpecific:
860 return_value = GetObjectDescription();
861 break;
862 }
863
Enrico Granata9fc19442011-07-06 02:13:41 +0000864 if (!return_value)
865 {
866 // try to pick the other choice
867 if (val_obj_display == eDisplayValue)
868 return_value = GetSummaryAsCString();
869 else if (val_obj_display == eDisplaySummary)
870 return_value = GetValueAsCString();
871 else
872 return_value = "";
873 }
Enrico Granata0a3958e2011-07-02 00:25:22 +0000874
Enrico Granata9fc19442011-07-06 02:13:41 +0000875 return (return_value ? return_value : "");
Enrico Granata0a3958e2011-07-02 00:25:22 +0000876
877}
878
Enrico Granata9fc19442011-07-06 02:13:41 +0000879bool
880ValueObject::DumpPrintableRepresentation(Stream& s,
881 ValueObjectRepresentationStyle val_obj_display,
882 lldb::Format custom_format)
883{
884 const char *targetvalue = GetPrintableRepresentation(val_obj_display, custom_format);
885 if(targetvalue)
886 s.PutCString(targetvalue);
887 bool var_success = (targetvalue != NULL);
888 if(custom_format != eFormatInvalid)
889 SetFormat(eFormatDefault);
890 return var_success;
891}
892
Greg Clayton737b9322010-09-13 03:32:57 +0000893addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000894ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000895{
Jim Ingham78a685a2011-04-16 00:01:13 +0000896 if (!UpdateValueIfNeeded())
897 return LLDB_INVALID_ADDRESS;
898
Greg Clayton73b472d2010-10-27 03:32:59 +0000899 switch (m_value.GetValueType())
900 {
901 case Value::eValueTypeScalar:
902 if (scalar_is_load_address)
903 {
904 address_type = eAddressTypeLoad;
905 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
906 }
907 break;
908
909 case Value::eValueTypeLoadAddress:
910 case Value::eValueTypeFileAddress:
911 case Value::eValueTypeHostAddress:
912 {
913 address_type = m_value.GetValueAddressType ();
914 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
915 }
916 break;
917 }
918 address_type = eAddressTypeInvalid;
919 return LLDB_INVALID_ADDRESS;
920}
921
922addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000923ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000924{
925 lldb::addr_t address = LLDB_INVALID_ADDRESS;
926 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +0000927
928 if (!UpdateValueIfNeeded())
929 return address;
930
Greg Clayton73b472d2010-10-27 03:32:59 +0000931 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000932 {
933 case Value::eValueTypeScalar:
934 if (scalar_is_load_address)
935 {
936 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
937 address_type = eAddressTypeLoad;
938 }
939 break;
940
941 case Value::eValueTypeLoadAddress:
942 case Value::eValueTypeFileAddress:
943 case Value::eValueTypeHostAddress:
944 {
945 uint32_t data_offset = 0;
946 address = m_data.GetPointer(&data_offset);
947 address_type = m_value.GetValueAddressType();
948 if (address_type == eAddressTypeInvalid)
949 address_type = eAddressTypeLoad;
950 }
951 break;
952 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000953
954 if (m_pointers_point_to_load_addrs)
955 address_type = eAddressTypeLoad;
956
Greg Clayton737b9322010-09-13 03:32:57 +0000957 return address;
958}
959
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000960bool
Jim Ingham6035b672011-03-31 00:19:25 +0000961ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962{
963 // Make sure our value is up to date first so that our location and location
964 // type is valid.
Jim Ingham6035b672011-03-31 00:19:25 +0000965 if (!UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966 return false;
967
968 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000969 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970
971 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000972 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973 switch (encoding)
974 {
975 case eEncodingInvalid:
976 return false;
977
978 case eEncodingUint:
979 if (byte_size > sizeof(unsigned long long))
980 {
981 return false;
982 }
983 else
984 {
985 unsigned long long ull_val = strtoull(value_str, &end, 0);
986 if (end && *end != '\0')
987 return false;
Greg Clayton644247c2011-07-07 01:59:51 +0000988 m_value.GetScalar() = ull_val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 // Limit the bytes in our m_data appropriately.
990 m_value.GetScalar().GetData (m_data, byte_size);
991 }
992 break;
993
994 case eEncodingSint:
995 if (byte_size > sizeof(long long))
996 {
997 return false;
998 }
999 else
1000 {
1001 long long sll_val = strtoll(value_str, &end, 0);
1002 if (end && *end != '\0')
1003 return false;
Greg Clayton644247c2011-07-07 01:59:51 +00001004 m_value.GetScalar() = sll_val;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001005 // Limit the bytes in our m_data appropriately.
1006 m_value.GetScalar().GetData (m_data, byte_size);
1007 }
1008 break;
1009
1010 case eEncodingIEEE754:
1011 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +00001013 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001014 if (dst != NULL)
1015 {
1016 // We are decoding a float into host byte order below, so make
1017 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +00001018 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
1020 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +00001021 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001022 value_str,
1023 dst,
1024 byte_size);
1025
1026 if (converted_byte_size == byte_size)
1027 {
1028 }
1029 }
1030 }
1031 break;
1032
1033 case eEncodingVector:
1034 return false;
1035
1036 default:
1037 return false;
1038 }
1039
1040 // If we have made it here the value is in m_data and we should write it
1041 // out to the target
1042 return Write ();
1043}
1044
1045bool
1046ValueObject::Write ()
1047{
1048 // Clear the update ID so the next time we try and read the value
1049 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +00001050 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051
1052 // TODO: when Value has a method to write a value back, call it from here.
1053 return false;
1054
1055}
1056
Jim Ingham5a369122010-09-28 01:25:32 +00001057lldb::LanguageType
1058ValueObject::GetObjectRuntimeLanguage ()
1059{
Greg Clayton73b472d2010-10-27 03:32:59 +00001060 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +00001061 if (opaque_qual_type == NULL)
1062 return lldb::eLanguageTypeC;
1063
1064 // If the type is a reference, then resolve it to what it refers to first:
1065 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
1066 if (qual_type->isAnyPointerType())
1067 {
1068 if (qual_type->isObjCObjectPointerType())
1069 return lldb::eLanguageTypeObjC;
1070
1071 clang::QualType pointee_type (qual_type->getPointeeType());
1072 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
1073 return lldb::eLanguageTypeC_plus_plus;
1074 if (pointee_type->isObjCObjectOrInterfaceType())
1075 return lldb::eLanguageTypeObjC;
1076 if (pointee_type->isObjCClassType())
1077 return lldb::eLanguageTypeObjC;
1078 }
1079 else
1080 {
1081 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
1082 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +00001083 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +00001084 return lldb::eLanguageTypeC_plus_plus;
1085 }
1086
1087 return lldb::eLanguageTypeC;
1088}
1089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090void
Jim Ingham58b59f92011-04-22 23:53:53 +00001091ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092{
Jim Ingham58b59f92011-04-22 23:53:53 +00001093 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001094}
1095
1096ValueObjectSP
1097ValueObject::GetSyntheticChild (const ConstString &key) const
1098{
1099 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +00001100 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001101 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +00001102 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001103 return synthetic_child_sp;
1104}
1105
1106bool
1107ValueObject::IsPointerType ()
1108{
Greg Clayton1be10fc2010-09-29 01:12:09 +00001109 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110}
1111
Jim Inghamb7603bb2011-03-18 00:05:18 +00001112bool
Enrico Granata9fc19442011-07-06 02:13:41 +00001113ValueObject::IsScalarType ()
1114{
1115 return ClangASTContext::IsScalarType (GetClangType());
1116}
1117
1118bool
Jim Inghamb7603bb2011-03-18 00:05:18 +00001119ValueObject::IsIntegerType (bool &is_signed)
1120{
1121 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
1122}
Greg Clayton73b472d2010-10-27 03:32:59 +00001123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124bool
1125ValueObject::IsPointerOrReferenceType ()
1126{
Greg Clayton007d5be2011-05-30 00:49:24 +00001127 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
1128}
1129
1130bool
1131ValueObject::IsPossibleCPlusPlusDynamicType ()
1132{
1133 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001134}
1135
Greg Claytondea8cb42011-06-29 22:09:02 +00001136bool
1137ValueObject::IsPossibleDynamicType ()
1138{
1139 return ClangASTContext::IsPossibleDynamicType (GetClangAST (), GetClangType());
1140}
1141
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001142ValueObjectSP
1143ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
1144{
1145 ValueObjectSP synthetic_child_sp;
1146 if (IsPointerType ())
1147 {
1148 char index_str[64];
1149 snprintf(index_str, sizeof(index_str), "[%i]", index);
1150 ConstString index_const_str(index_str);
1151 // Check if we have already created a synthetic array member in this
1152 // valid object. If we have we will re-use it.
1153 synthetic_child_sp = GetSyntheticChild (index_const_str);
1154 if (!synthetic_child_sp)
1155 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001156 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001157 // We haven't made a synthetic array member for INDEX yet, so
1158 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001159 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001160
1161 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001162 if (synthetic_child)
1163 {
1164 AddSyntheticChild(index_const_str, synthetic_child);
1165 synthetic_child_sp = synthetic_child->GetSP();
Enrico Granata0a3958e2011-07-02 00:25:22 +00001166 synthetic_child_sp->SetName(index_str);
1167 synthetic_child_sp->m_is_array_item_for_pointer = true;
Jim Ingham58b59f92011-04-22 23:53:53 +00001168 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 }
1170 }
1171 return synthetic_child_sp;
1172}
Jim Ingham22777012010-09-23 02:01:19 +00001173
Enrico Granata9fc19442011-07-06 02:13:41 +00001174ValueObjectSP
1175ValueObject::GetSyntheticBitFieldChild (uint32_t from, uint32_t to, bool can_create)
1176{
1177 ValueObjectSP synthetic_child_sp;
1178 if (IsScalarType ())
1179 {
1180 char index_str[64];
1181 snprintf(index_str, sizeof(index_str), "[%i-%i]", from, to);
1182 ConstString index_const_str(index_str);
1183 // Check if we have already created a synthetic array member in this
1184 // valid object. If we have we will re-use it.
1185 synthetic_child_sp = GetSyntheticChild (index_const_str);
1186 if (!synthetic_child_sp)
1187 {
1188 ValueObjectChild *synthetic_child;
1189 // We haven't made a synthetic array member for INDEX yet, so
1190 // lets make one and cache it for any future reference.
1191 synthetic_child = new ValueObjectChild(*this,
1192 GetClangAST(),
1193 GetClangType(),
1194 index_const_str,
1195 GetByteSize(),
1196 0,
1197 to-from+1,
1198 from,
1199 false,
1200 false);
1201
1202 // Cache the value if we got one back...
1203 if (synthetic_child)
1204 {
1205 AddSyntheticChild(index_const_str, synthetic_child);
1206 synthetic_child_sp = synthetic_child->GetSP();
1207 synthetic_child_sp->SetName(index_str);
1208 synthetic_child_sp->m_is_bitfield_for_scalar = true;
1209 }
1210 }
1211 }
1212 return synthetic_child_sp;
1213}
1214
Jim Ingham78a685a2011-04-16 00:01:13 +00001215void
Jim Ingham2837b762011-05-04 03:43:18 +00001216ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00001217{
Jim Ingham2837b762011-05-04 03:43:18 +00001218 if (use_dynamic == lldb::eNoDynamicValues)
1219 return;
1220
Jim Ingham58b59f92011-04-22 23:53:53 +00001221 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00001222 {
1223 Process *process = m_update_point.GetProcess();
1224 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +00001225
Jim Ingham78a685a2011-04-16 00:01:13 +00001226
1227 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1228 // hard code this everywhere.
1229 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1230 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1231 {
1232 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1233 if (runtime)
1234 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1235 }
1236 else
1237 {
1238 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1239 if (cpp_runtime)
1240 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1241
1242 if (!worth_having_dynamic_value)
1243 {
1244 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1245 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001246 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001247 }
1248 }
1249
1250 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001251 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001252
1253// if (worth_having_dynamic_value)
1254// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1255
Jim Ingham78a685a2011-04-16 00:01:13 +00001256 }
1257}
1258
Jim Ingham58b59f92011-04-22 23:53:53 +00001259ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001260ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001261{
Jim Ingham2837b762011-05-04 03:43:18 +00001262 if (use_dynamic == lldb::eNoDynamicValues)
1263 return ValueObjectSP();
1264
1265 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001266 {
Jim Ingham2837b762011-05-04 03:43:18 +00001267 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001268 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001269 if (m_dynamic_value)
1270 return m_dynamic_value->GetSP();
1271 else
1272 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001273}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001274
Greg Claytone221f822011-01-21 01:59:00 +00001275bool
1276ValueObject::GetBaseClassPath (Stream &s)
1277{
1278 if (IsBaseClass())
1279 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001280 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001281 clang_type_t clang_type = GetClangType();
1282 std::string cxx_class_name;
1283 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1284 if (this_had_base_class)
1285 {
1286 if (parent_had_base_class)
1287 s.PutCString("::");
1288 s.PutCString(cxx_class_name.c_str());
1289 }
1290 return parent_had_base_class || this_had_base_class;
1291 }
1292 return false;
1293}
1294
1295
1296ValueObject *
1297ValueObject::GetNonBaseClassParent()
1298{
Jim Ingham78a685a2011-04-16 00:01:13 +00001299 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001300 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001301 if (GetParent()->IsBaseClass())
1302 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001303 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001304 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001305 }
1306 return NULL;
1307}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001308
1309void
Enrico Granata4becb372011-06-29 22:27:15 +00001310ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes, GetExpressionPathFormat epformat)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001311{
Greg Claytone221f822011-01-21 01:59:00 +00001312 const bool is_deref_of_parent = IsDereferenceOfParent ();
Greg Claytone221f822011-01-21 01:59:00 +00001313
Enrico Granata4becb372011-06-29 22:27:15 +00001314 if(is_deref_of_parent && epformat == eDereferencePointers) {
1315 // this is the original format of GetExpressionPath() producing code like *(a_ptr).memberName, which is entirely
1316 // fine, until you put this into StackFrame::GetValueForVariableExpressionPath() which prefers to see a_ptr->memberName.
1317 // the eHonorPointers mode is meant to produce strings in this latter format
1318 s.PutCString("*(");
1319 }
Greg Claytone221f822011-01-21 01:59:00 +00001320
Enrico Granata4becb372011-06-29 22:27:15 +00001321 ValueObject* parent = GetParent();
1322
1323 if (parent)
1324 parent->GetExpressionPath (s, qualify_cxx_base_classes, epformat);
Enrico Granata0a3958e2011-07-02 00:25:22 +00001325
1326 // if we are a deref_of_parent just because we are synthetic array
1327 // members made up to allow ptr[%d] syntax to work in variable
1328 // printing, then add our name ([%d]) to the expression path
1329 if(m_is_array_item_for_pointer && epformat == eHonorPointers)
1330 s.PutCString(m_name.AsCString());
Enrico Granata4becb372011-06-29 22:27:15 +00001331
Greg Claytone221f822011-01-21 01:59:00 +00001332 if (!IsBaseClass())
1333 {
1334 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001335 {
Greg Claytone221f822011-01-21 01:59:00 +00001336 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1337 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001338 {
Greg Claytone221f822011-01-21 01:59:00 +00001339 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1340 if (non_base_class_parent_clang_type)
1341 {
1342 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1343
Enrico Granata4becb372011-06-29 22:27:15 +00001344 if(parent && parent->IsDereferenceOfParent() && epformat == eHonorPointers)
Greg Claytone221f822011-01-21 01:59:00 +00001345 {
1346 s.PutCString("->");
1347 }
Enrico Granata4becb372011-06-29 22:27:15 +00001348 else
1349 {
1350 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1351 {
1352 s.PutCString("->");
1353 }
1354 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1355 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1356 {
1357 s.PutChar('.');
1358 }
Greg Claytone221f822011-01-21 01:59:00 +00001359 }
1360 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001361 }
Greg Claytone221f822011-01-21 01:59:00 +00001362
1363 const char *name = GetName().GetCString();
1364 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001365 {
Greg Claytone221f822011-01-21 01:59:00 +00001366 if (qualify_cxx_base_classes)
1367 {
1368 if (GetBaseClassPath (s))
1369 s.PutCString("::");
1370 }
1371 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001372 }
1373 }
1374 }
1375
Enrico Granata4becb372011-06-29 22:27:15 +00001376 if (is_deref_of_parent && epformat == eDereferencePointers) {
Greg Claytone221f822011-01-21 01:59:00 +00001377 s.PutChar(')');
Enrico Granata4becb372011-06-29 22:27:15 +00001378 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001379}
1380
Enrico Granatafc7a7f32011-07-08 02:51:01 +00001381lldb::ValueObjectSP
1382ValueObject::GetValueForExpressionPath(const char* expression,
1383 const char** first_unparsed,
1384 ExpressionPathScanEndReason* reason_to_stop,
1385 ExpressionPathEndResultType* final_value_type,
1386 const GetValueForExpressionPathOptions& options,
1387 ExpressionPathAftermath* final_task_on_target)
1388{
1389
1390 const char* dummy_first_unparsed;
1391 ExpressionPathScanEndReason dummy_reason_to_stop;
1392 ExpressionPathEndResultType dummy_final_value_type;
1393 ExpressionPathAftermath dummy_final_task_on_target = ValueObject::eNothing;
1394
1395 ValueObjectSP ret_val = GetValueForExpressionPath_Impl(expression,
1396 first_unparsed ? first_unparsed : &dummy_first_unparsed,
1397 reason_to_stop ? reason_to_stop : &dummy_reason_to_stop,
1398 final_value_type ? final_value_type : &dummy_final_value_type,
1399 options,
1400 final_task_on_target ? final_task_on_target : &dummy_final_task_on_target);
1401
1402 if (!final_task_on_target || *final_task_on_target == ValueObject::eNothing)
1403 {
1404 return ret_val;
1405 }
1406 if (ret_val.get() && *final_value_type == ePlain) // I can only deref and takeaddress of plain objects
1407 {
1408 if (*final_task_on_target == ValueObject::eDereference)
1409 {
1410 Error error;
1411 ValueObjectSP final_value = ret_val->Dereference(error);
1412 if (error.Fail() || !final_value.get())
1413 {
1414 *reason_to_stop = ValueObject::eDereferencingFailed;
1415 *final_value_type = ValueObject::eInvalid;
1416 return ValueObjectSP();
1417 }
1418 else
1419 {
1420 *final_task_on_target = ValueObject::eNothing;
1421 return final_value;
1422 }
1423 }
1424 if (*final_task_on_target == ValueObject::eTakeAddress)
1425 {
1426 Error error;
1427 ValueObjectSP final_value = ret_val->AddressOf(error);
1428 if (error.Fail() || !final_value.get())
1429 {
1430 *reason_to_stop = ValueObject::eTakingAddressFailed;
1431 *final_value_type = ValueObject::eInvalid;
1432 return ValueObjectSP();
1433 }
1434 else
1435 {
1436 *final_task_on_target = ValueObject::eNothing;
1437 return final_value;
1438 }
1439 }
1440 }
1441 return ret_val; // final_task_on_target will still have its original value, so you know I did not do it
1442}
1443
1444lldb::ValueObjectSP
1445ValueObject::GetValueForExpressionPath_Impl(const char* expression_cstr,
1446 const char** first_unparsed,
1447 ExpressionPathScanEndReason* reason_to_stop,
1448 ExpressionPathEndResultType* final_result,
1449 const GetValueForExpressionPathOptions& options,
1450 ExpressionPathAftermath* what_next)
1451{
1452 ValueObjectSP root = GetSP();
1453
1454 if (!root.get())
1455 return ValueObjectSP();
1456
1457 *first_unparsed = expression_cstr;
1458
1459 while (true)
1460 {
1461
1462 const char* expression_cstr = *first_unparsed; // hide the top level expression_cstr
1463
1464 lldb::clang_type_t root_clang_type = root->GetClangType();
1465
1466 if (!expression_cstr || *expression_cstr == '\0')
1467 {
1468 *reason_to_stop = ValueObject::eEndOfString;
1469 return root;
1470 }
1471
1472 switch (*expression_cstr)
1473 {
1474 case '-':
1475 {
1476 if (options.m_check_dot_vs_arrow_syntax &&
1477 !ClangASTContext::IsPointerType(root_clang_type)) // if you are trying to use -> on a non-pointer and I must catch the error
1478 {
1479 *first_unparsed = expression_cstr;
1480 *reason_to_stop = ValueObject::eArrowInsteadOfDot;
1481 *final_result = ValueObject::eInvalid;
1482 return ValueObjectSP();
1483 }
1484 const uint32_t pointer_type_flags = ClangASTContext::GetTypeInfo (root_clang_type, NULL, NULL);
1485 if ((pointer_type_flags & ClangASTContext::eTypeIsObjC) && // if yo are trying to extract an ObjC IVar when this is forbidden
1486 (pointer_type_flags & ClangASTContext::eTypeIsPointer) &&
1487 options.m_no_fragile_ivar)
1488 {
1489 *first_unparsed = expression_cstr;
1490 *reason_to_stop = ValueObject::eFragileIVarNotAllowed;
1491 *final_result = ValueObject::eInvalid;
1492 return ValueObjectSP();
1493 }
1494 if (expression_cstr[1] != '>')
1495 {
1496 *first_unparsed = expression_cstr;
1497 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1498 *final_result = ValueObject::eInvalid;
1499 return ValueObjectSP();
1500 }
1501 expression_cstr++; // skip the -
1502 }
1503 case '.': // or fallthrough from ->
1504 {
1505 if (options.m_check_dot_vs_arrow_syntax && *expression_cstr == '.' &&
1506 ClangASTContext::IsPointerType(root_clang_type)) // if you are trying to use . on a pointer and I must catch the error
1507 {
1508 *first_unparsed = expression_cstr;
1509 *reason_to_stop = ValueObject::eDotInsteadOfArrow;
1510 *final_result = ValueObject::eInvalid;
1511 return ValueObjectSP();
1512 }
1513 expression_cstr++; // skip .
1514 const char *next_separator = strpbrk(expression_cstr+1,"-.[");
1515 ConstString child_name;
1516 if (!next_separator) // if no other separator just expand this last layer
1517 {
1518 child_name.SetCString (expression_cstr);
1519 root = root->GetChildMemberWithName(child_name, true);
1520 if (root.get()) // we know we are done, so just return
1521 {
1522 *first_unparsed = '\0';
1523 *reason_to_stop = ValueObject::eEndOfString;
1524 *final_result = ValueObject::ePlain;
1525 return root;
1526 }
1527 else
1528 {
1529 *first_unparsed = expression_cstr;
1530 *reason_to_stop = ValueObject::eNoSuchChild;
1531 *final_result = ValueObject::eInvalid;
1532 return ValueObjectSP();
1533 }
1534 }
1535 else // other layers do expand
1536 {
1537 child_name.SetCStringWithLength(expression_cstr, next_separator - expression_cstr);
1538 root = root->GetChildMemberWithName(child_name, true);
1539 if (root.get()) // store the new root and move on
1540 {
1541 *first_unparsed = next_separator;
1542 *final_result = ValueObject::ePlain;
1543 continue;
1544 }
1545 else
1546 {
1547 *first_unparsed = expression_cstr;
1548 *reason_to_stop = ValueObject::eNoSuchChild;
1549 *final_result = ValueObject::eInvalid;
1550 return ValueObjectSP();
1551 }
1552 }
1553 break;
1554 }
1555 case '[':
1556 {
1557 if (!ClangASTContext::IsArrayType(root_clang_type) && !ClangASTContext::IsPointerType(root_clang_type)) // if this is not a T[] nor a T*
1558 {
1559 if (!ClangASTContext::IsScalarType(root_clang_type)) // if this is not even a scalar, this syntax is just plain wrong!
1560 {
1561 *first_unparsed = expression_cstr;
1562 *reason_to_stop = ValueObject::eRangeOperatorInvalid;
1563 *final_result = ValueObject::eInvalid;
1564 return ValueObjectSP();
1565 }
1566 else if (!options.m_allow_bitfields_syntax) // if this is a scalar, check that we can expand bitfields
1567 {
1568 *first_unparsed = expression_cstr;
1569 *reason_to_stop = ValueObject::eRangeOperatorNotAllowed;
1570 *final_result = ValueObject::eInvalid;
1571 return ValueObjectSP();
1572 }
1573 }
1574 if (*(expression_cstr+1) == ']') // if this is an unbounded range it only works for arrays
1575 {
1576 if (!ClangASTContext::IsArrayType(root_clang_type))
1577 {
1578 *first_unparsed = expression_cstr;
1579 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
1580 *final_result = ValueObject::eInvalid;
1581 return ValueObjectSP();
1582 }
1583 else // even if something follows, we cannot expand unbounded ranges, just let the caller do it
1584 {
1585 *first_unparsed = expression_cstr+2;
1586 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
1587 *final_result = ValueObject::eUnboundedRange;
1588 return root;
1589 }
1590 }
1591 const char *separator_position = ::strchr(expression_cstr+1,'-');
1592 const char *close_bracket_position = ::strchr(expression_cstr+1,']');
1593 if (!close_bracket_position) // if there is no ], this is a syntax error
1594 {
1595 *first_unparsed = expression_cstr;
1596 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1597 *final_result = ValueObject::eInvalid;
1598 return ValueObjectSP();
1599 }
1600 if (!separator_position || separator_position > close_bracket_position) // if no separator, this is either [] or [N]
1601 {
1602 char *end = NULL;
1603 unsigned long index = ::strtoul (expression_cstr+1, &end, 0);
1604 if (!end || end != close_bracket_position) // if something weird is in our way return an error
1605 {
1606 *first_unparsed = expression_cstr;
1607 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1608 *final_result = ValueObject::eInvalid;
1609 return ValueObjectSP();
1610 }
1611 if (end - expression_cstr == 1) // if this is [], only return a valid value for arrays
1612 {
1613 if (ClangASTContext::IsArrayType(root_clang_type))
1614 {
1615 *first_unparsed = expression_cstr+2;
1616 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
1617 *final_result = ValueObject::eUnboundedRange;
1618 return root;
1619 }
1620 else
1621 {
1622 *first_unparsed = expression_cstr;
1623 *reason_to_stop = ValueObject::eEmptyRangeNotAllowed;
1624 *final_result = ValueObject::eInvalid;
1625 return ValueObjectSP();
1626 }
1627 }
1628 // from here on we do have a valid index
1629 if (ClangASTContext::IsArrayType(root_clang_type))
1630 {
1631 root = root->GetChildAtIndex(index, true);
1632 if (!root.get())
1633 {
1634 *first_unparsed = expression_cstr;
1635 *reason_to_stop = ValueObject::eNoSuchChild;
1636 *final_result = ValueObject::eInvalid;
1637 return ValueObjectSP();
1638 }
1639 else
1640 {
1641 *first_unparsed = end+1; // skip ]
1642 *final_result = ValueObject::ePlain;
1643 continue;
1644 }
1645 }
1646 else if (ClangASTContext::IsPointerType(root_clang_type))
1647 {
1648 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
1649 ClangASTContext::IsScalarType(clang::QualType::getFromOpaquePtr(root_clang_type).getTypePtr()->getPointeeType().getAsOpaquePtr()))
1650 {
1651 Error error;
1652 root = root->Dereference(error);
1653 if (error.Fail() || !root.get())
1654 {
1655 *first_unparsed = expression_cstr;
1656 *reason_to_stop = ValueObject::eDereferencingFailed;
1657 *final_result = ValueObject::eInvalid;
1658 return ValueObjectSP();
1659 }
1660 else
1661 {
1662 *what_next = eNothing;
1663 continue;
1664 }
1665 }
1666 else
1667 {
1668 root = root->GetSyntheticArrayMemberFromPointer(index, true);
1669 if (!root.get())
1670 {
1671 *first_unparsed = expression_cstr;
1672 *reason_to_stop = ValueObject::eNoSuchChild;
1673 *final_result = ValueObject::eInvalid;
1674 return ValueObjectSP();
1675 }
1676 else
1677 {
1678 *first_unparsed = end+1; // skip ]
1679 *final_result = ValueObject::ePlain;
1680 continue;
1681 }
1682 }
1683 }
1684 else /*if (ClangASTContext::IsScalarType(root_clang_type))*/
1685 {
1686 root = root->GetSyntheticBitFieldChild(index, index, true);
1687 if (!root.get())
1688 {
1689 *first_unparsed = expression_cstr;
1690 *reason_to_stop = ValueObject::eNoSuchChild;
1691 *final_result = ValueObject::eInvalid;
1692 return ValueObjectSP();
1693 }
1694 else // we do not know how to expand members of bitfields, so we just return and let the caller do any further processing
1695 {
1696 *first_unparsed = end+1; // skip ]
1697 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
1698 *final_result = ValueObject::eBitfield;
1699 return root;
1700 }
1701 }
1702 }
1703 else // we have a low and a high index
1704 {
1705 char *end = NULL;
1706 unsigned long index_lower = ::strtoul (expression_cstr+1, &end, 0);
1707 if (!end || end != separator_position) // if something weird is in our way return an error
1708 {
1709 *first_unparsed = expression_cstr;
1710 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1711 *final_result = ValueObject::eInvalid;
1712 return ValueObjectSP();
1713 }
1714 unsigned long index_higher = ::strtoul (separator_position+1, &end, 0);
1715 if (!end || end != close_bracket_position) // if something weird is in our way return an error
1716 {
1717 *first_unparsed = expression_cstr;
1718 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1719 *final_result = ValueObject::eInvalid;
1720 return ValueObjectSP();
1721 }
1722 if (index_lower > index_higher) // swap indices if required
1723 {
1724 unsigned long temp = index_lower;
1725 index_lower = index_higher;
1726 index_higher = temp;
1727 }
1728 if (ClangASTContext::IsScalarType(root_clang_type)) // expansion only works for scalars
1729 {
1730 root = root->GetSyntheticBitFieldChild(index_lower, index_higher, true);
1731 if (!root.get())
1732 {
1733 *first_unparsed = expression_cstr;
1734 *reason_to_stop = ValueObject::eNoSuchChild;
1735 *final_result = ValueObject::eInvalid;
1736 return ValueObjectSP();
1737 }
1738 else
1739 {
1740 *first_unparsed = end+1; // skip ]
1741 *reason_to_stop = ValueObject::eBitfieldRangeOperatorMet;
1742 *final_result = ValueObject::eBitfield;
1743 return root;
1744 }
1745 }
1746 else if (ClangASTContext::IsPointerType(root_clang_type) && // 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
1747 *what_next == ValueObject::eDereference &&
1748 ClangASTContext::IsScalarType(clang::QualType::getFromOpaquePtr(root_clang_type).getTypePtr()->getPointeeType().getAsOpaquePtr()))
1749 {
1750 Error error;
1751 root = root->Dereference(error);
1752 if (error.Fail() || !root.get())
1753 {
1754 *first_unparsed = expression_cstr;
1755 *reason_to_stop = ValueObject::eDereferencingFailed;
1756 *final_result = ValueObject::eInvalid;
1757 return ValueObjectSP();
1758 }
1759 else
1760 {
1761 *what_next = ValueObject::eNothing;
1762 continue;
1763 }
1764 }
1765 else
1766 {
1767 *first_unparsed = expression_cstr;
1768 *reason_to_stop = ValueObject::eArrayRangeOperatorMet;
1769 *final_result = ValueObject::eBoundedRange;
1770 return root;
1771 }
1772 }
1773 break;
1774 }
1775 default: // some non-separator is in the way
1776 {
1777 *first_unparsed = expression_cstr;
1778 *reason_to_stop = ValueObject::eUnexpectedSymbol;
1779 *final_result = ValueObject::eInvalid;
1780 return ValueObjectSP();
1781 break;
1782 }
1783 }
1784 }
1785}
1786
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001787void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001788ValueObject::DumpValueObject
1789(
1790 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00001791 ValueObject *valobj,
1792 const char *root_valobj_name,
1793 uint32_t ptr_depth,
1794 uint32_t curr_depth,
1795 uint32_t max_depth,
1796 bool show_types,
1797 bool show_location,
1798 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00001799 lldb::DynamicValueType use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001800 bool scope_already_checked,
1801 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001802)
1803{
Greg Clayton007d5be2011-05-30 00:49:24 +00001804 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001805 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001806 bool update_success = valobj->UpdateValueIfNeeded ();
1807
1808 if (update_success && use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001809 {
Jim Ingham2837b762011-05-04 03:43:18 +00001810 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001811 if (dynamic_value)
1812 valobj = dynamic_value;
1813 }
1814
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001815 clang_type_t clang_type = valobj->GetClangType();
1816
Greg Clayton73b472d2010-10-27 03:32:59 +00001817 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001818 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001819 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1820 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001821
1822 const bool print_valobj = flat_output == false || has_value;
1823
1824 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001825 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001826 if (show_location)
1827 {
Jim Ingham6035b672011-03-31 00:19:25 +00001828 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001829 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001830
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001831 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001832
Greg Clayton7c8a9662010-11-02 01:50:16 +00001833 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001834 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001835 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001836
Greg Clayton1d3afba2010-10-05 00:00:42 +00001837
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001838 if (flat_output)
1839 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001840 // If we are showing types, also qualify the C++ base classes
1841 const bool qualify_cxx_base_classes = show_types;
1842 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001843 s.PutCString(" =");
1844 }
1845 else
1846 {
1847 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1848 s.Printf ("%s =", name_cstr);
1849 }
1850
Jim Ingham6035b672011-03-31 00:19:25 +00001851 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001852 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001853 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001854 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001855 }
1856
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001857 const char *val_cstr = NULL;
Enrico Granata4becb372011-06-29 22:27:15 +00001858 const char *sum_cstr = NULL;
1859 SummaryFormat* entry = valobj->m_last_summary_format.get();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001860
1861 if (err_cstr == NULL)
1862 {
Jim Ingham6035b672011-03-31 00:19:25 +00001863 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001864 err_cstr = valobj->GetError().AsCString();
1865 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001866
1867 if (err_cstr)
1868 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001869 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001870 }
1871 else
1872 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001873 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001874 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001875 {
Enrico Granata4becb372011-06-29 22:27:15 +00001876
1877 sum_cstr = valobj->GetSummaryAsCString();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001878
Enrico Granata4becb372011-06-29 22:27:15 +00001879 // We must calculate this value in realtime because entry might alter this variable's value
1880 // (e.g. by saying ${var%fmt}) and render precached values useless
1881 if (val_cstr && (!entry || entry->DoesPrintValue() || !sum_cstr))
1882 s.Printf(" %s", valobj->GetValueAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001883
Enrico Granata0a3958e2011-07-02 00:25:22 +00001884 if(sum_cstr)
1885 {
1886 // for some reason, using %@ (ObjC description) in a summary string, makes
1887 // us believe we need to reset ourselves, thus invalidating the content of
1888 // sum_cstr. Thus, IF we had a valid sum_cstr before, but it is now empty
1889 // let us recalculate it!
1890 if (sum_cstr[0] == '\0')
1891 s.Printf(" %s", valobj->GetSummaryAsCString());
1892 else
1893 s.Printf(" %s", sum_cstr);
1894 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001895
1896 if (use_objc)
1897 {
Jim Ingham6035b672011-03-31 00:19:25 +00001898 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001899 if (object_desc)
1900 s.Printf(" %s\n", object_desc);
1901 else
Sean Callanan672ad942010-10-23 00:18:49 +00001902 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001903 return;
Enrico Granata0a3958e2011-07-02 00:25:22 +00001904 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001905 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001906
1907 if (curr_depth < max_depth)
1908 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001909 // We will show children for all concrete types. We won't show
1910 // pointer contents unless a pointer depth has been specified.
1911 // We won't reference contents unless the reference is the
1912 // root object (depth of zero).
1913 bool print_children = true;
1914
1915 // Use a new temporary pointer depth in case we override the
1916 // current pointer depth below...
1917 uint32_t curr_ptr_depth = ptr_depth;
1918
1919 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1920 if (is_ptr || is_ref)
1921 {
1922 // We have a pointer or reference whose value is an address.
1923 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001924 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001925 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1926 print_children = false;
1927
1928 else if (is_ref && curr_depth == 0)
1929 {
1930 // If this is the root object (depth is zero) that we are showing
1931 // and it is a reference, and no pointer depth has been supplied
1932 // print out what it references. Don't do this at deeper depths
1933 // otherwise we can end up with infinite recursion...
1934 curr_ptr_depth = 1;
1935 }
1936
1937 if (curr_ptr_depth == 0)
1938 print_children = false;
1939 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001940
Enrico Granata0a3958e2011-07-02 00:25:22 +00001941 if (print_children && (!entry || entry->DoesPrintChildren() || !sum_cstr))
Greg Clayton1d3afba2010-10-05 00:00:42 +00001942 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001943 const uint32_t num_children = valobj->GetNumChildren();
1944 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001945 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001946 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001947 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001948 if (print_valobj)
1949 s.EOL();
1950 }
1951 else
1952 {
1953 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001954 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001955 s.IndentMore();
1956 }
1957
1958 for (uint32_t idx=0; idx<num_children; ++idx)
1959 {
1960 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1961 if (child_sp.get())
1962 {
1963 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001964 child_sp.get(),
1965 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001966 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001967 curr_depth + 1,
1968 max_depth,
1969 show_types,
1970 show_location,
1971 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00001972 use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001973 true,
1974 flat_output);
1975 }
1976 }
1977
1978 if (!flat_output)
1979 {
1980 s.IndentLess();
1981 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001982 }
1983 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001984 else if (has_children)
1985 {
1986 // Aggregate, no children...
1987 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001988 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001989 }
1990 else
1991 {
1992 if (print_valobj)
1993 s.EOL();
1994 }
1995
Greg Clayton1d3afba2010-10-05 00:00:42 +00001996 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001997 else
1998 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001999 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00002000 }
2001 }
2002 else
2003 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002004 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00002005 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00002006 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00002007 }
2008 }
2009 }
2010 }
2011}
2012
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002013
2014ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00002015ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002016{
2017 ValueObjectSP valobj_sp;
2018
Jim Ingham6035b672011-03-31 00:19:25 +00002019 if (UpdateValueIfNeeded() && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002020 {
Jim Ingham6035b672011-03-31 00:19:25 +00002021 ExecutionContextScope *exe_scope = GetExecutionContextScope();
2022 if (exe_scope)
2023 {
2024 ExecutionContext exe_ctx;
2025 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002026
Jim Ingham6035b672011-03-31 00:19:25 +00002027 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002028
Jim Ingham6035b672011-03-31 00:19:25 +00002029 DataExtractor data;
2030 data.SetByteOrder (m_data.GetByteOrder());
2031 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002032
Greg Clayton644247c2011-07-07 01:59:51 +00002033 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0, GetModule());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002034
Jim Ingham58b59f92011-04-22 23:53:53 +00002035 valobj_sp = ValueObjectConstResult::Create (exe_scope,
2036 ast,
2037 GetClangType(),
2038 name,
2039 data);
Jim Ingham6035b672011-03-31 00:19:25 +00002040 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002041 }
Jim Ingham6035b672011-03-31 00:19:25 +00002042
2043 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002044 {
Jim Ingham58b59f92011-04-22 23:53:53 +00002045 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002046 }
2047 return valobj_sp;
2048}
2049
2050lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00002051ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002052{
Jim Ingham58b59f92011-04-22 23:53:53 +00002053 if (m_deref_valobj)
2054 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00002055
Greg Clayton54979cd2010-12-15 05:08:08 +00002056 const bool is_pointer_type = IsPointerType();
2057 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002058 {
2059 bool omit_empty_base_classes = true;
2060
2061 std::string child_name_str;
2062 uint32_t child_byte_size = 0;
2063 int32_t child_byte_offset = 0;
2064 uint32_t child_bitfield_bit_size = 0;
2065 uint32_t child_bitfield_bit_offset = 0;
2066 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00002067 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002068 const bool transparent_pointers = false;
2069 clang::ASTContext *clang_ast = GetClangAST();
2070 clang_type_t clang_type = GetClangType();
2071 clang_type_t child_clang_type;
Jim Inghamd555bac2011-06-24 22:03:24 +00002072
2073 ExecutionContext exe_ctx;
2074 GetExecutionContextScope()->CalculateExecutionContext (exe_ctx);
2075
2076 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (&exe_ctx,
2077 clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002078 GetName().GetCString(),
2079 clang_type,
2080 0,
2081 transparent_pointers,
2082 omit_empty_base_classes,
2083 child_name_str,
2084 child_byte_size,
2085 child_byte_offset,
2086 child_bitfield_bit_size,
2087 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00002088 child_is_base_class,
2089 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00002090 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002091 {
2092 ConstString child_name;
2093 if (!child_name_str.empty())
2094 child_name.SetCString (child_name_str.c_str());
2095
Jim Ingham58b59f92011-04-22 23:53:53 +00002096 m_deref_valobj = new ValueObjectChild (*this,
2097 clang_ast,
2098 child_clang_type,
2099 child_name,
2100 child_byte_size,
2101 child_byte_offset,
2102 child_bitfield_bit_size,
2103 child_bitfield_bit_offset,
2104 child_is_base_class,
2105 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002106 }
2107 }
Greg Clayton54979cd2010-12-15 05:08:08 +00002108
Jim Ingham58b59f92011-04-22 23:53:53 +00002109 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00002110 {
2111 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00002112 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00002113 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002114 else
2115 {
Greg Clayton54979cd2010-12-15 05:08:08 +00002116 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002117 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002118
2119 if (is_pointer_type)
2120 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
2121 else
2122 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00002123 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002124 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002125}
2126
Jim Ingham78a685a2011-04-16 00:01:13 +00002127lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00002128ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002129{
Jim Ingham78a685a2011-04-16 00:01:13 +00002130 if (m_addr_of_valobj_sp)
2131 return m_addr_of_valobj_sp;
2132
Greg Claytone0d378b2011-03-24 21:19:54 +00002133 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002134 const bool scalar_is_load_address = false;
2135 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00002136 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002137 if (addr != LLDB_INVALID_ADDRESS)
2138 {
2139 switch (address_type)
2140 {
Greg Clayton54979cd2010-12-15 05:08:08 +00002141 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002142 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00002143 {
2144 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00002145 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00002146 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
2147 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002148 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00002149
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002150 case eAddressTypeFile:
2151 case eAddressTypeLoad:
2152 case eAddressTypeHost:
2153 {
2154 clang::ASTContext *ast = GetClangAST();
2155 clang_type_t clang_type = GetClangType();
2156 if (ast && clang_type)
2157 {
2158 std::string name (1, '&');
2159 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00002160 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
2161 ast,
2162 ClangASTContext::CreatePointerType (ast, clang_type),
2163 ConstString (name.c_str()),
2164 addr,
2165 eAddressTypeInvalid,
2166 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002167 }
2168 }
2169 break;
2170 }
2171 }
Jim Ingham78a685a2011-04-16 00:01:13 +00002172 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00002173}
2174
Greg Claytonb2dcc362011-05-05 23:32:56 +00002175
2176lldb::ValueObjectSP
2177ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
2178{
2179 lldb::ValueObjectSP valobj_sp;
2180 AddressType address_type;
2181 const bool scalar_is_load_address = true;
2182 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
2183
2184 if (ptr_value != LLDB_INVALID_ADDRESS)
2185 {
2186 Address ptr_addr (NULL, ptr_value);
2187
2188 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
2189 name,
2190 ptr_addr,
2191 clang_ast_type);
2192 }
2193 return valobj_sp;
2194}
2195
2196lldb::ValueObjectSP
2197ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
2198{
2199 lldb::ValueObjectSP valobj_sp;
2200 AddressType address_type;
2201 const bool scalar_is_load_address = true;
2202 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
2203
2204 if (ptr_value != LLDB_INVALID_ADDRESS)
2205 {
2206 Address ptr_addr (NULL, ptr_value);
2207
2208 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
2209 name,
2210 ptr_addr,
2211 type_sp);
2212 }
2213 return valobj_sp;
2214}
2215
2216
Jim Ingham6035b672011-03-31 00:19:25 +00002217ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00002218 m_thread_id (LLDB_INVALID_UID),
2219 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00002220{
2221}
2222
2223ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00002224 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002225 m_first_update (true),
Jim Ingham89b61092011-07-06 17:42:14 +00002226 m_thread_id (LLDB_INVALID_THREAD_ID),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002227 m_stop_id (0)
2228
Jim Ingham6035b672011-03-31 00:19:25 +00002229{
2230 ExecutionContext exe_ctx;
2231 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
2232 // and if so we want to cache that not the original.
2233 if (exe_scope)
2234 exe_scope->CalculateExecutionContext(exe_ctx);
2235 if (exe_ctx.target != NULL)
2236 {
2237 m_target_sp = exe_ctx.target->GetSP();
2238
2239 if (exe_ctx.process == NULL)
2240 m_process_sp = exe_ctx.target->GetProcessSP();
2241 else
2242 m_process_sp = exe_ctx.process->GetSP();
2243
2244 if (m_process_sp != NULL)
2245 {
2246 m_stop_id = m_process_sp->GetStopID();
2247 Thread *thread = NULL;
2248
2249 if (exe_ctx.thread == NULL)
2250 {
2251 if (use_selected)
2252 {
2253 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
2254 if (thread)
2255 computed_exe_scope = thread;
2256 }
2257 }
2258 else
2259 thread = exe_ctx.thread;
2260
2261 if (thread != NULL)
2262 {
2263 m_thread_id = thread->GetIndexID();
2264 if (exe_ctx.frame == NULL)
2265 {
2266 if (use_selected)
2267 {
2268 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
2269 if (frame)
2270 {
2271 m_stack_id = frame->GetStackID();
2272 computed_exe_scope = frame;
2273 }
2274 }
2275 }
2276 else
2277 m_stack_id = exe_ctx.frame->GetStackID();
2278 }
2279 }
2280 }
2281 m_exe_scope = computed_exe_scope;
2282}
2283
2284ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
2285 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00002286 m_needs_update(true),
2287 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00002288 m_target_sp (rhs.m_target_sp),
2289 m_process_sp (rhs.m_process_sp),
2290 m_thread_id (rhs.m_thread_id),
2291 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00002292 m_stop_id (0)
2293{
2294}
2295
2296ValueObject::EvaluationPoint::~EvaluationPoint ()
2297{
2298}
2299
2300ExecutionContextScope *
2301ValueObject::EvaluationPoint::GetExecutionContextScope ()
2302{
2303 // We have to update before giving out the scope, or we could be handing out stale pointers.
2304 SyncWithProcessState();
2305
2306 return m_exe_scope;
2307}
2308
2309// This function checks the EvaluationPoint against the current process state. If the current
2310// state matches the evaluation point, or the evaluation point is already invalid, then we return
2311// false, meaning "no change". If the current state is different, we update our state, and return
2312// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
2313// future calls to NeedsUpdate will return true.
2314
2315bool
2316ValueObject::EvaluationPoint::SyncWithProcessState()
2317{
2318 // If we're already invalid, we don't need to do anything, and nothing has changed:
2319 if (m_stop_id == LLDB_INVALID_UID)
2320 {
2321 // Can't update with an invalid state.
2322 m_needs_update = false;
2323 return false;
2324 }
2325
2326 // If we don't have a process nothing can change.
2327 if (!m_process_sp)
2328 return false;
2329
2330 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00002331 uint32_t cur_stop_id = m_process_sp->GetStopID();
2332 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00002333 return false;
2334
Jim Ingham78a685a2011-04-16 00:01:13 +00002335 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
2336 // In either case, we aren't going to be able to sync with the process state.
2337 if (cur_stop_id == 0)
2338 return false;
2339
2340 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00002341 m_needs_update = true;
2342 m_exe_scope = m_process_sp.get();
2343
2344 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
2345 // doesn't, mark ourselves as invalid.
2346
2347 if (m_thread_id != LLDB_INVALID_THREAD_ID)
2348 {
2349 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
2350 if (our_thread == NULL)
Greg Clayton262f80d2011-07-06 16:49:27 +00002351 {
Jim Ingham89b61092011-07-06 17:42:14 +00002352 SetInvalid();
Greg Clayton262f80d2011-07-06 16:49:27 +00002353 }
Jim Ingham6035b672011-03-31 00:19:25 +00002354 else
2355 {
2356 m_exe_scope = our_thread;
2357
2358 if (m_stack_id.IsValid())
2359 {
2360 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
2361 if (our_frame == NULL)
2362 SetInvalid();
2363 else
2364 m_exe_scope = our_frame;
2365 }
2366 }
2367 }
2368 return true;
2369}
2370
Jim Ingham61be0902011-05-02 18:13:59 +00002371void
2372ValueObject::EvaluationPoint::SetUpdated ()
2373{
2374 m_first_update = false;
2375 m_needs_update = false;
2376 if (m_process_sp)
2377 m_stop_id = m_process_sp->GetStopID();
2378}
2379
2380
Jim Ingham6035b672011-03-31 00:19:25 +00002381bool
2382ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
2383{
2384 if (!IsValid())
2385 return false;
2386
2387 bool needs_update = false;
2388 m_exe_scope = NULL;
2389
2390 // The target has to be non-null, and the
2391 Target *target = exe_scope->CalculateTarget();
2392 if (target != NULL)
2393 {
2394 Target *old_target = m_target_sp.get();
2395 assert (target == old_target);
2396 Process *process = exe_scope->CalculateProcess();
2397 if (process != NULL)
2398 {
2399 // FOR NOW - assume you can't update variable objects across process boundaries.
2400 Process *old_process = m_process_sp.get();
2401 assert (process == old_process);
2402
2403 lldb::user_id_t stop_id = process->GetStopID();
2404 if (stop_id != m_stop_id)
2405 {
2406 needs_update = true;
2407 m_stop_id = stop_id;
2408 }
2409 // See if we're switching the thread or stack context. If no thread is given, this is
2410 // being evaluated in a global context.
2411 Thread *thread = exe_scope->CalculateThread();
2412 if (thread != NULL)
2413 {
2414 lldb::user_id_t new_thread_index = thread->GetIndexID();
2415 if (new_thread_index != m_thread_id)
2416 {
2417 needs_update = true;
2418 m_thread_id = new_thread_index;
2419 m_stack_id.Clear();
2420 }
2421
2422 StackFrame *new_frame = exe_scope->CalculateStackFrame();
2423 if (new_frame != NULL)
2424 {
2425 if (new_frame->GetStackID() != m_stack_id)
2426 {
2427 needs_update = true;
2428 m_stack_id = new_frame->GetStackID();
2429 }
2430 }
2431 else
2432 {
2433 m_stack_id.Clear();
2434 needs_update = true;
2435 }
2436 }
2437 else
2438 {
2439 // If this had been given a thread, and now there is none, we should update.
2440 // Otherwise we don't have to do anything.
2441 if (m_thread_id != LLDB_INVALID_UID)
2442 {
2443 m_thread_id = LLDB_INVALID_UID;
2444 m_stack_id.Clear();
2445 needs_update = true;
2446 }
2447 }
2448 }
2449 else
2450 {
2451 // If there is no process, then we don't need to update anything.
2452 // But if we're switching from having a process to not, we should try to update.
2453 if (m_process_sp.get() != NULL)
2454 {
2455 needs_update = true;
2456 m_process_sp.reset();
2457 m_thread_id = LLDB_INVALID_UID;
2458 m_stack_id.Clear();
2459 }
2460 }
2461 }
2462 else
2463 {
2464 // If there's no target, nothing can change so we don't need to update anything.
2465 // But if we're switching from having a target to not, we should try to update.
2466 if (m_target_sp.get() != NULL)
2467 {
2468 needs_update = true;
2469 m_target_sp.reset();
2470 m_process_sp.reset();
2471 m_thread_id = LLDB_INVALID_UID;
2472 m_stack_id.Clear();
2473 }
2474 }
2475 if (!m_needs_update)
2476 m_needs_update = needs_update;
2477
2478 return needs_update;
2479}