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