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