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 "SBReproducerPrivate.h" 11 #include "Utils.h" 12 #include "lldb/API/SBProcess.h" 13 #include "lldb/API/SBSection.h" 14 #include "lldb/API/SBStream.h" 15 #include "lldb/Core/Address.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Symbol/LineEntry.h" 18 #include "lldb/Target/Target.h" 19 #include "lldb/Utility/Log.h" 20 #include "lldb/Utility/StreamString.h" 21 22 using namespace lldb; 23 using namespace lldb_private; 24 25 SBAddress::SBAddress() : m_opaque_up(new Address()) { 26 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBAddress); 27 } 28 29 SBAddress::SBAddress(const Address *lldb_object_ptr) 30 : m_opaque_up(new Address()) { 31 if (lldb_object_ptr) 32 m_opaque_up = llvm::make_unique<Address>(*lldb_object_ptr); 33 } 34 35 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_up(new Address()) { 36 LLDB_RECORD_CONSTRUCTOR(SBAddress, (const lldb::SBAddress &), rhs); 37 38 m_opaque_up = clone(rhs.m_opaque_up); 39 } 40 41 SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset) 42 : m_opaque_up(new Address(section.GetSP(), offset)) { 43 LLDB_RECORD_CONSTRUCTOR(SBAddress, (lldb::SBSection, lldb::addr_t), section, 44 offset); 45 } 46 47 // Create an address by resolving a load address using the supplied target 48 SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target) 49 : m_opaque_up(new Address()) { 50 LLDB_RECORD_CONSTRUCTOR(SBAddress, (lldb::addr_t, lldb::SBTarget &), 51 load_addr, target); 52 53 SetLoadAddress(load_addr, target); 54 } 55 56 SBAddress::~SBAddress() {} 57 58 const SBAddress &SBAddress::operator=(const SBAddress &rhs) { 59 LLDB_RECORD_METHOD(const lldb::SBAddress &, 60 SBAddress, operator=,(const lldb::SBAddress &), rhs); 61 62 if (this != &rhs) 63 m_opaque_up = clone(rhs.m_opaque_up); 64 return *this; 65 } 66 67 bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) { 68 if (lhs.IsValid() && rhs.IsValid()) 69 return lhs.ref() == rhs.ref(); 70 return false; 71 } 72 73 bool SBAddress::IsValid() const { 74 LLDB_RECORD_METHOD_CONST_NO_ARGS(bool, SBAddress, IsValid); 75 76 return m_opaque_up != NULL && m_opaque_up->IsValid(); 77 } 78 79 void SBAddress::Clear() { 80 LLDB_RECORD_METHOD_NO_ARGS(void, SBAddress, Clear); 81 82 m_opaque_up.reset(new Address()); 83 } 84 85 void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) { 86 LLDB_RECORD_METHOD(void, SBAddress, SetAddress, 87 (lldb::SBSection, lldb::addr_t), section, offset); 88 89 Address &addr = ref(); 90 addr.SetSection(section.GetSP()); 91 addr.SetOffset(offset); 92 } 93 94 void SBAddress::SetAddress(const Address *lldb_object_ptr) { 95 if (lldb_object_ptr) 96 ref() = *lldb_object_ptr; 97 else 98 m_opaque_up.reset(new Address()); 99 } 100 101 lldb::addr_t SBAddress::GetFileAddress() const { 102 LLDB_RECORD_METHOD_CONST_NO_ARGS(lldb::addr_t, SBAddress, GetFileAddress); 103 104 if (m_opaque_up->IsValid()) 105 return m_opaque_up->GetFileAddress(); 106 else 107 return LLDB_INVALID_ADDRESS; 108 } 109 110 lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const { 111 LLDB_RECORD_METHOD_CONST(lldb::addr_t, SBAddress, GetLoadAddress, 112 (const lldb::SBTarget &), target); 113 114 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 115 116 lldb::addr_t addr = LLDB_INVALID_ADDRESS; 117 TargetSP target_sp(target.GetSP()); 118 if (target_sp) { 119 if (m_opaque_up->IsValid()) { 120 std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); 121 addr = m_opaque_up->GetLoadAddress(target_sp.get()); 122 } 123 } 124 125 if (log) { 126 if (addr == LLDB_INVALID_ADDRESS) 127 log->Printf( 128 "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", 129 static_cast<void *>(target_sp.get())); 130 else 131 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, 132 static_cast<void *>(target_sp.get()), addr); 133 } 134 135 return addr; 136 } 137 138 void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) { 139 LLDB_RECORD_METHOD(void, SBAddress, SetLoadAddress, 140 (lldb::addr_t, lldb::SBTarget &), load_addr, target); 141 142 // Create the address object if we don't already have one 143 ref(); 144 if (target.IsValid()) 145 *this = target.ResolveLoadAddress(load_addr); 146 else 147 m_opaque_up->Clear(); 148 149 // Check if we weren't were able to resolve a section offset address. If we 150 // weren't it is ok, the load address might be a location on the stack or 151 // heap, so we should just have an address with no section and a valid offset 152 if (!m_opaque_up->IsValid()) 153 m_opaque_up->SetOffset(load_addr); 154 } 155 156 bool SBAddress::OffsetAddress(addr_t offset) { 157 LLDB_RECORD_METHOD(bool, SBAddress, OffsetAddress, (lldb::addr_t), offset); 158 159 if (m_opaque_up->IsValid()) { 160 addr_t addr_offset = m_opaque_up->GetOffset(); 161 if (addr_offset != LLDB_INVALID_ADDRESS) { 162 m_opaque_up->SetOffset(addr_offset + offset); 163 return true; 164 } 165 } 166 return false; 167 } 168 169 lldb::SBSection SBAddress::GetSection() { 170 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSection, SBAddress, GetSection); 171 172 lldb::SBSection sb_section; 173 if (m_opaque_up->IsValid()) 174 sb_section.SetSP(m_opaque_up->GetSection()); 175 return LLDB_RECORD_RESULT(sb_section); 176 } 177 178 lldb::addr_t SBAddress::GetOffset() { 179 LLDB_RECORD_METHOD_NO_ARGS(lldb::addr_t, SBAddress, GetOffset); 180 181 if (m_opaque_up->IsValid()) 182 return m_opaque_up->GetOffset(); 183 return 0; 184 } 185 186 Address *SBAddress::operator->() { return m_opaque_up.get(); } 187 188 const Address *SBAddress::operator->() const { return m_opaque_up.get(); } 189 190 Address &SBAddress::ref() { 191 if (m_opaque_up == NULL) 192 m_opaque_up.reset(new Address()); 193 return *m_opaque_up; 194 } 195 196 const Address &SBAddress::ref() const { 197 // This object should already have checked with "IsValid()" prior to calling 198 // this function. In case you didn't we will assert and die to let you know. 199 assert(m_opaque_up.get()); 200 return *m_opaque_up; 201 } 202 203 Address *SBAddress::get() { return m_opaque_up.get(); } 204 205 bool SBAddress::GetDescription(SBStream &description) { 206 LLDB_RECORD_METHOD(bool, SBAddress, GetDescription, (lldb::SBStream &), 207 description); 208 209 // Call "ref()" on the stream to make sure it creates a backing stream in 210 // case there isn't one already... 211 Stream &strm = description.ref(); 212 if (m_opaque_up->IsValid()) { 213 m_opaque_up->Dump(&strm, NULL, Address::DumpStyleResolvedDescription, 214 Address::DumpStyleModuleWithFileAddress, 4); 215 StreamString sstrm; 216 // m_opaque_up->Dump (&sstrm, NULL, 217 // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, 218 // 4); 219 // if (sstrm.GetData()) 220 // strm.Printf (" (%s)", sstrm.GetData()); 221 } else 222 strm.PutCString("No value"); 223 224 return true; 225 } 226 227 SBModule SBAddress::GetModule() { 228 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBModule, SBAddress, GetModule); 229 230 SBModule sb_module; 231 if (m_opaque_up->IsValid()) 232 sb_module.SetSP(m_opaque_up->GetModule()); 233 return LLDB_RECORD_RESULT(sb_module); 234 } 235 236 SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) { 237 LLDB_RECORD_METHOD(lldb::SBSymbolContext, SBAddress, GetSymbolContext, 238 (uint32_t), resolve_scope); 239 240 SBSymbolContext sb_sc; 241 SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope); 242 if (m_opaque_up->IsValid()) 243 m_opaque_up->CalculateSymbolContext(&sb_sc.ref(), scope); 244 return LLDB_RECORD_RESULT(sb_sc); 245 } 246 247 SBCompileUnit SBAddress::GetCompileUnit() { 248 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBCompileUnit, SBAddress, GetCompileUnit); 249 250 SBCompileUnit sb_comp_unit; 251 if (m_opaque_up->IsValid()) 252 sb_comp_unit.reset(m_opaque_up->CalculateSymbolContextCompileUnit()); 253 return LLDB_RECORD_RESULT(sb_comp_unit); 254 } 255 256 SBFunction SBAddress::GetFunction() { 257 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBFunction, SBAddress, GetFunction); 258 259 SBFunction sb_function; 260 if (m_opaque_up->IsValid()) 261 sb_function.reset(m_opaque_up->CalculateSymbolContextFunction()); 262 return LLDB_RECORD_RESULT(sb_function); 263 } 264 265 SBBlock SBAddress::GetBlock() { 266 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBBlock, SBAddress, GetBlock); 267 268 SBBlock sb_block; 269 if (m_opaque_up->IsValid()) 270 sb_block.SetPtr(m_opaque_up->CalculateSymbolContextBlock()); 271 return LLDB_RECORD_RESULT(sb_block); 272 } 273 274 SBSymbol SBAddress::GetSymbol() { 275 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBSymbol, SBAddress, GetSymbol); 276 277 SBSymbol sb_symbol; 278 if (m_opaque_up->IsValid()) 279 sb_symbol.reset(m_opaque_up->CalculateSymbolContextSymbol()); 280 return LLDB_RECORD_RESULT(sb_symbol); 281 } 282 283 SBLineEntry SBAddress::GetLineEntry() { 284 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBLineEntry, SBAddress, GetLineEntry); 285 286 SBLineEntry sb_line_entry; 287 if (m_opaque_up->IsValid()) { 288 LineEntry line_entry; 289 if (m_opaque_up->CalculateSymbolContextLineEntry(line_entry)) 290 sb_line_entry.SetLineEntry(line_entry); 291 } 292 return LLDB_RECORD_RESULT(sb_line_entry); 293 } 294