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