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 () => %llu", value_sp.get(), (uint64_t)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::EvaluateExpressionOptions options;
736                 options.SetKeepInMemory(true);
737                 target->EvaluateExpression (expression,
738                                             exe_ctx.GetFramePtr(),
739                                             new_value_sp,
740                                             options);
741                 if (new_value_sp)
742                 {
743                     new_value_sp->SetName(ConstString(name));
744                     sb_value.SetSP(new_value_sp);
745                 }
746             }
747         }
748     }
749     if (log)
750     {
751         if (new_value_sp)
752             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => SBValue (%p)",
753                          value_sp.get(),
754                          name,
755                          expression,
756                          new_value_sp.get());
757         else
758             log->Printf ("SBValue(%p)::CreateValueFromExpression(name=\"%s\", expression=\"%s\") => NULL",
759                          value_sp.get(),
760                          name,
761                          expression);
762     }
763     return sb_value;
764 }
765 
766 lldb::SBValue
767 SBValue::CreateValueFromAddress(const char* name, lldb::addr_t address, SBType sb_type)
768 {
769     lldb::SBValue sb_value;
770     lldb::ValueObjectSP value_sp(GetSP());
771     lldb::ValueObjectSP new_value_sp;
772     lldb::TypeImplSP type_impl_sp (sb_type.GetSP());
773     if (value_sp && type_impl_sp)
774     {
775         ClangASTType pointee_ast_type(type_impl_sp->GetASTContext(), type_impl_sp->GetClangASTType().GetPointerType ());
776         lldb::TypeImplSP pointee_type_impl_sp (new TypeImpl(pointee_ast_type));
777         if (pointee_type_impl_sp)
778         {
779 
780             lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
781 
782             ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
783             ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
784                                                                                pointee_type_impl_sp->GetASTContext(),
785                                                                                pointee_type_impl_sp->GetOpaqueQualType(),
786                                                                                ConstString(name),
787                                                                                buffer,
788                                                                                lldb::endian::InlHostByteOrder(),
789                                                                                exe_ctx.GetAddressByteSize()));
790 
791             if (ptr_result_valobj_sp)
792             {
793                 ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
794                 Error err;
795                 new_value_sp = ptr_result_valobj_sp->Dereference(err);
796                 if (new_value_sp)
797                     new_value_sp->SetName(ConstString(name));
798             }
799             sb_value.SetSP(new_value_sp);
800         }
801     }
802     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
803     if (log)
804     {
805         if (new_value_sp)
806             log->Printf ("SBValue(%p)::CreateValueFromAddress => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
807         else
808             log->Printf ("SBValue(%p)::CreateValueFromAddress => NULL", value_sp.get());
809     }
810     return sb_value;
811 }
812 
813 lldb::SBValue
814 SBValue::CreateValueFromData (const char* name, SBData data, SBType type)
815 {
816     lldb::SBValue sb_value;
817     lldb::ValueObjectSP new_value_sp;
818     lldb::ValueObjectSP value_sp(GetSP());
819     if (value_sp)
820     {
821         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
822 
823         new_value_sp = ValueObjectConstResult::Create (exe_ctx.GetBestExecutionContextScope(),
824                                                        type.m_opaque_sp->GetASTContext() ,
825                                                        type.m_opaque_sp->GetOpaqueQualType(),
826                                                        ConstString(name),
827                                                        *data.m_opaque_sp,
828                                                        LLDB_INVALID_ADDRESS);
829         new_value_sp->SetAddressTypeOfChildren(eAddressTypeLoad);
830         sb_value.SetSP(new_value_sp);
831     }
832     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
833     if (log)
834     {
835         if (new_value_sp)
836             log->Printf ("SBValue(%p)::CreateValueFromData => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
837         else
838             log->Printf ("SBValue(%p)::CreateValueFromData => NULL", value_sp.get());
839     }
840     return sb_value;
841 }
842 
843 SBValue
844 SBValue::GetChildAtIndex (uint32_t idx)
845 {
846     const bool can_create_synthetic = false;
847     lldb::DynamicValueType use_dynamic = eNoDynamicValues;
848     lldb::ValueObjectSP value_sp(GetSP());
849     if (value_sp)
850     {
851         TargetSP target_sp(value_sp->GetTargetSP());
852         if (target_sp)
853             use_dynamic = target_sp->GetPreferDynamicValue();
854     }
855     return GetChildAtIndex (idx, use_dynamic, can_create_synthetic);
856 }
857 
858 SBValue
859 SBValue::GetChildAtIndex (uint32_t idx, lldb::DynamicValueType use_dynamic, bool can_create_synthetic)
860 {
861     lldb::ValueObjectSP child_sp;
862     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
863 
864     lldb::ValueObjectSP value_sp(GetSP());
865     if (value_sp)
866     {
867         ProcessSP process_sp(value_sp->GetProcessSP());
868         Process::StopLocker stop_locker;
869         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
870         {
871             if (log)
872                 log->Printf ("SBValue(%p)::GetChildAtIndex() => error: process is running", value_sp.get());
873         }
874         else
875         {
876             TargetSP target_sp(value_sp->GetTargetSP());
877             if (target_sp)
878             {
879                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
880                 const bool can_create = true;
881                 child_sp = value_sp->GetChildAtIndex (idx, can_create);
882                 if (can_create_synthetic && !child_sp)
883                 {
884                     if (value_sp->IsPointerType())
885                     {
886                         child_sp = value_sp->GetSyntheticArrayMemberFromPointer(idx, can_create);
887                     }
888                     else if (value_sp->IsArrayType())
889                     {
890                         child_sp = value_sp->GetSyntheticArrayMemberFromArray(idx, can_create);
891                     }
892                 }
893 
894                 if (child_sp)
895                 {
896                     if (use_dynamic != lldb::eNoDynamicValues)
897                     {
898                         lldb::ValueObjectSP dynamic_sp(child_sp->GetDynamicValue (use_dynamic));
899                         if (dynamic_sp)
900                             child_sp = dynamic_sp;
901                     }
902                 }
903             }
904         }
905     }
906 
907     SBValue sb_value (child_sp);
908     if (log)
909         log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)", value_sp.get(), idx, value_sp.get());
910 
911     return sb_value;
912 }
913 
914 uint32_t
915 SBValue::GetIndexOfChildWithName (const char *name)
916 {
917     uint32_t idx = UINT32_MAX;
918     lldb::ValueObjectSP value_sp(GetSP());
919     if (value_sp)
920     {
921         TargetSP target_sp(value_sp->GetTargetSP());
922         if (target_sp)
923         {
924             Mutex::Locker api_locker (target_sp->GetAPIMutex());
925 
926             idx = value_sp->GetIndexOfChildWithName (ConstString(name));
927         }
928     }
929     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
930     if (log)
931     {
932         if (idx == UINT32_MAX)
933             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND", value_sp.get(), name);
934         else
935             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u", value_sp.get(), name, idx);
936     }
937     return idx;
938 }
939 
940 SBValue
941 SBValue::GetChildMemberWithName (const char *name)
942 {
943     lldb::ValueObjectSP value_sp(GetSP());
944     if (value_sp)
945     {
946         lldb::DynamicValueType use_dynamic_value = eNoDynamicValues;
947         TargetSP target_sp(value_sp->GetTargetSP());
948         if (target_sp)
949         {
950             Mutex::Locker api_locker (target_sp->GetAPIMutex());
951             use_dynamic_value = target_sp->GetPreferDynamicValue();
952         }
953         return GetChildMemberWithName (name, use_dynamic_value);
954     }
955     return SBValue();
956 }
957 
958 SBValue
959 SBValue::GetChildMemberWithName (const char *name, lldb::DynamicValueType use_dynamic_value)
960 {
961     lldb::ValueObjectSP child_sp;
962     const ConstString str_name (name);
963 
964     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
965 
966     lldb::ValueObjectSP value_sp(GetSP());
967     if (value_sp)
968     {
969         ProcessSP process_sp(value_sp->GetProcessSP());
970         Process::StopLocker stop_locker;
971         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
972         {
973             if (log)
974                 log->Printf ("SBValue(%p)::GetChildMemberWithName() => error: process is running", value_sp.get());
975         }
976         else
977         {
978             TargetSP target_sp(value_sp->GetTargetSP());
979             if (target_sp)
980             {
981                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
982                 child_sp = value_sp->GetChildMemberWithName (str_name, true);
983                 if (use_dynamic_value != lldb::eNoDynamicValues)
984                 {
985                     if (child_sp)
986                     {
987                         lldb::ValueObjectSP dynamic_sp = child_sp->GetDynamicValue (use_dynamic_value);
988                         if (dynamic_sp)
989                             child_sp = dynamic_sp;
990                     }
991                 }
992             }
993         }
994     }
995 
996     SBValue sb_value (child_sp);
997 
998     if (log)
999         log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)", value_sp.get(), name, value_sp.get());
1000 
1001     return sb_value;
1002 }
1003 
1004 lldb::SBValue
1005 SBValue::GetDynamicValue (lldb::DynamicValueType use_dynamic)
1006 {
1007     lldb::ValueObjectSP value_sp(GetSP());
1008     if (value_sp)
1009     {
1010         ProcessSP process_sp(value_sp->GetProcessSP());
1011         Process::StopLocker stop_locker;
1012         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1013         {
1014             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1015             if (log)
1016                 log->Printf ("SBValue(%p)::GetDynamicValue() => error: process is running", value_sp.get());
1017         }
1018         else
1019         {
1020             TargetSP target_sp(value_sp->GetTargetSP());
1021             if (target_sp)
1022             {
1023                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1024                 return SBValue (value_sp->GetDynamicValue(use_dynamic));
1025             }
1026         }
1027     }
1028 
1029     return SBValue();
1030 }
1031 
1032 lldb::SBValue
1033 SBValue::GetStaticValue ()
1034 {
1035     lldb::ValueObjectSP value_sp(GetSP());
1036     if (value_sp)
1037     {
1038         TargetSP target_sp(value_sp->GetTargetSP());
1039         if (target_sp)
1040         {
1041             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1042             return SBValue(value_sp->GetStaticValue());
1043         }
1044     }
1045 
1046     return SBValue();
1047 }
1048 
1049 lldb::SBValue
1050 SBValue::GetNonSyntheticValue ()
1051 {
1052     SBValue sb_value;
1053     lldb::ValueObjectSP value_sp(GetSP());
1054     if (value_sp)
1055     {
1056         if (value_sp->IsSynthetic())
1057         {
1058             TargetSP target_sp(value_sp->GetTargetSP());
1059             if (target_sp)
1060             {
1061                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1062                 // deliberately breaking the rules here to optimize the case where we DO NOT want
1063                 // the synthetic value to be returned to the user - if we did not do this, we would have to tell
1064                 // the target to suppress the synthetic value, and then return the flag to its original value
1065                 if (value_sp->GetNonSyntheticValue())
1066                     sb_value.m_opaque_sp = value_sp->GetNonSyntheticValue();
1067             }
1068         }
1069     }
1070     return sb_value;
1071 }
1072 
1073 bool
1074 SBValue::IsDynamic()
1075 {
1076     lldb::ValueObjectSP value_sp(GetSP());
1077     if (value_sp)
1078     {
1079         TargetSP target_sp(value_sp->GetTargetSP());
1080         if (target_sp)
1081         {
1082             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1083             return value_sp->IsDynamic();
1084         }
1085     }
1086     return false;
1087 }
1088 
1089 lldb::SBValue
1090 SBValue::GetValueForExpressionPath(const char* expr_path)
1091 {
1092     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1093     lldb::ValueObjectSP child_sp;
1094     lldb::ValueObjectSP value_sp(GetSP());
1095     if (value_sp)
1096     {
1097         ProcessSP process_sp(value_sp->GetProcessSP());
1098         Process::StopLocker stop_locker;
1099         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1100         {
1101             if (log)
1102                 log->Printf ("SBValue(%p)::GetValueForExpressionPath() => error: process is running", value_sp.get());
1103         }
1104         else
1105         {
1106             TargetSP target_sp(value_sp->GetTargetSP());
1107             if (target_sp)
1108             {
1109                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1110                 // using default values for all the fancy options, just do it if you can
1111                 child_sp = value_sp->GetValueForExpressionPath(expr_path);
1112             }
1113         }
1114     }
1115 
1116     SBValue sb_value (child_sp);
1117 
1118     if (log)
1119         log->Printf ("SBValue(%p)::GetValueForExpressionPath (expr_path=\"%s\") => SBValue(%p)", value_sp.get(), expr_path, value_sp.get());
1120 
1121     return sb_value;
1122 }
1123 
1124 int64_t
1125 SBValue::GetValueAsSigned(SBError& error, int64_t fail_value)
1126 {
1127     error.Clear();
1128     lldb::ValueObjectSP value_sp(GetSP());
1129     if (value_sp)
1130     {
1131         ProcessSP process_sp(value_sp->GetProcessSP());
1132         Process::StopLocker stop_locker;
1133         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1134         {
1135             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1136             if (log)
1137                 log->Printf ("SBValue(%p)::GetValueAsSigned() => error: process is running", value_sp.get());
1138             error.SetErrorString("process is running");
1139         }
1140         else
1141         {
1142             TargetSP target_sp(value_sp->GetTargetSP());
1143             if (target_sp)
1144             {
1145                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1146                 Scalar scalar;
1147                 if (value_sp->ResolveValue (scalar))
1148                     return scalar.GetRawBits64(fail_value);
1149                 else
1150                     error.SetErrorString("could not get value");
1151             }
1152             else
1153                 error.SetErrorString("could not get target");
1154         }
1155     }
1156     error.SetErrorString("invalid SBValue");
1157     return fail_value;
1158 }
1159 
1160 uint64_t
1161 SBValue::GetValueAsUnsigned(SBError& error, uint64_t fail_value)
1162 {
1163     error.Clear();
1164     lldb::ValueObjectSP value_sp(GetSP());
1165     if (value_sp)
1166     {
1167         ProcessSP process_sp(value_sp->GetProcessSP());
1168         Process::StopLocker stop_locker;
1169         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1170         {
1171             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1172             if (log)
1173                 log->Printf ("SBValue(%p)::GetValueAsUnsigned() => error: process is running", value_sp.get());
1174             error.SetErrorString("process is running");
1175         }
1176         else
1177         {
1178             TargetSP target_sp(value_sp->GetTargetSP());
1179             if (target_sp)
1180             {
1181                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1182                 Scalar scalar;
1183                 if (value_sp->ResolveValue (scalar))
1184                     return scalar.GetRawBits64(fail_value);
1185                 else
1186                     error.SetErrorString("could not get value");
1187             }
1188             else
1189                 error.SetErrorString("could not get target");
1190         }
1191     }
1192     error.SetErrorString("invalid SBValue");
1193     return fail_value;
1194 }
1195 
1196 int64_t
1197 SBValue::GetValueAsSigned(int64_t fail_value)
1198 {
1199     lldb::ValueObjectSP value_sp(GetSP());
1200     if (value_sp)
1201     {
1202         ProcessSP process_sp(value_sp->GetProcessSP());
1203         Process::StopLocker stop_locker;
1204         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1205         {
1206             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1207             if (log)
1208                 log->Printf ("SBValue(%p)::GetValueAsSigned() => error: process is running", value_sp.get());
1209         }
1210         else
1211         {
1212             TargetSP target_sp(value_sp->GetTargetSP());
1213             if (target_sp)
1214             {
1215                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1216                 Scalar scalar;
1217                 if (value_sp->ResolveValue (scalar))
1218                     return scalar.GetRawBits64(fail_value);
1219             }
1220         }
1221     }
1222     return fail_value;
1223 }
1224 
1225 uint64_t
1226 SBValue::GetValueAsUnsigned(uint64_t fail_value)
1227 {
1228     lldb::ValueObjectSP value_sp(GetSP());
1229     if (value_sp)
1230     {
1231         ProcessSP process_sp(value_sp->GetProcessSP());
1232         Process::StopLocker stop_locker;
1233         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1234         {
1235             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1236             if (log)
1237                 log->Printf ("SBValue(%p)::GetValueAsUnsigned() => error: process is running", value_sp.get());
1238         }
1239         else
1240         {
1241             TargetSP target_sp(value_sp->GetTargetSP());
1242             if (target_sp)
1243             {
1244                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1245                 Scalar scalar;
1246                 if (value_sp->ResolveValue (scalar))
1247                     return scalar.GetRawBits64(fail_value);
1248             }
1249         }
1250     }
1251     return fail_value;
1252 }
1253 
1254 uint32_t
1255 SBValue::GetNumChildren ()
1256 {
1257     uint32_t num_children = 0;
1258 
1259     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1260     lldb::ValueObjectSP value_sp(GetSP());
1261     if (value_sp)
1262     {
1263         ProcessSP process_sp(value_sp->GetProcessSP());
1264         Process::StopLocker stop_locker;
1265         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1266         {
1267             if (log)
1268                 log->Printf ("SBValue(%p)::GetNumChildren() => error: process is running", value_sp.get());
1269         }
1270         else
1271         {
1272             TargetSP target_sp(value_sp->GetTargetSP());
1273             if (target_sp)
1274             {
1275                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1276 
1277                 num_children = value_sp->GetNumChildren();
1278             }
1279         }
1280     }
1281 
1282     if (log)
1283         log->Printf ("SBValue(%p)::GetNumChildren () => %u", value_sp.get(), num_children);
1284 
1285     return num_children;
1286 }
1287 
1288 
1289 SBValue
1290 SBValue::Dereference ()
1291 {
1292     SBValue sb_value;
1293     lldb::ValueObjectSP value_sp(GetSP());
1294     if (value_sp)
1295     {
1296         TargetSP target_sp(value_sp->GetTargetSP());
1297         if (target_sp)
1298         {
1299             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1300 
1301             Error error;
1302             sb_value = value_sp->Dereference (error);
1303         }
1304     }
1305     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1306     if (log)
1307         log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)", value_sp.get(), value_sp.get());
1308 
1309     return sb_value;
1310 }
1311 
1312 bool
1313 SBValue::TypeIsPointerType ()
1314 {
1315     bool is_ptr_type = false;
1316 
1317     lldb::ValueObjectSP value_sp(GetSP());
1318     if (value_sp)
1319     {
1320         TargetSP target_sp(value_sp->GetTargetSP());
1321         if (target_sp)
1322         {
1323             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1324 
1325             is_ptr_type = value_sp->IsPointerType();
1326         }
1327     }
1328 
1329     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1330     if (log)
1331         log->Printf ("SBValue(%p)::TypeIsPointerType () => %i", value_sp.get(), is_ptr_type);
1332 
1333 
1334     return is_ptr_type;
1335 }
1336 
1337 void *
1338 SBValue::GetOpaqueType()
1339 {
1340     lldb::ValueObjectSP value_sp(GetSP());
1341     if (value_sp)
1342     {
1343         TargetSP target_sp(value_sp->GetTargetSP());
1344         if (target_sp)
1345         {
1346             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1347 
1348             return value_sp->GetClangType();
1349         }
1350     }
1351     return NULL;
1352 }
1353 
1354 lldb::SBTarget
1355 SBValue::GetTarget()
1356 {
1357     SBTarget sb_target;
1358     TargetSP target_sp;
1359     lldb::ValueObjectSP value_sp(GetSP());
1360     if (value_sp)
1361     {
1362         target_sp = value_sp->GetTargetSP();
1363         sb_target.SetSP (target_sp);
1364     }
1365     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1366     if (log)
1367     {
1368         if (target_sp.get() == NULL)
1369             log->Printf ("SBValue(%p)::GetTarget () => NULL", value_sp.get());
1370         else
1371             log->Printf ("SBValue(%p)::GetTarget () => %p", value_sp.get(), target_sp.get());
1372     }
1373     return sb_target;
1374 }
1375 
1376 lldb::SBProcess
1377 SBValue::GetProcess()
1378 {
1379     SBProcess sb_process;
1380     ProcessSP process_sp;
1381     lldb::ValueObjectSP value_sp(GetSP());
1382     if (value_sp)
1383     {
1384         process_sp = value_sp->GetProcessSP();
1385         if (process_sp)
1386             sb_process.SetSP (process_sp);
1387     }
1388     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1389     if (log)
1390     {
1391         if (process_sp.get() == NULL)
1392             log->Printf ("SBValue(%p)::GetProcess () => NULL", value_sp.get());
1393         else
1394             log->Printf ("SBValue(%p)::GetProcess () => %p", value_sp.get(), process_sp.get());
1395     }
1396     return sb_process;
1397 }
1398 
1399 lldb::SBThread
1400 SBValue::GetThread()
1401 {
1402     SBThread sb_thread;
1403     ThreadSP thread_sp;
1404     lldb::ValueObjectSP value_sp(GetSP());
1405     if (value_sp)
1406     {
1407         thread_sp = value_sp->GetThreadSP();
1408         sb_thread.SetThread(thread_sp);
1409     }
1410     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1411     if (log)
1412     {
1413         if (thread_sp.get() == NULL)
1414             log->Printf ("SBValue(%p)::GetThread () => NULL", value_sp.get());
1415         else
1416             log->Printf ("SBValue(%p)::GetThread () => %p", value_sp.get(), thread_sp.get());
1417     }
1418     return sb_thread;
1419 }
1420 
1421 lldb::SBFrame
1422 SBValue::GetFrame()
1423 {
1424     SBFrame sb_frame;
1425     StackFrameSP frame_sp;
1426     lldb::ValueObjectSP value_sp(GetSP());
1427     if (value_sp)
1428     {
1429         frame_sp = value_sp->GetFrameSP();
1430         sb_frame.SetFrameSP (frame_sp);
1431     }
1432     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1433     if (log)
1434     {
1435         if (frame_sp.get() == NULL)
1436             log->Printf ("SBValue(%p)::GetFrame () => NULL", value_sp.get());
1437         else
1438             log->Printf ("SBValue(%p)::GetFrame () => %p", value_sp.get(), frame_sp.get());
1439     }
1440     return sb_frame;
1441 }
1442 
1443 
1444 lldb::ValueObjectSP
1445 SBValue::GetSP () const
1446 {
1447     return m_opaque_sp;
1448 }
1449 
1450 void
1451 SBValue::SetSP (const lldb::ValueObjectSP &sp)
1452 {
1453     m_opaque_sp = sp;
1454     if (IsValid() && m_opaque_sp->HasSyntheticValue())
1455         m_opaque_sp = m_opaque_sp->GetSyntheticValue();
1456 }
1457 
1458 
1459 bool
1460 SBValue::GetExpressionPath (SBStream &description)
1461 {
1462     lldb::ValueObjectSP value_sp(GetSP());
1463     if (value_sp)
1464     {
1465         value_sp->GetExpressionPath (description.ref(), false);
1466         return true;
1467     }
1468     return false;
1469 }
1470 
1471 bool
1472 SBValue::GetExpressionPath (SBStream &description, bool qualify_cxx_base_classes)
1473 {
1474     lldb::ValueObjectSP value_sp(GetSP());
1475     if (value_sp)
1476     {
1477         value_sp->GetExpressionPath (description.ref(), qualify_cxx_base_classes);
1478         return true;
1479     }
1480     return false;
1481 }
1482 
1483 bool
1484 SBValue::GetDescription (SBStream &description)
1485 {
1486     Stream &strm = description.ref();
1487 
1488     lldb::ValueObjectSP value_sp(GetSP());
1489     if (value_sp)
1490     {
1491         ProcessSP process_sp(value_sp->GetProcessSP());
1492         Process::StopLocker stop_locker;
1493         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1494         {
1495             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1496             if (log)
1497                 log->Printf ("SBValue(%p)::GetDescription() => error: process is running", value_sp.get());
1498         }
1499         else
1500         {
1501             ValueObject::DumpValueObject (strm, value_sp.get());
1502         }
1503     }
1504     else
1505         strm.PutCString ("No value");
1506 
1507     return true;
1508 }
1509 
1510 lldb::Format
1511 SBValue::GetFormat ()
1512 {
1513     lldb::ValueObjectSP value_sp(GetSP());
1514     if (value_sp)
1515         return value_sp->GetFormat();
1516     return eFormatDefault;
1517 }
1518 
1519 void
1520 SBValue::SetFormat (lldb::Format format)
1521 {
1522     lldb::ValueObjectSP value_sp(GetSP());
1523     if (value_sp)
1524         value_sp->SetFormat(format);
1525 }
1526 
1527 lldb::SBValue
1528 SBValue::AddressOf()
1529 {
1530     SBValue sb_value;
1531     lldb::ValueObjectSP value_sp(GetSP());
1532     if (value_sp)
1533     {
1534         TargetSP target_sp (value_sp->GetTargetSP());
1535         if (target_sp)
1536         {
1537             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1538             Error error;
1539             sb_value = value_sp->AddressOf (error);
1540         }
1541     }
1542     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1543     if (log)
1544         log->Printf ("SBValue(%p)::AddressOf () => SBValue(%p)", value_sp.get(), value_sp.get());
1545 
1546     return sb_value;
1547 }
1548 
1549 lldb::addr_t
1550 SBValue::GetLoadAddress()
1551 {
1552     lldb::addr_t value = LLDB_INVALID_ADDRESS;
1553     lldb::ValueObjectSP value_sp(GetSP());
1554     if (value_sp)
1555     {
1556         TargetSP target_sp (value_sp->GetTargetSP());
1557         if (target_sp)
1558         {
1559             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1560             const bool scalar_is_load_address = true;
1561             AddressType addr_type;
1562             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1563             if (addr_type == eAddressTypeFile)
1564             {
1565                 ModuleSP module_sp (value_sp->GetModule());
1566                 if (!module_sp)
1567                     value = LLDB_INVALID_ADDRESS;
1568                 else
1569                 {
1570                     Address addr;
1571                     module_sp->ResolveFileAddress(value, addr);
1572                     value = addr.GetLoadAddress(target_sp.get());
1573                 }
1574             }
1575             else if (addr_type == eAddressTypeHost || addr_type == eAddressTypeInvalid)
1576                 value = LLDB_INVALID_ADDRESS;
1577         }
1578     }
1579     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1580     if (log)
1581         log->Printf ("SBValue(%p)::GetLoadAddress () => (%llu)", value_sp.get(), value);
1582 
1583     return value;
1584 }
1585 
1586 lldb::SBAddress
1587 SBValue::GetAddress()
1588 {
1589     Address addr;
1590     lldb::ValueObjectSP value_sp(GetSP());
1591     if (value_sp)
1592     {
1593         TargetSP target_sp (value_sp->GetTargetSP());
1594         if (target_sp)
1595         {
1596             lldb::addr_t value = LLDB_INVALID_ADDRESS;
1597             Mutex::Locker api_locker (target_sp->GetAPIMutex());
1598             const bool scalar_is_load_address = true;
1599             AddressType addr_type;
1600             value = value_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1601             if (addr_type == eAddressTypeFile)
1602             {
1603                 ModuleSP module_sp (value_sp->GetModule());
1604                 if (module_sp)
1605                     module_sp->ResolveFileAddress(value, addr);
1606             }
1607             else if (addr_type == eAddressTypeLoad)
1608             {
1609                 // no need to check the return value on this.. if it can actually do the resolve
1610                 // addr will be in the form (section,offset), otherwise it will simply be returned
1611                 // as (NULL, value)
1612                 addr.SetLoadAddress(value, target_sp.get());
1613             }
1614         }
1615     }
1616     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1617     if (log)
1618         log->Printf ("SBValue(%p)::GetAddress () => (%s,%llu)", value_sp.get(), (addr.GetSection() ? addr.GetSection()->GetName().GetCString() : "NULL"), addr.GetOffset());
1619     return SBAddress(new Address(addr));
1620 }
1621 
1622 lldb::SBData
1623 SBValue::GetPointeeData (uint32_t item_idx,
1624                          uint32_t item_count)
1625 {
1626     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1627     lldb::SBData sb_data;
1628     lldb::ValueObjectSP value_sp(GetSP());
1629     if (value_sp)
1630     {
1631         ProcessSP process_sp(value_sp->GetProcessSP());
1632         Process::StopLocker stop_locker;
1633         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1634         {
1635             if (log)
1636                 log->Printf ("SBValue(%p)::GetPointeeData() => error: process is running", value_sp.get());
1637         }
1638         else
1639         {
1640             TargetSP target_sp (value_sp->GetTargetSP());
1641             if (target_sp)
1642             {
1643                 DataExtractorSP data_sp(new DataExtractor());
1644                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1645                 value_sp->GetPointeeData(*data_sp, item_idx, item_count);
1646                 if (data_sp->GetByteSize() > 0)
1647                     *sb_data = data_sp;
1648             }
1649         }
1650     }
1651     if (log)
1652         log->Printf ("SBValue(%p)::GetPointeeData (%d, %d) => SBData(%p)",
1653                      value_sp.get(),
1654                      item_idx,
1655                      item_count,
1656                      sb_data.get());
1657 
1658     return sb_data;
1659 }
1660 
1661 lldb::SBData
1662 SBValue::GetData ()
1663 {
1664     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1665     lldb::SBData sb_data;
1666     lldb::ValueObjectSP value_sp(GetSP());
1667     if (value_sp)
1668     {
1669         ProcessSP process_sp(value_sp->GetProcessSP());
1670         Process::StopLocker stop_locker;
1671         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1672         {
1673             if (log)
1674                 log->Printf ("SBValue(%p)::GetData() => error: process is running", value_sp.get());
1675         }
1676         else
1677         {
1678             TargetSP target_sp (value_sp->GetTargetSP());
1679             if (target_sp)
1680             {
1681                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
1682                 DataExtractorSP data_sp(new DataExtractor());
1683                 value_sp->GetData(*data_sp);
1684                 if (data_sp->GetByteSize() > 0)
1685                     *sb_data = data_sp;
1686             }
1687         }
1688     }
1689     if (log)
1690         log->Printf ("SBValue(%p)::GetData () => SBData(%p)",
1691                      value_sp.get(),
1692                      sb_data.get());
1693 
1694     return sb_data;
1695 }
1696 
1697 lldb::SBWatchpoint
1698 SBValue::Watch (bool resolve_location, bool read, bool write, SBError &error)
1699 {
1700     SBWatchpoint sb_watchpoint;
1701 
1702     // If the SBValue is not valid, there's no point in even trying to watch it.
1703     lldb::ValueObjectSP value_sp(GetSP());
1704     TargetSP target_sp (GetTarget().GetSP());
1705     if (value_sp && target_sp)
1706     {
1707         // Can't watch this if the process is running
1708         ProcessSP process_sp(value_sp->GetProcessSP());
1709         Process::StopLocker stop_locker;
1710         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1711         {
1712             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1713             if (log)
1714                 log->Printf ("SBValue(%p)::Watch() => error: process is running", value_sp.get());
1715             return sb_watchpoint;
1716         }
1717 
1718         // Read and Write cannot both be false.
1719         if (!read && !write)
1720             return sb_watchpoint;
1721 
1722         // If the value is not in scope, don't try and watch and invalid value
1723         if (!IsInScope())
1724             return sb_watchpoint;
1725 
1726         addr_t addr = GetLoadAddress();
1727         if (addr == LLDB_INVALID_ADDRESS)
1728             return sb_watchpoint;
1729         size_t byte_size = GetByteSize();
1730         if (byte_size == 0)
1731             return sb_watchpoint;
1732 
1733         uint32_t watch_type = 0;
1734         if (read)
1735             watch_type |= LLDB_WATCH_TYPE_READ;
1736         if (write)
1737             watch_type |= LLDB_WATCH_TYPE_WRITE;
1738 
1739         Error rc;
1740         WatchpointSP watchpoint_sp = target_sp->CreateWatchpoint(addr, byte_size, watch_type, rc);
1741         error.SetError(rc);
1742 
1743         if (watchpoint_sp)
1744         {
1745             sb_watchpoint.SetSP (watchpoint_sp);
1746             Declaration decl;
1747             if (value_sp->GetDeclaration (decl))
1748             {
1749                 if (decl.GetFile())
1750                 {
1751                     StreamString ss;
1752                     // True to show fullpath for declaration file.
1753                     decl.DumpStopContext(&ss, true);
1754                     watchpoint_sp->SetDeclInfo(ss.GetString());
1755                 }
1756             }
1757         }
1758     }
1759     return sb_watchpoint;
1760 }
1761 
1762 // FIXME: Remove this method impl (as well as the decl in .h) once it is no longer needed.
1763 // Backward compatibility fix in the interim.
1764 lldb::SBWatchpoint
1765 SBValue::Watch (bool resolve_location, bool read, bool write)
1766 {
1767     SBError error;
1768     return Watch(resolve_location, read, write, error);
1769 }
1770 
1771 lldb::SBWatchpoint
1772 SBValue::WatchPointee (bool resolve_location, bool read, bool write, SBError &error)
1773 {
1774     SBWatchpoint sb_watchpoint;
1775     if (IsInScope() && GetType().IsPointerType())
1776         sb_watchpoint = Dereference().Watch (resolve_location, read, write, error);
1777     return sb_watchpoint;
1778 }
1779 
1780