| 1 | /* Caching of GDB/DWARF index files. |
| 2 | |
| 3 | Copyright (C) 1994-2020 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 "dwarf-index-cache.h" |
| 22 | |
| 23 | #include "build-id.h" |
| 24 | #include "cli/cli-cmds.h" |
| 25 | #include "command.h" |
| 26 | #include "gdbsupport/scoped_mmap.h" |
| 27 | #include "gdbsupport/pathstuff.h" |
| 28 | #include "dwarf-index-write.h" |
| 29 | #include "dwarf2read.h" |
| 30 | #include "objfiles.h" |
| 31 | #include "gdbsupport/selftest.h" |
| 32 | #include <string> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | /* When set to true, show debug messages about the index cache. */ |
| 36 | static bool debug_index_cache = false; |
| 37 | |
| 38 | /* The index cache directory, used for "set/show index-cache directory". */ |
| 39 | static char *index_cache_directory = NULL; |
| 40 | |
| 41 | /* See dwarf-index.cache.h. */ |
| 42 | index_cache global_index_cache; |
| 43 | |
| 44 | /* set/show index-cache commands. */ |
| 45 | static cmd_list_element *set_index_cache_prefix_list; |
| 46 | static cmd_list_element *show_index_cache_prefix_list; |
| 47 | |
| 48 | /* Default destructor of index_cache_resource. */ |
| 49 | index_cache_resource::~index_cache_resource () = default; |
| 50 | |
| 51 | /* See dwarf-index-cache.h. */ |
| 52 | |
| 53 | void |
| 54 | index_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 | |
| 66 | void |
| 67 | index_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 | |
| 77 | void |
| 78 | index_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 | |
| 88 | void |
| 89 | index_cache::store (struct dwarf2_per_objfile *dwarf2_per_objfile) |
| 90 | { |
| 91 | objfile *obj = dwarf2_per_objfile->objfile; |
| 92 | |
| 93 | if (!enabled ()) |
| 94 | return; |
| 95 | |
| 96 | /* Get build id of objfile. */ |
| 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 | |
| 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 | |
| 129 | if (m_dir.empty ()) |
| 130 | { |
| 131 | warning (_("The index cache directory name is empty, skipping store.")); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | try |
| 136 | { |
| 137 | /* Try to create the containing directory. */ |
| 138 | if (!mkdir_recursive (m_dir.c_str ())) |
| 139 | { |
| 140 | warning (_("index cache: could not make cache directory: %s"), |
| 141 | safe_strerror (errno)); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | if (debug_index_cache) |
| 146 | printf_unfiltered ("index cache: writing index cache for objfile %s\n", |
| 147 | objfile_name (obj)); |
| 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 (), |
| 152 | build_id_str.c_str (), dwz_build_id_ptr, |
| 153 | dw_index_kind::GDB_INDEX); |
| 154 | } |
| 155 | catch (const gdb_exception_error &except) |
| 156 | { |
| 157 | if (debug_index_cache) |
| 158 | printf_unfiltered ("index cache: couldn't store index cache for objfile " |
| 159 | "%s: %s", objfile_name (obj), except.what ()); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #if HAVE_SYS_MMAN_H |
| 164 | |
| 165 | /* Hold the resources for an mmapped index file. */ |
| 166 | |
| 167 | struct 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 | |
| 180 | gdb::array_view<const gdb_byte> |
| 181 | index_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 | |
| 197 | try |
| 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 | } |
| 214 | catch (const gdb_exception_error &except) |
| 215 | { |
| 216 | if (debug_index_cache) |
| 217 | printf_unfiltered ("index cache: couldn't read %s: %s\n", |
| 218 | filename.c_str (), except.what ()); |
| 219 | } |
| 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 | |
| 228 | gdb::array_view<const gdb_byte> |
| 229 | index_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 | |
| 239 | std::string |
| 240 | index_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 | |
| 250 | static void |
| 251 | set_index_cache_command (const char *arg, int from_tty) |
| 252 | { |
| 253 | printf_unfiltered (_("\ |
| 254 | Missing 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. */ |
| 259 | static bool in_show_index_cache_command = false; |
| 260 | |
| 261 | /* "show index-cache" handler. */ |
| 262 | |
| 263 | static void |
| 264 | show_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 | |
| 280 | static void |
| 281 | set_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 | |
| 288 | static void |
| 289 | set_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 | |
| 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. */ |
| 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 | |
| 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 = " "; |
| 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 | |
| 328 | void |
| 329 | _initialize_index_cache () |
| 330 | { |
| 331 | /* Set the default index cache directory. */ |
| 332 | std::string cache_dir = get_standard_cache_dir (); |
| 333 | if (!cache_dir.empty ()) |
| 334 | { |
| 335 | index_cache_directory = xstrdup (cache_dir.c_str ()); |
| 336 | global_index_cache.set_directory (std::move (cache_dir)); |
| 337 | } |
| 338 | else |
| 339 | warning (_("Couldn't determine a path for the index cache directory.")); |
| 340 | |
| 341 | /* set index-cache */ |
| 342 | add_prefix_cmd ("index-cache", class_files, set_index_cache_command, |
| 343 | _("Set index-cache options."), &set_index_cache_prefix_list, |
| 344 | "set index-cache ", false, &setlist); |
| 345 | |
| 346 | /* show index-cache */ |
| 347 | add_prefix_cmd ("index-cache", class_files, show_index_cache_command, |
| 348 | _("Show index-cache options."), &show_index_cache_prefix_list, |
| 349 | "show index-cache ", false, &showlist); |
| 350 | |
| 351 | /* set index-cache on */ |
| 352 | add_cmd ("on", class_files, set_index_cache_on_command, |
| 353 | _("Enable the index cache."), &set_index_cache_prefix_list); |
| 354 | |
| 355 | /* set index-cache off */ |
| 356 | add_cmd ("off", class_files, set_index_cache_off_command, |
| 357 | _("Disable the index cache."), &set_index_cache_prefix_list); |
| 358 | |
| 359 | /* set index-cache directory */ |
| 360 | add_setshow_filename_cmd ("directory", class_files, &index_cache_directory, |
| 361 | _("Set the directory of the index cache."), |
| 362 | _("Show the directory of the index cache."), |
| 363 | NULL, |
| 364 | set_index_cache_directory_command, NULL, |
| 365 | &set_index_cache_prefix_list, |
| 366 | &show_index_cache_prefix_list); |
| 367 | |
| 368 | /* show index-cache stats */ |
| 369 | add_cmd ("stats", class_files, show_index_cache_stats_command, |
| 370 | _("Show some stats about the index cache."), |
| 371 | &show_index_cache_prefix_list); |
| 372 | |
| 373 | /* set debug index-cache */ |
| 374 | add_setshow_boolean_cmd ("index-cache", class_maintenance, |
| 375 | &debug_index_cache, |
| 376 | _("Set display of index-cache debug messages."), |
| 377 | _("Show display of index-cache debug messages."), |
| 378 | _("\ |
| 379 | When non-zero, debugging output for the index cache is displayed."), |
| 380 | NULL, NULL, |
| 381 | &setdebuglist, &showdebuglist); |
| 382 | } |