Symtab.cpp revision e14d3d3209f4260acc6e84d656460d5bdeade2c2
1e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent//===-- Symtab.cpp ----------------------------------------------*- C++ -*-===// 2e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// 3e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// The LLVM Compiler Infrastructure 4e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// 5e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// This file is distributed under the University of Illinois Open Source 6e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// License. See LICENSE.TXT for details. 7e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent// 8e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent//===----------------------------------------------------------------------===// 9e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 10e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include <map> 11c55a96383497a772a307b346368133960b02ad03Eric Laurent 12e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Core/Module.h" 13e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Core/RegularExpression.h" 14e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Core/Timer.h" 15e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Symbol/ObjectFile.h" 16e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Symbol/Symtab.h" 17e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent#include "lldb/Target/ObjCLanguageRuntime.h" 18e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 19e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentusing namespace lldb; 20e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentusing namespace lldb_private; 21c55a96383497a772a307b346368133960b02ad03Eric Laurent 22e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 23e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 24c55a96383497a772a307b346368133960b02ad03Eric LaurentSymtab::Symtab(ObjectFile *objfile) : 25e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_objfile (objfile), 26c55a96383497a772a307b346368133960b02ad03Eric Laurent m_symbols (), 27e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_addr_indexes (), 28e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_name_to_index (), 29e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_mutex (Mutex::eMutexTypeRecursive), 30e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_addr_indexes_computed (false), 31e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_name_indexes_computed (false) 32e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 33e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 34e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 35e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::~Symtab() 36e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 37e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 38e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 39e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentvoid 40e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::Reserve(uint32_t count) 41e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 42c55a96383497a772a307b346368133960b02ad03Eric Laurent // Clients should grab the mutex from this symbol table and lock it manually 43e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // when calling this function to avoid performance issues. 44e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_symbols.reserve (count); 45e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 46c55a96383497a772a307b346368133960b02ad03Eric Laurent 47e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymbol * 48e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::Resize(uint32_t count) 49e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 50e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // Clients should grab the mutex from this symbol table and lock it manually 51e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // when calling this function to avoid performance issues. 52e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_symbols.resize (count); 53e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent return &m_symbols[0]; 54e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 55e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 56e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentuint32_t 57e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::AddSymbol(const Symbol& symbol) 58e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 59e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // Clients should grab the mutex from this symbol table and lock it manually 60e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // when calling this function to avoid performance issues. 61e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent uint32_t symbol_idx = m_symbols.size(); 62e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_name_to_index.Clear(); 63e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_addr_indexes.clear(); 64e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_symbols.push_back(symbol); 65e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_addr_indexes_computed = false; 66e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_name_indexes_computed = false; 67e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent return symbol_idx; 68e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 69e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 70e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurentsize_t 71e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::GetNumSymbols() const 72e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 73e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent Mutex::Locker locker (m_mutex); 74e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent return m_symbols.size(); 75e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent} 76e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 77c55a96383497a772a307b346368133960b02ad03Eric Laurentvoid 78e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric LaurentSymtab::Dump (Stream *s, Target *target, SortOrder sort_order) 79e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent{ 80e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent Mutex::Locker locker (m_mutex); 81e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 82c55a96383497a772a307b346368133960b02ad03Eric Laurent// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 83e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->Indent(); 84c55a96383497a772a307b346368133960b02ad03Eric Laurent const FileSpec &file_spec = m_objfile->GetFileSpec(); 85e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent const char * object_name = NULL; 86e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent if (m_objfile->GetModule()) 87e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent object_name = m_objfile->GetModule()->GetObjectName().GetCString(); 88e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 89e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent if (file_spec) 90e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->Printf("Symtab, file = %s/%s%s%s%s, num_symbols = %lu", 91c55a96383497a772a307b346368133960b02ad03Eric Laurent file_spec.GetDirectory().AsCString(), 92e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent file_spec.GetFilename().AsCString(), 93e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent object_name ? "(" : "", 94e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent object_name ? object_name : "", 95e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent object_name ? ")" : "", 96e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent m_symbols.size()); 97e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent else 98e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->Printf("Symtab, num_symbols = %lu", m_symbols.size()); 99e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 100e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent if (!m_symbols.empty()) 101e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 102e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent switch (sort_order) 103e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 104c55a96383497a772a307b346368133960b02ad03Eric Laurent case eSortOrderNone: 105e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 106e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->PutCString (":\n"); 107e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent DumpSymbolHeader (s); 108e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent const_iterator begin = m_symbols.begin(); 109e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent const_iterator end = m_symbols.end(); 110e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) 111e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 112e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->Indent(); 113e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent pos->Dump(s, target, std::distance(begin, pos)); 114e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent } 115e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent } 116e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent break; 117e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 118e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent case eSortOrderByName: 119e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 120e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // Although we maintain a lookup by exact name map, the table 121e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // isn't sorted by name. So we must make the ordered symbol list 122e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent // up ourselves. 123e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->PutCString (" (sorted by name):\n"); 124e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent DumpSymbolHeader (s); 125e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol; 126e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent CStringToSymbol name_map; 127e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos) 128e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 129e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent const char *name = pos->GetMangled().GetName(Mangled::ePreferDemangled).AsCString(); 130e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent if (name && name[0]) 131e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent name_map.insert (std::make_pair(name, &(*pos))); 132e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent } 133e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 134e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos) 135e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 136e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->Indent(); 137e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent pos->second->Dump (s, target, pos->second - &m_symbols[0]); 138e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent } 139e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent } 140e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent break; 141e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent 142c55a96383497a772a307b346368133960b02ad03Eric Laurent case eSortOrderByAddress: 143e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent s->PutCString (" (sorted by address):\n"); 144e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent DumpSymbolHeader (s); 145e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent if (!m_addr_indexes_computed) 146e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent InitAddressIndexes(); 147c55a96383497a772a307b346368133960b02ad03Eric Laurent const size_t num_symbols = GetNumSymbols(); 148e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent std::vector<uint32_t>::const_iterator pos; 149e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent std::vector<uint32_t>::const_iterator end = m_addr_indexes.end(); 150e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent for (pos = m_addr_indexes.begin(); pos != end; ++pos) 151e48d5845c8b35de2ab73ea055c18a61fa3a9f0beEric Laurent { 152 uint32_t idx = *pos; 153 if (idx < num_symbols) 154 { 155 s->Indent(); 156 m_symbols[idx].Dump(s, target, idx); 157 } 158 } 159 break; 160 } 161 } 162} 163 164void 165Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const 166{ 167 Mutex::Locker locker (m_mutex); 168 169 const size_t num_symbols = GetNumSymbols(); 170 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 171 s->Indent(); 172 s->Printf("Symtab %lu symbol indexes (%lu symbols total):\n", indexes.size(), m_symbols.size()); 173 s->IndentMore(); 174 175 if (!indexes.empty()) 176 { 177 std::vector<uint32_t>::const_iterator pos; 178 std::vector<uint32_t>::const_iterator end = indexes.end(); 179 DumpSymbolHeader (s); 180 for (pos = indexes.begin(); pos != end; ++pos) 181 { 182 uint32_t idx = *pos; 183 if (idx < num_symbols) 184 { 185 s->Indent(); 186 m_symbols[idx].Dump(s, target, idx); 187 } 188 } 189 } 190 s->IndentLess (); 191} 192 193void 194Symtab::DumpSymbolHeader (Stream *s) 195{ 196 s->Indent(" Debug symbol\n"); 197 s->Indent(" |Synthetic symbol\n"); 198 s->Indent(" ||Externally Visible\n"); 199 s->Indent(" |||\n"); 200 s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n"); 201 s->Indent("------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------\n"); 202} 203 204 205static int 206CompareSymbolID (const void *key, const void *p) 207{ 208 const user_id_t match_uid = *(user_id_t*) key; 209 const user_id_t symbol_uid = ((Symbol *)p)->GetID(); 210 if (match_uid < symbol_uid) 211 return -1; 212 if (match_uid > symbol_uid) 213 return 1; 214 return 0; 215} 216 217Symbol * 218Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const 219{ 220 Mutex::Locker locker (m_mutex); 221 222 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid, 223 &m_symbols[0], 224 m_symbols.size(), 225 (uint8_t *)&m_symbols[1] - (uint8_t *)&m_symbols[0], 226 CompareSymbolID); 227 return symbol; 228} 229 230 231Symbol * 232Symtab::SymbolAtIndex(uint32_t idx) 233{ 234 // Clients should grab the mutex from this symbol table and lock it manually 235 // when calling this function to avoid performance issues. 236 if (idx < m_symbols.size()) 237 return &m_symbols[idx]; 238 return NULL; 239} 240 241 242const Symbol * 243Symtab::SymbolAtIndex(uint32_t idx) const 244{ 245 // Clients should grab the mutex from this symbol table and lock it manually 246 // when calling this function to avoid performance issues. 247 if (idx < m_symbols.size()) 248 return &m_symbols[idx]; 249 return NULL; 250} 251 252//---------------------------------------------------------------------- 253// InitNameIndexes 254//---------------------------------------------------------------------- 255void 256Symtab::InitNameIndexes() 257{ 258 // Protected function, no need to lock mutex... 259 if (!m_name_indexes_computed) 260 { 261 m_name_indexes_computed = true; 262 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 263 // Create the name index vector to be able to quickly search by name 264 const size_t count = m_symbols.size(); 265#if 1 266 m_name_to_index.Reserve (count); 267#else 268 // TODO: benchmark this to see if we save any memory. Otherwise we 269 // will always keep the memory reserved in the vector unless we pull 270 // some STL swap magic and then recopy... 271 uint32_t actual_count = 0; 272 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); 273 pos != end; 274 ++pos) 275 { 276 const Mangled &mangled = pos->GetMangled(); 277 if (mangled.GetMangledName()) 278 ++actual_count; 279 280 if (mangled.GetDemangledName()) 281 ++actual_count; 282 } 283 284 m_name_to_index.Reserve (actual_count); 285#endif 286 287 NameToIndexMap::Entry entry; 288 289 for (entry.value = 0; entry.value < count; ++entry.value) 290 { 291 const Symbol *symbol = &m_symbols[entry.value]; 292 293 // Don't let trampolines get into the lookup by name map 294 // If we ever need the trampoline symbols to be searchable by name 295 // we can remove this and then possibly add a new bool to any of the 296 // Symtab functions that lookup symbols by name to indicate if they 297 // want trampolines. 298 if (symbol->IsTrampoline()) 299 continue; 300 301 const Mangled &mangled = symbol->GetMangled(); 302 entry.cstring = mangled.GetMangledName().GetCString(); 303 if (entry.cstring && entry.cstring[0]) 304 m_name_to_index.Append (entry); 305 306 entry.cstring = mangled.GetDemangledName().GetCString(); 307 if (entry.cstring && entry.cstring[0]) 308 m_name_to_index.Append (entry); 309 310 // If the demangled name turns out to be an ObjC name, and 311 // is a category name, add the version without categories to the index too. 312 ConstString objc_base_name; 313 if (ObjCLanguageRuntime::ParseMethodName (entry.cstring, 314 NULL, 315 NULL, 316 &objc_base_name, 317 NULL) 318 && !objc_base_name.IsEmpty()) 319 { 320 entry.cstring = objc_base_name.GetCString(); 321 m_name_to_index.Append (entry); 322 } 323 324 } 325 m_name_to_index.Sort(); 326 m_name_to_index.SizeToFit(); 327 } 328} 329 330void 331Symtab::AppendSymbolNamesToMap (const IndexCollection &indexes, 332 bool add_demangled, 333 bool add_mangled, 334 NameToIndexMap &name_to_index_map) const 335{ 336 if (add_demangled || add_mangled) 337 { 338 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 339 Mutex::Locker locker (m_mutex); 340 341 // Create the name index vector to be able to quickly search by name 342 NameToIndexMap::Entry entry; 343 const size_t num_indexes = indexes.size(); 344 for (size_t i=0; i<num_indexes; ++i) 345 { 346 entry.value = indexes[i]; 347 assert (i < m_symbols.size()); 348 const Symbol *symbol = &m_symbols[entry.value]; 349 350 const Mangled &mangled = symbol->GetMangled(); 351 if (add_demangled) 352 { 353 entry.cstring = mangled.GetDemangledName().GetCString(); 354 if (entry.cstring && entry.cstring[0]) 355 name_to_index_map.Append (entry); 356 } 357 358 if (add_mangled) 359 { 360 entry.cstring = mangled.GetMangledName().GetCString(); 361 if (entry.cstring && entry.cstring[0]) 362 name_to_index_map.Append (entry); 363 } 364 } 365 } 366} 367 368uint32_t 369Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const 370{ 371 Mutex::Locker locker (m_mutex); 372 373 uint32_t prev_size = indexes.size(); 374 375 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 376 377 for (uint32_t i = start_idx; i < count; ++i) 378 { 379 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 380 indexes.push_back(i); 381 } 382 383 return indexes.size() - prev_size; 384} 385 386uint32_t 387Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const 388{ 389 Mutex::Locker locker (m_mutex); 390 391 uint32_t prev_size = indexes.size(); 392 393 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 394 395 for (uint32_t i = start_idx; i < count; ++i) 396 { 397 if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value) 398 indexes.push_back(i); 399 } 400 401 return indexes.size() - prev_size; 402} 403 404uint32_t 405Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const 406{ 407 Mutex::Locker locker (m_mutex); 408 409 uint32_t prev_size = indexes.size(); 410 411 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); 412 413 for (uint32_t i = start_idx; i < count; ++i) 414 { 415 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 416 { 417 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility)) 418 indexes.push_back(i); 419 } 420 } 421 422 return indexes.size() - prev_size; 423} 424 425 426uint32_t 427Symtab::GetIndexForSymbol (const Symbol *symbol) const 428{ 429 const Symbol *first_symbol = &m_symbols[0]; 430 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size()) 431 return symbol - first_symbol; 432 return UINT32_MAX; 433} 434 435struct SymbolSortInfo 436{ 437 const bool sort_by_load_addr; 438 const Symbol *symbols; 439}; 440 441namespace { 442 struct SymbolIndexComparator { 443 const std::vector<Symbol>& symbols; 444 SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { } 445 bool operator()(uint32_t index_a, uint32_t index_b) { 446 addr_t value_a; 447 addr_t value_b; 448 if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection()) { 449 value_a = symbols[index_a].GetValue ().GetOffset(); 450 value_b = symbols[index_b].GetValue ().GetOffset(); 451 } else { 452 value_a = symbols[index_a].GetValue ().GetFileAddress(); 453 value_b = symbols[index_b].GetValue ().GetFileAddress(); 454 } 455 456 if (value_a == value_b) { 457 // The if the values are equal, use the original symbol user ID 458 lldb::user_id_t uid_a = symbols[index_a].GetID(); 459 lldb::user_id_t uid_b = symbols[index_b].GetID(); 460 if (uid_a < uid_b) 461 return true; 462 if (uid_a > uid_b) 463 return false; 464 return false; 465 } else if (value_a < value_b) 466 return true; 467 468 return false; 469 } 470 }; 471} 472 473void 474Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const 475{ 476 Mutex::Locker locker (m_mutex); 477 478 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__); 479 // No need to sort if we have zero or one items... 480 if (indexes.size() <= 1) 481 return; 482 483 // Sort the indexes in place using std::stable_sort. 484 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance, 485 // not correctness. The indexes vector tends to be "close" to sorted, which the 486 // stable sort handles better. 487 std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols)); 488 489 // Remove any duplicates if requested 490 if (remove_duplicates) 491 std::unique(indexes.begin(), indexes.end()); 492} 493 494uint32_t 495Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes) 496{ 497 Mutex::Locker locker (m_mutex); 498 499 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 500 if (symbol_name) 501 { 502 const char *symbol_cstr = symbol_name.GetCString(); 503 if (!m_name_indexes_computed) 504 InitNameIndexes(); 505 506 return m_name_to_index.GetValues (symbol_cstr, indexes); 507 } 508 return 0; 509} 510 511uint32_t 512Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 513{ 514 Mutex::Locker locker (m_mutex); 515 516 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 517 if (symbol_name) 518 { 519 const size_t old_size = indexes.size(); 520 if (!m_name_indexes_computed) 521 InitNameIndexes(); 522 523 const char *symbol_cstr = symbol_name.GetCString(); 524 525 std::vector<uint32_t> all_name_indexes; 526 const size_t name_match_count = m_name_to_index.GetValues (symbol_cstr, all_name_indexes); 527 for (size_t i=0; i<name_match_count; ++i) 528 { 529 if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, symbol_visibility)) 530 indexes.push_back (all_name_indexes[i]); 531 } 532 return indexes.size() - old_size; 533 } 534 return 0; 535} 536 537uint32_t 538Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes) 539{ 540 Mutex::Locker locker (m_mutex); 541 542 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) 543 { 544 std::vector<uint32_t>::iterator pos = indexes.begin(); 545 while (pos != indexes.end()) 546 { 547 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) 548 ++pos; 549 else 550 indexes.erase(pos); 551 } 552 } 553 return indexes.size(); 554} 555 556uint32_t 557Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 558{ 559 Mutex::Locker locker (m_mutex); 560 561 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0) 562 { 563 std::vector<uint32_t>::iterator pos = indexes.begin(); 564 while (pos != indexes.end()) 565 { 566 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) 567 ++pos; 568 else 569 indexes.erase(pos); 570 } 571 } 572 return indexes.size(); 573} 574 575 576uint32_t 577Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, std::vector<uint32_t>& indexes) 578{ 579 Mutex::Locker locker (m_mutex); 580 581 uint32_t prev_size = indexes.size(); 582 uint32_t sym_end = m_symbols.size(); 583 584 for (int i = 0; i < sym_end; i++) 585 { 586 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 587 { 588 const char *name = m_symbols[i].GetMangled().GetName().AsCString(); 589 if (name) 590 { 591 if (regexp.Execute (name)) 592 indexes.push_back(i); 593 } 594 } 595 } 596 return indexes.size() - prev_size; 597 598} 599 600uint32_t 601Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) 602{ 603 Mutex::Locker locker (m_mutex); 604 605 uint32_t prev_size = indexes.size(); 606 uint32_t sym_end = m_symbols.size(); 607 608 for (int i = 0; i < sym_end; i++) 609 { 610 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) 611 { 612 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false) 613 continue; 614 615 const char *name = m_symbols[i].GetMangled().GetName().AsCString(); 616 if (name) 617 { 618 if (regexp.Execute (name)) 619 indexes.push_back(i); 620 } 621 } 622 } 623 return indexes.size() - prev_size; 624 625} 626 627Symbol * 628Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx) 629{ 630 Mutex::Locker locker (m_mutex); 631 632 const size_t count = m_symbols.size(); 633 for (uint32_t idx = start_idx; idx < count; ++idx) 634 { 635 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type) 636 { 637 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) 638 { 639 start_idx = idx; 640 return &m_symbols[idx]; 641 } 642 } 643 } 644 return NULL; 645} 646 647size_t 648Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes) 649{ 650 Mutex::Locker locker (m_mutex); 651 652 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 653 // Initialize all of the lookup by name indexes before converting NAME 654 // to a uniqued string NAME_STR below. 655 if (!m_name_indexes_computed) 656 InitNameIndexes(); 657 658 if (name) 659 { 660 // The string table did have a string that matched, but we need 661 // to check the symbols and match the symbol_type if any was given. 662 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes); 663 } 664 return symbol_indexes.size(); 665} 666 667size_t 668Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) 669{ 670 Mutex::Locker locker (m_mutex); 671 672 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 673 // Initialize all of the lookup by name indexes before converting NAME 674 // to a uniqued string NAME_STR below. 675 if (!m_name_indexes_computed) 676 InitNameIndexes(); 677 678 if (name) 679 { 680 // The string table did have a string that matched, but we need 681 // to check the symbols and match the symbol_type if any was given. 682 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); 683 } 684 return symbol_indexes.size(); 685} 686 687size_t 688Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression ®ex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) 689{ 690 Mutex::Locker locker (m_mutex); 691 692 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); 693 return symbol_indexes.size(); 694} 695 696Symbol * 697Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility) 698{ 699 Mutex::Locker locker (m_mutex); 700 701 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); 702 if (!m_name_indexes_computed) 703 InitNameIndexes(); 704 705 if (name) 706 { 707 std::vector<uint32_t> matching_indexes; 708 // The string table did have a string that matched, but we need 709 // to check the symbols and match the symbol_type if any was given. 710 if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes)) 711 { 712 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end(); 713 for (pos = matching_indexes.begin(); pos != end; ++pos) 714 { 715 Symbol *symbol = SymbolAtIndex(*pos); 716 717 if (symbol->Compare(name, symbol_type)) 718 return symbol; 719 } 720 } 721 } 722 return NULL; 723} 724 725typedef struct 726{ 727 const Symtab *symtab; 728 const addr_t file_addr; 729 Symbol *match_symbol; 730 const uint32_t *match_index_ptr; 731 addr_t match_offset; 732} SymbolSearchInfo; 733 734static int 735SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) 736{ 737 const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]); 738 if (curr_symbol == NULL) 739 return -1; 740 741 const addr_t info_file_addr = info->file_addr; 742 743 // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address 744 // range if the symbol has a section! 745 const AddressRange *curr_range = curr_symbol->GetAddressRangePtr(); 746 if (curr_range) 747 { 748 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress(); 749 if (info_file_addr < curr_file_addr) 750 return -1; 751 if (info_file_addr > curr_file_addr) 752 return +1; 753 info->match_symbol = const_cast<Symbol *>(curr_symbol); 754 info->match_index_ptr = index_ptr; 755 return 0; 756 } 757 758 return -1; 759} 760 761static int 762SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) 763{ 764 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]); 765 if (symbol == NULL) 766 return -1; 767 768 const addr_t info_file_addr = info->file_addr; 769 const AddressRange *curr_range = symbol->GetAddressRangePtr(); 770 if (curr_range) 771 { 772 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress(); 773 if (info_file_addr < curr_file_addr) 774 return -1; 775 776 // Since we are finding the closest symbol that is greater than or equal 777 // to 'info->file_addr' we set the symbol here. This will get set 778 // multiple times, but after the search is done it will contain the best 779 // symbol match 780 info->match_symbol = const_cast<Symbol *>(symbol); 781 info->match_index_ptr = index_ptr; 782 info->match_offset = info_file_addr - curr_file_addr; 783 784 if (info_file_addr > curr_file_addr) 785 return +1; 786 return 0; 787 } 788 return -1; 789} 790 791static SymbolSearchInfo 792FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) 793{ 794 SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 }; 795 ::bsearch (&info, 796 indexes, 797 num_indexes, 798 sizeof(uint32_t), 799 (ComparisonFunction)SymbolWithClosestFileAddress); 800 return info; 801} 802 803 804void 805Symtab::InitAddressIndexes() 806{ 807 // Protected function, no need to lock mutex... 808 if (!m_addr_indexes_computed && !m_symbols.empty()) 809 { 810 m_addr_indexes_computed = true; 811#if 0 812 // The old was to add only code, trampoline or data symbols... 813 AppendSymbolIndexesWithType (eSymbolTypeCode, m_addr_indexes); 814 AppendSymbolIndexesWithType (eSymbolTypeTrampoline, m_addr_indexes); 815 AppendSymbolIndexesWithType (eSymbolTypeData, m_addr_indexes); 816#else 817 // The new way adds all symbols with valid addresses that are section 818 // offset. 819 const_iterator begin = m_symbols.begin(); 820 const_iterator end = m_symbols.end(); 821 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) 822 { 823 if (pos->GetAddressRangePtr()) 824 m_addr_indexes.push_back (std::distance(begin, pos)); 825 } 826#endif 827 SortSymbolIndexesByValue (m_addr_indexes, false); 828 m_addr_indexes.push_back (UINT32_MAX); // Terminator for bsearch since we might need to look at the next symbol 829 } 830} 831 832size_t 833Symtab::CalculateSymbolSize (Symbol *symbol) 834{ 835 Mutex::Locker locker (m_mutex); 836 837 if (m_symbols.empty()) 838 return 0; 839 840 // Make sure this symbol is from this symbol table... 841 if (symbol < &m_symbols.front() || symbol > &m_symbols.back()) 842 return 0; 843 844 // See if this symbol already has a byte size? 845 size_t byte_size = symbol->GetByteSize(); 846 847 if (byte_size) 848 { 849 // It does, just return it 850 return byte_size; 851 } 852 853 // Else if this is an address based symbol, figure out the delta between 854 // it and the next address based symbol 855 if (symbol->GetAddressRangePtr()) 856 { 857 if (!m_addr_indexes_computed) 858 InitAddressIndexes(); 859 const size_t num_addr_indexes = m_addr_indexes.size(); 860 SymbolSearchInfo info = FindIndexPtrForSymbolContainingAddress(this, symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(), &m_addr_indexes.front(), num_addr_indexes); 861 if (info.match_index_ptr != NULL) 862 { 863 const lldb::addr_t curr_file_addr = symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(); 864 // We can figure out the address range of all symbols except the 865 // last one by taking the delta between the current symbol and 866 // the next symbol 867 868 for (uint32_t addr_index = info.match_index_ptr - &m_addr_indexes.front() + 1; 869 addr_index < num_addr_indexes; 870 ++addr_index) 871 { 872 Symbol *next_symbol = SymbolAtIndex(m_addr_indexes[addr_index]); 873 if (next_symbol == NULL) 874 break; 875 876 assert (next_symbol->GetAddressRangePtr()); 877 const lldb::addr_t next_file_addr = next_symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(); 878 if (next_file_addr > curr_file_addr) 879 { 880 byte_size = next_file_addr - curr_file_addr; 881 symbol->GetAddressRangePtr()->SetByteSize(byte_size); 882 symbol->SetSizeIsSynthesized(true); 883 break; 884 } 885 } 886 } 887 } 888 return byte_size; 889} 890 891Symbol * 892Symtab::FindSymbolWithFileAddress (addr_t file_addr) 893{ 894 Mutex::Locker locker (m_mutex); 895 896 if (!m_addr_indexes_computed) 897 InitAddressIndexes(); 898 899 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 }; 900 901 uint32_t* match = (uint32_t*)::bsearch (&info, 902 &m_addr_indexes[0], 903 m_addr_indexes.size(), 904 sizeof(uint32_t), 905 (ComparisonFunction)SymbolWithFileAddress); 906 if (match) 907 return SymbolAtIndex (*match); 908 return NULL; 909} 910 911 912Symbol * 913Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) 914{ 915 Mutex::Locker locker (m_mutex); 916 917 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 }; 918 919 ::bsearch (&info, 920 indexes, 921 num_indexes, 922 sizeof(uint32_t), 923 (ComparisonFunction)SymbolWithClosestFileAddress); 924 925 if (info.match_symbol) 926 { 927 if (info.match_offset == 0) 928 { 929 // We found an exact match! 930 return info.match_symbol; 931 } 932 933 const size_t symbol_byte_size = CalculateSymbolSize(info.match_symbol); 934 935 if (symbol_byte_size == 0) 936 { 937 // We weren't able to find the size of the symbol so lets just go 938 // with that match we found in our search... 939 return info.match_symbol; 940 } 941 942 // We were able to figure out a symbol size so lets make sure our 943 // offset puts "file_addr" in the symbol's address range. 944 if (info.match_offset < symbol_byte_size) 945 return info.match_symbol; 946 } 947 return NULL; 948} 949 950Symbol * 951Symtab::FindSymbolContainingFileAddress (addr_t file_addr) 952{ 953 Mutex::Locker locker (m_mutex); 954 955 if (!m_addr_indexes_computed) 956 InitAddressIndexes(); 957 958 return FindSymbolContainingFileAddress (file_addr, &m_addr_indexes[0], m_addr_indexes.size()); 959} 960 961