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