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