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/SBDeclaration.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/API/SBTypeFilter.h"
15 #include "lldb/API/SBTypeFormat.h"
16 #include "lldb/API/SBTypeSummary.h"
17 #include "lldb/API/SBTypeSynthetic.h"
18 
19 #include "lldb/Breakpoint/Watchpoint.h"
20 #include "lldb/Core/DataExtractor.h"
21 #include "lldb/Core/DataVisualization.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/Scalar.h"
25 #include "lldb/Core/Section.h"
26 #include "lldb/Core/Stream.h"
27 #include "lldb/Core/StreamFile.h"
28 #include "lldb/Core/Value.h"
29 #include "lldb/Core/ValueObject.h"
30 #include "lldb/Core/ValueObjectConstResult.h"
31 #include "lldb/Symbol/Block.h"
32 #include "lldb/Symbol/Declaration.h"
33 #include "lldb/Symbol/ObjectFile.h"
34 #include "lldb/Symbol/Type.h"
35 #include "lldb/Symbol/Variable.h"
36 #include "lldb/Symbol/VariableList.h"
37 #include "lldb/Target/ExecutionContext.h"
38 #include "lldb/Target/Process.h"
39 #include "lldb/Target/StackFrame.h"
40 #include "lldb/Target/Target.h"
41 #include "lldb/Target/Thread.h"
42 
43 #include "lldb/API/SBProcess.h"
44 #include "lldb/API/SBTarget.h"
45 #include "lldb/API/SBThread.h"
46 #include "lldb/API/SBFrame.h"
47 #include "lldb/API/SBDebugger.h"
48 
49 using namespace lldb;
50 using namespace lldb_private;
51 
52 SBValue::SBValue () :
53     m_opaque_sp ()
54 {
55 }
56 
57 SBValue::SBValue (const lldb::ValueObjectSP &value_sp)
58 {
59     SetSP(value_sp); // whenever setting the SP call SetSP() since it knows how to deal with synthetic values properly
60 }
61 
62 SBValue::SBValue(const SBValue &rhs)
63 {
64     SetSP(rhs.m_opaque_sp); // whenever setting the SP call SetSP() since it knows how to deal with synthetic values properly
65 }
66 
67 SBValue &
68 SBValue::operator = (const SBValue &rhs)
69 {
70     if (this != &rhs)
71     {
72         SetSP(rhs.m_opaque_sp); // whenever setting the SP call SetSP() since it knows how to deal with synthetic values properly
73     }
74     return *this;
75 }
76 
77 SBValue::~SBValue()
78 {
79 }
80 
81 bool
82 SBValue::IsValid ()
83 {
84     // If this function ever changes to anything that does more than just
85     // check if the opaque shared pointer is non NULL, then we need to update
86     // all "if (m_opaque_sp)" code in this file.
87     return m_opaque_sp.get() != NULL;
88 }
89 
90 void
91 SBValue::Clear()
92 {
93     m_opaque_sp.reset();
94 }
95 
96 SBError
97 SBValue::GetError()
98 {
99     SBError sb_error;
100 
101     lldb::ValueObjectSP value_sp(GetSP());
102     if (value_sp)
103         sb_error.SetError(value_sp->GetError());
104     else
105         sb_error.SetErrorString("error: invalid value");
106 
107     return sb_error;
108 }
109 
110 user_id_t
111 SBValue::GetID()
112 {
113     lldb::ValueObjectSP value_sp(GetSP());
114     if (value_sp)
115         return value_sp->GetID();
116     return LLDB_INVALID_UID;
117 }
118 
119 const char *
120 SBValue::GetName()
121 {
122 
123     const char *name = NULL;
124     lldb::ValueObjectSP value_sp(GetSP());
125     if (value_sp)
126         name = value_sp->GetName().GetCString();
127 
128     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
129     if (log)
130     {
131         if (name)
132             log->Printf ("SBValue(%p)::GetName () => \"%s\"", value_sp.get(), name);
133         else
134             log->Printf ("SBValue(%p)::GetName () => NULL", value_sp.get());
135     }
136 
137     return name;
138 }
139 
140 const char *
141 SBValue::GetTypeName ()
142 {
143     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
144     const char *name = NULL;
145     lldb::ValueObjectSP value_sp(GetSP());
146     if (value_sp)
147     {
148         // For a dynamic type we might have to run code to determine the type we are going to report,
149         // and we might not have updated the type before we get asked this.  So make sure to get the API lock.
150 
151         ProcessSP process_sp(value_sp->GetProcessSP());
152         Process::StopLocker stop_locker;
153         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
154         {
155             if (log)
156                 log->Printf ("SBValue(%p)::GetTypeName() => error: process is running", value_sp.get());
157         }
158         else
159         {
160             TargetSP target_sp(value_sp->GetTargetSP());
161             if (target_sp)
162             {
163                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
164                 name = value_sp->GetQualifiedTypeName().GetCString();
165             }
166         }
167     }
168 
169     if (log)
170     {
171         if (name)
172             log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"", value_sp.get(), name);
173         else
174             log->Printf ("SBValue(%p)::GetTypeName () => NULL", value_sp.get());
175     }
176 
177     return name;
178 }
179 
180 size_t
181 SBValue::GetByteSize ()
182 {
183     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
184     size_t result = 0;
185 
186     lldb::ValueObjectSP value_sp(GetSP());
187     if (value_sp)
188     {
189         // For a dynamic type we might have to run code to determine the type we are going to report,
190         // and we might not have updated the type before we get asked this.  So make sure to get the API lock.
191 
192         ProcessSP process_sp(value_sp->GetProcessSP());
193         Process::StopLocker stop_locker;
194         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
195         {
196             if (log)
197                 log->Printf ("SBValue(%p)::GetTypeName() => error: process is running", value_sp.get());
198         }
199         else
200         {
201             TargetSP target_sp(value_sp->GetTargetSP());
202             if (target_sp)
203             {
204                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
205                 result = value_sp->GetByteSize();
206             }
207         }
208     }
209 
210     if (log)
211         log->Printf ("SBValue(%p)::GetByteSize () => %llu", value_sp.get(), (uint64_t)result);
212 
213     return result;
214 }
215 
216 bool
217 SBValue::IsInScope ()
218 {
219     bool result = false;
220 
221     lldb::ValueObjectSP value_sp(GetSP());
222     if (value_sp)
223     {
224         TargetSP target_sp(value_sp->GetTargetSP());
225         if (target_sp)
226         {
227             Mutex::Locker api_locker (target_sp->GetAPIMutex());
228             result = value_sp->IsInScope ();
229         }
230     }
231 
232     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
233     if (log)
234         log->Printf ("SBValue(%p)::IsInScope () => %i", value_sp.get(), result);
235 
236     return result;
237 }
238 
239 const char *
240 SBValue::GetValue ()
241 {
242     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
243 
244     const char *cstr = NULL;
245     lldb::ValueObjectSP value_sp(GetSP());
246     if (value_sp)
247     {
248         ProcessSP process_sp(value_sp->GetProcessSP());
249         Process::StopLocker stop_locker;
250         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
251         {
252             if (log)
253                 log->Printf ("SBValue(%p)::GetValue() => error: process is running", value_sp.get());
254         }
255         else
256         {
257             TargetSP target_sp(value_sp->GetTargetSP());
258             if (target_sp)
259             {
260                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
261                 cstr = value_sp->GetValueAsCString ();
262             }
263         }
264     }
265     if (log)
266     {
267         if (cstr)
268             log->Printf ("SBValue(%p)::GetValue() => \"%s\"", value_sp.get(), cstr);
269         else
270             log->Printf ("SBValue(%p)::GetValue() => NULL", value_sp.get());
271     }
272 
273     return cstr;
274 }
275 
276 ValueType
277 SBValue::GetValueType ()
278 {
279     ValueType result = eValueTypeInvalid;
280     lldb::ValueObjectSP value_sp(GetSP());
281     if (value_sp)
282         result = value_sp->GetValueType();
283     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
284     if (log)
285     {
286         switch (result)
287         {
288         case eValueTypeInvalid:         log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid", value_sp.get()); break;
289         case eValueTypeVariableGlobal:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal", value_sp.get()); break;
290         case eValueTypeVariableStatic:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic", value_sp.get()); break;
291         case eValueTypeVariableArgument:log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument", value_sp.get()); break;
292         case eValueTypeVariableLocal:   log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal", value_sp.get()); break;
293         case eValueTypeRegister:        log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister", value_sp.get()); break;
294         case eValueTypeRegisterSet:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet", value_sp.get()); break;
295         case eValueTypeConstResult:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult", value_sp.get()); break;
296         default:     log->Printf ("SBValue(%p)::GetValueType () => %i ???", value_sp.get(), result); break;
297         }
298     }
299     return result;
300 }
301 
302 const char *
303 SBValue::GetObjectDescription ()
304 {
305     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
306     const char *cstr = NULL;
307     lldb::ValueObjectSP value_sp(GetSP());
308     if (value_sp)
309     {
310         ProcessSP process_sp(value_sp->GetProcessSP());
311         Process::StopLocker stop_locker;
312         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
313         {
314             if (log)
315                 log->Printf ("SBValue(%p)::GetObjectDescription() => error: process is running", value_sp.get());
316         }
317         else
318         {
319             TargetSP target_sp(value_sp->GetTargetSP());
320             if (target_sp)
321             {
322                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
323                 cstr = value_sp->GetObjectDescription ();
324             }
325         }
326     }
327     if (log)
328     {
329         if (cstr)
330             log->Printf ("SBValue(%p)::GetObjectDescription() => \"%s\"", value_sp.get(), cstr);
331         else
332             log->Printf ("SBValue(%p)::GetObjectDescription() => NULL", value_sp.get());
333     }
334     return cstr;
335 }
336 
337 SBType
338 SBValue::GetType()
339 {
340     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
341     SBType sb_type;
342     lldb::ValueObjectSP value_sp(GetSP());
343     TypeImplSP type_sp;
344     if (value_sp)
345     {
346         ProcessSP process_sp(value_sp->GetProcessSP());
347         Process::StopLocker stop_locker;
348         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
349         {
350             if (log)
351                 log->Printf ("SBValue(%p)::GetType() => error: process is running", value_sp.get());
352         }
353         else
354         {
355             TargetSP target_sp(value_sp->GetTargetSP());
356             if (target_sp)
357             {
358                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
359                 type_sp.reset (new TypeImpl(ClangASTType (value_sp->GetClangAST(), value_sp->GetClangType())));
360                 sb_type.SetSP(type_sp);
361             }
362         }
363     }
364     if (log)
365     {
366         if (type_sp)
367             log->Printf ("SBValue(%p)::GetType => SBType(%p)", value_sp.get(), type_sp.get());
368         else
369             log->Printf ("SBValue(%p)::GetType => NULL", value_sp.get());
370     }
371     return sb_type;
372 }
373 
374 bool
375 SBValue::GetValueDidChange ()
376 {
377     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
378     bool result = false;
379     lldb::ValueObjectSP value_sp(GetSP());
380     if (value_sp)
381     {
382         ProcessSP process_sp(value_sp->GetProcessSP());
383         Process::StopLocker stop_locker;
384         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
385         {
386             if (log)
387                 log->Printf ("SBValue(%p)::GetValueDidChange() => error: process is running", value_sp.get());
388         }
389         else
390         {
391             TargetSP target_sp(value_sp->GetTargetSP());
392             if (target_sp)
393             {
394                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
395                 result = value_sp->GetValueDidChange ();
396             }
397         }
398     }
399     if (log)
400         log->Printf ("SBValue(%p)::GetValueDidChange() => %i", value_sp.get(), result);
401 
402     return result;
403 }
404 
405 #ifndef LLDB_DISABLE_PYTHON
406 const char *
407 SBValue::GetSummary ()
408 {
409     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
410     const char *cstr = NULL;
411     lldb::ValueObjectSP value_sp(GetSP());
412     if (value_sp)
413     {
414         ProcessSP process_sp(value_sp->GetProcessSP());
415         Process::StopLocker stop_locker;
416         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
417         {
418             if (log)
419                 log->Printf ("SBValue(%p)::GetSummary() => error: process is running", value_sp.get());
420         }
421         else
422         {
423             TargetSP target_sp(value_sp->GetTargetSP());
424             if (target_sp)
425             {
426                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
427                 cstr = value_sp->GetSummaryAsCString();
428             }
429         }
430     }
431     if (log)
432     {
433         if (cstr)
434             log->Printf ("SBValue(%p)::GetSummary() => \"%s\"", value_sp.get(), cstr);
435         else
436             log->Printf ("SBValue(%p)::GetSummary() => NULL", value_sp.get());
437     }
438     return cstr;
439 }
440 #endif // LLDB_DISABLE_PYTHON
441 
442 const char *
443 SBValue::GetLocation ()
444 {
445     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
446     const char *cstr = NULL;
447     lldb::ValueObjectSP value_sp(GetSP());
448     if (value_sp)
449     {
450         ProcessSP process_sp(value_sp->GetProcessSP());
451         Process::StopLocker stop_locker;
452         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
453         {
454             if (log)
455                 log->Printf ("SBValue(%p)::GetLocation() => error: process is running", value_sp.get());
456         }
457         else
458         {
459             TargetSP target_sp(value_sp->GetTargetSP());
460             if (target_sp)
461             {
462                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
463                 cstr = value_sp->GetLocationAsCString();
464             }
465         }
466     }
467     if (log)
468     {
469         if (cstr)
470             log->Printf ("SBValue(%p)::GetLocation() => \"%s\"", value_sp.get(), cstr);
471         else
472             log->Printf ("SBValue(%p)::GetLocation() => NULL", value_sp.get());
473     }
474     return cstr;
475 }
476 
477 // Deprecated - use the one that takes an lldb::SBError
478 bool
479 SBValue::SetValueFromCString (const char *value_str)
480 {
481     lldb::SBError dummy;
482     return SetValueFromCString(value_str,dummy);
483 }
484 
485 bool
486 SBValue::SetValueFromCString (const char *value_str, lldb::SBError& error)
487 {
488     bool success = false;
489     lldb::ValueObjectSP value_sp(GetSP());
490     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
491     if (value_sp)
492     {
493         ProcessSP process_sp(value_sp->GetProcessSP());
494         Process::StopLocker stop_locker;
495         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
496         {
497             if (log)
498                 log->Printf ("SBValue(%p)::SetValueFromCString() => error: process is running", value_sp.get());
499         }
500         else
501         {
502             TargetSP target_sp(value_sp->GetTargetSP());
503             if (target_sp)
504             {
505                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
506                 success = value_sp->SetValueFromCString (value_str,error.ref());
507             }
508         }
509     }
510     if (log)
511         log->Printf ("SBValue(%p)::SetValueFromCString(\"%s\") => %i", value_sp.get(), value_str, success);
512 
513     return success;
514 }
515 
516 lldb::SBTypeFormat
517 SBValue::GetTypeFormat ()
518 {
519     lldb::SBTypeFormat format;
520     lldb::ValueObjectSP value_sp(GetSP());
521     if (value_sp)
522     {
523         ProcessSP process_sp(value_sp->GetProcessSP());
524         Process::StopLocker stop_locker;
525         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
526         {
527             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
528             if (log)
529                 log->Printf ("SBValue(%p)::GetTypeFormat() => error: process is running", value_sp.get());
530         }
531         else
532         {
533             TargetSP target_sp(value_sp->GetTargetSP());
534             if (target_sp)
535             {
536                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
537                 if (value_sp->UpdateValueIfNeeded(true))
538                 {
539                     lldb::TypeFormatImplSP format_sp = value_sp->GetValueFormat();
540                     if (format_sp)
541                         format.SetSP(format_sp);
542                 }
543             }
544         }
545     }
546     return format;
547 }
548 
549 #ifndef LLDB_DISABLE_PYTHON
550 lldb::SBTypeSummary
551 SBValue::GetTypeSummary ()
552 {
553     lldb::SBTypeSummary summary;
554     lldb::ValueObjectSP value_sp(GetSP());
555     if (value_sp)
556     {
557         ProcessSP process_sp(value_sp->GetProcessSP());
558         Process::StopLocker stop_locker;
559         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
560         {
561             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
562             if (log)
563                 log->Printf ("SBValue(%p)::GetTypeSummary() => error: process is running", value_sp.get());
564         }
565         else
566         {
567             TargetSP target_sp(value_sp->GetTargetSP());
568             if (target_sp)
569             {
570                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
571                 if (value_sp->UpdateValueIfNeeded(true))
572                 {
573                     lldb::TypeSummaryImplSP summary_sp = value_sp->GetSummaryFormat();
574                     if (summary_sp)
575                         summary.SetSP(summary_sp);
576                 }
577             }
578         }
579     }
580     return summary;
581 }
582 #endif // LLDB_DISABLE_PYTHON
583 
584 lldb::SBTypeFilter
585 SBValue::GetTypeFilter ()
586 {
587     lldb::SBTypeFilter filter;
588     lldb::ValueObjectSP value_sp(GetSP());
589     if (value_sp)
590     {
591         ProcessSP process_sp(value_sp->GetProcessSP());
592         Process::StopLocker stop_locker;
593         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
594         {
595             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
596             if (log)
597                 log->Printf ("SBValue(%p)::GetTypeFilter() => error: process is running", value_sp.get());
598         }
599         else
600         {
601             TargetSP target_sp(value_sp->GetTargetSP());
602             if (target_sp)
603             {
604                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
605                 if (value_sp->UpdateValueIfNeeded(true))
606                 {
607                     lldb::SyntheticChildrenSP synthetic_sp = value_sp->GetSyntheticChildren();
608 
609                     if (synthetic_sp && !synthetic_sp->IsScripted())
610                     {
611                         TypeFilterImplSP filter_sp = STD_STATIC_POINTER_CAST(TypeFilterImpl,synthetic_sp);
612                         filter.SetSP(filter_sp);
613                     }
614                 }
615             }
616         }
617     }
618     return filter;
619 }
620 
621 #ifndef LLDB_DISABLE_PYTHON
622 lldb::SBTypeSynthetic
623 SBValue::GetTypeSynthetic ()
624 {
625     lldb::SBTypeSynthetic synthetic;
626     lldb::ValueObjectSP value_sp(GetSP());
627     if (value_sp)
628     {
629         ProcessSP process_sp(value_sp->GetProcessSP());
630         Process::StopLocker stop_locker;
631         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
632         {
633             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
634             if (log)
635                 log->Printf ("SBValue(%p)::GetTypeSynthetic() => error: process is running", value_sp.get());
636         }
637         else
638         {
639             TargetSP target_sp(value_sp->GetTargetSP());
640             if (target_sp)
641             {
642                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
643                 if (value_sp->UpdateValueIfNeeded(true))
644                 {
645                     lldb::SyntheticChildrenSP children_sp = value_sp->GetSyntheticChildren();
646 
647                     if (children_sp && children_sp->IsScripted())
648                     {
649                         TypeSyntheticImplSP synth_sp = STD_STATIC_POINTER_CAST(TypeSyntheticImpl,children_sp);
650                         synthetic.SetSP(synth_sp);
651                     }
652                 }
653             }
654         }
655     }
656     return synthetic;
657 }
658 #endif
659 
660 lldb::SBValue
661 SBValue::CreateChildAtOffset (const char *name, uint32_t offset, SBType type)
662 {
663     lldb::SBValue sb_value;
664     lldb::ValueObjectSP value_sp(GetSP());
665     lldb::ValueObjectSP new_value_sp;
666     if (value_sp)
667     {
668         ProcessSP process_sp(value_sp->GetProcessSP());
669         Process::StopLocker stop_locker;
670         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
671         {
672             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
673             if (log)
674                 log->Printf ("SBValue(%p)::CreateChildAtOffset() => error: process is running", value_sp.get());
675         }
676         else
677         {
678             TargetSP target_sp(value_sp->GetTargetSP());
679             if (target_sp)
680             {
681                 Mutex::Locker api_locker (target_sp->GetAPIMutex());
682                 TypeImplSP type_sp (type.GetSP());
683                 if (type.IsValid())
684                 {
685                     sb_value = SBValue(value_sp->GetSyntheticChildAtOffset(offset, type_sp->GetClangASTType(), true));
686                     new_value_sp = sb_value.GetSP();
687                     if (new_value_sp)
688                         new_value_sp->SetName(ConstString(name));
689                 }
690             }
691         }
692     }
693     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
694     if (log)
695     {
696         if (new_value_sp)
697             log->Printf ("SBValue(%p)::CreateChildAtOffset => \"%s\"", value_sp.get(), new_value_sp->GetName().AsCString());
698         else
699             log->Printf ("SBValue(%p)::CreateChildAtOffset => NULL", value_sp.get());
700     }
701     return sb_value;
702 }
703 
704 lldb::SBValue
705 SBValue::Cast (SBType type)
706 {
707     lldb::SBValue sb_value;
708     lldb::ValueObjectSP value_sp(GetSP());
709     TypeImplSP type_sp (type.GetSP());
710     if (value_sp && type_sp)
711         sb_value.SetSP(value_sp->Cast(type_sp->GetClangASTType()));
712     return sb_value;
713 }
714 
715 lldb::SBValue
716 SBValue::CreateValueFromExpression (const char *name, const char* expression)
717 {
718     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
719     lldb::SBValue sb_value;
720     lldb::ValueObjectSP value_sp(GetSP());
721     lldb::ValueObjectSP new_value_sp;
722     if (value_sp)
723     {
724         ExecutionContext exe_ctx (value_sp->GetExecutionContextRef());
725         ProcessSP process_sp(exe_ctx.GetProcessSP());
726         Process::StopLocker stop_locker;
727         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
728         {
729             if (log)
730                 log->Printf ("SBValue(%p)::CreateValueFromExpression() => error: process is running", value_sp.get());
731         }
732         else
733         {
734             Target* target = exe_ctx.GetTargetPtr();
735             if (target)
736             {
737                 Target::EvaluateExpressionOptions options;
738                 options.SetKeepInMemory(true);
739                 target->EvaluateExpression (expression,
740                                             exe_ctx.GetFramePtr(),
741                                             new_value_sp,
742                                             options);
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::SBDeclaration
1700 SBValue::GetDeclaration ()
1701 {
1702     lldb::ValueObjectSP value_sp(GetSP());
1703     SBDeclaration decl_sb;
1704     if (value_sp)
1705     {
1706         Declaration decl;
1707         if (value_sp->GetDeclaration(decl))
1708             decl_sb.SetDeclaration(decl);
1709     }
1710     return decl_sb;
1711 }
1712 
1713 lldb::SBWatchpoint
1714 SBValue::Watch (bool resolve_location, bool read, bool write, SBError &error)
1715 {
1716     SBWatchpoint sb_watchpoint;
1717 
1718     // If the SBValue is not valid, there's no point in even trying to watch it.
1719     lldb::ValueObjectSP value_sp(GetSP());
1720     TargetSP target_sp (GetTarget().GetSP());
1721     if (value_sp && target_sp)
1722     {
1723         // Can't watch this if the process is running
1724         ProcessSP process_sp(value_sp->GetProcessSP());
1725         Process::StopLocker stop_locker;
1726         if (process_sp && !stop_locker.TryLock(&process_sp->GetRunLock()))
1727         {
1728             LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1729             if (log)
1730                 log->Printf ("SBValue(%p)::Watch() => error: process is running", value_sp.get());
1731             return sb_watchpoint;
1732         }
1733 
1734         // Read and Write cannot both be false.
1735         if (!read && !write)
1736             return sb_watchpoint;
1737 
1738         // If the value is not in scope, don't try and watch and invalid value
1739         if (!IsInScope())
1740             return sb_watchpoint;
1741 
1742         addr_t addr = GetLoadAddress();
1743         if (addr == LLDB_INVALID_ADDRESS)
1744             return sb_watchpoint;
1745         size_t byte_size = GetByteSize();
1746         if (byte_size == 0)
1747             return sb_watchpoint;
1748 
1749         uint32_t watch_type = 0;
1750         if (read)
1751             watch_type |= LLDB_WATCH_TYPE_READ;
1752         if (write)
1753             watch_type |= LLDB_WATCH_TYPE_WRITE;
1754 
1755         Error rc;
1756         WatchpointSP watchpoint_sp = target_sp->CreateWatchpoint(addr, byte_size, watch_type, rc);
1757         error.SetError(rc);
1758 
1759         if (watchpoint_sp)
1760         {
1761             sb_watchpoint.SetSP (watchpoint_sp);
1762             Declaration decl;
1763             if (value_sp->GetDeclaration (decl))
1764             {
1765                 if (decl.GetFile())
1766                 {
1767                     StreamString ss;
1768                     // True to show fullpath for declaration file.
1769                     decl.DumpStopContext(&ss, true);
1770                     watchpoint_sp->SetDeclInfo(ss.GetString());
1771                 }
1772             }
1773         }
1774     }
1775     return sb_watchpoint;
1776 }
1777 
1778 // FIXME: Remove this method impl (as well as the decl in .h) once it is no longer needed.
1779 // Backward compatibility fix in the interim.
1780 lldb::SBWatchpoint
1781 SBValue::Watch (bool resolve_location, bool read, bool write)
1782 {
1783     SBError error;
1784     return Watch(resolve_location, read, write, error);
1785 }
1786 
1787 lldb::SBWatchpoint
1788 SBValue::WatchPointee (bool resolve_location, bool read, bool write, SBError &error)
1789 {
1790     SBWatchpoint sb_watchpoint;
1791     if (IsInScope() && GetType().IsPointerType())
1792         sb_watchpoint = Dereference().Watch (resolve_location, read, write, error);
1793     return sb_watchpoint;
1794 }
1795 
1796