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