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/Core/StreamString.h"
18 #include "lldb/Symbol/LineEntry.h"
19 #include "lldb/Target/Target.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 SBAddress::IsValid() const {
59 return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid();
62 void SBAddress::Clear() { m_opaque_ap.reset(new Address()); }
64 void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
65 Address &addr = ref();
66 addr.SetSection(section.GetSP());
67 addr.SetOffset(offset);
70 void SBAddress::SetAddress(const Address *lldb_object_ptr) {
72 ref() = *lldb_object_ptr;
74 m_opaque_ap.reset(new Address());
77 lldb::addr_t SBAddress::GetFileAddress() const {
78 if (m_opaque_ap->IsValid())
79 return m_opaque_ap->GetFileAddress();
81 return LLDB_INVALID_ADDRESS;
84 lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
85 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
87 lldb::addr_t addr = LLDB_INVALID_ADDRESS;
88 TargetSP target_sp(target.GetSP());
90 if (m_opaque_ap->IsValid()) {
91 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
92 addr = m_opaque_ap->GetLoadAddress(target_sp.get());
97 if (addr == LLDB_INVALID_ADDRESS)
99 "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
100 static_cast<void *>(target_sp.get()));
102 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
103 static_cast<void *>(target_sp.get()), addr);
109 void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
110 // Create the address object if we don't already have one
112 if (target.IsValid())
113 *this = target.ResolveLoadAddress(load_addr);
115 m_opaque_ap->Clear();
117 // Check if we weren't were able to resolve a section offset address.
118 // If we weren't it is ok, the load address might be a location on the
119 // stack or heap, so we should just have an address with no section and
121 if (!m_opaque_ap->IsValid())
122 m_opaque_ap->SetOffset(load_addr);
125 bool SBAddress::OffsetAddress(addr_t offset) {
126 if (m_opaque_ap->IsValid()) {
127 addr_t addr_offset = m_opaque_ap->GetOffset();
128 if (addr_offset != LLDB_INVALID_ADDRESS) {
129 m_opaque_ap->SetOffset(addr_offset + offset);
136 lldb::SBSection SBAddress::GetSection() {
137 lldb::SBSection sb_section;
138 if (m_opaque_ap->IsValid())
139 sb_section.SetSP(m_opaque_ap->GetSection());
143 lldb::addr_t SBAddress::GetOffset() {
144 if (m_opaque_ap->IsValid())
145 return m_opaque_ap->GetOffset();
149 Address *SBAddress::operator->() { return m_opaque_ap.get(); }
151 const Address *SBAddress::operator->() const { return m_opaque_ap.get(); }
153 Address &SBAddress::ref() {
154 if (m_opaque_ap.get() == NULL)
155 m_opaque_ap.reset(new Address());
159 const Address &SBAddress::ref() const {
160 // This object should already have checked with "IsValid()"
161 // prior to calling this function. In case you didn't we will assert
162 // and die to let you know.
163 assert(m_opaque_ap.get());
167 Address *SBAddress::get() { return m_opaque_ap.get(); }
169 bool SBAddress::GetDescription(SBStream &description) {
170 // Call "ref()" on the stream to make sure it creates a backing stream in
171 // case there isn't one already...
172 Stream &strm = description.ref();
173 if (m_opaque_ap->IsValid()) {
174 m_opaque_ap->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
175 Address::DumpStyleModuleWithFileAddress, 4);
177 // m_opaque_ap->Dump (&sstrm, NULL,
178 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
180 // if (sstrm.GetData())
181 // strm.Printf (" (%s)", sstrm.GetData());
183 strm.PutCString("No value");
188 SBModule SBAddress::GetModule() {
190 if (m_opaque_ap->IsValid())
191 sb_module.SetSP(m_opaque_ap->GetModule());
195 SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
196 SBSymbolContext sb_sc;
197 if (m_opaque_ap->IsValid())
198 m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), resolve_scope);
202 SBCompileUnit SBAddress::GetCompileUnit() {
203 SBCompileUnit sb_comp_unit;
204 if (m_opaque_ap->IsValid())
205 sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit());
209 SBFunction SBAddress::GetFunction() {
210 SBFunction sb_function;
211 if (m_opaque_ap->IsValid())
212 sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction());
216 SBBlock SBAddress::GetBlock() {
218 if (m_opaque_ap->IsValid())
219 sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock());
223 SBSymbol SBAddress::GetSymbol() {
225 if (m_opaque_ap->IsValid())
226 sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol());
230 SBLineEntry SBAddress::GetLineEntry() {
231 SBLineEntry sb_line_entry;
232 if (m_opaque_ap->IsValid()) {
233 LineEntry line_entry;
234 if (m_opaque_ap->CalculateSymbolContextLineEntry(line_entry))
235 sb_line_entry.SetLineEntry(line_entry);
237 return sb_line_entry;
240 AddressClass SBAddress::GetAddressClass() {
241 if (m_opaque_ap->IsValid())
242 return m_opaque_ap->GetAddressClass();
243 return eAddressClassInvalid;