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 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/>. */
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 *);
81 /* Free all the storage associated with the struct symtab <- S.
82 Note that some symtabs have contents that all live inside one big block of
83 memory, and some share the contents of another symbol table and so you
84 should not free the contents on their behalf (except sometimes the
85 linetable, which maybe per symtab even when the rest is not).
86 It is s->free_code that says which alternative to use. */
89 free_symtab (struct symtab
*s
)
94 /* All the contents are part of a big block of memory (an obstack),
95 and some other symtab is in charge of freeing that block.
96 Therefore, do nothing. */
100 /* Everything will be freed either by our `free_func'
101 or by some other symtab, except for our linetable.
104 xfree (LINETABLE (s
));
108 /* If there is a single block of memory to free, free it. */
109 if (s
->free_func
!= NULL
)
112 /* Free source-related stuff */
113 if (s
->line_charpos
!= NULL
)
114 xfree (s
->line_charpos
);
115 if (s
->fullname
!= NULL
)
117 if (s
->debugformat
!= NULL
)
118 xfree (s
->debugformat
);
123 print_symbol_bcache_statistics (void)
125 struct program_space
*pspace
;
126 struct objfile
*objfile
;
130 ALL_PSPACE_OBJFILES (pspace
, objfile
)
132 printf_filtered (_("Byte cache statistics for '%s':\n"), objfile
->name
);
133 print_bcache_statistics (objfile
->psymbol_cache
, "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 (objfile
->psymbol_cache
));
192 printf_filtered (_(" Total memory used for macro cache: %d\n"),
193 bcache_memory_used (objfile
->macro_cache
));
194 printf_filtered (_(" Total memory used for file name cache: %d\n"),
195 bcache_memory_used (objfile
->filename_cache
));
201 dump_objfile (struct objfile
*objfile
)
203 struct symtab
*symtab
;
205 printf_filtered ("\nObject file %s: ", objfile
->name
);
206 printf_filtered ("Objfile at ");
207 gdb_print_host_address (objfile
, gdb_stdout
);
208 printf_filtered (", bfd at ");
209 gdb_print_host_address (objfile
->obfd
, gdb_stdout
);
210 printf_filtered (", %d minsyms\n\n",
211 objfile
->minimal_symbol_count
);
214 objfile
->sf
->qf
->dump (objfile
);
216 if (objfile
->symtabs
)
218 printf_filtered ("Symtabs:\n");
219 for (symtab
= objfile
->symtabs
;
221 symtab
= symtab
->next
)
223 printf_filtered ("%s at ", symtab
->filename
);
224 gdb_print_host_address (symtab
, gdb_stdout
);
225 printf_filtered (", ");
226 if (symtab
->objfile
!= objfile
)
228 printf_filtered ("NOT ON CHAIN! ");
232 printf_filtered ("\n\n");
236 /* Print minimal symbols from this objfile. */
239 dump_msymbols (struct objfile
*objfile
, struct ui_file
*outfile
)
241 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
242 struct minimal_symbol
*msymbol
;
246 fprintf_filtered (outfile
, "\nObject file %s:\n\n", objfile
->name
);
247 if (objfile
->minimal_symbol_count
== 0)
249 fprintf_filtered (outfile
, "No minimal symbols found.\n");
253 ALL_OBJFILE_MSYMBOLS (objfile
, msymbol
)
255 struct obj_section
*section
= SYMBOL_OBJ_SECTION (msymbol
);
257 switch (MSYMBOL_TYPE (msymbol
))
265 case mst_solib_trampoline
:
290 fprintf_filtered (outfile
, "[%2d] %c ", index
, ms_type
);
291 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (msymbol
)),
293 fprintf_filtered (outfile
, " %s", SYMBOL_LINKAGE_NAME (msymbol
));
295 fprintf_filtered (outfile
, " section %s",
296 bfd_section_name (objfile
->obfd
,
297 section
->the_bfd_section
));
298 if (SYMBOL_DEMANGLED_NAME (msymbol
) != NULL
)
300 fprintf_filtered (outfile
, " %s", SYMBOL_DEMANGLED_NAME (msymbol
));
302 if (msymbol
->filename
)
303 fprintf_filtered (outfile
, " %s", msymbol
->filename
);
304 fputs_filtered ("\n", outfile
);
307 if (objfile
->minimal_symbol_count
!= index
)
309 warning (_("internal error: minimal symbol count %d != %d"),
310 objfile
->minimal_symbol_count
, index
);
312 fprintf_filtered (outfile
, "\n");
316 dump_symtab_1 (struct objfile
*objfile
, struct symtab
*symtab
,
317 struct ui_file
*outfile
)
319 struct gdbarch
*gdbarch
= get_objfile_arch (objfile
);
321 struct dict_iterator iter
;
324 struct blockvector
*bv
;
329 fprintf_filtered (outfile
, "\nSymtab for file %s\n", symtab
->filename
);
331 fprintf_filtered (outfile
, "Compilation directory is %s\n",
333 fprintf_filtered (outfile
, "Read from object file %s (", objfile
->name
);
334 gdb_print_host_address (objfile
, outfile
);
335 fprintf_filtered (outfile
, ")\n");
336 fprintf_filtered (outfile
, "Language: %s\n", language_str (symtab
->language
));
338 /* First print the line table. */
339 l
= LINETABLE (symtab
);
342 fprintf_filtered (outfile
, "\nLine table:\n\n");
344 for (i
= 0; i
< len
; i
++)
346 fprintf_filtered (outfile
, " line %d at ", l
->item
[i
].line
);
347 fputs_filtered (paddress (gdbarch
, l
->item
[i
].pc
), outfile
);
348 fprintf_filtered (outfile
, "\n");
351 /* Now print the block info, but only for primary symtabs since we will
352 print lots of duplicate info otherwise. */
355 fprintf_filtered (outfile
, "\nBlockvector:\n\n");
356 bv
= BLOCKVECTOR (symtab
);
357 len
= BLOCKVECTOR_NBLOCKS (bv
);
358 for (i
= 0; i
< len
; i
++)
360 b
= BLOCKVECTOR_BLOCK (bv
, i
);
361 depth
= block_depth (b
) * 2;
362 print_spaces (depth
, outfile
);
363 fprintf_filtered (outfile
, "block #%03d, object at ", i
);
364 gdb_print_host_address (b
, outfile
);
365 if (BLOCK_SUPERBLOCK (b
))
367 fprintf_filtered (outfile
, " under ");
368 gdb_print_host_address (BLOCK_SUPERBLOCK (b
), outfile
);
370 /* drow/2002-07-10: We could save the total symbols count
371 even if we're using a hashtable, but nothing else but this message
373 fprintf_filtered (outfile
, ", %d syms/buckets in ",
374 dict_size (BLOCK_DICT (b
)));
375 fputs_filtered (paddress (gdbarch
, BLOCK_START (b
)), outfile
);
376 fprintf_filtered (outfile
, "..");
377 fputs_filtered (paddress (gdbarch
, BLOCK_END (b
)), outfile
);
378 if (BLOCK_FUNCTION (b
))
380 fprintf_filtered (outfile
, ", function %s",
381 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b
)));
382 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)) != NULL
)
384 fprintf_filtered (outfile
, ", %s",
385 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)));
388 fprintf_filtered (outfile
, "\n");
389 /* Now print each symbol in this block (in no particular order, if
390 we're using a hashtable). */
391 ALL_BLOCK_SYMBOLS (b
, iter
, sym
)
393 struct print_symbol_args s
;
399 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
403 fprintf_filtered (outfile
, "\n");
407 fprintf_filtered (outfile
, "\nBlockvector same as previous symtab\n\n");
412 dump_symtab (struct objfile
*objfile
, struct symtab
*symtab
,
413 struct ui_file
*outfile
)
415 /* Set the current language to the language of the symtab we're dumping
416 because certain routines used during dump_symtab() use the current
417 language to print an image of the symbol. We'll restore it later.
418 But use only real languages, not placeholders. */
419 if (symtab
->language
!= language_unknown
420 && symtab
->language
!= language_auto
)
422 enum language saved_lang
;
424 saved_lang
= set_language (symtab
->language
);
426 dump_symtab_1 (objfile
, symtab
, outfile
);
428 set_language (saved_lang
);
431 dump_symtab_1 (objfile
, symtab
, outfile
);
435 maintenance_print_symbols (char *args
, int from_tty
)
438 struct ui_file
*outfile
;
439 struct cleanup
*cleanups
;
440 char *symname
= NULL
;
441 char *filename
= DEV_TTY
;
442 struct objfile
*objfile
;
450 Arguments missing: an output file name and an optional symbol file name"));
452 argv
= gdb_buildargv (args
);
453 cleanups
= make_cleanup_freeargv (argv
);
458 /* If a second arg is supplied, it is a source file name to match on */
465 filename
= tilde_expand (filename
);
466 make_cleanup (xfree
, filename
);
468 outfile
= gdb_fopen (filename
, FOPEN_WT
);
470 perror_with_name (filename
);
471 make_cleanup_ui_file_delete (outfile
);
474 ALL_SYMTABS (objfile
, s
)
475 if (symname
== NULL
|| strcmp (symname
, s
->filename
) == 0)
476 dump_symtab (objfile
, s
, outfile
);
478 do_cleanups (cleanups
);
481 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
482 far to indent. ARGS is really a struct print_symbol_args *, but is
483 declared as char * to get it past catch_errors. Returns 0 for error,
487 print_symbol (void *args
)
489 struct gdbarch
*gdbarch
= ((struct print_symbol_args
*) args
)->gdbarch
;
490 struct symbol
*symbol
= ((struct print_symbol_args
*) args
)->symbol
;
491 int depth
= ((struct print_symbol_args
*) args
)->depth
;
492 struct ui_file
*outfile
= ((struct print_symbol_args
*) args
)->outfile
;
493 struct obj_section
*section
= SYMBOL_OBJ_SECTION (symbol
);
495 print_spaces (depth
, outfile
);
496 if (SYMBOL_DOMAIN (symbol
) == LABEL_DOMAIN
)
498 fprintf_filtered (outfile
, "label %s at ", SYMBOL_PRINT_NAME (symbol
));
499 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
502 fprintf_filtered (outfile
, " section %s\n",
503 bfd_section_name (section
->the_bfd_section
->owner
,
504 section
->the_bfd_section
));
506 fprintf_filtered (outfile
, "\n");
509 if (SYMBOL_DOMAIN (symbol
) == STRUCT_DOMAIN
)
511 if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol
)))
513 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
517 fprintf_filtered (outfile
, "%s %s = ",
518 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
520 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
521 ? "struct" : "union")),
522 SYMBOL_LINKAGE_NAME (symbol
));
523 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
525 fprintf_filtered (outfile
, ";\n");
529 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
530 fprintf_filtered (outfile
, "typedef ");
531 if (SYMBOL_TYPE (symbol
))
533 /* Print details of types, except for enums where it's clutter. */
534 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_PRINT_NAME (symbol
),
536 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
538 fprintf_filtered (outfile
, "; ");
541 fprintf_filtered (outfile
, "%s ", SYMBOL_PRINT_NAME (symbol
));
543 switch (SYMBOL_CLASS (symbol
))
546 fprintf_filtered (outfile
, "const %ld (0x%lx)",
547 SYMBOL_VALUE (symbol
),
548 SYMBOL_VALUE (symbol
));
551 case LOC_CONST_BYTES
:
554 struct type
*type
= check_typedef (SYMBOL_TYPE (symbol
));
556 fprintf_filtered (outfile
, "const %u hex bytes:",
558 for (i
= 0; i
< TYPE_LENGTH (type
); i
++)
559 fprintf_filtered (outfile
, " %02x",
560 (unsigned) SYMBOL_VALUE_BYTES (symbol
)[i
]);
565 fprintf_filtered (outfile
, "static at ");
566 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
569 fprintf_filtered (outfile
, " section %s",
570 bfd_section_name (section
->the_bfd_section
->owner
,
571 section
->the_bfd_section
));
575 if (SYMBOL_IS_ARGUMENT (symbol
))
576 fprintf_filtered (outfile
, "parameter register %ld",
577 SYMBOL_VALUE (symbol
));
579 fprintf_filtered (outfile
, "register %ld", SYMBOL_VALUE (symbol
));
583 fprintf_filtered (outfile
, "arg at offset 0x%lx",
584 SYMBOL_VALUE (symbol
));
588 fprintf_filtered (outfile
, "reference arg at 0x%lx", SYMBOL_VALUE (symbol
));
591 case LOC_REGPARM_ADDR
:
592 fprintf_filtered (outfile
, "address parameter register %ld", SYMBOL_VALUE (symbol
));
596 fprintf_filtered (outfile
, "local at offset 0x%lx",
597 SYMBOL_VALUE (symbol
));
604 fprintf_filtered (outfile
, "label at ");
605 fputs_filtered (paddress (gdbarch
, SYMBOL_VALUE_ADDRESS (symbol
)),
608 fprintf_filtered (outfile
, " section %s",
609 bfd_section_name (section
->the_bfd_section
->owner
,
610 section
->the_bfd_section
));
614 fprintf_filtered (outfile
, "block object ");
615 gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol
), outfile
);
616 fprintf_filtered (outfile
, ", ");
617 fputs_filtered (paddress (gdbarch
,
618 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
))),
620 fprintf_filtered (outfile
, "..");
621 fputs_filtered (paddress (gdbarch
,
622 BLOCK_END (SYMBOL_BLOCK_VALUE (symbol
))),
625 fprintf_filtered (outfile
, " section %s",
626 bfd_section_name (section
->the_bfd_section
->owner
,
627 section
->the_bfd_section
));
631 fprintf_filtered (outfile
, "computed at runtime");
635 fprintf_filtered (outfile
, "unresolved");
638 case LOC_OPTIMIZED_OUT
:
639 fprintf_filtered (outfile
, "optimized out");
643 fprintf_filtered (outfile
, "botched symbol class %x",
644 SYMBOL_CLASS (symbol
));
648 fprintf_filtered (outfile
, "\n");
653 maintenance_print_msymbols (char *args
, int from_tty
)
656 struct ui_file
*outfile
;
657 struct cleanup
*cleanups
;
658 char *filename
= DEV_TTY
;
659 char *symname
= NULL
;
660 struct program_space
*pspace
;
661 struct objfile
*objfile
;
663 struct stat sym_st
, obj_st
;
669 error (_("print-msymbols takes an output file name and optional symbol file name"));
671 argv
= gdb_buildargv (args
);
672 cleanups
= make_cleanup_freeargv (argv
);
677 /* If a second arg is supplied, it is a source file name to match on */
680 symname
= xfullpath (argv
[1]);
681 make_cleanup (xfree
, symname
);
682 if (symname
&& stat (symname
, &sym_st
))
683 perror_with_name (symname
);
687 filename
= tilde_expand (filename
);
688 make_cleanup (xfree
, filename
);
690 outfile
= gdb_fopen (filename
, FOPEN_WT
);
692 perror_with_name (filename
);
693 make_cleanup_ui_file_delete (outfile
);
697 ALL_PSPACE_OBJFILES (pspace
, objfile
)
699 || (!stat (objfile
->name
, &obj_st
) && sym_st
.st_ino
== obj_st
.st_ino
))
700 dump_msymbols (objfile
, outfile
);
702 fprintf_filtered (outfile
, "\n\n");
703 do_cleanups (cleanups
);
707 maintenance_print_objfiles (char *ignore
, int from_tty
)
709 struct program_space
*pspace
;
710 struct objfile
*objfile
;
716 ALL_PSPACE_OBJFILES (pspace
, objfile
)
717 dump_objfile (objfile
);
722 /* List all the symbol tables whose names match REGEXP (optional). */
724 maintenance_info_symtabs (char *regexp
, int from_tty
)
726 struct program_space
*pspace
;
727 struct objfile
*objfile
;
733 ALL_PSPACE_OBJFILES (pspace
, objfile
)
735 struct symtab
*symtab
;
737 /* We don't want to print anything for this objfile until we
738 actually find a symtab whose name matches. */
739 int printed_objfile_start
= 0;
741 ALL_OBJFILE_SYMTABS (objfile
, symtab
)
746 || re_exec (symtab
->filename
))
748 if (! printed_objfile_start
)
750 printf_filtered ("{ objfile %s ", objfile
->name
);
752 printf_filtered ("((struct objfile *) %s)\n",
753 host_address_to_string (objfile
));
754 printed_objfile_start
= 1;
757 printf_filtered (" { symtab %s ", symtab
->filename
);
759 printf_filtered ("((struct symtab *) %s)\n",
760 host_address_to_string (symtab
));
761 printf_filtered (" dirname %s\n",
762 symtab
->dirname
? symtab
->dirname
: "(null)");
763 printf_filtered (" fullname %s\n",
764 symtab
->fullname
? symtab
->fullname
: "(null)");
765 printf_filtered (" blockvector ((struct blockvector *) %s)%s\n",
766 host_address_to_string (symtab
->blockvector
),
767 symtab
->primary
? " (primary)" : "");
768 printf_filtered (" linetable ((struct linetable *) %s)\n",
769 host_address_to_string (symtab
->linetable
));
770 printf_filtered (" debugformat %s\n", symtab
->debugformat
);
771 printf_filtered (" }\n");
775 if (printed_objfile_start
)
776 printf_filtered ("}\n");
781 /* Return the nexting depth of a block within other blocks in its symtab. */
784 block_depth (struct block
*block
)
788 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
796 /* Do early runtime initializations. */
798 _initialize_symmisc (void)
This page took 0.045374 seconds and 4 git commands to generate.