1 /* Debug logging for the symbol file functions for the GNU debugger, GDB.
3 Copyright (C) 2013-2023 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"
37 #include "cli/cli-style.h"
39 /* We need to save a pointer to the real symbol functions.
40 Plus, the debug versions are malloc'd because we have to NULL out the
41 ones that are NULL in the real copy. */
43 struct debug_sym_fns_data
45 const struct sym_fns
*real_sf
= nullptr;
46 struct sym_fns debug_sf
{};
49 /* We need to record a pointer to the real set of functions for each
51 static const registry
<objfile
>::key
<debug_sym_fns_data
>
52 symfile_debug_objfile_data_key
;
54 /* If true all calls to the symfile functions are logged. */
55 static bool debug_symfile
= false;
57 /* Return non-zero if symfile debug logging is installed. */
60 symfile_debug_installed (struct objfile
*objfile
)
62 return (objfile
->sf
!= NULL
63 && symfile_debug_objfile_data_key
.get (objfile
) != NULL
);
66 /* Utility return the name to print for SYMTAB. */
69 debug_symtab_name (struct symtab
*symtab
)
71 return symtab_to_filename_for_display (symtab
);
78 objfile::has_partial_symbols ()
82 /* If we have not read psymbols, but we have a function capable of reading
83 them, then that is an indication that they are in fact available. Without
84 this function the symbols may have been already read in but they also may
85 not be present in this objfile. */
86 for (const auto &iter
: qf
)
88 if ((flags
& OBJF_PSYMTABS_READ
) == 0
89 && iter
->can_lazily_read_symbols ())
92 retval
= iter
->has_symbols (this);
98 gdb_printf (gdb_stdlog
, "qf->has_symbols (%s) = %d\n",
99 objfile_debug_name (this), retval
);
104 /* See objfiles.h. */
106 objfile::has_unexpanded_symtabs ()
109 gdb_printf (gdb_stdlog
, "qf->has_unexpanded_symtabs (%s)\n",
110 objfile_debug_name (this));
113 for (const auto &iter
: qf_require_partial_symbols ())
115 if (iter
->has_unexpanded_symtabs (this))
123 gdb_printf (gdb_stdlog
, "qf->has_unexpanded_symtabs (%s) = %d\n",
124 objfile_debug_name (this), (result
? 1 : 0));
130 objfile::find_last_source_symtab ()
132 struct symtab
*retval
= nullptr;
135 gdb_printf (gdb_stdlog
, "qf->find_last_source_symtab (%s)\n",
136 objfile_debug_name (this));
138 for (const auto &iter
: qf_require_partial_symbols ())
140 retval
= iter
->find_last_source_symtab (this);
141 if (retval
!= nullptr)
146 gdb_printf (gdb_stdlog
, "qf->find_last_source_symtab (...) = %s\n",
147 retval
? debug_symtab_name (retval
) : "NULL");
153 objfile::forget_cached_source_info ()
156 gdb_printf (gdb_stdlog
, "qf->forget_cached_source_info (%s)\n",
157 objfile_debug_name (this));
159 for (compunit_symtab
*cu
: compunits ())
161 for (symtab
*s
: cu
->filetabs ())
163 if (s
->fullname
!= NULL
)
171 for (const auto &iter
: qf_require_partial_symbols ())
172 iter
->forget_cached_source_info (this);
176 objfile::map_symtabs_matching_filename
177 (const char *name
, const char *real_path
,
178 gdb::function_view
<bool (symtab
*)> callback
)
181 gdb_printf (gdb_stdlog
,
182 "qf->map_symtabs_matching_filename (%s, \"%s\", "
184 objfile_debug_name (this), name
,
185 real_path
? real_path
: NULL
,
186 host_address_to_string (&callback
));
189 const char *name_basename
= lbasename (name
);
191 auto match_one_filename
= [&] (const char *filename
, bool basenames
)
193 if (compare_filenames_for_search (filename
, name
))
195 if (basenames
&& FILENAME_CMP (name_basename
, filename
) == 0)
197 if (real_path
!= nullptr && IS_ABSOLUTE_PATH (filename
)
198 && IS_ABSOLUTE_PATH (real_path
))
199 return filename_cmp (filename
, real_path
) == 0;
203 compunit_symtab
*last_made
= this->compunit_symtabs
;
205 auto on_expansion
= [&] (compunit_symtab
*symtab
)
207 /* The callback to iterate_over_some_symtabs returns false to keep
208 going and true to continue, so we have to invert the result
209 here, for expand_symtabs_matching. */
210 bool result
= !iterate_over_some_symtabs (name
, real_path
,
211 this->compunit_symtabs
,
214 last_made
= this->compunit_symtabs
;
218 for (const auto &iter
: qf_require_partial_symbols ())
220 if (!iter
->expand_symtabs_matching (this,
226 | SEARCH_STATIC_BLOCK
),
236 gdb_printf (gdb_stdlog
,
237 "qf->map_symtabs_matching_filename (...) = %d\n",
240 /* We must re-invert the return value here to match the caller's
245 struct compunit_symtab
*
246 objfile::lookup_symbol (block_enum kind
, const char *name
, domain_enum domain
)
248 struct compunit_symtab
*retval
= nullptr;
251 gdb_printf (gdb_stdlog
,
252 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
253 objfile_debug_name (this), kind
, name
,
254 domain_name (domain
));
256 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
258 auto search_one_symtab
= [&] (compunit_symtab
*stab
)
260 struct symbol
*sym
, *with_opaque
= NULL
;
261 const struct blockvector
*bv
= stab
->blockvector ();
262 const struct block
*block
= bv
->block (kind
);
264 sym
= block_find_symbol (block
, lookup_name
, domain
, &with_opaque
);
266 /* Some caution must be observed with overloaded functions
267 and methods, since the index will not contain any overload
268 information (but NAME might contain it). */
276 if (with_opaque
!= nullptr)
279 /* Keep looking through other psymtabs. */
283 for (const auto &iter
: qf_require_partial_symbols ())
285 if (!iter
->expand_symtabs_matching (this,
291 ? SEARCH_GLOBAL_BLOCK
292 : SEARCH_STATIC_BLOCK
,
299 gdb_printf (gdb_stdlog
, "qf->lookup_symbol (...) = %s\n",
301 ? debug_symtab_name (retval
->primary_filetab ())
308 objfile::print_stats (bool print_bcache
)
311 gdb_printf (gdb_stdlog
, "qf->print_stats (%s, %d)\n",
312 objfile_debug_name (this), print_bcache
);
314 for (const auto &iter
: qf_require_partial_symbols ())
315 iter
->print_stats (this, print_bcache
);
322 gdb_printf (gdb_stdlog
, "qf->dump (%s)\n",
323 objfile_debug_name (this));
325 for (const auto &iter
: qf
)
330 objfile::expand_symtabs_for_function (const char *func_name
)
333 gdb_printf (gdb_stdlog
,
334 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
335 objfile_debug_name (this), func_name
);
337 lookup_name_info
base_lookup (func_name
, symbol_name_match_type::FULL
);
338 lookup_name_info lookup_name
= base_lookup
.make_ignore_params ();
340 for (const auto &iter
: qf_require_partial_symbols ())
341 iter
->expand_symtabs_matching (this,
347 | SEARCH_STATIC_BLOCK
),
353 objfile::expand_all_symtabs ()
356 gdb_printf (gdb_stdlog
, "qf->expand_all_symtabs (%s)\n",
357 objfile_debug_name (this));
359 for (const auto &iter
: qf_require_partial_symbols ())
360 iter
->expand_all_symtabs (this);
364 objfile::expand_symtabs_with_fullname (const char *fullname
)
367 gdb_printf (gdb_stdlog
,
368 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
369 objfile_debug_name (this), fullname
);
371 const char *basename
= lbasename (fullname
);
372 auto file_matcher
= [&] (const char *filename
, bool basenames
)
374 return filename_cmp (basenames
? basename
: fullname
, filename
) == 0;
377 for (const auto &iter
: qf_require_partial_symbols ())
378 iter
->expand_symtabs_matching (this,
384 | SEARCH_STATIC_BLOCK
),
390 objfile::expand_matching_symbols
391 (const lookup_name_info
&name
, domain_enum domain
,
393 symbol_compare_ftype
*ordered_compare
)
396 gdb_printf (gdb_stdlog
,
397 "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
398 objfile_debug_name (this),
399 domain_name (domain
), global
,
400 host_address_to_string (ordered_compare
));
402 for (const auto &iter
: qf_require_partial_symbols ())
403 iter
->expand_matching_symbols (this, name
, domain
, global
,
408 objfile::expand_symtabs_matching
409 (gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
410 const lookup_name_info
*lookup_name
,
411 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
412 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
413 block_search_flags search_flags
,
415 enum search_domain kind
)
417 /* This invariant is documented in quick-functions.h. */
418 gdb_assert (lookup_name
!= nullptr || symbol_matcher
== nullptr);
421 gdb_printf (gdb_stdlog
,
422 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
423 objfile_debug_name (this),
424 host_address_to_string (&file_matcher
),
425 host_address_to_string (&symbol_matcher
),
426 host_address_to_string (&expansion_notify
),
427 search_domain_name (kind
));
429 for (const auto &iter
: qf_require_partial_symbols ())
430 if (!iter
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
431 symbol_matcher
, expansion_notify
,
432 search_flags
, domain
, kind
))
437 struct compunit_symtab
*
438 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
440 struct obj_section
*section
,
443 struct compunit_symtab
*retval
= nullptr;
446 gdb_printf (gdb_stdlog
,
447 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
448 objfile_debug_name (this),
449 host_address_to_string (msymbol
.minsym
),
451 host_address_to_string (section
),
454 for (const auto &iter
: qf_require_partial_symbols ())
456 retval
= iter
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
458 if (retval
!= nullptr)
463 gdb_printf (gdb_stdlog
,
464 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
466 ? debug_symtab_name (retval
->primary_filetab ())
473 objfile::map_symbol_filenames (gdb::function_view
<symbol_filename_ftype
> fun
,
477 gdb_printf (gdb_stdlog
,
478 "qf->map_symbol_filenames (%s, ..., %d)\n",
479 objfile_debug_name (this),
482 for (const auto &iter
: qf_require_partial_symbols ())
483 iter
->map_symbol_filenames (this, fun
, need_fullname
);
486 struct compunit_symtab
*
487 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
490 gdb_printf (gdb_stdlog
,
491 "qf->find_compunit_symtab_by_address (%s, %s)\n",
492 objfile_debug_name (this),
493 hex_string (address
));
495 struct compunit_symtab
*result
= NULL
;
496 for (const auto &iter
: qf_require_partial_symbols ())
498 result
= iter
->find_compunit_symtab_by_address (this, address
);
499 if (result
!= nullptr)
504 gdb_printf (gdb_stdlog
,
505 "qf->find_compunit_symtab_by_address (...) = %s\n",
507 ? debug_symtab_name (result
->primary_filetab ())
514 objfile::lookup_global_symbol_language (const char *name
,
516 bool *symbol_found_p
)
518 enum language result
= language_unknown
;
519 *symbol_found_p
= false;
521 for (const auto &iter
: qf_require_partial_symbols ())
523 result
= iter
->lookup_global_symbol_language (this, name
, domain
,
533 objfile::require_partial_symbols (bool verbose
)
535 if ((flags
& OBJF_PSYMTABS_READ
) == 0)
537 flags
|= OBJF_PSYMTABS_READ
;
539 bool printed
= false;
540 for (const auto &iter
: qf
)
542 if (iter
->can_lazily_read_symbols ())
544 if (verbose
&& !printed
)
546 gdb_printf (_("Reading symbols from %ps...\n"),
547 styled_string (file_name_style
.style (),
548 objfile_name (this)));
551 iter
->read_partial_symbols (this);
554 if (printed
&& !objfile_has_symbols (this))
555 gdb_printf (_("(No debugging symbols found in %ps)\n"),
556 styled_string (file_name_style
.style (),
557 objfile_name (this)));
562 /* Debugging version of struct sym_probe_fns. */
564 static const std::vector
<std::unique_ptr
<probe
>> &
565 debug_sym_get_probes (struct objfile
*objfile
)
567 const struct debug_sym_fns_data
*debug_data
568 = symfile_debug_objfile_data_key
.get (objfile
);
570 const std::vector
<std::unique_ptr
<probe
>> &retval
571 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
573 gdb_printf (gdb_stdlog
,
574 "probes->sym_get_probes (%s) = %s\n",
575 objfile_debug_name (objfile
),
576 host_address_to_string (retval
.data ()));
581 static const struct sym_probe_fns debug_sym_probe_fns
=
583 debug_sym_get_probes
,
586 /* Debugging version of struct sym_fns. */
589 debug_sym_new_init (struct objfile
*objfile
)
591 const struct debug_sym_fns_data
*debug_data
592 = symfile_debug_objfile_data_key
.get (objfile
);
594 gdb_printf (gdb_stdlog
, "sf->sym_new_init (%s)\n",
595 objfile_debug_name (objfile
));
597 debug_data
->real_sf
->sym_new_init (objfile
);
601 debug_sym_init (struct objfile
*objfile
)
603 const struct debug_sym_fns_data
*debug_data
604 = symfile_debug_objfile_data_key
.get (objfile
);
606 gdb_printf (gdb_stdlog
, "sf->sym_init (%s)\n",
607 objfile_debug_name (objfile
));
609 debug_data
->real_sf
->sym_init (objfile
);
613 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
615 const struct debug_sym_fns_data
*debug_data
616 = symfile_debug_objfile_data_key
.get (objfile
);
618 gdb_printf (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
619 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
621 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
625 debug_sym_finish (struct objfile
*objfile
)
627 const struct debug_sym_fns_data
*debug_data
628 = symfile_debug_objfile_data_key
.get (objfile
);
630 gdb_printf (gdb_stdlog
, "sf->sym_finish (%s)\n",
631 objfile_debug_name (objfile
));
633 debug_data
->real_sf
->sym_finish (objfile
);
637 debug_sym_offsets (struct objfile
*objfile
,
638 const section_addr_info
&info
)
640 const struct debug_sym_fns_data
*debug_data
641 = symfile_debug_objfile_data_key
.get (objfile
);
643 gdb_printf (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
644 objfile_debug_name (objfile
),
645 host_address_to_string (&info
));
647 debug_data
->real_sf
->sym_offsets (objfile
, info
);
650 static symfile_segment_data_up
651 debug_sym_segments (bfd
*abfd
)
653 /* This API function is annoying, it doesn't take a "this" pointer.
654 Fortunately it is only used in one place where we (re-)lookup the
655 sym_fns table to use. Thus we will never be called. */
656 gdb_assert_not_reached ("debug_sym_segments called");
660 debug_sym_read_linetable (struct objfile
*objfile
)
662 const struct debug_sym_fns_data
*debug_data
663 = symfile_debug_objfile_data_key
.get (objfile
);
665 gdb_printf (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
666 objfile_debug_name (objfile
));
668 debug_data
->real_sf
->sym_read_linetable (objfile
);
672 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
674 const struct debug_sym_fns_data
*debug_data
675 = symfile_debug_objfile_data_key
.get (objfile
);
678 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
680 gdb_printf (gdb_stdlog
,
681 "sf->sym_relocate (%s, %s, %s) = %s\n",
682 objfile_debug_name (objfile
),
683 host_address_to_string (sectp
),
684 host_address_to_string (buf
),
685 host_address_to_string (retval
));
690 /* Template of debugging version of struct sym_fns.
691 A copy is made, with sym_flavour updated, and a pointer to the real table
692 installed in real_sf, and then a pointer to the copy is installed in the
695 static const struct sym_fns debug_sym_fns
=
703 debug_sym_read_linetable
,
705 &debug_sym_probe_fns
,
708 /* Install the debugging versions of the symfile functions for OBJFILE.
709 Do not call this if the debug versions are already installed. */
712 install_symfile_debug_logging (struct objfile
*objfile
)
714 const struct sym_fns
*real_sf
;
715 struct debug_sym_fns_data
*debug_data
;
717 /* The debug versions should not already be installed. */
718 gdb_assert (!symfile_debug_installed (objfile
));
720 real_sf
= objfile
->sf
;
722 /* Alas we have to preserve NULL entries in REAL_SF. */
723 debug_data
= new struct debug_sym_fns_data
;
725 #define COPY_SF_PTR(from, to, name, func) \
728 (to)->debug_sf.name = func; \
731 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
732 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
733 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
734 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
735 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
736 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
737 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
738 debug_sym_read_linetable
);
739 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
740 if (real_sf
->sym_probe_fns
)
741 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
745 debug_data
->real_sf
= real_sf
;
746 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
747 objfile
->sf
= &debug_data
->debug_sf
;
750 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
751 Do not call this if the debug versions are not installed. */
754 uninstall_symfile_debug_logging (struct objfile
*objfile
)
756 struct debug_sym_fns_data
*debug_data
;
758 /* The debug versions should be currently installed. */
759 gdb_assert (symfile_debug_installed (objfile
));
761 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
763 objfile
->sf
= debug_data
->real_sf
;
764 symfile_debug_objfile_data_key
.clear (objfile
);
767 /* Call this function to set OBJFILE->SF.
768 Do not set OBJFILE->SF directly. */
771 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
773 if (symfile_debug_installed (objfile
))
775 gdb_assert (debug_symfile
);
776 /* Remove the current one, and reinstall a new one later. */
777 uninstall_symfile_debug_logging (objfile
);
780 /* Assume debug logging is disabled. */
783 /* Turn debug logging on if enabled. */
785 install_symfile_debug_logging (objfile
);
789 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
791 for (struct program_space
*pspace
: program_spaces
)
792 for (objfile
*objfile
: pspace
->objfiles ())
796 if (!symfile_debug_installed (objfile
))
797 install_symfile_debug_logging (objfile
);
801 if (symfile_debug_installed (objfile
))
802 uninstall_symfile_debug_logging (objfile
);
808 show_debug_symfile (struct ui_file
*file
, int from_tty
,
809 struct cmd_list_element
*c
, const char *value
)
811 gdb_printf (file
, _("Symfile debugging is %s.\n"), value
);
814 void _initialize_symfile_debug ();
816 _initialize_symfile_debug ()
818 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
819 Set debugging of the symfile functions."), _("\
820 Show debugging of the symfile functions."), _("\
821 When enabled, all calls to the symfile functions are logged."),
822 set_debug_symfile
, show_debug_symfile
,
823 &setdebuglist
, &showdebuglist
);
825 /* Note: We don't need a new-objfile observer because debug logging
826 will be installed when objfile init'n calls objfile_set_sym_fns. */