1 //===-- SBAddress.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/API/SBAddress.h"
11 #include "lldb/API/SBProcess.h"
12 #include "lldb/API/SBSection.h"
13 #include "lldb/API/SBStream.h"
14 #include "lldb/Core/Address.h"
15 #include "lldb/Core/Log.h"
16 #include "lldb/Core/Module.h"
17 #include "lldb/Host/Mutex.h"
18 #include "lldb/Target/Target.h"
22 using namespace lldb_private;
25 SBAddress::SBAddress () :
30 SBAddress::SBAddress (const Address *lldb_object_ptr) :
34 ref() = *lldb_object_ptr;
37 SBAddress::SBAddress (const SBAddress &rhs) :
45 SBAddress::SBAddress (lldb::SBSection section, lldb::addr_t offset) :
46 m_opaque_ap(new Address (section.GetSP(), offset))
50 // Create an address by resolving a load address using the supplied target
51 SBAddress::SBAddress (lldb::addr_t load_addr, lldb::SBTarget &target) :
54 SetLoadAddress (load_addr, target);
59 SBAddress::~SBAddress ()
64 SBAddress::operator = (const SBAddress &rhs)
77 SBAddress::IsValid () const
79 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
89 SBAddress::SetAddress (lldb::SBSection section, lldb::addr_t offset)
91 Address &addr = ref();
92 addr.SetSection (section.GetSP());
93 addr.SetOffset (offset);
98 SBAddress::SetAddress (const Address *lldb_object_ptr)
101 ref() = *lldb_object_ptr;
107 SBAddress::GetFileAddress () const
109 if (m_opaque_ap.get())
110 return m_opaque_ap->GetFileAddress();
112 return LLDB_INVALID_ADDRESS;
116 SBAddress::GetLoadAddress (const SBTarget &target) const
118 Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
120 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
121 TargetSP target_sp (target.GetSP());
124 if (m_opaque_ap.get())
126 Mutex::Locker api_locker (target_sp->GetAPIMutex());
127 addr = m_opaque_ap->GetLoadAddress (target_sp.get());
133 if (addr == LLDB_INVALID_ADDRESS)
134 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", target_sp.get());
136 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, target_sp.get(), addr);
143 SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
145 // Create the address object if we don't already have one
147 if (target.IsValid())
148 *this = target.ResolveLoadAddress(load_addr);
150 m_opaque_ap->Clear();
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
156 if (!m_opaque_ap->IsValid())
157 m_opaque_ap->SetOffset(load_addr);
161 SBAddress::OffsetAddress (addr_t offset)
163 if (m_opaque_ap.get())
165 addr_t addr_offset = m_opaque_ap->GetOffset();
166 if (addr_offset != LLDB_INVALID_ADDRESS)
168 m_opaque_ap->SetOffset(addr_offset + offset);
176 SBAddress::GetSection ()
178 lldb::SBSection sb_section;
179 if (m_opaque_ap.get())
180 sb_section.SetSP (m_opaque_ap->GetSection());
185 SBAddress::GetOffset ()
187 if (m_opaque_ap.get())
188 return m_opaque_ap->GetOffset();
193 SBAddress::operator->()
195 return m_opaque_ap.get();
199 SBAddress::operator->() const
201 return m_opaque_ap.get();
207 if (m_opaque_ap.get() == NULL)
208 m_opaque_ap.reset (new Address());
213 SBAddress::ref () const
215 // This object should already have checked with "IsValid()"
216 // prior to calling this function. In case you didn't we will assert
217 // and die to let you know.
218 assert (m_opaque_ap.get());
225 return m_opaque_ap.get();
229 SBAddress::GetDescription (SBStream &description)
231 // Call "ref()" on the stream to make sure it creates a backing stream in
232 // case there isn't one already...
233 Stream &strm = description.ref();
234 if (m_opaque_ap.get())
236 m_opaque_ap->Dump (&strm,
238 Address::DumpStyleResolvedDescription,
239 Address::DumpStyleModuleWithFileAddress,
242 // m_opaque_ap->Dump (&sstrm, NULL, Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 4);
243 // if (sstrm.GetData())
244 // strm.Printf (" (%s)", sstrm.GetData());
247 strm.PutCString ("No value");
253 SBAddress::GetModule ()
256 if (m_opaque_ap.get())
257 sb_module.SetSP (m_opaque_ap->GetModule());
262 SBAddress::GetSymbolContext (uint32_t resolve_scope)
264 SBSymbolContext sb_sc;
265 if (m_opaque_ap.get())
266 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
271 SBAddress::GetCompileUnit ()
273 SBCompileUnit sb_comp_unit;
274 if (m_opaque_ap.get())
275 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
280 SBAddress::GetFunction ()
282 SBFunction sb_function;
283 if (m_opaque_ap.get())
284 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
289 SBAddress::GetBlock ()
292 if (m_opaque_ap.get())
293 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
298 SBAddress::GetSymbol ()
301 if (m_opaque_ap.get())
302 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
307 SBAddress::GetLineEntry ()
309 SBLineEntry sb_line_entry;
310 if (m_opaque_ap.get())
312 LineEntry line_entry;
313 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
314 sb_line_entry.SetLineEntry (line_entry);
316 return sb_line_entry;
320 SBAddress::GetAddressClass ()
322 if (m_opaque_ap.get())
323 return m_opaque_ap->GetAddressClass();
324 return eAddressClassInvalid;