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"
41 #include "gdb_string.h"
42 #include "readline/readline.h"
47 #define DEV_TTY "/dev/tty"
50 /* Unfortunately for debugging, stderr is usually a macro. This is painful
51 when calling functions that take FILE *'s from the debugger.
52 So we make a variable which has the same value and which is accessible when
53 debugging GDB with itself. Because stdin et al need not be constants,
54 we initialize them in the _initialize_symmisc function at the bottom
60 /* Prototypes for local functions */
62 static void dump_symtab (struct objfile
*, struct symtab
*,
65 static void dump_msymbols (struct objfile
*, struct ui_file
*);
67 static void dump_objfile (struct objfile
*);
69 static int block_depth (struct block
*);
71 void _initialize_symmisc (void);
73 struct print_symbol_args
75 struct gdbarch
*gdbarch
;
76 struct symbol
*symbol
;
78 struct ui_file
*outfile
;
81 static int print_symbol (void *);
85 print_symbol_bcache_statistics (void)
87 struct program_space
*pspace
;
88 struct objfile
*objfile
;
91 ALL_PSPACE_OBJFILES (pspace
, objfile
)
94 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile
->name
);
95 print_bcache_statistics (psymbol_bcache_get_bcache (objfile
->psymbol_cache
),
96 "partial symbol cache");
97 print_bcache_statistics (objfile
->per_bfd
->macro_cache
,
98 "preprocessor macro cache");
99 print_bcache_statistics (objfile
->per_bfd
->filename_cache
,
105 print_objfile_statistics (void)
107 struct program_space
*pspace
;
108 struct objfile
*objfile
;
110 int i
, linetables
, blockvectors
;
113 ALL_PSPACE_OBJFILES (pspace
, objfile
)
116 printf_filtered (_("Statistics for '%s':\n"), objfile
->name
);
117 if (OBJSTAT (objfile
, n_stabs
) > 0)
118 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
119 OBJSTAT (objfile
, n_stabs
));
120 if (OBJSTAT (objfile
, n_minsyms
) > 0)
121 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
122 OBJSTAT (objfile
, n_minsyms
));
123 if (OBJSTAT (objfile
, n_psyms
) > 0)
124 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
125 OBJSTAT (objfile
, n_psyms
));
126 if (OBJSTAT (objfile
, n_syms
) > 0)
127 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
128 OBJSTAT (objfile
, n_syms
));
129 if (OBJSTAT (objfile
, n_types
) > 0)
130 printf_filtered (_(" Number of \"types\" defined: %d\n"),
131 OBJSTAT (objfile
, n_types
));
133 objfile
->sf
->qf
->print_stats (objfile
);
134 i
= linetables
= blockvectors
= 0;
135 ALL_OBJFILE_SYMTABS (objfile
, s
)
138 if (s
->linetable
!= NULL
)
143 printf_filtered (_(" Number of symbol tables: %d\n"), i
);
144 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
146 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
149 if (OBJSTAT (objfile
, sz_strtab
) > 0)
150 printf_filtered (_(" Space used by a.out string tables: %d\n"),
151 OBJSTAT (objfile
, sz_strtab
));
152 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
153 obstack_memory_used (&objfile
->objfile_obstack
));
154 printf_filtered (_(" Total memory used for BFD obstack: %d\n"),
155 obstack_memory_used (&objfile
->per_bfd
->storage_obstack
));
156 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
157 bcache_memory_used (psymbol_bcache_get_bcache
158 (objfile
->psymbol_cache
)));
159 printf_filtered (_(" Total memory used for macro cache: %d\n"),
160 bcache_memory_used (objfile
->per_bfd
->macro_cache
));
161 printf_filtered (_(" Total memory used for file name cache: %d\n"),
162 bcache_memory_used (objfile
->per_bfd
->filename_cache
));
167 dump_objfile (struct objfile
*objfile
)
169 struct symtab
*symtab
;
171 printf_filtered ("\nObject file %s: ", objfile
->name
);
172 printf_filtered ("Objfile at ");
173 gdb_print_host_address (objfile
, gdb_stdout
);
174 printf_filtered (", bfd at ");
175 gdb_print_host_address (objfile
->obfd
, gdb_stdout
);
176 printf_filtered (", %d minsyms\n\n",
177 objfile
->minimal_symbol_count
);
180 objfile
->sf
->qf
->dump (objfile
);
182 if (objfile
->symtabs
)
184 printf_filtered ("Symtabs:\n");
185 for (symtab
= objfile
->symtabs
;
187 symtab
= symtab
->next
)
189 printf_filtered ("%s at ", symtab_to_filename_for_display (symtab
));
190 gdb_print_host_address (symtab
, gdb_stdout
);
191 printf_filtered (", ");
192 if (symtab
->objfile
!= objfile
)
194 printf_filtered ("NOT ON CHAIN! ");
198 printf_filtered ("\n\n");
202 /* Print minimal symbols from this objfile. */
205 dump_msymbols (struct objfile
*objfile
, struct ui_file
*outfile
)
207 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
208 struct minimal_symbol
*msymbol
;
212 fprintf_filtered (outfile
, "\nObject file %s:\n\n", objfile
->name
);
213 if (objfile
->minimal_symbol_count
== 0)
215 fprintf_filtered (outfile
, "No minimal symbols found.\n");
219 ALL_OBJFILE_MSYMBOLS (objfile
, msymbol
)
221 struct obj_section
*section
= SYMBOL_OBJ_SECTION (objfile
, msymbol
);
223 switch (MSYMBOL_TYPE (msymbol
))
231 case mst_text_gnu_ifunc
:
234 case mst_solib_trampoline
:
259 fprintf_filtered (outfile
, "[%2d] %c ", index
, ms_type
);
260 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (msymbol
)),
262 fprintf_filtered (outfile
, " %s", SYMBOL_LINKAGE_NAME (msymbol
));
264 fprintf_filtered (outfile
, " section %s",
265 bfd_section_name (objfile
->obfd
,
266 section
->the_bfd_section
));
267 if (SYMBOL_DEMANGLED_NAME (msymbol
) != NULL
)
269 fprintf_filtered (outfile
, " %s", SYMBOL_DEMANGLED_NAME (msymbol
));
271 if (msymbol
->filename
)
272 fprintf_filtered (outfile
, " %s", msymbol
->filename
);
273 fputs_filtered ("\n", outfile
);
276 if (objfile
->minimal_symbol_count
!= index
)
278 warning (_("internal error: minimal symbol count %d != %d"),
279 objfile
->minimal_symbol_count
, index
);
281 fprintf_filtered (outfile
, "\n");
285 dump_symtab_1 (struct objfile
*objfile
, struct symtab
*symtab
,
286 struct ui_file
*outfile
)
288 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
290 struct dict_iterator iter
;
293 struct blockvector
*bv
;
298 fprintf_filtered (outfile
, "\nSymtab for file %s\n",
299 symtab_to_filename_for_display (symtab
));
301 fprintf_filtered (outfile
, "Compilation directory is %s\n",
303 fprintf_filtered (outfile
, "Read from object file %s (", objfile
->name
);
304 gdb_print_host_address (objfile
, outfile
);
305 fprintf_filtered (outfile
, ")\n");
306 fprintf_filtered (outfile
, "Language: %s\n",
307 language_str (symtab
->language
));
309 /* First print the line table. */
310 l
= LINETABLE (symtab
);
313 fprintf_filtered (outfile
, "\nLine table:\n\n");
315 for (i
= 0; i
< len
; i
++)
317 fprintf_filtered (outfile
, " line %d at ", l
->item
[i
].line
);
318 fputs_filtered (paddress (gdbarch
, l
->item
[i
].pc
), outfile
);
319 fprintf_filtered (outfile
, "\n");
322 /* Now print the block info, but only for primary symtabs since we will
323 print lots of duplicate info otherwise. */
326 fprintf_filtered (outfile
, "\nBlockvector:\n\n");
327 bv
= BLOCKVECTOR (symtab
);
328 len
= BLOCKVECTOR_NBLOCKS (bv
);
329 for (i
= 0; i
< len
; i
++)
331 b
= BLOCKVECTOR_BLOCK (bv
, i
);
332 depth
= block_depth (b
) * 2;
333 print_spaces (depth
, outfile
);
334 fprintf_filtered (outfile
, "block #%03d, object at ", i
);
335 gdb_print_host_address (b
, outfile
);
336 if (BLOCK_SUPERBLOCK (b
))
338 fprintf_filtered (outfile
, " under ");
339 gdb_print_host_address (BLOCK_SUPERBLOCK (b
), outfile
);
341 /* drow/2002-07-10: We could save the total symbols count
342 even if we're using a hashtable, but nothing else but this message
344 fprintf_filtered (outfile
, ", %d syms/buckets in ",
345 dict_size (BLOCK_DICT (b
)));
346 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), outfile
);
347 fprintf_filtered (outfile
, "..");
348 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), outfile
);
349 if (BLOCK_FUNCTION (b
))
351 fprintf_filtered (outfile
, ", function %s",
352 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b
)));
353 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)) != NULL
)
355 fprintf_filtered (outfile
, ", %s",
356 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)));
359 fprintf_filtered (outfile
, "\n");
360 /* Now print each symbol in this block (in no particular order, if
361 we're using a hashtable). Note that we only want this
362 block, not any blocks from included symtabs. */
363 ALL_DICT_SYMBOLS (BLOCK_DICT (b
), iter
, sym
)
365 struct print_symbol_args s
;
371 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
375 fprintf_filtered (outfile
, "\n");
379 fprintf_filtered (outfile
, "\nBlockvector same as previous symtab\n\n");
384 dump_symtab (struct objfile
*objfile
, struct symtab
*symtab
,
385 struct ui_file
*outfile
)
387 /* Set the current language to the language of the symtab we're dumping
388 because certain routines used during dump_symtab() use the current
389 language to print an image of the symbol. We'll restore it later.
390 But use only real languages, not placeholders. */
391 if (symtab
->language
!= language_unknown
392 && symtab
->language
!= language_auto
)
394 enum language saved_lang
;
396 saved_lang
= set_language (symtab
->language
);
398 dump_symtab_1 (objfile
, symtab
, outfile
);
400 set_language (saved_lang
);
403 dump_symtab_1 (objfile
, symtab
, outfile
);
407 maintenance_print_symbols (char *args
, int from_tty
)
410 struct ui_file
*outfile
;
411 struct cleanup
*cleanups
;
412 char *symname
= NULL
;
413 char *filename
= DEV_TTY
;
414 struct objfile
*objfile
;
421 error (_("Arguments missing: an output file name "
422 "and an optional symbol file name"));
424 argv
= gdb_buildargv (args
);
425 cleanups
= make_cleanup_freeargv (argv
);
430 /* If a second arg is supplied, it is a source file name to match on. */
437 filename
= tilde_expand (filename
);
438 make_cleanup (xfree
, filename
);
440 outfile
= gdb_fopen (filename
, FOPEN_WT
);
442 perror_with_name (filename
);
443 make_cleanup_ui_file_delete (outfile
);
445 ALL_SYMTABS (objfile
, s
)
449 || filename_cmp (symname
, symtab_to_filename_for_display (s
)) == 0)
450 dump_symtab (objfile
, s
, outfile
);
452 do_cleanups (cleanups
);
455 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
456 far to indent. ARGS is really a struct print_symbol_args *, but is
457 declared as char * to get it past catch_errors. Returns 0 for error,
461 print_symbol (void *args
)
463 struct gdbarch
*gdbarch
= ((struct print_symbol_args
*) args
)->gdbarch
;
464 struct symbol
*symbol
= ((struct print_symbol_args
*) args
)->symbol
;
465 int depth
= ((struct print_symbol_args
*) args
)->depth
;
466 struct ui_file
*outfile
= ((struct print_symbol_args
*) args
)->outfile
;
467 struct obj_section
*section
= SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (symbol
),
470 print_spaces (depth
, outfile
);
471 if (SYMBOL_DOMAIN (symbol
) == LABEL_DOMAIN
)
473 fprintf_filtered (outfile
, "label %s at ", SYMBOL_PRINT_NAME (symbol
));
474 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
477 fprintf_filtered (outfile
, " section %s\n",
478 bfd_section_name (section
->the_bfd_section
->owner
,
479 section
->the_bfd_section
));
481 fprintf_filtered (outfile
, "\n");
484 if (SYMBOL_DOMAIN (symbol
) == STRUCT_DOMAIN
)
486 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol
)))
488 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
,
489 &type_print_raw_options
);
493 fprintf_filtered (outfile
, "%s %s = ",
494 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
496 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
497 ? "struct" : "union")),
498 SYMBOL_LINKAGE_NAME (symbol
));
499 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
,
500 &type_print_raw_options
);
502 fprintf_filtered (outfile
, ";\n");
506 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
507 fprintf_filtered (outfile
, "typedef ");
508 if (SYMBOL_TYPE (symbol
))
510 /* Print details of types, except for enums where it's clutter. */
511 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_PRINT_NAME (symbol
),
513 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
515 &type_print_raw_options
);
516 fprintf_filtered (outfile
, "; ");
519 fprintf_filtered (outfile
, "%s ", SYMBOL_PRINT_NAME (symbol
));
521 switch (SYMBOL_CLASS (symbol
))
524 fprintf_filtered (outfile
, "const %s (%s)",
525 plongest (SYMBOL_VALUE (symbol
)),
526 hex_string (SYMBOL_VALUE (symbol
)));
529 case LOC_CONST_BYTES
:
532 struct type
*type
= check_typedef (SYMBOL_TYPE (symbol
));
534 fprintf_filtered (outfile
, "const %u hex bytes:",
536 for (i
= 0; i
< TYPE_LENGTH (type
); i
++)
537 fprintf_filtered (outfile
, " %02x",
538 (unsigned) SYMBOL_VALUE_BYTES (symbol
)[i
]);
543 fprintf_filtered (outfile
, "static at ");
544 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
547 fprintf_filtered (outfile
, " section %s",
548 bfd_section_name (section
->the_bfd_section
->owner
,
549 section
->the_bfd_section
));
553 if (SYMBOL_IS_ARGUMENT (symbol
))
554 fprintf_filtered (outfile
, "parameter register %s",
555 plongest (SYMBOL_VALUE (symbol
)));
557 fprintf_filtered (outfile
, "register %s",
558 plongest (SYMBOL_VALUE (symbol
)));
562 fprintf_filtered (outfile
, "arg at offset %s",
563 hex_string (SYMBOL_VALUE (symbol
)));
567 fprintf_filtered (outfile
, "reference arg at %s",
568 hex_string (SYMBOL_VALUE (symbol
)));
571 case LOC_REGPARM_ADDR
:
572 fprintf_filtered (outfile
, "address parameter register %s",
573 plongest (SYMBOL_VALUE (symbol
)));
577 fprintf_filtered (outfile
, "local at offset %s",
578 hex_string (SYMBOL_VALUE (symbol
)));
585 fprintf_filtered (outfile
, "label at ");
586 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
589 fprintf_filtered (outfile
, " section %s",
590 bfd_section_name (section
->the_bfd_section
->owner
,
591 section
->the_bfd_section
));
595 fprintf_filtered (outfile
, "block object ");
596 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol
), outfile
);
597 fprintf_filtered (outfile
, ", ");
598 fputs_filtered (paddress (gdbarch
,
599 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
))),
601 fprintf_filtered (outfile
, "..");
602 fputs_filtered (paddress (gdbarch
,
603 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol
))),
606 fprintf_filtered (outfile
, " section %s",
607 bfd_section_name (section
->the_bfd_section
->owner
,
608 section
->the_bfd_section
));
612 fprintf_filtered (outfile
, "computed at runtime");
616 fprintf_filtered (outfile
, "unresolved");
619 case LOC_OPTIMIZED_OUT
:
620 fprintf_filtered (outfile
, "optimized out");
624 fprintf_filtered (outfile
, "botched symbol class %x",
625 SYMBOL_CLASS (symbol
));
629 fprintf_filtered (outfile
, "\n");
634 maintenance_print_msymbols (char *args
, int from_tty
)
637 struct ui_file
*outfile
;
638 struct cleanup
*cleanups
;
639 char *filename
= DEV_TTY
;
640 char *symname
= NULL
;
641 struct program_space
*pspace
;
642 struct objfile
*objfile
;
644 struct stat sym_st
, obj_st
;
650 error (_("print-msymbols takes an output file "
651 "name and optional symbol file name"));
653 argv
= gdb_buildargv (args
);
654 cleanups
= make_cleanup_freeargv (argv
);
659 /* If a second arg is supplied, it is a source file name to match on. */
662 symname
= gdb_realpath (argv
[1]);
663 make_cleanup (xfree
, symname
);
664 if (symname
&& stat (symname
, &sym_st
))
665 perror_with_name (symname
);
669 filename
= tilde_expand (filename
);
670 make_cleanup (xfree
, filename
);
672 outfile
= gdb_fopen (filename
, FOPEN_WT
);
674 perror_with_name (filename
);
675 make_cleanup_ui_file_delete (outfile
);
678 ALL_PSPACE_OBJFILES (pspace
, objfile
)
681 if (symname
== NULL
|| (!stat (objfile
->name
, &obj_st
)
682 && sym_st
.st_dev
== obj_st
.st_dev
683 && sym_st
.st_ino
== obj_st
.st_ino
))
684 dump_msymbols (objfile
, outfile
);
686 fprintf_filtered (outfile
, "\n\n");
687 do_cleanups (cleanups
);
691 maintenance_print_objfiles (char *ignore
, int from_tty
)
693 struct program_space
*pspace
;
694 struct objfile
*objfile
;
699 ALL_PSPACE_OBJFILES (pspace
, objfile
)
702 dump_objfile (objfile
);
706 /* List all the symbol tables whose names match REGEXP (optional). */
709 maintenance_info_symtabs (char *regexp
, int from_tty
)
711 struct program_space
*pspace
;
712 struct objfile
*objfile
;
718 ALL_PSPACE_OBJFILES (pspace
, objfile
)
720 struct symtab
*symtab
;
722 /* We don't want to print anything for this objfile until we
723 actually find a symtab whose name matches. */
724 int printed_objfile_start
= 0;
726 ALL_OBJFILE_SYMTABS (objfile
, symtab
)
731 || re_exec (symtab_to_filename_for_display (symtab
)))
733 if (! printed_objfile_start
)
735 printf_filtered ("{ objfile %s ", objfile
->name
);
737 printf_filtered ("((struct objfile *) %s)\n",
738 host_address_to_string (objfile
));
739 printed_objfile_start
= 1;
742 printf_filtered (" { symtab %s ",
743 symtab_to_filename_for_display (symtab
));
745 printf_filtered ("((struct symtab *) %s)\n",
746 host_address_to_string (symtab
));
747 printf_filtered (" dirname %s\n",
748 symtab
->dirname
? symtab
->dirname
: "(null)");
749 printf_filtered (" fullname %s\n",
750 symtab
->fullname
? symtab
->fullname
: "(null)");
752 "blockvector ((struct blockvector *) %s)%s\n",
753 host_address_to_string (symtab
->blockvector
),
754 symtab
->primary
? " (primary)" : "");
756 "linetable ((struct linetable *) %s)\n",
757 host_address_to_string (symtab
->linetable
));
758 printf_filtered (" debugformat %s\n",
759 symtab
->debugformat
);
760 printf_filtered (" }\n");
764 if (printed_objfile_start
)
765 printf_filtered ("}\n");
770 /* Return the nexting depth of a block within other blocks in its symtab. */
773 block_depth (struct block
*block
)
777 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
785 /* Do early runtime initializations. */
788 _initialize_symmisc (void)
794 add_cmd ("symbols", class_maintenance
, maintenance_print_symbols
, _("\
795 Print dump of current symbol definitions.\n\
796 Entries in the full symbol table are dumped to file OUTFILE.\n\
797 If a SOURCE file is specified, dump only that file's symbols."),
798 &maintenanceprintlist
);
800 add_cmd ("msymbols", class_maintenance
, maintenance_print_msymbols
, _("\
801 Print dump of current minimal symbol definitions.\n\
802 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
803 If a SOURCE file is specified, dump only that file's minimal symbols."),
804 &maintenanceprintlist
);
806 add_cmd ("objfiles", class_maintenance
, maintenance_print_objfiles
,
807 _("Print dump of current object file definitions."),
808 &maintenanceprintlist
);
810 add_cmd ("symtabs", class_maintenance
, maintenance_info_symtabs
, _("\
811 List the full symbol tables for all object files.\n\
812 This does not include information about individual symbols, blocks, or\n\
813 linetables --- just the symbol table structures themselves.\n\
814 With an argument REGEXP, list the symbol tables whose names that match that."),
815 &maintenanceinfolist
);