blob: 948ace752d1a1c5068cf2cd0843fca6464c67d43 [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"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025#include "lldb/Core/ValueObjectList.h"
26
Greg Clayton7fb56d02011-02-01 01:31:41 +000027#include "lldb/Host/Endian.h"
28
Greg Claytone1a916a2010-07-21 22:12:05 +000029#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030#include "lldb/Symbol/ClangASTContext.h"
31#include "lldb/Symbol/Type.h"
32
Jim Ingham53c47f12010-09-10 23:12:17 +000033#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000034#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/Process.h"
36#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000037#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000039
40using namespace lldb;
41using namespace lldb_private;
42
43static lldb::user_id_t g_value_obj_uid = 0;
44
45//----------------------------------------------------------------------
46// ValueObject constructor
47//----------------------------------------------------------------------
Greg Clayton8f92f0a2010-10-14 22:52:14 +000048ValueObject::ValueObject (ValueObject *parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 UserID (++g_value_obj_uid), // Unique identifier for every value object
Greg Clayton8f92f0a2010-10-14 22:52:14 +000050 m_parent (parent),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051 m_update_id (0), // Value object lists always start at 1, value objects start at zero
52 m_name (),
53 m_data (),
54 m_value (),
55 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000056 m_value_str (),
57 m_old_value_str (),
58 m_location_str (),
59 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000060 m_object_desc_str (),
Greg Clayton288bdf92010-09-02 02:59:18 +000061 m_children (),
62 m_synthetic_children (),
Greg Clayton32c40852010-10-06 03:09:11 +000063 m_dynamic_value_sp (),
64 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000065 m_value_is_valid (false),
66 m_value_did_change (false),
67 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000068 m_old_value_valid (false),
Greg Claytone221f822011-01-21 01:59:00 +000069 m_pointers_point_to_load_addrs (false),
70 m_is_deref_of_parent (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000071{
72}
73
74//----------------------------------------------------------------------
75// Destructor
76//----------------------------------------------------------------------
77ValueObject::~ValueObject ()
78{
79}
80
81user_id_t
82ValueObject::GetUpdateID() const
83{
84 return m_update_id;
85}
86
87bool
88ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
89{
Greg Claytonb71f3842010-10-05 03:13:51 +000090 // If this is a constant value, then our success is predicated on whether
91 // we have an error or not
92 if (GetIsConstant())
93 return m_error.Success();
94
Chris Lattner30fdc8d2010-06-08 16:52:24 +000095 if (exe_scope)
96 {
97 Process *process = exe_scope->CalculateProcess();
98 if (process)
99 {
100 const user_id_t stop_id = process->GetStopID();
101 if (m_update_id != stop_id)
102 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000103 bool first_update = m_update_id == 0;
Greg Clayton73b953b2010-08-28 00:08:07 +0000104 // Save the old value using swap to avoid a string copy which
105 // also will clear our m_value_str
Greg Clayton288bdf92010-09-02 02:59:18 +0000106 if (m_value_str.empty())
107 {
108 m_old_value_valid = false;
109 }
110 else
111 {
112 m_old_value_valid = true;
113 m_old_value_str.swap (m_value_str);
114 m_value_str.clear();
115 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116 m_location_str.clear();
117 m_summary_str.clear();
Jim Ingham53c47f12010-09-10 23:12:17 +0000118 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119
Greg Clayton73b953b2010-08-28 00:08:07 +0000120 const bool value_was_valid = GetValueIsValid();
121 SetValueDidChange (false);
122
123 m_error.Clear();
124
125 // Call the pure virtual function to update the value
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000126 UpdateValue (exe_scope);
Greg Clayton73b953b2010-08-28 00:08:07 +0000127
128 // Update the fact that we tried to update the value for this
Greg Claytoned8a7052010-09-18 03:37:20 +0000129 // value object whether or not we succeed
Greg Clayton73b953b2010-08-28 00:08:07 +0000130 m_update_id = stop_id;
131 bool success = m_error.Success();
132 SetValueIsValid (success);
Greg Clayton288bdf92010-09-02 02:59:18 +0000133
134 if (first_update)
135 SetValueDidChange (false);
136 else if (!m_value_did_change && success == false)
Greg Clayton73b953b2010-08-28 00:08:07 +0000137 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000138 // The value wasn't gotten successfully, so we mark this
139 // as changed if the value used to be valid and now isn't
140 SetValueDidChange (value_was_valid);
Greg Clayton73b953b2010-08-28 00:08:07 +0000141 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000142 }
143 }
144 }
145 return m_error.Success();
146}
147
148const DataExtractor &
149ValueObject::GetDataExtractor () const
150{
151 return m_data;
152}
153
154DataExtractor &
155ValueObject::GetDataExtractor ()
156{
157 return m_data;
158}
159
160const Error &
161ValueObject::GetError() const
162{
163 return m_error;
164}
165
166const ConstString &
167ValueObject::GetName() const
168{
169 return m_name;
170}
171
172const char *
173ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
174{
175 if (UpdateValueIfNeeded(exe_scope))
176 {
177 if (m_location_str.empty())
178 {
179 StreamString sstr;
180
181 switch (m_value.GetValueType())
182 {
183 default:
184 break;
185
186 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000187 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000188 {
189 RegisterInfo *reg_info = m_value.GetRegisterInfo();
190 if (reg_info)
191 {
192 if (reg_info->name)
193 m_location_str = reg_info->name;
194 else if (reg_info->alt_name)
195 m_location_str = reg_info->alt_name;
196 break;
197 }
198 }
199 m_location_str = "scalar";
200 break;
201
202 case Value::eValueTypeLoadAddress:
203 case Value::eValueTypeFileAddress:
204 case Value::eValueTypeHostAddress:
205 {
206 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
207 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
208 m_location_str.swap(sstr.GetString());
209 }
210 break;
211 }
212 }
213 }
214 return m_location_str.c_str();
215}
216
217Value &
218ValueObject::GetValue()
219{
220 return m_value;
221}
222
223const Value &
224ValueObject::GetValue() const
225{
226 return m_value;
227}
228
229bool
Greg Clayton8f343b02010-11-04 01:54:29 +0000230ValueObject::ResolveValue (ExecutionContextScope *exe_scope, Scalar &scalar)
231{
232 ExecutionContext exe_ctx;
233 exe_scope->CalculateExecutionContext(exe_ctx);
234 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
235 return scalar.IsValid();
236}
237
238bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000239ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240{
Greg Clayton288bdf92010-09-02 02:59:18 +0000241 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
244
245void
246ValueObject::SetValueIsValid (bool b)
247{
Greg Clayton288bdf92010-09-02 02:59:18 +0000248 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000249}
250
251bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000252ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253{
Greg Clayton288bdf92010-09-02 02:59:18 +0000254 GetValueAsCString (exe_scope);
255 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000256}
257
258void
259ValueObject::SetValueDidChange (bool value_changed)
260{
Greg Clayton288bdf92010-09-02 02:59:18 +0000261 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000262}
263
264ValueObjectSP
265ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
266{
267 ValueObjectSP child_sp;
268 if (idx < GetNumChildren())
269 {
270 // Check if we have already made the child value object?
271 if (can_create && m_children[idx].get() == NULL)
272 {
273 // No we haven't created the child at this index, so lets have our
274 // subclass do it and cache the result for quick future access.
275 m_children[idx] = CreateChildAtIndex (idx, false, 0);
276 }
277
278 child_sp = m_children[idx];
279 }
280 return child_sp;
281}
282
283uint32_t
284ValueObject::GetIndexOfChildWithName (const ConstString &name)
285{
286 bool omit_empty_base_classes = true;
287 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000288 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000289 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000290 omit_empty_base_classes);
291}
292
293ValueObjectSP
294ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
295{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000296 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000297 // classes (which really aren't part of the expression path), so we
298 // need a vector of indexes that can get us down to the correct child
299 std::vector<uint32_t> child_indexes;
300 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton1be10fc2010-09-29 01:12:09 +0000301 void *clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302 bool omit_empty_base_classes = true;
303 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
304 clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000305 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000306 omit_empty_base_classes,
307 child_indexes);
308 ValueObjectSP child_sp;
309 if (num_child_indexes > 0)
310 {
311 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
312 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
313
314 child_sp = GetChildAtIndex(*pos, can_create);
315 for (++pos; pos != end; ++pos)
316 {
317 if (child_sp)
318 {
319 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
320 child_sp = new_child_sp;
321 }
322 else
323 {
324 child_sp.reset();
325 }
326
327 }
328 }
329 return child_sp;
330}
331
332
333uint32_t
334ValueObject::GetNumChildren ()
335{
Greg Clayton288bdf92010-09-02 02:59:18 +0000336 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000337 {
338 SetNumChildren (CalculateNumChildren());
339 }
340 return m_children.size();
341}
342void
343ValueObject::SetNumChildren (uint32_t num_children)
344{
Greg Clayton288bdf92010-09-02 02:59:18 +0000345 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000346 m_children.resize(num_children);
347}
348
349void
350ValueObject::SetName (const char *name)
351{
352 m_name.SetCString(name);
353}
354
355void
356ValueObject::SetName (const ConstString &name)
357{
358 m_name = name;
359}
360
361ValueObjectSP
362ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
363{
364 ValueObjectSP valobj_sp;
365 bool omit_empty_base_classes = true;
366
367 std::string child_name_str;
368 uint32_t child_byte_size = 0;
369 int32_t child_byte_offset = 0;
370 uint32_t child_bitfield_bit_size = 0;
371 uint32_t child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000372 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +0000373 bool child_is_deref_of_parent = false;
374
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 const bool transparent_pointers = synthetic_array_member == false;
376 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton73b472d2010-10-27 03:32:59 +0000377 clang_type_t clang_type = GetClangType();
378 clang_type_t child_clang_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000380 GetName().GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381 clang_type,
382 idx,
383 transparent_pointers,
384 omit_empty_base_classes,
385 child_name_str,
386 child_byte_size,
387 child_byte_offset,
388 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000389 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +0000390 child_is_base_class,
391 child_is_deref_of_parent);
Greg Clayton97a43712011-01-08 22:26:47 +0000392 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 {
394 if (synthetic_index)
395 child_byte_offset += child_byte_size * synthetic_index;
396
397 ConstString child_name;
398 if (!child_name_str.empty())
399 child_name.SetCString (child_name_str.c_str());
400
401 valobj_sp.reset (new ValueObjectChild (this,
402 clang_ast,
403 child_clang_type,
404 child_name,
405 child_byte_size,
406 child_byte_offset,
407 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000408 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +0000409 child_is_base_class,
410 child_is_deref_of_parent));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000411 if (m_pointers_point_to_load_addrs)
412 valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413 }
414 return valobj_sp;
415}
416
417const char *
418ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
419{
420 if (UpdateValueIfNeeded (exe_scope))
421 {
422 if (m_summary_str.empty())
423 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000424 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
426 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000427 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000428 {
Greg Clayton737b9322010-09-13 03:32:57 +0000429 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000430 clang_type_t elem_or_pointee_clang_type;
431 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000432 GetClangAST(),
433 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000434
Greg Clayton73b472d2010-10-27 03:32:59 +0000435 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
436 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 {
Greg Clayton737b9322010-09-13 03:32:57 +0000438 Process *process = exe_scope->CalculateProcess();
439 if (process != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000441 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
Greg Claytone0d378b2011-03-24 21:19:54 +0000442 AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000443
Greg Clayton73b472d2010-10-27 03:32:59 +0000444 size_t cstr_len = 0;
445 if (type_flags.Test (ClangASTContext::eTypeIsArray))
446 {
447 // We have an array
448 cstr_len = ClangASTContext::GetArraySize (clang_type);
449 cstr_address = GetAddressOf (cstr_address_type, true);
450 }
451 else
452 {
453 // We have a pointer
454 cstr_address = GetPointerValue (cstr_address_type, true);
455 }
Greg Clayton737b9322010-09-13 03:32:57 +0000456 if (cstr_address != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 {
Greg Clayton737b9322010-09-13 03:32:57 +0000458 DataExtractor data;
459 size_t bytes_read = 0;
460 std::vector<char> data_buffer;
Greg Clayton737b9322010-09-13 03:32:57 +0000461 Error error;
Greg Clayton73b472d2010-10-27 03:32:59 +0000462 if (cstr_len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000464 data_buffer.resize(cstr_len);
Greg Clayton7fb56d02011-02-01 01:31:41 +0000465 data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
Greg Clayton73b472d2010-10-27 03:32:59 +0000466 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
Greg Clayton737b9322010-09-13 03:32:57 +0000467 if (bytes_read > 0)
468 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 sstr << '"';
Greg Clayton23af0d82011-01-17 05:51:02 +0000470 data.Dump (&sstr,
471 0, // Start offset in "data"
472 eFormatChar, // Print as characters
473 1, // Size of item (1 byte for a char!)
474 bytes_read, // How many bytes to print?
475 UINT32_MAX, // num per line
476 LLDB_INVALID_ADDRESS,// base address
477 0, // bitfield bit size
478 0); // bitfield bit offset
Greg Clayton737b9322010-09-13 03:32:57 +0000479 sstr << '"';
480 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 }
Greg Clayton737b9322010-09-13 03:32:57 +0000482 else
483 {
484 const size_t k_max_buf_size = 256;
485 data_buffer.resize (k_max_buf_size + 1);
486 // NULL terminate in case we don't get the entire C string
487 data_buffer.back() = '\0';
Greg Clayton23af0d82011-01-17 05:51:02 +0000488
489 sstr << '"';
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490
Greg Clayton7fb56d02011-02-01 01:31:41 +0000491 data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
Greg Clayton737b9322010-09-13 03:32:57 +0000492 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
493 {
494 size_t len = strlen(&data_buffer.front());
495 if (len == 0)
496 break;
497 if (len > bytes_read)
498 len = bytes_read;
Greg Clayton737b9322010-09-13 03:32:57 +0000499
Greg Clayton23af0d82011-01-17 05:51:02 +0000500 data.Dump (&sstr,
501 0, // Start offset in "data"
502 eFormatChar, // Print as characters
503 1, // Size of item (1 byte for a char!)
504 len, // How many bytes to print?
505 UINT32_MAX, // num per line
506 LLDB_INVALID_ADDRESS,// base address
507 0, // bitfield bit size
508 0); // bitfield bit offset
509
Greg Clayton737b9322010-09-13 03:32:57 +0000510 if (len < k_max_buf_size)
511 break;
Greg Clayton23af0d82011-01-17 05:51:02 +0000512 cstr_address += k_max_buf_size;
Greg Clayton737b9322010-09-13 03:32:57 +0000513 }
Greg Clayton23af0d82011-01-17 05:51:02 +0000514 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000515 }
516 }
517 }
518
519 if (sstr.GetSize() > 0)
520 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
521 }
522 else if (ClangASTContext::IsFunctionPointerType (clang_type))
523 {
Greg Claytone0d378b2011-03-24 21:19:54 +0000524 AddressType func_ptr_address_type = eAddressTypeInvalid;
Greg Clayton737b9322010-09-13 03:32:57 +0000525 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
526
527 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
528 {
529 switch (func_ptr_address_type)
530 {
531 case eAddressTypeInvalid:
532 case eAddressTypeFile:
533 break;
534
535 case eAddressTypeLoad:
536 {
537 Address so_addr;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000538 Target *target = exe_scope->CalculateTarget();
539 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Clayton737b9322010-09-13 03:32:57 +0000540 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000541 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Greg Clayton737b9322010-09-13 03:32:57 +0000542 {
543 so_addr.Dump (&sstr,
544 exe_scope,
545 Address::DumpStyleResolvedDescription,
546 Address::DumpStyleSectionNameOffset);
547 }
548 }
549 }
550 break;
551
552 case eAddressTypeHost:
553 break;
554 }
555 }
556 if (sstr.GetSize() > 0)
557 {
558 m_summary_str.assign (1, '(');
559 m_summary_str.append (sstr.GetData(), sstr.GetSize());
560 m_summary_str.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561 }
562 }
563 }
564 }
565 }
566 if (m_summary_str.empty())
567 return NULL;
568 return m_summary_str.c_str();
569}
570
Jim Ingham53c47f12010-09-10 23:12:17 +0000571const char *
572ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
573{
574 if (!m_object_desc_str.empty())
575 return m_object_desc_str.c_str();
576
Jim Ingham53c47f12010-09-10 23:12:17 +0000577 if (!GetValueIsValid())
578 return NULL;
579
580 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000581 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000582 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000583
Jim Ingham53c47f12010-09-10 23:12:17 +0000584 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000585
586 lldb::LanguageType language = GetObjectRuntimeLanguage();
587 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
588
Jim Inghama2cf2632010-12-23 02:29:54 +0000589 if (runtime == NULL)
590 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000591 // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
Jim Inghama2cf2632010-12-23 02:29:54 +0000592 clang_type_t opaque_qual_type = GetClangType();
593 if (opaque_qual_type != NULL)
594 {
Jim Inghamb7603bb2011-03-18 00:05:18 +0000595 bool is_signed;
596 if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
597 || ClangASTContext::IsPointerType (opaque_qual_type))
598 {
Jim Inghama2cf2632010-12-23 02:29:54 +0000599 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
Jim Inghamb7603bb2011-03-18 00:05:18 +0000600 }
Jim Inghama2cf2632010-12-23 02:29:54 +0000601 }
602 }
603
Jim Ingham5a369122010-09-28 01:25:32 +0000604 if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
Jim Ingham53c47f12010-09-10 23:12:17 +0000605 {
606 m_object_desc_str.append (s.GetData());
607 }
Sean Callanan672ad942010-10-23 00:18:49 +0000608
609 if (m_object_desc_str.empty())
610 return NULL;
611 else
612 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000613}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
615const char *
616ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
617{
618 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000619 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000620 {
621 if (UpdateValueIfNeeded(exe_scope))
622 {
623 if (m_value_str.empty())
624 {
625 const Value::ContextType context_type = m_value.GetContextType();
626
627 switch (context_type)
628 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000629 case Value::eContextTypeClangType:
630 case Value::eContextTypeLLDBType:
631 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000633 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000634 if (clang_type)
635 {
636 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000637 if (m_format == eFormatDefault)
638 m_format = ClangASTType::GetFormat(clang_type);
639
640 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
641 clang_type, // The clang type to display
642 &sstr,
643 m_format, // Format to display this type with
644 m_data, // Data to extract from
645 0, // Byte offset into "m_data"
646 GetByteSize(), // Byte size of item in "m_data"
647 GetBitfieldBitSize(), // Bitfield bit size
648 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000649 m_value_str.swap(sstr.GetString());
650 else
651 m_value_str.clear();
652 }
653 }
654 break;
655
Greg Clayton526e5af2010-11-13 03:52:47 +0000656 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 {
658 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
659 if (reg_info)
660 {
661 StreamString reg_sstr;
662 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
663 m_value_str.swap(reg_sstr.GetString());
664 }
665 }
666 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000667
668 default:
669 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670 }
671 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000672
673 if (!m_value_did_change && m_old_value_valid)
674 {
675 // The value was gotten successfully, so we consider the
676 // value as changed if the value string differs
677 SetValueDidChange (m_old_value_str != m_value_str);
678 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 }
680 }
681 if (m_value_str.empty())
682 return NULL;
683 return m_value_str.c_str();
684}
685
Greg Clayton737b9322010-09-13 03:32:57 +0000686addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000687ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton73b472d2010-10-27 03:32:59 +0000688{
689 switch (m_value.GetValueType())
690 {
691 case Value::eValueTypeScalar:
692 if (scalar_is_load_address)
693 {
694 address_type = eAddressTypeLoad;
695 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
696 }
697 break;
698
699 case Value::eValueTypeLoadAddress:
700 case Value::eValueTypeFileAddress:
701 case Value::eValueTypeHostAddress:
702 {
703 address_type = m_value.GetValueAddressType ();
704 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
705 }
706 break;
707 }
708 address_type = eAddressTypeInvalid;
709 return LLDB_INVALID_ADDRESS;
710}
711
712addr_t
Greg Claytone0d378b2011-03-24 21:19:54 +0000713ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
Greg Clayton737b9322010-09-13 03:32:57 +0000714{
715 lldb::addr_t address = LLDB_INVALID_ADDRESS;
716 address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +0000717 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000718 {
719 case Value::eValueTypeScalar:
720 if (scalar_is_load_address)
721 {
722 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
723 address_type = eAddressTypeLoad;
724 }
725 break;
726
727 case Value::eValueTypeLoadAddress:
728 case Value::eValueTypeFileAddress:
729 case Value::eValueTypeHostAddress:
730 {
731 uint32_t data_offset = 0;
732 address = m_data.GetPointer(&data_offset);
733 address_type = m_value.GetValueAddressType();
734 if (address_type == eAddressTypeInvalid)
735 address_type = eAddressTypeLoad;
736 }
737 break;
738 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000739
740 if (m_pointers_point_to_load_addrs)
741 address_type = eAddressTypeLoad;
742
Greg Clayton737b9322010-09-13 03:32:57 +0000743 return address;
744}
745
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000746bool
747ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
748{
749 // Make sure our value is up to date first so that our location and location
750 // type is valid.
751 if (!UpdateValueIfNeeded(exe_scope))
752 return false;
753
754 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000755 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756
757 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000758 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000759 switch (encoding)
760 {
761 case eEncodingInvalid:
762 return false;
763
764 case eEncodingUint:
765 if (byte_size > sizeof(unsigned long long))
766 {
767 return false;
768 }
769 else
770 {
771 unsigned long long ull_val = strtoull(value_str, &end, 0);
772 if (end && *end != '\0')
773 return false;
774 m_value = ull_val;
775 // Limit the bytes in our m_data appropriately.
776 m_value.GetScalar().GetData (m_data, byte_size);
777 }
778 break;
779
780 case eEncodingSint:
781 if (byte_size > sizeof(long long))
782 {
783 return false;
784 }
785 else
786 {
787 long long sll_val = strtoll(value_str, &end, 0);
788 if (end && *end != '\0')
789 return false;
790 m_value = sll_val;
791 // Limit the bytes in our m_data appropriately.
792 m_value.GetScalar().GetData (m_data, byte_size);
793 }
794 break;
795
796 case eEncodingIEEE754:
797 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000798 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000799 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 if (dst != NULL)
801 {
802 // We are decoding a float into host byte order below, so make
803 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000804 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
806 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000807 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000808 value_str,
809 dst,
810 byte_size);
811
812 if (converted_byte_size == byte_size)
813 {
814 }
815 }
816 }
817 break;
818
819 case eEncodingVector:
820 return false;
821
822 default:
823 return false;
824 }
825
826 // If we have made it here the value is in m_data and we should write it
827 // out to the target
828 return Write ();
829}
830
831bool
832ValueObject::Write ()
833{
834 // Clear the update ID so the next time we try and read the value
835 // we try and read it again.
836 m_update_id = 0;
837
838 // TODO: when Value has a method to write a value back, call it from here.
839 return false;
840
841}
842
Jim Ingham5a369122010-09-28 01:25:32 +0000843lldb::LanguageType
844ValueObject::GetObjectRuntimeLanguage ()
845{
Greg Clayton73b472d2010-10-27 03:32:59 +0000846 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000847 if (opaque_qual_type == NULL)
848 return lldb::eLanguageTypeC;
849
850 // If the type is a reference, then resolve it to what it refers to first:
851 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
852 if (qual_type->isAnyPointerType())
853 {
854 if (qual_type->isObjCObjectPointerType())
855 return lldb::eLanguageTypeObjC;
856
857 clang::QualType pointee_type (qual_type->getPointeeType());
858 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
859 return lldb::eLanguageTypeC_plus_plus;
860 if (pointee_type->isObjCObjectOrInterfaceType())
861 return lldb::eLanguageTypeObjC;
862 if (pointee_type->isObjCClassType())
863 return lldb::eLanguageTypeObjC;
864 }
865 else
866 {
867 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
868 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000869 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000870 return lldb::eLanguageTypeC_plus_plus;
871 }
872
873 return lldb::eLanguageTypeC;
874}
875
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876void
877ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
878{
879 m_synthetic_children[key] = valobj_sp;
880}
881
882ValueObjectSP
883ValueObject::GetSyntheticChild (const ConstString &key) const
884{
885 ValueObjectSP synthetic_child_sp;
886 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
887 if (pos != m_synthetic_children.end())
888 synthetic_child_sp = pos->second;
889 return synthetic_child_sp;
890}
891
892bool
893ValueObject::IsPointerType ()
894{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000895 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896}
897
Jim Inghamb7603bb2011-03-18 00:05:18 +0000898bool
899ValueObject::IsIntegerType (bool &is_signed)
900{
901 return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
902}
Greg Clayton73b472d2010-10-27 03:32:59 +0000903
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000904bool
905ValueObject::IsPointerOrReferenceType ()
906{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000907 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908}
909
910ValueObjectSP
911ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
912{
913 ValueObjectSP synthetic_child_sp;
914 if (IsPointerType ())
915 {
916 char index_str[64];
917 snprintf(index_str, sizeof(index_str), "[%i]", index);
918 ConstString index_const_str(index_str);
919 // Check if we have already created a synthetic array member in this
920 // valid object. If we have we will re-use it.
921 synthetic_child_sp = GetSyntheticChild (index_const_str);
922 if (!synthetic_child_sp)
923 {
924 // We haven't made a synthetic array member for INDEX yet, so
925 // lets make one and cache it for any future reference.
926 synthetic_child_sp = CreateChildAtIndex(0, true, index);
927
928 // Cache the value if we got one back...
929 if (synthetic_child_sp)
930 AddSyntheticChild(index_const_str, synthetic_child_sp);
931 }
932 }
933 return synthetic_child_sp;
934}
Jim Ingham22777012010-09-23 02:01:19 +0000935
936bool
937ValueObject::SetDynamicValue ()
938{
939 if (!IsPointerOrReferenceType())
940 return false;
941
942 // Check that the runtime class is correct for determining the most specific class.
943 // If it is a C++ class, see if it is dynamic:
Jim Ingham5a369122010-09-28 01:25:32 +0000944
Jim Ingham22777012010-09-23 02:01:19 +0000945 return true;
946}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000947
Greg Claytone221f822011-01-21 01:59:00 +0000948bool
949ValueObject::GetBaseClassPath (Stream &s)
950{
951 if (IsBaseClass())
952 {
953 bool parent_had_base_class = m_parent && m_parent->GetBaseClassPath (s);
954 clang_type_t clang_type = GetClangType();
955 std::string cxx_class_name;
956 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
957 if (this_had_base_class)
958 {
959 if (parent_had_base_class)
960 s.PutCString("::");
961 s.PutCString(cxx_class_name.c_str());
962 }
963 return parent_had_base_class || this_had_base_class;
964 }
965 return false;
966}
967
968
969ValueObject *
970ValueObject::GetNonBaseClassParent()
971{
972 if (m_parent)
973 {
974 if (m_parent->IsBaseClass())
975 return m_parent->GetNonBaseClassParent();
976 else
977 return m_parent;
978 }
979 return NULL;
980}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000981
982void
Greg Clayton6beaaa62011-01-17 03:46:26 +0000983ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000984{
Greg Claytone221f822011-01-21 01:59:00 +0000985 const bool is_deref_of_parent = IsDereferenceOfParent ();
986
987 if (is_deref_of_parent)
988 s.PutCString("*(");
989
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000990 if (m_parent)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000991 m_parent->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +0000992
993 if (!IsBaseClass())
994 {
995 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000996 {
Greg Claytone221f822011-01-21 01:59:00 +0000997 ValueObject *non_base_class_parent = GetNonBaseClassParent();
998 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000999 {
Greg Claytone221f822011-01-21 01:59:00 +00001000 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1001 if (non_base_class_parent_clang_type)
1002 {
1003 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1004
1005 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1006 {
1007 s.PutCString("->");
1008 }
1009 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1010 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1011 {
1012 s.PutChar('.');
1013 }
1014 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001015 }
Greg Claytone221f822011-01-21 01:59:00 +00001016
1017 const char *name = GetName().GetCString();
1018 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001019 {
Greg Claytone221f822011-01-21 01:59:00 +00001020 if (qualify_cxx_base_classes)
1021 {
1022 if (GetBaseClassPath (s))
1023 s.PutCString("::");
1024 }
1025 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001026 }
1027 }
1028 }
1029
Greg Claytone221f822011-01-21 01:59:00 +00001030 if (is_deref_of_parent)
1031 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001032}
1033
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001034void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001035ValueObject::DumpValueObject
1036(
1037 Stream &s,
1038 ExecutionContextScope *exe_scope,
1039 ValueObject *valobj,
1040 const char *root_valobj_name,
1041 uint32_t ptr_depth,
1042 uint32_t curr_depth,
1043 uint32_t max_depth,
1044 bool show_types,
1045 bool show_location,
1046 bool use_objc,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001047 bool scope_already_checked,
1048 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001049)
1050{
1051 if (valobj)
1052 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001053 clang_type_t clang_type = valobj->GetClangType();
1054
Greg Clayton73b472d2010-10-27 03:32:59 +00001055 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001056 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001057 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1058 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001059
1060 const bool print_valobj = flat_output == false || has_value;
1061
1062 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001063 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001064 if (show_location)
1065 {
1066 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1067 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001068
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001069 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001070
Greg Clayton7c8a9662010-11-02 01:50:16 +00001071 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001072 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001073 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001074
Greg Clayton1d3afba2010-10-05 00:00:42 +00001075
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001076 if (flat_output)
1077 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001078 // If we are showing types, also qualify the C++ base classes
1079 const bool qualify_cxx_base_classes = show_types;
1080 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001081 s.PutCString(" =");
1082 }
1083 else
1084 {
1085 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1086 s.Printf ("%s =", name_cstr);
1087 }
1088
1089 if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1090 {
1091 err_cstr = "error: out of scope";
1092 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001093 }
1094
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001095 const char *val_cstr = NULL;
1096
1097 if (err_cstr == NULL)
1098 {
1099 val_cstr = valobj->GetValueAsCString(exe_scope);
1100 err_cstr = valobj->GetError().AsCString();
1101 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001102
1103 if (err_cstr)
1104 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001105 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001106 }
1107 else
1108 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001109 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001110 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001111 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001112 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001113
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001114 if (val_cstr)
1115 s.Printf(" %s", val_cstr);
1116
1117 if (sum_cstr)
1118 s.Printf(" %s", sum_cstr);
1119
1120 if (use_objc)
1121 {
1122 const char *object_desc = valobj->GetObjectDescription(exe_scope);
1123 if (object_desc)
1124 s.Printf(" %s\n", object_desc);
1125 else
Sean Callanan672ad942010-10-23 00:18:49 +00001126 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001127 return;
1128 }
1129 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001130
1131 if (curr_depth < max_depth)
1132 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001133 // We will show children for all concrete types. We won't show
1134 // pointer contents unless a pointer depth has been specified.
1135 // We won't reference contents unless the reference is the
1136 // root object (depth of zero).
1137 bool print_children = true;
1138
1139 // Use a new temporary pointer depth in case we override the
1140 // current pointer depth below...
1141 uint32_t curr_ptr_depth = ptr_depth;
1142
1143 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1144 if (is_ptr || is_ref)
1145 {
1146 // We have a pointer or reference whose value is an address.
1147 // Make sure that address is not NULL
Greg Claytone0d378b2011-03-24 21:19:54 +00001148 AddressType ptr_address_type;
Greg Clayton73b472d2010-10-27 03:32:59 +00001149 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1150 print_children = false;
1151
1152 else if (is_ref && curr_depth == 0)
1153 {
1154 // If this is the root object (depth is zero) that we are showing
1155 // and it is a reference, and no pointer depth has been supplied
1156 // print out what it references. Don't do this at deeper depths
1157 // otherwise we can end up with infinite recursion...
1158 curr_ptr_depth = 1;
1159 }
1160
1161 if (curr_ptr_depth == 0)
1162 print_children = false;
1163 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001164
Greg Clayton73b472d2010-10-27 03:32:59 +00001165 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001166 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001167 const uint32_t num_children = valobj->GetNumChildren();
1168 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001169 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001170 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001171 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001172 if (print_valobj)
1173 s.EOL();
1174 }
1175 else
1176 {
1177 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001178 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001179 s.IndentMore();
1180 }
1181
1182 for (uint32_t idx=0; idx<num_children; ++idx)
1183 {
1184 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1185 if (child_sp.get())
1186 {
1187 DumpValueObject (s,
1188 exe_scope,
1189 child_sp.get(),
1190 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001191 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001192 curr_depth + 1,
1193 max_depth,
1194 show_types,
1195 show_location,
1196 false,
1197 true,
1198 flat_output);
1199 }
1200 }
1201
1202 if (!flat_output)
1203 {
1204 s.IndentLess();
1205 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001206 }
1207 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001208 else if (has_children)
1209 {
1210 // Aggregate, no children...
1211 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001212 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001213 }
1214 else
1215 {
1216 if (print_valobj)
1217 s.EOL();
1218 }
1219
Greg Clayton1d3afba2010-10-05 00:00:42 +00001220 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001221 else
1222 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001223 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001224 }
1225 }
1226 else
1227 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001228 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001229 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001230 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001231 }
1232 }
1233 }
1234 }
1235}
1236
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001237
1238ValueObjectSP
1239ValueObject::CreateConstantValue (ExecutionContextScope *exe_scope, const ConstString &name)
1240{
1241 ValueObjectSP valobj_sp;
1242
1243 if (UpdateValueIfNeeded(exe_scope) && m_error.Success())
1244 {
1245 ExecutionContext exe_ctx;
1246 exe_scope->CalculateExecutionContext(exe_ctx);
1247
1248 clang::ASTContext *ast = GetClangAST ();
1249
1250 DataExtractor data;
1251 data.SetByteOrder (m_data.GetByteOrder());
1252 data.SetAddressByteSize(m_data.GetAddressByteSize());
1253
1254 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1255
1256 valobj_sp.reset (new ValueObjectConstResult (ast,
1257 GetClangType(),
1258 name,
1259 data));
1260 }
1261 else
1262 {
1263 valobj_sp.reset (new ValueObjectConstResult (m_error));
1264 }
1265 return valobj_sp;
1266}
1267
1268lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001269ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001270{
1271 lldb::ValueObjectSP valobj_sp;
Greg Clayton54979cd2010-12-15 05:08:08 +00001272 const bool is_pointer_type = IsPointerType();
1273 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001274 {
1275 bool omit_empty_base_classes = true;
1276
1277 std::string child_name_str;
1278 uint32_t child_byte_size = 0;
1279 int32_t child_byte_offset = 0;
1280 uint32_t child_bitfield_bit_size = 0;
1281 uint32_t child_bitfield_bit_offset = 0;
1282 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001283 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001284 const bool transparent_pointers = false;
1285 clang::ASTContext *clang_ast = GetClangAST();
1286 clang_type_t clang_type = GetClangType();
1287 clang_type_t child_clang_type;
1288 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1289 GetName().GetCString(),
1290 clang_type,
1291 0,
1292 transparent_pointers,
1293 omit_empty_base_classes,
1294 child_name_str,
1295 child_byte_size,
1296 child_byte_offset,
1297 child_bitfield_bit_size,
1298 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001299 child_is_base_class,
1300 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001301 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001302 {
1303 ConstString child_name;
1304 if (!child_name_str.empty())
1305 child_name.SetCString (child_name_str.c_str());
1306
1307 valobj_sp.reset (new ValueObjectChild (this,
1308 clang_ast,
1309 child_clang_type,
1310 child_name,
1311 child_byte_size,
1312 child_byte_offset,
1313 child_bitfield_bit_size,
1314 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001315 child_is_base_class,
1316 child_is_deref_of_parent));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001317 }
1318 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001319
1320 if (valobj_sp)
1321 {
1322 error.Clear();
1323 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001324 else
1325 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001326 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001327 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001328
1329 if (is_pointer_type)
1330 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1331 else
1332 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001333 }
1334
1335 return valobj_sp;
1336}
1337
Greg Clayton54979cd2010-12-15 05:08:08 +00001338 lldb::ValueObjectSP
1339ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001340{
1341 lldb::ValueObjectSP valobj_sp;
Greg Claytone0d378b2011-03-24 21:19:54 +00001342 AddressType address_type = eAddressTypeInvalid;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001343 const bool scalar_is_load_address = false;
1344 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001345 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001346 if (addr != LLDB_INVALID_ADDRESS)
1347 {
1348 switch (address_type)
1349 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001350 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001351 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001352 {
1353 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001354 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001355 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1356 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001357 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001358
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001359 case eAddressTypeFile:
1360 case eAddressTypeLoad:
1361 case eAddressTypeHost:
1362 {
1363 clang::ASTContext *ast = GetClangAST();
1364 clang_type_t clang_type = GetClangType();
1365 if (ast && clang_type)
1366 {
1367 std::string name (1, '&');
1368 name.append (m_name.AsCString(""));
1369 valobj_sp.reset (new ValueObjectConstResult (ast,
1370 ClangASTContext::CreatePointerType (ast, clang_type),
1371 ConstString (name.c_str()),
1372 addr,
Sean Callanan92adcac2011-01-13 08:53:35 +00001373 eAddressTypeInvalid,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001374 m_data.GetAddressByteSize()));
1375 }
1376 }
1377 break;
1378 }
1379 }
1380 return valobj_sp;
1381}
1382