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 2011 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/>. */
26 #include "filenames.h"
29 #include "breakpoint.h"
31 #include "gdb_obstack.h"
32 #include "exceptions.h"
36 #include "gdb_regex.h"
38 #include "dictionary.h"
40 #include "gdb_string.h"
41 #include "readline/readline.h"
46 #define DEV_TTY "/dev/tty"
49 /* Unfortunately for debugging, stderr is usually a macro. This is painful
50 when calling functions that take FILE *'s from the debugger.
51 So we make a variable which has the same value and which is accessible when
52 debugging GDB with itself. Because stdin et al need not be constants,
53 we initialize them in the _initialize_symmisc function at the bottom
59 /* Prototypes for local functions */
61 static void dump_symtab (struct objfile
*, struct symtab
*,
64 static void dump_msymbols (struct objfile
*, struct ui_file
*);
66 static void dump_objfile (struct objfile
*);
68 static int block_depth (struct block
*);
70 void _initialize_symmisc (void);
72 struct print_symbol_args
74 struct gdbarch
*gdbarch
;
75 struct symbol
*symbol
;
77 struct ui_file
*outfile
;
80 static int print_symbol (void *);
84 print_symbol_bcache_statistics (void)
86 struct program_space
*pspace
;
87 struct objfile
*objfile
;
91 ALL_PSPACE_OBJFILES (pspace
, objfile
)
93 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile
->name
);
94 print_bcache_statistics (psymbol_bcache_get_bcache (objfile
->psymbol_cache
),
95 "partial symbol cache");
96 print_bcache_statistics (objfile
->macro_cache
, "preprocessor macro cache");
97 print_bcache_statistics (objfile
->filename_cache
, "file name cache");
103 print_objfile_statistics (void)
105 struct program_space
*pspace
;
106 struct objfile
*objfile
;
108 int i
, linetables
, blockvectors
;
112 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 psymbol cache: %d\n"),
153 bcache_memory_used (psymbol_bcache_get_bcache
154 (objfile
->psymbol_cache
)));
155 printf_filtered (_(" Total memory used for macro cache: %d\n"),
156 bcache_memory_used (objfile
->macro_cache
));
157 printf_filtered (_(" Total memory used for file name cache: %d\n"),
158 bcache_memory_used (objfile
->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). */
358 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
360 struct print_symbol_args s
;
366 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
370 fprintf_filtered (outfile
, "\n");
374 fprintf_filtered (outfile
, "\nBlockvector same as previous symtab\n\n");
379 dump_symtab (struct objfile
*objfile
, struct symtab
*symtab
,
380 struct ui_file
*outfile
)
382 /* Set the current language to the language of the symtab we're dumping
383 because certain routines used during dump_symtab() use the current
384 language to print an image of the symbol. We'll restore it later.
385 But use only real languages, not placeholders. */
386 if (symtab
->language
!= language_unknown
387 && symtab
->language
!= language_auto
)
389 enum language saved_lang
;
391 saved_lang
= set_language (symtab
->language
);
393 dump_symtab_1 (objfile
, symtab
, outfile
);
395 set_language (saved_lang
);
398 dump_symtab_1 (objfile
, symtab
, outfile
);
402 maintenance_print_symbols (char *args
, int from_tty
)
405 struct ui_file
*outfile
;
406 struct cleanup
*cleanups
;
407 char *symname
= NULL
;
408 char *filename
= DEV_TTY
;
409 struct objfile
*objfile
;
416 error (_("Arguments missing: an output file name "
417 "and an optional symbol file name"));
419 argv
= gdb_buildargv (args
);
420 cleanups
= make_cleanup_freeargv (argv
);
425 /* If a second arg is supplied, it is a source file name to match on. */
432 filename
= tilde_expand (filename
);
433 make_cleanup (xfree
, filename
);
435 outfile
= gdb_fopen (filename
, FOPEN_WT
);
437 perror_with_name (filename
);
438 make_cleanup_ui_file_delete (outfile
);
441 ALL_SYMTABS (objfile
, s
)
442 if (symname
== NULL
|| filename_cmp (symname
, s
->filename
) == 0)
443 dump_symtab (objfile
, s
, outfile
);
445 do_cleanups (cleanups
);
448 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
449 far to indent. ARGS is really a struct print_symbol_args *, but is
450 declared as char * to get it past catch_errors. Returns 0 for error,
454 print_symbol (void *args
)
456 struct gdbarch
*gdbarch
= ((struct print_symbol_args
*) args
)->gdbarch
;
457 struct symbol
*symbol
= ((struct print_symbol_args
*) args
)->symbol
;
458 int depth
= ((struct print_symbol_args
*) args
)->depth
;
459 struct ui_file
*outfile
= ((struct print_symbol_args
*) args
)->outfile
;
460 struct obj_section
*section
= SYMBOL_OBJ_SECTION (symbol
);
462 print_spaces (depth
, outfile
);
463 if (SYMBOL_DOMAIN (symbol
) == LABEL_DOMAIN
)
465 fprintf_filtered (outfile
, "label %s at ", SYMBOL_PRINT_NAME (symbol
));
466 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
469 fprintf_filtered (outfile
, " section %s\n",
470 bfd_section_name (section
->the_bfd_section
->owner
,
471 section
->the_bfd_section
));
473 fprintf_filtered (outfile
, "\n");
476 if (SYMBOL_DOMAIN (symbol
) == STRUCT_DOMAIN
)
478 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol
)))
480 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
484 fprintf_filtered (outfile
, "%s %s = ",
485 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
487 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
488 ? "struct" : "union")),
489 SYMBOL_LINKAGE_NAME (symbol
));
490 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
492 fprintf_filtered (outfile
, ";\n");
496 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
497 fprintf_filtered (outfile
, "typedef ");
498 if (SYMBOL_TYPE (symbol
))
500 /* Print details of types, except for enums where it's clutter. */
501 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_PRINT_NAME (symbol
),
503 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
505 fprintf_filtered (outfile
, "; ");
508 fprintf_filtered (outfile
, "%s ", SYMBOL_PRINT_NAME (symbol
));
510 switch (SYMBOL_CLASS (symbol
))
513 fprintf_filtered (outfile
, "const %ld (0x%lx)",
514 SYMBOL_VALUE (symbol
),
515 SYMBOL_VALUE (symbol
));
518 case LOC_CONST_BYTES
:
521 struct type
*type
= check_typedef (SYMBOL_TYPE (symbol
));
523 fprintf_filtered (outfile
, "const %u hex bytes:",
525 for (i
= 0; i
< TYPE_LENGTH (type
); i
++)
526 fprintf_filtered (outfile
, " %02x",
527 (unsigned) SYMBOL_VALUE_BYTES (symbol
)[i
]);
532 fprintf_filtered (outfile
, "static at ");
533 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
536 fprintf_filtered (outfile
, " section %s",
537 bfd_section_name (section
->the_bfd_section
->owner
,
538 section
->the_bfd_section
));
542 if (SYMBOL_IS_ARGUMENT (symbol
))
543 fprintf_filtered (outfile
, "parameter register %ld",
544 SYMBOL_VALUE (symbol
));
546 fprintf_filtered (outfile
, "register %ld", SYMBOL_VALUE (symbol
));
550 fprintf_filtered (outfile
, "arg at offset 0x%lx",
551 SYMBOL_VALUE (symbol
));
555 fprintf_filtered (outfile
, "reference arg at 0x%lx", SYMBOL_VALUE (symbol
));
558 case LOC_REGPARM_ADDR
:
559 fprintf_filtered (outfile
, "address parameter register %ld", SYMBOL_VALUE (symbol
));
563 fprintf_filtered (outfile
, "local at offset 0x%lx",
564 SYMBOL_VALUE (symbol
));
571 fprintf_filtered (outfile
, "label at ");
572 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
575 fprintf_filtered (outfile
, " section %s",
576 bfd_section_name (section
->the_bfd_section
->owner
,
577 section
->the_bfd_section
));
581 fprintf_filtered (outfile
, "block object ");
582 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol
), outfile
);
583 fprintf_filtered (outfile
, ", ");
584 fputs_filtered (paddress (gdbarch
,
585 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
))),
587 fprintf_filtered (outfile
, "..");
588 fputs_filtered (paddress (gdbarch
,
589 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol
))),
592 fprintf_filtered (outfile
, " section %s",
593 bfd_section_name (section
->the_bfd_section
->owner
,
594 section
->the_bfd_section
));
598 fprintf_filtered (outfile
, "computed at runtime");
602 fprintf_filtered (outfile
, "unresolved");
605 case LOC_OPTIMIZED_OUT
:
606 fprintf_filtered (outfile
, "optimized out");
610 fprintf_filtered (outfile
, "botched symbol class %x",
611 SYMBOL_CLASS (symbol
));
615 fprintf_filtered (outfile
, "\n");
620 maintenance_print_msymbols (char *args
, int from_tty
)
623 struct ui_file
*outfile
;
624 struct cleanup
*cleanups
;
625 char *filename
= DEV_TTY
;
626 char *symname
= NULL
;
627 struct program_space
*pspace
;
628 struct objfile
*objfile
;
630 struct stat sym_st
, obj_st
;
636 error (_("print-msymbols takes an output file "
637 "name and optional symbol file name"));
639 argv
= gdb_buildargv (args
);
640 cleanups
= make_cleanup_freeargv (argv
);
645 /* If a second arg is supplied, it is a source file name to match on. */
648 symname
= xfullpath (argv
[1]);
649 make_cleanup (xfree
, symname
);
650 if (symname
&& stat (symname
, &sym_st
))
651 perror_with_name (symname
);
655 filename
= tilde_expand (filename
);
656 make_cleanup (xfree
, filename
);
658 outfile
= gdb_fopen (filename
, FOPEN_WT
);
660 perror_with_name (filename
);
661 make_cleanup_ui_file_delete (outfile
);
665 ALL_PSPACE_OBJFILES (pspace
, objfile
)
666 if (symname
== NULL
|| (!stat (objfile
->name
, &obj_st
)
667 && sym_st
.st_ino
== obj_st
.st_ino
))
668 dump_msymbols (objfile
, outfile
);
670 fprintf_filtered (outfile
, "\n\n");
671 do_cleanups (cleanups
);
675 maintenance_print_objfiles (char *ignore
, int from_tty
)
677 struct program_space
*pspace
;
678 struct objfile
*objfile
;
684 ALL_PSPACE_OBJFILES (pspace
, objfile
)
685 dump_objfile (objfile
);
690 /* List all the symbol tables whose names match REGEXP (optional). */
692 maintenance_info_symtabs (char *regexp
, int from_tty
)
694 struct program_space
*pspace
;
695 struct objfile
*objfile
;
701 ALL_PSPACE_OBJFILES (pspace
, objfile
)
703 struct symtab
*symtab
;
705 /* We don't want to print anything for this objfile until we
706 actually find a symtab whose name matches. */
707 int printed_objfile_start
= 0;
709 ALL_OBJFILE_SYMTABS (objfile
, symtab
)
714 || re_exec (symtab
->filename
))
716 if (! printed_objfile_start
)
718 printf_filtered ("{ objfile %s ", objfile
->name
);
720 printf_filtered ("((struct objfile *) %s)\n",
721 host_address_to_string (objfile
));
722 printed_objfile_start
= 1;
725 printf_filtered (" { symtab %s ", symtab
->filename
);
727 printf_filtered ("((struct symtab *) %s)\n",
728 host_address_to_string (symtab
));
729 printf_filtered (" dirname %s\n",
730 symtab
->dirname
? symtab
->dirname
: "(null)");
731 printf_filtered (" fullname %s\n",
732 symtab
->fullname
? symtab
->fullname
: "(null)");
734 "blockvector ((struct blockvector *) %s)%s\n",
735 host_address_to_string (symtab
->blockvector
),
736 symtab
->primary
? " (primary)" : "");
738 "linetable ((struct linetable *) %s)\n",
739 host_address_to_string (symtab
->linetable
));
740 printf_filtered (" debugformat %s\n",
741 symtab
->debugformat
);
742 printf_filtered (" }\n");
746 if (printed_objfile_start
)
747 printf_filtered ("}\n");
752 /* Return the nexting depth of a block within other blocks in its symtab. */
755 block_depth (struct block
*block
)
759 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
767 /* Do early runtime initializations. */
769 _initialize_symmisc (void)