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