gdb-2.4+.aux.coff
[deliverable/binutils-gdb.git] / gdb / RCS / symmisc.c,v
1 head 1.2;
2 access ;
3 symbols RMS-has:1.2;
4 locks ; strict;
5 comment @ * @;
6
7
8 1.2
9 date 88.01.26.05.09.53; author gnu; state Exp;
10 branches ;
11 next 1.1;
12
13 1.1
14 date 88.01.26.04.25.22; author gnu; state Exp;
15 branches ;
16 next ;
17
18
19 desc
20 @Original from RMS's devl sources on wheaties
21 @
22
23
24 1.2
25 log
26 @Check for null pointer passed to free()...
27 @
28 text
29 @/* Do various things to symbol tables (other than lookup)), for GDB.
30 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
31
32 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
33 WARRANTY. No author or distributor accepts responsibility to anyone
34 for the consequences of using it or for whether it serves any
35 particular purpose or works at all, unless he says so in writing.
36 Refer to the GDB General Public License for full details.
37
38 Everyone is granted permission to copy, modify and redistribute GDB,
39 but only under the conditions described in the GDB General Public
40 License. A copy of this license is supposed to have been given to you
41 along with GDB so you can know your rights and responsibilities. It
42 should be in a file named COPYING. Among other things, the copyright
43 notice and this notice must be preserved on all copies.
44
45 In other words, go ahead and share GDB, but don't try to stop
46 anyone else from sharing it farther. Help stamp out software hoarding!
47 */
48
49
50 #include "defs.h"
51 #include "initialize.h"
52 #include "symtab.h"
53
54 #include <stdio.h>
55 #include <obstack.h>
56
57 static void free_symtab ();
58
59 START_FILE
60 \f
61 /* Free all the symtabs that are currently installed,
62 and all storage associated with them.
63 Leaves us in a consistent state with no symtabs installed. */
64
65 void
66 free_all_symtabs ()
67 {
68 register struct symtab *s, *snext;
69
70 /* All values will be invalid because their types will be! */
71
72 clear_value_history ();
73 clear_displays ();
74 clear_internalvars ();
75 clear_breakpoints ();
76 set_default_breakpoint (0, 0, 0, 0);
77
78 current_source_symtab = 0;
79
80 for (s = symtab_list; s; s = snext)
81 {
82 snext = s->next;
83 free_symtab (s);
84 }
85 symtab_list = 0;
86 obstack_free (symbol_obstack, 0);
87 obstack_init (symbol_obstack);
88
89 if (misc_function_vector)
90 free (misc_function_vector);
91 misc_function_count = 0;
92 misc_function_vector = 0;
93 }
94
95 /* Free a struct block <- B and all the symbols defined in that block. */
96
97 static void
98 free_symtab_block (b)
99 struct block *b;
100 {
101 register int i, n;
102 n = BLOCK_NSYMS (b);
103 for (i = 0; i < n; i++)
104 {
105 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
106 free (BLOCK_SYM (b, i));
107 }
108 free (b);
109 }
110
111 /* Free all the storage associated with the struct symtab <- S.
112 Note that some symtabs have contents malloc'ed structure by structure,
113 while some have contents that all live inside one big block of memory,
114 and some share the contents of another symbol table and so you should
115 not free the contents on their behalf (except sometimes the linetable,
116 which maybe per symtab even when the rest is not).
117 It is s->free_code that says which alternative to use. */
118
119 static void
120 free_symtab (s)
121 register struct symtab *s;
122 {
123 register int i, n;
124 register struct blockvector *bv;
125 register struct type *type;
126 register struct typevector *tv;
127
128 switch (s->free_code)
129 {
130 case free_nothing:
131 /* All the contents are part of a big block of memory
132 and some other symtab is in charge of freeing that block.
133 Therefore, do nothing. */
134 break;
135
136 case free_explicit:
137 /* All the contents are part of a big block of memory
138 and that is our `free_ptr' and will be freed below. */
139 break;
140
141 case free_contents:
142 /* Here all the contents were malloc'ed structure by structure
143 and must be freed that way. */
144 /* First free the blocks (and their symbols. */
145 bv = BLOCKVECTOR (s);
146 n = BLOCKVECTOR_NBLOCKS (bv);
147 for (i = 0; i < n; i++)
148 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
149 /* Free the blockvector itself. */
150 free (bv);
151 /* Free the type vector. */
152 tv = TYPEVECTOR (s);
153 if (tv) /* FIXME, should this happen? It does... */
154 free (tv);
155 /* Also free the linetable. */
156
157 case free_linetable:
158 /* Everything will be freed either by our `free_ptr'
159 or by some other symbatb, except for our linetable.
160 Free that now. */
161 free (LINETABLE (s));
162 break;
163 }
164
165 /* If there is a single block of memory to free, free it. */
166 if (s->free_ptr)
167 free (s->free_ptr);
168
169 if (s->line_charpos)
170 free (s->line_charpos);
171 free (s->filename);
172 free (s);
173 }
174 \f
175 /* Convert a raw symbol-segment to a struct symtab,
176 and relocate its internal pointers so that it is valid. */
177
178 /* This is how to relocate one pointer, given a name for it.
179 Works independent of the type of object pointed to. */
180 #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
181
182 /* This is the inverse of RELOCATE. We use it when storing
183 a core address into a slot that has yet to be relocated. */
184 #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
185
186 /* During the process of relocation, this holds the amount to relocate by
187 (the address of the file's symtab data, in core in the debugger). */
188 static int relocation;
189
190 #define CORE_RELOCATE(slot) \
191 ((slot) += (((slot) < data_start) ? text_relocation \
192 : ((slot) < bss_start) ? data_relocation : bss_relocation))
193
194 #define TEXT_RELOCATE(slot) ((slot) += text_relocation)
195
196 /* Relocation amounts for addresses in the program's core image. */
197 static int text_relocation, data_relocation, bss_relocation;
198
199 /* Boundaries that divide program core addresses into text, data and bss;
200 used to determine which relocation amount to use. */
201 static int data_start, bss_start;
202
203 static void relocate_typevector ();
204 static void relocate_blockvector ();
205 static void relocate_type ();
206 static void relocate_block ();
207 static void relocate_symbol ();
208
209 /* Relocate a file symbol table so that all the pointers
210 are valid C pointers. Pass the struct symtab for the file
211 and the amount to relocate by. */
212
213 static struct symtab *
214 relocate_symtab (root)
215 struct symbol_root *root;
216 {
217 struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
218 bzero (sp, sizeof (struct symtab));
219
220 relocation = (int) root;
221 text_relocation = root->textrel;
222 data_relocation = root->datarel;
223 bss_relocation = root->bssrel;
224 data_start = root->databeg;
225 bss_start = root->bssbeg;
226
227 sp->filename = root->filename;
228 sp->ldsymoff = root->ldsymoff;
229 sp->language = root->language;
230 sp->compilation = root->compilation;
231 sp->version = root->version;
232 sp->blockvector = root->blockvector;
233 sp->typevector = root->typevector;
234 sp->free_code = free_explicit;
235 sp->free_ptr = (char *) root;
236
237 RELOCATE (TYPEVECTOR (sp));
238 RELOCATE (BLOCKVECTOR (sp));
239 RELOCATE (sp->version);
240 RELOCATE (sp->compilation);
241 RELOCATE (sp->filename);
242
243 relocate_typevector (TYPEVECTOR (sp));
244 relocate_blockvector (BLOCKVECTOR (sp));
245
246 return sp;
247 }
248
249 static void
250 relocate_typevector (tv)
251 struct typevector *tv;
252 {
253 register int ntypes = TYPEVECTOR_NTYPES (tv);
254 register int i;
255
256 for (i = 0; i < ntypes; i++)
257 RELOCATE (TYPEVECTOR_TYPE (tv, i));
258 for (i = 0; i < ntypes; i++)
259 relocate_type (TYPEVECTOR_TYPE (tv, i));
260 }
261
262 static void
263 relocate_blockvector (blp)
264 register struct blockvector *blp;
265 {
266 register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
267 register int i;
268 for (i = 0; i < nblocks; i++)
269 RELOCATE (BLOCKVECTOR_BLOCK (blp, i));
270 for (i = 0; i < nblocks; i++)
271 relocate_block (BLOCKVECTOR_BLOCK (blp, i));
272 }
273
274 static void
275 relocate_block (bp)
276 register struct block *bp;
277 {
278 register int nsyms = BLOCK_NSYMS (bp);
279 register int i;
280
281 TEXT_RELOCATE (BLOCK_START (bp));
282 TEXT_RELOCATE (BLOCK_END (bp));
283
284 /* These two should not be recursively processed.
285 The superblock need not be because all blocks are
286 processed from relocate_blockvector.
287 The function need not be because it will be processed
288 under the block which is its scope. */
289 RELOCATE (BLOCK_SUPERBLOCK (bp));
290 RELOCATE (BLOCK_FUNCTION (bp));
291
292 for (i = 0; i < nsyms; i++)
293 RELOCATE (BLOCK_SYM (bp, i));
294
295 for (i = 0; i < nsyms; i++)
296 relocate_symbol (BLOCK_SYM (bp, i));
297 }
298
299 static void
300 relocate_symbol (sp)
301 register struct symbol *sp;
302 {
303 RELOCATE (SYMBOL_NAME (sp));
304 if (SYMBOL_CLASS (sp) == LOC_BLOCK)
305 {
306 RELOCATE (SYMBOL_BLOCK_VALUE (sp));
307 /* We can assume the block that belongs to this symbol
308 is not relocated yet, since it comes after
309 the block that contains this symbol. */
310 BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)) = sp;
311 UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)));
312 }
313 else if (SYMBOL_CLASS (sp) == LOC_STATIC)
314 CORE_RELOCATE (SYMBOL_VALUE (sp));
315 else if (SYMBOL_CLASS (sp) == LOC_LABEL)
316 TEXT_RELOCATE (SYMBOL_VALUE (sp));
317 RELOCATE (SYMBOL_TYPE (sp));
318 }
319
320 /* We cannot come up with an a priori spanning tree
321 for the network of types, since types can be used
322 for many symbols and also as components of other types.
323 Therefore, we need to be able to mark types that we
324 already have relocated (or are already in the middle of relocating)
325 as in a garbage collector. */
326
327 static void
328 relocate_type (tp)
329 register struct type *tp;
330 {
331 register int nfields = TYPE_NFIELDS (tp);
332 register int i;
333
334 RELOCATE (TYPE_NAME (tp));
335 RELOCATE (TYPE_TARGET_TYPE (tp));
336 RELOCATE (TYPE_FIELDS (tp));
337 RELOCATE (TYPE_POINTER_TYPE (tp));
338
339 for (i = 0; i < nfields; i++)
340 {
341 RELOCATE (TYPE_FIELD_TYPE (tp, i));
342 RELOCATE (TYPE_FIELD_NAME (tp, i));
343 }
344 }
345 \f
346 /* Read symsegs from file named NAME open on DESC,
347 make symtabs from them, and return a chain of them.
348 Assumes DESC is prepositioned at the end of the string table,
349 just before the symsegs if there are any. */
350
351 struct symtab *
352 read_symsegs (desc, name)
353 int desc;
354 char *name;
355 {
356 struct symbol_root root;
357 register char *data;
358 register struct symtab *sp, *chain = 0;
359 register int len;
360
361 while (1)
362 {
363 len = myread (desc, &root, sizeof root);
364 if (len == 0 || root.format == 0)
365 break;
366 if (root.format != 1 ||
367 root.length < sizeof root)
368 error ("Invalid symbol segment format code");
369 data = (char *) xmalloc (root.length);
370 bcopy (&root, data, sizeof root);
371 len = myread (desc, data + sizeof root,
372 root.length - sizeof root);
373 sp = relocate_symtab (data);
374 sp->next = chain;
375 chain = sp;
376 }
377
378 return chain;
379 }
380 \f
381 static int block_depth ();
382 static void print_spaces ();
383 static void print_symbol ();
384
385 print_symtabs (filename)
386 char *filename;
387 {
388 FILE *outfile;
389 register struct symtab *s;
390 register int i, j;
391 int len, line, blen;
392 register struct linetable *l;
393 struct blockvector *bv;
394 register struct block *b;
395 int depth;
396 struct cleanup *cleanups;
397 extern int fclose();
398
399 if (filename == 0)
400 error_no_arg ("file to write symbol data in");
401 outfile = fopen (filename, "w");
402
403 cleanups = make_cleanup (fclose, outfile);
404 immediate_quit++;
405
406 for (s = symtab_list; s; s = s->next)
407 {
408 /* First print the line table. */
409 fprintf (outfile, "Symtab for file %s\n\n", s->filename);
410 fprintf (outfile, "Line table:\n\n");
411 l = LINETABLE (s);
412 len = l->nitems;
413 for (i = 0; i < len; i++)
414 {
415 if (l->item[i] < 0)
416 line = - l->item[i] - 1;
417 else
418 fprintf (outfile, " line %d at %x\n", ++line, l->item[i]);
419 }
420 /* Now print the block info. */
421 fprintf (outfile, "\nBlockvector:\n\n");
422 bv = BLOCKVECTOR (s);
423 len = BLOCKVECTOR_NBLOCKS (bv);
424 for (i = 0; i < len; i++)
425 {
426 b = BLOCKVECTOR_BLOCK (bv, i);
427 depth = block_depth (b) * 2;
428 print_spaces (depth, outfile);
429 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
430 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
431 if (BLOCK_SUPERBLOCK (b))
432 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
433 if (BLOCK_FUNCTION (b))
434 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
435 fputc ('\n', outfile);
436 blen = BLOCK_NSYMS (b);
437 for (j = 0; j < blen; j++)
438 {
439 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
440 }
441 }
442
443 fprintf (outfile, "\n\n");
444 }
445
446 immediate_quit--;
447 do_cleanups (cleanups);
448 }
449
450 static void
451 print_symbol (symbol, depth, outfile)
452 struct symbol *symbol;
453 int depth;
454 FILE *outfile;
455 {
456 print_spaces (depth, outfile);
457 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
458 {
459 fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
460 SYMBOL_VALUE (symbol));
461 return;
462 }
463 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
464 {
465 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
466 {
467 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
468 }
469 else
470 {
471 fprintf (outfile, "%s %s = ",
472 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
473 ? "enum"
474 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
475 ? "struct" : "union")),
476 SYMBOL_NAME (symbol));
477 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
478 }
479 fprintf (outfile, ";\n");
480 }
481 else
482 {
483 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
484 fprintf (outfile, "typedef ");
485 if (SYMBOL_TYPE (symbol))
486 {
487 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
488 outfile, 1, depth);
489 fprintf (outfile, "; ");
490 }
491 else
492 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
493
494 switch (SYMBOL_CLASS (symbol))
495 {
496 case LOC_CONST:
497 fprintf (outfile, "const %d (0x%x),",
498 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
499 break;
500
501 case LOC_CONST_BYTES:
502 fprintf (outfile, "const %d hex bytes:",
503 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
504 {
505 int i;
506 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
507 fprintf (outfile, " %2x", SYMBOL_VALUE_BYTES (symbol) [i]);
508 fprintf (outfile, ",");
509 }
510 break;
511
512 case LOC_STATIC:
513 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
514 break;
515
516 case LOC_REGISTER:
517 fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
518 break;
519
520 case LOC_ARG:
521 fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
522 break;
523
524 case LOC_LOCAL:
525 fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
526 break;
527
528 case LOC_TYPEDEF:
529 break;
530
531 case LOC_LABEL:
532 fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
533 break;
534
535 case LOC_BLOCK:
536 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
537 SYMBOL_VALUE (symbol),
538 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
539 break;
540 }
541 }
542 fprintf (outfile, "\n");
543 }
544
545 /* Return the nexting depth of a block within other blocks in its symtab. */
546
547 static int
548 block_depth (block)
549 struct block *block;
550 {
551 register int i = 0;
552 while (block = BLOCK_SUPERBLOCK (block)) i++;
553 return i;
554 }
555 \f
556 static
557 initialize ()
558 {
559 add_com ("printsyms", class_obscure, print_symtabs,
560 "Print dump of current symbol definitions to file OUTFILE.");
561 }
562
563 END_FILE
564 @
565
566
567 1.1
568 log
569 @Initial revision
570 @
571 text
572 @d125 2
573 a126 1
574 free (tv);
575 @
This page took 0.045307 seconds and 4 git commands to generate.