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