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 size_t
382 SBValue::GetByteSize ()
383 {
384     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
385     size_t result = 0;
386 
387     ValueLocker locker;
388     lldb::ValueObjectSP value_sp(GetSP(locker));
389     if (value_sp)
390     {
391         result = value_sp->GetByteSize();
392     }
393 
394     if (log)
395         log->Printf ("SBValue(%p)::GetByteSize () => %" PRIu64,
396                      static_cast<void*>(value_sp.get()),
397                      static_cast<uint64_t>(result));
398 
399     return result;
400 }
401 
402 bool
403 SBValue::IsInScope ()
404 {
405     bool result = false;
406 
407     ValueLocker locker;
408     lldb::ValueObjectSP value_sp(GetSP(locker));
409     if (value_sp)
410     {
411         result = value_sp->IsInScope ();
412     }
413 
414     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
415     if (log)
416         log->Printf ("SBValue(%p)::IsInScope () => %i",
417                      static_cast<void*>(value_sp.get()), result);
418 
419     return result;
420 }
421 
422 const char *
423 SBValue::GetValue ()
424 {
425     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
426 
427     const char *cstr = NULL;
428     ValueLocker locker;
429     lldb::ValueObjectSP value_sp(GetSP(locker));
430     if (value_sp)
431     {
432         cstr = value_sp->GetValueAsCString ();
433     }
434     if (log)
435     {
436         if (cstr)
437             log->Printf ("SBValue(%p)::GetValue() => \"%s\"",
438                          static_cast<void*>(value_sp.get()), cstr);
439         else
440             log->Printf ("SBValue(%p)::GetValue() => NULL",
441                          static_cast<void*>(value_sp.get()));
442     }
443 
444     return cstr;
445 }
446 
447 ValueType
448 SBValue::GetValueType ()
449 {
450     ValueType result = eValueTypeInvalid;
451     ValueLocker locker;
452     lldb::ValueObjectSP value_sp(GetSP(locker));
453     if (value_sp)
454         result = value_sp->GetValueType();
455 
456     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
457     if (log)
458     {
459         switch (result)
460         {
461             case eValueTypeInvalid:
462                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid",
463                              static_cast<void*>(value_sp.get()));
464                 break;
465             case eValueTypeVariableGlobal:
466                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal",
467                              static_cast<void*>(value_sp.get()));
468                 break;
469             case eValueTypeVariableStatic:
470                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic",
471                              static_cast<void*>(value_sp.get()));
472                 break;
473             case eValueTypeVariableArgument:
474                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument",
475                              static_cast<void*>(value_sp.get()));
476                 break;
477             case eValueTypeVariableLocal:
478                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal",
479                              static_cast<void*>(value_sp.get()));
480                 break;
481             case eValueTypeRegister:
482                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister",
483                              static_cast<void*>(value_sp.get()));
484                 break;
485             case eValueTypeRegisterSet:
486                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet",
487                              static_cast<void*>(value_sp.get()));
488                 break;
489             case eValueTypeConstResult:
490                 log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult",
491                              static_cast<void*>(value_sp.get()));
492                 break;
493         }
494     }
495     return result;
496 }
497 
498 const char *
499 SBValue::GetObjectDescription ()
500 {
501     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
502     const char *cstr = NULL;
503     ValueLocker locker;
504     lldb::ValueObjectSP value_sp(GetSP(locker));
505     if (value_sp)
506     {
507         cstr = value_sp->GetObjectDescription ();
508     }
509     if (log)
510     {
511         if (cstr)
512             log->Printf ("SBValue(%p)::GetObjectDescription() => \"%s\"",
513                          static_cast<void*>(value_sp.get()), cstr);
514         else
515             log->Printf ("SBValue(%p)::GetObjectDescription() => NULL",
516                          static_cast<void*>(value_sp.get()));
517     }
518     return cstr;
519 }
520 
521 SBType
522 SBValue::GetType()
523 {
524     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
525     SBType sb_type;
526     ValueLocker locker;
527     lldb::ValueObjectSP value_sp(GetSP(locker));
528     TypeImplSP type_sp;
529     if (value_sp)
530     {
531         type_sp.reset (new TypeImpl(value_sp->GetTypeImpl()));
532         sb_type.SetSP(type_sp);
533     }
534     if (log)
535     {
536         if (type_sp)
537             log->Printf ("SBValue(%p)::GetType => SBType(%p)",
538                          static_cast<void*>(value_sp.get()),
539                          static_cast<void*>(type_sp.get()));
540         else
541             log->Printf ("SBValue(%p)::GetType => NULL",
542                          static_cast<void*>(value_sp.get()));
543     }
544     return sb_type;
545 }
546 
547 bool
548 SBValue::GetValueDidChange ()
549 {
550     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
551     bool result = false;
552     ValueLocker locker;
553     lldb::ValueObjectSP value_sp(GetSP(locker));
554     if (value_sp)
555     {
556         result = value_sp->GetValueDidChange ();
557     }
558     if (log)
559         log->Printf ("SBValue(%p)::GetValueDidChange() => %i",
560                      static_cast<void*>(value_sp.get()), result);
561 
562     return result;
563 }
564 
565 #ifndef LLDB_DISABLE_PYTHON
566 const char *
567 SBValue::GetSummary ()
568 {
569     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
570     const char *cstr = NULL;
571     ValueLocker locker;
572     lldb::ValueObjectSP value_sp(GetSP(locker));
573     if (value_sp)
574     {
575         cstr = value_sp->GetSummaryAsCString();
576     }
577     if (log)
578     {
579         if (cstr)
580             log->Printf ("SBValue(%p)::GetSummary() => \"%s\"",
581                          static_cast<void*>(value_sp.get()), cstr);
582         else
583             log->Printf ("SBValue(%p)::GetSummary() => NULL",
584                          static_cast<void*>(value_sp.get()));
585     }
586     return cstr;
587 }
588 #endif // LLDB_DISABLE_PYTHON
589 
590 const char *
591 SBValue::GetLocation ()
592 {
593     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
594     const char *cstr = NULL;
595     ValueLocker locker;
596     lldb::ValueObjectSP value_sp(GetSP(locker));
597     if (value_sp)
598     {
599         cstr = value_sp->GetLocationAsCString();
600     }
601     if (log)
602     {
603         if (cstr)
604             log->Printf ("SBValue(%p)::GetLocation() => \"%s\"",
605                          static_cast<void*>(value_sp.get()), cstr);
606         else
607             log->Printf ("SBValue(%p)::GetLocation() => NULL",
608                          static_cast<void*>(value_sp.get()));
609     }
610     return cstr;
611 }
612 
613 // Deprecated - use the one that takes an lldb::SBError
614 bool
615 SBValue::SetValueFromCString (const char *value_str)
616 {
617     lldb::SBError dummy;
618     return SetValueFromCString(value_str,dummy);
619 }
620 
621 bool
622 SBValue::SetValueFromCString (const char *value_str, lldb::SBError& error)
623 {
624     bool success = false;
625     ValueLocker locker;
626     lldb::ValueObjectSP value_sp(GetSP(locker));
627     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
628     if (value_sp)
629     {
630         success = value_sp->SetValueFromCString (value_str,error.ref());
631     }
632     else
633         error.SetErrorStringWithFormat ("Could not get value: %s", locker.GetError().AsCString());
634 
635     if (log)
636         log->Printf ("SBValue(%p)::SetValueFromCString(\"%s\") => %i",
637                      static_cast<void*>(value_sp.get()), value_str, success);
638 
639     return success;
640 }
641 
642 lldb::SBTypeFormat
643 SBValue::GetTypeFormat ()
644 {
645     lldb::SBTypeFormat format;
646     ValueLocker locker;
647     lldb::ValueObjectSP value_sp(GetSP(locker));
648     if (value_sp)
649     {
650         if (value_sp->UpdateValueIfNeeded(true))
651         {
652             lldb::TypeFormatImplSP format_sp = value_sp->GetValueFormat();
653             if (format_sp)
654                 format.SetSP(format_sp);
655         }
656     }
657     return format;
658 }
659 
660 #ifndef LLDB_DISABLE_PYTHON
661 lldb::SBTypeSummary
662 SBValue::GetTypeSummary ()
663 {
664     lldb::SBTypeSummary summary;
665     ValueLocker locker;
666     lldb::ValueObjectSP value_sp(GetSP(locker));
667     if (value_sp)
668     {
669         if (value_sp->UpdateValueIfNeeded(true))
670         {
671             lldb::TypeSummaryImplSP summary_sp = value_sp->GetSummaryFormat();
672             if (summary_sp)
673                 summary.SetSP(summary_sp);
674         }
675     }
676     return summary;
677 }
678 #endif // LLDB_DISABLE_PYTHON
679 
680 lldb::SBTypeFilter
681 SBValue::GetTypeFilter ()
682 {
683     lldb::SBTypeFilter filter;
684     ValueLocker locker;
685     lldb::ValueObjectSP value_sp(GetSP(locker));
686     if (value_sp)
687     {
688         if (value_sp->UpdateValueIfNeeded(true))
689         {
690             lldb::SyntheticChildrenSP synthetic_sp = value_sp->GetSyntheticChildren();
691 
692             if (synthetic_sp && !synthetic_sp->IsScripted())
693             {
694                 TypeFilterImplSP filter_sp = std::static_pointer_cast<TypeFilterImpl>(synthetic_sp);
695                 filter.SetSP(filter_sp);
696             }
697         }
698     }
699     return filter;
700 }
701 
702 #ifndef LLDB_DISABLE_PYTHON
703 lldb::SBTypeSynthetic
704 SBValue::GetTypeSynthetic ()
705 {
706     lldb::SBTypeSynthetic synthetic;
707     ValueLocker locker;
708     lldb::ValueObjectSP value_sp(GetSP(locker));
709     if (value_sp)
710     {
711         if (value_sp->UpdateValueIfNeeded(true))
712         {
713             lldb::SyntheticChildrenSP children_sp = value_sp->GetSyntheticChildren();
714 
715             if (children_sp && children_sp->IsScripted())
716             {
717                 ScriptedSyntheticChildrenSP synth_sp = std::static_pointer_cast<ScriptedSyntheticChildren>(children_sp);
718                 synthetic.SetSP(synth_sp);
719             }
720         }
721     }
722     return synthetic;
723 }
724 #endif
725 
726 lldb::SBValue
727 SBValue::CreateChildAtOffset (const char *name, uint32_t offset, SBType type)
728 {
729     lldb::SBValue sb_value;
730     ValueLocker locker;
731     lldb::ValueObjectSP value_sp(GetSP(locker));
732     lldb::ValueObjectSP new_value_sp;
733     if (value_sp)
734     {
735         TypeImplSP type_sp (type.GetSP());
736         if (type.IsValid())
737         {
738             sb_value.SetSP(value_sp->GetSyntheticChildAtOffset(offset, type_sp->GetClangASTType(false), true),GetPreferDynamicValue(),GetPreferSyntheticValue(), name);
739         }
740     }
741     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
742     if (log)
743     {
744         if (new_value_sp)
745             log->Printf ("SBValue(%p)::CreateChildAtOffset => \"%s\"",
746                          static_cast<void*>(value_sp.get()),
747                          new_value_sp->GetName().AsCString());
748         else
749             log->Printf ("SBValue(%p)::CreateChildAtOffset => NULL",
750                          static_cast<void*>(value_sp.get()));
751     }
752     return sb_value;
753 }
754 
755 lldb::SBValue
756 SBValue::Cast (SBType type)
757 {
758     lldb::SBValue sb_value;
759     ValueLocker locker;
760     lldb::ValueObjectSP value_sp(GetSP(locker));
761     TypeImplSP type_sp (type.GetSP());
762     if (value_sp && type_sp)
763         sb_value.SetSP(value_sp->Cast(type_sp->GetClangASTType(false)),GetPreferDynamicValue(),GetPreferSyntheticValue());
764     return sb_value;
765 }
766 
767 lldb::SBValue
768 SBValue::CreateValueFromExpression (const char *name, const char* expression)
769 {
770     SBExpressionOptions options;
771     options.ref().SetKeepInMemory(true);
772     return CreateValueFromExpression (name, expression, options);
773 }
774 
775 lldb::SBValue
776 SBValue::CreateValueFromExpression (const char *name, const char *expression, SBExpressionOptions &options)
777 {
778     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
779     lldb::SBValue sb_value;
780     ValueLocker locker;
781     lldb::ValueObjectSP value_sp(GetSP(locker));
782     lldb::ValueObjectSP new_value_sp;
783     if (value_sp)
784     {
785         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
786         Target* target = exe_ctx.GetTargetPtr();
787         if (target)
788         {
789             options.ref().SetKeepInMemory(true);
790             target->EvaluateExpression (expression,
791                                         exe_ctx.GetFramePtr(),
792                                         new_value_sp,
793                                         options.ref());
794             if (new_value_sp)
795             {
796                 new_value_sp->SetName(ConstString(name));
797                 sb_value.SetSP(new_value_sp);
798             }
799         }
800     }
801     if (log)
802     {
803         if (new_value_sp)
804             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => SBValue (%p)",
805                          static_cast<void*>(value_sp.get()), name, expression,
806                          static_cast<void*>(new_value_sp.get()));
807         else
808             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => NULL",
809                          static_cast<void*>(value_sp.get()), name, expression);
810     }
811     return sb_value;
812 }
813 
814 lldb::SBValue
815 SBValue::CreateValueFromAddress(const char* name, lldb::addr_t address, SBType sb_type)
816 {
817     lldb::SBValue sb_value;
818     ValueLocker locker;
819     lldb::ValueObjectSP value_sp(GetSP(locker));
820     lldb::ValueObjectSP new_value_sp;
821     lldb::TypeImplSP type_impl_sp (sb_type.GetSP());
822     if (value_sp && type_impl_sp)
823     {
824         ClangASTType pointer_ast_type(type_impl_sp->GetClangASTType(false).GetPointerType ());
825         if (pointer_ast_type)
826         {
827             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
828 
829             ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
830             ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
831                                                                                pointer_ast_type,
832                                                                                ConstString(name),
833                                                                                buffer,
834                                                                                exe_ctx.GetByteOrder(),
835                                                                                exe_ctx.GetAddressByteSize()));
836 
837             if (ptr_result_valobj_sp)
838             {
839                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
840                 Error err;
841                 new_value_sp = ptr_result_valobj_sp->Dereference(err);
842                 if (new_value_sp)
843                     new_value_sp->SetName(ConstString(name));
844             }
845             sb_value.SetSP(new_value_sp);
846         }
847     }
848     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
849     if (log)
850     {
851         if (new_value_sp)
852             log->Printf ("SBValue(%p)::CreateValueFromAddress => \"%s\"",
853                          static_cast<void*>(value_sp.get()),
854                          new_value_sp->GetName().AsCString());
855         else
856             log->Printf ("SBValue(%p)::CreateValueFromAddress => NULL",
857                          static_cast<void*>(value_sp.get()));
858     }
859     return sb_value;
860 }
861 
862 lldb::SBValue
863 SBValue::CreateValueFromData (const char* name, SBData data, SBType type)
864 {
865     lldb::SBValue sb_value;
866     lldb::ValueObjectSP new_value_sp;
867     ValueLocker locker;
868     lldb::ValueObjectSP value_sp(GetSP(locker));
869     if (value_sp)
870     {
871         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
872 
873         new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
874                                                        type.m_opaque_sp->GetClangASTType(false),
875                                                        ConstString(name),
876                                                        *data.m_opaque_sp,
877                                                        LLDB_INVALID_ADDRESS);
878         new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
879         sb_value.SetSP(new_value_sp);
880     }
881     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
882     if (log)
883     {
884         if (new_value_sp)
885             log->Printf ("SBValue(%p)::CreateValueFromData => \"%s\"",
886                          static_cast<void*>(value_sp.get()),
887                          new_value_sp->GetName().AsCString());
888         else
889             log->Printf ("SBValue(%p)::CreateValueFromData => NULL",
890                          static_cast<void*>(value_sp.get()));
891     }
892     return sb_value;
893 }
894 
895 SBValue
896 SBValue::GetChildAtIndex (uint32_t idx)
897 {
898     const bool can_create_synthetic = false;
899     lldb::DynamicValueType use_dynamic = eNoDynamicValues;
900     TargetSP target_sp;
901     if (m_opaque_sp)
902         target_sp = m_opaque_sp->GetTargetSP();
903 
904     if (target_sp)
905         use_dynamic = target_sp->GetPreferDynamicValue();
906 
907     return GetChildAtIndex (idx, use_dynamic, can_create_synthetic);
908 }
909 
910 SBValue
911 SBValue::GetChildAtIndex (uint32_t idx, lldb::DynamicValueType use_dynamic, bool can_create_synthetic)
912 {
913     lldb::ValueObjectSP child_sp;
914     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
915 
916     ValueLocker locker;
917     lldb::ValueObjectSP value_sp(GetSP(locker));
918     if (value_sp)
919     {
920         const bool can_create = true;
921         child_sp = value_sp->GetChildAtIndex (idx, can_create);
922         if (can_create_synthetic && !child_sp)
923         {
924             if (value_sp->IsPointerType())
925             {
926                 child_sp = value_sp->GetSyntheticArrayMemberFromPointer(idx, can_create);
927             }
928             else if (value_sp->IsArrayType())
929             {
930                 child_sp = value_sp->GetSyntheticArrayMemberFromArray(idx, can_create);
931             }
932         }
933     }
934 
935     SBValue sb_value;
936     sb_value.SetSP (child_sp, use_dynamic, GetPreferSyntheticValue());
937     if (log)
938         log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)",
939                      static_cast<void*>(value_sp.get()), idx,
940                      static_cast<void*>(value_sp.get()));
941 
942     return sb_value;
943 }
944 
945 uint32_t
946 SBValue::GetIndexOfChildWithName (const char *name)
947 {
948     uint32_t idx = UINT32_MAX;
949     ValueLocker locker;
950     lldb::ValueObjectSP value_sp(GetSP(locker));
951     if (value_sp)
952     {
953         idx = value_sp->GetIndexOfChildWithName (ConstString(name));
954     }
955     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
956     if (log)
957     {
958         if (idx == UINT32_MAX)
959             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND",
960                          static_cast<void*>(value_sp.get()), name);
961         else
962             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u",
963                          static_cast<void*>(value_sp.get()), name, idx);
964     }
965     return idx;
966 }
967 
968 SBValue
969 SBValue::GetChildMemberWithName (const char *name)
970 {
971     lldb::DynamicValueType use_dynamic_value = eNoDynamicValues;
972     TargetSP target_sp;
973     if (m_opaque_sp)
974         target_sp = m_opaque_sp->GetTargetSP();
975 
976     if (target_sp)
977         use_dynamic_value = target_sp->GetPreferDynamicValue();
978     return GetChildMemberWithName (name, use_dynamic_value);
979 }
980 
981 SBValue
982 SBValue::GetChildMemberWithName (const char *name, lldb::DynamicValueType use_dynamic_value)
983 {
984     lldb::ValueObjectSP child_sp;
985     const ConstString str_name (name);
986 
987     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
988 
989     ValueLocker locker;
990     lldb::ValueObjectSP value_sp(GetSP(locker));
991     if (value_sp)
992     {
993         child_sp = value_sp->GetChildMemberWithName (str_name, true);
994     }
995 
996     SBValue sb_value;
997     sb_value.SetSP(child_sp, use_dynamic_value, GetPreferSyntheticValue());
998 
999     if (log)
1000         log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)",
1001                      static_cast<void*>(value_sp.get()), name,
1002                      static_cast<void*>(value_sp.get()));
1003 
1004     return sb_value;
1005 }
1006 
1007 lldb::SBValue
1008 SBValue::GetDynamicValue (lldb::DynamicValueType use_dynamic)
1009 {
1010     SBValue value_sb;
1011     if (IsValid())
1012     {
1013         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),use_dynamic,m_opaque_sp->GetUseSynthetic()));
1014         value_sb.SetSP(proxy_sp);
1015     }
1016     return value_sb;
1017 }
1018 
1019 lldb::SBValue
1020 SBValue::GetStaticValue ()
1021 {
1022     SBValue value_sb;
1023     if (IsValid())
1024     {
1025         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),eNoDynamicValues,m_opaque_sp->GetUseSynthetic()));
1026         value_sb.SetSP(proxy_sp);
1027     }
1028     return value_sb;
1029 }
1030 
1031 lldb::SBValue
1032 SBValue::GetNonSyntheticValue ()
1033 {
1034     SBValue value_sb;
1035     if (IsValid())
1036     {
1037         ValueImplSP proxy_sp(new ValueImpl(m_opaque_sp->GetRootSP(),m_opaque_sp->GetUseDynamic(),false));
1038         value_sb.SetSP(proxy_sp);
1039     }
1040     return value_sb;
1041 }
1042 
1043 lldb::DynamicValueType
1044 SBValue::GetPreferDynamicValue ()
1045 {
1046     if (!IsValid())
1047         return eNoDynamicValues;
1048     return m_opaque_sp->GetUseDynamic();
1049 }
1050 
1051 void
1052 SBValue::SetPreferDynamicValue (lldb::DynamicValueType use_dynamic)
1053 {
1054     if (IsValid())
1055         return m_opaque_sp->SetUseDynamic (use_dynamic);
1056 }
1057 
1058 bool
1059 SBValue::GetPreferSyntheticValue ()
1060 {
1061     if (!IsValid())
1062         return false;
1063     return m_opaque_sp->GetUseSynthetic();
1064 }
1065 
1066 void
1067 SBValue::SetPreferSyntheticValue (bool use_synthetic)
1068 {
1069     if (IsValid())
1070         return m_opaque_sp->SetUseSynthetic (use_synthetic);
1071 }
1072 
1073 bool
1074 SBValue::IsDynamic()
1075 {
1076     ValueLocker locker;
1077     lldb::ValueObjectSP value_sp(GetSP(locker));
1078     if (value_sp)
1079         return value_sp->IsDynamic();
1080     return false;
1081 }
1082 
1083 bool
1084 SBValue::IsSynthetic ()
1085 {
1086     ValueLocker locker;
1087     lldb::ValueObjectSP value_sp(GetSP(locker));
1088     if (value_sp)
1089         return value_sp->IsSynthetic();
1090     return false;
1091 }
1092 
1093 lldb::SBValue
1094 SBValue::GetValueForExpressionPath(const char* expr_path)
1095 {
1096     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1097     lldb::ValueObjectSP child_sp;
1098     ValueLocker locker;
1099     lldb::ValueObjectSP value_sp(GetSP(locker));
1100     if (value_sp)
1101     {
1102         // using default values for all the fancy options, just do it if you can
1103         child_sp = value_sp->GetValueForExpressionPath(expr_path);
1104     }
1105 
1106     SBValue sb_value;
1107     sb_value.SetSP(child_sp,GetPreferDynamicValue(),GetPreferSyntheticValue());
1108 
1109     if (log)
1110         log->Printf ("SBValue(%p)::GetValueForExpressionPath (expr_path=\"%s\") => SBValue(%p)",
1111                      static_cast<void*>(value_sp.get()), expr_path,
1112                      static_cast<void*>(value_sp.get()));
1113 
1114     return sb_value;
1115 }
1116 
1117 int64_t
1118 SBValue::GetValueAsSigned(SBError& error, int64_t fail_value)
1119 {
1120     error.Clear();
1121     ValueLocker locker;
1122     lldb::ValueObjectSP value_sp(GetSP(locker));
1123     if (value_sp)
1124     {
1125         bool success = true;
1126         uint64_t ret_val = fail_value;
1127         ret_val = value_sp->GetValueAsSigned(fail_value, &success);
1128         if (!success)
1129             error.SetErrorString("could not resolve value");
1130         return ret_val;
1131     }
1132     else
1133         error.SetErrorStringWithFormat ("could not get SBValue: %s", locker.GetError().AsCString());
1134 
1135     return fail_value;
1136 }
1137 
1138 uint64_t
1139 SBValue::GetValueAsUnsigned(SBError& error, uint64_t fail_value)
1140 {
1141     error.Clear();
1142     ValueLocker locker;
1143     lldb::ValueObjectSP value_sp(GetSP(locker));
1144     if (value_sp)
1145     {
1146         bool success = true;
1147         uint64_t ret_val = fail_value;
1148         ret_val = value_sp->GetValueAsUnsigned(fail_value, &success);
1149         if (!success)
1150             error.SetErrorString("could not resolve value");
1151         return ret_val;
1152     }
1153     else
1154         error.SetErrorStringWithFormat ("could not get SBValue: %s", locker.GetError().AsCString());
1155 
1156     return fail_value;
1157 }
1158 
1159 int64_t
1160 SBValue::GetValueAsSigned(int64_t fail_value)
1161 {
1162     ValueLocker locker;
1163     lldb::ValueObjectSP value_sp(GetSP(locker));
1164     if (value_sp)
1165     {
1166         return value_sp->GetValueAsSigned(fail_value);
1167     }
1168     return fail_value;
1169 }
1170 
1171 uint64_t
1172 SBValue::GetValueAsUnsigned(uint64_t fail_value)
1173 {
1174     ValueLocker locker;
1175     lldb::ValueObjectSP value_sp(GetSP(locker));
1176     if (value_sp)
1177     {
1178         return value_sp->GetValueAsUnsigned(fail_value);
1179     }
1180     return fail_value;
1181 }
1182 
1183 bool
1184 SBValue::MightHaveChildren ()
1185 {
1186     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1187     bool has_children = false;
1188     ValueLocker locker;
1189     lldb::ValueObjectSP value_sp(GetSP(locker));
1190     if (value_sp)
1191         has_children = value_sp->MightHaveChildren();
1192 
1193     if (log)
1194         log->Printf ("SBValue(%p)::MightHaveChildren() => %i",
1195                      static_cast<void*>(value_sp.get()), has_children);
1196     return has_children;
1197 }
1198 
1199 uint32_t
1200 SBValue::GetNumChildren ()
1201 {
1202     uint32_t num_children = 0;
1203 
1204     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1205     ValueLocker locker;
1206     lldb::ValueObjectSP value_sp(GetSP(locker));
1207     if (value_sp)
1208         num_children = value_sp->GetNumChildren();
1209 
1210     if (log)
1211         log->Printf ("SBValue(%p)::GetNumChildren () => %u",
1212                      static_cast<void*>(value_sp.get()), num_children);
1213 
1214     return num_children;
1215 }
1216 
1217 
1218 SBValue
1219 SBValue::Dereference ()
1220 {
1221     SBValue sb_value;
1222     ValueLocker locker;
1223     lldb::ValueObjectSP value_sp(GetSP(locker));
1224     if (value_sp)
1225     {
1226         Error error;
1227         sb_value = value_sp->Dereference (error);
1228     }
1229     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1230     if (log)
1231         log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)",
1232                      static_cast<void*>(value_sp.get()),
1233                      static_cast<void*>(value_sp.get()));
1234 
1235     return sb_value;
1236 }
1237 
1238 bool
1239 SBValue::TypeIsPointerType ()
1240 {
1241     bool is_ptr_type = false;
1242 
1243     ValueLocker locker;
1244     lldb::ValueObjectSP value_sp(GetSP(locker));
1245     if (value_sp)
1246         is_ptr_type = value_sp->IsPointerType();
1247 
1248     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1249     if (log)
1250         log->Printf ("SBValue(%p)::TypeIsPointerType () => %i",
1251                      static_cast<void*>(value_sp.get()), is_ptr_type);
1252 
1253     return is_ptr_type;
1254 }
1255 
1256 void *
1257 SBValue::GetOpaqueType()
1258 {
1259     ValueLocker locker;
1260     lldb::ValueObjectSP value_sp(GetSP(locker));
1261     if (value_sp)
1262         return value_sp->GetClangType().GetOpaqueQualType();
1263     return NULL;
1264 }
1265 
1266 lldb::SBTarget
1267 SBValue::GetTarget()
1268 {
1269     SBTarget sb_target;
1270     TargetSP target_sp;
1271     if (m_opaque_sp)
1272     {
1273         target_sp = m_opaque_sp->GetTargetSP();
1274         sb_target.SetSP (target_sp);
1275     }
1276     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1277     if (log)
1278     {
1279         if (target_sp.get() == NULL)
1280             log->Printf ("SBValue(%p)::GetTarget () => NULL",
1281                          static_cast<void*>(m_opaque_sp.get()));
1282         else
1283             log->Printf ("SBValue(%p)::GetTarget () => %p",
1284                          static_cast<void*>(m_opaque_sp.get()),
1285                          static_cast<void*>(target_sp.get()));
1286     }
1287     return sb_target;
1288 }
1289 
1290 lldb::SBProcess
1291 SBValue::GetProcess()
1292 {
1293     SBProcess sb_process;
1294     ProcessSP process_sp;
1295     if (m_opaque_sp)
1296     {
1297         process_sp = m_opaque_sp->GetProcessSP();
1298         sb_process.SetSP (process_sp);
1299     }
1300     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1301     if (log)
1302     {
1303         if (process_sp.get() == NULL)
1304             log->Printf ("SBValue(%p)::GetProcess () => NULL",
1305                          static_cast<void*>(m_opaque_sp.get()));
1306         else
1307             log->Printf ("SBValue(%p)::GetProcess () => %p",
1308                          static_cast<void*>(m_opaque_sp.get()),
1309                          static_cast<void*>(process_sp.get()));
1310     }
1311     return sb_process;
1312 }
1313 
1314 lldb::SBThread
1315 SBValue::GetThread()
1316 {
1317     SBThread sb_thread;
1318     ThreadSP thread_sp;
1319     if (m_opaque_sp)
1320     {
1321         thread_sp = m_opaque_sp->GetThreadSP();
1322         sb_thread.SetThread(thread_sp);
1323     }
1324     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1325     if (log)
1326     {
1327         if (thread_sp.get() == NULL)
1328             log->Printf ("SBValue(%p)::GetThread () => NULL",
1329                          static_cast<void*>(m_opaque_sp.get()));
1330         else
1331             log->Printf ("SBValue(%p)::GetThread () => %p",
1332                          static_cast<void*>(m_opaque_sp.get()),
1333                          static_cast<void*>(thread_sp.get()));
1334     }
1335     return sb_thread;
1336 }
1337 
1338 lldb::SBFrame
1339 SBValue::GetFrame()
1340 {
1341     SBFrame sb_frame;
1342     StackFrameSP frame_sp;
1343     if (m_opaque_sp)
1344     {
1345         frame_sp = m_opaque_sp->GetFrameSP();
1346         sb_frame.SetFrameSP (frame_sp);
1347     }
1348     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1349     if (log)
1350     {
1351         if (frame_sp.get() == NULL)
1352             log->Printf ("SBValue(%p)::GetFrame () => NULL",
1353                          static_cast<void*>(m_opaque_sp.get()));
1354         else
1355             log->Printf ("SBValue(%p)::GetFrame () => %p",
1356                          static_cast<void*>(m_opaque_sp.get()),
1357                          static_cast<void*>(frame_sp.get()));
1358     }
1359     return sb_frame;
1360 }
1361 
1362 
1363 lldb::ValueObjectSP
1364 SBValue::GetSP (ValueLocker &locker) const
1365 {
1366     if (!m_opaque_sp || !m_opaque_sp->IsValid())
1367         return ValueObjectSP();
1368     return locker.GetLockedSP(*m_opaque_sp.get());
1369 }
1370 
1371 lldb::ValueObjectSP
1372 SBValue::GetSP () const
1373 {
1374     ValueLocker locker;
1375     return GetSP(locker);
1376 }
1377 
1378 void
1379 SBValue::SetSP (ValueImplSP impl_sp)
1380 {
1381     m_opaque_sp = impl_sp;
1382 }
1383 
1384 void
1385 SBValue::SetSP (const lldb::ValueObjectSP &sp)
1386 {
1387     if (sp)
1388     {
1389         lldb::TargetSP target_sp(sp->GetTargetSP());
1390         if (target_sp)
1391         {
1392             lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue();
1393             bool use_synthetic = target_sp->TargetProperties::GetEnableSyntheticValue();
1394             m_opaque_sp = ValueImplSP(new ValueImpl(sp, use_dynamic, use_synthetic));
1395         }
1396         else
1397             m_opaque_sp = ValueImplSP(new ValueImpl(sp,eNoDynamicValues,true));
1398     }
1399     else
1400         m_opaque_sp = ValueImplSP(new ValueImpl(sp,eNoDynamicValues,false));
1401 }
1402 
1403 void
1404 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic)
1405 {
1406     if (sp)
1407     {
1408         lldb::TargetSP target_sp(sp->GetTargetSP());
1409         if (target_sp)
1410         {
1411             bool use_synthetic = target_sp->TargetProperties::GetEnableSyntheticValue();
1412             SetSP (sp, use_dynamic, use_synthetic);
1413         }
1414         else
1415             SetSP (sp, use_dynamic, true);
1416     }
1417     else
1418         SetSP (sp, use_dynamic, false);
1419 }
1420 
1421 void
1422 SBValue::SetSP (const lldb::ValueObjectSP &sp, bool use_synthetic)
1423 {
1424     if (sp)
1425     {
1426         lldb::TargetSP target_sp(sp->GetTargetSP());
1427         if (target_sp)
1428         {
1429             lldb::DynamicValueType use_dynamic = target_sp->GetPreferDynamicValue();
1430             SetSP (sp, use_dynamic, use_synthetic);
1431         }
1432         else
1433             SetSP (sp, eNoDynamicValues, use_synthetic);
1434     }
1435     else
1436         SetSP (sp, eNoDynamicValues, use_synthetic);
1437 }
1438 
1439 void
1440 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic, bool use_synthetic)
1441 {
1442     m_opaque_sp = ValueImplSP(new ValueImpl(sp,use_dynamic,use_synthetic));
1443 }
1444 
1445 void
1446 SBValue::SetSP (const lldb::ValueObjectSP &sp, lldb::DynamicValueType use_dynamic, bool use_synthetic, const char *name)
1447 {
1448     m_opaque_sp = ValueImplSP(new ValueImpl(sp,use_dynamic,use_synthetic, name));
1449 }
1450 
1451 bool
1452 SBValue::GetExpressionPath (SBStream &description)
1453 {
1454     ValueLocker locker;
1455     lldb::ValueObjectSP value_sp(GetSP(locker));
1456     if (value_sp)
1457     {
1458         value_sp->GetExpressionPath (description.ref(), false);
1459         return true;
1460     }
1461     return false;
1462 }
1463 
1464 bool
1465 SBValue::GetExpressionPath (SBStream &description, bool qualify_cxx_base_classes)
1466 {
1467     ValueLocker locker;
1468     lldb::ValueObjectSP value_sp(GetSP(locker));
1469     if (value_sp)
1470     {
1471         value_sp->GetExpressionPath (description.ref(), qualify_cxx_base_classes);
1472         return true;
1473     }
1474     return false;
1475 }
1476 
1477 bool
1478 SBValue::GetDescription (SBStream &description)
1479 {
1480     Stream &strm = description.ref();
1481 
1482     ValueLocker locker;
1483     lldb::ValueObjectSP value_sp(GetSP(locker));
1484     if (value_sp)
1485         value_sp->Dump(strm);
1486     else
1487         strm.PutCString ("No value");
1488 
1489     return true;
1490 }
1491 
1492 lldb::Format
1493 SBValue::GetFormat ()
1494 {
1495     ValueLocker locker;
1496     lldb::ValueObjectSP value_sp(GetSP(locker));
1497     if (value_sp)
1498         return value_sp->GetFormat();
1499     return eFormatDefault;
1500 }
1501 
1502 void
1503 SBValue::SetFormat (lldb::Format format)
1504 {
1505     ValueLocker locker;
1506     lldb::ValueObjectSP value_sp(GetSP(locker));
1507     if (value_sp)
1508         value_sp->SetFormat(format);
1509 }
1510 
1511 lldb::SBValue
1512 SBValue::AddressOf()
1513 {
1514     SBValue sb_value;
1515     ValueLocker locker;
1516     lldb::ValueObjectSP value_sp(GetSP(locker));
1517     if (value_sp)
1518     {
1519         Error error;
1520         sb_value.SetSP(value_sp->AddressOf (error),GetPreferDynamicValue(), GetPreferSyntheticValue());
1521     }
1522     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1523     if (log)
1524         log->Printf ("SBValue(%p)::AddressOf () => SBValue(%p)",
1525                      static_cast<void*>(value_sp.get()),
1526                      static_cast<void*>(value_sp.get()));
1527 
1528     return sb_value;
1529 }
1530 
1531 lldb::addr_t
1532 SBValue::GetLoadAddress()
1533 {
1534     lldb::addr_t value = LLDB_INVALID_ADDRESS;
1535     ValueLocker locker;
1536     lldb::ValueObjectSP value_sp(GetSP(locker));
1537     if (value_sp)
1538     {
1539         TargetSP target_sp (value_sp->GetTargetSP());
1540         if (target_sp)
1541         {
1542             const bool scalar_is_load_address = true;
1543             AddressType addr_type;
1544             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1545             if (addr_type == eAddressTypeFile)
1546             {
1547                 ModuleSP module_sp (value_sp->GetModule());
1548                 if (!module_sp)
1549                     value = LLDB_INVALID_ADDRESS;
1550                 else
1551                 {
1552                     Address addr;
1553                     module_sp->ResolveFileAddress(value, addr);
1554                     value = addr.GetLoadAddress(target_sp.get());
1555                 }
1556             }
1557             else if (addr_type == eAddressTypeHost || addr_type == eAddressTypeInvalid)
1558                 value = LLDB_INVALID_ADDRESS;
1559         }
1560     }
1561     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1562     if (log)
1563         log->Printf ("SBValue(%p)::GetLoadAddress () => (%" PRIu64 ")",
1564                      static_cast<void*>(value_sp.get()), value);
1565 
1566     return value;
1567 }
1568 
1569 lldb::SBAddress
1570 SBValue::GetAddress()
1571 {
1572     Address addr;
1573     ValueLocker locker;
1574     lldb::ValueObjectSP value_sp(GetSP(locker));
1575     if (value_sp)
1576     {
1577         TargetSP target_sp (value_sp->GetTargetSP());
1578         if (target_sp)
1579         {
1580             lldb::addr_t value = LLDB_INVALID_ADDRESS;
1581             const bool scalar_is_load_address = true;
1582             AddressType addr_type;
1583             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1584             if (addr_type == eAddressTypeFile)
1585             {
1586                 ModuleSP module_sp (value_sp->GetModule());
1587                 if (module_sp)
1588                     module_sp->ResolveFileAddress(value, addr);
1589             }
1590             else if (addr_type == eAddressTypeLoad)
1591             {
1592                 // no need to check the return value on this.. if it can actually do the resolve
1593                 // addr will be in the form (section,offset), otherwise it will simply be returned
1594                 // as (NULL, value)
1595                 addr.SetLoadAddress(value, target_sp.get());
1596             }
1597         }
1598     }
1599     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1600     if (log)
1601         log->Printf ("SBValue(%p)::GetAddress () => (%s,%" PRIu64 ")",
1602                      static_cast<void*>(value_sp.get()),
1603                      (addr.GetSection()
1604                         ? addr.GetSection()->GetName().GetCString()
1605                         : "NULL"),
1606                      addr.GetOffset());
1607     return SBAddress(new Address(addr));
1608 }
1609 
1610 lldb::SBData
1611 SBValue::GetPointeeData (uint32_t item_idx,
1612                          uint32_t item_count)
1613 {
1614     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1615     lldb::SBData sb_data;
1616     ValueLocker locker;
1617     lldb::ValueObjectSP value_sp(GetSP(locker));
1618     if (value_sp)
1619     {
1620         TargetSP target_sp (value_sp->GetTargetSP());
1621         if (target_sp)
1622         {
1623             DataExtractorSP data_sp(new DataExtractor());
1624             value_sp->GetPointeeData(*data_sp, item_idx, item_count);
1625             if (data_sp->GetByteSize() > 0)
1626                 *sb_data = data_sp;
1627         }
1628     }
1629     if (log)
1630         log->Printf ("SBValue(%p)::GetPointeeData (%d, %d) => SBData(%p)",
1631                      static_cast<void*>(value_sp.get()), item_idx, item_count,
1632                      static_cast<void*>(sb_data.get()));
1633 
1634     return sb_data;
1635 }
1636 
1637 lldb::SBData
1638 SBValue::GetData ()
1639 {
1640     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1641     lldb::SBData sb_data;
1642     ValueLocker locker;
1643     lldb::ValueObjectSP value_sp(GetSP(locker));
1644     if (value_sp)
1645     {
1646         DataExtractorSP data_sp(new DataExtractor());
1647         Error error;
1648         value_sp->GetData(*data_sp, error);
1649         if (error.Success())
1650             *sb_data = data_sp;
1651     }
1652     if (log)
1653         log->Printf ("SBValue(%p)::GetData () => SBData(%p)",
1654                      static_cast<void*>(value_sp.get()),
1655                      static_cast<void*>(sb_data.get()));
1656 
1657     return sb_data;
1658 }
1659 
1660 bool
1661 SBValue::SetData (lldb::SBData &data, SBError &error)
1662 {
1663     Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1664     ValueLocker locker;
1665     lldb::ValueObjectSP value_sp(GetSP(locker));
1666     bool ret = true;
1667 
1668     if (value_sp)
1669     {
1670         DataExtractor *data_extractor = data.get();
1671 
1672         if (!data_extractor)
1673         {
1674             if (log)
1675                 log->Printf ("SBValue(%p)::SetData() => error: no data to set",
1676                              static_cast<void*>(value_sp.get()));
1677 
1678             error.SetErrorString("No data to set");
1679             ret = false;
1680         }
1681         else
1682         {
1683             Error set_error;
1684 
1685             value_sp->SetData(*data_extractor, set_error);
1686 
1687             if (!set_error.Success())
1688             {
1689                 error.SetErrorStringWithFormat("Couldn't set data: %s", set_error.AsCString());
1690                 ret = false;
1691             }
1692         }
1693     }
1694     else
1695     {
1696         error.SetErrorStringWithFormat ("Couldn't set data: could not get SBValue: %s", locker.GetError().AsCString());
1697         ret = false;
1698     }
1699 
1700     if (log)
1701         log->Printf ("SBValue(%p)::SetData (%p) => %s",
1702                      static_cast<void*>(value_sp.get()),
1703                      static_cast<void*>(data.get()), ret ? "true" : "false");
1704     return ret;
1705 }
1706 
1707 lldb::SBDeclaration
1708 SBValue::GetDeclaration ()
1709 {
1710     ValueLocker locker;
1711     lldb::ValueObjectSP value_sp(GetSP(locker));
1712     SBDeclaration decl_sb;
1713     if (value_sp)
1714     {
1715         Declaration decl;
1716         if (value_sp->GetDeclaration(decl))
1717             decl_sb.SetDeclaration(decl);
1718     }
1719     return decl_sb;
1720 }
1721 
1722 lldb::SBWatchpoint
1723 SBValue::Watch (bool resolve_location, bool read, bool write, SBError &error)
1724 {
1725     SBWatchpoint sb_watchpoint;
1726 
1727     // If the SBValue is not valid, there's no point in even trying to watch it.
1728     ValueLocker locker;
1729     lldb::ValueObjectSP value_sp(GetSP(locker));
1730     TargetSP target_sp (GetTarget().GetSP());
1731     if (value_sp && target_sp)
1732     {
1733         // Read and Write cannot both be false.
1734         if (!read && !write)
1735             return sb_watchpoint;
1736 
1737         // If the value is not in scope, don't try and watch and invalid value
1738         if (!IsInScope())
1739             return sb_watchpoint;
1740 
1741         addr_t addr = GetLoadAddress();
1742         if (addr == LLDB_INVALID_ADDRESS)
1743             return sb_watchpoint;
1744         size_t byte_size = GetByteSize();
1745         if (byte_size == 0)
1746             return sb_watchpoint;
1747 
1748         uint32_t watch_type = 0;
1749         if (read)
1750             watch_type |= LLDB_WATCH_TYPE_READ;
1751         if (write)
1752             watch_type |= LLDB_WATCH_TYPE_WRITE;
1753 
1754         Error rc;
1755         ClangASTType type (value_sp->GetClangType());
1756         WatchpointSP watchpoint_sp = target_sp->CreateWatchpoint(addr, byte_size, &type, watch_type, rc);
1757         error.SetError(rc);
1758 
1759         if (watchpoint_sp)
1760         {
1761             sb_watchpoint.SetSP (watchpoint_sp);
1762             Declaration decl;
1763             if (value_sp->GetDeclaration (decl))
1764             {
1765                 if (decl.GetFile())
1766                 {
1767                     StreamString ss;
1768                     // True to show fullpath for declaration file.
1769                     decl.DumpStopContext(&ss, true);
1770                     watchpoint_sp->SetDeclInfo(ss.GetString());
1771                 }
1772             }
1773         }
1774     }
1775     else if (target_sp)
1776     {
1777         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1778         if (log)
1779             log->Printf ("SBValue(%p)::Watch() => error getting SBValue: %s",
1780                          static_cast<void*>(value_sp.get()),
1781                          locker.GetError().AsCString());
1782 
1783         error.SetErrorStringWithFormat("could not get SBValue: %s", locker.GetError().AsCString());
1784     }
1785     else
1786     {
1787         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1788         if (log)
1789             log->Printf ("SBValue(%p)::Watch() => error getting SBValue: no target",
1790                          static_cast<void*>(value_sp.get()));
1791         error.SetErrorString("could not set watchpoint, a target is required");
1792     }
1793 
1794     return sb_watchpoint;
1795 }
1796 
1797 // FIXME: Remove this method impl (as well as the decl in .h) once it is no longer needed.
1798 // Backward compatibility fix in the interim.
1799 lldb::SBWatchpoint
1800 SBValue::Watch (bool resolve_location, bool read, bool write)
1801 {
1802     SBError error;
1803     return Watch(resolve_location, read, write, error);
1804 }
1805 
1806 lldb::SBWatchpoint
1807 SBValue::WatchPointee (bool resolve_location, bool read, bool write, SBError &error)
1808 {
1809     SBWatchpoint sb_watchpoint;
1810     if (IsInScope() && GetType().IsPointerType())
1811         sb_watchpoint = Dereference().Watch (resolve_location, read, write, error);
1812     return sb_watchpoint;
1813 }
1814