blob: 60b1009ad7a9df0b1a8ca60c5fb476c4643f3081 [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 Claytone1a916a2010-07-21 22:12:05 +000027#include "lldb/Symbol/ClangASTType.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Symbol/ClangASTContext.h"
29#include "lldb/Symbol/Type.h"
30
Jim Ingham53c47f12010-09-10 23:12:17 +000031#include "lldb/Target/ExecutionContext.h"
Jim Ingham5a369122010-09-28 01:25:32 +000032#include "lldb/Target/LanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000033#include "lldb/Target/Process.h"
34#include "lldb/Target/RegisterContext.h"
Greg Claytonf5e56de2010-09-14 23:36:40 +000035#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Target/Thread.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037
38using namespace lldb;
39using namespace lldb_private;
40
41static lldb::user_id_t g_value_obj_uid = 0;
42
43//----------------------------------------------------------------------
44// ValueObject constructor
45//----------------------------------------------------------------------
Greg Clayton8f92f0a2010-10-14 22:52:14 +000046ValueObject::ValueObject (ValueObject *parent) :
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047 UserID (++g_value_obj_uid), // Unique identifier for every value object
Greg Clayton8f92f0a2010-10-14 22:52:14 +000048 m_parent (parent),
Chris Lattner30fdc8d2010-06-08 16:52:24 +000049 m_update_id (0), // Value object lists always start at 1, value objects start at zero
50 m_name (),
51 m_data (),
52 m_value (),
53 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000054 m_value_str (),
55 m_old_value_str (),
56 m_location_str (),
57 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000058 m_object_desc_str (),
Greg Clayton288bdf92010-09-02 02:59:18 +000059 m_children (),
60 m_synthetic_children (),
Greg Clayton32c40852010-10-06 03:09:11 +000061 m_dynamic_value_sp (),
62 m_format (eFormatDefault),
Greg Clayton288bdf92010-09-02 02:59:18 +000063 m_value_is_valid (false),
64 m_value_did_change (false),
65 m_children_count_valid (false),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +000066 m_old_value_valid (false),
67 m_pointers_point_to_load_addrs (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068{
69}
70
71//----------------------------------------------------------------------
72// Destructor
73//----------------------------------------------------------------------
74ValueObject::~ValueObject ()
75{
76}
77
78user_id_t
79ValueObject::GetUpdateID() const
80{
81 return m_update_id;
82}
83
84bool
85ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
86{
Greg Claytonb71f3842010-10-05 03:13:51 +000087 // If this is a constant value, then our success is predicated on whether
88 // we have an error or not
89 if (GetIsConstant())
90 return m_error.Success();
91
Chris Lattner30fdc8d2010-06-08 16:52:24 +000092 if (exe_scope)
93 {
94 Process *process = exe_scope->CalculateProcess();
95 if (process)
96 {
97 const user_id_t stop_id = process->GetStopID();
98 if (m_update_id != stop_id)
99 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000100 bool first_update = m_update_id == 0;
Greg Clayton73b953b2010-08-28 00:08:07 +0000101 // Save the old value using swap to avoid a string copy which
102 // also will clear our m_value_str
Greg Clayton288bdf92010-09-02 02:59:18 +0000103 if (m_value_str.empty())
104 {
105 m_old_value_valid = false;
106 }
107 else
108 {
109 m_old_value_valid = true;
110 m_old_value_str.swap (m_value_str);
111 m_value_str.clear();
112 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 m_location_str.clear();
114 m_summary_str.clear();
Jim Ingham53c47f12010-09-10 23:12:17 +0000115 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000116
Greg Clayton73b953b2010-08-28 00:08:07 +0000117 const bool value_was_valid = GetValueIsValid();
118 SetValueDidChange (false);
119
120 m_error.Clear();
121
122 // Call the pure virtual function to update the value
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000123 UpdateValue (exe_scope);
Greg Clayton73b953b2010-08-28 00:08:07 +0000124
125 // Update the fact that we tried to update the value for this
Greg Claytoned8a7052010-09-18 03:37:20 +0000126 // value object whether or not we succeed
Greg Clayton73b953b2010-08-28 00:08:07 +0000127 m_update_id = stop_id;
128 bool success = m_error.Success();
129 SetValueIsValid (success);
Greg Clayton288bdf92010-09-02 02:59:18 +0000130
131 if (first_update)
132 SetValueDidChange (false);
133 else if (!m_value_did_change && success == false)
Greg Clayton73b953b2010-08-28 00:08:07 +0000134 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000135 // The value wasn't gotten successfully, so we mark this
136 // as changed if the value used to be valid and now isn't
137 SetValueDidChange (value_was_valid);
Greg Clayton73b953b2010-08-28 00:08:07 +0000138 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000139 }
140 }
141 }
142 return m_error.Success();
143}
144
145const DataExtractor &
146ValueObject::GetDataExtractor () const
147{
148 return m_data;
149}
150
151DataExtractor &
152ValueObject::GetDataExtractor ()
153{
154 return m_data;
155}
156
157const Error &
158ValueObject::GetError() const
159{
160 return m_error;
161}
162
163const ConstString &
164ValueObject::GetName() const
165{
166 return m_name;
167}
168
169const char *
170ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
171{
172 if (UpdateValueIfNeeded(exe_scope))
173 {
174 if (m_location_str.empty())
175 {
176 StreamString sstr;
177
178 switch (m_value.GetValueType())
179 {
180 default:
181 break;
182
183 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000184 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000185 {
186 RegisterInfo *reg_info = m_value.GetRegisterInfo();
187 if (reg_info)
188 {
189 if (reg_info->name)
190 m_location_str = reg_info->name;
191 else if (reg_info->alt_name)
192 m_location_str = reg_info->alt_name;
193 break;
194 }
195 }
196 m_location_str = "scalar";
197 break;
198
199 case Value::eValueTypeLoadAddress:
200 case Value::eValueTypeFileAddress:
201 case Value::eValueTypeHostAddress:
202 {
203 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
204 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
205 m_location_str.swap(sstr.GetString());
206 }
207 break;
208 }
209 }
210 }
211 return m_location_str.c_str();
212}
213
214Value &
215ValueObject::GetValue()
216{
217 return m_value;
218}
219
220const Value &
221ValueObject::GetValue() const
222{
223 return m_value;
224}
225
226bool
Greg Clayton8f343b02010-11-04 01:54:29 +0000227ValueObject::ResolveValue (ExecutionContextScope *exe_scope, Scalar &scalar)
228{
229 ExecutionContext exe_ctx;
230 exe_scope->CalculateExecutionContext(exe_ctx);
231 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
232 return scalar.IsValid();
233}
234
235bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000236ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237{
Greg Clayton288bdf92010-09-02 02:59:18 +0000238 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000239}
240
241
242void
243ValueObject::SetValueIsValid (bool b)
244{
Greg Clayton288bdf92010-09-02 02:59:18 +0000245 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000246}
247
248bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000249ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000250{
Greg Clayton288bdf92010-09-02 02:59:18 +0000251 GetValueAsCString (exe_scope);
252 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000253}
254
255void
256ValueObject::SetValueDidChange (bool value_changed)
257{
Greg Clayton288bdf92010-09-02 02:59:18 +0000258 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000259}
260
261ValueObjectSP
262ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
263{
264 ValueObjectSP child_sp;
265 if (idx < GetNumChildren())
266 {
267 // Check if we have already made the child value object?
268 if (can_create && m_children[idx].get() == NULL)
269 {
270 // No we haven't created the child at this index, so lets have our
271 // subclass do it and cache the result for quick future access.
272 m_children[idx] = CreateChildAtIndex (idx, false, 0);
273 }
274
275 child_sp = m_children[idx];
276 }
277 return child_sp;
278}
279
280uint32_t
281ValueObject::GetIndexOfChildWithName (const ConstString &name)
282{
283 bool omit_empty_base_classes = true;
284 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000285 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000286 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 omit_empty_base_classes);
288}
289
290ValueObjectSP
291ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
292{
293 // when getting a child by name, it could be burried inside some base
294 // classes (which really aren't part of the expression path), so we
295 // need a vector of indexes that can get us down to the correct child
296 std::vector<uint32_t> child_indexes;
297 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton1be10fc2010-09-29 01:12:09 +0000298 void *clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 bool omit_empty_base_classes = true;
300 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
301 clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000302 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000303 omit_empty_base_classes,
304 child_indexes);
305 ValueObjectSP child_sp;
306 if (num_child_indexes > 0)
307 {
308 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
309 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
310
311 child_sp = GetChildAtIndex(*pos, can_create);
312 for (++pos; pos != end; ++pos)
313 {
314 if (child_sp)
315 {
316 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
317 child_sp = new_child_sp;
318 }
319 else
320 {
321 child_sp.reset();
322 }
323
324 }
325 }
326 return child_sp;
327}
328
329
330uint32_t
331ValueObject::GetNumChildren ()
332{
Greg Clayton288bdf92010-09-02 02:59:18 +0000333 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000334 {
335 SetNumChildren (CalculateNumChildren());
336 }
337 return m_children.size();
338}
339void
340ValueObject::SetNumChildren (uint32_t num_children)
341{
Greg Clayton288bdf92010-09-02 02:59:18 +0000342 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343 m_children.resize(num_children);
344}
345
346void
347ValueObject::SetName (const char *name)
348{
349 m_name.SetCString(name);
350}
351
352void
353ValueObject::SetName (const ConstString &name)
354{
355 m_name = name;
356}
357
358ValueObjectSP
359ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
360{
361 ValueObjectSP valobj_sp;
362 bool omit_empty_base_classes = true;
363
364 std::string child_name_str;
365 uint32_t child_byte_size = 0;
366 int32_t child_byte_offset = 0;
367 uint32_t child_bitfield_bit_size = 0;
368 uint32_t child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000369 bool child_is_base_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370 const bool transparent_pointers = synthetic_array_member == false;
371 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton73b472d2010-10-27 03:32:59 +0000372 clang_type_t clang_type = GetClangType();
373 clang_type_t child_clang_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000374 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000375 GetName().GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000376 clang_type,
377 idx,
378 transparent_pointers,
379 omit_empty_base_classes,
380 child_name_str,
381 child_byte_size,
382 child_byte_offset,
383 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000384 child_bitfield_bit_offset,
385 child_is_base_class);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000386 if (child_clang_type)
387 {
388 if (synthetic_index)
389 child_byte_offset += child_byte_size * synthetic_index;
390
391 ConstString child_name;
392 if (!child_name_str.empty())
393 child_name.SetCString (child_name_str.c_str());
394
395 valobj_sp.reset (new ValueObjectChild (this,
396 clang_ast,
397 child_clang_type,
398 child_name,
399 child_byte_size,
400 child_byte_offset,
401 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000402 child_bitfield_bit_offset,
403 child_is_base_class));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000404 if (m_pointers_point_to_load_addrs)
405 valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 }
407 return valobj_sp;
408}
409
410const char *
411ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
412{
413 if (UpdateValueIfNeeded (exe_scope))
414 {
415 if (m_summary_str.empty())
416 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000417 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
419 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000420 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 {
Greg Clayton737b9322010-09-13 03:32:57 +0000422 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000423 clang_type_t elem_or_pointee_clang_type;
424 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000425 GetClangAST(),
426 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000427
Greg Clayton73b472d2010-10-27 03:32:59 +0000428 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
429 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430 {
Greg Clayton737b9322010-09-13 03:32:57 +0000431 Process *process = exe_scope->CalculateProcess();
432 if (process != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000434 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +0000435 lldb::AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436
Greg Clayton73b472d2010-10-27 03:32:59 +0000437 size_t cstr_len = 0;
438 if (type_flags.Test (ClangASTContext::eTypeIsArray))
439 {
440 // We have an array
441 cstr_len = ClangASTContext::GetArraySize (clang_type);
442 cstr_address = GetAddressOf (cstr_address_type, true);
443 }
444 else
445 {
446 // We have a pointer
447 cstr_address = GetPointerValue (cstr_address_type, true);
448 }
Greg Clayton737b9322010-09-13 03:32:57 +0000449 if (cstr_address != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 {
Greg Clayton737b9322010-09-13 03:32:57 +0000451 DataExtractor data;
452 size_t bytes_read = 0;
453 std::vector<char> data_buffer;
454 std::vector<char> cstr_buffer;
455 size_t cstr_length;
456 Error error;
Greg Clayton73b472d2010-10-27 03:32:59 +0000457 if (cstr_len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000458 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000459 data_buffer.resize(cstr_len);
Greg Clayton737b9322010-09-13 03:32:57 +0000460 // Resize the formatted buffer in case every character
461 // uses the "\xXX" format and one extra byte for a NULL
462 cstr_buffer.resize(data_buffer.size() * 4 + 1);
463 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
Greg Clayton73b472d2010-10-27 03:32:59 +0000464 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
Greg Clayton737b9322010-09-13 03:32:57 +0000465 if (bytes_read > 0)
466 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000468 cstr_length = data.Dump (&sstr,
469 0, // Start offset in "data"
470 eFormatChar, // Print as characters
471 1, // Size of item (1 byte for a char!)
472 bytes_read, // How many bytes to print?
473 UINT32_MAX, // num per line
474 LLDB_INVALID_ADDRESS,// base address
475 0, // bitfield bit size
476 0); // bitfield bit offset
477 sstr << '"';
478 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000479 }
Greg Clayton737b9322010-09-13 03:32:57 +0000480 else
481 {
482 const size_t k_max_buf_size = 256;
483 data_buffer.resize (k_max_buf_size + 1);
484 // NULL terminate in case we don't get the entire C string
485 data_buffer.back() = '\0';
486 // Make a formatted buffer that can contain take 4
487 // bytes per character in case each byte uses the
488 // "\xXX" format and one extra byte for a NULL
489 cstr_buffer.resize (k_max_buf_size * 4 + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490
Greg Clayton737b9322010-09-13 03:32:57 +0000491 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
492 size_t total_cstr_len = 0;
493 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
494 {
495 size_t len = strlen(&data_buffer.front());
496 if (len == 0)
497 break;
498 if (len > bytes_read)
499 len = bytes_read;
500 if (sstr.GetSize() == 0)
501 sstr << '"';
502
503 cstr_length = data.Dump (&sstr,
504 0, // Start offset in "data"
505 eFormatChar, // Print as characters
506 1, // Size of item (1 byte for a char!)
507 len, // How many bytes to print?
508 UINT32_MAX, // num per line
509 LLDB_INVALID_ADDRESS,// base address
510 0, // bitfield bit size
511 0); // bitfield bit offset
512
513 if (len < k_max_buf_size)
514 break;
515 cstr_address += total_cstr_len;
516 }
517 if (sstr.GetSize() > 0)
518 sstr << '"';
519 }
520 }
521 }
522
523 if (sstr.GetSize() > 0)
524 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
525 }
526 else if (ClangASTContext::IsFunctionPointerType (clang_type))
527 {
528 lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
529 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
530
531 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
532 {
533 switch (func_ptr_address_type)
534 {
535 case eAddressTypeInvalid:
536 case eAddressTypeFile:
537 break;
538
539 case eAddressTypeLoad:
540 {
541 Address so_addr;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000542 Target *target = exe_scope->CalculateTarget();
543 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Clayton737b9322010-09-13 03:32:57 +0000544 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000545 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Greg Clayton737b9322010-09-13 03:32:57 +0000546 {
547 so_addr.Dump (&sstr,
548 exe_scope,
549 Address::DumpStyleResolvedDescription,
550 Address::DumpStyleSectionNameOffset);
551 }
552 }
553 }
554 break;
555
556 case eAddressTypeHost:
557 break;
558 }
559 }
560 if (sstr.GetSize() > 0)
561 {
562 m_summary_str.assign (1, '(');
563 m_summary_str.append (sstr.GetData(), sstr.GetSize());
564 m_summary_str.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000565 }
566 }
567 }
568 }
569 }
570 if (m_summary_str.empty())
571 return NULL;
572 return m_summary_str.c_str();
573}
574
Jim Ingham53c47f12010-09-10 23:12:17 +0000575const char *
576ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
577{
578 if (!m_object_desc_str.empty())
579 return m_object_desc_str.c_str();
580
Jim Ingham53c47f12010-09-10 23:12:17 +0000581 if (!GetValueIsValid())
582 return NULL;
583
584 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000585 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000586 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000587
Jim Ingham53c47f12010-09-10 23:12:17 +0000588 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000589
590 lldb::LanguageType language = GetObjectRuntimeLanguage();
591 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
592
593 if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
Jim Ingham53c47f12010-09-10 23:12:17 +0000594 {
595 m_object_desc_str.append (s.GetData());
596 }
Sean Callanan672ad942010-10-23 00:18:49 +0000597
598 if (m_object_desc_str.empty())
599 return NULL;
600 else
601 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000602}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603
604const char *
605ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
606{
607 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000608 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609 {
610 if (UpdateValueIfNeeded(exe_scope))
611 {
612 if (m_value_str.empty())
613 {
614 const Value::ContextType context_type = m_value.GetContextType();
615
616 switch (context_type)
617 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000618 case Value::eContextTypeClangType:
619 case Value::eContextTypeLLDBType:
620 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000622 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 if (clang_type)
624 {
625 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000626 if (m_format == eFormatDefault)
627 m_format = ClangASTType::GetFormat(clang_type);
628
629 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
630 clang_type, // The clang type to display
631 &sstr,
632 m_format, // Format to display this type with
633 m_data, // Data to extract from
634 0, // Byte offset into "m_data"
635 GetByteSize(), // Byte size of item in "m_data"
636 GetBitfieldBitSize(), // Bitfield bit size
637 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000638 m_value_str.swap(sstr.GetString());
639 else
640 m_value_str.clear();
641 }
642 }
643 break;
644
Greg Clayton526e5af2010-11-13 03:52:47 +0000645 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 {
647 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
648 if (reg_info)
649 {
650 StreamString reg_sstr;
651 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
652 m_value_str.swap(reg_sstr.GetString());
653 }
654 }
655 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000656
657 default:
658 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000659 }
660 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000661
662 if (!m_value_did_change && m_old_value_valid)
663 {
664 // The value was gotten successfully, so we consider the
665 // value as changed if the value string differs
666 SetValueDidChange (m_old_value_str != m_value_str);
667 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000668 }
669 }
670 if (m_value_str.empty())
671 return NULL;
672 return m_value_str.c_str();
673}
674
Greg Clayton737b9322010-09-13 03:32:57 +0000675addr_t
Greg Clayton73b472d2010-10-27 03:32:59 +0000676ValueObject::GetAddressOf (lldb::AddressType &address_type, bool scalar_is_load_address)
677{
678 switch (m_value.GetValueType())
679 {
680 case Value::eValueTypeScalar:
681 if (scalar_is_load_address)
682 {
683 address_type = eAddressTypeLoad;
684 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
685 }
686 break;
687
688 case Value::eValueTypeLoadAddress:
689 case Value::eValueTypeFileAddress:
690 case Value::eValueTypeHostAddress:
691 {
692 address_type = m_value.GetValueAddressType ();
693 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
694 }
695 break;
696 }
697 address_type = eAddressTypeInvalid;
698 return LLDB_INVALID_ADDRESS;
699}
700
701addr_t
Greg Clayton737b9322010-09-13 03:32:57 +0000702ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
703{
704 lldb::addr_t address = LLDB_INVALID_ADDRESS;
705 address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +0000706 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000707 {
708 case Value::eValueTypeScalar:
709 if (scalar_is_load_address)
710 {
711 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
712 address_type = eAddressTypeLoad;
713 }
714 break;
715
716 case Value::eValueTypeLoadAddress:
717 case Value::eValueTypeFileAddress:
718 case Value::eValueTypeHostAddress:
719 {
720 uint32_t data_offset = 0;
721 address = m_data.GetPointer(&data_offset);
722 address_type = m_value.GetValueAddressType();
723 if (address_type == eAddressTypeInvalid)
724 address_type = eAddressTypeLoad;
725 }
726 break;
727 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000728
729 if (m_pointers_point_to_load_addrs)
730 address_type = eAddressTypeLoad;
731
Greg Clayton737b9322010-09-13 03:32:57 +0000732 return address;
733}
734
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000735bool
736ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
737{
738 // Make sure our value is up to date first so that our location and location
739 // type is valid.
740 if (!UpdateValueIfNeeded(exe_scope))
741 return false;
742
743 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000744 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000745
746 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000747 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000748 switch (encoding)
749 {
750 case eEncodingInvalid:
751 return false;
752
753 case eEncodingUint:
754 if (byte_size > sizeof(unsigned long long))
755 {
756 return false;
757 }
758 else
759 {
760 unsigned long long ull_val = strtoull(value_str, &end, 0);
761 if (end && *end != '\0')
762 return false;
763 m_value = ull_val;
764 // Limit the bytes in our m_data appropriately.
765 m_value.GetScalar().GetData (m_data, byte_size);
766 }
767 break;
768
769 case eEncodingSint:
770 if (byte_size > sizeof(long long))
771 {
772 return false;
773 }
774 else
775 {
776 long long sll_val = strtoll(value_str, &end, 0);
777 if (end && *end != '\0')
778 return false;
779 m_value = sll_val;
780 // Limit the bytes in our m_data appropriately.
781 m_value.GetScalar().GetData (m_data, byte_size);
782 }
783 break;
784
785 case eEncodingIEEE754:
786 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000787 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000788 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789 if (dst != NULL)
790 {
791 // We are decoding a float into host byte order below, so make
792 // sure m_data knows what it contains.
793 m_data.SetByteOrder(eByteOrderHost);
794 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
795 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000796 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 value_str,
798 dst,
799 byte_size);
800
801 if (converted_byte_size == byte_size)
802 {
803 }
804 }
805 }
806 break;
807
808 case eEncodingVector:
809 return false;
810
811 default:
812 return false;
813 }
814
815 // If we have made it here the value is in m_data and we should write it
816 // out to the target
817 return Write ();
818}
819
820bool
821ValueObject::Write ()
822{
823 // Clear the update ID so the next time we try and read the value
824 // we try and read it again.
825 m_update_id = 0;
826
827 // TODO: when Value has a method to write a value back, call it from here.
828 return false;
829
830}
831
Jim Ingham5a369122010-09-28 01:25:32 +0000832lldb::LanguageType
833ValueObject::GetObjectRuntimeLanguage ()
834{
Greg Clayton73b472d2010-10-27 03:32:59 +0000835 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000836 if (opaque_qual_type == NULL)
837 return lldb::eLanguageTypeC;
838
839 // If the type is a reference, then resolve it to what it refers to first:
840 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
841 if (qual_type->isAnyPointerType())
842 {
843 if (qual_type->isObjCObjectPointerType())
844 return lldb::eLanguageTypeObjC;
845
846 clang::QualType pointee_type (qual_type->getPointeeType());
847 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
848 return lldb::eLanguageTypeC_plus_plus;
849 if (pointee_type->isObjCObjectOrInterfaceType())
850 return lldb::eLanguageTypeObjC;
851 if (pointee_type->isObjCClassType())
852 return lldb::eLanguageTypeObjC;
853 }
854 else
855 {
856 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
857 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000858 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000859 return lldb::eLanguageTypeC_plus_plus;
860 }
861
862 return lldb::eLanguageTypeC;
863}
864
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865void
866ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
867{
868 m_synthetic_children[key] = valobj_sp;
869}
870
871ValueObjectSP
872ValueObject::GetSyntheticChild (const ConstString &key) const
873{
874 ValueObjectSP synthetic_child_sp;
875 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
876 if (pos != m_synthetic_children.end())
877 synthetic_child_sp = pos->second;
878 return synthetic_child_sp;
879}
880
881bool
882ValueObject::IsPointerType ()
883{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000884 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000885}
886
Greg Clayton73b472d2010-10-27 03:32:59 +0000887
888
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889bool
890ValueObject::IsPointerOrReferenceType ()
891{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000892 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893}
894
895ValueObjectSP
896ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
897{
898 ValueObjectSP synthetic_child_sp;
899 if (IsPointerType ())
900 {
901 char index_str[64];
902 snprintf(index_str, sizeof(index_str), "[%i]", index);
903 ConstString index_const_str(index_str);
904 // Check if we have already created a synthetic array member in this
905 // valid object. If we have we will re-use it.
906 synthetic_child_sp = GetSyntheticChild (index_const_str);
907 if (!synthetic_child_sp)
908 {
909 // We haven't made a synthetic array member for INDEX yet, so
910 // lets make one and cache it for any future reference.
911 synthetic_child_sp = CreateChildAtIndex(0, true, index);
912
913 // Cache the value if we got one back...
914 if (synthetic_child_sp)
915 AddSyntheticChild(index_const_str, synthetic_child_sp);
916 }
917 }
918 return synthetic_child_sp;
919}
Jim Ingham22777012010-09-23 02:01:19 +0000920
921bool
922ValueObject::SetDynamicValue ()
923{
924 if (!IsPointerOrReferenceType())
925 return false;
926
927 // Check that the runtime class is correct for determining the most specific class.
928 // If it is a C++ class, see if it is dynamic:
Jim Ingham5a369122010-09-28 01:25:32 +0000929
Jim Ingham22777012010-09-23 02:01:19 +0000930 return true;
931}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000932
933
934void
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000935ValueObject::GetExpressionPath (Stream &s)
936{
937 if (m_parent)
938 {
939 m_parent->GetExpressionPath (s);
940 clang_type_t parent_clang_type = m_parent->GetClangType();
941 if (parent_clang_type)
942 {
943 if (ClangASTContext::IsPointerType(parent_clang_type))
944 {
945 s.PutCString("->");
946 }
947 else if (ClangASTContext::IsAggregateType (parent_clang_type))
948 {
949 if (ClangASTContext::IsArrayType (parent_clang_type) == false &&
950 m_parent->IsBaseClass() == false)
951 s.PutChar('.');
952 }
953 }
954 }
955
956 if (IsBaseClass())
957 {
958 clang_type_t clang_type = GetClangType();
959 std::string cxx_class_name;
960 if (ClangASTContext::GetCXXClassName (clang_type, cxx_class_name))
961 {
962 s << cxx_class_name.c_str() << "::";
963 }
964 }
965 else
966 {
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000967 const char *name = GetName().GetCString();
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000968 if (name)
969 s.PutCString(name);
970 }
971}
972
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000973void
Greg Clayton1d3afba2010-10-05 00:00:42 +0000974ValueObject::DumpValueObject
975(
976 Stream &s,
977 ExecutionContextScope *exe_scope,
978 ValueObject *valobj,
979 const char *root_valobj_name,
980 uint32_t ptr_depth,
981 uint32_t curr_depth,
982 uint32_t max_depth,
983 bool show_types,
984 bool show_location,
985 bool use_objc,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000986 bool scope_already_checked,
987 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +0000988)
989{
990 if (valobj)
991 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000992 clang_type_t clang_type = valobj->GetClangType();
993
Greg Clayton73b472d2010-10-27 03:32:59 +0000994 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000995 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +0000996 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
997 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000998
999 const bool print_valobj = flat_output == false || has_value;
1000
1001 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001002 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001003 if (show_location)
1004 {
1005 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1006 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001007
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001008 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001009
Greg Clayton7c8a9662010-11-02 01:50:16 +00001010 // Always show the type for the top level items.
1011 if (show_types || curr_depth == 0)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001012 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001013
Greg Clayton1d3afba2010-10-05 00:00:42 +00001014
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001015 if (flat_output)
1016 {
1017 valobj->GetExpressionPath(s);
1018 s.PutCString(" =");
1019 }
1020 else
1021 {
1022 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1023 s.Printf ("%s =", name_cstr);
1024 }
1025
1026 if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1027 {
1028 err_cstr = "error: out of scope";
1029 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001030 }
1031
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001032 const char *val_cstr = NULL;
1033
1034 if (err_cstr == NULL)
1035 {
1036 val_cstr = valobj->GetValueAsCString(exe_scope);
1037 err_cstr = valobj->GetError().AsCString();
1038 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001039
1040 if (err_cstr)
1041 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001042 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001043 }
1044 else
1045 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001046 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001047 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001048 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001049 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001050
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001051 if (val_cstr)
1052 s.Printf(" %s", val_cstr);
1053
1054 if (sum_cstr)
1055 s.Printf(" %s", sum_cstr);
1056
1057 if (use_objc)
1058 {
1059 const char *object_desc = valobj->GetObjectDescription(exe_scope);
1060 if (object_desc)
1061 s.Printf(" %s\n", object_desc);
1062 else
Sean Callanan672ad942010-10-23 00:18:49 +00001063 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001064 return;
1065 }
1066 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001067
1068 if (curr_depth < max_depth)
1069 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001070 // We will show children for all concrete types. We won't show
1071 // pointer contents unless a pointer depth has been specified.
1072 // We won't reference contents unless the reference is the
1073 // root object (depth of zero).
1074 bool print_children = true;
1075
1076 // Use a new temporary pointer depth in case we override the
1077 // current pointer depth below...
1078 uint32_t curr_ptr_depth = ptr_depth;
1079
1080 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1081 if (is_ptr || is_ref)
1082 {
1083 // We have a pointer or reference whose value is an address.
1084 // Make sure that address is not NULL
1085 lldb::AddressType ptr_address_type;
1086 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1087 print_children = false;
1088
1089 else if (is_ref && curr_depth == 0)
1090 {
1091 // If this is the root object (depth is zero) that we are showing
1092 // and it is a reference, and no pointer depth has been supplied
1093 // print out what it references. Don't do this at deeper depths
1094 // otherwise we can end up with infinite recursion...
1095 curr_ptr_depth = 1;
1096 }
1097
1098 if (curr_ptr_depth == 0)
1099 print_children = false;
1100 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001101
Greg Clayton73b472d2010-10-27 03:32:59 +00001102 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001103 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001104 const uint32_t num_children = valobj->GetNumChildren();
1105 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001106 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001107 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001108 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001109 if (print_valobj)
1110 s.EOL();
1111 }
1112 else
1113 {
1114 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001115 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001116 s.IndentMore();
1117 }
1118
1119 for (uint32_t idx=0; idx<num_children; ++idx)
1120 {
1121 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1122 if (child_sp.get())
1123 {
1124 DumpValueObject (s,
1125 exe_scope,
1126 child_sp.get(),
1127 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001128 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001129 curr_depth + 1,
1130 max_depth,
1131 show_types,
1132 show_location,
1133 false,
1134 true,
1135 flat_output);
1136 }
1137 }
1138
1139 if (!flat_output)
1140 {
1141 s.IndentLess();
1142 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001143 }
1144 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001145 else if (has_children)
1146 {
1147 // Aggregate, no children...
1148 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001149 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001150 }
1151 else
1152 {
1153 if (print_valobj)
1154 s.EOL();
1155 }
1156
Greg Clayton1d3afba2010-10-05 00:00:42 +00001157 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001158 else
1159 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001160 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001161 }
1162 }
1163 else
1164 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001165 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001166 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001167 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001168 }
1169 }
1170 }
1171 }
1172}
1173
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001174
1175ValueObjectSP
1176ValueObject::CreateConstantValue (ExecutionContextScope *exe_scope, const ConstString &name)
1177{
1178 ValueObjectSP valobj_sp;
1179
1180 if (UpdateValueIfNeeded(exe_scope) && m_error.Success())
1181 {
1182 ExecutionContext exe_ctx;
1183 exe_scope->CalculateExecutionContext(exe_ctx);
1184
1185 clang::ASTContext *ast = GetClangAST ();
1186
1187 DataExtractor data;
1188 data.SetByteOrder (m_data.GetByteOrder());
1189 data.SetAddressByteSize(m_data.GetAddressByteSize());
1190
1191 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1192
1193 valobj_sp.reset (new ValueObjectConstResult (ast,
1194 GetClangType(),
1195 name,
1196 data));
1197 }
1198 else
1199 {
1200 valobj_sp.reset (new ValueObjectConstResult (m_error));
1201 }
1202 return valobj_sp;
1203}
1204
1205lldb::ValueObjectSP
1206ValueObject::Dereference (ExecutionContextScope *exe_scope, Error *error_ptr)
1207{
1208 lldb::ValueObjectSP valobj_sp;
1209 if (IsPointerType())
1210 {
1211 bool omit_empty_base_classes = true;
1212
1213 std::string child_name_str;
1214 uint32_t child_byte_size = 0;
1215 int32_t child_byte_offset = 0;
1216 uint32_t child_bitfield_bit_size = 0;
1217 uint32_t child_bitfield_bit_offset = 0;
1218 bool child_is_base_class = false;
1219 const bool transparent_pointers = false;
1220 clang::ASTContext *clang_ast = GetClangAST();
1221 clang_type_t clang_type = GetClangType();
1222 clang_type_t child_clang_type;
1223 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1224 GetName().GetCString(),
1225 clang_type,
1226 0,
1227 transparent_pointers,
1228 omit_empty_base_classes,
1229 child_name_str,
1230 child_byte_size,
1231 child_byte_offset,
1232 child_bitfield_bit_size,
1233 child_bitfield_bit_offset,
1234 child_is_base_class);
1235 if (child_clang_type)
1236 {
1237 ConstString child_name;
1238 if (!child_name_str.empty())
1239 child_name.SetCString (child_name_str.c_str());
1240
1241 valobj_sp.reset (new ValueObjectChild (this,
1242 clang_ast,
1243 child_clang_type,
1244 child_name,
1245 child_byte_size,
1246 child_byte_offset,
1247 child_bitfield_bit_size,
1248 child_bitfield_bit_offset,
1249 child_is_base_class));
1250 }
1251 }
1252 else
1253 {
1254 if (error_ptr)
1255 error_ptr->SetErrorString("can't dereference a non-pointer value");
1256 }
1257
1258 return valobj_sp;
1259}
1260
1261
1262
1263//lldb::ValueObjectSP
1264//ValueObject::Dereference (ExecutionContextScope *exe_scope, Error *error_ptr)
1265//{
1266// lldb::ValueObjectSP valobj_sp;
1267// if (IsPointerType())
1268// {
1269// UpdateValueIfNeeded(exe_scope);
1270// if (m_error.Success())
1271// {
1272// lldb::AddressType address_type = eAddressTypeInvalid;
1273// const bool scalar_is_load_address = true;
1274// lldb::addr_t addr = GetPointerValue (address_type, scalar_is_load_address);
1275// if (addr != LLDB_INVALID_ADDRESS)
1276// {
1277// switch (address_type)
1278// {
1279// case eAddressTypeInvalid:
1280// if (error_ptr)
1281// error_ptr->SetErrorString("value is not in memory");
1282// break;
1283// case eAddressTypeFile:
1284// case eAddressTypeLoad:
1285// case eAddressTypeHost:
1286// {
1287// clang::ASTContext *ast = GetClangAST();
1288// clang_type_t clang_type = ClangASTType::GetPointeeType (GetClangType());
1289// if (ast && clang_type)
1290// {
1291// std::string name (1, '*');
1292// name.append (m_name.AsCString(""));
1293// valobj_sp.reset (new ValueObjectConstResult (ast,
1294// ClangASTContext::CreatePointerType (ast, clang_type),
1295// ConstString (name.c_str()),
1296// addr,
1297// address_type,
1298// m_data.GetAddressByteSize()));
1299// }
1300// else
1301// {
1302// if (error_ptr)
1303// error_ptr->SetErrorString("invalid clang type info");
1304// }
1305// }
1306// break;
1307// }
1308// }
1309// else
1310// {
1311// if (error_ptr)
1312// error_ptr->SetErrorString("failed to extract pointer value");
1313// }
1314// }
1315// else
1316// {
1317// if (error_ptr)
1318// *error_ptr = m_error;
1319// }
1320// }
1321// else
1322// {
1323// if (error_ptr)
1324// error_ptr->SetErrorString("can't dereference a non-pointer value");
1325// }
1326//
1327// return valobj_sp;
1328//}
1329
1330lldb::ValueObjectSP
1331ValueObject::AddressOf ()
1332{
1333 lldb::ValueObjectSP valobj_sp;
1334
1335 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);
1338 if (addr != LLDB_INVALID_ADDRESS)
1339 {
1340 switch (address_type)
1341 {
1342 case eAddressTypeInvalid:
1343 break;
1344 case eAddressTypeFile:
1345 case eAddressTypeLoad:
1346 case eAddressTypeHost:
1347 {
1348 clang::ASTContext *ast = GetClangAST();
1349 clang_type_t clang_type = GetClangType();
1350 if (ast && clang_type)
1351 {
1352 std::string name (1, '&');
1353 name.append (m_name.AsCString(""));
1354 valobj_sp.reset (new ValueObjectConstResult (ast,
1355 ClangASTContext::CreatePointerType (ast, clang_type),
1356 ConstString (name.c_str()),
1357 addr,
1358 address_type,
1359 m_data.GetAddressByteSize()));
1360 }
1361 }
1362 break;
1363 }
1364 }
1365 return valobj_sp;
1366}
1367