asan: score: global-buffer-overflow
[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 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 /* 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, block_enum kind,
144 const char *name, 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
232 (struct objfile *objfile,
233 const lookup_name_info &name, domain_enum domain,
234 int global,
235 gdb::function_view<symbol_found_callback_ftype> callback,
236 symbol_compare_ftype *ordered_compare)
237 {
238 const struct debug_sym_fns_data *debug_data
239 = symfile_debug_objfile_data_key.get (objfile);
240
241 fprintf_filtered (gdb_stdlog,
242 "qf->map_matching_symbols (%s, %s, %d, %s)\n",
243 objfile_debug_name (objfile),
244 domain_name (domain), global,
245 host_address_to_string (ordered_compare));
246
247 debug_data->real_sf->qf->map_matching_symbols (objfile, name,
248 domain, global,
249 callback,
250 ordered_compare);
251 }
252
253 static void
254 debug_qf_expand_symtabs_matching
255 (struct objfile *objfile,
256 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
257 const lookup_name_info &lookup_name,
258 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
259 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
260 enum search_domain kind)
261 {
262 const struct debug_sym_fns_data *debug_data
263 = symfile_debug_objfile_data_key.get (objfile);
264
265 fprintf_filtered (gdb_stdlog,
266 "qf->expand_symtabs_matching (%s, %s, %s, %s, %s)\n",
267 objfile_debug_name (objfile),
268 host_address_to_string (&file_matcher),
269 host_address_to_string (&symbol_matcher),
270 host_address_to_string (&expansion_notify),
271 search_domain_name (kind));
272
273 debug_data->real_sf->qf->expand_symtabs_matching (objfile,
274 file_matcher,
275 lookup_name,
276 symbol_matcher,
277 expansion_notify,
278 kind);
279 }
280
281 static struct compunit_symtab *
282 debug_qf_find_pc_sect_compunit_symtab (struct objfile *objfile,
283 struct bound_minimal_symbol msymbol,
284 CORE_ADDR pc,
285 struct obj_section *section,
286 int warn_if_readin)
287 {
288 const struct debug_sym_fns_data *debug_data
289 = symfile_debug_objfile_data_key.get (objfile);
290 struct compunit_symtab *retval;
291
292 fprintf_filtered (gdb_stdlog,
293 "qf->find_pc_sect_compunit_symtab (%s, %s, %s, %s, %d)\n",
294 objfile_debug_name (objfile),
295 host_address_to_string (msymbol.minsym),
296 hex_string (pc),
297 host_address_to_string (section),
298 warn_if_readin);
299
300 retval
301 = debug_data->real_sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
302 pc, section,
303 warn_if_readin);
304
305 fprintf_filtered (gdb_stdlog,
306 "qf->find_pc_sect_compunit_symtab (...) = %s\n",
307 retval
308 ? debug_symtab_name (compunit_primary_filetab (retval))
309 : "NULL");
310
311 return retval;
312 }
313
314 static void
315 debug_qf_map_symbol_filenames (struct objfile *objfile,
316 symbol_filename_ftype *fun, void *data,
317 int need_fullname)
318 {
319 const struct debug_sym_fns_data *debug_data
320 = symfile_debug_objfile_data_key.get (objfile);
321 fprintf_filtered (gdb_stdlog,
322 "qf->map_symbol_filenames (%s, %s, %s, %d)\n",
323 objfile_debug_name (objfile),
324 host_address_to_string (fun),
325 host_address_to_string (data),
326 need_fullname);
327
328 debug_data->real_sf->qf->map_symbol_filenames (objfile, fun, data,
329 need_fullname);
330 }
331
332 static struct compunit_symtab *
333 debug_qf_find_compunit_symtab_by_address (struct objfile *objfile,
334 CORE_ADDR address)
335 {
336 const struct debug_sym_fns_data *debug_data
337 = symfile_debug_objfile_data_key.get (objfile);
338 fprintf_filtered (gdb_stdlog,
339 "qf->find_compunit_symtab_by_address (%s, %s)\n",
340 objfile_debug_name (objfile),
341 hex_string (address));
342
343 struct compunit_symtab *result = NULL;
344 if (debug_data->real_sf->qf->map_symbol_filenames != NULL)
345 result
346 = debug_data->real_sf->qf->find_compunit_symtab_by_address (objfile,
347 address);
348
349 fprintf_filtered (gdb_stdlog,
350 "qf->find_compunit_symtab_by_address (...) = %s\n",
351 result
352 ? debug_symtab_name (compunit_primary_filetab (result))
353 : "NULL");
354
355 return result;
356 }
357
358 static const struct quick_symbol_functions debug_sym_quick_functions =
359 {
360 debug_qf_has_symbols,
361 debug_qf_find_last_source_symtab,
362 debug_qf_forget_cached_source_info,
363 debug_qf_map_symtabs_matching_filename,
364 debug_qf_lookup_symbol,
365 debug_qf_print_stats,
366 debug_qf_dump,
367 debug_qf_expand_symtabs_for_function,
368 debug_qf_expand_all_symtabs,
369 debug_qf_expand_symtabs_with_fullname,
370 debug_qf_map_matching_symbols,
371 debug_qf_expand_symtabs_matching,
372 debug_qf_find_pc_sect_compunit_symtab,
373 debug_qf_find_compunit_symtab_by_address,
374 debug_qf_map_symbol_filenames
375 };
376 \f
377 /* Debugging version of struct sym_probe_fns. */
378
379 static const std::vector<std::unique_ptr<probe>> &
380 debug_sym_get_probes (struct objfile *objfile)
381 {
382 const struct debug_sym_fns_data *debug_data
383 = symfile_debug_objfile_data_key.get (objfile);
384
385 const std::vector<std::unique_ptr<probe>> &retval
386 = debug_data->real_sf->sym_probe_fns->sym_get_probes (objfile);
387
388 fprintf_filtered (gdb_stdlog,
389 "probes->sym_get_probes (%s) = %s\n",
390 objfile_debug_name (objfile),
391 host_address_to_string (retval.data ()));
392
393 return retval;
394 }
395
396 static const struct sym_probe_fns debug_sym_probe_fns =
397 {
398 debug_sym_get_probes,
399 };
400 \f
401 /* Debugging version of struct sym_fns. */
402
403 static void
404 debug_sym_new_init (struct objfile *objfile)
405 {
406 const struct debug_sym_fns_data *debug_data
407 = symfile_debug_objfile_data_key.get (objfile);
408
409 fprintf_filtered (gdb_stdlog, "sf->sym_new_init (%s)\n",
410 objfile_debug_name (objfile));
411
412 debug_data->real_sf->sym_new_init (objfile);
413 }
414
415 static void
416 debug_sym_init (struct objfile *objfile)
417 {
418 const struct debug_sym_fns_data *debug_data
419 = symfile_debug_objfile_data_key.get (objfile);
420
421 fprintf_filtered (gdb_stdlog, "sf->sym_init (%s)\n",
422 objfile_debug_name (objfile));
423
424 debug_data->real_sf->sym_init (objfile);
425 }
426
427 static void
428 debug_sym_read (struct objfile *objfile, symfile_add_flags symfile_flags)
429 {
430 const struct debug_sym_fns_data *debug_data
431 = symfile_debug_objfile_data_key.get (objfile);
432
433 fprintf_filtered (gdb_stdlog, "sf->sym_read (%s, 0x%x)\n",
434 objfile_debug_name (objfile), (unsigned) symfile_flags);
435
436 debug_data->real_sf->sym_read (objfile, symfile_flags);
437 }
438
439 static void
440 debug_sym_read_psymbols (struct objfile *objfile)
441 {
442 const struct debug_sym_fns_data *debug_data
443 = symfile_debug_objfile_data_key.get (objfile);
444
445 fprintf_filtered (gdb_stdlog, "sf->sym_read_psymbols (%s)\n",
446 objfile_debug_name (objfile));
447
448 debug_data->real_sf->sym_read_psymbols (objfile);
449 }
450
451 static void
452 debug_sym_finish (struct objfile *objfile)
453 {
454 const struct debug_sym_fns_data *debug_data
455 = symfile_debug_objfile_data_key.get (objfile);
456
457 fprintf_filtered (gdb_stdlog, "sf->sym_finish (%s)\n",
458 objfile_debug_name (objfile));
459
460 debug_data->real_sf->sym_finish (objfile);
461 }
462
463 static void
464 debug_sym_offsets (struct objfile *objfile,
465 const section_addr_info &info)
466 {
467 const struct debug_sym_fns_data *debug_data
468 = symfile_debug_objfile_data_key.get (objfile);
469
470 fprintf_filtered (gdb_stdlog, "sf->sym_offsets (%s, %s)\n",
471 objfile_debug_name (objfile),
472 host_address_to_string (&info));
473
474 debug_data->real_sf->sym_offsets (objfile, info);
475 }
476
477 static struct symfile_segment_data *
478 debug_sym_segments (bfd *abfd)
479 {
480 /* This API function is annoying, it doesn't take a "this" pointer.
481 Fortunately it is only used in one place where we (re-)lookup the
482 sym_fns table to use. Thus we will never be called. */
483 gdb_assert_not_reached ("debug_sym_segments called");
484 }
485
486 static void
487 debug_sym_read_linetable (struct objfile *objfile)
488 {
489 const struct debug_sym_fns_data *debug_data
490 = symfile_debug_objfile_data_key.get (objfile);
491
492 fprintf_filtered (gdb_stdlog, "sf->sym_read_linetable (%s)\n",
493 objfile_debug_name (objfile));
494
495 debug_data->real_sf->sym_read_linetable (objfile);
496 }
497
498 static bfd_byte *
499 debug_sym_relocate (struct objfile *objfile, asection *sectp, bfd_byte *buf)
500 {
501 const struct debug_sym_fns_data *debug_data
502 = symfile_debug_objfile_data_key.get (objfile);
503 bfd_byte *retval;
504
505 retval = debug_data->real_sf->sym_relocate (objfile, sectp, buf);
506
507 fprintf_filtered (gdb_stdlog,
508 "sf->sym_relocate (%s, %s, %s) = %s\n",
509 objfile_debug_name (objfile),
510 host_address_to_string (sectp),
511 host_address_to_string (buf),
512 host_address_to_string (retval));
513
514 return retval;
515 }
516
517 /* Template of debugging version of struct sym_fns.
518 A copy is made, with sym_flavour updated, and a pointer to the real table
519 installed in real_sf, and then a pointer to the copy is installed in the
520 objfile. */
521
522 static const struct sym_fns debug_sym_fns =
523 {
524 debug_sym_new_init,
525 debug_sym_init,
526 debug_sym_read,
527 debug_sym_read_psymbols,
528 debug_sym_finish,
529 debug_sym_offsets,
530 debug_sym_segments,
531 debug_sym_read_linetable,
532 debug_sym_relocate,
533 &debug_sym_probe_fns,
534 &debug_sym_quick_functions
535 };
536 \f
537 /* Install the debugging versions of the symfile functions for OBJFILE.
538 Do not call this if the debug versions are already installed. */
539
540 static void
541 install_symfile_debug_logging (struct objfile *objfile)
542 {
543 const struct sym_fns *real_sf;
544 struct debug_sym_fns_data *debug_data;
545
546 /* The debug versions should not already be installed. */
547 gdb_assert (!symfile_debug_installed (objfile));
548
549 real_sf = objfile->sf;
550
551 /* Alas we have to preserve NULL entries in REAL_SF. */
552 debug_data = new struct debug_sym_fns_data;
553
554 #define COPY_SF_PTR(from, to, name, func) \
555 do { \
556 if ((from)->name) \
557 (to)->debug_sf.name = func; \
558 } while (0)
559
560 COPY_SF_PTR (real_sf, debug_data, sym_new_init, debug_sym_new_init);
561 COPY_SF_PTR (real_sf, debug_data, sym_init, debug_sym_init);
562 COPY_SF_PTR (real_sf, debug_data, sym_read, debug_sym_read);
563 COPY_SF_PTR (real_sf, debug_data, sym_read_psymbols,
564 debug_sym_read_psymbols);
565 COPY_SF_PTR (real_sf, debug_data, sym_finish, debug_sym_finish);
566 COPY_SF_PTR (real_sf, debug_data, sym_offsets, debug_sym_offsets);
567 COPY_SF_PTR (real_sf, debug_data, sym_segments, debug_sym_segments);
568 COPY_SF_PTR (real_sf, debug_data, sym_read_linetable,
569 debug_sym_read_linetable);
570 COPY_SF_PTR (real_sf, debug_data, sym_relocate, debug_sym_relocate);
571 if (real_sf->sym_probe_fns)
572 debug_data->debug_sf.sym_probe_fns = &debug_sym_probe_fns;
573 debug_data->debug_sf.qf = &debug_sym_quick_functions;
574
575 #undef COPY_SF_PTR
576
577 debug_data->real_sf = real_sf;
578 symfile_debug_objfile_data_key.set (objfile, debug_data);
579 objfile->sf = &debug_data->debug_sf;
580 }
581
582 /* Uninstall the debugging versions of the symfile functions for OBJFILE.
583 Do not call this if the debug versions are not installed. */
584
585 static void
586 uninstall_symfile_debug_logging (struct objfile *objfile)
587 {
588 struct debug_sym_fns_data *debug_data;
589
590 /* The debug versions should be currently installed. */
591 gdb_assert (symfile_debug_installed (objfile));
592
593 debug_data = symfile_debug_objfile_data_key.get (objfile);
594
595 objfile->sf = debug_data->real_sf;
596 symfile_debug_objfile_data_key.clear (objfile);
597 }
598
599 /* Call this function to set OBJFILE->SF.
600 Do not set OBJFILE->SF directly. */
601
602 void
603 objfile_set_sym_fns (struct objfile *objfile, const struct sym_fns *sf)
604 {
605 if (symfile_debug_installed (objfile))
606 {
607 gdb_assert (debug_symfile);
608 /* Remove the current one, and reinstall a new one later. */
609 uninstall_symfile_debug_logging (objfile);
610 }
611
612 /* Assume debug logging is disabled. */
613 objfile->sf = sf;
614
615 /* Turn debug logging on if enabled. */
616 if (debug_symfile)
617 install_symfile_debug_logging (objfile);
618 }
619
620 static void
621 set_debug_symfile (const char *args, int from_tty, struct cmd_list_element *c)
622 {
623 struct program_space *pspace;
624
625 ALL_PSPACES (pspace)
626 for (objfile *objfile : pspace->objfiles ())
627 {
628 if (debug_symfile)
629 {
630 if (!symfile_debug_installed (objfile))
631 install_symfile_debug_logging (objfile);
632 }
633 else
634 {
635 if (symfile_debug_installed (objfile))
636 uninstall_symfile_debug_logging (objfile);
637 }
638 }
639 }
640
641 static void
642 show_debug_symfile (struct ui_file *file, int from_tty,
643 struct cmd_list_element *c, const char *value)
644 {
645 fprintf_filtered (file, _("Symfile debugging is %s.\n"), value);
646 }
647
648 void
649 _initialize_symfile_debug (void)
650 {
651 add_setshow_boolean_cmd ("symfile", no_class, &debug_symfile, _("\
652 Set debugging of the symfile functions."), _("\
653 Show debugging of the symfile functions."), _("\
654 When enabled, all calls to the symfile functions are logged."),
655 set_debug_symfile, show_debug_symfile,
656 &setdebuglist, &showdebuglist);
657
658 /* Note: We don't need a new-objfile observer because debug logging
659 will be installed when objfile init'n calls objfile_set_sym_fns. */
660 }
This page took 0.04723 seconds and 4 git commands to generate.