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/Disassembler.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Symbol/CompileUnit.h"
16 #include "lldb/Symbol/Function.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/VariableList.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/Target.h"
21 #include "lldb/Utility/Instrumentation.h"
24 using namespace lldb_private
;
26 SBFunction::SBFunction() { LLDB_INSTRUMENT_VA(this); }
28 SBFunction::SBFunction(lldb_private::Function
*lldb_object_ptr
)
29 : m_opaque_ptr(lldb_object_ptr
) {}
31 SBFunction::SBFunction(const lldb::SBFunction
&rhs
)
32 : m_opaque_ptr(rhs
.m_opaque_ptr
) {
33 LLDB_INSTRUMENT_VA(this, rhs
);
36 const SBFunction
&SBFunction::operator=(const SBFunction
&rhs
) {
37 LLDB_INSTRUMENT_VA(this, rhs
);
39 m_opaque_ptr
= rhs
.m_opaque_ptr
;
43 SBFunction::~SBFunction() { m_opaque_ptr
= nullptr; }
45 bool SBFunction::IsValid() const {
46 LLDB_INSTRUMENT_VA(this);
47 return this->operator bool();
49 SBFunction::operator bool() const {
50 LLDB_INSTRUMENT_VA(this);
52 return m_opaque_ptr
!= nullptr;
55 const char *SBFunction::GetName() const {
56 LLDB_INSTRUMENT_VA(this);
59 return m_opaque_ptr
->GetName().AsCString();
64 const char *SBFunction::GetDisplayName() const {
65 LLDB_INSTRUMENT_VA(this);
68 return m_opaque_ptr
->GetMangled().GetDisplayDemangledName().AsCString();
73 const char *SBFunction::GetMangledName() const {
74 LLDB_INSTRUMENT_VA(this);
77 return m_opaque_ptr
->GetMangled().GetMangledName().AsCString();
81 bool SBFunction::operator==(const SBFunction
&rhs
) const {
82 LLDB_INSTRUMENT_VA(this, rhs
);
84 return m_opaque_ptr
== rhs
.m_opaque_ptr
;
87 bool SBFunction::operator!=(const SBFunction
&rhs
) const {
88 LLDB_INSTRUMENT_VA(this, rhs
);
90 return m_opaque_ptr
!= rhs
.m_opaque_ptr
;
93 bool SBFunction::GetDescription(SBStream
&s
) {
94 LLDB_INSTRUMENT_VA(this, s
);
97 s
.Printf("SBFunction: id = 0x%8.8" PRIx64
", name = %s",
98 m_opaque_ptr
->GetID(), m_opaque_ptr
->GetName().AsCString());
99 Type
*func_type
= m_opaque_ptr
->GetType();
101 s
.Printf(", type = %s", func_type
->GetName().AsCString());
104 s
.Printf("No value");
108 SBInstructionList
SBFunction::GetInstructions(SBTarget target
) {
109 LLDB_INSTRUMENT_VA(this, target
);
111 return GetInstructions(target
, nullptr);
114 SBInstructionList
SBFunction::GetInstructions(SBTarget target
,
115 const char *flavor
) {
116 LLDB_INSTRUMENT_VA(this, target
, flavor
);
118 SBInstructionList sb_instructions
;
120 TargetSP
target_sp(target
.GetSP());
121 std::unique_lock
<std::recursive_mutex
> lock
;
123 m_opaque_ptr
->GetAddressRange().GetBaseAddress().GetModule());
124 if (target_sp
&& module_sp
) {
125 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
126 const bool force_live_memory
= true;
127 sb_instructions
.SetDisassembler(Disassembler::DisassembleRange(
128 module_sp
->GetArchitecture(), nullptr, flavor
,
129 target_sp
->GetDisassemblyCPU(), target_sp
->GetDisassemblyFeatures(),
130 *target_sp
, m_opaque_ptr
->GetAddressRange(), force_live_memory
));
133 return sb_instructions
;
136 lldb_private::Function
*SBFunction::get() { return m_opaque_ptr
; }
138 void SBFunction::reset(lldb_private::Function
*lldb_object_ptr
) {
139 m_opaque_ptr
= lldb_object_ptr
;
142 SBAddress
SBFunction::GetStartAddress() {
143 LLDB_INSTRUMENT_VA(this);
147 addr
.SetAddress(m_opaque_ptr
->GetAddressRange().GetBaseAddress());
151 SBAddress
SBFunction::GetEndAddress() {
152 LLDB_INSTRUMENT_VA(this);
156 addr_t byte_size
= m_opaque_ptr
->GetAddressRange().GetByteSize();
158 addr
.SetAddress(m_opaque_ptr
->GetAddressRange().GetBaseAddress());
159 addr
->Slide(byte_size
);
165 lldb::SBAddressRangeList
SBFunction::GetRanges() {
166 LLDB_INSTRUMENT_VA(this);
168 lldb::SBAddressRangeList ranges
;
170 lldb::SBAddressRange range
;
171 (*range
.m_opaque_up
) = m_opaque_ptr
->GetAddressRange();
172 ranges
.Append(std::move(range
));
178 const char *SBFunction::GetArgumentName(uint32_t arg_idx
) {
179 LLDB_INSTRUMENT_VA(this, arg_idx
);
184 Block
&block
= m_opaque_ptr
->GetBlock(true);
185 VariableListSP variable_list_sp
= block
.GetBlockVariableList(true);
186 if (!variable_list_sp
)
189 VariableList arguments
;
190 variable_list_sp
->AppendVariablesWithScope(eValueTypeVariableArgument
,
192 lldb::VariableSP variable_sp
= arguments
.GetVariableAtIndex(arg_idx
);
196 return variable_sp
->GetName().GetCString();
199 uint32_t SBFunction::GetPrologueByteSize() {
200 LLDB_INSTRUMENT_VA(this);
203 return m_opaque_ptr
->GetPrologueByteSize();
207 SBType
SBFunction::GetType() {
208 LLDB_INSTRUMENT_VA(this);
212 Type
*function_type
= m_opaque_ptr
->GetType();
214 sb_type
.ref().SetType(function_type
->shared_from_this());
219 SBBlock
SBFunction::GetBlock() {
220 LLDB_INSTRUMENT_VA(this);
224 sb_block
.SetPtr(&m_opaque_ptr
->GetBlock(true));
228 lldb::LanguageType
SBFunction::GetLanguage() {
229 LLDB_INSTRUMENT_VA(this);
232 if (m_opaque_ptr
->GetCompileUnit())
233 return m_opaque_ptr
->GetCompileUnit()->GetLanguage();
235 return lldb::eLanguageTypeUnknown
;
238 bool SBFunction::GetIsOptimized() {
239 LLDB_INSTRUMENT_VA(this);
242 if (m_opaque_ptr
->GetCompileUnit())
243 return m_opaque_ptr
->GetCompileUnit()->GetIsOptimized();