248 lines · cpp
1//===-- SBSymbol.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 "lldb/API/SBSymbol.h"10#include "lldb/API/SBStream.h"11#include "lldb/Core/Disassembler.h"12#include "lldb/Core/Module.h"13#include "lldb/Symbol/Symbol.h"14#include "lldb/Target/ExecutionContext.h"15#include "lldb/Target/Target.h"16#include "lldb/Utility/Instrumentation.h"17 18using namespace lldb;19using namespace lldb_private;20 21SBSymbol::SBSymbol() { LLDB_INSTRUMENT_VA(this); }22 23SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr)24 : m_opaque_ptr(lldb_object_ptr) {}25 26SBSymbol::SBSymbol(const lldb::SBSymbol &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {27 LLDB_INSTRUMENT_VA(this, rhs);28}29 30const SBSymbol &SBSymbol::operator=(const SBSymbol &rhs) {31 LLDB_INSTRUMENT_VA(this, rhs);32 33 m_opaque_ptr = rhs.m_opaque_ptr;34 return *this;35}36 37SBSymbol::~SBSymbol() { m_opaque_ptr = nullptr; }38 39void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) {40 m_opaque_ptr = lldb_object_ptr;41}42 43bool SBSymbol::IsValid() const {44 LLDB_INSTRUMENT_VA(this);45 return this->operator bool();46}47SBSymbol::operator bool() const {48 LLDB_INSTRUMENT_VA(this);49 50 return m_opaque_ptr != nullptr;51}52 53const char *SBSymbol::GetName() const {54 LLDB_INSTRUMENT_VA(this);55 56 const char *name = nullptr;57 if (m_opaque_ptr)58 name = m_opaque_ptr->GetName().AsCString();59 60 return name;61}62 63const char *SBSymbol::GetDisplayName() const {64 LLDB_INSTRUMENT_VA(this);65 66 const char *name = nullptr;67 if (m_opaque_ptr)68 name = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();69 70 return name;71}72 73const char *SBSymbol::GetMangledName() const {74 LLDB_INSTRUMENT_VA(this);75 76 const char *name = nullptr;77 if (m_opaque_ptr)78 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();79 return name;80}81 82const char *SBSymbol::GetBaseName() const {83 LLDB_INSTRUMENT_VA(this);84 85 if (!m_opaque_ptr)86 return nullptr;87 88 return m_opaque_ptr->GetMangled().GetBaseName().AsCString();89}90 91bool SBSymbol::operator==(const SBSymbol &rhs) const {92 LLDB_INSTRUMENT_VA(this, rhs);93 94 return m_opaque_ptr == rhs.m_opaque_ptr;95}96 97bool SBSymbol::operator!=(const SBSymbol &rhs) const {98 LLDB_INSTRUMENT_VA(this, rhs);99 100 return m_opaque_ptr != rhs.m_opaque_ptr;101}102 103bool SBSymbol::GetDescription(SBStream &description) {104 LLDB_INSTRUMENT_VA(this, description);105 106 Stream &strm = description.ref();107 108 if (m_opaque_ptr) {109 m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, nullptr);110 } else111 strm.PutCString("No value");112 113 return true;114}115 116SBInstructionList SBSymbol::GetInstructions(SBTarget target) {117 LLDB_INSTRUMENT_VA(this, target);118 119 return GetInstructions(target, nullptr);120}121 122SBInstructionList SBSymbol::GetInstructions(SBTarget target,123 const char *flavor_string) {124 LLDB_INSTRUMENT_VA(this, target, flavor_string);125 126 SBInstructionList sb_instructions;127 if (m_opaque_ptr) {128 TargetSP target_sp(target.GetSP());129 std::unique_lock<std::recursive_mutex> lock;130 if (target_sp && m_opaque_ptr->ValueIsAddress()) {131 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());132 const Address &symbol_addr = m_opaque_ptr->GetAddressRef();133 ModuleSP module_sp = symbol_addr.GetModule();134 if (module_sp) {135 AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize());136 const bool force_live_memory = true;137 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(138 module_sp->GetArchitecture(), nullptr, flavor_string,139 target_sp->GetDisassemblyCPU(), target_sp->GetDisassemblyFeatures(),140 *target_sp, symbol_range, force_live_memory));141 }142 }143 }144 return sb_instructions;145}146 147lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; }148 149void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; }150 151SBAddress SBSymbol::GetStartAddress() {152 LLDB_INSTRUMENT_VA(this);153 154 SBAddress addr;155 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {156 addr.SetAddress(m_opaque_ptr->GetAddressRef());157 }158 return addr;159}160 161SBAddress SBSymbol::GetEndAddress() {162 LLDB_INSTRUMENT_VA(this);163 164 SBAddress addr;165 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {166 lldb::addr_t range_size = m_opaque_ptr->GetByteSize();167 if (range_size > 0) {168 addr.SetAddress(m_opaque_ptr->GetAddressRef());169 addr->Slide(m_opaque_ptr->GetByteSize());170 }171 }172 return addr;173}174 175uint64_t SBSymbol::GetValue() {176 LLDB_INSTRUMENT_VA(this);177 if (m_opaque_ptr)178 return m_opaque_ptr->GetRawValue();179 return 0;180}181 182uint64_t SBSymbol::GetSize() {183 LLDB_INSTRUMENT_VA(this);184 if (m_opaque_ptr && m_opaque_ptr->GetByteSizeIsValid())185 return m_opaque_ptr->GetByteSize();186 return 0;187}188 189uint32_t SBSymbol::GetPrologueByteSize() {190 LLDB_INSTRUMENT_VA(this);191 192 if (m_opaque_ptr)193 return m_opaque_ptr->GetPrologueByteSize();194 return 0;195}196 197SymbolType SBSymbol::GetType() {198 LLDB_INSTRUMENT_VA(this);199 200 if (m_opaque_ptr)201 return m_opaque_ptr->GetType();202 return eSymbolTypeInvalid;203}204 205uint32_t SBSymbol::GetID() {206 LLDB_INSTRUMENT_VA(this);207 208 if (m_opaque_ptr)209 return m_opaque_ptr->GetID();210 return 0;211}212 213bool SBSymbol::IsExternal() {214 LLDB_INSTRUMENT_VA(this);215 216 if (m_opaque_ptr)217 return m_opaque_ptr->IsExternal();218 return false;219}220 221bool SBSymbol::IsSynthetic() {222 LLDB_INSTRUMENT_VA(this);223 224 if (m_opaque_ptr)225 return m_opaque_ptr->IsSynthetic();226 return false;227}228 229bool SBSymbol::IsDebug() {230 LLDB_INSTRUMENT_VA(this);231 232 if (m_opaque_ptr)233 return m_opaque_ptr->IsDebug();234 return false;235}236 237const char *SBSymbol::GetTypeAsString(lldb::SymbolType symbol_type) {238 LLDB_INSTRUMENT_VA(symbol_type);239 240 return Symbol::GetTypeAsString(symbol_type);241}242 243lldb::SymbolType SBSymbol::GetTypeFromString(const char *str) {244 LLDB_INSTRUMENT_VA(str);245 246 return Symbol::GetTypeFromString(str);247}248