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 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
114 addr = m_opaque_up->GetLoadAddress(target_sp.get());
115 }
116 }
117
118 return addr;
119}
120
122 LLDB_INSTRUMENT_VA(this, load_addr, target);
123
124 // Create the address object if we don't already have one
125 ref();
126 if (target.IsValid())
127 *this = target.ResolveLoadAddress(load_addr);
128 else
129 m_opaque_up->Clear();
130
131 // Check if we weren't were able to resolve a section offset address. If we
132 // weren't it is ok, the load address might be a location on the stack or
133 // heap, so we should just have an address with no section and a valid offset
134 if (!m_opaque_up->IsValid())
135 m_opaque_up->SetOffset(load_addr);
136}
137
139 LLDB_INSTRUMENT_VA(this, offset);
140
141 if (m_opaque_up->IsValid())
142 return m_opaque_up->Slide(offset);
143 return false;
144}
145
147 LLDB_INSTRUMENT_VA(this);
148
149 lldb::SBSection sb_section;
150 if (m_opaque_up->IsValid())
151 sb_section.SetSP(m_opaque_up->GetSection());
152 return sb_section;
153}
154
156 LLDB_INSTRUMENT_VA(this);
157
158 if (m_opaque_up->IsValid())
159 return m_opaque_up->GetOffset();
160 return 0;
161}
162
164
165const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
166
168 if (m_opaque_up == nullptr)
169 m_opaque_up = std::make_unique<Address>();
170 return *m_opaque_up;
171}
172
173const Address &SBAddress::ref() const {
174 // This object should already have checked with "IsValid()" prior to calling
175 // this function. In case you didn't we will assert and die to let you know.
176 assert(m_opaque_up.get());
177 return *m_opaque_up;
178}
179
181
183 LLDB_INSTRUMENT_VA(this, description);
184
185 // Call "ref()" on the stream to make sure it creates a backing stream in
186 // case there isn't one already...
187 Stream &strm = description.ref();
188 if (m_opaque_up->IsValid()) {
191 } else
192 strm.PutCString("No value");
193
194 return true;
195}
196
198 LLDB_INSTRUMENT_VA(this);
199
200 SBModule sb_module;
201 if (m_opaque_up->IsValid())
202 sb_module.SetSP(m_opaque_up->GetModule());
203 return sb_module;
204}
205
207 LLDB_INSTRUMENT_VA(this, resolve_scope);
208
209 SBSymbolContext sb_sc;
210 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
211 if (m_opaque_up->IsValid())
212 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
213 return sb_sc;
214}
215
217 LLDB_INSTRUMENT_VA(this);
218
219 SBCompileUnit sb_comp_unit;
220 if (m_opaque_up->IsValid())
221 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
222 return sb_comp_unit;
223}
224
226 LLDB_INSTRUMENT_VA(this);
227
228 SBFunction sb_function;
229 if (m_opaque_up->IsValid())
230 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
231 return sb_function;
232}
233
235 LLDB_INSTRUMENT_VA(this);
236
237 SBBlock sb_block;
238 if (m_opaque_up->IsValid())
239 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
240 return sb_block;
241}
242
244 LLDB_INSTRUMENT_VA(this);
245
246 SBSymbol sb_symbol;
247 if (m_opaque_up->IsValid())
248 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
249 return sb_symbol;
250}
251
253 LLDB_INSTRUMENT_VA(this);
254
255 SBLineEntry sb_line_entry;
256 if (m_opaque_up->IsValid()) {
257 LineEntry line_entry;
258 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
259 sb_line_entry.SetLineEntry(line_entry);
260 }
261 return sb_line_entry;
262}
#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:178
lldb_private::SymbolContext & ref()
void reset(lldb_private::Symbol *)
Definition SBSymbol.cpp:149
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition SBTarget.cpp:595
bool IsValid() const
Definition SBTarget.cpp:164
lldb::TargetSP GetSP() const
Definition SBTarget.cpp:589
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:65
#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