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 
29 using namespace lldb;
30 using namespace lldb_private;
31 
32 Value::Value() :
33     m_value(),
34     m_value_type(eValueTypeScalar),
35     m_context(NULL),
36     m_context_type(eContextTypeInvalid)
37 {
38 }
39 
40 Value::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 
48 Value::Value(int v) :
49     m_value(v),
50     m_value_type(eValueTypeScalar),
51     m_context(NULL),
52     m_context_type(eContextTypeInvalid)
53 {
54 }
55 
56 Value::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 
64 Value::Value(long v) :
65     m_value(v),
66     m_value_type(eValueTypeScalar),
67     m_context(NULL),
68     m_context_type(eContextTypeInvalid)
69 {
70 }
71 
72 Value::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 
80 Value::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 
88 Value::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 
96 Value::Value(float v) :
97     m_value(v),
98     m_value_type(eValueTypeScalar),
99     m_context(NULL),
100     m_context_type(eContextTypeInvalid)
101 {
102 }
103 
104 Value::Value(double v) :
105     m_value(v),
106     m_value_type(eValueTypeScalar),
107     m_context(NULL),
108     m_context_type(eContextTypeInvalid)
109 {
110 }
111 
112 Value::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 
120 Value::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 
130 Value::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 
145 Value *
146 Value::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 
156 Value *
157 Value::GetProxyTarget()
158 {
159     if (m_context_type == eContextTypeValue)
160         return (Value*)m_context;
161     else
162         return NULL;
163 }
164 
165 void
166 Value::Dump (Stream* strm)
167 {
168     if (m_context_type == eContextTypeValue)
169     {
170         ((Value*)m_context)->Dump (strm);
171         return;
172     }
173 
174     m_value.GetValue (strm, true);
175     strm->Printf(", value_type = %s, context = %p, context_type = %s",
176                 Value::GetValueTypeAsCString(m_value_type),
177                 m_context,
178                 Value::GetContextTypeAsCString(m_context_type));
179 }
180 
181 Value::ValueType
182 Value::GetValueType() const
183 {
184     if (m_context_type == eContextTypeValue)
185         return ((Value*)m_context)->GetValueType ();
186 
187     return m_value_type;
188 }
189 
190 lldb::AddressType
191 Value::GetValueAddressType () const
192 {
193     if (m_context_type == eContextTypeValue)
194         return ((Value*)m_context)->GetValueAddressType ();
195 
196     switch (m_value_type)
197     {
198     default:
199     case eValueTypeScalar:
200         break;
201     case eValueTypeLoadAddress: return eAddressTypeLoad;
202     case eValueTypeFileAddress: return eAddressTypeFile;
203     case eValueTypeHostAddress: return eAddressTypeHost;
204     }
205     return eAddressTypeInvalid;
206 }
207 
208 
209 Value::ContextType
210 Value::GetContextType() const
211 {
212     if (m_context_type == eContextTypeValue)
213         return ((Value*)m_context)->GetContextType ();
214 
215     return m_context_type;
216 }
217 
218 void
219 Value::SetValueType (Value::ValueType value_type)
220 {
221     if (m_context_type == eContextTypeValue)
222     {
223         ((Value*)m_context)->SetValueType(value_type);
224         return;
225     }
226 
227     m_value_type = value_type;
228 }
229 
230 void
231 Value::ClearContext ()
232 {
233     if (m_context_type == eContextTypeValue)
234     {
235         ((Value*)m_context)->ClearContext();
236         return;
237     }
238 
239     m_context = NULL;
240     m_context_type = eContextTypeInvalid;
241 }
242 
243 void
244 Value::SetContext (Value::ContextType context_type, void *p)
245 {
246     if (m_context_type == eContextTypeValue)
247     {
248         ((Value*)m_context)->SetContext(context_type, p);
249         return;
250     }
251 
252     m_context_type = context_type;
253     m_context = p;
254 }
255 
256 RegisterInfo *
257 Value::GetRegisterInfo()
258 {
259     if (m_context_type == eContextTypeValue)
260         return ((Value*)m_context)->GetRegisterInfo();
261 
262     if (m_context_type == eContextTypeRegisterInfo)
263         return static_cast<RegisterInfo *> (m_context);
264     return NULL;
265 }
266 
267 Type *
268 Value::GetType()
269 {
270     if (m_context_type == eContextTypeValue)
271         return ((Value*)m_context)->GetType();
272 
273     if (m_context_type == eContextTypeLLDBType)
274         return static_cast<Type *> (m_context);
275     return NULL;
276 }
277 
278 Scalar &
279 Value::GetScalar()
280 {
281     if (m_context_type == eContextTypeValue)
282         return ((Value*)m_context)->GetScalar();
283 
284     return m_value;
285 }
286 
287 void
288 Value::ResizeData(int len)
289 {
290     if (m_context_type == eContextTypeValue)
291     {
292         ((Value*)m_context)->ResizeData(len);
293         return;
294     }
295 
296     m_value_type = eValueTypeHostAddress;
297     m_data_buffer.SetByteSize(len);
298     m_value = (uintptr_t)m_data_buffer.GetBytes();
299 }
300 
301 bool
302 Value::ValueOf(ExecutionContext *exe_ctx, clang::ASTContext *ast_context)
303 {
304     if (m_context_type == eContextTypeValue)
305         return ((Value*)m_context)->ValueOf(exe_ctx, ast_context);
306 
307     switch (m_context_type)
308     {
309     default:
310     case eContextTypeInvalid:
311     case eContextTypeClangType:          // clang::Type *
312     case eContextTypeRegisterInfo:     // RegisterInfo *
313     case eContextTypeLLDBType:             // Type *
314         break;
315 
316     case eContextTypeVariable:         // Variable *
317         ResolveValue(exe_ctx, ast_context);
318         return true;
319     }
320     return false;
321 }
322 
323 size_t
324 Value::GetValueByteSize (clang::ASTContext *ast_context, Error *error_ptr)
325 {
326     if (m_context_type == eContextTypeValue)
327         return ((Value*)m_context)->GetValueByteSize(ast_context, error_ptr);
328 
329     size_t byte_size = 0;
330 
331     switch (m_context_type)
332     {
333     default:
334     case eContextTypeInvalid:
335         // If we have no context, there is no way to know how much memory to read
336         if (error_ptr)
337             error_ptr->SetErrorString ("Invalid context type, there is no way to know how much memory to read.");
338         break;
339 
340     case eContextTypeClangType:
341         if (ast_context == NULL)
342         {
343             if (error_ptr)
344                 error_ptr->SetErrorString ("Can't determine size of opaque clang type with NULL ASTContext *.");
345         }
346         else
347         {
348             uint64_t bit_width = ClangASTType::GetClangTypeBitWidth (ast_context, m_context);
349             byte_size = (bit_width + 7 ) / 8;
350         }
351         break;
352 
353     case eContextTypeRegisterInfo:     // RegisterInfo *
354         if (GetRegisterInfo())
355             byte_size = GetRegisterInfo()->byte_size;
356         else if (error_ptr)
357                 error_ptr->SetErrorString ("Can't determine byte size with NULL RegisterInfo *.");
358 
359         break;
360 
361     case eContextTypeLLDBType:             // Type *
362         if (GetType())
363             byte_size = GetType()->GetByteSize();
364         else if (error_ptr)
365             error_ptr->SetErrorString ("Can't determine byte size with NULL Type *.");
366         break;
367 
368     case eContextTypeVariable:         // Variable *
369         if (GetVariable())
370             byte_size = GetVariable()->GetType()->GetByteSize();
371         else if (error_ptr)
372             error_ptr->SetErrorString ("Can't determine byte size with NULL Variable *.");
373         break;
374     }
375 
376     if (error_ptr)
377     {
378         if (byte_size == 0)
379         {
380             if (error_ptr->Success())
381                 error_ptr->SetErrorString("Unable to determine byte size.");
382         }
383         else
384         {
385             error_ptr->Clear();
386         }
387     }
388     return byte_size;
389 }
390 
391 void *
392 Value::GetClangType ()
393 {
394     if (m_context_type == eContextTypeValue)
395         return ((Value*)m_context)->GetClangType();
396 
397     switch (m_context_type)
398     {
399     default:
400     case eContextTypeInvalid:
401         break;
402 
403     case eContextTypeClangType:
404         return m_context;
405 
406     case eContextTypeRegisterInfo:
407         break;    // TODO: Eventually convert into a clang type?
408 
409     case eContextTypeLLDBType:
410         if (GetType())
411             return GetType()->GetClangType();
412         break;
413 
414     case eContextTypeVariable:
415         if (GetVariable())
416             return GetVariable()->GetType()->GetClangType();
417         break;
418     }
419 
420     return NULL;
421 }
422 
423 lldb::Format
424 Value::GetValueDefaultFormat ()
425 {
426     if (m_context_type == eContextTypeValue)
427         return ((Value*)m_context)->GetValueDefaultFormat();
428 
429     switch (m_context_type)
430     {
431     default:
432     case eContextTypeInvalid:
433         break;
434 
435     case eContextTypeClangType:
436         return ClangASTType::GetFormat (m_context);
437 
438     case eContextTypeRegisterInfo:
439         if (GetRegisterInfo())
440             return GetRegisterInfo()->format;
441         break;
442 
443     case eContextTypeLLDBType:
444         if (GetType())
445             return GetType()->GetFormat();
446         break;
447 
448     case eContextTypeVariable:
449         if (GetVariable())
450             return GetVariable()->GetType()->GetFormat();
451         break;
452 
453     }
454 
455     // Return a good default in case we can't figure anything out
456     return eFormatHex;
457 }
458 
459 bool
460 Value::GetData (DataExtractor &data)
461 {
462     switch (m_value_type)
463     {
464     default:
465         break;
466 
467     case eValueTypeScalar:
468         if (m_value.GetData (data))
469             return true;
470         break;
471 
472     case eValueTypeLoadAddress:
473     case eValueTypeFileAddress:
474     case eValueTypeHostAddress:
475         if (m_data_buffer.GetByteSize())
476         {
477             data.SetData(m_data_buffer.GetBytes(), m_data_buffer.GetByteSize(), data.GetByteOrder());
478             return true;
479         }
480         break;
481     }
482 
483     return false;
484 
485 }
486 
487 Error
488 Value::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 
648 Scalar &
649 Value::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 
728 Variable *
729 Value::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 
741 const char *
742 Value::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 
754 const char *
755 Value::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 
769 ValueList::ValueList (const ValueList &rhs)
770 {
771     m_values = rhs.m_values;
772 }
773 
774 const ValueList &
775 ValueList::operator= (const ValueList &rhs)
776 {
777     m_values = rhs.m_values;
778     return *this;
779 }
780 
781 void
782 ValueList::PushValue (const Value &value)
783 {
784     m_values.push_back (value);
785 }
786 
787 size_t
788 ValueList::GetSize()
789 {
790     return m_values.size();
791 }
792 
793 Value *
794 ValueList::GetValueAtIndex (size_t idx)
795 {
796     if (idx < GetSize())
797     {
798         return &(m_values[idx]);
799     }
800     else
801         return NULL;
802 }
803 
804 void
805 ValueList::Clear ()
806 {
807     m_values.clear();
808 }
809