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