LLDB mainline
SBAddress.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
8
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"
17#include "lldb/Target/Target.h"
20
21using namespace lldb;
22using namespace lldb_private;
23
24SBAddress::SBAddress() : m_opaque_up(new Address()) {
26}
27
29 : m_opaque_up(std::make_unique<Address>(address)) {}
30
31SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
32 LLDB_INSTRUMENT_VA(this, rhs);
33
35}
36
38 : m_opaque_up(new Address(section.GetSP(), offset)) {
39 LLDB_INSTRUMENT_VA(this, section, offset);
40}
41
42// Create an address by resolving a load address using the supplied target
44 : m_opaque_up(new Address()) {
45 LLDB_INSTRUMENT_VA(this, load_addr, target);
46
47 SetLoadAddress(load_addr, target);
48}
49
50SBAddress::~SBAddress() = default;
51
53 LLDB_INSTRUMENT_VA(this, rhs);
54
55 if (this != &rhs)
57 return *this;
58}
59
60bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
61 if (lhs.IsValid() && rhs.IsValid())
62 return lhs.ref() == rhs.ref();
63 return false;
64}
65
66bool SBAddress::operator!=(const SBAddress &rhs) const {
67 LLDB_INSTRUMENT_VA(this, rhs);
68
69 return !(*this == rhs);
70}
71
72bool SBAddress::IsValid() const {
74 return this->operator bool();
75}
76SBAddress::operator bool() const {
78
79 return m_opaque_up != nullptr && m_opaque_up->IsValid();
80}
81
84
85 m_opaque_up = std::make_unique<Address>();
86}
87
89 LLDB_INSTRUMENT_VA(this, section, offset);
90
91 Address &addr = ref();
92 addr.SetSection(section.GetSP());
93 addr.SetOffset(offset);
94}
95
96void SBAddress::SetAddress(const Address &address) { ref() = address; }
97
100
101 if (m_opaque_up->IsValid())
102 return m_opaque_up->GetFileAddress();
103 else
105}
106
108 LLDB_INSTRUMENT_VA(this, target);
109
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());
116 }
117 }
118
119 return addr;
120}
121
123 LLDB_INSTRUMENT_VA(this, load_addr, target);
124
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();
131
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);
137}
138
140 LLDB_INSTRUMENT_VA(this, offset);
141
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;
147 }
148 }
149 return false;
150}
151
153 LLDB_INSTRUMENT_VA(this);
154
155 lldb::SBSection sb_section;
156 if (m_opaque_up->IsValid())
157 sb_section.SetSP(m_opaque_up->GetSection());
158 return sb_section;
159}
160
162 LLDB_INSTRUMENT_VA(this);
163
164 if (m_opaque_up->IsValid())
165 return m_opaque_up->GetOffset();
166 return 0;
167}
168
170
171const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
172
174 if (m_opaque_up == nullptr)
175 m_opaque_up = std::make_unique<Address>();
176 return *m_opaque_up;
177}
178
179const 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;
184}
185
187
189 LLDB_INSTRUMENT_VA(this, description);
190
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()) {
197 } else
198 strm.PutCString("No value");
199
200 return true;
201}
202
204 LLDB_INSTRUMENT_VA(this);
205
206 SBModule sb_module;
207 if (m_opaque_up->IsValid())
208 sb_module.SetSP(m_opaque_up->GetModule());
209 return sb_module;
210}
211
213 LLDB_INSTRUMENT_VA(this, resolve_scope);
214
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;
220}
221
223 LLDB_INSTRUMENT_VA(this);
224
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;
229}
230
232 LLDB_INSTRUMENT_VA(this);
233
234 SBFunction sb_function;
235 if (m_opaque_up->IsValid())
236 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
237 return sb_function;
238}
239
241 LLDB_INSTRUMENT_VA(this);
242
243 SBBlock sb_block;
244 if (m_opaque_up->IsValid())
245 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
246 return sb_block;
247}
248
250 LLDB_INSTRUMENT_VA(this);
251
252 SBSymbol sb_symbol;
253 if (m_opaque_up->IsValid())
254 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
255 return sb_symbol;
256}
257
259 LLDB_INSTRUMENT_VA(this);
260
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);
266 }
267 return sb_line_entry;
268}
#define LLDB_INSTRUMENT_VA(...)
lldb_private::Address * get()
Definition: SBAddress.cpp:186
lldb::SBSection GetSection()
Definition: SBAddress.cpp:152
bool operator!=(const SBAddress &rhs) const
Definition: SBAddress.cpp:66
lldb::SBSymbol GetSymbol()
Definition: SBAddress.cpp:249
lldb::SBLineEntry GetLineEntry()
Definition: SBAddress.cpp:258
lldb::SBBlock GetBlock()
Definition: SBAddress.cpp:240
bool OffsetAddress(addr_t offset)
Definition: SBAddress.cpp:139
addr_t GetLoadAddress(const lldb::SBTarget &target) const
Definition: SBAddress.cpp:107
lldb_private::Address & ref()
Definition: SBAddress.cpp:173
bool IsValid() const
Definition: SBAddress.cpp:72
std::unique_ptr< lldb_private::Address > m_opaque_up
Definition: SBAddress.h:125
bool GetDescription(lldb::SBStream &description)
Definition: SBAddress.cpp:188
void SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Definition: SBAddress.cpp:122
addr_t GetFileAddress() const
Definition: SBAddress.cpp:98
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope)
Definition: SBAddress.cpp:212
lldb::SBModule GetModule()
Definition: SBAddress.cpp:203
lldb::SBFunction GetFunction()
Definition: SBAddress.cpp:231
lldb_private::Address * operator->()
Definition: SBAddress.cpp:169
const lldb::SBAddress & operator=(const lldb::SBAddress &rhs)
Definition: SBAddress.cpp:52
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:88
lldb::addr_t GetOffset()
Definition: SBAddress.cpp:161
lldb::SBCompileUnit GetCompileUnit()
Definition: SBAddress.cpp:222
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:161
void reset(lldb_private::CompileUnit *lldb_object_ptr)
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:136
void SetLineEntry(const lldb_private::LineEntry &lldb_object_ref)
Definition: SBLineEntry.cpp:43
void SetSP(const ModuleSP &module_sp)
Definition: SBModule.cpp:211
lldb::SectionSP GetSP() const
Definition: SBSection.cpp:112
void SetSP(const lldb::SectionSP &section_sp)
Definition: SBSection.cpp:114
lldb_private::Stream & ref()
Definition: SBStream.cpp:177
lldb_private::SymbolContext & ref()
void reset(lldb_private::Symbol *)
Definition: SBSymbol.cpp:139
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition: SBTarget.cpp:591
bool IsValid() const
Definition: SBTarget.cpp:153
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:585
A section + offset based address class.
Definition: Address.h:62
void SetSection(const lldb::SectionSP &section_sp)
Set accessor for the section.
Definition: Address.h:473
@ DumpStyleModuleWithFileAddress
Display as the file address with the module name prepended (if any).
Definition: Address.h:93
@ DumpStyleResolvedDescription
Display the details about what an address resolves to.
Definition: Address.h:104
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:448
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Definition: SBAddress.h:15
uint64_t addr_t
Definition: lldb-types.h:79
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition: SBAddress.cpp:60
A line table entry class.
Definition: LineEntry.h:21