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!
28 static void free_symtab ();
31 /* Free all the symtabs that are currently installed,
32 and all storage associated with them.
33 Leaves us in a consistent state with no symtabs installed. */
38 register struct symtab *s, *snext;
40 /* All values will be invalid because their types will be! */
42 clear_value_history ();
44 clear_internalvars ();
46 set_default_breakpoint (0, 0, 0, 0);
48 current_source_symtab = 0;
50 for (s = symtab_list; s; s = snext)
56 obstack_free (symbol_obstack, 0);
57 obstack_init (symbol_obstack);
59 if (misc_function_vector)
60 free (misc_function_vector);
61 misc_function_count = 0;
62 misc_function_vector = 0;
65 /* Free a struct block <- B and all the symbols defined in that block. */
73 for (i = 0; i < n; i++)
75 free (SYMBOL_NAME (BLOCK_SYM (b, i)));
76 free (BLOCK_SYM (b, i));
81 /* Free all the storage associated with the struct symtab <- S.
82 Note that some symtabs have contents malloc'ed structure by structure,
83 while some have contents that all live inside one big block of memory,
84 and some share the contents of another symbol table and so you should
85 not free the contents on their behalf (except sometimes the linetable,
86 which maybe per symtab even when the rest is not).
87 It is s->free_code that says which alternative to use. */
91 register struct symtab *s;
94 register struct blockvector *bv;
95 register struct type *type;
96 register struct typevector *tv;
101 /* All the contents are part of a big block of memory
102 and some other symtab is in charge of freeing that block.
103 Therefore, do nothing. */
107 /* Here all the contents were malloc'ed structure by structure
108 and must be freed that way. */
109 /* First free the blocks (and their symbols. */
110 bv = BLOCKVECTOR (s);
111 n = BLOCKVECTOR_NBLOCKS (bv);
112 for (i = 0; i < n; i++)
113 free_symtab_block (BLOCKVECTOR_BLOCK (bv, i));
114 /* Free the blockvector itself. */
116 /* Free the type vector. */
119 /* Also free the linetable. */
122 /* Everything will be freed either by our `free_ptr'
123 or by some other symbatb, except for our linetable.
125 free (LINETABLE (s));
129 /* If there is a single block of memory to free, free it. */
134 free (s->line_charpos);
139 /* Convert a raw symbol-segment to a struct symtab,
140 and relocate its internal pointers so that it is valid. */
142 /* This is how to relocate one pointer, given a name for it.
143 Works independent of the type of object pointed to. */
144 #define RELOCATE(slot) (slot ? (* (char **) &slot += relocation) : 0)
146 /* This is the inverse of RELOCATE. We use it when storing
147 a core address into a slot that has yet to be relocated. */
148 #define UNRELOCATE(slot) (slot ? (* (char **) &slot -= relocation) : 0)
150 /* During the process of relocation, this holds the amount to relocate by
151 (the address of the file's symtab data, in core in the debugger). */
152 static int relocation;
154 #define CORE_RELOCATE(slot) \
155 ((slot) += (((slot) < data_start) ? text_relocation \
156 : ((slot) < bss_start) ? data_relocation : bss_relocation))
158 #define TEXT_RELOCATE(slot) ((slot) += text_relocation)
160 /* Relocation amounts for addresses in the program's core image. */
161 static int text_relocation, data_relocation, bss_relocation;
163 /* Boundaries that divide program core addresses into text, data and bss;
164 used to determine which relocation amount to use. */
165 static int data_start, bss_start;
167 static void relocate_typevector ();
168 static void relocate_blockvector ();
169 static void relocate_type ();
170 static void relocate_block ();
171 static void relocate_symbol ();
172 static void relocate_source ();
174 /* Relocate a file's symseg so that all the pointers are valid C pointers.
175 Value is a `struct symtab'; but it is not suitable for direct
176 insertion into the `symtab_list' because it describes several files. */
178 static struct symtab *
179 relocate_symtab (root)
180 struct symbol_root *root;
182 struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
183 bzero (sp, sizeof (struct symtab));
185 relocation = (int) root;
186 text_relocation = root->textrel;
187 data_relocation = root->datarel;
188 bss_relocation = root->bssrel;
189 data_start = root->databeg;
190 bss_start = root->bssbeg;
192 sp->filename = root->filename;
193 sp->ldsymoff = root->ldsymoff;
194 sp->language = root->language;
195 sp->compilation = root->compilation;
196 sp->version = root->version;
197 sp->blockvector = root->blockvector;
198 sp->typevector = root->typevector;
200 RELOCATE (TYPEVECTOR (sp));
201 RELOCATE (BLOCKVECTOR (sp));
202 RELOCATE (sp->version);
203 RELOCATE (sp->compilation);
204 RELOCATE (sp->filename);
206 relocate_typevector (TYPEVECTOR (sp));
207 relocate_blockvector (BLOCKVECTOR (sp));
213 relocate_blockvector (blp)
214 register struct blockvector *blp;
216 register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
218 for (i = 0; i < nblocks; i++)
219 RELOCATE (BLOCKVECTOR_BLOCK (blp, i));
220 for (i = 0; i < nblocks; i++)
221 relocate_block (BLOCKVECTOR_BLOCK (blp, i));
226 register struct block *bp;
228 register int nsyms = BLOCK_NSYMS (bp);
231 TEXT_RELOCATE (BLOCK_START (bp));
232 TEXT_RELOCATE (BLOCK_END (bp));
234 /* These two should not be recursively processed.
235 The superblock need not be because all blocks are
236 processed from relocate_blockvector.
237 The function need not be because it will be processed
238 under the block which is its scope. */
239 RELOCATE (BLOCK_SUPERBLOCK (bp));
240 RELOCATE (BLOCK_FUNCTION (bp));
242 for (i = 0; i < nsyms; i++)
243 RELOCATE (BLOCK_SYM (bp, i));
245 for (i = 0; i < nsyms; i++)
246 relocate_symbol (BLOCK_SYM (bp, i));
251 register struct symbol *sp;
253 RELOCATE (SYMBOL_NAME (sp));
254 if (SYMBOL_CLASS (sp) == LOC_BLOCK)
256 RELOCATE (SYMBOL_BLOCK_VALUE (sp));
257 /* We can assume the block that belongs to this symbol
258 is not relocated yet, since it comes after
259 the block that contains this symbol. */
260 BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)) = sp;
261 UNRELOCATE (BLOCK_FUNCTION (SYMBOL_BLOCK_VALUE (sp)));
263 else if (SYMBOL_CLASS (sp) == LOC_STATIC)
264 CORE_RELOCATE (SYMBOL_VALUE (sp));
265 else if (SYMBOL_CLASS (sp) == LOC_LABEL)
266 TEXT_RELOCATE (SYMBOL_VALUE (sp));
267 RELOCATE (SYMBOL_TYPE (sp));
271 relocate_typevector (tv)
272 struct typevector *tv;
274 register int ntypes = TYPEVECTOR_NTYPES (tv);
277 for (i = 0; i < ntypes; i++)
278 RELOCATE (TYPEVECTOR_TYPE (tv, i));
279 for (i = 0; i < ntypes; i++)
280 relocate_type (TYPEVECTOR_TYPE (tv, i));
283 /* We cannot come up with an a priori spanning tree
284 for the network of types, since types can be used
285 for many symbols and also as components of other types.
286 Therefore, we need to be able to mark types that we
287 already have relocated (or are already in the middle of relocating)
288 as in a garbage collector. */
292 register struct type *tp;
294 register int nfields = TYPE_NFIELDS (tp);
297 RELOCATE (TYPE_NAME (tp));
298 RELOCATE (TYPE_TARGET_TYPE (tp));
299 RELOCATE (TYPE_FIELDS (tp));
300 RELOCATE (TYPE_POINTER_TYPE (tp));
302 for (i = 0; i < nfields; i++)
304 RELOCATE (TYPE_FIELD_TYPE (tp, i));
305 RELOCATE (TYPE_FIELD_NAME (tp, i));
310 relocate_sourcevector (svp)
311 register struct sourcevector *svp;
313 register int nfiles = svp->length;
315 for (i = 0; i < nfiles; i++)
316 RELOCATE (svp->source[i]);
317 for (i = 0; i < nfiles; i++)
318 relocate_source (svp->source[i]);
323 register struct source *sp;
325 register int nitems = sp->contents.nitems;
329 for (i = 0; i < nitems; i++)
330 TEXT_RELOCATE (sp->contents.item[i].pc);
333 /* Read symsegs from file named NAME open on DESC,
334 make symtabs from them, and return a chain of them.
335 These symtabs are not suitable for direct use in `symtab_list'
336 because each one describes a single object file, perhaps many source files.
337 `symbol_file_command' takes each of these, makes many real symtabs
338 from it, and then frees it.
340 We assume DESC is prepositioned at the end of the string table,
341 just before the symsegs if there are any. */
344 read_symsegs (desc, name)
348 struct symbol_root root;
350 register struct symtab *sp, *sp1, *chain = 0;
355 len = myread (desc, &root, sizeof root);
356 if (len == 0 || root.format == 0)
358 /* format 1 was ok for the original gdb, but since the size of the
359 type structure changed when C++ support was added, it can no
360 longer be used. Accept only format 2. */
361 if (root.format != 2 ||
362 root.length < sizeof root)
363 error ("\nInvalid symbol segment format code");
364 data = (char *) xmalloc (root.length);
365 bcopy (&root, data, sizeof root);
366 len = myread (desc, data + sizeof root,
367 root.length - sizeof root);
368 sp = relocate_symtab (data);
369 RELOCATE (((struct symbol_root *)data)->sourcevector);
370 relocate_sourcevector (((struct symbol_root *)data)->sourcevector);
373 sp->linetable = (struct linetable *) ((struct symbol_root *)data)->sourcevector;
379 static int block_depth ();
380 static void print_spaces ();
381 static void print_symbol ();
384 print_symtabs (filename)
388 register struct symtab *s;
391 register struct linetable *l;
392 struct blockvector *bv;
393 register struct block *b;
395 struct cleanup *cleanups;
399 error_no_arg ("file to write symbol data in");
400 outfile = fopen (filename, "w");
402 perror_with_name (filename);
404 cleanups = make_cleanup (fclose, outfile);
407 for (s = symtab_list; s; s = s->next)
409 /* First print the line table. */
410 fprintf (outfile, "Symtab for file %s\n\n", s->filename);
411 fprintf (outfile, "Line table:\n\n");
414 for (i = 0; i < len; i++)
415 fprintf (outfile, " line %d at %x\n", l->item[i].line,
417 /* Now print the block info. */
418 fprintf (outfile, "\nBlockvector:\n\n");
419 bv = BLOCKVECTOR (s);
420 len = BLOCKVECTOR_NBLOCKS (bv);
421 for (i = 0; i < len; i++)
423 b = BLOCKVECTOR_BLOCK (bv, i);
424 depth = block_depth (b) * 2;
425 print_spaces (depth, outfile);
426 fprintf (outfile, "block #%03d (object 0x%x) ", i, b);
427 fprintf (outfile, "[0x%x..0x%x]", BLOCK_START (b), BLOCK_END (b));
428 if (BLOCK_SUPERBLOCK (b))
429 fprintf (outfile, " (under 0x%x)", BLOCK_SUPERBLOCK (b));
430 if (BLOCK_FUNCTION (b))
431 fprintf (outfile, " %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
432 fputc ('\n', outfile);
433 blen = BLOCK_NSYMS (b);
434 for (j = 0; j < blen; j++)
436 print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
440 fprintf (outfile, "\n\n");
444 do_cleanups (cleanups);
448 print_symbol (symbol, depth, outfile)
449 struct symbol *symbol;
453 print_spaces (depth, outfile);
454 if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
456 fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
457 SYMBOL_VALUE (symbol));
460 if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
462 if (TYPE_NAME (SYMBOL_TYPE (symbol)))
464 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
468 fprintf (outfile, "%s %s = ",
469 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
471 : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
472 ? "struct" : "union")),
473 SYMBOL_NAME (symbol));
474 type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
476 fprintf (outfile, ";\n");
480 if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
481 fprintf (outfile, "typedef ");
482 if (SYMBOL_TYPE (symbol))
484 type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
486 fprintf (outfile, "; ");
489 fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
491 switch (SYMBOL_CLASS (symbol))
494 fprintf (outfile, "const %d (0x%x),",
495 SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
498 case LOC_CONST_BYTES:
499 fprintf (outfile, "const %d hex bytes:",
500 TYPE_LENGTH (SYMBOL_TYPE (symbol)));
503 for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
504 fprintf (outfile, " %2x", SYMBOL_VALUE_BYTES (symbol) [i]);
505 fprintf (outfile, ",");
510 fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
514 fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
518 fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
522 fprintf (outfile, "parameter register %d,", SYMBOL_VALUE (symbol));
526 fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
533 fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
537 fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
538 SYMBOL_VALUE (symbol),
539 BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
543 fprintf (outfile, "\n");
546 /* Return the nexting depth of a block within other blocks in its symtab. */
553 while (block = BLOCK_SUPERBLOCK (block)) i++;
558 * Free all partial_symtab storage.
563 obstack_free (psymbol_obstack, 0);
564 obstack_init (psymbol_obstack);
565 partial_symtab_list = (struct partial_symtab *) 0;
569 _initialize_symmisc ()
571 symtab_list = (struct symtab *) 0;
572 partial_symtab_list = (struct partial_symtab *) 0;
574 add_com ("printsyms", class_obscure, print_symtabs,
575 "Print dump of current symbol definitions to file OUTFILE.");