Regenerate AArch64 opcodes files
[binutils-gdb.git] / gdb / dwarf2 / index-cache.c
blobd9047ef46bb2cc5751a7634807bf4e8545de18d3
1 /* Caching of GDB/DWARF index files.
3 Copyright (C) 1994-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 #include "defs.h"
21 #include "dwarf2/index-cache.h"
23 #include "build-id.h"
24 #include "cli/cli-cmds.h"
25 #include "cli/cli-decode.h"
26 #include "command.h"
27 #include "gdbsupport/scoped_mmap.h"
28 #include "gdbsupport/pathstuff.h"
29 #include "dwarf2/index-write.h"
30 #include "dwarf2/read.h"
31 #include "dwarf2/dwz.h"
32 #include "objfiles.h"
33 #include "gdbsupport/selftest.h"
34 #include <string>
35 #include <stdlib.h>
36 #include "run-on-main-thread.h"
38 /* When set to true, show debug messages about the index cache. */
39 static bool debug_index_cache = false;
41 #define index_cache_debug(FMT, ...) \
42 debug_prefixed_printf_cond_nofunc (debug_index_cache, "index-cache", \
43 FMT, ## __VA_ARGS__)
45 /* The index cache directory, used for "set/show index-cache directory". */
46 static std::string index_cache_directory;
48 /* See dwarf-index.cache.h. */
49 index_cache global_index_cache;
51 /* set/show index-cache commands. */
52 static cmd_list_element *set_index_cache_prefix_list;
53 static cmd_list_element *show_index_cache_prefix_list;
55 /* Default destructor of index_cache_resource. */
56 index_cache_resource::~index_cache_resource () = default;
58 /* See dwarf-index-cache.h. */
60 void
61 index_cache::set_directory (std::string dir)
63 gdb_assert (!dir.empty ());
65 m_dir = std::move (dir);
67 index_cache_debug ("now using directory %s", m_dir.c_str ());
70 /* See dwarf-index-cache.h. */
72 void
73 index_cache::enable ()
75 index_cache_debug ("enabling (%s)", m_dir.c_str ());
77 m_enabled = true;
80 /* See dwarf-index-cache.h. */
82 void
83 index_cache::disable ()
85 index_cache_debug ("disabling");
87 m_enabled = false;
90 /* See index-cache.h. */
92 index_cache_store_context::index_cache_store_context (const index_cache &ic,
93 dwarf2_per_bfd *per_bfd)
94 : m_enabled (ic.enabled ()),
95 m_dir (ic.m_dir),
96 m_per_bfd (per_bfd)
98 /* Capturing globals may only be done on the main thread. */
99 gdb_assert (is_main_thread ());
101 if (!m_enabled)
102 return;
104 /* Get build id of objfile. */
105 const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
106 if (build_id == nullptr)
108 index_cache_debug ("objfile %s has no build id",
109 bfd_get_filename (per_bfd->obfd));
110 m_enabled = false;
111 return;
113 m_build_id_str = build_id_to_string (build_id);
115 /* Get build id of dwz file, if present. */
116 const dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
118 if (dwz != nullptr)
120 const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
122 if (dwz_build_id == nullptr)
124 index_cache_debug ("dwz objfile %s has no build id",
125 dwz->filename ());
126 m_enabled = false;
127 return;
130 m_dwz_build_id_str = build_id_to_string (dwz_build_id);
133 if (m_dir.empty ())
135 warning (_("The index cache directory name is empty, skipping store."));
136 m_enabled = false;
137 return;
142 /* Try to create the containing directory. */
143 if (!mkdir_recursive (m_dir.c_str ()))
145 warning (_("index cache: could not make cache directory: %s"),
146 safe_strerror (errno));
147 m_enabled = false;
148 return;
151 catch (const gdb_exception_error &except)
153 index_cache_debug ("couldn't store index cache for objfile %s: %s",
154 bfd_get_filename (per_bfd->obfd), except.what ());
155 m_enabled = false;
159 /* See dwarf-index-cache.h. */
161 void
162 index_cache_store_context::store () const
164 if (!m_enabled)
165 return;
167 const char *dwz_build_id_ptr = (m_dwz_build_id_str.has_value ()
168 ? m_dwz_build_id_str->c_str ()
169 : nullptr);
173 index_cache_debug ("writing index cache for objfile %s",
174 bfd_get_filename (m_per_bfd->obfd));
176 /* Write the index itself to the directory, using the build id as the
177 filename. */
178 write_dwarf_index (m_per_bfd, m_dir.c_str (),
179 m_build_id_str.c_str (), dwz_build_id_ptr,
180 dw_index_kind::GDB_INDEX);
182 catch (const gdb_exception_error &except)
184 index_cache_debug ("couldn't store index cache for objfile %s: %s",
185 bfd_get_filename (m_per_bfd->obfd), except.what ());
189 #if HAVE_SYS_MMAN_H
191 /* Hold the resources for an mmapped index file. */
193 struct index_cache_resource_mmap final : public index_cache_resource
195 /* Try to mmap FILENAME. Throw an exception on failure, including if the
196 file doesn't exist. */
197 index_cache_resource_mmap (const char *filename)
198 : mapping (mmap_file (filename))
201 scoped_mmap mapping;
204 /* See dwarf-index-cache.h. */
206 gdb::array_view<const gdb_byte>
207 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
208 std::unique_ptr<index_cache_resource> *resource)
210 if (!enabled ())
211 return {};
213 if (m_dir.empty ())
215 warning (_("The index cache directory name is empty, skipping cache "
216 "lookup."));
217 return {};
220 /* Compute where we would expect a gdb index file for this build id to be. */
221 std::string filename = make_index_filename (build_id, INDEX4_SUFFIX);
225 index_cache_debug ("trying to read %s",
226 filename.c_str ());
228 /* Try to map that file. */
229 index_cache_resource_mmap *mmap_resource
230 = new index_cache_resource_mmap (filename.c_str ());
232 /* Yay, it worked! Hand the resource to the caller. */
233 resource->reset (mmap_resource);
235 return gdb::array_view<const gdb_byte>
236 ((const gdb_byte *) mmap_resource->mapping.get (),
237 mmap_resource->mapping.size ());
239 catch (const gdb_exception_error &except)
241 index_cache_debug ("couldn't read %s: %s",
242 filename.c_str (), except.what ());
245 return {};
248 #else /* !HAVE_SYS_MMAN_H */
250 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
252 gdb::array_view<const gdb_byte>
253 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
254 std::unique_ptr<index_cache_resource> *resource)
256 return {};
259 #endif
261 /* See dwarf-index-cache.h. */
263 std::string
264 index_cache::make_index_filename (const bfd_build_id *build_id,
265 const char *suffix) const
267 std::string build_id_str = build_id_to_string (build_id);
269 return m_dir + SLASH_STRING + build_id_str + suffix;
272 /* True when we are executing "show index-cache". This is used to improve the
273 printout a little bit. */
274 static bool in_show_index_cache_command = false;
276 /* "show index-cache" handler. */
278 static void
279 show_index_cache_command (const char *arg, int from_tty)
281 /* Note that we are executing "show index-cache". */
282 auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
284 /* Call all "show index-cache" subcommands. */
285 cmd_show_list (show_index_cache_prefix_list, from_tty);
287 gdb_printf ("\n");
288 gdb_printf
289 (_("The index cache is currently %s.\n"),
290 global_index_cache.enabled () ? _("enabled") : _("disabled"));
293 /* "set/show index-cache enabled" set callback. */
295 static void
296 set_index_cache_enabled_command (bool value)
298 if (value)
299 global_index_cache.enable ();
300 else
301 global_index_cache.disable ();
304 /* "set/show index-cache enabled" get callback. */
306 static bool
307 get_index_cache_enabled_command ()
309 return global_index_cache.enabled ();
312 /* "set/show index-cache enabled" show callback. */
314 static void
315 show_index_cache_enabled_command (ui_file *stream, int from_tty,
316 cmd_list_element *cmd, const char *value)
318 gdb_printf (stream, _("The index cache is %s.\n"), value);
321 /* "set index-cache directory" handler. */
323 static void
324 set_index_cache_directory_command (const char *arg, int from_tty,
325 cmd_list_element *element)
327 /* Make sure the index cache directory is absolute and tilde-expanded. */
328 index_cache_directory = gdb_abspath (index_cache_directory.c_str ());
329 global_index_cache.set_directory (index_cache_directory);
332 /* "show index-cache stats" handler. */
334 static void
335 show_index_cache_stats_command (const char *arg, int from_tty)
337 const char *indent = "";
339 /* If this command is invoked through "show index-cache", make the display a
340 bit nicer. */
341 if (in_show_index_cache_command)
343 indent = " ";
344 gdb_printf ("\n");
347 gdb_printf (_("%s Cache hits (this session): %u\n"),
348 indent, global_index_cache.n_hits ());
349 gdb_printf (_("%sCache misses (this session): %u\n"),
350 indent, global_index_cache.n_misses ());
353 void _initialize_index_cache ();
354 void
355 _initialize_index_cache ()
357 /* Set the default index cache directory. */
358 std::string cache_dir = get_standard_cache_dir ();
359 if (!cache_dir.empty ())
361 index_cache_directory = cache_dir;
362 global_index_cache.set_directory (std::move (cache_dir));
364 else
365 warning (_("Couldn't determine a path for the index cache directory."));
367 /* set index-cache */
368 add_basic_prefix_cmd ("index-cache", class_files,
369 _("Set index-cache options."),
370 &set_index_cache_prefix_list,
371 false, &setlist);
373 /* show index-cache */
374 add_prefix_cmd ("index-cache", class_files, show_index_cache_command,
375 _("Show index-cache options."), &show_index_cache_prefix_list,
376 false, &showlist);
378 /* set/show index-cache enabled */
379 set_show_commands setshow_index_cache_enabled_cmds
380 = add_setshow_boolean_cmd ("enabled", class_files,
381 _("Enable the index cache."),
382 _("Show whether the index cache is enabled."),
383 _("When on, enable the use of the index cache."),
384 set_index_cache_enabled_command,
385 get_index_cache_enabled_command,
386 show_index_cache_enabled_command,
387 &set_index_cache_prefix_list,
388 &show_index_cache_prefix_list);
390 /* set index-cache on */
391 cmd_list_element *set_index_cache_on_cmd
392 = add_alias_cmd ("on", setshow_index_cache_enabled_cmds.set, class_files,
393 false, &set_index_cache_prefix_list);
394 deprecate_cmd (set_index_cache_on_cmd, "set index-cache enabled on");
395 set_index_cache_on_cmd->default_args = "on";
397 /* set index-cache off */
398 cmd_list_element *set_index_cache_off_cmd
399 = add_alias_cmd ("off", setshow_index_cache_enabled_cmds.set, class_files,
400 false, &set_index_cache_prefix_list);
401 deprecate_cmd (set_index_cache_off_cmd, "set index-cache enabled off");
402 set_index_cache_off_cmd->default_args = "off";
404 /* set index-cache directory */
405 add_setshow_filename_cmd ("directory", class_files, &index_cache_directory,
406 _("Set the directory of the index cache."),
407 _("Show the directory of the index cache."),
408 NULL,
409 set_index_cache_directory_command, NULL,
410 &set_index_cache_prefix_list,
411 &show_index_cache_prefix_list);
413 /* show index-cache stats */
414 add_cmd ("stats", class_files, show_index_cache_stats_command,
415 _("Show some stats about the index cache."),
416 &show_index_cache_prefix_list);
418 /* set debug index-cache */
419 add_setshow_boolean_cmd ("index-cache", class_maintenance,
420 &debug_index_cache,
421 _("Set display of index-cache debug messages."),
422 _("Show display of index-cache debug messages."),
423 _("\
424 When non-zero, debugging output for the index cache is displayed."),
425 NULL, NULL,
426 &setdebuglist, &showdebuglist);