[lldb][dwarf] Compute fully qualified names on simplified template names with DWARFT...
[llvm-project.git] / lldb / source / API / SBFunction.cpp
blobac61220ec8736ace425efaa1038e6a336e59878c
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/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"
23 using namespace lldb;
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;
40 return *this;
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);
58 if (m_opaque_ptr)
59 return m_opaque_ptr->GetName().AsCString();
61 return nullptr;
64 const char *SBFunction::GetDisplayName() const {
65 LLDB_INSTRUMENT_VA(this);
67 if (m_opaque_ptr)
68 return m_opaque_ptr->GetMangled().GetDisplayDemangledName().AsCString();
70 return nullptr;
73 const char *SBFunction::GetMangledName() const {
74 LLDB_INSTRUMENT_VA(this);
76 if (m_opaque_ptr)
77 return m_opaque_ptr->GetMangled().GetMangledName().AsCString();
78 return nullptr;
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);
96 if (m_opaque_ptr) {
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();
100 if (func_type)
101 s.Printf(", type = %s", func_type->GetName().AsCString());
102 return true;
104 s.Printf("No value");
105 return false;
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;
119 if (m_opaque_ptr) {
120 TargetSP target_sp(target.GetSP());
121 std::unique_lock<std::recursive_mutex> lock;
122 ModuleSP module_sp(
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);
145 SBAddress addr;
146 if (m_opaque_ptr)
147 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
148 return addr;
151 SBAddress SBFunction::GetEndAddress() {
152 LLDB_INSTRUMENT_VA(this);
154 SBAddress addr;
155 if (m_opaque_ptr) {
156 addr_t byte_size = m_opaque_ptr->GetAddressRange().GetByteSize();
157 if (byte_size > 0) {
158 addr.SetAddress(m_opaque_ptr->GetAddressRange().GetBaseAddress());
159 addr->Slide(byte_size);
162 return addr;
165 lldb::SBAddressRangeList SBFunction::GetRanges() {
166 LLDB_INSTRUMENT_VA(this);
168 lldb::SBAddressRangeList ranges;
169 if (m_opaque_ptr) {
170 lldb::SBAddressRange range;
171 (*range.m_opaque_up) = m_opaque_ptr->GetAddressRange();
172 ranges.Append(std::move(range));
175 return ranges;
178 const char *SBFunction::GetArgumentName(uint32_t arg_idx) {
179 LLDB_INSTRUMENT_VA(this, arg_idx);
181 if (!m_opaque_ptr)
182 return nullptr;
184 Block &block = m_opaque_ptr->GetBlock(true);
185 VariableListSP variable_list_sp = block.GetBlockVariableList(true);
186 if (!variable_list_sp)
187 return nullptr;
189 VariableList arguments;
190 variable_list_sp->AppendVariablesWithScope(eValueTypeVariableArgument,
191 arguments, true);
192 lldb::VariableSP variable_sp = arguments.GetVariableAtIndex(arg_idx);
193 if (!variable_sp)
194 return nullptr;
196 return variable_sp->GetName().GetCString();
199 uint32_t SBFunction::GetPrologueByteSize() {
200 LLDB_INSTRUMENT_VA(this);
202 if (m_opaque_ptr)
203 return m_opaque_ptr->GetPrologueByteSize();
204 return 0;
207 SBType SBFunction::GetType() {
208 LLDB_INSTRUMENT_VA(this);
210 SBType sb_type;
211 if (m_opaque_ptr) {
212 Type *function_type = m_opaque_ptr->GetType();
213 if (function_type)
214 sb_type.ref().SetType(function_type->shared_from_this());
216 return sb_type;
219 SBBlock SBFunction::GetBlock() {
220 LLDB_INSTRUMENT_VA(this);
222 SBBlock sb_block;
223 if (m_opaque_ptr)
224 sb_block.SetPtr(&m_opaque_ptr->GetBlock(true));
225 return sb_block;
228 lldb::LanguageType SBFunction::GetLanguage() {
229 LLDB_INSTRUMENT_VA(this);
231 if (m_opaque_ptr) {
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);
241 if (m_opaque_ptr) {
242 if (m_opaque_ptr->GetCompileUnit())
243 return m_opaque_ptr->GetCompileUnit()->GetIsOptimized();
245 return false;