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