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 "cli/cli-cmds.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"
39 
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.  */
43 
44 struct debug_sym_fns_data
45 {
46   const struct sym_fns *real_sf = nullptr;
47   struct sym_fns debug_sf {};
48 };
49 
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;
54 
55 /* If true all calls to the symfile functions are logged.  */
56 static bool debug_symfile = false;
57 
58 /* Return non-zero if symfile debug logging is installed.  */
59 
60 static int
symfile_debug_installed(struct objfile * objfile)61 symfile_debug_installed (struct objfile *objfile)
62 {
63   return (objfile->sf != NULL
64             && symfile_debug_objfile_data_key.get (objfile) != NULL);
65 }
66 
67 /* Utility return the name to print for SYMTAB.  */
68 
69 static const char *
debug_symtab_name(struct symtab * symtab)70 debug_symtab_name (struct symtab *symtab)
71 {
72   return symtab_to_filename_for_display (symtab);
73 }
74 
75 
76 /* See objfiles.h.  */
77 
78 bool
has_partial_symbols()79 objfile::has_partial_symbols ()
80 {
81   bool retval = false;
82 
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)
88     {
89       retval = iter->has_symbols (this);
90       if (retval)
91           break;
92     }
93 
94   if (debug_symfile)
95     gdb_printf (gdb_stdlog, "qf->has_symbols (%s) = %d\n",
96                     objfile_debug_name (this), retval);
97 
98   return retval;
99 }
100 
101 /* See objfiles.h.  */
102 bool
has_unexpanded_symtabs()103 objfile::has_unexpanded_symtabs ()
104 {
105   if (debug_symfile)
106     gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s)\n",
107                     objfile_debug_name (this));
108 
109   bool result = false;
110   for (const auto &iter : qf)
111     {
112       if (iter->has_unexpanded_symtabs (this))
113           {
114             result = true;
115             break;
116           }
117     }
118 
119   if (debug_symfile)
120     gdb_printf (gdb_stdlog, "qf->has_unexpanded_symtabs (%s) = %d\n",
121                     objfile_debug_name (this), (result ? 1 : 0));
122 
123   return result;
124 }
125 
126 struct symtab *
find_last_source_symtab()127 objfile::find_last_source_symtab ()
128 {
129   struct symtab *retval = nullptr;
130 
131   if (debug_symfile)
132     gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (%s)\n",
133                     objfile_debug_name (this));
134 
135   for (const auto &iter : qf)
136     {
137       retval = iter->find_last_source_symtab (this);
138       if (retval != nullptr)
139           break;
140     }
141 
142   if (debug_symfile)
143     gdb_printf (gdb_stdlog, "qf->find_last_source_symtab (...) = %s\n",
144                     retval ? debug_symtab_name (retval) : "NULL");
145 
146   return retval;
147 }
148 
149 void
forget_cached_source_info()150 objfile::forget_cached_source_info ()
151 {
152   if (debug_symfile)
153     gdb_printf (gdb_stdlog, "qf->forget_cached_source_info (%s)\n",
154                     objfile_debug_name (this));
155 
156   for (compunit_symtab *cu : compunits ())
157     {
158       for (symtab *s : cu->filetabs ())
159           {
160             if (s->fullname != NULL)
161               {
162                 xfree (s->fullname);
163                 s->fullname = NULL;
164               }
165           }
166     }
167 
168   for (const auto &iter : qf)
169     iter->forget_cached_source_info (this);
170 }
171 
172 bool
map_symtabs_matching_filename(const char * name,const char * real_path,gdb::function_view<bool (symtab *)> callback)173 objfile::map_symtabs_matching_filename
174   (const char *name, const char *real_path,
175    gdb::function_view<bool (symtab *)> callback)
176 {
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));
184 
185   bool retval = true;
186   const char *name_basename = lbasename (name);
187 
188   auto match_one_filename = [&] (const char *filename, bool basenames)
189   {
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;
198   };
199 
200   compunit_symtab *last_made = this->compunit_symtabs;
201 
202   auto on_expansion = [&] (compunit_symtab *symtab)
203   {
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;
213   };
214 
215   for (const auto &iter : qf)
216     {
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))
225           {
226             retval = false;
227             break;
228           }
229     }
230 
231   if (debug_symfile)
232     gdb_printf (gdb_stdlog,
233                     "qf->map_symtabs_matching_filename (...) = %d\n",
234                     retval);
235 
236   /* We must re-invert the return value here to match the caller's
237      expectations.  */
238   return !retval;
239 }
240 
241 struct compunit_symtab *
lookup_symbol(block_enum kind,const lookup_name_info & name,domain_search_flags domain)242 objfile::lookup_symbol (block_enum kind, const lookup_name_info &name,
243                               domain_search_flags domain)
244 {
245   struct compunit_symtab *retval = nullptr;
246 
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 ());
252 
253   auto search_one_symtab = [&] (compunit_symtab *stab)
254   {
255     struct symbol *sym, *with_opaque = NULL;
256     const struct blockvector *bv = stab->blockvector ();
257     const struct block *block = bv->block (kind);
258 
259     sym = block_find_symbol (block, name, domain, &with_opaque);
260 
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).  */
264 
265     if (sym != nullptr)
266       {
267           retval = stab;
268           /* Found it.  */
269           return false;
270       }
271     if (with_opaque != nullptr)
272       retval = stab;
273 
274     /* Keep looking through other psymtabs.  */
275     return true;
276   };
277 
278   for (const auto &iter : qf)
279     {
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;
290     }
291 
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");
297 
298   return retval;
299 }
300 
301 void
print_stats(bool print_bcache)302 objfile::print_stats (bool print_bcache)
303 {
304   if (debug_symfile)
305     gdb_printf (gdb_stdlog, "qf->print_stats (%s, %d)\n",
306                     objfile_debug_name (this), print_bcache);
307 
308   for (const auto &iter : qf)
309     iter->print_stats (this, print_bcache);
310 }
311 
312 void
dump()313 objfile::dump ()
314 {
315   if (debug_symfile)
316     gdb_printf (gdb_stdlog, "qf->dump (%s)\n",
317                     objfile_debug_name (this));
318 
319   for (const auto &iter : qf)
320     iter->dump (this);
321 }
322 
323 void
expand_symtabs_for_function(const char * func_name)324 objfile::expand_symtabs_for_function (const char *func_name)
325 {
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);
330 
331   lookup_name_info base_lookup (func_name, symbol_name_match_type::FULL);
332   lookup_name_info lookup_name = base_lookup.make_ignore_params ();
333 
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);
343 }
344 
345 void
expand_all_symtabs()346 objfile::expand_all_symtabs ()
347 {
348   if (debug_symfile)
349     gdb_printf (gdb_stdlog, "qf->expand_all_symtabs (%s)\n",
350                     objfile_debug_name (this));
351 
352   for (const auto &iter : qf)
353     iter->expand_all_symtabs (this);
354 }
355 
356 void
expand_symtabs_with_fullname(const char * fullname)357 objfile::expand_symtabs_with_fullname (const char *fullname)
358 {
359   if (debug_symfile)
360     gdb_printf (gdb_stdlog,
361                     "qf->expand_symtabs_with_fullname (%s, \"%s\")\n",
362                     objfile_debug_name (this), fullname);
363 
364   const char *basename = lbasename (fullname);
365   auto file_matcher = [&] (const char *filename, bool basenames)
366   {
367     return filename_cmp (basenames ? basename : fullname, filename) == 0;
368   };
369 
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);
379 }
380 
381 bool
expand_symtabs_matching(gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,const lookup_name_info * lookup_name,gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,block_search_flags search_flags,domain_search_flags domain)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)
389 {
390   /* This invariant is documented in quick-functions.h.  */
391   gdb_assert (lookup_name != nullptr || symbol_matcher == nullptr);
392 
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 ());
401 
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;
408 }
409 
410 struct compunit_symtab *
find_pc_sect_compunit_symtab(struct bound_minimal_symbol msymbol,CORE_ADDR pc,struct obj_section * section,int warn_if_readin)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)
415 {
416   struct compunit_symtab *retval = nullptr;
417 
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);
426 
427   for (const auto &iter : qf)
428     {
429       retval = iter->find_pc_sect_compunit_symtab (this, msymbol, pc, section,
430                                                                warn_if_readin);
431       if (retval != nullptr)
432           break;
433     }
434 
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");
441 
442   return retval;
443 }
444 
445 void
map_symbol_filenames(gdb::function_view<symbol_filename_ftype> fun,bool need_fullname)446 objfile::map_symbol_filenames (gdb::function_view<symbol_filename_ftype> fun,
447                                      bool need_fullname)
448 {
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);
454 
455   for (const auto &iter : qf)
456     iter->map_symbol_filenames (this, fun, need_fullname);
457 }
458 
459 void
compute_main_name()460 objfile::compute_main_name ()
461 {
462   if (debug_symfile)
463     gdb_printf (gdb_stdlog,
464                     "qf->compute_main_name (%s)\n",
465                     objfile_debug_name (this));
466 
467   for (const auto &iter : qf)
468     iter->compute_main_name (this);
469 }
470 
471 struct compunit_symtab *
find_compunit_symtab_by_address(CORE_ADDR address)472 objfile::find_compunit_symtab_by_address (CORE_ADDR address)
473 {
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));
479 
480   struct compunit_symtab *result = NULL;
481   for (const auto &iter : qf)
482     {
483       result = iter->find_compunit_symtab_by_address (this, address);
484       if (result != nullptr)
485           break;
486     }
487 
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");
494 
495   return result;
496 }
497 
498 enum language
lookup_global_symbol_language(const char * name,domain_search_flags domain,bool * symbol_found_p)499 objfile::lookup_global_symbol_language (const char *name,
500                                                   domain_search_flags domain,
501                                                   bool *symbol_found_p)
502 {
503   enum language result = language_unknown;
504   *symbol_found_p = false;
505 
506   for (const auto &iter : qf)
507     {
508       result = iter->lookup_global_symbol_language (this, name, domain,
509                                                                 symbol_found_p);
510       if (*symbol_found_p)
511           break;
512     }
513 
514   return result;
515 }
516 
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.
523 
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>
simple_find_and_open_separate_symbol_file(struct objfile * objfile,std::string (* lookup_func)(struct objfile *,deferred_warnings *),deferred_warnings * warnings)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)
532 {
533   std::string filename = lookup_func (objfile, warnings);
534 
535   if (!filename.empty ())
536     {
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 };
541     }
542 
543   return {};
544 }
545 
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.  */
553 
554 static std::pair<gdb_bfd_ref_ptr, std::string>
debuginfod_find_and_open_separate_symbol_file(struct objfile * objfile)555 debuginfod_find_and_open_separate_symbol_file (struct objfile * objfile)
556 {
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 ());
560 
561   if (build_id != nullptr)
562     {
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));
566 
567       if (fd.get () >= 0)
568           {
569             /* File successfully retrieved from server.  */
570             gdb_bfd_ref_ptr debug_bfd
571               (symfile_bfd_open_no_error (symfile_path.get ()));
572 
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 ()) };
577           }
578     }
579 
580   return {};
581 }
582 
583 /* See objfiles.h.  */
584 
585 bool
find_and_add_separate_symbol_file(symfile_add_flags symfile_flags)586 objfile::find_and_add_separate_symbol_file (symfile_add_flags symfile_flags)
587 {
588   bool has_dwarf2 = false;
589 
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)
597     {
598       gdb_assert (max_attempts <= 2);
599 
600       deferred_warnings warnings;
601       gdb_bfd_ref_ptr debug_bfd;
602       std::string filename;
603 
604       std::tie (debug_bfd, filename)
605           = simple_find_and_open_separate_symbol_file
606               (this, find_separate_debug_file_by_buildid, &warnings);
607 
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);
612 
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);
622 
623       if (debug_bfd != nullptr)
624           {
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;
631           }
632       else if (attempt == 0)
633           {
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.  */
639 
640             ext_lang_missing_debuginfo_result ext_result
641               = ext_lang_handle_missing_debuginfo (this);
642             if (!ext_result.filename ().empty ())
643               {
644                 /* Extension found a suitable debug file for us.  */
645                 debug_bfd
646                     = symfile_bfd_open_no_error (ext_result.filename ().c_str ());
647 
648                 if (debug_bfd != nullptr)
649                     {
650                       symbol_file_add_separate (debug_bfd,
651                                                       ext_result.filename ().c_str (),
652                                                       symfile_flags, this);
653                       has_dwarf2 = true;
654                     }
655               }
656             else if (ext_result.try_again ())
657               {
658                 max_attempts = 2;
659                 continue;
660               }
661           }
662 
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 ();
667     }
668 
669   return has_dwarf2;
670 }
671 
672 
673 /* Debugging version of struct sym_probe_fns.  */
674 
675 static const std::vector<std::unique_ptr<probe>> &
debug_sym_get_probes(struct objfile * objfile)676 debug_sym_get_probes (struct objfile *objfile)
677 {
678   const struct debug_sym_fns_data *debug_data
679     = symfile_debug_objfile_data_key.get (objfile);
680 
681   const std::vector<std::unique_ptr<probe>> &retval
682     = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
683 
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 ()));
688 
689   return retval;
690 }
691 
692 static const struct sym_probe_fns debug_sym_probe_fns =
693 {
694   debug_sym_get_probes,
695 };
696 
697 /* Debugging version of struct sym_fns.  */
698 
699 static void
debug_sym_new_init(struct objfile * objfile)700 debug_sym_new_init (struct objfile *objfile)
701 {
702   const struct debug_sym_fns_data *debug_data
703     = symfile_debug_objfile_data_key.get (objfile);
704 
705   gdb_printf (gdb_stdlog, "sf->sym_new_init (%s)\n",
706                 objfile_debug_name (objfile));
707 
708   debug_data->real_sf->sym_new_init (objfile);
709 }
710 
711 static void
debug_sym_init(struct objfile * objfile)712 debug_sym_init (struct objfile *objfile)
713 {
714   const struct debug_sym_fns_data *debug_data
715     = symfile_debug_objfile_data_key.get (objfile);
716 
717   gdb_printf (gdb_stdlog, "sf->sym_init (%s)\n",
718                 objfile_debug_name (objfile));
719 
720   debug_data->real_sf->sym_init (objfile);
721 }
722 
723 static void
debug_sym_read(struct objfile * objfile,symfile_add_flags symfile_flags)724 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
725 {
726   const struct debug_sym_fns_data *debug_data
727     = symfile_debug_objfile_data_key.get (objfile);
728 
729   gdb_printf (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
730                 objfile_debug_name (objfile), (unsigned) symfile_flags);
731 
732   debug_data->real_sf->sym_read (objfile, symfile_flags);
733 }
734 
735 static void
debug_sym_finish(struct objfile * objfile)736 debug_sym_finish (struct objfile *objfile)
737 {
738   const struct debug_sym_fns_data *debug_data
739     = symfile_debug_objfile_data_key.get (objfile);
740 
741   gdb_printf (gdb_stdlog, "sf->sym_finish (%s)\n",
742                 objfile_debug_name (objfile));
743 
744   debug_data->real_sf->sym_finish (objfile);
745 }
746 
747 static void
debug_sym_offsets(struct objfile * objfile,const section_addr_info & info)748 debug_sym_offsets (struct objfile *objfile,
749                        const section_addr_info &info)
750 {
751   const struct debug_sym_fns_data *debug_data
752     = symfile_debug_objfile_data_key.get (objfile);
753 
754   gdb_printf (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
755                 objfile_debug_name (objfile),
756                 host_address_to_string (&info));
757 
758   debug_data->real_sf->sym_offsets (objfile, info);
759 }
760 
761 static symfile_segment_data_up
debug_sym_segments(bfd * abfd)762 debug_sym_segments (bfd *abfd)
763 {
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");
768 }
769 
770 static void
debug_sym_read_linetable(struct objfile * objfile)771 debug_sym_read_linetable (struct objfile *objfile)
772 {
773   const struct debug_sym_fns_data *debug_data
774     = symfile_debug_objfile_data_key.get (objfile);
775 
776   gdb_printf (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
777                 objfile_debug_name (objfile));
778 
779   debug_data->real_sf->sym_read_linetable (objfile);
780 }
781 
782 static bfd_byte *
debug_sym_relocate(struct objfile * objfile,asection * sectp,bfd_byte * buf)783 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
784 {
785   const struct debug_sym_fns_data *debug_data
786     = symfile_debug_objfile_data_key.get (objfile);
787   bfd_byte *retval;
788 
789   retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
790 
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));
797 
798   return retval;
799 }
800 
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.  */
805 
806 static const struct sym_fns debug_sym_fns =
807 {
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,
817 };
818 
819 /* Install the debugging versions of the symfile functions for OBJFILE.
820    Do not call this if the debug versions are already installed.  */
821 
822 static void
install_symfile_debug_logging(struct objfile * objfile)823 install_symfile_debug_logging (struct objfile *objfile)
824 {
825   const struct sym_fns *real_sf;
826   struct debug_sym_fns_data *debug_data;
827 
828   /* The debug versions should not already be installed.  */
829   gdb_assert (!symfile_debug_installed (objfile));
830 
831   real_sf = objfile->sf;
832 
833   /* Alas we have to preserve NULL entries in REAL_SF.  */
834   debug_data = new struct debug_sym_fns_data;
835 
836 #define COPY_SF_PTR(from, to, name, func)         \
837   do {                                                      \
838     if ((from)->name)                                       \
839       (to)->debug_sf.name = func;                 \
840   } while (0)
841 
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;
853 
854 #undef COPY_SF_PTR
855 
856   debug_data->real_sf = real_sf;
857   symfile_debug_objfile_data_key.set (objfile, debug_data);
858   objfile->sf = &debug_data->debug_sf;
859 }
860 
861 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
862    Do not call this if the debug versions are not installed.  */
863 
864 static void
uninstall_symfile_debug_logging(struct objfile * objfile)865 uninstall_symfile_debug_logging (struct objfile *objfile)
866 {
867   struct debug_sym_fns_data *debug_data;
868 
869   /* The debug versions should be currently installed.  */
870   gdb_assert (symfile_debug_installed (objfile));
871 
872   debug_data = symfile_debug_objfile_data_key.get (objfile);
873 
874   objfile->sf = debug_data->real_sf;
875   symfile_debug_objfile_data_key.clear (objfile);
876 }
877 
878 /* Call this function to set OBJFILE->SF.
879    Do not set OBJFILE->SF directly.  */
880 
881 void
objfile_set_sym_fns(struct objfile * objfile,const struct sym_fns * sf)882 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
883 {
884   if (symfile_debug_installed (objfile))
885     {
886       gdb_assert (debug_symfile);
887       /* Remove the current one, and reinstall a new one later.  */
888       uninstall_symfile_debug_logging (objfile);
889     }
890 
891   /* Assume debug logging is disabled.  */
892   objfile->sf = sf;
893 
894   /* Turn debug logging on if enabled.  */
895   if (debug_symfile)
896     install_symfile_debug_logging (objfile);
897 }
898 
899 static void
set_debug_symfile(const char * args,int from_tty,struct cmd_list_element * c)900 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
901 {
902   for (struct program_space *pspace : program_spaces)
903     for (objfile *objfile : pspace->objfiles ())
904       {
905           if (debug_symfile)
906             {
907               if (!symfile_debug_installed (objfile))
908                 install_symfile_debug_logging (objfile);
909             }
910           else
911             {
912               if (symfile_debug_installed (objfile))
913                 uninstall_symfile_debug_logging (objfile);
914             }
915       }
916 }
917 
918 static void
show_debug_symfile(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)919 show_debug_symfile (struct ui_file *file, int from_tty,
920                               struct cmd_list_element *c, const char *value)
921 {
922   gdb_printf (file, _("Symfile debugging is %s.\n"), value);
923 }
924 
925 void _initialize_symfile_debug ();
926 void
_initialize_symfile_debug()927 _initialize_symfile_debug ()
928 {
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);
935 
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.  */
938 }
939