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