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 /* 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
= nullptr;
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_key
<debug_sym_fns_data
>
49 symfile_debug_objfile_data_key
;
51 /* If true all calls to the symfile functions are logged. */
52 static bool debug_symfile
= false;
54 /* Return non-zero if symfile debug logging is installed. */
57 symfile_debug_installed (struct objfile
*objfile
)
59 return (objfile
->sf
!= NULL
60 && symfile_debug_objfile_data_key
.get (objfile
) != NULL
);
63 /* Utility return the name to print for SYMTAB. */
66 debug_symtab_name (struct symtab
*symtab
)
68 return symtab_to_filename_for_display (symtab
);
75 objfile::has_partial_symbols ()
79 /* If we have not read psymbols, but we have a function capable of reading
80 them, then that is an indication that they are in fact available. Without
81 this function the symbols may have been already read in but they also may
82 not be present in this objfile. */
83 for (const auto &iter
: qf
)
85 if ((flags
& OBJF_PSYMTABS_READ
) == 0
86 && iter
->can_lazily_read_symbols ())
89 retval
= iter
->has_symbols (this);
95 fprintf_filtered (gdb_stdlog
, "qf->has_symbols (%s) = %d\n",
96 objfile_debug_name (this), retval
);
102 objfile::find_last_source_symtab ()
104 struct symtab
*retval
= nullptr;
107 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (%s)\n",
108 objfile_debug_name (this));
110 for (const auto &iter
: qf
)
112 retval
= iter
->find_last_source_symtab (this);
113 if (retval
!= nullptr)
118 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (...) = %s\n",
119 retval
? debug_symtab_name (retval
) : "NULL");
125 objfile::forget_cached_source_info ()
128 fprintf_filtered (gdb_stdlog
, "qf->forget_cached_source_info (%s)\n",
129 objfile_debug_name (this));
131 for (const auto &iter
: qf
)
132 iter
->forget_cached_source_info (this);
136 objfile::map_symtabs_matching_filename
137 (const char *name
, const char *real_path
,
138 gdb::function_view
<bool (symtab
*)> callback
)
141 fprintf_filtered (gdb_stdlog
,
142 "qf->map_symtabs_matching_filename (%s, \"%s\", "
144 objfile_debug_name (this), name
,
145 real_path
? real_path
: NULL
,
146 host_address_to_string (&callback
));
149 for (const auto &iter
: qf
)
151 retval
= (iter
->map_symtabs_matching_filename
152 (this, name
, real_path
, callback
));
158 fprintf_filtered (gdb_stdlog
,
159 "qf->map_symtabs_matching_filename (...) = %d\n",
165 struct compunit_symtab
*
166 objfile::lookup_symbol (block_enum kind
, const char *name
, domain_enum domain
)
168 struct compunit_symtab
*retval
= nullptr;
171 fprintf_filtered (gdb_stdlog
,
172 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
173 objfile_debug_name (this), kind
, name
,
174 domain_name (domain
));
176 for (const auto &iter
: qf
)
178 retval
= iter
->lookup_symbol (this, kind
, name
, domain
);
179 if (retval
!= nullptr)
184 fprintf_filtered (gdb_stdlog
, "qf->lookup_symbol (...) = %s\n",
186 ? debug_symtab_name (compunit_primary_filetab (retval
))
193 objfile::print_stats (bool print_bcache
)
196 fprintf_filtered (gdb_stdlog
, "qf->print_stats (%s, %d)\n",
197 objfile_debug_name (this), print_bcache
);
199 for (const auto &iter
: qf
)
200 iter
->print_stats (this, print_bcache
);
207 fprintf_filtered (gdb_stdlog
, "qf->dump (%s)\n",
208 objfile_debug_name (this));
210 for (const auto &iter
: qf
)
215 objfile::expand_symtabs_for_function (const char *func_name
)
218 fprintf_filtered (gdb_stdlog
,
219 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
220 objfile_debug_name (this), func_name
);
222 for (const auto &iter
: qf
)
223 iter
->expand_symtabs_for_function (this, func_name
);
227 objfile::expand_all_symtabs ()
230 fprintf_filtered (gdb_stdlog
, "qf->expand_all_symtabs (%s)\n",
231 objfile_debug_name (this));
233 for (const auto &iter
: qf
)
234 iter
->expand_all_symtabs (this);
238 objfile::expand_symtabs_with_fullname (const char *fullname
)
241 fprintf_filtered (gdb_stdlog
,
242 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
243 objfile_debug_name (this), fullname
);
245 for (const auto &iter
: qf
)
246 iter
->expand_symtabs_with_fullname (this, fullname
);
250 objfile::map_matching_symbols
251 (const lookup_name_info
&name
, domain_enum domain
,
253 gdb::function_view
<symbol_found_callback_ftype
> callback
,
254 symbol_compare_ftype
*ordered_compare
)
257 fprintf_filtered (gdb_stdlog
,
258 "qf->map_matching_symbols (%s, %s, %d, %s)\n",
259 objfile_debug_name (this),
260 domain_name (domain
), global
,
261 host_address_to_string (ordered_compare
));
263 for (const auto &iter
: qf
)
264 iter
->map_matching_symbols (this, name
, domain
, global
,
265 callback
, ordered_compare
);
269 objfile::expand_symtabs_matching
270 (gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
271 const lookup_name_info
*lookup_name
,
272 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
273 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
274 enum search_domain kind
)
277 fprintf_filtered (gdb_stdlog
,
278 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
279 objfile_debug_name (this),
280 host_address_to_string (&file_matcher
),
281 host_address_to_string (&symbol_matcher
),
282 host_address_to_string (&expansion_notify
),
283 search_domain_name (kind
));
285 for (const auto &iter
: qf
)
286 iter
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
287 symbol_matcher
, expansion_notify
, kind
);
290 struct compunit_symtab
*
291 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
293 struct obj_section
*section
,
296 struct compunit_symtab
*retval
= nullptr;
299 fprintf_filtered (gdb_stdlog
,
300 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
301 objfile_debug_name (this),
302 host_address_to_string (msymbol
.minsym
),
304 host_address_to_string (section
),
307 for (const auto &iter
: qf
)
309 retval
= iter
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
311 if (retval
!= nullptr)
316 fprintf_filtered (gdb_stdlog
,
317 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
319 ? debug_symtab_name (compunit_primary_filetab (retval
))
326 objfile::map_symbol_filenames (gdb::function_view
<symbol_filename_ftype
> fun
,
330 fprintf_filtered (gdb_stdlog
,
331 "qf->map_symbol_filenames (%s, ..., %d)\n",
332 objfile_debug_name (this),
335 for (const auto &iter
: qf
)
336 iter
->map_symbol_filenames (this, fun
, need_fullname
);
339 struct compunit_symtab
*
340 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
343 fprintf_filtered (gdb_stdlog
,
344 "qf->find_compunit_symtab_by_address (%s, %s)\n",
345 objfile_debug_name (this),
346 hex_string (address
));
348 struct compunit_symtab
*result
= NULL
;
349 for (const auto &iter
: qf
)
351 result
= iter
->find_compunit_symtab_by_address (this, address
);
352 if (result
!= nullptr)
357 fprintf_filtered (gdb_stdlog
,
358 "qf->find_compunit_symtab_by_address (...) = %s\n",
360 ? debug_symtab_name (compunit_primary_filetab (result
))
367 objfile::lookup_global_symbol_language (const char *name
,
369 bool *symbol_found_p
)
371 enum language result
= language_unknown
;
372 *symbol_found_p
= false;
374 for (const auto &iter
: qf
)
376 result
= iter
->lookup_global_symbol_language (this, name
, domain
,
386 objfile::require_partial_symbols (bool verbose
)
388 if ((flags
& OBJF_PSYMTABS_READ
) == 0)
390 flags
|= OBJF_PSYMTABS_READ
;
392 bool printed
= false;
393 for (const auto &iter
: qf
)
395 if (iter
->can_lazily_read_symbols ())
397 if (verbose
&& !printed
)
399 printf_filtered (_("Reading symbols from %s...\n"),
400 objfile_name (this));
403 iter
->read_partial_symbols (this);
406 if (printed
&& !objfile_has_symbols (this))
407 printf_filtered (_("(No debugging symbols found in %s)\n"),
408 objfile_name (this));
413 /* Debugging version of struct sym_probe_fns. */
415 static const std::vector
<std::unique_ptr
<probe
>> &
416 debug_sym_get_probes (struct objfile
*objfile
)
418 const struct debug_sym_fns_data
*debug_data
419 = symfile_debug_objfile_data_key
.get (objfile
);
421 const std::vector
<std::unique_ptr
<probe
>> &retval
422 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
424 fprintf_filtered (gdb_stdlog
,
425 "probes->sym_get_probes (%s) = %s\n",
426 objfile_debug_name (objfile
),
427 host_address_to_string (retval
.data ()));
432 static const struct sym_probe_fns debug_sym_probe_fns
=
434 debug_sym_get_probes
,
437 /* Debugging version of struct sym_fns. */
440 debug_sym_new_init (struct objfile
*objfile
)
442 const struct debug_sym_fns_data
*debug_data
443 = symfile_debug_objfile_data_key
.get (objfile
);
445 fprintf_filtered (gdb_stdlog
, "sf->sym_new_init (%s)\n",
446 objfile_debug_name (objfile
));
448 debug_data
->real_sf
->sym_new_init (objfile
);
452 debug_sym_init (struct objfile
*objfile
)
454 const struct debug_sym_fns_data
*debug_data
455 = symfile_debug_objfile_data_key
.get (objfile
);
457 fprintf_filtered (gdb_stdlog
, "sf->sym_init (%s)\n",
458 objfile_debug_name (objfile
));
460 debug_data
->real_sf
->sym_init (objfile
);
464 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
466 const struct debug_sym_fns_data
*debug_data
467 = symfile_debug_objfile_data_key
.get (objfile
);
469 fprintf_filtered (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
470 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
472 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
476 debug_sym_finish (struct objfile
*objfile
)
478 const struct debug_sym_fns_data
*debug_data
479 = symfile_debug_objfile_data_key
.get (objfile
);
481 fprintf_filtered (gdb_stdlog
, "sf->sym_finish (%s)\n",
482 objfile_debug_name (objfile
));
484 debug_data
->real_sf
->sym_finish (objfile
);
488 debug_sym_offsets (struct objfile
*objfile
,
489 const section_addr_info
&info
)
491 const struct debug_sym_fns_data
*debug_data
492 = symfile_debug_objfile_data_key
.get (objfile
);
494 fprintf_filtered (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
495 objfile_debug_name (objfile
),
496 host_address_to_string (&info
));
498 debug_data
->real_sf
->sym_offsets (objfile
, info
);
501 static symfile_segment_data_up
502 debug_sym_segments (bfd
*abfd
)
504 /* This API function is annoying, it doesn't take a "this" pointer.
505 Fortunately it is only used in one place where we (re-)lookup the
506 sym_fns table to use. Thus we will never be called. */
507 gdb_assert_not_reached ("debug_sym_segments called");
511 debug_sym_read_linetable (struct objfile
*objfile
)
513 const struct debug_sym_fns_data
*debug_data
514 = symfile_debug_objfile_data_key
.get (objfile
);
516 fprintf_filtered (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
517 objfile_debug_name (objfile
));
519 debug_data
->real_sf
->sym_read_linetable (objfile
);
523 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
525 const struct debug_sym_fns_data
*debug_data
526 = symfile_debug_objfile_data_key
.get (objfile
);
529 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
531 fprintf_filtered (gdb_stdlog
,
532 "sf->sym_relocate (%s, %s, %s) = %s\n",
533 objfile_debug_name (objfile
),
534 host_address_to_string (sectp
),
535 host_address_to_string (buf
),
536 host_address_to_string (retval
));
541 /* Template of debugging version of struct sym_fns.
542 A copy is made, with sym_flavour updated, and a pointer to the real table
543 installed in real_sf, and then a pointer to the copy is installed in the
546 static const struct sym_fns debug_sym_fns
=
554 debug_sym_read_linetable
,
556 &debug_sym_probe_fns
,
559 /* Install the debugging versions of the symfile functions for OBJFILE.
560 Do not call this if the debug versions are already installed. */
563 install_symfile_debug_logging (struct objfile
*objfile
)
565 const struct sym_fns
*real_sf
;
566 struct debug_sym_fns_data
*debug_data
;
568 /* The debug versions should not already be installed. */
569 gdb_assert (!symfile_debug_installed (objfile
));
571 real_sf
= objfile
->sf
;
573 /* Alas we have to preserve NULL entries in REAL_SF. */
574 debug_data
= new struct debug_sym_fns_data
;
576 #define COPY_SF_PTR(from, to, name, func) \
579 (to)->debug_sf.name = func; \
582 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
583 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
584 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
585 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
586 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
587 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
588 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
589 debug_sym_read_linetable
);
590 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
591 if (real_sf
->sym_probe_fns
)
592 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
596 debug_data
->real_sf
= real_sf
;
597 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
598 objfile
->sf
= &debug_data
->debug_sf
;
601 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
602 Do not call this if the debug versions are not installed. */
605 uninstall_symfile_debug_logging (struct objfile
*objfile
)
607 struct debug_sym_fns_data
*debug_data
;
609 /* The debug versions should be currently installed. */
610 gdb_assert (symfile_debug_installed (objfile
));
612 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
614 objfile
->sf
= debug_data
->real_sf
;
615 symfile_debug_objfile_data_key
.clear (objfile
);
618 /* Call this function to set OBJFILE->SF.
619 Do not set OBJFILE->SF directly. */
622 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
624 if (symfile_debug_installed (objfile
))
626 gdb_assert (debug_symfile
);
627 /* Remove the current one, and reinstall a new one later. */
628 uninstall_symfile_debug_logging (objfile
);
631 /* Assume debug logging is disabled. */
634 /* Turn debug logging on if enabled. */
636 install_symfile_debug_logging (objfile
);
640 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
642 for (struct program_space
*pspace
: program_spaces
)
643 for (objfile
*objfile
: pspace
->objfiles ())
647 if (!symfile_debug_installed (objfile
))
648 install_symfile_debug_logging (objfile
);
652 if (symfile_debug_installed (objfile
))
653 uninstall_symfile_debug_logging (objfile
);
659 show_debug_symfile (struct ui_file
*file
, int from_tty
,
660 struct cmd_list_element
*c
, const char *value
)
662 fprintf_filtered (file
, _("Symfile debugging is %s.\n"), value
);
665 void _initialize_symfile_debug ();
667 _initialize_symfile_debug ()
669 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
670 Set debugging of the symfile functions."), _("\
671 Show debugging of the symfile functions."), _("\
672 When enabled, all calls to the symfile functions are logged."),
673 set_debug_symfile
, show_debug_symfile
,
674 &setdebuglist
, &showdebuglist
);
676 /* Note: We don't need a new-objfile observer because debug logging
677 will be installed when objfile init'n calls objfile_set_sym_fns. */