blob: 2353639da5d0d20a3123698550349668cb9d5d9e [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBAddress.cpp -------------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/API/SBAddress.h"
Jonas Devliegherebaf56642019-03-06 00:06:00 +000010#include "SBReproducerPrivate.h"
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000011#include "Utils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include "lldb/API/SBProcess.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000013#include "lldb/API/SBSection.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000014#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000015#include "lldb/Core/Address.h"
Greg Clayton05d2b7f2011-03-31 01:08:07 +000016#include "lldb/Core/Module.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000017#include "lldb/Symbol/LineEntry.h"
Greg Claytonaf67cec2010-12-20 20:49:23 +000018#include "lldb/Target/Target.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000019#include "lldb/Utility/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000020
21using namespace lldb;
Caroline Ticeceb6b132010-10-26 03:11:13 +000022using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023
Jonas Devliegherebaf56642019-03-06 00:06:00 +000024SBAddress::SBAddress() : m_opaque_up(new Address()) {
25 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBAddress);
26}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028SBAddress::SBAddress(const Address *lldb_object_ptr)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000029 : m_opaque_up(new Address()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 if (lldb_object_ptr)
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000031 m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032}
33
Jonas Devlieghered5b44032019-02-13 06:25:41 +000034SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000035 LLDB_RECORD_CONSTRUCTOR(SBAddress, (const lldb::SBAddress &), rhs);
36
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000037 m_opaque_up = clone(rhs.m_opaque_up);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000038}
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
Jonas Devliegherebaf56642019-03-06 00:06:00 +000041 : m_opaque_up(new Address(section.GetSP(), offset)) {
42 LLDB_RECORD_CONSTRUCTOR(SBAddress, (lldb::SBSection, lldb::addr_t), section,
43 offset);
44}
Greg Clayton819134a2012-02-04 02:58:17 +000045
Greg Clayton00e6fbf2011-07-22 16:46:35 +000046// Create an address by resolving a load address using the supplied target
Kate Stoneb9c1b512016-09-06 20:57:50 +000047SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000048 : m_opaque_up(new Address()) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000049 LLDB_RECORD_CONSTRUCTOR(SBAddress, (lldb::addr_t, lldb::SBTarget &),
50 load_addr, target);
51
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 SetLoadAddress(load_addr, target);
Greg Clayton00e6fbf2011-07-22 16:46:35 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055SBAddress::~SBAddress() {}
Greg Clayton00e6fbf2011-07-22 16:46:35 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000058 LLDB_RECORD_METHOD(const lldb::SBAddress &,
59 SBAddress, operator=,(const lldb::SBAddress &), rhs);
60
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000061 if (this != &rhs)
62 m_opaque_up = clone(rhs.m_opaque_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000064}
65
Nitesh Jaindd125942017-05-04 11:34:42 +000066bool 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000072bool SBAddress::IsValid() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000073 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, IsValid);
Pavel Labath7f5237b2019-03-11 13:58:46 +000074 return this->operator bool();
75}
76SBAddress::operator bool() const {
77 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, operator bool);
Jonas Devliegherebaf56642019-03-06 00:06:00 +000078
Jonas Devlieghered5b44032019-02-13 06:25:41 +000079 return m_opaque_up != NULL && m_opaque_up->IsValid();
Kate Stoneb9c1b512016-09-06 20:57:50 +000080}
81
Jonas Devliegherebaf56642019-03-06 00:06:00 +000082void SBAddress::Clear() {
83 LLDB_RECORD_METHOD_NO_ARGS(void, SBAddress, Clear);
84
85 m_opaque_up.reset(new Address());
86}
Kate Stoneb9c1b512016-09-06 20:57:50 +000087
88void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +000089 LLDB_RECORD_METHOD(void, SBAddress, SetAddress,
90 (lldb::SBSection, lldb::addr_t), section, offset);
91
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 Address &addr = ref();
93 addr.SetSection(section.GetSP());
94 addr.SetOffset(offset);
95}
96
97void SBAddress::SetAddress(const Address *lldb_object_ptr) {
98 if (lldb_object_ptr)
99 ref() = *lldb_object_ptr;
100 else
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000101 m_opaque_up.reset(new Address());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102}
103
104lldb::addr_t SBAddress::GetFileAddress() const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000105 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::addr_t, SBAddress, GetFileAddress);
106
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000107 if (m_opaque_up->IsValid())
108 return m_opaque_up->GetFileAddress();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109 else
110 return LLDB_INVALID_ADDRESS;
111}
112
113lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000114 LLDB_RECORD_METHOD_CONST(lldb::addr_t, SBAddress, GetLoadAddress,
115 (const lldb::SBTarget &), target);
116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
118 TargetSP target_sp(target.GetSP());
119 if (target_sp) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000120 if (m_opaque_up->IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000121 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000122 addr = m_opaque_up->GetLoadAddress(target_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123 }
124 }
125
Kate Stoneb9c1b512016-09-06 20:57:50 +0000126 return addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127}
128
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000130 LLDB_RECORD_METHOD(void, SBAddress, SetLoadAddress,
131 (lldb::addr_t, lldb::SBTarget &), load_addr, target);
132
Kate Stoneb9c1b512016-09-06 20:57:50 +0000133 // Create the address object if we don't already have one
134 ref();
135 if (target.IsValid())
136 *this = target.ResolveLoadAddress(load_addr);
137 else
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000138 m_opaque_up->Clear();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000139
Adrian Prantl05097242018-04-30 16:49:04 +0000140 // Check if we weren't were able to resolve a section offset address. If we
141 // weren't it is ok, the load address might be a location on the stack or
142 // heap, so we should just have an address with no section and a valid offset
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000143 if (!m_opaque_up->IsValid())
144 m_opaque_up->SetOffset(load_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000145}
146
147bool SBAddress::OffsetAddress(addr_t offset) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000148 LLDB_RECORD_METHOD(bool, SBAddress, OffsetAddress, (lldb::addr_t), offset);
149
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000150 if (m_opaque_up->IsValid()) {
151 addr_t addr_offset = m_opaque_up->GetOffset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152 if (addr_offset != LLDB_INVALID_ADDRESS) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000153 m_opaque_up->SetOffset(addr_offset + offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 return true;
Caroline Ticeceb6b132010-10-26 03:11:13 +0000155 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000156 }
157 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000158}
159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160lldb::SBSection SBAddress::GetSection() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000161 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBAddress, GetSection);
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 lldb::SBSection sb_section;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000164 if (m_opaque_up->IsValid())
165 sb_section.SetSP(m_opaque_up->GetSection());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000166 return LLDB_RECORD_RESULT(sb_section);
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000167}
168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169lldb::addr_t SBAddress::GetOffset() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000170 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBAddress, GetOffset);
171
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000172 if (m_opaque_up->IsValid())
173 return m_opaque_up->GetOffset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000175}
176
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000177Address *SBAddress::operator->() { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000178
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000179const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000180
181Address &SBAddress::ref() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000182 if (m_opaque_up == NULL)
183 m_opaque_up.reset(new Address());
184 return *m_opaque_up;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000185}
186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187const Address &SBAddress::ref() const {
Adrian Prantl05097242018-04-30 16:49:04 +0000188 // This object should already have checked with "IsValid()" prior to calling
189 // this function. In case you didn't we will assert and die to let you know.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000190 assert(m_opaque_up.get());
191 return *m_opaque_up;
Greg Clayton13d19502012-01-29 06:07:39 +0000192}
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000193
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000194Address *SBAddress::get() { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195
196bool SBAddress::GetDescription(SBStream &description) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000197 LLDB_RECORD_METHOD(bool, SBAddress, GetDescription, (lldb::SBStream &),
198 description);
199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 // Call "ref()" on the stream to make sure it creates a backing stream in
201 // case there isn't one already...
202 Stream &strm = description.ref();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000203 if (m_opaque_up->IsValid()) {
204 m_opaque_up->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 Address::DumpStyleModuleWithFileAddress, 4);
206 StreamString sstrm;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000207 // m_opaque_up->Dump (&sstrm, NULL,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000208 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
209 // 4);
210 // if (sstrm.GetData())
211 // strm.Printf (" (%s)", sstrm.GetData());
212 } else
213 strm.PutCString("No value");
214
215 return true;
Greg Clayton09960032010-09-10 18:31:35 +0000216}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218SBModule SBAddress::GetModule() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000219 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBModule, SBAddress, GetModule);
220
Kate Stoneb9c1b512016-09-06 20:57:50 +0000221 SBModule sb_module;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000222 if (m_opaque_up->IsValid())
223 sb_module.SetSP(m_opaque_up->GetModule());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000224 return LLDB_RECORD_RESULT(sb_module);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000225}
226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000228 LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBAddress, GetSymbolContext,
229 (uint32_t), resolve_scope);
230
Kate Stoneb9c1b512016-09-06 20:57:50 +0000231 SBSymbolContext sb_sc;
Zachary Turner991e4452018-10-25 20:45:19 +0000232 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000233 if (m_opaque_up->IsValid())
234 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000235 return LLDB_RECORD_RESULT(sb_sc);
Greg Clayton09960032010-09-10 18:31:35 +0000236}
237
Kate Stoneb9c1b512016-09-06 20:57:50 +0000238SBCompileUnit SBAddress::GetCompileUnit() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000239 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCompileUnit, SBAddress, GetCompileUnit);
240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 SBCompileUnit sb_comp_unit;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000242 if (m_opaque_up->IsValid())
243 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000244 return LLDB_RECORD_RESULT(sb_comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247SBFunction SBAddress::GetFunction() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000248 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFunction, SBAddress, GetFunction);
249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 SBFunction sb_function;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000251 if (m_opaque_up->IsValid())
252 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000253 return LLDB_RECORD_RESULT(sb_function);
Caroline Tice750cd172010-10-26 23:49:36 +0000254}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000255
Kate Stoneb9c1b512016-09-06 20:57:50 +0000256SBBlock SBAddress::GetBlock() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000257 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBAddress, GetBlock);
258
Kate Stoneb9c1b512016-09-06 20:57:50 +0000259 SBBlock sb_block;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000260 if (m_opaque_up->IsValid())
261 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000262 return LLDB_RECORD_RESULT(sb_block);
Caroline Ticedde9cff2010-09-20 05:20:02 +0000263}
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000264
Kate Stoneb9c1b512016-09-06 20:57:50 +0000265SBSymbol SBAddress::GetSymbol() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000266 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSymbol, SBAddress, GetSymbol);
267
Kate Stoneb9c1b512016-09-06 20:57:50 +0000268 SBSymbol sb_symbol;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000269 if (m_opaque_up->IsValid())
270 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000271 return LLDB_RECORD_RESULT(sb_symbol);
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000272}
273
Kate Stoneb9c1b512016-09-06 20:57:50 +0000274SBLineEntry SBAddress::GetLineEntry() {
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000275 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBLineEntry, SBAddress, GetLineEntry);
276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277 SBLineEntry sb_line_entry;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000278 if (m_opaque_up->IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279 LineEntry line_entry;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000280 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000281 sb_line_entry.SetLineEntry(line_entry);
282 }
Jonas Devliegherebaf56642019-03-06 00:06:00 +0000283 return LLDB_RECORD_RESULT(sb_line_entry);
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000284}