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