blob: d10bf064c08d3ff2b8421655b27eba1bed3e6ea4 [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"
24#include "lldb/Core/ValueObjectList.h"
25
Greg Claytone1a916a2010-07-21 22:12:05 +000026#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027#include "lldb/Symbol/ClangASTContext.h"
28#include "lldb/Symbol/Type.h"
29
Jim Ingham53c47f12010-09-10 23:12:17 +000030#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000031#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032#include "lldb/Target/Process.h"
33#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000034#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036
37using namespace lldb;
38using namespace lldb_private;
39
40static lldb::user_id_t g_value_obj_uid = 0;
41
42//----------------------------------------------------------------------
43// ValueObject constructor
44//----------------------------------------------------------------------
Greg Clayton8f92f0a2010-10-14 22:52:14 +000045ValueObject::ValueObject (ValueObject *parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000046 UserID (++g_value_obj_uid), // Unique identifier for every value object
Greg Clayton8f92f0a2010-10-14 22:52:14 +000047 m_parent (parent),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048 m_update_id (0), // Value object lists always start at 1, value objects start at zero
49 m_name (),
50 m_data (),
51 m_value (),
52 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000053 m_value_str (),
54 m_old_value_str (),
55 m_location_str (),
56 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000057 m_object_desc_str (),
Greg Clayton288bdf92010-09-02 02:59:18 +000058 m_children (),
59 m_synthetic_children (),
Greg Clayton32c40852010-10-06 03:09:11 +000060 m_dynamic_value_sp (),
61 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000062 m_value_is_valid (false),
63 m_value_did_change (false),
64 m_children_count_valid (false),
65 m_old_value_valid (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066{
67}
68
69//----------------------------------------------------------------------
70// Destructor
71//----------------------------------------------------------------------
72ValueObject::~ValueObject ()
73{
74}
75
76user_id_t
77ValueObject::GetUpdateID() const
78{
79 return m_update_id;
80}
81
82bool
83ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
84{
Greg Claytonb71f3842010-10-05 03:13:51 +000085 // If this is a constant value, then our success is predicated on whether
86 // we have an error or not
87 if (GetIsConstant())
88 return m_error.Success();
89
Chris Lattner30fdc8d2010-06-08 16:52:24 +000090 if (exe_scope)
91 {
92 Process *process = exe_scope->CalculateProcess();
93 if (process)
94 {
95 const user_id_t stop_id = process->GetStopID();
96 if (m_update_id != stop_id)
97 {
Greg Clayton288bdf92010-09-02 02:59:18 +000098 bool first_update = m_update_id == 0;
Greg Clayton73b953b2010-08-28 00:08:07 +000099 // Save the old value using swap to avoid a string copy which
100 // also will clear our m_value_str
Greg Clayton288bdf92010-09-02 02:59:18 +0000101 if (m_value_str.empty())
102 {
103 m_old_value_valid = false;
104 }
105 else
106 {
107 m_old_value_valid = true;
108 m_old_value_str.swap (m_value_str);
109 m_value_str.clear();
110 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111 m_location_str.clear();
112 m_summary_str.clear();
Jim Ingham53c47f12010-09-10 23:12:17 +0000113 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114
Greg Clayton73b953b2010-08-28 00:08:07 +0000115 const bool value_was_valid = GetValueIsValid();
116 SetValueDidChange (false);
117
118 m_error.Clear();
119
120 // Call the pure virtual function to update the value
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000121 UpdateValue (exe_scope);
Greg Clayton73b953b2010-08-28 00:08:07 +0000122
123 // Update the fact that we tried to update the value for this
Greg Claytoned8a7052010-09-18 03:37:20 +0000124 // value object whether or not we succeed
Greg Clayton73b953b2010-08-28 00:08:07 +0000125 m_update_id = stop_id;
126 bool success = m_error.Success();
127 SetValueIsValid (success);
Greg Clayton288bdf92010-09-02 02:59:18 +0000128
129 if (first_update)
130 SetValueDidChange (false);
131 else if (!m_value_did_change && success == false)
Greg Clayton73b953b2010-08-28 00:08:07 +0000132 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000133 // The value wasn't gotten successfully, so we mark this
134 // as changed if the value used to be valid and now isn't
135 SetValueDidChange (value_was_valid);
Greg Clayton73b953b2010-08-28 00:08:07 +0000136 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137 }
138 }
139 }
140 return m_error.Success();
141}
142
143const DataExtractor &
144ValueObject::GetDataExtractor () const
145{
146 return m_data;
147}
148
149DataExtractor &
150ValueObject::GetDataExtractor ()
151{
152 return m_data;
153}
154
155const Error &
156ValueObject::GetError() const
157{
158 return m_error;
159}
160
161const ConstString &
162ValueObject::GetName() const
163{
164 return m_name;
165}
166
167const char *
168ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
169{
170 if (UpdateValueIfNeeded(exe_scope))
171 {
172 if (m_location_str.empty())
173 {
174 StreamString sstr;
175
176 switch (m_value.GetValueType())
177 {
178 default:
179 break;
180
181 case Value::eValueTypeScalar:
182 if (m_value.GetContextType() == Value::eContextTypeDCRegisterInfo)
183 {
184 RegisterInfo *reg_info = m_value.GetRegisterInfo();
185 if (reg_info)
186 {
187 if (reg_info->name)
188 m_location_str = reg_info->name;
189 else if (reg_info->alt_name)
190 m_location_str = reg_info->alt_name;
191 break;
192 }
193 }
194 m_location_str = "scalar";
195 break;
196
197 case Value::eValueTypeLoadAddress:
198 case Value::eValueTypeFileAddress:
199 case Value::eValueTypeHostAddress:
200 {
201 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
202 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
203 m_location_str.swap(sstr.GetString());
204 }
205 break;
206 }
207 }
208 }
209 return m_location_str.c_str();
210}
211
212Value &
213ValueObject::GetValue()
214{
215 return m_value;
216}
217
218const Value &
219ValueObject::GetValue() const
220{
221 return m_value;
222}
223
224bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000225ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000226{
Greg Clayton288bdf92010-09-02 02:59:18 +0000227 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000228}
229
230
231void
232ValueObject::SetValueIsValid (bool b)
233{
Greg Clayton288bdf92010-09-02 02:59:18 +0000234 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000235}
236
237bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000238ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239{
Greg Clayton288bdf92010-09-02 02:59:18 +0000240 GetValueAsCString (exe_scope);
241 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000242}
243
244void
245ValueObject::SetValueDidChange (bool value_changed)
246{
Greg Clayton288bdf92010-09-02 02:59:18 +0000247 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000248}
249
250ValueObjectSP
251ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
252{
253 ValueObjectSP child_sp;
254 if (idx < GetNumChildren())
255 {
256 // Check if we have already made the child value object?
257 if (can_create && m_children[idx].get() == NULL)
258 {
259 // No we haven't created the child at this index, so lets have our
260 // subclass do it and cache the result for quick future access.
261 m_children[idx] = CreateChildAtIndex (idx, false, 0);
262 }
263
264 child_sp = m_children[idx];
265 }
266 return child_sp;
267}
268
269uint32_t
270ValueObject::GetIndexOfChildWithName (const ConstString &name)
271{
272 bool omit_empty_base_classes = true;
273 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000274 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000275 name.AsCString(),
276 omit_empty_base_classes);
277}
278
279ValueObjectSP
280ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
281{
282 // when getting a child by name, it could be burried inside some base
283 // classes (which really aren't part of the expression path), so we
284 // need a vector of indexes that can get us down to the correct child
285 std::vector<uint32_t> child_indexes;
286 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton1be10fc2010-09-29 01:12:09 +0000287 void *clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 bool omit_empty_base_classes = true;
289 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
290 clang_type,
291 name.AsCString(),
292 omit_empty_base_classes,
293 child_indexes);
294 ValueObjectSP child_sp;
295 if (num_child_indexes > 0)
296 {
297 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
298 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
299
300 child_sp = GetChildAtIndex(*pos, can_create);
301 for (++pos; pos != end; ++pos)
302 {
303 if (child_sp)
304 {
305 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
306 child_sp = new_child_sp;
307 }
308 else
309 {
310 child_sp.reset();
311 }
312
313 }
314 }
315 return child_sp;
316}
317
318
319uint32_t
320ValueObject::GetNumChildren ()
321{
Greg Clayton288bdf92010-09-02 02:59:18 +0000322 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 {
324 SetNumChildren (CalculateNumChildren());
325 }
326 return m_children.size();
327}
328void
329ValueObject::SetNumChildren (uint32_t num_children)
330{
Greg Clayton288bdf92010-09-02 02:59:18 +0000331 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000332 m_children.resize(num_children);
333}
334
335void
336ValueObject::SetName (const char *name)
337{
338 m_name.SetCString(name);
339}
340
341void
342ValueObject::SetName (const ConstString &name)
343{
344 m_name = name;
345}
346
347ValueObjectSP
348ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
349{
350 ValueObjectSP valobj_sp;
351 bool omit_empty_base_classes = true;
352
353 std::string child_name_str;
354 uint32_t child_byte_size = 0;
355 int32_t child_byte_offset = 0;
356 uint32_t child_bitfield_bit_size = 0;
357 uint32_t child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000358 bool child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000359 const bool transparent_pointers = synthetic_array_member == false;
360 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton73b472d2010-10-27 03:32:59 +0000361 clang_type_t clang_type = GetClangType();
362 clang_type_t child_clang_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000363 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
364 GetName().AsCString(),
365 clang_type,
366 idx,
367 transparent_pointers,
368 omit_empty_base_classes,
369 child_name_str,
370 child_byte_size,
371 child_byte_offset,
372 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000373 child_bitfield_bit_offset,
374 child_is_base_class);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000375 if (child_clang_type)
376 {
377 if (synthetic_index)
378 child_byte_offset += child_byte_size * synthetic_index;
379
380 ConstString child_name;
381 if (!child_name_str.empty())
382 child_name.SetCString (child_name_str.c_str());
383
384 valobj_sp.reset (new ValueObjectChild (this,
385 clang_ast,
386 child_clang_type,
387 child_name,
388 child_byte_size,
389 child_byte_offset,
390 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000391 child_bitfield_bit_offset,
392 child_is_base_class));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000393 }
394 return valobj_sp;
395}
396
397const char *
398ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
399{
400 if (UpdateValueIfNeeded (exe_scope))
401 {
402 if (m_summary_str.empty())
403 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000404 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405
406 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000407 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000408 {
Greg Clayton737b9322010-09-13 03:32:57 +0000409 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000410 clang_type_t elem_or_pointee_clang_type;
411 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
412 GetClangAST(),
413 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000414
Greg Clayton73b472d2010-10-27 03:32:59 +0000415 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
416 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000417 {
Greg Clayton737b9322010-09-13 03:32:57 +0000418 Process *process = exe_scope->CalculateProcess();
419 if (process != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000420 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000421 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +0000422 lldb::AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423
Greg Clayton73b472d2010-10-27 03:32:59 +0000424 size_t cstr_len = 0;
425 if (type_flags.Test (ClangASTContext::eTypeIsArray))
426 {
427 // We have an array
428 cstr_len = ClangASTContext::GetArraySize (clang_type);
429 cstr_address = GetAddressOf (cstr_address_type, true);
430 }
431 else
432 {
433 // We have a pointer
434 cstr_address = GetPointerValue (cstr_address_type, true);
435 }
Greg Clayton737b9322010-09-13 03:32:57 +0000436 if (cstr_address != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437 {
Greg Clayton737b9322010-09-13 03:32:57 +0000438 DataExtractor data;
439 size_t bytes_read = 0;
440 std::vector<char> data_buffer;
441 std::vector<char> cstr_buffer;
442 size_t cstr_length;
443 Error error;
Greg Clayton73b472d2010-10-27 03:32:59 +0000444 if (cstr_len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000446 data_buffer.resize(cstr_len);
Greg Clayton737b9322010-09-13 03:32:57 +0000447 // Resize the formatted buffer in case every character
448 // uses the "\xXX" format and one extra byte for a NULL
449 cstr_buffer.resize(data_buffer.size() * 4 + 1);
450 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
Greg Clayton73b472d2010-10-27 03:32:59 +0000451 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
Greg Clayton737b9322010-09-13 03:32:57 +0000452 if (bytes_read > 0)
453 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000454 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000455 cstr_length = data.Dump (&sstr,
456 0, // Start offset in "data"
457 eFormatChar, // Print as characters
458 1, // Size of item (1 byte for a char!)
459 bytes_read, // How many bytes to print?
460 UINT32_MAX, // num per line
461 LLDB_INVALID_ADDRESS,// base address
462 0, // bitfield bit size
463 0); // bitfield bit offset
464 sstr << '"';
465 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000466 }
Greg Clayton737b9322010-09-13 03:32:57 +0000467 else
468 {
469 const size_t k_max_buf_size = 256;
470 data_buffer.resize (k_max_buf_size + 1);
471 // NULL terminate in case we don't get the entire C string
472 data_buffer.back() = '\0';
473 // Make a formatted buffer that can contain take 4
474 // bytes per character in case each byte uses the
475 // "\xXX" format and one extra byte for a NULL
476 cstr_buffer.resize (k_max_buf_size * 4 + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477
Greg Clayton737b9322010-09-13 03:32:57 +0000478 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
479 size_t total_cstr_len = 0;
480 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
481 {
482 size_t len = strlen(&data_buffer.front());
483 if (len == 0)
484 break;
485 if (len > bytes_read)
486 len = bytes_read;
487 if (sstr.GetSize() == 0)
488 sstr << '"';
489
490 cstr_length = data.Dump (&sstr,
491 0, // Start offset in "data"
492 eFormatChar, // Print as characters
493 1, // Size of item (1 byte for a char!)
494 len, // How many bytes to print?
495 UINT32_MAX, // num per line
496 LLDB_INVALID_ADDRESS,// base address
497 0, // bitfield bit size
498 0); // bitfield bit offset
499
500 if (len < k_max_buf_size)
501 break;
502 cstr_address += total_cstr_len;
503 }
504 if (sstr.GetSize() > 0)
505 sstr << '"';
506 }
507 }
508 }
509
510 if (sstr.GetSize() > 0)
511 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
512 }
513 else if (ClangASTContext::IsFunctionPointerType (clang_type))
514 {
515 lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
516 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
517
518 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
519 {
520 switch (func_ptr_address_type)
521 {
522 case eAddressTypeInvalid:
523 case eAddressTypeFile:
524 break;
525
526 case eAddressTypeLoad:
527 {
528 Address so_addr;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000529 Target *target = exe_scope->CalculateTarget();
530 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Clayton737b9322010-09-13 03:32:57 +0000531 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000532 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Greg Clayton737b9322010-09-13 03:32:57 +0000533 {
534 so_addr.Dump (&sstr,
535 exe_scope,
536 Address::DumpStyleResolvedDescription,
537 Address::DumpStyleSectionNameOffset);
538 }
539 }
540 }
541 break;
542
543 case eAddressTypeHost:
544 break;
545 }
546 }
547 if (sstr.GetSize() > 0)
548 {
549 m_summary_str.assign (1, '(');
550 m_summary_str.append (sstr.GetData(), sstr.GetSize());
551 m_summary_str.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000552 }
553 }
554 }
555 }
556 }
557 if (m_summary_str.empty())
558 return NULL;
559 return m_summary_str.c_str();
560}
561
Jim Ingham53c47f12010-09-10 23:12:17 +0000562const char *
563ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
564{
565 if (!m_object_desc_str.empty())
566 return m_object_desc_str.c_str();
567
Jim Ingham53c47f12010-09-10 23:12:17 +0000568 if (!GetValueIsValid())
569 return NULL;
570
571 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000572 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000573 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000574
Jim Ingham53c47f12010-09-10 23:12:17 +0000575 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000576
577 lldb::LanguageType language = GetObjectRuntimeLanguage();
578 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
579
580 if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
Jim Ingham53c47f12010-09-10 23:12:17 +0000581 {
582 m_object_desc_str.append (s.GetData());
583 }
Sean Callanan672ad942010-10-23 00:18:49 +0000584
585 if (m_object_desc_str.empty())
586 return NULL;
587 else
588 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000589}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590
591const char *
592ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
593{
594 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000595 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000596 {
597 if (UpdateValueIfNeeded(exe_scope))
598 {
599 if (m_value_str.empty())
600 {
601 const Value::ContextType context_type = m_value.GetContextType();
602
603 switch (context_type)
604 {
605 case Value::eContextTypeOpaqueClangQualType:
606 case Value::eContextTypeDCType:
607 case Value::eContextTypeDCVariable:
608 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000609 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000610 if (clang_type)
611 {
612 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000613 if (m_format == eFormatDefault)
614 m_format = ClangASTType::GetFormat(clang_type);
615
616 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
617 clang_type, // The clang type to display
618 &sstr,
619 m_format, // Format to display this type with
620 m_data, // Data to extract from
621 0, // Byte offset into "m_data"
622 GetByteSize(), // Byte size of item in "m_data"
623 GetBitfieldBitSize(), // Bitfield bit size
624 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625 m_value_str.swap(sstr.GetString());
626 else
627 m_value_str.clear();
628 }
629 }
630 break;
631
632 case Value::eContextTypeDCRegisterInfo:
633 {
634 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
635 if (reg_info)
636 {
637 StreamString reg_sstr;
638 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
639 m_value_str.swap(reg_sstr.GetString());
640 }
641 }
642 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000643
644 default:
645 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 }
647 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000648
649 if (!m_value_did_change && m_old_value_valid)
650 {
651 // The value was gotten successfully, so we consider the
652 // value as changed if the value string differs
653 SetValueDidChange (m_old_value_str != m_value_str);
654 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000655 }
656 }
657 if (m_value_str.empty())
658 return NULL;
659 return m_value_str.c_str();
660}
661
Greg Clayton737b9322010-09-13 03:32:57 +0000662addr_t
Greg Clayton73b472d2010-10-27 03:32:59 +0000663ValueObject::GetAddressOf (lldb::AddressType &address_type, bool scalar_is_load_address)
664{
665 switch (m_value.GetValueType())
666 {
667 case Value::eValueTypeScalar:
668 if (scalar_is_load_address)
669 {
670 address_type = eAddressTypeLoad;
671 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
672 }
673 break;
674
675 case Value::eValueTypeLoadAddress:
676 case Value::eValueTypeFileAddress:
677 case Value::eValueTypeHostAddress:
678 {
679 address_type = m_value.GetValueAddressType ();
680 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
681 }
682 break;
683 }
684 address_type = eAddressTypeInvalid;
685 return LLDB_INVALID_ADDRESS;
686}
687
688addr_t
Greg Clayton737b9322010-09-13 03:32:57 +0000689ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
690{
691 lldb::addr_t address = LLDB_INVALID_ADDRESS;
692 address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +0000693 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000694 {
695 case Value::eValueTypeScalar:
696 if (scalar_is_load_address)
697 {
698 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
699 address_type = eAddressTypeLoad;
700 }
701 break;
702
703 case Value::eValueTypeLoadAddress:
704 case Value::eValueTypeFileAddress:
705 case Value::eValueTypeHostAddress:
706 {
707 uint32_t data_offset = 0;
708 address = m_data.GetPointer(&data_offset);
709 address_type = m_value.GetValueAddressType();
710 if (address_type == eAddressTypeInvalid)
711 address_type = eAddressTypeLoad;
712 }
713 break;
714 }
715 return address;
716}
717
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718bool
719ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
720{
721 // Make sure our value is up to date first so that our location and location
722 // type is valid.
723 if (!UpdateValueIfNeeded(exe_scope))
724 return false;
725
726 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000727 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728
729 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000730 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000731 switch (encoding)
732 {
733 case eEncodingInvalid:
734 return false;
735
736 case eEncodingUint:
737 if (byte_size > sizeof(unsigned long long))
738 {
739 return false;
740 }
741 else
742 {
743 unsigned long long ull_val = strtoull(value_str, &end, 0);
744 if (end && *end != '\0')
745 return false;
746 m_value = ull_val;
747 // Limit the bytes in our m_data appropriately.
748 m_value.GetScalar().GetData (m_data, byte_size);
749 }
750 break;
751
752 case eEncodingSint:
753 if (byte_size > sizeof(long long))
754 {
755 return false;
756 }
757 else
758 {
759 long long sll_val = strtoll(value_str, &end, 0);
760 if (end && *end != '\0')
761 return false;
762 m_value = sll_val;
763 // Limit the bytes in our m_data appropriately.
764 m_value.GetScalar().GetData (m_data, byte_size);
765 }
766 break;
767
768 case eEncodingIEEE754:
769 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000771 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 if (dst != NULL)
773 {
774 // We are decoding a float into host byte order below, so make
775 // sure m_data knows what it contains.
776 m_data.SetByteOrder(eByteOrderHost);
777 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
778 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000779 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 value_str,
781 dst,
782 byte_size);
783
784 if (converted_byte_size == byte_size)
785 {
786 }
787 }
788 }
789 break;
790
791 case eEncodingVector:
792 return false;
793
794 default:
795 return false;
796 }
797
798 // If we have made it here the value is in m_data and we should write it
799 // out to the target
800 return Write ();
801}
802
803bool
804ValueObject::Write ()
805{
806 // Clear the update ID so the next time we try and read the value
807 // we try and read it again.
808 m_update_id = 0;
809
810 // TODO: when Value has a method to write a value back, call it from here.
811 return false;
812
813}
814
Jim Ingham5a369122010-09-28 01:25:32 +0000815lldb::LanguageType
816ValueObject::GetObjectRuntimeLanguage ()
817{
Greg Clayton73b472d2010-10-27 03:32:59 +0000818 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000819 if (opaque_qual_type == NULL)
820 return lldb::eLanguageTypeC;
821
822 // If the type is a reference, then resolve it to what it refers to first:
823 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
824 if (qual_type->isAnyPointerType())
825 {
826 if (qual_type->isObjCObjectPointerType())
827 return lldb::eLanguageTypeObjC;
828
829 clang::QualType pointee_type (qual_type->getPointeeType());
830 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
831 return lldb::eLanguageTypeC_plus_plus;
832 if (pointee_type->isObjCObjectOrInterfaceType())
833 return lldb::eLanguageTypeObjC;
834 if (pointee_type->isObjCClassType())
835 return lldb::eLanguageTypeObjC;
836 }
837 else
838 {
839 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
840 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000841 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000842 return lldb::eLanguageTypeC_plus_plus;
843 }
844
845 return lldb::eLanguageTypeC;
846}
847
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000848void
849ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
850{
851 m_synthetic_children[key] = valobj_sp;
852}
853
854ValueObjectSP
855ValueObject::GetSyntheticChild (const ConstString &key) const
856{
857 ValueObjectSP synthetic_child_sp;
858 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
859 if (pos != m_synthetic_children.end())
860 synthetic_child_sp = pos->second;
861 return synthetic_child_sp;
862}
863
864bool
865ValueObject::IsPointerType ()
866{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000867 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868}
869
Greg Clayton73b472d2010-10-27 03:32:59 +0000870
871
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000872bool
873ValueObject::IsPointerOrReferenceType ()
874{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000875 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000876}
877
878ValueObjectSP
879ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
880{
881 ValueObjectSP synthetic_child_sp;
882 if (IsPointerType ())
883 {
884 char index_str[64];
885 snprintf(index_str, sizeof(index_str), "[%i]", index);
886 ConstString index_const_str(index_str);
887 // Check if we have already created a synthetic array member in this
888 // valid object. If we have we will re-use it.
889 synthetic_child_sp = GetSyntheticChild (index_const_str);
890 if (!synthetic_child_sp)
891 {
892 // We haven't made a synthetic array member for INDEX yet, so
893 // lets make one and cache it for any future reference.
894 synthetic_child_sp = CreateChildAtIndex(0, true, index);
895
896 // Cache the value if we got one back...
897 if (synthetic_child_sp)
898 AddSyntheticChild(index_const_str, synthetic_child_sp);
899 }
900 }
901 return synthetic_child_sp;
902}
Jim Ingham22777012010-09-23 02:01:19 +0000903
904bool
905ValueObject::SetDynamicValue ()
906{
907 if (!IsPointerOrReferenceType())
908 return false;
909
910 // Check that the runtime class is correct for determining the most specific class.
911 // If it is a C++ class, see if it is dynamic:
Jim Ingham5a369122010-09-28 01:25:32 +0000912
Jim Ingham22777012010-09-23 02:01:19 +0000913 return true;
914}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000915
916
917void
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000918ValueObject::GetExpressionPath (Stream &s)
919{
920 if (m_parent)
921 {
922 m_parent->GetExpressionPath (s);
923 clang_type_t parent_clang_type = m_parent->GetClangType();
924 if (parent_clang_type)
925 {
926 if (ClangASTContext::IsPointerType(parent_clang_type))
927 {
928 s.PutCString("->");
929 }
930 else if (ClangASTContext::IsAggregateType (parent_clang_type))
931 {
932 if (ClangASTContext::IsArrayType (parent_clang_type) == false &&
933 m_parent->IsBaseClass() == false)
934 s.PutChar('.');
935 }
936 }
937 }
938
939 if (IsBaseClass())
940 {
941 clang_type_t clang_type = GetClangType();
942 std::string cxx_class_name;
943 if (ClangASTContext::GetCXXClassName (clang_type, cxx_class_name))
944 {
945 s << cxx_class_name.c_str() << "::";
946 }
947 }
948 else
949 {
950 const char *name = GetName().AsCString();
951 if (name)
952 s.PutCString(name);
953 }
954}
955
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000956void
Greg Clayton1d3afba2010-10-05 00:00:42 +0000957ValueObject::DumpValueObject
958(
959 Stream &s,
960 ExecutionContextScope *exe_scope,
961 ValueObject *valobj,
962 const char *root_valobj_name,
963 uint32_t ptr_depth,
964 uint32_t curr_depth,
965 uint32_t max_depth,
966 bool show_types,
967 bool show_location,
968 bool use_objc,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000969 bool scope_already_checked,
970 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +0000971)
972{
973 if (valobj)
974 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000975 clang_type_t clang_type = valobj->GetClangType();
976
Greg Clayton73b472d2010-10-27 03:32:59 +0000977 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000978 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +0000979 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
980 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000981
982 const bool print_valobj = flat_output == false || has_value;
983
984 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +0000985 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000986 if (show_location)
987 {
988 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
989 }
Greg Clayton1d3afba2010-10-05 00:00:42 +0000990
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000991 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +0000992
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000993 if (show_types)
994 s.Printf("(%s) ", valobj->GetTypeName().AsCString());
Greg Clayton1d3afba2010-10-05 00:00:42 +0000995
Greg Clayton1d3afba2010-10-05 00:00:42 +0000996
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000997 if (flat_output)
998 {
999 valobj->GetExpressionPath(s);
1000 s.PutCString(" =");
1001 }
1002 else
1003 {
1004 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1005 s.Printf ("%s =", name_cstr);
1006 }
1007
1008 if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1009 {
1010 err_cstr = "error: out of scope";
1011 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001012 }
1013
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001014 const char *val_cstr = NULL;
1015
1016 if (err_cstr == NULL)
1017 {
1018 val_cstr = valobj->GetValueAsCString(exe_scope);
1019 err_cstr = valobj->GetError().AsCString();
1020 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001021
1022 if (err_cstr)
1023 {
Greg Clayton0188eb92010-10-22 02:39:02 +00001024 s.Printf (" %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001025 }
1026 else
1027 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001028 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001029 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001030 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001031 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001032
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001033 if (val_cstr)
1034 s.Printf(" %s", val_cstr);
1035
1036 if (sum_cstr)
1037 s.Printf(" %s", sum_cstr);
1038
1039 if (use_objc)
1040 {
1041 const char *object_desc = valobj->GetObjectDescription(exe_scope);
1042 if (object_desc)
1043 s.Printf(" %s\n", object_desc);
1044 else
Sean Callanan672ad942010-10-23 00:18:49 +00001045 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001046 return;
1047 }
1048 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001049
1050 if (curr_depth < max_depth)
1051 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001052 // We will show children for all concrete types. We won't show
1053 // pointer contents unless a pointer depth has been specified.
1054 // We won't reference contents unless the reference is the
1055 // root object (depth of zero).
1056 bool print_children = true;
1057
1058 // Use a new temporary pointer depth in case we override the
1059 // current pointer depth below...
1060 uint32_t curr_ptr_depth = ptr_depth;
1061
1062 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1063 if (is_ptr || is_ref)
1064 {
1065 // We have a pointer or reference whose value is an address.
1066 // Make sure that address is not NULL
1067 lldb::AddressType ptr_address_type;
1068 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1069 print_children = false;
1070
1071 else if (is_ref && curr_depth == 0)
1072 {
1073 // If this is the root object (depth is zero) that we are showing
1074 // and it is a reference, and no pointer depth has been supplied
1075 // print out what it references. Don't do this at deeper depths
1076 // otherwise we can end up with infinite recursion...
1077 curr_ptr_depth = 1;
1078 }
1079
1080 if (curr_ptr_depth == 0)
1081 print_children = false;
1082 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001083
Greg Clayton73b472d2010-10-27 03:32:59 +00001084 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001085 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001086 const uint32_t num_children = valobj->GetNumChildren();
1087 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001088 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001089 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001090 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001091 if (print_valobj)
1092 s.EOL();
1093 }
1094 else
1095 {
1096 if (print_valobj)
1097 s.PutCString(" {\n");
1098 s.IndentMore();
1099 }
1100
1101 for (uint32_t idx=0; idx<num_children; ++idx)
1102 {
1103 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1104 if (child_sp.get())
1105 {
1106 DumpValueObject (s,
1107 exe_scope,
1108 child_sp.get(),
1109 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001110 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001111 curr_depth + 1,
1112 max_depth,
1113 show_types,
1114 show_location,
1115 false,
1116 true,
1117 flat_output);
1118 }
1119 }
1120
1121 if (!flat_output)
1122 {
1123 s.IndentLess();
1124 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001125 }
1126 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001127 else if (has_children)
1128 {
1129 // Aggregate, no children...
1130 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001131 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001132 }
1133 else
1134 {
1135 if (print_valobj)
1136 s.EOL();
1137 }
1138
Greg Clayton1d3afba2010-10-05 00:00:42 +00001139 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001140 else
1141 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001142 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001143 }
1144 }
1145 else
1146 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001147 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001148 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001149 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001150 }
1151 }
1152 }
1153 }
1154}
1155