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