1 /* Do various things to symbol tables (other than lookup)), for GDB.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
23 #include "initialize.h"
29 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 ();
48 set_default_breakpoint (0, 0, 0, 0);
50 current_source_symtab = 0;
52 for (s = symtab_list; s; s = snext)
58 obstack_free (symbol_obstack, 0);
59 obstack_init (symbol_obstack);
61 if (misc_function_vector)
62 free (misc_function_vector);
63 misc_function_count = 0;
64 misc_function_vector = 0;
67 /* Free a struct block <- B and all the symbols defined in that block. */
75 for (i = 0; i < n; i++)
77 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
78 free (BLOCK_SYM (b, i));
83 /* Free all the storage associated with the struct symtab <- S.
84 Note that some symtabs have contents malloc'ed structure by structure,
85 while some have contents that all live inside one big block of memory,
86 and some share the contents of another symbol table and so you should
87 not free the contents on their behalf (except sometimes the linetable,
88 which maybe per symtab even when the rest is not).
89 It is s->free_code that says which alternative to use. */
93 register struct symtab *s;
96 register struct blockvector *bv;
97 register struct type *type;
98 register struct typevector *tv;
100 switch (s->free_code)
103 /* All the contents are part of a big block of memory
104 and some other symtab is in charge of freeing that block.
105 Therefore, do nothing. */
109 /* All the contents are part of a big block of memory
110 and that is our `free_ptr' and will be freed below. */
114 /* Here all the contents were malloc'ed structure by structure
115 and must be freed that way. */
116 /* First free the blocks (and their symbols. */
117 bv = BLOCKVECTOR (s);
118 n = BLOCKVECTOR_NBLOCKS (bv);
119 for (i = 0; i < n; i++)
120 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
121 /* Free the blockvector itself. */
123 /* Free the type vector. */
125 if (tv) /* FIXME, should this happen? It does... */
127 /* Also free the linetable. */
130 /* Everything will be freed either by our `free_ptr'
131 or by some other symbatb, except for our linetable.
133 free (LINETABLE (s));
137 /* If there is a single block of memory to free, free it. */
142 free (s->line_charpos);
147 /* Convert a raw symbol-segment to a struct symtab,
148 and relocate its internal pointers so that it is valid. */
150 /* This is how to relocate one pointer, given a name for it.
151 Works independent of the type of object pointed to. */
152 #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
154 /* This is the inverse of RELOCATE. We use it when storing
155 a core address into a slot that has yet to be relocated. */
156 #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
158 /* During the process of relocation, this holds the amount to relocate by
159 (the address of the file's symtab data, in core in the debugger). */
160 static int relocation;
162 #define CORE_RELOCATE(slot) \
163 ((slot) += (((slot) < data_start) ? text_relocation \
164 : ((slot) < bss_start) ? data_relocation : bss_relocation))
166 #define TEXT_RELOCATE(slot) ((slot) += text_relocation)
168 /* Relocation amounts for addresses in the program's core image. */
169 static int text_relocation, data_relocation, bss_relocation;
171 /* Boundaries that divide program core addresses into text, data and bss;
172 used to determine which relocation amount to use. */
173 static int data_start, bss_start;
175 static void relocate_typevector ();
176 static void relocate_blockvector ();
177 static void relocate_type ();
178 static void relocate_block ();
179 static void relocate_symbol ();
181 /* Relocate a file symbol table so that all the pointers
182 are valid C pointers. Pass the struct symtab for the file
183 and the amount to relocate by. */
185 static struct symtab *
186 relocate_symtab (root)
187 struct symbol_root *root;
189 struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
190 bzero (sp, sizeof (struct symtab));
192 relocation = (int) root;
193 text_relocation = root->textrel;
194 data_relocation = root->datarel;
195 bss_relocation = root->bssrel;
196 data_start = root->databeg;
197 bss_start = root->bssbeg;
199 sp->filename = root->filename;
200 sp->ldsymoff = root->ldsymoff;
201 sp->language = root->language;
202 sp->compilation = root->compilation;
203 sp->version = root->version;
204 sp->blockvector = root->blockvector;
205 sp->typevector = root->typevector;
206 sp->free_code = free_explicit;
207 sp->free_ptr = (char *) root;
209 RELOCATE (TYPEVECTOR (sp));
210 RELOCATE (BLOCKVECTOR (sp));
211 RELOCATE (sp->version);
212 RELOCATE (sp->compilation);
213 RELOCATE (sp->filename);
215 relocate_typevector (TYPEVECTOR (sp));
216 relocate_blockvector (BLOCKVECTOR (sp));
222 relocate_typevector (tv)
223 struct typevector *tv;
225 register int ntypes = TYPEVECTOR_NTYPES (tv);
228 for (i = 0; i < ntypes; i++)
229 RELOCATE (TYPEVECTOR_TYPE (tv, i));
230 for (i = 0; i < ntypes; i++)
231 relocate_type (TYPEVECTOR_TYPE (tv, i));
235 relocate_blockvector (blp)
236 register struct blockvector *blp;
238 register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
240 for (i = 0; i < nblocks; i++)
241 RELOCATE (BLOCKVECTOR_BLOCK (blp, i));
242 for (i = 0; i < nblocks; i++)
243 relocate_block (BLOCKVECTOR_BLOCK (blp, i));
248 register struct block *bp;
250 register int nsyms = BLOCK_NSYMS (bp);
253 TEXT_RELOCATE (BLOCK_START (bp));
254 TEXT_RELOCATE (BLOCK_END (bp));
256 /* These two should not be recursively processed.
257 The superblock need not be because all blocks are
258 processed from relocate_blockvector.
259 The function need not be because it will be processed
260 under the block which is its scope. */
261 RELOCATE (BLOCK_SUPERBLOCK (bp));
262 RELOCATE (BLOCK_FUNCTION (bp));
264 for (i = 0; i < nsyms; i++)
265 RELOCATE (BLOCK_SYM (bp, i));
267 for (i = 0; i < nsyms; i++)
268 relocate_symbol (BLOCK_SYM (bp, i));
273 register struct symbol *sp;
275 RELOCATE (SYMBOL_NAME (sp));
276 if (SYMBOL_CLASS (sp) == LOC_BLOCK)
278 RELOCATE (SYMBOL_BLOCK_VALUE (sp));
279 /* We can assume the block that belongs to this symbol
280 is not relocated yet, since it comes after
281 the block that contains this symbol. */
282 BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)) = sp;
283 UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)));
285 else if (SYMBOL_CLASS (sp) == LOC_STATIC)
286 CORE_RELOCATE (SYMBOL_VALUE (sp));
287 else if (SYMBOL_CLASS (sp) == LOC_LABEL)
288 TEXT_RELOCATE (SYMBOL_VALUE (sp));
289 RELOCATE (SYMBOL_TYPE (sp));
292 /* We cannot come up with an a priori spanning tree
293 for the network of types, since types can be used
294 for many symbols and also as components of other types.
295 Therefore, we need to be able to mark types that we
296 already have relocated (or are already in the middle of relocating)
297 as in a garbage collector. */
301 register struct type *tp;
303 register int nfields = TYPE_NFIELDS (tp);
306 RELOCATE (TYPE_NAME (tp));
307 RELOCATE (TYPE_TARGET_TYPE (tp));
308 RELOCATE (TYPE_FIELDS (tp));
309 RELOCATE (TYPE_POINTER_TYPE (tp));
311 for (i = 0; i < nfields; i++)
313 RELOCATE (TYPE_FIELD_TYPE (tp, i));
314 RELOCATE (TYPE_FIELD_NAME (tp, i));
318 /* Read symsegs from file named NAME open on DESC,
319 make symtabs from them, and return a chain of them.
320 Assumes DESC is prepositioned at the end of the string table,
321 just before the symsegs if there are any. */
324 read_symsegs (desc, name)
328 struct symbol_root root;
330 register struct symtab *sp, *chain = 0;
335 len = myread (desc, &root, sizeof root);
336 if (len == 0 || root.format == 0)
338 if (root.format != 1 ||
339 root.length < sizeof root)
340 error ("Invalid symbol segment format code");
341 data = (char *) xmalloc (root.length);
342 bcopy (&root, data, sizeof root);
343 len = myread (desc, data + sizeof root,
344 root.length - sizeof root);
345 sp = relocate_symtab (data);
353 static int block_depth ();
354 static void print_spaces ();
355 static void print_symbol ();
357 print_symtabs (filename)
361 register struct symtab *s;
364 register struct linetable *l;
365 struct blockvector *bv;
366 register struct block *b;
368 struct cleanup *cleanups;
372 error_no_arg ("file to write symbol data in");
373 outfile = fopen (filename, "w");
375 cleanups = make_cleanup (fclose, outfile);
378 for (s = symtab_list; s; s = s->next)
380 /* First print the line table. */
381 fprintf (outfile, "Symtab for file %s\n\n", s->filename);
382 fprintf (outfile, "Line table:\n\n");
385 for (i = 0; i < len; i++)
388 line = - l->item[i] - 1;
390 fprintf (outfile, " line %d at %x\n", ++line, l->item[i]);
392 /* Now print the block info. */
393 fprintf (outfile, "\nBlockvector:\n\n");
394 bv = BLOCKVECTOR (s);
395 len = BLOCKVECTOR_NBLOCKS (bv);
396 for (i = 0; i < len; i++)
398 b = BLOCKVECTOR_BLOCK (bv, i);
399 depth = block_depth (b) * 2;
400 print_spaces (depth, outfile);
401 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
402 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
403 if (BLOCK_SUPERBLOCK (b))
404 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
405 if (BLOCK_FUNCTION (b))
406 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
407 fputc ('\n', outfile);
408 blen = BLOCK_NSYMS (b);
409 for (j = 0; j < blen; j++)
411 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
415 fprintf (outfile, "\n\n");
419 do_cleanups (cleanups);
423 print_symbol (symbol, depth, outfile)
424 struct symbol *symbol;
428 print_spaces (depth, outfile);
429 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
431 fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
432 SYMBOL_VALUE (symbol));
435 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
437 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
439 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
443 fprintf (outfile, "%s %s = ",
444 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
446 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
447 ? "struct" : "union")),
448 SYMBOL_NAME (symbol));
449 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
451 fprintf (outfile, ";\n");
455 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
456 fprintf (outfile, "typedef ");
457 if (SYMBOL_TYPE (symbol))
459 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
461 fprintf (outfile, "; ");
464 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
466 switch (SYMBOL_CLASS (symbol))
469 fprintf (outfile, "const %d (0x%x),",
470 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
473 case LOC_CONST_BYTES:
474 fprintf (outfile, "const %d hex bytes:",
475 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
478 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
479 fprintf (outfile, " %2x", SYMBOL_VALUE_BYTES (symbol) [i]);
480 fprintf (outfile, ",");
485 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
489 fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
493 fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
497 fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
504 fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
508 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
509 SYMBOL_VALUE (symbol),
510 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
514 fprintf (outfile, "\n");
517 /* Return the nexting depth of a block within other blocks in its symtab. */
524 while (block = BLOCK_SUPERBLOCK (block)) i++;
531 add_com ("printsyms", class_obscure, print_symtabs,
532 "Print dump of current symbol definitions to file OUTFILE.");