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