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