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