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