Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- SBAddress.cpp -------------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/API/SBAddress.h" |
| 10 | #include "lldb/API/SBProcess.h" |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 11 | #include "lldb/API/SBSection.h" |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 12 | #include "lldb/API/SBStream.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/Address.h" |
Greg Clayton | 05d2b7f | 2011-03-31 01:08:07 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
Zachary Turner | 32abc6e | 2015-03-03 19:23:09 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/LineEntry.h" |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 16 | #include "lldb/Target/Target.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/Log.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 18 | #include "lldb/Utility/StreamString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lldb; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 21 | using namespace lldb_private; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 23 | SBAddress::SBAddress() : m_opaque_ap(new Address()) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 25 | SBAddress::SBAddress(const Address *lldb_object_ptr) |
| 26 | : m_opaque_ap(new Address()) { |
| 27 | if (lldb_object_ptr) |
| 28 | ref() = *lldb_object_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 31 | SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_ap(new Address()) { |
| 32 | if (rhs.IsValid()) |
| 33 | ref() = rhs.ref(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset) |
| 37 | : m_opaque_ap(new Address(section.GetSP(), offset)) {} |
Greg Clayton | 819134a | 2012-02-04 02:58:17 +0000 | [diff] [blame] | 38 | |
Greg Clayton | 00e6fbf | 2011-07-22 16:46:35 +0000 | [diff] [blame] | 39 | // Create an address by resolving a load address using the supplied target |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 40 | SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target) |
| 41 | : m_opaque_ap(new Address()) { |
| 42 | SetLoadAddress(load_addr, target); |
Greg Clayton | 00e6fbf | 2011-07-22 16:46:35 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 45 | SBAddress::~SBAddress() {} |
Greg Clayton | 00e6fbf | 2011-07-22 16:46:35 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | const SBAddress &SBAddress::operator=(const SBAddress &rhs) { |
| 48 | if (this != &rhs) { |
| 49 | if (rhs.IsValid()) |
| 50 | ref() = rhs.ref(); |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 51 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 52 | m_opaque_ap.reset(new Address()); |
| 53 | } |
| 54 | return *this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Nitesh Jain | dd12594 | 2017-05-04 11:34:42 +0000 | [diff] [blame] | 57 | bool lldb::operator==(const SBAddress &lhs, const SBAddress &rhs) { |
| 58 | if (lhs.IsValid() && rhs.IsValid()) |
| 59 | return lhs.ref() == rhs.ref(); |
| 60 | return false; |
| 61 | } |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | bool SBAddress::IsValid() const { |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 64 | return m_opaque_ap != NULL && m_opaque_ap->IsValid(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void SBAddress::Clear() { m_opaque_ap.reset(new Address()); } |
| 68 | |
| 69 | void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) { |
| 70 | Address &addr = ref(); |
| 71 | addr.SetSection(section.GetSP()); |
| 72 | addr.SetOffset(offset); |
| 73 | } |
| 74 | |
| 75 | void SBAddress::SetAddress(const Address *lldb_object_ptr) { |
| 76 | if (lldb_object_ptr) |
| 77 | ref() = *lldb_object_ptr; |
| 78 | else |
| 79 | m_opaque_ap.reset(new Address()); |
| 80 | } |
| 81 | |
| 82 | lldb::addr_t SBAddress::GetFileAddress() const { |
| 83 | if (m_opaque_ap->IsValid()) |
| 84 | return m_opaque_ap->GetFileAddress(); |
| 85 | else |
| 86 | return LLDB_INVALID_ADDRESS; |
| 87 | } |
| 88 | |
| 89 | lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const { |
| 90 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 91 | |
| 92 | lldb::addr_t addr = LLDB_INVALID_ADDRESS; |
| 93 | TargetSP target_sp(target.GetSP()); |
| 94 | if (target_sp) { |
| 95 | if (m_opaque_ap->IsValid()) { |
| 96 | std::lock_guard<std::recursive_mutex> guard(target_sp->GetAPIMutex()); |
| 97 | addr = m_opaque_ap->GetLoadAddress(target_sp.get()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | if (log) { |
| 102 | if (addr == LLDB_INVALID_ADDRESS) |
| 103 | log->Printf( |
| 104 | "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", |
| 105 | static_cast<void *>(target_sp.get())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, |
| 108 | static_cast<void *>(target_sp.get()), addr); |
| 109 | } |
| 110 | |
| 111 | return addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) { |
| 115 | // Create the address object if we don't already have one |
| 116 | ref(); |
| 117 | if (target.IsValid()) |
| 118 | *this = target.ResolveLoadAddress(load_addr); |
| 119 | else |
| 120 | m_opaque_ap->Clear(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 121 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 122 | // Check if we weren't were able to resolve a section offset address. If we |
| 123 | // weren't it is ok, the load address might be a location on the stack or |
| 124 | // heap, so we should just have an address with no section and a valid offset |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 125 | if (!m_opaque_ap->IsValid()) |
| 126 | m_opaque_ap->SetOffset(load_addr); |
| 127 | } |
| 128 | |
| 129 | bool SBAddress::OffsetAddress(addr_t offset) { |
| 130 | if (m_opaque_ap->IsValid()) { |
| 131 | addr_t addr_offset = m_opaque_ap->GetOffset(); |
| 132 | if (addr_offset != LLDB_INVALID_ADDRESS) { |
| 133 | m_opaque_ap->SetOffset(addr_offset + offset); |
| 134 | return true; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 135 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | } |
| 137 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | lldb::SBSection SBAddress::GetSection() { |
| 141 | lldb::SBSection sb_section; |
| 142 | if (m_opaque_ap->IsValid()) |
| 143 | sb_section.SetSP(m_opaque_ap->GetSection()); |
| 144 | return sb_section; |
Greg Clayton | 00e6fbf | 2011-07-22 16:46:35 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 147 | lldb::addr_t SBAddress::GetOffset() { |
| 148 | if (m_opaque_ap->IsValid()) |
| 149 | return m_opaque_ap->GetOffset(); |
| 150 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | Address *SBAddress::operator->() { return m_opaque_ap.get(); } |
| 154 | |
| 155 | const Address *SBAddress::operator->() const { return m_opaque_ap.get(); } |
| 156 | |
| 157 | Address &SBAddress::ref() { |
Jonas Devlieghere | 3447077 | 2018-12-20 21:02:55 +0000 | [diff] [blame] | 158 | if (m_opaque_ap == NULL) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | m_opaque_ap.reset(new Address()); |
| 160 | return *m_opaque_ap; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | const Address &SBAddress::ref() const { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 164 | // This object should already have checked with "IsValid()" prior to calling |
| 165 | // this function. In case you didn't we will assert and die to let you know. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 166 | assert(m_opaque_ap.get()); |
| 167 | return *m_opaque_ap; |
Greg Clayton | 13d1950 | 2012-01-29 06:07:39 +0000 | [diff] [blame] | 168 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | Address *SBAddress::get() { return m_opaque_ap.get(); } |
| 171 | |
| 172 | bool SBAddress::GetDescription(SBStream &description) { |
| 173 | // Call "ref()" on the stream to make sure it creates a backing stream in |
| 174 | // case there isn't one already... |
| 175 | Stream &strm = description.ref(); |
| 176 | if (m_opaque_ap->IsValid()) { |
| 177 | m_opaque_ap->Dump(&strm, NULL, Address::DumpStyleResolvedDescription, |
| 178 | Address::DumpStyleModuleWithFileAddress, 4); |
| 179 | StreamString sstrm; |
| 180 | // m_opaque_ap->Dump (&sstrm, NULL, |
| 181 | // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, |
| 182 | // 4); |
| 183 | // if (sstrm.GetData()) |
| 184 | // strm.Printf (" (%s)", sstrm.GetData()); |
| 185 | } else |
| 186 | strm.PutCString("No value"); |
| 187 | |
| 188 | return true; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 189 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | SBModule SBAddress::GetModule() { |
| 192 | SBModule sb_module; |
| 193 | if (m_opaque_ap->IsValid()) |
| 194 | sb_module.SetSP(m_opaque_ap->GetModule()); |
| 195 | return sb_module; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 198 | SBSymbolContext SBAddress::GetSymbolContext(uint32_t resolve_scope) { |
| 199 | SBSymbolContext sb_sc; |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 200 | SymbolContextItem scope = static_cast<SymbolContextItem>(resolve_scope); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 201 | if (m_opaque_ap->IsValid()) |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 202 | m_opaque_ap->CalculateSymbolContext(&sb_sc.ref(), scope); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 203 | return sb_sc; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 206 | SBCompileUnit SBAddress::GetCompileUnit() { |
| 207 | SBCompileUnit sb_comp_unit; |
| 208 | if (m_opaque_ap->IsValid()) |
| 209 | sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit()); |
| 210 | return sb_comp_unit; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 213 | SBFunction SBAddress::GetFunction() { |
| 214 | SBFunction sb_function; |
| 215 | if (m_opaque_ap->IsValid()) |
| 216 | sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction()); |
| 217 | return sb_function; |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 218 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | SBBlock SBAddress::GetBlock() { |
| 221 | SBBlock sb_block; |
| 222 | if (m_opaque_ap->IsValid()) |
| 223 | sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock()); |
| 224 | return sb_block; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 225 | } |
Greg Clayton | 05d2b7f | 2011-03-31 01:08:07 +0000 | [diff] [blame] | 226 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 227 | SBSymbol SBAddress::GetSymbol() { |
| 228 | SBSymbol sb_symbol; |
| 229 | if (m_opaque_ap->IsValid()) |
| 230 | sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol()); |
| 231 | return sb_symbol; |
Greg Clayton | 05d2b7f | 2011-03-31 01:08:07 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | SBLineEntry SBAddress::GetLineEntry() { |
| 235 | SBLineEntry sb_line_entry; |
| 236 | if (m_opaque_ap->IsValid()) { |
| 237 | LineEntry line_entry; |
| 238 | if (m_opaque_ap->CalculateSymbolContextLineEntry(line_entry)) |
| 239 | sb_line_entry.SetLineEntry(line_entry); |
| 240 | } |
| 241 | return sb_line_entry; |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 242 | } |