1 //===-- SBAddress.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/SBAddress.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"
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
);
56 m_opaque_up
= clone(rhs
.m_opaque_up
);
60 bool lldb::operator==(const SBAddress
&lhs
, const SBAddress
&rhs
) {
61 if (lhs
.IsValid() && rhs
.IsValid())
62 return lhs
.ref() == rhs
.ref();
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();
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());
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());
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
127 if (target
.IsValid())
128 *this = target
.ResolveLoadAddress(load_addr
);
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
);
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());
161 lldb::addr_t
SBAddress::GetOffset() {
162 LLDB_INSTRUMENT_VA(this);
164 if (m_opaque_up
->IsValid())
165 return m_opaque_up
->GetOffset();
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
>();
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());
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);
198 strm
.PutCString("No value");
203 SBModule
SBAddress::GetModule() {
204 LLDB_INSTRUMENT_VA(this);
207 if (m_opaque_up
->IsValid())
208 sb_module
.SetSP(m_opaque_up
->GetModule());
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
);
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());
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());
240 SBBlock
SBAddress::GetBlock() {
241 LLDB_INSTRUMENT_VA(this);
244 if (m_opaque_up
->IsValid())
245 sb_block
.SetPtr(m_opaque_up
->CalculateSymbolContextBlock());
249 SBSymbol
SBAddress::GetSymbol() {
250 LLDB_INSTRUMENT_VA(this);
253 if (m_opaque_up
->IsValid())
254 sb_symbol
.reset(m_opaque_up
->CalculateSymbolContextSymbol());
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
;