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