| 1 | /* Caching of GDB/DWARF index files. |
| 2 | |
| 3 | Copyright (C) 1994-2019 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 "common/scoped_mmap.h" |
| 27 | #include "common/pathstuff.h" |
| 28 | #include "dwarf-index-write.h" |
| 29 | #include "dwarf2read.h" |
| 30 | #include "objfiles.h" |
| 31 | #include "common/selftest.h" |
| 32 | #include <string> |
| 33 | #include <stdlib.h> |
| 34 | |
| 35 | /* When set to 1, show debug messages about the index cache. */ |
| 36 | static int debug_index_cache = 0; |
| 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 | const bfd_build_id *build_id = build_id_bfd_get (obj->obfd); |
| 97 | if (build_id == nullptr) |
| 98 | { |
| 99 | if (debug_index_cache) |
| 100 | printf_unfiltered ("index cache: objfile %s has no build id\n", |
| 101 | objfile_name (obj)); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | if (m_dir.empty ()) |
| 106 | { |
| 107 | warning (_("The index cache directory name is empty, skipping store.")); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | std::string build_id_str = build_id_to_string (build_id); |
| 112 | |
| 113 | try |
| 114 | { |
| 115 | /* Try to create the containing directory. */ |
| 116 | if (!mkdir_recursive (m_dir.c_str ())) |
| 117 | { |
| 118 | warning (_("index cache: could not make cache directory: %s\n"), |
| 119 | safe_strerror (errno)); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if (debug_index_cache) |
| 124 | printf_unfiltered ("index cache: writing index cache for objfile %s\n", |
| 125 | objfile_name (obj)); |
| 126 | |
| 127 | /* Write the index itself to the directory, using the build id as the |
| 128 | filename. */ |
| 129 | write_psymtabs_to_index (dwarf2_per_objfile, m_dir.c_str (), |
| 130 | build_id_str.c_str (), dw_index_kind::GDB_INDEX); |
| 131 | } |
| 132 | catch (const gdb_exception_error &except) |
| 133 | { |
| 134 | if (debug_index_cache) |
| 135 | printf_unfiltered ("index cache: couldn't store index cache for objfile " |
| 136 | "%s: %s", objfile_name (obj), except.what ()); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #if HAVE_SYS_MMAN_H |
| 141 | |
| 142 | /* Hold the resources for an mmapped index file. */ |
| 143 | |
| 144 | struct index_cache_resource_mmap final : public index_cache_resource |
| 145 | { |
| 146 | /* Try to mmap FILENAME. Throw an exception on failure, including if the |
| 147 | file doesn't exist. */ |
| 148 | index_cache_resource_mmap (const char *filename) |
| 149 | : mapping (mmap_file (filename)) |
| 150 | {} |
| 151 | |
| 152 | scoped_mmap mapping; |
| 153 | }; |
| 154 | |
| 155 | /* See dwarf-index-cache.h. */ |
| 156 | |
| 157 | gdb::array_view<const gdb_byte> |
| 158 | index_cache::lookup_gdb_index (const bfd_build_id *build_id, |
| 159 | std::unique_ptr<index_cache_resource> *resource) |
| 160 | { |
| 161 | if (!enabled ()) |
| 162 | return {}; |
| 163 | |
| 164 | if (m_dir.empty ()) |
| 165 | { |
| 166 | warning (_("The index cache directory name is empty, skipping cache " |
| 167 | "lookup.")); |
| 168 | return {}; |
| 169 | } |
| 170 | |
| 171 | /* Compute where we would expect a gdb index file for this build id to be. */ |
| 172 | std::string filename = make_index_filename (build_id, INDEX4_SUFFIX); |
| 173 | |
| 174 | try |
| 175 | { |
| 176 | if (debug_index_cache) |
| 177 | printf_unfiltered ("index cache: trying to read %s\n", |
| 178 | filename.c_str ()); |
| 179 | |
| 180 | /* Try to map that file. */ |
| 181 | index_cache_resource_mmap *mmap_resource |
| 182 | = new index_cache_resource_mmap (filename.c_str ()); |
| 183 | |
| 184 | /* Yay, it worked! Hand the resource to the caller. */ |
| 185 | resource->reset (mmap_resource); |
| 186 | |
| 187 | return gdb::array_view<const gdb_byte> |
| 188 | ((const gdb_byte *) mmap_resource->mapping.get (), |
| 189 | mmap_resource->mapping.size ()); |
| 190 | } |
| 191 | catch (const gdb_exception_error &except) |
| 192 | { |
| 193 | if (debug_index_cache) |
| 194 | printf_unfiltered ("index cache: couldn't read %s: %s\n", |
| 195 | filename.c_str (), except.what ()); |
| 196 | } |
| 197 | |
| 198 | return {}; |
| 199 | } |
| 200 | |
| 201 | #else /* !HAVE_SYS_MMAN_H */ |
| 202 | |
| 203 | /* See dwarf-index-cache.h. This is a no-op on unsupported systems. */ |
| 204 | |
| 205 | gdb::array_view<const gdb_byte> |
| 206 | index_cache::lookup_gdb_index (const bfd_build_id *build_id, |
| 207 | std::unique_ptr<index_cache_resource> *resource) |
| 208 | { |
| 209 | return {}; |
| 210 | } |
| 211 | |
| 212 | #endif |
| 213 | |
| 214 | /* See dwarf-index-cache.h. */ |
| 215 | |
| 216 | std::string |
| 217 | index_cache::make_index_filename (const bfd_build_id *build_id, |
| 218 | const char *suffix) const |
| 219 | { |
| 220 | std::string build_id_str = build_id_to_string (build_id); |
| 221 | |
| 222 | return m_dir + SLASH_STRING + build_id_str + suffix; |
| 223 | } |
| 224 | |
| 225 | /* "set index-cache" handler. */ |
| 226 | |
| 227 | static void |
| 228 | set_index_cache_command (const char *arg, int from_tty) |
| 229 | { |
| 230 | printf_unfiltered (_("\ |
| 231 | Missing arguments. See \"help set index-cache\" for help.\n")); |
| 232 | } |
| 233 | |
| 234 | /* True when we are executing "show index-cache". This is used to improve the |
| 235 | printout a little bit. */ |
| 236 | static bool in_show_index_cache_command = false; |
| 237 | |
| 238 | /* "show index-cache" handler. */ |
| 239 | |
| 240 | static void |
| 241 | show_index_cache_command (const char *arg, int from_tty) |
| 242 | { |
| 243 | /* Note that we are executing "show index-cache". */ |
| 244 | auto restore_flag = make_scoped_restore (&in_show_index_cache_command, true); |
| 245 | |
| 246 | /* Call all "show index-cache" subcommands. */ |
| 247 | cmd_show_list (show_index_cache_prefix_list, from_tty, ""); |
| 248 | |
| 249 | printf_unfiltered ("\n"); |
| 250 | printf_unfiltered |
| 251 | (_("The index cache is currently %s.\n"), |
| 252 | global_index_cache.enabled () ? _("enabled") : _("disabled")); |
| 253 | } |
| 254 | |
| 255 | /* "set index-cache on" handler. */ |
| 256 | |
| 257 | static void |
| 258 | set_index_cache_on_command (const char *arg, int from_tty) |
| 259 | { |
| 260 | global_index_cache.enable (); |
| 261 | } |
| 262 | |
| 263 | /* "set index-cache off" handler. */ |
| 264 | |
| 265 | static void |
| 266 | set_index_cache_off_command (const char *arg, int from_tty) |
| 267 | { |
| 268 | global_index_cache.disable (); |
| 269 | } |
| 270 | |
| 271 | /* "set index-cache directory" handler. */ |
| 272 | |
| 273 | static void |
| 274 | set_index_cache_directory_command (const char *arg, int from_tty, |
| 275 | cmd_list_element *element) |
| 276 | { |
| 277 | /* Make sure the index cache directory is absolute and tilde-expanded. */ |
| 278 | gdb::unique_xmalloc_ptr<char> abs (gdb_abspath (index_cache_directory)); |
| 279 | xfree (index_cache_directory); |
| 280 | index_cache_directory = abs.release (); |
| 281 | global_index_cache.set_directory (index_cache_directory); |
| 282 | } |
| 283 | |
| 284 | /* "show index-cache stats" handler. */ |
| 285 | |
| 286 | static void |
| 287 | show_index_cache_stats_command (const char *arg, int from_tty) |
| 288 | { |
| 289 | const char *indent = ""; |
| 290 | |
| 291 | /* If this command is invoked through "show index-cache", make the display a |
| 292 | bit nicer. */ |
| 293 | if (in_show_index_cache_command) |
| 294 | { |
| 295 | indent = " "; |
| 296 | printf_unfiltered ("\n"); |
| 297 | } |
| 298 | |
| 299 | printf_unfiltered (_("%s Cache hits (this session): %u\n"), |
| 300 | indent, global_index_cache.n_hits ()); |
| 301 | printf_unfiltered (_("%sCache misses (this session): %u\n"), |
| 302 | indent, global_index_cache.n_misses ()); |
| 303 | } |
| 304 | |
| 305 | void |
| 306 | _initialize_index_cache () |
| 307 | { |
| 308 | /* Set the default index cache directory. */ |
| 309 | std::string cache_dir = get_standard_cache_dir (); |
| 310 | if (!cache_dir.empty ()) |
| 311 | { |
| 312 | index_cache_directory = xstrdup (cache_dir.c_str ()); |
| 313 | global_index_cache.set_directory (std::move (cache_dir)); |
| 314 | } |
| 315 | else |
| 316 | warning (_("Couldn't determine a path for the index cache directory.")); |
| 317 | |
| 318 | /* set index-cache */ |
| 319 | add_prefix_cmd ("index-cache", class_files, set_index_cache_command, |
| 320 | _("Set index-cache options"), &set_index_cache_prefix_list, |
| 321 | "set index-cache ", false, &setlist); |
| 322 | |
| 323 | /* show index-cache */ |
| 324 | add_prefix_cmd ("index-cache", class_files, show_index_cache_command, |
| 325 | _("Show index-cache options"), &show_index_cache_prefix_list, |
| 326 | "show index-cache ", false, &showlist); |
| 327 | |
| 328 | /* set index-cache on */ |
| 329 | add_cmd ("on", class_files, set_index_cache_on_command, |
| 330 | _("Enable the index cache."), &set_index_cache_prefix_list); |
| 331 | |
| 332 | /* set index-cache off */ |
| 333 | add_cmd ("off", class_files, set_index_cache_off_command, |
| 334 | _("Disable the index cache."), &set_index_cache_prefix_list); |
| 335 | |
| 336 | /* set index-cache directory */ |
| 337 | add_setshow_filename_cmd ("directory", class_files, &index_cache_directory, |
| 338 | _("Set the directory of the index cache."), |
| 339 | _("Show the directory of the index cache."), |
| 340 | NULL, |
| 341 | set_index_cache_directory_command, NULL, |
| 342 | &set_index_cache_prefix_list, |
| 343 | &show_index_cache_prefix_list); |
| 344 | |
| 345 | /* show index-cache stats */ |
| 346 | add_cmd ("stats", class_files, show_index_cache_stats_command, |
| 347 | _("Show some stats about the index cache."), |
| 348 | &show_index_cache_prefix_list); |
| 349 | |
| 350 | /* set debug index-cache */ |
| 351 | add_setshow_boolean_cmd ("index-cache", class_maintenance, |
| 352 | &debug_index_cache, |
| 353 | _("Set display of index-cache debug messages."), |
| 354 | _("Show display of index-cache debug messages."), |
| 355 | _("\ |
| 356 | When non-zero, debugging output for the index cache is displayed."), |
| 357 | NULL, NULL, |
| 358 | &setdebuglist, &showdebuglist); |
| 359 | } |