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