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/API/SBThread.h"
15#include "lldb/Core/Address.h"
16#include "lldb/Core/Module.h"
18#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
22
23using namespace lldb;
24using namespace lldb_private;
25
29
31 : m_opaque_up(std::make_unique<Address>(address)) {}
32
34 LLDB_INSTRUMENT_VA(this, rhs);
35
37}
38
40 : m_opaque_up(new Address(section.GetSP(), offset)) {
41 LLDB_INSTRUMENT_VA(this, section, offset);
42}
43
44// Create an address by resolving a load address using the supplied target
46 : m_opaque_up(new Address()) {
47 LLDB_INSTRUMENT_VA(this, load_addr, target);
48
49 SetLoadAddress(load_addr, target);
50}
51
52SBAddress::~SBAddress() = default;
53
55 LLDB_INSTRUMENT_VA(this, rhs);
56
57 if (this != &rhs)
59 return *this;
60}
61
62bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
63 if (lhs.IsValid() && rhs.IsValid())
64 return lhs.ref() == rhs.ref();
65 return false;
66}
67
68bool SBAddress::operator!=(const SBAddress &rhs) const {
69 LLDB_INSTRUMENT_VA(this, rhs);
70
71 return !(*this == rhs);
72}
73
74bool SBAddress::IsValid() const {
76 return this->operator bool();
77}
78SBAddress::operator bool() const {
80
81 return m_opaque_up != nullptr && m_opaque_up->IsValid();
82}
83
86
87 m_opaque_up = std::make_unique<Address>();
88}
89
91 LLDB_INSTRUMENT_VA(this, section, offset);
92
93 Address &addr = ref();
94 addr = Address(section.GetSP(), offset);
95}
96
97void SBAddress::SetAddress(const Address &address) { ref() = address; }
98
100 LLDB_INSTRUMENT_VA(this);
101
102 if (m_opaque_up->IsValid())
103 return m_opaque_up->GetFileAddress();
104 else
106}
107
109 LLDB_INSTRUMENT_VA(this, target);
110
112 TargetSP target_sp(target.GetSP());
113 if (target_sp) {
114 if (m_opaque_up->IsValid()) {
115 TargetAPIMutex api_lock = target_sp->GetAPIMutex();
116 std::lock_guard<TargetAPIMutex> guard(api_lock);
117 addr = m_opaque_up->GetLoadAddress(target_sp.get());
118 }
119 }
120
121 return addr;
122}
123
125 LLDB_INSTRUMENT_VA(this, load_addr, target);
126
127 // Create the address object if we don't already have one
128 ref();
129 if (target.IsValid())
130 *this = target.ResolveLoadAddress(load_addr);
131 else
132 m_opaque_up->Clear();
133
134 // Check if we weren't were able to resolve a section offset address. If we
135 // weren't it is ok, the load address might be a location on the stack or
136 // heap, so we should just have an address with no section and a valid offset
137 if (!m_opaque_up->IsValid())
138 m_opaque_up->SetOffset(load_addr);
139}
140
142 LLDB_INSTRUMENT_VA(this, offset);
143
144 if (m_opaque_up->IsValid())
145 return m_opaque_up->Slide(offset);
146 return false;
147}
148
150 LLDB_INSTRUMENT_VA(this);
151
152 lldb::SBSection sb_section;
153 if (m_opaque_up->IsValid())
154 sb_section.SetSP(m_opaque_up->GetSection());
155 return sb_section;
156}
157
159 LLDB_INSTRUMENT_VA(this);
160
161 if (m_opaque_up->IsValid())
162 return m_opaque_up->GetOffset();
163 return 0;
164}
165
167
168const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
169
171 if (m_opaque_up == nullptr)
172 m_opaque_up = std::make_unique<Address>();
173 return *m_opaque_up;
174}
175
176const Address &SBAddress::ref() const {
177 // This object should already have checked with "IsValid()" prior to calling
178 // this function. In case you didn't we will assert and die to let you know.
179 assert(m_opaque_up.get());
180 return *m_opaque_up;
181}
182
184
186 LLDB_INSTRUMENT_VA(this, description);
187
188 // Call "ref()" on the stream to make sure it creates a backing stream in
189 // case there isn't one already...
190 Stream &strm = description.ref();
191 if (m_opaque_up->IsValid()) {
194 } else
195 strm.PutCString("No value");
196
197 return true;
198}
199
201 LLDB_INSTRUMENT_VA(this);
202
203 SBModule sb_module;
204 if (m_opaque_up->IsValid())
205 sb_module.SetSP(m_opaque_up->GetModule());
206 return sb_module;
207}
208
210 LLDB_INSTRUMENT_VA(this, resolve_scope);
211
212 SBSymbolContext sb_sc;
213 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
214 if (m_opaque_up->IsValid())
215 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
216 return sb_sc;
217}
218
220 LLDB_INSTRUMENT_VA(this);
221
222 SBCompileUnit sb_comp_unit;
223 if (m_opaque_up->IsValid())
224 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
225 return sb_comp_unit;
226}
227
229 LLDB_INSTRUMENT_VA(this);
230
231 SBFunction sb_function;
232 if (m_opaque_up->IsValid())
233 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
234 return sb_function;
235}
236
238 LLDB_INSTRUMENT_VA(this);
239
240 SBBlock sb_block;
241 if (m_opaque_up->IsValid())
242 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
243 return sb_block;
244}
245
247 LLDB_INSTRUMENT_VA(this);
248
249 SBSymbol sb_symbol;
250 if (m_opaque_up->IsValid())
251 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
252 return sb_symbol;
253}
254
256 LLDB_INSTRUMENT_VA(this);
257
258 SBLineEntry sb_line_entry;
259 if (m_opaque_up->IsValid()) {
260 LineEntry line_entry;
261 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
262 sb_line_entry.SetLineEntry(line_entry);
263 }
264 return sb_line_entry;
265}
266
271
273 : m_opaque_up(new ProcessAddress(load_addr)) {
274 LLDB_INSTRUMENT_VA(this, load_addr);
275}
276
278 lldb::addr_space_t address_space_id)
279 : m_opaque_up(new ProcessAddress(addr, address_space_id)) {
280 LLDB_INSTRUMENT_VA(this, addr, address_space_id);
281}
282
284 lldb::addr_space_t address_space_id,
285 lldb::SBThread thread)
286 : m_opaque_up(
287 new ProcessAddress(addr, address_space_id, thread.GetThreadID())) {
288 LLDB_INSTRUMENT_VA(this, addr, address_space_id, thread);
289}
290
292
294
296
297const SBProcessAddress &
299 LLDB_INSTRUMENT_VA(this, rhs);
300 if (this != &rhs)
302 return *this;
303}
#define LLDB_INSTRUMENT_VA(...)
lldb_private::Address * get()
lldb::SBSection GetSection()
bool operator!=(const SBAddress &rhs) const
Definition SBAddress.cpp:68
lldb::SBSymbol GetSymbol()
lldb::SBLineEntry GetLineEntry()
lldb::SBBlock GetBlock()
bool OffsetAddress(addr_t offset)
addr_t GetLoadAddress(const lldb::SBTarget &target) const
lldb_private::Address & ref()
bool IsValid() const
Definition SBAddress.cpp:74
friend class SBSymbol
Definition SBAddress.h:99
friend class SBTarget
Definition SBAddress.h:101
friend class SBModule
Definition SBAddress.h:97
friend class SBSymbolContext
Definition SBAddress.h:100
std::unique_ptr< lldb_private::Address > m_opaque_up
Definition SBAddress.h:126
bool GetDescription(lldb::SBStream &description)
friend class SBLineEntry
Definition SBAddress.h:95
friend class SBBlock
Definition SBAddress.h:90
void SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
addr_t GetFileAddress() const
Definition SBAddress.cpp:99
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope)
lldb::SBModule GetModule()
lldb::SBFunction GetFunction()
lldb_private::Address * operator->()
const lldb::SBAddress & operator=(const lldb::SBAddress &rhs)
Definition SBAddress.cpp:54
friend class SBFunction
Definition SBAddress.h:94
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition SBAddress.cpp:90
lldb::addr_t GetOffset()
lldb::SBCompileUnit GetCompileUnit()
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition SBBlock.cpp:178
void reset(lldb_private::CompileUnit *lldb_object_ptr)
void reset(lldb_private::Function *lldb_object_ptr)
void SetLineEntry(const lldb_private::LineEntry &lldb_object_ref)
void SetSP(const ModuleSP &module_sp)
Definition SBModule.cpp:218
A memory address, optionally in a non-default address space.
Definition SBAddress.h:134
lldb_private::ProcessAddress & ref()
const lldb::SBProcessAddress & operator=(const lldb::SBProcessAddress &rhs)
SBProcessAddress(const SBProcessAddress &rhs)
std::unique_ptr< lldb_private::ProcessAddress > m_opaque_up
Definition SBAddress.h:160
lldb::SectionSP GetSP() const
void SetSP(const lldb::SectionSP &section_sp)
lldb_private::Stream & ref()
Definition SBStream.cpp:179
lldb_private::SymbolContext & ref()
void reset(lldb_private::Symbol *)
Definition SBSymbol.cpp:152
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition SBTarget.cpp:606
bool IsValid() const
Definition SBTarget.cpp:168
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:600
A section + offset based address class.
Definition Address.h:62
@ 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
An address in a process, qualified by an address space.
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:63
A Lockable handle over a Target's API mutex, returned by Target::GetAPIMutex() and backing the public...
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition Utils.h:17
uint64_t addr_t
Definition lldb-types.h:80
uint64_t addr_space_t
Definition lldb-types.h:81
std::shared_ptr< lldb_private::Target > TargetSP
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition SBAddress.cpp:62
A line table entry class.
Definition LineEntry.h:21