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 () :
26 m_opaque_ap (new Address())
30 SBAddress::SBAddress (const Address *lldb_object_ptr) :
31 m_opaque_ap (new Address())
34 ref() = *lldb_object_ptr;
37 SBAddress::SBAddress (const SBAddress &rhs) :
38 m_opaque_ap (new Address())
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) :
52 m_opaque_ap(new Address())
54 SetLoadAddress (load_addr, target);
59 SBAddress::~SBAddress ()
64 SBAddress::operator = (const SBAddress &rhs)
71 m_opaque_ap.reset (new Address());
77 SBAddress::IsValid () const
79 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
85 m_opaque_ap.reset (new Address());
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;
103 m_opaque_ap.reset (new Address());
107 SBAddress::GetFileAddress () const
109 if (m_opaque_ap->IsValid())
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->IsValid())
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",
135 static_cast<void*>(target_sp.get()));
137 log->Printf ("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
138 static_cast<void*>(target_sp.get()), addr);
145 SBAddress::SetLoadAddress (lldb::addr_t load_addr, lldb::SBTarget &target)
147 // Create the address object if we don't already have one
149 if (target.IsValid())
150 *this = target.ResolveLoadAddress(load_addr);
152 m_opaque_ap->Clear();
154 // Check if we weren't were able to resolve a section offset address.
155 // If we weren't it is ok, the load address might be a location on the
156 // stack or heap, so we should just have an address with no section and
158 if (!m_opaque_ap->IsValid())
159 m_opaque_ap->SetOffset(load_addr);
163 SBAddress::OffsetAddress (addr_t offset)
165 if (m_opaque_ap->IsValid())
167 addr_t addr_offset = m_opaque_ap->GetOffset();
168 if (addr_offset != LLDB_INVALID_ADDRESS)
170 m_opaque_ap->SetOffset(addr_offset + offset);
178 SBAddress::GetSection ()
180 lldb::SBSection sb_section;
181 if (m_opaque_ap->IsValid())
182 sb_section.SetSP (m_opaque_ap->GetSection());
187 SBAddress::GetOffset ()
189 if (m_opaque_ap->IsValid())
190 return m_opaque_ap->GetOffset();
195 SBAddress::operator->()
197 return m_opaque_ap.get();
201 SBAddress::operator->() const
203 return m_opaque_ap.get();
209 if (m_opaque_ap.get() == NULL)
210 m_opaque_ap.reset (new Address());
215 SBAddress::ref () const
217 // This object should already have checked with "IsValid()"
218 // prior to calling this function. In case you didn't we will assert
219 // and die to let you know.
220 assert (m_opaque_ap.get());
227 return m_opaque_ap.get();
231 SBAddress::GetDescription (SBStream &description)
233 // Call "ref()" on the stream to make sure it creates a backing stream in
234 // case there isn't one already...
235 Stream &strm = description.ref();
236 if (m_opaque_ap->IsValid())
238 m_opaque_ap->Dump (&strm,
240 Address::DumpStyleResolvedDescription,
241 Address::DumpStyleModuleWithFileAddress,
244 // m_opaque_ap->Dump (&sstrm, NULL, Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 4);
245 // if (sstrm.GetData())
246 // strm.Printf (" (%s)", sstrm.GetData());
249 strm.PutCString ("No value");
255 SBAddress::GetModule ()
258 if (m_opaque_ap->IsValid())
259 sb_module.SetSP (m_opaque_ap->GetModule());
264 SBAddress::GetSymbolContext (uint32_t resolve_scope)
266 SBSymbolContext sb_sc;
267 if (m_opaque_ap->IsValid())
268 m_opaque_ap->CalculateSymbolContext (&sb_sc.ref(), resolve_scope);
273 SBAddress::GetCompileUnit ()
275 SBCompileUnit sb_comp_unit;
276 if (m_opaque_ap->IsValid())
277 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
282 SBAddress::GetFunction ()
284 SBFunction sb_function;
285 if (m_opaque_ap->IsValid())
286 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
291 SBAddress::GetBlock ()
294 if (m_opaque_ap->IsValid())
295 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
300 SBAddress::GetSymbol ()
303 if (m_opaque_ap->IsValid())
304 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
309 SBAddress::GetLineEntry ()
311 SBLineEntry sb_line_entry;
312 if (m_opaque_ap->IsValid())
314 LineEntry line_entry;
315 if (m_opaque_ap->CalculateSymbolContextLineEntry (line_entry))
316 sb_line_entry.SetLineEntry (line_entry);
318 return sb_line_entry;
322 SBAddress::GetAddressClass ()
324 if (m_opaque_ap->IsValid())
325 return m_opaque_ap->GetAddressClass();
326 return eAddressClassInvalid;