[gdb/tdep] Fix reverse execution of LDR(immediate) T4
[binutils-gdb.git] / gdb / symfile-debug.c
blob36719fccabe443549ccaed61c0704f5157aaff6c
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 "defs.h"
29 #include "gdbcmd.h"
30 #include "objfiles.h"
31 #include "observable.h"
32 #include "source.h"
33 #include "symtab.h"
34 #include "symfile.h"
35 #include "block.h"
36 #include "filenames.h"
37 #include "cli/cli-style.h"
38 #include "build-id.h"
39 #include "debuginfod-support.h"
41 /* We need to save a pointer to the real symbol functions.
42 Plus, the debug versions are malloc'd because we have to NULL out the
43 ones that are NULL in the real copy. */
45 struct debug_sym_fns_data
47 const struct sym_fns *real_sf = nullptr;
48 struct sym_fns debug_sf {};
51 /* We need to record a pointer to the real set of functions for each
52 objfile. */
53 static const registry<objfile>::key<debug_sym_fns_data>
54 symfile_debug_objfile_data_key;
56 /* If true all calls to the symfile functions are logged. */
57 static bool debug_symfile = false;
59 /* Return non-zero if symfile debug logging is installed. */
61 static int
62 symfile_debug_installed (struct objfile *objfile)
64 return (objfile->sf != NULL
65 && symfile_debug_objfile_data_key.get (objfile) != NULL);
68 /* Utility return the name to print for SYMTAB. */
70 static const char *
71 debug_symtab_name (struct symtab *symtab)
73 return symtab_to_filename_for_display (symtab);
77 /* See objfiles.h. */
79 bool
80 objfile::has_partial_symbols ()
82 bool retval = false;
84 /* If we have not read psymbols, but we have a function capable of reading
85 them, then that is an indication that they are in fact available. Without
86 this function the symbols may have been already read in but they also may
87 not be present in this objfile. */
88 for (const auto &iter : qf)
90 retval = iter->has_symbols (this);
91 if (retval)
92 break;
95 if (debug_symfile)
96 gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
97 objfile_debug_name (this), retval);
99 return retval;
102 /* See objfiles.h. */
103 bool
104 objfile::has_unexpanded_symtabs ()
106 if (debug_symfile)
107 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
108 objfile_debug_name (this));
110 bool result = false;
111 for (const auto &iter : qf)
113 if (iter->has_unexpanded_symtabs (this))
115 result = true;
116 break;
120 if (debug_symfile)
121 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
122 objfile_debug_name (this), (result ? 1 : 0));
124 return result;
127 struct symtab *
128 objfile::find_last_source_symtab ()
130 struct symtab *retval = nullptr;
132 if (debug_symfile)
133 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
134 objfile_debug_name (this));
136 for (const auto &iter : qf)
138 retval = iter->find_last_source_symtab (this);
139 if (retval != nullptr)
140 break;
143 if (debug_symfile)
144 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
145 retval ? debug_symtab_name (retval) : "NULL");
147 return retval;
150 void
151 objfile::forget_cached_source_info ()
153 if (debug_symfile)
154 gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
155 objfile_debug_name (this));
157 for (compunit_symtab *cu : compunits ())
159 for (symtab *s : cu->filetabs ())
161 if (s->fullname != NULL)
163 xfree (s->fullname);
164 s->fullname = NULL;
169 for (const auto &iter : qf)
170 iter->forget_cached_source_info (this);
173 bool
174 objfile::map_symtabs_matching_filename
175 (const char *name, const char *real_path,
176 gdb::function_view<bool (symtab *)> callback)
178 if (debug_symfile)
179 gdb_printf (gdb_stdlog,
180 "qf->map_symtabs_matching_filename (%s, \"%s\", "
181 "\"%s\", %s)\n",
182 objfile_debug_name (this), name,
183 real_path ? real_path : NULL,
184 host_address_to_string (&callback));
186 bool retval = true;
187 const char *name_basename = lbasename (name);
189 auto match_one_filename = [&] (const char *filename, bool basenames)
191 if (compare_filenames_for_search (filename, name))
192 return true;
193 if (basenames && FILENAME_CMP (name_basename, filename) == 0)
194 return true;
195 if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
196 && IS_ABSOLUTE_PATH (real_path))
197 return filename_cmp (filename, real_path) == 0;
198 return false;
201 compunit_symtab *last_made = this->compunit_symtabs;
203 auto on_expansion = [&] (compunit_symtab *symtab)
205 /* The callback to iterate_over_some_symtabs returns false to keep
206 going and true to continue, so we have to invert the result
207 here, for expand_symtabs_matching. */
208 bool result = !iterate_over_some_symtabs (name, real_path,
209 this->compunit_symtabs,
210 last_made,
211 callback);
212 last_made = this->compunit_symtabs;
213 return result;
216 for (const auto &iter : qf)
218 if (!iter->expand_symtabs_matching (this,
219 match_one_filename,
220 nullptr,
221 nullptr,
222 on_expansion,
223 (SEARCH_GLOBAL_BLOCK
224 | SEARCH_STATIC_BLOCK),
225 SEARCH_ALL_DOMAINS))
227 retval = false;
228 break;
232 if (debug_symfile)
233 gdb_printf (gdb_stdlog,
234 "qf->map_symtabs_matching_filename (...) = %d\n",
235 retval);
237 /* We must re-invert the return value here to match the caller's
238 expectations. */
239 return !retval;
242 struct compunit_symtab *
243 objfile::lookup_symbol (block_enum kind, const char *name,
244 domain_search_flags domain)
246 struct compunit_symtab *retval = nullptr;
248 if (debug_symfile)
249 gdb_printf (gdb_stdlog,
250 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
251 objfile_debug_name (this), kind, name,
252 domain_name (domain).c_str ());
254 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
256 auto search_one_symtab = [&] (compunit_symtab *stab)
258 struct symbol *sym, *with_opaque = NULL;
259 const struct blockvector *bv = stab->blockvector ();
260 const struct block *block = bv->block (kind);
262 sym = block_find_symbol (block, lookup_name, domain, &with_opaque);
264 /* Some caution must be observed with overloaded functions
265 and methods, since the index will not contain any overload
266 information (but NAME might contain it). */
268 if (sym != nullptr)
270 retval = stab;
271 /* Found it. */
272 return false;
274 if (with_opaque != nullptr)
275 retval = stab;
277 /* Keep looking through other psymtabs. */
278 return true;
281 for (const auto &iter : qf)
283 if (!iter->expand_symtabs_matching (this,
284 nullptr,
285 &lookup_name,
286 nullptr,
287 search_one_symtab,
288 kind == GLOBAL_BLOCK
289 ? SEARCH_GLOBAL_BLOCK
290 : SEARCH_STATIC_BLOCK,
291 domain))
292 break;
295 if (debug_symfile)
296 gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
297 retval
298 ? debug_symtab_name (retval->primary_filetab ())
299 : "NULL");
301 return retval;
304 void
305 objfile::print_stats (bool print_bcache)
307 if (debug_symfile)
308 gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
309 objfile_debug_name (this), print_bcache);
311 for (const auto &iter : qf)
312 iter->print_stats (this, print_bcache);
315 void
316 objfile::dump ()
318 if (debug_symfile)
319 gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
320 objfile_debug_name (this));
322 for (const auto &iter : qf)
323 iter->dump (this);
326 void
327 objfile::expand_symtabs_for_function (const char *func_name)
329 if (debug_symfile)
330 gdb_printf (gdb_stdlog,
331 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
332 objfile_debug_name (this), func_name);
334 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
335 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
337 for (const auto &iter : qf)
338 iter->expand_symtabs_matching (this,
339 nullptr,
340 &lookup_name,
341 nullptr,
342 nullptr,
343 (SEARCH_GLOBAL_BLOCK
344 | SEARCH_STATIC_BLOCK),
345 SEARCH_FUNCTION_DOMAIN);
348 void
349 objfile::expand_all_symtabs ()
351 if (debug_symfile)
352 gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
353 objfile_debug_name (this));
355 for (const auto &iter : qf)
356 iter->expand_all_symtabs (this);
359 void
360 objfile::expand_symtabs_with_fullname (const char *fullname)
362 if (debug_symfile)
363 gdb_printf (gdb_stdlog,
364 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
365 objfile_debug_name (this), fullname);
367 const char *basename = lbasename (fullname);
368 auto file_matcher = [&] (const char *filename, bool basenames)
370 return filename_cmp (basenames ? basename : fullname, filename) == 0;
373 for (const auto &iter : qf)
374 iter->expand_symtabs_matching (this,
375 file_matcher,
376 nullptr,
377 nullptr,
378 nullptr,
379 (SEARCH_GLOBAL_BLOCK
380 | SEARCH_STATIC_BLOCK),
381 SEARCH_ALL_DOMAINS);
384 bool
385 objfile::expand_symtabs_matching
386 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
387 const lookup_name_info *lookup_name,
388 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
389 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
390 block_search_flags search_flags,
391 domain_search_flags domain)
393 /* This invariant is documented in quick-functions.h. */
394 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
396 if (debug_symfile)
397 gdb_printf (gdb_stdlog,
398 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
399 objfile_debug_name (this),
400 host_address_to_string (&file_matcher),
401 host_address_to_string (&symbol_matcher),
402 host_address_to_string (&expansion_notify),
403 domain_name (domain).c_str ());
405 for (const auto &iter : qf)
406 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
407 symbol_matcher, expansion_notify,
408 search_flags, domain))
409 return false;
410 return true;
413 struct compunit_symtab *
414 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
415 CORE_ADDR pc,
416 struct obj_section *section,
417 int warn_if_readin)
419 struct compunit_symtab *retval = nullptr;
421 if (debug_symfile)
422 gdb_printf (gdb_stdlog,
423 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
424 objfile_debug_name (this),
425 host_address_to_string (msymbol.minsym),
426 hex_string (pc),
427 host_address_to_string (section),
428 warn_if_readin);
430 for (const auto &iter : qf)
432 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
433 warn_if_readin);
434 if (retval != nullptr)
435 break;
438 if (debug_symfile)
439 gdb_printf (gdb_stdlog,
440 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
441 retval
442 ? debug_symtab_name (retval->primary_filetab ())
443 : "NULL");
445 return retval;
448 void
449 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
450 bool need_fullname)
452 if (debug_symfile)
453 gdb_printf (gdb_stdlog,
454 "qf->map_symbol_filenames (%s, ..., %d)\n",
455 objfile_debug_name (this),
456 need_fullname);
458 for (const auto &iter : qf)
459 iter->map_symbol_filenames (this, fun, need_fullname);
462 void
463 objfile::compute_main_name ()
465 if (debug_symfile)
466 gdb_printf (gdb_stdlog,
467 "qf->compute_main_name (%s)\n",
468 objfile_debug_name (this));
470 for (const auto &iter : qf)
471 iter->compute_main_name (this);
474 struct compunit_symtab *
475 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
477 if (debug_symfile)
478 gdb_printf (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)
488 break;
491 if (debug_symfile)
492 gdb_printf (gdb_stdlog,
493 "qf->find_compunit_symtab_by_address (...) = %s\n",
494 result
495 ? debug_symtab_name (result->primary_filetab ())
496 : "NULL");
498 return result;
501 enum language
502 objfile::lookup_global_symbol_language (const char *name,
503 domain_search_flags domain,
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,
512 symbol_found_p);
513 if (*symbol_found_p)
514 break;
517 return result;
520 /* Call LOOKUP_FUNC to find the filename of a file containing the separate
521 debug information matching OBJFILE. If LOOKUP_FUNC does return a
522 filename then open this file and return a std::pair containing the
523 gdb_bfd_ref_ptr of the open file and the filename returned by
524 LOOKUP_FUNC, otherwise this function returns an empty pair; the first
525 item will be nullptr, and the second will be an empty string.
527 Any warnings generated by this function, or by calling LOOKUP_FUNC are
528 placed into WARNINGS, these warnings are only displayed to the user if
529 GDB is unable to find the separate debug information via any route. */
530 static std::pair<gdb_bfd_ref_ptr, std::string>
531 simple_find_and_open_separate_symbol_file
532 (struct objfile *objfile,
533 std::string (*lookup_func) (struct objfile *, deferred_warnings *),
534 deferred_warnings *warnings)
536 std::string filename = lookup_func (objfile, warnings);
538 if (!filename.empty ())
540 gdb_bfd_ref_ptr symfile_bfd
541 = symfile_bfd_open_no_error (filename.c_str ());
542 if (symfile_bfd != nullptr)
543 return { symfile_bfd, filename };
546 return {};
549 /* Lookup separate debug information for OBJFILE via debuginfod. If
550 successful the debug information will be have been downloaded into the
551 debuginfod cache and this function will return a std::pair containing a
552 gdb_bfd_ref_ptr of the open debug information file and the filename for
553 the file within the debuginfod cache. If no debug information could be
554 found then this function returns an empty pair; the first item will be
555 nullptr, and the second will be an empty string. */
557 static std::pair<gdb_bfd_ref_ptr, std::string>
558 debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
560 const struct bfd_build_id *build_id
561 = build_id_bfd_get (objfile->obfd.get ());
562 const char *filename = bfd_get_filename (objfile->obfd.get ());
564 if (build_id != nullptr)
566 gdb::unique_xmalloc_ptr<char> symfile_path;
567 scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size,
568 filename, &symfile_path));
570 if (fd.get () >= 0)
572 /* File successfully retrieved from server. */
573 gdb_bfd_ref_ptr debug_bfd
574 (symfile_bfd_open_no_error (symfile_path.get ()));
576 if (debug_bfd != nullptr
577 && build_id_verify (debug_bfd.get (),
578 build_id->size, build_id->data))
579 return { debug_bfd, std::string (symfile_path.get ()) };
583 return {};
586 /* See objfiles.h. */
588 bool
589 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
591 bool has_dwarf2 = false;
593 /* Usually we only make a single pass when looking for separate debug
594 information. However, it is possible for an extension language hook
595 to request that GDB make a second pass, in which case max_attempts
596 will be updated, and the loop restarted. */
597 for (unsigned attempt = 0, max_attempts = 1;
598 attempt < max_attempts && !has_dwarf2;
599 ++attempt)
601 gdb_assert (max_attempts <= 2);
603 deferred_warnings warnings;
604 gdb_bfd_ref_ptr debug_bfd;
605 std::string filename;
607 std::tie (debug_bfd, filename)
608 = simple_find_and_open_separate_symbol_file
609 (this, find_separate_debug_file_by_buildid, &warnings);
611 if (debug_bfd == nullptr)
612 std::tie (debug_bfd, filename)
613 = simple_find_and_open_separate_symbol_file
614 (this, find_separate_debug_file_by_debuglink, &warnings);
616 /* Only try debuginfod on the first attempt. Sure, we could imagine
617 an extension that somehow adds the required debug info to the
618 debuginfod server but, at least for now, we don't support this
619 scenario. Better for the extension to return new debug info
620 directly to GDB. Plus, going to the debuginfod server might be
621 slow, so that's a good argument for only doing this once. */
622 if (debug_bfd == nullptr && attempt == 0)
623 std::tie (debug_bfd, filename)
624 = debuginfod_find_and_open_separate_symbol_file (this);
626 if (debug_bfd != nullptr)
628 /* We found a separate debug info symbol file. If this is our
629 first attempt then setting HAS_DWARF2 will cause us to break
630 from the attempt loop. */
631 symbol_file_add_separate (debug_bfd, filename.c_str (),
632 symfile_flags, this);
633 has_dwarf2 = true;
635 else if (attempt == 0)
637 /* Failed to find a separate debug info symbol file. Call out to
638 the extension languages. The user might have registered an
639 extension that can find the debug info for us, or maybe give
640 the user a system specific message that guides them to finding
641 the missing debug info. */
643 ext_lang_missing_debuginfo_result ext_result
644 = ext_lang_handle_missing_debuginfo (this);
645 if (!ext_result.filename ().empty ())
647 /* Extension found a suitable debug file for us. */
648 debug_bfd
649 = symfile_bfd_open_no_error (ext_result.filename ().c_str ());
651 if (debug_bfd != nullptr)
653 symbol_file_add_separate (debug_bfd,
654 ext_result.filename ().c_str (),
655 symfile_flags, this);
656 has_dwarf2 = true;
659 else if (ext_result.try_again ())
661 max_attempts = 2;
662 continue;
666 /* If we still have not got a separate debug symbol file, then
667 emit any warnings we've collected so far. */
668 if (!has_dwarf2)
669 warnings.emit ();
672 return has_dwarf2;
676 /* Debugging version of struct sym_probe_fns. */
678 static const std::vector<std::unique_ptr<probe>> &
679 debug_sym_get_probes (struct objfile *objfile)
681 const struct debug_sym_fns_data *debug_data
682 = symfile_debug_objfile_data_key.get (objfile);
684 const std::vector<std::unique_ptr<probe>> &retval
685 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
687 gdb_printf (gdb_stdlog,
688 "probes->sym_get_probes (%s) = %s\n",
689 objfile_debug_name (objfile),
690 host_address_to_string (retval.data ()));
692 return retval;
695 static const struct sym_probe_fns debug_sym_probe_fns =
697 debug_sym_get_probes,
700 /* Debugging version of struct sym_fns. */
702 static void
703 debug_sym_new_init (struct objfile *objfile)
705 const struct debug_sym_fns_data *debug_data
706 = symfile_debug_objfile_data_key.get (objfile);
708 gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
709 objfile_debug_name (objfile));
711 debug_data->real_sf->sym_new_init (objfile);
714 static void
715 debug_sym_init (struct objfile *objfile)
717 const struct debug_sym_fns_data *debug_data
718 = symfile_debug_objfile_data_key.get (objfile);
720 gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
721 objfile_debug_name (objfile));
723 debug_data->real_sf->sym_init (objfile);
726 static void
727 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
729 const struct debug_sym_fns_data *debug_data
730 = symfile_debug_objfile_data_key.get (objfile);
732 gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
733 objfile_debug_name (objfile), (unsigned) symfile_flags);
735 debug_data->real_sf->sym_read (objfile, symfile_flags);
738 static void
739 debug_sym_finish (struct objfile *objfile)
741 const struct debug_sym_fns_data *debug_data
742 = symfile_debug_objfile_data_key.get (objfile);
744 gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
745 objfile_debug_name (objfile));
747 debug_data->real_sf->sym_finish (objfile);
750 static void
751 debug_sym_offsets (struct objfile *objfile,
752 const section_addr_info &info)
754 const struct debug_sym_fns_data *debug_data
755 = symfile_debug_objfile_data_key.get (objfile);
757 gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
758 objfile_debug_name (objfile),
759 host_address_to_string (&info));
761 debug_data->real_sf->sym_offsets (objfile, info);
764 static symfile_segment_data_up
765 debug_sym_segments (bfd *abfd)
767 /* This API function is annoying, it doesn't take a "this" pointer.
768 Fortunately it is only used in one place where we (re-)lookup the
769 sym_fns table to use. Thus we will never be called. */
770 gdb_assert_not_reached ("debug_sym_segments called");
773 static void
774 debug_sym_read_linetable (struct objfile *objfile)
776 const struct debug_sym_fns_data *debug_data
777 = symfile_debug_objfile_data_key.get (objfile);
779 gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
780 objfile_debug_name (objfile));
782 debug_data->real_sf->sym_read_linetable (objfile);
785 static bfd_byte *
786 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
788 const struct debug_sym_fns_data *debug_data
789 = symfile_debug_objfile_data_key.get (objfile);
790 bfd_byte *retval;
792 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
794 gdb_printf (gdb_stdlog,
795 "sf->sym_relocate (%s, %s, %s) = %s\n",
796 objfile_debug_name (objfile),
797 host_address_to_string (sectp),
798 host_address_to_string (buf),
799 host_address_to_string (retval));
801 return retval;
804 /* Template of debugging version of struct sym_fns.
805 A copy is made, with sym_flavour updated, and a pointer to the real table
806 installed in real_sf, and then a pointer to the copy is installed in the
807 objfile. */
809 static const struct sym_fns debug_sym_fns =
811 debug_sym_new_init,
812 debug_sym_init,
813 debug_sym_read,
814 debug_sym_finish,
815 debug_sym_offsets,
816 debug_sym_segments,
817 debug_sym_read_linetable,
818 debug_sym_relocate,
819 &debug_sym_probe_fns,
822 /* Install the debugging versions of the symfile functions for OBJFILE.
823 Do not call this if the debug versions are already installed. */
825 static void
826 install_symfile_debug_logging (struct objfile *objfile)
828 const struct sym_fns *real_sf;
829 struct debug_sym_fns_data *debug_data;
831 /* The debug versions should not already be installed. */
832 gdb_assert (!symfile_debug_installed (objfile));
834 real_sf = objfile->sf;
836 /* Alas we have to preserve NULL entries in REAL_SF. */
837 debug_data = new struct debug_sym_fns_data;
839 #define COPY_SF_PTR(from, to, name, func) \
840 do { \
841 if ((from)->name) \
842 (to)->debug_sf.name = func; \
843 } while (0)
845 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
846 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
847 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
848 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
849 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
850 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
851 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
852 debug_sym_read_linetable);
853 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
854 if (real_sf->sym_probe_fns)
855 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
857 #undef COPY_SF_PTR
859 debug_data->real_sf = real_sf;
860 symfile_debug_objfile_data_key.set (objfile, debug_data);
861 objfile->sf = &debug_data->debug_sf;
864 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
865 Do not call this if the debug versions are not installed. */
867 static void
868 uninstall_symfile_debug_logging (struct objfile *objfile)
870 struct debug_sym_fns_data *debug_data;
872 /* The debug versions should be currently installed. */
873 gdb_assert (symfile_debug_installed (objfile));
875 debug_data = symfile_debug_objfile_data_key.get (objfile);
877 objfile->sf = debug_data->real_sf;
878 symfile_debug_objfile_data_key.clear (objfile);
881 /* Call this function to set OBJFILE->SF.
882 Do not set OBJFILE->SF directly. */
884 void
885 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
887 if (symfile_debug_installed (objfile))
889 gdb_assert (debug_symfile);
890 /* Remove the current one, and reinstall a new one later. */
891 uninstall_symfile_debug_logging (objfile);
894 /* Assume debug logging is disabled. */
895 objfile->sf = sf;
897 /* Turn debug logging on if enabled. */
898 if (debug_symfile)
899 install_symfile_debug_logging (objfile);
902 static void
903 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
905 for (struct program_space *pspace : program_spaces)
906 for (objfile *objfile : pspace->objfiles ())
908 if (debug_symfile)
910 if (!symfile_debug_installed (objfile))
911 install_symfile_debug_logging (objfile);
913 else
915 if (symfile_debug_installed (objfile))
916 uninstall_symfile_debug_logging (objfile);
921 static void
922 show_debug_symfile (struct ui_file *file, int from_tty,
923 struct cmd_list_element *c, const char *value)
925 gdb_printf (file, _("Symfile debugging is %s.\n"), value);
928 void _initialize_symfile_debug ();
929 void
930 _initialize_symfile_debug ()
932 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
933 Set debugging of the symfile functions."), _("\
934 Show debugging of the symfile functions."), _("\
935 When enabled, all calls to the symfile functions are logged."),
936 set_debug_symfile, show_debug_symfile,
937 &setdebuglist, &showdebuglist);
939 /* Note: We don't need a new-objfile observer because debug logging
940 will be installed when objfile init'n calls objfile_set_sym_fns. */