[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBFrame.cpp
blob2300bec4d685d2e099a59141745645f9365a638c
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 //===----------------------------------------------------------------------===//
9 #include <algorithm>
10 #include <set>
11 #include <string>
13 #include "lldb/API/SBFrame.h"
15 #include "lldb/lldb-types.h"
17 #include "Utils.h"
18 #include "lldb/Core/Address.h"
19 #include "lldb/Core/Debugger.h"
20 #include "lldb/Expression/ExpressionVariable.h"
21 #include "lldb/Expression/UserExpression.h"
22 #include "lldb/Host/Host.h"
23 #include "lldb/Symbol/Block.h"
24 #include "lldb/Symbol/Function.h"
25 #include "lldb/Symbol/Symbol.h"
26 #include "lldb/Symbol/SymbolContext.h"
27 #include "lldb/Symbol/Variable.h"
28 #include "lldb/Symbol/VariableList.h"
29 #include "lldb/Target/ExecutionContext.h"
30 #include "lldb/Target/Process.h"
31 #include "lldb/Target/RegisterContext.h"
32 #include "lldb/Target/StackFrame.h"
33 #include "lldb/Target/StackFrameRecognizer.h"
34 #include "lldb/Target/StackID.h"
35 #include "lldb/Target/Target.h"
36 #include "lldb/Target/Thread.h"
37 #include "lldb/Utility/ConstString.h"
38 #include "lldb/Utility/Instrumentation.h"
39 #include "lldb/Utility/LLDBLog.h"
40 #include "lldb/Utility/Stream.h"
41 #include "lldb/ValueObject/ValueObjectConstResult.h"
42 #include "lldb/ValueObject/ValueObjectRegister.h"
43 #include "lldb/ValueObject/ValueObjectVariable.h"
45 #include "lldb/API/SBAddress.h"
46 #include "lldb/API/SBDebugger.h"
47 #include "lldb/API/SBExpressionOptions.h"
48 #include "lldb/API/SBFormat.h"
49 #include "lldb/API/SBStream.h"
50 #include "lldb/API/SBStructuredData.h"
51 #include "lldb/API/SBSymbolContext.h"
52 #include "lldb/API/SBThread.h"
53 #include "lldb/API/SBValue.h"
54 #include "lldb/API/SBVariablesOptions.h"
56 #include "llvm/Support/PrettyStackTrace.h"
58 using namespace lldb;
59 using namespace lldb_private;
61 SBFrame::SBFrame() : m_opaque_sp(new ExecutionContextRef()) {
62 LLDB_INSTRUMENT_VA(this);
65 SBFrame::SBFrame(const StackFrameSP &lldb_object_sp)
66 : m_opaque_sp(new ExecutionContextRef(lldb_object_sp)) {
67 LLDB_INSTRUMENT_VA(this, lldb_object_sp);
70 SBFrame::SBFrame(const SBFrame &rhs) {
71 LLDB_INSTRUMENT_VA(this, rhs);
73 m_opaque_sp = clone(rhs.m_opaque_sp);
76 SBFrame::~SBFrame() = default;
78 const SBFrame &SBFrame::operator=(const SBFrame &rhs) {
79 LLDB_INSTRUMENT_VA(this, rhs);
81 if (this != &rhs)
82 m_opaque_sp = clone(rhs.m_opaque_sp);
83 return *this;
86 StackFrameSP SBFrame::GetFrameSP() const {
87 return (m_opaque_sp ? m_opaque_sp->GetFrameSP() : StackFrameSP());
90 void SBFrame::SetFrameSP(const StackFrameSP &lldb_object_sp) {
91 return m_opaque_sp->SetFrameSP(lldb_object_sp);
94 bool SBFrame::IsValid() const {
95 LLDB_INSTRUMENT_VA(this);
96 return this->operator bool();
98 SBFrame::operator bool() const {
99 LLDB_INSTRUMENT_VA(this);
101 std::unique_lock<std::recursive_mutex> lock;
102 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
104 Target *target = exe_ctx.GetTargetPtr();
105 Process *process = exe_ctx.GetProcessPtr();
106 if (target && process) {
107 Process::StopLocker stop_locker;
108 if (stop_locker.TryLock(&process->GetRunLock()))
109 return GetFrameSP().get() != nullptr;
112 // Without a target & process we can't have a valid stack frame.
113 return false;
116 SBSymbolContext SBFrame::GetSymbolContext(uint32_t resolve_scope) const {
117 LLDB_INSTRUMENT_VA(this, resolve_scope);
119 SBSymbolContext sb_sym_ctx;
120 std::unique_lock<std::recursive_mutex> lock;
121 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
122 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
123 Target *target = exe_ctx.GetTargetPtr();
124 Process *process = exe_ctx.GetProcessPtr();
125 if (target && process) {
126 Process::StopLocker stop_locker;
127 if (stop_locker.TryLock(&process->GetRunLock())) {
128 if (StackFrame *frame = exe_ctx.GetFramePtr())
129 sb_sym_ctx = frame->GetSymbolContext(scope);
133 return sb_sym_ctx;
136 SBModule SBFrame::GetModule() const {
137 LLDB_INSTRUMENT_VA(this);
139 SBModule sb_module;
140 ModuleSP module_sp;
141 std::unique_lock<std::recursive_mutex> lock;
142 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
144 StackFrame *frame = nullptr;
145 Target *target = exe_ctx.GetTargetPtr();
146 Process *process = exe_ctx.GetProcessPtr();
147 if (target && process) {
148 Process::StopLocker stop_locker;
149 if (stop_locker.TryLock(&process->GetRunLock())) {
150 frame = exe_ctx.GetFramePtr();
151 if (frame) {
152 module_sp = frame->GetSymbolContext(eSymbolContextModule).module_sp;
153 sb_module.SetSP(module_sp);
158 return sb_module;
161 SBCompileUnit SBFrame::GetCompileUnit() const {
162 LLDB_INSTRUMENT_VA(this);
164 SBCompileUnit sb_comp_unit;
165 std::unique_lock<std::recursive_mutex> lock;
166 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
168 StackFrame *frame = nullptr;
169 Target *target = exe_ctx.GetTargetPtr();
170 Process *process = exe_ctx.GetProcessPtr();
171 if (target && process) {
172 Process::StopLocker stop_locker;
173 if (stop_locker.TryLock(&process->GetRunLock())) {
174 frame = exe_ctx.GetFramePtr();
175 if (frame) {
176 sb_comp_unit.reset(
177 frame->GetSymbolContext(eSymbolContextCompUnit).comp_unit);
182 return sb_comp_unit;
185 SBFunction SBFrame::GetFunction() const {
186 LLDB_INSTRUMENT_VA(this);
188 SBFunction sb_function;
189 std::unique_lock<std::recursive_mutex> lock;
190 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
192 StackFrame *frame = nullptr;
193 Target *target = exe_ctx.GetTargetPtr();
194 Process *process = exe_ctx.GetProcessPtr();
195 if (target && process) {
196 Process::StopLocker stop_locker;
197 if (stop_locker.TryLock(&process->GetRunLock())) {
198 frame = exe_ctx.GetFramePtr();
199 if (frame) {
200 sb_function.reset(
201 frame->GetSymbolContext(eSymbolContextFunction).function);
206 return sb_function;
209 SBSymbol SBFrame::GetSymbol() const {
210 LLDB_INSTRUMENT_VA(this);
212 SBSymbol sb_symbol;
213 std::unique_lock<std::recursive_mutex> lock;
214 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
216 StackFrame *frame = nullptr;
217 Target *target = exe_ctx.GetTargetPtr();
218 Process *process = exe_ctx.GetProcessPtr();
219 if (target && process) {
220 Process::StopLocker stop_locker;
221 if (stop_locker.TryLock(&process->GetRunLock())) {
222 frame = exe_ctx.GetFramePtr();
223 if (frame) {
224 sb_symbol.reset(frame->GetSymbolContext(eSymbolContextSymbol).symbol);
229 return sb_symbol;
232 SBBlock SBFrame::GetBlock() const {
233 LLDB_INSTRUMENT_VA(this);
235 SBBlock sb_block;
236 std::unique_lock<std::recursive_mutex> lock;
237 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
239 StackFrame *frame = nullptr;
240 Target *target = exe_ctx.GetTargetPtr();
241 Process *process = exe_ctx.GetProcessPtr();
242 if (target && process) {
243 Process::StopLocker stop_locker;
244 if (stop_locker.TryLock(&process->GetRunLock())) {
245 frame = exe_ctx.GetFramePtr();
246 if (frame)
247 sb_block.SetPtr(frame->GetSymbolContext(eSymbolContextBlock).block);
250 return sb_block;
253 SBBlock SBFrame::GetFrameBlock() const {
254 LLDB_INSTRUMENT_VA(this);
256 SBBlock sb_block;
257 std::unique_lock<std::recursive_mutex> lock;
258 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
260 StackFrame *frame = nullptr;
261 Target *target = exe_ctx.GetTargetPtr();
262 Process *process = exe_ctx.GetProcessPtr();
263 if (target && process) {
264 Process::StopLocker stop_locker;
265 if (stop_locker.TryLock(&process->GetRunLock())) {
266 frame = exe_ctx.GetFramePtr();
267 if (frame)
268 sb_block.SetPtr(frame->GetFrameBlock());
271 return sb_block;
274 SBLineEntry SBFrame::GetLineEntry() const {
275 LLDB_INSTRUMENT_VA(this);
277 SBLineEntry sb_line_entry;
278 std::unique_lock<std::recursive_mutex> lock;
279 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
281 StackFrame *frame = nullptr;
282 Target *target = exe_ctx.GetTargetPtr();
283 Process *process = exe_ctx.GetProcessPtr();
284 if (target && process) {
285 Process::StopLocker stop_locker;
286 if (stop_locker.TryLock(&process->GetRunLock())) {
287 frame = exe_ctx.GetFramePtr();
288 if (frame) {
289 sb_line_entry.SetLineEntry(
290 frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
294 return sb_line_entry;
297 uint32_t SBFrame::GetFrameID() const {
298 LLDB_INSTRUMENT_VA(this);
300 uint32_t frame_idx = UINT32_MAX;
302 std::unique_lock<std::recursive_mutex> lock;
303 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
305 StackFrame *frame = exe_ctx.GetFramePtr();
306 if (frame)
307 frame_idx = frame->GetFrameIndex();
309 return frame_idx;
312 lldb::addr_t SBFrame::GetCFA() const {
313 LLDB_INSTRUMENT_VA(this);
315 std::unique_lock<std::recursive_mutex> lock;
316 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
318 StackFrame *frame = exe_ctx.GetFramePtr();
319 if (frame)
320 return frame->GetStackID().GetCallFrameAddress();
321 return LLDB_INVALID_ADDRESS;
324 addr_t SBFrame::GetPC() const {
325 LLDB_INSTRUMENT_VA(this);
327 addr_t addr = LLDB_INVALID_ADDRESS;
328 std::unique_lock<std::recursive_mutex> lock;
329 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
331 StackFrame *frame = nullptr;
332 Target *target = exe_ctx.GetTargetPtr();
333 Process *process = exe_ctx.GetProcessPtr();
334 if (target && process) {
335 Process::StopLocker stop_locker;
336 if (stop_locker.TryLock(&process->GetRunLock())) {
337 frame = exe_ctx.GetFramePtr();
338 if (frame) {
339 addr = frame->GetFrameCodeAddress().GetOpcodeLoadAddress(
340 target, AddressClass::eCode);
345 return addr;
348 bool SBFrame::SetPC(addr_t new_pc) {
349 LLDB_INSTRUMENT_VA(this, new_pc);
351 bool ret_val = false;
352 std::unique_lock<std::recursive_mutex> lock;
353 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
355 Target *target = exe_ctx.GetTargetPtr();
356 Process *process = exe_ctx.GetProcessPtr();
357 if (target && process) {
358 Process::StopLocker stop_locker;
359 if (stop_locker.TryLock(&process->GetRunLock())) {
360 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
361 if (RegisterContextSP reg_ctx_sp = frame->GetRegisterContext()) {
362 ret_val = reg_ctx_sp->SetPC(new_pc);
368 return ret_val;
371 addr_t SBFrame::GetSP() const {
372 LLDB_INSTRUMENT_VA(this);
374 addr_t addr = LLDB_INVALID_ADDRESS;
375 std::unique_lock<std::recursive_mutex> lock;
376 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
378 Target *target = exe_ctx.GetTargetPtr();
379 Process *process = exe_ctx.GetProcessPtr();
380 if (target && process) {
381 Process::StopLocker stop_locker;
382 if (stop_locker.TryLock(&process->GetRunLock())) {
383 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
384 if (RegisterContextSP reg_ctx_sp = frame->GetRegisterContext()) {
385 addr = reg_ctx_sp->GetSP();
391 return addr;
394 addr_t SBFrame::GetFP() const {
395 LLDB_INSTRUMENT_VA(this);
397 addr_t addr = LLDB_INVALID_ADDRESS;
398 std::unique_lock<std::recursive_mutex> lock;
399 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
401 Target *target = exe_ctx.GetTargetPtr();
402 Process *process = exe_ctx.GetProcessPtr();
403 if (target && process) {
404 Process::StopLocker stop_locker;
405 if (stop_locker.TryLock(&process->GetRunLock())) {
406 if (StackFrame *frame = exe_ctx.GetFramePtr()) {
407 if (RegisterContextSP reg_ctx_sp = frame->GetRegisterContext()) {
408 addr = reg_ctx_sp->GetFP();
414 return addr;
417 SBAddress SBFrame::GetPCAddress() const {
418 LLDB_INSTRUMENT_VA(this);
420 SBAddress sb_addr;
421 std::unique_lock<std::recursive_mutex> lock;
422 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
424 StackFrame *frame = exe_ctx.GetFramePtr();
425 Target *target = exe_ctx.GetTargetPtr();
426 Process *process = exe_ctx.GetProcessPtr();
427 if (target && process) {
428 Process::StopLocker stop_locker;
429 if (stop_locker.TryLock(&process->GetRunLock())) {
430 frame = exe_ctx.GetFramePtr();
431 if (frame)
432 sb_addr.SetAddress(frame->GetFrameCodeAddress());
435 return sb_addr;
438 void SBFrame::Clear() {
439 LLDB_INSTRUMENT_VA(this);
441 m_opaque_sp->Clear();
444 lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path) {
445 LLDB_INSTRUMENT_VA(this, var_path);
447 SBValue sb_value;
448 std::unique_lock<std::recursive_mutex> lock;
449 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
451 StackFrame *frame = exe_ctx.GetFramePtr();
452 Target *target = exe_ctx.GetTargetPtr();
453 if (frame && target) {
454 lldb::DynamicValueType use_dynamic =
455 frame->CalculateTarget()->GetPreferDynamicValue();
456 sb_value = GetValueForVariablePath(var_path, use_dynamic);
458 return sb_value;
461 lldb::SBValue SBFrame::GetValueForVariablePath(const char *var_path,
462 DynamicValueType use_dynamic) {
463 LLDB_INSTRUMENT_VA(this, var_path, use_dynamic);
465 SBValue sb_value;
466 if (var_path == nullptr || var_path[0] == '\0') {
467 return sb_value;
470 std::unique_lock<std::recursive_mutex> lock;
471 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
473 StackFrame *frame = nullptr;
474 Target *target = exe_ctx.GetTargetPtr();
475 Process *process = exe_ctx.GetProcessPtr();
476 if (target && process) {
477 Process::StopLocker stop_locker;
478 if (stop_locker.TryLock(&process->GetRunLock())) {
479 frame = exe_ctx.GetFramePtr();
480 if (frame) {
481 VariableSP var_sp;
482 Status error;
483 ValueObjectSP value_sp(frame->GetValueForVariableExpressionPath(
484 var_path, eNoDynamicValues,
485 StackFrame::eExpressionPathOptionCheckPtrVsMember |
486 StackFrame::eExpressionPathOptionsAllowDirectIVarAccess,
487 var_sp, error));
488 sb_value.SetSP(value_sp, use_dynamic);
492 return sb_value;
495 SBValue SBFrame::FindVariable(const char *name) {
496 LLDB_INSTRUMENT_VA(this, name);
498 SBValue value;
499 std::unique_lock<std::recursive_mutex> lock;
500 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
502 StackFrame *frame = exe_ctx.GetFramePtr();
503 Target *target = exe_ctx.GetTargetPtr();
504 if (frame && target) {
505 lldb::DynamicValueType use_dynamic =
506 frame->CalculateTarget()->GetPreferDynamicValue();
507 value = FindVariable(name, use_dynamic);
509 return value;
512 SBValue SBFrame::FindVariable(const char *name,
513 lldb::DynamicValueType use_dynamic) {
514 LLDB_INSTRUMENT_VA(this, name, use_dynamic);
516 VariableSP var_sp;
517 SBValue sb_value;
519 if (name == nullptr || name[0] == '\0') {
520 return sb_value;
523 ValueObjectSP value_sp;
524 std::unique_lock<std::recursive_mutex> lock;
525 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
527 StackFrame *frame = nullptr;
528 Target *target = exe_ctx.GetTargetPtr();
529 Process *process = exe_ctx.GetProcessPtr();
530 if (target && process) {
531 Process::StopLocker stop_locker;
532 if (stop_locker.TryLock(&process->GetRunLock())) {
533 frame = exe_ctx.GetFramePtr();
534 if (frame) {
535 value_sp = frame->FindVariable(ConstString(name));
537 if (value_sp)
538 sb_value.SetSP(value_sp, use_dynamic);
543 return sb_value;
546 SBValue SBFrame::FindValue(const char *name, ValueType value_type) {
547 LLDB_INSTRUMENT_VA(this, name, value_type);
549 SBValue value;
550 std::unique_lock<std::recursive_mutex> lock;
551 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
553 StackFrame *frame = exe_ctx.GetFramePtr();
554 Target *target = exe_ctx.GetTargetPtr();
555 if (frame && target) {
556 lldb::DynamicValueType use_dynamic =
557 frame->CalculateTarget()->GetPreferDynamicValue();
558 value = FindValue(name, value_type, use_dynamic);
560 return value;
563 SBValue SBFrame::FindValue(const char *name, ValueType value_type,
564 lldb::DynamicValueType use_dynamic) {
565 LLDB_INSTRUMENT_VA(this, name, value_type, use_dynamic);
567 SBValue sb_value;
569 if (name == nullptr || name[0] == '\0') {
570 return sb_value;
573 ValueObjectSP value_sp;
574 std::unique_lock<std::recursive_mutex> lock;
575 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
577 StackFrame *frame = nullptr;
578 Target *target = exe_ctx.GetTargetPtr();
579 Process *process = exe_ctx.GetProcessPtr();
580 if (target && process) {
581 Process::StopLocker stop_locker;
582 if (stop_locker.TryLock(&process->GetRunLock())) {
583 frame = exe_ctx.GetFramePtr();
584 if (frame) {
585 VariableList variable_list;
587 switch (value_type) {
588 case eValueTypeVariableGlobal: // global variable
589 case eValueTypeVariableStatic: // static variable
590 case eValueTypeVariableArgument: // function argument variables
591 case eValueTypeVariableLocal: // function local variables
592 case eValueTypeVariableThreadLocal: // thread local variables
594 SymbolContext sc(frame->GetSymbolContext(eSymbolContextBlock));
596 const bool can_create = true;
597 const bool get_parent_variables = true;
598 const bool stop_if_block_is_inlined_function = true;
600 if (sc.block)
601 sc.block->AppendVariables(
602 can_create, get_parent_variables,
603 stop_if_block_is_inlined_function,
604 [frame](Variable *v) { return v->IsInScope(frame); },
605 &variable_list);
606 if (value_type == eValueTypeVariableGlobal
607 || value_type == eValueTypeVariableStatic) {
608 const bool get_file_globals = true;
609 VariableList *frame_vars = frame->GetVariableList(get_file_globals,
610 nullptr);
611 if (frame_vars)
612 frame_vars->AppendVariablesIfUnique(variable_list);
614 ConstString const_name(name);
615 VariableSP variable_sp(
616 variable_list.FindVariable(const_name, value_type));
617 if (variable_sp) {
618 value_sp = frame->GetValueObjectForFrameVariable(variable_sp,
619 eNoDynamicValues);
620 sb_value.SetSP(value_sp, use_dynamic);
622 } break;
624 case eValueTypeRegister: // stack frame register value
626 RegisterContextSP reg_ctx(frame->GetRegisterContext());
627 if (reg_ctx) {
628 if (const RegisterInfo *reg_info =
629 reg_ctx->GetRegisterInfoByName(name)) {
630 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_info);
631 sb_value.SetSP(value_sp);
634 } break;
636 case eValueTypeRegisterSet: // A collection of stack frame register
637 // values
639 RegisterContextSP reg_ctx(frame->GetRegisterContext());
640 if (reg_ctx) {
641 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
642 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
643 const RegisterSet *reg_set = reg_ctx->GetRegisterSet(set_idx);
644 if (reg_set &&
645 (llvm::StringRef(reg_set->name).equals_insensitive(name) ||
646 llvm::StringRef(reg_set->short_name)
647 .equals_insensitive(name))) {
648 value_sp =
649 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx);
650 sb_value.SetSP(value_sp);
651 break;
655 } break;
657 case eValueTypeConstResult: // constant result variables
659 ConstString const_name(name);
660 ExpressionVariableSP expr_var_sp(
661 target->GetPersistentVariable(const_name));
662 if (expr_var_sp) {
663 value_sp = expr_var_sp->GetValueObject();
664 sb_value.SetSP(value_sp, use_dynamic);
666 } break;
668 default:
669 break;
675 return sb_value;
678 bool SBFrame::IsEqual(const SBFrame &that) const {
679 LLDB_INSTRUMENT_VA(this, that);
681 lldb::StackFrameSP this_sp = GetFrameSP();
682 lldb::StackFrameSP that_sp = that.GetFrameSP();
683 return (this_sp && that_sp && this_sp->GetStackID() == that_sp->GetStackID());
686 bool SBFrame::operator==(const SBFrame &rhs) const {
687 LLDB_INSTRUMENT_VA(this, rhs);
689 return IsEqual(rhs);
692 bool SBFrame::operator!=(const SBFrame &rhs) const {
693 LLDB_INSTRUMENT_VA(this, rhs);
695 return !IsEqual(rhs);
698 SBThread SBFrame::GetThread() const {
699 LLDB_INSTRUMENT_VA(this);
701 std::unique_lock<std::recursive_mutex> lock;
702 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
704 ThreadSP thread_sp(exe_ctx.GetThreadSP());
705 SBThread sb_thread(thread_sp);
707 return sb_thread;
710 const char *SBFrame::Disassemble() const {
711 LLDB_INSTRUMENT_VA(this);
713 std::unique_lock<std::recursive_mutex> lock;
714 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
715 Target *target = exe_ctx.GetTargetPtr();
716 Process *process = exe_ctx.GetProcessPtr();
717 if (!target || !process)
718 return nullptr;
720 Process::StopLocker stop_locker;
721 if (stop_locker.TryLock(&process->GetRunLock())) {
722 if (auto *frame = exe_ctx.GetFramePtr())
723 return ConstString(frame->Disassemble()).GetCString();
726 return nullptr;
729 SBValueList SBFrame::GetVariables(bool arguments, bool locals, bool statics,
730 bool in_scope_only) {
731 LLDB_INSTRUMENT_VA(this, arguments, locals, statics, in_scope_only);
733 SBValueList value_list;
734 std::unique_lock<std::recursive_mutex> lock;
735 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
737 StackFrame *frame = exe_ctx.GetFramePtr();
738 Target *target = exe_ctx.GetTargetPtr();
739 if (frame && target) {
740 lldb::DynamicValueType use_dynamic =
741 frame->CalculateTarget()->GetPreferDynamicValue();
742 const bool include_runtime_support_values =
743 target->GetDisplayRuntimeSupportValues();
745 SBVariablesOptions options;
746 options.SetIncludeArguments(arguments);
747 options.SetIncludeLocals(locals);
748 options.SetIncludeStatics(statics);
749 options.SetInScopeOnly(in_scope_only);
750 options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
751 options.SetUseDynamic(use_dynamic);
753 value_list = GetVariables(options);
755 return value_list;
758 lldb::SBValueList SBFrame::GetVariables(bool arguments, bool locals,
759 bool statics, bool in_scope_only,
760 lldb::DynamicValueType use_dynamic) {
761 LLDB_INSTRUMENT_VA(this, arguments, locals, statics, in_scope_only,
762 use_dynamic);
764 std::unique_lock<std::recursive_mutex> lock;
765 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
767 Target *target = exe_ctx.GetTargetPtr();
768 const bool include_runtime_support_values =
769 target ? target->GetDisplayRuntimeSupportValues() : false;
770 SBVariablesOptions options;
771 options.SetIncludeArguments(arguments);
772 options.SetIncludeLocals(locals);
773 options.SetIncludeStatics(statics);
774 options.SetInScopeOnly(in_scope_only);
775 options.SetIncludeRuntimeSupportValues(include_runtime_support_values);
776 options.SetUseDynamic(use_dynamic);
777 return GetVariables(options);
780 SBValueList SBFrame::GetVariables(const lldb::SBVariablesOptions &options) {
781 LLDB_INSTRUMENT_VA(this, options);
783 SBValueList value_list;
784 std::unique_lock<std::recursive_mutex> lock;
785 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
787 StackFrame *frame = nullptr;
788 Target *target = exe_ctx.GetTargetPtr();
790 const bool statics = options.GetIncludeStatics();
791 const bool arguments = options.GetIncludeArguments();
792 const bool recognized_arguments =
793 options.GetIncludeRecognizedArguments(SBTarget(exe_ctx.GetTargetSP()));
794 const bool locals = options.GetIncludeLocals();
795 const bool in_scope_only = options.GetInScopeOnly();
796 const bool include_runtime_support_values =
797 options.GetIncludeRuntimeSupportValues();
798 const lldb::DynamicValueType use_dynamic = options.GetUseDynamic();
801 std::set<VariableSP> variable_set;
802 Process *process = exe_ctx.GetProcessPtr();
803 if (target && process) {
804 Process::StopLocker stop_locker;
805 if (stop_locker.TryLock(&process->GetRunLock())) {
806 frame = exe_ctx.GetFramePtr();
807 if (frame) {
808 Debugger &dbg = process->GetTarget().GetDebugger();
809 VariableList *variable_list = nullptr;
810 Status var_error;
811 variable_list = frame->GetVariableList(true, &var_error);
812 if (var_error.Fail())
813 value_list.SetError(std::move(var_error));
814 if (variable_list) {
815 const size_t num_variables = variable_list->GetSize();
816 if (num_variables) {
817 size_t num_produced = 0;
818 for (const VariableSP &variable_sp : *variable_list) {
819 if (INTERRUPT_REQUESTED(dbg,
820 "Interrupted getting frame variables with {0} of {1} "
821 "produced.", num_produced, num_variables))
822 return {};
824 if (variable_sp) {
825 bool add_variable = false;
826 switch (variable_sp->GetScope()) {
827 case eValueTypeVariableGlobal:
828 case eValueTypeVariableStatic:
829 case eValueTypeVariableThreadLocal:
830 add_variable = statics;
831 break;
833 case eValueTypeVariableArgument:
834 add_variable = arguments;
835 break;
837 case eValueTypeVariableLocal:
838 add_variable = locals;
839 break;
841 default:
842 break;
844 if (add_variable) {
845 // Only add variables once so we don't end up with duplicates
846 if (variable_set.find(variable_sp) == variable_set.end())
847 variable_set.insert(variable_sp);
848 else
849 continue;
851 if (in_scope_only && !variable_sp->IsInScope(frame))
852 continue;
854 ValueObjectSP valobj_sp(frame->GetValueObjectForFrameVariable(
855 variable_sp, eNoDynamicValues));
857 if (!include_runtime_support_values && valobj_sp != nullptr &&
858 valobj_sp->IsRuntimeSupportValue())
859 continue;
861 SBValue value_sb;
862 value_sb.SetSP(valobj_sp, use_dynamic);
863 value_list.Append(value_sb);
867 num_produced++;
870 if (recognized_arguments) {
871 auto recognized_frame = frame->GetRecognizedFrame();
872 if (recognized_frame) {
873 ValueObjectListSP recognized_arg_list =
874 recognized_frame->GetRecognizedArguments();
875 if (recognized_arg_list) {
876 for (auto &rec_value_sp : recognized_arg_list->GetObjects()) {
877 SBValue value_sb;
878 value_sb.SetSP(rec_value_sp, use_dynamic);
879 value_list.Append(value_sb);
888 return value_list;
891 SBValueList SBFrame::GetRegisters() {
892 LLDB_INSTRUMENT_VA(this);
894 SBValueList value_list;
895 std::unique_lock<std::recursive_mutex> lock;
896 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
898 StackFrame *frame = nullptr;
899 Target *target = exe_ctx.GetTargetPtr();
900 Process *process = exe_ctx.GetProcessPtr();
901 if (target && process) {
902 Process::StopLocker stop_locker;
903 if (stop_locker.TryLock(&process->GetRunLock())) {
904 frame = exe_ctx.GetFramePtr();
905 if (frame) {
906 RegisterContextSP reg_ctx(frame->GetRegisterContext());
907 if (reg_ctx) {
908 const uint32_t num_sets = reg_ctx->GetRegisterSetCount();
909 for (uint32_t set_idx = 0; set_idx < num_sets; ++set_idx) {
910 value_list.Append(
911 ValueObjectRegisterSet::Create(frame, reg_ctx, set_idx));
918 return value_list;
921 SBValue SBFrame::FindRegister(const char *name) {
922 LLDB_INSTRUMENT_VA(this, name);
924 SBValue result;
925 ValueObjectSP value_sp;
926 std::unique_lock<std::recursive_mutex> lock;
927 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
929 StackFrame *frame = nullptr;
930 Target *target = exe_ctx.GetTargetPtr();
931 Process *process = exe_ctx.GetProcessPtr();
932 if (target && process) {
933 Process::StopLocker stop_locker;
934 if (stop_locker.TryLock(&process->GetRunLock())) {
935 frame = exe_ctx.GetFramePtr();
936 if (frame) {
937 RegisterContextSP reg_ctx(frame->GetRegisterContext());
938 if (reg_ctx) {
939 if (const RegisterInfo *reg_info =
940 reg_ctx->GetRegisterInfoByName(name)) {
941 value_sp = ValueObjectRegister::Create(frame, reg_ctx, reg_info);
942 result.SetSP(value_sp);
949 return result;
952 SBError SBFrame::GetDescriptionWithFormat(const SBFormat &format,
953 SBStream &output) {
954 Stream &strm = output.ref();
956 std::unique_lock<std::recursive_mutex> lock;
957 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
959 StackFrame *frame = nullptr;
960 Target *target = exe_ctx.GetTargetPtr();
961 Process *process = exe_ctx.GetProcessPtr();
962 SBError error;
964 if (!format) {
965 error.SetErrorString("The provided SBFormat object is invalid");
966 return error;
969 if (target && process) {
970 Process::StopLocker stop_locker;
971 if (stop_locker.TryLock(&process->GetRunLock())) {
972 frame = exe_ctx.GetFramePtr();
973 if (frame &&
974 frame->DumpUsingFormat(strm, format.GetFormatEntrySP().get())) {
975 return error;
979 error.SetErrorStringWithFormat(
980 "It was not possible to generate a frame "
981 "description with the given format string '%s'",
982 format.GetFormatEntrySP()->string.c_str());
983 return error;
986 bool SBFrame::GetDescription(SBStream &description) {
987 LLDB_INSTRUMENT_VA(this, description);
989 Stream &strm = description.ref();
991 std::unique_lock<std::recursive_mutex> lock;
992 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
994 StackFrame *frame;
995 Target *target = exe_ctx.GetTargetPtr();
996 Process *process = exe_ctx.GetProcessPtr();
997 if (target && process) {
998 Process::StopLocker stop_locker;
999 if (stop_locker.TryLock(&process->GetRunLock())) {
1000 frame = exe_ctx.GetFramePtr();
1001 if (frame) {
1002 frame->DumpUsingSettingsFormat(&strm);
1006 } else
1007 strm.PutCString("No value");
1009 return true;
1012 SBValue SBFrame::EvaluateExpression(const char *expr) {
1013 LLDB_INSTRUMENT_VA(this, expr);
1015 SBValue result;
1016 std::unique_lock<std::recursive_mutex> lock;
1017 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1019 StackFrame *frame = exe_ctx.GetFramePtr();
1020 Target *target = exe_ctx.GetTargetPtr();
1021 if (frame && target) {
1022 SBExpressionOptions options;
1023 lldb::DynamicValueType fetch_dynamic_value =
1024 frame->CalculateTarget()->GetPreferDynamicValue();
1025 options.SetFetchDynamicValue(fetch_dynamic_value);
1026 options.SetUnwindOnError(true);
1027 options.SetIgnoreBreakpoints(true);
1028 SourceLanguage language = target->GetLanguage();
1029 if (!language)
1030 language = frame->GetLanguage();
1031 options.SetLanguage((SBSourceLanguageName)language.name, language.version);
1032 return EvaluateExpression(expr, options);
1033 } else {
1034 Status error;
1035 error = Status::FromErrorString("can't evaluate expressions when the "
1036 "process is running.");
1037 ValueObjectSP error_val_sp =
1038 ValueObjectConstResult::Create(nullptr, std::move(error));
1039 result.SetSP(error_val_sp, false);
1041 return result;
1044 SBValue
1045 SBFrame::EvaluateExpression(const char *expr,
1046 lldb::DynamicValueType fetch_dynamic_value) {
1047 LLDB_INSTRUMENT_VA(this, expr, fetch_dynamic_value);
1049 SBExpressionOptions options;
1050 options.SetFetchDynamicValue(fetch_dynamic_value);
1051 options.SetUnwindOnError(true);
1052 options.SetIgnoreBreakpoints(true);
1053 std::unique_lock<std::recursive_mutex> lock;
1054 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1056 StackFrame *frame = exe_ctx.GetFramePtr();
1057 Target *target = exe_ctx.GetTargetPtr();
1058 SourceLanguage language;
1059 if (target)
1060 language = target->GetLanguage();
1061 if (!language && frame)
1062 language = frame->GetLanguage();
1063 options.SetLanguage((SBSourceLanguageName)language.name, language.version);
1064 return EvaluateExpression(expr, options);
1067 SBValue SBFrame::EvaluateExpression(const char *expr,
1068 lldb::DynamicValueType fetch_dynamic_value,
1069 bool unwind_on_error) {
1070 LLDB_INSTRUMENT_VA(this, expr, fetch_dynamic_value, unwind_on_error);
1072 SBExpressionOptions options;
1073 std::unique_lock<std::recursive_mutex> lock;
1074 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1076 options.SetFetchDynamicValue(fetch_dynamic_value);
1077 options.SetUnwindOnError(unwind_on_error);
1078 options.SetIgnoreBreakpoints(true);
1079 StackFrame *frame = exe_ctx.GetFramePtr();
1080 Target *target = exe_ctx.GetTargetPtr();
1081 SourceLanguage language;
1082 if (target)
1083 language = target->GetLanguage();
1084 if (!language && frame)
1085 language = frame->GetLanguage();
1086 options.SetLanguage((SBSourceLanguageName)language.name, language.version);
1087 return EvaluateExpression(expr, options);
1090 lldb::SBValue SBFrame::EvaluateExpression(const char *expr,
1091 const SBExpressionOptions &options) {
1092 LLDB_INSTRUMENT_VA(this, expr, options);
1094 Log *expr_log = GetLog(LLDBLog::Expressions);
1096 SBValue expr_result;
1098 if (expr == nullptr || expr[0] == '\0') {
1099 return expr_result;
1102 ValueObjectSP expr_value_sp;
1104 std::unique_lock<std::recursive_mutex> lock;
1105 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1107 StackFrame *frame = nullptr;
1108 Target *target = exe_ctx.GetTargetPtr();
1109 Process *process = exe_ctx.GetProcessPtr();
1111 if (target && process) {
1112 Process::StopLocker stop_locker;
1113 if (stop_locker.TryLock(&process->GetRunLock())) {
1114 frame = exe_ctx.GetFramePtr();
1115 if (frame) {
1116 std::unique_ptr<llvm::PrettyStackTraceFormat> stack_trace;
1117 if (target->GetDisplayExpressionsInCrashlogs()) {
1118 StreamString frame_description;
1119 frame->DumpUsingSettingsFormat(&frame_description);
1120 stack_trace = std::make_unique<llvm::PrettyStackTraceFormat>(
1121 "SBFrame::EvaluateExpression (expr = \"%s\", fetch_dynamic_value "
1122 "= %u) %s",
1123 expr, options.GetFetchDynamicValue(),
1124 frame_description.GetData());
1127 target->EvaluateExpression(expr, frame, expr_value_sp, options.ref());
1128 expr_result.SetSP(expr_value_sp, options.GetFetchDynamicValue());
1130 } else {
1131 Status error;
1132 error = Status::FromErrorString("can't evaluate expressions when the "
1133 "process is running.");
1134 expr_value_sp = ValueObjectConstResult::Create(nullptr, std::move(error));
1135 expr_result.SetSP(expr_value_sp, false);
1137 } else {
1138 Status error;
1139 error = Status::FromErrorString("sbframe object is not valid.");
1140 expr_value_sp = ValueObjectConstResult::Create(nullptr, std::move(error));
1141 expr_result.SetSP(expr_value_sp, false);
1144 if (expr_result.GetError().Success())
1145 LLDB_LOGF(expr_log,
1146 "** [SBFrame::EvaluateExpression] Expression result is "
1147 "%s, summary %s **",
1148 expr_result.GetValue(), expr_result.GetSummary());
1149 else
1150 LLDB_LOGF(expr_log,
1151 "** [SBFrame::EvaluateExpression] Expression evaluation failed: "
1152 "%s **",
1153 expr_result.GetError().GetCString());
1155 return expr_result;
1158 SBStructuredData SBFrame::GetLanguageSpecificData() const {
1159 LLDB_INSTRUMENT_VA(this);
1161 SBStructuredData sb_data;
1162 std::unique_lock<std::recursive_mutex> lock;
1163 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1164 StackFrame *frame = exe_ctx.GetFramePtr();
1165 if (!frame)
1166 return sb_data;
1168 StructuredData::ObjectSP data(frame->GetLanguageSpecificData());
1169 sb_data.m_impl_up->SetObjectSP(data);
1170 return sb_data;
1173 bool SBFrame::IsInlined() {
1174 LLDB_INSTRUMENT_VA(this);
1176 return static_cast<const SBFrame *>(this)->IsInlined();
1179 bool SBFrame::IsInlined() const {
1180 LLDB_INSTRUMENT_VA(this);
1182 std::unique_lock<std::recursive_mutex> lock;
1183 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1185 StackFrame *frame = nullptr;
1186 Target *target = exe_ctx.GetTargetPtr();
1187 Process *process = exe_ctx.GetProcessPtr();
1188 if (target && process) {
1189 Process::StopLocker stop_locker;
1190 if (stop_locker.TryLock(&process->GetRunLock())) {
1191 frame = exe_ctx.GetFramePtr();
1192 if (frame)
1193 return frame->IsInlined();
1196 return false;
1199 bool SBFrame::IsArtificial() {
1200 LLDB_INSTRUMENT_VA(this);
1202 return static_cast<const SBFrame *>(this)->IsArtificial();
1205 bool SBFrame::IsArtificial() const {
1206 LLDB_INSTRUMENT_VA(this);
1208 std::unique_lock<std::recursive_mutex> lock;
1209 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1211 if (StackFrame *frame = exe_ctx.GetFramePtr())
1212 return frame->IsArtificial();
1214 return false;
1217 bool SBFrame::IsHidden() const {
1218 LLDB_INSTRUMENT_VA(this);
1220 std::unique_lock<std::recursive_mutex> lock;
1221 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1223 if (StackFrame *frame = exe_ctx.GetFramePtr())
1224 return frame->IsHidden();
1226 return false;
1229 const char *SBFrame::GetFunctionName() {
1230 LLDB_INSTRUMENT_VA(this);
1232 return static_cast<const SBFrame *>(this)->GetFunctionName();
1235 lldb::LanguageType SBFrame::GuessLanguage() const {
1236 LLDB_INSTRUMENT_VA(this);
1238 std::unique_lock<std::recursive_mutex> lock;
1239 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1241 StackFrame *frame = nullptr;
1242 Target *target = exe_ctx.GetTargetPtr();
1243 Process *process = exe_ctx.GetProcessPtr();
1244 if (target && process) {
1245 Process::StopLocker stop_locker;
1246 if (stop_locker.TryLock(&process->GetRunLock())) {
1247 frame = exe_ctx.GetFramePtr();
1248 if (frame) {
1249 return frame->GuessLanguage().AsLanguageType();
1253 return eLanguageTypeUnknown;
1256 const char *SBFrame::GetFunctionName() const {
1257 LLDB_INSTRUMENT_VA(this);
1259 const char *name = nullptr;
1260 std::unique_lock<std::recursive_mutex> lock;
1261 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1263 StackFrame *frame = nullptr;
1264 Target *target = exe_ctx.GetTargetPtr();
1265 Process *process = exe_ctx.GetProcessPtr();
1266 if (target && process) {
1267 Process::StopLocker stop_locker;
1268 if (stop_locker.TryLock(&process->GetRunLock())) {
1269 frame = exe_ctx.GetFramePtr();
1270 if (frame)
1271 return frame->GetFunctionName();
1274 return name;
1277 const char *SBFrame::GetDisplayFunctionName() {
1278 LLDB_INSTRUMENT_VA(this);
1280 const char *name = nullptr;
1282 std::unique_lock<std::recursive_mutex> lock;
1283 ExecutionContext exe_ctx(m_opaque_sp.get(), lock);
1285 StackFrame *frame = nullptr;
1286 Target *target = exe_ctx.GetTargetPtr();
1287 Process *process = exe_ctx.GetProcessPtr();
1288 if (target && process) {
1289 Process::StopLocker stop_locker;
1290 if (stop_locker.TryLock(&process->GetRunLock())) {
1291 frame = exe_ctx.GetFramePtr();
1292 if (frame)
1293 return frame->GetDisplayFunctionName();
1296 return name;