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 "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"
16 #include "lldb/Symbol/LineEntry.h"
17 #include "lldb/Target/Target.h"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBAddress::SBAddress() : m_opaque_up(new Address()) {
25  LLDB_INSTRUMENT_VA(this);
26 }
27 
29  : m_opaque_up(std::make_unique<Address>(address)) {}
30 
31 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
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 
50 SBAddress::~SBAddress() = default;
51 
53  LLDB_INSTRUMENT_VA(this, rhs);
54 
55  if (this != &rhs)
57  return *this;
58 }
59 
60 bool 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 
66 bool SBAddress::operator!=(const SBAddress &rhs) const {
67  LLDB_INSTRUMENT_VA(this, rhs);
68 
69  return !(*this == rhs);
70 }
71 
72 bool SBAddress::IsValid() const {
73  LLDB_INSTRUMENT_VA(this);
74  return this->operator bool();
75 }
76 SBAddress::operator bool() const {
77  LLDB_INSTRUMENT_VA(this);
78 
79  return m_opaque_up != nullptr && m_opaque_up->IsValid();
80 }
81 
83  LLDB_INSTRUMENT_VA(this);
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.SetSection(section.GetSP());
93  addr.SetOffset(offset);
94 }
95 
96 void SBAddress::SetAddress(const Address &address) { ref() = address; }
97 
99  LLDB_INSTRUMENT_VA(this);
100 
101  if (m_opaque_up->IsValid())
102  return m_opaque_up->GetFileAddress();
103  else
104  return LLDB_INVALID_ADDRESS;
105 }
106 
108  LLDB_INSTRUMENT_VA(this, target);
109 
111  TargetSP target_sp(target.GetSP());
112  if (target_sp) {
113  if (m_opaque_up->IsValid()) {
114  std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
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  addr_t addr_offset = m_opaque_up->GetOffset();
144  if (addr_offset != LLDB_INVALID_ADDRESS) {
145  m_opaque_up->SetOffset(addr_offset + offset);
146  return true;
147  }
148  }
149  return false;
150 }
151 
153  LLDB_INSTRUMENT_VA(this);
154 
155  lldb::SBSection sb_section;
156  if (m_opaque_up->IsValid())
157  sb_section.SetSP(m_opaque_up->GetSection());
158  return sb_section;
159 }
160 
162  LLDB_INSTRUMENT_VA(this);
163 
164  if (m_opaque_up->IsValid())
165  return m_opaque_up->GetOffset();
166  return 0;
167 }
168 
170 
171 const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
172 
174  if (m_opaque_up == nullptr)
175  m_opaque_up = std::make_unique<Address>();
176  return *m_opaque_up;
177 }
178 
179 const Address &SBAddress::ref() const {
180  // This object should already have checked with "IsValid()" prior to calling
181  // this function. In case you didn't we will assert and die to let you know.
182  assert(m_opaque_up.get());
183  return *m_opaque_up;
184 }
185 
186 Address *SBAddress::get() { return m_opaque_up.get(); }
187 
189  LLDB_INSTRUMENT_VA(this, description);
190 
191  // Call "ref()" on the stream to make sure it creates a backing stream in
192  // case there isn't one already...
193  Stream &strm = description.ref();
194  if (m_opaque_up->IsValid()) {
195  m_opaque_up->Dump(&strm, nullptr, Address::DumpStyleResolvedDescription,
196  Address::DumpStyleModuleWithFileAddress, 4);
197  } else
198  strm.PutCString("No value");
199 
200  return true;
201 }
202 
204  LLDB_INSTRUMENT_VA(this);
205 
206  SBModule sb_module;
207  if (m_opaque_up->IsValid())
208  sb_module.SetSP(m_opaque_up->GetModule());
209  return sb_module;
210 }
211 
213  LLDB_INSTRUMENT_VA(this, resolve_scope);
214 
215  SBSymbolContext sb_sc;
216  SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
217  if (m_opaque_up->IsValid())
218  m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
219  return sb_sc;
220 }
221 
223  LLDB_INSTRUMENT_VA(this);
224 
225  SBCompileUnit sb_comp_unit;
226  if (m_opaque_up->IsValid())
227  sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
228  return sb_comp_unit;
229 }
230 
232  LLDB_INSTRUMENT_VA(this);
233 
234  SBFunction sb_function;
235  if (m_opaque_up->IsValid())
236  sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
237  return sb_function;
238 }
239 
241  LLDB_INSTRUMENT_VA(this);
242 
243  SBBlock sb_block;
244  if (m_opaque_up->IsValid())
245  sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
246  return sb_block;
247 }
248 
250  LLDB_INSTRUMENT_VA(this);
251 
252  SBSymbol sb_symbol;
253  if (m_opaque_up->IsValid())
254  sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
255  return sb_symbol;
256 }
257 
259  LLDB_INSTRUMENT_VA(this);
260 
261  SBLineEntry sb_line_entry;
262  if (m_opaque_up->IsValid()) {
263  LineEntry line_entry;
264  if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
265  sb_line_entry.SetLineEntry(line_entry);
266  }
267  return sb_line_entry;
268 }
Instrumentation.h
lldb::SBStream::ref
lldb_private::Stream & ref()
Definition: SBStream.cpp:176
lldb::SBTarget::IsValid
bool IsValid() const
Definition: SBTarget.cpp:152
lldb::SBAddress::GetSymbolContext
lldb::SBSymbolContext GetSymbolContext(uint32_t resolve_scope)
Definition: SBAddress.cpp:212
lldb::SBCompileUnit
Definition: SBCompileUnit.h:17
lldb::SBAddress::GetLineEntry
lldb::SBLineEntry GetLineEntry()
Definition: SBAddress.cpp:258
lldb::SBAddress::~SBAddress
~SBAddress()
lldb::SBAddress::SetLoadAddress
void SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Definition: SBAddress.cpp:122
Module.h
lldb::SBAddress::GetFunction
lldb::SBFunction GetFunction()
Definition: SBAddress.cpp:231
lldb::SBSymbol
Definition: SBSymbol.h:19
lldb::SBTarget::ResolveLoadAddress
lldb::SBAddress ResolveLoadAddress(lldb::addr_t vm_addr)
Resolve a current load address into a section offset address.
Definition: SBTarget.cpp:582
SBSection.h
lldb_private::Stream
Definition: Stream.h:28
lldb::SBBlock::SetPtr
void SetPtr(lldb_private::Block *lldb_object_ptr)
Definition: SBBlock.cpp:161
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:88
lldb::SBSymbolContext::ref
lldb_private::SymbolContext & ref()
Definition: SBSymbolContext.cpp:169
lldb::SBAddress::GetFileAddress
addr_t GetFileAddress() const
Definition: SBAddress.cpp:98
lldb::SBAddress::OffsetAddress
bool OffsetAddress(addr_t offset)
Definition: SBAddress.cpp:139
Target.h
lldb::SBTarget
Definition: SBTarget.h:29
lldb::SBAddress::GetSection
lldb::SBSection GetSection()
Definition: SBAddress.cpp:152
lldb::SBAddress::operator=
const lldb::SBAddress & operator=(const lldb::SBAddress &rhs)
Definition: SBAddress.cpp:52
SBStream.h
bool
lldb::SBAddress::GetBlock
lldb::SBBlock GetBlock()
Definition: SBAddress.cpp:240
lldb::SBSymbol::reset
void reset(lldb_private::Symbol *)
Definition: SBSymbol.cpp:139
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:107
Utils.h
lldb::SBModule::SetSP
void SetSP(const ModuleSP &module_sp)
Definition: SBModule.cpp:211
LineEntry.h
lldb::SBAddress::GetModule
lldb::SBModule GetModule()
Definition: SBAddress.cpp:203
lldb_private::Address::SetSection
void SetSection(const lldb::SectionSP &section_sp)
Set accessor for the section.
Definition: Address.h:463
StreamString.h
lldb::SBBlock
Definition: SBBlock.h:19
lldb::SBAddress::IsValid
bool IsValid() const
Definition: SBAddress.cpp:72
lldb::operator==
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)
Definition: SBAddress.cpp:60
lldb::SBFunction
Definition: SBFunction.h:18
lldb::SBFunction::reset
void reset(lldb_private::Function *lldb_object_ptr)
Definition: SBFunction.cpp:139
lldb::SBSection
Definition: SBSection.h:17
lldb::SBAddress::GetDescription
bool GetDescription(lldb::SBStream &description)
Definition: SBAddress.cpp:188
Address.h
lldb::SBAddress::operator->
lldb_private::Address * operator->()
Definition: SBAddress.cpp:169
lldb::SBAddress::Clear
void Clear()
Definition: SBAddress.cpp:82
lldb::SBAddress::GetCompileUnit
lldb::SBCompileUnit GetCompileUnit()
Definition: SBAddress.cpp:222
SBAddress.h
lldb::SBSymbolContext
Definition: SBSymbolContext.h:22
uint32_t
lldb::SBAddress::GetOffset
lldb::addr_t GetOffset()
Definition: SBAddress.cpp:161
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:66
lldb::SBSection::GetSP
lldb::SectionSP GetSP() const
Definition: SBSection.cpp:112
lldb::SBCompileUnit::reset
void reset(lldb_private::CompileUnit *lldb_object_ptr)
Definition: SBCompileUnit.cpp:220
lldb_private::Address::SetOffset
bool SetOffset(lldb::addr_t offset)
Set accessor for the offset.
Definition: Address.h:438
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
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:173
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:576
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
lldb::SBLineEntry::SetLineEntry
void SetLineEntry(const lldb_private::LineEntry &lldb_object_ref)
Definition: SBLineEntry.cpp:43
lldb::SBAddress::get
lldb_private::Address * get()
Definition: SBAddress.cpp:186
SBProcess.h
lldb::SBAddress::GetSymbol
lldb::SBSymbol GetSymbol()
Definition: SBAddress.cpp:249
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
Definition: SBAddress.h:15
lldb::SBSection::SetSP
void SetSP(const lldb::SectionSP &section_sp)
Definition: SBSection.cpp:114
lldb::SBAddress::SBAddress
SBAddress()
Definition: SBAddress.cpp:24
lldb_private::LineEntry
Definition: LineEntry.h:20