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