Symtab.h revision f58438fa7751274b6f4e4b1805940127dce13b00
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
11#ifndef liblldb_Symtab_h_
12#define liblldb_Symtab_h_
13
14#include <vector>
15
16#include "lldb/lldb-private.h"
17#include "lldb/Core/UniqueCStringMap.h"
18#include "lldb/Host/Mutex.h"
19#include "lldb/Symbol/Symbol.h"
20
21namespace lldb_private {
22
23class Symtab
24{
25public:
26    typedef std::vector<uint32_t> IndexCollection;
27    typedef UniqueCStringMap<uint32_t> NameToIndexMap;
28
29    typedef enum Debug {
30        eDebugNo,   // Not a debug symbol
31        eDebugYes,  // A debug symbol
32        eDebugAny
33    } Debug;
34
35    typedef enum Visibility {
36        eVisibilityAny,
37        eVisibilityExtern,
38        eVisibilityPrivate
39    } Visibility;
40
41                        Symtab(ObjectFile *objfile);
42                        ~Symtab();
43
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        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            Mutex &     GetMutex ()
52                        {
53                            return m_mutex;
54                        }
55            Symbol *    FindSymbolByID (lldb::user_id_t uid) const;
56            Symbol *    SymbolAtIndex (size_t idx);
57    const   Symbol *    SymbolAtIndex (size_t idx) const;
58            Symbol *    FindSymbolWithType (lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, uint32_t &start_idx);
59            uint32_t    AppendSymbolIndexesWithType (lldb::SymbolType symbol_type, std::vector<uint32_t>& indexes, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
60            uint32_t    AppendSymbolIndexesWithTypeAndFlagsValue (lldb::SymbolType symbol_type, uint32_t flags_value, std::vector<uint32_t>& indexes, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
61            uint32_t    AppendSymbolIndexesWithType (lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches, uint32_t start_idx = 0, uint32_t end_index = UINT32_MAX) const;
62            uint32_t    AppendSymbolIndexesWithName (const ConstString& symbol_name, std::vector<uint32_t>& matches);
63            uint32_t    AppendSymbolIndexesWithName (const ConstString& symbol_name, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches);
64            uint32_t    AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, lldb::SymbolType symbol_type, std::vector<uint32_t>& matches);
65            uint32_t    AppendSymbolIndexesWithNameAndType (const ConstString& symbol_name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& matches);
66            uint32_t    AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, std::vector<uint32_t>& indexes);
67            uint32_t    AppendSymbolIndexesMatchingRegExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& indexes);
68            size_t      FindAllSymbolsWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, std::vector<uint32_t>& symbol_indexes);
69            size_t      FindAllSymbolsWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes);
70            size_t      FindAllSymbolsMatchingRexExAndType (const RegularExpression &regex, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility, std::vector<uint32_t>& symbol_indexes);
71            Symbol *    FindFirstSymbolWithNameAndType (const ConstString &name, lldb::SymbolType symbol_type, Debug symbol_debug_type, Visibility symbol_visibility);
72            Symbol *    FindSymbolContainingFileAddress (lldb::addr_t file_addr, const uint32_t* indexes, uint32_t num_indexes);
73            Symbol *    FindSymbolContainingFileAddress (lldb::addr_t file_addr);
74            size_t      FindFunctionSymbols (const ConstString &name, uint32_t name_type_mask, SymbolContextList& sc_list);
75            size_t      CalculateSymbolSize (Symbol *symbol);
76
77            void        SortSymbolIndexesByValue (std::vector<uint32_t>& indexes, bool remove_duplicates) const;
78
79    static  void        DumpSymbolHeader (Stream *s);
80
81
82            void        Finalize ()
83                        {
84                            // Shrink to fit the symbols so we don't waste memory
85                            if (m_symbols.capacity() > m_symbols.size())
86                            {
87                                collection new_symbols (m_symbols.begin(), m_symbols.end());
88                                m_symbols.swap (new_symbols);
89                            }
90                        }
91
92            void        AppendSymbolNamesToMap (const IndexCollection &indexes,
93                                                bool add_demangled,
94                                                bool add_mangled,
95                                                NameToIndexMap &name_to_index_map) const;
96
97protected:
98    typedef std::vector<Symbol>         collection;
99    typedef collection::iterator        iterator;
100    typedef collection::const_iterator  const_iterator;
101
102            void        InitNameIndexes ();
103            void        InitAddressIndexes ();
104
105    ObjectFile *        m_objfile;
106    collection          m_symbols;
107    std::vector<uint32_t> m_addr_indexes;
108    UniqueCStringMap<uint32_t> m_name_to_index;
109    UniqueCStringMap<uint32_t> m_basename_to_index;
110    UniqueCStringMap<uint32_t> m_method_to_index;
111    UniqueCStringMap<uint32_t> m_selector_to_index;
112    mutable Mutex       m_mutex; // Provide thread safety for this symbol table
113    bool                m_addr_indexes_computed:1,
114                        m_name_indexes_computed:1;
115private:
116
117    bool
118    CheckSymbolAtIndex (size_t idx, Debug symbol_debug_type, Visibility symbol_visibility) const
119    {
120        switch (symbol_debug_type)
121        {
122        case eDebugNo:
123            if (m_symbols[idx].IsDebug() == true)
124                return false;
125            break;
126
127        case eDebugYes:
128            if (m_symbols[idx].IsDebug() == false)
129                return false;
130            break;
131
132        case eDebugAny:
133            break;
134        }
135
136        switch (symbol_visibility)
137        {
138        case eVisibilityAny:
139            return true;
140
141        case eVisibilityExtern:
142            return m_symbols[idx].IsExternal();
143
144        case eVisibilityPrivate:
145            return !m_symbols[idx].IsExternal();
146        }
147        return false;
148    }
149
150    void
151    SymbolIndicesToSymbolContextList (std::vector<uint32_t> &symbol_indexes,
152                                      SymbolContextList &sc_list);
153
154    DISALLOW_COPY_AND_ASSIGN (Symtab);
155};
156
157} // namespace lldb_private
158
159#endif  // liblldb_Symtab_h_
160