1 /* Do various things to symbol tables (other than lookup), for GDB.
2 Copyright 1986, 1987, 1989, 1991 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"
32 /* Free a struct block <- B and all the symbols defined in that block. */
40 for (i = 0; i < n; i++)
42 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
43 free (BLOCK_SYM (b, i));
48 /* Free all the storage associated with the struct symtab <- S.
49 Note that some symtabs have contents malloc'ed structure by structure,
50 while some have contents that all live inside one big block of memory,
51 and some share the contents of another symbol table and so you should
52 not free the contents on their behalf (except sometimes the linetable,
53 which maybe per symtab even when the rest is not).
54 It is s->free_code that says which alternative to use. */
58 register struct symtab *s;
61 register struct blockvector *bv;
66 /* All the contents are part of a big block of memory (an obstack),
67 and some other symtab is in charge of freeing that block.
68 Therefore, do nothing. */
72 /* Here all the contents were malloc'ed structure by structure
73 and must be freed that way. */
74 /* First free the blocks (and their symbols. */
76 n = BLOCKVECTOR_NBLOCKS (bv);
77 for (i = 0; i < n; i++)
78 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
79 /* Free the blockvector itself. */
81 /* Also free the linetable. */
84 /* Everything will be freed either by our `free_ptr'
85 or by some other symtab, except for our linetable.
92 /* If there is a single block of memory to free, free it. */
96 /* Free source-related stuff */
98 free (s->line_charpos);
104 static int block_depth ();
105 static void print_symbol ();
106 static void print_partial_symbol ();
109 printsyms_command (filename)
113 register struct symtab *s;
116 register struct linetable *l;
117 struct blockvector *bv;
118 register struct block *b;
120 struct cleanup *cleanups;
125 error_no_arg ("file to write symbol data in");
127 /* If a second arg is supplied, it is a source file name to match on */
128 symname = strchr (filename, ' ');
132 filename = tilde_expand (filename);
133 make_cleanup (free, filename);
135 outfile = fopen (filename, "w");
137 perror_with_name (filename);
139 cleanups = make_cleanup (fclose, outfile);
142 for (s = symtab_list; s; s = s->next)
144 /* If source file name is specified, reject all but that one. */
146 if (0 != strncmp (symname, s->filename, strlen (symname)))
149 fprintf (outfile, "Symtab for file %s\n", s->filename);
150 fprintf (outfile, "Read from object file %s (%x)\n", s->objfile->name,
153 /* First print the line table. */
156 fprintf (outfile, "\nLine table:\n\n");
158 for (i = 0; i < len; i++)
159 fprintf (outfile, " line %d at %x\n", l->item[i].line,
162 /* Now print the block info. */
163 fprintf (outfile, "\nBlockvector:\n\n");
164 bv = BLOCKVECTOR (s);
165 len = BLOCKVECTOR_NBLOCKS (bv);
166 for (i = 0; i < len; i++)
168 b = BLOCKVECTOR_BLOCK (bv, i);
169 depth = block_depth (b) * 2;
170 print_spaces (depth, outfile);
171 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
172 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
173 if (BLOCK_SUPERBLOCK (b))
174 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
175 if (BLOCK_FUNCTION (b))
176 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
177 fputc ('\n', outfile);
178 blen = BLOCK_NSYMS (b);
179 for (j = 0; j < blen; j++)
181 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
185 fprintf (outfile, "\n\n");
189 do_cleanups (cleanups);
193 print_symbol (symbol, depth, outfile)
194 struct symbol *symbol;
198 print_spaces (depth, outfile);
199 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
201 fprintf (outfile, "label %s at 0x%x\n", SYMBOL_NAME (symbol),
202 SYMBOL_VALUE_ADDRESS (symbol));
205 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
207 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
209 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
213 fprintf (outfile, "%s %s = ",
214 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
216 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
217 ? "struct" : "union")),
218 SYMBOL_NAME (symbol));
219 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
221 fprintf (outfile, ";\n");
225 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
226 fprintf (outfile, "typedef ");
227 if (SYMBOL_TYPE (symbol))
229 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
231 fprintf (outfile, "; ");
234 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
236 switch (SYMBOL_CLASS (symbol))
239 fprintf (outfile, "const %ld (0x%lx),",
240 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
243 case LOC_CONST_BYTES:
244 fprintf (outfile, "const %u hex bytes:",
245 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
248 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
249 fprintf (outfile, " %2x",
250 (unsigned)SYMBOL_VALUE_BYTES (symbol) [i]);
251 fprintf (outfile, ",");
256 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
260 fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
264 fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
268 fprintf (outfile, "arg at offset 0x%x from fp,",
269 SYMBOL_VALUE (symbol));
272 fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
276 fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
280 fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
287 fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
291 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
292 SYMBOL_BLOCK_VALUE (symbol),
293 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
297 fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
301 fprintf (outfile, "\n");
305 printpsyms_command (filename)
309 struct partial_symtab *p;
310 struct cleanup *cleanups;
315 error_no_arg ("file to write partial symbol data in");
317 /* If a second arg is supplied, it is a source file name to match on */
318 symname = strchr (filename, ' ');
322 filename = tilde_expand (filename);
323 make_cleanup (free, filename);
325 outfile = fopen (filename, "w");
327 perror_with_name (filename);
329 cleanups = make_cleanup (fclose, outfile);
332 for (p = partial_symtab_list; p; p = p->next)
334 /* If source file name is specified, reject all but that one. */
336 if (0 != strncmp (symname, p->filename, strlen (symname)))
339 fprintf_filtered (outfile, "Partial symtab for source file %s ",
341 fprintf_filtered (outfile, "(object 0x%x)\n\n", p);
342 fprintf (outfile, " Read from object file %s (0x%x)\n", p->objfile->name,
346 fprintf_filtered (outfile, " Full symtab was read (at 0x%x by function at 0x%x)\n",
347 p->symtab, p->read_symtab);
348 fprintf_filtered (outfile, " Relocate symbols by 0x%x\n", p->addr);
349 fprintf_filtered (outfile, " Symbols cover text addresses 0x%x-0x%x\n",
350 p->textlow, p->texthigh);
351 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
352 p->number_of_dependencies);
353 if (p->n_global_syms > 0)
354 print_partial_symbol (global_psymbols.list + p->globals_offset,
355 p->n_global_syms, "Global", outfile);
356 if (p->n_static_syms > 0)
357 print_partial_symbol (static_psymbols.list + p->statics_offset,
358 p->n_static_syms, "Static", outfile);
359 fprintf_filtered (outfile, "\n\n");
363 do_cleanups (cleanups);
367 print_partial_symbol (p, count, what, outfile)
368 struct partial_symbol *p;
374 fprintf_filtered (outfile, " %s partial symbols:\n", what);
377 fprintf_filtered (outfile, " `%s', ", SYMBOL_NAME(p));
378 switch (SYMBOL_NAMESPACE (p))
380 case UNDEF_NAMESPACE:
381 fputs_filtered ("undefined namespace, ", outfile);
384 /* This is the usual thing -- don't print it */
386 case STRUCT_NAMESPACE:
387 fputs_filtered ("struct namespace, ", outfile);
389 case LABEL_NAMESPACE:
390 fputs_filtered ("label namespace, ", outfile);
393 fputs_filtered ("<invalid namespace>, ", outfile);
396 switch (SYMBOL_CLASS (p))
399 fputs_filtered ("undefined", outfile);
402 fputs_filtered ("constant int", outfile);
405 fputs_filtered ("static", outfile);
408 fputs_filtered ("register", outfile);
411 fputs_filtered ("pass by value", outfile);
414 fputs_filtered ("pass by reference", outfile);
417 fputs_filtered ("register parameter", outfile);
420 fputs_filtered ("stack parameter", outfile);
423 fputs_filtered ("type", outfile);
426 fputs_filtered ("label", outfile);
429 fputs_filtered ("function", outfile);
431 case LOC_CONST_BYTES:
432 fputs_filtered ("constant bytes", outfile);
435 fputs_filtered ("shuffled arg", outfile);
438 fputs_filtered ("<invalid location>", outfile);
441 fputs_filtered (", ", outfile);
442 fprintf_filtered (outfile, "0x%x\n", SYMBOL_VALUE (p));
447 /* Return the nexting depth of a block within other blocks in its symtab. */
454 while (block = BLOCK_SUPERBLOCK (block)) i++;
459 printobjfiles_command ()
461 struct objfile *objfile;
462 struct symtab *symtab;
463 struct partial_symtab *psymtab;
466 for (objfile = object_files; objfile; objfile = objfile->next) {
467 printf_filtered ("\nObject file %s: ", objfile->name);
468 printf_filtered ("Objfile at %x, bfd at %x\n\n", objfile, objfile->obfd);
470 if (objfile->psymtabs) {
471 printf_filtered ("Psymtabs:\n");
472 for (psymtab = objfile->psymtabs;
474 psymtab = psymtab->objfile_chain) {
475 printf_filtered ("%s at %x, ", psymtab->filename, psymtab);
476 if (psymtab->objfile != objfile)
477 printf_filtered ("NOT ON CHAIN! ");
480 printf_filtered ("\n\n");
483 if (objfile->symtabs) {
484 printf_filtered ("Symtabs:\n");
485 for (symtab = objfile->symtabs;
487 symtab = symtab->objfile_chain) {
488 printf_filtered ("%s at %x, ", symtab->filename, symtab);
489 if (symtab->objfile != objfile)
490 printf_filtered ("NOT ON CHAIN! ");
493 printf_filtered ("\n\n");
497 /* Now check for psymtabs that aren't owned by an objfile. */
500 for (psymtab = partial_symtab_list; psymtab; psymtab = psymtab->next) {
501 for (objfile = object_files; objfile; objfile = objfile->next) {
502 if (psymtab->objfile == objfile)
506 printf_filtered ("Psymtabs that aren't owned by any objfile:\n");
508 printf_filtered (" %s at %x, psymtab->objfile %x\n", psymtab->filename,
509 psymtab, psymtab->objfile);
513 /* Now check for symtabs that aren't owned by an objfile. */
516 for (symtab = symtab_list; symtab; symtab = symtab->next) {
517 for (objfile = object_files; objfile; objfile = objfile->next) {
518 if (symtab->objfile == objfile)
522 printf_filtered ("Symtabs that aren't owned by any objfile:\n");
524 printf_filtered (" %s at %x, symtab->objfile %x\n", symtab->filename,
525 symtab, symtab->objfile);
530 struct cplus_struct_type cplus_struct_default;
533 allocate_cplus_struct_type (type)
536 if (!HAVE_CPLUS_STRUCT (type))
538 int nfields = TYPE_NFIELDS (type);
539 TYPE_CPLUS_SPECIFIC (type) = (struct cplus_struct_type *)
540 obstack_alloc (symbol_obstack, sizeof (struct cplus_struct_type));
541 *(TYPE_CPLUS_SPECIFIC(type)) = cplus_struct_default;
545 /* Increase the space allocated for LISTP. */
548 extend_psymbol_list(listp)
549 register struct psymbol_allocation_list *listp;
552 if (listp->size == 0)
555 listp->list = (struct partial_symbol *)
556 xmalloc (new_size * sizeof (struct partial_symbol));
560 new_size = listp->size * 2;
561 listp->list = (struct partial_symbol *)
562 xrealloc (listp->list, new_size * sizeof (struct partial_symbol));
564 /* Next assumes we only went one over. Should be good if
565 program works correctly */
566 listp->next = listp->list + listp->size;
567 listp->size = new_size;
571 _initialize_symmisc ()
573 symtab_list = (struct symtab *) 0;
574 partial_symtab_list = (struct partial_symtab *) 0;
576 add_com ("printsyms", class_obscure, printsyms_command,
577 "Print dump of current symbol definitions to file OUTFILE.\n\
578 If a SOURCE file is specified, dump only that file's symbols.");
579 add_com ("printpsyms", class_obscure, printpsyms_command,
580 "Print dump of current partial symbol definitions to file OUTFILE.\n\
581 If a SOURCE file is specified, dump only that file's partial symbols.");
582 add_com ("printobjfiles", class_obscure, printobjfiles_command,
583 "Print dump of current object file definitions.");