Split struct symtab into two: struct symtab and compunit_symtab.
[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 "language.h"
31 #include "bcache.h"
32 #include "block.h"
33 #include "gdb_regex.h"
34 #include <sys/stat.h>
35 #include "dictionary.h"
36 #include "typeprint.h"
37 #include "gdbcmd.h"
38 #include "source.h"
39 #include "readline/readline.h"
40
41 #include "psymtab.h"
42
43 #ifndef DEV_TTY
44 #define DEV_TTY "/dev/tty"
45 #endif
46
47 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
48    when calling functions that take FILE *'s from the debugger.
49    So we make a variable which has the same value and which is accessible when
50    debugging GDB with itself.  Because stdin et al need not be constants,
51    we initialize them in the _initialize_symmisc function at the bottom
52    of the file.  */
53 FILE *std_in;
54 FILE *std_out;
55 FILE *std_err;
56
57 /* Prototypes for local functions */
58
59 static int block_depth (struct block *);
60
61 void _initialize_symmisc (void);
62
63 struct print_symbol_args
64   {
65     struct gdbarch *gdbarch;
66     struct symbol *symbol;
67     int depth;
68     struct ui_file *outfile;
69   };
70
71 static int print_symbol (void *);
72 \f
73
74 void
75 print_symbol_bcache_statistics (void)
76 {
77   struct program_space *pspace;
78   struct objfile *objfile;
79
80   ALL_PSPACES (pspace)
81     ALL_PSPACE_OBJFILES (pspace, objfile)
82   {
83     QUIT;
84     printf_filtered (_("Byte cache statistics for '%s':\n"),
85                      objfile_name (objfile));
86     print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
87                              "partial symbol cache");
88     print_bcache_statistics (objfile->per_bfd->macro_cache,
89                              "preprocessor macro cache");
90     print_bcache_statistics (objfile->per_bfd->filename_cache,
91                              "file name cache");
92   }
93 }
94
95 void
96 print_objfile_statistics (void)
97 {
98   struct program_space *pspace;
99   struct objfile *objfile;
100   struct compunit_symtab *cu;
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_FILETABS (objfile, cu, s)
128       {
129         i++;
130         if (SYMTAB_LINETABLE (s) != NULL)
131           linetables++;
132       }
133     ALL_OBJFILE_COMPUNITS (objfile, cu)
134       blockvectors++;
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 compunit_symtab *cust;
164   struct symtab *symtab;
165
166   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
167   printf_filtered ("Objfile at ");
168   gdb_print_host_address (objfile, gdb_stdout);
169   printf_filtered (", bfd at ");
170   gdb_print_host_address (objfile->obfd, gdb_stdout);
171   printf_filtered (", %d minsyms\n\n",
172                    objfile->per_bfd->minimal_symbol_count);
173
174   if (objfile->sf)
175     objfile->sf->qf->dump (objfile);
176
177   if (objfile->compunit_symtabs != NULL)
178     {
179       printf_filtered ("Symtabs:\n");
180       ALL_OBJFILE_FILETABS (objfile, cust, symtab)
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 (symtab) != 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 (objfile));
206   if (objfile->per_bfd->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 = MSYMBOL_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, MSYMBOL_VALUE_ADDRESS (objfile,
254                                                                 msymbol)),
255                       outfile);
256       fprintf_filtered (outfile, " %s", MSYMBOL_LINKAGE_NAME (msymbol));
257       if (section)
258         {
259           if (section->the_bfd_section != NULL)
260             fprintf_filtered (outfile, " section %s",
261                               bfd_section_name (objfile->obfd,
262                                                 section->the_bfd_section));
263           else
264             fprintf_filtered (outfile, " spurious section %ld",
265                               (long) (section - objfile->sections));
266         }
267       if (MSYMBOL_DEMANGLED_NAME (msymbol) != NULL)
268         {
269           fprintf_filtered (outfile, "  %s", MSYMBOL_DEMANGLED_NAME (msymbol));
270         }
271       if (msymbol->filename)
272         fprintf_filtered (outfile, "  %s", msymbol->filename);
273       fputs_filtered ("\n", outfile);
274       index++;
275     }
276   if (objfile->per_bfd->minimal_symbol_count != index)
277     {
278       warning (_("internal error:  minimal symbol count %d != %d"),
279                objfile->per_bfd->minimal_symbol_count, index);
280     }
281   fprintf_filtered (outfile, "\n");
282 }
283
284 static void
285 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
286                struct ui_file *outfile)
287 {
288   struct gdbarch *gdbarch = get_objfile_arch (objfile);
289   int i;
290   struct dict_iterator iter;
291   int len;
292   struct linetable *l;
293   const struct blockvector *bv;
294   struct symbol *sym;
295   struct block *b;
296   int depth;
297
298   fprintf_filtered (outfile, "\nSymtab for file %s\n",
299                     symtab_to_filename_for_display (symtab));
300   if (SYMTAB_DIRNAME (symtab) != NULL)
301     fprintf_filtered (outfile, "Compilation directory is %s\n",
302                       SYMTAB_DIRNAME (symtab));
303   fprintf_filtered (outfile, "Read from object file %s (",
304                     objfile_name (objfile));
305   gdb_print_host_address (objfile, outfile);
306   fprintf_filtered (outfile, ")\n");
307   fprintf_filtered (outfile, "Language: %s\n",
308                     language_str (symtab->language));
309
310   /* First print the line table.  */
311   l = SYMTAB_LINETABLE (symtab);
312   if (l)
313     {
314       fprintf_filtered (outfile, "\nLine table:\n\n");
315       len = l->nitems;
316       for (i = 0; i < len; i++)
317         {
318           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
319           fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
320           fprintf_filtered (outfile, "\n");
321         }
322     }
323   /* Now print the block info, but only for compunit symtabs since we will
324      print lots of duplicate info otherwise.  */
325   if (symtab == COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)))
326     {
327       fprintf_filtered (outfile, "\nBlockvector:\n\n");
328       bv = SYMTAB_BLOCKVECTOR (symtab);
329       len = BLOCKVECTOR_NBLOCKS (bv);
330       for (i = 0; i < len; i++)
331         {
332           b = BLOCKVECTOR_BLOCK (bv, i);
333           depth = block_depth (b) * 2;
334           print_spaces (depth, outfile);
335           fprintf_filtered (outfile, "block #%03d, object at ", i);
336           gdb_print_host_address (b, outfile);
337           if (BLOCK_SUPERBLOCK (b))
338             {
339               fprintf_filtered (outfile, " under ");
340               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
341             }
342           /* drow/2002-07-10: We could save the total symbols count
343              even if we're using a hashtable, but nothing else but this message
344              wants it.  */
345           fprintf_filtered (outfile, ", %d syms/buckets in ",
346                             dict_size (BLOCK_DICT (b)));
347           fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
348           fprintf_filtered (outfile, "..");
349           fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
350           if (BLOCK_FUNCTION (b))
351             {
352               fprintf_filtered (outfile, ", function %s",
353                                 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
354               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
355                 {
356                   fprintf_filtered (outfile, ", %s",
357                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
358                 }
359             }
360           fprintf_filtered (outfile, "\n");
361           /* Now print each symbol in this block (in no particular order, if
362              we're using a hashtable).  Note that we only want this
363              block, not any blocks from included symtabs.  */
364           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
365             {
366               struct print_symbol_args s;
367
368               s.gdbarch = gdbarch;
369               s.symbol = sym;
370               s.depth = depth + 1;
371               s.outfile = outfile;
372               catch_errors (print_symbol, &s, "Error printing symbol:\n",
373                             RETURN_MASK_ERROR);
374             }
375         }
376       fprintf_filtered (outfile, "\n");
377     }
378   else
379     {
380       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
381     }
382 }
383
384 static void
385 dump_symtab (struct objfile *objfile, struct symtab *symtab,
386              struct ui_file *outfile)
387 {
388   /* Set the current language to the language of the symtab we're dumping
389      because certain routines used during dump_symtab() use the current
390      language to print an image of the symbol.  We'll restore it later.
391      But use only real languages, not placeholders.  */
392   if (symtab->language != language_unknown
393       && symtab->language != language_auto)
394     {
395       enum language saved_lang;
396
397       saved_lang = set_language (symtab->language);
398
399       dump_symtab_1 (objfile, symtab, outfile);
400
401       set_language (saved_lang);
402     }
403   else
404     dump_symtab_1 (objfile, symtab, outfile);
405 }
406
407 static void
408 maintenance_print_symbols (char *args, int from_tty)
409 {
410   char **argv;
411   struct ui_file *outfile;
412   struct cleanup *cleanups;
413   char *symname = NULL;
414   char *filename = DEV_TTY;
415   struct objfile *objfile;
416   struct compunit_symtab *cu;
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_FILETABS (objfile, cu, 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 compunit_symtab *cust;
730       struct symtab *symtab;
731
732       /* We don't want to print anything for this objfile until we
733          actually find a symtab whose name matches.  */
734       int printed_objfile_start = 0;
735
736       ALL_OBJFILE_COMPUNITS (objfile, cust)
737         {
738           int printed_compunit_symtab_start = 0;
739
740           ALL_COMPUNIT_FILETABS (cust, symtab)
741             {
742               QUIT;
743
744               if (! regexp
745                   || re_exec (symtab_to_filename_for_display (symtab)))
746                 {
747                   if (! printed_objfile_start)
748                     {
749                       printf_filtered ("{ objfile %s ", objfile_name (objfile));
750                       wrap_here ("  ");
751                       printf_filtered ("((struct objfile *) %s)\n",
752                                        host_address_to_string (objfile));
753                       printed_objfile_start = 1;
754                     }
755                   if (! printed_compunit_symtab_start)
756                     {
757                       printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
758                                        host_address_to_string (cust));
759                       printf_filtered ("    debugformat %s\n",
760                                        COMPUNIT_DEBUGFORMAT (cust));
761                       printf_filtered ("    producer %s\n",
762                                        COMPUNIT_PRODUCER (cust) != NULL
763                                        ? COMPUNIT_PRODUCER (cust)
764                                        : "(null)");
765                       printf_filtered ("    dirname %s\n",
766                                        COMPUNIT_DIRNAME (cust) != NULL
767                                        ? COMPUNIT_DIRNAME (cust)
768                                        : "(null)");
769                       printf_filtered ("    blockvector"
770                                        " ((struct blockvector *) %s)\n",
771                                        host_address_to_string
772                                          (COMPUNIT_BLOCKVECTOR (cust)));
773                       printed_compunit_symtab_start = 1;
774                     }
775
776                   printf_filtered ("\t{ symtab %s ",
777                                    symtab_to_filename_for_display (symtab));
778                   wrap_here ("    ");
779                   printf_filtered ("((struct symtab *) %s)\n",
780                                    host_address_to_string (symtab));
781                   printf_filtered ("\t  fullname %s\n",
782                                    symtab->fullname != NULL
783                                    ? symtab->fullname
784                                    : "(null)");
785                   printf_filtered ("\t  "
786                                    "linetable ((struct linetable *) %s)\n",
787                                    host_address_to_string (symtab->linetable));
788                   printf_filtered ("\t}\n");
789                 }
790             }
791
792           if (printed_compunit_symtab_start)
793             printf_filtered ("  }\n");
794         }
795
796       if (printed_objfile_start)
797         printf_filtered ("}\n");
798     }
799 }
800
801 /* Check consistency of symtabs.
802    An example of what this checks for is NULL blockvectors.
803    They can happen if there's a bug during debug info reading.
804    GDB assumes they are always non-NULL.
805
806    Note: This does not check for psymtab vs symtab consistency.
807    Use "maint check-psymtabs" for that.  */
808
809 static void
810 maintenance_check_symtabs (char *ignore, int from_tty)
811 {
812   struct program_space *pspace;
813   struct objfile *objfile;
814
815   ALL_PSPACES (pspace)
816     ALL_PSPACE_OBJFILES (pspace, objfile)
817     {
818       struct compunit_symtab *cust;
819
820       /* We don't want to print anything for this objfile until we
821          actually find something worth printing.  */
822       int printed_objfile_start = 0;
823
824       ALL_OBJFILE_COMPUNITS (objfile, cust)
825         {
826           int found_something = 0;
827           struct symtab *symtab = compunit_primary_filetab (cust);
828
829           QUIT;
830
831           if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
832             found_something = 1;
833           /* Add more checks here.  */
834
835           if (found_something)
836             {
837               if (! printed_objfile_start)
838                 {
839                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
840                   wrap_here ("  ");
841                   printf_filtered ("((struct objfile *) %s)\n",
842                                    host_address_to_string (objfile));
843                   printed_objfile_start = 1;
844                 }
845               printf_filtered ("  { symtab %s\n",
846                                symtab_to_filename_for_display (symtab));
847               if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
848                 printf_filtered ("    NULL blockvector\n");
849               printf_filtered ("  }\n");
850             }
851         }
852
853       if (printed_objfile_start)
854         printf_filtered ("}\n");
855     }
856 }
857
858 /* Helper function for maintenance_expand_symtabs.
859    This is the name_matcher function for expand_symtabs_matching.  */
860
861 static int
862 maintenance_expand_name_matcher (const char *symname, void *data)
863 {
864   /* Since we're not searching on symbols, just return TRUE.  */
865   return 1;
866 }
867
868 /* Helper function for maintenance_expand_symtabs.
869    This is the file_matcher function for expand_symtabs_matching.  */
870
871 static int
872 maintenance_expand_file_matcher (const char *filename, void *data,
873                                  int basenames)
874 {
875   const char *regexp = data;
876
877   QUIT;
878
879   /* KISS: Only apply the regexp to the complete file name.  */
880   if (basenames)
881     return 0;
882
883   if (regexp == NULL || re_exec (filename))
884     return 1;
885
886   return 0;
887 }
888
889 /* Expand all symbol tables whose name matches an optional regexp.  */
890
891 static void
892 maintenance_expand_symtabs (char *args, int from_tty)
893 {
894   struct program_space *pspace;
895   struct objfile *objfile;
896   struct cleanup *cleanups;
897   char **argv;
898   char *regexp = NULL;
899
900   /* We use buildargv here so that we handle spaces in the regexp
901      in a way that allows adding more arguments later.  */
902   argv = gdb_buildargv (args);
903   cleanups = make_cleanup_freeargv (argv);
904
905   if (argv != NULL)
906     {
907       if (argv[0] != NULL)
908         {
909           regexp = argv[0];
910           if (argv[1] != NULL)
911             error (_("Extra arguments after regexp."));
912         }
913     }
914
915   if (regexp)
916     re_comp (regexp);
917
918   ALL_PSPACES (pspace)
919     ALL_PSPACE_OBJFILES (pspace, objfile)
920     {
921       if (objfile->sf)
922         {
923           objfile->sf->qf->expand_symtabs_matching
924             (objfile, maintenance_expand_file_matcher,
925              maintenance_expand_name_matcher, ALL_DOMAIN, regexp);
926         }
927     }
928
929   do_cleanups (cleanups);
930 }
931 \f
932
933 /* Return the nexting depth of a block within other blocks in its symtab.  */
934
935 static int
936 block_depth (struct block *block)
937 {
938   int i = 0;
939
940   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
941     {
942       i++;
943     }
944   return i;
945 }
946 \f
947
948 /* Do early runtime initializations.  */
949
950 void
951 _initialize_symmisc (void)
952 {
953   std_in = stdin;
954   std_out = stdout;
955   std_err = stderr;
956
957   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
958 Print dump of current symbol definitions.\n\
959 Entries in the full symbol table are dumped to file OUTFILE.\n\
960 If a SOURCE file is specified, dump only that file's symbols."),
961            &maintenanceprintlist);
962
963   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
964 Print dump of current minimal symbol definitions.\n\
965 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
966 If a SOURCE file is specified, dump only that file's minimal symbols."),
967            &maintenanceprintlist);
968
969   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
970            _("Print dump of current object file definitions.\n\
971 With an argument REGEXP, list the object files with matching names."),
972            &maintenanceprintlist);
973
974   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
975 List the full symbol tables for all object files.\n\
976 This does not include information about individual symbols, blocks, or\n\
977 linetables --- just the symbol table structures themselves.\n\
978 With an argument REGEXP, list the symbol tables with matching names."),
979            &maintenanceinfolist);
980
981   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
982            _("\
983 Check consistency of currently expanded symtabs."),
984            &maintenancelist);
985
986   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
987            _("Expand symbol tables.\n\
988 With an argument REGEXP, only expand the symbol tables with matching names."),
989            &maintenancelist);
990 }