Path: blob/main/contrib/llvm-project/lldb/source/API/SBFunction.cpp
39587 views
//===-- SBFunction.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/SBFunction.h"9#include "lldb/API/SBAddressRange.h"10#include "lldb/API/SBProcess.h"11#include "lldb/API/SBStream.h"12#include "lldb/Core/Disassembler.h"13#include "lldb/Core/Module.h"14#include "lldb/Symbol/CompileUnit.h"15#include "lldb/Symbol/Function.h"16#include "lldb/Symbol/Type.h"17#include "lldb/Symbol/VariableList.h"18#include "lldb/Target/ExecutionContext.h"19#include "lldb/Target/Target.h"20#include "lldb/Utility/Instrumentation.h"2122using namespace lldb;23using namespace lldb_private;2425SBFunction::SBFunction() { LLDB_INSTRUMENT_VA(this); }2627SBFunction::SBFunction(lldb_private::Function *lldb_object_ptr)28: m_opaque_ptr(lldb_object_ptr) {}2930SBFunction::SBFunction(const lldb::SBFunction &rhs)31: m_opaque_ptr(rhs.m_opaque_ptr) {32LLDB_INSTRUMENT_VA(this, rhs);33}3435const SBFunction &SBFunction::operator=(const SBFunction &rhs) {36LLDB_INSTRUMENT_VA(this, rhs);3738m_opaque_ptr = rhs.m_opaque_ptr;39return *this;40}4142SBFunction::~SBFunction() { m_opaque_ptr = nullptr; }4344bool SBFunction::IsValid() const {45LLDB_INSTRUMENT_VA(this);46return this->operator bool();47}48SBFunction::operator bool() const {49LLDB_INSTRUMENT_VA(this);5051return m_opaque_ptr != nullptr;52}5354const char *SBFunction::GetName() const {55LLDB_INSTRUMENT_VA(this);5657if (m_opaque_ptr)58return m_opaque_ptr->GetName().AsCString();5960return nullptr;61}6263const char *SBFunction::GetDisplayName() const {64LLDB_INSTRUMENT_VA(this);6566if (m_opaque_ptr)67return m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();6869return nullptr;70}7172const char *SBFunction::GetMangledName() const {73LLDB_INSTRUMENT_VA(this);7475if (m_opaque_ptr)76return m_opaque_ptr->GetMangled().GetMangledName().AsCString();77return nullptr;78}7980bool SBFunction::operator==(const SBFunction &rhs) const {81LLDB_INSTRUMENT_VA(this, rhs);8283return m_opaque_ptr == rhs.m_opaque_ptr;84}8586bool SBFunction::operator!=(const SBFunction &rhs) const {87LLDB_INSTRUMENT_VA(this, rhs);8889return m_opaque_ptr != rhs.m_opaque_ptr;90}9192bool SBFunction::GetDescription(SBStream &s) {93LLDB_INSTRUMENT_VA(this, s);9495if (m_opaque_ptr) {96s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",97m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString());98Type *func_type = m_opaque_ptr->GetType();99if (func_type)100s.Printf(", type = %s", func_type->GetName().AsCString());101return true;102}103s.Printf("No value");104return false;105}106107SBInstructionList SBFunction::GetInstructions(SBTarget target) {108LLDB_INSTRUMENT_VA(this, target);109110return GetInstructions(target, nullptr);111}112113SBInstructionList SBFunction::GetInstructions(SBTarget target,114const char *flavor) {115LLDB_INSTRUMENT_VA(this, target, flavor);116117SBInstructionList sb_instructions;118if (m_opaque_ptr) {119TargetSP target_sp(target.GetSP());120std::unique_lock<std::recursive_mutex> lock;121ModuleSP module_sp(122m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());123if (target_sp && module_sp) {124lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());125const bool force_live_memory = true;126sb_instructions.SetDisassembler(Disassembler::DisassembleRange(127module_sp->GetArchitecture(), nullptr, flavor, *target_sp,128m_opaque_ptr->GetAddressRange(), force_live_memory));129}130}131return sb_instructions;132}133134lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }135136void SBFunction::reset(lldb_private::Function *lldb_object_ptr) {137m_opaque_ptr = lldb_object_ptr;138}139140SBAddress SBFunction::GetStartAddress() {141LLDB_INSTRUMENT_VA(this);142143SBAddress addr;144if (m_opaque_ptr)145addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());146return addr;147}148149SBAddress SBFunction::GetEndAddress() {150LLDB_INSTRUMENT_VA(this);151152SBAddress addr;153if (m_opaque_ptr) {154addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();155if (byte_size > 0) {156addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());157addr->Slide(byte_size);158}159}160return addr;161}162163lldb::SBAddressRangeList SBFunction::GetRanges() {164LLDB_INSTRUMENT_VA(this);165166lldb::SBAddressRangeList ranges;167if (m_opaque_ptr) {168lldb::SBAddressRange range;169(*range.m_opaque_up) = m_opaque_ptr->GetAddressRange();170ranges.Append(std::move(range));171}172173return ranges;174}175176const char *SBFunction::GetArgumentName(uint32_t arg_idx) {177LLDB_INSTRUMENT_VA(this, arg_idx);178179if (!m_opaque_ptr)180return nullptr;181182Block &block = m_opaque_ptr->GetBlock(true);183VariableListSP variable_list_sp = block.GetBlockVariableList(true);184if (!variable_list_sp)185return nullptr;186187VariableList arguments;188variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,189arguments, true);190lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);191if (!variable_sp)192return nullptr;193194return variable_sp->GetName().GetCString();195}196197uint32_t SBFunction::GetPrologueByteSize() {198LLDB_INSTRUMENT_VA(this);199200if (m_opaque_ptr)201return m_opaque_ptr->GetPrologueByteSize();202return 0;203}204205SBType SBFunction::GetType() {206LLDB_INSTRUMENT_VA(this);207208SBType sb_type;209if (m_opaque_ptr) {210Type *function_type = m_opaque_ptr->GetType();211if (function_type)212sb_type.ref().SetType(function_type->shared_from_this());213}214return sb_type;215}216217SBBlock SBFunction::GetBlock() {218LLDB_INSTRUMENT_VA(this);219220SBBlock sb_block;221if (m_opaque_ptr)222sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));223return sb_block;224}225226lldb::LanguageType SBFunction::GetLanguage() {227LLDB_INSTRUMENT_VA(this);228229if (m_opaque_ptr) {230if (m_opaque_ptr->GetCompileUnit())231return m_opaque_ptr->GetCompileUnit()->GetLanguage();232}233return lldb::eLanguageTypeUnknown;234}235236bool SBFunction::GetIsOptimized() {237LLDB_INSTRUMENT_VA(this);238239if (m_opaque_ptr) {240if (m_opaque_ptr->GetCompileUnit())241return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();242}243return false;244}245246247