blob: bd98f3276a4c1bfc9204f14c095c5b5d9b5bdbdf [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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"
14#include "lldb/Core/Timer.h"
15#include "lldb/Symbol/ObjectFile.h"
16#include "lldb/Symbol/Symtab.h"
Jim Inghamff5f5ff2011-08-15 01:32:22 +000017#include "lldb/Target/ObjCLanguageRuntime.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000018
19using namespace lldb;
20using namespace lldb_private;
21
22
23
24Symtab::Symtab(ObjectFile *objfile) :
Greg Clayton8087ca22010-10-08 04:20:14 +000025 m_objfile (objfile),
26 m_symbols (),
27 m_addr_indexes (),
28 m_name_to_index (),
29 m_mutex (Mutex::eMutexTypeRecursive),
30 m_addr_indexes_computed (false),
31 m_name_indexes_computed (false)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000032{
33}
34
35Symtab::~Symtab()
36{
37}
38
39void
40Symtab::Reserve(uint32_t count)
41{
Greg Clayton8087ca22010-10-08 04:20:14 +000042 // Clients should grab the mutex from this symbol table and lock it manually
43 // when calling this function to avoid performance issues.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044 m_symbols.reserve (count);
45}
46
47Symbol *
48Symtab::Resize(uint32_t count)
49{
Greg Clayton8087ca22010-10-08 04:20:14 +000050 // Clients should grab the mutex from this symbol table and lock it manually
51 // when calling this function to avoid performance issues.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052 m_symbols.resize (count);
53 return &m_symbols[0];
54}
55
56uint32_t
57Symtab::AddSymbol(const Symbol& symbol)
58{
Greg Clayton8087ca22010-10-08 04:20:14 +000059 // Clients should grab the mutex from this symbol table and lock it manually
60 // when calling this function to avoid performance issues.
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061 uint32_t symbol_idx = m_symbols.size();
62 m_name_to_index.Clear();
63 m_addr_indexes.clear();
64 m_symbols.push_back(symbol);
Greg Clayton8087ca22010-10-08 04:20:14 +000065 m_addr_indexes_computed = false;
66 m_name_indexes_computed = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067 return symbol_idx;
68}
69
70size_t
71Symtab::GetNumSymbols() const
72{
Greg Clayton8087ca22010-10-08 04:20:14 +000073 Mutex::Locker locker (m_mutex);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074 return m_symbols.size();
75}
76
77void
Greg Claytone0d378b2011-03-24 21:19:54 +000078Symtab::Dump (Stream *s, Target *target, SortOrder sort_order)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079{
Greg Clayton8087ca22010-10-08 04:20:14 +000080 Mutex::Locker locker (m_mutex);
81
Greg Clayton89411422010-10-08 00:21:05 +000082// s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000083 s->Indent();
84 const FileSpec &file_spec = m_objfile->GetFileSpec();
85 const char * object_name = NULL;
86 if (m_objfile->GetModule())
87 object_name = m_objfile->GetModule()->GetObjectName().GetCString();
88
89 if (file_spec)
Greg Clayton8087ca22010-10-08 04:20:14 +000090 s->Printf("Symtab, file = %s/%s%s%s%s, num_symbols = %u",
Chris Lattner30fdc8d2010-06-08 16:52:24 +000091 file_spec.GetDirectory().AsCString(),
92 file_spec.GetFilename().AsCString(),
93 object_name ? "(" : "",
94 object_name ? object_name : "",
95 object_name ? ")" : "",
96 m_symbols.size());
97 else
Greg Clayton8087ca22010-10-08 04:20:14 +000098 s->Printf("Symtab, num_symbols = %u", m_symbols.size());
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099
100 if (!m_symbols.empty())
101 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000102 switch (sort_order)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000103 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000104 case eSortOrderNone:
105 {
106 s->PutCString (":\n");
107 DumpSymbolHeader (s);
108 const_iterator begin = m_symbols.begin();
109 const_iterator end = m_symbols.end();
110 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
111 {
112 s->Indent();
113 pos->Dump(s, target, std::distance(begin, pos));
114 }
115 }
116 break;
117
118 case eSortOrderByName:
119 {
120 // Although we maintain a lookup by exact name map, the table
121 // isn't sorted by name. So we must make the ordered symbol list
122 // up ourselves.
123 s->PutCString (" (sorted by name):\n");
124 DumpSymbolHeader (s);
125 typedef std::multimap<const char*, const Symbol *, CStringCompareFunctionObject> CStringToSymbol;
126 CStringToSymbol name_map;
127 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end(); pos != end; ++pos)
128 {
129 const char *name = pos->GetMangled().GetName(Mangled::ePreferDemangled).AsCString();
130 if (name && name[0])
131 name_map.insert (std::make_pair(name, &(*pos)));
132 }
133
134 for (CStringToSymbol::const_iterator pos = name_map.begin(), end = name_map.end(); pos != end; ++pos)
135 {
136 s->Indent();
137 pos->second->Dump (s, target, pos->second - &m_symbols[0]);
138 }
139 }
140 break;
141
142 case eSortOrderByAddress:
143 s->PutCString (" (sorted by address):\n");
144 DumpSymbolHeader (s);
145 if (!m_addr_indexes_computed)
146 InitAddressIndexes();
147 const size_t num_symbols = GetNumSymbols();
148 std::vector<uint32_t>::const_iterator pos;
149 std::vector<uint32_t>::const_iterator end = m_addr_indexes.end();
150 for (pos = m_addr_indexes.begin(); pos != end; ++pos)
151 {
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;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 }
161 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162}
163
164void
Greg Claytonf5e56de2010-09-14 23:36:40 +0000165Symtab::Dump(Stream *s, Target *target, std::vector<uint32_t>& indexes) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166{
Greg Clayton8087ca22010-10-08 04:20:14 +0000167 Mutex::Locker locker (m_mutex);
168
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000169 const size_t num_symbols = GetNumSymbols();
Greg Clayton8087ca22010-10-08 04:20:14 +0000170 //s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171 s->Indent();
172 s->Printf("Symtab %u symbol indexes (%u 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();
Greg Claytonf5e56de2010-09-14 23:36:40 +0000186 m_symbols[idx].Dump(s, target, idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000187 }
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
Greg Clayton49bd1c82010-09-07 17:36:17 +0000204
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{
Greg Clayton8087ca22010-10-08 04:20:14 +0000220 Mutex::Locker locker (m_mutex);
221
Greg Clayton49bd1c82010-09-07 17:36:17 +0000222 Symbol *symbol = (Symbol*)::bsearch (&symbol_uid,
223 &m_symbols[0],
224 m_symbols.size(),
Greg Clayton0c38b0d2010-09-12 05:25:16 +0000225 (uint8_t *)&m_symbols[1] - (uint8_t *)&m_symbols[0],
Greg Clayton49bd1c82010-09-07 17:36:17 +0000226 CompareSymbolID);
227 return symbol;
228}
229
230
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000231Symbol *
232Symtab::SymbolAtIndex(uint32_t idx)
233{
Greg Clayton8087ca22010-10-08 04:20:14 +0000234 // Clients should grab the mutex from this symbol table and lock it manually
235 // when calling this function to avoid performance issues.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000236 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{
Greg Clayton8087ca22010-10-08 04:20:14 +0000245 // Clients should grab the mutex from this symbol table and lock it manually
246 // when calling this function to avoid performance issues.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000247 if (idx < m_symbols.size())
248 return &m_symbols[idx];
249 return NULL;
250}
251
252//----------------------------------------------------------------------
253// InitNameIndexes
254//----------------------------------------------------------------------
255void
256Symtab::InitNameIndexes()
257{
Greg Clayton8087ca22010-10-08 04:20:14 +0000258 // Protected function, no need to lock mutex...
259 if (!m_name_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000260 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000261 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 assert(m_objfile != NULL);
266 assert(m_objfile->GetModule() != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267
Greg Clayton8087ca22010-10-08 04:20:14 +0000268#if 1
269 m_name_to_index.Reserve (count);
270#else
271 // TODO: benchmark this to see if we save any memory. Otherwise we
272 // will always keep the memory reserved in the vector unless we pull
273 // some STL swap magic and then recopy...
274 uint32_t actual_count = 0;
275 for (const_iterator pos = m_symbols.begin(), end = m_symbols.end();
276 pos != end;
277 ++pos)
278 {
279 const Mangled &mangled = pos->GetMangled();
280 if (mangled.GetMangledName())
281 ++actual_count;
282
283 if (mangled.GetDemangledName())
284 ++actual_count;
285 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000286
Greg Clayton8087ca22010-10-08 04:20:14 +0000287 m_name_to_index.Reserve (actual_count);
288#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000289
Greg Clayton8087ca22010-10-08 04:20:14 +0000290 UniqueCStringMap<uint32_t>::Entry entry;
291
292 for (entry.value = 0; entry.value < count; ++entry.value)
293 {
294 const Symbol *symbol = &m_symbols[entry.value];
295
296 // Don't let trampolines get into the lookup by name map
297 // If we ever need the trampoline symbols to be searchable by name
298 // we can remove this and then possibly add a new bool to any of the
299 // Symtab functions that lookup symbols by name to indicate if they
300 // want trampolines.
301 if (symbol->IsTrampoline())
302 continue;
303
304 const Mangled &mangled = symbol->GetMangled();
305 entry.cstring = mangled.GetMangledName().GetCString();
306 if (entry.cstring && entry.cstring[0])
307 m_name_to_index.Append (entry);
308
309 entry.cstring = mangled.GetDemangledName().GetCString();
310 if (entry.cstring && entry.cstring[0])
311 m_name_to_index.Append (entry);
Jim Inghamff5f5ff2011-08-15 01:32:22 +0000312
313 // If the demangled name turns out to be an ObjC name, and
314 // is a category name, add the version without categories to the index too.
315 ConstString objc_base_name;
316 if (ObjCLanguageRuntime::ParseMethodName (entry.cstring,
317 NULL,
318 NULL,
319 &objc_base_name)
320 && !objc_base_name.IsEmpty())
321 {
322 entry.cstring = objc_base_name.GetCString();
323 m_name_to_index.Append (entry);
324 }
325
Greg Clayton8087ca22010-10-08 04:20:14 +0000326 }
327 m_name_to_index.Sort();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000328 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329}
330
331uint32_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000332Symtab::AppendSymbolIndexesWithType (SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000333{
Greg Clayton8087ca22010-10-08 04:20:14 +0000334 Mutex::Locker locker (m_mutex);
335
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000336 uint32_t prev_size = indexes.size();
337
338 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
339
340 for (uint32_t i = start_idx; i < count; ++i)
341 {
342 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
343 indexes.push_back(i);
344 }
345
346 return indexes.size() - prev_size;
347}
348
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000349uint32_t
Greg Clayton16b2d2b2011-01-20 06:08:59 +0000350Symtab::AppendSymbolIndexesWithTypeAndFlagsValue (SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx, uint32_t end_index) const
351{
352 Mutex::Locker locker (m_mutex);
353
354 uint32_t prev_size = indexes.size();
355
356 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
357
358 for (uint32_t i = start_idx; i < count; ++i)
359 {
360 if ((symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type) && m_symbols[i].GetFlags() == flags_value)
361 indexes.push_back(i);
362 }
363
364 return indexes.size() - prev_size;
365}
366
367uint32_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000368Symtab::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
369{
Greg Clayton8087ca22010-10-08 04:20:14 +0000370 Mutex::Locker locker (m_mutex);
371
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000372 uint32_t prev_size = indexes.size();
373
374 const uint32_t count = std::min<uint32_t> (m_symbols.size(), end_index);
375
376 for (uint32_t i = start_idx; i < count; ++i)
377 {
378 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
379 {
380 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility))
381 indexes.push_back(i);
382 }
383 }
384
385 return indexes.size() - prev_size;
386}
387
388
389uint32_t
390Symtab::GetIndexForSymbol (const Symbol *symbol) const
391{
392 const Symbol *first_symbol = &m_symbols[0];
393 if (symbol >= first_symbol && symbol < first_symbol + m_symbols.size())
394 return symbol - first_symbol;
395 return UINT32_MAX;
396}
397
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000398struct SymbolSortInfo
399{
400 const bool sort_by_load_addr;
401 const Symbol *symbols;
402};
403
Owen Andersonc7da5f42010-06-16 17:34:05 +0000404namespace {
405 struct SymbolIndexComparator {
406 const std::vector<Symbol>& symbols;
407 SymbolIndexComparator(const std::vector<Symbol>& s) : symbols(s) { }
408 bool operator()(uint32_t index_a, uint32_t index_b) {
409 addr_t value_a;
410 addr_t value_b;
411 if (symbols[index_a].GetValue().GetSection() == symbols[index_b].GetValue().GetSection()) {
412 value_a = symbols[index_a].GetValue ().GetOffset();
413 value_b = symbols[index_b].GetValue ().GetOffset();
414 } else {
415 value_a = symbols[index_a].GetValue ().GetFileAddress();
416 value_b = symbols[index_b].GetValue ().GetFileAddress();
417 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000418
Owen Andersonc7da5f42010-06-16 17:34:05 +0000419 if (value_a == value_b) {
420 // The if the values are equal, use the original symbol user ID
421 lldb::user_id_t uid_a = symbols[index_a].GetID();
422 lldb::user_id_t uid_b = symbols[index_b].GetID();
423 if (uid_a < uid_b)
424 return true;
425 if (uid_a > uid_b)
426 return false;
427 return false;
428 } else if (value_a < value_b)
429 return true;
430
431 return false;
432 }
433 };
Eli Friedmana92e3322010-06-10 23:36:31 +0000434}
435
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436void
437Symtab::SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const
438{
Greg Clayton8087ca22010-10-08 04:20:14 +0000439 Mutex::Locker locker (m_mutex);
440
Owen Andersonc7da5f42010-06-16 17:34:05 +0000441 Timer scoped_timer (__PRETTY_FUNCTION__,__PRETTY_FUNCTION__);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000442 // No need to sort if we have zero or one items...
443 if (indexes.size() <= 1)
444 return;
445
Owen Anderson836af6b2010-06-17 00:51:12 +0000446 // Sort the indexes in place using std::stable_sort.
447 // NOTE: The use of std::stable_sort instead of std::sort here is strictly for performance,
448 // not correctness. The indexes vector tends to be "close" to sorted, which the
449 // stable sort handles better.
Owen Andersonc7da5f42010-06-16 17:34:05 +0000450 std::stable_sort(indexes.begin(), indexes.end(), SymbolIndexComparator(m_symbols));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451
452 // Remove any duplicates if requested
453 if (remove_duplicates)
454 std::unique(indexes.begin(), indexes.end());
455}
456
457uint32_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000458Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& indexes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459{
Greg Clayton8087ca22010-10-08 04:20:14 +0000460 Mutex::Locker locker (m_mutex);
461
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
463 if (symbol_name)
464 {
465 const size_t old_size = indexes.size();
Greg Clayton8087ca22010-10-08 04:20:14 +0000466 if (!m_name_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000467 InitNameIndexes();
468
469 const char *symbol_cstr = symbol_name.GetCString();
470 const UniqueCStringMap<uint32_t>::Entry *entry_ptr;
Greg Clayton8087ca22010-10-08 04:20:14 +0000471
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 for (entry_ptr = m_name_to_index.FindFirstValueForName (symbol_cstr);
473 entry_ptr!= NULL;
474 entry_ptr = m_name_to_index.FindNextValueForName (symbol_cstr, entry_ptr))
475 {
476 indexes.push_back (entry_ptr->value);
477 }
478 return indexes.size() - old_size;
479 }
480 return 0;
481}
482
483uint32_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000484Symtab::AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
485{
Greg Clayton8087ca22010-10-08 04:20:14 +0000486 Mutex::Locker locker (m_mutex);
487
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000488 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
489 if (symbol_name)
490 {
491 const size_t old_size = indexes.size();
Greg Clayton8087ca22010-10-08 04:20:14 +0000492 if (!m_name_indexes_computed)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000493 InitNameIndexes();
494
495 const char *symbol_cstr = symbol_name.GetCString();
496 const UniqueCStringMap<uint32_t>::Entry *entry_ptr;
497 for (entry_ptr = m_name_to_index.FindFirstValueForName (symbol_cstr);
498 entry_ptr!= NULL;
499 entry_ptr = m_name_to_index.FindNextValueForName (symbol_cstr, entry_ptr))
500 {
501 if (CheckSymbolAtIndex(entry_ptr->value, symbol_debug_type, symbol_visibility))
502 indexes.push_back (entry_ptr->value);
503 }
504 return indexes.size() - old_size;
505 }
506 return 0;
507}
508
509uint32_t
510Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, std::vector<uint32_t>& indexes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000511{
Greg Clayton8087ca22010-10-08 04:20:14 +0000512 Mutex::Locker locker (m_mutex);
513
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000514 if (AppendSymbolIndexesWithName(symbol_name, indexes) > 0)
515 {
516 std::vector<uint32_t>::iterator pos = indexes.begin();
517 while (pos != indexes.end())
518 {
519 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
520 ++pos;
521 else
522 indexes.erase(pos);
523 }
524 }
525 return indexes.size();
526}
527
528uint32_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000529Symtab::AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
530{
Greg Clayton8087ca22010-10-08 04:20:14 +0000531 Mutex::Locker locker (m_mutex);
532
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000533 if (AppendSymbolIndexesWithName(symbol_name, symbol_debug_type, symbol_visibility, indexes) > 0)
534 {
535 std::vector<uint32_t>::iterator pos = indexes.begin();
536 while (pos != indexes.end())
537 {
538 if (symbol_type == eSymbolTypeAny || m_symbols[*pos].GetType() == symbol_type)
539 ++pos;
540 else
541 indexes.erase(pos);
542 }
543 }
544 return indexes.size();
545}
546
547
548uint32_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regexp, SymbolType symbol_type, std::vector<uint32_t>& indexes)
550{
Greg Clayton8087ca22010-10-08 04:20:14 +0000551 Mutex::Locker locker (m_mutex);
552
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000553 uint32_t prev_size = indexes.size();
554 uint32_t sym_end = m_symbols.size();
555
556 for (int i = 0; i < sym_end; i++)
557 {
558 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
559 {
560 const char *name = m_symbols[i].GetMangled().GetName().AsCString();
561 if (name)
562 {
563 if (regexp.Execute (name))
564 indexes.push_back(i);
565 }
566 }
567 }
568 return indexes.size() - prev_size;
569
570}
571
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000572uint32_t
573Symtab::AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regexp, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000574{
Greg Clayton8087ca22010-10-08 04:20:14 +0000575 Mutex::Locker locker (m_mutex);
576
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000577 uint32_t prev_size = indexes.size();
578 uint32_t sym_end = m_symbols.size();
579
580 for (int i = 0; i < sym_end; i++)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000582 if (symbol_type == eSymbolTypeAny || m_symbols[i].GetType() == symbol_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000583 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000584 if (CheckSymbolAtIndex(i, symbol_debug_type, symbol_visibility) == false)
585 continue;
586
587 const char *name = m_symbols[i].GetMangled().GetName().AsCString();
588 if (name)
589 {
590 if (regexp.Execute (name))
591 indexes.push_back(i);
592 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593 }
594 }
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000595 return indexes.size() - prev_size;
596
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000599Symbol *
600Symtab::FindSymbolWithType (SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t& start_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000601{
Greg Clayton8087ca22010-10-08 04:20:14 +0000602 Mutex::Locker locker (m_mutex);
603
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 const size_t count = m_symbols.size();
605 for (uint32_t idx = start_idx; idx < count; ++idx)
606 {
607 if (symbol_type == eSymbolTypeAny || m_symbols[idx].GetType() == symbol_type)
608 {
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000609 if (CheckSymbolAtIndex(idx, symbol_debug_type, symbol_visibility))
610 {
611 start_idx = idx;
612 return &m_symbols[idx];
613 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614 }
615 }
616 return NULL;
617}
618
619size_t
620Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes)
621{
Greg Clayton8087ca22010-10-08 04:20:14 +0000622 Mutex::Locker locker (m_mutex);
623
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000624 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
625 // Initialize all of the lookup by name indexes before converting NAME
626 // to a uniqued string NAME_STR below.
Greg Clayton8087ca22010-10-08 04:20:14 +0000627 if (!m_name_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000628 InitNameIndexes();
629
630 if (name)
631 {
632 // The string table did have a string that matched, but we need
633 // to check the symbols and match the symbol_type if any was given.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000634 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 }
636 return symbol_indexes.size();
637}
638
639size_t
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000640Symtab::FindAllSymbolsWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000641{
Greg Clayton8087ca22010-10-08 04:20:14 +0000642 Mutex::Locker locker (m_mutex);
643
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000644 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
645 // Initialize all of the lookup by name indexes before converting NAME
646 // to a uniqued string NAME_STR below.
Greg Clayton8087ca22010-10-08 04:20:14 +0000647 if (!m_name_indexes_computed)
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000648 InitNameIndexes();
649
650 if (name)
651 {
652 // The string table did have a string that matched, but we need
653 // to check the symbols and match the symbol_type if any was given.
654 AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
655 }
656 return symbol_indexes.size();
657}
658
659size_t
660Symtab::FindAllSymbolsMatchingRexExAndType (const RegularExpression &regex, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes)
661{
Greg Clayton8087ca22010-10-08 04:20:14 +0000662 Mutex::Locker locker (m_mutex);
663
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000664 AppendSymbolIndexesMatchingRegExAndType(regex, symbol_type, symbol_debug_type, symbol_visibility, symbol_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000665 return symbol_indexes.size();
666}
667
668Symbol *
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000669Symtab::FindFirstSymbolWithNameAndType (const ConstString &name, SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000670{
Greg Clayton8087ca22010-10-08 04:20:14 +0000671 Mutex::Locker locker (m_mutex);
672
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673 Timer scoped_timer (__PRETTY_FUNCTION__, "%s", __PRETTY_FUNCTION__);
Greg Clayton8087ca22010-10-08 04:20:14 +0000674 if (!m_name_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000675 InitNameIndexes();
676
677 if (name)
678 {
679 std::vector<uint32_t> matching_indexes;
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.
Greg Claytonbcf2cfb2010-09-11 03:13:28 +0000682 if (AppendSymbolIndexesWithNameAndType (name, symbol_type, symbol_debug_type, symbol_visibility, matching_indexes))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 {
684 std::vector<uint32_t>::const_iterator pos, end = matching_indexes.end();
685 for (pos = matching_indexes.begin(); pos != end; ++pos)
686 {
687 Symbol *symbol = SymbolAtIndex(*pos);
688
689 if (symbol->Compare(name, symbol_type))
690 return symbol;
691 }
692 }
693 }
694 return NULL;
695}
696
697typedef struct
698{
699 const Symtab *symtab;
700 const addr_t file_addr;
701 Symbol *match_symbol;
702 const uint32_t *match_index_ptr;
703 addr_t match_offset;
704} SymbolSearchInfo;
705
706static int
707SymbolWithFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
708{
709 const Symbol *curr_symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
710 if (curr_symbol == NULL)
711 return -1;
712
713 const addr_t info_file_addr = info->file_addr;
714
715 // lldb::Symbol::GetAddressRangePtr() will only return a non NULL address
716 // range if the symbol has a section!
717 const AddressRange *curr_range = curr_symbol->GetAddressRangePtr();
718 if (curr_range)
719 {
720 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress();
721 if (info_file_addr < curr_file_addr)
722 return -1;
723 if (info_file_addr > curr_file_addr)
724 return +1;
725 info->match_symbol = const_cast<Symbol *>(curr_symbol);
726 info->match_index_ptr = index_ptr;
727 return 0;
728 }
729
730 return -1;
731}
732
733static int
734SymbolWithClosestFileAddress (SymbolSearchInfo *info, const uint32_t *index_ptr)
735{
736 const Symbol *symbol = info->symtab->SymbolAtIndex (index_ptr[0]);
737 if (symbol == NULL)
738 return -1;
739
740 const addr_t info_file_addr = info->file_addr;
741 const AddressRange *curr_range = symbol->GetAddressRangePtr();
742 if (curr_range)
743 {
744 const addr_t curr_file_addr = curr_range->GetBaseAddress().GetFileAddress();
745 if (info_file_addr < curr_file_addr)
746 return -1;
747
748 // Since we are finding the closest symbol that is greater than or equal
749 // to 'info->file_addr' we set the symbol here. This will get set
750 // multiple times, but after the search is done it will contain the best
751 // symbol match
752 info->match_symbol = const_cast<Symbol *>(symbol);
753 info->match_index_ptr = index_ptr;
754 info->match_offset = info_file_addr - curr_file_addr;
755
756 if (info_file_addr > curr_file_addr)
757 return +1;
758 return 0;
759 }
760 return -1;
761}
762
763static SymbolSearchInfo
764FindIndexPtrForSymbolContainingAddress(Symtab* symtab, addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
765{
766 SymbolSearchInfo info = { symtab, file_addr, NULL, NULL, 0 };
Greg Claytone0d378b2011-03-24 21:19:54 +0000767 ::bsearch (&info,
768 indexes,
769 num_indexes,
770 sizeof(uint32_t),
771 (ComparisonFunction)SymbolWithClosestFileAddress);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000772 return info;
773}
774
775
776void
777Symtab::InitAddressIndexes()
778{
Greg Clayton8087ca22010-10-08 04:20:14 +0000779 // Protected function, no need to lock mutex...
780 if (!m_addr_indexes_computed && !m_symbols.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000781 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000782 m_addr_indexes_computed = true;
783#if 0
784 // The old was to add only code, trampoline or data symbols...
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785 AppendSymbolIndexesWithType (eSymbolTypeCode, m_addr_indexes);
786 AppendSymbolIndexesWithType (eSymbolTypeTrampoline, m_addr_indexes);
787 AppendSymbolIndexesWithType (eSymbolTypeData, m_addr_indexes);
Greg Clayton8087ca22010-10-08 04:20:14 +0000788#else
789 // The new way adds all symbols with valid addresses that are section
790 // offset.
791 const_iterator begin = m_symbols.begin();
792 const_iterator end = m_symbols.end();
793 for (const_iterator pos = m_symbols.begin(); pos != end; ++pos)
794 {
795 if (pos->GetAddressRangePtr())
796 m_addr_indexes.push_back (std::distance(begin, pos));
797 }
798#endif
799 SortSymbolIndexesByValue (m_addr_indexes, false);
800 m_addr_indexes.push_back (UINT32_MAX); // Terminator for bsearch since we might need to look at the next symbol
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000801 }
802}
803
804size_t
805Symtab::CalculateSymbolSize (Symbol *symbol)
806{
Greg Clayton8087ca22010-10-08 04:20:14 +0000807 Mutex::Locker locker (m_mutex);
808
Greg Clayton471b31c2010-07-20 22:52:08 +0000809 if (m_symbols.empty())
810 return 0;
811
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 // Make sure this symbol is from this symbol table...
Greg Clayton471b31c2010-07-20 22:52:08 +0000813 if (symbol < &m_symbols.front() || symbol > &m_symbols.back())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000814 return 0;
815
816 // See if this symbol already has a byte size?
817 size_t byte_size = symbol->GetByteSize();
818
819 if (byte_size)
820 {
821 // It does, just return it
822 return byte_size;
823 }
824
825 // Else if this is an address based symbol, figure out the delta between
826 // it and the next address based symbol
827 if (symbol->GetAddressRangePtr())
828 {
Greg Clayton8087ca22010-10-08 04:20:14 +0000829 if (!m_addr_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000830 InitAddressIndexes();
831 const size_t num_addr_indexes = m_addr_indexes.size();
Greg Clayton471b31c2010-07-20 22:52:08 +0000832 SymbolSearchInfo info = FindIndexPtrForSymbolContainingAddress(this, symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress(), &m_addr_indexes.front(), num_addr_indexes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 if (info.match_index_ptr != NULL)
834 {
835 const lldb::addr_t curr_file_addr = symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
836 // We can figure out the address range of all symbols except the
837 // last one by taking the delta between the current symbol and
838 // the next symbol
839
Greg Clayton471b31c2010-07-20 22:52:08 +0000840 for (uint32_t addr_index = info.match_index_ptr - &m_addr_indexes.front() + 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000841 addr_index < num_addr_indexes;
842 ++addr_index)
843 {
844 Symbol *next_symbol = SymbolAtIndex(m_addr_indexes[addr_index]);
845 if (next_symbol == NULL)
846 break;
847
848 assert (next_symbol->GetAddressRangePtr());
849 const lldb::addr_t next_file_addr = next_symbol->GetAddressRangePtr()->GetBaseAddress().GetFileAddress();
850 if (next_file_addr > curr_file_addr)
851 {
852 byte_size = next_file_addr - curr_file_addr;
853 symbol->GetAddressRangePtr()->SetByteSize(byte_size);
854 symbol->SetSizeIsSynthesized(true);
855 break;
856 }
857 }
858 }
859 }
860 return byte_size;
861}
862
863Symbol *
864Symtab::FindSymbolWithFileAddress (addr_t file_addr)
865{
Greg Clayton8087ca22010-10-08 04:20:14 +0000866 Mutex::Locker locker (m_mutex);
867
868 if (!m_addr_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000869 InitAddressIndexes();
870
871 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 };
872
Greg Claytone0d378b2011-03-24 21:19:54 +0000873 uint32_t* match = (uint32_t*)::bsearch (&info,
874 &m_addr_indexes[0],
875 m_addr_indexes.size(),
876 sizeof(uint32_t),
877 (ComparisonFunction)SymbolWithFileAddress);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000878 if (match)
879 return SymbolAtIndex (*match);
880 return NULL;
881}
882
883
884Symbol *
885Symtab::FindSymbolContainingFileAddress (addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes)
886{
Greg Clayton8087ca22010-10-08 04:20:14 +0000887 Mutex::Locker locker (m_mutex);
888
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 SymbolSearchInfo info = { this, file_addr, NULL, NULL, 0 };
890
Greg Claytone0d378b2011-03-24 21:19:54 +0000891 ::bsearch (&info,
892 indexes,
893 num_indexes,
894 sizeof(uint32_t),
895 (ComparisonFunction)SymbolWithClosestFileAddress);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000896
897 if (info.match_symbol)
898 {
Greg Clayton89411422010-10-08 00:21:05 +0000899 if (info.match_offset == 0)
900 {
901 // We found an exact match!
902 return info.match_symbol;
903 }
904
905 const size_t symbol_byte_size = CalculateSymbolSize(info.match_symbol);
906
907 if (symbol_byte_size == 0)
908 {
909 // We weren't able to find the size of the symbol so lets just go
910 // with that match we found in our search...
911 return info.match_symbol;
912 }
913
914 // We were able to figure out a symbol size so lets make sure our
915 // offset puts "file_addr" in the symbol's address range.
916 if (info.match_offset < symbol_byte_size)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000917 return info.match_symbol;
918 }
919 return NULL;
920}
921
922Symbol *
923Symtab::FindSymbolContainingFileAddress (addr_t file_addr)
924{
Greg Clayton8087ca22010-10-08 04:20:14 +0000925 Mutex::Locker locker (m_mutex);
926
927 if (!m_addr_indexes_computed)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000928 InitAddressIndexes();
929
930 return FindSymbolContainingFileAddress (file_addr, &m_addr_indexes[0], m_addr_indexes.size());
931}
932