Disambiguate info_print_options
[deliverable/binutils-gdb.git] / gdb / dwarf-index-cache.c
CommitLineData
87d6a7aa
SM
1/* Caching of GDB/DWARF index files.
2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
87d6a7aa
SM
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20#include "defs.h"
4de283e4 21#include "dwarf-index-cache.h"
87d6a7aa
SM
22
23#include "build-id.h"
24#include "cli/cli-cmds.h"
25#include "command.h"
268a13a5
TT
26#include "gdbsupport/scoped_mmap.h"
27#include "gdbsupport/pathstuff.h"
87d6a7aa
SM
28#include "dwarf-index-write.h"
29#include "dwarf2read.h"
30#include "objfiles.h"
268a13a5 31#include "gdbsupport/selftest.h"
4de283e4
TT
32#include <string>
33#include <stdlib.h>
87d6a7aa 34
491144b5
CB
35/* When set to true, show debug messages about the index cache. */
36static bool debug_index_cache = false;
87d6a7aa
SM
37
38/* The index cache directory, used for "set/show index-cache directory". */
39static char *index_cache_directory = NULL;
40
41/* See dwarf-index.cache.h. */
42index_cache global_index_cache;
43
44/* set/show index-cache commands. */
45static cmd_list_element *set_index_cache_prefix_list;
46static cmd_list_element *show_index_cache_prefix_list;
47
87d6a7aa
SM
48/* Default destructor of index_cache_resource. */
49index_cache_resource::~index_cache_resource () = default;
50
51/* See dwarf-index-cache.h. */
52
53void
54index_cache::set_directory (std::string dir)
55{
56 gdb_assert (!dir.empty ());
57
58 m_dir = std::move (dir);
59
60 if (debug_index_cache)
61 printf_unfiltered ("index cache: now using directory %s\n", m_dir.c_str ());
62}
63
64/* See dwarf-index-cache.h. */
65
66void
67index_cache::enable ()
68{
69 if (debug_index_cache)
70 printf_unfiltered ("index cache: enabling (%s)\n", m_dir.c_str ());
71
72 m_enabled = true;
73}
74
75/* See dwarf-index-cache.h. */
76
77void
78index_cache::disable ()
79{
80 if (debug_index_cache)
81 printf_unfiltered ("index cache: disabling\n");
82
83 m_enabled = false;
84}
85
86/* See dwarf-index-cache.h. */
87
88void
89index_cache::store (struct dwarf2_per_objfile *dwarf2_per_objfile)
90{
91 objfile *obj = dwarf2_per_objfile->objfile;
92
93 if (!enabled ())
94 return;
95
c4973306 96 /* Get build id of objfile. */
87d6a7aa
SM
97 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
98 if (build_id == nullptr)
99 {
100 if (debug_index_cache)
101 printf_unfiltered ("index cache: objfile %s has no build id\n",
102 objfile_name (obj));
103 return;
104 }
105
c4973306
SM
106 std::string build_id_str = build_id_to_string (build_id);
107
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;
112
113 if (dwz != nullptr)
114 {
115 const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
116
117 if (dwz_build_id == nullptr)
118 {
119 if (debug_index_cache)
120 printf_unfiltered ("index cache: dwz objfile %s has no build id\n",
121 dwz->filename ());
122 return;
123 }
124
125 dwz_build_id_str = build_id_to_string (dwz_build_id);
126 dwz_build_id_ptr = dwz_build_id_str->c_str ();
127 }
128
87d6a7aa
SM
129 if (m_dir.empty ())
130 {
131 warning (_("The index cache directory name is empty, skipping store."));
132 return;
133 }
134
a70b8144 135 try
87d6a7aa
SM
136 {
137 /* Try to create the containing directory. */
e418a61a
TT
138 if (!mkdir_recursive (m_dir.c_str ()))
139 {
422186a9 140 warning (_("index cache: could not make cache directory: %s"),
e418a61a
TT
141 safe_strerror (errno));
142 return;
143 }
87d6a7aa
SM
144
145 if (debug_index_cache)
146 printf_unfiltered ("index cache: writing index cache for objfile %s\n",
c4973306 147 objfile_name (obj));
87d6a7aa
SM
148
149 /* Write the index itself to the directory, using the build id as the
150 filename. */
151 write_psymtabs_to_index (dwarf2_per_objfile, m_dir.c_str (),
c4973306
SM
152 build_id_str.c_str (), dwz_build_id_ptr,
153 dw_index_kind::GDB_INDEX);
87d6a7aa 154 }
230d2906 155 catch (const gdb_exception_error &except)
87d6a7aa
SM
156 {
157 if (debug_index_cache)
158 printf_unfiltered ("index cache: couldn't store index cache for objfile "
3d6e9d23 159 "%s: %s", objfile_name (obj), except.what ());
87d6a7aa 160 }
87d6a7aa
SM
161}
162
163#if HAVE_SYS_MMAN_H
164
165/* Hold the resources for an mmapped index file. */
166
167struct index_cache_resource_mmap final : public index_cache_resource
168{
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))
173 {}
174
175 scoped_mmap mapping;
176};
177
178/* See dwarf-index-cache.h. */
179
180gdb::array_view<const gdb_byte>
181index_cache::lookup_gdb_index (const bfd_build_id *build_id,
182 std::unique_ptr<index_cache_resource> *resource)
183{
184 if (!enabled ())
185 return {};
186
187 if (m_dir.empty ())
188 {
189 warning (_("The index cache directory name is empty, skipping cache "
190 "lookup."));
191 return {};
192 }
193
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);
196
a70b8144 197 try
87d6a7aa
SM
198 {
199 if (debug_index_cache)
200 printf_unfiltered ("index cache: trying to read %s\n",
201 filename.c_str ());
202
203 /* Try to map that file. */
204 index_cache_resource_mmap *mmap_resource
205 = new index_cache_resource_mmap (filename.c_str ());
206
207 /* Yay, it worked! Hand the resource to the caller. */
208 resource->reset (mmap_resource);
209
210 return gdb::array_view<const gdb_byte>
211 ((const gdb_byte *) mmap_resource->mapping.get (),
212 mmap_resource->mapping.size ());
213 }
230d2906 214 catch (const gdb_exception_error &except)
87d6a7aa
SM
215 {
216 if (debug_index_cache)
217 printf_unfiltered ("index cache: couldn't read %s: %s\n",
3d6e9d23 218 filename.c_str (), except.what ());
87d6a7aa 219 }
87d6a7aa
SM
220
221 return {};
222}
223
224#else /* !HAVE_SYS_MMAN_H */
225
226/* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
227
228gdb::array_view<const gdb_byte>
229index_cache::lookup_gdb_index (const bfd_build_id *build_id,
230 std::unique_ptr<index_cache_resource> *resource)
231{
232 return {};
233}
234
235#endif
236
237/* See dwarf-index-cache.h. */
238
239std::string
240index_cache::make_index_filename (const bfd_build_id *build_id,
241 const char *suffix) const
242{
243 std::string build_id_str = build_id_to_string (build_id);
244
245 return m_dir + SLASH_STRING + build_id_str + suffix;
246}
247
248/* "set index-cache" handler. */
249
250static void
251set_index_cache_command (const char *arg, int from_tty)
252{
253 printf_unfiltered (_("\
254Missing arguments. See \"help set index-cache\" for help.\n"));
255}
256
257/* True when we are executing "show index-cache". This is used to improve the
258 printout a little bit. */
259static bool in_show_index_cache_command = false;
260
261/* "show index-cache" handler. */
262
263static void
264show_index_cache_command (const char *arg, int from_tty)
265{
266 /* Note that we are executing "show index-cache". */
267 auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
268
269 /* Call all "show index-cache" subcommands. */
270 cmd_show_list (show_index_cache_prefix_list, from_tty, "");
271
272 printf_unfiltered ("\n");
273 printf_unfiltered
274 (_("The index cache is currently %s.\n"),
275 global_index_cache.enabled () ? _("enabled") : _("disabled"));
276}
277
278/* "set index-cache on" handler. */
279
280static void
281set_index_cache_on_command (const char *arg, int from_tty)
282{
283 global_index_cache.enable ();
284}
285
286/* "set index-cache off" handler. */
287
288static void
289set_index_cache_off_command (const char *arg, int from_tty)
290{
291 global_index_cache.disable ();
292}
293
294/* "set index-cache directory" handler. */
295
296static void
297set_index_cache_directory_command (const char *arg, int from_tty,
298 cmd_list_element *element)
299{
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);
305}
306
307/* "show index-cache stats" handler. */
308
309static void
310show_index_cache_stats_command (const char *arg, int from_tty)
311{
312 const char *indent = "";
313
314 /* If this command is invoked through "show index-cache", make the display a
315 bit nicer. */
316 if (in_show_index_cache_command)
317 {
318 indent = " ";
319 printf_unfiltered ("\n");
320 }
321
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 ());
326}
327
6c265988 328void _initialize_index_cache ();
87d6a7aa
SM
329void
330_initialize_index_cache ()
331{
332 /* Set the default index cache directory. */
333 std::string cache_dir = get_standard_cache_dir ();
334 if (!cache_dir.empty ())
335 {
336 index_cache_directory = xstrdup (cache_dir.c_str ());
337 global_index_cache.set_directory (std::move (cache_dir));
338 }
339 else
340 warning (_("Couldn't determine a path for the index cache directory."));
341
342 /* set index-cache */
343 add_prefix_cmd ("index-cache", class_files, set_index_cache_command,
590042fc 344 _("Set index-cache options."), &set_index_cache_prefix_list,
87d6a7aa
SM
345 "set index-cache ", false, &setlist);
346
347 /* show index-cache */
348 add_prefix_cmd ("index-cache", class_files, show_index_cache_command,
590042fc 349 _("Show index-cache options."), &show_index_cache_prefix_list,
87d6a7aa
SM
350 "show index-cache ", false, &showlist);
351
352 /* set index-cache on */
353 add_cmd ("on", class_files, set_index_cache_on_command,
354 _("Enable the index cache."), &set_index_cache_prefix_list);
355
356 /* set index-cache off */
357 add_cmd ("off", class_files, set_index_cache_off_command,
358 _("Disable the index cache."), &set_index_cache_prefix_list);
359
360 /* set index-cache directory */
361 add_setshow_filename_cmd ("directory", class_files, &index_cache_directory,
362 _("Set the directory of the index cache."),
363 _("Show the directory of the index cache."),
364 NULL,
365 set_index_cache_directory_command, NULL,
366 &set_index_cache_prefix_list,
367 &show_index_cache_prefix_list);
368
369 /* show index-cache stats */
370 add_cmd ("stats", class_files, show_index_cache_stats_command,
371 _("Show some stats about the index cache."),
372 &show_index_cache_prefix_list);
373
374 /* set debug index-cache */
375 add_setshow_boolean_cmd ("index-cache", class_maintenance,
376 &debug_index_cache,
377 _("Set display of index-cache debug messages."),
378 _("Show display of index-cache debug messages."),
379 _("\
380When non-zero, debugging output for the index cache is displayed."),
381 NULL, NULL,
382 &setdebuglist, &showdebuglist);
87d6a7aa 383}
This page took 0.177094 seconds and 4 git commands to generate.