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