]> Git Repo - binutils.git/blob - gdb/symmisc.c
Initial revision
[binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup)), for GDB.
2    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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)
9 any later version.
10
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.
15
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.  */
19
20
21 #include "defs.h"
22 #include "param.h"
23 #include "symtab.h"
24 #include "breakpoint.h"
25 #include "command.h"
26
27 #include <stdio.h>
28 #include <obstack.h>
29
30 static void free_symtab ();
31
32 \f
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.  */
36
37 void
38 free_all_symtabs ()
39 {
40   register struct symtab *s, *snext;
41
42   /* All values will be invalid because their types will be!  */
43
44   clear_value_history ();
45   clear_displays ();
46   clear_internalvars ();
47 #if defined (CLEAR_SOLIB)
48   CLEAR_SOLIB ();
49 #endif
50   set_default_breakpoint (0, 0, 0, 0);
51
52   current_source_symtab = 0;
53
54   for (s = symtab_list; s; s = snext)
55     {
56       snext = s->next;
57       free_symtab (s);
58     }
59   symtab_list = 0;
60   obstack_free (symbol_obstack, 0);
61   obstack_init (symbol_obstack);
62
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();
68 }
69
70 /* Free a struct block <- B and all the symbols defined in that block.  */
71
72 static void
73 free_symtab_block (b)
74      struct block *b;
75 {
76   register int i, n;
77   n = BLOCK_NSYMS (b);
78   for (i = 0; i < n; i++)
79     {
80       free (SYMBOL_NAME (BLOCK_SYM (b, i)));
81       free (BLOCK_SYM (b, i));
82     }
83   free (b);
84 }
85
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.  */
93
94 static void
95 free_symtab (s)
96      register struct symtab *s;
97 {
98   register int i, n;
99   register struct blockvector *bv;
100   register struct typevector *tv;
101
102   switch (s->free_code)
103     {
104     case free_nothing:
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.  */
108       break;
109
110     case free_contents:
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.  */
119       free (bv);
120       /* Free the type vector.  */
121       tv = TYPEVECTOR (s);
122       free (tv);
123       /* Also free the linetable.  */
124       
125     case free_linetable:
126       /* Everything will be freed either by our `free_ptr'
127          or by some other symbatb, except for our linetable.
128          Free that now.  */
129       free (LINETABLE (s));
130       break;
131     }
132
133   /* If there is a single block of memory to free, free it.  */
134   if (s->free_ptr)
135     free (s->free_ptr);
136
137   /* Free source-related stuff */
138   if (s->line_charpos)
139     free (s->line_charpos);
140   if (s->fullname)
141     free (s->fullname);
142   free (s);
143 }
144
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"
148    command.
149    
150    FIXME.  I think this is not the right way to do this.  It needs further
151    investigation, though.  -- gnu@cygnus  */
152
153 void
154 free_named_symtab (name)
155      char *name;
156 {
157   register struct symtab *s;
158   register struct symtab *prev;
159   struct blockvector *bv;
160
161 #if 0   /* FIXME */
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 ().  */
165
166   for (s = symtab_list; s; s = s->next)
167     {
168       if (!strcmp (name, s->filename))
169         break;
170       prev = s;
171     }
172
173   if (s)
174     {
175       if (s == symtab_list)
176         symtab_list = s->next;
177       else
178         prev->next = s->next;
179
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.  */
183
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).  */
189
190       bv = BLOCKLIST (s);
191       if (BLOCKLIST_NBLOCKS (bv) > 2
192           || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, 0))
193           || BLOCK_NSYMS (BLOCKVECTOR_BLOCK (bv, 1)))
194         {
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.  */
198           /*
199           printf ("Clearing breakpoints and resetting debugger state.\n");
200           */
201
202           clear_value_history ();
203           clear_displays ();
204           clear_internalvars ();
205           clear_breakpoints ();
206           set_default_breakpoint (0, 0, 0, 0);
207           current_source_symtab = 0;
208         }
209
210       free_symtab (s);
211     }
212   else
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.  */
219     ;
220 #endif  /* FIXME */
221 }
222
223 \f
224 static int block_depth ();
225 static void print_symbol ();
226
227 void
228 print_symtabs (filename)
229      char *filename;
230 {
231   FILE *outfile;
232   register struct symtab *s;
233   register int i, j;
234   int len, blen;
235   register struct linetable *l;
236   struct blockvector *bv;
237   register struct block *b;
238   int depth;
239   struct cleanup *cleanups;
240   extern int fclose();
241
242   if (filename == 0)
243     error_no_arg ("file to write symbol data in");
244
245   filename = tilde_expand (filename);
246   make_cleanup (free, filename);
247   
248   outfile = fopen (filename, "w");
249   if (outfile == 0)
250     perror_with_name (filename);
251
252   cleanups = make_cleanup (fclose, outfile);
253   immediate_quit++;
254
255   for (s = symtab_list; s; s = s->next)
256     {
257       /* First print the line table.  */
258       fprintf (outfile, "Symtab for file %s\n\n", s->filename);
259       fprintf (outfile, "Line table:\n\n");
260       l = LINETABLE (s);
261       len = l->nitems;
262       for (i = 0; i < len; i++)
263         fprintf (outfile, " line %d at %x\n", l->item[i].line,
264                  l->item[i].pc);
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++)
270         {
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++)
283             {
284               print_symbol (BLOCK_SYM (b, j), depth + 1, outfile);
285             }
286         }
287
288       fprintf (outfile, "\n\n");
289     }
290
291   immediate_quit--;
292   do_cleanups (cleanups);
293 }
294
295 static void
296 print_symbol (symbol, depth, outfile)
297      struct symbol *symbol;
298      int depth;
299      FILE *outfile;
300 {
301   print_spaces (depth, outfile);
302   if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
303     {
304       fprintf (outfile, "label %s at 0x%x\n", SYMBOL_NAME (symbol),
305                SYMBOL_VALUE_ADDRESS (symbol));
306       return;
307     }
308   if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
309     {
310       if (TYPE_NAME (SYMBOL_TYPE (symbol)))
311         {
312           type_print_1 (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
313         }
314       else
315         {
316           fprintf (outfile, "%s %s = ",
317                (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
318                 ? "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);
323         }
324       fprintf (outfile, ";\n");
325     }
326   else
327     {
328       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
329         fprintf (outfile, "typedef ");
330       if (SYMBOL_TYPE (symbol))
331         {
332           type_print_1 (SYMBOL_TYPE (symbol), SYMBOL_NAME (symbol),
333                         outfile, 1, depth);
334           fprintf (outfile, "; ");
335         }
336       else
337         fprintf (outfile, "%s ", SYMBOL_NAME (symbol));
338
339       switch (SYMBOL_CLASS (symbol))
340         {
341         case LOC_CONST:
342           fprintf (outfile, "const %ld (0x%lx),",
343                    SYMBOL_VALUE (symbol), SYMBOL_VALUE (symbol));
344           break;
345
346         case LOC_CONST_BYTES:
347           fprintf (outfile, "const %u hex bytes:",
348                    TYPE_LENGTH (SYMBOL_TYPE (symbol)));
349           {
350             unsigned i;
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, ",");
355           }
356           break;
357
358         case LOC_STATIC:
359           fprintf (outfile, "static at 0x%x,", SYMBOL_VALUE_ADDRESS (symbol));
360           break;
361
362         case LOC_REGISTER:
363           fprintf (outfile, "register %ld,", SYMBOL_VALUE (symbol));
364           break;
365
366         case LOC_ARG:
367           fprintf (outfile, "arg at 0x%lx,", SYMBOL_VALUE (symbol));
368           break;
369
370         case LOC_LOCAL_ARG:
371           fprintf (outfile, "arg at offset 0x%x from fp,",
372                    SYMBOL_VALUE (symbol));
373
374         case LOC_REF_ARG:
375           fprintf (outfile, "reference arg at 0x%lx,", SYMBOL_VALUE (symbol));
376           break;
377
378         case LOC_REGPARM:
379           fprintf (outfile, "parameter register %ld,", SYMBOL_VALUE (symbol));
380           break;
381
382         case LOC_LOCAL:
383           fprintf (outfile, "local at 0x%lx,", SYMBOL_VALUE (symbol));
384           break;
385
386         case LOC_TYPEDEF:
387           break;
388
389         case LOC_LABEL:
390           fprintf (outfile, "label at 0x%lx", SYMBOL_VALUE_ADDRESS (symbol));
391           break;
392
393         case LOC_BLOCK:
394           fprintf (outfile, "block (object 0x%x) starting at 0x%x,",
395                    SYMBOL_BLOCK_VALUE (symbol),
396                    BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)));
397           break;
398
399         case LOC_EXTERNAL:
400           fprintf (outfile, "external at 0x%x", SYMBOL_VALUE_ADDRESS (symbol));
401           break;
402
403         default:
404           fprintf (outfile, "botched symbol class %x", SYMBOL_CLASS (symbol));
405           break;
406         }
407     }
408   fprintf (outfile, "\n");
409 }
410
411 /* Return the nexting depth of a block within other blocks in its symtab.  */
412
413 static int
414 block_depth (block)
415      struct block *block;
416 {
417   register int i = 0;
418   while (block = BLOCK_SUPERBLOCK (block)) i++;
419   return i;
420 }
421 \f
422 /*
423  * Free all partial_symtab storage.
424  */
425 void
426 free_all_psymtabs()
427 {
428   obstack_free (psymbol_obstack, 0);
429   obstack_init (psymbol_obstack);
430   partial_symtab_list = (struct partial_symtab *) 0;
431 }
432 \f
433 void
434 _initialize_symmisc ()
435 {
436   symtab_list = (struct symtab *) 0;
437   partial_symtab_list = (struct partial_symtab *) 0;
438   
439   add_com ("printsyms", class_obscure, print_symtabs,
440            "Print dump of current symbol definitions to file OUTFILE.");
441 }
442
This page took 0.056826 seconds and 4 git commands to generate.