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