Value.cpp revision 254721
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:
342254721Semaste        data.SetByteOrder (lldb::endian::InlHostByteOrder());
343254721Semaste        if (ast_type.IsValid())
344254721Semaste            data.SetAddressByteSize (ast_type.GetPointerByteSize());
345254721Semaste        else
346254721Semaste            data.SetAddressByteSize(sizeof(void *));
347254721Semaste        if (m_value.GetData (data))
348254721Semaste            return error;   // Success;
349254721Semaste        error.SetErrorStringWithFormat("extracting data from value failed");
350254721Semaste        break;
351254721Semaste
352254721Semaste    case eValueTypeLoadAddress:
353254721Semaste        if (exe_ctx == NULL)
354254721Semaste        {
355254721Semaste            error.SetErrorString ("can't read load address (no execution context)");
356254721Semaste        }
357254721Semaste        else
358254721Semaste        {
359254721Semaste            Process *process = exe_ctx->GetProcessPtr();
360254721Semaste            if (process == NULL || !process->IsAlive())
361254721Semaste            {
362254721Semaste                Target *target = exe_ctx->GetTargetPtr();
363254721Semaste                if (target)
364254721Semaste                {
365254721Semaste                    // Allow expressions to run and evaluate things when the target
366254721Semaste                    // has memory sections loaded. This allows you to use "target modules load"
367254721Semaste                    // to load your executable and any shared libraries, then execute
368254721Semaste                    // commands where you can look at types in data sections.
369254721Semaste                    const SectionLoadList &target_sections = target->GetSectionLoadList();
370254721Semaste                    if (!target_sections.IsEmpty())
371254721Semaste                    {
372254721Semaste                        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
373254721Semaste                        if (target_sections.ResolveLoadAddress(address, file_so_addr))
374254721Semaste                        {
375254721Semaste                            address_type = eAddressTypeLoad;
376254721Semaste                            data.SetByteOrder(target->GetArchitecture().GetByteOrder());
377254721Semaste                            data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
378254721Semaste                        }
379254721Semaste                        else
380254721Semaste                            address = LLDB_INVALID_ADDRESS;
381254721Semaste                    }
382254721Semaste//                    else
383254721Semaste//                    {
384254721Semaste//                        ModuleSP exe_module_sp (target->GetExecutableModule());
385254721Semaste//                        if (exe_module_sp)
386254721Semaste//                        {
387254721Semaste//                            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
388254721Semaste//                            if (address != LLDB_INVALID_ADDRESS)
389254721Semaste//                            {
390254721Semaste//                                if (exe_module_sp->ResolveFileAddress(address, file_so_addr))
391254721Semaste//                                {
392254721Semaste//                                    data.SetByteOrder(target->GetArchitecture().GetByteOrder());
393254721Semaste//                                    data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
394254721Semaste//                                    address_type = eAddressTypeFile;
395254721Semaste//                                }
396254721Semaste//                                else
397254721Semaste//                                {
398254721Semaste//                                    address = LLDB_INVALID_ADDRESS;
399254721Semaste//                                }
400254721Semaste//                            }
401254721Semaste//                        }
402254721Semaste//                    }
403254721Semaste                }
404254721Semaste                else
405254721Semaste                {
406254721Semaste                    error.SetErrorString ("can't read load address (invalid process)");
407254721Semaste                }
408254721Semaste            }
409254721Semaste            else
410254721Semaste            {
411254721Semaste                address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
412254721Semaste                address_type = eAddressTypeLoad;
413254721Semaste                data.SetByteOrder(process->GetTarget().GetArchitecture().GetByteOrder());
414254721Semaste                data.SetAddressByteSize(process->GetTarget().GetArchitecture().GetAddressByteSize());
415254721Semaste            }
416254721Semaste        }
417254721Semaste        break;
418254721Semaste
419254721Semaste    case eValueTypeFileAddress:
420254721Semaste        if (exe_ctx == NULL)
421254721Semaste        {
422254721Semaste            error.SetErrorString ("can't read file address (no execution context)");
423254721Semaste        }
424254721Semaste        else if (exe_ctx->GetTargetPtr() == NULL)
425254721Semaste        {
426254721Semaste            error.SetErrorString ("can't read file address (invalid target)");
427254721Semaste        }
428254721Semaste        else
429254721Semaste        {
430254721Semaste            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
431254721Semaste            if (address == LLDB_INVALID_ADDRESS)
432254721Semaste            {
433254721Semaste                error.SetErrorString ("invalid file address");
434254721Semaste            }
435254721Semaste            else
436254721Semaste            {
437254721Semaste                if (module == NULL)
438254721Semaste                {
439254721Semaste                    // The only thing we can currently lock down to a module so that
440254721Semaste                    // we can resolve a file address, is a variable.
441254721Semaste                    Variable *variable = GetVariable();
442254721Semaste                    if (variable)
443254721Semaste                    {
444254721Semaste                        SymbolContext var_sc;
445254721Semaste                        variable->CalculateSymbolContext(&var_sc);
446254721Semaste                        module = var_sc.module_sp.get();
447254721Semaste                    }
448254721Semaste                }
449254721Semaste
450254721Semaste                if (module)
451254721Semaste                {
452254721Semaste                    bool resolved = false;
453254721Semaste                    ObjectFile *objfile = module->GetObjectFile();
454254721Semaste                    if (objfile)
455254721Semaste                    {
456254721Semaste                        Address so_addr(address, objfile->GetSectionList());
457254721Semaste                        addr_t load_address = so_addr.GetLoadAddress (exe_ctx->GetTargetPtr());
458254721Semaste                        bool process_launched_and_stopped = exe_ctx->GetProcessPtr()
459254721Semaste                            ? StateIsStoppedState(exe_ctx->GetProcessPtr()->GetState(), true /* must_exist */)
460254721Semaste                            : false;
461254721Semaste                        // Don't use the load address if the process has exited.
462254721Semaste                        if (load_address != LLDB_INVALID_ADDRESS && process_launched_and_stopped)
463254721Semaste                        {
464254721Semaste                            resolved = true;
465254721Semaste                            address = load_address;
466254721Semaste                            address_type = eAddressTypeLoad;
467254721Semaste                            data.SetByteOrder(exe_ctx->GetTargetRef().GetArchitecture().GetByteOrder());
468254721Semaste                            data.SetAddressByteSize(exe_ctx->GetTargetRef().GetArchitecture().GetAddressByteSize());
469254721Semaste                        }
470254721Semaste                        else
471254721Semaste                        {
472254721Semaste                            if (so_addr.IsSectionOffset())
473254721Semaste                            {
474254721Semaste                                resolved = true;
475254721Semaste                                file_so_addr = so_addr;
476254721Semaste                                data.SetByteOrder(objfile->GetByteOrder());
477254721Semaste                                data.SetAddressByteSize(objfile->GetAddressByteSize());
478254721Semaste                            }
479254721Semaste                        }
480254721Semaste                    }
481254721Semaste                    if (!resolved)
482254721Semaste                    {
483254721Semaste                        Variable *variable = GetVariable();
484254721Semaste
485254721Semaste                        if (module)
486254721Semaste                        {
487254721Semaste                            if (variable)
488254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s' in %s",
489254721Semaste                                                                address,
490254721Semaste                                                                variable->GetName().AsCString(""),
491254721Semaste                                                                module->GetFileSpec().GetPath().c_str());
492254721Semaste                            else
493254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " in %s",
494254721Semaste                                                                address,
495254721Semaste                                                                module->GetFileSpec().GetPath().c_str());
496254721Semaste                        }
497254721Semaste                        else
498254721Semaste                        {
499254721Semaste                            if (variable)
500254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64 " for variable '%s'",
501254721Semaste                                                                address,
502254721Semaste                                                                variable->GetName().AsCString(""));
503254721Semaste                            else
504254721Semaste                                error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%" PRIx64, address);
505254721Semaste                        }
506254721Semaste                    }
507254721Semaste                }
508254721Semaste                else
509254721Semaste                {
510254721Semaste                    // Can't convert a file address to anything valid without more
511254721Semaste                    // context (which Module it came from)
512254721Semaste                    error.SetErrorString ("can't read memory from file address without more context");
513254721Semaste                }
514254721Semaste            }
515254721Semaste        }
516254721Semaste        break;
517254721Semaste
518254721Semaste    case eValueTypeHostAddress:
519254721Semaste        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
520254721Semaste        address_type = eAddressTypeHost;
521254721Semaste        if (exe_ctx)
522254721Semaste        {
523254721Semaste            Target *target = exe_ctx->GetTargetPtr();
524254721Semaste            if (target)
525254721Semaste            {
526254721Semaste                data.SetByteOrder(target->GetArchitecture().GetByteOrder());
527254721Semaste                data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
528254721Semaste                break;
529254721Semaste            }
530254721Semaste        }
531254721Semaste        // fallback to host settings
532254721Semaste        data.SetByteOrder(lldb::endian::InlHostByteOrder());
533254721Semaste        data.SetAddressByteSize(sizeof(void *));
534254721Semaste        break;
535254721Semaste    }
536254721Semaste
537254721Semaste    // Bail if we encountered any errors
538254721Semaste    if (error.Fail())
539254721Semaste        return error;
540254721Semaste
541254721Semaste    if (address == LLDB_INVALID_ADDRESS)
542254721Semaste    {
543254721Semaste        error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
544254721Semaste        return error;
545254721Semaste    }
546254721Semaste
547254721Semaste    // If we got here, we need to read the value from memory
548254721Semaste    size_t byte_size = GetValueByteSize (&error);
549254721Semaste
550254721Semaste    // Bail if we encountered any errors getting the byte size
551254721Semaste    if (error.Fail())
552254721Semaste        return error;
553254721Semaste
554254721Semaste    // Make sure we have enough room within "data", and if we don't make
555254721Semaste    // something large enough that does
556254721Semaste    if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
557254721Semaste    {
558254721Semaste        DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
559254721Semaste        data.SetData(data_sp);
560254721Semaste    }
561254721Semaste
562254721Semaste    uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
563254721Semaste    if (dst != NULL)
564254721Semaste    {
565254721Semaste        if (address_type == eAddressTypeHost)
566254721Semaste        {
567254721Semaste            // The address is an address in this process, so just copy it
568254721Semaste            memcpy (dst, (uint8_t*)NULL + address, byte_size);
569254721Semaste        }
570254721Semaste        else if ((address_type == eAddressTypeLoad) || (address_type == eAddressTypeFile))
571254721Semaste        {
572254721Semaste            if (file_so_addr.IsValid())
573254721Semaste            {
574254721Semaste                // We have a file address that we were able to translate into a
575254721Semaste                // section offset address so we might be able to read this from
576254721Semaste                // the object files if we don't have a live process. Lets always
577254721Semaste                // try and read from the process if we have one though since we
578254721Semaste                // want to read the actual value by setting "prefer_file_cache"
579254721Semaste                // to false.
580254721Semaste                const bool prefer_file_cache = false;
581254721Semaste                if (exe_ctx->GetTargetRef().ReadMemory(file_so_addr, prefer_file_cache, dst, byte_size, error) != byte_size)
582254721Semaste                {
583254721Semaste                    error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed", (uint64_t)address);
584254721Semaste                }
585254721Semaste            }
586254721Semaste            else
587254721Semaste            {
588254721Semaste                // The execution context might have a NULL process, but it
589254721Semaste                // might have a valid process in the exe_ctx->target, so use
590254721Semaste                // the ExecutionContext::GetProcess accessor to ensure we
591254721Semaste                // get the process if there is one.
592254721Semaste                Process *process = exe_ctx->GetProcessPtr();
593254721Semaste
594254721Semaste                if (process)
595254721Semaste                {
596254721Semaste                    const size_t bytes_read = process->ReadMemory(address, dst, byte_size, error);
597254721Semaste                    if (bytes_read != byte_size)
598254721Semaste                        error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (%u of %u bytes read)",
599254721Semaste                                                       (uint64_t)address,
600254721Semaste                                                       (uint32_t)bytes_read,
601254721Semaste                                                       (uint32_t)byte_size);
602254721Semaste                }
603254721Semaste                else
604254721Semaste                {
605254721Semaste                    error.SetErrorStringWithFormat("read memory from 0x%" PRIx64 " failed (invalid process)", (uint64_t)address);
606254721Semaste                }
607254721Semaste            }
608254721Semaste        }
609254721Semaste        else
610254721Semaste        {
611254721Semaste            error.SetErrorStringWithFormat ("unsupported AddressType value (%i)", address_type);
612254721Semaste        }
613254721Semaste    }
614254721Semaste    else
615254721Semaste    {
616254721Semaste        error.SetErrorStringWithFormat ("out of memory");
617254721Semaste    }
618254721Semaste
619254721Semaste    return error;
620254721Semaste}
621254721Semaste
622254721SemasteScalar &
623254721SemasteValue::ResolveValue(ExecutionContext *exe_ctx)
624254721Semaste{
625254721Semaste    const ClangASTType &clang_type = GetClangType();
626254721Semaste    if (clang_type.IsValid())
627254721Semaste    {
628254721Semaste        switch (m_value_type)
629254721Semaste        {
630254721Semaste        case eValueTypeScalar:               // raw scalar value
631254721Semaste            break;
632254721Semaste
633254721Semaste        default:
634254721Semaste        case eValueTypeFileAddress:
635254721Semaste        case eValueTypeLoadAddress:          // load address value
636254721Semaste        case eValueTypeHostAddress:          // host address value (for memory in the process that is using liblldb)
637254721Semaste            {
638254721Semaste                DataExtractor data;
639254721Semaste                lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
640254721Semaste                Error error (GetValueAsData (exe_ctx, data, 0, NULL));
641254721Semaste                if (error.Success())
642254721Semaste                {
643254721Semaste                    Scalar scalar;
644254721Semaste                    if (clang_type.GetValueAsScalar (data, 0, data.GetByteSize(), scalar))
645254721Semaste                    {
646254721Semaste                        m_value = scalar;
647254721Semaste                        m_value_type = eValueTypeScalar;
648254721Semaste                    }
649254721Semaste                    else
650254721Semaste                    {
651254721Semaste                        if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
652254721Semaste                        {
653254721Semaste                            m_value.Clear();
654254721Semaste                            m_value_type = eValueTypeScalar;
655254721Semaste                        }
656254721Semaste                    }
657254721Semaste                }
658254721Semaste                else
659254721Semaste                {
660254721Semaste                    if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
661254721Semaste                    {
662254721Semaste                        m_value.Clear();
663254721Semaste                        m_value_type = eValueTypeScalar;
664254721Semaste                    }
665254721Semaste                }
666254721Semaste            }
667254721Semaste            break;
668254721Semaste        }
669254721Semaste    }
670254721Semaste    return m_value;
671254721Semaste}
672254721Semaste
673254721SemasteVariable *
674254721SemasteValue::GetVariable()
675254721Semaste{
676254721Semaste    if (m_context_type == eContextTypeVariable)
677254721Semaste        return static_cast<Variable *> (m_context);
678254721Semaste    return NULL;
679254721Semaste}
680254721Semaste
681254721Semastevoid
682254721SemasteValue::Clear()
683254721Semaste{
684254721Semaste    m_value.Clear();
685254721Semaste    m_vector.Clear();
686254721Semaste    m_clang_type.Clear();
687254721Semaste    m_value_type = eValueTypeScalar;
688254721Semaste    m_context = NULL;
689254721Semaste    m_context_type = eContextTypeInvalid;
690254721Semaste    m_data_buffer.Clear();
691254721Semaste}
692254721Semaste
693254721Semaste
694254721Semasteconst char *
695254721SemasteValue::GetValueTypeAsCString (ValueType value_type)
696254721Semaste{
697254721Semaste    switch (value_type)
698254721Semaste    {
699254721Semaste    case eValueTypeScalar:      return "scalar";
700254721Semaste    case eValueTypeVector:      return "vector";
701254721Semaste    case eValueTypeFileAddress: return "file address";
702254721Semaste    case eValueTypeLoadAddress: return "load address";
703254721Semaste    case eValueTypeHostAddress: return "host address";
704254721Semaste    };
705254721Semaste    return "???";
706254721Semaste}
707254721Semaste
708254721Semasteconst char *
709254721SemasteValue::GetContextTypeAsCString (ContextType context_type)
710254721Semaste{
711254721Semaste    switch (context_type)
712254721Semaste    {
713254721Semaste    case eContextTypeInvalid:       return "invalid";
714254721Semaste    case eContextTypeRegisterInfo:  return "RegisterInfo *";
715254721Semaste    case eContextTypeLLDBType:      return "Type *";
716254721Semaste    case eContextTypeVariable:      return "Variable *";
717254721Semaste    };
718254721Semaste    return "???";
719254721Semaste}
720254721Semaste
721254721SemasteValueList::ValueList (const ValueList &rhs)
722254721Semaste{
723254721Semaste    m_values = rhs.m_values;
724254721Semaste}
725254721Semaste
726254721Semasteconst ValueList &
727254721SemasteValueList::operator= (const ValueList &rhs)
728254721Semaste{
729254721Semaste    m_values = rhs.m_values;
730254721Semaste    return *this;
731254721Semaste}
732254721Semaste
733254721Semastevoid
734254721SemasteValueList::PushValue (const Value &value)
735254721Semaste{
736254721Semaste    m_values.push_back (value);
737254721Semaste}
738254721Semaste
739254721Semastesize_t
740254721SemasteValueList::GetSize()
741254721Semaste{
742254721Semaste    return m_values.size();
743254721Semaste}
744254721Semaste
745254721SemasteValue *
746254721SemasteValueList::GetValueAtIndex (size_t idx)
747254721Semaste{
748254721Semaste    if (idx < GetSize())
749254721Semaste    {
750254721Semaste        return &(m_values[idx]);
751254721Semaste    }
752254721Semaste    else
753254721Semaste        return NULL;
754254721Semaste}
755254721Semaste
756254721Semastevoid
757254721SemasteValueList::Clear ()
758254721Semaste{
759254721Semaste    m_values.clear();
760254721Semaste}
761254721Semaste
762