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"
36 struct dwarf2_per_cu_data
;
38 /* Flags that describe an entry in the index. */
39 enum cooked_index_flag_enum
: unsigned char
41 /* True if this entry is the program's "main". */
43 /* True if this entry represents a "static" object. */
45 /* True if this entry is an "enum class". */
47 /* True if this entry uses the linkage name. */
50 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum
, cooked_index_flag
);
52 /* A cooked_index_entry represents a single item in the index. Note
53 that two entries can be created for the same DIE -- one using the
54 name, and another one using the linkage name, if any.
56 This is an "open" class and the members are all directly
57 accessible. It is read-only after the index has been fully read
59 struct cooked_index_entry
: public allocate_on_obstack
61 cooked_index_entry (sect_offset die_offset_
, enum dwarf_tag tag_
,
62 cooked_index_flag flags_
, const char *name_
,
63 const cooked_index_entry
*parent_entry_
,
64 dwarf2_per_cu_data
*per_cu_
)
68 die_offset (die_offset_
),
69 parent_entry (parent_entry_
),
74 /* Return true if this entry matches SEARCH_FLAGS. */
75 bool matches (block_search_flags search_flags
) const
77 if ((search_flags
& SEARCH_STATIC_BLOCK
) != 0
78 && (flags
& IS_STATIC
) != 0)
80 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) != 0
81 && (flags
& IS_STATIC
) == 0)
86 /* Return true if this entry matches DOMAIN. */
87 bool matches (domain_enum domain
) const
95 return tag
== DW_TAG_module
;
97 case COMMON_BLOCK_DOMAIN
:
98 return tag
== DW_TAG_common_block
;
104 /* Return true if this entry matches KIND. */
105 bool matches (enum search_domain kind
) const
109 case VARIABLES_DOMAIN
:
110 return tag
== DW_TAG_variable
;
111 case FUNCTIONS_DOMAIN
:
112 return tag
== DW_TAG_subprogram
;
114 return tag
== DW_TAG_typedef
|| tag
== DW_TAG_structure_type
;
116 return tag
== DW_TAG_module
;
122 /* Construct the fully-qualified name of this entry and return a
123 pointer to it. If allocation is needed, it will be done on
125 const char *full_name (struct obstack
*storage
) const;
127 /* Entries must be sorted case-insensitively; this compares two
129 bool operator< (const cooked_index_entry
&other
) const
131 return strcasecmp (canonical
, other
.canonical
) < 0;
134 /* The name as it appears in DWARF. This always points into one of
135 the mapped DWARF sections. Note that this may be the name or the
136 linkage name -- two entries are created for DIEs which have both
139 /* The canonical name. For C++ names, this may differ from NAME.
140 In all other cases, this is equal to NAME. */
141 const char *canonical
= nullptr;
144 /* Any flags attached to this entry. */
145 cooked_index_flag flags
;
146 /* The offset of this DIE. */
147 sect_offset die_offset
;
148 /* The parent entry. This is NULL for top-level entries.
149 Otherwise, it points to the parent entry, such as a namespace or
151 const cooked_index_entry
*parent_entry
;
152 /* The CU from which this entry originates. */
153 dwarf2_per_cu_data
*per_cu
;
157 void write_scope (struct obstack
*storage
, const char *sep
) const;
160 class cooked_index_vector
;
162 /* An index of interesting DIEs. This is "cooked", in contrast to a
163 mapped .debug_names or .gdb_index, which are "raw". An entry in
164 the index is of type cooked_index_entry.
166 Operations on the index are described below. They are chosen to
167 make it relatively simple to implement the symtab "quick"
172 cooked_index () = default;
173 DISABLE_COPY_AND_ASSIGN (cooked_index
);
175 /* Create a new cooked_index_entry and register it with this object.
176 Entries are owned by this object. The new item is returned. */
177 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
178 cooked_index_flag flags
,
180 const cooked_index_entry
*parent_entry
,
181 dwarf2_per_cu_data
*per_cu
);
183 /* Install a new fixed addrmap from the given mutable addrmap. */
184 void install_addrmap (addrmap
*map
)
186 gdb_assert (m_addrmap
== nullptr);
187 m_addrmap
= addrmap_create_fixed (map
, &m_storage
);
190 friend class cooked_index_vector
;
194 /* Return the entry that is believed to represent the program's
195 "main". This will return NULL if no such entry is available. */
196 const cooked_index_entry
*get_main () const
201 /* Look up ADDR in the address map, and return either the
202 corresponding CU, or nullptr if the address could not be
204 dwarf2_per_cu_data
*lookup (CORE_ADDR addr
)
206 return (dwarf2_per_cu_data
*) addrmap_find (m_addrmap
, addr
);
209 /* Create a new cooked_index_entry and register it with this object.
210 Entries are owned by this object. The new item is returned. */
211 cooked_index_entry
*create (sect_offset die_offset
,
213 cooked_index_flag flags
,
215 const cooked_index_entry
*parent_entry
,
216 dwarf2_per_cu_data
*per_cu
)
218 return new (&m_storage
) cooked_index_entry (die_offset
, tag
, flags
,
223 /* Storage for the entries. */
224 auto_obstack m_storage
;
225 /* List of all entries. */
226 std::vector
<cooked_index_entry
*> m_entries
;
227 /* If we found "main" or an entry with 'is_main' set, store it
229 cooked_index_entry
*m_main
= nullptr;
230 /* When constructing the index, entries are stored on a linked list.
231 This member points to the head of that list. Later, they are
232 entered into the hash table, at which point this is no longer
234 cooked_index_entry
*m_start
= nullptr;
235 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
237 addrmap
*m_addrmap
= nullptr;
240 /* The main index of DIEs. The parallel DIE indexers create
241 cooked_index objects. Then, these are all handled to a
242 cooked_index_vector for storage and final indexing. The index is
243 made by iterating over the entries previously created. */
245 class cooked_index_vector
: public dwarf_scanner_base
249 /* A convenience typedef for the vector that is contained in this
251 typedef std::vector
<std::unique_ptr
<cooked_index
>> vec_type
;
253 explicit cooked_index_vector (vec_type
&&vec
);
254 DISABLE_COPY_AND_ASSIGN (cooked_index_vector
);
256 ~cooked_index_vector ()
258 /* The 'finalize' method may be run in a different thread. If
259 this object is destroyed before this completes, then the method
260 will end up writing to freed memory. Waiting for this to
261 complete avoids this problem; and the cost seems ignorable
262 because creating and immediately destroying the debug info is a
263 relatively rare thing to do. */
267 /* A simple range over part of m_entries. */
268 typedef iterator_range
<std::vector
<cooked_index_entry
*>::iterator
> range
;
270 /* Look up an entry by name. Returns a range of all matching
271 results. If COMPLETING is true, then a larger range, suitable
272 for completion, will be returned. */
273 range
find (gdb::string_view name
, bool completing
);
275 /* Return a range of all the entries. */
279 return { m_entries
.begin (), m_entries
.end () };
282 /* Look up ADDR in the address map, and return either the
283 corresponding CU, or nullptr if the address could not be
285 dwarf2_per_cu_data
*lookup (CORE_ADDR addr
);
287 /* Return a new vector of all the addrmaps used by all the indexes
288 held by this object. */
289 std::vector
<addrmap
*> get_addrmaps ();
291 /* Return the entry that is believed to represent the program's
292 "main". This will return NULL if no such entry is available. */
293 const cooked_index_entry
*get_main () const;
295 quick_symbol_functions_up
make_quick_functions () const override
;
299 /* GNAT only emits mangled ("encoded") names in the DWARF, and does
300 not emit the module structure. However, we need this structure
301 to do lookups. This function recreates that structure for an
302 existing entry. It returns the base name (last element) of the
303 full decoded name. */
304 gdb::unique_xmalloc_ptr
<char> handle_gnat_encoded_entry
305 (cooked_index_entry
*entry
, htab_t gnat_entries
);
307 /* Finalize the index. This should be called a single time, when
308 the index has been fully populated. It enters all the entries
309 into the internal hash table. */
312 /* The vector of cooked_index objects. This is stored because the
313 entries are stored on the obstacks in those objects. */
316 /* List of all entries. This is sorted during finalization. */
317 std::vector
<cooked_index_entry
*> m_entries
;
319 /* Storage for canonical names. */
320 std::vector
<gdb::unique_xmalloc_ptr
<char>> m_names
;
322 /* A future that tracks when the 'finalize' method is done. Note
323 that the 'get' method is never called on this future, only
325 std::future
<void> m_future
;
328 #endif /* GDB_DWARF2_COOKED_INDEX_H */