xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/dwarf2/index-cache.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
17d62b00eSchristos /* Caching of GDB/DWARF index files.
27d62b00eSchristos 
3*6881a400Schristos    Copyright (C) 2018-2023 Free Software Foundation, Inc.
47d62b00eSchristos 
57d62b00eSchristos    This file is part of GDB.
67d62b00eSchristos 
77d62b00eSchristos    This program is free software; you can redistribute it and/or modify
87d62b00eSchristos    it under the terms of the GNU General Public License as published by
97d62b00eSchristos    the Free Software Foundation; either version 3 of the License, or
107d62b00eSchristos    (at your option) any later version.
117d62b00eSchristos 
127d62b00eSchristos    This program is distributed in the hope that it will be useful,
137d62b00eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
147d62b00eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
157d62b00eSchristos    GNU General Public License for more details.
167d62b00eSchristos 
177d62b00eSchristos    You should have received a copy of the GNU General Public License
187d62b00eSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
197d62b00eSchristos 
207d62b00eSchristos #ifndef DWARF_INDEX_CACHE_H
217d62b00eSchristos #define DWARF_INDEX_CACHE_H
227d62b00eSchristos 
237d62b00eSchristos #include "dwarf2/index-common.h"
247d62b00eSchristos #include "gdbsupport/array-view.h"
257d62b00eSchristos #include "symfile.h"
267d62b00eSchristos 
277d62b00eSchristos /* Base of the classes used to hold the resources of the indices loaded from
287d62b00eSchristos    the cache (e.g. mmapped files).  */
297d62b00eSchristos 
307d62b00eSchristos struct index_cache_resource
317d62b00eSchristos {
327d62b00eSchristos   virtual ~index_cache_resource () = 0;
337d62b00eSchristos };
347d62b00eSchristos 
357d62b00eSchristos /* Class to manage the access to the DWARF index cache.  */
367d62b00eSchristos 
377d62b00eSchristos class index_cache
387d62b00eSchristos {
397d62b00eSchristos public:
407d62b00eSchristos   /* Change the directory used to save/load index files.  */
417d62b00eSchristos   void set_directory (std::string dir);
427d62b00eSchristos 
437d62b00eSchristos   /* Return true if the usage of the cache is enabled.  */
447d62b00eSchristos   bool enabled () const
457d62b00eSchristos   {
467d62b00eSchristos     return m_enabled;
477d62b00eSchristos   }
487d62b00eSchristos 
497d62b00eSchristos   /* Enable the cache.  */
507d62b00eSchristos   void enable ();
517d62b00eSchristos 
527d62b00eSchristos   /* Disable the cache.  */
537d62b00eSchristos   void disable ();
547d62b00eSchristos 
557d62b00eSchristos   /* Store an index for the specified object file in the cache.  */
567d62b00eSchristos   void store (dwarf2_per_objfile *per_objfile);
577d62b00eSchristos 
587d62b00eSchristos   /* Look for an index file matching BUILD_ID.  If found, return the contents
597d62b00eSchristos      as an array_view and store the underlying resources (allocated memory,
607d62b00eSchristos      mapped file, etc) in RESOURCE.  The returned array_view is valid as long
617d62b00eSchristos      as RESOURCE is not destroyed.
627d62b00eSchristos 
637d62b00eSchristos      If no matching index file is found, return an empty array view.  */
647d62b00eSchristos   gdb::array_view<const gdb_byte>
657d62b00eSchristos   lookup_gdb_index (const bfd_build_id *build_id,
667d62b00eSchristos 		    std::unique_ptr<index_cache_resource> *resource);
677d62b00eSchristos 
687d62b00eSchristos   /* Return the number of cache hits.  */
697d62b00eSchristos   unsigned int n_hits () const
707d62b00eSchristos   { return m_n_hits; }
717d62b00eSchristos 
727d62b00eSchristos   /* Record a cache hit.  */
737d62b00eSchristos   void hit ()
747d62b00eSchristos   {
757d62b00eSchristos     if (enabled ())
767d62b00eSchristos       m_n_hits++;
777d62b00eSchristos   }
787d62b00eSchristos 
797d62b00eSchristos   /* Return the number of cache misses.  */
807d62b00eSchristos   unsigned int n_misses () const
817d62b00eSchristos   { return m_n_misses; }
827d62b00eSchristos 
837d62b00eSchristos   /* Record a cache miss.  */
847d62b00eSchristos   void miss ()
857d62b00eSchristos   {
867d62b00eSchristos     if (enabled ())
877d62b00eSchristos       m_n_misses++;
887d62b00eSchristos   }
897d62b00eSchristos 
907d62b00eSchristos private:
917d62b00eSchristos 
927d62b00eSchristos   /* Compute the absolute filename where the index of the objfile with build
937d62b00eSchristos      id BUILD_ID will be stored.  SUFFIX is appended at the end of the
947d62b00eSchristos      filename.  */
957d62b00eSchristos   std::string make_index_filename (const bfd_build_id *build_id,
967d62b00eSchristos 				   const char *suffix) const;
977d62b00eSchristos 
987d62b00eSchristos   /* The base directory where we are storing and looking up index files.  */
997d62b00eSchristos   std::string m_dir;
1007d62b00eSchristos 
1017d62b00eSchristos   /* Whether the cache is enabled.  */
1027d62b00eSchristos   bool m_enabled = false;
1037d62b00eSchristos 
1047d62b00eSchristos   /* Number of cache hits and misses during this GDB session.  */
1057d62b00eSchristos   unsigned int m_n_hits = 0;
1067d62b00eSchristos   unsigned int m_n_misses = 0;
1077d62b00eSchristos };
1087d62b00eSchristos 
1097d62b00eSchristos /* The global instance of the index cache.  */
1107d62b00eSchristos extern index_cache global_index_cache;
1117d62b00eSchristos 
1127d62b00eSchristos #endif /* DWARF_INDEX_CACHE_H */
113