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