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 = NULL;
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                             {
694                                 m_error.SetErrorStringWithFormat ("unsufficient data for value (only %u of %u bytes available)",
695                                                                   m_data.GetByteSize(),
696                                                                   GetByteSize());
697                                 m_value_str.clear();
698                             }
699                         }
700                     }
701                     break;
702 
703                 case Value::eContextTypeRegisterInfo:
704                     {
705                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
706                         if (reg_info)
707                         {
708                             StreamString reg_sstr;
709                             m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
710                             m_value_str.swap(reg_sstr.GetString());
711                         }
712                     }
713                     break;
714 
715                 default:
716                     break;
717                 }
718             }
719 
720             if (!m_value_did_change && m_old_value_valid)
721             {
722                 // The value was gotten successfully, so we consider the
723                 // value as changed if the value string differs
724                 SetValueDidChange (m_old_value_str != m_value_str);
725             }
726         }
727     }
728     if (m_value_str.empty())
729         return NULL;
730     return m_value_str.c_str();
731 }
732 
733 addr_t
734 ValueObject::GetAddressOf (AddressType &address_type, bool scalar_is_load_address)
735 {
736     if (!UpdateValueIfNeeded())
737         return LLDB_INVALID_ADDRESS;
738 
739     switch (m_value.GetValueType())
740     {
741     case Value::eValueTypeScalar:
742         if (scalar_is_load_address)
743         {
744             address_type = eAddressTypeLoad;
745             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
746         }
747         break;
748 
749     case Value::eValueTypeLoadAddress:
750     case Value::eValueTypeFileAddress:
751     case Value::eValueTypeHostAddress:
752         {
753             address_type = m_value.GetValueAddressType ();
754             return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
755         }
756         break;
757     }
758     address_type = eAddressTypeInvalid;
759     return LLDB_INVALID_ADDRESS;
760 }
761 
762 addr_t
763 ValueObject::GetPointerValue (AddressType &address_type, bool scalar_is_load_address)
764 {
765     lldb::addr_t address = LLDB_INVALID_ADDRESS;
766     address_type = eAddressTypeInvalid;
767 
768     if (!UpdateValueIfNeeded())
769         return address;
770 
771     switch (m_value.GetValueType())
772     {
773     case Value::eValueTypeScalar:
774         if (scalar_is_load_address)
775         {
776             address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
777             address_type = eAddressTypeLoad;
778         }
779         break;
780 
781     case Value::eValueTypeLoadAddress:
782     case Value::eValueTypeFileAddress:
783     case Value::eValueTypeHostAddress:
784         {
785             uint32_t data_offset = 0;
786             address = m_data.GetPointer(&data_offset);
787             address_type = m_value.GetValueAddressType();
788             if (address_type == eAddressTypeInvalid)
789                 address_type = eAddressTypeLoad;
790         }
791         break;
792     }
793 
794     if (m_pointers_point_to_load_addrs)
795         address_type = eAddressTypeLoad;
796 
797     return address;
798 }
799 
800 bool
801 ValueObject::SetValueFromCString (const char *value_str)
802 {
803     // Make sure our value is up to date first so that our location and location
804     // type is valid.
805     if (!UpdateValueIfNeeded())
806         return false;
807 
808     uint32_t count = 0;
809     lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
810 
811     char *end = NULL;
812     const size_t byte_size = GetByteSize();
813     switch (encoding)
814     {
815     case eEncodingInvalid:
816         return false;
817 
818     case eEncodingUint:
819         if (byte_size > sizeof(unsigned long long))
820         {
821             return false;
822         }
823         else
824         {
825             unsigned long long ull_val = strtoull(value_str, &end, 0);
826             if (end && *end != '\0')
827                 return false;
828             m_value = ull_val;
829             // Limit the bytes in our m_data appropriately.
830             m_value.GetScalar().GetData (m_data, byte_size);
831         }
832         break;
833 
834     case eEncodingSint:
835         if (byte_size > sizeof(long long))
836         {
837             return false;
838         }
839         else
840         {
841             long long sll_val = strtoll(value_str, &end, 0);
842             if (end && *end != '\0')
843                 return false;
844             m_value = sll_val;
845             // Limit the bytes in our m_data appropriately.
846             m_value.GetScalar().GetData (m_data, byte_size);
847         }
848         break;
849 
850     case eEncodingIEEE754:
851         {
852             const off_t byte_offset = GetByteOffset();
853             uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
854             if (dst != NULL)
855             {
856                 // We are decoding a float into host byte order below, so make
857                 // sure m_data knows what it contains.
858                 m_data.SetByteOrder(lldb::endian::InlHostByteOrder());
859                 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
860                                                         GetClangAST(),
861                                                         GetClangType(),
862                                                         value_str,
863                                                         dst,
864                                                         byte_size);
865 
866                 if (converted_byte_size == byte_size)
867                 {
868                 }
869             }
870         }
871         break;
872 
873     case eEncodingVector:
874         return false;
875 
876     default:
877         return false;
878     }
879 
880     // If we have made it here the value is in m_data and we should write it
881     // out to the target
882     return Write ();
883 }
884 
885 bool
886 ValueObject::Write ()
887 {
888     // Clear the update ID so the next time we try and read the value
889     // we try and read it again.
890     m_update_point.SetNeedsUpdate();
891 
892     // TODO: when Value has a method to write a value back, call it from here.
893     return false;
894 
895 }
896 
897 lldb::LanguageType
898 ValueObject::GetObjectRuntimeLanguage ()
899 {
900     clang_type_t opaque_qual_type = GetClangType();
901     if (opaque_qual_type == NULL)
902         return lldb::eLanguageTypeC;
903 
904     // If the type is a reference, then resolve it to what it refers to first:
905     clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
906     if (qual_type->isAnyPointerType())
907     {
908         if (qual_type->isObjCObjectPointerType())
909             return lldb::eLanguageTypeObjC;
910 
911         clang::QualType pointee_type (qual_type->getPointeeType());
912         if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
913             return lldb::eLanguageTypeC_plus_plus;
914         if (pointee_type->isObjCObjectOrInterfaceType())
915             return lldb::eLanguageTypeObjC;
916         if (pointee_type->isObjCClassType())
917             return lldb::eLanguageTypeObjC;
918     }
919     else
920     {
921         if (ClangASTContext::IsObjCClassType (opaque_qual_type))
922             return lldb::eLanguageTypeObjC;
923         if (ClangASTContext::IsCXXClassType (opaque_qual_type))
924             return lldb::eLanguageTypeC_plus_plus;
925     }
926 
927     return lldb::eLanguageTypeC;
928 }
929 
930 void
931 ValueObject::AddSyntheticChild (const ConstString &key, ValueObject *valobj)
932 {
933     m_synthetic_children[key] = valobj;
934 }
935 
936 ValueObjectSP
937 ValueObject::GetSyntheticChild (const ConstString &key) const
938 {
939     ValueObjectSP synthetic_child_sp;
940     std::map<ConstString, ValueObject *>::const_iterator pos = m_synthetic_children.find (key);
941     if (pos != m_synthetic_children.end())
942         synthetic_child_sp = pos->second->GetSP();
943     return synthetic_child_sp;
944 }
945 
946 bool
947 ValueObject::IsPointerType ()
948 {
949     return ClangASTContext::IsPointerType (GetClangType());
950 }
951 
952 bool
953 ValueObject::IsIntegerType (bool &is_signed)
954 {
955     return ClangASTContext::IsIntegerType (GetClangType(), is_signed);
956 }
957 
958 bool
959 ValueObject::IsPointerOrReferenceType ()
960 {
961     return ClangASTContext::IsPointerOrReferenceType (GetClangType());
962 }
963 
964 bool
965 ValueObject::IsPossibleCPlusPlusDynamicType ()
966 {
967     return ClangASTContext::IsPossibleCPlusPlusDynamicType (GetClangAST (), GetClangType());
968 }
969 
970 ValueObjectSP
971 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
972 {
973     ValueObjectSP synthetic_child_sp;
974     if (IsPointerType ())
975     {
976         char index_str[64];
977         snprintf(index_str, sizeof(index_str), "[%i]", index);
978         ConstString index_const_str(index_str);
979         // Check if we have already created a synthetic array member in this
980         // valid object. If we have we will re-use it.
981         synthetic_child_sp = GetSyntheticChild (index_const_str);
982         if (!synthetic_child_sp)
983         {
984             ValueObject *synthetic_child;
985             // We haven't made a synthetic array member for INDEX yet, so
986             // lets make one and cache it for any future reference.
987             synthetic_child = CreateChildAtIndex(0, true, index);
988 
989             // Cache the value if we got one back...
990             if (synthetic_child)
991             {
992                 AddSyntheticChild(index_const_str, synthetic_child);
993                 synthetic_child_sp = synthetic_child->GetSP();
994             }
995         }
996     }
997     return synthetic_child_sp;
998 }
999 
1000 void
1001 ValueObject::CalculateDynamicValue (lldb::DynamicValueType use_dynamic)
1002 {
1003     if (use_dynamic == lldb::eNoDynamicValues)
1004         return;
1005 
1006     if (!m_dynamic_value && !IsDynamic())
1007     {
1008         Process *process = m_update_point.GetProcess();
1009         bool worth_having_dynamic_value = false;
1010 
1011 
1012         // FIXME: Process should have some kind of "map over Runtimes" so we don't have to
1013         // hard code this everywhere.
1014         lldb::LanguageType known_type = GetObjectRuntimeLanguage();
1015         if (known_type != lldb::eLanguageTypeUnknown && known_type != lldb::eLanguageTypeC)
1016         {
1017             LanguageRuntime *runtime = process->GetLanguageRuntime (known_type);
1018             if (runtime)
1019                 worth_having_dynamic_value = runtime->CouldHaveDynamicValue(*this);
1020         }
1021         else
1022         {
1023             LanguageRuntime *cpp_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeC_plus_plus);
1024             if (cpp_runtime)
1025                 worth_having_dynamic_value = cpp_runtime->CouldHaveDynamicValue(*this);
1026 
1027             if (!worth_having_dynamic_value)
1028             {
1029                 LanguageRuntime *objc_runtime = process->GetLanguageRuntime (lldb::eLanguageTypeObjC);
1030                 if (objc_runtime)
1031                     worth_having_dynamic_value = objc_runtime->CouldHaveDynamicValue(*this);
1032             }
1033         }
1034 
1035         if (worth_having_dynamic_value)
1036             m_dynamic_value = new ValueObjectDynamicValue (*this, use_dynamic);
1037 
1038 //        if (worth_having_dynamic_value)
1039 //            printf ("Adding dynamic value %s (%p) to (%p) - manager %p.\n", m_name.GetCString(), m_dynamic_value, this, m_manager);
1040 
1041     }
1042 }
1043 
1044 ValueObjectSP
1045 ValueObject::GetDynamicValue (DynamicValueType use_dynamic)
1046 {
1047     if (use_dynamic == lldb::eNoDynamicValues)
1048         return ValueObjectSP();
1049 
1050     if (!IsDynamic() && m_dynamic_value == NULL)
1051     {
1052         CalculateDynamicValue(use_dynamic);
1053     }
1054     if (m_dynamic_value)
1055         return m_dynamic_value->GetSP();
1056     else
1057         return ValueObjectSP();
1058 }
1059 
1060 bool
1061 ValueObject::GetBaseClassPath (Stream &s)
1062 {
1063     if (IsBaseClass())
1064     {
1065         bool parent_had_base_class = GetParent() && GetParent()->GetBaseClassPath (s);
1066         clang_type_t clang_type = GetClangType();
1067         std::string cxx_class_name;
1068         bool this_had_base_class = ClangASTContext::GetCXXClassName (clang_type, cxx_class_name);
1069         if (this_had_base_class)
1070         {
1071             if (parent_had_base_class)
1072                 s.PutCString("::");
1073             s.PutCString(cxx_class_name.c_str());
1074         }
1075         return parent_had_base_class || this_had_base_class;
1076     }
1077     return false;
1078 }
1079 
1080 
1081 ValueObject *
1082 ValueObject::GetNonBaseClassParent()
1083 {
1084     if (GetParent())
1085     {
1086         if (GetParent()->IsBaseClass())
1087             return GetParent()->GetNonBaseClassParent();
1088         else
1089             return GetParent();
1090     }
1091     return NULL;
1092 }
1093 
1094 void
1095 ValueObject::GetExpressionPath (Stream &s, bool qualify_cxx_base_classes)
1096 {
1097     const bool is_deref_of_parent = IsDereferenceOfParent ();
1098 
1099     if (is_deref_of_parent)
1100         s.PutCString("*(");
1101 
1102     if (GetParent())
1103         GetParent()->GetExpressionPath (s, qualify_cxx_base_classes);
1104 
1105     if (!IsBaseClass())
1106     {
1107         if (!is_deref_of_parent)
1108         {
1109             ValueObject *non_base_class_parent = GetNonBaseClassParent();
1110             if (non_base_class_parent)
1111             {
1112                 clang_type_t non_base_class_parent_clang_type = non_base_class_parent->GetClangType();
1113                 if (non_base_class_parent_clang_type)
1114                 {
1115                     const uint32_t non_base_class_parent_type_info = ClangASTContext::GetTypeInfo (non_base_class_parent_clang_type, NULL, NULL);
1116 
1117                     if (non_base_class_parent_type_info & ClangASTContext::eTypeIsPointer)
1118                     {
1119                         s.PutCString("->");
1120                     }
1121                     else if ((non_base_class_parent_type_info & ClangASTContext::eTypeHasChildren) &&
1122                         !(non_base_class_parent_type_info & ClangASTContext::eTypeIsArray))
1123                     {
1124                         s.PutChar('.');
1125                     }
1126                 }
1127             }
1128 
1129             const char *name = GetName().GetCString();
1130             if (name)
1131             {
1132                 if (qualify_cxx_base_classes)
1133                 {
1134                     if (GetBaseClassPath (s))
1135                         s.PutCString("::");
1136                 }
1137                 s.PutCString(name);
1138             }
1139         }
1140     }
1141 
1142     if (is_deref_of_parent)
1143         s.PutChar(')');
1144 }
1145 
1146 void
1147 ValueObject::DumpValueObject
1148 (
1149     Stream &s,
1150     ValueObject *valobj,
1151     const char *root_valobj_name,
1152     uint32_t ptr_depth,
1153     uint32_t curr_depth,
1154     uint32_t max_depth,
1155     bool show_types,
1156     bool show_location,
1157     bool use_objc,
1158     lldb::DynamicValueType use_dynamic,
1159     bool scope_already_checked,
1160     bool flat_output
1161 )
1162 {
1163     if (valobj)
1164     {
1165         bool update_success = valobj->UpdateValueIfNeeded ();
1166 
1167         if (update_success && use_dynamic != lldb::eNoDynamicValues)
1168         {
1169             ValueObject *dynamic_value = valobj->GetDynamicValue(use_dynamic).get();
1170             if (dynamic_value)
1171                 valobj = dynamic_value;
1172         }
1173 
1174         clang_type_t clang_type = valobj->GetClangType();
1175 
1176         const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
1177         const char *err_cstr = NULL;
1178         const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
1179         const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
1180 
1181         const bool print_valobj = flat_output == false || has_value;
1182 
1183         if (print_valobj)
1184         {
1185             if (show_location)
1186             {
1187                 s.Printf("%s: ", valobj->GetLocationAsCString());
1188             }
1189 
1190             s.Indent();
1191 
1192             // Always show the type for the top level items.
1193             if (show_types || (curr_depth == 0 && !flat_output))
1194                 s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
1195 
1196 
1197             if (flat_output)
1198             {
1199                 // If we are showing types, also qualify the C++ base classes
1200                 const bool qualify_cxx_base_classes = show_types;
1201                 valobj->GetExpressionPath(s, qualify_cxx_base_classes);
1202                 s.PutCString(" =");
1203             }
1204             else
1205             {
1206                 const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1207                 s.Printf ("%s =", name_cstr);
1208             }
1209 
1210             if (!scope_already_checked && !valobj->IsInScope())
1211             {
1212                 err_cstr = "out of scope";
1213             }
1214         }
1215 
1216         const char *val_cstr = NULL;
1217 
1218         if (err_cstr == NULL)
1219         {
1220             val_cstr = valobj->GetValueAsCString();
1221             err_cstr = valobj->GetError().AsCString();
1222         }
1223 
1224         if (err_cstr)
1225         {
1226             s.Printf (" <%s>\n", err_cstr);
1227         }
1228         else
1229         {
1230             const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
1231             if (print_valobj)
1232             {
1233                 const char *sum_cstr = valobj->GetSummaryAsCString();
1234 
1235                 if (val_cstr)
1236                     s.Printf(" %s", val_cstr);
1237 
1238                 if (sum_cstr)
1239                     s.Printf(" %s", sum_cstr);
1240 
1241                 if (use_objc)
1242                 {
1243                     const char *object_desc = valobj->GetObjectDescription();
1244                     if (object_desc)
1245                         s.Printf(" %s\n", object_desc);
1246                     else
1247                         s.Printf (" [no Objective-C description available]\n");
1248                     return;
1249                 }
1250             }
1251 
1252             if (curr_depth < max_depth)
1253             {
1254                 // We will show children for all concrete types. We won't show
1255                 // pointer contents unless a pointer depth has been specified.
1256                 // We won't reference contents unless the reference is the
1257                 // root object (depth of zero).
1258                 bool print_children = true;
1259 
1260                 // Use a new temporary pointer depth in case we override the
1261                 // current pointer depth below...
1262                 uint32_t curr_ptr_depth = ptr_depth;
1263 
1264                 const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1265                 if (is_ptr || is_ref)
1266                 {
1267                     // We have a pointer or reference whose value is an address.
1268                     // Make sure that address is not NULL
1269                     AddressType ptr_address_type;
1270                     if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1271                         print_children = false;
1272 
1273                     else if (is_ref && curr_depth == 0)
1274                     {
1275                         // If this is the root object (depth is zero) that we are showing
1276                         // and it is a reference, and no pointer depth has been supplied
1277                         // print out what it references. Don't do this at deeper depths
1278                         // otherwise we can end up with infinite recursion...
1279                         curr_ptr_depth = 1;
1280                     }
1281 
1282                     if (curr_ptr_depth == 0)
1283                         print_children = false;
1284                 }
1285 
1286                 if (print_children)
1287                 {
1288                     const uint32_t num_children = valobj->GetNumChildren();
1289                     if (num_children)
1290                     {
1291                         if (flat_output)
1292                         {
1293                             if (print_valobj)
1294                                 s.EOL();
1295                         }
1296                         else
1297                         {
1298                             if (print_valobj)
1299                                 s.PutCString(is_ref ? ": {\n" : " {\n");
1300                             s.IndentMore();
1301                         }
1302 
1303                         for (uint32_t idx=0; idx<num_children; ++idx)
1304                         {
1305                             ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1306                             if (child_sp.get())
1307                             {
1308                                 DumpValueObject (s,
1309                                                  child_sp.get(),
1310                                                  NULL,
1311                                                  (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
1312                                                  curr_depth + 1,
1313                                                  max_depth,
1314                                                  show_types,
1315                                                  show_location,
1316                                                  false,
1317                                                  use_dynamic,
1318                                                  true,
1319                                                  flat_output);
1320                             }
1321                         }
1322 
1323                         if (!flat_output)
1324                         {
1325                             s.IndentLess();
1326                             s.Indent("}\n");
1327                         }
1328                     }
1329                     else if (has_children)
1330                     {
1331                         // Aggregate, no children...
1332                         if (print_valobj)
1333                             s.PutCString(" {}\n");
1334                     }
1335                     else
1336                     {
1337                         if (print_valobj)
1338                             s.EOL();
1339                     }
1340 
1341                 }
1342                 else
1343                 {
1344                     s.EOL();
1345                 }
1346             }
1347             else
1348             {
1349                 if (has_children && print_valobj)
1350                 {
1351                     s.PutCString("{...}\n");
1352                 }
1353             }
1354         }
1355     }
1356 }
1357 
1358 
1359 ValueObjectSP
1360 ValueObject::CreateConstantValue (const ConstString &name)
1361 {
1362     ValueObjectSP valobj_sp;
1363 
1364     if (UpdateValueIfNeeded() && m_error.Success())
1365     {
1366         ExecutionContextScope *exe_scope = GetExecutionContextScope();
1367         if (exe_scope)
1368         {
1369             ExecutionContext exe_ctx;
1370             exe_scope->CalculateExecutionContext(exe_ctx);
1371 
1372             clang::ASTContext *ast = GetClangAST ();
1373 
1374             DataExtractor data;
1375             data.SetByteOrder (m_data.GetByteOrder());
1376             data.SetAddressByteSize(m_data.GetAddressByteSize());
1377 
1378             m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1379 
1380             valobj_sp = ValueObjectConstResult::Create (exe_scope,
1381                                                         ast,
1382                                                         GetClangType(),
1383                                                         name,
1384                                                         data);
1385         }
1386     }
1387 
1388     if (!valobj_sp)
1389     {
1390         valobj_sp = ValueObjectConstResult::Create (NULL, m_error);
1391     }
1392     return valobj_sp;
1393 }
1394 
1395 lldb::ValueObjectSP
1396 ValueObject::Dereference (Error &error)
1397 {
1398     if (m_deref_valobj)
1399         return m_deref_valobj->GetSP();
1400 
1401     const bool is_pointer_type = IsPointerType();
1402     if (is_pointer_type)
1403     {
1404         bool omit_empty_base_classes = true;
1405 
1406         std::string child_name_str;
1407         uint32_t child_byte_size = 0;
1408         int32_t child_byte_offset = 0;
1409         uint32_t child_bitfield_bit_size = 0;
1410         uint32_t child_bitfield_bit_offset = 0;
1411         bool child_is_base_class = false;
1412         bool child_is_deref_of_parent = false;
1413         const bool transparent_pointers = false;
1414         clang::ASTContext *clang_ast = GetClangAST();
1415         clang_type_t clang_type = GetClangType();
1416         clang_type_t child_clang_type;
1417         child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1418                                                                       GetName().GetCString(),
1419                                                                       clang_type,
1420                                                                       0,
1421                                                                       transparent_pointers,
1422                                                                       omit_empty_base_classes,
1423                                                                       child_name_str,
1424                                                                       child_byte_size,
1425                                                                       child_byte_offset,
1426                                                                       child_bitfield_bit_size,
1427                                                                       child_bitfield_bit_offset,
1428                                                                       child_is_base_class,
1429                                                                       child_is_deref_of_parent);
1430         if (child_clang_type && child_byte_size)
1431         {
1432             ConstString child_name;
1433             if (!child_name_str.empty())
1434                 child_name.SetCString (child_name_str.c_str());
1435 
1436             m_deref_valobj = new ValueObjectChild (*this,
1437                                                    clang_ast,
1438                                                    child_clang_type,
1439                                                    child_name,
1440                                                    child_byte_size,
1441                                                    child_byte_offset,
1442                                                    child_bitfield_bit_size,
1443                                                    child_bitfield_bit_offset,
1444                                                    child_is_base_class,
1445                                                    child_is_deref_of_parent);
1446         }
1447     }
1448 
1449     if (m_deref_valobj)
1450     {
1451         error.Clear();
1452         return m_deref_valobj->GetSP();
1453     }
1454     else
1455     {
1456         StreamString strm;
1457         GetExpressionPath(strm, true);
1458 
1459         if (is_pointer_type)
1460             error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1461         else
1462             error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1463         return ValueObjectSP();
1464     }
1465 }
1466 
1467 lldb::ValueObjectSP
1468 ValueObject::AddressOf (Error &error)
1469 {
1470     if (m_addr_of_valobj_sp)
1471         return m_addr_of_valobj_sp;
1472 
1473     AddressType address_type = eAddressTypeInvalid;
1474     const bool scalar_is_load_address = false;
1475     lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
1476     error.Clear();
1477     if (addr != LLDB_INVALID_ADDRESS)
1478     {
1479         switch (address_type)
1480         {
1481         default:
1482         case eAddressTypeInvalid:
1483             {
1484                 StreamString expr_path_strm;
1485                 GetExpressionPath(expr_path_strm, true);
1486                 error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1487             }
1488             break;
1489 
1490         case eAddressTypeFile:
1491         case eAddressTypeLoad:
1492         case eAddressTypeHost:
1493             {
1494                 clang::ASTContext *ast = GetClangAST();
1495                 clang_type_t clang_type = GetClangType();
1496                 if (ast && clang_type)
1497                 {
1498                     std::string name (1, '&');
1499                     name.append (m_name.AsCString(""));
1500                     m_addr_of_valobj_sp = ValueObjectConstResult::Create (GetExecutionContextScope(),
1501                                                                           ast,
1502                                                                           ClangASTContext::CreatePointerType (ast, clang_type),
1503                                                                           ConstString (name.c_str()),
1504                                                                           addr,
1505                                                                           eAddressTypeInvalid,
1506                                                                           m_data.GetAddressByteSize());
1507                 }
1508             }
1509             break;
1510         }
1511     }
1512     return m_addr_of_valobj_sp;
1513 }
1514 
1515 
1516 lldb::ValueObjectSP
1517 ValueObject::CastPointerType (const char *name, ClangASTType &clang_ast_type)
1518 {
1519     lldb::ValueObjectSP valobj_sp;
1520     AddressType address_type;
1521     const bool scalar_is_load_address = true;
1522     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1523 
1524     if (ptr_value != LLDB_INVALID_ADDRESS)
1525     {
1526         Address ptr_addr (NULL, ptr_value);
1527 
1528         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1529                                                name,
1530                                                ptr_addr,
1531                                                clang_ast_type);
1532     }
1533     return valobj_sp;
1534 }
1535 
1536 lldb::ValueObjectSP
1537 ValueObject::CastPointerType (const char *name, TypeSP &type_sp)
1538 {
1539     lldb::ValueObjectSP valobj_sp;
1540     AddressType address_type;
1541     const bool scalar_is_load_address = true;
1542     lldb::addr_t ptr_value = GetPointerValue (address_type, scalar_is_load_address);
1543 
1544     if (ptr_value != LLDB_INVALID_ADDRESS)
1545     {
1546         Address ptr_addr (NULL, ptr_value);
1547 
1548         valobj_sp = ValueObjectMemory::Create (GetExecutionContextScope(),
1549                                                name,
1550                                                ptr_addr,
1551                                                type_sp);
1552     }
1553     return valobj_sp;
1554 }
1555 
1556 
1557 ValueObject::EvaluationPoint::EvaluationPoint () :
1558     m_thread_id (LLDB_INVALID_UID),
1559     m_stop_id (0)
1560 {
1561 }
1562 
1563 ValueObject::EvaluationPoint::EvaluationPoint (ExecutionContextScope *exe_scope, bool use_selected):
1564     m_needs_update (true),
1565     m_first_update (true),
1566     m_thread_id (LLDB_INVALID_UID),
1567     m_stop_id (0)
1568 
1569 {
1570     ExecutionContext exe_ctx;
1571     ExecutionContextScope *computed_exe_scope = exe_scope;  // If use_selected is true, we may find a better scope,
1572                                                             // and if so we want to cache that not the original.
1573     if (exe_scope)
1574         exe_scope->CalculateExecutionContext(exe_ctx);
1575     if (exe_ctx.target != NULL)
1576     {
1577         m_target_sp = exe_ctx.target->GetSP();
1578 
1579         if (exe_ctx.process == NULL)
1580             m_process_sp = exe_ctx.target->GetProcessSP();
1581         else
1582             m_process_sp = exe_ctx.process->GetSP();
1583 
1584         if (m_process_sp != NULL)
1585         {
1586             m_stop_id = m_process_sp->GetStopID();
1587             Thread *thread = NULL;
1588 
1589             if (exe_ctx.thread == NULL)
1590             {
1591                 if (use_selected)
1592                 {
1593                     thread = m_process_sp->GetThreadList().GetSelectedThread().get();
1594                     if (thread)
1595                         computed_exe_scope = thread;
1596                 }
1597             }
1598             else
1599                 thread = exe_ctx.thread;
1600 
1601             if (thread != NULL)
1602             {
1603                 m_thread_id = thread->GetIndexID();
1604                 if (exe_ctx.frame == NULL)
1605                 {
1606                     if (use_selected)
1607                     {
1608                         StackFrame *frame = exe_ctx.thread->GetSelectedFrame().get();
1609                         if (frame)
1610                         {
1611                             m_stack_id = frame->GetStackID();
1612                             computed_exe_scope = frame;
1613                         }
1614                     }
1615                 }
1616                 else
1617                     m_stack_id = exe_ctx.frame->GetStackID();
1618             }
1619         }
1620     }
1621     m_exe_scope = computed_exe_scope;
1622 }
1623 
1624 ValueObject::EvaluationPoint::EvaluationPoint (const ValueObject::EvaluationPoint &rhs) :
1625     m_exe_scope (rhs.m_exe_scope),
1626     m_needs_update(true),
1627     m_first_update(true),
1628     m_target_sp (rhs.m_target_sp),
1629     m_process_sp (rhs.m_process_sp),
1630     m_thread_id (rhs.m_thread_id),
1631     m_stack_id (rhs.m_stack_id),
1632     m_stop_id (0)
1633 {
1634 }
1635 
1636 ValueObject::EvaluationPoint::~EvaluationPoint ()
1637 {
1638 }
1639 
1640 ExecutionContextScope *
1641 ValueObject::EvaluationPoint::GetExecutionContextScope ()
1642 {
1643     // We have to update before giving out the scope, or we could be handing out stale pointers.
1644     SyncWithProcessState();
1645 
1646     return m_exe_scope;
1647 }
1648 
1649 // This function checks the EvaluationPoint against the current process state.  If the current
1650 // state matches the evaluation point, or the evaluation point is already invalid, then we return
1651 // false, meaning "no change".  If the current state is different, we update our state, and return
1652 // true meaning "yes, change".  If we did see a change, we also set m_needs_update to true, so
1653 // future calls to NeedsUpdate will return true.
1654 
1655 bool
1656 ValueObject::EvaluationPoint::SyncWithProcessState()
1657 {
1658     // If we're already invalid, we don't need to do anything, and nothing has changed:
1659     if (m_stop_id == LLDB_INVALID_UID)
1660     {
1661         // Can't update with an invalid state.
1662         m_needs_update = false;
1663         return false;
1664     }
1665 
1666     // If we don't have a process nothing can change.
1667     if (!m_process_sp)
1668         return false;
1669 
1670     // If our stop id is the current stop ID, nothing has changed:
1671     uint32_t cur_stop_id = m_process_sp->GetStopID();
1672     if (m_stop_id == cur_stop_id)
1673         return false;
1674 
1675     // If the current stop id is 0, either we haven't run yet, or the process state has been cleared.
1676     // In either case, we aren't going to be able to sync with the process state.
1677     if (cur_stop_id == 0)
1678         return false;
1679 
1680     m_stop_id = cur_stop_id;
1681     m_needs_update = true;
1682     m_exe_scope = m_process_sp.get();
1683 
1684     // Something has changed, so we will return true.  Now make sure the thread & frame still exist, and if either
1685     // doesn't, mark ourselves as invalid.
1686 
1687     if (m_thread_id != LLDB_INVALID_THREAD_ID)
1688     {
1689         Thread *our_thread = m_process_sp->GetThreadList().FindThreadByIndexID (m_thread_id).get();
1690         if (our_thread == NULL)
1691             SetInvalid();
1692         else
1693         {
1694             m_exe_scope = our_thread;
1695 
1696             if (m_stack_id.IsValid())
1697             {
1698                 StackFrame *our_frame = our_thread->GetFrameWithStackID (m_stack_id).get();
1699                 if (our_frame == NULL)
1700                     SetInvalid();
1701                 else
1702                     m_exe_scope = our_frame;
1703             }
1704         }
1705     }
1706     return true;
1707 }
1708 
1709 void
1710 ValueObject::EvaluationPoint::SetUpdated ()
1711 {
1712     m_first_update = false;
1713     m_needs_update = false;
1714     if (m_process_sp)
1715         m_stop_id = m_process_sp->GetStopID();
1716 }
1717 
1718 
1719 bool
1720 ValueObject::EvaluationPoint::SetContext (ExecutionContextScope *exe_scope)
1721 {
1722     if (!IsValid())
1723         return false;
1724 
1725     bool needs_update = false;
1726     m_exe_scope = NULL;
1727 
1728     // The target has to be non-null, and the
1729     Target *target = exe_scope->CalculateTarget();
1730     if (target != NULL)
1731     {
1732         Target *old_target = m_target_sp.get();
1733         assert (target == old_target);
1734         Process *process = exe_scope->CalculateProcess();
1735         if (process != NULL)
1736         {
1737             // FOR NOW - assume you can't update variable objects across process boundaries.
1738             Process *old_process = m_process_sp.get();
1739             assert (process == old_process);
1740 
1741             lldb::user_id_t stop_id = process->GetStopID();
1742             if (stop_id != m_stop_id)
1743             {
1744                 needs_update = true;
1745                 m_stop_id = stop_id;
1746             }
1747             // See if we're switching the thread or stack context.  If no thread is given, this is
1748             // being evaluated in a global context.
1749             Thread *thread = exe_scope->CalculateThread();
1750             if (thread != NULL)
1751             {
1752                 lldb::user_id_t new_thread_index = thread->GetIndexID();
1753                 if (new_thread_index != m_thread_id)
1754                 {
1755                     needs_update = true;
1756                     m_thread_id = new_thread_index;
1757                     m_stack_id.Clear();
1758                 }
1759 
1760                 StackFrame *new_frame = exe_scope->CalculateStackFrame();
1761                 if (new_frame != NULL)
1762                 {
1763                     if (new_frame->GetStackID() != m_stack_id)
1764                     {
1765                         needs_update = true;
1766                         m_stack_id = new_frame->GetStackID();
1767                     }
1768                 }
1769                 else
1770                 {
1771                     m_stack_id.Clear();
1772                     needs_update = true;
1773                 }
1774             }
1775             else
1776             {
1777                 // If this had been given a thread, and now there is none, we should update.
1778                 // Otherwise we don't have to do anything.
1779                 if (m_thread_id != LLDB_INVALID_UID)
1780                 {
1781                     m_thread_id = LLDB_INVALID_UID;
1782                     m_stack_id.Clear();
1783                     needs_update = true;
1784                 }
1785             }
1786         }
1787         else
1788         {
1789             // If there is no process, then we don't need to update anything.
1790             // But if we're switching from having a process to not, we should try to update.
1791             if (m_process_sp.get() != NULL)
1792             {
1793                 needs_update = true;
1794                 m_process_sp.reset();
1795                 m_thread_id = LLDB_INVALID_UID;
1796                 m_stack_id.Clear();
1797             }
1798         }
1799     }
1800     else
1801     {
1802         // If there's no target, nothing can change so we don't need to update anything.
1803         // But if we're switching from having a target to not, we should try to update.
1804         if (m_target_sp.get() != NULL)
1805         {
1806             needs_update = true;
1807             m_target_sp.reset();
1808             m_process_sp.reset();
1809             m_thread_id = LLDB_INVALID_UID;
1810             m_stack_id.Clear();
1811         }
1812     }
1813     if (!m_needs_update)
1814         m_needs_update = needs_update;
1815 
1816     return needs_update;
1817 }
1818