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