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"
34 struct dwarf2_per_cu_data
;
36 /* Flags that describe an entry in the index. */
37 enum cooked_index_flag_enum
: unsigned char
39 /* True if this entry is the program's "main". */
41 /* True if this entry represents a "static" object. */
43 /* True if this entry is an "enum class". */
45 /* True if this entry uses the linkage name. */
48 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum
, cooked_index_flag
);
50 /* A cooked_index_entry represents a single item in the index. Note
51 that two entries can be created for the same DIE -- one using the
52 name, and another one using the linkage name, if any.
54 This is an "open" class and the members are all directly
55 accessible. It is read-only after the index has been fully read
57 struct cooked_index_entry
: public allocate_on_obstack
59 cooked_index_entry (sect_offset die_offset_
, enum dwarf_tag tag_
,
60 cooked_index_flag flags_
, const char *name_
,
61 const cooked_index_entry
*parent_entry_
,
62 dwarf2_per_cu_data
*per_cu_
)
66 die_offset (die_offset_
),
67 parent_entry (parent_entry_
),
72 /* Return true if this entry matches SEARCH_FLAGS. */
73 bool matches (block_search_flags search_flags
) const
75 if ((search_flags
& SEARCH_STATIC_BLOCK
) != 0
76 && (flags
& IS_STATIC
) != 0)
78 if ((search_flags
& SEARCH_GLOBAL_BLOCK
) != 0
79 && (flags
& IS_STATIC
) == 0)
84 /* Return true if this entry matches DOMAIN. */
85 bool matches (domain_enum domain
) const
93 return tag
== DW_TAG_module
;
95 case COMMON_BLOCK_DOMAIN
:
96 return tag
== DW_TAG_common_block
;
102 /* Return true if this entry matches KIND. */
103 bool matches (enum search_domain kind
) const
107 case VARIABLES_DOMAIN
:
108 return tag
== DW_TAG_variable
;
109 case FUNCTIONS_DOMAIN
:
110 return tag
== DW_TAG_subprogram
;
112 return tag
== DW_TAG_typedef
|| tag
== DW_TAG_structure_type
;
114 return tag
== DW_TAG_module
;
120 /* Construct the fully-qualified name of this entry and return a
121 pointer to it. If allocation is needed, it will be done on
123 const char *full_name (struct obstack
*storage
) const;
125 /* Entries must be sorted case-insensitively; this compares two
127 bool operator< (const cooked_index_entry
&other
) const
129 return strcasecmp (canonical
, other
.canonical
) < 0;
132 /* The name as it appears in DWARF. This always points into one of
133 the mapped DWARF sections. Note that this may be the name or the
134 linkage name -- two entries are created for DIEs which have both
137 /* The canonical name. For C++ names, this may differ from NAME.
138 In all other cases, this is equal to NAME. */
139 const char *canonical
= nullptr;
142 /* Any flags attached to this entry. */
143 cooked_index_flag flags
;
144 /* The offset of this DIE. */
145 sect_offset die_offset
;
146 /* The parent entry. This is NULL for top-level entries.
147 Otherwise, it points to the parent entry, such as a namespace or
149 const cooked_index_entry
*parent_entry
;
150 /* The CU from which this entry originates. */
151 dwarf2_per_cu_data
*per_cu
;
155 void write_scope (struct obstack
*storage
, const char *sep
) const;
158 /* An index of interesting DIEs. This is "cooked", in contrast to a
159 mapped .debug_names or .gdb_index, which are "raw". An entry in
160 the index is of type cooked_index_entry.
162 Operations on the index are described below. They are chosen to
163 make it relatively simple to implement the symtab "quick"
168 cooked_index () = default;
169 explicit cooked_index (cooked_index
&&other
) = default;
170 DISABLE_COPY_AND_ASSIGN (cooked_index
);
171 cooked_index
&operator= (cooked_index
&&other
) = default;
173 /* Create a new cooked_index_entry and register it with this object.
174 Entries are owned by this object. The new item is returned. */
175 const cooked_index_entry
*add (sect_offset die_offset
, enum dwarf_tag tag
,
176 cooked_index_flag flags
,
178 const cooked_index_entry
*parent_entry
,
179 dwarf2_per_cu_data
*per_cu
);
181 /* Return the entry that is believed to represent the program's
182 "main". This will return NULL if no such entry is available. */
183 const cooked_index_entry
*get_main () const
188 /* Install a new fixed addrmap from the given mutable addrmap. */
189 void install_addrmap (addrmap
*map
)
191 gdb_assert (m_addrmap
== nullptr);
192 m_addrmap
= addrmap_create_fixed (map
, &m_storage
);
195 /* Look up ADDR in the address map, and return either the
196 corresponding CU, or nullptr if the address could not be
198 dwarf2_per_cu_data
*lookup (CORE_ADDR addr
)
200 return (dwarf2_per_cu_data
*) addrmap_find (m_addrmap
, addr
);
203 /* Finalize the index. This should be called a single time, when
204 the index has been fully populated. It enters all the entries
205 into the internal hash table. */
208 /* A simple range over part of m_entries. */
209 typedef iterator_range
<std::vector
<cooked_index_entry
*>::iterator
> range
;
211 /* Look up an entry by name. Returns a range of all matching
212 results. If COMPLETING is true, then a larger range, suitable
213 for completion, will be returned. */
214 range
find (gdb::string_view name
, bool completing
);
216 /* Return a range of all the entries. */
219 return { m_entries
.begin (), m_entries
.end () };
224 /* GNAT only emits mangled ("encoded") names in the DWARF, and does
225 not emit the module structure. However, we need this structure
226 to do lookups. This function recreates that structure for an
227 existing entry. It returns the base name (last element) of the
228 full decoded name. */
229 gdb::unique_xmalloc_ptr
<char> handle_gnat_encoded_entry
230 (cooked_index_entry
*entry
, htab_t gnat_entries
);
232 /* Create a new cooked_index_entry and register it with this object.
233 Entries are owned by this object. The new item is returned. */
234 cooked_index_entry
*create (sect_offset die_offset
,
236 cooked_index_flag flags
,
238 const cooked_index_entry
*parent_entry
,
239 dwarf2_per_cu_data
*per_cu
)
241 return new (&m_storage
) cooked_index_entry (die_offset
, tag
, flags
,
246 /* Storage for the entries. */
247 auto_obstack m_storage
;
248 /* List of all entries. */
249 std::vector
<cooked_index_entry
*> m_entries
;
250 /* If we found "main" or an entry with 'is_main' set, store it
252 cooked_index_entry
*m_main
= nullptr;
253 /* Storage for canonical names. */
254 std::vector
<gdb::unique_xmalloc_ptr
<char>> m_names
;
255 /* The addrmap. This maps address ranges to dwarf2_per_cu_data
257 addrmap
*m_addrmap
= nullptr;
260 #endif /* GDB_DWARF2_COOKED_INDEX_H */