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
);
103 /* See objfiles.h. */
105 objfile::has_unexpanded_symtabs ()
108 fprintf_filtered (gdb_stdlog
, "qf->has_unexpanded_symtabs (%s)\n",
109 objfile_debug_name (this));
112 for (const auto &iter
: qf
)
114 if (iter
->has_unexpanded_symtabs (this))
122 fprintf_filtered (gdb_stdlog
, "qf->has_unexpanded_symtabs (%s) = %d\n",
123 objfile_debug_name (this), (result
? 1 : 0));
129 objfile::find_last_source_symtab ()
131 struct symtab
*retval
= nullptr;
134 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (%s)\n",
135 objfile_debug_name (this));
137 for (const auto &iter
: qf
)
139 retval
= iter
->find_last_source_symtab (this);
140 if (retval
!= nullptr)
145 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (...) = %s\n",
146 retval
? debug_symtab_name (retval
) : "NULL");
152 objfile::forget_cached_source_info ()
155 fprintf_filtered (gdb_stdlog
, "qf->forget_cached_source_info (%s)\n",
156 objfile_debug_name (this));
158 for (const auto &iter
: qf
)
159 iter
->forget_cached_source_info (this);
163 objfile::map_symtabs_matching_filename
164 (const char *name
, const char *real_path
,
165 gdb::function_view
<bool (symtab
*)> callback
)
168 fprintf_filtered (gdb_stdlog
,
169 "qf->map_symtabs_matching_filename (%s, \"%s\", "
171 objfile_debug_name (this), name
,
172 real_path
? real_path
: NULL
,
173 host_address_to_string (&callback
));
176 const char *name_basename
= lbasename (name
);
178 auto match_one_filename
= [&] (const char *filename
, bool basenames
)
180 if (compare_filenames_for_search (filename
, name
))
182 if (basenames
&& FILENAME_CMP (name_basename
, filename
) == 0)
184 if (real_path
!= nullptr && IS_ABSOLUTE_PATH (filename
)
185 && IS_ABSOLUTE_PATH (real_path
))
186 return filename_cmp (filename
, real_path
) == 0;
190 compunit_symtab
*last_made
= this->compunit_symtabs
;
192 auto on_expansion
= [&] (compunit_symtab
*symtab
)
194 /* The callback to iterate_over_some_symtabs returns false to keep
195 going and true to continue, so we have to invert the result
196 here, for expand_symtabs_matching. */
197 bool result
= !iterate_over_some_symtabs (name
, real_path
,
198 this->compunit_symtabs
,
201 last_made
= this->compunit_symtabs
;
205 for (const auto &iter
: qf
)
207 if (!iter
->expand_symtabs_matching (this,
213 | SEARCH_STATIC_BLOCK
),
223 fprintf_filtered (gdb_stdlog
,
224 "qf->map_symtabs_matching_filename (...) = %d\n",
227 /* We must re-invert the return value here to match the caller's
232 struct compunit_symtab
*
233 objfile::lookup_symbol (block_enum kind
, const char *name
, domain_enum domain
)
235 struct compunit_symtab
*retval
= nullptr;
238 fprintf_filtered (gdb_stdlog
,
239 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
240 objfile_debug_name (this), kind
, name
,
241 domain_name (domain
));
243 lookup_name_info
lookup_name (name
, symbol_name_match_type::FULL
);
245 auto search_one_symtab
= [&] (compunit_symtab
*stab
)
247 struct symbol
*sym
, *with_opaque
= NULL
;
248 const struct blockvector
*bv
= COMPUNIT_BLOCKVECTOR (stab
);
249 const struct block
*block
= BLOCKVECTOR_BLOCK (bv
, kind
);
251 sym
= block_find_symbol (block
, name
, domain
,
252 block_find_non_opaque_type_preferred
,
255 /* Some caution must be observed with overloaded functions
256 and methods, since the index will not contain any overload
257 information (but NAME might contain it). */
260 && SYMBOL_MATCHES_SEARCH_NAME (sym
, lookup_name
))
266 if (with_opaque
!= NULL
267 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque
, lookup_name
))
270 /* Keep looking through other psymtabs. */
274 for (const auto &iter
: qf
)
276 if (!iter
->expand_symtabs_matching (this,
282 ? SEARCH_GLOBAL_BLOCK
283 : SEARCH_STATIC_BLOCK
,
290 fprintf_filtered (gdb_stdlog
, "qf->lookup_symbol (...) = %s\n",
292 ? debug_symtab_name (compunit_primary_filetab (retval
))
299 objfile::print_stats (bool print_bcache
)
302 fprintf_filtered (gdb_stdlog
, "qf->print_stats (%s, %d)\n",
303 objfile_debug_name (this), print_bcache
);
305 for (const auto &iter
: qf
)
306 iter
->print_stats (this, print_bcache
);
313 fprintf_filtered (gdb_stdlog
, "qf->dump (%s)\n",
314 objfile_debug_name (this));
316 for (const auto &iter
: qf
)
321 objfile::expand_symtabs_for_function (const char *func_name
)
324 fprintf_filtered (gdb_stdlog
,
325 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
326 objfile_debug_name (this), func_name
);
328 lookup_name_info
base_lookup (func_name
, symbol_name_match_type::FULL
);
329 lookup_name_info lookup_name
= base_lookup
.make_ignore_params ();
331 for (const auto &iter
: qf
)
332 iter
->expand_symtabs_matching (this,
338 | SEARCH_STATIC_BLOCK
),
344 objfile::expand_all_symtabs ()
347 fprintf_filtered (gdb_stdlog
, "qf->expand_all_symtabs (%s)\n",
348 objfile_debug_name (this));
350 for (const auto &iter
: qf
)
351 iter
->expand_all_symtabs (this);
355 objfile::expand_symtabs_with_fullname (const char *fullname
)
358 fprintf_filtered (gdb_stdlog
,
359 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
360 objfile_debug_name (this), fullname
);
362 const char *basename
= lbasename (fullname
);
363 auto file_matcher
= [&] (const char *filename
, bool basenames
)
365 return filename_cmp (basenames
? basename
: fullname
, filename
) == 0;
368 for (const auto &iter
: qf
)
369 iter
->expand_symtabs_matching (this,
375 | SEARCH_STATIC_BLOCK
),
381 objfile::expand_matching_symbols
382 (const lookup_name_info
&name
, domain_enum domain
,
384 symbol_compare_ftype
*ordered_compare
)
387 fprintf_filtered (gdb_stdlog
,
388 "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
389 objfile_debug_name (this),
390 domain_name (domain
), global
,
391 host_address_to_string (ordered_compare
));
393 for (const auto &iter
: qf
)
394 iter
->expand_matching_symbols (this, name
, domain
, global
,
399 objfile::expand_symtabs_matching
400 (gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
401 const lookup_name_info
*lookup_name
,
402 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
403 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
404 block_search_flags search_flags
,
406 enum search_domain kind
)
409 fprintf_filtered (gdb_stdlog
,
410 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
411 objfile_debug_name (this),
412 host_address_to_string (&file_matcher
),
413 host_address_to_string (&symbol_matcher
),
414 host_address_to_string (&expansion_notify
),
415 search_domain_name (kind
));
417 for (const auto &iter
: qf
)
418 if (!iter
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
419 symbol_matcher
, expansion_notify
,
420 search_flags
, domain
, kind
))
425 struct compunit_symtab
*
426 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
428 struct obj_section
*section
,
431 struct compunit_symtab
*retval
= nullptr;
434 fprintf_filtered (gdb_stdlog
,
435 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
436 objfile_debug_name (this),
437 host_address_to_string (msymbol
.minsym
),
439 host_address_to_string (section
),
442 for (const auto &iter
: qf
)
444 retval
= iter
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
446 if (retval
!= nullptr)
451 fprintf_filtered (gdb_stdlog
,
452 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
454 ? debug_symtab_name (compunit_primary_filetab (retval
))
461 objfile::map_symbol_filenames (gdb::function_view
<symbol_filename_ftype
> fun
,
465 fprintf_filtered (gdb_stdlog
,
466 "qf->map_symbol_filenames (%s, ..., %d)\n",
467 objfile_debug_name (this),
470 for (const auto &iter
: qf
)
471 iter
->map_symbol_filenames (this, fun
, need_fullname
);
474 struct compunit_symtab
*
475 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
478 fprintf_filtered (gdb_stdlog
,
479 "qf->find_compunit_symtab_by_address (%s, %s)\n",
480 objfile_debug_name (this),
481 hex_string (address
));
483 struct compunit_symtab
*result
= NULL
;
484 for (const auto &iter
: qf
)
486 result
= iter
->find_compunit_symtab_by_address (this, address
);
487 if (result
!= nullptr)
492 fprintf_filtered (gdb_stdlog
,
493 "qf->find_compunit_symtab_by_address (...) = %s\n",
495 ? debug_symtab_name (compunit_primary_filetab (result
))
502 objfile::lookup_global_symbol_language (const char *name
,
504 bool *symbol_found_p
)
506 enum language result
= language_unknown
;
507 *symbol_found_p
= false;
509 for (const auto &iter
: qf
)
511 result
= iter
->lookup_global_symbol_language (this, name
, domain
,
521 objfile::require_partial_symbols (bool verbose
)
523 if ((flags
& OBJF_PSYMTABS_READ
) == 0)
525 flags
|= OBJF_PSYMTABS_READ
;
527 bool printed
= false;
528 for (const auto &iter
: qf
)
530 if (iter
->can_lazily_read_symbols ())
532 if (verbose
&& !printed
)
534 printf_filtered (_("Reading symbols from %s...\n"),
535 objfile_name (this));
538 iter
->read_partial_symbols (this);
541 if (printed
&& !objfile_has_symbols (this))
542 printf_filtered (_("(No debugging symbols found in %s)\n"),
543 objfile_name (this));
548 /* Debugging version of struct sym_probe_fns. */
550 static const std::vector
<std::unique_ptr
<probe
>> &
551 debug_sym_get_probes (struct objfile
*objfile
)
553 const struct debug_sym_fns_data
*debug_data
554 = symfile_debug_objfile_data_key
.get (objfile
);
556 const std::vector
<std::unique_ptr
<probe
>> &retval
557 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
559 fprintf_filtered (gdb_stdlog
,
560 "probes->sym_get_probes (%s) = %s\n",
561 objfile_debug_name (objfile
),
562 host_address_to_string (retval
.data ()));
567 static const struct sym_probe_fns debug_sym_probe_fns
=
569 debug_sym_get_probes
,
572 /* Debugging version of struct sym_fns. */
575 debug_sym_new_init (struct objfile
*objfile
)
577 const struct debug_sym_fns_data
*debug_data
578 = symfile_debug_objfile_data_key
.get (objfile
);
580 fprintf_filtered (gdb_stdlog
, "sf->sym_new_init (%s)\n",
581 objfile_debug_name (objfile
));
583 debug_data
->real_sf
->sym_new_init (objfile
);
587 debug_sym_init (struct objfile
*objfile
)
589 const struct debug_sym_fns_data
*debug_data
590 = symfile_debug_objfile_data_key
.get (objfile
);
592 fprintf_filtered (gdb_stdlog
, "sf->sym_init (%s)\n",
593 objfile_debug_name (objfile
));
595 debug_data
->real_sf
->sym_init (objfile
);
599 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
601 const struct debug_sym_fns_data
*debug_data
602 = symfile_debug_objfile_data_key
.get (objfile
);
604 fprintf_filtered (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
605 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
607 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
611 debug_sym_finish (struct objfile
*objfile
)
613 const struct debug_sym_fns_data
*debug_data
614 = symfile_debug_objfile_data_key
.get (objfile
);
616 fprintf_filtered (gdb_stdlog
, "sf->sym_finish (%s)\n",
617 objfile_debug_name (objfile
));
619 debug_data
->real_sf
->sym_finish (objfile
);
623 debug_sym_offsets (struct objfile
*objfile
,
624 const section_addr_info
&info
)
626 const struct debug_sym_fns_data
*debug_data
627 = symfile_debug_objfile_data_key
.get (objfile
);
629 fprintf_filtered (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
630 objfile_debug_name (objfile
),
631 host_address_to_string (&info
));
633 debug_data
->real_sf
->sym_offsets (objfile
, info
);
636 static symfile_segment_data_up
637 debug_sym_segments (bfd
*abfd
)
639 /* This API function is annoying, it doesn't take a "this" pointer.
640 Fortunately it is only used in one place where we (re-)lookup the
641 sym_fns table to use. Thus we will never be called. */
642 gdb_assert_not_reached ("debug_sym_segments called");
646 debug_sym_read_linetable (struct objfile
*objfile
)
648 const struct debug_sym_fns_data
*debug_data
649 = symfile_debug_objfile_data_key
.get (objfile
);
651 fprintf_filtered (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
652 objfile_debug_name (objfile
));
654 debug_data
->real_sf
->sym_read_linetable (objfile
);
658 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
660 const struct debug_sym_fns_data
*debug_data
661 = symfile_debug_objfile_data_key
.get (objfile
);
664 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
666 fprintf_filtered (gdb_stdlog
,
667 "sf->sym_relocate (%s, %s, %s) = %s\n",
668 objfile_debug_name (objfile
),
669 host_address_to_string (sectp
),
670 host_address_to_string (buf
),
671 host_address_to_string (retval
));
676 /* Template of debugging version of struct sym_fns.
677 A copy is made, with sym_flavour updated, and a pointer to the real table
678 installed in real_sf, and then a pointer to the copy is installed in the
681 static const struct sym_fns debug_sym_fns
=
689 debug_sym_read_linetable
,
691 &debug_sym_probe_fns
,
694 /* Install the debugging versions of the symfile functions for OBJFILE.
695 Do not call this if the debug versions are already installed. */
698 install_symfile_debug_logging (struct objfile
*objfile
)
700 const struct sym_fns
*real_sf
;
701 struct debug_sym_fns_data
*debug_data
;
703 /* The debug versions should not already be installed. */
704 gdb_assert (!symfile_debug_installed (objfile
));
706 real_sf
= objfile
->sf
;
708 /* Alas we have to preserve NULL entries in REAL_SF. */
709 debug_data
= new struct debug_sym_fns_data
;
711 #define COPY_SF_PTR(from, to, name, func) \
714 (to)->debug_sf.name = func; \
717 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
718 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
719 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
720 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
721 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
722 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
723 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
724 debug_sym_read_linetable
);
725 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
726 if (real_sf
->sym_probe_fns
)
727 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
731 debug_data
->real_sf
= real_sf
;
732 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
733 objfile
->sf
= &debug_data
->debug_sf
;
736 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
737 Do not call this if the debug versions are not installed. */
740 uninstall_symfile_debug_logging (struct objfile
*objfile
)
742 struct debug_sym_fns_data
*debug_data
;
744 /* The debug versions should be currently installed. */
745 gdb_assert (symfile_debug_installed (objfile
));
747 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
749 objfile
->sf
= debug_data
->real_sf
;
750 symfile_debug_objfile_data_key
.clear (objfile
);
753 /* Call this function to set OBJFILE->SF.
754 Do not set OBJFILE->SF directly. */
757 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
759 if (symfile_debug_installed (objfile
))
761 gdb_assert (debug_symfile
);
762 /* Remove the current one, and reinstall a new one later. */
763 uninstall_symfile_debug_logging (objfile
);
766 /* Assume debug logging is disabled. */
769 /* Turn debug logging on if enabled. */
771 install_symfile_debug_logging (objfile
);
775 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
777 for (struct program_space
*pspace
: program_spaces
)
778 for (objfile
*objfile
: pspace
->objfiles ())
782 if (!symfile_debug_installed (objfile
))
783 install_symfile_debug_logging (objfile
);
787 if (symfile_debug_installed (objfile
))
788 uninstall_symfile_debug_logging (objfile
);
794 show_debug_symfile (struct ui_file
*file
, int from_tty
,
795 struct cmd_list_element
*c
, const char *value
)
797 fprintf_filtered (file
, _("Symfile debugging is %s.\n"), value
);
800 void _initialize_symfile_debug ();
802 _initialize_symfile_debug ()
804 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
805 Set debugging of the symfile functions."), _("\
806 Show debugging of the symfile functions."), _("\
807 When enabled, all calls to the symfile functions are logged."),
808 set_debug_symfile
, show_debug_symfile
,
809 &setdebuglist
, &showdebuglist
);
811 /* Note: We don't need a new-objfile observer because debug logging
812 will be installed when objfile init'n calls objfile_set_sym_fns. */