Add linux_get_hwcap
[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 (const 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 : pspace->objfiles ())
68       {
69         QUIT;
70         printf_filtered (_("Byte cache statistics for '%s':\n"),
71                          objfile_name (objfile));
72         objfile->partial_symtabs->psymbol_cache.print_statistics
73           ("partial symbol cache");
74         objfile->per_bfd->macro_cache.print_statistics
75           ("preprocessor macro cache");
76         objfile->per_bfd->filename_cache.print_statistics ("file name cache");
77       }
78 }
79
80 void
81 print_objfile_statistics (void)
82 {
83   struct program_space *pspace;
84   int i, linetables, blockvectors;
85
86   ALL_PSPACES (pspace)
87   for (objfile *objfile : pspace->objfiles ())
88     {
89       QUIT;
90       printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
91       if (OBJSTAT (objfile, n_stabs) > 0)
92         printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
93                          OBJSTAT (objfile, n_stabs));
94       if (objfile->per_bfd->n_minsyms > 0)
95         printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
96                          objfile->per_bfd->n_minsyms);
97       if (OBJSTAT (objfile, n_psyms) > 0)
98         printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
99                          OBJSTAT (objfile, n_psyms));
100       if (OBJSTAT (objfile, n_syms) > 0)
101         printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
102                          OBJSTAT (objfile, n_syms));
103       if (OBJSTAT (objfile, n_types) > 0)
104         printf_filtered (_("  Number of \"types\" defined: %d\n"),
105                          OBJSTAT (objfile, n_types));
106       if (objfile->sf)
107         objfile->sf->qf->print_stats (objfile);
108       i = linetables = 0;
109       for (compunit_symtab *cu : objfile->compunits ())
110         {
111           for (symtab *s : compunit_filetabs (cu))
112             {
113               i++;
114               if (SYMTAB_LINETABLE (s) != NULL)
115                 linetables++;
116             }
117         }
118       blockvectors = std::distance (objfile->compunits ().begin (),
119                                     objfile->compunits ().end ());
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
136         (_("  Total memory used for psymbol cache: %d\n"),
137          objfile->partial_symtabs->psymbol_cache.memory_used ());
138       printf_filtered (_("  Total memory used for macro cache: %d\n"),
139                        objfile->per_bfd->macro_cache.memory_used ());
140       printf_filtered (_("  Total memory used for file name cache: %d\n"),
141                        objfile->per_bfd->filename_cache.memory_used ());
142     }
143 }
144
145 static void
146 dump_objfile (struct objfile *objfile)
147 {
148   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
149   printf_filtered ("Objfile at ");
150   gdb_print_host_address (objfile, gdb_stdout);
151   printf_filtered (", bfd at ");
152   gdb_print_host_address (objfile->obfd, gdb_stdout);
153   printf_filtered (", %d minsyms\n\n",
154                    objfile->per_bfd->minimal_symbol_count);
155
156   if (objfile->sf)
157     objfile->sf->qf->dump (objfile);
158
159   if (objfile->compunit_symtabs != NULL)
160     {
161       printf_filtered ("Symtabs:\n");
162       for (compunit_symtab *cu : objfile->compunits ())
163         {
164           for (symtab *symtab : compunit_filetabs (cu))
165             {
166               printf_filtered ("%s at ",
167                                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         }
177       printf_filtered ("\n\n");
178     }
179 }
180
181 /* Print minimal symbols from this objfile.  */
182
183 static void
184 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
185 {
186   struct gdbarch *gdbarch = get_objfile_arch (objfile);
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   for (minimal_symbol *msymbol : objfile->msymbols ())
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 mdict_iterator miter;
277   int len;
278   struct linetable *l;
279   const struct blockvector *bv;
280   struct symbol *sym;
281   const 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                             mdict_size (BLOCK_MULTIDICT (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_MULTIDICT (b), miter, 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       scoped_restore_current_language save_lang;
388       set_language (symtab->language);
389       dump_symtab_1 (symtab, outfile);
390     }
391   else
392     dump_symtab_1 (symtab, outfile);
393 }
394
395 static void
396 maintenance_print_symbols (const char *args, int from_tty)
397 {
398   struct ui_file *outfile = gdb_stdout;
399   char *address_arg = NULL, *source_arg = NULL, *objfile_arg = NULL;
400   int i, outfile_idx;
401
402   dont_repeat ();
403
404   gdb_argv argv (args);
405
406   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
407     {
408       if (strcmp (argv[i], "-pc") == 0)
409         {
410           if (argv[i + 1] == NULL)
411             error (_("Missing pc value"));
412           address_arg = argv[++i];
413         }
414       else if (strcmp (argv[i], "-source") == 0)
415         {
416           if (argv[i + 1] == NULL)
417             error (_("Missing source file"));
418           source_arg = argv[++i];
419         }
420       else if (strcmp (argv[i], "-objfile") == 0)
421         {
422           if (argv[i + 1] == NULL)
423             error (_("Missing objfile name"));
424           objfile_arg = argv[++i];
425         }
426       else if (strcmp (argv[i], "--") == 0)
427         {
428           /* End of options.  */
429           ++i;
430           break;
431         }
432       else if (argv[i][0] == '-')
433         {
434           /* Future proofing: Don't allow OUTFILE to begin with "-".  */
435           error (_("Unknown option: %s"), argv[i]);
436         }
437       else
438         break;
439     }
440   outfile_idx = i;
441
442   if (address_arg != NULL && source_arg != NULL)
443     error (_("Must specify at most one of -pc and -source"));
444
445   stdio_file arg_outfile;
446
447   if (argv != NULL && argv[outfile_idx] != NULL)
448     {
449       if (argv[outfile_idx + 1] != NULL)
450         error (_("Junk at end of command"));
451       gdb::unique_xmalloc_ptr<char> outfile_name
452         (tilde_expand (argv[outfile_idx]));
453       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
454         perror_with_name (outfile_name.get ());
455       outfile = &arg_outfile;
456     }
457
458   if (address_arg != NULL)
459     {
460       CORE_ADDR pc = parse_and_eval_address (address_arg);
461       struct symtab *s = find_pc_line_symtab (pc);
462
463       if (s == NULL)
464         error (_("No symtab for address: %s"), address_arg);
465       dump_symtab (s, outfile);
466     }
467   else
468     {
469       int found = 0;
470
471       for (objfile *objfile : current_program_space->objfiles ())
472         {
473           int print_for_objfile = 1;
474
475           if (objfile_arg != NULL)
476             print_for_objfile
477               = compare_filenames_for_search (objfile_name (objfile),
478                                               objfile_arg);
479           if (!print_for_objfile)
480             continue;
481
482           for (compunit_symtab *cu : objfile->compunits ())
483             {
484               for (symtab *s : compunit_filetabs (cu))
485                 {
486                   int print_for_source = 0;
487
488                   QUIT;
489                   if (source_arg != NULL)
490                     {
491                       print_for_source
492                         = compare_filenames_for_search
493                         (symtab_to_filename_for_display (s), source_arg);
494                       found = 1;
495                     }
496                   if (source_arg == NULL
497                       || print_for_source)
498                     dump_symtab (s, outfile);
499                 }
500             }
501         }
502
503       if (source_arg != NULL && !found)
504         error (_("No symtab for source file: %s"), source_arg);
505     }
506 }
507
508 /* Print symbol SYMBOL on OUTFILE.  DEPTH says how far to indent.  */
509
510 static void
511 print_symbol (struct gdbarch *gdbarch, struct symbol *symbol,
512               int depth, ui_file *outfile)
513 {
514   struct obj_section *section;
515
516   if (SYMBOL_OBJFILE_OWNED (symbol))
517     section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
518   else
519     section = NULL;
520
521   print_spaces (depth, outfile);
522   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
523     {
524       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
525       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
526                       outfile);
527       if (section)
528         fprintf_filtered (outfile, " section %s\n",
529                           bfd_section_name (section->the_bfd_section->owner,
530                                             section->the_bfd_section));
531       else
532         fprintf_filtered (outfile, "\n");
533       return;
534     }
535
536   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
537     {
538       if (TYPE_NAME (SYMBOL_TYPE (symbol)))
539         {
540           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
541                          &type_print_raw_options);
542         }
543       else
544         {
545           fprintf_filtered (outfile, "%s %s = ",
546                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
547                           ? "enum"
548                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
549                         ? "struct" : "union")),
550                             SYMBOL_LINKAGE_NAME (symbol));
551           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
552                          &type_print_raw_options);
553         }
554       fprintf_filtered (outfile, ";\n");
555     }
556   else
557     {
558       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
559         fprintf_filtered (outfile, "typedef ");
560       if (SYMBOL_TYPE (symbol))
561         {
562           /* Print details of types, except for enums where it's clutter.  */
563           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
564                          outfile,
565                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
566                          depth,
567                          &type_print_raw_options);
568           fprintf_filtered (outfile, "; ");
569         }
570       else
571         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
572
573       switch (SYMBOL_CLASS (symbol))
574         {
575         case LOC_CONST:
576           fprintf_filtered (outfile, "const %s (%s)",
577                             plongest (SYMBOL_VALUE (symbol)),
578                             hex_string (SYMBOL_VALUE (symbol)));
579           break;
580
581         case LOC_CONST_BYTES:
582           {
583             unsigned i;
584             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
585
586             fprintf_filtered (outfile, "const %u hex bytes:",
587                               TYPE_LENGTH (type));
588             for (i = 0; i < TYPE_LENGTH (type); i++)
589               fprintf_filtered (outfile, " %02x",
590                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
591           }
592           break;
593
594         case LOC_STATIC:
595           fprintf_filtered (outfile, "static at ");
596           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
597                           outfile);
598           if (section)
599             fprintf_filtered (outfile, " section %s",
600                               bfd_section_name (section->the_bfd_section->owner,
601                                                 section->the_bfd_section));
602           break;
603
604         case LOC_REGISTER:
605           if (SYMBOL_IS_ARGUMENT (symbol))
606             fprintf_filtered (outfile, "parameter register %s",
607                               plongest (SYMBOL_VALUE (symbol)));
608           else
609             fprintf_filtered (outfile, "register %s",
610                               plongest (SYMBOL_VALUE (symbol)));
611           break;
612
613         case LOC_ARG:
614           fprintf_filtered (outfile, "arg at offset %s",
615                             hex_string (SYMBOL_VALUE (symbol)));
616           break;
617
618         case LOC_REF_ARG:
619           fprintf_filtered (outfile, "reference arg at %s",
620                             hex_string (SYMBOL_VALUE (symbol)));
621           break;
622
623         case LOC_REGPARM_ADDR:
624           fprintf_filtered (outfile, "address parameter register %s",
625                             plongest (SYMBOL_VALUE (symbol)));
626           break;
627
628         case LOC_LOCAL:
629           fprintf_filtered (outfile, "local at offset %s",
630                             hex_string (SYMBOL_VALUE (symbol)));
631           break;
632
633         case LOC_TYPEDEF:
634           break;
635
636         case LOC_LABEL:
637           fprintf_filtered (outfile, "label at ");
638           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
639                           outfile);
640           if (section)
641             fprintf_filtered (outfile, " section %s",
642                               bfd_section_name (section->the_bfd_section->owner,
643                                                 section->the_bfd_section));
644           break;
645
646         case LOC_BLOCK:
647           fprintf_filtered (outfile, "block object ");
648           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
649           fprintf_filtered (outfile, ", ");
650           fputs_filtered (paddress (gdbarch,
651                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
652                           outfile);
653           fprintf_filtered (outfile, "..");
654           fputs_filtered (paddress (gdbarch,
655                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
656                           outfile);
657           if (section)
658             fprintf_filtered (outfile, " section %s",
659                               bfd_section_name (section->the_bfd_section->owner,
660                                                 section->the_bfd_section));
661           break;
662
663         case LOC_COMPUTED:
664           fprintf_filtered (outfile, "computed at runtime");
665           break;
666
667         case LOC_UNRESOLVED:
668           fprintf_filtered (outfile, "unresolved");
669           break;
670
671         case LOC_OPTIMIZED_OUT:
672           fprintf_filtered (outfile, "optimized out");
673           break;
674
675         default:
676           fprintf_filtered (outfile, "botched symbol class %x",
677                             SYMBOL_CLASS (symbol));
678           break;
679         }
680     }
681   fprintf_filtered (outfile, "\n");
682 }
683
684 static void
685 maintenance_print_msymbols (const char *args, int from_tty)
686 {
687   struct ui_file *outfile = gdb_stdout;
688   char *objfile_arg = NULL;
689   int i, outfile_idx;
690
691   dont_repeat ();
692
693   gdb_argv argv (args);
694
695   for (i = 0; argv != NULL && argv[i] != NULL; ++i)
696     {
697       if (strcmp (argv[i], "-objfile") == 0)
698         {
699           if (argv[i + 1] == NULL)
700             error (_("Missing objfile name"));
701           objfile_arg = argv[++i];
702         }
703       else if (strcmp (argv[i], "--") == 0)
704         {
705           /* End of options.  */
706           ++i;
707           break;
708         }
709       else if (argv[i][0] == '-')
710         {
711           /* Future proofing: Don't allow OUTFILE to begin with "-".  */
712           error (_("Unknown option: %s"), argv[i]);
713         }
714       else
715         break;
716     }
717   outfile_idx = i;
718
719   stdio_file arg_outfile;
720
721   if (argv != NULL && argv[outfile_idx] != NULL)
722     {
723       if (argv[outfile_idx + 1] != NULL)
724         error (_("Junk at end of command"));
725       gdb::unique_xmalloc_ptr<char> outfile_name
726         (tilde_expand (argv[outfile_idx]));
727       if (!arg_outfile.open (outfile_name.get (), FOPEN_WT))
728         perror_with_name (outfile_name.get ());
729       outfile = &arg_outfile;
730     }
731
732   for (objfile *objfile : current_program_space->objfiles ())
733     {
734       QUIT;
735       if (objfile_arg == NULL
736           || compare_filenames_for_search (objfile_name (objfile), objfile_arg))
737         dump_msymbols (objfile, outfile);
738     }
739 }
740
741 static void
742 maintenance_print_objfiles (const char *regexp, int from_tty)
743 {
744   struct program_space *pspace;
745
746   dont_repeat ();
747
748   if (regexp)
749     re_comp (regexp);
750
751   ALL_PSPACES (pspace)
752     for (objfile *objfile : pspace->objfiles ())
753       {
754         QUIT;
755         if (! regexp
756             || re_exec (objfile_name (objfile)))
757           dump_objfile (objfile);
758       }
759 }
760
761 /* List all the symbol tables whose names match REGEXP (optional).  */
762
763 static void
764 maintenance_info_symtabs (const char *regexp, int from_tty)
765 {
766   struct program_space *pspace;
767
768   dont_repeat ();
769
770   if (regexp)
771     re_comp (regexp);
772
773   ALL_PSPACES (pspace)
774     for (objfile *objfile : pspace->objfiles ())
775       {
776         /* We don't want to print anything for this objfile until we
777            actually find a symtab whose name matches.  */
778         int printed_objfile_start = 0;
779
780         for (compunit_symtab *cust : objfile->compunits ())
781           {
782             int printed_compunit_symtab_start = 0;
783
784             for (symtab *symtab : compunit_filetabs (cust))
785               {
786                 QUIT;
787
788                 if (! regexp
789                     || re_exec (symtab_to_filename_for_display (symtab)))
790                   {
791                     if (! printed_objfile_start)
792                       {
793                         printf_filtered ("{ objfile %s ", objfile_name (objfile));
794                         wrap_here ("  ");
795                         printf_filtered ("((struct objfile *) %s)\n",
796                                          host_address_to_string (objfile));
797                         printed_objfile_start = 1;
798                       }
799                     if (! printed_compunit_symtab_start)
800                       {
801                         printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
802                                          host_address_to_string (cust));
803                         printf_filtered ("    debugformat %s\n",
804                                          COMPUNIT_DEBUGFORMAT (cust));
805                         printf_filtered ("    producer %s\n",
806                                          COMPUNIT_PRODUCER (cust) != NULL
807                                          ? COMPUNIT_PRODUCER (cust)
808                                          : "(null)");
809                         printf_filtered ("    dirname %s\n",
810                                          COMPUNIT_DIRNAME (cust) != NULL
811                                          ? COMPUNIT_DIRNAME (cust)
812                                          : "(null)");
813                         printf_filtered ("    blockvector"
814                                          " ((struct blockvector *) %s)\n",
815                                          host_address_to_string
816                                          (COMPUNIT_BLOCKVECTOR (cust)));
817                         printed_compunit_symtab_start = 1;
818                       }
819
820                     printf_filtered ("\t{ symtab %s ",
821                                      symtab_to_filename_for_display (symtab));
822                     wrap_here ("    ");
823                     printf_filtered ("((struct symtab *) %s)\n",
824                                      host_address_to_string (symtab));
825                     printf_filtered ("\t  fullname %s\n",
826                                      symtab->fullname != NULL
827                                      ? symtab->fullname
828                                      : "(null)");
829                     printf_filtered ("\t  "
830                                      "linetable ((struct linetable *) %s)\n",
831                                      host_address_to_string (symtab->linetable));
832                     printf_filtered ("\t}\n");
833                   }
834               }
835
836             if (printed_compunit_symtab_start)
837               printf_filtered ("  }\n");
838           }
839
840         if (printed_objfile_start)
841           printf_filtered ("}\n");
842       }
843 }
844
845 /* Check consistency of symtabs.
846    An example of what this checks for is NULL blockvectors.
847    They can happen if there's a bug during debug info reading.
848    GDB assumes they are always non-NULL.
849
850    Note: This does not check for psymtab vs symtab consistency.
851    Use "maint check-psymtabs" for that.  */
852
853 static void
854 maintenance_check_symtabs (const char *ignore, int from_tty)
855 {
856   struct program_space *pspace;
857
858   ALL_PSPACES (pspace)
859     for (objfile *objfile : pspace->objfiles ())
860       {
861         /* We don't want to print anything for this objfile until we
862            actually find something worth printing.  */
863         int printed_objfile_start = 0;
864
865         for (compunit_symtab *cust : objfile->compunits ())
866           {
867             int found_something = 0;
868             struct symtab *symtab = compunit_primary_filetab (cust);
869
870             QUIT;
871
872             if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
873               found_something = 1;
874             /* Add more checks here.  */
875
876             if (found_something)
877               {
878                 if (! printed_objfile_start)
879                   {
880                     printf_filtered ("{ objfile %s ", objfile_name (objfile));
881                     wrap_here ("  ");
882                     printf_filtered ("((struct objfile *) %s)\n",
883                                      host_address_to_string (objfile));
884                     printed_objfile_start = 1;
885                   }
886                 printf_filtered ("  { symtab %s\n",
887                                  symtab_to_filename_for_display (symtab));
888                 if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
889                   printf_filtered ("    NULL blockvector\n");
890                 printf_filtered ("  }\n");
891               }
892           }
893
894         if (printed_objfile_start)
895           printf_filtered ("}\n");
896       }
897 }
898
899 /* Expand all symbol tables whose name matches an optional regexp.  */
900
901 static void
902 maintenance_expand_symtabs (const char *args, int from_tty)
903 {
904   struct program_space *pspace;
905   char *regexp = NULL;
906
907   /* We use buildargv here so that we handle spaces in the regexp
908      in a way that allows adding more arguments later.  */
909   gdb_argv argv (args);
910
911   if (argv != NULL)
912     {
913       if (argv[0] != NULL)
914         {
915           regexp = argv[0];
916           if (argv[1] != NULL)
917             error (_("Extra arguments after regexp."));
918         }
919     }
920
921   if (regexp)
922     re_comp (regexp);
923
924   ALL_PSPACES (pspace)
925     for (objfile *objfile : pspace->objfiles ())
926       {
927         if (objfile->sf)
928           {
929             objfile->sf->qf->expand_symtabs_matching
930               (objfile,
931                [&] (const char *filename, bool basenames)
932                {
933                  /* KISS: Only apply the regexp to the complete file name.  */
934                  return (!basenames
935                          && (regexp == NULL || re_exec (filename)));
936                },
937                lookup_name_info::match_any (),
938                [] (const char *symname)
939                {
940                  /* Since we're not searching on symbols, just return true.  */
941                  return true;
942                },
943                NULL,
944                ALL_DOMAIN);
945           }
946       }
947 }
948 \f
949
950 /* Return the nexting depth of a block within other blocks in its symtab.  */
951
952 static int
953 block_depth (const struct block *block)
954 {
955   int i = 0;
956
957   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
958     {
959       i++;
960     }
961   return i;
962 }
963 \f
964
965 /* Used by MAINTENANCE_INFO_LINE_TABLES to print the information about a
966    single line table.  */
967
968 static int
969 maintenance_print_one_line_table (struct symtab *symtab, void *data)
970 {
971   struct linetable *linetable;
972   struct objfile *objfile;
973
974   objfile = symtab->compunit_symtab->objfile;
975   printf_filtered (_("objfile: %s ((struct objfile *) %s)\n"),
976                    objfile_name (objfile),
977                    host_address_to_string (objfile));
978   printf_filtered (_("compunit_symtab: ((struct compunit_symtab *) %s)\n"),
979                    host_address_to_string (symtab->compunit_symtab));
980   printf_filtered (_("symtab: %s ((struct symtab *) %s)\n"),
981                    symtab_to_fullname (symtab),
982                    host_address_to_string (symtab));
983   linetable = SYMTAB_LINETABLE (symtab);
984   printf_filtered (_("linetable: ((struct linetable *) %s):\n"),
985                    host_address_to_string (linetable));
986
987   if (linetable == NULL)
988     printf_filtered (_("No line table.\n"));
989   else if (linetable->nitems <= 0)
990     printf_filtered (_("Line table has no lines.\n"));
991   else
992     {
993       int i;
994
995       /* Leave space for 6 digits of index and line number.  After that the
996          tables will just not format as well.  */
997       printf_filtered (_("%-6s %6s %s\n"),
998                        _("INDEX"), _("LINE"), _("ADDRESS"));
999
1000       for (i = 0; i < linetable->nitems; ++i)
1001         {
1002           struct linetable_entry *item;
1003
1004           item = &linetable->item [i];
1005           printf_filtered (_("%-6d %6d %s\n"), i, item->line,
1006                            core_addr_to_string (item->pc));
1007         }
1008     }
1009
1010   return 0;
1011 }
1012
1013 /* Implement the 'maint info line-table' command.  */
1014
1015 static void
1016 maintenance_info_line_tables (const char *regexp, int from_tty)
1017 {
1018   struct program_space *pspace;
1019
1020   dont_repeat ();
1021
1022   if (regexp != NULL)
1023     re_comp (regexp);
1024
1025   ALL_PSPACES (pspace)
1026     for (objfile *objfile : pspace->objfiles ())
1027       {
1028         for (compunit_symtab *cust : objfile->compunits ())
1029           {
1030             for (symtab *symtab : compunit_filetabs (cust))
1031               {
1032                 QUIT;
1033
1034                 if (regexp == NULL
1035                     || re_exec (symtab_to_filename_for_display (symtab)))
1036                   maintenance_print_one_line_table (symtab, NULL);
1037               }
1038           }
1039       }
1040 }
1041
1042 \f
1043
1044 /* Do early runtime initializations.  */
1045
1046 void
1047 _initialize_symmisc (void)
1048 {
1049   std_in = stdin;
1050   std_out = stdout;
1051   std_err = stderr;
1052
1053   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
1054 Print dump of current symbol definitions.\n\
1055 Usage: mt print symbols [-pc ADDRESS] [--] [OUTFILE]\n\
1056        mt print symbols [-objfile OBJFILE] [-source SOURCE] [--] [OUTFILE]\n\
1057 Entries in the full symbol table are dumped to file OUTFILE,\n\
1058 or the terminal if OUTFILE is unspecified.\n\
1059 If ADDRESS is provided, dump only the file for that address.\n\
1060 If SOURCE is provided, dump only that file's symbols.\n\
1061 If OBJFILE is provided, dump only that file's minimal symbols."),
1062            &maintenanceprintlist);
1063
1064   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
1065 Print dump of current minimal symbol definitions.\n\
1066 Usage: mt print msymbols [-objfile OBJFILE] [--] [OUTFILE]\n\
1067 Entries in the minimal symbol table are dumped to file OUTFILE,\n\
1068 or the terminal if OUTFILE is unspecified.\n\
1069 If OBJFILE is provided, dump only that file's minimal symbols."),
1070            &maintenanceprintlist);
1071
1072   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
1073            _("Print dump of current object file definitions.\n\
1074 With an argument REGEXP, list the object files with matching names."),
1075            &maintenanceprintlist);
1076
1077   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
1078 List the full symbol tables for all object files.\n\
1079 This does not include information about individual symbols, blocks, or\n\
1080 linetables --- just the symbol table structures themselves.\n\
1081 With an argument REGEXP, list the symbol tables with matching names."),
1082            &maintenanceinfolist);
1083
1084   add_cmd ("line-table", class_maintenance, maintenance_info_line_tables, _("\
1085 List the contents of all line tables, from all symbol tables.\n\
1086 With an argument REGEXP, list just the line tables for the symbol\n\
1087 tables with matching names."),
1088            &maintenanceinfolist);
1089
1090   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
1091            _("\
1092 Check consistency of currently expanded symtabs."),
1093            &maintenancelist);
1094
1095   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
1096            _("Expand symbol tables.\n\
1097 With an argument REGEXP, only expand the symbol tables with matching names."),
1098            &maintenancelist);
1099 }