1254721Semaste//===-- Value.cpp -----------------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Core/Value.h"
11254721Semaste
12254721Semaste// C Includes
13254721Semaste// C++ Includes
14254721Semaste// Other libraries and framework includes
15254721Semaste// Project includes
16254721Semaste#include "lldb/Core/DataExtractor.h"
17254721Semaste#include "lldb/Core/DataBufferHeap.h"
18254721Semaste#include "lldb/Core/Module.h"
19254721Semaste#include "lldb/Core/State.h"
20254721Semaste#include "lldb/Core/Stream.h"
21254721Semaste#include "lldb/Symbol/ClangASTType.h"
22254721Semaste#include "lldb/Symbol/ClangASTContext.h"
23254721Semaste#include "lldb/Symbol/ObjectFile.h"
24254721Semaste#include "lldb/Symbol/SymbolContext.h"
25254721Semaste#include "lldb/Symbol/Type.h"
26254721Semaste#include "lldb/Symbol/Variable.h"
27254721Semaste#include "lldb/Target/ExecutionContext.h"
28254721Semaste#include "lldb/Target/Process.h"
29269024Semaste#include "lldb/Target/SectionLoadList.h"
30254721Semaste#include "lldb/Target/Target.h"
31254721Semaste
32254721Semasteusing namespace lldb;
33254721Semasteusing namespace lldb_private;
34254721Semaste
35254721SemasteValue::Value() :
36254721Semaste    m_value (),
37254721Semaste    m_vector (),
38254721Semaste    m_clang_type (),
39254721Semaste    m_context (NULL),
40254721Semaste    m_value_type (eValueTypeScalar),
41254721Semaste    m_context_type (eContextTypeInvalid),
42254721Semaste    m_data_buffer ()
43254721Semaste{
44254721Semaste}
45254721Semaste
46254721SemasteValue::Value(const Scalar& scalar) :
47254721Semaste    m_value (scalar),
48254721Semaste    m_vector (),
49254721Semaste    m_clang_type (),
50254721Semaste    m_context (NULL),
51254721Semaste    m_value_type (eValueTypeScalar),
52254721Semaste    m_context_type (eContextTypeInvalid),
53254721Semaste    m_data_buffer ()
54254721Semaste{
55254721Semaste}
56254721Semaste
57254721Semaste
58254721SemasteValue::Value(const uint8_t *bytes, int len) :
59254721Semaste    m_value (),
60254721Semaste    m_vector (),
61254721Semaste    m_clang_type (),
62254721Semaste    m_context (NULL),
63254721Semaste    m_value_type (eValueTypeHostAddress),
64254721Semaste    m_context_type (eContextTypeInvalid),
65254721Semaste    m_data_buffer ()
66254721Semaste{
67254721Semaste    m_data_buffer.CopyData(bytes, len);
68254721Semaste    m_value = (uintptr_t)m_data_buffer.GetBytes();
69254721Semaste}
70254721Semaste
71254721SemasteValue::Value(const Value &v) :
72254721Semaste    m_value (v.m_value),
73254721Semaste    m_vector (v.m_vector),
74254721Semaste    m_clang_type (v.m_clang_type),
75254721Semaste    m_context (v.m_context),
76254721Semaste    m_value_type (v.m_value_type),
77254721Semaste    m_context_type (v.m_context_type),
78254721Semaste    m_data_buffer ()
79254721Semaste{
80254721Semaste    if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
81254721Semaste    {
82254721Semaste        m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
83254721Semaste                               v.m_data_buffer.GetByteSize());
84254721Semaste
85254721Semaste        m_value = (uintptr_t)m_data_buffer.GetBytes();
86254721Semaste    }
87254721Semaste}
88254721Semaste
89254721SemasteValue &
90254721SemasteValue::operator=(const Value &rhs)
91254721Semaste{
92254721Semaste    if (this != &rhs)
93254721Semaste    {
94254721Semaste        m_value = rhs.m_value;
95254721Semaste        m_vector = rhs.m_vector;
96254721Semaste        m_clang_type = rhs.m_clang_type;
97254721Semaste        m_context = rhs.m_context;
98254721Semaste        m_value_type = rhs.m_value_type;
99254721Semaste        m_context_type = rhs.m_context_type;
100254721Semaste        if ((uintptr_t)rhs.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)rhs.m_data_buffer.GetBytes())
101254721Semaste        {
102254721Semaste            m_data_buffer.CopyData(rhs.m_data_buffer.GetBytes(),
103254721Semaste                                   rhs.m_data_buffer.GetByteSize());
104254721Semaste
105254721Semaste            m_value = (uintptr_t)m_data_buffer.GetBytes();
106254721Semaste        }
107254721Semaste    }
108254721Semaste    return *this;
109254721Semaste}
110254721Semaste
111254721Semastevoid
112254721SemasteValue::Dump (Stream* strm)
113254721Semaste{
114254721Semaste    m_value.GetValue (strm, true);
115254721Semaste    strm->Printf(", value_type = %s, context = %p, context_type = %s",
116254721Semaste                Value::GetValueTypeAsCString(m_value_type),
117254721Semaste                m_context,
118254721Semaste                Value::GetContextTypeAsCString(m_context_type));
119254721Semaste}
120254721Semaste
121254721SemasteValue::ValueType
122254721SemasteValue::GetValueType() const
123254721Semaste{
124254721Semaste    return m_value_type;
125254721Semaste}
126254721Semaste
127254721SemasteAddressType
128254721SemasteValue::GetValueAddressType () const
129254721Semaste{
130254721Semaste    switch (m_value_type)
131254721Semaste    {
132254721Semaste    default:
133254721Semaste    case eValueTypeScalar:
134254721Semaste        break;
135254721Semaste    case eValueTypeLoadAddress: return eAddressTypeLoad;
136254721Semaste    case eValueTypeFileAddress: return eAddressTypeFile;
137254721Semaste    case eValueTypeHostAddress: return eAddressTypeHost;
138254721Semaste    }
139254721Semaste    return eAddressTypeInvalid;
140254721Semaste}
141254721Semaste
142254721SemasteRegisterInfo *
143254721SemasteValue::GetRegisterInfo() const
144254721Semaste{
145254721Semaste    if (m_context_type == eContextTypeRegisterInfo)
146254721Semaste        return static_cast<RegisterInfo *> (m_context);
147254721Semaste    return NULL;
148254721Semaste}
149254721Semaste
150254721SemasteType *
151254721SemasteValue::GetType()
152254721Semaste{
153254721Semaste    if (m_context_type == eContextTypeLLDBType)
154254721Semaste        return static_cast<Type *> (m_context);
155254721Semaste    return NULL;
156254721Semaste}
157254721Semaste
158254721Semastevoid
159254721SemasteValue::ResizeData(size_t len)
160254721Semaste{
161254721Semaste    m_value_type = eValueTypeHostAddress;
162254721Semaste    m_data_buffer.SetByteSize(len);
163254721Semaste    m_value = (uintptr_t)m_data_buffer.GetBytes();
164254721Semaste}
165254721Semaste
166254721Semastebool
167254721SemasteValue::ValueOf(ExecutionContext *exe_ctx)
168254721Semaste{
169254721Semaste    switch (m_context_type)
170254721Semaste    {
171254721Semaste    case eContextTypeInvalid:
172254721Semaste    case eContextTypeRegisterInfo:      // RegisterInfo *
173254721Semaste    case eContextTypeLLDBType:          // Type *
174254721Semaste        break;
175254721Semaste
176254721Semaste    case eContextTypeVariable:          // Variable *
177254721Semaste        ResolveValue(exe_ctx);
178254721Semaste        return true;
179254721Semaste    }
180254721Semaste    return false;
181254721Semaste}
182254721Semaste
183254721Semasteuint64_t
184254721SemasteValue::GetValueByteSize (Error *error_ptr)
185254721Semaste{
186254721Semaste    uint64_t byte_size = 0;
187254721Semaste
188254721Semaste    switch (m_context_type)
189254721Semaste    {
190254721Semaste    case eContextTypeRegisterInfo:     // RegisterInfo *
191254721Semaste        if (GetRegisterInfo())
192254721Semaste            byte_size = GetRegisterInfo()->byte_size;
193254721Semaste        break;
194254721Semaste
195254721Semaste    case eContextTypeInvalid:
196254721Semaste    case eContextTypeLLDBType:         // Type *
197254721Semaste    case eContextTypeVariable:         // Variable *
198254721Semaste        {
199254721Semaste            const ClangASTType &ast_type = GetClangType();
200254721Semaste            if (ast_type.IsValid())
201254721Semaste                byte_size = ast_type.GetByteSize();
202254721Semaste        }
203254721Semaste        break;
204254721Semaste    }
205254721Semaste
206254721Semaste    if (error_ptr)
207254721Semaste    {
208254721Semaste        if (byte_size == 0)
209254721Semaste        {
210254721Semaste            if (error_ptr->Success())
211254721Semaste                error_ptr->SetErrorString("Unable to determine byte size.");
212254721Semaste        }
213254721Semaste        else
214254721Semaste        {
215254721Semaste            error_ptr->Clear();
216254721Semaste        }
217254721Semaste    }
218254721Semaste    return byte_size;
219254721Semaste}
220254721Semaste
221254721Semasteconst ClangASTType &
222254721SemasteValue::GetClangType ()
223254721Semaste{
224254721Semaste    if (!m_clang_type.IsValid())
225254721Semaste    {
226254721Semaste        switch (m_context_type)
227254721Semaste        {
228254721Semaste        case eContextTypeInvalid:
229254721Semaste            break;
230254721Semaste
231254721Semaste        case eContextTypeRegisterInfo:
232254721Semaste            break;    // TODO: Eventually convert into a clang type?
233254721Semaste
234254721Semaste        case eContextTypeLLDBType:
235254721Semaste            {
236254721Semaste                Type *lldb_type = GetType();
237254721Semaste                if (lldb_type)
238254721Semaste                    m_clang_type = lldb_type->GetClangForwardType();
239254721Semaste            }
240254721Semaste            break;
241254721Semaste
242254721Semaste        case eContextTypeVariable:
243254721Semaste            {
244254721Semaste                Variable *variable = GetVariable();
245254721Semaste                if (variable)
246254721Semaste                {
247254721Semaste                    Type *variable_type = variable->GetType();
248254721Semaste                    if (variable_type)
249254721Semaste                        m_clang_type = variable_type->GetClangForwardType();
250254721Semaste                }
251254721Semaste            }
252254721Semaste            break;
253254721Semaste        }
254254721Semaste    }
255254721Semaste
256254721Semaste    return m_clang_type;
257254721Semaste}
258254721Semaste
259254721Semastevoid
260254721SemasteValue::SetClangType (const ClangASTType &clang_type)
261254721Semaste{
262254721Semaste    m_clang_type = clang_type;
263254721Semaste}
264254721Semaste
265254721Semastelldb::Format
266254721SemasteValue::GetValueDefaultFormat ()
267254721Semaste{
268254721Semaste    switch (m_context_type)
269254721Semaste    {
270254721Semaste    case eContextTypeRegisterInfo:
271254721Semaste        if (GetRegisterInfo())
272254721Semaste            return GetRegisterInfo()->format;
273254721Semaste        break;
274254721Semaste
275254721Semaste    case eContextTypeInvalid:
276254721Semaste    case eContextTypeLLDBType:
277254721Semaste    case eContextTypeVariable:
278254721Semaste        {
279254721Semaste            const ClangASTType &ast_type = GetClangType();
280254721Semaste            if (ast_type.IsValid())
281254721Semaste                return ast_type.GetFormat();
282254721Semaste        }
283254721Semaste        break;
284254721Semaste
285254721Semaste    }
286254721Semaste
287254721Semaste    // Return a good default in case we can't figure anything out
288254721Semaste    return eFormatHex;
289254721Semaste}
290254721Semaste
291254721Semastebool
292254721SemasteValue::GetData (DataExtractor &data)
293254721Semaste{
294254721Semaste    switch (m_value_type)
295254721Semaste    {
296254721Semaste    default:
297254721Semaste        break;
298254721Semaste
299254721Semaste    case eValueTypeScalar:
300254721Semaste        if (m_value.GetData (data))
301254721Semaste            return true;
302254721Semaste        break;
303254721Semaste
304254721Semaste    case eValueTypeLoadAddress:
305254721Semaste    case eValueTypeFileAddress:
306254721Semaste    case eValueTypeHostAddress:
307254721Semaste        if (m_data_buffer.GetByteSize())
308254721Semaste        {
309254721Semaste            data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
310254721Semaste            return true;
311254721Semaste        }
312254721Semaste        break;
313254721Semaste    }
314254721Semaste
315254721Semaste    return false;
316254721Semaste
317254721Semaste}
318254721Semaste
319254721SemasteError
320254721SemasteValue::GetValueAsData (ExecutionContext *exe_ctx,
321254721Semaste                       DataExtractor &data,
322254721Semaste                       uint32_t data_offset,
323254721Semaste                       Module *module)
324254721Semaste{
325254721Semaste    data.Clear();
326254721Semaste
327254721Semaste    Error error;
328254721Semaste    lldb::addr_t address = LLDB_INVALID_ADDRESS;
329254721Semaste    AddressType address_type = eAddressTypeFile;
330254721Semaste    Address file_so_addr;
331254721Semaste    const ClangASTType &ast_type = GetClangType();
332254721Semaste    switch (m_value_type)
333254721Semaste    {
334254721Semaste    case eValueTypeVector:
335254721Semaste        if (ast_type.IsValid())
336254721Semaste            data.SetAddressByteSize (ast_type.GetPointerByteSize());
337254721Semaste        else
338254721Semaste            data.SetAddressByteSize(sizeof(void *));
339254721Semaste        data.SetData(m_vector.bytes, m_vector.length, m_vector.byte_order);
340254721Semaste        break;
341254721Semaste
342254721Semaste    case eValueTypeScalar:
343263363Semaste        {
344263363Semaste            data.SetByteOrder (lldb::endian::InlHostByteOrder());
345263363Semaste            if (ast_type.IsValid())
346263363Semaste                data.SetAddressByteSize (ast_type.GetPointerByteSize());
347263363Semaste            else
348263363Semaste                data.SetAddressByteSize(sizeof(void *));
349254721Semaste
350263363Semaste            uint32_t limit_byte_size = UINT32_MAX;
351263363Semaste
352263363Semaste            if (ast_type.IsValid() && ast_type.IsScalarType())
353263363Semaste            {
354263363Semaste                uint64_t type_encoding_count = 0;
355263363Semaste                lldb::Encoding type_encoding = ast_type.GetEncoding(type_encoding_count);
356263363Semaste
357263363Semaste                if (type_encoding == eEncodingUint || type_encoding == eEncodingSint)
358263363Semaste                    limit_byte_size = ast_type.GetByteSize();
359263363Semaste            }
360263363Semaste
361263363Semaste            if (m_value.GetData (data, limit_byte_size))
362263363Semaste                return error;   // Success;
363263363Semaste
364263363Semaste            error.SetErrorStringWithFormat("extracting data from value failed");
365263363Semaste            break;
366263363Semaste        }
367254721Semaste    case eValueTypeLoadAddress:
368254721Semaste        if (exe_ctx == NULL)
369254721Semaste        {
370254721Semaste            error.SetErrorString ("can't read load address (no execution context)");
371254721Semaste        }
372254721Semaste        else
373254721Semaste        {
374254721Semaste            Process *process = exe_ctx->GetProcessPtr();
375254721Semaste            if (process == NULL || !process->IsAlive())
376254721Semaste            {
377254721Semaste                Target *target = exe_ctx->GetTargetPtr();
378254721Semaste                if (target)
379254721Semaste                {
380254721Semaste                    // Allow expressions to run and evaluate things when the target
381254721Semaste                    // has memory sections loaded. This allows you to use "target modules load"
382254721Semaste                    // to load your executable and any shared libraries, then execute
383254721Semaste                    // commands where you can look at types in data sections.
384254721Semaste                    const SectionLoadList &target_sections = target->GetSectionLoadList();
385254721Semaste                    if (!target_sections.IsEmpty())
386254721Semaste                    {
387254721Semaste                        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
388254721Semaste                        if (target_sections.ResolveLoadAddress(address, file_so_addr))
389254721Semaste                        {
390254721Semaste                            address_type = eAddressTypeLoad;
391254721Semaste                            data.SetByteOrder(target->GetArchitecture().GetByteOrder());
392254721Semaste                            data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
393254721Semaste                        }
394254721Semaste                        else
395254721Semaste                            address = LLDB_INVALID_ADDRESS;
396254721Semaste                    }
397254721Semaste//                    else
398254721Semaste//                    {
399254721Semaste//                        ModuleSP exe_module_sp (target->GetExecutableModule());
400254721Semaste//                        if (exe_module_sp)
401254721Semaste//                        {
402254721Semaste//                            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
403254721Semaste//                            if (address != LLDB_INVALID_ADDRESS)
404254721Semaste//                            {
405254721Semaste//                                if (exe_module_sp->ResolveFileAddress(address, file_so_addr))
406254721Semaste//                                {
407254721Semaste//                                    data.SetByteOrder(target->GetArchitecture().GetByteOrder());
408254721Semaste//                                    data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
409254721Semaste//                                    address_type = eAddressTypeFile;
410254721Semaste//                                }
411254721Semaste//                                else
412254721Semaste//                                {
413254721Semaste//                                    address = LLDB_INVALID_ADDRESS;
414254721Semaste//                                }
415254721Semaste//                            }
416254721Semaste//                        }
417254721Semaste//                    }
418254721Semaste                }
419254721Semaste                else
420254721Semaste                {
421254721Semaste                    error.SetErrorString ("can't read load address (invalid process)");
422254721Semaste                }
423254721Semaste            }
424254721Semaste            else
425254721Semaste            {
426254721Semaste                address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
427254721Semaste                address_type = eAddressTypeLoad;
428254721Semaste                data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
429254721Semaste                data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
430254721Semaste            }
431254721Semaste        }
432254721Semaste        break;
433254721Semaste
434254721Semaste    case eValueTypeFileAddress:
435254721Semaste        if (exe_ctx == NULL)
436254721Semaste        {
437254721Semaste            error.SetErrorString ("can't read file address (no execution context)");
438254721Semaste        }
439254721Semaste        else if (exe_ctx->GetTargetPtr() == NULL)
440254721Semaste        {
441254721Semaste            error.SetErrorString ("can't read file address (invalid target)");
442254721Semaste        }
443254721Semaste        else
444254721Semaste        {
445254721Semaste            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
446254721Semaste            if (address == LLDB_INVALID_ADDRESS)
447254721Semaste            {
448254721Semaste                error.SetErrorString ("invalid file address");
449254721Semaste            }
450254721Semaste            else
451254721Semaste            {
452254721Semaste                if (module == NULL)
453254721Semaste                {
454254721Semaste                    // The only thing we can currently lock down to a module so that
455254721Semaste                    // we can resolve a file address, is a variable.
456254721Semaste                    Variable *variable = GetVariable();
457254721Semaste                    if (variable)
458254721Semaste                    {
459254721Semaste                        SymbolContext var_sc;
460254721Semaste                        variable->CalculateSymbolContext(&var_sc);
461254721Semaste                        module = var_sc.module_sp.get();
462254721Semaste                    }
463254721Semaste                }
464254721Semaste
465254721Semaste                if (module)
466254721Semaste                {
467254721Semaste                    bool resolved = false;
468254721Semaste                    ObjectFile *objfile = module->GetObjectFile();
469254721Semaste                    if (objfile)
470254721Semaste                    {
471254721Semaste                        Address so_addr(address, objfile->GetSectionList());
472254721Semaste                        addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
473254721Semaste                        bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
474254721Semaste                            ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
475254721Semaste                            : false;
476254721Semaste                        // Don't use the load address if the process has exited.
477254721Semaste                        if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
478254721Semaste                        {
479254721Semaste                            resolved = true;
480254721Semaste                            address = load_address;
481254721Semaste                            address_type = eAddressTypeLoad;
482254721Semaste                            data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
483254721Semaste                            data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
484254721Semaste                        }
485254721Semaste                        else
486254721Semaste                        {
487254721Semaste                            if (so_addr.IsSectionOffset())
488254721Semaste                            {
489254721Semaste                                resolved = true;
490254721Semaste                                file_so_addr = so_addr;
491254721Semaste                                data.SetByteOrder(objfile->GetByteOrder());
492254721Semaste                                data.SetAddressByteSize(objfile->GetAddressByteSize());
493254721Semaste                            }
494254721Semaste                        }
495254721Semaste                    }
496254721Semaste                    if (!resolved)
497254721Semaste                    {
498254721Semaste                        Variable *variable = GetVariable();
499254721Semaste
500254721Semaste                        if (module)
501254721Semaste                        {
502254721Semaste                            if (variable)
503254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s",
504254721Semaste                                                                address,
505254721Semaste                                                                variable->GetName().AsCString(""),
506254721Semaste                                                                module->GetFileSpec().GetPath().c_str());
507254721Semaste                            else
508254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s",
509254721Semaste                                                                address,
510254721Semaste                                                                module->GetFileSpec().GetPath().c_str());
511254721Semaste                        }
512254721Semaste                        else
513254721Semaste                        {
514254721Semaste                            if (variable)
515254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'",
516254721Semaste                                                                address,
517254721Semaste                                                                variable->GetName().AsCString(""));
518254721Semaste                            else
519254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address);
520254721Semaste                        }
521254721Semaste                    }
522254721Semaste                }
523254721Semaste                else
524254721Semaste                {
525254721Semaste                    // Can't convert a file address to anything valid without more
526254721Semaste                    // context (which Module it came from)
527254721Semaste                    error.SetErrorString ("can't read memory from file address without more context");
528254721Semaste                }
529254721Semaste            }
530254721Semaste        }
531254721Semaste        break;
532254721Semaste
533254721Semaste    case eValueTypeHostAddress:
534254721Semaste        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
535254721Semaste        address_type = eAddressTypeHost;
536254721Semaste        if (exe_ctx)
537254721Semaste        {
538254721Semaste            Target *target = exe_ctx->GetTargetPtr();
539254721Semaste            if (target)
540254721Semaste            {
541254721Semaste                data.SetByteOrder(target->GetArchitecture().GetByteOrder());
542254721Semaste                data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
543254721Semaste                break;
544254721Semaste            }
545254721Semaste        }
546254721Semaste        // fallback to host settings
547254721Semaste        data.SetByteOrder(lldb::endian::InlHostByteOrder());
548254721Semaste        data.SetAddressByteSize(sizeof(void *));
549254721Semaste        break;
550254721Semaste    }
551254721Semaste
552254721Semaste    // Bail if we encountered any errors
553254721Semaste    if (error.Fail())
554254721Semaste        return error;
555254721Semaste
556254721Semaste    if (address == LLDB_INVALID_ADDRESS)
557254721Semaste    {
558254721Semaste        error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
559254721Semaste        return error;
560254721Semaste    }
561254721Semaste
562254721Semaste    // If we got here, we need to read the value from memory
563254721Semaste    size_t byte_size = GetValueByteSize (&error);
564254721Semaste
565254721Semaste    // Bail if we encountered any errors getting the byte size
566254721Semaste    if (error.Fail())
567254721Semaste        return error;
568254721Semaste
569254721Semaste    // Make sure we have enough room within "data", and if we don't make
570254721Semaste    // something large enough that does
571254721Semaste    if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
572254721Semaste    {
573254721Semaste        DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
574254721Semaste        data.SetData(data_sp);
575254721Semaste    }
576254721Semaste
577254721Semaste    uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
578254721Semaste    if (dst != NULL)
579254721Semaste    {
580254721Semaste        if (address_type == eAddressTypeHost)
581254721Semaste        {
582254721Semaste            // The address is an address in this process, so just copy it
583254721Semaste            memcpy (dst, (uint8_t*)NULL + address, byte_size);
584254721Semaste        }
585254721Semaste        else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
586254721Semaste        {
587254721Semaste            if (file_so_addr.IsValid())
588254721Semaste            {
589254721Semaste                // We have a file address that we were able to translate into a
590254721Semaste                // section offset address so we might be able to read this from
591254721Semaste                // the object files if we don't have a live process. Lets always
592254721Semaste                // try and read from the process if we have one though since we
593254721Semaste                // want to read the actual value by setting "prefer_file_cache"
594254721Semaste                // to false.
595254721Semaste                const bool prefer_file_cache = false;
596254721Semaste                if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
597254721Semaste                {
598254721Semaste                    error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address);
599254721Semaste                }
600254721Semaste            }
601254721Semaste            else
602254721Semaste            {
603254721Semaste                // The execution context might have a NULL process, but it
604254721Semaste                // might have a valid process in the exe_ctx->target, so use
605254721Semaste                // the ExecutionContext::GetProcess accessor to ensure we
606254721Semaste                // get the process if there is one.
607254721Semaste                Process *process = exe_ctx->GetProcessPtr();
608254721Semaste
609254721Semaste                if (process)
610254721Semaste                {
611254721Semaste                    const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
612254721Semaste                    if (bytes_read != byte_size)
613254721Semaste                        error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
614254721Semaste                                                       (uint64_t)address,
615254721Semaste                                                       (uint32_t)bytes_read,
616254721Semaste                                                       (uint32_t)byte_size);
617254721Semaste                }
618254721Semaste                else
619254721Semaste                {
620254721Semaste                    error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address);
621254721Semaste                }
622254721Semaste            }
623254721Semaste        }
624254721Semaste        else
625254721Semaste        {
626254721Semaste            error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
627254721Semaste        }
628254721Semaste    }
629254721Semaste    else
630254721Semaste    {
631254721Semaste        error.SetErrorStringWithFormat ("out of memory");
632254721Semaste    }
633254721Semaste
634254721Semaste    return error;
635254721Semaste}
636254721Semaste
637254721SemasteScalar &
638254721SemasteValue::ResolveValue(ExecutionContext *exe_ctx)
639254721Semaste{
640254721Semaste    const ClangASTType &clang_type = GetClangType();
641254721Semaste    if (clang_type.IsValid())
642254721Semaste    {
643254721Semaste        switch (m_value_type)
644254721Semaste        {
645254721Semaste        case eValueTypeScalar:               // raw scalar value
646254721Semaste            break;
647254721Semaste
648254721Semaste        default:
649254721Semaste        case eValueTypeFileAddress:
650254721Semaste        case eValueTypeLoadAddress:          // load address value
651254721Semaste        case eValueTypeHostAddress:          // host address value (for memory in the process that is using liblldb)
652254721Semaste            {
653254721Semaste                DataExtractor data;
654254721Semaste                lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
655254721Semaste                Error error (GetValueAsData (exe_ctx, data, 0, NULL));
656254721Semaste                if (error.Success())
657254721Semaste                {
658254721Semaste                    Scalar scalar;
659254721Semaste                    if (clang_type.GetValueAsScalar (data, 0, data.GetByteSize(), scalar))
660254721Semaste                    {
661254721Semaste                        m_value = scalar;
662254721Semaste                        m_value_type = eValueTypeScalar;
663254721Semaste                    }
664254721Semaste                    else
665254721Semaste                    {
666254721Semaste                        if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
667254721Semaste                        {
668254721Semaste                            m_value.Clear();
669254721Semaste                            m_value_type = eValueTypeScalar;
670254721Semaste                        }
671254721Semaste                    }
672254721Semaste                }
673254721Semaste                else
674254721Semaste                {
675254721Semaste                    if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
676254721Semaste                    {
677254721Semaste                        m_value.Clear();
678254721Semaste                        m_value_type = eValueTypeScalar;
679254721Semaste                    }
680254721Semaste                }
681254721Semaste            }
682254721Semaste            break;
683254721Semaste        }
684254721Semaste    }
685254721Semaste    return m_value;
686254721Semaste}
687254721Semaste
688254721SemasteVariable *
689254721SemasteValue::GetVariable()
690254721Semaste{
691254721Semaste    if (m_context_type == eContextTypeVariable)
692254721Semaste        return static_cast<Variable *> (m_context);
693254721Semaste    return NULL;
694254721Semaste}
695254721Semaste
696254721Semastevoid
697254721SemasteValue::Clear()
698254721Semaste{
699254721Semaste    m_value.Clear();
700254721Semaste    m_vector.Clear();
701254721Semaste    m_clang_type.Clear();
702254721Semaste    m_value_type = eValueTypeScalar;
703254721Semaste    m_context = NULL;
704254721Semaste    m_context_type = eContextTypeInvalid;
705254721Semaste    m_data_buffer.Clear();
706254721Semaste}
707254721Semaste
708254721Semaste
709254721Semasteconst char *
710254721SemasteValue::GetValueTypeAsCString (ValueType value_type)
711254721Semaste{
712254721Semaste    switch (value_type)
713254721Semaste    {
714254721Semaste    case eValueTypeScalar:      return "scalar";
715254721Semaste    case eValueTypeVector:      return "vector";
716254721Semaste    case eValueTypeFileAddress: return "file address";
717254721Semaste    case eValueTypeLoadAddress: return "load address";
718254721Semaste    case eValueTypeHostAddress: return "host address";
719254721Semaste    };
720254721Semaste    return "???";
721254721Semaste}
722254721Semaste
723254721Semasteconst char *
724254721SemasteValue::GetContextTypeAsCString (ContextType context_type)
725254721Semaste{
726254721Semaste    switch (context_type)
727254721Semaste    {
728254721Semaste    case eContextTypeInvalid:       return "invalid";
729254721Semaste    case eContextTypeRegisterInfo:  return "RegisterInfo *";
730254721Semaste    case eContextTypeLLDBType:      return "Type *";
731254721Semaste    case eContextTypeVariable:      return "Variable *";
732254721Semaste    };
733254721Semaste    return "???";
734254721Semaste}
735254721Semaste
736254721SemasteValueList::ValueList (const ValueList &rhs)
737254721Semaste{
738254721Semaste    m_values = rhs.m_values;
739254721Semaste}
740254721Semaste
741254721Semasteconst ValueList &
742254721SemasteValueList::operator= (const ValueList &rhs)
743254721Semaste{
744254721Semaste    m_values = rhs.m_values;
745254721Semaste    return *this;
746254721Semaste}
747254721Semaste
748254721Semastevoid
749254721SemasteValueList::PushValue (const Value &value)
750254721Semaste{
751254721Semaste    m_values.push_back (value);
752254721Semaste}
753254721Semaste
754254721Semastesize_t
755254721SemasteValueList::GetSize()
756254721Semaste{
757254721Semaste    return m_values.size();
758254721Semaste}
759254721Semaste
760254721SemasteValue *
761254721SemasteValueList::GetValueAtIndex (size_t idx)
762254721Semaste{
763254721Semaste    if (idx < GetSize())
764254721Semaste    {
765254721Semaste        return &(m_values[idx]);
766254721Semaste    }
767254721Semaste    else
768254721Semaste        return NULL;
769254721Semaste}
770254721Semaste
771254721Semastevoid
772254721SemasteValueList::Clear ()
773254721Semaste{
774254721Semaste    m_values.clear();
775254721Semaste}
776254721Semaste
777