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/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"
23 using namespace lldb_private
;
25 SBFunction::SBFunction() { LLDB_INSTRUMENT_VA(this); }
27 SBFunction::SBFunction(lldb_private::Function
*lldb_object_ptr
)
28 : m_opaque_ptr(lldb_object_ptr
) {}
30 SBFunction::SBFunction(const lldb::SBFunction
&rhs
)
31 : m_opaque_ptr(rhs
.m_opaque_ptr
) {
32 LLDB_INSTRUMENT_VA(this, rhs
);
35 const SBFunction
&SBFunction::operator=(const SBFunction
&rhs
) {
36 LLDB_INSTRUMENT_VA(this, rhs
);
38 m_opaque_ptr
= rhs
.m_opaque_ptr
;
42 SBFunction::~SBFunction() { m_opaque_ptr
= nullptr; }
44 bool SBFunction::IsValid() const {
45 LLDB_INSTRUMENT_VA(this);
46 return this->operator bool();
48 SBFunction::operator bool() const {
49 LLDB_INSTRUMENT_VA(this);
51 return m_opaque_ptr
!= nullptr;
54 const char *SBFunction::GetName() const {
55 LLDB_INSTRUMENT_VA(this);
58 return m_opaque_ptr
->GetName().AsCString();
63 const char *SBFunction::GetDisplayName() const {
64 LLDB_INSTRUMENT_VA(this);
67 return m_opaque_ptr
->GetMangled().GetDisplayDemangledName().AsCString();
72 const char *SBFunction::GetMangledName() const {
73 LLDB_INSTRUMENT_VA(this);
76 return m_opaque_ptr
->GetMangled().GetMangledName().AsCString();
80 bool SBFunction::operator==(const SBFunction
&rhs
) const {
81 LLDB_INSTRUMENT_VA(this, rhs
);
83 return m_opaque_ptr
== rhs
.m_opaque_ptr
;
86 bool SBFunction::operator!=(const SBFunction
&rhs
) const {
87 LLDB_INSTRUMENT_VA(this, rhs
);
89 return m_opaque_ptr
!= rhs
.m_opaque_ptr
;
92 bool SBFunction::GetDescription(SBStream
&s
) {
93 LLDB_INSTRUMENT_VA(this, s
);
96 s
.Printf("SBFunction: id = 0x%8.8" PRIx64
", name = %s",
97 m_opaque_ptr
->GetID(), m_opaque_ptr
->GetName().AsCString());
98 Type
*func_type
= m_opaque_ptr
->GetType();
100 s
.Printf(", type = %s", func_type
->GetName().AsCString());
103 s
.Printf("No value");
107 SBInstructionList
SBFunction::GetInstructions(SBTarget target
) {
108 LLDB_INSTRUMENT_VA(this, target
);
110 return GetInstructions(target
, nullptr);
113 SBInstructionList
SBFunction::GetInstructions(SBTarget target
,
114 const char *flavor
) {
115 LLDB_INSTRUMENT_VA(this, target
, flavor
);
117 SBInstructionList sb_instructions
;
119 TargetSP
target_sp(target
.GetSP());
120 std::unique_lock
<std::recursive_mutex
> lock
;
122 m_opaque_ptr
->GetAddressRange().GetBaseAddress().GetModule());
123 if (target_sp
&& module_sp
) {
124 lock
= std::unique_lock
<std::recursive_mutex
>(target_sp
->GetAPIMutex());
125 const bool force_live_memory
= true;
126 sb_instructions
.SetDisassembler(Disassembler::DisassembleRange(
127 module_sp
->GetArchitecture(), nullptr, flavor
, *target_sp
,
128 m_opaque_ptr
->GetAddressRange(), force_live_memory
));
131 return sb_instructions
;
134 lldb_private::Function
*SBFunction::get() { return m_opaque_ptr
; }
136 void SBFunction::reset(lldb_private::Function
*lldb_object_ptr
) {
137 m_opaque_ptr
= lldb_object_ptr
;
140 SBAddress
SBFunction::GetStartAddress() {
141 LLDB_INSTRUMENT_VA(this);
145 addr
.SetAddress(m_opaque_ptr
->GetAddressRange().GetBaseAddress());
149 SBAddress
SBFunction::GetEndAddress() {
150 LLDB_INSTRUMENT_VA(this);
154 addr_t byte_size
= m_opaque_ptr
->GetAddressRange().GetByteSize();
156 addr
.SetAddress(m_opaque_ptr
->GetAddressRange().GetBaseAddress());
157 addr
->Slide(byte_size
);
163 const char *SBFunction::GetArgumentName(uint32_t arg_idx
) {
164 LLDB_INSTRUMENT_VA(this, arg_idx
);
169 Block
&block
= m_opaque_ptr
->GetBlock(true);
170 VariableListSP variable_list_sp
= block
.GetBlockVariableList(true);
171 if (!variable_list_sp
)
174 VariableList arguments
;
175 variable_list_sp
->AppendVariablesWithScope(eValueTypeVariableArgument
,
177 lldb::VariableSP variable_sp
= arguments
.GetVariableAtIndex(arg_idx
);
181 return variable_sp
->GetName().GetCString();
184 uint32_t SBFunction::GetPrologueByteSize() {
185 LLDB_INSTRUMENT_VA(this);
188 return m_opaque_ptr
->GetPrologueByteSize();
192 SBType
SBFunction::GetType() {
193 LLDB_INSTRUMENT_VA(this);
197 Type
*function_type
= m_opaque_ptr
->GetType();
199 sb_type
.ref().SetType(function_type
->shared_from_this());
204 SBBlock
SBFunction::GetBlock() {
205 LLDB_INSTRUMENT_VA(this);
209 sb_block
.SetPtr(&m_opaque_ptr
->GetBlock(true));
213 lldb::LanguageType
SBFunction::GetLanguage() {
214 LLDB_INSTRUMENT_VA(this);
217 if (m_opaque_ptr
->GetCompileUnit())
218 return m_opaque_ptr
->GetCompileUnit()->GetLanguage();
220 return lldb::eLanguageTypeUnknown
;
223 bool SBFunction::GetIsOptimized() {
224 LLDB_INSTRUMENT_VA(this);
227 if (m_opaque_ptr
->GetCompileUnit())
228 return m_opaque_ptr
->GetCompileUnit()->GetIsOptimized();