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 
9 #include "lldb/API/SBAddress.h"
10 #include "SBReproducerPrivate.h"
11 #include "Utils.h"
12 #include "lldb/API/SBProcess.h"
13 #include "lldb/API/SBSection.h"
14 #include "lldb/API/SBStream.h"
15 #include "lldb/Core/Address.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Symbol/LineEntry.h"
18 #include "lldb/Target/Target.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBAddress::SBAddress() : m_opaque_up(new Address()) {
26 }
27 
29  : m_opaque_up(std::make_unique<Address>(address)) {}
30 
31 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
33 
35 }
36 
38  : m_opaque_up(new Address(section.GetSP(), offset)) {
40  offset);
41 }
42 
43 // Create an address by resolving a load address using the supplied target
45  : m_opaque_up(new Address()) {
47  load_addr, target);
48 
49  SetLoadAddress(load_addr, target);
50 }
51 
52 SBAddress::~SBAddress() = default;
53 
56  SBAddress, operator=,(const lldb::SBAddress &), rhs);
57 
58  if (this != &rhs)
60  return LLDB_RECORD_RESULT(*this);
61 }
62 
63 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
64  if (lhs.IsValid() && rhs.IsValid())
65  return lhs.ref() == rhs.ref();
66  return false;
67 }
68 
69 bool SBAddress::operator!=(const SBAddress &rhs) const {
70  LLDB_RECORD_METHOD_CONST(bool, SBAddress, operator!=,(const SBAddress &),
71  &rhs);
72 
73  return !(*this == rhs);
74 }
75 
76 bool SBAddress::IsValid() const {
78  return this->operator bool();
79 }
80 SBAddress::operator bool() const {
81  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, operator bool);
82 
83  return m_opaque_up != nullptr && m_opaque_up->IsValid();
84 }
85 
88 
89  m_opaque_up = std::make_unique<Address>();
90 }
91 
94  (lldb::SBSection, lldb::addr_t), section, offset);
95 
96  Address &addr = ref();
97  addr.SetSection(section.GetSP());
98  addr.SetOffset(offset);
99 }
100 
101 void SBAddress::SetAddress(const Address &address) { ref() = address; }
102 
105 
106  if (m_opaque_up->IsValid())
107  return m_opaque_up->GetFileAddress();
108  else
109  return LLDB_INVALID_ADDRESS;
110 }
111 
114  (const lldb::SBTarget &), target);
115 
117  TargetSP target_sp(target.GetSP());
118  if (target_sp) {
119  if (m_opaque_up->IsValid()) {
120  std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
121  addr = m_opaque_up->GetLoadAddress(target_sp.get());
122  }
123  }
124 
125  return addr;
126 }
127 
130  (lldb::addr_t, lldb::SBTarget &), load_addr, target);
131 
132  // Create the address object if we don't already have one
133  ref();
134  if (target.IsValid())
135  *this = target.ResolveLoadAddress(load_addr);
136  else
137  m_opaque_up->Clear();
138 
139  // Check if we weren't were able to resolve a section offset address. If we
140  // weren't it is ok, the load address might be a location on the stack or
141  // heap, so we should just have an address with no section and a valid offset
142  if (!m_opaque_up->IsValid())
143  m_opaque_up->SetOffset(load_addr);
144 }
145 
148 
149  if (m_opaque_up->IsValid()) {
150  addr_t addr_offset = m_opaque_up->GetOffset();
151  if (addr_offset != LLDB_INVALID_ADDRESS) {
152  m_opaque_up->SetOffset(addr_offset + offset);
153  return true;
154  }
155  }
156  return false;
157 }
158 
161 
162  lldb::SBSection sb_section;
163  if (m_opaque_up->IsValid())
164  sb_section.SetSP(m_opaque_up->GetSection());
165  return LLDB_RECORD_RESULT(sb_section);
166 }
167 
170 
171  if (m_opaque_up->IsValid())
172  return m_opaque_up->GetOffset();
173  return 0;
174 }
175 
177 
178 const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
179 
181  if (m_opaque_up == nullptr)
182  m_opaque_up = std::make_unique<Address>();
183  return *m_opaque_up;
184 }
185 
186 const Address &SBAddress::ref() const {
187  // This object should already have checked with "IsValid()" prior to calling
188  // this function. In case you didn't we will assert and die to let you know.
189  assert(m_opaque_up.get());
190  return *m_opaque_up;
191 }
192 
193 Address *SBAddress::get() { return m_opaque_up.get(); }
194 
197  description);
198 
199  // Call "ref()" on the stream to make sure it creates a backing stream in
200  // case there isn't one already...
201  Stream &strm = description.ref();
202  if (m_opaque_up->IsValid()) {
203  m_opaque_up->Dump(&strm, nullptr, Address::DumpStyleResolvedDescription,
204  Address::DumpStyleModuleWithFileAddress, 4);
205  } else
206  strm.PutCString("No value");
207 
208  return true;
209 }
210 
213 
214  SBModule sb_module;
215  if (m_opaque_up->IsValid())
216  sb_module.SetSP(m_opaque_up->GetModule());
217  return LLDB_RECORD_RESULT(sb_module);
218 }
219 
222  (uint32_t), resolve_scope);
223 
224  SBSymbolContext sb_sc;
225  SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
226  if (m_opaque_up->IsValid())
227  m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
228  return LLDB_RECORD_RESULT(sb_sc);
229 }
230 
233 
234  SBCompileUnit sb_comp_unit;
235  if (m_opaque_up->IsValid())
236  sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
237  return LLDB_RECORD_RESULT(sb_comp_unit);
238 }
239 
242 
243  SBFunction sb_function;
244  if (m_opaque_up->IsValid())
245  sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
246  return LLDB_RECORD_RESULT(sb_function);
247 }
248 
251 
252  SBBlock sb_block;
253  if (m_opaque_up->IsValid())
254  sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
255  return LLDB_RECORD_RESULT(sb_block);
256 }
257 
260 
261  SBSymbol sb_symbol;
262  if (m_opaque_up->IsValid())
263  sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
264  return LLDB_RECORD_RESULT(sb_symbol);
265 }
266 
269 
270  SBLineEntry sb_line_entry;
271  if (m_opaque_up->IsValid()) {
272  LineEntry line_entry;
273  if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
274  sb_line_entry.SetLineEntry(line_entry);
275  }
276  return LLDB_RECORD_RESULT(sb_line_entry);
277 }
278 
279 namespace lldb_private {
280 namespace repro {
281 
282 template <>
289  SBAddress, operator=,(const lldb::SBAddress &));
291  SBAddress, operator!=,(const lldb::SBAddress &));
292  LLDB_REGISTER_METHOD_CONST(bool, SBAddress, IsValid, ());
293  LLDB_REGISTER_METHOD_CONST(bool, SBAddress, operator bool, ());
294  LLDB_REGISTER_METHOD(void, SBAddress, Clear, ());
295  LLDB_REGISTER_METHOD(void, SBAddress, SetAddress,
297  LLDB_REGISTER_METHOD_CONST(lldb::addr_t, SBAddress, GetFileAddress, ());
299  (const lldb::SBTarget &));
300  LLDB_REGISTER_METHOD(void, SBAddress, SetLoadAddress,
302  LLDB_REGISTER_METHOD(bool, SBAddress, OffsetAddress, (lldb::addr_t));
305  LLDB_REGISTER_METHOD(bool, SBAddress, GetDescription, (lldb::SBStream &));
308  (uint32_t));
309  LLDB_REGISTER_METHOD(lldb::SBCompileUnit, SBAddress, GetCompileUnit, ());
314 }
315 
316 }
317 }
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:178
lldb::SBTarget::IsValid
bool IsValid() const
Definition: SBTarget.cpp:162
lldb::SBAddress::GetSymbolContext
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope)
Definition: SBAddress.cpp:220
lldb::SBCompileUnit
Definition: SBCompileUnit.h:17
LLDB_RECORD_RESULT
#define LLDB_RECORD_RESULT(Result)
Definition: ReproducerInstrumentation.h:211
LLDB_RECORD_METHOD_CONST_NO_ARGS
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:168
lldb::SBAddress::GetLineEntry
lldb::SBLineEntry GetLineEntry()
Definition: SBAddress.cpp:267
LLDB_RECORD_METHOD_NO_ARGS
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
Definition: ReproducerInstrumentation.h:165
lldb::SBAddress::~SBAddress
~SBAddress()
lldb::SBAddress::SetLoadAddress
void SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Definition: SBAddress.cpp:128
Module.h
lldb::SBAddress::GetFunction
lldb::SBFunction GetFunction()
Definition: SBAddress.cpp:240
lldb::SBSymbol
Definition: SBSymbol.h:19
LLDB_RECORD_CONSTRUCTOR_NO_ARGS
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
Definition: ReproducerInstrumentation.h:136
lldb::SBTarget::ResolveLoadAddress
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition: SBTarget.cpp:611
LLDB_REGISTER_METHOD_CONST
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:98
SBSection.h
lldb_private::Stream
Definition: Stream.h:28
lldb::SBBlock::SetPtr
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:163
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb::SBAddress::SetAddress
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:92
SBReproducerPrivate.h
lldb::SBSymbolContext::ref
lldb_private::SymbolContext & ref()
Definition: SBSymbolContext.cpp:192
LLDB_REGISTER_CONSTRUCTOR
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
Definition: ReproducerInstrumentation.h:89
lldb::SBAddress::GetFileAddress
addr_t GetFileAddress() const
Definition: SBAddress.cpp:103
lldb::SBAddress::OffsetAddress
bool OffsetAddress(addr_t offset)
Definition: SBAddress.cpp:146
Target.h
lldb::SBTarget
Definition: SBTarget.h:29
lldb::SBAddress::GetSection
lldb::SBSection GetSection()
Definition: SBAddress.cpp:159
lldb::SBAddress::operator=
const lldb::SBAddress & operator=(const lldb::SBAddress &rhs)
Definition: SBAddress.cpp:54
SBStream.h
LLDB_RECORD_METHOD
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:158
lldb::SBAddress::GetBlock
lldb::SBBlock GetBlock()
Definition: SBAddress.cpp:249
lldb::SBSymbol::reset
void reset(lldb_private::Symbol *)
Definition: SBSymbol.cpp:145
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
lldb::SBAddress::GetLoadAddress
addr_t GetLoadAddress(const lldb::SBTarget &target) const
Definition: SBAddress.cpp:112
Utils.h
lldb::SBModule::SetSP
void SetSP(const ModuleSP &module_sp)
Definition: SBModule.cpp:208
LineEntry.h
lldb::SBAddress::GetModule
lldb::SBModule GetModule()
Definition: SBAddress.cpp:211
lldb_private::Address::SetSection
void SetSection(const lldb::SectionSP &section_sp)
Set accessor for the section.
Definition: Address.h:454
StreamString.h
lldb::SBBlock
Definition: SBBlock.h:19
lldb::SBAddress::IsValid
bool IsValid() const
Definition: SBAddress.cpp:76
lldb::operator==
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition: SBAddress.cpp:63
lldb::SBFunction
Definition: SBFunction.h:18
lldb::SBFunction::reset
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:144
lldb::SBSection
Definition: SBSection.h:17
lldb::SBAddress::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBAddress.cpp:195
Address.h
lldb::SBAddress::operator->
lldb_private::Address * operator->()
Definition: SBAddress.cpp:176
lldb::SBAddress::Clear
void Clear()
Definition: SBAddress.cpp:86
lldb::SBAddress::GetCompileUnit
lldb::SBCompileUnit GetCompileUnit()
Definition: SBAddress.cpp:231
SBAddress.h
LLDB_RECORD_CONSTRUCTOR
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
Definition: ReproducerInstrumentation.h:133
lldb_private::repro::Registry
The registry contains a unique mapping between functions and their ID.
Definition: ReproducerInstrumentation.h:517
lldb::SBSymbolContext
Definition: SBSymbolContext.h:22
uint32_t
lldb::SBAddress::GetOffset
lldb::addr_t GetOffset()
Definition: SBAddress.cpp:168
lldb_private::Address
Definition: Address.h:59
lldb::SBModule
Definition: SBModule.h:20
lldb::SBAddress::operator!=
bool operator!=(const SBAddress &rhs) const
Definition: SBAddress.cpp:69
lldb::SBSection::GetSP
lldb::SectionSP GetSP() const
Definition: SBSection.cpp:118
lldb::SBCompileUnit::reset
void reset(lldb_private::CompileUnit *lldb_object_ptr)
Definition: SBCompileUnit.cpp:223
lldb_private::Address::SetOffset
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:429
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
lldb::SBAddress::m_opaque_up
std::unique_ptr< lldb_private::Address > m_opaque_up
Definition: SBAddress.h:123
lldb::SBLineEntry
Definition: SBLineEntry.h:18
lldb::SBAddress
Definition: SBAddress.h:17
lldb::SBAddress::ref
lldb_private::Address & ref()
Definition: SBAddress.cpp:180
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBTarget::GetSP
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:605
lldb::SBLineEntry::SetLineEntry
void SetLineEntry(const lldb_private::LineEntry &lldb_object_ref)
Definition: SBLineEntry.cpp:47
lldb::SBAddress::get
lldb_private::Address * get()
Definition: SBAddress.cpp:193
SBProcess.h
lldb_private::repro::RegisterMethods< SBAddress >
void RegisterMethods< SBAddress >(Registry &R)
Definition: SBAddress.cpp:283
lldb::SBAddress::GetSymbol
lldb::SBSymbol GetSymbol()
Definition: SBAddress.cpp:258
lldb_private::Stream::PutCString
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:63
lldb::SBStream
Definition: SBStream.h:18
LLDB_RECORD_METHOD_CONST
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
Definition: ReproducerInstrumentation.h:161
lldb
Definition: SBAddress.h:15
lldb::SBSection::SetSP
void SetSP(const lldb::SectionSP &section_sp)
Definition: SBSection.cpp:120
lldb::SBAddress::SBAddress
SBAddress()
Definition: SBAddress.cpp:24
lldb_private::LineEntry
Definition: LineEntry.h:20
LLDB_REGISTER_METHOD
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
Definition: ReproducerInstrumentation.h:93