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