blob: 7d7d1d7789ed7387a2c6b902a7fcab23050469a8 [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 Clayton39f54ea2012-02-04 02:58:17 +0000106
107SBAddress::SBAddress (lldb::SBSection section, lldb::addr_t offset) :
108 m_opaque_ap(new AddressImpl (Address(section.GetSection(), offset)))
109{
110}
111
Greg Claytona3955062011-07-22 16:46:35 +0000112// Create an address by resolving a load address using the supplied target
113SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
114 m_opaque_ap()
115{
116 SetLoadAddress (load_addr, target);
117}
118
119
120
Chris Lattner24943d22010-06-08 16:52:24 +0000121SBAddress::~SBAddress ()
122{
123}
124
125const SBAddress &
126SBAddress::operator = (const SBAddress &rhs)
127{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000128 if (this != &rhs)
129 {
130 if (rhs.IsValid())
131 m_opaque_ap.reset(new AddressImpl(*rhs.m_opaque_ap.get()));
132 else
133 m_opaque_ap.reset();
134 }
Chris Lattner24943d22010-06-08 16:52:24 +0000135 return *this;
136}
137
138bool
139SBAddress::IsValid () const
140{
Greg Clayton63094e02010-06-23 01:19:29 +0000141 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +0000142}
143
144void
Greg Clayton466f6c42010-09-10 18:31:35 +0000145SBAddress::Clear ()
146{
147 m_opaque_ap.reset();
148}
149
150void
Greg Clayton39f54ea2012-02-04 02:58:17 +0000151SBAddress::SetAddress (lldb::SBSection section, lldb::addr_t offset)
152{
153 Address &addr = ref();
154 addr.SetSection (section.GetSection());
155 addr.SetOffset (offset);
156}
157
158
159void
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000160SBAddress::SetAddress (const Address *lldb_object_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000161{
162 if (lldb_object_ptr)
163 {
Greg Clayton63094e02010-06-23 01:19:29 +0000164 if (m_opaque_ap.get())
165 *m_opaque_ap = *lldb_object_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000166 else
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000167 m_opaque_ap.reset (new AddressImpl(*lldb_object_ptr));
Chris Lattner24943d22010-06-08 16:52:24 +0000168 }
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000169 else
170 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000171}
172
173lldb::addr_t
174SBAddress::GetFileAddress () const
175{
Greg Clayton63094e02010-06-23 01:19:29 +0000176 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000177 return m_opaque_ap->GetAddress().GetFileAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000178 else
179 return LLDB_INVALID_ADDRESS;
180}
181
182lldb::addr_t
Greg Claytoneea26402010-09-14 23:36:40 +0000183SBAddress::GetLoadAddress (const SBTarget &target) const
Chris Lattner24943d22010-06-08 16:52:24 +0000184{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000185 LogSP log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000186
Greg Claytonbdcda462010-12-20 20:49:23 +0000187 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
Greg Clayton334d33a2012-01-30 07:41:31 +0000188 TargetSP target_sp (target.GetSP());
Greg Clayton63094e02010-06-23 01:19:29 +0000189 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000190 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000191 Mutex::Locker api_locker (target_sp->GetAPIMutex());
192 addr = m_opaque_ap->GetAddress().GetLoadAddress (target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000193 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000194
195 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000196 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000197 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton334d33a2012-01-30 07:41:31 +0000198 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target_sp.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000199 else
Greg Clayton334d33a2012-01-30 07:41:31 +0000200 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%llx", target_sp.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000201 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000202
203 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000204}
205
Greg Claytona3955062011-07-22 16:46:35 +0000206void
207SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
208{
209 // Create the address object if we don't already have one
210 ref();
211 if (target.IsValid())
212 *this = target.ResolveLoadAddress(load_addr);
213 else
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000214 m_opaque_ap->GetAddress().Clear();
Greg Claytona3955062011-07-22 16:46:35 +0000215
216 // Check if we weren't were able to resolve a section offset address.
217 // If we weren't it is ok, the load address might be a location on the
218 // stack or heap, so we should just have an address with no section and
219 // a valid offset
220 if (!m_opaque_ap->IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000221 m_opaque_ap->GetAddress().SetOffset(load_addr);
Greg Claytona3955062011-07-22 16:46:35 +0000222}
223
Chris Lattner24943d22010-06-08 16:52:24 +0000224bool
225SBAddress::OffsetAddress (addr_t offset)
226{
Greg Clayton63094e02010-06-23 01:19:29 +0000227 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000228 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000229 addr_t addr_offset = m_opaque_ap->GetAddress().GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000230 if (addr_offset != LLDB_INVALID_ADDRESS)
231 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000232 m_opaque_ap->GetAddress().SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000233 return true;
234 }
235 }
236 return false;
237}
238
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000239lldb::SBSection
240SBAddress::GetSection ()
241{
242 lldb::SBSection sb_section;
243 if (m_opaque_ap.get())
244 sb_section.SetSection(m_opaque_ap->GetAddress().GetSection());
245 return sb_section;
246}
247
Greg Clayton1b925202012-01-29 06:07:39 +0000248lldb::addr_t
249SBAddress::GetOffset ()
250{
251 if (m_opaque_ap.get())
252 m_opaque_ap->GetAddress().GetOffset();
253 return 0;
254}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000255
256Address *
Greg Clayton466f6c42010-09-10 18:31:35 +0000257SBAddress::operator->()
258{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000259 if (m_opaque_ap.get())
260 return &m_opaque_ap->GetAddress();
261 return NULL;
Greg Clayton466f6c42010-09-10 18:31:35 +0000262}
Chris Lattner24943d22010-06-08 16:52:24 +0000263
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000264const Address *
Chris Lattner24943d22010-06-08 16:52:24 +0000265SBAddress::operator->() const
266{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000267 if (m_opaque_ap.get())
268 return &m_opaque_ap->GetAddress();
269 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000270}
271
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000272Address &
Greg Claytona3955062011-07-22 16:46:35 +0000273SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000274{
275 if (m_opaque_ap.get() == NULL)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000276 m_opaque_ap.reset (new AddressImpl());
277 return m_opaque_ap->GetAddress();
Greg Clayton466f6c42010-09-10 18:31:35 +0000278}
279
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000280const Address &
Greg Claytona3955062011-07-22 16:46:35 +0000281SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000282{
Greg Claytona3955062011-07-22 16:46:35 +0000283 // "const SBAddress &addr" should already have checked "addr.IsValid()"
284 // prior to calling this function. In case you didn't we will assert
285 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000286 assert (m_opaque_ap.get());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000287 return m_opaque_ap->GetAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000288}
289
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000290Address *
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000291SBAddress::get ()
292{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000293 if (m_opaque_ap.get())
294 return &m_opaque_ap->GetAddress();
295 return NULL;
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000296}
Chris Lattner24943d22010-06-08 16:52:24 +0000297
Caroline Tice98f930f2010-09-20 05:20:02 +0000298bool
299SBAddress::GetDescription (SBStream &description)
300{
Greg Clayton49ce6822010-10-31 03:01:06 +0000301 // Call "ref()" on the stream to make sure it creates a backing stream in
302 // case there isn't one already...
Greg Clayton96154be2011-11-13 06:57:31 +0000303 Stream &strm = description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000304 if (m_opaque_ap.get())
Greg Clayton96154be2011-11-13 06:57:31 +0000305 m_opaque_ap->GetAddress().Dump (&strm, NULL, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleInvalid, 4);
Caroline Tice98f930f2010-09-20 05:20:02 +0000306 else
Greg Clayton96154be2011-11-13 06:57:31 +0000307 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000308
309 return true;
310}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000311
Greg Claytonb0e68d92011-03-31 01:08:07 +0000312SBModule
313SBAddress::GetModule ()
314{
315 SBModule sb_module;
316 if (m_opaque_ap.get())
317 {
Greg Clayton0416bdf2012-01-30 09:04:36 +0000318 sb_module.SetSP (m_opaque_ap->GetModuleSP());
Greg Claytonb0e68d92011-03-31 01:08:07 +0000319 }
320 return sb_module;
321}
322
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000323SBSymbolContext
324SBAddress::GetSymbolContext (uint32_t resolve_scope)
325{
326 SBSymbolContext sb_sc;
327 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000328 m_opaque_ap->GetAddress().CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000329 return sb_sc;
330}
331
332SBCompileUnit
333SBAddress::GetCompileUnit ()
334{
335 SBCompileUnit sb_comp_unit;
336 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000337 sb_comp_unit.reset(m_opaque_ap->GetAddress().CalculateSymbolContextCompileUnit());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000338 return sb_comp_unit;
339}
340
341SBFunction
342SBAddress::GetFunction ()
343{
344 SBFunction sb_function;
345 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000346 sb_function.reset(m_opaque_ap->GetAddress().CalculateSymbolContextFunction());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000347 return sb_function;
348}
349
350SBBlock
351SBAddress::GetBlock ()
352{
353 SBBlock sb_block;
354 if (m_opaque_ap.get())
Greg Clayton7dd5c512012-02-06 01:44:54 +0000355 sb_block.SetPtr(m_opaque_ap->GetAddress().CalculateSymbolContextBlock());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000356 return sb_block;
357}
358
359SBSymbol
360SBAddress::GetSymbol ()
361{
362 SBSymbol sb_symbol;
363 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000364 sb_symbol.reset(m_opaque_ap->GetAddress().CalculateSymbolContextSymbol());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000365 return sb_symbol;
366}
367
368SBLineEntry
369SBAddress::GetLineEntry ()
370{
371 SBLineEntry sb_line_entry;
372 if (m_opaque_ap.get())
373 {
374 LineEntry line_entry;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000375 if (m_opaque_ap->GetAddress().CalculateSymbolContextLineEntry (line_entry))
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000376 sb_line_entry.SetLineEntry (line_entry);
377 }
378 return sb_line_entry;
379}
380
Greg Claytonb0e68d92011-03-31 01:08:07 +0000381