1 //===-- 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 #include <stdlib.h>
14 
15 // C++ Includes
16 // Other libraries and framework includes
17 #include "llvm/Support/raw_ostream.h"
18 #include "clang/AST/Type.h"
19 
20 // Project includes
21 #include "lldb/Core/DataBufferHeap.h"
22 #include "lldb/Core/StreamString.h"
23 #include "lldb/Core/ValueObjectChild.h"
24 #include "lldb/Core/ValueObjectConstResult.h"
25 #include "lldb/Core/ValueObjectList.h"
26 
27 #include "lldb/Symbol/ClangASTType.h"
28 #include "lldb/Symbol/ClangASTContext.h"
29 #include "lldb/Symbol/Type.h"
30 
31 #include "lldb/Target/ExecutionContext.h"
32 #include "lldb/Target/LanguageRuntime.h"
33 #include "lldb/Target/Process.h"
34 #include "lldb/Target/RegisterContext.h"
35 #include "lldb/Target/Target.h"
36 #include "lldb/Target/Thread.h"
37 
38 using namespace lldb;
39 using namespace lldb_private;
40 
41 static lldb::user_id_t g_value_obj_uid = 0;
42 
43 //----------------------------------------------------------------------
44 // ValueObject constructor
45 //----------------------------------------------------------------------
46 ValueObject::ValueObject (ValueObject *parent) :
47     UserID (++g_value_obj_uid), // Unique identifier for every value object
48     m_parent (parent),
49     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 (),
54     m_value_str (),
55     m_old_value_str (),
56     m_location_str (),
57     m_summary_str (),
58     m_object_desc_str (),
59     m_children (),
60     m_synthetic_children (),
61     m_dynamic_value_sp (),
62     m_format (eFormatDefault),
63     m_value_is_valid (false),
64     m_value_did_change (false),
65     m_children_count_valid (false),
66     m_old_value_valid (false),
67     m_pointers_point_to_load_addrs (false),
68     m_is_deref_of_parent (false)
69 {
70 }
71 
72 //----------------------------------------------------------------------
73 // Destructor
74 //----------------------------------------------------------------------
75 ValueObject::~ValueObject ()
76 {
77 }
78 
79 user_id_t
80 ValueObject::GetUpdateID() const
81 {
82     return m_update_id;
83 }
84 
85 bool
86 ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
87 {
88     // 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 
93     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             {
101                 bool first_update = m_update_id == 0;
102                 // Save the old value using swap to avoid a string copy which
103                 // also will clear our m_value_str
104                 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                 }
114                 m_location_str.clear();
115                 m_summary_str.clear();
116                 m_object_desc_str.clear();
117 
118                 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
124                 UpdateValue (exe_scope);
125 
126                 // Update the fact that we tried to update the value for this
127                 // value object whether or not we succeed
128                 m_update_id = stop_id;
129                 bool success = m_error.Success();
130                 SetValueIsValid (success);
131 
132                 if (first_update)
133                     SetValueDidChange (false);
134                 else if (!m_value_did_change && success == false)
135                 {
136                     // 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);
139                 }
140             }
141         }
142     }
143     return m_error.Success();
144 }
145 
146 const DataExtractor &
147 ValueObject::GetDataExtractor () const
148 {
149     return m_data;
150 }
151 
152 DataExtractor &
153 ValueObject::GetDataExtractor ()
154 {
155     return m_data;
156 }
157 
158 const Error &
159 ValueObject::GetError() const
160 {
161     return m_error;
162 }
163 
164 const ConstString &
165 ValueObject::GetName() const
166 {
167     return m_name;
168 }
169 
170 const char *
171 ValueObject::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:
185                 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
186                 {
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 
215 Value &
216 ValueObject::GetValue()
217 {
218     return m_value;
219 }
220 
221 const Value &
222 ValueObject::GetValue() const
223 {
224     return m_value;
225 }
226 
227 bool
228 ValueObject::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 
236 bool
237 ValueObject::GetValueIsValid () const
238 {
239     return m_value_is_valid;
240 }
241 
242 
243 void
244 ValueObject::SetValueIsValid (bool b)
245 {
246     m_value_is_valid = b;
247 }
248 
249 bool
250 ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
251 {
252     GetValueAsCString (exe_scope);
253     return m_value_did_change;
254 }
255 
256 void
257 ValueObject::SetValueDidChange (bool value_changed)
258 {
259     m_value_did_change = value_changed;
260 }
261 
262 ValueObjectSP
263 ValueObject::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 
281 uint32_t
282 ValueObject::GetIndexOfChildWithName (const ConstString &name)
283 {
284     bool omit_empty_base_classes = true;
285     return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
286                                                      GetClangType(),
287                                                      name.GetCString(),
288                                                      omit_empty_base_classes);
289 }
290 
291 ValueObjectSP
292 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
293 {
294     // when getting a child by name, it could be buried inside some base
295     // 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();
299     void *clang_type = GetClangType();
300     bool omit_empty_base_classes = true;
301     const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
302                                                                                       clang_type,
303                                                                                       name.GetCString(),
304                                                                                       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 
331 uint32_t
332 ValueObject::GetNumChildren ()
333 {
334     if (!m_children_count_valid)
335     {
336         SetNumChildren (CalculateNumChildren());
337     }
338     return m_children.size();
339 }
340 void
341 ValueObject::SetNumChildren (uint32_t num_children)
342 {
343     m_children_count_valid = true;
344     m_children.resize(num_children);
345 }
346 
347 void
348 ValueObject::SetName (const char *name)
349 {
350     m_name.SetCString(name);
351 }
352 
353 void
354 ValueObject::SetName (const ConstString &name)
355 {
356     m_name = name;
357 }
358 
359 ValueObjectSP
360 ValueObject::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;
370     bool child_is_base_class = false;
371     bool child_is_deref_of_parent = false;
372 
373     const bool transparent_pointers = synthetic_array_member == false;
374     clang::ASTContext *clang_ast = GetClangAST();
375     clang_type_t clang_type = GetClangType();
376     clang_type_t child_clang_type;
377     child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
378                                                                   GetName().GetCString(),
379                                                                   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,
387                                                                   child_bitfield_bit_offset,
388                                                                   child_is_base_class,
389                                                                   child_is_deref_of_parent);
390     if (child_clang_type && child_byte_size)
391     {
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,
406                                                child_bitfield_bit_offset,
407                                                child_is_base_class,
408                                                child_is_deref_of_parent));
409         if (m_pointers_point_to_load_addrs)
410             valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
411     }
412     return valobj_sp;
413 }
414 
415 const char *
416 ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
417 {
418     if (UpdateValueIfNeeded (exe_scope))
419     {
420         if (m_summary_str.empty())
421         {
422             clang_type_t clang_type = GetClangType();
423 
424             // See if this is a pointer to a C string?
425             if (clang_type)
426             {
427                 StreamString sstr;
428                 clang_type_t elem_or_pointee_clang_type;
429                 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
430                                                                       GetClangAST(),
431                                                                       &elem_or_pointee_clang_type));
432 
433                 if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
434                     ClangASTContext::IsCharType (elem_or_pointee_clang_type))
435                 {
436                     Process *process = exe_scope->CalculateProcess();
437                     if (process != NULL)
438                     {
439                         lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
440                         lldb::AddressType cstr_address_type = eAddressTypeInvalid;
441 
442                         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                         }
454                         if (cstr_address != LLDB_INVALID_ADDRESS)
455                         {
456                             DataExtractor data;
457                             size_t bytes_read = 0;
458                             std::vector<char> data_buffer;
459                             Error error;
460                             if (cstr_len > 0)
461                             {
462                                 data_buffer.resize(cstr_len);
463                                 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
464                                 bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
465                                 if (bytes_read > 0)
466                                 {
467                                     sstr << '"';
468                                     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
477                                     sstr << '"';
478                                 }
479                             }
480                             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';
486 
487                                 sstr << '"';
488 
489                                 data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
490                                 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;
497 
498                                     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 
508                                     if (len < k_max_buf_size)
509                                         break;
510                                     cstr_address += k_max_buf_size;
511                                 }
512                                 sstr << '"';
513                             }
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;
536                                 Target *target = exe_scope->CalculateTarget();
537                                 if (target && target->GetSectionLoadList().IsEmpty() == false)
538                                 {
539                                     if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
540                                     {
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, ')');
559                     }
560                 }
561             }
562         }
563     }
564     if (m_summary_str.empty())
565         return NULL;
566     return m_summary_str.c_str();
567 }
568 
569 const char *
570 ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
571 {
572     if (!m_object_desc_str.empty())
573         return m_object_desc_str.c_str();
574 
575     if (!GetValueIsValid())
576         return NULL;
577 
578     Process *process = exe_scope->CalculateProcess();
579     if (process == NULL)
580         return NULL;
581 
582     StreamString s;
583 
584     lldb::LanguageType language = GetObjectRuntimeLanguage();
585     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
586 
587     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 
599     if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
600     {
601         m_object_desc_str.append (s.GetData());
602     }
603 
604     if (m_object_desc_str.empty())
605         return NULL;
606     else
607         return m_object_desc_str.c_str();
608 }
609 
610 const char *
611 ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
612 {
613     // If our byte size is zero this is an aggregate type that has children
614     if (ClangASTContext::IsAggregateType (GetClangType()) == false)
615     {
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                 {
624                 case Value::eContextTypeClangType:
625                 case Value::eContextTypeLLDBType:
626                 case Value::eContextTypeVariable:
627                     {
628                         clang_type_t clang_type = GetClangType ();
629                         if (clang_type)
630                         {
631                             StreamString sstr;
632                             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
644                                 m_value_str.swap(sstr.GetString());
645                             else
646                                 m_value_str.clear();
647                         }
648                     }
649                     break;
650 
651                 case Value::eContextTypeRegisterInfo:
652                     {
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;
662 
663                 default:
664                     break;
665                 }
666             }
667 
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             }
674         }
675     }
676     if (m_value_str.empty())
677         return NULL;
678     return m_value_str.c_str();
679 }
680 
681 addr_t
682 ValueObject::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 
707 addr_t
708 ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
709 {
710     lldb::addr_t address = LLDB_INVALID_ADDRESS;
711     address_type = eAddressTypeInvalid;
712     switch (m_value.GetValueType())
713     {
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     }
734 
735     if (m_pointers_point_to_load_addrs)
736         address_type = eAddressTypeLoad;
737 
738     return address;
739 }
740 
741 bool
742 ValueObject::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;
750     lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
751 
752     char *end = NULL;
753     const size_t byte_size = GetByteSize();
754     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         {
793             const off_t byte_offset = GetByteOffset();
794             uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
795             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(),
802                                                         GetClangType(),
803                                                         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 
826 bool
827 ValueObject::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 
838 lldb::LanguageType
839 ValueObject::GetObjectRuntimeLanguage ()
840 {
841     clang_type_t opaque_qual_type = GetClangType();
842     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;
864         if (ClangASTContext::IsCXXClassType (opaque_qual_type))
865             return lldb::eLanguageTypeC_plus_plus;
866     }
867 
868     return lldb::eLanguageTypeC;
869 }
870 
871 void
872 ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
873 {
874     m_synthetic_children[key] = valobj_sp;
875 }
876 
877 ValueObjectSP
878 ValueObject::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 
887 bool
888 ValueObject::IsPointerType ()
889 {
890     return ClangASTContext::IsPointerType (GetClangType());
891 }
892 
893 
894 
895 bool
896 ValueObject::IsPointerOrReferenceType ()
897 {
898     return ClangASTContext::IsPointerOrReferenceType(GetClangType());
899 }
900 
901 ValueObjectSP
902 ValueObject::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 }
926 
927 bool
928 ValueObject::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:
935 
936     return true;
937 }
938 
939 bool
940 ValueObject::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 
960 ValueObject *
961 ValueObject::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 }
972 
973 void
974 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
975 {
976     const bool is_deref_of_parent = IsDereferenceOfParent ();
977 
978     if (is_deref_of_parent)
979         s.PutCString("*(");
980 
981     if (m_parent)
982         m_parent->GetExpressionPath (s, qualify_cxx_base_classes);
983 
984     if (!IsBaseClass())
985     {
986         if (!is_deref_of_parent)
987         {
988             ValueObject *non_base_class_parent = GetNonBaseClassParent();
989             if (non_base_class_parent)
990             {
991                 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                 }
1006             }
1007 
1008             const char *name = GetName().GetCString();
1009             if (name)
1010             {
1011                 if (qualify_cxx_base_classes)
1012                 {
1013                     if (GetBaseClassPath (s))
1014                         s.PutCString("::");
1015                 }
1016                 s.PutCString(name);
1017             }
1018         }
1019     }
1020 
1021     if (is_deref_of_parent)
1022         s.PutChar(')');
1023 }
1024 
1025 void
1026 ValueObject::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,
1038     bool scope_already_checked,
1039     bool flat_output
1040 )
1041 {
1042     if (valobj)
1043     {
1044         clang_type_t clang_type = valobj->GetClangType();
1045 
1046         const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
1047         const char *err_cstr = NULL;
1048         const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1049         const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
1050 
1051         const bool print_valobj = flat_output == false || has_value;
1052 
1053         if (print_valobj)
1054         {
1055             if (show_location)
1056             {
1057                 s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1058             }
1059 
1060             s.Indent();
1061 
1062             // Always show the type for the top level items.
1063             if (show_types || (curr_depth == 0 && !flat_output))
1064                 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
1065 
1066 
1067             if (flat_output)
1068             {
1069                 // 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);
1072                 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             }
1084         }
1085 
1086         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         }
1093 
1094         if (err_cstr)
1095         {
1096             s.Printf (" error: %s\n", err_cstr);
1097         }
1098         else
1099         {
1100             const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
1101             if (print_valobj)
1102             {
1103                 const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
1104 
1105                 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
1117                         s.Printf (" [no Objective-C description available]\n");
1118                     return;
1119                 }
1120             }
1121 
1122             if (curr_depth < max_depth)
1123             {
1124                 // 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                 }
1155 
1156                 if (print_children)
1157                 {
1158                     const uint32_t num_children = valobj->GetNumChildren();
1159                     if (num_children)
1160                     {
1161                         if (flat_output)
1162                         {
1163                             if (print_valobj)
1164                                 s.EOL();
1165                         }
1166                         else
1167                         {
1168                             if (print_valobj)
1169                                 s.PutCString(is_ref ? ": {\n" : " {\n");
1170                             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,
1182                                                  (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
1183                                                  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");
1197                         }
1198                     }
1199                     else if (has_children)
1200                     {
1201                         // Aggregate, no children...
1202                         if (print_valobj)
1203                             s.PutCString(" {}\n");
1204                     }
1205                     else
1206                     {
1207                         if (print_valobj)
1208                             s.EOL();
1209                     }
1210 
1211                 }
1212                 else
1213                 {
1214                     s.EOL();
1215                 }
1216             }
1217             else
1218             {
1219                 if (has_children && print_valobj)
1220                 {
1221                     s.PutCString("{...}\n");
1222                 }
1223             }
1224         }
1225     }
1226 }
1227 
1228 
1229 ValueObjectSP
1230 ValueObject::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 
1259 lldb::ValueObjectSP
1260 ValueObject::Dereference (Error &error)
1261 {
1262     lldb::ValueObjectSP valobj_sp;
1263     const bool is_pointer_type = IsPointerType();
1264     if (is_pointer_type)
1265     {
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;
1274         bool child_is_deref_of_parent = false;
1275         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,
1290                                                                       child_is_base_class,
1291                                                                       child_is_deref_of_parent);
1292         if (child_clang_type && child_byte_size)
1293         {
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,
1306                                                    child_is_base_class,
1307                                                    child_is_deref_of_parent));
1308         }
1309     }
1310 
1311     if (valobj_sp)
1312     {
1313         error.Clear();
1314     }
1315     else
1316     {
1317         StreamString strm;
1318         GetExpressionPath(strm, true);
1319 
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());
1324     }
1325 
1326     return valobj_sp;
1327 }
1328 
1329     lldb::ValueObjectSP
1330 ValueObject::AddressOf (Error &error)
1331 {
1332     lldb::ValueObjectSP valobj_sp;
1333     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);
1336     error.Clear();
1337     if (addr != LLDB_INVALID_ADDRESS)
1338     {
1339         switch (address_type)
1340         {
1341         default:
1342         case eAddressTypeInvalid:
1343             {
1344                 StreamString expr_path_strm;
1345                 GetExpressionPath(expr_path_strm, true);
1346                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1347             }
1348             break;
1349 
1350         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,
1364                                                                  eAddressTypeInvalid,
1365                                                                  m_data.GetAddressByteSize()));
1366                 }
1367             }
1368             break;
1369         }
1370     }
1371     return valobj_sp;
1372 }
1373 
1374