1 1.1 christos /* DIE indexing 2 1.1 christos 3 1.1.1.2 christos Copyright (C) 2022-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.1 christos #ifndef GDB_DWARF2_COOKED_INDEX_H 21 1.1 christos #define GDB_DWARF2_COOKED_INDEX_H 22 1.1 christos 23 1.1 christos #include "dwarf2.h" 24 1.1.1.2 christos #include "dwarf2/types.h" 25 1.1 christos #include "symtab.h" 26 1.1 christos #include "hashtab.h" 27 1.1 christos #include "quick-symbol.h" 28 1.1 christos #include "gdbsupport/gdb_obstack.h" 29 1.1 christos #include "addrmap.h" 30 1.1 christos #include "gdbsupport/iterator-range.h" 31 1.1 christos #include "dwarf2/mapped-index.h" 32 1.1.1.2 christos #include "dwarf2/read.h" 33 1.1.1.3 christos #include "dwarf2/abbrev-table-cache.h" 34 1.1.1.2 christos #include "dwarf2/parent-map.h" 35 1.1 christos #include "gdbsupport/range-chain.h" 36 1.1.1.2 christos #include "complaints.h" 37 1.1.1.2 christos 38 1.1.1.2 christos #if CXX_STD_THREAD 39 1.1.1.2 christos #include <mutex> 40 1.1.1.2 christos #include <condition_variable> 41 1.1.1.2 christos #endif /* CXX_STD_THREAD */ 42 1.1 christos 43 1.1 christos struct dwarf2_per_cu_data; 44 1.1.1.2 christos struct dwarf2_per_bfd; 45 1.1.1.2 christos struct index_cache_store_context; 46 1.1.1.2 christos struct cooked_index_entry; 47 1.1 christos 48 1.1 christos /* Flags that describe an entry in the index. */ 49 1.1 christos enum cooked_index_flag_enum : unsigned char 50 1.1 christos { 51 1.1 christos /* True if this entry is the program's "main". */ 52 1.1 christos IS_MAIN = 1, 53 1.1 christos /* True if this entry represents a "static" object. */ 54 1.1 christos IS_STATIC = 2, 55 1.1 christos /* True if this entry uses the linkage name. */ 56 1.1.1.2 christos IS_LINKAGE = 4, 57 1.1 christos /* True if this entry is just for the declaration of a type, not the 58 1.1 christos definition. */ 59 1.1.1.2 christos IS_TYPE_DECLARATION = 8, 60 1.1.1.2 christos /* True is parent_entry.deferred has a value rather than parent_entry 61 1.1.1.2 christos .resolved. */ 62 1.1.1.2 christos IS_PARENT_DEFERRED = 16, 63 1.1 christos }; 64 1.1 christos DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag); 65 1.1 christos 66 1.1.1.2 christos /* Type representing either a resolved or deferred cooked_index_entry. */ 67 1.1.1.2 christos 68 1.1.1.2 christos union cooked_index_entry_ref 69 1.1.1.2 christos { 70 1.1.1.2 christos cooked_index_entry_ref (parent_map::addr_type deferred_) 71 1.1.1.2 christos { 72 1.1.1.2 christos deferred = deferred_; 73 1.1.1.2 christos } 74 1.1.1.2 christos 75 1.1.1.2 christos cooked_index_entry_ref (const cooked_index_entry *resolved_) 76 1.1.1.2 christos { 77 1.1.1.2 christos resolved = resolved_; 78 1.1.1.2 christos } 79 1.1.1.2 christos 80 1.1.1.2 christos const cooked_index_entry *resolved; 81 1.1.1.2 christos parent_map::addr_type deferred; 82 1.1.1.2 christos }; 83 1.1.1.2 christos 84 1.1.1.2 christos /* Return a string representation of FLAGS. */ 85 1.1.1.2 christos 86 1.1.1.2 christos std::string to_string (cooked_index_flag flags); 87 1.1.1.2 christos 88 1.1 christos /* Return true if LANG requires canonicalization. This is used 89 1.1 christos primarily to work around an issue computing the name of "main". 90 1.1 christos This function must be kept in sync with 91 1.1.1.2 christos cooked_index_shard::finalize. */ 92 1.1 christos 93 1.1 christos extern bool language_requires_canonicalization (enum language lang); 94 1.1 christos 95 1.1 christos /* A cooked_index_entry represents a single item in the index. Note 96 1.1 christos that two entries can be created for the same DIE -- one using the 97 1.1 christos name, and another one using the linkage name, if any. 98 1.1 christos 99 1.1 christos This is an "open" class and the members are all directly 100 1.1 christos accessible. It is read-only after the index has been fully read 101 1.1 christos and processed. */ 102 1.1.1.2 christos struct cooked_index_entry : public allocate_on_obstack<cooked_index_entry> 103 1.1 christos { 104 1.1 christos cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_, 105 1.1.1.2 christos cooked_index_flag flags_, 106 1.1.1.2 christos enum language lang_, const char *name_, 107 1.1.1.2 christos cooked_index_entry_ref parent_entry_, 108 1.1 christos dwarf2_per_cu_data *per_cu_) 109 1.1 christos : name (name_), 110 1.1 christos tag (tag_), 111 1.1 christos flags (flags_), 112 1.1.1.2 christos lang (lang_), 113 1.1 christos die_offset (die_offset_), 114 1.1.1.2 christos per_cu (per_cu_), 115 1.1.1.2 christos m_parent_entry (parent_entry_) 116 1.1 christos { 117 1.1 christos } 118 1.1 christos 119 1.1 christos /* Return true if this entry matches SEARCH_FLAGS. */ 120 1.1 christos bool matches (block_search_flags search_flags) const 121 1.1 christos { 122 1.1 christos /* Just reject type declarations. */ 123 1.1 christos if ((flags & IS_TYPE_DECLARATION) != 0) 124 1.1 christos return false; 125 1.1 christos 126 1.1 christos if ((search_flags & SEARCH_STATIC_BLOCK) != 0 127 1.1 christos && (flags & IS_STATIC) != 0) 128 1.1 christos return true; 129 1.1 christos if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0 130 1.1 christos && (flags & IS_STATIC) == 0) 131 1.1 christos return true; 132 1.1 christos return false; 133 1.1 christos } 134 1.1 christos 135 1.1 christos /* Return true if this entry matches KIND. */ 136 1.1.1.2 christos bool matches (domain_search_flags kind) const; 137 1.1 christos 138 1.1 christos /* Construct the fully-qualified name of this entry and return a 139 1.1 christos pointer to it. If allocation is needed, it will be done on 140 1.1 christos STORAGE. FOR_MAIN is true if we are computing the name of the 141 1.1 christos "main" entry -- one marked DW_AT_main_subprogram. This matters 142 1.1 christos for avoiding name canonicalization and also a related race (if 143 1.1.1.3 christos "main" computation is done during finalization). If the language 144 1.1.1.3 christos doesn't prescribe a separator, one can be specified using 145 1.1.1.3 christos DEFAULT_SEP. */ 146 1.1.1.3 christos const char *full_name (struct obstack *storage, bool for_main = false, 147 1.1.1.3 christos const char *default_sep = nullptr) const; 148 1.1 christos 149 1.1 christos /* Comparison modes for the 'compare' function. See the function 150 1.1 christos for a description. */ 151 1.1 christos enum comparison_mode 152 1.1 christos { 153 1.1 christos MATCH, 154 1.1 christos SORT, 155 1.1 christos COMPLETE, 156 1.1 christos }; 157 1.1 christos 158 1.1 christos /* Compare two strings, case-insensitively. Return -1 if STRA is 159 1.1 christos less than STRB, 0 if they are equal, and 1 if STRA is greater. 160 1.1 christos 161 1.1 christos When comparing, '<' is considered to be less than all other 162 1.1 christos printable characters. This ensures that "t<x>" sorts before 163 1.1 christos "t1", which is necessary when looking up "t". This '<' handling 164 1.1 christos is to ensure that certain C++ lookups work correctly. It is 165 1.1 christos inexact, and applied regardless of the search language, but this 166 1.1 christos is ok because callers of this code do more precise filtering 167 1.1 christos according to their needs. This is also why using a 168 1.1 christos case-insensitive comparison works even for languages that are 169 1.1 christos case sensitive. 170 1.1 christos 171 1.1 christos MODE controls how the comparison proceeds. 172 1.1 christos 173 1.1 christos MODE==SORT is used when sorting and the only special '<' handling 174 1.1 christos that it does is to ensure that '<' sorts before all other 175 1.1 christos printable characters. This ensures that the resulting ordering 176 1.1 christos will be binary-searchable. 177 1.1 christos 178 1.1 christos MODE==MATCH is used when searching for a symbol. In this case, 179 1.1 christos STRB must always be the search name, and STRA must be the name in 180 1.1 christos the index that is under consideration. In compare mode, early 181 1.1 christos termination of STRB may match STRA -- for example, "t<int>" and 182 1.1 christos "t" will be considered to be equal. (However, if A=="t" and 183 1.1 christos B=="t<int>", then this will not consider them as equal.) 184 1.1 christos 185 1.1 christos MODE==COMPLETE is used when searching for a symbol for 186 1.1 christos completion. In this case, STRB must always be the search name, 187 1.1 christos and STRA must be the name in the index that is under 188 1.1 christos consideration. In completion mode, early termination of STRB 189 1.1 christos always results in a match. */ 190 1.1 christos static int compare (const char *stra, const char *strb, 191 1.1 christos comparison_mode mode); 192 1.1 christos 193 1.1 christos /* Compare two entries by canonical name. */ 194 1.1 christos bool operator< (const cooked_index_entry &other) const 195 1.1 christos { 196 1.1 christos return compare (canonical, other.canonical, SORT) < 0; 197 1.1 christos } 198 1.1 christos 199 1.1.1.2 christos /* Set parent entry to PARENT. */ 200 1.1.1.2 christos void set_parent (const cooked_index_entry *parent) 201 1.1.1.2 christos { 202 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) == 0); 203 1.1.1.2 christos m_parent_entry.resolved = parent; 204 1.1.1.2 christos } 205 1.1.1.2 christos 206 1.1.1.2 christos /* Resolve deferred parent entry to PARENT. */ 207 1.1.1.2 christos void resolve_parent (const cooked_index_entry *parent) 208 1.1.1.2 christos { 209 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) != 0); 210 1.1.1.2 christos flags = flags & ~IS_PARENT_DEFERRED; 211 1.1.1.2 christos m_parent_entry.resolved = parent; 212 1.1.1.2 christos } 213 1.1.1.2 christos 214 1.1.1.2 christos /* Return parent entry. */ 215 1.1.1.2 christos const cooked_index_entry *get_parent () const 216 1.1.1.2 christos { 217 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) == 0); 218 1.1.1.2 christos return m_parent_entry.resolved; 219 1.1.1.2 christos } 220 1.1.1.2 christos 221 1.1.1.2 christos /* Return deferred parent entry. */ 222 1.1.1.2 christos parent_map::addr_type get_deferred_parent () const 223 1.1.1.2 christos { 224 1.1.1.2 christos gdb_assert ((flags & IS_PARENT_DEFERRED) != 0); 225 1.1.1.2 christos return m_parent_entry.deferred; 226 1.1.1.2 christos } 227 1.1.1.2 christos 228 1.1 christos /* The name as it appears in DWARF. This always points into one of 229 1.1 christos the mapped DWARF sections. Note that this may be the name or the 230 1.1 christos linkage name -- two entries are created for DIEs which have both 231 1.1 christos attributes. */ 232 1.1 christos const char *name; 233 1.1.1.3 christos /* The canonical name. This may be equal to NAME. */ 234 1.1 christos const char *canonical = nullptr; 235 1.1 christos /* The DWARF tag. */ 236 1.1 christos enum dwarf_tag tag; 237 1.1 christos /* Any flags attached to this entry. */ 238 1.1 christos cooked_index_flag flags; 239 1.1.1.2 christos /* The language of this symbol. */ 240 1.1.1.2 christos ENUM_BITFIELD (language) lang : LANGUAGE_BITS; 241 1.1 christos /* The offset of this DIE. */ 242 1.1 christos sect_offset die_offset; 243 1.1 christos /* The CU from which this entry originates. */ 244 1.1 christos dwarf2_per_cu_data *per_cu; 245 1.1 christos 246 1.1 christos private: 247 1.1 christos 248 1.1 christos /* A helper method for full_name. Emits the full scope of this 249 1.1 christos object, followed by the separator, to STORAGE. If this entry has 250 1.1.1.3 christos a parent, its write_scope method is called first. FOR_MAIN is 251 1.1.1.3 christos true when computing the name of 'main'; see full_name. */ 252 1.1 christos void write_scope (struct obstack *storage, const char *sep, 253 1.1.1.3 christos bool for_main) const; 254 1.1.1.2 christos 255 1.1.1.2 christos /* The parent entry. This is NULL for top-level entries. 256 1.1.1.2 christos Otherwise, it points to the parent entry, such as a namespace or 257 1.1.1.2 christos class. */ 258 1.1.1.2 christos cooked_index_entry_ref m_parent_entry; 259 1.1 christos }; 260 1.1 christos 261 1.1.1.2 christos class cooked_index; 262 1.1 christos 263 1.1 christos /* An index of interesting DIEs. This is "cooked", in contrast to a 264 1.1 christos mapped .debug_names or .gdb_index, which are "raw". An entry in 265 1.1 christos the index is of type cooked_index_entry. 266 1.1 christos 267 1.1 christos Operations on the index are described below. They are chosen to 268 1.1 christos make it relatively simple to implement the symtab "quick" 269 1.1 christos methods. */ 270 1.1.1.2 christos class cooked_index_shard 271 1.1 christos { 272 1.1 christos public: 273 1.1.1.2 christos cooked_index_shard () = default; 274 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_shard); 275 1.1 christos 276 1.1 christos /* Create a new cooked_index_entry and register it with this object. 277 1.1 christos Entries are owned by this object. The new item is returned. */ 278 1.1.1.2 christos cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag, 279 1.1.1.2 christos cooked_index_flag flags, enum language lang, 280 1.1.1.2 christos const char *name, 281 1.1.1.2 christos cooked_index_entry_ref parent_entry, 282 1.1.1.2 christos dwarf2_per_cu_data *per_cu); 283 1.1 christos 284 1.1 christos /* Install a new fixed addrmap from the given mutable addrmap. */ 285 1.1 christos void install_addrmap (addrmap_mutable *map) 286 1.1 christos { 287 1.1 christos gdb_assert (m_addrmap == nullptr); 288 1.1 christos m_addrmap = new (&m_storage) addrmap_fixed (&m_storage, map); 289 1.1 christos } 290 1.1 christos 291 1.1.1.2 christos friend class cooked_index; 292 1.1 christos 293 1.1 christos /* A simple range over part of m_entries. */ 294 1.1.1.2 christos typedef iterator_range<std::vector<cooked_index_entry *>::const_iterator> 295 1.1.1.2 christos range; 296 1.1 christos 297 1.1 christos /* Return a range of all the entries. */ 298 1.1.1.2 christos range all_entries () const 299 1.1 christos { 300 1.1.1.2 christos return { m_entries.cbegin (), m_entries.cend () }; 301 1.1 christos } 302 1.1 christos 303 1.1 christos /* Look up an entry by name. Returns a range of all matching 304 1.1 christos results. If COMPLETING is true, then a larger range, suitable 305 1.1 christos for completion, will be returned. */ 306 1.1.1.2 christos range find (const std::string &name, bool completing) const; 307 1.1 christos 308 1.1 christos private: 309 1.1 christos 310 1.1 christos /* Return the entry that is believed to represent the program's 311 1.1 christos "main". This will return NULL if no such entry is available. */ 312 1.1 christos const cooked_index_entry *get_main () const 313 1.1 christos { 314 1.1 christos return m_main; 315 1.1 christos } 316 1.1 christos 317 1.1 christos /* Look up ADDR in the address map, and return either the 318 1.1 christos corresponding CU, or nullptr if the address could not be 319 1.1 christos found. */ 320 1.1.1.2 christos dwarf2_per_cu_data *lookup (unrelocated_addr addr) 321 1.1 christos { 322 1.1.1.2 christos return (static_cast<dwarf2_per_cu_data *> 323 1.1.1.2 christos (m_addrmap->find ((CORE_ADDR) addr))); 324 1.1 christos } 325 1.1 christos 326 1.1 christos /* Create a new cooked_index_entry and register it with this object. 327 1.1 christos Entries are owned by this object. The new item is returned. */ 328 1.1 christos cooked_index_entry *create (sect_offset die_offset, 329 1.1 christos enum dwarf_tag tag, 330 1.1 christos cooked_index_flag flags, 331 1.1.1.2 christos enum language lang, 332 1.1 christos const char *name, 333 1.1.1.2 christos cooked_index_entry_ref parent_entry, 334 1.1 christos dwarf2_per_cu_data *per_cu) 335 1.1 christos { 336 1.1 christos return new (&m_storage) cooked_index_entry (die_offset, tag, flags, 337 1.1.1.2 christos lang, name, parent_entry, 338 1.1 christos per_cu); 339 1.1 christos } 340 1.1 christos 341 1.1 christos /* GNAT only emits mangled ("encoded") names in the DWARF, and does 342 1.1 christos not emit the module structure. However, we need this structure 343 1.1 christos to do lookups. This function recreates that structure for an 344 1.1.1.3 christos existing entry, modifying ENTRY as appropriate. */ 345 1.1.1.3 christos void handle_gnat_encoded_entry 346 1.1 christos (cooked_index_entry *entry, htab_t gnat_entries); 347 1.1 christos 348 1.1.1.2 christos /* Finalize the index. This should be called a single time, when 349 1.1.1.2 christos the index has been fully populated. It enters all the entries 350 1.1.1.2 christos into the internal table and fixes up all missing parent links. 351 1.1.1.2 christos This may be invoked in a worker thread. */ 352 1.1.1.2 christos void finalize (const parent_map_map *parent_maps); 353 1.1 christos 354 1.1 christos /* Storage for the entries. */ 355 1.1 christos auto_obstack m_storage; 356 1.1 christos /* List of all entries. */ 357 1.1 christos std::vector<cooked_index_entry *> m_entries; 358 1.1 christos /* If we found an entry with 'is_main' set, store it here. */ 359 1.1 christos cooked_index_entry *m_main = nullptr; 360 1.1 christos /* The addrmap. This maps address ranges to dwarf2_per_cu_data 361 1.1 christos objects. */ 362 1.1.1.2 christos addrmap_fixed *m_addrmap = nullptr; 363 1.1 christos /* Storage for canonical names. */ 364 1.1 christos std::vector<gdb::unique_xmalloc_ptr<char>> m_names; 365 1.1 christos }; 366 1.1 christos 367 1.1.1.2 christos class cutu_reader; 368 1.1.1.2 christos 369 1.1.1.2 christos /* An instance of this is created when scanning DWARF to create a 370 1.1.1.2 christos cooked index. */ 371 1.1 christos 372 1.1.1.2 christos class cooked_index_storage 373 1.1 christos { 374 1.1 christos public: 375 1.1 christos 376 1.1.1.2 christos cooked_index_storage (); 377 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_storage); 378 1.1.1.2 christos 379 1.1.1.3 christos /* Return the current abbrev table_cache. */ 380 1.1.1.3 christos const abbrev_table_cache &get_abbrev_table_cache () const 381 1.1.1.3 christos { return m_abbrev_table_cache; } 382 1.1.1.2 christos 383 1.1.1.2 christos /* Return the DIE reader corresponding to PER_CU. If no such reader 384 1.1.1.2 christos has been registered, return NULL. */ 385 1.1.1.2 christos cutu_reader *get_reader (dwarf2_per_cu_data *per_cu); 386 1.1 christos 387 1.1.1.2 christos /* Preserve READER by storing it in the local hash table. */ 388 1.1.1.2 christos cutu_reader *preserve (std::unique_ptr<cutu_reader> reader); 389 1.1.1.2 christos 390 1.1.1.2 christos /* Add an entry to the index. The arguments describe the entry; see 391 1.1.1.2 christos cooked-index.h. The new entry is returned. */ 392 1.1.1.2 christos cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag, 393 1.1.1.2 christos cooked_index_flag flags, 394 1.1.1.2 christos const char *name, 395 1.1.1.2 christos cooked_index_entry_ref parent_entry, 396 1.1.1.2 christos dwarf2_per_cu_data *per_cu) 397 1.1.1.2 christos { 398 1.1.1.2 christos return m_index->add (die_offset, tag, flags, per_cu->lang (), 399 1.1.1.2 christos name, parent_entry, per_cu); 400 1.1.1.2 christos } 401 1.1.1.2 christos 402 1.1.1.2 christos /* Install the current addrmap into the shard being constructed, 403 1.1.1.2 christos then transfer ownership of the index to the caller. */ 404 1.1.1.2 christos std::unique_ptr<cooked_index_shard> release () 405 1.1.1.2 christos { 406 1.1.1.2 christos m_index->install_addrmap (&m_addrmap); 407 1.1.1.2 christos return std::move (m_index); 408 1.1.1.2 christos } 409 1.1.1.2 christos 410 1.1.1.2 christos /* Return the mutable addrmap that is currently being created. */ 411 1.1.1.2 christos addrmap_mutable *get_addrmap () 412 1.1.1.2 christos { 413 1.1.1.2 christos return &m_addrmap; 414 1.1.1.2 christos } 415 1.1 christos 416 1.1.1.2 christos /* Return the parent_map that is currently being created. */ 417 1.1.1.2 christos parent_map *get_parent_map () 418 1.1 christos { 419 1.1.1.2 christos return &m_parent_map; 420 1.1 christos } 421 1.1 christos 422 1.1.1.2 christos /* Return the parent_map that is currently being created. Ownership 423 1.1.1.2 christos is passed to the caller. */ 424 1.1.1.2 christos parent_map release_parent_map () 425 1.1 christos { 426 1.1.1.2 christos return std::move (m_parent_map); 427 1.1 christos } 428 1.1 christos 429 1.1.1.2 christos private: 430 1.1.1.2 christos 431 1.1.1.2 christos /* Hash function for a cutu_reader. */ 432 1.1.1.2 christos static hashval_t hash_cutu_reader (const void *a); 433 1.1.1.2 christos 434 1.1.1.2 christos /* Equality function for cutu_reader. */ 435 1.1.1.2 christos static int eq_cutu_reader (const void *a, const void *b); 436 1.1.1.2 christos 437 1.1.1.3 christos /* The abbrev table cache used by this indexer. */ 438 1.1.1.3 christos abbrev_table_cache m_abbrev_table_cache; 439 1.1.1.3 christos 440 1.1.1.2 christos /* A hash table of cutu_reader objects. */ 441 1.1.1.2 christos htab_up m_reader_hash; 442 1.1.1.2 christos /* The index shard that is being constructed. */ 443 1.1.1.2 christos std::unique_ptr<cooked_index_shard> m_index; 444 1.1.1.2 christos 445 1.1.1.2 christos /* Parent map for each CU that is read. */ 446 1.1.1.2 christos parent_map m_parent_map; 447 1.1.1.2 christos 448 1.1.1.2 christos /* A writeable addrmap being constructed by this scanner. */ 449 1.1.1.2 christos addrmap_mutable m_addrmap; 450 1.1.1.2 christos }; 451 1.1.1.2 christos 452 1.1.1.2 christos /* The possible states of the index. See the explanatory comment 453 1.1.1.2 christos before cooked_index for more details. */ 454 1.1.1.2 christos enum class cooked_state 455 1.1.1.2 christos { 456 1.1.1.2 christos /* The default state. This is not a valid argument to 'wait'. */ 457 1.1.1.2 christos INITIAL, 458 1.1.1.2 christos /* The initial scan has completed. The name of "main" is now 459 1.1.1.2 christos available (if known). The addrmaps are usable now. 460 1.1.1.2 christos Finalization has started but is not complete. */ 461 1.1.1.2 christos MAIN_AVAILABLE, 462 1.1.1.2 christos /* Finalization has completed. This means the index is fully 463 1.1.1.2 christos available for queries. */ 464 1.1.1.2 christos FINALIZED, 465 1.1.1.2 christos /* Writing to the index cache has finished. */ 466 1.1.1.2 christos CACHE_DONE, 467 1.1.1.2 christos }; 468 1.1.1.2 christos 469 1.1.1.2 christos /* An object of this type controls the scanning of the DWARF. It 470 1.1.1.2 christos schedules the worker tasks and tracks the current state. Once 471 1.1.1.2 christos scanning is done, this object is discarded. 472 1.1.1.2 christos 473 1.1.1.2 christos This is an abstract base class that defines the basic behavior of 474 1.1.1.2 christos scanners. Separate concrete implementations exist for scanning 475 1.1.1.2 christos .debug_names and .debug_info. */ 476 1.1.1.2 christos 477 1.1.1.2 christos class cooked_index_worker 478 1.1.1.2 christos { 479 1.1.1.2 christos public: 480 1.1.1.2 christos 481 1.1.1.2 christos explicit cooked_index_worker (dwarf2_per_objfile *per_objfile) 482 1.1.1.2 christos : m_per_objfile (per_objfile), 483 1.1.1.2 christos m_cache_store (global_index_cache, per_objfile->per_bfd) 484 1.1.1.2 christos { } 485 1.1.1.2 christos virtual ~cooked_index_worker () 486 1.1.1.2 christos { } 487 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index_worker); 488 1.1.1.2 christos 489 1.1.1.2 christos /* Start reading. */ 490 1.1.1.2 christos void start (); 491 1.1.1.2 christos 492 1.1.1.2 christos /* Wait for a particular state to be achieved. If ALLOW_QUIT is 493 1.1.1.2 christos true, then the loop will check the QUIT flag. Normally this 494 1.1.1.2 christos method may only be called from the main thread; however, it can 495 1.1.1.2 christos be called from a worker thread provided that the desired state 496 1.1.1.2 christos has already been attained. (This oddity is used by the index 497 1.1.1.2 christos cache writer.) */ 498 1.1.1.2 christos bool wait (cooked_state desired_state, bool allow_quit); 499 1.1.1.2 christos 500 1.1.1.2 christos protected: 501 1.1.1.2 christos 502 1.1.1.2 christos /* Let cooked_index call the 'set' and 'write_to_cache' methods. */ 503 1.1.1.2 christos friend class cooked_index; 504 1.1.1.2 christos 505 1.1.1.2 christos /* Set the current state. */ 506 1.1.1.2 christos void set (cooked_state desired_state); 507 1.1.1.2 christos 508 1.1.1.2 christos /* Write to the index cache. */ 509 1.1.1.2 christos void write_to_cache (const cooked_index *idx, 510 1.1.1.2 christos deferred_warnings *warn) const; 511 1.1.1.2 christos 512 1.1.1.2 christos /* Helper function that does the work of reading. This must be able 513 1.1.1.2 christos to be run in a worker thread without problems. */ 514 1.1.1.2 christos virtual void do_reading () = 0; 515 1.1.1.2 christos 516 1.1.1.2 christos /* A callback that can print stats, if needed. This is called when 517 1.1.1.2 christos transitioning to the 'MAIN_AVAILABLE' state. */ 518 1.1.1.2 christos virtual void print_stats () 519 1.1.1.2 christos { } 520 1.1.1.2 christos 521 1.1.1.2 christos /* Each thread returns a tuple holding a cooked index, any collected 522 1.1.1.2 christos complaints, a vector of errors that should be printed, and a 523 1.1.1.2 christos vector of parent maps. 524 1.1.1.2 christos 525 1.1.1.2 christos The errors are retained because GDB's I/O system is not 526 1.1.1.2 christos thread-safe. run_on_main_thread could be used, but that would 527 1.1.1.2 christos mean the messages are printed after the prompt, which looks 528 1.1.1.2 christos weird. */ 529 1.1.1.2 christos using result_type = std::tuple<std::unique_ptr<cooked_index_shard>, 530 1.1.1.2 christos complaint_collection, 531 1.1.1.2 christos std::vector<gdb_exception>, 532 1.1.1.2 christos parent_map>; 533 1.1.1.2 christos 534 1.1.1.2 christos /* The per-objfile object. */ 535 1.1.1.2 christos dwarf2_per_objfile *m_per_objfile; 536 1.1.1.2 christos /* Result of each worker task. */ 537 1.1.1.2 christos std::vector<result_type> m_results; 538 1.1.1.2 christos /* Any warnings emitted. This is not in 'result_type' because (for 539 1.1.1.2 christos the time being at least), it's only needed in do_reading, not in 540 1.1.1.2 christos every worker. Note that deferred_warnings uses gdb_stderr in its 541 1.1.1.2 christos constructor, and this should only be done from the main thread. 542 1.1.1.2 christos This is enforced in the cooked_index_worker constructor. */ 543 1.1.1.2 christos deferred_warnings m_warnings; 544 1.1.1.2 christos 545 1.1.1.2 christos /* A map of all parent maps. Used during finalization to fix up 546 1.1.1.2 christos parent relationships. */ 547 1.1.1.2 christos parent_map_map m_all_parents_map; 548 1.1.1.2 christos 549 1.1.1.2 christos #if CXX_STD_THREAD 550 1.1.1.2 christos /* Current state of this object. */ 551 1.1.1.2 christos cooked_state m_state = cooked_state::INITIAL; 552 1.1.1.2 christos /* Mutex and condition variable used to synchronize. */ 553 1.1.1.2 christos std::mutex m_mutex; 554 1.1.1.2 christos std::condition_variable m_cond; 555 1.1.1.2 christos #endif /* CXX_STD_THREAD */ 556 1.1.1.2 christos /* This flag indicates whether any complaints or exceptions that 557 1.1.1.2 christos arose during scanning have been reported by 'wait'. This may 558 1.1.1.2 christos only be modified on the main thread. */ 559 1.1.1.2 christos bool m_reported = false; 560 1.1.1.2 christos /* If set, an exception occurred during reading; in this case the 561 1.1.1.2 christos scanning is stopped and this exception will later be reported by 562 1.1.1.2 christos the 'wait' method. */ 563 1.1.1.2 christos std::optional<gdb_exception> m_failed; 564 1.1.1.2 christos /* An object used to write to the index cache. */ 565 1.1.1.2 christos index_cache_store_context m_cache_store; 566 1.1.1.2 christos }; 567 1.1.1.2 christos 568 1.1.1.2 christos /* The main index of DIEs. 569 1.1.1.2 christos 570 1.1.1.2 christos The index is created by multiple threads. The overall process is 571 1.1.1.2 christos somewhat complicated, so here's a diagram to help sort it out. 572 1.1.1.2 christos 573 1.1.1.2 christos The basic idea behind this design is (1) to do as much work as 574 1.1.1.2 christos possible in worker threads, and (2) to start the work as early as 575 1.1.1.2 christos possible. This combination should help hide the effort from the 576 1.1.1.2 christos user to the maximum possible degree. 577 1.1.1.2 christos 578 1.1.1.2 christos . Main Thread | Worker Threads 579 1.1.1.2 christos ============================================================ 580 1.1.1.2 christos . dwarf2_initialize_objfile 581 1.1.1.2 christos . | 582 1.1.1.2 christos . v 583 1.1.1.2 christos . cooked index ------------> cooked_index_worker::start 584 1.1.1.2 christos . | / | \ 585 1.1.1.2 christos . v / | \ 586 1.1.1.2 christos . install / | \ 587 1.1.1.2 christos . cooked_index_functions scan CUs in workers 588 1.1.1.2 christos . | create cooked_index_shard objects 589 1.1.1.2 christos . | \ | / 590 1.1.1.2 christos . v \|/ 591 1.1.1.2 christos . return to caller v 592 1.1.1.2 christos . initial scan is done 593 1.1.1.2 christos . state = MAIN_AVAILABLE 594 1.1.1.2 christos . "main" name now available 595 1.1.1.2 christos . | 596 1.1.1.2 christos . | 597 1.1.1.2 christos . if main thread calls... v 598 1.1.1.2 christos . compute_main_name cooked_index::set_contents 599 1.1.1.2 christos . | / | \ 600 1.1.1.2 christos . v / | \ 601 1.1.1.2 christos . wait (MAIN_AVAILABLE) finalization 602 1.1.1.2 christos . | \ | / 603 1.1.1.2 christos . v \ | / 604 1.1.1.2 christos . done state = FINALIZED 605 1.1.1.2 christos . | 606 1.1.1.2 christos . v 607 1.1.1.2 christos . maybe write to index cache 608 1.1.1.2 christos . state = CACHE_DONE 609 1.1.1.2 christos . 610 1.1.1.2 christos . 611 1.1.1.2 christos . if main thread calls... 612 1.1.1.2 christos . any other "quick" API 613 1.1.1.2 christos . | 614 1.1.1.2 christos . v 615 1.1.1.2 christos . wait (FINALIZED) 616 1.1.1.2 christos . | 617 1.1.1.2 christos . v 618 1.1.1.2 christos . use the index 619 1.1.1.2 christos */ 620 1.1.1.2 christos 621 1.1.1.2 christos class cooked_index : public dwarf_scanner_base 622 1.1.1.2 christos { 623 1.1.1.2 christos public: 624 1.1.1.2 christos 625 1.1.1.2 christos /* A convenience typedef for the vector that is contained in this 626 1.1.1.2 christos object. */ 627 1.1.1.2 christos using vec_type = std::vector<std::unique_ptr<cooked_index_shard>>; 628 1.1.1.2 christos 629 1.1.1.2 christos cooked_index (dwarf2_per_objfile *per_objfile, 630 1.1.1.2 christos std::unique_ptr<cooked_index_worker> &&worker); 631 1.1.1.2 christos ~cooked_index () override; 632 1.1.1.2 christos 633 1.1.1.2 christos DISABLE_COPY_AND_ASSIGN (cooked_index); 634 1.1.1.2 christos 635 1.1.1.2 christos /* Start reading the DWARF. */ 636 1.1.1.2 christos void start_reading (); 637 1.1.1.2 christos 638 1.1.1.2 christos /* Called by cooked_index_worker to set the contents of this index 639 1.1.1.2 christos and transition to the MAIN_AVAILABLE state. WARN is used to 640 1.1.1.2 christos collect any warnings that may arise when writing to the cache. 641 1.1.1.2 christos PARENT_MAPS is used when resolving pending parent links. 642 1.1.1.2 christos PARENT_MAPS may be NULL if there are no IS_PARENT_DEFERRED 643 1.1.1.2 christos entries in VEC. */ 644 1.1.1.2 christos void set_contents (vec_type &&vec, deferred_warnings *warn, 645 1.1.1.2 christos const parent_map_map *parent_maps); 646 1.1.1.2 christos 647 1.1 christos /* A range over a vector of subranges. */ 648 1.1.1.2 christos using range = range_chain<cooked_index_shard::range>; 649 1.1 christos 650 1.1 christos /* Look up an entry by name. Returns a range of all matching 651 1.1 christos results. If COMPLETING is true, then a larger range, suitable 652 1.1 christos for completion, will be returned. */ 653 1.1 christos range find (const std::string &name, bool completing); 654 1.1 christos 655 1.1 christos /* Return a range of all the entries. */ 656 1.1 christos range all_entries () 657 1.1 christos { 658 1.1.1.2 christos wait (cooked_state::FINALIZED, true); 659 1.1.1.2 christos std::vector<cooked_index_shard::range> result_range; 660 1.1 christos result_range.reserve (m_vector.size ()); 661 1.1 christos for (auto &entry : m_vector) 662 1.1 christos result_range.push_back (entry->all_entries ()); 663 1.1 christos return range (std::move (result_range)); 664 1.1 christos } 665 1.1 christos 666 1.1 christos /* Look up ADDR in the address map, and return either the 667 1.1 christos corresponding CU, or nullptr if the address could not be 668 1.1 christos found. */ 669 1.1.1.3 christos dwarf2_per_cu_data *lookup (unrelocated_addr addr) override; 670 1.1 christos 671 1.1 christos /* Return a new vector of all the addrmaps used by all the indexes 672 1.1 christos held by this object. */ 673 1.1.1.2 christos std::vector<const addrmap *> get_addrmaps (); 674 1.1 christos 675 1.1 christos /* Return the entry that is believed to represent the program's 676 1.1 christos "main". This will return NULL if no such entry is available. */ 677 1.1 christos const cooked_index_entry *get_main () const; 678 1.1 christos 679 1.1.1.2 christos const char *get_main_name (struct obstack *obstack, enum language *lang) 680 1.1.1.2 christos const; 681 1.1.1.2 christos 682 1.1.1.2 christos cooked_index *index_for_writing () override 683 1.1 christos { 684 1.1.1.2 christos wait (cooked_state::FINALIZED, true); 685 1.1 christos return this; 686 1.1 christos } 687 1.1 christos 688 1.1 christos quick_symbol_functions_up make_quick_functions () const override; 689 1.1 christos 690 1.1.1.2 christos /* Dump a human-readable form of the contents of the index. */ 691 1.1.1.2 christos void dump (gdbarch *arch); 692 1.1.1.2 christos 693 1.1.1.2 christos /* Wait until this object reaches the desired state. Note that 694 1.1.1.2 christos DESIRED_STATE may not be INITIAL -- it does not make sense to 695 1.1.1.2 christos wait for this. If ALLOW_QUIT is true, timed waits will be done 696 1.1.1.2 christos and the quit flag will be checked in a loop. This may normally 697 1.1.1.2 christos only be called from the main thread; however, it is ok to call 698 1.1.1.2 christos from a worker as long as the desired state has already been 699 1.1.1.2 christos attained. (This property is needed by the index cache 700 1.1.1.2 christos writer.) */ 701 1.1.1.2 christos void wait (cooked_state desired_state, bool allow_quit = false); 702 1.1.1.2 christos 703 1.1.1.2 christos void wait_completely () override 704 1.1.1.2 christos { wait (cooked_state::CACHE_DONE); } 705 1.1.1.2 christos 706 1.1 christos private: 707 1.1 christos 708 1.1 christos /* The vector of cooked_index objects. This is stored because the 709 1.1 christos entries are stored on the obstacks in those objects. */ 710 1.1 christos vec_type m_vector; 711 1.1.1.2 christos 712 1.1.1.2 christos /* This tracks the current state. When this is nullptr, it means 713 1.1.1.2 christos that the state is CACHE_DONE -- it's important to note that only 714 1.1.1.2 christos the main thread may change the value of this pointer. */ 715 1.1.1.2 christos std::unique_ptr<cooked_index_worker> m_state; 716 1.1.1.2 christos 717 1.1.1.2 christos dwarf2_per_bfd *m_per_bfd; 718 1.1.1.2 christos }; 719 1.1.1.2 christos 720 1.1.1.2 christos /* An implementation of quick_symbol_functions for the cooked DWARF 721 1.1.1.2 christos index. */ 722 1.1.1.2 christos 723 1.1.1.2 christos struct cooked_index_functions : public dwarf2_base_index_functions 724 1.1.1.2 christos { 725 1.1.1.2 christos cooked_index *wait (struct objfile *objfile, bool allow_quit) 726 1.1.1.2 christos { 727 1.1.1.2 christos dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile); 728 1.1.1.2 christos cooked_index *table 729 1.1.1.2 christos = (gdb::checked_static_cast<cooked_index *> 730 1.1.1.2 christos (per_objfile->per_bfd->index_table.get ())); 731 1.1.1.2 christos table->wait (cooked_state::MAIN_AVAILABLE, allow_quit); 732 1.1.1.2 christos return table; 733 1.1.1.2 christos } 734 1.1.1.2 christos 735 1.1.1.2 christos struct compunit_symtab *find_compunit_symtab_by_address 736 1.1.1.2 christos (struct objfile *objfile, CORE_ADDR address) override; 737 1.1.1.2 christos 738 1.1.1.2 christos bool has_unexpanded_symtabs (struct objfile *objfile) override 739 1.1.1.2 christos { 740 1.1.1.2 christos wait (objfile, true); 741 1.1.1.2 christos return dwarf2_base_index_functions::has_unexpanded_symtabs (objfile); 742 1.1.1.2 christos } 743 1.1.1.2 christos 744 1.1.1.2 christos struct symtab *find_last_source_symtab (struct objfile *objfile) override 745 1.1.1.2 christos { 746 1.1.1.2 christos wait (objfile, true); 747 1.1.1.2 christos return dwarf2_base_index_functions::find_last_source_symtab (objfile); 748 1.1.1.2 christos } 749 1.1.1.2 christos 750 1.1.1.2 christos void forget_cached_source_info (struct objfile *objfile) override 751 1.1.1.2 christos { 752 1.1.1.2 christos wait (objfile, true); 753 1.1.1.2 christos dwarf2_base_index_functions::forget_cached_source_info (objfile); 754 1.1.1.2 christos } 755 1.1.1.2 christos 756 1.1.1.2 christos void print_stats (struct objfile *objfile, bool print_bcache) override 757 1.1.1.2 christos { 758 1.1.1.2 christos wait (objfile, true); 759 1.1.1.2 christos dwarf2_base_index_functions::print_stats (objfile, print_bcache); 760 1.1.1.2 christos } 761 1.1.1.2 christos 762 1.1.1.2 christos void dump (struct objfile *objfile) override 763 1.1.1.2 christos { 764 1.1.1.2 christos cooked_index *index = wait (objfile, true); 765 1.1.1.2 christos gdb_printf ("Cooked index in use:\n"); 766 1.1.1.2 christos gdb_printf ("\n"); 767 1.1.1.2 christos index->dump (objfile->arch ()); 768 1.1.1.2 christos } 769 1.1.1.2 christos 770 1.1.1.2 christos void expand_all_symtabs (struct objfile *objfile) override 771 1.1.1.2 christos { 772 1.1.1.2 christos wait (objfile, true); 773 1.1.1.2 christos dwarf2_base_index_functions::expand_all_symtabs (objfile); 774 1.1.1.2 christos } 775 1.1.1.2 christos 776 1.1.1.2 christos bool expand_symtabs_matching 777 1.1.1.2 christos (struct objfile *objfile, 778 1.1.1.2 christos gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher, 779 1.1.1.2 christos const lookup_name_info *lookup_name, 780 1.1.1.2 christos gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher, 781 1.1.1.2 christos gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify, 782 1.1.1.2 christos block_search_flags search_flags, 783 1.1.1.3 christos domain_search_flags domain, 784 1.1.1.3 christos gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher) 785 1.1.1.3 christos override; 786 1.1.1.2 christos 787 1.1.1.2 christos struct compunit_symtab *find_pc_sect_compunit_symtab 788 1.1.1.3 christos (struct objfile *objfile, bound_minimal_symbol msymbol, 789 1.1.1.2 christos CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override 790 1.1.1.2 christos { 791 1.1.1.2 christos wait (objfile, true); 792 1.1.1.2 christos return (dwarf2_base_index_functions::find_pc_sect_compunit_symtab 793 1.1.1.2 christos (objfile, msymbol, pc, section, warn_if_readin)); 794 1.1.1.2 christos } 795 1.1.1.2 christos 796 1.1.1.2 christos void map_symbol_filenames 797 1.1.1.2 christos (struct objfile *objfile, 798 1.1.1.2 christos gdb::function_view<symbol_filename_ftype> fun, 799 1.1.1.2 christos bool need_fullname) override 800 1.1.1.2 christos { 801 1.1.1.2 christos wait (objfile, true); 802 1.1.1.2 christos return (dwarf2_base_index_functions::map_symbol_filenames 803 1.1.1.2 christos (objfile, fun, need_fullname)); 804 1.1.1.2 christos } 805 1.1.1.2 christos 806 1.1.1.2 christos void compute_main_name (struct objfile *objfile) override 807 1.1.1.2 christos { 808 1.1.1.2 christos wait (objfile, false); 809 1.1.1.2 christos } 810 1.1 christos }; 811 1.1 christos 812 1.1 christos #endif /* GDB_DWARF2_COOKED_INDEX_H */ 813