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 #include "lldb/API/SBStream.h"
12 
13 #include "lldb/Breakpoint/Watchpoint.h"
14 #include "lldb/Core/DataExtractor.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Core/Scalar.h"
18 #include "lldb/Core/Stream.h"
19 #include "lldb/Core/StreamFile.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Core/ValueObject.h"
22 #include "lldb/Core/ValueObjectConstResult.h"
23 #include "lldb/Symbol/Block.h"
24 #include "lldb/Symbol/ObjectFile.h"
25 #include "lldb/Symbol/Variable.h"
26 #include "lldb/Symbol/VariableList.h"
27 #include "lldb/Target/ExecutionContext.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 
33 #include "lldb/API/SBProcess.h"
34 #include "lldb/API/SBTarget.h"
35 #include "lldb/API/SBThread.h"
36 #include "lldb/API/SBFrame.h"
37 #include "lldb/API/SBDebugger.h"
38 
39 using namespace lldb;
40 using namespace lldb_private;
41 
42 SBValue::SBValue () :
43     m_opaque_sp ()
44 {
45 }
46 
47 SBValue::SBValue (const lldb::ValueObjectSP &value_sp) :
48     m_opaque_sp (value_sp)
49 {
50 }
51 
52 SBValue::SBValue(const SBValue &rhs) :
53     m_opaque_sp (rhs.m_opaque_sp)
54 {
55 }
56 
57 SBValue &
58 SBValue::operator = (const SBValue &rhs)
59 {
60     if (this != &rhs)
61         m_opaque_sp = rhs.m_opaque_sp;
62     return *this;
63 }
64 
65 SBValue::~SBValue()
66 {
67 }
68 
69 bool
70 SBValue::IsValid ()
71 {
72     // If this function ever changes to anything that does more than just
73     // check if the opaque shared pointer is non NULL, then we need to update
74     // all "if (m_opaque_sp)" code in this file.
75     return m_opaque_sp.get() != NULL;
76 }
77 
78 SBError
79 SBValue::GetError()
80 {
81     SBError sb_error;
82 
83     if (m_opaque_sp.get())
84         sb_error.SetError(m_opaque_sp->GetError());
85 
86     return sb_error;
87 }
88 
89 user_id_t
90 SBValue::GetID()
91 {
92     if (m_opaque_sp)
93         return m_opaque_sp->GetID();
94     return LLDB_INVALID_UID;
95 }
96 
97 const char *
98 SBValue::GetName()
99 {
100 
101     const char *name = NULL;
102     if (m_opaque_sp)
103         name = m_opaque_sp->GetName().GetCString();
104 
105     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
106     if (log)
107     {
108         if (name)
109             log->Printf ("SBValue(%p)::GetName () => \"%s\"", m_opaque_sp.get(), name);
110         else
111             log->Printf ("SBValue(%p)::GetName () => NULL", m_opaque_sp.get());
112     }
113 
114     return name;
115 }
116 
117 const char *
118 SBValue::GetTypeName ()
119 {
120     const char *name = NULL;
121     if (m_opaque_sp)
122         name = m_opaque_sp->GetTypeName().GetCString();
123     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
124     if (log)
125     {
126         if (name)
127             log->Printf ("SBValue(%p)::GetTypeName () => \"%s\"", m_opaque_sp.get(), name);
128         else
129             log->Printf ("SBValue(%p)::GetTypeName () => NULL", m_opaque_sp.get());
130     }
131 
132     return name;
133 }
134 
135 size_t
136 SBValue::GetByteSize ()
137 {
138     size_t result = 0;
139 
140     if (m_opaque_sp)
141         result = m_opaque_sp->GetByteSize();
142 
143     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
144     if (log)
145         log->Printf ("SBValue(%p)::GetByteSize () => %zu", m_opaque_sp.get(), result);
146 
147     return result;
148 }
149 
150 bool
151 SBValue::IsInScope ()
152 {
153     bool result = false;
154 
155     if (m_opaque_sp)
156     {
157         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
158         {
159             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
160             result = m_opaque_sp->IsInScope ();
161         }
162     }
163 
164     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
165     if (log)
166         log->Printf ("SBValue(%p)::IsInScope () => %i", m_opaque_sp.get(), result);
167 
168     return result;
169 }
170 
171 const char *
172 SBValue::GetValue ()
173 {
174     const char *cstr = NULL;
175     if (m_opaque_sp)
176     {
177         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
178         {
179             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
180             cstr = m_opaque_sp->GetValueAsCString ();
181         }
182     }
183     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
184     if (log)
185     {
186         if (cstr)
187             log->Printf ("SBValue(%p)::GetValue => \"%s\"", m_opaque_sp.get(), cstr);
188         else
189             log->Printf ("SBValue(%p)::GetValue => NULL", m_opaque_sp.get());
190     }
191 
192     return cstr;
193 }
194 
195 ValueType
196 SBValue::GetValueType ()
197 {
198     ValueType result = eValueTypeInvalid;
199     if (m_opaque_sp)
200         result = m_opaque_sp->GetValueType();
201     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
202     if (log)
203     {
204         switch (result)
205         {
206         case eValueTypeInvalid:         log->Printf ("SBValue(%p)::GetValueType () => eValueTypeInvalid", m_opaque_sp.get()); break;
207         case eValueTypeVariableGlobal:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableGlobal", m_opaque_sp.get()); break;
208         case eValueTypeVariableStatic:  log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableStatic", m_opaque_sp.get()); break;
209         case eValueTypeVariableArgument:log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableArgument", m_opaque_sp.get()); break;
210         case eValueTypeVariableLocal:   log->Printf ("SBValue(%p)::GetValueType () => eValueTypeVariableLocal", m_opaque_sp.get()); break;
211         case eValueTypeRegister:        log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegister", m_opaque_sp.get()); break;
212         case eValueTypeRegisterSet:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeRegisterSet", m_opaque_sp.get()); break;
213         case eValueTypeConstResult:     log->Printf ("SBValue(%p)::GetValueType () => eValueTypeConstResult", m_opaque_sp.get()); break;
214         default:     log->Printf ("SBValue(%p)::GetValueType () => %i ???", m_opaque_sp.get(), result); break;
215         }
216     }
217     return result;
218 }
219 
220 const char *
221 SBValue::GetObjectDescription ()
222 {
223     const char *cstr = NULL;
224     if (m_opaque_sp)
225     {
226         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
227         {
228             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
229             cstr = m_opaque_sp->GetObjectDescription ();
230         }
231     }
232     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
233     if (log)
234     {
235         if (cstr)
236             log->Printf ("SBValue(%p)::GetObjectDescription => \"%s\"", m_opaque_sp.get(), cstr);
237         else
238             log->Printf ("SBValue(%p)::GetObjectDescription => NULL", m_opaque_sp.get());
239     }
240     return cstr;
241 }
242 
243 SBType
244 SBValue::GetType()
245 {
246     SBType result;
247     if (m_opaque_sp)
248     {
249         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
250         {
251             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
252             result = SBType(ClangASTType (m_opaque_sp->GetClangAST(), m_opaque_sp->GetClangType()));
253         }
254     }
255     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
256     if (log)
257     {
258         if (result.IsValid())
259             log->Printf ("SBValue(%p)::GetType => %p", m_opaque_sp.get(), &result);
260         else
261             log->Printf ("SBValue(%p)::GetType => NULL", m_opaque_sp.get());
262     }
263     return result;
264 }
265 
266 bool
267 SBValue::GetValueDidChange ()
268 {
269     bool result = false;
270     if (m_opaque_sp)
271     {
272         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
273         {
274             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
275             result = m_opaque_sp->GetValueDidChange ();
276         }
277     }
278     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
279     if (log)
280         log->Printf ("SBValue(%p)::GetValueDidChange => %i", m_opaque_sp.get(), result);
281 
282     return result;
283 }
284 
285 const char *
286 SBValue::GetSummary ()
287 {
288     const char *cstr = NULL;
289     if (m_opaque_sp)
290     {
291         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
292         {
293             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
294             cstr = m_opaque_sp->GetSummaryAsCString();
295         }
296     }
297     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
298     if (log)
299     {
300         if (cstr)
301             log->Printf ("SBValue(%p)::GetSummary => \"%s\"", m_opaque_sp.get(), cstr);
302         else
303             log->Printf ("SBValue(%p)::GetSummary => NULL", m_opaque_sp.get());
304     }
305     return cstr;
306 }
307 
308 const char *
309 SBValue::GetLocation ()
310 {
311     const char *cstr = NULL;
312     if (m_opaque_sp)
313     {
314         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
315         {
316             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
317             cstr = m_opaque_sp->GetLocationAsCString();
318         }
319     }
320     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
321     if (log)
322     {
323         if (cstr)
324             log->Printf ("SBValue(%p)::GetSummary => \"%s\"", m_opaque_sp.get(), cstr);
325         else
326             log->Printf ("SBValue(%p)::GetSummary => NULL", m_opaque_sp.get());
327     }
328     return cstr;
329 }
330 
331 bool
332 SBValue::SetValueFromCString (const char *value_str)
333 {
334     bool success = false;
335     if (m_opaque_sp)
336     {
337         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
338         {
339             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
340             success = m_opaque_sp->SetValueFromCString (value_str);
341         }
342     }
343     return success;
344 }
345 
346 lldb::SBValue
347 SBValue::CreateChildAtOffset (const char *name, uint32_t offset, SBType type)
348 {
349     lldb::SBValue result;
350     if (m_opaque_sp)
351     {
352         if (type.IsValid())
353         {
354             result = SBValue(m_opaque_sp->GetSyntheticChildAtOffset(offset, type.m_opaque_sp->GetClangASTType(), true));
355             result.m_opaque_sp->SetName(ConstString(name));
356         }
357     }
358     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
359     if (log)
360     {
361         if (result.IsValid())
362             log->Printf ("SBValue(%p)::GetChildAtOffset => \"%s\"", m_opaque_sp.get(), result.m_opaque_sp->GetName().AsCString());
363         else
364             log->Printf ("SBValue(%p)::GetChildAtOffset => NULL", m_opaque_sp.get());
365     }
366     return result;
367 }
368 
369 lldb::SBValue
370 SBValue::Cast (SBType type)
371 {
372     return CreateChildAtOffset(m_opaque_sp->GetName().GetCString(), 0, type);
373 }
374 
375 lldb::SBValue
376 SBValue::CreateValueFromExpression (const char *name, const char* expression)
377 {
378     lldb::SBValue result;
379     if (m_opaque_sp)
380     {
381         ValueObjectSP result_valobj_sp;
382         m_opaque_sp->GetUpdatePoint().GetTargetSP()->EvaluateExpression (expression,
383                                                                          m_opaque_sp->GetUpdatePoint().GetExecutionContextScope()->CalculateStackFrame(),
384                                                                          eExecutionPolicyOnlyWhenNeeded,
385                                                                          true, // unwind on error
386                                                                          true, // keep in memory
387                                                                          eNoDynamicValues,
388                                                                          result_valobj_sp);
389         result_valobj_sp->SetName(ConstString(name));
390         result = SBValue(result_valobj_sp);
391     }
392     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
393     if (log)
394     {
395         if (result.IsValid())
396             log->Printf ("SBValue(%p)::GetChildFromExpression => \"%s\"", m_opaque_sp.get(), result.m_opaque_sp->GetName().AsCString());
397         else
398             log->Printf ("SBValue(%p)::GetChildFromExpression => NULL", m_opaque_sp.get());
399     }
400     return result;
401 }
402 
403 lldb::SBValue
404 SBValue::CreateValueFromAddress(const char* name, lldb::addr_t address, SBType type)
405 {
406     lldb::SBValue result;
407     if (m_opaque_sp)
408     {
409 
410         SBType real_type(type.GetPointerType());
411 
412         lldb::DataBufferSP buffer(new lldb_private::DataBufferHeap(&address,sizeof(lldb::addr_t)));
413 
414         ValueObjectSP ptr_result_valobj_sp(ValueObjectConstResult::Create (m_opaque_sp->GetUpdatePoint().GetExecutionContextScope(),
415                                                                            real_type.m_opaque_sp->GetASTContext(),
416                                                                            real_type.m_opaque_sp->GetOpaqueQualType(),
417                                                                            ConstString(name),
418                                                                            buffer,
419                                                                            lldb::endian::InlHostByteOrder(),
420                                                                            GetTarget().GetProcess().GetAddressByteSize()));
421 
422         ValueObjectSP result_valobj_sp;
423 
424         ptr_result_valobj_sp->GetValue().SetValueType(Value::eValueTypeLoadAddress);
425         if (ptr_result_valobj_sp)
426         {
427             Error err;
428             result_valobj_sp = ptr_result_valobj_sp->Dereference(err);
429             if (result_valobj_sp)
430                 result_valobj_sp->SetName(ConstString(name));
431         }
432         result = SBValue(result_valobj_sp);
433     }
434     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
435     if (log)
436     {
437         if (result.IsValid())
438             log->Printf ("SBValue(%p)::GetChildFromAddress => \"%s\"", m_opaque_sp.get(), result.m_opaque_sp->GetName().AsCString());
439         else
440             log->Printf ("SBValue(%p)::GetChildFromAddress => NULL", m_opaque_sp.get());
441     }
442     return result;
443 }
444 
445 lldb::SBValue
446 SBValue::CreateValueFromData (const char* name, SBData data, SBType type)
447 {
448     SBValue result;
449 
450     AddressType addr_of_children_priv = eAddressTypeLoad;
451 
452     if (m_opaque_sp)
453     {
454         ValueObjectSP valobj_sp;
455         valobj_sp = ValueObjectConstResult::Create (m_opaque_sp->GetExecutionContextScope(),
456                                                     type.m_opaque_sp->GetASTContext() ,
457                                                     type.m_opaque_sp->GetOpaqueQualType(),
458                                                     ConstString(name),
459                                                     *data.m_opaque_sp,
460                                                     LLDB_INVALID_ADDRESS);
461         valobj_sp->SetAddressTypeOfChildren(addr_of_children_priv);
462         result = SBValue(valobj_sp);
463     }
464     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
465     if (log)
466     {
467         if (result.IsValid())
468             log->Printf ("SBValue(%p)::GetChildFromExpression => \"%s\"", m_opaque_sp.get(), result.m_opaque_sp->GetName().AsCString());
469         else
470             log->Printf ("SBValue(%p)::GetChildFromExpression => NULL", m_opaque_sp.get());
471     }
472     return result;
473 }
474 
475 SBValue
476 SBValue::GetChildAtIndex (uint32_t idx)
477 {
478     const bool can_create_synthetic = false;
479     lldb::DynamicValueType use_dynamic = eNoDynamicValues;
480     if (m_opaque_sp)
481         use_dynamic = m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetPreferDynamicValue();
482     return GetChildAtIndex (idx, use_dynamic, can_create_synthetic);
483 }
484 
485 SBValue
486 SBValue::GetChildAtIndex (uint32_t idx, lldb::DynamicValueType use_dynamic, bool can_create_synthetic)
487 {
488     lldb::ValueObjectSP child_sp;
489 
490     if (m_opaque_sp)
491     {
492         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
493         {
494             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
495             const bool can_create = true;
496             child_sp = m_opaque_sp->GetChildAtIndex (idx, can_create);
497             if (can_create_synthetic && !child_sp)
498             {
499                 if (m_opaque_sp->IsPointerType())
500                 {
501                     child_sp = m_opaque_sp->GetSyntheticArrayMemberFromPointer(idx, can_create);
502                 }
503                 else if (m_opaque_sp->IsArrayType())
504                 {
505                     child_sp = m_opaque_sp->GetSyntheticArrayMemberFromArray(idx, can_create);
506                 }
507             }
508 
509             if (child_sp)
510             {
511                 if (use_dynamic != lldb::eNoDynamicValues)
512                 {
513                     lldb::ValueObjectSP dynamic_sp(child_sp->GetDynamicValue (use_dynamic));
514                     if (dynamic_sp)
515                         child_sp = dynamic_sp;
516                 }
517             }
518         }
519     }
520 
521     SBValue sb_value (child_sp);
522     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
523     if (log)
524         log->Printf ("SBValue(%p)::GetChildAtIndex (%u) => SBValue(%p)", m_opaque_sp.get(), idx, sb_value.get());
525 
526     return sb_value;
527 }
528 
529 uint32_t
530 SBValue::GetIndexOfChildWithName (const char *name)
531 {
532     uint32_t idx = UINT32_MAX;
533     if (m_opaque_sp)
534     {
535         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
536         {
537             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
538 
539             idx = m_opaque_sp->GetIndexOfChildWithName (ConstString(name));
540         }
541     }
542     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
543     if (log)
544     {
545         if (idx == UINT32_MAX)
546             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => NOT FOUND", m_opaque_sp.get(), name);
547         else
548             log->Printf ("SBValue(%p)::GetIndexOfChildWithName (name=\"%s\") => %u", m_opaque_sp.get(), name, idx);
549     }
550     return idx;
551 }
552 
553 SBValue
554 SBValue::GetChildMemberWithName (const char *name)
555 {
556     if (m_opaque_sp)
557     {
558         lldb::DynamicValueType use_dynamic_value = m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetPreferDynamicValue();
559         return GetChildMemberWithName (name, use_dynamic_value);
560     }
561     else
562         return GetChildMemberWithName (name, eNoDynamicValues);
563 }
564 
565 SBValue
566 SBValue::GetChildMemberWithName (const char *name, lldb::DynamicValueType use_dynamic_value)
567 {
568     lldb::ValueObjectSP child_sp;
569     const ConstString str_name (name);
570 
571 
572     if (m_opaque_sp)
573     {
574         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
575         {
576             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
577             child_sp = m_opaque_sp->GetChildMemberWithName (str_name, true);
578             if (use_dynamic_value != lldb::eNoDynamicValues)
579             {
580                 if (child_sp)
581                 {
582                     lldb::ValueObjectSP dynamic_sp = child_sp->GetDynamicValue (use_dynamic_value);
583                     if (dynamic_sp)
584                         child_sp = dynamic_sp;
585                 }
586             }
587         }
588     }
589 
590     SBValue sb_value (child_sp);
591 
592     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
593     if (log)
594         log->Printf ("SBValue(%p)::GetChildMemberWithName (name=\"%s\") => SBValue(%p)", m_opaque_sp.get(), name, sb_value.get());
595 
596     return sb_value;
597 }
598 
599 lldb::SBValue
600 SBValue::GetDynamicValue (lldb::DynamicValueType use_dynamic)
601 {
602     if (m_opaque_sp)
603     {
604         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
605         {
606             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
607             return SBValue (m_opaque_sp->GetDynamicValue(use_dynamic));
608         }
609     }
610 
611     return SBValue();
612 }
613 
614 lldb::SBValue
615 SBValue::GetStaticValue ()
616 {
617     if (m_opaque_sp)
618     {
619         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
620         {
621             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
622             return SBValue(m_opaque_sp->GetStaticValue());
623         }
624     }
625 
626     return SBValue();
627 }
628 
629 bool
630 SBValue::IsDynamic()
631 {
632     if (m_opaque_sp)
633     {
634         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
635         {
636             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
637             return m_opaque_sp->IsDynamic();
638         }
639     }
640     return false;
641 }
642 
643 lldb::SBValue
644 SBValue::GetValueForExpressionPath(const char* expr_path)
645 {
646     lldb::ValueObjectSP child_sp;
647     if (m_opaque_sp)
648     {
649         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
650         {
651             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
652             // using default values for all the fancy options, just do it if you can
653             child_sp = m_opaque_sp->GetValueForExpressionPath(expr_path);
654         }
655     }
656 
657     SBValue sb_value (child_sp);
658 
659     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
660     if (log)
661         log->Printf ("SBValue(%p)::GetValueForExpressionPath (expr_path=\"%s\") => SBValue(%p)", m_opaque_sp.get(), expr_path, sb_value.get());
662 
663     return sb_value;
664 }
665 
666 int64_t
667 SBValue::GetValueAsSigned(SBError& error, int64_t fail_value)
668 {
669     error.Clear();
670     if (m_opaque_sp)
671     {
672         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
673         {
674             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
675             Scalar scalar;
676             if (m_opaque_sp->ResolveValue (scalar))
677                 return scalar.GetRawBits64(fail_value);
678             else
679                 error.SetErrorString("could not get value");
680         }
681         else
682             error.SetErrorString("could not get target");
683     }
684     error.SetErrorString("invalid SBValue");
685     return fail_value;
686 }
687 
688 uint64_t
689 SBValue::GetValueAsUnsigned(SBError& error, uint64_t fail_value)
690 {
691     error.Clear();
692     if (m_opaque_sp)
693     {
694         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
695         {
696             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
697             Scalar scalar;
698             if (m_opaque_sp->ResolveValue (scalar))
699                 return scalar.GetRawBits64(fail_value);
700             else
701                 error.SetErrorString("could not get value");
702         }
703         else
704             error.SetErrorString("could not get target");
705     }
706     error.SetErrorString("invalid SBValue");
707     return fail_value;
708 }
709 
710 int64_t
711 SBValue::GetValueAsSigned(int64_t fail_value)
712 {
713     if (m_opaque_sp)
714     {
715         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
716         {
717             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
718             Scalar scalar;
719             if (m_opaque_sp->ResolveValue (scalar))
720                 return scalar.GetRawBits64(fail_value);
721         }
722     }
723     return fail_value;
724 }
725 
726 uint64_t
727 SBValue::GetValueAsUnsigned(uint64_t fail_value)
728 {
729     if (m_opaque_sp)
730     {
731         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
732         {
733             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
734             Scalar scalar;
735             if (m_opaque_sp->ResolveValue (scalar))
736                 return scalar.GetRawBits64(fail_value);
737         }
738     }
739     return fail_value;
740 }
741 
742 uint32_t
743 SBValue::GetNumChildren ()
744 {
745     uint32_t num_children = 0;
746 
747     if (m_opaque_sp)
748     {
749         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
750         {
751             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
752 
753             num_children = m_opaque_sp->GetNumChildren();
754         }
755     }
756 
757     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
758     if (log)
759         log->Printf ("SBValue(%p)::GetNumChildren () => %u", m_opaque_sp.get(), num_children);
760 
761     return num_children;
762 }
763 
764 
765 SBValue
766 SBValue::Dereference ()
767 {
768     SBValue sb_value;
769     if (m_opaque_sp)
770     {
771         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
772         {
773             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
774 
775             Error error;
776             sb_value = m_opaque_sp->Dereference (error);
777         }
778     }
779     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
780     if (log)
781         log->Printf ("SBValue(%p)::Dereference () => SBValue(%p)", m_opaque_sp.get(), sb_value.get());
782 
783     return sb_value;
784 }
785 
786 bool
787 SBValue::TypeIsPointerType ()
788 {
789     bool is_ptr_type = false;
790 
791     if (m_opaque_sp)
792     {
793         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
794         {
795             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
796 
797             is_ptr_type = m_opaque_sp->IsPointerType();
798         }
799     }
800 
801     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
802     if (log)
803         log->Printf ("SBValue(%p)::TypeIsPointerType () => %i", m_opaque_sp.get(), is_ptr_type);
804 
805 
806     return is_ptr_type;
807 }
808 
809 void *
810 SBValue::GetOpaqueType()
811 {
812     if (m_opaque_sp)
813     {
814         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
815         {
816             Mutex::Locker api_locker (m_opaque_sp->GetUpdatePoint().GetTargetSP()->GetAPIMutex());
817 
818             return m_opaque_sp->GetClangType();
819         }
820     }
821     return NULL;
822 }
823 
824 lldb::SBTarget
825 SBValue::GetTarget()
826 {
827     SBTarget result;
828     if (m_opaque_sp)
829     {
830         if (m_opaque_sp->GetUpdatePoint().GetTargetSP())
831         {
832             result = SBTarget(lldb::TargetSP(m_opaque_sp->GetUpdatePoint().GetTargetSP()));
833         }
834     }
835     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
836     if (log)
837     {
838         if (result.get() == NULL)
839             log->Printf ("SBValue(%p)::GetTarget () => NULL", m_opaque_sp.get());
840         else
841             log->Printf ("SBValue(%p)::GetTarget () => %p", m_opaque_sp.get(), result.get());
842     }
843     return result;
844 }
845 
846 lldb::SBProcess
847 SBValue::GetProcess()
848 {
849     SBProcess result;
850     if (m_opaque_sp)
851     {
852         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
853         if (target)
854         {
855             result = SBProcess(lldb::ProcessSP(target->GetProcessSP()));
856         }
857     }
858     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
859     if (log)
860     {
861         if (result.get() == NULL)
862             log->Printf ("SBValue(%p)::GetProcess () => NULL", m_opaque_sp.get());
863         else
864             log->Printf ("SBValue(%p)::GetProcess () => %p", m_opaque_sp.get(), result.get());
865     }
866     return result;
867 }
868 
869 lldb::SBThread
870 SBValue::GetThread()
871 {
872     SBThread result;
873     if (m_opaque_sp)
874     {
875         if (m_opaque_sp->GetUpdatePoint().GetExecutionContextScope())
876         {
877             result = SBThread(m_opaque_sp->GetUpdatePoint().GetExecutionContextScope()->CalculateThread()->GetSP());
878         }
879     }
880     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
881     if (log)
882     {
883         if (result.get() == NULL)
884             log->Printf ("SBValue(%p)::GetThread () => NULL", m_opaque_sp.get());
885         else
886             log->Printf ("SBValue(%p)::GetThread () => %p", m_opaque_sp.get(), result.get());
887     }
888     return result;
889 }
890 
891 lldb::SBFrame
892 SBValue::GetFrame()
893 {
894     SBFrame result;
895     if (m_opaque_sp)
896     {
897         if (m_opaque_sp->GetUpdatePoint().GetExecutionContextScope())
898         {
899             result.SetFrame (m_opaque_sp->GetUpdatePoint().GetExecutionContextScope()->CalculateStackFrame()->GetSP());
900         }
901     }
902     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
903     if (log)
904     {
905         if (result.get() == NULL)
906             log->Printf ("SBValue(%p)::GetFrame () => NULL", m_opaque_sp.get());
907         else
908             log->Printf ("SBValue(%p)::GetFrame () => %p", m_opaque_sp.get(), result.get());
909     }
910     return result;
911 }
912 
913 
914 // Mimic shared pointer...
915 lldb_private::ValueObject *
916 SBValue::get() const
917 {
918     return m_opaque_sp.get();
919 }
920 
921 lldb_private::ValueObject *
922 SBValue::operator->() const
923 {
924     return m_opaque_sp.get();
925 }
926 
927 lldb::ValueObjectSP &
928 SBValue::operator*()
929 {
930     return m_opaque_sp;
931 }
932 
933 const lldb::ValueObjectSP &
934 SBValue::operator*() const
935 {
936     return m_opaque_sp;
937 }
938 
939 bool
940 SBValue::GetExpressionPath (SBStream &description)
941 {
942     if (m_opaque_sp)
943     {
944         m_opaque_sp->GetExpressionPath (description.ref(), false);
945         return true;
946     }
947     return false;
948 }
949 
950 bool
951 SBValue::GetExpressionPath (SBStream &description, bool qualify_cxx_base_classes)
952 {
953     if (m_opaque_sp)
954     {
955         m_opaque_sp->GetExpressionPath (description.ref(), qualify_cxx_base_classes);
956         return true;
957     }
958     return false;
959 }
960 
961 bool
962 SBValue::GetDescription (SBStream &description)
963 {
964     Stream &strm = description.ref();
965 
966     if (m_opaque_sp)
967     {
968         ValueObject::DumpValueObject (strm, m_opaque_sp.get());
969     }
970     else
971         strm.PutCString ("No value");
972 
973     return true;
974 }
975 
976 lldb::Format
977 SBValue::GetFormat ()
978 {
979     if (m_opaque_sp)
980         return m_opaque_sp->GetFormat();
981     return eFormatDefault;
982 }
983 
984 void
985 SBValue::SetFormat (lldb::Format format)
986 {
987     if (m_opaque_sp)
988         m_opaque_sp->SetFormat(format);
989 }
990 
991 lldb::SBValue
992 SBValue::AddressOf()
993 {
994     SBValue sb_value;
995     if (m_opaque_sp)
996     {
997         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
998         if (target)
999         {
1000             Mutex::Locker api_locker (target->GetAPIMutex());
1001             Error error;
1002             sb_value = m_opaque_sp->AddressOf (error);
1003         }
1004     }
1005     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1006     if (log)
1007         log->Printf ("SBValue(%p)::GetPointerToObject () => SBValue(%p)", m_opaque_sp.get(), sb_value.get());
1008 
1009     return sb_value;
1010 }
1011 
1012 lldb::addr_t
1013 SBValue::GetLoadAddress()
1014 {
1015     lldb::addr_t value = LLDB_INVALID_ADDRESS;
1016     if (m_opaque_sp)
1017     {
1018         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1019         if (target)
1020         {
1021             Mutex::Locker api_locker (target->GetAPIMutex());
1022             const bool scalar_is_load_address = true;
1023             AddressType addr_type;
1024             value = m_opaque_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1025             if (addr_type == eAddressTypeFile)
1026             {
1027                 Module* module = m_opaque_sp->GetModule();
1028                 if (!module)
1029                     value = LLDB_INVALID_ADDRESS;
1030                 else
1031                 {
1032                     Address addr;
1033                     module->ResolveFileAddress(value, addr);
1034                     value = addr.GetLoadAddress(m_opaque_sp->GetUpdatePoint().GetTargetSP().get());
1035                 }
1036             }
1037             else if (addr_type == eAddressTypeHost || addr_type == eAddressTypeInvalid)
1038                 value = LLDB_INVALID_ADDRESS;
1039         }
1040     }
1041     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1042     if (log)
1043         log->Printf ("SBValue(%p)::GetLoadAddress () => (%llu)", m_opaque_sp.get(), value);
1044 
1045     return value;
1046 }
1047 
1048 lldb::SBAddress
1049 SBValue::GetAddress()
1050 {
1051     Address addr;
1052     if (m_opaque_sp)
1053     {
1054         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1055         if (target)
1056         {
1057             lldb::addr_t value = LLDB_INVALID_ADDRESS;
1058             Mutex::Locker api_locker (target->GetAPIMutex());
1059             const bool scalar_is_load_address = true;
1060             AddressType addr_type;
1061             value = m_opaque_sp->GetAddressOf(scalar_is_load_address, &addr_type);
1062             if (addr_type == eAddressTypeFile)
1063             {
1064                 Module* module = m_opaque_sp->GetModule();
1065                 if (module)
1066                     module->ResolveFileAddress(value, addr);
1067             }
1068             else if (addr_type == eAddressTypeLoad)
1069             {
1070                 // no need to check the return value on this.. if it can actually do the resolve
1071                 // addr will be in the form (section,offset), otherwise it will simply be returned
1072                 // as (NULL, value)
1073                 addr.SetLoadAddress(value, target);
1074             }
1075         }
1076     }
1077     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1078     if (log)
1079         log->Printf ("SBValue(%p)::GetAddress () => (%s,%llu)", m_opaque_sp.get(), (addr.GetSection() ? addr.GetSection()->GetName().GetCString() : "NULL"), addr.GetOffset());
1080     return SBAddress(new Address(addr));
1081 }
1082 
1083 lldb::SBData
1084 SBValue::GetPointeeData (uint32_t item_idx,
1085                          uint32_t item_count)
1086 {
1087     lldb::SBData sb_data;
1088     if (m_opaque_sp)
1089     {
1090         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1091         if (target)
1092         {
1093 			DataExtractorSP data_sp(new DataExtractor());
1094             Mutex::Locker api_locker (target->GetAPIMutex());
1095             m_opaque_sp->GetPointeeData(*data_sp, item_idx, item_count);
1096             if (data_sp->GetByteSize() > 0)
1097                 *sb_data = data_sp;
1098         }
1099     }
1100     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1101     if (log)
1102         log->Printf ("SBValue(%p)::GetPointeeData (%d, %d) => SBData(%p)",
1103                      m_opaque_sp.get(),
1104                      item_idx,
1105                      item_count,
1106                      sb_data.get());
1107 
1108     return sb_data;
1109 }
1110 
1111 lldb::SBData
1112 SBValue::GetData ()
1113 {
1114     lldb::SBData sb_data;
1115     if (m_opaque_sp)
1116     {
1117         Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1118         if (target)
1119         {
1120 			DataExtractorSP data_sp(new DataExtractor());
1121             Mutex::Locker api_locker (target->GetAPIMutex());
1122             m_opaque_sp->GetData(*data_sp);
1123             if (data_sp->GetByteSize() > 0)
1124                 *sb_data = data_sp;
1125         }
1126     }
1127     LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1128     if (log)
1129         log->Printf ("SBValue(%p)::GetData () => SBData(%p)",
1130                      m_opaque_sp.get(),
1131                      sb_data.get());
1132 
1133     return sb_data;
1134 }
1135 
1136 lldb::SBWatchpoint
1137 SBValue::Watch (bool resolve_location, bool read, bool write)
1138 {
1139     lldb::SBWatchpoint sb_watchpoint;
1140     if (!m_opaque_sp)
1141         return sb_watchpoint;
1142 
1143     Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1144     if (target)
1145     {
1146         Mutex::Locker api_locker (target->GetAPIMutex());
1147         sb_watchpoint = WatchValue(read, write, false);
1148     }
1149     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1150     if (log)
1151         log->Printf ("SBValue(%p)::Watch (resolve_location=%i, read=%i, write=%i) => wp(%p)",
1152                      m_opaque_sp.get(), resolve_location, read, write, sb_watchpoint.get());
1153     return sb_watchpoint;
1154 }
1155 
1156 lldb::SBWatchpoint
1157 SBValue::WatchPointee (bool resolve_location, bool read, bool write)
1158 {
1159     lldb::SBWatchpoint sb_watchpoint;
1160     if (!m_opaque_sp)
1161         return sb_watchpoint;
1162 
1163     Target* target = m_opaque_sp->GetUpdatePoint().GetTargetSP().get();
1164     if (target)
1165     {
1166         Mutex::Locker api_locker (target->GetAPIMutex());
1167         sb_watchpoint = WatchValue(read, write, true);
1168     }
1169     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
1170     if (log)
1171         log->Printf ("SBValue(%p)::WatchPointee (resolve_location=%i, read=%i, write=%i) => wp(%p)",
1172                      m_opaque_sp.get(), resolve_location, read, write, sb_watchpoint.get());
1173     return sb_watchpoint;
1174 }
1175 
1176 // Helper function for SBValue::Watch() and SBValue::WatchPointee().
1177 SBWatchpoint
1178 SBValue::WatchValue(bool read, bool write, bool watch_pointee)
1179 {
1180     SBWatchpoint sb_wp_empty;
1181 
1182     // If the SBValue is not valid, there's no point in even trying to watch it.
1183     if (!IsValid() || !GetFrame().IsValid())
1184         return sb_wp_empty;
1185 
1186     // Read and Write cannot both be false.
1187     if (!read && !write)
1188         return sb_wp_empty;
1189 
1190     // If we are watching the pointee, check that the SBValue is a pointer type.
1191     if (watch_pointee && !GetType().IsPointerType())
1192         return sb_wp_empty;
1193 
1194     addr_t addr;
1195     size_t size;
1196     if (watch_pointee) {
1197         addr = GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
1198         size = GetType().GetPointeeType().GetByteSize();
1199     } else {
1200         addr = GetLoadAddress();
1201         size = GetByteSize();
1202     }
1203 
1204     // Sanity check the address and the size before calling Target::CreateWatchpoint().
1205     if (addr == LLDB_INVALID_ADDRESS || size == 0)
1206         return sb_wp_empty;
1207 
1208     uint32_t watch_type = (read ? LLDB_WATCH_TYPE_READ : 0) |
1209         (write ? LLDB_WATCH_TYPE_WRITE : 0);
1210     WatchpointSP wp_sp = GetFrame().m_opaque_sp->GetThread().GetProcess().GetTarget().
1211         CreateWatchpoint(addr, size, watch_type);
1212 
1213     if (wp_sp) {
1214         // StackFrame::GetInScopeVariableList(true) to get file globals as well.
1215         VariableListSP var_list_sp(GetFrame().m_opaque_sp->GetInScopeVariableList(true));
1216         VariableSP var_sp = var_list_sp->FindVariable(ConstString(GetName()));
1217         if (var_sp && var_sp->GetDeclaration().GetFile()) {
1218             StreamString ss;
1219             // True to show fullpath for declaration file.
1220             var_sp->GetDeclaration().DumpStopContext(&ss, true);
1221             wp_sp->SetDeclInfo(ss.GetString());
1222         }
1223     }
1224     return wp_sp;
1225 }
1226 
1227