1 //===-- SBFunction.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/SBFunction.h"
10 #include "lldb/API/SBProcess.h"
11 #include "lldb/API/SBStream.h"
12 #include "lldb/Core/Disassembler.h"
13 #include "lldb/Core/Module.h"
14 #include "lldb/Symbol/CompileUnit.h"
15 #include "lldb/Symbol/Function.h"
16 #include "lldb/Symbol/Type.h"
17 #include "lldb/Symbol/VariableList.h"
18 #include "lldb/Target/ExecutionContext.h"
19 #include "lldb/Target/Target.h"
20 #include "lldb/Utility/Instrumentation.h"
21
22 using namespace lldb;
23 using namespace lldb_private;
24
SBFunction()25 SBFunction::SBFunction() { LLDB_INSTRUMENT_VA(this); }
26
SBFunction(lldb_private::Function * lldb_object_ptr)27 SBFunction::SBFunction(lldb_private::Function *lldb_object_ptr)
28 : m_opaque_ptr(lldb_object_ptr) {}
29
SBFunction(const lldb::SBFunction & rhs)30 SBFunction::SBFunction(const lldb::SBFunction &rhs)
31 : m_opaque_ptr(rhs.m_opaque_ptr) {
32 LLDB_INSTRUMENT_VA(this, rhs);
33 }
34
operator =(const SBFunction & rhs)35 const SBFunction &SBFunction::operator=(const SBFunction &rhs) {
36 LLDB_INSTRUMENT_VA(this, rhs);
37
38 m_opaque_ptr = rhs.m_opaque_ptr;
39 return *this;
40 }
41
~SBFunction()42 SBFunction::~SBFunction() { m_opaque_ptr = nullptr; }
43
IsValid() const44 bool SBFunction::IsValid() const {
45 LLDB_INSTRUMENT_VA(this);
46 return this->operator bool();
47 }
operator bool() const48 SBFunction::operator bool() const {
49 LLDB_INSTRUMENT_VA(this);
50
51 return m_opaque_ptr != nullptr;
52 }
53
GetName() const54 const char *SBFunction::GetName() const {
55 LLDB_INSTRUMENT_VA(this);
56
57 const char *cstr = nullptr;
58 if (m_opaque_ptr)
59 cstr = m_opaque_ptr->GetName().AsCString();
60
61 return cstr;
62 }
63
GetDisplayName() const64 const char *SBFunction::GetDisplayName() const {
65 LLDB_INSTRUMENT_VA(this);
66
67 const char *cstr = nullptr;
68 if (m_opaque_ptr)
69 cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
70
71 return cstr;
72 }
73
GetMangledName() const74 const char *SBFunction::GetMangledName() const {
75 LLDB_INSTRUMENT_VA(this);
76
77 const char *cstr = nullptr;
78 if (m_opaque_ptr)
79 cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
80 return cstr;
81 }
82
operator ==(const SBFunction & rhs) const83 bool SBFunction::operator==(const SBFunction &rhs) const {
84 LLDB_INSTRUMENT_VA(this, rhs);
85
86 return m_opaque_ptr == rhs.m_opaque_ptr;
87 }
88
operator !=(const SBFunction & rhs) const89 bool SBFunction::operator!=(const SBFunction &rhs) const {
90 LLDB_INSTRUMENT_VA(this, rhs);
91
92 return m_opaque_ptr != rhs.m_opaque_ptr;
93 }
94
GetDescription(SBStream & s)95 bool SBFunction::GetDescription(SBStream &s) {
96 LLDB_INSTRUMENT_VA(this, s);
97
98 if (m_opaque_ptr) {
99 s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
100 m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString());
101 Type *func_type = m_opaque_ptr->GetType();
102 if (func_type)
103 s.Printf(", type = %s", func_type->GetName().AsCString());
104 return true;
105 }
106 s.Printf("No value");
107 return false;
108 }
109
GetInstructions(SBTarget target)110 SBInstructionList SBFunction::GetInstructions(SBTarget target) {
111 LLDB_INSTRUMENT_VA(this, target);
112
113 return GetInstructions(target, nullptr);
114 }
115
GetInstructions(SBTarget target,const char * flavor)116 SBInstructionList SBFunction::GetInstructions(SBTarget target,
117 const char *flavor) {
118 LLDB_INSTRUMENT_VA(this, target, flavor);
119
120 SBInstructionList sb_instructions;
121 if (m_opaque_ptr) {
122 TargetSP target_sp(target.GetSP());
123 std::unique_lock<std::recursive_mutex> lock;
124 ModuleSP module_sp(
125 m_opaque_ptr->GetAddressRange().GetBaseAddress().GetModule());
126 if (target_sp && module_sp) {
127 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
128 const bool force_live_memory = true;
129 sb_instructions.SetDisassembler(Disassembler::DisassembleRange(
130 module_sp->GetArchitecture(), nullptr, flavor, *target_sp,
131 m_opaque_ptr->GetAddressRange(), force_live_memory));
132 }
133 }
134 return sb_instructions;
135 }
136
get()137 lldb_private::Function *SBFunction::get() { return m_opaque_ptr; }
138
reset(lldb_private::Function * lldb_object_ptr)139 void SBFunction::reset(lldb_private::Function *lldb_object_ptr) {
140 m_opaque_ptr = lldb_object_ptr;
141 }
142
GetStartAddress()143 SBAddress SBFunction::GetStartAddress() {
144 LLDB_INSTRUMENT_VA(this);
145
146 SBAddress addr;
147 if (m_opaque_ptr)
148 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
149 return addr;
150 }
151
GetEndAddress()152 SBAddress SBFunction::GetEndAddress() {
153 LLDB_INSTRUMENT_VA(this);
154
155 SBAddress addr;
156 if (m_opaque_ptr) {
157 addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
158 if (byte_size > 0) {
159 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
160 addr->Slide(byte_size);
161 }
162 }
163 return addr;
164 }
165
GetArgumentName(uint32_t arg_idx)166 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
167 LLDB_INSTRUMENT_VA(this, arg_idx);
168
169 if (m_opaque_ptr) {
170 Block &block = m_opaque_ptr->GetBlock(true);
171 VariableListSP variable_list_sp = block.GetBlockVariableList(true);
172 if (variable_list_sp) {
173 VariableList arguments;
174 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
175 arguments, true);
176 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
177 if (variable_sp)
178 return variable_sp->GetName().GetCString();
179 }
180 }
181 return nullptr;
182 }
183
GetPrologueByteSize()184 uint32_t SBFunction::GetPrologueByteSize() {
185 LLDB_INSTRUMENT_VA(this);
186
187 if (m_opaque_ptr)
188 return m_opaque_ptr->GetPrologueByteSize();
189 return 0;
190 }
191
GetType()192 SBType SBFunction::GetType() {
193 LLDB_INSTRUMENT_VA(this);
194
195 SBType sb_type;
196 if (m_opaque_ptr) {
197 Type *function_type = m_opaque_ptr->GetType();
198 if (function_type)
199 sb_type.ref().SetType(function_type->shared_from_this());
200 }
201 return sb_type;
202 }
203
GetBlock()204 SBBlock SBFunction::GetBlock() {
205 LLDB_INSTRUMENT_VA(this);
206
207 SBBlock sb_block;
208 if (m_opaque_ptr)
209 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
210 return sb_block;
211 }
212
GetLanguage()213 lldb::LanguageType SBFunction::GetLanguage() {
214 LLDB_INSTRUMENT_VA(this);
215
216 if (m_opaque_ptr) {
217 if (m_opaque_ptr->GetCompileUnit())
218 return m_opaque_ptr->GetCompileUnit()->GetLanguage();
219 }
220 return lldb::eLanguageTypeUnknown;
221 }
222
GetIsOptimized()223 bool SBFunction::GetIsOptimized() {
224 LLDB_INSTRUMENT_VA(this);
225
226 if (m_opaque_ptr) {
227 if (m_opaque_ptr->GetCompileUnit())
228 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
229 }
230 return false;
231 }
232