1 //===-- SBValue.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/API/SBValue.h"
11 #include "lldb/API/SBStream.h"
12 
13 #include "lldb/Breakpoint/Watchpoint.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Scalar.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Core/StreamFile.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Core/ValueObjectConstResult.h"
23 #include "lldb/Symbol/Block.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/Type.h"
26 #include "lldb/Symbol/Variable.h"
27 #include "lldb/Symbol/VariableList.h"
28 #include "lldb/Target/ExecutionContext.h"
29 #include "lldb/Target/Process.h"
30 #include "lldb/Target/StackFrame.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Thread.h"
33 
34 #include "lldb/API/SBProcess.h"
35 #include "lldb/API/SBTarget.h"
36 #include "lldb/API/SBThread.h"
37 #include "lldb/API/SBFrame.h"
38 #include "lldb/API/SBDebugger.h"
39 
40 using namespace lldb;
41 using namespace lldb_private;
42 
43 SBValue::SBValue () :
44     m_opaque_sp ()
45 {
46 }
47 
48 SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
49     m_opaque_sp (value_sp)
50 {
51 }
52 
53 SBValue::SBValue(const SBValue &rhs) :
54     m_opaque_sp (rhs.m_opaque_sp)
55 {
56 }
57 
58 SBValue &
59 SBValue::operator = (const SBValue &rhs)
60 {
61     if (this != &rhs)
62         m_opaque_sp = rhs.m_opaque_sp;
63     return *this;
64 }
65 
66 SBValue::~SBValue()
67 {
68 }
69 
70 bool
71 SBValue::IsValid ()
72 {
73     // If this function ever changes to anything that does more than just
74     // check if the opaque shared pointer is non NULL, then we need to update
75     // all "if (m_opaque_sp)" code in this file.
76     return m_opaque_sp.get() != NULL;
77 }
78 
79 void
80 SBValue::Clear()
81 {
82     m_opaque_sp.reset();
83 }
84 
85 SBError
86 SBValue::GetError()
87 {
88     SBError sb_error;
89 
90     lldb::ValueObjectSP value_sp(GetSP());
91     if (value_sp)
92         sb_error.SetError(value_sp->GetError());
93     else
94         sb_error.SetErrorString("error: invalid value");
95 
96     return sb_error;
97 }
98 
99 user_id_t
100 SBValue::GetID()
101 {
102     lldb::ValueObjectSP value_sp(GetSP());
103     if (value_sp)
104         return value_sp->GetID();
105     return LLDB_INVALID_UID;
106 }
107 
108 const char *
109 SBValue::GetName()
110 {
111 
112     const char *name = NULL;
113     lldb::ValueObjectSP value_sp(GetSP());
114     if (value_sp)
115         name = value_sp->GetName().GetCString();
116 
117     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
118     if (log)
119     {
120         if (name)
121             log->Printf ("SBValue(%p)::GetName () => \"%s\"", value_sp.get(), name);
122         else
123             log->Printf ("SBValue(%p)::GetName () => NULL", value_sp.get());
124     }
125 
126     return name;
127 }
128 
129 const char *
130 SBValue::GetTypeName ()
131 {
132     const char *name = NULL;
133     lldb::ValueObjectSP value_sp(GetSP());
134     if (value_sp)
135         name = value_sp->GetTypeName().GetCString();
136     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
137     if (log)
138     {
139         if (name)
140             log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"", value_sp.get(), name);
141         else
142             log->Printf ("SBValue(%p)::GetTypeName () => NULL", value_sp.get());
143     }
144 
145     return name;
146 }
147 
148 size_t
149 SBValue::GetByteSize ()
150 {
151     size_t result = 0;
152 
153     lldb::ValueObjectSP value_sp(GetSP());
154     if (value_sp)
155         result = value_sp->GetByteSize();
156 
157     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
158     if (log)
159         log->Printf ("SBValue(%p)::GetByteSize () => %zu", value_sp.get(), result);
160 
161     return result;
162 }
163 
164 bool
165 SBValue::IsInScope ()
166 {
167     bool result = false;
168 
169     lldb::ValueObjectSP value_sp(GetSP());
170     if (value_sp)
171     {
172         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
173         if (target_sp)
174         {
175             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
176             result = value_sp->IsInScope ();
177         }
178     }
179 
180     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
181     if (log)
182         log->Printf ("SBValue(%p)::IsInScope () => %i", value_sp.get(), result);
183 
184     return result;
185 }
186 
187 const char *
188 SBValue::GetValue ()
189 {
190     const char *cstr = NULL;
191     lldb::ValueObjectSP value_sp(GetSP());
192     if (value_sp)
193     {
194         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
195         if (target_sp)
196         {
197             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
198             cstr = value_sp->GetValueAsCString ();
199         }
200     }
201     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
202     if (log)
203     {
204         if (cstr)
205             log->Printf ("SBValue(%p)::GetValue => \"%s\"", value_sp.get(), cstr);
206         else
207             log->Printf ("SBValue(%p)::GetValue => NULL", value_sp.get());
208     }
209 
210     return cstr;
211 }
212 
213 ValueType
214 SBValue::GetValueType ()
215 {
216     ValueType result = eValueTypeInvalid;
217     lldb::ValueObjectSP value_sp(GetSP());
218     if (value_sp)
219         result = value_sp->GetValueType();
220     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
221     if (log)
222     {
223         switch (result)
224         {
225         case eValueTypeInvalid:         log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid", value_sp.get()); break;
226         case eValueTypeVariableGlobal:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal", value_sp.get()); break;
227         case eValueTypeVariableStatic:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic", value_sp.get()); break;
228         case eValueTypeVariableArgument:log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument", value_sp.get()); break;
229         case eValueTypeVariableLocal:   log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal", value_sp.get()); break;
230         case eValueTypeRegister:        log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister", value_sp.get()); break;
231         case eValueTypeRegisterSet:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet", value_sp.get()); break;
232         case eValueTypeConstResult:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult", value_sp.get()); break;
233         default:     log->Printf ("SBValue(%p)::GetValueType () => %i ???", value_sp.get(), result); break;
234         }
235     }
236     return result;
237 }
238 
239 const char *
240 SBValue::GetObjectDescription ()
241 {
242     const char *cstr = NULL;
243     lldb::ValueObjectSP value_sp(GetSP());
244     if (value_sp)
245     {
246         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
247         if (target_sp)
248         {
249             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
250             cstr = value_sp->GetObjectDescription ();
251         }
252     }
253     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
254     if (log)
255     {
256         if (cstr)
257             log->Printf ("SBValue(%p)::GetObjectDescription => \"%s\"", value_sp.get(), cstr);
258         else
259             log->Printf ("SBValue(%p)::GetObjectDescription => NULL", value_sp.get());
260     }
261     return cstr;
262 }
263 
264 SBType
265 SBValue::GetType()
266 {
267     SBType sb_type;
268     lldb::ValueObjectSP value_sp(GetSP());
269     TypeImplSP type_sp;
270     if (value_sp)
271     {
272         type_sp.reset (new TypeImpl(ClangASTType (value_sp->GetClangAST(), value_sp->GetClangType())));
273         sb_type.SetSP(type_sp);
274     }
275     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
276     if (log)
277     {
278         if (type_sp)
279             log->Printf ("SBValue(%p)::GetType => SBType(%p)", value_sp.get(), type_sp.get());
280         else
281             log->Printf ("SBValue(%p)::GetType => NULL", value_sp.get());
282     }
283     return sb_type;
284 }
285 
286 bool
287 SBValue::GetValueDidChange ()
288 {
289     bool result = false;
290     lldb::ValueObjectSP value_sp(GetSP());
291     if (value_sp)
292     {
293         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
294         if (target_sp)
295         {
296             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
297             result = value_sp->GetValueDidChange ();
298         }
299     }
300     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
301     if (log)
302         log->Printf ("SBValue(%p)::GetValueDidChange => %i", value_sp.get(), result);
303 
304     return result;
305 }
306 
307 const char *
308 SBValue::GetSummary ()
309 {
310     const char *cstr = NULL;
311     lldb::ValueObjectSP value_sp(GetSP());
312     if (value_sp)
313     {
314         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
315         if (target_sp)
316         {
317             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
318             cstr = value_sp->GetSummaryAsCString();
319         }
320     }
321     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
322     if (log)
323     {
324         if (cstr)
325             log->Printf ("SBValue(%p)::GetSummary => \"%s\"", value_sp.get(), cstr);
326         else
327             log->Printf ("SBValue(%p)::GetSummary => NULL", value_sp.get());
328     }
329     return cstr;
330 }
331 
332 const char *
333 SBValue::GetLocation ()
334 {
335     const char *cstr = NULL;
336     lldb::ValueObjectSP value_sp(GetSP());
337     if (value_sp)
338     {
339         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
340         if (target_sp)
341         {
342             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
343             cstr = value_sp->GetLocationAsCString();
344         }
345     }
346     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
347     if (log)
348     {
349         if (cstr)
350             log->Printf ("SBValue(%p)::GetSummary => \"%s\"", value_sp.get(), cstr);
351         else
352             log->Printf ("SBValue(%p)::GetSummary => NULL", value_sp.get());
353     }
354     return cstr;
355 }
356 
357 bool
358 SBValue::SetValueFromCString (const char *value_str)
359 {
360     bool success = false;
361     lldb::ValueObjectSP value_sp(GetSP());
362     if (value_sp)
363     {
364         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
365         if (target_sp)
366         {
367             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
368             success = value_sp->SetValueFromCString (value_str);
369         }
370     }
371     return success;
372 }
373 
374 lldb::SBValue
375 SBValue::CreateChildAtOffset (const char *name, uint32_t offset, SBType type)
376 {
377     lldb::SBValue sb_value;
378     lldb::ValueObjectSP value_sp(GetSP());
379     lldb::ValueObjectSP new_value_sp;
380     if (value_sp)
381     {
382         TypeImplSP type_sp (type.GetSP());
383         if (type.IsValid())
384         {
385             sb_value = SBValue(value_sp->GetSyntheticChildAtOffset(offset, type_sp->GetClangASTType(), true));
386             new_value_sp = sb_value.GetSP();
387             if (new_value_sp)
388                 new_value_sp->SetName(ConstString(name));
389         }
390     }
391     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
392     if (log)
393     {
394         if (new_value_sp)
395             log->Printf ("SBValue(%p)::GetChildAtOffset => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
396         else
397             log->Printf ("SBValue(%p)::GetChildAtOffset => NULL", value_sp.get());
398     }
399     return sb_value;
400 }
401 
402 lldb::SBValue
403 SBValue::Cast (SBType type)
404 {
405     lldb::SBValue sb_value;
406     lldb::ValueObjectSP value_sp(GetSP());
407     TypeImplSP type_sp (type.GetSP());
408     if (value_sp && type_sp)
409         sb_value.SetSP(value_sp->Cast(type_sp->GetClangASTType()));
410     return sb_value;
411 }
412 
413 lldb::SBValue
414 SBValue::CreateValueFromExpression (const char *name, const char* expression)
415 {
416     lldb::SBValue sb_value;
417     lldb::ValueObjectSP value_sp(GetSP());
418     lldb::ValueObjectSP new_value_sp;
419     if (value_sp)
420     {
421         value_sp->GetUpdatePoint().GetTargetSP()->EvaluateExpression (expression,
422                                                                       value_sp->GetExecutionContextScope()->CalculateStackFrame(),
423                                                                       eExecutionPolicyOnlyWhenNeeded,
424                                                                       false, // coerce to id
425                                                                       true, // unwind on error
426                                                                       true, // keep in memory
427                                                                       eNoDynamicValues,
428                                                                       new_value_sp);
429         if (new_value_sp)
430         {
431             new_value_sp->SetName(ConstString(name));
432             sb_value.SetSP(new_value_sp);
433         }
434     }
435     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
436     if (log)
437     {
438         if (new_value_sp)
439             log->Printf ("SBValue(%p)::GetChildFromExpression => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
440         else
441             log->Printf ("SBValue(%p)::GetChildFromExpression => NULL", value_sp.get());
442     }
443     return sb_value;
444 }
445 
446 lldb::SBValue
447 SBValue::CreateValueFromAddress(const char* name, lldb::addr_t address, SBType sb_type)
448 {
449     lldb::SBValue sb_value;
450     lldb::ValueObjectSP value_sp(GetSP());
451     lldb::ValueObjectSP new_value_sp;
452     lldb::TypeImplSP type_impl_sp (sb_type.GetSP());
453     if (value_sp && type_impl_sp)
454     {
455         ClangASTType pointee_ast_type(type_impl_sp->GetASTContext(), type_impl_sp->GetClangASTType().GetPointerType ());
456         lldb::TypeImplSP pointee_type_impl_sp (new TypeImpl(pointee_ast_type));
457         if (pointee_type_impl_sp)
458         {
459 
460             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
461 
462             ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (value_sp->GetExecutionContextScope(),
463                                                                                pointee_type_impl_sp->GetASTContext(),
464                                                                                pointee_type_impl_sp->GetOpaqueQualType(),
465                                                                                ConstString(name),
466                                                                                buffer,
467                                                                                lldb::endian::InlHostByteOrder(),
468                                                                                GetTarget().GetProcess().GetAddressByteSize()));
469 
470             if (ptr_result_valobj_sp)
471             {
472                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
473                 Error err;
474                 new_value_sp = ptr_result_valobj_sp->Dereference(err);
475                 if (new_value_sp)
476                     new_value_sp->SetName(ConstString(name));
477             }
478             sb_value.SetSP(new_value_sp);
479         }
480     }
481     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
482     if (log)
483     {
484         if (new_value_sp)
485             log->Printf ("SBValue(%p)::CreateValueFromAddress => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
486         else
487             log->Printf ("SBValue(%p)::CreateValueFromAddress => NULL", value_sp.get());
488     }
489     return sb_value;
490 }
491 
492 lldb::SBValue
493 SBValue::CreateValueFromData (const char* name, SBData data, SBType type)
494 {
495     lldb::SBValue sb_value;
496     lldb::ValueObjectSP new_value_sp;
497     lldb::ValueObjectSP value_sp(GetSP());
498     if (value_sp)
499     {
500         new_value_sp = ValueObjectConstResult::Create (value_sp->GetExecutionContextScope(),
501                                                        type.m_opaque_sp->GetASTContext() ,
502                                                        type.m_opaque_sp->GetOpaqueQualType(),
503                                                        ConstString(name),
504                                                        *data.m_opaque_sp,
505                                                        LLDB_INVALID_ADDRESS);
506         new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
507         sb_value.SetSP(new_value_sp);
508     }
509     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
510     if (log)
511     {
512         if (new_value_sp)
513             log->Printf ("SBValue(%p)::GetChildFromExpression => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
514         else
515             log->Printf ("SBValue(%p)::GetChildFromExpression => NULL", value_sp.get());
516     }
517     return sb_value;
518 }
519 
520 SBValue
521 SBValue::GetChildAtIndex (uint32_t idx)
522 {
523     const bool can_create_synthetic = false;
524     lldb::DynamicValueType use_dynamic = eNoDynamicValues;
525     lldb::ValueObjectSP value_sp(GetSP());
526     if (value_sp)
527         use_dynamic = value_sp->GetUpdatePoint().GetTargetSP()->GetPreferDynamicValue();
528     return GetChildAtIndex (idx, use_dynamic, can_create_synthetic);
529 }
530 
531 SBValue
532 SBValue::GetChildAtIndex (uint32_t idx, lldb::DynamicValueType use_dynamic, bool can_create_synthetic)
533 {
534     lldb::ValueObjectSP child_sp;
535 
536     lldb::ValueObjectSP value_sp(GetSP());
537     if (value_sp)
538     {
539         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
540         if (target_sp)
541         {
542             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
543             const bool can_create = true;
544             child_sp = value_sp->GetChildAtIndex (idx, can_create);
545             if (can_create_synthetic && !child_sp)
546             {
547                 if (value_sp->IsPointerType())
548                 {
549                     child_sp = value_sp->GetSyntheticArrayMemberFromPointer(idx, can_create);
550                 }
551                 else if (value_sp->IsArrayType())
552                 {
553                     child_sp = value_sp->GetSyntheticArrayMemberFromArray(idx, can_create);
554                 }
555             }
556 
557             if (child_sp)
558             {
559                 if (use_dynamic != lldb::eNoDynamicValues)
560                 {
561                     lldb::ValueObjectSP dynamic_sp(child_sp->GetDynamicValue (use_dynamic));
562                     if (dynamic_sp)
563                         child_sp = dynamic_sp;
564                 }
565             }
566         }
567     }
568 
569     SBValue sb_value (child_sp);
570     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
571     if (log)
572         log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)", value_sp.get(), idx, value_sp.get());
573 
574     return sb_value;
575 }
576 
577 uint32_t
578 SBValue::GetIndexOfChildWithName (const char *name)
579 {
580     uint32_t idx = UINT32_MAX;
581     lldb::ValueObjectSP value_sp(GetSP());
582     if (value_sp)
583     {
584         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
585         if (target_sp)
586         {
587             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
588 
589             idx = value_sp->GetIndexOfChildWithName (ConstString(name));
590         }
591     }
592     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
593     if (log)
594     {
595         if (idx == UINT32_MAX)
596             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND", value_sp.get(), name);
597         else
598             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u", value_sp.get(), name, idx);
599     }
600     return idx;
601 }
602 
603 SBValue
604 SBValue::GetChildMemberWithName (const char *name)
605 {
606     lldb::ValueObjectSP value_sp(GetSP());
607     if (value_sp)
608     {
609         lldb::DynamicValueType use_dynamic_value = eNoDynamicValues;
610         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
611         if (target_sp)
612         {
613             Mutex::Locker api_locker (target_sp->GetAPIMutex());
614             use_dynamic_value = target_sp->GetPreferDynamicValue();
615         }
616         return GetChildMemberWithName (name, use_dynamic_value);
617     }
618     return SBValue();
619 }
620 
621 SBValue
622 SBValue::GetChildMemberWithName (const char *name, lldb::DynamicValueType use_dynamic_value)
623 {
624     lldb::ValueObjectSP child_sp;
625     const ConstString str_name (name);
626 
627 
628     lldb::ValueObjectSP value_sp(GetSP());
629     if (value_sp)
630     {
631         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
632         if (target_sp)
633         {
634             Mutex::Locker api_locker (target_sp->GetAPIMutex());
635             child_sp = value_sp->GetChildMemberWithName (str_name, true);
636             if (use_dynamic_value != lldb::eNoDynamicValues)
637             {
638                 if (child_sp)
639                 {
640                     lldb::ValueObjectSP dynamic_sp = child_sp->GetDynamicValue (use_dynamic_value);
641                     if (dynamic_sp)
642                         child_sp = dynamic_sp;
643                 }
644             }
645         }
646     }
647 
648     SBValue sb_value (child_sp);
649 
650     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
651     if (log)
652         log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)", value_sp.get(), name, value_sp.get());
653 
654     return sb_value;
655 }
656 
657 lldb::SBValue
658 SBValue::GetDynamicValue (lldb::DynamicValueType use_dynamic)
659 {
660     lldb::ValueObjectSP value_sp(GetSP());
661     if (value_sp)
662     {
663         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
664         if (target_sp)
665         {
666             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
667             return SBValue (value_sp->GetDynamicValue(use_dynamic));
668         }
669     }
670 
671     return SBValue();
672 }
673 
674 lldb::SBValue
675 SBValue::GetStaticValue ()
676 {
677     lldb::ValueObjectSP value_sp(GetSP());
678     if (value_sp)
679     {
680         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
681         if (target_sp)
682         {
683             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
684             return SBValue(value_sp->GetStaticValue());
685         }
686     }
687 
688     return SBValue();
689 }
690 
691 bool
692 SBValue::IsDynamic()
693 {
694     lldb::ValueObjectSP value_sp(GetSP());
695     if (value_sp)
696     {
697         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
698         if (target_sp)
699         {
700             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
701             return value_sp->IsDynamic();
702         }
703     }
704     return false;
705 }
706 
707 lldb::SBValue
708 SBValue::GetValueForExpressionPath(const char* expr_path)
709 {
710     lldb::ValueObjectSP child_sp;
711     lldb::ValueObjectSP value_sp(GetSP());
712     if (value_sp)
713     {
714         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
715         if (target_sp)
716         {
717             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
718             // using default values for all the fancy options, just do it if you can
719             child_sp = value_sp->GetValueForExpressionPath(expr_path);
720         }
721     }
722 
723     SBValue sb_value (child_sp);
724 
725     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
726     if (log)
727         log->Printf ("SBValue(%p)::GetValueForExpressionPath (expr_path=\"%s\") => SBValue(%p)", value_sp.get(), expr_path, value_sp.get());
728 
729     return sb_value;
730 }
731 
732 int64_t
733 SBValue::GetValueAsSigned(SBError& error, int64_t fail_value)
734 {
735     error.Clear();
736     lldb::ValueObjectSP value_sp(GetSP());
737     if (value_sp)
738     {
739         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
740         if (target_sp)
741         {
742             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
743             Scalar scalar;
744             if (value_sp->ResolveValue (scalar))
745                 return scalar.GetRawBits64(fail_value);
746             else
747                 error.SetErrorString("could not get value");
748         }
749         else
750             error.SetErrorString("could not get target");
751     }
752     error.SetErrorString("invalid SBValue");
753     return fail_value;
754 }
755 
756 uint64_t
757 SBValue::GetValueAsUnsigned(SBError& error, uint64_t fail_value)
758 {
759     error.Clear();
760     lldb::ValueObjectSP value_sp(GetSP());
761     if (value_sp)
762     {
763         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
764         if (target_sp)
765         {
766             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
767             Scalar scalar;
768             if (value_sp->ResolveValue (scalar))
769                 return scalar.GetRawBits64(fail_value);
770             else
771                 error.SetErrorString("could not get value");
772         }
773         else
774             error.SetErrorString("could not get target");
775     }
776     error.SetErrorString("invalid SBValue");
777     return fail_value;
778 }
779 
780 int64_t
781 SBValue::GetValueAsSigned(int64_t fail_value)
782 {
783     lldb::ValueObjectSP value_sp(GetSP());
784     if (value_sp)
785     {
786         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
787         if (target_sp)
788         {
789             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
790             Scalar scalar;
791             if (value_sp->ResolveValue (scalar))
792                 return scalar.GetRawBits64(fail_value);
793         }
794     }
795     return fail_value;
796 }
797 
798 uint64_t
799 SBValue::GetValueAsUnsigned(uint64_t fail_value)
800 {
801     lldb::ValueObjectSP value_sp(GetSP());
802     if (value_sp)
803     {
804         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
805         if (target_sp)
806         {
807             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
808             Scalar scalar;
809             if (value_sp->ResolveValue (scalar))
810                 return scalar.GetRawBits64(fail_value);
811         }
812     }
813     return fail_value;
814 }
815 
816 uint32_t
817 SBValue::GetNumChildren ()
818 {
819     uint32_t num_children = 0;
820 
821     lldb::ValueObjectSP value_sp(GetSP());
822     if (value_sp)
823     {
824         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
825         if (target_sp)
826         {
827             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
828 
829             num_children = value_sp->GetNumChildren();
830         }
831     }
832 
833     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
834     if (log)
835         log->Printf ("SBValue(%p)::GetNumChildren () => %u", value_sp.get(), num_children);
836 
837     return num_children;
838 }
839 
840 
841 SBValue
842 SBValue::Dereference ()
843 {
844     SBValue sb_value;
845     lldb::ValueObjectSP value_sp(GetSP());
846     if (value_sp)
847     {
848         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
849         if (target_sp)
850         {
851             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
852 
853             Error error;
854             sb_value = value_sp->Dereference (error);
855         }
856     }
857     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
858     if (log)
859         log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)", value_sp.get(), value_sp.get());
860 
861     return sb_value;
862 }
863 
864 bool
865 SBValue::TypeIsPointerType ()
866 {
867     bool is_ptr_type = false;
868 
869     lldb::ValueObjectSP value_sp(GetSP());
870     if (value_sp)
871     {
872         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
873         if (target_sp)
874         {
875             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
876 
877             is_ptr_type = value_sp->IsPointerType();
878         }
879     }
880 
881     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
882     if (log)
883         log->Printf ("SBValue(%p)::TypeIsPointerType () => %i", value_sp.get(), is_ptr_type);
884 
885 
886     return is_ptr_type;
887 }
888 
889 void *
890 SBValue::GetOpaqueType()
891 {
892     lldb::ValueObjectSP value_sp(GetSP());
893     if (value_sp)
894     {
895         TargetSP target_sp(value_sp->GetUpdatePoint().GetTargetSP());
896         if (target_sp)
897         {
898             Mutex::Locker api_locker (value_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
899 
900             return value_sp->GetClangType();
901         }
902     }
903     return NULL;
904 }
905 
906 lldb::SBTarget
907 SBValue::GetTarget()
908 {
909     SBTarget sb_target;
910     TargetSP target_sp;
911     lldb::ValueObjectSP value_sp(GetSP());
912     if (value_sp)
913     {
914         target_sp = value_sp->GetUpdatePoint().GetTargetSP();
915         sb_target.SetSP (target_sp);
916     }
917     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
918     if (log)
919     {
920         if (target_sp.get() == NULL)
921             log->Printf ("SBValue(%p)::GetTarget () => NULL", value_sp.get());
922         else
923             log->Printf ("SBValue(%p)::GetTarget () => %p", value_sp.get(), target_sp.get());
924     }
925     return sb_target;
926 }
927 
928 lldb::SBProcess
929 SBValue::GetProcess()
930 {
931     SBProcess sb_process;
932     ProcessSP process_sp;
933     lldb::ValueObjectSP value_sp(GetSP());
934     if (value_sp)
935     {
936         process_sp = value_sp->GetUpdatePoint().GetProcessSP();
937         if (process_sp)
938             sb_process.SetSP (process_sp);
939     }
940     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
941     if (log)
942     {
943         if (process_sp.get() == NULL)
944             log->Printf ("SBValue(%p)::GetProcess () => NULL", value_sp.get());
945         else
946             log->Printf ("SBValue(%p)::GetProcess () => %p", value_sp.get(), process_sp.get());
947     }
948     return sb_process;
949 }
950 
951 lldb::SBThread
952 SBValue::GetThread()
953 {
954     SBThread sb_thread;
955     ThreadSP thread_sp;
956     lldb::ValueObjectSP value_sp(GetSP());
957     if (value_sp)
958     {
959         if (value_sp->GetExecutionContextScope())
960         {
961             thread_sp = value_sp->GetExecutionContextScope()->CalculateThread()->shared_from_this();
962             sb_thread.SetThread(thread_sp);
963         }
964     }
965     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
966     if (log)
967     {
968         if (thread_sp.get() == NULL)
969             log->Printf ("SBValue(%p)::GetThread () => NULL", value_sp.get());
970         else
971             log->Printf ("SBValue(%p)::GetThread () => %p", value_sp.get(), thread_sp.get());
972     }
973     return sb_thread;
974 }
975 
976 lldb::SBFrame
977 SBValue::GetFrame()
978 {
979     SBFrame sb_frame;
980     StackFrameSP frame_sp;
981     lldb::ValueObjectSP value_sp(GetSP());
982     if (value_sp)
983     {
984         if (value_sp->GetExecutionContextScope())
985         {
986             frame_sp = value_sp->GetExecutionContextScope()->CalculateStackFrame()->shared_from_this();
987             sb_frame.SetFrameSP (frame_sp);
988         }
989     }
990     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
991     if (log)
992     {
993         if (frame_sp.get() == NULL)
994             log->Printf ("SBValue(%p)::GetFrame () => NULL", value_sp.get());
995         else
996             log->Printf ("SBValue(%p)::GetFrame () => %p", value_sp.get(), frame_sp.get());
997     }
998     return sb_frame;
999 }
1000 
1001 
1002 lldb::ValueObjectSP
1003 SBValue::GetSP () const
1004 {
1005     return m_opaque_sp;
1006 }
1007 
1008 void
1009 SBValue::SetSP (const lldb::ValueObjectSP &sp)
1010 {
1011     m_opaque_sp = sp;
1012 }
1013 
1014 
1015 bool
1016 SBValue::GetExpressionPath (SBStream &description)
1017 {
1018     lldb::ValueObjectSP value_sp(GetSP());
1019     if (value_sp)
1020     {
1021         value_sp->GetExpressionPath (description.ref(), false);
1022         return true;
1023     }
1024     return false;
1025 }
1026 
1027 bool
1028 SBValue::GetExpressionPath (SBStream &description, bool qualify_cxx_base_classes)
1029 {
1030     lldb::ValueObjectSP value_sp(GetSP());
1031     if (value_sp)
1032     {
1033         value_sp->GetExpressionPath (description.ref(), qualify_cxx_base_classes);
1034         return true;
1035     }
1036     return false;
1037 }
1038 
1039 bool
1040 SBValue::GetDescription (SBStream &description)
1041 {
1042     Stream &strm = description.ref();
1043 
1044     lldb::ValueObjectSP value_sp(GetSP());
1045     if (value_sp)
1046     {
1047         ValueObject::DumpValueObject (strm, value_sp.get());
1048     }
1049     else
1050         strm.PutCString ("No value");
1051 
1052     return true;
1053 }
1054 
1055 lldb::Format
1056 SBValue::GetFormat ()
1057 {
1058     lldb::ValueObjectSP value_sp(GetSP());
1059     if (value_sp)
1060         return value_sp->GetFormat();
1061     return eFormatDefault;
1062 }
1063 
1064 void
1065 SBValue::SetFormat (lldb::Format format)
1066 {
1067     lldb::ValueObjectSP value_sp(GetSP());
1068     if (value_sp)
1069         value_sp->SetFormat(format);
1070 }
1071 
1072 lldb::SBValue
1073 SBValue::AddressOf()
1074 {
1075     SBValue sb_value;
1076     lldb::ValueObjectSP value_sp(GetSP());
1077     if (value_sp)
1078     {
1079         Target* target = value_sp->GetUpdatePoint().GetTargetSP().get();
1080         if (target)
1081         {
1082             Mutex::Locker api_locker (target->GetAPIMutex());
1083             Error error;
1084             sb_value = value_sp->AddressOf (error);
1085         }
1086     }
1087     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1088     if (log)
1089         log->Printf ("SBValue(%p)::GetPointerToObject () => SBValue(%p)", value_sp.get(), value_sp.get());
1090 
1091     return sb_value;
1092 }
1093 
1094 lldb::addr_t
1095 SBValue::GetLoadAddress()
1096 {
1097     lldb::addr_t value = LLDB_INVALID_ADDRESS;
1098     lldb::ValueObjectSP value_sp(GetSP());
1099     if (value_sp)
1100     {
1101         Target* target = value_sp->GetUpdatePoint().GetTargetSP().get();
1102         if (target)
1103         {
1104             Mutex::Locker api_locker (target->GetAPIMutex());
1105             const bool scalar_is_load_address = true;
1106             AddressType addr_type;
1107             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1108             if (addr_type == eAddressTypeFile)
1109             {
1110                 Module* module = value_sp->GetModule();
1111                 if (!module)
1112                     value = LLDB_INVALID_ADDRESS;
1113                 else
1114                 {
1115                     Address addr;
1116                     module->ResolveFileAddress(value, addr);
1117                     value = addr.GetLoadAddress(value_sp->GetUpdatePoint().GetTargetSP().get());
1118                 }
1119             }
1120             else if (addr_type == eAddressTypeHost || addr_type == eAddressTypeInvalid)
1121                 value = LLDB_INVALID_ADDRESS;
1122         }
1123     }
1124     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1125     if (log)
1126         log->Printf ("SBValue(%p)::GetLoadAddress () => (%llu)", value_sp.get(), value);
1127 
1128     return value;
1129 }
1130 
1131 lldb::SBAddress
1132 SBValue::GetAddress()
1133 {
1134     Address addr;
1135     lldb::ValueObjectSP value_sp(GetSP());
1136     if (value_sp)
1137     {
1138         Target* target = value_sp->GetUpdatePoint().GetTargetSP().get();
1139         if (target)
1140         {
1141             lldb::addr_t value = LLDB_INVALID_ADDRESS;
1142             Mutex::Locker api_locker (target->GetAPIMutex());
1143             const bool scalar_is_load_address = true;
1144             AddressType addr_type;
1145             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1146             if (addr_type == eAddressTypeFile)
1147             {
1148                 Module* module = value_sp->GetModule();
1149                 if (module)
1150                     module->ResolveFileAddress(value, addr);
1151             }
1152             else if (addr_type == eAddressTypeLoad)
1153             {
1154                 // no need to check the return value on this.. if it can actually do the resolve
1155                 // addr will be in the form (section,offset), otherwise it will simply be returned
1156                 // as (NULL, value)
1157                 addr.SetLoadAddress(value, target);
1158             }
1159         }
1160     }
1161     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1162     if (log)
1163         log->Printf ("SBValue(%p)::GetAddress () => (%s,%llu)", value_sp.get(), (addr.GetSection() ? addr.GetSection()->GetName().GetCString() : "NULL"), addr.GetOffset());
1164     return SBAddress(new Address(addr));
1165 }
1166 
1167 lldb::SBData
1168 SBValue::GetPointeeData (uint32_t item_idx,
1169                          uint32_t item_count)
1170 {
1171     lldb::SBData sb_data;
1172     lldb::ValueObjectSP value_sp(GetSP());
1173     if (value_sp)
1174     {
1175         Target* target = value_sp->GetUpdatePoint().GetTargetSP().get();
1176         if (target)
1177         {
1178 			DataExtractorSP data_sp(new DataExtractor());
1179             Mutex::Locker api_locker (target->GetAPIMutex());
1180             value_sp->GetPointeeData(*data_sp, item_idx, item_count);
1181             if (data_sp->GetByteSize() > 0)
1182                 *sb_data = data_sp;
1183         }
1184     }
1185     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1186     if (log)
1187         log->Printf ("SBValue(%p)::GetPointeeData (%d, %d) => SBData(%p)",
1188                      value_sp.get(),
1189                      item_idx,
1190                      item_count,
1191                      sb_data.get());
1192 
1193     return sb_data;
1194 }
1195 
1196 lldb::SBData
1197 SBValue::GetData ()
1198 {
1199     lldb::SBData sb_data;
1200     lldb::ValueObjectSP value_sp(GetSP());
1201     if (value_sp)
1202     {
1203         TargetSP target_sp (value_sp->GetUpdatePoint().GetTargetSP());
1204         if (target_sp)
1205         {
1206             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1207 			DataExtractorSP data_sp(new DataExtractor());
1208             value_sp->GetData(*data_sp);
1209             if (data_sp->GetByteSize() > 0)
1210                 *sb_data = data_sp;
1211         }
1212     }
1213     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1214     if (log)
1215         log->Printf ("SBValue(%p)::GetData () => SBData(%p)",
1216                      value_sp.get(),
1217                      sb_data.get());
1218 
1219     return sb_data;
1220 }
1221 
1222 lldb::SBWatchpoint
1223 SBValue::Watch (bool resolve_location, bool read, bool write)
1224 {
1225     SBWatchpoint sb_watchpoint;
1226 
1227     // If the SBValue is not valid, there's no point in even trying to watch it.
1228     lldb::ValueObjectSP value_sp(GetSP());
1229     TargetSP target_sp (GetTarget().GetSP());
1230     if (value_sp && target_sp)
1231     {
1232         // Read and Write cannot both be false.
1233         if (!read && !write)
1234             return sb_watchpoint;
1235 
1236         // If the value is not in scope, don't try and watch and invalid value
1237         if (!IsInScope())
1238             return sb_watchpoint;
1239 
1240         addr_t addr = GetLoadAddress();
1241         if (addr == LLDB_INVALID_ADDRESS)
1242             return sb_watchpoint;
1243         size_t byte_size = GetByteSize();
1244         if (byte_size == 0)
1245             return sb_watchpoint;
1246 
1247         uint32_t watch_type = 0;
1248         if (read)
1249             watch_type |= LLDB_WATCH_TYPE_READ;
1250         if (write)
1251             watch_type |= LLDB_WATCH_TYPE_WRITE;
1252 
1253         WatchpointSP watchpoint_sp = target_sp->CreateWatchpoint(addr, byte_size, watch_type);
1254 
1255         if (watchpoint_sp)
1256         {
1257             sb_watchpoint.SetSP (watchpoint_sp);
1258             Declaration decl;
1259             if (value_sp->GetDeclaration (decl))
1260             {
1261                 if (decl.GetFile())
1262                 {
1263                     StreamString ss;
1264                     // True to show fullpath for declaration file.
1265                     decl.DumpStopContext(&ss, true);
1266                     watchpoint_sp->SetDeclInfo(ss.GetString());
1267                 }
1268             }
1269         }
1270     }
1271     return sb_watchpoint;
1272 }
1273 
1274 lldb::SBWatchpoint
1275 SBValue::WatchPointee (bool resolve_location, bool read, bool write)
1276 {
1277     SBWatchpoint sb_watchpoint;
1278     if (IsInScope() && GetType().IsPointerType())
1279         sb_watchpoint = Dereference().Watch (resolve_location, read, write);
1280     return sb_watchpoint;
1281 }
1282 
1283