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