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