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