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 *));
68 print_symbol PARAMS ((struct symbol *, int, FILE *));
71 free_symtab_block PARAMS ((struct objfile *, struct block *));
74 /* Free a struct block <- B and all the symbols defined in that block. */
77 free_symtab_block (objfile, b)
78 struct objfile *objfile;
83 for (i = 0; i < n; i++)
85 mfree (objfile -> md, SYMBOL_NAME (BLOCK_SYM (b, i)));
86 mfree (objfile -> md, (PTR) BLOCK_SYM (b, i));
88 mfree (objfile -> md, (PTR) b);
91 /* Free all the storage associated with the struct symtab <- S.
92 Note that some symtabs have contents malloc'ed structure by structure,
93 while some have contents that all live inside one big block of memory,
94 and some share the contents of another symbol table and so you should
95 not free the contents on their behalf (except sometimes the linetable,
96 which maybe per symtab even when the rest is not).
97 It is s->free_code that says which alternative to use. */
101 register struct symtab *s;
104 register struct blockvector *bv;
106 switch (s->free_code)
109 /* All the contents are part of a big block of memory (an obstack),
110 and some other symtab is in charge of freeing that block.
111 Therefore, do nothing. */
115 /* Here all the contents were malloc'ed structure by structure
116 and must be freed that way. */
117 /* First free the blocks (and their symbols. */
118 bv = BLOCKVECTOR (s);
119 n = BLOCKVECTOR_NBLOCKS (bv);
120 for (i = 0; i < n; i++)
121 free_symtab_block (s -> objfile, BLOCKVECTOR_BLOCK (bv, i));
122 /* Free the blockvector itself. */
123 mfree (s -> objfile -> md, (PTR) bv);
124 /* Also free the linetable. */
127 /* Everything will be freed either by our `free_ptr'
128 or by some other symtab, except for our linetable.
131 mfree (s -> objfile -> md, (PTR) LINETABLE (s));
135 /* If there is a single block of memory to free, free it. */
136 if (s -> free_ptr != NULL)
137 mfree (s -> objfile -> md, s -> free_ptr);
139 /* Free source-related stuff */
140 if (s -> line_charpos != NULL)
141 mfree (s -> objfile -> md, (PTR) s -> line_charpos);
142 if (s -> fullname != NULL)
143 mfree (s -> objfile -> md, s -> fullname);
144 mfree (s -> objfile -> md, (PTR) s);
150 dump_objfile (objfile)
151 struct objfile *objfile;
153 struct symtab *symtab;
154 struct partial_symtab *psymtab;
156 printf_filtered ("\nObject file %s: ", objfile -> name);
157 printf_filtered ("Objfile at %x, bfd at %x, %d minsyms\n\n",
158 objfile, objfile -> obfd, objfile->minimal_symbol_count);
160 if (objfile -> psymtabs)
162 printf_filtered ("Psymtabs:\n");
163 for (psymtab = objfile -> psymtabs;
165 psymtab = psymtab -> next)
167 printf_filtered ("%s at %x, ", psymtab -> filename, psymtab);
168 if (psymtab -> objfile != objfile)
170 printf_filtered ("NOT ON CHAIN! ");
174 printf_filtered ("\n\n");
177 if (objfile -> symtabs)
179 printf_filtered ("Symtabs:\n");
180 for (symtab = objfile -> symtabs;
182 symtab = symtab->next)
184 printf_filtered ("%s at %x, ", symtab -> filename, symtab);
185 if (symtab -> objfile != objfile)
187 printf_filtered ("NOT ON CHAIN! ");
191 printf_filtered ("\n\n");
195 /* Print minimal symbols from this objfile. */
198 dump_msymbols (objfile, outfile)
199 struct objfile *objfile;
202 struct minimal_symbol *msymbol;
206 fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile -> name);
207 if (objfile -> minimal_symbol_count == 0)
209 fprintf_filtered (outfile, "No minimal symbols found.\n");
212 for (index = 0, msymbol = objfile -> msymbols;
213 SYMBOL_NAME (msymbol) != NULL; msymbol++, index++)
215 switch (msymbol -> type)
245 fprintf_filtered (outfile, "[%2d] %c %#10x %s", index, ms_type,
246 SYMBOL_VALUE_ADDRESS (msymbol), SYMBOL_NAME (msymbol));
247 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
249 fprintf_filtered (outfile, " %s", SYMBOL_DEMANGLED_NAME (msymbol));
251 fputs_filtered ("\n", outfile);
253 if (objfile -> minimal_symbol_count != index)
255 warning ("internal error: minimal symbol count %d != %d",
256 objfile -> minimal_symbol_count, index);
258 fprintf_filtered (outfile, "\n");
262 dump_psymtab (objfile, psymtab, outfile)
263 struct objfile *objfile;
264 struct partial_symtab *psymtab;
269 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
270 psymtab -> filename);
271 fprintf_filtered (outfile, "(object 0x%x)\n\n", psymtab);
272 fprintf (outfile, " Read from object file %s (0x%x)\n",
273 objfile -> name, (unsigned int) objfile);
275 if (psymtab -> readin)
277 fprintf_filtered (outfile,
278 " Full symtab was read (at 0x%x by function at 0x%x)\n",
279 psymtab -> symtab, psymtab -> read_symtab);
282 /* FIXME, we need to be able to print the relocation stuff. */
283 /* This prints some garbage for anything but stabs right now. FIXME. */
284 if (psymtab->section_offsets)
285 fprintf_filtered (outfile, " Relocate symbols by 0x%x, 0x%x, 0x%x, 0x%x.\n",
286 ANOFFSET (psymtab->section_offsets, 0),
287 ANOFFSET (psymtab->section_offsets, 1),
288 ANOFFSET (psymtab->section_offsets, 2),
289 ANOFFSET (psymtab->section_offsets, 3));
291 fprintf_filtered (outfile, " Symbols cover text addresses 0x%x-0x%x\n",
292 psymtab -> textlow, psymtab -> texthigh);
293 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
294 psymtab -> number_of_dependencies);
295 for (i = 0; i < psymtab -> number_of_dependencies; i++)
297 fprintf_filtered (outfile, " %d 0x%lx %s\n", i,
298 (unsigned long) psymtab -> dependencies[i],
299 psymtab -> dependencies[i] -> filename);
301 if (psymtab -> n_global_syms > 0)
303 print_partial_symbol (objfile -> global_psymbols.list
304 + psymtab -> globals_offset,
305 psymtab -> n_global_syms, "Global", outfile);
307 if (psymtab -> n_static_syms > 0)
309 print_partial_symbol (objfile -> static_psymbols.list
310 + psymtab -> statics_offset,
311 psymtab -> n_static_syms, "Static", outfile);
313 fprintf_filtered (outfile, "\n");
317 dump_symtab (objfile, symtab, outfile)
318 struct objfile *objfile;
319 struct symtab *symtab;
324 register struct linetable *l;
325 struct blockvector *bv;
326 register struct block *b;
329 fprintf (outfile, "\nSymtab for file %s\n", symtab->filename);
330 fprintf (outfile, "Read from object file %s (%x)\n", objfile->name,
331 (unsigned int) objfile);
332 fprintf (outfile, "Language: %s\n", language_str (symtab -> language));
334 /* First print the line table. */
335 l = LINETABLE (symtab);
337 fprintf (outfile, "\nLine table:\n\n");
339 for (i = 0; i < len; i++)
340 fprintf (outfile, " line %d at %x\n", l->item[i].line,
343 /* Now print the block info. */
344 fprintf (outfile, "\nBlockvector:\n\n");
345 bv = BLOCKVECTOR (symtab);
346 len = BLOCKVECTOR_NBLOCKS (bv);
347 for (i = 0; i < len; i++)
349 b = BLOCKVECTOR_BLOCK (bv, i);
350 depth = block_depth (b) * 2;
351 print_spaces (depth, outfile);
352 fprintf (outfile, "block #%03d (object 0x%x) ", i, (unsigned int) b);
353 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
354 if (BLOCK_SUPERBLOCK (b))
355 fprintf (outfile, " (under 0x%x)", (unsigned int) BLOCK_SUPERBLOCK (b));
356 if (BLOCK_FUNCTION (b))
358 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
359 if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
361 fprintf (outfile, " %s",
362 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
365 if (BLOCK_GCC_COMPILED(b))
366 fprintf (outfile, " gcc%d compiled", BLOCK_GCC_COMPILED(b));
367 fputc ('\n', outfile);
368 blen = BLOCK_NSYMS (b);
369 for (j = 0; j < blen; j++)
371 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
374 fprintf (outfile, "\n");
378 maintenance_print_symbols (args, from_tty)
384 struct cleanup *cleanups;
385 char *symname = NULL;
386 char *filename = DEV_TTY;
387 struct objfile *objfile;
394 error ("print-symbols takes an output file name and optional symbol file name");
396 else if ((argv = buildargv (args)) == NULL)
400 cleanups = make_cleanup (freeargv, (char *) argv);
405 /* If a second arg is supplied, it is a source file name to match on */
412 filename = tilde_expand (filename);
413 make_cleanup (free, filename);
415 outfile = fopen (filename, FOPEN_WT);
417 perror_with_name (filename);
418 make_cleanup (fclose, (char *) outfile);
421 ALL_SYMTABS (objfile, s)
422 if (symname == NULL || (STREQ (symname, s -> filename)))
423 dump_symtab (objfile, s, outfile);
425 do_cleanups (cleanups);
429 print_symbol (symbol, depth, outfile)
430 struct symbol *symbol;
434 print_spaces (depth, outfile);
435 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
437 fprintf (outfile, "label %s at 0x%x\n", SYMBOL_SOURCE_NAME (symbol),
438 SYMBOL_VALUE_ADDRESS (symbol));
441 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
443 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
445 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
449 fprintf (outfile, "%s %s = ",
450 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
452 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
453 ? "struct" : "union")),
454 SYMBOL_NAME (symbol));
455 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
457 fprintf (outfile, ";\n");
461 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
462 fprintf (outfile, "typedef ");
463 if (SYMBOL_TYPE (symbol))
465 /* Print details of types, except for enums where it's clutter. */
466 LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_SOURCE_NAME (symbol),
468 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
470 fprintf (outfile, "; ");
473 fprintf (outfile, "%s ", SYMBOL_SOURCE_NAME (symbol));
475 switch (SYMBOL_CLASS (symbol))
478 fprintf (outfile, "const %ld (0x%lx),",
479 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
482 case LOC_CONST_BYTES:
483 fprintf (outfile, "const %u hex bytes:",
484 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
487 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
488 fprintf (outfile, " %2x",
489 (unsigned)SYMBOL_VALUE_BYTES (symbol) [i]);
490 fprintf (outfile, ",");
495 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
499 fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
503 if (SYMBOL_BASEREG_VALID (symbol))
505 fprintf (outfile, "arg at 0x%lx from register %d,",
506 SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
510 fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
515 if (SYMBOL_BASEREG_VALID (symbol))
517 fprintf (outfile, "arg at offset 0x%lx from register %d,",
518 SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
522 fprintf (outfile, "arg at offset 0x%lx from fp,",
523 SYMBOL_VALUE (symbol));
527 fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
531 fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
534 case LOC_REGPARM_ADDR:
535 fprintf (outfile, "address parameter register %ld,", SYMBOL_VALUE (symbol));
539 if (SYMBOL_BASEREG_VALID (symbol))
541 fprintf (outfile, "local at 0x%lx from register %d",
542 SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
546 fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
554 fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
558 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
559 (unsigned int) SYMBOL_BLOCK_VALUE (symbol),
560 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
563 case LOC_OPTIMIZED_OUT:
564 fprintf (outfile, "optimized out");
568 fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
572 fprintf (outfile, "\n");
576 maintenance_print_psymbols (args, from_tty)
582 struct cleanup *cleanups;
583 char *symname = NULL;
584 char *filename = DEV_TTY;
585 struct objfile *objfile;
586 struct partial_symtab *ps;
592 error ("print-psymbols takes an output file name and optional symbol file name");
594 else if ((argv = buildargv (args)) == NULL)
598 cleanups = make_cleanup (freeargv, (char *) argv);
603 /* If a second arg is supplied, it is a source file name to match on */
610 filename = tilde_expand (filename);
611 make_cleanup (free, filename);
613 outfile = fopen (filename, FOPEN_WT);
615 perror_with_name (filename);
616 make_cleanup (fclose, outfile);
619 ALL_PSYMTABS (objfile, ps)
620 if (symname == NULL || (STREQ (symname, ps -> filename)))
621 dump_psymtab (objfile, ps, outfile);
623 do_cleanups (cleanups);
627 print_partial_symbol (p, count, what, outfile)
628 struct partial_symbol *p;
634 fprintf_filtered (outfile, " %s partial symbols:\n", what);
637 fprintf_filtered (outfile, " `%s'", SYMBOL_NAME(p));
638 if (SYMBOL_DEMANGLED_NAME (p) != NULL)
640 fprintf_filtered (outfile, " `%s'", SYMBOL_DEMANGLED_NAME (p));
642 fputs_filtered (", ", outfile);
643 switch (SYMBOL_NAMESPACE (p))
645 case UNDEF_NAMESPACE:
646 fputs_filtered ("undefined namespace, ", outfile);
649 /* This is the usual thing -- don't print it */
651 case STRUCT_NAMESPACE:
652 fputs_filtered ("struct namespace, ", outfile);
654 case LABEL_NAMESPACE:
655 fputs_filtered ("label namespace, ", outfile);
658 fputs_filtered ("<invalid namespace>, ", outfile);
661 switch (SYMBOL_CLASS (p))
664 fputs_filtered ("undefined", outfile);
667 fputs_filtered ("constant int", outfile);
670 fputs_filtered ("static", outfile);
673 fputs_filtered ("register", outfile);
676 fputs_filtered ("pass by value", outfile);
679 fputs_filtered ("pass by reference", outfile);
682 fputs_filtered ("register parameter", outfile);
684 case LOC_REGPARM_ADDR:
685 fputs_filtered ("register address parameter", outfile);
688 fputs_filtered ("stack parameter", outfile);
691 fputs_filtered ("type", outfile);
694 fputs_filtered ("label", outfile);
697 fputs_filtered ("function", outfile);
699 case LOC_CONST_BYTES:
700 fputs_filtered ("constant bytes", outfile);
703 fputs_filtered ("shuffled arg", outfile);
705 case LOC_OPTIMIZED_OUT:
706 fputs_filtered ("optimized out", outfile);
709 fputs_filtered ("<invalid location>", outfile);
712 fputs_filtered (", ", outfile);
713 fprintf_filtered (outfile, "0x%x\n", SYMBOL_VALUE (p));
719 maintenance_print_msymbols (args, from_tty)
725 struct cleanup *cleanups;
726 char *filename = DEV_TTY;
727 char *symname = NULL;
728 struct objfile *objfile;
734 error ("print-msymbols takes an output file name and optional symbol file name");
736 else if ((argv = buildargv (args)) == NULL)
740 cleanups = make_cleanup (freeargv, argv);
745 /* If a second arg is supplied, it is a source file name to match on */
752 filename = tilde_expand (filename);
753 make_cleanup (free, filename);
755 outfile = fopen (filename, FOPEN_WT);
757 perror_with_name (filename);
758 make_cleanup (fclose, outfile);
761 ALL_OBJFILES (objfile)
762 if (symname == NULL || (STREQ (symname, objfile -> name)))
763 dump_msymbols (objfile, outfile);
765 fprintf_filtered (outfile, "\n\n");
766 do_cleanups (cleanups);
770 maintenance_print_objfiles (ignore, from_tty)
774 struct objfile *objfile;
779 ALL_OBJFILES (objfile)
780 dump_objfile (objfile);
785 /* Return the nexting depth of a block within other blocks in its symtab. */
792 while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
799 #endif /* MAINTENANCE_CMDS */
802 /* Increase the space allocated for LISTP, which is probably
803 global_psymbol_list or static_psymbol_list. This space will eventually
804 be freed in free_objfile(). */
807 extend_psymbol_list (listp, objfile)
808 register struct psymbol_allocation_list *listp;
809 struct objfile *objfile;
812 if (listp->size == 0)
815 listp->list = (struct partial_symbol *)
816 xmmalloc (objfile -> md, new_size * sizeof (struct partial_symbol));
820 new_size = listp->size * 2;
821 listp->list = (struct partial_symbol *)
822 xmrealloc (objfile -> md, (char *) listp->list,
823 new_size * sizeof (struct partial_symbol));
825 /* Next assumes we only went one over. Should be good if
826 program works correctly */
827 listp->next = listp->list + listp->size;
828 listp->size = new_size;
832 /* Do early runtime initializations. */
834 _initialize_symmisc ()