xref: /dragonfly/contrib/gdb-7/gdb/symmisc.c (revision 73e0051e)
1 /* Do various things to symbol tables (other than lookup), for GDB.
2 
3    Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4    1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2007, 2008, 2009
5    Free Software Foundation, Inc.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 #include "defs.h"
23 #include "symtab.h"
24 #include "gdbtypes.h"
25 #include "bfd.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "breakpoint.h"
29 #include "command.h"
30 #include "gdb_obstack.h"
31 #include "exceptions.h"
32 #include "language.h"
33 #include "bcache.h"
34 #include "block.h"
35 #include "gdb_regex.h"
36 #include "gdb_stat.h"
37 #include "dictionary.h"
38 
39 #include "gdb_string.h"
40 #include "readline/readline.h"
41 
42 #ifndef DEV_TTY
43 #define DEV_TTY "/dev/tty"
44 #endif
45 
46 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
47    when calling functions that take FILE *'s from the debugger.
48    So we make a variable which has the same value and which is accessible when
49    debugging GDB with itself.  Because stdin et al need not be constants,
50    we initialize them in the _initialize_symmisc function at the bottom
51    of the file.  */
52 FILE *std_in;
53 FILE *std_out;
54 FILE *std_err;
55 
56 /* Prototypes for local functions */
57 
58 static void dump_symtab (struct objfile *, struct symtab *,
59 			 struct ui_file *);
60 
61 static void dump_psymtab (struct objfile *, struct partial_symtab *,
62 			  struct ui_file *);
63 
64 static void dump_msymbols (struct objfile *, struct ui_file *);
65 
66 static void dump_objfile (struct objfile *);
67 
68 static int block_depth (struct block *);
69 
70 static void print_partial_symbols (struct gdbarch *,
71 				   struct partial_symbol **, int,
72 				   char *, struct ui_file *);
73 
74 void _initialize_symmisc (void);
75 
76 struct print_symbol_args
77   {
78     struct gdbarch *gdbarch;
79     struct symbol *symbol;
80     int depth;
81     struct ui_file *outfile;
82   };
83 
84 static int print_symbol (void *);
85 
86 /* Free all the storage associated with the struct symtab <- S.
87    Note that some symtabs have contents that all live inside one big block of
88    memory, and some share the contents of another symbol table and so you
89    should not free the contents on their behalf (except sometimes the
90    linetable, which maybe per symtab even when the rest is not).
91    It is s->free_code that says which alternative to use.  */
92 
93 void
94 free_symtab (struct symtab *s)
95 {
96   int i, n;
97   struct blockvector *bv;
98 
99   switch (s->free_code)
100     {
101     case free_nothing:
102       /* All the contents are part of a big block of memory (an obstack),
103          and some other symtab is in charge of freeing that block.
104          Therefore, do nothing.  */
105       break;
106 
107     case free_linetable:
108       /* Everything will be freed either by our `free_func'
109          or by some other symtab, except for our linetable.
110          Free that now.  */
111       if (LINETABLE (s))
112 	xfree (LINETABLE (s));
113       break;
114     }
115 
116   /* If there is a single block of memory to free, free it.  */
117   if (s->free_func != NULL)
118     s->free_func (s);
119 
120   /* Free source-related stuff */
121   if (s->line_charpos != NULL)
122     xfree (s->line_charpos);
123   if (s->fullname != NULL)
124     xfree (s->fullname);
125   if (s->debugformat != NULL)
126     xfree (s->debugformat);
127   xfree (s);
128 }
129 
130 void
131 print_symbol_bcache_statistics (void)
132 {
133   struct objfile *objfile;
134 
135   immediate_quit++;
136   ALL_OBJFILES (objfile)
137   {
138     printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
139     print_bcache_statistics (objfile->psymbol_cache, "partial symbol cache");
140     print_bcache_statistics (objfile->macro_cache, "preprocessor macro cache");
141   }
142   immediate_quit--;
143 }
144 
145 void
146 print_objfile_statistics (void)
147 {
148   struct objfile *objfile;
149   struct symtab *s;
150   struct partial_symtab *ps;
151   int i, linetables, blockvectors;
152 
153   immediate_quit++;
154   ALL_OBJFILES (objfile)
155   {
156     printf_filtered (_("Statistics for '%s':\n"), objfile->name);
157     if (OBJSTAT (objfile, n_stabs) > 0)
158       printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
159 		       OBJSTAT (objfile, n_stabs));
160     if (OBJSTAT (objfile, n_minsyms) > 0)
161       printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
162 		       OBJSTAT (objfile, n_minsyms));
163     if (OBJSTAT (objfile, n_psyms) > 0)
164       printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
165 		       OBJSTAT (objfile, n_psyms));
166     if (OBJSTAT (objfile, n_syms) > 0)
167       printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
168 		       OBJSTAT (objfile, n_syms));
169     if (OBJSTAT (objfile, n_types) > 0)
170       printf_filtered (_("  Number of \"types\" defined: %d\n"),
171 		       OBJSTAT (objfile, n_types));
172     i = 0;
173     ALL_OBJFILE_PSYMTABS (objfile, ps)
174       {
175         if (ps->readin == 0)
176           i++;
177       }
178     printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
179     i = linetables = blockvectors = 0;
180     ALL_OBJFILE_SYMTABS (objfile, s)
181       {
182         i++;
183         if (s->linetable != NULL)
184           linetables++;
185         if (s->primary == 1)
186           blockvectors++;
187       }
188     printf_filtered (_("  Number of symbol tables: %d\n"), i);
189     printf_filtered (_("  Number of symbol tables with line tables: %d\n"),
190                      linetables);
191     printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"),
192                      blockvectors);
193 
194     if (OBJSTAT (objfile, sz_strtab) > 0)
195       printf_filtered (_("  Space used by a.out string tables: %d\n"),
196 		       OBJSTAT (objfile, sz_strtab));
197     printf_filtered (_("  Total memory used for objfile obstack: %d\n"),
198 		     obstack_memory_used (&objfile->objfile_obstack));
199     printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
200 		     bcache_memory_used (objfile->psymbol_cache));
201     printf_filtered (_("  Total memory used for macro cache: %d\n"),
202 		     bcache_memory_used (objfile->macro_cache));
203   }
204   immediate_quit--;
205 }
206 
207 static void
208 dump_objfile (struct objfile *objfile)
209 {
210   struct symtab *symtab;
211   struct partial_symtab *psymtab;
212 
213   printf_filtered ("\nObject file %s:  ", objfile->name);
214   printf_filtered ("Objfile at ");
215   gdb_print_host_address (objfile, gdb_stdout);
216   printf_filtered (", bfd at ");
217   gdb_print_host_address (objfile->obfd, gdb_stdout);
218   printf_filtered (", %d minsyms\n\n",
219 		   objfile->minimal_symbol_count);
220 
221   if (objfile->psymtabs)
222     {
223       printf_filtered ("Psymtabs:\n");
224       for (psymtab = objfile->psymtabs;
225 	   psymtab != NULL;
226 	   psymtab = psymtab->next)
227 	{
228 	  printf_filtered ("%s at ",
229 			   psymtab->filename);
230 	  gdb_print_host_address (psymtab, gdb_stdout);
231 	  printf_filtered (", ");
232 	  if (psymtab->objfile != objfile)
233 	    {
234 	      printf_filtered ("NOT ON CHAIN!  ");
235 	    }
236 	  wrap_here ("  ");
237 	}
238       printf_filtered ("\n\n");
239     }
240 
241   if (objfile->symtabs)
242     {
243       printf_filtered ("Symtabs:\n");
244       for (symtab = objfile->symtabs;
245 	   symtab != NULL;
246 	   symtab = symtab->next)
247 	{
248 	  printf_filtered ("%s at ", symtab->filename);
249 	  gdb_print_host_address (symtab, gdb_stdout);
250 	  printf_filtered (", ");
251 	  if (symtab->objfile != objfile)
252 	    {
253 	      printf_filtered ("NOT ON CHAIN!  ");
254 	    }
255 	  wrap_here ("  ");
256 	}
257       printf_filtered ("\n\n");
258     }
259 }
260 
261 /* Print minimal symbols from this objfile.  */
262 
263 static void
264 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
265 {
266   struct gdbarch *gdbarch = get_objfile_arch (objfile);
267   struct minimal_symbol *msymbol;
268   int index;
269   char ms_type;
270 
271   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
272   if (objfile->minimal_symbol_count == 0)
273     {
274       fprintf_filtered (outfile, "No minimal symbols found.\n");
275       return;
276     }
277   index = 0;
278   ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
279     {
280       struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
281 
282       switch (MSYMBOL_TYPE (msymbol))
283 	{
284 	case mst_unknown:
285 	  ms_type = 'u';
286 	  break;
287 	case mst_text:
288 	  ms_type = 'T';
289 	  break;
290 	case mst_solib_trampoline:
291 	  ms_type = 'S';
292 	  break;
293 	case mst_data:
294 	  ms_type = 'D';
295 	  break;
296 	case mst_bss:
297 	  ms_type = 'B';
298 	  break;
299 	case mst_abs:
300 	  ms_type = 'A';
301 	  break;
302 	case mst_file_text:
303 	  ms_type = 't';
304 	  break;
305 	case mst_file_data:
306 	  ms_type = 'd';
307 	  break;
308 	case mst_file_bss:
309 	  ms_type = 'b';
310 	  break;
311 	default:
312 	  ms_type = '?';
313 	  break;
314 	}
315       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
316       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
317 		      outfile);
318       fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
319       if (section)
320 	fprintf_filtered (outfile, " section %s",
321 			  bfd_section_name (objfile->obfd,
322 					    section->the_bfd_section));
323       if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
324 	{
325 	  fprintf_filtered (outfile, "  %s", SYMBOL_DEMANGLED_NAME (msymbol));
326 	}
327       if (msymbol->filename)
328 	fprintf_filtered (outfile, "  %s", msymbol->filename);
329       fputs_filtered ("\n", outfile);
330       index++;
331     }
332   if (objfile->minimal_symbol_count != index)
333     {
334       warning (_("internal error:  minimal symbol count %d != %d"),
335 	       objfile->minimal_symbol_count, index);
336     }
337   fprintf_filtered (outfile, "\n");
338 }
339 
340 static void
341 dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
342 	      struct ui_file *outfile)
343 {
344   struct gdbarch *gdbarch = get_objfile_arch (objfile);
345   int i;
346 
347   fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
348 		    psymtab->filename);
349   fprintf_filtered (outfile, "(object ");
350   gdb_print_host_address (psymtab, outfile);
351   fprintf_filtered (outfile, ")\n\n");
352   fprintf_unfiltered (outfile, "  Read from object file %s (",
353 		      objfile->name);
354   gdb_print_host_address (objfile, outfile);
355   fprintf_unfiltered (outfile, ")\n");
356 
357   if (psymtab->readin)
358     {
359       fprintf_filtered (outfile,
360 			"  Full symtab was read (at ");
361       gdb_print_host_address (psymtab->symtab, outfile);
362       fprintf_filtered (outfile, " by function at ");
363       gdb_print_host_address (psymtab->read_symtab, outfile);
364       fprintf_filtered (outfile, ")\n");
365     }
366 
367   fprintf_filtered (outfile, "  Relocate symbols by ");
368   for (i = 0; i < psymtab->objfile->num_sections; ++i)
369     {
370       if (i != 0)
371 	fprintf_filtered (outfile, ", ");
372       wrap_here ("    ");
373       fputs_filtered (paddress (gdbarch,
374 				ANOFFSET (psymtab->section_offsets, i)),
375 		      outfile);
376     }
377   fprintf_filtered (outfile, "\n");
378 
379   fprintf_filtered (outfile, "  Symbols cover text addresses ");
380   fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
381   fprintf_filtered (outfile, "-");
382   fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
383   fprintf_filtered (outfile, "\n");
384   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
385 		    psymtab->number_of_dependencies);
386   for (i = 0; i < psymtab->number_of_dependencies; i++)
387     {
388       fprintf_filtered (outfile, "    %d ", i);
389       gdb_print_host_address (psymtab->dependencies[i], outfile);
390       fprintf_filtered (outfile, " %s\n",
391 			psymtab->dependencies[i]->filename);
392     }
393   if (psymtab->n_global_syms > 0)
394     {
395       print_partial_symbols (gdbarch,
396 			     objfile->global_psymbols.list
397 			     + psymtab->globals_offset,
398 			     psymtab->n_global_syms, "Global", outfile);
399     }
400   if (psymtab->n_static_syms > 0)
401     {
402       print_partial_symbols (gdbarch,
403 			     objfile->static_psymbols.list
404 			     + psymtab->statics_offset,
405 			     psymtab->n_static_syms, "Static", outfile);
406     }
407   fprintf_filtered (outfile, "\n");
408 }
409 
410 static void
411 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
412 	       struct ui_file *outfile)
413 {
414   struct gdbarch *gdbarch = get_objfile_arch (objfile);
415   int i;
416   struct dict_iterator iter;
417   int len, blen;
418   struct linetable *l;
419   struct blockvector *bv;
420   struct symbol *sym;
421   struct block *b;
422   int depth;
423 
424   fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
425   if (symtab->dirname)
426     fprintf_filtered (outfile, "Compilation directory is %s\n",
427 		      symtab->dirname);
428   fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
429   gdb_print_host_address (objfile, outfile);
430   fprintf_filtered (outfile, ")\n");
431   fprintf_filtered (outfile, "Language: %s\n", language_str (symtab->language));
432 
433   /* First print the line table.  */
434   l = LINETABLE (symtab);
435   if (l)
436     {
437       fprintf_filtered (outfile, "\nLine table:\n\n");
438       len = l->nitems;
439       for (i = 0; i < len; i++)
440 	{
441 	  fprintf_filtered (outfile, " line %d at ", l->item[i].line);
442 	  fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
443 	  fprintf_filtered (outfile, "\n");
444 	}
445     }
446   /* Now print the block info, but only for primary symtabs since we will
447      print lots of duplicate info otherwise. */
448   if (symtab->primary)
449     {
450       fprintf_filtered (outfile, "\nBlockvector:\n\n");
451       bv = BLOCKVECTOR (symtab);
452       len = BLOCKVECTOR_NBLOCKS (bv);
453       for (i = 0; i < len; i++)
454 	{
455 	  b = BLOCKVECTOR_BLOCK (bv, i);
456 	  depth = block_depth (b) * 2;
457 	  print_spaces (depth, outfile);
458 	  fprintf_filtered (outfile, "block #%03d, object at ", i);
459 	  gdb_print_host_address (b, outfile);
460 	  if (BLOCK_SUPERBLOCK (b))
461 	    {
462 	      fprintf_filtered (outfile, " under ");
463 	      gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
464 	    }
465 	  /* drow/2002-07-10: We could save the total symbols count
466 	     even if we're using a hashtable, but nothing else but this message
467 	     wants it.  */
468 	  fprintf_filtered (outfile, ", %d syms/buckets in ",
469 			    dict_size (BLOCK_DICT (b)));
470 	  fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
471 	  fprintf_filtered (outfile, "..");
472 	  fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
473 	  if (BLOCK_FUNCTION (b))
474 	    {
475 	      fprintf_filtered (outfile, ", function %s",
476 				SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
477 	      if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
478 		{
479 		  fprintf_filtered (outfile, ", %s",
480 				SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
481 		}
482 	    }
483 	  fprintf_filtered (outfile, "\n");
484 	  /* Now print each symbol in this block (in no particular order, if
485 	     we're using a hashtable).  */
486 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
487 	    {
488 	      struct print_symbol_args s;
489 	      s.gdbarch = gdbarch;
490 	      s.symbol = sym;
491 	      s.depth = depth + 1;
492 	      s.outfile = outfile;
493 	      catch_errors (print_symbol, &s, "Error printing symbol:\n",
494 			    RETURN_MASK_ERROR);
495 	    }
496 	}
497       fprintf_filtered (outfile, "\n");
498     }
499   else
500     {
501       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
502     }
503 }
504 
505 static void
506 dump_symtab (struct objfile *objfile, struct symtab *symtab,
507 	     struct ui_file *outfile)
508 {
509   /* Set the current language to the language of the symtab we're dumping
510      because certain routines used during dump_symtab() use the current
511      language to print an image of the symbol.  We'll restore it later.
512      But use only real languages, not placeholders.  */
513   if (symtab->language != language_unknown
514       && symtab->language != language_auto)
515     {
516       enum language saved_lang;
517 
518       saved_lang = set_language (symtab->language);
519 
520       dump_symtab_1 (objfile, symtab, outfile);
521 
522       set_language (saved_lang);
523     }
524   else
525     dump_symtab_1 (objfile, symtab, outfile);
526 }
527 
528 void
529 maintenance_print_symbols (char *args, int from_tty)
530 {
531   char **argv;
532   struct ui_file *outfile;
533   struct cleanup *cleanups;
534   char *symname = NULL;
535   char *filename = DEV_TTY;
536   struct objfile *objfile;
537   struct symtab *s;
538 
539   dont_repeat ();
540 
541   if (args == NULL)
542     {
543       error (_("\
544 Arguments missing: an output file name and an optional symbol file name"));
545     }
546   argv = gdb_buildargv (args);
547   cleanups = make_cleanup_freeargv (argv);
548 
549   if (argv[0] != NULL)
550     {
551       filename = argv[0];
552       /* If a second arg is supplied, it is a source file name to match on */
553       if (argv[1] != NULL)
554 	{
555 	  symname = argv[1];
556 	}
557     }
558 
559   filename = tilde_expand (filename);
560   make_cleanup (xfree, filename);
561 
562   outfile = gdb_fopen (filename, FOPEN_WT);
563   if (outfile == 0)
564     perror_with_name (filename);
565   make_cleanup_ui_file_delete (outfile);
566 
567   immediate_quit++;
568   ALL_SYMTABS (objfile, s)
569     if (symname == NULL || strcmp (symname, s->filename) == 0)
570     dump_symtab (objfile, s, outfile);
571   immediate_quit--;
572   do_cleanups (cleanups);
573 }
574 
575 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
576    far to indent.  ARGS is really a struct print_symbol_args *, but is
577    declared as char * to get it past catch_errors.  Returns 0 for error,
578    1 for success.  */
579 
580 static int
581 print_symbol (void *args)
582 {
583   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
584   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
585   int depth = ((struct print_symbol_args *) args)->depth;
586   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
587   struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
588 
589   print_spaces (depth, outfile);
590   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
591     {
592       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
593       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
594 		      outfile);
595       if (section)
596 	fprintf_filtered (outfile, " section %s\n",
597 			  bfd_section_name (section->the_bfd_section->owner,
598 					    section->the_bfd_section));
599       else
600 	fprintf_filtered (outfile, "\n");
601       return 1;
602     }
603   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
604     {
605       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
606 	{
607 	  LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
608 	}
609       else
610 	{
611 	  fprintf_filtered (outfile, "%s %s = ",
612 			 (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
613 			  ? "enum"
614 		     : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
615 			? "struct" : "union")),
616 			    SYMBOL_LINKAGE_NAME (symbol));
617 	  LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
618 	}
619       fprintf_filtered (outfile, ";\n");
620     }
621   else
622     {
623       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
624 	fprintf_filtered (outfile, "typedef ");
625       if (SYMBOL_TYPE (symbol))
626 	{
627 	  /* Print details of types, except for enums where it's clutter.  */
628 	  LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
629 			 outfile,
630 			 TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
631 			 depth);
632 	  fprintf_filtered (outfile, "; ");
633 	}
634       else
635 	fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
636 
637       switch (SYMBOL_CLASS (symbol))
638 	{
639 	case LOC_CONST:
640 	  fprintf_filtered (outfile, "const %ld (0x%lx)",
641 			    SYMBOL_VALUE (symbol),
642 			    SYMBOL_VALUE (symbol));
643 	  break;
644 
645 	case LOC_CONST_BYTES:
646 	  {
647 	    unsigned i;
648 	    struct type *type = check_typedef (SYMBOL_TYPE (symbol));
649 	    fprintf_filtered (outfile, "const %u hex bytes:",
650 			      TYPE_LENGTH (type));
651 	    for (i = 0; i < TYPE_LENGTH (type); i++)
652 	      fprintf_filtered (outfile, " %02x",
653 				(unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
654 	  }
655 	  break;
656 
657 	case LOC_STATIC:
658 	  fprintf_filtered (outfile, "static at ");
659 	  fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
660 			  outfile);
661 	  if (section)
662 	    fprintf_filtered (outfile, " section %s",
663 			      bfd_section_name (section->the_bfd_section->owner,
664 						section->the_bfd_section));
665 	  break;
666 
667 	case LOC_REGISTER:
668 	  if (SYMBOL_IS_ARGUMENT (symbol))
669 	    fprintf_filtered (outfile, "parameter register %ld",
670 			      SYMBOL_VALUE (symbol));
671 	  else
672 	    fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
673 	  break;
674 
675 	case LOC_ARG:
676 	  fprintf_filtered (outfile, "arg at offset 0x%lx",
677 			    SYMBOL_VALUE (symbol));
678 	  break;
679 
680 	case LOC_REF_ARG:
681 	  fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
682 	  break;
683 
684 	case LOC_REGPARM_ADDR:
685 	  fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
686 	  break;
687 
688 	case LOC_LOCAL:
689 	  fprintf_filtered (outfile, "local at offset 0x%lx",
690 			    SYMBOL_VALUE (symbol));
691 	  break;
692 
693 	case LOC_TYPEDEF:
694 	  break;
695 
696 	case LOC_LABEL:
697 	  fprintf_filtered (outfile, "label at ");
698 	  fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
699 			  outfile);
700 	  if (section)
701 	    fprintf_filtered (outfile, " section %s",
702 			      bfd_section_name (section->the_bfd_section->owner,
703 						section->the_bfd_section));
704 	  break;
705 
706 	case LOC_BLOCK:
707 	  fprintf_filtered (outfile, "block object ");
708 	  gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
709 	  fprintf_filtered (outfile, ", ");
710 	  fputs_filtered (paddress (gdbarch,
711 				    BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
712 			  outfile);
713 	  fprintf_filtered (outfile, "..");
714 	  fputs_filtered (paddress (gdbarch,
715 				    BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
716 			  outfile);
717 	  if (section)
718 	    fprintf_filtered (outfile, " section %s",
719 			      bfd_section_name (section->the_bfd_section->owner,
720 						section->the_bfd_section));
721 	  break;
722 
723 	case LOC_COMPUTED:
724 	  fprintf_filtered (outfile, "computed at runtime");
725 	  break;
726 
727 	case LOC_UNRESOLVED:
728 	  fprintf_filtered (outfile, "unresolved");
729 	  break;
730 
731 	case LOC_OPTIMIZED_OUT:
732 	  fprintf_filtered (outfile, "optimized out");
733 	  break;
734 
735 	default:
736 	  fprintf_filtered (outfile, "botched symbol class %x",
737 			    SYMBOL_CLASS (symbol));
738 	  break;
739 	}
740     }
741   fprintf_filtered (outfile, "\n");
742   return 1;
743 }
744 
745 void
746 maintenance_print_psymbols (char *args, int from_tty)
747 {
748   char **argv;
749   struct ui_file *outfile;
750   struct cleanup *cleanups;
751   char *symname = NULL;
752   char *filename = DEV_TTY;
753   struct objfile *objfile;
754   struct partial_symtab *ps;
755 
756   dont_repeat ();
757 
758   if (args == NULL)
759     {
760       error (_("print-psymbols takes an output file name and optional symbol file name"));
761     }
762   argv = gdb_buildargv (args);
763   cleanups = make_cleanup_freeargv (argv);
764 
765   if (argv[0] != NULL)
766     {
767       filename = argv[0];
768       /* If a second arg is supplied, it is a source file name to match on */
769       if (argv[1] != NULL)
770 	{
771 	  symname = argv[1];
772 	}
773     }
774 
775   filename = tilde_expand (filename);
776   make_cleanup (xfree, filename);
777 
778   outfile = gdb_fopen (filename, FOPEN_WT);
779   if (outfile == 0)
780     perror_with_name (filename);
781   make_cleanup_ui_file_delete (outfile);
782 
783   immediate_quit++;
784   ALL_PSYMTABS (objfile, ps)
785     if (symname == NULL || strcmp (symname, ps->filename) == 0)
786     dump_psymtab (objfile, ps, outfile);
787   immediate_quit--;
788   do_cleanups (cleanups);
789 }
790 
791 static void
792 print_partial_symbols (struct gdbarch *gdbarch,
793 		       struct partial_symbol **p, int count, char *what,
794 		       struct ui_file *outfile)
795 {
796   fprintf_filtered (outfile, "  %s partial symbols:\n", what);
797   while (count-- > 0)
798     {
799       fprintf_filtered (outfile, "    `%s'", SYMBOL_LINKAGE_NAME (*p));
800       if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
801 	{
802 	  fprintf_filtered (outfile, "  `%s'", SYMBOL_DEMANGLED_NAME (*p));
803 	}
804       fputs_filtered (", ", outfile);
805       switch (SYMBOL_DOMAIN (*p))
806 	{
807 	case UNDEF_DOMAIN:
808 	  fputs_filtered ("undefined domain, ", outfile);
809 	  break;
810 	case VAR_DOMAIN:
811 	  /* This is the usual thing -- don't print it */
812 	  break;
813 	case STRUCT_DOMAIN:
814 	  fputs_filtered ("struct domain, ", outfile);
815 	  break;
816 	case LABEL_DOMAIN:
817 	  fputs_filtered ("label domain, ", outfile);
818 	  break;
819 	default:
820 	  fputs_filtered ("<invalid domain>, ", outfile);
821 	  break;
822 	}
823       switch (SYMBOL_CLASS (*p))
824 	{
825 	case LOC_UNDEF:
826 	  fputs_filtered ("undefined", outfile);
827 	  break;
828 	case LOC_CONST:
829 	  fputs_filtered ("constant int", outfile);
830 	  break;
831 	case LOC_STATIC:
832 	  fputs_filtered ("static", outfile);
833 	  break;
834 	case LOC_REGISTER:
835 	  fputs_filtered ("register", outfile);
836 	  break;
837 	case LOC_ARG:
838 	  fputs_filtered ("pass by value", outfile);
839 	  break;
840 	case LOC_REF_ARG:
841 	  fputs_filtered ("pass by reference", outfile);
842 	  break;
843 	case LOC_REGPARM_ADDR:
844 	  fputs_filtered ("register address parameter", outfile);
845 	  break;
846 	case LOC_LOCAL:
847 	  fputs_filtered ("stack parameter", outfile);
848 	  break;
849 	case LOC_TYPEDEF:
850 	  fputs_filtered ("type", outfile);
851 	  break;
852 	case LOC_LABEL:
853 	  fputs_filtered ("label", outfile);
854 	  break;
855 	case LOC_BLOCK:
856 	  fputs_filtered ("function", outfile);
857 	  break;
858 	case LOC_CONST_BYTES:
859 	  fputs_filtered ("constant bytes", outfile);
860 	  break;
861 	case LOC_UNRESOLVED:
862 	  fputs_filtered ("unresolved", outfile);
863 	  break;
864 	case LOC_OPTIMIZED_OUT:
865 	  fputs_filtered ("optimized out", outfile);
866 	  break;
867 	case LOC_COMPUTED:
868 	  fputs_filtered ("computed at runtime", outfile);
869 	  break;
870 	default:
871 	  fputs_filtered ("<invalid location>", outfile);
872 	  break;
873 	}
874       fputs_filtered (", ", outfile);
875       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
876       fprintf_filtered (outfile, "\n");
877       p++;
878     }
879 }
880 
881 void
882 maintenance_print_msymbols (char *args, int from_tty)
883 {
884   char **argv;
885   struct ui_file *outfile;
886   struct cleanup *cleanups;
887   char *filename = DEV_TTY;
888   char *symname = NULL;
889   struct objfile *objfile;
890 
891   struct stat sym_st, obj_st;
892 
893   dont_repeat ();
894 
895   if (args == NULL)
896     {
897       error (_("print-msymbols takes an output file name and optional symbol file name"));
898     }
899   argv = gdb_buildargv (args);
900   cleanups = make_cleanup_freeargv (argv);
901 
902   if (argv[0] != NULL)
903     {
904       filename = argv[0];
905       /* If a second arg is supplied, it is a source file name to match on */
906       if (argv[1] != NULL)
907 	{
908 	  symname = xfullpath (argv[1]);
909 	  make_cleanup (xfree, symname);
910 	  if (symname && stat (symname, &sym_st))
911 	    perror_with_name (symname);
912 	}
913     }
914 
915   filename = tilde_expand (filename);
916   make_cleanup (xfree, filename);
917 
918   outfile = gdb_fopen (filename, FOPEN_WT);
919   if (outfile == 0)
920     perror_with_name (filename);
921   make_cleanup_ui_file_delete (outfile);
922 
923   immediate_quit++;
924   ALL_OBJFILES (objfile)
925     if (symname == NULL
926 	|| (!stat (objfile->name, &obj_st) && sym_st.st_ino == obj_st.st_ino))
927       dump_msymbols (objfile, outfile);
928   immediate_quit--;
929   fprintf_filtered (outfile, "\n\n");
930   do_cleanups (cleanups);
931 }
932 
933 void
934 maintenance_print_objfiles (char *ignore, int from_tty)
935 {
936   struct objfile *objfile;
937 
938   dont_repeat ();
939 
940   immediate_quit++;
941   ALL_OBJFILES (objfile)
942     dump_objfile (objfile);
943   immediate_quit--;
944 }
945 
946 
947 /* List all the symbol tables whose names match REGEXP (optional).  */
948 void
949 maintenance_info_symtabs (char *regexp, int from_tty)
950 {
951   struct objfile *objfile;
952 
953   if (regexp)
954     re_comp (regexp);
955 
956   ALL_OBJFILES (objfile)
957     {
958       struct symtab *symtab;
959 
960       /* We don't want to print anything for this objfile until we
961          actually find a symtab whose name matches.  */
962       int printed_objfile_start = 0;
963 
964       ALL_OBJFILE_SYMTABS (objfile, symtab)
965 	{
966 	  QUIT;
967 
968 	  if (! regexp
969 	      || re_exec (symtab->filename))
970 	    {
971 	      if (! printed_objfile_start)
972 		{
973 		  printf_filtered ("{ objfile %s ", objfile->name);
974 		  wrap_here ("  ");
975 		  printf_filtered ("((struct objfile *) %s)\n",
976 				   host_address_to_string (objfile));
977 		  printed_objfile_start = 1;
978 		}
979 
980 	      printf_filtered ("	{ symtab %s ", symtab->filename);
981 	      wrap_here ("    ");
982 	      printf_filtered ("((struct symtab *) %s)\n",
983 			       host_address_to_string (symtab));
984 	      printf_filtered ("	  dirname %s\n",
985 			       symtab->dirname ? symtab->dirname : "(null)");
986 	      printf_filtered ("	  fullname %s\n",
987 			       symtab->fullname ? symtab->fullname : "(null)");
988 	      printf_filtered ("	  blockvector ((struct blockvector *) %s)%s\n",
989 			       host_address_to_string (symtab->blockvector),
990 			       symtab->primary ? " (primary)" : "");
991 	      printf_filtered ("	  linetable ((struct linetable *) %s)\n",
992 			       host_address_to_string (symtab->linetable));
993 	      printf_filtered ("	  debugformat %s\n", symtab->debugformat);
994 	      printf_filtered ("	}\n");
995 	    }
996 	}
997 
998       if (printed_objfile_start)
999         printf_filtered ("}\n");
1000     }
1001 }
1002 
1003 
1004 /* List all the partial symbol tables whose names match REGEXP (optional).  */
1005 void
1006 maintenance_info_psymtabs (char *regexp, int from_tty)
1007 {
1008   struct objfile *objfile;
1009 
1010   if (regexp)
1011     re_comp (regexp);
1012 
1013   ALL_OBJFILES (objfile)
1014     {
1015       struct gdbarch *gdbarch = get_objfile_arch (objfile);
1016       struct partial_symtab *psymtab;
1017 
1018       /* We don't want to print anything for this objfile until we
1019          actually find a symtab whose name matches.  */
1020       int printed_objfile_start = 0;
1021 
1022       ALL_OBJFILE_PSYMTABS (objfile, psymtab)
1023 	{
1024 	  QUIT;
1025 
1026 	  if (! regexp
1027 	      || re_exec (psymtab->filename))
1028 	    {
1029 	      if (! printed_objfile_start)
1030 		{
1031 		  printf_filtered ("{ objfile %s ", objfile->name);
1032 		  wrap_here ("  ");
1033 		  printf_filtered ("((struct objfile *) %s)\n",
1034 				   host_address_to_string (objfile));
1035 		  printed_objfile_start = 1;
1036 		}
1037 
1038 	      printf_filtered ("  { psymtab %s ", psymtab->filename);
1039 	      wrap_here ("    ");
1040 	      printf_filtered ("((struct partial_symtab *) %s)\n",
1041 			       host_address_to_string (psymtab));
1042 
1043 	      printf_filtered ("    readin %s\n",
1044 			       psymtab->readin ? "yes" : "no");
1045 	      printf_filtered ("    fullname %s\n",
1046 			       psymtab->fullname ? psymtab->fullname : "(null)");
1047 	      printf_filtered ("    text addresses ");
1048 	      fputs_filtered (paddress (gdbarch, psymtab->textlow),
1049 			      gdb_stdout);
1050 	      printf_filtered (" -- ");
1051 	      fputs_filtered (paddress (gdbarch, psymtab->texthigh),
1052 			      gdb_stdout);
1053 	      printf_filtered ("\n");
1054 	      printf_filtered ("    globals ");
1055 	      if (psymtab->n_global_syms)
1056 		{
1057 		  printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
1058 				   host_address_to_string (psymtab->objfile->global_psymbols.list
1059 				    + psymtab->globals_offset),
1060 				   psymtab->n_global_syms);
1061 		}
1062 	      else
1063 		printf_filtered ("(none)\n");
1064 	      printf_filtered ("    statics ");
1065 	      if (psymtab->n_static_syms)
1066 		{
1067 		  printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
1068 				   host_address_to_string (psymtab->objfile->static_psymbols.list
1069 				    + psymtab->statics_offset),
1070 				   psymtab->n_static_syms);
1071 		}
1072 	      else
1073 		printf_filtered ("(none)\n");
1074 	      printf_filtered ("    dependencies ");
1075 	      if (psymtab->number_of_dependencies)
1076 		{
1077 		  int i;
1078 
1079 		  printf_filtered ("{\n");
1080 		  for (i = 0; i < psymtab->number_of_dependencies; i++)
1081 		    {
1082 		      struct partial_symtab *dep = psymtab->dependencies[i];
1083 
1084 		      /* Note the string concatenation there --- no comma.  */
1085 		      printf_filtered ("      psymtab %s "
1086 				       "((struct partial_symtab *) %s)\n",
1087 				       dep->filename,
1088 				       host_address_to_string (dep));
1089 		    }
1090 		  printf_filtered ("    }\n");
1091 		}
1092 	      else
1093 		printf_filtered ("(none)\n");
1094 	      printf_filtered ("  }\n");
1095 	    }
1096 	}
1097 
1098       if (printed_objfile_start)
1099         printf_filtered ("}\n");
1100     }
1101 }
1102 
1103 
1104 /* Check consistency of psymtabs and symtabs.  */
1105 
1106 void
1107 maintenance_check_symtabs (char *ignore, int from_tty)
1108 {
1109   struct symbol *sym;
1110   struct partial_symbol **psym;
1111   struct symtab *s = NULL;
1112   struct partial_symtab *ps;
1113   struct blockvector *bv;
1114   struct objfile *objfile;
1115   struct block *b;
1116   int length;
1117 
1118   ALL_PSYMTABS (objfile, ps)
1119   {
1120     struct gdbarch *gdbarch = get_objfile_arch (objfile);
1121     s = PSYMTAB_TO_SYMTAB (ps);
1122     if (s == NULL)
1123       continue;
1124     bv = BLOCKVECTOR (s);
1125     b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1126     psym = ps->objfile->static_psymbols.list + ps->statics_offset;
1127     length = ps->n_static_syms;
1128     while (length--)
1129       {
1130 	sym = lookup_block_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
1131 				   NULL, SYMBOL_DOMAIN (*psym));
1132 	if (!sym)
1133 	  {
1134 	    printf_filtered ("Static symbol `");
1135 	    puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
1136 	    printf_filtered ("' only found in ");
1137 	    puts_filtered (ps->filename);
1138 	    printf_filtered (" psymtab\n");
1139 	  }
1140 	psym++;
1141       }
1142     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1143     psym = ps->objfile->global_psymbols.list + ps->globals_offset;
1144     length = ps->n_global_syms;
1145     while (length--)
1146       {
1147 	sym = lookup_block_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
1148 				   NULL, SYMBOL_DOMAIN (*psym));
1149 	if (!sym)
1150 	  {
1151 	    printf_filtered ("Global symbol `");
1152 	    puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
1153 	    printf_filtered ("' only found in ");
1154 	    puts_filtered (ps->filename);
1155 	    printf_filtered (" psymtab\n");
1156 	  }
1157 	psym++;
1158       }
1159     if (ps->texthigh < ps->textlow)
1160       {
1161 	printf_filtered ("Psymtab ");
1162 	puts_filtered (ps->filename);
1163 	printf_filtered (" covers bad range ");
1164 	fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
1165 	printf_filtered (" - ");
1166 	fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
1167 	printf_filtered ("\n");
1168 	continue;
1169       }
1170     if (ps->texthigh == 0)
1171       continue;
1172     if (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b))
1173       {
1174 	printf_filtered ("Psymtab ");
1175 	puts_filtered (ps->filename);
1176 	printf_filtered (" covers ");
1177 	fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
1178 	printf_filtered (" - ");
1179 	fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
1180 	printf_filtered (" but symtab covers only ");
1181 	fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
1182 	printf_filtered (" - ");
1183 	fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
1184 	printf_filtered ("\n");
1185       }
1186   }
1187 }
1188 
1189 
1190 /* Return the nexting depth of a block within other blocks in its symtab.  */
1191 
1192 static int
1193 block_depth (struct block *block)
1194 {
1195   int i = 0;
1196   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
1197     {
1198       i++;
1199     }
1200   return i;
1201 }
1202 
1203 
1204 /* Increase the space allocated for LISTP, which is probably
1205    global_psymbols or static_psymbols. This space will eventually
1206    be freed in free_objfile().  */
1207 
1208 void
1209 extend_psymbol_list (struct psymbol_allocation_list *listp,
1210 		     struct objfile *objfile)
1211 {
1212   int new_size;
1213   if (listp->size == 0)
1214     {
1215       new_size = 255;
1216       listp->list = (struct partial_symbol **)
1217 	xmalloc (new_size * sizeof (struct partial_symbol *));
1218     }
1219   else
1220     {
1221       new_size = listp->size * 2;
1222       listp->list = (struct partial_symbol **)
1223 	xrealloc ((char *) listp->list,
1224 		  new_size * sizeof (struct partial_symbol *));
1225     }
1226   /* Next assumes we only went one over.  Should be good if
1227      program works correctly */
1228   listp->next = listp->list + listp->size;
1229   listp->size = new_size;
1230 }
1231 
1232 
1233 /* Do early runtime initializations. */
1234 void
1235 _initialize_symmisc (void)
1236 {
1237   std_in = stdin;
1238   std_out = stdout;
1239   std_err = stderr;
1240 }
1241