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