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"
40 #include "gdb_string.h"
41 #include "readline/readline.h"
46 #define DEV_TTY "/dev/tty"
49 /* Unfortunately for debugging, stderr is usually a macro. This is painful
50 when calling functions that take FILE *'s from the debugger.
51 So we make a variable which has the same value and which is accessible when
52 debugging GDB with itself. Because stdin et al need not be constants,
53 we initialize them in the _initialize_symmisc function at the bottom
59 /* Prototypes for local functions */
61 static void dump_symtab (struct objfile *, struct symtab *,
64 static void dump_msymbols (struct objfile *, struct ui_file *);
66 static void dump_objfile (struct objfile *);
68 static int block_depth (struct block *);
70 void _initialize_symmisc (void);
72 struct print_symbol_args
74 struct gdbarch *gdbarch;
75 struct symbol *symbol;
77 struct ui_file *outfile;
80 static int print_symbol (void *);
84 print_symbol_bcache_statistics (void)
86 struct program_space *pspace;
87 struct objfile *objfile;
90 ALL_PSPACE_OBJFILES (pspace, objfile)
93 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
94 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
95 "partial symbol cache");
96 print_bcache_statistics (objfile->per_bfd->macro_cache,
97 "preprocessor macro cache");
98 print_bcache_statistics (objfile->per_bfd->filename_cache,
104 print_objfile_statistics (void)
106 struct program_space *pspace;
107 struct objfile *objfile;
109 int i, linetables, blockvectors;
112 ALL_PSPACE_OBJFILES (pspace, objfile)
115 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
116 if (OBJSTAT (objfile, n_stabs) > 0)
117 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
118 OBJSTAT (objfile, n_stabs));
119 if (OBJSTAT (objfile, n_minsyms) > 0)
120 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
121 OBJSTAT (objfile, n_minsyms));
122 if (OBJSTAT (objfile, n_psyms) > 0)
123 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
124 OBJSTAT (objfile, n_psyms));
125 if (OBJSTAT (objfile, n_syms) > 0)
126 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
127 OBJSTAT (objfile, n_syms));
128 if (OBJSTAT (objfile, n_types) > 0)
129 printf_filtered (_(" Number of \"types\" defined: %d\n"),
130 OBJSTAT (objfile, n_types));
132 objfile->sf->qf->print_stats (objfile);
133 i = linetables = blockvectors = 0;
134 ALL_OBJFILE_SYMTABS (objfile, s)
137 if (s->linetable != NULL)
142 printf_filtered (_(" Number of symbol tables: %d\n"), i);
143 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
145 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
148 if (OBJSTAT (objfile, sz_strtab) > 0)
149 printf_filtered (_(" Space used by a.out string tables: %d\n"),
150 OBJSTAT (objfile, sz_strtab));
151 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
152 obstack_memory_used (&objfile->objfile_obstack));
153 printf_filtered (_(" Total memory used for BFD obstack: %d\n"),
154 obstack_memory_used (&objfile->per_bfd->storage_obstack));
155 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
156 bcache_memory_used (psymbol_bcache_get_bcache
157 (objfile->psymbol_cache)));
158 printf_filtered (_(" Total memory used for macro cache: %d\n"),
159 bcache_memory_used (objfile->per_bfd->macro_cache));
160 printf_filtered (_(" Total memory used for file name cache: %d\n"),
161 bcache_memory_used (objfile->per_bfd->filename_cache));
166 dump_objfile (struct objfile *objfile)
168 struct symtab *symtab;
170 printf_filtered ("\nObject file %s: ", objfile->name);
171 printf_filtered ("Objfile at ");
172 gdb_print_host_address (objfile, gdb_stdout);
173 printf_filtered (", bfd at ");
174 gdb_print_host_address (objfile->obfd, gdb_stdout);
175 printf_filtered (", %d minsyms\n\n",
176 objfile->minimal_symbol_count);
179 objfile->sf->qf->dump (objfile);
181 if (objfile->symtabs)
183 printf_filtered ("Symtabs:\n");
184 for (symtab = objfile->symtabs;
186 symtab = symtab->next)
188 printf_filtered ("%s at ", symtab->filename);
189 gdb_print_host_address (symtab, gdb_stdout);
190 printf_filtered (", ");
191 if (symtab->objfile != objfile)
193 printf_filtered ("NOT ON CHAIN! ");
197 printf_filtered ("\n\n");
201 /* Print minimal symbols from this objfile. */
204 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
207 struct minimal_symbol *msymbol;
211 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
212 if (objfile->minimal_symbol_count == 0)
214 fprintf_filtered (outfile, "No minimal symbols found.\n");
218 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
220 struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
222 switch (MSYMBOL_TYPE (msymbol))
230 case mst_text_gnu_ifunc:
233 case mst_solib_trampoline:
258 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
259 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
261 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
263 fprintf_filtered (outfile, " section %s",
264 bfd_section_name (objfile->obfd,
265 section->the_bfd_section));
266 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
268 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
270 if (msymbol->filename)
271 fprintf_filtered (outfile, " %s", msymbol->filename);
272 fputs_filtered ("\n", outfile);
275 if (objfile->minimal_symbol_count != index)
277 warning (_("internal error: minimal symbol count %d != %d"),
278 objfile->minimal_symbol_count, index);
280 fprintf_filtered (outfile, "\n");
284 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
285 struct ui_file *outfile)
287 struct gdbarch *gdbarch = get_objfile_arch (objfile);
289 struct dict_iterator iter;
292 struct blockvector *bv;
297 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
299 fprintf_filtered (outfile, "Compilation directory is %s\n",
301 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
302 gdb_print_host_address (objfile, outfile);
303 fprintf_filtered (outfile, ")\n");
304 fprintf_filtered (outfile, "Language: %s\n",
305 language_str (symtab->language));
307 /* First print the line table. */
308 l = LINETABLE (symtab);
311 fprintf_filtered (outfile, "\nLine table:\n\n");
313 for (i = 0; i < len; i++)
315 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
316 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
317 fprintf_filtered (outfile, "\n");
320 /* Now print the block info, but only for primary symtabs since we will
321 print lots of duplicate info otherwise. */
324 fprintf_filtered (outfile, "\nBlockvector:\n\n");
325 bv = BLOCKVECTOR (symtab);
326 len = BLOCKVECTOR_NBLOCKS (bv);
327 for (i = 0; i < len; i++)
329 b = BLOCKVECTOR_BLOCK (bv, i);
330 depth = block_depth (b) * 2;
331 print_spaces (depth, outfile);
332 fprintf_filtered (outfile, "block #%03d, object at ", i);
333 gdb_print_host_address (b, outfile);
334 if (BLOCK_SUPERBLOCK (b))
336 fprintf_filtered (outfile, " under ");
337 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
339 /* drow/2002-07-10: We could save the total symbols count
340 even if we're using a hashtable, but nothing else but this message
342 fprintf_filtered (outfile, ", %d syms/buckets in ",
343 dict_size (BLOCK_DICT (b)));
344 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
345 fprintf_filtered (outfile, "..");
346 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
347 if (BLOCK_FUNCTION (b))
349 fprintf_filtered (outfile, ", function %s",
350 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
351 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
353 fprintf_filtered (outfile, ", %s",
354 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
357 fprintf_filtered (outfile, "\n");
358 /* Now print each symbol in this block (in no particular order, if
359 we're using a hashtable). Note that we only want this
360 block, not any blocks from included symtabs. */
361 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
363 struct print_symbol_args s;
369 catch_errors (print_symbol, &s, "Error printing symbol:\n",
373 fprintf_filtered (outfile, "\n");
377 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
382 dump_symtab (struct objfile *objfile, struct symtab *symtab,
383 struct ui_file *outfile)
385 /* Set the current language to the language of the symtab we're dumping
386 because certain routines used during dump_symtab() use the current
387 language to print an image of the symbol. We'll restore it later.
388 But use only real languages, not placeholders. */
389 if (symtab->language != language_unknown
390 && symtab->language != language_auto)
392 enum language saved_lang;
394 saved_lang = set_language (symtab->language);
396 dump_symtab_1 (objfile, symtab, outfile);
398 set_language (saved_lang);
401 dump_symtab_1 (objfile, symtab, outfile);
405 maintenance_print_symbols (char *args, int from_tty)
408 struct ui_file *outfile;
409 struct cleanup *cleanups;
410 char *symname = NULL;
411 char *filename = DEV_TTY;
412 struct objfile *objfile;
419 error (_("Arguments missing: an output file name "
420 "and an optional symbol file name"));
422 argv = gdb_buildargv (args);
423 cleanups = make_cleanup_freeargv (argv);
428 /* If a second arg is supplied, it is a source file name to match on. */
435 filename = tilde_expand (filename);
436 make_cleanup (xfree, filename);
438 outfile = gdb_fopen (filename, FOPEN_WT);
440 perror_with_name (filename);
441 make_cleanup_ui_file_delete (outfile);
443 ALL_SYMTABS (objfile, s)
446 if (symname == NULL || filename_cmp (symname, s->filename) == 0)
447 dump_symtab (objfile, s, outfile);
449 do_cleanups (cleanups);
452 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
453 far to indent. ARGS is really a struct print_symbol_args *, but is
454 declared as char * to get it past catch_errors. Returns 0 for error,
458 print_symbol (void *args)
460 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
461 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
462 int depth = ((struct print_symbol_args *) args)->depth;
463 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
464 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
466 print_spaces (depth, outfile);
467 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
469 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
470 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
473 fprintf_filtered (outfile, " section %s\n",
474 bfd_section_name (section->the_bfd_section->owner,
475 section->the_bfd_section));
477 fprintf_filtered (outfile, "\n");
480 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
482 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
484 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
485 &type_print_raw_options);
489 fprintf_filtered (outfile, "%s %s = ",
490 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
492 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
493 ? "struct" : "union")),
494 SYMBOL_LINKAGE_NAME (symbol));
495 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
496 &type_print_raw_options);
498 fprintf_filtered (outfile, ";\n");
502 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
503 fprintf_filtered (outfile, "typedef ");
504 if (SYMBOL_TYPE (symbol))
506 /* Print details of types, except for enums where it's clutter. */
507 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
509 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
511 &type_print_raw_options);
512 fprintf_filtered (outfile, "; ");
515 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
517 switch (SYMBOL_CLASS (symbol))
520 fprintf_filtered (outfile, "const %s (%s)",
521 plongest (SYMBOL_VALUE (symbol)),
522 hex_string (SYMBOL_VALUE (symbol)));
525 case LOC_CONST_BYTES:
528 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
530 fprintf_filtered (outfile, "const %u hex bytes:",
532 for (i = 0; i < TYPE_LENGTH (type); i++)
533 fprintf_filtered (outfile, " %02x",
534 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
539 fprintf_filtered (outfile, "static at ");
540 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
543 fprintf_filtered (outfile, " section %s",
544 bfd_section_name (section->the_bfd_section->owner,
545 section->the_bfd_section));
549 if (SYMBOL_IS_ARGUMENT (symbol))
550 fprintf_filtered (outfile, "parameter register %s",
551 plongest (SYMBOL_VALUE (symbol)));
553 fprintf_filtered (outfile, "register %s",
554 plongest (SYMBOL_VALUE (symbol)));
558 fprintf_filtered (outfile, "arg at offset %s",
559 hex_string (SYMBOL_VALUE (symbol)));
563 fprintf_filtered (outfile, "reference arg at %s",
564 hex_string (SYMBOL_VALUE (symbol)));
567 case LOC_REGPARM_ADDR:
568 fprintf_filtered (outfile, "address parameter register %s",
569 plongest (SYMBOL_VALUE (symbol)));
573 fprintf_filtered (outfile, "local at offset %s",
574 hex_string (SYMBOL_VALUE (symbol)));
581 fprintf_filtered (outfile, "label at ");
582 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
585 fprintf_filtered (outfile, " section %s",
586 bfd_section_name (section->the_bfd_section->owner,
587 section->the_bfd_section));
591 fprintf_filtered (outfile, "block object ");
592 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
593 fprintf_filtered (outfile, ", ");
594 fputs_filtered (paddress (gdbarch,
595 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
597 fprintf_filtered (outfile, "..");
598 fputs_filtered (paddress (gdbarch,
599 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
602 fprintf_filtered (outfile, " section %s",
603 bfd_section_name (section->the_bfd_section->owner,
604 section->the_bfd_section));
608 fprintf_filtered (outfile, "computed at runtime");
612 fprintf_filtered (outfile, "unresolved");
615 case LOC_OPTIMIZED_OUT:
616 fprintf_filtered (outfile, "optimized out");
620 fprintf_filtered (outfile, "botched symbol class %x",
621 SYMBOL_CLASS (symbol));
625 fprintf_filtered (outfile, "\n");
630 maintenance_print_msymbols (char *args, int from_tty)
633 struct ui_file *outfile;
634 struct cleanup *cleanups;
635 char *filename = DEV_TTY;
636 char *symname = NULL;
637 struct program_space *pspace;
638 struct objfile *objfile;
640 struct stat sym_st, obj_st;
646 error (_("print-msymbols takes an output file "
647 "name and optional symbol file name"));
649 argv = gdb_buildargv (args);
650 cleanups = make_cleanup_freeargv (argv);
655 /* If a second arg is supplied, it is a source file name to match on. */
658 symname = xfullpath (argv[1]);
659 make_cleanup (xfree, symname);
660 if (symname && stat (symname, &sym_st))
661 perror_with_name (symname);
665 filename = tilde_expand (filename);
666 make_cleanup (xfree, filename);
668 outfile = gdb_fopen (filename, FOPEN_WT);
670 perror_with_name (filename);
671 make_cleanup_ui_file_delete (outfile);
674 ALL_PSPACE_OBJFILES (pspace, objfile)
677 if (symname == NULL || (!stat (objfile->name, &obj_st)
678 && sym_st.st_dev == obj_st.st_dev
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);