1 //===-- SBFrame.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/SBFrame.h"
11 
12 #include <string>
13 #include <algorithm>
14 
15 #include "lldb/lldb-types.h"
16 
17 #include "lldb/Core/Address.h"
18 #include "lldb/Core/ConstString.h"
19 #include "lldb/Core/Log.h"
20 #include "lldb/Core/Stream.h"
21 #include "lldb/Core/StreamFile.h"
22 #include "lldb/Core/ValueObjectRegister.h"
23 #include "lldb/Core/ValueObjectVariable.h"
24 #include "lldb/Expression/ClangUserExpression.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Symbol/Block.h"
27 #include "lldb/Symbol/SymbolContext.h"
28 #include "lldb/Symbol/VariableList.h"
29 #include "lldb/Symbol/Variable.h"
30 #include "lldb/Target/ExecutionContext.h"
31 #include "lldb/Target/Target.h"
32 #include "lldb/Target/Process.h"
33 #include "lldb/Target/RegisterContext.h"
34 #include "lldb/Target/StackFrame.h"
35 #include "lldb/Target/StackID.h"
36 #include "lldb/Target/Thread.h"
37 
38 #include "lldb/API/SBDebugger.h"
39 #include "lldb/API/SBValue.h"
40 #include "lldb/API/SBAddress.h"
41 #include "lldb/API/SBStream.h"
42 #include "lldb/API/SBSymbolContext.h"
43 #include "lldb/API/SBThread.h"
44 
45 using namespace lldb;
46 using namespace lldb_private;
47 
48 
49 SBFrame::SBFrame () :
50     m_opaque_sp (new ExecutionContextRef())
51 {
52 }
53 
54 SBFrame::SBFrame (const StackFrameSP &lldb_object_sp) :
55     m_opaque_sp (new ExecutionContextRef (lldb_object_sp))
56 {
57     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
58 
59     if (log)
60     {
61         SBStream sstr;
62         GetDescription (sstr);
63         log->Printf ("SBFrame::SBFrame (sp=%p) => SBFrame(%p): %s",
64                      lldb_object_sp.get(), lldb_object_sp.get(), sstr.GetData());
65 
66     }
67 }
68 
69 SBFrame::SBFrame(const SBFrame &rhs) :
70     m_opaque_sp (new ExecutionContextRef (*rhs.m_opaque_sp))
71 {
72 }
73 
74 const SBFrame &
75 SBFrame::operator = (const SBFrame &rhs)
76 {
77     if (this != &rhs)
78         *m_opaque_sp = *rhs.m_opaque_sp;
79     return *this;
80 }
81 
82 SBFrame::~SBFrame()
83 {
84 }
85 
86 StackFrameSP
87 SBFrame::GetFrameSP() const
88 {
89     return m_opaque_sp->GetFrameSP();
90 }
91 
92 void
93 SBFrame::SetFrameSP (const StackFrameSP &lldb_object_sp)
94 {
95     return m_opaque_sp->SetFrameSP(lldb_object_sp);
96 }
97 
98 bool
99 SBFrame::IsValid() const
100 {
101     return GetFrameSP().get() != NULL;
102 }
103 
104 SBSymbolContext
105 SBFrame::GetSymbolContext (uint32_t resolve_scope) const
106 {
107 
108     SBSymbolContext sb_sym_ctx;
109     ExecutionContext exe_ctx(m_opaque_sp.get());
110     StackFrame *frame = exe_ctx.GetFramePtr();
111     Target *target = exe_ctx.GetTargetPtr();
112     if (frame && target)
113     {
114         Process::StopLocker stop_locker;
115         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
116         {
117             Mutex::Locker api_locker (target->GetAPIMutex());
118             sb_sym_ctx.SetSymbolContext(&frame->GetSymbolContext (resolve_scope));
119         }
120     }
121 
122     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
123     if (log)
124         log->Printf ("SBFrame(%p)::GetSymbolContext (resolve_scope=0x%8.8x) => SBSymbolContext(%p)",
125                      frame, resolve_scope, sb_sym_ctx.get());
126 
127     return sb_sym_ctx;
128 }
129 
130 SBModule
131 SBFrame::GetModule () const
132 {
133     SBModule sb_module;
134     ModuleSP module_sp;
135     ExecutionContext exe_ctx(m_opaque_sp.get());
136     StackFrame *frame = exe_ctx.GetFramePtr();
137     Target *target = exe_ctx.GetTargetPtr();
138     if (frame && target)
139     {
140         Process::StopLocker stop_locker;
141         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
142         {
143             Mutex::Locker api_locker (target->GetAPIMutex());
144             module_sp = frame->GetSymbolContext (eSymbolContextModule).module_sp;
145             sb_module.SetSP (module_sp);
146         }
147     }
148 
149     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
150     if (log)
151         log->Printf ("SBFrame(%p)::GetModule () => SBModule(%p)",
152                      frame, module_sp.get());
153 
154     return sb_module;
155 }
156 
157 SBCompileUnit
158 SBFrame::GetCompileUnit () const
159 {
160     SBCompileUnit sb_comp_unit;
161     ExecutionContext exe_ctx(m_opaque_sp.get());
162     StackFrame *frame = exe_ctx.GetFramePtr();
163     Target *target = exe_ctx.GetTargetPtr();
164     if (frame && target)
165     {
166         Process::StopLocker stop_locker;
167         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
168         {
169             Mutex::Locker api_locker (target->GetAPIMutex());
170             sb_comp_unit.reset (frame->GetSymbolContext (eSymbolContextCompUnit).comp_unit);
171         }
172     }
173     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
174     if (log)
175         log->Printf ("SBFrame(%p)::GetModule () => SBCompileUnit(%p)",
176                      frame, sb_comp_unit.get());
177 
178     return sb_comp_unit;
179 }
180 
181 SBFunction
182 SBFrame::GetFunction () const
183 {
184     SBFunction sb_function;
185     ExecutionContext exe_ctx(m_opaque_sp.get());
186     StackFrame *frame = exe_ctx.GetFramePtr();
187     Target *target = exe_ctx.GetTargetPtr();
188     if (frame && target)
189     {
190         Process::StopLocker stop_locker;
191         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
192         {
193             Mutex::Locker api_locker (target->GetAPIMutex());
194             sb_function.reset(frame->GetSymbolContext (eSymbolContextFunction).function);
195         }
196     }
197     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
198     if (log)
199         log->Printf ("SBFrame(%p)::GetFunction () => SBFunction(%p)",
200                      frame, sb_function.get());
201 
202     return sb_function;
203 }
204 
205 SBSymbol
206 SBFrame::GetSymbol () const
207 {
208     SBSymbol sb_symbol;
209     ExecutionContext exe_ctx(m_opaque_sp.get());
210     StackFrame *frame = exe_ctx.GetFramePtr();
211     Target *target = exe_ctx.GetTargetPtr();
212     if (frame && target)
213     {
214         Process::StopLocker stop_locker;
215         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
216         {
217             Mutex::Locker api_locker (target->GetAPIMutex());
218             sb_symbol.reset(frame->GetSymbolContext (eSymbolContextSymbol).symbol);
219         }
220     }
221     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
222     if (log)
223         log->Printf ("SBFrame(%p)::GetSymbol () => SBSymbol(%p)",
224                      frame, sb_symbol.get());
225     return sb_symbol;
226 }
227 
228 SBBlock
229 SBFrame::GetBlock () const
230 {
231     SBBlock sb_block;
232     ExecutionContext exe_ctx(m_opaque_sp.get());
233     StackFrame *frame = exe_ctx.GetFramePtr();
234     Target *target = exe_ctx.GetTargetPtr();
235     if (frame && target)
236     {
237         Process::StopLocker stop_locker;
238         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
239         {
240             Mutex::Locker api_locker (target->GetAPIMutex());
241             sb_block.SetPtr (frame->GetSymbolContext (eSymbolContextBlock).block);
242         }
243     }
244     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
245     if (log)
246         log->Printf ("SBFrame(%p)::GetBlock () => SBBlock(%p)",
247                      frame, sb_block.GetPtr());
248     return sb_block;
249 }
250 
251 SBBlock
252 SBFrame::GetFrameBlock () const
253 {
254     SBBlock sb_block;
255     ExecutionContext exe_ctx(m_opaque_sp.get());
256     StackFrame *frame = exe_ctx.GetFramePtr();
257     Target *target = exe_ctx.GetTargetPtr();
258     if (frame && target)
259     {
260         Process::StopLocker stop_locker;
261         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
262         {
263             Mutex::Locker api_locker (target->GetAPIMutex());
264             sb_block.SetPtr(frame->GetFrameBlock ());
265         }
266     }
267     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
268     if (log)
269         log->Printf ("SBFrame(%p)::GetFrameBlock () => SBBlock(%p)",
270                      frame, sb_block.GetPtr());
271     return sb_block;
272 }
273 
274 SBLineEntry
275 SBFrame::GetLineEntry () const
276 {
277     SBLineEntry sb_line_entry;
278     ExecutionContext exe_ctx(m_opaque_sp.get());
279     StackFrame *frame = exe_ctx.GetFramePtr();
280     Target *target = exe_ctx.GetTargetPtr();
281     if (frame && target)
282     {
283         Process::StopLocker stop_locker;
284         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
285         {
286             Mutex::Locker api_locker (target->GetAPIMutex());
287             sb_line_entry.SetLineEntry (frame->GetSymbolContext (eSymbolContextLineEntry).line_entry);
288         }
289     }
290     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
291     if (log)
292         log->Printf ("SBFrame(%p)::GetLineEntry () => SBLineEntry(%p)",
293                      frame, sb_line_entry.get());
294     return sb_line_entry;
295 }
296 
297 uint32_t
298 SBFrame::GetFrameID () const
299 {
300     uint32_t frame_idx = UINT32_MAX;
301 
302     ExecutionContext exe_ctx(m_opaque_sp.get());
303     StackFrame *frame = exe_ctx.GetFramePtr();
304     if (frame)
305         frame_idx = frame->GetFrameIndex ();
306 
307     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
308     if (log)
309         log->Printf ("SBFrame(%p)::GetFrameID () => %u",
310                      frame, frame_idx);
311     return frame_idx;
312 }
313 
314 addr_t
315 SBFrame::GetPC () const
316 {
317     addr_t addr = LLDB_INVALID_ADDRESS;
318     ExecutionContext exe_ctx(m_opaque_sp.get());
319     StackFrame *frame = exe_ctx.GetFramePtr();
320     Target *target = exe_ctx.GetTargetPtr();
321     if (frame && target)
322     {
323         Process::StopLocker stop_locker;
324         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
325         {
326             Mutex::Locker api_locker (target->GetAPIMutex());
327             addr = frame->GetFrameCodeAddress().GetOpcodeLoadAddress (target);
328         }
329     }
330 
331     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
332     if (log)
333         log->Printf ("SBFrame(%p)::GetPC () => 0x%llx", frame, addr);
334 
335     return addr;
336 }
337 
338 bool
339 SBFrame::SetPC (addr_t new_pc)
340 {
341     bool ret_val = false;
342     ExecutionContext exe_ctx(m_opaque_sp.get());
343     StackFrame *frame = exe_ctx.GetFramePtr();
344     Target *target = exe_ctx.GetTargetPtr();
345     if (frame && target)
346     {
347         Process::StopLocker stop_locker;
348         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
349         {
350             Mutex::Locker api_locker (target->GetAPIMutex());
351             ret_val = frame->GetRegisterContext()->SetPC (new_pc);
352         }
353     }
354 
355     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
356     if (log)
357         log->Printf ("SBFrame(%p)::SetPC (new_pc=0x%llx) => %i",
358                      frame, new_pc, ret_val);
359 
360     return ret_val;
361 }
362 
363 addr_t
364 SBFrame::GetSP () const
365 {
366     addr_t addr = LLDB_INVALID_ADDRESS;
367     ExecutionContext exe_ctx(m_opaque_sp.get());
368     StackFrame *frame = exe_ctx.GetFramePtr();
369     Target *target = exe_ctx.GetTargetPtr();
370     if (frame && target)
371     {
372         Process::StopLocker stop_locker;
373         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
374         {
375             Mutex::Locker api_locker (target->GetAPIMutex());
376             addr = frame->GetRegisterContext()->GetSP();
377         }
378     }
379     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
380     if (log)
381         log->Printf ("SBFrame(%p)::GetSP () => 0x%llx", frame, addr);
382 
383     return addr;
384 }
385 
386 
387 addr_t
388 SBFrame::GetFP () const
389 {
390     addr_t addr = LLDB_INVALID_ADDRESS;
391     ExecutionContext exe_ctx(m_opaque_sp.get());
392     StackFrame *frame = exe_ctx.GetFramePtr();
393     Target *target = exe_ctx.GetTargetPtr();
394     if (frame && target)
395     {
396         Process::StopLocker stop_locker;
397         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
398         {
399             Mutex::Locker api_locker (target->GetAPIMutex());
400             addr = frame->GetRegisterContext()->GetFP();
401         }
402     }
403 
404     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
405     if (log)
406         log->Printf ("SBFrame(%p)::GetFP () => 0x%llx", frame, addr);
407     return addr;
408 }
409 
410 
411 SBAddress
412 SBFrame::GetPCAddress () const
413 {
414     SBAddress sb_addr;
415     ExecutionContext exe_ctx(m_opaque_sp.get());
416     StackFrame *frame = exe_ctx.GetFramePtr();
417     Target *target = exe_ctx.GetTargetPtr();
418     if (frame && target)
419     {
420         Process::StopLocker stop_locker;
421         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
422         {
423             Mutex::Locker api_locker (target->GetAPIMutex());
424             sb_addr.SetAddress (&frame->GetFrameCodeAddress());
425         }
426     }
427     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
428     if (log)
429         log->Printf ("SBFrame(%p)::GetPCAddress () => SBAddress(%p)", frame, sb_addr.get());
430     return sb_addr;
431 }
432 
433 void
434 SBFrame::Clear()
435 {
436     m_opaque_sp.reset();
437 }
438 
439 lldb::SBValue
440 SBFrame::GetValueForVariablePath (const char *var_path)
441 {
442     SBValue sb_value;
443     ExecutionContext exe_ctx(m_opaque_sp.get());
444     StackFrame *frame = exe_ctx.GetFramePtr();
445     Target *target = exe_ctx.GetTargetPtr();
446     if (frame && target)
447     {
448         Process::StopLocker stop_locker;
449         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
450         {
451             lldb::DynamicValueType  use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue();
452             sb_value = GetValueForVariablePath (var_path, use_dynamic);
453         }
454     }
455     return sb_value;
456 }
457 
458 lldb::SBValue
459 SBFrame::GetValueForVariablePath (const char *var_path, DynamicValueType use_dynamic)
460 {
461     SBValue sb_value;
462     ExecutionContext exe_ctx(m_opaque_sp.get());
463     StackFrame *frame = exe_ctx.GetFramePtr();
464     Target *target = exe_ctx.GetTargetPtr();
465     if (frame && target && var_path && var_path[0])
466     {
467         Process::StopLocker stop_locker;
468         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
469         {
470             Mutex::Locker api_locker (target->GetAPIMutex());
471             VariableSP var_sp;
472             Error error;
473             ValueObjectSP value_sp (frame->GetValueForVariableExpressionPath (var_path,
474                                                                               use_dynamic,
475                                                                               StackFrame::eExpressionPathOptionCheckPtrVsMember,
476                                                                               var_sp,
477                                                                               error));
478             sb_value.SetSP(value_sp);
479         }
480     }
481     return sb_value;
482 }
483 
484 SBValue
485 SBFrame::FindVariable (const char *name)
486 {
487     SBValue value;
488     ExecutionContext exe_ctx(m_opaque_sp.get());
489     StackFrame *frame = exe_ctx.GetFramePtr();
490     Target *target = exe_ctx.GetTargetPtr();
491     if (frame && target)
492     {
493         lldb::DynamicValueType  use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue();
494         value = FindVariable (name, use_dynamic);
495     }
496     return value;
497 }
498 
499 
500 SBValue
501 SBFrame::FindVariable (const char *name, lldb::DynamicValueType use_dynamic)
502 {
503     VariableSP var_sp;
504     SBValue sb_value;
505     ValueObjectSP value_sp;
506     ExecutionContext exe_ctx(m_opaque_sp.get());
507     StackFrame *frame = exe_ctx.GetFramePtr();
508     Target *target = exe_ctx.GetTargetPtr();
509     if (frame && target && name && name[0])
510     {
511         Process::StopLocker stop_locker;
512         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
513         {
514             VariableList variable_list;
515             Mutex::Locker api_locker (target->GetAPIMutex());
516             SymbolContext sc (frame->GetSymbolContext (eSymbolContextBlock));
517 
518             if (sc.block)
519             {
520                 const bool can_create = true;
521                 const bool get_parent_variables = true;
522                 const bool stop_if_block_is_inlined_function = true;
523 
524                 if (sc.block->AppendVariables (can_create,
525                                                get_parent_variables,
526                                                stop_if_block_is_inlined_function,
527                                                &variable_list))
528                 {
529                     var_sp = variable_list.FindVariable (ConstString(name));
530                 }
531             }
532 
533             if (var_sp)
534             {
535                 value_sp = frame->GetValueObjectForFrameVariable(var_sp, use_dynamic);
536                 sb_value.SetSP(value_sp);
537             }
538         }
539     }
540 
541     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
542     if (log)
543         log->Printf ("SBFrame(%p)::FindVariable (name=\"%s\") => SBValue(%p)",
544                      frame, name, value_sp.get());
545 
546     return sb_value;
547 }
548 
549 SBValue
550 SBFrame::FindValue (const char *name, ValueType value_type)
551 {
552     SBValue value;
553     ExecutionContext exe_ctx(m_opaque_sp.get());
554     StackFrame *frame = exe_ctx.GetFramePtr();
555     Target *target = exe_ctx.GetTargetPtr();
556     if (frame && target)
557     {
558         lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue();
559         value = FindValue (name, value_type, use_dynamic);
560     }
561     return value;
562 }
563 
564 SBValue
565 SBFrame::FindValue (const char *name, ValueType value_type, lldb::DynamicValueType use_dynamic)
566 {
567     SBValue sb_value;
568     ValueObjectSP value_sp;
569     ExecutionContext exe_ctx(m_opaque_sp.get());
570     StackFrame *frame = exe_ctx.GetFramePtr();
571     Target *target = exe_ctx.GetTargetPtr();
572     if (frame && target && name && name[0])
573     {
574         Process::StopLocker stop_locker;
575         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
576         {
577             Mutex::Locker api_locker (target->GetAPIMutex());
578 
579             switch (value_type)
580             {
581             case eValueTypeVariableGlobal:      // global variable
582             case eValueTypeVariableStatic:      // static variable
583             case eValueTypeVariableArgument:    // function argument variables
584             case eValueTypeVariableLocal:       // function local variables
585                 {
586                     VariableList *variable_list = frame->GetVariableList(true);
587 
588                     SymbolContext sc (frame->GetSymbolContext (eSymbolContextBlock));
589 
590                     const bool can_create = true;
591                     const bool get_parent_variables = true;
592                     const bool stop_if_block_is_inlined_function = true;
593 
594                     if (sc.block && sc.block->AppendVariables (can_create,
595                                                                get_parent_variables,
596                                                                stop_if_block_is_inlined_function,
597                                                                variable_list))
598                     {
599                         ConstString const_name(name);
600                         const uint32_t num_variables = variable_list->GetSize();
601                         for (uint32_t i = 0; i < num_variables; ++i)
602                         {
603                             VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
604                             if (variable_sp &&
605                                 variable_sp->GetScope() == value_type &&
606                                 variable_sp->GetName() == const_name)
607                             {
608                                 value_sp = frame->GetValueObjectForFrameVariable (variable_sp, use_dynamic);
609                                 sb_value.SetSP (value_sp);
610                                 break;
611                             }
612                         }
613                     }
614                 }
615                 break;
616 
617             case eValueTypeRegister:            // stack frame register value
618                 {
619                     RegisterContextSP reg_ctx (frame->GetRegisterContext());
620                     if (reg_ctx)
621                     {
622                         const uint32_t num_regs = reg_ctx->GetRegisterCount();
623                         for (uint32_t reg_idx = 0; reg_idx < num_regs; ++reg_idx)
624                         {
625                             const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex (reg_idx);
626                             if (reg_info &&
627                                 ((reg_info->name && strcasecmp (reg_info->name, name) == 0) ||
628                                  (reg_info->alt_name && strcasecmp (reg_info->alt_name, name) == 0)))
629                             {
630                                 value_sp = ValueObjectRegister::Create (frame, reg_ctx, reg_idx);
631                                 sb_value.SetSP (value_sp);
632                                 break;
633                             }
634                         }
635                     }
636                 }
637                 break;
638 
639             case eValueTypeRegisterSet:         // A collection of stack frame register values
640                 {
641                     RegisterContextSP reg_ctx (frame->GetRegisterContext());
642                     if (reg_ctx)
643                     {
644                         const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
645                         for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx)
646                         {
647                             const RegisterSet *reg_set = reg_ctx->GetRegisterSet (set_idx);
648                             if (reg_set &&
649                                 ((reg_set->name && strcasecmp (reg_set->name, name) == 0) ||
650                                  (reg_set->short_name && strcasecmp (reg_set->short_name, name) == 0)))
651                             {
652                                 value_sp = ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx);
653                                 sb_value.SetSP (value_sp);
654                                 break;
655                             }
656                         }
657                     }
658                 }
659                 break;
660 
661             case eValueTypeConstResult:         // constant result variables
662                 {
663                     ConstString const_name(name);
664                     ClangExpressionVariableSP expr_var_sp (target->GetPersistentVariables().GetVariable (const_name));
665                     if (expr_var_sp)
666                     {
667                         value_sp = expr_var_sp->GetValueObject();
668                         sb_value.SetSP (value_sp);
669                     }
670                 }
671                 break;
672 
673             default:
674                 break;
675             }
676         }
677     }
678 
679     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
680     if (log)
681         log->Printf ("SBFrame(%p)::FindVariableInScope (name=\"%s\", value_type=%i) => SBValue(%p)",
682                      frame, name, value_type, value_sp.get());
683 
684 
685     return sb_value;
686 }
687 
688 bool
689 SBFrame::IsEqual (const SBFrame &that) const
690 {
691     lldb::StackFrameSP this_sp = GetFrameSP();
692     lldb::StackFrameSP that_sp = that.GetFrameSP();
693     return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID());
694 }
695 
696 bool
697 SBFrame::operator == (const SBFrame &rhs) const
698 {
699     return IsEqual(rhs);
700 }
701 
702 bool
703 SBFrame::operator != (const SBFrame &rhs) const
704 {
705     return !IsEqual(rhs);
706 }
707 
708 SBThread
709 SBFrame::GetThread () const
710 {
711     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
712 
713     ExecutionContext exe_ctx(m_opaque_sp.get());
714     ThreadSP thread_sp (exe_ctx.GetThreadSP());
715     SBThread sb_thread (thread_sp);
716 
717     if (log)
718     {
719         SBStream sstr;
720         sb_thread.GetDescription (sstr);
721         log->Printf ("SBFrame(%p)::GetThread () => SBThread(%p): %s",
722                      exe_ctx.GetFramePtr(),
723                      thread_sp.get(),
724                      sstr.GetData());
725     }
726 
727     return sb_thread;
728 }
729 
730 const char *
731 SBFrame::Disassemble () const
732 {
733     const char *disassembly = NULL;
734     ExecutionContext exe_ctx(m_opaque_sp.get());
735     StackFrame *frame = exe_ctx.GetFramePtr();
736     Target *target = exe_ctx.GetTargetPtr();
737     if (frame && target)
738     {
739         Process::StopLocker stop_locker;
740         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
741         {
742             Mutex::Locker api_locker (target->GetAPIMutex());
743             disassembly = frame->Disassemble();
744         }
745     }
746     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
747 
748     if (log)
749         log->Printf ("SBFrame(%p)::Disassemble () => %s", frame, disassembly);
750 
751     return disassembly;
752 }
753 
754 
755 SBValueList
756 SBFrame::GetVariables (bool arguments,
757                        bool locals,
758                        bool statics,
759                        bool in_scope_only)
760 {
761     SBValueList value_list;
762     ExecutionContext exe_ctx(m_opaque_sp.get());
763     StackFrame *frame = exe_ctx.GetFramePtr();
764     Target *target = exe_ctx.GetTargetPtr();
765     if (frame && target)
766     {
767         lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue();
768         value_list = GetVariables (arguments, locals, statics, in_scope_only, use_dynamic);
769     }
770     return value_list;
771 }
772 
773 SBValueList
774 SBFrame::GetVariables (bool arguments,
775                        bool locals,
776                        bool statics,
777                        bool in_scope_only,
778                        lldb::DynamicValueType  use_dynamic)
779 {
780     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
781 
782     SBValueList value_list;
783     ExecutionContext exe_ctx(m_opaque_sp.get());
784     StackFrame *frame = exe_ctx.GetFramePtr();
785     Target *target = exe_ctx.GetTargetPtr();
786 
787     if (log)
788         log->Printf ("SBFrame(%p)::GetVariables (arguments=%i, locals=%i, statics=%i, in_scope_only=%i)",
789                      frame,
790                      arguments,
791                      locals,
792                      statics,
793                      in_scope_only);
794 
795     if (frame && target)
796     {
797         Process::StopLocker stop_locker;
798         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
799         {
800 
801             size_t i;
802             VariableList *variable_list = NULL;
803             // Scope for locker
804             {
805                 Mutex::Locker api_locker (target->GetAPIMutex());
806                 variable_list = frame->GetVariableList(true);
807             }
808             if (variable_list)
809             {
810                 const size_t num_variables = variable_list->GetSize();
811                 if (num_variables)
812                 {
813                     for (i = 0; i < num_variables; ++i)
814                     {
815                         VariableSP variable_sp (variable_list->GetVariableAtIndex(i));
816                         if (variable_sp)
817                         {
818                             bool add_variable = false;
819                             switch (variable_sp->GetScope())
820                             {
821                             case eValueTypeVariableGlobal:
822                             case eValueTypeVariableStatic:
823                                 add_variable = statics;
824                                 break;
825 
826                             case eValueTypeVariableArgument:
827                                 add_variable = arguments;
828                                 break;
829 
830                             case eValueTypeVariableLocal:
831                                 add_variable = locals;
832                                 break;
833 
834                             default:
835                                 break;
836                             }
837                             if (add_variable)
838                             {
839                                 if (in_scope_only && !variable_sp->IsInScope(frame))
840                                     continue;
841 
842                                 value_list.Append(frame->GetValueObjectForFrameVariable (variable_sp, use_dynamic));
843                             }
844                         }
845                     }
846                 }
847             }
848         }
849     }
850 
851     if (log)
852     {
853         log->Printf ("SBFrame(%p)::GetVariables (...) => SBValueList(%p)", frame,
854                      value_list.get());
855     }
856 
857     return value_list;
858 }
859 
860 SBValueList
861 SBFrame::GetRegisters ()
862 {
863     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
864 
865     SBValueList value_list;
866     ExecutionContext exe_ctx(m_opaque_sp.get());
867     StackFrame *frame = exe_ctx.GetFramePtr();
868     Target *target = exe_ctx.GetTargetPtr();
869     if (frame && target)
870     {
871         Process::StopLocker stop_locker;
872         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
873         {
874             Mutex::Locker api_locker (target->GetAPIMutex());
875             RegisterContextSP reg_ctx (frame->GetRegisterContext());
876             if (reg_ctx)
877             {
878                 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
879                 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx)
880                 {
881                     value_list.Append(ValueObjectRegisterSet::Create (frame, reg_ctx, set_idx));
882                 }
883             }
884         }
885     }
886 
887     if (log)
888         log->Printf ("SBFrame(%p)::Registers () => SBValueList(%p)", frame, value_list.get());
889 
890     return value_list;
891 }
892 
893 bool
894 SBFrame::GetDescription (SBStream &description)
895 {
896     Stream &strm = description.ref();
897 
898     ExecutionContext exe_ctx(m_opaque_sp.get());
899     StackFrame *frame = exe_ctx.GetFramePtr();
900     Target *target = exe_ctx.GetTargetPtr();
901     if (frame && target)
902     {
903         Process::StopLocker stop_locker;
904         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
905         {
906             Mutex::Locker api_locker (target->GetAPIMutex());
907             frame->DumpUsingSettingsFormat (&strm);
908         }
909     }
910     else
911         strm.PutCString ("No value");
912 
913     return true;
914 }
915 
916 SBValue
917 SBFrame::EvaluateExpression (const char *expr)
918 {
919     SBValue result;
920     ExecutionContext exe_ctx(m_opaque_sp.get());
921     StackFrame *frame = exe_ctx.GetFramePtr();
922     Target *target = exe_ctx.GetTargetPtr();
923     if (frame && target)
924     {
925         lldb::DynamicValueType use_dynamic = frame->CalculateTarget()->GetPreferDynamicValue();
926         result = EvaluateExpression (expr, use_dynamic);
927     }
928     return result;
929 }
930 
931 SBValue
932 SBFrame::EvaluateExpression (const char *expr, lldb::DynamicValueType fetch_dynamic_value)
933 {
934     LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
935 
936     LogSP expr_log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
937 
938     ExecutionResults exe_results;
939     SBValue expr_result;
940     ValueObjectSP expr_value_sp;
941 
942     ExecutionContext exe_ctx(m_opaque_sp.get());
943     StackFrame *frame = exe_ctx.GetFramePtr();
944     Target *target = exe_ctx.GetTargetPtr();
945     if (log)
946         log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\")...", frame, expr);
947 
948     if (frame && target)
949     {
950         Process::StopLocker stop_locker;
951         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
952         {
953 
954             Mutex::Locker api_locker (target->GetAPIMutex());
955 
956 
957             StreamString frame_description;
958             frame->DumpUsingSettingsFormat (&frame_description);
959 
960             Host::SetCrashDescriptionWithFormat ("SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value = %u) %s",
961                                                  expr, fetch_dynamic_value, frame_description.GetString().c_str());
962 
963             const bool coerce_to_id = false;
964             const bool unwind_on_error = true;
965             const bool keep_in_memory = false;
966 
967             exe_results = target->EvaluateExpression (expr,
968                                                       frame,
969                                                       eExecutionPolicyOnlyWhenNeeded,
970                                                       coerce_to_id,
971                                                       unwind_on_error,
972                                                       keep_in_memory,
973                                                       fetch_dynamic_value,
974                                                       expr_value_sp);
975             expr_result.SetSP(expr_value_sp);
976             Host::SetCrashDescription (NULL);
977         }
978     }
979 
980 #ifndef LLDB_DISABLE_PYTHON
981     if (expr_log)
982         expr_log->Printf("** [SBFrame::EvaluateExpression] Expression result is %s, summary %s **",
983                          expr_result.GetValue(),
984                          expr_result.GetSummary());
985 
986     if (log)
987         log->Printf ("SBFrame(%p)::EvaluateExpression (expr=\"%s\") => SBValue(%p) (execution result=%d)",
988                      frame,
989                      expr,
990                      expr_value_sp.get(),
991                      exe_results);
992 #endif
993 
994     return expr_result;
995 }
996 
997 bool
998 SBFrame::IsInlined()
999 {
1000     ExecutionContext exe_ctx(m_opaque_sp.get());
1001     StackFrame *frame = exe_ctx.GetFramePtr();
1002     Target *target = exe_ctx.GetTargetPtr();
1003     if (frame && target)
1004     {
1005         Process::StopLocker stop_locker;
1006         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1007         {
1008 
1009             Block *block = frame->GetSymbolContext(eSymbolContextBlock).block;
1010             if (block)
1011                 return block->GetContainingInlinedBlock () != NULL;
1012         }
1013     }
1014     return false;
1015 }
1016 
1017 const char *
1018 SBFrame::GetFunctionName()
1019 {
1020     const char *name = NULL;
1021     ExecutionContext exe_ctx(m_opaque_sp.get());
1022     StackFrame *frame = exe_ctx.GetFramePtr();
1023     Target *target = exe_ctx.GetTargetPtr();
1024     if (frame && target)
1025     {
1026         Process::StopLocker stop_locker;
1027         if (stop_locker.TryLock(&exe_ctx.GetProcessPtr()->GetRunLock()))
1028         {
1029             SymbolContext sc (frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextBlock | eSymbolContextSymbol));
1030             if (sc.block)
1031             {
1032                 Block *inlined_block = sc.block->GetContainingInlinedBlock ();
1033                 if (inlined_block)
1034                 {
1035                     const InlineFunctionInfo* inlined_info = inlined_block->GetInlinedFunctionInfo();
1036                     name = inlined_info->GetName().AsCString();
1037                 }
1038             }
1039 
1040             if (name == NULL)
1041             {
1042                 if (sc.function)
1043                     name = sc.function->GetName().GetCString();
1044             }
1045 
1046             if (name == NULL)
1047             {
1048                 if (sc.symbol)
1049                     name = sc.symbol->GetName().GetCString();
1050             }
1051         }
1052     }
1053     return name;
1054 }
1055 
1056