gdb/symmisc.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Do various things to symbol tables (other than lookup), for GDB.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "bfd.h"
  18. #include "filenames.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "breakpoint.h"
  22. #include "command.h"
  23. #include "gdb_obstack.h"
  24. #include "language.h"
  25. #include "bcache.h"
  26. #include "block.h"
  27. #include "gdb_regex.h"
  28. #include <sys/stat.h>
  29. #include "dictionary.h"
  30. #include "typeprint.h"
  31. #include "gdbcmd.h"
  32. #include "source.h"
  33. #include "readline/readline.h"

  34. #include "psymtab.h"

  35. #ifndef DEV_TTY
  36. #define DEV_TTY "/dev/tty"
  37. #endif

  38. /* Unfortunately for debugging, stderr is usually a macro.  This is painful
  39.    when calling functions that take FILE *'s from the debugger.
  40.    So we make a variable which has the same value and which is accessible when
  41.    debugging GDB with itself.  Because stdin et al need not be constants,
  42.    we initialize them in the _initialize_symmisc function at the bottom
  43.    of the file.  */
  44. FILE *std_in;
  45. FILE *std_out;
  46. FILE *std_err;

  47. /* Prototypes for local functions */

  48. static int block_depth (struct block *);

  49. void _initialize_symmisc (void);

  50. struct print_symbol_args
  51.   {
  52.     struct gdbarch *gdbarch;
  53.     struct symbol *symbol;
  54.     int depth;
  55.     struct ui_file *outfile;
  56.   };

  57. static int print_symbol (void *);


  58. void
  59. print_symbol_bcache_statistics (void)
  60. {
  61.   struct program_space *pspace;
  62.   struct objfile *objfile;

  63.   ALL_PSPACES (pspace)
  64.     ALL_PSPACE_OBJFILES (pspace, objfile)
  65.   {
  66.     QUIT;
  67.     printf_filtered (_("Byte cache statistics for '%s':\n"),
  68.                      objfile_name (objfile));
  69.     print_bcache_statistics (psymbol_bcache_get_bcache (objfile->psymbol_cache),
  70.                              "partial symbol cache");
  71.     print_bcache_statistics (objfile->per_bfd->macro_cache,
  72.                              "preprocessor macro cache");
  73.     print_bcache_statistics (objfile->per_bfd->filename_cache,
  74.                              "file name cache");
  75.   }
  76. }

  77. void
  78. print_objfile_statistics (void)
  79. {
  80.   struct program_space *pspace;
  81.   struct objfile *objfile;
  82.   struct compunit_symtab *cu;
  83.   struct symtab *s;
  84.   int i, linetables, blockvectors;

  85.   ALL_PSPACES (pspace)
  86.     ALL_PSPACE_OBJFILES (pspace, objfile)
  87.   {
  88.     QUIT;
  89.     printf_filtered (_("Statistics for '%s':\n"), objfile_name (objfile));
  90.     if (OBJSTAT (objfile, n_stabs) > 0)
  91.       printf_filtered (_("  Number of \"stab\" symbols read: %d\n"),
  92.                        OBJSTAT (objfile, n_stabs));
  93.     if (objfile->per_bfd->n_minsyms > 0)
  94.       printf_filtered (_("  Number of \"minimal\" symbols read: %d\n"),
  95.                        objfile->per_bfd->n_minsyms);
  96.     if (OBJSTAT (objfile, n_psyms) > 0)
  97.       printf_filtered (_("  Number of \"partial\" symbols read: %d\n"),
  98.                        OBJSTAT (objfile, n_psyms));
  99.     if (OBJSTAT (objfile, n_syms) > 0)
  100.       printf_filtered (_("  Number of \"full\" symbols read: %d\n"),
  101.                        OBJSTAT (objfile, n_syms));
  102.     if (OBJSTAT (objfile, n_types) > 0)
  103.       printf_filtered (_("  Number of \"types\" defined: %d\n"),
  104.                        OBJSTAT (objfile, n_types));
  105.     if (objfile->sf)
  106.       objfile->sf->qf->print_stats (objfile);
  107.     i = linetables = blockvectors = 0;
  108.     ALL_OBJFILE_FILETABS (objfile, cu, s)
  109.       {
  110.         i++;
  111.         if (SYMTAB_LINETABLE (s) != NULL)
  112.           linetables++;
  113.       }
  114.     ALL_OBJFILE_COMPUNITS (objfile, cu)
  115.       blockvectors++;
  116.     printf_filtered (_("  Number of symbol tables: %d\n"), i);
  117.     printf_filtered (_("  Number of symbol tables with line tables: %d\n"),
  118.                      linetables);
  119.     printf_filtered (_("  Number of symbol tables with blockvectors: %d\n"),
  120.                      blockvectors);

  121.     if (OBJSTAT (objfile, sz_strtab) > 0)
  122.       printf_filtered (_("  Space used by a.out string tables: %d\n"),
  123.                        OBJSTAT (objfile, sz_strtab));
  124.     printf_filtered (_("  Total memory used for objfile obstack: %s\n"),
  125.                      pulongest (obstack_memory_used (&objfile
  126.                                                      ->objfile_obstack)));
  127.     printf_filtered (_("  Total memory used for BFD obstack: %s\n"),
  128.                      pulongest (obstack_memory_used (&objfile->per_bfd
  129.                                                      ->storage_obstack)));
  130.     printf_filtered (_("  Total memory used for psymbol cache: %d\n"),
  131.                      bcache_memory_used (psymbol_bcache_get_bcache
  132.                                           (objfile->psymbol_cache)));
  133.     printf_filtered (_("  Total memory used for macro cache: %d\n"),
  134.                      bcache_memory_used (objfile->per_bfd->macro_cache));
  135.     printf_filtered (_("  Total memory used for file name cache: %d\n"),
  136.                      bcache_memory_used (objfile->per_bfd->filename_cache));
  137.   }
  138. }

  139. static void
  140. dump_objfile (struct objfile *objfile)
  141. {
  142.   struct compunit_symtab *cust;
  143.   struct symtab *symtab;

  144.   printf_filtered ("\nObject file %s:  ", objfile_name (objfile));
  145.   printf_filtered ("Objfile at ");
  146.   gdb_print_host_address (objfile, gdb_stdout);
  147.   printf_filtered (", bfd at ");
  148.   gdb_print_host_address (objfile->obfd, gdb_stdout);
  149.   printf_filtered (", %d minsyms\n\n",
  150.                    objfile->per_bfd->minimal_symbol_count);

  151.   if (objfile->sf)
  152.     objfile->sf->qf->dump (objfile);

  153.   if (objfile->compunit_symtabs != NULL)
  154.     {
  155.       printf_filtered ("Symtabs:\n");
  156.       ALL_OBJFILE_FILETABS (objfile, cust, symtab)
  157.         {
  158.           printf_filtered ("%s at ", symtab_to_filename_for_display (symtab));
  159.           gdb_print_host_address (symtab, gdb_stdout);
  160.           printf_filtered (", ");
  161.           if (SYMTAB_OBJFILE (symtab) != objfile)
  162.             {
  163.               printf_filtered ("NOT ON CHAIN!  ");
  164.             }
  165.           wrap_here ("  ");
  166.         }
  167.       printf_filtered ("\n\n");
  168.     }
  169. }

  170. /* Print minimal symbols from this objfile.  */

  171. static void
  172. dump_msymbols (struct objfile *objfile, struct ui_file *outfile)
  173. {
  174.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  175.   struct minimal_symbol *msymbol;
  176.   int index;
  177.   char ms_type;

  178.   fprintf_filtered (outfile, "\nObject file %s:\n\n", objfile_name (objfile));
  179.   if (objfile->per_bfd->minimal_symbol_count == 0)
  180.     {
  181.       fprintf_filtered (outfile, "No minimal symbols found.\n");
  182.       return;
  183.     }
  184.   index = 0;
  185.   ALL_OBJFILE_MSYMBOLS (objfile, msymbol)
  186.     {
  187.       struct obj_section *section = MSYMBOL_OBJ_SECTION (objfile, msymbol);

  188.       switch (MSYMBOL_TYPE (msymbol))
  189.         {
  190.         case mst_unknown:
  191.           ms_type = 'u';
  192.           break;
  193.         case mst_text:
  194.           ms_type = 'T';
  195.           break;
  196.         case mst_text_gnu_ifunc:
  197.           ms_type = 'i';
  198.           break;
  199.         case mst_solib_trampoline:
  200.           ms_type = 'S';
  201.           break;
  202.         case mst_data:
  203.           ms_type = 'D';
  204.           break;
  205.         case mst_bss:
  206.           ms_type = 'B';
  207.           break;
  208.         case mst_abs:
  209.           ms_type = 'A';
  210.           break;
  211.         case mst_file_text:
  212.           ms_type = 't';
  213.           break;
  214.         case mst_file_data:
  215.           ms_type = 'd';
  216.           break;
  217.         case mst_file_bss:
  218.           ms_type = 'b';
  219.           break;
  220.         default:
  221.           ms_type = '?';
  222.           break;
  223.         }
  224.       fprintf_filtered (outfile, "[%2d] %c ", index, ms_type);
  225.       fputs_filtered (paddress (gdbarch, MSYMBOL_VALUE_ADDRESS (objfile,
  226.                                                                 msymbol)),
  227.                       outfile);
  228.       fprintf_filtered (outfile, " %s", MSYMBOL_LINKAGE_NAME (msymbol));
  229.       if (section)
  230.         {
  231.           if (section->the_bfd_section != NULL)
  232.             fprintf_filtered (outfile, " section %s",
  233.                               bfd_section_name (objfile->obfd,
  234.                                                 section->the_bfd_section));
  235.           else
  236.             fprintf_filtered (outfile, " spurious section %ld",
  237.                               (long) (section - objfile->sections));
  238.         }
  239.       if (MSYMBOL_DEMANGLED_NAME (msymbol) != NULL)
  240.         {
  241.           fprintf_filtered (outfile, %s", MSYMBOL_DEMANGLED_NAME (msymbol));
  242.         }
  243.       if (msymbol->filename)
  244.         fprintf_filtered (outfile, %s", msymbol->filename);
  245.       fputs_filtered ("\n", outfile);
  246.       index++;
  247.     }
  248.   if (objfile->per_bfd->minimal_symbol_count != index)
  249.     {
  250.       warning (_("internal error:  minimal symbol count %d != %d"),
  251.                objfile->per_bfd->minimal_symbol_count, index);
  252.     }
  253.   fprintf_filtered (outfile, "\n");
  254. }

  255. static void
  256. dump_symtab_1 (struct objfile *objfile, struct symtab *symtab,
  257.                struct ui_file *outfile)
  258. {
  259.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  260.   int i;
  261.   struct dict_iterator iter;
  262.   int len;
  263.   struct linetable *l;
  264.   const struct blockvector *bv;
  265.   struct symbol *sym;
  266.   struct block *b;
  267.   int depth;

  268.   fprintf_filtered (outfile, "\nSymtab for file %s\n",
  269.                     symtab_to_filename_for_display (symtab));
  270.   if (SYMTAB_DIRNAME (symtab) != NULL)
  271.     fprintf_filtered (outfile, "Compilation directory is %s\n",
  272.                       SYMTAB_DIRNAME (symtab));
  273.   fprintf_filtered (outfile, "Read from object file %s (",
  274.                     objfile_name (objfile));
  275.   gdb_print_host_address (objfile, outfile);
  276.   fprintf_filtered (outfile, ")\n");
  277.   fprintf_filtered (outfile, "Language: %s\n",
  278.                     language_str (symtab->language));

  279.   /* First print the line table.  */
  280.   l = SYMTAB_LINETABLE (symtab);
  281.   if (l)
  282.     {
  283.       fprintf_filtered (outfile, "\nLine table:\n\n");
  284.       len = l->nitems;
  285.       for (i = 0; i < len; i++)
  286.         {
  287.           fprintf_filtered (outfile, " line %d at ", l->item[i].line);
  288.           fputs_filtered (paddress (gdbarch, l->item[i].pc), outfile);
  289.           fprintf_filtered (outfile, "\n");
  290.         }
  291.     }
  292.   /* Now print the block info, but only for compunit symtabs since we will
  293.      print lots of duplicate info otherwise.  */
  294.   if (symtab == COMPUNIT_FILETABS (SYMTAB_COMPUNIT (symtab)))
  295.     {
  296.       fprintf_filtered (outfile, "\nBlockvector:\n\n");
  297.       bv = SYMTAB_BLOCKVECTOR (symtab);
  298.       len = BLOCKVECTOR_NBLOCKS (bv);
  299.       for (i = 0; i < len; i++)
  300.         {
  301.           b = BLOCKVECTOR_BLOCK (bv, i);
  302.           depth = block_depth (b) * 2;
  303.           print_spaces (depth, outfile);
  304.           fprintf_filtered (outfile, "block #%03d, object at ", i);
  305.           gdb_print_host_address (b, outfile);
  306.           if (BLOCK_SUPERBLOCK (b))
  307.             {
  308.               fprintf_filtered (outfile, " under ");
  309.               gdb_print_host_address (BLOCK_SUPERBLOCK (b), outfile);
  310.             }
  311.           /* drow/2002-07-10: We could save the total symbols count
  312.              even if we're using a hashtable, but nothing else but this message
  313.              wants it.  */
  314.           fprintf_filtered (outfile, ", %d syms/buckets in ",
  315.                             dict_size (BLOCK_DICT (b)));
  316.           fputs_filtered (paddress (gdbarch, BLOCK_START (b)), outfile);
  317.           fprintf_filtered (outfile, "..");
  318.           fputs_filtered (paddress (gdbarch, BLOCK_END (b)), outfile);
  319.           if (BLOCK_FUNCTION (b))
  320.             {
  321.               fprintf_filtered (outfile, ", function %s",
  322.                                 SYMBOL_LINKAGE_NAME (BLOCK_FUNCTION (b)));
  323.               if (SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)) != NULL)
  324.                 {
  325.                   fprintf_filtered (outfile, ", %s",
  326.                                 SYMBOL_DEMANGLED_NAME (BLOCK_FUNCTION (b)));
  327.                 }
  328.             }
  329.           fprintf_filtered (outfile, "\n");
  330.           /* Now print each symbol in this block (in no particular order, if
  331.              we're using a hashtable).  Note that we only want this
  332.              block, not any blocks from included symtabs.  */
  333.           ALL_DICT_SYMBOLS (BLOCK_DICT (b), iter, sym)
  334.             {
  335.               struct print_symbol_args s;

  336.               s.gdbarch = gdbarch;
  337.               s.symbol = sym;
  338.               s.depth = depth + 1;
  339.               s.outfile = outfile;
  340.               catch_errors (print_symbol, &s, "Error printing symbol:\n",
  341.                             RETURN_MASK_ERROR);
  342.             }
  343.         }
  344.       fprintf_filtered (outfile, "\n");
  345.     }
  346.   else
  347.     {
  348.       fprintf_filtered (outfile, "\nBlockvector same as previous symtab\n\n");
  349.     }
  350. }

  351. static void
  352. dump_symtab (struct objfile *objfile, struct symtab *symtab,
  353.              struct ui_file *outfile)
  354. {
  355.   /* Set the current language to the language of the symtab we're dumping
  356.      because certain routines used during dump_symtab() use the current
  357.      language to print an image of the symbol.  We'll restore it later.
  358.      But use only real languages, not placeholders.  */
  359.   if (symtab->language != language_unknown
  360.       && symtab->language != language_auto)
  361.     {
  362.       enum language saved_lang;

  363.       saved_lang = set_language (symtab->language);

  364.       dump_symtab_1 (objfile, symtab, outfile);

  365.       set_language (saved_lang);
  366.     }
  367.   else
  368.     dump_symtab_1 (objfile, symtab, outfile);
  369. }

  370. static void
  371. maintenance_print_symbols (char *args, int from_tty)
  372. {
  373.   char **argv;
  374.   struct ui_file *outfile;
  375.   struct cleanup *cleanups;
  376.   char *symname = NULL;
  377.   char *filename = DEV_TTY;
  378.   struct objfile *objfile;
  379.   struct compunit_symtab *cu;
  380.   struct symtab *s;

  381.   dont_repeat ();

  382.   if (args == NULL)
  383.     {
  384.       error (_("Arguments missing: an output file name "
  385.                "and an optional symbol file name"));
  386.     }
  387.   argv = gdb_buildargv (args);
  388.   cleanups = make_cleanup_freeargv (argv);

  389.   if (argv[0] != NULL)
  390.     {
  391.       filename = argv[0];
  392.       /* If a second arg is supplied, it is a source file name to match on.  */
  393.       if (argv[1] != NULL)
  394.         {
  395.           symname = argv[1];
  396.         }
  397.     }

  398.   filename = tilde_expand (filename);
  399.   make_cleanup (xfree, filename);

  400.   outfile = gdb_fopen (filename, FOPEN_WT);
  401.   if (outfile == 0)
  402.     perror_with_name (filename);
  403.   make_cleanup_ui_file_delete (outfile);

  404.   ALL_FILETABS (objfile, cu, s)
  405.     {
  406.       QUIT;
  407.       if (symname == NULL
  408.           || filename_cmp (symname, symtab_to_filename_for_display (s)) == 0)
  409.         dump_symtab (objfile, s, outfile);
  410.     }
  411.   do_cleanups (cleanups);
  412. }

  413. /* Print symbol ARGS->SYMBOL on ARGS->OUTFILE.  ARGS->DEPTH says how
  414.    far to indent.  ARGS is really a struct print_symbol_args *, but is
  415.    declared as char * to get it past catch_errors.  Returns 0 for error,
  416.    1 for success.  */

  417. static int
  418. print_symbol (void *args)
  419. {
  420.   struct gdbarch *gdbarch = ((struct print_symbol_args *) args)->gdbarch;
  421.   struct symbol *symbol = ((struct print_symbol_args *) args)->symbol;
  422.   int depth = ((struct print_symbol_args *) args)->depth;
  423.   struct ui_file *outfile = ((struct print_symbol_args *) args)->outfile;
  424.   struct obj_section *section;

  425.   if (SYMBOL_OBJFILE_OWNED (symbol))
  426.     section = SYMBOL_OBJ_SECTION (symbol_objfile (symbol), symbol);
  427.   else
  428.     section = NULL;

  429.   print_spaces (depth, outfile);
  430.   if (SYMBOL_DOMAIN (symbol) == LABEL_DOMAIN)
  431.     {
  432.       fprintf_filtered (outfile, "label %s at ", SYMBOL_PRINT_NAME (symbol));
  433.       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
  434.                       outfile);
  435.       if (section)
  436.         fprintf_filtered (outfile, " section %s\n",
  437.                           bfd_section_name (section->the_bfd_section->owner,
  438.                                             section->the_bfd_section));
  439.       else
  440.         fprintf_filtered (outfile, "\n");
  441.       return 1;
  442.     }
  443.   if (SYMBOL_DOMAIN (symbol) == STRUCT_DOMAIN)
  444.     {
  445.       if (TYPE_TAG_NAME (SYMBOL_TYPE (symbol)))
  446.         {
  447.           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
  448.                          &type_print_raw_options);
  449.         }
  450.       else
  451.         {
  452.           fprintf_filtered (outfile, "%s %s = ",
  453.                          (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_ENUM
  454.                           ? "enum"
  455.                      : (TYPE_CODE (SYMBOL_TYPE (symbol)) == TYPE_CODE_STRUCT
  456.                         ? "struct" : "union")),
  457.                             SYMBOL_LINKAGE_NAME (symbol));
  458.           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), "", outfile, 1, depth,
  459.                          &type_print_raw_options);
  460.         }
  461.       fprintf_filtered (outfile, ";\n");
  462.     }
  463.   else
  464.     {
  465.       if (SYMBOL_CLASS (symbol) == LOC_TYPEDEF)
  466.         fprintf_filtered (outfile, "typedef ");
  467.       if (SYMBOL_TYPE (symbol))
  468.         {
  469.           /* Print details of types, except for enums where it's clutter.  */
  470.           LA_PRINT_TYPE (SYMBOL_TYPE (symbol), SYMBOL_PRINT_NAME (symbol),
  471.                          outfile,
  472.                          TYPE_CODE (SYMBOL_TYPE (symbol)) != TYPE_CODE_ENUM,
  473.                          depth,
  474.                          &type_print_raw_options);
  475.           fprintf_filtered (outfile, "; ");
  476.         }
  477.       else
  478.         fprintf_filtered (outfile, "%s ", SYMBOL_PRINT_NAME (symbol));

  479.       switch (SYMBOL_CLASS (symbol))
  480.         {
  481.         case LOC_CONST:
  482.           fprintf_filtered (outfile, "const %s (%s)",
  483.                             plongest (SYMBOL_VALUE (symbol)),
  484.                             hex_string (SYMBOL_VALUE (symbol)));
  485.           break;

  486.         case LOC_CONST_BYTES:
  487.           {
  488.             unsigned i;
  489.             struct type *type = check_typedef (SYMBOL_TYPE (symbol));

  490.             fprintf_filtered (outfile, "const %u hex bytes:",
  491.                               TYPE_LENGTH (type));
  492.             for (i = 0; i < TYPE_LENGTH (type); i++)
  493.               fprintf_filtered (outfile, " %02x",
  494.                                 (unsigned) SYMBOL_VALUE_BYTES (symbol)[i]);
  495.           }
  496.           break;

  497.         case LOC_STATIC:
  498.           fprintf_filtered (outfile, "static at ");
  499.           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
  500.                           outfile);
  501.           if (section)
  502.             fprintf_filtered (outfile, " section %s",
  503.                               bfd_section_name (section->the_bfd_section->owner,
  504.                                                 section->the_bfd_section));
  505.           break;

  506.         case LOC_REGISTER:
  507.           if (SYMBOL_IS_ARGUMENT (symbol))
  508.             fprintf_filtered (outfile, "parameter register %s",
  509.                               plongest (SYMBOL_VALUE (symbol)));
  510.           else
  511.             fprintf_filtered (outfile, "register %s",
  512.                               plongest (SYMBOL_VALUE (symbol)));
  513.           break;

  514.         case LOC_ARG:
  515.           fprintf_filtered (outfile, "arg at offset %s",
  516.                             hex_string (SYMBOL_VALUE (symbol)));
  517.           break;

  518.         case LOC_REF_ARG:
  519.           fprintf_filtered (outfile, "reference arg at %s",
  520.                             hex_string (SYMBOL_VALUE (symbol)));
  521.           break;

  522.         case LOC_REGPARM_ADDR:
  523.           fprintf_filtered (outfile, "address parameter register %s",
  524.                             plongest (SYMBOL_VALUE (symbol)));
  525.           break;

  526.         case LOC_LOCAL:
  527.           fprintf_filtered (outfile, "local at offset %s",
  528.                             hex_string (SYMBOL_VALUE (symbol)));
  529.           break;

  530.         case LOC_TYPEDEF:
  531.           break;

  532.         case LOC_LABEL:
  533.           fprintf_filtered (outfile, "label at ");
  534.           fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (symbol)),
  535.                           outfile);
  536.           if (section)
  537.             fprintf_filtered (outfile, " section %s",
  538.                               bfd_section_name (section->the_bfd_section->owner,
  539.                                                 section->the_bfd_section));
  540.           break;

  541.         case LOC_BLOCK:
  542.           fprintf_filtered (outfile, "block object ");
  543.           gdb_print_host_address (SYMBOL_BLOCK_VALUE (symbol), outfile);
  544.           fprintf_filtered (outfile, ", ");
  545.           fputs_filtered (paddress (gdbarch,
  546.                                     BLOCK_START (SYMBOL_BLOCK_VALUE (symbol))),
  547.                           outfile);
  548.           fprintf_filtered (outfile, "..");
  549.           fputs_filtered (paddress (gdbarch,
  550.                                     BLOCK_END (SYMBOL_BLOCK_VALUE (symbol))),
  551.                           outfile);
  552.           if (section)
  553.             fprintf_filtered (outfile, " section %s",
  554.                               bfd_section_name (section->the_bfd_section->owner,
  555.                                                 section->the_bfd_section));
  556.           break;

  557.         case LOC_COMPUTED:
  558.           fprintf_filtered (outfile, "computed at runtime");
  559.           break;

  560.         case LOC_UNRESOLVED:
  561.           fprintf_filtered (outfile, "unresolved");
  562.           break;

  563.         case LOC_OPTIMIZED_OUT:
  564.           fprintf_filtered (outfile, "optimized out");
  565.           break;

  566.         default:
  567.           fprintf_filtered (outfile, "botched symbol class %x",
  568.                             SYMBOL_CLASS (symbol));
  569.           break;
  570.         }
  571.     }
  572.   fprintf_filtered (outfile, "\n");
  573.   return 1;
  574. }

  575. static void
  576. maintenance_print_msymbols (char *args, int from_tty)
  577. {
  578.   char **argv;
  579.   struct ui_file *outfile;
  580.   struct cleanup *cleanups;
  581.   char *filename = DEV_TTY;
  582.   char *symname = NULL;
  583.   struct program_space *pspace;
  584.   struct objfile *objfile;

  585.   struct stat sym_st, obj_st;

  586.   dont_repeat ();

  587.   if (args == NULL)
  588.     {
  589.       error (_("print-msymbols takes an output file "
  590.                "name and optional symbol file name"));
  591.     }
  592.   argv = gdb_buildargv (args);
  593.   cleanups = make_cleanup_freeargv (argv);

  594.   if (argv[0] != NULL)
  595.     {
  596.       filename = argv[0];
  597.       /* If a second arg is supplied, it is a source file name to match on.  */
  598.       if (argv[1] != NULL)
  599.         {
  600.           symname = gdb_realpath (argv[1]);
  601.           make_cleanup (xfree, symname);
  602.           if (symname && stat (symname, &sym_st))
  603.             perror_with_name (symname);
  604.         }
  605.     }

  606.   filename = tilde_expand (filename);
  607.   make_cleanup (xfree, filename);

  608.   outfile = gdb_fopen (filename, FOPEN_WT);
  609.   if (outfile == 0)
  610.     perror_with_name (filename);
  611.   make_cleanup_ui_file_delete (outfile);

  612.   ALL_PSPACES (pspace)
  613.     ALL_PSPACE_OBJFILES (pspace, objfile)
  614.       {
  615.         QUIT;
  616.         if (symname == NULL || (!stat (objfile_name (objfile), &obj_st)
  617.                                 && sym_st.st_dev == obj_st.st_dev
  618.                                 && sym_st.st_ino == obj_st.st_ino))
  619.           dump_msymbols (objfile, outfile);
  620.       }
  621.   fprintf_filtered (outfile, "\n\n");
  622.   do_cleanups (cleanups);
  623. }

  624. static void
  625. maintenance_print_objfiles (char *regexp, int from_tty)
  626. {
  627.   struct program_space *pspace;
  628.   struct objfile *objfile;

  629.   dont_repeat ();

  630.   if (regexp)
  631.     re_comp (regexp);

  632.   ALL_PSPACES (pspace)
  633.     ALL_PSPACE_OBJFILES (pspace, objfile)
  634.       {
  635.         QUIT;
  636.         if (! regexp
  637.             || re_exec (objfile_name (objfile)))
  638.           dump_objfile (objfile);
  639.       }
  640. }

  641. /* List all the symbol tables whose names match REGEXP (optional).  */

  642. static void
  643. maintenance_info_symtabs (char *regexp, int from_tty)
  644. {
  645.   struct program_space *pspace;
  646.   struct objfile *objfile;

  647.   dont_repeat ();

  648.   if (regexp)
  649.     re_comp (regexp);

  650.   ALL_PSPACES (pspace)
  651.     ALL_PSPACE_OBJFILES (pspace, objfile)
  652.     {
  653.       struct compunit_symtab *cust;
  654.       struct symtab *symtab;

  655.       /* We don't want to print anything for this objfile until we
  656.          actually find a symtab whose name matches.  */
  657.       int printed_objfile_start = 0;

  658.       ALL_OBJFILE_COMPUNITS (objfile, cust)
  659.         {
  660.           int printed_compunit_symtab_start = 0;

  661.           ALL_COMPUNIT_FILETABS (cust, symtab)
  662.             {
  663.               QUIT;

  664.               if (! regexp
  665.                   || re_exec (symtab_to_filename_for_display (symtab)))
  666.                 {
  667.                   if (! printed_objfile_start)
  668.                     {
  669.                       printf_filtered ("{ objfile %s ", objfile_name (objfile));
  670.                       wrap_here ("  ");
  671.                       printf_filtered ("((struct objfile *) %s)\n",
  672.                                        host_address_to_string (objfile));
  673.                       printed_objfile_start = 1;
  674.                     }
  675.                   if (! printed_compunit_symtab_start)
  676.                     {
  677.                       printf_filtered ("  { ((struct compunit_symtab *) %s)\n",
  678.                                        host_address_to_string (cust));
  679.                       printf_filtered ("    debugformat %s\n",
  680.                                        COMPUNIT_DEBUGFORMAT (cust));
  681.                       printf_filtered ("    producer %s\n",
  682.                                        COMPUNIT_PRODUCER (cust) != NULL
  683.                                        ? COMPUNIT_PRODUCER (cust)
  684.                                        : "(null)");
  685.                       printf_filtered ("    dirname %s\n",
  686.                                        COMPUNIT_DIRNAME (cust) != NULL
  687.                                        ? COMPUNIT_DIRNAME (cust)
  688.                                        : "(null)");
  689.                       printf_filtered ("    blockvector"
  690.                                        " ((struct blockvector *) %s)\n",
  691.                                        host_address_to_string
  692.                                          (COMPUNIT_BLOCKVECTOR (cust)));
  693.                       printed_compunit_symtab_start = 1;
  694.                     }

  695.                   printf_filtered ("\t{ symtab %s ",
  696.                                    symtab_to_filename_for_display (symtab));
  697.                   wrap_here ("    ");
  698.                   printf_filtered ("((struct symtab *) %s)\n",
  699.                                    host_address_to_string (symtab));
  700.                   printf_filtered ("\t  fullname %s\n",
  701.                                    symtab->fullname != NULL
  702.                                    ? symtab->fullname
  703.                                    : "(null)");
  704.                   printf_filtered ("\t  "
  705.                                    "linetable ((struct linetable *) %s)\n",
  706.                                    host_address_to_string (symtab->linetable));
  707.                   printf_filtered ("\t}\n");
  708.                 }
  709.             }

  710.           if (printed_compunit_symtab_start)
  711.             printf_filtered ("  }\n");
  712.         }

  713.       if (printed_objfile_start)
  714.         printf_filtered ("}\n");
  715.     }
  716. }

  717. /* Check consistency of symtabs.
  718.    An example of what this checks for is NULL blockvectors.
  719.    They can happen if there's a bug during debug info reading.
  720.    GDB assumes they are always non-NULL.

  721.    Note: This does not check for psymtab vs symtab consistency.
  722.    Use "maint check-psymtabs" for that.  */

  723. static void
  724. maintenance_check_symtabs (char *ignore, int from_tty)
  725. {
  726.   struct program_space *pspace;
  727.   struct objfile *objfile;

  728.   ALL_PSPACES (pspace)
  729.     ALL_PSPACE_OBJFILES (pspace, objfile)
  730.     {
  731.       struct compunit_symtab *cust;

  732.       /* We don't want to print anything for this objfile until we
  733.          actually find something worth printing.  */
  734.       int printed_objfile_start = 0;

  735.       ALL_OBJFILE_COMPUNITS (objfile, cust)
  736.         {
  737.           int found_something = 0;
  738.           struct symtab *symtab = compunit_primary_filetab (cust);

  739.           QUIT;

  740.           if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
  741.             found_something = 1;
  742.           /* Add more checks here.  */

  743.           if (found_something)
  744.             {
  745.               if (! printed_objfile_start)
  746.                 {
  747.                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
  748.                   wrap_here ("  ");
  749.                   printf_filtered ("((struct objfile *) %s)\n",
  750.                                    host_address_to_string (objfile));
  751.                   printed_objfile_start = 1;
  752.                 }
  753.               printf_filtered ("  { symtab %s\n",
  754.                                symtab_to_filename_for_display (symtab));
  755.               if (COMPUNIT_BLOCKVECTOR (cust) == NULL)
  756.                 printf_filtered ("    NULL blockvector\n");
  757.               printf_filtered ("  }\n");
  758.             }
  759.         }

  760.       if (printed_objfile_start)
  761.         printf_filtered ("}\n");
  762.     }
  763. }

  764. /* Helper function for maintenance_expand_symtabs.
  765.    This is the name_matcher function for expand_symtabs_matching.  */

  766. static int
  767. maintenance_expand_name_matcher (const char *symname, void *data)
  768. {
  769.   /* Since we're not searching on symbols, just return TRUE.  */
  770.   return 1;
  771. }

  772. /* Helper function for maintenance_expand_symtabs.
  773.    This is the file_matcher function for expand_symtabs_matching.  */

  774. static int
  775. maintenance_expand_file_matcher (const char *filename, void *data,
  776.                                  int basenames)
  777. {
  778.   const char *regexp = data;

  779.   QUIT;

  780.   /* KISS: Only apply the regexp to the complete file name.  */
  781.   if (basenames)
  782.     return 0;

  783.   if (regexp == NULL || re_exec (filename))
  784.     return 1;

  785.   return 0;
  786. }

  787. /* Expand all symbol tables whose name matches an optional regexp.  */

  788. static void
  789. maintenance_expand_symtabs (char *args, int from_tty)
  790. {
  791.   struct program_space *pspace;
  792.   struct objfile *objfile;
  793.   struct cleanup *cleanups;
  794.   char **argv;
  795.   char *regexp = NULL;

  796.   /* We use buildargv here so that we handle spaces in the regexp
  797.      in a way that allows adding more arguments later.  */
  798.   argv = gdb_buildargv (args);
  799.   cleanups = make_cleanup_freeargv (argv);

  800.   if (argv != NULL)
  801.     {
  802.       if (argv[0] != NULL)
  803.         {
  804.           regexp = argv[0];
  805.           if (argv[1] != NULL)
  806.             error (_("Extra arguments after regexp."));
  807.         }
  808.     }

  809.   if (regexp)
  810.     re_comp (regexp);

  811.   ALL_PSPACES (pspace)
  812.     ALL_PSPACE_OBJFILES (pspace, objfile)
  813.     {
  814.       if (objfile->sf)
  815.         {
  816.           objfile->sf->qf->expand_symtabs_matching
  817.             (objfile, maintenance_expand_file_matcher,
  818.              maintenance_expand_name_matcher, ALL_DOMAIN, regexp);
  819.         }
  820.     }

  821.   do_cleanups (cleanups);
  822. }


  823. /* Return the nexting depth of a block within other blocks in its symtab.  */

  824. static int
  825. block_depth (struct block *block)
  826. {
  827.   int i = 0;

  828.   while ((block = BLOCK_SUPERBLOCK (block)) != NULL)
  829.     {
  830.       i++;
  831.     }
  832.   return i;
  833. }


  834. /* Do early runtime initializations.  */

  835. void
  836. _initialize_symmisc (void)
  837. {
  838.   std_in = stdin;
  839.   std_out = stdout;
  840.   std_err = stderr;

  841.   add_cmd ("symbols", class_maintenance, maintenance_print_symbols, _("\
  842. Print dump of current symbol definitions.\n\
  843. Entries in the full symbol table are dumped to file OUTFILE.\n\
  844. If a SOURCE file is specified, dump only that file's symbols."),
  845.            &maintenanceprintlist);

  846.   add_cmd ("msymbols", class_maintenance, maintenance_print_msymbols, _("\
  847. Print dump of current minimal symbol definitions.\n\
  848. Entries in the minimal symbol table are dumped to file OUTFILE.\n\
  849. If a SOURCE file is specified, dump only that file's minimal symbols."),
  850.            &maintenanceprintlist);

  851.   add_cmd ("objfiles", class_maintenance, maintenance_print_objfiles,
  852.            _("Print dump of current object file definitions.\n\
  853. With an argument REGEXP, list the object files with matching names."),
  854.            &maintenanceprintlist);

  855.   add_cmd ("symtabs", class_maintenance, maintenance_info_symtabs, _("\
  856. List the full symbol tables for all object files.\n\
  857. This does not include information about individual symbols, blocks, or\n\
  858. linetables --- just the symbol table structures themselves.\n\
  859. With an argument REGEXP, list the symbol tables with matching names."),
  860.            &maintenanceinfolist);

  861.   add_cmd ("check-symtabs", class_maintenance, maintenance_check_symtabs,
  862.            _("\
  863. Check consistency of currently expanded symtabs."),
  864.            &maintenancelist);

  865.   add_cmd ("expand-symtabs", class_maintenance, maintenance_expand_symtabs,
  866.            _("Expand symbol tables.\n\
  867. With an argument REGEXP, only expand the symbol tables with matching names."),
  868.            &maintenancelist);
  869. }