GDB (xrefs)
Loading...
Searching...
No Matches
index-cache.c
Go to the documentation of this file.
1/* Caching of GDB/DWARF index files.
2
3 Copyright (C) 1994-2023 Free Software Foundation, Inc.
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"
21#include "dwarf2/index-cache.h"
22
23#include "build-id.h"
24#include "cli/cli-cmds.h"
25#include "cli/cli-decode.h"
26#include "command.h"
27#include "gdbsupport/scoped_mmap.h"
28#include "gdbsupport/pathstuff.h"
29#include "dwarf2/index-write.h"
30#include "dwarf2/read.h"
31#include "dwarf2/dwz.h"
32#include "objfiles.h"
33#include "gdbsupport/selftest.h"
34#include <string>
35#include <stdlib.h>
36
37/* When set to true, show debug messages about the index cache. */
38static bool debug_index_cache = false;
39
40#define index_cache_debug(FMT, ...) \
41 debug_prefixed_printf_cond_nofunc (debug_index_cache, "index-cache", \
42 FMT, ## __VA_ARGS__)
43
44/* The index cache directory, used for "set/show index-cache directory". */
45static std::string index_cache_directory;
46
47/* See dwarf-index.cache.h. */
49
50/* set/show index-cache commands. */
53
54/* Default destructor of index_cache_resource. */
56
57/* See dwarf-index-cache.h. */
58
59void
61{
62 gdb_assert (!dir.empty ());
63
64 m_dir = std::move (dir);
65
66 index_cache_debug ("now using directory %s", m_dir.c_str ());
67}
68
69/* See dwarf-index-cache.h. */
70
71void
73{
74 index_cache_debug ("enabling (%s)", m_dir.c_str ());
75
76 m_enabled = true;
77}
78
79/* See dwarf-index-cache.h. */
80
81void
83{
84 index_cache_debug ("disabling");
85
86 m_enabled = false;
87}
88
89/* See dwarf-index-cache.h. */
90
91void
93{
94 objfile *obj = per_objfile->objfile;
95
96 if (!enabled ())
97 return;
98
99 /* If the objfile does not correspond to an actual file, skip it. */
100 if ((obj->flags & OBJF_NOT_FILENAME) != 0)
101 return;
102
103 /* Get build id of objfile. */
104 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd.get ());
105 if (build_id == nullptr)
106 {
107 index_cache_debug ("objfile %s has no build id",
108 objfile_name (obj));
109 return;
110 }
111
112 std::string build_id_str = build_id_to_string (build_id);
113
114 /* Get build id of dwz file, if present. */
115 gdb::optional<std::string> dwz_build_id_str;
116 const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
117 const char *dwz_build_id_ptr = NULL;
118
119 if (dwz != nullptr)
120 {
121 const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
122
123 if (dwz_build_id == nullptr)
124 {
125 index_cache_debug ("dwz objfile %s has no build id",
126 dwz->filename ());
127 return;
128 }
129
130 dwz_build_id_str = build_id_to_string (dwz_build_id);
131 dwz_build_id_ptr = dwz_build_id_str->c_str ();
132 }
133
134 if (m_dir.empty ())
135 {
136 warning (_("The index cache directory name is empty, skipping store."));
137 return;
138 }
139
140 try
141 {
142 /* Try to create the containing directory. */
143 if (!mkdir_recursive (m_dir.c_str ()))
144 {
145 warning (_("index cache: could not make cache directory: %s"),
146 safe_strerror (errno));
147 return;
148 }
149
150 index_cache_debug ("writing index cache for objfile %s",
151 objfile_name (obj));
152
153 /* Write the index itself to the directory, using the build id as the
154 filename. */
155 write_dwarf_index (per_objfile, m_dir.c_str (),
156 build_id_str.c_str (), dwz_build_id_ptr,
157 dw_index_kind::GDB_INDEX);
158 }
159 catch (const gdb_exception_error &except)
160 {
161 index_cache_debug ("couldn't store index cache for objfile %s: %s",
162 objfile_name (obj), except.what ());
163 }
164}
165
166#if HAVE_SYS_MMAN_H
167
168/* Hold the resources for an mmapped index file. */
169
170struct index_cache_resource_mmap final : public index_cache_resource
171{
172 /* Try to mmap FILENAME. Throw an exception on failure, including if the
173 file doesn't exist. */
174 index_cache_resource_mmap (const char *filename)
175 : mapping (mmap_file (filename))
176 {}
177
178 scoped_mmap mapping;
179};
180
181/* See dwarf-index-cache.h. */
182
183gdb::array_view<const gdb_byte>
184index_cache::lookup_gdb_index (const bfd_build_id *build_id,
185 std::unique_ptr<index_cache_resource> *resource)
186{
187 if (!enabled ())
188 return {};
189
190 if (m_dir.empty ())
191 {
192 warning (_("The index cache directory name is empty, skipping cache "
193 "lookup."));
194 return {};
195 }
196
197 /* Compute where we would expect a gdb index file for this build id to be. */
198 std::string filename = make_index_filename (build_id, INDEX4_SUFFIX);
199
200 try
201 {
202 index_cache_debug ("trying to read %s",
203 filename.c_str ());
204
205 /* Try to map that file. */
206 index_cache_resource_mmap *mmap_resource
207 = new index_cache_resource_mmap (filename.c_str ());
208
209 /* Yay, it worked! Hand the resource to the caller. */
210 resource->reset (mmap_resource);
211
212 return gdb::array_view<const gdb_byte>
213 ((const gdb_byte *) mmap_resource->mapping.get (),
214 mmap_resource->mapping.size ());
215 }
216 catch (const gdb_exception_error &except)
217 {
218 index_cache_debug ("couldn't read %s: %s",
219 filename.c_str (), except.what ());
220 }
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/* True when we are executing "show index-cache". This is used to improve the
250 printout a little bit. */
251static bool in_show_index_cache_command = false;
252
253/* "show index-cache" handler. */
254
255static void
256show_index_cache_command (const char *arg, int from_tty)
257{
258 /* Note that we are executing "show index-cache". */
259 auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
260
261 /* Call all "show index-cache" subcommands. */
263
264 gdb_printf ("\n");
266 (_("The index cache is currently %s.\n"),
267 global_index_cache.enabled () ? _("enabled") : _("disabled"));
268}
269
270/* "set/show index-cache enabled" set callback. */
271
272static void
274{
275 if (value)
277 else
279}
280
281/* "set/show index-cache enabled" get callback. */
282
283static bool
285{
286 return global_index_cache.enabled ();
287}
288
289/* "set/show index-cache enabled" show callback. */
290
291static void
293 cmd_list_element *cmd, const char *value)
294{
295 gdb_printf (stream, _("The index cache is %s.\n"), value);
296}
297
298/* "set index-cache directory" handler. */
299
300static void
301set_index_cache_directory_command (const char *arg, int from_tty,
302 cmd_list_element *element)
303{
304 /* Make sure the index cache directory is absolute and tilde-expanded. */
305 index_cache_directory = gdb_abspath (index_cache_directory.c_str ());
307}
308
309/* "show index-cache stats" handler. */
310
311static void
312show_index_cache_stats_command (const char *arg, int from_tty)
313{
314 const char *indent = "";
315
316 /* If this command is invoked through "show index-cache", make the display a
317 bit nicer. */
319 {
320 indent = " ";
321 gdb_printf ("\n");
322 }
323
324 gdb_printf (_("%s Cache hits (this session): %u\n"),
325 indent, global_index_cache.n_hits ());
326 gdb_printf (_("%sCache misses (this session): %u\n"),
327 indent, global_index_cache.n_misses ());
328}
329
331void
333{
334 /* Set the default index cache directory. */
335 std::string cache_dir = get_standard_cache_dir ();
336 if (!cache_dir.empty ())
337 {
338 index_cache_directory = cache_dir;
339 global_index_cache.set_directory (std::move (cache_dir));
340 }
341 else
342 warning (_("Couldn't determine a path for the index cache directory."));
343
344 /* set index-cache */
345 add_basic_prefix_cmd ("index-cache", class_files,
346 _("Set index-cache options."),
348 false, &setlist);
349
350 /* show index-cache */
352 _("Show index-cache options."), &show_index_cache_prefix_list,
353 false, &showlist);
354
355 /* set/show index-cache enabled */
356 set_show_commands setshow_index_cache_enabled_cmds
358 _("Enable the index cache."),
359 _("Show whether the index cache is enabled."),
360 _("When on, enable the use of the index cache."),
366
367 /* set index-cache on */
368 cmd_list_element *set_index_cache_on_cmd
369 = add_alias_cmd ("on", setshow_index_cache_enabled_cmds.set, class_files,
371 deprecate_cmd (set_index_cache_on_cmd, "set index-cache enabled on");
372 set_index_cache_on_cmd->default_args = "on";
373
374 /* set index-cache off */
375 cmd_list_element *set_index_cache_off_cmd
376 = add_alias_cmd ("off", setshow_index_cache_enabled_cmds.set, class_files,
378 deprecate_cmd (set_index_cache_off_cmd, "set index-cache enabled off");
379 set_index_cache_off_cmd->default_args = "off";
380
381 /* set index-cache directory */
383 _("Set the directory of the index cache."),
384 _("Show the directory of the index cache."),
385 NULL,
389
390 /* show index-cache stats */
392 _("Show some stats about the index cache."),
394
395 /* set debug index-cache */
398 _("Set display of index-cache debug messages."),
399 _("Show display of index-cache debug messages."),
400 _("\
401When non-zero, debugging output for the index cache is displayed."),
402 NULL, NULL,
404}
const struct bfd_build_id * build_id_bfd_get(bfd *abfd)
Definition: build-id.c:33
static std::string build_id_to_string(const bfd_build_id *build_id)
Definition: build-id.h:60
unsigned int n_hits() const
Definition: index-cache.h:69
unsigned int n_misses() const
Definition: index-cache.h:80
gdb::array_view< const gdb_byte > lookup_gdb_index(const bfd_build_id *build_id, std::unique_ptr< index_cache_resource > *resource)
Definition: index-cache.c:230
void disable()
Definition: index-cache.c:82
bool m_enabled
Definition: index-cache.h:102
std::string make_index_filename(const bfd_build_id *build_id, const char *suffix) const
Definition: index-cache.c:241
std::string m_dir
Definition: index-cache.h:99
bool enabled() const
Definition: index-cache.h:44
void store(dwarf2_per_objfile *per_objfile)
Definition: index-cache.c:92
void enable()
Definition: index-cache.c:72
void set_directory(std::string dir)
Definition: index-cache.c:60
struct cmd_list_element * showlist
Definition: cli-cmds.c:125
struct cmd_list_element * setlist
Definition: cli-cmds.c:117
struct cmd_list_element * showdebuglist
Definition: cli-cmds.c:165
struct cmd_list_element * setdebuglist
Definition: cli-cmds.c:163
set_show_commands add_setshow_filename_cmd(const char *name, enum command_class theclass, std::string *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:785
struct cmd_list_element * add_alias_cmd(const char *name, cmd_list_element *target, enum command_class theclass, int abbrev_flag, struct cmd_list_element **list)
Definition: cli-decode.c:294
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:233
struct cmd_list_element * deprecate_cmd(struct cmd_list_element *cmd, const char *replacement)
Definition: cli-decode.c:280
struct cmd_list_element * add_prefix_cmd(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:357
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
Definition: cli-decode.c:739
struct cmd_list_element * add_basic_prefix_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **subcommands, int allow_unknown, struct cmd_list_element **list)
Definition: cli-decode.c:391
void cmd_show_list(struct cmd_list_element *list, int from_tty)
Definition: cli-setshow.c:700
@ class_maintenance
Definition: command.h:65
@ class_files
Definition: command.h:57
struct dwz_file * dwarf2_get_dwz_file(dwarf2_per_bfd *per_bfd, bool require)
Definition: dwz.c:188
static cmd_list_element * show_index_cache_prefix_list
Definition: index-cache.c:52
static void show_index_cache_command(const char *arg, int from_tty)
Definition: index-cache.c:256
static bool debug_index_cache
Definition: index-cache.c:38
#define index_cache_debug(FMT,...)
Definition: index-cache.c:40
static cmd_list_element * set_index_cache_prefix_list
Definition: index-cache.c:51
static void set_index_cache_directory_command(const char *arg, int from_tty, cmd_list_element *element)
Definition: index-cache.c:301
static void show_index_cache_stats_command(const char *arg, int from_tty)
Definition: index-cache.c:312
static std::string index_cache_directory
Definition: index-cache.c:45
static bool in_show_index_cache_command
Definition: index-cache.c:251
static void set_index_cache_enabled_command(bool value)
Definition: index-cache.c:273
void _initialize_index_cache()
Definition: index-cache.c:332
static void show_index_cache_enabled_command(ui_file *stream, int from_tty, cmd_list_element *cmd, const char *value)
Definition: index-cache.c:292
static bool get_index_cache_enabled_command()
Definition: index-cache.c:284
index_cache global_index_cache
Definition: index-cache.c:48
#define INDEX4_SUFFIX
Definition: index-common.h:24
void write_dwarf_index(dwarf2_per_objfile *per_objfile, const char *dir, const char *basename, const char *dwz_basename, dw_index_kind index_kind)
Definition: index-write.c:1457
@ OBJF_NOT_FILENAME
Definition: objfile-flags.h:66
const char * objfile_name(const struct objfile *objfile)
Definition: objfiles.c:1308
std::string default_args
Definition: cli-decode.h:210
struct objfile * objfile
Definition: read.h:648
struct dwarf2_per_bfd * per_bfd
Definition: read.h:652
Definition: dwz.h:32
const char * filename() const
Definition: dwz.h:38
gdb_bfd_ref_ptr dwz_bfd
Definition: dwz.h:53
virtual ~index_cache_resource()=0
gdb_bfd_ref_ptr obfd
Definition: objfiles.h:653
objfile_flags flags
Definition: objfiles.h:637
cmd_list_element * set
Definition: command.h:406
Definition: value.c:181
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition: utils.c:1865