OSDN Git Service

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