LLDB mainline
SBFunction.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
8
11#include "lldb/API/SBProcess.h"
12#include "lldb/API/SBStream.h"
15#include "lldb/Core/Module.h"
18#include "lldb/Symbol/Type.h"
21#include "lldb/Target/Target.h"
23
24using namespace lldb;
25using namespace lldb_private;
26
28
30 : m_opaque_ptr(lldb_object_ptr) {}
31
36
38 LLDB_INSTRUMENT_VA(this, rhs);
39
41 return *this;
42}
43
45
46bool SBFunction::IsValid() const {
48 return this->operator bool();
49}
50SBFunction::operator bool() const {
52
53 return m_opaque_ptr != nullptr;
54}
55
56const char *SBFunction::GetName() const {
58
59 if (m_opaque_ptr)
60 return m_opaque_ptr->GetName().AsCString(nullptr);
61
62 return nullptr;
63}
64
65const char *SBFunction::GetDisplayName() const {
67
68 if (m_opaque_ptr)
69 return m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString(
70 nullptr);
71
72 return nullptr;
73}
74
75const char *SBFunction::GetMangledName() const {
77
78 if (m_opaque_ptr)
79 return m_opaque_ptr->GetMangled().GetMangledName().AsCString(nullptr);
80 return nullptr;
81}
82
83const char *SBFunction::GetBaseName() const {
85
86 if (!m_opaque_ptr)
87 return nullptr;
88
89 return m_opaque_ptr->GetMangled().GetBaseName().AsCString(nullptr);
90}
91
92bool SBFunction::operator==(const SBFunction &rhs) const {
93 LLDB_INSTRUMENT_VA(this, rhs);
94
95 return m_opaque_ptr == rhs.m_opaque_ptr;
96}
97
98bool SBFunction::operator!=(const SBFunction &rhs) const {
99 LLDB_INSTRUMENT_VA(this, rhs);
100
101 return m_opaque_ptr != rhs.m_opaque_ptr;
102}
103
105 LLDB_INSTRUMENT_VA(this, s);
106
107 if (m_opaque_ptr) {
108 s.Printf("SBFunction: id = 0x%8.8" PRIx64 ", name = %s",
109 m_opaque_ptr->GetID(), m_opaque_ptr->GetName().AsCString(""));
110 Type *func_type = m_opaque_ptr->GetType();
111 if (func_type)
112 s.Printf(", type = %s", func_type->GetName().AsCString(""));
113 return true;
114 }
115 s.Printf("No value");
116 return false;
117}
118
120 LLDB_INSTRUMENT_VA(this, target);
121
122 return GetInstructions(target, nullptr);
123}
124
126 const char *flavor) {
127 LLDB_INSTRUMENT_VA(this, target, flavor);
128
129 SBInstructionList sb_instructions;
130 if (m_opaque_ptr) {
131 TargetSP target_sp(target.GetSP());
132 TargetAPIMutex api_lock;
133 std::unique_lock<TargetAPIMutex> guard;
134 ModuleSP module_sp(m_opaque_ptr->GetAddress().GetModule());
135 if (target_sp && module_sp) {
136 api_lock = TargetAPIMutex(target_sp->GetAPIMutex());
137 guard = std::unique_lock<TargetAPIMutex>(api_lock);
138 const bool force_live_memory = true;
140 module_sp->GetArchitecture(), nullptr, flavor,
141 target_sp->GetDisassemblyCPU(), target_sp->GetDisassemblyFeatures(),
142 *target_sp, m_opaque_ptr->GetAddressRanges(), force_live_memory));
143 }
144 }
145 return sb_instructions;
146}
147
149
151 m_opaque_ptr = lldb_object_ptr;
152}
153
155 LLDB_INSTRUMENT_VA(this);
156
157 SBAddress addr;
158 if (m_opaque_ptr)
159 addr.SetAddress(m_opaque_ptr->GetAddress());
160 return addr;
161}
162
163SBAddress SBFunction::GetEndAddress() {
164 LLDB_INSTRUMENT_VA(this);
165
166 SBAddress addr;
167 if (m_opaque_ptr) {
169 if (!ranges.empty()) {
170 // Return the end of the first range, use GetRanges to get all ranges.
171 addr.SetAddress(ranges.front().GetBaseAddress());
172 addr->Slide(ranges.front().GetByteSize());
173 }
174 }
175 return addr;
176}
177
179 LLDB_INSTRUMENT_VA(this);
180
182 if (m_opaque_ptr)
183 ranges.ref() = AddressRangeListImpl(m_opaque_ptr->GetAddressRanges());
184
185 return ranges;
186}
187
188const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
189 LLDB_INSTRUMENT_VA(this, arg_idx);
190
191 if (!m_opaque_ptr)
192 return nullptr;
193
194 Block &block = m_opaque_ptr->GetBlock(true);
195 VariableListSP variable_list_sp = block.GetBlockVariableList(true);
196 if (!variable_list_sp)
197 return nullptr;
198
199 VariableList arguments;
200 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
201 arguments, true);
202 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
203 if (!variable_sp)
204 return nullptr;
205
206 return variable_sp->GetName().GetCString();
207}
208
210 LLDB_INSTRUMENT_VA(this);
211
212 if (m_opaque_ptr)
213 return m_opaque_ptr->GetPrologueByteSize();
214 return 0;
215}
216
218 LLDB_INSTRUMENT_VA(this);
219
220 SBType sb_type;
221 if (m_opaque_ptr) {
222 Type *function_type = m_opaque_ptr->GetType();
223 if (function_type)
224 sb_type.ref().SetType(function_type->shared_from_this());
225 }
226 return sb_type;
227}
228
230 LLDB_INSTRUMENT_VA(this);
231
232 SBBlock sb_block;
233 if (m_opaque_ptr)
234 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
235 return sb_block;
236}
237
239 LLDB_INSTRUMENT_VA(this);
240
241 if (m_opaque_ptr) {
242 if (m_opaque_ptr->GetCompileUnit())
243 return m_opaque_ptr->GetCompileUnit()->GetLanguage();
244 }
246}
247
249 LLDB_INSTRUMENT_VA(this);
250
251 if (m_opaque_ptr) {
252 if (m_opaque_ptr->GetCompileUnit())
253 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
254 }
255 return false;
256}
#define LLDB_INSTRUMENT_VA(...)
lldb_private::AddressRangeListImpl & ref() const
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition SBAddress.cpp:88
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition SBBlock.cpp:178
uint32_t GetPrologueByteSize()
const char * GetBaseName() const
const char * GetArgumentName(uint32_t arg_idx)
const char * GetMangledName() const
lldb_private::Function * m_opaque_ptr
Definition SBFunction.h:84
bool GetDescription(lldb::SBStream &description)
lldb::SBAddress GetStartAddress()
const lldb::SBFunction & operator=(const lldb::SBFunction &rhs)
friend class SBAddress
Definition SBFunction.h:78
bool IsValid() const
const char * GetDisplayName() const
lldb::SBBlock GetBlock()
lldb_private::Function * get()
lldb::SBInstructionList GetInstructions(lldb::SBTarget target)
bool operator!=(const lldb::SBFunction &rhs) const
void reset(lldb_private::Function *lldb_object_ptr)
bool operator==(const lldb::SBFunction &rhs) const
const char * GetName() const
LLDB_DEPRECATED_FIXME("Not compatible with discontinuous functions.", "GetRanges()") lldb lldb::SBAddressRangeList GetRanges()
lldb::LanguageType GetLanguage()
lldb::SBType GetType()
void SetDisassembler(const lldb::DisassemblerSP &opaque_sp)
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:600
lldb_private::TypeImpl & ref()
Definition SBType.cpp:99
bool Slide(int64_t offset)
Definition Address.h:446
A class that describes a single lexical block.
Definition Block.h:41
lldb::VariableListSP GetBlockVariableList(bool can_create)
Get the variable list for this block only.
Definition Block.cpp:382
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
static lldb::DisassemblerSP DisassembleRange(const ArchSpec &arch, const char *plugin_name, const char *flavor, const char *cpu, const char *features, Target &target, llvm::ArrayRef< AddressRange > disasm_ranges, bool force_live_memory=false)
A class that describes a function.
Definition Function.h:377
AddressRanges GetAddressRanges()
Definition Function.h:425
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
void SetType(const lldb::TypeSP &type_sp)
Definition Type.cpp:973
ConstString GetName()
Definition Type.cpp:442
lldb::VariableSP GetVariableAtIndex(size_t idx) const
A class that represents a running process on the host machine.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::VariableList > VariableListSP
std::shared_ptr< lldb_private::Variable > VariableSP
@ eValueTypeVariableArgument
function argument variables
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP