ValueObject.cpp revision c3b61d239a53271d013b82ffaba6ab4e92b7fcc1
1//===-- ValueObject.cpp -----------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ValueObject.h"
11
12// C Includes
13#include <stdlib.h>
14
15// C++ Includes
16// Other libraries and framework includes
17#include "llvm/Support/raw_ostream.h"
18#include "clang/AST/Type.h"
19
20// Project includes
21#include "lldb/Core/DataBufferHeap.h"
22#include "lldb/Core/StreamString.h"
23#include "lldb/Core/ValueObjectChild.h"
24#include "lldb/Core/ValueObjectConstResult.h"
25#include "lldb/Core/ValueObjectList.h"
26
27#include "lldb/Symbol/ClangASTType.h"
28#include "lldb/Symbol/ClangASTContext.h"
29#include "lldb/Symbol/Type.h"
30
31#include "lldb/Target/ExecutionContext.h"
32#include "lldb/Target/LanguageRuntime.h"
33#include "lldb/Target/Process.h"
34#include "lldb/Target/RegisterContext.h"
35#include "lldb/Target/Target.h"
36#include "lldb/Target/Thread.h"
37
38using namespace lldb;
39using namespace lldb_private;
40
41static lldb::user_id_t g_value_obj_uid = 0;
42
43//----------------------------------------------------------------------
44// ValueObject constructor
45//----------------------------------------------------------------------
46ValueObject::ValueObject (ValueObject *parent) :
47    UserID (++g_value_obj_uid), // Unique identifier for every value object
48    m_parent (parent),
49    m_update_id (0),    // Value object lists always start at 1, value objects start at zero
50    m_name (),
51    m_data (),
52    m_value (),
53    m_error (),
54    m_value_str (),
55    m_old_value_str (),
56    m_location_str (),
57    m_summary_str (),
58    m_object_desc_str (),
59    m_children (),
60    m_synthetic_children (),
61    m_dynamic_value_sp (),
62    m_format (eFormatDefault),
63    m_value_is_valid (false),
64    m_value_did_change (false),
65    m_children_count_valid (false),
66    m_old_value_valid (false),
67    m_pointers_point_to_load_addrs (false)
68{
69}
70
71//----------------------------------------------------------------------
72// Destructor
73//----------------------------------------------------------------------
74ValueObject::~ValueObject ()
75{
76}
77
78user_id_t
79ValueObject::GetUpdateID() const
80{
81    return m_update_id;
82}
83
84bool
85ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
86{
87    // If this is a constant value, then our success is predicated on whether
88    // we have an error or not
89    if (GetIsConstant())
90        return m_error.Success();
91
92    if (exe_scope)
93    {
94        Process *process = exe_scope->CalculateProcess();
95        if (process)
96        {
97            const user_id_t stop_id = process->GetStopID();
98            if (m_update_id != stop_id)
99            {
100                bool first_update = m_update_id == 0;
101                // Save the old value using swap to avoid a string copy which
102                // also will clear our m_value_str
103                if (m_value_str.empty())
104                {
105                    m_old_value_valid = false;
106                }
107                else
108                {
109                    m_old_value_valid = true;
110                    m_old_value_str.swap (m_value_str);
111                    m_value_str.clear();
112                }
113                m_location_str.clear();
114                m_summary_str.clear();
115                m_object_desc_str.clear();
116
117                const bool value_was_valid = GetValueIsValid();
118                SetValueDidChange (false);
119
120                m_error.Clear();
121
122                // Call the pure virtual function to update the value
123                UpdateValue (exe_scope);
124
125                // Update the fact that we tried to update the value for this
126                // value object whether or not we succeed
127                m_update_id = stop_id;
128                bool success = m_error.Success();
129                SetValueIsValid (success);
130
131                if (first_update)
132                    SetValueDidChange (false);
133                else if (!m_value_did_change && success == false)
134                {
135                    // The value wasn't gotten successfully, so we mark this
136                    // as changed if the value used to be valid and now isn't
137                    SetValueDidChange (value_was_valid);
138                }
139            }
140        }
141    }
142    return m_error.Success();
143}
144
145const DataExtractor &
146ValueObject::GetDataExtractor () const
147{
148    return m_data;
149}
150
151DataExtractor &
152ValueObject::GetDataExtractor ()
153{
154    return m_data;
155}
156
157const Error &
158ValueObject::GetError() const
159{
160    return m_error;
161}
162
163const ConstString &
164ValueObject::GetName() const
165{
166    return m_name;
167}
168
169const char *
170ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
171{
172    if (UpdateValueIfNeeded(exe_scope))
173    {
174        if (m_location_str.empty())
175        {
176            StreamString sstr;
177
178            switch (m_value.GetValueType())
179            {
180            default:
181                break;
182
183            case Value::eValueTypeScalar:
184                if (m_value.GetContextType() == Value::eContextTypeRegisterInfo)
185                {
186                    RegisterInfo *reg_info = m_value.GetRegisterInfo();
187                    if (reg_info)
188                    {
189                        if (reg_info->name)
190                            m_location_str = reg_info->name;
191                        else if (reg_info->alt_name)
192                            m_location_str = reg_info->alt_name;
193                        break;
194                    }
195                }
196                m_location_str = "scalar";
197                break;
198
199            case Value::eValueTypeLoadAddress:
200            case Value::eValueTypeFileAddress:
201            case Value::eValueTypeHostAddress:
202                {
203                    uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
204                    sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
205                    m_location_str.swap(sstr.GetString());
206                }
207                break;
208            }
209        }
210    }
211    return m_location_str.c_str();
212}
213
214Value &
215ValueObject::GetValue()
216{
217    return m_value;
218}
219
220const Value &
221ValueObject::GetValue() const
222{
223    return m_value;
224}
225
226bool
227ValueObject::ResolveValue (ExecutionContextScope *exe_scope, Scalar &scalar)
228{
229    ExecutionContext exe_ctx;
230    exe_scope->CalculateExecutionContext(exe_ctx);
231    scalar = m_value.ResolveValue(&exe_ctx, GetClangAST ());
232    return scalar.IsValid();
233}
234
235bool
236ValueObject::GetValueIsValid () const
237{
238    return m_value_is_valid;
239}
240
241
242void
243ValueObject::SetValueIsValid (bool b)
244{
245    m_value_is_valid = b;
246}
247
248bool
249ValueObject::GetValueDidChange (ExecutionContextScope *exe_scope)
250{
251    GetValueAsCString (exe_scope);
252    return m_value_did_change;
253}
254
255void
256ValueObject::SetValueDidChange (bool value_changed)
257{
258    m_value_did_change = value_changed;
259}
260
261ValueObjectSP
262ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
263{
264    ValueObjectSP child_sp;
265    if (idx < GetNumChildren())
266    {
267        // Check if we have already made the child value object?
268        if (can_create && m_children[idx].get() == NULL)
269        {
270            // No we haven't created the child at this index, so lets have our
271            // subclass do it and cache the result for quick future access.
272            m_children[idx] = CreateChildAtIndex (idx, false, 0);
273        }
274
275        child_sp = m_children[idx];
276    }
277    return child_sp;
278}
279
280uint32_t
281ValueObject::GetIndexOfChildWithName (const ConstString &name)
282{
283    bool omit_empty_base_classes = true;
284    return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
285                                                     GetClangType(),
286                                                     name.GetCString(),
287                                                     omit_empty_base_classes);
288}
289
290ValueObjectSP
291ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
292{
293    // when getting a child by name, it could be burried inside some base
294    // classes (which really aren't part of the expression path), so we
295    // need a vector of indexes that can get us down to the correct child
296    std::vector<uint32_t> child_indexes;
297    clang::ASTContext *clang_ast = GetClangAST();
298    void *clang_type = GetClangType();
299    bool omit_empty_base_classes = true;
300    const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
301                                                                                      clang_type,
302                                                                                      name.GetCString(),
303                                                                                      omit_empty_base_classes,
304                                                                                      child_indexes);
305    ValueObjectSP child_sp;
306    if (num_child_indexes > 0)
307    {
308        std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
309        std::vector<uint32_t>::const_iterator end = child_indexes.end ();
310
311        child_sp = GetChildAtIndex(*pos, can_create);
312        for (++pos; pos != end; ++pos)
313        {
314            if (child_sp)
315            {
316                ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
317                child_sp = new_child_sp;
318            }
319            else
320            {
321                child_sp.reset();
322            }
323
324        }
325    }
326    return child_sp;
327}
328
329
330uint32_t
331ValueObject::GetNumChildren ()
332{
333    if (!m_children_count_valid)
334    {
335        SetNumChildren (CalculateNumChildren());
336    }
337    return m_children.size();
338}
339void
340ValueObject::SetNumChildren (uint32_t num_children)
341{
342    m_children_count_valid = true;
343    m_children.resize(num_children);
344}
345
346void
347ValueObject::SetName (const char *name)
348{
349    m_name.SetCString(name);
350}
351
352void
353ValueObject::SetName (const ConstString &name)
354{
355    m_name = name;
356}
357
358ValueObjectSP
359ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
360{
361    ValueObjectSP valobj_sp;
362    bool omit_empty_base_classes = true;
363
364    std::string child_name_str;
365    uint32_t child_byte_size = 0;
366    int32_t child_byte_offset = 0;
367    uint32_t child_bitfield_bit_size = 0;
368    uint32_t child_bitfield_bit_offset = 0;
369    bool child_is_base_class = false;
370    const bool transparent_pointers = synthetic_array_member == false;
371    clang::ASTContext *clang_ast = GetClangAST();
372    clang_type_t clang_type = GetClangType();
373    clang_type_t child_clang_type;
374    child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
375                                                                  GetName().GetCString(),
376                                                                  clang_type,
377                                                                  idx,
378                                                                  transparent_pointers,
379                                                                  omit_empty_base_classes,
380                                                                  child_name_str,
381                                                                  child_byte_size,
382                                                                  child_byte_offset,
383                                                                  child_bitfield_bit_size,
384                                                                  child_bitfield_bit_offset,
385                                                                  child_is_base_class);
386    if (child_clang_type)
387    {
388        if (synthetic_index)
389            child_byte_offset += child_byte_size * synthetic_index;
390
391        ConstString child_name;
392        if (!child_name_str.empty())
393            child_name.SetCString (child_name_str.c_str());
394
395        valobj_sp.reset (new ValueObjectChild (this,
396                                               clang_ast,
397                                               child_clang_type,
398                                               child_name,
399                                               child_byte_size,
400                                               child_byte_offset,
401                                               child_bitfield_bit_size,
402                                               child_bitfield_bit_offset,
403                                               child_is_base_class));
404        if (m_pointers_point_to_load_addrs)
405            valobj_sp->SetPointersPointToLoadAddrs (m_pointers_point_to_load_addrs);
406    }
407    return valobj_sp;
408}
409
410const char *
411ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
412{
413    if (UpdateValueIfNeeded (exe_scope))
414    {
415        if (m_summary_str.empty())
416        {
417            clang_type_t clang_type = GetClangType();
418
419            // See if this is a pointer to a C string?
420            if (clang_type)
421            {
422                StreamString sstr;
423                clang_type_t elem_or_pointee_clang_type;
424                const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type,
425                                                                      GetClangAST(),
426                                                                      &elem_or_pointee_clang_type));
427
428                if (type_flags.AnySet (ClangASTContext::eTypeIsArray | ClangASTContext::eTypeIsPointer) &&
429                    ClangASTContext::IsCharType (elem_or_pointee_clang_type))
430                {
431                    Process *process = exe_scope->CalculateProcess();
432                    if (process != NULL)
433                    {
434                        lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
435                        lldb::AddressType cstr_address_type = eAddressTypeInvalid;
436
437                        size_t cstr_len = 0;
438                        if (type_flags.Test (ClangASTContext::eTypeIsArray))
439                        {
440                            // We have an array
441                            cstr_len = ClangASTContext::GetArraySize (clang_type);
442                            cstr_address = GetAddressOf (cstr_address_type, true);
443                        }
444                        else
445                        {
446                            // We have a pointer
447                            cstr_address = GetPointerValue (cstr_address_type, true);
448                        }
449                        if (cstr_address != LLDB_INVALID_ADDRESS)
450                        {
451                            DataExtractor data;
452                            size_t bytes_read = 0;
453                            std::vector<char> data_buffer;
454                            std::vector<char> cstr_buffer;
455                            size_t cstr_length;
456                            Error error;
457                            if (cstr_len > 0)
458                            {
459                                data_buffer.resize(cstr_len);
460                                // Resize the formatted buffer in case every character
461                                // uses the "\xXX" format and one extra byte for a NULL
462                                cstr_buffer.resize(data_buffer.size() * 4 + 1);
463                                data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
464                                bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), cstr_len, error);
465                                if (bytes_read > 0)
466                                {
467                                    sstr << '"';
468                                    cstr_length = data.Dump (&sstr,
469                                                             0,                 // Start offset in "data"
470                                                             eFormatChar,       // Print as characters
471                                                             1,                 // Size of item (1 byte for a char!)
472                                                             bytes_read,        // How many bytes to print?
473                                                             UINT32_MAX,        // num per line
474                                                             LLDB_INVALID_ADDRESS,// base address
475                                                             0,                 // bitfield bit size
476                                                             0);                // bitfield bit offset
477                                    sstr << '"';
478                                }
479                            }
480                            else
481                            {
482                                const size_t k_max_buf_size = 256;
483                                data_buffer.resize (k_max_buf_size + 1);
484                                // NULL terminate in case we don't get the entire C string
485                                data_buffer.back() = '\0';
486                                // Make a formatted buffer that can contain take 4
487                                // bytes per character in case each byte uses the
488                                // "\xXX" format and one extra byte for a NULL
489                                cstr_buffer.resize (k_max_buf_size * 4 + 1);
490
491                                data.SetData (&data_buffer.front(), data_buffer.size(), eByteOrderHost);
492                                size_t total_cstr_len = 0;
493                                while ((bytes_read = process->ReadMemory (cstr_address, &data_buffer.front(), k_max_buf_size, error)) > 0)
494                                {
495                                    size_t len = strlen(&data_buffer.front());
496                                    if (len == 0)
497                                        break;
498                                    if (len > bytes_read)
499                                        len = bytes_read;
500                                    if (sstr.GetSize() == 0)
501                                        sstr << '"';
502
503                                    cstr_length = data.Dump (&sstr,
504                                                             0,                 // Start offset in "data"
505                                                             eFormatChar,       // Print as characters
506                                                             1,                 // Size of item (1 byte for a char!)
507                                                             len,               // How many bytes to print?
508                                                             UINT32_MAX,        // num per line
509                                                             LLDB_INVALID_ADDRESS,// base address
510                                                             0,                 // bitfield bit size
511                                                             0);                // bitfield bit offset
512
513                                    if (len < k_max_buf_size)
514                                        break;
515                                    cstr_address += total_cstr_len;
516                                }
517                                if (sstr.GetSize() > 0)
518                                    sstr << '"';
519                            }
520                        }
521                    }
522
523                    if (sstr.GetSize() > 0)
524                        m_summary_str.assign (sstr.GetData(), sstr.GetSize());
525                }
526                else if (ClangASTContext::IsFunctionPointerType (clang_type))
527                {
528                    lldb::AddressType func_ptr_address_type = eAddressTypeInvalid;
529                    lldb::addr_t func_ptr_address = GetPointerValue (func_ptr_address_type, true);
530
531                    if (func_ptr_address != 0 && func_ptr_address != LLDB_INVALID_ADDRESS)
532                    {
533                        switch (func_ptr_address_type)
534                        {
535                        case eAddressTypeInvalid:
536                        case eAddressTypeFile:
537                            break;
538
539                        case eAddressTypeLoad:
540                            {
541                                Address so_addr;
542                                Target *target = exe_scope->CalculateTarget();
543                                if (target && target->GetSectionLoadList().IsEmpty() == false)
544                                {
545                                    if (target->GetSectionLoadList().ResolveLoadAddress(func_ptr_address, so_addr))
546                                    {
547                                        so_addr.Dump (&sstr,
548                                                      exe_scope,
549                                                      Address::DumpStyleResolvedDescription,
550                                                      Address::DumpStyleSectionNameOffset);
551                                    }
552                                }
553                            }
554                            break;
555
556                        case eAddressTypeHost:
557                            break;
558                        }
559                    }
560                    if (sstr.GetSize() > 0)
561                    {
562                        m_summary_str.assign (1, '(');
563                        m_summary_str.append (sstr.GetData(), sstr.GetSize());
564                        m_summary_str.append (1, ')');
565                    }
566                }
567            }
568        }
569    }
570    if (m_summary_str.empty())
571        return NULL;
572    return m_summary_str.c_str();
573}
574
575const char *
576ValueObject::GetObjectDescription (ExecutionContextScope *exe_scope)
577{
578    if (!m_object_desc_str.empty())
579        return m_object_desc_str.c_str();
580
581    if (!GetValueIsValid())
582        return NULL;
583
584    Process *process = exe_scope->CalculateProcess();
585    if (process == NULL)
586        return NULL;
587
588    StreamString s;
589
590    lldb::LanguageType language = GetObjectRuntimeLanguage();
591    LanguageRuntime *runtime = process->GetLanguageRuntime(language);
592
593    if (runtime && runtime->GetObjectDescription(s, *this, exe_scope))
594    {
595        m_object_desc_str.append (s.GetData());
596    }
597
598    if (m_object_desc_str.empty())
599        return NULL;
600    else
601        return m_object_desc_str.c_str();
602}
603
604const char *
605ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
606{
607    // If our byte size is zero this is an aggregate type that has children
608    if (ClangASTContext::IsAggregateType (GetClangType()) == false)
609    {
610        if (UpdateValueIfNeeded(exe_scope))
611        {
612            if (m_value_str.empty())
613            {
614                const Value::ContextType context_type = m_value.GetContextType();
615
616                switch (context_type)
617                {
618                case Value::eContextTypeClangType:
619                case Value::eContextTypeLLDBType:
620                case Value::eContextTypeVariable:
621                    {
622                        clang_type_t clang_type = GetClangType ();
623                        if (clang_type)
624                        {
625                            StreamString sstr;
626                            if (m_format == eFormatDefault)
627                                m_format = ClangASTType::GetFormat(clang_type);
628
629                            if (ClangASTType::DumpTypeValue (GetClangAST(),            // The clang AST
630                                                             clang_type,               // The clang type to display
631                                                             &sstr,
632                                                             m_format,                 // Format to display this type with
633                                                             m_data,                   // Data to extract from
634                                                             0,                        // Byte offset into "m_data"
635                                                             GetByteSize(),            // Byte size of item in "m_data"
636                                                             GetBitfieldBitSize(),     // Bitfield bit size
637                                                             GetBitfieldBitOffset()))  // Bitfield bit offset
638                                m_value_str.swap(sstr.GetString());
639                            else
640                                m_value_str.clear();
641                        }
642                    }
643                    break;
644
645                case Value::eContextTypeRegisterInfo:
646                    {
647                        const RegisterInfo *reg_info = m_value.GetRegisterInfo();
648                        if (reg_info)
649                        {
650                            StreamString reg_sstr;
651                            m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
652                            m_value_str.swap(reg_sstr.GetString());
653                        }
654                    }
655                    break;
656
657                default:
658                    break;
659                }
660            }
661
662            if (!m_value_did_change && m_old_value_valid)
663            {
664                // The value was gotten successfully, so we consider the
665                // value as changed if the value string differs
666                SetValueDidChange (m_old_value_str != m_value_str);
667            }
668        }
669    }
670    if (m_value_str.empty())
671        return NULL;
672    return m_value_str.c_str();
673}
674
675addr_t
676ValueObject::GetAddressOf (lldb::AddressType &address_type, bool scalar_is_load_address)
677{
678    switch (m_value.GetValueType())
679    {
680    case Value::eValueTypeScalar:
681        if (scalar_is_load_address)
682        {
683            address_type = eAddressTypeLoad;
684            return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
685        }
686        break;
687
688    case Value::eValueTypeLoadAddress:
689    case Value::eValueTypeFileAddress:
690    case Value::eValueTypeHostAddress:
691        {
692            address_type = m_value.GetValueAddressType ();
693            return m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
694        }
695        break;
696    }
697    address_type = eAddressTypeInvalid;
698    return LLDB_INVALID_ADDRESS;
699}
700
701addr_t
702ValueObject::GetPointerValue (lldb::AddressType &address_type, bool scalar_is_load_address)
703{
704    lldb::addr_t address = LLDB_INVALID_ADDRESS;
705    address_type = eAddressTypeInvalid;
706    switch (m_value.GetValueType())
707    {
708    case Value::eValueTypeScalar:
709        if (scalar_is_load_address)
710        {
711            address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
712            address_type = eAddressTypeLoad;
713        }
714        break;
715
716    case Value::eValueTypeLoadAddress:
717    case Value::eValueTypeFileAddress:
718    case Value::eValueTypeHostAddress:
719        {
720            uint32_t data_offset = 0;
721            address = m_data.GetPointer(&data_offset);
722            address_type = m_value.GetValueAddressType();
723            if (address_type == eAddressTypeInvalid)
724                address_type = eAddressTypeLoad;
725        }
726        break;
727    }
728
729    if (m_pointers_point_to_load_addrs)
730        address_type = eAddressTypeLoad;
731
732    return address;
733}
734
735bool
736ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
737{
738    // Make sure our value is up to date first so that our location and location
739    // type is valid.
740    if (!UpdateValueIfNeeded(exe_scope))
741        return false;
742
743    uint32_t count = 0;
744    lldb::Encoding encoding = ClangASTType::GetEncoding (GetClangType(), count);
745
746    char *end = NULL;
747    const size_t byte_size = GetByteSize();
748    switch (encoding)
749    {
750    case eEncodingInvalid:
751        return false;
752
753    case eEncodingUint:
754        if (byte_size > sizeof(unsigned long long))
755        {
756            return false;
757        }
758        else
759        {
760            unsigned long long ull_val = strtoull(value_str, &end, 0);
761            if (end && *end != '\0')
762                return false;
763            m_value = ull_val;
764            // Limit the bytes in our m_data appropriately.
765            m_value.GetScalar().GetData (m_data, byte_size);
766        }
767        break;
768
769    case eEncodingSint:
770        if (byte_size > sizeof(long long))
771        {
772            return false;
773        }
774        else
775        {
776            long long sll_val = strtoll(value_str, &end, 0);
777            if (end && *end != '\0')
778                return false;
779            m_value = sll_val;
780            // Limit the bytes in our m_data appropriately.
781            m_value.GetScalar().GetData (m_data, byte_size);
782        }
783        break;
784
785    case eEncodingIEEE754:
786        {
787            const off_t byte_offset = GetByteOffset();
788            uint8_t *dst = const_cast<uint8_t *>(m_data.PeekData(byte_offset, byte_size));
789            if (dst != NULL)
790            {
791                // We are decoding a float into host byte order below, so make
792                // sure m_data knows what it contains.
793                m_data.SetByteOrder(eByteOrderHost);
794                const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
795                                                        GetClangAST(),
796                                                        GetClangType(),
797                                                        value_str,
798                                                        dst,
799                                                        byte_size);
800
801                if (converted_byte_size == byte_size)
802                {
803                }
804            }
805        }
806        break;
807
808    case eEncodingVector:
809        return false;
810
811    default:
812        return false;
813    }
814
815    // If we have made it here the value is in m_data and we should write it
816    // out to the target
817    return Write ();
818}
819
820bool
821ValueObject::Write ()
822{
823    // Clear the update ID so the next time we try and read the value
824    // we try and read it again.
825    m_update_id = 0;
826
827    // TODO: when Value has a method to write a value back, call it from here.
828    return false;
829
830}
831
832lldb::LanguageType
833ValueObject::GetObjectRuntimeLanguage ()
834{
835    clang_type_t opaque_qual_type = GetClangType();
836    if (opaque_qual_type == NULL)
837        return lldb::eLanguageTypeC;
838
839    // If the type is a reference, then resolve it to what it refers to first:
840    clang::QualType qual_type (clang::QualType::getFromOpaquePtr(opaque_qual_type).getNonReferenceType());
841    if (qual_type->isAnyPointerType())
842    {
843        if (qual_type->isObjCObjectPointerType())
844            return lldb::eLanguageTypeObjC;
845
846        clang::QualType pointee_type (qual_type->getPointeeType());
847        if (pointee_type->getCXXRecordDeclForPointerType() != NULL)
848            return lldb::eLanguageTypeC_plus_plus;
849        if (pointee_type->isObjCObjectOrInterfaceType())
850            return lldb::eLanguageTypeObjC;
851        if (pointee_type->isObjCClassType())
852            return lldb::eLanguageTypeObjC;
853    }
854    else
855    {
856        if (ClangASTContext::IsObjCClassType (opaque_qual_type))
857            return lldb::eLanguageTypeObjC;
858        if (ClangASTContext::IsCXXClassType (opaque_qual_type))
859            return lldb::eLanguageTypeC_plus_plus;
860    }
861
862    return lldb::eLanguageTypeC;
863}
864
865void
866ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
867{
868    m_synthetic_children[key] = valobj_sp;
869}
870
871ValueObjectSP
872ValueObject::GetSyntheticChild (const ConstString &key) const
873{
874    ValueObjectSP synthetic_child_sp;
875    std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
876    if (pos != m_synthetic_children.end())
877        synthetic_child_sp = pos->second;
878    return synthetic_child_sp;
879}
880
881bool
882ValueObject::IsPointerType ()
883{
884    return ClangASTContext::IsPointerType (GetClangType());
885}
886
887
888
889bool
890ValueObject::IsPointerOrReferenceType ()
891{
892    return ClangASTContext::IsPointerOrReferenceType(GetClangType());
893}
894
895ValueObjectSP
896ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
897{
898    ValueObjectSP synthetic_child_sp;
899    if (IsPointerType ())
900    {
901        char index_str[64];
902        snprintf(index_str, sizeof(index_str), "[%i]", index);
903        ConstString index_const_str(index_str);
904        // Check if we have already created a synthetic array member in this
905        // valid object. If we have we will re-use it.
906        synthetic_child_sp = GetSyntheticChild (index_const_str);
907        if (!synthetic_child_sp)
908        {
909            // We haven't made a synthetic array member for INDEX yet, so
910            // lets make one and cache it for any future reference.
911            synthetic_child_sp = CreateChildAtIndex(0, true, index);
912
913            // Cache the value if we got one back...
914            if (synthetic_child_sp)
915                AddSyntheticChild(index_const_str, synthetic_child_sp);
916        }
917    }
918    return synthetic_child_sp;
919}
920
921bool
922ValueObject::SetDynamicValue ()
923{
924    if (!IsPointerOrReferenceType())
925        return false;
926
927    // Check that the runtime class is correct for determining the most specific class.
928    // If it is a C++ class, see if it is dynamic:
929
930    return true;
931}
932
933
934void
935ValueObject::GetExpressionPath (Stream &s)
936{
937    if (m_parent)
938    {
939        m_parent->GetExpressionPath (s);
940        clang_type_t parent_clang_type = m_parent->GetClangType();
941        if (parent_clang_type)
942        {
943            if (ClangASTContext::IsPointerType(parent_clang_type))
944            {
945                s.PutCString("->");
946            }
947            else if (ClangASTContext::IsAggregateType (parent_clang_type))
948            {
949                if (ClangASTContext::IsArrayType (parent_clang_type) == false &&
950                    m_parent->IsBaseClass() == false)
951                    s.PutChar('.');
952            }
953        }
954    }
955
956    if (IsBaseClass())
957    {
958        clang_type_t clang_type = GetClangType();
959        std::string cxx_class_name;
960        if (ClangASTContext::GetCXXClassName (clang_type, cxx_class_name))
961        {
962            s << cxx_class_name.c_str() << "::";
963        }
964    }
965    else
966    {
967        const char *name = GetName().GetCString();
968        if (name)
969            s.PutCString(name);
970    }
971}
972
973void
974ValueObject::DumpValueObject
975(
976    Stream &s,
977    ExecutionContextScope *exe_scope,
978    ValueObject *valobj,
979    const char *root_valobj_name,
980    uint32_t ptr_depth,
981    uint32_t curr_depth,
982    uint32_t max_depth,
983    bool show_types,
984    bool show_location,
985    bool use_objc,
986    bool scope_already_checked,
987    bool flat_output
988)
989{
990    if (valobj)
991    {
992        clang_type_t clang_type = valobj->GetClangType();
993
994        const Flags type_flags (ClangASTContext::GetTypeInfo (clang_type, NULL, NULL));
995        const char *err_cstr = NULL;
996        const bool has_children = type_flags.Test (ClangASTContext::eTypeHasChildren);
997        const bool has_value = type_flags.Test (ClangASTContext::eTypeHasValue);
998
999        const bool print_valobj = flat_output == false || has_value;
1000
1001        if (print_valobj)
1002        {
1003            if (show_location)
1004            {
1005                s.Printf("%s: ", valobj->GetLocationAsCString(exe_scope));
1006            }
1007
1008            s.Indent();
1009
1010            // Always show the type for the top level items.
1011            if (show_types || curr_depth == 0)
1012                s.Printf("(%s) ", valobj->GetTypeName().AsCString("<invalid type>"));
1013
1014
1015            if (flat_output)
1016            {
1017                valobj->GetExpressionPath(s);
1018                s.PutCString(" =");
1019            }
1020            else
1021            {
1022                const char *name_cstr = root_valobj_name ? root_valobj_name : valobj->GetName().AsCString("");
1023                s.Printf ("%s =", name_cstr);
1024            }
1025
1026            if (!scope_already_checked && !valobj->IsInScope(exe_scope->CalculateStackFrame()))
1027            {
1028                err_cstr = "error: out of scope";
1029            }
1030        }
1031
1032        const char *val_cstr = NULL;
1033
1034        if (err_cstr == NULL)
1035        {
1036            val_cstr = valobj->GetValueAsCString(exe_scope);
1037            err_cstr = valobj->GetError().AsCString();
1038        }
1039
1040        if (err_cstr)
1041        {
1042            s.Printf (" error: %s\n", err_cstr);
1043        }
1044        else
1045        {
1046            const bool is_ref = type_flags.Test (ClangASTContext::eTypeIsReference);
1047            if (print_valobj)
1048            {
1049                const char *sum_cstr = valobj->GetSummaryAsCString(exe_scope);
1050
1051                if (val_cstr)
1052                    s.Printf(" %s", val_cstr);
1053
1054                if (sum_cstr)
1055                    s.Printf(" %s", sum_cstr);
1056
1057                if (use_objc)
1058                {
1059                    const char *object_desc = valobj->GetObjectDescription(exe_scope);
1060                    if (object_desc)
1061                        s.Printf(" %s\n", object_desc);
1062                    else
1063                        s.Printf (" [no Objective-C description available]\n");
1064                    return;
1065                }
1066            }
1067
1068            if (curr_depth < max_depth)
1069            {
1070                // We will show children for all concrete types. We won't show
1071                // pointer contents unless a pointer depth has been specified.
1072                // We won't reference contents unless the reference is the
1073                // root object (depth of zero).
1074                bool print_children = true;
1075
1076                // Use a new temporary pointer depth in case we override the
1077                // current pointer depth below...
1078                uint32_t curr_ptr_depth = ptr_depth;
1079
1080                const bool is_ptr = type_flags.Test (ClangASTContext::eTypeIsPointer);
1081                if (is_ptr || is_ref)
1082                {
1083                    // We have a pointer or reference whose value is an address.
1084                    // Make sure that address is not NULL
1085                    lldb::AddressType ptr_address_type;
1086                    if (valobj->GetPointerValue (ptr_address_type, true) == 0)
1087                        print_children = false;
1088
1089                    else if (is_ref && curr_depth == 0)
1090                    {
1091                        // If this is the root object (depth is zero) that we are showing
1092                        // and it is a reference, and no pointer depth has been supplied
1093                        // print out what it references. Don't do this at deeper depths
1094                        // otherwise we can end up with infinite recursion...
1095                        curr_ptr_depth = 1;
1096                    }
1097
1098                    if (curr_ptr_depth == 0)
1099                        print_children = false;
1100                }
1101
1102                if (print_children)
1103                {
1104                    const uint32_t num_children = valobj->GetNumChildren();
1105                    if (num_children)
1106                    {
1107                        if (flat_output)
1108                        {
1109                            if (print_valobj)
1110                                s.EOL();
1111                        }
1112                        else
1113                        {
1114                            if (print_valobj)
1115                                s.PutCString(is_ref ? ": {\n" : " {\n");
1116                            s.IndentMore();
1117                        }
1118
1119                        for (uint32_t idx=0; idx<num_children; ++idx)
1120                        {
1121                            ValueObjectSP child_sp(valobj->GetChildAtIndex(idx, true));
1122                            if (child_sp.get())
1123                            {
1124                                DumpValueObject (s,
1125                                                 exe_scope,
1126                                                 child_sp.get(),
1127                                                 NULL,
1128                                                 (is_ptr || is_ref) ? curr_ptr_depth - 1 : curr_ptr_depth,
1129                                                 curr_depth + 1,
1130                                                 max_depth,
1131                                                 show_types,
1132                                                 show_location,
1133                                                 false,
1134                                                 true,
1135                                                 flat_output);
1136                            }
1137                        }
1138
1139                        if (!flat_output)
1140                        {
1141                            s.IndentLess();
1142                            s.Indent("}\n");
1143                        }
1144                    }
1145                    else if (has_children)
1146                    {
1147                        // Aggregate, no children...
1148                        if (print_valobj)
1149                            s.PutCString(" {}\n");
1150                    }
1151                    else
1152                    {
1153                        if (print_valobj)
1154                            s.EOL();
1155                    }
1156
1157                }
1158                else
1159                {
1160                    s.EOL();
1161                }
1162            }
1163            else
1164            {
1165                if (has_children && print_valobj)
1166                {
1167                    s.PutCString("{...}\n");
1168                }
1169            }
1170        }
1171    }
1172}
1173
1174
1175ValueObjectSP
1176ValueObject::CreateConstantValue (ExecutionContextScope *exe_scope, const ConstString &name)
1177{
1178    ValueObjectSP valobj_sp;
1179
1180    if (UpdateValueIfNeeded(exe_scope) && m_error.Success())
1181    {
1182        ExecutionContext exe_ctx;
1183        exe_scope->CalculateExecutionContext(exe_ctx);
1184
1185        clang::ASTContext *ast = GetClangAST ();
1186
1187        DataExtractor data;
1188        data.SetByteOrder (m_data.GetByteOrder());
1189        data.SetAddressByteSize(m_data.GetAddressByteSize());
1190
1191        m_error = m_value.GetValueAsData (&exe_ctx, ast, data, 0);
1192
1193        valobj_sp.reset (new ValueObjectConstResult (ast,
1194                                                     GetClangType(),
1195                                                     name,
1196                                                     data));
1197    }
1198    else
1199    {
1200        valobj_sp.reset (new ValueObjectConstResult (m_error));
1201    }
1202    return valobj_sp;
1203}
1204
1205lldb::ValueObjectSP
1206ValueObject::Dereference (ExecutionContextScope *exe_scope, Error &error)
1207{
1208    lldb::ValueObjectSP valobj_sp;
1209    const bool is_pointer_type = IsPointerType();
1210    if (is_pointer_type)
1211    {
1212        bool omit_empty_base_classes = true;
1213
1214        std::string child_name_str;
1215        uint32_t child_byte_size = 0;
1216        int32_t child_byte_offset = 0;
1217        uint32_t child_bitfield_bit_size = 0;
1218        uint32_t child_bitfield_bit_offset = 0;
1219        bool child_is_base_class = false;
1220        const bool transparent_pointers = false;
1221        clang::ASTContext *clang_ast = GetClangAST();
1222        clang_type_t clang_type = GetClangType();
1223        clang_type_t child_clang_type;
1224        child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
1225                                                                      GetName().GetCString(),
1226                                                                      clang_type,
1227                                                                      0,
1228                                                                      transparent_pointers,
1229                                                                      omit_empty_base_classes,
1230                                                                      child_name_str,
1231                                                                      child_byte_size,
1232                                                                      child_byte_offset,
1233                                                                      child_bitfield_bit_size,
1234                                                                      child_bitfield_bit_offset,
1235                                                                      child_is_base_class);
1236        if (child_clang_type)
1237        {
1238            ConstString child_name;
1239            if (!child_name_str.empty())
1240                child_name.SetCString (child_name_str.c_str());
1241
1242            valobj_sp.reset (new ValueObjectChild (this,
1243                                                   clang_ast,
1244                                                   child_clang_type,
1245                                                   child_name,
1246                                                   child_byte_size,
1247                                                   child_byte_offset,
1248                                                   child_bitfield_bit_size,
1249                                                   child_bitfield_bit_offset,
1250                                                   child_is_base_class));
1251        }
1252    }
1253
1254    if (valobj_sp)
1255    {
1256        error.Clear();
1257    }
1258    else
1259    {
1260        StreamString strm;
1261        GetExpressionPath(strm);
1262
1263        if (is_pointer_type)
1264            error.SetErrorStringWithFormat("dereference failed: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1265        else
1266            error.SetErrorStringWithFormat("not a pointer type: (%s) %s", GetTypeName().AsCString("<invalid type>"), strm.GetString().c_str());
1267    }
1268
1269    return valobj_sp;
1270}
1271
1272    lldb::ValueObjectSP
1273ValueObject::AddressOf (Error &error)
1274{
1275    lldb::ValueObjectSP valobj_sp;
1276    lldb::AddressType address_type = eAddressTypeInvalid;
1277    const bool scalar_is_load_address = false;
1278    lldb::addr_t addr = GetAddressOf (address_type, scalar_is_load_address);
1279    error.Clear();
1280    if (addr != LLDB_INVALID_ADDRESS)
1281    {
1282        switch (address_type)
1283        {
1284        default:
1285        case eAddressTypeInvalid:
1286            {
1287                StreamString expr_path_strm;
1288                GetExpressionPath(expr_path_strm);
1289                error.SetErrorStringWithFormat("'%s' is not in memory", expr_path_strm.GetString().c_str());
1290            }
1291            break;
1292
1293        case eAddressTypeFile:
1294        case eAddressTypeLoad:
1295        case eAddressTypeHost:
1296            {
1297                clang::ASTContext *ast = GetClangAST();
1298                clang_type_t clang_type = GetClangType();
1299                if (ast && clang_type)
1300                {
1301                    std::string name (1, '&');
1302                    name.append (m_name.AsCString(""));
1303                    valobj_sp.reset (new ValueObjectConstResult (ast,
1304                                                                 ClangASTContext::CreatePointerType (ast, clang_type),
1305                                                                 ConstString (name.c_str()),
1306                                                                 addr,
1307                                                                 address_type,
1308                                                                 m_data.GetAddressByteSize()));
1309                }
1310            }
1311            break;
1312        }
1313    }
1314    return valobj_sp;
1315}
1316
1317