1 //===-- SBSymbol.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/SBSymbol.h" 11 #include "lldb/API/SBStream.h" 12 #include "lldb/Core/Disassembler.h" 13 #include "lldb/Core/Log.h" 14 #include "lldb/Core/Module.h" 15 #include "lldb/Symbol/Symbol.h" 16 #include "lldb/Target/ExecutionContext.h" 17 #include "lldb/Target/Target.h" 18 19 using namespace lldb; 20 using namespace lldb_private; 21 22 SBSymbol::SBSymbol () : 23 m_opaque_ptr (NULL) 24 { 25 } 26 27 SBSymbol::SBSymbol (lldb_private::Symbol *lldb_object_ptr) : 28 m_opaque_ptr (lldb_object_ptr) 29 { 30 } 31 32 SBSymbol::SBSymbol (const lldb::SBSymbol &rhs) : 33 m_opaque_ptr (rhs.m_opaque_ptr) 34 { 35 } 36 37 const SBSymbol & 38 SBSymbol::operator = (const SBSymbol &rhs) 39 { 40 m_opaque_ptr = rhs.m_opaque_ptr; 41 return *this; 42 } 43 44 SBSymbol::~SBSymbol () 45 { 46 m_opaque_ptr = NULL; 47 } 48 49 void 50 SBSymbol::SetSymbol (lldb_private::Symbol *lldb_object_ptr) 51 { 52 m_opaque_ptr = lldb_object_ptr; 53 } 54 55 bool 56 SBSymbol::IsValid () const 57 { 58 return m_opaque_ptr != NULL; 59 } 60 61 const char * 62 SBSymbol::GetName() const 63 { 64 const char *name = NULL; 65 if (m_opaque_ptr) 66 name = m_opaque_ptr->GetName().AsCString(); 67 68 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 69 if (log) 70 log->Printf ("SBSymbol(%p)::GetName () => \"%s\"", 71 static_cast<void*>(m_opaque_ptr), name ? name : ""); 72 return name; 73 } 74 75 const char * 76 SBSymbol::GetDisplayName() const 77 { 78 const char *name = NULL; 79 if (m_opaque_ptr) 80 name = m_opaque_ptr->GetMangled().GetDisplayDemangledName(m_opaque_ptr->GetLanguage()).AsCString(); 81 82 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 83 if (log) 84 log->Printf ("SBSymbol(%p)::GetDisplayName () => \"%s\"", 85 static_cast<void*>(m_opaque_ptr), name ? name : ""); 86 return name; 87 } 88 89 const char * 90 SBSymbol::GetMangledName () const 91 { 92 const char *name = NULL; 93 if (m_opaque_ptr) 94 name = m_opaque_ptr->GetMangled().GetMangledName().AsCString(); 95 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); 96 if (log) 97 log->Printf ("SBSymbol(%p)::GetMangledName () => \"%s\"", 98 static_cast<void*>(m_opaque_ptr), name ? name : ""); 99 100 return name; 101 } 102 103 104 bool 105 SBSymbol::operator == (const SBSymbol &rhs) const 106 { 107 return m_opaque_ptr == rhs.m_opaque_ptr; 108 } 109 110 bool 111 SBSymbol::operator != (const SBSymbol &rhs) const 112 { 113 return m_opaque_ptr != rhs.m_opaque_ptr; 114 } 115 116 bool 117 SBSymbol::GetDescription (SBStream &description) 118 { 119 Stream &strm = description.ref(); 120 121 if (m_opaque_ptr) 122 { 123 m_opaque_ptr->GetDescription (&strm, 124 lldb::eDescriptionLevelFull, NULL); 125 } 126 else 127 strm.PutCString ("No value"); 128 129 return true; 130 } 131 132 SBInstructionList 133 SBSymbol::GetInstructions (SBTarget target) 134 { 135 return GetInstructions (target, NULL); 136 } 137 138 SBInstructionList 139 SBSymbol::GetInstructions (SBTarget target, const char *flavor_string) 140 { 141 SBInstructionList sb_instructions; 142 if (m_opaque_ptr) 143 { 144 Mutex::Locker api_locker; 145 ExecutionContext exe_ctx; 146 TargetSP target_sp (target.GetSP()); 147 if (target_sp) 148 { 149 api_locker.Lock (target_sp->GetAPIMutex()); 150 target_sp->CalculateExecutionContext (exe_ctx); 151 } 152 if (m_opaque_ptr->ValueIsAddress()) 153 { 154 const Address &symbol_addr = m_opaque_ptr->GetAddressRef(); 155 ModuleSP module_sp = symbol_addr.GetModule(); 156 if (module_sp) 157 { 158 AddressRange symbol_range (symbol_addr, m_opaque_ptr->GetByteSize()); 159 const bool prefer_file_cache = false; 160 sb_instructions.SetDisassembler (Disassembler::DisassembleRange (module_sp->GetArchitecture (), 161 NULL, 162 flavor_string, 163 exe_ctx, 164 symbol_range, 165 prefer_file_cache)); 166 } 167 } 168 } 169 return sb_instructions; 170 } 171 172 lldb_private::Symbol * 173 SBSymbol::get () 174 { 175 return m_opaque_ptr; 176 } 177 178 void 179 SBSymbol::reset (lldb_private::Symbol *symbol) 180 { 181 m_opaque_ptr = symbol; 182 } 183 184 SBAddress 185 SBSymbol::GetStartAddress () 186 { 187 SBAddress addr; 188 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) 189 { 190 addr.SetAddress (&m_opaque_ptr->GetAddressRef()); 191 } 192 return addr; 193 } 194 195 SBAddress 196 SBSymbol::GetEndAddress () 197 { 198 SBAddress addr; 199 if (m_opaque_ptr && m_opaque_ptr->ValueIsAddress()) 200 { 201 lldb::addr_t range_size = m_opaque_ptr->GetByteSize(); 202 if (range_size > 0) 203 { 204 addr.SetAddress (&m_opaque_ptr->GetAddressRef()); 205 addr->Slide (m_opaque_ptr->GetByteSize()); 206 } 207 } 208 return addr; 209 } 210 211 uint32_t 212 SBSymbol::GetPrologueByteSize () 213 { 214 if (m_opaque_ptr) 215 return m_opaque_ptr->GetPrologueByteSize(); 216 return 0; 217 } 218 219 SymbolType 220 SBSymbol::GetType () 221 { 222 if (m_opaque_ptr) 223 return m_opaque_ptr->GetType(); 224 return eSymbolTypeInvalid; 225 } 226 227 bool 228 SBSymbol::IsExternal() 229 { 230 if (m_opaque_ptr) 231 return m_opaque_ptr->IsExternal(); 232 return false; 233 } 234 235 bool 236 SBSymbol::IsSynthetic() 237 { 238 if (m_opaque_ptr) 239 return m_opaque_ptr->IsSynthetic(); 240 return false; 241 } 242 243