Value.cpp revision 6916e358c9725b75ed91f31236c147f26c9af10e
1//===-- Value.cpp -----------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/Value.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/DataBufferHeap.h"
18#include "lldb/Core/Module.h"
19#include "lldb/Core/Stream.h"
20#include "lldb/Symbol/ClangASTType.h"
21#include "lldb/Symbol/ClangASTContext.h"
22#include "lldb/Symbol/ObjectFile.h"
23#include "lldb/Symbol/SymbolContext.h"
24#include "lldb/Symbol/Type.h"
25#include "lldb/Symbol/Variable.h"
26#include "lldb/Target/ExecutionContext.h"
27#include "lldb/Target/Process.h"
28
29using namespace lldb;
30using namespace lldb_private;
31
32Value::Value() :
33    m_value(),
34    m_value_type(eValueTypeScalar),
35    m_context(NULL),
36    m_context_type(eContextTypeInvalid)
37{
38}
39
40Value::Value(const Scalar& scalar) :
41    m_value(scalar),
42    m_value_type(eValueTypeScalar),
43    m_context(NULL),
44    m_context_type(eContextTypeInvalid)
45{
46}
47
48Value::Value(int v) :
49    m_value(v),
50    m_value_type(eValueTypeScalar),
51    m_context(NULL),
52    m_context_type(eContextTypeInvalid)
53{
54}
55
56Value::Value(unsigned int v) :
57    m_value(v),
58    m_value_type(eValueTypeScalar),
59    m_context(NULL),
60    m_context_type(eContextTypeInvalid)
61{
62}
63
64Value::Value(long v) :
65    m_value(v),
66    m_value_type(eValueTypeScalar),
67    m_context(NULL),
68    m_context_type(eContextTypeInvalid)
69{
70}
71
72Value::Value(unsigned long v) :
73    m_value(v),
74    m_value_type(eValueTypeScalar),
75    m_context(NULL),
76    m_context_type(eContextTypeInvalid)
77{
78}
79
80Value::Value(long long v) :
81    m_value(v),
82    m_value_type(eValueTypeScalar),
83    m_context(NULL),
84    m_context_type(eContextTypeInvalid)
85{
86}
87
88Value::Value(unsigned long long v) :
89    m_value(v),
90    m_value_type(eValueTypeScalar),
91    m_context(NULL),
92    m_context_type(eContextTypeInvalid)
93{
94}
95
96Value::Value(float v) :
97    m_value(v),
98    m_value_type(eValueTypeScalar),
99    m_context(NULL),
100    m_context_type(eContextTypeInvalid)
101{
102}
103
104Value::Value(double v) :
105    m_value(v),
106    m_value_type(eValueTypeScalar),
107    m_context(NULL),
108    m_context_type(eContextTypeInvalid)
109{
110}
111
112Value::Value(long double v) :
113    m_value(v),
114    m_value_type(eValueTypeScalar),
115    m_context(NULL),
116    m_context_type(eContextTypeInvalid)
117{
118}
119
120Value::Value(const uint8_t *bytes, int len) :
121    m_value(),
122    m_value_type(eValueTypeHostAddress),
123    m_context(NULL),
124    m_context_type(eContextTypeInvalid)
125{
126    m_data_buffer.CopyData(bytes, len);
127    m_value = (uintptr_t)m_data_buffer.GetBytes();
128}
129
130Value::Value(const Value &v) :
131    m_value(v.m_value),
132    m_value_type(v.m_value_type),
133    m_context(v.m_context),
134    m_context_type(v.m_context_type)
135{
136    if ((uintptr_t)v.m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v.m_data_buffer.GetBytes())
137    {
138        m_data_buffer.CopyData(v.m_data_buffer.GetBytes(),
139                               v.m_data_buffer.GetByteSize());
140
141        m_value = (uintptr_t)m_data_buffer.GetBytes();
142    }
143}
144
145Value *
146Value::CreateProxy()
147{
148    if (m_context_type == eContextTypeValue)
149        return ((Value*)m_context)->CreateProxy ();
150
151    Value *ret = new Value;
152    ret->SetContext(eContextTypeValue, this);
153    return ret;
154}
155
156Value *
157Value::GetProxyTarget()
158{
159    if (m_context_type == eContextTypeValue)
160        return (Value*)m_context;
161    else
162        return NULL;
163}
164
165//#include "clang/Lex/LiteralSupport.h"
166//#include "clang/AST/ASTContext.h"
167//#include "clang/Frontend/CompilerInstance.h"
168//
169//Value::Value (const char *data, llvm::CompilerInstance *compiler)
170//{
171//    clang::NumericLiteralParser parser(data, data + strlen(data), clang::SourceLocation(),
172//                                compiler->getPreprocessor());
173//    if (parser.had_error)
174//    {
175//    }
176//    else if (parser.isBool)
177//    {
178//        APInt value;
179//        parser.GetIntegerValue(value);
180//    }
181//    else if (parser.isLong)
182//    {
183//    }
184//    else if (parser.isLongLong)
185//    {
186//    }
187//    else if (parser.isFloat)
188//    {
189//    }
190//
191//}
192//
193void
194Value::Dump (Stream* strm)
195{
196    if (m_context_type == eContextTypeValue)
197    {
198        ((Value*)m_context)->Dump (strm);
199        return;
200    }
201
202    m_value.GetValue (strm, true);
203    strm->Printf(", value_type = %s, context = %p, context_type = %s",
204                Value::GetValueTypeAsCString(m_value_type),
205                m_context,
206                Value::GetContextTypeAsCString(m_context_type));
207}
208
209Value::ValueType
210Value::GetValueType() const
211{
212    if (m_context_type == eContextTypeValue)
213        return ((Value*)m_context)->GetValueType ();
214
215    return m_value_type;
216}
217
218lldb::AddressType
219Value::GetValueAddressType () const
220{
221    if (m_context_type == eContextTypeValue)
222        return ((Value*)m_context)->GetValueAddressType ();
223
224    switch (m_value_type)
225    {
226    default:
227    case eValueTypeScalar:
228        break;
229    case eValueTypeLoadAddress: return eAddressTypeLoad;
230    case eValueTypeFileAddress: return eAddressTypeFile;
231    case eValueTypeHostAddress: return eAddressTypeHost;
232    }
233    return eAddressTypeInvalid;
234}
235
236
237Value::ContextType
238Value::GetContextType() const
239{
240    if (m_context_type == eContextTypeValue)
241        return ((Value*)m_context)->GetContextType ();
242
243    return m_context_type;
244}
245
246void
247Value::SetValueType (Value::ValueType value_type)
248{
249    if (m_context_type == eContextTypeValue)
250    {
251        ((Value*)m_context)->SetValueType(value_type);
252        return;
253    }
254
255    m_value_type = value_type;
256}
257
258void
259Value::ClearContext ()
260{
261    if (m_context_type == eContextTypeValue)
262    {
263        ((Value*)m_context)->ClearContext();
264        return;
265    }
266
267    m_context = NULL;
268    m_context_type = eContextTypeInvalid;
269}
270
271void
272Value::SetContext (Value::ContextType context_type, void *p)
273{
274    if (m_context_type == eContextTypeValue)
275    {
276        ((Value*)m_context)->SetContext(context_type, p);
277        return;
278    }
279
280    m_context_type = context_type;
281    m_context = p;
282}
283
284RegisterInfo *
285Value::GetRegisterInfo()
286{
287    if (m_context_type == eContextTypeValue)
288        return ((Value*)m_context)->GetRegisterInfo();
289
290    if (m_context_type == eContextTypeRegisterInfo)
291        return static_cast<RegisterInfo *> (m_context);
292    return NULL;
293}
294
295Type *
296Value::GetType()
297{
298    if (m_context_type == eContextTypeValue)
299        return ((Value*)m_context)->GetType();
300
301    if (m_context_type == eContextTypeLLDBType)
302        return static_cast<Type *> (m_context);
303    return NULL;
304}
305
306Scalar &
307Value::GetScalar()
308{
309    if (m_context_type == eContextTypeValue)
310        return ((Value*)m_context)->GetScalar();
311
312    return m_value;
313}
314
315void
316Value::ResizeData(int len)
317{
318    if (m_context_type == eContextTypeValue)
319    {
320        ((Value*)m_context)->ResizeData(len);
321        return;
322    }
323
324    m_value_type = eValueTypeHostAddress;
325    m_data_buffer.SetByteSize(len);
326    m_value = (uintptr_t)m_data_buffer.GetBytes();
327}
328
329bool
330Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
331{
332    if (m_context_type == eContextTypeValue)
333        return ((Value*)m_context)->ValueOf(exe_ctx, ast_context);
334
335    switch (m_context_type)
336    {
337    default:
338    case eContextTypeInvalid:
339    case eContextTypeClangType:          // clang::Type *
340    case eContextTypeRegisterInfo:     // RegisterInfo *
341    case eContextTypeLLDBType:             // Type *
342        break;
343
344    case eContextTypeVariable:         // Variable *
345        ResolveValue(exe_ctx, ast_context);
346        return true;
347    }
348    return false;
349}
350
351size_t
352Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr)
353{
354    if (m_context_type == eContextTypeValue)
355        return ((Value*)m_context)->GetValueByteSize(ast_context, error_ptr);
356
357    size_t byte_size = 0;
358
359    switch (m_context_type)
360    {
361    default:
362    case eContextTypeInvalid:
363        // If we have no context, there is no way to know how much memory to read
364        if (error_ptr)
365            error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read.");
366        break;
367
368    case eContextTypeClangType:
369        if (ast_context == NULL)
370        {
371            if (error_ptr)
372                error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *.");
373        }
374        else
375        {
376            uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context);
377            byte_size = (bit_width + 7 ) / 8;
378        }
379        break;
380
381    case eContextTypeRegisterInfo:     // RegisterInfo *
382        if (GetRegisterInfo())
383            byte_size = GetRegisterInfo()->byte_size;
384        else if (error_ptr)
385                error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *.");
386
387        break;
388
389    case eContextTypeLLDBType:             // Type *
390        if (GetType())
391            byte_size = GetType()->GetByteSize();
392        else if (error_ptr)
393            error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
394        break;
395
396    case eContextTypeVariable:         // Variable *
397        if (GetVariable())
398            byte_size = GetVariable()->GetType()->GetByteSize();
399        else if (error_ptr)
400            error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *.");
401        break;
402    }
403
404    if (error_ptr)
405    {
406        if (byte_size == 0)
407        {
408            if (error_ptr->Success())
409                error_ptr->SetErrorString("Unable to determine byte size.");
410        }
411        else
412        {
413            error_ptr->Clear();
414        }
415    }
416    return byte_size;
417}
418
419void *
420Value::GetClangType ()
421{
422    if (m_context_type == eContextTypeValue)
423        return ((Value*)m_context)->GetClangType();
424
425    switch (m_context_type)
426    {
427    default:
428    case eContextTypeInvalid:
429        break;
430
431    case eContextTypeClangType:
432        return m_context;
433
434    case eContextTypeRegisterInfo:
435        break;    // TODO: Eventually convert into a clang type?
436
437    case eContextTypeLLDBType:
438        if (GetType())
439            return GetType()->GetClangType();
440        break;
441
442    case eContextTypeVariable:
443        if (GetVariable())
444            return GetVariable()->GetType()->GetClangType();
445        break;
446    }
447
448    return NULL;
449}
450
451lldb::Format
452Value::GetValueDefaultFormat ()
453{
454    if (m_context_type == eContextTypeValue)
455        return ((Value*)m_context)->GetValueDefaultFormat();
456
457    switch (m_context_type)
458    {
459    default:
460    case eContextTypeInvalid:
461        break;
462
463    case eContextTypeClangType:
464        return ClangASTType::GetFormat (m_context);
465
466    case eContextTypeRegisterInfo:
467        if (GetRegisterInfo())
468            return GetRegisterInfo()->format;
469        break;
470
471    case eContextTypeLLDBType:
472        if (GetType())
473            return GetType()->GetFormat();
474        break;
475
476    case eContextTypeVariable:
477        if (GetVariable())
478            return GetVariable()->GetType()->GetFormat();
479        break;
480
481    }
482
483    // Return a good default in case we can't figure anything out
484    return eFormatHex;
485}
486
487Error
488Value::GetValueAsData (ExecutionContext *exe_ctx, clang::ASTContext *ast_context, DataExtractor &data, uint32_t data_offset)
489{
490    if (m_context_type == eContextTypeValue)
491        return ((Value*)m_context)->GetValueAsData(exe_ctx, ast_context, data, data_offset);
492
493    data.Clear();
494
495    Error error;
496    lldb::addr_t address = LLDB_INVALID_ADDRESS;
497    lldb::AddressType address_type = eAddressTypeFile;
498    switch (m_value_type)
499    {
500    default:
501        error.SetErrorStringWithFormat("invalid value type %i", m_value_type);
502        break;
503
504    case eValueTypeScalar:
505        data.SetByteOrder (eByteOrderHost);
506        data.SetAddressByteSize(sizeof(void *));
507        if (m_value.GetData (data))
508            return error;   // Success;
509        error.SetErrorStringWithFormat("extracting data from value failed");
510        break;
511
512    case eValueTypeLoadAddress:
513        if (exe_ctx == NULL)
514        {
515            error.SetErrorString ("can't read memory (no execution context)");
516        }
517        else if (exe_ctx->process == NULL)
518        {
519            error.SetErrorString ("can't read memory (invalid process)");
520        }
521        else
522        {
523            address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
524            address_type = eAddressTypeLoad;
525            data.SetByteOrder(exe_ctx->process->GetByteOrder());
526            data.SetAddressByteSize(exe_ctx->process->GetAddressByteSize());
527        }
528        break;
529
530    case eValueTypeFileAddress:
531        {
532            // The only thing we can currently lock down to a module so that
533            // we can resolve a file address, is a variable.
534            Variable *variable = GetVariable();
535
536            if (GetVariable())
537            {
538                lldb::addr_t file_addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
539                if (file_addr != LLDB_INVALID_ADDRESS)
540                {
541                    SymbolContext var_sc;
542                    variable->CalculateSymbolContext(&var_sc);
543                    if (var_sc.module_sp)
544                    {
545                        ObjectFile *objfile = var_sc.module_sp->GetObjectFile();
546                        if (objfile)
547                        {
548                            Address so_addr(file_addr, objfile->GetSectionList());
549                            address = so_addr.GetLoadAddress (exe_ctx->target);
550                            if (address != LLDB_INVALID_ADDRESS)
551                            {
552                                address_type = eAddressTypeLoad;
553                                data.SetByteOrder(exe_ctx->process->GetByteOrder());
554                                data.SetAddressByteSize(exe_ctx->process->GetAddressByteSize());
555                            }
556                            else
557                            {
558                                data.SetByteOrder(objfile->GetByteOrder());
559                                data.SetAddressByteSize(objfile->GetAddressByteSize());
560                            }
561                        }
562                        if (address_type == eAddressTypeFile)
563                            error.SetErrorStringWithFormat ("%s is not loaded.\n", var_sc.module_sp->GetFileSpec().GetFilename().AsCString());
564                    }
565                    else
566                    {
567                        error.SetErrorStringWithFormat ("unable to resolve the module for file address 0x%llx for variable '%s'", file_addr, variable->GetName().AsCString(""));
568                    }
569                }
570                else
571                {
572                    error.SetErrorString ("Invalid file address.");
573                }
574            }
575            else
576            {
577                // Can't convert a file address to anything valid without more
578                // context (which Module it came from)
579                error.SetErrorString ("can't read memory from file address without more context");
580            }
581        }
582        break;
583
584    case eValueTypeHostAddress:
585        address = m_value.ULongLong(LLDB_INVALID_ADDRESS);
586        data.SetByteOrder(eByteOrderHost);
587        data.SetAddressByteSize(sizeof(void *));
588        address_type = eAddressTypeHost;
589        break;
590    }
591
592    // Bail if we encountered any errors
593    if (error.Fail())
594        return error;
595
596    if (address == LLDB_INVALID_ADDRESS)
597    {
598        error.SetErrorStringWithFormat ("invalid %s address", address_type == eAddressTypeHost ? "host" : "load");
599        return error;
600    }
601
602    // If we got here, we need to read the value from memory
603    uint32_t byte_size = GetValueByteSize (ast_context, &error);
604
605    // Bail if we encountered any errors getting the byte size
606    if (error.Fail())
607        return error;
608
609    // Make sure we have enough room within "data", and if we don't make
610    // something large enough that does
611    if (!data.ValidOffsetForDataOfSize (data_offset, byte_size))
612    {
613        DataBufferSP data_sp(new DataBufferHeap (data_offset + byte_size, '\0'));
614        data.SetData(data_sp);
615    }
616
617    uint8_t* dst = const_cast<uint8_t*>(data.PeekData (data_offset, byte_size));
618    if (dst != NULL)
619    {
620        if (address_type == eAddressTypeHost)
621        {
622            // The address is an address in this process, so just copy it
623            memcpy (dst, (uint8_t*)NULL + address, byte_size);
624        }
625        else if (address_type == eAddressTypeLoad)
626        {
627            if (exe_ctx->process->ReadMemory(address, dst, byte_size, error) != byte_size)
628            {
629                if (error.Success())
630                    error.SetErrorStringWithFormat("read %u bytes of memory from 0x%llx failed", (uint64_t)address, byte_size);
631                else
632                    error.SetErrorStringWithFormat("read memory from 0x%llx failed", (uint64_t)address);
633            }
634        }
635        else
636        {
637            error.SetErrorStringWithFormat ("unsupported lldb::AddressType value (%i)", address_type);
638        }
639    }
640    else
641    {
642        error.SetErrorStringWithFormat ("out of memory");
643    }
644
645    return error;
646}
647
648Scalar &
649Value::ResolveValue(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
650{
651    Scalar scalar;
652    if (m_context_type == eContextTypeValue)
653    {
654        // Resolve the proxy
655
656        Value * v = (Value*)m_context;
657
658        m_value = v->m_value;
659        m_value_type = v->m_value_type;
660        m_context = v->m_context;
661        m_context_type = v->m_context_type;
662
663        if ((uintptr_t)v->m_value.ULongLong(LLDB_INVALID_ADDRESS) == (uintptr_t)v->m_data_buffer.GetBytes())
664        {
665            m_data_buffer.CopyData(v->m_data_buffer.GetBytes(),
666                                   v->m_data_buffer.GetByteSize());
667
668            m_value = (uintptr_t)m_data_buffer.GetBytes();
669        }
670    }
671
672    if (m_context_type == eContextTypeClangType)
673    {
674        void *opaque_clang_qual_type = GetClangType();
675        switch (m_value_type)
676        {
677        case eValueTypeScalar:               // raw scalar value
678            break;
679
680        case eContextTypeValue:
681            m_value.Clear();    // TODO: Sean, fill this in
682            break;
683
684        default:
685        case eValueTypeFileAddress:
686            m_value.Clear();
687            break;
688
689        case eValueTypeLoadAddress:          // load address value
690        case eValueTypeHostAddress:          // host address value (for memory in the process that is using liblldb)
691            {
692                lldb::AddressType address_type = m_value_type == eValueTypeLoadAddress ? eAddressTypeLoad : eAddressTypeHost;
693                lldb::addr_t addr = m_value.ULongLong(LLDB_INVALID_ADDRESS);
694                DataExtractor data;
695                if (ClangASTType::ReadFromMemory (ast_context, opaque_clang_qual_type, exe_ctx, addr, address_type, data))
696                {
697                    if (ClangASTType::GetValueAsScalar (ast_context, opaque_clang_qual_type, data, 0, data.GetByteSize(), scalar))
698                    {
699                        m_value = scalar;
700                        m_value_type = eValueTypeScalar;
701                    }
702                    else
703                    {
704                        if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
705                        {
706                            m_value.Clear();
707                            m_value_type = eValueTypeScalar;
708                        }
709                    }
710                }
711                else
712                {
713                    if ((uintptr_t)addr != (uintptr_t)m_data_buffer.GetBytes())
714                    {
715                        m_value.Clear();
716                        m_value_type = eValueTypeScalar;
717                    }
718                }
719            }
720            break;
721        }
722
723
724    }
725    return m_value;
726}
727
728Variable *
729Value::GetVariable()
730{
731    if (m_context_type == eContextTypeValue)
732        return ((Value*)m_context)->GetVariable();
733
734    if (m_context_type == eContextTypeVariable)
735        return static_cast<Variable *> (m_context);
736    return NULL;
737}
738
739
740
741const char *
742Value::GetValueTypeAsCString (ValueType value_type)
743{
744    switch (value_type)
745    {
746    case eValueTypeScalar:      return "scalar";
747    case eValueTypeFileAddress: return "file address";
748    case eValueTypeLoadAddress: return "load address";
749    case eValueTypeHostAddress: return "host address";
750    };
751    return "???";
752}
753
754const char *
755Value::GetContextTypeAsCString (ContextType context_type)
756{
757    switch (context_type)
758    {
759    case eContextTypeInvalid:               return "invalid";
760    case eContextTypeClangType:   return "clang::Type *";
761    case eContextTypeRegisterInfo:        return "RegisterInfo *";
762    case eContextTypeLLDBType:                return "Type *";
763    case eContextTypeVariable:            return "Variable *";
764    case eContextTypeValue:                 return "Value"; // TODO: Sean, more description here?
765    };
766    return "???";
767}
768
769ValueList::ValueList (const ValueList &rhs)
770{
771    m_values = rhs.m_values;
772}
773
774const ValueList &
775ValueList::operator= (const ValueList &rhs)
776{
777    m_values = rhs.m_values;
778    return *this;
779}
780
781void
782ValueList::PushValue (const Value &value)
783{
784    m_values.push_back (value);
785}
786
787size_t
788ValueList::GetSize()
789{
790    return m_values.size();
791}
792
793Value *
794ValueList::GetValueAtIndex (size_t idx)
795{
796    if (idx < GetSize())
797    {
798        return &(m_values[idx]);
799    }
800    else
801        return NULL;
802}
803
804void
805ValueList::Clear ()
806{
807    m_values.clear();
808}
809