blob: 430e11aa1d493415d9a6fa098d68f3ca88c9026a [file] [log] [blame]
Chris Lattner24943d22010-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 Clayton3e8c25f2011-09-24 00:52:29 +000012#include "lldb/API/SBSection.h"
Caroline Tice98f930f2010-09-20 05:20:02 +000013#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000014#include "lldb/Core/Address.h"
Caroline Tice7826c882010-10-26 03:11:13 +000015#include "lldb/Core/Log.h"
Greg Claytonb0e68d92011-03-31 01:08:07 +000016#include "lldb/Core/Module.h"
Greg Claytonbdcda462010-12-20 20:49:23 +000017#include "lldb/Host/Mutex.h"
18#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000019
Greg Clayton3e8c25f2011-09-24 00:52:29 +000020namespace lldb_private
21{
22 // We need a address implementation to hold onto a reference to the module
23 // since if the module goes away and we have anyone still holding onto a
24 // SBAddress object, we could crash.
25 class AddressImpl
26 {
27 public:
28 AddressImpl () :
29 m_module_sp(),
30 m_address()
31 {
32 }
33
34 AddressImpl (const Address &addr) :
Greg Clayton13d24fb2012-01-29 20:56:30 +000035 m_module_sp (addr.GetModuleSP()),
Greg Clayton3e8c25f2011-09-24 00:52:29 +000036 m_address (addr)
37 {
38 }
39
40 AddressImpl (const AddressImpl &rhs) :
41 m_module_sp (rhs.m_module_sp),
42 m_address (rhs.m_address)
43 {
44 }
45
46 bool
47 IsValid () const
48 {
49 return m_address.IsValid();
50 }
51
52 void
53 operator = (const AddressImpl &rhs)
54 {
55 m_module_sp = rhs.m_module_sp;
56 m_address = rhs.m_address;
57 }
58
59 Address &
60 GetAddress ()
61 {
62 return m_address;
63 }
64
65 Module *
66 GetModule()
67 {
68 return m_module_sp.get();
69 }
70
71 const lldb::ModuleSP &
72 GetModuleSP() const
73 {
74 return m_module_sp;
75 }
76 protected:
77 lldb::ModuleSP m_module_sp;
78 Address m_address;
79 };
80}
81
82
Chris Lattner24943d22010-06-08 16:52:24 +000083using namespace lldb;
Caroline Tice7826c882010-10-26 03:11:13 +000084using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000085
86
87SBAddress::SBAddress () :
Greg Clayton63094e02010-06-23 01:19:29 +000088 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000089{
90}
91
Greg Clayton3e8c25f2011-09-24 00:52:29 +000092SBAddress::SBAddress (const Address *lldb_object_ptr) :
Greg Clayton63094e02010-06-23 01:19:29 +000093 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000094{
95 if (lldb_object_ptr)
Greg Clayton3e8c25f2011-09-24 00:52:29 +000096 m_opaque_ap.reset (new AddressImpl(*lldb_object_ptr));
Chris Lattner24943d22010-06-08 16:52:24 +000097}
98
99SBAddress::SBAddress (const SBAddress &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +0000100 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +0000101{
102 if (rhs.IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000103 m_opaque_ap.reset (new AddressImpl(*rhs.m_opaque_ap.get()));
Chris Lattner24943d22010-06-08 16:52:24 +0000104}
105
Greg Claytona3955062011-07-22 16:46:35 +0000106// Create an address by resolving a load address using the supplied target
107SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
108 m_opaque_ap()
109{
110 SetLoadAddress (load_addr, target);
111}
112
113
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115SBAddress::~SBAddress ()
116{
117}
118
119const SBAddress &
120SBAddress::operator = (const SBAddress &rhs)
121{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000122 if (this != &rhs)
123 {
124 if (rhs.IsValid())
125 m_opaque_ap.reset(new AddressImpl(*rhs.m_opaque_ap.get()));
126 else
127 m_opaque_ap.reset();
128 }
Chris Lattner24943d22010-06-08 16:52:24 +0000129 return *this;
130}
131
132bool
133SBAddress::IsValid () const
134{
Greg Clayton63094e02010-06-23 01:19:29 +0000135 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000136}
137
138void
Greg Clayton466f6c42010-09-10 18:31:35 +0000139SBAddress::Clear ()
140{
141 m_opaque_ap.reset();
142}
143
144void
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000145SBAddress::SetAddress (const Address *lldb_object_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000146{
147 if (lldb_object_ptr)
148 {
Greg Clayton63094e02010-06-23 01:19:29 +0000149 if (m_opaque_ap.get())
150 *m_opaque_ap = *lldb_object_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000151 else
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000152 m_opaque_ap.reset (new AddressImpl(*lldb_object_ptr));
Chris Lattner24943d22010-06-08 16:52:24 +0000153 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000154 else
155 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000156}
157
158lldb::addr_t
159SBAddress::GetFileAddress () const
160{
Greg Clayton63094e02010-06-23 01:19:29 +0000161 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000162 return m_opaque_ap->GetAddress().GetFileAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000163 else
164 return LLDB_INVALID_ADDRESS;
165}
166
167lldb::addr_t
Greg Claytoneea26402010-09-14 23:36:40 +0000168SBAddress::GetLoadAddress (const SBTarget &target) const
Chris Lattner24943d22010-06-08 16:52:24 +0000169{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000170 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000171
Greg Claytonbdcda462010-12-20 20:49:23 +0000172 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
Greg Clayton334d33a2012-01-30 07:41:31 +0000173 TargetSP target_sp (target.GetSP());
Greg Clayton63094e02010-06-23 01:19:29 +0000174 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000175 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000176 Mutex::Locker api_locker (target_sp->GetAPIMutex());
177 addr = m_opaque_ap->GetAddress().GetLoadAddress (target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000178 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000179
180 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000181 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000182 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton334d33a2012-01-30 07:41:31 +0000183 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target_sp.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000184 else
Greg Clayton334d33a2012-01-30 07:41:31 +0000185 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%llx", target_sp.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000186 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000187
188 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000189}
190
Greg Claytona3955062011-07-22 16:46:35 +0000191void
192SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
193{
194 // Create the address object if we don't already have one
195 ref();
196 if (target.IsValid())
197 *this = target.ResolveLoadAddress(load_addr);
198 else
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000199 m_opaque_ap->GetAddress().Clear();
Greg Claytona3955062011-07-22 16:46:35 +0000200
201 // Check if we weren't were able to resolve a section offset address.
202 // If we weren't it is ok, the load address might be a location on the
203 // stack or heap, so we should just have an address with no section and
204 // a valid offset
205 if (!m_opaque_ap->IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000206 m_opaque_ap->GetAddress().SetOffset(load_addr);
Greg Claytona3955062011-07-22 16:46:35 +0000207}
208
Chris Lattner24943d22010-06-08 16:52:24 +0000209bool
210SBAddress::OffsetAddress (addr_t offset)
211{
Greg Clayton63094e02010-06-23 01:19:29 +0000212 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000213 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000214 addr_t addr_offset = m_opaque_ap->GetAddress().GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000215 if (addr_offset != LLDB_INVALID_ADDRESS)
216 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000217 m_opaque_ap->GetAddress().SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000218 return true;
219 }
220 }
221 return false;
222}
223
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000224lldb::SBSection
225SBAddress::GetSection ()
226{
227 lldb::SBSection sb_section;
228 if (m_opaque_ap.get())
229 sb_section.SetSection(m_opaque_ap->GetAddress().GetSection());
230 return sb_section;
231}
232
Greg Clayton1b925202012-01-29 06:07:39 +0000233lldb::addr_t
234SBAddress::GetOffset ()
235{
236 if (m_opaque_ap.get())
237 m_opaque_ap->GetAddress().GetOffset();
238 return 0;
239}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000240
241Address *
Greg Clayton466f6c42010-09-10 18:31:35 +0000242SBAddress::operator->()
243{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000244 if (m_opaque_ap.get())
245 return &m_opaque_ap->GetAddress();
246 return NULL;
Greg Clayton466f6c42010-09-10 18:31:35 +0000247}
Chris Lattner24943d22010-06-08 16:52:24 +0000248
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000249const Address *
Chris Lattner24943d22010-06-08 16:52:24 +0000250SBAddress::operator->() const
251{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000252 if (m_opaque_ap.get())
253 return &m_opaque_ap->GetAddress();
254 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000255}
256
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000257Address &
Greg Claytona3955062011-07-22 16:46:35 +0000258SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000259{
260 if (m_opaque_ap.get() == NULL)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000261 m_opaque_ap.reset (new AddressImpl());
262 return m_opaque_ap->GetAddress();
Greg Clayton466f6c42010-09-10 18:31:35 +0000263}
264
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000265const Address &
Greg Claytona3955062011-07-22 16:46:35 +0000266SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000267{
Greg Claytona3955062011-07-22 16:46:35 +0000268 // "const SBAddress &addr" should already have checked "addr.IsValid()"
269 // prior to calling this function. In case you didn't we will assert
270 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000271 assert (m_opaque_ap.get());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000272 return m_opaque_ap->GetAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000273}
274
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000275Address *
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000276SBAddress::get ()
277{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000278 if (m_opaque_ap.get())
279 return &m_opaque_ap->GetAddress();
280 return NULL;
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000281}
Chris Lattner24943d22010-06-08 16:52:24 +0000282
Caroline Tice98f930f2010-09-20 05:20:02 +0000283bool
284SBAddress::GetDescription (SBStream &description)
285{
Greg Clayton49ce6822010-10-31 03:01:06 +0000286 // Call "ref()" on the stream to make sure it creates a backing stream in
287 // case there isn't one already...
Greg Clayton96154be2011-11-13 06:57:31 +0000288 Stream &strm = description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000289 if (m_opaque_ap.get())
Greg Clayton96154be2011-11-13 06:57:31 +0000290 m_opaque_ap->GetAddress().Dump (&strm, NULL, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleInvalid, 4);
Caroline Tice98f930f2010-09-20 05:20:02 +0000291 else
Greg Clayton96154be2011-11-13 06:57:31 +0000292 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000293
294 return true;
295}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000296
Greg Claytonb0e68d92011-03-31 01:08:07 +0000297SBModule
298SBAddress::GetModule ()
299{
300 SBModule sb_module;
301 if (m_opaque_ap.get())
302 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000303 sb_module.SetSP (m_opaque_ap->GetModuleSP());
Greg Claytonb0e68d92011-03-31 01:08:07 +0000304 }
305 return sb_module;
306}
307
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000308SBSymbolContext
309SBAddress::GetSymbolContext (uint32_t resolve_scope)
310{
311 SBSymbolContext sb_sc;
312 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000313 m_opaque_ap->GetAddress().CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000314 return sb_sc;
315}
316
317SBCompileUnit
318SBAddress::GetCompileUnit ()
319{
320 SBCompileUnit sb_comp_unit;
321 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000322 sb_comp_unit.reset(m_opaque_ap->GetAddress().CalculateSymbolContextCompileUnit());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000323 return sb_comp_unit;
324}
325
326SBFunction
327SBAddress::GetFunction ()
328{
329 SBFunction sb_function;
330 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000331 sb_function.reset(m_opaque_ap->GetAddress().CalculateSymbolContextFunction());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000332 return sb_function;
333}
334
335SBBlock
336SBAddress::GetBlock ()
337{
338 SBBlock sb_block;
339 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000340 sb_block.reset(m_opaque_ap->GetAddress().CalculateSymbolContextBlock());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000341 return sb_block;
342}
343
344SBSymbol
345SBAddress::GetSymbol ()
346{
347 SBSymbol sb_symbol;
348 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000349 sb_symbol.reset(m_opaque_ap->GetAddress().CalculateSymbolContextSymbol());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000350 return sb_symbol;
351}
352
353SBLineEntry
354SBAddress::GetLineEntry ()
355{
356 SBLineEntry sb_line_entry;
357 if (m_opaque_ap.get())
358 {
359 LineEntry line_entry;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000360 if (m_opaque_ap->GetAddress().CalculateSymbolContextLineEntry (line_entry))
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000361 sb_line_entry.SetLineEntry (line_entry);
362 }
363 return sb_line_entry;
364}
365
Greg Claytonb0e68d92011-03-31 01:08:07 +0000366