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;
89 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->per_bfd->filename_cache,
102 print_objfile_statistics (void)
104 struct program_space *pspace;
105 struct objfile *objfile;
107 int i, linetables, blockvectors;
110 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 BFD obstack: %d\n"),
152 obstack_memory_used (&objfile->per_bfd->storage_obstack));
153 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
154 bcache_memory_used (psymbol_bcache_get_bcache
155 (objfile->psymbol_cache)));
156 printf_filtered (_(" Total memory used for macro cache: %d\n"),
157 bcache_memory_used (objfile->macro_cache));
158 printf_filtered (_(" Total memory used for file name cache: %d\n"),
159 bcache_memory_used (objfile->per_bfd->filename_cache));
164 dump_objfile (struct objfile *objfile)
166 struct symtab *symtab;
168 printf_filtered ("\nObject file %s: ", objfile->name);
169 printf_filtered ("Objfile at ");
170 gdb_print_host_address (objfile, gdb_stdout);
171 printf_filtered (", bfd at ");
172 gdb_print_host_address (objfile->obfd, gdb_stdout);
173 printf_filtered (", %d minsyms\n\n",
174 objfile->minimal_symbol_count);
177 objfile->sf->qf->dump (objfile);
179 if (objfile->symtabs)
181 printf_filtered ("Symtabs:\n");
182 for (symtab = objfile->symtabs;
184 symtab = symtab->next)
186 printf_filtered ("%s at ", symtab->filename);
187 gdb_print_host_address (symtab, gdb_stdout);
188 printf_filtered (", ");
189 if (symtab->objfile != objfile)
191 printf_filtered ("NOT ON CHAIN! ");
195 printf_filtered ("\n\n");
199 /* Print minimal symbols from this objfile. */
202 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
204 struct gdbarch *gdbarch = get_objfile_arch (objfile);
205 struct minimal_symbol *msymbol;
209 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
210 if (objfile->minimal_symbol_count == 0)
212 fprintf_filtered (outfile, "No minimal symbols found.\n");
216 ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
218 struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
220 switch (MSYMBOL_TYPE (msymbol))
228 case mst_text_gnu_ifunc:
231 case mst_solib_trampoline:
256 fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
257 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
259 fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
261 fprintf_filtered (outfile, " section %s",
262 bfd_section_name (objfile->obfd,
263 section->the_bfd_section));
264 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
266 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
268 if (msymbol->filename)
269 fprintf_filtered (outfile, " %s", msymbol->filename);
270 fputs_filtered ("\n", outfile);
273 if (objfile->minimal_symbol_count != index)
275 warning (_("internal error: minimal symbol count %d != %d"),
276 objfile->minimal_symbol_count, index);
278 fprintf_filtered (outfile, "\n");
282 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
283 struct ui_file *outfile)
285 struct gdbarch *gdbarch = get_objfile_arch (objfile);
287 struct dict_iterator iter;
290 struct blockvector *bv;
295 fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
297 fprintf_filtered (outfile, "Compilation directory is %s\n",
299 fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
300 gdb_print_host_address (objfile, outfile);
301 fprintf_filtered (outfile, ")\n");
302 fprintf_filtered (outfile, "Language: %s\n",
303 language_str (symtab->language));
305 /* First print the line table. */
306 l = LINETABLE (symtab);
309 fprintf_filtered (outfile, "\nLine table:\n\n");
311 for (i = 0; i < len; i++)
313 fprintf_filtered (outfile, " line %d at ", l->item[i].line);
314 fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
315 fprintf_filtered (outfile, "\n");
318 /* Now print the block info, but only for primary symtabs since we will
319 print lots of duplicate info otherwise. */
322 fprintf_filtered (outfile, "\nBlockvector:\n\n");
323 bv = BLOCKVECTOR (symtab);
324 len = BLOCKVECTOR_NBLOCKS (bv);
325 for (i = 0; i < len; i++)
327 b = BLOCKVECTOR_BLOCK (bv, i);
328 depth = block_depth (b) * 2;
329 print_spaces (depth, outfile);
330 fprintf_filtered (outfile, "block #%03d, object at ", i);
331 gdb_print_host_address (b, outfile);
332 if (BLOCK_SUPERBLOCK (b))
334 fprintf_filtered (outfile, " under ");
335 gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
337 /* drow/2002-07-10: We could save the total symbols count
338 even if we're using a hashtable, but nothing else but this message
340 fprintf_filtered (outfile, ", %d syms/buckets in ",
341 dict_size (BLOCK_DICT (b)));
342 fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
343 fprintf_filtered (outfile, "..");
344 fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
345 if (BLOCK_FUNCTION (b))
347 fprintf_filtered (outfile, ", function %s",
348 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
349 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
351 fprintf_filtered (outfile, ", %s",
352 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
355 fprintf_filtered (outfile, "\n");
356 /* Now print each symbol in this block (in no particular order, if
357 we're using a hashtable). Note that we only want this
358 block, not any blocks from included symtabs. */
359 ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
361 struct print_symbol_args s;
367 catch_errors (print_symbol, &s, "Error printing symbol:\n",
371 fprintf_filtered (outfile, "\n");
375 fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
380 dump_symtab (struct objfile *objfile, struct symtab *symtab,
381 struct ui_file *outfile)
383 /* Set the current language to the language of the symtab we're dumping
384 because certain routines used during dump_symtab() use the current
385 language to print an image of the symbol. We'll restore it later.
386 But use only real languages, not placeholders. */
387 if (symtab->language != language_unknown
388 && symtab->language != language_auto)
390 enum language saved_lang;
392 saved_lang = set_language (symtab->language);
394 dump_symtab_1 (objfile, symtab, outfile);
396 set_language (saved_lang);
399 dump_symtab_1 (objfile, symtab, outfile);
403 maintenance_print_symbols (char *args, int from_tty)
406 struct ui_file *outfile;
407 struct cleanup *cleanups;
408 char *symname = NULL;
409 char *filename = DEV_TTY;
410 struct objfile *objfile;
417 error (_("Arguments missing: an output file name "
418 "and an optional symbol file name"));
420 argv = gdb_buildargv (args);
421 cleanups = make_cleanup_freeargv (argv);
426 /* If a second arg is supplied, it is a source file name to match on. */
433 filename = tilde_expand (filename);
434 make_cleanup (xfree, filename);
436 outfile = gdb_fopen (filename, FOPEN_WT);
438 perror_with_name (filename);
439 make_cleanup_ui_file_delete (outfile);
441 ALL_SYMTABS (objfile, s)
444 if (symname == NULL || filename_cmp (symname, s->filename) == 0)
445 dump_symtab (objfile, s, outfile);
447 do_cleanups (cleanups);
450 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
451 far to indent. ARGS is really a struct print_symbol_args *, but is
452 declared as char * to get it past catch_errors. Returns 0 for error,
456 print_symbol (void *args)
458 struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
459 struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
460 int depth = ((struct print_symbol_args *) args)->depth;
461 struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
462 struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
464 print_spaces (depth, outfile);
465 if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
467 fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
468 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
471 fprintf_filtered (outfile, " section %s\n",
472 bfd_section_name (section->the_bfd_section->owner,
473 section->the_bfd_section));
475 fprintf_filtered (outfile, "\n");
478 if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
480 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
482 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
486 fprintf_filtered (outfile, "%s %s = ",
487 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
489 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
490 ? "struct" : "union")),
491 SYMBOL_LINKAGE_NAME (symbol));
492 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
494 fprintf_filtered (outfile, ";\n");
498 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
499 fprintf_filtered (outfile, "typedef ");
500 if (SYMBOL_TYPE (symbol))
502 /* Print details of types, except for enums where it's clutter. */
503 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
505 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
507 fprintf_filtered (outfile, "; ");
510 fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
512 switch (SYMBOL_CLASS (symbol))
515 fprintf_filtered (outfile, "const %s (%s)",
516 plongest (SYMBOL_VALUE (symbol)),
517 hex_string (SYMBOL_VALUE (symbol)));
520 case LOC_CONST_BYTES:
523 struct type *type = check_typedef (SYMBOL_TYPE (symbol));
525 fprintf_filtered (outfile, "const %u hex bytes:",
527 for (i = 0; i < TYPE_LENGTH (type); i++)
528 fprintf_filtered (outfile, " %02x",
529 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
534 fprintf_filtered (outfile, "static at ");
535 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
538 fprintf_filtered (outfile, " section %s",
539 bfd_section_name (section->the_bfd_section->owner,
540 section->the_bfd_section));
544 if (SYMBOL_IS_ARGUMENT (symbol))
545 fprintf_filtered (outfile, "parameter register %s",
546 plongest (SYMBOL_VALUE (symbol)));
548 fprintf_filtered (outfile, "register %s",
549 plongest (SYMBOL_VALUE (symbol)));
553 fprintf_filtered (outfile, "arg at offset %s",
554 hex_string (SYMBOL_VALUE (symbol)));
558 fprintf_filtered (outfile, "reference arg at %s",
559 hex_string (SYMBOL_VALUE (symbol)));
562 case LOC_REGPARM_ADDR:
563 fprintf_filtered (outfile, "address parameter register %s",
564 plongest (SYMBOL_VALUE (symbol)));
568 fprintf_filtered (outfile, "local at offset %s",
569 hex_string (SYMBOL_VALUE (symbol)));
576 fprintf_filtered (outfile, "label at ");
577 fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
580 fprintf_filtered (outfile, " section %s",
581 bfd_section_name (section->the_bfd_section->owner,
582 section->the_bfd_section));
586 fprintf_filtered (outfile, "block object ");
587 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
588 fprintf_filtered (outfile, ", ");
589 fputs_filtered (paddress (gdbarch,
590 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
592 fprintf_filtered (outfile, "..");
593 fputs_filtered (paddress (gdbarch,
594 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
597 fprintf_filtered (outfile, " section %s",
598 bfd_section_name (section->the_bfd_section->owner,
599 section->the_bfd_section));
603 fprintf_filtered (outfile, "computed at runtime");
607 fprintf_filtered (outfile, "unresolved");
610 case LOC_OPTIMIZED_OUT:
611 fprintf_filtered (outfile, "optimized out");
615 fprintf_filtered (outfile, "botched symbol class %x",
616 SYMBOL_CLASS (symbol));
620 fprintf_filtered (outfile, "\n");
625 maintenance_print_msymbols (char *args, int from_tty)
628 struct ui_file *outfile;
629 struct cleanup *cleanups;
630 char *filename = DEV_TTY;
631 char *symname = NULL;
632 struct program_space *pspace;
633 struct objfile *objfile;
635 struct stat sym_st, obj_st;
641 error (_("print-msymbols takes an output file "
642 "name and optional symbol file name"));
644 argv = gdb_buildargv (args);
645 cleanups = make_cleanup_freeargv (argv);
650 /* If a second arg is supplied, it is a source file name to match on. */
653 symname = xfullpath (argv[1]);
654 make_cleanup (xfree, symname);
655 if (symname && stat (symname, &sym_st))
656 perror_with_name (symname);
660 filename = tilde_expand (filename);
661 make_cleanup (xfree, filename);
663 outfile = gdb_fopen (filename, FOPEN_WT);
665 perror_with_name (filename);
666 make_cleanup_ui_file_delete (outfile);
669 ALL_PSPACE_OBJFILES (pspace, objfile)
672 if (symname == NULL || (!stat (objfile->name, &obj_st)
673 && sym_st.st_ino == obj_st.st_ino))
674 dump_msymbols (objfile, outfile);
676 fprintf_filtered (outfile, "\n\n");
677 do_cleanups (cleanups);
681 maintenance_print_objfiles (char *ignore, int from_tty)
683 struct program_space *pspace;
684 struct objfile *objfile;
689 ALL_PSPACE_OBJFILES (pspace, objfile)
692 dump_objfile (objfile);
697 /* List all the symbol tables whose names match REGEXP (optional). */
699 maintenance_info_symtabs (char *regexp, int from_tty)
701 struct program_space *pspace;
702 struct objfile *objfile;
708 ALL_PSPACE_OBJFILES (pspace, objfile)
710 struct symtab *symtab;
712 /* We don't want to print anything for this objfile until we
713 actually find a symtab whose name matches. */
714 int printed_objfile_start = 0;
716 ALL_OBJFILE_SYMTABS (objfile, symtab)
721 || re_exec (symtab->filename))
723 if (! printed_objfile_start)
725 printf_filtered ("{ objfile %s ", objfile->name);
727 printf_filtered ("((struct objfile *) %s)\n",
728 host_address_to_string (objfile));
729 printed_objfile_start = 1;
732 printf_filtered (" { symtab %s ", symtab->filename);
734 printf_filtered ("((struct symtab *) %s)\n",
735 host_address_to_string (symtab));
736 printf_filtered (" dirname %s\n",
737 symtab->dirname ? symtab->dirname : "(null)");
738 printf_filtered (" fullname %s\n",
739 symtab->fullname ? symtab->fullname : "(null)");
741 "blockvector ((struct blockvector *) %s)%s\n",
742 host_address_to_string (symtab->blockvector),
743 symtab->primary ? " (primary)" : "");
745 "linetable ((struct linetable *) %s)\n",
746 host_address_to_string (symtab->linetable));
747 printf_filtered (" debugformat %s\n",
748 symtab->debugformat);
749 printf_filtered (" }\n");
753 if (printed_objfile_start)
754 printf_filtered ("}\n");
759 /* Return the nexting depth of a block within other blocks in its symtab. */
762 block_depth (struct block *block)
766 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
774 /* Do early runtime initializations. */
776 _initialize_symmisc (void)