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 if ((flags
& OBJF_PSYMTABS_READ
) == 0
85 && qf
->can_lazily_read_symbols ())
87 else if (qf
!= nullptr)
88 retval
= qf
->has_symbols (this);
91 fprintf_filtered (gdb_stdlog
, "qf->has_symbols (%s) = %d\n",
92 objfile_debug_name (this), retval
);
98 objfile::find_last_source_symtab ()
100 struct symtab
*retval
= nullptr;
103 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (%s)\n",
104 objfile_debug_name (this));
107 retval
= qf
->find_last_source_symtab (this);
110 fprintf_filtered (gdb_stdlog
, "qf->find_last_source_symtab (...) = %s\n",
111 retval
? debug_symtab_name (retval
) : "NULL");
117 objfile::forget_cached_source_info ()
120 fprintf_filtered (gdb_stdlog
, "qf->forget_cached_source_info (%s)\n",
121 objfile_debug_name (this));
124 qf
->forget_cached_source_info (this);
128 objfile::map_symtabs_matching_filename
129 (const char *name
, const char *real_path
,
130 gdb::function_view
<bool (symtab
*)> callback
)
133 fprintf_filtered (gdb_stdlog
,
134 "qf->map_symtabs_matching_filename (%s, \"%s\", "
136 objfile_debug_name (this), name
,
137 real_path
? real_path
: NULL
,
138 host_address_to_string (&callback
));
142 retval
= (qf
->map_symtabs_matching_filename
143 (this, name
, real_path
, callback
));
146 fprintf_filtered (gdb_stdlog
,
147 "qf->map_symtabs_matching_filename (...) = %d\n",
153 struct compunit_symtab
*
154 objfile::lookup_symbol (block_enum kind
, const char *name
, domain_enum domain
)
156 struct compunit_symtab
*retval
= nullptr;
159 fprintf_filtered (gdb_stdlog
,
160 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
161 objfile_debug_name (this), kind
, name
,
162 domain_name (domain
));
165 retval
= qf
->lookup_symbol (this, kind
, name
, domain
);
168 fprintf_filtered (gdb_stdlog
, "qf->lookup_symbol (...) = %s\n",
170 ? debug_symtab_name (compunit_primary_filetab (retval
))
177 objfile::print_stats (bool print_bcache
)
180 fprintf_filtered (gdb_stdlog
, "qf->print_stats (%s, %d)\n",
181 objfile_debug_name (this), print_bcache
);
184 qf
->print_stats (this, print_bcache
);
191 fprintf_filtered (gdb_stdlog
, "qf->dump (%s)\n",
192 objfile_debug_name (this));
199 objfile::expand_symtabs_for_function (const char *func_name
)
202 fprintf_filtered (gdb_stdlog
,
203 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
204 objfile_debug_name (this), func_name
);
207 qf
->expand_symtabs_for_function (this, func_name
);
211 objfile::expand_all_symtabs ()
214 fprintf_filtered (gdb_stdlog
, "qf->expand_all_symtabs (%s)\n",
215 objfile_debug_name (this));
218 qf
->expand_all_symtabs (this);
222 objfile::expand_symtabs_with_fullname (const char *fullname
)
225 fprintf_filtered (gdb_stdlog
,
226 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
227 objfile_debug_name (this), fullname
);
230 qf
->expand_symtabs_with_fullname (this, fullname
);
234 objfile::map_matching_symbols
235 (const lookup_name_info
&name
, domain_enum domain
,
237 gdb::function_view
<symbol_found_callback_ftype
> callback
,
238 symbol_compare_ftype
*ordered_compare
)
241 fprintf_filtered (gdb_stdlog
,
242 "qf->map_matching_symbols (%s, %s, %d, %s)\n",
243 objfile_debug_name (this),
244 domain_name (domain
), global
,
245 host_address_to_string (ordered_compare
));
248 qf
->map_matching_symbols (this, name
, domain
, global
,
249 callback
, ordered_compare
);
253 objfile::expand_symtabs_matching
254 (gdb::function_view
<expand_symtabs_file_matcher_ftype
> file_matcher
,
255 const lookup_name_info
*lookup_name
,
256 gdb::function_view
<expand_symtabs_symbol_matcher_ftype
> symbol_matcher
,
257 gdb::function_view
<expand_symtabs_exp_notify_ftype
> expansion_notify
,
258 enum search_domain kind
)
261 fprintf_filtered (gdb_stdlog
,
262 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
263 objfile_debug_name (this),
264 host_address_to_string (&file_matcher
),
265 host_address_to_string (&symbol_matcher
),
266 host_address_to_string (&expansion_notify
),
267 search_domain_name (kind
));
270 qf
->expand_symtabs_matching (this, file_matcher
, lookup_name
,
271 symbol_matcher
, expansion_notify
, kind
);
274 struct compunit_symtab
*
275 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol
,
277 struct obj_section
*section
,
280 struct compunit_symtab
*retval
= nullptr;
283 fprintf_filtered (gdb_stdlog
,
284 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
285 objfile_debug_name (this),
286 host_address_to_string (msymbol
.minsym
),
288 host_address_to_string (section
),
292 retval
= qf
->find_pc_sect_compunit_symtab (this, msymbol
, pc
, section
,
296 fprintf_filtered (gdb_stdlog
,
297 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
299 ? debug_symtab_name (compunit_primary_filetab (retval
))
306 objfile::map_symbol_filenames (symbol_filename_ftype
*fun
, void *data
,
310 fprintf_filtered (gdb_stdlog
,
311 "qf->map_symbol_filenames (%s, %s, %s, %d)\n",
312 objfile_debug_name (this),
313 host_address_to_string (fun
),
314 host_address_to_string (data
),
318 qf
->map_symbol_filenames (this, fun
, data
, need_fullname
);
321 struct compunit_symtab
*
322 objfile::find_compunit_symtab_by_address (CORE_ADDR address
)
325 fprintf_filtered (gdb_stdlog
,
326 "qf->find_compunit_symtab_by_address (%s, %s)\n",
327 objfile_debug_name (this),
328 hex_string (address
));
330 struct compunit_symtab
*result
= NULL
;
332 result
= qf
->find_compunit_symtab_by_address (this, address
);
335 fprintf_filtered (gdb_stdlog
,
336 "qf->find_compunit_symtab_by_address (...) = %s\n",
338 ? debug_symtab_name (compunit_primary_filetab (result
))
345 objfile::lookup_global_symbol_language (const char *name
,
347 bool *symbol_found_p
)
349 enum language result
= language_unknown
;
352 result
= qf
->lookup_global_symbol_language (this, name
, domain
,
355 *symbol_found_p
= false;
361 /* Debugging version of struct sym_probe_fns. */
363 static const std::vector
<std::unique_ptr
<probe
>> &
364 debug_sym_get_probes (struct objfile
*objfile
)
366 const struct debug_sym_fns_data
*debug_data
367 = symfile_debug_objfile_data_key
.get (objfile
);
369 const std::vector
<std::unique_ptr
<probe
>> &retval
370 = debug_data
->real_sf
->sym_probe_fns
->sym_get_probes (objfile
);
372 fprintf_filtered (gdb_stdlog
,
373 "probes->sym_get_probes (%s) = %s\n",
374 objfile_debug_name (objfile
),
375 host_address_to_string (retval
.data ()));
380 static const struct sym_probe_fns debug_sym_probe_fns
=
382 debug_sym_get_probes
,
385 /* Debugging version of struct sym_fns. */
388 debug_sym_new_init (struct objfile
*objfile
)
390 const struct debug_sym_fns_data
*debug_data
391 = symfile_debug_objfile_data_key
.get (objfile
);
393 fprintf_filtered (gdb_stdlog
, "sf->sym_new_init (%s)\n",
394 objfile_debug_name (objfile
));
396 debug_data
->real_sf
->sym_new_init (objfile
);
400 debug_sym_init (struct objfile
*objfile
)
402 const struct debug_sym_fns_data
*debug_data
403 = symfile_debug_objfile_data_key
.get (objfile
);
405 fprintf_filtered (gdb_stdlog
, "sf->sym_init (%s)\n",
406 objfile_debug_name (objfile
));
408 debug_data
->real_sf
->sym_init (objfile
);
412 debug_sym_read (struct objfile
*objfile
, symfile_add_flags symfile_flags
)
414 const struct debug_sym_fns_data
*debug_data
415 = symfile_debug_objfile_data_key
.get (objfile
);
417 fprintf_filtered (gdb_stdlog
, "sf->sym_read (%s, 0x%x)\n",
418 objfile_debug_name (objfile
), (unsigned) symfile_flags
);
420 debug_data
->real_sf
->sym_read (objfile
, symfile_flags
);
424 debug_sym_finish (struct objfile
*objfile
)
426 const struct debug_sym_fns_data
*debug_data
427 = symfile_debug_objfile_data_key
.get (objfile
);
429 fprintf_filtered (gdb_stdlog
, "sf->sym_finish (%s)\n",
430 objfile_debug_name (objfile
));
432 debug_data
->real_sf
->sym_finish (objfile
);
436 debug_sym_offsets (struct objfile
*objfile
,
437 const section_addr_info
&info
)
439 const struct debug_sym_fns_data
*debug_data
440 = symfile_debug_objfile_data_key
.get (objfile
);
442 fprintf_filtered (gdb_stdlog
, "sf->sym_offsets (%s, %s)\n",
443 objfile_debug_name (objfile
),
444 host_address_to_string (&info
));
446 debug_data
->real_sf
->sym_offsets (objfile
, info
);
449 static symfile_segment_data_up
450 debug_sym_segments (bfd
*abfd
)
452 /* This API function is annoying, it doesn't take a "this" pointer.
453 Fortunately it is only used in one place where we (re-)lookup the
454 sym_fns table to use. Thus we will never be called. */
455 gdb_assert_not_reached ("debug_sym_segments called");
459 debug_sym_read_linetable (struct objfile
*objfile
)
461 const struct debug_sym_fns_data
*debug_data
462 = symfile_debug_objfile_data_key
.get (objfile
);
464 fprintf_filtered (gdb_stdlog
, "sf->sym_read_linetable (%s)\n",
465 objfile_debug_name (objfile
));
467 debug_data
->real_sf
->sym_read_linetable (objfile
);
471 debug_sym_relocate (struct objfile
*objfile
, asection
*sectp
, bfd_byte
*buf
)
473 const struct debug_sym_fns_data
*debug_data
474 = symfile_debug_objfile_data_key
.get (objfile
);
477 retval
= debug_data
->real_sf
->sym_relocate (objfile
, sectp
, buf
);
479 fprintf_filtered (gdb_stdlog
,
480 "sf->sym_relocate (%s, %s, %s) = %s\n",
481 objfile_debug_name (objfile
),
482 host_address_to_string (sectp
),
483 host_address_to_string (buf
),
484 host_address_to_string (retval
));
489 /* Template of debugging version of struct sym_fns.
490 A copy is made, with sym_flavour updated, and a pointer to the real table
491 installed in real_sf, and then a pointer to the copy is installed in the
494 static const struct sym_fns debug_sym_fns
=
502 debug_sym_read_linetable
,
504 &debug_sym_probe_fns
,
507 /* Install the debugging versions of the symfile functions for OBJFILE.
508 Do not call this if the debug versions are already installed. */
511 install_symfile_debug_logging (struct objfile
*objfile
)
513 const struct sym_fns
*real_sf
;
514 struct debug_sym_fns_data
*debug_data
;
516 /* The debug versions should not already be installed. */
517 gdb_assert (!symfile_debug_installed (objfile
));
519 real_sf
= objfile
->sf
;
521 /* Alas we have to preserve NULL entries in REAL_SF. */
522 debug_data
= new struct debug_sym_fns_data
;
524 #define COPY_SF_PTR(from, to, name, func) \
527 (to)->debug_sf.name = func; \
530 COPY_SF_PTR (real_sf
, debug_data
, sym_new_init
, debug_sym_new_init
);
531 COPY_SF_PTR (real_sf
, debug_data
, sym_init
, debug_sym_init
);
532 COPY_SF_PTR (real_sf
, debug_data
, sym_read
, debug_sym_read
);
533 COPY_SF_PTR (real_sf
, debug_data
, sym_finish
, debug_sym_finish
);
534 COPY_SF_PTR (real_sf
, debug_data
, sym_offsets
, debug_sym_offsets
);
535 COPY_SF_PTR (real_sf
, debug_data
, sym_segments
, debug_sym_segments
);
536 COPY_SF_PTR (real_sf
, debug_data
, sym_read_linetable
,
537 debug_sym_read_linetable
);
538 COPY_SF_PTR (real_sf
, debug_data
, sym_relocate
, debug_sym_relocate
);
539 if (real_sf
->sym_probe_fns
)
540 debug_data
->debug_sf
.sym_probe_fns
= &debug_sym_probe_fns
;
544 debug_data
->real_sf
= real_sf
;
545 symfile_debug_objfile_data_key
.set (objfile
, debug_data
);
546 objfile
->sf
= &debug_data
->debug_sf
;
549 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
550 Do not call this if the debug versions are not installed. */
553 uninstall_symfile_debug_logging (struct objfile
*objfile
)
555 struct debug_sym_fns_data
*debug_data
;
557 /* The debug versions should be currently installed. */
558 gdb_assert (symfile_debug_installed (objfile
));
560 debug_data
= symfile_debug_objfile_data_key
.get (objfile
);
562 objfile
->sf
= debug_data
->real_sf
;
563 symfile_debug_objfile_data_key
.clear (objfile
);
566 /* Call this function to set OBJFILE->SF.
567 Do not set OBJFILE->SF directly. */
570 objfile_set_sym_fns (struct objfile
*objfile
, const struct sym_fns
*sf
)
572 if (symfile_debug_installed (objfile
))
574 gdb_assert (debug_symfile
);
575 /* Remove the current one, and reinstall a new one later. */
576 uninstall_symfile_debug_logging (objfile
);
579 /* Assume debug logging is disabled. */
582 /* Turn debug logging on if enabled. */
584 install_symfile_debug_logging (objfile
);
588 set_debug_symfile (const char *args
, int from_tty
, struct cmd_list_element
*c
)
590 for (struct program_space
*pspace
: program_spaces
)
591 for (objfile
*objfile
: pspace
->objfiles ())
595 if (!symfile_debug_installed (objfile
))
596 install_symfile_debug_logging (objfile
);
600 if (symfile_debug_installed (objfile
))
601 uninstall_symfile_debug_logging (objfile
);
607 show_debug_symfile (struct ui_file
*file
, int from_tty
,
608 struct cmd_list_element
*c
, const char *value
)
610 fprintf_filtered (file
, _("Symfile debugging is %s.\n"), value
);
613 void _initialize_symfile_debug ();
615 _initialize_symfile_debug ()
617 add_setshow_boolean_cmd ("symfile", no_class
, &debug_symfile
, _("\
618 Set debugging of the symfile functions."), _("\
619 Show debugging of the symfile functions."), _("\
620 When enabled, all calls to the symfile functions are logged."),
621 set_debug_symfile
, show_debug_symfile
,
622 &setdebuglist
, &showdebuglist
);
624 /* Note: We don't need a new-objfile observer because debug logging
625 will be installed when objfile init'n calls objfile_set_sym_fns. */