Introduce and use dwarf_scanner_base
[binutils-gdb.git] / gdb / dwarf2 / cooked-index.h
1 /* DIE indexing
2
3 Copyright (C) 2022 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #ifndef GDB_DWARF2_COOKED_INDEX_H
21 #define GDB_DWARF2_COOKED_INDEX_H
22
23 #include "dwarf2.h"
24 #include "gdbtypes.h"
25 #include "symtab.h"
26 #include "hashtab.h"
27 #include "dwarf2/index-common.h"
28 #include "gdbsupport/gdb_string_view.h"
29 #include "quick-symbol.h"
30 #include "gdbsupport/gdb_obstack.h"
31 #include "addrmap.h"
32 #include "gdbsupport/iterator-range.h"
33 #include "gdbsupport/thread-pool.h"
34 #include "dwarf2/mapped-index.h"
35
36 struct dwarf2_per_cu_data;
37
38 /* Flags that describe an entry in the index. */
39 enum cooked_index_flag_enum : unsigned char
40 {
41 /* True if this entry is the program's "main". */
42 IS_MAIN = 1,
43 /* True if this entry represents a "static" object. */
44 IS_STATIC = 2,
45 /* True if this entry is an "enum class". */
46 IS_ENUM_CLASS = 4,
47 /* True if this entry uses the linkage name. */
48 IS_LINKAGE = 8,
49 };
50 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag);
51
52 /* A cooked_index_entry represents a single item in the index. Note
53 that two entries can be created for the same DIE -- one using the
54 name, and another one using the linkage name, if any.
55
56 This is an "open" class and the members are all directly
57 accessible. It is read-only after the index has been fully read
58 and processed. */
59 struct cooked_index_entry : public allocate_on_obstack
60 {
61 cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_,
62 cooked_index_flag flags_, const char *name_,
63 const cooked_index_entry *parent_entry_,
64 dwarf2_per_cu_data *per_cu_)
65 : name (name_),
66 tag (tag_),
67 flags (flags_),
68 die_offset (die_offset_),
69 parent_entry (parent_entry_),
70 per_cu (per_cu_)
71 {
72 }
73
74 /* Return true if this entry matches SEARCH_FLAGS. */
75 bool matches (block_search_flags search_flags) const
76 {
77 if ((search_flags & SEARCH_STATIC_BLOCK) != 0
78 && (flags & IS_STATIC) != 0)
79 return true;
80 if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0
81 && (flags & IS_STATIC) == 0)
82 return true;
83 return false;
84 }
85
86 /* Return true if this entry matches DOMAIN. */
87 bool matches (domain_enum domain) const
88 {
89 switch (domain)
90 {
91 case LABEL_DOMAIN:
92 return false;
93
94 case MODULE_DOMAIN:
95 return tag == DW_TAG_module;
96
97 case COMMON_BLOCK_DOMAIN:
98 return tag == DW_TAG_common_block;
99 }
100
101 return true;
102 }
103
104 /* Return true if this entry matches KIND. */
105 bool matches (enum search_domain kind) const
106 {
107 switch (kind)
108 {
109 case VARIABLES_DOMAIN:
110 return tag == DW_TAG_variable;
111 case FUNCTIONS_DOMAIN:
112 return tag == DW_TAG_subprogram;
113 case TYPES_DOMAIN:
114 return tag == DW_TAG_typedef || tag == DW_TAG_structure_type;
115 case MODULES_DOMAIN:
116 return tag == DW_TAG_module;
117 }
118
119 return true;
120 }
121
122 /* Construct the fully-qualified name of this entry and return a
123 pointer to it. If allocation is needed, it will be done on
124 STORAGE. */
125 const char *full_name (struct obstack *storage) const;
126
127 /* Entries must be sorted case-insensitively; this compares two
128 entries. */
129 bool operator< (const cooked_index_entry &other) const
130 {
131 return strcasecmp (canonical, other.canonical) < 0;
132 }
133
134 /* The name as it appears in DWARF. This always points into one of
135 the mapped DWARF sections. Note that this may be the name or the
136 linkage name -- two entries are created for DIEs which have both
137 attributes. */
138 const char *name;
139 /* The canonical name. For C++ names, this may differ from NAME.
140 In all other cases, this is equal to NAME. */
141 const char *canonical = nullptr;
142 /* The DWARF tag. */
143 enum dwarf_tag tag;
144 /* Any flags attached to this entry. */
145 cooked_index_flag flags;
146 /* The offset of this DIE. */
147 sect_offset die_offset;
148 /* The parent entry. This is NULL for top-level entries.
149 Otherwise, it points to the parent entry, such as a namespace or
150 class. */
151 const cooked_index_entry *parent_entry;
152 /* The CU from which this entry originates. */
153 dwarf2_per_cu_data *per_cu;
154
155 private:
156
157 void write_scope (struct obstack *storage, const char *sep) const;
158 };
159
160 class cooked_index_vector;
161
162 /* An index of interesting DIEs. This is "cooked", in contrast to a
163 mapped .debug_names or .gdb_index, which are "raw". An entry in
164 the index is of type cooked_index_entry.
165
166 Operations on the index are described below. They are chosen to
167 make it relatively simple to implement the symtab "quick"
168 methods. */
169 class cooked_index
170 {
171 public:
172 cooked_index () = default;
173 DISABLE_COPY_AND_ASSIGN (cooked_index);
174
175 /* Create a new cooked_index_entry and register it with this object.
176 Entries are owned by this object. The new item is returned. */
177 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
178 cooked_index_flag flags,
179 const char *name,
180 const cooked_index_entry *parent_entry,
181 dwarf2_per_cu_data *per_cu);
182
183 /* Install a new fixed addrmap from the given mutable addrmap. */
184 void install_addrmap (addrmap *map)
185 {
186 gdb_assert (m_addrmap == nullptr);
187 m_addrmap = addrmap_create_fixed (map, &m_storage);
188 }
189
190 friend class cooked_index_vector;
191
192 private:
193
194 /* Return the entry that is believed to represent the program's
195 "main". This will return NULL if no such entry is available. */
196 const cooked_index_entry *get_main () const
197 {
198 return m_main;
199 }
200
201 /* Look up ADDR in the address map, and return either the
202 corresponding CU, or nullptr if the address could not be
203 found. */
204 dwarf2_per_cu_data *lookup (CORE_ADDR addr)
205 {
206 return (dwarf2_per_cu_data *) addrmap_find (m_addrmap, addr);
207 }
208
209 /* Create a new cooked_index_entry and register it with this object.
210 Entries are owned by this object. The new item is returned. */
211 cooked_index_entry *create (sect_offset die_offset,
212 enum dwarf_tag tag,
213 cooked_index_flag flags,
214 const char *name,
215 const cooked_index_entry *parent_entry,
216 dwarf2_per_cu_data *per_cu)
217 {
218 return new (&m_storage) cooked_index_entry (die_offset, tag, flags,
219 name, parent_entry,
220 per_cu);
221 }
222
223 /* Storage for the entries. */
224 auto_obstack m_storage;
225 /* List of all entries. */
226 std::vector<cooked_index_entry *> m_entries;
227 /* If we found "main" or an entry with 'is_main' set, store it
228 here. */
229 cooked_index_entry *m_main = nullptr;
230 /* When constructing the index, entries are stored on a linked list.
231 This member points to the head of that list. Later, they are
232 entered into the hash table, at which point this is no longer
233 used. */
234 cooked_index_entry *m_start = nullptr;
235 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
236 objects. */
237 addrmap *m_addrmap = nullptr;
238 };
239
240 /* The main index of DIEs. The parallel DIE indexers create
241 cooked_index objects. Then, these are all handled to a
242 cooked_index_vector for storage and final indexing. The index is
243 made by iterating over the entries previously created. */
244
245 class cooked_index_vector : public dwarf_scanner_base
246 {
247 public:
248
249 /* A convenience typedef for the vector that is contained in this
250 object. */
251 typedef std::vector<std::unique_ptr<cooked_index>> vec_type;
252
253 explicit cooked_index_vector (vec_type &&vec);
254 DISABLE_COPY_AND_ASSIGN (cooked_index_vector);
255
256 ~cooked_index_vector ()
257 {
258 /* The 'finalize' method may be run in a different thread. If
259 this object is destroyed before this completes, then the method
260 will end up writing to freed memory. Waiting for this to
261 complete avoids this problem; and the cost seems ignorable
262 because creating and immediately destroying the debug info is a
263 relatively rare thing to do. */
264 m_future.wait ();
265 }
266
267 /* A simple range over part of m_entries. */
268 typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
269
270 /* Look up an entry by name. Returns a range of all matching
271 results. If COMPLETING is true, then a larger range, suitable
272 for completion, will be returned. */
273 range find (gdb::string_view name, bool completing);
274
275 /* Return a range of all the entries. */
276 range all_entries ()
277 {
278 m_future.wait ();
279 return { m_entries.begin (), m_entries.end () };
280 }
281
282 /* Look up ADDR in the address map, and return either the
283 corresponding CU, or nullptr if the address could not be
284 found. */
285 dwarf2_per_cu_data *lookup (CORE_ADDR addr);
286
287 /* Return a new vector of all the addrmaps used by all the indexes
288 held by this object. */
289 std::vector<addrmap *> get_addrmaps ();
290
291 /* Return the entry that is believed to represent the program's
292 "main". This will return NULL if no such entry is available. */
293 const cooked_index_entry *get_main () const;
294
295 quick_symbol_functions_up make_quick_functions () const override;
296
297 private:
298
299 /* GNAT only emits mangled ("encoded") names in the DWARF, and does
300 not emit the module structure. However, we need this structure
301 to do lookups. This function recreates that structure for an
302 existing entry. It returns the base name (last element) of the
303 full decoded name. */
304 gdb::unique_xmalloc_ptr<char> handle_gnat_encoded_entry
305 (cooked_index_entry *entry, htab_t gnat_entries);
306
307 /* Finalize the index. This should be called a single time, when
308 the index has been fully populated. It enters all the entries
309 into the internal hash table. */
310 void finalize ();
311
312 /* The vector of cooked_index objects. This is stored because the
313 entries are stored on the obstacks in those objects. */
314 vec_type m_vector;
315
316 /* List of all entries. This is sorted during finalization. */
317 std::vector<cooked_index_entry *> m_entries;
318
319 /* Storage for canonical names. */
320 std::vector<gdb::unique_xmalloc_ptr<char>> m_names;
321
322 /* A future that tracks when the 'finalize' method is done. Note
323 that the 'get' method is never called on this future, only
324 'wait'. */
325 std::future<void> m_future;
326 };
327
328 #endif /* GDB_DWARF2_COOKED_INDEX_H */