gdb/symtab.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Symbol table lookup for the GNU debugger, 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 "gdbcore.h"
  18. #include "frame.h"
  19. #include "target.h"
  20. #include "value.h"
  21. #include "symfile.h"
  22. #include "objfiles.h"
  23. #include "gdbcmd.h"
  24. #include "gdb_regex.h"
  25. #include "expression.h"
  26. #include "language.h"
  27. #include "demangle.h"
  28. #include "inferior.h"
  29. #include "source.h"
  30. #include "filenames.h"                /* for FILENAME_CMP */
  31. #include "objc-lang.h"
  32. #include "d-lang.h"
  33. #include "ada-lang.h"
  34. #include "go-lang.h"
  35. #include "p-lang.h"
  36. #include "addrmap.h"
  37. #include "cli/cli-utils.h"

  38. #include "hashtab.h"

  39. #include "gdb_obstack.h"
  40. #include "block.h"
  41. #include "dictionary.h"

  42. #include <sys/types.h>
  43. #include <fcntl.h>
  44. #include <sys/stat.h>
  45. #include <ctype.h>
  46. #include "cp-abi.h"
  47. #include "cp-support.h"
  48. #include "observer.h"
  49. #include "solist.h"
  50. #include "macrotab.h"
  51. #include "macroscope.h"

  52. #include "parser-defs.h"

  53. /* Forward declarations for local functions.  */

  54. static void rbreak_command (char *, int);

  55. static int find_line_common (struct linetable *, int, int *, int);

  56. static struct symbol *lookup_symbol_aux (const char *name,
  57.                                          const struct block *block,
  58.                                          const domain_enum domain,
  59.                                          enum language language,
  60.                                          struct field_of_this_result *);

  61. static
  62. struct symbol *lookup_local_symbol (const char *name,
  63.                                     const struct block *block,
  64.                                     const domain_enum domain,
  65.                                     enum language language);

  66. static struct symbol *
  67.   lookup_symbol_in_objfile (struct objfile *objfile, int block_index,
  68.                             const char *name, const domain_enum domain);

  69. extern initialize_file_ftype _initialize_symtab;

  70. /* Program space key for finding name and language of "main".  */

  71. static const struct program_space_data *main_progspace_key;

  72. /* Type of the data stored on the program space.  */

  73. struct main_info
  74. {
  75.   /* Name of "main".  */

  76.   char *name_of_main;

  77.   /* Language of "main".  */

  78.   enum language language_of_main;
  79. };

  80. /* When non-zero, print debugging messages related to symtab creation.  */
  81. unsigned int symtab_create_debug = 0;

  82. /* When non-zero, print debugging messages related to symbol lookup.  */
  83. unsigned int symbol_lookup_debug = 0;

  84. /* Non-zero if a file may be known by two different basenames.
  85.    This is the uncommon case, and significantly slows down gdb.
  86.    Default set to "off" to not slow down the common case.  */
  87. int basenames_may_differ = 0;

  88. /* Allow the user to configure the debugger behavior with respect
  89.    to multiple-choice menus when more than one symbol matches during
  90.    a symbol lookup.  */

  91. const char multiple_symbols_ask[] = "ask";
  92. const char multiple_symbols_all[] = "all";
  93. const char multiple_symbols_cancel[] = "cancel";
  94. static const char *const multiple_symbols_modes[] =
  95. {
  96.   multiple_symbols_ask,
  97.   multiple_symbols_all,
  98.   multiple_symbols_cancel,
  99.   NULL
  100. };
  101. static const char *multiple_symbols_mode = multiple_symbols_all;

  102. /* Read-only accessor to AUTO_SELECT_MODE.  */

  103. const char *
  104. multiple_symbols_select_mode (void)
  105. {
  106.   return multiple_symbols_mode;
  107. }

  108. /* Block in which the most recently searched-for symbol was found.
  109.    Might be better to make this a parameter to lookup_symbol and
  110.    value_of_this.  */

  111. const struct block *block_found;

  112. /* Return the name of a domain_enum.  */

  113. const char *
  114. domain_name (domain_enum e)
  115. {
  116.   switch (e)
  117.     {
  118.     case UNDEF_DOMAIN: return "UNDEF_DOMAIN";
  119.     case VAR_DOMAIN: return "VAR_DOMAIN";
  120.     case STRUCT_DOMAIN: return "STRUCT_DOMAIN";
  121.     case MODULE_DOMAIN: return "MODULE_DOMAIN";
  122.     case LABEL_DOMAIN: return "LABEL_DOMAIN";
  123.     case COMMON_BLOCK_DOMAIN: return "COMMON_BLOCK_DOMAIN";
  124.     default: gdb_assert_not_reached ("bad domain_enum");
  125.     }
  126. }

  127. /* Return the name of a search_domain .  */

  128. const char *
  129. search_domain_name (enum search_domain e)
  130. {
  131.   switch (e)
  132.     {
  133.     case VARIABLES_DOMAIN: return "VARIABLES_DOMAIN";
  134.     case FUNCTIONS_DOMAIN: return "FUNCTIONS_DOMAIN";
  135.     case TYPES_DOMAIN: return "TYPES_DOMAIN";
  136.     case ALL_DOMAIN: return "ALL_DOMAIN";
  137.     default: gdb_assert_not_reached ("bad search_domain");
  138.     }
  139. }

  140. /* See symtab.h.  */

  141. struct symtab *
  142. compunit_primary_filetab (const struct compunit_symtab *cust)
  143. {
  144.   gdb_assert (COMPUNIT_FILETABS (cust) != NULL);

  145.   /* The primary file symtab is the first one in the list.  */
  146.   return COMPUNIT_FILETABS (cust);
  147. }

  148. /* See symtab.h.  */

  149. enum language
  150. compunit_language (const struct compunit_symtab *cust)
  151. {
  152.   struct symtab *symtab = compunit_primary_filetab (cust);

  153. /* The language of the compunit symtab is the language of its primary
  154.    source file.  */
  155.   return SYMTAB_LANGUAGE (symtab);
  156. }

  157. /* See whether FILENAME matches SEARCH_NAME using the rule that we
  158.    advertise to the user.  (The manual's description of linespecs
  159.    describes what we advertise).  Returns true if they match, false
  160.    otherwise.  */

  161. int
  162. compare_filenames_for_search (const char *filename, const char *search_name)
  163. {
  164.   int len = strlen (filename);
  165.   size_t search_len = strlen (search_name);

  166.   if (len < search_len)
  167.     return 0;

  168.   /* The tail of FILENAME must match.  */
  169.   if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
  170.     return 0;

  171.   /* Either the names must completely match, or the character
  172.      preceding the trailing SEARCH_NAME segment of FILENAME must be a
  173.      directory separator.

  174.      The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
  175.      cannot match FILENAME "/path//dir/file.c" - as user has requested
  176.      absolute path.  The sama applies for "c:\file.c" possibly
  177.      incorrectly hypothetically matching "d:\dir\c:\file.c".

  178.      The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
  179.      compatible with SEARCH_NAME "file.c".  In such case a compiler had
  180.      to put the "c:file.c" name into debug info.  Such compatibility
  181.      works only on GDB built for DOS host.  */
  182.   return (len == search_len
  183.           || (!IS_ABSOLUTE_PATH (search_name)
  184.               && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
  185.           || (HAS_DRIVE_SPEC (filename)
  186.               && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
  187. }

  188. /* Check for a symtab of a specific name by searching some symtabs.
  189.    This is a helper function for callbacks of iterate_over_symtabs.

  190.    If NAME is not absolute, then REAL_PATH is NULL
  191.    If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.

  192.    The return value, NAME, REAL_PATH, CALLBACK, and DATA
  193.    are identical to the `map_symtabs_matching_filename' method of
  194.    quick_symbol_functions.

  195.    FIRST and AFTER_LAST indicate the range of compunit symtabs to search.
  196.    Each symtab within the specified compunit symtab is also searched.
  197.    AFTER_LAST is one past the last compunit symtab to search; NULL means to
  198.    search until the end of the list.  */

  199. int
  200. iterate_over_some_symtabs (const char *name,
  201.                            const char *real_path,
  202.                            int (*callback) (struct symtab *symtab,
  203.                                             void *data),
  204.                            void *data,
  205.                            struct compunit_symtab *first,
  206.                            struct compunit_symtab *after_last)
  207. {
  208.   struct compunit_symtab *cust;
  209.   struct symtab *s;
  210.   const char* base_name = lbasename (name);

  211.   for (cust = first; cust != NULL && cust != after_last; cust = cust->next)
  212.     {
  213.       ALL_COMPUNIT_FILETABS (cust, s)
  214.         {
  215.           if (compare_filenames_for_search (s->filename, name))
  216.             {
  217.               if (callback (s, data))
  218.                 return 1;
  219.               continue;
  220.             }

  221.           /* Before we invoke realpath, which can get expensive when many
  222.              files are involved, do a quick comparison of the basenames.  */
  223.           if (! basenames_may_differ
  224.               && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
  225.             continue;

  226.           if (compare_filenames_for_search (symtab_to_fullname (s), name))
  227.             {
  228.               if (callback (s, data))
  229.                 return 1;
  230.               continue;
  231.             }

  232.           /* If the user gave us an absolute path, try to find the file in
  233.              this symtab and use its absolute path.  */
  234.           if (real_path != NULL)
  235.             {
  236.               const char *fullname = symtab_to_fullname (s);

  237.               gdb_assert (IS_ABSOLUTE_PATH (real_path));
  238.               gdb_assert (IS_ABSOLUTE_PATH (name));
  239.               if (FILENAME_CMP (real_path, fullname) == 0)
  240.                 {
  241.                   if (callback (s, data))
  242.                     return 1;
  243.                   continue;
  244.                 }
  245.             }
  246.         }
  247.     }

  248.   return 0;
  249. }

  250. /* Check for a symtab of a specific name; first in symtabs, then in
  251.    psymtabs.  *If* there is no '/' in the name, a match after a '/'
  252.    in the symtab filename will also work.

  253.    Calls CALLBACK with each symtab that is found and with the supplied
  254.    DATA.  If CALLBACK returns true, the search stops.  */

  255. void
  256. iterate_over_symtabs (const char *name,
  257.                       int (*callback) (struct symtab *symtab,
  258.                                        void *data),
  259.                       void *data)
  260. {
  261.   struct objfile *objfile;
  262.   char *real_path = NULL;
  263.   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);

  264.   /* Here we are interested in canonicalizing an absolute path, not
  265.      absolutizing a relative path.  */
  266.   if (IS_ABSOLUTE_PATH (name))
  267.     {
  268.       real_path = gdb_realpath (name);
  269.       make_cleanup (xfree, real_path);
  270.       gdb_assert (IS_ABSOLUTE_PATH (real_path));
  271.     }

  272.   ALL_OBJFILES (objfile)
  273.   {
  274.     if (iterate_over_some_symtabs (name, real_path, callback, data,
  275.                                    objfile->compunit_symtabs, NULL))
  276.       {
  277.         do_cleanups (cleanups);
  278.         return;
  279.       }
  280.   }

  281.   /* Same search rules as above apply here, but now we look thru the
  282.      psymtabs.  */

  283.   ALL_OBJFILES (objfile)
  284.   {
  285.     if (objfile->sf
  286.         && objfile->sf->qf->map_symtabs_matching_filename (objfile,
  287.                                                            name,
  288.                                                            real_path,
  289.                                                            callback,
  290.                                                            data))
  291.       {
  292.         do_cleanups (cleanups);
  293.         return;
  294.       }
  295.   }

  296.   do_cleanups (cleanups);
  297. }

  298. /* The callback function used by lookup_symtab.  */

  299. static int
  300. lookup_symtab_callback (struct symtab *symtab, void *data)
  301. {
  302.   struct symtab **result_ptr = data;

  303.   *result_ptr = symtab;
  304.   return 1;
  305. }

  306. /* A wrapper for iterate_over_symtabs that returns the first matching
  307.    symtab, or NULL.  */

  308. struct symtab *
  309. lookup_symtab (const char *name)
  310. {
  311.   struct symtab *result = NULL;

  312.   iterate_over_symtabs (name, lookup_symtab_callback, &result);
  313.   return result;
  314. }


  315. /* Mangle a GDB method stub type.  This actually reassembles the pieces of the
  316.    full method name, which consist of the class name (from T), the unadorned
  317.    method name from METHOD_ID, and the signature for the specific overload,
  318.    specified by SIGNATURE_ID.  Note that this function is g++ specific.  */

  319. char *
  320. gdb_mangle_name (struct type *type, int method_id, int signature_id)
  321. {
  322.   int mangled_name_len;
  323.   char *mangled_name;
  324.   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
  325.   struct fn_field *method = &f[signature_id];
  326.   const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
  327.   const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
  328.   const char *newname = type_name_no_tag (type);

  329.   /* Does the form of physname indicate that it is the full mangled name
  330.      of a constructor (not just the args)?  */
  331.   int is_full_physname_constructor;

  332.   int is_constructor;
  333.   int is_destructor = is_destructor_name (physname);
  334.   /* Need a new type prefix.  */
  335.   char *const_prefix = method->is_const ? "C" : "";
  336.   char *volatile_prefix = method->is_volatile ? "V" : "";
  337.   char buf[20];
  338.   int len = (newname == NULL ? 0 : strlen (newname));

  339.   /* Nothing to do if physname already contains a fully mangled v3 abi name
  340.      or an operator name.  */
  341.   if ((physname[0] == '_' && physname[1] == 'Z')
  342.       || is_operator_name (field_name))
  343.     return xstrdup (physname);

  344.   is_full_physname_constructor = is_constructor_name (physname);

  345.   is_constructor = is_full_physname_constructor
  346.     || (newname && strcmp (field_name, newname) == 0);

  347.   if (!is_destructor)
  348.     is_destructor = (strncmp (physname, "__dt", 4) == 0);

  349.   if (is_destructor || is_full_physname_constructor)
  350.     {
  351.       mangled_name = (char *) xmalloc (strlen (physname) + 1);
  352.       strcpy (mangled_name, physname);
  353.       return mangled_name;
  354.     }

  355.   if (len == 0)
  356.     {
  357.       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
  358.     }
  359.   else if (physname[0] == 't' || physname[0] == 'Q')
  360.     {
  361.       /* The physname for template and qualified methods already includes
  362.          the class name.  */
  363.       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
  364.       newname = NULL;
  365.       len = 0;
  366.     }
  367.   else
  368.     {
  369.       xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
  370.                  volatile_prefix, len);
  371.     }
  372.   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
  373.                       + strlen (buf) + len + strlen (physname) + 1);

  374.   mangled_name = (char *) xmalloc (mangled_name_len);
  375.   if (is_constructor)
  376.     mangled_name[0] = '\0';
  377.   else
  378.     strcpy (mangled_name, field_name);

  379.   strcat (mangled_name, buf);
  380.   /* If the class doesn't have a name, i.e. newname NULL, then we just
  381.      mangle it using 0 for the length of the class.  Thus it gets mangled
  382.      as something starting with `::' rather than `classname::'.  */
  383.   if (newname != NULL)
  384.     strcat (mangled_name, newname);

  385.   strcat (mangled_name, physname);
  386.   return (mangled_name);
  387. }

  388. /* Set the demangled name of GSYMBOL to NAMENAME must be already
  389.    correctly allocated.  */

  390. void
  391. symbol_set_demangled_name (struct general_symbol_info *gsymbol,
  392.                            const char *name,
  393.                            struct obstack *obstack)
  394. {
  395.   if (gsymbol->language == language_ada)
  396.     {
  397.       if (name == NULL)
  398.         {
  399.           gsymbol->ada_mangled = 0;
  400.           gsymbol->language_specific.obstack = obstack;
  401.         }
  402.       else
  403.         {
  404.           gsymbol->ada_mangled = 1;
  405.           gsymbol->language_specific.mangled_lang.demangled_name = name;
  406.         }
  407.     }
  408.   else
  409.     gsymbol->language_specific.mangled_lang.demangled_name = name;
  410. }

  411. /* Return the demangled name of GSYMBOL.  */

  412. const char *
  413. symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
  414. {
  415.   if (gsymbol->language == language_ada)
  416.     {
  417.       if (!gsymbol->ada_mangled)
  418.         return NULL;
  419.       /* Fall through.  */
  420.     }

  421.   return gsymbol->language_specific.mangled_lang.demangled_name;
  422. }


  423. /* Initialize the language dependent portion of a symbol
  424.    depending upon the language for the symbol.  */

  425. void
  426. symbol_set_language (struct general_symbol_info *gsymbol,
  427.                      enum language language,
  428.                      struct obstack *obstack)
  429. {
  430.   gsymbol->language = language;
  431.   if (gsymbol->language == language_cplus
  432.       || gsymbol->language == language_d
  433.       || gsymbol->language == language_go
  434.       || gsymbol->language == language_java
  435.       || gsymbol->language == language_objc
  436.       || gsymbol->language == language_fortran)
  437.     {
  438.       symbol_set_demangled_name (gsymbol, NULL, obstack);
  439.     }
  440.   else if (gsymbol->language == language_ada)
  441.     {
  442.       gdb_assert (gsymbol->ada_mangled == 0);
  443.       gsymbol->language_specific.obstack = obstack;
  444.     }
  445.   else
  446.     {
  447.       memset (&gsymbol->language_specific, 0,
  448.               sizeof (gsymbol->language_specific));
  449.     }
  450. }

  451. /* Functions to initialize a symbol's mangled name.  */

  452. /* Objects of this type are stored in the demangled name hash table.  */
  453. struct demangled_name_entry
  454. {
  455.   const char *mangled;
  456.   char demangled[1];
  457. };

  458. /* Hash function for the demangled name hash.  */

  459. static hashval_t
  460. hash_demangled_name_entry (const void *data)
  461. {
  462.   const struct demangled_name_entry *e = data;

  463.   return htab_hash_string (e->mangled);
  464. }

  465. /* Equality function for the demangled name hash.  */

  466. static int
  467. eq_demangled_name_entry (const void *a, const void *b)
  468. {
  469.   const struct demangled_name_entry *da = a;
  470.   const struct demangled_name_entry *db = b;

  471.   return strcmp (da->mangled, db->mangled) == 0;
  472. }

  473. /* Create the hash table used for demangled names.  Each hash entry is
  474.    a pair of strings; one for the mangled name and one for the demangled
  475.    name.  The entry is hashed via just the mangled name.  */

  476. static void
  477. create_demangled_names_hash (struct objfile *objfile)
  478. {
  479.   /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
  480.      The hash table code will round this up to the next prime number.
  481.      Choosing a much larger table size wastes memory, and saves only about
  482.      1% in symbol reading.  */

  483.   objfile->per_bfd->demangled_names_hash = htab_create_alloc
  484.     (256, hash_demangled_name_entry, eq_demangled_name_entry,
  485.      NULL, xcalloc, xfree);
  486. }

  487. /* Try to determine the demangled name for a symbol, based on the
  488.    language of that symbol.  If the language is set to language_auto,
  489.    it will attempt to find any demangling algorithm that works and
  490.    then set the language appropriately.  The returned name is allocated
  491.    by the demangler and should be xfree'd.  */

  492. static char *
  493. symbol_find_demangled_name (struct general_symbol_info *gsymbol,
  494.                             const char *mangled)
  495. {
  496.   char *demangled = NULL;

  497.   if (gsymbol->language == language_unknown)
  498.     gsymbol->language = language_auto;

  499.   if (gsymbol->language == language_objc
  500.       || gsymbol->language == language_auto)
  501.     {
  502.       demangled =
  503.         objc_demangle (mangled, 0);
  504.       if (demangled != NULL)
  505.         {
  506.           gsymbol->language = language_objc;
  507.           return demangled;
  508.         }
  509.     }
  510.   if (gsymbol->language == language_cplus
  511.       || gsymbol->language == language_auto)
  512.     {
  513.       demangled =
  514.         gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
  515.       if (demangled != NULL)
  516.         {
  517.           gsymbol->language = language_cplus;
  518.           return demangled;
  519.         }
  520.     }
  521.   if (gsymbol->language == language_java)
  522.     {
  523.       demangled =
  524.         gdb_demangle (mangled,
  525.                       DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
  526.       if (demangled != NULL)
  527.         {
  528.           gsymbol->language = language_java;
  529.           return demangled;
  530.         }
  531.     }
  532.   if (gsymbol->language == language_d
  533.       || gsymbol->language == language_auto)
  534.     {
  535.       demangled = d_demangle(mangled, 0);
  536.       if (demangled != NULL)
  537.         {
  538.           gsymbol->language = language_d;
  539.           return demangled;
  540.         }
  541.     }
  542.   /* FIXME(dje): Continually adding languages here is clumsy.
  543.      Better to just call la_demangle if !auto, and if auto then call
  544.      a utility routine that tries successive languages in turn and reports
  545.      which one it finds.  I realize the la_demangle options may be different
  546.      for different languages but there's already a FIXME for that.  */
  547.   if (gsymbol->language == language_go
  548.       || gsymbol->language == language_auto)
  549.     {
  550.       demangled = go_demangle (mangled, 0);
  551.       if (demangled != NULL)
  552.         {
  553.           gsymbol->language = language_go;
  554.           return demangled;
  555.         }
  556.     }

  557.   /* We could support `gsymbol->language == language_fortran' here to provide
  558.      module namespaces also for inferiors with only minimal symbol table (ELF
  559.      symbols).  Just the mangling standard is not standardized across compilers
  560.      and there is no DW_AT_producer available for inferiors with only the ELF
  561.      symbols to check the mangling kind.  */

  562.   /* Check for Ada symbols last.  See comment below explaining why.  */

  563.   if (gsymbol->language == language_auto)
  564.    {
  565.      const char *demangled = ada_decode (mangled);

  566.      if (demangled != mangled && demangled != NULL && demangled[0] != '<')
  567.        {
  568.          /* Set the gsymbol language to Ada, but still return NULL.
  569.             Two reasons for that:

  570.               1. For Ada, we prefer computing the symbol's decoded name
  571.                  on the fly rather than pre-compute it, in order to save
  572.                  memory (Ada projects are typically very large).

  573.               2. There are some areas in the definition of the GNAT
  574.                  encoding where, with a bit of bad luck, we might be able
  575.                  to decode a non-Ada symbol, generating an incorrect
  576.                  demangled name (Eg: names ending with "TB" for instance
  577.                  are identified as task bodies and so stripped from
  578.                  the decoded name returned).

  579.                  Returning NULL, here, helps us get a little bit of
  580.                  the best of both worlds.  Because we're last, we should
  581.                  not affect any of the other languages that were able to
  582.                  demangle the symbol before us; we get to correctly tag
  583.                  Ada symbols as such; and even if we incorrectly tagged
  584.                  a non-Ada symbol, which should be rare, any routing
  585.                  through the Ada language should be transparent (Ada
  586.                  tries to behave much like C/C++ with non-Ada symbols).  */
  587.          gsymbol->language = language_ada;
  588.          return NULL;
  589.        }
  590.    }

  591.   return NULL;
  592. }

  593. /* Set both the mangled and demangled (if any) names for GSYMBOL based
  594.    on LINKAGE_NAME and LEN.  Ordinarily, NAME is copied onto the
  595.    objfile's obstack; but if COPY_NAME is 0 and if NAME is
  596.    NUL-terminated, then this function assumes that NAME is already
  597.    correctly saved (either permanently or with a lifetime tied to the
  598.    objfile), and it will not be copied.

  599.    The hash table corresponding to OBJFILE is used, and the memory
  600.    comes from the per-BFD storage_obstack.  LINKAGE_NAME is copied,
  601.    so the pointer can be discarded after calling this function.  */

  602. /* We have to be careful when dealing with Java names: when we run
  603.    into a Java minimal symbol, we don't know it's a Java symbol, so it
  604.    gets demangled as a C++ name.  This is unfortunate, but there's not
  605.    much we can do about it: but when demangling partial symbols and
  606.    regular symbols, we'd better not reuse the wrong demangled name.
  607.    (See PR gdb/1039.)  We solve this by putting a distinctive prefix
  608.    on Java names when storing them in the hash table.  */

  609. /* FIXME: carlton/2003-03-13: This is an unfortunate situation.  I
  610.    don't mind the Java prefix so much: different languages have
  611.    different demangling requirements, so it's only natural that we
  612.    need to keep language data around in our demangling cache.  But
  613.    it's not good that the minimal symbol has the wrong demangled name.
  614.    Unfortunately, I can't think of any easy solution to that
  615.    problem.  */

  616. #define JAVA_PREFIX "##JAVA$$"
  617. #define JAVA_PREFIX_LEN 8

  618. void
  619. symbol_set_names (struct general_symbol_info *gsymbol,
  620.                   const char *linkage_name, int len, int copy_name,
  621.                   struct objfile *objfile)
  622. {
  623.   struct demangled_name_entry **slot;
  624.   /* A 0-terminated copy of the linkage name.  */
  625.   const char *linkage_name_copy;
  626.   /* A copy of the linkage name that might have a special Java prefix
  627.      added to it, for use when looking names up in the hash table.  */
  628.   const char *lookup_name;
  629.   /* The length of lookup_name.  */
  630.   int lookup_len;
  631.   struct demangled_name_entry entry;
  632.   struct objfile_per_bfd_storage *per_bfd = objfile->per_bfd;

  633.   if (gsymbol->language == language_ada)
  634.     {
  635.       /* In Ada, we do the symbol lookups using the mangled name, so
  636.          we can save some space by not storing the demangled name.

  637.          As a side note, we have also observed some overlap between
  638.          the C++ mangling and Ada mangling, similarly to what has
  639.          been observed with Java.  Because we don't store the demangled
  640.          name with the symbol, we don't need to use the same trick
  641.          as Java.  */
  642.       if (!copy_name)
  643.         gsymbol->name = linkage_name;
  644.       else
  645.         {
  646.           char *name = obstack_alloc (&per_bfd->storage_obstack, len + 1);

  647.           memcpy (name, linkage_name, len);
  648.           name[len] = '\0';
  649.           gsymbol->name = name;
  650.         }
  651.       symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);

  652.       return;
  653.     }

  654.   if (per_bfd->demangled_names_hash == NULL)
  655.     create_demangled_names_hash (objfile);

  656.   /* The stabs reader generally provides names that are not
  657.      NUL-terminated; most of the other readers don't do this, so we
  658.      can just use the given copy, unless we're in the Java case.  */
  659.   if (gsymbol->language == language_java)
  660.     {
  661.       char *alloc_name;

  662.       lookup_len = len + JAVA_PREFIX_LEN;
  663.       alloc_name = alloca (lookup_len + 1);
  664.       memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
  665.       memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
  666.       alloc_name[lookup_len] = '\0';

  667.       lookup_name = alloc_name;
  668.       linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
  669.     }
  670.   else if (linkage_name[len] != '\0')
  671.     {
  672.       char *alloc_name;

  673.       lookup_len = len;
  674.       alloc_name = alloca (lookup_len + 1);
  675.       memcpy (alloc_name, linkage_name, len);
  676.       alloc_name[lookup_len] = '\0';

  677.       lookup_name = alloc_name;
  678.       linkage_name_copy = alloc_name;
  679.     }
  680.   else
  681.     {
  682.       lookup_len = len;
  683.       lookup_name = linkage_name;
  684.       linkage_name_copy = linkage_name;
  685.     }

  686.   entry.mangled = lookup_name;
  687.   slot = ((struct demangled_name_entry **)
  688.           htab_find_slot (per_bfd->demangled_names_hash,
  689.                           &entry, INSERT));

  690.   /* If this name is not in the hash table, add it.  */
  691.   if (*slot == NULL
  692.       /* A C version of the symbol may have already snuck into the table.
  693.          This happens to, e.g., main.init (__go_init_main).  Cope.  */
  694.       || (gsymbol->language == language_go
  695.           && (*slot)->demangled[0] == '\0'))
  696.     {
  697.       char *demangled_name = symbol_find_demangled_name (gsymbol,
  698.                                                          linkage_name_copy);
  699.       int demangled_len = demangled_name ? strlen (demangled_name) : 0;

  700.       /* Suppose we have demangled_name==NULL, copy_name==0, and
  701.          lookup_name==linkage_name.  In this case, we already have the
  702.          mangled name saved, and we don't have a demangled name.  So,
  703.          you might think we could save a little space by not recording
  704.          this in the hash table at all.

  705.          It turns out that it is actually important to still save such
  706.          an entry in the hash table, because storing this name gives
  707.          us better bcache hit rates for partial symbols.  */
  708.       if (!copy_name && lookup_name == linkage_name)
  709.         {
  710.           *slot = obstack_alloc (&per_bfd->storage_obstack,
  711.                                  offsetof (struct demangled_name_entry,
  712.                                            demangled)
  713.                                  + demangled_len + 1);
  714.           (*slot)->mangled = lookup_name;
  715.         }
  716.       else
  717.         {
  718.           char *mangled_ptr;

  719.           /* If we must copy the mangled name, put it directly after
  720.              the demangled name so we can have a single
  721.              allocation.  */
  722.           *slot = obstack_alloc (&per_bfd->storage_obstack,
  723.                                  offsetof (struct demangled_name_entry,
  724.                                            demangled)
  725.                                  + lookup_len + demangled_len + 2);
  726.           mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
  727.           strcpy (mangled_ptr, lookup_name);
  728.           (*slot)->mangled = mangled_ptr;
  729.         }

  730.       if (demangled_name != NULL)
  731.         {
  732.           strcpy ((*slot)->demangled, demangled_name);
  733.           xfree (demangled_name);
  734.         }
  735.       else
  736.         (*slot)->demangled[0] = '\0';
  737.     }

  738.   gsymbol->name = (*slot)->mangled + lookup_len - len;
  739.   if ((*slot)->demangled[0] != '\0')
  740.     symbol_set_demangled_name (gsymbol, (*slot)->demangled,
  741.                                &per_bfd->storage_obstack);
  742.   else
  743.     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
  744. }

  745. /* Return the source code name of a symbol.  In languages where
  746.    demangling is necessary, this is the demangled name.  */

  747. const char *
  748. symbol_natural_name (const struct general_symbol_info *gsymbol)
  749. {
  750.   switch (gsymbol->language)
  751.     {
  752.     case language_cplus:
  753.     case language_d:
  754.     case language_go:
  755.     case language_java:
  756.     case language_objc:
  757.     case language_fortran:
  758.       if (symbol_get_demangled_name (gsymbol) != NULL)
  759.         return symbol_get_demangled_name (gsymbol);
  760.       break;
  761.     case language_ada:
  762.       return ada_decode_symbol (gsymbol);
  763.     default:
  764.       break;
  765.     }
  766.   return gsymbol->name;
  767. }

  768. /* Return the demangled name for a symbol based on the language for
  769.    that symbol.  If no demangled name exists, return NULL.  */

  770. const char *
  771. symbol_demangled_name (const struct general_symbol_info *gsymbol)
  772. {
  773.   const char *dem_name = NULL;

  774.   switch (gsymbol->language)
  775.     {
  776.     case language_cplus:
  777.     case language_d:
  778.     case language_go:
  779.     case language_java:
  780.     case language_objc:
  781.     case language_fortran:
  782.       dem_name = symbol_get_demangled_name (gsymbol);
  783.       break;
  784.     case language_ada:
  785.       dem_name = ada_decode_symbol (gsymbol);
  786.       break;
  787.     default:
  788.       break;
  789.     }
  790.   return dem_name;
  791. }

  792. /* Return the search name of a symbol---generally the demangled or
  793.    linkage name of the symbol, depending on how it will be searched for.
  794.    If there is no distinct demangled name, then returns the same value
  795.    (same pointer) as SYMBOL_LINKAGE_NAME.  */

  796. const char *
  797. symbol_search_name (const struct general_symbol_info *gsymbol)
  798. {
  799.   if (gsymbol->language == language_ada)
  800.     return gsymbol->name;
  801.   else
  802.     return symbol_natural_name (gsymbol);
  803. }

  804. /* Initialize the structure fields to zero values.  */

  805. void
  806. init_sal (struct symtab_and_line *sal)
  807. {
  808.   memset (sal, 0, sizeof (*sal));
  809. }


  810. /* Return 1 if the two sections are the same, or if they could
  811.    plausibly be copies of each other, one in an original object
  812.    file and another in a separated debug file.  */

  813. int
  814. matching_obj_sections (struct obj_section *obj_first,
  815.                        struct obj_section *obj_second)
  816. {
  817.   asection *first = obj_first? obj_first->the_bfd_section : NULL;
  818.   asection *second = obj_second? obj_second->the_bfd_section : NULL;
  819.   struct objfile *obj;

  820.   /* If they're the same section, then they match.  */
  821.   if (first == second)
  822.     return 1;

  823.   /* If either is NULL, give up.  */
  824.   if (first == NULL || second == NULL)
  825.     return 0;

  826.   /* This doesn't apply to absolute symbols.  */
  827.   if (first->owner == NULL || second->owner == NULL)
  828.     return 0;

  829.   /* If they're in the same object file, they must be different sections.  */
  830.   if (first->owner == second->owner)
  831.     return 0;

  832.   /* Check whether the two sections are potentially corresponding.  They must
  833.      have the same size, address, and name.  We can't compare section indexes,
  834.      which would be more reliable, because some sections may have been
  835.      stripped.  */
  836.   if (bfd_get_section_size (first) != bfd_get_section_size (second))
  837.     return 0;

  838.   /* In-memory addresses may start at a different offset, relativize them.  */
  839.   if (bfd_get_section_vma (first->owner, first)
  840.       - bfd_get_start_address (first->owner)
  841.       != bfd_get_section_vma (second->owner, second)
  842.          - bfd_get_start_address (second->owner))
  843.     return 0;

  844.   if (bfd_get_section_name (first->owner, first) == NULL
  845.       || bfd_get_section_name (second->owner, second) == NULL
  846.       || strcmp (bfd_get_section_name (first->owner, first),
  847.                  bfd_get_section_name (second->owner, second)) != 0)
  848.     return 0;

  849.   /* Otherwise check that they are in corresponding objfiles.  */

  850.   ALL_OBJFILES (obj)
  851.     if (obj->obfd == first->owner)
  852.       break;
  853.   gdb_assert (obj != NULL);

  854.   if (obj->separate_debug_objfile != NULL
  855.       && obj->separate_debug_objfile->obfd == second->owner)
  856.     return 1;
  857.   if (obj->separate_debug_objfile_backlink != NULL
  858.       && obj->separate_debug_objfile_backlink->obfd == second->owner)
  859.     return 1;

  860.   return 0;
  861. }

  862. /* See symtab.h.  */

  863. void
  864. expand_symtab_containing_pc (CORE_ADDR pc, struct obj_section *section)
  865. {
  866.   struct objfile *objfile;
  867.   struct bound_minimal_symbol msymbol;

  868.   /* If we know that this is not a text address, return failure.  This is
  869.      necessary because we loop based on texthigh and textlow, which do
  870.      not include the data ranges.  */
  871.   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
  872.   if (msymbol.minsym
  873.       && (MSYMBOL_TYPE (msymbol.minsym) == mst_data
  874.           || MSYMBOL_TYPE (msymbol.minsym) == mst_bss
  875.           || MSYMBOL_TYPE (msymbol.minsym) == mst_abs
  876.           || MSYMBOL_TYPE (msymbol.minsym) == mst_file_data
  877.           || MSYMBOL_TYPE (msymbol.minsym) == mst_file_bss))
  878.     return;

  879.   ALL_OBJFILES (objfile)
  880.   {
  881.     struct compunit_symtab *cust = NULL;

  882.     if (objfile->sf)
  883.       cust = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile, msymbol,
  884.                                                             pc, section, 0);
  885.     if (cust)
  886.       return;
  887.   }
  888. }

  889. /* Debug symbols usually don't have section information.  We need to dig that
  890.    out of the minimal symbols and stash that in the debug symbol.  */

  891. void
  892. fixup_section (struct general_symbol_info *ginfo,
  893.                CORE_ADDR addr, struct objfile *objfile)
  894. {
  895.   struct minimal_symbol *msym;

  896.   /* First, check whether a minimal symbol with the same name exists
  897.      and points to the same address.  The address check is required
  898.      e.g. on PowerPC64, where the minimal symbol for a function will
  899.      point to the function descriptor, while the debug symbol will
  900.      point to the actual function code.  */
  901.   msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->name, objfile);
  902.   if (msym)
  903.     ginfo->section = MSYMBOL_SECTION (msym);
  904.   else
  905.     {
  906.       /* Static, function-local variables do appear in the linker
  907.          (minimal) symbols, but are frequently given names that won't
  908.          be found via lookup_minimal_symbol().  E.g., it has been
  909.          observed in frv-uclinux (ELF) executables that a static,
  910.          function-local variable named "foo" might appear in the
  911.          linker symbols as "foo.6" or "foo.3".  Thus, there is no
  912.          point in attempting to extend the lookup-by-name mechanism to
  913.          handle this case due to the fact that there can be multiple
  914.          names.

  915.          So, instead, search the section table when lookup by name has
  916.          failed.  The ``addr'' and ``endaddr'' fields may have already
  917.          been relocated.  If so, the relocation offset (i.e. the
  918.          ANOFFSET value) needs to be subtracted from these values when
  919.          performing the comparison.  We unconditionally subtract it,
  920.          because, when no relocation has been performed, the ANOFFSET
  921.          value will simply be zero.

  922.          The address of the symbol whose section we're fixing up HAS
  923.          NOT BEEN adjusted (relocated) yet.  It can't have been since
  924.          the section isn't yet known and knowing the section is
  925.          necessary in order to add the correct relocation value.  In
  926.          other words, we wouldn't even be in this function (attempting
  927.          to compute the section) if it were already known.

  928.          Note that it is possible to search the minimal symbols
  929.          (subtracting the relocation value if necessary) to find the
  930.          matching minimal symbol, but this is overkill and much less
  931.          efficient.  It is not necessary to find the matching minimal
  932.          symbol, only its section.

  933.          Note that this technique (of doing a section table search)
  934.          can fail when unrelocated section addresses overlap.  For
  935.          this reason, we still attempt a lookup by name prior to doing
  936.          a search of the section table.  */

  937.       struct obj_section *s;
  938.       int fallback = -1;

  939.       ALL_OBJFILE_OSECTIONS (objfile, s)
  940.         {
  941.           int idx = s - objfile->sections;
  942.           CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);

  943.           if (fallback == -1)
  944.             fallback = idx;

  945.           if (obj_section_addr (s) - offset <= addr
  946.               && addr < obj_section_endaddr (s) - offset)
  947.             {
  948.               ginfo->section = idx;
  949.               return;
  950.             }
  951.         }

  952.       /* If we didn't find the section, assume it is in the first
  953.          section.  If there is no allocated section, then it hardly
  954.          matters what we pick, so just pick zero.  */
  955.       if (fallback == -1)
  956.         ginfo->section = 0;
  957.       else
  958.         ginfo->section = fallback;
  959.     }
  960. }

  961. struct symbol *
  962. fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
  963. {
  964.   CORE_ADDR addr;

  965.   if (!sym)
  966.     return NULL;

  967.   if (!SYMBOL_OBJFILE_OWNED (sym))
  968.     return sym;

  969.   /* We either have an OBJFILE, or we can get at it from the sym's
  970.      symtab.  Anything else is a bug.  */
  971.   gdb_assert (objfile || symbol_symtab (sym));

  972.   if (objfile == NULL)
  973.     objfile = symbol_objfile (sym);

  974.   if (SYMBOL_OBJ_SECTION (objfile, sym))
  975.     return sym;

  976.   /* We should have an objfile by now.  */
  977.   gdb_assert (objfile);

  978.   switch (SYMBOL_CLASS (sym))
  979.     {
  980.     case LOC_STATIC:
  981.     case LOC_LABEL:
  982.       addr = SYMBOL_VALUE_ADDRESS (sym);
  983.       break;
  984.     case LOC_BLOCK:
  985.       addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  986.       break;

  987.     default:
  988.       /* Nothing else will be listed in the minsyms -- no use looking
  989.          it up.  */
  990.       return sym;
  991.     }

  992.   fixup_section (&sym->ginfo, addr, objfile);

  993.   return sym;
  994. }

  995. /* Compute the demangled form of NAME as used by the various symbol
  996.    lookup functions.  The result is stored in *RESULT_NAME.  Returns a
  997.    cleanup which can be used to clean up the result.

  998.    For Ada, this function just sets *RESULT_NAME to NAME, unmodified.
  999.    Normally, Ada symbol lookups are performed using the encoded name
  1000.    rather than the demangled name, and so it might seem to make sense
  1001.    for this function to return an encoded version of NAME.
  1002.    Unfortunately, we cannot do this, because this function is used in
  1003.    circumstances where it is not appropriate to try to encode NAME.
  1004.    For instance, when displaying the frame info, we demangle the name
  1005.    of each parameter, and then perform a symbol lookup inside our
  1006.    function using that demangled name.  In Ada, certain functions
  1007.    have internally-generated parameters whose name contain uppercase
  1008.    characters.  Encoding those name would result in those uppercase
  1009.    characters to become lowercase, and thus cause the symbol lookup
  1010.    to fail.  */

  1011. struct cleanup *
  1012. demangle_for_lookup (const char *name, enum language lang,
  1013.                      const char **result_name)
  1014. {
  1015.   char *demangled_name = NULL;
  1016.   const char *modified_name = NULL;
  1017.   struct cleanup *cleanup = make_cleanup (null_cleanup, 0);

  1018.   modified_name = name;

  1019.   /* If we are using C++, D, Go, or Java, demangle the name before doing a
  1020.      lookup, so we can always binary search.  */
  1021.   if (lang == language_cplus)
  1022.     {
  1023.       demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
  1024.       if (demangled_name)
  1025.         {
  1026.           modified_name = demangled_name;
  1027.           make_cleanup (xfree, demangled_name);
  1028.         }
  1029.       else
  1030.         {
  1031.           /* If we were given a non-mangled name, canonicalize it
  1032.              according to the language (so far only for C++).  */
  1033.           demangled_name = cp_canonicalize_string (name);
  1034.           if (demangled_name)
  1035.             {
  1036.               modified_name = demangled_name;
  1037.               make_cleanup (xfree, demangled_name);
  1038.             }
  1039.         }
  1040.     }
  1041.   else if (lang == language_java)
  1042.     {
  1043.       demangled_name = gdb_demangle (name,
  1044.                                      DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
  1045.       if (demangled_name)
  1046.         {
  1047.           modified_name = demangled_name;
  1048.           make_cleanup (xfree, demangled_name);
  1049.         }
  1050.     }
  1051.   else if (lang == language_d)
  1052.     {
  1053.       demangled_name = d_demangle (name, 0);
  1054.       if (demangled_name)
  1055.         {
  1056.           modified_name = demangled_name;
  1057.           make_cleanup (xfree, demangled_name);
  1058.         }
  1059.     }
  1060.   else if (lang == language_go)
  1061.     {
  1062.       demangled_name = go_demangle (name, 0);
  1063.       if (demangled_name)
  1064.         {
  1065.           modified_name = demangled_name;
  1066.           make_cleanup (xfree, demangled_name);
  1067.         }
  1068.     }

  1069.   *result_name = modified_name;
  1070.   return cleanup;
  1071. }

  1072. /* See symtab.h.

  1073.    This function (or rather its subordinates) have a bunch of loops and
  1074.    it would seem to be attractive to put in some QUIT's (though I'm not really
  1075.    sure whether it can run long enough to be really important).  But there
  1076.    are a few calls for which it would appear to be bad news to quit
  1077.    out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c.  (Note
  1078.    that there is C++ code below which can error(), but that probably
  1079.    doesn't affect these calls since they are looking for a known
  1080.    variable and thus can probably assume it will never hit the C++
  1081.    code).  */

  1082. struct symbol *
  1083. lookup_symbol_in_language (const char *name, const struct block *block,
  1084.                            const domain_enum domain, enum language lang,
  1085.                            struct field_of_this_result *is_a_field_of_this)
  1086. {
  1087.   const char *modified_name;
  1088.   struct symbol *returnval;
  1089.   struct cleanup *cleanup = demangle_for_lookup (name, lang, &modified_name);

  1090.   returnval = lookup_symbol_aux (modified_name, block, domain, lang,
  1091.                                  is_a_field_of_this);
  1092.   do_cleanups (cleanup);

  1093.   return returnval;
  1094. }

  1095. /* See symtab.h.  */

  1096. struct symbol *
  1097. lookup_symbol (const char *name, const struct block *block,
  1098.                domain_enum domain,
  1099.                struct field_of_this_result *is_a_field_of_this)
  1100. {
  1101.   return lookup_symbol_in_language (name, block, domain,
  1102.                                     current_language->la_language,
  1103.                                     is_a_field_of_this);
  1104. }

  1105. /* See symtab.h.  */

  1106. struct symbol *
  1107. lookup_language_this (const struct language_defn *lang,
  1108.                       const struct block *block)
  1109. {
  1110.   if (lang->la_name_of_this == NULL || block == NULL)
  1111.     return NULL;

  1112.   if (symbol_lookup_debug > 1)
  1113.     {
  1114.       struct objfile *objfile = lookup_objfile_from_block (block);

  1115.       fprintf_unfiltered (gdb_stdlog,
  1116.                           "lookup_language_this (%s, %s (objfile %s))",
  1117.                           lang->la_name, host_address_to_string (block),
  1118.                           objfile_debug_name (objfile));
  1119.     }

  1120.   while (block)
  1121.     {
  1122.       struct symbol *sym;

  1123.       sym = block_lookup_symbol (block, lang->la_name_of_this, VAR_DOMAIN);
  1124.       if (sym != NULL)
  1125.         {
  1126.           if (symbol_lookup_debug > 1)
  1127.             {
  1128.               fprintf_unfiltered (gdb_stdlog, " = %s (%s, block %s)\n",
  1129.                                   SYMBOL_PRINT_NAME (sym),
  1130.                                   host_address_to_string (sym),
  1131.                                   host_address_to_string (block));
  1132.             }
  1133.           block_found = block;
  1134.           return sym;
  1135.         }
  1136.       if (BLOCK_FUNCTION (block))
  1137.         break;
  1138.       block = BLOCK_SUPERBLOCK (block);
  1139.     }

  1140.   if (symbol_lookup_debug > 1)
  1141.     fprintf_unfiltered (gdb_stdlog, " = NULL\n");
  1142.   return NULL;
  1143. }

  1144. /* Given TYPE, a structure/union,
  1145.    return 1 if the component named NAME from the ultimate target
  1146.    structure/union is defined, otherwise, return 0.  */

  1147. static int
  1148. check_field (struct type *type, const char *name,
  1149.              struct field_of_this_result *is_a_field_of_this)
  1150. {
  1151.   int i;

  1152.   /* The type may be a stub.  */
  1153.   CHECK_TYPEDEF (type);

  1154.   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
  1155.     {
  1156.       const char *t_field_name = TYPE_FIELD_NAME (type, i);

  1157.       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
  1158.         {
  1159.           is_a_field_of_this->type = type;
  1160.           is_a_field_of_this->field = &TYPE_FIELD (type, i);
  1161.           return 1;
  1162.         }
  1163.     }

  1164.   /* C++: If it was not found as a data field, then try to return it
  1165.      as a pointer to a method.  */

  1166.   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
  1167.     {
  1168.       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
  1169.         {
  1170.           is_a_field_of_this->type = type;
  1171.           is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
  1172.           return 1;
  1173.         }
  1174.     }

  1175.   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
  1176.     if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
  1177.       return 1;

  1178.   return 0;
  1179. }

  1180. /* Behave like lookup_symbol except that NAME is the natural name
  1181.    (e.g., demangled name) of the symbol that we're looking for.  */

  1182. static struct symbol *
  1183. lookup_symbol_aux (const char *name, const struct block *block,
  1184.                    const domain_enum domain, enum language language,
  1185.                    struct field_of_this_result *is_a_field_of_this)
  1186. {
  1187.   struct symbol *sym;
  1188.   const struct language_defn *langdef;

  1189.   if (symbol_lookup_debug)
  1190.     {
  1191.       struct objfile *objfile = lookup_objfile_from_block (block);

  1192.       fprintf_unfiltered (gdb_stdlog,
  1193.                           "lookup_symbol_aux (%s, %s (objfile %s), %s, %s)\n",
  1194.                           name, host_address_to_string (block),
  1195.                           objfile != NULL
  1196.                           ? objfile_debug_name (objfile) : "NULL",
  1197.                           domain_name (domain), language_str (language));
  1198.     }

  1199.   /* Initialize block_found so that the language la_lookup_symbol_nonlocal
  1200.      routines don't have to set it (to NULL) if a primitive type is found.
  1201.      We do this early so that block_found is also NULL if no symbol is
  1202.      found (though this is not part of the API, and callers cannot assume
  1203.      this).  */
  1204.   block_found = NULL;

  1205.   /* Make sure we do something sensible with is_a_field_of_this, since
  1206.      the callers that set this parameter to some non-null value will
  1207.      certainly use it later.  If we don't set it, the contents of
  1208.      is_a_field_of_this are undefined.  */
  1209.   if (is_a_field_of_this != NULL)
  1210.     memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));

  1211.   /* Search specified block and its superiors.  Don't search
  1212.      STATIC_BLOCK or GLOBAL_BLOCK.  */

  1213.   sym = lookup_local_symbol (name, block, domain, language);
  1214.   if (sym != NULL)
  1215.     {
  1216.       if (symbol_lookup_debug)
  1217.         {
  1218.           fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1219.                               host_address_to_string (sym));
  1220.         }
  1221.       return sym;
  1222.     }

  1223.   /* If requested to do so by the caller and if appropriate for LANGUAGE,
  1224.      check to see if NAME is a field of `this'.  */

  1225.   langdef = language_def (language);

  1226.   /* Don't do this check if we are searching for a struct.  It will
  1227.      not be found by check_field, but will be found by other
  1228.      means.  */
  1229.   if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
  1230.     {
  1231.       struct symbol *sym = lookup_language_this (langdef, block);

  1232.       if (sym)
  1233.         {
  1234.           struct type *t = sym->type;

  1235.           /* I'm not really sure that type of this can ever
  1236.              be typedefed; just be safe.  */
  1237.           CHECK_TYPEDEF (t);
  1238.           if (TYPE_CODE (t) == TYPE_CODE_PTR
  1239.               || TYPE_CODE (t) == TYPE_CODE_REF)
  1240.             t = TYPE_TARGET_TYPE (t);

  1241.           if (TYPE_CODE (t) != TYPE_CODE_STRUCT
  1242.               && TYPE_CODE (t) != TYPE_CODE_UNION)
  1243.             error (_("Internal error: `%s' is not an aggregate"),
  1244.                    langdef->la_name_of_this);

  1245.           if (check_field (t, name, is_a_field_of_this))
  1246.             {
  1247.               if (symbol_lookup_debug)
  1248.                 {
  1249.                   fprintf_unfiltered (gdb_stdlog,
  1250.                                       "lookup_symbol_aux (...) = NULL\n");
  1251.                 }
  1252.               return NULL;
  1253.             }
  1254.         }
  1255.     }

  1256.   /* Now do whatever is appropriate for LANGUAGE to look
  1257.      up static and global variables.  */

  1258.   sym = langdef->la_lookup_symbol_nonlocal (langdef, name, block, domain);
  1259.   if (sym != NULL)
  1260.     {
  1261.       if (symbol_lookup_debug)
  1262.         {
  1263.           fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1264.                               host_address_to_string (sym));
  1265.         }
  1266.       return sym;
  1267.     }

  1268.   /* Now search all static file-level symbols.  Not strictly correct,
  1269.      but more useful than an error.  */

  1270.   sym = lookup_static_symbol (name, domain);
  1271.   if (symbol_lookup_debug)
  1272.     {
  1273.       fprintf_unfiltered (gdb_stdlog, "lookup_symbol_aux (...) = %s\n",
  1274.                           sym != NULL ? host_address_to_string (sym) : "NULL");
  1275.     }
  1276.   return sym;
  1277. }

  1278. /* Check to see if the symbol is defined in BLOCK or its superiors.
  1279.    Don't search STATIC_BLOCK or GLOBAL_BLOCK.  */

  1280. static struct symbol *
  1281. lookup_local_symbol (const char *name, const struct block *block,
  1282.                      const domain_enum domain,
  1283.                      enum language language)
  1284. {
  1285.   struct symbol *sym;
  1286.   const struct block *static_block = block_static_block (block);
  1287.   const char *scope = block_scope (block);

  1288.   /* Check if either no block is specified or it's a global block.  */

  1289.   if (static_block == NULL)
  1290.     return NULL;

  1291.   while (block != static_block)
  1292.     {
  1293.       sym = lookup_symbol_in_block (name, block, domain);
  1294.       if (sym != NULL)
  1295.         return sym;

  1296.       if (language == language_cplus || language == language_fortran)
  1297.         {
  1298.           sym = cp_lookup_symbol_imports_or_template (scope, name, block,
  1299.                                                       domain);
  1300.           if (sym != NULL)
  1301.             return sym;
  1302.         }

  1303.       if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
  1304.         break;
  1305.       block = BLOCK_SUPERBLOCK (block);
  1306.     }

  1307.   /* We've reached the end of the function without finding a result.  */

  1308.   return NULL;
  1309. }

  1310. /* See symtab.h.  */

  1311. struct objfile *
  1312. lookup_objfile_from_block (const struct block *block)
  1313. {
  1314.   struct objfile *obj;
  1315.   struct compunit_symtab *cust;

  1316.   if (block == NULL)
  1317.     return NULL;

  1318.   block = block_global_block (block);
  1319.   /* Look through all blockvectors.  */
  1320.   ALL_COMPUNITS (obj, cust)
  1321.     if (block == BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust),
  1322.                                     GLOBAL_BLOCK))
  1323.       {
  1324.         if (obj->separate_debug_objfile_backlink)
  1325.           obj = obj->separate_debug_objfile_backlink;

  1326.         return obj;
  1327.       }

  1328.   return NULL;
  1329. }

  1330. /* See symtab.h.  */

  1331. struct symbol *
  1332. lookup_symbol_in_block (const char *name, const struct block *block,
  1333.                         const domain_enum domain)
  1334. {
  1335.   struct symbol *sym;

  1336.   if (symbol_lookup_debug > 1)
  1337.     {
  1338.       struct objfile *objfile = lookup_objfile_from_block (block);

  1339.       fprintf_unfiltered (gdb_stdlog,
  1340.                           "lookup_symbol_in_block (%s, %s (objfile %s), %s)",
  1341.                           name, host_address_to_string (block),
  1342.                           objfile_debug_name (objfile),
  1343.                           domain_name (domain));
  1344.     }

  1345.   sym = block_lookup_symbol (block, name, domain);
  1346.   if (sym)
  1347.     {
  1348.       if (symbol_lookup_debug > 1)
  1349.         {
  1350.           fprintf_unfiltered (gdb_stdlog, " = %s\n",
  1351.                               host_address_to_string (sym));
  1352.         }
  1353.       block_found = block;
  1354.       return fixup_symbol_section (sym, NULL);
  1355.     }

  1356.   if (symbol_lookup_debug > 1)
  1357.     fprintf_unfiltered (gdb_stdlog, " = NULL\n");
  1358.   return NULL;
  1359. }

  1360. /* See symtab.h.  */

  1361. struct symbol *
  1362. lookup_global_symbol_from_objfile (struct objfile *main_objfile,
  1363.                                    const char *name,
  1364.                                    const domain_enum domain)
  1365. {
  1366.   struct objfile *objfile;

  1367.   for (objfile = main_objfile;
  1368.        objfile;
  1369.        objfile = objfile_separate_debug_iterate (main_objfile, objfile))
  1370.     {
  1371.       struct symbol *sym = lookup_symbol_in_objfile (objfile, GLOBAL_BLOCK,
  1372.                                                      name, domain);

  1373.       if (sym != NULL)
  1374.         return sym;
  1375.     }

  1376.   return NULL;
  1377. }

  1378. /* Check to see if the symbol is defined in one of the OBJFILE's
  1379.    symtabs.  BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
  1380.    depending on whether or not we want to search global symbols or
  1381.    static symbols.  */

  1382. static struct symbol *
  1383. lookup_symbol_in_objfile_symtabs (struct objfile *objfile, int block_index,
  1384.                                   const char *name, const domain_enum domain)
  1385. {
  1386.   struct compunit_symtab *cust;

  1387.   gdb_assert (block_index == GLOBAL_BLOCK || block_index == STATIC_BLOCK);

  1388.   if (symbol_lookup_debug > 1)
  1389.     {
  1390.       fprintf_unfiltered (gdb_stdlog,
  1391.                           "lookup_symbol_in_objfile_symtabs (%s, %s, %s, %s)",
  1392.                           objfile_debug_name (objfile),
  1393.                           block_index == GLOBAL_BLOCK
  1394.                           ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  1395.                           name, domain_name (domain));
  1396.     }

  1397.   ALL_OBJFILE_COMPUNITS (objfile, cust)
  1398.     {
  1399.       const struct blockvector *bv;
  1400.       const struct block *block;
  1401.       struct symbol *sym;

  1402.       bv = COMPUNIT_BLOCKVECTOR (cust);
  1403.       block = BLOCKVECTOR_BLOCK (bv, block_index);
  1404.       sym = block_lookup_symbol_primary (block, name, domain);
  1405.       if (sym)
  1406.         {
  1407.           if (symbol_lookup_debug > 1)
  1408.             {
  1409.               fprintf_unfiltered (gdb_stdlog, " = %s (block %s)\n",
  1410.                                   host_address_to_string (sym),
  1411.                                   host_address_to_string (block));
  1412.             }
  1413.           block_found = block;
  1414.           return fixup_symbol_section (sym, objfile);
  1415.         }
  1416.     }

  1417.   if (symbol_lookup_debug > 1)
  1418.     fprintf_unfiltered (gdb_stdlog, " = NULL\n");
  1419.   return NULL;
  1420. }

  1421. /* Wrapper around lookup_symbol_in_objfile_symtabs for search_symbols.
  1422.    Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
  1423.    and all associated separate debug objfiles.

  1424.    Normally we only look in OBJFILE, and not any separate debug objfiles
  1425.    because the outer loop will cause them to be searched too.  This case is
  1426.    different.  Here we're called from search_symbols where it will only
  1427.    call us for the the objfile that contains a matching minsym.  */

  1428. static struct symbol *
  1429. lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
  1430.                                             const char *linkage_name,
  1431.                                             domain_enum domain)
  1432. {
  1433.   enum language lang = current_language->la_language;
  1434.   const char *modified_name;
  1435.   struct cleanup *cleanup = demangle_for_lookup (linkage_name, lang,
  1436.                                                  &modified_name);
  1437.   struct objfile *main_objfile, *cur_objfile;

  1438.   if (objfile->separate_debug_objfile_backlink)
  1439.     main_objfile = objfile->separate_debug_objfile_backlink;
  1440.   else
  1441.     main_objfile = objfile;

  1442.   for (cur_objfile = main_objfile;
  1443.        cur_objfile;
  1444.        cur_objfile = objfile_separate_debug_iterate (main_objfile, cur_objfile))
  1445.     {
  1446.       struct symbol *sym;

  1447.       sym = lookup_symbol_in_objfile_symtabs (cur_objfile, GLOBAL_BLOCK,
  1448.                                               modified_name, domain);
  1449.       if (sym == NULL)
  1450.         sym = lookup_symbol_in_objfile_symtabs (cur_objfile, STATIC_BLOCK,
  1451.                                                 modified_name, domain);
  1452.       if (sym != NULL)
  1453.         {
  1454.           do_cleanups (cleanup);
  1455.           return sym;
  1456.         }
  1457.     }

  1458.   do_cleanups (cleanup);
  1459.   return NULL;
  1460. }

  1461. /* A helper function that throws an exception when a symbol was found
  1462.    in a psymtab but not in a symtab.  */

  1463. static void ATTRIBUTE_NORETURN
  1464. error_in_psymtab_expansion (int block_index, const char *name,
  1465.                             struct compunit_symtab *cust)
  1466. {
  1467.   error (_("\
  1468. Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
  1469. %s may be an inlined function, or may be a template function\n         \
  1470. (if a template, try specifying an instantiation: %s<type>)."),
  1471.          block_index == GLOBAL_BLOCK ? "global" : "static",
  1472.          name,
  1473.          symtab_to_filename_for_display (compunit_primary_filetab (cust)),
  1474.          name, name);
  1475. }

  1476. /* A helper function for various lookup routines that interfaces with
  1477.    the "quick" symbol table functions.  */

  1478. static struct symbol *
  1479. lookup_symbol_via_quick_fns (struct objfile *objfile, int block_index,
  1480.                              const char *name, const domain_enum domain)
  1481. {
  1482.   struct compunit_symtab *cust;
  1483.   const struct blockvector *bv;
  1484.   const struct block *block;
  1485.   struct symbol *sym;

  1486.   if (!objfile->sf)
  1487.     return NULL;

  1488.   if (symbol_lookup_debug > 1)
  1489.     {
  1490.       fprintf_unfiltered (gdb_stdlog,
  1491.                           "lookup_symbol_via_quick_fns (%s, %s, %s, %s)\n",
  1492.                           objfile_debug_name (objfile),
  1493.                           block_index == GLOBAL_BLOCK
  1494.                           ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  1495.                           name, domain_name (domain));
  1496.     }

  1497.   cust = objfile->sf->qf->lookup_symbol (objfile, block_index, name, domain);
  1498.   if (cust == NULL)
  1499.     {
  1500.       if (symbol_lookup_debug > 1)
  1501.         {
  1502.           fprintf_unfiltered (gdb_stdlog,
  1503.                               "lookup_symbol_via_quick_fns (...) = NULL\n");
  1504.         }
  1505.       return NULL;
  1506.     }

  1507.   bv = COMPUNIT_BLOCKVECTOR (cust);
  1508.   block = BLOCKVECTOR_BLOCK (bv, block_index);
  1509.   sym = block_lookup_symbol (block, name, domain);
  1510.   if (!sym)
  1511.     error_in_psymtab_expansion (block_index, name, cust);

  1512.   if (symbol_lookup_debug > 1)
  1513.     {
  1514.       fprintf_unfiltered (gdb_stdlog,
  1515.                           "lookup_symbol_via_quick_fns (...) = %s (block %s)\n",
  1516.                           host_address_to_string (sym),
  1517.                           host_address_to_string (block));
  1518.     }

  1519.   block_found = block;
  1520.   return fixup_symbol_section (sym, objfile);
  1521. }

  1522. /* See symtab.h.  */

  1523. struct symbol *
  1524. basic_lookup_symbol_nonlocal (const struct language_defn *langdef,
  1525.                               const char *name,
  1526.                               const struct block *block,
  1527.                               const domain_enum domain)
  1528. {
  1529.   struct symbol *sym;

  1530.   /* NOTE: carlton/2003-05-19: The comments below were written when
  1531.      this (or what turned into this) was part of lookup_symbol_aux;
  1532.      I'm much less worried about these questions now, since these
  1533.      decisions have turned out well, but I leave these comments here
  1534.      for posterity.  */

  1535.   /* NOTE: carlton/2002-12-05: There is a question as to whether or
  1536.      not it would be appropriate to search the current global block
  1537.      here as well.  (That's what this code used to do before the
  1538.      is_a_field_of_this check was moved up.)  On the one hand, it's
  1539.      redundant with the lookup in all objfiles search that happens
  1540.      next.  On the other hand, if decode_line_1 is passed an argument
  1541.      like filename:var, then the user presumably wants 'var' to be
  1542.      searched for in filename.  On the third hand, there shouldn't be
  1543.      multiple global variables all of which are named 'var', and it's
  1544.      not like decode_line_1 has ever restricted its search to only
  1545.      global variables in a single filename.  All in all, only
  1546.      searching the static block here seems best: it's correct and it's
  1547.      cleanest.  */

  1548.   /* NOTE: carlton/2002-12-05: There's also a possible performance
  1549.      issue here: if you usually search for global symbols in the
  1550.      current file, then it would be slightly better to search the
  1551.      current global block before searching all the symtabs.  But there
  1552.      are other factors that have a much greater effect on performance
  1553.      than that one, so I don't think we should worry about that for
  1554.      now.  */

  1555.   /* NOTE: dje/2014-10-26: The lookup in all objfiles search could skip
  1556.      the current objfile.  Searching the current objfile first is useful
  1557.      for both matching user expectations as well as performance.  */

  1558.   sym = lookup_symbol_in_static_block (name, block, domain);
  1559.   if (sym != NULL)
  1560.     return sym;

  1561.   /* If we didn't find a definition for a builtin type in the static block,
  1562.      search for it now.  This is actually the right thing to do and can be
  1563.      a massive performance win.  E.g., when debugging a program with lots of
  1564.      shared libraries we could search all of them only to find out the
  1565.      builtin type isn't defined in any of them.  This is common for types
  1566.      like "void".  */
  1567.   if (domain == VAR_DOMAIN)
  1568.     {
  1569.       struct gdbarch *gdbarch;

  1570.       if (block == NULL)
  1571.         gdbarch = target_gdbarch ();
  1572.       else
  1573.         gdbarch = block_gdbarch (block);
  1574.       sym = language_lookup_primitive_type_as_symbol (langdef, gdbarch, name);
  1575.       if (sym != NULL)
  1576.         return sym;
  1577.     }

  1578.   return lookup_global_symbol (name, block, domain);
  1579. }

  1580. /* See symtab.h.  */

  1581. struct symbol *
  1582. lookup_symbol_in_static_block (const char *name,
  1583.                                const struct block *block,
  1584.                                const domain_enum domain)
  1585. {
  1586.   const struct block *static_block = block_static_block (block);
  1587.   struct symbol *sym;

  1588.   if (static_block == NULL)
  1589.     return NULL;

  1590.   if (symbol_lookup_debug)
  1591.     {
  1592.       struct objfile *objfile = lookup_objfile_from_block (static_block);

  1593.       fprintf_unfiltered (gdb_stdlog,
  1594.                           "lookup_symbol_in_static_block (%s, %s (objfile %s),"
  1595.                           " %s)\n",
  1596.                           name,
  1597.                           host_address_to_string (block),
  1598.                           objfile_debug_name (objfile),
  1599.                           domain_name (domain));
  1600.     }

  1601.   sym = lookup_symbol_in_block (name, static_block, domain);
  1602.   if (symbol_lookup_debug)
  1603.     {
  1604.       fprintf_unfiltered (gdb_stdlog,
  1605.                           "lookup_symbol_in_static_block (...) = %s\n",
  1606.                           sym != NULL ? host_address_to_string (sym) : "NULL");
  1607.     }
  1608.   return sym;
  1609. }

  1610. /* Perform the standard symbol lookup of NAME in OBJFILE:
  1611.    1) First search expanded symtabs, and if not found
  1612.    2) Search the "quick" symtabs (partial or .gdb_index).
  1613.    BLOCK_INDEX is one of GLOBAL_BLOCK or STATIC_BLOCK.  */

  1614. static struct symbol *
  1615. lookup_symbol_in_objfile (struct objfile *objfile, int block_index,
  1616.                           const char *name, const domain_enum domain)
  1617. {
  1618.   struct symbol *result;

  1619.   if (symbol_lookup_debug)
  1620.     {
  1621.       fprintf_unfiltered (gdb_stdlog,
  1622.                           "lookup_symbol_in_objfile (%s, %s, %s, %s)\n",
  1623.                           objfile_debug_name (objfile),
  1624.                           block_index == GLOBAL_BLOCK
  1625.                           ? "GLOBAL_BLOCK" : "STATIC_BLOCK",
  1626.                           name, domain_name (domain));
  1627.     }

  1628.   result = lookup_symbol_in_objfile_symtabs (objfile, block_index,
  1629.                                              name, domain);
  1630.   if (result != NULL)
  1631.     {
  1632.       if (symbol_lookup_debug)
  1633.         {
  1634.           fprintf_unfiltered (gdb_stdlog,
  1635.                               "lookup_symbol_in_objfile (...) = %s"
  1636.                               " (in symtabs)\n",
  1637.                               host_address_to_string (result));
  1638.         }
  1639.       return result;
  1640.     }

  1641.   result = lookup_symbol_via_quick_fns (objfile, block_index,
  1642.                                         name, domain);
  1643.   if (symbol_lookup_debug)
  1644.     {
  1645.       fprintf_unfiltered (gdb_stdlog,
  1646.                           "lookup_symbol_in_objfile (...) = %s%s\n",
  1647.                           result != NULL
  1648.                           ? host_address_to_string (result)
  1649.                           : "NULL",
  1650.                           result != NULL ? " (via quick fns)" : "");
  1651.     }
  1652.   return result;
  1653. }

  1654. /* See symtab.h.  */

  1655. struct symbol *
  1656. lookup_static_symbol (const char *name, const domain_enum domain)
  1657. {
  1658.   struct objfile *objfile;
  1659.   struct symbol *result;

  1660.   ALL_OBJFILES (objfile)
  1661.     {
  1662.       result = lookup_symbol_in_objfile (objfile, STATIC_BLOCK, name, domain);
  1663.       if (result != NULL)
  1664.         return result;
  1665.     }

  1666.   return NULL;
  1667. }

  1668. /* Private data to be used with lookup_symbol_global_iterator_cb.  */

  1669. struct global_sym_lookup_data
  1670. {
  1671.   /* The name of the symbol we are searching for.  */
  1672.   const char *name;

  1673.   /* The domain to use for our search.  */
  1674.   domain_enum domain;

  1675.   /* The field where the callback should store the symbol if found.
  1676.      It should be initialized to NULL before the search is started.  */
  1677.   struct symbol *result;
  1678. };

  1679. /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
  1680.    It searches by name for a symbol in the GLOBAL_BLOCK of the given
  1681.    OBJFILE.  The arguments for the search are passed via CB_DATA,
  1682.    which in reality is a pointer to struct global_sym_lookup_data.  */

  1683. static int
  1684. lookup_symbol_global_iterator_cb (struct objfile *objfile,
  1685.                                   void *cb_data)
  1686. {
  1687.   struct global_sym_lookup_data *data =
  1688.     (struct global_sym_lookup_data *) cb_data;

  1689.   gdb_assert (data->result == NULL);

  1690.   data->result = lookup_symbol_in_objfile (objfile, GLOBAL_BLOCK,
  1691.                                            data->name, data->domain);

  1692.   /* If we found a match, tell the iterator to stop.  Otherwise,
  1693.      keep going.  */
  1694.   return (data->result != NULL);
  1695. }

  1696. /* See symtab.h.  */

  1697. struct symbol *
  1698. lookup_global_symbol (const char *name,
  1699.                       const struct block *block,
  1700.                       const domain_enum domain)
  1701. {
  1702.   struct symbol *sym = NULL;
  1703.   struct objfile *objfile = NULL;
  1704.   struct global_sym_lookup_data lookup_data;

  1705.   /* Call library-specific lookup procedure.  */
  1706.   objfile = lookup_objfile_from_block (block);
  1707.   if (objfile != NULL)
  1708.     sym = solib_global_lookup (objfile, name, domain);
  1709.   if (sym != NULL)
  1710.     return sym;

  1711.   memset (&lookup_data, 0, sizeof (lookup_data));
  1712.   lookup_data.name = name;
  1713.   lookup_data.domain = domain;
  1714.   gdbarch_iterate_over_objfiles_in_search_order
  1715.     (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
  1716.      lookup_symbol_global_iterator_cb, &lookup_data, objfile);

  1717.   return lookup_data.result;
  1718. }

  1719. int
  1720. symbol_matches_domain (enum language symbol_language,
  1721.                        domain_enum symbol_domain,
  1722.                        domain_enum domain)
  1723. {
  1724.   /* For C++ "struct foo { ... }" also defines a typedef for "foo".
  1725.      A Java class declaration also defines a typedef for the class.
  1726.      Similarly, any Ada type declaration implicitly defines a typedef.  */
  1727.   if (symbol_language == language_cplus
  1728.       || symbol_language == language_d
  1729.       || symbol_language == language_java
  1730.       || symbol_language == language_ada)
  1731.     {
  1732.       if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
  1733.           && symbol_domain == STRUCT_DOMAIN)
  1734.         return 1;
  1735.     }
  1736.   /* For all other languages, strict match is required.  */
  1737.   return (symbol_domain == domain);
  1738. }

  1739. /* See symtab.h.  */

  1740. struct type *
  1741. lookup_transparent_type (const char *name)
  1742. {
  1743.   return current_language->la_lookup_transparent_type (name);
  1744. }

  1745. /* A helper for basic_lookup_transparent_type that interfaces with the
  1746.    "quick" symbol table functions.  */

  1747. static struct type *
  1748. basic_lookup_transparent_type_quick (struct objfile *objfile, int block_index,
  1749.                                      const char *name)
  1750. {
  1751.   struct compunit_symtab *cust;
  1752.   const struct blockvector *bv;
  1753.   struct block *block;
  1754.   struct symbol *sym;

  1755.   if (!objfile->sf)
  1756.     return NULL;
  1757.   cust = objfile->sf->qf->lookup_symbol (objfile, block_index, name,
  1758.                                          STRUCT_DOMAIN);
  1759.   if (cust == NULL)
  1760.     return NULL;

  1761.   bv = COMPUNIT_BLOCKVECTOR (cust);
  1762.   block = BLOCKVECTOR_BLOCK (bv, block_index);
  1763.   sym = block_lookup_symbol (block, name, STRUCT_DOMAIN);
  1764.   if (!sym)
  1765.     error_in_psymtab_expansion (block_index, name, cust);

  1766.   if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
  1767.     return SYMBOL_TYPE (sym);

  1768.   return NULL;
  1769. }

  1770. /* The standard implementation of lookup_transparent_type.  This code
  1771.    was modeled on lookup_symbol -- the parts not relevant to looking
  1772.    up types were just left out.  In particular it's assumed here that
  1773.    types are available in STRUCT_DOMAIN and only in file-static or
  1774.    global blocks.  */

  1775. struct type *
  1776. basic_lookup_transparent_type (const char *name)
  1777. {
  1778.   struct symbol *sym;
  1779.   struct compunit_symtab *cust;
  1780.   const struct blockvector *bv;
  1781.   struct objfile *objfile;
  1782.   struct block *block;
  1783.   struct type *t;

  1784.   /* Now search all the global symbols.  Do the symtab's first, then
  1785.      check the psymtab's.  If a psymtab indicates the existence
  1786.      of the desired name as a global, then do psymtab-to-symtab
  1787.      conversion on the fly and return the found symbol.  */

  1788.   ALL_OBJFILES (objfile)
  1789.   {
  1790.     ALL_OBJFILE_COMPUNITS (objfile, cust)
  1791.       {
  1792.         bv = COMPUNIT_BLOCKVECTOR (cust);
  1793.         block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  1794.         sym = block_lookup_symbol (block, name, STRUCT_DOMAIN);
  1795.         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
  1796.           {
  1797.             return SYMBOL_TYPE (sym);
  1798.           }
  1799.       }
  1800.   }

  1801.   ALL_OBJFILES (objfile)
  1802.   {
  1803.     t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
  1804.     if (t)
  1805.       return t;
  1806.   }

  1807.   /* Now search the static file-level symbols.
  1808.      Not strictly correct, but more useful than an error.
  1809.      Do the symtab's first, then
  1810.      check the psymtab's.  If a psymtab indicates the existence
  1811.      of the desired name as a file-level static, then do psymtab-to-symtab
  1812.      conversion on the fly and return the found symbol.  */

  1813.   ALL_OBJFILES (objfile)
  1814.   {
  1815.     ALL_OBJFILE_COMPUNITS (objfile, cust)
  1816.       {
  1817.         bv = COMPUNIT_BLOCKVECTOR (cust);
  1818.         block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  1819.         sym = block_lookup_symbol (block, name, STRUCT_DOMAIN);
  1820.         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
  1821.           {
  1822.             return SYMBOL_TYPE (sym);
  1823.           }
  1824.       }
  1825.   }

  1826.   ALL_OBJFILES (objfile)
  1827.   {
  1828.     t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
  1829.     if (t)
  1830.       return t;
  1831.   }

  1832.   return (struct type *) 0;
  1833. }

  1834. /* Iterate over the symbols named NAME, matching DOMAIN, in BLOCK.

  1835.    For each symbol that matches, CALLBACK is called.  The symbol and
  1836.    DATA are passed to the callback.

  1837.    If CALLBACK returns zero, the iteration ends.  Otherwise, the
  1838.    search continues.  */

  1839. void
  1840. iterate_over_symbols (const struct block *block, const char *name,
  1841.                       const domain_enum domain,
  1842.                       symbol_found_callback_ftype *callback,
  1843.                       void *data)
  1844. {
  1845.   struct block_iterator iter;
  1846.   struct symbol *sym;

  1847.   ALL_BLOCK_SYMBOLS_WITH_NAME (block, name, iter, sym)
  1848.     {
  1849.       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
  1850.                                  SYMBOL_DOMAIN (sym), domain))
  1851.         {
  1852.           if (!callback (sym, data))
  1853.             return;
  1854.         }
  1855.     }
  1856. }

  1857. /* Find the compunit symtab associated with PC and SECTION.
  1858.    This will read in debug info as necessary.  */

  1859. struct compunit_symtab *
  1860. find_pc_sect_compunit_symtab (CORE_ADDR pc, struct obj_section *section)
  1861. {
  1862.   struct compunit_symtab *cust;
  1863.   struct compunit_symtab *best_cust = NULL;
  1864.   struct objfile *objfile;
  1865.   CORE_ADDR distance = 0;
  1866.   struct bound_minimal_symbol msymbol;

  1867.   /* If we know that this is not a text address, return failure.  This is
  1868.      necessary because we loop based on the block's high and low code
  1869.      addresses, which do not include the data ranges, and because
  1870.      we call find_pc_sect_psymtab which has a similar restriction based
  1871.      on the partial_symtab's texthigh and textlow.  */
  1872.   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
  1873.   if (msymbol.minsym
  1874.       && (MSYMBOL_TYPE (msymbol.minsym) == mst_data
  1875.           || MSYMBOL_TYPE (msymbol.minsym) == mst_bss
  1876.           || MSYMBOL_TYPE (msymbol.minsym) == mst_abs
  1877.           || MSYMBOL_TYPE (msymbol.minsym) == mst_file_data
  1878.           || MSYMBOL_TYPE (msymbol.minsym) == mst_file_bss))
  1879.     return NULL;

  1880.   /* Search all symtabs for the one whose file contains our address, and which
  1881.      is the smallest of all the ones containing the address.  This is designed
  1882.      to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
  1883.      and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
  1884.      0x1000-0x4000, but for address 0x2345 we want to return symtab b.

  1885.      This happens for native ecoff format, where code from included files
  1886.      gets its own symtab.  The symtab for the included file should have
  1887.      been read in already via the dependency mechanism.
  1888.      It might be swifter to create several symtabs with the same name
  1889.      like xcoff does (I'm not sure).

  1890.      It also happens for objfiles that have their functions reordered.
  1891.      For these, the symtab we are looking for is not necessarily read in.  */

  1892.   ALL_COMPUNITS (objfile, cust)
  1893.   {
  1894.     struct block *b;
  1895.     const struct blockvector *bv;

  1896.     bv = COMPUNIT_BLOCKVECTOR (cust);
  1897.     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);

  1898.     if (BLOCK_START (b) <= pc
  1899.         && BLOCK_END (b) > pc
  1900.         && (distance == 0
  1901.             || BLOCK_END (b) - BLOCK_START (b) < distance))
  1902.       {
  1903.         /* For an objfile that has its functions reordered,
  1904.            find_pc_psymtab will find the proper partial symbol table
  1905.            and we simply return its corresponding symtab.  */
  1906.         /* In order to better support objfiles that contain both
  1907.            stabs and coff debugging info, we continue on if a psymtab
  1908.            can't be found.  */
  1909.         if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
  1910.           {
  1911.             struct compunit_symtab *result;

  1912.             result
  1913.               = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
  1914.                                                                msymbol,
  1915.                                                                pc, section,
  1916.                                                                0);
  1917.             if (result != NULL)
  1918.               return result;
  1919.           }
  1920.         if (section != 0)
  1921.           {
  1922.             struct block_iterator iter;
  1923.             struct symbol *sym = NULL;

  1924.             ALL_BLOCK_SYMBOLS (b, iter, sym)
  1925.               {
  1926.                 fixup_symbol_section (sym, objfile);
  1927.                 if (matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, sym),
  1928.                                            section))
  1929.                   break;
  1930.               }
  1931.             if (sym == NULL)
  1932.               continue;                /* No symbol in this symtab matches
  1933.                                    section.  */
  1934.           }
  1935.         distance = BLOCK_END (b) - BLOCK_START (b);
  1936.         best_cust = cust;
  1937.       }
  1938.   }

  1939.   if (best_cust != NULL)
  1940.     return best_cust;

  1941.   /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs).  */

  1942.   ALL_OBJFILES (objfile)
  1943.   {
  1944.     struct compunit_symtab *result;

  1945.     if (!objfile->sf)
  1946.       continue;
  1947.     result = objfile->sf->qf->find_pc_sect_compunit_symtab (objfile,
  1948.                                                             msymbol,
  1949.                                                             pc, section,
  1950.                                                             1);
  1951.     if (result != NULL)
  1952.       return result;
  1953.   }

  1954.   return NULL;
  1955. }

  1956. /* Find the compunit symtab associated with PC.
  1957.    This will read in debug info as necessary.
  1958.    Backward compatibility, no section.  */

  1959. struct compunit_symtab *
  1960. find_pc_compunit_symtab (CORE_ADDR pc)
  1961. {
  1962.   return find_pc_sect_compunit_symtab (pc, find_pc_mapped_section (pc));
  1963. }


  1964. /* Find the source file and line number for a given PC value and SECTION.
  1965.    Return a structure containing a symtab pointer, a line number,
  1966.    and a pc range for the entire source line.
  1967.    The value's .pc field is NOT the specified pc.
  1968.    NOTCURRENT nonzero means, if specified pc is on a line boundary,
  1969.    use the line that ends there.  Otherwise, in that case, the line
  1970.    that begins there is used.  */

  1971. /* The big complication here is that a line may start in one file, and end just
  1972.    before the start of another file.  This usually occurs when you #include
  1973.    code in the middle of a subroutine.  To properly find the end of a line's PC
  1974.    range, we must search all symtabs associated with this compilation unit, and
  1975.    find the one whose first PC is closer than that of the next line in this
  1976.    symtab.  */

  1977. /* If it's worth the effort, we could be using a binary search.  */

  1978. struct symtab_and_line
  1979. find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
  1980. {
  1981.   struct compunit_symtab *cust;
  1982.   struct symtab *iter_s;
  1983.   struct linetable *l;
  1984.   int len;
  1985.   int i;
  1986.   struct linetable_entry *item;
  1987.   struct symtab_and_line val;
  1988.   const struct blockvector *bv;
  1989.   struct bound_minimal_symbol msymbol;

  1990.   /* Info on best line seen so far, and where it starts, and its file.  */

  1991.   struct linetable_entry *best = NULL;
  1992.   CORE_ADDR best_end = 0;
  1993.   struct symtab *best_symtab = 0;

  1994.   /* Store here the first line number
  1995.      of a file which contains the line at the smallest pc after PC.
  1996.      If we don't find a line whose range contains PC,
  1997.      we will use a line one less than this,
  1998.      with a range from the start of that file to the first line's pc.  */
  1999.   struct linetable_entry *alt = NULL;

  2000.   /* Info on best line seen in this file.  */

  2001.   struct linetable_entry *prev;

  2002.   /* If this pc is not from the current frame,
  2003.      it is the address of the end of a call instruction.
  2004.      Quite likely that is the start of the following statement.
  2005.      But what we want is the statement containing the instruction.
  2006.      Fudge the pc to make sure we get that.  */

  2007.   init_sal (&val);                /* initialize to zeroes */

  2008.   val.pspace = current_program_space;

  2009.   /* It's tempting to assume that, if we can't find debugging info for
  2010.      any function enclosing PC, that we shouldn't search for line
  2011.      number info, either.  However, GAS can emit line number info for
  2012.      assembly files --- very helpful when debugging hand-written
  2013.      assembly code.  In such a case, we'd have no debug info for the
  2014.      function, but we would have line info.  */

  2015.   if (notcurrent)
  2016.     pc -= 1;

  2017.   /* elz: added this because this function returned the wrong
  2018.      information if the pc belongs to a stub (import/export)
  2019.      to call a shlib function.  This stub would be anywhere between
  2020.      two functions in the target, and the line info was erroneously
  2021.      taken to be the one of the line before the pc.  */

  2022.   /* RT: Further explanation:

  2023.    * We have stubs (trampolines) inserted between procedures.
  2024.    *
  2025.    * Example: "shr1" exists in a shared library, and a "shr1" stub also
  2026.    * exists in the main image.
  2027.    *
  2028.    * In the minimal symbol table, we have a bunch of symbols
  2029.    * sorted by start address.  The stubs are marked as "trampoline",
  2030.    * the others appear as text. E.g.:
  2031.    *
  2032.    *  Minimal symbol table for main image
  2033.    *     maincode for main (text symbol)
  2034.    *     shr1: stub  (trampoline symbol)
  2035.    *     foo:   code for foo (text symbol)
  2036.    *     ...
  2037.    *  Minimal symbol table for "shr1" image:
  2038.    *     ...
  2039.    *     shr1: code for shr1 (text symbol)
  2040.    *     ...
  2041.    *
  2042.    * So the code below is trying to detect if we are in the stub
  2043.    * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
  2044.    * and if found,  do the symbolization from the real-code address
  2045.    * rather than the stub address.
  2046.    *
  2047.    * Assumptions being made about the minimal symbol table:
  2048.    *   1. lookup_minimal_symbol_by_pc() will return a trampoline only
  2049.    *      if we're really in the trampoline.s If we're beyond it (say
  2050.    *      we're in "foo" in the above example), it'll have a closer
  2051.    *      symbol (the "foo" text symbol for example) and will not
  2052.    *      return the trampoline.
  2053.    *   2. lookup_minimal_symbol_text() will find a real text symbol
  2054.    *      corresponding to the trampoline, and whose address will
  2055.    *      be different than the trampoline addressI put in a sanity
  2056.    *      check for the address being the same, to avoid an
  2057.    *      infinite recursion.
  2058.    */
  2059.   msymbol = lookup_minimal_symbol_by_pc (pc);
  2060.   if (msymbol.minsym != NULL)
  2061.     if (MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
  2062.       {
  2063.         struct bound_minimal_symbol mfunsym
  2064.           = lookup_minimal_symbol_text (MSYMBOL_LINKAGE_NAME (msymbol.minsym),
  2065.                                         NULL);

  2066.         if (mfunsym.minsym == NULL)
  2067.           /* I eliminated this warning since it is coming out
  2068.            * in the following situation:
  2069.            * gdb shmain // test program with shared libraries
  2070.            * (gdb) break shr1  // function in shared lib
  2071.            * Warning: In stub for ...
  2072.            * In the above situation, the shared lib is not loaded yet,
  2073.            * so of course we can't find the real func/line info,
  2074.            * but the "break" still works, and the warning is annoying.
  2075.            * So I commented out the warning.  RT */
  2076.           /* warning ("In stub for %s; unable to find real function/line info",
  2077.              SYMBOL_LINKAGE_NAME (msymbol)); */
  2078.           ;
  2079.         /* fall through */
  2080.         else if (BMSYMBOL_VALUE_ADDRESS (mfunsym)
  2081.                  == BMSYMBOL_VALUE_ADDRESS (msymbol))
  2082.           /* Avoid infinite recursion */
  2083.           /* See above comment about why warning is commented out.  */
  2084.           /* warning ("In stub for %s; unable to find real function/line info",
  2085.              SYMBOL_LINKAGE_NAME (msymbol)); */
  2086.           ;
  2087.         /* fall through */
  2088.         else
  2089.           return find_pc_line (BMSYMBOL_VALUE_ADDRESS (mfunsym), 0);
  2090.       }


  2091.   cust = find_pc_sect_compunit_symtab (pc, section);
  2092.   if (cust == NULL)
  2093.     {
  2094.       /* If no symbol information, return previous pc.  */
  2095.       if (notcurrent)
  2096.         pc++;
  2097.       val.pc = pc;
  2098.       return val;
  2099.     }

  2100.   bv = COMPUNIT_BLOCKVECTOR (cust);

  2101.   /* Look at all the symtabs that share this blockvector.
  2102.      They all have the same apriori range, that we found was right;
  2103.      but they have different line tables.  */

  2104.   ALL_COMPUNIT_FILETABS (cust, iter_s)
  2105.     {
  2106.       /* Find the best line in this symtab.  */
  2107.       l = SYMTAB_LINETABLE (iter_s);
  2108.       if (!l)
  2109.         continue;
  2110.       len = l->nitems;
  2111.       if (len <= 0)
  2112.         {
  2113.           /* I think len can be zero if the symtab lacks line numbers
  2114.              (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
  2115.              I'm not sure which, and maybe it depends on the symbol
  2116.              reader).  */
  2117.           continue;
  2118.         }

  2119.       prev = NULL;
  2120.       item = l->item;                /* Get first line info.  */

  2121.       /* Is this file's first line closer than the first lines of other files?
  2122.          If so, record this file, and its first line, as best alternate.  */
  2123.       if (item->pc > pc && (!alt || item->pc < alt->pc))
  2124.         alt = item;

  2125.       for (i = 0; i < len; i++, item++)
  2126.         {
  2127.           /* Leave prev pointing to the linetable entry for the last line
  2128.              that started at or before PC.  */
  2129.           if (item->pc > pc)
  2130.             break;

  2131.           prev = item;
  2132.         }

  2133.       /* At this point, prev points at the line whose start addr is <= pc, and
  2134.          item points at the next line.  If we ran off the end of the linetable
  2135.          (pc >= start of the last line), then prev == item.  If pc < start of
  2136.          the first line, prev will not be set.  */

  2137.       /* Is this file's best line closer than the best in the other files?
  2138.          If so, record this file, and its best line, as best so far.  Don't
  2139.          save prev if it represents the end of a function (i.e. line number
  2140.          0) instead of a real line.  */

  2141.       if (prev && prev->line && (!best || prev->pc > best->pc))
  2142.         {
  2143.           best = prev;
  2144.           best_symtab = iter_s;

  2145.           /* Discard BEST_END if it's before the PC of the current BEST.  */
  2146.           if (best_end <= best->pc)
  2147.             best_end = 0;
  2148.         }

  2149.       /* If another line (denoted by ITEM) is in the linetable and its
  2150.          PC is after BEST's PC, but before the current BEST_END, then
  2151.          use ITEM's PC as the new best_end.  */
  2152.       if (best && i < len && item->pc > best->pc
  2153.           && (best_end == 0 || best_end > item->pc))
  2154.         best_end = item->pc;
  2155.     }

  2156.   if (!best_symtab)
  2157.     {
  2158.       /* If we didn't find any line number info, just return zeros.
  2159.          We used to return alt->line - 1 here, but that could be
  2160.          anywhere; if we don't have line number info for this PC,
  2161.          don't make some up.  */
  2162.       val.pc = pc;
  2163.     }
  2164.   else if (best->line == 0)
  2165.     {
  2166.       /* If our best fit is in a range of PC's for which no line
  2167.          number info is available (line number is zero) then we didn't
  2168.          find any valid line information.  */
  2169.       val.pc = pc;
  2170.     }
  2171.   else
  2172.     {
  2173.       val.symtab = best_symtab;
  2174.       val.line = best->line;
  2175.       val.pc = best->pc;
  2176.       if (best_end && (!alt || best_end < alt->pc))
  2177.         val.end = best_end;
  2178.       else if (alt)
  2179.         val.end = alt->pc;
  2180.       else
  2181.         val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
  2182.     }
  2183.   val.section = section;
  2184.   return val;
  2185. }

  2186. /* Backward compatibility (no section).  */

  2187. struct symtab_and_line
  2188. find_pc_line (CORE_ADDR pc, int notcurrent)
  2189. {
  2190.   struct obj_section *section;

  2191.   section = find_pc_overlay (pc);
  2192.   if (pc_in_unmapped_range (pc, section))
  2193.     pc = overlay_mapped_address (pc, section);
  2194.   return find_pc_sect_line (pc, section, notcurrent);
  2195. }

  2196. /* See symtab.h.  */

  2197. struct symtab *
  2198. find_pc_line_symtab (CORE_ADDR pc)
  2199. {
  2200.   struct symtab_and_line sal;

  2201.   /* This always passes zero for NOTCURRENT to find_pc_line.
  2202.      There are currently no callers that ever pass non-zero.  */
  2203.   sal = find_pc_line (pc, 0);
  2204.   return sal.symtab;
  2205. }

  2206. /* Find line number LINE in any symtab whose name is the same as
  2207.    SYMTAB.

  2208.    If found, return the symtab that contains the linetable in which it was
  2209.    found, set *INDEX to the index in the linetable of the best entry
  2210.    found, and set *EXACT_MATCH nonzero if the value returned is an
  2211.    exact match.

  2212.    If not found, return NULL.  */

  2213. struct symtab *
  2214. find_line_symtab (struct symtab *symtab, int line,
  2215.                   int *index, int *exact_match)
  2216. {
  2217.   int exact = 0/* Initialized here to avoid a compiler warning.  */

  2218.   /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
  2219.      so far seen.  */

  2220.   int best_index;
  2221.   struct linetable *best_linetable;
  2222.   struct symtab *best_symtab;

  2223.   /* First try looking it up in the given symtab.  */
  2224.   best_linetable = SYMTAB_LINETABLE (symtab);
  2225.   best_symtab = symtab;
  2226.   best_index = find_line_common (best_linetable, line, &exact, 0);
  2227.   if (best_index < 0 || !exact)
  2228.     {
  2229.       /* Didn't find an exact match.  So we better keep looking for
  2230.          another symtab with the same name.  In the case of xcoff,
  2231.          multiple csects for one source file (produced by IBM's FORTRAN
  2232.          compiler) produce multiple symtabs (this is unavoidable
  2233.          assuming csects can be at arbitrary places in memory and that
  2234.          the GLOBAL_BLOCK of a symtab has a begin and end address).  */

  2235.       /* BEST is the smallest linenumber > LINE so far seen,
  2236.          or 0 if none has been seen so far.
  2237.          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
  2238.       int best;

  2239.       struct objfile *objfile;
  2240.       struct compunit_symtab *cu;
  2241.       struct symtab *s;

  2242.       if (best_index >= 0)
  2243.         best = best_linetable->item[best_index].line;
  2244.       else
  2245.         best = 0;

  2246.       ALL_OBJFILES (objfile)
  2247.       {
  2248.         if (objfile->sf)
  2249.           objfile->sf->qf->expand_symtabs_with_fullname (objfile,
  2250.                                                    symtab_to_fullname (symtab));
  2251.       }

  2252.       ALL_FILETABS (objfile, cu, s)
  2253.       {
  2254.         struct linetable *l;
  2255.         int ind;

  2256.         if (FILENAME_CMP (symtab->filename, s->filename) != 0)
  2257.           continue;
  2258.         if (FILENAME_CMP (symtab_to_fullname (symtab),
  2259.                           symtab_to_fullname (s)) != 0)
  2260.           continue;
  2261.         l = SYMTAB_LINETABLE (s);
  2262.         ind = find_line_common (l, line, &exact, 0);
  2263.         if (ind >= 0)
  2264.           {
  2265.             if (exact)
  2266.               {
  2267.                 best_index = ind;
  2268.                 best_linetable = l;
  2269.                 best_symtab = s;
  2270.                 goto done;
  2271.               }
  2272.             if (best == 0 || l->item[ind].line < best)
  2273.               {
  2274.                 best = l->item[ind].line;
  2275.                 best_index = ind;
  2276.                 best_linetable = l;
  2277.                 best_symtab = s;
  2278.               }
  2279.           }
  2280.       }
  2281.     }
  2282. done:
  2283.   if (best_index < 0)
  2284.     return NULL;

  2285.   if (index)
  2286.     *index = best_index;
  2287.   if (exact_match)
  2288.     *exact_match = exact;

  2289.   return best_symtab;
  2290. }

  2291. /* Given SYMTAB, returns all the PCs function in the symtab that
  2292.    exactly match LINE.  Returns NULL if there are no exact matches,
  2293.    but updates BEST_ITEM in this case.  */

  2294. VEC (CORE_ADDR) *
  2295. find_pcs_for_symtab_line (struct symtab *symtab, int line,
  2296.                           struct linetable_entry **best_item)
  2297. {
  2298.   int start = 0;
  2299.   VEC (CORE_ADDR) *result = NULL;

  2300.   /* First, collect all the PCs that are at this line.  */
  2301.   while (1)
  2302.     {
  2303.       int was_exact;
  2304.       int idx;

  2305.       idx = find_line_common (SYMTAB_LINETABLE (symtab), line, &was_exact,
  2306.                               start);
  2307.       if (idx < 0)
  2308.         break;

  2309.       if (!was_exact)
  2310.         {
  2311.           struct linetable_entry *item = &SYMTAB_LINETABLE (symtab)->item[idx];

  2312.           if (*best_item == NULL || item->line < (*best_item)->line)
  2313.             *best_item = item;

  2314.           break;
  2315.         }

  2316.       VEC_safe_push (CORE_ADDR, result,
  2317.                      SYMTAB_LINETABLE (symtab)->item[idx].pc);
  2318.       start = idx + 1;
  2319.     }

  2320.   return result;
  2321. }


  2322. /* Set the PC value for a given source file and line number and return true.
  2323.    Returns zero for invalid line number (and sets the PC to 0).
  2324.    The source file is specified with a struct symtab.  */

  2325. int
  2326. find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
  2327. {
  2328.   struct linetable *l;
  2329.   int ind;

  2330.   *pc = 0;
  2331.   if (symtab == 0)
  2332.     return 0;

  2333.   symtab = find_line_symtab (symtab, line, &ind, NULL);
  2334.   if (symtab != NULL)
  2335.     {
  2336.       l = SYMTAB_LINETABLE (symtab);
  2337.       *pc = l->item[ind].pc;
  2338.       return 1;
  2339.     }
  2340.   else
  2341.     return 0;
  2342. }

  2343. /* Find the range of pc values in a line.
  2344.    Store the starting pc of the line into *STARTPTR
  2345.    and the ending pc (start of next line) into *ENDPTR.
  2346.    Returns 1 to indicate success.
  2347.    Returns 0 if could not find the specified line.  */

  2348. int
  2349. find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
  2350.                     CORE_ADDR *endptr)
  2351. {
  2352.   CORE_ADDR startaddr;
  2353.   struct symtab_and_line found_sal;

  2354.   startaddr = sal.pc;
  2355.   if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
  2356.     return 0;

  2357.   /* This whole function is based on address.  For example, if line 10 has
  2358.      two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
  2359.      "info line *0x123" should say the line goes from 0x100 to 0x200
  2360.      and "info line *0x355" should say the line goes from 0x300 to 0x400.
  2361.      This also insures that we never give a range like "starts at 0x134
  2362.      and ends at 0x12c".  */

  2363.   found_sal = find_pc_sect_line (startaddr, sal.section, 0);
  2364.   if (found_sal.line != sal.line)
  2365.     {
  2366.       /* The specified line (sal) has zero bytes.  */
  2367.       *startptr = found_sal.pc;
  2368.       *endptr = found_sal.pc;
  2369.     }
  2370.   else
  2371.     {
  2372.       *startptr = found_sal.pc;
  2373.       *endptr = found_sal.end;
  2374.     }
  2375.   return 1;
  2376. }

  2377. /* Given a line table and a line number, return the index into the line
  2378.    table for the pc of the nearest line whose number is >= the specified one.
  2379.    Return -1 if none is found.  The value is >= 0 if it is an index.
  2380.    START is the index at which to start searching the line table.

  2381.    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */

  2382. static int
  2383. find_line_common (struct linetable *l, int lineno,
  2384.                   int *exact_match, int start)
  2385. {
  2386.   int i;
  2387.   int len;

  2388.   /* BEST is the smallest linenumber > LINENO so far seen,
  2389.      or 0 if none has been seen so far.
  2390.      BEST_INDEX identifies the item for it.  */

  2391.   int best_index = -1;
  2392.   int best = 0;

  2393.   *exact_match = 0;

  2394.   if (lineno <= 0)
  2395.     return -1;
  2396.   if (l == 0)
  2397.     return -1;

  2398.   len = l->nitems;
  2399.   for (i = start; i < len; i++)
  2400.     {
  2401.       struct linetable_entry *item = &(l->item[i]);

  2402.       if (item->line == lineno)
  2403.         {
  2404.           /* Return the first (lowest address) entry which matches.  */
  2405.           *exact_match = 1;
  2406.           return i;
  2407.         }

  2408.       if (item->line > lineno && (best == 0 || item->line < best))
  2409.         {
  2410.           best = item->line;
  2411.           best_index = i;
  2412.         }
  2413.     }

  2414.   /* If we got here, we didn't get an exact match.  */
  2415.   return best_index;
  2416. }

  2417. int
  2418. find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
  2419. {
  2420.   struct symtab_and_line sal;

  2421.   sal = find_pc_line (pc, 0);
  2422.   *startptr = sal.pc;
  2423.   *endptr = sal.end;
  2424.   return sal.symtab != 0;
  2425. }

  2426. /* Given a function symbol SYM, find the symtab and line for the start
  2427.    of the function.
  2428.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  2429.    of real code inside the function.  */

  2430. struct symtab_and_line
  2431. find_function_start_sal (struct symbol *sym, int funfirstline)
  2432. {
  2433.   struct symtab_and_line sal;
  2434.   struct obj_section *section;

  2435.   fixup_symbol_section (sym, NULL);
  2436.   section = SYMBOL_OBJ_SECTION (symbol_objfile (sym), sym);
  2437.   sal = find_pc_sect_line (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)), section, 0);

  2438.   /* We always should have a line for the function start address.
  2439.      If we don't, something is odd.  Create a plain SAL refering
  2440.      just the PC and hope that skip_prologue_sal (if requested)
  2441.      can find a line number for after the prologue.  */
  2442.   if (sal.pc < BLOCK_START (SYMBOL_BLOCK_VALUE (sym)))
  2443.     {
  2444.       init_sal (&sal);
  2445.       sal.pspace = current_program_space;
  2446.       sal.pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  2447.       sal.section = section;
  2448.     }

  2449.   if (funfirstline)
  2450.     skip_prologue_sal (&sal);

  2451.   return sal;
  2452. }

  2453. /* Given a function start address FUNC_ADDR and SYMTAB, find the first
  2454.    address for that function that has an entry in SYMTAB's line info
  2455.    table.  If such an entry cannot be found, return FUNC_ADDR
  2456.    unaltered.  */

  2457. static CORE_ADDR
  2458. skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
  2459. {
  2460.   CORE_ADDR func_start, func_end;
  2461.   struct linetable *l;
  2462.   int i;

  2463.   /* Give up if this symbol has no lineinfo table.  */
  2464.   l = SYMTAB_LINETABLE (symtab);
  2465.   if (l == NULL)
  2466.     return func_addr;

  2467.   /* Get the range for the function's PC values, or give up if we
  2468.      cannot, for some reason.  */
  2469.   if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
  2470.     return func_addr;

  2471.   /* Linetable entries are ordered by PC values, see the commentary in
  2472.      symtab.h where `struct linetable' is defined.  Thus, the first
  2473.      entry whose PC is in the range [FUNC_START..FUNC_END[ is the
  2474.      address we are looking for.  */
  2475.   for (i = 0; i < l->nitems; i++)
  2476.     {
  2477.       struct linetable_entry *item = &(l->item[i]);

  2478.       /* Don't use line numbers of zero, they mark special entries in
  2479.          the table.  See the commentary on symtab.h before the
  2480.          definition of struct linetable.  */
  2481.       if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
  2482.         return item->pc;
  2483.     }

  2484.   return func_addr;
  2485. }

  2486. /* Adjust SAL to the first instruction past the function prologue.
  2487.    If the PC was explicitly specified, the SAL is not changed.
  2488.    If the line number was explicitly specified, at most the SAL's PC
  2489.    is updated.  If SAL is already past the prologue, then do nothing.  */

  2490. void
  2491. skip_prologue_sal (struct symtab_and_line *sal)
  2492. {
  2493.   struct symbol *sym;
  2494.   struct symtab_and_line start_sal;
  2495.   struct cleanup *old_chain;
  2496.   CORE_ADDR pc, saved_pc;
  2497.   struct obj_section *section;
  2498.   const char *name;
  2499.   struct objfile *objfile;
  2500.   struct gdbarch *gdbarch;
  2501.   const struct block *b, *function_block;
  2502.   int force_skip, skip;

  2503.   /* Do not change the SAL if PC was specified explicitly.  */
  2504.   if (sal->explicit_pc)
  2505.     return;

  2506.   old_chain = save_current_space_and_thread ();
  2507.   switch_to_program_space_and_thread (sal->pspace);

  2508.   sym = find_pc_sect_function (sal->pc, sal->section);
  2509.   if (sym != NULL)
  2510.     {
  2511.       fixup_symbol_section (sym, NULL);

  2512.       objfile = symbol_objfile (sym);
  2513.       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  2514.       section = SYMBOL_OBJ_SECTION (objfile, sym);
  2515.       name = SYMBOL_LINKAGE_NAME (sym);
  2516.     }
  2517.   else
  2518.     {
  2519.       struct bound_minimal_symbol msymbol
  2520.         = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);

  2521.       if (msymbol.minsym == NULL)
  2522.         {
  2523.           do_cleanups (old_chain);
  2524.           return;
  2525.         }

  2526.       objfile = msymbol.objfile;
  2527.       pc = BMSYMBOL_VALUE_ADDRESS (msymbol);
  2528.       section = MSYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
  2529.       name = MSYMBOL_LINKAGE_NAME (msymbol.minsym);
  2530.     }

  2531.   gdbarch = get_objfile_arch (objfile);

  2532.   /* Process the prologue in two passes.  In the first pass try to skip the
  2533.      prologue (SKIP is true) and verify there is a real need for it (indicated
  2534.      by FORCE_SKIP).  If no such reason was found run a second pass where the
  2535.      prologue is not skipped (SKIP is false).  */

  2536.   skip = 1;
  2537.   force_skip = 1;

  2538.   /* Be conservative - allow direct PC (without skipping prologue) only if we
  2539.      have proven the CU (Compilation Unit) supports it.  sal->SYMTAB does not
  2540.      have to be set by the caller so we use SYM instead.  */
  2541.   if (sym != NULL
  2542.       && COMPUNIT_LOCATIONS_VALID (SYMTAB_COMPUNIT (symbol_symtab (sym))))
  2543.     force_skip = 0;

  2544.   saved_pc = pc;
  2545.   do
  2546.     {
  2547.       pc = saved_pc;

  2548.       /* If the function is in an unmapped overlay, use its unmapped LMA address,
  2549.          so that gdbarch_skip_prologue has something unique to work on.  */
  2550.       if (section_is_overlay (section) && !section_is_mapped (section))
  2551.         pc = overlay_unmapped_address (pc, section);

  2552.       /* Skip "first line" of function (which is actually its prologue).  */
  2553.       pc += gdbarch_deprecated_function_start_offset (gdbarch);
  2554.       if (gdbarch_skip_entrypoint_p (gdbarch))
  2555.         pc = gdbarch_skip_entrypoint (gdbarch, pc);
  2556.       if (skip)
  2557.         pc = gdbarch_skip_prologue (gdbarch, pc);

  2558.       /* For overlays, map pc back into its mapped VMA range.  */
  2559.       pc = overlay_mapped_address (pc, section);

  2560.       /* Calculate line number.  */
  2561.       start_sal = find_pc_sect_line (pc, section, 0);

  2562.       /* Check if gdbarch_skip_prologue left us in mid-line, and the next
  2563.          line is still part of the same function.  */
  2564.       if (skip && start_sal.pc != pc
  2565.           && (sym ? (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
  2566.                      && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
  2567.               : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
  2568.                  == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
  2569.         {
  2570.           /* First pc of next line */
  2571.           pc = start_sal.end;
  2572.           /* Recalculate the line number (might not be N+1).  */
  2573.           start_sal = find_pc_sect_line (pc, section, 0);
  2574.         }

  2575.       /* On targets with executable formats that don't have a concept of
  2576.          constructors (ELF with .init has, PE doesn't), gcc emits a call
  2577.          to `__main' in `main' between the prologue and before user
  2578.          code.  */
  2579.       if (gdbarch_skip_main_prologue_p (gdbarch)
  2580.           && name && strcmp_iw (name, "main") == 0)
  2581.         {
  2582.           pc = gdbarch_skip_main_prologue (gdbarch, pc);
  2583.           /* Recalculate the line number (might not be N+1).  */
  2584.           start_sal = find_pc_sect_line (pc, section, 0);
  2585.           force_skip = 1;
  2586.         }
  2587.     }
  2588.   while (!force_skip && skip--);

  2589.   /* If we still don't have a valid source line, try to find the first
  2590.      PC in the lineinfo table that belongs to the same function.  This
  2591.      happens with COFF debug info, which does not seem to have an
  2592.      entry in lineinfo table for the code after the prologue which has
  2593.      no direct relation to source.  For example, this was found to be
  2594.      the case with the DJGPP target using "gcc -gcoff" when the
  2595.      compiler inserted code after the prologue to make sure the stack
  2596.      is aligned.  */
  2597.   if (!force_skip && sym && start_sal.symtab == NULL)
  2598.     {
  2599.       pc = skip_prologue_using_lineinfo (pc, symbol_symtab (sym));
  2600.       /* Recalculate the line number.  */
  2601.       start_sal = find_pc_sect_line (pc, section, 0);
  2602.     }

  2603.   do_cleanups (old_chain);

  2604.   /* If we're already past the prologue, leave SAL unchanged.  Otherwise
  2605.      forward SAL to the end of the prologue.  */
  2606.   if (sal->pc >= pc)
  2607.     return;

  2608.   sal->pc = pc;
  2609.   sal->section = section;

  2610.   /* Unless the explicit_line flag was set, update the SAL line
  2611.      and symtab to correspond to the modified PC location.  */
  2612.   if (sal->explicit_line)
  2613.     return;

  2614.   sal->symtab = start_sal.symtab;
  2615.   sal->line = start_sal.line;
  2616.   sal->end = start_sal.end;

  2617.   /* Check if we are now inside an inlined function.  If we can,
  2618.      use the call site of the function instead.  */
  2619.   b = block_for_pc_sect (sal->pc, sal->section);
  2620.   function_block = NULL;
  2621.   while (b != NULL)
  2622.     {
  2623.       if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
  2624.         function_block = b;
  2625.       else if (BLOCK_FUNCTION (b) != NULL)
  2626.         break;
  2627.       b = BLOCK_SUPERBLOCK (b);
  2628.     }
  2629.   if (function_block != NULL
  2630.       && SYMBOL_LINE (BLOCK_FUNCTION (function_block)) != 0)
  2631.     {
  2632.       sal->line = SYMBOL_LINE (BLOCK_FUNCTION (function_block));
  2633.       sal->symtab = symbol_symtab (BLOCK_FUNCTION (function_block));
  2634.     }
  2635. }

  2636. /* Given PC at the function's start address, attempt to find the
  2637.    prologue end using SAL information.  Return zero if the skip fails.

  2638.    A non-optimized prologue traditionally has one SAL for the function
  2639.    and a second for the function body.  A single line function has
  2640.    them both pointing at the same line.

  2641.    An optimized prologue is similar but the prologue may contain
  2642.    instructions (SALs) from the instruction body.  Need to skip those
  2643.    while not getting into the function body.

  2644.    The functions end point and an increasing SAL line are used as
  2645.    indicators of the prologue's endpoint.

  2646.    This code is based on the function refine_prologue_limit
  2647.    (found in ia64).  */

  2648. CORE_ADDR
  2649. skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
  2650. {
  2651.   struct symtab_and_line prologue_sal;
  2652.   CORE_ADDR start_pc;
  2653.   CORE_ADDR end_pc;
  2654.   const struct block *bl;

  2655.   /* Get an initial range for the function.  */
  2656.   find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
  2657.   start_pc += gdbarch_deprecated_function_start_offset (gdbarch);

  2658.   prologue_sal = find_pc_line (start_pc, 0);
  2659.   if (prologue_sal.line != 0)
  2660.     {
  2661.       /* For languages other than assembly, treat two consecutive line
  2662.          entries at the same address as a zero-instruction prologue.
  2663.          The GNU assembler emits separate line notes for each instruction
  2664.          in a multi-instruction macro, but compilers generally will not
  2665.          do this.  */
  2666.       if (prologue_sal.symtab->language != language_asm)
  2667.         {
  2668.           struct linetable *linetable = SYMTAB_LINETABLE (prologue_sal.symtab);
  2669.           int idx = 0;

  2670.           /* Skip any earlier lines, and any end-of-sequence marker
  2671.              from a previous function.  */
  2672.           while (linetable->item[idx].pc != prologue_sal.pc
  2673.                  || linetable->item[idx].line == 0)
  2674.             idx++;

  2675.           if (idx+1 < linetable->nitems
  2676.               && linetable->item[idx+1].line != 0
  2677.               && linetable->item[idx+1].pc == start_pc)
  2678.             return start_pc;
  2679.         }

  2680.       /* If there is only one sal that covers the entire function,
  2681.          then it is probably a single line function, like
  2682.          "foo(){}".  */
  2683.       if (prologue_sal.end >= end_pc)
  2684.         return 0;

  2685.       while (prologue_sal.end < end_pc)
  2686.         {
  2687.           struct symtab_and_line sal;

  2688.           sal = find_pc_line (prologue_sal.end, 0);
  2689.           if (sal.line == 0)
  2690.             break;
  2691.           /* Assume that a consecutive SAL for the same (or larger)
  2692.              line mark the prologue -> body transition.  */
  2693.           if (sal.line >= prologue_sal.line)
  2694.             break;
  2695.           /* Likewise if we are in a different symtab altogether
  2696.              (e.g. within a file included via #include).  */
  2697.           if (sal.symtab != prologue_sal.symtab)
  2698.             break;

  2699.           /* The line number is smaller.  Check that it's from the
  2700.              same function, not something inlined.  If it's inlined,
  2701.              then there is no point comparing the line numbers.  */
  2702.           bl = block_for_pc (prologue_sal.end);
  2703.           while (bl)
  2704.             {
  2705.               if (block_inlined_p (bl))
  2706.                 break;
  2707.               if (BLOCK_FUNCTION (bl))
  2708.                 {
  2709.                   bl = NULL;
  2710.                   break;
  2711.                 }
  2712.               bl = BLOCK_SUPERBLOCK (bl);
  2713.             }
  2714.           if (bl != NULL)
  2715.             break;

  2716.           /* The case in which compiler's optimizer/scheduler has
  2717.              moved instructions into the prologue.  We look ahead in
  2718.              the function looking for address ranges whose
  2719.              corresponding line number is less the first one that we
  2720.              found for the function.  This is more conservative then
  2721.              refine_prologue_limit which scans a large number of SALs
  2722.              looking for any in the prologue.  */
  2723.           prologue_sal = sal;
  2724.         }
  2725.     }

  2726.   if (prologue_sal.end < end_pc)
  2727.     /* Return the end of this line, or zero if we could not find a
  2728.        line.  */
  2729.     return prologue_sal.end;
  2730.   else
  2731.     /* Don't return END_PC, which is past the end of the function.  */
  2732.     return prologue_sal.pc;
  2733. }

  2734. /* If P is of the form "operator[ \t]+..." where `...' is
  2735.    some legitimate operator text, return a pointer to the
  2736.    beginning of the substring of the operator text.
  2737.    Otherwise, return "".  */

  2738. static const char *
  2739. operator_chars (const char *p, const char **end)
  2740. {
  2741.   *end = "";
  2742.   if (strncmp (p, "operator", 8))
  2743.     return *end;
  2744.   p += 8;

  2745.   /* Don't get faked out by `operator' being part of a longer
  2746.      identifier.  */
  2747.   if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
  2748.     return *end;

  2749.   /* Allow some whitespace between `operator' and the operator symbol.  */
  2750.   while (*p == ' ' || *p == '\t')
  2751.     p++;

  2752.   /* Recognize 'operator TYPENAME'.  */

  2753.   if (isalpha (*p) || *p == '_' || *p == '$')
  2754.     {
  2755.       const char *q = p + 1;

  2756.       while (isalnum (*q) || *q == '_' || *q == '$')
  2757.         q++;
  2758.       *end = q;
  2759.       return p;
  2760.     }

  2761.   while (*p)
  2762.     switch (*p)
  2763.       {
  2764.       case '\\':                        /* regexp quoting */
  2765.         if (p[1] == '*')
  2766.           {
  2767.             if (p[2] == '=')                /* 'operator\*=' */
  2768.               *end = p + 3;
  2769.             else                        /* 'operator\*'  */
  2770.               *end = p + 2;
  2771.             return p;
  2772.           }
  2773.         else if (p[1] == '[')
  2774.           {
  2775.             if (p[2] == ']')
  2776.               error (_("mismatched quoting on brackets, "
  2777.                        "try 'operator\\[\\]'"));
  2778.             else if (p[2] == '\\' && p[3] == ']')
  2779.               {
  2780.                 *end = p + 4;        /* 'operator\[\]' */
  2781.                 return p;
  2782.               }
  2783.             else
  2784.               error (_("nothing is allowed between '[' and ']'"));
  2785.           }
  2786.         else
  2787.           {
  2788.             /* Gratuitous qoute: skip it and move on.  */
  2789.             p++;
  2790.             continue;
  2791.           }
  2792.         break;
  2793.       case '!':
  2794.       case '=':
  2795.       case '*':
  2796.       case '/':
  2797.       case '%':
  2798.       case '^':
  2799.         if (p[1] == '=')
  2800.           *end = p + 2;
  2801.         else
  2802.           *end = p + 1;
  2803.         return p;
  2804.       case '<':
  2805.       case '>':
  2806.       case '+':
  2807.       case '-':
  2808.       case '&':
  2809.       case '|':
  2810.         if (p[0] == '-' && p[1] == '>')
  2811.           {
  2812.             /* Struct pointer member operator 'operator->'.  */
  2813.             if (p[2] == '*')
  2814.               {
  2815.                 *end = p + 3;        /* 'operator->*' */
  2816.                 return p;
  2817.               }
  2818.             else if (p[2] == '\\')
  2819.               {
  2820.                 *end = p + 4;        /* Hopefully 'operator->\*' */
  2821.                 return p;
  2822.               }
  2823.             else
  2824.               {
  2825.                 *end = p + 2;        /* 'operator->' */
  2826.                 return p;
  2827.               }
  2828.           }
  2829.         if (p[1] == '=' || p[1] == p[0])
  2830.           *end = p + 2;
  2831.         else
  2832.           *end = p + 1;
  2833.         return p;
  2834.       case '~':
  2835.       case ',':
  2836.         *end = p + 1;
  2837.         return p;
  2838.       case '(':
  2839.         if (p[1] != ')')
  2840.           error (_("`operator ()' must be specified "
  2841.                    "without whitespace in `()'"));
  2842.         *end = p + 2;
  2843.         return p;
  2844.       case '?':
  2845.         if (p[1] != ':')
  2846.           error (_("`operator ?:' must be specified "
  2847.                    "without whitespace in `?:'"));
  2848.         *end = p + 2;
  2849.         return p;
  2850.       case '[':
  2851.         if (p[1] != ']')
  2852.           error (_("`operator []' must be specified "
  2853.                    "without whitespace in `[]'"));
  2854.         *end = p + 2;
  2855.         return p;
  2856.       default:
  2857.         error (_("`operator %s' not supported"), p);
  2858.         break;
  2859.       }

  2860.   *end = "";
  2861.   return *end;
  2862. }


  2863. /* Cache to watch for file names already seen by filename_seen.  */

  2864. struct filename_seen_cache
  2865. {
  2866.   /* Table of files seen so far.  */
  2867.   htab_t tab;
  2868.   /* Initial size of the table.  It automagically grows from here.  */
  2869. #define INITIAL_FILENAME_SEEN_CACHE_SIZE 100
  2870. };

  2871. /* filename_seen_cache constructor.  */

  2872. static struct filename_seen_cache *
  2873. create_filename_seen_cache (void)
  2874. {
  2875.   struct filename_seen_cache *cache;

  2876.   cache = XNEW (struct filename_seen_cache);
  2877.   cache->tab = htab_create_alloc (INITIAL_FILENAME_SEEN_CACHE_SIZE,
  2878.                                   filename_hash, filename_eq,
  2879.                                   NULL, xcalloc, xfree);

  2880.   return cache;
  2881. }

  2882. /* Empty the cache, but do not delete it.  */

  2883. static void
  2884. clear_filename_seen_cache (struct filename_seen_cache *cache)
  2885. {
  2886.   htab_empty (cache->tab);
  2887. }

  2888. /* filename_seen_cache destructor.
  2889.    This takes a void * argument as it is generally used as a cleanup.  */

  2890. static void
  2891. delete_filename_seen_cache (void *ptr)
  2892. {
  2893.   struct filename_seen_cache *cache = ptr;

  2894.   htab_delete (cache->tab);
  2895.   xfree (cache);
  2896. }

  2897. /* If FILE is not already in the table of files in CACHE, return zero;
  2898.    otherwise return non-zero.  Optionally add FILE to the table if ADD
  2899.    is non-zero.

  2900.    NOTE: We don't manage space for FILE, we assume FILE lives as long
  2901.    as the caller needs.  */

  2902. static int
  2903. filename_seen (struct filename_seen_cache *cache, const char *file, int add)
  2904. {
  2905.   void **slot;

  2906.   /* Is FILE in tab?  */
  2907.   slot = htab_find_slot (cache->tab, file, add ? INSERT : NO_INSERT);
  2908.   if (*slot != NULL)
  2909.     return 1;

  2910.   /* No; maybe add it to tab.  */
  2911.   if (add)
  2912.     *slot = (char *) file;

  2913.   return 0;
  2914. }

  2915. /* Data structure to maintain printing state for output_source_filename.  */

  2916. struct output_source_filename_data
  2917. {
  2918.   /* Cache of what we've seen so far.  */
  2919.   struct filename_seen_cache *filename_seen_cache;

  2920.   /* Flag of whether we're printing the first one.  */
  2921.   int first;
  2922. };

  2923. /* Slave routine for sources_info.  Force line breaks at ,'s.
  2924.    NAME is the name to print.
  2925.    DATA contains the state for printing and watching for duplicates.  */

  2926. static void
  2927. output_source_filename (const char *name,
  2928.                         struct output_source_filename_data *data)
  2929. {
  2930.   /* Since a single source file can result in several partial symbol
  2931.      tables, we need to avoid printing it more than once.  Note: if
  2932.      some of the psymtabs are read in and some are not, it gets
  2933.      printed both under "Source files for which symbols have been
  2934.      read" and "Source files for which symbols will be read in on
  2935.      demand".  I consider this a reasonable way to deal with the
  2936.      situation.  I'm not sure whether this can also happen for
  2937.      symtabs; it doesn't hurt to check.  */

  2938.   /* Was NAME already seen?  */
  2939.   if (filename_seen (data->filename_seen_cache, name, 1))
  2940.     {
  2941.       /* Yes; don't print it again.  */
  2942.       return;
  2943.     }

  2944.   /* No; print it and reset *FIRST.  */
  2945.   if (! data->first)
  2946.     printf_filtered (", ");
  2947.   data->first = 0;

  2948.   wrap_here ("");
  2949.   fputs_filtered (name, gdb_stdout);
  2950. }

  2951. /* A callback for map_partial_symbol_filenames.  */

  2952. static void
  2953. output_partial_symbol_filename (const char *filename, const char *fullname,
  2954.                                 void *data)
  2955. {
  2956.   output_source_filename (fullname ? fullname : filename, data);
  2957. }

  2958. static void
  2959. sources_info (char *ignore, int from_tty)
  2960. {
  2961.   struct compunit_symtab *cu;
  2962.   struct symtab *s;
  2963.   struct objfile *objfile;
  2964.   struct output_source_filename_data data;
  2965.   struct cleanup *cleanups;

  2966.   if (!have_full_symbols () && !have_partial_symbols ())
  2967.     {
  2968.       error (_("No symbol table is loaded.  Use the \"file\" command."));
  2969.     }

  2970.   data.filename_seen_cache = create_filename_seen_cache ();
  2971.   cleanups = make_cleanup (delete_filename_seen_cache,
  2972.                            data.filename_seen_cache);

  2973.   printf_filtered ("Source files for which symbols have been read in:\n\n");

  2974.   data.first = 1;
  2975.   ALL_FILETABS (objfile, cu, s)
  2976.   {
  2977.     const char *fullname = symtab_to_fullname (s);

  2978.     output_source_filename (fullname, &data);
  2979.   }
  2980.   printf_filtered ("\n\n");

  2981.   printf_filtered ("Source files for which symbols "
  2982.                    "will be read in on demand:\n\n");

  2983.   clear_filename_seen_cache (data.filename_seen_cache);
  2984.   data.first = 1;
  2985.   map_symbol_filenames (output_partial_symbol_filename, &data,
  2986.                         1 /*need_fullname*/);
  2987.   printf_filtered ("\n");

  2988.   do_cleanups (cleanups);
  2989. }

  2990. /* Compare FILE against all the NFILES entries of FILES.  If BASENAMES is
  2991.    non-zero compare only lbasename of FILES.  */

  2992. static int
  2993. file_matches (const char *file, const char *files[], int nfiles, int basenames)
  2994. {
  2995.   int i;

  2996.   if (file != NULL && nfiles != 0)
  2997.     {
  2998.       for (i = 0; i < nfiles; i++)
  2999.         {
  3000.           if (compare_filenames_for_search (file, (basenames
  3001.                                                    ? lbasename (files[i])
  3002.                                                    : files[i])))
  3003.             return 1;
  3004.         }
  3005.     }
  3006.   else if (nfiles == 0)
  3007.     return 1;
  3008.   return 0;
  3009. }

  3010. /* Free any memory associated with a search.  */

  3011. void
  3012. free_search_symbols (struct symbol_search *symbols)
  3013. {
  3014.   struct symbol_search *p;
  3015.   struct symbol_search *next;

  3016.   for (p = symbols; p != NULL; p = next)
  3017.     {
  3018.       next = p->next;
  3019.       xfree (p);
  3020.     }
  3021. }

  3022. static void
  3023. do_free_search_symbols_cleanup (void *symbolsp)
  3024. {
  3025.   struct symbol_search *symbols = *(struct symbol_search **) symbolsp;

  3026.   free_search_symbols (symbols);
  3027. }

  3028. struct cleanup *
  3029. make_cleanup_free_search_symbols (struct symbol_search **symbolsp)
  3030. {
  3031.   return make_cleanup (do_free_search_symbols_cleanup, symbolsp);
  3032. }

  3033. /* Helper function for sort_search_symbols_remove_dups and qsort.  Can only
  3034.    sort symbols, not minimal symbols.  */

  3035. static int
  3036. compare_search_syms (const void *sa, const void *sb)
  3037. {
  3038.   struct symbol_search *sym_a = *(struct symbol_search **) sa;
  3039.   struct symbol_search *sym_b = *(struct symbol_search **) sb;
  3040.   int c;

  3041.   c = FILENAME_CMP (symbol_symtab (sym_a->symbol)->filename,
  3042.                     symbol_symtab (sym_b->symbol)->filename);
  3043.   if (c != 0)
  3044.     return c;

  3045.   if (sym_a->block != sym_b->block)
  3046.     return sym_a->block - sym_b->block;

  3047.   return strcmp (SYMBOL_PRINT_NAME (sym_a->symbol),
  3048.                  SYMBOL_PRINT_NAME (sym_b->symbol));
  3049. }

  3050. /* Sort the NFOUND symbols in list FOUND and remove duplicates.
  3051.    The duplicates are freed, and the new list is returned in
  3052.    *NEW_HEAD, *NEW_TAIL.  */

  3053. static void
  3054. sort_search_symbols_remove_dups (struct symbol_search *found, int nfound,
  3055.                                  struct symbol_search **new_head,
  3056.                                  struct symbol_search **new_tail)
  3057. {
  3058.   struct symbol_search **symbols, *symp, *old_next;
  3059.   int i, j, nunique;

  3060.   gdb_assert (found != NULL && nfound > 0);

  3061.   /* Build an array out of the list so we can easily sort them.  */
  3062.   symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
  3063.                                                * nfound);
  3064.   symp = found;
  3065.   for (i = 0; i < nfound; i++)
  3066.     {
  3067.       gdb_assert (symp != NULL);
  3068.       gdb_assert (symp->block >= 0 && symp->block <= 1);
  3069.       symbols[i] = symp;
  3070.       symp = symp->next;
  3071.     }
  3072.   gdb_assert (symp == NULL);

  3073.   qsort (symbols, nfound, sizeof (struct symbol_search *),
  3074.          compare_search_syms);

  3075.   /* Collapse out the dups.  */
  3076.   for (i = 1, j = 1; i < nfound; ++i)
  3077.     {
  3078.       if (compare_search_syms (&symbols[j - 1], &symbols[i]) != 0)
  3079.         symbols[j++] = symbols[i];
  3080.       else
  3081.         xfree (symbols[i]);
  3082.     }
  3083.   nunique = j;
  3084.   symbols[j - 1]->next = NULL;

  3085.   /* Rebuild the linked list.  */
  3086.   for (i = 0; i < nunique - 1; i++)
  3087.     symbols[i]->next = symbols[i + 1];
  3088.   symbols[nunique - 1]->next = NULL;

  3089.   *new_head = symbols[0];
  3090.   *new_tail = symbols[nunique - 1];
  3091.   xfree (symbols);
  3092. }

  3093. /* An object of this type is passed as the user_data to the
  3094.    expand_symtabs_matching method.  */
  3095. struct search_symbols_data
  3096. {
  3097.   int nfiles;
  3098.   const char **files;

  3099.   /* It is true if PREG contains valid data, false otherwise.  */
  3100.   unsigned preg_p : 1;
  3101.   regex_t preg;
  3102. };

  3103. /* A callback for expand_symtabs_matching.  */

  3104. static int
  3105. search_symbols_file_matches (const char *filename, void *user_data,
  3106.                              int basenames)
  3107. {
  3108.   struct search_symbols_data *data = user_data;

  3109.   return file_matches (filename, data->files, data->nfiles, basenames);
  3110. }

  3111. /* A callback for expand_symtabs_matching.  */

  3112. static int
  3113. search_symbols_name_matches (const char *symname, void *user_data)
  3114. {
  3115.   struct search_symbols_data *data = user_data;

  3116.   return !data->preg_p || regexec (&data->preg, symname, 0, NULL, 0) == 0;
  3117. }

  3118. /* Search the symbol table for matches to the regular expression REGEXP,
  3119.    returning the results in *MATCHES.

  3120.    Only symbols of KIND are searched:
  3121.    VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
  3122.                       and constants (enums)
  3123.    FUNCTIONS_DOMAIN - search all functions
  3124.    TYPES_DOMAIN     - search all type names
  3125.    ALL_DOMAIN       - an internal error for this function

  3126.    free_search_symbols should be called when *MATCHES is no longer needed.

  3127.    Within each file the results are sorted locally; each symtab's global and
  3128.    static blocks are separately alphabetized.
  3129.    Duplicate entries are removed.  */

  3130. void
  3131. search_symbols (const char *regexp, enum search_domain kind,
  3132.                 int nfiles, const char *files[],
  3133.                 struct symbol_search **matches)
  3134. {
  3135.   struct compunit_symtab *cust;
  3136.   const struct blockvector *bv;
  3137.   struct block *b;
  3138.   int i = 0;
  3139.   struct block_iterator iter;
  3140.   struct symbol *sym;
  3141.   struct objfile *objfile;
  3142.   struct minimal_symbol *msymbol;
  3143.   int found_misc = 0;
  3144.   static const enum minimal_symbol_type types[]
  3145.     = {mst_data, mst_text, mst_abs};
  3146.   static const enum minimal_symbol_type types2[]
  3147.     = {mst_bss, mst_file_text, mst_abs};
  3148.   static const enum minimal_symbol_type types3[]
  3149.     = {mst_file_data, mst_solib_trampoline, mst_abs};
  3150.   static const enum minimal_symbol_type types4[]
  3151.     = {mst_file_bss, mst_text_gnu_ifunc, mst_abs};
  3152.   enum minimal_symbol_type ourtype;
  3153.   enum minimal_symbol_type ourtype2;
  3154.   enum minimal_symbol_type ourtype3;
  3155.   enum minimal_symbol_type ourtype4;
  3156.   struct symbol_search *found;
  3157.   struct symbol_search *tail;
  3158.   struct search_symbols_data datum;
  3159.   int nfound;

  3160.   /* OLD_CHAIN .. RETVAL_CHAIN is always freed, RETVAL_CHAIN .. current
  3161.      CLEANUP_CHAIN is freed only in the case of an error.  */
  3162.   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
  3163.   struct cleanup *retval_chain;

  3164.   gdb_assert (kind <= TYPES_DOMAIN);

  3165.   ourtype = types[kind];
  3166.   ourtype2 = types2[kind];
  3167.   ourtype3 = types3[kind];
  3168.   ourtype4 = types4[kind];

  3169.   *matches = NULL;
  3170.   datum.preg_p = 0;

  3171.   if (regexp != NULL)
  3172.     {
  3173.       /* Make sure spacing is right for C++ operators.
  3174.          This is just a courtesy to make the matching less sensitive
  3175.          to how many spaces the user leaves between 'operator'
  3176.          and <TYPENAME> or <OPERATOR>.  */
  3177.       const char *opend;
  3178.       const char *opname = operator_chars (regexp, &opend);
  3179.       int errcode;

  3180.       if (*opname)
  3181.         {
  3182.           int fix = -1;                /* -1 means ok; otherwise number of
  3183.                                     spaces needed.  */

  3184.           if (isalpha (*opname) || *opname == '_' || *opname == '$')
  3185.             {
  3186.               /* There should 1 space between 'operator' and 'TYPENAME'.  */
  3187.               if (opname[-1] != ' ' || opname[-2] == ' ')
  3188.                 fix = 1;
  3189.             }
  3190.           else
  3191.             {
  3192.               /* There should 0 spaces between 'operator' and 'OPERATOR'.  */
  3193.               if (opname[-1] == ' ')
  3194.                 fix = 0;
  3195.             }
  3196.           /* If wrong number of spaces, fix it.  */
  3197.           if (fix >= 0)
  3198.             {
  3199.               char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);

  3200.               sprintf (tmp, "operator%.*s%s", fix, " ", opname);
  3201.               regexp = tmp;
  3202.             }
  3203.         }

  3204.       errcode = regcomp (&datum.preg, regexp,
  3205.                          REG_NOSUB | (case_sensitivity == case_sensitive_off
  3206.                                       ? REG_ICASE : 0));
  3207.       if (errcode != 0)
  3208.         {
  3209.           char *err = get_regcomp_error (errcode, &datum.preg);

  3210.           make_cleanup (xfree, err);
  3211.           error (_("Invalid regexp (%s): %s"), err, regexp);
  3212.         }
  3213.       datum.preg_p = 1;
  3214.       make_regfree_cleanup (&datum.preg);
  3215.     }

  3216.   /* Search through the partial symtabs *first* for all symbols
  3217.      matching the regexp.  That way we don't have to reproduce all of
  3218.      the machinery below.  */

  3219.   datum.nfiles = nfiles;
  3220.   datum.files = files;
  3221.   expand_symtabs_matching ((nfiles == 0
  3222.                             ? NULL
  3223.                             : search_symbols_file_matches),
  3224.                            search_symbols_name_matches,
  3225.                            kind, &datum);

  3226.   /* Here, we search through the minimal symbol tables for functions
  3227.      and variables that match, and force their symbols to be read.
  3228.      This is in particular necessary for demangled variable names,
  3229.      which are no longer put into the partial symbol tables.
  3230.      The symbol will then be found during the scan of symtabs below.

  3231.      For functions, find_pc_symtab should succeed if we have debug info
  3232.      for the function, for variables we have to call
  3233.      lookup_symbol_in_objfile_from_linkage_name to determine if the variable
  3234.      has debug info.
  3235.      If the lookup fails, set found_misc so that we will rescan to print
  3236.      any matching symbols without debug info.
  3237.      We only search the objfile the msymbol came from, we no longer search
  3238.      all objfiles.  In large programs (1000s of shared libs) searching all
  3239.      objfiles is not worth the pain.  */

  3240.   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
  3241.     {
  3242.       ALL_MSYMBOLS (objfile, msymbol)
  3243.       {
  3244.         QUIT;

  3245.         if (msymbol->created_by_gdb)
  3246.           continue;

  3247.         if (MSYMBOL_TYPE (msymbol) == ourtype
  3248.             || MSYMBOL_TYPE (msymbol) == ourtype2
  3249.             || MSYMBOL_TYPE (msymbol) == ourtype3
  3250.             || MSYMBOL_TYPE (msymbol) == ourtype4)
  3251.           {
  3252.             if (!datum.preg_p
  3253.                 || regexec (&datum.preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
  3254.                             NULL, 0) == 0)
  3255.               {
  3256.                 /* Note: An important side-effect of these lookup functions
  3257.                    is to expand the symbol table if msymbol is found, for the
  3258.                    benefit of the next loop on ALL_COMPUNITS.  */
  3259.                 if (kind == FUNCTIONS_DOMAIN
  3260.                     ? (find_pc_compunit_symtab
  3261.                        (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL)
  3262.                     : (lookup_symbol_in_objfile_from_linkage_name
  3263.                        (objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
  3264.                        == NULL))
  3265.                   found_misc = 1;
  3266.               }
  3267.           }
  3268.       }
  3269.     }

  3270.   found = NULL;
  3271.   tail = NULL;
  3272.   nfound = 0;
  3273.   retval_chain = make_cleanup_free_search_symbols (&found);

  3274.   ALL_COMPUNITS (objfile, cust)
  3275.   {
  3276.     bv = COMPUNIT_BLOCKVECTOR (cust);
  3277.     for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
  3278.       {
  3279.         b = BLOCKVECTOR_BLOCK (bv, i);
  3280.         ALL_BLOCK_SYMBOLS (b, iter, sym)
  3281.           {
  3282.             struct symtab *real_symtab = symbol_symtab (sym);

  3283.             QUIT;

  3284.             /* Check first sole REAL_SYMTAB->FILENAME.  It does not need to be
  3285.                a substring of symtab_to_fullname as it may contain "./" etc.  */
  3286.             if ((file_matches (real_symtab->filename, files, nfiles, 0)
  3287.                  || ((basenames_may_differ
  3288.                       || file_matches (lbasename (real_symtab->filename),
  3289.                                        files, nfiles, 1))
  3290.                      && file_matches (symtab_to_fullname (real_symtab),
  3291.                                       files, nfiles, 0)))
  3292.                 && ((!datum.preg_p
  3293.                      || regexec (&datum.preg, SYMBOL_NATURAL_NAME (sym), 0,
  3294.                                  NULL, 0) == 0)
  3295.                     && ((kind == VARIABLES_DOMAIN
  3296.                          && SYMBOL_CLASS (sym) != LOC_TYPEDEF
  3297.                          && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
  3298.                          && SYMBOL_CLASS (sym) != LOC_BLOCK
  3299.                          /* LOC_CONST can be used for more than just enums,
  3300.                             e.g., c++ static const members.
  3301.                             We only want to skip enums here.  */
  3302.                          && !(SYMBOL_CLASS (sym) == LOC_CONST
  3303.                               && (TYPE_CODE (SYMBOL_TYPE (sym))
  3304.                                   == TYPE_CODE_ENUM)))
  3305.                         || (kind == FUNCTIONS_DOMAIN
  3306.                             && SYMBOL_CLASS (sym) == LOC_BLOCK)
  3307.                         || (kind == TYPES_DOMAIN
  3308.                             && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
  3309.               {
  3310.                 /* match */
  3311.                 struct symbol_search *psr = (struct symbol_search *)
  3312.                   xmalloc (sizeof (struct symbol_search));
  3313.                 psr->block = i;
  3314.                 psr->symbol = sym;
  3315.                 memset (&psr->msymbol, 0, sizeof (psr->msymbol));
  3316.                 psr->next = NULL;
  3317.                 if (tail == NULL)
  3318.                   found = psr;
  3319.                 else
  3320.                   tail->next = psr;
  3321.                 tail = psr;
  3322.                 nfound ++;
  3323.               }
  3324.           }
  3325.       }
  3326.   }

  3327.   if (found != NULL)
  3328.     {
  3329.       sort_search_symbols_remove_dups (found, nfound, &found, &tail);
  3330.       /* Note: nfound is no longer useful beyond this point.  */
  3331.     }

  3332.   /* If there are no eyes, avoid all contact.  I mean, if there are
  3333.      no debug symbols, then add matching minsyms.  */

  3334.   if (found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
  3335.     {
  3336.       ALL_MSYMBOLS (objfile, msymbol)
  3337.       {
  3338.         QUIT;

  3339.         if (msymbol->created_by_gdb)
  3340.           continue;

  3341.         if (MSYMBOL_TYPE (msymbol) == ourtype
  3342.             || MSYMBOL_TYPE (msymbol) == ourtype2
  3343.             || MSYMBOL_TYPE (msymbol) == ourtype3
  3344.             || MSYMBOL_TYPE (msymbol) == ourtype4)
  3345.           {
  3346.             if (!datum.preg_p
  3347.                 || regexec (&datum.preg, MSYMBOL_NATURAL_NAME (msymbol), 0,
  3348.                             NULL, 0) == 0)
  3349.               {
  3350.                 /* For functions we can do a quick check of whether the
  3351.                    symbol might be found via find_pc_symtab.  */
  3352.                 if (kind != FUNCTIONS_DOMAIN
  3353.                     || (find_pc_compunit_symtab
  3354.                         (MSYMBOL_VALUE_ADDRESS (objfile, msymbol)) == NULL))
  3355.                   {
  3356.                     if (lookup_symbol_in_objfile_from_linkage_name
  3357.                         (objfile, MSYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
  3358.                         == NULL)
  3359.                       {
  3360.                         /* match */
  3361.                         struct symbol_search *psr = (struct symbol_search *)
  3362.                           xmalloc (sizeof (struct symbol_search));
  3363.                         psr->block = i;
  3364.                         psr->msymbol.minsym = msymbol;
  3365.                         psr->msymbol.objfile = objfile;
  3366.                         psr->symbol = NULL;
  3367.                         psr->next = NULL;
  3368.                         if (tail == NULL)
  3369.                           found = psr;
  3370.                         else
  3371.                           tail->next = psr;
  3372.                         tail = psr;
  3373.                       }
  3374.                   }
  3375.               }
  3376.           }
  3377.       }
  3378.     }

  3379.   discard_cleanups (retval_chain);
  3380.   do_cleanups (old_chain);
  3381.   *matches = found;
  3382. }

  3383. /* Helper function for symtab_symbol_info, this function uses
  3384.    the data returned from search_symbols() to print information
  3385.    regarding the match to gdb_stdout.  */

  3386. static void
  3387. print_symbol_info (enum search_domain kind,
  3388.                    struct symbol *sym,
  3389.                    int block, const char *last)
  3390. {
  3391.   struct symtab *s = symbol_symtab (sym);
  3392.   const char *s_filename = symtab_to_filename_for_display (s);

  3393.   if (last == NULL || filename_cmp (last, s_filename) != 0)
  3394.     {
  3395.       fputs_filtered ("\nFile ", gdb_stdout);
  3396.       fputs_filtered (s_filename, gdb_stdout);
  3397.       fputs_filtered (":\n", gdb_stdout);
  3398.     }

  3399.   if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
  3400.     printf_filtered ("static ");

  3401.   /* Typedef that is not a C++ class.  */
  3402.   if (kind == TYPES_DOMAIN
  3403.       && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
  3404.     typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
  3405.   /* variable, func, or typedef-that-is-c++-class.  */
  3406.   else if (kind < TYPES_DOMAIN
  3407.            || (kind == TYPES_DOMAIN
  3408.                && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
  3409.     {
  3410.       type_print (SYMBOL_TYPE (sym),
  3411.                   (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  3412.                    ? "" : SYMBOL_PRINT_NAME (sym)),
  3413.                   gdb_stdout, 0);

  3414.       printf_filtered (";\n");
  3415.     }
  3416. }

  3417. /* This help function for symtab_symbol_info() prints information
  3418.    for non-debugging symbols to gdb_stdout.  */

  3419. static void
  3420. print_msymbol_info (struct bound_minimal_symbol msymbol)
  3421. {
  3422.   struct gdbarch *gdbarch = get_objfile_arch (msymbol.objfile);
  3423.   char *tmp;

  3424.   if (gdbarch_addr_bit (gdbarch) <= 32)
  3425.     tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol)
  3426.                              & (CORE_ADDR) 0xffffffff,
  3427.                              8);
  3428.   else
  3429.     tmp = hex_string_custom (BMSYMBOL_VALUE_ADDRESS (msymbol),
  3430.                              16);
  3431.   printf_filtered ("%s  %s\n",
  3432.                    tmp, MSYMBOL_PRINT_NAME (msymbol.minsym));
  3433. }

  3434. /* This is the guts of the commands "info functions", "info types", and
  3435.    "info variables".  It calls search_symbols to find all matches and then
  3436.    print_[m]symbol_info to print out some useful information about the
  3437.    matches.  */

  3438. static void
  3439. symtab_symbol_info (char *regexp, enum search_domain kind, int from_tty)
  3440. {
  3441.   static const char * const classnames[] =
  3442.     {"variable", "function", "type"};
  3443.   struct symbol_search *symbols;
  3444.   struct symbol_search *p;
  3445.   struct cleanup *old_chain;
  3446.   const char *last_filename = NULL;
  3447.   int first = 1;

  3448.   gdb_assert (kind <= TYPES_DOMAIN);

  3449.   /* Must make sure that if we're interrupted, symbols gets freed.  */
  3450.   search_symbols (regexp, kind, 0, NULL, &symbols);
  3451.   old_chain = make_cleanup_free_search_symbols (&symbols);

  3452.   if (regexp != NULL)
  3453.     printf_filtered (_("All %ss matching regular expression \"%s\":\n"),
  3454.                      classnames[kind], regexp);
  3455.   else
  3456.     printf_filtered (_("All defined %ss:\n"), classnames[kind]);

  3457.   for (p = symbols; p != NULL; p = p->next)
  3458.     {
  3459.       QUIT;

  3460.       if (p->msymbol.minsym != NULL)
  3461.         {
  3462.           if (first)
  3463.             {
  3464.               printf_filtered (_("\nNon-debugging symbols:\n"));
  3465.               first = 0;
  3466.             }
  3467.           print_msymbol_info (p->msymbol);
  3468.         }
  3469.       else
  3470.         {
  3471.           print_symbol_info (kind,
  3472.                              p->symbol,
  3473.                              p->block,
  3474.                              last_filename);
  3475.           last_filename
  3476.             = symtab_to_filename_for_display (symbol_symtab (p->symbol));
  3477.         }
  3478.     }

  3479.   do_cleanups (old_chain);
  3480. }

  3481. static void
  3482. variables_info (char *regexp, int from_tty)
  3483. {
  3484.   symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
  3485. }

  3486. static void
  3487. functions_info (char *regexp, int from_tty)
  3488. {
  3489.   symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
  3490. }


  3491. static void
  3492. types_info (char *regexp, int from_tty)
  3493. {
  3494.   symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
  3495. }

  3496. /* Breakpoint all functions matching regular expression.  */

  3497. void
  3498. rbreak_command_wrapper (char *regexp, int from_tty)
  3499. {
  3500.   rbreak_command (regexp, from_tty);
  3501. }

  3502. /* A cleanup function that calls end_rbreak_breakpoints.  */

  3503. static void
  3504. do_end_rbreak_breakpoints (void *ignore)
  3505. {
  3506.   end_rbreak_breakpoints ();
  3507. }

  3508. static void
  3509. rbreak_command (char *regexp, int from_tty)
  3510. {
  3511.   struct symbol_search *ss;
  3512.   struct symbol_search *p;
  3513.   struct cleanup *old_chain;
  3514.   char *string = NULL;
  3515.   int len = 0;
  3516.   const char **files = NULL;
  3517.   const char *file_name;
  3518.   int nfiles = 0;

  3519.   if (regexp)
  3520.     {
  3521.       char *colon = strchr (regexp, ':');

  3522.       if (colon && *(colon + 1) != ':')
  3523.         {
  3524.           int colon_index;
  3525.           char *local_name;

  3526.           colon_index = colon - regexp;
  3527.           local_name = alloca (colon_index + 1);
  3528.           memcpy (local_name, regexp, colon_index);
  3529.           local_name[colon_index--] = 0;
  3530.           while (isspace (local_name[colon_index]))
  3531.             local_name[colon_index--] = 0;
  3532.           file_name = local_name;
  3533.           files = &file_name;
  3534.           nfiles = 1;
  3535.           regexp = skip_spaces (colon + 1);
  3536.         }
  3537.     }

  3538.   search_symbols (regexp, FUNCTIONS_DOMAIN, nfiles, files, &ss);
  3539.   old_chain = make_cleanup_free_search_symbols (&ss);
  3540.   make_cleanup (free_current_contents, &string);

  3541.   start_rbreak_breakpoints ();
  3542.   make_cleanup (do_end_rbreak_breakpoints, NULL);
  3543.   for (p = ss; p != NULL; p = p->next)
  3544.     {
  3545.       if (p->msymbol.minsym == NULL)
  3546.         {
  3547.           struct symtab *symtab = symbol_symtab (p->symbol);
  3548.           const char *fullname = symtab_to_fullname (symtab);

  3549.           int newlen = (strlen (fullname)
  3550.                         + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
  3551.                         + 4);

  3552.           if (newlen > len)
  3553.             {
  3554.               string = xrealloc (string, newlen);
  3555.               len = newlen;
  3556.             }
  3557.           strcpy (string, fullname);
  3558.           strcat (string, ":'");
  3559.           strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
  3560.           strcat (string, "'");
  3561.           break_command (string, from_tty);
  3562.           print_symbol_info (FUNCTIONS_DOMAIN,
  3563.                              p->symbol,
  3564.                              p->block,
  3565.                              symtab_to_filename_for_display (symtab));
  3566.         }
  3567.       else
  3568.         {
  3569.           int newlen = (strlen (MSYMBOL_LINKAGE_NAME (p->msymbol.minsym)) + 3);

  3570.           if (newlen > len)
  3571.             {
  3572.               string = xrealloc (string, newlen);
  3573.               len = newlen;
  3574.             }
  3575.           strcpy (string, "'");
  3576.           strcat (string, MSYMBOL_LINKAGE_NAME (p->msymbol.minsym));
  3577.           strcat (string, "'");

  3578.           break_command (string, from_tty);
  3579.           printf_filtered ("<function, no debug info> %s;\n",
  3580.                            MSYMBOL_PRINT_NAME (p->msymbol.minsym));
  3581.         }
  3582.     }

  3583.   do_cleanups (old_chain);
  3584. }


  3585. /* Evaluate if NAME matches SYM_TEXT and SYM_TEXT_LEN.

  3586.    Either sym_text[sym_text_len] != '(' and then we search for any
  3587.    symbol starting with SYM_TEXT text.

  3588.    Otherwise sym_text[sym_text_len] == '(' and then we require symbol name to
  3589.    be terminated at that point.  Partial symbol tables do not have parameters
  3590.    information.  */

  3591. static int
  3592. compare_symbol_name (const char *name, const char *sym_text, int sym_text_len)
  3593. {
  3594.   int (*ncmp) (const char *, const char *, size_t);

  3595.   ncmp = (case_sensitivity == case_sensitive_on ? strncmp : strncasecmp);

  3596.   if (ncmp (name, sym_text, sym_text_len) != 0)
  3597.     return 0;

  3598.   if (sym_text[sym_text_len] == '(')
  3599.     {
  3600.       /* User searches for `name(someth...'.  Require NAME to be terminated.
  3601.          Normally psymtabs and gdbindex have no parameter types so '\0' will be
  3602.          present but accept even parameters presence.  In this case this
  3603.          function is in fact strcmp_iw but whitespace skipping is not supported
  3604.          for tab completion.  */

  3605.       if (name[sym_text_len] != '\0' && name[sym_text_len] != '(')
  3606.         return 0;
  3607.     }

  3608.   return 1;
  3609. }

  3610. /* Free any memory associated with a completion list.  */

  3611. static void
  3612. free_completion_list (VEC (char_ptr) **list_ptr)
  3613. {
  3614.   int i;
  3615.   char *p;

  3616.   for (i = 0; VEC_iterate (char_ptr, *list_ptr, i, p); ++i)
  3617.     xfree (p);
  3618.   VEC_free (char_ptr, *list_ptr);
  3619. }

  3620. /* Callback for make_cleanup.  */

  3621. static void
  3622. do_free_completion_list (void *list)
  3623. {
  3624.   free_completion_list (list);
  3625. }

  3626. /* Helper routine for make_symbol_completion_list.  */

  3627. static VEC (char_ptr) *return_val;

  3628. #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
  3629.       completion_list_add_name \
  3630.         (SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))

  3631. #define MCOMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
  3632.       completion_list_add_name \
  3633.         (MSYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))

  3634. /*  Test to see if the symbol specified by SYMNAME (which is already
  3635.    demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
  3636.    characters.  If so, add it to the current completion list.  */

  3637. static void
  3638. completion_list_add_name (const char *symname,
  3639.                           const char *sym_text, int sym_text_len,
  3640.                           const char *text, const char *word)
  3641. {
  3642.   /* Clip symbols that cannot match.  */
  3643.   if (!compare_symbol_name (symname, sym_text, sym_text_len))
  3644.     return;

  3645.   /* We have a match for a completion, so add SYMNAME to the current list
  3646.      of matches.  Note that the name is moved to freshly malloc'd space.  */

  3647.   {
  3648.     char *new;

  3649.     if (word == sym_text)
  3650.       {
  3651.         new = xmalloc (strlen (symname) + 5);
  3652.         strcpy (new, symname);
  3653.       }
  3654.     else if (word > sym_text)
  3655.       {
  3656.         /* Return some portion of symname.  */
  3657.         new = xmalloc (strlen (symname) + 5);
  3658.         strcpy (new, symname + (word - sym_text));
  3659.       }
  3660.     else
  3661.       {
  3662.         /* Return some of SYM_TEXT plus symname.  */
  3663.         new = xmalloc (strlen (symname) + (sym_text - word) + 5);
  3664.         strncpy (new, word, sym_text - word);
  3665.         new[sym_text - word] = '\0';
  3666.         strcat (new, symname);
  3667.       }

  3668.     VEC_safe_push (char_ptr, return_val, new);
  3669.   }
  3670. }

  3671. /* ObjC: In case we are completing on a selector, look as the msymbol
  3672.    again and feed all the selectors into the mill.  */

  3673. static void
  3674. completion_list_objc_symbol (struct minimal_symbol *msymbol,
  3675.                              const char *sym_text, int sym_text_len,
  3676.                              const char *text, const char *word)
  3677. {
  3678.   static char *tmp = NULL;
  3679.   static unsigned int tmplen = 0;

  3680.   const char *method, *category, *selector;
  3681.   char *tmp2 = NULL;

  3682.   method = MSYMBOL_NATURAL_NAME (msymbol);

  3683.   /* Is it a method?  */
  3684.   if ((method[0] != '-') && (method[0] != '+'))
  3685.     return;

  3686.   if (sym_text[0] == '[')
  3687.     /* Complete on shortened method method.  */
  3688.     completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);

  3689.   while ((strlen (method) + 1) >= tmplen)
  3690.     {
  3691.       if (tmplen == 0)
  3692.         tmplen = 1024;
  3693.       else
  3694.         tmplen *= 2;
  3695.       tmp = xrealloc (tmp, tmplen);
  3696.     }
  3697.   selector = strchr (method, ' ');
  3698.   if (selector != NULL)
  3699.     selector++;

  3700.   category = strchr (method, '(');

  3701.   if ((category != NULL) && (selector != NULL))
  3702.     {
  3703.       memcpy (tmp, method, (category - method));
  3704.       tmp[category - method] = ' ';
  3705.       memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
  3706.       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
  3707.       if (sym_text[0] == '[')
  3708.         completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
  3709.     }

  3710.   if (selector != NULL)
  3711.     {
  3712.       /* Complete on selector only.  */
  3713.       strcpy (tmp, selector);
  3714.       tmp2 = strchr (tmp, ']');
  3715.       if (tmp2 != NULL)
  3716.         *tmp2 = '\0';

  3717.       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
  3718.     }
  3719. }

  3720. /* Break the non-quoted text based on the characters which are in
  3721.    symbols.  FIXME: This should probably be language-specific.  */

  3722. static const char *
  3723. language_search_unquoted_string (const char *text, const char *p)
  3724. {
  3725.   for (; p > text; --p)
  3726.     {
  3727.       if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
  3728.         continue;
  3729.       else
  3730.         {
  3731.           if ((current_language->la_language == language_objc))
  3732.             {
  3733.               if (p[-1] == ':')     /* Might be part of a method name.  */
  3734.                 continue;
  3735.               else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
  3736.                 p -= 2;             /* Beginning of a method name.  */
  3737.               else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
  3738.                 {                   /* Might be part of a method name.  */
  3739.                   const char *t = p;

  3740.                   /* Seeing a ' ' or a '(' is not conclusive evidence
  3741.                      that we are in the middle of a method name.  However,
  3742.                      finding "-[" or "+[" should be pretty un-ambiguous.
  3743.                      Unfortunately we have to find it now to decide.  */

  3744.                   while (t > text)
  3745.                     if (isalnum (t[-1]) || t[-1] == '_' ||
  3746.                         t[-1] == ' '    || t[-1] == ':' ||
  3747.                         t[-1] == '('    || t[-1] == ')')
  3748.                       --t;
  3749.                     else
  3750.                       break;

  3751.                   if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
  3752.                     p = t - 2;      /* Method name detected.  */
  3753.                   /* Else we leave with p unchanged.  */
  3754.                 }
  3755.             }
  3756.           break;
  3757.         }
  3758.     }
  3759.   return p;
  3760. }

  3761. static void
  3762. completion_list_add_fields (struct symbol *sym, const char *sym_text,
  3763.                             int sym_text_len, const char *text,
  3764.                             const char *word)
  3765. {
  3766.   if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  3767.     {
  3768.       struct type *t = SYMBOL_TYPE (sym);
  3769.       enum type_code c = TYPE_CODE (t);
  3770.       int j;

  3771.       if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
  3772.         for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
  3773.           if (TYPE_FIELD_NAME (t, j))
  3774.             completion_list_add_name (TYPE_FIELD_NAME (t, j),
  3775.                                       sym_text, sym_text_len, text, word);
  3776.     }
  3777. }

  3778. /* Type of the user_data argument passed to add_macro_name or
  3779.    symbol_completion_matcher.  The contents are simply whatever is
  3780.    needed by completion_list_add_name.  */
  3781. struct add_name_data
  3782. {
  3783.   const char *sym_text;
  3784.   int sym_text_len;
  3785.   const char *text;
  3786.   const char *word;
  3787. };

  3788. /* A callback used with macro_for_each and macro_for_each_in_scope.
  3789.    This adds a macro's name to the current completion list.  */

  3790. static void
  3791. add_macro_name (const char *name, const struct macro_definition *ignore,
  3792.                 struct macro_source_file *ignore2, int ignore3,
  3793.                 void *user_data)
  3794. {
  3795.   struct add_name_data *datum = (struct add_name_data *) user_data;

  3796.   completion_list_add_name (name,
  3797.                             datum->sym_text, datum->sym_text_len,
  3798.                             datum->text, datum->word);
  3799. }

  3800. /* A callback for expand_symtabs_matching.  */

  3801. static int
  3802. symbol_completion_matcher (const char *name, void *user_data)
  3803. {
  3804.   struct add_name_data *datum = (struct add_name_data *) user_data;

  3805.   return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
  3806. }

  3807. VEC (char_ptr) *
  3808. default_make_symbol_completion_list_break_on (const char *text,
  3809.                                               const char *word,
  3810.                                               const char *break_on,
  3811.                                               enum type_code code)
  3812. {
  3813.   /* Problem: All of the symbols have to be copied because readline
  3814.      frees them.  I'm not going to worry about this; hopefully there
  3815.      won't be that many.  */

  3816.   struct symbol *sym;
  3817.   struct compunit_symtab *cust;
  3818.   struct minimal_symbol *msymbol;
  3819.   struct objfile *objfile;
  3820.   const struct block *b;
  3821.   const struct block *surrounding_static_block, *surrounding_global_block;
  3822.   struct block_iterator iter;
  3823.   /* The symbol we are completing on.  Points in same buffer as text.  */
  3824.   const char *sym_text;
  3825.   /* Length of sym_text.  */
  3826.   int sym_text_len;
  3827.   struct add_name_data datum;
  3828.   struct cleanup *back_to;

  3829.   /* Now look for the symbol we are supposed to complete on.  */
  3830.   {
  3831.     const char *p;
  3832.     char quote_found;
  3833.     const char *quote_pos = NULL;

  3834.     /* First see if this is a quoted string.  */
  3835.     quote_found = '\0';
  3836.     for (p = text; *p != '\0'; ++p)
  3837.       {
  3838.         if (quote_found != '\0')
  3839.           {
  3840.             if (*p == quote_found)
  3841.               /* Found close quote.  */
  3842.               quote_found = '\0';
  3843.             else if (*p == '\\' && p[1] == quote_found)
  3844.               /* A backslash followed by the quote character
  3845.                  doesn't end the string.  */
  3846.               ++p;
  3847.           }
  3848.         else if (*p == '\'' || *p == '"')
  3849.           {
  3850.             quote_found = *p;
  3851.             quote_pos = p;
  3852.           }
  3853.       }
  3854.     if (quote_found == '\'')
  3855.       /* A string within single quotes can be a symbol, so complete on it.  */
  3856.       sym_text = quote_pos + 1;
  3857.     else if (quote_found == '"')
  3858.       /* A double-quoted string is never a symbol, nor does it make sense
  3859.          to complete it any other way.  */
  3860.       {
  3861.         return NULL;
  3862.       }
  3863.     else
  3864.       {
  3865.         /* It is not a quoted string.  Break it based on the characters
  3866.            which are in symbols.  */
  3867.         while (p > text)
  3868.           {
  3869.             if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
  3870.                 || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
  3871.               --p;
  3872.             else
  3873.               break;
  3874.           }
  3875.         sym_text = p;
  3876.       }
  3877.   }

  3878.   sym_text_len = strlen (sym_text);

  3879.   /* Prepare SYM_TEXT_LEN for compare_symbol_name.  */

  3880.   if (current_language->la_language == language_cplus
  3881.       || current_language->la_language == language_java
  3882.       || current_language->la_language == language_fortran)
  3883.     {
  3884.       /* These languages may have parameters entered by user but they are never
  3885.          present in the partial symbol tables.  */

  3886.       const char *cs = memchr (sym_text, '(', sym_text_len);

  3887.       if (cs)
  3888.         sym_text_len = cs - sym_text;
  3889.     }
  3890.   gdb_assert (sym_text[sym_text_len] == '\0' || sym_text[sym_text_len] == '(');

  3891.   return_val = NULL;
  3892.   back_to = make_cleanup (do_free_completion_list, &return_val);

  3893.   datum.sym_text = sym_text;
  3894.   datum.sym_text_len = sym_text_len;
  3895.   datum.text = text;
  3896.   datum.word = word;

  3897.   /* Look through the partial symtabs for all symbols which begin
  3898.      by matching SYM_TEXT.  Expand all CUs that you find to the list.
  3899.      The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
  3900.   expand_symtabs_matching (NULL, symbol_completion_matcher, ALL_DOMAIN,
  3901.                            &datum);

  3902.   /* At this point scan through the misc symbol vectors and add each
  3903.      symbol you find to the list.  Eventually we want to ignore
  3904.      anything that isn't a text symbol (everything else will be
  3905.      handled by the psymtab code above).  */

  3906.   if (code == TYPE_CODE_UNDEF)
  3907.     {
  3908.       ALL_MSYMBOLS (objfile, msymbol)
  3909.         {
  3910.           QUIT;
  3911.           MCOMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text,
  3912.                                        word);

  3913.           completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text,
  3914.                                        word);
  3915.         }
  3916.     }

  3917.   /* Search upwards from currently selected frame (so that we can
  3918.      complete on local vars).  Also catch fields of types defined in
  3919.      this places which match our text string.  Only complete on types
  3920.      visible from current context.  */

  3921.   b = get_selected_block (0);
  3922.   surrounding_static_block = block_static_block (b);
  3923.   surrounding_global_block = block_global_block (b);
  3924.   if (surrounding_static_block != NULL)
  3925.     while (b != surrounding_static_block)
  3926.       {
  3927.         QUIT;

  3928.         ALL_BLOCK_SYMBOLS (b, iter, sym)
  3929.           {
  3930.             if (code == TYPE_CODE_UNDEF)
  3931.               {
  3932.                 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
  3933.                                             word);
  3934.                 completion_list_add_fields (sym, sym_text, sym_text_len, text,
  3935.                                             word);
  3936.               }
  3937.             else if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
  3938.                      && TYPE_CODE (SYMBOL_TYPE (sym)) == code)
  3939.               COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
  3940.                                           word);
  3941.           }

  3942.         /* Stop when we encounter an enclosing function.  Do not stop for
  3943.            non-inlined functions - the locals of the enclosing function
  3944.            are in scope for a nested function.  */
  3945.         if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
  3946.           break;
  3947.         b = BLOCK_SUPERBLOCK (b);
  3948.       }

  3949.   /* Add fields from the file's types; symbols will be added below.  */

  3950.   if (code == TYPE_CODE_UNDEF)
  3951.     {
  3952.       if (surrounding_static_block != NULL)
  3953.         ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
  3954.           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);

  3955.       if (surrounding_global_block != NULL)
  3956.         ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
  3957.           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
  3958.     }

  3959.   /* Go through the symtabs and check the externs and statics for
  3960.      symbols which match.  */

  3961.   ALL_COMPUNITS (objfile, cust)
  3962.   {
  3963.     QUIT;
  3964.     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), GLOBAL_BLOCK);
  3965.     ALL_BLOCK_SYMBOLS (b, iter, sym)
  3966.       {
  3967.         if (code == TYPE_CODE_UNDEF
  3968.             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
  3969.                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
  3970.           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  3971.       }
  3972.   }

  3973.   ALL_COMPUNITS (objfile, cust)
  3974.   {
  3975.     QUIT;
  3976.     b = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), STATIC_BLOCK);
  3977.     ALL_BLOCK_SYMBOLS (b, iter, sym)
  3978.       {
  3979.         if (code == TYPE_CODE_UNDEF
  3980.             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
  3981.                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
  3982.           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  3983.       }
  3984.   }

  3985.   /* Skip macros if we are completing a struct tag -- arguable but
  3986.      usually what is expected.  */
  3987.   if (current_language->la_macro_expansion == macro_expansion_c
  3988.       && code == TYPE_CODE_UNDEF)
  3989.     {
  3990.       struct macro_scope *scope;

  3991.       /* Add any macros visible in the default scope.  Note that this
  3992.          may yield the occasional wrong result, because an expression
  3993.          might be evaluated in a scope other than the default.  For
  3994.          example, if the user types "break file:line if <TAB>", the
  3995.          resulting expression will be evaluated at "file:line" -- but
  3996.          at there does not seem to be a way to detect this at
  3997.          completion time.  */
  3998.       scope = default_macro_scope ();
  3999.       if (scope)
  4000.         {
  4001.           macro_for_each_in_scope (scope->file, scope->line,
  4002.                                    add_macro_name, &datum);
  4003.           xfree (scope);
  4004.         }

  4005.       /* User-defined macros are always visible.  */
  4006.       macro_for_each (macro_user_macros, add_macro_name, &datum);
  4007.     }

  4008.   discard_cleanups (back_to);
  4009.   return (return_val);
  4010. }

  4011. VEC (char_ptr) *
  4012. default_make_symbol_completion_list (const char *text, const char *word,
  4013.                                      enum type_code code)
  4014. {
  4015.   return default_make_symbol_completion_list_break_on (text, word, "", code);
  4016. }

  4017. /* Return a vector of all symbols (regardless of class) which begin by
  4018.    matching TEXT.  If the answer is no symbols, then the return value
  4019.    is NULL.  */

  4020. VEC (char_ptr) *
  4021. make_symbol_completion_list (const char *text, const char *word)
  4022. {
  4023.   return current_language->la_make_symbol_completion_list (text, word,
  4024.                                                            TYPE_CODE_UNDEF);
  4025. }

  4026. /* Like make_symbol_completion_list, but only return STRUCT_DOMAIN
  4027.    symbols whose type code is CODE.  */

  4028. VEC (char_ptr) *
  4029. make_symbol_completion_type (const char *text, const char *word,
  4030.                              enum type_code code)
  4031. {
  4032.   gdb_assert (code == TYPE_CODE_UNION
  4033.               || code == TYPE_CODE_STRUCT
  4034.               || code == TYPE_CODE_ENUM);
  4035.   return current_language->la_make_symbol_completion_list (text, word, code);
  4036. }

  4037. /* Like make_symbol_completion_list, but suitable for use as a
  4038.    completion function.  */

  4039. VEC (char_ptr) *
  4040. make_symbol_completion_list_fn (struct cmd_list_element *ignore,
  4041.                                 const char *text, const char *word)
  4042. {
  4043.   return make_symbol_completion_list (text, word);
  4044. }

  4045. /* Like make_symbol_completion_list, but returns a list of symbols
  4046.    defined in a source file FILE.  */

  4047. VEC (char_ptr) *
  4048. make_file_symbol_completion_list (const char *text, const char *word,
  4049.                                   const char *srcfile)
  4050. {
  4051.   struct symbol *sym;
  4052.   struct symtab *s;
  4053.   struct block *b;
  4054.   struct block_iterator iter;
  4055.   /* The symbol we are completing on.  Points in same buffer as text.  */
  4056.   const char *sym_text;
  4057.   /* Length of sym_text.  */
  4058.   int sym_text_len;

  4059.   /* Now look for the symbol we are supposed to complete on.
  4060.      FIXME: This should be language-specific.  */
  4061.   {
  4062.     const char *p;
  4063.     char quote_found;
  4064.     const char *quote_pos = NULL;

  4065.     /* First see if this is a quoted string.  */
  4066.     quote_found = '\0';
  4067.     for (p = text; *p != '\0'; ++p)
  4068.       {
  4069.         if (quote_found != '\0')
  4070.           {
  4071.             if (*p == quote_found)
  4072.               /* Found close quote.  */
  4073.               quote_found = '\0';
  4074.             else if (*p == '\\' && p[1] == quote_found)
  4075.               /* A backslash followed by the quote character
  4076.                  doesn't end the string.  */
  4077.               ++p;
  4078.           }
  4079.         else if (*p == '\'' || *p == '"')
  4080.           {
  4081.             quote_found = *p;
  4082.             quote_pos = p;
  4083.           }
  4084.       }
  4085.     if (quote_found == '\'')
  4086.       /* A string within single quotes can be a symbol, so complete on it.  */
  4087.       sym_text = quote_pos + 1;
  4088.     else if (quote_found == '"')
  4089.       /* A double-quoted string is never a symbol, nor does it make sense
  4090.          to complete it any other way.  */
  4091.       {
  4092.         return NULL;
  4093.       }
  4094.     else
  4095.       {
  4096.         /* Not a quoted string.  */
  4097.         sym_text = language_search_unquoted_string (text, p);
  4098.       }
  4099.   }

  4100.   sym_text_len = strlen (sym_text);

  4101.   return_val = NULL;

  4102.   /* Find the symtab for SRCFILE (this loads it if it was not yet read
  4103.      in).  */
  4104.   s = lookup_symtab (srcfile);
  4105.   if (s == NULL)
  4106.     {
  4107.       /* Maybe they typed the file with leading directories, while the
  4108.          symbol tables record only its basename.  */
  4109.       const char *tail = lbasename (srcfile);

  4110.       if (tail > srcfile)
  4111.         s = lookup_symtab (tail);
  4112.     }

  4113.   /* If we have no symtab for that file, return an empty list.  */
  4114.   if (s == NULL)
  4115.     return (return_val);

  4116.   /* Go through this symtab and check the externs and statics for
  4117.      symbols which match.  */

  4118.   b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), GLOBAL_BLOCK);
  4119.   ALL_BLOCK_SYMBOLS (b, iter, sym)
  4120.     {
  4121.       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  4122.     }

  4123.   b = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (s), STATIC_BLOCK);
  4124.   ALL_BLOCK_SYMBOLS (b, iter, sym)
  4125.     {
  4126.       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  4127.     }

  4128.   return (return_val);
  4129. }

  4130. /* A helper function for make_source_files_completion_list.  It adds
  4131.    another file name to a list of possible completions, growing the
  4132.    list as necessary.  */

  4133. static void
  4134. add_filename_to_list (const char *fname, const char *text, const char *word,
  4135.                       VEC (char_ptr) **list)
  4136. {
  4137.   char *new;
  4138.   size_t fnlen = strlen (fname);

  4139.   if (word == text)
  4140.     {
  4141.       /* Return exactly fname.  */
  4142.       new = xmalloc (fnlen + 5);
  4143.       strcpy (new, fname);
  4144.     }
  4145.   else if (word > text)
  4146.     {
  4147.       /* Return some portion of fname.  */
  4148.       new = xmalloc (fnlen + 5);
  4149.       strcpy (new, fname + (word - text));
  4150.     }
  4151.   else
  4152.     {
  4153.       /* Return some of TEXT plus fname.  */
  4154.       new = xmalloc (fnlen + (text - word) + 5);
  4155.       strncpy (new, word, text - word);
  4156.       new[text - word] = '\0';
  4157.       strcat (new, fname);
  4158.     }
  4159.   VEC_safe_push (char_ptr, *list, new);
  4160. }

  4161. static int
  4162. not_interesting_fname (const char *fname)
  4163. {
  4164.   static const char *illegal_aliens[] = {
  4165.     "_globals_",        /* inserted by coff_symtab_read */
  4166.     NULL
  4167.   };
  4168.   int i;

  4169.   for (i = 0; illegal_aliens[i]; i++)
  4170.     {
  4171.       if (filename_cmp (fname, illegal_aliens[i]) == 0)
  4172.         return 1;
  4173.     }
  4174.   return 0;
  4175. }

  4176. /* An object of this type is passed as the user_data argument to
  4177.    map_partial_symbol_filenames.  */
  4178. struct add_partial_filename_data
  4179. {
  4180.   struct filename_seen_cache *filename_seen_cache;
  4181.   const char *text;
  4182.   const char *word;
  4183.   int text_len;
  4184.   VEC (char_ptr) **list;
  4185. };

  4186. /* A callback for map_partial_symbol_filenames.  */

  4187. static void
  4188. maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
  4189.                                    void *user_data)
  4190. {
  4191.   struct add_partial_filename_data *data = user_data;

  4192.   if (not_interesting_fname (filename))
  4193.     return;
  4194.   if (!filename_seen (data->filename_seen_cache, filename, 1)
  4195.       && filename_ncmp (filename, data->text, data->text_len) == 0)
  4196.     {
  4197.       /* This file matches for a completion; add it to the
  4198.          current list of matches.  */
  4199.       add_filename_to_list (filename, data->text, data->word, data->list);
  4200.     }
  4201.   else
  4202.     {
  4203.       const char *base_name = lbasename (filename);

  4204.       if (base_name != filename
  4205.           && !filename_seen (data->filename_seen_cache, base_name, 1)
  4206.           && filename_ncmp (base_name, data->text, data->text_len) == 0)
  4207.         add_filename_to_list (base_name, data->text, data->word, data->list);
  4208.     }
  4209. }

  4210. /* Return a vector of all source files whose names begin with matching
  4211.    TEXT.  The file names are looked up in the symbol tables of this
  4212.    program.  If the answer is no matchess, then the return value is
  4213.    NULL.  */

  4214. VEC (char_ptr) *
  4215. make_source_files_completion_list (const char *text, const char *word)
  4216. {
  4217.   struct compunit_symtab *cu;
  4218.   struct symtab *s;
  4219.   struct objfile *objfile;
  4220.   size_t text_len = strlen (text);
  4221.   VEC (char_ptr) *list = NULL;
  4222.   const char *base_name;
  4223.   struct add_partial_filename_data datum;
  4224.   struct filename_seen_cache *filename_seen_cache;
  4225.   struct cleanup *back_to, *cache_cleanup;

  4226.   if (!have_full_symbols () && !have_partial_symbols ())
  4227.     return list;

  4228.   back_to = make_cleanup (do_free_completion_list, &list);

  4229.   filename_seen_cache = create_filename_seen_cache ();
  4230.   cache_cleanup = make_cleanup (delete_filename_seen_cache,
  4231.                                 filename_seen_cache);

  4232.   ALL_FILETABS (objfile, cu, s)
  4233.     {
  4234.       if (not_interesting_fname (s->filename))
  4235.         continue;
  4236.       if (!filename_seen (filename_seen_cache, s->filename, 1)
  4237.           && filename_ncmp (s->filename, text, text_len) == 0)
  4238.         {
  4239.           /* This file matches for a completion; add it to the current
  4240.              list of matches.  */
  4241.           add_filename_to_list (s->filename, text, word, &list);
  4242.         }
  4243.       else
  4244.         {
  4245.           /* NOTE: We allow the user to type a base name when the
  4246.              debug info records leading directories, but not the other
  4247.              way around.  This is what subroutines of breakpoint
  4248.              command do when they parse file names.  */
  4249.           base_name = lbasename (s->filename);
  4250.           if (base_name != s->filename
  4251.               && !filename_seen (filename_seen_cache, base_name, 1)
  4252.               && filename_ncmp (base_name, text, text_len) == 0)
  4253.             add_filename_to_list (base_name, text, word, &list);
  4254.         }
  4255.     }

  4256.   datum.filename_seen_cache = filename_seen_cache;
  4257.   datum.text = text;
  4258.   datum.word = word;
  4259.   datum.text_len = text_len;
  4260.   datum.list = &list;
  4261.   map_symbol_filenames (maybe_add_partial_symtab_filename, &datum,
  4262.                         0 /*need_fullname*/);

  4263.   do_cleanups (cache_cleanup);
  4264.   discard_cleanups (back_to);

  4265.   return list;
  4266. }

  4267. /* Track MAIN */

  4268. /* Return the "main_info" object for the current program space.  If
  4269.    the object has not yet been created, create it and fill in some
  4270.    default values.  */

  4271. static struct main_info *
  4272. get_main_info (void)
  4273. {
  4274.   struct main_info *info = program_space_data (current_program_space,
  4275.                                                main_progspace_key);

  4276.   if (info == NULL)
  4277.     {
  4278.       /* It may seem strange to store the main name in the progspace
  4279.          and also in whatever objfile happens to see a main name in
  4280.          its debug info.  The reason for this is mainly historical:
  4281.          gdb returned "main" as the name even if no function named
  4282.          "main" was defined the program; and this approach lets us
  4283.          keep compatibility.  */
  4284.       info = XCNEW (struct main_info);
  4285.       info->language_of_main = language_unknown;
  4286.       set_program_space_data (current_program_space, main_progspace_key,
  4287.                               info);
  4288.     }

  4289.   return info;
  4290. }

  4291. /* A cleanup to destroy a struct main_info when a progspace is
  4292.    destroyed.  */

  4293. static void
  4294. main_info_cleanup (struct program_space *pspace, void *data)
  4295. {
  4296.   struct main_info *info = data;

  4297.   if (info != NULL)
  4298.     xfree (info->name_of_main);
  4299.   xfree (info);
  4300. }

  4301. static void
  4302. set_main_name (const char *name, enum language lang)
  4303. {
  4304.   struct main_info *info = get_main_info ();

  4305.   if (info->name_of_main != NULL)
  4306.     {
  4307.       xfree (info->name_of_main);
  4308.       info->name_of_main = NULL;
  4309.       info->language_of_main = language_unknown;
  4310.     }
  4311.   if (name != NULL)
  4312.     {
  4313.       info->name_of_main = xstrdup (name);
  4314.       info->language_of_main = lang;
  4315.     }
  4316. }

  4317. /* Deduce the name of the main procedure, and set NAME_OF_MAIN
  4318.    accordingly.  */

  4319. static void
  4320. find_main_name (void)
  4321. {
  4322.   const char *new_main_name;
  4323.   struct objfile *objfile;

  4324.   /* First check the objfiles to see whether a debuginfo reader has
  4325.      picked up the appropriate main name.  Historically the main name
  4326.      was found in a more or less random way; this approach instead
  4327.      relies on the order of objfile creation -- which still isn't
  4328.      guaranteed to get the correct answer, but is just probably more
  4329.      accurate.  */
  4330.   ALL_OBJFILES (objfile)
  4331.   {
  4332.     if (objfile->per_bfd->name_of_main != NULL)
  4333.       {
  4334.         set_main_name (objfile->per_bfd->name_of_main,
  4335.                        objfile->per_bfd->language_of_main);
  4336.         return;
  4337.       }
  4338.   }

  4339.   /* Try to see if the main procedure is in Ada.  */
  4340.   /* FIXME: brobecker/2005-03-07: Another way of doing this would
  4341.      be to add a new method in the language vector, and call this
  4342.      method for each language until one of them returns a non-empty
  4343.      name.  This would allow us to remove this hard-coded call to
  4344.      an Ada function.  It is not clear that this is a better approach
  4345.      at this point, because all methods need to be written in a way
  4346.      such that false positives never be returned.  For instance, it is
  4347.      important that a method does not return a wrong name for the main
  4348.      procedure if the main procedure is actually written in a different
  4349.      language.  It is easy to guaranty this with Ada, since we use a
  4350.      special symbol generated only when the main in Ada to find the name
  4351.      of the main procedure.  It is difficult however to see how this can
  4352.      be guarantied for languages such as C, for instance.  This suggests
  4353.      that order of call for these methods becomes important, which means
  4354.      a more complicated approach.  */
  4355.   new_main_name = ada_main_name ();
  4356.   if (new_main_name != NULL)
  4357.     {
  4358.       set_main_name (new_main_name, language_ada);
  4359.       return;
  4360.     }

  4361.   new_main_name = d_main_name ();
  4362.   if (new_main_name != NULL)
  4363.     {
  4364.       set_main_name (new_main_name, language_d);
  4365.       return;
  4366.     }

  4367.   new_main_name = go_main_name ();
  4368.   if (new_main_name != NULL)
  4369.     {
  4370.       set_main_name (new_main_name, language_go);
  4371.       return;
  4372.     }

  4373.   new_main_name = pascal_main_name ();
  4374.   if (new_main_name != NULL)
  4375.     {
  4376.       set_main_name (new_main_name, language_pascal);
  4377.       return;
  4378.     }

  4379.   /* The languages above didn't identify the name of the main procedure.
  4380.      Fallback to "main".  */
  4381.   set_main_name ("main", language_unknown);
  4382. }

  4383. char *
  4384. main_name (void)
  4385. {
  4386.   struct main_info *info = get_main_info ();

  4387.   if (info->name_of_main == NULL)
  4388.     find_main_name ();

  4389.   return info->name_of_main;
  4390. }

  4391. /* Return the language of the main function.  If it is not known,
  4392.    return language_unknown.  */

  4393. enum language
  4394. main_language (void)
  4395. {
  4396.   struct main_info *info = get_main_info ();

  4397.   if (info->name_of_main == NULL)
  4398.     find_main_name ();

  4399.   return info->language_of_main;
  4400. }

  4401. /* Handle ``executable_changed'' events for the symtab module.  */

  4402. static void
  4403. symtab_observer_executable_changed (void)
  4404. {
  4405.   /* NAME_OF_MAIN may no longer be the same, so reset it for now.  */
  4406.   set_main_name (NULL, language_unknown);
  4407. }

  4408. /* Return 1 if the supplied producer string matches the ARM RealView
  4409.    compiler (armcc).  */

  4410. int
  4411. producer_is_realview (const char *producer)
  4412. {
  4413.   static const char *const arm_idents[] = {
  4414.     "ARM C Compiler, ADS",
  4415.     "Thumb C Compiler, ADS",
  4416.     "ARM C++ Compiler, ADS",
  4417.     "Thumb C++ Compiler, ADS",
  4418.     "ARM/Thumb C/C++ Compiler, RVCT",
  4419.     "ARM C/C++ Compiler, RVCT"
  4420.   };
  4421.   int i;

  4422.   if (producer == NULL)
  4423.     return 0;

  4424.   for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
  4425.     if (strncmp (producer, arm_idents[i], strlen (arm_idents[i])) == 0)
  4426.       return 1;

  4427.   return 0;
  4428. }



  4429. /* The next index to hand out in response to a registration request.  */

  4430. static int next_aclass_value = LOC_FINAL_VALUE;

  4431. /* The maximum number of "aclass" registrations we support.  This is
  4432.    constant for convenience.  */
  4433. #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 10)

  4434. /* The objects representing the various "aclass" values.  The elements
  4435.    from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
  4436.    elements are those registered at gdb initialization time.  */

  4437. static struct symbol_impl symbol_impl[MAX_SYMBOL_IMPLS];

  4438. /* The globally visible pointer.  This is separate from 'symbol_impl'
  4439.    so that it can be const.  */

  4440. const struct symbol_impl *symbol_impls = &symbol_impl[0];

  4441. /* Make sure we saved enough room in struct symbol.  */

  4442. gdb_static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));

  4443. /* Register a computed symbol type.  ACLASS must be LOC_COMPUTED.  OPS
  4444.    is the ops vector associated with this index.  This returns the new
  4445.    index, which should be used as the aclass_index field for symbols
  4446.    of this type.  */

  4447. int
  4448. register_symbol_computed_impl (enum address_class aclass,
  4449.                                const struct symbol_computed_ops *ops)
  4450. {
  4451.   int result = next_aclass_value++;

  4452.   gdb_assert (aclass == LOC_COMPUTED);
  4453.   gdb_assert (result < MAX_SYMBOL_IMPLS);
  4454.   symbol_impl[result].aclass = aclass;
  4455.   symbol_impl[result].ops_computed = ops;

  4456.   /* Sanity check OPS.  */
  4457.   gdb_assert (ops != NULL);
  4458.   gdb_assert (ops->tracepoint_var_ref != NULL);
  4459.   gdb_assert (ops->describe_location != NULL);
  4460.   gdb_assert (ops->read_needs_frame != NULL);
  4461.   gdb_assert (ops->read_variable != NULL);

  4462.   return result;
  4463. }

  4464. /* Register a function with frame base type.  ACLASS must be LOC_BLOCK.
  4465.    OPS is the ops vector associated with this index.  This returns the
  4466.    new index, which should be used as the aclass_index field for symbols
  4467.    of this type.  */

  4468. int
  4469. register_symbol_block_impl (enum address_class aclass,
  4470.                             const struct symbol_block_ops *ops)
  4471. {
  4472.   int result = next_aclass_value++;

  4473.   gdb_assert (aclass == LOC_BLOCK);
  4474.   gdb_assert (result < MAX_SYMBOL_IMPLS);
  4475.   symbol_impl[result].aclass = aclass;
  4476.   symbol_impl[result].ops_block = ops;

  4477.   /* Sanity check OPS.  */
  4478.   gdb_assert (ops != NULL);
  4479.   gdb_assert (ops->find_frame_base_location != NULL);

  4480.   return result;
  4481. }

  4482. /* Register a register symbol type.  ACLASS must be LOC_REGISTER or
  4483.    LOC_REGPARM_ADDR.  OPS is the register ops vector associated with
  4484.    this index.  This returns the new index, which should be used as
  4485.    the aclass_index field for symbols of this type.  */

  4486. int
  4487. register_symbol_register_impl (enum address_class aclass,
  4488.                                const struct symbol_register_ops *ops)
  4489. {
  4490.   int result = next_aclass_value++;

  4491.   gdb_assert (aclass == LOC_REGISTER || aclass == LOC_REGPARM_ADDR);
  4492.   gdb_assert (result < MAX_SYMBOL_IMPLS);
  4493.   symbol_impl[result].aclass = aclass;
  4494.   symbol_impl[result].ops_register = ops;

  4495.   return result;
  4496. }

  4497. /* Initialize elements of 'symbol_impl' for the constants in enum
  4498.    address_class.  */

  4499. static void
  4500. initialize_ordinary_address_classes (void)
  4501. {
  4502.   int i;

  4503.   for (i = 0; i < LOC_FINAL_VALUE; ++i)
  4504.     symbol_impl[i].aclass = i;
  4505. }



  4506. /* Helper function to initialize the fields of an objfile-owned symbol.
  4507.    It assumed that *SYM is already all zeroes.  */

  4508. static void
  4509. initialize_objfile_symbol_1 (struct symbol *sym)
  4510. {
  4511.   SYMBOL_OBJFILE_OWNED (sym) = 1;
  4512.   SYMBOL_SECTION (sym) = -1;
  4513. }

  4514. /* Initialize the symbol SYM, and mark it as being owned by an objfile.  */

  4515. void
  4516. initialize_objfile_symbol (struct symbol *sym)
  4517. {
  4518.   memset (sym, 0, sizeof (*sym));
  4519.   initialize_objfile_symbol_1 (sym);
  4520. }

  4521. /* Allocate and initialize a new 'struct symbol' on OBJFILE's
  4522.    obstack.  */

  4523. struct symbol *
  4524. allocate_symbol (struct objfile *objfile)
  4525. {
  4526.   struct symbol *result;

  4527.   result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
  4528.   initialize_objfile_symbol_1 (result);

  4529.   return result;
  4530. }

  4531. /* Allocate and initialize a new 'struct template_symbol' on OBJFILE's
  4532.    obstack.  */

  4533. struct template_symbol *
  4534. allocate_template_symbol (struct objfile *objfile)
  4535. {
  4536.   struct template_symbol *result;

  4537.   result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct template_symbol);
  4538.   initialize_objfile_symbol_1 (&result->base);

  4539.   return result;
  4540. }

  4541. /* See symtab.h.  */

  4542. struct objfile *
  4543. symbol_objfile (const struct symbol *symbol)
  4544. {
  4545.   gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
  4546.   return SYMTAB_OBJFILE (symbol->owner.symtab);
  4547. }

  4548. /* See symtab.h.  */

  4549. struct gdbarch *
  4550. symbol_arch (const struct symbol *symbol)
  4551. {
  4552.   if (!SYMBOL_OBJFILE_OWNED (symbol))
  4553.     return symbol->owner.arch;
  4554.   return get_objfile_arch (SYMTAB_OBJFILE (symbol->owner.symtab));
  4555. }

  4556. /* See symtab.h.  */

  4557. struct symtab *
  4558. symbol_symtab (const struct symbol *symbol)
  4559. {
  4560.   gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
  4561.   return symbol->owner.symtab;
  4562. }

  4563. /* See symtab.h.  */

  4564. void
  4565. symbol_set_symtab (struct symbol *symbol, struct symtab *symtab)
  4566. {
  4567.   gdb_assert (SYMBOL_OBJFILE_OWNED (symbol));
  4568.   symbol->owner.symtab = symtab;
  4569. }



  4570. void
  4571. _initialize_symtab (void)
  4572. {
  4573.   initialize_ordinary_address_classes ();

  4574.   main_progspace_key
  4575.     = register_program_space_data_with_cleanup (NULL, main_info_cleanup);

  4576.   add_info ("variables", variables_info, _("\
  4577. All global and static variable names, or those matching REGEXP."));
  4578.   if (dbx_commands)
  4579.     add_com ("whereis", class_info, variables_info, _("\
  4580. All global and static variable names, or those matching REGEXP."));

  4581.   add_info ("functions", functions_info,
  4582.             _("All function names, or those matching REGEXP."));

  4583.   /* FIXME:  This command has at least the following problems:
  4584.      1.  It prints builtin types (in a very strange and confusing fashion).
  4585.      2.  It doesn't print right, e.g. with
  4586.      typedef struct foo *FOO
  4587.      type_print prints "FOO" when we want to make it (in this situation)
  4588.      print "struct foo *".
  4589.      I also think "ptype" or "whatis" is more likely to be useful (but if
  4590.      there is much disagreement "info types" can be fixed).  */
  4591.   add_info ("types", types_info,
  4592.             _("All type names, or those matching REGEXP."));

  4593.   add_info ("sources", sources_info,
  4594.             _("Source files in the program."));

  4595.   add_com ("rbreak", class_breakpoint, rbreak_command,
  4596.            _("Set a breakpoint for all functions matching REGEXP."));

  4597.   if (xdb_commands)
  4598.     {
  4599.       add_com ("lf", class_info, sources_info,
  4600.                _("Source files in the program"));
  4601.       add_com ("lg", class_info, variables_info, _("\
  4602. All global and static variable names, or those matching REGEXP."));
  4603.     }

  4604.   add_setshow_enum_cmd ("multiple-symbols", no_class,
  4605.                         multiple_symbols_modes, &multiple_symbols_mode,
  4606.                         _("\
  4607. Set the debugger behavior when more than one symbol are possible matches\n\
  4608. in an expression."), _("\
  4609. Show how the debugger handles ambiguities in expressions."), _("\
  4610. Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
  4611.                         NULL, NULL, &setlist, &showlist);

  4612.   add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
  4613.                            &basenames_may_differ, _("\
  4614. Set whether a source file may have multiple base names."), _("\
  4615. Show whether a source file may have multiple base names."), _("\
  4616. (A \"base name\" is the name of a file with the directory part removed.\n\
  4617. Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
  4618. If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
  4619. before comparing them.  Canonicalization is an expensive operation,\n\
  4620. but it allows the same file be known by more than one base name.\n\
  4621. If not set (the default), all source files are assumed to have just\n\
  4622. one base name, and gdb will do file name comparisons more efficiently."),
  4623.                            NULL, NULL,
  4624.                            &setlist, &showlist);

  4625.   add_setshow_zuinteger_cmd ("symtab-create", no_class, &symtab_create_debug,
  4626.                              _("Set debugging of symbol table creation."),
  4627.                              _("Show debugging of symbol table creation."), _("\
  4628. When enabled (non-zero), debugging messages are printed when building\n\
  4629. symbol tables.  A value of 1 (one) normally provides enough information.\n\
  4630. A value greater than 1 provides more verbose information."),
  4631.                              NULL,
  4632.                              NULL,
  4633.                              &setdebuglist, &showdebuglist);

  4634.   add_setshow_zuinteger_cmd ("symbol-lookup", no_class, &symbol_lookup_debug,
  4635.                            _("\
  4636. Set debugging of symbol lookup."), _("\
  4637. Show debugging of symbol lookup."), _("\
  4638. When enabled (non-zero), symbol lookups are logged."),
  4639.                            NULL, NULL,
  4640.                            &setdebuglist, &showdebuglist);

  4641.   observer_attach_executable_changed (symtab_observer_executable_changed);
  4642. }