1*6881a400Schristos /* DIE indexing 2*6881a400Schristos 3*6881a400Schristos Copyright (C) 2022-2023 Free Software Foundation, Inc. 4*6881a400Schristos 5*6881a400Schristos This file is part of GDB. 6*6881a400Schristos 7*6881a400Schristos This program is free software; you can redistribute it and/or modify 8*6881a400Schristos it under the terms of the GNU General Public License as published by 9*6881a400Schristos the Free Software Foundation; either version 3 of the License, or 10*6881a400Schristos (at your option) any later version. 11*6881a400Schristos 12*6881a400Schristos This program is distributed in the hope that it will be useful, 13*6881a400Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 14*6881a400Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*6881a400Schristos GNU General Public License for more details. 16*6881a400Schristos 17*6881a400Schristos You should have received a copy of the GNU General Public License 18*6881a400Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19*6881a400Schristos 20*6881a400Schristos #ifndef GDB_DWARF2_COOKED_INDEX_H 21*6881a400Schristos #define GDB_DWARF2_COOKED_INDEX_H 22*6881a400Schristos 23*6881a400Schristos #include "dwarf2.h" 24*6881a400Schristos #include "gdbtypes.h" 25*6881a400Schristos #include "symtab.h" 26*6881a400Schristos #include "hashtab.h" 27*6881a400Schristos #include "dwarf2/index-common.h" 28*6881a400Schristos #include "gdbsupport/gdb_string_view.h" 29*6881a400Schristos #include "quick-symbol.h" 30*6881a400Schristos #include "gdbsupport/gdb_obstack.h" 31*6881a400Schristos #include "addrmap.h" 32*6881a400Schristos #include "gdbsupport/iterator-range.h" 33*6881a400Schristos #include "gdbsupport/thread-pool.h" 34*6881a400Schristos #include "dwarf2/mapped-index.h" 35*6881a400Schristos #include "dwarf2/tag.h" 36*6881a400Schristos #include "gdbsupport/range-chain.h" 37*6881a400Schristos 38*6881a400Schristos struct dwarf2_per_cu_data; 39*6881a400Schristos 40*6881a400Schristos /* Flags that describe an entry in the index. */ 41*6881a400Schristos enum cooked_index_flag_enum : unsigned char 42*6881a400Schristos { 43*6881a400Schristos /* True if this entry is the program's "main". */ 44*6881a400Schristos IS_MAIN = 1, 45*6881a400Schristos /* True if this entry represents a "static" object. */ 46*6881a400Schristos IS_STATIC = 2, 47*6881a400Schristos /* True if this entry is an "enum class". */ 48*6881a400Schristos IS_ENUM_CLASS = 4, 49*6881a400Schristos /* True if this entry uses the linkage name. */ 50*6881a400Schristos IS_LINKAGE = 8, 51*6881a400Schristos /* True if this entry is just for the declaration of a type, not the 52*6881a400Schristos definition. */ 53*6881a400Schristos IS_TYPE_DECLARATION = 16, 54*6881a400Schristos }; 55*6881a400Schristos DEF_ENUM_FLAGS_TYPE (enum cooked_index_flag_enum, cooked_index_flag); 56*6881a400Schristos 57*6881a400Schristos /* Return true if LANG requires canonicalization. This is used 58*6881a400Schristos primarily to work around an issue computing the name of "main". 59*6881a400Schristos This function must be kept in sync with 60*6881a400Schristos cooked_index_shard::do_finalize. */ 61*6881a400Schristos 62*6881a400Schristos extern bool language_requires_canonicalization (enum language lang); 63*6881a400Schristos 64*6881a400Schristos /* A cooked_index_entry represents a single item in the index. Note 65*6881a400Schristos that two entries can be created for the same DIE -- one using the 66*6881a400Schristos name, and another one using the linkage name, if any. 67*6881a400Schristos 68*6881a400Schristos This is an "open" class and the members are all directly 69*6881a400Schristos accessible. It is read-only after the index has been fully read 70*6881a400Schristos and processed. */ 71*6881a400Schristos struct cooked_index_entry : public allocate_on_obstack 72*6881a400Schristos { 73*6881a400Schristos cooked_index_entry (sect_offset die_offset_, enum dwarf_tag tag_, 74*6881a400Schristos cooked_index_flag flags_, const char *name_, 75*6881a400Schristos const cooked_index_entry *parent_entry_, 76*6881a400Schristos dwarf2_per_cu_data *per_cu_) 77*6881a400Schristos : name (name_), 78*6881a400Schristos tag (tag_), 79*6881a400Schristos flags (flags_), 80*6881a400Schristos die_offset (die_offset_), 81*6881a400Schristos parent_entry (parent_entry_), 82*6881a400Schristos per_cu (per_cu_) 83*6881a400Schristos { 84*6881a400Schristos } 85*6881a400Schristos 86*6881a400Schristos /* Return true if this entry matches SEARCH_FLAGS. */ 87*6881a400Schristos bool matches (block_search_flags search_flags) const 88*6881a400Schristos { 89*6881a400Schristos /* Just reject type declarations. */ 90*6881a400Schristos if ((flags & IS_TYPE_DECLARATION) != 0) 91*6881a400Schristos return false; 92*6881a400Schristos 93*6881a400Schristos if ((search_flags & SEARCH_STATIC_BLOCK) != 0 94*6881a400Schristos && (flags & IS_STATIC) != 0) 95*6881a400Schristos return true; 96*6881a400Schristos if ((search_flags & SEARCH_GLOBAL_BLOCK) != 0 97*6881a400Schristos && (flags & IS_STATIC) == 0) 98*6881a400Schristos return true; 99*6881a400Schristos return false; 100*6881a400Schristos } 101*6881a400Schristos 102*6881a400Schristos /* Return true if this entry matches DOMAIN. */ 103*6881a400Schristos bool matches (domain_enum domain) const 104*6881a400Schristos { 105*6881a400Schristos /* Just reject type declarations. */ 106*6881a400Schristos if ((flags & IS_TYPE_DECLARATION) != 0) 107*6881a400Schristos return false; 108*6881a400Schristos 109*6881a400Schristos switch (domain) 110*6881a400Schristos { 111*6881a400Schristos case LABEL_DOMAIN: 112*6881a400Schristos return false; 113*6881a400Schristos 114*6881a400Schristos case MODULE_DOMAIN: 115*6881a400Schristos return tag == DW_TAG_module; 116*6881a400Schristos 117*6881a400Schristos case COMMON_BLOCK_DOMAIN: 118*6881a400Schristos return tag == DW_TAG_common_block; 119*6881a400Schristos } 120*6881a400Schristos 121*6881a400Schristos return true; 122*6881a400Schristos } 123*6881a400Schristos 124*6881a400Schristos /* Return true if this entry matches KIND. */ 125*6881a400Schristos bool matches (enum search_domain kind) const 126*6881a400Schristos { 127*6881a400Schristos /* Just reject type declarations. */ 128*6881a400Schristos if ((flags & IS_TYPE_DECLARATION) != 0) 129*6881a400Schristos return false; 130*6881a400Schristos 131*6881a400Schristos switch (kind) 132*6881a400Schristos { 133*6881a400Schristos case VARIABLES_DOMAIN: 134*6881a400Schristos return (tag == DW_TAG_variable 135*6881a400Schristos || tag == DW_TAG_constant 136*6881a400Schristos || tag == DW_TAG_enumerator); 137*6881a400Schristos case FUNCTIONS_DOMAIN: 138*6881a400Schristos return tag == DW_TAG_subprogram; 139*6881a400Schristos case TYPES_DOMAIN: 140*6881a400Schristos return tag_is_type (tag); 141*6881a400Schristos case MODULES_DOMAIN: 142*6881a400Schristos return tag == DW_TAG_module; 143*6881a400Schristos } 144*6881a400Schristos 145*6881a400Schristos return true; 146*6881a400Schristos } 147*6881a400Schristos 148*6881a400Schristos /* Construct the fully-qualified name of this entry and return a 149*6881a400Schristos pointer to it. If allocation is needed, it will be done on 150*6881a400Schristos STORAGE. FOR_MAIN is true if we are computing the name of the 151*6881a400Schristos "main" entry -- one marked DW_AT_main_subprogram. This matters 152*6881a400Schristos for avoiding name canonicalization and also a related race (if 153*6881a400Schristos "main" computation is done during finalization). */ 154*6881a400Schristos const char *full_name (struct obstack *storage, bool for_main = false) const; 155*6881a400Schristos 156*6881a400Schristos /* Comparison modes for the 'compare' function. See the function 157*6881a400Schristos for a description. */ 158*6881a400Schristos enum comparison_mode 159*6881a400Schristos { 160*6881a400Schristos MATCH, 161*6881a400Schristos SORT, 162*6881a400Schristos COMPLETE, 163*6881a400Schristos }; 164*6881a400Schristos 165*6881a400Schristos /* Compare two strings, case-insensitively. Return -1 if STRA is 166*6881a400Schristos less than STRB, 0 if they are equal, and 1 if STRA is greater. 167*6881a400Schristos 168*6881a400Schristos When comparing, '<' is considered to be less than all other 169*6881a400Schristos printable characters. This ensures that "t<x>" sorts before 170*6881a400Schristos "t1", which is necessary when looking up "t". This '<' handling 171*6881a400Schristos is to ensure that certain C++ lookups work correctly. It is 172*6881a400Schristos inexact, and applied regardless of the search language, but this 173*6881a400Schristos is ok because callers of this code do more precise filtering 174*6881a400Schristos according to their needs. This is also why using a 175*6881a400Schristos case-insensitive comparison works even for languages that are 176*6881a400Schristos case sensitive. 177*6881a400Schristos 178*6881a400Schristos MODE controls how the comparison proceeds. 179*6881a400Schristos 180*6881a400Schristos MODE==SORT is used when sorting and the only special '<' handling 181*6881a400Schristos that it does is to ensure that '<' sorts before all other 182*6881a400Schristos printable characters. This ensures that the resulting ordering 183*6881a400Schristos will be binary-searchable. 184*6881a400Schristos 185*6881a400Schristos MODE==MATCH is used when searching for a symbol. In this case, 186*6881a400Schristos STRB must always be the search name, and STRA must be the name in 187*6881a400Schristos the index that is under consideration. In compare mode, early 188*6881a400Schristos termination of STRB may match STRA -- for example, "t<int>" and 189*6881a400Schristos "t" will be considered to be equal. (However, if A=="t" and 190*6881a400Schristos B=="t<int>", then this will not consider them as equal.) 191*6881a400Schristos 192*6881a400Schristos MODE==COMPLETE is used when searching for a symbol for 193*6881a400Schristos completion. In this case, STRB must always be the search name, 194*6881a400Schristos and STRA must be the name in the index that is under 195*6881a400Schristos consideration. In completion mode, early termination of STRB 196*6881a400Schristos always results in a match. */ 197*6881a400Schristos static int compare (const char *stra, const char *strb, 198*6881a400Schristos comparison_mode mode); 199*6881a400Schristos 200*6881a400Schristos /* Compare two entries by canonical name. */ 201*6881a400Schristos bool operator< (const cooked_index_entry &other) const 202*6881a400Schristos { 203*6881a400Schristos return compare (canonical, other.canonical, SORT) < 0; 204*6881a400Schristos } 205*6881a400Schristos 206*6881a400Schristos /* The name as it appears in DWARF. This always points into one of 207*6881a400Schristos the mapped DWARF sections. Note that this may be the name or the 208*6881a400Schristos linkage name -- two entries are created for DIEs which have both 209*6881a400Schristos attributes. */ 210*6881a400Schristos const char *name; 211*6881a400Schristos /* The canonical name. For C++ names, this may differ from NAME. 212*6881a400Schristos In all other cases, this is equal to NAME. */ 213*6881a400Schristos const char *canonical = nullptr; 214*6881a400Schristos /* The DWARF tag. */ 215*6881a400Schristos enum dwarf_tag tag; 216*6881a400Schristos /* Any flags attached to this entry. */ 217*6881a400Schristos cooked_index_flag flags; 218*6881a400Schristos /* The offset of this DIE. */ 219*6881a400Schristos sect_offset die_offset; 220*6881a400Schristos /* The parent entry. This is NULL for top-level entries. 221*6881a400Schristos Otherwise, it points to the parent entry, such as a namespace or 222*6881a400Schristos class. */ 223*6881a400Schristos const cooked_index_entry *parent_entry; 224*6881a400Schristos /* The CU from which this entry originates. */ 225*6881a400Schristos dwarf2_per_cu_data *per_cu; 226*6881a400Schristos 227*6881a400Schristos private: 228*6881a400Schristos 229*6881a400Schristos /* A helper method for full_name. Emits the full scope of this 230*6881a400Schristos object, followed by the separator, to STORAGE. If this entry has 231*6881a400Schristos a parent, its write_scope method is called first. */ 232*6881a400Schristos void write_scope (struct obstack *storage, const char *sep, 233*6881a400Schristos bool for_name) const; 234*6881a400Schristos }; 235*6881a400Schristos 236*6881a400Schristos class cooked_index_vector; 237*6881a400Schristos 238*6881a400Schristos /* An index of interesting DIEs. This is "cooked", in contrast to a 239*6881a400Schristos mapped .debug_names or .gdb_index, which are "raw". An entry in 240*6881a400Schristos the index is of type cooked_index_entry. 241*6881a400Schristos 242*6881a400Schristos Operations on the index are described below. They are chosen to 243*6881a400Schristos make it relatively simple to implement the symtab "quick" 244*6881a400Schristos methods. */ 245*6881a400Schristos class cooked_index 246*6881a400Schristos { 247*6881a400Schristos public: 248*6881a400Schristos cooked_index () = default; 249*6881a400Schristos DISABLE_COPY_AND_ASSIGN (cooked_index); 250*6881a400Schristos 251*6881a400Schristos /* Create a new cooked_index_entry and register it with this object. 252*6881a400Schristos Entries are owned by this object. The new item is returned. */ 253*6881a400Schristos const cooked_index_entry *add (sect_offset die_offset, enum dwarf_tag tag, 254*6881a400Schristos cooked_index_flag flags, 255*6881a400Schristos const char *name, 256*6881a400Schristos const cooked_index_entry *parent_entry, 257*6881a400Schristos dwarf2_per_cu_data *per_cu); 258*6881a400Schristos 259*6881a400Schristos /* Install a new fixed addrmap from the given mutable addrmap. */ 260*6881a400Schristos void install_addrmap (addrmap_mutable *map) 261*6881a400Schristos { 262*6881a400Schristos gdb_assert (m_addrmap == nullptr); 263*6881a400Schristos m_addrmap = new (&m_storage) addrmap_fixed (&m_storage, map); 264*6881a400Schristos } 265*6881a400Schristos 266*6881a400Schristos /* Finalize the index. This should be called a single time, when 267*6881a400Schristos the index has been fully populated. It enters all the entries 268*6881a400Schristos into the internal table. */ 269*6881a400Schristos void finalize (); 270*6881a400Schristos 271*6881a400Schristos /* Wait for this index's finalization to be complete. */ 272*6881a400Schristos void wait () 273*6881a400Schristos { 274*6881a400Schristos m_future.wait (); 275*6881a400Schristos } 276*6881a400Schristos 277*6881a400Schristos friend class cooked_index_vector; 278*6881a400Schristos 279*6881a400Schristos /* A simple range over part of m_entries. */ 280*6881a400Schristos typedef iterator_range<std::vector<cooked_index_entry *>::iterator> range; 281*6881a400Schristos 282*6881a400Schristos /* Return a range of all the entries. */ 283*6881a400Schristos range all_entries () 284*6881a400Schristos { 285*6881a400Schristos wait (); 286*6881a400Schristos return { m_entries.begin (), m_entries.end () }; 287*6881a400Schristos } 288*6881a400Schristos 289*6881a400Schristos /* Look up an entry by name. Returns a range of all matching 290*6881a400Schristos results. If COMPLETING is true, then a larger range, suitable 291*6881a400Schristos for completion, will be returned. */ 292*6881a400Schristos range find (const std::string &name, bool completing); 293*6881a400Schristos 294*6881a400Schristos private: 295*6881a400Schristos 296*6881a400Schristos /* Return the entry that is believed to represent the program's 297*6881a400Schristos "main". This will return NULL if no such entry is available. */ 298*6881a400Schristos const cooked_index_entry *get_main () const 299*6881a400Schristos { 300*6881a400Schristos return m_main; 301*6881a400Schristos } 302*6881a400Schristos 303*6881a400Schristos /* Look up ADDR in the address map, and return either the 304*6881a400Schristos corresponding CU, or nullptr if the address could not be 305*6881a400Schristos found. */ 306*6881a400Schristos dwarf2_per_cu_data *lookup (CORE_ADDR addr) 307*6881a400Schristos { 308*6881a400Schristos return (dwarf2_per_cu_data *) m_addrmap->find (addr); 309*6881a400Schristos } 310*6881a400Schristos 311*6881a400Schristos /* Create a new cooked_index_entry and register it with this object. 312*6881a400Schristos Entries are owned by this object. The new item is returned. */ 313*6881a400Schristos cooked_index_entry *create (sect_offset die_offset, 314*6881a400Schristos enum dwarf_tag tag, 315*6881a400Schristos cooked_index_flag flags, 316*6881a400Schristos const char *name, 317*6881a400Schristos const cooked_index_entry *parent_entry, 318*6881a400Schristos dwarf2_per_cu_data *per_cu) 319*6881a400Schristos { 320*6881a400Schristos return new (&m_storage) cooked_index_entry (die_offset, tag, flags, 321*6881a400Schristos name, parent_entry, 322*6881a400Schristos per_cu); 323*6881a400Schristos } 324*6881a400Schristos 325*6881a400Schristos /* GNAT only emits mangled ("encoded") names in the DWARF, and does 326*6881a400Schristos not emit the module structure. However, we need this structure 327*6881a400Schristos to do lookups. This function recreates that structure for an 328*6881a400Schristos existing entry. It returns the base name (last element) of the 329*6881a400Schristos full decoded name. */ 330*6881a400Schristos gdb::unique_xmalloc_ptr<char> handle_gnat_encoded_entry 331*6881a400Schristos (cooked_index_entry *entry, htab_t gnat_entries); 332*6881a400Schristos 333*6881a400Schristos /* A helper method that does the work of 'finalize'. */ 334*6881a400Schristos void do_finalize (); 335*6881a400Schristos 336*6881a400Schristos /* Storage for the entries. */ 337*6881a400Schristos auto_obstack m_storage; 338*6881a400Schristos /* List of all entries. */ 339*6881a400Schristos std::vector<cooked_index_entry *> m_entries; 340*6881a400Schristos /* If we found an entry with 'is_main' set, store it here. */ 341*6881a400Schristos cooked_index_entry *m_main = nullptr; 342*6881a400Schristos /* The addrmap. This maps address ranges to dwarf2_per_cu_data 343*6881a400Schristos objects. */ 344*6881a400Schristos addrmap *m_addrmap = nullptr; 345*6881a400Schristos /* Storage for canonical names. */ 346*6881a400Schristos std::vector<gdb::unique_xmalloc_ptr<char>> m_names; 347*6881a400Schristos /* A future that tracks when the 'finalize' method is done. Note 348*6881a400Schristos that the 'get' method is never called on this future, only 349*6881a400Schristos 'wait'. */ 350*6881a400Schristos gdb::future<void> m_future; 351*6881a400Schristos }; 352*6881a400Schristos 353*6881a400Schristos /* The main index of DIEs. The parallel DIE indexers create 354*6881a400Schristos cooked_index objects. Then, these are all handled to a 355*6881a400Schristos cooked_index_vector for storage and final indexing. The index is 356*6881a400Schristos made by iterating over the entries previously created. */ 357*6881a400Schristos 358*6881a400Schristos class cooked_index_vector : public dwarf_scanner_base 359*6881a400Schristos { 360*6881a400Schristos public: 361*6881a400Schristos 362*6881a400Schristos /* A convenience typedef for the vector that is contained in this 363*6881a400Schristos object. */ 364*6881a400Schristos typedef std::vector<std::unique_ptr<cooked_index>> vec_type; 365*6881a400Schristos 366*6881a400Schristos explicit cooked_index_vector (vec_type &&vec); 367*6881a400Schristos DISABLE_COPY_AND_ASSIGN (cooked_index_vector); 368*6881a400Schristos 369*6881a400Schristos /* Wait until the finalization of the entire cooked_index_vector is 370*6881a400Schristos done. */ 371*6881a400Schristos void wait () 372*6881a400Schristos { 373*6881a400Schristos for (auto &item : m_vector) 374*6881a400Schristos item->wait (); 375*6881a400Schristos } 376*6881a400Schristos 377*6881a400Schristos ~cooked_index_vector () 378*6881a400Schristos { 379*6881a400Schristos /* The 'finalize' methods may be run in a different thread. If 380*6881a400Schristos this object is destroyed before these complete, then one will 381*6881a400Schristos end up writing to freed memory. Waiting for finalization to 382*6881a400Schristos complete avoids this problem; and the cost seems ignorable 383*6881a400Schristos because creating and immediately destroying the debug info is a 384*6881a400Schristos relatively rare thing to do. */ 385*6881a400Schristos wait (); 386*6881a400Schristos } 387*6881a400Schristos 388*6881a400Schristos /* A range over a vector of subranges. */ 389*6881a400Schristos typedef range_chain<cooked_index::range> range; 390*6881a400Schristos 391*6881a400Schristos /* Look up an entry by name. Returns a range of all matching 392*6881a400Schristos results. If COMPLETING is true, then a larger range, suitable 393*6881a400Schristos for completion, will be returned. */ 394*6881a400Schristos range find (const std::string &name, bool completing); 395*6881a400Schristos 396*6881a400Schristos /* Return a range of all the entries. */ 397*6881a400Schristos range all_entries () 398*6881a400Schristos { 399*6881a400Schristos std::vector<cooked_index::range> result_range; 400*6881a400Schristos result_range.reserve (m_vector.size ()); 401*6881a400Schristos for (auto &entry : m_vector) 402*6881a400Schristos result_range.push_back (entry->all_entries ()); 403*6881a400Schristos return range (std::move (result_range)); 404*6881a400Schristos } 405*6881a400Schristos 406*6881a400Schristos /* Look up ADDR in the address map, and return either the 407*6881a400Schristos corresponding CU, or nullptr if the address could not be 408*6881a400Schristos found. */ 409*6881a400Schristos dwarf2_per_cu_data *lookup (CORE_ADDR addr); 410*6881a400Schristos 411*6881a400Schristos /* Return a new vector of all the addrmaps used by all the indexes 412*6881a400Schristos held by this object. */ 413*6881a400Schristos std::vector<addrmap *> get_addrmaps (); 414*6881a400Schristos 415*6881a400Schristos /* Return the entry that is believed to represent the program's 416*6881a400Schristos "main". This will return NULL if no such entry is available. */ 417*6881a400Schristos const cooked_index_entry *get_main () const; 418*6881a400Schristos 419*6881a400Schristos cooked_index_vector *index_for_writing () override 420*6881a400Schristos { 421*6881a400Schristos return this; 422*6881a400Schristos } 423*6881a400Schristos 424*6881a400Schristos quick_symbol_functions_up make_quick_functions () const override; 425*6881a400Schristos 426*6881a400Schristos private: 427*6881a400Schristos 428*6881a400Schristos /* The vector of cooked_index objects. This is stored because the 429*6881a400Schristos entries are stored on the obstacks in those objects. */ 430*6881a400Schristos vec_type m_vector; 431*6881a400Schristos }; 432*6881a400Schristos 433*6881a400Schristos #endif /* GDB_DWARF2_COOKED_INDEX_H */ 434