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);
57 const char *cstr = nullptr;
59 cstr = m_opaque_ptr->GetName().AsCString();
64 const char *SBFunction::GetDisplayName() const {
65 LLDB_INSTRUMENT_VA(this);
67 const char *cstr = nullptr;
69 cstr = m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
74 const char *SBFunction::GetMangledName() const {
75 LLDB_INSTRUMENT_VA(this);
77 const char *cstr = nullptr;
79 cstr = m_opaque_ptr->GetMangled().GetMangledName().AsCString();
83 bool SBFunction::operator==(const SBFunction &rhs) const {
84 LLDB_INSTRUMENT_VA(this, rhs);
86 return m_opaque_ptr == rhs.m_opaque_ptr;
89 bool SBFunction::operator!=(const SBFunction &rhs) const {
90 LLDB_INSTRUMENT_VA(this, rhs);
92 return m_opaque_ptr != rhs.m_opaque_ptr;
95 bool SBFunction::GetDescription(SBStream &s) {
96 LLDB_INSTRUMENT_VA(this, s);
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();
103 s.Printf(", type = %s", func_type->GetName().AsCString());
106 s.Printf("No value");
110 SBInstructionList SBFunction::GetInstructions(SBTarget target) {
111 LLDB_INSTRUMENT_VA(this, target);
113 return GetInstructions(target, nullptr);
116 SBInstructionList SBFunction::GetInstructions(SBTarget target,
117 const char *flavor) {
118 LLDB_INSTRUMENT_VA(this, target, flavor);
120 SBInstructionList sb_instructions;
122 TargetSP target_sp(target.GetSP());
123 std::unique_lock<std::recursive_mutex> lock;
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));
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 addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
159 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
160 addr->Slide(byte_size);
166 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
167 LLDB_INSTRUMENT_VA(this, arg_idx);
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,
176 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
178 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();