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 *);
82 /* Free all the storage associated with the struct symtab <- S.
83 Note that some symtabs have contents that all live inside one big block of
84 memory, and some share the contents of another symbol table and so you
85 should not free the contents on their behalf (except sometimes the
86 linetable, which maybe per symtab even when the rest is not).
87 It is s->free_code that says which alternative to use. */
90 free_symtab (struct symtab
*s
)
95 /* All the contents are part of a big block of memory (an obstack),
96 and some other symtab is in charge of freeing that block.
97 Therefore, do nothing. */
101 /* Everything will be freed either by our `free_func'
102 or by some other symtab, except for our linetable.
105 xfree (LINETABLE (s
));
109 /* If there is a single block of memory to free, free it. */
110 if (s
->free_func
!= NULL
)
113 /* Free source-related stuff. */
114 if (s
->line_charpos
!= NULL
)
115 xfree (s
->line_charpos
);
116 if (s
->fullname
!= NULL
)
122 print_symbol_bcache_statistics (void)
124 struct program_space
*pspace
;
125 struct objfile
*objfile
;
129 ALL_PSPACE_OBJFILES (pspace
, objfile
)
131 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile
->name
);
132 print_bcache_statistics (psymbol_bcache_get_bcache (objfile
->psymbol_cache
),
133 "partial symbol cache");
134 print_bcache_statistics (objfile
->macro_cache
, "preprocessor macro cache");
135 print_bcache_statistics (objfile
->filename_cache
, "file name cache");
141 print_objfile_statistics (void)
143 struct program_space
*pspace
;
144 struct objfile
*objfile
;
146 int i
, linetables
, blockvectors
;
150 ALL_PSPACE_OBJFILES (pspace
, objfile
)
152 printf_filtered (_("Statistics for '%s':\n"), objfile
->name
);
153 if (OBJSTAT (objfile
, n_stabs
) > 0)
154 printf_filtered (_(" Number of \"stab\" symbols read: %d\n"),
155 OBJSTAT (objfile
, n_stabs
));
156 if (OBJSTAT (objfile
, n_minsyms
) > 0)
157 printf_filtered (_(" Number of \"minimal\" symbols read: %d\n"),
158 OBJSTAT (objfile
, n_minsyms
));
159 if (OBJSTAT (objfile
, n_psyms
) > 0)
160 printf_filtered (_(" Number of \"partial\" symbols read: %d\n"),
161 OBJSTAT (objfile
, n_psyms
));
162 if (OBJSTAT (objfile
, n_syms
) > 0)
163 printf_filtered (_(" Number of \"full\" symbols read: %d\n"),
164 OBJSTAT (objfile
, n_syms
));
165 if (OBJSTAT (objfile
, n_types
) > 0)
166 printf_filtered (_(" Number of \"types\" defined: %d\n"),
167 OBJSTAT (objfile
, n_types
));
169 objfile
->sf
->qf
->print_stats (objfile
);
170 i
= linetables
= blockvectors
= 0;
171 ALL_OBJFILE_SYMTABS (objfile
, s
)
174 if (s
->linetable
!= NULL
)
179 printf_filtered (_(" Number of symbol tables: %d\n"), i
);
180 printf_filtered (_(" Number of symbol tables with line tables: %d\n"),
182 printf_filtered (_(" Number of symbol tables with blockvectors: %d\n"),
185 if (OBJSTAT (objfile
, sz_strtab
) > 0)
186 printf_filtered (_(" Space used by a.out string tables: %d\n"),
187 OBJSTAT (objfile
, sz_strtab
));
188 printf_filtered (_(" Total memory used for objfile obstack: %d\n"),
189 obstack_memory_used (&objfile
->objfile_obstack
));
190 printf_filtered (_(" Total memory used for psymbol cache: %d\n"),
191 bcache_memory_used (psymbol_bcache_get_bcache
192 (objfile
->psymbol_cache
)));
193 printf_filtered (_(" Total memory used for macro cache: %d\n"),
194 bcache_memory_used (objfile
->macro_cache
));
195 printf_filtered (_(" Total memory used for file name cache: %d\n"),
196 bcache_memory_used (objfile
->filename_cache
));
202 dump_objfile (struct objfile
*objfile
)
204 struct symtab
*symtab
;
206 printf_filtered ("\nObject file %s: ", objfile
->name
);
207 printf_filtered ("Objfile at ");
208 gdb_print_host_address (objfile
, gdb_stdout
);
209 printf_filtered (", bfd at ");
210 gdb_print_host_address (objfile
->obfd
, gdb_stdout
);
211 printf_filtered (", %d minsyms\n\n",
212 objfile
->minimal_symbol_count
);
215 objfile
->sf
->qf
->dump (objfile
);
217 if (objfile
->symtabs
)
219 printf_filtered ("Symtabs:\n");
220 for (symtab
= objfile
->symtabs
;
222 symtab
= symtab
->next
)
224 printf_filtered ("%s at ", symtab
->filename
);
225 gdb_print_host_address (symtab
, gdb_stdout
);
226 printf_filtered (", ");
227 if (symtab
->objfile
!= objfile
)
229 printf_filtered ("NOT ON CHAIN! ");
233 printf_filtered ("\n\n");
237 /* Print minimal symbols from this objfile. */
240 dump_msymbols (struct objfile
*objfile
, struct ui_file
*outfile
)
242 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
243 struct minimal_symbol
*msymbol
;
247 fprintf_filtered (outfile
, "\nObject file %s:\n\n", objfile
->name
);
248 if (objfile
->minimal_symbol_count
== 0)
250 fprintf_filtered (outfile
, "No minimal symbols found.\n");
254 ALL_OBJFILE_MSYMBOLS (objfile
, msymbol
)
256 struct obj_section
*section
= SYMBOL_OBJ_SECTION (msymbol
);
258 switch (MSYMBOL_TYPE (msymbol
))
266 case mst_text_gnu_ifunc
:
269 case mst_solib_trampoline
:
294 fprintf_filtered (outfile
, "[%2d] %c ", index
, ms_type
);
295 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (msymbol
)),
297 fprintf_filtered (outfile
, " %s", SYMBOL_LINKAGE_NAME (msymbol
));
299 fprintf_filtered (outfile
, " section %s",
300 bfd_section_name (objfile
->obfd
,
301 section
->the_bfd_section
));
302 if (SYMBOL_DEMANGLED_NAME (msymbol
) != NULL
)
304 fprintf_filtered (outfile
, " %s", SYMBOL_DEMANGLED_NAME (msymbol
));
306 if (msymbol
->filename
)
307 fprintf_filtered (outfile
, " %s", msymbol
->filename
);
308 fputs_filtered ("\n", outfile
);
311 if (objfile
->minimal_symbol_count
!= index
)
313 warning (_("internal error: minimal symbol count %d != %d"),
314 objfile
->minimal_symbol_count
, index
);
316 fprintf_filtered (outfile
, "\n");
320 dump_symtab_1 (struct objfile
*objfile
, struct symtab
*symtab
,
321 struct ui_file
*outfile
)
323 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
325 struct dict_iterator iter
;
328 struct blockvector
*bv
;
333 fprintf_filtered (outfile
, "\nSymtab for file %s\n", symtab
->filename
);
335 fprintf_filtered (outfile
, "Compilation directory is %s\n",
337 fprintf_filtered (outfile
, "Read from object file %s (", objfile
->name
);
338 gdb_print_host_address (objfile
, outfile
);
339 fprintf_filtered (outfile
, ")\n");
340 fprintf_filtered (outfile
, "Language: %s\n",
341 language_str (symtab
->language
));
343 /* First print the line table. */
344 l
= LINETABLE (symtab
);
347 fprintf_filtered (outfile
, "\nLine table:\n\n");
349 for (i
= 0; i
< len
; i
++)
351 fprintf_filtered (outfile
, " line %d at ", l
->item
[i
].line
);
352 fputs_filtered (paddress (gdbarch
, l
->item
[i
].pc
), outfile
);
353 fprintf_filtered (outfile
, "\n");
356 /* Now print the block info, but only for primary symtabs since we will
357 print lots of duplicate info otherwise. */
360 fprintf_filtered (outfile
, "\nBlockvector:\n\n");
361 bv
= BLOCKVECTOR (symtab
);
362 len
= BLOCKVECTOR_NBLOCKS (bv
);
363 for (i
= 0; i
< len
; i
++)
365 b
= BLOCKVECTOR_BLOCK (bv
, i
);
366 depth
= block_depth (b
) * 2;
367 print_spaces (depth
, outfile
);
368 fprintf_filtered (outfile
, "block #%03d, object at ", i
);
369 gdb_print_host_address (b
, outfile
);
370 if (BLOCK_SUPERBLOCK (b
))
372 fprintf_filtered (outfile
, " under ");
373 gdb_print_host_address (BLOCK_SUPERBLOCK (b
), outfile
);
375 /* drow/2002-07-10: We could save the total symbols count
376 even if we're using a hashtable, but nothing else but this message
378 fprintf_filtered (outfile
, ", %d syms/buckets in ",
379 dict_size (BLOCK_DICT (b
)));
380 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), outfile
);
381 fprintf_filtered (outfile
, "..");
382 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), outfile
);
383 if (BLOCK_FUNCTION (b
))
385 fprintf_filtered (outfile
, ", function %s",
386 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b
)));
387 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)) != NULL
)
389 fprintf_filtered (outfile
, ", %s",
390 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)));
393 fprintf_filtered (outfile
, "\n");
394 /* Now print each symbol in this block (in no particular order, if
395 we're using a hashtable). */
396 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
398 struct print_symbol_args s
;
404 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
408 fprintf_filtered (outfile
, "\n");
412 fprintf_filtered (outfile
, "\nBlockvector same as previous symtab\n\n");
417 dump_symtab (struct objfile
*objfile
, struct symtab
*symtab
,
418 struct ui_file
*outfile
)
420 /* Set the current language to the language of the symtab we're dumping
421 because certain routines used during dump_symtab() use the current
422 language to print an image of the symbol. We'll restore it later.
423 But use only real languages, not placeholders. */
424 if (symtab
->language
!= language_unknown
425 && symtab
->language
!= language_auto
)
427 enum language saved_lang
;
429 saved_lang
= set_language (symtab
->language
);
431 dump_symtab_1 (objfile
, symtab
, outfile
);
433 set_language (saved_lang
);
436 dump_symtab_1 (objfile
, symtab
, outfile
);
440 maintenance_print_symbols (char *args
, int from_tty
)
443 struct ui_file
*outfile
;
444 struct cleanup
*cleanups
;
445 char *symname
= NULL
;
446 char *filename
= DEV_TTY
;
447 struct objfile
*objfile
;
454 error (_("Arguments missing: an output file name "
455 "and an optional symbol file name"));
457 argv
= gdb_buildargv (args
);
458 cleanups
= make_cleanup_freeargv (argv
);
463 /* If a second arg is supplied, it is a source file name to match on. */
470 filename
= tilde_expand (filename
);
471 make_cleanup (xfree
, filename
);
473 outfile
= gdb_fopen (filename
, FOPEN_WT
);
475 perror_with_name (filename
);
476 make_cleanup_ui_file_delete (outfile
);
479 ALL_SYMTABS (objfile
, s
)
480 if (symname
== NULL
|| filename_cmp (symname
, s
->filename
) == 0)
481 dump_symtab (objfile
, s
, outfile
);
483 do_cleanups (cleanups
);
486 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
487 far to indent. ARGS is really a struct print_symbol_args *, but is
488 declared as char * to get it past catch_errors. Returns 0 for error,
492 print_symbol (void *args
)
494 struct gdbarch
*gdbarch
= ((struct print_symbol_args
*) args
)->gdbarch
;
495 struct symbol
*symbol
= ((struct print_symbol_args
*) args
)->symbol
;
496 int depth
= ((struct print_symbol_args
*) args
)->depth
;
497 struct ui_file
*outfile
= ((struct print_symbol_args
*) args
)->outfile
;
498 struct obj_section
*section
= SYMBOL_OBJ_SECTION (symbol
);
500 print_spaces (depth
, outfile
);
501 if (SYMBOL_DOMAIN (symbol
) == LABEL_DOMAIN
)
503 fprintf_filtered (outfile
, "label %s at ", SYMBOL_PRINT_NAME (symbol
));
504 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
507 fprintf_filtered (outfile
, " section %s\n",
508 bfd_section_name (section
->the_bfd_section
->owner
,
509 section
->the_bfd_section
));
511 fprintf_filtered (outfile
, "\n");
514 if (SYMBOL_DOMAIN (symbol
) == STRUCT_DOMAIN
)
516 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol
)))
518 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
522 fprintf_filtered (outfile
, "%s %s = ",
523 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
525 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
526 ? "struct" : "union")),
527 SYMBOL_LINKAGE_NAME (symbol
));
528 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
530 fprintf_filtered (outfile
, ";\n");
534 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
535 fprintf_filtered (outfile
, "typedef ");
536 if (SYMBOL_TYPE (symbol
))
538 /* Print details of types, except for enums where it's clutter. */
539 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_PRINT_NAME (symbol
),
541 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
543 fprintf_filtered (outfile
, "; ");
546 fprintf_filtered (outfile
, "%s ", SYMBOL_PRINT_NAME (symbol
));
548 switch (SYMBOL_CLASS (symbol
))
551 fprintf_filtered (outfile
, "const %ld (0x%lx)",
552 SYMBOL_VALUE (symbol
),
553 SYMBOL_VALUE (symbol
));
556 case LOC_CONST_BYTES
:
559 struct type
*type
= check_typedef (SYMBOL_TYPE (symbol
));
561 fprintf_filtered (outfile
, "const %u hex bytes:",
563 for (i
= 0; i
< TYPE_LENGTH (type
); i
++)
564 fprintf_filtered (outfile
, " %02x",
565 (unsigned) SYMBOL_VALUE_BYTES (symbol
)[i
]);
570 fprintf_filtered (outfile
, "static at ");
571 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
574 fprintf_filtered (outfile
, " section %s",
575 bfd_section_name (section
->the_bfd_section
->owner
,
576 section
->the_bfd_section
));
580 if (SYMBOL_IS_ARGUMENT (symbol
))
581 fprintf_filtered (outfile
, "parameter register %ld",
582 SYMBOL_VALUE (symbol
));
584 fprintf_filtered (outfile
, "register %ld", SYMBOL_VALUE (symbol
));
588 fprintf_filtered (outfile
, "arg at offset 0x%lx",
589 SYMBOL_VALUE (symbol
));
593 fprintf_filtered (outfile
, "reference arg at 0x%lx", SYMBOL_VALUE (symbol
));
596 case LOC_REGPARM_ADDR
:
597 fprintf_filtered (outfile
, "address parameter register %ld", SYMBOL_VALUE (symbol
));
601 fprintf_filtered (outfile
, "local at offset 0x%lx",
602 SYMBOL_VALUE (symbol
));
609 fprintf_filtered (outfile
, "label at ");
610 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
613 fprintf_filtered (outfile
, " section %s",
614 bfd_section_name (section
->the_bfd_section
->owner
,
615 section
->the_bfd_section
));
619 fprintf_filtered (outfile
, "block object ");
620 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol
), outfile
);
621 fprintf_filtered (outfile
, ", ");
622 fputs_filtered (paddress (gdbarch
,
623 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
))),
625 fprintf_filtered (outfile
, "..");
626 fputs_filtered (paddress (gdbarch
,
627 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol
))),
630 fprintf_filtered (outfile
, " section %s",
631 bfd_section_name (section
->the_bfd_section
->owner
,
632 section
->the_bfd_section
));
636 fprintf_filtered (outfile
, "computed at runtime");
640 fprintf_filtered (outfile
, "unresolved");
643 case LOC_OPTIMIZED_OUT
:
644 fprintf_filtered (outfile
, "optimized out");
648 fprintf_filtered (outfile
, "botched symbol class %x",
649 SYMBOL_CLASS (symbol
));
653 fprintf_filtered (outfile
, "\n");
658 maintenance_print_msymbols (char *args
, int from_tty
)
661 struct ui_file
*outfile
;
662 struct cleanup
*cleanups
;
663 char *filename
= DEV_TTY
;
664 char *symname
= NULL
;
665 struct program_space
*pspace
;
666 struct objfile
*objfile
;
668 struct stat sym_st
, obj_st
;
674 error (_("print-msymbols takes an output file "
675 "name and optional symbol file name"));
677 argv
= gdb_buildargv (args
);
678 cleanups
= make_cleanup_freeargv (argv
);
683 /* If a second arg is supplied, it is a source file name to match on. */
686 symname
= xfullpath (argv
[1]);
687 make_cleanup (xfree
, symname
);
688 if (symname
&& stat (symname
, &sym_st
))
689 perror_with_name (symname
);
693 filename
= tilde_expand (filename
);
694 make_cleanup (xfree
, filename
);
696 outfile
= gdb_fopen (filename
, FOPEN_WT
);
698 perror_with_name (filename
);
699 make_cleanup_ui_file_delete (outfile
);
703 ALL_PSPACE_OBJFILES (pspace
, objfile
)
704 if (symname
== NULL
|| (!stat (objfile
->name
, &obj_st
)
705 && sym_st
.st_ino
== obj_st
.st_ino
))
706 dump_msymbols (objfile
, outfile
);
708 fprintf_filtered (outfile
, "\n\n");
709 do_cleanups (cleanups
);
713 maintenance_print_objfiles (char *ignore
, int from_tty
)
715 struct program_space
*pspace
;
716 struct objfile
*objfile
;
722 ALL_PSPACE_OBJFILES (pspace
, objfile
)
723 dump_objfile (objfile
);
728 /* List all the symbol tables whose names match REGEXP (optional). */
730 maintenance_info_symtabs (char *regexp
, int from_tty
)
732 struct program_space
*pspace
;
733 struct objfile
*objfile
;
739 ALL_PSPACE_OBJFILES (pspace
, objfile
)
741 struct symtab
*symtab
;
743 /* We don't want to print anything for this objfile until we
744 actually find a symtab whose name matches. */
745 int printed_objfile_start
= 0;
747 ALL_OBJFILE_SYMTABS (objfile
, symtab
)
752 || re_exec (symtab
->filename
))
754 if (! printed_objfile_start
)
756 printf_filtered ("{ objfile %s ", objfile
->name
);
758 printf_filtered ("((struct objfile *) %s)\n",
759 host_address_to_string (objfile
));
760 printed_objfile_start
= 1;
763 printf_filtered (" { symtab %s ", symtab
->filename
);
765 printf_filtered ("((struct symtab *) %s)\n",
766 host_address_to_string (symtab
));
767 printf_filtered (" dirname %s\n",
768 symtab
->dirname
? symtab
->dirname
: "(null)");
769 printf_filtered (" fullname %s\n",
770 symtab
->fullname
? symtab
->fullname
: "(null)");
772 "blockvector ((struct blockvector *) %s)%s\n",
773 host_address_to_string (symtab
->blockvector
),
774 symtab
->primary
? " (primary)" : "");
776 "linetable ((struct linetable *) %s)\n",
777 host_address_to_string (symtab
->linetable
));
778 printf_filtered (" debugformat %s\n",
779 symtab
->debugformat
);
780 printf_filtered (" }\n");
784 if (printed_objfile_start
)
785 printf_filtered ("}\n");
790 /* Return the nexting depth of a block within other blocks in its symtab. */
793 block_depth (struct block
*block
)
797 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
805 /* Do early runtime initializations. */
807 _initialize_symmisc (void)