Document array indexing for Python gdb.Value
[binutils-gdb.git] / gdb / dwarf2 / index-cache.c
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. */
38 static 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". */
45 static std::string index_cache_directory;
46
47 /* See dwarf-index.cache.h. */
48 index_cache global_index_cache;
49
50 /* set/show index-cache commands. */
51 static cmd_list_element *set_index_cache_prefix_list;
52 static cmd_list_element *show_index_cache_prefix_list;
53
54 /* Default destructor of index_cache_resource. */
55 index_cache_resource::~index_cache_resource () = default;
56
57 /* See dwarf-index-cache.h. */
58
59 void
60 index_cache::set_directory (std::string dir)
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
71 void
72 index_cache::enable ()
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
81 void
82 index_cache::disable ()
83 {
84 index_cache_debug ("disabling");
85
86 m_enabled = false;
87 }
88
89 /* See dwarf-index-cache.h. */
90
91 void
92 index_cache::store (dwarf2_per_bfd *per_bfd)
93 {
94 if (!enabled ())
95 return;
96
97 /* Get build id of objfile. */
98 const bfd_build_id *build_id = build_id_bfd_get (per_bfd->obfd);
99 if (build_id == nullptr)
100 {
101 index_cache_debug ("objfile %s has no build id",
102 bfd_get_filename (per_bfd->obfd));
103 return;
104 }
105
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 (per_bfd);
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 index_cache_debug ("dwz objfile %s has no build id",
120 dwz->filename ());
121 return;
122 }
123
124 dwz_build_id_str = build_id_to_string (dwz_build_id);
125 dwz_build_id_ptr = dwz_build_id_str->c_str ();
126 }
127
128 if (m_dir.empty ())
129 {
130 warning (_("The index cache directory name is empty, skipping store."));
131 return;
132 }
133
134 try
135 {
136 /* Try to create the containing directory. */
137 if (!mkdir_recursive (m_dir.c_str ()))
138 {
139 warning (_("index cache: could not make cache directory: %s"),
140 safe_strerror (errno));
141 return;
142 }
143
144 index_cache_debug ("writing index cache for objfile %s",
145 bfd_get_filename (per_bfd->obfd));
146
147 /* Write the index itself to the directory, using the build id as the
148 filename. */
149 write_dwarf_index (per_bfd, m_dir.c_str (),
150 build_id_str.c_str (), dwz_build_id_ptr,
151 dw_index_kind::GDB_INDEX);
152 }
153 catch (const gdb_exception_error &except)
154 {
155 index_cache_debug ("couldn't store index cache for objfile %s: %s",
156 bfd_get_filename (per_bfd->obfd), except.what ());
157 }
158 }
159
160 #if HAVE_SYS_MMAN_H
161
162 /* Hold the resources for an mmapped index file. */
163
164 struct index_cache_resource_mmap final : public index_cache_resource
165 {
166 /* Try to mmap FILENAME. Throw an exception on failure, including if the
167 file doesn't exist. */
168 index_cache_resource_mmap (const char *filename)
169 : mapping (mmap_file (filename))
170 {}
171
172 scoped_mmap mapping;
173 };
174
175 /* See dwarf-index-cache.h. */
176
177 gdb::array_view<const gdb_byte>
178 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
179 std::unique_ptr<index_cache_resource> *resource)
180 {
181 if (!enabled ())
182 return {};
183
184 if (m_dir.empty ())
185 {
186 warning (_("The index cache directory name is empty, skipping cache "
187 "lookup."));
188 return {};
189 }
190
191 /* Compute where we would expect a gdb index file for this build id to be. */
192 std::string filename = make_index_filename (build_id, INDEX4_SUFFIX);
193
194 try
195 {
196 index_cache_debug ("trying to read %s",
197 filename.c_str ());
198
199 /* Try to map that file. */
200 index_cache_resource_mmap *mmap_resource
201 = new index_cache_resource_mmap (filename.c_str ());
202
203 /* Yay, it worked! Hand the resource to the caller. */
204 resource->reset (mmap_resource);
205
206 return gdb::array_view<const gdb_byte>
207 ((const gdb_byte *) mmap_resource->mapping.get (),
208 mmap_resource->mapping.size ());
209 }
210 catch (const gdb_exception_error &except)
211 {
212 index_cache_debug ("couldn't read %s: %s",
213 filename.c_str (), except.what ());
214 }
215
216 return {};
217 }
218
219 #else /* !HAVE_SYS_MMAN_H */
220
221 /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */
222
223 gdb::array_view<const gdb_byte>
224 index_cache::lookup_gdb_index (const bfd_build_id *build_id,
225 std::unique_ptr<index_cache_resource> *resource)
226 {
227 return {};
228 }
229
230 #endif
231
232 /* See dwarf-index-cache.h. */
233
234 std::string
235 index_cache::make_index_filename (const bfd_build_id *build_id,
236 const char *suffix) const
237 {
238 std::string build_id_str = build_id_to_string (build_id);
239
240 return m_dir + SLASH_STRING + build_id_str + suffix;
241 }
242
243 /* True when we are executing "show index-cache". This is used to improve the
244 printout a little bit. */
245 static bool in_show_index_cache_command = false;
246
247 /* "show index-cache" handler. */
248
249 static void
250 show_index_cache_command (const char *arg, int from_tty)
251 {
252 /* Note that we are executing "show index-cache". */
253 auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true);
254
255 /* Call all "show index-cache" subcommands. */
256 cmd_show_list (show_index_cache_prefix_list, from_tty);
257
258 gdb_printf ("\n");
259 gdb_printf
260 (_("The index cache is currently %s.\n"),
261 global_index_cache.enabled () ? _("enabled") : _("disabled"));
262 }
263
264 /* "set/show index-cache enabled" set callback. */
265
266 static void
267 set_index_cache_enabled_command (bool value)
268 {
269 if (value)
270 global_index_cache.enable ();
271 else
272 global_index_cache.disable ();
273 }
274
275 /* "set/show index-cache enabled" get callback. */
276
277 static bool
278 get_index_cache_enabled_command ()
279 {
280 return global_index_cache.enabled ();
281 }
282
283 /* "set/show index-cache enabled" show callback. */
284
285 static void
286 show_index_cache_enabled_command (ui_file *stream, int from_tty,
287 cmd_list_element *cmd, const char *value)
288 {
289 gdb_printf (stream, _("The index cache is %s.\n"), value);
290 }
291
292 /* "set index-cache directory" handler. */
293
294 static void
295 set_index_cache_directory_command (const char *arg, int from_tty,
296 cmd_list_element *element)
297 {
298 /* Make sure the index cache directory is absolute and tilde-expanded. */
299 index_cache_directory = gdb_abspath (index_cache_directory.c_str ());
300 global_index_cache.set_directory (index_cache_directory);
301 }
302
303 /* "show index-cache stats" handler. */
304
305 static void
306 show_index_cache_stats_command (const char *arg, int from_tty)
307 {
308 const char *indent = "";
309
310 /* If this command is invoked through "show index-cache", make the display a
311 bit nicer. */
312 if (in_show_index_cache_command)
313 {
314 indent = " ";
315 gdb_printf ("\n");
316 }
317
318 gdb_printf (_("%s Cache hits (this session): %u\n"),
319 indent, global_index_cache.n_hits ());
320 gdb_printf (_("%sCache misses (this session): %u\n"),
321 indent, global_index_cache.n_misses ());
322 }
323
324 void _initialize_index_cache ();
325 void
326 _initialize_index_cache ()
327 {
328 /* Set the default index cache directory. */
329 std::string cache_dir = get_standard_cache_dir ();
330 if (!cache_dir.empty ())
331 {
332 index_cache_directory = cache_dir;
333 global_index_cache.set_directory (std::move (cache_dir));
334 }
335 else
336 warning (_("Couldn't determine a path for the index cache directory."));
337
338 /* set index-cache */
339 add_basic_prefix_cmd ("index-cache", class_files,
340 _("Set index-cache options."),
341 &set_index_cache_prefix_list,
342 false, &setlist);
343
344 /* show index-cache */
345 add_prefix_cmd ("index-cache", class_files, show_index_cache_command,
346 _("Show index-cache options."), &show_index_cache_prefix_list,
347 false, &showlist);
348
349 /* set/show index-cache enabled */
350 set_show_commands setshow_index_cache_enabled_cmds
351 = add_setshow_boolean_cmd ("enabled", class_files,
352 _("Enable the index cache."),
353 _("Show whether the index cache is enabled."),
354 _("When on, enable the use of the index cache."),
355 set_index_cache_enabled_command,
356 get_index_cache_enabled_command,
357 show_index_cache_enabled_command,
358 &set_index_cache_prefix_list,
359 &show_index_cache_prefix_list);
360
361 /* set index-cache on */
362 cmd_list_element *set_index_cache_on_cmd
363 = add_alias_cmd ("on", setshow_index_cache_enabled_cmds.set, class_files,
364 false, &set_index_cache_prefix_list);
365 deprecate_cmd (set_index_cache_on_cmd, "set index-cache enabled on");
366 set_index_cache_on_cmd->default_args = "on";
367
368 /* set index-cache off */
369 cmd_list_element *set_index_cache_off_cmd
370 = add_alias_cmd ("off", setshow_index_cache_enabled_cmds.set, class_files,
371 false, &set_index_cache_prefix_list);
372 deprecate_cmd (set_index_cache_off_cmd, "set index-cache enabled off");
373 set_index_cache_off_cmd->default_args = "off";
374
375 /* set index-cache directory */
376 add_setshow_filename_cmd ("directory", class_files, &index_cache_directory,
377 _("Set the directory of the index cache."),
378 _("Show the directory of the index cache."),
379 NULL,
380 set_index_cache_directory_command, NULL,
381 &set_index_cache_prefix_list,
382 &show_index_cache_prefix_list);
383
384 /* show index-cache stats */
385 add_cmd ("stats", class_files, show_index_cache_stats_command,
386 _("Show some stats about the index cache."),
387 &show_index_cache_prefix_list);
388
389 /* set debug index-cache */
390 add_setshow_boolean_cmd ("index-cache", class_maintenance,
391 &debug_index_cache,
392 _("Set display of index-cache debug messages."),
393 _("Show display of index-cache debug messages."),
394 _("\
395 When non-zero, debugging output for the index cache is displayed."),
396 NULL, NULL,
397 &setdebuglist, &showdebuglist);
398 }