1 /* Caching of GDB/DWARF index files.
3 Copyright (C) 1994-2019 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 "common/scoped_mmap.h"
27 #include "common/pathstuff.h"
28 #include "dwarf-index-write.h"
29 #include "dwarf2read.h"
31 #include "common/selftest.h"
35 /* When set to 1, show debug messages about the index cache. */
36 static int debug_index_cache = 0;
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 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
97 if (build_id == nullptr)
99 if (debug_index_cache)
100 printf_unfiltered ("index cache: objfile %s has no build id\n",
107 warning (_("The index cache directory name is empty, skipping store."));
111 std::string build_id_str = build_id_to_string (build_id);
115 /* Try to create the containing directory. */
116 if (!mkdir_recursive (m_dir.c_str ()))
118 warning (_("index cache: could not make cache directory: %s\n"),
119 safe_strerror (errno));
123 if (debug_index_cache)
124 printf_unfiltered ("index cache: writing index cache for objfile %s\n",
127 /* Write the index itself to the directory, using the build id as the
129 write_psymtabs_to_index (dwarf2_per_objfile, m_dir.c_str (),
130 build_id_str.c_str (), dw_index_kind::GDB_INDEX);
132 catch (const gdb_exception_error &except)
134 if (debug_index_cache)
135 printf_unfiltered ("index cache: couldn't store index cache for objfile "
136 "%s: %s", objfile_name (obj), except.what ());
142 /* Hold the resources for an mmapped index file. */
144 struct index_cache_resource_mmap final : public index_cache_resource
146 /* Try to mmap FILENAME. Throw an exception on failure, including if the
147 file doesn't exist. */
148 index_cache_resource_mmap (const char *filename)
149 : mapping (mmap_file (filename))
155 /* See dwarf-index-cache.h. */
157 gdb::array_view<const gdb_byte>
158 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
159 std::unique_ptr<index_cache_resource> *resource)
166 warning (_("The index cache directory name is empty, skipping cache "
171 /* Compute where we would expect a gdb index file for this build id to be. */
172 std::string filename = make_index_filename (build_id, INDEX4_SUFFIX);
176 if (debug_index_cache)
177 printf_unfiltered ("index cache: trying to read %s\n",
180 /* Try to map that file. */
181 index_cache_resource_mmap *mmap_resource
182 = new index_cache_resource_mmap (filename.c_str ());
184 /* Yay, it worked! Hand the resource to the caller. */
185 resource->reset (mmap_resource);
187 return gdb::array_view<const gdb_byte>
188 ((const gdb_byte *) mmap_resource->mapping.get (),
189 mmap_resource->mapping.size ());
191 catch (const gdb_exception_error &except)
193 if (debug_index_cache)
194 printf_unfiltered ("index cache: couldn't read %s: %s\n",
195 filename.c_str (), except.what ());
201 #else /* !HAVE_SYS_MMAN_H */
203 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
205 gdb::array_view<const gdb_byte>
206 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
207 std::unique_ptr<index_cache_resource> *resource)
214 /* See dwarf-index-cache.h. */
217 index_cache::make_index_filename (const bfd_build_id *build_id,
218 const char *suffix) const
220 std::string build_id_str = build_id_to_string (build_id);
222 return m_dir + SLASH_STRING + build_id_str + suffix;
225 /* "set index-cache" handler. */
228 set_index_cache_command (const char *arg, int from_tty)
230 printf_unfiltered (_("\
231 Missing arguments. See \"help set index-cache\" for help.\n"));
234 /* True when we are executing "show index-cache". This is used to improve the
235 printout a little bit. */
236 static bool in_show_index_cache_command = false;
238 /* "show index-cache" handler. */
241 show_index_cache_command (const char *arg, int from_tty)
243 /* Note that we are executing "show index-cache". */
244 auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
246 /* Call all "show index-cache" subcommands. */
247 cmd_show_list (show_index_cache_prefix_list, from_tty, "");
249 printf_unfiltered ("\n");
251 (_("The index cache is currently %s.\n"),
252 global_index_cache.enabled () ? _("enabled") : _("disabled"));
255 /* "set index-cache on" handler. */
258 set_index_cache_on_command (const char *arg, int from_tty)
260 global_index_cache.enable ();
263 /* "set index-cache off" handler. */
266 set_index_cache_off_command (const char *arg, int from_tty)
268 global_index_cache.disable ();
271 /* "set index-cache directory" handler. */
274 set_index_cache_directory_command (const char *arg, int from_tty,
275 cmd_list_element *element)
277 /* Make sure the index cache directory is absolute and tilde-expanded. */
278 gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (index_cache_directory));
279 xfree (index_cache_directory);
280 index_cache_directory = abs.release ();
281 global_index_cache.set_directory (index_cache_directory);
284 /* "show index-cache stats" handler. */
287 show_index_cache_stats_command (const char *arg, int from_tty)
289 const char *indent = "";
291 /* If this command is invoked through "show index-cache", make the display a
293 if (in_show_index_cache_command)
296 printf_unfiltered ("\n");
299 printf_unfiltered (_("%s Cache hits (this session): %u\n"),
300 indent, global_index_cache.n_hits ());
301 printf_unfiltered (_("%sCache misses (this session): %u\n"),
302 indent, global_index_cache.n_misses ());
306 _initialize_index_cache ()
308 /* Set the default index cache directory. */
309 std::string cache_dir = get_standard_cache_dir ();
310 if (!cache_dir.empty ())
312 index_cache_directory = xstrdup (cache_dir.c_str ());
313 global_index_cache.set_directory (std::move (cache_dir));
316 warning (_("Couldn't determine a path for the index cache directory."));
318 /* set index-cache */
319 add_prefix_cmd ("index-cache", class_files, set_index_cache_command,
320 _("Set index-cache options"), &set_index_cache_prefix_list,
321 "set index-cache ", false, &setlist);
323 /* show index-cache */
324 add_prefix_cmd ("index-cache", class_files, show_index_cache_command,
325 _("Show index-cache options"), &show_index_cache_prefix_list,
326 "show index-cache ", false, &showlist);
328 /* set index-cache on */
329 add_cmd ("on", class_files, set_index_cache_on_command,
330 _("Enable the index cache."), &set_index_cache_prefix_list);
332 /* set index-cache off */
333 add_cmd ("off", class_files, set_index_cache_off_command,
334 _("Disable the index cache."), &set_index_cache_prefix_list);
336 /* set index-cache directory */
337 add_setshow_filename_cmd ("directory", class_files, &index_cache_directory,
338 _("Set the directory of the index cache."),
339 _("Show the directory of the index cache."),
341 set_index_cache_directory_command, NULL,
342 &set_index_cache_prefix_list,
343 &show_index_cache_prefix_list);
345 /* show index-cache stats */
346 add_cmd ("stats", class_files, show_index_cache_stats_command,
347 _("Show some stats about the index cache."),
348 &show_index_cache_prefix_list);
350 /* set debug index-cache */
351 add_setshow_boolean_cmd ("index-cache", class_maintenance,
353 _("Set display of index-cache debug messages."),
354 _("Show display of index-cache debug messages."),
356 When non-zero, debugging output for the index cache is displayed."),
358 &setdebuglist, &showdebuglist);