3 Copyright (C) 2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #ifndef GDB_DWARF2_COOKED_INDEX_H
21 #define GDB_DWARF2_COOKED_INDEX_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"
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"
38 struct dwarf2_per_cu_data;
40 /* Flags that describe an entry in the index. */
41 enum cooked_index_flag_enum : unsigned char
43 /* True if this entry is the program's "main". */
45 /* True if this entry represents a "static" object. */
47 /* True if this entry is an "enum class". */
49 /* True if this entry uses the linkage name. */
51 /* True if this entry is just for the declaration of a type, not the
53 IS_TYPE_DECLARATION = 16,
55 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag);
57 /* A cooked_index_entry represents a single item in the index. Note
58 that two entries can be created for the same DIE -- one using the
59 name, and another one using the linkage name, if any.
61 This is an "open" class and the members are all directly
62 accessible. It is read-only after the index has been fully read
64 struct cooked_index_entry : public allocate_on_obstack
66 cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_,
67 cooked_index_flag flags_, const char *name_,
68 const cooked_index_entry *parent_entry_,
69 dwarf2_per_cu_data *per_cu_)
73 die_offset (die_offset_),
74 parent_entry (parent_entry_),
79 /* Return true if this entry matches SEARCH_FLAGS. */
80 bool matches (block_search_flags search_flags) const
82 /* Just reject type declarations. */
83 if ((flags & IS_TYPE_DECLARATION) != 0)
86 if ((search_flags & SEARCH_STATIC_BLOCK) != 0
87 && (flags & IS_STATIC) != 0)
89 if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0
90 && (flags & IS_STATIC) == 0)
95 /* Return true if this entry matches DOMAIN. */
96 bool matches (domain_enum domain) const
98 /* Just reject type declarations. */
99 if ((flags & IS_TYPE_DECLARATION) != 0)
108 return tag == DW_TAG_module;
110 case COMMON_BLOCK_DOMAIN:
111 return tag == DW_TAG_common_block;
117 /* Return true if this entry matches KIND. */
118 bool matches (enum search_domain kind) const
120 /* Just reject type declarations. */
121 if ((flags & IS_TYPE_DECLARATION) != 0)
126 case VARIABLES_DOMAIN:
127 return (tag == DW_TAG_variable
128 || tag == DW_TAG_constant
129 || tag == DW_TAG_enumerator);
130 case FUNCTIONS_DOMAIN:
131 return tag == DW_TAG_subprogram;
133 return tag_is_type (tag);
135 return tag == DW_TAG_module;
141 /* Construct the fully-qualified name of this entry and return a
142 pointer to it. If allocation is needed, it will be done on
144 const char *full_name (struct obstack *storage) const;
146 /* Entries must be sorted case-insensitively; this compares two
148 bool operator< (const cooked_index_entry &other) const
150 return strcasecmp (canonical, other.canonical) < 0;
153 /* The name as it appears in DWARF. This always points into one of
154 the mapped DWARF sections. Note that this may be the name or the
155 linkage name -- two entries are created for DIEs which have both
158 /* The canonical name. For C++ names, this may differ from NAME.
159 In all other cases, this is equal to NAME. */
160 const char *canonical = nullptr;
163 /* Any flags attached to this entry. */
164 cooked_index_flag flags;
165 /* The offset of this DIE. */
166 sect_offset die_offset;
167 /* The parent entry. This is NULL for top-level entries.
168 Otherwise, it points to the parent entry, such as a namespace or
170 const cooked_index_entry *parent_entry;
171 /* The CU from which this entry originates. */
172 dwarf2_per_cu_data *per_cu;
176 void write_scope (struct obstack *storage, const char *sep) const;
179 class cooked_index_vector;
181 /* An index of interesting DIEs. This is "cooked", in contrast to a
182 mapped .debug_names or .gdb_index, which are "raw". An entry in
183 the index is of type cooked_index_entry.
185 Operations on the index are described below. They are chosen to
186 make it relatively simple to implement the symtab "quick"
191 cooked_index () = default;
192 DISABLE_COPY_AND_ASSIGN (cooked_index);
194 /* Create a new cooked_index_entry and register it with this object.
195 Entries are owned by this object. The new item is returned. */
196 const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag,
197 cooked_index_flag flags,
199 const cooked_index_entry *parent_entry,
200 dwarf2_per_cu_data *per_cu);
202 /* Install a new fixed addrmap from the given mutable addrmap. */
203 void install_addrmap (addrmap_mutable *map)
205 gdb_assert (m_addrmap == nullptr);
206 m_addrmap = new (&m_storage) addrmap_fixed (&m_storage, map);
209 /* Finalize the index. This should be called a single time, when
210 the index has been fully populated. It enters all the entries
211 into the internal table. */
214 /* Wait for this index's finalization to be complete. */
220 friend class cooked_index_vector;
222 /* A simple range over part of m_entries. */
223 typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
225 /* Return a range of all the entries. */
229 return { m_entries.begin (), m_entries.end () };
232 /* Look up an entry by name. Returns a range of all matching
233 results. If COMPLETING is true, then a larger range, suitable
234 for completion, will be returned. */
235 range find (gdb::string_view name, bool completing);
239 /* Return the entry that is believed to represent the program's
240 "main". This will return NULL if no such entry is available. */
241 const cooked_index_entry *get_main () const
246 /* Look up ADDR in the address map, and return either the
247 corresponding CU, or nullptr if the address could not be
249 dwarf2_per_cu_data *lookup (CORE_ADDR addr)
251 return (dwarf2_per_cu_data *) m_addrmap->find (addr);
254 /* Create a new cooked_index_entry and register it with this object.
255 Entries are owned by this object. The new item is returned. */
256 cooked_index_entry *create (sect_offset die_offset,
258 cooked_index_flag flags,
260 const cooked_index_entry *parent_entry,
261 dwarf2_per_cu_data *per_cu)
263 return new (&m_storage) cooked_index_entry (die_offset, tag, flags,
268 /* GNAT only emits mangled ("encoded") names in the DWARF, and does
269 not emit the module structure. However, we need this structure
270 to do lookups. This function recreates that structure for an
271 existing entry. It returns the base name (last element) of the
272 full decoded name. */
273 gdb::unique_xmalloc_ptr<char> handle_gnat_encoded_entry
274 (cooked_index_entry *entry, htab_t gnat_entries);
276 /* A helper method that does the work of 'finalize'. */
279 /* Storage for the entries. */
280 auto_obstack m_storage;
281 /* List of all entries. */
282 std::vector<cooked_index_entry *> m_entries;
283 /* If we found "main" or an entry with 'is_main' set, store it
285 cooked_index_entry *m_main = nullptr;
286 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
288 addrmap *m_addrmap = nullptr;
289 /* Storage for canonical names. */
290 std::vector<gdb::unique_xmalloc_ptr<char>> m_names;
291 /* A future that tracks when the 'finalize' method is done. Note
292 that the 'get' method is never called on this future, only
294 gdb::future<void> m_future;
297 /* The main index of DIEs. The parallel DIE indexers create
298 cooked_index objects. Then, these are all handled to a
299 cooked_index_vector for storage and final indexing. The index is
300 made by iterating over the entries previously created. */
302 class cooked_index_vector : public dwarf_scanner_base
306 /* A convenience typedef for the vector that is contained in this
308 typedef std::vector<std::unique_ptr<cooked_index>> vec_type;
310 explicit cooked_index_vector (vec_type &&vec);
311 DISABLE_COPY_AND_ASSIGN (cooked_index_vector);
313 /* Wait until the finalization of the entire cooked_index_vector is
317 for (auto &item : m_vector)
321 ~cooked_index_vector ()
323 /* The 'finalize' methods may be run in a different thread. If
324 this object is destroyed before these complete, then one will
325 end up writing to freed memory. Waiting for finalization to
326 complete avoids this problem; and the cost seems ignorable
327 because creating and immediately destroying the debug info is a
328 relatively rare thing to do. */
332 /* A range over a vector of subranges. */
333 typedef range_chain<cooked_index::range> range;
335 /* Look up an entry by name. Returns a range of all matching
336 results. If COMPLETING is true, then a larger range, suitable
337 for completion, will be returned. */
338 range find (gdb::string_view name, bool completing);
340 /* Return a range of all the entries. */
343 std::vector<cooked_index::range> result_range;
344 result_range.reserve (m_vector.size ());
345 for (auto &entry : m_vector)
346 result_range.push_back (entry->all_entries ());
347 return range (std::move (result_range));
350 /* Look up ADDR in the address map, and return either the
351 corresponding CU, or nullptr if the address could not be
353 dwarf2_per_cu_data *lookup (CORE_ADDR addr);
355 /* Return a new vector of all the addrmaps used by all the indexes
356 held by this object. */
357 std::vector<addrmap *> get_addrmaps ();
359 /* Return the entry that is believed to represent the program's
360 "main". This will return NULL if no such entry is available. */
361 const cooked_index_entry *get_main () const;
363 cooked_index_vector *index_for_writing () override
368 quick_symbol_functions_up make_quick_functions () const override;
372 /* The vector of cooked_index objects. This is stored because the
373 entries are stored on the obstacks in those objects. */
377 #endif /* GDB_DWARF2_COOKED_INDEX_H */