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/Module.h"
16 #include "lldb/Symbol/LineEntry.h"
17 #include "lldb/Target/Target.h"
18 #include "lldb/Utility/Log.h"
19 #include "lldb/Utility/StreamString.h"
22 using namespace lldb_private;
24 SBAddress::SBAddress() : m_opaque_ap(new Address()) {}
26 SBAddress::SBAddress(const Address *lldb_object_ptr)
27 : m_opaque_ap(new Address()) {
29 ref() = *lldb_object_ptr;
32 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_ap(new Address()) {
37 SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
38 : m_opaque_ap(new Address(section.GetSP(), offset)) {}
40 // Create an address by resolving a load address using the supplied target
41 SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
42 : m_opaque_ap(new Address()) {
43 SetLoadAddress(load_addr, target);
46 SBAddress::~SBAddress() {}
48 const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
53 m_opaque_ap.reset(new Address());
58 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
59 if (lhs.IsValid() && rhs.IsValid())
60 return lhs.ref() == rhs.ref();
64 bool SBAddress::IsValid() const {
65 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
68 void SBAddress::Clear() { m_opaque_ap.reset(new Address()); }
70 void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
71 Address &addr = ref();
72 addr.SetSection(section.GetSP());
73 addr.SetOffset(offset);
76 void SBAddress::SetAddress(const Address *lldb_object_ptr) {
78 ref() = *lldb_object_ptr;
80 m_opaque_ap.reset(new Address());
83 lldb::addr_t SBAddress::GetFileAddress() const {
84 if (m_opaque_ap->IsValid())
85 return m_opaque_ap->GetFileAddress();
87 return LLDB_INVALID_ADDRESS;
90 lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
91 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
93 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
94 TargetSP target_sp(target.GetSP());
96 if (m_opaque_ap->IsValid()) {
97 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
98 addr = m_opaque_ap->GetLoadAddress(target_sp.get());
103 if (addr == LLDB_INVALID_ADDRESS)
105 "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
106 static_cast<void *>(target_sp.get()));
108 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
109 static_cast<void *>(target_sp.get()), addr);
115 void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
116 // Create the address object if we don't already have one
118 if (target.IsValid())
119 *this = target.ResolveLoadAddress(load_addr);
121 m_opaque_ap->Clear();
123 // Check if we weren't were able to resolve a section offset address.
124 // If we weren't it is ok, the load address might be a location on the
125 // stack or heap, so we should just have an address with no section and
127 if (!m_opaque_ap->IsValid())
128 m_opaque_ap->SetOffset(load_addr);
131 bool SBAddress::OffsetAddress(addr_t offset) {
132 if (m_opaque_ap->IsValid()) {
133 addr_t addr_offset = m_opaque_ap->GetOffset();
134 if (addr_offset != LLDB_INVALID_ADDRESS) {
135 m_opaque_ap->SetOffset(addr_offset + offset);
142 lldb::SBSection SBAddress::GetSection() {
143 lldb::SBSection sb_section;
144 if (m_opaque_ap->IsValid())
145 sb_section.SetSP(m_opaque_ap->GetSection());
149 lldb::addr_t SBAddress::GetOffset() {
150 if (m_opaque_ap->IsValid())
151 return m_opaque_ap->GetOffset();
155 Address *SBAddress::operator->() { return m_opaque_ap.get(); }
157 const Address *SBAddress::operator->() const { return m_opaque_ap.get(); }
159 Address &SBAddress::ref() {
160 if (m_opaque_ap.get() == NULL)
161 m_opaque_ap.reset(new Address());
165 const Address &SBAddress::ref() const {
166 // This object should already have checked with "IsValid()"
167 // prior to calling this function. In case you didn't we will assert
168 // and die to let you know.
169 assert(m_opaque_ap.get());
173 Address *SBAddress::get() { return m_opaque_ap.get(); }
175 bool SBAddress::GetDescription(SBStream &description) {
176 // Call "ref()" on the stream to make sure it creates a backing stream in
177 // case there isn't one already...
178 Stream &strm = description.ref();
179 if (m_opaque_ap->IsValid()) {
180 m_opaque_ap->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
181 Address::DumpStyleModuleWithFileAddress, 4);
183 // m_opaque_ap->Dump (&sstrm, NULL,
184 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
186 // if (sstrm.GetData())
187 // strm.Printf (" (%s)", sstrm.GetData());
189 strm.PutCString("No value");
194 SBModule SBAddress::GetModule() {
196 if (m_opaque_ap->IsValid())
197 sb_module.SetSP(m_opaque_ap->GetModule());
201 SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
202 SBSymbolContext sb_sc;
203 if (m_opaque_ap->IsValid())
204 m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), resolve_scope);
208 SBCompileUnit SBAddress::GetCompileUnit() {
209 SBCompileUnit sb_comp_unit;
210 if (m_opaque_ap->IsValid())
211 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
215 SBFunction SBAddress::GetFunction() {
216 SBFunction sb_function;
217 if (m_opaque_ap->IsValid())
218 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
222 SBBlock SBAddress::GetBlock() {
224 if (m_opaque_ap->IsValid())
225 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
229 SBSymbol SBAddress::GetSymbol() {
231 if (m_opaque_ap->IsValid())
232 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
236 SBLineEntry SBAddress::GetLineEntry() {
237 SBLineEntry sb_line_entry;
238 if (m_opaque_ap->IsValid()) {
239 LineEntry line_entry;
240 if (m_opaque_ap->CalculateSymbolContextLineEntry(line_entry))
241 sb_line_entry.SetLineEntry(line_entry);
243 return sb_line_entry;
246 AddressClass SBAddress::GetAddressClass() {
247 if (m_opaque_ap->IsValid())
248 return m_opaque_ap->GetAddressClass();
249 return eAddressClassInvalid;