This commit was generated by cvs2svn to track changes on a CVS vendor
[external/binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup), for GDB.
2
3    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4    1995, 1996, 1997, 1998, 1999, 2000, 2002 Free Software Foundation,
5    Inc.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include "defs.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "bfd.h"
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "breakpoint.h"
31 #include "command.h"
32 #include "gdb_obstack.h"
33 #include "language.h"
34 #include "bcache.h"
35
36 #include "gdb_string.h"
37
38 #ifndef DEV_TTY
39 #define DEV_TTY "/dev/tty"
40 #endif
41
42 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
43    when calling functions that take FILE *'s from the debugger.
44    So we make a variable which has the same value and which is accessible when
45    debugging GDB with itself.  Because stdin et al need not be constants,
46    we initialize them in the _initialize_symmisc function at the bottom
47    of the file.  */
48 FILE *std_in;
49 FILE *std_out;
50 FILE *std_err;
51
52 /* Prototypes for local functions */
53
54 static void dump_symtab (struct objfile *, struct symtab *,
55                          struct ui_file *);
56
57 static void dump_psymtab (struct objfile *, struct partial_symtab *,
58                           struct ui_file *);
59
60 static void dump_msymbols (struct objfile *, struct ui_file *);
61
62 static void dump_objfile (struct objfile *);
63
64 static int block_depth (struct block *);
65
66 static void print_partial_symbols (struct partial_symbol **, int,
67                                    char *, struct ui_file *);
68
69 static void free_symtab_block (struct objfile *, struct block *);
70
71 void _initialize_symmisc (void);
72
73 struct print_symbol_args
74   {
75     struct symbol *symbol;
76     int depth;
77     struct ui_file *outfile;
78   };
79
80 static int print_symbol (PTR);
81
82 static void free_symtab_block (struct objfile *, struct block *);
83 \f
84
85 /* Free a struct block <- B and all the symbols defined in that block.  */
86
87 static void
88 free_symtab_block (struct objfile *objfile, struct block *b)
89 {
90   register int i, n;
91   struct symbol *sym, *next_sym;
92
93   n = BLOCK_BUCKETS (b);
94   for (i = 0; i < n; i++)
95     {
96       for (sym = BLOCK_BUCKET (b, i); sym; sym = next_sym)
97         {
98           next_sym = sym->hash_next;
99           xmfree (objfile->md, SYMBOL_NAME (sym));
100           xmfree (objfile->md, (PTR) sym);
101         }
102     }
103   xmfree (objfile->md, (PTR) b);
104 }
105
106 /* Free all the storage associated with the struct symtab <- S.
107    Note that some symtabs have contents malloc'ed structure by structure,
108    while some have contents that all live inside one big block of memory,
109    and some share the contents of another symbol table and so you should
110    not free the contents on their behalf (except sometimes the linetable,
111    which maybe per symtab even when the rest is not).
112    It is s->free_code that says which alternative to use.  */
113
114 void
115 free_symtab (register struct symtab *s)
116 {
117   register int i, n;
118   register struct blockvector *bv;
119
120   switch (s->free_code)
121     {
122     case free_nothing:
123       /* All the contents are part of a big block of memory (an obstack),
124          and some other symtab is in charge of freeing that block.
125          Therefore, do nothing.  */
126       break;
127
128     case free_contents:
129       /* Here all the contents were malloc'ed structure by structure
130          and must be freed that way.  */
131       /* First free the blocks (and their symbols.  */
132       bv = BLOCKVECTOR (s);
133       n = BLOCKVECTOR_NBLOCKS (bv);
134       for (i = 0; i < n; i++)
135         free_symtab_block (s->objfile, BLOCKVECTOR_BLOCK (bv, i));
136       /* Free the blockvector itself.  */
137       xmfree (s->objfile->md, (PTR) bv);
138       /* Also free the linetable.  */
139
140     case free_linetable:
141       /* Everything will be freed either by our `free_ptr'
142          or by some other symtab, except for our linetable.
143          Free that now.  */
144       if (LINETABLE (s))
145         xmfree (s->objfile->md, (PTR) LINETABLE (s));
146       break;
147     }
148
149   /* If there is a single block of memory to free, free it.  */
150   if (s->free_ptr != NULL)
151     xmfree (s->objfile->md, s->free_ptr);
152
153   /* Free source-related stuff */
154   if (s->line_charpos != NULL)
155     xmfree (s->objfile->md, (PTR) s->line_charpos);
156   if (s->fullname != NULL)
157     xmfree (s->objfile->md, s->fullname);
158   if (s->debugformat != NULL)
159     xmfree (s->objfile->md, s->debugformat);
160   xmfree (s->objfile->md, (PTR) s);
161 }
162
163 void
164 print_symbol_bcache_statistics (void)
165 {
166   struct objfile *objfile;
167
168   immediate_quit++;
169   ALL_OBJFILES (objfile)
170   {
171     printf_filtered ("Byte cache statistics for '%s':\n", objfile->name);
172     print_bcache_statistics (objfile->psymbol_cache, "partial symbol cache");
173   }
174   immediate_quit--;
175 }
176
177 void
178 print_objfile_statistics (void)
179 {
180   struct objfile *objfile;
181
182   immediate_quit++;
183   ALL_OBJFILES (objfile)
184   {
185     printf_filtered ("Statistics for '%s':\n", objfile->name);
186     if (OBJSTAT (objfile, n_stabs) > 0)
187       printf_filtered ("  Number of \"stab\" symbols read: %d\n",
188                        OBJSTAT (objfile, n_stabs));
189     if (OBJSTAT (objfile, n_minsyms) > 0)
190       printf_filtered ("  Number of \"minimal\" symbols read: %d\n",
191                        OBJSTAT (objfile, n_minsyms));
192     if (OBJSTAT (objfile, n_psyms) > 0)
193       printf_filtered ("  Number of \"partial\" symbols read: %d\n",
194                        OBJSTAT (objfile, n_psyms));
195     if (OBJSTAT (objfile, n_syms) > 0)
196       printf_filtered ("  Number of \"full\" symbols read: %d\n",
197                        OBJSTAT (objfile, n_syms));
198     if (OBJSTAT (objfile, n_types) > 0)
199       printf_filtered ("  Number of \"types\" defined: %d\n",
200                        OBJSTAT (objfile, n_types));
201     if (OBJSTAT (objfile, sz_strtab) > 0)
202       printf_filtered ("  Space used by a.out string tables: %d\n",
203                        OBJSTAT (objfile, sz_strtab));
204     printf_filtered ("  Total memory used for psymbol obstack: %d\n",
205                      obstack_memory_used (&objfile->psymbol_obstack));
206     printf_filtered ("  Total memory used for psymbol cache: %d\n",
207                      bcache_memory_used (objfile->psymbol_cache));
208     printf_filtered ("  Total memory used for macro cache: %d\n",
209                      bcache_memory_used (objfile->macro_cache));
210     printf_filtered ("  Total memory used for symbol obstack: %d\n",
211                      obstack_memory_used (&objfile->symbol_obstack));
212     printf_filtered ("  Total memory used for type obstack: %d\n",
213                      obstack_memory_used (&objfile->type_obstack));
214   }
215   immediate_quit--;
216 }
217
218 static void
219 dump_objfile (struct objfile *objfile)
220 {
221   struct symtab *symtab;
222   struct partial_symtab *psymtab;
223
224   printf_filtered ("\nObject file %s:  ", objfile->name);
225   printf_filtered ("Objfile at ");
226   gdb_print_host_address (objfile, gdb_stdout);
227   printf_filtered (", bfd at ");
228   gdb_print_host_address (objfile->obfd, gdb_stdout);
229   printf_filtered (", %d minsyms\n\n",
230                    objfile->minimal_symbol_count);
231
232   if (objfile->psymtabs)
233     {
234       printf_filtered ("Psymtabs:\n");
235       for (psymtab = objfile->psymtabs;
236            psymtab != NULL;
237            psymtab = psymtab->next)
238         {
239           printf_filtered ("%s at ",
240                            psymtab->filename);
241           gdb_print_host_address (psymtab, gdb_stdout);
242           printf_filtered (", ");
243           if (psymtab->objfile != objfile)
244             {
245               printf_filtered ("NOT ON CHAIN!  ");
246             }
247           wrap_here ("  ");
248         }
249       printf_filtered ("\n\n");
250     }
251
252   if (objfile->symtabs)
253     {
254       printf_filtered ("Symtabs:\n");
255       for (symtab = objfile->symtabs;
256            symtab != NULL;
257            symtab = symtab->next)
258         {
259           printf_filtered ("%s at ", symtab->filename);
260           gdb_print_host_address (symtab, gdb_stdout);
261           printf_filtered (", ");
262           if (symtab->objfile != objfile)
263             {
264               printf_filtered ("NOT ON CHAIN!  ");
265             }
266           wrap_here ("  ");
267         }
268       printf_filtered ("\n\n");
269     }
270 }
271
272 /* Print minimal symbols from this objfile.  */
273
274 static void
275 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
276 {
277   struct minimal_symbol *msymbol;
278   int index;
279   char ms_type;
280
281   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
282   if (objfile->minimal_symbol_count == 0)
283     {
284       fprintf_filtered (outfile, "No minimal symbols found.\n");
285       return;
286     }
287   for (index = 0, msymbol = objfile->msymbols;
288        SYMBOL_NAME (msymbol) != NULL; msymbol++, index++)
289     {
290       switch (msymbol->type)
291         {
292         case mst_unknown:
293           ms_type = 'u';
294           break;
295         case mst_text:
296           ms_type = 'T';
297           break;
298         case mst_solib_trampoline:
299           ms_type = 'S';
300           break;
301         case mst_data:
302           ms_type = 'D';
303           break;
304         case mst_bss:
305           ms_type = 'B';
306           break;
307         case mst_abs:
308           ms_type = 'A';
309           break;
310         case mst_file_text:
311           ms_type = 't';
312           break;
313         case mst_file_data:
314           ms_type = 'd';
315           break;
316         case mst_file_bss:
317           ms_type = 'b';
318           break;
319         default:
320           ms_type = '?';
321           break;
322         }
323       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
324       print_address_numeric (SYMBOL_VALUE_ADDRESS (msymbol), 1, outfile);
325       fprintf_filtered (outfile, " %s", SYMBOL_NAME (msymbol));
326       if (SYMBOL_BFD_SECTION (msymbol))
327         fprintf_filtered (outfile, " section %s",
328                           bfd_section_name (objfile->obfd,
329                                             SYMBOL_BFD_SECTION (msymbol)));
330       if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
331         {
332           fprintf_filtered (outfile, "  %s", SYMBOL_DEMANGLED_NAME (msymbol));
333         }
334 #ifdef SOFUN_ADDRESS_MAYBE_MISSING
335       if (msymbol->filename)
336         fprintf_filtered (outfile, "  %s", msymbol->filename);
337 #endif
338       fputs_filtered ("\n", outfile);
339     }
340   if (objfile->minimal_symbol_count != index)
341     {
342       warning ("internal error:  minimal symbol count %d != %d",
343                objfile->minimal_symbol_count, index);
344     }
345   fprintf_filtered (outfile, "\n");
346 }
347
348 static void
349 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
350               struct ui_file *outfile)
351 {
352   int i;
353
354   fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
355                     psymtab->filename);
356   fprintf_filtered (outfile, "(object ");
357   gdb_print_host_address (psymtab, outfile);
358   fprintf_filtered (outfile, ")\n\n");
359   fprintf_unfiltered (outfile, "  Read from object file %s (",
360                       objfile->name);
361   gdb_print_host_address (objfile, outfile);
362   fprintf_unfiltered (outfile, ")\n");
363
364   if (psymtab->readin)
365     {
366       fprintf_filtered (outfile,
367                         "  Full symtab was read (at ");
368       gdb_print_host_address (psymtab->symtab, outfile);
369       fprintf_filtered (outfile, " by function at ");
370       gdb_print_host_address ((PTR) psymtab->read_symtab, outfile);
371       fprintf_filtered (outfile, ")\n");
372     }
373
374   fprintf_filtered (outfile, "  Relocate symbols by ");
375   for (i = 0; i < psymtab->objfile->num_sections; ++i)
376     {
377       if (i != 0)
378         fprintf_filtered (outfile, ", ");
379       wrap_here ("    ");
380       print_address_numeric (ANOFFSET (psymtab->section_offsets, i),
381                              1,
382                              outfile);
383     }
384   fprintf_filtered (outfile, "\n");
385
386   fprintf_filtered (outfile, "  Symbols cover text addresses ");
387   print_address_numeric (psymtab->textlow, 1, outfile);
388   fprintf_filtered (outfile, "-");
389   print_address_numeric (psymtab->texthigh, 1, outfile);
390   fprintf_filtered (outfile, "\n");
391   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
392                     psymtab->number_of_dependencies);
393   for (i = 0; i < psymtab->number_of_dependencies; i++)
394     {
395       fprintf_filtered (outfile, "    %d ", i);
396       gdb_print_host_address (psymtab->dependencies[i], outfile);
397       fprintf_filtered (outfile, " %s\n",
398                         psymtab->dependencies[i]->filename);
399     }
400   if (psymtab->n_global_syms > 0)
401     {
402       print_partial_symbols (objfile->global_psymbols.list
403                              + psymtab->globals_offset,
404                              psymtab->n_global_syms, "Global", outfile);
405     }
406   if (psymtab->n_static_syms > 0)
407     {
408       print_partial_symbols (objfile->static_psymbols.list
409                              + psymtab->statics_offset,
410                              psymtab->n_static_syms, "Static", outfile);
411     }
412   fprintf_filtered (outfile, "\n");
413 }
414
415 static void
416 dump_symtab (struct objfile *objfile, struct symtab *symtab,
417              struct ui_file *outfile)
418 {
419   register int i, j;
420   int len, blen;
421   register struct linetable *l;
422   struct blockvector *bv;
423   struct symbol *sym;
424   register struct block *b;
425   int depth;
426
427   fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
428   if (symtab->dirname)
429     fprintf_filtered (outfile, "Compilation directory is %s\n",
430                       symtab->dirname);
431   fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
432   gdb_print_host_address (objfile, outfile);
433   fprintf_filtered (outfile, ")\n");
434   fprintf_filtered (outfile, "Language: %s\n", language_str (symtab->language));
435
436   /* First print the line table.  */
437   l = LINETABLE (symtab);
438   if (l)
439     {
440       fprintf_filtered (outfile, "\nLine table:\n\n");
441       len = l->nitems;
442       for (i = 0; i < len; i++)
443         {
444           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
445           print_address_numeric (l->item[i].pc, 1, outfile);
446           fprintf_filtered (outfile, "\n");
447         }
448     }
449   /* Now print the block info, but only for primary symtabs since we will
450      print lots of duplicate info otherwise. */
451   if (symtab->primary)
452     {
453       fprintf_filtered (outfile, "\nBlockvector:\n\n");
454       bv = BLOCKVECTOR (symtab);
455       len = BLOCKVECTOR_NBLOCKS (bv);
456       for (i = 0; i < len; i++)
457         {
458           b = BLOCKVECTOR_BLOCK (bv, i);
459           depth = block_depth (b) * 2;
460           print_spaces (depth, outfile);
461           fprintf_filtered (outfile, "block #%03d, object at ", i);
462           gdb_print_host_address (b, outfile);
463           if (BLOCK_SUPERBLOCK (b))
464             {
465               fprintf_filtered (outfile, " under ");
466               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
467             }
468           /* drow/2002-07-10: We could save the total symbols count
469              even if we're using a hashtable, but nothing else but this message
470              wants it.  */
471           blen = BLOCK_BUCKETS (b);
472           if (BLOCK_HASHTABLE (b))
473             fprintf_filtered (outfile, ", %d buckets in ", blen);
474           else
475             fprintf_filtered (outfile, ", %d syms in ", blen);
476           print_address_numeric (BLOCK_START (b), 1, outfile);
477           fprintf_filtered (outfile, "..");
478           print_address_numeric (BLOCK_END (b), 1, outfile);
479           if (BLOCK_FUNCTION (b))
480             {
481               fprintf_filtered (outfile, ", function %s", SYMBOL_NAME (BLOCK_FUNCTION (b)));
482               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
483                 {
484                   fprintf_filtered (outfile, ", %s",
485                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
486                 }
487             }
488           if (BLOCK_GCC_COMPILED (b))
489             fprintf_filtered (outfile, ", compiled with gcc%d", BLOCK_GCC_COMPILED (b));
490           fprintf_filtered (outfile, "\n");
491           /* Now print each symbol in this block (in no particular order, if
492              we're using a hashtable).  */
493           ALL_BLOCK_SYMBOLS (b, j, sym)
494             {
495               struct print_symbol_args s;
496               s.symbol = sym;
497               s.depth = depth + 1;
498               s.outfile = outfile;
499               catch_errors (print_symbol, &s, "Error printing symbol:\n",
500                             RETURN_MASK_ALL);
501             }
502         }
503       fprintf_filtered (outfile, "\n");
504     }
505   else
506     {
507       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
508     }
509 }
510
511 void
512 maintenance_print_symbols (char *args, int from_tty)
513 {
514   char **argv;
515   struct ui_file *outfile;
516   struct cleanup *cleanups;
517   char *symname = NULL;
518   char *filename = DEV_TTY;
519   struct objfile *objfile;
520   struct symtab *s;
521
522   dont_repeat ();
523
524   if (args == NULL)
525     {
526       error ("\
527 Arguments missing: an output file name and an optional symbol file name");
528     }
529   else if ((argv = buildargv (args)) == NULL)
530     {
531       nomem (0);
532     }
533   cleanups = make_cleanup_freeargv (argv);
534
535   if (argv[0] != NULL)
536     {
537       filename = argv[0];
538       /* If a second arg is supplied, it is a source file name to match on */
539       if (argv[1] != NULL)
540         {
541           symname = argv[1];
542         }
543     }
544
545   filename = tilde_expand (filename);
546   make_cleanup (xfree, filename);
547
548   outfile = gdb_fopen (filename, FOPEN_WT);
549   if (outfile == 0)
550     perror_with_name (filename);
551   make_cleanup_ui_file_delete (outfile);
552
553   immediate_quit++;
554   ALL_SYMTABS (objfile, s)
555     if (symname == NULL || (STREQ (symname, s->filename)))
556     dump_symtab (objfile, s, outfile);
557   immediate_quit--;
558   do_cleanups (cleanups);
559 }
560
561 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
562    far to indent.  ARGS is really a struct print_symbol_args *, but is
563    declared as char * to get it past catch_errors.  Returns 0 for error,
564    1 for success.  */
565
566 static int
567 print_symbol (PTR args)
568 {
569   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
570   int depth = ((struct print_symbol_args *) args)->depth;
571   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
572
573   print_spaces (depth, outfile);
574   if (SYMBOL_NAMESPACE (symbol) == LABEL_NAMESPACE)
575     {
576       fprintf_filtered (outfile, "label %s at ", SYMBOL_SOURCE_NAME (symbol));
577       print_address_numeric (SYMBOL_VALUE_ADDRESS (symbol), 1, outfile);
578       if (SYMBOL_BFD_SECTION (symbol))
579         fprintf_filtered (outfile, " section %s\n",
580                        bfd_section_name (SYMBOL_BFD_SECTION (symbol)->owner,
581                                          SYMBOL_BFD_SECTION (symbol)));
582       else
583         fprintf_filtered (outfile, "\n");
584       return 1;
585     }
586   if (SYMBOL_NAMESPACE (symbol) == STRUCT_NAMESPACE)
587     {
588       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
589         {
590           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
591         }
592       else
593         {
594           fprintf_filtered (outfile, "%s %s = ",
595                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
596                           ? "enum"
597                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
598                         ? "struct" : "union")),
599                             SYMBOL_NAME (symbol));
600           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
601         }
602       fprintf_filtered (outfile, ";\n");
603     }
604   else
605     {
606       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
607         fprintf_filtered (outfile, "typedef ");
608       if (SYMBOL_TYPE (symbol))
609         {
610           /* Print details of types, except for enums where it's clutter.  */
611           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_SOURCE_NAME (symbol),
612                          outfile,
613                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
614                          depth);
615           fprintf_filtered (outfile, "; ");
616         }
617       else
618         fprintf_filtered (outfile, "%s ", SYMBOL_SOURCE_NAME (symbol));
619
620       switch (SYMBOL_CLASS (symbol))
621         {
622         case LOC_CONST:
623           fprintf_filtered (outfile, "const %ld (0x%lx)",
624                             SYMBOL_VALUE (symbol),
625                             SYMBOL_VALUE (symbol));
626           break;
627
628         case LOC_CONST_BYTES:
629           {
630             unsigned i;
631             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
632             fprintf_filtered (outfile, "const %u hex bytes:",
633                               TYPE_LENGTH (type));
634             for (i = 0; i < TYPE_LENGTH (type); i++)
635               fprintf_filtered (outfile, " %02x",
636                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
637           }
638           break;
639
640         case LOC_STATIC:
641           fprintf_filtered (outfile, "static at ");
642           print_address_numeric (SYMBOL_VALUE_ADDRESS (symbol), 1, outfile);
643           if (SYMBOL_BFD_SECTION (symbol))
644             fprintf_filtered (outfile, " section %s",
645                               bfd_section_name
646                               (SYMBOL_BFD_SECTION (symbol)->owner,
647                                SYMBOL_BFD_SECTION (symbol)));
648           break;
649
650         case LOC_INDIRECT:
651           fprintf_filtered (outfile, "extern global at *(");
652           print_address_numeric (SYMBOL_VALUE_ADDRESS (symbol), 1, outfile);
653           fprintf_filtered (outfile, "),");
654           break;
655
656         case LOC_REGISTER:
657           fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
658           break;
659
660         case LOC_ARG:
661           fprintf_filtered (outfile, "arg at offset 0x%lx",
662                             SYMBOL_VALUE (symbol));
663           break;
664
665         case LOC_LOCAL_ARG:
666           fprintf_filtered (outfile, "arg at offset 0x%lx from fp",
667                             SYMBOL_VALUE (symbol));
668           break;
669
670         case LOC_REF_ARG:
671           fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
672           break;
673
674         case LOC_REGPARM:
675           fprintf_filtered (outfile, "parameter register %ld", SYMBOL_VALUE (symbol));
676           break;
677
678         case LOC_REGPARM_ADDR:
679           fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
680           break;
681
682         case LOC_LOCAL:
683           fprintf_filtered (outfile, "local at offset 0x%lx",
684                             SYMBOL_VALUE (symbol));
685           break;
686
687         case LOC_BASEREG:
688           fprintf_filtered (outfile, "local at 0x%lx from register %d",
689                             SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
690           break;
691
692         case LOC_BASEREG_ARG:
693           fprintf_filtered (outfile, "arg at 0x%lx from register %d",
694                             SYMBOL_VALUE (symbol), SYMBOL_BASEREG (symbol));
695           break;
696
697         case LOC_TYPEDEF:
698           break;
699
700         case LOC_LABEL:
701           fprintf_filtered (outfile, "label at ");
702           print_address_numeric (SYMBOL_VALUE_ADDRESS (symbol), 1, outfile);
703           if (SYMBOL_BFD_SECTION (symbol))
704             fprintf_filtered (outfile, " section %s",
705                               bfd_section_name
706                               (SYMBOL_BFD_SECTION (symbol)->owner,
707                                SYMBOL_BFD_SECTION (symbol)));
708           break;
709
710         case LOC_BLOCK:
711           fprintf_filtered (outfile, "block object ");
712           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
713           fprintf_filtered (outfile, ", ");
714           print_address_numeric (BLOCK_START (SYMBOL_BLOCK_VALUE (symbol)),
715                                  1,
716                                  outfile);
717           fprintf_filtered (outfile, "..");
718           print_address_numeric (BLOCK_END (SYMBOL_BLOCK_VALUE (symbol)),
719                                  1,
720                                  outfile);
721           if (SYMBOL_BFD_SECTION (symbol))
722             fprintf_filtered (outfile, " section %s",
723                               bfd_section_name
724                               (SYMBOL_BFD_SECTION (symbol)->owner,
725                                SYMBOL_BFD_SECTION (symbol)));
726           break;
727
728         case LOC_UNRESOLVED:
729           fprintf_filtered (outfile, "unresolved");
730           break;
731
732         case LOC_OPTIMIZED_OUT:
733           fprintf_filtered (outfile, "optimized out");
734           break;
735
736         default:
737           fprintf_filtered (outfile, "botched symbol class %x",
738                             SYMBOL_CLASS (symbol));
739           break;
740         }
741     }
742   fprintf_filtered (outfile, "\n");
743   return 1;
744 }
745
746 void
747 maintenance_print_psymbols (char *args, int from_tty)
748 {
749   char **argv;
750   struct ui_file *outfile;
751   struct cleanup *cleanups;
752   char *symname = NULL;
753   char *filename = DEV_TTY;
754   struct objfile *objfile;
755   struct partial_symtab *ps;
756
757   dont_repeat ();
758
759   if (args == NULL)
760     {
761       error ("print-psymbols takes an output file name and optional symbol file name");
762     }
763   else if ((argv = buildargv (args)) == NULL)
764     {
765       nomem (0);
766     }
767   cleanups = make_cleanup_freeargv (argv);
768
769   if (argv[0] != NULL)
770     {
771       filename = argv[0];
772       /* If a second arg is supplied, it is a source file name to match on */
773       if (argv[1] != NULL)
774         {
775           symname = argv[1];
776         }
777     }
778
779   filename = tilde_expand (filename);
780   make_cleanup (xfree, filename);
781
782   outfile = gdb_fopen (filename, FOPEN_WT);
783   if (outfile == 0)
784     perror_with_name (filename);
785   make_cleanup_ui_file_delete (outfile);
786
787   immediate_quit++;
788   ALL_PSYMTABS (objfile, ps)
789     if (symname == NULL || (STREQ (symname, ps->filename)))
790     dump_psymtab (objfile, ps, outfile);
791   immediate_quit--;
792   do_cleanups (cleanups);
793 }
794
795 static void
796 print_partial_symbols (struct partial_symbol **p, int count, char *what,
797                        struct ui_file *outfile)
798 {
799   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
800   while (count-- > 0)
801     {
802       fprintf_filtered (outfile, "    `%s'", SYMBOL_NAME (*p));
803       if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
804         {
805           fprintf_filtered (outfile, "  `%s'", SYMBOL_DEMANGLED_NAME (*p));
806         }
807       fputs_filtered (", ", outfile);
808       switch (SYMBOL_NAMESPACE (*p))
809         {
810         case UNDEF_NAMESPACE:
811           fputs_filtered ("undefined namespace, ", outfile);
812           break;
813         case VAR_NAMESPACE:
814           /* This is the usual thing -- don't print it */
815           break;
816         case STRUCT_NAMESPACE:
817           fputs_filtered ("struct namespace, ", outfile);
818           break;
819         case LABEL_NAMESPACE:
820           fputs_filtered ("label namespace, ", outfile);
821           break;
822         default:
823           fputs_filtered ("<invalid namespace>, ", outfile);
824           break;
825         }
826       switch (SYMBOL_CLASS (*p))
827         {
828         case LOC_UNDEF:
829           fputs_filtered ("undefined", outfile);
830           break;
831         case LOC_CONST:
832           fputs_filtered ("constant int", outfile);
833           break;
834         case LOC_STATIC:
835           fputs_filtered ("static", outfile);
836           break;
837         case LOC_INDIRECT:
838           fputs_filtered ("extern global", outfile);
839           break;
840         case LOC_REGISTER:
841           fputs_filtered ("register", outfile);
842           break;
843         case LOC_ARG:
844           fputs_filtered ("pass by value", outfile);
845           break;
846         case LOC_REF_ARG:
847           fputs_filtered ("pass by reference", outfile);
848           break;
849         case LOC_REGPARM:
850           fputs_filtered ("register parameter", outfile);
851           break;
852         case LOC_REGPARM_ADDR:
853           fputs_filtered ("register address parameter", outfile);
854           break;
855         case LOC_LOCAL:
856           fputs_filtered ("stack parameter", outfile);
857           break;
858         case LOC_TYPEDEF:
859           fputs_filtered ("type", outfile);
860           break;
861         case LOC_LABEL:
862           fputs_filtered ("label", outfile);
863           break;
864         case LOC_BLOCK:
865           fputs_filtered ("function", outfile);
866           break;
867         case LOC_CONST_BYTES:
868           fputs_filtered ("constant bytes", outfile);
869           break;
870         case LOC_LOCAL_ARG:
871           fputs_filtered ("shuffled arg", outfile);
872           break;
873         case LOC_UNRESOLVED:
874           fputs_filtered ("unresolved", outfile);
875           break;
876         case LOC_OPTIMIZED_OUT:
877           fputs_filtered ("optimized out", outfile);
878           break;
879         default:
880           fputs_filtered ("<invalid location>", outfile);
881           break;
882         }
883       fputs_filtered (", ", outfile);
884       print_address_numeric (SYMBOL_VALUE_ADDRESS (*p), 1, outfile);
885       fprintf_filtered (outfile, "\n");
886       p++;
887     }
888 }
889
890 void
891 maintenance_print_msymbols (char *args, int from_tty)
892 {
893   char **argv;
894   struct ui_file *outfile;
895   struct cleanup *cleanups;
896   char *filename = DEV_TTY;
897   char *symname = NULL;
898   struct objfile *objfile;
899
900   dont_repeat ();
901
902   if (args == NULL)
903     {
904       error ("print-msymbols takes an output file name and optional symbol file name");
905     }
906   else if ((argv = buildargv (args)) == NULL)
907     {
908       nomem (0);
909     }
910   cleanups = make_cleanup_freeargv (argv);
911
912   if (argv[0] != NULL)
913     {
914       filename = argv[0];
915       /* If a second arg is supplied, it is a source file name to match on */
916       if (argv[1] != NULL)
917         {
918           symname = argv[1];
919         }
920     }
921
922   filename = tilde_expand (filename);
923   make_cleanup (xfree, filename);
924
925   outfile = gdb_fopen (filename, FOPEN_WT);
926   if (outfile == 0)
927     perror_with_name (filename);
928   make_cleanup_ui_file_delete (outfile);
929
930   immediate_quit++;
931   ALL_OBJFILES (objfile)
932     if (symname == NULL || (STREQ (symname, objfile->name)))
933     dump_msymbols (objfile, outfile);
934   immediate_quit--;
935   fprintf_filtered (outfile, "\n\n");
936   do_cleanups (cleanups);
937 }
938
939 void
940 maintenance_print_objfiles (char *ignore, int from_tty)
941 {
942   struct objfile *objfile;
943
944   dont_repeat ();
945
946   immediate_quit++;
947   ALL_OBJFILES (objfile)
948     dump_objfile (objfile);
949   immediate_quit--;
950 }
951
952 /* Check consistency of psymtabs and symtabs.  */
953
954 void
955 maintenance_check_symtabs (char *ignore, int from_tty)
956 {
957   register struct symbol *sym;
958   register struct partial_symbol **psym;
959   register struct symtab *s = NULL;
960   register struct partial_symtab *ps;
961   struct blockvector *bv;
962   register struct objfile *objfile;
963   register struct block *b;
964   int length;
965
966   ALL_PSYMTABS (objfile, ps)
967   {
968     s = PSYMTAB_TO_SYMTAB (ps);
969     if (s == NULL)
970       continue;
971     bv = BLOCKVECTOR (s);
972     b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
973     psym = ps->objfile->static_psymbols.list + ps->statics_offset;
974     length = ps->n_static_syms;
975     while (length--)
976       {
977         sym = lookup_block_symbol (b, SYMBOL_NAME (*psym),
978                                    NULL, SYMBOL_NAMESPACE (*psym));
979         if (!sym)
980           {
981             printf_filtered ("Static symbol `");
982             puts_filtered (SYMBOL_NAME (*psym));
983             printf_filtered ("' only found in ");
984             puts_filtered (ps->filename);
985             printf_filtered (" psymtab\n");
986           }
987         psym++;
988       }
989     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
990     psym = ps->objfile->global_psymbols.list + ps->globals_offset;
991     length = ps->n_global_syms;
992     while (length--)
993       {
994         sym = lookup_block_symbol (b, SYMBOL_NAME (*psym),
995                                    NULL, SYMBOL_NAMESPACE (*psym));
996         if (!sym)
997           {
998             printf_filtered ("Global symbol `");
999             puts_filtered (SYMBOL_NAME (*psym));
1000             printf_filtered ("' only found in ");
1001             puts_filtered (ps->filename);
1002             printf_filtered (" psymtab\n");
1003           }
1004         psym++;
1005       }
1006     if (ps->texthigh < ps->textlow)
1007       {
1008         printf_filtered ("Psymtab ");
1009         puts_filtered (ps->filename);
1010         printf_filtered (" covers bad range ");
1011         print_address_numeric (ps->textlow, 1, gdb_stdout);
1012         printf_filtered (" - ");
1013         print_address_numeric (ps->texthigh, 1, gdb_stdout);
1014         printf_filtered ("\n");
1015         continue;
1016       }
1017     if (ps->texthigh == 0)
1018       continue;
1019     if (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b))
1020       {
1021         printf_filtered ("Psymtab ");
1022         puts_filtered (ps->filename);
1023         printf_filtered (" covers ");
1024         print_address_numeric (ps->textlow, 1, gdb_stdout);
1025         printf_filtered (" - ");
1026         print_address_numeric (ps->texthigh, 1, gdb_stdout);
1027         printf_filtered (" but symtab covers only ");
1028         print_address_numeric (BLOCK_START (b), 1, gdb_stdout);
1029         printf_filtered (" - ");
1030         print_address_numeric (BLOCK_END (b), 1, gdb_stdout);
1031         printf_filtered ("\n");
1032       }
1033   }
1034 }
1035 \f
1036
1037 /* Return the nexting depth of a block within other blocks in its symtab.  */
1038
1039 static int
1040 block_depth (struct block *block)
1041 {
1042   register int i = 0;
1043   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
1044     {
1045       i++;
1046     }
1047   return i;
1048 }
1049 \f
1050
1051 /* Increase the space allocated for LISTP, which is probably
1052    global_psymbols or static_psymbols. This space will eventually
1053    be freed in free_objfile().  */
1054
1055 void
1056 extend_psymbol_list (register struct psymbol_allocation_list *listp,
1057                      struct objfile *objfile)
1058 {
1059   int new_size;
1060   if (listp->size == 0)
1061     {
1062       new_size = 255;
1063       listp->list = (struct partial_symbol **)
1064         xmmalloc (objfile->md, new_size * sizeof (struct partial_symbol *));
1065     }
1066   else
1067     {
1068       new_size = listp->size * 2;
1069       listp->list = (struct partial_symbol **)
1070         xmrealloc (objfile->md, (char *) listp->list,
1071                    new_size * sizeof (struct partial_symbol *));
1072     }
1073   /* Next assumes we only went one over.  Should be good if
1074      program works correctly */
1075   listp->next = listp->list + listp->size;
1076   listp->size = new_size;
1077 }
1078
1079
1080 /* Do early runtime initializations. */
1081 void
1082 _initialize_symmisc (void)
1083 {
1084   std_in = stdin;
1085   std_out = stdout;
1086   std_err = stderr;
1087 }