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