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 "dwarf2/index-cache.h"
24 #include "cli/cli-cmds.h"
26 #include "gdbsupport/scoped_mmap.h"
27 #include "gdbsupport/pathstuff.h"
28 #include "dwarf2/index-write.h"
29 #include "dwarf2/read.h"
30 #include "dwarf2/dwz.h"
32 #include "gdbsupport/selftest.h"
36 /* When set to true, show debug messages about the index cache. */
37 static bool debug_index_cache
= false;
39 /* The index cache directory, used for "set/show index-cache directory". */
40 static char *index_cache_directory
= NULL
;
42 /* See dwarf-index.cache.h. */
43 index_cache global_index_cache
;
45 /* set/show index-cache commands. */
46 static cmd_list_element
*set_index_cache_prefix_list
;
47 static cmd_list_element
*show_index_cache_prefix_list
;
49 /* Default destructor of index_cache_resource. */
50 index_cache_resource::~index_cache_resource () = default;
52 /* See dwarf-index-cache.h. */
55 index_cache::set_directory (std::string dir
)
57 gdb_assert (!dir
.empty ());
59 m_dir
= std::move (dir
);
61 if (debug_index_cache
)
62 printf_unfiltered ("index cache: now using directory %s\n", m_dir
.c_str ());
65 /* See dwarf-index-cache.h. */
68 index_cache::enable ()
70 if (debug_index_cache
)
71 printf_unfiltered ("index cache: enabling (%s)\n", m_dir
.c_str ());
76 /* See dwarf-index-cache.h. */
79 index_cache::disable ()
81 if (debug_index_cache
)
82 printf_unfiltered ("index cache: disabling\n");
87 /* See dwarf-index-cache.h. */
90 index_cache::store (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
92 objfile
*obj
= dwarf2_per_objfile
->objfile
;
97 /* Get build id of objfile. */
98 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
99 if (build_id
== nullptr)
101 if (debug_index_cache
)
102 printf_unfiltered ("index cache: objfile %s has no build id\n",
107 std::string build_id_str
= build_id_to_string (build_id
);
109 /* Get build id of dwz file, if present. */
110 gdb::optional
<std::string
> dwz_build_id_str
;
111 const dwz_file
*dwz
= dwarf2_get_dwz_file (dwarf2_per_objfile
);
112 const char *dwz_build_id_ptr
= NULL
;
116 const bfd_build_id
*dwz_build_id
= build_id_bfd_get (dwz
->dwz_bfd
.get ());
118 if (dwz_build_id
== nullptr)
120 if (debug_index_cache
)
121 printf_unfiltered ("index cache: dwz objfile %s has no build id\n",
126 dwz_build_id_str
= build_id_to_string (dwz_build_id
);
127 dwz_build_id_ptr
= dwz_build_id_str
->c_str ();
132 warning (_("The index cache directory name is empty, skipping store."));
138 /* Try to create the containing directory. */
139 if (!mkdir_recursive (m_dir
.c_str ()))
141 warning (_("index cache: could not make cache directory: %s"),
142 safe_strerror (errno
));
146 if (debug_index_cache
)
147 printf_unfiltered ("index cache: writing index cache for objfile %s\n",
150 /* Write the index itself to the directory, using the build id as the
152 write_psymtabs_to_index (dwarf2_per_objfile
, m_dir
.c_str (),
153 build_id_str
.c_str (), dwz_build_id_ptr
,
154 dw_index_kind::GDB_INDEX
);
156 catch (const gdb_exception_error
&except
)
158 if (debug_index_cache
)
159 printf_unfiltered ("index cache: couldn't store index cache for objfile "
160 "%s: %s", objfile_name (obj
), except
.what ());
166 /* Hold the resources for an mmapped index file. */
168 struct index_cache_resource_mmap final
: public index_cache_resource
170 /* Try to mmap FILENAME. Throw an exception on failure, including if the
171 file doesn't exist. */
172 index_cache_resource_mmap (const char *filename
)
173 : mapping (mmap_file (filename
))
179 /* See dwarf-index-cache.h. */
181 gdb::array_view
<const gdb_byte
>
182 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
183 std::unique_ptr
<index_cache_resource
> *resource
)
190 warning (_("The index cache directory name is empty, skipping cache "
195 /* Compute where we would expect a gdb index file for this build id to be. */
196 std::string filename
= make_index_filename (build_id
, INDEX4_SUFFIX
);
200 if (debug_index_cache
)
201 printf_unfiltered ("index cache: trying to read %s\n",
204 /* Try to map that file. */
205 index_cache_resource_mmap
*mmap_resource
206 = new index_cache_resource_mmap (filename
.c_str ());
208 /* Yay, it worked! Hand the resource to the caller. */
209 resource
->reset (mmap_resource
);
211 return gdb::array_view
<const gdb_byte
>
212 ((const gdb_byte
*) mmap_resource
->mapping
.get (),
213 mmap_resource
->mapping
.size ());
215 catch (const gdb_exception_error
&except
)
217 if (debug_index_cache
)
218 printf_unfiltered ("index cache: couldn't read %s: %s\n",
219 filename
.c_str (), except
.what ());
225 #else /* !HAVE_SYS_MMAN_H */
227 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
229 gdb::array_view
<const gdb_byte
>
230 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
231 std::unique_ptr
<index_cache_resource
> *resource
)
238 /* See dwarf-index-cache.h. */
241 index_cache::make_index_filename (const bfd_build_id
*build_id
,
242 const char *suffix
) const
244 std::string build_id_str
= build_id_to_string (build_id
);
246 return m_dir
+ SLASH_STRING
+ build_id_str
+ suffix
;
249 /* "set index-cache" handler. */
252 set_index_cache_command (const char *arg
, int from_tty
)
254 printf_unfiltered (_("\
255 Missing arguments. See \"help set index-cache\" for help.\n"));
258 /* True when we are executing "show index-cache". This is used to improve the
259 printout a little bit. */
260 static bool in_show_index_cache_command
= false;
262 /* "show index-cache" handler. */
265 show_index_cache_command (const char *arg
, int from_tty
)
267 /* Note that we are executing "show index-cache". */
268 auto restore_flag
= make_scoped_restore (&in_show_index_cache_command
, true);
270 /* Call all "show index-cache" subcommands. */
271 cmd_show_list (show_index_cache_prefix_list
, from_tty
, "");
273 printf_unfiltered ("\n");
275 (_("The index cache is currently %s.\n"),
276 global_index_cache
.enabled () ? _("enabled") : _("disabled"));
279 /* "set index-cache on" handler. */
282 set_index_cache_on_command (const char *arg
, int from_tty
)
284 global_index_cache
.enable ();
287 /* "set index-cache off" handler. */
290 set_index_cache_off_command (const char *arg
, int from_tty
)
292 global_index_cache
.disable ();
295 /* "set index-cache directory" handler. */
298 set_index_cache_directory_command (const char *arg
, int from_tty
,
299 cmd_list_element
*element
)
301 /* Make sure the index cache directory is absolute and tilde-expanded. */
302 gdb::unique_xmalloc_ptr
<char> abs (gdb_abspath (index_cache_directory
));
303 xfree (index_cache_directory
);
304 index_cache_directory
= abs
.release ();
305 global_index_cache
.set_directory (index_cache_directory
);
308 /* "show index-cache stats" handler. */
311 show_index_cache_stats_command (const char *arg
, int from_tty
)
313 const char *indent
= "";
315 /* If this command is invoked through "show index-cache", make the display a
317 if (in_show_index_cache_command
)
320 printf_unfiltered ("\n");
323 printf_unfiltered (_("%s Cache hits (this session): %u\n"),
324 indent
, global_index_cache
.n_hits ());
325 printf_unfiltered (_("%sCache misses (this session): %u\n"),
326 indent
, global_index_cache
.n_misses ());
329 void _initialize_index_cache ();
331 _initialize_index_cache ()
333 /* Set the default index cache directory. */
334 std::string cache_dir
= get_standard_cache_dir ();
335 if (!cache_dir
.empty ())
337 index_cache_directory
= xstrdup (cache_dir
.c_str ());
338 global_index_cache
.set_directory (std::move (cache_dir
));
341 warning (_("Couldn't determine a path for the index cache directory."));
343 /* set index-cache */
344 add_prefix_cmd ("index-cache", class_files
, set_index_cache_command
,
345 _("Set index-cache options."), &set_index_cache_prefix_list
,
346 "set index-cache ", false, &setlist
);
348 /* show index-cache */
349 add_prefix_cmd ("index-cache", class_files
, show_index_cache_command
,
350 _("Show index-cache options."), &show_index_cache_prefix_list
,
351 "show index-cache ", false, &showlist
);
353 /* set index-cache on */
354 add_cmd ("on", class_files
, set_index_cache_on_command
,
355 _("Enable the index cache."), &set_index_cache_prefix_list
);
357 /* set index-cache off */
358 add_cmd ("off", class_files
, set_index_cache_off_command
,
359 _("Disable the index cache."), &set_index_cache_prefix_list
);
361 /* set index-cache directory */
362 add_setshow_filename_cmd ("directory", class_files
, &index_cache_directory
,
363 _("Set the directory of the index cache."),
364 _("Show the directory of the index cache."),
366 set_index_cache_directory_command
, NULL
,
367 &set_index_cache_prefix_list
,
368 &show_index_cache_prefix_list
);
370 /* show index-cache stats */
371 add_cmd ("stats", class_files
, show_index_cache_stats_command
,
372 _("Show some stats about the index cache."),
373 &show_index_cache_prefix_list
);
375 /* set debug index-cache */
376 add_setshow_boolean_cmd ("index-cache", class_maintenance
,
378 _("Set display of index-cache debug messages."),
379 _("Show display of index-cache debug messages."),
381 When non-zero, debugging output for the index cache is displayed."),
383 &setdebuglist
, &showdebuglist
);