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/>. */
22 /* Standard C includes. */
25 /* Standard C++ includes. */
28 /* Local non-gdb includes. */
30 #include "cli/cli-cmds.h"
32 #include "common/pathstuff.h"
33 #include "common/scoped_mmap.h"
34 #include "common/selftest.h"
35 #include "dwarf-index-cache.h"
36 #include "dwarf-index-write.h"
37 #include "dwarf2read.h"
40 /* When set to 1, show debug messages about the index cache. */
41 static int debug_index_cache
= 0;
43 /* The index cache directory, used for "set/show index-cache directory". */
44 static char *index_cache_directory
= NULL
;
46 /* See dwarf-index.cache.h. */
47 index_cache global_index_cache
;
49 /* set/show index-cache commands. */
50 static cmd_list_element
*set_index_cache_prefix_list
;
51 static cmd_list_element
*show_index_cache_prefix_list
;
53 /* Default destructor of index_cache_resource. */
54 index_cache_resource::~index_cache_resource () = default;
56 /* See dwarf-index-cache.h. */
59 index_cache::set_directory (std::string dir
)
61 gdb_assert (!dir
.empty ());
63 m_dir
= std::move (dir
);
65 if (debug_index_cache
)
66 printf_unfiltered ("index cache: now using directory %s\n", m_dir
.c_str ());
69 /* See dwarf-index-cache.h. */
72 index_cache::enable ()
74 if (debug_index_cache
)
75 printf_unfiltered ("index cache: enabling (%s)\n", m_dir
.c_str ());
80 /* See dwarf-index-cache.h. */
83 index_cache::disable ()
85 if (debug_index_cache
)
86 printf_unfiltered ("index cache: disabling\n");
91 /* See dwarf-index-cache.h. */
94 index_cache::store (struct dwarf2_per_objfile
*dwarf2_per_objfile
)
96 objfile
*obj
= dwarf2_per_objfile
->objfile
;
101 const bfd_build_id
*build_id
= build_id_bfd_get (obj
->obfd
);
102 if (build_id
== nullptr)
104 if (debug_index_cache
)
105 printf_unfiltered ("index cache: objfile %s has no build id\n",
112 warning (_("The index cache directory name is empty, skipping store."));
116 std::string build_id_str
= build_id_to_string (build_id
);
120 /* Try to create the containing directory. */
121 if (!mkdir_recursive (m_dir
.c_str ()))
123 warning (_("index cache: could not make cache directory: %s\n"),
124 safe_strerror (errno
));
128 if (debug_index_cache
)
129 printf_unfiltered ("index cache: writing index cache for objfile %s\n",
132 /* Write the index itself to the directory, using the build id as the
134 write_psymtabs_to_index (dwarf2_per_objfile
, m_dir
.c_str (),
135 build_id_str
.c_str (), dw_index_kind::GDB_INDEX
);
137 CATCH (except
, RETURN_MASK_ERROR
)
139 if (debug_index_cache
)
140 printf_unfiltered ("index cache: couldn't store index cache for objfile "
141 "%s: %s", objfile_name (obj
), except
.message
);
148 /* Hold the resources for an mmapped index file. */
150 struct index_cache_resource_mmap final
: public index_cache_resource
152 /* Try to mmap FILENAME. Throw an exception on failure, including if the
153 file doesn't exist. */
154 index_cache_resource_mmap (const char *filename
)
155 : mapping (mmap_file (filename
))
161 /* See dwarf-index-cache.h. */
163 gdb::array_view
<const gdb_byte
>
164 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
165 std::unique_ptr
<index_cache_resource
> *resource
)
172 warning (_("The index cache directory name is empty, skipping cache "
177 /* Compute where we would expect a gdb index file for this build id to be. */
178 std::string filename
= make_index_filename (build_id
, INDEX4_SUFFIX
);
182 if (debug_index_cache
)
183 printf_unfiltered ("index cache: trying to read %s\n",
186 /* Try to map that file. */
187 index_cache_resource_mmap
*mmap_resource
188 = new index_cache_resource_mmap (filename
.c_str ());
190 /* Yay, it worked! Hand the resource to the caller. */
191 resource
->reset (mmap_resource
);
193 return gdb::array_view
<const gdb_byte
>
194 ((const gdb_byte
*) mmap_resource
->mapping
.get (),
195 mmap_resource
->mapping
.size ());
197 CATCH (except
, RETURN_MASK_ERROR
)
199 if (debug_index_cache
)
200 printf_unfiltered ("index cache: couldn't read %s: %s\n",
201 filename
.c_str (), except
.message
);
208 #else /* !HAVE_SYS_MMAN_H */
210 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
212 gdb::array_view
<const gdb_byte
>
213 index_cache::lookup_gdb_index (const bfd_build_id
*build_id
,
214 std::unique_ptr
<index_cache_resource
> *resource
)
221 /* See dwarf-index-cache.h. */
224 index_cache::make_index_filename (const bfd_build_id
*build_id
,
225 const char *suffix
) const
227 std::string build_id_str
= build_id_to_string (build_id
);
229 return m_dir
+ SLASH_STRING
+ build_id_str
+ suffix
;
232 /* "set index-cache" handler. */
235 set_index_cache_command (const char *arg
, int from_tty
)
237 printf_unfiltered (_("\
238 Missing arguments. See \"help set index-cache\" for help.\n"));
241 /* True when we are executing "show index-cache". This is used to improve the
242 printout a little bit. */
243 static bool in_show_index_cache_command
= false;
245 /* "show index-cache" handler. */
248 show_index_cache_command (const char *arg
, int from_tty
)
250 /* Note that we are executing "show index-cache". */
251 auto restore_flag
= make_scoped_restore (&in_show_index_cache_command
, true);
253 /* Call all "show index-cache" subcommands. */
254 cmd_show_list (show_index_cache_prefix_list
, from_tty
, "");
256 printf_unfiltered ("\n");
258 (_("The index cache is currently %s.\n"),
259 global_index_cache
.enabled () ? _("enabled") : _("disabled"));
262 /* "set index-cache on" handler. */
265 set_index_cache_on_command (const char *arg
, int from_tty
)
267 global_index_cache
.enable ();
270 /* "set index-cache off" handler. */
273 set_index_cache_off_command (const char *arg
, int from_tty
)
275 global_index_cache
.disable ();
278 /* "set index-cache directory" handler. */
281 set_index_cache_directory_command (const char *arg
, int from_tty
,
282 cmd_list_element
*element
)
284 /* Make sure the index cache directory is absolute and tilde-expanded. */
285 gdb::unique_xmalloc_ptr
<char> abs (gdb_abspath (index_cache_directory
));
286 xfree (index_cache_directory
);
287 index_cache_directory
= abs
.release ();
288 global_index_cache
.set_directory (index_cache_directory
);
291 /* "show index-cache stats" handler. */
294 show_index_cache_stats_command (const char *arg
, int from_tty
)
296 const char *indent
= "";
298 /* If this command is invoked through "show index-cache", make the display a
300 if (in_show_index_cache_command
)
303 printf_unfiltered ("\n");
306 printf_unfiltered (_("%s Cache hits (this session): %u\n"),
307 indent
, global_index_cache
.n_hits ());
308 printf_unfiltered (_("%sCache misses (this session): %u\n"),
309 indent
, global_index_cache
.n_misses ());
313 _initialize_index_cache ()
315 /* Set the default index cache directory. */
316 std::string cache_dir
= get_standard_cache_dir ();
317 if (!cache_dir
.empty ())
319 index_cache_directory
= xstrdup (cache_dir
.c_str ());
320 global_index_cache
.set_directory (std::move (cache_dir
));
323 warning (_("Couldn't determine a path for the index cache directory."));
325 /* set index-cache */
326 add_prefix_cmd ("index-cache", class_files
, set_index_cache_command
,
327 _("Set index-cache options"), &set_index_cache_prefix_list
,
328 "set index-cache ", false, &setlist
);
330 /* show index-cache */
331 add_prefix_cmd ("index-cache", class_files
, show_index_cache_command
,
332 _("Show index-cache options"), &show_index_cache_prefix_list
,
333 "show index-cache ", false, &showlist
);
335 /* set index-cache on */
336 add_cmd ("on", class_files
, set_index_cache_on_command
,
337 _("Enable the index cache."), &set_index_cache_prefix_list
);
339 /* set index-cache off */
340 add_cmd ("off", class_files
, set_index_cache_off_command
,
341 _("Disable the index cache."), &set_index_cache_prefix_list
);
343 /* set index-cache directory */
344 add_setshow_filename_cmd ("directory", class_files
, &index_cache_directory
,
345 _("Set the directory of the index cache."),
346 _("Show the directory of the index cache."),
348 set_index_cache_directory_command
, NULL
,
349 &set_index_cache_prefix_list
,
350 &show_index_cache_prefix_list
);
352 /* show index-cache stats */
353 add_cmd ("stats", class_files
, show_index_cache_stats_command
,
354 _("Show some stats about the index cache."),
355 &show_index_cache_prefix_list
);
357 /* set debug index-cache */
358 add_setshow_boolean_cmd ("index-cache", class_maintenance
,
360 _("Set display of index-cache debug messages."),
361 _("Show display of index-cache debug messages."),
363 When non-zero, debugging output for the index cache is displayed."),
365 &setdebuglist
, &showdebuglist
);