blob: 80ff55e5ba131bcab7f491b5b06241185f343277 [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"
Greg Clayton4e4294b2011-06-17 23:50:44 +0000508 eFormatCharArray, // Print as characters
Jim Ingham6035b672011-03-31 00:19:25 +0000509 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"
Greg Clayton4e4294b2011-06-17 23:50:44 +0000538 eFormatCharArray, // Print as characters
Jim Ingham6035b672011-03-31 00:19:25 +0000539 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 {
Greg Claytonf60f3752011-06-23 18:38:25 +0000664 /*
665 this is a quick fix for the case in which we display a variable, then change its format with
666 type format add and the old display string keeps showing until one steps through the code
667 */
668 {
669 const Value::ContextType context_type = m_value.GetContextType();
670 switch (context_type)
671 {
672 case Value::eContextTypeClangType:
673 case Value::eContextTypeLLDBType:
674 case Value::eContextTypeVariable:
Greg Claytonbb7f31f2011-06-23 21:22:24 +0000675 {
676 Format format = GetFormat();
677 if (format != m_last_format)
678 m_value_str.clear();
679 }
680 break;
681
682 default:
Greg Claytonf60f3752011-06-23 18:38:25 +0000683 break;
684 }
685 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686 if (m_value_str.empty())
687 {
688 const Value::ContextType context_type = m_value.GetContextType();
689
690 switch (context_type)
691 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000692 case Value::eContextTypeClangType:
693 case Value::eContextTypeLLDBType:
694 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000695 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000696 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 if (clang_type)
698 {
699 StreamString sstr;
Greg Clayton68ebae62011-04-28 20:55:26 +0000700 Format format = GetFormat();
701 if (format == eFormatDefault)
702 format = ClangASTType::GetFormat(clang_type);
Greg Clayton32c40852010-10-06 03:09:11 +0000703
704 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
705 clang_type, // The clang type to display
706 &sstr,
Greg Claytonf60f3752011-06-23 18:38:25 +0000707 m_last_format = format, // Format to display this type with
Greg Clayton32c40852010-10-06 03:09:11 +0000708 m_data, // Data to extract from
709 0, // Byte offset into "m_data"
710 GetByteSize(), // Byte size of item in "m_data"
711 GetBitfieldBitSize(), // Bitfield bit size
712 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 m_value_str.swap(sstr.GetString());
714 else
Greg Clayton007d5be2011-05-30 00:49:24 +0000715 {
716 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
717 m_data.GetByteSize(),
718 GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000719 m_value_str.clear();
Greg Clayton007d5be2011-05-30 00:49:24 +0000720 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 }
722 }
723 break;
724
Greg Clayton526e5af2010-11-13 03:52:47 +0000725 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 {
727 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
728 if (reg_info)
729 {
730 StreamString reg_sstr;
731 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
732 m_value_str.swap(reg_sstr.GetString());
733 }
734 }
735 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000736
737 default:
738 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 }
740 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000741
742 if (!m_value_did_change && m_old_value_valid)
743 {
744 // The value was gotten successfully, so we consider the
745 // value as changed if the value string differs
746 SetValueDidChange (m_old_value_str != m_value_str);
747 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 }
749 }
750 if (m_value_str.empty())
751 return NULL;
752 return m_value_str.c_str();
753}
754
Greg Clayton737b9322010-09-13 03:32:57 +0000755addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000756ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000757{
Jim Ingham78a685a2011-04-16 00:01:13 +0000758 if (!UpdateValueIfNeeded())
759 return LLDB_INVALID_ADDRESS;
760
Greg Clayton73b472d2010-10-27 03:32:59 +0000761 switch (m_value.GetValueType())
762 {
763 case Value::eValueTypeScalar:
764 if (scalar_is_load_address)
765 {
766 address_type = eAddressTypeLoad;
767 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
768 }
769 break;
770
771 case Value::eValueTypeLoadAddress:
772 case Value::eValueTypeFileAddress:
773 case Value::eValueTypeHostAddress:
774 {
775 address_type = m_value.GetValueAddressType ();
776 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
777 }
778 break;
779 }
780 address_type = eAddressTypeInvalid;
781 return LLDB_INVALID_ADDRESS;
782}
783
784addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000785ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000786{
787 lldb::addr_t address = LLDB_INVALID_ADDRESS;
788 address_type = eAddressTypeInvalid;
Jim Ingham78a685a2011-04-16 00:01:13 +0000789
790 if (!UpdateValueIfNeeded())
791 return address;
792
Greg Clayton73b472d2010-10-27 03:32:59 +0000793 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000794 {
795 case Value::eValueTypeScalar:
796 if (scalar_is_load_address)
797 {
798 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
799 address_type = eAddressTypeLoad;
800 }
801 break;
802
803 case Value::eValueTypeLoadAddress:
804 case Value::eValueTypeFileAddress:
805 case Value::eValueTypeHostAddress:
806 {
807 uint32_t data_offset = 0;
808 address = m_data.GetPointer(&data_offset);
809 address_type = m_value.GetValueAddressType();
810 if (address_type == eAddressTypeInvalid)
811 address_type = eAddressTypeLoad;
812 }
813 break;
814 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000815
816 if (m_pointers_point_to_load_addrs)
817 address_type = eAddressTypeLoad;
818
Greg Clayton737b9322010-09-13 03:32:57 +0000819 return address;
820}
821
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000822bool
Jim Ingham6035b672011-03-31 00:19:25 +0000823ValueObject::SetValueFromCString (const char *value_str)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000824{
825 // Make sure our value is up to date first so that our location and location
826 // type is valid.
Jim Ingham6035b672011-03-31 00:19:25 +0000827 if (!UpdateValueIfNeeded())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 return false;
829
830 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000831 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832
833 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000834 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000835 switch (encoding)
836 {
837 case eEncodingInvalid:
838 return false;
839
840 case eEncodingUint:
841 if (byte_size > sizeof(unsigned long long))
842 {
843 return false;
844 }
845 else
846 {
847 unsigned long long ull_val = strtoull(value_str, &end, 0);
848 if (end && *end != '\0')
849 return false;
850 m_value = ull_val;
851 // Limit the bytes in our m_data appropriately.
852 m_value.GetScalar().GetData (m_data, byte_size);
853 }
854 break;
855
856 case eEncodingSint:
857 if (byte_size > sizeof(long long))
858 {
859 return false;
860 }
861 else
862 {
863 long long sll_val = strtoll(value_str, &end, 0);
864 if (end && *end != '\0')
865 return false;
866 m_value = sll_val;
867 // Limit the bytes in our m_data appropriately.
868 m_value.GetScalar().GetData (m_data, byte_size);
869 }
870 break;
871
872 case eEncodingIEEE754:
873 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000875 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876 if (dst != NULL)
877 {
878 // We are decoding a float into host byte order below, so make
879 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000880 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
882 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000883 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000884 value_str,
885 dst,
886 byte_size);
887
888 if (converted_byte_size == byte_size)
889 {
890 }
891 }
892 }
893 break;
894
895 case eEncodingVector:
896 return false;
897
898 default:
899 return false;
900 }
901
902 // If we have made it here the value is in m_data and we should write it
903 // out to the target
904 return Write ();
905}
906
907bool
908ValueObject::Write ()
909{
910 // Clear the update ID so the next time we try and read the value
911 // we try and read it again.
Jim Ingham6035b672011-03-31 00:19:25 +0000912 m_update_point.SetNeedsUpdate();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000913
914 // TODO: when Value has a method to write a value back, call it from here.
915 return false;
916
917}
918
Jim Ingham5a369122010-09-28 01:25:32 +0000919lldb::LanguageType
920ValueObject::GetObjectRuntimeLanguage ()
921{
Greg Clayton73b472d2010-10-27 03:32:59 +0000922 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000923 if (opaque_qual_type == NULL)
924 return lldb::eLanguageTypeC;
925
926 // If the type is a reference, then resolve it to what it refers to first:
927 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
928 if (qual_type->isAnyPointerType())
929 {
930 if (qual_type->isObjCObjectPointerType())
931 return lldb::eLanguageTypeObjC;
932
933 clang::QualType pointee_type (qual_type->getPointeeType());
934 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
935 return lldb::eLanguageTypeC_plus_plus;
936 if (pointee_type->isObjCObjectOrInterfaceType())
937 return lldb::eLanguageTypeObjC;
938 if (pointee_type->isObjCClassType())
939 return lldb::eLanguageTypeObjC;
940 }
941 else
942 {
943 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
944 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000945 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000946 return lldb::eLanguageTypeC_plus_plus;
947 }
948
949 return lldb::eLanguageTypeC;
950}
951
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000952void
Jim Ingham58b59f92011-04-22 23:53:53 +0000953ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000954{
Jim Ingham58b59f92011-04-22 23:53:53 +0000955 m_synthetic_children[key] = valobj;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956}
957
958ValueObjectSP
959ValueObject::GetSyntheticChild (const ConstString &key) const
960{
961 ValueObjectSP synthetic_child_sp;
Jim Ingham58b59f92011-04-22 23:53:53 +0000962 std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000963 if (pos != m_synthetic_children.end())
Jim Ingham58b59f92011-04-22 23:53:53 +0000964 synthetic_child_sp = pos->second->GetSP();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000965 return synthetic_child_sp;
966}
967
968bool
969ValueObject::IsPointerType ()
970{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000971 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000972}
973
Jim Inghamb7603bb2011-03-18 00:05:18 +0000974bool
975ValueObject::IsIntegerType (bool &is_signed)
976{
977 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
978}
Greg Clayton73b472d2010-10-27 03:32:59 +0000979
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980bool
981ValueObject::IsPointerOrReferenceType ()
982{
Greg Clayton007d5be2011-05-30 00:49:24 +0000983 return ClangASTContext::IsPointerOrReferenceType (GetClangType());
984}
985
986bool
987ValueObject::IsPossibleCPlusPlusDynamicType ()
988{
989 return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000990}
991
992ValueObjectSP
993ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
994{
995 ValueObjectSP synthetic_child_sp;
996 if (IsPointerType ())
997 {
998 char index_str[64];
999 snprintf(index_str, sizeof(index_str), "[%i]", index);
1000 ConstString index_const_str(index_str);
1001 // Check if we have already created a synthetic array member in this
1002 // valid object. If we have we will re-use it.
1003 synthetic_child_sp = GetSyntheticChild (index_const_str);
1004 if (!synthetic_child_sp)
1005 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001006 ValueObject *synthetic_child;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001007 // We haven't made a synthetic array member for INDEX yet, so
1008 // lets make one and cache it for any future reference.
Jim Ingham58b59f92011-04-22 23:53:53 +00001009 synthetic_child = CreateChildAtIndex(0, true, index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010
1011 // Cache the value if we got one back...
Jim Ingham58b59f92011-04-22 23:53:53 +00001012 if (synthetic_child)
1013 {
1014 AddSyntheticChild(index_const_str, synthetic_child);
1015 synthetic_child_sp = synthetic_child->GetSP();
1016 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001017 }
1018 }
1019 return synthetic_child_sp;
1020}
Jim Ingham22777012010-09-23 02:01:19 +00001021
Jim Ingham78a685a2011-04-16 00:01:13 +00001022void
Jim Ingham2837b762011-05-04 03:43:18 +00001023ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
Jim Ingham22777012010-09-23 02:01:19 +00001024{
Jim Ingham2837b762011-05-04 03:43:18 +00001025 if (use_dynamic == lldb::eNoDynamicValues)
1026 return;
1027
Jim Ingham58b59f92011-04-22 23:53:53 +00001028 if (!m_dynamic_value && !IsDynamic())
Jim Ingham78a685a2011-04-16 00:01:13 +00001029 {
1030 Process *process = m_update_point.GetProcess();
1031 bool worth_having_dynamic_value = false;
Jim Ingham22777012010-09-23 02:01:19 +00001032
Jim Ingham78a685a2011-04-16 00:01:13 +00001033
1034 // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1035 // hard code this everywhere.
1036 lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1037 if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1038 {
1039 LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1040 if (runtime)
1041 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1042 }
1043 else
1044 {
1045 LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1046 if (cpp_runtime)
1047 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1048
1049 if (!worth_having_dynamic_value)
1050 {
1051 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1052 if (objc_runtime)
Jim Ingham2837b762011-05-04 03:43:18 +00001053 worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
Jim Ingham78a685a2011-04-16 00:01:13 +00001054 }
1055 }
1056
1057 if (worth_having_dynamic_value)
Jim Ingham2837b762011-05-04 03:43:18 +00001058 m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
Jim Ingham58b59f92011-04-22 23:53:53 +00001059
1060// if (worth_having_dynamic_value)
1061// printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1062
Jim Ingham78a685a2011-04-16 00:01:13 +00001063 }
1064}
1065
Jim Ingham58b59f92011-04-22 23:53:53 +00001066ValueObjectSP
Jim Ingham2837b762011-05-04 03:43:18 +00001067ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
Jim Ingham78a685a2011-04-16 00:01:13 +00001068{
Jim Ingham2837b762011-05-04 03:43:18 +00001069 if (use_dynamic == lldb::eNoDynamicValues)
1070 return ValueObjectSP();
1071
1072 if (!IsDynamic() && m_dynamic_value == NULL)
Jim Ingham78a685a2011-04-16 00:01:13 +00001073 {
Jim Ingham2837b762011-05-04 03:43:18 +00001074 CalculateDynamicValue(use_dynamic);
Jim Ingham78a685a2011-04-16 00:01:13 +00001075 }
Jim Ingham58b59f92011-04-22 23:53:53 +00001076 if (m_dynamic_value)
1077 return m_dynamic_value->GetSP();
1078 else
1079 return ValueObjectSP();
Jim Ingham22777012010-09-23 02:01:19 +00001080}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001081
Greg Claytone221f822011-01-21 01:59:00 +00001082bool
1083ValueObject::GetBaseClassPath (Stream &s)
1084{
1085 if (IsBaseClass())
1086 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001087 bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
Greg Claytone221f822011-01-21 01:59:00 +00001088 clang_type_t clang_type = GetClangType();
1089 std::string cxx_class_name;
1090 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1091 if (this_had_base_class)
1092 {
1093 if (parent_had_base_class)
1094 s.PutCString("::");
1095 s.PutCString(cxx_class_name.c_str());
1096 }
1097 return parent_had_base_class || this_had_base_class;
1098 }
1099 return false;
1100}
1101
1102
1103ValueObject *
1104ValueObject::GetNonBaseClassParent()
1105{
Jim Ingham78a685a2011-04-16 00:01:13 +00001106 if (GetParent())
Greg Claytone221f822011-01-21 01:59:00 +00001107 {
Jim Ingham78a685a2011-04-16 00:01:13 +00001108 if (GetParent()->IsBaseClass())
1109 return GetParent()->GetNonBaseClassParent();
Greg Claytone221f822011-01-21 01:59:00 +00001110 else
Jim Ingham78a685a2011-04-16 00:01:13 +00001111 return GetParent();
Greg Claytone221f822011-01-21 01:59:00 +00001112 }
1113 return NULL;
1114}
Greg Clayton1d3afba2010-10-05 00:00:42 +00001115
1116void
Greg Clayton6beaaa62011-01-17 03:46:26 +00001117ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001118{
Greg Claytone221f822011-01-21 01:59:00 +00001119 const bool is_deref_of_parent = IsDereferenceOfParent ();
1120
1121 if (is_deref_of_parent)
1122 s.PutCString("*(");
1123
Jim Ingham78a685a2011-04-16 00:01:13 +00001124 if (GetParent())
1125 GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +00001126
1127 if (!IsBaseClass())
1128 {
1129 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001130 {
Greg Claytone221f822011-01-21 01:59:00 +00001131 ValueObject *non_base_class_parent = GetNonBaseClassParent();
1132 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001133 {
Greg Claytone221f822011-01-21 01:59:00 +00001134 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1135 if (non_base_class_parent_clang_type)
1136 {
1137 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1138
1139 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1140 {
1141 s.PutCString("->");
1142 }
1143 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1144 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1145 {
1146 s.PutChar('.');
1147 }
1148 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001149 }
Greg Claytone221f822011-01-21 01:59:00 +00001150
1151 const char *name = GetName().GetCString();
1152 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001153 {
Greg Claytone221f822011-01-21 01:59:00 +00001154 if (qualify_cxx_base_classes)
1155 {
1156 if (GetBaseClassPath (s))
1157 s.PutCString("::");
1158 }
1159 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001160 }
1161 }
1162 }
1163
Greg Claytone221f822011-01-21 01:59:00 +00001164 if (is_deref_of_parent)
1165 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001166}
1167
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001168void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001169ValueObject::DumpValueObject
1170(
1171 Stream &s,
Greg Clayton1d3afba2010-10-05 00:00:42 +00001172 ValueObject *valobj,
1173 const char *root_valobj_name,
1174 uint32_t ptr_depth,
1175 uint32_t curr_depth,
1176 uint32_t max_depth,
1177 bool show_types,
1178 bool show_location,
1179 bool use_objc,
Jim Ingham2837b762011-05-04 03:43:18 +00001180 lldb::DynamicValueType use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001181 bool scope_already_checked,
1182 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001183)
1184{
Greg Clayton007d5be2011-05-30 00:49:24 +00001185 if (valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001186 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001187 bool update_success = valobj->UpdateValueIfNeeded ();
1188
1189 if (update_success && use_dynamic != lldb::eNoDynamicValues)
Jim Ingham78a685a2011-04-16 00:01:13 +00001190 {
Jim Ingham2837b762011-05-04 03:43:18 +00001191 ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
Jim Ingham78a685a2011-04-16 00:01:13 +00001192 if (dynamic_value)
1193 valobj = dynamic_value;
1194 }
1195
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001196 clang_type_t clang_type = valobj->GetClangType();
1197
Greg Clayton73b472d2010-10-27 03:32:59 +00001198 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001199 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001200 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1201 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001202
1203 const bool print_valobj = flat_output == false || has_value;
1204
1205 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001206 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001207 if (show_location)
1208 {
Jim Ingham6035b672011-03-31 00:19:25 +00001209 s.Printf("%s: ", valobj->GetLocationAsCString());
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001210 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001211
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001212 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001213
Greg Clayton7c8a9662010-11-02 01:50:16 +00001214 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001215 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001216 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001217
Greg Clayton1d3afba2010-10-05 00:00:42 +00001218
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001219 if (flat_output)
1220 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001221 // If we are showing types, also qualify the C++ base classes
1222 const bool qualify_cxx_base_classes = show_types;
1223 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001224 s.PutCString(" =");
1225 }
1226 else
1227 {
1228 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1229 s.Printf ("%s =", name_cstr);
1230 }
1231
Jim Ingham6035b672011-03-31 00:19:25 +00001232 if (!scope_already_checked && !valobj->IsInScope())
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001233 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001234 err_cstr = "out of scope";
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001235 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001236 }
1237
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001238 const char *val_cstr = NULL;
1239
1240 if (err_cstr == NULL)
1241 {
Jim Ingham6035b672011-03-31 00:19:25 +00001242 val_cstr = valobj->GetValueAsCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001243 err_cstr = valobj->GetError().AsCString();
1244 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001245
1246 if (err_cstr)
1247 {
Greg Clayton007d5be2011-05-30 00:49:24 +00001248 s.Printf (" <%s>\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001249 }
1250 else
1251 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001252 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001253 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001254 {
Jim Ingham6035b672011-03-31 00:19:25 +00001255 const char *sum_cstr = valobj->GetSummaryAsCString();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001256
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001257 if (val_cstr)
1258 s.Printf(" %s", val_cstr);
1259
1260 if (sum_cstr)
1261 s.Printf(" %s", sum_cstr);
1262
1263 if (use_objc)
1264 {
Jim Ingham6035b672011-03-31 00:19:25 +00001265 const char *object_desc = valobj->GetObjectDescription();
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001266 if (object_desc)
1267 s.Printf(" %s\n", object_desc);
1268 else
Sean Callanan672ad942010-10-23 00:18:49 +00001269 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001270 return;
1271 }
1272 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001273
1274 if (curr_depth < max_depth)
1275 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001276 // We will show children for all concrete types. We won't show
1277 // pointer contents unless a pointer depth has been specified.
1278 // We won't reference contents unless the reference is the
1279 // root object (depth of zero).
1280 bool print_children = true;
1281
1282 // Use a new temporary pointer depth in case we override the
1283 // current pointer depth below...
1284 uint32_t curr_ptr_depth = ptr_depth;
1285
1286 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1287 if (is_ptr || is_ref)
1288 {
1289 // We have a pointer or reference whose value is an address.
1290 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001291 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001292 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1293 print_children = false;
1294
1295 else if (is_ref && curr_depth == 0)
1296 {
1297 // If this is the root object (depth is zero) that we are showing
1298 // and it is a reference, and no pointer depth has been supplied
1299 // print out what it references. Don't do this at deeper depths
1300 // otherwise we can end up with infinite recursion...
1301 curr_ptr_depth = 1;
1302 }
1303
1304 if (curr_ptr_depth == 0)
1305 print_children = false;
1306 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001307
Greg Clayton73b472d2010-10-27 03:32:59 +00001308 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001309 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001310 const uint32_t num_children = valobj->GetNumChildren();
1311 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001312 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001313 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001314 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001315 if (print_valobj)
1316 s.EOL();
1317 }
1318 else
1319 {
1320 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001321 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001322 s.IndentMore();
1323 }
1324
1325 for (uint32_t idx=0; idx<num_children; ++idx)
1326 {
1327 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1328 if (child_sp.get())
1329 {
1330 DumpValueObject (s,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001331 child_sp.get(),
1332 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001333 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001334 curr_depth + 1,
1335 max_depth,
1336 show_types,
1337 show_location,
1338 false,
Jim Ingham78a685a2011-04-16 00:01:13 +00001339 use_dynamic,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001340 true,
1341 flat_output);
1342 }
1343 }
1344
1345 if (!flat_output)
1346 {
1347 s.IndentLess();
1348 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001349 }
1350 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001351 else if (has_children)
1352 {
1353 // Aggregate, no children...
1354 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001355 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001356 }
1357 else
1358 {
1359 if (print_valobj)
1360 s.EOL();
1361 }
1362
Greg Clayton1d3afba2010-10-05 00:00:42 +00001363 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001364 else
1365 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001366 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001367 }
1368 }
1369 else
1370 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001371 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001372 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001373 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001374 }
1375 }
1376 }
1377 }
1378}
1379
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001380
1381ValueObjectSP
Jim Ingham6035b672011-03-31 00:19:25 +00001382ValueObject::CreateConstantValue (const ConstString &name)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001383{
1384 ValueObjectSP valobj_sp;
1385
Jim Ingham6035b672011-03-31 00:19:25 +00001386 if (UpdateValueIfNeeded() && m_error.Success())
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001387 {
Jim Ingham6035b672011-03-31 00:19:25 +00001388 ExecutionContextScope *exe_scope = GetExecutionContextScope();
1389 if (exe_scope)
1390 {
1391 ExecutionContext exe_ctx;
1392 exe_scope->CalculateExecutionContext(exe_ctx);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001393
Jim Ingham6035b672011-03-31 00:19:25 +00001394 clang::ASTContext *ast = GetClangAST ();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001395
Jim Ingham6035b672011-03-31 00:19:25 +00001396 DataExtractor data;
1397 data.SetByteOrder (m_data.GetByteOrder());
1398 data.SetAddressByteSize(m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001399
Jim Ingham6035b672011-03-31 00:19:25 +00001400 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001401
Jim Ingham58b59f92011-04-22 23:53:53 +00001402 valobj_sp = ValueObjectConstResult::Create (exe_scope,
1403 ast,
1404 GetClangType(),
1405 name,
1406 data);
Jim Ingham6035b672011-03-31 00:19:25 +00001407 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001408 }
Jim Ingham6035b672011-03-31 00:19:25 +00001409
1410 if (!valobj_sp)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001411 {
Jim Ingham58b59f92011-04-22 23:53:53 +00001412 valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001413 }
1414 return valobj_sp;
1415}
1416
1417lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001418ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001419{
Jim Ingham58b59f92011-04-22 23:53:53 +00001420 if (m_deref_valobj)
1421 return m_deref_valobj->GetSP();
Jim Ingham78a685a2011-04-16 00:01:13 +00001422
Greg Clayton54979cd2010-12-15 05:08:08 +00001423 const bool is_pointer_type = IsPointerType();
1424 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001425 {
1426 bool omit_empty_base_classes = true;
1427
1428 std::string child_name_str;
1429 uint32_t child_byte_size = 0;
1430 int32_t child_byte_offset = 0;
1431 uint32_t child_bitfield_bit_size = 0;
1432 uint32_t child_bitfield_bit_offset = 0;
1433 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001434 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001435 const bool transparent_pointers = false;
1436 clang::ASTContext *clang_ast = GetClangAST();
1437 clang_type_t clang_type = GetClangType();
1438 clang_type_t child_clang_type;
1439 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1440 GetName().GetCString(),
1441 clang_type,
1442 0,
1443 transparent_pointers,
1444 omit_empty_base_classes,
1445 child_name_str,
1446 child_byte_size,
1447 child_byte_offset,
1448 child_bitfield_bit_size,
1449 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001450 child_is_base_class,
1451 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001452 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001453 {
1454 ConstString child_name;
1455 if (!child_name_str.empty())
1456 child_name.SetCString (child_name_str.c_str());
1457
Jim Ingham58b59f92011-04-22 23:53:53 +00001458 m_deref_valobj = new ValueObjectChild (*this,
1459 clang_ast,
1460 child_clang_type,
1461 child_name,
1462 child_byte_size,
1463 child_byte_offset,
1464 child_bitfield_bit_size,
1465 child_bitfield_bit_offset,
1466 child_is_base_class,
1467 child_is_deref_of_parent);
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001468 }
1469 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001470
Jim Ingham58b59f92011-04-22 23:53:53 +00001471 if (m_deref_valobj)
Greg Clayton54979cd2010-12-15 05:08:08 +00001472 {
1473 error.Clear();
Jim Ingham58b59f92011-04-22 23:53:53 +00001474 return m_deref_valobj->GetSP();
Greg Clayton54979cd2010-12-15 05:08:08 +00001475 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001476 else
1477 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001478 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001479 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001480
1481 if (is_pointer_type)
1482 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1483 else
1484 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Jim Ingham58b59f92011-04-22 23:53:53 +00001485 return ValueObjectSP();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001486 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001487}
1488
Jim Ingham78a685a2011-04-16 00:01:13 +00001489lldb::ValueObjectSP
Greg Clayton54979cd2010-12-15 05:08:08 +00001490ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001491{
Jim Ingham78a685a2011-04-16 00:01:13 +00001492 if (m_addr_of_valobj_sp)
1493 return m_addr_of_valobj_sp;
1494
Greg Claytone0d378b2011-03-24 21:19:54 +00001495 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001496 const bool scalar_is_load_address = false;
1497 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001498 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001499 if (addr != LLDB_INVALID_ADDRESS)
1500 {
1501 switch (address_type)
1502 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001503 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001504 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001505 {
1506 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001507 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001508 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1509 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001510 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001511
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001512 case eAddressTypeFile:
1513 case eAddressTypeLoad:
1514 case eAddressTypeHost:
1515 {
1516 clang::ASTContext *ast = GetClangAST();
1517 clang_type_t clang_type = GetClangType();
1518 if (ast && clang_type)
1519 {
1520 std::string name (1, '&');
1521 name.append (m_name.AsCString(""));
Jim Ingham58b59f92011-04-22 23:53:53 +00001522 m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1523 ast,
1524 ClangASTContext::CreatePointerType (ast, clang_type),
1525 ConstString (name.c_str()),
1526 addr,
1527 eAddressTypeInvalid,
1528 m_data.GetAddressByteSize());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001529 }
1530 }
1531 break;
1532 }
1533 }
Jim Ingham78a685a2011-04-16 00:01:13 +00001534 return m_addr_of_valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001535}
1536
Greg Claytonb2dcc362011-05-05 23:32:56 +00001537
1538lldb::ValueObjectSP
1539ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
1540{
1541 lldb::ValueObjectSP valobj_sp;
1542 AddressType address_type;
1543 const bool scalar_is_load_address = true;
1544 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1545
1546 if (ptr_value != LLDB_INVALID_ADDRESS)
1547 {
1548 Address ptr_addr (NULL, ptr_value);
1549
1550 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1551 name,
1552 ptr_addr,
1553 clang_ast_type);
1554 }
1555 return valobj_sp;
1556}
1557
1558lldb::ValueObjectSP
1559ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
1560{
1561 lldb::ValueObjectSP valobj_sp;
1562 AddressType address_type;
1563 const bool scalar_is_load_address = true;
1564 lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1565
1566 if (ptr_value != LLDB_INVALID_ADDRESS)
1567 {
1568 Address ptr_addr (NULL, ptr_value);
1569
1570 valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1571 name,
1572 ptr_addr,
1573 type_sp);
1574 }
1575 return valobj_sp;
1576}
1577
1578
Jim Ingham6035b672011-03-31 00:19:25 +00001579ValueObject::EvaluationPoint::EvaluationPoint () :
Stephen Wilson71c21d12011-04-11 19:41:40 +00001580 m_thread_id (LLDB_INVALID_UID),
1581 m_stop_id (0)
Jim Ingham6035b672011-03-31 00:19:25 +00001582{
1583}
1584
1585ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
Jim Ingham6035b672011-03-31 00:19:25 +00001586 m_needs_update (true),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001587 m_first_update (true),
1588 m_thread_id (LLDB_INVALID_UID),
1589 m_stop_id (0)
1590
Jim Ingham6035b672011-03-31 00:19:25 +00001591{
1592 ExecutionContext exe_ctx;
1593 ExecutionContextScope *computed_exe_scope = exe_scope; // If use_selected is true, we may find a better scope,
1594 // and if so we want to cache that not the original.
1595 if (exe_scope)
1596 exe_scope->CalculateExecutionContext(exe_ctx);
1597 if (exe_ctx.target != NULL)
1598 {
1599 m_target_sp = exe_ctx.target->GetSP();
1600
1601 if (exe_ctx.process == NULL)
1602 m_process_sp = exe_ctx.target->GetProcessSP();
1603 else
1604 m_process_sp = exe_ctx.process->GetSP();
1605
1606 if (m_process_sp != NULL)
1607 {
1608 m_stop_id = m_process_sp->GetStopID();
1609 Thread *thread = NULL;
1610
1611 if (exe_ctx.thread == NULL)
1612 {
1613 if (use_selected)
1614 {
1615 thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1616 if (thread)
1617 computed_exe_scope = thread;
1618 }
1619 }
1620 else
1621 thread = exe_ctx.thread;
1622
1623 if (thread != NULL)
1624 {
1625 m_thread_id = thread->GetIndexID();
1626 if (exe_ctx.frame == NULL)
1627 {
1628 if (use_selected)
1629 {
1630 StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1631 if (frame)
1632 {
1633 m_stack_id = frame->GetStackID();
1634 computed_exe_scope = frame;
1635 }
1636 }
1637 }
1638 else
1639 m_stack_id = exe_ctx.frame->GetStackID();
1640 }
1641 }
1642 }
1643 m_exe_scope = computed_exe_scope;
1644}
1645
1646ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1647 m_exe_scope (rhs.m_exe_scope),
Stephen Wilson71c21d12011-04-11 19:41:40 +00001648 m_needs_update(true),
1649 m_first_update(true),
Jim Ingham6035b672011-03-31 00:19:25 +00001650 m_target_sp (rhs.m_target_sp),
1651 m_process_sp (rhs.m_process_sp),
1652 m_thread_id (rhs.m_thread_id),
1653 m_stack_id (rhs.m_stack_id),
Jim Ingham6035b672011-03-31 00:19:25 +00001654 m_stop_id (0)
1655{
1656}
1657
1658ValueObject::EvaluationPoint::~EvaluationPoint ()
1659{
1660}
1661
1662ExecutionContextScope *
1663ValueObject::EvaluationPoint::GetExecutionContextScope ()
1664{
1665 // We have to update before giving out the scope, or we could be handing out stale pointers.
1666 SyncWithProcessState();
1667
1668 return m_exe_scope;
1669}
1670
1671// This function checks the EvaluationPoint against the current process state. If the current
1672// state matches the evaluation point, or the evaluation point is already invalid, then we return
1673// false, meaning "no change". If the current state is different, we update our state, and return
1674// true meaning "yes, change". If we did see a change, we also set m_needs_update to true, so
1675// future calls to NeedsUpdate will return true.
1676
1677bool
1678ValueObject::EvaluationPoint::SyncWithProcessState()
1679{
1680 // If we're already invalid, we don't need to do anything, and nothing has changed:
1681 if (m_stop_id == LLDB_INVALID_UID)
1682 {
1683 // Can't update with an invalid state.
1684 m_needs_update = false;
1685 return false;
1686 }
1687
1688 // If we don't have a process nothing can change.
1689 if (!m_process_sp)
1690 return false;
1691
1692 // If our stop id is the current stop ID, nothing has changed:
Jim Ingham78a685a2011-04-16 00:01:13 +00001693 uint32_t cur_stop_id = m_process_sp->GetStopID();
1694 if (m_stop_id == cur_stop_id)
Jim Ingham6035b672011-03-31 00:19:25 +00001695 return false;
1696
Jim Ingham78a685a2011-04-16 00:01:13 +00001697 // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1698 // In either case, we aren't going to be able to sync with the process state.
1699 if (cur_stop_id == 0)
1700 return false;
1701
1702 m_stop_id = cur_stop_id;
Jim Ingham6035b672011-03-31 00:19:25 +00001703 m_needs_update = true;
1704 m_exe_scope = m_process_sp.get();
1705
1706 // Something has changed, so we will return true. Now make sure the thread & frame still exist, and if either
1707 // doesn't, mark ourselves as invalid.
1708
1709 if (m_thread_id != LLDB_INVALID_THREAD_ID)
1710 {
1711 Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1712 if (our_thread == NULL)
1713 SetInvalid();
1714 else
1715 {
1716 m_exe_scope = our_thread;
1717
1718 if (m_stack_id.IsValid())
1719 {
1720 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1721 if (our_frame == NULL)
1722 SetInvalid();
1723 else
1724 m_exe_scope = our_frame;
1725 }
1726 }
1727 }
1728 return true;
1729}
1730
Jim Ingham61be0902011-05-02 18:13:59 +00001731void
1732ValueObject::EvaluationPoint::SetUpdated ()
1733{
1734 m_first_update = false;
1735 m_needs_update = false;
1736 if (m_process_sp)
1737 m_stop_id = m_process_sp->GetStopID();
1738}
1739
1740
Jim Ingham6035b672011-03-31 00:19:25 +00001741bool
1742ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1743{
1744 if (!IsValid())
1745 return false;
1746
1747 bool needs_update = false;
1748 m_exe_scope = NULL;
1749
1750 // The target has to be non-null, and the
1751 Target *target = exe_scope->CalculateTarget();
1752 if (target != NULL)
1753 {
1754 Target *old_target = m_target_sp.get();
1755 assert (target == old_target);
1756 Process *process = exe_scope->CalculateProcess();
1757 if (process != NULL)
1758 {
1759 // FOR NOW - assume you can't update variable objects across process boundaries.
1760 Process *old_process = m_process_sp.get();
1761 assert (process == old_process);
1762
1763 lldb::user_id_t stop_id = process->GetStopID();
1764 if (stop_id != m_stop_id)
1765 {
1766 needs_update = true;
1767 m_stop_id = stop_id;
1768 }
1769 // See if we're switching the thread or stack context. If no thread is given, this is
1770 // being evaluated in a global context.
1771 Thread *thread = exe_scope->CalculateThread();
1772 if (thread != NULL)
1773 {
1774 lldb::user_id_t new_thread_index = thread->GetIndexID();
1775 if (new_thread_index != m_thread_id)
1776 {
1777 needs_update = true;
1778 m_thread_id = new_thread_index;
1779 m_stack_id.Clear();
1780 }
1781
1782 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1783 if (new_frame != NULL)
1784 {
1785 if (new_frame->GetStackID() != m_stack_id)
1786 {
1787 needs_update = true;
1788 m_stack_id = new_frame->GetStackID();
1789 }
1790 }
1791 else
1792 {
1793 m_stack_id.Clear();
1794 needs_update = true;
1795 }
1796 }
1797 else
1798 {
1799 // If this had been given a thread, and now there is none, we should update.
1800 // Otherwise we don't have to do anything.
1801 if (m_thread_id != LLDB_INVALID_UID)
1802 {
1803 m_thread_id = LLDB_INVALID_UID;
1804 m_stack_id.Clear();
1805 needs_update = true;
1806 }
1807 }
1808 }
1809 else
1810 {
1811 // If there is no process, then we don't need to update anything.
1812 // But if we're switching from having a process to not, we should try to update.
1813 if (m_process_sp.get() != NULL)
1814 {
1815 needs_update = true;
1816 m_process_sp.reset();
1817 m_thread_id = LLDB_INVALID_UID;
1818 m_stack_id.Clear();
1819 }
1820 }
1821 }
1822 else
1823 {
1824 // If there's no target, nothing can change so we don't need to update anything.
1825 // But if we're switching from having a target to not, we should try to update.
1826 if (m_target_sp.get() != NULL)
1827 {
1828 needs_update = true;
1829 m_target_sp.reset();
1830 m_process_sp.reset();
1831 m_thread_id = LLDB_INVALID_UID;
1832 m_stack_id.Clear();
1833 }
1834 }
1835 if (!m_needs_update)
1836 m_needs_update = needs_update;
1837
1838 return needs_update;
1839}