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