blob: e4161a2bebcd6cf172b73972d725db5062bac5bc [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"
Caroline Tice98f930f2010-09-20 05:20:02 +000012#include "lldb/API/SBStream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000013#include "lldb/Core/Address.h"
Caroline Tice7826c882010-10-26 03:11:13 +000014#include "lldb/Core/Log.h"
Greg Claytonb0e68d92011-03-31 01:08:07 +000015#include "lldb/Core/Module.h"
Greg Claytonbdcda462010-12-20 20:49:23 +000016#include "lldb/Host/Mutex.h"
17#include "lldb/Target/Target.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018
19using namespace lldb;
Caroline Tice7826c882010-10-26 03:11:13 +000020using namespace lldb_private;
Chris Lattner24943d22010-06-08 16:52:24 +000021
22
23SBAddress::SBAddress () :
Greg Clayton63094e02010-06-23 01:19:29 +000024 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000025{
26}
27
28SBAddress::SBAddress (const lldb_private::Address *lldb_object_ptr) :
Greg Clayton63094e02010-06-23 01:19:29 +000029 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000030{
31 if (lldb_object_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000032 m_opaque_ap.reset (new lldb_private::Address(*lldb_object_ptr));
Chris Lattner24943d22010-06-08 16:52:24 +000033}
34
35SBAddress::SBAddress (const SBAddress &rhs) :
Greg Clayton63094e02010-06-23 01:19:29 +000036 m_opaque_ap ()
Chris Lattner24943d22010-06-08 16:52:24 +000037{
38 if (rhs.IsValid())
Greg Clayton63094e02010-06-23 01:19:29 +000039 m_opaque_ap.reset (new lldb_private::Address(*rhs.m_opaque_ap.get()));
Chris Lattner24943d22010-06-08 16:52:24 +000040}
41
Greg Claytona3955062011-07-22 16:46:35 +000042// Create an address by resolving a load address using the supplied target
43SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
44 m_opaque_ap()
45{
46 SetLoadAddress (load_addr, target);
47}
48
49
50
Chris Lattner24943d22010-06-08 16:52:24 +000051SBAddress::~SBAddress ()
52{
53}
54
55const SBAddress &
56SBAddress::operator = (const SBAddress &rhs)
57{
Greg Clayton49ce6822010-10-31 03:01:06 +000058 if (this != &rhs && rhs.IsValid())
59 m_opaque_ap.reset (new lldb_private::Address(*rhs.m_opaque_ap.get()));
Chris Lattner24943d22010-06-08 16:52:24 +000060 return *this;
61}
62
63bool
64SBAddress::IsValid () const
65{
Greg Clayton63094e02010-06-23 01:19:29 +000066 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
Chris Lattner24943d22010-06-08 16:52:24 +000067}
68
69void
Greg Clayton466f6c42010-09-10 18:31:35 +000070SBAddress::Clear ()
71{
72 m_opaque_ap.reset();
73}
74
75void
Chris Lattner24943d22010-06-08 16:52:24 +000076SBAddress::SetAddress (const lldb_private::Address *lldb_object_ptr)
77{
78 if (lldb_object_ptr)
79 {
Greg Clayton63094e02010-06-23 01:19:29 +000080 if (m_opaque_ap.get())
81 *m_opaque_ap = *lldb_object_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +000082 else
Greg Clayton63094e02010-06-23 01:19:29 +000083 m_opaque_ap.reset (new lldb_private::Address(*lldb_object_ptr));
Chris Lattner24943d22010-06-08 16:52:24 +000084 return;
85 }
Greg Clayton63094e02010-06-23 01:19:29 +000086 if (m_opaque_ap.get())
87 m_opaque_ap->Clear();
Chris Lattner24943d22010-06-08 16:52:24 +000088}
89
90lldb::addr_t
91SBAddress::GetFileAddress () const
92{
Greg Clayton63094e02010-06-23 01:19:29 +000093 if (m_opaque_ap.get())
94 return m_opaque_ap->GetFileAddress();
Chris Lattner24943d22010-06-08 16:52:24 +000095 else
96 return LLDB_INVALID_ADDRESS;
97}
98
99lldb::addr_t
Greg Claytoneea26402010-09-14 23:36:40 +0000100SBAddress::GetLoadAddress (const SBTarget &target) const
Chris Lattner24943d22010-06-08 16:52:24 +0000101{
Greg Claytone005f2c2010-11-06 01:53:30 +0000102 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000103
Greg Claytonbdcda462010-12-20 20:49:23 +0000104 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
Greg Clayton63094e02010-06-23 01:19:29 +0000105 if (m_opaque_ap.get())
Caroline Tice7826c882010-10-26 03:11:13 +0000106 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000107 Mutex::Locker api_locker (target->GetAPIMutex());
108 addr = m_opaque_ap->GetLoadAddress (target.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000109 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000110
111 if (log)
Caroline Tice7826c882010-10-26 03:11:13 +0000112 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000113 if (addr == LLDB_INVALID_ADDRESS)
Greg Clayton49ce6822010-10-31 03:01:06 +0000114 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target.get());
Greg Claytonbdcda462010-12-20 20:49:23 +0000115 else
116 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%llx", target.get(), addr);
Caroline Tice7826c882010-10-26 03:11:13 +0000117 }
Greg Claytonbdcda462010-12-20 20:49:23 +0000118
119 return addr;
Chris Lattner24943d22010-06-08 16:52:24 +0000120}
121
Greg Claytona3955062011-07-22 16:46:35 +0000122void
123SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
124{
125 // Create the address object if we don't already have one
126 ref();
127 if (target.IsValid())
128 *this = target.ResolveLoadAddress(load_addr);
129 else
130 m_opaque_ap->Clear();
131
132 // Check if we weren't were able to resolve a section offset address.
133 // If we weren't it is ok, the load address might be a location on the
134 // stack or heap, so we should just have an address with no section and
135 // a valid offset
136 if (!m_opaque_ap->IsValid())
137 m_opaque_ap->SetOffset(load_addr);
138}
139
Chris Lattner24943d22010-06-08 16:52:24 +0000140bool
141SBAddress::OffsetAddress (addr_t offset)
142{
Greg Clayton63094e02010-06-23 01:19:29 +0000143 if (m_opaque_ap.get())
Chris Lattner24943d22010-06-08 16:52:24 +0000144 {
Greg Clayton63094e02010-06-23 01:19:29 +0000145 addr_t addr_offset = m_opaque_ap->GetOffset();
Chris Lattner24943d22010-06-08 16:52:24 +0000146 if (addr_offset != LLDB_INVALID_ADDRESS)
147 {
Greg Clayton63094e02010-06-23 01:19:29 +0000148 m_opaque_ap->SetOffset(addr_offset + offset);
Chris Lattner24943d22010-06-08 16:52:24 +0000149 return true;
150 }
151 }
152 return false;
153}
154
Greg Clayton466f6c42010-09-10 18:31:35 +0000155lldb_private::Address *
156SBAddress::operator->()
157{
158 return m_opaque_ap.get();
159}
Chris Lattner24943d22010-06-08 16:52:24 +0000160
161const lldb_private::Address *
162SBAddress::operator->() const
163{
Greg Clayton63094e02010-06-23 01:19:29 +0000164 return m_opaque_ap.get();
Chris Lattner24943d22010-06-08 16:52:24 +0000165}
166
Greg Clayton466f6c42010-09-10 18:31:35 +0000167lldb_private::Address &
Greg Claytona3955062011-07-22 16:46:35 +0000168SBAddress::ref ()
Greg Clayton466f6c42010-09-10 18:31:35 +0000169{
170 if (m_opaque_ap.get() == NULL)
171 m_opaque_ap.reset (new lldb_private::Address);
172 return *m_opaque_ap;
173}
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175const lldb_private::Address &
Greg Claytona3955062011-07-22 16:46:35 +0000176SBAddress::ref () const
Chris Lattner24943d22010-06-08 16:52:24 +0000177{
Greg Claytona3955062011-07-22 16:46:35 +0000178 // "const SBAddress &addr" should already have checked "addr.IsValid()"
179 // prior to calling this function. In case you didn't we will assert
180 // and die to let you know.
Greg Clayton466f6c42010-09-10 18:31:35 +0000181 assert (m_opaque_ap.get());
Greg Clayton63094e02010-06-23 01:19:29 +0000182 return *m_opaque_ap;
Chris Lattner24943d22010-06-08 16:52:24 +0000183}
184
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000185lldb_private::Address *
186SBAddress::get ()
187{
188 return m_opaque_ap.get();
189}
Chris Lattner24943d22010-06-08 16:52:24 +0000190
Caroline Tice98f930f2010-09-20 05:20:02 +0000191bool
192SBAddress::GetDescription (SBStream &description)
193{
Greg Clayton49ce6822010-10-31 03:01:06 +0000194 // Call "ref()" on the stream to make sure it creates a backing stream in
195 // case there isn't one already...
Caroline Ticee49ec182010-09-22 23:01:29 +0000196 description.ref();
Caroline Tice98f930f2010-09-20 05:20:02 +0000197 if (m_opaque_ap.get())
Greg Clayton49ce6822010-10-31 03:01:06 +0000198 m_opaque_ap->Dump (description.get(), NULL, Address::DumpStyleModuleWithFileAddress, Address::DumpStyleInvalid, 4);
Caroline Tice98f930f2010-09-20 05:20:02 +0000199 else
200 description.Printf ("No value");
201
202 return true;
203}
Greg Claytonb0e68d92011-03-31 01:08:07 +0000204
205SectionType
206SBAddress::GetSectionType ()
207{
208 if (m_opaque_ap.get())
209 {
210 const Section *section = m_opaque_ap->GetSection();
211 if (section)
212 return section->GetType();
213 }
214 return eSectionTypeInvalid;
215}
216
217
218SBModule
219SBAddress::GetModule ()
220{
221 SBModule sb_module;
222 if (m_opaque_ap.get())
223 {
Greg Clayton02e210c2011-09-17 07:23:18 +0000224 Module *module = m_opaque_ap->GetModule();
Greg Claytonb0e68d92011-03-31 01:08:07 +0000225 if (module)
Greg Clayton02e210c2011-09-17 07:23:18 +0000226 *sb_module = module;
Greg Claytonb0e68d92011-03-31 01:08:07 +0000227 }
228 return sb_module;
229}
230
Greg Claytonc51ffbf2011-08-12 21:40:01 +0000231SBSymbolContext
232SBAddress::GetSymbolContext (uint32_t resolve_scope)
233{
234 SBSymbolContext sb_sc;
235 if (m_opaque_ap.get())
236 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
237 return sb_sc;
238}
239
240SBCompileUnit
241SBAddress::GetCompileUnit ()
242{
243 SBCompileUnit sb_comp_unit;
244 if (m_opaque_ap.get())
245 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
246 return sb_comp_unit;
247}
248
249SBFunction
250SBAddress::GetFunction ()
251{
252 SBFunction sb_function;
253 if (m_opaque_ap.get())
254 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
255 return sb_function;
256}
257
258SBBlock
259SBAddress::GetBlock ()
260{
261 SBBlock sb_block;
262 if (m_opaque_ap.get())
263 sb_block.reset(m_opaque_ap->CalculateSymbolContextBlock());
264 return sb_block;
265}
266
267SBSymbol
268SBAddress::GetSymbol ()
269{
270 SBSymbol sb_symbol;
271 if (m_opaque_ap.get())
272 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
273 return sb_symbol;
274}
275
276SBLineEntry
277SBAddress::GetLineEntry ()
278{
279 SBLineEntry sb_line_entry;
280 if (m_opaque_ap.get())
281 {
282 LineEntry line_entry;
283 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
284 sb_line_entry.SetLineEntry (line_entry);
285 }
286 return sb_line_entry;
287}
288
Greg Claytonb0e68d92011-03-31 01:08:07 +0000289