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 block_search_flags search_flags
,
275 enum search_domain kind
)
278 fprintf_filtered (gdb_stdlog
,
279 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
280 objfile_debug_name (this),
281 host_address_to_string (&file_matcher
),
282 host_address_to_string (&symbol_matcher
),
283 host_address_to_string (&expansion_notify
),
284 search_domain_name (kind
));
286 for (const auto &iter
: qf
)
287 if (!iter
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
288 symbol_matcher
, expansion_notify
,
294 struct compunit_symtab
*
295 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
297 struct obj_section
*section
,
300 struct compunit_symtab
*retval
= nullptr;
303 fprintf_filtered (gdb_stdlog
,
304 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
305 objfile_debug_name (this),
306 host_address_to_string (msymbol
.minsym
),
308 host_address_to_string (section
),
311 for (const auto &iter
: qf
)
313 retval
= iter
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
315 if (retval
!= nullptr)
320 fprintf_filtered (gdb_stdlog
,
321 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
323 ? debug_symtab_name (compunit_primary_filetab (retval
))
330 objfile::map_symbol_filenames (gdb::function_view
<symbol_filename_ftype
> fun
,
334 fprintf_filtered (gdb_stdlog
,
335 "qf->map_symbol_filenames (%s, ..., %d)\n",
336 objfile_debug_name (this),
339 for (const auto &iter
: qf
)
340 iter
->map_symbol_filenames (this, fun
, need_fullname
);
343 struct compunit_symtab
*
344 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
347 fprintf_filtered (gdb_stdlog
,
348 "qf->find_compunit_symtab_by_address (%s, %s)\n",
349 objfile_debug_name (this),
350 hex_string (address
));
352 struct compunit_symtab
*result
= NULL
;
353 for (const auto &iter
: qf
)
355 result
= iter
->find_compunit_symtab_by_address (this, address
);
356 if (result
!= nullptr)
361 fprintf_filtered (gdb_stdlog
,
362 "qf->find_compunit_symtab_by_address (...) = %s\n",
364 ? debug_symtab_name (compunit_primary_filetab (result
))
371 objfile::lookup_global_symbol_language (const char *name
,
373 bool *symbol_found_p
)
375 enum language result
= language_unknown
;
376 *symbol_found_p
= false;
378 for (const auto &iter
: qf
)
380 result
= iter
->lookup_global_symbol_language (this, name
, domain
,
390 objfile::require_partial_symbols (bool verbose
)
392 if ((flags
& OBJF_PSYMTABS_READ
) == 0)
394 flags
|= OBJF_PSYMTABS_READ
;
396 bool printed
= false;
397 for (const auto &iter
: qf
)
399 if (iter
->can_lazily_read_symbols ())
401 if (verbose
&& !printed
)
403 printf_filtered (_("Reading symbols from %s...\n"),
404 objfile_name (this));
407 iter
->read_partial_symbols (this);
410 if (printed
&& !objfile_has_symbols (this))
411 printf_filtered (_("(No debugging symbols found in %s)\n"),
412 objfile_name (this));
417 /* Debugging version of struct sym_probe_fns. */
419 static const std::vector
<std::unique_ptr
<probe
>> &
420 debug_sym_get_probes (struct objfile
*objfile
)
422 const struct debug_sym_fns_data
*debug_data
423 = symfile_debug_objfile_data_key
.get (objfile
);
425 const std::vector
<std::unique_ptr
<probe
>> &retval
426 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
428 fprintf_filtered (gdb_stdlog
,
429 "probes->sym_get_probes (%s) = %s\n",
430 objfile_debug_name (objfile
),
431 host_address_to_string (retval
.data ()));
436 static const struct sym_probe_fns debug_sym_probe_fns
=
438 debug_sym_get_probes
,
441 /* Debugging version of struct sym_fns. */
444 debug_sym_new_init (struct objfile
*objfile
)
446 const struct debug_sym_fns_data
*debug_data
447 = symfile_debug_objfile_data_key
.get (objfile
);
449 fprintf_filtered (gdb_stdlog
, "sf->sym_new_init (%s)\n",
450 objfile_debug_name (objfile
));
452 debug_data
->real_sf
->sym_new_init (objfile
);
456 debug_sym_init (struct objfile
*objfile
)
458 const struct debug_sym_fns_data
*debug_data
459 = symfile_debug_objfile_data_key
.get (objfile
);
461 fprintf_filtered (gdb_stdlog
, "sf->sym_init (%s)\n",
462 objfile_debug_name (objfile
));
464 debug_data
->real_sf
->sym_init (objfile
);
468 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
470 const struct debug_sym_fns_data
*debug_data
471 = symfile_debug_objfile_data_key
.get (objfile
);
473 fprintf_filtered (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
474 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
476 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
480 debug_sym_finish (struct objfile
*objfile
)
482 const struct debug_sym_fns_data
*debug_data
483 = symfile_debug_objfile_data_key
.get (objfile
);
485 fprintf_filtered (gdb_stdlog
, "sf->sym_finish (%s)\n",
486 objfile_debug_name (objfile
));
488 debug_data
->real_sf
->sym_finish (objfile
);
492 debug_sym_offsets (struct objfile
*objfile
,
493 const section_addr_info
&info
)
495 const struct debug_sym_fns_data
*debug_data
496 = symfile_debug_objfile_data_key
.get (objfile
);
498 fprintf_filtered (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
499 objfile_debug_name (objfile
),
500 host_address_to_string (&info
));
502 debug_data
->real_sf
->sym_offsets (objfile
, info
);
505 static symfile_segment_data_up
506 debug_sym_segments (bfd
*abfd
)
508 /* This API function is annoying, it doesn't take a "this" pointer.
509 Fortunately it is only used in one place where we (re-)lookup the
510 sym_fns table to use. Thus we will never be called. */
511 gdb_assert_not_reached ("debug_sym_segments called");
515 debug_sym_read_linetable (struct objfile
*objfile
)
517 const struct debug_sym_fns_data
*debug_data
518 = symfile_debug_objfile_data_key
.get (objfile
);
520 fprintf_filtered (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
521 objfile_debug_name (objfile
));
523 debug_data
->real_sf
->sym_read_linetable (objfile
);
527 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
529 const struct debug_sym_fns_data
*debug_data
530 = symfile_debug_objfile_data_key
.get (objfile
);
533 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
535 fprintf_filtered (gdb_stdlog
,
536 "sf->sym_relocate (%s, %s, %s) = %s\n",
537 objfile_debug_name (objfile
),
538 host_address_to_string (sectp
),
539 host_address_to_string (buf
),
540 host_address_to_string (retval
));
545 /* Template of debugging version of struct sym_fns.
546 A copy is made, with sym_flavour updated, and a pointer to the real table
547 installed in real_sf, and then a pointer to the copy is installed in the
550 static const struct sym_fns debug_sym_fns
=
558 debug_sym_read_linetable
,
560 &debug_sym_probe_fns
,
563 /* Install the debugging versions of the symfile functions for OBJFILE.
564 Do not call this if the debug versions are already installed. */
567 install_symfile_debug_logging (struct objfile
*objfile
)
569 const struct sym_fns
*real_sf
;
570 struct debug_sym_fns_data
*debug_data
;
572 /* The debug versions should not already be installed. */
573 gdb_assert (!symfile_debug_installed (objfile
));
575 real_sf
= objfile
->sf
;
577 /* Alas we have to preserve NULL entries in REAL_SF. */
578 debug_data
= new struct debug_sym_fns_data
;
580 #define COPY_SF_PTR(from, to, name, func) \
583 (to)->debug_sf.name = func; \
586 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
587 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
588 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
589 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
590 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
591 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
592 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
593 debug_sym_read_linetable
);
594 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
595 if (real_sf
->sym_probe_fns
)
596 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
600 debug_data
->real_sf
= real_sf
;
601 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
602 objfile
->sf
= &debug_data
->debug_sf
;
605 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
606 Do not call this if the debug versions are not installed. */
609 uninstall_symfile_debug_logging (struct objfile
*objfile
)
611 struct debug_sym_fns_data
*debug_data
;
613 /* The debug versions should be currently installed. */
614 gdb_assert (symfile_debug_installed (objfile
));
616 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
618 objfile
->sf
= debug_data
->real_sf
;
619 symfile_debug_objfile_data_key
.clear (objfile
);
622 /* Call this function to set OBJFILE->SF.
623 Do not set OBJFILE->SF directly. */
626 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
628 if (symfile_debug_installed (objfile
))
630 gdb_assert (debug_symfile
);
631 /* Remove the current one, and reinstall a new one later. */
632 uninstall_symfile_debug_logging (objfile
);
635 /* Assume debug logging is disabled. */
638 /* Turn debug logging on if enabled. */
640 install_symfile_debug_logging (objfile
);
644 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
646 for (struct program_space
*pspace
: program_spaces
)
647 for (objfile
*objfile
: pspace
->objfiles ())
651 if (!symfile_debug_installed (objfile
))
652 install_symfile_debug_logging (objfile
);
656 if (symfile_debug_installed (objfile
))
657 uninstall_symfile_debug_logging (objfile
);
663 show_debug_symfile (struct ui_file
*file
, int from_tty
,
664 struct cmd_list_element
*c
, const char *value
)
666 fprintf_filtered (file
, _("Symfile debugging is %s.\n"), value
);
669 void _initialize_symfile_debug ();
671 _initialize_symfile_debug ()
673 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
674 Set debugging of the symfile functions."), _("\
675 Show debugging of the symfile functions."), _("\
676 When enabled, all calls to the symfile functions are logged."),
677 set_debug_symfile
, show_debug_symfile
,
678 &setdebuglist
, &showdebuglist
);
680 /* Note: We don't need a new-objfile observer because debug logging
681 will be installed when objfile init'n calls objfile_set_sym_fns. */