1 //===-- SBSymbol.cpp ------------------------------------------------------===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBSymbol.h"
10 #include "SBReproducerPrivate.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Core/Disassembler.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/Symbol.h"
15 #include "lldb/Target/ExecutionContext.h"
16 #include "lldb/Target/Target.h"
19 using namespace lldb_private;
21 SBSymbol::SBSymbol() : m_opaque_ptr(nullptr) {
22 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSymbol);
25 SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr)
26 : m_opaque_ptr(lldb_object_ptr) {}
28 SBSymbol::SBSymbol(const lldb::SBSymbol &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
29 LLDB_RECORD_CONSTRUCTOR(SBSymbol, (const lldb::SBSymbol &), rhs);
32 const SBSymbol &SBSymbol::operator=(const SBSymbol &rhs) {
33 LLDB_RECORD_METHOD(const lldb::SBSymbol &,
34 SBSymbol, operator=,(const lldb::SBSymbol &), rhs);
36 m_opaque_ptr = rhs.m_opaque_ptr;
37 return LLDB_RECORD_RESULT(*this);
40 SBSymbol::~SBSymbol() { m_opaque_ptr = nullptr; }
42 void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) {
43 m_opaque_ptr = lldb_object_ptr;
46 bool SBSymbol::IsValid() const {
47 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbol, IsValid);
48 return this->operator bool();
50 SBSymbol::operator bool() const {
51 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbol, operator bool);
53 return m_opaque_ptr != nullptr;
56 const char *SBSymbol::GetName() const {
57 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetName);
59 const char *name = nullptr;
61 name = m_opaque_ptr->GetName().AsCString();
66 const char *SBSymbol::GetDisplayName() const {
67 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetDisplayName);
69 const char *name = nullptr;
71 name = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
76 const char *SBSymbol::GetMangledName() const {
77 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetMangledName);
79 const char *name = nullptr;
81 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
85 bool SBSymbol::operator==(const SBSymbol &rhs) const {
86 LLDB_RECORD_METHOD_CONST(bool, SBSymbol, operator==,(const lldb::SBSymbol &),
89 return m_opaque_ptr == rhs.m_opaque_ptr;
92 bool SBSymbol::operator!=(const SBSymbol &rhs) const {
93 LLDB_RECORD_METHOD_CONST(bool, SBSymbol, operator!=,(const lldb::SBSymbol &),
96 return m_opaque_ptr != rhs.m_opaque_ptr;
99 bool SBSymbol::GetDescription(SBStream &description) {
100 LLDB_RECORD_METHOD(bool, SBSymbol, GetDescription, (lldb::SBStream &),
103 Stream &strm = description.ref();
106 m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, nullptr);
108 strm.PutCString("No value");
113 SBInstructionList SBSymbol::GetInstructions(SBTarget target) {
114 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
115 (lldb::SBTarget), target);
117 return LLDB_RECORD_RESULT(GetInstructions(target, nullptr));
120 SBInstructionList SBSymbol::GetInstructions(SBTarget target,
121 const char *flavor_string) {
122 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
123 (lldb::SBTarget, const char *), target, flavor_string);
125 SBInstructionList sb_instructions;
127 TargetSP target_sp(target.GetSP());
128 std::unique_lock<std::recursive_mutex> lock;
129 if (target_sp && m_opaque_ptr->ValueIsAddress()) {
130 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
131 const Address &symbol_addr = m_opaque_ptr->GetAddressRef();
132 ModuleSP module_sp = symbol_addr.GetModule();
134 AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize());
135 const bool prefer_file_cache = false;
136 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
137 module_sp->GetArchitecture(), nullptr, flavor_string, *target_sp,
138 symbol_range, prefer_file_cache));
142 return LLDB_RECORD_RESULT(sb_instructions);
145 lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; }
147 void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; }
149 SBAddress SBSymbol::GetStartAddress() {
150 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBAddress, SBSymbol, GetStartAddress);
153 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
154 addr.SetAddress(&m_opaque_ptr->GetAddressRef());
156 return LLDB_RECORD_RESULT(addr);
159 SBAddress SBSymbol::GetEndAddress() {
160 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBAddress, SBSymbol, GetEndAddress);
163 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
164 lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
165 if (range_size > 0) {
166 addr.SetAddress(&m_opaque_ptr->GetAddressRef());
167 addr->Slide(m_opaque_ptr->GetByteSize());
170 return LLDB_RECORD_RESULT(addr);
173 uint32_t SBSymbol::GetPrologueByteSize() {
174 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSymbol, GetPrologueByteSize);
177 return m_opaque_ptr->GetPrologueByteSize();
181 SymbolType SBSymbol::GetType() {
182 LLDB_RECORD_METHOD_NO_ARGS(lldb::SymbolType, SBSymbol, GetType);
185 return m_opaque_ptr->GetType();
186 return eSymbolTypeInvalid;
189 bool SBSymbol::IsExternal() {
190 LLDB_RECORD_METHOD_NO_ARGS(bool, SBSymbol, IsExternal);
193 return m_opaque_ptr->IsExternal();
197 bool SBSymbol::IsSynthetic() {
198 LLDB_RECORD_METHOD_NO_ARGS(bool, SBSymbol, IsSynthetic);
201 return m_opaque_ptr->IsSynthetic();
205 namespace lldb_private {
209 void RegisterMethods<SBSymbol>(Registry &R) {
210 LLDB_REGISTER_CONSTRUCTOR(SBSymbol, ());
211 LLDB_REGISTER_CONSTRUCTOR(SBSymbol, (const lldb::SBSymbol &));
212 LLDB_REGISTER_METHOD(const lldb::SBSymbol &,
213 SBSymbol, operator=,(const lldb::SBSymbol &));
214 LLDB_REGISTER_METHOD_CONST(bool, SBSymbol, IsValid, ());
215 LLDB_REGISTER_METHOD_CONST(bool, SBSymbol, operator bool, ());
216 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetName, ());
217 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetDisplayName, ());
218 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetMangledName, ());
219 LLDB_REGISTER_METHOD_CONST(bool,
220 SBSymbol, operator==,(const lldb::SBSymbol &));
221 LLDB_REGISTER_METHOD_CONST(bool,
222 SBSymbol, operator!=,(const lldb::SBSymbol &));
223 LLDB_REGISTER_METHOD(bool, SBSymbol, GetDescription, (lldb::SBStream &));
224 LLDB_REGISTER_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
226 LLDB_REGISTER_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
227 (lldb::SBTarget, const char *));
228 LLDB_REGISTER_METHOD(lldb::SBAddress, SBSymbol, GetStartAddress, ());
229 LLDB_REGISTER_METHOD(lldb::SBAddress, SBSymbol, GetEndAddress, ());
230 LLDB_REGISTER_METHOD(uint32_t, SBSymbol, GetPrologueByteSize, ());
231 LLDB_REGISTER_METHOD(lldb::SymbolType, SBSymbol, GetType, ());
232 LLDB_REGISTER_METHOD(bool, SBSymbol, IsExternal, ());
233 LLDB_REGISTER_METHOD(bool, SBSymbol, IsSynthetic, ());