symmisc.c (dump_symtab_1, dump_symtab): Delete arg objfile.
[external/binutils.git] / gdb / symmisc.c
1 /* Do various things to symbol tables (other than lookup), for GDB.
2
3    Copyright (C) 1986-2016 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 symtab *symtab, struct ui_file *outfile)
286 {
287   struct objfile *objfile = SYMTAB_OBJFILE (symtab);
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 symtab *symtab, struct ui_file *outfile)
386 {
387   /* Set the current language to the language of the symtab we're dumping
388      because certain routines used during dump_symtab() use the current
389      language to print an image of the symbol.  We'll restore it later.
390      But use only real languages, not placeholders.  */
391   if (symtab->language != language_unknown
392       && symtab->language != language_auto)
393     {
394       enum language saved_lang;
395
396       saved_lang = set_language (symtab->language);
397
398       dump_symtab_1 (symtab, outfile);
399
400       set_language (saved_lang);
401     }
402   else
403     dump_symtab_1 (symtab, outfile);
404 }
405
406 static void
407 maintenance_print_symbols (char *args, int from_tty)
408 {
409   char **argv;
410   struct ui_file *outfile;
411   struct cleanup *cleanups;
412   char *symname = NULL;
413   char *filename = DEV_TTY;
414   struct objfile *objfile;
415   struct compunit_symtab *cu;
416   struct symtab *s;
417
418   dont_repeat ();
419
420   if (args == NULL)
421     {
422       error (_("Arguments missing: an output file name "
423                "and an optional symbol file name"));
424     }
425   argv = gdb_buildargv (args);
426   cleanups = make_cleanup_freeargv (argv);
427
428   if (argv[0] != NULL)
429     {
430       filename = argv[0];
431       /* If a second arg is supplied, it is a source file name to match on.  */
432       if (argv[1] != NULL)
433         {
434           symname = argv[1];
435         }
436     }
437
438   filename = tilde_expand (filename);
439   make_cleanup (xfree, filename);
440
441   outfile = gdb_fopen (filename, FOPEN_WT);
442   if (outfile == 0)
443     perror_with_name (filename);
444   make_cleanup_ui_file_delete (outfile);
445
446   ALL_FILETABS (objfile, cu, s)
447     {
448       QUIT;
449       if (symname == NULL
450           || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
451         dump_symtab (s, outfile);
452     }
453   do_cleanups (cleanups);
454 }
455
456 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
457    far to indent.  ARGS is really a struct print_symbol_args *, but is
458    declared as char * to get it past catch_errors.  Returns 0 for error,
459    1 for success.  */
460
461 static int
462 print_symbol (void *args)
463 {
464   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
465   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
466   int depth = ((struct print_symbol_args *) args)->depth;
467   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
468   struct obj_section *section;
469
470   if (SYMBOL_OBJFILE_OWNED (symbol))
471     section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
472   else
473     section = NULL;
474
475   print_spaces (depth, outfile);
476   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
477     {
478       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
479       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
480                       outfile);
481       if (section)
482         fprintf_filtered (outfile, " section %s\n",
483                           bfd_section_name (section->the_bfd_section->owner,
484                                             section->the_bfd_section));
485       else
486         fprintf_filtered (outfile, "\n");
487       return 1;
488     }
489   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
490     {
491       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
492         {
493           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
494                          &type_print_raw_options);
495         }
496       else
497         {
498           fprintf_filtered (outfile, "%s %s = ",
499                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
500                           ? "enum"
501                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
502                         ? "struct" : "union")),
503                             SYMBOL_LINKAGE_NAME (symbol));
504           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
505                          &type_print_raw_options);
506         }
507       fprintf_filtered (outfile, ";\n");
508     }
509   else
510     {
511       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
512         fprintf_filtered (outfile, "typedef ");
513       if (SYMBOL_TYPE (symbol))
514         {
515           /* Print details of types, except for enums where it's clutter.  */
516           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
517                          outfile,
518                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
519                          depth,
520                          &type_print_raw_options);
521           fprintf_filtered (outfile, "; ");
522         }
523       else
524         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
525
526       switch (SYMBOL_CLASS (symbol))
527         {
528         case LOC_CONST:
529           fprintf_filtered (outfile, "const %s (%s)",
530                             plongest (SYMBOL_VALUE (symbol)),
531                             hex_string (SYMBOL_VALUE (symbol)));
532           break;
533
534         case LOC_CONST_BYTES:
535           {
536             unsigned i;
537             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
538
539             fprintf_filtered (outfile, "const %u hex bytes:",
540                               TYPE_LENGTH (type));
541             for (i = 0; i < TYPE_LENGTH (type); i++)
542               fprintf_filtered (outfile, " %02x",
543                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
544           }
545           break;
546
547         case LOC_STATIC:
548           fprintf_filtered (outfile, "static at ");
549           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
550                           outfile);
551           if (section)
552             fprintf_filtered (outfile, " section %s",
553                               bfd_section_name (section->the_bfd_section->owner,
554                                                 section->the_bfd_section));
555           break;
556
557         case LOC_REGISTER:
558           if (SYMBOL_IS_ARGUMENT (symbol))
559             fprintf_filtered (outfile, "parameter register %s",
560                               plongest (SYMBOL_VALUE (symbol)));
561           else
562             fprintf_filtered (outfile, "register %s",
563                               plongest (SYMBOL_VALUE (symbol)));
564           break;
565
566         case LOC_ARG:
567           fprintf_filtered (outfile, "arg at offset %s",
568                             hex_string (SYMBOL_VALUE (symbol)));
569           break;
570
571         case LOC_REF_ARG:
572           fprintf_filtered (outfile, "reference arg at %s",
573                             hex_string (SYMBOL_VALUE (symbol)));
574           break;
575
576         case LOC_REGPARM_ADDR:
577           fprintf_filtered (outfile, "address parameter register %s",
578                             plongest (SYMBOL_VALUE (symbol)));
579           break;
580
581         case LOC_LOCAL:
582           fprintf_filtered (outfile, "local at offset %s",
583                             hex_string (SYMBOL_VALUE (symbol)));
584           break;
585
586         case LOC_TYPEDEF:
587           break;
588
589         case LOC_LABEL:
590           fprintf_filtered (outfile, "label at ");
591           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
592                           outfile);
593           if (section)
594             fprintf_filtered (outfile, " section %s",
595                               bfd_section_name (section->the_bfd_section->owner,
596                                                 section->the_bfd_section));
597           break;
598
599         case LOC_BLOCK:
600           fprintf_filtered (outfile, "block object ");
601           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
602           fprintf_filtered (outfile, ", ");
603           fputs_filtered (paddress (gdbarch,
604                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
605                           outfile);
606           fprintf_filtered (outfile, "..");
607           fputs_filtered (paddress (gdbarch,
608                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
609                           outfile);
610           if (section)
611             fprintf_filtered (outfile, " section %s",
612                               bfd_section_name (section->the_bfd_section->owner,
613                                                 section->the_bfd_section));
614           break;
615
616         case LOC_COMPUTED:
617           fprintf_filtered (outfile, "computed at runtime");
618           break;
619
620         case LOC_UNRESOLVED:
621           fprintf_filtered (outfile, "unresolved");
622           break;
623
624         case LOC_OPTIMIZED_OUT:
625           fprintf_filtered (outfile, "optimized out");
626           break;
627
628         default:
629           fprintf_filtered (outfile, "botched symbol class %x",
630                             SYMBOL_CLASS (symbol));
631           break;
632         }
633     }
634   fprintf_filtered (outfile, "\n");
635   return 1;
636 }
637
638 static void
639 maintenance_print_msymbols (char *args, int from_tty)
640 {
641   char **argv;
642   struct ui_file *outfile;
643   struct cleanup *cleanups;
644   char *filename = DEV_TTY;
645   char *symname = NULL;
646   struct program_space *pspace;
647   struct objfile *objfile;
648
649   struct stat sym_st, obj_st;
650
651   dont_repeat ();
652
653   if (args == NULL)
654     {
655       error (_("print-msymbols takes an output file "
656                "name and optional symbol file name"));
657     }
658   argv = gdb_buildargv (args);
659   cleanups = make_cleanup_freeargv (argv);
660
661   if (argv[0] != NULL)
662     {
663       filename = argv[0];
664       /* If a second arg is supplied, it is a source file name to match on.  */
665       if (argv[1] != NULL)
666         {
667           symname = gdb_realpath (argv[1]);
668           make_cleanup (xfree, symname);
669           if (symname && stat (symname, &sym_st))
670             perror_with_name (symname);
671         }
672     }
673
674   filename = tilde_expand (filename);
675   make_cleanup (xfree, filename);
676
677   outfile = gdb_fopen (filename, FOPEN_WT);
678   if (outfile == 0)
679     perror_with_name (filename);
680   make_cleanup_ui_file_delete (outfile);
681
682   ALL_PSPACES (pspace)
683     ALL_PSPACE_OBJFILES (pspace, objfile)
684       {
685         QUIT;
686         if (symname == NULL || (!stat (objfile_name (objfile), &obj_st)
687                                 && sym_st.st_dev == obj_st.st_dev
688                                 && sym_st.st_ino == obj_st.st_ino))
689           dump_msymbols (objfile, outfile);
690       }
691   fprintf_filtered (outfile, "\n\n");
692   do_cleanups (cleanups);
693 }
694
695 static void
696 maintenance_print_objfiles (char *regexp, int from_tty)
697 {
698   struct program_space *pspace;
699   struct objfile *objfile;
700
701   dont_repeat ();
702
703   if (regexp)
704     re_comp (regexp);
705
706   ALL_PSPACES (pspace)
707     ALL_PSPACE_OBJFILES (pspace, objfile)
708       {
709         QUIT;
710         if (! regexp
711             || re_exec (objfile_name (objfile)))
712           dump_objfile (objfile);
713       }
714 }
715
716 /* List all the symbol tables whose names match REGEXP (optional).  */
717
718 static void
719 maintenance_info_symtabs (char *regexp, int from_tty)
720 {
721   struct program_space *pspace;
722   struct objfile *objfile;
723
724   dont_repeat ();
725
726   if (regexp)
727     re_comp (regexp);
728
729   ALL_PSPACES (pspace)
730     ALL_PSPACE_OBJFILES (pspace, objfile)
731     {
732       struct compunit_symtab *cust;
733       struct symtab *symtab;
734
735       /* We don't want to print anything for this objfile until we
736          actually find a symtab whose name matches.  */
737       int printed_objfile_start = 0;
738
739       ALL_OBJFILE_COMPUNITS (objfile, cust)
740         {
741           int printed_compunit_symtab_start = 0;
742
743           ALL_COMPUNIT_FILETABS (cust, symtab)
744             {
745               QUIT;
746
747               if (! regexp
748                   || re_exec (symtab_to_filename_for_display (symtab)))
749                 {
750                   if (! printed_objfile_start)
751                     {
752                       printf_filtered ("{ objfile %s ", objfile_name (objfile));
753                       wrap_here ("  ");
754                       printf_filtered ("((struct objfile *) %s)\n",
755                                        host_address_to_string (objfile));
756                       printed_objfile_start = 1;
757                     }
758                   if (! printed_compunit_symtab_start)
759                     {
760                       printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
761                                        host_address_to_string (cust));
762                       printf_filtered ("    debugformat %s\n",
763                                        COMPUNIT_DEBUGFORMAT (cust));
764                       printf_filtered ("    producer %s\n",
765                                        COMPUNIT_PRODUCER (cust) != NULL
766                                        ? COMPUNIT_PRODUCER (cust)
767                                        : "(null)");
768                       printf_filtered ("    dirname %s\n",
769                                        COMPUNIT_DIRNAME (cust) != NULL
770                                        ? COMPUNIT_DIRNAME (cust)
771                                        : "(null)");
772                       printf_filtered ("    blockvector"
773                                        " ((struct blockvector *) %s)\n",
774                                        host_address_to_string
775                                          (COMPUNIT_BLOCKVECTOR (cust)));
776                       printed_compunit_symtab_start = 1;
777                     }
778
779                   printf_filtered ("\t{ symtab %s ",
780                                    symtab_to_filename_for_display (symtab));
781                   wrap_here ("    ");
782                   printf_filtered ("((struct symtab *) %s)\n",
783                                    host_address_to_string (symtab));
784                   printf_filtered ("\t  fullname %s\n",
785                                    symtab->fullname != NULL
786                                    ? symtab->fullname
787                                    : "(null)");
788                   printf_filtered ("\t  "
789                                    "linetable ((struct linetable *) %s)\n",
790                                    host_address_to_string (symtab->linetable));
791                   printf_filtered ("\t}\n");
792                 }
793             }
794
795           if (printed_compunit_symtab_start)
796             printf_filtered ("  }\n");
797         }
798
799       if (printed_objfile_start)
800         printf_filtered ("}\n");
801     }
802 }
803
804 /* Check consistency of symtabs.
805    An example of what this checks for is NULL blockvectors.
806    They can happen if there's a bug during debug info reading.
807    GDB assumes they are always non-NULL.
808
809    Note: This does not check for psymtab vs symtab consistency.
810    Use "maint check-psymtabs" for that.  */
811
812 static void
813 maintenance_check_symtabs (char *ignore, int from_tty)
814 {
815   struct program_space *pspace;
816   struct objfile *objfile;
817
818   ALL_PSPACES (pspace)
819     ALL_PSPACE_OBJFILES (pspace, objfile)
820     {
821       struct compunit_symtab *cust;
822
823       /* We don't want to print anything for this objfile until we
824          actually find something worth printing.  */
825       int printed_objfile_start = 0;
826
827       ALL_OBJFILE_COMPUNITS (objfile, cust)
828         {
829           int found_something = 0;
830           struct symtab *symtab = compunit_primary_filetab (cust);
831
832           QUIT;
833
834           if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
835             found_something = 1;
836           /* Add more checks here.  */
837
838           if (found_something)
839             {
840               if (! printed_objfile_start)
841                 {
842                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
843                   wrap_here ("  ");
844                   printf_filtered ("((struct objfile *) %s)\n",
845                                    host_address_to_string (objfile));
846                   printed_objfile_start = 1;
847                 }
848               printf_filtered ("  { symtab %s\n",
849                                symtab_to_filename_for_display (symtab));
850               if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
851                 printf_filtered ("    NULL blockvector\n");
852               printf_filtered ("  }\n");
853             }
854         }
855
856       if (printed_objfile_start)
857         printf_filtered ("}\n");
858     }
859 }
860
861 /* Helper function for maintenance_expand_symtabs.
862    This is the name_matcher function for expand_symtabs_matching.  */
863
864 static int
865 maintenance_expand_name_matcher (const char *symname, void *data)
866 {
867   /* Since we're not searching on symbols, just return TRUE.  */
868   return 1;
869 }
870
871 /* Helper function for maintenance_expand_symtabs.
872    This is the file_matcher function for expand_symtabs_matching.  */
873
874 static int
875 maintenance_expand_file_matcher (const char *filename, void *data,
876                                  int basenames)
877 {
878   const char *regexp = (const char *) data;
879
880   QUIT;
881
882   /* KISS: Only apply the regexp to the complete file name.  */
883   if (basenames)
884     return 0;
885
886   if (regexp == NULL || re_exec (filename))
887     return 1;
888
889   return 0;
890 }
891
892 /* Expand all symbol tables whose name matches an optional regexp.  */
893
894 static void
895 maintenance_expand_symtabs (char *args, int from_tty)
896 {
897   struct program_space *pspace;
898   struct objfile *objfile;
899   struct cleanup *cleanups;
900   char **argv;
901   char *regexp = NULL;
902
903   /* We use buildargv here so that we handle spaces in the regexp
904      in a way that allows adding more arguments later.  */
905   argv = gdb_buildargv (args);
906   cleanups = make_cleanup_freeargv (argv);
907
908   if (argv != NULL)
909     {
910       if (argv[0] != NULL)
911         {
912           regexp = argv[0];
913           if (argv[1] != NULL)
914             error (_("Extra arguments after regexp."));
915         }
916     }
917
918   if (regexp)
919     re_comp (regexp);
920
921   ALL_PSPACES (pspace)
922     ALL_PSPACE_OBJFILES (pspace, objfile)
923     {
924       if (objfile->sf)
925         {
926           objfile->sf->qf->expand_symtabs_matching
927             (objfile, maintenance_expand_file_matcher,
928              maintenance_expand_name_matcher, NULL, ALL_DOMAIN, regexp);
929         }
930     }
931
932   do_cleanups (cleanups);
933 }
934 \f
935
936 /* Return the nexting depth of a block within other blocks in its symtab.  */
937
938 static int
939 block_depth (struct block *block)
940 {
941   int i = 0;
942
943   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
944     {
945       i++;
946     }
947   return i;
948 }
949 \f
950
951 /* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
952    single line table.  */
953
954 static int
955 maintenance_print_one_line_table (struct symtab *symtab, void *data)
956 {
957   struct linetable *linetable;
958   struct objfile *objfile;
959
960   objfile = symtab->compunit_symtab->objfile;
961   printf_filtered (_("objfile: %s ((struct objfile *) %s)\n"),
962                    objfile_name (objfile),
963                    host_address_to_string (objfile));
964   printf_filtered (_("compunit_symtab: ((struct compunit_symtab *) %s)\n"),
965                    host_address_to_string (symtab->compunit_symtab));
966   printf_filtered (_("symtab: %s ((struct symtab *) %s)\n"),
967                    symtab_to_fullname (symtab),
968                    host_address_to_string (symtab));
969   linetable = SYMTAB_LINETABLE (symtab);
970   printf_filtered (_("linetable: ((struct linetable *) %s):\n"),
971                    host_address_to_string (linetable));
972
973   if (linetable == NULL)
974     printf_filtered (_("No line table.\n"));
975   else if (linetable->nitems <= 0)
976     printf_filtered (_("Line table has no lines.\n"));
977   else
978     {
979       int i;
980
981       /* Leave space for 6 digits of index and line number.  After that the
982          tables will just not format as well.  */
983       printf_filtered (_("%-6s %6s %s\n"),
984                        _("INDEX"), _("LINE"), _("ADDRESS"));
985
986       for (i = 0; i < linetable->nitems; ++i)
987         {
988           struct linetable_entry *item;
989           struct cleanup *row_cleanup;
990
991           item = &linetable->item [i];
992           printf_filtered (_("%-6d %6d %s\n"), i, item->line,
993                            core_addr_to_string (item->pc));
994         }
995     }
996
997   return 0;
998 }
999
1000 /* Implement the 'maint info line-table' command.  */
1001
1002 static void
1003 maintenance_info_line_tables (char *regexp, int from_tty)
1004 {
1005   struct program_space *pspace;
1006   struct objfile *objfile;
1007
1008   dont_repeat ();
1009
1010   if (regexp != NULL)
1011     re_comp (regexp);
1012
1013   ALL_PSPACES (pspace)
1014     ALL_PSPACE_OBJFILES (pspace, objfile)
1015     {
1016       struct compunit_symtab *cust;
1017       struct symtab *symtab;
1018
1019       ALL_OBJFILE_COMPUNITS (objfile, cust)
1020         {
1021           ALL_COMPUNIT_FILETABS (cust, symtab)
1022             {
1023               QUIT;
1024
1025               if (regexp == NULL
1026                   || re_exec (symtab_to_filename_for_display (symtab)))
1027                 maintenance_print_one_line_table (symtab, NULL);
1028             }
1029         }
1030     }
1031 }
1032
1033 \f
1034
1035 /* Do early runtime initializations.  */
1036
1037 void
1038 _initialize_symmisc (void)
1039 {
1040   std_in = stdin;
1041   std_out = stdout;
1042   std_err = stderr;
1043
1044   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1045 Print dump of current symbol definitions.\n\
1046 Entries in the full symbol table are dumped to file OUTFILE.\n\
1047 If a SOURCE file is specified, dump only that file's symbols."),
1048            &maintenanceprintlist);
1049
1050   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1051 Print dump of current minimal symbol definitions.\n\
1052 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
1053 If a SOURCE file is specified, dump only that file's minimal symbols."),
1054            &maintenanceprintlist);
1055
1056   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
1057            _("Print dump of current object file definitions.\n\
1058 With an argument REGEXP, list the object files with matching names."),
1059            &maintenanceprintlist);
1060
1061   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1062 List the full symbol tables for all object files.\n\
1063 This does not include information about individual symbols, blocks, or\n\
1064 linetables --- just the symbol table structures themselves.\n\
1065 With an argument REGEXP, list the symbol tables with matching names."),
1066            &maintenanceinfolist);
1067
1068   add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1069 List the contents of all line tables, from all symbol tables.\n\
1070 With an argument REGEXP, list just the line tables for the symbol\n\
1071 tables with matching names."),
1072            &maintenanceinfolist);
1073
1074   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1075            _("\
1076 Check consistency of currently expanded symtabs."),
1077            &maintenancelist);
1078
1079   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1080            _("Expand symbol tables.\n\
1081 With an argument REGEXP, only expand the symbol tables with matching names."),
1082            &maintenancelist);
1083 }