blob: 8e7560930db63b5300b40a653aba8c65132abd66 [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 Clayton3e8c25f2011-09-24 00:52:29 +0000118 LogSP 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 Clayton63094e02010-06-23 01:19:29 +0000122 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000123 {
Greg Clayton334d33a2012-01-30 07:41:31 +0000124 Mutex::Locker api_locker (target_sp->GetAPIMutex());
Greg Clayton69c540d2012-04-04 20:36:48 +0000125 addr = m_opaque_ap->GetLoadAddress (target_sp.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000126 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000127
128 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000129 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000130 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton334d33a2012-01-30 07:41:31 +0000131 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target_sp.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000132 else
Greg Clayton334d33a2012-01-30 07:41:31 +0000133 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%llx", target_sp.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000134 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000135
136 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000137}
138
Greg Claytona3955062011-07-22 16:46:35 +0000139void
140SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
141{
142 // Create the address object if we don't already have one
143 ref();
144 if (target.IsValid())
145 *this = target.ResolveLoadAddress(load_addr);
146 else
Greg Clayton69c540d2012-04-04 20:36:48 +0000147 m_opaque_ap->Clear();
Greg Claytona3955062011-07-22 16:46:35 +0000148
149 // Check if we weren't were able to resolve a section offset address.
150 // If we weren't it is ok, the load address might be a location on the
151 // stack or heap, so we should just have an address with no section and
152 // a valid offset
153 if (!m_opaque_ap->IsValid())
Greg Clayton69c540d2012-04-04 20:36:48 +0000154 m_opaque_ap->SetOffset(load_addr);
Greg Claytona3955062011-07-22 16:46:35 +0000155}
156
Chris Lattner24943d22010-06-08 16:52:24 +0000157bool
158SBAddress::OffsetAddress (addr_t offset)
159{
Greg Clayton63094e02010-06-23 01:19:29 +0000160 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000161 {
Greg Clayton69c540d2012-04-04 20:36:48 +0000162 addr_t addr_offset = m_opaque_ap->GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000163 if (addr_offset != LLDB_INVALID_ADDRESS)
164 {
Greg Clayton69c540d2012-04-04 20:36:48 +0000165 m_opaque_ap->SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000166 return true;
167 }
168 }
169 return false;
170}
171
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000172lldb::SBSection
173SBAddress::GetSection ()
174{
175 lldb::SBSection sb_section;
176 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000177 sb_section.SetSP (m_opaque_ap->GetSection());
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000178 return sb_section;
179}
180
Greg Clayton1b925202012-01-29 06:07:39 +0000181lldb::addr_t
182SBAddress::GetOffset ()
183{
184 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000185 m_opaque_ap->GetOffset();
Greg Clayton1b925202012-01-29 06:07:39 +0000186 return 0;
187}
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000188
189Address *
Greg Clayton466f6c42010-09-10 18:31:35 +0000190SBAddress::operator->()
191{
Greg Clayton69c540d2012-04-04 20:36:48 +0000192 return m_opaque_ap.get();
Greg Clayton466f6c42010-09-10 18:31:35 +0000193}
Chris Lattner24943d22010-06-08 16:52:24 +0000194
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000195const Address *
Chris Lattner24943d22010-06-08 16:52:24 +0000196SBAddress::operator->() const
197{
Greg Clayton69c540d2012-04-04 20:36:48 +0000198 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000199}
200
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000201Address &
Greg Claytona3955062011-07-22 16:46:35 +0000202SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000203{
204 if (m_opaque_ap.get() == NULL)
Greg Clayton69c540d2012-04-04 20:36:48 +0000205 m_opaque_ap.reset (new Address());
206 return *m_opaque_ap;
Greg Clayton466f6c42010-09-10 18:31:35 +0000207}
208
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000209const Address &
Greg Claytona3955062011-07-22 16:46:35 +0000210SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000211{
Greg Clayton69c540d2012-04-04 20:36:48 +0000212 // This object should already have checked with "IsValid()"
Greg Claytona3955062011-07-22 16:46:35 +0000213 // prior to calling this function. In case you didn't we will assert
214 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000215 assert (m_opaque_ap.get());
Greg Clayton69c540d2012-04-04 20:36:48 +0000216 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000217}
218
Greg Clayton3e8c25f2011-09-24 00:52:29 +0000219Address *
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000220SBAddress::get ()
221{
Greg Clayton69c540d2012-04-04 20:36:48 +0000222 return m_opaque_ap.get();
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000223}
Chris Lattner24943d22010-06-08 16:52:24 +0000224
Caroline Tice98f930f2010-09-20 05:20:02 +0000225bool
226SBAddress::GetDescription (SBStream &description)
227{
Greg Clayton49ce6822010-10-31 03:01:06 +0000228 // Call "ref()" on the stream to make sure it creates a backing stream in
229 // case there isn't one already...
Greg Clayton96154be2011-11-13 06:57:31 +0000230 Stream &strm = description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000231 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000232 {
233 m_opaque_ap->Dump (&strm,
234 NULL,
235 Address::DumpStyleResolvedDescription,
236 Address::DumpStyleModuleWithFileAddress,
237 4);
238 StreamString sstrm;
239// m_opaque_ap->Dump (&sstrm, NULL, Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 4);
240// if (sstrm.GetData())
241// strm.Printf (" (%s)", sstrm.GetData());
242 }
Caroline Tice98f930f2010-09-20 05:20:02 +0000243 else
Greg Clayton96154be2011-11-13 06:57:31 +0000244 strm.PutCString ("No value");
Caroline Tice98f930f2010-09-20 05:20:02 +0000245
246 return true;
247}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000248
Greg Claytonb0e68d92011-03-31 01:08:07 +0000249SBModule
250SBAddress::GetModule ()
251{
252 SBModule sb_module;
253 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000254 sb_module.SetSP (m_opaque_ap->GetModule());
Greg Claytonb0e68d92011-03-31 01:08:07 +0000255 return sb_module;
256}
257
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000258SBSymbolContext
259SBAddress::GetSymbolContext (uint32_t resolve_scope)
260{
261 SBSymbolContext sb_sc;
262 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000263 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000264 return sb_sc;
265}
266
267SBCompileUnit
268SBAddress::GetCompileUnit ()
269{
270 SBCompileUnit sb_comp_unit;
271 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000272 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000273 return sb_comp_unit;
274}
275
276SBFunction
277SBAddress::GetFunction ()
278{
279 SBFunction sb_function;
280 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000281 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000282 return sb_function;
283}
284
285SBBlock
286SBAddress::GetBlock ()
287{
288 SBBlock sb_block;
289 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000290 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000291 return sb_block;
292}
293
294SBSymbol
295SBAddress::GetSymbol ()
296{
297 SBSymbol sb_symbol;
298 if (m_opaque_ap.get())
Greg Clayton69c540d2012-04-04 20:36:48 +0000299 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000300 return sb_symbol;
301}
302
303SBLineEntry
304SBAddress::GetLineEntry ()
305{
306 SBLineEntry sb_line_entry;
307 if (m_opaque_ap.get())
308 {
309 LineEntry line_entry;
Greg Clayton69c540d2012-04-04 20:36:48 +0000310 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000311 sb_line_entry.SetLineEntry (line_entry);
312 }
313 return sb_line_entry;
314}
315
Greg Claytonb0e68d92011-03-31 01:08:07 +0000316