1 //===-- SBAddress.cpp -------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "lldb/API/SBAddress.h"
10 #include "Utils.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"
20 
21 using namespace lldb;
22 using namespace lldb_private;
23 
24 SBAddress::SBAddress() : m_opaque_up(new Address()) {}
25 
26 SBAddress::SBAddress(const Address *lldb_object_ptr)
27     : m_opaque_up(new Address()) {
28   if (lldb_object_ptr)
29     m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr);
30 }
31 
32 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) {
33   m_opaque_up = clone(rhs.m_opaque_up);
34 }
35 
36 SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset)
37     : m_opaque_up(new Address(section.GetSP(), offset)) {}
38 
39 // Create an address by resolving a load address using the supplied target
40 SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target)
41     : m_opaque_up(new Address()) {
42   SetLoadAddress(load_addr, target);
43 }
44 
45 SBAddress::~SBAddress() {}
46 
47 const SBAddress &SBAddress::operator=(const SBAddress &rhs) {
48   if (this != &rhs)
49     m_opaque_up = clone(rhs.m_opaque_up);
50   return *this;
51 }
52 
53 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) {
54   if (lhs.IsValid() && rhs.IsValid())
55     return lhs.ref() == rhs.ref();
56   return false;
57 }
58 
59 bool SBAddress::IsValid() const {
60   return m_opaque_up != NULL && m_opaque_up->IsValid();
61 }
62 
63 void SBAddress::Clear() { m_opaque_up.reset(new Address()); }
64 
65 void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) {
66   Address &addr = ref();
67   addr.SetSection(section.GetSP());
68   addr.SetOffset(offset);
69 }
70 
71 void SBAddress::SetAddress(const Address *lldb_object_ptr) {
72   if (lldb_object_ptr)
73     ref() = *lldb_object_ptr;
74   else
75     m_opaque_up.reset(new Address());
76 }
77 
78 lldb::addr_t SBAddress::GetFileAddress() const {
79   if (m_opaque_up->IsValid())
80     return m_opaque_up->GetFileAddress();
81   else
82     return LLDB_INVALID_ADDRESS;
83 }
84 
85 lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const {
86   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
87 
88   lldb::addr_t addr = LLDB_INVALID_ADDRESS;
89   TargetSP target_sp(target.GetSP());
90   if (target_sp) {
91     if (m_opaque_up->IsValid()) {
92       std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex());
93       addr = m_opaque_up->GetLoadAddress(target_sp.get());
94     }
95   }
96 
97   if (log) {
98     if (addr == LLDB_INVALID_ADDRESS)
99       log->Printf(
100           "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS",
101           static_cast<void *>(target_sp.get()));
102     else
103       log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64,
104                   static_cast<void *>(target_sp.get()), addr);
105   }
106 
107   return addr;
108 }
109 
110 void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) {
111   // Create the address object if we don't already have one
112   ref();
113   if (target.IsValid())
114     *this = target.ResolveLoadAddress(load_addr);
115   else
116     m_opaque_up->Clear();
117 
118   // Check if we weren't were able to resolve a section offset address. If we
119   // weren't it is ok, the load address might be a location on the stack or
120   // heap, so we should just have an address with no section and a valid offset
121   if (!m_opaque_up->IsValid())
122     m_opaque_up->SetOffset(load_addr);
123 }
124 
125 bool SBAddress::OffsetAddress(addr_t offset) {
126   if (m_opaque_up->IsValid()) {
127     addr_t addr_offset = m_opaque_up->GetOffset();
128     if (addr_offset != LLDB_INVALID_ADDRESS) {
129       m_opaque_up->SetOffset(addr_offset + offset);
130       return true;
131     }
132   }
133   return false;
134 }
135 
136 lldb::SBSection SBAddress::GetSection() {
137   lldb::SBSection sb_section;
138   if (m_opaque_up->IsValid())
139     sb_section.SetSP(m_opaque_up->GetSection());
140   return sb_section;
141 }
142 
143 lldb::addr_t SBAddress::GetOffset() {
144   if (m_opaque_up->IsValid())
145     return m_opaque_up->GetOffset();
146   return 0;
147 }
148 
149 Address *SBAddress::operator->() { return m_opaque_up.get(); }
150 
151 const Address *SBAddress::operator->() const { return m_opaque_up.get(); }
152 
153 Address &SBAddress::ref() {
154   if (m_opaque_up == NULL)
155     m_opaque_up.reset(new Address());
156   return *m_opaque_up;
157 }
158 
159 const Address &SBAddress::ref() const {
160   // This object should already have checked with "IsValid()" prior to calling
161   // this function. In case you didn't we will assert and die to let you know.
162   assert(m_opaque_up.get());
163   return *m_opaque_up;
164 }
165 
166 Address *SBAddress::get() { return m_opaque_up.get(); }
167 
168 bool SBAddress::GetDescription(SBStream &description) {
169   // Call "ref()" on the stream to make sure it creates a backing stream in
170   // case there isn't one already...
171   Stream &strm = description.ref();
172   if (m_opaque_up->IsValid()) {
173     m_opaque_up->Dump(&strm, NULL, Address::DumpStyleResolvedDescription,
174                       Address::DumpStyleModuleWithFileAddress, 4);
175     StreamString sstrm;
176     //        m_opaque_up->Dump (&sstrm, NULL,
177     //        Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid,
178     //        4);
179     //        if (sstrm.GetData())
180     //            strm.Printf (" (%s)", sstrm.GetData());
181   } else
182     strm.PutCString("No value");
183 
184   return true;
185 }
186 
187 SBModule SBAddress::GetModule() {
188   SBModule sb_module;
189   if (m_opaque_up->IsValid())
190     sb_module.SetSP(m_opaque_up->GetModule());
191   return sb_module;
192 }
193 
194 SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) {
195   SBSymbolContext sb_sc;
196   SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope);
197   if (m_opaque_up->IsValid())
198     m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope);
199   return sb_sc;
200 }
201 
202 SBCompileUnit SBAddress::GetCompileUnit() {
203   SBCompileUnit sb_comp_unit;
204   if (m_opaque_up->IsValid())
205     sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit());
206   return sb_comp_unit;
207 }
208 
209 SBFunction SBAddress::GetFunction() {
210   SBFunction sb_function;
211   if (m_opaque_up->IsValid())
212     sb_function.reset(m_opaque_up->CalculateSymbolContextFunction());
213   return sb_function;
214 }
215 
216 SBBlock SBAddress::GetBlock() {
217   SBBlock sb_block;
218   if (m_opaque_up->IsValid())
219     sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock());
220   return sb_block;
221 }
222 
223 SBSymbol SBAddress::GetSymbol() {
224   SBSymbol sb_symbol;
225   if (m_opaque_up->IsValid())
226     sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol());
227   return sb_symbol;
228 }
229 
230 SBLineEntry SBAddress::GetLineEntry() {
231   SBLineEntry sb_line_entry;
232   if (m_opaque_up->IsValid()) {
233     LineEntry line_entry;
234     if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry))
235       sb_line_entry.SetLineEntry(line_entry);
236   }
237   return sb_line_entry;
238 }
239