1 //===-- SBSymbol.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/SBSymbol.h" 10 #include "lldb/API/SBStream.h" 11 #include "lldb/Core/Disassembler.h" 12 #include "lldb/Core/Module.h" 13 #include "lldb/Symbol/Symbol.h" 14 #include "lldb/Target/ExecutionContext.h" 15 #include "lldb/Target/Target.h" 16 #include "lldb/Utility/Log.h" 17 18 using namespace lldb; 19 using namespace lldb_private; 20 21 SBSymbol::SBSymbol() : m_opaque_ptr(NULL) {} 22 23 SBSymbol::SBSymbol(lldb_private::Symbol *lldb_object_ptr) 24 : m_opaque_ptr(lldb_object_ptr) {} 25 26 SBSymbol::SBSymbol(const lldb::SBSymbol &rhs) 27 : m_opaque_ptr(rhs.m_opaque_ptr) {} 28 29 const SBSymbol &SBSymbol::operator=(const SBSymbol &rhs) { 30 m_opaque_ptr = rhs.m_opaque_ptr; 31 return *this; 32 } 33 34 SBSymbol::~SBSymbol() { m_opaque_ptr = NULL; } 35 36 void SBSymbol::SetSymbol(lldb_private::Symbol *lldb_object_ptr) { 37 m_opaque_ptr = lldb_object_ptr; 38 } 39 40 bool SBSymbol::IsValid() const { return m_opaque_ptr != NULL; } 41 42 const char *SBSymbol::GetName() const { 43 const char *name = NULL; 44 if (m_opaque_ptr) 45 name = m_opaque_ptr->GetName().AsCString(); 46 47 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 48 if (log) 49 log->Printf("SBSymbol(%p)::GetName () => \"%s\"", 50 static_cast<void *>(m_opaque_ptr), name ? name : ""); 51 return name; 52 } 53 54 const char *SBSymbol::GetDisplayName() const { 55 const char *name = NULL; 56 if (m_opaque_ptr) 57 name = m_opaque_ptr->GetMangled() 58 .GetDisplayDemangledName(m_opaque_ptr->GetLanguage()) 59 .AsCString(); 60 61 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 62 if (log) 63 log->Printf("SBSymbol(%p)::GetDisplayName () => \"%s\"", 64 static_cast<void *>(m_opaque_ptr), name ? name : ""); 65 return name; 66 } 67 68 const char *SBSymbol::GetMangledName() const { 69 const char *name = NULL; 70 if (m_opaque_ptr) 71 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString(); 72 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_API)); 73 if (log) 74 log->Printf("SBSymbol(%p)::GetMangledName () => \"%s\"", 75 static_cast<void *>(m_opaque_ptr), name ? name : ""); 76 77 return name; 78 } 79 80 bool SBSymbol::operator==(const SBSymbol &rhs) const { 81 return m_opaque_ptr == rhs.m_opaque_ptr; 82 } 83 84 bool SBSymbol::operator!=(const SBSymbol &rhs) const { 85 return m_opaque_ptr != rhs.m_opaque_ptr; 86 } 87 88 bool SBSymbol::GetDescription(SBStream &description) { 89 Stream &strm = description.ref(); 90 91 if (m_opaque_ptr) { 92 m_opaque_ptr->GetDescription(&strm, lldb::eDescriptionLevelFull, NULL); 93 } else 94 strm.PutCString("No value"); 95 96 return true; 97 } 98 99 SBInstructionList SBSymbol::GetInstructions(SBTarget target) { 100 return GetInstructions(target, NULL); 101 } 102 103 SBInstructionList SBSymbol::GetInstructions(SBTarget target, 104 const char *flavor_string) { 105 SBInstructionList sb_instructions; 106 if (m_opaque_ptr) { 107 ExecutionContext exe_ctx; 108 TargetSP target_sp(target.GetSP()); 109 std::unique_lock<std::recursive_mutex> lock; 110 if (target_sp) { 111 lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex()); 112 113 target_sp->CalculateExecutionContext(exe_ctx); 114 } 115 if (m_opaque_ptr->ValueIsAddress()) { 116 const Address &symbol_addr = m_opaque_ptr->GetAddressRef(); 117 ModuleSP module_sp = symbol_addr.GetModule(); 118 if (module_sp) { 119 AddressRange symbol_range(symbol_addr, m_opaque_ptr->GetByteSize()); 120 const bool prefer_file_cache = false; 121 sb_instructions.SetDisassembler(Disassembler::DisassembleRange( 122 module_sp->GetArchitecture(), NULL, flavor_string, exe_ctx, 123 symbol_range, prefer_file_cache)); 124 } 125 } 126 } 127 return sb_instructions; 128 } 129 130 lldb_private::Symbol *SBSymbol::get() { return m_opaque_ptr; } 131 132 void SBSymbol::reset(lldb_private::Symbol *symbol) { m_opaque_ptr = symbol; } 133 134 SBAddress SBSymbol::GetStartAddress() { 135 SBAddress addr; 136 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) { 137 addr.SetAddress(&m_opaque_ptr->GetAddressRef()); 138 } 139 return addr; 140 } 141 142 SBAddress SBSymbol::GetEndAddress() { 143 SBAddress addr; 144 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) { 145 lldb::addr_t range_size = m_opaque_ptr->GetByteSize(); 146 if (range_size > 0) { 147 addr.SetAddress(&m_opaque_ptr->GetAddressRef()); 148 addr->Slide(m_opaque_ptr->GetByteSize()); 149 } 150 } 151 return addr; 152 } 153 154 uint32_t SBSymbol::GetPrologueByteSize() { 155 if (m_opaque_ptr) 156 return m_opaque_ptr->GetPrologueByteSize(); 157 return 0; 158 } 159 160 SymbolType SBSymbol::GetType() { 161 if (m_opaque_ptr) 162 return m_opaque_ptr->GetType(); 163 return eSymbolTypeInvalid; 164 } 165 166 bool SBSymbol::IsExternal() { 167 if (m_opaque_ptr) 168 return m_opaque_ptr->IsExternal(); 169 return false; 170 } 171 172 bool SBSymbol::IsSynthetic() { 173 if (m_opaque_ptr) 174 return m_opaque_ptr->IsSynthetic(); 175 return false; 176 } 177