blob: 799c909076342c258fcd6e0d9ddd6876d5cc9daa [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 +000020
Chris Lattner24943d22010-06-08 16:52:24 +000021using namespace lldb;
Caroline Tice7826c882010-10-26 03:11:13 +000022using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000023
24
25SBAddress::SBAddress () :
Greg Clayton63094e02010-06-23 01:19:29 +000026 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000027{
28}
29
Greg Clayton3e8c25f2011-09-24 00:52:29 +000030SBAddress::SBAddress (const Address *lldb_object_ptr) :
Greg Clayton63094e02010-06-23 01:19:29 +000031 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000032{
33 if (lldb_object_ptr)
Greg Clayton69c540d2012-04-04 20:36:48 +000034 ref() = *lldb_object_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +000035}
36
37SBAddress::SBAddress (const SBAddress &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000038 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000039{
40 if (rhs.IsValid())
Greg Clayton69c540d2012-04-04 20:36:48 +000041 ref() = rhs.ref();
Chris Lattner24943d22010-06-08 16:52:24 +000042}
43
Greg Clayton39f54ea2012-02-04 02:58:17 +000044
45SBAddress::SBAddress (lldb::SBSection section, lldb::addr_t offset) :
Greg Clayton69c540d2012-04-04 20:36:48 +000046 m_opaque_ap(new Address (section.GetSP(), offset))
Greg Clayton39f54ea2012-02-04 02:58:17 +000047{
48}
49
Greg Claytona3955062011-07-22 16:46:35 +000050// Create an address by resolving a load address using the supplied target
51SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
52 m_opaque_ap()
53{
54 SetLoadAddress (load_addr, target);
55}
56
57
58
Chris Lattner24943d22010-06-08 16:52:24 +000059SBAddress::~SBAddress ()
60{
61}
62
63const SBAddress &
64SBAddress::operator = (const SBAddress &rhs)
65{
Greg Clayton3e8c25f2011-09-24 00:52:29 +000066 if (this != &rhs)
67 {
68 if (rhs.IsValid())
Greg Clayton69c540d2012-04-04 20:36:48 +000069 ref() = rhs.ref();
Greg Clayton3e8c25f2011-09-24 00:52:29 +000070 else
71 m_opaque_ap.reset();
72 }
Chris Lattner24943d22010-06-08 16:52:24 +000073 return *this;
74}
75
76bool
77SBAddress::IsValid () const
78{
Greg Clayton63094e02010-06-23 01:19:29 +000079 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +000080}
81
82void
Greg Clayton466f6c42010-09-10 18:31:35 +000083SBAddress::Clear ()
84{
85 m_opaque_ap.reset();
86}
87
88void
Greg Clayton39f54ea2012-02-04 02:58:17 +000089SBAddress::SetAddress (lldb::SBSection section, lldb::addr_t offset)
90{
91 Address &addr = ref();
Greg Clayton3508c382012-02-24 01:59:29 +000092 addr.SetSection (section.GetSP());
Greg Clayton39f54ea2012-02-04 02:58:17 +000093 addr.SetOffset (offset);
94}
95
96
97void
Greg Clayton3e8c25f2011-09-24 00:52:29 +000098SBAddress::SetAddress (const Address *lldb_object_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +000099{
100 if (lldb_object_ptr)
Greg Clayton69c540d2012-04-04 20:36:48 +0000101 ref() = *lldb_object_ptr;
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000102 else
103 m_opaque_ap.reset();
Chris Lattner24943d22010-06-08 16:52:24 +0000104}
105
106lldb::addr_t
107SBAddress::GetFileAddress () const
108{
Greg Clayton63094e02010-06-23 01:19:29 +0000109 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000110 return m_opaque_ap->GetFileAddress();
Chris Lattner24943d22010-06-08 16:52:24 +0000111 else
112 return LLDB_INVALID_ADDRESS;
113}
114
115lldb::addr_t
Greg Claytoneea26402010-09-14 23:36:40 +0000116SBAddress::GetLoadAddress (const SBTarget &target) const
Chris Lattner24943d22010-06-08 16:52:24 +0000117{
Greg Clayton952e9dc2013-03-27 23:08:40 +0000118 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000119
Greg Claytonbdcda462010-12-20 20:49:23 +0000120 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
Greg Clayton334d33a2012-01-30 07:41:31 +0000121 TargetSP target_sp (target.GetSP());
Greg Clayton6837dfd2012-10-22 20:49:35 +0000122 if (target_sp)
Caroline Tice7826c882010-10-26 03:11:13 +0000123 {
Greg Clayton6837dfd2012-10-22 20:49:35 +0000124 if (m_opaque_ap.get())
125 {
126 Mutex::Locker api_locker (target_sp->GetAPIMutex());
127 addr = m_opaque_ap->GetLoadAddress (target_sp.get());
128 }
Caroline Tice7826c882010-10-26 03:11:13 +0000129 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000130
131 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000132 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000133 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton334d33a2012-01-30 07:41:31 +0000134 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target_sp.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000135 else
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000136 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, target_sp.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000137 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000138
139 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000140}
141
Greg Claytona3955062011-07-22 16:46:35 +0000142void
143SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
144{
145 // Create the address object if we don't already have one
146 ref();
147 if (target.IsValid())
148 *this = target.ResolveLoadAddress(load_addr);
149 else
Greg Clayton69c540d2012-04-04 20:36:48 +0000150 m_opaque_ap->Clear();
Greg Claytona3955062011-07-22 16:46:35 +0000151
152 // Check if we weren't were able to resolve a section offset address.
153 // If we weren't it is ok, the load address might be a location on the
154 // stack or heap, so we should just have an address with no section and
155 // a valid offset
156 if (!m_opaque_ap->IsValid())
Greg Clayton69c540d2012-04-04 20:36:48 +0000157 m_opaque_ap->SetOffset(load_addr);
Greg Claytona3955062011-07-22 16:46:35 +0000158}
159
Chris Lattner24943d22010-06-08 16:52:24 +0000160bool
161SBAddress::OffsetAddress (addr_t offset)
162{
Greg Clayton63094e02010-06-23 01:19:29 +0000163 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000164 {
Greg Clayton69c540d2012-04-04 20:36:48 +0000165 addr_t addr_offset = m_opaque_ap->GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000166 if (addr_offset != LLDB_INVALID_ADDRESS)
167 {
Greg Clayton69c540d2012-04-04 20:36:48 +0000168 m_opaque_ap->SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000169 return true;
170 }
171 }
172 return false;
173}
174
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000175lldb::SBSection
176SBAddress::GetSection ()
177{
178 lldb::SBSection sb_section;
179 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000180 sb_section.SetSP (m_opaque_ap->GetSection());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000181 return sb_section;
182}
183
Greg Clayton1b925202012-01-29 06:07:39 +0000184lldb::addr_t
185SBAddress::GetOffset ()
186{
187 if (m_opaque_ap.get())
Greg Claytonaaf00882012-10-30 16:32:38 +0000188 return m_opaque_ap->GetOffset();
Greg Clayton1b925202012-01-29 06:07:39 +0000189 return 0;
190}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000191
192Address *
Greg Clayton466f6c42010-09-10 18:31:35 +0000193SBAddress::operator->()
194{
Greg Clayton69c540d2012-04-04 20:36:48 +0000195 return m_opaque_ap.get();
Greg Clayton466f6c42010-09-10 18:31:35 +0000196}
Chris Lattner24943d22010-06-08 16:52:24 +0000197
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000198const Address *
Chris Lattner24943d22010-06-08 16:52:24 +0000199SBAddress::operator->() const
200{
Greg Clayton69c540d2012-04-04 20:36:48 +0000201 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000202}
203
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000204Address &
Greg Claytona3955062011-07-22 16:46:35 +0000205SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000206{
207 if (m_opaque_ap.get() == NULL)
Greg Clayton69c540d2012-04-04 20:36:48 +0000208 m_opaque_ap.reset (new Address());
209 return *m_opaque_ap;
Greg Clayton466f6c42010-09-10 18:31:35 +0000210}
211
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000212const Address &
Greg Claytona3955062011-07-22 16:46:35 +0000213SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000214{
Greg Clayton69c540d2012-04-04 20:36:48 +0000215 // This object should already have checked with "IsValid()"
Greg Claytona3955062011-07-22 16:46:35 +0000216 // prior to calling this function. In case you didn't we will assert
217 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000218 assert (m_opaque_ap.get());
Greg Clayton69c540d2012-04-04 20:36:48 +0000219 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000220}
221
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000222Address *
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000223SBAddress::get ()
224{
Greg Clayton69c540d2012-04-04 20:36:48 +0000225 return m_opaque_ap.get();
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000226}
Chris Lattner24943d22010-06-08 16:52:24 +0000227
Caroline Tice98f930f2010-09-20 05:20:02 +0000228bool
229SBAddress::GetDescription (SBStream &description)
230{
Greg Clayton49ce6822010-10-31 03:01:06 +0000231 // Call "ref()" on the stream to make sure it creates a backing stream in
232 // case there isn't one already...
Greg Clayton96154be2011-11-13 06:57:31 +0000233 Stream &strm = description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000234 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000235 {
236 m_opaque_ap->Dump (&strm,
237 NULL,
238 Address::DumpStyleResolvedDescription,
239 Address::DumpStyleModuleWithFileAddress,
240 4);
241 StreamString sstrm;
242// m_opaque_ap->Dump (&sstrm, NULL, Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 4);
243// if (sstrm.GetData())
244// strm.Printf (" (%s)", sstrm.GetData());
245 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000246 else
Greg Clayton96154be2011-11-13 06:57:31 +0000247 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000248
249 return true;
250}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000251
Greg Claytonb0e68d92011-03-31 01:08:07 +0000252SBModule
253SBAddress::GetModule ()
254{
255 SBModule sb_module;
256 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000257 sb_module.SetSP (m_opaque_ap->GetModule());
Greg Claytonb0e68d92011-03-31 01:08:07 +0000258 return sb_module;
259}
260
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000261SBSymbolContext
262SBAddress::GetSymbolContext (uint32_t resolve_scope)
263{
264 SBSymbolContext sb_sc;
265 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000266 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000267 return sb_sc;
268}
269
270SBCompileUnit
271SBAddress::GetCompileUnit ()
272{
273 SBCompileUnit sb_comp_unit;
274 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000275 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000276 return sb_comp_unit;
277}
278
279SBFunction
280SBAddress::GetFunction ()
281{
282 SBFunction sb_function;
283 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000284 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000285 return sb_function;
286}
287
288SBBlock
289SBAddress::GetBlock ()
290{
291 SBBlock sb_block;
292 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000293 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000294 return sb_block;
295}
296
297SBSymbol
298SBAddress::GetSymbol ()
299{
300 SBSymbol sb_symbol;
301 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000302 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000303 return sb_symbol;
304}
305
306SBLineEntry
307SBAddress::GetLineEntry ()
308{
309 SBLineEntry sb_line_entry;
310 if (m_opaque_ap.get())
311 {
312 LineEntry line_entry;
Greg Clayton69c540d2012-04-04 20:36:48 +0000313 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000314 sb_line_entry.SetLineEntry (line_entry);
315 }
316 return sb_line_entry;
317}
318
Greg Clayton7fb14302012-04-13 00:07:34 +0000319AddressClass
320SBAddress::GetAddressClass ()
321{
322 if (m_opaque_ap.get())
323 return m_opaque_ap->GetAddressClass();
324 return eAddressClassInvalid;
325}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000326