blob: 9066bb448fbfcaeb0a1a78a5c5fb6d730a6b1cf6 [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) :
35 m_module_sp (addr.GetModule()),
36 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 Clayton63094e02010-06-23 01:19:29 +0000173 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000174 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000175 Mutex::Locker api_locker (target->GetAPIMutex());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000176 addr = m_opaque_ap->GetAddress().GetLoadAddress (target.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000177 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000178
179 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000180 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000181 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton49ce6822010-10-31 03:01:06 +0000182 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000183 else
184 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%llx", target.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000185 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000186
187 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000188}
189
Greg Claytona3955062011-07-22 16:46:35 +0000190void
191SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
192{
193 // Create the address object if we don't already have one
194 ref();
195 if (target.IsValid())
196 *this = target.ResolveLoadAddress(load_addr);
197 else
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000198 m_opaque_ap->GetAddress().Clear();
Greg Claytona3955062011-07-22 16:46:35 +0000199
200 // Check if we weren't were able to resolve a section offset address.
201 // If we weren't it is ok, the load address might be a location on the
202 // stack or heap, so we should just have an address with no section and
203 // a valid offset
204 if (!m_opaque_ap->IsValid())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000205 m_opaque_ap->GetAddress().SetOffset(load_addr);
Greg Claytona3955062011-07-22 16:46:35 +0000206}
207
Chris Lattner24943d22010-06-08 16:52:24 +0000208bool
209SBAddress::OffsetAddress (addr_t offset)
210{
Greg Clayton63094e02010-06-23 01:19:29 +0000211 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000212 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000213 addr_t addr_offset = m_opaque_ap->GetAddress().GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000214 if (addr_offset != LLDB_INVALID_ADDRESS)
215 {
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000216 m_opaque_ap->GetAddress().SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000217 return true;
218 }
219 }
220 return false;
221}
222
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000223lldb::SBSection
224SBAddress::GetSection ()
225{
226 lldb::SBSection sb_section;
227 if (m_opaque_ap.get())
228 sb_section.SetSection(m_opaque_ap->GetAddress().GetSection());
229 return sb_section;
230}
231
232
233Address *
Greg Clayton466f6c42010-09-10 18:31:35 +0000234SBAddress::operator->()
235{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000236 if (m_opaque_ap.get())
237 return &m_opaque_ap->GetAddress();
238 return NULL;
Greg Clayton466f6c42010-09-10 18:31:35 +0000239}
Chris Lattner24943d22010-06-08 16:52:24 +0000240
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000241const Address *
Chris Lattner24943d22010-06-08 16:52:24 +0000242SBAddress::operator->() const
243{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000244 if (m_opaque_ap.get())
245 return &m_opaque_ap->GetAddress();
246 return NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000247}
248
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000249Address &
Greg Claytona3955062011-07-22 16:46:35 +0000250SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000251{
252 if (m_opaque_ap.get() == NULL)
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000253 m_opaque_ap.reset (new AddressImpl());
254 return m_opaque_ap->GetAddress();
Greg Clayton466f6c42010-09-10 18:31:35 +0000255}
256
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000257const Address &
Greg Claytona3955062011-07-22 16:46:35 +0000258SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000259{
Greg Claytona3955062011-07-22 16:46:35 +0000260 // "const SBAddress &addr" should already have checked "addr.IsValid()"
261 // prior to calling this function. In case you didn't we will assert
262 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000263 assert (m_opaque_ap.get());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000264 return m_opaque_ap->GetAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000265}
266
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000267Address *
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000268SBAddress::get ()
269{
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000270 if (m_opaque_ap.get())
271 return &m_opaque_ap->GetAddress();
272 return NULL;
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000273}
Chris Lattner24943d22010-06-08 16:52:24 +0000274
Caroline Tice98f930f2010-09-20 05:20:02 +0000275bool
276SBAddress::GetDescription (SBStream &description)
277{
Greg Clayton49ce6822010-10-31 03:01:06 +0000278 // Call "ref()" on the stream to make sure it creates a backing stream in
279 // case there isn't one already...
Caroline Ticee49ec182010-09-22 23:01:29 +0000280 description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000281 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000282 m_opaque_ap->GetAddress().Dump (description.get(), NULL, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleInvalid, 4);
Caroline Tice98f930f2010-09-20 05:20:02 +0000283 else
284 description.Printf ("No value");
285
286 return true;
287}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000288
Greg Claytonb0e68d92011-03-31 01:08:07 +0000289SBModule
290SBAddress::GetModule ()
291{
292 SBModule sb_module;
293 if (m_opaque_ap.get())
294 {
Greg Clayton02e210c2011-09-17 07:23:18 +0000295 Module *module = m_opaque_ap->GetModule();
Greg Claytonb0e68d92011-03-31 01:08:07 +0000296 if (module)
Greg Clayton02e210c2011-09-17 07:23:18 +0000297 *sb_module = module;
Greg Claytonb0e68d92011-03-31 01:08:07 +0000298 }
299 return sb_module;
300}
301
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000302SBSymbolContext
303SBAddress::GetSymbolContext (uint32_t resolve_scope)
304{
305 SBSymbolContext sb_sc;
306 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000307 m_opaque_ap->GetAddress().CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000308 return sb_sc;
309}
310
311SBCompileUnit
312SBAddress::GetCompileUnit ()
313{
314 SBCompileUnit sb_comp_unit;
315 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000316 sb_comp_unit.reset(m_opaque_ap->GetAddress().CalculateSymbolContextCompileUnit());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000317 return sb_comp_unit;
318}
319
320SBFunction
321SBAddress::GetFunction ()
322{
323 SBFunction sb_function;
324 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000325 sb_function.reset(m_opaque_ap->GetAddress().CalculateSymbolContextFunction());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000326 return sb_function;
327}
328
329SBBlock
330SBAddress::GetBlock ()
331{
332 SBBlock sb_block;
333 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000334 sb_block.reset(m_opaque_ap->GetAddress().CalculateSymbolContextBlock());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000335 return sb_block;
336}
337
338SBSymbol
339SBAddress::GetSymbol ()
340{
341 SBSymbol sb_symbol;
342 if (m_opaque_ap.get())
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000343 sb_symbol.reset(m_opaque_ap->GetAddress().CalculateSymbolContextSymbol());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000344 return sb_symbol;
345}
346
347SBLineEntry
348SBAddress::GetLineEntry ()
349{
350 SBLineEntry sb_line_entry;
351 if (m_opaque_ap.get())
352 {
353 LineEntry line_entry;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000354 if (m_opaque_ap->GetAddress().CalculateSymbolContextLineEntry (line_entry))
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000355 sb_line_entry.SetLineEntry (line_entry);
356 }
357 return sb_line_entry;
358}
359
Greg Claytonb0e68d92011-03-31 01:08:07 +0000360