* NEWS: Mention new maintenance commands check-symtabs, and
[platform/upstream/binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup), for GDB.
2
3    Copyright (C) 1986-2013 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "gdbtypes.h"
23 #include "bfd.h"
24 #include "filenames.h"
25 #include "symfile.h"
26 #include "objfiles.h"
27 #include "breakpoint.h"
28 #include "command.h"
29 #include "gdb_obstack.h"
30 #include "exceptions.h"
31 #include "language.h"
32 #include "bcache.h"
33 #include "block.h"
34 #include "gdb_regex.h"
35 #include "gdb_stat.h"
36 #include "dictionary.h"
37 #include "typeprint.h"
38 #include "gdbcmd.h"
39 #include "source.h"
40
41 #include "gdb_string.h"
42 #include "readline/readline.h"
43
44 #include "psymtab.h"
45
46 #ifndef DEV_TTY
47 #define DEV_TTY "/dev/tty"
48 #endif
49
50 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
51    when calling functions that take FILE *'s from the debugger.
52    So we make a variable which has the same value and which is accessible when
53    debugging GDB with itself.  Because stdin et al need not be constants,
54    we initialize them in the _initialize_symmisc function at the bottom
55    of the file.  */
56 FILE *std_in;
57 FILE *std_out;
58 FILE *std_err;
59
60 /* Prototypes for local functions */
61
62 static void dump_symtab (struct objfile *, struct symtab *,
63                          struct ui_file *);
64
65 static void dump_msymbols (struct objfile *, struct ui_file *);
66
67 static void dump_objfile (struct objfile *);
68
69 static int block_depth (struct block *);
70
71 void _initialize_symmisc (void);
72
73 struct print_symbol_args
74   {
75     struct gdbarch *gdbarch;
76     struct symbol *symbol;
77     int depth;
78     struct ui_file *outfile;
79   };
80
81 static int print_symbol (void *);
82 \f
83
84 void
85 print_symbol_bcache_statistics (void)
86 {
87   struct program_space *pspace;
88   struct objfile *objfile;
89
90   ALL_PSPACES (pspace)
91     ALL_PSPACE_OBJFILES (pspace, objfile)
92   {
93     QUIT;
94     printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
95     print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
96                              "partial symbol cache");
97     print_bcache_statistics (objfile->per_bfd->macro_cache,
98                              "preprocessor macro cache");
99     print_bcache_statistics (objfile->per_bfd->filename_cache,
100                              "file name cache");
101   }
102 }
103
104 void
105 print_objfile_statistics (void)
106 {
107   struct program_space *pspace;
108   struct objfile *objfile;
109   struct symtab *s;
110   int i, linetables, blockvectors;
111
112   ALL_PSPACES (pspace)
113     ALL_PSPACE_OBJFILES (pspace, objfile)
114   {
115     QUIT;
116     printf_filtered (_("Statistics for '%s':\n"), objfile->name);
117     if (OBJSTAT (objfile, n_stabs) > 0)
118       printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
119                        OBJSTAT (objfile, n_stabs));
120     if (OBJSTAT (objfile, n_minsyms) > 0)
121       printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
122                        OBJSTAT (objfile, n_minsyms));
123     if (OBJSTAT (objfile, n_psyms) > 0)
124       printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
125                        OBJSTAT (objfile, n_psyms));
126     if (OBJSTAT (objfile, n_syms) > 0)
127       printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
128                        OBJSTAT (objfile, n_syms));
129     if (OBJSTAT (objfile, n_types) > 0)
130       printf_filtered (_("  Number of \"types\" defined: %d\n"),
131                        OBJSTAT (objfile, n_types));
132     if (objfile->sf)
133       objfile->sf->qf->print_stats (objfile);
134     i = linetables = blockvectors = 0;
135     ALL_OBJFILE_SYMTABS (objfile, s)
136       {
137         i++;
138         if (s->linetable != NULL)
139           linetables++;
140         if (s->primary == 1)
141           blockvectors++;
142       }
143     printf_filtered (_("  Number of symbol tables: %d\n"), i);
144     printf_filtered (_("  Number of symbol tables with line tables: %d\n"), 
145                      linetables);
146     printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"), 
147                      blockvectors);
148     
149     if (OBJSTAT (objfile, sz_strtab) > 0)
150       printf_filtered (_("  Space used by a.out string tables: %d\n"),
151                        OBJSTAT (objfile, sz_strtab));
152     printf_filtered (_("  Total memory used for objfile obstack: %d\n"),
153                      obstack_memory_used (&objfile->objfile_obstack));
154     printf_filtered (_("  Total memory used for BFD obstack: %d\n"),
155                      obstack_memory_used (&objfile->per_bfd->storage_obstack));
156     printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
157                      bcache_memory_used (psymbol_bcache_get_bcache
158                                           (objfile->psymbol_cache)));
159     printf_filtered (_("  Total memory used for macro cache: %d\n"),
160                      bcache_memory_used (objfile->per_bfd->macro_cache));
161     printf_filtered (_("  Total memory used for file name cache: %d\n"),
162                      bcache_memory_used (objfile->per_bfd->filename_cache));
163   }
164 }
165
166 static void
167 dump_objfile (struct objfile *objfile)
168 {
169   struct symtab *symtab;
170
171   printf_filtered ("\nObject file %s:  ", objfile->name);
172   printf_filtered ("Objfile at ");
173   gdb_print_host_address (objfile, gdb_stdout);
174   printf_filtered (", bfd at ");
175   gdb_print_host_address (objfile->obfd, gdb_stdout);
176   printf_filtered (", %d minsyms\n\n",
177                    objfile->minimal_symbol_count);
178
179   if (objfile->sf)
180     objfile->sf->qf->dump (objfile);
181
182   if (objfile->symtabs)
183     {
184       printf_filtered ("Symtabs:\n");
185       for (symtab = objfile->symtabs;
186            symtab != NULL;
187            symtab = symtab->next)
188         {
189           printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
190           gdb_print_host_address (symtab, gdb_stdout);
191           printf_filtered (", ");
192           if (symtab->objfile != objfile)
193             {
194               printf_filtered ("NOT ON CHAIN!  ");
195             }
196           wrap_here ("  ");
197         }
198       printf_filtered ("\n\n");
199     }
200 }
201
202 /* Print minimal symbols from this objfile.  */
203
204 static void
205 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
206 {
207   struct gdbarch *gdbarch = get_objfile_arch (objfile);
208   struct minimal_symbol *msymbol;
209   int index;
210   char ms_type;
211
212   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
213   if (objfile->minimal_symbol_count == 0)
214     {
215       fprintf_filtered (outfile, "No minimal symbols found.\n");
216       return;
217     }
218   index = 0;
219   ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
220     {
221       struct obj_section *section = SYMBOL_OBJ_SECTION (objfile, msymbol);
222
223       switch (MSYMBOL_TYPE (msymbol))
224         {
225         case mst_unknown:
226           ms_type = 'u';
227           break;
228         case mst_text:
229           ms_type = 'T';
230           break;
231         case mst_text_gnu_ifunc:
232           ms_type = 'i';
233           break;
234         case mst_solib_trampoline:
235           ms_type = 'S';
236           break;
237         case mst_data:
238           ms_type = 'D';
239           break;
240         case mst_bss:
241           ms_type = 'B';
242           break;
243         case mst_abs:
244           ms_type = 'A';
245           break;
246         case mst_file_text:
247           ms_type = 't';
248           break;
249         case mst_file_data:
250           ms_type = 'd';
251           break;
252         case mst_file_bss:
253           ms_type = 'b';
254           break;
255         default:
256           ms_type = '?';
257           break;
258         }
259       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
260       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
261                       outfile);
262       fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
263       if (section)
264         {
265           if (section->the_bfd_section != NULL)
266             fprintf_filtered (outfile, " section %s",
267                               bfd_section_name (objfile->obfd,
268                                                 section->the_bfd_section));
269           else
270             fprintf_filtered (outfile, " spurious section %ld",
271                               (long) (section - objfile->sections));
272         }
273       if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
274         {
275           fprintf_filtered (outfile, "  %s", SYMBOL_DEMANGLED_NAME (msymbol));
276         }
277       if (msymbol->filename)
278         fprintf_filtered (outfile, "  %s", msymbol->filename);
279       fputs_filtered ("\n", outfile);
280       index++;
281     }
282   if (objfile->minimal_symbol_count != index)
283     {
284       warning (_("internal error:  minimal symbol count %d != %d"),
285                objfile->minimal_symbol_count, index);
286     }
287   fprintf_filtered (outfile, "\n");
288 }
289
290 static void
291 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
292                struct ui_file *outfile)
293 {
294   struct gdbarch *gdbarch = get_objfile_arch (objfile);
295   int i;
296   struct dict_iterator iter;
297   int len;
298   struct linetable *l;
299   struct blockvector *bv;
300   struct symbol *sym;
301   struct block *b;
302   int depth;
303
304   fprintf_filtered (outfile, "\nSymtab for file %s\n",
305                     symtab_to_filename_for_display (symtab));
306   if (symtab->dirname)
307     fprintf_filtered (outfile, "Compilation directory is %s\n",
308                       symtab->dirname);
309   fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
310   gdb_print_host_address (objfile, outfile);
311   fprintf_filtered (outfile, ")\n");
312   fprintf_filtered (outfile, "Language: %s\n",
313                     language_str (symtab->language));
314
315   /* First print the line table.  */
316   l = LINETABLE (symtab);
317   if (l)
318     {
319       fprintf_filtered (outfile, "\nLine table:\n\n");
320       len = l->nitems;
321       for (i = 0; i < len; i++)
322         {
323           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
324           fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
325           fprintf_filtered (outfile, "\n");
326         }
327     }
328   /* Now print the block info, but only for primary symtabs since we will
329      print lots of duplicate info otherwise.  */
330   if (symtab->primary)
331     {
332       fprintf_filtered (outfile, "\nBlockvector:\n\n");
333       bv = BLOCKVECTOR (symtab);
334       len = BLOCKVECTOR_NBLOCKS (bv);
335       for (i = 0; i < len; i++)
336         {
337           b = BLOCKVECTOR_BLOCK (bv, i);
338           depth = block_depth (b) * 2;
339           print_spaces (depth, outfile);
340           fprintf_filtered (outfile, "block #%03d, object at ", i);
341           gdb_print_host_address (b, outfile);
342           if (BLOCK_SUPERBLOCK (b))
343             {
344               fprintf_filtered (outfile, " under ");
345               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
346             }
347           /* drow/2002-07-10: We could save the total symbols count
348              even if we're using a hashtable, but nothing else but this message
349              wants it.  */
350           fprintf_filtered (outfile, ", %d syms/buckets in ",
351                             dict_size (BLOCK_DICT (b)));
352           fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
353           fprintf_filtered (outfile, "..");
354           fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
355           if (BLOCK_FUNCTION (b))
356             {
357               fprintf_filtered (outfile, ", function %s",
358                                 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
359               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
360                 {
361                   fprintf_filtered (outfile, ", %s",
362                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
363                 }
364             }
365           fprintf_filtered (outfile, "\n");
366           /* Now print each symbol in this block (in no particular order, if
367              we're using a hashtable).  Note that we only want this
368              block, not any blocks from included symtabs.  */
369           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
370             {
371               struct print_symbol_args s;
372
373               s.gdbarch = gdbarch;
374               s.symbol = sym;
375               s.depth = depth + 1;
376               s.outfile = outfile;
377               catch_errors (print_symbol, &s, "Error printing symbol:\n",
378                             RETURN_MASK_ERROR);
379             }
380         }
381       fprintf_filtered (outfile, "\n");
382     }
383   else
384     {
385       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
386     }
387 }
388
389 static void
390 dump_symtab (struct objfile *objfile, struct symtab *symtab,
391              struct ui_file *outfile)
392 {
393   /* Set the current language to the language of the symtab we're dumping
394      because certain routines used during dump_symtab() use the current
395      language to print an image of the symbol.  We'll restore it later.
396      But use only real languages, not placeholders.  */
397   if (symtab->language != language_unknown
398       && symtab->language != language_auto)
399     {
400       enum language saved_lang;
401
402       saved_lang = set_language (symtab->language);
403
404       dump_symtab_1 (objfile, symtab, outfile);
405
406       set_language (saved_lang);
407     }
408   else
409     dump_symtab_1 (objfile, symtab, outfile);
410 }
411
412 static void
413 maintenance_print_symbols (char *args, int from_tty)
414 {
415   char **argv;
416   struct ui_file *outfile;
417   struct cleanup *cleanups;
418   char *symname = NULL;
419   char *filename = DEV_TTY;
420   struct objfile *objfile;
421   struct symtab *s;
422
423   dont_repeat ();
424
425   if (args == NULL)
426     {
427       error (_("Arguments missing: an output file name "
428                "and an optional symbol file name"));
429     }
430   argv = gdb_buildargv (args);
431   cleanups = make_cleanup_freeargv (argv);
432
433   if (argv[0] != NULL)
434     {
435       filename = argv[0];
436       /* If a second arg is supplied, it is a source file name to match on.  */
437       if (argv[1] != NULL)
438         {
439           symname = argv[1];
440         }
441     }
442
443   filename = tilde_expand (filename);
444   make_cleanup (xfree, filename);
445
446   outfile = gdb_fopen (filename, FOPEN_WT);
447   if (outfile == 0)
448     perror_with_name (filename);
449   make_cleanup_ui_file_delete (outfile);
450
451   ALL_SYMTABS (objfile, s)
452     {
453       QUIT;
454       if (symname == NULL
455           || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
456         dump_symtab (objfile, s, outfile);
457     }
458   do_cleanups (cleanups);
459 }
460
461 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
462    far to indent.  ARGS is really a struct print_symbol_args *, but is
463    declared as char * to get it past catch_errors.  Returns 0 for error,
464    1 for success.  */
465
466 static int
467 print_symbol (void *args)
468 {
469   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
470   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
471   int depth = ((struct print_symbol_args *) args)->depth;
472   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
473   struct obj_section *section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (symbol),
474                                                     symbol);
475
476   print_spaces (depth, outfile);
477   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
478     {
479       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
480       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
481                       outfile);
482       if (section)
483         fprintf_filtered (outfile, " section %s\n",
484                           bfd_section_name (section->the_bfd_section->owner,
485                                             section->the_bfd_section));
486       else
487         fprintf_filtered (outfile, "\n");
488       return 1;
489     }
490   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
491     {
492       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
493         {
494           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
495                          &type_print_raw_options);
496         }
497       else
498         {
499           fprintf_filtered (outfile, "%s %s = ",
500                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
501                           ? "enum"
502                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
503                         ? "struct" : "union")),
504                             SYMBOL_LINKAGE_NAME (symbol));
505           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
506                          &type_print_raw_options);
507         }
508       fprintf_filtered (outfile, ";\n");
509     }
510   else
511     {
512       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
513         fprintf_filtered (outfile, "typedef ");
514       if (SYMBOL_TYPE (symbol))
515         {
516           /* Print details of types, except for enums where it's clutter.  */
517           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
518                          outfile,
519                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
520                          depth,
521                          &type_print_raw_options);
522           fprintf_filtered (outfile, "; ");
523         }
524       else
525         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
526
527       switch (SYMBOL_CLASS (symbol))
528         {
529         case LOC_CONST:
530           fprintf_filtered (outfile, "const %s (%s)",
531                             plongest (SYMBOL_VALUE (symbol)),
532                             hex_string (SYMBOL_VALUE (symbol)));
533           break;
534
535         case LOC_CONST_BYTES:
536           {
537             unsigned i;
538             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
539
540             fprintf_filtered (outfile, "const %u hex bytes:",
541                               TYPE_LENGTH (type));
542             for (i = 0; i < TYPE_LENGTH (type); i++)
543               fprintf_filtered (outfile, " %02x",
544                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
545           }
546           break;
547
548         case LOC_STATIC:
549           fprintf_filtered (outfile, "static at ");
550           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
551                           outfile);
552           if (section)
553             fprintf_filtered (outfile, " section %s",
554                               bfd_section_name (section->the_bfd_section->owner,
555                                                 section->the_bfd_section));
556           break;
557
558         case LOC_REGISTER:
559           if (SYMBOL_IS_ARGUMENT (symbol))
560             fprintf_filtered (outfile, "parameter register %s",
561                               plongest (SYMBOL_VALUE (symbol)));
562           else
563             fprintf_filtered (outfile, "register %s",
564                               plongest (SYMBOL_VALUE (symbol)));
565           break;
566
567         case LOC_ARG:
568           fprintf_filtered (outfile, "arg at offset %s",
569                             hex_string (SYMBOL_VALUE (symbol)));
570           break;
571
572         case LOC_REF_ARG:
573           fprintf_filtered (outfile, "reference arg at %s",
574                             hex_string (SYMBOL_VALUE (symbol)));
575           break;
576
577         case LOC_REGPARM_ADDR:
578           fprintf_filtered (outfile, "address parameter register %s",
579                             plongest (SYMBOL_VALUE (symbol)));
580           break;
581
582         case LOC_LOCAL:
583           fprintf_filtered (outfile, "local at offset %s",
584                             hex_string (SYMBOL_VALUE (symbol)));
585           break;
586
587         case LOC_TYPEDEF:
588           break;
589
590         case LOC_LABEL:
591           fprintf_filtered (outfile, "label at ");
592           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
593                           outfile);
594           if (section)
595             fprintf_filtered (outfile, " section %s",
596                               bfd_section_name (section->the_bfd_section->owner,
597                                                 section->the_bfd_section));
598           break;
599
600         case LOC_BLOCK:
601           fprintf_filtered (outfile, "block object ");
602           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
603           fprintf_filtered (outfile, ", ");
604           fputs_filtered (paddress (gdbarch,
605                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
606                           outfile);
607           fprintf_filtered (outfile, "..");
608           fputs_filtered (paddress (gdbarch,
609                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
610                           outfile);
611           if (section)
612             fprintf_filtered (outfile, " section %s",
613                               bfd_section_name (section->the_bfd_section->owner,
614                                                 section->the_bfd_section));
615           break;
616
617         case LOC_COMPUTED:
618           fprintf_filtered (outfile, "computed at runtime");
619           break;
620
621         case LOC_UNRESOLVED:
622           fprintf_filtered (outfile, "unresolved");
623           break;
624
625         case LOC_OPTIMIZED_OUT:
626           fprintf_filtered (outfile, "optimized out");
627           break;
628
629         default:
630           fprintf_filtered (outfile, "botched symbol class %x",
631                             SYMBOL_CLASS (symbol));
632           break;
633         }
634     }
635   fprintf_filtered (outfile, "\n");
636   return 1;
637 }
638
639 static void
640 maintenance_print_msymbols (char *args, int from_tty)
641 {
642   char **argv;
643   struct ui_file *outfile;
644   struct cleanup *cleanups;
645   char *filename = DEV_TTY;
646   char *symname = NULL;
647   struct program_space *pspace;
648   struct objfile *objfile;
649
650   struct stat sym_st, obj_st;
651
652   dont_repeat ();
653
654   if (args == NULL)
655     {
656       error (_("print-msymbols takes an output file "
657                "name and optional symbol file name"));
658     }
659   argv = gdb_buildargv (args);
660   cleanups = make_cleanup_freeargv (argv);
661
662   if (argv[0] != NULL)
663     {
664       filename = argv[0];
665       /* If a second arg is supplied, it is a source file name to match on.  */
666       if (argv[1] != NULL)
667         {
668           symname = gdb_realpath (argv[1]);
669           make_cleanup (xfree, symname);
670           if (symname && stat (symname, &sym_st))
671             perror_with_name (symname);
672         }
673     }
674
675   filename = tilde_expand (filename);
676   make_cleanup (xfree, filename);
677
678   outfile = gdb_fopen (filename, FOPEN_WT);
679   if (outfile == 0)
680     perror_with_name (filename);
681   make_cleanup_ui_file_delete (outfile);
682
683   ALL_PSPACES (pspace)
684     ALL_PSPACE_OBJFILES (pspace, objfile)
685       {
686         QUIT;
687         if (symname == NULL || (!stat (objfile->name, &obj_st)
688                                 && sym_st.st_dev == obj_st.st_dev
689                                 && sym_st.st_ino == obj_st.st_ino))
690           dump_msymbols (objfile, outfile);
691       }
692   fprintf_filtered (outfile, "\n\n");
693   do_cleanups (cleanups);
694 }
695
696 static void
697 maintenance_print_objfiles (char *ignore, int from_tty)
698 {
699   struct program_space *pspace;
700   struct objfile *objfile;
701
702   dont_repeat ();
703
704   ALL_PSPACES (pspace)
705     ALL_PSPACE_OBJFILES (pspace, objfile)
706       {
707         QUIT;
708         dump_objfile (objfile);
709       }
710 }
711
712 /* List all the symbol tables whose names match REGEXP (optional).  */
713
714 static void
715 maintenance_info_symtabs (char *regexp, int from_tty)
716 {
717   struct program_space *pspace;
718   struct objfile *objfile;
719
720   if (regexp)
721     re_comp (regexp);
722
723   ALL_PSPACES (pspace)
724     ALL_PSPACE_OBJFILES (pspace, objfile)
725     {
726       struct symtab *symtab;
727       
728       /* We don't want to print anything for this objfile until we
729          actually find a symtab whose name matches.  */
730       int printed_objfile_start = 0;
731
732       ALL_OBJFILE_SYMTABS (objfile, symtab)
733         {
734           QUIT;
735
736           if (! regexp
737               || re_exec (symtab_to_filename_for_display (symtab)))
738             {
739               if (! printed_objfile_start)
740                 {
741                   printf_filtered ("{ objfile %s ", objfile->name);
742                   wrap_here ("  ");
743                   printf_filtered ("((struct objfile *) %s)\n", 
744                                    host_address_to_string (objfile));
745                   printed_objfile_start = 1;
746                 }
747
748               printf_filtered ("        { symtab %s ",
749                                symtab_to_filename_for_display (symtab));
750               wrap_here ("    ");
751               printf_filtered ("((struct symtab *) %s)\n", 
752                                host_address_to_string (symtab));
753               printf_filtered ("          dirname %s\n",
754                                symtab->dirname ? symtab->dirname : "(null)");
755               printf_filtered ("          fullname %s\n",
756                                symtab->fullname ? symtab->fullname : "(null)");
757               printf_filtered ("          "
758                                "blockvector ((struct blockvector *) %s)%s\n",
759                                host_address_to_string (symtab->blockvector),
760                                symtab->primary ? " (primary)" : "");
761               printf_filtered ("          "
762                                "linetable ((struct linetable *) %s)\n",
763                                host_address_to_string (symtab->linetable));
764               printf_filtered ("          debugformat %s\n",
765                                symtab->debugformat);
766               printf_filtered ("        }\n");
767             }
768         }
769
770       if (printed_objfile_start)
771         printf_filtered ("}\n");
772     }
773 }
774
775 /* Check consistency of symtabs.
776    An example of what this checks for is NULL blockvectors.
777    They can happen if there's a bug during debug info reading.
778    GDB assumes they are always non-NULL.
779
780    Note: This does not check for psymtab vs symtab consistency.
781    Use "maint check-psymtabs" for that.  */
782
783 static void
784 maintenance_check_symtabs (char *ignore, int from_tty)
785 {
786   struct program_space *pspace;
787   struct objfile *objfile;
788
789   ALL_PSPACES (pspace)
790     ALL_PSPACE_OBJFILES (pspace, objfile)
791     {
792       struct symtab *symtab;
793
794       /* We don't want to print anything for this objfile until we
795          actually find something worth printing.  */
796       int printed_objfile_start = 0;
797
798       ALL_OBJFILE_SYMTABS (objfile, symtab)
799         {
800           int found_something = 0;
801
802           QUIT;
803
804           if (symtab->blockvector == NULL)
805             found_something = 1;
806           /* Add more checks here.  */
807
808           if (found_something)
809             {
810               if (! printed_objfile_start)
811                 {
812                   printf_filtered ("{ objfile %s ", objfile->name);
813                   wrap_here ("  ");
814                   printf_filtered ("((struct objfile *) %s)\n", 
815                                    host_address_to_string (objfile));
816                   printed_objfile_start = 1;
817                 }
818               printf_filtered ("  { symtab %s\n",
819                                symtab_to_filename_for_display (symtab));
820               if (symtab->blockvector == NULL)
821                 printf_filtered ("    NULL blockvector\n");
822               printf_filtered ("  }\n");
823             }
824         }
825
826       if (printed_objfile_start)
827         printf_filtered ("}\n");
828     }
829 }
830
831 /* Helper function for maintenance_expand_symtabs.
832    This is the name_matcher function for expand_symtabs_matching.  */
833
834 static int
835 maintenance_expand_name_matcher (const char *symname, void *data)
836 {
837   /* Since we're not searching on symbols, just return TRUE.  */
838   return 1;
839 }
840
841 /* Helper function for maintenance_expand_symtabs.
842    This is the file_matcher function for expand_symtabs_matching.  */
843
844 static int
845 maintenance_expand_file_matcher (const char *filename, void *data,
846                                  int basenames)
847 {
848   const char *regexp = data;
849
850   QUIT;
851
852   /* KISS: Only apply the regexp to the complete file name.  */
853   if (basenames)
854     return 0;
855
856   if (regexp == NULL || re_exec (filename))
857     return 1;
858
859   return 0;
860 }
861
862 /* Expand all symbol tables whose name matches an optional regexp.  */
863
864 static void
865 maintenance_expand_symtabs (char *args, int from_tty)
866 {
867   struct program_space *pspace;
868   struct objfile *objfile;
869   struct cleanup *cleanups;
870   char **argv;
871   char *regexp = NULL;
872
873   /* We use buildargv here so that we handle spaces in the regexp
874      in a way that allows adding more arguments later.  */
875   argv = gdb_buildargv (args);
876   cleanups = make_cleanup_freeargv (argv);
877
878   if (argv != NULL)
879     {
880       if (argv[0] != NULL)
881         {
882           regexp = argv[0];
883           if (argv[1] != NULL)
884             error (_("Extra arguments after regexp."));
885         }
886     }
887
888   if (regexp)
889     re_comp (regexp);
890
891   ALL_PSPACES (pspace)
892     ALL_PSPACE_OBJFILES (pspace, objfile)
893     {
894       if (objfile->sf)
895         {
896           objfile->sf->qf->expand_symtabs_matching
897             (objfile, maintenance_expand_file_matcher,
898              maintenance_expand_name_matcher, ALL_DOMAIN, regexp);
899         }
900     }
901 }
902 \f
903
904 /* Return the nexting depth of a block within other blocks in its symtab.  */
905
906 static int
907 block_depth (struct block *block)
908 {
909   int i = 0;
910
911   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
912     {
913       i++;
914     }
915   return i;
916 }
917 \f
918
919 /* Do early runtime initializations.  */
920
921 void
922 _initialize_symmisc (void)
923 {
924   std_in = stdin;
925   std_out = stdout;
926   std_err = stderr;
927
928   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
929 Print dump of current symbol definitions.\n\
930 Entries in the full symbol table are dumped to file OUTFILE.\n\
931 If a SOURCE file is specified, dump only that file's symbols."),
932            &maintenanceprintlist);
933
934   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
935 Print dump of current minimal symbol definitions.\n\
936 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
937 If a SOURCE file is specified, dump only that file's minimal symbols."),
938            &maintenanceprintlist);
939
940   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
941            _("Print dump of current object file definitions."),
942            &maintenanceprintlist);
943
944   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
945 List the full symbol tables for all object files.\n\
946 This does not include information about individual symbols, blocks, or\n\
947 linetables --- just the symbol table structures themselves.\n\
948 With an argument REGEXP, list the symbol tables whose names that match that."),
949            &maintenanceinfolist);
950
951   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
952            _("\
953 Check consistency of currently expanded symtabs."),
954            &maintenancelist);
955
956   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
957            _("Expand symbol tables.\n\
958 With an argument REGEXP, only expand the symbol tables with matching names."),
959            &maintenancelist);
960 }