blob: 95669255bd52cc664c806d66c4ad2120a6936342 [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 Clayton737b9322010-09-13 03:32:57 +0000442 lldb::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 {
524 lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
525 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 {
591 // Aw, hell, if the things a pointer, let's try ObjC anyway...
592 clang_type_t opaque_qual_type = GetClangType();
593 if (opaque_qual_type != NULL)
594 {
595 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
596 if (qual_type->isAnyPointerType())
597 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
598 }
599 }
600
Jim Ingham5a369122010-09-28 01:25:32 +0000601 if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
Jim Ingham53c47f12010-09-10 23:12:17 +0000602 {
603 m_object_desc_str.append (s.GetData());
604 }
Sean Callanan672ad942010-10-23 00:18:49 +0000605
606 if (m_object_desc_str.empty())
607 return NULL;
608 else
609 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000610}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000611
612const char *
613ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
614{
615 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000616 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000617 {
618 if (UpdateValueIfNeeded(exe_scope))
619 {
620 if (m_value_str.empty())
621 {
622 const Value::ContextType context_type = m_value.GetContextType();
623
624 switch (context_type)
625 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000626 case Value::eContextTypeClangType:
627 case Value::eContextTypeLLDBType:
628 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000630 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000631 if (clang_type)
632 {
633 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000634 if (m_format == eFormatDefault)
635 m_format = ClangASTType::GetFormat(clang_type);
636
637 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
638 clang_type, // The clang type to display
639 &sstr,
640 m_format, // Format to display this type with
641 m_data, // Data to extract from
642 0, // Byte offset into "m_data"
643 GetByteSize(), // Byte size of item in "m_data"
644 GetBitfieldBitSize(), // Bitfield bit size
645 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 m_value_str.swap(sstr.GetString());
647 else
648 m_value_str.clear();
649 }
650 }
651 break;
652
Greg Clayton526e5af2010-11-13 03:52:47 +0000653 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 {
655 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
656 if (reg_info)
657 {
658 StreamString reg_sstr;
659 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
660 m_value_str.swap(reg_sstr.GetString());
661 }
662 }
663 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000664
665 default:
666 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 }
668 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000669
670 if (!m_value_did_change && m_old_value_valid)
671 {
672 // The value was gotten successfully, so we consider the
673 // value as changed if the value string differs
674 SetValueDidChange (m_old_value_str != m_value_str);
675 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000676 }
677 }
678 if (m_value_str.empty())
679 return NULL;
680 return m_value_str.c_str();
681}
682
Greg Clayton737b9322010-09-13 03:32:57 +0000683addr_t
Greg Clayton73b472d2010-10-27 03:32:59 +0000684ValueObject::GetAddressOf (lldb::AddressType &address_type, bool scalar_is_load_address)
685{
686 switch (m_value.GetValueType())
687 {
688 case Value::eValueTypeScalar:
689 if (scalar_is_load_address)
690 {
691 address_type = eAddressTypeLoad;
692 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
693 }
694 break;
695
696 case Value::eValueTypeLoadAddress:
697 case Value::eValueTypeFileAddress:
698 case Value::eValueTypeHostAddress:
699 {
700 address_type = m_value.GetValueAddressType ();
701 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
702 }
703 break;
704 }
705 address_type = eAddressTypeInvalid;
706 return LLDB_INVALID_ADDRESS;
707}
708
709addr_t
Greg Clayton737b9322010-09-13 03:32:57 +0000710ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
711{
712 lldb::addr_t address = LLDB_INVALID_ADDRESS;
713 address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +0000714 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000715 {
716 case Value::eValueTypeScalar:
717 if (scalar_is_load_address)
718 {
719 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
720 address_type = eAddressTypeLoad;
721 }
722 break;
723
724 case Value::eValueTypeLoadAddress:
725 case Value::eValueTypeFileAddress:
726 case Value::eValueTypeHostAddress:
727 {
728 uint32_t data_offset = 0;
729 address = m_data.GetPointer(&data_offset);
730 address_type = m_value.GetValueAddressType();
731 if (address_type == eAddressTypeInvalid)
732 address_type = eAddressTypeLoad;
733 }
734 break;
735 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000736
737 if (m_pointers_point_to_load_addrs)
738 address_type = eAddressTypeLoad;
739
Greg Clayton737b9322010-09-13 03:32:57 +0000740 return address;
741}
742
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743bool
744ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
745{
746 // Make sure our value is up to date first so that our location and location
747 // type is valid.
748 if (!UpdateValueIfNeeded(exe_scope))
749 return false;
750
751 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000752 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000753
754 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000755 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 switch (encoding)
757 {
758 case eEncodingInvalid:
759 return false;
760
761 case eEncodingUint:
762 if (byte_size > sizeof(unsigned long long))
763 {
764 return false;
765 }
766 else
767 {
768 unsigned long long ull_val = strtoull(value_str, &end, 0);
769 if (end && *end != '\0')
770 return false;
771 m_value = ull_val;
772 // Limit the bytes in our m_data appropriately.
773 m_value.GetScalar().GetData (m_data, byte_size);
774 }
775 break;
776
777 case eEncodingSint:
778 if (byte_size > sizeof(long long))
779 {
780 return false;
781 }
782 else
783 {
784 long long sll_val = strtoll(value_str, &end, 0);
785 if (end && *end != '\0')
786 return false;
787 m_value = sll_val;
788 // Limit the bytes in our m_data appropriately.
789 m_value.GetScalar().GetData (m_data, byte_size);
790 }
791 break;
792
793 case eEncodingIEEE754:
794 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000796 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 if (dst != NULL)
798 {
799 // We are decoding a float into host byte order below, so make
800 // sure m_data knows what it contains.
Greg Clayton7fb56d02011-02-01 01:31:41 +0000801 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
803 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000804 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 value_str,
806 dst,
807 byte_size);
808
809 if (converted_byte_size == byte_size)
810 {
811 }
812 }
813 }
814 break;
815
816 case eEncodingVector:
817 return false;
818
819 default:
820 return false;
821 }
822
823 // If we have made it here the value is in m_data and we should write it
824 // out to the target
825 return Write ();
826}
827
828bool
829ValueObject::Write ()
830{
831 // Clear the update ID so the next time we try and read the value
832 // we try and read it again.
833 m_update_id = 0;
834
835 // TODO: when Value has a method to write a value back, call it from here.
836 return false;
837
838}
839
Jim Ingham5a369122010-09-28 01:25:32 +0000840lldb::LanguageType
841ValueObject::GetObjectRuntimeLanguage ()
842{
Greg Clayton73b472d2010-10-27 03:32:59 +0000843 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000844 if (opaque_qual_type == NULL)
845 return lldb::eLanguageTypeC;
846
847 // If the type is a reference, then resolve it to what it refers to first:
848 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
849 if (qual_type->isAnyPointerType())
850 {
851 if (qual_type->isObjCObjectPointerType())
852 return lldb::eLanguageTypeObjC;
853
854 clang::QualType pointee_type (qual_type->getPointeeType());
855 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
856 return lldb::eLanguageTypeC_plus_plus;
857 if (pointee_type->isObjCObjectOrInterfaceType())
858 return lldb::eLanguageTypeObjC;
859 if (pointee_type->isObjCClassType())
860 return lldb::eLanguageTypeObjC;
861 }
862 else
863 {
864 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
865 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000866 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000867 return lldb::eLanguageTypeC_plus_plus;
868 }
869
870 return lldb::eLanguageTypeC;
871}
872
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000873void
874ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
875{
876 m_synthetic_children[key] = valobj_sp;
877}
878
879ValueObjectSP
880ValueObject::GetSyntheticChild (const ConstString &key) const
881{
882 ValueObjectSP synthetic_child_sp;
883 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
884 if (pos != m_synthetic_children.end())
885 synthetic_child_sp = pos->second;
886 return synthetic_child_sp;
887}
888
889bool
890ValueObject::IsPointerType ()
891{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000892 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893}
894
Greg Clayton73b472d2010-10-27 03:32:59 +0000895
896
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897bool
898ValueObject::IsPointerOrReferenceType ()
899{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000900 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000901}
902
903ValueObjectSP
904ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
905{
906 ValueObjectSP synthetic_child_sp;
907 if (IsPointerType ())
908 {
909 char index_str[64];
910 snprintf(index_str, sizeof(index_str), "[%i]", index);
911 ConstString index_const_str(index_str);
912 // Check if we have already created a synthetic array member in this
913 // valid object. If we have we will re-use it.
914 synthetic_child_sp = GetSyntheticChild (index_const_str);
915 if (!synthetic_child_sp)
916 {
917 // We haven't made a synthetic array member for INDEX yet, so
918 // lets make one and cache it for any future reference.
919 synthetic_child_sp = CreateChildAtIndex(0, true, index);
920
921 // Cache the value if we got one back...
922 if (synthetic_child_sp)
923 AddSyntheticChild(index_const_str, synthetic_child_sp);
924 }
925 }
926 return synthetic_child_sp;
927}
Jim Ingham22777012010-09-23 02:01:19 +0000928
929bool
930ValueObject::SetDynamicValue ()
931{
932 if (!IsPointerOrReferenceType())
933 return false;
934
935 // Check that the runtime class is correct for determining the most specific class.
936 // If it is a C++ class, see if it is dynamic:
Jim Ingham5a369122010-09-28 01:25:32 +0000937
Jim Ingham22777012010-09-23 02:01:19 +0000938 return true;
939}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000940
Greg Claytone221f822011-01-21 01:59:00 +0000941bool
942ValueObject::GetBaseClassPath (Stream &s)
943{
944 if (IsBaseClass())
945 {
946 bool parent_had_base_class = m_parent && m_parent->GetBaseClassPath (s);
947 clang_type_t clang_type = GetClangType();
948 std::string cxx_class_name;
949 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
950 if (this_had_base_class)
951 {
952 if (parent_had_base_class)
953 s.PutCString("::");
954 s.PutCString(cxx_class_name.c_str());
955 }
956 return parent_had_base_class || this_had_base_class;
957 }
958 return false;
959}
960
961
962ValueObject *
963ValueObject::GetNonBaseClassParent()
964{
965 if (m_parent)
966 {
967 if (m_parent->IsBaseClass())
968 return m_parent->GetNonBaseClassParent();
969 else
970 return m_parent;
971 }
972 return NULL;
973}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000974
975void
Greg Clayton6beaaa62011-01-17 03:46:26 +0000976ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000977{
Greg Claytone221f822011-01-21 01:59:00 +0000978 const bool is_deref_of_parent = IsDereferenceOfParent ();
979
980 if (is_deref_of_parent)
981 s.PutCString("*(");
982
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000983 if (m_parent)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000984 m_parent->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +0000985
986 if (!IsBaseClass())
987 {
988 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000989 {
Greg Claytone221f822011-01-21 01:59:00 +0000990 ValueObject *non_base_class_parent = GetNonBaseClassParent();
991 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000992 {
Greg Claytone221f822011-01-21 01:59:00 +0000993 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
994 if (non_base_class_parent_clang_type)
995 {
996 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
997
998 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
999 {
1000 s.PutCString("->");
1001 }
1002 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1003 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1004 {
1005 s.PutChar('.');
1006 }
1007 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001008 }
Greg Claytone221f822011-01-21 01:59:00 +00001009
1010 const char *name = GetName().GetCString();
1011 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001012 {
Greg Claytone221f822011-01-21 01:59:00 +00001013 if (qualify_cxx_base_classes)
1014 {
1015 if (GetBaseClassPath (s))
1016 s.PutCString("::");
1017 }
1018 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001019 }
1020 }
1021 }
1022
Greg Claytone221f822011-01-21 01:59:00 +00001023 if (is_deref_of_parent)
1024 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001025}
1026
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001027void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001028ValueObject::DumpValueObject
1029(
1030 Stream &s,
1031 ExecutionContextScope *exe_scope,
1032 ValueObject *valobj,
1033 const char *root_valobj_name,
1034 uint32_t ptr_depth,
1035 uint32_t curr_depth,
1036 uint32_t max_depth,
1037 bool show_types,
1038 bool show_location,
1039 bool use_objc,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001040 bool scope_already_checked,
1041 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001042)
1043{
1044 if (valobj)
1045 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001046 clang_type_t clang_type = valobj->GetClangType();
1047
Greg Clayton73b472d2010-10-27 03:32:59 +00001048 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001049 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001050 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1051 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001052
1053 const bool print_valobj = flat_output == false || has_value;
1054
1055 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001056 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001057 if (show_location)
1058 {
1059 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1060 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001061
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001062 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001063
Greg Clayton7c8a9662010-11-02 01:50:16 +00001064 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001065 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001066 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001067
Greg Clayton1d3afba2010-10-05 00:00:42 +00001068
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001069 if (flat_output)
1070 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001071 // If we are showing types, also qualify the C++ base classes
1072 const bool qualify_cxx_base_classes = show_types;
1073 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001074 s.PutCString(" =");
1075 }
1076 else
1077 {
1078 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1079 s.Printf ("%s =", name_cstr);
1080 }
1081
1082 if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1083 {
1084 err_cstr = "error: out of scope";
1085 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001086 }
1087
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001088 const char *val_cstr = NULL;
1089
1090 if (err_cstr == NULL)
1091 {
1092 val_cstr = valobj->GetValueAsCString(exe_scope);
1093 err_cstr = valobj->GetError().AsCString();
1094 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001095
1096 if (err_cstr)
1097 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001098 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001099 }
1100 else
1101 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001102 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001103 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001104 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001105 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001106
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001107 if (val_cstr)
1108 s.Printf(" %s", val_cstr);
1109
1110 if (sum_cstr)
1111 s.Printf(" %s", sum_cstr);
1112
1113 if (use_objc)
1114 {
1115 const char *object_desc = valobj->GetObjectDescription(exe_scope);
1116 if (object_desc)
1117 s.Printf(" %s\n", object_desc);
1118 else
Sean Callanan672ad942010-10-23 00:18:49 +00001119 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001120 return;
1121 }
1122 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001123
1124 if (curr_depth < max_depth)
1125 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001126 // We will show children for all concrete types. We won't show
1127 // pointer contents unless a pointer depth has been specified.
1128 // We won't reference contents unless the reference is the
1129 // root object (depth of zero).
1130 bool print_children = true;
1131
1132 // Use a new temporary pointer depth in case we override the
1133 // current pointer depth below...
1134 uint32_t curr_ptr_depth = ptr_depth;
1135
1136 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1137 if (is_ptr || is_ref)
1138 {
1139 // We have a pointer or reference whose value is an address.
1140 // Make sure that address is not NULL
1141 lldb::AddressType ptr_address_type;
1142 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1143 print_children = false;
1144
1145 else if (is_ref && curr_depth == 0)
1146 {
1147 // If this is the root object (depth is zero) that we are showing
1148 // and it is a reference, and no pointer depth has been supplied
1149 // print out what it references. Don't do this at deeper depths
1150 // otherwise we can end up with infinite recursion...
1151 curr_ptr_depth = 1;
1152 }
1153
1154 if (curr_ptr_depth == 0)
1155 print_children = false;
1156 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001157
Greg Clayton73b472d2010-10-27 03:32:59 +00001158 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001159 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001160 const uint32_t num_children = valobj->GetNumChildren();
1161 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001162 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001163 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001164 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001165 if (print_valobj)
1166 s.EOL();
1167 }
1168 else
1169 {
1170 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001171 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001172 s.IndentMore();
1173 }
1174
1175 for (uint32_t idx=0; idx<num_children; ++idx)
1176 {
1177 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1178 if (child_sp.get())
1179 {
1180 DumpValueObject (s,
1181 exe_scope,
1182 child_sp.get(),
1183 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001184 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001185 curr_depth + 1,
1186 max_depth,
1187 show_types,
1188 show_location,
1189 false,
1190 true,
1191 flat_output);
1192 }
1193 }
1194
1195 if (!flat_output)
1196 {
1197 s.IndentLess();
1198 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001199 }
1200 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001201 else if (has_children)
1202 {
1203 // Aggregate, no children...
1204 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001205 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001206 }
1207 else
1208 {
1209 if (print_valobj)
1210 s.EOL();
1211 }
1212
Greg Clayton1d3afba2010-10-05 00:00:42 +00001213 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001214 else
1215 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001216 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001217 }
1218 }
1219 else
1220 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001221 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001222 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001223 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001224 }
1225 }
1226 }
1227 }
1228}
1229
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001230
1231ValueObjectSP
1232ValueObject::CreateConstantValue (ExecutionContextScope *exe_scope, const ConstString &name)
1233{
1234 ValueObjectSP valobj_sp;
1235
1236 if (UpdateValueIfNeeded(exe_scope) && m_error.Success())
1237 {
1238 ExecutionContext exe_ctx;
1239 exe_scope->CalculateExecutionContext(exe_ctx);
1240
1241 clang::ASTContext *ast = GetClangAST ();
1242
1243 DataExtractor data;
1244 data.SetByteOrder (m_data.GetByteOrder());
1245 data.SetAddressByteSize(m_data.GetAddressByteSize());
1246
1247 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1248
1249 valobj_sp.reset (new ValueObjectConstResult (ast,
1250 GetClangType(),
1251 name,
1252 data));
1253 }
1254 else
1255 {
1256 valobj_sp.reset (new ValueObjectConstResult (m_error));
1257 }
1258 return valobj_sp;
1259}
1260
1261lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001262ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001263{
1264 lldb::ValueObjectSP valobj_sp;
Greg Clayton54979cd2010-12-15 05:08:08 +00001265 const bool is_pointer_type = IsPointerType();
1266 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001267 {
1268 bool omit_empty_base_classes = true;
1269
1270 std::string child_name_str;
1271 uint32_t child_byte_size = 0;
1272 int32_t child_byte_offset = 0;
1273 uint32_t child_bitfield_bit_size = 0;
1274 uint32_t child_bitfield_bit_offset = 0;
1275 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001276 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001277 const bool transparent_pointers = false;
1278 clang::ASTContext *clang_ast = GetClangAST();
1279 clang_type_t clang_type = GetClangType();
1280 clang_type_t child_clang_type;
1281 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1282 GetName().GetCString(),
1283 clang_type,
1284 0,
1285 transparent_pointers,
1286 omit_empty_base_classes,
1287 child_name_str,
1288 child_byte_size,
1289 child_byte_offset,
1290 child_bitfield_bit_size,
1291 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001292 child_is_base_class,
1293 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001294 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001295 {
1296 ConstString child_name;
1297 if (!child_name_str.empty())
1298 child_name.SetCString (child_name_str.c_str());
1299
1300 valobj_sp.reset (new ValueObjectChild (this,
1301 clang_ast,
1302 child_clang_type,
1303 child_name,
1304 child_byte_size,
1305 child_byte_offset,
1306 child_bitfield_bit_size,
1307 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001308 child_is_base_class,
1309 child_is_deref_of_parent));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001310 }
1311 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001312
1313 if (valobj_sp)
1314 {
1315 error.Clear();
1316 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001317 else
1318 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001319 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001320 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001321
1322 if (is_pointer_type)
1323 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1324 else
1325 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001326 }
1327
1328 return valobj_sp;
1329}
1330
Greg Clayton54979cd2010-12-15 05:08:08 +00001331 lldb::ValueObjectSP
1332ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001333{
1334 lldb::ValueObjectSP valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001335 lldb::AddressType address_type = eAddressTypeInvalid;
1336 const bool scalar_is_load_address = false;
1337 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001338 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001339 if (addr != LLDB_INVALID_ADDRESS)
1340 {
1341 switch (address_type)
1342 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001343 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001344 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001345 {
1346 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001347 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001348 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1349 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001350 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001351
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001352 case eAddressTypeFile:
1353 case eAddressTypeLoad:
1354 case eAddressTypeHost:
1355 {
1356 clang::ASTContext *ast = GetClangAST();
1357 clang_type_t clang_type = GetClangType();
1358 if (ast && clang_type)
1359 {
1360 std::string name (1, '&');
1361 name.append (m_name.AsCString(""));
1362 valobj_sp.reset (new ValueObjectConstResult (ast,
1363 ClangASTContext::CreatePointerType (ast, clang_type),
1364 ConstString (name.c_str()),
1365 addr,
Sean Callanan92adcac2011-01-13 08:53:35 +00001366 eAddressTypeInvalid,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001367 m_data.GetAddressByteSize()));
1368 }
1369 }
1370 break;
1371 }
1372 }
1373 return valobj_sp;
1374}
1375