blob: 83206912ba31065ef4570559acf9a083851f365b [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
Jim Ingham53c47f12010-09-10 23:12:17 +000027#include "lldb/Target/ExecutionContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include "lldb/Target/Process.h"
29#include "lldb/Target/RegisterContext.h"
30#include "lldb/Target/Thread.h"
Eli Friedman88966972010-06-09 08:50:27 +000031#include <stdlib.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36static lldb::user_id_t g_value_obj_uid = 0;
37
38//----------------------------------------------------------------------
39// ValueObject constructor
40//----------------------------------------------------------------------
41ValueObject::ValueObject () :
42 UserID (++g_value_obj_uid), // Unique identifier for every value object
43 m_update_id (0), // Value object lists always start at 1, value objects start at zero
44 m_name (),
45 m_data (),
46 m_value (),
47 m_error (),
Greg Clayton288bdf92010-09-02 02:59:18 +000048 m_value_str (),
49 m_old_value_str (),
50 m_location_str (),
51 m_summary_str (),
Jim Ingham53c47f12010-09-10 23:12:17 +000052 m_object_desc_str (),
Greg Clayton288bdf92010-09-02 02:59:18 +000053 m_children (),
54 m_synthetic_children (),
55 m_value_is_valid (false),
56 m_value_did_change (false),
57 m_children_count_valid (false),
58 m_old_value_valid (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000059{
60}
61
62//----------------------------------------------------------------------
63// Destructor
64//----------------------------------------------------------------------
65ValueObject::~ValueObject ()
66{
67}
68
69user_id_t
70ValueObject::GetUpdateID() const
71{
72 return m_update_id;
73}
74
75bool
76ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
77{
78 if (exe_scope)
79 {
80 Process *process = exe_scope->CalculateProcess();
81 if (process)
82 {
83 const user_id_t stop_id = process->GetStopID();
84 if (m_update_id != stop_id)
85 {
Greg Clayton288bdf92010-09-02 02:59:18 +000086 bool first_update = m_update_id == 0;
Greg Clayton73b953b2010-08-28 00:08:07 +000087 // Save the old value using swap to avoid a string copy which
88 // also will clear our m_value_str
Greg Clayton288bdf92010-09-02 02:59:18 +000089 if (m_value_str.empty())
90 {
91 m_old_value_valid = false;
92 }
93 else
94 {
95 m_old_value_valid = true;
96 m_old_value_str.swap (m_value_str);
97 m_value_str.clear();
98 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099 m_location_str.clear();
100 m_summary_str.clear();
Jim Ingham53c47f12010-09-10 23:12:17 +0000101 m_object_desc_str.clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102
Greg Clayton73b953b2010-08-28 00:08:07 +0000103 const bool value_was_valid = GetValueIsValid();
104 SetValueDidChange (false);
105
106 m_error.Clear();
107
108 // Call the pure virtual function to update the value
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 UpdateValue (exe_scope);
Greg Clayton73b953b2010-08-28 00:08:07 +0000110
111 // Update the fact that we tried to update the value for this
112 // value object wether or not we succeed
113 m_update_id = stop_id;
114 bool success = m_error.Success();
115 SetValueIsValid (success);
Greg Clayton288bdf92010-09-02 02:59:18 +0000116
117 if (first_update)
118 SetValueDidChange (false);
119 else if (!m_value_did_change && success == false)
Greg Clayton73b953b2010-08-28 00:08:07 +0000120 {
Greg Clayton288bdf92010-09-02 02:59:18 +0000121 // The value wasn't gotten successfully, so we mark this
122 // as changed if the value used to be valid and now isn't
123 SetValueDidChange (value_was_valid);
Greg Clayton73b953b2010-08-28 00:08:07 +0000124 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000125 }
126 }
127 }
128 return m_error.Success();
129}
130
131const DataExtractor &
132ValueObject::GetDataExtractor () const
133{
134 return m_data;
135}
136
137DataExtractor &
138ValueObject::GetDataExtractor ()
139{
140 return m_data;
141}
142
143const Error &
144ValueObject::GetError() const
145{
146 return m_error;
147}
148
149const ConstString &
150ValueObject::GetName() const
151{
152 return m_name;
153}
154
155const char *
156ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
157{
158 if (UpdateValueIfNeeded(exe_scope))
159 {
160 if (m_location_str.empty())
161 {
162 StreamString sstr;
163
164 switch (m_value.GetValueType())
165 {
166 default:
167 break;
168
169 case Value::eValueTypeScalar:
170 if (m_value.GetContextType() == Value::eContextTypeDCRegisterInfo)
171 {
172 RegisterInfo *reg_info = m_value.GetRegisterInfo();
173 if (reg_info)
174 {
175 if (reg_info->name)
176 m_location_str = reg_info->name;
177 else if (reg_info->alt_name)
178 m_location_str = reg_info->alt_name;
179 break;
180 }
181 }
182 m_location_str = "scalar";
183 break;
184
185 case Value::eValueTypeLoadAddress:
186 case Value::eValueTypeFileAddress:
187 case Value::eValueTypeHostAddress:
188 {
189 uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
190 sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
191 m_location_str.swap(sstr.GetString());
192 }
193 break;
194 }
195 }
196 }
197 return m_location_str.c_str();
198}
199
200Value &
201ValueObject::GetValue()
202{
203 return m_value;
204}
205
206const Value &
207ValueObject::GetValue() const
208{
209 return m_value;
210}
211
212bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000213ValueObject::GetValueIsValid () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214{
Greg Clayton288bdf92010-09-02 02:59:18 +0000215 return m_value_is_valid;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000216}
217
218
219void
220ValueObject::SetValueIsValid (bool b)
221{
Greg Clayton288bdf92010-09-02 02:59:18 +0000222 m_value_is_valid = b;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000223}
224
225bool
Greg Clayton288bdf92010-09-02 02:59:18 +0000226ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000227{
Greg Clayton288bdf92010-09-02 02:59:18 +0000228 GetValueAsCString (exe_scope);
229 return m_value_did_change;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000230}
231
232void
233ValueObject::SetValueDidChange (bool value_changed)
234{
Greg Clayton288bdf92010-09-02 02:59:18 +0000235 m_value_did_change = value_changed;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236}
237
238ValueObjectSP
239ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
240{
241 ValueObjectSP child_sp;
242 if (idx < GetNumChildren())
243 {
244 // Check if we have already made the child value object?
245 if (can_create && m_children[idx].get() == NULL)
246 {
247 // No we haven't created the child at this index, so lets have our
248 // subclass do it and cache the result for quick future access.
249 m_children[idx] = CreateChildAtIndex (idx, false, 0);
250 }
251
252 child_sp = m_children[idx];
253 }
254 return child_sp;
255}
256
257uint32_t
258ValueObject::GetIndexOfChildWithName (const ConstString &name)
259{
260 bool omit_empty_base_classes = true;
261 return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
262 GetOpaqueClangQualType(),
263 name.AsCString(),
264 omit_empty_base_classes);
265}
266
267ValueObjectSP
268ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
269{
270 // when getting a child by name, it could be burried inside some base
271 // classes (which really aren't part of the expression path), so we
272 // need a vector of indexes that can get us down to the correct child
273 std::vector<uint32_t> child_indexes;
274 clang::ASTContext *clang_ast = GetClangAST();
275 void *clang_type = GetOpaqueClangQualType();
276 bool omit_empty_base_classes = true;
277 const size_t num_child_indexes = ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
278 clang_type,
279 name.AsCString(),
280 omit_empty_base_classes,
281 child_indexes);
282 ValueObjectSP child_sp;
283 if (num_child_indexes > 0)
284 {
285 std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
286 std::vector<uint32_t>::const_iterator end = child_indexes.end ();
287
288 child_sp = GetChildAtIndex(*pos, can_create);
289 for (++pos; pos != end; ++pos)
290 {
291 if (child_sp)
292 {
293 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
294 child_sp = new_child_sp;
295 }
296 else
297 {
298 child_sp.reset();
299 }
300
301 }
302 }
303 return child_sp;
304}
305
306
307uint32_t
308ValueObject::GetNumChildren ()
309{
Greg Clayton288bdf92010-09-02 02:59:18 +0000310 if (!m_children_count_valid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 {
312 SetNumChildren (CalculateNumChildren());
313 }
314 return m_children.size();
315}
316void
317ValueObject::SetNumChildren (uint32_t num_children)
318{
Greg Clayton288bdf92010-09-02 02:59:18 +0000319 m_children_count_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000320 m_children.resize(num_children);
321}
322
323void
324ValueObject::SetName (const char *name)
325{
326 m_name.SetCString(name);
327}
328
329void
330ValueObject::SetName (const ConstString &name)
331{
332 m_name = name;
333}
334
335ValueObjectSP
336ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
337{
338 ValueObjectSP valobj_sp;
339 bool omit_empty_base_classes = true;
340
341 std::string child_name_str;
342 uint32_t child_byte_size = 0;
343 int32_t child_byte_offset = 0;
344 uint32_t child_bitfield_bit_size = 0;
345 uint32_t child_bitfield_bit_offset = 0;
346 const bool transparent_pointers = synthetic_array_member == false;
347 clang::ASTContext *clang_ast = GetClangAST();
348 void *clang_type = GetOpaqueClangQualType();
349 void *child_clang_type;
350 child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
351 GetName().AsCString(),
352 clang_type,
353 idx,
354 transparent_pointers,
355 omit_empty_base_classes,
356 child_name_str,
357 child_byte_size,
358 child_byte_offset,
359 child_bitfield_bit_size,
360 child_bitfield_bit_offset);
361 if (child_clang_type)
362 {
363 if (synthetic_index)
364 child_byte_offset += child_byte_size * synthetic_index;
365
366 ConstString child_name;
367 if (!child_name_str.empty())
368 child_name.SetCString (child_name_str.c_str());
369
370 valobj_sp.reset (new ValueObjectChild (this,
371 clang_ast,
372 child_clang_type,
373 child_name,
374 child_byte_size,
375 child_byte_offset,
376 child_bitfield_bit_size,
377 child_bitfield_bit_offset));
378 }
379 return valobj_sp;
380}
381
382const char *
383ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
384{
385 if (UpdateValueIfNeeded (exe_scope))
386 {
387 if (m_summary_str.empty())
388 {
389 void *clang_type = GetOpaqueClangQualType();
390
391 // See if this is a pointer to a C string?
392 uint32_t fixed_length = 0;
Greg Clayton737b9322010-09-13 03:32:57 +0000393 if (clang_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394 {
Greg Clayton737b9322010-09-13 03:32:57 +0000395 StreamString sstr;
396
397 if (ClangASTContext::IsCStringType (clang_type, fixed_length))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398 {
Greg Clayton737b9322010-09-13 03:32:57 +0000399 Process *process = exe_scope->CalculateProcess();
400 if (process != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000401 {
Greg Clayton737b9322010-09-13 03:32:57 +0000402 lldb::AddressType cstr_address_type = eAddressTypeInvalid;
403 lldb::addr_t cstr_address = GetPointerValue (cstr_address_type, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000404
Greg Clayton737b9322010-09-13 03:32:57 +0000405 if (cstr_address != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406 {
Greg Clayton737b9322010-09-13 03:32:57 +0000407 DataExtractor data;
408 size_t bytes_read = 0;
409 std::vector<char> data_buffer;
410 std::vector<char> cstr_buffer;
411 size_t cstr_length;
412 Error error;
413 if (fixed_length > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000414 {
Greg Clayton737b9322010-09-13 03:32:57 +0000415 data_buffer.resize(fixed_length);
416 // Resize the formatted buffer in case every character
417 // uses the "\xXX" format and one extra byte for a NULL
418 cstr_buffer.resize(data_buffer.size() * 4 + 1);
419 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
420 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), fixed_length, error);
421 if (bytes_read > 0)
422 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000423 sstr << '"';
Greg Clayton737b9322010-09-13 03:32:57 +0000424 cstr_length = data.Dump (&sstr,
425 0, // Start offset in "data"
426 eFormatChar, // Print as characters
427 1, // Size of item (1 byte for a char!)
428 bytes_read, // How many bytes to print?
429 UINT32_MAX, // num per line
430 LLDB_INVALID_ADDRESS,// base address
431 0, // bitfield bit size
432 0); // bitfield bit offset
433 sstr << '"';
434 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435 }
Greg Clayton737b9322010-09-13 03:32:57 +0000436 else
437 {
438 const size_t k_max_buf_size = 256;
439 data_buffer.resize (k_max_buf_size + 1);
440 // NULL terminate in case we don't get the entire C string
441 data_buffer.back() = '\0';
442 // Make a formatted buffer that can contain take 4
443 // bytes per character in case each byte uses the
444 // "\xXX" format and one extra byte for a NULL
445 cstr_buffer.resize (k_max_buf_size * 4 + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446
Greg Clayton737b9322010-09-13 03:32:57 +0000447 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
448 size_t total_cstr_len = 0;
449 while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
450 {
451 size_t len = strlen(&data_buffer.front());
452 if (len == 0)
453 break;
454 if (len > bytes_read)
455 len = bytes_read;
456 if (sstr.GetSize() == 0)
457 sstr << '"';
458
459 cstr_length = data.Dump (&sstr,
460 0, // Start offset in "data"
461 eFormatChar, // Print as characters
462 1, // Size of item (1 byte for a char!)
463 len, // How many bytes to print?
464 UINT32_MAX, // num per line
465 LLDB_INVALID_ADDRESS,// base address
466 0, // bitfield bit size
467 0); // bitfield bit offset
468
469 if (len < k_max_buf_size)
470 break;
471 cstr_address += total_cstr_len;
472 }
473 if (sstr.GetSize() > 0)
474 sstr << '"';
475 }
476 }
477 }
478
479 if (sstr.GetSize() > 0)
480 m_summary_str.assign (sstr.GetData(), sstr.GetSize());
481 }
482 else if (ClangASTContext::IsFunctionPointerType (clang_type))
483 {
484 lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
485 lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
486
487 if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
488 {
489 switch (func_ptr_address_type)
490 {
491 case eAddressTypeInvalid:
492 case eAddressTypeFile:
493 break;
494
495 case eAddressTypeLoad:
496 {
497 Address so_addr;
498 Process *process = exe_scope->CalculateProcess();
499 if (process != NULL)
500 {
501 if (process->ResolveLoadAddress(func_ptr_address, so_addr))
502 {
503 so_addr.Dump (&sstr,
504 exe_scope,
505 Address::DumpStyleResolvedDescription,
506 Address::DumpStyleSectionNameOffset);
507 }
508 }
509 }
510 break;
511
512 case eAddressTypeHost:
513 break;
514 }
515 }
516 if (sstr.GetSize() > 0)
517 {
518 m_summary_str.assign (1, '(');
519 m_summary_str.append (sstr.GetData(), sstr.GetSize());
520 m_summary_str.append (1, ')');
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000521 }
522 }
523 }
524 }
525 }
526 if (m_summary_str.empty())
527 return NULL;
528 return m_summary_str.c_str();
529}
530
Greg Clayton737b9322010-09-13 03:32:57 +0000531
Jim Ingham53c47f12010-09-10 23:12:17 +0000532const char *
533ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
534{
535 if (!m_object_desc_str.empty())
536 return m_object_desc_str.c_str();
537
538 if (!ClangASTContext::IsPointerType (GetOpaqueClangQualType()))
539 return NULL;
540
541 if (!GetValueIsValid())
542 return NULL;
543
544 Process *process = exe_scope->CalculateProcess();
545
546 if (!process)
547 return NULL;
548
549 Scalar scalar;
550
551 if (!ClangASTType::GetValueAsScalar (GetClangAST(),
552 GetOpaqueClangQualType(),
553 GetDataExtractor(),
554 0,
555 GetByteSize(),
556 scalar))
557 return NULL;
558
559 ExecutionContext exe_ctx;
560 exe_scope->Calculate(exe_ctx);
561
562 Value val(scalar);
563 val.SetContext(Value::eContextTypeOpaqueClangQualType,
564 ClangASTContext::GetVoidPtrType(GetClangAST(), false));
565
566 StreamString s;
567 // FIXME: Check the runtime this object belongs to and get the appropriate object printer for the object kind.
568 if (process->GetObjCObjectPrinter().PrintObject(s, val, exe_ctx))
569 {
570 m_object_desc_str.append (s.GetData());
571 }
572 return m_object_desc_str.c_str();
573}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574
575const char *
576ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
577{
578 // If our byte size is zero this is an aggregate type that has children
579 if (ClangASTContext::IsAggregateType (GetOpaqueClangQualType()) == false)
580 {
581 if (UpdateValueIfNeeded(exe_scope))
582 {
583 if (m_value_str.empty())
584 {
585 const Value::ContextType context_type = m_value.GetContextType();
586
587 switch (context_type)
588 {
589 case Value::eContextTypeOpaqueClangQualType:
590 case Value::eContextTypeDCType:
591 case Value::eContextTypeDCVariable:
592 {
593 void *clang_type = GetOpaqueClangQualType ();
594 if (clang_type)
595 {
596 StreamString sstr;
Greg Claytone1a916a2010-07-21 22:12:05 +0000597 lldb::Format format = ClangASTType::GetFormat(clang_type);
598 if (ClangASTType::DumpTypeValue(GetClangAST(), // The clang AST
599 clang_type, // The clang type to display
600 &sstr,
601 format, // Format to display this type with
602 m_data, // Data to extract from
603 0, // Byte offset into "m_data"
604 GetByteSize(), // Byte size of item in "m_data"
605 GetBitfieldBitSize(), // Bitfield bit size
606 GetBitfieldBitOffset())) // Bitfield bit offset
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000607 m_value_str.swap(sstr.GetString());
608 else
609 m_value_str.clear();
610 }
611 }
612 break;
613
614 case Value::eContextTypeDCRegisterInfo:
615 {
616 const RegisterInfo *reg_info = m_value.GetRegisterInfo();
617 if (reg_info)
618 {
619 StreamString reg_sstr;
620 m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
621 m_value_str.swap(reg_sstr.GetString());
622 }
623 }
624 break;
Greg Claytonc982c762010-07-09 20:39:50 +0000625
626 default:
627 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 }
629 }
Greg Clayton288bdf92010-09-02 02:59:18 +0000630
631 if (!m_value_did_change && m_old_value_valid)
632 {
633 // The value was gotten successfully, so we consider the
634 // value as changed if the value string differs
635 SetValueDidChange (m_old_value_str != m_value_str);
636 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637 }
638 }
639 if (m_value_str.empty())
640 return NULL;
641 return m_value_str.c_str();
642}
643
Greg Clayton737b9322010-09-13 03:32:57 +0000644addr_t
645ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
646{
647 lldb::addr_t address = LLDB_INVALID_ADDRESS;
648 address_type = eAddressTypeInvalid;
649 switch (GetValue().GetValueType())
650 {
651 case Value::eValueTypeScalar:
652 if (scalar_is_load_address)
653 {
654 address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
655 address_type = eAddressTypeLoad;
656 }
657 break;
658
659 case Value::eValueTypeLoadAddress:
660 case Value::eValueTypeFileAddress:
661 case Value::eValueTypeHostAddress:
662 {
663 uint32_t data_offset = 0;
664 address = m_data.GetPointer(&data_offset);
665 address_type = m_value.GetValueAddressType();
666 if (address_type == eAddressTypeInvalid)
667 address_type = eAddressTypeLoad;
668 }
669 break;
670 }
671 return address;
672}
673
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000674bool
675ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
676{
677 // Make sure our value is up to date first so that our location and location
678 // type is valid.
679 if (!UpdateValueIfNeeded(exe_scope))
680 return false;
681
682 uint32_t count = 0;
Greg Claytone1a916a2010-07-21 22:12:05 +0000683 lldb::Encoding encoding = ClangASTType::GetEncoding (GetOpaqueClangQualType(), count);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684
685 char *end = NULL;
Greg Claytonb1320972010-07-14 00:18:15 +0000686 const size_t byte_size = GetByteSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687 switch (encoding)
688 {
689 case eEncodingInvalid:
690 return false;
691
692 case eEncodingUint:
693 if (byte_size > sizeof(unsigned long long))
694 {
695 return false;
696 }
697 else
698 {
699 unsigned long long ull_val = strtoull(value_str, &end, 0);
700 if (end && *end != '\0')
701 return false;
702 m_value = ull_val;
703 // Limit the bytes in our m_data appropriately.
704 m_value.GetScalar().GetData (m_data, byte_size);
705 }
706 break;
707
708 case eEncodingSint:
709 if (byte_size > sizeof(long long))
710 {
711 return false;
712 }
713 else
714 {
715 long long sll_val = strtoll(value_str, &end, 0);
716 if (end && *end != '\0')
717 return false;
718 m_value = sll_val;
719 // Limit the bytes in our m_data appropriately.
720 m_value.GetScalar().GetData (m_data, byte_size);
721 }
722 break;
723
724 case eEncodingIEEE754:
725 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000726 const off_t byte_offset = GetByteOffset();
Greg Claytonc982c762010-07-09 20:39:50 +0000727 uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000728 if (dst != NULL)
729 {
730 // We are decoding a float into host byte order below, so make
731 // sure m_data knows what it contains.
732 m_data.SetByteOrder(eByteOrderHost);
733 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
734 GetClangAST(),
735 GetOpaqueClangQualType(),
736 value_str,
737 dst,
738 byte_size);
739
740 if (converted_byte_size == byte_size)
741 {
742 }
743 }
744 }
745 break;
746
747 case eEncodingVector:
748 return false;
749
750 default:
751 return false;
752 }
753
754 // If we have made it here the value is in m_data and we should write it
755 // out to the target
756 return Write ();
757}
758
759bool
760ValueObject::Write ()
761{
762 // Clear the update ID so the next time we try and read the value
763 // we try and read it again.
764 m_update_id = 0;
765
766 // TODO: when Value has a method to write a value back, call it from here.
767 return false;
768
769}
770
771void
772ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
773{
774 m_synthetic_children[key] = valobj_sp;
775}
776
777ValueObjectSP
778ValueObject::GetSyntheticChild (const ConstString &key) const
779{
780 ValueObjectSP synthetic_child_sp;
781 std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
782 if (pos != m_synthetic_children.end())
783 synthetic_child_sp = pos->second;
784 return synthetic_child_sp;
785}
786
787bool
788ValueObject::IsPointerType ()
789{
790 return ClangASTContext::IsPointerType (GetOpaqueClangQualType());
791}
792
793bool
794ValueObject::IsPointerOrReferenceType ()
795{
796 return ClangASTContext::IsPointerOrReferenceType(GetOpaqueClangQualType());
797}
798
799ValueObjectSP
800ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
801{
802 ValueObjectSP synthetic_child_sp;
803 if (IsPointerType ())
804 {
805 char index_str[64];
806 snprintf(index_str, sizeof(index_str), "[%i]", index);
807 ConstString index_const_str(index_str);
808 // Check if we have already created a synthetic array member in this
809 // valid object. If we have we will re-use it.
810 synthetic_child_sp = GetSyntheticChild (index_const_str);
811 if (!synthetic_child_sp)
812 {
813 // We haven't made a synthetic array member for INDEX yet, so
814 // lets make one and cache it for any future reference.
815 synthetic_child_sp = CreateChildAtIndex(0, true, index);
816
817 // Cache the value if we got one back...
818 if (synthetic_child_sp)
819 AddSyntheticChild(index_const_str, synthetic_child_sp);
820 }
821 }
822 return synthetic_child_sp;
823}