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