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