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