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