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 "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 
18 using namespace lldb;
19 using namespace lldb_private;
20 
21 SBSymbol::SBSymbol() { LLDB_INSTRUMENT_VA(this); }
22 
23 SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr)
24     : m_opaque_ptr(lldb_object_ptr) {}
25 
26 SBSymbol::SBSymbol(const lldb::SBSymbol &rhs) : m_opaque_ptr(rhs.m_opaque_ptr) {
27   LLDB_INSTRUMENT_VA(this, rhs);
28 }
29 
30 const 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 
37 SBSymbol::~SBSymbol() { m_opaque_ptr = nullptr; }
38 
39 void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) {
40   m_opaque_ptr = lldb_object_ptr;
41 }
42 
43 bool SBSymbol::IsValid() const {
44   LLDB_INSTRUMENT_VA(this);
45   return this->operator bool();
46 }
47 SBSymbol::operator bool() const {
48   LLDB_INSTRUMENT_VA(this);
49 
50   return m_opaque_ptr != nullptr;
51 }
52 
53 const 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 
63 const 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 
73 const 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 
82 bool SBSymbol::operator==(const SBSymbol &rhs) const {
83   LLDB_INSTRUMENT_VA(this, rhs);
84 
85   return m_opaque_ptr == rhs.m_opaque_ptr;
86 }
87 
88 bool SBSymbol::operator!=(const SBSymbol &rhs) const {
89   LLDB_INSTRUMENT_VA(this, rhs);
90 
91   return m_opaque_ptr != rhs.m_opaque_ptr;
92 }
93 
94 bool SBSymbol::GetDescription(SBStream &description) {
95   LLDB_INSTRUMENT_VA(this, description);
96 
97   Stream &strm = description.ref();
98 
99   if (m_opaque_ptr) {
100     m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, nullptr);
101   } else
102     strm.PutCString("No value");
103 
104   return true;
105 }
106 
107 SBInstructionList SBSymbol::GetInstructions(SBTarget target) {
108   LLDB_INSTRUMENT_VA(this, target);
109 
110   return GetInstructions(target, nullptr);
111 }
112 
113 SBInstructionList SBSymbol::GetInstructions(SBTarget target,
114                                             const char *flavor_string) {
115   LLDB_INSTRUMENT_VA(this, target, flavor_string);
116 
117   SBInstructionList sb_instructions;
118   if (m_opaque_ptr) {
119     TargetSP target_sp(target.GetSP());
120     std::unique_lock<std::recursive_mutex> lock;
121     if (target_sp && m_opaque_ptr->ValueIsAddress()) {
122       lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
123       const Address &symbol_addr = m_opaque_ptr->GetAddressRef();
124       ModuleSP module_sp = symbol_addr.GetModule();
125       if (module_sp) {
126         AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize());
127         const bool force_live_memory = true;
128         sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
129             module_sp->GetArchitecture(), nullptr, flavor_string, *target_sp,
130             symbol_range, force_live_memory));
131       }
132     }
133   }
134   return sb_instructions;
135 }
136 
137 lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; }
138 
139 void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; }
140 
141 SBAddress SBSymbol::GetStartAddress() {
142   LLDB_INSTRUMENT_VA(this);
143 
144   SBAddress addr;
145   if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
146     addr.SetAddress(m_opaque_ptr->GetAddressRef());
147   }
148   return addr;
149 }
150 
151 SBAddress SBSymbol::GetEndAddress() {
152   LLDB_INSTRUMENT_VA(this);
153 
154   SBAddress addr;
155   if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) {
156     lldb::addr_t range_size = m_opaque_ptr->GetByteSize();
157     if (range_size > 0) {
158       addr.SetAddress(m_opaque_ptr->GetAddressRef());
159       addr->Slide(m_opaque_ptr->GetByteSize());
160     }
161   }
162   return addr;
163 }
164 
165 uint32_t SBSymbol::GetPrologueByteSize() {
166   LLDB_INSTRUMENT_VA(this);
167 
168   if (m_opaque_ptr)
169     return m_opaque_ptr->GetPrologueByteSize();
170   return 0;
171 }
172 
173 SymbolType SBSymbol::GetType() {
174   LLDB_INSTRUMENT_VA(this);
175 
176   if (m_opaque_ptr)
177     return m_opaque_ptr->GetType();
178   return eSymbolTypeInvalid;
179 }
180 
181 bool SBSymbol::IsExternal() {
182   LLDB_INSTRUMENT_VA(this);
183 
184   if (m_opaque_ptr)
185     return m_opaque_ptr->IsExternal();
186   return false;
187 }
188 
189 bool SBSymbol::IsSynthetic() {
190   LLDB_INSTRUMENT_VA(this);
191 
192   if (m_opaque_ptr)
193     return m_opaque_ptr->IsSynthetic();
194   return false;
195 }
196