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