1 /* Debug logging for the symbol file functions for the GNU debugger, GDB.
3 Copyright (C) 2013-2017 Free Software Foundation, Inc.
5 Contributed by Cygnus Support, using pieces from other GDB modules.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* Note: Be careful with functions that can throw errors.
23 We want to see a logging message regardless of whether an error was thrown.
24 This typically means printing a message before calling the real function
25 and then if the function returns a result printing a message after it
36 /* We need to save a pointer to the real symbol functions.
37 Plus, the debug versions are malloc'd because we have to NULL out the
38 ones that are NULL in the real copy. */
40 struct debug_sym_fns_data
42 const struct sym_fns *real_sf;
43 struct sym_fns debug_sf;
46 /* We need to record a pointer to the real set of functions for each
48 static const struct objfile_data *symfile_debug_objfile_data_key;
50 /* If non-zero all calls to the symfile functions are logged. */
51 static int debug_symfile = 0;
53 /* Return non-zero if symfile debug logging is installed. */
56 symfile_debug_installed (struct objfile *objfile)
58 return (objfile->sf != NULL
59 && objfile_data (objfile, symfile_debug_objfile_data_key) != NULL);
62 /* Utility return the name to print for SYMTAB. */
65 debug_symtab_name (struct symtab *symtab)
67 return symtab_to_filename_for_display (symtab);
70 /* Debugging version of struct quick_symbol_functions. */
73 debug_qf_has_symbols (struct objfile *objfile)
75 const struct debug_sym_fns_data *debug_data
76 = ((const struct debug_sym_fns_data *)
77 objfile_data (objfile, symfile_debug_objfile_data_key));
80 retval = debug_data->real_sf->qf->has_symbols (objfile);
82 fprintf_filtered (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
83 objfile_debug_name (objfile), retval);
88 static struct symtab *
89 debug_qf_find_last_source_symtab (struct objfile *objfile)
91 const struct debug_sym_fns_data *debug_data
92 = ((const struct debug_sym_fns_data *)
93 objfile_data (objfile, symfile_debug_objfile_data_key));
94 struct symtab *retval;
96 fprintf_filtered (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
97 objfile_debug_name (objfile));
99 retval = debug_data->real_sf->qf->find_last_source_symtab (objfile);
101 fprintf_filtered (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
102 retval ? debug_symtab_name (retval) : "NULL");
108 debug_qf_forget_cached_source_info (struct objfile *objfile)
110 const struct debug_sym_fns_data *debug_data
111 = ((const struct debug_sym_fns_data *)
112 objfile_data (objfile, symfile_debug_objfile_data_key));
114 fprintf_filtered (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
115 objfile_debug_name (objfile));
117 debug_data->real_sf->qf->forget_cached_source_info (objfile);
121 debug_qf_map_symtabs_matching_filename
122 (struct objfile *objfile, const char *name, const char *real_path,
123 gdb::function_view<bool (symtab *)> callback)
125 const struct debug_sym_fns_data *debug_data
126 = ((const struct debug_sym_fns_data *)
127 objfile_data (objfile, symfile_debug_objfile_data_key));
129 fprintf_filtered (gdb_stdlog,
130 "qf->map_symtabs_matching_filename (%s, \"%s\", \"%s\", %s)\n",
131 objfile_debug_name (objfile), name,
132 real_path ? real_path : NULL,
133 host_address_to_string (&callback));
135 bool retval = (debug_data->real_sf->qf->map_symtabs_matching_filename
136 (objfile, name, real_path, callback));
138 fprintf_filtered (gdb_stdlog,
139 "qf->map_symtabs_matching_filename (...) = %d\n",
145 static struct compunit_symtab *
146 debug_qf_lookup_symbol (struct objfile *objfile, int kind, const char *name,
149 const struct debug_sym_fns_data *debug_data
150 = ((const struct debug_sym_fns_data *)
151 objfile_data (objfile, symfile_debug_objfile_data_key));
152 struct compunit_symtab *retval;
154 fprintf_filtered (gdb_stdlog,
155 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
156 objfile_debug_name (objfile), kind, name,
157 domain_name (domain));
159 retval = debug_data->real_sf->qf->lookup_symbol (objfile, kind, name,
162 fprintf_filtered (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
164 ? debug_symtab_name (compunit_primary_filetab (retval))
171 debug_qf_print_stats (struct objfile *objfile)
173 const struct debug_sym_fns_data *debug_data
174 = ((const struct debug_sym_fns_data *)
175 objfile_data (objfile, symfile_debug_objfile_data_key));
177 fprintf_filtered (gdb_stdlog, "qf->print_stats (%s)\n",
178 objfile_debug_name (objfile));
180 debug_data->real_sf->qf->print_stats (objfile);
184 debug_qf_dump (struct objfile *objfile)
186 const struct debug_sym_fns_data *debug_data
187 = ((const struct debug_sym_fns_data *)
188 objfile_data (objfile, symfile_debug_objfile_data_key));
190 fprintf_filtered (gdb_stdlog, "qf->dump (%s)\n",
191 objfile_debug_name (objfile));
193 debug_data->real_sf->qf->dump (objfile);
197 debug_qf_relocate (struct objfile *objfile,
198 const struct section_offsets *new_offsets,
199 const struct section_offsets *delta)
201 const struct debug_sym_fns_data *debug_data
202 = ((const struct debug_sym_fns_data *)
203 objfile_data (objfile, symfile_debug_objfile_data_key));
205 fprintf_filtered (gdb_stdlog, "qf->relocate (%s, %s, %s)\n",
206 objfile_debug_name (objfile),
207 host_address_to_string (new_offsets),
208 host_address_to_string (delta));
210 debug_data->real_sf->qf->relocate (objfile, new_offsets, delta);
214 debug_qf_expand_symtabs_for_function (struct objfile *objfile,
215 const char *func_name)
217 const struct debug_sym_fns_data *debug_data
218 = ((const struct debug_sym_fns_data *)
219 objfile_data (objfile, symfile_debug_objfile_data_key));
221 fprintf_filtered (gdb_stdlog,
222 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
223 objfile_debug_name (objfile), func_name);
225 debug_data->real_sf->qf->expand_symtabs_for_function (objfile, func_name);
229 debug_qf_expand_all_symtabs (struct objfile *objfile)
231 const struct debug_sym_fns_data *debug_data
232 = ((const struct debug_sym_fns_data *)
233 objfile_data (objfile, symfile_debug_objfile_data_key));
235 fprintf_filtered (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
236 objfile_debug_name (objfile));
238 debug_data->real_sf->qf->expand_all_symtabs (objfile);
242 debug_qf_expand_symtabs_with_fullname (struct objfile *objfile,
243 const char *fullname)
245 const struct debug_sym_fns_data *debug_data
246 = ((const struct debug_sym_fns_data *)
247 objfile_data (objfile, symfile_debug_objfile_data_key));
249 fprintf_filtered (gdb_stdlog,
250 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
251 objfile_debug_name (objfile), fullname);
253 debug_data->real_sf->qf->expand_symtabs_with_fullname (objfile, fullname);
257 debug_qf_map_matching_symbols (struct objfile *objfile,
258 const char *name, domain_enum domain,
260 int (*callback) (struct block *,
261 struct symbol *, void *),
263 symbol_name_match_type match,
264 symbol_compare_ftype *ordered_compare)
266 const struct debug_sym_fns_data *debug_data
267 = ((const struct debug_sym_fns_data *)
268 objfile_data (objfile, symfile_debug_objfile_data_key));
270 fprintf_filtered (gdb_stdlog,
271 "qf->map_matching_symbols (%s, \"%s\", %s, %d, %s, %s, %s, %s)\n",
272 objfile_debug_name (objfile), name,
273 domain_name (domain), global,
274 host_address_to_string (callback),
275 host_address_to_string (data),
276 plongest ((LONGEST) match),
277 host_address_to_string (ordered_compare));
279 debug_data->real_sf->qf->map_matching_symbols (objfile, name,
287 debug_qf_expand_symtabs_matching
288 (struct objfile *objfile,
289 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
290 const lookup_name_info &lookup_name,
291 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
292 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
293 enum search_domain kind)
295 const struct debug_sym_fns_data *debug_data
296 = ((const struct debug_sym_fns_data *)
297 objfile_data (objfile, symfile_debug_objfile_data_key));
299 fprintf_filtered (gdb_stdlog,
300 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
301 objfile_debug_name (objfile),
302 host_address_to_string (&file_matcher),
303 host_address_to_string (&symbol_matcher),
304 host_address_to_string (&expansion_notify),
305 search_domain_name (kind));
307 debug_data->real_sf->qf->expand_symtabs_matching (objfile,
315 static struct compunit_symtab *
316 debug_qf_find_pc_sect_compunit_symtab (struct objfile *objfile,
317 struct bound_minimal_symbol msymbol,
319 struct obj_section *section,
322 const struct debug_sym_fns_data *debug_data
323 = ((const struct debug_sym_fns_data *)
324 objfile_data (objfile, symfile_debug_objfile_data_key));
325 struct compunit_symtab *retval;
327 fprintf_filtered (gdb_stdlog,
328 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
329 objfile_debug_name (objfile),
330 host_address_to_string (msymbol.minsym),
332 host_address_to_string (section),
336 = debug_data->real_sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
340 fprintf_filtered (gdb_stdlog,
341 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
343 ? debug_symtab_name (compunit_primary_filetab (retval))
350 debug_qf_map_symbol_filenames (struct objfile *objfile,
351 symbol_filename_ftype *fun, void *data,
354 const struct debug_sym_fns_data *debug_data
355 = ((const struct debug_sym_fns_data *)
356 objfile_data (objfile, symfile_debug_objfile_data_key));
357 fprintf_filtered (gdb_stdlog,
358 "qf->map_symbol_filenames (%s, %s, %s, %d)\n",
359 objfile_debug_name (objfile),
360 host_address_to_string (fun),
361 host_address_to_string (data),
364 debug_data->real_sf->qf->map_symbol_filenames (objfile, fun, data,
368 static const struct quick_symbol_functions debug_sym_quick_functions =
370 debug_qf_has_symbols,
371 debug_qf_find_last_source_symtab,
372 debug_qf_forget_cached_source_info,
373 debug_qf_map_symtabs_matching_filename,
374 debug_qf_lookup_symbol,
375 debug_qf_print_stats,
378 debug_qf_expand_symtabs_for_function,
379 debug_qf_expand_all_symtabs,
380 debug_qf_expand_symtabs_with_fullname,
381 debug_qf_map_matching_symbols,
382 debug_qf_expand_symtabs_matching,
383 debug_qf_find_pc_sect_compunit_symtab,
384 debug_qf_map_symbol_filenames
387 /* Debugging version of struct sym_probe_fns. */
389 static const std::vector<probe *> &
390 debug_sym_get_probes (struct objfile *objfile)
392 const struct debug_sym_fns_data *debug_data
393 = ((const struct debug_sym_fns_data *)
394 objfile_data (objfile, symfile_debug_objfile_data_key));
396 const std::vector<probe *> &retval
397 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
399 fprintf_filtered (gdb_stdlog,
400 "probes->sym_get_probes (%s) = %s\n",
401 objfile_debug_name (objfile),
402 host_address_to_string (retval.data ()));
407 static const struct sym_probe_fns debug_sym_probe_fns =
409 debug_sym_get_probes,
412 /* Debugging version of struct sym_fns. */
415 debug_sym_new_init (struct objfile *objfile)
417 const struct debug_sym_fns_data *debug_data
418 = ((const struct debug_sym_fns_data *)
419 objfile_data (objfile, symfile_debug_objfile_data_key));
421 fprintf_filtered (gdb_stdlog, "sf->sym_new_init (%s)\n",
422 objfile_debug_name (objfile));
424 debug_data->real_sf->sym_new_init (objfile);
428 debug_sym_init (struct objfile *objfile)
430 const struct debug_sym_fns_data *debug_data
431 = ((const struct debug_sym_fns_data *)
432 objfile_data (objfile, symfile_debug_objfile_data_key));
434 fprintf_filtered (gdb_stdlog, "sf->sym_init (%s)\n",
435 objfile_debug_name (objfile));
437 debug_data->real_sf->sym_init (objfile);
441 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
443 const struct debug_sym_fns_data *debug_data
444 = ((const struct debug_sym_fns_data *)
445 objfile_data (objfile, symfile_debug_objfile_data_key));
447 fprintf_filtered (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
448 objfile_debug_name (objfile), (unsigned) symfile_flags);
450 debug_data->real_sf->sym_read (objfile, symfile_flags);
454 debug_sym_read_psymbols (struct objfile *objfile)
456 const struct debug_sym_fns_data *debug_data
457 = ((const struct debug_sym_fns_data *)
458 objfile_data (objfile, symfile_debug_objfile_data_key));
460 fprintf_filtered (gdb_stdlog, "sf->sym_read_psymbols (%s)\n",
461 objfile_debug_name (objfile));
463 debug_data->real_sf->sym_read_psymbols (objfile);
467 debug_sym_finish (struct objfile *objfile)
469 const struct debug_sym_fns_data *debug_data
470 = ((const struct debug_sym_fns_data *)
471 objfile_data (objfile, symfile_debug_objfile_data_key));
473 fprintf_filtered (gdb_stdlog, "sf->sym_finish (%s)\n",
474 objfile_debug_name (objfile));
476 debug_data->real_sf->sym_finish (objfile);
480 debug_sym_offsets (struct objfile *objfile,
481 const struct section_addr_info *info)
483 const struct debug_sym_fns_data *debug_data
484 = ((const struct debug_sym_fns_data *)
485 objfile_data (objfile, symfile_debug_objfile_data_key));
487 fprintf_filtered (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
488 objfile_debug_name (objfile),
489 host_address_to_string (info));
491 debug_data->real_sf->sym_offsets (objfile, info);
494 static struct symfile_segment_data *
495 debug_sym_segments (bfd *abfd)
497 /* This API function is annoying, it doesn't take a "this" pointer.
498 Fortunately it is only used in one place where we (re-)lookup the
499 sym_fns table to use. Thus we will never be called. */
500 gdb_assert_not_reached ("debug_sym_segments called");
504 debug_sym_read_linetable (struct objfile *objfile)
506 const struct debug_sym_fns_data *debug_data
507 = ((const struct debug_sym_fns_data *)
508 objfile_data (objfile, symfile_debug_objfile_data_key));
510 fprintf_filtered (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
511 objfile_debug_name (objfile));
513 debug_data->real_sf->sym_read_linetable (objfile);
517 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
519 const struct debug_sym_fns_data *debug_data
520 = ((const struct debug_sym_fns_data *)
521 objfile_data (objfile, symfile_debug_objfile_data_key));
524 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
526 fprintf_filtered (gdb_stdlog,
527 "sf->sym_relocate (%s, %s, %s) = %s\n",
528 objfile_debug_name (objfile),
529 host_address_to_string (sectp),
530 host_address_to_string (buf),
531 host_address_to_string (retval));
536 /* Template of debugging version of struct sym_fns.
537 A copy is made, with sym_flavour updated, and a pointer to the real table
538 installed in real_sf, and then a pointer to the copy is installed in the
541 static const struct sym_fns debug_sym_fns =
546 debug_sym_read_psymbols,
550 debug_sym_read_linetable,
552 &debug_sym_probe_fns,
553 &debug_sym_quick_functions
556 /* Free the copy of sym_fns recorded in the registry. */
559 symfile_debug_free_objfile (struct objfile *objfile, void *datum)
564 /* Install the debugging versions of the symfile functions for OBJFILE.
565 Do not call this if the debug versions are already installed. */
568 install_symfile_debug_logging (struct objfile *objfile)
570 const struct sym_fns *real_sf;
571 struct debug_sym_fns_data *debug_data;
573 /* The debug versions should not already be installed. */
574 gdb_assert (!symfile_debug_installed (objfile));
576 real_sf = objfile->sf;
578 /* Alas we have to preserve NULL entries in REAL_SF. */
579 debug_data = XCNEW (struct debug_sym_fns_data);
581 #define COPY_SF_PTR(from, to, name, func) \
584 (to)->debug_sf.name = func; \
587 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
588 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
589 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
590 COPY_SF_PTR (real_sf, debug_data, sym_read_psymbols,
591 debug_sym_read_psymbols);
592 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
593 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
594 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
595 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
596 debug_sym_read_linetable);
597 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
598 if (real_sf->sym_probe_fns)
599 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
600 debug_data->debug_sf.qf = &debug_sym_quick_functions;
604 debug_data->real_sf = real_sf;
605 set_objfile_data (objfile, symfile_debug_objfile_data_key, debug_data);
606 objfile->sf = &debug_data->debug_sf;
609 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
610 Do not call this if the debug versions are not installed. */
613 uninstall_symfile_debug_logging (struct objfile *objfile)
615 struct debug_sym_fns_data *debug_data;
617 /* The debug versions should be currently installed. */
618 gdb_assert (symfile_debug_installed (objfile));
620 debug_data = ((struct debug_sym_fns_data *)
621 objfile_data (objfile, symfile_debug_objfile_data_key));
623 objfile->sf = debug_data->real_sf;
625 set_objfile_data (objfile, symfile_debug_objfile_data_key, NULL);
628 /* Call this function to set OBJFILE->SF.
629 Do not set OBJFILE->SF directly. */
632 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
634 if (symfile_debug_installed (objfile))
636 gdb_assert (debug_symfile);
637 /* Remove the current one, and reinstall a new one later. */
638 uninstall_symfile_debug_logging (objfile);
641 /* Assume debug logging is disabled. */
644 /* Turn debug logging on if enabled. */
646 install_symfile_debug_logging (objfile);
650 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
652 struct program_space *pspace;
653 struct objfile *objfile;
656 ALL_PSPACE_OBJFILES (pspace, objfile)
660 if (!symfile_debug_installed (objfile))
661 install_symfile_debug_logging (objfile);
665 if (symfile_debug_installed (objfile))
666 uninstall_symfile_debug_logging (objfile);
672 show_debug_symfile (struct ui_file *file, int from_tty,
673 struct cmd_list_element *c, const char *value)
675 fprintf_filtered (file, _("Symfile debugging is %s.\n"), value);
679 _initialize_symfile_debug (void)
681 symfile_debug_objfile_data_key
682 = register_objfile_data_with_cleanup (NULL, symfile_debug_free_objfile);
684 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
685 Set debugging of the symfile functions."), _("\
686 Show debugging of the symfile functions."), _("\
687 When enabled, all calls to the symfile functions are logged."),
688 set_debug_symfile, show_debug_symfile,
689 &setdebuglist, &showdebuglist);
691 /* Note: We don't need a new-objfile observer because debug logging
692 will be installed when objfile init'n calls objfile_set_sym_fns. */