]>
Commit | Line | Data |
---|---|---|
87d6a7aa SM |
1 | /* Caching of GDB/DWARF index files. |
2 | ||
4a94e368 | 3 | Copyright (C) 1994-2022 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" | |
7bc5c369 | 25 | #include "cli/cli-decode.h" |
87d6a7aa | 26 | #include "command.h" |
268a13a5 TT |
27 | #include "gdbsupport/scoped_mmap.h" |
28 | #include "gdbsupport/pathstuff.h" | |
82ca8957 TT |
29 | #include "dwarf2/index-write.h" |
30 | #include "dwarf2/read.h" | |
9fda78b6 | 31 | #include "dwarf2/dwz.h" |
87d6a7aa | 32 | #include "objfiles.h" |
268a13a5 | 33 | #include "gdbsupport/selftest.h" |
4de283e4 TT |
34 | #include <string> |
35 | #include <stdlib.h> | |
87d6a7aa | 36 | |
491144b5 CB |
37 | /* When set to true, show debug messages about the index cache. */ |
38 | static bool debug_index_cache = false; | |
87d6a7aa | 39 | |
ca78fadb TT |
40 | #define index_cache_debug(FMT, ...) \ |
41 | debug_prefixed_printf_cond_nofunc (debug_index_cache, "index-cache", \ | |
42 | FMT, ## __VA_ARGS__) | |
43 | ||
87d6a7aa | 44 | /* The index cache directory, used for "set/show index-cache directory". */ |
e0700ba4 | 45 | static std::string index_cache_directory; |
87d6a7aa SM |
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 | ||
87d6a7aa SM |
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 | ||
bafae552 | 66 | index_cache_debug ("now using directory %s", m_dir.c_str ()); |
87d6a7aa SM |
67 | } |
68 | ||
69 | /* See dwarf-index-cache.h. */ | |
70 | ||
71 | void | |
72 | index_cache::enable () | |
73 | { | |
bafae552 | 74 | index_cache_debug ("enabling (%s)", m_dir.c_str ()); |
87d6a7aa SM |
75 | |
76 | m_enabled = true; | |
77 | } | |
78 | ||
79 | /* See dwarf-index-cache.h. */ | |
80 | ||
81 | void | |
82 | index_cache::disable () | |
83 | { | |
bafae552 | 84 | index_cache_debug ("disabling"); |
87d6a7aa SM |
85 | |
86 | m_enabled = false; | |
87 | } | |
88 | ||
89 | /* See dwarf-index-cache.h. */ | |
90 | ||
91 | void | |
976ca316 | 92 | index_cache::store (dwarf2_per_objfile *per_objfile) |
87d6a7aa | 93 | { |
976ca316 | 94 | objfile *obj = per_objfile->objfile; |
87d6a7aa SM |
95 | |
96 | if (!enabled ()) | |
97 | return; | |
98 | ||
c4973306 | 99 | /* Get build id of objfile. */ |
87d6a7aa SM |
100 | const bfd_build_id *build_id = build_id_bfd_get (obj->obfd); |
101 | if (build_id == nullptr) | |
102 | { | |
bafae552 | 103 | index_cache_debug ("objfile %s has no build id", |
ca78fadb | 104 | objfile_name (obj)); |
87d6a7aa SM |
105 | return; |
106 | } | |
107 | ||
c4973306 SM |
108 | std::string build_id_str = build_id_to_string (build_id); |
109 | ||
110 | /* Get build id of dwz file, if present. */ | |
111 | gdb::optional<std::string> dwz_build_id_str; | |
976ca316 | 112 | const dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd); |
c4973306 SM |
113 | const char *dwz_build_id_ptr = NULL; |
114 | ||
115 | if (dwz != nullptr) | |
116 | { | |
117 | const bfd_build_id *dwz_build_id = build_id_bfd_get (dwz->dwz_bfd.get ()); | |
118 | ||
119 | if (dwz_build_id == nullptr) | |
120 | { | |
bafae552 | 121 | index_cache_debug ("dwz objfile %s has no build id", |
ca78fadb | 122 | dwz->filename ()); |
c4973306 SM |
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 | 145 | |
bafae552 | 146 | index_cache_debug ("writing index cache for objfile %s", |
ca78fadb | 147 | objfile_name (obj)); |
87d6a7aa SM |
148 | |
149 | /* Write the index itself to the directory, using the build id as the | |
dda83cd7 | 150 | filename. */ |
976ca316 | 151 | write_psymtabs_to_index (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 | 156 | { |
bafae552 AB |
157 | index_cache_debug ("couldn't store index cache for objfile %s: %s", |
158 | objfile_name (obj), except.what ()); | |
87d6a7aa | 159 | } |
87d6a7aa SM |
160 | } |
161 | ||
162 | #if HAVE_SYS_MMAN_H | |
163 | ||
164 | /* Hold the resources for an mmapped index file. */ | |
165 | ||
166 | struct index_cache_resource_mmap final : public index_cache_resource | |
167 | { | |
168 | /* Try to mmap FILENAME. Throw an exception on failure, including if the | |
169 | file doesn't exist. */ | |
170 | index_cache_resource_mmap (const char *filename) | |
171 | : mapping (mmap_file (filename)) | |
172 | {} | |
173 | ||
174 | scoped_mmap mapping; | |
175 | }; | |
176 | ||
177 | /* See dwarf-index-cache.h. */ | |
178 | ||
179 | gdb::array_view<const gdb_byte> | |
180 | index_cache::lookup_gdb_index (const bfd_build_id *build_id, | |
181 | std::unique_ptr<index_cache_resource> *resource) | |
182 | { | |
183 | if (!enabled ()) | |
184 | return {}; | |
185 | ||
186 | if (m_dir.empty ()) | |
187 | { | |
188 | warning (_("The index cache directory name is empty, skipping cache " | |
189 | "lookup.")); | |
190 | return {}; | |
191 | } | |
192 | ||
193 | /* Compute where we would expect a gdb index file for this build id to be. */ | |
194 | std::string filename = make_index_filename (build_id, INDEX4_SUFFIX); | |
195 | ||
a70b8144 | 196 | try |
87d6a7aa | 197 | { |
bafae552 | 198 | index_cache_debug ("trying to read %s", |
ca78fadb | 199 | filename.c_str ()); |
87d6a7aa SM |
200 | |
201 | /* Try to map that file. */ | |
202 | index_cache_resource_mmap *mmap_resource | |
203 | = new index_cache_resource_mmap (filename.c_str ()); | |
204 | ||
205 | /* Yay, it worked! Hand the resource to the caller. */ | |
206 | resource->reset (mmap_resource); | |
207 | ||
208 | return gdb::array_view<const gdb_byte> | |
209 | ((const gdb_byte *) mmap_resource->mapping.get (), | |
210 | mmap_resource->mapping.size ()); | |
211 | } | |
230d2906 | 212 | catch (const gdb_exception_error &except) |
87d6a7aa | 213 | { |
bafae552 | 214 | index_cache_debug ("couldn't read %s: %s", |
ca78fadb | 215 | filename.c_str (), except.what ()); |
87d6a7aa | 216 | } |
87d6a7aa SM |
217 | |
218 | return {}; | |
219 | } | |
220 | ||
221 | #else /* !HAVE_SYS_MMAN_H */ | |
222 | ||
223 | /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */ | |
224 | ||
225 | gdb::array_view<const gdb_byte> | |
226 | index_cache::lookup_gdb_index (const bfd_build_id *build_id, | |
227 | std::unique_ptr<index_cache_resource> *resource) | |
228 | { | |
229 | return {}; | |
230 | } | |
231 | ||
232 | #endif | |
233 | ||
234 | /* See dwarf-index-cache.h. */ | |
235 | ||
236 | std::string | |
237 | index_cache::make_index_filename (const bfd_build_id *build_id, | |
238 | const char *suffix) const | |
239 | { | |
240 | std::string build_id_str = build_id_to_string (build_id); | |
241 | ||
242 | return m_dir + SLASH_STRING + build_id_str + suffix; | |
243 | } | |
244 | ||
87d6a7aa SM |
245 | /* True when we are executing "show index-cache". This is used to improve the |
246 | printout a little bit. */ | |
247 | static bool in_show_index_cache_command = false; | |
248 | ||
249 | /* "show index-cache" handler. */ | |
250 | ||
251 | static void | |
252 | show_index_cache_command (const char *arg, int from_tty) | |
253 | { | |
254 | /* Note that we are executing "show index-cache". */ | |
255 | auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true); | |
256 | ||
257 | /* Call all "show index-cache" subcommands. */ | |
7aa1b46f | 258 | cmd_show_list (show_index_cache_prefix_list, from_tty); |
87d6a7aa | 259 | |
6cb06a8c TT |
260 | gdb_printf ("\n"); |
261 | gdb_printf | |
87d6a7aa SM |
262 | (_("The index cache is currently %s.\n"), |
263 | global_index_cache.enabled () ? _("enabled") : _("disabled")); | |
264 | } | |
265 | ||
7bc5c369 | 266 | /* "set/show index-cache enabled" set callback. */ |
87d6a7aa SM |
267 | |
268 | static void | |
7bc5c369 | 269 | set_index_cache_enabled_command (bool value) |
87d6a7aa | 270 | { |
7bc5c369 SM |
271 | if (value) |
272 | global_index_cache.enable (); | |
273 | else | |
274 | global_index_cache.disable (); | |
275 | } | |
276 | ||
277 | /* "set/show index-cache enabled" get callback. */ | |
278 | ||
279 | static bool | |
280 | get_index_cache_enabled_command () | |
281 | { | |
282 | return global_index_cache.enabled (); | |
87d6a7aa SM |
283 | } |
284 | ||
7bc5c369 | 285 | /* "set/show index-cache enabled" show callback. */ |
87d6a7aa SM |
286 | |
287 | static void | |
7bc5c369 SM |
288 | show_index_cache_enabled_command (ui_file *stream, int from_tty, |
289 | cmd_list_element *cmd, const char *value) | |
87d6a7aa | 290 | { |
6cb06a8c | 291 | gdb_printf (stream, _("The index cache is %s.\n"), value); |
87d6a7aa SM |
292 | } |
293 | ||
294 | /* "set index-cache directory" handler. */ | |
295 | ||
296 | static void | |
297 | set_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. */ | |
e0700ba4 SM |
301 | gdb::unique_xmalloc_ptr<char> abs |
302 | = gdb_abspath (index_cache_directory.c_str ()); | |
303 | index_cache_directory = abs.get (); | |
87d6a7aa SM |
304 | global_index_cache.set_directory (index_cache_directory); |
305 | } | |
306 | ||
307 | /* "show index-cache stats" handler. */ | |
308 | ||
309 | static void | |
310 | show_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 = " "; | |
6cb06a8c | 319 | gdb_printf ("\n"); |
87d6a7aa SM |
320 | } |
321 | ||
6cb06a8c TT |
322 | gdb_printf (_("%s Cache hits (this session): %u\n"), |
323 | indent, global_index_cache.n_hits ()); | |
324 | gdb_printf (_("%sCache misses (this session): %u\n"), | |
325 | indent, global_index_cache.n_misses ()); | |
87d6a7aa SM |
326 | } |
327 | ||
6c265988 | 328 | void _initialize_index_cache (); |
87d6a7aa SM |
329 | void |
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 | { | |
e0700ba4 | 336 | index_cache_directory = cache_dir; |
87d6a7aa SM |
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 */ | |
3b6acaee TT |
343 | add_basic_prefix_cmd ("index-cache", class_files, |
344 | _("Set index-cache options."), | |
345 | &set_index_cache_prefix_list, | |
2f822da5 | 346 | false, &setlist); |
87d6a7aa SM |
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, |
2f822da5 | 351 | false, &showlist); |
87d6a7aa | 352 | |
7bc5c369 SM |
353 | /* set/show index-cache enabled */ |
354 | set_show_commands setshow_index_cache_enabled_cmds | |
355 | = add_setshow_boolean_cmd ("enabled", class_files, | |
356 | _("Enable the index cache."), | |
357 | _("Show whether the index cache is enabled."), | |
bd51ddc8 | 358 | _("When on, enable the use of the index cache."), |
7bc5c369 SM |
359 | set_index_cache_enabled_command, |
360 | get_index_cache_enabled_command, | |
361 | show_index_cache_enabled_command, | |
362 | &set_index_cache_prefix_list, | |
363 | &show_index_cache_prefix_list); | |
364 | ||
87d6a7aa | 365 | /* set index-cache on */ |
7bc5c369 SM |
366 | cmd_list_element *set_index_cache_on_cmd |
367 | = add_alias_cmd ("on", setshow_index_cache_enabled_cmds.set, class_files, | |
368 | false, &set_index_cache_prefix_list); | |
369 | deprecate_cmd (set_index_cache_on_cmd, "set index-cache enabled on"); | |
370 | set_index_cache_on_cmd->default_args = "on"; | |
87d6a7aa SM |
371 | |
372 | /* set index-cache off */ | |
7bc5c369 SM |
373 | cmd_list_element *set_index_cache_off_cmd |
374 | = add_alias_cmd ("off", setshow_index_cache_enabled_cmds.set, class_files, | |
375 | false, &set_index_cache_prefix_list); | |
376 | deprecate_cmd (set_index_cache_off_cmd, "set index-cache enabled off"); | |
377 | set_index_cache_off_cmd->default_args = "off"; | |
87d6a7aa SM |
378 | |
379 | /* set index-cache directory */ | |
380 | add_setshow_filename_cmd ("directory", class_files, &index_cache_directory, | |
381 | _("Set the directory of the index cache."), | |
382 | _("Show the directory of the index cache."), | |
383 | NULL, | |
384 | set_index_cache_directory_command, NULL, | |
385 | &set_index_cache_prefix_list, | |
386 | &show_index_cache_prefix_list); | |
387 | ||
388 | /* show index-cache stats */ | |
389 | add_cmd ("stats", class_files, show_index_cache_stats_command, | |
390 | _("Show some stats about the index cache."), | |
391 | &show_index_cache_prefix_list); | |
392 | ||
393 | /* set debug index-cache */ | |
394 | add_setshow_boolean_cmd ("index-cache", class_maintenance, | |
395 | &debug_index_cache, | |
396 | _("Set display of index-cache debug messages."), | |
397 | _("Show display of index-cache debug messages."), | |
398 | _("\ | |
399 | When non-zero, debugging output for the index cache is displayed."), | |
400 | NULL, NULL, | |
401 | &setdebuglist, &showdebuglist); | |
87d6a7aa | 402 | } |