Add translations for various sub-directories
[binutils-gdb.git] / gdb / symfile-debug.c
blobb21a5a433f3492e4f55c812abdf15e7c8349607d
1 /* Debug logging for the symbol file functions for the GNU debugger, GDB.
3 Copyright (C) 2013-2024 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
26 returns. */
28 #include "cli/cli-cmds.h"
29 #include "objfiles.h"
30 #include "observable.h"
31 #include "source.h"
32 #include "symtab.h"
33 #include "symfile.h"
34 #include "block.h"
35 #include "filenames.h"
36 #include "cli/cli-style.h"
37 #include "build-id.h"
38 #include "debuginfod-support.h"
40 /* We need to save a pointer to the real symbol functions.
41 Plus, the debug versions are malloc'd because we have to NULL out the
42 ones that are NULL in the real copy. */
44 struct debug_sym_fns_data
46 const struct sym_fns *real_sf = nullptr;
47 struct sym_fns debug_sf {};
50 /* We need to record a pointer to the real set of functions for each
51 objfile. */
52 static const registry<objfile>::key<debug_sym_fns_data>
53 symfile_debug_objfile_data_key;
55 /* If true all calls to the symfile functions are logged. */
56 static bool debug_symfile = false;
58 /* Return non-zero if symfile debug logging is installed. */
60 static int
61 symfile_debug_installed (struct objfile *objfile)
63 return (objfile->sf != NULL
64 && symfile_debug_objfile_data_key.get (objfile) != NULL);
67 /* Utility return the name to print for SYMTAB. */
69 static const char *
70 debug_symtab_name (struct symtab *symtab)
72 return symtab_to_filename_for_display (symtab);
76 /* See objfiles.h. */
78 bool
79 objfile::has_partial_symbols ()
81 bool retval = false;
83 /* If we have not read psymbols, but we have a function capable of reading
84 them, then that is an indication that they are in fact available. Without
85 this function the symbols may have been already read in but they also may
86 not be present in this objfile. */
87 for (const auto &iter : qf)
89 retval = iter->has_symbols (this);
90 if (retval)
91 break;
94 if (debug_symfile)
95 gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
96 objfile_debug_name (this), retval);
98 return retval;
101 /* See objfiles.h. */
102 bool
103 objfile::has_unexpanded_symtabs ()
105 if (debug_symfile)
106 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
107 objfile_debug_name (this));
109 bool result = false;
110 for (const auto &iter : qf)
112 if (iter->has_unexpanded_symtabs (this))
114 result = true;
115 break;
119 if (debug_symfile)
120 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
121 objfile_debug_name (this), (result ? 1 : 0));
123 return result;
126 struct symtab *
127 objfile::find_last_source_symtab ()
129 struct symtab *retval = nullptr;
131 if (debug_symfile)
132 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
133 objfile_debug_name (this));
135 for (const auto &iter : qf)
137 retval = iter->find_last_source_symtab (this);
138 if (retval != nullptr)
139 break;
142 if (debug_symfile)
143 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
144 retval ? debug_symtab_name (retval) : "NULL");
146 return retval;
149 void
150 objfile::forget_cached_source_info ()
152 if (debug_symfile)
153 gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
154 objfile_debug_name (this));
156 for (compunit_symtab *cu : compunits ())
157 cu->forget_cached_source_info ();
159 for (const auto &iter : qf)
160 iter->forget_cached_source_info (this);
163 bool
164 objfile::map_symtabs_matching_filename
165 (const char *name, const char *real_path,
166 gdb::function_view<bool (symtab *)> callback)
168 if (debug_symfile)
169 gdb_printf (gdb_stdlog,
170 "qf->map_symtabs_matching_filename (%s, \"%s\", "
171 "\"%s\", %s)\n",
172 objfile_debug_name (this), name,
173 real_path ? real_path : NULL,
174 host_address_to_string (&callback));
176 bool retval = true;
177 const char *name_basename = lbasename (name);
179 auto match_one_filename = [&] (const char *filename, bool basenames)
181 if (compare_filenames_for_search (filename, name))
182 return true;
183 if (basenames && FILENAME_CMP (name_basename, filename) == 0)
184 return true;
185 if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
186 && IS_ABSOLUTE_PATH (real_path))
187 return filename_cmp (filename, real_path) == 0;
188 return false;
191 compunit_symtab *last_made = this->compunit_symtabs;
193 auto on_expansion = [&] (compunit_symtab *symtab)
195 /* The callback to iterate_over_some_symtabs returns false to keep
196 going and true to continue, so we have to invert the result
197 here, for expand_symtabs_matching. */
198 bool result = !iterate_over_some_symtabs (name, real_path,
199 this->compunit_symtabs,
200 last_made,
201 callback);
202 last_made = this->compunit_symtabs;
203 return result;
206 for (const auto &iter : qf)
208 if (!iter->expand_symtabs_matching (this,
209 match_one_filename,
210 nullptr,
211 nullptr,
212 on_expansion,
213 (SEARCH_GLOBAL_BLOCK
214 | SEARCH_STATIC_BLOCK),
215 SEARCH_ALL_DOMAINS))
217 retval = false;
218 break;
222 if (debug_symfile)
223 gdb_printf (gdb_stdlog,
224 "qf->map_symtabs_matching_filename (...) = %d\n",
225 retval);
227 /* We must re-invert the return value here to match the caller's
228 expectations. */
229 return !retval;
232 struct compunit_symtab *
233 objfile::lookup_symbol (block_enum kind, const lookup_name_info &name,
234 domain_search_flags domain)
236 struct compunit_symtab *retval = nullptr;
238 if (debug_symfile)
239 gdb_printf (gdb_stdlog,
240 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
241 objfile_debug_name (this), kind, name.c_str (),
242 domain_name (domain).c_str ());
244 auto search_one_symtab = [&] (compunit_symtab *stab)
246 struct symbol *sym, *with_opaque = NULL;
247 const struct blockvector *bv = stab->blockvector ();
248 const struct block *block = bv->block (kind);
250 sym = block_find_symbol (block, name, domain, &with_opaque);
252 /* Some caution must be observed with overloaded functions
253 and methods, since the index will not contain any overload
254 information (but NAME might contain it). */
256 if (sym != nullptr)
258 retval = stab;
259 /* Found it. */
260 return false;
262 if (with_opaque != nullptr)
263 retval = stab;
265 /* Keep looking through other psymtabs. */
266 return true;
269 for (const auto &iter : qf)
271 if (!iter->expand_symtabs_matching (this,
272 nullptr,
273 &name,
274 nullptr,
275 search_one_symtab,
276 kind == GLOBAL_BLOCK
277 ? SEARCH_GLOBAL_BLOCK
278 : SEARCH_STATIC_BLOCK,
279 domain))
280 break;
283 if (debug_symfile)
284 gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
285 retval
286 ? debug_symtab_name (retval->primary_filetab ())
287 : "NULL");
289 return retval;
292 void
293 objfile::print_stats (bool print_bcache)
295 if (debug_symfile)
296 gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
297 objfile_debug_name (this), print_bcache);
299 for (const auto &iter : qf)
300 iter->print_stats (this, print_bcache);
303 void
304 objfile::dump ()
306 if (debug_symfile)
307 gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
308 objfile_debug_name (this));
310 for (const auto &iter : qf)
311 iter->dump (this);
314 void
315 objfile::expand_symtabs_for_function (const char *func_name)
317 if (debug_symfile)
318 gdb_printf (gdb_stdlog,
319 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
320 objfile_debug_name (this), func_name);
322 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
323 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
325 for (const auto &iter : qf)
326 iter->expand_symtabs_matching (this,
327 nullptr,
328 &lookup_name,
329 nullptr,
330 nullptr,
331 (SEARCH_GLOBAL_BLOCK
332 | SEARCH_STATIC_BLOCK),
333 SEARCH_FUNCTION_DOMAIN);
336 void
337 objfile::expand_all_symtabs ()
339 if (debug_symfile)
340 gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
341 objfile_debug_name (this));
343 for (const auto &iter : qf)
344 iter->expand_all_symtabs (this);
347 void
348 objfile::expand_symtabs_with_fullname (const char *fullname)
350 if (debug_symfile)
351 gdb_printf (gdb_stdlog,
352 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
353 objfile_debug_name (this), fullname);
355 const char *basename = lbasename (fullname);
356 auto file_matcher = [&] (const char *filename, bool basenames)
358 return filename_cmp (basenames ? basename : fullname, filename) == 0;
361 for (const auto &iter : qf)
362 iter->expand_symtabs_matching (this,
363 file_matcher,
364 nullptr,
365 nullptr,
366 nullptr,
367 (SEARCH_GLOBAL_BLOCK
368 | SEARCH_STATIC_BLOCK),
369 SEARCH_ALL_DOMAINS);
372 bool
373 objfile::expand_symtabs_matching
374 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
375 const lookup_name_info *lookup_name,
376 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
377 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
378 block_search_flags search_flags,
379 domain_search_flags domain,
380 gdb::function_view<expand_symtabs_lang_matcher_ftype> lang_matcher)
382 /* This invariant is documented in quick-functions.h. */
383 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
385 if (debug_symfile)
386 gdb_printf (gdb_stdlog,
387 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
388 objfile_debug_name (this),
389 host_address_to_string (&file_matcher),
390 host_address_to_string (&symbol_matcher),
391 host_address_to_string (&expansion_notify),
392 domain_name (domain).c_str ());
394 for (const auto &iter : qf)
395 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
396 symbol_matcher, expansion_notify,
397 search_flags, domain,
398 lang_matcher))
399 return false;
400 return true;
403 struct compunit_symtab *
404 objfile::find_pc_sect_compunit_symtab (bound_minimal_symbol msymbol,
405 CORE_ADDR pc,
406 struct obj_section *section,
407 int warn_if_readin)
409 struct compunit_symtab *retval = nullptr;
411 if (debug_symfile)
412 gdb_printf (gdb_stdlog,
413 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
414 objfile_debug_name (this),
415 host_address_to_string (msymbol.minsym),
416 hex_string (pc),
417 host_address_to_string (section),
418 warn_if_readin);
420 for (const auto &iter : qf)
422 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
423 warn_if_readin);
424 if (retval != nullptr)
425 break;
428 if (debug_symfile)
429 gdb_printf (gdb_stdlog,
430 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
431 retval
432 ? debug_symtab_name (retval->primary_filetab ())
433 : "NULL");
435 return retval;
438 void
439 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
440 bool need_fullname)
442 if (debug_symfile)
443 gdb_printf (gdb_stdlog,
444 "qf->map_symbol_filenames (%s, ..., %d)\n",
445 objfile_debug_name (this),
446 need_fullname);
448 for (const auto &iter : qf)
449 iter->map_symbol_filenames (this, fun, need_fullname);
452 void
453 objfile::compute_main_name ()
455 if (debug_symfile)
456 gdb_printf (gdb_stdlog,
457 "qf->compute_main_name (%s)\n",
458 objfile_debug_name (this));
460 for (const auto &iter : qf)
461 iter->compute_main_name (this);
464 struct compunit_symtab *
465 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
467 if (debug_symfile)
468 gdb_printf (gdb_stdlog,
469 "qf->find_compunit_symtab_by_address (%s, %s)\n",
470 objfile_debug_name (this),
471 hex_string (address));
473 struct compunit_symtab *result = NULL;
474 for (const auto &iter : qf)
476 result = iter->find_compunit_symtab_by_address (this, address);
477 if (result != nullptr)
478 break;
481 if (debug_symfile)
482 gdb_printf (gdb_stdlog,
483 "qf->find_compunit_symtab_by_address (...) = %s\n",
484 result
485 ? debug_symtab_name (result->primary_filetab ())
486 : "NULL");
488 return result;
491 enum language
492 objfile::lookup_global_symbol_language (const char *name,
493 domain_search_flags domain,
494 bool *symbol_found_p)
496 enum language result = language_unknown;
497 *symbol_found_p = false;
499 for (const auto &iter : qf)
501 result = iter->lookup_global_symbol_language (this, name, domain,
502 symbol_found_p);
503 if (*symbol_found_p)
504 break;
507 return result;
510 /* Call LOOKUP_FUNC to find the filename of a file containing the separate
511 debug information matching OBJFILE. If LOOKUP_FUNC does return a
512 filename then open this file and return a std::pair containing the
513 gdb_bfd_ref_ptr of the open file and the filename returned by
514 LOOKUP_FUNC, otherwise this function returns an empty pair; the first
515 item will be nullptr, and the second will be an empty string.
517 Any warnings generated by this function, or by calling LOOKUP_FUNC are
518 placed into WARNINGS, these warnings are only displayed to the user if
519 GDB is unable to find the separate debug information via any route. */
520 static std::pair<gdb_bfd_ref_ptr, std::string>
521 simple_find_and_open_separate_symbol_file
522 (struct objfile *objfile,
523 std::string (*lookup_func) (struct objfile *, deferred_warnings *),
524 deferred_warnings *warnings)
526 std::string filename = lookup_func (objfile, warnings);
528 if (!filename.empty ())
530 gdb_bfd_ref_ptr symfile_bfd
531 = symfile_bfd_open_no_error (filename.c_str ());
532 if (symfile_bfd != nullptr)
533 return { symfile_bfd, filename };
536 return {};
539 /* Lookup separate debug information for OBJFILE via debuginfod. If
540 successful the debug information will be have been downloaded into the
541 debuginfod cache and this function will return a std::pair containing a
542 gdb_bfd_ref_ptr of the open debug information file and the filename for
543 the file within the debuginfod cache. If no debug information could be
544 found then this function returns an empty pair; the first item will be
545 nullptr, and the second will be an empty string. */
547 static std::pair<gdb_bfd_ref_ptr, std::string>
548 debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
550 const struct bfd_build_id *build_id
551 = build_id_bfd_get (objfile->obfd.get ());
552 const char *filename = bfd_get_filename (objfile->obfd.get ());
554 if (build_id != nullptr)
556 gdb::unique_xmalloc_ptr<char> symfile_path;
557 scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size,
558 filename, &symfile_path));
560 if (fd.get () >= 0)
562 /* File successfully retrieved from server. */
563 gdb_bfd_ref_ptr debug_bfd
564 (symfile_bfd_open_no_error (symfile_path.get ()));
566 if (debug_bfd != nullptr
567 && build_id_verify (debug_bfd.get (),
568 build_id->size, build_id->data))
569 return { debug_bfd, std::string (symfile_path.get ()) };
573 return {};
576 /* See objfiles.h. */
578 bool
579 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
581 bool has_dwarf2 = false;
583 /* Usually we only make a single pass when looking for separate debug
584 information. However, it is possible for an extension language hook
585 to request that GDB make a second pass, in which case max_attempts
586 will be updated, and the loop restarted. */
587 for (unsigned attempt = 0, max_attempts = 1;
588 attempt < max_attempts && !has_dwarf2;
589 ++attempt)
591 gdb_assert (max_attempts <= 2);
593 deferred_warnings warnings;
594 gdb_bfd_ref_ptr debug_bfd;
595 std::string filename;
597 std::tie (debug_bfd, filename)
598 = simple_find_and_open_separate_symbol_file
599 (this, find_separate_debug_file_by_buildid, &warnings);
601 if (debug_bfd == nullptr)
602 std::tie (debug_bfd, filename)
603 = simple_find_and_open_separate_symbol_file
604 (this, find_separate_debug_file_by_debuglink, &warnings);
606 /* Only try debuginfod on the first attempt. Sure, we could imagine
607 an extension that somehow adds the required debug info to the
608 debuginfod server but, at least for now, we don't support this
609 scenario. Better for the extension to return new debug info
610 directly to GDB. Plus, going to the debuginfod server might be
611 slow, so that's a good argument for only doing this once. */
612 if (debug_bfd == nullptr && attempt == 0)
613 std::tie (debug_bfd, filename)
614 = debuginfod_find_and_open_separate_symbol_file (this);
616 if (debug_bfd != nullptr)
618 /* We found a separate debug info symbol file. If this is our
619 first attempt then setting HAS_DWARF2 will cause us to break
620 from the attempt loop. */
621 symbol_file_add_separate (debug_bfd, filename.c_str (),
622 symfile_flags, this);
623 has_dwarf2 = true;
625 else if (attempt == 0)
627 /* Failed to find a separate debug info symbol file. Call out to
628 the extension languages. The user might have registered an
629 extension that can find the debug info for us, or maybe give
630 the user a system specific message that guides them to finding
631 the missing debug info. */
633 ext_lang_missing_file_result ext_result
634 = ext_lang_handle_missing_debuginfo (this);
635 if (!ext_result.filename ().empty ())
637 /* Extension found a suitable debug file for us. */
638 debug_bfd
639 = symfile_bfd_open_no_error (ext_result.filename ().c_str ());
641 if (debug_bfd != nullptr)
643 symbol_file_add_separate (debug_bfd,
644 ext_result.filename ().c_str (),
645 symfile_flags, this);
646 has_dwarf2 = true;
649 else if (ext_result.try_again ())
651 max_attempts = 2;
652 continue;
656 /* If we still have not got a separate debug symbol file, then
657 emit any warnings we've collected so far. */
658 if (!has_dwarf2)
659 warnings.emit ();
662 return has_dwarf2;
666 /* Debugging version of struct sym_probe_fns. */
668 static const std::vector<std::unique_ptr<probe>> &
669 debug_sym_get_probes (struct objfile *objfile)
671 const struct debug_sym_fns_data *debug_data
672 = symfile_debug_objfile_data_key.get (objfile);
674 const std::vector<std::unique_ptr<probe>> &retval
675 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
677 gdb_printf (gdb_stdlog,
678 "probes->sym_get_probes (%s) = %s\n",
679 objfile_debug_name (objfile),
680 host_address_to_string (retval.data ()));
682 return retval;
685 static const struct sym_probe_fns debug_sym_probe_fns =
687 debug_sym_get_probes,
690 /* Debugging version of struct sym_fns. */
692 static void
693 debug_sym_new_init (struct objfile *objfile)
695 const struct debug_sym_fns_data *debug_data
696 = symfile_debug_objfile_data_key.get (objfile);
698 gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
699 objfile_debug_name (objfile));
701 debug_data->real_sf->sym_new_init (objfile);
704 static void
705 debug_sym_init (struct objfile *objfile)
707 const struct debug_sym_fns_data *debug_data
708 = symfile_debug_objfile_data_key.get (objfile);
710 gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
711 objfile_debug_name (objfile));
713 debug_data->real_sf->sym_init (objfile);
716 static void
717 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
719 const struct debug_sym_fns_data *debug_data
720 = symfile_debug_objfile_data_key.get (objfile);
722 gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
723 objfile_debug_name (objfile), (unsigned) symfile_flags);
725 debug_data->real_sf->sym_read (objfile, symfile_flags);
728 static void
729 debug_sym_finish (struct objfile *objfile)
731 const struct debug_sym_fns_data *debug_data
732 = symfile_debug_objfile_data_key.get (objfile);
734 gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
735 objfile_debug_name (objfile));
737 debug_data->real_sf->sym_finish (objfile);
740 static void
741 debug_sym_offsets (struct objfile *objfile,
742 const section_addr_info &info)
744 const struct debug_sym_fns_data *debug_data
745 = symfile_debug_objfile_data_key.get (objfile);
747 gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
748 objfile_debug_name (objfile),
749 host_address_to_string (&info));
751 debug_data->real_sf->sym_offsets (objfile, info);
754 static symfile_segment_data_up
755 debug_sym_segments (bfd *abfd)
757 /* This API function is annoying, it doesn't take a "this" pointer.
758 Fortunately it is only used in one place where we (re-)lookup the
759 sym_fns table to use. Thus we will never be called. */
760 gdb_assert_not_reached ("debug_sym_segments called");
763 static void
764 debug_sym_read_linetable (struct objfile *objfile)
766 const struct debug_sym_fns_data *debug_data
767 = symfile_debug_objfile_data_key.get (objfile);
769 gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
770 objfile_debug_name (objfile));
772 debug_data->real_sf->sym_read_linetable (objfile);
775 static bfd_byte *
776 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
778 const struct debug_sym_fns_data *debug_data
779 = symfile_debug_objfile_data_key.get (objfile);
780 bfd_byte *retval;
782 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
784 gdb_printf (gdb_stdlog,
785 "sf->sym_relocate (%s, %s, %s) = %s\n",
786 objfile_debug_name (objfile),
787 host_address_to_string (sectp),
788 host_address_to_string (buf),
789 host_address_to_string (retval));
791 return retval;
794 /* Template of debugging version of struct sym_fns.
795 A copy is made, with sym_flavour updated, and a pointer to the real table
796 installed in real_sf, and then a pointer to the copy is installed in the
797 objfile. */
799 static const struct sym_fns debug_sym_fns =
801 debug_sym_new_init,
802 debug_sym_init,
803 debug_sym_read,
804 debug_sym_finish,
805 debug_sym_offsets,
806 debug_sym_segments,
807 debug_sym_read_linetable,
808 debug_sym_relocate,
809 &debug_sym_probe_fns,
812 /* Install the debugging versions of the symfile functions for OBJFILE.
813 Do not call this if the debug versions are already installed. */
815 static void
816 install_symfile_debug_logging (struct objfile *objfile)
818 const struct sym_fns *real_sf;
819 struct debug_sym_fns_data *debug_data;
821 /* The debug versions should not already be installed. */
822 gdb_assert (!symfile_debug_installed (objfile));
824 real_sf = objfile->sf;
826 /* Alas we have to preserve NULL entries in REAL_SF. */
827 debug_data = new struct debug_sym_fns_data;
829 #define COPY_SF_PTR(from, to, name, func) \
830 do { \
831 if ((from)->name) \
832 (to)->debug_sf.name = func; \
833 } while (0)
835 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
836 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
837 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
838 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
839 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
840 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
841 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
842 debug_sym_read_linetable);
843 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
844 if (real_sf->sym_probe_fns)
845 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
847 #undef COPY_SF_PTR
849 debug_data->real_sf = real_sf;
850 symfile_debug_objfile_data_key.set (objfile, debug_data);
851 objfile->sf = &debug_data->debug_sf;
854 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
855 Do not call this if the debug versions are not installed. */
857 static void
858 uninstall_symfile_debug_logging (struct objfile *objfile)
860 struct debug_sym_fns_data *debug_data;
862 /* The debug versions should be currently installed. */
863 gdb_assert (symfile_debug_installed (objfile));
865 debug_data = symfile_debug_objfile_data_key.get (objfile);
867 objfile->sf = debug_data->real_sf;
868 symfile_debug_objfile_data_key.clear (objfile);
871 /* Call this function to set OBJFILE->SF.
872 Do not set OBJFILE->SF directly. */
874 void
875 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
877 if (symfile_debug_installed (objfile))
879 gdb_assert (debug_symfile);
880 /* Remove the current one, and reinstall a new one later. */
881 uninstall_symfile_debug_logging (objfile);
884 /* Assume debug logging is disabled. */
885 objfile->sf = sf;
887 /* Turn debug logging on if enabled. */
888 if (debug_symfile)
889 install_symfile_debug_logging (objfile);
892 static void
893 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
895 for (struct program_space *pspace : program_spaces)
896 for (objfile *objfile : pspace->objfiles ())
898 if (debug_symfile)
900 if (!symfile_debug_installed (objfile))
901 install_symfile_debug_logging (objfile);
903 else
905 if (symfile_debug_installed (objfile))
906 uninstall_symfile_debug_logging (objfile);
911 static void
912 show_debug_symfile (struct ui_file *file, int from_tty,
913 struct cmd_list_element *c, const char *value)
915 gdb_printf (file, _("Symfile debugging is %s.\n"), value);
918 void _initialize_symfile_debug ();
919 void
920 _initialize_symfile_debug ()
922 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
923 Set debugging of the symfile functions."), _("\
924 Show debugging of the symfile functions."), _("\
925 When enabled, all calls to the symfile functions are logged."),
926 set_debug_symfile, show_debug_symfile,
927 &setdebuglist, &showdebuglist);
929 /* Note: We don't need a new-objfile observer because debug logging
930 will be installed when objfile init'n calls objfile_set_sym_fns. */