1 //===-- SBFunction.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBFunction.h"
10 #include "lldb/API/SBAddressRange.h"
11 #include "lldb/API/SBProcess.h"
12 #include "lldb/API/SBStream.h"
13 #include "lldb/Core/AddressRangeListImpl.h"
14 #include "lldb/Core/Disassembler.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/CompileUnit.h"
17 #include "lldb/Symbol/Function.h"
18 #include "lldb/Symbol/Type.h"
19 #include "lldb/Symbol/VariableList.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/Target.h"
22 #include "lldb/Utility/Instrumentation.h"
25 using namespace lldb_private
;
27 SBFunction::SBFunction() { LLDB_INSTRUMENT_VA(this); }
29 SBFunction::SBFunction(lldb_private::Function
*lldb_object_ptr
)
30 : m_opaque_ptr(lldb_object_ptr
) {}
32 SBFunction::SBFunction(const lldb::SBFunction
&rhs
)
33 : m_opaque_ptr(rhs
.m_opaque_ptr
) {
34 LLDB_INSTRUMENT_VA(this, rhs
);
37 const SBFunction
&SBFunction::operator=(const SBFunction
&rhs
) {
38 LLDB_INSTRUMENT_VA(this, rhs
);
40 m_opaque_ptr
= rhs
.m_opaque_ptr
;
44 SBFunction::~SBFunction() { m_opaque_ptr
= nullptr; }
46 bool SBFunction::IsValid() const {
47 LLDB_INSTRUMENT_VA(this);
48 return this->operator bool();
50 SBFunction::operator bool() const {
51 LLDB_INSTRUMENT_VA(this);
53 return m_opaque_ptr
!= nullptr;
56 const char *SBFunction::GetName() const {
57 LLDB_INSTRUMENT_VA(this);
60 return m_opaque_ptr
->GetName().AsCString();
65 const char *SBFunction::GetDisplayName() const {
66 LLDB_INSTRUMENT_VA(this);
69 return m_opaque_ptr
->GetMangled().GetDisplayDemangledName().AsCString();
74 const char *SBFunction::GetMangledName() const {
75 LLDB_INSTRUMENT_VA(this);
78 return m_opaque_ptr
->GetMangled().GetMangledName().AsCString();
82 bool SBFunction::operator==(const SBFunction
&rhs
) const {
83 LLDB_INSTRUMENT_VA(this, rhs
);
85 return m_opaque_ptr
== rhs
.m_opaque_ptr
;
88 bool SBFunction::operator!=(const SBFunction
&rhs
) const {
89 LLDB_INSTRUMENT_VA(this, rhs
);
91 return m_opaque_ptr
!= rhs
.m_opaque_ptr
;
94 bool SBFunction::GetDescription(SBStream
&s
) {
95 LLDB_INSTRUMENT_VA(this, s
);
98 s
.Printf("SBFunction: id = 0x%8.8" PRIx64
", name = %s",
99 m_opaque_ptr
->GetID(), m_opaque_ptr
->GetName().AsCString());
100 Type
*func_type
= m_opaque_ptr
->GetType();
102 s
.Printf(", type = %s", func_type
->GetName().AsCString());
105 s
.Printf("No value");
109 SBInstructionList
SBFunction::GetInstructions(SBTarget target
) {
110 LLDB_INSTRUMENT_VA(this, target
);
112 return GetInstructions(target
, nullptr);
115 SBInstructionList
SBFunction::GetInstructions(SBTarget target
,
116 const char *flavor
) {
117 LLDB_INSTRUMENT_VA(this, target
, flavor
);
119 SBInstructionList sb_instructions
;
121 TargetSP
target_sp(target
.GetSP());
122 std::unique_lock
<std::recursive_mutex
> lock
;
124 m_opaque_ptr
->GetAddressRange().GetBaseAddress().GetModule());
125 if (target_sp
&& module_sp
) {
126 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
127 const bool force_live_memory
= true;
128 sb_instructions
.SetDisassembler(Disassembler::DisassembleRange(
129 module_sp
->GetArchitecture(), nullptr, flavor
,
130 target_sp
->GetDisassemblyCPU(), target_sp
->GetDisassemblyFeatures(),
131 *target_sp
, m_opaque_ptr
->GetAddressRange(), force_live_memory
));
134 return sb_instructions
;
137 lldb_private::Function
*SBFunction::get() { return m_opaque_ptr
; }
139 void SBFunction::reset(lldb_private::Function
*lldb_object_ptr
) {
140 m_opaque_ptr
= lldb_object_ptr
;
143 SBAddress
SBFunction::GetStartAddress() {
144 LLDB_INSTRUMENT_VA(this);
148 addr
.SetAddress(m_opaque_ptr
->GetAddressRange().GetBaseAddress());
152 SBAddress
SBFunction::GetEndAddress() {
153 LLDB_INSTRUMENT_VA(this);
157 AddressRanges ranges
= m_opaque_ptr
->GetAddressRanges();
158 if (!ranges
.empty()) {
159 // Return the end of the first range, use GetRanges to get all ranges.
160 addr
.SetAddress(ranges
.front().GetBaseAddress());
161 addr
->Slide(ranges
.front().GetByteSize());
167 lldb::SBAddressRangeList
SBFunction::GetRanges() {
168 LLDB_INSTRUMENT_VA(this);
170 lldb::SBAddressRangeList ranges
;
172 ranges
.ref() = AddressRangeListImpl(m_opaque_ptr
->GetAddressRanges());
177 const char *SBFunction::GetArgumentName(uint32_t arg_idx
) {
178 LLDB_INSTRUMENT_VA(this, arg_idx
);
183 Block
&block
= m_opaque_ptr
->GetBlock(true);
184 VariableListSP variable_list_sp
= block
.GetBlockVariableList(true);
185 if (!variable_list_sp
)
188 VariableList arguments
;
189 variable_list_sp
->AppendVariablesWithScope(eValueTypeVariableArgument
,
191 lldb::VariableSP variable_sp
= arguments
.GetVariableAtIndex(arg_idx
);
195 return variable_sp
->GetName().GetCString();
198 uint32_t SBFunction::GetPrologueByteSize() {
199 LLDB_INSTRUMENT_VA(this);
202 return m_opaque_ptr
->GetPrologueByteSize();
206 SBType
SBFunction::GetType() {
207 LLDB_INSTRUMENT_VA(this);
211 Type
*function_type
= m_opaque_ptr
->GetType();
213 sb_type
.ref().SetType(function_type
->shared_from_this());
218 SBBlock
SBFunction::GetBlock() {
219 LLDB_INSTRUMENT_VA(this);
223 sb_block
.SetPtr(&m_opaque_ptr
->GetBlock(true));
227 lldb::LanguageType
SBFunction::GetLanguage() {
228 LLDB_INSTRUMENT_VA(this);
231 if (m_opaque_ptr
->GetCompileUnit())
232 return m_opaque_ptr
->GetCompileUnit()->GetLanguage();
234 return lldb::eLanguageTypeUnknown
;
237 bool SBFunction::GetIsOptimized() {
238 LLDB_INSTRUMENT_VA(this);
241 if (m_opaque_ptr
->GetCompileUnit())
242 return m_opaque_ptr
->GetCompileUnit()->GetIsOptimized();