1 /* Do various things to symbol tables (other than lookup), for GDB.
2 Copyright 1986, 1987, 1989, 1991, 1992 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"
33 #define DEV_TTY "/dev/tty"
36 /* Unfortunately for debugging, stderr is usually a macro. Better if we
37 make a variable which has the same value and which is accessible when
38 debugging GDB with itself. */
40 FILE *std_out = stdout;
41 FILE *std_err = stderr;
43 /* Prototypes for local functions */
46 dump_symtab PARAMS ((struct objfile *, struct symtab *, FILE *));
49 dump_psymtab PARAMS ((struct objfile *, struct partial_symtab *, FILE *));
52 dump_msymbols PARAMS ((struct objfile *, FILE *));
55 dump_objfile PARAMS ((struct objfile *));
58 printobjfiles_command PARAMS ((char *, int));
61 block_depth PARAMS ((struct block *));
64 print_partial_symbol PARAMS ((struct partial_symbol *, int, char *, FILE *));
67 printpsyms_command PARAMS ((char *, int));
70 print_symbol PARAMS ((struct symbol *, int, FILE *));
73 printsyms_command PARAMS ((char *, int));
76 free_symtab_block PARAMS ((struct objfile *, struct block *));
79 printmsyms_command PARAMS ((char *, int));
81 /* Free a struct block <- B and all the symbols defined in that block. */
84 free_symtab_block (objfile, b)
85 struct objfile *objfile;
90 for (i = 0; i < n; i++)
92 mfree (objfile -> md, SYMBOL_NAME (BLOCK_SYM (b, i)));
93 mfree (objfile -> md, (PTR) BLOCK_SYM (b, i));
95 mfree (objfile -> md, (PTR) b);
98 /* Free all the storage associated with the struct symtab <- S.
99 Note that some symtabs have contents malloc'ed structure by structure,
100 while some have contents that all live inside one big block of memory,
101 and some share the contents of another symbol table and so you should
102 not free the contents on their behalf (except sometimes the linetable,
103 which maybe per symtab even when the rest is not).
104 It is s->free_code that says which alternative to use. */
108 register struct symtab *s;
111 register struct blockvector *bv;
113 switch (s->free_code)
116 /* All the contents are part of a big block of memory (an obstack),
117 and some other symtab is in charge of freeing that block.
118 Therefore, do nothing. */
122 /* Here all the contents were malloc'ed structure by structure
123 and must be freed that way. */
124 /* First free the blocks (and their symbols. */
125 bv = BLOCKVECTOR (s);
126 n = BLOCKVECTOR_NBLOCKS (bv);
127 for (i = 0; i < n; i++)
128 free_symtab_block (s -> objfile, BLOCKVECTOR_BLOCK (bv, i));
129 /* Free the blockvector itself. */
130 mfree (s -> objfile -> md, (PTR) bv);
131 /* Also free the linetable. */
134 /* Everything will be freed either by our `free_ptr'
135 or by some other symtab, except for our linetable.
138 mfree (s -> objfile -> md, (PTR) LINETABLE (s));
142 /* If there is a single block of memory to free, free it. */
143 if (s -> free_ptr != NULL)
144 mfree (s -> objfile -> md, s -> free_ptr);
146 /* Free source-related stuff */
147 if (s -> line_charpos != NULL)
148 mfree (s -> objfile -> md, (PTR) s -> line_charpos);
149 if (s -> fullname != NULL)
150 mfree (s -> objfile -> md, s -> fullname);
151 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 for (index = 0, msymbol = objfile -> msymbols;
213 msymbol -> name != NULL; msymbol++, index++)
215 switch (msymbol -> type)
236 fprintf_filtered (outfile, "[%2d] %c %#10x %s\n", index, ms_type,
237 msymbol -> address, msymbol -> name);
239 if (objfile -> minimal_symbol_count != index)
241 warning ("internal error: minimal symbol count %d != %d",
242 objfile -> minimal_symbol_count, index);
244 fprintf_filtered (outfile, "\n");
248 dump_psymtab (objfile, psymtab, outfile)
249 struct objfile *objfile;
250 struct partial_symtab *psymtab;
254 fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
255 psymtab -> filename);
256 fprintf_filtered (outfile, "(object 0x%x)\n\n", psymtab);
257 fprintf (outfile, " Read from object file %s (0x%x)\n",
258 objfile -> name, objfile);
260 if (psymtab -> readin)
262 fprintf_filtered (outfile,
263 " Full symtab was read (at 0x%x by function at 0x%x)\n",
264 psymtab -> symtab, psymtab -> read_symtab);
266 fprintf_filtered (outfile, " Relocate symbols by 0x%x\n",
268 fprintf_filtered (outfile, " Symbols cover text addresses 0x%x-0x%x\n",
269 psymtab -> textlow, psymtab -> texthigh);
270 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
271 psymtab -> number_of_dependencies);
272 if (psymtab -> n_global_syms > 0)
274 print_partial_symbol (objfile -> global_psymbols.list
275 + psymtab -> globals_offset,
276 psymtab -> n_global_syms, "Global", outfile);
278 if (psymtab -> n_static_syms > 0)
280 print_partial_symbol (objfile -> static_psymbols.list
281 + psymtab -> statics_offset,
282 psymtab -> n_static_syms, "Static", outfile);
284 fprintf_filtered (outfile, "\n");
288 dump_symtab (objfile, symtab, outfile)
289 struct objfile *objfile;
290 struct symtab *symtab;
295 register struct linetable *l;
296 struct blockvector *bv;
297 register struct block *b;
300 fprintf (outfile, "\nSymtab for file %s\n", symtab->filename);
301 fprintf (outfile, "Read from object file %s (%x)\n", objfile->name,
303 fprintf (outfile, "Language: %s\n", language_str (symtab -> language));
305 /* First print the line table. */
306 l = LINETABLE (symtab);
308 fprintf (outfile, "\nLine table:\n\n");
310 for (i = 0; i < len; i++)
311 fprintf (outfile, " line %d at %x\n", l->item[i].line,
314 /* Now print the block info. */
315 fprintf (outfile, "\nBlockvector:\n\n");
316 bv = BLOCKVECTOR (symtab);
317 len = BLOCKVECTOR_NBLOCKS (bv);
318 for (i = 0; i < len; i++)
320 b = BLOCKVECTOR_BLOCK (bv, i);
321 depth = block_depth (b) * 2;
322 print_spaces (depth, outfile);
323 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
324 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
325 if (BLOCK_SUPERBLOCK (b))
326 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
327 if (BLOCK_FUNCTION (b))
328 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
329 fputc ('\n', outfile);
330 blen = BLOCK_NSYMS (b);
331 for (j = 0; j < blen; j++)
333 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
336 fprintf (outfile, "\n");
340 printsyms_command (args, from_tty)
346 struct cleanup *cleanups;
347 char *symname = NULL;
348 char *filename = DEV_TTY;
349 struct objfile *objfile;
356 error ("printsyms takes an output file name and optional symbol file name");
358 else if ((argv = buildargv (args)) == NULL)
362 cleanups = make_cleanup (freeargv, (char *) argv);
367 /* If a second arg is supplied, it is a source file name to match on */
374 filename = tilde_expand (filename);
375 make_cleanup (free, filename);
377 outfile = fopen (filename, "w");
379 perror_with_name (filename);
380 make_cleanup (fclose, (char *) outfile);
383 ALL_SYMTABS (objfile, s)
384 if (symname == NULL || (strcmp (symname, s -> filename) == 0))
385 dump_symtab (objfile, s, outfile);
387 do_cleanups (cleanups);
391 print_symbol (symbol, depth, outfile)
392 struct symbol *symbol;
396 print_spaces (depth, outfile);
397 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
399 fprintf (outfile, "label %s at 0x%x\n", SYMBOL_NAME (symbol),
400 SYMBOL_VALUE_ADDRESS (symbol));
403 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
405 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
407 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
411 fprintf (outfile, "%s %s = ",
412 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
414 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
415 ? "struct" : "union")),
416 SYMBOL_NAME (symbol));
417 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
419 fprintf (outfile, ";\n");
423 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
424 fprintf (outfile, "typedef ");
425 if (SYMBOL_TYPE (symbol))
427 /* Print details of types, except for enums where it's clutter. */
428 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol), outfile,
429 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
431 fprintf (outfile, "; ");
434 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
436 switch (SYMBOL_CLASS (symbol))
439 fprintf (outfile, "const %ld (0x%lx),",
440 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
443 case LOC_CONST_BYTES:
444 fprintf (outfile, "const %u hex bytes:",
445 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
448 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
449 fprintf (outfile, " %2x",
450 (unsigned)SYMBOL_VALUE_BYTES (symbol) [i]);
451 fprintf (outfile, ",");
456 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
460 fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
464 fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
468 fprintf (outfile, "arg at offset 0x%x from fp,",
469 SYMBOL_VALUE (symbol));
472 fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
476 fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
480 fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
487 fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
491 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
492 SYMBOL_BLOCK_VALUE (symbol),
493 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
497 fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
501 fprintf (outfile, "\n");
505 printpsyms_command (args, from_tty)
511 struct cleanup *cleanups;
512 char *symname = NULL;
513 char *filename = DEV_TTY;
514 struct objfile *objfile;
515 struct partial_symtab *ps;
521 error ("printpsyms takes an output file name and optional symbol file name");
523 else if ((argv = buildargv (args)) == NULL)
527 cleanups = make_cleanup (freeargv, (char *) argv);
532 /* If a second arg is supplied, it is a source file name to match on */
539 filename = tilde_expand (filename);
540 make_cleanup (free, filename);
542 outfile = fopen (filename, "w");
544 perror_with_name (filename);
545 make_cleanup (fclose, outfile);
548 ALL_PSYMTABS (objfile, ps)
549 if (symname == NULL || (strcmp (symname, ps -> filename) == 0))
550 dump_psymtab (objfile, ps, outfile);
552 do_cleanups (cleanups);
556 print_partial_symbol (p, count, what, outfile)
557 struct partial_symbol *p;
563 fprintf_filtered (outfile, " %s partial symbols:\n", what);
566 fprintf_filtered (outfile, " `%s', ", SYMBOL_NAME(p));
567 switch (SYMBOL_NAMESPACE (p))
569 case UNDEF_NAMESPACE:
570 fputs_filtered ("undefined namespace, ", outfile);
573 /* This is the usual thing -- don't print it */
575 case STRUCT_NAMESPACE:
576 fputs_filtered ("struct namespace, ", outfile);
578 case LABEL_NAMESPACE:
579 fputs_filtered ("label namespace, ", outfile);
582 fputs_filtered ("<invalid namespace>, ", outfile);
585 switch (SYMBOL_CLASS (p))
588 fputs_filtered ("undefined", outfile);
591 fputs_filtered ("constant int", outfile);
594 fputs_filtered ("static", outfile);
597 fputs_filtered ("register", outfile);
600 fputs_filtered ("pass by value", outfile);
603 fputs_filtered ("pass by reference", outfile);
606 fputs_filtered ("register parameter", outfile);
609 fputs_filtered ("stack parameter", outfile);
612 fputs_filtered ("type", outfile);
615 fputs_filtered ("label", outfile);
618 fputs_filtered ("function", outfile);
620 case LOC_CONST_BYTES:
621 fputs_filtered ("constant bytes", outfile);
624 fputs_filtered ("shuffled arg", outfile);
627 fputs_filtered ("<invalid location>", outfile);
630 fputs_filtered (", ", outfile);
631 fprintf_filtered (outfile, "0x%x\n", SYMBOL_VALUE (p));
637 printmsyms_command (args, from_tty)
643 struct cleanup *cleanups;
644 char *filename = DEV_TTY;
645 char *symname = NULL;
646 struct objfile *objfile;
652 error ("printmsyms takes an output file name and optional symbol file name");
654 else if ((argv = buildargv (args)) == NULL)
658 cleanups = make_cleanup (freeargv, argv);
663 /* If a second arg is supplied, it is a source file name to match on */
670 filename = tilde_expand (filename);
671 make_cleanup (free, filename);
673 outfile = fopen (filename, "w");
675 perror_with_name (filename);
676 make_cleanup (fclose, outfile);
679 ALL_OBJFILES (objfile)
680 if (symname == NULL || (strcmp (symname, objfile -> name) == 0))
681 dump_msymbols (objfile, outfile);
683 fprintf_filtered (outfile, "\n\n");
684 do_cleanups (cleanups);
688 printobjfiles_command (ignore, from_tty)
692 struct objfile *objfile;
697 ALL_OBJFILES (objfile)
698 dump_objfile (objfile);
702 /* Return the nexting depth of a block within other blocks in its symtab. */
709 while (block = BLOCK_SUPERBLOCK (block)) i++;
714 /* Increase the space allocated for LISTP, which is probably
715 global_psymbol_list or static_psymbol_list. This space will eventually
716 be freed in free_objfile(). */
719 extend_psymbol_list (listp, objfile)
720 register struct psymbol_allocation_list *listp;
721 struct objfile *objfile;
724 if (listp->size == 0)
727 listp->list = (struct partial_symbol *)
728 xmmalloc (objfile -> md, new_size * sizeof (struct partial_symbol));
732 new_size = listp->size * 2;
733 listp->list = (struct partial_symbol *)
734 xmrealloc (objfile -> md, (char *) listp->list,
735 new_size * sizeof (struct partial_symbol));
737 /* Next assumes we only went one over. Should be good if
738 program works correctly */
739 listp->next = listp->list + listp->size;
740 listp->size = new_size;
745 /* The work performed by this function is normally done by the macro
746 ADD_PSYMBOL_TO_LIST defined in symfile.h. When debugging gdb, this
747 function makes things easier. */
750 add_psymbol_to_list (name, namelength, namespace, class, listp, psymval)
753 enum namespace namespace;
754 enum address_class class;
755 struct psymbol_allocation_list *listp;
756 unsigned long psymval;
758 register struct partial_symbol *psym;
760 if (listp -> next >= listp -> list + listp -> size)
761 extend_psymbol_list (listp, objfile);
762 psym = listp -> next++;
763 SYMBOL_NAME (psym) = (char *) obstack_alloc (&objfile->psymbol_obstack,
765 memcpy (SYMBOL_NAME (psym), name, namelength);
766 SYMBOL_NAME (psym)[namelength] = '\0';
767 SYMBOL_NAMESPACE (psym) = namespace;
768 SYMBOL_CLASS (psym) = class;
769 SYMBOL_VALUE (psym) = psymval;
772 /* The work performed by this function is normally done by the macro
773 ADD_PSYMBOL_ADDR_TO_LIST defined in symfile.h. When debugging gdb, this
774 function makes things easier. */
777 add_psymbol_addr_to_list (name, namelength, namespace, class, listp, psymval)
780 enum namespace namespace;
781 enum address_class class;
782 struct psymbol_allocation_list *listp;
785 register struct partial_symbol *psym;
787 if (listp -> next >= listp -> list + listp -> size)
788 extend_psymbol_list (listp, objfile);
789 psym = listp -> next++;
790 SYMBOL_NAME (psym) = (char *) obstack_alloc (&objfile->psymbol_obstack,
792 memcpy (SYMBOL_NAME (psym), name, namelength);
793 SYMBOL_NAME (psym)[namelength] = '\0';
794 SYMBOL_NAMESPACE (psym) = namespace;
795 SYMBOL_CLASS (psym) = class;
796 SYMBOL_VALUE_ADDRESS (psym) = psymval;
802 _initialize_symmisc ()
804 add_com ("printmsyms", class_obscure, printmsyms_command,
805 "Print dump of current minimal symbol definitions to file OUTFILE.\n\
806 If a SOURCE file is specified, dump only that file's symbols.");
807 add_com ("printpsyms", class_obscure, printpsyms_command,
808 "Print dump of current partial symbol definitions to file OUTFILE.\n\
809 If a SOURCE file is specified, dump only that file's partial symbols.");
810 add_com ("printsyms", class_obscure, printsyms_command,
811 "Print dump of current symbol definitions to file OUTFILE.\n\
812 If a SOURCE file is specified, dump only that file's symbols.");
813 add_com ("printobjfiles", class_obscure, printobjfiles_command,
814 "Print dump of current object file definitions.");