[DAG] Fix typo in VSELECT SimplifyDemandedVectorElts handling. NFC.
[llvm-project.git] / lldb / source / API / SBAddress.cpp
blobe519f0bcc83c64b87126cd06232cefadc28e6c61
1 //===-- SBAddress.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/SBAddress.h"
10 #include "Utils.h"
11 #include "lldb/API/SBProcess.h"
12 #include "lldb/API/SBSection.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Symbol/LineEntry.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/Instrumentation.h"
19 #include "lldb/Utility/StreamString.h"
21 using namespace lldb;
22 using namespace lldb_private;
24 SBAddress::SBAddress() : m_opaque_up(new Address()) {
25 LLDB_INSTRUMENT_VA(this);
28 SBAddress::SBAddress(const Address &address)
29 : m_opaque_up(std::make_unique<Address>(address)) {}
31 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
32 LLDB_INSTRUMENT_VA(this, rhs);
34 m_opaque_up = clone(rhs.m_opaque_up);
37 SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
38 : m_opaque_up(new Address(section.GetSP(), offset)) {
39 LLDB_INSTRUMENT_VA(this, section, offset);
42 // Create an address by resolving a load address using the supplied target
43 SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
44 : m_opaque_up(new Address()) {
45 LLDB_INSTRUMENT_VA(this, load_addr, target);
47 SetLoadAddress(load_addr, target);
50 SBAddress::~SBAddress() = default;
52 const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
53 LLDB_INSTRUMENT_VA(this, rhs);
55 if (this != &rhs)
56 m_opaque_up = clone(rhs.m_opaque_up);
57 return *this;
60 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
61 if (lhs.IsValid() && rhs.IsValid())
62 return lhs.ref() == rhs.ref();
63 return false;
66 bool SBAddress::operator!=(const SBAddress &rhs) const {
67 LLDB_INSTRUMENT_VA(this, rhs);
69 return !(*this == rhs);
72 bool SBAddress::IsValid() const {
73 LLDB_INSTRUMENT_VA(this);
74 return this->operator bool();
76 SBAddress::operator bool() const {
77 LLDB_INSTRUMENT_VA(this);
79 return m_opaque_up != nullptr && m_opaque_up->IsValid();
82 void SBAddress::Clear() {
83 LLDB_INSTRUMENT_VA(this);
85 m_opaque_up = std::make_unique<Address>();
88 void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
89 LLDB_INSTRUMENT_VA(this, section, offset);
91 Address &addr = ref();
92 addr.SetSection(section.GetSP());
93 addr.SetOffset(offset);
96 void SBAddress::SetAddress(const Address &address) { ref() = address; }
98 lldb::addr_t SBAddress::GetFileAddress() const {
99 LLDB_INSTRUMENT_VA(this);
101 if (m_opaque_up->IsValid())
102 return m_opaque_up->GetFileAddress();
103 else
104 return LLDB_INVALID_ADDRESS;
107 lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
108 LLDB_INSTRUMENT_VA(this, target);
110 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
111 TargetSP target_sp(target.GetSP());
112 if (target_sp) {
113 if (m_opaque_up->IsValid()) {
114 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
115 addr = m_opaque_up->GetLoadAddress(target_sp.get());
119 return addr;
122 void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
123 LLDB_INSTRUMENT_VA(this, load_addr, target);
125 // Create the address object if we don't already have one
126 ref();
127 if (target.IsValid())
128 *this = target.ResolveLoadAddress(load_addr);
129 else
130 m_opaque_up->Clear();
132 // Check if we weren't were able to resolve a section offset address. If we
133 // weren't it is ok, the load address might be a location on the stack or
134 // heap, so we should just have an address with no section and a valid offset
135 if (!m_opaque_up->IsValid())
136 m_opaque_up->SetOffset(load_addr);
139 bool SBAddress::OffsetAddress(addr_t offset) {
140 LLDB_INSTRUMENT_VA(this, offset);
142 if (m_opaque_up->IsValid()) {
143 addr_t addr_offset = m_opaque_up->GetOffset();
144 if (addr_offset != LLDB_INVALID_ADDRESS) {
145 m_opaque_up->SetOffset(addr_offset + offset);
146 return true;
149 return false;
152 lldb::SBSection SBAddress::GetSection() {
153 LLDB_INSTRUMENT_VA(this);
155 lldb::SBSection sb_section;
156 if (m_opaque_up->IsValid())
157 sb_section.SetSP(m_opaque_up->GetSection());
158 return sb_section;
161 lldb::addr_t SBAddress::GetOffset() {
162 LLDB_INSTRUMENT_VA(this);
164 if (m_opaque_up->IsValid())
165 return m_opaque_up->GetOffset();
166 return 0;
169 Address *SBAddress::operator->() { return m_opaque_up.get(); }
171 const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
173 Address &SBAddress::ref() {
174 if (m_opaque_up == nullptr)
175 m_opaque_up = std::make_unique<Address>();
176 return *m_opaque_up;
179 const Address &SBAddress::ref() const {
180 // This object should already have checked with "IsValid()" prior to calling
181 // this function. In case you didn't we will assert and die to let you know.
182 assert(m_opaque_up.get());
183 return *m_opaque_up;
186 Address *SBAddress::get() { return m_opaque_up.get(); }
188 bool SBAddress::GetDescription(SBStream &description) {
189 LLDB_INSTRUMENT_VA(this, description);
191 // Call "ref()" on the stream to make sure it creates a backing stream in
192 // case there isn't one already...
193 Stream &strm = description.ref();
194 if (m_opaque_up->IsValid()) {
195 m_opaque_up->Dump(&strm, nullptr, Address::DumpStyleResolvedDescription,
196 Address::DumpStyleModuleWithFileAddress, 4);
197 } else
198 strm.PutCString("No value");
200 return true;
203 SBModule SBAddress::GetModule() {
204 LLDB_INSTRUMENT_VA(this);
206 SBModule sb_module;
207 if (m_opaque_up->IsValid())
208 sb_module.SetSP(m_opaque_up->GetModule());
209 return sb_module;
212 SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
213 LLDB_INSTRUMENT_VA(this, resolve_scope);
215 SBSymbolContext sb_sc;
216 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
217 if (m_opaque_up->IsValid())
218 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
219 return sb_sc;
222 SBCompileUnit SBAddress::GetCompileUnit() {
223 LLDB_INSTRUMENT_VA(this);
225 SBCompileUnit sb_comp_unit;
226 if (m_opaque_up->IsValid())
227 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
228 return sb_comp_unit;
231 SBFunction SBAddress::GetFunction() {
232 LLDB_INSTRUMENT_VA(this);
234 SBFunction sb_function;
235 if (m_opaque_up->IsValid())
236 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
237 return sb_function;
240 SBBlock SBAddress::GetBlock() {
241 LLDB_INSTRUMENT_VA(this);
243 SBBlock sb_block;
244 if (m_opaque_up->IsValid())
245 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
246 return sb_block;
249 SBSymbol SBAddress::GetSymbol() {
250 LLDB_INSTRUMENT_VA(this);
252 SBSymbol sb_symbol;
253 if (m_opaque_up->IsValid())
254 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
255 return sb_symbol;
258 SBLineEntry SBAddress::GetLineEntry() {
259 LLDB_INSTRUMENT_VA(this);
261 SBLineEntry sb_line_entry;
262 if (m_opaque_up->IsValid()) {
263 LineEntry line_entry;
264 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
265 sb_line_entry.SetLineEntry(line_entry);
267 return sb_line_entry;