blob: ee44876a0eca43f892641fc728378d678171fab1 [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"
Greg Claytonb2dcc362011-05-05 23:32:56 +000027#include "lldb/Core/ValueObjectMemory.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028
Greg Clayton7fb56d02011-02-01 01:31:41 +000029#include "lldb/Host/Endian.h"
30
Greg Claytone1a916a2010-07-21 22:12:05 +000031#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Symbol/ClangASTContext.h"
33#include "lldb/Symbol/Type.h"
34
Jim Ingham53c47f12010-09-10 23:12:17 +000035#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000036#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037#include "lldb/Target/Process.h"
38#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000039#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000040#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041
42using namespace lldb;
43using namespace lldb_private;
44
45static lldb::user_id_t g_value_obj_uid = 0;
46
47//----------------------------------------------------------------------
48// ValueObject constructor
49//----------------------------------------------------------------------
Jim Ingham6035b672011-03-31 00:19:25 +000050ValueObject::ValueObject (ValueObject &parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 UserID (++g_value_obj_uid), // Unique identifier for every value object
Jim Ingham6035b672011-03-31 00:19:25 +000052 m_parent (&parent),
Stephen Wilson71c21d12011-04-11 19:41:40 +000053 m_update_point (parent.GetUpdatePoint ()),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000054 m_name (),
55 m_data (),
56 m_value (),
57 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000058 m_value_str (),
59 m_old_value_str (),
60 m_location_str (),
61 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000062 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000063 m_manager(parent.GetManager()),
Greg Clayton288bdf92010-09-02 02:59:18 +000064 m_children (),
65 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000066 m_dynamic_value (NULL),
67 m_deref_valobj(NULL),
Greg Clayton32c40852010-10-06 03:09:11 +000068 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000069 m_value_is_valid (false),
70 m_value_did_change (false),
71 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000072 m_old_value_valid (false),
Greg Claytone221f822011-01-21 01:59:00 +000073 m_pointers_point_to_load_addrs (false),
Stephen Wilson71c21d12011-04-11 19:41:40 +000074 m_is_deref_of_parent (false)
Jim Ingham6035b672011-03-31 00:19:25 +000075{
Jim Ingham58b59f92011-04-22 23:53:53 +000076 m_manager->ManageObject(this);
Jim Ingham6035b672011-03-31 00:19:25 +000077}
78
79//----------------------------------------------------------------------
80// ValueObject constructor
81//----------------------------------------------------------------------
82ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
83 UserID (++g_value_obj_uid), // Unique identifier for every value object
84 m_parent (NULL),
Stephen Wilson71c21d12011-04-11 19:41:40 +000085 m_update_point (exe_scope),
Jim Ingham6035b672011-03-31 00:19:25 +000086 m_name (),
87 m_data (),
88 m_value (),
89 m_error (),
90 m_value_str (),
91 m_old_value_str (),
92 m_location_str (),
93 m_summary_str (),
94 m_object_desc_str (),
Jim Ingham58b59f92011-04-22 23:53:53 +000095 m_manager(),
Jim Ingham6035b672011-03-31 00:19:25 +000096 m_children (),
97 m_synthetic_children (),
Jim Ingham58b59f92011-04-22 23:53:53 +000098 m_dynamic_value (NULL),
99 m_deref_valobj(NULL),
Jim Ingham6035b672011-03-31 00:19:25 +0000100 m_format (eFormatDefault),
101 m_value_is_valid (false),
102 m_value_did_change (false),
103 m_children_count_valid (false),
104 m_old_value_valid (false),
105 m_pointers_point_to_load_addrs (false),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000106 m_is_deref_of_parent (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107{
Jim Ingham58b59f92011-04-22 23:53:53 +0000108 m_manager = new ValueObjectManager();
109 m_manager->ManageObject (this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
112//----------------------------------------------------------------------
113// Destructor
114//----------------------------------------------------------------------
115ValueObject::~ValueObject ()
116{
117}
118
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119bool
Jim Ingham6035b672011-03-31 00:19:25 +0000120ValueObject::UpdateValueIfNeeded ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121{
Greg Claytonb71f3842010-10-05 03:13:51 +0000122 // If this is a constant value, then our success is predicated on whether
123 // we have an error or not
124 if (GetIsConstant())
125 return m_error.Success();
126
Jim Ingham6035b672011-03-31 00:19:25 +0000127 bool first_update = m_update_point.IsFirstEvaluation();
128
129 if (m_update_point.NeedsUpdating())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000130 {
Jim Ingham6035b672011-03-31 00:19:25 +0000131 m_update_point.SetUpdated();
132
133 // Save the old value using swap to avoid a string copy which
134 // also will clear our m_value_str
135 if (m_value_str.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136 {
Jim Ingham6035b672011-03-31 00:19:25 +0000137 m_old_value_valid = false;
138 }
139 else
140 {
141 m_old_value_valid = true;
142 m_old_value_str.swap (m_value_str);
143 m_value_str.clear();
144 }
145 m_location_str.clear();
146 m_summary_str.clear();
147 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000148
Jim Ingham6035b672011-03-31 00:19:25 +0000149 const bool value_was_valid = GetValueIsValid();
150 SetValueDidChange (false);
Greg Clayton73b953b2010-08-28 00:08:07 +0000151
Jim Ingham6035b672011-03-31 00:19:25 +0000152 m_error.Clear();
Greg Clayton73b953b2010-08-28 00:08:07 +0000153
Jim Ingham6035b672011-03-31 00:19:25 +0000154 // Call the pure virtual function to update the value
155 bool success = UpdateValue ();
156
157 SetValueIsValid (success);
158
159 if (first_update)
160 SetValueDidChange (false);
161 else if (!m_value_did_change && success == false)
162 {
163 // The value wasn't gotten successfully, so we mark this
164 // as changed if the value used to be valid and now isn't
165 SetValueDidChange (value_was_valid);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 }
167 }
168 return m_error.Success();
169}
170
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171DataExtractor &
172ValueObject::GetDataExtractor ()
173{
Jim Ingham78a685a2011-04-16 00:01:13 +0000174 UpdateValueIfNeeded();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175 return m_data;
176}
177
178const Error &
179ValueObject::GetError() const
180{
181 return m_error;
182}
183
184const ConstString &
185ValueObject::GetName() const
186{
187 return m_name;
188}
189
190const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000191ValueObject::GetLocationAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192{
Jim Ingham6035b672011-03-31 00:19:25 +0000193 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000194 {
195 if (m_location_str.empty())
196 {
197 StreamString sstr;
198
199 switch (m_value.GetValueType())
200 {
201 default:
202 break;
203
204 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000205 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206 {
207 RegisterInfo *reg_info = m_value.GetRegisterInfo();
208 if (reg_info)
209 {
210 if (reg_info->name)
211 m_location_str = reg_info->name;
212 else if (reg_info->alt_name)
213 m_location_str = reg_info->alt_name;
214 break;
215 }
216 }
217 m_location_str = "scalar";
218 break;
219
220 case Value::eValueTypeLoadAddress:
221 case Value::eValueTypeFileAddress:
222 case Value::eValueTypeHostAddress:
223 {
224 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
225 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
226 m_location_str.swap(sstr.GetString());
227 }
228 break;
229 }
230 }
231 }
232 return m_location_str.c_str();
233}
234
235Value &
236ValueObject::GetValue()
237{
238 return m_value;
239}
240
241const Value &
242ValueObject::GetValue() const
243{
244 return m_value;
245}
246
247bool
Jim Ingham6035b672011-03-31 00:19:25 +0000248ValueObject::ResolveValue (Scalar &scalar)
Greg Clayton8f343b02010-11-04 01:54:29 +0000249{
250 ExecutionContext exe_ctx;
Jim Ingham6035b672011-03-31 00:19:25 +0000251 ExecutionContextScope *exe_scope = GetExecutionContextScope();
252 if (exe_scope)
253 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8f343b02010-11-04 01:54:29 +0000254 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
255 return scalar.IsValid();
256}
257
258bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000259ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260{
Greg Clayton288bdf92010-09-02 02:59:18 +0000261 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
264
265void
266ValueObject::SetValueIsValid (bool b)
267{
Greg Clayton288bdf92010-09-02 02:59:18 +0000268 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269}
270
271bool
Jim Ingham6035b672011-03-31 00:19:25 +0000272ValueObject::GetValueDidChange ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000273{
Jim Ingham6035b672011-03-31 00:19:25 +0000274 GetValueAsCString ();
Greg Clayton288bdf92010-09-02 02:59:18 +0000275 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000276}
277
278void
279ValueObject::SetValueDidChange (bool value_changed)
280{
Greg Clayton288bdf92010-09-02 02:59:18 +0000281 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282}
283
284ValueObjectSP
285ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
286{
287 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000288 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000290 if (idx < GetNumChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000291 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000292 // Check if we have already made the child value object?
Jim Ingham58b59f92011-04-22 23:53:53 +0000293 if (can_create && m_children[idx] == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +0000294 {
295 // No we haven't created the child at this index, so lets have our
296 // subclass do it and cache the result for quick future access.
297 m_children[idx] = CreateChildAtIndex (idx, false, 0);
298 }
Jim Ingham58b59f92011-04-22 23:53:53 +0000299
300 if (m_children[idx] != NULL)
301 return m_children[idx]->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +0000302 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 }
304 return child_sp;
305}
306
307uint32_t
308ValueObject::GetIndexOfChildWithName (const ConstString &name)
309{
310 bool omit_empty_base_classes = true;
311 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000312 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000313 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000314 omit_empty_base_classes);
315}
316
317ValueObjectSP
318ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
319{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000320 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000321 // classes (which really aren't part of the expression path), so we
322 // need a vector of indexes that can get us down to the correct child
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 ValueObjectSP child_sp;
Jim Ingham78a685a2011-04-16 00:01:13 +0000324
325 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000326 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000327 std::vector<uint32_t> child_indexes;
328 clang::ASTContext *clang_ast = GetClangAST();
329 void *clang_type = GetClangType();
330 bool omit_empty_base_classes = true;
331 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
332 clang_type,
333 name.GetCString(),
334 omit_empty_base_classes,
335 child_indexes);
336 if (num_child_indexes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000338 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
339 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000340
Jim Ingham78a685a2011-04-16 00:01:13 +0000341 child_sp = GetChildAtIndex(*pos, can_create);
342 for (++pos; pos != end; ++pos)
343 {
344 if (child_sp)
345 {
346 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
347 child_sp = new_child_sp;
348 }
349 else
350 {
351 child_sp.reset();
352 }
353
354 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000355 }
356 }
357 return child_sp;
358}
359
360
361uint32_t
362ValueObject::GetNumChildren ()
363{
Greg Clayton288bdf92010-09-02 02:59:18 +0000364 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365 {
366 SetNumChildren (CalculateNumChildren());
367 }
368 return m_children.size();
369}
370void
371ValueObject::SetNumChildren (uint32_t num_children)
372{
Greg Clayton288bdf92010-09-02 02:59:18 +0000373 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 m_children.resize(num_children);
375}
376
377void
378ValueObject::SetName (const char *name)
379{
380 m_name.SetCString(name);
381}
382
383void
384ValueObject::SetName (const ConstString &name)
385{
386 m_name = name;
387}
388
Jim Ingham58b59f92011-04-22 23:53:53 +0000389ValueObject *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
391{
Jim Ingham2eec4872011-05-07 00:10:58 +0000392 ValueObject *valobj = NULL;
Jim Ingham78a685a2011-04-16 00:01:13 +0000393
394 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000395 {
Jim Ingham78a685a2011-04-16 00:01:13 +0000396 bool omit_empty_base_classes = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000397
Jim Ingham78a685a2011-04-16 00:01:13 +0000398 std::string child_name_str;
399 uint32_t child_byte_size = 0;
400 int32_t child_byte_offset = 0;
401 uint32_t child_bitfield_bit_size = 0;
402 uint32_t child_bitfield_bit_offset = 0;
403 bool child_is_base_class = false;
404 bool child_is_deref_of_parent = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
Jim Ingham78a685a2011-04-16 00:01:13 +0000406 const bool transparent_pointers = synthetic_array_member == false;
407 clang::ASTContext *clang_ast = GetClangAST();
408 clang_type_t clang_type = GetClangType();
409 clang_type_t child_clang_type;
410 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
411 GetName().GetCString(),
412 clang_type,
413 idx,
414 transparent_pointers,
415 omit_empty_base_classes,
416 child_name_str,
417 child_byte_size,
418 child_byte_offset,
419 child_bitfield_bit_size,
420 child_bitfield_bit_offset,
421 child_is_base_class,
422 child_is_deref_of_parent);
423 if (child_clang_type && child_byte_size)
424 {
425 if (synthetic_index)
426 child_byte_offset += child_byte_size * synthetic_index;
427
428 ConstString child_name;
429 if (!child_name_str.empty())
430 child_name.SetCString (child_name_str.c_str());
431
Jim Ingham58b59f92011-04-22 23:53:53 +0000432 valobj = new ValueObjectChild (*this,
433 clang_ast,
434 child_clang_type,
435 child_name,
436 child_byte_size,
437 child_byte_offset,
438 child_bitfield_bit_size,
439 child_bitfield_bit_offset,
440 child_is_base_class,
441 child_is_deref_of_parent);
Jim Ingham78a685a2011-04-16 00:01:13 +0000442 if (m_pointers_point_to_load_addrs)
Jim Ingham58b59f92011-04-22 23:53:53 +0000443 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Jim Ingham78a685a2011-04-16 00:01:13 +0000444 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 }
Jim Ingham78a685a2011-04-16 00:01:13 +0000446
Jim Ingham58b59f92011-04-22 23:53:53 +0000447 return valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000448}
449
450const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000451ValueObject::GetSummaryAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000452{
Jim Ingham6035b672011-03-31 00:19:25 +0000453 if (UpdateValueIfNeeded ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 {
455 if (m_summary_str.empty())
456 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000457 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458
459 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000460 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 {
Greg Clayton737b9322010-09-13 03:32:57 +0000462 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000463 clang_type_t elem_or_pointee_clang_type;
464 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000465 GetClangAST(),
466 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000467
Jim Ingham6035b672011-03-31 00:19:25 +0000468 ExecutionContextScope *exe_scope = GetExecutionContextScope();
469 if (exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000470 {
Jim Ingham6035b672011-03-31 00:19:25 +0000471 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
472 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000473 {
Jim Ingham6035b672011-03-31 00:19:25 +0000474 Process *process = exe_scope->CalculateProcess();
475 if (process != NULL)
476 {
477 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
478 AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479
Jim Ingham6035b672011-03-31 00:19:25 +0000480 size_t cstr_len = 0;
481 if (type_flags.Test (ClangASTContext::eTypeIsArray))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000482 {
Jim Ingham6035b672011-03-31 00:19:25 +0000483 // We have an array
484 cstr_len = ClangASTContext::GetArraySize (clang_type);
485 cstr_address = GetAddressOf (cstr_address_type, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000486 }
Greg Clayton737b9322010-09-13 03:32:57 +0000487 else
488 {
Jim Ingham6035b672011-03-31 00:19:25 +0000489 // We have a pointer
490 cstr_address = GetPointerValue (cstr_address_type, true);
Greg Clayton737b9322010-09-13 03:32:57 +0000491 }
Jim Ingham6035b672011-03-31 00:19:25 +0000492 if (cstr_address != LLDB_INVALID_ADDRESS)
Greg Clayton737b9322010-09-13 03:32:57 +0000493 {
Jim Ingham6035b672011-03-31 00:19:25 +0000494 DataExtractor data;
495 size_t bytes_read = 0;
496 std::vector<char> data_buffer;
497 Error error;
498 if (cstr_len > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000499 {
Jim Ingham6035b672011-03-31 00:19:25 +0000500 data_buffer.resize(cstr_len);
501 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
502 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
503 if (bytes_read > 0)
Greg Clayton737b9322010-09-13 03:32:57 +0000504 {
Jim Ingham6035b672011-03-31 00:19:25 +0000505 sstr << '"';
506 data.Dump (&sstr,
507 0, // Start offset in "data"
508 eFormatChar, // Print as characters
509 1, // Size of item (1 byte for a char!)
510 bytes_read, // How many bytes to print?
511 UINT32_MAX, // num per line
512 LLDB_INVALID_ADDRESS,// base address
513 0, // bitfield bit size
514 0); // bitfield bit offset
515 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000516 }
517 }
Jim Ingham6035b672011-03-31 00:19:25 +0000518 else
519 {
520 const size_t k_max_buf_size = 256;
521 data_buffer.resize (k_max_buf_size + 1);
522 // NULL terminate in case we don't get the entire C string
523 data_buffer.back() = '\0';
Greg Clayton737b9322010-09-13 03:32:57 +0000524
Jim Ingham6035b672011-03-31 00:19:25 +0000525 sstr << '"';
526
527 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
528 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
529 {
530 size_t len = strlen(&data_buffer.front());
531 if (len == 0)
532 break;
533 if (len > bytes_read)
534 len = bytes_read;
535
536 data.Dump (&sstr,
537 0, // Start offset in "data"
538 eFormatChar, // Print as characters
539 1, // Size of item (1 byte for a char!)
540 len, // How many bytes to print?
541 UINT32_MAX, // num per line
542 LLDB_INVALID_ADDRESS,// base address
543 0, // bitfield bit size
544 0); // bitfield bit offset
545
546 if (len < k_max_buf_size)
547 break;
548 cstr_address += k_max_buf_size;
549 }
550 sstr << '"';
551 }
552 }
Greg Clayton737b9322010-09-13 03:32:57 +0000553 }
Jim Ingham6035b672011-03-31 00:19:25 +0000554
555 if (sstr.GetSize() > 0)
556 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
Greg Clayton737b9322010-09-13 03:32:57 +0000557 }
Jim Ingham6035b672011-03-31 00:19:25 +0000558 else if (ClangASTContext::IsFunctionPointerType (clang_type))
Greg Clayton737b9322010-09-13 03:32:57 +0000559 {
Jim Ingham6035b672011-03-31 00:19:25 +0000560 AddressType func_ptr_address_type = eAddressTypeInvalid;
561 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
562
563 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
564 {
565 switch (func_ptr_address_type)
566 {
567 case eAddressTypeInvalid:
568 case eAddressTypeFile:
569 break;
570
571 case eAddressTypeLoad:
572 {
573 Address so_addr;
574 Target *target = exe_scope->CalculateTarget();
575 if (target && target->GetSectionLoadList().IsEmpty() == false)
576 {
577 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
578 {
579 so_addr.Dump (&sstr,
580 exe_scope,
581 Address::DumpStyleResolvedDescription,
582 Address::DumpStyleSectionNameOffset);
583 }
584 }
585 }
586 break;
587
588 case eAddressTypeHost:
589 break;
590 }
591 }
592 if (sstr.GetSize() > 0)
593 {
594 m_summary_str.assign (1, '(');
595 m_summary_str.append (sstr.GetData(), sstr.GetSize());
596 m_summary_str.append (1, ')');
597 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598 }
599 }
600 }
601 }
602 }
603 if (m_summary_str.empty())
604 return NULL;
605 return m_summary_str.c_str();
606}
607
Jim Ingham53c47f12010-09-10 23:12:17 +0000608const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000609ValueObject::GetObjectDescription ()
Jim Ingham53c47f12010-09-10 23:12:17 +0000610{
611 if (!m_object_desc_str.empty())
612 return m_object_desc_str.c_str();
613
Jim Ingham6035b672011-03-31 00:19:25 +0000614 if (!UpdateValueIfNeeded ())
Jim Ingham53c47f12010-09-10 23:12:17 +0000615 return NULL;
Jim Ingham6035b672011-03-31 00:19:25 +0000616
617 ExecutionContextScope *exe_scope = GetExecutionContextScope();
618 if (exe_scope == NULL)
619 return NULL;
620
Jim Ingham53c47f12010-09-10 23:12:17 +0000621 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000622 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000623 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000624
Jim Ingham53c47f12010-09-10 23:12:17 +0000625 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000626
627 lldb::LanguageType language = GetObjectRuntimeLanguage();
628 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
629
Jim Inghama2cf2632010-12-23 02:29:54 +0000630 if (runtime == NULL)
631 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000632 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +0000633 clang_type_t opaque_qual_type = GetClangType();
634 if (opaque_qual_type != NULL)
635 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000636 bool is_signed;
637 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
638 || ClangASTContext::IsPointerType (opaque_qual_type))
639 {
Jim Inghama2cf2632010-12-23 02:29:54 +0000640 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +0000641 }
Jim Inghama2cf2632010-12-23 02:29:54 +0000642 }
643 }
644
Jim Ingham8d543de2011-03-31 23:01:21 +0000645 if (runtime && runtime->GetObjectDescription(s, *this))
Jim Ingham53c47f12010-09-10 23:12:17 +0000646 {
647 m_object_desc_str.append (s.GetData());
648 }
Sean Callanan672ad942010-10-23 00:18:49 +0000649
650 if (m_object_desc_str.empty())
651 return NULL;
652 else
653 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000654}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655
656const char *
Jim Ingham6035b672011-03-31 00:19:25 +0000657ValueObject::GetValueAsCString ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000658{
659 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000660 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000661 {
Jim Ingham6035b672011-03-31 00:19:25 +0000662 if (UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000663 {
664 if (m_value_str.empty())
665 {
666 const Value::ContextType context_type = m_value.GetContextType();
667
668 switch (context_type)
669 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000670 case Value::eContextTypeClangType:
671 case Value::eContextTypeLLDBType:
672 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000674 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 if (clang_type)
676 {
677 StreamString sstr;
Greg Clayton68ebae62011-04-28 20:55:26 +0000678 Format format = GetFormat();
679 if (format == eFormatDefault)
680 format = ClangASTType::GetFormat(clang_type);
Greg Clayton32c40852010-10-06 03:09:11 +0000681
682 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
683 clang_type, // The clang type to display
684 &sstr,
Greg Clayton68ebae62011-04-28 20:55:26 +0000685 format, // Format to display this type with
Greg Clayton32c40852010-10-06 03:09:11 +0000686 m_data, // Data to extract from
687 0, // Byte offset into "m_data"
688 GetByteSize(), // Byte size of item in "m_data"
689 GetBitfieldBitSize(), // Bitfield bit size
690 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000691 m_value_str.swap(sstr.GetString());
692 else
Greg Clayton007d5be2011-05-30 00:49:24 +0000693 {
694 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
695 m_data.GetByteSize(),
696 GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 m_value_str.clear();
Greg Clayton007d5be2011-05-30 00:49:24 +0000698 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 }
700 }
701 break;
702
Greg Clayton526e5af2010-11-13 03:52:47 +0000703 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704 {
705 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
706 if (reg_info)
707 {
708 StreamString reg_sstr;
709 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
710 m_value_str.swap(reg_sstr.GetString());
711 }
712 }
713 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000714
715 default:
716 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
718 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000719
720 if (!m_value_did_change && m_old_value_valid)
721 {
722 // The value was gotten successfully, so we consider the
723 // value as changed if the value string differs
724 SetValueDidChange (m_old_value_str != m_value_str);
725 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 }
727 }
728 if (m_value_str.empty())
729 return NULL;
730 return m_value_str.c_str();
731}
732
Greg Clayton737b9322010-09-13 03:32:57 +0000733addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000734ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000735{
Jim Ingham78a685a2011-04-16 00:01:13 +0000736 if (!UpdateValueIfNeeded())
737 return LLDB_INVALID_ADDRESS;
738
Greg Clayton73b472d2010-10-27 03:32:59 +0000739 switch (m_value.GetValueType())
740 {
741 case Value::eValueTypeScalar:
742 if (scalar_is_load_address)
743 {
744 address_type = eAddressTypeLoad;
745 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
746 }
747 break;
748
749 case Value::eValueTypeLoadAddress:
750 case Value::eValueTypeFileAddress:
751 case Value::eValueTypeHostAddress:
752 {
753 address_type = m_value.GetValueAddressType ();
754 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
755 }
756 break;
757 }
758 address_type = eAddressTypeInvalid;
759 return LLDB_INVALID_ADDRESS;
760}
761
762addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000763ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000764{
765 lldb::addr_t address = LLDB_INVALID_ADDRESS;
766 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +0000767
768 if (!UpdateValueIfNeeded())
769 return address;
770
Greg Clayton73b472d2010-10-27 03:32:59 +0000771 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000772 {
773 case Value::eValueTypeScalar:
774 if (scalar_is_load_address)
775 {
776 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
777 address_type = eAddressTypeLoad;
778 }
779 break;
780
781 case Value::eValueTypeLoadAddress:
782 case Value::eValueTypeFileAddress:
783 case Value::eValueTypeHostAddress:
784 {
785 uint32_t data_offset = 0;
786 address = m_data.GetPointer(&data_offset);
787 address_type = m_value.GetValueAddressType();
788 if (address_type == eAddressTypeInvalid)
789 address_type = eAddressTypeLoad;
790 }
791 break;
792 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000793
794 if (m_pointers_point_to_load_addrs)
795 address_type = eAddressTypeLoad;
796
Greg Clayton737b9322010-09-13 03:32:57 +0000797 return address;
798}
799
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800bool
Jim Ingham6035b672011-03-31 00:19:25 +0000801ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802{
803 // Make sure our value is up to date first so that our location and location
804 // type is valid.
Jim Ingham6035b672011-03-31 00:19:25 +0000805 if (!UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000806 return false;
807
808 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000809 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810
811 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000812 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000813 switch (encoding)
814 {
815 case eEncodingInvalid:
816 return false;
817
818 case eEncodingUint:
819 if (byte_size > sizeof(unsigned long long))
820 {
821 return false;
822 }
823 else
824 {
825 unsigned long long ull_val = strtoull(value_str, &end, 0);
826 if (end && *end != '\0')
827 return false;
828 m_value = ull_val;
829 // Limit the bytes in our m_data appropriately.
830 m_value.GetScalar().GetData (m_data, byte_size);
831 }
832 break;
833
834 case eEncodingSint:
835 if (byte_size > sizeof(long long))
836 {
837 return false;
838 }
839 else
840 {
841 long long sll_val = strtoll(value_str, &end, 0);
842 if (end && *end != '\0')
843 return false;
844 m_value = sll_val;
845 // Limit the bytes in our m_data appropriately.
846 m_value.GetScalar().GetData (m_data, byte_size);
847 }
848 break;
849
850 case eEncodingIEEE754:
851 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000852 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000853 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 if (dst != NULL)
855 {
856 // We are decoding a float into host byte order below, so make
857 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000858 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
860 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000861 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 value_str,
863 dst,
864 byte_size);
865
866 if (converted_byte_size == byte_size)
867 {
868 }
869 }
870 }
871 break;
872
873 case eEncodingVector:
874 return false;
875
876 default:
877 return false;
878 }
879
880 // If we have made it here the value is in m_data and we should write it
881 // out to the target
882 return Write ();
883}
884
885bool
886ValueObject::Write ()
887{
888 // Clear the update ID so the next time we try and read the value
889 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +0000890 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891
892 // TODO: when Value has a method to write a value back, call it from here.
893 return false;
894
895}
896
Jim Ingham5a369122010-09-28 01:25:32 +0000897lldb::LanguageType
898ValueObject::GetObjectRuntimeLanguage ()
899{
Greg Clayton73b472d2010-10-27 03:32:59 +0000900 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000901 if (opaque_qual_type == NULL)
902 return lldb::eLanguageTypeC;
903
904 // If the type is a reference, then resolve it to what it refers to first:
905 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
906 if (qual_type->isAnyPointerType())
907 {
908 if (qual_type->isObjCObjectPointerType())
909 return lldb::eLanguageTypeObjC;
910
911 clang::QualType pointee_type (qual_type->getPointeeType());
912 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
913 return lldb::eLanguageTypeC_plus_plus;
914 if (pointee_type->isObjCObjectOrInterfaceType())
915 return lldb::eLanguageTypeObjC;
916 if (pointee_type->isObjCClassType())
917 return lldb::eLanguageTypeObjC;
918 }
919 else
920 {
921 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
922 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000923 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000924 return lldb::eLanguageTypeC_plus_plus;
925 }
926
927 return lldb::eLanguageTypeC;
928}
929
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000930void
Jim Ingham58b59f92011-04-22 23:53:53 +0000931ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000932{
Jim Ingham58b59f92011-04-22 23:53:53 +0000933 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934}
935
936ValueObjectSP
937ValueObject::GetSyntheticChild (const ConstString &key) const
938{
939 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +0000940 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000941 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +0000942 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000943 return synthetic_child_sp;
944}
945
946bool
947ValueObject::IsPointerType ()
948{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000949 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000950}
951
Jim Inghamb7603bb2011-03-18 00:05:18 +0000952bool
953ValueObject::IsIntegerType (bool &is_signed)
954{
955 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
956}
Greg Clayton73b472d2010-10-27 03:32:59 +0000957
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000958bool
959ValueObject::IsPointerOrReferenceType ()
960{
Greg Clayton007d5be2011-05-30 00:49:24 +0000961 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
962}
963
964bool
965ValueObject::IsPossibleCPlusPlusDynamicType ()
966{
967 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968}
969
970ValueObjectSP
971ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
972{
973 ValueObjectSP synthetic_child_sp;
974 if (IsPointerType ())
975 {
976 char index_str[64];
977 snprintf(index_str, sizeof(index_str), "[%i]", index);
978 ConstString index_const_str(index_str);
979 // Check if we have already created a synthetic array member in this
980 // valid object. If we have we will re-use it.
981 synthetic_child_sp = GetSyntheticChild (index_const_str);
982 if (!synthetic_child_sp)
983 {
Jim Ingham58b59f92011-04-22 23:53:53 +0000984 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 // We haven't made a synthetic array member for INDEX yet, so
986 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +0000987 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000988
989 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +0000990 if (synthetic_child)
991 {
992 AddSyntheticChild(index_const_str, synthetic_child);
993 synthetic_child_sp = synthetic_child->GetSP();
994 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000995 }
996 }
997 return synthetic_child_sp;
998}
Jim Ingham22777012010-09-23 02:01:19 +0000999
Jim Ingham78a685a2011-04-16 00:01:13 +00001000void
Jim Ingham2837b762011-05-04 03:43:18 +00001001ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00001002{
Jim Ingham2837b762011-05-04 03:43:18 +00001003 if (use_dynamic == lldb::eNoDynamicValues)
1004 return;
1005
Jim Ingham58b59f92011-04-22 23:53:53 +00001006 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00001007 {
1008 Process *process = m_update_point.GetProcess();
1009 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +00001010
Jim Ingham78a685a2011-04-16 00:01:13 +00001011
1012 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1013 // hard code this everywhere.
1014 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1015 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1016 {
1017 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1018 if (runtime)
1019 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1020 }
1021 else
1022 {
1023 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1024 if (cpp_runtime)
1025 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1026
1027 if (!worth_having_dynamic_value)
1028 {
1029 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1030 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001031 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001032 }
1033 }
1034
1035 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001036 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001037
1038// if (worth_having_dynamic_value)
1039// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1040
Jim Ingham78a685a2011-04-16 00:01:13 +00001041 }
1042}
1043
Jim Ingham58b59f92011-04-22 23:53:53 +00001044ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001045ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001046{
Jim Ingham2837b762011-05-04 03:43:18 +00001047 if (use_dynamic == lldb::eNoDynamicValues)
1048 return ValueObjectSP();
1049
1050 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001051 {
Jim Ingham2837b762011-05-04 03:43:18 +00001052 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001053 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001054 if (m_dynamic_value)
1055 return m_dynamic_value->GetSP();
1056 else
1057 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001058}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001059
Greg Claytone221f822011-01-21 01:59:00 +00001060bool
1061ValueObject::GetBaseClassPath (Stream &s)
1062{
1063 if (IsBaseClass())
1064 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001065 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001066 clang_type_t clang_type = GetClangType();
1067 std::string cxx_class_name;
1068 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1069 if (this_had_base_class)
1070 {
1071 if (parent_had_base_class)
1072 s.PutCString("::");
1073 s.PutCString(cxx_class_name.c_str());
1074 }
1075 return parent_had_base_class || this_had_base_class;
1076 }
1077 return false;
1078}
1079
1080
1081ValueObject *
1082ValueObject::GetNonBaseClassParent()
1083{
Jim Ingham78a685a2011-04-16 00:01:13 +00001084 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001085 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001086 if (GetParent()->IsBaseClass())
1087 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001088 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001089 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001090 }
1091 return NULL;
1092}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001093
1094void
Greg Clayton6beaaa62011-01-17 03:46:26 +00001095ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001096{
Greg Claytone221f822011-01-21 01:59:00 +00001097 const bool is_deref_of_parent = IsDereferenceOfParent ();
1098
1099 if (is_deref_of_parent)
1100 s.PutCString("*(");
1101
Jim Ingham78a685a2011-04-16 00:01:13 +00001102 if (GetParent())
1103 GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +00001104
1105 if (!IsBaseClass())
1106 {
1107 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001108 {
Greg Claytone221f822011-01-21 01:59:00 +00001109 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1110 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001111 {
Greg Claytone221f822011-01-21 01:59:00 +00001112 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1113 if (non_base_class_parent_clang_type)
1114 {
1115 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1116
1117 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1118 {
1119 s.PutCString("->");
1120 }
1121 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1122 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1123 {
1124 s.PutChar('.');
1125 }
1126 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001127 }
Greg Claytone221f822011-01-21 01:59:00 +00001128
1129 const char *name = GetName().GetCString();
1130 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001131 {
Greg Claytone221f822011-01-21 01:59:00 +00001132 if (qualify_cxx_base_classes)
1133 {
1134 if (GetBaseClassPath (s))
1135 s.PutCString("::");
1136 }
1137 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001138 }
1139 }
1140 }
1141
Greg Claytone221f822011-01-21 01:59:00 +00001142 if (is_deref_of_parent)
1143 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001144}
1145
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001146void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001147ValueObject::DumpValueObject
1148(
1149 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00001150 ValueObject *valobj,
1151 const char *root_valobj_name,
1152 uint32_t ptr_depth,
1153 uint32_t curr_depth,
1154 uint32_t max_depth,
1155 bool show_types,
1156 bool show_location,
1157 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00001158 lldb::DynamicValueType use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001159 bool scope_already_checked,
1160 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001161)
1162{
Greg Clayton007d5be2011-05-30 00:49:24 +00001163 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001164 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001165 bool update_success = valobj->UpdateValueIfNeeded ();
1166
1167 if (update_success && use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001168 {
Jim Ingham2837b762011-05-04 03:43:18 +00001169 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001170 if (dynamic_value)
1171 valobj = dynamic_value;
1172 }
1173
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001174 clang_type_t clang_type = valobj->GetClangType();
1175
Greg Clayton73b472d2010-10-27 03:32:59 +00001176 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001177 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001178 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1179 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001180
1181 const bool print_valobj = flat_output == false || has_value;
1182
1183 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001184 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001185 if (show_location)
1186 {
Jim Ingham6035b672011-03-31 00:19:25 +00001187 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001188 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001189
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001190 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001191
Greg Clayton7c8a9662010-11-02 01:50:16 +00001192 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001193 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001194 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001195
Greg Clayton1d3afba2010-10-05 00:00:42 +00001196
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001197 if (flat_output)
1198 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001199 // If we are showing types, also qualify the C++ base classes
1200 const bool qualify_cxx_base_classes = show_types;
1201 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001202 s.PutCString(" =");
1203 }
1204 else
1205 {
1206 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1207 s.Printf ("%s =", name_cstr);
1208 }
1209
Jim Ingham6035b672011-03-31 00:19:25 +00001210 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001211 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001212 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001213 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001214 }
1215
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001216 const char *val_cstr = NULL;
1217
1218 if (err_cstr == NULL)
1219 {
Jim Ingham6035b672011-03-31 00:19:25 +00001220 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001221 err_cstr = valobj->GetError().AsCString();
1222 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001223
1224 if (err_cstr)
1225 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001226 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001227 }
1228 else
1229 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001230 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001231 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001232 {
Jim Ingham6035b672011-03-31 00:19:25 +00001233 const char *sum_cstr = valobj->GetSummaryAsCString();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001234
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001235 if (val_cstr)
1236 s.Printf(" %s", val_cstr);
1237
1238 if (sum_cstr)
1239 s.Printf(" %s", sum_cstr);
1240
1241 if (use_objc)
1242 {
Jim Ingham6035b672011-03-31 00:19:25 +00001243 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001244 if (object_desc)
1245 s.Printf(" %s\n", object_desc);
1246 else
Sean Callanan672ad942010-10-23 00:18:49 +00001247 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001248 return;
1249 }
1250 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001251
1252 if (curr_depth < max_depth)
1253 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001254 // We will show children for all concrete types. We won't show
1255 // pointer contents unless a pointer depth has been specified.
1256 // We won't reference contents unless the reference is the
1257 // root object (depth of zero).
1258 bool print_children = true;
1259
1260 // Use a new temporary pointer depth in case we override the
1261 // current pointer depth below...
1262 uint32_t curr_ptr_depth = ptr_depth;
1263
1264 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1265 if (is_ptr || is_ref)
1266 {
1267 // We have a pointer or reference whose value is an address.
1268 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001269 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001270 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1271 print_children = false;
1272
1273 else if (is_ref && curr_depth == 0)
1274 {
1275 // If this is the root object (depth is zero) that we are showing
1276 // and it is a reference, and no pointer depth has been supplied
1277 // print out what it references. Don't do this at deeper depths
1278 // otherwise we can end up with infinite recursion...
1279 curr_ptr_depth = 1;
1280 }
1281
1282 if (curr_ptr_depth == 0)
1283 print_children = false;
1284 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001285
Greg Clayton73b472d2010-10-27 03:32:59 +00001286 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001287 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001288 const uint32_t num_children = valobj->GetNumChildren();
1289 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001290 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001291 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001292 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001293 if (print_valobj)
1294 s.EOL();
1295 }
1296 else
1297 {
1298 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001299 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001300 s.IndentMore();
1301 }
1302
1303 for (uint32_t idx=0; idx<num_children; ++idx)
1304 {
1305 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1306 if (child_sp.get())
1307 {
1308 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001309 child_sp.get(),
1310 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001311 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001312 curr_depth + 1,
1313 max_depth,
1314 show_types,
1315 show_location,
1316 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00001317 use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001318 true,
1319 flat_output);
1320 }
1321 }
1322
1323 if (!flat_output)
1324 {
1325 s.IndentLess();
1326 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001327 }
1328 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001329 else if (has_children)
1330 {
1331 // Aggregate, no children...
1332 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001333 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001334 }
1335 else
1336 {
1337 if (print_valobj)
1338 s.EOL();
1339 }
1340
Greg Clayton1d3afba2010-10-05 00:00:42 +00001341 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001342 else
1343 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001344 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001345 }
1346 }
1347 else
1348 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001349 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001350 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001351 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001352 }
1353 }
1354 }
1355 }
1356}
1357
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001358
1359ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00001360ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001361{
1362 ValueObjectSP valobj_sp;
1363
Jim Ingham6035b672011-03-31 00:19:25 +00001364 if (UpdateValueIfNeeded() && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001365 {
Jim Ingham6035b672011-03-31 00:19:25 +00001366 ExecutionContextScope *exe_scope = GetExecutionContextScope();
1367 if (exe_scope)
1368 {
1369 ExecutionContext exe_ctx;
1370 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001371
Jim Ingham6035b672011-03-31 00:19:25 +00001372 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001373
Jim Ingham6035b672011-03-31 00:19:25 +00001374 DataExtractor data;
1375 data.SetByteOrder (m_data.GetByteOrder());
1376 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001377
Jim Ingham6035b672011-03-31 00:19:25 +00001378 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001379
Jim Ingham58b59f92011-04-22 23:53:53 +00001380 valobj_sp = ValueObjectConstResult::Create (exe_scope,
1381 ast,
1382 GetClangType(),
1383 name,
1384 data);
Jim Ingham6035b672011-03-31 00:19:25 +00001385 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001386 }
Jim Ingham6035b672011-03-31 00:19:25 +00001387
1388 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001389 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001390 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001391 }
1392 return valobj_sp;
1393}
1394
1395lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001396ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001397{
Jim Ingham58b59f92011-04-22 23:53:53 +00001398 if (m_deref_valobj)
1399 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001400
Greg Clayton54979cd2010-12-15 05:08:08 +00001401 const bool is_pointer_type = IsPointerType();
1402 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001403 {
1404 bool omit_empty_base_classes = true;
1405
1406 std::string child_name_str;
1407 uint32_t child_byte_size = 0;
1408 int32_t child_byte_offset = 0;
1409 uint32_t child_bitfield_bit_size = 0;
1410 uint32_t child_bitfield_bit_offset = 0;
1411 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001412 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001413 const bool transparent_pointers = false;
1414 clang::ASTContext *clang_ast = GetClangAST();
1415 clang_type_t clang_type = GetClangType();
1416 clang_type_t child_clang_type;
1417 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1418 GetName().GetCString(),
1419 clang_type,
1420 0,
1421 transparent_pointers,
1422 omit_empty_base_classes,
1423 child_name_str,
1424 child_byte_size,
1425 child_byte_offset,
1426 child_bitfield_bit_size,
1427 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001428 child_is_base_class,
1429 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001430 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001431 {
1432 ConstString child_name;
1433 if (!child_name_str.empty())
1434 child_name.SetCString (child_name_str.c_str());
1435
Jim Ingham58b59f92011-04-22 23:53:53 +00001436 m_deref_valobj = new ValueObjectChild (*this,
1437 clang_ast,
1438 child_clang_type,
1439 child_name,
1440 child_byte_size,
1441 child_byte_offset,
1442 child_bitfield_bit_size,
1443 child_bitfield_bit_offset,
1444 child_is_base_class,
1445 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001446 }
1447 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001448
Jim Ingham58b59f92011-04-22 23:53:53 +00001449 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00001450 {
1451 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00001452 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00001453 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001454 else
1455 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001456 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001457 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001458
1459 if (is_pointer_type)
1460 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1461 else
1462 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00001463 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001464 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001465}
1466
Jim Ingham78a685a2011-04-16 00:01:13 +00001467lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00001468ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001469{
Jim Ingham78a685a2011-04-16 00:01:13 +00001470 if (m_addr_of_valobj_sp)
1471 return m_addr_of_valobj_sp;
1472
Greg Claytone0d378b2011-03-24 21:19:54 +00001473 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001474 const bool scalar_is_load_address = false;
1475 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001476 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001477 if (addr != LLDB_INVALID_ADDRESS)
1478 {
1479 switch (address_type)
1480 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001481 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001482 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001483 {
1484 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001485 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001486 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1487 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001488 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001489
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001490 case eAddressTypeFile:
1491 case eAddressTypeLoad:
1492 case eAddressTypeHost:
1493 {
1494 clang::ASTContext *ast = GetClangAST();
1495 clang_type_t clang_type = GetClangType();
1496 if (ast && clang_type)
1497 {
1498 std::string name (1, '&');
1499 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00001500 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1501 ast,
1502 ClangASTContext::CreatePointerType (ast, clang_type),
1503 ConstString (name.c_str()),
1504 addr,
1505 eAddressTypeInvalid,
1506 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001507 }
1508 }
1509 break;
1510 }
1511 }
Jim Ingham78a685a2011-04-16 00:01:13 +00001512 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001513}
1514
Greg Claytonb2dcc362011-05-05 23:32:56 +00001515
1516lldb::ValueObjectSP
1517ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
1518{
1519 lldb::ValueObjectSP valobj_sp;
1520 AddressType address_type;
1521 const bool scalar_is_load_address = true;
1522 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1523
1524 if (ptr_value != LLDB_INVALID_ADDRESS)
1525 {
1526 Address ptr_addr (NULL, ptr_value);
1527
1528 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1529 name,
1530 ptr_addr,
1531 clang_ast_type);
1532 }
1533 return valobj_sp;
1534}
1535
1536lldb::ValueObjectSP
1537ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
1538{
1539 lldb::ValueObjectSP valobj_sp;
1540 AddressType address_type;
1541 const bool scalar_is_load_address = true;
1542 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1543
1544 if (ptr_value != LLDB_INVALID_ADDRESS)
1545 {
1546 Address ptr_addr (NULL, ptr_value);
1547
1548 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1549 name,
1550 ptr_addr,
1551 type_sp);
1552 }
1553 return valobj_sp;
1554}
1555
1556
Jim Ingham6035b672011-03-31 00:19:25 +00001557ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00001558 m_thread_id (LLDB_INVALID_UID),
1559 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00001560{
1561}
1562
1563ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00001564 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001565 m_first_update (true),
1566 m_thread_id (LLDB_INVALID_UID),
1567 m_stop_id (0)
1568
Jim Ingham6035b672011-03-31 00:19:25 +00001569{
1570 ExecutionContext exe_ctx;
1571 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
1572 // and if so we want to cache that not the original.
1573 if (exe_scope)
1574 exe_scope->CalculateExecutionContext(exe_ctx);
1575 if (exe_ctx.target != NULL)
1576 {
1577 m_target_sp = exe_ctx.target->GetSP();
1578
1579 if (exe_ctx.process == NULL)
1580 m_process_sp = exe_ctx.target->GetProcessSP();
1581 else
1582 m_process_sp = exe_ctx.process->GetSP();
1583
1584 if (m_process_sp != NULL)
1585 {
1586 m_stop_id = m_process_sp->GetStopID();
1587 Thread *thread = NULL;
1588
1589 if (exe_ctx.thread == NULL)
1590 {
1591 if (use_selected)
1592 {
1593 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1594 if (thread)
1595 computed_exe_scope = thread;
1596 }
1597 }
1598 else
1599 thread = exe_ctx.thread;
1600
1601 if (thread != NULL)
1602 {
1603 m_thread_id = thread->GetIndexID();
1604 if (exe_ctx.frame == NULL)
1605 {
1606 if (use_selected)
1607 {
1608 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1609 if (frame)
1610 {
1611 m_stack_id = frame->GetStackID();
1612 computed_exe_scope = frame;
1613 }
1614 }
1615 }
1616 else
1617 m_stack_id = exe_ctx.frame->GetStackID();
1618 }
1619 }
1620 }
1621 m_exe_scope = computed_exe_scope;
1622}
1623
1624ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1625 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001626 m_needs_update(true),
1627 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00001628 m_target_sp (rhs.m_target_sp),
1629 m_process_sp (rhs.m_process_sp),
1630 m_thread_id (rhs.m_thread_id),
1631 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00001632 m_stop_id (0)
1633{
1634}
1635
1636ValueObject::EvaluationPoint::~EvaluationPoint ()
1637{
1638}
1639
1640ExecutionContextScope *
1641ValueObject::EvaluationPoint::GetExecutionContextScope ()
1642{
1643 // We have to update before giving out the scope, or we could be handing out stale pointers.
1644 SyncWithProcessState();
1645
1646 return m_exe_scope;
1647}
1648
1649// This function checks the EvaluationPoint against the current process state. If the current
1650// state matches the evaluation point, or the evaluation point is already invalid, then we return
1651// false, meaning "no change". If the current state is different, we update our state, and return
1652// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
1653// future calls to NeedsUpdate will return true.
1654
1655bool
1656ValueObject::EvaluationPoint::SyncWithProcessState()
1657{
1658 // If we're already invalid, we don't need to do anything, and nothing has changed:
1659 if (m_stop_id == LLDB_INVALID_UID)
1660 {
1661 // Can't update with an invalid state.
1662 m_needs_update = false;
1663 return false;
1664 }
1665
1666 // If we don't have a process nothing can change.
1667 if (!m_process_sp)
1668 return false;
1669
1670 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00001671 uint32_t cur_stop_id = m_process_sp->GetStopID();
1672 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00001673 return false;
1674
Jim Ingham78a685a2011-04-16 00:01:13 +00001675 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1676 // In either case, we aren't going to be able to sync with the process state.
1677 if (cur_stop_id == 0)
1678 return false;
1679
1680 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00001681 m_needs_update = true;
1682 m_exe_scope = m_process_sp.get();
1683
1684 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
1685 // doesn't, mark ourselves as invalid.
1686
1687 if (m_thread_id != LLDB_INVALID_THREAD_ID)
1688 {
1689 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1690 if (our_thread == NULL)
1691 SetInvalid();
1692 else
1693 {
1694 m_exe_scope = our_thread;
1695
1696 if (m_stack_id.IsValid())
1697 {
1698 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1699 if (our_frame == NULL)
1700 SetInvalid();
1701 else
1702 m_exe_scope = our_frame;
1703 }
1704 }
1705 }
1706 return true;
1707}
1708
Jim Ingham61be0902011-05-02 18:13:59 +00001709void
1710ValueObject::EvaluationPoint::SetUpdated ()
1711{
1712 m_first_update = false;
1713 m_needs_update = false;
1714 if (m_process_sp)
1715 m_stop_id = m_process_sp->GetStopID();
1716}
1717
1718
Jim Ingham6035b672011-03-31 00:19:25 +00001719bool
1720ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1721{
1722 if (!IsValid())
1723 return false;
1724
1725 bool needs_update = false;
1726 m_exe_scope = NULL;
1727
1728 // The target has to be non-null, and the
1729 Target *target = exe_scope->CalculateTarget();
1730 if (target != NULL)
1731 {
1732 Target *old_target = m_target_sp.get();
1733 assert (target == old_target);
1734 Process *process = exe_scope->CalculateProcess();
1735 if (process != NULL)
1736 {
1737 // FOR NOW - assume you can't update variable objects across process boundaries.
1738 Process *old_process = m_process_sp.get();
1739 assert (process == old_process);
1740
1741 lldb::user_id_t stop_id = process->GetStopID();
1742 if (stop_id != m_stop_id)
1743 {
1744 needs_update = true;
1745 m_stop_id = stop_id;
1746 }
1747 // See if we're switching the thread or stack context. If no thread is given, this is
1748 // being evaluated in a global context.
1749 Thread *thread = exe_scope->CalculateThread();
1750 if (thread != NULL)
1751 {
1752 lldb::user_id_t new_thread_index = thread->GetIndexID();
1753 if (new_thread_index != m_thread_id)
1754 {
1755 needs_update = true;
1756 m_thread_id = new_thread_index;
1757 m_stack_id.Clear();
1758 }
1759
1760 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1761 if (new_frame != NULL)
1762 {
1763 if (new_frame->GetStackID() != m_stack_id)
1764 {
1765 needs_update = true;
1766 m_stack_id = new_frame->GetStackID();
1767 }
1768 }
1769 else
1770 {
1771 m_stack_id.Clear();
1772 needs_update = true;
1773 }
1774 }
1775 else
1776 {
1777 // If this had been given a thread, and now there is none, we should update.
1778 // Otherwise we don't have to do anything.
1779 if (m_thread_id != LLDB_INVALID_UID)
1780 {
1781 m_thread_id = LLDB_INVALID_UID;
1782 m_stack_id.Clear();
1783 needs_update = true;
1784 }
1785 }
1786 }
1787 else
1788 {
1789 // If there is no process, then we don't need to update anything.
1790 // But if we're switching from having a process to not, we should try to update.
1791 if (m_process_sp.get() != NULL)
1792 {
1793 needs_update = true;
1794 m_process_sp.reset();
1795 m_thread_id = LLDB_INVALID_UID;
1796 m_stack_id.Clear();
1797 }
1798 }
1799 }
1800 else
1801 {
1802 // If there's no target, nothing can change so we don't need to update anything.
1803 // But if we're switching from having a target to not, we should try to update.
1804 if (m_target_sp.get() != NULL)
1805 {
1806 needs_update = true;
1807 m_target_sp.reset();
1808 m_process_sp.reset();
1809 m_thread_id = LLDB_INVALID_UID;
1810 m_stack_id.Clear();
1811 }
1812 }
1813 if (!m_needs_update)
1814 m_needs_update = needs_update;
1815
1816 return needs_update;
1817}