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
27
29 : m_opaque_up(std::make_unique<Address>(address)) {}
30
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 = Address(section.GetSP(), offset);
93}
94
95void SBAddress::SetAddress(const Address &address) { ref() = address; }
96
99
100 if (m_opaque_up->IsValid())
101 return m_opaque_up->GetFileAddress();
102 else
104}
105
107 LLDB_INSTRUMENT_VA(this, target);
108
110 TargetSP target_sp(target.GetSP());
111 if (target_sp) {
112 if (m_opaque_up->IsValid()) {
113 TargetAPIMutex api_lock = target_sp->GetAPIMutex();
114 std::lock_guard<TargetAPIMutex> guard(api_lock);
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 return m_opaque_up->Slide(offset);
144 return false;
145}
146
148 LLDB_INSTRUMENT_VA(this);
149
150 lldb::SBSection sb_section;
151 if (m_opaque_up->IsValid())
152 sb_section.SetSP(m_opaque_up->GetSection());
153 return sb_section;
154}
155
157 LLDB_INSTRUMENT_VA(this);
158
159 if (m_opaque_up->IsValid())
160 return m_opaque_up->GetOffset();
161 return 0;
162}
163
165
166const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
167
169 if (m_opaque_up == nullptr)
170 m_opaque_up = std::make_unique<Address>();
171 return *m_opaque_up;
172}
173
174const Address &SBAddress::ref() const {
175 // This object should already have checked with "IsValid()" prior to calling
176 // this function. In case you didn't we will assert and die to let you know.
177 assert(m_opaque_up.get());
178 return *m_opaque_up;
179}
180
182
184 LLDB_INSTRUMENT_VA(this, description);
185
186 // Call "ref()" on the stream to make sure it creates a backing stream in
187 // case there isn't one already...
188 Stream &strm = description.ref();
189 if (m_opaque_up->IsValid()) {
192 } else
193 strm.PutCString("No value");
194
195 return true;
196}
197
199 LLDB_INSTRUMENT_VA(this);
200
201 SBModule sb_module;
202 if (m_opaque_up->IsValid())
203 sb_module.SetSP(m_opaque_up->GetModule());
204 return sb_module;
205}
206
208 LLDB_INSTRUMENT_VA(this, resolve_scope);
209
210 SBSymbolContext sb_sc;
211 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
212 if (m_opaque_up->IsValid())
213 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
214 return sb_sc;
215}
216
218 LLDB_INSTRUMENT_VA(this);
219
220 SBCompileUnit sb_comp_unit;
221 if (m_opaque_up->IsValid())
222 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
223 return sb_comp_unit;
224}
225
227 LLDB_INSTRUMENT_VA(this);
228
229 SBFunction sb_function;
230 if (m_opaque_up->IsValid())
231 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
232 return sb_function;
233}
234
236 LLDB_INSTRUMENT_VA(this);
237
238 SBBlock sb_block;
239 if (m_opaque_up->IsValid())
240 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
241 return sb_block;
242}
243
245 LLDB_INSTRUMENT_VA(this);
246
247 SBSymbol sb_symbol;
248 if (m_opaque_up->IsValid())
249 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
250 return sb_symbol;
251}
252
254 LLDB_INSTRUMENT_VA(this);
255
256 SBLineEntry sb_line_entry;
257 if (m_opaque_up->IsValid()) {
258 LineEntry line_entry;
259 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
260 sb_line_entry.SetLineEntry(line_entry);
261 }
262 return sb_line_entry;
263}
#define LLDB_INSTRUMENT_VA(...)
lldb_private::Address * get()
lldb::SBSection GetSection()
bool operator!=(const SBAddress &rhs) const
Definition SBAddress.cpp:66
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:72
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:97
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:52
friend class SBFunction
Definition SBAddress.h:94
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition SBAddress.cpp:88
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
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
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
std::shared_ptr< lldb_private::Target > TargetSP
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition SBAddress.cpp:60
A line table entry class.
Definition LineEntry.h:21