2011-03-23 Kai Tietz <ktietz@redhat.com>
[platform/upstream/binutils.git] / gdb / symmisc.c
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, 2010,
5    2011 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 "filenames.h"
27 #include "symfile.h"
28 #include "objfiles.h"
29 #include "breakpoint.h"
30 #include "command.h"
31 #include "gdb_obstack.h"
32 #include "exceptions.h"
33 #include "language.h"
34 #include "bcache.h"
35 #include "block.h"
36 #include "gdb_regex.h"
37 #include "gdb_stat.h"
38 #include "dictionary.h"
39
40 #include "gdb_string.h"
41 #include "readline/readline.h"
42
43 #include "psymtab.h"
44
45 #ifndef DEV_TTY
46 #define DEV_TTY "/dev/tty"
47 #endif
48
49 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
50    when calling functions that take FILE *'s from the debugger.
51    So we make a variable which has the same value and which is accessible when
52    debugging GDB with itself.  Because stdin et al need not be constants,
53    we initialize them in the _initialize_symmisc function at the bottom
54    of the file.  */
55 FILE *std_in;
56 FILE *std_out;
57 FILE *std_err;
58
59 /* Prototypes for local functions */
60
61 static void dump_symtab (struct objfile *, struct 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 void _initialize_symmisc (void);
71
72 struct print_symbol_args
73   {
74     struct gdbarch *gdbarch;
75     struct symbol *symbol;
76     int depth;
77     struct ui_file *outfile;
78   };
79
80 static int print_symbol (void *);
81 \f
82 /* Free all the storage associated with the struct symtab <- S.
83    Note that some symtabs have contents that all live inside one big block of
84    memory, and some share the contents of another symbol table and so you
85    should not free the contents on their behalf (except sometimes the
86    linetable, which maybe per symtab even when the rest is not).
87    It is s->free_code that says which alternative to use.  */
88
89 void
90 free_symtab (struct symtab *s)
91 {
92   switch (s->free_code)
93     {
94     case free_nothing:
95       /* All the contents are part of a big block of memory (an obstack),
96          and some other symtab is in charge of freeing that block.
97          Therefore, do nothing.  */
98       break;
99
100     case free_linetable:
101       /* Everything will be freed either by our `free_func'
102          or by some other symtab, except for our linetable.
103          Free that now.  */
104       if (LINETABLE (s))
105         xfree (LINETABLE (s));
106       break;
107     }
108
109   /* If there is a single block of memory to free, free it.  */
110   if (s->free_func != NULL)
111     s->free_func (s);
112
113   /* Free source-related stuff.  */
114   if (s->line_charpos != NULL)
115     xfree (s->line_charpos);
116   if (s->fullname != NULL)
117     xfree (s->fullname);
118   if (s->debugformat != NULL)
119     xfree (s->debugformat);
120   xfree (s);
121 }
122
123 void
124 print_symbol_bcache_statistics (void)
125 {
126   struct program_space *pspace;
127   struct objfile *objfile;
128
129   immediate_quit++;
130   ALL_PSPACES (pspace)
131     ALL_PSPACE_OBJFILES (pspace, objfile)
132   {
133     printf_filtered (_("Byte cache statistics for '%s':\n"), objfile->name);
134     print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
135                              "partial symbol cache");
136     print_bcache_statistics (objfile->macro_cache, "preprocessor macro cache");
137     print_bcache_statistics (objfile->filename_cache, "file name cache");
138   }
139   immediate_quit--;
140 }
141
142 void
143 print_objfile_statistics (void)
144 {
145   struct program_space *pspace;
146   struct objfile *objfile;
147   struct symtab *s;
148   int i, linetables, blockvectors;
149
150   immediate_quit++;
151   ALL_PSPACES (pspace)
152     ALL_PSPACE_OBJFILES (pspace, objfile)
153   {
154     printf_filtered (_("Statistics for '%s':\n"), objfile->name);
155     if (OBJSTAT (objfile, n_stabs) > 0)
156       printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
157                        OBJSTAT (objfile, n_stabs));
158     if (OBJSTAT (objfile, n_minsyms) > 0)
159       printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
160                        OBJSTAT (objfile, n_minsyms));
161     if (OBJSTAT (objfile, n_psyms) > 0)
162       printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
163                        OBJSTAT (objfile, n_psyms));
164     if (OBJSTAT (objfile, n_syms) > 0)
165       printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
166                        OBJSTAT (objfile, n_syms));
167     if (OBJSTAT (objfile, n_types) > 0)
168       printf_filtered (_("  Number of \"types\" defined: %d\n"),
169                        OBJSTAT (objfile, n_types));
170     if (objfile->sf)
171       objfile->sf->qf->print_stats (objfile);
172     i = linetables = blockvectors = 0;
173     ALL_OBJFILE_SYMTABS (objfile, s)
174       {
175         i++;
176         if (s->linetable != NULL)
177           linetables++;
178         if (s->primary == 1)
179           blockvectors++;
180       }
181     printf_filtered (_("  Number of symbol tables: %d\n"), i);
182     printf_filtered (_("  Number of symbol tables with line tables: %d\n"), 
183                      linetables);
184     printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"), 
185                      blockvectors);
186     
187     if (OBJSTAT (objfile, sz_strtab) > 0)
188       printf_filtered (_("  Space used by a.out string tables: %d\n"),
189                        OBJSTAT (objfile, sz_strtab));
190     printf_filtered (_("  Total memory used for objfile obstack: %d\n"),
191                      obstack_memory_used (&objfile->objfile_obstack));
192     printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
193                      bcache_memory_used (psymbol_bcache_get_bcache
194                                           (objfile->psymbol_cache)));
195     printf_filtered (_("  Total memory used for macro cache: %d\n"),
196                      bcache_memory_used (objfile->macro_cache));
197     printf_filtered (_("  Total memory used for file name cache: %d\n"),
198                      bcache_memory_used (objfile->filename_cache));
199   }
200   immediate_quit--;
201 }
202
203 static void
204 dump_objfile (struct objfile *objfile)
205 {
206   struct symtab *symtab;
207
208   printf_filtered ("\nObject file %s:  ", objfile->name);
209   printf_filtered ("Objfile at ");
210   gdb_print_host_address (objfile, gdb_stdout);
211   printf_filtered (", bfd at ");
212   gdb_print_host_address (objfile->obfd, gdb_stdout);
213   printf_filtered (", %d minsyms\n\n",
214                    objfile->minimal_symbol_count);
215
216   if (objfile->sf)
217     objfile->sf->qf->dump (objfile);
218
219   if (objfile->symtabs)
220     {
221       printf_filtered ("Symtabs:\n");
222       for (symtab = objfile->symtabs;
223            symtab != NULL;
224            symtab = symtab->next)
225         {
226           printf_filtered ("%s at ", symtab->filename);
227           gdb_print_host_address (symtab, gdb_stdout);
228           printf_filtered (", ");
229           if (symtab->objfile != objfile)
230             {
231               printf_filtered ("NOT ON CHAIN!  ");
232             }
233           wrap_here ("  ");
234         }
235       printf_filtered ("\n\n");
236     }
237 }
238
239 /* Print minimal symbols from this objfile.  */
240
241 static void
242 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
243 {
244   struct gdbarch *gdbarch = get_objfile_arch (objfile);
245   struct minimal_symbol *msymbol;
246   int index;
247   char ms_type;
248
249   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile->name);
250   if (objfile->minimal_symbol_count == 0)
251     {
252       fprintf_filtered (outfile, "No minimal symbols found.\n");
253       return;
254     }
255   index = 0;
256   ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
257     {
258       struct obj_section *section = SYMBOL_OBJ_SECTION (msymbol);
259
260       switch (MSYMBOL_TYPE (msymbol))
261         {
262         case mst_unknown:
263           ms_type = 'u';
264           break;
265         case mst_text:
266           ms_type = 'T';
267           break;
268         case mst_solib_trampoline:
269           ms_type = 'S';
270           break;
271         case mst_data:
272           ms_type = 'D';
273           break;
274         case mst_bss:
275           ms_type = 'B';
276           break;
277         case mst_abs:
278           ms_type = 'A';
279           break;
280         case mst_file_text:
281           ms_type = 't';
282           break;
283         case mst_file_data:
284           ms_type = 'd';
285           break;
286         case mst_file_bss:
287           ms_type = 'b';
288           break;
289         default:
290           ms_type = '?';
291           break;
292         }
293       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
294       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
295                       outfile);
296       fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
297       if (section)
298         fprintf_filtered (outfile, " section %s",
299                           bfd_section_name (objfile->obfd,
300                                             section->the_bfd_section));
301       if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
302         {
303           fprintf_filtered (outfile, "  %s", SYMBOL_DEMANGLED_NAME (msymbol));
304         }
305       if (msymbol->filename)
306         fprintf_filtered (outfile, "  %s", msymbol->filename);
307       fputs_filtered ("\n", outfile);
308       index++;
309     }
310   if (objfile->minimal_symbol_count != index)
311     {
312       warning (_("internal error:  minimal symbol count %d != %d"),
313                objfile->minimal_symbol_count, index);
314     }
315   fprintf_filtered (outfile, "\n");
316 }
317
318 static void
319 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
320                struct ui_file *outfile)
321 {
322   struct gdbarch *gdbarch = get_objfile_arch (objfile);
323   int i;
324   struct dict_iterator iter;
325   int len;
326   struct linetable *l;
327   struct blockvector *bv;
328   struct symbol *sym;
329   struct block *b;
330   int depth;
331
332   fprintf_filtered (outfile, "\nSymtab for file %s\n", symtab->filename);
333   if (symtab->dirname)
334     fprintf_filtered (outfile, "Compilation directory is %s\n",
335                       symtab->dirname);
336   fprintf_filtered (outfile, "Read from object file %s (", objfile->name);
337   gdb_print_host_address (objfile, outfile);
338   fprintf_filtered (outfile, ")\n");
339   fprintf_filtered (outfile, "Language: %s\n",
340                     language_str (symtab->language));
341
342   /* First print the line table.  */
343   l = LINETABLE (symtab);
344   if (l)
345     {
346       fprintf_filtered (outfile, "\nLine table:\n\n");
347       len = l->nitems;
348       for (i = 0; i < len; i++)
349         {
350           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
351           fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
352           fprintf_filtered (outfile, "\n");
353         }
354     }
355   /* Now print the block info, but only for primary symtabs since we will
356      print lots of duplicate info otherwise.  */
357   if (symtab->primary)
358     {
359       fprintf_filtered (outfile, "\nBlockvector:\n\n");
360       bv = BLOCKVECTOR (symtab);
361       len = BLOCKVECTOR_NBLOCKS (bv);
362       for (i = 0; i < len; i++)
363         {
364           b = BLOCKVECTOR_BLOCK (bv, i);
365           depth = block_depth (b) * 2;
366           print_spaces (depth, outfile);
367           fprintf_filtered (outfile, "block #%03d, object at ", i);
368           gdb_print_host_address (b, outfile);
369           if (BLOCK_SUPERBLOCK (b))
370             {
371               fprintf_filtered (outfile, " under ");
372               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
373             }
374           /* drow/2002-07-10: We could save the total symbols count
375              even if we're using a hashtable, but nothing else but this message
376              wants it.  */
377           fprintf_filtered (outfile, ", %d syms/buckets in ",
378                             dict_size (BLOCK_DICT (b)));
379           fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
380           fprintf_filtered (outfile, "..");
381           fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
382           if (BLOCK_FUNCTION (b))
383             {
384               fprintf_filtered (outfile, ", function %s",
385                                 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
386               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
387                 {
388                   fprintf_filtered (outfile, ", %s",
389                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
390                 }
391             }
392           fprintf_filtered (outfile, "\n");
393           /* Now print each symbol in this block (in no particular order, if
394              we're using a hashtable).  */
395           ALL_BLOCK_SYMBOLS (b, iter, sym)
396             {
397               struct print_symbol_args s;
398
399               s.gdbarch = gdbarch;
400               s.symbol = sym;
401               s.depth = depth + 1;
402               s.outfile = outfile;
403               catch_errors (print_symbol, &s, "Error printing symbol:\n",
404                             RETURN_MASK_ERROR);
405             }
406         }
407       fprintf_filtered (outfile, "\n");
408     }
409   else
410     {
411       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
412     }
413 }
414
415 static void
416 dump_symtab (struct objfile *objfile, struct symtab *symtab,
417              struct ui_file *outfile)
418 {
419   /* Set the current language to the language of the symtab we're dumping
420      because certain routines used during dump_symtab() use the current
421      language to print an image of the symbol.  We'll restore it later.
422      But use only real languages, not placeholders.  */
423   if (symtab->language != language_unknown
424       && symtab->language != language_auto)
425     {
426       enum language saved_lang;
427
428       saved_lang = set_language (symtab->language);
429
430       dump_symtab_1 (objfile, symtab, outfile);
431
432       set_language (saved_lang);
433     }
434   else
435     dump_symtab_1 (objfile, symtab, outfile);
436 }
437
438 void
439 maintenance_print_symbols (char *args, int from_tty)
440 {
441   char **argv;
442   struct ui_file *outfile;
443   struct cleanup *cleanups;
444   char *symname = NULL;
445   char *filename = DEV_TTY;
446   struct objfile *objfile;
447   struct symtab *s;
448
449   dont_repeat ();
450
451   if (args == NULL)
452     {
453       error (_("Arguments missing: an output file name "
454                "and an optional symbol file name"));
455     }
456   argv = gdb_buildargv (args);
457   cleanups = make_cleanup_freeargv (argv);
458
459   if (argv[0] != NULL)
460     {
461       filename = argv[0];
462       /* If a second arg is supplied, it is a source file name to match on.  */
463       if (argv[1] != NULL)
464         {
465           symname = argv[1];
466         }
467     }
468
469   filename = tilde_expand (filename);
470   make_cleanup (xfree, filename);
471
472   outfile = gdb_fopen (filename, FOPEN_WT);
473   if (outfile == 0)
474     perror_with_name (filename);
475   make_cleanup_ui_file_delete (outfile);
476
477   immediate_quit++;
478   ALL_SYMTABS (objfile, s)
479     if (symname == NULL || filename_cmp (symname, s->filename) == 0)
480     dump_symtab (objfile, s, outfile);
481   immediate_quit--;
482   do_cleanups (cleanups);
483 }
484
485 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
486    far to indent.  ARGS is really a struct print_symbol_args *, but is
487    declared as char * to get it past catch_errors.  Returns 0 for error,
488    1 for success.  */
489
490 static int
491 print_symbol (void *args)
492 {
493   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
494   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
495   int depth = ((struct print_symbol_args *) args)->depth;
496   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
497   struct obj_section *section = SYMBOL_OBJ_SECTION (symbol);
498
499   print_spaces (depth, outfile);
500   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
501     {
502       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
503       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
504                       outfile);
505       if (section)
506         fprintf_filtered (outfile, " section %s\n",
507                           bfd_section_name (section->the_bfd_section->owner,
508                                             section->the_bfd_section));
509       else
510         fprintf_filtered (outfile, "\n");
511       return 1;
512     }
513   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
514     {
515       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
516         {
517           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
518         }
519       else
520         {
521           fprintf_filtered (outfile, "%s %s = ",
522                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
523                           ? "enum"
524                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
525                         ? "struct" : "union")),
526                             SYMBOL_LINKAGE_NAME (symbol));
527           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth);
528         }
529       fprintf_filtered (outfile, ";\n");
530     }
531   else
532     {
533       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
534         fprintf_filtered (outfile, "typedef ");
535       if (SYMBOL_TYPE (symbol))
536         {
537           /* Print details of types, except for enums where it's clutter.  */
538           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
539                          outfile,
540                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
541                          depth);
542           fprintf_filtered (outfile, "; ");
543         }
544       else
545         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
546
547       switch (SYMBOL_CLASS (symbol))
548         {
549         case LOC_CONST:
550           fprintf_filtered (outfile, "const %ld (0x%lx)",
551                             SYMBOL_VALUE (symbol),
552                             SYMBOL_VALUE (symbol));
553           break;
554
555         case LOC_CONST_BYTES:
556           {
557             unsigned i;
558             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
559
560             fprintf_filtered (outfile, "const %u hex bytes:",
561                               TYPE_LENGTH (type));
562             for (i = 0; i < TYPE_LENGTH (type); i++)
563               fprintf_filtered (outfile, " %02x",
564                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
565           }
566           break;
567
568         case LOC_STATIC:
569           fprintf_filtered (outfile, "static at ");
570           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
571                           outfile);
572           if (section)
573             fprintf_filtered (outfile, " section %s",
574                               bfd_section_name (section->the_bfd_section->owner,
575                                                 section->the_bfd_section));
576           break;
577
578         case LOC_REGISTER:
579           if (SYMBOL_IS_ARGUMENT (symbol))
580             fprintf_filtered (outfile, "parameter register %ld",
581                               SYMBOL_VALUE (symbol));
582           else
583             fprintf_filtered (outfile, "register %ld", SYMBOL_VALUE (symbol));
584           break;
585
586         case LOC_ARG:
587           fprintf_filtered (outfile, "arg at offset 0x%lx",
588                             SYMBOL_VALUE (symbol));
589           break;
590
591         case LOC_REF_ARG:
592           fprintf_filtered (outfile, "reference arg at 0x%lx", SYMBOL_VALUE (symbol));
593           break;
594
595         case LOC_REGPARM_ADDR:
596           fprintf_filtered (outfile, "address parameter register %ld", SYMBOL_VALUE (symbol));
597           break;
598
599         case LOC_LOCAL:
600           fprintf_filtered (outfile, "local at offset 0x%lx",
601                             SYMBOL_VALUE (symbol));
602           break;
603
604         case LOC_TYPEDEF:
605           break;
606
607         case LOC_LABEL:
608           fprintf_filtered (outfile, "label at ");
609           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
610                           outfile);
611           if (section)
612             fprintf_filtered (outfile, " section %s",
613                               bfd_section_name (section->the_bfd_section->owner,
614                                                 section->the_bfd_section));
615           break;
616
617         case LOC_BLOCK:
618           fprintf_filtered (outfile, "block object ");
619           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
620           fprintf_filtered (outfile, ", ");
621           fputs_filtered (paddress (gdbarch,
622                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
623                           outfile);
624           fprintf_filtered (outfile, "..");
625           fputs_filtered (paddress (gdbarch,
626                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
627                           outfile);
628           if (section)
629             fprintf_filtered (outfile, " section %s",
630                               bfd_section_name (section->the_bfd_section->owner,
631                                                 section->the_bfd_section));
632           break;
633
634         case LOC_COMPUTED:
635           fprintf_filtered (outfile, "computed at runtime");
636           break;
637
638         case LOC_UNRESOLVED:
639           fprintf_filtered (outfile, "unresolved");
640           break;
641
642         case LOC_OPTIMIZED_OUT:
643           fprintf_filtered (outfile, "optimized out");
644           break;
645
646         default:
647           fprintf_filtered (outfile, "botched symbol class %x",
648                             SYMBOL_CLASS (symbol));
649           break;
650         }
651     }
652   fprintf_filtered (outfile, "\n");
653   return 1;
654 }
655
656 void
657 maintenance_print_msymbols (char *args, int from_tty)
658 {
659   char **argv;
660   struct ui_file *outfile;
661   struct cleanup *cleanups;
662   char *filename = DEV_TTY;
663   char *symname = NULL;
664   struct program_space *pspace;
665   struct objfile *objfile;
666
667   struct stat sym_st, obj_st;
668
669   dont_repeat ();
670
671   if (args == NULL)
672     {
673       error (_("print-msymbols takes an output file "
674                "name and optional symbol file name"));
675     }
676   argv = gdb_buildargv (args);
677   cleanups = make_cleanup_freeargv (argv);
678
679   if (argv[0] != NULL)
680     {
681       filename = argv[0];
682       /* If a second arg is supplied, it is a source file name to match on.  */
683       if (argv[1] != NULL)
684         {
685           symname = xfullpath (argv[1]);
686           make_cleanup (xfree, symname);
687           if (symname && stat (symname, &sym_st))
688             perror_with_name (symname);
689         }
690     }
691
692   filename = tilde_expand (filename);
693   make_cleanup (xfree, filename);
694
695   outfile = gdb_fopen (filename, FOPEN_WT);
696   if (outfile == 0)
697     perror_with_name (filename);
698   make_cleanup_ui_file_delete (outfile);
699
700   immediate_quit++;
701   ALL_PSPACES (pspace)
702     ALL_PSPACE_OBJFILES (pspace, objfile)
703       if (symname == NULL || (!stat (objfile->name, &obj_st)
704                               && sym_st.st_ino == obj_st.st_ino))
705         dump_msymbols (objfile, outfile);
706   immediate_quit--;
707   fprintf_filtered (outfile, "\n\n");
708   do_cleanups (cleanups);
709 }
710
711 void
712 maintenance_print_objfiles (char *ignore, int from_tty)
713 {
714   struct program_space *pspace;
715   struct objfile *objfile;
716
717   dont_repeat ();
718
719   immediate_quit++;
720   ALL_PSPACES (pspace)
721     ALL_PSPACE_OBJFILES (pspace, objfile)
722       dump_objfile (objfile);
723   immediate_quit--;
724 }
725
726
727 /* List all the symbol tables whose names match REGEXP (optional).  */
728 void
729 maintenance_info_symtabs (char *regexp, int from_tty)
730 {
731   struct program_space *pspace;
732   struct objfile *objfile;
733
734   if (regexp)
735     re_comp (regexp);
736
737   ALL_PSPACES (pspace)
738     ALL_PSPACE_OBJFILES (pspace, objfile)
739     {
740       struct symtab *symtab;
741       
742       /* We don't want to print anything for this objfile until we
743          actually find a symtab whose name matches.  */
744       int printed_objfile_start = 0;
745
746       ALL_OBJFILE_SYMTABS (objfile, symtab)
747         {
748           QUIT;
749
750           if (! regexp
751               || re_exec (symtab->filename))
752             {
753               if (! printed_objfile_start)
754                 {
755                   printf_filtered ("{ objfile %s ", objfile->name);
756                   wrap_here ("  ");
757                   printf_filtered ("((struct objfile *) %s)\n", 
758                                    host_address_to_string (objfile));
759                   printed_objfile_start = 1;
760                 }
761
762               printf_filtered ("        { symtab %s ", symtab->filename);
763               wrap_here ("    ");
764               printf_filtered ("((struct symtab *) %s)\n", 
765                                host_address_to_string (symtab));
766               printf_filtered ("          dirname %s\n",
767                                symtab->dirname ? symtab->dirname : "(null)");
768               printf_filtered ("          fullname %s\n",
769                                symtab->fullname ? symtab->fullname : "(null)");
770               printf_filtered ("          "
771                                "blockvector ((struct blockvector *) %s)%s\n",
772                                host_address_to_string (symtab->blockvector),
773                                symtab->primary ? " (primary)" : "");
774               printf_filtered ("          "
775                                "linetable ((struct linetable *) %s)\n",
776                                host_address_to_string (symtab->linetable));
777               printf_filtered ("          debugformat %s\n",
778                                symtab->debugformat);
779               printf_filtered ("        }\n");
780             }
781         }
782
783       if (printed_objfile_start)
784         printf_filtered ("}\n");
785     }
786 }
787 \f
788
789 /* Return the nexting depth of a block within other blocks in its symtab.  */
790
791 static int
792 block_depth (struct block *block)
793 {
794   int i = 0;
795
796   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
797     {
798       i++;
799     }
800   return i;
801 }
802 \f
803
804 /* Do early runtime initializations.  */
805 void
806 _initialize_symmisc (void)
807 {
808   std_in = stdin;
809   std_out = stdout;
810   std_err = stderr;
811 }