blob: 72984c6c08241d894a8a22553e76b26da8e39e81 [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
13// C++ Includes
14// Other libraries and framework includes
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "llvm/Support/raw_ostream.h"
16
17// Project includes
18#include "lldb/Core/DataBufferHeap.h"
19#include "lldb/Core/StreamString.h"
20#include "lldb/Core/ValueObjectChild.h"
21#include "lldb/Core/ValueObjectList.h"
22
23#include "lldb/Symbol/ClangASTContext.h"
24#include "lldb/Symbol/Type.h"
25
26#include "lldb/Target/Process.h"
27#include "lldb/Target/RegisterContext.h"
28#include "lldb/Target/Thread.h"
Eli Friedman88966972010-06-09 08:50:27 +000029#include <stdlib.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030
31using namespace lldb;
32using namespace lldb_private;
33
34static lldb::user_id_t g_value_obj_uid = 0;
35
36//----------------------------------------------------------------------
37// ValueObject constructor
38//----------------------------------------------------------------------
39ValueObject::ValueObject () :
40 UserID (++g_value_obj_uid), // Unique identifier for every value object
41 m_update_id (0), // Value object lists always start at 1, value objects start at zero
42 m_name (),
43 m_data (),
44 m_value (),
45 m_error (),
46 m_flags (),
47 m_value_str(),
48 m_location_str(),
49 m_summary_str(),
50 m_children(),
51 m_synthetic_children()
52{
53}
54
55//----------------------------------------------------------------------
56// Destructor
57//----------------------------------------------------------------------
58ValueObject::~ValueObject ()
59{
60}
61
62user_id_t
63ValueObject::GetUpdateID() const
64{
65 return m_update_id;
66}
67
68bool
69ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
70{
71 if (exe_scope)
72 {
73 Process *process = exe_scope->CalculateProcess();
74 if (process)
75 {
76 const user_id_t stop_id = process->GetStopID();
77 if (m_update_id != stop_id)
78 {
79 m_value_str.clear();
80 m_location_str.clear();
81 m_summary_str.clear();
82
83 UpdateValue (exe_scope);
84 if (m_error.Success())
85 m_update_id = stop_id;
86 }
87 }
88 }
89 return m_error.Success();
90}
91
92const DataExtractor &
93ValueObject::GetDataExtractor () const
94{
95 return m_data;
96}
97
98DataExtractor &
99ValueObject::GetDataExtractor ()
100{
101 return m_data;
102}
103
104const Error &
105ValueObject::GetError() const
106{
107 return m_error;
108}
109
110const ConstString &
111ValueObject::GetName() const
112{
113 return m_name;
114}
115
116const char *
117ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
118{
119 if (UpdateValueIfNeeded(exe_scope))
120 {
121 if (m_location_str.empty())
122 {
123 StreamString sstr;
124
125 switch (m_value.GetValueType())
126 {
127 default:
128 break;
129
130 case Value::eValueTypeScalar:
131 if (m_value.GetContextType() == Value::eContextTypeDCRegisterInfo)
132 {
133 RegisterInfo *reg_info = m_value.GetRegisterInfo();
134 if (reg_info)
135 {
136 if (reg_info->name)
137 m_location_str = reg_info->name;
138 else if (reg_info->alt_name)
139 m_location_str = reg_info->alt_name;
140 break;
141 }
142 }
143 m_location_str = "scalar";
144 break;
145
146 case Value::eValueTypeLoadAddress:
147 case Value::eValueTypeFileAddress:
148 case Value::eValueTypeHostAddress:
149 {
150 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
151 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
152 m_location_str.swap(sstr.GetString());
153 }
154 break;
155 }
156 }
157 }
158 return m_location_str.c_str();
159}
160
161Value &
162ValueObject::GetValue()
163{
164 return m_value;
165}
166
167const Value &
168ValueObject::GetValue() const
169{
170 return m_value;
171}
172
173bool
174ValueObject::GetValueIsValid ()
175{
176 return m_flags.IsSet(eValueIsValid);
177}
178
179
180void
181ValueObject::SetValueIsValid (bool b)
182{
183 if (b)
184 m_flags.Set(eValueIsValid);
185 else
186 m_flags.Clear(eValueIsValid);
187}
188
189bool
190ValueObject::GetValueDidChange () const
191{
192 return m_flags.IsSet(eValueChanged);
193}
194
195void
196ValueObject::SetValueDidChange (bool value_changed)
197{
198 m_flags.Set(eValueChanged);
199}
200
201ValueObjectSP
202ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
203{
204 ValueObjectSP child_sp;
205 if (idx < GetNumChildren())
206 {
207 // Check if we have already made the child value object?
208 if (can_create && m_children[idx].get() == NULL)
209 {
210 // No we haven't created the child at this index, so lets have our
211 // subclass do it and cache the result for quick future access.
212 m_children[idx] = CreateChildAtIndex (idx, false, 0);
213 }
214
215 child_sp = m_children[idx];
216 }
217 return child_sp;
218}
219
220uint32_t
221ValueObject::GetIndexOfChildWithName (const ConstString &name)
222{
223 bool omit_empty_base_classes = true;
224 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
225 GetOpaqueClangQualType(),
226 name.AsCString(),
227 omit_empty_base_classes);
228}
229
230ValueObjectSP
231ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
232{
233 // when getting a child by name, it could be burried inside some base
234 // classes (which really aren't part of the expression path), so we
235 // need a vector of indexes that can get us down to the correct child
236 std::vector<uint32_t> child_indexes;
237 clang::ASTContext *clang_ast = GetClangAST();
238 void *clang_type = GetOpaqueClangQualType();
239 bool omit_empty_base_classes = true;
240 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
241 clang_type,
242 name.AsCString(),
243 omit_empty_base_classes,
244 child_indexes);
245 ValueObjectSP child_sp;
246 if (num_child_indexes > 0)
247 {
248 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
249 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
250
251 child_sp = GetChildAtIndex(*pos, can_create);
252 for (++pos; pos != end; ++pos)
253 {
254 if (child_sp)
255 {
256 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
257 child_sp = new_child_sp;
258 }
259 else
260 {
261 child_sp.reset();
262 }
263
264 }
265 }
266 return child_sp;
267}
268
269
270uint32_t
271ValueObject::GetNumChildren ()
272{
273 if (m_flags.IsClear(eNumChildrenHasBeenSet))
274 {
275 SetNumChildren (CalculateNumChildren());
276 }
277 return m_children.size();
278}
279void
280ValueObject::SetNumChildren (uint32_t num_children)
281{
282 m_flags.Set(eNumChildrenHasBeenSet);
283 m_children.resize(num_children);
284}
285
286void
287ValueObject::SetName (const char *name)
288{
289 m_name.SetCString(name);
290}
291
292void
293ValueObject::SetName (const ConstString &name)
294{
295 m_name = name;
296}
297
298ValueObjectSP
299ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
300{
301 ValueObjectSP valobj_sp;
302 bool omit_empty_base_classes = true;
303
304 std::string child_name_str;
305 uint32_t child_byte_size = 0;
306 int32_t child_byte_offset = 0;
307 uint32_t child_bitfield_bit_size = 0;
308 uint32_t child_bitfield_bit_offset = 0;
309 const bool transparent_pointers = synthetic_array_member == false;
310 clang::ASTContext *clang_ast = GetClangAST();
311 void *clang_type = GetOpaqueClangQualType();
312 void *child_clang_type;
313 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
314 GetName().AsCString(),
315 clang_type,
316 idx,
317 transparent_pointers,
318 omit_empty_base_classes,
319 child_name_str,
320 child_byte_size,
321 child_byte_offset,
322 child_bitfield_bit_size,
323 child_bitfield_bit_offset);
324 if (child_clang_type)
325 {
326 if (synthetic_index)
327 child_byte_offset += child_byte_size * synthetic_index;
328
329 ConstString child_name;
330 if (!child_name_str.empty())
331 child_name.SetCString (child_name_str.c_str());
332
333 valobj_sp.reset (new ValueObjectChild (this,
334 clang_ast,
335 child_clang_type,
336 child_name,
337 child_byte_size,
338 child_byte_offset,
339 child_bitfield_bit_size,
340 child_bitfield_bit_offset));
341 }
342 return valobj_sp;
343}
344
345const char *
346ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
347{
348 if (UpdateValueIfNeeded (exe_scope))
349 {
350 if (m_summary_str.empty())
351 {
352 void *clang_type = GetOpaqueClangQualType();
353
354 // See if this is a pointer to a C string?
355 uint32_t fixed_length = 0;
356 if (clang_type && ClangASTContext::IsCStringType (clang_type, fixed_length))
357 {
358 Process *process = exe_scope->CalculateProcess();
359 if (process != NULL)
360 {
361 StreamString sstr;
362 lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
363 lldb::AddressType cstr_address_type = eAddressTypeInvalid;
364 switch (GetValue().GetValueType())
365 {
366 case Value::eValueTypeScalar:
367 cstr_address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
368 cstr_address_type = eAddressTypeLoad;
369 break;
370
371 case Value::eValueTypeLoadAddress:
372 case Value::eValueTypeFileAddress:
373 case Value::eValueTypeHostAddress:
374 {
375 uint32_t data_offset = 0;
376 cstr_address = m_data.GetPointer(&data_offset);
377 cstr_address_type = m_value.GetValueAddressType();
378 if (cstr_address_type == eAddressTypeInvalid)
379 cstr_address_type = eAddressTypeLoad;
380 }
381 break;
382 }
383
384 if (cstr_address != LLDB_INVALID_ADDRESS)
385 {
386 DataExtractor data;
387 size_t bytes_read = 0;
388 std::vector<char> data_buffer;
389 std::vector<char> cstr_buffer;
390 size_t cstr_length;
391 Error error;
392 if (fixed_length > 0)
393 {
394 data_buffer.resize(fixed_length);
395 // Resize the formatted buffer in case every character
396 // uses the "\xXX" format and one extra byte for a NULL
397 cstr_buffer.resize(data_buffer.size() * 4 + 1);
Greg Clayton471b31c2010-07-20 22:52:08 +0000398 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
399 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), fixed_length, error);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000400 if (bytes_read > 0)
401 {
402 sstr << '"';
403 cstr_length = data.Dump (&sstr,
404 0, // Start offset in "data"
405 eFormatChar, // Print as characters
406 1, // Size of item (1 byte for a char!)
407 bytes_read, // How many bytes to print?
408 UINT32_MAX, // num per line
409 LLDB_INVALID_ADDRESS,// base address
410 0, // bitfield bit size
411 0); // bitfield bit offset
412 sstr << '"';
413 }
414 }
415 else
416 {
417 const size_t k_max_buf_size = 256;
418 data_buffer.resize (k_max_buf_size + 1);
419 // NULL terminate in case we don't get the entire C string
420 data_buffer.back() = '\0';
421 // Make a formatted buffer that can contain take 4
422 // bytes per character in case each byte uses the
423 // "\xXX" format and one extra byte for a NULL
424 cstr_buffer.resize (k_max_buf_size * 4 + 1);
425
Greg Clayton471b31c2010-07-20 22:52:08 +0000426 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427 size_t total_cstr_len = 0;
Greg Clayton471b31c2010-07-20 22:52:08 +0000428 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429 {
Greg Clayton471b31c2010-07-20 22:52:08 +0000430 size_t len = strlen(&data_buffer.front());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431 if (len == 0)
432 break;
433 if (len > bytes_read)
434 len = bytes_read;
435 if (sstr.GetSize() == 0)
436 sstr << '"';
437
438 cstr_length = data.Dump (&sstr,
439 0, // Start offset in "data"
440 eFormatChar, // Print as characters
441 1, // Size of item (1 byte for a char!)
442 len, // How many bytes to print?
443 UINT32_MAX, // num per line
444 LLDB_INVALID_ADDRESS,// base address
445 0, // bitfield bit size
446 0); // bitfield bit offset
447
448 if (len < k_max_buf_size)
449 break;
450 cstr_address += total_cstr_len;
451 }
452 if (sstr.GetSize() > 0)
453 sstr << '"';
454 }
455
456 if (sstr.GetSize() > 0)
457 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
458 }
459 }
460 }
461 }
462 }
463 if (m_summary_str.empty())
464 return NULL;
465 return m_summary_str.c_str();
466}
467
468
469const char *
470ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
471{
472 // If our byte size is zero this is an aggregate type that has children
473 if (ClangASTContext::IsAggregateType (GetOpaqueClangQualType()) == false)
474 {
475 if (UpdateValueIfNeeded(exe_scope))
476 {
477 if (m_value_str.empty())
478 {
479 const Value::ContextType context_type = m_value.GetContextType();
480
481 switch (context_type)
482 {
483 case Value::eContextTypeOpaqueClangQualType:
484 case Value::eContextTypeDCType:
485 case Value::eContextTypeDCVariable:
486 {
487 void *clang_type = GetOpaqueClangQualType ();
488 if (clang_type)
489 {
490 StreamString sstr;
491 lldb::Format format = Type::GetFormat(clang_type);
492 if (Type::DumpTypeValue(&sstr,
493 GetClangAST(), // The clang AST
494 clang_type, // The clang type to display
495 format, // Format to display this type with
496 m_data, // Data to extract from
497 0, // Byte offset into "m_data"
498 GetByteSize(), // Byte size of item in "m_data"
499 GetBitfieldBitSize(), // Bitfield bit size
500 GetBitfieldBitOffset())) // Bitfield bit offset
501 m_value_str.swap(sstr.GetString());
502 else
503 m_value_str.clear();
504 }
505 }
506 break;
507
508 case Value::eContextTypeDCRegisterInfo:
509 {
510 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
511 if (reg_info)
512 {
513 StreamString reg_sstr;
514 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
515 m_value_str.swap(reg_sstr.GetString());
516 }
517 }
518 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000519
520 default:
521 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000522 }
523 }
524 }
525 }
526 if (m_value_str.empty())
527 return NULL;
528 return m_value_str.c_str();
529}
530
531bool
532ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
533{
534 // Make sure our value is up to date first so that our location and location
535 // type is valid.
536 if (!UpdateValueIfNeeded(exe_scope))
537 return false;
538
539 uint32_t count = 0;
540 lldb::Encoding encoding = Type::GetEncoding (GetOpaqueClangQualType(), count);
541
542 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000543 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000544 switch (encoding)
545 {
546 case eEncodingInvalid:
547 return false;
548
549 case eEncodingUint:
550 if (byte_size > sizeof(unsigned long long))
551 {
552 return false;
553 }
554 else
555 {
556 unsigned long long ull_val = strtoull(value_str, &end, 0);
557 if (end && *end != '\0')
558 return false;
559 m_value = ull_val;
560 // Limit the bytes in our m_data appropriately.
561 m_value.GetScalar().GetData (m_data, byte_size);
562 }
563 break;
564
565 case eEncodingSint:
566 if (byte_size > sizeof(long long))
567 {
568 return false;
569 }
570 else
571 {
572 long long sll_val = strtoll(value_str, &end, 0);
573 if (end && *end != '\0')
574 return false;
575 m_value = sll_val;
576 // Limit the bytes in our m_data appropriately.
577 m_value.GetScalar().GetData (m_data, byte_size);
578 }
579 break;
580
581 case eEncodingIEEE754:
582 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000584 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585 if (dst != NULL)
586 {
587 // We are decoding a float into host byte order below, so make
588 // sure m_data knows what it contains.
589 m_data.SetByteOrder(eByteOrderHost);
590 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
591 GetClangAST(),
592 GetOpaqueClangQualType(),
593 value_str,
594 dst,
595 byte_size);
596
597 if (converted_byte_size == byte_size)
598 {
599 }
600 }
601 }
602 break;
603
604 case eEncodingVector:
605 return false;
606
607 default:
608 return false;
609 }
610
611 // If we have made it here the value is in m_data and we should write it
612 // out to the target
613 return Write ();
614}
615
616bool
617ValueObject::Write ()
618{
619 // Clear the update ID so the next time we try and read the value
620 // we try and read it again.
621 m_update_id = 0;
622
623 // TODO: when Value has a method to write a value back, call it from here.
624 return false;
625
626}
627
628void
629ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
630{
631 m_synthetic_children[key] = valobj_sp;
632}
633
634ValueObjectSP
635ValueObject::GetSyntheticChild (const ConstString &key) const
636{
637 ValueObjectSP synthetic_child_sp;
638 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
639 if (pos != m_synthetic_children.end())
640 synthetic_child_sp = pos->second;
641 return synthetic_child_sp;
642}
643
644bool
645ValueObject::IsPointerType ()
646{
647 return ClangASTContext::IsPointerType (GetOpaqueClangQualType());
648}
649
650bool
651ValueObject::IsPointerOrReferenceType ()
652{
653 return ClangASTContext::IsPointerOrReferenceType(GetOpaqueClangQualType());
654}
655
656ValueObjectSP
657ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
658{
659 ValueObjectSP synthetic_child_sp;
660 if (IsPointerType ())
661 {
662 char index_str[64];
663 snprintf(index_str, sizeof(index_str), "[%i]", index);
664 ConstString index_const_str(index_str);
665 // Check if we have already created a synthetic array member in this
666 // valid object. If we have we will re-use it.
667 synthetic_child_sp = GetSyntheticChild (index_const_str);
668 if (!synthetic_child_sp)
669 {
670 // We haven't made a synthetic array member for INDEX yet, so
671 // lets make one and cache it for any future reference.
672 synthetic_child_sp = CreateChildAtIndex(0, true, index);
673
674 // Cache the value if we got one back...
675 if (synthetic_child_sp)
676 AddSyntheticChild(index_const_str, synthetic_child_sp);
677 }
678 }
679 return synthetic_child_sp;
680}