LLDB  mainline
SBAddress.cpp
Go to the documentation of this file.
1 //===-- SBAddress.cpp -------------------------------------------*- C++ -*-===//
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 
28 SBAddress::SBAddress(const Address *lldb_object_ptr)
29  : m_opaque_up(new Address()) {
30  if (lldb_object_ptr)
31  m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr);
32 }
33 
34 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
36 
37  m_opaque_up = clone(rhs.m_opaque_up);
38 }
39 
41  : m_opaque_up(new Address(section.GetSP(), offset)) {
43  offset);
44 }
45 
46 // Create an address by resolving a load address using the supplied target
48  : m_opaque_up(new Address()) {
50  load_addr, target);
51 
52  SetLoadAddress(load_addr, target);
53 }
54 
56 
59  SBAddress, operator=,(const lldb::SBAddress &), rhs);
60 
61  if (this != &rhs)
62  m_opaque_up = clone(rhs.m_opaque_up);
63  return LLDB_RECORD_RESULT(*this);
64 }
65 
66 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
67  if (lhs.IsValid() && rhs.IsValid())
68  return lhs.ref() == rhs.ref();
69  return false;
70 }
71 
72 bool SBAddress::operator!=(const SBAddress &rhs) const {
73  LLDB_RECORD_METHOD_CONST(bool, SBAddress, operator!=,(const SBAddress &),
74  &rhs);
75 
76  return !(*this == rhs);
77 }
78 
79 bool SBAddress::IsValid() const {
81  return this->operator bool();
82 }
83 SBAddress::operator bool() const {
84  LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, operator bool);
85 
86  return m_opaque_up != NULL && m_opaque_up->IsValid();
87 }
88 
91 
92  m_opaque_up.reset(new Address());
93 }
94 
97  (lldb::SBSection, lldb::addr_t), section, offset);
98 
99  Address &addr = ref();
100  addr.SetSection(section.GetSP());
101  addr.SetOffset(offset);
102 }
103 
104 void SBAddress::SetAddress(const Address *lldb_object_ptr) {
105  if (lldb_object_ptr)
106  ref() = *lldb_object_ptr;
107  else
108  m_opaque_up.reset(new Address());
109 }
110 
113 
114  if (m_opaque_up->IsValid())
115  return m_opaque_up->GetFileAddress();
116  else
117  return LLDB_INVALID_ADDRESS;
118 }
119 
122  (const lldb::SBTarget &), target);
123 
125  TargetSP target_sp(target.GetSP());
126  if (target_sp) {
127  if (m_opaque_up->IsValid()) {
128  std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
129  addr = m_opaque_up->GetLoadAddress(target_sp.get());
130  }
131  }
132 
133  return addr;
134 }
135 
138  (lldb::addr_t, lldb::SBTarget &), load_addr, target);
139 
140  // Create the address object if we don't already have one
141  ref();
142  if (target.IsValid())
143  *this = target.ResolveLoadAddress(load_addr);
144  else
145  m_opaque_up->Clear();
146 
147  // Check if we weren't were able to resolve a section offset address. If we
148  // weren't it is ok, the load address might be a location on the stack or
149  // heap, so we should just have an address with no section and a valid offset
150  if (!m_opaque_up->IsValid())
151  m_opaque_up->SetOffset(load_addr);
152 }
153 
156 
157  if (m_opaque_up->IsValid()) {
158  addr_t addr_offset = m_opaque_up->GetOffset();
159  if (addr_offset != LLDB_INVALID_ADDRESS) {
160  m_opaque_up->SetOffset(addr_offset + offset);
161  return true;
162  }
163  }
164  return false;
165 }
166 
169 
170  lldb::SBSection sb_section;
171  if (m_opaque_up->IsValid())
172  sb_section.SetSP(m_opaque_up->GetSection());
173  return LLDB_RECORD_RESULT(sb_section);
174 }
175 
178 
179  if (m_opaque_up->IsValid())
180  return m_opaque_up->GetOffset();
181  return 0;
182 }
183 
184 Address *SBAddress::operator->() { return m_opaque_up.get(); }
185 
186 const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
187 
189  if (m_opaque_up == NULL)
190  m_opaque_up.reset(new Address());
191  return *m_opaque_up;
192 }
193 
194 const Address &SBAddress::ref() const {
195  // This object should already have checked with "IsValid()" prior to calling
196  // this function. In case you didn't we will assert and die to let you know.
197  assert(m_opaque_up.get());
198  return *m_opaque_up;
199 }
200 
201 Address *SBAddress::get() { return m_opaque_up.get(); }
202 
205  description);
206 
207  // Call "ref()" on the stream to make sure it creates a backing stream in
208  // case there isn't one already...
209  Stream &strm = description.ref();
210  if (m_opaque_up->IsValid()) {
211  m_opaque_up->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
212  Address::DumpStyleModuleWithFileAddress, 4);
213  StreamString sstrm;
214  // m_opaque_up->Dump (&sstrm, NULL,
215  // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
216  // 4);
217  // if (sstrm.GetData())
218  // strm.Printf (" (%s)", sstrm.GetData());
219  } else
220  strm.PutCString("No value");
221 
222  return true;
223 }
224 
227 
228  SBModule sb_module;
229  if (m_opaque_up->IsValid())
230  sb_module.SetSP(m_opaque_up->GetModule());
231  return LLDB_RECORD_RESULT(sb_module);
232 }
233 
236  (uint32_t), resolve_scope);
237 
238  SBSymbolContext sb_sc;
239  SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
240  if (m_opaque_up->IsValid())
241  m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
242  return LLDB_RECORD_RESULT(sb_sc);
243 }
244 
247 
248  SBCompileUnit sb_comp_unit;
249  if (m_opaque_up->IsValid())
250  sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
251  return LLDB_RECORD_RESULT(sb_comp_unit);
252 }
253 
256 
257  SBFunction sb_function;
258  if (m_opaque_up->IsValid())
259  sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
260  return LLDB_RECORD_RESULT(sb_function);
261 }
262 
265 
266  SBBlock sb_block;
267  if (m_opaque_up->IsValid())
268  sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
269  return LLDB_RECORD_RESULT(sb_block);
270 }
271 
274 
275  SBSymbol sb_symbol;
276  if (m_opaque_up->IsValid())
277  sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
278  return LLDB_RECORD_RESULT(sb_symbol);
279 }
280 
283 
284  SBLineEntry sb_line_entry;
285  if (m_opaque_up->IsValid()) {
286  LineEntry line_entry;
287  if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
288  sb_line_entry.SetLineEntry(line_entry);
289  }
290  return LLDB_RECORD_RESULT(sb_line_entry);
291 }
292 
293 namespace lldb_private {
294 namespace repro {
295 
296 template <>
303  SBAddress, operator=,(const lldb::SBAddress &));
305  SBAddress, operator!=,(const lldb::SBAddress &));
307  LLDB_REGISTER_METHOD_CONST(bool, SBAddress, operator bool, ());
313  (const lldb::SBTarget &));
322  (uint32_t));
328 }
329 
330 }
331 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
A line table entry class.
Definition: LineEntry.h:20
The registry contains a unique mapping between functions and their ID.
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
#define LLDB_RECORD_CONSTRUCTOR_NO_ARGS(Class)
bool GetDescription(lldb::SBStream &description)
Definition: SBAddress.cpp:203
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition: SBAddress.cpp:66
lldb_private::Address * operator->()
Definition: SBAddress.cpp:184
void SetSection(const lldb::SectionSP &section_sp)
Set accessor for the section.
Definition: Address.h:444
void RegisterMethods< SBAddress >(Registry &R)
Definition: SBAddress.cpp:297
lldb::SBSection GetSection()
Definition: SBAddress.cpp:167
void SetAddress(lldb::SBSection section, lldb::addr_t offset)
Definition: SBAddress.cpp:95
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:419
#define LLDB_REGISTER_CONSTRUCTOR(Class, Signature)
#define LLDB_RECORD_METHOD_NO_ARGS(Result, Class, Method)
void SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Definition: SBAddress.cpp:136
#define LLDB_INVALID_ADDRESS
Invalid value definitions.
Definition: lldb-defines.h:85
lldb::TargetSP GetSP() const
Definition: SBTarget.cpp:600
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
lldb::SBBlock GetBlock()
Definition: SBAddress.cpp:263
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope)
Definition: SBAddress.cpp:234
lldb_private::Address & ref()
Definition: SBAddress.cpp:188
#define LLDB_RECORD_CONSTRUCTOR(Class, Signature,...)
bool operator!=(const SBAddress &rhs) const
Definition: SBAddress.cpp:72
#define LLDB_RECORD_METHOD(Result, Class, Method, Signature,...)
lldb::addr_t GetOffset()
Definition: SBAddress.cpp:176
lldb_private::SymbolContext & ref()
A section + offset based address class.
Definition: Address.h:80
bool IsValid() const
Definition: SBTarget.cpp:167
lldb_private::Stream & ref()
Definition: SBStream.cpp:162
bool OffsetAddress(addr_t offset)
Definition: SBAddress.cpp:154
#define LLDB_RECORD_METHOD_CONST(Result, Class, Method, Signature,...)
uint64_t addr_t
Definition: lldb-types.h:83
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition: SBTarget.cpp:606
lldb::SBLineEntry GetLineEntry()
Definition: SBAddress.cpp:281
addr_t GetLoadAddress(const lldb::SBTarget &target) const
Definition: SBAddress.cpp:120
Definition: SBAddress.h:15
lldb::SBModule GetModule()
Definition: SBAddress.cpp:225
#define LLDB_REGISTER_METHOD(Result, Class, Method, Signature)
lldb_private::Address * get()
Definition: SBAddress.cpp:201
const lldb::SBAddress & operator=(const lldb::SBAddress &rhs)
Definition: SBAddress.cpp:57
#define LLDB_RECORD_METHOD_CONST_NO_ARGS(Result, Class, Method)
lldb::SBFunction GetFunction()
Definition: SBAddress.cpp:254
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:153
void reset(lldb_private::Symbol *)
Definition: SBSymbol.cpp:154
addr_t GetFileAddress() const
Definition: SBAddress.cpp:111
#define LLDB_RECORD_RESULT(Result)
lldb::SBCompileUnit GetCompileUnit()
Definition: SBAddress.cpp:245
bool IsValid() const
Definition: SBAddress.cpp:79
#define LLDB_REGISTER_METHOD_CONST(Result, Class, Method, Signature)
lldb::SBSymbol GetSymbol()
Definition: SBAddress.cpp:272