blob: 89fc8b317861b33af7745736d668609da846c211 [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),
Greg Claytone221f822011-01-21 01:59:00 +000067 m_pointers_point_to_load_addrs (false),
68 m_is_deref_of_parent (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069{
70}
71
72//----------------------------------------------------------------------
73// Destructor
74//----------------------------------------------------------------------
75ValueObject::~ValueObject ()
76{
77}
78
79user_id_t
80ValueObject::GetUpdateID() const
81{
82 return m_update_id;
83}
84
85bool
86ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
87{
Greg Claytonb71f3842010-10-05 03:13:51 +000088 // If this is a constant value, then our success is predicated on whether
89 // we have an error or not
90 if (GetIsConstant())
91 return m_error.Success();
92
Chris Lattner30fdc8d2010-06-08 16:52:24 +000093 if (exe_scope)
94 {
95 Process *process = exe_scope->CalculateProcess();
96 if (process)
97 {
98 const user_id_t stop_id = process->GetStopID();
99 if (m_update_id != stop_id)
100 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000101 bool first_update = m_update_id == 0;
Greg Clayton73b953b2010-08-28 00:08:07 +0000102 // Save the old value using swap to avoid a string copy which
103 // also will clear our m_value_str
Greg Clayton288bdf92010-09-02 02:59:18 +0000104 if (m_value_str.empty())
105 {
106 m_old_value_valid = false;
107 }
108 else
109 {
110 m_old_value_valid = true;
111 m_old_value_str.swap (m_value_str);
112 m_value_str.clear();
113 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000114 m_location_str.clear();
115 m_summary_str.clear();
Jim Ingham53c47f12010-09-10 23:12:17 +0000116 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000117
Greg Clayton73b953b2010-08-28 00:08:07 +0000118 const bool value_was_valid = GetValueIsValid();
119 SetValueDidChange (false);
120
121 m_error.Clear();
122
123 // Call the pure virtual function to update the value
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000124 UpdateValue (exe_scope);
Greg Clayton73b953b2010-08-28 00:08:07 +0000125
126 // Update the fact that we tried to update the value for this
Greg Claytoned8a7052010-09-18 03:37:20 +0000127 // value object whether or not we succeed
Greg Clayton73b953b2010-08-28 00:08:07 +0000128 m_update_id = stop_id;
129 bool success = m_error.Success();
130 SetValueIsValid (success);
Greg Clayton288bdf92010-09-02 02:59:18 +0000131
132 if (first_update)
133 SetValueDidChange (false);
134 else if (!m_value_did_change && success == false)
Greg Clayton73b953b2010-08-28 00:08:07 +0000135 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000136 // The value wasn't gotten successfully, so we mark this
137 // as changed if the value used to be valid and now isn't
138 SetValueDidChange (value_was_valid);
Greg Clayton73b953b2010-08-28 00:08:07 +0000139 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140 }
141 }
142 }
143 return m_error.Success();
144}
145
146const DataExtractor &
147ValueObject::GetDataExtractor () const
148{
149 return m_data;
150}
151
152DataExtractor &
153ValueObject::GetDataExtractor ()
154{
155 return m_data;
156}
157
158const Error &
159ValueObject::GetError() const
160{
161 return m_error;
162}
163
164const ConstString &
165ValueObject::GetName() const
166{
167 return m_name;
168}
169
170const char *
171ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
172{
173 if (UpdateValueIfNeeded(exe_scope))
174 {
175 if (m_location_str.empty())
176 {
177 StreamString sstr;
178
179 switch (m_value.GetValueType())
180 {
181 default:
182 break;
183
184 case Value::eValueTypeScalar:
Greg Clayton526e5af2010-11-13 03:52:47 +0000185 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186 {
187 RegisterInfo *reg_info = m_value.GetRegisterInfo();
188 if (reg_info)
189 {
190 if (reg_info->name)
191 m_location_str = reg_info->name;
192 else if (reg_info->alt_name)
193 m_location_str = reg_info->alt_name;
194 break;
195 }
196 }
197 m_location_str = "scalar";
198 break;
199
200 case Value::eValueTypeLoadAddress:
201 case Value::eValueTypeFileAddress:
202 case Value::eValueTypeHostAddress:
203 {
204 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
205 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
206 m_location_str.swap(sstr.GetString());
207 }
208 break;
209 }
210 }
211 }
212 return m_location_str.c_str();
213}
214
215Value &
216ValueObject::GetValue()
217{
218 return m_value;
219}
220
221const Value &
222ValueObject::GetValue() const
223{
224 return m_value;
225}
226
227bool
Greg Clayton8f343b02010-11-04 01:54:29 +0000228ValueObject::ResolveValue (ExecutionContextScope *exe_scope, Scalar &scalar)
229{
230 ExecutionContext exe_ctx;
231 exe_scope->CalculateExecutionContext(exe_ctx);
232 scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
233 return scalar.IsValid();
234}
235
236bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000237ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238{
Greg Clayton288bdf92010-09-02 02:59:18 +0000239 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240}
241
242
243void
244ValueObject::SetValueIsValid (bool b)
245{
Greg Clayton288bdf92010-09-02 02:59:18 +0000246 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247}
248
249bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000250ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000251{
Greg Clayton288bdf92010-09-02 02:59:18 +0000252 GetValueAsCString (exe_scope);
253 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000254}
255
256void
257ValueObject::SetValueDidChange (bool value_changed)
258{
Greg Clayton288bdf92010-09-02 02:59:18 +0000259 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260}
261
262ValueObjectSP
263ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
264{
265 ValueObjectSP child_sp;
266 if (idx < GetNumChildren())
267 {
268 // Check if we have already made the child value object?
269 if (can_create && m_children[idx].get() == NULL)
270 {
271 // No we haven't created the child at this index, so lets have our
272 // subclass do it and cache the result for quick future access.
273 m_children[idx] = CreateChildAtIndex (idx, false, 0);
274 }
275
276 child_sp = m_children[idx];
277 }
278 return child_sp;
279}
280
281uint32_t
282ValueObject::GetIndexOfChildWithName (const ConstString &name)
283{
284 bool omit_empty_base_classes = true;
285 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000286 GetClangType(),
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000287 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000288 omit_empty_base_classes);
289}
290
291ValueObjectSP
292ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
293{
Greg Clayton710dd5a2011-01-08 20:28:42 +0000294 // when getting a child by name, it could be buried inside some base
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000295 // classes (which really aren't part of the expression path), so we
296 // need a vector of indexes that can get us down to the correct child
297 std::vector<uint32_t> child_indexes;
298 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton1be10fc2010-09-29 01:12:09 +0000299 void *clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300 bool omit_empty_base_classes = true;
301 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
302 clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000303 name.GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000304 omit_empty_base_classes,
305 child_indexes);
306 ValueObjectSP child_sp;
307 if (num_child_indexes > 0)
308 {
309 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
310 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
311
312 child_sp = GetChildAtIndex(*pos, can_create);
313 for (++pos; pos != end; ++pos)
314 {
315 if (child_sp)
316 {
317 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
318 child_sp = new_child_sp;
319 }
320 else
321 {
322 child_sp.reset();
323 }
324
325 }
326 }
327 return child_sp;
328}
329
330
331uint32_t
332ValueObject::GetNumChildren ()
333{
Greg Clayton288bdf92010-09-02 02:59:18 +0000334 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 {
336 SetNumChildren (CalculateNumChildren());
337 }
338 return m_children.size();
339}
340void
341ValueObject::SetNumChildren (uint32_t num_children)
342{
Greg Clayton288bdf92010-09-02 02:59:18 +0000343 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000344 m_children.resize(num_children);
345}
346
347void
348ValueObject::SetName (const char *name)
349{
350 m_name.SetCString(name);
351}
352
353void
354ValueObject::SetName (const ConstString &name)
355{
356 m_name = name;
357}
358
359ValueObjectSP
360ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
361{
362 ValueObjectSP valobj_sp;
363 bool omit_empty_base_classes = true;
364
365 std::string child_name_str;
366 uint32_t child_byte_size = 0;
367 int32_t child_byte_offset = 0;
368 uint32_t child_bitfield_bit_size = 0;
369 uint32_t child_bitfield_bit_offset = 0;
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000370 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +0000371 bool child_is_deref_of_parent = false;
372
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373 const bool transparent_pointers = synthetic_array_member == false;
374 clang::ASTContext *clang_ast = GetClangAST();
Greg Clayton73b472d2010-10-27 03:32:59 +0000375 clang_type_t clang_type = GetClangType();
376 clang_type_t child_clang_type;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000377 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000378 GetName().GetCString(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379 clang_type,
380 idx,
381 transparent_pointers,
382 omit_empty_base_classes,
383 child_name_str,
384 child_byte_size,
385 child_byte_offset,
386 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000387 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +0000388 child_is_base_class,
389 child_is_deref_of_parent);
Greg Clayton97a43712011-01-08 22:26:47 +0000390 if (child_clang_type && child_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000391 {
392 if (synthetic_index)
393 child_byte_offset += child_byte_size * synthetic_index;
394
395 ConstString child_name;
396 if (!child_name_str.empty())
397 child_name.SetCString (child_name_str.c_str());
398
399 valobj_sp.reset (new ValueObjectChild (this,
400 clang_ast,
401 child_clang_type,
402 child_name,
403 child_byte_size,
404 child_byte_offset,
405 child_bitfield_bit_size,
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000406 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +0000407 child_is_base_class,
408 child_is_deref_of_parent));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000409 if (m_pointers_point_to_load_addrs)
410 valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000411 }
412 return valobj_sp;
413}
414
415const char *
416ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
417{
418 if (UpdateValueIfNeeded (exe_scope))
419 {
420 if (m_summary_str.empty())
421 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000422 clang_type_t clang_type = GetClangType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423
424 // See if this is a pointer to a C string?
Greg Clayton737b9322010-09-13 03:32:57 +0000425 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000426 {
Greg Clayton737b9322010-09-13 03:32:57 +0000427 StreamString sstr;
Greg Clayton73b472d2010-10-27 03:32:59 +0000428 clang_type_t elem_or_pointee_clang_type;
429 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000430 GetClangAST(),
431 &elem_or_pointee_clang_type));
Greg Clayton737b9322010-09-13 03:32:57 +0000432
Greg Clayton73b472d2010-10-27 03:32:59 +0000433 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
434 ClangASTContext::IsCharType (elem_or_pointee_clang_type))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 {
Greg Clayton737b9322010-09-13 03:32:57 +0000436 Process *process = exe_scope->CalculateProcess();
437 if (process != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000439 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
Greg Clayton737b9322010-09-13 03:32:57 +0000440 lldb::AddressType cstr_address_type = eAddressTypeInvalid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441
Greg Clayton73b472d2010-10-27 03:32:59 +0000442 size_t cstr_len = 0;
443 if (type_flags.Test (ClangASTContext::eTypeIsArray))
444 {
445 // We have an array
446 cstr_len = ClangASTContext::GetArraySize (clang_type);
447 cstr_address = GetAddressOf (cstr_address_type, true);
448 }
449 else
450 {
451 // We have a pointer
452 cstr_address = GetPointerValue (cstr_address_type, true);
453 }
Greg Clayton737b9322010-09-13 03:32:57 +0000454 if (cstr_address != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 {
Greg Clayton737b9322010-09-13 03:32:57 +0000456 DataExtractor data;
457 size_t bytes_read = 0;
458 std::vector<char> data_buffer;
Greg Clayton737b9322010-09-13 03:32:57 +0000459 Error error;
Greg Clayton73b472d2010-10-27 03:32:59 +0000460 if (cstr_len > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000461 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000462 data_buffer.resize(cstr_len);
Greg Clayton737b9322010-09-13 03:32:57 +0000463 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 Clayton23af0d82011-01-17 05:51:02 +0000468 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
Greg Clayton737b9322010-09-13 03:32:57 +0000477 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';
Greg Clayton23af0d82011-01-17 05:51:02 +0000486
487 sstr << '"';
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000488
Greg Clayton737b9322010-09-13 03:32:57 +0000489 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
Greg Clayton737b9322010-09-13 03:32:57 +0000490 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
491 {
492 size_t len = strlen(&data_buffer.front());
493 if (len == 0)
494 break;
495 if (len > bytes_read)
496 len = bytes_read;
Greg Clayton737b9322010-09-13 03:32:57 +0000497
Greg Clayton23af0d82011-01-17 05:51:02 +0000498 data.Dump (&sstr,
499 0, // Start offset in "data"
500 eFormatChar, // Print as characters
501 1, // Size of item (1 byte for a char!)
502 len, // How many bytes to print?
503 UINT32_MAX, // num per line
504 LLDB_INVALID_ADDRESS,// base address
505 0, // bitfield bit size
506 0); // bitfield bit offset
507
Greg Clayton737b9322010-09-13 03:32:57 +0000508 if (len < k_max_buf_size)
509 break;
Greg Clayton23af0d82011-01-17 05:51:02 +0000510 cstr_address += k_max_buf_size;
Greg Clayton737b9322010-09-13 03:32:57 +0000511 }
Greg Clayton23af0d82011-01-17 05:51:02 +0000512 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000513 }
514 }
515 }
516
517 if (sstr.GetSize() > 0)
518 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
519 }
520 else if (ClangASTContext::IsFunctionPointerType (clang_type))
521 {
522 lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
523 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
524
525 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
526 {
527 switch (func_ptr_address_type)
528 {
529 case eAddressTypeInvalid:
530 case eAddressTypeFile:
531 break;
532
533 case eAddressTypeLoad:
534 {
535 Address so_addr;
Greg Claytonf5e56de2010-09-14 23:36:40 +0000536 Target *target = exe_scope->CalculateTarget();
537 if (target && target->GetSectionLoadList().IsEmpty() == false)
Greg Clayton737b9322010-09-13 03:32:57 +0000538 {
Greg Claytonf5e56de2010-09-14 23:36:40 +0000539 if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
Greg Clayton737b9322010-09-13 03:32:57 +0000540 {
541 so_addr.Dump (&sstr,
542 exe_scope,
543 Address::DumpStyleResolvedDescription,
544 Address::DumpStyleSectionNameOffset);
545 }
546 }
547 }
548 break;
549
550 case eAddressTypeHost:
551 break;
552 }
553 }
554 if (sstr.GetSize() > 0)
555 {
556 m_summary_str.assign (1, '(');
557 m_summary_str.append (sstr.GetData(), sstr.GetSize());
558 m_summary_str.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000559 }
560 }
561 }
562 }
563 }
564 if (m_summary_str.empty())
565 return NULL;
566 return m_summary_str.c_str();
567}
568
Jim Ingham53c47f12010-09-10 23:12:17 +0000569const char *
570ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
571{
572 if (!m_object_desc_str.empty())
573 return m_object_desc_str.c_str();
574
Jim Ingham53c47f12010-09-10 23:12:17 +0000575 if (!GetValueIsValid())
576 return NULL;
577
578 Process *process = exe_scope->CalculateProcess();
Jim Ingham5a369122010-09-28 01:25:32 +0000579 if (process == NULL)
Jim Ingham53c47f12010-09-10 23:12:17 +0000580 return NULL;
Jim Ingham5a369122010-09-28 01:25:32 +0000581
Jim Ingham53c47f12010-09-10 23:12:17 +0000582 StreamString s;
Jim Ingham5a369122010-09-28 01:25:32 +0000583
584 lldb::LanguageType language = GetObjectRuntimeLanguage();
585 LanguageRuntime *runtime = process->GetLanguageRuntime(language);
586
Jim Inghama2cf2632010-12-23 02:29:54 +0000587 if (runtime == NULL)
588 {
589 // Aw, hell, if the things a pointer, let's try ObjC anyway...
590 clang_type_t opaque_qual_type = GetClangType();
591 if (opaque_qual_type != NULL)
592 {
593 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
594 if (qual_type->isAnyPointerType())
595 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
596 }
597 }
598
Jim Ingham5a369122010-09-28 01:25:32 +0000599 if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
Jim Ingham53c47f12010-09-10 23:12:17 +0000600 {
601 m_object_desc_str.append (s.GetData());
602 }
Sean Callanan672ad942010-10-23 00:18:49 +0000603
604 if (m_object_desc_str.empty())
605 return NULL;
606 else
607 return m_object_desc_str.c_str();
Jim Ingham53c47f12010-09-10 23:12:17 +0000608}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000609
610const char *
611ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
612{
613 // If our byte size is zero this is an aggregate type that has children
Greg Clayton1be10fc2010-09-29 01:12:09 +0000614 if (ClangASTContext::IsAggregateType (GetClangType()) == false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000615 {
616 if (UpdateValueIfNeeded(exe_scope))
617 {
618 if (m_value_str.empty())
619 {
620 const Value::ContextType context_type = m_value.GetContextType();
621
622 switch (context_type)
623 {
Greg Clayton526e5af2010-11-13 03:52:47 +0000624 case Value::eContextTypeClangType:
625 case Value::eContextTypeLLDBType:
626 case Value::eContextTypeVariable:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000627 {
Greg Clayton73b472d2010-10-27 03:32:59 +0000628 clang_type_t clang_type = GetClangType ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629 if (clang_type)
630 {
631 StreamString sstr;
Greg Clayton32c40852010-10-06 03:09:11 +0000632 if (m_format == eFormatDefault)
633 m_format = ClangASTType::GetFormat(clang_type);
634
635 if (ClangASTType::DumpTypeValue (GetClangAST(), // The clang AST
636 clang_type, // The clang type to display
637 &sstr,
638 m_format, // Format to display this type with
639 m_data, // Data to extract from
640 0, // Byte offset into "m_data"
641 GetByteSize(), // Byte size of item in "m_data"
642 GetBitfieldBitSize(), // Bitfield bit size
643 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000644 m_value_str.swap(sstr.GetString());
645 else
646 m_value_str.clear();
647 }
648 }
649 break;
650
Greg Clayton526e5af2010-11-13 03:52:47 +0000651 case Value::eContextTypeRegisterInfo:
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652 {
653 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
654 if (reg_info)
655 {
656 StreamString reg_sstr;
657 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
658 m_value_str.swap(reg_sstr.GetString());
659 }
660 }
661 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000662
663 default:
664 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 }
666 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000667
668 if (!m_value_did_change && m_old_value_valid)
669 {
670 // The value was gotten successfully, so we consider the
671 // value as changed if the value string differs
672 SetValueDidChange (m_old_value_str != m_value_str);
673 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674 }
675 }
676 if (m_value_str.empty())
677 return NULL;
678 return m_value_str.c_str();
679}
680
Greg Clayton737b9322010-09-13 03:32:57 +0000681addr_t
Greg Clayton73b472d2010-10-27 03:32:59 +0000682ValueObject::GetAddressOf (lldb::AddressType &address_type, bool scalar_is_load_address)
683{
684 switch (m_value.GetValueType())
685 {
686 case Value::eValueTypeScalar:
687 if (scalar_is_load_address)
688 {
689 address_type = eAddressTypeLoad;
690 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
691 }
692 break;
693
694 case Value::eValueTypeLoadAddress:
695 case Value::eValueTypeFileAddress:
696 case Value::eValueTypeHostAddress:
697 {
698 address_type = m_value.GetValueAddressType ();
699 return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
700 }
701 break;
702 }
703 address_type = eAddressTypeInvalid;
704 return LLDB_INVALID_ADDRESS;
705}
706
707addr_t
Greg Clayton737b9322010-09-13 03:32:57 +0000708ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
709{
710 lldb::addr_t address = LLDB_INVALID_ADDRESS;
711 address_type = eAddressTypeInvalid;
Greg Clayton73b472d2010-10-27 03:32:59 +0000712 switch (m_value.GetValueType())
Greg Clayton737b9322010-09-13 03:32:57 +0000713 {
714 case Value::eValueTypeScalar:
715 if (scalar_is_load_address)
716 {
717 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
718 address_type = eAddressTypeLoad;
719 }
720 break;
721
722 case Value::eValueTypeLoadAddress:
723 case Value::eValueTypeFileAddress:
724 case Value::eValueTypeHostAddress:
725 {
726 uint32_t data_offset = 0;
727 address = m_data.GetPointer(&data_offset);
728 address_type = m_value.GetValueAddressType();
729 if (address_type == eAddressTypeInvalid)
730 address_type = eAddressTypeLoad;
731 }
732 break;
733 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +0000734
735 if (m_pointers_point_to_load_addrs)
736 address_type = eAddressTypeLoad;
737
Greg Clayton737b9322010-09-13 03:32:57 +0000738 return address;
739}
740
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000741bool
742ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
743{
744 // Make sure our value is up to date first so that our location and location
745 // type is valid.
746 if (!UpdateValueIfNeeded(exe_scope))
747 return false;
748
749 uint32_t count = 0;
Greg Clayton1be10fc2010-09-29 01:12:09 +0000750 lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751
752 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000753 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 switch (encoding)
755 {
756 case eEncodingInvalid:
757 return false;
758
759 case eEncodingUint:
760 if (byte_size > sizeof(unsigned long long))
761 {
762 return false;
763 }
764 else
765 {
766 unsigned long long ull_val = strtoull(value_str, &end, 0);
767 if (end && *end != '\0')
768 return false;
769 m_value = ull_val;
770 // Limit the bytes in our m_data appropriately.
771 m_value.GetScalar().GetData (m_data, byte_size);
772 }
773 break;
774
775 case eEncodingSint:
776 if (byte_size > sizeof(long long))
777 {
778 return false;
779 }
780 else
781 {
782 long long sll_val = strtoll(value_str, &end, 0);
783 if (end && *end != '\0')
784 return false;
785 m_value = sll_val;
786 // Limit the bytes in our m_data appropriately.
787 m_value.GetScalar().GetData (m_data, byte_size);
788 }
789 break;
790
791 case eEncodingIEEE754:
792 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000794 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795 if (dst != NULL)
796 {
797 // We are decoding a float into host byte order below, so make
798 // sure m_data knows what it contains.
799 m_data.SetByteOrder(eByteOrderHost);
800 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
801 GetClangAST(),
Greg Clayton1be10fc2010-09-29 01:12:09 +0000802 GetClangType(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803 value_str,
804 dst,
805 byte_size);
806
807 if (converted_byte_size == byte_size)
808 {
809 }
810 }
811 }
812 break;
813
814 case eEncodingVector:
815 return false;
816
817 default:
818 return false;
819 }
820
821 // If we have made it here the value is in m_data and we should write it
822 // out to the target
823 return Write ();
824}
825
826bool
827ValueObject::Write ()
828{
829 // Clear the update ID so the next time we try and read the value
830 // we try and read it again.
831 m_update_id = 0;
832
833 // TODO: when Value has a method to write a value back, call it from here.
834 return false;
835
836}
837
Jim Ingham5a369122010-09-28 01:25:32 +0000838lldb::LanguageType
839ValueObject::GetObjectRuntimeLanguage ()
840{
Greg Clayton73b472d2010-10-27 03:32:59 +0000841 clang_type_t opaque_qual_type = GetClangType();
Jim Ingham5a369122010-09-28 01:25:32 +0000842 if (opaque_qual_type == NULL)
843 return lldb::eLanguageTypeC;
844
845 // If the type is a reference, then resolve it to what it refers to first:
846 clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
847 if (qual_type->isAnyPointerType())
848 {
849 if (qual_type->isObjCObjectPointerType())
850 return lldb::eLanguageTypeObjC;
851
852 clang::QualType pointee_type (qual_type->getPointeeType());
853 if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
854 return lldb::eLanguageTypeC_plus_plus;
855 if (pointee_type->isObjCObjectOrInterfaceType())
856 return lldb::eLanguageTypeObjC;
857 if (pointee_type->isObjCClassType())
858 return lldb::eLanguageTypeObjC;
859 }
860 else
861 {
862 if (ClangASTContext::IsObjCClassType (opaque_qual_type))
863 return lldb::eLanguageTypeObjC;
Johnny Chend440bcc2010-09-28 16:10:54 +0000864 if (ClangASTContext::IsCXXClassType (opaque_qual_type))
Jim Ingham5a369122010-09-28 01:25:32 +0000865 return lldb::eLanguageTypeC_plus_plus;
866 }
867
868 return lldb::eLanguageTypeC;
869}
870
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871void
872ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
873{
874 m_synthetic_children[key] = valobj_sp;
875}
876
877ValueObjectSP
878ValueObject::GetSyntheticChild (const ConstString &key) const
879{
880 ValueObjectSP synthetic_child_sp;
881 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
882 if (pos != m_synthetic_children.end())
883 synthetic_child_sp = pos->second;
884 return synthetic_child_sp;
885}
886
887bool
888ValueObject::IsPointerType ()
889{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000890 return ClangASTContext::IsPointerType (GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891}
892
Greg Clayton73b472d2010-10-27 03:32:59 +0000893
894
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000895bool
896ValueObject::IsPointerOrReferenceType ()
897{
Greg Clayton1be10fc2010-09-29 01:12:09 +0000898 return ClangASTContext::IsPointerOrReferenceType(GetClangType());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000899}
900
901ValueObjectSP
902ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
903{
904 ValueObjectSP synthetic_child_sp;
905 if (IsPointerType ())
906 {
907 char index_str[64];
908 snprintf(index_str, sizeof(index_str), "[%i]", index);
909 ConstString index_const_str(index_str);
910 // Check if we have already created a synthetic array member in this
911 // valid object. If we have we will re-use it.
912 synthetic_child_sp = GetSyntheticChild (index_const_str);
913 if (!synthetic_child_sp)
914 {
915 // We haven't made a synthetic array member for INDEX yet, so
916 // lets make one and cache it for any future reference.
917 synthetic_child_sp = CreateChildAtIndex(0, true, index);
918
919 // Cache the value if we got one back...
920 if (synthetic_child_sp)
921 AddSyntheticChild(index_const_str, synthetic_child_sp);
922 }
923 }
924 return synthetic_child_sp;
925}
Jim Ingham22777012010-09-23 02:01:19 +0000926
927bool
928ValueObject::SetDynamicValue ()
929{
930 if (!IsPointerOrReferenceType())
931 return false;
932
933 // Check that the runtime class is correct for determining the most specific class.
934 // If it is a C++ class, see if it is dynamic:
Jim Ingham5a369122010-09-28 01:25:32 +0000935
Jim Ingham22777012010-09-23 02:01:19 +0000936 return true;
937}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000938
Greg Claytone221f822011-01-21 01:59:00 +0000939bool
940ValueObject::GetBaseClassPath (Stream &s)
941{
942 if (IsBaseClass())
943 {
944 bool parent_had_base_class = m_parent && m_parent->GetBaseClassPath (s);
945 clang_type_t clang_type = GetClangType();
946 std::string cxx_class_name;
947 bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
948 if (this_had_base_class)
949 {
950 if (parent_had_base_class)
951 s.PutCString("::");
952 s.PutCString(cxx_class_name.c_str());
953 }
954 return parent_had_base_class || this_had_base_class;
955 }
956 return false;
957}
958
959
960ValueObject *
961ValueObject::GetNonBaseClassParent()
962{
963 if (m_parent)
964 {
965 if (m_parent->IsBaseClass())
966 return m_parent->GetNonBaseClassParent();
967 else
968 return m_parent;
969 }
970 return NULL;
971}
Greg Clayton1d3afba2010-10-05 00:00:42 +0000972
973void
Greg Clayton6beaaa62011-01-17 03:46:26 +0000974ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000975{
Greg Claytone221f822011-01-21 01:59:00 +0000976 const bool is_deref_of_parent = IsDereferenceOfParent ();
977
978 if (is_deref_of_parent)
979 s.PutCString("*(");
980
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000981 if (m_parent)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000982 m_parent->GetExpressionPath (s, qualify_cxx_base_classes);
Greg Claytone221f822011-01-21 01:59:00 +0000983
984 if (!IsBaseClass())
985 {
986 if (!is_deref_of_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000987 {
Greg Claytone221f822011-01-21 01:59:00 +0000988 ValueObject *non_base_class_parent = GetNonBaseClassParent();
989 if (non_base_class_parent)
Greg Clayton8f92f0a2010-10-14 22:52:14 +0000990 {
Greg Claytone221f822011-01-21 01:59:00 +0000991 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
992 if (non_base_class_parent_clang_type)
993 {
994 const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
995
996 if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
997 {
998 s.PutCString("->");
999 }
1000 else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1001 !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1002 {
1003 s.PutChar('.');
1004 }
1005 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001006 }
Greg Claytone221f822011-01-21 01:59:00 +00001007
1008 const char *name = GetName().GetCString();
1009 if (name)
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001010 {
Greg Claytone221f822011-01-21 01:59:00 +00001011 if (qualify_cxx_base_classes)
1012 {
1013 if (GetBaseClassPath (s))
1014 s.PutCString("::");
1015 }
1016 s.PutCString(name);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001017 }
1018 }
1019 }
1020
Greg Claytone221f822011-01-21 01:59:00 +00001021 if (is_deref_of_parent)
1022 s.PutChar(')');
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001023}
1024
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001025void
Greg Clayton1d3afba2010-10-05 00:00:42 +00001026ValueObject::DumpValueObject
1027(
1028 Stream &s,
1029 ExecutionContextScope *exe_scope,
1030 ValueObject *valobj,
1031 const char *root_valobj_name,
1032 uint32_t ptr_depth,
1033 uint32_t curr_depth,
1034 uint32_t max_depth,
1035 bool show_types,
1036 bool show_location,
1037 bool use_objc,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001038 bool scope_already_checked,
1039 bool flat_output
Greg Clayton1d3afba2010-10-05 00:00:42 +00001040)
1041{
1042 if (valobj)
1043 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001044 clang_type_t clang_type = valobj->GetClangType();
1045
Greg Clayton73b472d2010-10-27 03:32:59 +00001046 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001047 const char *err_cstr = NULL;
Greg Clayton73b472d2010-10-27 03:32:59 +00001048 const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1049 const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001050
1051 const bool print_valobj = flat_output == false || has_value;
1052
1053 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001054 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001055 if (show_location)
1056 {
1057 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1058 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001059
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001060 s.Indent();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001061
Greg Clayton7c8a9662010-11-02 01:50:16 +00001062 // Always show the type for the top level items.
Greg Claytone221f822011-01-21 01:59:00 +00001063 if (show_types || (curr_depth == 0 && !flat_output))
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001064 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
Greg Clayton1d3afba2010-10-05 00:00:42 +00001065
Greg Clayton1d3afba2010-10-05 00:00:42 +00001066
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001067 if (flat_output)
1068 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001069 // If we are showing types, also qualify the C++ base classes
1070 const bool qualify_cxx_base_classes = show_types;
1071 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001072 s.PutCString(" =");
1073 }
1074 else
1075 {
1076 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1077 s.Printf ("%s =", name_cstr);
1078 }
1079
1080 if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1081 {
1082 err_cstr = "error: out of scope";
1083 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001084 }
1085
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001086 const char *val_cstr = NULL;
1087
1088 if (err_cstr == NULL)
1089 {
1090 val_cstr = valobj->GetValueAsCString(exe_scope);
1091 err_cstr = valobj->GetError().AsCString();
1092 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001093
1094 if (err_cstr)
1095 {
Greg Clayton7c8a9662010-11-02 01:50:16 +00001096 s.Printf (" error: %s\n", err_cstr);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001097 }
1098 else
1099 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001100 const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001101 if (print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001102 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001103 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
Greg Clayton1d3afba2010-10-05 00:00:42 +00001104
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001105 if (val_cstr)
1106 s.Printf(" %s", val_cstr);
1107
1108 if (sum_cstr)
1109 s.Printf(" %s", sum_cstr);
1110
1111 if (use_objc)
1112 {
1113 const char *object_desc = valobj->GetObjectDescription(exe_scope);
1114 if (object_desc)
1115 s.Printf(" %s\n", object_desc);
1116 else
Sean Callanan672ad942010-10-23 00:18:49 +00001117 s.Printf (" [no Objective-C description available]\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001118 return;
1119 }
1120 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001121
1122 if (curr_depth < max_depth)
1123 {
Greg Clayton73b472d2010-10-27 03:32:59 +00001124 // We will show children for all concrete types. We won't show
1125 // pointer contents unless a pointer depth has been specified.
1126 // We won't reference contents unless the reference is the
1127 // root object (depth of zero).
1128 bool print_children = true;
1129
1130 // Use a new temporary pointer depth in case we override the
1131 // current pointer depth below...
1132 uint32_t curr_ptr_depth = ptr_depth;
1133
1134 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1135 if (is_ptr || is_ref)
1136 {
1137 // We have a pointer or reference whose value is an address.
1138 // Make sure that address is not NULL
1139 lldb::AddressType ptr_address_type;
1140 if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1141 print_children = false;
1142
1143 else if (is_ref && curr_depth == 0)
1144 {
1145 // If this is the root object (depth is zero) that we are showing
1146 // and it is a reference, and no pointer depth has been supplied
1147 // print out what it references. Don't do this at deeper depths
1148 // otherwise we can end up with infinite recursion...
1149 curr_ptr_depth = 1;
1150 }
1151
1152 if (curr_ptr_depth == 0)
1153 print_children = false;
1154 }
Greg Clayton1d3afba2010-10-05 00:00:42 +00001155
Greg Clayton73b472d2010-10-27 03:32:59 +00001156 if (print_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001157 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001158 const uint32_t num_children = valobj->GetNumChildren();
1159 if (num_children)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001160 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001161 if (flat_output)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001162 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001163 if (print_valobj)
1164 s.EOL();
1165 }
1166 else
1167 {
1168 if (print_valobj)
Greg Clayton93aa84e2010-10-29 04:59:35 +00001169 s.PutCString(is_ref ? ": {\n" : " {\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001170 s.IndentMore();
1171 }
1172
1173 for (uint32_t idx=0; idx<num_children; ++idx)
1174 {
1175 ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1176 if (child_sp.get())
1177 {
1178 DumpValueObject (s,
1179 exe_scope,
1180 child_sp.get(),
1181 NULL,
Greg Clayton73b472d2010-10-27 03:32:59 +00001182 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001183 curr_depth + 1,
1184 max_depth,
1185 show_types,
1186 show_location,
1187 false,
1188 true,
1189 flat_output);
1190 }
1191 }
1192
1193 if (!flat_output)
1194 {
1195 s.IndentLess();
1196 s.Indent("}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001197 }
1198 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001199 else if (has_children)
1200 {
1201 // Aggregate, no children...
1202 if (print_valobj)
Greg Clayton73b472d2010-10-27 03:32:59 +00001203 s.PutCString(" {}\n");
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001204 }
1205 else
1206 {
1207 if (print_valobj)
1208 s.EOL();
1209 }
1210
Greg Clayton1d3afba2010-10-05 00:00:42 +00001211 }
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001212 else
1213 {
Greg Clayton1d3afba2010-10-05 00:00:42 +00001214 s.EOL();
Greg Clayton1d3afba2010-10-05 00:00:42 +00001215 }
1216 }
1217 else
1218 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001219 if (has_children && print_valobj)
Greg Clayton1d3afba2010-10-05 00:00:42 +00001220 {
Greg Clayton8f92f0a2010-10-14 22:52:14 +00001221 s.PutCString("{...}\n");
Greg Clayton1d3afba2010-10-05 00:00:42 +00001222 }
1223 }
1224 }
1225 }
1226}
1227
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001228
1229ValueObjectSP
1230ValueObject::CreateConstantValue (ExecutionContextScope *exe_scope, const ConstString &name)
1231{
1232 ValueObjectSP valobj_sp;
1233
1234 if (UpdateValueIfNeeded(exe_scope) && m_error.Success())
1235 {
1236 ExecutionContext exe_ctx;
1237 exe_scope->CalculateExecutionContext(exe_ctx);
1238
1239 clang::ASTContext *ast = GetClangAST ();
1240
1241 DataExtractor data;
1242 data.SetByteOrder (m_data.GetByteOrder());
1243 data.SetAddressByteSize(m_data.GetAddressByteSize());
1244
1245 m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1246
1247 valobj_sp.reset (new ValueObjectConstResult (ast,
1248 GetClangType(),
1249 name,
1250 data));
1251 }
1252 else
1253 {
1254 valobj_sp.reset (new ValueObjectConstResult (m_error));
1255 }
1256 return valobj_sp;
1257}
1258
1259lldb::ValueObjectSP
Greg Claytonaf67cec2010-12-20 20:49:23 +00001260ValueObject::Dereference (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001261{
1262 lldb::ValueObjectSP valobj_sp;
Greg Clayton54979cd2010-12-15 05:08:08 +00001263 const bool is_pointer_type = IsPointerType();
1264 if (is_pointer_type)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001265 {
1266 bool omit_empty_base_classes = true;
1267
1268 std::string child_name_str;
1269 uint32_t child_byte_size = 0;
1270 int32_t child_byte_offset = 0;
1271 uint32_t child_bitfield_bit_size = 0;
1272 uint32_t child_bitfield_bit_offset = 0;
1273 bool child_is_base_class = false;
Greg Claytone221f822011-01-21 01:59:00 +00001274 bool child_is_deref_of_parent = false;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001275 const bool transparent_pointers = false;
1276 clang::ASTContext *clang_ast = GetClangAST();
1277 clang_type_t clang_type = GetClangType();
1278 clang_type_t child_clang_type;
1279 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1280 GetName().GetCString(),
1281 clang_type,
1282 0,
1283 transparent_pointers,
1284 omit_empty_base_classes,
1285 child_name_str,
1286 child_byte_size,
1287 child_byte_offset,
1288 child_bitfield_bit_size,
1289 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001290 child_is_base_class,
1291 child_is_deref_of_parent);
Greg Clayton3e06bd92011-01-09 21:07:35 +00001292 if (child_clang_type && child_byte_size)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001293 {
1294 ConstString child_name;
1295 if (!child_name_str.empty())
1296 child_name.SetCString (child_name_str.c_str());
1297
1298 valobj_sp.reset (new ValueObjectChild (this,
1299 clang_ast,
1300 child_clang_type,
1301 child_name,
1302 child_byte_size,
1303 child_byte_offset,
1304 child_bitfield_bit_size,
1305 child_bitfield_bit_offset,
Greg Claytone221f822011-01-21 01:59:00 +00001306 child_is_base_class,
1307 child_is_deref_of_parent));
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001308 }
1309 }
Greg Clayton54979cd2010-12-15 05:08:08 +00001310
1311 if (valobj_sp)
1312 {
1313 error.Clear();
1314 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001315 else
1316 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001317 StreamString strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001318 GetExpressionPath(strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001319
1320 if (is_pointer_type)
1321 error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1322 else
1323 error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001324 }
1325
1326 return valobj_sp;
1327}
1328
Greg Clayton54979cd2010-12-15 05:08:08 +00001329 lldb::ValueObjectSP
1330ValueObject::AddressOf (Error &error)
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001331{
1332 lldb::ValueObjectSP valobj_sp;
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001333 lldb::AddressType address_type = eAddressTypeInvalid;
1334 const bool scalar_is_load_address = false;
1335 lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
Greg Clayton54979cd2010-12-15 05:08:08 +00001336 error.Clear();
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001337 if (addr != LLDB_INVALID_ADDRESS)
1338 {
1339 switch (address_type)
1340 {
Greg Clayton54979cd2010-12-15 05:08:08 +00001341 default:
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001342 case eAddressTypeInvalid:
Greg Clayton54979cd2010-12-15 05:08:08 +00001343 {
1344 StreamString expr_path_strm;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001345 GetExpressionPath(expr_path_strm, true);
Greg Clayton54979cd2010-12-15 05:08:08 +00001346 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1347 }
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001348 break;
Greg Clayton54979cd2010-12-15 05:08:08 +00001349
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001350 case eAddressTypeFile:
1351 case eAddressTypeLoad:
1352 case eAddressTypeHost:
1353 {
1354 clang::ASTContext *ast = GetClangAST();
1355 clang_type_t clang_type = GetClangType();
1356 if (ast && clang_type)
1357 {
1358 std::string name (1, '&');
1359 name.append (m_name.AsCString(""));
1360 valobj_sp.reset (new ValueObjectConstResult (ast,
1361 ClangASTContext::CreatePointerType (ast, clang_type),
1362 ConstString (name.c_str()),
1363 addr,
Sean Callanan92adcac2011-01-13 08:53:35 +00001364 eAddressTypeInvalid,
Greg Clayton8b2fe6d2010-12-14 02:59:59 +00001365 m_data.GetAddressByteSize()));
1366 }
1367 }
1368 break;
1369 }
1370 }
1371 return valobj_sp;
1372}
1373