cooked-index.h revision 1.1.1.2 1 1.1 christos /* DIE indexing
2 1.1 christos
3 1.1.1.2 christos Copyright (C) 2022-2024 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #ifndef GDB_DWARF2_COOKED_INDEX_H
21 1.1 christos #define GDB_DWARF2_COOKED_INDEX_H
22 1.1 christos
23 1.1 christos #include "dwarf2.h"
24 1.1.1.2 christos #include "dwarf2/types.h"
25 1.1 christos #include "symtab.h"
26 1.1 christos #include "hashtab.h"
27 1.1 christos #include "dwarf2/index-common.h"
28 1.1.1.2 christos #include <string_view>
29 1.1 christos #include "quick-symbol.h"
30 1.1 christos #include "gdbsupport/gdb_obstack.h"
31 1.1 christos #include "addrmap.h"
32 1.1 christos #include "gdbsupport/iterator-range.h"
33 1.1 christos #include "gdbsupport/thread-pool.h"
34 1.1 christos #include "dwarf2/mapped-index.h"
35 1.1.1.2 christos #include "dwarf2/read.h"
36 1.1 christos #include "dwarf2/tag.h"
37 1.1.1.2 christos #include "dwarf2/abbrev-cache.h"
38 1.1.1.2 christos #include "dwarf2/parent-map.h"
39 1.1 christos #include "gdbsupport/range-chain.h"
40 1.1.1.2 christos #include "gdbsupport/task-group.h"
41 1.1.1.2 christos #include "complaints.h"
42 1.1.1.2 christos #include "run-on-main-thread.h"
43 1.1.1.2 christos
44 1.1.1.2 christos #if CXX_STD_THREAD
45 1.1.1.2 christos #include <mutex>
46 1.1.1.2 christos #include <condition_variable>
47 1.1.1.2 christos #endif /* CXX_STD_THREAD */
48 1.1 christos
49 1.1 christos struct dwarf2_per_cu_data;
50 1.1.1.2 christos struct dwarf2_per_bfd;
51 1.1.1.2 christos struct index_cache_store_context;
52 1.1.1.2 christos struct cooked_index_entry;
53 1.1 christos
54 1.1 christos /* Flags that describe an entry in the index. */
55 1.1 christos enum cooked_index_flag_enum : unsigned char
56 1.1 christos {
57 1.1 christos /* True if this entry is the program's "main". */
58 1.1 christos IS_MAIN = 1,
59 1.1 christos /* True if this entry represents a "static" object. */
60 1.1 christos IS_STATIC = 2,
61 1.1 christos /* True if this entry uses the linkage name. */
62 1.1.1.2 christos IS_LINKAGE = 4,
63 1.1 christos /* True if this entry is just for the declaration of a type, not the
64 1.1 christos definition. */
65 1.1.1.2 christos IS_TYPE_DECLARATION = 8,
66 1.1.1.2 christos /* True is parent_entry.deferred has a value rather than parent_entry
67 1.1.1.2 christos .resolved. */
68 1.1.1.2 christos IS_PARENT_DEFERRED = 16,
69 1.1 christos };
70 1.1 christos DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag);
71 1.1 christos
72 1.1.1.2 christos /* Type representing either a resolved or deferred cooked_index_entry. */
73 1.1.1.2 christos
74 1.1.1.2 christos union cooked_index_entry_ref
75 1.1.1.2 christos {
76 1.1.1.2 christos cooked_index_entry_ref (parent_map::addr_type deferred_)
77 1.1.1.2 christos {
78 1.1.1.2 christos deferred = deferred_;
79 1.1.1.2 christos }
80 1.1.1.2 christos
81 1.1.1.2 christos cooked_index_entry_ref (const cooked_index_entry *resolved_)
82 1.1.1.2 christos {
83 1.1.1.2 christos resolved = resolved_;
84 1.1.1.2 christos }
85 1.1.1.2 christos
86 1.1.1.2 christos const cooked_index_entry *resolved;
87 1.1.1.2 christos parent_map::addr_type deferred;
88 1.1.1.2 christos };
89 1.1.1.2 christos
90 1.1.1.2 christos /* Return a string representation of FLAGS. */
91 1.1.1.2 christos
92 1.1.1.2 christos std::string to_string (cooked_index_flag flags);
93 1.1.1.2 christos
94 1.1 christos /* Return true if LANG requires canonicalization. This is used
95 1.1 christos primarily to work around an issue computing the name of "main".
96 1.1 christos This function must be kept in sync with
97 1.1.1.2 christos cooked_index_shard::finalize. */
98 1.1 christos
99 1.1 christos extern bool language_requires_canonicalization (enum language lang);
100 1.1 christos
101 1.1 christos /* A cooked_index_entry represents a single item in the index. Note
102 1.1 christos that two entries can be created for the same DIE -- one using the
103 1.1 christos name, and another one using the linkage name, if any.
104 1.1 christos
105 1.1 christos This is an "open" class and the members are all directly
106 1.1 christos accessible. It is read-only after the index has been fully read
107 1.1 christos and processed. */
108 1.1.1.2 christos struct cooked_index_entry : public allocate_on_obstack<cooked_index_entry>
109 1.1 christos {
110 1.1 christos cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_,
111 1.1.1.2 christos cooked_index_flag flags_,
112 1.1.1.2 christos enum language lang_, const char *name_,
113 1.1.1.2 christos cooked_index_entry_ref parent_entry_,
114 1.1 christos dwarf2_per_cu_data *per_cu_)
115 1.1 christos : name (name_),
116 1.1 christos tag (tag_),
117 1.1 christos flags (flags_),
118 1.1.1.2 christos lang (lang_),
119 1.1 christos die_offset (die_offset_),
120 1.1.1.2 christos per_cu (per_cu_),
121 1.1.1.2 christos m_parent_entry (parent_entry_)
122 1.1 christos {
123 1.1 christos }
124 1.1 christos
125 1.1 christos /* Return true if this entry matches SEARCH_FLAGS. */
126 1.1 christos bool matches (block_search_flags search_flags) const
127 1.1 christos {
128 1.1 christos /* Just reject type declarations. */
129 1.1 christos if ((flags & IS_TYPE_DECLARATION) != 0)
130 1.1 christos return false;
131 1.1 christos
132 1.1 christos if ((search_flags & SEARCH_STATIC_BLOCK) != 0
133 1.1 christos && (flags & IS_STATIC) != 0)
134 1.1 christos return true;
135 1.1 christos if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0
136 1.1 christos && (flags & IS_STATIC) == 0)
137 1.1 christos return true;
138 1.1 christos return false;
139 1.1 christos }
140 1.1 christos
141 1.1 christos /* Return true if this entry matches KIND. */
142 1.1.1.2 christos bool matches (domain_search_flags kind) const;
143 1.1 christos
144 1.1 christos /* Construct the fully-qualified name of this entry and return a
145 1.1 christos pointer to it. If allocation is needed, it will be done on
146 1.1 christos STORAGE. FOR_MAIN is true if we are computing the name of the
147 1.1 christos "main" entry -- one marked DW_AT_main_subprogram. This matters
148 1.1 christos for avoiding name canonicalization and also a related race (if
149 1.1 christos "main" computation is done during finalization). */
150 1.1 christos const char *full_name (struct obstack *storage, bool for_main = false) const;
151 1.1 christos
152 1.1 christos /* Comparison modes for the 'compare' function. See the function
153 1.1 christos for a description. */
154 1.1 christos enum comparison_mode
155 1.1 christos {
156 1.1 christos MATCH,
157 1.1 christos SORT,
158 1.1 christos COMPLETE,
159 1.1 christos };
160 1.1 christos
161 1.1 christos /* Compare two strings, case-insensitively. Return -1 if STRA is
162 1.1 christos less than STRB, 0 if they are equal, and 1 if STRA is greater.
163 1.1 christos
164 1.1 christos When comparing, '<' is considered to be less than all other
165 1.1 christos printable characters. This ensures that "t<x>" sorts before
166 1.1 christos "t1", which is necessary when looking up "t". This '<' handling
167 1.1 christos is to ensure that certain C++ lookups work correctly. It is
168 1.1 christos inexact, and applied regardless of the search language, but this
169 1.1 christos is ok because callers of this code do more precise filtering
170 1.1 christos according to their needs. This is also why using a
171 1.1 christos case-insensitive comparison works even for languages that are
172 1.1 christos case sensitive.
173 1.1 christos
174 1.1 christos MODE controls how the comparison proceeds.
175 1.1 christos
176 1.1 christos MODE==SORT is used when sorting and the only special '<' handling
177 1.1 christos that it does is to ensure that '<' sorts before all other
178 1.1 christos printable characters. This ensures that the resulting ordering
179 1.1 christos will be binary-searchable.
180 1.1 christos
181 1.1 christos MODE==MATCH is used when searching for a symbol. In this case,
182 1.1 christos STRB must always be the search name, and STRA must be the name in
183 1.1 christos the index that is under consideration. In compare mode, early
184 1.1 christos termination of STRB may match STRA -- for example, "t<int>" and
185 1.1 christos "t" will be considered to be equal. (However, if A=="t" and
186 1.1 christos B=="t<int>", then this will not consider them as equal.)
187 1.1 christos
188 1.1 christos MODE==COMPLETE is used when searching for a symbol for
189 1.1 christos completion. In this case, STRB must always be the search name,
190 1.1 christos and STRA must be the name in the index that is under
191 1.1 christos consideration. In completion mode, early termination of STRB
192 1.1 christos always results in a match. */
193 1.1 christos static int compare (const char *stra, const char *strb,
194 1.1 christos comparison_mode mode);
195 1.1 christos
196 1.1 christos /* Compare two entries by canonical name. */
197 1.1 christos bool operator< (const cooked_index_entry &other) const
198 1.1 christos {
199 1.1 christos return compare (canonical, other.canonical, SORT) < 0;
200 1.1 christos }
201 1.1 christos
202 1.1.1.2 christos /* Set parent entry to PARENT. */
203 1.1.1.2 christos void set_parent (const cooked_index_entry *parent)
204 1.1.1.2 christos {
205 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) == 0);
206 1.1.1.2 christos m_parent_entry.resolved = parent;
207 1.1.1.2 christos }
208 1.1.1.2 christos
209 1.1.1.2 christos /* Resolve deferred parent entry to PARENT. */
210 1.1.1.2 christos void resolve_parent (const cooked_index_entry *parent)
211 1.1.1.2 christos {
212 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) != 0);
213 1.1.1.2 christos flags = flags & ~IS_PARENT_DEFERRED;
214 1.1.1.2 christos m_parent_entry.resolved = parent;
215 1.1.1.2 christos }
216 1.1.1.2 christos
217 1.1.1.2 christos /* Return parent entry. */
218 1.1.1.2 christos const cooked_index_entry *get_parent () const
219 1.1.1.2 christos {
220 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) == 0);
221 1.1.1.2 christos return m_parent_entry.resolved;
222 1.1.1.2 christos }
223 1.1.1.2 christos
224 1.1.1.2 christos /* Return deferred parent entry. */
225 1.1.1.2 christos parent_map::addr_type get_deferred_parent () const
226 1.1.1.2 christos {
227 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) != 0);
228 1.1.1.2 christos return m_parent_entry.deferred;
229 1.1.1.2 christos }
230 1.1.1.2 christos
231 1.1 christos /* The name as it appears in DWARF. This always points into one of
232 1.1 christos the mapped DWARF sections. Note that this may be the name or the
233 1.1 christos linkage name -- two entries are created for DIEs which have both
234 1.1 christos attributes. */
235 1.1 christos const char *name;
236 1.1 christos /* The canonical name. For C++ names, this may differ from NAME.
237 1.1 christos In all other cases, this is equal to NAME. */
238 1.1 christos const char *canonical = nullptr;
239 1.1 christos /* The DWARF tag. */
240 1.1 christos enum dwarf_tag tag;
241 1.1 christos /* Any flags attached to this entry. */
242 1.1 christos cooked_index_flag flags;
243 1.1.1.2 christos /* The language of this symbol. */
244 1.1.1.2 christos ENUM_BITFIELD (language) lang : LANGUAGE_BITS;
245 1.1 christos /* The offset of this DIE. */
246 1.1 christos sect_offset die_offset;
247 1.1 christos /* The CU from which this entry originates. */
248 1.1 christos dwarf2_per_cu_data *per_cu;
249 1.1 christos
250 1.1 christos private:
251 1.1 christos
252 1.1 christos /* A helper method for full_name. Emits the full scope of this
253 1.1 christos object, followed by the separator, to STORAGE. If this entry has
254 1.1 christos a parent, its write_scope method is called first. */
255 1.1 christos void write_scope (struct obstack *storage, const char *sep,
256 1.1 christos bool for_name) const;
257 1.1.1.2 christos
258 1.1.1.2 christos /* The parent entry. This is NULL for top-level entries.
259 1.1.1.2 christos Otherwise, it points to the parent entry, such as a namespace or
260 1.1.1.2 christos class. */
261 1.1.1.2 christos cooked_index_entry_ref m_parent_entry;
262 1.1 christos };
263 1.1 christos
264 1.1.1.2 christos class cooked_index;
265 1.1 christos
266 1.1 christos /* An index of interesting DIEs. This is "cooked", in contrast to a
267 1.1 christos mapped .debug_names or .gdb_index, which are "raw". An entry in
268 1.1 christos the index is of type cooked_index_entry.
269 1.1 christos
270 1.1 christos Operations on the index are described below. They are chosen to
271 1.1 christos make it relatively simple to implement the symtab "quick"
272 1.1 christos methods. */
273 1.1.1.2 christos class cooked_index_shard
274 1.1 christos {
275 1.1 christos public:
276 1.1.1.2 christos cooked_index_shard () = default;
277 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_shard);
278 1.1 christos
279 1.1 christos /* Create a new cooked_index_entry and register it with this object.
280 1.1 christos Entries are owned by this object. The new item is returned. */
281 1.1.1.2 christos cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
282 1.1.1.2 christos cooked_index_flag flags, enum language lang,
283 1.1.1.2 christos const char *name,
284 1.1.1.2 christos cooked_index_entry_ref parent_entry,
285 1.1.1.2 christos dwarf2_per_cu_data *per_cu);
286 1.1 christos
287 1.1 christos /* Install a new fixed addrmap from the given mutable addrmap. */
288 1.1 christos void install_addrmap (addrmap_mutable *map)
289 1.1 christos {
290 1.1 christos gdb_assert (m_addrmap == nullptr);
291 1.1 christos m_addrmap = new (&m_storage) addrmap_fixed (&m_storage, map);
292 1.1 christos }
293 1.1 christos
294 1.1.1.2 christos friend class cooked_index;
295 1.1 christos
296 1.1 christos /* A simple range over part of m_entries. */
297 1.1.1.2 christos typedef iterator_range<std::vector<cooked_index_entry *>::const_iterator>
298 1.1.1.2 christos range;
299 1.1 christos
300 1.1 christos /* Return a range of all the entries. */
301 1.1.1.2 christos range all_entries () const
302 1.1 christos {
303 1.1.1.2 christos return { m_entries.cbegin (), m_entries.cend () };
304 1.1 christos }
305 1.1 christos
306 1.1 christos /* Look up an entry by name. Returns a range of all matching
307 1.1 christos results. If COMPLETING is true, then a larger range, suitable
308 1.1 christos for completion, will be returned. */
309 1.1.1.2 christos range find (const std::string &name, bool completing) const;
310 1.1 christos
311 1.1 christos private:
312 1.1 christos
313 1.1 christos /* Return the entry that is believed to represent the program's
314 1.1 christos "main". This will return NULL if no such entry is available. */
315 1.1 christos const cooked_index_entry *get_main () const
316 1.1 christos {
317 1.1 christos return m_main;
318 1.1 christos }
319 1.1 christos
320 1.1 christos /* Look up ADDR in the address map, and return either the
321 1.1 christos corresponding CU, or nullptr if the address could not be
322 1.1 christos found. */
323 1.1.1.2 christos dwarf2_per_cu_data *lookup (unrelocated_addr addr)
324 1.1 christos {
325 1.1.1.2 christos return (static_cast<dwarf2_per_cu_data *>
326 1.1.1.2 christos (m_addrmap->find ((CORE_ADDR) addr)));
327 1.1 christos }
328 1.1 christos
329 1.1 christos /* Create a new cooked_index_entry and register it with this object.
330 1.1 christos Entries are owned by this object. The new item is returned. */
331 1.1 christos cooked_index_entry *create (sect_offset die_offset,
332 1.1 christos enum dwarf_tag tag,
333 1.1 christos cooked_index_flag flags,
334 1.1.1.2 christos enum language lang,
335 1.1 christos const char *name,
336 1.1.1.2 christos cooked_index_entry_ref parent_entry,
337 1.1 christos dwarf2_per_cu_data *per_cu)
338 1.1 christos {
339 1.1 christos return new (&m_storage) cooked_index_entry (die_offset, tag, flags,
340 1.1.1.2 christos lang, name, parent_entry,
341 1.1 christos per_cu);
342 1.1 christos }
343 1.1 christos
344 1.1 christos /* GNAT only emits mangled ("encoded") names in the DWARF, and does
345 1.1 christos not emit the module structure. However, we need this structure
346 1.1 christos to do lookups. This function recreates that structure for an
347 1.1 christos existing entry. It returns the base name (last element) of the
348 1.1 christos full decoded name. */
349 1.1 christos gdb::unique_xmalloc_ptr<char> handle_gnat_encoded_entry
350 1.1 christos (cooked_index_entry *entry, htab_t gnat_entries);
351 1.1 christos
352 1.1.1.2 christos /* Finalize the index. This should be called a single time, when
353 1.1.1.2 christos the index has been fully populated. It enters all the entries
354 1.1.1.2 christos into the internal table and fixes up all missing parent links.
355 1.1.1.2 christos This may be invoked in a worker thread. */
356 1.1.1.2 christos void finalize (const parent_map_map *parent_maps);
357 1.1 christos
358 1.1 christos /* Storage for the entries. */
359 1.1 christos auto_obstack m_storage;
360 1.1 christos /* List of all entries. */
361 1.1 christos std::vector<cooked_index_entry *> m_entries;
362 1.1 christos /* If we found an entry with 'is_main' set, store it here. */
363 1.1 christos cooked_index_entry *m_main = nullptr;
364 1.1 christos /* The addrmap. This maps address ranges to dwarf2_per_cu_data
365 1.1 christos objects. */
366 1.1.1.2 christos addrmap_fixed *m_addrmap = nullptr;
367 1.1 christos /* Storage for canonical names. */
368 1.1 christos std::vector<gdb::unique_xmalloc_ptr<char>> m_names;
369 1.1 christos };
370 1.1 christos
371 1.1.1.2 christos class cutu_reader;
372 1.1.1.2 christos
373 1.1.1.2 christos /* An instance of this is created when scanning DWARF to create a
374 1.1.1.2 christos cooked index. */
375 1.1 christos
376 1.1.1.2 christos class cooked_index_storage
377 1.1 christos {
378 1.1 christos public:
379 1.1 christos
380 1.1.1.2 christos cooked_index_storage ();
381 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_storage);
382 1.1.1.2 christos
383 1.1.1.2 christos /* Return the current abbrev cache. */
384 1.1.1.2 christos abbrev_cache *get_abbrev_cache ()
385 1.1.1.2 christos {
386 1.1.1.2 christos return &m_abbrev_cache;
387 1.1.1.2 christos }
388 1.1.1.2 christos
389 1.1.1.2 christos /* Return the DIE reader corresponding to PER_CU. If no such reader
390 1.1.1.2 christos has been registered, return NULL. */
391 1.1.1.2 christos cutu_reader *get_reader (dwarf2_per_cu_data *per_cu);
392 1.1 christos
393 1.1.1.2 christos /* Preserve READER by storing it in the local hash table. */
394 1.1.1.2 christos cutu_reader *preserve (std::unique_ptr<cutu_reader> reader);
395 1.1.1.2 christos
396 1.1.1.2 christos /* Add an entry to the index. The arguments describe the entry; see
397 1.1.1.2 christos cooked-index.h. The new entry is returned. */
398 1.1.1.2 christos cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
399 1.1.1.2 christos cooked_index_flag flags,
400 1.1.1.2 christos const char *name,
401 1.1.1.2 christos cooked_index_entry_ref parent_entry,
402 1.1.1.2 christos dwarf2_per_cu_data *per_cu)
403 1.1.1.2 christos {
404 1.1.1.2 christos return m_index->add (die_offset, tag, flags, per_cu->lang (),
405 1.1.1.2 christos name, parent_entry, per_cu);
406 1.1.1.2 christos }
407 1.1.1.2 christos
408 1.1.1.2 christos /* Install the current addrmap into the shard being constructed,
409 1.1.1.2 christos then transfer ownership of the index to the caller. */
410 1.1.1.2 christos std::unique_ptr<cooked_index_shard> release ()
411 1.1.1.2 christos {
412 1.1.1.2 christos m_index->install_addrmap (&m_addrmap);
413 1.1.1.2 christos return std::move (m_index);
414 1.1.1.2 christos }
415 1.1.1.2 christos
416 1.1.1.2 christos /* Return the mutable addrmap that is currently being created. */
417 1.1.1.2 christos addrmap_mutable *get_addrmap ()
418 1.1.1.2 christos {
419 1.1.1.2 christos return &m_addrmap;
420 1.1.1.2 christos }
421 1.1 christos
422 1.1.1.2 christos /* Return the parent_map that is currently being created. */
423 1.1.1.2 christos parent_map *get_parent_map ()
424 1.1 christos {
425 1.1.1.2 christos return &m_parent_map;
426 1.1 christos }
427 1.1 christos
428 1.1.1.2 christos /* Return the parent_map that is currently being created. Ownership
429 1.1.1.2 christos is passed to the caller. */
430 1.1.1.2 christos parent_map release_parent_map ()
431 1.1 christos {
432 1.1.1.2 christos return std::move (m_parent_map);
433 1.1 christos }
434 1.1 christos
435 1.1.1.2 christos private:
436 1.1.1.2 christos
437 1.1.1.2 christos /* Hash function for a cutu_reader. */
438 1.1.1.2 christos static hashval_t hash_cutu_reader (const void *a);
439 1.1.1.2 christos
440 1.1.1.2 christos /* Equality function for cutu_reader. */
441 1.1.1.2 christos static int eq_cutu_reader (const void *a, const void *b);
442 1.1.1.2 christos
443 1.1.1.2 christos /* The abbrev cache used by this indexer. */
444 1.1.1.2 christos abbrev_cache m_abbrev_cache;
445 1.1.1.2 christos /* A hash table of cutu_reader objects. */
446 1.1.1.2 christos htab_up m_reader_hash;
447 1.1.1.2 christos /* The index shard that is being constructed. */
448 1.1.1.2 christos std::unique_ptr<cooked_index_shard> m_index;
449 1.1.1.2 christos
450 1.1.1.2 christos /* Parent map for each CU that is read. */
451 1.1.1.2 christos parent_map m_parent_map;
452 1.1.1.2 christos
453 1.1.1.2 christos /* A writeable addrmap being constructed by this scanner. */
454 1.1.1.2 christos addrmap_mutable m_addrmap;
455 1.1.1.2 christos };
456 1.1.1.2 christos
457 1.1.1.2 christos /* The possible states of the index. See the explanatory comment
458 1.1.1.2 christos before cooked_index for more details. */
459 1.1.1.2 christos enum class cooked_state
460 1.1.1.2 christos {
461 1.1.1.2 christos /* The default state. This is not a valid argument to 'wait'. */
462 1.1.1.2 christos INITIAL,
463 1.1.1.2 christos /* The initial scan has completed. The name of "main" is now
464 1.1.1.2 christos available (if known). The addrmaps are usable now.
465 1.1.1.2 christos Finalization has started but is not complete. */
466 1.1.1.2 christos MAIN_AVAILABLE,
467 1.1.1.2 christos /* Finalization has completed. This means the index is fully
468 1.1.1.2 christos available for queries. */
469 1.1.1.2 christos FINALIZED,
470 1.1.1.2 christos /* Writing to the index cache has finished. */
471 1.1.1.2 christos CACHE_DONE,
472 1.1.1.2 christos };
473 1.1.1.2 christos
474 1.1.1.2 christos /* An object of this type controls the scanning of the DWARF. It
475 1.1.1.2 christos schedules the worker tasks and tracks the current state. Once
476 1.1.1.2 christos scanning is done, this object is discarded.
477 1.1.1.2 christos
478 1.1.1.2 christos This is an abstract base class that defines the basic behavior of
479 1.1.1.2 christos scanners. Separate concrete implementations exist for scanning
480 1.1.1.2 christos .debug_names and .debug_info. */
481 1.1.1.2 christos
482 1.1.1.2 christos class cooked_index_worker
483 1.1.1.2 christos {
484 1.1.1.2 christos public:
485 1.1.1.2 christos
486 1.1.1.2 christos explicit cooked_index_worker (dwarf2_per_objfile *per_objfile)
487 1.1.1.2 christos : m_per_objfile (per_objfile),
488 1.1.1.2 christos m_cache_store (global_index_cache, per_objfile->per_bfd)
489 1.1.1.2 christos { }
490 1.1.1.2 christos virtual ~cooked_index_worker ()
491 1.1.1.2 christos { }
492 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_worker);
493 1.1.1.2 christos
494 1.1.1.2 christos /* Start reading. */
495 1.1.1.2 christos void start ();
496 1.1.1.2 christos
497 1.1.1.2 christos /* Wait for a particular state to be achieved. If ALLOW_QUIT is
498 1.1.1.2 christos true, then the loop will check the QUIT flag. Normally this
499 1.1.1.2 christos method may only be called from the main thread; however, it can
500 1.1.1.2 christos be called from a worker thread provided that the desired state
501 1.1.1.2 christos has already been attained. (This oddity is used by the index
502 1.1.1.2 christos cache writer.) */
503 1.1.1.2 christos bool wait (cooked_state desired_state, bool allow_quit);
504 1.1.1.2 christos
505 1.1.1.2 christos protected:
506 1.1.1.2 christos
507 1.1.1.2 christos /* Let cooked_index call the 'set' and 'write_to_cache' methods. */
508 1.1.1.2 christos friend class cooked_index;
509 1.1.1.2 christos
510 1.1.1.2 christos /* Set the current state. */
511 1.1.1.2 christos void set (cooked_state desired_state);
512 1.1.1.2 christos
513 1.1.1.2 christos /* Write to the index cache. */
514 1.1.1.2 christos void write_to_cache (const cooked_index *idx,
515 1.1.1.2 christos deferred_warnings *warn) const;
516 1.1.1.2 christos
517 1.1.1.2 christos /* Helper function that does the work of reading. This must be able
518 1.1.1.2 christos to be run in a worker thread without problems. */
519 1.1.1.2 christos virtual void do_reading () = 0;
520 1.1.1.2 christos
521 1.1.1.2 christos /* A callback that can print stats, if needed. This is called when
522 1.1.1.2 christos transitioning to the 'MAIN_AVAILABLE' state. */
523 1.1.1.2 christos virtual void print_stats ()
524 1.1.1.2 christos { }
525 1.1.1.2 christos
526 1.1.1.2 christos /* Each thread returns a tuple holding a cooked index, any collected
527 1.1.1.2 christos complaints, a vector of errors that should be printed, and a
528 1.1.1.2 christos vector of parent maps.
529 1.1.1.2 christos
530 1.1.1.2 christos The errors are retained because GDB's I/O system is not
531 1.1.1.2 christos thread-safe. run_on_main_thread could be used, but that would
532 1.1.1.2 christos mean the messages are printed after the prompt, which looks
533 1.1.1.2 christos weird. */
534 1.1.1.2 christos using result_type = std::tuple<std::unique_ptr<cooked_index_shard>,
535 1.1.1.2 christos complaint_collection,
536 1.1.1.2 christos std::vector<gdb_exception>,
537 1.1.1.2 christos parent_map>;
538 1.1.1.2 christos
539 1.1.1.2 christos /* The per-objfile object. */
540 1.1.1.2 christos dwarf2_per_objfile *m_per_objfile;
541 1.1.1.2 christos /* Result of each worker task. */
542 1.1.1.2 christos std::vector<result_type> m_results;
543 1.1.1.2 christos /* Any warnings emitted. This is not in 'result_type' because (for
544 1.1.1.2 christos the time being at least), it's only needed in do_reading, not in
545 1.1.1.2 christos every worker. Note that deferred_warnings uses gdb_stderr in its
546 1.1.1.2 christos constructor, and this should only be done from the main thread.
547 1.1.1.2 christos This is enforced in the cooked_index_worker constructor. */
548 1.1.1.2 christos deferred_warnings m_warnings;
549 1.1.1.2 christos
550 1.1.1.2 christos /* A map of all parent maps. Used during finalization to fix up
551 1.1.1.2 christos parent relationships. */
552 1.1.1.2 christos parent_map_map m_all_parents_map;
553 1.1.1.2 christos
554 1.1.1.2 christos #if CXX_STD_THREAD
555 1.1.1.2 christos /* Current state of this object. */
556 1.1.1.2 christos cooked_state m_state = cooked_state::INITIAL;
557 1.1.1.2 christos /* Mutex and condition variable used to synchronize. */
558 1.1.1.2 christos std::mutex m_mutex;
559 1.1.1.2 christos std::condition_variable m_cond;
560 1.1.1.2 christos #endif /* CXX_STD_THREAD */
561 1.1.1.2 christos /* This flag indicates whether any complaints or exceptions that
562 1.1.1.2 christos arose during scanning have been reported by 'wait'. This may
563 1.1.1.2 christos only be modified on the main thread. */
564 1.1.1.2 christos bool m_reported = false;
565 1.1.1.2 christos /* If set, an exception occurred during reading; in this case the
566 1.1.1.2 christos scanning is stopped and this exception will later be reported by
567 1.1.1.2 christos the 'wait' method. */
568 1.1.1.2 christos std::optional<gdb_exception> m_failed;
569 1.1.1.2 christos /* An object used to write to the index cache. */
570 1.1.1.2 christos index_cache_store_context m_cache_store;
571 1.1.1.2 christos };
572 1.1.1.2 christos
573 1.1.1.2 christos /* The main index of DIEs.
574 1.1.1.2 christos
575 1.1.1.2 christos The index is created by multiple threads. The overall process is
576 1.1.1.2 christos somewhat complicated, so here's a diagram to help sort it out.
577 1.1.1.2 christos
578 1.1.1.2 christos The basic idea behind this design is (1) to do as much work as
579 1.1.1.2 christos possible in worker threads, and (2) to start the work as early as
580 1.1.1.2 christos possible. This combination should help hide the effort from the
581 1.1.1.2 christos user to the maximum possible degree.
582 1.1.1.2 christos
583 1.1.1.2 christos . Main Thread | Worker Threads
584 1.1.1.2 christos ============================================================
585 1.1.1.2 christos . dwarf2_initialize_objfile
586 1.1.1.2 christos . |
587 1.1.1.2 christos . v
588 1.1.1.2 christos . cooked index ------------> cooked_index_worker::start
589 1.1.1.2 christos . | / | \
590 1.1.1.2 christos . v / | \
591 1.1.1.2 christos . install / | \
592 1.1.1.2 christos . cooked_index_functions scan CUs in workers
593 1.1.1.2 christos . | create cooked_index_shard objects
594 1.1.1.2 christos . | \ | /
595 1.1.1.2 christos . v \|/
596 1.1.1.2 christos . return to caller v
597 1.1.1.2 christos . initial scan is done
598 1.1.1.2 christos . state = MAIN_AVAILABLE
599 1.1.1.2 christos . "main" name now available
600 1.1.1.2 christos . |
601 1.1.1.2 christos . |
602 1.1.1.2 christos . if main thread calls... v
603 1.1.1.2 christos . compute_main_name cooked_index::set_contents
604 1.1.1.2 christos . | / | \
605 1.1.1.2 christos . v / | \
606 1.1.1.2 christos . wait (MAIN_AVAILABLE) finalization
607 1.1.1.2 christos . | \ | /
608 1.1.1.2 christos . v \ | /
609 1.1.1.2 christos . done state = FINALIZED
610 1.1.1.2 christos . |
611 1.1.1.2 christos . v
612 1.1.1.2 christos . maybe write to index cache
613 1.1.1.2 christos . state = CACHE_DONE
614 1.1.1.2 christos .
615 1.1.1.2 christos .
616 1.1.1.2 christos . if main thread calls...
617 1.1.1.2 christos . any other "quick" API
618 1.1.1.2 christos . |
619 1.1.1.2 christos . v
620 1.1.1.2 christos . wait (FINALIZED)
621 1.1.1.2 christos . |
622 1.1.1.2 christos . v
623 1.1.1.2 christos . use the index
624 1.1.1.2 christos */
625 1.1.1.2 christos
626 1.1.1.2 christos class cooked_index : public dwarf_scanner_base
627 1.1.1.2 christos {
628 1.1.1.2 christos public:
629 1.1.1.2 christos
630 1.1.1.2 christos /* A convenience typedef for the vector that is contained in this
631 1.1.1.2 christos object. */
632 1.1.1.2 christos using vec_type = std::vector<std::unique_ptr<cooked_index_shard>>;
633 1.1.1.2 christos
634 1.1.1.2 christos cooked_index (dwarf2_per_objfile *per_objfile,
635 1.1.1.2 christos std::unique_ptr<cooked_index_worker> &&worker);
636 1.1.1.2 christos ~cooked_index () override;
637 1.1.1.2 christos
638 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index);
639 1.1.1.2 christos
640 1.1.1.2 christos /* Start reading the DWARF. */
641 1.1.1.2 christos void start_reading ();
642 1.1.1.2 christos
643 1.1.1.2 christos /* Called by cooked_index_worker to set the contents of this index
644 1.1.1.2 christos and transition to the MAIN_AVAILABLE state. WARN is used to
645 1.1.1.2 christos collect any warnings that may arise when writing to the cache.
646 1.1.1.2 christos PARENT_MAPS is used when resolving pending parent links.
647 1.1.1.2 christos PARENT_MAPS may be NULL if there are no IS_PARENT_DEFERRED
648 1.1.1.2 christos entries in VEC. */
649 1.1.1.2 christos void set_contents (vec_type &&vec, deferred_warnings *warn,
650 1.1.1.2 christos const parent_map_map *parent_maps);
651 1.1.1.2 christos
652 1.1 christos /* A range over a vector of subranges. */
653 1.1.1.2 christos using range = range_chain<cooked_index_shard::range>;
654 1.1 christos
655 1.1 christos /* Look up an entry by name. Returns a range of all matching
656 1.1 christos results. If COMPLETING is true, then a larger range, suitable
657 1.1 christos for completion, will be returned. */
658 1.1 christos range find (const std::string &name, bool completing);
659 1.1 christos
660 1.1 christos /* Return a range of all the entries. */
661 1.1 christos range all_entries ()
662 1.1 christos {
663 1.1.1.2 christos wait (cooked_state::FINALIZED, true);
664 1.1.1.2 christos std::vector<cooked_index_shard::range> result_range;
665 1.1 christos result_range.reserve (m_vector.size ());
666 1.1 christos for (auto &entry : m_vector)
667 1.1 christos result_range.push_back (entry->all_entries ());
668 1.1 christos return range (std::move (result_range));
669 1.1 christos }
670 1.1 christos
671 1.1 christos /* Look up ADDR in the address map, and return either the
672 1.1 christos corresponding CU, or nullptr if the address could not be
673 1.1 christos found. */
674 1.1.1.2 christos dwarf2_per_cu_data *lookup (unrelocated_addr addr);
675 1.1 christos
676 1.1 christos /* Return a new vector of all the addrmaps used by all the indexes
677 1.1 christos held by this object. */
678 1.1.1.2 christos std::vector<const addrmap *> get_addrmaps ();
679 1.1 christos
680 1.1 christos /* Return the entry that is believed to represent the program's
681 1.1 christos "main". This will return NULL if no such entry is available. */
682 1.1 christos const cooked_index_entry *get_main () const;
683 1.1 christos
684 1.1.1.2 christos const char *get_main_name (struct obstack *obstack, enum language *lang)
685 1.1.1.2 christos const;
686 1.1.1.2 christos
687 1.1.1.2 christos cooked_index *index_for_writing () override
688 1.1 christos {
689 1.1.1.2 christos wait (cooked_state::FINALIZED, true);
690 1.1 christos return this;
691 1.1 christos }
692 1.1 christos
693 1.1 christos quick_symbol_functions_up make_quick_functions () const override;
694 1.1 christos
695 1.1.1.2 christos /* Dump a human-readable form of the contents of the index. */
696 1.1.1.2 christos void dump (gdbarch *arch);
697 1.1.1.2 christos
698 1.1.1.2 christos /* Wait until this object reaches the desired state. Note that
699 1.1.1.2 christos DESIRED_STATE may not be INITIAL -- it does not make sense to
700 1.1.1.2 christos wait for this. If ALLOW_QUIT is true, timed waits will be done
701 1.1.1.2 christos and the quit flag will be checked in a loop. This may normally
702 1.1.1.2 christos only be called from the main thread; however, it is ok to call
703 1.1.1.2 christos from a worker as long as the desired state has already been
704 1.1.1.2 christos attained. (This property is needed by the index cache
705 1.1.1.2 christos writer.) */
706 1.1.1.2 christos void wait (cooked_state desired_state, bool allow_quit = false);
707 1.1.1.2 christos
708 1.1.1.2 christos void wait_completely () override
709 1.1.1.2 christos { wait (cooked_state::CACHE_DONE); }
710 1.1.1.2 christos
711 1.1 christos private:
712 1.1 christos
713 1.1 christos /* The vector of cooked_index objects. This is stored because the
714 1.1 christos entries are stored on the obstacks in those objects. */
715 1.1 christos vec_type m_vector;
716 1.1.1.2 christos
717 1.1.1.2 christos /* This tracks the current state. When this is nullptr, it means
718 1.1.1.2 christos that the state is CACHE_DONE -- it's important to note that only
719 1.1.1.2 christos the main thread may change the value of this pointer. */
720 1.1.1.2 christos std::unique_ptr<cooked_index_worker> m_state;
721 1.1.1.2 christos
722 1.1.1.2 christos dwarf2_per_bfd *m_per_bfd;
723 1.1.1.2 christos };
724 1.1.1.2 christos
725 1.1.1.2 christos /* An implementation of quick_symbol_functions for the cooked DWARF
726 1.1.1.2 christos index. */
727 1.1.1.2 christos
728 1.1.1.2 christos struct cooked_index_functions : public dwarf2_base_index_functions
729 1.1.1.2 christos {
730 1.1.1.2 christos cooked_index *wait (struct objfile *objfile, bool allow_quit)
731 1.1.1.2 christos {
732 1.1.1.2 christos dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
733 1.1.1.2 christos cooked_index *table
734 1.1.1.2 christos = (gdb::checked_static_cast<cooked_index *>
735 1.1.1.2 christos (per_objfile->per_bfd->index_table.get ()));
736 1.1.1.2 christos table->wait (cooked_state::MAIN_AVAILABLE, allow_quit);
737 1.1.1.2 christos return table;
738 1.1.1.2 christos }
739 1.1.1.2 christos
740 1.1.1.2 christos dwarf2_per_cu_data *find_per_cu (dwarf2_per_bfd *per_bfd,
741 1.1.1.2 christos unrelocated_addr adjusted_pc) override;
742 1.1.1.2 christos
743 1.1.1.2 christos struct compunit_symtab *find_compunit_symtab_by_address
744 1.1.1.2 christos (struct objfile *objfile, CORE_ADDR address) override;
745 1.1.1.2 christos
746 1.1.1.2 christos bool has_unexpanded_symtabs (struct objfile *objfile) override
747 1.1.1.2 christos {
748 1.1.1.2 christos wait (objfile, true);
749 1.1.1.2 christos return dwarf2_base_index_functions::has_unexpanded_symtabs (objfile);
750 1.1.1.2 christos }
751 1.1.1.2 christos
752 1.1.1.2 christos struct symtab *find_last_source_symtab (struct objfile *objfile) override
753 1.1.1.2 christos {
754 1.1.1.2 christos wait (objfile, true);
755 1.1.1.2 christos return dwarf2_base_index_functions::find_last_source_symtab (objfile);
756 1.1.1.2 christos }
757 1.1.1.2 christos
758 1.1.1.2 christos void forget_cached_source_info (struct objfile *objfile) override
759 1.1.1.2 christos {
760 1.1.1.2 christos wait (objfile, true);
761 1.1.1.2 christos dwarf2_base_index_functions::forget_cached_source_info (objfile);
762 1.1.1.2 christos }
763 1.1.1.2 christos
764 1.1.1.2 christos void print_stats (struct objfile *objfile, bool print_bcache) override
765 1.1.1.2 christos {
766 1.1.1.2 christos wait (objfile, true);
767 1.1.1.2 christos dwarf2_base_index_functions::print_stats (objfile, print_bcache);
768 1.1.1.2 christos }
769 1.1.1.2 christos
770 1.1.1.2 christos void dump (struct objfile *objfile) override
771 1.1.1.2 christos {
772 1.1.1.2 christos cooked_index *index = wait (objfile, true);
773 1.1.1.2 christos gdb_printf ("Cooked index in use:\n");
774 1.1.1.2 christos gdb_printf ("\n");
775 1.1.1.2 christos index->dump (objfile->arch ());
776 1.1.1.2 christos }
777 1.1.1.2 christos
778 1.1.1.2 christos void expand_all_symtabs (struct objfile *objfile) override
779 1.1.1.2 christos {
780 1.1.1.2 christos wait (objfile, true);
781 1.1.1.2 christos dwarf2_base_index_functions::expand_all_symtabs (objfile);
782 1.1.1.2 christos }
783 1.1.1.2 christos
784 1.1.1.2 christos bool expand_symtabs_matching
785 1.1.1.2 christos (struct objfile *objfile,
786 1.1.1.2 christos gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
787 1.1.1.2 christos const lookup_name_info *lookup_name,
788 1.1.1.2 christos gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
789 1.1.1.2 christos gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
790 1.1.1.2 christos block_search_flags search_flags,
791 1.1.1.2 christos domain_search_flags domain) override;
792 1.1.1.2 christos
793 1.1.1.2 christos struct compunit_symtab *find_pc_sect_compunit_symtab
794 1.1.1.2 christos (struct objfile *objfile, struct bound_minimal_symbol msymbol,
795 1.1.1.2 christos CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override
796 1.1.1.2 christos {
797 1.1.1.2 christos wait (objfile, true);
798 1.1.1.2 christos return (dwarf2_base_index_functions::find_pc_sect_compunit_symtab
799 1.1.1.2 christos (objfile, msymbol, pc, section, warn_if_readin));
800 1.1.1.2 christos }
801 1.1.1.2 christos
802 1.1.1.2 christos void map_symbol_filenames
803 1.1.1.2 christos (struct objfile *objfile,
804 1.1.1.2 christos gdb::function_view<symbol_filename_ftype> fun,
805 1.1.1.2 christos bool need_fullname) override
806 1.1.1.2 christos {
807 1.1.1.2 christos wait (objfile, true);
808 1.1.1.2 christos return (dwarf2_base_index_functions::map_symbol_filenames
809 1.1.1.2 christos (objfile, fun, need_fullname));
810 1.1.1.2 christos }
811 1.1.1.2 christos
812 1.1.1.2 christos void compute_main_name (struct objfile *objfile) override
813 1.1.1.2 christos {
814 1.1.1.2 christos wait (objfile, false);
815 1.1.1.2 christos }
816 1.1 christos };
817 1.1 christos
818 1.1 christos #endif /* GDB_DWARF2_COOKED_INDEX_H */
819