gdb-3.1
[external/binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup)), for GDB.
2    Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
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.
9
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.
16
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!
19 */
20
21
22 #include "defs.h"
23 #include "symtab.h"
24
25 #include <stdio.h>
26 #include <obstack.h>
27
28 static void free_symtab ();
29
30 \f
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.  */
34
35 void
36 free_all_symtabs ()
37 {
38   register struct symtab *s, *snext;
39
40   /* All values will be invalid because their types will be!  */
41
42   clear_value_history ();
43   clear_displays ();
44   clear_internalvars ();
45   clear_breakpoints ();
46   set_default_breakpoint (0, 0, 0, 0);
47
48   current_source_symtab = 0;
49
50   for (s = symtab_list; s; s = snext)
51     {
52       snext = s->next;
53       free_symtab (s);
54     }
55   symtab_list = 0;
56   obstack_free (symbol_obstack, 0);
57   obstack_init (symbol_obstack);
58
59   if (misc_function_vector)
60     free (misc_function_vector);
61   misc_function_count = 0;
62   misc_function_vector = 0;
63 }
64
65 /* Free a struct block <- B and all the symbols defined in that block.  */
66
67 static void
68 free_symtab_block (b)
69      struct block *b;
70 {
71   register int i, n;
72   n = BLOCK_NSYMS (b);
73   for (i = 0; i < n; i++)
74     {
75       free (SYMBOL_NAME (BLOCK_SYM (b, i)));
76       free (BLOCK_SYM (b, i));
77     }
78   free (b);
79 }
80
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.  */
88
89 static void
90 free_symtab (s)
91      register struct symtab *s;
92 {
93   register int i, n;
94   register struct blockvector *bv;
95   register struct type *type;
96   register struct typevector *tv;
97
98   switch (s->free_code)
99     {
100     case free_nothing:
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.  */
104       break;
105
106     case free_contents:
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.  */
115       free (bv);
116       /* Free the type vector.  */
117       tv = TYPEVECTOR (s);
118       free (tv);
119       /* Also free the linetable.  */
120       
121     case free_linetable:
122       /* Everything will be freed either by our `free_ptr'
123          or by some other symbatb, except for our linetable.
124          Free that now.  */
125       free (LINETABLE (s));
126       break;
127     }
128
129   /* If there is a single block of memory to free, free it.  */
130   if (s->free_ptr)
131     free (s->free_ptr);
132
133   if (s->line_charpos)
134     free (s->line_charpos);
135   free (s->filename);
136   free (s);
137 }
138 \f
139 /* Convert a raw symbol-segment to a struct symtab,
140    and relocate its internal pointers so that it is valid.  */
141
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)
145
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)
149
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;
153
154 #define CORE_RELOCATE(slot) \
155   ((slot) += (((slot) < data_start) ? text_relocation           \
156               : ((slot) < bss_start) ? data_relocation : bss_relocation))
157
158 #define TEXT_RELOCATE(slot)  ((slot) += text_relocation)
159
160 /* Relocation amounts for addresses in the program's core image.  */
161 static int text_relocation, data_relocation, bss_relocation;
162
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;
166
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 ();
173
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.  */
177
178 static struct symtab *
179 relocate_symtab (root)
180      struct symbol_root *root;
181 {
182   struct symtab *sp = (struct symtab *) xmalloc (sizeof (struct symtab));
183   bzero (sp, sizeof (struct symtab));
184
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;
191
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;
199
200   RELOCATE (TYPEVECTOR (sp));
201   RELOCATE (BLOCKVECTOR (sp));
202   RELOCATE (sp->version);
203   RELOCATE (sp->compilation);
204   RELOCATE (sp->filename);
205
206   relocate_typevector (TYPEVECTOR (sp));
207   relocate_blockvector (BLOCKVECTOR (sp));
208
209   return sp;
210 }
211
212 static void
213 relocate_blockvector (blp)
214      register struct blockvector *blp;
215 {
216   register int nblocks = BLOCKVECTOR_NBLOCKS (blp);
217   register int i;
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));
222 }
223
224 static void
225 relocate_block (bp)
226      register struct block *bp;
227 {
228   register int nsyms = BLOCK_NSYMS (bp);
229   register int i;
230
231   TEXT_RELOCATE (BLOCK_START (bp));
232   TEXT_RELOCATE (BLOCK_END (bp));
233
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));
241
242   for (i = 0; i < nsyms; i++)
243     RELOCATE (BLOCK_SYM (bp, i));
244
245   for (i = 0; i < nsyms; i++)
246     relocate_symbol (BLOCK_SYM (bp, i));
247 }
248
249 static void
250 relocate_symbol (sp)
251      register struct symbol *sp;
252 {
253   RELOCATE (SYMBOL_NAME (sp));
254   if (SYMBOL_CLASS (sp) == LOC_BLOCK)
255     {
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)));
262     }
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));
268 }
269
270 static void
271 relocate_typevector (tv)
272      struct typevector *tv;
273 {
274   register int ntypes = TYPEVECTOR_NTYPES (tv);
275   register int i;
276
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));
281 }
282
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.  */
289
290 static void
291 relocate_type (tp)
292      register struct type *tp;
293 {
294   register int nfields = TYPE_NFIELDS (tp);
295   register int i;
296
297   RELOCATE (TYPE_NAME (tp));
298   RELOCATE (TYPE_TARGET_TYPE (tp));
299   RELOCATE (TYPE_FIELDS (tp));
300   RELOCATE (TYPE_POINTER_TYPE (tp));
301
302   for (i = 0; i < nfields; i++)
303     {
304       RELOCATE (TYPE_FIELD_TYPE (tp, i));
305       RELOCATE (TYPE_FIELD_NAME (tp, i));
306     }
307 }
308
309 static void
310 relocate_sourcevector (svp)
311      register struct sourcevector *svp;
312 {
313   register int nfiles = svp->length;
314   register int i;
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]);
319 }
320
321 static void
322 relocate_source (sp)
323      register struct source *sp;
324 {
325   register int nitems = sp->contents.nitems;
326   register int i;
327
328   RELOCATE (sp->name);
329   for (i = 0; i < nitems; i++)
330     TEXT_RELOCATE (sp->contents.item[i].pc);
331 }
332 \f
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.
339
340    We assume DESC is prepositioned at the end of the string table,
341    just before the symsegs if there are any.  */
342
343 struct symtab *
344 read_symsegs (desc, name)
345      int desc;
346      char *name;
347 {
348   struct symbol_root root;
349   register char *data;
350   register struct symtab *sp, *sp1, *chain = 0;
351   register int len;
352
353   while (1)
354     {
355       len = myread (desc, &root, sizeof root);
356       if (len == 0 || root.format == 0)
357         break;
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);
371       sp->next = chain;
372       chain = sp;
373       sp->linetable = (struct linetable *) ((struct symbol_root *)data)->sourcevector;
374     }
375
376   return chain;
377 }
378 \f
379 static int block_depth ();
380 static void print_spaces ();
381 static void print_symbol ();
382
383 void
384 print_symtabs (filename)
385      char *filename;
386 {
387   FILE *outfile;
388   register struct symtab *s;
389   register int i, j;
390   int len, line, blen;
391   register struct linetable *l;
392   struct blockvector *bv;
393   register struct block *b;
394   int depth;
395   struct cleanup *cleanups;
396   extern int fclose();
397
398   if (filename == 0)
399     error_no_arg ("file to write symbol data in");
400   outfile = fopen (filename, "w");
401   if (outfile == 0)
402     perror_with_name (filename);
403
404   cleanups = make_cleanup (fclose, outfile);
405   immediate_quit++;
406
407   for (s = symtab_list; s; s = s->next)
408     {
409       /* First print the line table.  */
410       fprintf (outfile, "Symtab for file %s\n\n", s->filename);
411       fprintf (outfile, "Line table:\n\n");
412       l = LINETABLE (s);
413       len = l->nitems;
414       for (i = 0; i < len; i++)
415         fprintf (outfile, " line %d at %x\n", l->item[i].line,
416                  l->item[i].pc);
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++)
422         {
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++)
435             {
436               print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
437             }
438         }
439
440       fprintf (outfile, "\n\n");
441     }
442
443   immediate_quit--;
444   do_cleanups (cleanups);
445 }
446
447 static void
448 print_symbol (symbol, depth, outfile)
449      struct symbol *symbol;
450      int depth;
451      FILE *outfile;
452 {
453   print_spaces (depth, outfile);
454   if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
455     {
456       fprintf (outfile, "label %s at 0x%x", SYMBOL_NAME (symbol),
457                SYMBOL_VALUE (symbol));
458       return;
459     }
460   if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
461     {
462       if (TYPE_NAME (SYMBOL_TYPE (symbol)))
463         {
464           type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
465         }
466       else
467         {
468           fprintf (outfile, "%s %s = ",
469                (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
470                 ? "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);
475         }
476       fprintf (outfile, ";\n");
477     }
478   else
479     {
480       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
481         fprintf (outfile, "typedef ");
482       if (SYMBOL_TYPE (symbol))
483         {
484           type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
485                         outfile, 1, depth);
486           fprintf (outfile, "; ");
487         }
488       else
489         fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
490
491       switch (SYMBOL_CLASS (symbol))
492         {
493         case LOC_CONST:
494           fprintf (outfile, "const %d (0x%x),",
495                    SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
496           break;
497
498         case LOC_CONST_BYTES:
499           fprintf (outfile, "const %d hex bytes:",
500                    TYPE_LENGTH (SYMBOL_TYPE (symbol)));
501           {
502             int i;
503             for (i = 0; i < TYPE_LENGTH (SYMBOL_TYPE (symbol)); i++)
504               fprintf (outfile, " %2x", SYMBOL_VALUE_BYTES (symbol) [i]);
505             fprintf (outfile, ",");
506           }
507           break;
508
509         case LOC_STATIC:
510           fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE (symbol));
511           break;
512
513         case LOC_REGISTER:
514           fprintf (outfile, "register %d,", SYMBOL_VALUE (symbol));
515           break;
516
517         case LOC_ARG:
518           fprintf (outfile, "arg at 0x%x,", SYMBOL_VALUE (symbol));
519           break;
520
521         case LOC_REGPARM:
522           fprintf (outfile, "parameter register %d,", SYMBOL_VALUE (symbol));
523           break;
524
525         case LOC_LOCAL:
526           fprintf (outfile, "local at 0x%x,", SYMBOL_VALUE (symbol));
527           break;
528
529         case LOC_TYPEDEF:
530           break;
531
532         case LOC_LABEL:
533           fprintf (outfile, "label at 0x%x", SYMBOL_VALUE (symbol));
534           break;
535
536         case LOC_BLOCK:
537           fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
538                    SYMBOL_VALUE (symbol),
539                    BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
540           break;
541         }
542     }
543   fprintf (outfile, "\n");
544 }
545
546 /* Return the nexting depth of a block within other blocks in its symtab.  */
547
548 static int
549 block_depth (block)
550      struct block *block;
551 {
552   register int i = 0;
553   while (block = BLOCK_SUPERBLOCK (block)) i++;
554   return i;
555 }
556 \f
557 /*
558  * Free all partial_symtab storage.
559  */
560 void
561 free_all_psymtabs()
562 {
563   obstack_free (psymbol_obstack, 0);
564   obstack_init (psymbol_obstack);
565   partial_symtab_list = (struct partial_symtab *) 0;
566 }
567 \f
568 void
569 _initialize_symmisc ()
570 {
571   symtab_list = (struct symtab *) 0;
572   partial_symtab_list = (struct partial_symtab *) 0;
573   
574   add_com ("printsyms", class_obscure, print_symtabs,
575            "Print dump of current symbol definitions to file OUTFILE.");
576 }
577