1 /* Do various things to symbol tables (other than lookup)), for GDB.
2 Copyright (C) 1986, 1987, 1989 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. */
27 #include "breakpoint.h"
32 /* Free all the symtabs that are currently installed,
33 and all storage associated with them.
34 Leaves us in a consistent state with no symtabs installed. */
39 register struct symtab *s, *snext;
41 /* All values will be invalid because their types will be! */
43 clear_value_history ();
45 clear_internalvars ();
46 #if defined (CLEAR_SOLIB)
49 set_default_breakpoint (0, 0, 0, 0);
51 current_source_symtab = 0;
53 for (s = symtab_list; s; s = snext)
59 obstack_free (symbol_obstack, 0);
60 obstack_init (symbol_obstack);
62 if (misc_function_vector)
63 free (misc_function_vector);
64 misc_function_count = 0;
65 misc_function_vector = 0;
66 clear_pc_function_cache();
69 /* Free a struct block <- B and all the symbols defined in that block. */
77 for (i = 0; i < n; i++)
79 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
80 free (BLOCK_SYM (b, i));
85 /* Free all the storage associated with the struct symtab <- S.
86 Note that some symtabs have contents malloc'ed structure by structure,
87 while some have contents that all live inside one big block of memory,
88 and some share the contents of another symbol table and so you should
89 not free the contents on their behalf (except sometimes the linetable,
90 which maybe per symtab even when the rest is not).
91 It is s->free_code that says which alternative to use. */
95 register struct symtab *s;
98 register struct blockvector *bv;
100 switch (s->free_code)
103 /* All the contents are part of a big block of memory (an obstack),
104 and some other symtab is in charge of freeing that block.
105 Therefore, do nothing. */
109 /* Here all the contents were malloc'ed structure by structure
110 and must be freed that way. */
111 /* First free the blocks (and their symbols. */
112 bv = BLOCKVECTOR (s);
113 n = BLOCKVECTOR_NBLOCKS (bv);
114 for (i = 0; i < n; i++)
115 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
116 /* Free the blockvector itself. */
118 /* Also free the linetable. */
121 /* Everything will be freed either by our `free_ptr'
122 or by some other symbatb, except for our linetable.
124 free (LINETABLE (s));
128 /* If there is a single block of memory to free, free it. */
132 /* Free source-related stuff */
134 free (s->line_charpos);
140 static int block_depth ();
141 static void print_symbol ();
142 static void print_partial_symbol ();
145 print_symtabs (filename)
149 register struct symtab *s;
152 register struct linetable *l;
153 struct blockvector *bv;
154 register struct block *b;
156 struct cleanup *cleanups;
160 error_no_arg ("file to write symbol data in");
162 filename = tilde_expand (filename);
163 make_cleanup (free, filename);
165 outfile = fopen (filename, "w");
167 perror_with_name (filename);
169 cleanups = make_cleanup (fclose, outfile);
172 for (s = symtab_list; s; s = s->next)
174 /* First print the line table. */
175 fprintf (outfile, "Symtab for file %s\n", s->filename);
178 fprintf (outfile, "\nLine table:\n\n");
180 for (i = 0; i < len; i++)
181 fprintf (outfile, " line %d at %x\n", l->item[i].line,
184 /* Now print the block info. */
185 fprintf (outfile, "\nBlockvector:\n\n");
186 bv = BLOCKVECTOR (s);
187 len = BLOCKVECTOR_NBLOCKS (bv);
188 for (i = 0; i < len; i++)
190 b = BLOCKVECTOR_BLOCK (bv, i);
191 depth = block_depth (b) * 2;
192 print_spaces (depth, outfile);
193 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
194 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
195 if (BLOCK_SUPERBLOCK (b))
196 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
197 if (BLOCK_FUNCTION (b))
198 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
199 fputc ('\n', outfile);
200 blen = BLOCK_NSYMS (b);
201 for (j = 0; j < blen; j++)
203 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
207 fprintf (outfile, "\n\n");
211 do_cleanups (cleanups);
215 print_symbol (symbol, depth, outfile)
216 struct symbol *symbol;
220 print_spaces (depth, outfile);
221 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
223 fprintf (outfile, "label %s at 0x%x\n", SYMBOL_NAME (symbol),
224 SYMBOL_VALUE_ADDRESS (symbol));
227 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
229 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
231 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
235 fprintf (outfile, "%s %s = ",
236 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
238 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
239 ? "struct" : "union")),
240 SYMBOL_NAME (symbol));
241 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
243 fprintf (outfile, ";\n");
247 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
248 fprintf (outfile, "typedef ");
249 if (SYMBOL_TYPE (symbol))
251 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
253 fprintf (outfile, "; ");
256 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
258 switch (SYMBOL_CLASS (symbol))
261 fprintf (outfile, "const %ld (0x%lx),",
262 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
265 case LOC_CONST_BYTES:
266 fprintf (outfile, "const %u hex bytes:",
267 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
270 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
271 fprintf (outfile, " %2x",
272 (unsigned)SYMBOL_VALUE_BYTES (symbol) [i]);
273 fprintf (outfile, ",");
278 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
282 fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
286 fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
290 fprintf (outfile, "arg at offset 0x%x from fp,",
291 SYMBOL_VALUE (symbol));
294 fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
298 fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
302 fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
309 fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
313 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
314 SYMBOL_BLOCK_VALUE (symbol),
315 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
319 fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
323 fprintf (outfile, "\n");
327 print_partial_symtabs (filename)
331 struct partial_symtab *p;
332 struct cleanup *cleanups;
336 error_no_arg ("file to write partial symbol data in");
338 filename = tilde_expand (filename);
339 make_cleanup (free, filename);
341 outfile = fopen (filename, "w");
343 perror_with_name (filename);
345 cleanups = make_cleanup (fclose, outfile);
348 for (p = partial_symtab_list; p; p = p->next)
350 fprintf_filtered (outfile, "Partial symtab for source file %s ",
352 fprintf_filtered (outfile, "(object 0x%x)\n\n", p);
353 fprintf_filtered (outfile, " Full symbol table %s been read from %s\n",
354 p->readin ? "has" : "has not yet",
357 fprintf_filtered (outfile, " Was read into symtab at 0x%x by function at 0x%x\n",
358 p->symtab, p->read_symtab);
359 fprintf_filtered (outfile, " Relocate symbols by 0x%x\n", p->addr);
360 fprintf_filtered (outfile, " Symbols cover text addresses 0x%x-0x%x\n",
361 p->textlow, p->texthigh);
362 fprintf_filtered (outfile, " Depends on %d other partial symtabs.\n",
363 p->number_of_dependencies);
364 if (p->n_global_syms > 0)
365 print_partial_symbol (global_psymbols.list + p->globals_offset,
366 p->n_global_syms, "Global", outfile);
367 if (p->n_static_syms > 0)
368 print_partial_symbol (static_psymbols.list + p->statics_offset,
369 p->n_static_syms, "Static", outfile);
370 fprintf_filtered (outfile, "\n\n");
374 do_cleanups (cleanups);
378 print_partial_symbol (p, count, what, outfile)
379 struct partial_symbol *p;
387 fprintf_filtered (outfile, " %s partial symbols:\n", what);
390 fprintf_filtered (outfile, " `%s', ", SYMBOL_NAME(p));
391 switch (SYMBOL_NAMESPACE (p))
393 case UNDEF_NAMESPACE:
394 fputs_filtered ("undefined namespace, ", outfile);
397 /* This is the usual thing -- don't print it */
399 case STRUCT_NAMESPACE:
400 fputs_filtered ("struct namespace, ", outfile);
402 case LABEL_NAMESPACE:
403 fputs_filtered ("label namespace, ", outfile);
406 fputs_filtered ("<invalid namespace>, ", outfile);
409 switch (SYMBOL_CLASS (p))
412 fputs_filtered ("undefined", outfile);
415 fputs_filtered ("constant int", outfile);
418 fputs_filtered ("static", outfile);
421 fputs_filtered ("register", outfile);
424 fputs_filtered ("pass by value", outfile);
427 fputs_filtered ("pass by reference", outfile);
430 fputs_filtered ("register parameter", outfile);
433 fputs_filtered ("stack parameter", outfile);
436 fputs_filtered ("type", outfile);
439 fputs_filtered ("label", outfile);
442 fputs_filtered ("function", outfile);
444 case LOC_CONST_BYTES:
445 fputs_filtered ("constant bytes", outfile);
448 fputs_filtered ("shuffled arg", outfile);
451 fputs_filtered ("<invalid location>", outfile);
454 fputs_filtered (", ", outfile);
455 fprintf_filtered (outfile, "0x%x\n", SYMBOL_VALUE (p));
460 /* Return the nexting depth of a block within other blocks in its symtab. */
467 while (block = BLOCK_SUPERBLOCK (block)) i++;
472 * Free all partial_symtab storage.
477 obstack_free (psymbol_obstack, 0);
478 obstack_init (psymbol_obstack);
479 partial_symtab_list = (struct partial_symtab *) 0;
483 _initialize_symmisc ()
485 symtab_list = (struct symtab *) 0;
486 partial_symtab_list = (struct partial_symtab *) 0;
488 add_com ("printsyms", class_obscure, print_symtabs,
489 "Print dump of current symbol definitions to file OUTFILE.");
490 add_com ("printpsyms", class_obscure, print_partial_symtabs,
491 "Print dump of current partial symbol definitions to file OUTFILE.");