1 /* Do various things to symbol tables (other than lookup), for GDB.
3 Copyright (C) 1986-2000, 2002-2004, 2007-2012 Free Software
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "filenames.h"
28 #include "breakpoint.h"
30 #include "gdb_obstack.h"
31 #include "exceptions.h"
35 #include "gdb_regex.h"
37 #include "dictionary.h"
38 #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->filename);
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 (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));
264 fprintf_filtered (outfile, " section %s",
265 bfd_section_name (objfile->obfd,
266 section->the_bfd_section));
267 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
269 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
271 if (msymbol->filename)
272 fprintf_filtered (outfile, " %s", msymbol->filename);
273 fputs_filtered ("\n", outfile);
276 if (objfile->minimal_symbol_count != index)
278 warning (_("internal error: minimal symbol count %d != %d"),
279 objfile->minimal_symbol_count, index);
281 fprintf_filtered (outfile, "\n");
285 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
286 struct ui_file *outfile)
288 struct gdbarch *gdbarch = get_objfile_arch (objfile);
290 struct dict_iterator iter;
293 struct blockvector *bv;
298 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
300 fprintf_filtered (outfile, "Compilation directory is %s\n",
302 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
303 gdb_print_host_address (objfile, outfile);
304 fprintf_filtered (outfile, ")\n");
305 fprintf_filtered (outfile, "Language: %s\n",
306 language_str (symtab->language));
308 /* First print the line table. */
309 l = LINETABLE (symtab);
312 fprintf_filtered (outfile, "\nLine table:\n\n");
314 for (i = 0; i < len; i++)
316 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
317 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
318 fprintf_filtered (outfile, "\n");
321 /* Now print the block info, but only for primary symtabs since we will
322 print lots of duplicate info otherwise. */
325 fprintf_filtered (outfile, "\nBlockvector:\n\n");
326 bv = BLOCKVECTOR (symtab);
327 len = BLOCKVECTOR_NBLOCKS (bv);
328 for (i = 0; i < len; i++)
330 b = BLOCKVECTOR_BLOCK (bv, i);
331 depth = block_depth (b) * 2;
332 print_spaces (depth, outfile);
333 fprintf_filtered (outfile, "block #%03d, object at ", i);
334 gdb_print_host_address (b, outfile);
335 if (BLOCK_SUPERBLOCK (b))
337 fprintf_filtered (outfile, " under ");
338 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
340 /* drow/2002-07-10: We could save the total symbols count
341 even if we're using a hashtable, but nothing else but this message
343 fprintf_filtered (outfile, ", %d syms/buckets in ",
344 dict_size (BLOCK_DICT (b)));
345 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
346 fprintf_filtered (outfile, "..");
347 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
348 if (BLOCK_FUNCTION (b))
350 fprintf_filtered (outfile, ", function %s",
351 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
352 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
354 fprintf_filtered (outfile, ", %s",
355 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
358 fprintf_filtered (outfile, "\n");
359 /* Now print each symbol in this block (in no particular order, if
360 we're using a hashtable). Note that we only want this
361 block, not any blocks from included symtabs. */
362 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
364 struct print_symbol_args s;
370 catch_errors (print_symbol, &s, "Error printing symbol:\n",
374 fprintf_filtered (outfile, "\n");
378 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
383 dump_symtab (struct objfile *objfile, struct symtab *symtab,
384 struct ui_file *outfile)
386 /* Set the current language to the language of the symtab we're dumping
387 because certain routines used during dump_symtab() use the current
388 language to print an image of the symbol. We'll restore it later.
389 But use only real languages, not placeholders. */
390 if (symtab->language != language_unknown
391 && symtab->language != language_auto)
393 enum language saved_lang;
395 saved_lang = set_language (symtab->language);
397 dump_symtab_1 (objfile, symtab, outfile);
399 set_language (saved_lang);
402 dump_symtab_1 (objfile, symtab, outfile);
406 maintenance_print_symbols (char *args, int from_tty)
409 struct ui_file *outfile;
410 struct cleanup *cleanups;
411 char *symname = NULL;
412 char *filename = DEV_TTY;
413 struct objfile *objfile;
420 error (_("Arguments missing: an output file name "
421 "and an optional symbol file name"));
423 argv = gdb_buildargv (args);
424 cleanups = make_cleanup_freeargv (argv);
429 /* If a second arg is supplied, it is a source file name to match on. */
436 filename = tilde_expand (filename);
437 make_cleanup (xfree, filename);
439 outfile = gdb_fopen (filename, FOPEN_WT);
441 perror_with_name (filename);
442 make_cleanup_ui_file_delete (outfile);
444 ALL_SYMTABS (objfile, s)
447 if (symname == NULL || filename_cmp (symname, s->filename) == 0)
448 dump_symtab (objfile, s, outfile);
450 do_cleanups (cleanups);
453 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
454 far to indent. ARGS is really a struct print_symbol_args *, but is
455 declared as char * to get it past catch_errors. Returns 0 for error,
459 print_symbol (void *args)
461 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
462 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
463 int depth = ((struct print_symbol_args *) args)->depth;
464 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
465 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
467 print_spaces (depth, outfile);
468 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
470 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
471 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
474 fprintf_filtered (outfile, " section %s\n",
475 bfd_section_name (section->the_bfd_section->owner,
476 section->the_bfd_section));
478 fprintf_filtered (outfile, "\n");
481 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
483 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
485 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
486 &type_print_raw_options);
490 fprintf_filtered (outfile, "%s %s = ",
491 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
493 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
494 ? "struct" : "union")),
495 SYMBOL_LINKAGE_NAME (symbol));
496 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
497 &type_print_raw_options);
499 fprintf_filtered (outfile, ";\n");
503 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
504 fprintf_filtered (outfile, "typedef ");
505 if (SYMBOL_TYPE (symbol))
507 /* Print details of types, except for enums where it's clutter. */
508 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
510 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
512 &type_print_raw_options);
513 fprintf_filtered (outfile, "; ");
516 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
518 switch (SYMBOL_CLASS (symbol))
521 fprintf_filtered (outfile, "const %s (%s)",
522 plongest (SYMBOL_VALUE (symbol)),
523 hex_string (SYMBOL_VALUE (symbol)));
526 case LOC_CONST_BYTES:
529 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
531 fprintf_filtered (outfile, "const %u hex bytes:",
533 for (i = 0; i < TYPE_LENGTH (type); i++)
534 fprintf_filtered (outfile, " %02x",
535 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
540 fprintf_filtered (outfile, "static at ");
541 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
544 fprintf_filtered (outfile, " section %s",
545 bfd_section_name (section->the_bfd_section->owner,
546 section->the_bfd_section));
550 if (SYMBOL_IS_ARGUMENT (symbol))
551 fprintf_filtered (outfile, "parameter register %s",
552 plongest (SYMBOL_VALUE (symbol)));
554 fprintf_filtered (outfile, "register %s",
555 plongest (SYMBOL_VALUE (symbol)));
559 fprintf_filtered (outfile, "arg at offset %s",
560 hex_string (SYMBOL_VALUE (symbol)));
564 fprintf_filtered (outfile, "reference arg at %s",
565 hex_string (SYMBOL_VALUE (symbol)));
568 case LOC_REGPARM_ADDR:
569 fprintf_filtered (outfile, "address parameter register %s",
570 plongest (SYMBOL_VALUE (symbol)));
574 fprintf_filtered (outfile, "local at offset %s",
575 hex_string (SYMBOL_VALUE (symbol)));
582 fprintf_filtered (outfile, "label at ");
583 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
586 fprintf_filtered (outfile, " section %s",
587 bfd_section_name (section->the_bfd_section->owner,
588 section->the_bfd_section));
592 fprintf_filtered (outfile, "block object ");
593 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
594 fprintf_filtered (outfile, ", ");
595 fputs_filtered (paddress (gdbarch,
596 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
598 fprintf_filtered (outfile, "..");
599 fputs_filtered (paddress (gdbarch,
600 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
603 fprintf_filtered (outfile, " section %s",
604 bfd_section_name (section->the_bfd_section->owner,
605 section->the_bfd_section));
609 fprintf_filtered (outfile, "computed at runtime");
613 fprintf_filtered (outfile, "unresolved");
616 case LOC_OPTIMIZED_OUT:
617 fprintf_filtered (outfile, "optimized out");
621 fprintf_filtered (outfile, "botched symbol class %x",
622 SYMBOL_CLASS (symbol));
626 fprintf_filtered (outfile, "\n");
631 maintenance_print_msymbols (char *args, int from_tty)
634 struct ui_file *outfile;
635 struct cleanup *cleanups;
636 char *filename = DEV_TTY;
637 char *symname = NULL;
638 struct program_space *pspace;
639 struct objfile *objfile;
641 struct stat sym_st, obj_st;
647 error (_("print-msymbols takes an output file "
648 "name and optional symbol file name"));
650 argv = gdb_buildargv (args);
651 cleanups = make_cleanup_freeargv (argv);
656 /* If a second arg is supplied, it is a source file name to match on. */
659 symname = xfullpath (argv[1]);
660 make_cleanup (xfree, symname);
661 if (symname && stat (symname, &sym_st))
662 perror_with_name (symname);
666 filename = tilde_expand (filename);
667 make_cleanup (xfree, filename);
669 outfile = gdb_fopen (filename, FOPEN_WT);
671 perror_with_name (filename);
672 make_cleanup_ui_file_delete (outfile);
675 ALL_PSPACE_OBJFILES (pspace, objfile)
678 if (symname == NULL || (!stat (objfile->name, &obj_st)
679 && sym_st.st_ino == obj_st.st_ino))
680 dump_msymbols (objfile, outfile);
682 fprintf_filtered (outfile, "\n\n");
683 do_cleanups (cleanups);
687 maintenance_print_objfiles (char *ignore, int from_tty)
689 struct program_space *pspace;
690 struct objfile *objfile;
695 ALL_PSPACE_OBJFILES (pspace, objfile)
698 dump_objfile (objfile);
702 /* List all the symbol tables whose names match REGEXP (optional). */
705 maintenance_info_symtabs (char *regexp, int from_tty)
707 struct program_space *pspace;
708 struct objfile *objfile;
714 ALL_PSPACE_OBJFILES (pspace, objfile)
716 struct symtab *symtab;
718 /* We don't want to print anything for this objfile until we
719 actually find a symtab whose name matches. */
720 int printed_objfile_start = 0;
722 ALL_OBJFILE_SYMTABS (objfile, symtab)
727 || re_exec (symtab->filename))
729 if (! printed_objfile_start)
731 printf_filtered ("{ objfile %s ", objfile->name);
733 printf_filtered ("((struct objfile *) %s)\n",
734 host_address_to_string (objfile));
735 printed_objfile_start = 1;
738 printf_filtered (" { symtab %s ", symtab->filename);
740 printf_filtered ("((struct symtab *) %s)\n",
741 host_address_to_string (symtab));
742 printf_filtered (" dirname %s\n",
743 symtab->dirname ? symtab->dirname : "(null)");
744 printf_filtered (" fullname %s\n",
745 symtab->fullname ? symtab->fullname : "(null)");
747 "blockvector ((struct blockvector *) %s)%s\n",
748 host_address_to_string (symtab->blockvector),
749 symtab->primary ? " (primary)" : "");
751 "linetable ((struct linetable *) %s)\n",
752 host_address_to_string (symtab->linetable));
753 printf_filtered (" debugformat %s\n",
754 symtab->debugformat);
755 printf_filtered (" }\n");
759 if (printed_objfile_start)
760 printf_filtered ("}\n");
765 /* Return the nexting depth of a block within other blocks in its symtab. */
768 block_depth (struct block *block)
772 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
780 /* Do early runtime initializations. */
783 _initialize_symmisc (void)
789 add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
790 Print dump of current symbol definitions.\n\
791 Entries in the full symbol table are dumped to file OUTFILE.\n\
792 If a SOURCE file is specified, dump only that file's symbols."),
793 &maintenanceprintlist);
795 add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
796 Print dump of current minimal symbol definitions.\n\
797 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
798 If a SOURCE file is specified, dump only that file's minimal symbols."),
799 &maintenanceprintlist);
801 add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
802 _("Print dump of current object file definitions."),
803 &maintenanceprintlist);
805 add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
806 List the full symbol tables for all object files.\n\
807 This does not include information about individual symbols, blocks, or\n\
808 linetables --- just the symbol table structures themselves.\n\
809 With an argument REGEXP, list the symbol tables whose names that match that."),
810 &maintenanceinfolist);