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