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