1 /* Do various things to symbol tables (other than lookup), for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #include "breakpoint.h"
34 #define DEV_TTY "/dev/tty"
37 /* Unfortunately for debugging, stderr is usually a macro. This is painful
38 when calling functions that take FILE *'s from the debugger.
39 So we make a variable which has the same value and which is accessible when
40 debugging GDB with itself. Because stdin et al need not be constants,
41 we initialize them in the _initialize_symmisc function at the bottom
47 /* Prototypes for local functions */
50 dump_symtab
PARAMS ((struct objfile
*, struct symtab
*, FILE *));
53 dump_psymtab
PARAMS ((struct objfile
*, struct partial_symtab
*, FILE *));
56 dump_msymbols
PARAMS ((struct objfile
*, FILE *));
59 dump_objfile
PARAMS ((struct objfile
*));
62 block_depth
PARAMS ((struct block
*));
65 print_partial_symbol
PARAMS ((struct partial_symbol
*, int, char *, FILE *));
67 struct print_symbol_args
{
68 struct symbol
*symbol
;
73 static int print_symbol
PARAMS ((char *));
76 free_symtab_block
PARAMS ((struct objfile
*, struct block
*));
79 /* Free a struct block <- B and all the symbols defined in that block. */
82 free_symtab_block (objfile
, b
)
83 struct objfile
*objfile
;
88 for (i
= 0; i
< n
; i
++)
90 mfree (objfile
-> md
, SYMBOL_NAME (BLOCK_SYM (b
, i
)));
91 mfree (objfile
-> md
, (PTR
) BLOCK_SYM (b
, i
));
93 mfree (objfile
-> md
, (PTR
) b
);
96 /* Free all the storage associated with the struct symtab <- S.
97 Note that some symtabs have contents malloc'ed structure by structure,
98 while some have contents that all live inside one big block of memory,
99 and some share the contents of another symbol table and so you should
100 not free the contents on their behalf (except sometimes the linetable,
101 which maybe per symtab even when the rest is not).
102 It is s->free_code that says which alternative to use. */
106 register struct symtab
*s
;
109 register struct blockvector
*bv
;
111 switch (s
->free_code
)
114 /* All the contents are part of a big block of memory (an obstack),
115 and some other symtab is in charge of freeing that block.
116 Therefore, do nothing. */
120 /* Here all the contents were malloc'ed structure by structure
121 and must be freed that way. */
122 /* First free the blocks (and their symbols. */
123 bv
= BLOCKVECTOR (s
);
124 n
= BLOCKVECTOR_NBLOCKS (bv
);
125 for (i
= 0; i
< n
; i
++)
126 free_symtab_block (s
-> objfile
, BLOCKVECTOR_BLOCK (bv
, i
));
127 /* Free the blockvector itself. */
128 mfree (s
-> objfile
-> md
, (PTR
) bv
);
129 /* Also free the linetable. */
132 /* Everything will be freed either by our `free_ptr'
133 or by some other symtab, except for our linetable.
136 mfree (s
-> objfile
-> md
, (PTR
) LINETABLE (s
));
140 /* If there is a single block of memory to free, free it. */
141 if (s
-> free_ptr
!= NULL
)
142 mfree (s
-> objfile
-> md
, s
-> free_ptr
);
144 /* Free source-related stuff */
145 if (s
-> line_charpos
!= NULL
)
146 mfree (s
-> objfile
-> md
, (PTR
) s
-> line_charpos
);
147 if (s
-> fullname
!= NULL
)
148 mfree (s
-> objfile
-> md
, s
-> fullname
);
149 mfree (s
-> objfile
-> md
, (PTR
) s
);
155 dump_objfile (objfile
)
156 struct objfile
*objfile
;
158 struct symtab
*symtab
;
159 struct partial_symtab
*psymtab
;
161 printf_filtered ("\nObject file %s: ", objfile
-> name
);
162 printf_filtered ("Objfile at %x, bfd at %x, %d minsyms\n\n",
163 objfile
, objfile
-> obfd
, objfile
->minimal_symbol_count
);
165 if (objfile
-> psymtabs
)
167 printf_filtered ("Psymtabs:\n");
168 for (psymtab
= objfile
-> psymtabs
;
170 psymtab
= psymtab
-> next
)
172 printf_filtered ("%s at %x, ", psymtab
-> filename
, psymtab
);
173 if (psymtab
-> objfile
!= objfile
)
175 printf_filtered ("NOT ON CHAIN! ");
179 printf_filtered ("\n\n");
182 if (objfile
-> symtabs
)
184 printf_filtered ("Symtabs:\n");
185 for (symtab
= objfile
-> symtabs
;
187 symtab
= symtab
->next
)
189 printf_filtered ("%s at %x, ", symtab
-> filename
, symtab
);
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 (objfile
, outfile
)
204 struct objfile
*objfile
;
207 struct minimal_symbol
*msymbol
;
211 fprintf_filtered (outfile
, "\nObject file %s:\n\n", objfile
-> name
);
212 if (objfile
-> minimal_symbol_count
== 0)
214 fprintf_filtered (outfile
, "No minimal symbols found.\n");
217 for (index
= 0, msymbol
= objfile
-> msymbols
;
218 SYMBOL_NAME (msymbol
) != NULL
; msymbol
++, index
++)
220 switch (msymbol
-> type
)
250 fprintf_filtered (outfile
, "[%2d] %c %#10x %s", index
, ms_type
,
251 SYMBOL_VALUE_ADDRESS (msymbol
), SYMBOL_NAME (msymbol
));
252 if (SYMBOL_DEMANGLED_NAME (msymbol
) != NULL
)
254 fprintf_filtered (outfile
, " %s", SYMBOL_DEMANGLED_NAME (msymbol
));
256 fputs_filtered ("\n", outfile
);
258 if (objfile
-> minimal_symbol_count
!= index
)
260 warning ("internal error: minimal symbol count %d != %d",
261 objfile
-> minimal_symbol_count
, index
);
263 fprintf_filtered (outfile
, "\n");
267 dump_psymtab (objfile
, psymtab
, outfile
)
268 struct objfile
*objfile
;
269 struct partial_symtab
*psymtab
;
274 fprintf_filtered (outfile
, "\nPartial symtab for source file %s ",
275 psymtab
-> filename
);
276 fprintf_filtered (outfile
, "(object 0x%x)\n\n", psymtab
);
277 fprintf (outfile
, " Read from object file %s (0x%x)\n",
278 objfile
-> name
, (unsigned int) objfile
);
280 if (psymtab
-> readin
)
282 fprintf_filtered (outfile
,
283 " Full symtab was read (at 0x%x by function at 0x%x)\n",
284 psymtab
-> symtab
, psymtab
-> read_symtab
);
287 /* FIXME, we need to be able to print the relocation stuff. */
288 /* This prints some garbage for anything but stabs right now. FIXME. */
289 if (psymtab
->section_offsets
)
290 fprintf_filtered (outfile
, " Relocate symbols by 0x%x, 0x%x, 0x%x, 0x%x.\n",
291 ANOFFSET (psymtab
->section_offsets
, 0),
292 ANOFFSET (psymtab
->section_offsets
, 1),
293 ANOFFSET (psymtab
->section_offsets
, 2),
294 ANOFFSET (psymtab
->section_offsets
, 3));
296 fprintf_filtered (outfile
, " Symbols cover text addresses 0x%x-0x%x\n",
297 psymtab
-> textlow
, psymtab
-> texthigh
);
298 fprintf_filtered (outfile
, " Depends on %d other partial symtabs.\n",
299 psymtab
-> number_of_dependencies
);
300 for (i
= 0; i
< psymtab
-> number_of_dependencies
; i
++)
302 fprintf_filtered (outfile
, " %d 0x%lx %s\n", i
,
303 (unsigned long) psymtab
-> dependencies
[i
],
304 psymtab
-> dependencies
[i
] -> filename
);
306 if (psymtab
-> n_global_syms
> 0)
308 print_partial_symbol (objfile
-> global_psymbols
.list
309 + psymtab
-> globals_offset
,
310 psymtab
-> n_global_syms
, "Global", outfile
);
312 if (psymtab
-> n_static_syms
> 0)
314 print_partial_symbol (objfile
-> static_psymbols
.list
315 + psymtab
-> statics_offset
,
316 psymtab
-> n_static_syms
, "Static", outfile
);
318 fprintf_filtered (outfile
, "\n");
322 dump_symtab (objfile
, symtab
, outfile
)
323 struct objfile
*objfile
;
324 struct symtab
*symtab
;
329 register struct linetable
*l
;
330 struct blockvector
*bv
;
331 register struct block
*b
;
334 fprintf (outfile
, "\nSymtab for file %s\n", symtab
->filename
);
335 fprintf (outfile
, "Read from object file %s (%x)\n", objfile
->name
,
336 (unsigned int) objfile
);
337 fprintf (outfile
, "Language: %s\n", language_str (symtab
-> language
));
339 /* First print the line table. */
340 l
= LINETABLE (symtab
);
342 fprintf (outfile
, "\nLine table:\n\n");
344 for (i
= 0; i
< len
; i
++)
345 fprintf (outfile
, " line %d at %x\n", l
->item
[i
].line
,
348 /* Now print the block info. */
349 fprintf (outfile
, "\nBlockvector:\n\n");
350 bv
= BLOCKVECTOR (symtab
);
351 len
= BLOCKVECTOR_NBLOCKS (bv
);
352 for (i
= 0; i
< len
; i
++)
354 b
= BLOCKVECTOR_BLOCK (bv
, i
);
355 depth
= block_depth (b
) * 2;
356 print_spaces (depth
, outfile
);
357 fprintf (outfile
, "block #%03d (object 0x%x) ", i
, (unsigned int) b
);
358 fprintf (outfile
, "[0x%x..0x%x]", BLOCK_START (b
), BLOCK_END (b
));
359 if (BLOCK_SUPERBLOCK (b
))
360 fprintf (outfile
, " (under 0x%x)", (unsigned int) BLOCK_SUPERBLOCK (b
));
361 if (BLOCK_FUNCTION (b
))
363 fprintf (outfile
, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b
)));
364 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)) != NULL
)
366 fprintf (outfile
, " %s",
367 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b
)));
370 if (BLOCK_GCC_COMPILED(b
))
371 fprintf (outfile
, " gcc%d compiled", BLOCK_GCC_COMPILED(b
));
372 fputc ('\n', outfile
);
373 blen
= BLOCK_NSYMS (b
);
374 for (j
= 0; j
< blen
; j
++)
376 struct print_symbol_args s
;
377 s
.symbol
= BLOCK_SYM (b
, j
);
380 catch_errors (print_symbol
, &s
, "Error printing symbol:\n",
384 fprintf (outfile
, "\n");
388 maintenance_print_symbols (args
, from_tty
)
394 struct cleanup
*cleanups
;
395 char *symname
= NULL
;
396 char *filename
= DEV_TTY
;
397 struct objfile
*objfile
;
404 error ("print-symbols takes an output file name and optional symbol file name");
406 else if ((argv
= buildargv (args
)) == NULL
)
410 cleanups
= make_cleanup (freeargv
, (char *) argv
);
415 /* If a second arg is supplied, it is a source file name to match on */
422 filename
= tilde_expand (filename
);
423 make_cleanup (free
, filename
);
425 outfile
= fopen (filename
, FOPEN_WT
);
427 perror_with_name (filename
);
428 make_cleanup (fclose
, (char *) outfile
);
431 ALL_SYMTABS (objfile
, s
)
432 if (symname
== NULL
|| (STREQ (symname
, s
-> filename
)))
433 dump_symtab (objfile
, s
, outfile
);
435 do_cleanups (cleanups
);
438 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE. ARGS->DEPTH says how
439 far to indent. ARGS is really a struct print_symbol_args *, but is
440 declared as char * to get it past catch_errors. */
446 struct symbol
*symbol
= ((struct print_symbol_args
*)args
)->symbol
;
447 int depth
= ((struct print_symbol_args
*)args
)->depth
;
448 FILE *outfile
= ((struct print_symbol_args
*)args
)->outfile
;
450 print_spaces (depth
, outfile
);
451 if (SYMBOL_NAMESPACE (symbol
) == LABEL_NAMESPACE
)
453 fprintf (outfile
, "label %s at 0x%x\n", SYMBOL_SOURCE_NAME (symbol
),
454 SYMBOL_VALUE_ADDRESS (symbol
));
457 if (SYMBOL_NAMESPACE (symbol
) == STRUCT_NAMESPACE
)
459 if (TYPE_NAME (SYMBOL_TYPE (symbol
)))
461 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
465 fprintf (outfile
, "%s %s = ",
466 (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_ENUM
468 : (TYPE_CODE (SYMBOL_TYPE (symbol
)) == TYPE_CODE_STRUCT
469 ? "struct" : "union")),
470 SYMBOL_NAME (symbol
));
471 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), "", outfile
, 1, depth
);
473 fprintf (outfile
, ";\n");
477 if (SYMBOL_CLASS (symbol
) == LOC_TYPEDEF
)
478 fprintf (outfile
, "typedef ");
479 if (SYMBOL_TYPE (symbol
))
481 /* Print details of types, except for enums where it's clutter. */
482 LA_PRINT_TYPE (SYMBOL_TYPE (symbol
), SYMBOL_SOURCE_NAME (symbol
),
484 TYPE_CODE (SYMBOL_TYPE (symbol
)) != TYPE_CODE_ENUM
,
486 fprintf (outfile
, "; ");
489 fprintf (outfile
, "%s ", SYMBOL_SOURCE_NAME (symbol
));
491 switch (SYMBOL_CLASS (symbol
))
494 fprintf (outfile
, "const %ld (0x%lx),",
495 SYMBOL_VALUE (symbol
), SYMBOL_VALUE (symbol
));
498 case LOC_CONST_BYTES
:
499 fprintf (outfile
, "const %u hex bytes:",
500 TYPE_LENGTH (SYMBOL_TYPE (symbol
)));
503 for (i
= 0; i
< TYPE_LENGTH (SYMBOL_TYPE (symbol
)); i
++)
504 fprintf (outfile
, " %2x",
505 (unsigned)SYMBOL_VALUE_BYTES (symbol
) [i
]);
506 fprintf (outfile
, ",");
511 fprintf (outfile
, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol
));
515 fprintf (outfile
, "register %ld,", SYMBOL_VALUE (symbol
));
519 fprintf (outfile
, "arg at 0x%lx,", SYMBOL_VALUE (symbol
));
523 fprintf (outfile
, "arg at offset 0x%lx from fp,",
524 SYMBOL_VALUE (symbol
));
528 fprintf (outfile
, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol
));
532 fprintf (outfile
, "parameter register %ld,", SYMBOL_VALUE (symbol
));
535 case LOC_REGPARM_ADDR
:
536 fprintf (outfile
, "address parameter register %ld,", SYMBOL_VALUE (symbol
));
540 fprintf (outfile
, "local at 0x%lx,", SYMBOL_VALUE (symbol
));
544 fprintf (outfile
, "local at 0x%lx from register %d",
545 SYMBOL_VALUE (symbol
), SYMBOL_BASEREG (symbol
));
548 case LOC_BASEREG_ARG
:
549 fprintf (outfile
, "arg at 0x%lx from register %d,",
550 SYMBOL_VALUE (symbol
), SYMBOL_BASEREG (symbol
));
557 fprintf (outfile
, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol
));
561 fprintf (outfile
, "block (object 0x%x) starting at 0x%x,",
562 (unsigned int) SYMBOL_BLOCK_VALUE (symbol
),
563 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol
)));
566 case LOC_OPTIMIZED_OUT
:
567 fprintf (outfile
, "optimized out");
571 fprintf (outfile
, "botched symbol class %x", SYMBOL_CLASS (symbol
));
575 fprintf (outfile
, "\n");
579 maintenance_print_psymbols (args
, from_tty
)
585 struct cleanup
*cleanups
;
586 char *symname
= NULL
;
587 char *filename
= DEV_TTY
;
588 struct objfile
*objfile
;
589 struct partial_symtab
*ps
;
595 error ("print-psymbols takes an output file name and optional symbol file name");
597 else if ((argv
= buildargv (args
)) == NULL
)
601 cleanups
= make_cleanup (freeargv
, (char *) argv
);
606 /* If a second arg is supplied, it is a source file name to match on */
613 filename
= tilde_expand (filename
);
614 make_cleanup (free
, filename
);
616 outfile
= fopen (filename
, FOPEN_WT
);
618 perror_with_name (filename
);
619 make_cleanup (fclose
, outfile
);
622 ALL_PSYMTABS (objfile
, ps
)
623 if (symname
== NULL
|| (STREQ (symname
, ps
-> filename
)))
624 dump_psymtab (objfile
, ps
, outfile
);
626 do_cleanups (cleanups
);
630 print_partial_symbol (p
, count
, what
, outfile
)
631 struct partial_symbol
*p
;
637 fprintf_filtered (outfile
, " %s partial symbols:\n", what
);
640 fprintf_filtered (outfile
, " `%s'", SYMBOL_NAME(p
));
641 if (SYMBOL_DEMANGLED_NAME (p
) != NULL
)
643 fprintf_filtered (outfile
, " `%s'", SYMBOL_DEMANGLED_NAME (p
));
645 fputs_filtered (", ", outfile
);
646 switch (SYMBOL_NAMESPACE (p
))
648 case UNDEF_NAMESPACE
:
649 fputs_filtered ("undefined namespace, ", outfile
);
652 /* This is the usual thing -- don't print it */
654 case STRUCT_NAMESPACE
:
655 fputs_filtered ("struct namespace, ", outfile
);
657 case LABEL_NAMESPACE
:
658 fputs_filtered ("label namespace, ", outfile
);
661 fputs_filtered ("<invalid namespace>, ", outfile
);
664 switch (SYMBOL_CLASS (p
))
667 fputs_filtered ("undefined", outfile
);
670 fputs_filtered ("constant int", outfile
);
673 fputs_filtered ("static", outfile
);
676 fputs_filtered ("register", outfile
);
679 fputs_filtered ("pass by value", outfile
);
682 fputs_filtered ("pass by reference", outfile
);
685 fputs_filtered ("register parameter", outfile
);
687 case LOC_REGPARM_ADDR
:
688 fputs_filtered ("register address parameter", outfile
);
691 fputs_filtered ("stack parameter", outfile
);
694 fputs_filtered ("type", outfile
);
697 fputs_filtered ("label", outfile
);
700 fputs_filtered ("function", outfile
);
702 case LOC_CONST_BYTES
:
703 fputs_filtered ("constant bytes", outfile
);
706 fputs_filtered ("shuffled arg", outfile
);
708 case LOC_OPTIMIZED_OUT
:
709 fputs_filtered ("optimized out", outfile
);
712 fputs_filtered ("<invalid location>", outfile
);
715 fputs_filtered (", ", outfile
);
716 fprintf_filtered (outfile
, "0x%x\n", SYMBOL_VALUE (p
));
722 maintenance_print_msymbols (args
, from_tty
)
728 struct cleanup
*cleanups
;
729 char *filename
= DEV_TTY
;
730 char *symname
= NULL
;
731 struct objfile
*objfile
;
737 error ("print-msymbols takes an output file name and optional symbol file name");
739 else if ((argv
= buildargv (args
)) == NULL
)
743 cleanups
= make_cleanup (freeargv
, argv
);
748 /* If a second arg is supplied, it is a source file name to match on */
755 filename
= tilde_expand (filename
);
756 make_cleanup (free
, filename
);
758 outfile
= fopen (filename
, FOPEN_WT
);
760 perror_with_name (filename
);
761 make_cleanup (fclose
, outfile
);
764 ALL_OBJFILES (objfile
)
765 if (symname
== NULL
|| (STREQ (symname
, objfile
-> name
)))
766 dump_msymbols (objfile
, outfile
);
768 fprintf_filtered (outfile
, "\n\n");
769 do_cleanups (cleanups
);
773 maintenance_print_objfiles (ignore
, from_tty
)
777 struct objfile
*objfile
;
782 ALL_OBJFILES (objfile
)
783 dump_objfile (objfile
);
788 /* Return the nexting depth of a block within other blocks in its symtab. */
795 while ((block
= BLOCK_SUPERBLOCK (block
)) != NULL
)
802 #endif /* MAINTENANCE_CMDS */
805 /* Increase the space allocated for LISTP, which is probably
806 global_psymbol_list or static_psymbol_list. This space will eventually
807 be freed in free_objfile(). */
810 extend_psymbol_list (listp
, objfile
)
811 register struct psymbol_allocation_list
*listp
;
812 struct objfile
*objfile
;
815 if (listp
->size
== 0)
818 listp
->list
= (struct partial_symbol
*)
819 xmmalloc (objfile
-> md
, new_size
* sizeof (struct partial_symbol
));
823 new_size
= listp
->size
* 2;
824 listp
->list
= (struct partial_symbol
*)
825 xmrealloc (objfile
-> md
, (char *) listp
->list
,
826 new_size
* sizeof (struct partial_symbol
));
828 /* Next assumes we only went one over. Should be good if
829 program works correctly */
830 listp
->next
= listp
->list
+ listp
->size
;
831 listp
->size
= new_size
;
835 /* Do early runtime initializations. */
837 _initialize_symmisc ()