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 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include "breakpoint.h"
30 static void free_symtab ();
33 /* Free all the symtabs that are currently installed,
34 and all storage associated with them.
35 Leaves us in a consistent state with no symtabs installed. */
40 register struct symtab *s, *snext;
42 /* All values will be invalid because their types will be! */
44 clear_value_history ();
46 clear_internalvars ();
47 #if defined (CLEAR_SOLIB)
50 set_default_breakpoint (0, 0, 0, 0);
52 current_source_symtab = 0;
54 for (s = symtab_list; s; s = snext)
60 obstack_free (symbol_obstack, 0);
61 obstack_init (symbol_obstack);
63 if (misc_function_vector)
64 free (misc_function_vector);
65 misc_function_count = 0;
66 misc_function_vector = 0;
67 clear_pc_function_cache();
70 /* Free a struct block <- B and all the symbols defined in that block. */
78 for (i = 0; i < n; i++)
80 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
81 free (BLOCK_SYM (b, i));
86 /* Free all the storage associated with the struct symtab <- S.
87 Note that some symtabs have contents malloc'ed structure by structure,
88 while some have contents that all live inside one big block of memory,
89 and some share the contents of another symbol table and so you should
90 not free the contents on their behalf (except sometimes the linetable,
91 which maybe per symtab even when the rest is not).
92 It is s->free_code that says which alternative to use. */
96 register struct symtab *s;
99 register struct blockvector *bv;
100 register struct typevector *tv;
102 switch (s->free_code)
105 /* All the contents are part of a big block of memory
106 and some other symtab is in charge of freeing that block.
107 Therefore, do nothing. */
111 /* Here all the contents were malloc'ed structure by structure
112 and must be freed that way. */
113 /* First free the blocks (and their symbols. */
114 bv = BLOCKVECTOR (s);
115 n = BLOCKVECTOR_NBLOCKS (bv);
116 for (i = 0; i < n; i++)
117 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
118 /* Free the blockvector itself. */
120 /* Free the type vector. */
123 /* Also free the linetable. */
126 /* Everything will be freed either by our `free_ptr'
127 or by some other symbatb, except for our linetable.
129 free (LINETABLE (s));
133 /* If there is a single block of memory to free, free it. */
137 /* Free source-related stuff */
139 free (s->line_charpos);
145 /* If a symtab for filename NAME is found, free it along
146 with any dependent breakpoints, displays, etc.
147 Used when loading new versions of object modules with the "add-file"
150 FIXME. I think this is not the right way to do this. It needs further
151 investigation, though. -- gnu@cygnus */
154 free_named_symtab (name)
157 register struct symtab *s;
158 register struct symtab *prev;
159 struct blockvector *bv;
162 /* Look for a symtab with the specified name.
163 We can't use lookup_symtab () for this, since it
164 might generate a recursive call to psymtab_to_symtab (). */
166 for (s = symtab_list; s; s = s->next)
168 if (!strcmp (name, s->filename))
175 if (s == symtab_list)
176 symtab_list = s->next;
178 prev->next = s->next;
180 /* For now, delete all breakpoints, displays, etc., whether or
181 not they depend on the symtab being freed. This should be
182 changed so that only those data structures affected are deleted. */
184 /* But don't delete anything if the symtab is empty.
185 This test is necessary due to a bug in "dbxread.c" that
186 causes empty symtabs to be created for N_SO symbols that
187 contain the pathname of the object file. (This problem
188 has been fixed in GDB 3.9x). */
191 if (BLOCKLIST_NBLOCKS (bv) > 2
192 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, 0))
193 || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, 1)))
195 /* Took the following line out because GDB ends up printing it
196 many times when a given module is loaded, because each module
197 contains many symtabs. */
199 printf ("Clearing breakpoints and resetting debugger state.\n");
202 clear_value_history ();
204 clear_internalvars ();
205 clear_breakpoints ();
206 set_default_breakpoint (0, 0, 0, 0);
207 current_source_symtab = 0;
213 /* It is still possible that some breakpoints will be affected
214 even though no symtab was found, since the file might have
215 been compiled without debugging, and hence not be associated
216 with a symtab. In order to handle this correctly, we would need
217 to keep a list of text address ranges for undebuggable files.
218 For now, we do nothing, since this is a fairly obscure case. */
224 static int block_depth ();
225 static void print_symbol ();
228 print_symtabs (filename)
232 register struct symtab *s;
235 register struct linetable *l;
236 struct blockvector *bv;
237 register struct block *b;
239 struct cleanup *cleanups;
243 error_no_arg ("file to write symbol data in");
245 filename = tilde_expand (filename);
246 make_cleanup (free, filename);
248 outfile = fopen (filename, "w");
250 perror_with_name (filename);
252 cleanups = make_cleanup (fclose, outfile);
255 for (s = symtab_list; s; s = s->next)
257 /* First print the line table. */
258 fprintf (outfile, "Symtab for file %s\n\n", s->filename);
259 fprintf (outfile, "Line table:\n\n");
262 for (i = 0; i < len; i++)
263 fprintf (outfile, " line %d at %x\n", l->item[i].line,
265 /* Now print the block info. */
266 fprintf (outfile, "\nBlockvector:\n\n");
267 bv = BLOCKVECTOR (s);
268 len = BLOCKVECTOR_NBLOCKS (bv);
269 for (i = 0; i < len; i++)
271 b = BLOCKVECTOR_BLOCK (bv, i);
272 depth = block_depth (b) * 2;
273 print_spaces (depth, outfile);
274 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
275 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
276 if (BLOCK_SUPERBLOCK (b))
277 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
278 if (BLOCK_FUNCTION (b))
279 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
280 fputc ('\n', outfile);
281 blen = BLOCK_NSYMS (b);
282 for (j = 0; j < blen; j++)
284 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
288 fprintf (outfile, "\n\n");
292 do_cleanups (cleanups);
296 print_symbol (symbol, depth, outfile)
297 struct symbol *symbol;
301 print_spaces (depth, outfile);
302 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
304 fprintf (outfile, "label %s at 0x%x\n", SYMBOL_NAME (symbol),
305 SYMBOL_VALUE_ADDRESS (symbol));
308 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
310 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
312 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
316 fprintf (outfile, "%s %s = ",
317 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
319 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
320 ? "struct" : "union")),
321 SYMBOL_NAME (symbol));
322 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
324 fprintf (outfile, ";\n");
328 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
329 fprintf (outfile, "typedef ");
330 if (SYMBOL_TYPE (symbol))
332 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
334 fprintf (outfile, "; ");
337 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
339 switch (SYMBOL_CLASS (symbol))
342 fprintf (outfile, "const %ld (0x%lx),",
343 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
346 case LOC_CONST_BYTES:
347 fprintf (outfile, "const %u hex bytes:",
348 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
351 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
352 fprintf (outfile, " %2x",
353 (unsigned)SYMBOL_VALUE_BYTES (symbol) [i]);
354 fprintf (outfile, ",");
359 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
363 fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
367 fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
371 fprintf (outfile, "arg at offset 0x%x from fp,",
372 SYMBOL_VALUE (symbol));
375 fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
379 fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
383 fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
390 fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
394 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
395 SYMBOL_BLOCK_VALUE (symbol),
396 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
400 fprintf (outfile, "external at 0x%x", SYMBOL_VALUE_ADDRESS (symbol));
404 fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
408 fprintf (outfile, "\n");
411 /* Return the nexting depth of a block within other blocks in its symtab. */
418 while (block = BLOCK_SUPERBLOCK (block)) i++;
423 * Free all partial_symtab storage.
428 obstack_free (psymbol_obstack, 0);
429 obstack_init (psymbol_obstack);
430 partial_symtab_list = (struct partial_symtab *) 0;
434 _initialize_symmisc ()
436 symtab_list = (struct symtab *) 0;
437 partial_symtab_list = (struct partial_symtab *) 0;
439 add_com ("printsyms", class_obscure, print_symtabs,
440 "Print dump of current symbol definitions to file OUTFILE.");