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