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 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 58 | bool SBAddress::IsValid() const { |
| 59 | return m_opaque_ap.get() != NULL && m_opaque_ap->IsValid(); |
| 60 | } |
| 61 | |
| 62 | void SBAddress::Clear() { m_opaque_ap.reset(new Address()); } |
| 63 | |
| 64 | void SBAddress::SetAddress(lldb::SBSection section, lldb::addr_t offset) { |
| 65 | Address &addr = ref(); |
| 66 | addr.SetSection(section.GetSP()); |
| 67 | addr.SetOffset(offset); |
| 68 | } |
| 69 | |
| 70 | void SBAddress::SetAddress(const Address *lldb_object_ptr) { |
| 71 | if (lldb_object_ptr) |
| 72 | ref() = *lldb_object_ptr; |
| 73 | else |
| 74 | m_opaque_ap.reset(new Address()); |
| 75 | } |
| 76 | |
| 77 | lldb::addr_t SBAddress::GetFileAddress() const { |
| 78 | if (m_opaque_ap->IsValid()) |
| 79 | return m_opaque_ap->GetFileAddress(); |
| 80 | else |
| 81 | return LLDB_INVALID_ADDRESS; |
| 82 | } |
| 83 | |
| 84 | lldb::addr_t SBAddress::GetLoadAddress(const SBTarget &target) const { |
| 85 | Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); |
| 86 | |
| 87 | lldb::addr_t addr = LLDB_INVALID_ADDRESS; |
| 88 | TargetSP target_sp(target.GetSP()); |
| 89 | if (target_sp) { |
| 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()); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if (log) { |
| 97 | if (addr == LLDB_INVALID_ADDRESS) |
| 98 | log->Printf( |
| 99 | "SBAddress::GetLoadAddress (SBTarget(%p)) => LLDB_INVALID_ADDRESS", |
| 100 | static_cast<void *>(target_sp.get())); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, |
| 103 | static_cast<void *>(target_sp.get()), addr); |
| 104 | } |
| 105 | |
| 106 | return addr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | void SBAddress::SetLoadAddress(lldb::addr_t load_addr, lldb::SBTarget &target) { |
| 110 | // Create the address object if we don't already have one |
| 111 | ref(); |
| 112 | if (target.IsValid()) |
| 113 | *this = target.ResolveLoadAddress(load_addr); |
| 114 | else |
| 115 | m_opaque_ap->Clear(); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 116 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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 |
| 120 | // a valid offset |
| 121 | if (!m_opaque_ap->IsValid()) |
| 122 | m_opaque_ap->SetOffset(load_addr); |
| 123 | } |
| 124 | |
| 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); |
| 130 | return true; |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 131 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } |
| 133 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 136 | lldb::SBSection SBAddress::GetSection() { |
| 137 | lldb::SBSection sb_section; |
| 138 | if (m_opaque_ap->IsValid()) |
| 139 | sb_section.SetSP(m_opaque_ap->GetSection()); |
| 140 | return sb_section; |
Greg Clayton | 00e6fbf | 2011-07-22 16:46:35 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | lldb::addr_t SBAddress::GetOffset() { |
| 144 | if (m_opaque_ap->IsValid()) |
| 145 | return m_opaque_ap->GetOffset(); |
| 146 | return 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | Address *SBAddress::operator->() { return m_opaque_ap.get(); } |
| 150 | |
| 151 | const Address *SBAddress::operator->() const { return m_opaque_ap.get(); } |
| 152 | |
| 153 | Address &SBAddress::ref() { |
| 154 | if (m_opaque_ap.get() == NULL) |
| 155 | m_opaque_ap.reset(new Address()); |
| 156 | return *m_opaque_ap; |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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()); |
| 164 | return *m_opaque_ap; |
Greg Clayton | 13d1950 | 2012-01-29 06:07:39 +0000 | [diff] [blame] | 165 | } |
Greg Clayton | cac9c5f | 2011-09-24 00:52:29 +0000 | [diff] [blame] | 166 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 167 | Address *SBAddress::get() { return m_opaque_ap.get(); } |
| 168 | |
| 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); |
| 176 | StreamString sstrm; |
| 177 | // m_opaque_ap->Dump (&sstrm, NULL, |
| 178 | // Address::DumpStyleResolvedDescription, Address::DumpStyleInvalid, |
| 179 | // 4); |
| 180 | // if (sstrm.GetData()) |
| 181 | // strm.Printf (" (%s)", sstrm.GetData()); |
| 182 | } else |
| 183 | strm.PutCString("No value"); |
| 184 | |
| 185 | return true; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 186 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | SBModule SBAddress::GetModule() { |
| 189 | SBModule sb_module; |
| 190 | if (m_opaque_ap->IsValid()) |
| 191 | sb_module.SetSP(m_opaque_ap->GetModule()); |
| 192 | return sb_module; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
| 199 | return sb_sc; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | SBCompileUnit SBAddress::GetCompileUnit() { |
| 203 | SBCompileUnit sb_comp_unit; |
| 204 | if (m_opaque_ap->IsValid()) |
| 205 | sb_comp_unit.reset(m_opaque_ap->CalculateSymbolContextCompileUnit()); |
| 206 | return sb_comp_unit; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 209 | SBFunction SBAddress::GetFunction() { |
| 210 | SBFunction sb_function; |
| 211 | if (m_opaque_ap->IsValid()) |
| 212 | sb_function.reset(m_opaque_ap->CalculateSymbolContextFunction()); |
| 213 | return sb_function; |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | SBBlock SBAddress::GetBlock() { |
| 217 | SBBlock sb_block; |
| 218 | if (m_opaque_ap->IsValid()) |
| 219 | sb_block.SetPtr(m_opaque_ap->CalculateSymbolContextBlock()); |
| 220 | return sb_block; |
Caroline Tice | dde9cff | 2010-09-20 05:20:02 +0000 | [diff] [blame] | 221 | } |
Greg Clayton | 05d2b7f | 2011-03-31 01:08:07 +0000 | [diff] [blame] | 222 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | SBSymbol SBAddress::GetSymbol() { |
| 224 | SBSymbol sb_symbol; |
| 225 | if (m_opaque_ap->IsValid()) |
| 226 | sb_symbol.reset(m_opaque_ap->CalculateSymbolContextSymbol()); |
| 227 | return sb_symbol; |
Greg Clayton | 05d2b7f | 2011-03-31 01:08:07 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 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); |
| 236 | } |
| 237 | return sb_line_entry; |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | AddressClass SBAddress::GetAddressClass() { |
| 241 | if (m_opaque_ap->IsValid()) |
| 242 | return m_opaque_ap->GetAddressClass(); |
| 243 | return eAddressClassInvalid; |
Greg Clayton | 7e9b1fd | 2011-08-12 21:40:01 +0000 | [diff] [blame] | 244 | } |