1 /* Do various things to symbol tables (other than lookup), for GDB.
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2007, 2008, 2009, 2010,
5 2011 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
26 #include "filenames.h"
29 #include "breakpoint.h"
31 #include "gdb_obstack.h"
32 #include "exceptions.h"
36 #include "gdb_regex.h"
38 #include "dictionary.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 *);
82 /* Free all the storage associated with the struct symtab <- S.
83 Note that some symtabs have contents that all live inside one big block of
84 memory, and some share the contents of another symbol table and so you
85 should not free the contents on their behalf (except sometimes the
86 linetable, which maybe per symtab even when the rest is not).
87 It is s->free_code that says which alternative to use. */
90 free_symtab (struct symtab *s)
95 /* All the contents are part of a big block of memory (an obstack),
96 and some other symtab is in charge of freeing that block.
97 Therefore, do nothing. */
101 /* Everything will be freed either by our `free_func'
102 or by some other symtab, except for our linetable.
105 xfree (LINETABLE (s));
109 /* If there is a single block of memory to free, free it. */
110 if (s->free_func != NULL)
113 /* Free source-related stuff. */
114 if (s->line_charpos != NULL)
115 xfree (s->line_charpos);
116 if (s->fullname != NULL)
118 if (s->debugformat != NULL)
119 xfree (s->debugformat);
124 print_symbol_bcache_statistics (void)
126 struct program_space *pspace;
127 struct objfile *objfile;
131 ALL_PSPACE_OBJFILES (pspace, objfile)
133 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
134 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
135 "partial symbol cache");
136 print_bcache_statistics (objfile->macro_cache, "preprocessor macro cache");
137 print_bcache_statistics (objfile->filename_cache, "file name cache");
143 print_objfile_statistics (void)
145 struct program_space *pspace;
146 struct objfile *objfile;
148 int i, linetables, blockvectors;
152 ALL_PSPACE_OBJFILES (pspace, objfile)
154 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
155 if (OBJSTAT (objfile, n_stabs) > 0)
156 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
157 OBJSTAT (objfile, n_stabs));
158 if (OBJSTAT (objfile, n_minsyms) > 0)
159 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
160 OBJSTAT (objfile, n_minsyms));
161 if (OBJSTAT (objfile, n_psyms) > 0)
162 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
163 OBJSTAT (objfile, n_psyms));
164 if (OBJSTAT (objfile, n_syms) > 0)
165 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
166 OBJSTAT (objfile, n_syms));
167 if (OBJSTAT (objfile, n_types) > 0)
168 printf_filtered (_(" Number of \"types\" defined: %d\n"),
169 OBJSTAT (objfile, n_types));
171 objfile->sf->qf->print_stats (objfile);
172 i = linetables = blockvectors = 0;
173 ALL_OBJFILE_SYMTABS (objfile, s)
176 if (s->linetable != NULL)
181 printf_filtered (_(" Number of symbol tables: %d\n"), i);
182 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
184 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
187 if (OBJSTAT (objfile, sz_strtab) > 0)
188 printf_filtered (_(" Space used by a.out string tables: %d\n"),
189 OBJSTAT (objfile, sz_strtab));
190 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
191 obstack_memory_used (&objfile->objfile_obstack));
192 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
193 bcache_memory_used (psymbol_bcache_get_bcache
194 (objfile->psymbol_cache)));
195 printf_filtered (_(" Total memory used for macro cache: %d\n"),
196 bcache_memory_used (objfile->macro_cache));
197 printf_filtered (_(" Total memory used for file name cache: %d\n"),
198 bcache_memory_used (objfile->filename_cache));
204 dump_objfile (struct objfile *objfile)
206 struct symtab *symtab;
208 printf_filtered ("\nObject file %s: ", objfile->name);
209 printf_filtered ("Objfile at ");
210 gdb_print_host_address (objfile, gdb_stdout);
211 printf_filtered (", bfd at ");
212 gdb_print_host_address (objfile->obfd, gdb_stdout);
213 printf_filtered (", %d minsyms\n\n",
214 objfile->minimal_symbol_count);
217 objfile->sf->qf->dump (objfile);
219 if (objfile->symtabs)
221 printf_filtered ("Symtabs:\n");
222 for (symtab = objfile->symtabs;
224 symtab = symtab->next)
226 printf_filtered ("%s at ", symtab->filename);
227 gdb_print_host_address (symtab, gdb_stdout);
228 printf_filtered (", ");
229 if (symtab->objfile != objfile)
231 printf_filtered ("NOT ON CHAIN! ");
235 printf_filtered ("\n\n");
239 /* Print minimal symbols from this objfile. */
242 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
244 struct gdbarch *gdbarch = get_objfile_arch (objfile);
245 struct minimal_symbol *msymbol;
249 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
250 if (objfile->minimal_symbol_count == 0)
252 fprintf_filtered (outfile, "No minimal symbols found.\n");
256 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
258 struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
260 switch (MSYMBOL_TYPE (msymbol))
268 case mst_text_gnu_ifunc:
271 case mst_solib_trampoline:
296 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
297 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
299 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
301 fprintf_filtered (outfile, " section %s",
302 bfd_section_name (objfile->obfd,
303 section->the_bfd_section));
304 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
306 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
308 if (msymbol->filename)
309 fprintf_filtered (outfile, " %s", msymbol->filename);
310 fputs_filtered ("\n", outfile);
313 if (objfile->minimal_symbol_count != index)
315 warning (_("internal error: minimal symbol count %d != %d"),
316 objfile->minimal_symbol_count, index);
318 fprintf_filtered (outfile, "\n");
322 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
323 struct ui_file *outfile)
325 struct gdbarch *gdbarch = get_objfile_arch (objfile);
327 struct dict_iterator iter;
330 struct blockvector *bv;
335 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
337 fprintf_filtered (outfile, "Compilation directory is %s\n",
339 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
340 gdb_print_host_address (objfile, outfile);
341 fprintf_filtered (outfile, ")\n");
342 fprintf_filtered (outfile, "Language: %s\n",
343 language_str (symtab->language));
345 /* First print the line table. */
346 l = LINETABLE (symtab);
349 fprintf_filtered (outfile, "\nLine table:\n\n");
351 for (i = 0; i < len; i++)
353 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
354 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
355 fprintf_filtered (outfile, "\n");
358 /* Now print the block info, but only for primary symtabs since we will
359 print lots of duplicate info otherwise. */
362 fprintf_filtered (outfile, "\nBlockvector:\n\n");
363 bv = BLOCKVECTOR (symtab);
364 len = BLOCKVECTOR_NBLOCKS (bv);
365 for (i = 0; i < len; i++)
367 b = BLOCKVECTOR_BLOCK (bv, i);
368 depth = block_depth (b) * 2;
369 print_spaces (depth, outfile);
370 fprintf_filtered (outfile, "block #%03d, object at ", i);
371 gdb_print_host_address (b, outfile);
372 if (BLOCK_SUPERBLOCK (b))
374 fprintf_filtered (outfile, " under ");
375 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
377 /* drow/2002-07-10: We could save the total symbols count
378 even if we're using a hashtable, but nothing else but this message
380 fprintf_filtered (outfile, ", %d syms/buckets in ",
381 dict_size (BLOCK_DICT (b)));
382 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
383 fprintf_filtered (outfile, "..");
384 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
385 if (BLOCK_FUNCTION (b))
387 fprintf_filtered (outfile, ", function %s",
388 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
389 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
391 fprintf_filtered (outfile, ", %s",
392 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
395 fprintf_filtered (outfile, "\n");
396 /* Now print each symbol in this block (in no particular order, if
397 we're using a hashtable). */
398 ALL_BLOCK_SYMBOLS (b, iter, sym)
400 struct print_symbol_args s;
406 catch_errors (print_symbol, &s, "Error printing symbol:\n",
410 fprintf_filtered (outfile, "\n");
414 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
419 dump_symtab (struct objfile *objfile, struct symtab *symtab,
420 struct ui_file *outfile)
422 /* Set the current language to the language of the symtab we're dumping
423 because certain routines used during dump_symtab() use the current
424 language to print an image of the symbol. We'll restore it later.
425 But use only real languages, not placeholders. */
426 if (symtab->language != language_unknown
427 && symtab->language != language_auto)
429 enum language saved_lang;
431 saved_lang = set_language (symtab->language);
433 dump_symtab_1 (objfile, symtab, outfile);
435 set_language (saved_lang);
438 dump_symtab_1 (objfile, symtab, outfile);
442 maintenance_print_symbols (char *args, int from_tty)
445 struct ui_file *outfile;
446 struct cleanup *cleanups;
447 char *symname = NULL;
448 char *filename = DEV_TTY;
449 struct objfile *objfile;
456 error (_("Arguments missing: an output file name "
457 "and an optional symbol file name"));
459 argv = gdb_buildargv (args);
460 cleanups = make_cleanup_freeargv (argv);
465 /* If a second arg is supplied, it is a source file name to match on. */
472 filename = tilde_expand (filename);
473 make_cleanup (xfree, filename);
475 outfile = gdb_fopen (filename, FOPEN_WT);
477 perror_with_name (filename);
478 make_cleanup_ui_file_delete (outfile);
481 ALL_SYMTABS (objfile, s)
482 if (symname == NULL || filename_cmp (symname, s->filename) == 0)
483 dump_symtab (objfile, s, outfile);
485 do_cleanups (cleanups);
488 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
489 far to indent. ARGS is really a struct print_symbol_args *, but is
490 declared as char * to get it past catch_errors. Returns 0 for error,
494 print_symbol (void *args)
496 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
497 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
498 int depth = ((struct print_symbol_args *) args)->depth;
499 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
500 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
502 print_spaces (depth, outfile);
503 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
505 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
506 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
509 fprintf_filtered (outfile, " section %s\n",
510 bfd_section_name (section->the_bfd_section->owner,
511 section->the_bfd_section));
513 fprintf_filtered (outfile, "\n");
516 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
518 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
520 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
524 fprintf_filtered (outfile, "%s %s = ",
525 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
527 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
528 ? "struct" : "union")),
529 SYMBOL_LINKAGE_NAME (symbol));
530 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
532 fprintf_filtered (outfile, ";\n");
536 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
537 fprintf_filtered (outfile, "typedef ");
538 if (SYMBOL_TYPE (symbol))
540 /* Print details of types, except for enums where it's clutter. */
541 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
543 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
545 fprintf_filtered (outfile, "; ");
548 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
550 switch (SYMBOL_CLASS (symbol))
553 fprintf_filtered (outfile, "const %ld (0x%lx)",
554 SYMBOL_VALUE (symbol),
555 SYMBOL_VALUE (symbol));
558 case LOC_CONST_BYTES:
561 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
563 fprintf_filtered (outfile, "const %u hex bytes:",
565 for (i = 0; i < TYPE_LENGTH (type); i++)
566 fprintf_filtered (outfile, " %02x",
567 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
572 fprintf_filtered (outfile, "static at ");
573 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
576 fprintf_filtered (outfile, " section %s",
577 bfd_section_name (section->the_bfd_section->owner,
578 section->the_bfd_section));
582 if (SYMBOL_IS_ARGUMENT (symbol))
583 fprintf_filtered (outfile, "parameter register %ld",
584 SYMBOL_VALUE (symbol));
586 fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
590 fprintf_filtered (outfile, "arg at offset 0x%lx",
591 SYMBOL_VALUE (symbol));
595 fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
598 case LOC_REGPARM_ADDR:
599 fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
603 fprintf_filtered (outfile, "local at offset 0x%lx",
604 SYMBOL_VALUE (symbol));
611 fprintf_filtered (outfile, "label at ");
612 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
615 fprintf_filtered (outfile, " section %s",
616 bfd_section_name (section->the_bfd_section->owner,
617 section->the_bfd_section));
621 fprintf_filtered (outfile, "block object ");
622 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
623 fprintf_filtered (outfile, ", ");
624 fputs_filtered (paddress (gdbarch,
625 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
627 fprintf_filtered (outfile, "..");
628 fputs_filtered (paddress (gdbarch,
629 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
632 fprintf_filtered (outfile, " section %s",
633 bfd_section_name (section->the_bfd_section->owner,
634 section->the_bfd_section));
638 fprintf_filtered (outfile, "computed at runtime");
642 fprintf_filtered (outfile, "unresolved");
645 case LOC_OPTIMIZED_OUT:
646 fprintf_filtered (outfile, "optimized out");
650 fprintf_filtered (outfile, "botched symbol class %x",
651 SYMBOL_CLASS (symbol));
655 fprintf_filtered (outfile, "\n");
660 maintenance_print_msymbols (char *args, int from_tty)
663 struct ui_file *outfile;
664 struct cleanup *cleanups;
665 char *filename = DEV_TTY;
666 char *symname = NULL;
667 struct program_space *pspace;
668 struct objfile *objfile;
670 struct stat sym_st, obj_st;
676 error (_("print-msymbols takes an output file "
677 "name and optional symbol file name"));
679 argv = gdb_buildargv (args);
680 cleanups = make_cleanup_freeargv (argv);
685 /* If a second arg is supplied, it is a source file name to match on. */
688 symname = xfullpath (argv[1]);
689 make_cleanup (xfree, symname);
690 if (symname && stat (symname, &sym_st))
691 perror_with_name (symname);
695 filename = tilde_expand (filename);
696 make_cleanup (xfree, filename);
698 outfile = gdb_fopen (filename, FOPEN_WT);
700 perror_with_name (filename);
701 make_cleanup_ui_file_delete (outfile);
705 ALL_PSPACE_OBJFILES (pspace, objfile)
706 if (symname == NULL || (!stat (objfile->name, &obj_st)
707 && sym_st.st_ino == obj_st.st_ino))
708 dump_msymbols (objfile, outfile);
710 fprintf_filtered (outfile, "\n\n");
711 do_cleanups (cleanups);
715 maintenance_print_objfiles (char *ignore, int from_tty)
717 struct program_space *pspace;
718 struct objfile *objfile;
724 ALL_PSPACE_OBJFILES (pspace, objfile)
725 dump_objfile (objfile);
730 /* List all the symbol tables whose names match REGEXP (optional). */
732 maintenance_info_symtabs (char *regexp, int from_tty)
734 struct program_space *pspace;
735 struct objfile *objfile;
741 ALL_PSPACE_OBJFILES (pspace, objfile)
743 struct symtab *symtab;
745 /* We don't want to print anything for this objfile until we
746 actually find a symtab whose name matches. */
747 int printed_objfile_start = 0;
749 ALL_OBJFILE_SYMTABS (objfile, symtab)
754 || re_exec (symtab->filename))
756 if (! printed_objfile_start)
758 printf_filtered ("{ objfile %s ", objfile->name);
760 printf_filtered ("((struct objfile *) %s)\n",
761 host_address_to_string (objfile));
762 printed_objfile_start = 1;
765 printf_filtered (" { symtab %s ", symtab->filename);
767 printf_filtered ("((struct symtab *) %s)\n",
768 host_address_to_string (symtab));
769 printf_filtered (" dirname %s\n",
770 symtab->dirname ? symtab->dirname : "(null)");
771 printf_filtered (" fullname %s\n",
772 symtab->fullname ? symtab->fullname : "(null)");
774 "blockvector ((struct blockvector *) %s)%s\n",
775 host_address_to_string (symtab->blockvector),
776 symtab->primary ? " (primary)" : "");
778 "linetable ((struct linetable *) %s)\n",
779 host_address_to_string (symtab->linetable));
780 printf_filtered (" debugformat %s\n",
781 symtab->debugformat);
782 printf_filtered (" }\n");
786 if (printed_objfile_start)
787 printf_filtered ("}\n");
792 /* Return the nexting depth of a block within other blocks in its symtab. */
795 block_depth (struct block *block)
799 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
807 /* Do early runtime initializations. */
809 _initialize_symmisc (void)