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