1 /* Caching of GDB/DWARF index files.
3 Copyright (C) 1994-2020 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/>. */
21 #include "dwarf-index-cache.h"
24 #include "cli/cli-cmds.h"
26 #include "gdbsupport/scoped_mmap.h"
27 #include "gdbsupport/pathstuff.h"
28 #include "dwarf-index-write.h"
29 #include "dwarf2read.h"
31 #include "gdbsupport/selftest.h"
35 /* When set to true, show debug messages about the index cache. */
36 static bool debug_index_cache
= false;
38 /* The index cache directory, used for "set/show index-cache directory". */
39 static char *index_cache_directory
= NULL
;
41 /* See dwarf-index.cache.h. */
42 index_cache global_index_cache
;
44 /* set/show index-cache commands. */
45 static cmd_list_element
*set_index_cache_prefix_list
;
46 static cmd_list_element
*show_index_cache_prefix_list
;
48 /* Default destructor of index_cache_resource. */
49 index_cache_resource::~index_cache_resource () = default;
51 /* See dwarf-index-cache.h. */
54 index_cache::set_directory (std::string dir
)
56 gdb_assert (!dir
.empty ());
58 m_dir
= std::move (dir
);
60 if (debug_index_cache
)
61 printf_unfiltered ("index cache: now using directory %s\n", m_dir
.c_str ());
64 /* See dwarf-index-cache.h. */
67 index_cache::enable ()
69 if (debug_index_cache
)
70 printf_unfiltered ("index cache: enabling (%s)\n", m_dir
.c_str ());
75 /* See dwarf-index-cache.h. */
78 index_cache::disable ()
80 if (debug_index_cache
)
81 printf_unfiltered ("index cache: disabling\n");
86 /* See dwarf-index-cache.h. */
89 index_cache::store (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
91 objfile
*obj
= dwarf2_per_objfile
->objfile
;
96 /* Get build id of objfile. */
97 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
98 if (build_id
== nullptr)
100 if (debug_index_cache
)
101 printf_unfiltered ("index cache: objfile %s has no build id\n",
106 std::string build_id_str
= build_id_to_string (build_id
);
108 /* Get build id of dwz file, if present. */
109 gdb::optional
<std::string
> dwz_build_id_str
;
110 const dwz_file
*dwz
= dwarf2_get_dwz_file (dwarf2_per_objfile
);
111 const char *dwz_build_id_ptr
= NULL
;
115 const bfd_build_id
*dwz_build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
117 if (dwz_build_id
== nullptr)
119 if (debug_index_cache
)
120 printf_unfiltered ("index cache: dwz objfile %s has no build id\n",
125 dwz_build_id_str
= build_id_to_string (dwz_build_id
);
126 dwz_build_id_ptr
= dwz_build_id_str
->c_str ();
131 warning (_("The index cache directory name is empty, skipping store."));
137 /* Try to create the containing directory. */
138 if (!mkdir_recursive (m_dir
.c_str ()))
140 warning (_("index cache: could not make cache directory: %s"),
141 safe_strerror (errno
));
145 if (debug_index_cache
)
146 printf_unfiltered ("index cache: writing index cache for objfile %s\n",
149 /* Write the index itself to the directory, using the build id as the
151 write_psymtabs_to_index (dwarf2_per_objfile
, m_dir
.c_str (),
152 build_id_str
.c_str (), dwz_build_id_ptr
,
153 dw_index_kind::GDB_INDEX
);
155 catch (const gdb_exception_error
&except
)
157 if (debug_index_cache
)
158 printf_unfiltered ("index cache: couldn't store index cache for objfile "
159 "%s: %s", objfile_name (obj
), except
.what ());
165 /* Hold the resources for an mmapped index file. */
167 struct index_cache_resource_mmap final
: public index_cache_resource
169 /* Try to mmap FILENAME. Throw an exception on failure, including if the
170 file doesn't exist. */
171 index_cache_resource_mmap (const char *filename
)
172 : mapping (mmap_file (filename
))
178 /* See dwarf-index-cache.h. */
180 gdb::array_view
<const gdb_byte
>
181 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
182 std::unique_ptr
<index_cache_resource
> *resource
)
189 warning (_("The index cache directory name is empty, skipping cache "
194 /* Compute where we would expect a gdb index file for this build id to be. */
195 std::string filename
= make_index_filename (build_id
, INDEX4_SUFFIX
);
199 if (debug_index_cache
)
200 printf_unfiltered ("index cache: trying to read %s\n",
203 /* Try to map that file. */
204 index_cache_resource_mmap
*mmap_resource
205 = new index_cache_resource_mmap (filename
.c_str ());
207 /* Yay, it worked! Hand the resource to the caller. */
208 resource
->reset (mmap_resource
);
210 return gdb::array_view
<const gdb_byte
>
211 ((const gdb_byte
*) mmap_resource
->mapping
.get (),
212 mmap_resource
->mapping
.size ());
214 catch (const gdb_exception_error
&except
)
216 if (debug_index_cache
)
217 printf_unfiltered ("index cache: couldn't read %s: %s\n",
218 filename
.c_str (), except
.what ());
224 #else /* !HAVE_SYS_MMAN_H */
226 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
228 gdb::array_view
<const gdb_byte
>
229 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
230 std::unique_ptr
<index_cache_resource
> *resource
)
237 /* See dwarf-index-cache.h. */
240 index_cache::make_index_filename (const bfd_build_id
*build_id
,
241 const char *suffix
) const
243 std::string build_id_str
= build_id_to_string (build_id
);
245 return m_dir
+ SLASH_STRING
+ build_id_str
+ suffix
;
248 /* "set index-cache" handler. */
251 set_index_cache_command (const char *arg
, int from_tty
)
253 printf_unfiltered (_("\
254 Missing arguments. See \"help set index-cache\" for help.\n"));
257 /* True when we are executing "show index-cache". This is used to improve the
258 printout a little bit. */
259 static bool in_show_index_cache_command
= false;
261 /* "show index-cache" handler. */
264 show_index_cache_command (const char *arg
, int from_tty
)
266 /* Note that we are executing "show index-cache". */
267 auto restore_flag
= make_scoped_restore (&in_show_index_cache_command
, true);
269 /* Call all "show index-cache" subcommands. */
270 cmd_show_list (show_index_cache_prefix_list
, from_tty
, "");
272 printf_unfiltered ("\n");
274 (_("The index cache is currently %s.\n"),
275 global_index_cache
.enabled () ? _("enabled") : _("disabled"));
278 /* "set index-cache on" handler. */
281 set_index_cache_on_command (const char *arg
, int from_tty
)
283 global_index_cache
.enable ();
286 /* "set index-cache off" handler. */
289 set_index_cache_off_command (const char *arg
, int from_tty
)
291 global_index_cache
.disable ();
294 /* "set index-cache directory" handler. */
297 set_index_cache_directory_command (const char *arg
, int from_tty
,
298 cmd_list_element
*element
)
300 /* Make sure the index cache directory is absolute and tilde-expanded. */
301 gdb::unique_xmalloc_ptr
<char> abs (gdb_abspath (index_cache_directory
));
302 xfree (index_cache_directory
);
303 index_cache_directory
= abs
.release ();
304 global_index_cache
.set_directory (index_cache_directory
);
307 /* "show index-cache stats" handler. */
310 show_index_cache_stats_command (const char *arg
, int from_tty
)
312 const char *indent
= "";
314 /* If this command is invoked through "show index-cache", make the display a
316 if (in_show_index_cache_command
)
319 printf_unfiltered ("\n");
322 printf_unfiltered (_("%s Cache hits (this session): %u\n"),
323 indent
, global_index_cache
.n_hits ());
324 printf_unfiltered (_("%sCache misses (this session): %u\n"),
325 indent
, global_index_cache
.n_misses ());
329 _initialize_index_cache ()
331 /* Set the default index cache directory. */
332 std::string cache_dir
= get_standard_cache_dir ();
333 if (!cache_dir
.empty ())
335 index_cache_directory
= xstrdup (cache_dir
.c_str ());
336 global_index_cache
.set_directory (std::move (cache_dir
));
339 warning (_("Couldn't determine a path for the index cache directory."));
341 /* set index-cache */
342 add_prefix_cmd ("index-cache", class_files
, set_index_cache_command
,
343 _("Set index-cache options."), &set_index_cache_prefix_list
,
344 "set index-cache ", false, &setlist
);
346 /* show index-cache */
347 add_prefix_cmd ("index-cache", class_files
, show_index_cache_command
,
348 _("Show index-cache options."), &show_index_cache_prefix_list
,
349 "show index-cache ", false, &showlist
);
351 /* set index-cache on */
352 add_cmd ("on", class_files
, set_index_cache_on_command
,
353 _("Enable the index cache."), &set_index_cache_prefix_list
);
355 /* set index-cache off */
356 add_cmd ("off", class_files
, set_index_cache_off_command
,
357 _("Disable the index cache."), &set_index_cache_prefix_list
);
359 /* set index-cache directory */
360 add_setshow_filename_cmd ("directory", class_files
, &index_cache_directory
,
361 _("Set the directory of the index cache."),
362 _("Show the directory of the index cache."),
364 set_index_cache_directory_command
, NULL
,
365 &set_index_cache_prefix_list
,
366 &show_index_cache_prefix_list
);
368 /* show index-cache stats */
369 add_cmd ("stats", class_files
, show_index_cache_stats_command
,
370 _("Show some stats about the index cache."),
371 &show_index_cache_prefix_list
);
373 /* set debug index-cache */
374 add_setshow_boolean_cmd ("index-cache", class_maintenance
,
376 _("Set display of index-cache debug messages."),
377 _("Show display of index-cache debug messages."),
379 When non-zero, debugging output for the index cache is displayed."),
381 &setdebuglist
, &showdebuglist
);