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