]> Git Repo - binutils.git/blob - gdb/dwarf2/cooked-index.h
Automatic date update in version.in
[binutils.git] / gdb / dwarf2 / cooked-index.h
1 /* DIE indexing 
2
3    Copyright (C) 2022 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #ifndef GDB_DWARF2_COOKED_INDEX_H
21 #define GDB_DWARF2_COOKED_INDEX_H
22
23 #include "dwarf2.h"
24 #include "gdbtypes.h"
25 #include "symtab.h"
26 #include "hashtab.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"
31 #include "addrmap.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"
37
38 struct dwarf2_per_cu_data;
39
40 /* Flags that describe an entry in the index.  */
41 enum cooked_index_flag_enum : unsigned char
42 {
43   /* True if this entry is the program's "main".  */
44   IS_MAIN = 1,
45   /* True if this entry represents a "static" object.  */
46   IS_STATIC = 2,
47   /* True if this entry is an "enum class".  */
48   IS_ENUM_CLASS = 4,
49   /* True if this entry uses the linkage name.  */
50   IS_LINKAGE = 8,
51   /* True if this entry is just for the declaration of a type, not the
52      definition.  */
53   IS_TYPE_DECLARATION = 16,
54 };
55 DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag);
56
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.
60
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
63    and processed.  */
64 struct cooked_index_entry : public allocate_on_obstack
65 {
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_)
70     : name (name_),
71       tag (tag_),
72       flags (flags_),
73       die_offset (die_offset_),
74       parent_entry (parent_entry_),
75       per_cu (per_cu_)
76   {
77   }
78
79   /* Return true if this entry matches SEARCH_FLAGS.  */
80   bool matches (block_search_flags search_flags) const
81   {
82     /* Just reject type declarations.  */
83     if ((flags & IS_TYPE_DECLARATION) != 0)
84       return false;
85
86     if ((search_flags & SEARCH_STATIC_BLOCK) != 0
87         && (flags & IS_STATIC) != 0)
88       return true;
89     if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0
90         && (flags & IS_STATIC) == 0)
91       return true;
92     return false;
93   }
94
95   /* Return true if this entry matches DOMAIN.  */
96   bool matches (domain_enum domain) const
97   {
98     /* Just reject type declarations.  */
99     if ((flags & IS_TYPE_DECLARATION) != 0)
100       return false;
101
102     switch (domain)
103       {
104       case LABEL_DOMAIN:
105         return false;
106
107       case MODULE_DOMAIN:
108         return tag == DW_TAG_module;
109
110       case COMMON_BLOCK_DOMAIN:
111         return tag == DW_TAG_common_block;
112       }
113
114     return true;
115   }
116
117   /* Return true if this entry matches KIND.  */
118   bool matches (enum search_domain kind) const
119   {
120     /* Just reject type declarations.  */
121     if ((flags & IS_TYPE_DECLARATION) != 0)
122       return false;
123
124     switch (kind)
125       {
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;
132       case TYPES_DOMAIN:
133         return tag_is_type (tag);
134       case MODULES_DOMAIN:
135         return tag == DW_TAG_module;
136       }
137
138     return true;
139   }
140
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
143      STORAGE.  */
144   const char *full_name (struct obstack *storage) const;
145
146   /* Entries must be sorted case-insensitively; this compares two
147      entries.  */
148   bool operator< (const cooked_index_entry &other) const
149   {
150     return strcasecmp (canonical, other.canonical) < 0;
151   }
152
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
156      attributes.  */
157   const char *name;
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;
161   /* The DWARF tag.  */
162   enum dwarf_tag tag;
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
169      class.  */
170   const cooked_index_entry *parent_entry;
171   /* The CU from which this entry originates.  */
172   dwarf2_per_cu_data *per_cu;
173
174 private:
175
176   void write_scope (struct obstack *storage, const char *sep) const;
177 };
178
179 class cooked_index_vector;
180
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.
184
185    Operations on the index are described below.  They are chosen to
186    make it relatively simple to implement the symtab "quick"
187    methods.  */
188 class cooked_index
189 {
190 public:
191   cooked_index () = default;
192   DISABLE_COPY_AND_ASSIGN (cooked_index);
193
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,
198                                  const char *name,
199                                  const cooked_index_entry *parent_entry,
200                                  dwarf2_per_cu_data *per_cu);
201
202   /* Install a new fixed addrmap from the given mutable addrmap.  */
203   void install_addrmap (addrmap_mutable *map)
204   {
205     gdb_assert (m_addrmap == nullptr);
206     m_addrmap = new (&m_storage) addrmap_fixed (&m_storage, map);
207   }
208
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.  */
212   void finalize ();
213
214   /* Wait for this index's finalization to be complete.  */
215   void wait ()
216   {
217     m_future.wait ();
218   }
219
220   friend class cooked_index_vector;
221
222   /* A simple range over part of m_entries.  */
223   typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range;
224
225   /* Return a range of all the entries.  */
226   range all_entries ()
227   {
228     wait ();
229     return { m_entries.begin (), m_entries.end () };
230   }
231
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);
236
237 private:
238
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
242   {
243     return m_main;
244   }
245
246   /* Look up ADDR in the address map, and return either the
247      corresponding CU, or nullptr if the address could not be
248      found.  */
249   dwarf2_per_cu_data *lookup (CORE_ADDR addr)
250   {
251     return (dwarf2_per_cu_data *) m_addrmap->find (addr);
252   }
253
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,
257                               enum dwarf_tag tag,
258                               cooked_index_flag flags,
259                               const char *name,
260                               const cooked_index_entry *parent_entry,
261                               dwarf2_per_cu_data *per_cu)
262   {
263     return new (&m_storage) cooked_index_entry (die_offset, tag, flags,
264                                                 name, parent_entry,
265                                                 per_cu);
266   }
267
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);
275
276   /* A helper method that does the work of 'finalize'.  */
277   void do_finalize ();
278
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
284      here.  */
285   cooked_index_entry *m_main = nullptr;
286   /* The addrmap.  This maps address ranges to dwarf2_per_cu_data
287      objects.  */
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
293      'wait'.  */
294   gdb::future<void> m_future;
295 };
296
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.  */
301
302 class cooked_index_vector : public dwarf_scanner_base
303 {
304 public:
305
306   /* A convenience typedef for the vector that is contained in this
307      object.  */
308   typedef std::vector<std::unique_ptr<cooked_index>> vec_type;
309
310   explicit cooked_index_vector (vec_type &&vec);
311   DISABLE_COPY_AND_ASSIGN (cooked_index_vector);
312
313   /* Wait until the finalization of the entire cooked_index_vector is
314      done.  */
315   void wait ()
316   {
317     for (auto &item : m_vector)
318       item->wait ();
319   }
320
321   ~cooked_index_vector ()
322   {
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.  */
329     wait ();
330   }
331
332   /* A range over a vector of subranges.  */
333   typedef range_chain<cooked_index::range> range;
334
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);
339
340   /* Return a range of all the entries.  */
341   range all_entries ()
342   {
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));
348   }
349
350   /* Look up ADDR in the address map, and return either the
351      corresponding CU, or nullptr if the address could not be
352      found.  */
353   dwarf2_per_cu_data *lookup (CORE_ADDR addr);
354
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 ();
358
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;
362
363   cooked_index_vector *index_for_writing () override
364   {
365     return this;
366   }
367
368   quick_symbol_functions_up make_quick_functions () const override;
369
370 private:
371
372   /* The vector of cooked_index objects.  This is stored because the
373      entries are stored on the obstacks in those objects.  */
374   vec_type m_vector;
375 };
376
377 #endif /* GDB_DWARF2_COOKED_INDEX_H */
This page took 0.043755 seconds and 4 git commands to generate.