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