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