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"
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 *);
83 print_symbol_bcache_statistics (void)
85 struct program_space *pspace;
86 struct objfile *objfile;
90 ALL_PSPACE_OBJFILES (pspace, objfile)
92 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
93 print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
94 "partial symbol cache");
95 print_bcache_statistics (objfile->macro_cache, "preprocessor macro cache");
96 print_bcache_statistics (objfile->filename_cache, "file name cache");
102 print_objfile_statistics (void)
104 struct program_space *pspace;
105 struct objfile *objfile;
107 int i, linetables, blockvectors;
111 ALL_PSPACE_OBJFILES (pspace, objfile)
113 printf_filtered (_("Statistics for '%s':\n"), objfile->name);
114 if (OBJSTAT (objfile, n_stabs) > 0)
115 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
116 OBJSTAT (objfile, n_stabs));
117 if (OBJSTAT (objfile, n_minsyms) > 0)
118 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
119 OBJSTAT (objfile, n_minsyms));
120 if (OBJSTAT (objfile, n_psyms) > 0)
121 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
122 OBJSTAT (objfile, n_psyms));
123 if (OBJSTAT (objfile, n_syms) > 0)
124 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
125 OBJSTAT (objfile, n_syms));
126 if (OBJSTAT (objfile, n_types) > 0)
127 printf_filtered (_(" Number of \"types\" defined: %d\n"),
128 OBJSTAT (objfile, n_types));
130 objfile->sf->qf->print_stats (objfile);
131 i = linetables = blockvectors = 0;
132 ALL_OBJFILE_SYMTABS (objfile, s)
135 if (s->linetable != NULL)
140 printf_filtered (_(" Number of symbol tables: %d\n"), i);
141 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
143 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
146 if (OBJSTAT (objfile, sz_strtab) > 0)
147 printf_filtered (_(" Space used by a.out string tables: %d\n"),
148 OBJSTAT (objfile, sz_strtab));
149 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
150 obstack_memory_used (&objfile->objfile_obstack));
151 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
152 bcache_memory_used (psymbol_bcache_get_bcache
153 (objfile->psymbol_cache)));
154 printf_filtered (_(" Total memory used for macro cache: %d\n"),
155 bcache_memory_used (objfile->macro_cache));
156 printf_filtered (_(" Total memory used for file name cache: %d\n"),
157 bcache_memory_used (objfile->filename_cache));
163 dump_objfile (struct objfile *objfile)
165 struct symtab *symtab;
167 printf_filtered ("\nObject file %s: ", objfile->name);
168 printf_filtered ("Objfile at ");
169 gdb_print_host_address (objfile, gdb_stdout);
170 printf_filtered (", bfd at ");
171 gdb_print_host_address (objfile->obfd, gdb_stdout);
172 printf_filtered (", %d minsyms\n\n",
173 objfile->minimal_symbol_count);
176 objfile->sf->qf->dump (objfile);
178 if (objfile->symtabs)
180 printf_filtered ("Symtabs:\n");
181 for (symtab = objfile->symtabs;
183 symtab = symtab->next)
185 printf_filtered ("%s at ", symtab->filename);
186 gdb_print_host_address (symtab, gdb_stdout);
187 printf_filtered (", ");
188 if (symtab->objfile != objfile)
190 printf_filtered ("NOT ON CHAIN! ");
194 printf_filtered ("\n\n");
198 /* Print minimal symbols from this objfile. */
201 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
203 struct gdbarch *gdbarch = get_objfile_arch (objfile);
204 struct minimal_symbol *msymbol;
208 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
209 if (objfile->minimal_symbol_count == 0)
211 fprintf_filtered (outfile, "No minimal symbols found.\n");
215 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
217 struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
219 switch (MSYMBOL_TYPE (msymbol))
227 case mst_text_gnu_ifunc:
230 case mst_solib_trampoline:
255 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
256 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
258 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
260 fprintf_filtered (outfile, " section %s",
261 bfd_section_name (objfile->obfd,
262 section->the_bfd_section));
263 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
265 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
267 if (msymbol->filename)
268 fprintf_filtered (outfile, " %s", msymbol->filename);
269 fputs_filtered ("\n", outfile);
272 if (objfile->minimal_symbol_count != index)
274 warning (_("internal error: minimal symbol count %d != %d"),
275 objfile->minimal_symbol_count, index);
277 fprintf_filtered (outfile, "\n");
281 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
282 struct ui_file *outfile)
284 struct gdbarch *gdbarch = get_objfile_arch (objfile);
286 struct dict_iterator iter;
289 struct blockvector *bv;
294 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
296 fprintf_filtered (outfile, "Compilation directory is %s\n",
298 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
299 gdb_print_host_address (objfile, outfile);
300 fprintf_filtered (outfile, ")\n");
301 fprintf_filtered (outfile, "Language: %s\n",
302 language_str (symtab->language));
304 /* First print the line table. */
305 l = LINETABLE (symtab);
308 fprintf_filtered (outfile, "\nLine table:\n\n");
310 for (i = 0; i < len; i++)
312 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
313 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
314 fprintf_filtered (outfile, "\n");
317 /* Now print the block info, but only for primary symtabs since we will
318 print lots of duplicate info otherwise. */
321 fprintf_filtered (outfile, "\nBlockvector:\n\n");
322 bv = BLOCKVECTOR (symtab);
323 len = BLOCKVECTOR_NBLOCKS (bv);
324 for (i = 0; i < len; i++)
326 b = BLOCKVECTOR_BLOCK (bv, i);
327 depth = block_depth (b) * 2;
328 print_spaces (depth, outfile);
329 fprintf_filtered (outfile, "block #%03d, object at ", i);
330 gdb_print_host_address (b, outfile);
331 if (BLOCK_SUPERBLOCK (b))
333 fprintf_filtered (outfile, " under ");
334 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
336 /* drow/2002-07-10: We could save the total symbols count
337 even if we're using a hashtable, but nothing else but this message
339 fprintf_filtered (outfile, ", %d syms/buckets in ",
340 dict_size (BLOCK_DICT (b)));
341 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
342 fprintf_filtered (outfile, "..");
343 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
344 if (BLOCK_FUNCTION (b))
346 fprintf_filtered (outfile, ", function %s",
347 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
348 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
350 fprintf_filtered (outfile, ", %s",
351 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
354 fprintf_filtered (outfile, "\n");
355 /* Now print each symbol in this block (in no particular order, if
356 we're using a hashtable). */
357 ALL_BLOCK_SYMBOLS (b, iter, sym)
359 struct print_symbol_args s;
365 catch_errors (print_symbol, &s, "Error printing symbol:\n",
369 fprintf_filtered (outfile, "\n");
373 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
378 dump_symtab (struct objfile *objfile, struct symtab *symtab,
379 struct ui_file *outfile)
381 /* Set the current language to the language of the symtab we're dumping
382 because certain routines used during dump_symtab() use the current
383 language to print an image of the symbol. We'll restore it later.
384 But use only real languages, not placeholders. */
385 if (symtab->language != language_unknown
386 && symtab->language != language_auto)
388 enum language saved_lang;
390 saved_lang = set_language (symtab->language);
392 dump_symtab_1 (objfile, symtab, outfile);
394 set_language (saved_lang);
397 dump_symtab_1 (objfile, symtab, outfile);
401 maintenance_print_symbols (char *args, int from_tty)
404 struct ui_file *outfile;
405 struct cleanup *cleanups;
406 char *symname = NULL;
407 char *filename = DEV_TTY;
408 struct objfile *objfile;
415 error (_("Arguments missing: an output file name "
416 "and an optional symbol file name"));
418 argv = gdb_buildargv (args);
419 cleanups = make_cleanup_freeargv (argv);
424 /* If a second arg is supplied, it is a source file name to match on. */
431 filename = tilde_expand (filename);
432 make_cleanup (xfree, filename);
434 outfile = gdb_fopen (filename, FOPEN_WT);
436 perror_with_name (filename);
437 make_cleanup_ui_file_delete (outfile);
440 ALL_SYMTABS (objfile, s)
441 if (symname == NULL || filename_cmp (symname, s->filename) == 0)
442 dump_symtab (objfile, s, outfile);
444 do_cleanups (cleanups);
447 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
448 far to indent. ARGS is really a struct print_symbol_args *, but is
449 declared as char * to get it past catch_errors. Returns 0 for error,
453 print_symbol (void *args)
455 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
456 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
457 int depth = ((struct print_symbol_args *) args)->depth;
458 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
459 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
461 print_spaces (depth, outfile);
462 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
464 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
465 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
468 fprintf_filtered (outfile, " section %s\n",
469 bfd_section_name (section->the_bfd_section->owner,
470 section->the_bfd_section));
472 fprintf_filtered (outfile, "\n");
475 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
477 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
479 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
483 fprintf_filtered (outfile, "%s %s = ",
484 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
486 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
487 ? "struct" : "union")),
488 SYMBOL_LINKAGE_NAME (symbol));
489 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
491 fprintf_filtered (outfile, ";\n");
495 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
496 fprintf_filtered (outfile, "typedef ");
497 if (SYMBOL_TYPE (symbol))
499 /* Print details of types, except for enums where it's clutter. */
500 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
502 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
504 fprintf_filtered (outfile, "; ");
507 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
509 switch (SYMBOL_CLASS (symbol))
512 fprintf_filtered (outfile, "const %s (%s)",
513 plongest (SYMBOL_VALUE (symbol)),
514 hex_string (SYMBOL_VALUE (symbol)));
517 case LOC_CONST_BYTES:
520 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
522 fprintf_filtered (outfile, "const %u hex bytes:",
524 for (i = 0; i < TYPE_LENGTH (type); i++)
525 fprintf_filtered (outfile, " %02x",
526 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
531 fprintf_filtered (outfile, "static at ");
532 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
535 fprintf_filtered (outfile, " section %s",
536 bfd_section_name (section->the_bfd_section->owner,
537 section->the_bfd_section));
541 if (SYMBOL_IS_ARGUMENT (symbol))
542 fprintf_filtered (outfile, "parameter register %s",
543 plongest (SYMBOL_VALUE (symbol)));
545 fprintf_filtered (outfile, "register %s",
546 plongest (SYMBOL_VALUE (symbol)));
550 fprintf_filtered (outfile, "arg at offset %s",
551 hex_string (SYMBOL_VALUE (symbol)));
555 fprintf_filtered (outfile, "reference arg at %s",
556 hex_string (SYMBOL_VALUE (symbol)));
559 case LOC_REGPARM_ADDR:
560 fprintf_filtered (outfile, "address parameter register %s",
561 plongest (SYMBOL_VALUE (symbol)));
565 fprintf_filtered (outfile, "local at offset %s",
566 hex_string (SYMBOL_VALUE (symbol)));
573 fprintf_filtered (outfile, "label at ");
574 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
577 fprintf_filtered (outfile, " section %s",
578 bfd_section_name (section->the_bfd_section->owner,
579 section->the_bfd_section));
583 fprintf_filtered (outfile, "block object ");
584 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
585 fprintf_filtered (outfile, ", ");
586 fputs_filtered (paddress (gdbarch,
587 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
589 fprintf_filtered (outfile, "..");
590 fputs_filtered (paddress (gdbarch,
591 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
594 fprintf_filtered (outfile, " section %s",
595 bfd_section_name (section->the_bfd_section->owner,
596 section->the_bfd_section));
600 fprintf_filtered (outfile, "computed at runtime");
604 fprintf_filtered (outfile, "unresolved");
607 case LOC_OPTIMIZED_OUT:
608 fprintf_filtered (outfile, "optimized out");
612 fprintf_filtered (outfile, "botched symbol class %x",
613 SYMBOL_CLASS (symbol));
617 fprintf_filtered (outfile, "\n");
622 maintenance_print_msymbols (char *args, int from_tty)
625 struct ui_file *outfile;
626 struct cleanup *cleanups;
627 char *filename = DEV_TTY;
628 char *symname = NULL;
629 struct program_space *pspace;
630 struct objfile *objfile;
632 struct stat sym_st, obj_st;
638 error (_("print-msymbols takes an output file "
639 "name and optional symbol file name"));
641 argv = gdb_buildargv (args);
642 cleanups = make_cleanup_freeargv (argv);
647 /* If a second arg is supplied, it is a source file name to match on. */
650 symname = xfullpath (argv[1]);
651 make_cleanup (xfree, symname);
652 if (symname && stat (symname, &sym_st))
653 perror_with_name (symname);
657 filename = tilde_expand (filename);
658 make_cleanup (xfree, filename);
660 outfile = gdb_fopen (filename, FOPEN_WT);
662 perror_with_name (filename);
663 make_cleanup_ui_file_delete (outfile);
667 ALL_PSPACE_OBJFILES (pspace, objfile)
668 if (symname == NULL || (!stat (objfile->name, &obj_st)
669 && sym_st.st_ino == obj_st.st_ino))
670 dump_msymbols (objfile, outfile);
672 fprintf_filtered (outfile, "\n\n");
673 do_cleanups (cleanups);
677 maintenance_print_objfiles (char *ignore, int from_tty)
679 struct program_space *pspace;
680 struct objfile *objfile;
686 ALL_PSPACE_OBJFILES (pspace, objfile)
687 dump_objfile (objfile);
692 /* List all the symbol tables whose names match REGEXP (optional). */
694 maintenance_info_symtabs (char *regexp, int from_tty)
696 struct program_space *pspace;
697 struct objfile *objfile;
703 ALL_PSPACE_OBJFILES (pspace, objfile)
705 struct symtab *symtab;
707 /* We don't want to print anything for this objfile until we
708 actually find a symtab whose name matches. */
709 int printed_objfile_start = 0;
711 ALL_OBJFILE_SYMTABS (objfile, symtab)
716 || re_exec (symtab->filename))
718 if (! printed_objfile_start)
720 printf_filtered ("{ objfile %s ", objfile->name);
722 printf_filtered ("((struct objfile *) %s)\n",
723 host_address_to_string (objfile));
724 printed_objfile_start = 1;
727 printf_filtered (" { symtab %s ", symtab->filename);
729 printf_filtered ("((struct symtab *) %s)\n",
730 host_address_to_string (symtab));
731 printf_filtered (" dirname %s\n",
732 symtab->dirname ? symtab->dirname : "(null)");
733 printf_filtered (" fullname %s\n",
734 symtab->fullname ? symtab->fullname : "(null)");
736 "blockvector ((struct blockvector *) %s)%s\n",
737 host_address_to_string (symtab->blockvector),
738 symtab->primary ? " (primary)" : "");
740 "linetable ((struct linetable *) %s)\n",
741 host_address_to_string (symtab->linetable));
742 printf_filtered (" debugformat %s\n",
743 symtab->debugformat);
744 printf_filtered (" }\n");
748 if (printed_objfile_start)
749 printf_filtered ("}\n");
754 /* Return the nexting depth of a block within other blocks in its symtab. */
757 block_depth (struct block *block)
761 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
769 /* Do early runtime initializations. */
771 _initialize_symmisc (void)