gdb/psymtab.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Partial symbol tables.

  2.    Copyright (C) 2009-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 "psympriv.h"
  17. #include "objfiles.h"
  18. #include "block.h"
  19. #include "filenames.h"
  20. #include "source.h"
  21. #include "addrmap.h"
  22. #include "gdbtypes.h"
  23. #include "bcache.h"
  24. #include "ui-out.h"
  25. #include "command.h"
  26. #include "readline/readline.h"
  27. #include "gdb_regex.h"
  28. #include "dictionary.h"
  29. #include "language.h"
  30. #include "cp-support.h"
  31. #include "gdbcmd.h"

  32. #ifndef DEV_TTY
  33. #define DEV_TTY "/dev/tty"
  34. #endif

  35. struct psymbol_bcache
  36. {
  37.   struct bcache *bcache;
  38. };

  39. static struct partial_symbol *match_partial_symbol (struct objfile *,
  40.                                                     struct partial_symtab *,
  41.                                                     int,
  42.                                                     const char *, domain_enum,
  43.                                                     symbol_compare_ftype *,
  44.                                                     symbol_compare_ftype *);

  45. static struct partial_symbol *lookup_partial_symbol (struct objfile *,
  46.                                                      struct partial_symtab *,
  47.                                                      const char *, int,
  48.                                                      domain_enum);

  49. static const char *psymtab_to_fullname (struct partial_symtab *ps);

  50. static struct partial_symbol *find_pc_sect_psymbol (struct objfile *,
  51.                                                     struct partial_symtab *,
  52.                                                     CORE_ADDR,
  53.                                                     struct obj_section *);

  54. static void fixup_psymbol_section (struct partial_symbol *psym,
  55.                                    struct objfile *objfile);

  56. static struct compunit_symtab *psymtab_to_symtab (struct objfile *objfile,
  57.                                                   struct partial_symtab *pst);

  58. /* Ensure that the partial symbols for OBJFILE have been loaded.  This
  59.    function always returns its argument, as a convenience.  */

  60. struct objfile *
  61. require_partial_symbols (struct objfile *objfile, int verbose)
  62. {
  63.   if ((objfile->flags & OBJF_PSYMTABS_READ) == 0)
  64.     {
  65.       objfile->flags |= OBJF_PSYMTABS_READ;

  66.       if (objfile->sf->sym_read_psymbols)
  67.         {
  68.           if (verbose)
  69.             {
  70.               printf_unfiltered (_("Reading symbols from %s..."),
  71.                                  objfile_name (objfile));
  72.               gdb_flush (gdb_stdout);
  73.             }
  74.           (*objfile->sf->sym_read_psymbols) (objfile);
  75.           if (verbose)
  76.             {
  77.               if (!objfile_has_symbols (objfile))
  78.                 {
  79.                   wrap_here ("");
  80.                   printf_unfiltered (_("(no debugging symbols found)..."));
  81.                   wrap_here ("");
  82.                 }

  83.               printf_unfiltered (_("done.\n"));
  84.             }
  85.         }
  86.     }

  87.   return objfile;
  88. }

  89. /* Traverse all psymtabs in one objfile, requiring that the psymtabs
  90.    be read in.  */

  91. #define ALL_OBJFILE_PSYMTABS_REQUIRED(objfile, p)                \
  92.     for ((p) = require_partial_symbols (objfile, 1)->psymtabs;        \
  93.          (p) != NULL;                                                \
  94.          (p) = (p)->next)

  95. /* We want to make sure this file always requires psymtabs.  */

  96. #undef ALL_OBJFILE_PSYMTABS

  97. /* Traverse all psymtabs in all objfiles.  */

  98. #define ALL_PSYMTABS(objfile, p) \
  99.   ALL_OBJFILES (objfile)         \
  100.     ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)

  101. /* Helper function for psym_map_symtabs_matching_filename that
  102.    expands the symtabs and calls the iterator.  */

  103. static int
  104. partial_map_expand_apply (struct objfile *objfile,
  105.                           const char *name,
  106.                           const char *real_path,
  107.                           struct partial_symtab *pst,
  108.                           int (*callback) (struct symtab *, void *),
  109.                           void *data)
  110. {
  111.   struct compunit_symtab *last_made = objfile->compunit_symtabs;

  112.   /* Shared psymtabs should never be seen here.  Instead they should
  113.      be handled properly by the caller.  */
  114.   gdb_assert (pst->user == NULL);

  115.   /* Don't visit already-expanded psymtabs.  */
  116.   if (pst->readin)
  117.     return 0;

  118.   /* This may expand more than one symtab, and we want to iterate over
  119.      all of them.  */
  120.   psymtab_to_symtab (objfile, pst);

  121.   return iterate_over_some_symtabs (name, real_path, callback, data,
  122.                                     objfile->compunit_symtabs, last_made);
  123. }

  124. /*  Psymtab version of map_symtabs_matching_filename.  See its definition in
  125.     the definition of quick_symbol_functions in symfile.h.  */

  126. static int
  127. psym_map_symtabs_matching_filename (struct objfile *objfile,
  128.                                     const char *name,
  129.                                     const char *real_path,
  130.                                     int (*callback) (struct symtab *,
  131.                                                      void *),
  132.                                     void *data)
  133. {
  134.   struct partial_symtab *pst;
  135.   const char *name_basename = lbasename (name);

  136.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
  137.   {
  138.     /* We can skip shared psymtabs here, because any file name will be
  139.        attached to the unshared psymtab.  */
  140.     if (pst->user != NULL)
  141.       continue;

  142.     /* Anonymous psymtabs don't have a file name.  */
  143.     if (pst->anonymous)
  144.       continue;

  145.     if (compare_filenames_for_search (pst->filename, name))
  146.       {
  147.         if (partial_map_expand_apply (objfile, name, real_path,
  148.                                       pst, callback, data))
  149.           return 1;
  150.         continue;
  151.       }

  152.     /* Before we invoke realpath, which can get expensive when many
  153.        files are involved, do a quick comparison of the basenames.  */
  154.     if (! basenames_may_differ
  155.         && FILENAME_CMP (name_basename, lbasename (pst->filename)) != 0)
  156.       continue;

  157.     if (compare_filenames_for_search (psymtab_to_fullname (pst), name))
  158.       {
  159.         if (partial_map_expand_apply (objfile, name, real_path,
  160.                                       pst, callback, data))
  161.           return 1;
  162.         continue;
  163.       }

  164.     /* If the user gave us an absolute path, try to find the file in
  165.        this symtab and use its absolute path.  */
  166.     if (real_path != NULL)
  167.       {
  168.         gdb_assert (IS_ABSOLUTE_PATH (real_path));
  169.         gdb_assert (IS_ABSOLUTE_PATH (name));
  170.         if (filename_cmp (psymtab_to_fullname (pst), real_path) == 0)
  171.           {
  172.             if (partial_map_expand_apply (objfile, name, real_path,
  173.                                           pst, callback, data))
  174.               return 1;
  175.             continue;
  176.           }
  177.       }
  178.   }

  179.   return 0;
  180. }

  181. /* Find which partial symtab contains PC and SECTION starting at psymtab PST.
  182.    We may find a different psymtab than PST.  See FIND_PC_SECT_PSYMTAB.  */

  183. static struct partial_symtab *
  184. find_pc_sect_psymtab_closer (struct objfile *objfile,
  185.                              CORE_ADDR pc, struct obj_section *section,
  186.                              struct partial_symtab *pst,
  187.                              struct bound_minimal_symbol msymbol)
  188. {
  189.   struct partial_symtab *tpst;
  190.   struct partial_symtab *best_pst = pst;
  191.   CORE_ADDR best_addr = pst->textlow;

  192.   gdb_assert (!pst->psymtabs_addrmap_supported);

  193.   /* An objfile that has its functions reordered might have
  194.      many partial symbol tables containing the PC, but
  195.      we want the partial symbol table that contains the
  196.      function containing the PC.  */
  197.   if (!(objfile->flags & OBJF_REORDERED) &&
  198.       section == 0)        /* Can't validate section this way.  */
  199.     return pst;

  200.   if (msymbol.minsym == NULL)
  201.     return (pst);

  202.   /* The code range of partial symtabs sometimes overlap, so, in
  203.      the loop below, we need to check all partial symtabs and
  204.      find the one that fits better for the given PC address.  We
  205.      select the partial symtab that contains a symbol whose
  206.      address is closest to the PC address.  By closest we mean
  207.      that find_pc_sect_symbol returns the symbol with address
  208.      that is closest and still less than the given PC.  */
  209.   for (tpst = pst; tpst != NULL; tpst = tpst->next)
  210.     {
  211.       if (pc >= tpst->textlow && pc < tpst->texthigh)
  212.         {
  213.           struct partial_symbol *p;
  214.           CORE_ADDR this_addr;

  215.           /* NOTE: This assumes that every psymbol has a
  216.              corresponding msymbol, which is not necessarily
  217.              true; the debug info might be much richer than the
  218.              object's symbol table.  */
  219.           p = find_pc_sect_psymbol (objfile, tpst, pc, section);
  220.           if (p != NULL
  221.               && SYMBOL_VALUE_ADDRESS (p)
  222.               == BMSYMBOL_VALUE_ADDRESS (msymbol))
  223.             return tpst;

  224.           /* Also accept the textlow value of a psymtab as a
  225.              "symbol", to provide some support for partial
  226.              symbol tables with line information but no debug
  227.              symbols (e.g. those produced by an assembler).  */
  228.           if (p != NULL)
  229.             this_addr = SYMBOL_VALUE_ADDRESS (p);
  230.           else
  231.             this_addr = tpst->textlow;

  232.           /* Check whether it is closer than our current
  233.              BEST_ADDR.  Since this symbol address is
  234.              necessarily lower or equal to PC, the symbol closer
  235.              to PC is the symbol which address is the highest.
  236.              This way we return the psymtab which contains such
  237.              best match symbol.  This can help in cases where the
  238.              symbol information/debuginfo is not complete, like
  239.              for instance on IRIX6 with gcc, where no debug info
  240.              is emitted for statics.  (See also the nodebug.exp
  241.              testcase.)  */
  242.           if (this_addr > best_addr)
  243.             {
  244.               best_addr = this_addr;
  245.               best_pst = tpst;
  246.             }
  247.         }
  248.     }
  249.   return best_pst;
  250. }

  251. /* Find which partial symtab contains PC and SECTION.  Return 0 if
  252.    none.  We return the psymtab that contains a symbol whose address
  253.    exactly matches PC, or, if we cannot find an exact match, the
  254.    psymtab that contains a symbol whose address is closest to PC.  */
  255. static struct partial_symtab *
  256. find_pc_sect_psymtab (struct objfile *objfile, CORE_ADDR pc,
  257.                       struct obj_section *section,
  258.                       struct bound_minimal_symbol msymbol)
  259. {
  260.   struct partial_symtab *pst;

  261.   /* Try just the PSYMTABS_ADDRMAP mapping first as it has better granularity
  262.      than the later used TEXTLOW/TEXTHIGH one.  */

  263.   if (objfile->psymtabs_addrmap != NULL)
  264.     {
  265.       pst = addrmap_find (objfile->psymtabs_addrmap, pc);
  266.       if (pst != NULL)
  267.         {
  268.           /* FIXME: addrmaps currently do not handle overlayed sections,
  269.              so fall back to the non-addrmap case if we're debugging
  270.              overlays and the addrmap returned the wrong section.  */
  271.           if (overlay_debugging && msymbol.minsym && section)
  272.             {
  273.               struct partial_symbol *p;

  274.               /* NOTE: This assumes that every psymbol has a
  275.                  corresponding msymbol, which is not necessarily
  276.                  true; the debug info might be much richer than the
  277.                  object's symbol table.  */
  278.               p = find_pc_sect_psymbol (objfile, pst, pc, section);
  279.               if (!p
  280.                   || SYMBOL_VALUE_ADDRESS (p)
  281.                   != BMSYMBOL_VALUE_ADDRESS (msymbol))
  282.                 goto next;
  283.             }

  284.           /* We do not try to call FIND_PC_SECT_PSYMTAB_CLOSER as
  285.              PSYMTABS_ADDRMAP we used has already the best 1-byte
  286.              granularity and FIND_PC_SECT_PSYMTAB_CLOSER may mislead us into
  287.              a worse chosen section due to the TEXTLOW/TEXTHIGH ranges
  288.              overlap.  */

  289.           return pst;
  290.         }
  291.     }

  292. next:

  293.   /* Existing PSYMTABS_ADDRMAP mapping is present even for PARTIAL_SYMTABs
  294.      which still have no corresponding full SYMTABs read.  But it is not
  295.      present for non-DWARF2 debug infos not supporting PSYMTABS_ADDRMAP in GDB
  296.      so far.  */

  297.   /* Check even OBJFILE with non-zero PSYMTABS_ADDRMAP as only several of
  298.      its CUs may be missing in PSYMTABS_ADDRMAP as they may be varying
  299.      debug info type in single OBJFILE.  */

  300.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
  301.     if (!pst->psymtabs_addrmap_supported
  302.         && pc >= pst->textlow && pc < pst->texthigh)
  303.       {
  304.         struct partial_symtab *best_pst;

  305.         best_pst = find_pc_sect_psymtab_closer (objfile, pc, section, pst,
  306.                                                 msymbol);
  307.         if (best_pst != NULL)
  308.           return best_pst;
  309.       }

  310.   return NULL;
  311. }

  312. /*  Psymtab version of find_pc_sect_compunit_symtab.  See its definition in
  313.     the definition of quick_symbol_functions in symfile.h.  */

  314. static struct compunit_symtab *
  315. psym_find_pc_sect_compunit_symtab (struct objfile *objfile,
  316.                                    struct bound_minimal_symbol msymbol,
  317.                                    CORE_ADDR pc,
  318.                                    struct obj_section *section,
  319.                                    int warn_if_readin)
  320. {
  321.   struct partial_symtab *ps = find_pc_sect_psymtab (objfile, pc, section,
  322.                                                     msymbol);
  323.   if (ps)
  324.     {
  325.       if (warn_if_readin && ps->readin)
  326.         /* Might want to error() here (in case symtab is corrupt and
  327.            will cause a core dump), but maybe we can successfully
  328.            continue, so let's not.  */
  329.         warning (_("\
  330. (Internal error: pc %s in read in psymtab, but not in symtab.)\n"),
  331.                  paddress (get_objfile_arch (objfile), pc));
  332.       psymtab_to_symtab (objfile, ps);
  333.       return ps->compunit_symtab;
  334.     }
  335.   return NULL;
  336. }

  337. /* Find which partial symbol within a psymtab matches PC and SECTION.
  338.    Return 0 if none.  */

  339. static struct partial_symbol *
  340. find_pc_sect_psymbol (struct objfile *objfile,
  341.                       struct partial_symtab *psymtab, CORE_ADDR pc,
  342.                       struct obj_section *section)
  343. {
  344.   struct partial_symbol *best = NULL, *p, **pp;
  345.   CORE_ADDR best_pc;

  346.   gdb_assert (psymtab != NULL);

  347.   /* Cope with programs that start at address 0.  */
  348.   best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;

  349.   /* Search the global symbols as well as the static symbols, so that
  350.      find_pc_partial_function doesn't use a minimal symbol and thus
  351.      cache a bad endaddr.  */
  352.   for (pp = objfile->global_psymbols.list + psymtab->globals_offset;
  353.     (pp - (objfile->global_psymbols.list + psymtab->globals_offset)
  354.      < psymtab->n_global_syms);
  355.        pp++)
  356.     {
  357.       p = *pp;
  358.       if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
  359.           && PSYMBOL_CLASS (p) == LOC_BLOCK
  360.           && pc >= SYMBOL_VALUE_ADDRESS (p)
  361.           && (SYMBOL_VALUE_ADDRESS (p) > best_pc
  362.               || (psymtab->textlow == 0
  363.                   && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
  364.         {
  365.           if (section)                /* Match on a specific section.  */
  366.             {
  367.               fixup_psymbol_section (p, objfile);
  368.               if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
  369.                                           section))
  370.                 continue;
  371.             }
  372.           best_pc = SYMBOL_VALUE_ADDRESS (p);
  373.           best = p;
  374.         }
  375.     }

  376.   for (pp = objfile->static_psymbols.list + psymtab->statics_offset;
  377.     (pp - (objfile->static_psymbols.list + psymtab->statics_offset)
  378.      < psymtab->n_static_syms);
  379.        pp++)
  380.     {
  381.       p = *pp;
  382.       if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
  383.           && PSYMBOL_CLASS (p) == LOC_BLOCK
  384.           && pc >= SYMBOL_VALUE_ADDRESS (p)
  385.           && (SYMBOL_VALUE_ADDRESS (p) > best_pc
  386.               || (psymtab->textlow == 0
  387.                   && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
  388.         {
  389.           if (section)                /* Match on a specific section.  */
  390.             {
  391.               fixup_psymbol_section (p, objfile);
  392.               if (!matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, p),
  393.                                           section))
  394.                 continue;
  395.             }
  396.           best_pc = SYMBOL_VALUE_ADDRESS (p);
  397.           best = p;
  398.         }
  399.     }

  400.   return best;
  401. }

  402. static void
  403. fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
  404. {
  405.   CORE_ADDR addr;

  406.   if (!psym)
  407.     return;

  408.   if (SYMBOL_SECTION (psym) >= 0)
  409.     return;

  410.   gdb_assert (objfile);

  411.   switch (PSYMBOL_CLASS (psym))
  412.     {
  413.     case LOC_STATIC:
  414.     case LOC_LABEL:
  415.     case LOC_BLOCK:
  416.       addr = SYMBOL_VALUE_ADDRESS (psym);
  417.       break;
  418.     default:
  419.       /* Nothing else will be listed in the minsyms -- no use looking
  420.          it up.  */
  421.       return;
  422.     }

  423.   fixup_section (&psym->ginfo, addr, objfile);
  424. }

  425. /*  Psymtab version of lookup_symbol.  See its definition in
  426.     the definition of quick_symbol_functions in symfile.h.  */

  427. static struct compunit_symtab *
  428. psym_lookup_symbol (struct objfile *objfile,
  429.                     int block_index, const char *name,
  430.                     const domain_enum domain)
  431. {
  432.   struct partial_symtab *ps;
  433.   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
  434.   struct compunit_symtab *stab_best = NULL;

  435.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  436.   {
  437.     if (!ps->readin && lookup_partial_symbol (objfile, ps, name,
  438.                                               psymtab_index, domain))
  439.       {
  440.         struct symbol *sym = NULL;
  441.         struct compunit_symtab *stab = psymtab_to_symtab (objfile, ps);
  442.         /* Note: While psymtab_to_symtab can return NULL if the partial symtab
  443.            is empty, we can assume it won't here because lookup_partial_symbol
  444.            succeeded.  */
  445.         const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
  446.         struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);

  447.         /* Some caution must be observed with overloaded functions
  448.            and methods, since the psymtab will not contain any overload
  449.            information (but NAME might contain it).  */
  450.         sym = block_lookup_symbol (block, name, domain);

  451.         if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
  452.           {
  453.             if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
  454.               return stab;

  455.             stab_best = stab;
  456.           }

  457.         /* Keep looking through other psymtabs.  */
  458.       }
  459.   }

  460.   return stab_best;
  461. }

  462. /* Look in PST for a symbol in DOMAIN whose name matches NAME.  Search
  463.    the global block of PST if GLOBAL, and otherwise the static block.
  464.    MATCH is the comparison operation that returns true iff MATCH (s,
  465.    NAME), where s is a SYMBOL_SEARCH_NAME.  If ORDERED_COMPARE is
  466.    non-null, the symbols in the block are assumed to be ordered
  467.    according to it (allowing binary search).  It must be compatible
  468.    with MATCH.  Returns the symbol, if found, and otherwise NULL.  */

  469. static struct partial_symbol *
  470. match_partial_symbol (struct objfile *objfile,
  471.                       struct partial_symtab *pst, int global,
  472.                       const char *name, domain_enum domain,
  473.                       symbol_compare_ftype *match,
  474.                       symbol_compare_ftype *ordered_compare)
  475. {
  476.   struct partial_symbol **start, **psym;
  477.   struct partial_symbol **top, **real_top, **bottom, **center;
  478.   int length = (global ? pst->n_global_syms : pst->n_static_syms);
  479.   int do_linear_search = 1;

  480.   if (length == 0)
  481.       return NULL;
  482.   start = (global ?
  483.            objfile->global_psymbols.list + pst->globals_offset :
  484.            objfile->static_psymbols.list + pst->statics_offset);

  485.   if (global && ordered_compare)  /* Can use a binary search.  */
  486.     {
  487.       do_linear_search = 0;

  488.       /* Binary search.  This search is guaranteed to end with center
  489.          pointing at the earliest partial symbol whose name might be
  490.          correct.  At that point *all* partial symbols with an
  491.          appropriate name will be checked against the correct
  492.          domain.  */

  493.       bottom = start;
  494.       top = start + length - 1;
  495.       real_top = top;
  496.       while (top > bottom)
  497.         {
  498.           center = bottom + (top - bottom) / 2;
  499.           gdb_assert (center < top);
  500.           if (!do_linear_search
  501.               && (SYMBOL_LANGUAGE (*center) == language_java))
  502.             do_linear_search = 1;
  503.           if (ordered_compare (SYMBOL_SEARCH_NAME (*center), name) >= 0)
  504.             top = center;
  505.           else
  506.             bottom = center + 1;
  507.         }
  508.       gdb_assert (top == bottom);

  509.       while (top <= real_top
  510.              && match (SYMBOL_SEARCH_NAME (*top), name) == 0)
  511.         {
  512.           if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
  513.                                      SYMBOL_DOMAIN (*top), domain))
  514.             return *top;
  515.           top++;
  516.         }
  517.     }

  518.   /* Can't use a binary search or else we found during the binary search that
  519.      we should also do a linear search.  */

  520.   if (do_linear_search)
  521.     {
  522.       for (psym = start; psym < start + length; psym++)
  523.         {
  524.           if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
  525.                                      SYMBOL_DOMAIN (*psym), domain)
  526.               && match (SYMBOL_SEARCH_NAME (*psym), name) == 0)
  527.             return *psym;
  528.         }
  529.     }

  530.   return NULL;
  531. }

  532. /* Returns the name used to search psymtabs.  Unlike symtabs, psymtabs do
  533.    not contain any method/function instance information (since this would
  534.    force reading type information while reading psymtabs).  Therefore,
  535.    if NAME contains overload information, it must be stripped before searching
  536.    psymtabs.

  537.    The caller is responsible for freeing the return result.  */

  538. static char *
  539. psymtab_search_name (const char *name)
  540. {
  541.   switch (current_language->la_language)
  542.     {
  543.     case language_cplus:
  544.     case language_java:
  545.       {
  546.         if (strchr (name, '('))
  547.           {
  548.             char *ret = cp_remove_params (name);

  549.             if (ret)
  550.               return ret;
  551.           }
  552.       }
  553.       break;

  554.     default:
  555.       break;
  556.     }

  557.   return xstrdup (name);
  558. }

  559. /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
  560.    Check the global symbols if GLOBAL, the static symbols if not.  */

  561. static struct partial_symbol *
  562. lookup_partial_symbol (struct objfile *objfile,
  563.                        struct partial_symtab *pst, const char *name,
  564.                        int global, domain_enum domain)
  565. {
  566.   struct partial_symbol **start, **psym;
  567.   struct partial_symbol **top, **real_top, **bottom, **center;
  568.   int length = (global ? pst->n_global_syms : pst->n_static_syms);
  569.   int do_linear_search = 1;
  570.   char *search_name;
  571.   struct cleanup *cleanup;

  572.   if (length == 0)
  573.     {
  574.       return (NULL);
  575.     }

  576.   search_name = psymtab_search_name (name);
  577.   cleanup = make_cleanup (xfree, search_name);
  578.   start = (global ?
  579.            objfile->global_psymbols.list + pst->globals_offset :
  580.            objfile->static_psymbols.list + pst->statics_offset);

  581.   if (global)                        /* This means we can use a binary search.  */
  582.     {
  583.       do_linear_search = 0;

  584.       /* Binary search.  This search is guaranteed to end with center
  585.          pointing at the earliest partial symbol whose name might be
  586.          correct.  At that point *all* partial symbols with an
  587.          appropriate name will be checked against the correct
  588.          domain.  */

  589.       bottom = start;
  590.       top = start + length - 1;
  591.       real_top = top;
  592.       while (top > bottom)
  593.         {
  594.           center = bottom + (top - bottom) / 2;
  595.           if (!(center < top))
  596.             internal_error (__FILE__, __LINE__,
  597.                             _("failed internal consistency check"));
  598.           if (!do_linear_search
  599.               && SYMBOL_LANGUAGE (*center) == language_java)
  600.             {
  601.               do_linear_search = 1;
  602.             }
  603.           if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center),
  604.                                  search_name) >= 0)
  605.             {
  606.               top = center;
  607.             }
  608.           else
  609.             {
  610.               bottom = center + 1;
  611.             }
  612.         }
  613.       if (!(top == bottom))
  614.         internal_error (__FILE__, __LINE__,
  615.                         _("failed internal consistency check"));

  616.       /* For `case_sensitivity == case_sensitive_off' strcmp_iw_ordered will
  617.          search more exactly than what matches SYMBOL_MATCHES_SEARCH_NAME.  */
  618.       while (top >= start && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
  619.         top--;

  620.       /* Fixup to have a symbol which matches SYMBOL_MATCHES_SEARCH_NAME.  */
  621.       top++;

  622.       while (top <= real_top && SYMBOL_MATCHES_SEARCH_NAME (*top, search_name))
  623.         {
  624.           if (symbol_matches_domain (SYMBOL_LANGUAGE (*top),
  625.                                      SYMBOL_DOMAIN (*top), domain))
  626.             {
  627.               do_cleanups (cleanup);
  628.               return (*top);
  629.             }
  630.           top++;
  631.         }
  632.     }

  633.   /* Can't use a binary search or else we found during the binary search that
  634.      we should also do a linear search.  */

  635.   if (do_linear_search)
  636.     {
  637.       for (psym = start; psym < start + length; psym++)
  638.         {
  639.           if (symbol_matches_domain (SYMBOL_LANGUAGE (*psym),
  640.                                      SYMBOL_DOMAIN (*psym), domain)
  641.               && SYMBOL_MATCHES_SEARCH_NAME (*psym, search_name))
  642.             {
  643.               do_cleanups (cleanup);
  644.               return (*psym);
  645.             }
  646.         }
  647.     }

  648.   do_cleanups (cleanup);
  649.   return (NULL);
  650. }

  651. /* Get the symbol table that corresponds to a partial_symtab.
  652.    This is fast after the first time you do it.
  653.    The result will be NULL if the primary symtab has no symbols,
  654.    which can happen.  Otherwise the result is the primary symtab
  655.    that contains PST.  */

  656. static struct compunit_symtab *
  657. psymtab_to_symtab (struct objfile *objfile, struct partial_symtab *pst)
  658. {
  659.   /* If it is a shared psymtab, find an unshared psymtab that includes
  660.      it.  Any such psymtab will do.  */
  661.   while (pst->user != NULL)
  662.     pst = pst->user;

  663.   /* If it's been looked up before, return it.  */
  664.   if (pst->compunit_symtab)
  665.     return pst->compunit_symtab;

  666.   /* If it has not yet been read in, read it.  */
  667.   if (!pst->readin)
  668.     {
  669.       struct cleanup *back_to = increment_reading_symtab ();

  670.       (*pst->read_symtab) (pst, objfile);
  671.       do_cleanups (back_to);
  672.     }

  673.   return pst->compunit_symtab;
  674. }

  675. /*  Psymtab version of relocate.  See its definition in
  676.     the definition of quick_symbol_functions in symfile.h.  */

  677. static void
  678. psym_relocate (struct objfile *objfile,
  679.                const struct section_offsets *new_offsets,
  680.                const struct section_offsets *delta)
  681. {
  682.   struct partial_symbol **psym;
  683.   struct partial_symtab *p;

  684.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
  685.     {
  686.       p->textlow += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
  687.       p->texthigh += ANOFFSET (delta, SECT_OFF_TEXT (objfile));
  688.     }

  689.   for (psym = objfile->global_psymbols.list;
  690.        psym < objfile->global_psymbols.next;
  691.        psym++)
  692.     {
  693.       fixup_psymbol_section (*psym, objfile);
  694.       if (SYMBOL_SECTION (*psym) >= 0)
  695.         SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
  696.                                                   SYMBOL_SECTION (*psym));
  697.     }
  698.   for (psym = objfile->static_psymbols.list;
  699.        psym < objfile->static_psymbols.next;
  700.        psym++)
  701.     {
  702.       fixup_psymbol_section (*psym, objfile);
  703.       if (SYMBOL_SECTION (*psym) >= 0)
  704.         SYMBOL_VALUE_ADDRESS (*psym) += ANOFFSET (delta,
  705.                                                   SYMBOL_SECTION (*psym));
  706.     }
  707. }

  708. /*  Psymtab version of find_last_source_symtab.  See its definition in
  709.     the definition of quick_symbol_functions in symfile.h.  */

  710. static struct symtab *
  711. psym_find_last_source_symtab (struct objfile *ofp)
  712. {
  713.   struct partial_symtab *ps;
  714.   struct partial_symtab *cs_pst = 0;

  715.   ALL_OBJFILE_PSYMTABS_REQUIRED (ofp, ps)
  716.     {
  717.       const char *name = ps->filename;
  718.       int len = strlen (name);

  719.       if (!(len > 2 && (strcmp (&name[len - 2], ".h") == 0
  720.                         || strcmp (name, "<<C++-namespaces>>") == 0)))
  721.         cs_pst = ps;
  722.     }

  723.   if (cs_pst)
  724.     {
  725.       if (cs_pst->readin)
  726.         {
  727.           internal_error (__FILE__, __LINE__,
  728.                           _("select_source_symtab: "
  729.                           "readin pst found and no symtabs."));
  730.         }
  731.       else
  732.         {
  733.           struct compunit_symtab *cust = psymtab_to_symtab (ofp, cs_pst);

  734.           if (cust == NULL)
  735.             return NULL;
  736.           return compunit_primary_filetab (cust);
  737.         }
  738.     }
  739.   return NULL;
  740. }

  741. /*  Psymtab version of forget_cached_source_info.  See its definition in
  742.     the definition of quick_symbol_functions in symfile.h.  */

  743. static void
  744. psym_forget_cached_source_info (struct objfile *objfile)
  745. {
  746.   struct partial_symtab *pst;

  747.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, pst)
  748.     {
  749.       if (pst->fullname != NULL)
  750.         {
  751.           xfree (pst->fullname);
  752.           pst->fullname = NULL;
  753.         }
  754.     }
  755. }

  756. static void
  757. print_partial_symbols (struct gdbarch *gdbarch,
  758.                        struct partial_symbol **p, int count, char *what,
  759.                        struct ui_file *outfile)
  760. {
  761.   fprintf_filtered (outfile, %s partial symbols:\n", what);
  762.   while (count-- > 0)
  763.     {
  764.       QUIT;
  765.       fprintf_filtered (outfile, "    `%s'", SYMBOL_LINKAGE_NAME (*p));
  766.       if (SYMBOL_DEMANGLED_NAME (*p) != NULL)
  767.         {
  768.           fprintf_filtered (outfile, "  `%s'", SYMBOL_DEMANGLED_NAME (*p));
  769.         }
  770.       fputs_filtered (", ", outfile);
  771.       switch (SYMBOL_DOMAIN (*p))
  772.         {
  773.         case UNDEF_DOMAIN:
  774.           fputs_filtered ("undefined domain, ", outfile);
  775.           break;
  776.         case VAR_DOMAIN:
  777.           /* This is the usual thing -- don't print it.  */
  778.           break;
  779.         case STRUCT_DOMAIN:
  780.           fputs_filtered ("struct domain, ", outfile);
  781.           break;
  782.         case LABEL_DOMAIN:
  783.           fputs_filtered ("label domain, ", outfile);
  784.           break;
  785.         default:
  786.           fputs_filtered ("<invalid domain>, ", outfile);
  787.           break;
  788.         }
  789.       switch (PSYMBOL_CLASS (*p))
  790.         {
  791.         case LOC_UNDEF:
  792.           fputs_filtered ("undefined", outfile);
  793.           break;
  794.         case LOC_CONST:
  795.           fputs_filtered ("constant int", outfile);
  796.           break;
  797.         case LOC_STATIC:
  798.           fputs_filtered ("static", outfile);
  799.           break;
  800.         case LOC_REGISTER:
  801.           fputs_filtered ("register", outfile);
  802.           break;
  803.         case LOC_ARG:
  804.           fputs_filtered ("pass by value", outfile);
  805.           break;
  806.         case LOC_REF_ARG:
  807.           fputs_filtered ("pass by reference", outfile);
  808.           break;
  809.         case LOC_REGPARM_ADDR:
  810.           fputs_filtered ("register address parameter", outfile);
  811.           break;
  812.         case LOC_LOCAL:
  813.           fputs_filtered ("stack parameter", outfile);
  814.           break;
  815.         case LOC_TYPEDEF:
  816.           fputs_filtered ("type", outfile);
  817.           break;
  818.         case LOC_LABEL:
  819.           fputs_filtered ("label", outfile);
  820.           break;
  821.         case LOC_BLOCK:
  822.           fputs_filtered ("function", outfile);
  823.           break;
  824.         case LOC_CONST_BYTES:
  825.           fputs_filtered ("constant bytes", outfile);
  826.           break;
  827.         case LOC_UNRESOLVED:
  828.           fputs_filtered ("unresolved", outfile);
  829.           break;
  830.         case LOC_OPTIMIZED_OUT:
  831.           fputs_filtered ("optimized out", outfile);
  832.           break;
  833.         case LOC_COMPUTED:
  834.           fputs_filtered ("computed at runtime", outfile);
  835.           break;
  836.         default:
  837.           fputs_filtered ("<invalid location>", outfile);
  838.           break;
  839.         }
  840.       fputs_filtered (", ", outfile);
  841.       fputs_filtered (paddress (gdbarch, SYMBOL_VALUE_ADDRESS (*p)), outfile);
  842.       fprintf_filtered (outfile, "\n");
  843.       p++;
  844.     }
  845. }

  846. static void
  847. dump_psymtab (struct objfile *objfile, struct partial_symtab *psymtab,
  848.               struct ui_file *outfile)
  849. {
  850.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  851.   int i;

  852.   if (psymtab->anonymous)
  853.     {
  854.       fprintf_filtered (outfile, "\nAnonymous partial symtab (%s) ",
  855.                         psymtab->filename);
  856.     }
  857.   else
  858.     {
  859.       fprintf_filtered (outfile, "\nPartial symtab for source file %s ",
  860.                         psymtab->filename);
  861.     }
  862.   fprintf_filtered (outfile, "(object ");
  863.   gdb_print_host_address (psymtab, outfile);
  864.   fprintf_filtered (outfile, ")\n\n");
  865.   fprintf_unfiltered (outfile, "  Read from object file %s (",
  866.                       objfile_name (objfile));
  867.   gdb_print_host_address (objfile, outfile);
  868.   fprintf_unfiltered (outfile, ")\n");

  869.   if (psymtab->readin)
  870.     {
  871.       fprintf_filtered (outfile,
  872.                         "  Full symtab was read (at ");
  873.       gdb_print_host_address (psymtab->compunit_symtab, outfile);
  874.       fprintf_filtered (outfile, " by function at ");
  875.       gdb_print_host_address (psymtab->read_symtab, outfile);
  876.       fprintf_filtered (outfile, ")\n");
  877.     }

  878.   fprintf_filtered (outfile, "  Relocate symbols by ");
  879.   for (i = 0; i < objfile->num_sections; ++i)
  880.     {
  881.       if (i != 0)
  882.         fprintf_filtered (outfile, ", ");
  883.       wrap_here ("    ");
  884.       fputs_filtered (paddress (gdbarch,
  885.                                 ANOFFSET (psymtab->section_offsets, i)),
  886.                       outfile);
  887.     }
  888.   fprintf_filtered (outfile, "\n");

  889.   fprintf_filtered (outfile, "  Symbols cover text addresses ");
  890.   fputs_filtered (paddress (gdbarch, psymtab->textlow), outfile);
  891.   fprintf_filtered (outfile, "-");
  892.   fputs_filtered (paddress (gdbarch, psymtab->texthigh), outfile);
  893.   fprintf_filtered (outfile, "\n");
  894.   fprintf_filtered (outfile, "  Address map supported - %s.\n",
  895.                     psymtab->psymtabs_addrmap_supported ? "yes" : "no");
  896.   fprintf_filtered (outfile, "  Depends on %d other partial symtabs.\n",
  897.                     psymtab->number_of_dependencies);
  898.   for (i = 0; i < psymtab->number_of_dependencies; i++)
  899.     {
  900.       fprintf_filtered (outfile, "    %d ", i);
  901.       gdb_print_host_address (psymtab->dependencies[i], outfile);
  902.       fprintf_filtered (outfile, " %s\n",
  903.                         psymtab->dependencies[i]->filename);
  904.     }
  905.   if (psymtab->user != NULL)
  906.     {
  907.       fprintf_filtered (outfile, "  Shared partial symtab with user ");
  908.       gdb_print_host_address (psymtab->user, outfile);
  909.       fprintf_filtered (outfile, "\n");
  910.     }
  911.   if (psymtab->n_global_syms > 0)
  912.     {
  913.       print_partial_symbols (gdbarch,
  914.                              objfile->global_psymbols.list
  915.                              + psymtab->globals_offset,
  916.                              psymtab->n_global_syms, "Global", outfile);
  917.     }
  918.   if (psymtab->n_static_syms > 0)
  919.     {
  920.       print_partial_symbols (gdbarch,
  921.                              objfile->static_psymbols.list
  922.                              + psymtab->statics_offset,
  923.                              psymtab->n_static_syms, "Static", outfile);
  924.     }
  925.   fprintf_filtered (outfile, "\n");
  926. }

  927. /*  Psymtab version of print_stats.  See its definition in
  928.     the definition of quick_symbol_functions in symfile.h.  */

  929. static void
  930. psym_print_stats (struct objfile *objfile)
  931. {
  932.   int i;
  933.   struct partial_symtab *ps;

  934.   i = 0;
  935.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  936.     {
  937.       if (ps->readin == 0)
  938.         i++;
  939.     }
  940.   printf_filtered (_("  Number of psym tables (not yet expanded): %d\n"), i);
  941. }

  942. /*  Psymtab version of dump.  See its definition in
  943.     the definition of quick_symbol_functions in symfile.h.  */

  944. static void
  945. psym_dump (struct objfile *objfile)
  946. {
  947.   struct partial_symtab *psymtab;

  948.   if (objfile->psymtabs)
  949.     {
  950.       printf_filtered ("Psymtabs:\n");
  951.       for (psymtab = objfile->psymtabs;
  952.            psymtab != NULL;
  953.            psymtab = psymtab->next)
  954.         {
  955.           printf_filtered ("%s at ",
  956.                            psymtab->filename);
  957.           gdb_print_host_address (psymtab, gdb_stdout);
  958.           printf_filtered (", ");
  959.           wrap_here ("  ");
  960.         }
  961.       printf_filtered ("\n\n");
  962.     }
  963. }

  964. /*  Psymtab version of expand_symtabs_for_function.  See its definition in
  965.     the definition of quick_symbol_functions in symfile.h.  */

  966. static void
  967. psym_expand_symtabs_for_function (struct objfile *objfile,
  968.                                   const char *func_name)
  969. {
  970.   struct partial_symtab *ps;

  971.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  972.   {
  973.     if (ps->readin)
  974.       continue;

  975.     if ((lookup_partial_symbol (objfile, ps, func_name, 1, VAR_DOMAIN)
  976.          != NULL)
  977.         || (lookup_partial_symbol (objfile, ps, func_name, 0, VAR_DOMAIN)
  978.             != NULL))
  979.       psymtab_to_symtab (objfile, ps);
  980.   }
  981. }

  982. /*  Psymtab version of expand_all_symtabs.  See its definition in
  983.     the definition of quick_symbol_functions in symfile.h.  */

  984. static void
  985. psym_expand_all_symtabs (struct objfile *objfile)
  986. {
  987.   struct partial_symtab *psymtab;

  988.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
  989.     {
  990.       psymtab_to_symtab (objfile, psymtab);
  991.     }
  992. }

  993. /*  Psymtab version of expand_symtabs_with_fullname.  See its definition in
  994.     the definition of quick_symbol_functions in symfile.h.  */

  995. static void
  996. psym_expand_symtabs_with_fullname (struct objfile *objfile,
  997.                                    const char *fullname)
  998. {
  999.   struct partial_symtab *p;

  1000.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, p)
  1001.     {
  1002.       /* Anonymous psymtabs don't have a name of a source file.  */
  1003.       if (p->anonymous)
  1004.         continue;

  1005.       /* psymtab_to_fullname tries to open the file which is slow.
  1006.          Don't call it if we know the basenames don't match.  */
  1007.       if ((basenames_may_differ
  1008.            || filename_cmp (lbasename (fullname), lbasename (p->filename)) == 0)
  1009.           && filename_cmp (fullname, psymtab_to_fullname (p)) == 0)
  1010.         psymtab_to_symtab (objfile, p);
  1011.     }
  1012. }

  1013. /*  Psymtab version of map_symbol_filenames.  See its definition in
  1014.     the definition of quick_symbol_functions in symfile.h.  */

  1015. static void
  1016. psym_map_symbol_filenames (struct objfile *objfile,
  1017.                            symbol_filename_ftype *fun, void *data,
  1018.                            int need_fullname)
  1019. {
  1020.   struct partial_symtab *ps;

  1021.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  1022.     {
  1023.       const char *fullname;

  1024.       if (ps->readin)
  1025.         continue;

  1026.       /* We can skip shared psymtabs here, because any file name will be
  1027.          attached to the unshared psymtab.  */
  1028.       if (ps->user != NULL)
  1029.         continue;

  1030.       /* Anonymous psymtabs don't have a file name.  */
  1031.       if (ps->anonymous)
  1032.         continue;

  1033.       QUIT;
  1034.       if (need_fullname)
  1035.         fullname = psymtab_to_fullname (ps);
  1036.       else
  1037.         fullname = NULL;
  1038.       (*fun) (ps->filename, fullname, data);
  1039.     }
  1040. }

  1041. /* Finds the fullname that a partial_symtab represents.

  1042.    If this functions finds the fullname, it will save it in ps->fullname
  1043.    and it will also return the value.

  1044.    If this function fails to find the file that this partial_symtab represents,
  1045.    NULL will be returned and ps->fullname will be set to NULL.  */

  1046. static const char *
  1047. psymtab_to_fullname (struct partial_symtab *ps)
  1048. {
  1049.   gdb_assert (!ps->anonymous);

  1050.   /* Use cached copy if we have it.
  1051.      We rely on forget_cached_source_info being called appropriately
  1052.      to handle cases like the file being moved.  */
  1053.   if (ps->fullname == NULL)
  1054.     {
  1055.       int fd = find_and_open_source (ps->filename, ps->dirname, &ps->fullname);

  1056.       if (fd >= 0)
  1057.         close (fd);
  1058.       else
  1059.         {
  1060.           char *fullname;
  1061.           struct cleanup *back_to;

  1062.           /* rewrite_source_path would be applied by find_and_open_source, we
  1063.              should report the pathname where GDB tried to find the file.  */

  1064.           if (ps->dirname == NULL || IS_ABSOLUTE_PATH (ps->filename))
  1065.             fullname = xstrdup (ps->filename);
  1066.           else
  1067.             fullname = concat (ps->dirname, SLASH_STRING, ps->filename, NULL);

  1068.           back_to = make_cleanup (xfree, fullname);
  1069.           ps->fullname = rewrite_source_path (fullname);
  1070.           if (ps->fullname == NULL)
  1071.             ps->fullname = xstrdup (fullname);
  1072.           do_cleanups (back_to);
  1073.         }
  1074.     }

  1075.   return ps->fullname;
  1076. }

  1077. /*  For all symbols, s, in BLOCK that are in NAMESPACE and match NAME
  1078.     according to the function MATCH, call CALLBACK(BLOCK, s, DATA).
  1079.     BLOCK is assumed to come from OBJFILE.  Returns 1 iff CALLBACK
  1080.     ever returns non-zero, and otherwise returns 0.  */

  1081. static int
  1082. map_block (const char *name, domain_enum namespace, struct objfile *objfile,
  1083.            struct block *block,
  1084.            int (*callback) (struct block *, struct symbol *, void *),
  1085.            void *data, symbol_compare_ftype *match)
  1086. {
  1087.   struct block_iterator iter;
  1088.   struct symbol *sym;

  1089.   for (sym = block_iter_match_first (block, name, match, &iter);
  1090.        sym != NULL; sym = block_iter_match_next (name, match, &iter))
  1091.     {
  1092.       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
  1093.                                  SYMBOL_DOMAIN (sym), namespace))
  1094.         {
  1095.           if (callback (block, sym, data))
  1096.             return 1;
  1097.         }
  1098.     }

  1099.   return 0;
  1100. }

  1101. /*  Psymtab version of map_matching_symbols.  See its definition in
  1102.     the definition of quick_symbol_functions in symfile.h.  */

  1103. static void
  1104. psym_map_matching_symbols (struct objfile *objfile,
  1105.                            const char *name, domain_enum namespace,
  1106.                            int global,
  1107.                            int (*callback) (struct block *,
  1108.                                             struct symbol *, void *),
  1109.                            void *data,
  1110.                            symbol_compare_ftype *match,
  1111.                            symbol_compare_ftype *ordered_compare)
  1112. {
  1113.   const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
  1114.   struct partial_symtab *ps;

  1115.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  1116.     {
  1117.       QUIT;
  1118.       if (ps->readin
  1119.           || match_partial_symbol (objfile, ps, global, name, namespace, match,
  1120.                                    ordered_compare))
  1121.         {
  1122.           struct compunit_symtab *cust = psymtab_to_symtab (objfile, ps);
  1123.           struct block *block;

  1124.           if (cust == NULL)
  1125.             continue;
  1126.           block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
  1127.           if (map_block (name, namespace, objfile, block,
  1128.                          callback, data, match))
  1129.             return;
  1130.           if (callback (block, NULL, data))
  1131.             return;
  1132.         }
  1133.     }
  1134. }

  1135. /* A helper for psym_expand_symtabs_matching that handles
  1136.    searching included psymtabs.  This returns 1 if a symbol is found,
  1137.    and zero otherwise.  It also updates the 'searched_flag' on the
  1138.    various psymtabs that it searches.  */

  1139. static int
  1140. recursively_search_psymtabs (struct partial_symtab *ps,
  1141.                              struct objfile *objfile,
  1142.                              enum search_domain kind,
  1143.                              expand_symtabs_symbol_matcher_ftype *sym_matcher,
  1144.                              void *data)
  1145. {
  1146.   struct partial_symbol **psym;
  1147.   struct partial_symbol **bound, **gbound, **sbound;
  1148.   int keep_going = 1;
  1149.   int result = PST_SEARCHED_AND_NOT_FOUND;
  1150.   int i;

  1151.   if (ps->searched_flag != PST_NOT_SEARCHED)
  1152.     return ps->searched_flag == PST_SEARCHED_AND_FOUND;

  1153.   /* Recurse into shared psymtabs first, because they may have already
  1154.      been searched, and this could save some time.  */
  1155.   for (i = 0; i < ps->number_of_dependencies; ++i)
  1156.     {
  1157.       int r;

  1158.       /* Skip non-shared dependencies, these are handled elsewhere.  */
  1159.       if (ps->dependencies[i]->user == NULL)
  1160.         continue;

  1161.       r = recursively_search_psymtabs (ps->dependencies[i],
  1162.                                        objfile, kind, sym_matcher, data);
  1163.       if (r != 0)
  1164.         {
  1165.           ps->searched_flag = PST_SEARCHED_AND_FOUND;
  1166.           return 1;
  1167.         }
  1168.     }

  1169.   gbound = (objfile->global_psymbols.list
  1170.             + ps->globals_offset + ps->n_global_syms);
  1171.   sbound = (objfile->static_psymbols.list
  1172.             + ps->statics_offset + ps->n_static_syms);
  1173.   bound = gbound;

  1174.   /* Go through all of the symbols stored in a partial
  1175.      symtab in one loop.  */
  1176.   psym = objfile->global_psymbols.list + ps->globals_offset;
  1177.   while (keep_going)
  1178.     {
  1179.       if (psym >= bound)
  1180.         {
  1181.           if (bound == gbound && ps->n_static_syms != 0)
  1182.             {
  1183.               psym = objfile->static_psymbols.list + ps->statics_offset;
  1184.               bound = sbound;
  1185.             }
  1186.           else
  1187.             keep_going = 0;
  1188.           continue;
  1189.         }
  1190.       else
  1191.         {
  1192.           QUIT;

  1193.           if ((kind == ALL_DOMAIN
  1194.                || (kind == VARIABLES_DOMAIN
  1195.                    && PSYMBOL_CLASS (*psym) != LOC_TYPEDEF
  1196.                    && PSYMBOL_CLASS (*psym) != LOC_BLOCK)
  1197.                || (kind == FUNCTIONS_DOMAIN
  1198.                    && PSYMBOL_CLASS (*psym) == LOC_BLOCK)
  1199.                || (kind == TYPES_DOMAIN
  1200.                    && PSYMBOL_CLASS (*psym) == LOC_TYPEDEF))
  1201.               && (*sym_matcher) (SYMBOL_SEARCH_NAME (*psym), data))
  1202.             {
  1203.               /* Found a match, so notify our caller.  */
  1204.               result = PST_SEARCHED_AND_FOUND;
  1205.               keep_going = 0;
  1206.             }
  1207.         }
  1208.       psym++;
  1209.     }

  1210.   ps->searched_flag = result;
  1211.   return result == PST_SEARCHED_AND_FOUND;
  1212. }

  1213. /*  Psymtab version of expand_symtabs_matching.  See its definition in
  1214.     the definition of quick_symbol_functions in symfile.h.  */

  1215. static void
  1216. psym_expand_symtabs_matching
  1217.   (struct objfile *objfile,
  1218.    expand_symtabs_file_matcher_ftype *file_matcher,
  1219.    expand_symtabs_symbol_matcher_ftype *symbol_matcher,
  1220.    enum search_domain kind,
  1221.    void *data)
  1222. {
  1223.   struct partial_symtab *ps;

  1224.   /* Clear the search flags.  */
  1225.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  1226.     {
  1227.       ps->searched_flag = PST_NOT_SEARCHED;
  1228.     }

  1229.   ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, ps)
  1230.     {
  1231.       if (ps->readin)
  1232.         continue;

  1233.       /* We skip shared psymtabs because file-matching doesn't apply
  1234.          to them; but we search them later in the loop.  */
  1235.       if (ps->user != NULL)
  1236.         continue;

  1237.       if (file_matcher)
  1238.         {
  1239.           int match;

  1240.           if (ps->anonymous)
  1241.             continue;

  1242.           match = (*file_matcher) (ps->filename, data, 0);
  1243.           if (!match)
  1244.             {
  1245.               /* Before we invoke realpath, which can get expensive when many
  1246.                  files are involved, do a quick comparison of the basenames.  */
  1247.               if (basenames_may_differ
  1248.                   || (*file_matcher) (lbasename (ps->filename), data, 1))
  1249.                 match = (*file_matcher) (psymtab_to_fullname (ps), data, 0);
  1250.             }
  1251.           if (!match)
  1252.             continue;
  1253.         }

  1254.       if (recursively_search_psymtabs (ps, objfile, kind, symbol_matcher, data))
  1255.         psymtab_to_symtab (objfile, ps);
  1256.     }
  1257. }

  1258. /*  Psymtab version of has_symbols.  See its definition in
  1259.     the definition of quick_symbol_functions in symfile.h.  */

  1260. static int
  1261. psym_has_symbols (struct objfile *objfile)
  1262. {
  1263.   return objfile->psymtabs != NULL;
  1264. }

  1265. const struct quick_symbol_functions psym_functions =
  1266. {
  1267.   psym_has_symbols,
  1268.   psym_find_last_source_symtab,
  1269.   psym_forget_cached_source_info,
  1270.   psym_map_symtabs_matching_filename,
  1271.   psym_lookup_symbol,
  1272.   psym_print_stats,
  1273.   psym_dump,
  1274.   psym_relocate,
  1275.   psym_expand_symtabs_for_function,
  1276.   psym_expand_all_symtabs,
  1277.   psym_expand_symtabs_with_fullname,
  1278.   psym_map_matching_symbols,
  1279.   psym_expand_symtabs_matching,
  1280.   psym_find_pc_sect_compunit_symtab,
  1281.   psym_map_symbol_filenames
  1282. };



  1283. /* This compares two partial symbols by names, using strcmp_iw_ordered
  1284.    for the comparison.  */

  1285. static int
  1286. compare_psymbols (const void *s1p, const void *s2p)
  1287. {
  1288.   struct partial_symbol *const *s1 = s1p;
  1289.   struct partial_symbol *const *s2 = s2p;

  1290.   return strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*s1),
  1291.                             SYMBOL_SEARCH_NAME (*s2));
  1292. }

  1293. void
  1294. sort_pst_symbols (struct objfile *objfile, struct partial_symtab *pst)
  1295. {
  1296.   /* Sort the global list; don't sort the static list.  */

  1297.   qsort (objfile->global_psymbols.list + pst->globals_offset,
  1298.          pst->n_global_syms, sizeof (struct partial_symbol *),
  1299.          compare_psymbols);
  1300. }

  1301. /* Allocate and partially fill a partial symtab.  It will be
  1302.    completely filled at the end of the symbol list.

  1303.    FILENAME is the name of the symbol-file we are reading from.  */

  1304. struct partial_symtab *
  1305. start_psymtab_common (struct objfile *objfile,
  1306.                       struct section_offsets *section_offsets,
  1307.                       const char *filename,
  1308.                       CORE_ADDR textlow, struct partial_symbol **global_syms,
  1309.                       struct partial_symbol **static_syms)
  1310. {
  1311.   struct partial_symtab *psymtab;

  1312.   psymtab = allocate_psymtab (filename, objfile);
  1313.   psymtab->section_offsets = section_offsets;
  1314.   psymtab->textlow = textlow;
  1315.   psymtab->texthigh = psymtab->textlow;                /* default */
  1316.   psymtab->globals_offset = global_syms - objfile->global_psymbols.list;
  1317.   psymtab->statics_offset = static_syms - objfile->static_psymbols.list;
  1318.   return (psymtab);
  1319. }

  1320. /* Calculate a hash code for the given partial symbol.  The hash is
  1321.    calculated using the symbol's value, language, domain, class
  1322.    and name.  These are the values which are set by
  1323.    add_psymbol_to_bcache.  */

  1324. static unsigned long
  1325. psymbol_hash (const void *addr, int length)
  1326. {
  1327.   unsigned long h = 0;
  1328.   struct partial_symbol *psymbol = (struct partial_symbol *) addr;
  1329.   unsigned int lang = psymbol->ginfo.language;
  1330.   unsigned int domain = PSYMBOL_DOMAIN (psymbol);
  1331.   unsigned int class = PSYMBOL_CLASS (psymbol);

  1332.   h = hash_continue (&psymbol->ginfo.value, sizeof (psymbol->ginfo.value), h);
  1333.   h = hash_continue (&lang, sizeof (unsigned int), h);
  1334.   h = hash_continue (&domain, sizeof (unsigned int), h);
  1335.   h = hash_continue (&class, sizeof (unsigned int), h);
  1336.   h = hash_continue (psymbol->ginfo.name, strlen (psymbol->ginfo.name), h);

  1337.   return h;
  1338. }

  1339. /* Returns true if the symbol at addr1 equals the symbol at addr2.
  1340.    For the comparison this function uses a symbols value,
  1341.    language, domain, class and name.  */

  1342. static int
  1343. psymbol_compare (const void *addr1, const void *addr2, int length)
  1344. {
  1345.   struct partial_symbol *sym1 = (struct partial_symbol *) addr1;
  1346.   struct partial_symbol *sym2 = (struct partial_symbol *) addr2;

  1347.   return (memcmp (&sym1->ginfo.value, &sym1->ginfo.value,
  1348.                   sizeof (sym1->ginfo.value)) == 0
  1349.           && sym1->ginfo.language == sym2->ginfo.language
  1350.           && PSYMBOL_DOMAIN (sym1) == PSYMBOL_DOMAIN (sym2)
  1351.           && PSYMBOL_CLASS (sym1) == PSYMBOL_CLASS (sym2)
  1352.           && sym1->ginfo.name == sym2->ginfo.name);
  1353. }

  1354. /* Initialize a partial symbol bcache.  */

  1355. struct psymbol_bcache *
  1356. psymbol_bcache_init (void)
  1357. {
  1358.   struct psymbol_bcache *bcache = XCNEW (struct psymbol_bcache);
  1359.   bcache->bcache = bcache_xmalloc (psymbol_hash, psymbol_compare);
  1360.   return bcache;
  1361. }

  1362. /* Free a partial symbol bcache.  */
  1363. void
  1364. psymbol_bcache_free (struct psymbol_bcache *bcache)
  1365. {
  1366.   if (bcache == NULL)
  1367.     return;

  1368.   bcache_xfree (bcache->bcache);
  1369.   xfree (bcache);
  1370. }

  1371. /* Return the internal bcache of the psymbol_bcache BCACHE.  */

  1372. struct bcache *
  1373. psymbol_bcache_get_bcache (struct psymbol_bcache *bcache)
  1374. {
  1375.   return bcache->bcache;
  1376. }

  1377. /* Find a copy of the SYM in BCACHE.  If BCACHE has never seen this
  1378.    symbol before, add a copy to BCACHE.  In either case, return a pointer
  1379.    to BCACHE's copy of the symbol.  If optional ADDED is not NULL, return
  1380.    1 in case of new entry or 0 if returning an old entry.  */

  1381. static const struct partial_symbol *
  1382. psymbol_bcache_full (struct partial_symbol *sym,
  1383.                      struct psymbol_bcache *bcache,
  1384.                      int *added)
  1385. {
  1386.   return bcache_full (sym,
  1387.                       sizeof (struct partial_symbol),
  1388.                       bcache->bcache,
  1389.                       added);
  1390. }

  1391. /* Helper function, initialises partial symbol structure and stashes
  1392.    it into objfile's bcache.  Note that our caching mechanism will
  1393.    use all fields of struct partial_symbol to determine hash value of the
  1394.    structure.  In other words, having two symbols with the same name but
  1395.    different domain (or address) is possible and correct.  */

  1396. static const struct partial_symbol *
  1397. add_psymbol_to_bcache (const char *name, int namelength, int copy_name,
  1398.                        domain_enum domain,
  1399.                        enum address_class class,
  1400.                        long val,        /* Value as a long */
  1401.                        CORE_ADDR coreaddr,        /* Value as a CORE_ADDR */
  1402.                        enum language language, struct objfile *objfile,
  1403.                        int *added)
  1404. {
  1405.   struct partial_symbol psymbol;

  1406.   /* We must ensure that the entire struct has been zeroed before
  1407.      assigning to it, because an assignment may not touch some of the
  1408.      holes.  */
  1409.   memset (&psymbol, 0, sizeof (psymbol));

  1410.   /* val and coreaddr are mutually exclusive, one of them *will* be zero.  */
  1411.   if (val != 0)
  1412.     {
  1413.       SYMBOL_VALUE (&psymbol) = val;
  1414.     }
  1415.   else
  1416.     {
  1417.       SYMBOL_VALUE_ADDRESS (&psymbol) = coreaddr;
  1418.     }
  1419.   SYMBOL_SECTION (&psymbol) = -1;
  1420.   SYMBOL_SET_LANGUAGE (&psymbol, language, &objfile->objfile_obstack);
  1421.   PSYMBOL_DOMAIN (&psymbol) = domain;
  1422.   PSYMBOL_CLASS (&psymbol) = class;

  1423.   SYMBOL_SET_NAMES (&psymbol, name, namelength, copy_name, objfile);

  1424.   /* Stash the partial symbol away in the cache.  */
  1425.   return psymbol_bcache_full (&psymbol,
  1426.                               objfile->psymbol_cache,
  1427.                               added);
  1428. }

  1429. /* Increase the space allocated for LISTP, which is probably
  1430.    global_psymbols or static_psymbols.  This space will eventually
  1431.    be freed in free_objfile().  */

  1432. static void
  1433. extend_psymbol_list (struct psymbol_allocation_list *listp,
  1434.                      struct objfile *objfile)
  1435. {
  1436.   int new_size;

  1437.   if (listp->size == 0)
  1438.     {
  1439.       new_size = 255;
  1440.       listp->list = (struct partial_symbol **)
  1441.         xmalloc (new_size * sizeof (struct partial_symbol *));
  1442.     }
  1443.   else
  1444.     {
  1445.       new_size = listp->size * 2;
  1446.       listp->list = (struct partial_symbol **)
  1447.         xrealloc ((char *) listp->list,
  1448.                   new_size * sizeof (struct partial_symbol *));
  1449.     }
  1450.   /* Next assumes we only went one over.  Should be good if
  1451.      program works correctly.  */
  1452.   listp->next = listp->list + listp->size;
  1453.   listp->size = new_size;
  1454. }

  1455. /* Helper function, adds partial symbol to the given partial symbol
  1456.    list.  */

  1457. static void
  1458. append_psymbol_to_list (struct psymbol_allocation_list *list,
  1459.                         const struct partial_symbol *psym,
  1460.                         struct objfile *objfile)
  1461. {
  1462.   if (list->next >= list->list + list->size)
  1463.     extend_psymbol_list (list, objfile);
  1464.   *list->next++ = (struct partial_symbol *) psym;
  1465.   OBJSTAT (objfile, n_psyms++);
  1466. }

  1467. /* Add a symbol with a long value to a psymtab.
  1468.    Since one arg is a struct, we pass in a ptr and deref it (sigh).
  1469.    Return the partial symbol that has been added.  */

  1470. void
  1471. add_psymbol_to_list (const char *name, int namelength, int copy_name,
  1472.                      domain_enum domain,
  1473.                      enum address_class class,
  1474.                      struct psymbol_allocation_list *list,
  1475.                      long val,        /* Value as a long */
  1476.                      CORE_ADDR coreaddr,        /* Value as a CORE_ADDR */
  1477.                      enum language language, struct objfile *objfile)
  1478. {
  1479.   const struct partial_symbol *psym;

  1480.   int added;

  1481.   /* Stash the partial symbol away in the cache.  */
  1482.   psym = add_psymbol_to_bcache (name, namelength, copy_name, domain, class,
  1483.                                 val, coreaddr, language, objfile, &added);

  1484.   /* Do not duplicate global partial symbols.  */
  1485.   if (list == &objfile->global_psymbols
  1486.       && !added)
  1487.     return;

  1488.   /* Save pointer to partial symbol in psymtab, growing symtab if needed.  */
  1489.   append_psymbol_to_list (list, psym, objfile);
  1490. }

  1491. /* Initialize storage for partial symbols.  */

  1492. void
  1493. init_psymbol_list (struct objfile *objfile, int total_symbols)
  1494. {
  1495.   /* Free any previously allocated psymbol lists.  */

  1496.   if (objfile->global_psymbols.list)
  1497.     {
  1498.       xfree (objfile->global_psymbols.list);
  1499.     }
  1500.   if (objfile->static_psymbols.list)
  1501.     {
  1502.       xfree (objfile->static_psymbols.list);
  1503.     }

  1504.   /* Current best guess is that approximately a twentieth
  1505.      of the total symbols (in a debugging file) are global or static
  1506.      oriented symbols, then multiply that by slop factor of two.  */

  1507.   objfile->global_psymbols.size = total_symbols / 10;
  1508.   objfile->static_psymbols.size = total_symbols / 10;

  1509.   if (objfile->global_psymbols.size > 0)
  1510.     {
  1511.       objfile->global_psymbols.next =
  1512.         objfile->global_psymbols.list = (struct partial_symbol **)
  1513.         xmalloc ((objfile->global_psymbols.size
  1514.                   * sizeof (struct partial_symbol *)));
  1515.     }
  1516.   if (objfile->static_psymbols.size > 0)
  1517.     {
  1518.       objfile->static_psymbols.next =
  1519.         objfile->static_psymbols.list = (struct partial_symbol **)
  1520.         xmalloc ((objfile->static_psymbols.size
  1521.                   * sizeof (struct partial_symbol *)));
  1522.     }
  1523. }

  1524. struct partial_symtab *
  1525. allocate_psymtab (const char *filename, struct objfile *objfile)
  1526. {
  1527.   struct partial_symtab *psymtab;

  1528.   if (objfile->free_psymtabs)
  1529.     {
  1530.       psymtab = objfile->free_psymtabs;
  1531.       objfile->free_psymtabs = psymtab->next;
  1532.     }
  1533.   else
  1534.     psymtab = (struct partial_symtab *)
  1535.       obstack_alloc (&objfile->objfile_obstack,
  1536.                      sizeof (struct partial_symtab));

  1537.   memset (psymtab, 0, sizeof (struct partial_symtab));
  1538.   psymtab->filename = bcache (filename, strlen (filename) + 1,
  1539.                               objfile->per_bfd->filename_cache);
  1540.   psymtab->compunit_symtab = NULL;

  1541.   /* Prepend it to the psymtab list for the objfile it belongs to.
  1542.      Psymtabs are searched in most recent inserted -> least recent
  1543.      inserted order.  */

  1544.   psymtab->next = objfile->psymtabs;
  1545.   objfile->psymtabs = psymtab;

  1546.   if (symtab_create_debug)
  1547.     {
  1548.       /* Be a bit clever with debugging messages, and don't print objfile
  1549.          every time, only when it changes.  */
  1550.       static char *last_objfile_name = NULL;

  1551.       if (last_objfile_name == NULL
  1552.           || strcmp (last_objfile_name, objfile_name (objfile)) != 0)
  1553.         {
  1554.           xfree (last_objfile_name);
  1555.           last_objfile_name = xstrdup (objfile_name (objfile));
  1556.           fprintf_unfiltered (gdb_stdlog,
  1557.                               "Creating one or more psymtabs for objfile %s ...\n",
  1558.                               last_objfile_name);
  1559.         }
  1560.       fprintf_unfiltered (gdb_stdlog,
  1561.                           "Created psymtab %s for module %s.\n",
  1562.                           host_address_to_string (psymtab), filename);
  1563.     }

  1564.   return (psymtab);
  1565. }

  1566. void
  1567. discard_psymtab (struct objfile *objfile, struct partial_symtab *pst)
  1568. {
  1569.   struct partial_symtab **prev_pst;

  1570.   /* From dbxread.c:
  1571.      Empty psymtabs happen as a result of header files which don't
  1572.      have any symbols in them.  There can be a lot of them.  But this
  1573.      check is wrong, in that a psymtab with N_SLINE entries but
  1574.      nothing else is not empty, but we don't realize that.  Fixing
  1575.      that without slowing things down might be tricky.  */

  1576.   /* First, snip it out of the psymtab chain.  */

  1577.   prev_pst = &(objfile->psymtabs);
  1578.   while ((*prev_pst) != pst)
  1579.     prev_pst = &((*prev_pst)->next);
  1580.   (*prev_pst) = pst->next;

  1581.   /* Next, put it on a free list for recycling.  */

  1582.   pst->next = objfile->free_psymtabs;
  1583.   objfile->free_psymtabs = pst;
  1584. }

  1585. /* An object of this type is passed to discard_psymtabs_upto.  */

  1586. struct psymtab_state
  1587. {
  1588.   /* The objfile where psymtabs are discarded.  */

  1589.   struct objfile *objfile;

  1590.   /* The first psymtab to save.  */

  1591.   struct partial_symtab *save;
  1592. };

  1593. /* A cleanup function used by make_cleanup_discard_psymtabs.  */

  1594. static void
  1595. discard_psymtabs_upto (void *arg)
  1596. {
  1597.   struct psymtab_state *state = arg;

  1598.   while (state->objfile->psymtabs != state->save)
  1599.     discard_psymtab (state->objfile, state->objfile->psymtabs);
  1600. }

  1601. /* Return a new cleanup that discards all psymtabs created in OBJFILE
  1602.    after this function is called.  */

  1603. struct cleanup *
  1604. make_cleanup_discard_psymtabs (struct objfile *objfile)
  1605. {
  1606.   struct psymtab_state *state = XNEW (struct psymtab_state);

  1607.   state->objfile = objfile;
  1608.   state->save = objfile->psymtabs;

  1609.   return make_cleanup_dtor (discard_psymtabs_upto, state, xfree);
  1610. }



  1611. static void
  1612. maintenance_print_psymbols (char *args, int from_tty)
  1613. {
  1614.   char **argv;
  1615.   struct ui_file *outfile;
  1616.   struct cleanup *cleanups;
  1617.   char *symname = NULL;
  1618.   char *filename = DEV_TTY;
  1619.   struct objfile *objfile;
  1620.   struct partial_symtab *ps;

  1621.   dont_repeat ();

  1622.   if (args == NULL)
  1623.     {
  1624.       error (_("\
  1625. print-psymbols takes an output file name and optional symbol file name"));
  1626.     }
  1627.   argv = gdb_buildargv (args);
  1628.   cleanups = make_cleanup_freeargv (argv);

  1629.   if (argv[0] != NULL)
  1630.     {
  1631.       filename = argv[0];
  1632.       /* If a second arg is supplied, it is a source file name to match on.  */
  1633.       if (argv[1] != NULL)
  1634.         {
  1635.           symname = argv[1];
  1636.         }
  1637.     }

  1638.   filename = tilde_expand (filename);
  1639.   make_cleanup (xfree, filename);

  1640.   outfile = gdb_fopen (filename, FOPEN_WT);
  1641.   if (outfile == 0)
  1642.     perror_with_name (filename);
  1643.   make_cleanup_ui_file_delete (outfile);

  1644.   ALL_PSYMTABS (objfile, ps)
  1645.     {
  1646.       QUIT;
  1647.       if (symname == NULL || filename_cmp (symname, ps->filename) == 0)
  1648.         dump_psymtab (objfile, ps, outfile);
  1649.     }
  1650.   do_cleanups (cleanups);
  1651. }

  1652. /* List all the partial symbol tables whose names match REGEXP (optional).  */
  1653. static void
  1654. maintenance_info_psymtabs (char *regexp, int from_tty)
  1655. {
  1656.   struct program_space *pspace;
  1657.   struct objfile *objfile;

  1658.   if (regexp)
  1659.     re_comp (regexp);

  1660.   ALL_PSPACES (pspace)
  1661.     ALL_PSPACE_OBJFILES (pspace, objfile)
  1662.     {
  1663.       struct gdbarch *gdbarch = get_objfile_arch (objfile);
  1664.       struct partial_symtab *psymtab;

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

  1668.       ALL_OBJFILE_PSYMTABS_REQUIRED (objfile, psymtab)
  1669.         {
  1670.           QUIT;

  1671.           if (! regexp
  1672.               || re_exec (psymtab->filename))
  1673.             {
  1674.               if (! printed_objfile_start)
  1675.                 {
  1676.                   printf_filtered ("{ objfile %s ", objfile_name (objfile));
  1677.                   wrap_here ("  ");
  1678.                   printf_filtered ("((struct objfile *) %s)\n",
  1679.                                    host_address_to_string (objfile));
  1680.                   printed_objfile_start = 1;
  1681.                 }

  1682.               printf_filtered ("  { psymtab %s ", psymtab->filename);
  1683.               wrap_here ("    ");
  1684.               printf_filtered ("((struct partial_symtab *) %s)\n",
  1685.                                host_address_to_string (psymtab));

  1686.               printf_filtered ("    readin %s\n",
  1687.                                psymtab->readin ? "yes" : "no");
  1688.               printf_filtered ("    fullname %s\n",
  1689.                                psymtab->fullname
  1690.                                ? psymtab->fullname : "(null)");
  1691.               printf_filtered ("    text addresses ");
  1692.               fputs_filtered (paddress (gdbarch, psymtab->textlow),
  1693.                               gdb_stdout);
  1694.               printf_filtered (" -- ");
  1695.               fputs_filtered (paddress (gdbarch, psymtab->texthigh),
  1696.                               gdb_stdout);
  1697.               printf_filtered ("\n");
  1698.               printf_filtered ("    psymtabs_addrmap_supported %s\n",
  1699.                                (psymtab->psymtabs_addrmap_supported
  1700.                                 ? "yes" : "no"));
  1701.               printf_filtered ("    globals ");
  1702.               if (psymtab->n_global_syms)
  1703.                 {
  1704.                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
  1705.                                    host_address_to_string (objfile->global_psymbols.list
  1706.                                     + psymtab->globals_offset),
  1707.                                    psymtab->n_global_syms);
  1708.                 }
  1709.               else
  1710.                 printf_filtered ("(none)\n");
  1711.               printf_filtered ("    statics ");
  1712.               if (psymtab->n_static_syms)
  1713.                 {
  1714.                   printf_filtered ("(* (struct partial_symbol **) %s @ %d)\n",
  1715.                                    host_address_to_string (objfile->static_psymbols.list
  1716.                                     + psymtab->statics_offset),
  1717.                                    psymtab->n_static_syms);
  1718.                 }
  1719.               else
  1720.                 printf_filtered ("(none)\n");
  1721.               printf_filtered ("    dependencies ");
  1722.               if (psymtab->number_of_dependencies)
  1723.                 {
  1724.                   int i;

  1725.                   printf_filtered ("{\n");
  1726.                   for (i = 0; i < psymtab->number_of_dependencies; i++)
  1727.                     {
  1728.                       struct partial_symtab *dep = psymtab->dependencies[i];

  1729.                       /* Note the string concatenation there --- no comma.  */
  1730.                       printf_filtered ("      psymtab %s "
  1731.                                        "((struct partial_symtab *) %s)\n",
  1732.                                        dep->filename,
  1733.                                        host_address_to_string (dep));
  1734.                     }
  1735.                   printf_filtered ("    }\n");
  1736.                 }
  1737.               else
  1738.                 printf_filtered ("(none)\n");
  1739.               printf_filtered ("  }\n");
  1740.             }
  1741.         }

  1742.       if (printed_objfile_start)
  1743.         printf_filtered ("}\n");
  1744.     }
  1745. }

  1746. /* Check consistency of currently expanded psymtabs vs symtabs.  */

  1747. static void
  1748. maintenance_check_psymtabs (char *ignore, int from_tty)
  1749. {
  1750.   struct symbol *sym;
  1751.   struct partial_symbol **psym;
  1752.   struct compunit_symtab *cust = NULL;
  1753.   struct partial_symtab *ps;
  1754.   const struct blockvector *bv;
  1755.   struct objfile *objfile;
  1756.   struct block *b;
  1757.   int length;

  1758.   ALL_PSYMTABS (objfile, ps)
  1759.   {
  1760.     struct gdbarch *gdbarch = get_objfile_arch (objfile);

  1761.     /* We don't call psymtab_to_symtab here because that may cause symtab
  1762.        expansion.  When debugging a problem it helps if checkers leave
  1763.        things unchanged.  */
  1764.     cust = ps->compunit_symtab;

  1765.     /* First do some checks that don't require the associated symtab.  */
  1766.     if (ps->texthigh < ps->textlow)
  1767.       {
  1768.         printf_filtered ("Psymtab ");
  1769.         puts_filtered (ps->filename);
  1770.         printf_filtered (" covers bad range ");
  1771.         fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
  1772.         printf_filtered (" - ");
  1773.         fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
  1774.         printf_filtered ("\n");
  1775.         continue;
  1776.       }

  1777.     /* Now do checks requiring the associated symtab.  */
  1778.     if (cust == NULL)
  1779.       continue;
  1780.     bv = COMPUNIT_BLOCKVECTOR (cust);
  1781.     b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1782.     psym = objfile->static_psymbols.list + ps->statics_offset;
  1783.     length = ps->n_static_syms;
  1784.     while (length--)
  1785.       {
  1786.         sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
  1787.                                    SYMBOL_DOMAIN (*psym));
  1788.         if (!sym)
  1789.           {
  1790.             printf_filtered ("Static symbol `");
  1791.             puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
  1792.             printf_filtered ("' only found in ");
  1793.             puts_filtered (ps->filename);
  1794.             printf_filtered (" psymtab\n");
  1795.           }
  1796.         psym++;
  1797.       }
  1798.     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1799.     psym = objfile->global_psymbols.list + ps->globals_offset;
  1800.     length = ps->n_global_syms;
  1801.     while (length--)
  1802.       {
  1803.         sym = block_lookup_symbol (b, SYMBOL_LINKAGE_NAME (*psym),
  1804.                                    SYMBOL_DOMAIN (*psym));
  1805.         if (!sym)
  1806.           {
  1807.             printf_filtered ("Global symbol `");
  1808.             puts_filtered (SYMBOL_LINKAGE_NAME (*psym));
  1809.             printf_filtered ("' only found in ");
  1810.             puts_filtered (ps->filename);
  1811.             printf_filtered (" psymtab\n");
  1812.           }
  1813.         psym++;
  1814.       }
  1815.     if (ps->texthigh != 0
  1816.         && (ps->textlow < BLOCK_START (b) || ps->texthigh > BLOCK_END (b)))
  1817.       {
  1818.         printf_filtered ("Psymtab ");
  1819.         puts_filtered (ps->filename);
  1820.         printf_filtered (" covers ");
  1821.         fputs_filtered (paddress (gdbarch, ps->textlow), gdb_stdout);
  1822.         printf_filtered (" - ");
  1823.         fputs_filtered (paddress (gdbarch, ps->texthigh), gdb_stdout);
  1824.         printf_filtered (" but symtab covers only ");
  1825.         fputs_filtered (paddress (gdbarch, BLOCK_START (b)), gdb_stdout);
  1826.         printf_filtered (" - ");
  1827.         fputs_filtered (paddress (gdbarch, BLOCK_END (b)), gdb_stdout);
  1828.         printf_filtered ("\n");
  1829.       }
  1830.   }
  1831. }



  1832. extern initialize_file_ftype _initialize_psymtab;

  1833. void
  1834. _initialize_psymtab (void)
  1835. {
  1836.   add_cmd ("psymbols", class_maintenance, maintenance_print_psymbols, _("\
  1837. Print dump of current partial symbol definitions.\n\
  1838. Entries in the partial symbol table are dumped to file OUTFILE.\n\
  1839. If a SOURCE file is specified, dump only that file's partial symbols."),
  1840.            &maintenanceprintlist);

  1841.   add_cmd ("psymtabs", class_maintenance, maintenance_info_psymtabs, _("\
  1842. List the partial symbol tables for all object files.\n\
  1843. This does not include information about individual partial symbols,\n\
  1844. just the symbol table structures themselves."),
  1845.            &maintenanceinfolist);

  1846.   add_cmd ("check-psymtabs", class_maintenance, maintenance_check_psymtabs,
  1847.            _("\
  1848. Check consistency of currently expanded psymtabs versus symtabs."),
  1849.            &maintenancelist);
  1850. }