blob: b452ce327ab7927acab4a91ffc28aa07a8ac7a92 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SBAddress.cpp -------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/API/SBAddress.h"
11#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
Kate Stoneb9c1b512016-09-06 20:57:50 +000024SBAddress::SBAddress() : m_opaque_ap(new Address()) {}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
Kate Stoneb9c1b512016-09-06 20:57:50 +000026SBAddress::SBAddress(const Address *lldb_object_ptr)
27 : m_opaque_ap(new Address()) {
28 if (lldb_object_ptr)
29 ref() = *lldb_object_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000030}
31
Kate Stoneb9c1b512016-09-06 20:57:50 +000032SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_ap(new Address()) {
33 if (rhs.IsValid())
34 ref() = rhs.ref();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000035}
36
Kate Stoneb9c1b512016-09-06 20:57:50 +000037SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
38 : m_opaque_ap(new Address(section.GetSP(), offset)) {}
Greg Clayton819134a2012-02-04 02:58:17 +000039
Greg Clayton00e6fbf2011-07-22 16:46:35 +000040// Create an address by resolving a load address using the supplied target
Kate Stoneb9c1b512016-09-06 20:57:50 +000041SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
42 : m_opaque_ap(new Address()) {
43 SetLoadAddress(load_addr, target);
Greg Clayton00e6fbf2011-07-22 16:46:35 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046SBAddress::~SBAddress() {}
Greg Clayton00e6fbf2011-07-22 16:46:35 +000047
Kate Stoneb9c1b512016-09-06 20:57:50 +000048const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
49 if (this != &rhs) {
50 if (rhs.IsValid())
51 ref() = rhs.ref();
Greg Claytoncac9c5f2011-09-24 00:52:29 +000052 else
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 m_opaque_ap.reset(new Address());
54 }
55 return *this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000056}
57
Kate Stoneb9c1b512016-09-06 20:57:50 +000058bool SBAddress::IsValid() const {
59 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
60}
61
62void SBAddress::Clear() { m_opaque_ap.reset(new Address()); }
63
64void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
65 Address &addr = ref();
66 addr.SetSection(section.GetSP());
67 addr.SetOffset(offset);
68}
69
70void SBAddress::SetAddress(const Address *lldb_object_ptr) {
71 if (lldb_object_ptr)
72 ref() = *lldb_object_ptr;
73 else
74 m_opaque_ap.reset(new Address());
75}
76
77lldb::addr_t SBAddress::GetFileAddress() const {
78 if (m_opaque_ap->IsValid())
79 return m_opaque_ap->GetFileAddress();
80 else
81 return LLDB_INVALID_ADDRESS;
82}
83
84lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
85 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
86
87 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
88 TargetSP target_sp(target.GetSP());
89 if (target_sp) {
90 if (m_opaque_ap->IsValid()) {
91 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
92 addr = m_opaque_ap->GetLoadAddress(target_sp.get());
93 }
94 }
95
96 if (log) {
97 if (addr == LLDB_INVALID_ADDRESS)
98 log->Printf(
99 "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
100 static_cast<void *>(target_sp.get()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
103 static_cast<void *>(target_sp.get()), addr);
104 }
105
106 return addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107}
108
Kate Stoneb9c1b512016-09-06 20:57:50 +0000109void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
110 // Create the address object if we don't already have one
111 ref();
112 if (target.IsValid())
113 *this = target.ResolveLoadAddress(load_addr);
114 else
115 m_opaque_ap->Clear();
Caroline Ticeceb6b132010-10-26 03:11:13 +0000116
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 // Check if we weren't were able to resolve a section offset address.
118 // If we weren't it is ok, the load address might be a location on the
119 // stack or heap, so we should just have an address with no section and
120 // a valid offset
121 if (!m_opaque_ap->IsValid())
122 m_opaque_ap->SetOffset(load_addr);
123}
124
125bool SBAddress::OffsetAddress(addr_t offset) {
126 if (m_opaque_ap->IsValid()) {
127 addr_t addr_offset = m_opaque_ap->GetOffset();
128 if (addr_offset != LLDB_INVALID_ADDRESS) {
129 m_opaque_ap->SetOffset(addr_offset + offset);
130 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;
138 if (m_opaque_ap->IsValid())
139 sb_section.SetSP(m_opaque_ap->GetSection());
140 return sb_section;
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143lldb::addr_t SBAddress::GetOffset() {
144 if (m_opaque_ap->IsValid())
145 return m_opaque_ap->GetOffset();
146 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149Address *SBAddress::operator->() { return m_opaque_ap.get(); }
150
151const Address *SBAddress::operator->() const { return m_opaque_ap.get(); }
152
153Address &SBAddress::ref() {
154 if (m_opaque_ap.get() == NULL)
155 m_opaque_ap.reset(new Address());
156 return *m_opaque_ap;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000157}
158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159const Address &SBAddress::ref() const {
160 // This object should already have checked with "IsValid()"
161 // prior to calling this function. In case you didn't we will assert
162 // and die to let you know.
163 assert(m_opaque_ap.get());
164 return *m_opaque_ap;
Greg Clayton13d19502012-01-29 06:07:39 +0000165}
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000166
Kate Stoneb9c1b512016-09-06 20:57:50 +0000167Address *SBAddress::get() { return m_opaque_ap.get(); }
168
169bool SBAddress::GetDescription(SBStream &description) {
170 // Call "ref()" on the stream to make sure it creates a backing stream in
171 // case there isn't one already...
172 Stream &strm = description.ref();
173 if (m_opaque_ap->IsValid()) {
174 m_opaque_ap->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
175 Address::DumpStyleModuleWithFileAddress, 4);
176 StreamString sstrm;
177 // m_opaque_ap->Dump (&sstrm, NULL,
178 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
179 // 4);
180 // if (sstrm.GetData())
181 // strm.Printf (" (%s)", sstrm.GetData());
182 } else
183 strm.PutCString("No value");
184
185 return true;
Greg Clayton09960032010-09-10 18:31:35 +0000186}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187
Kate Stoneb9c1b512016-09-06 20:57:50 +0000188SBModule SBAddress::GetModule() {
189 SBModule sb_module;
190 if (m_opaque_ap->IsValid())
191 sb_module.SetSP(m_opaque_ap->GetModule());
192 return sb_module;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193}
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
196 SBSymbolContext sb_sc;
197 if (m_opaque_ap->IsValid())
198 m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), resolve_scope);
199 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;
204 if (m_opaque_ap->IsValid())
205 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
206 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;
211 if (m_opaque_ap->IsValid())
212 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
213 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;
218 if (m_opaque_ap->IsValid())
219 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
220 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;
225 if (m_opaque_ap->IsValid())
226 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
227 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;
232 if (m_opaque_ap->IsValid()) {
233 LineEntry line_entry;
234 if (m_opaque_ap->CalculateSymbolContextLineEntry(line_entry))
235 sb_line_entry.SetLineEntry(line_entry);
236 }
237 return sb_line_entry;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000238}
239
Kate Stoneb9c1b512016-09-06 20:57:50 +0000240AddressClass SBAddress::GetAddressClass() {
241 if (m_opaque_ap->IsValid())
242 return m_opaque_ap->GetAddressClass();
243 return eAddressClassInvalid;
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000244}