Fix null pointer dereference in process_debug_info()
[binutils-gdb.git] / gdb / symfile-debug.c
blobbc70447600eea6fbd77cecf0b64538c8bad3bbaa
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 "gdbcmd.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 ())
158 for (symtab *s : cu->filetabs ())
160 if (s->fullname != NULL)
162 xfree (s->fullname);
163 s->fullname = NULL;
168 for (const auto &iter : qf)
169 iter->forget_cached_source_info (this);
172 bool
173 objfile::map_symtabs_matching_filename
174 (const char *name, const char *real_path,
175 gdb::function_view<bool (symtab *)> callback)
177 if (debug_symfile)
178 gdb_printf (gdb_stdlog,
179 "qf->map_symtabs_matching_filename (%s, \"%s\", "
180 "\"%s\", %s)\n",
181 objfile_debug_name (this), name,
182 real_path ? real_path : NULL,
183 host_address_to_string (&callback));
185 bool retval = true;
186 const char *name_basename = lbasename (name);
188 auto match_one_filename = [&] (const char *filename, bool basenames)
190 if (compare_filenames_for_search (filename, name))
191 return true;
192 if (basenames && FILENAME_CMP (name_basename, filename) == 0)
193 return true;
194 if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
195 && IS_ABSOLUTE_PATH (real_path))
196 return filename_cmp (filename, real_path) == 0;
197 return false;
200 compunit_symtab *last_made = this->compunit_symtabs;
202 auto on_expansion = [&] (compunit_symtab *symtab)
204 /* The callback to iterate_over_some_symtabs returns false to keep
205 going and true to continue, so we have to invert the result
206 here, for expand_symtabs_matching. */
207 bool result = !iterate_over_some_symtabs (name, real_path,
208 this->compunit_symtabs,
209 last_made,
210 callback);
211 last_made = this->compunit_symtabs;
212 return result;
215 for (const auto &iter : qf)
217 if (!iter->expand_symtabs_matching (this,
218 match_one_filename,
219 nullptr,
220 nullptr,
221 on_expansion,
222 (SEARCH_GLOBAL_BLOCK
223 | SEARCH_STATIC_BLOCK),
224 SEARCH_ALL_DOMAINS))
226 retval = false;
227 break;
231 if (debug_symfile)
232 gdb_printf (gdb_stdlog,
233 "qf->map_symtabs_matching_filename (...) = %d\n",
234 retval);
236 /* We must re-invert the return value here to match the caller's
237 expectations. */
238 return !retval;
241 struct compunit_symtab *
242 objfile::lookup_symbol (block_enum kind, const lookup_name_info &name,
243 domain_search_flags domain)
245 struct compunit_symtab *retval = nullptr;
247 if (debug_symfile)
248 gdb_printf (gdb_stdlog,
249 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
250 objfile_debug_name (this), kind, name.c_str (),
251 domain_name (domain).c_str ());
253 auto search_one_symtab = [&] (compunit_symtab *stab)
255 struct symbol *sym, *with_opaque = NULL;
256 const struct blockvector *bv = stab->blockvector ();
257 const struct block *block = bv->block (kind);
259 sym = block_find_symbol (block, name, domain, &with_opaque);
261 /* Some caution must be observed with overloaded functions
262 and methods, since the index will not contain any overload
263 information (but NAME might contain it). */
265 if (sym != nullptr)
267 retval = stab;
268 /* Found it. */
269 return false;
271 if (with_opaque != nullptr)
272 retval = stab;
274 /* Keep looking through other psymtabs. */
275 return true;
278 for (const auto &iter : qf)
280 if (!iter->expand_symtabs_matching (this,
281 nullptr,
282 &name,
283 nullptr,
284 search_one_symtab,
285 kind == GLOBAL_BLOCK
286 ? SEARCH_GLOBAL_BLOCK
287 : SEARCH_STATIC_BLOCK,
288 domain))
289 break;
292 if (debug_symfile)
293 gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
294 retval
295 ? debug_symtab_name (retval->primary_filetab ())
296 : "NULL");
298 return retval;
301 void
302 objfile::print_stats (bool print_bcache)
304 if (debug_symfile)
305 gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
306 objfile_debug_name (this), print_bcache);
308 for (const auto &iter : qf)
309 iter->print_stats (this, print_bcache);
312 void
313 objfile::dump ()
315 if (debug_symfile)
316 gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
317 objfile_debug_name (this));
319 for (const auto &iter : qf)
320 iter->dump (this);
323 void
324 objfile::expand_symtabs_for_function (const char *func_name)
326 if (debug_symfile)
327 gdb_printf (gdb_stdlog,
328 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
329 objfile_debug_name (this), func_name);
331 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
332 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
334 for (const auto &iter : qf)
335 iter->expand_symtabs_matching (this,
336 nullptr,
337 &lookup_name,
338 nullptr,
339 nullptr,
340 (SEARCH_GLOBAL_BLOCK
341 | SEARCH_STATIC_BLOCK),
342 SEARCH_FUNCTION_DOMAIN);
345 void
346 objfile::expand_all_symtabs ()
348 if (debug_symfile)
349 gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
350 objfile_debug_name (this));
352 for (const auto &iter : qf)
353 iter->expand_all_symtabs (this);
356 void
357 objfile::expand_symtabs_with_fullname (const char *fullname)
359 if (debug_symfile)
360 gdb_printf (gdb_stdlog,
361 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
362 objfile_debug_name (this), fullname);
364 const char *basename = lbasename (fullname);
365 auto file_matcher = [&] (const char *filename, bool basenames)
367 return filename_cmp (basenames ? basename : fullname, filename) == 0;
370 for (const auto &iter : qf)
371 iter->expand_symtabs_matching (this,
372 file_matcher,
373 nullptr,
374 nullptr,
375 nullptr,
376 (SEARCH_GLOBAL_BLOCK
377 | SEARCH_STATIC_BLOCK),
378 SEARCH_ALL_DOMAINS);
381 bool
382 objfile::expand_symtabs_matching
383 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
384 const lookup_name_info *lookup_name,
385 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
386 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
387 block_search_flags search_flags,
388 domain_search_flags domain)
390 /* This invariant is documented in quick-functions.h. */
391 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
393 if (debug_symfile)
394 gdb_printf (gdb_stdlog,
395 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
396 objfile_debug_name (this),
397 host_address_to_string (&file_matcher),
398 host_address_to_string (&symbol_matcher),
399 host_address_to_string (&expansion_notify),
400 domain_name (domain).c_str ());
402 for (const auto &iter : qf)
403 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
404 symbol_matcher, expansion_notify,
405 search_flags, domain))
406 return false;
407 return true;
410 struct compunit_symtab *
411 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
412 CORE_ADDR pc,
413 struct obj_section *section,
414 int warn_if_readin)
416 struct compunit_symtab *retval = nullptr;
418 if (debug_symfile)
419 gdb_printf (gdb_stdlog,
420 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
421 objfile_debug_name (this),
422 host_address_to_string (msymbol.minsym),
423 hex_string (pc),
424 host_address_to_string (section),
425 warn_if_readin);
427 for (const auto &iter : qf)
429 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
430 warn_if_readin);
431 if (retval != nullptr)
432 break;
435 if (debug_symfile)
436 gdb_printf (gdb_stdlog,
437 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
438 retval
439 ? debug_symtab_name (retval->primary_filetab ())
440 : "NULL");
442 return retval;
445 void
446 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
447 bool need_fullname)
449 if (debug_symfile)
450 gdb_printf (gdb_stdlog,
451 "qf->map_symbol_filenames (%s, ..., %d)\n",
452 objfile_debug_name (this),
453 need_fullname);
455 for (const auto &iter : qf)
456 iter->map_symbol_filenames (this, fun, need_fullname);
459 void
460 objfile::compute_main_name ()
462 if (debug_symfile)
463 gdb_printf (gdb_stdlog,
464 "qf->compute_main_name (%s)\n",
465 objfile_debug_name (this));
467 for (const auto &iter : qf)
468 iter->compute_main_name (this);
471 struct compunit_symtab *
472 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
474 if (debug_symfile)
475 gdb_printf (gdb_stdlog,
476 "qf->find_compunit_symtab_by_address (%s, %s)\n",
477 objfile_debug_name (this),
478 hex_string (address));
480 struct compunit_symtab *result = NULL;
481 for (const auto &iter : qf)
483 result = iter->find_compunit_symtab_by_address (this, address);
484 if (result != nullptr)
485 break;
488 if (debug_symfile)
489 gdb_printf (gdb_stdlog,
490 "qf->find_compunit_symtab_by_address (...) = %s\n",
491 result
492 ? debug_symtab_name (result->primary_filetab ())
493 : "NULL");
495 return result;
498 enum language
499 objfile::lookup_global_symbol_language (const char *name,
500 domain_search_flags domain,
501 bool *symbol_found_p)
503 enum language result = language_unknown;
504 *symbol_found_p = false;
506 for (const auto &iter : qf)
508 result = iter->lookup_global_symbol_language (this, name, domain,
509 symbol_found_p);
510 if (*symbol_found_p)
511 break;
514 return result;
517 /* Call LOOKUP_FUNC to find the filename of a file containing the separate
518 debug information matching OBJFILE. If LOOKUP_FUNC does return a
519 filename then open this file and return a std::pair containing the
520 gdb_bfd_ref_ptr of the open file and the filename returned by
521 LOOKUP_FUNC, otherwise this function returns an empty pair; the first
522 item will be nullptr, and the second will be an empty string.
524 Any warnings generated by this function, or by calling LOOKUP_FUNC are
525 placed into WARNINGS, these warnings are only displayed to the user if
526 GDB is unable to find the separate debug information via any route. */
527 static std::pair<gdb_bfd_ref_ptr, std::string>
528 simple_find_and_open_separate_symbol_file
529 (struct objfile *objfile,
530 std::string (*lookup_func) (struct objfile *, deferred_warnings *),
531 deferred_warnings *warnings)
533 std::string filename = lookup_func (objfile, warnings);
535 if (!filename.empty ())
537 gdb_bfd_ref_ptr symfile_bfd
538 = symfile_bfd_open_no_error (filename.c_str ());
539 if (symfile_bfd != nullptr)
540 return { symfile_bfd, filename };
543 return {};
546 /* Lookup separate debug information for OBJFILE via debuginfod. If
547 successful the debug information will be have been downloaded into the
548 debuginfod cache and this function will return a std::pair containing a
549 gdb_bfd_ref_ptr of the open debug information file and the filename for
550 the file within the debuginfod cache. If no debug information could be
551 found then this function returns an empty pair; the first item will be
552 nullptr, and the second will be an empty string. */
554 static std::pair<gdb_bfd_ref_ptr, std::string>
555 debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
557 const struct bfd_build_id *build_id
558 = build_id_bfd_get (objfile->obfd.get ());
559 const char *filename = bfd_get_filename (objfile->obfd.get ());
561 if (build_id != nullptr)
563 gdb::unique_xmalloc_ptr<char> symfile_path;
564 scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size,
565 filename, &symfile_path));
567 if (fd.get () >= 0)
569 /* File successfully retrieved from server. */
570 gdb_bfd_ref_ptr debug_bfd
571 (symfile_bfd_open_no_error (symfile_path.get ()));
573 if (debug_bfd != nullptr
574 && build_id_verify (debug_bfd.get (),
575 build_id->size, build_id->data))
576 return { debug_bfd, std::string (symfile_path.get ()) };
580 return {};
583 /* See objfiles.h. */
585 bool
586 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
588 bool has_dwarf2 = false;
590 /* Usually we only make a single pass when looking for separate debug
591 information. However, it is possible for an extension language hook
592 to request that GDB make a second pass, in which case max_attempts
593 will be updated, and the loop restarted. */
594 for (unsigned attempt = 0, max_attempts = 1;
595 attempt < max_attempts && !has_dwarf2;
596 ++attempt)
598 gdb_assert (max_attempts <= 2);
600 deferred_warnings warnings;
601 gdb_bfd_ref_ptr debug_bfd;
602 std::string filename;
604 std::tie (debug_bfd, filename)
605 = simple_find_and_open_separate_symbol_file
606 (this, find_separate_debug_file_by_buildid, &warnings);
608 if (debug_bfd == nullptr)
609 std::tie (debug_bfd, filename)
610 = simple_find_and_open_separate_symbol_file
611 (this, find_separate_debug_file_by_debuglink, &warnings);
613 /* Only try debuginfod on the first attempt. Sure, we could imagine
614 an extension that somehow adds the required debug info to the
615 debuginfod server but, at least for now, we don't support this
616 scenario. Better for the extension to return new debug info
617 directly to GDB. Plus, going to the debuginfod server might be
618 slow, so that's a good argument for only doing this once. */
619 if (debug_bfd == nullptr && attempt == 0)
620 std::tie (debug_bfd, filename)
621 = debuginfod_find_and_open_separate_symbol_file (this);
623 if (debug_bfd != nullptr)
625 /* We found a separate debug info symbol file. If this is our
626 first attempt then setting HAS_DWARF2 will cause us to break
627 from the attempt loop. */
628 symbol_file_add_separate (debug_bfd, filename.c_str (),
629 symfile_flags, this);
630 has_dwarf2 = true;
632 else if (attempt == 0)
634 /* Failed to find a separate debug info symbol file. Call out to
635 the extension languages. The user might have registered an
636 extension that can find the debug info for us, or maybe give
637 the user a system specific message that guides them to finding
638 the missing debug info. */
640 ext_lang_missing_debuginfo_result ext_result
641 = ext_lang_handle_missing_debuginfo (this);
642 if (!ext_result.filename ().empty ())
644 /* Extension found a suitable debug file for us. */
645 debug_bfd
646 = symfile_bfd_open_no_error (ext_result.filename ().c_str ());
648 if (debug_bfd != nullptr)
650 symbol_file_add_separate (debug_bfd,
651 ext_result.filename ().c_str (),
652 symfile_flags, this);
653 has_dwarf2 = true;
656 else if (ext_result.try_again ())
658 max_attempts = 2;
659 continue;
663 /* If we still have not got a separate debug symbol file, then
664 emit any warnings we've collected so far. */
665 if (!has_dwarf2)
666 warnings.emit ();
669 return has_dwarf2;
673 /* Debugging version of struct sym_probe_fns. */
675 static const std::vector<std::unique_ptr<probe>> &
676 debug_sym_get_probes (struct objfile *objfile)
678 const struct debug_sym_fns_data *debug_data
679 = symfile_debug_objfile_data_key.get (objfile);
681 const std::vector<std::unique_ptr<probe>> &retval
682 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
684 gdb_printf (gdb_stdlog,
685 "probes->sym_get_probes (%s) = %s\n",
686 objfile_debug_name (objfile),
687 host_address_to_string (retval.data ()));
689 return retval;
692 static const struct sym_probe_fns debug_sym_probe_fns =
694 debug_sym_get_probes,
697 /* Debugging version of struct sym_fns. */
699 static void
700 debug_sym_new_init (struct objfile *objfile)
702 const struct debug_sym_fns_data *debug_data
703 = symfile_debug_objfile_data_key.get (objfile);
705 gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
706 objfile_debug_name (objfile));
708 debug_data->real_sf->sym_new_init (objfile);
711 static void
712 debug_sym_init (struct objfile *objfile)
714 const struct debug_sym_fns_data *debug_data
715 = symfile_debug_objfile_data_key.get (objfile);
717 gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
718 objfile_debug_name (objfile));
720 debug_data->real_sf->sym_init (objfile);
723 static void
724 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
726 const struct debug_sym_fns_data *debug_data
727 = symfile_debug_objfile_data_key.get (objfile);
729 gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
730 objfile_debug_name (objfile), (unsigned) symfile_flags);
732 debug_data->real_sf->sym_read (objfile, symfile_flags);
735 static void
736 debug_sym_finish (struct objfile *objfile)
738 const struct debug_sym_fns_data *debug_data
739 = symfile_debug_objfile_data_key.get (objfile);
741 gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
742 objfile_debug_name (objfile));
744 debug_data->real_sf->sym_finish (objfile);
747 static void
748 debug_sym_offsets (struct objfile *objfile,
749 const section_addr_info &info)
751 const struct debug_sym_fns_data *debug_data
752 = symfile_debug_objfile_data_key.get (objfile);
754 gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
755 objfile_debug_name (objfile),
756 host_address_to_string (&info));
758 debug_data->real_sf->sym_offsets (objfile, info);
761 static symfile_segment_data_up
762 debug_sym_segments (bfd *abfd)
764 /* This API function is annoying, it doesn't take a "this" pointer.
765 Fortunately it is only used in one place where we (re-)lookup the
766 sym_fns table to use. Thus we will never be called. */
767 gdb_assert_not_reached ("debug_sym_segments called");
770 static void
771 debug_sym_read_linetable (struct objfile *objfile)
773 const struct debug_sym_fns_data *debug_data
774 = symfile_debug_objfile_data_key.get (objfile);
776 gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
777 objfile_debug_name (objfile));
779 debug_data->real_sf->sym_read_linetable (objfile);
782 static bfd_byte *
783 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
785 const struct debug_sym_fns_data *debug_data
786 = symfile_debug_objfile_data_key.get (objfile);
787 bfd_byte *retval;
789 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
791 gdb_printf (gdb_stdlog,
792 "sf->sym_relocate (%s, %s, %s) = %s\n",
793 objfile_debug_name (objfile),
794 host_address_to_string (sectp),
795 host_address_to_string (buf),
796 host_address_to_string (retval));
798 return retval;
801 /* Template of debugging version of struct sym_fns.
802 A copy is made, with sym_flavour updated, and a pointer to the real table
803 installed in real_sf, and then a pointer to the copy is installed in the
804 objfile. */
806 static const struct sym_fns debug_sym_fns =
808 debug_sym_new_init,
809 debug_sym_init,
810 debug_sym_read,
811 debug_sym_finish,
812 debug_sym_offsets,
813 debug_sym_segments,
814 debug_sym_read_linetable,
815 debug_sym_relocate,
816 &debug_sym_probe_fns,
819 /* Install the debugging versions of the symfile functions for OBJFILE.
820 Do not call this if the debug versions are already installed. */
822 static void
823 install_symfile_debug_logging (struct objfile *objfile)
825 const struct sym_fns *real_sf;
826 struct debug_sym_fns_data *debug_data;
828 /* The debug versions should not already be installed. */
829 gdb_assert (!symfile_debug_installed (objfile));
831 real_sf = objfile->sf;
833 /* Alas we have to preserve NULL entries in REAL_SF. */
834 debug_data = new struct debug_sym_fns_data;
836 #define COPY_SF_PTR(from, to, name, func) \
837 do { \
838 if ((from)->name) \
839 (to)->debug_sf.name = func; \
840 } while (0)
842 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
843 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
844 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
845 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
846 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
847 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
848 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
849 debug_sym_read_linetable);
850 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
851 if (real_sf->sym_probe_fns)
852 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
854 #undef COPY_SF_PTR
856 debug_data->real_sf = real_sf;
857 symfile_debug_objfile_data_key.set (objfile, debug_data);
858 objfile->sf = &debug_data->debug_sf;
861 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
862 Do not call this if the debug versions are not installed. */
864 static void
865 uninstall_symfile_debug_logging (struct objfile *objfile)
867 struct debug_sym_fns_data *debug_data;
869 /* The debug versions should be currently installed. */
870 gdb_assert (symfile_debug_installed (objfile));
872 debug_data = symfile_debug_objfile_data_key.get (objfile);
874 objfile->sf = debug_data->real_sf;
875 symfile_debug_objfile_data_key.clear (objfile);
878 /* Call this function to set OBJFILE->SF.
879 Do not set OBJFILE->SF directly. */
881 void
882 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
884 if (symfile_debug_installed (objfile))
886 gdb_assert (debug_symfile);
887 /* Remove the current one, and reinstall a new one later. */
888 uninstall_symfile_debug_logging (objfile);
891 /* Assume debug logging is disabled. */
892 objfile->sf = sf;
894 /* Turn debug logging on if enabled. */
895 if (debug_symfile)
896 install_symfile_debug_logging (objfile);
899 static void
900 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
902 for (struct program_space *pspace : program_spaces)
903 for (objfile *objfile : pspace->objfiles ())
905 if (debug_symfile)
907 if (!symfile_debug_installed (objfile))
908 install_symfile_debug_logging (objfile);
910 else
912 if (symfile_debug_installed (objfile))
913 uninstall_symfile_debug_logging (objfile);
918 static void
919 show_debug_symfile (struct ui_file *file, int from_tty,
920 struct cmd_list_element *c, const char *value)
922 gdb_printf (file, _("Symfile debugging is %s.\n"), value);
925 void _initialize_symfile_debug ();
926 void
927 _initialize_symfile_debug ()
929 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
930 Set debugging of the symfile functions."), _("\
931 Show debugging of the symfile functions."), _("\
932 When enabled, all calls to the symfile functions are logged."),
933 set_debug_symfile, show_debug_symfile,
934 &setdebuglist, &showdebuglist);
936 /* Note: We don't need a new-objfile observer because debug logging
937 will be installed when objfile init'n calls objfile_set_sym_fns. */