]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/symfile-debug.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / symfile-debug.c
1 /* Debug logging for the symbol file functions for the GNU debugger, GDB.
2
3 Copyright (C) 2013-2024 Free Software Foundation, Inc.
4
5 Contributed by Cygnus Support, using pieces from other GDB modules.
6
7 This file is part of GDB.
8
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.
13
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.
18
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/>. */
21
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. */
27
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"
40
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. */
44
45 struct debug_sym_fns_data
46 {
47 const struct sym_fns *real_sf = nullptr;
48 struct sym_fns debug_sf {};
49 };
50
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;
55
56 /* If true all calls to the symfile functions are logged. */
57 static bool debug_symfile = false;
58
59 /* Return non-zero if symfile debug logging is installed. */
60
61 static int
62 symfile_debug_installed (struct objfile *objfile)
63 {
64 return (objfile->sf != NULL
65 && symfile_debug_objfile_data_key.get (objfile) != NULL);
66 }
67
68 /* Utility return the name to print for SYMTAB. */
69
70 static const char *
71 debug_symtab_name (struct symtab *symtab)
72 {
73 return symtab_to_filename_for_display (symtab);
74 }
75 \f
76
77 /* See objfiles.h. */
78
79 bool
80 objfile::has_partial_symbols ()
81 {
82 bool retval = false;
83
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)
89 {
90 retval = iter->has_symbols (this);
91 if (retval)
92 break;
93 }
94
95 if (debug_symfile)
96 gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
97 objfile_debug_name (this), retval);
98
99 return retval;
100 }
101
102 /* See objfiles.h. */
103 bool
104 objfile::has_unexpanded_symtabs ()
105 {
106 if (debug_symfile)
107 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
108 objfile_debug_name (this));
109
110 bool result = false;
111 for (const auto &iter : qf)
112 {
113 if (iter->has_unexpanded_symtabs (this))
114 {
115 result = true;
116 break;
117 }
118 }
119
120 if (debug_symfile)
121 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
122 objfile_debug_name (this), (result ? 1 : 0));
123
124 return result;
125 }
126
127 struct symtab *
128 objfile::find_last_source_symtab ()
129 {
130 struct symtab *retval = nullptr;
131
132 if (debug_symfile)
133 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
134 objfile_debug_name (this));
135
136 for (const auto &iter : qf)
137 {
138 retval = iter->find_last_source_symtab (this);
139 if (retval != nullptr)
140 break;
141 }
142
143 if (debug_symfile)
144 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
145 retval ? debug_symtab_name (retval) : "NULL");
146
147 return retval;
148 }
149
150 void
151 objfile::forget_cached_source_info ()
152 {
153 if (debug_symfile)
154 gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
155 objfile_debug_name (this));
156
157 for (compunit_symtab *cu : compunits ())
158 {
159 for (symtab *s : cu->filetabs ())
160 {
161 if (s->fullname != NULL)
162 {
163 xfree (s->fullname);
164 s->fullname = NULL;
165 }
166 }
167 }
168
169 for (const auto &iter : qf)
170 iter->forget_cached_source_info (this);
171 }
172
173 bool
174 objfile::map_symtabs_matching_filename
175 (const char *name, const char *real_path,
176 gdb::function_view<bool (symtab *)> callback)
177 {
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));
185
186 bool retval = true;
187 const char *name_basename = lbasename (name);
188
189 auto match_one_filename = [&] (const char *filename, bool basenames)
190 {
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;
199 };
200
201 compunit_symtab *last_made = this->compunit_symtabs;
202
203 auto on_expansion = [&] (compunit_symtab *symtab)
204 {
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;
214 };
215
216 for (const auto &iter : qf)
217 {
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 UNDEF_DOMAIN,
226 ALL_DOMAIN))
227 {
228 retval = false;
229 break;
230 }
231 }
232
233 if (debug_symfile)
234 gdb_printf (gdb_stdlog,
235 "qf->map_symtabs_matching_filename (...) = %d\n",
236 retval);
237
238 /* We must re-invert the return value here to match the caller's
239 expectations. */
240 return !retval;
241 }
242
243 struct compunit_symtab *
244 objfile::lookup_symbol (block_enum kind, const char *name, domain_enum domain)
245 {
246 struct compunit_symtab *retval = nullptr;
247
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));
253
254 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
255
256 auto search_one_symtab = [&] (compunit_symtab *stab)
257 {
258 struct symbol *sym, *with_opaque = NULL;
259 const struct blockvector *bv = stab->blockvector ();
260 const struct block *block = bv->block (kind);
261
262 sym = block_find_symbol (block, lookup_name, domain, &with_opaque);
263
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). */
267
268 if (sym != nullptr)
269 {
270 retval = stab;
271 /* Found it. */
272 return false;
273 }
274 if (with_opaque != nullptr)
275 retval = stab;
276
277 /* Keep looking through other psymtabs. */
278 return true;
279 };
280
281 for (const auto &iter : qf)
282 {
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 ALL_DOMAIN))
293 break;
294 }
295
296 if (debug_symfile)
297 gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
298 retval
299 ? debug_symtab_name (retval->primary_filetab ())
300 : "NULL");
301
302 return retval;
303 }
304
305 void
306 objfile::print_stats (bool print_bcache)
307 {
308 if (debug_symfile)
309 gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
310 objfile_debug_name (this), print_bcache);
311
312 for (const auto &iter : qf)
313 iter->print_stats (this, print_bcache);
314 }
315
316 void
317 objfile::dump ()
318 {
319 if (debug_symfile)
320 gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
321 objfile_debug_name (this));
322
323 for (const auto &iter : qf)
324 iter->dump (this);
325 }
326
327 void
328 objfile::expand_symtabs_for_function (const char *func_name)
329 {
330 if (debug_symfile)
331 gdb_printf (gdb_stdlog,
332 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
333 objfile_debug_name (this), func_name);
334
335 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
336 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
337
338 for (const auto &iter : qf)
339 iter->expand_symtabs_matching (this,
340 nullptr,
341 &lookup_name,
342 nullptr,
343 nullptr,
344 (SEARCH_GLOBAL_BLOCK
345 | SEARCH_STATIC_BLOCK),
346 VAR_DOMAIN,
347 ALL_DOMAIN);
348 }
349
350 void
351 objfile::expand_all_symtabs ()
352 {
353 if (debug_symfile)
354 gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
355 objfile_debug_name (this));
356
357 for (const auto &iter : qf)
358 iter->expand_all_symtabs (this);
359 }
360
361 void
362 objfile::expand_symtabs_with_fullname (const char *fullname)
363 {
364 if (debug_symfile)
365 gdb_printf (gdb_stdlog,
366 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
367 objfile_debug_name (this), fullname);
368
369 const char *basename = lbasename (fullname);
370 auto file_matcher = [&] (const char *filename, bool basenames)
371 {
372 return filename_cmp (basenames ? basename : fullname, filename) == 0;
373 };
374
375 for (const auto &iter : qf)
376 iter->expand_symtabs_matching (this,
377 file_matcher,
378 nullptr,
379 nullptr,
380 nullptr,
381 (SEARCH_GLOBAL_BLOCK
382 | SEARCH_STATIC_BLOCK),
383 UNDEF_DOMAIN,
384 ALL_DOMAIN);
385 }
386
387 bool
388 objfile::expand_symtabs_matching
389 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
390 const lookup_name_info *lookup_name,
391 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
392 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
393 block_search_flags search_flags,
394 domain_enum domain,
395 enum search_domain kind)
396 {
397 /* This invariant is documented in quick-functions.h. */
398 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
399
400 if (debug_symfile)
401 gdb_printf (gdb_stdlog,
402 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
403 objfile_debug_name (this),
404 host_address_to_string (&file_matcher),
405 host_address_to_string (&symbol_matcher),
406 host_address_to_string (&expansion_notify),
407 search_domain_name (kind));
408
409 for (const auto &iter : qf)
410 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
411 symbol_matcher, expansion_notify,
412 search_flags, domain, kind))
413 return false;
414 return true;
415 }
416
417 struct compunit_symtab *
418 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
419 CORE_ADDR pc,
420 struct obj_section *section,
421 int warn_if_readin)
422 {
423 struct compunit_symtab *retval = nullptr;
424
425 if (debug_symfile)
426 gdb_printf (gdb_stdlog,
427 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
428 objfile_debug_name (this),
429 host_address_to_string (msymbol.minsym),
430 hex_string (pc),
431 host_address_to_string (section),
432 warn_if_readin);
433
434 for (const auto &iter : qf)
435 {
436 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
437 warn_if_readin);
438 if (retval != nullptr)
439 break;
440 }
441
442 if (debug_symfile)
443 gdb_printf (gdb_stdlog,
444 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
445 retval
446 ? debug_symtab_name (retval->primary_filetab ())
447 : "NULL");
448
449 return retval;
450 }
451
452 void
453 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
454 bool need_fullname)
455 {
456 if (debug_symfile)
457 gdb_printf (gdb_stdlog,
458 "qf->map_symbol_filenames (%s, ..., %d)\n",
459 objfile_debug_name (this),
460 need_fullname);
461
462 for (const auto &iter : qf)
463 iter->map_symbol_filenames (this, fun, need_fullname);
464 }
465
466 void
467 objfile::compute_main_name ()
468 {
469 if (debug_symfile)
470 gdb_printf (gdb_stdlog,
471 "qf->compute_main_name (%s)\n",
472 objfile_debug_name (this));
473
474 for (const auto &iter : qf)
475 iter->compute_main_name (this);
476 }
477
478 struct compunit_symtab *
479 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
480 {
481 if (debug_symfile)
482 gdb_printf (gdb_stdlog,
483 "qf->find_compunit_symtab_by_address (%s, %s)\n",
484 objfile_debug_name (this),
485 hex_string (address));
486
487 struct compunit_symtab *result = NULL;
488 for (const auto &iter : qf)
489 {
490 result = iter->find_compunit_symtab_by_address (this, address);
491 if (result != nullptr)
492 break;
493 }
494
495 if (debug_symfile)
496 gdb_printf (gdb_stdlog,
497 "qf->find_compunit_symtab_by_address (...) = %s\n",
498 result
499 ? debug_symtab_name (result->primary_filetab ())
500 : "NULL");
501
502 return result;
503 }
504
505 enum language
506 objfile::lookup_global_symbol_language (const char *name,
507 domain_enum domain,
508 bool *symbol_found_p)
509 {
510 enum language result = language_unknown;
511 *symbol_found_p = false;
512
513 for (const auto &iter : qf)
514 {
515 result = iter->lookup_global_symbol_language (this, name, domain,
516 symbol_found_p);
517 if (*symbol_found_p)
518 break;
519 }
520
521 return result;
522 }
523
524 /* Call LOOKUP_FUNC to find the filename of a file containing the separate
525 debug information matching OBJFILE. If LOOKUP_FUNC does return a
526 filename then open this file and return a std::pair containing the
527 gdb_bfd_ref_ptr of the open file and the filename returned by
528 LOOKUP_FUNC, otherwise this function returns an empty pair; the first
529 item will be nullptr, and the second will be an empty string.
530
531 Any warnings generated by this function, or by calling LOOKUP_FUNC are
532 placed into WARNINGS, these warnings are only displayed to the user if
533 GDB is unable to find the separate debug information via any route. */
534 static std::pair<gdb_bfd_ref_ptr, std::string>
535 simple_find_and_open_separate_symbol_file
536 (struct objfile *objfile,
537 std::string (*lookup_func) (struct objfile *, deferred_warnings *),
538 deferred_warnings *warnings)
539 {
540 std::string filename = lookup_func (objfile, warnings);
541
542 if (!filename.empty ())
543 {
544 gdb_bfd_ref_ptr symfile_bfd
545 = symfile_bfd_open_no_error (filename.c_str ());
546 if (symfile_bfd != nullptr)
547 return { symfile_bfd, filename };
548 }
549
550 return {};
551 }
552
553 /* Lookup separate debug information for OBJFILE via debuginfod. If
554 successful the debug information will be have been downloaded into the
555 debuginfod cache and this function will return a std::pair containing a
556 gdb_bfd_ref_ptr of the open debug information file and the filename for
557 the file within the debuginfod cache. If no debug information could be
558 found then this function returns an empty pair; the first item will be
559 nullptr, and the second will be an empty string. */
560
561 static std::pair<gdb_bfd_ref_ptr, std::string>
562 debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
563 {
564 const struct bfd_build_id *build_id
565 = build_id_bfd_get (objfile->obfd.get ());
566 const char *filename = bfd_get_filename (objfile->obfd.get ());
567
568 if (build_id != nullptr)
569 {
570 gdb::unique_xmalloc_ptr<char> symfile_path;
571 scoped_fd fd (debuginfod_debuginfo_query (build_id->data, build_id->size,
572 filename, &symfile_path));
573
574 if (fd.get () >= 0)
575 {
576 /* File successfully retrieved from server. */
577 gdb_bfd_ref_ptr debug_bfd
578 (symfile_bfd_open_no_error (symfile_path.get ()));
579
580 if (debug_bfd != nullptr
581 && build_id_verify (debug_bfd.get (),
582 build_id->size, build_id->data))
583 return { debug_bfd, std::string (symfile_path.get ()) };
584 }
585 }
586
587 return {};
588 }
589
590 /* See objfiles.h. */
591
592 bool
593 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
594 {
595 bool has_dwarf2 = false;
596
597 /* Usually we only make a single pass when looking for separate debug
598 information. However, it is possible for an extension language hook
599 to request that GDB make a second pass, in which case max_attempts
600 will be updated, and the loop restarted. */
601 for (unsigned attempt = 0, max_attempts = 1;
602 attempt < max_attempts && !has_dwarf2;
603 ++attempt)
604 {
605 gdb_assert (max_attempts <= 2);
606
607 deferred_warnings warnings;
608 gdb_bfd_ref_ptr debug_bfd;
609 std::string filename;
610
611 std::tie (debug_bfd, filename)
612 = simple_find_and_open_separate_symbol_file
613 (this, find_separate_debug_file_by_buildid, &warnings);
614
615 if (debug_bfd == nullptr)
616 std::tie (debug_bfd, filename)
617 = simple_find_and_open_separate_symbol_file
618 (this, find_separate_debug_file_by_debuglink, &warnings);
619
620 /* Only try debuginfod on the first attempt. Sure, we could imagine
621 an extension that somehow adds the required debug info to the
622 debuginfod server but, at least for now, we don't support this
623 scenario. Better for the extension to return new debug info
624 directly to GDB. Plus, going to the debuginfod server might be
625 slow, so that's a good argument for only doing this once. */
626 if (debug_bfd == nullptr && attempt == 0)
627 std::tie (debug_bfd, filename)
628 = debuginfod_find_and_open_separate_symbol_file (this);
629
630 if (debug_bfd != nullptr)
631 {
632 /* We found a separate debug info symbol file. If this is our
633 first attempt then setting HAS_DWARF2 will cause us to break
634 from the attempt loop. */
635 symbol_file_add_separate (debug_bfd, filename.c_str (),
636 symfile_flags, this);
637 has_dwarf2 = true;
638 }
639 else if (attempt == 0)
640 {
641 /* Failed to find a separate debug info symbol file. Call out to
642 the extension languages. The user might have registered an
643 extension that can find the debug info for us, or maybe give
644 the user a system specific message that guides them to finding
645 the missing debug info. */
646
647 ext_lang_missing_debuginfo_result ext_result
648 = ext_lang_handle_missing_debuginfo (this);
649 if (!ext_result.filename ().empty ())
650 {
651 /* Extension found a suitable debug file for us. */
652 debug_bfd
653 = symfile_bfd_open_no_error (ext_result.filename ().c_str ());
654
655 if (debug_bfd != nullptr)
656 {
657 symbol_file_add_separate (debug_bfd,
658 ext_result.filename ().c_str (),
659 symfile_flags, this);
660 has_dwarf2 = true;
661 }
662 }
663 else if (ext_result.try_again ())
664 {
665 max_attempts = 2;
666 continue;
667 }
668 }
669
670 /* If we still have not got a separate debug symbol file, then
671 emit any warnings we've collected so far. */
672 if (!has_dwarf2)
673 warnings.emit ();
674 }
675
676 return has_dwarf2;
677 }
678
679 \f
680 /* Debugging version of struct sym_probe_fns. */
681
682 static const std::vector<std::unique_ptr<probe>> &
683 debug_sym_get_probes (struct objfile *objfile)
684 {
685 const struct debug_sym_fns_data *debug_data
686 = symfile_debug_objfile_data_key.get (objfile);
687
688 const std::vector<std::unique_ptr<probe>> &retval
689 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
690
691 gdb_printf (gdb_stdlog,
692 "probes->sym_get_probes (%s) = %s\n",
693 objfile_debug_name (objfile),
694 host_address_to_string (retval.data ()));
695
696 return retval;
697 }
698
699 static const struct sym_probe_fns debug_sym_probe_fns =
700 {
701 debug_sym_get_probes,
702 };
703 \f
704 /* Debugging version of struct sym_fns. */
705
706 static void
707 debug_sym_new_init (struct objfile *objfile)
708 {
709 const struct debug_sym_fns_data *debug_data
710 = symfile_debug_objfile_data_key.get (objfile);
711
712 gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
713 objfile_debug_name (objfile));
714
715 debug_data->real_sf->sym_new_init (objfile);
716 }
717
718 static void
719 debug_sym_init (struct objfile *objfile)
720 {
721 const struct debug_sym_fns_data *debug_data
722 = symfile_debug_objfile_data_key.get (objfile);
723
724 gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
725 objfile_debug_name (objfile));
726
727 debug_data->real_sf->sym_init (objfile);
728 }
729
730 static void
731 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
732 {
733 const struct debug_sym_fns_data *debug_data
734 = symfile_debug_objfile_data_key.get (objfile);
735
736 gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
737 objfile_debug_name (objfile), (unsigned) symfile_flags);
738
739 debug_data->real_sf->sym_read (objfile, symfile_flags);
740 }
741
742 static void
743 debug_sym_finish (struct objfile *objfile)
744 {
745 const struct debug_sym_fns_data *debug_data
746 = symfile_debug_objfile_data_key.get (objfile);
747
748 gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
749 objfile_debug_name (objfile));
750
751 debug_data->real_sf->sym_finish (objfile);
752 }
753
754 static void
755 debug_sym_offsets (struct objfile *objfile,
756 const section_addr_info &info)
757 {
758 const struct debug_sym_fns_data *debug_data
759 = symfile_debug_objfile_data_key.get (objfile);
760
761 gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
762 objfile_debug_name (objfile),
763 host_address_to_string (&info));
764
765 debug_data->real_sf->sym_offsets (objfile, info);
766 }
767
768 static symfile_segment_data_up
769 debug_sym_segments (bfd *abfd)
770 {
771 /* This API function is annoying, it doesn't take a "this" pointer.
772 Fortunately it is only used in one place where we (re-)lookup the
773 sym_fns table to use. Thus we will never be called. */
774 gdb_assert_not_reached ("debug_sym_segments called");
775 }
776
777 static void
778 debug_sym_read_linetable (struct objfile *objfile)
779 {
780 const struct debug_sym_fns_data *debug_data
781 = symfile_debug_objfile_data_key.get (objfile);
782
783 gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
784 objfile_debug_name (objfile));
785
786 debug_data->real_sf->sym_read_linetable (objfile);
787 }
788
789 static bfd_byte *
790 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
791 {
792 const struct debug_sym_fns_data *debug_data
793 = symfile_debug_objfile_data_key.get (objfile);
794 bfd_byte *retval;
795
796 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
797
798 gdb_printf (gdb_stdlog,
799 "sf->sym_relocate (%s, %s, %s) = %s\n",
800 objfile_debug_name (objfile),
801 host_address_to_string (sectp),
802 host_address_to_string (buf),
803 host_address_to_string (retval));
804
805 return retval;
806 }
807
808 /* Template of debugging version of struct sym_fns.
809 A copy is made, with sym_flavour updated, and a pointer to the real table
810 installed in real_sf, and then a pointer to the copy is installed in the
811 objfile. */
812
813 static const struct sym_fns debug_sym_fns =
814 {
815 debug_sym_new_init,
816 debug_sym_init,
817 debug_sym_read,
818 debug_sym_finish,
819 debug_sym_offsets,
820 debug_sym_segments,
821 debug_sym_read_linetable,
822 debug_sym_relocate,
823 &debug_sym_probe_fns,
824 };
825 \f
826 /* Install the debugging versions of the symfile functions for OBJFILE.
827 Do not call this if the debug versions are already installed. */
828
829 static void
830 install_symfile_debug_logging (struct objfile *objfile)
831 {
832 const struct sym_fns *real_sf;
833 struct debug_sym_fns_data *debug_data;
834
835 /* The debug versions should not already be installed. */
836 gdb_assert (!symfile_debug_installed (objfile));
837
838 real_sf = objfile->sf;
839
840 /* Alas we have to preserve NULL entries in REAL_SF. */
841 debug_data = new struct debug_sym_fns_data;
842
843 #define COPY_SF_PTR(from, to, name, func) \
844 do { \
845 if ((from)->name) \
846 (to)->debug_sf.name = func; \
847 } while (0)
848
849 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
850 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
851 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
852 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
853 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
854 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
855 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
856 debug_sym_read_linetable);
857 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
858 if (real_sf->sym_probe_fns)
859 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
860
861 #undef COPY_SF_PTR
862
863 debug_data->real_sf = real_sf;
864 symfile_debug_objfile_data_key.set (objfile, debug_data);
865 objfile->sf = &debug_data->debug_sf;
866 }
867
868 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
869 Do not call this if the debug versions are not installed. */
870
871 static void
872 uninstall_symfile_debug_logging (struct objfile *objfile)
873 {
874 struct debug_sym_fns_data *debug_data;
875
876 /* The debug versions should be currently installed. */
877 gdb_assert (symfile_debug_installed (objfile));
878
879 debug_data = symfile_debug_objfile_data_key.get (objfile);
880
881 objfile->sf = debug_data->real_sf;
882 symfile_debug_objfile_data_key.clear (objfile);
883 }
884
885 /* Call this function to set OBJFILE->SF.
886 Do not set OBJFILE->SF directly. */
887
888 void
889 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
890 {
891 if (symfile_debug_installed (objfile))
892 {
893 gdb_assert (debug_symfile);
894 /* Remove the current one, and reinstall a new one later. */
895 uninstall_symfile_debug_logging (objfile);
896 }
897
898 /* Assume debug logging is disabled. */
899 objfile->sf = sf;
900
901 /* Turn debug logging on if enabled. */
902 if (debug_symfile)
903 install_symfile_debug_logging (objfile);
904 }
905
906 static void
907 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
908 {
909 for (struct program_space *pspace : program_spaces)
910 for (objfile *objfile : pspace->objfiles ())
911 {
912 if (debug_symfile)
913 {
914 if (!symfile_debug_installed (objfile))
915 install_symfile_debug_logging (objfile);
916 }
917 else
918 {
919 if (symfile_debug_installed (objfile))
920 uninstall_symfile_debug_logging (objfile);
921 }
922 }
923 }
924
925 static void
926 show_debug_symfile (struct ui_file *file, int from_tty,
927 struct cmd_list_element *c, const char *value)
928 {
929 gdb_printf (file, _("Symfile debugging is %s.\n"), value);
930 }
931
932 void _initialize_symfile_debug ();
933 void
934 _initialize_symfile_debug ()
935 {
936 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
937 Set debugging of the symfile functions."), _("\
938 Show debugging of the symfile functions."), _("\
939 When enabled, all calls to the symfile functions are logged."),
940 set_debug_symfile, show_debug_symfile,
941 &setdebuglist, &showdebuglist);
942
943 /* Note: We don't need a new-objfile observer because debug logging
944 will be installed when objfile init'n calls objfile_set_sym_fns. */
945 }