[RISCV] Reduce redundancy in vnsrl tests
[llvm-project.git] / lldb / source / API / SBFunction.cpp
blob3f6b4eea983187d9498121523d44e1e25f8f10d6
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 //===----------------------------------------------------------------------===//
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"
24 using namespace lldb;
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;
41 return *this;
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);
59 if (m_opaque_ptr)
60 return m_opaque_ptr->GetName().AsCString();
62 return nullptr;
65 const char *SBFunction::GetDisplayName() const {
66 LLDB_INSTRUMENT_VA(this);
68 if (m_opaque_ptr)
69 return m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
71 return nullptr;
74 const char *SBFunction::GetMangledName() const {
75 LLDB_INSTRUMENT_VA(this);
77 if (m_opaque_ptr)
78 return m_opaque_ptr->GetMangled().GetMangledName().AsCString();
79 return nullptr;
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);
97 if (m_opaque_ptr) {
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();
101 if (func_type)
102 s.Printf(", type = %s", func_type->GetName().AsCString());
103 return true;
105 s.Printf("No value");
106 return false;
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;
120 if (m_opaque_ptr) {
121 TargetSP target_sp(target.GetSP());
122 std::unique_lock<std::recursive_mutex> lock;
123 ModuleSP module_sp(
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);
146 SBAddress addr;
147 if (m_opaque_ptr)
148 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
149 return addr;
152 SBAddress SBFunction::GetEndAddress() {
153 LLDB_INSTRUMENT_VA(this);
155 SBAddress addr;
156 if (m_opaque_ptr) {
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());
164 return addr;
167 lldb::SBAddressRangeList SBFunction::GetRanges() {
168 LLDB_INSTRUMENT_VA(this);
170 lldb::SBAddressRangeList ranges;
171 if (m_opaque_ptr)
172 ranges.ref() = AddressRangeListImpl(m_opaque_ptr->GetAddressRanges());
174 return ranges;
177 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
178 LLDB_INSTRUMENT_VA(this, arg_idx);
180 if (!m_opaque_ptr)
181 return nullptr;
183 Block &block = m_opaque_ptr->GetBlock(true);
184 VariableListSP variable_list_sp = block.GetBlockVariableList(true);
185 if (!variable_list_sp)
186 return nullptr;
188 VariableList arguments;
189 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
190 arguments, true);
191 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
192 if (!variable_sp)
193 return nullptr;
195 return variable_sp->GetName().GetCString();
198 uint32_t SBFunction::GetPrologueByteSize() {
199 LLDB_INSTRUMENT_VA(this);
201 if (m_opaque_ptr)
202 return m_opaque_ptr->GetPrologueByteSize();
203 return 0;
206 SBType SBFunction::GetType() {
207 LLDB_INSTRUMENT_VA(this);
209 SBType sb_type;
210 if (m_opaque_ptr) {
211 Type *function_type = m_opaque_ptr->GetType();
212 if (function_type)
213 sb_type.ref().SetType(function_type->shared_from_this());
215 return sb_type;
218 SBBlock SBFunction::GetBlock() {
219 LLDB_INSTRUMENT_VA(this);
221 SBBlock sb_block;
222 if (m_opaque_ptr)
223 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
224 return sb_block;
227 lldb::LanguageType SBFunction::GetLanguage() {
228 LLDB_INSTRUMENT_VA(this);
230 if (m_opaque_ptr) {
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);
240 if (m_opaque_ptr) {
241 if (m_opaque_ptr->GetCompileUnit())
242 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
244 return false;