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