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/lldb-python.h"
11 
12 #include "lldb/API/SBValue.h"
13 
14 #include "lldb/API/SBDeclaration.h"
15 #include "lldb/API/SBStream.h"
16 #include "lldb/API/SBTypeFilter.h"
17 #include "lldb/API/SBTypeFormat.h"
18 #include "lldb/API/SBTypeSummary.h"
19 #include "lldb/API/SBTypeSynthetic.h"
20 
21 #include "lldb/Breakpoint/Watchpoint.h"
22 #include "lldb/Core/DataExtractor.h"
23 #include "lldb/Core/Log.h"
24 #include "lldb/Core/Module.h"
25 #include "lldb/Core/Scalar.h"
26 #include "lldb/Core/Section.h"
27 #include "lldb/Core/Stream.h"
28 #include "lldb/Core/StreamFile.h"
29 #include "lldb/Core/Value.h"
30 #include "lldb/Core/ValueObject.h"
31 #include "lldb/Core/ValueObjectConstResult.h"
32 #include "lldb/DataFormatters/DataVisualization.h"
33 #include "lldb/Symbol/Block.h"
34 #include "lldb/Symbol/Declaration.h"
35 #include "lldb/Symbol/ObjectFile.h"
36 #include "lldb/Symbol/Type.h"
37 #include "lldb/Symbol/Variable.h"
38 #include "lldb/Symbol/VariableList.h"
39 #include "lldb/Target/ExecutionContext.h"
40 #include "lldb/Target/Process.h"
41 #include "lldb/Target/StackFrame.h"
42 #include "lldb/Target/Target.h"
43 #include "lldb/Target/Thread.h"
44 
45 #include "lldb/API/SBDebugger.h"
46 #include "lldb/API/SBExpressionOptions.h"
47 #include "lldb/API/SBFrame.h"
48 #include "lldb/API/SBProcess.h"
49 #include "lldb/API/SBTarget.h"
50 #include "lldb/API/SBThread.h"
51 
52 using namespace lldb;
53 using namespace lldb_private;
54 
55 class ValueImpl
56 {
57 public:
58     ValueImpl ()
59     {
60     }
61 
62     ValueImpl (lldb::ValueObjectSP in_valobj_sp,
63                lldb::DynamicValueType use_dynamic,
64                bool use_synthetic,
65                const char *name = NULL) :
66     m_valobj_sp(in_valobj_sp),
67     m_use_dynamic(use_dynamic),
68     m_use_synthetic(use_synthetic),
69     m_name (name)
70     {
71         if (!m_name.IsEmpty() && m_valobj_sp)
72             m_valobj_sp->SetName(m_name);
73     }
74 
75     ValueImpl (const ValueImpl& rhs) :
76     m_valobj_sp(rhs.m_valobj_sp),
77     m_use_dynamic(rhs.m_use_dynamic),
78     m_use_synthetic(rhs.m_use_synthetic),
79     m_name (rhs.m_name)
80     {
81     }
82 
83     ValueImpl &
84     operator = (const ValueImpl &rhs)
85     {
86         if (this != &rhs)
87         {
88             m_valobj_sp = rhs.m_valobj_sp;
89             m_use_dynamic = rhs.m_use_dynamic;
90             m_use_synthetic = rhs.m_use_synthetic;
91             m_name = rhs.m_name;
92         }
93         return *this;
94     }
95 
96     bool
97     IsValid ()
98     {
99         if (m_valobj_sp.get() == NULL)
100             return false;
101         else
102         {
103             // FIXME: This check is necessary but not sufficient.  We for sure don't want to touch SBValues whose owning
104             // targets have gone away.  This check is a little weak in that it enforces that restriction when you call
105             // IsValid, but since IsValid doesn't lock the target, you have no guarantee that the SBValue won't go
106             // invalid after you call this...
107             // Also, an SBValue could depend on data from one of the modules in the target, and those could go away
108             // independently of the target, for instance if a module is unloaded.  But right now, neither SBValues
109             // nor ValueObjects know which modules they depend on.  So I have no good way to make that check without
110             // tracking that in all the ValueObject subclasses.
111             TargetSP target_sp = m_valobj_sp->GetTargetSP();
112             if (target_sp && target_sp->IsValid())
113                 return true;
114             else
115                 return false;
116         }
117     }
118 
119     lldb::ValueObjectSP
120     GetRootSP ()
121     {
122         return m_valobj_sp;
123     }
124 
125     lldb::ValueObjectSP
126     GetSP (Process::StopLocker &stop_locker, Mutex::Locker &api_locker, Error &error)
127     {
128         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
129         if (!m_valobj_sp)
130         {
131             error.SetErrorString("invalid value object");
132             return m_valobj_sp;
133         }
134 
135         lldb::ValueObjectSP value_sp = m_valobj_sp;
136 
137         Target *target = value_sp->GetTargetSP().get();
138         if (target)
139             api_locker.Lock(target->GetAPIMutex());
140         else
141             return ValueObjectSP();
142 
143         ProcessSP process_sp(value_sp->GetProcessSP());
144         if (process_sp && !stop_locker.TryLock (&process_sp->GetRunLock()))
145         {
146             // We don't allow people to play around with ValueObject if the process is running.
147             // If you want to look at values, pause the process, then look.
148             if (log)
149                 log->Printf ("SBValue(%p)::GetSP() => error: process is running",
150                              static_cast<void*>(value_sp.get()));
151             error.SetErrorString ("process must be stopped.");
152             return ValueObjectSP();
153         }
154 
155         if (value_sp->GetDynamicValue(m_use_dynamic))
156             value_sp = value_sp->GetDynamicValue(m_use_dynamic);
157         if (value_sp->GetSyntheticValue(m_use_synthetic))
158             value_sp = value_sp->GetSyntheticValue(m_use_synthetic);
159         if (!value_sp)
160             error.SetErrorString("invalid value object");
161         if (!m_name.IsEmpty())
162             value_sp->SetName(m_name);
163 
164         return value_sp;
165     }
166 
167     void
168     SetUseDynamic (lldb::DynamicValueType use_dynamic)
169     {
170         m_use_dynamic = use_dynamic;
171     }
172 
173     void
174     SetUseSynthetic (bool use_synthetic)
175     {
176         m_use_synthetic = use_synthetic;
177     }
178 
179     lldb::DynamicValueType
180     GetUseDynamic ()
181     {
182         return m_use_dynamic;
183     }
184 
185     bool
186     GetUseSynthetic ()
187     {
188         return m_use_synthetic;
189     }
190 
191     // All the derived values that we would make from the m_valobj_sp will share
192     // the ExecutionContext with m_valobj_sp, so we don't need to do the calculations
193     // in GetSP to return the Target, Process, Thread or Frame.  It is convenient to
194     // provide simple accessors for these, which I do here.
195     TargetSP
196     GetTargetSP ()
197     {
198         if (m_valobj_sp)
199             return m_valobj_sp->GetTargetSP();
200         else
201             return TargetSP();
202     }
203 
204     ProcessSP
205     GetProcessSP ()
206     {
207         if (m_valobj_sp)
208             return m_valobj_sp->GetProcessSP();
209         else
210             return ProcessSP();
211     }
212 
213     ThreadSP
214     GetThreadSP ()
215     {
216         if (m_valobj_sp)
217             return m_valobj_sp->GetThreadSP();
218         else
219             return ThreadSP();
220     }
221 
222     StackFrameSP
223     GetFrameSP ()
224     {
225         if (m_valobj_sp)
226             return m_valobj_sp->GetFrameSP();
227         else
228             return StackFrameSP();
229     }
230 
231 private:
232     lldb::ValueObjectSP m_valobj_sp;
233     lldb::DynamicValueType m_use_dynamic;
234     bool m_use_synthetic;
235     ConstString m_name;
236 };
237 
238 class ValueLocker
239 {
240 public:
241     ValueLocker ()
242     {
243     }
244 
245     ValueObjectSP
246     GetLockedSP(ValueImpl &in_value)
247     {
248         return in_value.GetSP(m_stop_locker, m_api_locker, m_lock_error);
249     }
250 
251     Error &
252     GetError()
253     {
254         return m_lock_error;
255     }
256 
257 private:
258     Process::StopLocker m_stop_locker;
259     Mutex::Locker m_api_locker;
260     Error m_lock_error;
261 
262 };
263 
264 SBValue::SBValue () :
265 m_opaque_sp ()
266 {
267 }
268 
269 SBValue::SBValue (const lldb::ValueObjectSP &value_sp)
270 {
271     SetSP(value_sp);
272 }
273 
274 SBValue::SBValue(const SBValue &rhs)
275 {
276     SetSP(rhs.m_opaque_sp);
277 }
278 
279 SBValue &
280 SBValue::operator = (const SBValue &rhs)
281 {
282     if (this != &rhs)
283     {
284         SetSP(rhs.m_opaque_sp);
285     }
286     return *this;
287 }
288 
289 SBValue::~SBValue()
290 {
291 }
292 
293 bool
294 SBValue::IsValid ()
295 {
296     // If this function ever changes to anything that does more than just
297     // check if the opaque shared pointer is non NULL, then we need to update
298     // all "if (m_opaque_sp)" code in this file.
299     return m_opaque_sp.get() != NULL && m_opaque_sp->IsValid() && m_opaque_sp->GetRootSP().get() != NULL;
300 }
301 
302 void
303 SBValue::Clear()
304 {
305     m_opaque_sp.reset();
306 }
307 
308 SBError
309 SBValue::GetError()
310 {
311     SBError sb_error;
312 
313     ValueLocker locker;
314     lldb::ValueObjectSP value_sp(GetSP(locker));
315     if (value_sp)
316         sb_error.SetError(value_sp->GetError());
317     else
318         sb_error.SetErrorStringWithFormat ("error: %s", locker.GetError().AsCString());
319 
320     return sb_error;
321 }
322 
323 user_id_t
324 SBValue::GetID()
325 {
326     ValueLocker locker;
327     lldb::ValueObjectSP value_sp(GetSP(locker));
328     if (value_sp)
329         return value_sp->GetID();
330     return LLDB_INVALID_UID;
331 }
332 
333 const char *
334 SBValue::GetName()
335 {
336     const char *name = NULL;
337     ValueLocker locker;
338     lldb::ValueObjectSP value_sp(GetSP(locker));
339     if (value_sp)
340         name = value_sp->GetName().GetCString();
341 
342     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
343     if (log)
344     {
345         if (name)
346             log->Printf ("SBValue(%p)::GetName () => \"%s\"",
347                          static_cast<void*>(value_sp.get()), name);
348         else
349             log->Printf ("SBValue(%p)::GetName () => NULL",
350                          static_cast<void*>(value_sp.get()));
351     }
352 
353     return name;
354 }
355 
356 const char *
357 SBValue::GetTypeName ()
358 {
359     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
360     const char *name = NULL;
361     ValueLocker locker;
362     lldb::ValueObjectSP value_sp(GetSP(locker));
363     if (value_sp)
364     {
365         name = value_sp->GetQualifiedTypeName().GetCString();
366     }
367 
368     if (log)
369     {
370         if (name)
371             log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"",
372                          static_cast<void*>(value_sp.get()), name);
373         else
374             log->Printf ("SBValue(%p)::GetTypeName () => NULL",
375                          static_cast<void*>(value_sp.get()));
376     }
377 
378     return name;
379 }
380 
381 const char *
382 SBValue::GetDisplayTypeName ()
383 {
384     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
385     const char *name = NULL;
386     ValueLocker locker;
387     lldb::ValueObjectSP value_sp(GetSP(locker));
388     if (value_sp)
389     {
390         name = value_sp->GetDisplayTypeName().GetCString();
391     }
392 
393     if (log)
394     {
395         if (name)
396             log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"",
397                          static_cast<void*>(value_sp.get()), name);
398         else
399             log->Printf ("SBValue(%p)::GetTypeName () => NULL",
400                          static_cast<void*>(value_sp.get()));
401     }
402 
403     return name;
404 }
405 
406 size_t
407 SBValue::GetByteSize ()
408 {
409     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
410     size_t result = 0;
411 
412     ValueLocker locker;
413     lldb::ValueObjectSP value_sp(GetSP(locker));
414     if (value_sp)
415     {
416         result = value_sp->GetByteSize();
417     }
418 
419     if (log)
420         log->Printf ("SBValue(%p)::GetByteSize () => %" PRIu64,
421                      static_cast<void*>(value_sp.get()),
422                      static_cast<uint64_t>(result));
423 
424     return result;
425 }
426 
427 bool
428 SBValue::IsInScope ()
429 {
430     bool result = false;
431 
432     ValueLocker locker;
433     lldb::ValueObjectSP value_sp(GetSP(locker));
434     if (value_sp)
435     {
436         result = value_sp->IsInScope ();
437     }
438 
439     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
440     if (log)
441         log->Printf ("SBValue(%p)::IsInScope () => %i",
442                      static_cast<void*>(value_sp.get()), result);
443 
444     return result;
445 }
446 
447 const char *
448 SBValue::GetValue ()
449 {
450     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
451 
452     const char *cstr = NULL;
453     ValueLocker locker;
454     lldb::ValueObjectSP value_sp(GetSP(locker));
455     if (value_sp)
456     {
457         cstr = value_sp->GetValueAsCString ();
458     }
459     if (log)
460     {
461         if (cstr)
462             log->Printf ("SBValue(%p)::GetValue() => \"%s\"",
463                          static_cast<void*>(value_sp.get()), cstr);
464         else
465             log->Printf ("SBValue(%p)::GetValue() => NULL",
466                          static_cast<void*>(value_sp.get()));
467     }
468 
469     return cstr;
470 }
471 
472 ValueType
473 SBValue::GetValueType ()
474 {
475     ValueType result = eValueTypeInvalid;
476     ValueLocker locker;
477     lldb::ValueObjectSP value_sp(GetSP(locker));
478     if (value_sp)
479         result = value_sp->GetValueType();
480 
481     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
482     if (log)
483     {
484         switch (result)
485         {
486             case eValueTypeInvalid:
487                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid",
488                              static_cast<void*>(value_sp.get()));
489                 break;
490             case eValueTypeVariableGlobal:
491                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal",
492                              static_cast<void*>(value_sp.get()));
493                 break;
494             case eValueTypeVariableStatic:
495                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic",
496                              static_cast<void*>(value_sp.get()));
497                 break;
498             case eValueTypeVariableArgument:
499                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument",
500                              static_cast<void*>(value_sp.get()));
501                 break;
502             case eValueTypeVariableLocal:
503                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal",
504                              static_cast<void*>(value_sp.get()));
505                 break;
506             case eValueTypeRegister:
507                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister",
508                              static_cast<void*>(value_sp.get()));
509                 break;
510             case eValueTypeRegisterSet:
511                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet",
512                              static_cast<void*>(value_sp.get()));
513                 break;
514             case eValueTypeConstResult:
515                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult",
516                              static_cast<void*>(value_sp.get()));
517                 break;
518         }
519     }
520     return result;
521 }
522 
523 const char *
524 SBValue::GetObjectDescription ()
525 {
526     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
527     const char *cstr = NULL;
528     ValueLocker locker;
529     lldb::ValueObjectSP value_sp(GetSP(locker));
530     if (value_sp)
531     {
532         cstr = value_sp->GetObjectDescription ();
533     }
534     if (log)
535     {
536         if (cstr)
537             log->Printf ("SBValue(%p)::GetObjectDescription() => \"%s\"",
538                          static_cast<void*>(value_sp.get()), cstr);
539         else
540             log->Printf ("SBValue(%p)::GetObjectDescription() => NULL",
541                          static_cast<void*>(value_sp.get()));
542     }
543     return cstr;
544 }
545 
546 const char *
547 SBValue::GetTypeValidatorResult ()
548 {
549     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
550     const char *cstr = NULL;
551     ValueLocker locker;
552     lldb::ValueObjectSP value_sp(GetSP(locker));
553     if (value_sp)
554     {
555         const auto& validation(value_sp->GetValidationStatus());
556         if (TypeValidatorResult::Failure == validation.first)
557         {
558             if (validation.second.empty())
559                 cstr = "unknown error";
560             else
561                 cstr = validation.second.c_str();
562         }
563     }
564     if (log)
565     {
566         if (cstr)
567             log->Printf ("SBValue(%p)::GetTypeValidatorResult() => \"%s\"",
568                          static_cast<void*>(value_sp.get()), cstr);
569         else
570             log->Printf ("SBValue(%p)::GetTypeValidatorResult() => NULL",
571                          static_cast<void*>(value_sp.get()));
572     }
573     return cstr;
574 }
575 
576 SBType
577 SBValue::GetType()
578 {
579     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
580     SBType sb_type;
581     ValueLocker locker;
582     lldb::ValueObjectSP value_sp(GetSP(locker));
583     TypeImplSP type_sp;
584     if (value_sp)
585     {
586         type_sp.reset (new TypeImpl(value_sp->GetTypeImpl()));
587         sb_type.SetSP(type_sp);
588     }
589     if (log)
590     {
591         if (type_sp)
592             log->Printf ("SBValue(%p)::GetType => SBType(%p)",
593                          static_cast<void*>(value_sp.get()),
594                          static_cast<void*>(type_sp.get()));
595         else
596             log->Printf ("SBValue(%p)::GetType => NULL",
597                          static_cast<void*>(value_sp.get()));
598     }
599     return sb_type;
600 }
601 
602 bool
603 SBValue::GetValueDidChange ()
604 {
605     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
606     bool result = false;
607     ValueLocker locker;
608     lldb::ValueObjectSP value_sp(GetSP(locker));
609     if (value_sp)
610     {
611         result = value_sp->GetValueDidChange ();
612     }
613     if (log)
614         log->Printf ("SBValue(%p)::GetValueDidChange() => %i",
615                      static_cast<void*>(value_sp.get()), result);
616 
617     return result;
618 }
619 
620 #ifndef LLDB_DISABLE_PYTHON
621 const char *
622 SBValue::GetSummary ()
623 {
624     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
625     const char *cstr = NULL;
626     ValueLocker locker;
627     lldb::ValueObjectSP value_sp(GetSP(locker));
628     if (value_sp)
629     {
630         cstr = value_sp->GetSummaryAsCString();
631     }
632     if (log)
633     {
634         if (cstr)
635             log->Printf ("SBValue(%p)::GetSummary() => \"%s\"",
636                          static_cast<void*>(value_sp.get()), cstr);
637         else
638             log->Printf ("SBValue(%p)::GetSummary() => NULL",
639                          static_cast<void*>(value_sp.get()));
640     }
641     return cstr;
642 }
643 
644 const char *
645 SBValue::GetSummary (lldb::SBStream& stream,
646                      lldb::SBTypeSummaryOptions& options)
647 {
648     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
649     ValueLocker locker;
650     lldb::ValueObjectSP value_sp(GetSP(locker));
651     if (value_sp)
652     {
653         std::string buffer;
654         if (value_sp->GetSummaryAsCString(buffer,options.ref()) && !buffer.empty())
655             stream.Printf("%s",buffer.c_str());
656     }
657     const char* cstr = stream.GetData();
658     if (log)
659     {
660         if (cstr)
661             log->Printf ("SBValue(%p)::GetSummary() => \"%s\"",
662                          static_cast<void*>(value_sp.get()), cstr);
663         else
664             log->Printf ("SBValue(%p)::GetSummary() => NULL",
665                          static_cast<void*>(value_sp.get()));
666     }
667     return cstr;
668 }
669 #endif // LLDB_DISABLE_PYTHON
670 
671 const char *
672 SBValue::GetLocation ()
673 {
674     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
675     const char *cstr = NULL;
676     ValueLocker locker;
677     lldb::ValueObjectSP value_sp(GetSP(locker));
678     if (value_sp)
679     {
680         cstr = value_sp->GetLocationAsCString();
681     }
682     if (log)
683     {
684         if (cstr)
685             log->Printf ("SBValue(%p)::GetLocation() => \"%s\"",
686                          static_cast<void*>(value_sp.get()), cstr);
687         else
688             log->Printf ("SBValue(%p)::GetLocation() => NULL",
689                          static_cast<void*>(value_sp.get()));
690     }
691     return cstr;
692 }
693 
694 // Deprecated - use the one that takes an lldb::SBError
695 bool
696 SBValue::SetValueFromCString (const char *value_str)
697 {
698     lldb::SBError dummy;
699     return SetValueFromCString(value_str,dummy);
700 }
701 
702 bool
703 SBValue::SetValueFromCString (const char *value_str, lldb::SBError& error)
704 {
705     bool success = false;
706     ValueLocker locker;
707     lldb::ValueObjectSP value_sp(GetSP(locker));
708     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
709     if (value_sp)
710     {
711         success = value_sp->SetValueFromCString (value_str,error.ref());
712     }
713     else
714         error.SetErrorStringWithFormat ("Could not get value: %s", locker.GetError().AsCString());
715 
716     if (log)
717         log->Printf ("SBValue(%p)::SetValueFromCString(\"%s\") => %i",
718                      static_cast<void*>(value_sp.get()), value_str, success);
719 
720     return success;
721 }
722 
723 lldb::SBTypeFormat
724 SBValue::GetTypeFormat ()
725 {
726     lldb::SBTypeFormat format;
727     ValueLocker locker;
728     lldb::ValueObjectSP value_sp(GetSP(locker));
729     if (value_sp)
730     {
731         if (value_sp->UpdateValueIfNeeded(true))
732         {
733             lldb::TypeFormatImplSP format_sp = value_sp->GetValueFormat();
734             if (format_sp)
735                 format.SetSP(format_sp);
736         }
737     }
738     return format;
739 }
740 
741 #ifndef LLDB_DISABLE_PYTHON
742 lldb::SBTypeSummary
743 SBValue::GetTypeSummary ()
744 {
745     lldb::SBTypeSummary summary;
746     ValueLocker locker;
747     lldb::ValueObjectSP value_sp(GetSP(locker));
748     if (value_sp)
749     {
750         if (value_sp->UpdateValueIfNeeded(true))
751         {
752             lldb::TypeSummaryImplSP summary_sp = value_sp->GetSummaryFormat();
753             if (summary_sp)
754                 summary.SetSP(summary_sp);
755         }
756     }
757     return summary;
758 }
759 #endif // LLDB_DISABLE_PYTHON
760 
761 lldb::SBTypeFilter
762 SBValue::GetTypeFilter ()
763 {
764     lldb::SBTypeFilter filter;
765     ValueLocker locker;
766     lldb::ValueObjectSP value_sp(GetSP(locker));
767     if (value_sp)
768     {
769         if (value_sp->UpdateValueIfNeeded(true))
770         {
771             lldb::SyntheticChildrenSP synthetic_sp = value_sp->GetSyntheticChildren();
772 
773             if (synthetic_sp && !synthetic_sp->IsScripted())
774             {
775                 TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(synthetic_sp);
776                 filter.SetSP(filter_sp);
777             }
778         }
779     }
780     return filter;
781 }
782 
783 #ifndef LLDB_DISABLE_PYTHON
784 lldb::SBTypeSynthetic
785 SBValue::GetTypeSynthetic ()
786 {
787     lldb::SBTypeSynthetic synthetic;
788     ValueLocker locker;
789     lldb::ValueObjectSP value_sp(GetSP(locker));
790     if (value_sp)
791     {
792         if (value_sp->UpdateValueIfNeeded(true))
793         {
794             lldb::SyntheticChildrenSP children_sp = value_sp->GetSyntheticChildren();
795 
796             if (children_sp && children_sp->IsScripted())
797             {
798                 ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
799                 synthetic.SetSP(synth_sp);
800             }
801         }
802     }
803     return synthetic;
804 }
805 #endif
806 
807 lldb::SBValue
808 SBValue::CreateChildAtOffset (const char *name, uint32_t offset, SBType type)
809 {
810     lldb::SBValue sb_value;
811     ValueLocker locker;
812     lldb::ValueObjectSP value_sp(GetSP(locker));
813     lldb::ValueObjectSP new_value_sp;
814     if (value_sp)
815     {
816         TypeImplSP type_sp (type.GetSP());
817         if (type.IsValid())
818         {
819             sb_value.SetSP(value_sp->GetSyntheticChildAtOffset(offset, type_sp->GetClangASTType(false), true),GetPreferDynamicValue(),GetPreferSyntheticValue(), name);
820         }
821     }
822     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
823     if (log)
824     {
825         if (new_value_sp)
826             log->Printf ("SBValue(%p)::CreateChildAtOffset => \"%s\"",
827                          static_cast<void*>(value_sp.get()),
828                          new_value_sp->GetName().AsCString());
829         else
830             log->Printf ("SBValue(%p)::CreateChildAtOffset => NULL",
831                          static_cast<void*>(value_sp.get()));
832     }
833     return sb_value;
834 }
835 
836 lldb::SBValue
837 SBValue::Cast (SBType type)
838 {
839     lldb::SBValue sb_value;
840     ValueLocker locker;
841     lldb::ValueObjectSP value_sp(GetSP(locker));
842     TypeImplSP type_sp (type.GetSP());
843     if (value_sp && type_sp)
844         sb_value.SetSP(value_sp->Cast(type_sp->GetClangASTType(false)),GetPreferDynamicValue(),GetPreferSyntheticValue());
845     return sb_value;
846 }
847 
848 lldb::SBValue
849 SBValue::CreateValueFromExpression (const char *name, const char* expression)
850 {
851     SBExpressionOptions options;
852     options.ref().SetKeepInMemory(true);
853     return CreateValueFromExpression (name, expression, options);
854 }
855 
856 lldb::SBValue
857 SBValue::CreateValueFromExpression (const char *name, const char *expression, SBExpressionOptions &options)
858 {
859     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
860     lldb::SBValue sb_value;
861     ValueLocker locker;
862     lldb::ValueObjectSP value_sp(GetSP(locker));
863     lldb::ValueObjectSP new_value_sp;
864     if (value_sp)
865     {
866         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
867         new_value_sp = ValueObject::CreateValueObjectFromExpression(name, expression, exe_ctx, options.ref());
868         if (new_value_sp)
869             new_value_sp->SetName(ConstString(name));
870     }
871     sb_value.SetSP(new_value_sp);
872     if (log)
873     {
874         if (new_value_sp)
875             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => SBValue (%p)",
876                          static_cast<void*>(value_sp.get()), name, expression,
877                          static_cast<void*>(new_value_sp.get()));
878         else
879             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => NULL",
880                          static_cast<void*>(value_sp.get()), name, expression);
881     }
882     return sb_value;
883 }
884 
885 lldb::SBValue
886 SBValue::CreateValueFromAddress(const char* name, lldb::addr_t address, SBType sb_type)
887 {
888     lldb::SBValue sb_value;
889     ValueLocker locker;
890     lldb::ValueObjectSP value_sp(GetSP(locker));
891     lldb::ValueObjectSP new_value_sp;
892     lldb::TypeImplSP type_impl_sp (sb_type.GetSP());
893     if (value_sp && type_impl_sp)
894     {
895         ClangASTType ast_type(type_impl_sp->GetClangASTType(true));
896         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
897         new_value_sp = ValueObject::CreateValueObjectFromAddress(name, address, exe_ctx, ast_type);
898     }
899     sb_value.SetSP(new_value_sp);
900     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
901     if (log)
902     {
903         if (new_value_sp)
904             log->Printf ("SBValue(%p)::CreateValueFromAddress => \"%s\"",
905                          static_cast<void*>(value_sp.get()),
906                          new_value_sp->GetName().AsCString());
907         else
908             log->Printf ("SBValue(%p)::CreateValueFromAddress => NULL",
909                          static_cast<void*>(value_sp.get()));
910     }
911     return sb_value;
912 }
913 
914 lldb::SBValue
915 SBValue::CreateValueFromData (const char* name, SBData data, SBType type)
916 {
917     lldb::SBValue sb_value;
918     lldb::ValueObjectSP new_value_sp;
919     ValueLocker locker;
920     lldb::ValueObjectSP value_sp(GetSP(locker));
921     if (value_sp)
922     {
923         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
924         new_value_sp = ValueObject::CreateValueObjectFromData(name, **data, exe_ctx, type.GetSP()->GetClangASTType(true));
925         new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
926     }
927     sb_value.SetSP(new_value_sp);
928     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
929     if (log)
930     {
931         if (new_value_sp)
932             log->Printf ("SBValue(%p)::CreateValueFromData => \"%s\"",
933                          static_cast<void*>(value_sp.get()),
934                          new_value_sp->GetName().AsCString());
935         else
936             log->Printf ("SBValue(%p)::CreateValueFromData => NULL",
937                          static_cast<void*>(value_sp.get()));
938     }
939     return sb_value;
940 }
941 
942 SBValue
943 SBValue::GetChildAtIndex (uint32_t idx)
944 {
945     const bool can_create_synthetic = false;
946     lldb::DynamicValueType use_dynamic = eNoDynamicValues;
947     TargetSP target_sp;
948     if (m_opaque_sp)
949         target_sp = m_opaque_sp->GetTargetSP();
950 
951     if (target_sp)
952         use_dynamic = target_sp->GetPreferDynamicValue();
953 
954     return GetChildAtIndex (idx, use_dynamic, can_create_synthetic);
955 }
956 
957 SBValue
958 SBValue::GetChildAtIndex (uint32_t idx, lldb::DynamicValueType use_dynamic, bool can_create_synthetic)
959 {
960     lldb::ValueObjectSP child_sp;
961     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
962 
963     ValueLocker locker;
964     lldb::ValueObjectSP value_sp(GetSP(locker));
965     if (value_sp)
966     {
967         const bool can_create = true;
968         child_sp = value_sp->GetChildAtIndex (idx, can_create);
969         if (can_create_synthetic && !child_sp)
970         {
971             if (value_sp->IsPointerType())
972             {
973                 child_sp = value_sp->GetSyntheticArrayMemberFromPointer(idx, can_create);
974             }
975             else if (value_sp->IsArrayType())
976             {
977                 child_sp = value_sp->GetSyntheticArrayMemberFromArray(idx, can_create);
978             }
979         }
980     }
981 
982     SBValue sb_value;
983     sb_value.SetSP (child_sp, use_dynamic, GetPreferSyntheticValue());
984     if (log)
985         log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)",
986                      static_cast<void*>(value_sp.get()), idx,
987                      static_cast<void*>(value_sp.get()));
988 
989     return sb_value;
990 }
991 
992 uint32_t
993 SBValue::GetIndexOfChildWithName (const char *name)
994 {
995     uint32_t idx = UINT32_MAX;
996     ValueLocker locker;
997     lldb::ValueObjectSP value_sp(GetSP(locker));
998     if (value_sp)
999     {
1000         idx = value_sp->GetIndexOfChildWithName (ConstString(name));
1001     }
1002     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1003     if (log)
1004     {
1005         if (idx == UINT32_MAX)
1006             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND",
1007                          static_cast<void*>(value_sp.get()), name);
1008         else
1009             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u",
1010                          static_cast<void*>(value_sp.get()), name, idx);
1011     }
1012     return idx;
1013 }
1014 
1015 SBValue
1016 SBValue::GetChildMemberWithName (const char *name)
1017 {
1018     lldb::DynamicValueType use_dynamic_value = eNoDynamicValues;
1019     TargetSP target_sp;
1020     if (m_opaque_sp)
1021         target_sp = m_opaque_sp->GetTargetSP();
1022 
1023     if (target_sp)
1024         use_dynamic_value = target_sp->GetPreferDynamicValue();
1025     return GetChildMemberWithName (name, use_dynamic_value);
1026 }
1027 
1028 SBValue
1029 SBValue::GetChildMemberWithName (const char *name, lldb::DynamicValueType use_dynamic_value)
1030 {
1031     lldb::ValueObjectSP child_sp;
1032     const ConstString str_name (name);
1033 
1034     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1035 
1036     ValueLocker locker;
1037     lldb::ValueObjectSP value_sp(GetSP(locker));
1038     if (value_sp)
1039     {
1040         child_sp = value_sp->GetChildMemberWithName (str_name, true);
1041     }
1042 
1043     SBValue sb_value;
1044     sb_value.SetSP(child_sp, use_dynamic_value, GetPreferSyntheticValue());
1045 
1046     if (log)
1047         log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)",
1048                      static_cast<void*>(value_sp.get()), name,
1049                      static_cast<void*>(value_sp.get()));
1050 
1051     return sb_value;
1052 }
1053 
1054 lldb::SBValue
1055 SBValue::GetDynamicValue (lldb::DynamicValueType use_dynamic)
1056 {
1057     SBValue value_sb;
1058     if (IsValid())
1059     {
1060         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),use_dynamic,m_opaque_sp->GetUseSynthetic()));
1061         value_sb.SetSP(proxy_sp);
1062     }
1063     return value_sb;
1064 }
1065 
1066 lldb::SBValue
1067 SBValue::GetStaticValue ()
1068 {
1069     SBValue value_sb;
1070     if (IsValid())
1071     {
1072         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),eNoDynamicValues,m_opaque_sp->GetUseSynthetic()));
1073         value_sb.SetSP(proxy_sp);
1074     }
1075     return value_sb;
1076 }
1077 
1078 lldb::SBValue
1079 SBValue::GetNonSyntheticValue ()
1080 {
1081     SBValue value_sb;
1082     if (IsValid())
1083     {
1084         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),m_opaque_sp->GetUseDynamic(),false));
1085         value_sb.SetSP(proxy_sp);
1086     }
1087     return value_sb;
1088 }
1089 
1090 lldb::DynamicValueType
1091 SBValue::GetPreferDynamicValue ()
1092 {
1093     if (!IsValid())
1094         return eNoDynamicValues;
1095     return m_opaque_sp->GetUseDynamic();
1096 }
1097 
1098 void
1099 SBValue::SetPreferDynamicValue (lldb::DynamicValueType use_dynamic)
1100 {
1101     if (IsValid())
1102         return m_opaque_sp->SetUseDynamic (use_dynamic);
1103 }
1104 
1105 bool
1106 SBValue::GetPreferSyntheticValue ()
1107 {
1108     if (!IsValid())
1109         return false;
1110     return m_opaque_sp->GetUseSynthetic();
1111 }
1112 
1113 void
1114 SBValue::SetPreferSyntheticValue (bool use_synthetic)
1115 {
1116     if (IsValid())
1117         return m_opaque_sp->SetUseSynthetic (use_synthetic);
1118 }
1119 
1120 bool
1121 SBValue::IsDynamic()
1122 {
1123     ValueLocker locker;
1124     lldb::ValueObjectSP value_sp(GetSP(locker));
1125     if (value_sp)
1126         return value_sp->IsDynamic();
1127     return false;
1128 }
1129 
1130 bool
1131 SBValue::IsSynthetic ()
1132 {
1133     ValueLocker locker;
1134     lldb::ValueObjectSP value_sp(GetSP(locker));
1135     if (value_sp)
1136         return value_sp->IsSynthetic();
1137     return false;
1138 }
1139 
1140 lldb::SBValue
1141 SBValue::GetValueForExpressionPath(const char* expr_path)
1142 {
1143     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1144     lldb::ValueObjectSP child_sp;
1145     ValueLocker locker;
1146     lldb::ValueObjectSP value_sp(GetSP(locker));
1147     if (value_sp)
1148     {
1149         // using default values for all the fancy options, just do it if you can
1150         child_sp = value_sp->GetValueForExpressionPath(expr_path);
1151     }
1152 
1153     SBValue sb_value;
1154     sb_value.SetSP(child_sp,GetPreferDynamicValue(),GetPreferSyntheticValue());
1155 
1156     if (log)
1157         log->Printf ("SBValue(%p)::GetValueForExpressionPath (expr_path=\"%s\") => SBValue(%p)",
1158                      static_cast<void*>(value_sp.get()), expr_path,
1159                      static_cast<void*>(value_sp.get()));
1160 
1161     return sb_value;
1162 }
1163 
1164 int64_t
1165 SBValue::GetValueAsSigned(SBError& error, int64_t fail_value)
1166 {
1167     error.Clear();
1168     ValueLocker locker;
1169     lldb::ValueObjectSP value_sp(GetSP(locker));
1170     if (value_sp)
1171     {
1172         bool success = true;
1173         uint64_t ret_val = fail_value;
1174         ret_val = value_sp->GetValueAsSigned(fail_value, &success);
1175         if (!success)
1176             error.SetErrorString("could not resolve value");
1177         return ret_val;
1178     }
1179     else
1180         error.SetErrorStringWithFormat ("could not get SBValue: %s", locker.GetError().AsCString());
1181 
1182     return fail_value;
1183 }
1184 
1185 uint64_t
1186 SBValue::GetValueAsUnsigned(SBError& error, uint64_t fail_value)
1187 {
1188     error.Clear();
1189     ValueLocker locker;
1190     lldb::ValueObjectSP value_sp(GetSP(locker));
1191     if (value_sp)
1192     {
1193         bool success = true;
1194         uint64_t ret_val = fail_value;
1195         ret_val = value_sp->GetValueAsUnsigned(fail_value, &success);
1196         if (!success)
1197             error.SetErrorString("could not resolve value");
1198         return ret_val;
1199     }
1200     else
1201         error.SetErrorStringWithFormat ("could not get SBValue: %s", locker.GetError().AsCString());
1202 
1203     return fail_value;
1204 }
1205 
1206 int64_t
1207 SBValue::GetValueAsSigned(int64_t fail_value)
1208 {
1209     ValueLocker locker;
1210     lldb::ValueObjectSP value_sp(GetSP(locker));
1211     if (value_sp)
1212     {
1213         return value_sp->GetValueAsSigned(fail_value);
1214     }
1215     return fail_value;
1216 }
1217 
1218 uint64_t
1219 SBValue::GetValueAsUnsigned(uint64_t fail_value)
1220 {
1221     ValueLocker locker;
1222     lldb::ValueObjectSP value_sp(GetSP(locker));
1223     if (value_sp)
1224     {
1225         return value_sp->GetValueAsUnsigned(fail_value);
1226     }
1227     return fail_value;
1228 }
1229 
1230 bool
1231 SBValue::MightHaveChildren ()
1232 {
1233     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1234     bool has_children = false;
1235     ValueLocker locker;
1236     lldb::ValueObjectSP value_sp(GetSP(locker));
1237     if (value_sp)
1238         has_children = value_sp->MightHaveChildren();
1239 
1240     if (log)
1241         log->Printf ("SBValue(%p)::MightHaveChildren() => %i",
1242                      static_cast<void*>(value_sp.get()), has_children);
1243     return has_children;
1244 }
1245 
1246 uint32_t
1247 SBValue::GetNumChildren ()
1248 {
1249     uint32_t num_children = 0;
1250 
1251     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1252     ValueLocker locker;
1253     lldb::ValueObjectSP value_sp(GetSP(locker));
1254     if (value_sp)
1255         num_children = value_sp->GetNumChildren();
1256 
1257     if (log)
1258         log->Printf ("SBValue(%p)::GetNumChildren () => %u",
1259                      static_cast<void*>(value_sp.get()), num_children);
1260 
1261     return num_children;
1262 }
1263 
1264 
1265 SBValue
1266 SBValue::Dereference ()
1267 {
1268     SBValue sb_value;
1269     ValueLocker locker;
1270     lldb::ValueObjectSP value_sp(GetSP(locker));
1271     if (value_sp)
1272     {
1273         Error error;
1274         sb_value = value_sp->Dereference (error);
1275     }
1276     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1277     if (log)
1278         log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)",
1279                      static_cast<void*>(value_sp.get()),
1280                      static_cast<void*>(value_sp.get()));
1281 
1282     return sb_value;
1283 }
1284 
1285 bool
1286 SBValue::TypeIsPointerType ()
1287 {
1288     bool is_ptr_type = false;
1289 
1290     ValueLocker locker;
1291     lldb::ValueObjectSP value_sp(GetSP(locker));
1292     if (value_sp)
1293         is_ptr_type = value_sp->IsPointerType();
1294 
1295     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1296     if (log)
1297         log->Printf ("SBValue(%p)::TypeIsPointerType () => %i",
1298                      static_cast<void*>(value_sp.get()), is_ptr_type);
1299 
1300     return is_ptr_type;
1301 }
1302 
1303 void *
1304 SBValue::GetOpaqueType()
1305 {
1306     ValueLocker locker;
1307     lldb::ValueObjectSP value_sp(GetSP(locker));
1308     if (value_sp)
1309         return value_sp->GetClangType().GetOpaqueQualType();
1310     return NULL;
1311 }
1312 
1313 lldb::SBTarget
1314 SBValue::GetTarget()
1315 {
1316     SBTarget sb_target;
1317     TargetSP target_sp;
1318     if (m_opaque_sp)
1319     {
1320         target_sp = m_opaque_sp->GetTargetSP();
1321         sb_target.SetSP (target_sp);
1322     }
1323     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1324     if (log)
1325     {
1326         if (target_sp.get() == NULL)
1327             log->Printf ("SBValue(%p)::GetTarget () => NULL",
1328                          static_cast<void*>(m_opaque_sp.get()));
1329         else
1330             log->Printf ("SBValue(%p)::GetTarget () => %p",
1331                          static_cast<void*>(m_opaque_sp.get()),
1332                          static_cast<void*>(target_sp.get()));
1333     }
1334     return sb_target;
1335 }
1336 
1337 lldb::SBProcess
1338 SBValue::GetProcess()
1339 {
1340     SBProcess sb_process;
1341     ProcessSP process_sp;
1342     if (m_opaque_sp)
1343     {
1344         process_sp = m_opaque_sp->GetProcessSP();
1345         sb_process.SetSP (process_sp);
1346     }
1347     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1348     if (log)
1349     {
1350         if (process_sp.get() == NULL)
1351             log->Printf ("SBValue(%p)::GetProcess () => NULL",
1352                          static_cast<void*>(m_opaque_sp.get()));
1353         else
1354             log->Printf ("SBValue(%p)::GetProcess () => %p",
1355                          static_cast<void*>(m_opaque_sp.get()),
1356                          static_cast<void*>(process_sp.get()));
1357     }
1358     return sb_process;
1359 }
1360 
1361 lldb::SBThread
1362 SBValue::GetThread()
1363 {
1364     SBThread sb_thread;
1365     ThreadSP thread_sp;
1366     if (m_opaque_sp)
1367     {
1368         thread_sp = m_opaque_sp->GetThreadSP();
1369         sb_thread.SetThread(thread_sp);
1370     }
1371     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1372     if (log)
1373     {
1374         if (thread_sp.get() == NULL)
1375             log->Printf ("SBValue(%p)::GetThread () => NULL",
1376                          static_cast<void*>(m_opaque_sp.get()));
1377         else
1378             log->Printf ("SBValue(%p)::GetThread () => %p",
1379                          static_cast<void*>(m_opaque_sp.get()),
1380                          static_cast<void*>(thread_sp.get()));
1381     }
1382     return sb_thread;
1383 }
1384 
1385 lldb::SBFrame
1386 SBValue::GetFrame()
1387 {
1388     SBFrame sb_frame;
1389     StackFrameSP frame_sp;
1390     if (m_opaque_sp)
1391     {
1392         frame_sp = m_opaque_sp->GetFrameSP();
1393         sb_frame.SetFrameSP (frame_sp);
1394     }
1395     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1396     if (log)
1397     {
1398         if (frame_sp.get() == NULL)
1399             log->Printf ("SBValue(%p)::GetFrame () => NULL",
1400                          static_cast<void*>(m_opaque_sp.get()));
1401         else
1402             log->Printf ("SBValue(%p)::GetFrame () => %p",
1403                          static_cast<void*>(m_opaque_sp.get()),
1404                          static_cast<void*>(frame_sp.get()));
1405     }
1406     return sb_frame;
1407 }
1408 
1409 
1410 lldb::ValueObjectSP
1411 SBValue::GetSP (ValueLocker &locker) const
1412 {
1413     if (!m_opaque_sp || !m_opaque_sp->IsValid())
1414         return ValueObjectSP();
1415     return locker.GetLockedSP(*m_opaque_sp.get());
1416 }
1417 
1418 lldb::ValueObjectSP
1419 SBValue::GetSP () const
1420 {
1421     ValueLocker locker;
1422     return GetSP(locker);
1423 }
1424 
1425 void
1426 SBValue::SetSP (ValueImplSP impl_sp)
1427 {
1428     m_opaque_sp = impl_sp;
1429 }
1430 
1431 void
1432 SBValue::SetSP (const lldb::ValueObjectSP &sp)
1433 {
1434     if (sp)
1435     {
1436         lldb::TargetSP target_sp(sp->GetTargetSP());
1437         if (target_sp)
1438         {
1439             lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue();
1440             bool use_synthetic = target_sp->TargetProperties::GetEnableSyntheticValue();
1441             m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic));
1442         }
1443         else
1444             m_opaque_sp = ValueImplSP(new ValueImpl(sp,eNoDynamicValues,true));
1445     }
1446     else
1447         m_opaque_sp = ValueImplSP(new ValueImpl(sp,eNoDynamicValues,false));
1448 }
1449 
1450 void
1451 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic)
1452 {
1453     if (sp)
1454     {
1455         lldb::TargetSP target_sp(sp->GetTargetSP());
1456         if (target_sp)
1457         {
1458             bool use_synthetic = target_sp->TargetProperties::GetEnableSyntheticValue();
1459             SetSP (sp, use_dynamic, use_synthetic);
1460         }
1461         else
1462             SetSP (sp, use_dynamic, true);
1463     }
1464     else
1465         SetSP (sp, use_dynamic, false);
1466 }
1467 
1468 void
1469 SBValue::SetSP (const lldb::ValueObjectSP &sp, bool use_synthetic)
1470 {
1471     if (sp)
1472     {
1473         lldb::TargetSP target_sp(sp->GetTargetSP());
1474         if (target_sp)
1475         {
1476             lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue();
1477             SetSP (sp, use_dynamic, use_synthetic);
1478         }
1479         else
1480             SetSP (sp, eNoDynamicValues, use_synthetic);
1481     }
1482     else
1483         SetSP (sp, eNoDynamicValues, use_synthetic);
1484 }
1485 
1486 void
1487 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic, bool use_synthetic)
1488 {
1489     m_opaque_sp = ValueImplSP(new ValueImpl(sp,use_dynamic,use_synthetic));
1490 }
1491 
1492 void
1493 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, const char *name)
1494 {
1495     m_opaque_sp = ValueImplSP(new ValueImpl(sp,use_dynamic,use_synthetic, name));
1496 }
1497 
1498 bool
1499 SBValue::GetExpressionPath (SBStream &description)
1500 {
1501     ValueLocker locker;
1502     lldb::ValueObjectSP value_sp(GetSP(locker));
1503     if (value_sp)
1504     {
1505         value_sp->GetExpressionPath (description.ref(), false);
1506         return true;
1507     }
1508     return false;
1509 }
1510 
1511 bool
1512 SBValue::GetExpressionPath (SBStream &description, bool qualify_cxx_base_classes)
1513 {
1514     ValueLocker locker;
1515     lldb::ValueObjectSP value_sp(GetSP(locker));
1516     if (value_sp)
1517     {
1518         value_sp->GetExpressionPath (description.ref(), qualify_cxx_base_classes);
1519         return true;
1520     }
1521     return false;
1522 }
1523 
1524 bool
1525 SBValue::GetDescription (SBStream &description)
1526 {
1527     Stream &strm = description.ref();
1528 
1529     ValueLocker locker;
1530     lldb::ValueObjectSP value_sp(GetSP(locker));
1531     if (value_sp)
1532         value_sp->Dump(strm);
1533     else
1534         strm.PutCString ("No value");
1535 
1536     return true;
1537 }
1538 
1539 lldb::Format
1540 SBValue::GetFormat ()
1541 {
1542     ValueLocker locker;
1543     lldb::ValueObjectSP value_sp(GetSP(locker));
1544     if (value_sp)
1545         return value_sp->GetFormat();
1546     return eFormatDefault;
1547 }
1548 
1549 void
1550 SBValue::SetFormat (lldb::Format format)
1551 {
1552     ValueLocker locker;
1553     lldb::ValueObjectSP value_sp(GetSP(locker));
1554     if (value_sp)
1555         value_sp->SetFormat(format);
1556 }
1557 
1558 lldb::SBValue
1559 SBValue::AddressOf()
1560 {
1561     SBValue sb_value;
1562     ValueLocker locker;
1563     lldb::ValueObjectSP value_sp(GetSP(locker));
1564     if (value_sp)
1565     {
1566         Error error;
1567         sb_value.SetSP(value_sp->AddressOf (error),GetPreferDynamicValue(), GetPreferSyntheticValue());
1568     }
1569     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1570     if (log)
1571         log->Printf ("SBValue(%p)::AddressOf () => SBValue(%p)",
1572                      static_cast<void*>(value_sp.get()),
1573                      static_cast<void*>(value_sp.get()));
1574 
1575     return sb_value;
1576 }
1577 
1578 lldb::addr_t
1579 SBValue::GetLoadAddress()
1580 {
1581     lldb::addr_t value = LLDB_INVALID_ADDRESS;
1582     ValueLocker locker;
1583     lldb::ValueObjectSP value_sp(GetSP(locker));
1584     if (value_sp)
1585     {
1586         TargetSP target_sp (value_sp->GetTargetSP());
1587         if (target_sp)
1588         {
1589             const bool scalar_is_load_address = true;
1590             AddressType addr_type;
1591             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1592             if (addr_type == eAddressTypeFile)
1593             {
1594                 ModuleSP module_sp (value_sp->GetModule());
1595                 if (!module_sp)
1596                     value = LLDB_INVALID_ADDRESS;
1597                 else
1598                 {
1599                     Address addr;
1600                     module_sp->ResolveFileAddress(value, addr);
1601                     value = addr.GetLoadAddress(target_sp.get());
1602                 }
1603             }
1604             else if (addr_type == eAddressTypeHost || addr_type == eAddressTypeInvalid)
1605                 value = LLDB_INVALID_ADDRESS;
1606         }
1607     }
1608     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1609     if (log)
1610         log->Printf ("SBValue(%p)::GetLoadAddress () => (%" PRIu64 ")",
1611                      static_cast<void*>(value_sp.get()), value);
1612 
1613     return value;
1614 }
1615 
1616 lldb::SBAddress
1617 SBValue::GetAddress()
1618 {
1619     Address addr;
1620     ValueLocker locker;
1621     lldb::ValueObjectSP value_sp(GetSP(locker));
1622     if (value_sp)
1623     {
1624         TargetSP target_sp (value_sp->GetTargetSP());
1625         if (target_sp)
1626         {
1627             lldb::addr_t value = LLDB_INVALID_ADDRESS;
1628             const bool scalar_is_load_address = true;
1629             AddressType addr_type;
1630             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1631             if (addr_type == eAddressTypeFile)
1632             {
1633                 ModuleSP module_sp (value_sp->GetModule());
1634                 if (module_sp)
1635                     module_sp->ResolveFileAddress(value, addr);
1636             }
1637             else if (addr_type == eAddressTypeLoad)
1638             {
1639                 // no need to check the return value on this.. if it can actually do the resolve
1640                 // addr will be in the form (section,offset), otherwise it will simply be returned
1641                 // as (NULL, value)
1642                 addr.SetLoadAddress(value, target_sp.get());
1643             }
1644         }
1645     }
1646     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1647     if (log)
1648         log->Printf ("SBValue(%p)::GetAddress () => (%s,%" PRIu64 ")",
1649                      static_cast<void*>(value_sp.get()),
1650                      (addr.GetSection()
1651                         ? addr.GetSection()->GetName().GetCString()
1652                         : "NULL"),
1653                      addr.GetOffset());
1654     return SBAddress(new Address(addr));
1655 }
1656 
1657 lldb::SBData
1658 SBValue::GetPointeeData (uint32_t item_idx,
1659                          uint32_t item_count)
1660 {
1661     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1662     lldb::SBData sb_data;
1663     ValueLocker locker;
1664     lldb::ValueObjectSP value_sp(GetSP(locker));
1665     if (value_sp)
1666     {
1667         TargetSP target_sp (value_sp->GetTargetSP());
1668         if (target_sp)
1669         {
1670             DataExtractorSP data_sp(new DataExtractor());
1671             value_sp->GetPointeeData(*data_sp, item_idx, item_count);
1672             if (data_sp->GetByteSize() > 0)
1673                 *sb_data = data_sp;
1674         }
1675     }
1676     if (log)
1677         log->Printf ("SBValue(%p)::GetPointeeData (%d, %d) => SBData(%p)",
1678                      static_cast<void*>(value_sp.get()), item_idx, item_count,
1679                      static_cast<void*>(sb_data.get()));
1680 
1681     return sb_data;
1682 }
1683 
1684 lldb::SBData
1685 SBValue::GetData ()
1686 {
1687     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1688     lldb::SBData sb_data;
1689     ValueLocker locker;
1690     lldb::ValueObjectSP value_sp(GetSP(locker));
1691     if (value_sp)
1692     {
1693         DataExtractorSP data_sp(new DataExtractor());
1694         Error error;
1695         value_sp->GetData(*data_sp, error);
1696         if (error.Success())
1697             *sb_data = data_sp;
1698     }
1699     if (log)
1700         log->Printf ("SBValue(%p)::GetData () => SBData(%p)",
1701                      static_cast<void*>(value_sp.get()),
1702                      static_cast<void*>(sb_data.get()));
1703 
1704     return sb_data;
1705 }
1706 
1707 bool
1708 SBValue::SetData (lldb::SBData &data, SBError &error)
1709 {
1710     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1711     ValueLocker locker;
1712     lldb::ValueObjectSP value_sp(GetSP(locker));
1713     bool ret = true;
1714 
1715     if (value_sp)
1716     {
1717         DataExtractor *data_extractor = data.get();
1718 
1719         if (!data_extractor)
1720         {
1721             if (log)
1722                 log->Printf ("SBValue(%p)::SetData() => error: no data to set",
1723                              static_cast<void*>(value_sp.get()));
1724 
1725             error.SetErrorString("No data to set");
1726             ret = false;
1727         }
1728         else
1729         {
1730             Error set_error;
1731 
1732             value_sp->SetData(*data_extractor, set_error);
1733 
1734             if (!set_error.Success())
1735             {
1736                 error.SetErrorStringWithFormat("Couldn't set data: %s", set_error.AsCString());
1737                 ret = false;
1738             }
1739         }
1740     }
1741     else
1742     {
1743         error.SetErrorStringWithFormat ("Couldn't set data: could not get SBValue: %s", locker.GetError().AsCString());
1744         ret = false;
1745     }
1746 
1747     if (log)
1748         log->Printf ("SBValue(%p)::SetData (%p) => %s",
1749                      static_cast<void*>(value_sp.get()),
1750                      static_cast<void*>(data.get()), ret ? "true" : "false");
1751     return ret;
1752 }
1753 
1754 lldb::SBDeclaration
1755 SBValue::GetDeclaration ()
1756 {
1757     ValueLocker locker;
1758     lldb::ValueObjectSP value_sp(GetSP(locker));
1759     SBDeclaration decl_sb;
1760     if (value_sp)
1761     {
1762         Declaration decl;
1763         if (value_sp->GetDeclaration(decl))
1764             decl_sb.SetDeclaration(decl);
1765     }
1766     return decl_sb;
1767 }
1768 
1769 lldb::SBWatchpoint
1770 SBValue::Watch (bool resolve_location, bool read, bool write, SBError &error)
1771 {
1772     SBWatchpoint sb_watchpoint;
1773 
1774     // If the SBValue is not valid, there's no point in even trying to watch it.
1775     ValueLocker locker;
1776     lldb::ValueObjectSP value_sp(GetSP(locker));
1777     TargetSP target_sp (GetTarget().GetSP());
1778     if (value_sp && target_sp)
1779     {
1780         // Read and Write cannot both be false.
1781         if (!read && !write)
1782             return sb_watchpoint;
1783 
1784         // If the value is not in scope, don't try and watch and invalid value
1785         if (!IsInScope())
1786             return sb_watchpoint;
1787 
1788         addr_t addr = GetLoadAddress();
1789         if (addr == LLDB_INVALID_ADDRESS)
1790             return sb_watchpoint;
1791         size_t byte_size = GetByteSize();
1792         if (byte_size == 0)
1793             return sb_watchpoint;
1794 
1795         uint32_t watch_type = 0;
1796         if (read)
1797             watch_type |= LLDB_WATCH_TYPE_READ;
1798         if (write)
1799             watch_type |= LLDB_WATCH_TYPE_WRITE;
1800 
1801         Error rc;
1802         ClangASTType type (value_sp->GetClangType());
1803         WatchpointSP watchpoint_sp = target_sp->CreateWatchpoint(addr, byte_size, &type, watch_type, rc);
1804         error.SetError(rc);
1805 
1806         if (watchpoint_sp)
1807         {
1808             sb_watchpoint.SetSP (watchpoint_sp);
1809             Declaration decl;
1810             if (value_sp->GetDeclaration (decl))
1811             {
1812                 if (decl.GetFile())
1813                 {
1814                     StreamString ss;
1815                     // True to show fullpath for declaration file.
1816                     decl.DumpStopContext(&ss, true);
1817                     watchpoint_sp->SetDeclInfo(ss.GetString());
1818                 }
1819             }
1820         }
1821     }
1822     else if (target_sp)
1823     {
1824         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1825         if (log)
1826             log->Printf ("SBValue(%p)::Watch() => error getting SBValue: %s",
1827                          static_cast<void*>(value_sp.get()),
1828                          locker.GetError().AsCString());
1829 
1830         error.SetErrorStringWithFormat("could not get SBValue: %s", locker.GetError().AsCString());
1831     }
1832     else
1833     {
1834         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1835         if (log)
1836             log->Printf ("SBValue(%p)::Watch() => error getting SBValue: no target",
1837                          static_cast<void*>(value_sp.get()));
1838         error.SetErrorString("could not set watchpoint, a target is required");
1839     }
1840 
1841     return sb_watchpoint;
1842 }
1843 
1844 // FIXME: Remove this method impl (as well as the decl in .h) once it is no longer needed.
1845 // Backward compatibility fix in the interim.
1846 lldb::SBWatchpoint
1847 SBValue::Watch (bool resolve_location, bool read, bool write)
1848 {
1849     SBError error;
1850     return Watch(resolve_location, read, write, error);
1851 }
1852 
1853 lldb::SBWatchpoint
1854 SBValue::WatchPointee (bool resolve_location, bool read, bool write, SBError &error)
1855 {
1856     SBWatchpoint sb_watchpoint;
1857     if (IsInScope() && GetType().IsPointerType())
1858         sb_watchpoint = Dereference().Watch (resolve_location, read, write, error);
1859     return sb_watchpoint;
1860 }
1861 
1862 lldb::SBValue
1863 SBValue::Persist ()
1864 {
1865     ValueLocker locker;
1866     lldb::ValueObjectSP value_sp(GetSP(locker));
1867     SBValue persisted_sb;
1868     if (value_sp)
1869     {
1870         persisted_sb.SetSP(value_sp->Persist());
1871     }
1872     return persisted_sb;
1873 }
1874