blob: f95fcb8b39854201037e1cc83950a581aad1923a [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"
Caroline Ticeceb6b132010-10-26 03:11:13 +000015#include "lldb/Core/Log.h"
Greg Clayton05d2b7f2011-03-31 01:08:07 +000016#include "lldb/Core/Module.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000017#include "lldb/Core/StreamString.h"
Greg Claytonaf67cec2010-12-20 20:49:23 +000018#include "lldb/Host/Mutex.h"
Zachary Turner32abc6e2015-03-03 19:23:09 +000019#include "lldb/Symbol/LineEntry.h"
Greg Claytonaf67cec2010-12-20 20:49:23 +000020#include "lldb/Target/Target.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021
Greg Claytoncac9c5f2011-09-24 00:52:29 +000022
Chris Lattner30fdc8d2010-06-08 16:52:24 +000023using namespace lldb;
Caroline Ticeceb6b132010-10-26 03:11:13 +000024using namespace lldb_private;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000025
26
27SBAddress::SBAddress () :
Jason Molendafca26da2014-10-31 21:30:59 +000028 m_opaque_ap (new Address())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029{
30}
31
Greg Claytoncac9c5f2011-09-24 00:52:29 +000032SBAddress::SBAddress (const Address *lldb_object_ptr) :
Jason Molendafca26da2014-10-31 21:30:59 +000033 m_opaque_ap (new Address())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000034{
35 if (lldb_object_ptr)
Greg Clayton7e8a6012012-04-04 20:36:48 +000036 ref() = *lldb_object_ptr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000037}
38
39SBAddress::SBAddress (const SBAddress &rhs) :
Jason Molendafca26da2014-10-31 21:30:59 +000040 m_opaque_ap (new Address())
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041{
42 if (rhs.IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +000043 ref() = rhs.ref();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044}
45
Greg Clayton819134a2012-02-04 02:58:17 +000046
47SBAddress::SBAddress (lldb::SBSection section, lldb::addr_t offset) :
Greg Clayton7e8a6012012-04-04 20:36:48 +000048 m_opaque_ap(new Address (section.GetSP(), offset))
Greg Clayton819134a2012-02-04 02:58:17 +000049{
50}
51
Greg Clayton00e6fbf2011-07-22 16:46:35 +000052// Create an address by resolving a load address using the supplied target
53SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
Jason Molendafca26da2014-10-31 21:30:59 +000054 m_opaque_ap(new Address())
Greg Clayton00e6fbf2011-07-22 16:46:35 +000055{
56 SetLoadAddress (load_addr, target);
57}
58
59
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061SBAddress::~SBAddress ()
62{
63}
64
65const SBAddress &
66SBAddress::operator = (const SBAddress &rhs)
67{
Greg Claytoncac9c5f2011-09-24 00:52:29 +000068 if (this != &rhs)
69 {
70 if (rhs.IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +000071 ref() = rhs.ref();
Greg Claytoncac9c5f2011-09-24 00:52:29 +000072 else
Jason Molendafca26da2014-10-31 21:30:59 +000073 m_opaque_ap.reset (new Address());
Greg Claytoncac9c5f2011-09-24 00:52:29 +000074 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075 return *this;
76}
77
78bool
79SBAddress::IsValid () const
80{
Greg Clayton66111032010-06-23 01:19:29 +000081 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082}
83
84void
Greg Clayton09960032010-09-10 18:31:35 +000085SBAddress::Clear ()
86{
Jason Molendafca26da2014-10-31 21:30:59 +000087 m_opaque_ap.reset (new Address());
Greg Clayton09960032010-09-10 18:31:35 +000088}
89
90void
Greg Clayton819134a2012-02-04 02:58:17 +000091SBAddress::SetAddress (lldb::SBSection section, lldb::addr_t offset)
92{
93 Address &addr = ref();
Greg Claytone72dfb32012-02-24 01:59:29 +000094 addr.SetSection (section.GetSP());
Greg Clayton819134a2012-02-04 02:58:17 +000095 addr.SetOffset (offset);
96}
97
98
99void
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000100SBAddress::SetAddress (const Address *lldb_object_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101{
102 if (lldb_object_ptr)
Greg Clayton7e8a6012012-04-04 20:36:48 +0000103 ref() = *lldb_object_ptr;
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000104 else
Jason Molendafca26da2014-10-31 21:30:59 +0000105 m_opaque_ap.reset (new Address());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106}
107
108lldb::addr_t
109SBAddress::GetFileAddress () const
110{
Jason Molendafca26da2014-10-31 21:30:59 +0000111 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000112 return m_opaque_ap->GetFileAddress();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000113 else
114 return LLDB_INVALID_ADDRESS;
115}
116
117lldb::addr_t
Greg Claytonf5e56de2010-09-14 23:36:40 +0000118SBAddress::GetLoadAddress (const SBTarget &target) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000119{
Greg Clayton5160ce52013-03-27 23:08:40 +0000120 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Ticeceb6b132010-10-26 03:11:13 +0000121
Greg Claytonaf67cec2010-12-20 20:49:23 +0000122 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
Greg Claytonb9556ac2012-01-30 07:41:31 +0000123 TargetSP target_sp (target.GetSP());
Greg Claytone4c1ef52012-10-22 20:49:35 +0000124 if (target_sp)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000125 {
Jason Molendafca26da2014-10-31 21:30:59 +0000126 if (m_opaque_ap->IsValid())
Greg Claytone4c1ef52012-10-22 20:49:35 +0000127 {
128 Mutex::Locker api_locker (target_sp->GetAPIMutex());
129 addr = m_opaque_ap->GetLoadAddress (target_sp.get());
130 }
Caroline Ticeceb6b132010-10-26 03:11:13 +0000131 }
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000132
Greg Claytonaf67cec2010-12-20 20:49:23 +0000133 if (log)
Caroline Ticeceb6b132010-10-26 03:11:13 +0000134 {
Greg Claytonaf67cec2010-12-20 20:49:23 +0000135 if (addr == LLDB_INVALID_ADDRESS)
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000136 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
137 static_cast<void*>(target_sp.get()));
Greg Claytonaf67cec2010-12-20 20:49:23 +0000138 else
Saleem Abdulrasool324a1032014-04-04 04:06:10 +0000139 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
140 static_cast<void*>(target_sp.get()), addr);
Caroline Ticeceb6b132010-10-26 03:11:13 +0000141 }
Greg Claytonaf67cec2010-12-20 20:49:23 +0000142
143 return addr;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000144}
145
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000146void
147SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
148{
149 // Create the address object if we don't already have one
150 ref();
151 if (target.IsValid())
152 *this = target.ResolveLoadAddress(load_addr);
153 else
Greg Clayton7e8a6012012-04-04 20:36:48 +0000154 m_opaque_ap->Clear();
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000155
156 // Check if we weren't were able to resolve a section offset address.
157 // If we weren't it is ok, the load address might be a location on the
158 // stack or heap, so we should just have an address with no section and
159 // a valid offset
160 if (!m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000161 m_opaque_ap->SetOffset(load_addr);
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000162}
163
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000164bool
165SBAddress::OffsetAddress (addr_t offset)
166{
Jason Molendafca26da2014-10-31 21:30:59 +0000167 if (m_opaque_ap->IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 {
Greg Clayton7e8a6012012-04-04 20:36:48 +0000169 addr_t addr_offset = m_opaque_ap->GetOffset();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000170 if (addr_offset != LLDB_INVALID_ADDRESS)
171 {
Greg Clayton7e8a6012012-04-04 20:36:48 +0000172 m_opaque_ap->SetOffset(addr_offset + offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000173 return true;
174 }
175 }
176 return false;
177}
178
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000179lldb::SBSection
180SBAddress::GetSection ()
181{
182 lldb::SBSection sb_section;
Jason Molendafca26da2014-10-31 21:30:59 +0000183 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000184 sb_section.SetSP (m_opaque_ap->GetSection());
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000185 return sb_section;
186}
187
Greg Clayton13d19502012-01-29 06:07:39 +0000188lldb::addr_t
189SBAddress::GetOffset ()
190{
Jason Molendafca26da2014-10-31 21:30:59 +0000191 if (m_opaque_ap->IsValid())
Greg Claytonbbffdd72012-10-30 16:32:38 +0000192 return m_opaque_ap->GetOffset();
Greg Clayton13d19502012-01-29 06:07:39 +0000193 return 0;
194}
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000195
196Address *
Greg Clayton09960032010-09-10 18:31:35 +0000197SBAddress::operator->()
198{
Greg Clayton7e8a6012012-04-04 20:36:48 +0000199 return m_opaque_ap.get();
Greg Clayton09960032010-09-10 18:31:35 +0000200}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000201
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000202const Address *
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000203SBAddress::operator->() const
204{
Greg Clayton7e8a6012012-04-04 20:36:48 +0000205 return m_opaque_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000206}
207
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000208Address &
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000209SBAddress::ref ()
Greg Clayton09960032010-09-10 18:31:35 +0000210{
211 if (m_opaque_ap.get() == NULL)
Greg Clayton7e8a6012012-04-04 20:36:48 +0000212 m_opaque_ap.reset (new Address());
213 return *m_opaque_ap;
Greg Clayton09960032010-09-10 18:31:35 +0000214}
215
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000216const Address &
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000217SBAddress::ref () const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000218{
Greg Clayton7e8a6012012-04-04 20:36:48 +0000219 // This object should already have checked with "IsValid()"
Greg Clayton00e6fbf2011-07-22 16:46:35 +0000220 // prior to calling this function. In case you didn't we will assert
221 // and die to let you know.
Greg Clayton09960032010-09-10 18:31:35 +0000222 assert (m_opaque_ap.get());
Greg Clayton7e8a6012012-04-04 20:36:48 +0000223 return *m_opaque_ap;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224}
225
Greg Claytoncac9c5f2011-09-24 00:52:29 +0000226Address *
Caroline Tice750cd172010-10-26 23:49:36 +0000227SBAddress::get ()
228{
Greg Clayton7e8a6012012-04-04 20:36:48 +0000229 return m_opaque_ap.get();
Caroline Tice750cd172010-10-26 23:49:36 +0000230}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231
Caroline Ticedde9cff2010-09-20 05:20:02 +0000232bool
233SBAddress::GetDescription (SBStream &description)
234{
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000235 // Call "ref()" on the stream to make sure it creates a backing stream in
236 // case there isn't one already...
Greg Claytonda7bc7d2011-11-13 06:57:31 +0000237 Stream &strm = description.ref();
Jason Molendafca26da2014-10-31 21:30:59 +0000238 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000239 {
240 m_opaque_ap->Dump (&strm,
241 NULL,
242 Address::DumpStyleResolvedDescription,
243 Address::DumpStyleModuleWithFileAddress,
244 4);
245 StreamString sstrm;
246// m_opaque_ap->Dump (&sstrm, NULL, Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 4);
247// if (sstrm.GetData())
248// strm.Printf (" (%s)", sstrm.GetData());
249 }
Caroline Ticedde9cff2010-09-20 05:20:02 +0000250 else
Greg Claytonda7bc7d2011-11-13 06:57:31 +0000251 strm.PutCString ("No value");
Caroline Ticedde9cff2010-09-20 05:20:02 +0000252
253 return true;
254}
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000255
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000256SBModule
257SBAddress::GetModule ()
258{
259 SBModule sb_module;
Jason Molendafca26da2014-10-31 21:30:59 +0000260 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000261 sb_module.SetSP (m_opaque_ap->GetModule());
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000262 return sb_module;
263}
264
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000265SBSymbolContext
266SBAddress::GetSymbolContext (uint32_t resolve_scope)
267{
268 SBSymbolContext sb_sc;
Jason Molendafca26da2014-10-31 21:30:59 +0000269 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000270 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000271 return sb_sc;
272}
273
274SBCompileUnit
275SBAddress::GetCompileUnit ()
276{
277 SBCompileUnit sb_comp_unit;
Jason Molendafca26da2014-10-31 21:30:59 +0000278 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000279 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000280 return sb_comp_unit;
281}
282
283SBFunction
284SBAddress::GetFunction ()
285{
286 SBFunction sb_function;
Jason Molendafca26da2014-10-31 21:30:59 +0000287 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000288 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000289 return sb_function;
290}
291
292SBBlock
293SBAddress::GetBlock ()
294{
295 SBBlock sb_block;
Jason Molendafca26da2014-10-31 21:30:59 +0000296 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000297 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000298 return sb_block;
299}
300
301SBSymbol
302SBAddress::GetSymbol ()
303{
304 SBSymbol sb_symbol;
Jason Molendafca26da2014-10-31 21:30:59 +0000305 if (m_opaque_ap->IsValid())
Greg Clayton7e8a6012012-04-04 20:36:48 +0000306 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000307 return sb_symbol;
308}
309
310SBLineEntry
311SBAddress::GetLineEntry ()
312{
313 SBLineEntry sb_line_entry;
Jason Molendafca26da2014-10-31 21:30:59 +0000314 if (m_opaque_ap->IsValid())
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000315 {
316 LineEntry line_entry;
Greg Clayton7e8a6012012-04-04 20:36:48 +0000317 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
Greg Clayton7e9b1fd2011-08-12 21:40:01 +0000318 sb_line_entry.SetLineEntry (line_entry);
319 }
320 return sb_line_entry;
321}
322
Greg Claytonc8e0c242012-04-13 00:07:34 +0000323AddressClass
324SBAddress::GetAddressClass ()
325{
Jason Molendafca26da2014-10-31 21:30:59 +0000326 if (m_opaque_ap->IsValid())
Greg Claytonc8e0c242012-04-13 00:07:34 +0000327 return m_opaque_ap->GetAddressClass();
328 return eAddressClassInvalid;
329}
Greg Clayton05d2b7f2011-03-31 01:08:07 +0000330