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-exception
6 //
7 //===----------------------------------------------------------------------===//
8
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"
17
18 using namespace lldb;
19 using namespace lldb_private;
20
SBSymbol()21 SBSymbol::SBSymbol() { LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBSymbol); }
22
SBSymbol(lldb_private::Symbol * lldb_object_ptr)23 SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr)
24 : m_opaque_ptr(lldb_object_ptr) {}
25
SBSymbol(const lldb::SBSymbol & rhs)26 SBSymbol::SBSymbol(const lldb::SBSymbol &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
27 LLDB_RECORD_CONSTRUCTOR(SBSymbol, (const lldb::SBSymbol &), rhs);
28 }
29
operator =(const SBSymbol & rhs)30 const SBSymbol &SBSymbol::operator=(const SBSymbol &rhs) {
31 LLDB_RECORD_METHOD(const lldb::SBSymbol &,
32 SBSymbol, operator=,(const lldb::SBSymbol &), rhs);
33
34 m_opaque_ptr = rhs.m_opaque_ptr;
35 return LLDB_RECORD_RESULT(*this);
36 }
37
~SBSymbol()38 SBSymbol::~SBSymbol() { m_opaque_ptr = nullptr; }
39
SetSymbol(lldb_private::Symbol * lldb_object_ptr)40 void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) {
41 m_opaque_ptr = lldb_object_ptr;
42 }
43
IsValid() const44 bool SBSymbol::IsValid() const {
45 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbol, IsValid);
46 return this->operator bool();
47 }
operator bool() const48 SBSymbol::operator bool() const {
49 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBSymbol, operator bool);
50
51 return m_opaque_ptr != nullptr;
52 }
53
GetName() const54 const char *SBSymbol::GetName() const {
55 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetName);
56
57 const char *name = nullptr;
58 if (m_opaque_ptr)
59 name = m_opaque_ptr->GetName().AsCString();
60
61 return name;
62 }
63
GetDisplayName() const64 const char *SBSymbol::GetDisplayName() const {
65 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetDisplayName);
66
67 const char *name = nullptr;
68 if (m_opaque_ptr)
69 name = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
70
71 return name;
72 }
73
GetMangledName() const74 const char *SBSymbol::GetMangledName() const {
75 LLDB_RECORD_METHOD_CONST_NO_ARGS(const char *, SBSymbol, GetMangledName);
76
77 const char *name = nullptr;
78 if (m_opaque_ptr)
79 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
80 return name;
81 }
82
operator ==(const SBSymbol & rhs) const83 bool SBSymbol::operator==(const SBSymbol &rhs) const {
84 LLDB_RECORD_METHOD_CONST(bool, SBSymbol, operator==,(const lldb::SBSymbol &),
85 rhs);
86
87 return m_opaque_ptr == rhs.m_opaque_ptr;
88 }
89
operator !=(const SBSymbol & rhs) const90 bool SBSymbol::operator!=(const SBSymbol &rhs) const {
91 LLDB_RECORD_METHOD_CONST(bool, SBSymbol, operator!=,(const lldb::SBSymbol &),
92 rhs);
93
94 return m_opaque_ptr != rhs.m_opaque_ptr;
95 }
96
GetDescription(SBStream & description)97 bool SBSymbol::GetDescription(SBStream &description) {
98 LLDB_RECORD_METHOD(bool, SBSymbol, GetDescription, (lldb::SBStream &),
99 description);
100
101 Stream &strm = description.ref();
102
103 if (m_opaque_ptr) {
104 m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, nullptr);
105 } else
106 strm.PutCString("No value");
107
108 return true;
109 }
110
GetInstructions(SBTarget target)111 SBInstructionList SBSymbol::GetInstructions(SBTarget target) {
112 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
113 (lldb::SBTarget), target);
114
115 return LLDB_RECORD_RESULT(GetInstructions(target, nullptr));
116 }
117
GetInstructions(SBTarget target,const char * flavor_string)118 SBInstructionList SBSymbol::GetInstructions(SBTarget target,
119 const char *flavor_string) {
120 LLDB_RECORD_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
121 (lldb::SBTarget, const char *), target, flavor_string);
122
123 SBInstructionList sb_instructions;
124 if (m_opaque_ptr) {
125 TargetSP target_sp(target.GetSP());
126 std::unique_lock<std::recursive_mutex> lock;
127 if (target_sp && m_opaque_ptr->ValueIsAddress()) {
128 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
129 const Address &symbol_addr = m_opaque_ptr->GetAddressRef();
130 ModuleSP module_sp = symbol_addr.GetModule();
131 if (module_sp) {
132 AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize());
133 const bool force_live_memory = true;
134 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
135 module_sp->GetArchitecture(), nullptr, flavor_string, *target_sp,
136 symbol_range, force_live_memory));
137 }
138 }
139 }
140 return LLDB_RECORD_RESULT(sb_instructions);
141 }
142
get()143 lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; }
144
reset(lldb_private::Symbol * symbol)145 void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; }
146
GetStartAddress()147 SBAddress SBSymbol::GetStartAddress() {
148 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBAddress, SBSymbol, GetStartAddress);
149
150 SBAddress addr;
151 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
152 addr.SetAddress(m_opaque_ptr->GetAddressRef());
153 }
154 return LLDB_RECORD_RESULT(addr);
155 }
156
GetEndAddress()157 SBAddress SBSymbol::GetEndAddress() {
158 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBAddress, SBSymbol, GetEndAddress);
159
160 SBAddress addr;
161 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
162 lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
163 if (range_size > 0) {
164 addr.SetAddress(m_opaque_ptr->GetAddressRef());
165 addr->Slide(m_opaque_ptr->GetByteSize());
166 }
167 }
168 return LLDB_RECORD_RESULT(addr);
169 }
170
GetPrologueByteSize()171 uint32_t SBSymbol::GetPrologueByteSize() {
172 LLDB_RECORD_METHOD_NO_ARGS(uint32_t, SBSymbol, GetPrologueByteSize);
173
174 if (m_opaque_ptr)
175 return m_opaque_ptr->GetPrologueByteSize();
176 return 0;
177 }
178
GetType()179 SymbolType SBSymbol::GetType() {
180 LLDB_RECORD_METHOD_NO_ARGS(lldb::SymbolType, SBSymbol, GetType);
181
182 if (m_opaque_ptr)
183 return m_opaque_ptr->GetType();
184 return eSymbolTypeInvalid;
185 }
186
IsExternal()187 bool SBSymbol::IsExternal() {
188 LLDB_RECORD_METHOD_NO_ARGS(bool, SBSymbol, IsExternal);
189
190 if (m_opaque_ptr)
191 return m_opaque_ptr->IsExternal();
192 return false;
193 }
194
IsSynthetic()195 bool SBSymbol::IsSynthetic() {
196 LLDB_RECORD_METHOD_NO_ARGS(bool, SBSymbol, IsSynthetic);
197
198 if (m_opaque_ptr)
199 return m_opaque_ptr->IsSynthetic();
200 return false;
201 }
202
203 namespace lldb_private {
204 namespace repro {
205
206 template <>
RegisterMethods(Registry & R)207 void RegisterMethods<SBSymbol>(Registry &R) {
208 LLDB_REGISTER_CONSTRUCTOR(SBSymbol, ());
209 LLDB_REGISTER_CONSTRUCTOR(SBSymbol, (const lldb::SBSymbol &));
210 LLDB_REGISTER_METHOD(const lldb::SBSymbol &,
211 SBSymbol, operator=,(const lldb::SBSymbol &));
212 LLDB_REGISTER_METHOD_CONST(bool, SBSymbol, IsValid, ());
213 LLDB_REGISTER_METHOD_CONST(bool, SBSymbol, operator bool, ());
214 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetName, ());
215 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetDisplayName, ());
216 LLDB_REGISTER_METHOD_CONST(const char *, SBSymbol, GetMangledName, ());
217 LLDB_REGISTER_METHOD_CONST(bool,
218 SBSymbol, operator==,(const lldb::SBSymbol &));
219 LLDB_REGISTER_METHOD_CONST(bool,
220 SBSymbol, operator!=,(const lldb::SBSymbol &));
221 LLDB_REGISTER_METHOD(bool, SBSymbol, GetDescription, (lldb::SBStream &));
222 LLDB_REGISTER_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
223 (lldb::SBTarget));
224 LLDB_REGISTER_METHOD(lldb::SBInstructionList, SBSymbol, GetInstructions,
225 (lldb::SBTarget, const char *));
226 LLDB_REGISTER_METHOD(lldb::SBAddress, SBSymbol, GetStartAddress, ());
227 LLDB_REGISTER_METHOD(lldb::SBAddress, SBSymbol, GetEndAddress, ());
228 LLDB_REGISTER_METHOD(uint32_t, SBSymbol, GetPrologueByteSize, ());
229 LLDB_REGISTER_METHOD(lldb::SymbolType, SBSymbol, GetType, ());
230 LLDB_REGISTER_METHOD(bool, SBSymbol, IsExternal, ());
231 LLDB_REGISTER_METHOD(bool, SBSymbol, IsSynthetic, ());
232 }
233
234 }
235 }
236