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