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