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. */
52 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum
, cooked_index_flag
);
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.
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
61 struct cooked_index_entry
: public allocate_on_obstack
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_
)
70 die_offset (die_offset_
),
71 parent_entry (parent_entry_
),
76 /* Return true if this entry matches SEARCH_FLAGS. */
77 bool matches (block_search_flags search_flags
) const
79 if ((search_flags
& SEARCH_STATIC_BLOCK
) != 0
80 && (flags
& IS_STATIC
) != 0)
82 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) != 0
83 && (flags
& IS_STATIC
) == 0)
88 /* Return true if this entry matches DOMAIN. */
89 bool matches (domain_enum domain
) const
97 return tag
== DW_TAG_module
;
99 case COMMON_BLOCK_DOMAIN
:
100 return tag
== DW_TAG_common_block
;
106 /* Return true if this entry matches KIND. */
107 bool matches (enum search_domain kind
) const
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
;
118 return tag_is_type (tag
);
120 return tag
== DW_TAG_module
;
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
129 const char *full_name (struct obstack
*storage
) const;
131 /* Entries must be sorted case-insensitively; this compares two
133 bool operator< (const cooked_index_entry
&other
) const
135 return strcasecmp (canonical
, other
.canonical
) < 0;
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
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;
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
155 const cooked_index_entry
*parent_entry
;
156 /* The CU from which this entry originates. */
157 dwarf2_per_cu_data
*per_cu
;
161 void write_scope (struct obstack
*storage
, const char *sep
) const;
164 class cooked_index_vector
;
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.
170 Operations on the index are described below. They are chosen to
171 make it relatively simple to implement the symtab "quick"
176 cooked_index () = default;
177 DISABLE_COPY_AND_ASSIGN (cooked_index
);
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
,
184 const cooked_index_entry
*parent_entry
,
185 dwarf2_per_cu_data
*per_cu
);
187 /* Install a new fixed addrmap from the given mutable addrmap. */
188 void install_addrmap (addrmap_mutable
*map
)
190 gdb_assert (m_addrmap
== nullptr);
191 m_addrmap
= new (&m_storage
) addrmap_fixed (&m_storage
, map
);
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. */
199 /* Wait for this index's finalization to be complete. */
205 friend class cooked_index_vector
;
207 /* A simple range over part of m_entries. */
208 typedef iterator_range
<std::vector
<cooked_index_entry
*>::iterator
> range
;
210 /* Return a range of all the entries. */
214 return { m_entries
.begin (), m_entries
.end () };
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
);
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
231 /* Look up ADDR in the address map, and return either the
232 corresponding CU, or nullptr if the address could not be
234 dwarf2_per_cu_data
*lookup (CORE_ADDR addr
)
236 return (dwarf2_per_cu_data
*) m_addrmap
->find (addr
);
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
,
243 cooked_index_flag flags
,
245 const cooked_index_entry
*parent_entry
,
246 dwarf2_per_cu_data
*per_cu
)
248 return new (&m_storage
) cooked_index_entry (die_offset
, tag
, flags
,
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
);
261 /* A helper method that does the work of 'finalize'. */
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
270 cooked_index_entry
*m_main
= nullptr;
271 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
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
279 gdb::future
<void> m_future
;
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. */
287 class cooked_index_vector
: public dwarf_scanner_base
291 /* A convenience typedef for the vector that is contained in this
293 typedef std::vector
<std::unique_ptr
<cooked_index
>> vec_type
;
295 explicit cooked_index_vector (vec_type
&&vec
);
296 DISABLE_COPY_AND_ASSIGN (cooked_index_vector
);
298 /* Wait until the finalization of the entire cooked_index_vector is
302 for (auto &item
: m_vector
)
306 ~cooked_index_vector ()
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. */
317 /* A range over a vector of subranges. */
318 typedef range_chain
<cooked_index::range
> range
;
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
);
325 /* Return a range of all the entries. */
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
));
335 /* Look up ADDR in the address map, and return either the
336 corresponding CU, or nullptr if the address could not be
338 dwarf2_per_cu_data
*lookup (CORE_ADDR addr
);
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 ();
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;
348 cooked_index_vector
*index_for_writing () override
353 quick_symbol_functions_up
make_quick_functions () const override
;
357 /* The vector of cooked_index objects. This is stored because the
358 entries are stored on the obstacks in those objects. */
362 #endif /* GDB_DWARF2_COOKED_INDEX_H */