GDB (API)
/home/stan/gdb/src/gdb/symmisc.c
Go to the documentation of this file.
00001 /* Do various things to symbol tables (other than lookup), for GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "bfd.h"
00024 #include "filenames.h"
00025 #include "symfile.h"
00026 #include "objfiles.h"
00027 #include "breakpoint.h"
00028 #include "command.h"
00029 #include "gdb_obstack.h"
00030 #include "exceptions.h"
00031 #include "language.h"
00032 #include "bcache.h"
00033 #include "block.h"
00034 #include "gdb_regex.h"
00035 #include "gdb_stat.h"
00036 #include "dictionary.h"
00037 #include "typeprint.h"
00038 #include "gdbcmd.h"
00039 #include "source.h"
00040 
00041 #include "gdb_string.h"
00042 #include "readline/readline.h"
00043 
00044 #include "psymtab.h"
00045 
00046 #ifndef DEV_TTY
00047 #define DEV_TTY "/dev/tty"
00048 #endif
00049 
00050 /* Unfortunately for debugging, stderr is usually a macro.  This is painful
00051    when calling functions that take FILE *'s from the debugger.
00052    So we make a variable which has the same value and which is accessible when
00053    debugging GDB with itself.  Because stdin et al need not be constants,
00054    we initialize them in the _initialize_symmisc function at the bottom
00055    of the file.  */
00056 FILE *std_in;
00057 FILE *std_out;
00058 FILE *std_err;
00059 
00060 /* Prototypes for local functions */
00061 
00062 static int block_depth (struct block *);
00063 
00064 void _initialize_symmisc (void);
00065 
00066 struct print_symbol_args
00067   {
00068     struct gdbarch *gdbarch;
00069     struct symbol *symbol;
00070     int depth;
00071     struct ui_file *outfile;
00072   };
00073 
00074 static int print_symbol (void *);
00075 
00076 
00077 void
00078 print_symbol_bcache_statistics (void)
00079 {
00080   struct program_space *pspace;
00081   struct objfile *objfile;
00082 
00083   ALL_PSPACES (pspace)
00084     ALL_PSPACE_OBJFILES (pspace, objfile)
00085   {
00086     QUIT;
00087     printf_filtered (_("Byte cache statistics for '%s':\n"),
00088                      objfile_name (objfile));
00089     print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
00090                              "partial symbol cache");
00091     print_bcache_statistics (objfile->per_bfd->macro_cache,
00092                              "preprocessor macro cache");
00093     print_bcache_statistics (objfile->per_bfd->filename_cache,
00094                              "file name cache");
00095   }
00096 }
00097 
00098 void
00099 print_objfile_statistics (void)
00100 {
00101   struct program_space *pspace;
00102   struct objfile *objfile;
00103   struct symtab *s;
00104   int i, linetables, blockvectors;
00105 
00106   ALL_PSPACES (pspace)
00107     ALL_PSPACE_OBJFILES (pspace, objfile)
00108   {
00109     QUIT;
00110     printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
00111     if (OBJSTAT (objfile, n_stabs) > 0)
00112       printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
00113                        OBJSTAT (objfile, n_stabs));
00114     if (OBJSTAT (objfile, n_minsyms) > 0)
00115       printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
00116                        OBJSTAT (objfile, n_minsyms));
00117     if (OBJSTAT (objfile, n_psyms) > 0)
00118       printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
00119                        OBJSTAT (objfile, n_psyms));
00120     if (OBJSTAT (objfile, n_syms) > 0)
00121       printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
00122                        OBJSTAT (objfile, n_syms));
00123     if (OBJSTAT (objfile, n_types) > 0)
00124       printf_filtered (_("  Number of \"types\" defined: %d\n"),
00125                        OBJSTAT (objfile, n_types));
00126     if (objfile->sf)
00127       objfile->sf->qf->print_stats (objfile);
00128     i = linetables = blockvectors = 0;
00129     ALL_OBJFILE_SYMTABS (objfile, s)
00130       {
00131         i++;
00132         if (s->linetable != NULL)
00133           linetables++;
00134         if (s->primary == 1)
00135           blockvectors++;
00136       }
00137     printf_filtered (_("  Number of symbol tables: %d\n"), i);
00138     printf_filtered (_("  Number of symbol tables with line tables: %d\n"), 
00139                      linetables);
00140     printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"), 
00141                      blockvectors);
00142     
00143     if (OBJSTAT (objfile, sz_strtab) > 0)
00144       printf_filtered (_("  Space used by a.out string tables: %d\n"),
00145                        OBJSTAT (objfile, sz_strtab));
00146     printf_filtered (_("  Total memory used for objfile obstack: %d\n"),
00147                      obstack_memory_used (&objfile->objfile_obstack));
00148     printf_filtered (_("  Total memory used for BFD obstack: %d\n"),
00149                      obstack_memory_used (&objfile->per_bfd->storage_obstack));
00150     printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
00151                      bcache_memory_used (psymbol_bcache_get_bcache
00152                                           (objfile->psymbol_cache)));
00153     printf_filtered (_("  Total memory used for macro cache: %d\n"),
00154                      bcache_memory_used (objfile->per_bfd->macro_cache));
00155     printf_filtered (_("  Total memory used for file name cache: %d\n"),
00156                      bcache_memory_used (objfile->per_bfd->filename_cache));
00157   }
00158 }
00159 
00160 static void
00161 dump_objfile (struct objfile *objfile)
00162 {
00163   struct symtab *symtab;
00164 
00165   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
00166   printf_filtered ("Objfile at ");
00167   gdb_print_host_address (objfile, gdb_stdout);
00168   printf_filtered (", bfd at ");
00169   gdb_print_host_address (objfile->obfd, gdb_stdout);
00170   printf_filtered (", %d minsyms\n\n",
00171                    objfile->minimal_symbol_count);
00172 
00173   if (objfile->sf)
00174     objfile->sf->qf->dump (objfile);
00175 
00176   if (objfile->symtabs)
00177     {
00178       printf_filtered ("Symtabs:\n");
00179       for (symtab = objfile->symtabs;
00180            symtab != NULL;
00181            symtab = symtab->next)
00182         {
00183           printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
00184           gdb_print_host_address (symtab, gdb_stdout);
00185           printf_filtered (", ");
00186           if (symtab->objfile != objfile)
00187             {
00188               printf_filtered ("NOT ON CHAIN!  ");
00189             }
00190           wrap_here ("  ");
00191         }
00192       printf_filtered ("\n\n");
00193     }
00194 }
00195 
00196 /* Print minimal symbols from this objfile.  */
00197 
00198 static void
00199 dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
00200 {
00201   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00202   struct minimal_symbol *msymbol;
00203   int index;
00204   char ms_type;
00205 
00206   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
00207   if (objfile->minimal_symbol_count == 0)
00208     {
00209       fprintf_filtered (outfile, "No minimal symbols found.\n");
00210       return;
00211     }
00212   index = 0;
00213   ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
00214     {
00215       struct obj_section *section = SYMBOL_OBJ_SECTION (objfile, msymbol);
00216 
00217       switch (MSYMBOL_TYPE (msymbol))
00218         {
00219         case mst_unknown:
00220           ms_type = 'u';
00221           break;
00222         case mst_text:
00223           ms_type = 'T';
00224           break;
00225         case mst_text_gnu_ifunc:
00226           ms_type = 'i';
00227           break;
00228         case mst_solib_trampoline:
00229           ms_type = 'S';
00230           break;
00231         case mst_data:
00232           ms_type = 'D';
00233           break;
00234         case mst_bss:
00235           ms_type = 'B';
00236           break;
00237         case mst_abs:
00238           ms_type = 'A';
00239           break;
00240         case mst_file_text:
00241           ms_type = 't';
00242           break;
00243         case mst_file_data:
00244           ms_type = 'd';
00245           break;
00246         case mst_file_bss:
00247           ms_type = 'b';
00248           break;
00249         default:
00250           ms_type = '?';
00251           break;
00252         }
00253       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
00254       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (msymbol)),
00255                       outfile);
00256       fprintf_filtered (outfile, " %s", SYMBOL_LINKAGE_NAME (msymbol));
00257       if (section)
00258         {
00259           if (section->the_bfd_section != NULL)
00260             fprintf_filtered (outfile, " section %s",
00261                               bfd_section_name (objfile->obfd,
00262                                                 section->the_bfd_section));
00263           else
00264             fprintf_filtered (outfile, " spurious section %ld",
00265                               (long) (section - objfile->sections));
00266         }
00267       if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
00268         {
00269           fprintf_filtered (outfile, "  %s", SYMBOL_DEMANGLED_NAME (msymbol));
00270         }
00271       if (msymbol->filename)
00272         fprintf_filtered (outfile, "  %s", msymbol->filename);
00273       fputs_filtered ("\n", outfile);
00274       index++;
00275     }
00276   if (objfile->minimal_symbol_count != index)
00277     {
00278       warning (_("internal error:  minimal symbol count %d != %d"),
00279                objfile->minimal_symbol_count, index);
00280     }
00281   fprintf_filtered (outfile, "\n");
00282 }
00283 
00284 static void
00285 dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
00286                struct ui_file *outfile)
00287 {
00288   struct gdbarch *gdbarch = get_objfile_arch (objfile);
00289   int i;
00290   struct dict_iterator iter;
00291   int len;
00292   struct linetable *l;
00293   struct blockvector *bv;
00294   struct symbol *sym;
00295   struct block *b;
00296   int depth;
00297 
00298   fprintf_filtered (outfile, "\nSymtab for file %s\n",
00299                     symtab_to_filename_for_display (symtab));
00300   if (symtab->dirname)
00301     fprintf_filtered (outfile, "Compilation directory is %s\n",
00302                       symtab->dirname);
00303   fprintf_filtered (outfile, "Read from object file %s (",
00304                     objfile_name (objfile));
00305   gdb_print_host_address (objfile, outfile);
00306   fprintf_filtered (outfile, ")\n");
00307   fprintf_filtered (outfile, "Language: %s\n",
00308                     language_str (symtab->language));
00309 
00310   /* First print the line table.  */
00311   l = LINETABLE (symtab);
00312   if (l)
00313     {
00314       fprintf_filtered (outfile, "\nLine table:\n\n");
00315       len = l->nitems;
00316       for (i = 0; i < len; i++)
00317         {
00318           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
00319           fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
00320           fprintf_filtered (outfile, "\n");
00321         }
00322     }
00323   /* Now print the block info, but only for primary symtabs since we will
00324      print lots of duplicate info otherwise.  */
00325   if (symtab->primary)
00326     {
00327       fprintf_filtered (outfile, "\nBlockvector:\n\n");
00328       bv = BLOCKVECTOR (symtab);
00329       len = BLOCKVECTOR_NBLOCKS (bv);
00330       for (i = 0; i < len; i++)
00331         {
00332           b = BLOCKVECTOR_BLOCK (bv, i);
00333           depth = block_depth (b) * 2;
00334           print_spaces (depth, outfile);
00335           fprintf_filtered (outfile, "block #%03d, object at ", i);
00336           gdb_print_host_address (b, outfile);
00337           if (BLOCK_SUPERBLOCK (b))
00338             {
00339               fprintf_filtered (outfile, " under ");
00340               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
00341             }
00342           /* drow/2002-07-10: We could save the total symbols count
00343              even if we're using a hashtable, but nothing else but this message
00344              wants it.  */
00345           fprintf_filtered (outfile, ", %d syms/buckets in ",
00346                             dict_size (BLOCK_DICT (b)));
00347           fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
00348           fprintf_filtered (outfile, "..");
00349           fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
00350           if (BLOCK_FUNCTION (b))
00351             {
00352               fprintf_filtered (outfile, ", function %s",
00353                                 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
00354               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
00355                 {
00356                   fprintf_filtered (outfile, ", %s",
00357                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
00358                 }
00359             }
00360           fprintf_filtered (outfile, "\n");
00361           /* Now print each symbol in this block (in no particular order, if
00362              we're using a hashtable).  Note that we only want this
00363              block, not any blocks from included symtabs.  */
00364           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
00365             {
00366               struct print_symbol_args s;
00367 
00368               s.gdbarch = gdbarch;
00369               s.symbol = sym;
00370               s.depth = depth + 1;
00371               s.outfile = outfile;
00372               catch_errors (print_symbol, &s, "Error printing symbol:\n",
00373                             RETURN_MASK_ERROR);
00374             }
00375         }
00376       fprintf_filtered (outfile, "\n");
00377     }
00378   else
00379     {
00380       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
00381     }
00382 }
00383 
00384 static void
00385 dump_symtab (struct objfile *objfile, struct symtab *symtab,
00386              struct ui_file *outfile)
00387 {
00388   /* Set the current language to the language of the symtab we're dumping
00389      because certain routines used during dump_symtab() use the current
00390      language to print an image of the symbol.  We'll restore it later.
00391      But use only real languages, not placeholders.  */
00392   if (symtab->language != language_unknown
00393       && symtab->language != language_auto)
00394     {
00395       enum language saved_lang;
00396 
00397       saved_lang = set_language (symtab->language);
00398 
00399       dump_symtab_1 (objfile, symtab, outfile);
00400 
00401       set_language (saved_lang);
00402     }
00403   else
00404     dump_symtab_1 (objfile, symtab, outfile);
00405 }
00406 
00407 static void
00408 maintenance_print_symbols (char *args, int from_tty)
00409 {
00410   char **argv;
00411   struct ui_file *outfile;
00412   struct cleanup *cleanups;
00413   char *symname = NULL;
00414   char *filename = DEV_TTY;
00415   struct objfile *objfile;
00416   struct symtab *s;
00417 
00418   dont_repeat ();
00419 
00420   if (args == NULL)
00421     {
00422       error (_("Arguments missing: an output file name "
00423                "and an optional symbol file name"));
00424     }
00425   argv = gdb_buildargv (args);
00426   cleanups = make_cleanup_freeargv (argv);
00427 
00428   if (argv[0] != NULL)
00429     {
00430       filename = argv[0];
00431       /* If a second arg is supplied, it is a source file name to match on.  */
00432       if (argv[1] != NULL)
00433         {
00434           symname = argv[1];
00435         }
00436     }
00437 
00438   filename = tilde_expand (filename);
00439   make_cleanup (xfree, filename);
00440 
00441   outfile = gdb_fopen (filename, FOPEN_WT);
00442   if (outfile == 0)
00443     perror_with_name (filename);
00444   make_cleanup_ui_file_delete (outfile);
00445 
00446   ALL_SYMTABS (objfile, s)
00447     {
00448       QUIT;
00449       if (symname == NULL
00450           || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
00451         dump_symtab (objfile, s, outfile);
00452     }
00453   do_cleanups (cleanups);
00454 }
00455 
00456 /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
00457    far to indent.  ARGS is really a struct print_symbol_args *, but is
00458    declared as char * to get it past catch_errors.  Returns 0 for error,
00459    1 for success.  */
00460 
00461 static int
00462 print_symbol (void *args)
00463 {
00464   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
00465   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
00466   int depth = ((struct print_symbol_args *) args)->depth;
00467   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
00468   struct obj_section *section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (symbol),
00469                                                     symbol);
00470 
00471   print_spaces (depth, outfile);
00472   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
00473     {
00474       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
00475       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
00476                       outfile);
00477       if (section)
00478         fprintf_filtered (outfile, " section %s\n",
00479                           bfd_section_name (section->the_bfd_section->owner,
00480                                             section->the_bfd_section));
00481       else
00482         fprintf_filtered (outfile, "\n");
00483       return 1;
00484     }
00485   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
00486     {
00487       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
00488         {
00489           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
00490                          &type_print_raw_options);
00491         }
00492       else
00493         {
00494           fprintf_filtered (outfile, "%s %s = ",
00495                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
00496                           ? "enum"
00497                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
00498                         ? "struct" : "union")),
00499                             SYMBOL_LINKAGE_NAME (symbol));
00500           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
00501                          &type_print_raw_options);
00502         }
00503       fprintf_filtered (outfile, ";\n");
00504     }
00505   else
00506     {
00507       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
00508         fprintf_filtered (outfile, "typedef ");
00509       if (SYMBOL_TYPE (symbol))
00510         {
00511           /* Print details of types, except for enums where it's clutter.  */
00512           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
00513                          outfile,
00514                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
00515                          depth,
00516                          &type_print_raw_options);
00517           fprintf_filtered (outfile, "; ");
00518         }
00519       else
00520         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));
00521 
00522       switch (SYMBOL_CLASS (symbol))
00523         {
00524         case LOC_CONST:
00525           fprintf_filtered (outfile, "const %s (%s)",
00526                             plongest (SYMBOL_VALUE (symbol)),
00527                             hex_string (SYMBOL_VALUE (symbol)));
00528           break;
00529 
00530         case LOC_CONST_BYTES:
00531           {
00532             unsigned i;
00533             struct type *type = check_typedef (SYMBOL_TYPE (symbol));
00534 
00535             fprintf_filtered (outfile, "const %u hex bytes:",
00536                               TYPE_LENGTH (type));
00537             for (i = 0; i < TYPE_LENGTH (type); i++)
00538               fprintf_filtered (outfile, " %02x",
00539                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
00540           }
00541           break;
00542 
00543         case LOC_STATIC:
00544           fprintf_filtered (outfile, "static at ");
00545           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
00546                           outfile);
00547           if (section)
00548             fprintf_filtered (outfile, " section %s",
00549                               bfd_section_name (section->the_bfd_section->owner,
00550                                                 section->the_bfd_section));
00551           break;
00552 
00553         case LOC_REGISTER:
00554           if (SYMBOL_IS_ARGUMENT (symbol))
00555             fprintf_filtered (outfile, "parameter register %s",
00556                               plongest (SYMBOL_VALUE (symbol)));
00557           else
00558             fprintf_filtered (outfile, "register %s",
00559                               plongest (SYMBOL_VALUE (symbol)));
00560           break;
00561 
00562         case LOC_ARG:
00563           fprintf_filtered (outfile, "arg at offset %s",
00564                             hex_string (SYMBOL_VALUE (symbol)));
00565           break;
00566 
00567         case LOC_REF_ARG:
00568           fprintf_filtered (outfile, "reference arg at %s",
00569                             hex_string (SYMBOL_VALUE (symbol)));
00570           break;
00571 
00572         case LOC_REGPARM_ADDR:
00573           fprintf_filtered (outfile, "address parameter register %s",
00574                             plongest (SYMBOL_VALUE (symbol)));
00575           break;
00576 
00577         case LOC_LOCAL:
00578           fprintf_filtered (outfile, "local at offset %s",
00579                             hex_string (SYMBOL_VALUE (symbol)));
00580           break;
00581 
00582         case LOC_TYPEDEF:
00583           break;
00584 
00585         case LOC_LABEL:
00586           fprintf_filtered (outfile, "label at ");
00587           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
00588                           outfile);
00589           if (section)
00590             fprintf_filtered (outfile, " section %s",
00591                               bfd_section_name (section->the_bfd_section->owner,
00592                                                 section->the_bfd_section));
00593           break;
00594 
00595         case LOC_BLOCK:
00596           fprintf_filtered (outfile, "block object ");
00597           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
00598           fprintf_filtered (outfile, ", ");
00599           fputs_filtered (paddress (gdbarch,
00600                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
00601                           outfile);
00602           fprintf_filtered (outfile, "..");
00603           fputs_filtered (paddress (gdbarch,
00604                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
00605                           outfile);
00606           if (section)
00607             fprintf_filtered (outfile, " section %s",
00608                               bfd_section_name (section->the_bfd_section->owner,
00609                                                 section->the_bfd_section));
00610           break;
00611 
00612         case LOC_COMPUTED:
00613           fprintf_filtered (outfile, "computed at runtime");
00614           break;
00615 
00616         case LOC_UNRESOLVED:
00617           fprintf_filtered (outfile, "unresolved");
00618           break;
00619 
00620         case LOC_OPTIMIZED_OUT:
00621           fprintf_filtered (outfile, "optimized out");
00622           break;
00623 
00624         default:
00625           fprintf_filtered (outfile, "botched symbol class %x",
00626                             SYMBOL_CLASS (symbol));
00627           break;
00628         }
00629     }
00630   fprintf_filtered (outfile, "\n");
00631   return 1;
00632 }
00633 
00634 static void
00635 maintenance_print_msymbols (char *args, int from_tty)
00636 {
00637   char **argv;
00638   struct ui_file *outfile;
00639   struct cleanup *cleanups;
00640   char *filename = DEV_TTY;
00641   char *symname = NULL;
00642   struct program_space *pspace;
00643   struct objfile *objfile;
00644 
00645   struct stat sym_st, obj_st;
00646 
00647   dont_repeat ();
00648 
00649   if (args == NULL)
00650     {
00651       error (_("print-msymbols takes an output file "
00652                "name and optional symbol file name"));
00653     }
00654   argv = gdb_buildargv (args);
00655   cleanups = make_cleanup_freeargv (argv);
00656 
00657   if (argv[0] != NULL)
00658     {
00659       filename = argv[0];
00660       /* If a second arg is supplied, it is a source file name to match on.  */
00661       if (argv[1] != NULL)
00662         {
00663           symname = gdb_realpath (argv[1]);
00664           make_cleanup (xfree, symname);
00665           if (symname && stat (symname, &sym_st))
00666             perror_with_name (symname);
00667         }
00668     }
00669 
00670   filename = tilde_expand (filename);
00671   make_cleanup (xfree, filename);
00672 
00673   outfile = gdb_fopen (filename, FOPEN_WT);
00674   if (outfile == 0)
00675     perror_with_name (filename);
00676   make_cleanup_ui_file_delete (outfile);
00677 
00678   ALL_PSPACES (pspace)
00679     ALL_PSPACE_OBJFILES (pspace, objfile)
00680       {
00681         QUIT;
00682         if (symname == NULL || (!stat (objfile_name (objfile), &obj_st)
00683                                 && sym_st.st_dev == obj_st.st_dev
00684                                 && sym_st.st_ino == obj_st.st_ino))
00685           dump_msymbols (objfile, outfile);
00686       }
00687   fprintf_filtered (outfile, "\n\n");
00688   do_cleanups (cleanups);
00689 }
00690 
00691 static void
00692 maintenance_print_objfiles (char *regexp, int from_tty)
00693 {
00694   struct program_space *pspace;
00695   struct objfile *objfile;
00696 
00697   dont_repeat ();
00698 
00699   if (regexp)
00700     re_comp (regexp);
00701 
00702   ALL_PSPACES (pspace)
00703     ALL_PSPACE_OBJFILES (pspace, objfile)
00704       {
00705         QUIT;
00706         if (! regexp
00707             || re_exec (objfile_name (objfile)))
00708           dump_objfile (objfile);
00709       }
00710 }
00711 
00712 /* List all the symbol tables whose names match REGEXP (optional).  */
00713 
00714 static void
00715 maintenance_info_symtabs (char *regexp, int from_tty)
00716 {
00717   struct program_space *pspace;
00718   struct objfile *objfile;
00719 
00720   dont_repeat ();
00721 
00722   if (regexp)
00723     re_comp (regexp);
00724 
00725   ALL_PSPACES (pspace)
00726     ALL_PSPACE_OBJFILES (pspace, objfile)
00727     {
00728       struct symtab *symtab;
00729       
00730       /* We don't want to print anything for this objfile until we
00731          actually find a symtab whose name matches.  */
00732       int printed_objfile_start = 0;
00733 
00734       ALL_OBJFILE_SYMTABS (objfile, symtab)
00735         {
00736           QUIT;
00737 
00738           if (! regexp
00739               || re_exec (symtab_to_filename_for_display (symtab)))
00740             {
00741               if (! printed_objfile_start)
00742                 {
00743                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
00744                   wrap_here ("  ");
00745                   printf_filtered ("((struct objfile *) %s)\n", 
00746                                    host_address_to_string (objfile));
00747                   printed_objfile_start = 1;
00748                 }
00749 
00750               printf_filtered ("        { symtab %s ",
00751                                symtab_to_filename_for_display (symtab));
00752               wrap_here ("    ");
00753               printf_filtered ("((struct symtab *) %s)\n", 
00754                                host_address_to_string (symtab));
00755               printf_filtered ("          dirname %s\n",
00756                                symtab->dirname ? symtab->dirname : "(null)");
00757               printf_filtered ("          fullname %s\n",
00758                                symtab->fullname ? symtab->fullname : "(null)");
00759               printf_filtered ("          "
00760                                "blockvector ((struct blockvector *) %s)%s\n",
00761                                host_address_to_string (symtab->blockvector),
00762                                symtab->primary ? " (primary)" : "");
00763               printf_filtered ("          "
00764                                "linetable ((struct linetable *) %s)\n",
00765                                host_address_to_string (symtab->linetable));
00766               printf_filtered ("          debugformat %s\n",
00767                                symtab->debugformat);
00768               printf_filtered ("        }\n");
00769             }
00770         }
00771 
00772       if (printed_objfile_start)
00773         printf_filtered ("}\n");
00774     }
00775 }
00776 
00777 /* Check consistency of symtabs.
00778    An example of what this checks for is NULL blockvectors.
00779    They can happen if there's a bug during debug info reading.
00780    GDB assumes they are always non-NULL.
00781 
00782    Note: This does not check for psymtab vs symtab consistency.
00783    Use "maint check-psymtabs" for that.  */
00784 
00785 static void
00786 maintenance_check_symtabs (char *ignore, int from_tty)
00787 {
00788   struct program_space *pspace;
00789   struct objfile *objfile;
00790 
00791   ALL_PSPACES (pspace)
00792     ALL_PSPACE_OBJFILES (pspace, objfile)
00793     {
00794       struct symtab *symtab;
00795 
00796       /* We don't want to print anything for this objfile until we
00797          actually find something worth printing.  */
00798       int printed_objfile_start = 0;
00799 
00800       ALL_OBJFILE_SYMTABS (objfile, symtab)
00801         {
00802           int found_something = 0;
00803 
00804           QUIT;
00805 
00806           if (symtab->blockvector == NULL)
00807             found_something = 1;
00808           /* Add more checks here.  */
00809 
00810           if (found_something)
00811             {
00812               if (! printed_objfile_start)
00813                 {
00814                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
00815                   wrap_here ("  ");
00816                   printf_filtered ("((struct objfile *) %s)\n", 
00817                                    host_address_to_string (objfile));
00818                   printed_objfile_start = 1;
00819                 }
00820               printf_filtered ("  { symtab %s\n",
00821                                symtab_to_filename_for_display (symtab));
00822               if (symtab->blockvector == NULL)
00823                 printf_filtered ("    NULL blockvector\n");
00824               printf_filtered ("  }\n");
00825             }
00826         }
00827 
00828       if (printed_objfile_start)
00829         printf_filtered ("}\n");
00830     }
00831 }
00832 
00833 /* Helper function for maintenance_expand_symtabs.
00834    This is the name_matcher function for expand_symtabs_matching.  */
00835 
00836 static int
00837 maintenance_expand_name_matcher (const char *symname, void *data)
00838 {
00839   /* Since we're not searching on symbols, just return TRUE.  */
00840   return 1;
00841 }
00842 
00843 /* Helper function for maintenance_expand_symtabs.
00844    This is the file_matcher function for expand_symtabs_matching.  */
00845 
00846 static int
00847 maintenance_expand_file_matcher (const char *filename, void *data,
00848                                  int basenames)
00849 {
00850   const char *regexp = data;
00851 
00852   QUIT;
00853 
00854   /* KISS: Only apply the regexp to the complete file name.  */
00855   if (basenames)
00856     return 0;
00857 
00858   if (regexp == NULL || re_exec (filename))
00859     return 1;
00860 
00861   return 0;
00862 }
00863 
00864 /* Expand all symbol tables whose name matches an optional regexp.  */
00865 
00866 static void
00867 maintenance_expand_symtabs (char *args, int from_tty)
00868 {
00869   struct program_space *pspace;
00870   struct objfile *objfile;
00871   struct cleanup *cleanups;
00872   char **argv;
00873   char *regexp = NULL;
00874 
00875   /* We use buildargv here so that we handle spaces in the regexp
00876      in a way that allows adding more arguments later.  */
00877   argv = gdb_buildargv (args);
00878   cleanups = make_cleanup_freeargv (argv);
00879 
00880   if (argv != NULL)
00881     {
00882       if (argv[0] != NULL)
00883         {
00884           regexp = argv[0];
00885           if (argv[1] != NULL)
00886             error (_("Extra arguments after regexp."));
00887         }
00888     }
00889 
00890   if (regexp)
00891     re_comp (regexp);
00892 
00893   ALL_PSPACES (pspace)
00894     ALL_PSPACE_OBJFILES (pspace, objfile)
00895     {
00896       if (objfile->sf)
00897         {
00898           objfile->sf->qf->expand_symtabs_matching
00899             (objfile, maintenance_expand_file_matcher,
00900              maintenance_expand_name_matcher, ALL_DOMAIN, regexp);
00901         }
00902     }
00903 
00904   do_cleanups (cleanups);
00905 }
00906 
00907 
00908 /* Return the nexting depth of a block within other blocks in its symtab.  */
00909 
00910 static int
00911 block_depth (struct block *block)
00912 {
00913   int i = 0;
00914 
00915   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
00916     {
00917       i++;
00918     }
00919   return i;
00920 }
00921 
00922 
00923 /* Do early runtime initializations.  */
00924 
00925 void
00926 _initialize_symmisc (void)
00927 {
00928   std_in = stdin;
00929   std_out = stdout;
00930   std_err = stderr;
00931 
00932   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
00933 Print dump of current symbol definitions.\n\
00934 Entries in the full symbol table are dumped to file OUTFILE.\n\
00935 If a SOURCE file is specified, dump only that file's symbols."),
00936            &maintenanceprintlist);
00937 
00938   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
00939 Print dump of current minimal symbol definitions.\n\
00940 Entries in the minimal symbol table are dumped to file OUTFILE.\n\
00941 If a SOURCE file is specified, dump only that file's minimal symbols."),
00942            &maintenanceprintlist);
00943 
00944   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
00945            _("Print dump of current object file definitions.\n\
00946 With an argument REGEXP, list the object files with matching names."),
00947            &maintenanceprintlist);
00948 
00949   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
00950 List the full symbol tables for all object files.\n\
00951 This does not include information about individual symbols, blocks, or\n\
00952 linetables --- just the symbol table structures themselves.\n\
00953 With an argument REGEXP, list the symbol tables with matching names."),
00954            &maintenanceinfolist);
00955 
00956   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
00957            _("\
00958 Check consistency of currently expanded symtabs."),
00959            &maintenancelist);
00960 
00961   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
00962            _("Expand symbol tables.\n\
00963 With an argument REGEXP, only expand the symbol tables with matching names."),
00964            &maintenancelist);
00965 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines