Path: blob/main/contrib/llvm-project/lldb/source/API/SBBlock.cpp
39587 views
//===-- SBBlock.cpp -------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBBlock.h"9#include "lldb/API/SBAddress.h"10#include "lldb/API/SBFileSpec.h"11#include "lldb/API/SBFrame.h"12#include "lldb/API/SBStream.h"13#include "lldb/API/SBValue.h"14#include "lldb/Core/AddressRange.h"15#include "lldb/Core/AddressRangeListImpl.h"16#include "lldb/Core/ValueObjectVariable.h"17#include "lldb/Symbol/Block.h"18#include "lldb/Symbol/Function.h"19#include "lldb/Symbol/SymbolContext.h"20#include "lldb/Symbol/VariableList.h"21#include "lldb/Target/StackFrame.h"22#include "lldb/Target/Target.h"23#include "lldb/Utility/Instrumentation.h"2425using namespace lldb;26using namespace lldb_private;2728SBBlock::SBBlock() { LLDB_INSTRUMENT_VA(this); }2930SBBlock::SBBlock(lldb_private::Block *lldb_object_ptr)31: m_opaque_ptr(lldb_object_ptr) {}3233SBBlock::SBBlock(const SBBlock &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {34LLDB_INSTRUMENT_VA(this, rhs);35}3637const SBBlock &SBBlock::operator=(const SBBlock &rhs) {38LLDB_INSTRUMENT_VA(this, rhs);3940m_opaque_ptr = rhs.m_opaque_ptr;41return *this;42}4344SBBlock::~SBBlock() { m_opaque_ptr = nullptr; }4546bool SBBlock::IsValid() const {47LLDB_INSTRUMENT_VA(this);48return this->operator bool();49}50SBBlock::operator bool() const {51LLDB_INSTRUMENT_VA(this);5253return m_opaque_ptr != nullptr;54}5556bool SBBlock::IsInlined() const {57LLDB_INSTRUMENT_VA(this);5859if (m_opaque_ptr)60return m_opaque_ptr->GetInlinedFunctionInfo() != nullptr;61return false;62}6364const char *SBBlock::GetInlinedName() const {65LLDB_INSTRUMENT_VA(this);6667if (m_opaque_ptr) {68const InlineFunctionInfo *inlined_info =69m_opaque_ptr->GetInlinedFunctionInfo();70if (inlined_info) {71return inlined_info->GetName().AsCString(nullptr);72}73}74return nullptr;75}7677SBFileSpec SBBlock::GetInlinedCallSiteFile() const {78LLDB_INSTRUMENT_VA(this);7980SBFileSpec sb_file;81if (m_opaque_ptr) {82const InlineFunctionInfo *inlined_info =83m_opaque_ptr->GetInlinedFunctionInfo();84if (inlined_info)85sb_file.SetFileSpec(inlined_info->GetCallSite().GetFile());86}87return sb_file;88}8990uint32_t SBBlock::GetInlinedCallSiteLine() const {91LLDB_INSTRUMENT_VA(this);9293if (m_opaque_ptr) {94const InlineFunctionInfo *inlined_info =95m_opaque_ptr->GetInlinedFunctionInfo();96if (inlined_info)97return inlined_info->GetCallSite().GetLine();98}99return 0;100}101102uint32_t SBBlock::GetInlinedCallSiteColumn() const {103LLDB_INSTRUMENT_VA(this);104105if (m_opaque_ptr) {106const InlineFunctionInfo *inlined_info =107m_opaque_ptr->GetInlinedFunctionInfo();108if (inlined_info)109return inlined_info->GetCallSite().GetColumn();110}111return 0;112}113114void SBBlock::AppendVariables(bool can_create, bool get_parent_variables,115lldb_private::VariableList *var_list) {116if (IsValid()) {117bool show_inline = true;118m_opaque_ptr->AppendVariables(can_create, get_parent_variables, show_inline,119[](Variable *) { return true; }, var_list);120}121}122123SBBlock SBBlock::GetParent() {124LLDB_INSTRUMENT_VA(this);125126SBBlock sb_block;127if (m_opaque_ptr)128sb_block.m_opaque_ptr = m_opaque_ptr->GetParent();129return sb_block;130}131132lldb::SBBlock SBBlock::GetContainingInlinedBlock() {133LLDB_INSTRUMENT_VA(this);134135SBBlock sb_block;136if (m_opaque_ptr)137sb_block.m_opaque_ptr = m_opaque_ptr->GetContainingInlinedBlock();138return sb_block;139}140141SBBlock SBBlock::GetSibling() {142LLDB_INSTRUMENT_VA(this);143144SBBlock sb_block;145if (m_opaque_ptr)146sb_block.m_opaque_ptr = m_opaque_ptr->GetSibling();147return sb_block;148}149150SBBlock SBBlock::GetFirstChild() {151LLDB_INSTRUMENT_VA(this);152153SBBlock sb_block;154if (m_opaque_ptr)155sb_block.m_opaque_ptr = m_opaque_ptr->GetFirstChild();156return sb_block;157}158159lldb_private::Block *SBBlock::GetPtr() { return m_opaque_ptr; }160161void SBBlock::SetPtr(lldb_private::Block *block) { m_opaque_ptr = block; }162163bool SBBlock::GetDescription(SBStream &description) {164LLDB_INSTRUMENT_VA(this, description);165166Stream &strm = description.ref();167168if (m_opaque_ptr) {169lldb::user_id_t id = m_opaque_ptr->GetID();170strm.Printf("Block: {id: %" PRIu64 "} ", id);171if (IsInlined()) {172strm.Printf(" (inlined, '%s') ", GetInlinedName());173}174lldb_private::SymbolContext sc;175m_opaque_ptr->CalculateSymbolContext(&sc);176if (sc.function) {177m_opaque_ptr->DumpAddressRanges(178&strm,179sc.function->GetAddressRange().GetBaseAddress().GetFileAddress());180}181} else182strm.PutCString("No value");183184return true;185}186187uint32_t SBBlock::GetNumRanges() {188LLDB_INSTRUMENT_VA(this);189190if (m_opaque_ptr)191return m_opaque_ptr->GetNumRanges();192return 0;193}194195lldb::SBAddress SBBlock::GetRangeStartAddress(uint32_t idx) {196LLDB_INSTRUMENT_VA(this, idx);197198lldb::SBAddress sb_addr;199if (m_opaque_ptr) {200AddressRange range;201if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {202sb_addr.ref() = range.GetBaseAddress();203}204}205return sb_addr;206}207208lldb::SBAddress SBBlock::GetRangeEndAddress(uint32_t idx) {209LLDB_INSTRUMENT_VA(this, idx);210211lldb::SBAddress sb_addr;212if (m_opaque_ptr) {213AddressRange range;214if (m_opaque_ptr->GetRangeAtIndex(idx, range)) {215sb_addr.ref() = range.GetBaseAddress();216sb_addr.ref().Slide(range.GetByteSize());217}218}219return sb_addr;220}221222lldb::SBAddressRangeList SBBlock::GetRanges() {223LLDB_INSTRUMENT_VA(this);224225lldb::SBAddressRangeList sb_ranges;226if (m_opaque_ptr)227sb_ranges.m_opaque_up->ref() = m_opaque_ptr->GetRanges();228return sb_ranges;229}230231uint32_t SBBlock::GetRangeIndexForBlockAddress(lldb::SBAddress block_addr) {232LLDB_INSTRUMENT_VA(this, block_addr);233234if (m_opaque_ptr && block_addr.IsValid()) {235return m_opaque_ptr->GetRangeIndexContainingAddress(block_addr.ref());236}237238return UINT32_MAX;239}240241lldb::SBValueList SBBlock::GetVariables(lldb::SBFrame &frame, bool arguments,242bool locals, bool statics,243lldb::DynamicValueType use_dynamic) {244LLDB_INSTRUMENT_VA(this, frame, arguments, locals, statics, use_dynamic);245246Block *block = GetPtr();247SBValueList value_list;248if (block) {249StackFrameSP frame_sp(frame.GetFrameSP());250VariableListSP variable_list_sp(block->GetBlockVariableList(true));251252if (variable_list_sp) {253const size_t num_variables = variable_list_sp->GetSize();254if (num_variables) {255for (size_t i = 0; i < num_variables; ++i) {256VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));257if (variable_sp) {258bool add_variable = false;259switch (variable_sp->GetScope()) {260case eValueTypeVariableGlobal:261case eValueTypeVariableStatic:262case eValueTypeVariableThreadLocal:263add_variable = statics;264break;265266case eValueTypeVariableArgument:267add_variable = arguments;268break;269270case eValueTypeVariableLocal:271add_variable = locals;272break;273274default:275break;276}277if (add_variable) {278if (frame_sp) {279lldb::ValueObjectSP valobj_sp(280frame_sp->GetValueObjectForFrameVariable(variable_sp,281eNoDynamicValues));282SBValue value_sb;283value_sb.SetSP(valobj_sp, use_dynamic);284value_list.Append(value_sb);285}286}287}288}289}290}291}292return value_list;293}294295lldb::SBValueList SBBlock::GetVariables(lldb::SBTarget &target, bool arguments,296bool locals, bool statics) {297LLDB_INSTRUMENT_VA(this, target, arguments, locals, statics);298299Block *block = GetPtr();300301SBValueList value_list;302if (block) {303TargetSP target_sp(target.GetSP());304305VariableListSP variable_list_sp(block->GetBlockVariableList(true));306307if (variable_list_sp) {308const size_t num_variables = variable_list_sp->GetSize();309if (num_variables) {310for (size_t i = 0; i < num_variables; ++i) {311VariableSP variable_sp(variable_list_sp->GetVariableAtIndex(i));312if (variable_sp) {313bool add_variable = false;314switch (variable_sp->GetScope()) {315case eValueTypeVariableGlobal:316case eValueTypeVariableStatic:317case eValueTypeVariableThreadLocal:318add_variable = statics;319break;320321case eValueTypeVariableArgument:322add_variable = arguments;323break;324325case eValueTypeVariableLocal:326add_variable = locals;327break;328329default:330break;331}332if (add_variable) {333if (target_sp)334value_list.Append(335ValueObjectVariable::Create(target_sp.get(), variable_sp));336}337}338}339}340}341}342return value_list;343}344345346