850da4147a3f02969c9c6929e1339795df362709
[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
39 /* We need to save a pointer to the real symbol functions.
40 Plus, the debug versions are malloc'd because we have to NULL out the
41 ones that are NULL in the real copy. */
42
43 struct debug_sym_fns_data
44 {
45 const struct sym_fns *real_sf = nullptr;
46 struct sym_fns debug_sf {};
47 };
48
49 /* We need to record a pointer to the real set of functions for each
50 objfile. */
51 static const registry<objfile>::key<debug_sym_fns_data>
52 symfile_debug_objfile_data_key;
53
54 /* If true all calls to the symfile functions are logged. */
55 static bool debug_symfile = false;
56
57 /* Return non-zero if symfile debug logging is installed. */
58
59 static int
60 symfile_debug_installed (struct objfile *objfile)
61 {
62 return (objfile->sf != NULL
63 && symfile_debug_objfile_data_key.get (objfile) != NULL);
64 }
65
66 /* Utility return the name to print for SYMTAB. */
67
68 static const char *
69 debug_symtab_name (struct symtab *symtab)
70 {
71 return symtab_to_filename_for_display (symtab);
72 }
73 \f
74
75 /* See objfiles.h. */
76
77 bool
78 objfile::has_partial_symbols ()
79 {
80 bool retval = false;
81
82 /* If we have not read psymbols, but we have a function capable of reading
83 them, then that is an indication that they are in fact available. Without
84 this function the symbols may have been already read in but they also may
85 not be present in this objfile. */
86 for (const auto &iter : qf)
87 {
88 if ((flags & OBJF_PSYMTABS_READ) == 0
89 && iter->can_lazily_read_symbols ())
90 retval = true;
91 else
92 retval = iter->has_symbols (this);
93 if (retval)
94 break;
95 }
96
97 if (debug_symfile)
98 gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
99 objfile_debug_name (this), retval);
100
101 return retval;
102 }
103
104 /* See objfiles.h. */
105 bool
106 objfile::has_unexpanded_symtabs ()
107 {
108 if (debug_symfile)
109 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
110 objfile_debug_name (this));
111
112 bool result = false;
113 for (const auto &iter : qf_require_partial_symbols ())
114 {
115 if (iter->has_unexpanded_symtabs (this))
116 {
117 result = true;
118 break;
119 }
120 }
121
122 if (debug_symfile)
123 gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
124 objfile_debug_name (this), (result ? 1 : 0));
125
126 return result;
127 }
128
129 struct symtab *
130 objfile::find_last_source_symtab ()
131 {
132 struct symtab *retval = nullptr;
133
134 if (debug_symfile)
135 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
136 objfile_debug_name (this));
137
138 for (const auto &iter : qf_require_partial_symbols ())
139 {
140 retval = iter->find_last_source_symtab (this);
141 if (retval != nullptr)
142 break;
143 }
144
145 if (debug_symfile)
146 gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
147 retval ? debug_symtab_name (retval) : "NULL");
148
149 return retval;
150 }
151
152 void
153 objfile::forget_cached_source_info ()
154 {
155 if (debug_symfile)
156 gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
157 objfile_debug_name (this));
158
159 for (compunit_symtab *cu : compunits ())
160 {
161 for (symtab *s : cu->filetabs ())
162 {
163 if (s->fullname != NULL)
164 {
165 xfree (s->fullname);
166 s->fullname = NULL;
167 }
168 }
169 }
170
171 for (const auto &iter : qf_require_partial_symbols ())
172 iter->forget_cached_source_info (this);
173 }
174
175 bool
176 objfile::map_symtabs_matching_filename
177 (const char *name, const char *real_path,
178 gdb::function_view<bool (symtab *)> callback)
179 {
180 if (debug_symfile)
181 gdb_printf (gdb_stdlog,
182 "qf->map_symtabs_matching_filename (%s, \"%s\", "
183 "\"%s\", %s)\n",
184 objfile_debug_name (this), name,
185 real_path ? real_path : NULL,
186 host_address_to_string (&callback));
187
188 bool retval = true;
189 const char *name_basename = lbasename (name);
190
191 auto match_one_filename = [&] (const char *filename, bool basenames)
192 {
193 if (compare_filenames_for_search (filename, name))
194 return true;
195 if (basenames && FILENAME_CMP (name_basename, filename) == 0)
196 return true;
197 if (real_path != nullptr && IS_ABSOLUTE_PATH (filename)
198 && IS_ABSOLUTE_PATH (real_path))
199 return filename_cmp (filename, real_path) == 0;
200 return false;
201 };
202
203 compunit_symtab *last_made = this->compunit_symtabs;
204
205 auto on_expansion = [&] (compunit_symtab *symtab)
206 {
207 /* The callback to iterate_over_some_symtabs returns false to keep
208 going and true to continue, so we have to invert the result
209 here, for expand_symtabs_matching. */
210 bool result = !iterate_over_some_symtabs (name, real_path,
211 this->compunit_symtabs,
212 last_made,
213 callback);
214 last_made = this->compunit_symtabs;
215 return result;
216 };
217
218 for (const auto &iter : qf_require_partial_symbols ())
219 {
220 if (!iter->expand_symtabs_matching (this,
221 match_one_filename,
222 nullptr,
223 nullptr,
224 on_expansion,
225 (SEARCH_GLOBAL_BLOCK
226 | SEARCH_STATIC_BLOCK),
227 UNDEF_DOMAIN,
228 ALL_DOMAIN))
229 {
230 retval = false;
231 break;
232 }
233 }
234
235 if (debug_symfile)
236 gdb_printf (gdb_stdlog,
237 "qf->map_symtabs_matching_filename (...) = %d\n",
238 retval);
239
240 /* We must re-invert the return value here to match the caller's
241 expectations. */
242 return !retval;
243 }
244
245 struct compunit_symtab *
246 objfile::lookup_symbol (block_enum kind, const char *name, domain_enum domain)
247 {
248 struct compunit_symtab *retval = nullptr;
249
250 if (debug_symfile)
251 gdb_printf (gdb_stdlog,
252 "qf->lookup_symbol (%s, %d, \"%s\", %s)\n",
253 objfile_debug_name (this), kind, name,
254 domain_name (domain));
255
256 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
257
258 auto search_one_symtab = [&] (compunit_symtab *stab)
259 {
260 struct symbol *sym, *with_opaque = NULL;
261 const struct blockvector *bv = stab->blockvector ();
262 const struct block *block = bv->block (kind);
263
264 sym = block_find_symbol (block, lookup_name, domain, &with_opaque);
265
266 /* Some caution must be observed with overloaded functions
267 and methods, since the index will not contain any overload
268 information (but NAME might contain it). */
269
270 if (sym != nullptr)
271 {
272 retval = stab;
273 /* Found it. */
274 return false;
275 }
276 if (with_opaque != nullptr)
277 retval = stab;
278
279 /* Keep looking through other psymtabs. */
280 return true;
281 };
282
283 for (const auto &iter : qf_require_partial_symbols ())
284 {
285 if (!iter->expand_symtabs_matching (this,
286 nullptr,
287 &lookup_name,
288 nullptr,
289 search_one_symtab,
290 kind == GLOBAL_BLOCK
291 ? SEARCH_GLOBAL_BLOCK
292 : SEARCH_STATIC_BLOCK,
293 domain,
294 ALL_DOMAIN))
295 break;
296 }
297
298 if (debug_symfile)
299 gdb_printf (gdb_stdlog, "qf->lookup_symbol (...) = %s\n",
300 retval
301 ? debug_symtab_name (retval->primary_filetab ())
302 : "NULL");
303
304 return retval;
305 }
306
307 void
308 objfile::print_stats (bool print_bcache)
309 {
310 if (debug_symfile)
311 gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
312 objfile_debug_name (this), print_bcache);
313
314 for (const auto &iter : qf_require_partial_symbols ())
315 iter->print_stats (this, print_bcache);
316 }
317
318 void
319 objfile::dump ()
320 {
321 if (debug_symfile)
322 gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
323 objfile_debug_name (this));
324
325 for (const auto &iter : qf)
326 iter->dump (this);
327 }
328
329 void
330 objfile::expand_symtabs_for_function (const char *func_name)
331 {
332 if (debug_symfile)
333 gdb_printf (gdb_stdlog,
334 "qf->expand_symtabs_for_function (%s, \"%s\")\n",
335 objfile_debug_name (this), func_name);
336
337 lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
338 lookup_name_info lookup_name = base_lookup.make_ignore_params ();
339
340 for (const auto &iter : qf_require_partial_symbols ())
341 iter->expand_symtabs_matching (this,
342 nullptr,
343 &lookup_name,
344 nullptr,
345 nullptr,
346 (SEARCH_GLOBAL_BLOCK
347 | SEARCH_STATIC_BLOCK),
348 VAR_DOMAIN,
349 ALL_DOMAIN);
350 }
351
352 void
353 objfile::expand_all_symtabs ()
354 {
355 if (debug_symfile)
356 gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
357 objfile_debug_name (this));
358
359 for (const auto &iter : qf_require_partial_symbols ())
360 iter->expand_all_symtabs (this);
361 }
362
363 void
364 objfile::expand_symtabs_with_fullname (const char *fullname)
365 {
366 if (debug_symfile)
367 gdb_printf (gdb_stdlog,
368 "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
369 objfile_debug_name (this), fullname);
370
371 const char *basename = lbasename (fullname);
372 auto file_matcher = [&] (const char *filename, bool basenames)
373 {
374 return filename_cmp (basenames ? basename : fullname, filename) == 0;
375 };
376
377 for (const auto &iter : qf_require_partial_symbols ())
378 iter->expand_symtabs_matching (this,
379 file_matcher,
380 nullptr,
381 nullptr,
382 nullptr,
383 (SEARCH_GLOBAL_BLOCK
384 | SEARCH_STATIC_BLOCK),
385 UNDEF_DOMAIN,
386 ALL_DOMAIN);
387 }
388
389 void
390 objfile::expand_matching_symbols
391 (const lookup_name_info &name, domain_enum domain,
392 int global,
393 symbol_compare_ftype *ordered_compare)
394 {
395 if (debug_symfile)
396 gdb_printf (gdb_stdlog,
397 "qf->expand_matching_symbols (%s, %s, %d, %s)\n",
398 objfile_debug_name (this),
399 domain_name (domain), global,
400 host_address_to_string (ordered_compare));
401
402 for (const auto &iter : qf_require_partial_symbols ())
403 iter->expand_matching_symbols (this, name, domain, global,
404 ordered_compare);
405 }
406
407 bool
408 objfile::expand_symtabs_matching
409 (gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
410 const lookup_name_info *lookup_name,
411 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
412 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
413 block_search_flags search_flags,
414 domain_enum domain,
415 enum search_domain kind)
416 {
417 /* This invariant is documented in quick-functions.h. */
418 gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
419
420 if (debug_symfile)
421 gdb_printf (gdb_stdlog,
422 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
423 objfile_debug_name (this),
424 host_address_to_string (&file_matcher),
425 host_address_to_string (&symbol_matcher),
426 host_address_to_string (&expansion_notify),
427 search_domain_name (kind));
428
429 for (const auto &iter : qf_require_partial_symbols ())
430 if (!iter->expand_symtabs_matching (this, file_matcher, lookup_name,
431 symbol_matcher, expansion_notify,
432 search_flags, domain, kind))
433 return false;
434 return true;
435 }
436
437 struct compunit_symtab *
438 objfile::find_pc_sect_compunit_symtab (struct bound_minimal_symbol msymbol,
439 CORE_ADDR pc,
440 struct obj_section *section,
441 int warn_if_readin)
442 {
443 struct compunit_symtab *retval = nullptr;
444
445 if (debug_symfile)
446 gdb_printf (gdb_stdlog,
447 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
448 objfile_debug_name (this),
449 host_address_to_string (msymbol.minsym),
450 hex_string (pc),
451 host_address_to_string (section),
452 warn_if_readin);
453
454 for (const auto &iter : qf_require_partial_symbols ())
455 {
456 retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
457 warn_if_readin);
458 if (retval != nullptr)
459 break;
460 }
461
462 if (debug_symfile)
463 gdb_printf (gdb_stdlog,
464 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
465 retval
466 ? debug_symtab_name (retval->primary_filetab ())
467 : "NULL");
468
469 return retval;
470 }
471
472 void
473 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
474 bool need_fullname)
475 {
476 if (debug_symfile)
477 gdb_printf (gdb_stdlog,
478 "qf->map_symbol_filenames (%s, ..., %d)\n",
479 objfile_debug_name (this),
480 need_fullname);
481
482 for (const auto &iter : qf_require_partial_symbols ())
483 iter->map_symbol_filenames (this, fun, need_fullname);
484 }
485
486 struct compunit_symtab *
487 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
488 {
489 if (debug_symfile)
490 gdb_printf (gdb_stdlog,
491 "qf->find_compunit_symtab_by_address (%s, %s)\n",
492 objfile_debug_name (this),
493 hex_string (address));
494
495 struct compunit_symtab *result = NULL;
496 for (const auto &iter : qf_require_partial_symbols ())
497 {
498 result = iter->find_compunit_symtab_by_address (this, address);
499 if (result != nullptr)
500 break;
501 }
502
503 if (debug_symfile)
504 gdb_printf (gdb_stdlog,
505 "qf->find_compunit_symtab_by_address (...) = %s\n",
506 result
507 ? debug_symtab_name (result->primary_filetab ())
508 : "NULL");
509
510 return result;
511 }
512
513 enum language
514 objfile::lookup_global_symbol_language (const char *name,
515 domain_enum domain,
516 bool *symbol_found_p)
517 {
518 enum language result = language_unknown;
519 *symbol_found_p = false;
520
521 for (const auto &iter : qf_require_partial_symbols ())
522 {
523 result = iter->lookup_global_symbol_language (this, name, domain,
524 symbol_found_p);
525 if (*symbol_found_p)
526 break;
527 }
528
529 return result;
530 }
531
532 void
533 objfile::require_partial_symbols (bool verbose)
534 {
535 if ((flags & OBJF_PSYMTABS_READ) == 0)
536 {
537 flags |= OBJF_PSYMTABS_READ;
538
539 bool printed = false;
540 for (const auto &iter : qf)
541 {
542 if (iter->can_lazily_read_symbols ())
543 {
544 if (verbose && !printed)
545 {
546 gdb_printf (_("Reading symbols from %ps...\n"),
547 styled_string (file_name_style.style (),
548 objfile_name (this)));
549 printed = true;
550 }
551 iter->read_partial_symbols (this);
552 }
553 }
554 if (printed && !objfile_has_symbols (this))
555 gdb_printf (_("(No debugging symbols found in %ps)\n"),
556 styled_string (file_name_style.style (),
557 objfile_name (this)));
558 }
559 }
560
561 \f
562 /* Debugging version of struct sym_probe_fns. */
563
564 static const std::vector<std::unique_ptr<probe>> &
565 debug_sym_get_probes (struct objfile *objfile)
566 {
567 const struct debug_sym_fns_data *debug_data
568 = symfile_debug_objfile_data_key.get (objfile);
569
570 const std::vector<std::unique_ptr<probe>> &retval
571 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
572
573 gdb_printf (gdb_stdlog,
574 "probes->sym_get_probes (%s) = %s\n",
575 objfile_debug_name (objfile),
576 host_address_to_string (retval.data ()));
577
578 return retval;
579 }
580
581 static const struct sym_probe_fns debug_sym_probe_fns =
582 {
583 debug_sym_get_probes,
584 };
585 \f
586 /* Debugging version of struct sym_fns. */
587
588 static void
589 debug_sym_new_init (struct objfile *objfile)
590 {
591 const struct debug_sym_fns_data *debug_data
592 = symfile_debug_objfile_data_key.get (objfile);
593
594 gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
595 objfile_debug_name (objfile));
596
597 debug_data->real_sf->sym_new_init (objfile);
598 }
599
600 static void
601 debug_sym_init (struct objfile *objfile)
602 {
603 const struct debug_sym_fns_data *debug_data
604 = symfile_debug_objfile_data_key.get (objfile);
605
606 gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
607 objfile_debug_name (objfile));
608
609 debug_data->real_sf->sym_init (objfile);
610 }
611
612 static void
613 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
614 {
615 const struct debug_sym_fns_data *debug_data
616 = symfile_debug_objfile_data_key.get (objfile);
617
618 gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
619 objfile_debug_name (objfile), (unsigned) symfile_flags);
620
621 debug_data->real_sf->sym_read (objfile, symfile_flags);
622 }
623
624 static void
625 debug_sym_finish (struct objfile *objfile)
626 {
627 const struct debug_sym_fns_data *debug_data
628 = symfile_debug_objfile_data_key.get (objfile);
629
630 gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
631 objfile_debug_name (objfile));
632
633 debug_data->real_sf->sym_finish (objfile);
634 }
635
636 static void
637 debug_sym_offsets (struct objfile *objfile,
638 const section_addr_info &info)
639 {
640 const struct debug_sym_fns_data *debug_data
641 = symfile_debug_objfile_data_key.get (objfile);
642
643 gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
644 objfile_debug_name (objfile),
645 host_address_to_string (&info));
646
647 debug_data->real_sf->sym_offsets (objfile, info);
648 }
649
650 static symfile_segment_data_up
651 debug_sym_segments (bfd *abfd)
652 {
653 /* This API function is annoying, it doesn't take a "this" pointer.
654 Fortunately it is only used in one place where we (re-)lookup the
655 sym_fns table to use. Thus we will never be called. */
656 gdb_assert_not_reached ("debug_sym_segments called");
657 }
658
659 static void
660 debug_sym_read_linetable (struct objfile *objfile)
661 {
662 const struct debug_sym_fns_data *debug_data
663 = symfile_debug_objfile_data_key.get (objfile);
664
665 gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
666 objfile_debug_name (objfile));
667
668 debug_data->real_sf->sym_read_linetable (objfile);
669 }
670
671 static bfd_byte *
672 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
673 {
674 const struct debug_sym_fns_data *debug_data
675 = symfile_debug_objfile_data_key.get (objfile);
676 bfd_byte *retval;
677
678 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
679
680 gdb_printf (gdb_stdlog,
681 "sf->sym_relocate (%s, %s, %s) = %s\n",
682 objfile_debug_name (objfile),
683 host_address_to_string (sectp),
684 host_address_to_string (buf),
685 host_address_to_string (retval));
686
687 return retval;
688 }
689
690 /* Template of debugging version of struct sym_fns.
691 A copy is made, with sym_flavour updated, and a pointer to the real table
692 installed in real_sf, and then a pointer to the copy is installed in the
693 objfile. */
694
695 static const struct sym_fns debug_sym_fns =
696 {
697 debug_sym_new_init,
698 debug_sym_init,
699 debug_sym_read,
700 debug_sym_finish,
701 debug_sym_offsets,
702 debug_sym_segments,
703 debug_sym_read_linetable,
704 debug_sym_relocate,
705 &debug_sym_probe_fns,
706 };
707 \f
708 /* Install the debugging versions of the symfile functions for OBJFILE.
709 Do not call this if the debug versions are already installed. */
710
711 static void
712 install_symfile_debug_logging (struct objfile *objfile)
713 {
714 const struct sym_fns *real_sf;
715 struct debug_sym_fns_data *debug_data;
716
717 /* The debug versions should not already be installed. */
718 gdb_assert (!symfile_debug_installed (objfile));
719
720 real_sf = objfile->sf;
721
722 /* Alas we have to preserve NULL entries in REAL_SF. */
723 debug_data = new struct debug_sym_fns_data;
724
725 #define COPY_SF_PTR(from, to, name, func) \
726 do { \
727 if ((from)->name) \
728 (to)->debug_sf.name = func; \
729 } while (0)
730
731 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
732 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
733 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
734 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
735 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
736 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
737 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
738 debug_sym_read_linetable);
739 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
740 if (real_sf->sym_probe_fns)
741 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
742
743 #undef COPY_SF_PTR
744
745 debug_data->real_sf = real_sf;
746 symfile_debug_objfile_data_key.set (objfile, debug_data);
747 objfile->sf = &debug_data->debug_sf;
748 }
749
750 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
751 Do not call this if the debug versions are not installed. */
752
753 static void
754 uninstall_symfile_debug_logging (struct objfile *objfile)
755 {
756 struct debug_sym_fns_data *debug_data;
757
758 /* The debug versions should be currently installed. */
759 gdb_assert (symfile_debug_installed (objfile));
760
761 debug_data = symfile_debug_objfile_data_key.get (objfile);
762
763 objfile->sf = debug_data->real_sf;
764 symfile_debug_objfile_data_key.clear (objfile);
765 }
766
767 /* Call this function to set OBJFILE->SF.
768 Do not set OBJFILE->SF directly. */
769
770 void
771 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
772 {
773 if (symfile_debug_installed (objfile))
774 {
775 gdb_assert (debug_symfile);
776 /* Remove the current one, and reinstall a new one later. */
777 uninstall_symfile_debug_logging (objfile);
778 }
779
780 /* Assume debug logging is disabled. */
781 objfile->sf = sf;
782
783 /* Turn debug logging on if enabled. */
784 if (debug_symfile)
785 install_symfile_debug_logging (objfile);
786 }
787
788 static void
789 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
790 {
791 for (struct program_space *pspace : program_spaces)
792 for (objfile *objfile : pspace->objfiles ())
793 {
794 if (debug_symfile)
795 {
796 if (!symfile_debug_installed (objfile))
797 install_symfile_debug_logging (objfile);
798 }
799 else
800 {
801 if (symfile_debug_installed (objfile))
802 uninstall_symfile_debug_logging (objfile);
803 }
804 }
805 }
806
807 static void
808 show_debug_symfile (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
810 {
811 gdb_printf (file, _("Symfile debugging is %s.\n"), value);
812 }
813
814 void _initialize_symfile_debug ();
815 void
816 _initialize_symfile_debug ()
817 {
818 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
819 Set debugging of the symfile functions."), _("\
820 Show debugging of the symfile functions."), _("\
821 When enabled, all calls to the symfile functions are logged."),
822 set_debug_symfile, show_debug_symfile,
823 &setdebuglist, &showdebuglist);
824
825 /* Note: We don't need a new-objfile observer because debug logging
826 will be installed when objfile init'n calls objfile_set_sym_fns. */
827 }