1 //===-- ValueObject.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/ValueObject.h"
11 
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "clang/AST/Type.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 // Project includes
19 #include "lldb/Core/DataBufferHeap.h"
20 #include "lldb/Core/StreamString.h"
21 #include "lldb/Core/ValueObjectChild.h"
22 #include "lldb/Core/ValueObjectList.h"
23 
24 #include "lldb/Symbol/ClangASTContext.h"
25 #include "lldb/Symbol/Type.h"
26 
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/Thread.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 static lldb::user_id_t g_value_obj_uid = 0;
35 
36 //----------------------------------------------------------------------
37 // ValueObject constructor
38 //----------------------------------------------------------------------
39 ValueObject::ValueObject () :
40     UserID (++g_value_obj_uid), // Unique identifier for every value object
41     m_update_id (0),    // Value object lists always start at 1, value objects start at zero
42     m_name (),
43     m_data (),
44     m_value (),
45     m_error (),
46     m_flags (),
47     m_value_str(),
48     m_location_str(),
49     m_summary_str(),
50     m_children(),
51     m_synthetic_children()
52 {
53 }
54 
55 //----------------------------------------------------------------------
56 // Destructor
57 //----------------------------------------------------------------------
58 ValueObject::~ValueObject ()
59 {
60 }
61 
62 user_id_t
63 ValueObject::GetUpdateID() const
64 {
65     return m_update_id;
66 }
67 
68 bool
69 ValueObject::UpdateValueIfNeeded (ExecutionContextScope *exe_scope)
70 {
71     if (exe_scope)
72     {
73         Process *process = exe_scope->CalculateProcess();
74         if (process)
75         {
76             const user_id_t stop_id = process->GetStopID();
77             if (m_update_id != stop_id)
78             {
79                 m_value_str.clear();
80                 m_location_str.clear();
81                 m_summary_str.clear();
82 
83                 UpdateValue (exe_scope);
84                 if (m_error.Success())
85                     m_update_id = stop_id;
86             }
87         }
88     }
89     return m_error.Success();
90 }
91 
92 const DataExtractor &
93 ValueObject::GetDataExtractor () const
94 {
95     return m_data;
96 }
97 
98 DataExtractor &
99 ValueObject::GetDataExtractor ()
100 {
101     return m_data;
102 }
103 
104 const Error &
105 ValueObject::GetError() const
106 {
107     return m_error;
108 }
109 
110 const ConstString &
111 ValueObject::GetName() const
112 {
113     return m_name;
114 }
115 
116 const char *
117 ValueObject::GetLocationAsCString (ExecutionContextScope *exe_scope)
118 {
119     if (UpdateValueIfNeeded(exe_scope))
120     {
121         if (m_location_str.empty())
122         {
123             StreamString sstr;
124 
125             switch (m_value.GetValueType())
126             {
127             default:
128                 break;
129 
130             case Value::eValueTypeScalar:
131                 if (m_value.GetContextType() == Value::eContextTypeDCRegisterInfo)
132                 {
133                     RegisterInfo *reg_info = m_value.GetRegisterInfo();
134                     if (reg_info)
135                     {
136                         if (reg_info->name)
137                             m_location_str = reg_info->name;
138                         else if (reg_info->alt_name)
139                             m_location_str = reg_info->alt_name;
140                         break;
141                     }
142                 }
143                 m_location_str = "scalar";
144                 break;
145 
146             case Value::eValueTypeLoadAddress:
147             case Value::eValueTypeFileAddress:
148             case Value::eValueTypeHostAddress:
149                 {
150                     uint32_t addr_nibble_size = m_data.GetAddressByteSize() * 2;
151                     sstr.Printf("0x%*.*llx", addr_nibble_size, addr_nibble_size, m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS));
152                     m_location_str.swap(sstr.GetString());
153                 }
154                 break;
155             }
156         }
157     }
158     return m_location_str.c_str();
159 }
160 
161 Value &
162 ValueObject::GetValue()
163 {
164     return m_value;
165 }
166 
167 const Value &
168 ValueObject::GetValue() const
169 {
170     return m_value;
171 }
172 
173 bool
174 ValueObject::GetValueIsValid ()
175 {
176     return m_flags.IsSet(eValueIsValid);
177 }
178 
179 
180 void
181 ValueObject::SetValueIsValid (bool b)
182 {
183     if (b)
184         m_flags.Set(eValueIsValid);
185     else
186         m_flags.Clear(eValueIsValid);
187 }
188 
189 bool
190 ValueObject::GetValueDidChange () const
191 {
192     return m_flags.IsSet(eValueChanged);
193 }
194 
195 void
196 ValueObject::SetValueDidChange (bool value_changed)
197 {
198     m_flags.Set(eValueChanged);
199 }
200 
201 ValueObjectSP
202 ValueObject::GetChildAtIndex (uint32_t idx, bool can_create)
203 {
204     ValueObjectSP child_sp;
205     if (idx < GetNumChildren())
206     {
207         // Check if we have already made the child value object?
208         if (can_create && m_children[idx].get() == NULL)
209         {
210             // No we haven't created the child at this index, so lets have our
211             // subclass do it and cache the result for quick future access.
212             m_children[idx] = CreateChildAtIndex (idx, false, 0);
213         }
214 
215         child_sp = m_children[idx];
216     }
217     return child_sp;
218 }
219 
220 uint32_t
221 ValueObject::GetIndexOfChildWithName (const ConstString &name)
222 {
223     bool omit_empty_base_classes = true;
224     return ClangASTContext::GetIndexOfChildWithName (GetClangAST(),
225                                                      GetOpaqueClangQualType(),
226                                                      name.AsCString(),
227                                                      omit_empty_base_classes);
228 }
229 
230 ValueObjectSP
231 ValueObject::GetChildMemberWithName (const ConstString &name, bool can_create)
232 {
233     // when getting a child by name, it could be burried inside some base
234     // classes (which really aren't part of the expression path), so we
235     // need a vector of indexes that can get us down to the correct child
236     std::vector<uint32_t> child_indexes;
237     clang::ASTContext *clang_ast = GetClangAST();
238     void *clang_type = GetOpaqueClangQualType();
239     bool omit_empty_base_classes = true;
240     const size_t num_child_indexes =  ClangASTContext::GetIndexOfChildMemberWithName (clang_ast,
241                                                                                       clang_type,
242                                                                                       name.AsCString(),
243                                                                                       omit_empty_base_classes,
244                                                                                       child_indexes);
245     ValueObjectSP child_sp;
246     if (num_child_indexes > 0)
247     {
248         std::vector<uint32_t>::const_iterator pos = child_indexes.begin ();
249         std::vector<uint32_t>::const_iterator end = child_indexes.end ();
250 
251         child_sp = GetChildAtIndex(*pos, can_create);
252         for (++pos; pos != end; ++pos)
253         {
254             if (child_sp)
255             {
256                 ValueObjectSP new_child_sp(child_sp->GetChildAtIndex (*pos, can_create));
257                 child_sp = new_child_sp;
258             }
259             else
260             {
261                 child_sp.reset();
262             }
263 
264         }
265     }
266     return child_sp;
267 }
268 
269 
270 uint32_t
271 ValueObject::GetNumChildren ()
272 {
273     if (m_flags.IsClear(eNumChildrenHasBeenSet))
274     {
275         SetNumChildren (CalculateNumChildren());
276     }
277     return m_children.size();
278 }
279 void
280 ValueObject::SetNumChildren (uint32_t num_children)
281 {
282     m_flags.Set(eNumChildrenHasBeenSet);
283     m_children.resize(num_children);
284 }
285 
286 void
287 ValueObject::SetName (const char *name)
288 {
289     m_name.SetCString(name);
290 }
291 
292 void
293 ValueObject::SetName (const ConstString &name)
294 {
295     m_name = name;
296 }
297 
298 ValueObjectSP
299 ValueObject::CreateChildAtIndex (uint32_t idx, bool synthetic_array_member, int32_t synthetic_index)
300 {
301     ValueObjectSP valobj_sp;
302     bool omit_empty_base_classes = true;
303 
304     std::string child_name_str;
305     uint32_t child_byte_size = 0;
306     int32_t child_byte_offset = 0;
307     uint32_t child_bitfield_bit_size = 0;
308     uint32_t child_bitfield_bit_offset = 0;
309     const bool transparent_pointers = synthetic_array_member == false;
310     clang::ASTContext *clang_ast = GetClangAST();
311     void *clang_type = GetOpaqueClangQualType();
312     void *child_clang_type;
313     child_clang_type = ClangASTContext::GetChildClangTypeAtIndex (clang_ast,
314                                                                   GetName().AsCString(),
315                                                                   clang_type,
316                                                                   idx,
317                                                                   transparent_pointers,
318                                                                   omit_empty_base_classes,
319                                                                   child_name_str,
320                                                                   child_byte_size,
321                                                                   child_byte_offset,
322                                                                   child_bitfield_bit_size,
323                                                                   child_bitfield_bit_offset);
324     if (child_clang_type)
325     {
326         if (synthetic_index)
327             child_byte_offset += child_byte_size * synthetic_index;
328 
329         ConstString child_name;
330         if (!child_name_str.empty())
331             child_name.SetCString (child_name_str.c_str());
332 
333         valobj_sp.reset (new ValueObjectChild (this,
334                                                clang_ast,
335                                                child_clang_type,
336                                                child_name,
337                                                child_byte_size,
338                                                child_byte_offset,
339                                                child_bitfield_bit_size,
340                                                child_bitfield_bit_offset));
341     }
342     return valobj_sp;
343 }
344 
345 const char *
346 ValueObject::GetSummaryAsCString (ExecutionContextScope *exe_scope)
347 {
348     if (UpdateValueIfNeeded (exe_scope))
349     {
350         if (m_summary_str.empty())
351         {
352             void *clang_type = GetOpaqueClangQualType();
353 
354             // See if this is a pointer to a C string?
355             uint32_t fixed_length = 0;
356             if (clang_type && ClangASTContext::IsCStringType (clang_type, fixed_length))
357             {
358                 Process *process = exe_scope->CalculateProcess();
359                 if (process != NULL)
360                 {
361                     StreamString sstr;
362                     lldb::addr_t cstr_address = LLDB_INVALID_ADDRESS;
363                     lldb::AddressType cstr_address_type = eAddressTypeInvalid;
364                     switch (GetValue().GetValueType())
365                     {
366                     case Value::eValueTypeScalar:
367                         cstr_address = m_value.GetScalar().ULongLong(LLDB_INVALID_ADDRESS);
368                         cstr_address_type = eAddressTypeLoad;
369                         break;
370 
371                     case Value::eValueTypeLoadAddress:
372                     case Value::eValueTypeFileAddress:
373                     case Value::eValueTypeHostAddress:
374                         {
375                             uint32_t data_offset = 0;
376                             cstr_address = m_data.GetPointer(&data_offset);
377                             cstr_address_type = m_value.GetValueAddressType();
378                             if (cstr_address_type == eAddressTypeInvalid)
379                                 cstr_address_type = eAddressTypeLoad;
380                         }
381                         break;
382                     }
383 
384                     if (cstr_address != LLDB_INVALID_ADDRESS)
385                     {
386                         DataExtractor data;
387                         size_t bytes_read = 0;
388                         std::vector<char> data_buffer;
389                         std::vector<char> cstr_buffer;
390                         size_t cstr_length;
391                         Error error;
392                         if (fixed_length > 0)
393                         {
394                             data_buffer.resize(fixed_length);
395                             // Resize the formatted buffer in case every character
396                             // uses the "\xXX" format and one extra byte for a NULL
397                             cstr_buffer.resize(data_buffer.size() * 4 + 1);
398                             data.SetData (data_buffer.data(), data_buffer.size(), eByteOrderHost);
399                             bytes_read = process->ReadMemory (cstr_address, data_buffer.data(), fixed_length, error);
400                             if (bytes_read > 0)
401                             {
402                                 sstr << '"';
403                                 cstr_length = data.Dump (&sstr,
404                                                          0,                 // Start offset in "data"
405                                                          eFormatChar,       // Print as characters
406                                                          1,                 // Size of item (1 byte for a char!)
407                                                          bytes_read,        // How many bytes to print?
408                                                          UINT32_MAX,        // num per line
409                                                          LLDB_INVALID_ADDRESS,// base address
410                                                          0,                 // bitfield bit size
411                                                          0);                // bitfield bit offset
412                                 sstr << '"';
413                             }
414                         }
415                         else
416                         {
417                             const size_t k_max_buf_size = 256;
418                             data_buffer.resize (k_max_buf_size + 1);
419                             // NULL terminate in case we don't get the entire C string
420                             data_buffer.back() = '\0';
421                             // Make a formatted buffer that can contain take 4
422                             // bytes per character in case each byte uses the
423                             // "\xXX" format and one extra byte for a NULL
424                             cstr_buffer.resize (k_max_buf_size * 4 + 1);
425 
426                             data.SetData (data_buffer.data(), data_buffer.size(), eByteOrderHost);
427                             size_t total_cstr_len = 0;
428                             while ((bytes_read = process->ReadMemory (cstr_address, data_buffer.data(), k_max_buf_size, error)) > 0)
429                             {
430                                 size_t len = strlen(data_buffer.data());
431                                 if (len == 0)
432                                     break;
433                                 if (len > bytes_read)
434                                     len = bytes_read;
435                                 if (sstr.GetSize() == 0)
436                                     sstr << '"';
437 
438                                 cstr_length = data.Dump (&sstr,
439                                                          0,                 // Start offset in "data"
440                                                          eFormatChar,       // Print as characters
441                                                          1,                 // Size of item (1 byte for a char!)
442                                                          len,               // How many bytes to print?
443                                                          UINT32_MAX,        // num per line
444                                                          LLDB_INVALID_ADDRESS,// base address
445                                                          0,                 // bitfield bit size
446                                                          0);                // bitfield bit offset
447 
448                                 if (len < k_max_buf_size)
449                                     break;
450                                 cstr_address += total_cstr_len;
451                             }
452                             if (sstr.GetSize() > 0)
453                                 sstr << '"';
454                         }
455 
456                         if (sstr.GetSize() > 0)
457                             m_summary_str.assign (sstr.GetData(), sstr.GetSize());
458                     }
459                 }
460             }
461         }
462     }
463     if (m_summary_str.empty())
464         return NULL;
465     return m_summary_str.c_str();
466 }
467 
468 
469 const char *
470 ValueObject::GetValueAsCString (ExecutionContextScope *exe_scope)
471 {
472     // If our byte size is zero this is an aggregate type that has children
473     if (ClangASTContext::IsAggregateType (GetOpaqueClangQualType()) == false)
474     {
475         if (UpdateValueIfNeeded(exe_scope))
476         {
477             if (m_value_str.empty())
478             {
479                 const Value::ContextType context_type = m_value.GetContextType();
480 
481                 switch (context_type)
482                 {
483                 case Value::eContextTypeOpaqueClangQualType:
484                 case Value::eContextTypeDCType:
485                 case Value::eContextTypeDCVariable:
486                     {
487                         void *clang_type = GetOpaqueClangQualType ();
488                         if (clang_type)
489                         {
490                             StreamString sstr;
491                             lldb::Format format = Type::GetFormat(clang_type);
492                             if (Type::DumpTypeValue(&sstr,
493                                                     GetClangAST(),            // The clang AST
494                                                     clang_type,               // The clang type to display
495                                                     format,                   // Format to display this type with
496                                                     m_data,                   // Data to extract from
497                                                     0,                        // Byte offset into "m_data"
498                                                     GetByteSize(),            // Byte size of item in "m_data"
499                                                     GetBitfieldBitSize(),     // Bitfield bit size
500                                                     GetBitfieldBitOffset()))  // Bitfield bit offset
501                                 m_value_str.swap(sstr.GetString());
502                             else
503                                 m_value_str.clear();
504                         }
505                     }
506                     break;
507 
508                 case Value::eContextTypeDCRegisterInfo:
509                     {
510                         const RegisterInfo *reg_info = m_value.GetRegisterInfo();
511                         if (reg_info)
512                         {
513                             StreamString reg_sstr;
514                             m_data.Dump(&reg_sstr, 0, reg_info->format, reg_info->byte_size, 1, UINT32_MAX, LLDB_INVALID_ADDRESS, 0, 0);
515                             m_value_str.swap(reg_sstr.GetString());
516                         }
517                     }
518                     break;
519                 }
520             }
521         }
522     }
523     if (m_value_str.empty())
524         return NULL;
525     return m_value_str.c_str();
526 }
527 
528 bool
529 ValueObject::SetValueFromCString (ExecutionContextScope *exe_scope, const char *value_str)
530 {
531     // Make sure our value is up to date first so that our location and location
532     // type is valid.
533     if (!UpdateValueIfNeeded(exe_scope))
534         return false;
535 
536     uint32_t count = 0;
537     lldb::Encoding encoding = Type::GetEncoding (GetOpaqueClangQualType(), count);
538 
539     char *end = NULL;
540     size_t byte_size = GetByteSize();
541     switch (encoding)
542     {
543     case eEncodingInvalid:
544         return false;
545 
546     case eEncodingUint:
547         if (byte_size > sizeof(unsigned long long))
548         {
549             return false;
550         }
551         else
552         {
553             unsigned long long ull_val = strtoull(value_str, &end, 0);
554             if (end && *end != '\0')
555                 return false;
556             m_value = ull_val;
557             // Limit the bytes in our m_data appropriately.
558             m_value.GetScalar().GetData (m_data, byte_size);
559         }
560         break;
561 
562     case eEncodingSint:
563         if (byte_size > sizeof(long long))
564         {
565             return false;
566         }
567         else
568         {
569             long long sll_val = strtoll(value_str, &end, 0);
570             if (end && *end != '\0')
571                 return false;
572             m_value = sll_val;
573             // Limit the bytes in our m_data appropriately.
574             m_value.GetScalar().GetData (m_data, byte_size);
575         }
576         break;
577 
578     case eEncodingIEEE754:
579         {
580             const size_t byte_size = GetByteSize();
581             const off_t byte_offset = GetByteOffset();
582             uint8_t *dst = (uint8_t *)m_data.PeekData(byte_offset, byte_size);
583             if (dst != NULL)
584             {
585                 // We are decoding a float into host byte order below, so make
586                 // sure m_data knows what it contains.
587                 m_data.SetByteOrder(eByteOrderHost);
588                 const size_t converted_byte_size = ClangASTContext::ConvertStringToFloatValue (
589                                                         GetClangAST(),
590                                                         GetOpaqueClangQualType(),
591                                                         value_str,
592                                                         dst,
593                                                         byte_size);
594 
595                 if (converted_byte_size == byte_size)
596                 {
597                 }
598             }
599         }
600         break;
601 
602     case eEncodingVector:
603         return false;
604 
605     default:
606         return false;
607     }
608 
609     // If we have made it here the value is in m_data and we should write it
610     // out to the target
611     return Write ();
612 }
613 
614 bool
615 ValueObject::Write ()
616 {
617     // Clear the update ID so the next time we try and read the value
618     // we try and read it again.
619     m_update_id = 0;
620 
621     // TODO: when Value has a method to write a value back, call it from here.
622     return false;
623 
624 }
625 
626 void
627 ValueObject::AddSyntheticChild (const ConstString &key, ValueObjectSP& valobj_sp)
628 {
629     m_synthetic_children[key] = valobj_sp;
630 }
631 
632 ValueObjectSP
633 ValueObject::GetSyntheticChild (const ConstString &key) const
634 {
635     ValueObjectSP synthetic_child_sp;
636     std::map<ConstString, ValueObjectSP>::const_iterator pos = m_synthetic_children.find (key);
637     if (pos != m_synthetic_children.end())
638         synthetic_child_sp = pos->second;
639     return synthetic_child_sp;
640 }
641 
642 bool
643 ValueObject::IsPointerType ()
644 {
645     return ClangASTContext::IsPointerType (GetOpaqueClangQualType());
646 }
647 
648 bool
649 ValueObject::IsPointerOrReferenceType ()
650 {
651     return ClangASTContext::IsPointerOrReferenceType(GetOpaqueClangQualType());
652 }
653 
654 ValueObjectSP
655 ValueObject::GetSyntheticArrayMemberFromPointer (int32_t index, bool can_create)
656 {
657     ValueObjectSP synthetic_child_sp;
658     if (IsPointerType ())
659     {
660         char index_str[64];
661         snprintf(index_str, sizeof(index_str), "[%i]", index);
662         ConstString index_const_str(index_str);
663         // Check if we have already created a synthetic array member in this
664         // valid object. If we have we will re-use it.
665         synthetic_child_sp = GetSyntheticChild (index_const_str);
666         if (!synthetic_child_sp)
667         {
668             // We haven't made a synthetic array member for INDEX yet, so
669             // lets make one and cache it for any future reference.
670             synthetic_child_sp = CreateChildAtIndex(0, true, index);
671 
672             // Cache the value if we got one back...
673             if (synthetic_child_sp)
674                 AddSyntheticChild(index_const_str, synthetic_child_sp);
675         }
676     }
677     return synthetic_child_sp;
678 }
679