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/ValueObjectDynamicValue.h"
26 #include "lldb/Core/ValueObjectList.h"
27 #include "lldb/Core/ValueObjectMemory.h"
28 
29 #include "lldb/Host/Endian.h"
30 
31 #include "lldb/Symbol/ClangASTType.h"
32 #include "lldb/Symbol/ClangASTContext.h"
33 #include "lldb/Symbol/Type.h"
34 
35 #include "lldb/Target/ExecutionContext.h"
36 #include "lldb/Target/LanguageRuntime.h"
37 #include "lldb/Target/Process.h"
38 #include "lldb/Target/RegisterContext.h"
39 #include "lldb/Target/Target.h"
40 #include "lldb/Target/Thread.h"
41 
42 using namespace lldb;
43 using namespace lldb_private;
44 
45 static lldb::user_id_t g_value_obj_uid = 0;
46 
47 //----------------------------------------------------------------------
48 // ValueObject constructor
49 //----------------------------------------------------------------------
50 ValueObject::ValueObject (ValueObject &parent) :
51     UserID (++g_value_obj_uid), // Unique identifier for every value object
52     m_parent (&parent),
53     m_update_point (parent.GetUpdatePoint ()),
54     m_name (),
55     m_data (),
56     m_value (),
57     m_error (),
58     m_value_str (),
59     m_old_value_str (),
60     m_location_str (),
61     m_summary_str (),
62     m_object_desc_str (),
63     m_manager(parent.GetManager()),
64     m_children (),
65     m_synthetic_children (),
66     m_dynamic_value (NULL),
67     m_deref_valobj(NULL),
68     m_format (eFormatDefault),
69     m_value_is_valid (false),
70     m_value_did_change (false),
71     m_children_count_valid (false),
72     m_old_value_valid (false),
73     m_pointers_point_to_load_addrs (false),
74     m_is_deref_of_parent (false)
75 {
76     m_manager->ManageObject(this);
77 }
78 
79 //----------------------------------------------------------------------
80 // ValueObject constructor
81 //----------------------------------------------------------------------
82 ValueObject::ValueObject (ExecutionContextScope *exe_scope) :
83     UserID (++g_value_obj_uid), // Unique identifier for every value object
84     m_parent (NULL),
85     m_update_point (exe_scope),
86     m_name (),
87     m_data (),
88     m_value (),
89     m_error (),
90     m_value_str (),
91     m_old_value_str (),
92     m_location_str (),
93     m_summary_str (),
94     m_object_desc_str (),
95     m_manager(),
96     m_children (),
97     m_synthetic_children (),
98     m_dynamic_value (NULL),
99     m_deref_valobj(NULL),
100     m_format (eFormatDefault),
101     m_value_is_valid (false),
102     m_value_did_change (false),
103     m_children_count_valid (false),
104     m_old_value_valid (false),
105     m_pointers_point_to_load_addrs (false),
106     m_is_deref_of_parent (false)
107 {
108     m_manager = new ValueObjectManager();
109     m_manager->ManageObject (this);
110 }
111 
112 //----------------------------------------------------------------------
113 // Destructor
114 //----------------------------------------------------------------------
115 ValueObject::~ValueObject ()
116 {
117 }
118 
119 bool
120 ValueObject::UpdateValueIfNeeded ()
121 {
122     // If this is a constant value, then our success is predicated on whether
123     // we have an error or not
124     if (GetIsConstant())
125         return m_error.Success();
126 
127     bool first_update = m_update_point.IsFirstEvaluation();
128 
129     if (m_update_point.NeedsUpdating())
130     {
131         m_update_point.SetUpdated();
132 
133         // Save the old value using swap to avoid a string copy which
134         // also will clear our m_value_str
135         if (m_value_str.empty())
136         {
137             m_old_value_valid = false;
138         }
139         else
140         {
141             m_old_value_valid = true;
142             m_old_value_str.swap (m_value_str);
143             m_value_str.clear();
144         }
145         m_location_str.clear();
146         m_summary_str.clear();
147         m_object_desc_str.clear();
148 
149         const bool value_was_valid = GetValueIsValid();
150         SetValueDidChange (false);
151 
152         m_error.Clear();
153 
154         // Call the pure virtual function to update the value
155         bool success = UpdateValue ();
156 
157         SetValueIsValid (success);
158 
159         if (first_update)
160             SetValueDidChange (false);
161         else if (!m_value_did_change && success == false)
162         {
163             // The value wasn't gotten successfully, so we mark this
164             // as changed if the value used to be valid and now isn't
165             SetValueDidChange (value_was_valid);
166         }
167     }
168     return m_error.Success();
169 }
170 
171 DataExtractor &
172 ValueObject::GetDataExtractor ()
173 {
174     UpdateValueIfNeeded();
175     return m_data;
176 }
177 
178 const Error &
179 ValueObject::GetError() const
180 {
181     return m_error;
182 }
183 
184 const ConstString &
185 ValueObject::GetName() const
186 {
187     return m_name;
188 }
189 
190 const char *
191 ValueObject::GetLocationAsCString ()
192 {
193     if (UpdateValueIfNeeded())
194     {
195         if (m_location_str.empty())
196         {
197             StreamString sstr;
198 
199             switch (m_value.GetValueType())
200             {
201             default:
202                 break;
203 
204             case Value::eValueTypeScalar:
205                 if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
206                 {
207                     RegisterInfo *reg_info = m_value.GetRegisterInfo();
208                     if (reg_info)
209                     {
210                         if (reg_info->name)
211                             m_location_str = reg_info->name;
212                         else if (reg_info->alt_name)
213                             m_location_str = reg_info->alt_name;
214                         break;
215                     }
216                 }
217                 m_location_str = "scalar";
218                 break;
219 
220             case Value::eValueTypeLoadAddress:
221             case Value::eValueTypeFileAddress:
222             case Value::eValueTypeHostAddress:
223                 {
224                     uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
225                     sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
226                     m_location_str.swap(sstr.GetString());
227                 }
228                 break;
229             }
230         }
231     }
232     return m_location_str.c_str();
233 }
234 
235 Value &
236 ValueObject::GetValue()
237 {
238     return m_value;
239 }
240 
241 const Value &
242 ValueObject::GetValue() const
243 {
244     return m_value;
245 }
246 
247 bool
248 ValueObject::ResolveValue (Scalar &scalar)
249 {
250     ExecutionContext exe_ctx;
251     ExecutionContextScope *exe_scope = GetExecutionContextScope();
252     if (exe_scope)
253         exe_scope->CalculateExecutionContext(exe_ctx);
254     scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
255     return scalar.IsValid();
256 }
257 
258 bool
259 ValueObject::GetValueIsValid () const
260 {
261     return m_value_is_valid;
262 }
263 
264 
265 void
266 ValueObject::SetValueIsValid (bool b)
267 {
268     m_value_is_valid = b;
269 }
270 
271 bool
272 ValueObject::GetValueDidChange ()
273 {
274     GetValueAsCString ();
275     return m_value_did_change;
276 }
277 
278 void
279 ValueObject::SetValueDidChange (bool value_changed)
280 {
281     m_value_did_change = value_changed;
282 }
283 
284 ValueObjectSP
285 ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
286 {
287     ValueObjectSP child_sp;
288     if (UpdateValueIfNeeded())
289     {
290         if (idx < GetNumChildren())
291         {
292             // Check if we have already made the child value object?
293             if (can_create && m_children[idx] == NULL)
294             {
295                 // No we haven't created the child at this index, so lets have our
296                 // subclass do it and cache the result for quick future access.
297                 m_children[idx] = CreateChildAtIndex (idx, false, 0);
298             }
299 
300             if (m_children[idx] != NULL)
301                 return m_children[idx]->GetSP();
302         }
303     }
304     return child_sp;
305 }
306 
307 uint32_t
308 ValueObject::GetIndexOfChildWithName (const ConstString &name)
309 {
310     bool omit_empty_base_classes = true;
311     return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
312                                                      GetClangType(),
313                                                      name.GetCString(),
314                                                      omit_empty_base_classes);
315 }
316 
317 ValueObjectSP
318 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
319 {
320     // when getting a child by name, it could be buried inside some base
321     // classes (which really aren't part of the expression path), so we
322     // need a vector of indexes that can get us down to the correct child
323     ValueObjectSP child_sp;
324 
325     if (UpdateValueIfNeeded())
326     {
327         std::vector<uint32_t> child_indexes;
328         clang::ASTContext *clang_ast = GetClangAST();
329         void *clang_type = GetClangType();
330         bool omit_empty_base_classes = true;
331         const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
332                                                                                           clang_type,
333                                                                                           name.GetCString(),
334                                                                                           omit_empty_base_classes,
335                                                                                           child_indexes);
336         if (num_child_indexes > 0)
337         {
338             std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
339             std::vector<uint32_t>::const_iterator end = child_indexes.end ();
340 
341             child_sp = GetChildAtIndex(*pos, can_create);
342             for (++pos; pos != end; ++pos)
343             {
344                 if (child_sp)
345                 {
346                     ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
347                     child_sp = new_child_sp;
348                 }
349                 else
350                 {
351                     child_sp.reset();
352                 }
353 
354             }
355         }
356     }
357     return child_sp;
358 }
359 
360 
361 uint32_t
362 ValueObject::GetNumChildren ()
363 {
364     if (!m_children_count_valid)
365     {
366         SetNumChildren (CalculateNumChildren());
367     }
368     return m_children.size();
369 }
370 void
371 ValueObject::SetNumChildren (uint32_t num_children)
372 {
373     m_children_count_valid = true;
374     m_children.resize(num_children);
375 }
376 
377 void
378 ValueObject::SetName (const char *name)
379 {
380     m_name.SetCString(name);
381 }
382 
383 void
384 ValueObject::SetName (const ConstString &name)
385 {
386     m_name = name;
387 }
388 
389 ValueObject *
390 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
391 {
392     ValueObject *valobj;
393 
394     if (UpdateValueIfNeeded())
395     {
396         bool omit_empty_base_classes = true;
397 
398         std::string child_name_str;
399         uint32_t child_byte_size = 0;
400         int32_t child_byte_offset = 0;
401         uint32_t child_bitfield_bit_size = 0;
402         uint32_t child_bitfield_bit_offset = 0;
403         bool child_is_base_class = false;
404         bool child_is_deref_of_parent = false;
405 
406         const bool transparent_pointers = synthetic_array_member == false;
407         clang::ASTContext *clang_ast = GetClangAST();
408         clang_type_t clang_type = GetClangType();
409         clang_type_t child_clang_type;
410         child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
411                                                                       GetName().GetCString(),
412                                                                       clang_type,
413                                                                       idx,
414                                                                       transparent_pointers,
415                                                                       omit_empty_base_classes,
416                                                                       child_name_str,
417                                                                       child_byte_size,
418                                                                       child_byte_offset,
419                                                                       child_bitfield_bit_size,
420                                                                       child_bitfield_bit_offset,
421                                                                       child_is_base_class,
422                                                                       child_is_deref_of_parent);
423         if (child_clang_type && child_byte_size)
424         {
425             if (synthetic_index)
426                 child_byte_offset += child_byte_size * synthetic_index;
427 
428             ConstString child_name;
429             if (!child_name_str.empty())
430                 child_name.SetCString (child_name_str.c_str());
431 
432             valobj = new ValueObjectChild (*this,
433                                            clang_ast,
434                                            child_clang_type,
435                                            child_name,
436                                            child_byte_size,
437                                            child_byte_offset,
438                                            child_bitfield_bit_size,
439                                            child_bitfield_bit_offset,
440                                            child_is_base_class,
441                                            child_is_deref_of_parent);
442             if (m_pointers_point_to_load_addrs)
443                 valobj->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
444         }
445     }
446 
447     return valobj;
448 }
449 
450 const char *
451 ValueObject::GetSummaryAsCString ()
452 {
453     if (UpdateValueIfNeeded ())
454     {
455         if (m_summary_str.empty())
456         {
457             clang_type_t clang_type = GetClangType();
458 
459             // See if this is a pointer to a C string?
460             if (clang_type)
461             {
462                 StreamString sstr;
463                 clang_type_t elem_or_pointee_clang_type;
464                 const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
465                                                                       GetClangAST(),
466                                                                       &elem_or_pointee_clang_type));
467 
468                 ExecutionContextScope *exe_scope = GetExecutionContextScope();
469                 if (exe_scope)
470                 {
471                     if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
472                         ClangASTContext::IsCharType (elem_or_pointee_clang_type))
473                     {
474                         Process *process = exe_scope->CalculateProcess();
475                         if (process != NULL)
476                         {
477                             lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
478                             AddressType cstr_address_type = eAddressTypeInvalid;
479 
480                             size_t cstr_len = 0;
481                             if (type_flags.Test (ClangASTContext::eTypeIsArray))
482                             {
483                                 // We have an array
484                                 cstr_len = ClangASTContext::GetArraySize (clang_type);
485                                 cstr_address = GetAddressOf (cstr_address_type, true);
486                             }
487                             else
488                             {
489                                 // We have a pointer
490                                 cstr_address = GetPointerValue (cstr_address_type, true);
491                             }
492                             if (cstr_address != LLDB_INVALID_ADDRESS)
493                             {
494                                 DataExtractor data;
495                                 size_t bytes_read = 0;
496                                 std::vector<char> data_buffer;
497                                 Error error;
498                                 if (cstr_len > 0)
499                                 {
500                                     data_buffer.resize(cstr_len);
501                                     data.SetData (&data_buffer.front(), data_buffer.size(), lldb::endian::InlHostByteOrder());
502                                     bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
503                                     if (bytes_read > 0)
504                                     {
505                                         sstr << '"';
506                                         data.Dump (&sstr,
507                                                    0,                 // Start offset in "data"
508                                                    eFormatChar,       // Print as characters
509                                                    1,                 // Size of item (1 byte for a char!)
510                                                    bytes_read,        // How many bytes to print?
511                                                    UINT32_MAX,        // num per line
512                                                    LLDB_INVALID_ADDRESS,// base address
513                                                    0,                 // bitfield bit size
514                                                    0);                // bitfield bit offset
515                                         sstr << '"';
516                                     }
517                                 }
518                                 else
519                                 {
520                                     const size_t k_max_buf_size = 256;
521                                     data_buffer.resize (k_max_buf_size + 1);
522                                     // NULL terminate in case we don't get the entire C string
523                                     data_buffer.back() = '\0';
524 
525                                     sstr << '"';
526 
527                                     data.SetData (&data_buffer.front(), data_buffer.size(), endian::InlHostByteOrder());
528                                     while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
529                                     {
530                                         size_t len = strlen(&data_buffer.front());
531                                         if (len == 0)
532                                             break;
533                                         if (len > bytes_read)
534                                             len = bytes_read;
535 
536                                         data.Dump (&sstr,
537                                                    0,                 // Start offset in "data"
538                                                    eFormatChar,       // Print as characters
539                                                    1,                 // Size of item (1 byte for a char!)
540                                                    len,               // How many bytes to print?
541                                                    UINT32_MAX,        // num per line
542                                                    LLDB_INVALID_ADDRESS,// base address
543                                                    0,                 // bitfield bit size
544                                                    0);                // bitfield bit offset
545 
546                                         if (len < k_max_buf_size)
547                                             break;
548                                         cstr_address += k_max_buf_size;
549                                     }
550                                     sstr << '"';
551                                 }
552                             }
553                         }
554 
555                         if (sstr.GetSize() > 0)
556                             m_summary_str.assign (sstr.GetData(), sstr.GetSize());
557                     }
558                     else if (ClangASTContext::IsFunctionPointerType (clang_type))
559                     {
560                         AddressType func_ptr_address_type = eAddressTypeInvalid;
561                         lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
562 
563                         if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
564                         {
565                             switch (func_ptr_address_type)
566                             {
567                             case eAddressTypeInvalid:
568                             case eAddressTypeFile:
569                                 break;
570 
571                             case eAddressTypeLoad:
572                                 {
573                                     Address so_addr;
574                                     Target *target = exe_scope->CalculateTarget();
575                                     if (target && target->GetSectionLoadList().IsEmpty() == false)
576                                     {
577                                         if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
578                                         {
579                                             so_addr.Dump (&sstr,
580                                                           exe_scope,
581                                                           Address::DumpStyleResolvedDescription,
582                                                           Address::DumpStyleSectionNameOffset);
583                                         }
584                                     }
585                                 }
586                                 break;
587 
588                             case eAddressTypeHost:
589                                 break;
590                             }
591                         }
592                         if (sstr.GetSize() > 0)
593                         {
594                             m_summary_str.assign (1, '(');
595                             m_summary_str.append (sstr.GetData(), sstr.GetSize());
596                             m_summary_str.append (1, ')');
597                         }
598                     }
599                 }
600             }
601         }
602     }
603     if (m_summary_str.empty())
604         return NULL;
605     return m_summary_str.c_str();
606 }
607 
608 const char *
609 ValueObject::GetObjectDescription ()
610 {
611     if (!m_object_desc_str.empty())
612         return m_object_desc_str.c_str();
613 
614     if (!UpdateValueIfNeeded ())
615         return NULL;
616 
617     ExecutionContextScope *exe_scope = GetExecutionContextScope();
618     if (exe_scope == NULL)
619         return NULL;
620 
621     Process *process = exe_scope->CalculateProcess();
622     if (process == NULL)
623         return NULL;
624 
625     StreamString s;
626 
627     lldb::LanguageType language = GetObjectRuntimeLanguage();
628     LanguageRuntime *runtime = process->GetLanguageRuntime(language);
629 
630     if (runtime == NULL)
631     {
632         // Aw, hell, if the things a pointer, or even just an integer, let's try ObjC anyway...
633         clang_type_t opaque_qual_type = GetClangType();
634         if (opaque_qual_type != NULL)
635         {
636             bool is_signed;
637             if (ClangASTContext::IsIntegerType (opaque_qual_type, is_signed)
638                 || ClangASTContext::IsPointerType (opaque_qual_type))
639             {
640                 runtime = process->GetLanguageRuntime(lldb::eLanguageTypeObjC);
641             }
642         }
643     }
644 
645     if (runtime && runtime->GetObjectDescription(s, *this))
646     {
647         m_object_desc_str.append (s.GetData());
648     }
649 
650     if (m_object_desc_str.empty())
651         return NULL;
652     else
653         return m_object_desc_str.c_str();
654 }
655 
656 const char *
657 ValueObject::GetValueAsCString ()
658 {
659     // If our byte size is zero this is an aggregate type that has children
660     if (ClangASTContext::IsAggregateType (GetClangType()) == false)
661     {
662         if (UpdateValueIfNeeded())
663         {
664             if (m_value_str.empty())
665             {
666                 const Value::ContextType context_type = m_value.GetContextType();
667 
668                 switch (context_type)
669                 {
670                 case Value::eContextTypeClangType:
671                 case Value::eContextTypeLLDBType:
672                 case Value::eContextTypeVariable:
673                     {
674                         clang_type_t clang_type = GetClangType ();
675                         if (clang_type)
676                         {
677                             StreamString sstr;
678                             Format format = GetFormat();
679                             if (format == eFormatDefault)
680                                 format = ClangASTType::GetFormat(clang_type);
681 
682                             if (ClangASTType::DumpTypeValue (GetClangAST(),            // The clang AST
683                                                              clang_type,               // The clang type to display
684                                                              &sstr,
685                                                              format,                   // Format to display this type with
686                                                              m_data,                   // Data to extract from
687                                                              0,                        // Byte offset into "m_data"
688                                                              GetByteSize(),            // Byte size of item in "m_data"
689                                                              GetBitfieldBitSize(),     // Bitfield bit size
690                                                              GetBitfieldBitOffset()))  // Bitfield bit offset
691                                 m_value_str.swap(sstr.GetString());
692                             else
693                                 m_value_str.clear();
694                         }
695                     }
696                     break;
697 
698                 case Value::eContextTypeRegisterInfo:
699                     {
700                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
701                         if (reg_info)
702                         {
703                             StreamString reg_sstr;
704                             m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
705                             m_value_str.swap(reg_sstr.GetString());
706                         }
707                     }
708                     break;
709 
710                 default:
711                     break;
712                 }
713             }
714 
715             if (!m_value_did_change && m_old_value_valid)
716             {
717                 // The value was gotten successfully, so we consider the
718                 // value as changed if the value string differs
719                 SetValueDidChange (m_old_value_str != m_value_str);
720             }
721         }
722     }
723     if (m_value_str.empty())
724         return NULL;
725     return m_value_str.c_str();
726 }
727 
728 addr_t
729 ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
730 {
731     if (!UpdateValueIfNeeded())
732         return LLDB_INVALID_ADDRESS;
733 
734     switch (m_value.GetValueType())
735     {
736     case Value::eValueTypeScalar:
737         if (scalar_is_load_address)
738         {
739             address_type = eAddressTypeLoad;
740             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
741         }
742         break;
743 
744     case Value::eValueTypeLoadAddress:
745     case Value::eValueTypeFileAddress:
746     case Value::eValueTypeHostAddress:
747         {
748             address_type = m_value.GetValueAddressType ();
749             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
750         }
751         break;
752     }
753     address_type = eAddressTypeInvalid;
754     return LLDB_INVALID_ADDRESS;
755 }
756 
757 addr_t
758 ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
759 {
760     lldb::addr_t address = LLDB_INVALID_ADDRESS;
761     address_type = eAddressTypeInvalid;
762 
763     if (!UpdateValueIfNeeded())
764         return address;
765 
766     switch (m_value.GetValueType())
767     {
768     case Value::eValueTypeScalar:
769         if (scalar_is_load_address)
770         {
771             address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
772             address_type = eAddressTypeLoad;
773         }
774         break;
775 
776     case Value::eValueTypeLoadAddress:
777     case Value::eValueTypeFileAddress:
778     case Value::eValueTypeHostAddress:
779         {
780             uint32_t data_offset = 0;
781             address = m_data.GetPointer(&data_offset);
782             address_type = m_value.GetValueAddressType();
783             if (address_type == eAddressTypeInvalid)
784                 address_type = eAddressTypeLoad;
785         }
786         break;
787     }
788 
789     if (m_pointers_point_to_load_addrs)
790         address_type = eAddressTypeLoad;
791 
792     return address;
793 }
794 
795 bool
796 ValueObject::SetValueFromCString (const char *value_str)
797 {
798     // Make sure our value is up to date first so that our location and location
799     // type is valid.
800     if (!UpdateValueIfNeeded())
801         return false;
802 
803     uint32_t count = 0;
804     lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
805 
806     char *end = NULL;
807     const size_t byte_size = GetByteSize();
808     switch (encoding)
809     {
810     case eEncodingInvalid:
811         return false;
812 
813     case eEncodingUint:
814         if (byte_size > sizeof(unsigned long long))
815         {
816             return false;
817         }
818         else
819         {
820             unsigned long long ull_val = strtoull(value_str, &end, 0);
821             if (end && *end != '\0')
822                 return false;
823             m_value = ull_val;
824             // Limit the bytes in our m_data appropriately.
825             m_value.GetScalar().GetData (m_data, byte_size);
826         }
827         break;
828 
829     case eEncodingSint:
830         if (byte_size > sizeof(long long))
831         {
832             return false;
833         }
834         else
835         {
836             long long sll_val = strtoll(value_str, &end, 0);
837             if (end && *end != '\0')
838                 return false;
839             m_value = sll_val;
840             // Limit the bytes in our m_data appropriately.
841             m_value.GetScalar().GetData (m_data, byte_size);
842         }
843         break;
844 
845     case eEncodingIEEE754:
846         {
847             const off_t byte_offset = GetByteOffset();
848             uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
849             if (dst != NULL)
850             {
851                 // We are decoding a float into host byte order below, so make
852                 // sure m_data knows what it contains.
853                 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
854                 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
855                                                         GetClangAST(),
856                                                         GetClangType(),
857                                                         value_str,
858                                                         dst,
859                                                         byte_size);
860 
861                 if (converted_byte_size == byte_size)
862                 {
863                 }
864             }
865         }
866         break;
867 
868     case eEncodingVector:
869         return false;
870 
871     default:
872         return false;
873     }
874 
875     // If we have made it here the value is in m_data and we should write it
876     // out to the target
877     return Write ();
878 }
879 
880 bool
881 ValueObject::Write ()
882 {
883     // Clear the update ID so the next time we try and read the value
884     // we try and read it again.
885     m_update_point.SetNeedsUpdate();
886 
887     // TODO: when Value has a method to write a value back, call it from here.
888     return false;
889 
890 }
891 
892 lldb::LanguageType
893 ValueObject::GetObjectRuntimeLanguage ()
894 {
895     clang_type_t opaque_qual_type = GetClangType();
896     if (opaque_qual_type == NULL)
897         return lldb::eLanguageTypeC;
898 
899     // If the type is a reference, then resolve it to what it refers to first:
900     clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
901     if (qual_type->isAnyPointerType())
902     {
903         if (qual_type->isObjCObjectPointerType())
904             return lldb::eLanguageTypeObjC;
905 
906         clang::QualType pointee_type (qual_type->getPointeeType());
907         if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
908             return lldb::eLanguageTypeC_plus_plus;
909         if (pointee_type->isObjCObjectOrInterfaceType())
910             return lldb::eLanguageTypeObjC;
911         if (pointee_type->isObjCClassType())
912             return lldb::eLanguageTypeObjC;
913     }
914     else
915     {
916         if (ClangASTContext::IsObjCClassType (opaque_qual_type))
917             return lldb::eLanguageTypeObjC;
918         if (ClangASTContext::IsCXXClassType (opaque_qual_type))
919             return lldb::eLanguageTypeC_plus_plus;
920     }
921 
922     return lldb::eLanguageTypeC;
923 }
924 
925 void
926 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
927 {
928     m_synthetic_children[key] = valobj;
929 }
930 
931 ValueObjectSP
932 ValueObject::GetSyntheticChild (const ConstString &key) const
933 {
934     ValueObjectSP synthetic_child_sp;
935     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
936     if (pos != m_synthetic_children.end())
937         synthetic_child_sp = pos->second->GetSP();
938     return synthetic_child_sp;
939 }
940 
941 bool
942 ValueObject::IsPointerType ()
943 {
944     return ClangASTContext::IsPointerType (GetClangType());
945 }
946 
947 bool
948 ValueObject::IsIntegerType (bool &is_signed)
949 {
950     return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
951 }
952 
953 bool
954 ValueObject::IsPointerOrReferenceType ()
955 {
956     return ClangASTContext::IsPointerOrReferenceType(GetClangType());
957 }
958 
959 ValueObjectSP
960 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
961 {
962     ValueObjectSP synthetic_child_sp;
963     if (IsPointerType ())
964     {
965         char index_str[64];
966         snprintf(index_str, sizeof(index_str), "[%i]", index);
967         ConstString index_const_str(index_str);
968         // Check if we have already created a synthetic array member in this
969         // valid object. If we have we will re-use it.
970         synthetic_child_sp = GetSyntheticChild (index_const_str);
971         if (!synthetic_child_sp)
972         {
973             ValueObject *synthetic_child;
974             // We haven't made a synthetic array member for INDEX yet, so
975             // lets make one and cache it for any future reference.
976             synthetic_child = CreateChildAtIndex(0, true, index);
977 
978             // Cache the value if we got one back...
979             if (synthetic_child)
980             {
981                 AddSyntheticChild(index_const_str, synthetic_child);
982                 synthetic_child_sp = synthetic_child->GetSP();
983             }
984         }
985     }
986     return synthetic_child_sp;
987 }
988 
989 void
990 ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
991 {
992     if (use_dynamic == lldb::eNoDynamicValues)
993         return;
994 
995     if (!m_dynamic_value && !IsDynamic())
996     {
997         Process *process = m_update_point.GetProcess();
998         bool worth_having_dynamic_value = false;
999 
1000 
1001         // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1002         // hard code this everywhere.
1003         lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1004         if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1005         {
1006             LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1007             if (runtime)
1008                 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1009         }
1010         else
1011         {
1012             LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1013             if (cpp_runtime)
1014                 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1015 
1016             if (!worth_having_dynamic_value)
1017             {
1018                 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1019                 if (objc_runtime)
1020                     worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
1021             }
1022         }
1023 
1024         if (worth_having_dynamic_value)
1025             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
1026 
1027 //        if (worth_having_dynamic_value)
1028 //            printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1029 
1030     }
1031 }
1032 
1033 ValueObjectSP
1034 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
1035 {
1036     if (use_dynamic == lldb::eNoDynamicValues)
1037         return ValueObjectSP();
1038 
1039     if (!IsDynamic() && m_dynamic_value == NULL)
1040     {
1041         CalculateDynamicValue(use_dynamic);
1042     }
1043     if (m_dynamic_value)
1044         return m_dynamic_value->GetSP();
1045     else
1046         return ValueObjectSP();
1047 }
1048 
1049 bool
1050 ValueObject::GetBaseClassPath (Stream &s)
1051 {
1052     if (IsBaseClass())
1053     {
1054         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
1055         clang_type_t clang_type = GetClangType();
1056         std::string cxx_class_name;
1057         bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1058         if (this_had_base_class)
1059         {
1060             if (parent_had_base_class)
1061                 s.PutCString("::");
1062             s.PutCString(cxx_class_name.c_str());
1063         }
1064         return parent_had_base_class || this_had_base_class;
1065     }
1066     return false;
1067 }
1068 
1069 
1070 ValueObject *
1071 ValueObject::GetNonBaseClassParent()
1072 {
1073     if (GetParent())
1074     {
1075         if (GetParent()->IsBaseClass())
1076             return GetParent()->GetNonBaseClassParent();
1077         else
1078             return GetParent();
1079     }
1080     return NULL;
1081 }
1082 
1083 void
1084 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
1085 {
1086     const bool is_deref_of_parent = IsDereferenceOfParent ();
1087 
1088     if (is_deref_of_parent)
1089         s.PutCString("*(");
1090 
1091     if (GetParent())
1092         GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
1093 
1094     if (!IsBaseClass())
1095     {
1096         if (!is_deref_of_parent)
1097         {
1098             ValueObject *non_base_class_parent = GetNonBaseClassParent();
1099             if (non_base_class_parent)
1100             {
1101                 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1102                 if (non_base_class_parent_clang_type)
1103                 {
1104                     const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1105 
1106                     if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1107                     {
1108                         s.PutCString("->");
1109                     }
1110                     else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1111                         !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1112                     {
1113                         s.PutChar('.');
1114                     }
1115                 }
1116             }
1117 
1118             const char *name = GetName().GetCString();
1119             if (name)
1120             {
1121                 if (qualify_cxx_base_classes)
1122                 {
1123                     if (GetBaseClassPath (s))
1124                         s.PutCString("::");
1125                 }
1126                 s.PutCString(name);
1127             }
1128         }
1129     }
1130 
1131     if (is_deref_of_parent)
1132         s.PutChar(')');
1133 }
1134 
1135 void
1136 ValueObject::DumpValueObject
1137 (
1138     Stream &s,
1139     ValueObject *valobj,
1140     const char *root_valobj_name,
1141     uint32_t ptr_depth,
1142     uint32_t curr_depth,
1143     uint32_t max_depth,
1144     bool show_types,
1145     bool show_location,
1146     bool use_objc,
1147     lldb::DynamicValueType use_dynamic,
1148     bool scope_already_checked,
1149     bool flat_output
1150 )
1151 {
1152     if (valobj  && valobj->UpdateValueIfNeeded ())
1153     {
1154         if (use_dynamic != lldb::eNoDynamicValues)
1155         {
1156             ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
1157             if (dynamic_value)
1158                 valobj = dynamic_value;
1159         }
1160 
1161         clang_type_t clang_type = valobj->GetClangType();
1162 
1163         const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
1164         const char *err_cstr = NULL;
1165         const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1166         const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
1167 
1168         const bool print_valobj = flat_output == false || has_value;
1169 
1170         if (print_valobj)
1171         {
1172             if (show_location)
1173             {
1174                 s.Printf("%s: ", valobj->GetLocationAsCString());
1175             }
1176 
1177             s.Indent();
1178 
1179             // Always show the type for the top level items.
1180             if (show_types || (curr_depth == 0 && !flat_output))
1181                 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
1182 
1183 
1184             if (flat_output)
1185             {
1186                 // If we are showing types, also qualify the C++ base classes
1187                 const bool qualify_cxx_base_classes = show_types;
1188                 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
1189                 s.PutCString(" =");
1190             }
1191             else
1192             {
1193                 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1194                 s.Printf ("%s =", name_cstr);
1195             }
1196 
1197             if (!scope_already_checked && !valobj->IsInScope())
1198             {
1199                 err_cstr = "error: out of scope";
1200             }
1201         }
1202 
1203         const char *val_cstr = NULL;
1204 
1205         if (err_cstr == NULL)
1206         {
1207             val_cstr = valobj->GetValueAsCString();
1208             err_cstr = valobj->GetError().AsCString();
1209         }
1210 
1211         if (err_cstr)
1212         {
1213             s.Printf (" error: %s\n", err_cstr);
1214         }
1215         else
1216         {
1217             const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
1218             if (print_valobj)
1219             {
1220                 const char *sum_cstr = valobj->GetSummaryAsCString();
1221 
1222                 if (val_cstr)
1223                     s.Printf(" %s", val_cstr);
1224 
1225                 if (sum_cstr)
1226                     s.Printf(" %s", sum_cstr);
1227 
1228                 if (use_objc)
1229                 {
1230                     const char *object_desc = valobj->GetObjectDescription();
1231                     if (object_desc)
1232                         s.Printf(" %s\n", object_desc);
1233                     else
1234                         s.Printf (" [no Objective-C description available]\n");
1235                     return;
1236                 }
1237             }
1238 
1239             if (curr_depth < max_depth)
1240             {
1241                 // We will show children for all concrete types. We won't show
1242                 // pointer contents unless a pointer depth has been specified.
1243                 // We won't reference contents unless the reference is the
1244                 // root object (depth of zero).
1245                 bool print_children = true;
1246 
1247                 // Use a new temporary pointer depth in case we override the
1248                 // current pointer depth below...
1249                 uint32_t curr_ptr_depth = ptr_depth;
1250 
1251                 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1252                 if (is_ptr || is_ref)
1253                 {
1254                     // We have a pointer or reference whose value is an address.
1255                     // Make sure that address is not NULL
1256                     AddressType ptr_address_type;
1257                     if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1258                         print_children = false;
1259 
1260                     else if (is_ref && curr_depth == 0)
1261                     {
1262                         // If this is the root object (depth is zero) that we are showing
1263                         // and it is a reference, and no pointer depth has been supplied
1264                         // print out what it references. Don't do this at deeper depths
1265                         // otherwise we can end up with infinite recursion...
1266                         curr_ptr_depth = 1;
1267                     }
1268 
1269                     if (curr_ptr_depth == 0)
1270                         print_children = false;
1271                 }
1272 
1273                 if (print_children)
1274                 {
1275                     const uint32_t num_children = valobj->GetNumChildren();
1276                     if (num_children)
1277                     {
1278                         if (flat_output)
1279                         {
1280                             if (print_valobj)
1281                                 s.EOL();
1282                         }
1283                         else
1284                         {
1285                             if (print_valobj)
1286                                 s.PutCString(is_ref ? ": {\n" : " {\n");
1287                             s.IndentMore();
1288                         }
1289 
1290                         for (uint32_t idx=0; idx<num_children; ++idx)
1291                         {
1292                             ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1293                             if (child_sp.get())
1294                             {
1295                                 DumpValueObject (s,
1296                                                  child_sp.get(),
1297                                                  NULL,
1298                                                  (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
1299                                                  curr_depth + 1,
1300                                                  max_depth,
1301                                                  show_types,
1302                                                  show_location,
1303                                                  false,
1304                                                  use_dynamic,
1305                                                  true,
1306                                                  flat_output);
1307                             }
1308                         }
1309 
1310                         if (!flat_output)
1311                         {
1312                             s.IndentLess();
1313                             s.Indent("}\n");
1314                         }
1315                     }
1316                     else if (has_children)
1317                     {
1318                         // Aggregate, no children...
1319                         if (print_valobj)
1320                             s.PutCString(" {}\n");
1321                     }
1322                     else
1323                     {
1324                         if (print_valobj)
1325                             s.EOL();
1326                     }
1327 
1328                 }
1329                 else
1330                 {
1331                     s.EOL();
1332                 }
1333             }
1334             else
1335             {
1336                 if (has_children && print_valobj)
1337                 {
1338                     s.PutCString("{...}\n");
1339                 }
1340             }
1341         }
1342     }
1343 }
1344 
1345 
1346 ValueObjectSP
1347 ValueObject::CreateConstantValue (const ConstString &name)
1348 {
1349     ValueObjectSP valobj_sp;
1350 
1351     if (UpdateValueIfNeeded() && m_error.Success())
1352     {
1353         ExecutionContextScope *exe_scope = GetExecutionContextScope();
1354         if (exe_scope)
1355         {
1356             ExecutionContext exe_ctx;
1357             exe_scope->CalculateExecutionContext(exe_ctx);
1358 
1359             clang::ASTContext *ast = GetClangAST ();
1360 
1361             DataExtractor data;
1362             data.SetByteOrder (m_data.GetByteOrder());
1363             data.SetAddressByteSize(m_data.GetAddressByteSize());
1364 
1365             m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1366 
1367             valobj_sp = ValueObjectConstResult::Create (exe_scope,
1368                                                         ast,
1369                                                         GetClangType(),
1370                                                         name,
1371                                                         data);
1372         }
1373     }
1374 
1375     if (!valobj_sp)
1376     {
1377         valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
1378     }
1379     return valobj_sp;
1380 }
1381 
1382 lldb::ValueObjectSP
1383 ValueObject::Dereference (Error &error)
1384 {
1385     if (m_deref_valobj)
1386         return m_deref_valobj->GetSP();
1387 
1388     const bool is_pointer_type = IsPointerType();
1389     if (is_pointer_type)
1390     {
1391         bool omit_empty_base_classes = true;
1392 
1393         std::string child_name_str;
1394         uint32_t child_byte_size = 0;
1395         int32_t child_byte_offset = 0;
1396         uint32_t child_bitfield_bit_size = 0;
1397         uint32_t child_bitfield_bit_offset = 0;
1398         bool child_is_base_class = false;
1399         bool child_is_deref_of_parent = false;
1400         const bool transparent_pointers = false;
1401         clang::ASTContext *clang_ast = GetClangAST();
1402         clang_type_t clang_type = GetClangType();
1403         clang_type_t child_clang_type;
1404         child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1405                                                                       GetName().GetCString(),
1406                                                                       clang_type,
1407                                                                       0,
1408                                                                       transparent_pointers,
1409                                                                       omit_empty_base_classes,
1410                                                                       child_name_str,
1411                                                                       child_byte_size,
1412                                                                       child_byte_offset,
1413                                                                       child_bitfield_bit_size,
1414                                                                       child_bitfield_bit_offset,
1415                                                                       child_is_base_class,
1416                                                                       child_is_deref_of_parent);
1417         if (child_clang_type && child_byte_size)
1418         {
1419             ConstString child_name;
1420             if (!child_name_str.empty())
1421                 child_name.SetCString (child_name_str.c_str());
1422 
1423             m_deref_valobj = new ValueObjectChild (*this,
1424                                                    clang_ast,
1425                                                    child_clang_type,
1426                                                    child_name,
1427                                                    child_byte_size,
1428                                                    child_byte_offset,
1429                                                    child_bitfield_bit_size,
1430                                                    child_bitfield_bit_offset,
1431                                                    child_is_base_class,
1432                                                    child_is_deref_of_parent);
1433         }
1434     }
1435 
1436     if (m_deref_valobj)
1437     {
1438         error.Clear();
1439         return m_deref_valobj->GetSP();
1440     }
1441     else
1442     {
1443         StreamString strm;
1444         GetExpressionPath(strm, true);
1445 
1446         if (is_pointer_type)
1447             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1448         else
1449             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1450         return ValueObjectSP();
1451     }
1452 }
1453 
1454 lldb::ValueObjectSP
1455 ValueObject::AddressOf (Error &error)
1456 {
1457     if (m_addr_of_valobj_sp)
1458         return m_addr_of_valobj_sp;
1459 
1460     AddressType address_type = eAddressTypeInvalid;
1461     const bool scalar_is_load_address = false;
1462     lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
1463     error.Clear();
1464     if (addr != LLDB_INVALID_ADDRESS)
1465     {
1466         switch (address_type)
1467         {
1468         default:
1469         case eAddressTypeInvalid:
1470             {
1471                 StreamString expr_path_strm;
1472                 GetExpressionPath(expr_path_strm, true);
1473                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1474             }
1475             break;
1476 
1477         case eAddressTypeFile:
1478         case eAddressTypeLoad:
1479         case eAddressTypeHost:
1480             {
1481                 clang::ASTContext *ast = GetClangAST();
1482                 clang_type_t clang_type = GetClangType();
1483                 if (ast && clang_type)
1484                 {
1485                     std::string name (1, '&');
1486                     name.append (m_name.AsCString(""));
1487                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1488                                                                           ast,
1489                                                                           ClangASTContext::CreatePointerType (ast, clang_type),
1490                                                                           ConstString (name.c_str()),
1491                                                                           addr,
1492                                                                           eAddressTypeInvalid,
1493                                                                           m_data.GetAddressByteSize());
1494                 }
1495             }
1496             break;
1497         }
1498     }
1499     return m_addr_of_valobj_sp;
1500 }
1501 
1502 
1503 lldb::ValueObjectSP
1504 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
1505 {
1506     lldb::ValueObjectSP valobj_sp;
1507     AddressType address_type;
1508     const bool scalar_is_load_address = true;
1509     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1510 
1511     if (ptr_value != LLDB_INVALID_ADDRESS)
1512     {
1513         Address ptr_addr (NULL, ptr_value);
1514 
1515         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1516                                                name,
1517                                                ptr_addr,
1518                                                clang_ast_type);
1519     }
1520     return valobj_sp;
1521 }
1522 
1523 lldb::ValueObjectSP
1524 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
1525 {
1526     lldb::ValueObjectSP valobj_sp;
1527     AddressType address_type;
1528     const bool scalar_is_load_address = true;
1529     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1530 
1531     if (ptr_value != LLDB_INVALID_ADDRESS)
1532     {
1533         Address ptr_addr (NULL, ptr_value);
1534 
1535         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1536                                                name,
1537                                                ptr_addr,
1538                                                type_sp);
1539     }
1540     return valobj_sp;
1541 }
1542 
1543 
1544 ValueObject::EvaluationPoint::EvaluationPoint () :
1545     m_thread_id (LLDB_INVALID_UID),
1546     m_stop_id (0)
1547 {
1548 }
1549 
1550 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
1551     m_needs_update (true),
1552     m_first_update (true),
1553     m_thread_id (LLDB_INVALID_UID),
1554     m_stop_id (0)
1555 
1556 {
1557     ExecutionContext exe_ctx;
1558     ExecutionContextScope *computed_exe_scope = exe_scope;  // If use_selected is true, we may find a better scope,
1559                                                             // and if so we want to cache that not the original.
1560     if (exe_scope)
1561         exe_scope->CalculateExecutionContext(exe_ctx);
1562     if (exe_ctx.target != NULL)
1563     {
1564         m_target_sp = exe_ctx.target->GetSP();
1565 
1566         if (exe_ctx.process == NULL)
1567             m_process_sp = exe_ctx.target->GetProcessSP();
1568         else
1569             m_process_sp = exe_ctx.process->GetSP();
1570 
1571         if (m_process_sp != NULL)
1572         {
1573             m_stop_id = m_process_sp->GetStopID();
1574             Thread *thread = NULL;
1575 
1576             if (exe_ctx.thread == NULL)
1577             {
1578                 if (use_selected)
1579                 {
1580                     thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1581                     if (thread)
1582                         computed_exe_scope = thread;
1583                 }
1584             }
1585             else
1586                 thread = exe_ctx.thread;
1587 
1588             if (thread != NULL)
1589             {
1590                 m_thread_id = thread->GetIndexID();
1591                 if (exe_ctx.frame == NULL)
1592                 {
1593                     if (use_selected)
1594                     {
1595                         StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1596                         if (frame)
1597                         {
1598                             m_stack_id = frame->GetStackID();
1599                             computed_exe_scope = frame;
1600                         }
1601                     }
1602                 }
1603                 else
1604                     m_stack_id = exe_ctx.frame->GetStackID();
1605             }
1606         }
1607     }
1608     m_exe_scope = computed_exe_scope;
1609 }
1610 
1611 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1612     m_exe_scope (rhs.m_exe_scope),
1613     m_needs_update(true),
1614     m_first_update(true),
1615     m_target_sp (rhs.m_target_sp),
1616     m_process_sp (rhs.m_process_sp),
1617     m_thread_id (rhs.m_thread_id),
1618     m_stack_id (rhs.m_stack_id),
1619     m_stop_id (0)
1620 {
1621 }
1622 
1623 ValueObject::EvaluationPoint::~EvaluationPoint ()
1624 {
1625 }
1626 
1627 ExecutionContextScope *
1628 ValueObject::EvaluationPoint::GetExecutionContextScope ()
1629 {
1630     // We have to update before giving out the scope, or we could be handing out stale pointers.
1631     SyncWithProcessState();
1632 
1633     return m_exe_scope;
1634 }
1635 
1636 // This function checks the EvaluationPoint against the current process state.  If the current
1637 // state matches the evaluation point, or the evaluation point is already invalid, then we return
1638 // false, meaning "no change".  If the current state is different, we update our state, and return
1639 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
1640 // future calls to NeedsUpdate will return true.
1641 
1642 bool
1643 ValueObject::EvaluationPoint::SyncWithProcessState()
1644 {
1645     // If we're already invalid, we don't need to do anything, and nothing has changed:
1646     if (m_stop_id == LLDB_INVALID_UID)
1647     {
1648         // Can't update with an invalid state.
1649         m_needs_update = false;
1650         return false;
1651     }
1652 
1653     // If we don't have a process nothing can change.
1654     if (!m_process_sp)
1655         return false;
1656 
1657     // If our stop id is the current stop ID, nothing has changed:
1658     uint32_t cur_stop_id = m_process_sp->GetStopID();
1659     if (m_stop_id == cur_stop_id)
1660         return false;
1661 
1662     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1663     // In either case, we aren't going to be able to sync with the process state.
1664     if (cur_stop_id == 0)
1665         return false;
1666 
1667     m_stop_id = cur_stop_id;
1668     m_needs_update = true;
1669     m_exe_scope = m_process_sp.get();
1670 
1671     // Something has changed, so we will return true.  Now make sure the thread & frame still exist, and if either
1672     // doesn't, mark ourselves as invalid.
1673 
1674     if (m_thread_id != LLDB_INVALID_THREAD_ID)
1675     {
1676         Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1677         if (our_thread == NULL)
1678             SetInvalid();
1679         else
1680         {
1681             m_exe_scope = our_thread;
1682 
1683             if (m_stack_id.IsValid())
1684             {
1685                 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1686                 if (our_frame == NULL)
1687                     SetInvalid();
1688                 else
1689                     m_exe_scope = our_frame;
1690             }
1691         }
1692     }
1693     return true;
1694 }
1695 
1696 void
1697 ValueObject::EvaluationPoint::SetUpdated ()
1698 {
1699     m_first_update = false;
1700     m_needs_update = false;
1701     if (m_process_sp)
1702         m_stop_id = m_process_sp->GetStopID();
1703 }
1704 
1705 
1706 bool
1707 ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1708 {
1709     if (!IsValid())
1710         return false;
1711 
1712     bool needs_update = false;
1713     m_exe_scope = NULL;
1714 
1715     // The target has to be non-null, and the
1716     Target *target = exe_scope->CalculateTarget();
1717     if (target != NULL)
1718     {
1719         Target *old_target = m_target_sp.get();
1720         assert (target == old_target);
1721         Process *process = exe_scope->CalculateProcess();
1722         if (process != NULL)
1723         {
1724             // FOR NOW - assume you can't update variable objects across process boundaries.
1725             Process *old_process = m_process_sp.get();
1726             assert (process == old_process);
1727 
1728             lldb::user_id_t stop_id = process->GetStopID();
1729             if (stop_id != m_stop_id)
1730             {
1731                 needs_update = true;
1732                 m_stop_id = stop_id;
1733             }
1734             // See if we're switching the thread or stack context.  If no thread is given, this is
1735             // being evaluated in a global context.
1736             Thread *thread = exe_scope->CalculateThread();
1737             if (thread != NULL)
1738             {
1739                 lldb::user_id_t new_thread_index = thread->GetIndexID();
1740                 if (new_thread_index != m_thread_id)
1741                 {
1742                     needs_update = true;
1743                     m_thread_id = new_thread_index;
1744                     m_stack_id.Clear();
1745                 }
1746 
1747                 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1748                 if (new_frame != NULL)
1749                 {
1750                     if (new_frame->GetStackID() != m_stack_id)
1751                     {
1752                         needs_update = true;
1753                         m_stack_id = new_frame->GetStackID();
1754                     }
1755                 }
1756                 else
1757                 {
1758                     m_stack_id.Clear();
1759                     needs_update = true;
1760                 }
1761             }
1762             else
1763             {
1764                 // If this had been given a thread, and now there is none, we should update.
1765                 // Otherwise we don't have to do anything.
1766                 if (m_thread_id != LLDB_INVALID_UID)
1767                 {
1768                     m_thread_id = LLDB_INVALID_UID;
1769                     m_stack_id.Clear();
1770                     needs_update = true;
1771                 }
1772             }
1773         }
1774         else
1775         {
1776             // If there is no process, then we don't need to update anything.
1777             // But if we're switching from having a process to not, we should try to update.
1778             if (m_process_sp.get() != NULL)
1779             {
1780                 needs_update = true;
1781                 m_process_sp.reset();
1782                 m_thread_id = LLDB_INVALID_UID;
1783                 m_stack_id.Clear();
1784             }
1785         }
1786     }
1787     else
1788     {
1789         // If there's no target, nothing can change so we don't need to update anything.
1790         // But if we're switching from having a target to not, we should try to update.
1791         if (m_target_sp.get() != NULL)
1792         {
1793             needs_update = true;
1794             m_target_sp.reset();
1795             m_process_sp.reset();
1796             m_thread_id = LLDB_INVALID_UID;
1797             m_stack_id.Clear();
1798         }
1799     }
1800     if (!m_needs_update)
1801         m_needs_update = needs_update;
1802 
1803     return needs_update;
1804 }
1805