Path: blob/main/contrib/llvm-project/lldb/source/API/SBAddress.cpp
39587 views
//===-- SBAddress.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/SBAddress.h"9#include "Utils.h"10#include "lldb/API/SBProcess.h"11#include "lldb/API/SBSection.h"12#include "lldb/API/SBStream.h"13#include "lldb/Core/Address.h"14#include "lldb/Core/Module.h"15#include "lldb/Symbol/LineEntry.h"16#include "lldb/Target/Target.h"17#include "lldb/Utility/Instrumentation.h"18#include "lldb/Utility/StreamString.h"1920using namespace lldb;21using namespace lldb_private;2223SBAddress::SBAddress() : m_opaque_up(new Address()) {24LLDB_INSTRUMENT_VA(this);25}2627SBAddress::SBAddress(const Address &address)28: m_opaque_up(std::make_unique<Address>(address)) {}2930SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {31LLDB_INSTRUMENT_VA(this, rhs);3233m_opaque_up = clone(rhs.m_opaque_up);34}3536SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)37: m_opaque_up(new Address(section.GetSP(), offset)) {38LLDB_INSTRUMENT_VA(this, section, offset);39}4041// Create an address by resolving a load address using the supplied target42SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)43: m_opaque_up(new Address()) {44LLDB_INSTRUMENT_VA(this, load_addr, target);4546SetLoadAddress(load_addr, target);47}4849SBAddress::~SBAddress() = default;5051const SBAddress &SBAddress::operator=(const SBAddress &rhs) {52LLDB_INSTRUMENT_VA(this, rhs);5354if (this != &rhs)55m_opaque_up = clone(rhs.m_opaque_up);56return *this;57}5859bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {60if (lhs.IsValid() && rhs.IsValid())61return lhs.ref() == rhs.ref();62return false;63}6465bool SBAddress::operator!=(const SBAddress &rhs) const {66LLDB_INSTRUMENT_VA(this, rhs);6768return !(*this == rhs);69}7071bool SBAddress::IsValid() const {72LLDB_INSTRUMENT_VA(this);73return this->operator bool();74}75SBAddress::operator bool() const {76LLDB_INSTRUMENT_VA(this);7778return m_opaque_up != nullptr && m_opaque_up->IsValid();79}8081void SBAddress::Clear() {82LLDB_INSTRUMENT_VA(this);8384m_opaque_up = std::make_unique<Address>();85}8687void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {88LLDB_INSTRUMENT_VA(this, section, offset);8990Address &addr = ref();91addr.SetSection(section.GetSP());92addr.SetOffset(offset);93}9495void SBAddress::SetAddress(const Address &address) { ref() = address; }9697lldb::addr_t SBAddress::GetFileAddress() const {98LLDB_INSTRUMENT_VA(this);99100if (m_opaque_up->IsValid())101return m_opaque_up->GetFileAddress();102else103return LLDB_INVALID_ADDRESS;104}105106lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {107LLDB_INSTRUMENT_VA(this, target);108109lldb::addr_t addr = LLDB_INVALID_ADDRESS;110TargetSP target_sp(target.GetSP());111if (target_sp) {112if (m_opaque_up->IsValid()) {113std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());114addr = m_opaque_up->GetLoadAddress(target_sp.get());115}116}117118return addr;119}120121void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {122LLDB_INSTRUMENT_VA(this, load_addr, target);123124// Create the address object if we don't already have one125ref();126if (target.IsValid())127*this = target.ResolveLoadAddress(load_addr);128else129m_opaque_up->Clear();130131// Check if we weren't were able to resolve a section offset address. If we132// weren't it is ok, the load address might be a location on the stack or133// heap, so we should just have an address with no section and a valid offset134if (!m_opaque_up->IsValid())135m_opaque_up->SetOffset(load_addr);136}137138bool SBAddress::OffsetAddress(addr_t offset) {139LLDB_INSTRUMENT_VA(this, offset);140141if (m_opaque_up->IsValid()) {142addr_t addr_offset = m_opaque_up->GetOffset();143if (addr_offset != LLDB_INVALID_ADDRESS) {144m_opaque_up->SetOffset(addr_offset + offset);145return true;146}147}148return false;149}150151lldb::SBSection SBAddress::GetSection() {152LLDB_INSTRUMENT_VA(this);153154lldb::SBSection sb_section;155if (m_opaque_up->IsValid())156sb_section.SetSP(m_opaque_up->GetSection());157return sb_section;158}159160lldb::addr_t SBAddress::GetOffset() {161LLDB_INSTRUMENT_VA(this);162163if (m_opaque_up->IsValid())164return m_opaque_up->GetOffset();165return 0;166}167168Address *SBAddress::operator->() { return m_opaque_up.get(); }169170const Address *SBAddress::operator->() const { return m_opaque_up.get(); }171172Address &SBAddress::ref() {173if (m_opaque_up == nullptr)174m_opaque_up = std::make_unique<Address>();175return *m_opaque_up;176}177178const Address &SBAddress::ref() const {179// This object should already have checked with "IsValid()" prior to calling180// this function. In case you didn't we will assert and die to let you know.181assert(m_opaque_up.get());182return *m_opaque_up;183}184185Address *SBAddress::get() { return m_opaque_up.get(); }186187bool SBAddress::GetDescription(SBStream &description) {188LLDB_INSTRUMENT_VA(this, description);189190// Call "ref()" on the stream to make sure it creates a backing stream in191// case there isn't one already...192Stream &strm = description.ref();193if (m_opaque_up->IsValid()) {194m_opaque_up->Dump(&strm, nullptr, Address::DumpStyleResolvedDescription,195Address::DumpStyleModuleWithFileAddress, 4);196} else197strm.PutCString("No value");198199return true;200}201202SBModule SBAddress::GetModule() {203LLDB_INSTRUMENT_VA(this);204205SBModule sb_module;206if (m_opaque_up->IsValid())207sb_module.SetSP(m_opaque_up->GetModule());208return sb_module;209}210211SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {212LLDB_INSTRUMENT_VA(this, resolve_scope);213214SBSymbolContext sb_sc;215SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);216if (m_opaque_up->IsValid())217m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);218return sb_sc;219}220221SBCompileUnit SBAddress::GetCompileUnit() {222LLDB_INSTRUMENT_VA(this);223224SBCompileUnit sb_comp_unit;225if (m_opaque_up->IsValid())226sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());227return sb_comp_unit;228}229230SBFunction SBAddress::GetFunction() {231LLDB_INSTRUMENT_VA(this);232233SBFunction sb_function;234if (m_opaque_up->IsValid())235sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());236return sb_function;237}238239SBBlock SBAddress::GetBlock() {240LLDB_INSTRUMENT_VA(this);241242SBBlock sb_block;243if (m_opaque_up->IsValid())244sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());245return sb_block;246}247248SBSymbol SBAddress::GetSymbol() {249LLDB_INSTRUMENT_VA(this);250251SBSymbol sb_symbol;252if (m_opaque_up->IsValid())253sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());254return sb_symbol;255}256257SBLineEntry SBAddress::GetLineEntry() {258LLDB_INSTRUMENT_VA(this);259260SBLineEntry sb_line_entry;261if (m_opaque_up->IsValid()) {262LineEntry line_entry;263if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))264sb_line_entry.SetLineEntry(line_entry);265}266return sb_line_entry;267}268269270