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