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 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/>. */
28 #include "breakpoint.h"
30 #include "gdb_obstack.h"
31 #include "exceptions.h"
35 #include "gdb_regex.h"
37 #include "dictionary.h"
39 #include "gdb_string.h"
40 #include "readline/readline.h"
45 #define DEV_TTY "/dev/tty"
48 /* Unfortunately for debugging, stderr is usually a macro. This is painful
49 when calling functions that take FILE *'s from the debugger.
50 So we make a variable which has the same value and which is accessible when
51 debugging GDB with itself. Because stdin et al need not be constants,
52 we initialize them in the _initialize_symmisc function at the bottom
58 /* Prototypes for local functions */
60 static void dump_symtab (struct objfile *, struct symtab *,
63 static void dump_msymbols (struct objfile *, struct ui_file *);
65 static void dump_objfile (struct objfile *);
67 static int block_depth (struct block *);
69 void _initialize_symmisc (void);
71 struct print_symbol_args
73 struct gdbarch *gdbarch;
74 struct symbol *symbol;
76 struct ui_file *outfile;
79 static int print_symbol (void *);
81 /* Free all the storage associated with the struct symtab <- S.
82 Note that some symtabs have contents that all live inside one big block of
83 memory, and some share the contents of another symbol table and so you
84 should not free the contents on their behalf (except sometimes the
85 linetable, which maybe per symtab even when the rest is not).
86 It is s->free_code that says which alternative to use. */
89 free_symtab (struct symtab *s)
92 struct blockvector *bv;
97 /* All the contents are part of a big block of memory (an obstack),
98 and some other symtab is in charge of freeing that block.
99 Therefore, do nothing. */
103 /* Everything will be freed either by our `free_func'
104 or by some other symtab, except for our linetable.
107 xfree (LINETABLE (s));
111 /* If there is a single block of memory to free, free it. */
112 if (s->free_func != NULL)
115 /* Free source-related stuff */
116 if (s->line_charpos != NULL)
117 xfree (s->line_charpos);
118 if (s->fullname != NULL)
120 if (s->debugformat != NULL)
121 xfree (s->debugformat);
126 print_symbol_bcache_statistics (void)
128 struct program_space *pspace;
129 struct objfile *objfile;
133 ALL_PSPACE_OBJFILES (pspace, objfile)
135 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
136 print_bcache_statistics (objfile->psymbol_cache, "partial symbol cache");
137 print_bcache_statistics (objfile->macro_cache, "preprocessor macro cache");
138 print_bcache_statistics (objfile->filename_cache, "file name cache");
144 print_objfile_statistics (void)
146 struct program_space *pspace;
147 struct objfile *objfile;
149 int i, linetables, blockvectors;
153 ALL_PSPACE_OBJFILES (pspace, objfile)
155 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
156 if (OBJSTAT (objfile, n_stabs) > 0)
157 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
158 OBJSTAT (objfile, n_stabs));
159 if (OBJSTAT (objfile, n_minsyms) > 0)
160 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
161 OBJSTAT (objfile, n_minsyms));
162 if (OBJSTAT (objfile, n_psyms) > 0)
163 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
164 OBJSTAT (objfile, n_psyms));
165 if (OBJSTAT (objfile, n_syms) > 0)
166 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
167 OBJSTAT (objfile, n_syms));
168 if (OBJSTAT (objfile, n_types) > 0)
169 printf_filtered (_(" Number of \"types\" defined: %d\n"),
170 OBJSTAT (objfile, n_types));
172 objfile->sf->qf->print_stats (objfile);
173 i = linetables = blockvectors = 0;
174 ALL_OBJFILE_SYMTABS (objfile, s)
177 if (s->linetable != NULL)
182 printf_filtered (_(" Number of symbol tables: %d\n"), i);
183 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
185 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
188 if (OBJSTAT (objfile, sz_strtab) > 0)
189 printf_filtered (_(" Space used by a.out string tables: %d\n"),
190 OBJSTAT (objfile, sz_strtab));
191 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
192 obstack_memory_used (&objfile->objfile_obstack));
193 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
194 bcache_memory_used (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_solib_trampoline:
293 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
294 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
296 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
298 fprintf_filtered (outfile, " section %s",
299 bfd_section_name (objfile->obfd,
300 section->the_bfd_section));
301 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
303 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
305 if (msymbol->filename)
306 fprintf_filtered (outfile, " %s", msymbol->filename);
307 fputs_filtered ("\n", outfile);
310 if (objfile->minimal_symbol_count != index)
312 warning (_("internal error: minimal symbol count %d != %d"),
313 objfile->minimal_symbol_count, index);
315 fprintf_filtered (outfile, "\n");
319 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
320 struct ui_file *outfile)
322 struct gdbarch *gdbarch = get_objfile_arch (objfile);
324 struct dict_iterator iter;
327 struct blockvector *bv;
332 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
334 fprintf_filtered (outfile, "Compilation directory is %s\n",
336 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
337 gdb_print_host_address (objfile, outfile);
338 fprintf_filtered (outfile, ")\n");
339 fprintf_filtered (outfile, "Language: %s\n", language_str (symtab->language));
341 /* First print the line table. */
342 l = LINETABLE (symtab);
345 fprintf_filtered (outfile, "\nLine table:\n\n");
347 for (i = 0; i < len; i++)
349 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
350 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
351 fprintf_filtered (outfile, "\n");
354 /* Now print the block info, but only for primary symtabs since we will
355 print lots of duplicate info otherwise. */
358 fprintf_filtered (outfile, "\nBlockvector:\n\n");
359 bv = BLOCKVECTOR (symtab);
360 len = BLOCKVECTOR_NBLOCKS (bv);
361 for (i = 0; i < len; i++)
363 b = BLOCKVECTOR_BLOCK (bv, i);
364 depth = block_depth (b) * 2;
365 print_spaces (depth, outfile);
366 fprintf_filtered (outfile, "block #%03d, object at ", i);
367 gdb_print_host_address (b, outfile);
368 if (BLOCK_SUPERBLOCK (b))
370 fprintf_filtered (outfile, " under ");
371 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
373 /* drow/2002-07-10: We could save the total symbols count
374 even if we're using a hashtable, but nothing else but this message
376 fprintf_filtered (outfile, ", %d syms/buckets in ",
377 dict_size (BLOCK_DICT (b)));
378 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
379 fprintf_filtered (outfile, "..");
380 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
381 if (BLOCK_FUNCTION (b))
383 fprintf_filtered (outfile, ", function %s",
384 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
385 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
387 fprintf_filtered (outfile, ", %s",
388 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
391 fprintf_filtered (outfile, "\n");
392 /* Now print each symbol in this block (in no particular order, if
393 we're using a hashtable). */
394 ALL_BLOCK_SYMBOLS (b, iter, sym)
396 struct print_symbol_args s;
401 catch_errors (print_symbol, &s, "Error printing symbol:\n",
405 fprintf_filtered (outfile, "\n");
409 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
414 dump_symtab (struct objfile *objfile, struct symtab *symtab,
415 struct ui_file *outfile)
417 /* Set the current language to the language of the symtab we're dumping
418 because certain routines used during dump_symtab() use the current
419 language to print an image of the symbol. We'll restore it later.
420 But use only real languages, not placeholders. */
421 if (symtab->language != language_unknown
422 && symtab->language != language_auto)
424 enum language saved_lang;
426 saved_lang = set_language (symtab->language);
428 dump_symtab_1 (objfile, symtab, outfile);
430 set_language (saved_lang);
433 dump_symtab_1 (objfile, symtab, outfile);
437 maintenance_print_symbols (char *args, int from_tty)
440 struct ui_file *outfile;
441 struct cleanup *cleanups;
442 char *symname = NULL;
443 char *filename = DEV_TTY;
444 struct objfile *objfile;
452 Arguments missing: an output file name and an optional symbol file name"));
454 argv = gdb_buildargv (args);
455 cleanups = make_cleanup_freeargv (argv);
460 /* If a second arg is supplied, it is a source file name to match on */
467 filename = tilde_expand (filename);
468 make_cleanup (xfree, filename);
470 outfile = gdb_fopen (filename, FOPEN_WT);
472 perror_with_name (filename);
473 make_cleanup_ui_file_delete (outfile);
476 ALL_SYMTABS (objfile, s)
477 if (symname == NULL || strcmp (symname, s->filename) == 0)
478 dump_symtab (objfile, s, outfile);
480 do_cleanups (cleanups);
483 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
484 far to indent. ARGS is really a struct print_symbol_args *, but is
485 declared as char * to get it past catch_errors. Returns 0 for error,
489 print_symbol (void *args)
491 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
492 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
493 int depth = ((struct print_symbol_args *) args)->depth;
494 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
495 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
497 print_spaces (depth, outfile);
498 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
500 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
501 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
504 fprintf_filtered (outfile, " section %s\n",
505 bfd_section_name (section->the_bfd_section->owner,
506 section->the_bfd_section));
508 fprintf_filtered (outfile, "\n");
511 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
513 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
515 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
519 fprintf_filtered (outfile, "%s %s = ",
520 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
522 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
523 ? "struct" : "union")),
524 SYMBOL_LINKAGE_NAME (symbol));
525 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
527 fprintf_filtered (outfile, ";\n");
531 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
532 fprintf_filtered (outfile, "typedef ");
533 if (SYMBOL_TYPE (symbol))
535 /* Print details of types, except for enums where it's clutter. */
536 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
538 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
540 fprintf_filtered (outfile, "; ");
543 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
545 switch (SYMBOL_CLASS (symbol))
548 fprintf_filtered (outfile, "const %ld (0x%lx)",
549 SYMBOL_VALUE (symbol),
550 SYMBOL_VALUE (symbol));
553 case LOC_CONST_BYTES:
556 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
557 fprintf_filtered (outfile, "const %u hex bytes:",
559 for (i = 0; i < TYPE_LENGTH (type); i++)
560 fprintf_filtered (outfile, " %02x",
561 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
566 fprintf_filtered (outfile, "static at ");
567 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
570 fprintf_filtered (outfile, " section %s",
571 bfd_section_name (section->the_bfd_section->owner,
572 section->the_bfd_section));
576 if (SYMBOL_IS_ARGUMENT (symbol))
577 fprintf_filtered (outfile, "parameter register %ld",
578 SYMBOL_VALUE (symbol));
580 fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
584 fprintf_filtered (outfile, "arg at offset 0x%lx",
585 SYMBOL_VALUE (symbol));
589 fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
592 case LOC_REGPARM_ADDR:
593 fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
597 fprintf_filtered (outfile, "local at offset 0x%lx",
598 SYMBOL_VALUE (symbol));
605 fprintf_filtered (outfile, "label at ");
606 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
609 fprintf_filtered (outfile, " section %s",
610 bfd_section_name (section->the_bfd_section->owner,
611 section->the_bfd_section));
615 fprintf_filtered (outfile, "block object ");
616 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
617 fprintf_filtered (outfile, ", ");
618 fputs_filtered (paddress (gdbarch,
619 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
621 fprintf_filtered (outfile, "..");
622 fputs_filtered (paddress (gdbarch,
623 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
626 fprintf_filtered (outfile, " section %s",
627 bfd_section_name (section->the_bfd_section->owner,
628 section->the_bfd_section));
632 fprintf_filtered (outfile, "computed at runtime");
636 fprintf_filtered (outfile, "unresolved");
639 case LOC_OPTIMIZED_OUT:
640 fprintf_filtered (outfile, "optimized out");
644 fprintf_filtered (outfile, "botched symbol class %x",
645 SYMBOL_CLASS (symbol));
649 fprintf_filtered (outfile, "\n");
654 maintenance_print_msymbols (char *args, int from_tty)
657 struct ui_file *outfile;
658 struct cleanup *cleanups;
659 char *filename = DEV_TTY;
660 char *symname = NULL;
661 struct program_space *pspace;
662 struct objfile *objfile;
664 struct stat sym_st, obj_st;
670 error (_("print-msymbols takes an output file name and optional symbol file name"));
672 argv = gdb_buildargv (args);
673 cleanups = make_cleanup_freeargv (argv);
678 /* If a second arg is supplied, it is a source file name to match on */
681 symname = xfullpath (argv[1]);
682 make_cleanup (xfree, symname);
683 if (symname && stat (symname, &sym_st))
684 perror_with_name (symname);
688 filename = tilde_expand (filename);
689 make_cleanup (xfree, filename);
691 outfile = gdb_fopen (filename, FOPEN_WT);
693 perror_with_name (filename);
694 make_cleanup_ui_file_delete (outfile);
698 ALL_PSPACE_OBJFILES (pspace, objfile)
700 || (!stat (objfile->name, &obj_st) && sym_st.st_ino == obj_st.st_ino))
701 dump_msymbols (objfile, outfile);
703 fprintf_filtered (outfile, "\n\n");
704 do_cleanups (cleanups);
708 maintenance_print_objfiles (char *ignore, int from_tty)
710 struct program_space *pspace;
711 struct objfile *objfile;
717 ALL_PSPACE_OBJFILES (pspace, objfile)
718 dump_objfile (objfile);
723 /* List all the symbol tables whose names match REGEXP (optional). */
725 maintenance_info_symtabs (char *regexp, int from_tty)
727 struct program_space *pspace;
728 struct objfile *objfile;
734 ALL_PSPACE_OBJFILES (pspace, objfile)
736 struct symtab *symtab;
738 /* We don't want to print anything for this objfile until we
739 actually find a symtab whose name matches. */
740 int printed_objfile_start = 0;
742 ALL_OBJFILE_SYMTABS (objfile, symtab)
747 || re_exec (symtab->filename))
749 if (! printed_objfile_start)
751 printf_filtered ("{ objfile %s ", objfile->name);
753 printf_filtered ("((struct objfile *) %s)\n",
754 host_address_to_string (objfile));
755 printed_objfile_start = 1;
758 printf_filtered (" { symtab %s ", symtab->filename);
760 printf_filtered ("((struct symtab *) %s)\n",
761 host_address_to_string (symtab));
762 printf_filtered (" dirname %s\n",
763 symtab->dirname ? symtab->dirname : "(null)");
764 printf_filtered (" fullname %s\n",
765 symtab->fullname ? symtab->fullname : "(null)");
766 printf_filtered (" blockvector ((struct blockvector *) %s)%s\n",
767 host_address_to_string (symtab->blockvector),
768 symtab->primary ? " (primary)" : "");
769 printf_filtered (" linetable ((struct linetable *) %s)\n",
770 host_address_to_string (symtab->linetable));
771 printf_filtered (" debugformat %s\n", symtab->debugformat);
772 printf_filtered (" }\n");
776 if (printed_objfile_start)
777 printf_filtered ("}\n");
782 /* Return the nexting depth of a block within other blocks in its symtab. */
785 block_depth (struct block *block)
788 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
796 /* Do early runtime initializations. */
798 _initialize_symmisc (void)