1 /* Do various things to symbol tables (other than lookup), for GDB.
3 Copyright (C) 1986-2013 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "filenames.h"
27 #include "breakpoint.h"
29 #include "gdb_obstack.h"
30 #include "exceptions.h"
34 #include "gdb_regex.h"
36 #include "dictionary.h"
37 #include "typeprint.h"
41 #include "gdb_string.h"
42 #include "readline/readline.h"
47 #define DEV_TTY "/dev/tty"
50 /* Unfortunately for debugging, stderr is usually a macro. This is painful
51 when calling functions that take FILE *'s from the debugger.
52 So we make a variable which has the same value and which is accessible when
53 debugging GDB with itself. Because stdin et al need not be constants,
54 we initialize them in the _initialize_symmisc function at the bottom
60 /* Prototypes for local functions */
62 static void dump_symtab (struct objfile *, struct symtab *,
65 static void dump_msymbols (struct objfile *, struct ui_file *);
67 static void dump_objfile (struct objfile *);
69 static int block_depth (struct block *);
71 void _initialize_symmisc (void);
73 struct print_symbol_args
75 struct gdbarch *gdbarch;
76 struct symbol *symbol;
78 struct ui_file *outfile;
81 static int print_symbol (void *);
85 print_symbol_bcache_statistics (void)
87 struct program_space *pspace;
88 struct objfile *objfile;
91 ALL_PSPACE_OBJFILES (pspace, objfile)
94 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
95 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
96 "partial symbol cache");
97 print_bcache_statistics (objfile->per_bfd->macro_cache,
98 "preprocessor macro cache");
99 print_bcache_statistics (objfile->per_bfd->filename_cache,
105 print_objfile_statistics (void)
107 struct program_space *pspace;
108 struct objfile *objfile;
110 int i, linetables, blockvectors;
113 ALL_PSPACE_OBJFILES (pspace, objfile)
116 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
117 if (OBJSTAT (objfile, n_stabs) > 0)
118 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
119 OBJSTAT (objfile, n_stabs));
120 if (OBJSTAT (objfile, n_minsyms) > 0)
121 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
122 OBJSTAT (objfile, n_minsyms));
123 if (OBJSTAT (objfile, n_psyms) > 0)
124 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
125 OBJSTAT (objfile, n_psyms));
126 if (OBJSTAT (objfile, n_syms) > 0)
127 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
128 OBJSTAT (objfile, n_syms));
129 if (OBJSTAT (objfile, n_types) > 0)
130 printf_filtered (_(" Number of \"types\" defined: %d\n"),
131 OBJSTAT (objfile, n_types));
133 objfile->sf->qf->print_stats (objfile);
134 i = linetables = blockvectors = 0;
135 ALL_OBJFILE_SYMTABS (objfile, s)
138 if (s->linetable != NULL)
143 printf_filtered (_(" Number of symbol tables: %d\n"), i);
144 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
146 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
149 if (OBJSTAT (objfile, sz_strtab) > 0)
150 printf_filtered (_(" Space used by a.out string tables: %d\n"),
151 OBJSTAT (objfile, sz_strtab));
152 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
153 obstack_memory_used (&objfile->objfile_obstack));
154 printf_filtered (_(" Total memory used for BFD obstack: %d\n"),
155 obstack_memory_used (&objfile->per_bfd->storage_obstack));
156 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
157 bcache_memory_used (psymbol_bcache_get_bcache
158 (objfile->psymbol_cache)));
159 printf_filtered (_(" Total memory used for macro cache: %d\n"),
160 bcache_memory_used (objfile->per_bfd->macro_cache));
161 printf_filtered (_(" Total memory used for file name cache: %d\n"),
162 bcache_memory_used (objfile->per_bfd->filename_cache));
167 dump_objfile (struct objfile *objfile)
169 struct symtab *symtab;
171 printf_filtered ("\nObject file %s: ", objfile->name);
172 printf_filtered ("Objfile at ");
173 gdb_print_host_address (objfile, gdb_stdout);
174 printf_filtered (", bfd at ");
175 gdb_print_host_address (objfile->obfd, gdb_stdout);
176 printf_filtered (", %d minsyms\n\n",
177 objfile->minimal_symbol_count);
180 objfile->sf->qf->dump (objfile);
182 if (objfile->symtabs)
184 printf_filtered ("Symtabs:\n");
185 for (symtab = objfile->symtabs;
187 symtab = symtab->next)
189 printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
190 gdb_print_host_address (symtab, gdb_stdout);
191 printf_filtered (", ");
192 if (symtab->objfile != objfile)
194 printf_filtered ("NOT ON CHAIN! ");
198 printf_filtered ("\n\n");
202 /* Print minimal symbols from this objfile. */
205 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
207 struct gdbarch *gdbarch = get_objfile_arch (objfile);
208 struct minimal_symbol *msymbol;
212 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
213 if (objfile->minimal_symbol_count == 0)
215 fprintf_filtered (outfile, "No minimal symbols found.\n");
219 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
221 struct obj_section *section = SYMBOL_OBJ_SECTION (objfile, msymbol);
223 switch (MSYMBOL_TYPE (msymbol))
231 case mst_text_gnu_ifunc:
234 case mst_solib_trampoline:
259 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
260 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
262 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
265 if (section->the_bfd_section != NULL)
266 fprintf_filtered (outfile, " section %s",
267 bfd_section_name (objfile->obfd,
268 section->the_bfd_section));
270 fprintf_filtered (outfile, " spurious section %ld",
271 (long) (section - objfile->sections));
273 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
275 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
277 if (msymbol->filename)
278 fprintf_filtered (outfile, " %s", msymbol->filename);
279 fputs_filtered ("\n", outfile);
282 if (objfile->minimal_symbol_count != index)
284 warning (_("internal error: minimal symbol count %d != %d"),
285 objfile->minimal_symbol_count, index);
287 fprintf_filtered (outfile, "\n");
291 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
292 struct ui_file *outfile)
294 struct gdbarch *gdbarch = get_objfile_arch (objfile);
296 struct dict_iterator iter;
299 struct blockvector *bv;
304 fprintf_filtered (outfile, "\nSymtab for file %s\n",
305 symtab_to_filename_for_display (symtab));
307 fprintf_filtered (outfile, "Compilation directory is %s\n",
309 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
310 gdb_print_host_address (objfile, outfile);
311 fprintf_filtered (outfile, ")\n");
312 fprintf_filtered (outfile, "Language: %s\n",
313 language_str (symtab->language));
315 /* First print the line table. */
316 l = LINETABLE (symtab);
319 fprintf_filtered (outfile, "\nLine table:\n\n");
321 for (i = 0; i < len; i++)
323 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
324 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
325 fprintf_filtered (outfile, "\n");
328 /* Now print the block info, but only for primary symtabs since we will
329 print lots of duplicate info otherwise. */
332 fprintf_filtered (outfile, "\nBlockvector:\n\n");
333 bv = BLOCKVECTOR (symtab);
334 len = BLOCKVECTOR_NBLOCKS (bv);
335 for (i = 0; i < len; i++)
337 b = BLOCKVECTOR_BLOCK (bv, i);
338 depth = block_depth (b) * 2;
339 print_spaces (depth, outfile);
340 fprintf_filtered (outfile, "block #%03d, object at ", i);
341 gdb_print_host_address (b, outfile);
342 if (BLOCK_SUPERBLOCK (b))
344 fprintf_filtered (outfile, " under ");
345 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
347 /* drow/2002-07-10: We could save the total symbols count
348 even if we're using a hashtable, but nothing else but this message
350 fprintf_filtered (outfile, ", %d syms/buckets in ",
351 dict_size (BLOCK_DICT (b)));
352 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
353 fprintf_filtered (outfile, "..");
354 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
355 if (BLOCK_FUNCTION (b))
357 fprintf_filtered (outfile, ", function %s",
358 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
359 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
361 fprintf_filtered (outfile, ", %s",
362 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
365 fprintf_filtered (outfile, "\n");
366 /* Now print each symbol in this block (in no particular order, if
367 we're using a hashtable). Note that we only want this
368 block, not any blocks from included symtabs. */
369 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
371 struct print_symbol_args s;
377 catch_errors (print_symbol, &s, "Error printing symbol:\n",
381 fprintf_filtered (outfile, "\n");
385 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
390 dump_symtab (struct objfile *objfile, struct symtab *symtab,
391 struct ui_file *outfile)
393 /* Set the current language to the language of the symtab we're dumping
394 because certain routines used during dump_symtab() use the current
395 language to print an image of the symbol. We'll restore it later.
396 But use only real languages, not placeholders. */
397 if (symtab->language != language_unknown
398 && symtab->language != language_auto)
400 enum language saved_lang;
402 saved_lang = set_language (symtab->language);
404 dump_symtab_1 (objfile, symtab, outfile);
406 set_language (saved_lang);
409 dump_symtab_1 (objfile, symtab, outfile);
413 maintenance_print_symbols (char *args, int from_tty)
416 struct ui_file *outfile;
417 struct cleanup *cleanups;
418 char *symname = NULL;
419 char *filename = DEV_TTY;
420 struct objfile *objfile;
427 error (_("Arguments missing: an output file name "
428 "and an optional symbol file name"));
430 argv = gdb_buildargv (args);
431 cleanups = make_cleanup_freeargv (argv);
436 /* If a second arg is supplied, it is a source file name to match on. */
443 filename = tilde_expand (filename);
444 make_cleanup (xfree, filename);
446 outfile = gdb_fopen (filename, FOPEN_WT);
448 perror_with_name (filename);
449 make_cleanup_ui_file_delete (outfile);
451 ALL_SYMTABS (objfile, s)
455 || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
456 dump_symtab (objfile, s, outfile);
458 do_cleanups (cleanups);
461 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
462 far to indent. ARGS is really a struct print_symbol_args *, but is
463 declared as char * to get it past catch_errors. Returns 0 for error,
467 print_symbol (void *args)
469 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
470 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
471 int depth = ((struct print_symbol_args *) args)->depth;
472 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
473 struct obj_section *section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (symbol),
476 print_spaces (depth, outfile);
477 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
479 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
480 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
483 fprintf_filtered (outfile, " section %s\n",
484 bfd_section_name (section->the_bfd_section->owner,
485 section->the_bfd_section));
487 fprintf_filtered (outfile, "\n");
490 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
492 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
494 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
495 &type_print_raw_options);
499 fprintf_filtered (outfile, "%s %s = ",
500 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
502 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
503 ? "struct" : "union")),
504 SYMBOL_LINKAGE_NAME (symbol));
505 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
506 &type_print_raw_options);
508 fprintf_filtered (outfile, ";\n");
512 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
513 fprintf_filtered (outfile, "typedef ");
514 if (SYMBOL_TYPE (symbol))
516 /* Print details of types, except for enums where it's clutter. */
517 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
519 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
521 &type_print_raw_options);
522 fprintf_filtered (outfile, "; ");
525 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
527 switch (SYMBOL_CLASS (symbol))
530 fprintf_filtered (outfile, "const %s (%s)",
531 plongest (SYMBOL_VALUE (symbol)),
532 hex_string (SYMBOL_VALUE (symbol)));
535 case LOC_CONST_BYTES:
538 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
540 fprintf_filtered (outfile, "const %u hex bytes:",
542 for (i = 0; i < TYPE_LENGTH (type); i++)
543 fprintf_filtered (outfile, " %02x",
544 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
549 fprintf_filtered (outfile, "static at ");
550 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
553 fprintf_filtered (outfile, " section %s",
554 bfd_section_name (section->the_bfd_section->owner,
555 section->the_bfd_section));
559 if (SYMBOL_IS_ARGUMENT (symbol))
560 fprintf_filtered (outfile, "parameter register %s",
561 plongest (SYMBOL_VALUE (symbol)));
563 fprintf_filtered (outfile, "register %s",
564 plongest (SYMBOL_VALUE (symbol)));
568 fprintf_filtered (outfile, "arg at offset %s",
569 hex_string (SYMBOL_VALUE (symbol)));
573 fprintf_filtered (outfile, "reference arg at %s",
574 hex_string (SYMBOL_VALUE (symbol)));
577 case LOC_REGPARM_ADDR:
578 fprintf_filtered (outfile, "address parameter register %s",
579 plongest (SYMBOL_VALUE (symbol)));
583 fprintf_filtered (outfile, "local at offset %s",
584 hex_string (SYMBOL_VALUE (symbol)));
591 fprintf_filtered (outfile, "label at ");
592 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
595 fprintf_filtered (outfile, " section %s",
596 bfd_section_name (section->the_bfd_section->owner,
597 section->the_bfd_section));
601 fprintf_filtered (outfile, "block object ");
602 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
603 fprintf_filtered (outfile, ", ");
604 fputs_filtered (paddress (gdbarch,
605 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
607 fprintf_filtered (outfile, "..");
608 fputs_filtered (paddress (gdbarch,
609 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
612 fprintf_filtered (outfile, " section %s",
613 bfd_section_name (section->the_bfd_section->owner,
614 section->the_bfd_section));
618 fprintf_filtered (outfile, "computed at runtime");
622 fprintf_filtered (outfile, "unresolved");
625 case LOC_OPTIMIZED_OUT:
626 fprintf_filtered (outfile, "optimized out");
630 fprintf_filtered (outfile, "botched symbol class %x",
631 SYMBOL_CLASS (symbol));
635 fprintf_filtered (outfile, "\n");
640 maintenance_print_msymbols (char *args, int from_tty)
643 struct ui_file *outfile;
644 struct cleanup *cleanups;
645 char *filename = DEV_TTY;
646 char *symname = NULL;
647 struct program_space *pspace;
648 struct objfile *objfile;
650 struct stat sym_st, obj_st;
656 error (_("print-msymbols takes an output file "
657 "name and optional symbol file name"));
659 argv = gdb_buildargv (args);
660 cleanups = make_cleanup_freeargv (argv);
665 /* If a second arg is supplied, it is a source file name to match on. */
668 symname = gdb_realpath (argv[1]);
669 make_cleanup (xfree, symname);
670 if (symname && stat (symname, &sym_st))
671 perror_with_name (symname);
675 filename = tilde_expand (filename);
676 make_cleanup (xfree, filename);
678 outfile = gdb_fopen (filename, FOPEN_WT);
680 perror_with_name (filename);
681 make_cleanup_ui_file_delete (outfile);
684 ALL_PSPACE_OBJFILES (pspace, objfile)
687 if (symname == NULL || (!stat (objfile->name, &obj_st)
688 && sym_st.st_dev == obj_st.st_dev
689 && sym_st.st_ino == obj_st.st_ino))
690 dump_msymbols (objfile, outfile);
692 fprintf_filtered (outfile, "\n\n");
693 do_cleanups (cleanups);
697 maintenance_print_objfiles (char *ignore, int from_tty)
699 struct program_space *pspace;
700 struct objfile *objfile;
705 ALL_PSPACE_OBJFILES (pspace, objfile)
708 dump_objfile (objfile);
712 /* List all the symbol tables whose names match REGEXP (optional). */
715 maintenance_info_symtabs (char *regexp, int from_tty)
717 struct program_space *pspace;
718 struct objfile *objfile;
724 ALL_PSPACE_OBJFILES (pspace, objfile)
726 struct symtab *symtab;
728 /* We don't want to print anything for this objfile until we
729 actually find a symtab whose name matches. */
730 int printed_objfile_start = 0;
732 ALL_OBJFILE_SYMTABS (objfile, symtab)
737 || re_exec (symtab_to_filename_for_display (symtab)))
739 if (! printed_objfile_start)
741 printf_filtered ("{ objfile %s ", objfile->name);
743 printf_filtered ("((struct objfile *) %s)\n",
744 host_address_to_string (objfile));
745 printed_objfile_start = 1;
748 printf_filtered (" { symtab %s ",
749 symtab_to_filename_for_display (symtab));
751 printf_filtered ("((struct symtab *) %s)\n",
752 host_address_to_string (symtab));
753 printf_filtered (" dirname %s\n",
754 symtab->dirname ? symtab->dirname : "(null)");
755 printf_filtered (" fullname %s\n",
756 symtab->fullname ? symtab->fullname : "(null)");
758 "blockvector ((struct blockvector *) %s)%s\n",
759 host_address_to_string (symtab->blockvector),
760 symtab->primary ? " (primary)" : "");
762 "linetable ((struct linetable *) %s)\n",
763 host_address_to_string (symtab->linetable));
764 printf_filtered (" debugformat %s\n",
765 symtab->debugformat);
766 printf_filtered (" }\n");
770 if (printed_objfile_start)
771 printf_filtered ("}\n");
776 /* Return the nexting depth of a block within other blocks in its symtab. */
779 block_depth (struct block *block)
783 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
791 /* Do early runtime initializations. */
794 _initialize_symmisc (void)
800 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
801 Print dump of current symbol definitions.\n\
802 Entries in the full symbol table are dumped to file OUTFILE.\n\
803 If a SOURCE file is specified, dump only that file's symbols."),
804 &maintenanceprintlist);
806 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
807 Print dump of current minimal symbol definitions.\n\
808 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
809 If a SOURCE file is specified, dump only that file's minimal symbols."),
810 &maintenanceprintlist);
812 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
813 _("Print dump of current object file definitions."),
814 &maintenanceprintlist);
816 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
817 List the full symbol tables for all object files.\n\
818 This does not include information about individual symbols, blocks, or\n\
819 linetables --- just the symbol table structures themselves.\n\
820 With an argument REGEXP, list the symbol tables whose names that match that."),
821 &maintenanceinfolist);