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