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