blob: 2b1beed38724591c2813520365f91387029263c8 [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"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/ValueObjectChild.h"
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000024#include "lldb/Core/ValueObjectConstResult.h"
Jim Ingham78a685a2011-04-16 00:01:13 +000025#include "lldb/Core/ValueObjectDynamicValue.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026#include "lldb/Core/ValueObjectList.h"
27
Greg Clayton7fb56d02011-02-01 01:31:41 +000028#include "lldb/Host/Endian.h"
29
Greg Claytone1a916a2010-07-21 22:12:05 +000030#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Symbol/ClangASTContext.h"
32#include "lldb/Symbol/Type.h"
33
Jim Ingham53c47f12010-09-10 23:12:17 +000034#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000035#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/Process.h"
37#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000038#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040
41using namespace lldb;
42using namespace lldb_private;
43
44static lldb::user_id_t g_value_obj_uid = 0;
45
46//----------------------------------------------------------------------
47// ValueObject constructor
48//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000049ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000051 m_parent (&parent),
Stephen Wilson71c21d12011-04-11 19:41:40 +000052 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000053 m_name (),
54 m_data (),
55 m_value (),
56 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000057 m_value_str (),
58 m_old_value_str (),
59 m_location_str (),
60 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000061 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000062 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000063 m_children (),
64 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000065 m_dynamic_value (NULL),
66 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000067 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000068 m_value_is_valid (false),
69 m_value_did_change (false),
70 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000071 m_old_value_valid (false),
Greg Claytone221f822011-01-21 01:59:00 +000072 m_pointers_point_to_load_addrs (false),
Stephen Wilson71c21d12011-04-11 19:41:40 +000073 m_is_deref_of_parent (false)
Jim Ingham6035b672011-03-31 00:19:25 +000074{
Jim Ingham58b59f92011-04-22 23:53:53 +000075 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +000076}
77
78//----------------------------------------------------------------------
79// ValueObject constructor
80//----------------------------------------------------------------------
81ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
82 UserID (++g_value_obj_uid), // Unique identifier for every value object
83 m_parent (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000084 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +000085 m_name (),
86 m_data (),
87 m_value (),
88 m_error (),
89 m_value_str (),
90 m_old_value_str (),
91 m_location_str (),
92 m_summary_str (),
93 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000094 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +000095 m_children (),
96 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000097 m_dynamic_value (NULL),
98 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +000099 m_format (eFormatDefault),
100 m_value_is_valid (false),
101 m_value_did_change (false),
102 m_children_count_valid (false),
103 m_old_value_valid (false),
104 m_pointers_point_to_load_addrs (false),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000105 m_is_deref_of_parent (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106{
Jim Ingham58b59f92011-04-22 23:53:53 +0000107 m_manager = new ValueObjectManager();
108 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109}
110
111//----------------------------------------------------------------------
112// Destructor
113//----------------------------------------------------------------------
114ValueObject::~ValueObject ()
115{
116}
117
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118bool
Jim Ingham6035b672011-03-31 00:19:25 +0000119ValueObject::UpdateValueIfNeeded ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000120{
Greg Claytonb71f3842010-10-05 03:13:51 +0000121 // If this is a constant value, then our success is predicated on whether
122 // we have an error or not
123 if (GetIsConstant())
124 return m_error.Success();
125
Jim Ingham6035b672011-03-31 00:19:25 +0000126 bool first_update = m_update_point.IsFirstEvaluation();
127
128 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129 {
Jim Ingham6035b672011-03-31 00:19:25 +0000130 m_update_point.SetUpdated();
131
132 // Save the old value using swap to avoid a string copy which
133 // also will clear our m_value_str
134 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000135 {
Jim Ingham6035b672011-03-31 00:19:25 +0000136 m_old_value_valid = false;
137 }
138 else
139 {
140 m_old_value_valid = true;
141 m_old_value_str.swap (m_value_str);
142 m_value_str.clear();
143 }
144 m_location_str.clear();
145 m_summary_str.clear();
146 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147
Jim Ingham6035b672011-03-31 00:19:25 +0000148 const bool value_was_valid = GetValueIsValid();
149 SetValueDidChange (false);
Greg Clayton73b953b2010-08-28 00:08:07 +0000150
Jim Ingham6035b672011-03-31 00:19:25 +0000151 m_error.Clear();
Greg Clayton73b953b2010-08-28 00:08:07 +0000152
Jim Ingham6035b672011-03-31 00:19:25 +0000153 // Call the pure virtual function to update the value
154 bool success = UpdateValue ();
155
156 SetValueIsValid (success);
157
158 if (first_update)
159 SetValueDidChange (false);
160 else if (!m_value_did_change && success == false)
161 {
162 // The value wasn't gotten successfully, so we mark this
163 // as changed if the value used to be valid and now isn't
164 SetValueDidChange (value_was_valid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000165 }
166 }
167 return m_error.Success();
168}
169
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170DataExtractor &
171ValueObject::GetDataExtractor ()
172{
Jim Ingham78a685a2011-04-16 00:01:13 +0000173 UpdateValueIfNeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174 return m_data;
175}
176
177const Error &
178ValueObject::GetError() const
179{
180 return m_error;
181}
182
183const ConstString &
184ValueObject::GetName() const
185{
186 return m_name;
187}
188
189const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000190ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000191{
Jim Ingham6035b672011-03-31 00:19:25 +0000192 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193 {
194 if (m_location_str.empty())
195 {
196 StreamString sstr;
197
198 switch (m_value.GetValueType())
199 {
200 default:
201 break;
202
203 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000204 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000205 {
206 RegisterInfo *reg_info = m_value.GetRegisterInfo();
207 if (reg_info)
208 {
209 if (reg_info->name)
210 m_location_str = reg_info->name;
211 else if (reg_info->alt_name)
212 m_location_str = reg_info->alt_name;
213 break;
214 }
215 }
216 m_location_str = "scalar";
217 break;
218
219 case Value::eValueTypeLoadAddress:
220 case Value::eValueTypeFileAddress:
221 case Value::eValueTypeHostAddress:
222 {
223 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
224 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
225 m_location_str.swap(sstr.GetString());
226 }
227 break;
228 }
229 }
230 }
231 return m_location_str.c_str();
232}
233
234Value &
235ValueObject::GetValue()
236{
237 return m_value;
238}
239
240const Value &
241ValueObject::GetValue() const
242{
243 return m_value;
244}
245
246bool
Jim Ingham6035b672011-03-31 00:19:25 +0000247ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000248{
249 ExecutionContext exe_ctx;
Jim Ingham6035b672011-03-31 00:19:25 +0000250 ExecutionContextScope *exe_scope = GetExecutionContextScope();
251 if (exe_scope)
252 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8f343b02010-11-04 01:54:29 +0000253 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
254 return scalar.IsValid();
255}
256
257bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000258ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259{
Greg Clayton288bdf92010-09-02 02:59:18 +0000260 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000261}
262
263
264void
265ValueObject::SetValueIsValid (bool b)
266{
Greg Clayton288bdf92010-09-02 02:59:18 +0000267 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000268}
269
270bool
Jim Ingham6035b672011-03-31 00:19:25 +0000271ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000272{
Jim Ingham6035b672011-03-31 00:19:25 +0000273 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000274 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275}
276
277void
278ValueObject::SetValueDidChange (bool value_changed)
279{
Greg Clayton288bdf92010-09-02 02:59:18 +0000280 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281}
282
283ValueObjectSP
284ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
285{
286 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000287 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000289 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000291 // Check if we have already made the child value object?
Jim Ingham58b59f92011-04-22 23:53:53 +0000292 if (can_create && m_children[idx] == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +0000293 {
294 // No we haven't created the child at this index, so lets have our
295 // subclass do it and cache the result for quick future access.
296 m_children[idx] = CreateChildAtIndex (idx, false, 0);
297 }
Jim Ingham58b59f92011-04-22 23:53:53 +0000298
299 if (m_children[idx] != NULL)
300 return m_children[idx]->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +0000301 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 }
303 return child_sp;
304}
305
306uint32_t
307ValueObject::GetIndexOfChildWithName (const ConstString &name)
308{
309 bool omit_empty_base_classes = true;
310 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000311 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000312 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000313 omit_empty_base_classes);
314}
315
316ValueObjectSP
317ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
318{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000319 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 // classes (which really aren't part of the expression path), so we
321 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000322 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000323
324 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000325 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000326 std::vector<uint32_t> child_indexes;
327 clang::ASTContext *clang_ast = GetClangAST();
328 void *clang_type = GetClangType();
329 bool omit_empty_base_classes = true;
330 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
331 clang_type,
332 name.GetCString(),
333 omit_empty_base_classes,
334 child_indexes);
335 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000337 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
338 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000339
Jim Ingham78a685a2011-04-16 00:01:13 +0000340 child_sp = GetChildAtIndex(*pos, can_create);
341 for (++pos; pos != end; ++pos)
342 {
343 if (child_sp)
344 {
345 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
346 child_sp = new_child_sp;
347 }
348 else
349 {
350 child_sp.reset();
351 }
352
353 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000354 }
355 }
356 return child_sp;
357}
358
359
360uint32_t
361ValueObject::GetNumChildren ()
362{
Greg Clayton288bdf92010-09-02 02:59:18 +0000363 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000364 {
365 SetNumChildren (CalculateNumChildren());
366 }
367 return m_children.size();
368}
369void
370ValueObject::SetNumChildren (uint32_t num_children)
371{
Greg Clayton288bdf92010-09-02 02:59:18 +0000372 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 m_children.resize(num_children);
374}
375
376void
377ValueObject::SetName (const char *name)
378{
379 m_name.SetCString(name);
380}
381
382void
383ValueObject::SetName (const ConstString &name)
384{
385 m_name = name;
386}
387
Jim Ingham58b59f92011-04-22 23:53:53 +0000388ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000389ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
390{
Jim Ingham58b59f92011-04-22 23:53:53 +0000391 ValueObject *valobj;
Jim Ingham78a685a2011-04-16 00:01:13 +0000392
393 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000395 bool omit_empty_base_classes = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000396
Jim Ingham78a685a2011-04-16 00:01:13 +0000397 std::string child_name_str;
398 uint32_t child_byte_size = 0;
399 int32_t child_byte_offset = 0;
400 uint32_t child_bitfield_bit_size = 0;
401 uint32_t child_bitfield_bit_offset = 0;
402 bool child_is_base_class = false;
403 bool child_is_deref_of_parent = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404
Jim Ingham78a685a2011-04-16 00:01:13 +0000405 const bool transparent_pointers = synthetic_array_member == false;
406 clang::ASTContext *clang_ast = GetClangAST();
407 clang_type_t clang_type = GetClangType();
408 clang_type_t child_clang_type;
409 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
410 GetName().GetCString(),
411 clang_type,
412 idx,
413 transparent_pointers,
414 omit_empty_base_classes,
415 child_name_str,
416 child_byte_size,
417 child_byte_offset,
418 child_bitfield_bit_size,
419 child_bitfield_bit_offset,
420 child_is_base_class,
421 child_is_deref_of_parent);
422 if (child_clang_type && child_byte_size)
423 {
424 if (synthetic_index)
425 child_byte_offset += child_byte_size * synthetic_index;
426
427 ConstString child_name;
428 if (!child_name_str.empty())
429 child_name.SetCString (child_name_str.c_str());
430
Jim Ingham58b59f92011-04-22 23:53:53 +0000431 valobj = new ValueObjectChild (*this,
432 clang_ast,
433 child_clang_type,
434 child_name,
435 child_byte_size,
436 child_byte_offset,
437 child_bitfield_bit_size,
438 child_bitfield_bit_offset,
439 child_is_base_class,
440 child_is_deref_of_parent);
Jim Ingham78a685a2011-04-16 00:01:13 +0000441 if (m_pointers_point_to_load_addrs)
Jim Ingham58b59f92011-04-22 23:53:53 +0000442 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Jim Ingham78a685a2011-04-16 00:01:13 +0000443 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000444 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000445
Jim Ingham58b59f92011-04-22 23:53:53 +0000446 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447}
448
449const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000450ValueObject::GetSummaryAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451{
Jim Ingham6035b672011-03-31 00:19:25 +0000452 if (UpdateValueIfNeeded ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 {
454 if (m_summary_str.empty())
455 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000456 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457
458 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000459 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 {
Greg Clayton737b9322010-09-13 03:32:57 +0000461 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000462 clang_type_t elem_or_pointee_clang_type;
463 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000464 GetClangAST(),
465 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000466
Jim Ingham6035b672011-03-31 00:19:25 +0000467 ExecutionContextScope *exe_scope = GetExecutionContextScope();
468 if (exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 {
Jim Ingham6035b672011-03-31 00:19:25 +0000470 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
471 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 {
Jim Ingham6035b672011-03-31 00:19:25 +0000473 Process *process = exe_scope->CalculateProcess();
474 if (process != NULL)
475 {
476 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
477 AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000478
Jim Ingham6035b672011-03-31 00:19:25 +0000479 size_t cstr_len = 0;
480 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 {
Jim Ingham6035b672011-03-31 00:19:25 +0000482 // We have an array
483 cstr_len = ClangASTContext::GetArraySize (clang_type);
484 cstr_address = GetAddressOf (cstr_address_type, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000485 }
Greg Clayton737b9322010-09-13 03:32:57 +0000486 else
487 {
Jim Ingham6035b672011-03-31 00:19:25 +0000488 // We have a pointer
489 cstr_address = GetPointerValue (cstr_address_type, true);
Greg Clayton737b9322010-09-13 03:32:57 +0000490 }
Jim Ingham6035b672011-03-31 00:19:25 +0000491 if (cstr_address != LLDB_INVALID_ADDRESS)
Greg Clayton737b9322010-09-13 03:32:57 +0000492 {
Jim Ingham6035b672011-03-31 00:19:25 +0000493 DataExtractor data;
494 size_t bytes_read = 0;
495 std::vector<char> data_buffer;
496 Error error;
497 if (cstr_len > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000498 {
Jim Ingham6035b672011-03-31 00:19:25 +0000499 data_buffer.resize(cstr_len);
500 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
501 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
502 if (bytes_read > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000503 {
Jim Ingham6035b672011-03-31 00:19:25 +0000504 sstr << '"';
505 data.Dump (&sstr,
506 0, // Start offset in "data"
507 eFormatChar, // Print as characters
508 1, // Size of item (1 byte for a char!)
509 bytes_read, // How many bytes to print?
510 UINT32_MAX, // num per line
511 LLDB_INVALID_ADDRESS,// base address
512 0, // bitfield bit size
513 0); // bitfield bit offset
514 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000515 }
516 }
Jim Ingham6035b672011-03-31 00:19:25 +0000517 else
518 {
519 const size_t k_max_buf_size = 256;
520 data_buffer.resize (k_max_buf_size + 1);
521 // NULL terminate in case we don't get the entire C string
522 data_buffer.back() = '\0';
Greg Clayton737b9322010-09-13 03:32:57 +0000523
Jim Ingham6035b672011-03-31 00:19:25 +0000524 sstr << '"';
525
526 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
527 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
528 {
529 size_t len = strlen(&data_buffer.front());
530 if (len == 0)
531 break;
532 if (len > bytes_read)
533 len = bytes_read;
534
535 data.Dump (&sstr,
536 0, // Start offset in "data"
537 eFormatChar, // Print as characters
538 1, // Size of item (1 byte for a char!)
539 len, // How many bytes to print?
540 UINT32_MAX, // num per line
541 LLDB_INVALID_ADDRESS,// base address
542 0, // bitfield bit size
543 0); // bitfield bit offset
544
545 if (len < k_max_buf_size)
546 break;
547 cstr_address += k_max_buf_size;
548 }
549 sstr << '"';
550 }
551 }
Greg Clayton737b9322010-09-13 03:32:57 +0000552 }
Jim Ingham6035b672011-03-31 00:19:25 +0000553
554 if (sstr.GetSize() > 0)
555 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
Greg Clayton737b9322010-09-13 03:32:57 +0000556 }
Jim Ingham6035b672011-03-31 00:19:25 +0000557 else if (ClangASTContext::IsFunctionPointerType (clang_type))
Greg Clayton737b9322010-09-13 03:32:57 +0000558 {
Jim Ingham6035b672011-03-31 00:19:25 +0000559 AddressType func_ptr_address_type = eAddressTypeInvalid;
560 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
561
562 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
563 {
564 switch (func_ptr_address_type)
565 {
566 case eAddressTypeInvalid:
567 case eAddressTypeFile:
568 break;
569
570 case eAddressTypeLoad:
571 {
572 Address so_addr;
573 Target *target = exe_scope->CalculateTarget();
574 if (target && target->GetSectionLoadList().IsEmpty() == false)
575 {
576 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
577 {
578 so_addr.Dump (&sstr,
579 exe_scope,
580 Address::DumpStyleResolvedDescription,
581 Address::DumpStyleSectionNameOffset);
582 }
583 }
584 }
585 break;
586
587 case eAddressTypeHost:
588 break;
589 }
590 }
591 if (sstr.GetSize() > 0)
592 {
593 m_summary_str.assign (1, '(');
594 m_summary_str.append (sstr.GetData(), sstr.GetSize());
595 m_summary_str.append (1, ')');
596 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597 }
598 }
599 }
600 }
601 }
602 if (m_summary_str.empty())
603 return NULL;
604 return m_summary_str.c_str();
605}
606
Jim Ingham53c47f12010-09-10 23:12:17 +0000607const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000608ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +0000609{
610 if (!m_object_desc_str.empty())
611 return m_object_desc_str.c_str();
612
Jim Ingham6035b672011-03-31 00:19:25 +0000613 if (!UpdateValueIfNeeded ())
Jim Ingham53c47f12010-09-10 23:12:17 +0000614 return NULL;
Jim Ingham6035b672011-03-31 00:19:25 +0000615
616 ExecutionContextScope *exe_scope = GetExecutionContextScope();
617 if (exe_scope == NULL)
618 return NULL;
619
Jim Ingham53c47f12010-09-10 23:12:17 +0000620 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000621 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000622 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000623
Jim Ingham53c47f12010-09-10 23:12:17 +0000624 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000625
626 lldb::LanguageType language = GetObjectRuntimeLanguage();
627 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
628
Jim Inghama2cf2632010-12-23 02:29:54 +0000629 if (runtime == NULL)
630 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000631 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +0000632 clang_type_t opaque_qual_type = GetClangType();
633 if (opaque_qual_type != NULL)
634 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000635 bool is_signed;
636 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
637 || ClangASTContext::IsPointerType (opaque_qual_type))
638 {
Jim Inghama2cf2632010-12-23 02:29:54 +0000639 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +0000640 }
Jim Inghama2cf2632010-12-23 02:29:54 +0000641 }
642 }
643
Jim Ingham8d543de2011-03-31 23:01:21 +0000644 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +0000645 {
646 m_object_desc_str.append (s.GetData());
647 }
Sean Callanan672ad942010-10-23 00:18:49 +0000648
649 if (m_object_desc_str.empty())
650 return NULL;
651 else
652 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000653}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654
655const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000656ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657{
658 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000659 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000660 {
Jim Ingham6035b672011-03-31 00:19:25 +0000661 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662 {
663 if (m_value_str.empty())
664 {
665 const Value::ContextType context_type = m_value.GetContextType();
666
667 switch (context_type)
668 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000669 case Value::eContextTypeClangType:
670 case Value::eContextTypeLLDBType:
671 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000673 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 if (clang_type)
675 {
676 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000677 if (m_format == eFormatDefault)
678 m_format = ClangASTType::GetFormat(clang_type);
679
680 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
681 clang_type, // The clang type to display
682 &sstr,
683 m_format, // Format to display this type with
684 m_data, // Data to extract from
685 0, // Byte offset into "m_data"
686 GetByteSize(), // Byte size of item in "m_data"
687 GetBitfieldBitSize(), // Bitfield bit size
688 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000689 m_value_str.swap(sstr.GetString());
690 else
691 m_value_str.clear();
692 }
693 }
694 break;
695
Greg Clayton526e5af2010-11-13 03:52:47 +0000696 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 {
698 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
699 if (reg_info)
700 {
701 StreamString reg_sstr;
702 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
703 m_value_str.swap(reg_sstr.GetString());
704 }
705 }
706 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000707
708 default:
709 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 }
711 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000712
713 if (!m_value_did_change && m_old_value_valid)
714 {
715 // The value was gotten successfully, so we consider the
716 // value as changed if the value string differs
717 SetValueDidChange (m_old_value_str != m_value_str);
718 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 }
720 }
721 if (m_value_str.empty())
722 return NULL;
723 return m_value_str.c_str();
724}
725
Greg Clayton737b9322010-09-13 03:32:57 +0000726addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000727ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000728{
Jim Ingham78a685a2011-04-16 00:01:13 +0000729 if (!UpdateValueIfNeeded())
730 return LLDB_INVALID_ADDRESS;
731
Greg Clayton73b472d2010-10-27 03:32:59 +0000732 switch (m_value.GetValueType())
733 {
734 case Value::eValueTypeScalar:
735 if (scalar_is_load_address)
736 {
737 address_type = eAddressTypeLoad;
738 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
739 }
740 break;
741
742 case Value::eValueTypeLoadAddress:
743 case Value::eValueTypeFileAddress:
744 case Value::eValueTypeHostAddress:
745 {
746 address_type = m_value.GetValueAddressType ();
747 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
748 }
749 break;
750 }
751 address_type = eAddressTypeInvalid;
752 return LLDB_INVALID_ADDRESS;
753}
754
755addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000756ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000757{
758 lldb::addr_t address = LLDB_INVALID_ADDRESS;
759 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +0000760
761 if (!UpdateValueIfNeeded())
762 return address;
763
Greg Clayton73b472d2010-10-27 03:32:59 +0000764 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000765 {
766 case Value::eValueTypeScalar:
767 if (scalar_is_load_address)
768 {
769 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
770 address_type = eAddressTypeLoad;
771 }
772 break;
773
774 case Value::eValueTypeLoadAddress:
775 case Value::eValueTypeFileAddress:
776 case Value::eValueTypeHostAddress:
777 {
778 uint32_t data_offset = 0;
779 address = m_data.GetPointer(&data_offset);
780 address_type = m_value.GetValueAddressType();
781 if (address_type == eAddressTypeInvalid)
782 address_type = eAddressTypeLoad;
783 }
784 break;
785 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000786
787 if (m_pointers_point_to_load_addrs)
788 address_type = eAddressTypeLoad;
789
Greg Clayton737b9322010-09-13 03:32:57 +0000790 return address;
791}
792
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793bool
Jim Ingham6035b672011-03-31 00:19:25 +0000794ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795{
796 // Make sure our value is up to date first so that our location and location
797 // type is valid.
Jim Ingham6035b672011-03-31 00:19:25 +0000798 if (!UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 return false;
800
801 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000802 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803
804 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000805 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 switch (encoding)
807 {
808 case eEncodingInvalid:
809 return false;
810
811 case eEncodingUint:
812 if (byte_size > sizeof(unsigned long long))
813 {
814 return false;
815 }
816 else
817 {
818 unsigned long long ull_val = strtoull(value_str, &end, 0);
819 if (end && *end != '\0')
820 return false;
821 m_value = ull_val;
822 // Limit the bytes in our m_data appropriately.
823 m_value.GetScalar().GetData (m_data, byte_size);
824 }
825 break;
826
827 case eEncodingSint:
828 if (byte_size > sizeof(long long))
829 {
830 return false;
831 }
832 else
833 {
834 long long sll_val = strtoll(value_str, &end, 0);
835 if (end && *end != '\0')
836 return false;
837 m_value = sll_val;
838 // Limit the bytes in our m_data appropriately.
839 m_value.GetScalar().GetData (m_data, byte_size);
840 }
841 break;
842
843 case eEncodingIEEE754:
844 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000845 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000846 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 if (dst != NULL)
848 {
849 // We are decoding a float into host byte order below, so make
850 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000851 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
853 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000854 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855 value_str,
856 dst,
857 byte_size);
858
859 if (converted_byte_size == byte_size)
860 {
861 }
862 }
863 }
864 break;
865
866 case eEncodingVector:
867 return false;
868
869 default:
870 return false;
871 }
872
873 // If we have made it here the value is in m_data and we should write it
874 // out to the target
875 return Write ();
876}
877
878bool
879ValueObject::Write ()
880{
881 // Clear the update ID so the next time we try and read the value
882 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +0000883 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884
885 // TODO: when Value has a method to write a value back, call it from here.
886 return false;
887
888}
889
Jim Ingham5a369122010-09-28 01:25:32 +0000890lldb::LanguageType
891ValueObject::GetObjectRuntimeLanguage ()
892{
Greg Clayton73b472d2010-10-27 03:32:59 +0000893 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000894 if (opaque_qual_type == NULL)
895 return lldb::eLanguageTypeC;
896
897 // If the type is a reference, then resolve it to what it refers to first:
898 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
899 if (qual_type->isAnyPointerType())
900 {
901 if (qual_type->isObjCObjectPointerType())
902 return lldb::eLanguageTypeObjC;
903
904 clang::QualType pointee_type (qual_type->getPointeeType());
905 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
906 return lldb::eLanguageTypeC_plus_plus;
907 if (pointee_type->isObjCObjectOrInterfaceType())
908 return lldb::eLanguageTypeObjC;
909 if (pointee_type->isObjCClassType())
910 return lldb::eLanguageTypeObjC;
911 }
912 else
913 {
914 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
915 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000916 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000917 return lldb::eLanguageTypeC_plus_plus;
918 }
919
920 return lldb::eLanguageTypeC;
921}
922
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000923void
Jim Ingham58b59f92011-04-22 23:53:53 +0000924ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925{
Jim Ingham58b59f92011-04-22 23:53:53 +0000926 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927}
928
929ValueObjectSP
930ValueObject::GetSyntheticChild (const ConstString &key) const
931{
932 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +0000933 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +0000935 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 return synthetic_child_sp;
937}
938
939bool
940ValueObject::IsPointerType ()
941{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000942 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943}
944
Jim Inghamb7603bb2011-03-18 00:05:18 +0000945bool
946ValueObject::IsIntegerType (bool &is_signed)
947{
948 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
949}
Greg Clayton73b472d2010-10-27 03:32:59 +0000950
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000951bool
952ValueObject::IsPointerOrReferenceType ()
953{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000954 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000955}
956
957ValueObjectSP
958ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
959{
960 ValueObjectSP synthetic_child_sp;
961 if (IsPointerType ())
962 {
963 char index_str[64];
964 snprintf(index_str, sizeof(index_str), "[%i]", index);
965 ConstString index_const_str(index_str);
966 // Check if we have already created a synthetic array member in this
967 // valid object. If we have we will re-use it.
968 synthetic_child_sp = GetSyntheticChild (index_const_str);
969 if (!synthetic_child_sp)
970 {
Jim Ingham58b59f92011-04-22 23:53:53 +0000971 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972 // We haven't made a synthetic array member for INDEX yet, so
973 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +0000974 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000975
976 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +0000977 if (synthetic_child)
978 {
979 AddSyntheticChild(index_const_str, synthetic_child);
980 synthetic_child_sp = synthetic_child->GetSP();
981 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000982 }
983 }
984 return synthetic_child_sp;
985}
Jim Ingham22777012010-09-23 02:01:19 +0000986
Jim Ingham78a685a2011-04-16 00:01:13 +0000987void
988ValueObject::CalculateDynamicValue ()
Jim Ingham22777012010-09-23 02:01:19 +0000989{
Jim Ingham58b59f92011-04-22 23:53:53 +0000990 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +0000991 {
992 Process *process = m_update_point.GetProcess();
993 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +0000994
Jim Ingham78a685a2011-04-16 00:01:13 +0000995
996 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
997 // hard code this everywhere.
998 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
999 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1000 {
1001 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1002 if (runtime)
1003 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1004 }
1005 else
1006 {
1007 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1008 if (cpp_runtime)
1009 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1010
1011 if (!worth_having_dynamic_value)
1012 {
1013 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1014 if (objc_runtime)
1015 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1016 }
1017 }
1018
1019 if (worth_having_dynamic_value)
Jim Ingham58b59f92011-04-22 23:53:53 +00001020 m_dynamic_value = new ValueObjectDynamicValue (*this);
1021
1022// if (worth_having_dynamic_value)
1023// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1024
Jim Ingham78a685a2011-04-16 00:01:13 +00001025 }
1026}
1027
Jim Ingham58b59f92011-04-22 23:53:53 +00001028ValueObjectSP
Jim Ingham78a685a2011-04-16 00:01:13 +00001029ValueObject::GetDynamicValue (bool can_create)
1030{
Jim Ingham58b59f92011-04-22 23:53:53 +00001031 if (!IsDynamic() && m_dynamic_value == NULL && can_create)
Jim Ingham78a685a2011-04-16 00:01:13 +00001032 {
1033 CalculateDynamicValue();
1034 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001035 if (m_dynamic_value)
1036 return m_dynamic_value->GetSP();
1037 else
1038 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001039}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001040
Greg Claytone221f822011-01-21 01:59:00 +00001041bool
1042ValueObject::GetBaseClassPath (Stream &s)
1043{
1044 if (IsBaseClass())
1045 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001046 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001047 clang_type_t clang_type = GetClangType();
1048 std::string cxx_class_name;
1049 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1050 if (this_had_base_class)
1051 {
1052 if (parent_had_base_class)
1053 s.PutCString("::");
1054 s.PutCString(cxx_class_name.c_str());
1055 }
1056 return parent_had_base_class || this_had_base_class;
1057 }
1058 return false;
1059}
1060
1061
1062ValueObject *
1063ValueObject::GetNonBaseClassParent()
1064{
Jim Ingham78a685a2011-04-16 00:01:13 +00001065 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001066 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001067 if (GetParent()->IsBaseClass())
1068 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001069 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001070 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001071 }
1072 return NULL;
1073}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001074
1075void
Greg Clayton6beaaa62011-01-17 03:46:26 +00001076ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001077{
Greg Claytone221f822011-01-21 01:59:00 +00001078 const bool is_deref_of_parent = IsDereferenceOfParent ();
1079
1080 if (is_deref_of_parent)
1081 s.PutCString("*(");
1082
Jim Ingham78a685a2011-04-16 00:01:13 +00001083 if (GetParent())
1084 GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +00001085
1086 if (!IsBaseClass())
1087 {
1088 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001089 {
Greg Claytone221f822011-01-21 01:59:00 +00001090 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1091 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001092 {
Greg Claytone221f822011-01-21 01:59:00 +00001093 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1094 if (non_base_class_parent_clang_type)
1095 {
1096 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1097
1098 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1099 {
1100 s.PutCString("->");
1101 }
1102 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1103 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1104 {
1105 s.PutChar('.');
1106 }
1107 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001108 }
Greg Claytone221f822011-01-21 01:59:00 +00001109
1110 const char *name = GetName().GetCString();
1111 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001112 {
Greg Claytone221f822011-01-21 01:59:00 +00001113 if (qualify_cxx_base_classes)
1114 {
1115 if (GetBaseClassPath (s))
1116 s.PutCString("::");
1117 }
1118 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001119 }
1120 }
1121 }
1122
Greg Claytone221f822011-01-21 01:59:00 +00001123 if (is_deref_of_parent)
1124 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001125}
1126
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001127void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001128ValueObject::DumpValueObject
1129(
1130 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00001131 ValueObject *valobj,
1132 const char *root_valobj_name,
1133 uint32_t ptr_depth,
1134 uint32_t curr_depth,
1135 uint32_t max_depth,
1136 bool show_types,
1137 bool show_location,
1138 bool use_objc,
Jim Ingham78a685a2011-04-16 00:01:13 +00001139 bool use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001140 bool scope_already_checked,
1141 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001142)
1143{
Jim Ingham6035b672011-03-31 00:19:25 +00001144 if (valobj && valobj->UpdateValueIfNeeded ())
Greg Clayton1d3afba2010-10-05 00:00:42 +00001145 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001146 if (use_dynamic)
1147 {
1148 ValueObject *dynamic_value = valobj->GetDynamicValue(true).get();
1149 if (dynamic_value)
1150 valobj = dynamic_value;
1151 }
1152
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001153 clang_type_t clang_type = valobj->GetClangType();
1154
Greg Clayton73b472d2010-10-27 03:32:59 +00001155 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001156 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001157 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1158 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001159
1160 const bool print_valobj = flat_output == false || has_value;
1161
1162 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001163 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001164 if (show_location)
1165 {
Jim Ingham6035b672011-03-31 00:19:25 +00001166 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001167 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001168
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001169 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001170
Greg Clayton7c8a9662010-11-02 01:50:16 +00001171 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001172 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001173 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001174
Greg Clayton1d3afba2010-10-05 00:00:42 +00001175
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001176 if (flat_output)
1177 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001178 // If we are showing types, also qualify the C++ base classes
1179 const bool qualify_cxx_base_classes = show_types;
1180 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001181 s.PutCString(" =");
1182 }
1183 else
1184 {
1185 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1186 s.Printf ("%s =", name_cstr);
1187 }
1188
Jim Ingham6035b672011-03-31 00:19:25 +00001189 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001190 {
1191 err_cstr = "error: out of scope";
1192 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001193 }
1194
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001195 const char *val_cstr = NULL;
1196
1197 if (err_cstr == NULL)
1198 {
Jim Ingham6035b672011-03-31 00:19:25 +00001199 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001200 err_cstr = valobj->GetError().AsCString();
1201 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001202
1203 if (err_cstr)
1204 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001205 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001206 }
1207 else
1208 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001209 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001210 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001211 {
Jim Ingham6035b672011-03-31 00:19:25 +00001212 const char *sum_cstr = valobj->GetSummaryAsCString();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001213
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001214 if (val_cstr)
1215 s.Printf(" %s", val_cstr);
1216
1217 if (sum_cstr)
1218 s.Printf(" %s", sum_cstr);
1219
1220 if (use_objc)
1221 {
Jim Ingham6035b672011-03-31 00:19:25 +00001222 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001223 if (object_desc)
1224 s.Printf(" %s\n", object_desc);
1225 else
Sean Callanan672ad942010-10-23 00:18:49 +00001226 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001227 return;
1228 }
1229 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001230
1231 if (curr_depth < max_depth)
1232 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001233 // We will show children for all concrete types. We won't show
1234 // pointer contents unless a pointer depth has been specified.
1235 // We won't reference contents unless the reference is the
1236 // root object (depth of zero).
1237 bool print_children = true;
1238
1239 // Use a new temporary pointer depth in case we override the
1240 // current pointer depth below...
1241 uint32_t curr_ptr_depth = ptr_depth;
1242
1243 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1244 if (is_ptr || is_ref)
1245 {
1246 // We have a pointer or reference whose value is an address.
1247 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001248 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001249 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1250 print_children = false;
1251
1252 else if (is_ref && curr_depth == 0)
1253 {
1254 // If this is the root object (depth is zero) that we are showing
1255 // and it is a reference, and no pointer depth has been supplied
1256 // print out what it references. Don't do this at deeper depths
1257 // otherwise we can end up with infinite recursion...
1258 curr_ptr_depth = 1;
1259 }
1260
1261 if (curr_ptr_depth == 0)
1262 print_children = false;
1263 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001264
Greg Clayton73b472d2010-10-27 03:32:59 +00001265 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001266 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001267 const uint32_t num_children = valobj->GetNumChildren();
1268 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001269 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001270 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001271 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001272 if (print_valobj)
1273 s.EOL();
1274 }
1275 else
1276 {
1277 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001278 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001279 s.IndentMore();
1280 }
1281
1282 for (uint32_t idx=0; idx<num_children; ++idx)
1283 {
1284 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1285 if (child_sp.get())
1286 {
1287 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001288 child_sp.get(),
1289 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001290 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001291 curr_depth + 1,
1292 max_depth,
1293 show_types,
1294 show_location,
1295 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00001296 use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001297 true,
1298 flat_output);
1299 }
1300 }
1301
1302 if (!flat_output)
1303 {
1304 s.IndentLess();
1305 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001306 }
1307 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001308 else if (has_children)
1309 {
1310 // Aggregate, no children...
1311 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001312 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001313 }
1314 else
1315 {
1316 if (print_valobj)
1317 s.EOL();
1318 }
1319
Greg Clayton1d3afba2010-10-05 00:00:42 +00001320 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001321 else
1322 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001323 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001324 }
1325 }
1326 else
1327 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001328 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001329 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001330 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001331 }
1332 }
1333 }
1334 }
1335}
1336
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001337
1338ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00001339ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001340{
1341 ValueObjectSP valobj_sp;
1342
Jim Ingham6035b672011-03-31 00:19:25 +00001343 if (UpdateValueIfNeeded() && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001344 {
Jim Ingham6035b672011-03-31 00:19:25 +00001345 ExecutionContextScope *exe_scope = GetExecutionContextScope();
1346 if (exe_scope)
1347 {
1348 ExecutionContext exe_ctx;
1349 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001350
Jim Ingham6035b672011-03-31 00:19:25 +00001351 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001352
Jim Ingham6035b672011-03-31 00:19:25 +00001353 DataExtractor data;
1354 data.SetByteOrder (m_data.GetByteOrder());
1355 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001356
Jim Ingham6035b672011-03-31 00:19:25 +00001357 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001358
Jim Ingham58b59f92011-04-22 23:53:53 +00001359 valobj_sp = ValueObjectConstResult::Create (exe_scope,
1360 ast,
1361 GetClangType(),
1362 name,
1363 data);
Jim Ingham6035b672011-03-31 00:19:25 +00001364 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001365 }
Jim Ingham6035b672011-03-31 00:19:25 +00001366
1367 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001368 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001369 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001370 }
1371 return valobj_sp;
1372}
1373
1374lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001375ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001376{
Jim Ingham58b59f92011-04-22 23:53:53 +00001377 if (m_deref_valobj)
1378 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001379
Greg Clayton54979cd2010-12-15 05:08:08 +00001380 const bool is_pointer_type = IsPointerType();
1381 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001382 {
1383 bool omit_empty_base_classes = true;
1384
1385 std::string child_name_str;
1386 uint32_t child_byte_size = 0;
1387 int32_t child_byte_offset = 0;
1388 uint32_t child_bitfield_bit_size = 0;
1389 uint32_t child_bitfield_bit_offset = 0;
1390 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001391 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001392 const bool transparent_pointers = false;
1393 clang::ASTContext *clang_ast = GetClangAST();
1394 clang_type_t clang_type = GetClangType();
1395 clang_type_t child_clang_type;
1396 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1397 GetName().GetCString(),
1398 clang_type,
1399 0,
1400 transparent_pointers,
1401 omit_empty_base_classes,
1402 child_name_str,
1403 child_byte_size,
1404 child_byte_offset,
1405 child_bitfield_bit_size,
1406 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001407 child_is_base_class,
1408 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001409 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001410 {
1411 ConstString child_name;
1412 if (!child_name_str.empty())
1413 child_name.SetCString (child_name_str.c_str());
1414
Jim Ingham58b59f92011-04-22 23:53:53 +00001415 m_deref_valobj = new ValueObjectChild (*this,
1416 clang_ast,
1417 child_clang_type,
1418 child_name,
1419 child_byte_size,
1420 child_byte_offset,
1421 child_bitfield_bit_size,
1422 child_bitfield_bit_offset,
1423 child_is_base_class,
1424 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001425 }
1426 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001427
Jim Ingham58b59f92011-04-22 23:53:53 +00001428 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00001429 {
1430 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00001431 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00001432 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001433 else
1434 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001435 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001436 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001437
1438 if (is_pointer_type)
1439 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1440 else
1441 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00001442 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001443 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001444}
1445
Jim Ingham78a685a2011-04-16 00:01:13 +00001446lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00001447ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001448{
Jim Ingham78a685a2011-04-16 00:01:13 +00001449 if (m_addr_of_valobj_sp)
1450 return m_addr_of_valobj_sp;
1451
Greg Claytone0d378b2011-03-24 21:19:54 +00001452 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001453 const bool scalar_is_load_address = false;
1454 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001455 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001456 if (addr != LLDB_INVALID_ADDRESS)
1457 {
1458 switch (address_type)
1459 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001460 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001461 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001462 {
1463 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001464 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001465 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1466 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001467 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001468
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001469 case eAddressTypeFile:
1470 case eAddressTypeLoad:
1471 case eAddressTypeHost:
1472 {
1473 clang::ASTContext *ast = GetClangAST();
1474 clang_type_t clang_type = GetClangType();
1475 if (ast && clang_type)
1476 {
1477 std::string name (1, '&');
1478 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00001479 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1480 ast,
1481 ClangASTContext::CreatePointerType (ast, clang_type),
1482 ConstString (name.c_str()),
1483 addr,
1484 eAddressTypeInvalid,
1485 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001486 }
1487 }
1488 break;
1489 }
1490 }
Jim Ingham78a685a2011-04-16 00:01:13 +00001491 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001492}
1493
Jim Ingham6035b672011-03-31 00:19:25 +00001494ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00001495 m_thread_id (LLDB_INVALID_UID),
1496 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00001497{
1498}
1499
1500ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00001501 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001502 m_first_update (true),
1503 m_thread_id (LLDB_INVALID_UID),
1504 m_stop_id (0)
1505
Jim Ingham6035b672011-03-31 00:19:25 +00001506{
1507 ExecutionContext exe_ctx;
1508 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
1509 // and if so we want to cache that not the original.
1510 if (exe_scope)
1511 exe_scope->CalculateExecutionContext(exe_ctx);
1512 if (exe_ctx.target != NULL)
1513 {
1514 m_target_sp = exe_ctx.target->GetSP();
1515
1516 if (exe_ctx.process == NULL)
1517 m_process_sp = exe_ctx.target->GetProcessSP();
1518 else
1519 m_process_sp = exe_ctx.process->GetSP();
1520
1521 if (m_process_sp != NULL)
1522 {
1523 m_stop_id = m_process_sp->GetStopID();
1524 Thread *thread = NULL;
1525
1526 if (exe_ctx.thread == NULL)
1527 {
1528 if (use_selected)
1529 {
1530 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1531 if (thread)
1532 computed_exe_scope = thread;
1533 }
1534 }
1535 else
1536 thread = exe_ctx.thread;
1537
1538 if (thread != NULL)
1539 {
1540 m_thread_id = thread->GetIndexID();
1541 if (exe_ctx.frame == NULL)
1542 {
1543 if (use_selected)
1544 {
1545 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1546 if (frame)
1547 {
1548 m_stack_id = frame->GetStackID();
1549 computed_exe_scope = frame;
1550 }
1551 }
1552 }
1553 else
1554 m_stack_id = exe_ctx.frame->GetStackID();
1555 }
1556 }
1557 }
1558 m_exe_scope = computed_exe_scope;
1559}
1560
1561ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1562 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001563 m_needs_update(true),
1564 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00001565 m_target_sp (rhs.m_target_sp),
1566 m_process_sp (rhs.m_process_sp),
1567 m_thread_id (rhs.m_thread_id),
1568 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00001569 m_stop_id (0)
1570{
1571}
1572
1573ValueObject::EvaluationPoint::~EvaluationPoint ()
1574{
1575}
1576
1577ExecutionContextScope *
1578ValueObject::EvaluationPoint::GetExecutionContextScope ()
1579{
1580 // We have to update before giving out the scope, or we could be handing out stale pointers.
1581 SyncWithProcessState();
1582
1583 return m_exe_scope;
1584}
1585
1586// This function checks the EvaluationPoint against the current process state. If the current
1587// state matches the evaluation point, or the evaluation point is already invalid, then we return
1588// false, meaning "no change". If the current state is different, we update our state, and return
1589// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
1590// future calls to NeedsUpdate will return true.
1591
1592bool
1593ValueObject::EvaluationPoint::SyncWithProcessState()
1594{
1595 // If we're already invalid, we don't need to do anything, and nothing has changed:
1596 if (m_stop_id == LLDB_INVALID_UID)
1597 {
1598 // Can't update with an invalid state.
1599 m_needs_update = false;
1600 return false;
1601 }
1602
1603 // If we don't have a process nothing can change.
1604 if (!m_process_sp)
1605 return false;
1606
1607 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00001608 uint32_t cur_stop_id = m_process_sp->GetStopID();
1609 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00001610 return false;
1611
Jim Ingham78a685a2011-04-16 00:01:13 +00001612 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1613 // In either case, we aren't going to be able to sync with the process state.
1614 if (cur_stop_id == 0)
1615 return false;
1616
1617 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00001618 m_needs_update = true;
1619 m_exe_scope = m_process_sp.get();
1620
1621 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
1622 // doesn't, mark ourselves as invalid.
1623
1624 if (m_thread_id != LLDB_INVALID_THREAD_ID)
1625 {
1626 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1627 if (our_thread == NULL)
1628 SetInvalid();
1629 else
1630 {
1631 m_exe_scope = our_thread;
1632
1633 if (m_stack_id.IsValid())
1634 {
1635 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1636 if (our_frame == NULL)
1637 SetInvalid();
1638 else
1639 m_exe_scope = our_frame;
1640 }
1641 }
1642 }
1643 return true;
1644}
1645
1646bool
1647ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1648{
1649 if (!IsValid())
1650 return false;
1651
1652 bool needs_update = false;
1653 m_exe_scope = NULL;
1654
1655 // The target has to be non-null, and the
1656 Target *target = exe_scope->CalculateTarget();
1657 if (target != NULL)
1658 {
1659 Target *old_target = m_target_sp.get();
1660 assert (target == old_target);
1661 Process *process = exe_scope->CalculateProcess();
1662 if (process != NULL)
1663 {
1664 // FOR NOW - assume you can't update variable objects across process boundaries.
1665 Process *old_process = m_process_sp.get();
1666 assert (process == old_process);
1667
1668 lldb::user_id_t stop_id = process->GetStopID();
1669 if (stop_id != m_stop_id)
1670 {
1671 needs_update = true;
1672 m_stop_id = stop_id;
1673 }
1674 // See if we're switching the thread or stack context. If no thread is given, this is
1675 // being evaluated in a global context.
1676 Thread *thread = exe_scope->CalculateThread();
1677 if (thread != NULL)
1678 {
1679 lldb::user_id_t new_thread_index = thread->GetIndexID();
1680 if (new_thread_index != m_thread_id)
1681 {
1682 needs_update = true;
1683 m_thread_id = new_thread_index;
1684 m_stack_id.Clear();
1685 }
1686
1687 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1688 if (new_frame != NULL)
1689 {
1690 if (new_frame->GetStackID() != m_stack_id)
1691 {
1692 needs_update = true;
1693 m_stack_id = new_frame->GetStackID();
1694 }
1695 }
1696 else
1697 {
1698 m_stack_id.Clear();
1699 needs_update = true;
1700 }
1701 }
1702 else
1703 {
1704 // If this had been given a thread, and now there is none, we should update.
1705 // Otherwise we don't have to do anything.
1706 if (m_thread_id != LLDB_INVALID_UID)
1707 {
1708 m_thread_id = LLDB_INVALID_UID;
1709 m_stack_id.Clear();
1710 needs_update = true;
1711 }
1712 }
1713 }
1714 else
1715 {
1716 // If there is no process, then we don't need to update anything.
1717 // But if we're switching from having a process to not, we should try to update.
1718 if (m_process_sp.get() != NULL)
1719 {
1720 needs_update = true;
1721 m_process_sp.reset();
1722 m_thread_id = LLDB_INVALID_UID;
1723 m_stack_id.Clear();
1724 }
1725 }
1726 }
1727 else
1728 {
1729 // If there's no target, nothing can change so we don't need to update anything.
1730 // But if we're switching from having a target to not, we should try to update.
1731 if (m_target_sp.get() != NULL)
1732 {
1733 needs_update = true;
1734 m_target_sp.reset();
1735 m_process_sp.reset();
1736 m_thread_id = LLDB_INVALID_UID;
1737 m_stack_id.Clear();
1738 }
1739 }
1740 if (!m_needs_update)
1741 m_needs_update = needs_update;
1742
1743 return needs_update;
1744}