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