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"
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 (except
, RETURN_MASK_ERROR
)
134 if (debug_index_cache
)
135 printf_unfiltered ("index cache: couldn't store index cache for objfile "
136 "%s: %s", objfile_name (obj
), except
.message
);
143 /* Hold the resources for an mmapped index file. */
145 struct index_cache_resource_mmap final
: public index_cache_resource
147 /* Try to mmap FILENAME. Throw an exception on failure, including if the
148 file doesn't exist. */
149 index_cache_resource_mmap (const char *filename
)
150 : mapping (mmap_file (filename
))
156 /* See dwarf-index-cache.h. */
158 gdb::array_view
<const gdb_byte
>
159 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
160 std::unique_ptr
<index_cache_resource
> *resource
)
167 warning (_("The index cache directory name is empty, skipping cache "
172 /* Compute where we would expect a gdb index file for this build id to be. */
173 std::string filename
= make_index_filename (build_id
, INDEX4_SUFFIX
);
177 if (debug_index_cache
)
178 printf_unfiltered ("index cache: trying to read %s\n",
181 /* Try to map that file. */
182 index_cache_resource_mmap
*mmap_resource
183 = new index_cache_resource_mmap (filename
.c_str ());
185 /* Yay, it worked! Hand the resource to the caller. */
186 resource
->reset (mmap_resource
);
188 return gdb::array_view
<const gdb_byte
>
189 ((const gdb_byte
*) mmap_resource
->mapping
.get (),
190 mmap_resource
->mapping
.size ());
192 CATCH (except
, RETURN_MASK_ERROR
)
194 if (debug_index_cache
)
195 printf_unfiltered ("index cache: couldn't read %s: %s\n",
196 filename
.c_str (), except
.message
);
203 #else /* !HAVE_SYS_MMAN_H */
205 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
207 gdb::array_view
<const gdb_byte
>
208 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
209 std::unique_ptr
<index_cache_resource
> *resource
)
216 /* See dwarf-index-cache.h. */
219 index_cache::make_index_filename (const bfd_build_id
*build_id
,
220 const char *suffix
) const
222 std::string build_id_str
= build_id_to_string (build_id
);
224 return m_dir
+ SLASH_STRING
+ build_id_str
+ suffix
;
227 /* "set index-cache" handler. */
230 set_index_cache_command (const char *arg
, int from_tty
)
232 printf_unfiltered (_("\
233 Missing arguments. See \"help set index-cache\" for help.\n"));
236 /* True when we are executing "show index-cache". This is used to improve the
237 printout a little bit. */
238 static bool in_show_index_cache_command
= false;
240 /* "show index-cache" handler. */
243 show_index_cache_command (const char *arg
, int from_tty
)
245 /* Note that we are executing "show index-cache". */
246 auto restore_flag
= make_scoped_restore (&in_show_index_cache_command
, true);
248 /* Call all "show index-cache" subcommands. */
249 cmd_show_list (show_index_cache_prefix_list
, from_tty
, "");
251 printf_unfiltered ("\n");
253 (_("The index cache is currently %s.\n"),
254 global_index_cache
.enabled () ? _("enabled") : _("disabled"));
257 /* "set index-cache on" handler. */
260 set_index_cache_on_command (const char *arg
, int from_tty
)
262 global_index_cache
.enable ();
265 /* "set index-cache off" handler. */
268 set_index_cache_off_command (const char *arg
, int from_tty
)
270 global_index_cache
.disable ();
273 /* "set index-cache directory" handler. */
276 set_index_cache_directory_command (const char *arg
, int from_tty
,
277 cmd_list_element
*element
)
279 /* Make sure the index cache directory is absolute and tilde-expanded. */
280 gdb::unique_xmalloc_ptr
<char> abs (gdb_abspath (index_cache_directory
));
281 xfree (index_cache_directory
);
282 index_cache_directory
= abs
.release ();
283 global_index_cache
.set_directory (index_cache_directory
);
286 /* "show index-cache stats" handler. */
289 show_index_cache_stats_command (const char *arg
, int from_tty
)
291 const char *indent
= "";
293 /* If this command is invoked through "show index-cache", make the display a
295 if (in_show_index_cache_command
)
298 printf_unfiltered ("\n");
301 printf_unfiltered (_("%s Cache hits (this session): %u\n"),
302 indent
, global_index_cache
.n_hits ());
303 printf_unfiltered (_("%sCache misses (this session): %u\n"),
304 indent
, global_index_cache
.n_misses ());
308 _initialize_index_cache ()
310 /* Set the default index cache directory. */
311 std::string cache_dir
= get_standard_cache_dir ();
312 if (!cache_dir
.empty ())
314 index_cache_directory
= xstrdup (cache_dir
.c_str ());
315 global_index_cache
.set_directory (std::move (cache_dir
));
318 warning (_("Couldn't determine a path for the index cache directory."));
320 /* set index-cache */
321 add_prefix_cmd ("index-cache", class_files
, set_index_cache_command
,
322 _("Set index-cache options"), &set_index_cache_prefix_list
,
323 "set index-cache ", false, &setlist
);
325 /* show index-cache */
326 add_prefix_cmd ("index-cache", class_files
, show_index_cache_command
,
327 _("Show index-cache options"), &show_index_cache_prefix_list
,
328 "show index-cache ", false, &showlist
);
330 /* set index-cache on */
331 add_cmd ("on", class_files
, set_index_cache_on_command
,
332 _("Enable the index cache."), &set_index_cache_prefix_list
);
334 /* set index-cache off */
335 add_cmd ("off", class_files
, set_index_cache_off_command
,
336 _("Disable the index cache."), &set_index_cache_prefix_list
);
338 /* set index-cache directory */
339 add_setshow_filename_cmd ("directory", class_files
, &index_cache_directory
,
340 _("Set the directory of the index cache."),
341 _("Show the directory of the index cache."),
343 set_index_cache_directory_command
, NULL
,
344 &set_index_cache_prefix_list
,
345 &show_index_cache_prefix_list
);
347 /* show index-cache stats */
348 add_cmd ("stats", class_files
, show_index_cache_stats_command
,
349 _("Show some stats about the index cache."),
350 &show_index_cache_prefix_list
);
352 /* set debug index-cache */
353 add_setshow_boolean_cmd ("index-cache", class_maintenance
,
355 _("Set display of index-cache debug messages."),
356 _("Show display of index-cache debug messages."),
358 When non-zero, debugging output for the index cache is displayed."),
360 &setdebuglist
, &showdebuglist
);