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