Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Symtab.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 <map> |
| 11 | |
| 12 | #include "lldb/Core/Module.h" |
| 13 | #include "lldb/Core/RegularExpression.h" |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Section.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 15 | #include "lldb/Core/Timer.h" |
| 16 | #include "lldb/Symbol/ObjectFile.h" |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/SymbolContext.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 18 | #include "lldb/Symbol/Symtab.h" |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 19 | #include "lldb/Target/CPPLanguageRuntime.h" |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 20 | #include "lldb/Target/ObjCLanguageRuntime.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace lldb; |
| 23 | using namespace lldb_private; |
| 24 | |
| 25 | |
| 26 | |
| 27 | Symtab::Symtab(ObjectFile *objfile) : |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 28 | m_objfile (objfile), |
| 29 | m_symbols (), |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 30 | m_file_addr_to_index (), |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 31 | m_name_to_index (), |
| 32 | m_mutex (Mutex::eMutexTypeRecursive), |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 33 | m_file_addr_to_index_computed (false), |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 34 | m_name_indexes_computed (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | Symtab::~Symtab() |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 43 | Symtab::Reserve(size_t count) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 45 | // Clients should grab the mutex from this symbol table and lock it manually |
| 46 | // when calling this function to avoid performance issues. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 47 | m_symbols.reserve (count); |
| 48 | } |
| 49 | |
| 50 | Symbol * |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 51 | Symtab::Resize(size_t count) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 53 | // Clients should grab the mutex from this symbol table and lock it manually |
| 54 | // when calling this function to avoid performance issues. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | m_symbols.resize (count); |
| 56 | return &m_symbols[0]; |
| 57 | } |
| 58 | |
| 59 | uint32_t |
| 60 | Symtab::AddSymbol(const Symbol& symbol) |
| 61 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 62 | // Clients should grab the mutex from this symbol table and lock it manually |
| 63 | // when calling this function to avoid performance issues. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | uint32_t symbol_idx = m_symbols.size(); |
| 65 | m_name_to_index.Clear(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 66 | m_file_addr_to_index.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | m_symbols.push_back(symbol); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 68 | m_file_addr_to_index_computed = false; |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 69 | m_name_indexes_computed = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | return symbol_idx; |
| 71 | } |
| 72 | |
| 73 | size_t |
| 74 | Symtab::GetNumSymbols() const |
| 75 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 76 | Mutex::Locker locker (m_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 77 | return m_symbols.size(); |
| 78 | } |
| 79 | |
| 80 | void |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 81 | Symtab::Dump (Stream *s, Target *target, SortOrder sort_order) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 83 | Mutex::Locker locker (m_mutex); |
| 84 | |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 85 | // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 86 | s->Indent(); |
| 87 | const FileSpec &file_spec = m_objfile->GetFileSpec(); |
| 88 | const char * object_name = NULL; |
| 89 | if (m_objfile->GetModule()) |
| 90 | object_name = m_objfile->GetModule()->GetObjectName().GetCString(); |
| 91 | |
| 92 | if (file_spec) |
Greg Clayton | b5ad4ec | 2013-04-29 17:25:54 +0000 | [diff] [blame] | 93 | s->Printf("Symtab, file = %s%s%s%s, num_symbols = %lu", |
| 94 | file_spec.GetPath().c_str(), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | object_name ? "(" : "", |
| 96 | object_name ? object_name : "", |
| 97 | object_name ? ")" : "", |
| 98 | m_symbols.size()); |
| 99 | else |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 100 | s->Printf("Symtab, num_symbols = %lu", m_symbols.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | |
| 102 | if (!m_symbols.empty()) |
| 103 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 104 | switch (sort_order) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 105 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 106 | case eSortOrderNone: |
| 107 | { |
| 108 | s->PutCString (":\n"); |
| 109 | DumpSymbolHeader (s); |
| 110 | const_iterator begin = m_symbols.begin(); |
| 111 | const_iterator end = m_symbols.end(); |
| 112 | for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) |
| 113 | { |
| 114 | s->Indent(); |
| 115 | pos->Dump(s, target, std::distance(begin, pos)); |
| 116 | } |
| 117 | } |
| 118 | break; |
| 119 | |
| 120 | case eSortOrderByName: |
| 121 | { |
| 122 | // Although we maintain a lookup by exact name map, the table |
| 123 | // isn't sorted by name. So we must make the ordered symbol list |
| 124 | // up ourselves. |
| 125 | s->PutCString (" (sorted by name):\n"); |
| 126 | DumpSymbolHeader (s); |
| 127 | typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol; |
| 128 | CStringToSymbol name_map; |
| 129 | for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos) |
| 130 | { |
| 131 | const char *name = pos->GetMangled().GetName(Mangled::ePreferDemangled).AsCString(); |
| 132 | if (name && name[0]) |
| 133 | name_map.insert (std::make_pair(name, &(*pos))); |
| 134 | } |
| 135 | |
| 136 | for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos) |
| 137 | { |
| 138 | s->Indent(); |
| 139 | pos->second->Dump (s, target, pos->second - &m_symbols[0]); |
| 140 | } |
| 141 | } |
| 142 | break; |
| 143 | |
| 144 | case eSortOrderByAddress: |
| 145 | s->PutCString (" (sorted by address):\n"); |
| 146 | DumpSymbolHeader (s); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 147 | if (!m_file_addr_to_index_computed) |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 148 | InitAddressIndexes(); |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 149 | const size_t num_entries = m_file_addr_to_index.GetSize(); |
| 150 | for (size_t i=0; i<num_entries; ++i) |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 151 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 152 | s->Indent(); |
| 153 | const uint32_t symbol_idx = m_file_addr_to_index.GetEntryRef(i).data; |
| 154 | m_symbols[symbol_idx].Dump(s, target, symbol_idx); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 155 | } |
| 156 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 162 | Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 163 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 164 | Mutex::Locker locker (m_mutex); |
| 165 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | const size_t num_symbols = GetNumSymbols(); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 167 | //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | s->Indent(); |
Jason Molenda | fd54b36 | 2011-09-20 21:44:10 +0000 | [diff] [blame] | 169 | s->Printf("Symtab %lu symbol indexes (%lu symbols total):\n", indexes.size(), m_symbols.size()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | s->IndentMore(); |
| 171 | |
| 172 | if (!indexes.empty()) |
| 173 | { |
| 174 | std::vector<uint32_t>::const_iterator pos; |
| 175 | std::vector<uint32_t>::const_iterator end = indexes.end(); |
| 176 | DumpSymbolHeader (s); |
| 177 | for (pos = indexes.begin(); pos != end; ++pos) |
| 178 | { |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 179 | size_t idx = *pos; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | if (idx < num_symbols) |
| 181 | { |
| 182 | s->Indent(); |
Greg Clayton | f5e56de | 2010-09-14 23:36:40 +0000 | [diff] [blame] | 183 | m_symbols[idx].Dump(s, target, idx); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | } |
| 187 | s->IndentLess (); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | Symtab::DumpSymbolHeader (Stream *s) |
| 192 | { |
| 193 | s->Indent(" Debug symbol\n"); |
| 194 | s->Indent(" |Synthetic symbol\n"); |
| 195 | s->Indent(" ||Externally Visible\n"); |
| 196 | s->Indent(" |||\n"); |
| 197 | s->Indent("Index UserID DSX Type File Address/Value Load Address Size Flags Name\n"); |
| 198 | s->Indent("------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------\n"); |
| 199 | } |
| 200 | |
Greg Clayton | 49bd1c8 | 2010-09-07 17:36:17 +0000 | [diff] [blame] | 201 | |
| 202 | static int |
| 203 | CompareSymbolID (const void *key, const void *p) |
| 204 | { |
| 205 | const user_id_t match_uid = *(user_id_t*) key; |
| 206 | const user_id_t symbol_uid = ((Symbol *)p)->GetID(); |
| 207 | if (match_uid < symbol_uid) |
| 208 | return -1; |
| 209 | if (match_uid > symbol_uid) |
| 210 | return 1; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | Symbol * |
| 215 | Symtab::FindSymbolByID (lldb::user_id_t symbol_uid) const |
| 216 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 217 | Mutex::Locker locker (m_mutex); |
| 218 | |
Greg Clayton | 49bd1c8 | 2010-09-07 17:36:17 +0000 | [diff] [blame] | 219 | Symbol *symbol = (Symbol*)::bsearch (&symbol_uid, |
| 220 | &m_symbols[0], |
| 221 | m_symbols.size(), |
Greg Clayton | 0c38b0d | 2010-09-12 05:25:16 +0000 | [diff] [blame] | 222 | (uint8_t *)&m_symbols[1] - (uint8_t *)&m_symbols[0], |
Greg Clayton | 49bd1c8 | 2010-09-07 17:36:17 +0000 | [diff] [blame] | 223 | CompareSymbolID); |
| 224 | return symbol; |
| 225 | } |
| 226 | |
| 227 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | Symbol * |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 229 | Symtab::SymbolAtIndex(size_t idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 231 | // Clients should grab the mutex from this symbol table and lock it manually |
| 232 | // when calling this function to avoid performance issues. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 233 | if (idx < m_symbols.size()) |
| 234 | return &m_symbols[idx]; |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | const Symbol * |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 240 | Symtab::SymbolAtIndex(size_t idx) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 241 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 242 | // Clients should grab the mutex from this symbol table and lock it manually |
| 243 | // when calling this function to avoid performance issues. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | if (idx < m_symbols.size()) |
| 245 | return &m_symbols[idx]; |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | //---------------------------------------------------------------------- |
| 250 | // InitNameIndexes |
| 251 | //---------------------------------------------------------------------- |
| 252 | void |
| 253 | Symtab::InitNameIndexes() |
| 254 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 255 | // Protected function, no need to lock mutex... |
| 256 | if (!m_name_indexes_computed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 257 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 258 | m_name_indexes_computed = true; |
| 259 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 260 | // Create the name index vector to be able to quickly search by name |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 261 | const size_t num_symbols = m_symbols.size(); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 262 | #if 1 |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 263 | m_name_to_index.Reserve (num_symbols); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 264 | #else |
| 265 | // TODO: benchmark this to see if we save any memory. Otherwise we |
| 266 | // will always keep the memory reserved in the vector unless we pull |
| 267 | // some STL swap magic and then recopy... |
| 268 | uint32_t actual_count = 0; |
| 269 | for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); |
| 270 | pos != end; |
| 271 | ++pos) |
| 272 | { |
| 273 | const Mangled &mangled = pos->GetMangled(); |
| 274 | if (mangled.GetMangledName()) |
| 275 | ++actual_count; |
| 276 | |
| 277 | if (mangled.GetDemangledName()) |
| 278 | ++actual_count; |
| 279 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 280 | |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 281 | m_name_to_index.Reserve (actual_count); |
| 282 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | |
Greg Clayton | 1075aca | 2011-12-03 20:02:42 +0000 | [diff] [blame] | 284 | NameToIndexMap::Entry entry; |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 285 | |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 286 | // The "const char *" in "class_contexts" must come from a ConstString::GetCString() |
| 287 | std::set<const char *> class_contexts; |
| 288 | UniqueCStringMap<uint32_t> mangled_name_to_index; |
| 289 | std::vector<const char *> symbol_contexts(num_symbols, NULL); |
| 290 | |
| 291 | for (entry.value = 0; entry.value<num_symbols; ++entry.value) |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 292 | { |
| 293 | const Symbol *symbol = &m_symbols[entry.value]; |
| 294 | |
| 295 | // Don't let trampolines get into the lookup by name map |
| 296 | // If we ever need the trampoline symbols to be searchable by name |
| 297 | // we can remove this and then possibly add a new bool to any of the |
| 298 | // Symtab functions that lookup symbols by name to indicate if they |
| 299 | // want trampolines. |
| 300 | if (symbol->IsTrampoline()) |
| 301 | continue; |
| 302 | |
| 303 | const Mangled &mangled = symbol->GetMangled(); |
| 304 | entry.cstring = mangled.GetMangledName().GetCString(); |
| 305 | if (entry.cstring && entry.cstring[0]) |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 306 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 307 | m_name_to_index.Append (entry); |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 308 | |
| 309 | const SymbolType symbol_type = symbol->GetType(); |
| 310 | if (symbol_type == eSymbolTypeCode || symbol_type == eSymbolTypeResolver) |
| 311 | { |
| 312 | if (entry.cstring[0] == '_' && entry.cstring[1] == 'Z' && |
| 313 | (entry.cstring[2] != 'T' && // avoid virtual table, VTT structure, typeinfo structure, and typeinfo name |
| 314 | entry.cstring[2] != 'G' && // avoid guard variables |
| 315 | entry.cstring[2] != 'Z')) // named local entities (if we eventually handle eSymbolTypeData, we will want this back) |
| 316 | { |
| 317 | CPPLanguageRuntime::MethodName cxx_method (mangled.GetDemangledName()); |
Greg Clayton | 6ecb232 | 2013-05-18 00:11:21 +0000 | [diff] [blame] | 318 | entry.cstring = ConstString(cxx_method.GetBasename()).GetCString(); |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 319 | if (entry.cstring && entry.cstring[0]) |
| 320 | { |
| 321 | // ConstString objects permanently store the string in the pool so calling |
| 322 | // GetCString() on the value gets us a const char * that will never go away |
| 323 | const char *const_context = ConstString(cxx_method.GetContext()).GetCString(); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 324 | |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 325 | if (entry.cstring[0] == '~' || !cxx_method.GetQualifiers().empty()) |
| 326 | { |
| 327 | // The first character of the demangled basename is '~' which |
| 328 | // means we have a class destructor. We can use this information |
| 329 | // to help us know what is a class and what isn't. |
| 330 | if (class_contexts.find(const_context) == class_contexts.end()) |
| 331 | class_contexts.insert(const_context); |
| 332 | m_method_to_index.Append (entry); |
| 333 | } |
| 334 | else |
| 335 | { |
| 336 | if (const_context && const_context[0]) |
| 337 | { |
| 338 | if (class_contexts.find(const_context) != class_contexts.end()) |
| 339 | { |
| 340 | // The current decl context is in our "class_contexts" which means |
| 341 | // this is a method on a class |
| 342 | m_method_to_index.Append (entry); |
| 343 | } |
| 344 | else |
| 345 | { |
| 346 | // We don't know if this is a function basename or a method, |
| 347 | // so put it into a temporary collection so once we are done |
| 348 | // we can look in class_contexts to see if each entry is a class |
| 349 | // or just a function and will put any remaining items into |
| 350 | // m_method_to_index or m_basename_to_index as needed |
| 351 | mangled_name_to_index.Append (entry); |
| 352 | symbol_contexts[entry.value] = const_context; |
| 353 | } |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | // No context for this function so this has to be a basename |
| 358 | m_basename_to_index.Append(entry); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 366 | entry.cstring = mangled.GetDemangledName().GetCString(); |
| 367 | if (entry.cstring && entry.cstring[0]) |
| 368 | m_name_to_index.Append (entry); |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 369 | |
| 370 | // If the demangled name turns out to be an ObjC name, and |
| 371 | // is a category name, add the version without categories to the index too. |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 372 | ObjCLanguageRuntime::MethodName objc_method (entry.cstring, true); |
| 373 | if (objc_method.IsValid(true)) |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 374 | { |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 375 | entry.cstring = objc_method.GetSelector().GetCString(); |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 376 | m_selector_to_index.Append (entry); |
Greg Clayton | 1b3815c | 2013-01-30 00:18:29 +0000 | [diff] [blame] | 377 | |
| 378 | ConstString objc_method_no_category (objc_method.GetFullNameWithoutCategory(true)); |
| 379 | if (objc_method_no_category) |
| 380 | { |
| 381 | entry.cstring = objc_method_no_category.GetCString(); |
| 382 | m_name_to_index.Append (entry); |
| 383 | } |
Jim Ingham | ff5f5ff | 2011-08-15 01:32:22 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 386 | } |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 387 | |
| 388 | size_t count; |
| 389 | if (!mangled_name_to_index.IsEmpty()) |
| 390 | { |
| 391 | count = mangled_name_to_index.GetSize(); |
| 392 | for (size_t i=0; i<count; ++i) |
| 393 | { |
| 394 | if (mangled_name_to_index.GetValueAtIndex(i, entry.value)) |
| 395 | { |
| 396 | entry.cstring = mangled_name_to_index.GetCStringAtIndex(i); |
| 397 | if (symbol_contexts[entry.value] && class_contexts.find(symbol_contexts[entry.value]) != class_contexts.end()) |
| 398 | { |
| 399 | m_method_to_index.Append (entry); |
| 400 | } |
| 401 | else |
| 402 | { |
| 403 | // If we got here, we have something that had a context (was inside a namespace or class) |
| 404 | // yet we don't know if the entry |
| 405 | m_method_to_index.Append (entry); |
| 406 | m_basename_to_index.Append (entry); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 411 | m_name_to_index.Sort(); |
Greg Clayton | d2c46a6 | 2011-12-28 22:24:04 +0000 | [diff] [blame] | 412 | m_name_to_index.SizeToFit(); |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 413 | m_selector_to_index.Sort(); |
| 414 | m_selector_to_index.SizeToFit(); |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 415 | m_basename_to_index.Sort(); |
| 416 | m_basename_to_index.SizeToFit(); |
| 417 | m_method_to_index.Sort(); |
| 418 | m_method_to_index.SizeToFit(); |
| 419 | |
| 420 | // static StreamFile a ("/tmp/a.txt"); |
| 421 | // |
| 422 | // count = m_basename_to_index.GetSize(); |
| 423 | // if (count) |
| 424 | // { |
| 425 | // for (size_t i=0; i<count; ++i) |
| 426 | // { |
| 427 | // if (m_basename_to_index.GetValueAtIndex(i, entry.value)) |
| 428 | // a.Printf ("%s BASENAME\n", m_symbols[entry.value].GetMangled().GetName().GetCString()); |
| 429 | // } |
| 430 | // } |
| 431 | // count = m_method_to_index.GetSize(); |
| 432 | // if (count) |
| 433 | // { |
| 434 | // for (size_t i=0; i<count; ++i) |
| 435 | // { |
| 436 | // if (m_method_to_index.GetValueAtIndex(i, entry.value)) |
| 437 | // a.Printf ("%s METHOD\n", m_symbols[entry.value].GetMangled().GetName().GetCString()); |
| 438 | // } |
| 439 | // } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 440 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Greg Clayton | 1075aca | 2011-12-03 20:02:42 +0000 | [diff] [blame] | 443 | void |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 444 | Symtab::AppendSymbolNamesToMap (const IndexCollection &indexes, |
Greg Clayton | 1075aca | 2011-12-03 20:02:42 +0000 | [diff] [blame] | 445 | bool add_demangled, |
| 446 | bool add_mangled, |
| 447 | NameToIndexMap &name_to_index_map) const |
| 448 | { |
| 449 | if (add_demangled || add_mangled) |
| 450 | { |
| 451 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 452 | Mutex::Locker locker (m_mutex); |
| 453 | |
| 454 | // Create the name index vector to be able to quickly search by name |
| 455 | NameToIndexMap::Entry entry; |
| 456 | const size_t num_indexes = indexes.size(); |
| 457 | for (size_t i=0; i<num_indexes; ++i) |
| 458 | { |
| 459 | entry.value = indexes[i]; |
| 460 | assert (i < m_symbols.size()); |
| 461 | const Symbol *symbol = &m_symbols[entry.value]; |
| 462 | |
| 463 | const Mangled &mangled = symbol->GetMangled(); |
| 464 | if (add_demangled) |
| 465 | { |
| 466 | entry.cstring = mangled.GetDemangledName().GetCString(); |
| 467 | if (entry.cstring && entry.cstring[0]) |
| 468 | name_to_index_map.Append (entry); |
| 469 | } |
| 470 | |
| 471 | if (add_mangled) |
| 472 | { |
| 473 | entry.cstring = mangled.GetMangledName().GetCString(); |
| 474 | if (entry.cstring && entry.cstring[0]) |
| 475 | name_to_index_map.Append (entry); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 481 | uint32_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 482 | Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 483 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 484 | Mutex::Locker locker (m_mutex); |
| 485 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 486 | uint32_t prev_size = indexes.size(); |
| 487 | |
| 488 | const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); |
| 489 | |
| 490 | for (uint32_t i = start_idx; i < count; ++i) |
| 491 | { |
| 492 | if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) |
| 493 | indexes.push_back(i); |
| 494 | } |
| 495 | |
| 496 | return indexes.size() - prev_size; |
| 497 | } |
| 498 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 499 | uint32_t |
Greg Clayton | 16b2d2b | 2011-01-20 06:08:59 +0000 | [diff] [blame] | 500 | Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const |
| 501 | { |
| 502 | Mutex::Locker locker (m_mutex); |
| 503 | |
| 504 | uint32_t prev_size = indexes.size(); |
| 505 | |
| 506 | const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); |
| 507 | |
| 508 | for (uint32_t i = start_idx; i < count; ++i) |
| 509 | { |
| 510 | if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value) |
| 511 | indexes.push_back(i); |
| 512 | } |
| 513 | |
| 514 | return indexes.size() - prev_size; |
| 515 | } |
| 516 | |
| 517 | uint32_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 518 | Symtab::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 |
| 519 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 520 | Mutex::Locker locker (m_mutex); |
| 521 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 522 | uint32_t prev_size = indexes.size(); |
| 523 | |
| 524 | const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index); |
| 525 | |
| 526 | for (uint32_t i = start_idx; i < count; ++i) |
| 527 | { |
| 528 | if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) |
| 529 | { |
| 530 | if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility)) |
| 531 | indexes.push_back(i); |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | return indexes.size() - prev_size; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | uint32_t |
| 540 | Symtab::GetIndexForSymbol (const Symbol *symbol) const |
| 541 | { |
| 542 | const Symbol *first_symbol = &m_symbols[0]; |
| 543 | if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size()) |
| 544 | return symbol - first_symbol; |
| 545 | return UINT32_MAX; |
| 546 | } |
| 547 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 548 | struct SymbolSortInfo |
| 549 | { |
| 550 | const bool sort_by_load_addr; |
| 551 | const Symbol *symbols; |
| 552 | }; |
| 553 | |
Owen Anderson | c7da5f4 | 2010-06-16 17:34:05 +0000 | [diff] [blame] | 554 | namespace { |
| 555 | struct SymbolIndexComparator { |
| 556 | const std::vector<Symbol>& symbols; |
Jim Ingham | e2b6ad6 | 2012-05-01 01:34:15 +0000 | [diff] [blame] | 557 | std::vector<lldb::addr_t> &addr_cache; |
| 558 | |
| 559 | // Getting from the symbol to the Address to the File Address involves some work. |
| 560 | // Since there are potentially many symbols here, and we're using this for sorting so |
| 561 | // we're going to be computing the address many times, cache that in addr_cache. |
| 562 | // The array passed in has to be the same size as the symbols array passed into the |
| 563 | // member variable symbols, and should be initialized with LLDB_INVALID_ADDRESS. |
| 564 | // NOTE: You have to make addr_cache externally and pass it in because std::stable_sort |
| 565 | // makes copies of the comparator it is initially passed in, and you end up spending |
| 566 | // huge amounts of time copying this array... |
| 567 | |
| 568 | SymbolIndexComparator(const std::vector<Symbol>& s, std::vector<lldb::addr_t> &a) : symbols(s), addr_cache(a) { |
| 569 | assert (symbols.size() == addr_cache.size()); |
| 570 | } |
Owen Anderson | c7da5f4 | 2010-06-16 17:34:05 +0000 | [diff] [blame] | 571 | bool operator()(uint32_t index_a, uint32_t index_b) { |
Jim Ingham | e2b6ad6 | 2012-05-01 01:34:15 +0000 | [diff] [blame] | 572 | addr_t value_a = addr_cache[index_a]; |
| 573 | if (value_a == LLDB_INVALID_ADDRESS) |
| 574 | { |
| 575 | value_a = symbols[index_a].GetAddress().GetFileAddress(); |
| 576 | addr_cache[index_a] = value_a; |
| 577 | } |
| 578 | |
| 579 | addr_t value_b = addr_cache[index_b]; |
| 580 | if (value_b == LLDB_INVALID_ADDRESS) |
| 581 | { |
| 582 | value_b = symbols[index_b].GetAddress().GetFileAddress(); |
| 583 | addr_cache[index_b] = value_b; |
| 584 | } |
| 585 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 586 | |
Owen Anderson | c7da5f4 | 2010-06-16 17:34:05 +0000 | [diff] [blame] | 587 | if (value_a == value_b) { |
| 588 | // The if the values are equal, use the original symbol user ID |
| 589 | lldb::user_id_t uid_a = symbols[index_a].GetID(); |
| 590 | lldb::user_id_t uid_b = symbols[index_b].GetID(); |
| 591 | if (uid_a < uid_b) |
| 592 | return true; |
| 593 | if (uid_a > uid_b) |
| 594 | return false; |
| 595 | return false; |
| 596 | } else if (value_a < value_b) |
| 597 | return true; |
| 598 | |
| 599 | return false; |
| 600 | } |
| 601 | }; |
Eli Friedman | a92e332 | 2010-06-10 23:36:31 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 604 | void |
| 605 | Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const |
| 606 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 607 | Mutex::Locker locker (m_mutex); |
| 608 | |
Owen Anderson | c7da5f4 | 2010-06-16 17:34:05 +0000 | [diff] [blame] | 609 | Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 610 | // No need to sort if we have zero or one items... |
| 611 | if (indexes.size() <= 1) |
| 612 | return; |
| 613 | |
Owen Anderson | 836af6b | 2010-06-17 00:51:12 +0000 | [diff] [blame] | 614 | // Sort the indexes in place using std::stable_sort. |
| 615 | // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance, |
| 616 | // not correctness. The indexes vector tends to be "close" to sorted, which the |
| 617 | // stable sort handles better. |
Jim Ingham | e2b6ad6 | 2012-05-01 01:34:15 +0000 | [diff] [blame] | 618 | |
| 619 | std::vector<lldb::addr_t> addr_cache(m_symbols.size(), LLDB_INVALID_ADDRESS); |
| 620 | |
| 621 | SymbolIndexComparator comparator(m_symbols, addr_cache); |
| 622 | std::stable_sort(indexes.begin(), indexes.end(), comparator); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 623 | |
| 624 | // Remove any duplicates if requested |
| 625 | if (remove_duplicates) |
| 626 | std::unique(indexes.begin(), indexes.end()); |
| 627 | } |
| 628 | |
| 629 | uint32_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 630 | Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 631 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 632 | Mutex::Locker locker (m_mutex); |
| 633 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 634 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 635 | if (symbol_name) |
| 636 | { |
Greg Clayton | 38e953d | 2011-09-11 00:20:09 +0000 | [diff] [blame] | 637 | const char *symbol_cstr = symbol_name.GetCString(); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 638 | if (!m_name_indexes_computed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 639 | InitNameIndexes(); |
| 640 | |
Greg Clayton | 38e953d | 2011-09-11 00:20:09 +0000 | [diff] [blame] | 641 | return m_name_to_index.GetValues (symbol_cstr, indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 642 | } |
| 643 | return 0; |
| 644 | } |
| 645 | |
| 646 | uint32_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 647 | Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) |
| 648 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 649 | Mutex::Locker locker (m_mutex); |
| 650 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 651 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 652 | if (symbol_name) |
| 653 | { |
| 654 | const size_t old_size = indexes.size(); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 655 | if (!m_name_indexes_computed) |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 656 | InitNameIndexes(); |
| 657 | |
| 658 | const char *symbol_cstr = symbol_name.GetCString(); |
Greg Clayton | 38e953d | 2011-09-11 00:20:09 +0000 | [diff] [blame] | 659 | |
| 660 | std::vector<uint32_t> all_name_indexes; |
| 661 | const size_t name_match_count = m_name_to_index.GetValues (symbol_cstr, all_name_indexes); |
| 662 | for (size_t i=0; i<name_match_count; ++i) |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 663 | { |
Greg Clayton | 38e953d | 2011-09-11 00:20:09 +0000 | [diff] [blame] | 664 | if (CheckSymbolAtIndex(all_name_indexes[i], symbol_debug_type, symbol_visibility)) |
| 665 | indexes.push_back (all_name_indexes[i]); |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 666 | } |
| 667 | return indexes.size() - old_size; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | uint32_t |
| 673 | Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 674 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 675 | Mutex::Locker locker (m_mutex); |
| 676 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 677 | if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0) |
| 678 | { |
| 679 | std::vector<uint32_t>::iterator pos = indexes.begin(); |
| 680 | while (pos != indexes.end()) |
| 681 | { |
| 682 | if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) |
| 683 | ++pos; |
| 684 | else |
Jim Ingham | 6dafc42 | 2013-09-27 20:58:17 +0000 | [diff] [blame] | 685 | pos = indexes.erase(pos); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | return indexes.size(); |
| 689 | } |
| 690 | |
| 691 | uint32_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 692 | Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) |
| 693 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 694 | Mutex::Locker locker (m_mutex); |
| 695 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 696 | if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0) |
| 697 | { |
| 698 | std::vector<uint32_t>::iterator pos = indexes.begin(); |
| 699 | while (pos != indexes.end()) |
| 700 | { |
| 701 | if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type) |
| 702 | ++pos; |
| 703 | else |
Jim Ingham | 6dafc42 | 2013-09-27 20:58:17 +0000 | [diff] [blame] | 704 | pos = indexes.erase(pos); |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 705 | } |
| 706 | } |
| 707 | return indexes.size(); |
| 708 | } |
| 709 | |
| 710 | |
| 711 | uint32_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 712 | Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, std::vector<uint32_t>& indexes) |
| 713 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 714 | Mutex::Locker locker (m_mutex); |
| 715 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 716 | uint32_t prev_size = indexes.size(); |
| 717 | uint32_t sym_end = m_symbols.size(); |
| 718 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 719 | for (uint32_t i = 0; i < sym_end; i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 720 | { |
| 721 | if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) |
| 722 | { |
| 723 | const char *name = m_symbols[i].GetMangled().GetName().AsCString(); |
| 724 | if (name) |
| 725 | { |
| 726 | if (regexp.Execute (name)) |
| 727 | indexes.push_back(i); |
| 728 | } |
| 729 | } |
| 730 | } |
| 731 | return indexes.size() - prev_size; |
| 732 | |
| 733 | } |
| 734 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 735 | uint32_t |
| 736 | Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression ®exp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 737 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 738 | Mutex::Locker locker (m_mutex); |
| 739 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 740 | uint32_t prev_size = indexes.size(); |
| 741 | uint32_t sym_end = m_symbols.size(); |
| 742 | |
Andy Gibbs | a297a97 | 2013-06-19 19:04:53 +0000 | [diff] [blame] | 743 | for (uint32_t i = 0; i < sym_end; i++) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 744 | { |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 745 | if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 746 | { |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 747 | if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false) |
| 748 | continue; |
| 749 | |
| 750 | const char *name = m_symbols[i].GetMangled().GetName().AsCString(); |
| 751 | if (name) |
| 752 | { |
| 753 | if (regexp.Execute (name)) |
| 754 | indexes.push_back(i); |
| 755 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 758 | return indexes.size() - prev_size; |
| 759 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 762 | Symbol * |
| 763 | Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 764 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 765 | Mutex::Locker locker (m_mutex); |
| 766 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 767 | const size_t count = m_symbols.size(); |
Greg Clayton | c7bece56 | 2013-01-25 18:06:21 +0000 | [diff] [blame] | 768 | for (size_t idx = start_idx; idx < count; ++idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 769 | { |
| 770 | if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type) |
| 771 | { |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 772 | if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility)) |
| 773 | { |
| 774 | start_idx = idx; |
| 775 | return &m_symbols[idx]; |
| 776 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 777 | } |
| 778 | } |
| 779 | return NULL; |
| 780 | } |
| 781 | |
| 782 | size_t |
| 783 | Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes) |
| 784 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 785 | Mutex::Locker locker (m_mutex); |
| 786 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 787 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 788 | // Initialize all of the lookup by name indexes before converting NAME |
| 789 | // to a uniqued string NAME_STR below. |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 790 | if (!m_name_indexes_computed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 791 | InitNameIndexes(); |
| 792 | |
| 793 | if (name) |
| 794 | { |
| 795 | // The string table did have a string that matched, but we need |
| 796 | // to check the symbols and match the symbol_type if any was given. |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 797 | AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 798 | } |
| 799 | return symbol_indexes.size(); |
| 800 | } |
| 801 | |
| 802 | size_t |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 803 | Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 804 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 805 | Mutex::Locker locker (m_mutex); |
| 806 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 807 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
| 808 | // Initialize all of the lookup by name indexes before converting NAME |
| 809 | // to a uniqued string NAME_STR below. |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 810 | if (!m_name_indexes_computed) |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 811 | InitNameIndexes(); |
| 812 | |
| 813 | if (name) |
| 814 | { |
| 815 | // The string table did have a string that matched, but we need |
| 816 | // to check the symbols and match the symbol_type if any was given. |
| 817 | AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); |
| 818 | } |
| 819 | return symbol_indexes.size(); |
| 820 | } |
| 821 | |
| 822 | size_t |
| 823 | Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression ®ex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes) |
| 824 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 825 | Mutex::Locker locker (m_mutex); |
| 826 | |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 827 | AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | return symbol_indexes.size(); |
| 829 | } |
| 830 | |
| 831 | Symbol * |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 832 | Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 834 | Mutex::Locker locker (m_mutex); |
| 835 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 836 | Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__); |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 837 | if (!m_name_indexes_computed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 838 | InitNameIndexes(); |
| 839 | |
| 840 | if (name) |
| 841 | { |
| 842 | std::vector<uint32_t> matching_indexes; |
| 843 | // The string table did have a string that matched, but we need |
| 844 | // to check the symbols and match the symbol_type if any was given. |
Greg Clayton | bcf2cfb | 2010-09-11 03:13:28 +0000 | [diff] [blame] | 845 | if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 846 | { |
| 847 | std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end(); |
| 848 | for (pos = matching_indexes.begin(); pos != end; ++pos) |
| 849 | { |
| 850 | Symbol *symbol = SymbolAtIndex(*pos); |
| 851 | |
| 852 | if (symbol->Compare(name, symbol_type)) |
| 853 | return symbol; |
| 854 | } |
| 855 | } |
| 856 | } |
| 857 | return NULL; |
| 858 | } |
| 859 | |
| 860 | typedef struct |
| 861 | { |
| 862 | const Symtab *symtab; |
| 863 | const addr_t file_addr; |
| 864 | Symbol *match_symbol; |
| 865 | const uint32_t *match_index_ptr; |
| 866 | addr_t match_offset; |
| 867 | } SymbolSearchInfo; |
| 868 | |
| 869 | static int |
| 870 | SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) |
| 871 | { |
| 872 | const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]); |
| 873 | if (curr_symbol == NULL) |
| 874 | return -1; |
| 875 | |
| 876 | const addr_t info_file_addr = info->file_addr; |
| 877 | |
| 878 | // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address |
| 879 | // range if the symbol has a section! |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 880 | if (curr_symbol->ValueIsAddress()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 881 | { |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 882 | const addr_t curr_file_addr = curr_symbol->GetAddress().GetFileAddress(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 883 | if (info_file_addr < curr_file_addr) |
| 884 | return -1; |
| 885 | if (info_file_addr > curr_file_addr) |
| 886 | return +1; |
| 887 | info->match_symbol = const_cast<Symbol *>(curr_symbol); |
| 888 | info->match_index_ptr = index_ptr; |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | return -1; |
| 893 | } |
| 894 | |
| 895 | static int |
| 896 | SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr) |
| 897 | { |
| 898 | const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]); |
| 899 | if (symbol == NULL) |
| 900 | return -1; |
| 901 | |
| 902 | const addr_t info_file_addr = info->file_addr; |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 903 | if (symbol->ValueIsAddress()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 904 | { |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 905 | const addr_t curr_file_addr = symbol->GetAddress().GetFileAddress(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 906 | if (info_file_addr < curr_file_addr) |
| 907 | return -1; |
| 908 | |
| 909 | // Since we are finding the closest symbol that is greater than or equal |
| 910 | // to 'info->file_addr' we set the symbol here. This will get set |
| 911 | // multiple times, but after the search is done it will contain the best |
| 912 | // symbol match |
| 913 | info->match_symbol = const_cast<Symbol *>(symbol); |
| 914 | info->match_index_ptr = index_ptr; |
| 915 | info->match_offset = info_file_addr - curr_file_addr; |
| 916 | |
| 917 | if (info_file_addr > curr_file_addr) |
| 918 | return +1; |
| 919 | return 0; |
| 920 | } |
| 921 | return -1; |
| 922 | } |
| 923 | |
| 924 | static SymbolSearchInfo |
| 925 | FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) |
| 926 | { |
| 927 | SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 }; |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 928 | ::bsearch (&info, |
| 929 | indexes, |
| 930 | num_indexes, |
| 931 | sizeof(uint32_t), |
| 932 | (ComparisonFunction)SymbolWithClosestFileAddress); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 933 | return info; |
| 934 | } |
| 935 | |
| 936 | |
| 937 | void |
| 938 | Symtab::InitAddressIndexes() |
| 939 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 940 | // Protected function, no need to lock mutex... |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 941 | if (!m_file_addr_to_index_computed && !m_symbols.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 942 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 943 | m_file_addr_to_index_computed = true; |
Greg Clayton | 9594f4c | 2013-04-13 23:17:23 +0000 | [diff] [blame] | 944 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 945 | FileRangeToIndexMap::Entry entry; |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 946 | const_iterator begin = m_symbols.begin(); |
| 947 | const_iterator end = m_symbols.end(); |
| 948 | for (const_iterator pos = m_symbols.begin(); pos != end; ++pos) |
| 949 | { |
Greg Clayton | e761213 | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 950 | if (pos->ValueIsAddress()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 951 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 952 | entry.SetRangeBase(pos->GetAddress().GetFileAddress()); |
| 953 | entry.SetByteSize(pos->GetByteSize()); |
| 954 | entry.data = std::distance(begin, pos); |
| 955 | m_file_addr_to_index.Append(entry); |
| 956 | } |
| 957 | } |
| 958 | const size_t num_entries = m_file_addr_to_index.GetSize(); |
| 959 | if (num_entries > 0) |
| 960 | { |
| 961 | m_file_addr_to_index.Sort(); |
| 962 | m_file_addr_to_index.CalculateSizesOfZeroByteSizeRanges(); |
| 963 | |
| 964 | // Now our last symbols might not have had sizes because there |
| 965 | // was no subsequent symbol to calculate the size from. If this is |
| 966 | // the case, then calculate the size by capping it at the end of the |
| 967 | // section in which the symbol resides |
| 968 | for (int i = num_entries - 1; i >= 0; --i) |
| 969 | { |
| 970 | const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i); |
| 971 | // As we iterate backwards, as soon as we find a symbol with a valid |
| 972 | // byte size, we are done |
| 973 | if (entry.GetByteSize() > 0) |
| 974 | break; |
| 975 | |
| 976 | // Cap the size to the end of the section in which the symbol resides |
| 977 | SectionSP section_sp (m_objfile->GetSectionList()->FindSectionContainingFileAddress (entry.GetRangeBase())); |
| 978 | if (section_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 979 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 980 | const lldb::addr_t end_section_file_addr = section_sp->GetFileAddress() + section_sp->GetByteSize(); |
| 981 | const lldb::addr_t symbol_file_addr = entry.GetRangeBase(); |
| 982 | if (end_section_file_addr > symbol_file_addr) |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 983 | { |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 984 | Symbol &symbol = m_symbols[entry.data]; |
| 985 | |
| 986 | symbol.SetByteSize(end_section_file_addr - symbol_file_addr); |
| 987 | symbol.SetSizeIsSynthesized(true); |
Greg Clayton | 9422dd6 | 2013-03-04 21:46:16 +0000 | [diff] [blame] | 988 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 991 | // Sort again in case the range size changes the ordering |
| 992 | m_file_addr_to_index.Sort(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 993 | } |
| 994 | } |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | void |
| 998 | Symtab::CalculateSymbolSizes () |
| 999 | { |
| 1000 | Mutex::Locker locker (m_mutex); |
| 1001 | |
| 1002 | if (!m_symbols.empty()) |
| 1003 | { |
| 1004 | if (!m_file_addr_to_index_computed) |
| 1005 | InitAddressIndexes(); |
| 1006 | |
| 1007 | const size_t num_entries = m_file_addr_to_index.GetSize(); |
| 1008 | |
| 1009 | for (size_t i = 0; i < num_entries; ++i) |
| 1010 | { |
| 1011 | // The entries in the m_file_addr_to_index have calculated the sizes already |
| 1012 | // so we will use this size if we need to. |
| 1013 | const FileRangeToIndexMap::Entry &entry = m_file_addr_to_index.GetEntryRef(i); |
| 1014 | |
| 1015 | Symbol &symbol = m_symbols[entry.data]; |
| 1016 | |
| 1017 | // If the symbol size is already valid, no need to do anything |
| 1018 | if (symbol.GetByteSizeIsValid()) |
| 1019 | continue; |
| 1020 | |
| 1021 | const addr_t range_size = entry.GetByteSize(); |
| 1022 | if (range_size > 0) |
| 1023 | { |
| 1024 | symbol.SetByteSize(range_size); |
| 1025 | symbol.SetSizeIsSynthesized(true); |
| 1026 | } |
| 1027 | } |
| 1028 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | Symbol * |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1032 | Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes) |
| 1033 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 1034 | Mutex::Locker locker (m_mutex); |
| 1035 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1036 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1037 | SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 }; |
| 1038 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 1039 | ::bsearch (&info, |
| 1040 | indexes, |
| 1041 | num_indexes, |
| 1042 | sizeof(uint32_t), |
| 1043 | (ComparisonFunction)SymbolWithClosestFileAddress); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (info.match_symbol) |
| 1046 | { |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1047 | if (info.match_offset == 0) |
| 1048 | { |
| 1049 | // We found an exact match! |
| 1050 | return info.match_symbol; |
| 1051 | } |
| 1052 | |
Greg Clayton | da171f1 | 2012-03-02 03:01:16 +0000 | [diff] [blame] | 1053 | const size_t symbol_byte_size = info.match_symbol->GetByteSize(); |
Greg Clayton | 8941142 | 2010-10-08 00:21:05 +0000 | [diff] [blame] | 1054 | |
| 1055 | if (symbol_byte_size == 0) |
| 1056 | { |
| 1057 | // We weren't able to find the size of the symbol so lets just go |
| 1058 | // with that match we found in our search... |
| 1059 | return info.match_symbol; |
| 1060 | } |
| 1061 | |
| 1062 | // We were able to figure out a symbol size so lets make sure our |
| 1063 | // offset puts "file_addr" in the symbol's address range. |
| 1064 | if (info.match_offset < symbol_byte_size) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1065 | return info.match_symbol; |
| 1066 | } |
| 1067 | return NULL; |
| 1068 | } |
| 1069 | |
| 1070 | Symbol * |
| 1071 | Symtab::FindSymbolContainingFileAddress (addr_t file_addr) |
| 1072 | { |
Greg Clayton | 8087ca2 | 2010-10-08 04:20:14 +0000 | [diff] [blame] | 1073 | Mutex::Locker locker (m_mutex); |
| 1074 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1075 | if (!m_file_addr_to_index_computed) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | InitAddressIndexes(); |
| 1077 | |
Greg Clayton | 3046e66 | 2013-07-10 01:23:25 +0000 | [diff] [blame] | 1078 | const FileRangeToIndexMap::Entry *entry = m_file_addr_to_index.FindEntryThatContains(file_addr); |
| 1079 | if (entry) |
| 1080 | return SymbolAtIndex(entry->data); |
| 1081 | return NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1084 | void |
| 1085 | Symtab::SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes, SymbolContextList &sc_list) |
| 1086 | { |
| 1087 | // No need to protect this call using m_mutex all other method calls are |
| 1088 | // already thread safe. |
| 1089 | |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 1090 | const bool merge_symbol_into_function = true; |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1091 | size_t num_indices = symbol_indexes.size(); |
| 1092 | if (num_indices > 0) |
| 1093 | { |
| 1094 | SymbolContext sc; |
| 1095 | sc.module_sp = m_objfile->GetModule(); |
| 1096 | for (size_t i = 0; i < num_indices; i++) |
| 1097 | { |
| 1098 | sc.symbol = SymbolAtIndex (symbol_indexes[i]); |
| 1099 | if (sc.symbol) |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 1100 | sc_list.AppendIfUnique(sc, merge_symbol_into_function); |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | |
| 1106 | size_t |
| 1107 | Symtab::FindFunctionSymbols (const ConstString &name, |
| 1108 | uint32_t name_type_mask, |
| 1109 | SymbolContextList& sc_list) |
| 1110 | { |
| 1111 | size_t count = 0; |
| 1112 | std::vector<uint32_t> symbol_indexes; |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 1113 | |
| 1114 | const char *name_cstr = name.GetCString(); |
| 1115 | |
| 1116 | // eFunctionNameTypeAuto should be pre-resolved by a call to Module::PrepareForFunctionNameLookup() |
| 1117 | assert ((name_type_mask & eFunctionNameTypeAuto) == 0); |
| 1118 | |
| 1119 | if (name_type_mask & (eFunctionNameTypeBase | eFunctionNameTypeFull)) |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1120 | { |
Matt Kopec | 5e6a5d6 | 2013-04-22 17:02:04 +0000 | [diff] [blame] | 1121 | std::vector<uint32_t> temp_symbol_indexes; |
| 1122 | FindAllSymbolsWithNameAndType (name, eSymbolTypeAny, temp_symbol_indexes); |
| 1123 | |
| 1124 | unsigned temp_symbol_indexes_size = temp_symbol_indexes.size(); |
| 1125 | if (temp_symbol_indexes_size > 0) |
| 1126 | { |
| 1127 | Mutex::Locker locker (m_mutex); |
| 1128 | for (unsigned i = 0; i < temp_symbol_indexes_size; i++) |
| 1129 | { |
| 1130 | SymbolContext sym_ctx; |
| 1131 | sym_ctx.symbol = SymbolAtIndex (temp_symbol_indexes[i]); |
| 1132 | if (sym_ctx.symbol) |
| 1133 | { |
| 1134 | switch (sym_ctx.symbol->GetType()) |
| 1135 | { |
| 1136 | case eSymbolTypeCode: |
| 1137 | case eSymbolTypeResolver: |
Greg Clayton | 9191db4 | 2013-10-21 18:40:51 +0000 | [diff] [blame^] | 1138 | case eSymbolTypeReExported: |
Matt Kopec | 5e6a5d6 | 2013-04-22 17:02:04 +0000 | [diff] [blame] | 1139 | symbol_indexes.push_back(temp_symbol_indexes[i]); |
| 1140 | break; |
| 1141 | default: |
| 1142 | break; |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | } |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 1149 | if (name_type_mask & eFunctionNameTypeBase) |
| 1150 | { |
| 1151 | // From mangled names we can't tell what is a basename and what |
| 1152 | // is a method name, so we just treat them the same |
| 1153 | if (!m_name_indexes_computed) |
| 1154 | InitNameIndexes(); |
| 1155 | |
| 1156 | if (!m_basename_to_index.IsEmpty()) |
| 1157 | { |
| 1158 | const UniqueCStringMap<uint32_t>::Entry *match; |
| 1159 | for (match = m_basename_to_index.FindFirstValueForName(name_cstr); |
| 1160 | match != NULL; |
| 1161 | match = m_basename_to_index.FindNextValueForName(match)) |
| 1162 | { |
| 1163 | symbol_indexes.push_back(match->value); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | if (name_type_mask & eFunctionNameTypeMethod) |
| 1169 | { |
| 1170 | if (!m_name_indexes_computed) |
| 1171 | InitNameIndexes(); |
| 1172 | |
| 1173 | if (!m_method_to_index.IsEmpty()) |
| 1174 | { |
| 1175 | const UniqueCStringMap<uint32_t>::Entry *match; |
| 1176 | for (match = m_method_to_index.FindFirstValueForName(name_cstr); |
| 1177 | match != NULL; |
| 1178 | match = m_method_to_index.FindNextValueForName(match)) |
| 1179 | { |
| 1180 | symbol_indexes.push_back(match->value); |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1185 | if (name_type_mask & eFunctionNameTypeSelector) |
| 1186 | { |
| 1187 | if (!m_name_indexes_computed) |
| 1188 | InitNameIndexes(); |
| 1189 | |
| 1190 | if (!m_selector_to_index.IsEmpty()) |
| 1191 | { |
| 1192 | const UniqueCStringMap<uint32_t>::Entry *match; |
Greg Clayton | 43fe217 | 2013-04-03 02:00:15 +0000 | [diff] [blame] | 1193 | for (match = m_selector_to_index.FindFirstValueForName(name_cstr); |
Greg Clayton | c1b2ccf | 2013-01-08 00:01:36 +0000 | [diff] [blame] | 1194 | match != NULL; |
| 1195 | match = m_selector_to_index.FindNextValueForName(match)) |
| 1196 | { |
| 1197 | symbol_indexes.push_back(match->value); |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | if (!symbol_indexes.empty()) |
| 1203 | { |
| 1204 | std::sort(symbol_indexes.begin(), symbol_indexes.end()); |
| 1205 | symbol_indexes.erase(std::unique(symbol_indexes.begin(), symbol_indexes.end()), symbol_indexes.end()); |
| 1206 | count = symbol_indexes.size(); |
| 1207 | SymbolIndicesToSymbolContextList (symbol_indexes, sc_list); |
| 1208 | } |
| 1209 | |
| 1210 | return count; |
| 1211 | } |
| 1212 | |