blob: e7dcbd30c0d3f1d6acc39bf640e0db5d1c4ea746 [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 Devliegherebd4bf822019-03-06 00:05:55 +000010#include "Utils.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000011#include "lldb/API/SBProcess.h"
Greg Claytoncac9c5f2011-09-24 00:52:29 +000012#include "lldb/API/SBSection.h"
Caroline Ticedde9cff2010-09-20 05:20:02 +000013#include "lldb/API/SBStream.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include "lldb/Core/Address.h"
Greg Clayton05d2b7f2011-03-31 01:08:07 +000015#include "lldb/Core/Module.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000016#include "lldb/Symbol/LineEntry.h"
Greg Claytonaf67cec2010-12-20 20:49:23 +000017#include "lldb/Target/Target.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000018#include "lldb/Utility/Log.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 Devlieghered5b44032019-02-13 06:25:41 +000024SBAddress::SBAddress() : m_opaque_up(new Address()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBAddress::SBAddress(const Address *lldb_object_ptr)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000027 : m_opaque_up(new Address()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 if (lldb_object_ptr)
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000029 m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030}
31
Jonas Devlieghered5b44032019-02-13 06:25:41 +000032SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000033 m_opaque_up = clone(rhs.m_opaque_up);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034}
35
Kate Stoneb9c1b512016-09-06 20:57:50 +000036SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000037 : m_opaque_up(new Address(section.GetSP(), offset)) {}
Greg Clayton819134a2012-02-04 02:58:17 +000038
Greg Clayton00e6fbf2011-07-22 16:46:35 +000039// Create an address by resolving a load address using the supplied target
Kate Stoneb9c1b512016-09-06 20:57:50 +000040SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
Jonas Devlieghered5b44032019-02-13 06:25:41 +000041 : m_opaque_up(new Address()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000042 SetLoadAddress(load_addr, target);
Greg Clayton00e6fbf2011-07-22 16:46:35 +000043}
44
Kate Stoneb9c1b512016-09-06 20:57:50 +000045SBAddress::~SBAddress() {}
Greg Clayton00e6fbf2011-07-22 16:46:35 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
Jonas Devliegherebd4bf822019-03-06 00:05:55 +000048 if (this != &rhs)
49 m_opaque_up = clone(rhs.m_opaque_up);
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000051}
52
Nitesh Jaindd125942017-05-04 11:34:42 +000053bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
54 if (lhs.IsValid() && rhs.IsValid())
55 return lhs.ref() == rhs.ref();
56 return false;
57}
58
Kate Stoneb9c1b512016-09-06 20:57:50 +000059bool SBAddress::IsValid() const {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000060 return m_opaque_up != NULL && m_opaque_up->IsValid();
Kate Stoneb9c1b512016-09-06 20:57:50 +000061}
62
Jonas Devlieghered5b44032019-02-13 06:25:41 +000063void SBAddress::Clear() { m_opaque_up.reset(new Address()); }
Kate Stoneb9c1b512016-09-06 20:57:50 +000064
65void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
66 Address &addr = ref();
67 addr.SetSection(section.GetSP());
68 addr.SetOffset(offset);
69}
70
71void SBAddress::SetAddress(const Address *lldb_object_ptr) {
72 if (lldb_object_ptr)
73 ref() = *lldb_object_ptr;
74 else
Jonas Devlieghered5b44032019-02-13 06:25:41 +000075 m_opaque_up.reset(new Address());
Kate Stoneb9c1b512016-09-06 20:57:50 +000076}
77
78lldb::addr_t SBAddress::GetFileAddress() const {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000079 if (m_opaque_up->IsValid())
80 return m_opaque_up->GetFileAddress();
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 else
82 return LLDB_INVALID_ADDRESS;
83}
84
85lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
86 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
87
88 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
89 TargetSP target_sp(target.GetSP());
90 if (target_sp) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +000091 if (m_opaque_up->IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
Jonas Devlieghered5b44032019-02-13 06:25:41 +000093 addr = m_opaque_up->GetLoadAddress(target_sp.get());
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 }
95 }
96
97 if (log) {
98 if (addr == LLDB_INVALID_ADDRESS)
99 log->Printf(
100 "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
101 static_cast<void *>(target_sp.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
104 static_cast<void *>(target_sp.get()), addr);
105 }
106
107 return addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108}
109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
111 // Create the address object if we don't already have one
112 ref();
113 if (target.IsValid())
114 *this = target.ResolveLoadAddress(load_addr);
115 else
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000116 m_opaque_up->Clear();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000117
Adrian Prantl05097242018-04-30 16:49:04 +0000118 // Check if we weren't were able to resolve a section offset address. If we
119 // weren't it is ok, the load address might be a location on the stack or
120 // heap, so we should just have an address with no section and a valid offset
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000121 if (!m_opaque_up->IsValid())
122 m_opaque_up->SetOffset(load_addr);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000123}
124
125bool SBAddress::OffsetAddress(addr_t offset) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000126 if (m_opaque_up->IsValid()) {
127 addr_t addr_offset = m_opaque_up->GetOffset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 if (addr_offset != LLDB_INVALID_ADDRESS) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000129 m_opaque_up->SetOffset(addr_offset + offset);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 return true;
Caroline Ticeceb6b132010-10-26 03:11:13 +0000131 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000132 }
133 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136lldb::SBSection SBAddress::GetSection() {
137 lldb::SBSection sb_section;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000138 if (m_opaque_up->IsValid())
139 sb_section.SetSP(m_opaque_up->GetSection());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 return sb_section;
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143lldb::addr_t SBAddress::GetOffset() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000144 if (m_opaque_up->IsValid())
145 return m_opaque_up->GetOffset();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000149Address *SBAddress::operator->() { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000151const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000152
153Address &SBAddress::ref() {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000154 if (m_opaque_up == NULL)
155 m_opaque_up.reset(new Address());
156 return *m_opaque_up;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000157}
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159const Address &SBAddress::ref() const {
Adrian Prantl05097242018-04-30 16:49:04 +0000160 // This object should already have checked with "IsValid()" prior to calling
161 // this function. In case you didn't we will assert and die to let you know.
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000162 assert(m_opaque_up.get());
163 return *m_opaque_up;
Greg Clayton13d19502012-01-29 06:07:39 +0000164}
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000165
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000166Address *SBAddress::get() { return m_opaque_up.get(); }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167
168bool SBAddress::GetDescription(SBStream &description) {
169 // Call "ref()" on the stream to make sure it creates a backing stream in
170 // case there isn't one already...
171 Stream &strm = description.ref();
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000172 if (m_opaque_up->IsValid()) {
173 m_opaque_up->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 Address::DumpStyleModuleWithFileAddress, 4);
175 StreamString sstrm;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000176 // m_opaque_up->Dump (&sstrm, NULL,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000177 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
178 // 4);
179 // if (sstrm.GetData())
180 // strm.Printf (" (%s)", sstrm.GetData());
181 } else
182 strm.PutCString("No value");
183
184 return true;
Greg Clayton09960032010-09-10 18:31:35 +0000185}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187SBModule SBAddress::GetModule() {
188 SBModule sb_module;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000189 if (m_opaque_up->IsValid())
190 sb_module.SetSP(m_opaque_up->GetModule());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000191 return sb_module;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192}
193
Kate Stoneb9c1b512016-09-06 20:57:50 +0000194SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
195 SBSymbolContext sb_sc;
Zachary Turner991e4452018-10-25 20:45:19 +0000196 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000197 if (m_opaque_up->IsValid())
198 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199 return sb_sc;
Greg Clayton09960032010-09-10 18:31:35 +0000200}
201
Kate Stoneb9c1b512016-09-06 20:57:50 +0000202SBCompileUnit SBAddress::GetCompileUnit() {
203 SBCompileUnit sb_comp_unit;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000204 if (m_opaque_up->IsValid())
205 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 return sb_comp_unit;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000207}
208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209SBFunction SBAddress::GetFunction() {
210 SBFunction sb_function;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000211 if (m_opaque_up->IsValid())
212 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000213 return sb_function;
Caroline Tice750cd172010-10-26 23:49:36 +0000214}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000215
Kate Stoneb9c1b512016-09-06 20:57:50 +0000216SBBlock SBAddress::GetBlock() {
217 SBBlock sb_block;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000218 if (m_opaque_up->IsValid())
219 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000220 return sb_block;
Caroline Ticedde9cff2010-09-20 05:20:02 +0000221}
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000222
Kate Stoneb9c1b512016-09-06 20:57:50 +0000223SBSymbol SBAddress::GetSymbol() {
224 SBSymbol sb_symbol;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000225 if (m_opaque_up->IsValid())
226 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 return sb_symbol;
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000228}
229
Kate Stoneb9c1b512016-09-06 20:57:50 +0000230SBLineEntry SBAddress::GetLineEntry() {
231 SBLineEntry sb_line_entry;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000232 if (m_opaque_up->IsValid()) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233 LineEntry line_entry;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000234 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000235 sb_line_entry.SetLineEntry(line_entry);
236 }
237 return sb_line_entry;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000238}