Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / dwarf2 / index-cache.h
blob95f217ed961c758cdc7dd542d97dbc5526f06df5
1 /* Caching of GDB/DWARF index files.
3 Copyright (C) 2018-2024 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 DWARF_INDEX_CACHE_H
21 #define DWARF_INDEX_CACHE_H
23 #include "dwarf2/index-common.h"
24 #include "gdbsupport/array-view.h"
25 #include "symfile.h"
27 class dwarf2_per_bfd;
28 class index_cache;
30 /* Base of the classes used to hold the resources of the indices loaded from
31 the cache (e.g. mmapped files). */
33 struct index_cache_resource
35 virtual ~index_cache_resource () = 0;
38 /* Information to be captured in the main thread, and to be used by worker
39 threads during store (). */
41 struct index_cache_store_context
43 index_cache_store_context (const index_cache &ic, dwarf2_per_bfd *per_bfd);
45 /* Store the index in the cache. */
46 void store () const;
48 private:
49 /* Captured value of enabled (). */
50 bool m_enabled;
52 /* Captured value of index cache directory. */
53 std::string m_dir;
55 /* The per-bfd object that we're caching. */
56 dwarf2_per_bfd *m_per_bfd;
58 /* Captured value of build id. */
59 std::string m_build_id_str;
61 /* Captured value of dwz build id. */
62 std::optional<std::string> m_dwz_build_id_str;
65 /* Class to manage the access to the DWARF index cache. */
67 class index_cache
69 friend struct index_cache_store_context;
70 public:
71 /* Change the directory used to save/load index files. */
72 void set_directory (std::string dir);
74 /* Return true if the usage of the cache is enabled. */
75 bool enabled () const
77 return m_enabled;
80 /* Enable the cache. */
81 void enable ();
83 /* Disable the cache. */
84 void disable ();
86 /* Look for an index file matching BUILD_ID. If found, return the contents
87 as an array_view and store the underlying resources (allocated memory,
88 mapped file, etc) in RESOURCE. The returned array_view is valid as long
89 as RESOURCE is not destroyed.
91 If no matching index file is found, return an empty array view. */
92 gdb::array_view<const gdb_byte>
93 lookup_gdb_index (const bfd_build_id *build_id,
94 std::unique_ptr<index_cache_resource> *resource);
96 /* Return the number of cache hits. */
97 unsigned int n_hits () const
98 { return m_n_hits; }
100 /* Record a cache hit. */
101 void hit ()
103 if (enabled ())
104 m_n_hits++;
107 /* Return the number of cache misses. */
108 unsigned int n_misses () const
109 { return m_n_misses; }
111 /* Record a cache miss. */
112 void miss ()
114 if (enabled ())
115 m_n_misses++;
118 private:
120 /* Compute the absolute filename where the index of the objfile with build
121 id BUILD_ID will be stored. SUFFIX is appended at the end of the
122 filename. */
123 std::string make_index_filename (const bfd_build_id *build_id,
124 const char *suffix) const;
126 /* The base directory where we are storing and looking up index files. */
127 std::string m_dir;
129 /* Whether the cache is enabled. */
130 bool m_enabled = false;
132 /* Number of cache hits and misses during this GDB session. */
133 unsigned int m_n_hits = 0;
134 unsigned int m_n_misses = 0;
137 /* The global instance of the index cache. */
138 extern index_cache global_index_cache;
140 #endif /* DWARF_INDEX_CACHE_H */