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" 12 #include "lldb/API/SBSection.h" 13 #include "lldb/API/SBStream.h" 14 #include "lldb/Core/Address.h" 15 #include "lldb/Core/Log.h" 16 #include "lldb/Core/Module.h" 17 #include "lldb/Core/StreamString.h" 18 #include "lldb/Symbol/LineEntry.h" 19 #include "lldb/Target/Target.h" 20 21 using namespace lldb; 22 using namespace lldb_private; 23 24 SBAddress::SBAddress() : m_opaque_ap(new Address()) {} 25 26 SBAddress::SBAddress(const Address *lldb_object_ptr) 27 : m_opaque_ap(new Address()) { 28 if (lldb_object_ptr) 29 ref() = *lldb_object_ptr; 30 } 31 32 SBAddress::SBAddress(const SBAddress &rhs) : m_opaque_ap(new Address()) { 33 if (rhs.IsValid()) 34 ref() = rhs.ref(); 35 } 36 37 SBAddress::SBAddress(lldb::SBSection section, lldb::addr_t offset) 38 : m_opaque_ap(new Address(section.GetSP(), offset)) {} 39 40 // Create an address by resolving a load address using the supplied target 41 SBAddress::SBAddress(lldb::addr_t load_addr, lldb::SBTarget &target) 42 : m_opaque_ap(new Address()) { 43 SetLoadAddress(load_addr, target); 44 } 45 46 SBAddress::~SBAddress() {} 47 48 const SBAddress &SBAddress::operator=(const SBAddress &rhs) { 49 if (this != &rhs) { 50 if (rhs.IsValid()) 51 ref() = rhs.ref(); 52 else 53 m_opaque_ap.reset(new Address()); 54 } 55 return *this; 56 } 57 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())); 101 else 102 log->Printf("SBAddress::GetLoadAddress (SBTarget(%p)) => 0x%" PRIx64, 103 static_cast<void *>(target_sp.get()), addr); 104 } 105 106 return addr; 107 } 108 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(); 116 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; 131 } 132 } 133 return false; 134 } 135 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; 141 } 142 143 lldb::addr_t SBAddress::GetOffset() { 144 if (m_opaque_ap->IsValid()) 145 return m_opaque_ap->GetOffset(); 146 return 0; 147 } 148 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; 157 } 158 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; 165 } 166 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; 186 } 187 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; 193 } 194 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; 200 } 201 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; 207 } 208 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; 214 } 215 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; 221 } 222 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; 228 } 229 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; 238 } 239 240 AddressClass SBAddress::GetAddressClass() { 241 if (m_opaque_ap->IsValid()) 242 return m_opaque_ap->GetAddressClass(); 243 return eAddressClassInvalid; 244 } 245