blob: 10247db11e2a539773b491fca8a1ca7fa9ccf341 [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
693 m_value_str.clear();
694 }
695 }
696 break;
697
Greg Clayton526e5af2010-11-13 03:52:47 +0000698 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000699 {
700 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
701 if (reg_info)
702 {
703 StreamString reg_sstr;
704 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
705 m_value_str.swap(reg_sstr.GetString());
706 }
707 }
708 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000709
710 default:
711 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712 }
713 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000714
715 if (!m_value_did_change && m_old_value_valid)
716 {
717 // The value was gotten successfully, so we consider the
718 // value as changed if the value string differs
719 SetValueDidChange (m_old_value_str != m_value_str);
720 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 }
722 }
723 if (m_value_str.empty())
724 return NULL;
725 return m_value_str.c_str();
726}
727
Greg Clayton737b9322010-09-13 03:32:57 +0000728addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000729ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000730{
Jim Ingham78a685a2011-04-16 00:01:13 +0000731 if (!UpdateValueIfNeeded())
732 return LLDB_INVALID_ADDRESS;
733
Greg Clayton73b472d2010-10-27 03:32:59 +0000734 switch (m_value.GetValueType())
735 {
736 case Value::eValueTypeScalar:
737 if (scalar_is_load_address)
738 {
739 address_type = eAddressTypeLoad;
740 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
741 }
742 break;
743
744 case Value::eValueTypeLoadAddress:
745 case Value::eValueTypeFileAddress:
746 case Value::eValueTypeHostAddress:
747 {
748 address_type = m_value.GetValueAddressType ();
749 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
750 }
751 break;
752 }
753 address_type = eAddressTypeInvalid;
754 return LLDB_INVALID_ADDRESS;
755}
756
757addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000758ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000759{
760 lldb::addr_t address = LLDB_INVALID_ADDRESS;
761 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +0000762
763 if (!UpdateValueIfNeeded())
764 return address;
765
Greg Clayton73b472d2010-10-27 03:32:59 +0000766 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000767 {
768 case Value::eValueTypeScalar:
769 if (scalar_is_load_address)
770 {
771 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
772 address_type = eAddressTypeLoad;
773 }
774 break;
775
776 case Value::eValueTypeLoadAddress:
777 case Value::eValueTypeFileAddress:
778 case Value::eValueTypeHostAddress:
779 {
780 uint32_t data_offset = 0;
781 address = m_data.GetPointer(&data_offset);
782 address_type = m_value.GetValueAddressType();
783 if (address_type == eAddressTypeInvalid)
784 address_type = eAddressTypeLoad;
785 }
786 break;
787 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000788
789 if (m_pointers_point_to_load_addrs)
790 address_type = eAddressTypeLoad;
791
Greg Clayton737b9322010-09-13 03:32:57 +0000792 return address;
793}
794
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795bool
Jim Ingham6035b672011-03-31 00:19:25 +0000796ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797{
798 // Make sure our value is up to date first so that our location and location
799 // type is valid.
Jim Ingham6035b672011-03-31 00:19:25 +0000800 if (!UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 return false;
802
803 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000804 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805
806 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000807 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 switch (encoding)
809 {
810 case eEncodingInvalid:
811 return false;
812
813 case eEncodingUint:
814 if (byte_size > sizeof(unsigned long long))
815 {
816 return false;
817 }
818 else
819 {
820 unsigned long long ull_val = strtoull(value_str, &end, 0);
821 if (end && *end != '\0')
822 return false;
823 m_value = ull_val;
824 // Limit the bytes in our m_data appropriately.
825 m_value.GetScalar().GetData (m_data, byte_size);
826 }
827 break;
828
829 case eEncodingSint:
830 if (byte_size > sizeof(long long))
831 {
832 return false;
833 }
834 else
835 {
836 long long sll_val = strtoll(value_str, &end, 0);
837 if (end && *end != '\0')
838 return false;
839 m_value = sll_val;
840 // Limit the bytes in our m_data appropriately.
841 m_value.GetScalar().GetData (m_data, byte_size);
842 }
843 break;
844
845 case eEncodingIEEE754:
846 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000847 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000848 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000849 if (dst != NULL)
850 {
851 // We are decoding a float into host byte order below, so make
852 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000853 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000854 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
855 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000856 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857 value_str,
858 dst,
859 byte_size);
860
861 if (converted_byte_size == byte_size)
862 {
863 }
864 }
865 }
866 break;
867
868 case eEncodingVector:
869 return false;
870
871 default:
872 return false;
873 }
874
875 // If we have made it here the value is in m_data and we should write it
876 // out to the target
877 return Write ();
878}
879
880bool
881ValueObject::Write ()
882{
883 // Clear the update ID so the next time we try and read the value
884 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +0000885 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000886
887 // TODO: when Value has a method to write a value back, call it from here.
888 return false;
889
890}
891
Jim Ingham5a369122010-09-28 01:25:32 +0000892lldb::LanguageType
893ValueObject::GetObjectRuntimeLanguage ()
894{
Greg Clayton73b472d2010-10-27 03:32:59 +0000895 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000896 if (opaque_qual_type == NULL)
897 return lldb::eLanguageTypeC;
898
899 // If the type is a reference, then resolve it to what it refers to first:
900 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
901 if (qual_type->isAnyPointerType())
902 {
903 if (qual_type->isObjCObjectPointerType())
904 return lldb::eLanguageTypeObjC;
905
906 clang::QualType pointee_type (qual_type->getPointeeType());
907 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
908 return lldb::eLanguageTypeC_plus_plus;
909 if (pointee_type->isObjCObjectOrInterfaceType())
910 return lldb::eLanguageTypeObjC;
911 if (pointee_type->isObjCClassType())
912 return lldb::eLanguageTypeObjC;
913 }
914 else
915 {
916 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
917 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000918 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000919 return lldb::eLanguageTypeC_plus_plus;
920 }
921
922 return lldb::eLanguageTypeC;
923}
924
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000925void
Jim Ingham58b59f92011-04-22 23:53:53 +0000926ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000927{
Jim Ingham58b59f92011-04-22 23:53:53 +0000928 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000929}
930
931ValueObjectSP
932ValueObject::GetSyntheticChild (const ConstString &key) const
933{
934 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +0000935 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000936 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +0000937 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000938 return synthetic_child_sp;
939}
940
941bool
942ValueObject::IsPointerType ()
943{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000944 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000945}
946
Jim Inghamb7603bb2011-03-18 00:05:18 +0000947bool
948ValueObject::IsIntegerType (bool &is_signed)
949{
950 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
951}
Greg Clayton73b472d2010-10-27 03:32:59 +0000952
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000953bool
954ValueObject::IsPointerOrReferenceType ()
955{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000956 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000957}
958
959ValueObjectSP
960ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
961{
962 ValueObjectSP synthetic_child_sp;
963 if (IsPointerType ())
964 {
965 char index_str[64];
966 snprintf(index_str, sizeof(index_str), "[%i]", index);
967 ConstString index_const_str(index_str);
968 // Check if we have already created a synthetic array member in this
969 // valid object. If we have we will re-use it.
970 synthetic_child_sp = GetSyntheticChild (index_const_str);
971 if (!synthetic_child_sp)
972 {
Jim Ingham58b59f92011-04-22 23:53:53 +0000973 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974 // We haven't made a synthetic array member for INDEX yet, so
975 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +0000976 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977
978 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +0000979 if (synthetic_child)
980 {
981 AddSyntheticChild(index_const_str, synthetic_child);
982 synthetic_child_sp = synthetic_child->GetSP();
983 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000984 }
985 }
986 return synthetic_child_sp;
987}
Jim Ingham22777012010-09-23 02:01:19 +0000988
Jim Ingham78a685a2011-04-16 00:01:13 +0000989void
Jim Ingham2837b762011-05-04 03:43:18 +0000990ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +0000991{
Jim Ingham2837b762011-05-04 03:43:18 +0000992 if (use_dynamic == lldb::eNoDynamicValues)
993 return;
994
Jim Ingham58b59f92011-04-22 23:53:53 +0000995 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +0000996 {
997 Process *process = m_update_point.GetProcess();
998 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +0000999
Jim Ingham78a685a2011-04-16 00:01:13 +00001000
1001 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1002 // hard code this everywhere.
1003 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1004 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1005 {
1006 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1007 if (runtime)
1008 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1009 }
1010 else
1011 {
1012 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1013 if (cpp_runtime)
1014 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1015
1016 if (!worth_having_dynamic_value)
1017 {
1018 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1019 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001020 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001021 }
1022 }
1023
1024 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001025 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001026
1027// if (worth_having_dynamic_value)
1028// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1029
Jim Ingham78a685a2011-04-16 00:01:13 +00001030 }
1031}
1032
Jim Ingham58b59f92011-04-22 23:53:53 +00001033ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001034ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001035{
Jim Ingham2837b762011-05-04 03:43:18 +00001036 if (use_dynamic == lldb::eNoDynamicValues)
1037 return ValueObjectSP();
1038
1039 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001040 {
Jim Ingham2837b762011-05-04 03:43:18 +00001041 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001042 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001043 if (m_dynamic_value)
1044 return m_dynamic_value->GetSP();
1045 else
1046 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001047}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001048
Greg Claytone221f822011-01-21 01:59:00 +00001049bool
1050ValueObject::GetBaseClassPath (Stream &s)
1051{
1052 if (IsBaseClass())
1053 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001054 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001055 clang_type_t clang_type = GetClangType();
1056 std::string cxx_class_name;
1057 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1058 if (this_had_base_class)
1059 {
1060 if (parent_had_base_class)
1061 s.PutCString("::");
1062 s.PutCString(cxx_class_name.c_str());
1063 }
1064 return parent_had_base_class || this_had_base_class;
1065 }
1066 return false;
1067}
1068
1069
1070ValueObject *
1071ValueObject::GetNonBaseClassParent()
1072{
Jim Ingham78a685a2011-04-16 00:01:13 +00001073 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001074 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001075 if (GetParent()->IsBaseClass())
1076 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001077 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001078 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001079 }
1080 return NULL;
1081}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001082
1083void
Greg Clayton6beaaa62011-01-17 03:46:26 +00001084ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001085{
Greg Claytone221f822011-01-21 01:59:00 +00001086 const bool is_deref_of_parent = IsDereferenceOfParent ();
1087
1088 if (is_deref_of_parent)
1089 s.PutCString("*(");
1090
Jim Ingham78a685a2011-04-16 00:01:13 +00001091 if (GetParent())
1092 GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +00001093
1094 if (!IsBaseClass())
1095 {
1096 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001097 {
Greg Claytone221f822011-01-21 01:59:00 +00001098 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1099 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001100 {
Greg Claytone221f822011-01-21 01:59:00 +00001101 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1102 if (non_base_class_parent_clang_type)
1103 {
1104 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1105
1106 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1107 {
1108 s.PutCString("->");
1109 }
1110 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1111 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1112 {
1113 s.PutChar('.');
1114 }
1115 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001116 }
Greg Claytone221f822011-01-21 01:59:00 +00001117
1118 const char *name = GetName().GetCString();
1119 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001120 {
Greg Claytone221f822011-01-21 01:59:00 +00001121 if (qualify_cxx_base_classes)
1122 {
1123 if (GetBaseClassPath (s))
1124 s.PutCString("::");
1125 }
1126 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001127 }
1128 }
1129 }
1130
Greg Claytone221f822011-01-21 01:59:00 +00001131 if (is_deref_of_parent)
1132 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001133}
1134
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001135void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001136ValueObject::DumpValueObject
1137(
1138 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00001139 ValueObject *valobj,
1140 const char *root_valobj_name,
1141 uint32_t ptr_depth,
1142 uint32_t curr_depth,
1143 uint32_t max_depth,
1144 bool show_types,
1145 bool show_location,
1146 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00001147 lldb::DynamicValueType use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001148 bool scope_already_checked,
1149 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001150)
1151{
Jim Ingham6035b672011-03-31 00:19:25 +00001152 if (valobj && valobj->UpdateValueIfNeeded ())
Greg Clayton1d3afba2010-10-05 00:00:42 +00001153 {
Jim Ingham2837b762011-05-04 03:43:18 +00001154 if (use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001155 {
Jim Ingham2837b762011-05-04 03:43:18 +00001156 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001157 if (dynamic_value)
1158 valobj = dynamic_value;
1159 }
1160
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001161 clang_type_t clang_type = valobj->GetClangType();
1162
Greg Clayton73b472d2010-10-27 03:32:59 +00001163 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001164 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001165 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1166 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001167
1168 const bool print_valobj = flat_output == false || has_value;
1169
1170 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001171 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001172 if (show_location)
1173 {
Jim Ingham6035b672011-03-31 00:19:25 +00001174 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001175 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001176
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001177 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001178
Greg Clayton7c8a9662010-11-02 01:50:16 +00001179 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001180 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001181 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001182
Greg Clayton1d3afba2010-10-05 00:00:42 +00001183
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001184 if (flat_output)
1185 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001186 // If we are showing types, also qualify the C++ base classes
1187 const bool qualify_cxx_base_classes = show_types;
1188 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001189 s.PutCString(" =");
1190 }
1191 else
1192 {
1193 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1194 s.Printf ("%s =", name_cstr);
1195 }
1196
Jim Ingham6035b672011-03-31 00:19:25 +00001197 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001198 {
1199 err_cstr = "error: out of scope";
1200 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001201 }
1202
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001203 const char *val_cstr = NULL;
1204
1205 if (err_cstr == NULL)
1206 {
Jim Ingham6035b672011-03-31 00:19:25 +00001207 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001208 err_cstr = valobj->GetError().AsCString();
1209 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001210
1211 if (err_cstr)
1212 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001213 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001214 }
1215 else
1216 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001217 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001218 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001219 {
Jim Ingham6035b672011-03-31 00:19:25 +00001220 const char *sum_cstr = valobj->GetSummaryAsCString();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001221
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001222 if (val_cstr)
1223 s.Printf(" %s", val_cstr);
1224
1225 if (sum_cstr)
1226 s.Printf(" %s", sum_cstr);
1227
1228 if (use_objc)
1229 {
Jim Ingham6035b672011-03-31 00:19:25 +00001230 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001231 if (object_desc)
1232 s.Printf(" %s\n", object_desc);
1233 else
Sean Callanan672ad942010-10-23 00:18:49 +00001234 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001235 return;
1236 }
1237 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001238
1239 if (curr_depth < max_depth)
1240 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001241 // We will show children for all concrete types. We won't show
1242 // pointer contents unless a pointer depth has been specified.
1243 // We won't reference contents unless the reference is the
1244 // root object (depth of zero).
1245 bool print_children = true;
1246
1247 // Use a new temporary pointer depth in case we override the
1248 // current pointer depth below...
1249 uint32_t curr_ptr_depth = ptr_depth;
1250
1251 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1252 if (is_ptr || is_ref)
1253 {
1254 // We have a pointer or reference whose value is an address.
1255 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001256 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001257 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1258 print_children = false;
1259
1260 else if (is_ref && curr_depth == 0)
1261 {
1262 // If this is the root object (depth is zero) that we are showing
1263 // and it is a reference, and no pointer depth has been supplied
1264 // print out what it references. Don't do this at deeper depths
1265 // otherwise we can end up with infinite recursion...
1266 curr_ptr_depth = 1;
1267 }
1268
1269 if (curr_ptr_depth == 0)
1270 print_children = false;
1271 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001272
Greg Clayton73b472d2010-10-27 03:32:59 +00001273 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001274 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001275 const uint32_t num_children = valobj->GetNumChildren();
1276 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001277 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001278 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001279 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001280 if (print_valobj)
1281 s.EOL();
1282 }
1283 else
1284 {
1285 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001286 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001287 s.IndentMore();
1288 }
1289
1290 for (uint32_t idx=0; idx<num_children; ++idx)
1291 {
1292 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1293 if (child_sp.get())
1294 {
1295 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001296 child_sp.get(),
1297 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001298 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001299 curr_depth + 1,
1300 max_depth,
1301 show_types,
1302 show_location,
1303 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00001304 use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001305 true,
1306 flat_output);
1307 }
1308 }
1309
1310 if (!flat_output)
1311 {
1312 s.IndentLess();
1313 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001314 }
1315 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001316 else if (has_children)
1317 {
1318 // Aggregate, no children...
1319 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001320 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001321 }
1322 else
1323 {
1324 if (print_valobj)
1325 s.EOL();
1326 }
1327
Greg Clayton1d3afba2010-10-05 00:00:42 +00001328 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001329 else
1330 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001331 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001332 }
1333 }
1334 else
1335 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001336 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001337 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001338 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001339 }
1340 }
1341 }
1342 }
1343}
1344
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001345
1346ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00001347ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001348{
1349 ValueObjectSP valobj_sp;
1350
Jim Ingham6035b672011-03-31 00:19:25 +00001351 if (UpdateValueIfNeeded() && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001352 {
Jim Ingham6035b672011-03-31 00:19:25 +00001353 ExecutionContextScope *exe_scope = GetExecutionContextScope();
1354 if (exe_scope)
1355 {
1356 ExecutionContext exe_ctx;
1357 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001358
Jim Ingham6035b672011-03-31 00:19:25 +00001359 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001360
Jim Ingham6035b672011-03-31 00:19:25 +00001361 DataExtractor data;
1362 data.SetByteOrder (m_data.GetByteOrder());
1363 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001364
Jim Ingham6035b672011-03-31 00:19:25 +00001365 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001366
Jim Ingham58b59f92011-04-22 23:53:53 +00001367 valobj_sp = ValueObjectConstResult::Create (exe_scope,
1368 ast,
1369 GetClangType(),
1370 name,
1371 data);
Jim Ingham6035b672011-03-31 00:19:25 +00001372 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001373 }
Jim Ingham6035b672011-03-31 00:19:25 +00001374
1375 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001376 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001377 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001378 }
1379 return valobj_sp;
1380}
1381
1382lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001383ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001384{
Jim Ingham58b59f92011-04-22 23:53:53 +00001385 if (m_deref_valobj)
1386 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001387
Greg Clayton54979cd2010-12-15 05:08:08 +00001388 const bool is_pointer_type = IsPointerType();
1389 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001390 {
1391 bool omit_empty_base_classes = true;
1392
1393 std::string child_name_str;
1394 uint32_t child_byte_size = 0;
1395 int32_t child_byte_offset = 0;
1396 uint32_t child_bitfield_bit_size = 0;
1397 uint32_t child_bitfield_bit_offset = 0;
1398 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001399 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001400 const bool transparent_pointers = false;
1401 clang::ASTContext *clang_ast = GetClangAST();
1402 clang_type_t clang_type = GetClangType();
1403 clang_type_t child_clang_type;
1404 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1405 GetName().GetCString(),
1406 clang_type,
1407 0,
1408 transparent_pointers,
1409 omit_empty_base_classes,
1410 child_name_str,
1411 child_byte_size,
1412 child_byte_offset,
1413 child_bitfield_bit_size,
1414 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001415 child_is_base_class,
1416 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001417 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001418 {
1419 ConstString child_name;
1420 if (!child_name_str.empty())
1421 child_name.SetCString (child_name_str.c_str());
1422
Jim Ingham58b59f92011-04-22 23:53:53 +00001423 m_deref_valobj = new ValueObjectChild (*this,
1424 clang_ast,
1425 child_clang_type,
1426 child_name,
1427 child_byte_size,
1428 child_byte_offset,
1429 child_bitfield_bit_size,
1430 child_bitfield_bit_offset,
1431 child_is_base_class,
1432 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001433 }
1434 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001435
Jim Ingham58b59f92011-04-22 23:53:53 +00001436 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00001437 {
1438 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00001439 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00001440 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001441 else
1442 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001443 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001444 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001445
1446 if (is_pointer_type)
1447 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1448 else
1449 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00001450 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001451 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001452}
1453
Jim Ingham78a685a2011-04-16 00:01:13 +00001454lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00001455ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001456{
Jim Ingham78a685a2011-04-16 00:01:13 +00001457 if (m_addr_of_valobj_sp)
1458 return m_addr_of_valobj_sp;
1459
Greg Claytone0d378b2011-03-24 21:19:54 +00001460 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001461 const bool scalar_is_load_address = false;
1462 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001463 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001464 if (addr != LLDB_INVALID_ADDRESS)
1465 {
1466 switch (address_type)
1467 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001468 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001469 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001470 {
1471 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001472 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001473 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1474 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001475 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001476
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001477 case eAddressTypeFile:
1478 case eAddressTypeLoad:
1479 case eAddressTypeHost:
1480 {
1481 clang::ASTContext *ast = GetClangAST();
1482 clang_type_t clang_type = GetClangType();
1483 if (ast && clang_type)
1484 {
1485 std::string name (1, '&');
1486 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00001487 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1488 ast,
1489 ClangASTContext::CreatePointerType (ast, clang_type),
1490 ConstString (name.c_str()),
1491 addr,
1492 eAddressTypeInvalid,
1493 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001494 }
1495 }
1496 break;
1497 }
1498 }
Jim Ingham78a685a2011-04-16 00:01:13 +00001499 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001500}
1501
Greg Claytonb2dcc362011-05-05 23:32:56 +00001502
1503lldb::ValueObjectSP
1504ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
1505{
1506 lldb::ValueObjectSP valobj_sp;
1507 AddressType address_type;
1508 const bool scalar_is_load_address = true;
1509 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1510
1511 if (ptr_value != LLDB_INVALID_ADDRESS)
1512 {
1513 Address ptr_addr (NULL, ptr_value);
1514
1515 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1516 name,
1517 ptr_addr,
1518 clang_ast_type);
1519 }
1520 return valobj_sp;
1521}
1522
1523lldb::ValueObjectSP
1524ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
1525{
1526 lldb::ValueObjectSP valobj_sp;
1527 AddressType address_type;
1528 const bool scalar_is_load_address = true;
1529 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1530
1531 if (ptr_value != LLDB_INVALID_ADDRESS)
1532 {
1533 Address ptr_addr (NULL, ptr_value);
1534
1535 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1536 name,
1537 ptr_addr,
1538 type_sp);
1539 }
1540 return valobj_sp;
1541}
1542
1543
Jim Ingham6035b672011-03-31 00:19:25 +00001544ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00001545 m_thread_id (LLDB_INVALID_UID),
1546 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00001547{
1548}
1549
1550ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00001551 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001552 m_first_update (true),
1553 m_thread_id (LLDB_INVALID_UID),
1554 m_stop_id (0)
1555
Jim Ingham6035b672011-03-31 00:19:25 +00001556{
1557 ExecutionContext exe_ctx;
1558 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
1559 // and if so we want to cache that not the original.
1560 if (exe_scope)
1561 exe_scope->CalculateExecutionContext(exe_ctx);
1562 if (exe_ctx.target != NULL)
1563 {
1564 m_target_sp = exe_ctx.target->GetSP();
1565
1566 if (exe_ctx.process == NULL)
1567 m_process_sp = exe_ctx.target->GetProcessSP();
1568 else
1569 m_process_sp = exe_ctx.process->GetSP();
1570
1571 if (m_process_sp != NULL)
1572 {
1573 m_stop_id = m_process_sp->GetStopID();
1574 Thread *thread = NULL;
1575
1576 if (exe_ctx.thread == NULL)
1577 {
1578 if (use_selected)
1579 {
1580 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1581 if (thread)
1582 computed_exe_scope = thread;
1583 }
1584 }
1585 else
1586 thread = exe_ctx.thread;
1587
1588 if (thread != NULL)
1589 {
1590 m_thread_id = thread->GetIndexID();
1591 if (exe_ctx.frame == NULL)
1592 {
1593 if (use_selected)
1594 {
1595 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1596 if (frame)
1597 {
1598 m_stack_id = frame->GetStackID();
1599 computed_exe_scope = frame;
1600 }
1601 }
1602 }
1603 else
1604 m_stack_id = exe_ctx.frame->GetStackID();
1605 }
1606 }
1607 }
1608 m_exe_scope = computed_exe_scope;
1609}
1610
1611ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1612 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001613 m_needs_update(true),
1614 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00001615 m_target_sp (rhs.m_target_sp),
1616 m_process_sp (rhs.m_process_sp),
1617 m_thread_id (rhs.m_thread_id),
1618 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00001619 m_stop_id (0)
1620{
1621}
1622
1623ValueObject::EvaluationPoint::~EvaluationPoint ()
1624{
1625}
1626
1627ExecutionContextScope *
1628ValueObject::EvaluationPoint::GetExecutionContextScope ()
1629{
1630 // We have to update before giving out the scope, or we could be handing out stale pointers.
1631 SyncWithProcessState();
1632
1633 return m_exe_scope;
1634}
1635
1636// This function checks the EvaluationPoint against the current process state. If the current
1637// state matches the evaluation point, or the evaluation point is already invalid, then we return
1638// false, meaning "no change". If the current state is different, we update our state, and return
1639// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
1640// future calls to NeedsUpdate will return true.
1641
1642bool
1643ValueObject::EvaluationPoint::SyncWithProcessState()
1644{
1645 // If we're already invalid, we don't need to do anything, and nothing has changed:
1646 if (m_stop_id == LLDB_INVALID_UID)
1647 {
1648 // Can't update with an invalid state.
1649 m_needs_update = false;
1650 return false;
1651 }
1652
1653 // If we don't have a process nothing can change.
1654 if (!m_process_sp)
1655 return false;
1656
1657 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00001658 uint32_t cur_stop_id = m_process_sp->GetStopID();
1659 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00001660 return false;
1661
Jim Ingham78a685a2011-04-16 00:01:13 +00001662 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1663 // In either case, we aren't going to be able to sync with the process state.
1664 if (cur_stop_id == 0)
1665 return false;
1666
1667 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00001668 m_needs_update = true;
1669 m_exe_scope = m_process_sp.get();
1670
1671 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
1672 // doesn't, mark ourselves as invalid.
1673
1674 if (m_thread_id != LLDB_INVALID_THREAD_ID)
1675 {
1676 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1677 if (our_thread == NULL)
1678 SetInvalid();
1679 else
1680 {
1681 m_exe_scope = our_thread;
1682
1683 if (m_stack_id.IsValid())
1684 {
1685 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1686 if (our_frame == NULL)
1687 SetInvalid();
1688 else
1689 m_exe_scope = our_frame;
1690 }
1691 }
1692 }
1693 return true;
1694}
1695
Jim Ingham61be0902011-05-02 18:13:59 +00001696void
1697ValueObject::EvaluationPoint::SetUpdated ()
1698{
1699 m_first_update = false;
1700 m_needs_update = false;
1701 if (m_process_sp)
1702 m_stop_id = m_process_sp->GetStopID();
1703}
1704
1705
Jim Ingham6035b672011-03-31 00:19:25 +00001706bool
1707ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1708{
1709 if (!IsValid())
1710 return false;
1711
1712 bool needs_update = false;
1713 m_exe_scope = NULL;
1714
1715 // The target has to be non-null, and the
1716 Target *target = exe_scope->CalculateTarget();
1717 if (target != NULL)
1718 {
1719 Target *old_target = m_target_sp.get();
1720 assert (target == old_target);
1721 Process *process = exe_scope->CalculateProcess();
1722 if (process != NULL)
1723 {
1724 // FOR NOW - assume you can't update variable objects across process boundaries.
1725 Process *old_process = m_process_sp.get();
1726 assert (process == old_process);
1727
1728 lldb::user_id_t stop_id = process->GetStopID();
1729 if (stop_id != m_stop_id)
1730 {
1731 needs_update = true;
1732 m_stop_id = stop_id;
1733 }
1734 // See if we're switching the thread or stack context. If no thread is given, this is
1735 // being evaluated in a global context.
1736 Thread *thread = exe_scope->CalculateThread();
1737 if (thread != NULL)
1738 {
1739 lldb::user_id_t new_thread_index = thread->GetIndexID();
1740 if (new_thread_index != m_thread_id)
1741 {
1742 needs_update = true;
1743 m_thread_id = new_thread_index;
1744 m_stack_id.Clear();
1745 }
1746
1747 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1748 if (new_frame != NULL)
1749 {
1750 if (new_frame->GetStackID() != m_stack_id)
1751 {
1752 needs_update = true;
1753 m_stack_id = new_frame->GetStackID();
1754 }
1755 }
1756 else
1757 {
1758 m_stack_id.Clear();
1759 needs_update = true;
1760 }
1761 }
1762 else
1763 {
1764 // If this had been given a thread, and now there is none, we should update.
1765 // Otherwise we don't have to do anything.
1766 if (m_thread_id != LLDB_INVALID_UID)
1767 {
1768 m_thread_id = LLDB_INVALID_UID;
1769 m_stack_id.Clear();
1770 needs_update = true;
1771 }
1772 }
1773 }
1774 else
1775 {
1776 // If there is no process, then we don't need to update anything.
1777 // But if we're switching from having a process to not, we should try to update.
1778 if (m_process_sp.get() != NULL)
1779 {
1780 needs_update = true;
1781 m_process_sp.reset();
1782 m_thread_id = LLDB_INVALID_UID;
1783 m_stack_id.Clear();
1784 }
1785 }
1786 }
1787 else
1788 {
1789 // If there's no target, nothing can change so we don't need to update anything.
1790 // But if we're switching from having a target to not, we should try to update.
1791 if (m_target_sp.get() != NULL)
1792 {
1793 needs_update = true;
1794 m_target_sp.reset();
1795 m_process_sp.reset();
1796 m_thread_id = LLDB_INVALID_UID;
1797 m_stack_id.Clear();
1798 }
1799 }
1800 if (!m_needs_update)
1801 m_needs_update = needs_update;
1802
1803 return needs_update;
1804}