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