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