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