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