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
->per_bfd
->macro_cache
,
96 "preprocessor macro cache");
97 print_bcache_statistics (objfile
->per_bfd
->filename_cache
,
103 print_objfile_statistics (void)
105 struct program_space
*pspace
;
106 struct objfile
*objfile
;
108 int i
, linetables
, blockvectors
;
111 ALL_PSPACE_OBJFILES (pspace
, objfile
)
114 printf_filtered (_("Statistics for '%s':\n"), objfile
->name
);
115 if (OBJSTAT (objfile
, n_stabs
) > 0)
116 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
117 OBJSTAT (objfile
, n_stabs
));
118 if (OBJSTAT (objfile
, n_minsyms
) > 0)
119 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
120 OBJSTAT (objfile
, n_minsyms
));
121 if (OBJSTAT (objfile
, n_psyms
) > 0)
122 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
123 OBJSTAT (objfile
, n_psyms
));
124 if (OBJSTAT (objfile
, n_syms
) > 0)
125 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
126 OBJSTAT (objfile
, n_syms
));
127 if (OBJSTAT (objfile
, n_types
) > 0)
128 printf_filtered (_(" Number of \"types\" defined: %d\n"),
129 OBJSTAT (objfile
, n_types
));
131 objfile
->sf
->qf
->print_stats (objfile
);
132 i
= linetables
= blockvectors
= 0;
133 ALL_OBJFILE_SYMTABS (objfile
, s
)
136 if (s
->linetable
!= NULL
)
141 printf_filtered (_(" Number of symbol tables: %d\n"), i
);
142 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
144 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
147 if (OBJSTAT (objfile
, sz_strtab
) > 0)
148 printf_filtered (_(" Space used by a.out string tables: %d\n"),
149 OBJSTAT (objfile
, sz_strtab
));
150 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
151 obstack_memory_used (&objfile
->objfile_obstack
));
152 printf_filtered (_(" Total memory used for BFD obstack: %d\n"),
153 obstack_memory_used (&objfile
->per_bfd
->storage_obstack
));
154 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
155 bcache_memory_used (psymbol_bcache_get_bcache
156 (objfile
->psymbol_cache
)));
157 printf_filtered (_(" Total memory used for macro cache: %d\n"),
158 bcache_memory_used (objfile
->per_bfd
->macro_cache
));
159 printf_filtered (_(" Total memory used for file name cache: %d\n"),
160 bcache_memory_used (objfile
->per_bfd
->filename_cache
));
165 dump_objfile (struct objfile
*objfile
)
167 struct symtab
*symtab
;
169 printf_filtered ("\nObject file %s: ", objfile
->name
);
170 printf_filtered ("Objfile at ");
171 gdb_print_host_address (objfile
, gdb_stdout
);
172 printf_filtered (", bfd at ");
173 gdb_print_host_address (objfile
->obfd
, gdb_stdout
);
174 printf_filtered (", %d minsyms\n\n",
175 objfile
->minimal_symbol_count
);
178 objfile
->sf
->qf
->dump (objfile
);
180 if (objfile
->symtabs
)
182 printf_filtered ("Symtabs:\n");
183 for (symtab
= objfile
->symtabs
;
185 symtab
= symtab
->next
)
187 printf_filtered ("%s at ", symtab
->filename
);
188 gdb_print_host_address (symtab
, gdb_stdout
);
189 printf_filtered (", ");
190 if (symtab
->objfile
!= objfile
)
192 printf_filtered ("NOT ON CHAIN! ");
196 printf_filtered ("\n\n");
200 /* Print minimal symbols from this objfile. */
203 dump_msymbols (struct objfile
*objfile
, struct ui_file
*outfile
)
205 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
206 struct minimal_symbol
*msymbol
;
210 fprintf_filtered (outfile
, "\nObject file %s:\n\n", objfile
->name
);
211 if (objfile
->minimal_symbol_count
== 0)
213 fprintf_filtered (outfile
, "No minimal symbols found.\n");
217 ALL_OBJFILE_MSYMBOLS (objfile
, msymbol
)
219 struct obj_section
*section
= SYMBOL_OBJ_SECTION (msymbol
);
221 switch (MSYMBOL_TYPE (msymbol
))
229 case mst_text_gnu_ifunc
:
232 case mst_solib_trampoline
:
257 fprintf_filtered (outfile
, "[%2d] %c ", index
, ms_type
);
258 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (msymbol
)),
260 fprintf_filtered (outfile
, " %s", SYMBOL_LINKAGE_NAME (msymbol
));
262 fprintf_filtered (outfile
, " section %s",
263 bfd_section_name (objfile
->obfd
,
264 section
->the_bfd_section
));
265 if (SYMBOL_DEMANGLED_NAME (msymbol
) != NULL
)
267 fprintf_filtered (outfile
, " %s", SYMBOL_DEMANGLED_NAME (msymbol
));
269 if (msymbol
->filename
)
270 fprintf_filtered (outfile
, " %s", msymbol
->filename
);
271 fputs_filtered ("\n", outfile
);
274 if (objfile
->minimal_symbol_count
!= index
)
276 warning (_("internal error: minimal symbol count %d != %d"),
277 objfile
->minimal_symbol_count
, index
);
279 fprintf_filtered (outfile
, "\n");
283 dump_symtab_1 (struct objfile
*objfile
, struct symtab
*symtab
,
284 struct ui_file
*outfile
)
286 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
288 struct dict_iterator iter
;
291 struct blockvector
*bv
;
296 fprintf_filtered (outfile
, "\nSymtab for file %s\n", symtab
->filename
);
298 fprintf_filtered (outfile
, "Compilation directory is %s\n",
300 fprintf_filtered (outfile
, "Read from object file %s (", objfile
->name
);
301 gdb_print_host_address (objfile
, outfile
);
302 fprintf_filtered (outfile
, ")\n");
303 fprintf_filtered (outfile
, "Language: %s\n",
304 language_str (symtab
->language
));
306 /* First print the line table. */
307 l
= LINETABLE (symtab
);
310 fprintf_filtered (outfile
, "\nLine table:\n\n");
312 for (i
= 0; i
< len
; i
++)
314 fprintf_filtered (outfile
, " line %d at ", l
->item
[i
].line
);
315 fputs_filtered (paddress (gdbarch
, l
->item
[i
].pc
), outfile
);
316 fprintf_filtered (outfile
, "\n");
319 /* Now print the block info, but only for primary symtabs since we will
320 print lots of duplicate info otherwise. */
323 fprintf_filtered (outfile
, "\nBlockvector:\n\n");
324 bv
= BLOCKVECTOR (symtab
);
325 len
= BLOCKVECTOR_NBLOCKS (bv
);
326 for (i
= 0; i
< len
; i
++)
328 b
= BLOCKVECTOR_BLOCK (bv
, i
);
329 depth
= block_depth (b
) * 2;
330 print_spaces (depth
, outfile
);
331 fprintf_filtered (outfile
, "block #%03d, object at ", i
);
332 gdb_print_host_address (b
, outfile
);
333 if (BLOCK_SUPERBLOCK (b
))
335 fprintf_filtered (outfile
, " under ");
336 gdb_print_host_address (BLOCK_SUPERBLOCK (b
), outfile
);
338 /* drow/2002-07-10: We could save the total symbols count
339 even if we're using a hashtable, but nothing else but this message
341 fprintf_filtered (outfile
, ", %d syms/buckets in ",
342 dict_size (BLOCK_DICT (b
)));
343 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), outfile
);
344 fprintf_filtered (outfile
, "..");
345 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), outfile
);
346 if (BLOCK_FUNCTION (b
))
348 fprintf_filtered (outfile
, ", function %s",
349 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b
)));
350 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)) != NULL
)
352 fprintf_filtered (outfile
, ", %s",
353 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)));
356 fprintf_filtered (outfile
, "\n");
357 /* Now print each symbol in this block (in no particular order, if
358 we're using a hashtable). Note that we only want this
359 block, not any blocks from included symtabs. */
360 ALL_DICT_SYMBOLS (BLOCK_DICT (b
), iter
, sym
)
362 struct print_symbol_args s
;
368 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
372 fprintf_filtered (outfile
, "\n");
376 fprintf_filtered (outfile
, "\nBlockvector same as previous symtab\n\n");
381 dump_symtab (struct objfile
*objfile
, struct symtab
*symtab
,
382 struct ui_file
*outfile
)
384 /* Set the current language to the language of the symtab we're dumping
385 because certain routines used during dump_symtab() use the current
386 language to print an image of the symbol. We'll restore it later.
387 But use only real languages, not placeholders. */
388 if (symtab
->language
!= language_unknown
389 && symtab
->language
!= language_auto
)
391 enum language saved_lang
;
393 saved_lang
= set_language (symtab
->language
);
395 dump_symtab_1 (objfile
, symtab
, outfile
);
397 set_language (saved_lang
);
400 dump_symtab_1 (objfile
, symtab
, outfile
);
404 maintenance_print_symbols (char *args
, int from_tty
)
407 struct ui_file
*outfile
;
408 struct cleanup
*cleanups
;
409 char *symname
= NULL
;
410 char *filename
= DEV_TTY
;
411 struct objfile
*objfile
;
418 error (_("Arguments missing: an output file name "
419 "and an optional symbol file name"));
421 argv
= gdb_buildargv (args
);
422 cleanups
= make_cleanup_freeargv (argv
);
427 /* If a second arg is supplied, it is a source file name to match on. */
434 filename
= tilde_expand (filename
);
435 make_cleanup (xfree
, filename
);
437 outfile
= gdb_fopen (filename
, FOPEN_WT
);
439 perror_with_name (filename
);
440 make_cleanup_ui_file_delete (outfile
);
442 ALL_SYMTABS (objfile
, s
)
445 if (symname
== NULL
|| filename_cmp (symname
, s
->filename
) == 0)
446 dump_symtab (objfile
, s
, outfile
);
448 do_cleanups (cleanups
);
451 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
452 far to indent. ARGS is really a struct print_symbol_args *, but is
453 declared as char * to get it past catch_errors. Returns 0 for error,
457 print_symbol (void *args
)
459 struct gdbarch
*gdbarch
= ((struct print_symbol_args
*) args
)->gdbarch
;
460 struct symbol
*symbol
= ((struct print_symbol_args
*) args
)->symbol
;
461 int depth
= ((struct print_symbol_args
*) args
)->depth
;
462 struct ui_file
*outfile
= ((struct print_symbol_args
*) args
)->outfile
;
463 struct obj_section
*section
= SYMBOL_OBJ_SECTION (symbol
);
465 print_spaces (depth
, outfile
);
466 if (SYMBOL_DOMAIN (symbol
) == LABEL_DOMAIN
)
468 fprintf_filtered (outfile
, "label %s at ", SYMBOL_PRINT_NAME (symbol
));
469 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
472 fprintf_filtered (outfile
, " section %s\n",
473 bfd_section_name (section
->the_bfd_section
->owner
,
474 section
->the_bfd_section
));
476 fprintf_filtered (outfile
, "\n");
479 if (SYMBOL_DOMAIN (symbol
) == STRUCT_DOMAIN
)
481 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol
)))
483 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
487 fprintf_filtered (outfile
, "%s %s = ",
488 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
490 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
491 ? "struct" : "union")),
492 SYMBOL_LINKAGE_NAME (symbol
));
493 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
495 fprintf_filtered (outfile
, ";\n");
499 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
500 fprintf_filtered (outfile
, "typedef ");
501 if (SYMBOL_TYPE (symbol
))
503 /* Print details of types, except for enums where it's clutter. */
504 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_PRINT_NAME (symbol
),
506 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
508 fprintf_filtered (outfile
, "; ");
511 fprintf_filtered (outfile
, "%s ", SYMBOL_PRINT_NAME (symbol
));
513 switch (SYMBOL_CLASS (symbol
))
516 fprintf_filtered (outfile
, "const %s (%s)",
517 plongest (SYMBOL_VALUE (symbol
)),
518 hex_string (SYMBOL_VALUE (symbol
)));
521 case LOC_CONST_BYTES
:
524 struct type
*type
= check_typedef (SYMBOL_TYPE (symbol
));
526 fprintf_filtered (outfile
, "const %u hex bytes:",
528 for (i
= 0; i
< TYPE_LENGTH (type
); i
++)
529 fprintf_filtered (outfile
, " %02x",
530 (unsigned) SYMBOL_VALUE_BYTES (symbol
)[i
]);
535 fprintf_filtered (outfile
, "static at ");
536 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
539 fprintf_filtered (outfile
, " section %s",
540 bfd_section_name (section
->the_bfd_section
->owner
,
541 section
->the_bfd_section
));
545 if (SYMBOL_IS_ARGUMENT (symbol
))
546 fprintf_filtered (outfile
, "parameter register %s",
547 plongest (SYMBOL_VALUE (symbol
)));
549 fprintf_filtered (outfile
, "register %s",
550 plongest (SYMBOL_VALUE (symbol
)));
554 fprintf_filtered (outfile
, "arg at offset %s",
555 hex_string (SYMBOL_VALUE (symbol
)));
559 fprintf_filtered (outfile
, "reference arg at %s",
560 hex_string (SYMBOL_VALUE (symbol
)));
563 case LOC_REGPARM_ADDR
:
564 fprintf_filtered (outfile
, "address parameter register %s",
565 plongest (SYMBOL_VALUE (symbol
)));
569 fprintf_filtered (outfile
, "local at offset %s",
570 hex_string (SYMBOL_VALUE (symbol
)));
577 fprintf_filtered (outfile
, "label at ");
578 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
581 fprintf_filtered (outfile
, " section %s",
582 bfd_section_name (section
->the_bfd_section
->owner
,
583 section
->the_bfd_section
));
587 fprintf_filtered (outfile
, "block object ");
588 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol
), outfile
);
589 fprintf_filtered (outfile
, ", ");
590 fputs_filtered (paddress (gdbarch
,
591 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
))),
593 fprintf_filtered (outfile
, "..");
594 fputs_filtered (paddress (gdbarch
,
595 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol
))),
598 fprintf_filtered (outfile
, " section %s",
599 bfd_section_name (section
->the_bfd_section
->owner
,
600 section
->the_bfd_section
));
604 fprintf_filtered (outfile
, "computed at runtime");
608 fprintf_filtered (outfile
, "unresolved");
611 case LOC_OPTIMIZED_OUT
:
612 fprintf_filtered (outfile
, "optimized out");
616 fprintf_filtered (outfile
, "botched symbol class %x",
617 SYMBOL_CLASS (symbol
));
621 fprintf_filtered (outfile
, "\n");
626 maintenance_print_msymbols (char *args
, int from_tty
)
629 struct ui_file
*outfile
;
630 struct cleanup
*cleanups
;
631 char *filename
= DEV_TTY
;
632 char *symname
= NULL
;
633 struct program_space
*pspace
;
634 struct objfile
*objfile
;
636 struct stat sym_st
, obj_st
;
642 error (_("print-msymbols takes an output file "
643 "name and optional symbol file name"));
645 argv
= gdb_buildargv (args
);
646 cleanups
= make_cleanup_freeargv (argv
);
651 /* If a second arg is supplied, it is a source file name to match on. */
654 symname
= xfullpath (argv
[1]);
655 make_cleanup (xfree
, symname
);
656 if (symname
&& stat (symname
, &sym_st
))
657 perror_with_name (symname
);
661 filename
= tilde_expand (filename
);
662 make_cleanup (xfree
, filename
);
664 outfile
= gdb_fopen (filename
, FOPEN_WT
);
666 perror_with_name (filename
);
667 make_cleanup_ui_file_delete (outfile
);
670 ALL_PSPACE_OBJFILES (pspace
, objfile
)
673 if (symname
== NULL
|| (!stat (objfile
->name
, &obj_st
)
674 && sym_st
.st_ino
== obj_st
.st_ino
))
675 dump_msymbols (objfile
, outfile
);
677 fprintf_filtered (outfile
, "\n\n");
678 do_cleanups (cleanups
);
682 maintenance_print_objfiles (char *ignore
, int from_tty
)
684 struct program_space
*pspace
;
685 struct objfile
*objfile
;
690 ALL_PSPACE_OBJFILES (pspace
, objfile
)
693 dump_objfile (objfile
);
698 /* List all the symbol tables whose names match REGEXP (optional). */
700 maintenance_info_symtabs (char *regexp
, int from_tty
)
702 struct program_space
*pspace
;
703 struct objfile
*objfile
;
709 ALL_PSPACE_OBJFILES (pspace
, objfile
)
711 struct symtab
*symtab
;
713 /* We don't want to print anything for this objfile until we
714 actually find a symtab whose name matches. */
715 int printed_objfile_start
= 0;
717 ALL_OBJFILE_SYMTABS (objfile
, symtab
)
722 || re_exec (symtab
->filename
))
724 if (! printed_objfile_start
)
726 printf_filtered ("{ objfile %s ", objfile
->name
);
728 printf_filtered ("((struct objfile *) %s)\n",
729 host_address_to_string (objfile
));
730 printed_objfile_start
= 1;
733 printf_filtered (" { symtab %s ", symtab
->filename
);
735 printf_filtered ("((struct symtab *) %s)\n",
736 host_address_to_string (symtab
));
737 printf_filtered (" dirname %s\n",
738 symtab
->dirname
? symtab
->dirname
: "(null)");
739 printf_filtered (" fullname %s\n",
740 symtab
->fullname
? symtab
->fullname
: "(null)");
742 "blockvector ((struct blockvector *) %s)%s\n",
743 host_address_to_string (symtab
->blockvector
),
744 symtab
->primary
? " (primary)" : "");
746 "linetable ((struct linetable *) %s)\n",
747 host_address_to_string (symtab
->linetable
));
748 printf_filtered (" debugformat %s\n",
749 symtab
->debugformat
);
750 printf_filtered (" }\n");
754 if (printed_objfile_start
)
755 printf_filtered ("}\n");
760 /* Return the nexting depth of a block within other blocks in its symtab. */
763 block_depth (struct block
*block
)
767 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
775 /* Do early runtime initializations. */
777 _initialize_symmisc (void)