Symtab.h revision 321369
1//===-- Symtab.h ------------------------------------------------*- 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#ifndef liblldb_Symtab_h_
11#define liblldb_Symtab_h_
12
13#include <mutex>
14#include <vector>
15
16#include "lldb/Core/RangeMap.h"
17#include "lldb/Core/UniqueCStringMap.h"
18#include "lldb/Symbol/Symbol.h"
19#include "lldb/lldb-private.h"
20
21namespace lldb_private {
22
23class Symtab {
24public:
25  typedef std::vector<uint32_t> IndexCollection;
26  typedef UniqueCStringMap<uint32_t> NameToIndexMap;
27
28  typedef enum Debug {
29    eDebugNo,  // Not a debug symbol
30    eDebugYes, // A debug symbol
31    eDebugAny
32  } Debug;
33
34  typedef enum Visibility {
35    eVisibilityAny,
36    eVisibilityExtern,
37    eVisibilityPrivate
38  } Visibility;
39
40  Symtab(ObjectFile *objfile);
41  ~Symtab();
42
43  void PreloadSymbols();
44  void Reserve(size_t count);
45  Symbol *Resize(size_t count);
46  uint32_t AddSymbol(const Symbol &symbol);
47  size_t GetNumSymbols() const;
48  void SectionFileAddressesChanged();
49  void Dump(Stream *s, Target *target, SortOrder sort_type);
50  void Dump(Stream *s, Target *target, std::vector<uint32_t> &indexes) const;
51  uint32_t GetIndexForSymbol(const Symbol *symbol) const;
52  std::recursive_mutex &GetMutex() { return m_mutex; }
53  Symbol *FindSymbolByID(lldb::user_id_t uid) const;
54  Symbol *SymbolAtIndex(size_t idx);
55  const Symbol *SymbolAtIndex(size_t idx) const;
56  Symbol *FindSymbolWithType(lldb::SymbolType symbol_type,
57                             Debug symbol_debug_type,
58                             Visibility symbol_visibility, uint32_t &start_idx);
59  //----------------------------------------------------------------------
60  /// Get the parent symbol for the given symbol.
61  ///
62  /// Many symbols in symbol tables are scoped by other symbols that
63  /// contain one or more symbol. This function will look for such a
64  /// containing symbol and return it if there is one.
65  //----------------------------------------------------------------------
66  const Symbol *GetParent(Symbol *symbol) const;
67  uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type,
68                                       std::vector<uint32_t> &indexes,
69                                       uint32_t start_idx = 0,
70                                       uint32_t end_index = UINT32_MAX) const;
71  uint32_t AppendSymbolIndexesWithTypeAndFlagsValue(
72      lldb::SymbolType symbol_type, uint32_t flags_value,
73      std::vector<uint32_t> &indexes, uint32_t start_idx = 0,
74      uint32_t end_index = UINT32_MAX) const;
75  uint32_t AppendSymbolIndexesWithType(lldb::SymbolType symbol_type,
76                                       Debug symbol_debug_type,
77                                       Visibility symbol_visibility,
78                                       std::vector<uint32_t> &matches,
79                                       uint32_t start_idx = 0,
80                                       uint32_t end_index = UINT32_MAX) const;
81  uint32_t AppendSymbolIndexesWithName(const ConstString &symbol_name,
82                                       std::vector<uint32_t> &matches);
83  uint32_t AppendSymbolIndexesWithName(const ConstString &symbol_name,
84                                       Debug symbol_debug_type,
85                                       Visibility symbol_visibility,
86                                       std::vector<uint32_t> &matches);
87  uint32_t AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name,
88                                              lldb::SymbolType symbol_type,
89                                              std::vector<uint32_t> &matches);
90  uint32_t AppendSymbolIndexesWithNameAndType(const ConstString &symbol_name,
91                                              lldb::SymbolType symbol_type,
92                                              Debug symbol_debug_type,
93                                              Visibility symbol_visibility,
94                                              std::vector<uint32_t> &matches);
95  uint32_t
96  AppendSymbolIndexesMatchingRegExAndType(const RegularExpression &regex,
97                                          lldb::SymbolType symbol_type,
98                                          std::vector<uint32_t> &indexes);
99  uint32_t AppendSymbolIndexesMatchingRegExAndType(
100      const RegularExpression &regex, lldb::SymbolType symbol_type,
101      Debug symbol_debug_type, Visibility symbol_visibility,
102      std::vector<uint32_t> &indexes);
103  size_t FindAllSymbolsWithNameAndType(const ConstString &name,
104                                       lldb::SymbolType symbol_type,
105                                       std::vector<uint32_t> &symbol_indexes);
106  size_t FindAllSymbolsWithNameAndType(const ConstString &name,
107                                       lldb::SymbolType symbol_type,
108                                       Debug symbol_debug_type,
109                                       Visibility symbol_visibility,
110                                       std::vector<uint32_t> &symbol_indexes);
111  size_t FindAllSymbolsMatchingRexExAndType(
112      const RegularExpression &regex, lldb::SymbolType symbol_type,
113      Debug symbol_debug_type, Visibility symbol_visibility,
114      std::vector<uint32_t> &symbol_indexes);
115  Symbol *FindFirstSymbolWithNameAndType(const ConstString &name,
116                                         lldb::SymbolType symbol_type,
117                                         Debug symbol_debug_type,
118                                         Visibility symbol_visibility);
119  Symbol *FindSymbolAtFileAddress(lldb::addr_t file_addr);
120  Symbol *FindSymbolContainingFileAddress(lldb::addr_t file_addr);
121  void ForEachSymbolContainingFileAddress(
122      lldb::addr_t file_addr, std::function<bool(Symbol *)> const &callback);
123  size_t FindFunctionSymbols(const ConstString &name, uint32_t name_type_mask,
124                             SymbolContextList &sc_list);
125  void CalculateSymbolSizes();
126
127  void SortSymbolIndexesByValue(std::vector<uint32_t> &indexes,
128                                bool remove_duplicates) const;
129
130  static void DumpSymbolHeader(Stream *s);
131
132  void Finalize() {
133    // Shrink to fit the symbols so we don't waste memory
134    if (m_symbols.capacity() > m_symbols.size()) {
135      collection new_symbols(m_symbols.begin(), m_symbols.end());
136      m_symbols.swap(new_symbols);
137    }
138  }
139
140  void AppendSymbolNamesToMap(const IndexCollection &indexes,
141                              bool add_demangled, bool add_mangled,
142                              NameToIndexMap &name_to_index_map) const;
143
144  ObjectFile *GetObjectFile() { return m_objfile; }
145
146protected:
147  typedef std::vector<Symbol> collection;
148  typedef collection::iterator iterator;
149  typedef collection::const_iterator const_iterator;
150  typedef RangeDataVector<lldb::addr_t, lldb::addr_t, uint32_t>
151      FileRangeToIndexMap;
152  void InitNameIndexes();
153  void InitAddressIndexes();
154
155  ObjectFile *m_objfile;
156  collection m_symbols;
157  FileRangeToIndexMap m_file_addr_to_index;
158  UniqueCStringMap<uint32_t> m_name_to_index;
159  UniqueCStringMap<uint32_t> m_basename_to_index;
160  UniqueCStringMap<uint32_t> m_method_to_index;
161  UniqueCStringMap<uint32_t> m_selector_to_index;
162  mutable std::recursive_mutex
163      m_mutex; // Provide thread safety for this symbol table
164  bool m_file_addr_to_index_computed : 1, m_name_indexes_computed : 1;
165
166private:
167  bool CheckSymbolAtIndex(size_t idx, Debug symbol_debug_type,
168                          Visibility symbol_visibility) const {
169    switch (symbol_debug_type) {
170    case eDebugNo:
171      if (m_symbols[idx].IsDebug() == true)
172        return false;
173      break;
174
175    case eDebugYes:
176      if (m_symbols[idx].IsDebug() == false)
177        return false;
178      break;
179
180    case eDebugAny:
181      break;
182    }
183
184    switch (symbol_visibility) {
185    case eVisibilityAny:
186      return true;
187
188    case eVisibilityExtern:
189      return m_symbols[idx].IsExternal();
190
191    case eVisibilityPrivate:
192      return !m_symbols[idx].IsExternal();
193    }
194    return false;
195  }
196
197  void SymbolIndicesToSymbolContextList(std::vector<uint32_t> &symbol_indexes,
198                                        SymbolContextList &sc_list);
199
200  DISALLOW_COPY_AND_ASSIGN(Symtab);
201};
202
203} // namespace lldb_private
204
205#endif // liblldb_Symtab_h_
206