1 /* Debug logging for the symbol file functions for the GNU debugger, GDB.
3 Copyright (C) 2013-2021 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
31 #include "observable.h"
36 #include "filenames.h"
38 /* We need to save a pointer to the real symbol functions.
39 Plus, the debug versions are malloc'd because we have to NULL out the
40 ones that are NULL in the real copy. */
42 struct debug_sym_fns_data
44 const struct sym_fns
*real_sf
= nullptr;
45 struct sym_fns debug_sf
{};
48 /* We need to record a pointer to the real set of functions for each
50 static const struct objfile_key
<debug_sym_fns_data
>
51 symfile_debug_objfile_data_key
;
53 /* If true all calls to the symfile functions are logged. */
54 static bool debug_symfile
= false;
56 /* Return non-zero if symfile debug logging is installed. */
59 symfile_debug_installed (struct objfile
*objfile
)
61 return (objfile
->sf
!= NULL
62 && symfile_debug_objfile_data_key
.get (objfile
) != NULL
);
65 /* Utility return the name to print for SYMTAB. */
68 debug_symtab_name (struct symtab
*symtab
)
70 return symtab_to_filename_for_display (symtab
);
77 objfile::has_partial_symbols ()
81 /* If we have not read psymbols, but we have a function capable of reading
82 them, then that is an indication that they are in fact available. Without
83 this function the symbols may have been already read in but they also may
84 not be present in this objfile. */
85 for (const auto &iter
: qf
)
87 if ((flags
& OBJF_PSYMTABS_READ
) == 0
88 && iter
->can_lazily_read_symbols ())
91 retval
= iter
->has_symbols (this);
97 fprintf_filtered (gdb_stdlog
, "qf->has_symbols (%s) = %d\n",
98 objfile_debug_name (this), retval
);
104 objfile::find_last_source_symtab ()
106 struct symtab
*retval
= nullptr;
109 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (%s)\n",
110 objfile_debug_name (this));
112 for (const auto &iter
: qf
)
114 retval
= iter
->find_last_source_symtab (this);
115 if (retval
!= nullptr)
120 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (...) = %s\n",
121 retval
? debug_symtab_name (retval
) : "NULL");
127 objfile::forget_cached_source_info ()
130 fprintf_filtered (gdb_stdlog
, "qf->forget_cached_source_info (%s)\n",
131 objfile_debug_name (this));
133 for (const auto &iter
: qf
)
134 iter
->forget_cached_source_info (this);
138 objfile::map_symtabs_matching_filename
139 (const char *name
, const char *real_path
,
140 gdb::function_view
<bool (symtab
*)> callback
)
143 fprintf_filtered (gdb_stdlog
,
144 "qf->map_symtabs_matching_filename (%s, \"%s\", "
146 objfile_debug_name (this), name
,
147 real_path
? real_path
: NULL
,
148 host_address_to_string (&callback
));
151 const char *name_basename
= lbasename (name
);
153 auto match_one_filename
= [&] (const char *filename
, bool basenames
)
155 if (compare_filenames_for_search (filename
, name
))
157 if (basenames
&& FILENAME_CMP (name_basename
, filename
) == 0)
159 if (real_path
!= nullptr && IS_ABSOLUTE_PATH (filename
)
160 && IS_ABSOLUTE_PATH (real_path
))
161 return filename_cmp (filename
, real_path
) == 0;
165 compunit_symtab
*last_made
= this->compunit_symtabs
;
167 auto on_expansion
= [&] (compunit_symtab
*symtab
)
169 /* The callback to iterate_over_some_symtabs returns false to keep
170 going and true to continue, so we have to invert the result
171 here, for expand_symtabs_matching. */
172 bool result
= !iterate_over_some_symtabs (name
, real_path
,
173 this->compunit_symtabs
,
176 last_made
= this->compunit_symtabs
;
180 for (const auto &iter
: qf
)
182 if (!iter
->expand_symtabs_matching (this,
188 | SEARCH_STATIC_BLOCK
),
198 fprintf_filtered (gdb_stdlog
,
199 "qf->map_symtabs_matching_filename (...) = %d\n",
202 /* We must re-invert the return value here to match the caller's
207 struct compunit_symtab
*
208 objfile::lookup_symbol (block_enum kind
, const char *name
, domain_enum domain
)
210 struct compunit_symtab
*retval
= nullptr;
213 fprintf_filtered (gdb_stdlog
,
214 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
215 objfile_debug_name (this), kind
, name
,
216 domain_name (domain
));
218 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
220 auto search_one_symtab
= [&] (compunit_symtab
*stab
)
222 struct symbol
*sym
, *with_opaque
= NULL
;
223 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
224 const struct block
*block
= BLOCKVECTOR_BLOCK (bv
, kind
);
226 sym
= block_find_symbol (block
, name
, domain
,
227 block_find_non_opaque_type_preferred
,
230 /* Some caution must be observed with overloaded functions
231 and methods, since the index will not contain any overload
232 information (but NAME might contain it). */
235 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
241 if (with_opaque
!= NULL
242 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
245 /* Keep looking through other psymtabs. */
249 for (const auto &iter
: qf
)
251 if (!iter
->expand_symtabs_matching (this,
257 ? SEARCH_GLOBAL_BLOCK
258 : SEARCH_STATIC_BLOCK
,
265 fprintf_filtered (gdb_stdlog
, "qf->lookup_symbol (...) = %s\n",
267 ? debug_symtab_name (compunit_primary_filetab (retval
))
274 objfile::print_stats (bool print_bcache
)
277 fprintf_filtered (gdb_stdlog
, "qf->print_stats (%s, %d)\n",
278 objfile_debug_name (this), print_bcache
);
280 for (const auto &iter
: qf
)
281 iter
->print_stats (this, print_bcache
);
288 fprintf_filtered (gdb_stdlog
, "qf->dump (%s)\n",
289 objfile_debug_name (this));
291 for (const auto &iter
: qf
)
296 objfile::expand_symtabs_for_function (const char *func_name
)
299 fprintf_filtered (gdb_stdlog
,
300 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
301 objfile_debug_name (this), func_name
);
303 lookup_name_info
base_lookup (func_name
, symbol_name_match_type::FULL
);
304 lookup_name_info lookup_name
= base_lookup
.make_ignore_params ();
306 for (const auto &iter
: qf
)
307 iter
->expand_symtabs_matching (this,
313 | SEARCH_STATIC_BLOCK
),
319 objfile::expand_all_symtabs ()
322 fprintf_filtered (gdb_stdlog
, "qf->expand_all_symtabs (%s)\n",
323 objfile_debug_name (this));
325 for (const auto &iter
: qf
)
326 iter
->expand_all_symtabs (this);
330 objfile::expand_symtabs_with_fullname (const char *fullname
)
333 fprintf_filtered (gdb_stdlog
,
334 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
335 objfile_debug_name (this), fullname
);
337 const char *basename
= lbasename (fullname
);
338 auto file_matcher
= [&] (const char *filename
, bool basenames
)
340 return filename_cmp (basenames
? basename
: fullname
, filename
) == 0;
343 for (const auto &iter
: qf
)
344 iter
->expand_symtabs_matching (this,
350 | SEARCH_STATIC_BLOCK
),
356 objfile::expand_matching_symbols
357 (const lookup_name_info
&name
, domain_enum domain
,
359 symbol_compare_ftype
*ordered_compare
)
362 fprintf_filtered (gdb_stdlog
,
363 "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
364 objfile_debug_name (this),
365 domain_name (domain
), global
,
366 host_address_to_string (ordered_compare
));
368 for (const auto &iter
: qf
)
369 iter
->expand_matching_symbols (this, name
, domain
, global
,
374 objfile::expand_symtabs_matching
375 (gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
376 const lookup_name_info
*lookup_name
,
377 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
378 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
379 block_search_flags search_flags
,
381 enum search_domain kind
)
384 fprintf_filtered (gdb_stdlog
,
385 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
386 objfile_debug_name (this),
387 host_address_to_string (&file_matcher
),
388 host_address_to_string (&symbol_matcher
),
389 host_address_to_string (&expansion_notify
),
390 search_domain_name (kind
));
392 for (const auto &iter
: qf
)
393 if (!iter
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
394 symbol_matcher
, expansion_notify
,
395 search_flags
, domain
, kind
))
400 struct compunit_symtab
*
401 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
403 struct obj_section
*section
,
406 struct compunit_symtab
*retval
= nullptr;
409 fprintf_filtered (gdb_stdlog
,
410 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
411 objfile_debug_name (this),
412 host_address_to_string (msymbol
.minsym
),
414 host_address_to_string (section
),
417 for (const auto &iter
: qf
)
419 retval
= iter
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
421 if (retval
!= nullptr)
426 fprintf_filtered (gdb_stdlog
,
427 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
429 ? debug_symtab_name (compunit_primary_filetab (retval
))
436 objfile::map_symbol_filenames (gdb::function_view
<symbol_filename_ftype
> fun
,
440 fprintf_filtered (gdb_stdlog
,
441 "qf->map_symbol_filenames (%s, ..., %d)\n",
442 objfile_debug_name (this),
445 for (const auto &iter
: qf
)
446 iter
->map_symbol_filenames (this, fun
, need_fullname
);
449 struct compunit_symtab
*
450 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
453 fprintf_filtered (gdb_stdlog
,
454 "qf->find_compunit_symtab_by_address (%s, %s)\n",
455 objfile_debug_name (this),
456 hex_string (address
));
458 struct compunit_symtab
*result
= NULL
;
459 for (const auto &iter
: qf
)
461 result
= iter
->find_compunit_symtab_by_address (this, address
);
462 if (result
!= nullptr)
467 fprintf_filtered (gdb_stdlog
,
468 "qf->find_compunit_symtab_by_address (...) = %s\n",
470 ? debug_symtab_name (compunit_primary_filetab (result
))
477 objfile::lookup_global_symbol_language (const char *name
,
479 bool *symbol_found_p
)
481 enum language result
= language_unknown
;
482 *symbol_found_p
= false;
484 for (const auto &iter
: qf
)
486 result
= iter
->lookup_global_symbol_language (this, name
, domain
,
496 objfile::require_partial_symbols (bool verbose
)
498 if ((flags
& OBJF_PSYMTABS_READ
) == 0)
500 flags
|= OBJF_PSYMTABS_READ
;
502 bool printed
= false;
503 for (const auto &iter
: qf
)
505 if (iter
->can_lazily_read_symbols ())
507 if (verbose
&& !printed
)
509 printf_filtered (_("Reading symbols from %s...\n"),
510 objfile_name (this));
513 iter
->read_partial_symbols (this);
516 if (printed
&& !objfile_has_symbols (this))
517 printf_filtered (_("(No debugging symbols found in %s)\n"),
518 objfile_name (this));
523 /* Debugging version of struct sym_probe_fns. */
525 static const std::vector
<std::unique_ptr
<probe
>> &
526 debug_sym_get_probes (struct objfile
*objfile
)
528 const struct debug_sym_fns_data
*debug_data
529 = symfile_debug_objfile_data_key
.get (objfile
);
531 const std::vector
<std::unique_ptr
<probe
>> &retval
532 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
534 fprintf_filtered (gdb_stdlog
,
535 "probes->sym_get_probes (%s) = %s\n",
536 objfile_debug_name (objfile
),
537 host_address_to_string (retval
.data ()));
542 static const struct sym_probe_fns debug_sym_probe_fns
=
544 debug_sym_get_probes
,
547 /* Debugging version of struct sym_fns. */
550 debug_sym_new_init (struct objfile
*objfile
)
552 const struct debug_sym_fns_data
*debug_data
553 = symfile_debug_objfile_data_key
.get (objfile
);
555 fprintf_filtered (gdb_stdlog
, "sf->sym_new_init (%s)\n",
556 objfile_debug_name (objfile
));
558 debug_data
->real_sf
->sym_new_init (objfile
);
562 debug_sym_init (struct objfile
*objfile
)
564 const struct debug_sym_fns_data
*debug_data
565 = symfile_debug_objfile_data_key
.get (objfile
);
567 fprintf_filtered (gdb_stdlog
, "sf->sym_init (%s)\n",
568 objfile_debug_name (objfile
));
570 debug_data
->real_sf
->sym_init (objfile
);
574 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
576 const struct debug_sym_fns_data
*debug_data
577 = symfile_debug_objfile_data_key
.get (objfile
);
579 fprintf_filtered (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
580 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
582 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
586 debug_sym_finish (struct objfile
*objfile
)
588 const struct debug_sym_fns_data
*debug_data
589 = symfile_debug_objfile_data_key
.get (objfile
);
591 fprintf_filtered (gdb_stdlog
, "sf->sym_finish (%s)\n",
592 objfile_debug_name (objfile
));
594 debug_data
->real_sf
->sym_finish (objfile
);
598 debug_sym_offsets (struct objfile
*objfile
,
599 const section_addr_info
&info
)
601 const struct debug_sym_fns_data
*debug_data
602 = symfile_debug_objfile_data_key
.get (objfile
);
604 fprintf_filtered (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
605 objfile_debug_name (objfile
),
606 host_address_to_string (&info
));
608 debug_data
->real_sf
->sym_offsets (objfile
, info
);
611 static symfile_segment_data_up
612 debug_sym_segments (bfd
*abfd
)
614 /* This API function is annoying, it doesn't take a "this" pointer.
615 Fortunately it is only used in one place where we (re-)lookup the
616 sym_fns table to use. Thus we will never be called. */
617 gdb_assert_not_reached ("debug_sym_segments called");
621 debug_sym_read_linetable (struct objfile
*objfile
)
623 const struct debug_sym_fns_data
*debug_data
624 = symfile_debug_objfile_data_key
.get (objfile
);
626 fprintf_filtered (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
627 objfile_debug_name (objfile
));
629 debug_data
->real_sf
->sym_read_linetable (objfile
);
633 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
635 const struct debug_sym_fns_data
*debug_data
636 = symfile_debug_objfile_data_key
.get (objfile
);
639 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
641 fprintf_filtered (gdb_stdlog
,
642 "sf->sym_relocate (%s, %s, %s) = %s\n",
643 objfile_debug_name (objfile
),
644 host_address_to_string (sectp
),
645 host_address_to_string (buf
),
646 host_address_to_string (retval
));
651 /* Template of debugging version of struct sym_fns.
652 A copy is made, with sym_flavour updated, and a pointer to the real table
653 installed in real_sf, and then a pointer to the copy is installed in the
656 static const struct sym_fns debug_sym_fns
=
664 debug_sym_read_linetable
,
666 &debug_sym_probe_fns
,
669 /* Install the debugging versions of the symfile functions for OBJFILE.
670 Do not call this if the debug versions are already installed. */
673 install_symfile_debug_logging (struct objfile
*objfile
)
675 const struct sym_fns
*real_sf
;
676 struct debug_sym_fns_data
*debug_data
;
678 /* The debug versions should not already be installed. */
679 gdb_assert (!symfile_debug_installed (objfile
));
681 real_sf
= objfile
->sf
;
683 /* Alas we have to preserve NULL entries in REAL_SF. */
684 debug_data
= new struct debug_sym_fns_data
;
686 #define COPY_SF_PTR(from, to, name, func) \
689 (to)->debug_sf.name = func; \
692 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
693 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
694 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
695 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
696 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
697 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
698 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
699 debug_sym_read_linetable
);
700 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
701 if (real_sf
->sym_probe_fns
)
702 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
706 debug_data
->real_sf
= real_sf
;
707 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
708 objfile
->sf
= &debug_data
->debug_sf
;
711 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
712 Do not call this if the debug versions are not installed. */
715 uninstall_symfile_debug_logging (struct objfile
*objfile
)
717 struct debug_sym_fns_data
*debug_data
;
719 /* The debug versions should be currently installed. */
720 gdb_assert (symfile_debug_installed (objfile
));
722 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
724 objfile
->sf
= debug_data
->real_sf
;
725 symfile_debug_objfile_data_key
.clear (objfile
);
728 /* Call this function to set OBJFILE->SF.
729 Do not set OBJFILE->SF directly. */
732 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
734 if (symfile_debug_installed (objfile
))
736 gdb_assert (debug_symfile
);
737 /* Remove the current one, and reinstall a new one later. */
738 uninstall_symfile_debug_logging (objfile
);
741 /* Assume debug logging is disabled. */
744 /* Turn debug logging on if enabled. */
746 install_symfile_debug_logging (objfile
);
750 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
752 for (struct program_space
*pspace
: program_spaces
)
753 for (objfile
*objfile
: pspace
->objfiles ())
757 if (!symfile_debug_installed (objfile
))
758 install_symfile_debug_logging (objfile
);
762 if (symfile_debug_installed (objfile
))
763 uninstall_symfile_debug_logging (objfile
);
769 show_debug_symfile (struct ui_file
*file
, int from_tty
,
770 struct cmd_list_element
*c
, const char *value
)
772 fprintf_filtered (file
, _("Symfile debugging is %s.\n"), value
);
775 void _initialize_symfile_debug ();
777 _initialize_symfile_debug ()
779 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
780 Set debugging of the symfile functions."), _("\
781 Show debugging of the symfile functions."), _("\
782 When enabled, all calls to the symfile functions are logged."),
783 set_debug_symfile
, show_debug_symfile
,
784 &setdebuglist
, &showdebuglist
);
786 /* Note: We don't need a new-objfile observer because debug logging
787 will be installed when objfile init'n calls objfile_set_sym_fns. */