GDB (API)
/home/stan/gdb/src/gdb/symtab.c
Go to the documentation of this file.
00001 /* Symbol table lookup for the GNU debugger, GDB.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "symtab.h"
00022 #include "gdbtypes.h"
00023 #include "gdbcore.h"
00024 #include "frame.h"
00025 #include "target.h"
00026 #include "value.h"
00027 #include "symfile.h"
00028 #include "objfiles.h"
00029 #include "gdbcmd.h"
00030 #include "gdb_regex.h"
00031 #include "expression.h"
00032 #include "language.h"
00033 #include "demangle.h"
00034 #include "inferior.h"
00035 #include "source.h"
00036 #include "filenames.h"          /* for FILENAME_CMP */
00037 #include "objc-lang.h"
00038 #include "d-lang.h"
00039 #include "ada-lang.h"
00040 #include "go-lang.h"
00041 #include "p-lang.h"
00042 #include "addrmap.h"
00043 #include "cli/cli-utils.h"
00044 
00045 #include "hashtab.h"
00046 
00047 #include "gdb_obstack.h"
00048 #include "block.h"
00049 #include "dictionary.h"
00050 
00051 #include <sys/types.h>
00052 #include <fcntl.h>
00053 #include "gdb_string.h"
00054 #include "gdb_stat.h"
00055 #include <ctype.h>
00056 #include "cp-abi.h"
00057 #include "cp-support.h"
00058 #include "observer.h"
00059 #include "gdb_assert.h"
00060 #include "solist.h"
00061 #include "macrotab.h"
00062 #include "macroscope.h"
00063 
00064 #include "psymtab.h"
00065 #include "parser-defs.h"
00066 
00067 /* Prototypes for local functions */
00068 
00069 static void rbreak_command (char *, int);
00070 
00071 static void types_info (char *, int);
00072 
00073 static void functions_info (char *, int);
00074 
00075 static void variables_info (char *, int);
00076 
00077 static void sources_info (char *, int);
00078 
00079 static int find_line_common (struct linetable *, int, int *, int);
00080 
00081 static struct symbol *lookup_symbol_aux (const char *name,
00082                                          const struct block *block,
00083                                          const domain_enum domain,
00084                                          enum language language,
00085                                          struct field_of_this_result *is_a_field_of_this);
00086 
00087 static
00088 struct symbol *lookup_symbol_aux_local (const char *name,
00089                                         const struct block *block,
00090                                         const domain_enum domain,
00091                                         enum language language);
00092 
00093 static
00094 struct symbol *lookup_symbol_aux_symtabs (int block_index,
00095                                           const char *name,
00096                                           const domain_enum domain);
00097 
00098 static
00099 struct symbol *lookup_symbol_aux_quick (struct objfile *objfile,
00100                                         int block_index,
00101                                         const char *name,
00102                                         const domain_enum domain);
00103 
00104 void _initialize_symtab (void);
00105 
00106 /* */
00107 
00108 /* When non-zero, print debugging messages related to symtab creation.  */
00109 int symtab_create_debug = 0;
00110 
00111 /* Non-zero if a file may be known by two different basenames.
00112    This is the uncommon case, and significantly slows down gdb.
00113    Default set to "off" to not slow down the common case.  */
00114 int basenames_may_differ = 0;
00115 
00116 /* Allow the user to configure the debugger behavior with respect
00117    to multiple-choice menus when more than one symbol matches during
00118    a symbol lookup.  */
00119 
00120 const char multiple_symbols_ask[] = "ask";
00121 const char multiple_symbols_all[] = "all";
00122 const char multiple_symbols_cancel[] = "cancel";
00123 static const char *const multiple_symbols_modes[] =
00124 {
00125   multiple_symbols_ask,
00126   multiple_symbols_all,
00127   multiple_symbols_cancel,
00128   NULL
00129 };
00130 static const char *multiple_symbols_mode = multiple_symbols_all;
00131 
00132 /* Read-only accessor to AUTO_SELECT_MODE.  */
00133 
00134 const char *
00135 multiple_symbols_select_mode (void)
00136 {
00137   return multiple_symbols_mode;
00138 }
00139 
00140 /* Block in which the most recently searched-for symbol was found.
00141    Might be better to make this a parameter to lookup_symbol and
00142    value_of_this.  */
00143 
00144 const struct block *block_found;
00145 
00146 /* Return the name of a domain_enum.  */
00147 
00148 const char *
00149 domain_name (domain_enum e)
00150 {
00151   switch (e)
00152     {
00153     case UNDEF_DOMAIN: return "UNDEF_DOMAIN";
00154     case VAR_DOMAIN: return "VAR_DOMAIN";
00155     case STRUCT_DOMAIN: return "STRUCT_DOMAIN";
00156     case LABEL_DOMAIN: return "LABEL_DOMAIN";
00157     case COMMON_BLOCK_DOMAIN: return "COMMON_BLOCK_DOMAIN";
00158     default: gdb_assert_not_reached ("bad domain_enum");
00159     }
00160 }
00161 
00162 /* Return the name of a search_domain .  */
00163 
00164 const char *
00165 search_domain_name (enum search_domain e)
00166 {
00167   switch (e)
00168     {
00169     case VARIABLES_DOMAIN: return "VARIABLES_DOMAIN";
00170     case FUNCTIONS_DOMAIN: return "FUNCTIONS_DOMAIN";
00171     case TYPES_DOMAIN: return "TYPES_DOMAIN";
00172     case ALL_DOMAIN: return "ALL_DOMAIN";
00173     default: gdb_assert_not_reached ("bad search_domain");
00174     }
00175 }
00176 
00177 /* See whether FILENAME matches SEARCH_NAME using the rule that we
00178    advertise to the user.  (The manual's description of linespecs
00179    describes what we advertise).  Returns true if they match, false
00180    otherwise.  */
00181 
00182 int
00183 compare_filenames_for_search (const char *filename, const char *search_name)
00184 {
00185   int len = strlen (filename);
00186   size_t search_len = strlen (search_name);
00187 
00188   if (len < search_len)
00189     return 0;
00190 
00191   /* The tail of FILENAME must match.  */
00192   if (FILENAME_CMP (filename + len - search_len, search_name) != 0)
00193     return 0;
00194 
00195   /* Either the names must completely match, or the character
00196      preceding the trailing SEARCH_NAME segment of FILENAME must be a
00197      directory separator.
00198 
00199      The check !IS_ABSOLUTE_PATH ensures SEARCH_NAME "/dir/file.c"
00200      cannot match FILENAME "/path//dir/file.c" - as user has requested
00201      absolute path.  The sama applies for "c:\file.c" possibly
00202      incorrectly hypothetically matching "d:\dir\c:\file.c".
00203 
00204      The HAS_DRIVE_SPEC purpose is to make FILENAME "c:file.c"
00205      compatible with SEARCH_NAME "file.c".  In such case a compiler had
00206      to put the "c:file.c" name into debug info.  Such compatibility
00207      works only on GDB built for DOS host.  */
00208   return (len == search_len
00209           || (!IS_ABSOLUTE_PATH (search_name)
00210               && IS_DIR_SEPARATOR (filename[len - search_len - 1]))
00211           || (HAS_DRIVE_SPEC (filename)
00212               && STRIP_DRIVE_SPEC (filename) == &filename[len - search_len]));
00213 }
00214 
00215 /* Check for a symtab of a specific name by searching some symtabs.
00216    This is a helper function for callbacks of iterate_over_symtabs.
00217 
00218    If NAME is not absolute, then REAL_PATH is NULL
00219    If NAME is absolute, then REAL_PATH is the gdb_realpath form of NAME.
00220 
00221    The return value, NAME, REAL_PATH, CALLBACK, and DATA
00222    are identical to the `map_symtabs_matching_filename' method of
00223    quick_symbol_functions.
00224 
00225    FIRST and AFTER_LAST indicate the range of symtabs to search.
00226    AFTER_LAST is one past the last symtab to search; NULL means to
00227    search until the end of the list.  */
00228 
00229 int
00230 iterate_over_some_symtabs (const char *name,
00231                            const char *real_path,
00232                            int (*callback) (struct symtab *symtab,
00233                                             void *data),
00234                            void *data,
00235                            struct symtab *first,
00236                            struct symtab *after_last)
00237 {
00238   struct symtab *s = NULL;
00239   const char* base_name = lbasename (name);
00240 
00241   for (s = first; s != NULL && s != after_last; s = s->next)
00242     {
00243       if (compare_filenames_for_search (s->filename, name))
00244         {
00245           if (callback (s, data))
00246             return 1;
00247           continue;
00248         }
00249 
00250       /* Before we invoke realpath, which can get expensive when many
00251          files are involved, do a quick comparison of the basenames.  */
00252       if (! basenames_may_differ
00253           && FILENAME_CMP (base_name, lbasename (s->filename)) != 0)
00254         continue;
00255 
00256       if (compare_filenames_for_search (symtab_to_fullname (s), name))
00257         {
00258           if (callback (s, data))
00259             return 1;
00260           continue;
00261         }
00262 
00263       /* If the user gave us an absolute path, try to find the file in
00264          this symtab and use its absolute path.  */
00265       if (real_path != NULL)
00266         {
00267           const char *fullname = symtab_to_fullname (s);
00268 
00269           gdb_assert (IS_ABSOLUTE_PATH (real_path));
00270           gdb_assert (IS_ABSOLUTE_PATH (name));
00271           if (FILENAME_CMP (real_path, fullname) == 0)
00272             {
00273               if (callback (s, data))
00274                 return 1;
00275               continue;
00276             }
00277         }
00278     }
00279 
00280   return 0;
00281 }
00282 
00283 /* Check for a symtab of a specific name; first in symtabs, then in
00284    psymtabs.  *If* there is no '/' in the name, a match after a '/'
00285    in the symtab filename will also work.
00286 
00287    Calls CALLBACK with each symtab that is found and with the supplied
00288    DATA.  If CALLBACK returns true, the search stops.  */
00289 
00290 void
00291 iterate_over_symtabs (const char *name,
00292                       int (*callback) (struct symtab *symtab,
00293                                        void *data),
00294                       void *data)
00295 {
00296   struct objfile *objfile;
00297   char *real_path = NULL;
00298   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
00299 
00300   /* Here we are interested in canonicalizing an absolute path, not
00301      absolutizing a relative path.  */
00302   if (IS_ABSOLUTE_PATH (name))
00303     {
00304       real_path = gdb_realpath (name);
00305       make_cleanup (xfree, real_path);
00306       gdb_assert (IS_ABSOLUTE_PATH (real_path));
00307     }
00308 
00309   ALL_OBJFILES (objfile)
00310   {
00311     if (iterate_over_some_symtabs (name, real_path, callback, data,
00312                                    objfile->symtabs, NULL))
00313       {
00314         do_cleanups (cleanups);
00315         return;
00316       }
00317   }
00318 
00319   /* Same search rules as above apply here, but now we look thru the
00320      psymtabs.  */
00321 
00322   ALL_OBJFILES (objfile)
00323   {
00324     if (objfile->sf
00325         && objfile->sf->qf->map_symtabs_matching_filename (objfile,
00326                                                            name,
00327                                                            real_path,
00328                                                            callback,
00329                                                            data))
00330       {
00331         do_cleanups (cleanups);
00332         return;
00333       }
00334   }
00335 
00336   do_cleanups (cleanups);
00337 }
00338 
00339 /* The callback function used by lookup_symtab.  */
00340 
00341 static int
00342 lookup_symtab_callback (struct symtab *symtab, void *data)
00343 {
00344   struct symtab **result_ptr = data;
00345 
00346   *result_ptr = symtab;
00347   return 1;
00348 }
00349 
00350 /* A wrapper for iterate_over_symtabs that returns the first matching
00351    symtab, or NULL.  */
00352 
00353 struct symtab *
00354 lookup_symtab (const char *name)
00355 {
00356   struct symtab *result = NULL;
00357 
00358   iterate_over_symtabs (name, lookup_symtab_callback, &result);
00359   return result;
00360 }
00361 
00362 
00363 /* Mangle a GDB method stub type.  This actually reassembles the pieces of the
00364    full method name, which consist of the class name (from T), the unadorned
00365    method name from METHOD_ID, and the signature for the specific overload,
00366    specified by SIGNATURE_ID.  Note that this function is g++ specific.  */
00367 
00368 char *
00369 gdb_mangle_name (struct type *type, int method_id, int signature_id)
00370 {
00371   int mangled_name_len;
00372   char *mangled_name;
00373   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
00374   struct fn_field *method = &f[signature_id];
00375   const char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
00376   const char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
00377   const char *newname = type_name_no_tag (type);
00378 
00379   /* Does the form of physname indicate that it is the full mangled name
00380      of a constructor (not just the args)?  */
00381   int is_full_physname_constructor;
00382 
00383   int is_constructor;
00384   int is_destructor = is_destructor_name (physname);
00385   /* Need a new type prefix.  */
00386   char *const_prefix = method->is_const ? "C" : "";
00387   char *volatile_prefix = method->is_volatile ? "V" : "";
00388   char buf[20];
00389   int len = (newname == NULL ? 0 : strlen (newname));
00390 
00391   /* Nothing to do if physname already contains a fully mangled v3 abi name
00392      or an operator name.  */
00393   if ((physname[0] == '_' && physname[1] == 'Z')
00394       || is_operator_name (field_name))
00395     return xstrdup (physname);
00396 
00397   is_full_physname_constructor = is_constructor_name (physname);
00398 
00399   is_constructor = is_full_physname_constructor 
00400     || (newname && strcmp (field_name, newname) == 0);
00401 
00402   if (!is_destructor)
00403     is_destructor = (strncmp (physname, "__dt", 4) == 0);
00404 
00405   if (is_destructor || is_full_physname_constructor)
00406     {
00407       mangled_name = (char *) xmalloc (strlen (physname) + 1);
00408       strcpy (mangled_name, physname);
00409       return mangled_name;
00410     }
00411 
00412   if (len == 0)
00413     {
00414       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
00415     }
00416   else if (physname[0] == 't' || physname[0] == 'Q')
00417     {
00418       /* The physname for template and qualified methods already includes
00419          the class name.  */
00420       xsnprintf (buf, sizeof (buf), "__%s%s", const_prefix, volatile_prefix);
00421       newname = NULL;
00422       len = 0;
00423     }
00424   else
00425     {
00426       xsnprintf (buf, sizeof (buf), "__%s%s%d", const_prefix,
00427                  volatile_prefix, len);
00428     }
00429   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
00430                       + strlen (buf) + len + strlen (physname) + 1);
00431 
00432   mangled_name = (char *) xmalloc (mangled_name_len);
00433   if (is_constructor)
00434     mangled_name[0] = '\0';
00435   else
00436     strcpy (mangled_name, field_name);
00437 
00438   strcat (mangled_name, buf);
00439   /* If the class doesn't have a name, i.e. newname NULL, then we just
00440      mangle it using 0 for the length of the class.  Thus it gets mangled
00441      as something starting with `::' rather than `classname::'.  */
00442   if (newname != NULL)
00443     strcat (mangled_name, newname);
00444 
00445   strcat (mangled_name, physname);
00446   return (mangled_name);
00447 }
00448 
00449 /* Initialize the cplus_specific structure.  'cplus_specific' should
00450    only be allocated for use with cplus symbols.  */
00451 
00452 static void
00453 symbol_init_cplus_specific (struct general_symbol_info *gsymbol,
00454                             struct obstack *obstack)
00455 {
00456   /* A language_specific structure should not have been previously
00457      initialized.  */
00458   gdb_assert (gsymbol->language_specific.cplus_specific == NULL);
00459   gdb_assert (obstack != NULL);
00460 
00461   gsymbol->language_specific.cplus_specific =
00462     OBSTACK_ZALLOC (obstack, struct cplus_specific);
00463 }
00464 
00465 /* Set the demangled name of GSYMBOL to NAME.  NAME must be already
00466    correctly allocated.  For C++ symbols a cplus_specific struct is
00467    allocated so OBJFILE must not be NULL.  If this is a non C++ symbol
00468    OBJFILE can be NULL.  */
00469 
00470 void
00471 symbol_set_demangled_name (struct general_symbol_info *gsymbol,
00472                            const char *name,
00473                            struct obstack *obstack)
00474 {
00475   if (gsymbol->language == language_cplus)
00476     {
00477       if (gsymbol->language_specific.cplus_specific == NULL)
00478         symbol_init_cplus_specific (gsymbol, obstack);
00479 
00480       gsymbol->language_specific.cplus_specific->demangled_name = name;
00481     }
00482   else if (gsymbol->language == language_ada)
00483     {
00484       if (name == NULL)
00485         {
00486           gsymbol->ada_mangled = 0;
00487           gsymbol->language_specific.obstack = obstack;
00488         }
00489       else
00490         {
00491           gsymbol->ada_mangled = 1;
00492           gsymbol->language_specific.mangled_lang.demangled_name = name;
00493         }
00494     }
00495   else
00496     gsymbol->language_specific.mangled_lang.demangled_name = name;
00497 }
00498 
00499 /* Return the demangled name of GSYMBOL.  */
00500 
00501 const char *
00502 symbol_get_demangled_name (const struct general_symbol_info *gsymbol)
00503 {
00504   if (gsymbol->language == language_cplus)
00505     {
00506       if (gsymbol->language_specific.cplus_specific != NULL)
00507         return gsymbol->language_specific.cplus_specific->demangled_name;
00508       else
00509         return NULL;
00510     }
00511   else if (gsymbol->language == language_ada)
00512     {
00513       if (!gsymbol->ada_mangled)
00514         return NULL;
00515       /* Fall through.  */
00516     }
00517 
00518   return gsymbol->language_specific.mangled_lang.demangled_name;
00519 }
00520 
00521 
00522 /* Initialize the language dependent portion of a symbol
00523    depending upon the language for the symbol.  */
00524 
00525 void
00526 symbol_set_language (struct general_symbol_info *gsymbol,
00527                      enum language language,
00528                      struct obstack *obstack)
00529 {
00530   gsymbol->language = language;
00531   if (gsymbol->language == language_d
00532       || gsymbol->language == language_go
00533       || gsymbol->language == language_java
00534       || gsymbol->language == language_objc
00535       || gsymbol->language == language_fortran)
00536     {
00537       symbol_set_demangled_name (gsymbol, NULL, obstack);
00538     }
00539   else if (gsymbol->language == language_ada)
00540     {
00541       gdb_assert (gsymbol->ada_mangled == 0);
00542       gsymbol->language_specific.obstack = obstack;
00543     }
00544   else if (gsymbol->language == language_cplus)
00545     gsymbol->language_specific.cplus_specific = NULL;
00546   else
00547     {
00548       memset (&gsymbol->language_specific, 0,
00549               sizeof (gsymbol->language_specific));
00550     }
00551 }
00552 
00553 /* Functions to initialize a symbol's mangled name.  */
00554 
00555 /* Objects of this type are stored in the demangled name hash table.  */
00556 struct demangled_name_entry
00557 {
00558   const char *mangled;
00559   char demangled[1];
00560 };
00561 
00562 /* Hash function for the demangled name hash.  */
00563 
00564 static hashval_t
00565 hash_demangled_name_entry (const void *data)
00566 {
00567   const struct demangled_name_entry *e = data;
00568 
00569   return htab_hash_string (e->mangled);
00570 }
00571 
00572 /* Equality function for the demangled name hash.  */
00573 
00574 static int
00575 eq_demangled_name_entry (const void *a, const void *b)
00576 {
00577   const struct demangled_name_entry *da = a;
00578   const struct demangled_name_entry *db = b;
00579 
00580   return strcmp (da->mangled, db->mangled) == 0;
00581 }
00582 
00583 /* Create the hash table used for demangled names.  Each hash entry is
00584    a pair of strings; one for the mangled name and one for the demangled
00585    name.  The entry is hashed via just the mangled name.  */
00586 
00587 static void
00588 create_demangled_names_hash (struct objfile *objfile)
00589 {
00590   /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
00591      The hash table code will round this up to the next prime number.
00592      Choosing a much larger table size wastes memory, and saves only about
00593      1% in symbol reading.  */
00594 
00595   objfile->per_bfd->demangled_names_hash = htab_create_alloc
00596     (256, hash_demangled_name_entry, eq_demangled_name_entry,
00597      NULL, xcalloc, xfree);
00598 }
00599 
00600 /* Try to determine the demangled name for a symbol, based on the
00601    language of that symbol.  If the language is set to language_auto,
00602    it will attempt to find any demangling algorithm that works and
00603    then set the language appropriately.  The returned name is allocated
00604    by the demangler and should be xfree'd.  */
00605 
00606 static char *
00607 symbol_find_demangled_name (struct general_symbol_info *gsymbol,
00608                             const char *mangled)
00609 {
00610   char *demangled = NULL;
00611 
00612   if (gsymbol->language == language_unknown)
00613     gsymbol->language = language_auto;
00614 
00615   if (gsymbol->language == language_objc
00616       || gsymbol->language == language_auto)
00617     {
00618       demangled =
00619         objc_demangle (mangled, 0);
00620       if (demangled != NULL)
00621         {
00622           gsymbol->language = language_objc;
00623           return demangled;
00624         }
00625     }
00626   if (gsymbol->language == language_cplus
00627       || gsymbol->language == language_auto)
00628     {
00629       demangled =
00630         gdb_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
00631       if (demangled != NULL)
00632         {
00633           gsymbol->language = language_cplus;
00634           return demangled;
00635         }
00636     }
00637   if (gsymbol->language == language_java)
00638     {
00639       demangled =
00640         gdb_demangle (mangled,
00641                       DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
00642       if (demangled != NULL)
00643         {
00644           gsymbol->language = language_java;
00645           return demangled;
00646         }
00647     }
00648   if (gsymbol->language == language_d
00649       || gsymbol->language == language_auto)
00650     {
00651       demangled = d_demangle(mangled, 0);
00652       if (demangled != NULL)
00653         {
00654           gsymbol->language = language_d;
00655           return demangled;
00656         }
00657     }
00658   /* FIXME(dje): Continually adding languages here is clumsy.
00659      Better to just call la_demangle if !auto, and if auto then call
00660      a utility routine that tries successive languages in turn and reports
00661      which one it finds.  I realize the la_demangle options may be different
00662      for different languages but there's already a FIXME for that.  */
00663   if (gsymbol->language == language_go
00664       || gsymbol->language == language_auto)
00665     {
00666       demangled = go_demangle (mangled, 0);
00667       if (demangled != NULL)
00668         {
00669           gsymbol->language = language_go;
00670           return demangled;
00671         }
00672     }
00673 
00674   /* We could support `gsymbol->language == language_fortran' here to provide
00675      module namespaces also for inferiors with only minimal symbol table (ELF
00676      symbols).  Just the mangling standard is not standardized across compilers
00677      and there is no DW_AT_producer available for inferiors with only the ELF
00678      symbols to check the mangling kind.  */
00679   return NULL;
00680 }
00681 
00682 /* Set both the mangled and demangled (if any) names for GSYMBOL based
00683    on LINKAGE_NAME and LEN.  Ordinarily, NAME is copied onto the
00684    objfile's obstack; but if COPY_NAME is 0 and if NAME is
00685    NUL-terminated, then this function assumes that NAME is already
00686    correctly saved (either permanently or with a lifetime tied to the
00687    objfile), and it will not be copied.
00688 
00689    The hash table corresponding to OBJFILE is used, and the memory
00690    comes from the per-BFD storage_obstack.  LINKAGE_NAME is copied,
00691    so the pointer can be discarded after calling this function.  */
00692 
00693 /* We have to be careful when dealing with Java names: when we run
00694    into a Java minimal symbol, we don't know it's a Java symbol, so it
00695    gets demangled as a C++ name.  This is unfortunate, but there's not
00696    much we can do about it: but when demangling partial symbols and
00697    regular symbols, we'd better not reuse the wrong demangled name.
00698    (See PR gdb/1039.)  We solve this by putting a distinctive prefix
00699    on Java names when storing them in the hash table.  */
00700 
00701 /* FIXME: carlton/2003-03-13: This is an unfortunate situation.  I
00702    don't mind the Java prefix so much: different languages have
00703    different demangling requirements, so it's only natural that we
00704    need to keep language data around in our demangling cache.  But
00705    it's not good that the minimal symbol has the wrong demangled name.
00706    Unfortunately, I can't think of any easy solution to that
00707    problem.  */
00708 
00709 #define JAVA_PREFIX "##JAVA$$"
00710 #define JAVA_PREFIX_LEN 8
00711 
00712 void
00713 symbol_set_names (struct general_symbol_info *gsymbol,
00714                   const char *linkage_name, int len, int copy_name,
00715                   struct objfile *objfile)
00716 {
00717   struct demangled_name_entry **slot;
00718   /* A 0-terminated copy of the linkage name.  */
00719   const char *linkage_name_copy;
00720   /* A copy of the linkage name that might have a special Java prefix
00721      added to it, for use when looking names up in the hash table.  */
00722   const char *lookup_name;
00723   /* The length of lookup_name.  */
00724   int lookup_len;
00725   struct demangled_name_entry entry;
00726   struct objfile_per_bfd_storage *per_bfd = objfile->per_bfd;
00727 
00728   if (gsymbol->language == language_ada)
00729     {
00730       /* In Ada, we do the symbol lookups using the mangled name, so
00731          we can save some space by not storing the demangled name.
00732 
00733          As a side note, we have also observed some overlap between
00734          the C++ mangling and Ada mangling, similarly to what has
00735          been observed with Java.  Because we don't store the demangled
00736          name with the symbol, we don't need to use the same trick
00737          as Java.  */
00738       if (!copy_name)
00739         gsymbol->name = linkage_name;
00740       else
00741         {
00742           char *name = obstack_alloc (&per_bfd->storage_obstack, len + 1);
00743 
00744           memcpy (name, linkage_name, len);
00745           name[len] = '\0';
00746           gsymbol->name = name;
00747         }
00748       symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
00749 
00750       return;
00751     }
00752 
00753   if (per_bfd->demangled_names_hash == NULL)
00754     create_demangled_names_hash (objfile);
00755 
00756   /* The stabs reader generally provides names that are not
00757      NUL-terminated; most of the other readers don't do this, so we
00758      can just use the given copy, unless we're in the Java case.  */
00759   if (gsymbol->language == language_java)
00760     {
00761       char *alloc_name;
00762 
00763       lookup_len = len + JAVA_PREFIX_LEN;
00764       alloc_name = alloca (lookup_len + 1);
00765       memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
00766       memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
00767       alloc_name[lookup_len] = '\0';
00768 
00769       lookup_name = alloc_name;
00770       linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
00771     }
00772   else if (linkage_name[len] != '\0')
00773     {
00774       char *alloc_name;
00775 
00776       lookup_len = len;
00777       alloc_name = alloca (lookup_len + 1);
00778       memcpy (alloc_name, linkage_name, len);
00779       alloc_name[lookup_len] = '\0';
00780 
00781       lookup_name = alloc_name;
00782       linkage_name_copy = alloc_name;
00783     }
00784   else
00785     {
00786       lookup_len = len;
00787       lookup_name = linkage_name;
00788       linkage_name_copy = linkage_name;
00789     }
00790 
00791   entry.mangled = lookup_name;
00792   slot = ((struct demangled_name_entry **)
00793           htab_find_slot (per_bfd->demangled_names_hash,
00794                           &entry, INSERT));
00795 
00796   /* If this name is not in the hash table, add it.  */
00797   if (*slot == NULL
00798       /* A C version of the symbol may have already snuck into the table.
00799          This happens to, e.g., main.init (__go_init_main).  Cope.  */
00800       || (gsymbol->language == language_go
00801           && (*slot)->demangled[0] == '\0'))
00802     {
00803       char *demangled_name = symbol_find_demangled_name (gsymbol,
00804                                                          linkage_name_copy);
00805       int demangled_len = demangled_name ? strlen (demangled_name) : 0;
00806 
00807       /* Suppose we have demangled_name==NULL, copy_name==0, and
00808          lookup_name==linkage_name.  In this case, we already have the
00809          mangled name saved, and we don't have a demangled name.  So,
00810          you might think we could save a little space by not recording
00811          this in the hash table at all.
00812          
00813          It turns out that it is actually important to still save such
00814          an entry in the hash table, because storing this name gives
00815          us better bcache hit rates for partial symbols.  */
00816       if (!copy_name && lookup_name == linkage_name)
00817         {
00818           *slot = obstack_alloc (&per_bfd->storage_obstack,
00819                                  offsetof (struct demangled_name_entry,
00820                                            demangled)
00821                                  + demangled_len + 1);
00822           (*slot)->mangled = lookup_name;
00823         }
00824       else
00825         {
00826           char *mangled_ptr;
00827 
00828           /* If we must copy the mangled name, put it directly after
00829              the demangled name so we can have a single
00830              allocation.  */
00831           *slot = obstack_alloc (&per_bfd->storage_obstack,
00832                                  offsetof (struct demangled_name_entry,
00833                                            demangled)
00834                                  + lookup_len + demangled_len + 2);
00835           mangled_ptr = &((*slot)->demangled[demangled_len + 1]);
00836           strcpy (mangled_ptr, lookup_name);
00837           (*slot)->mangled = mangled_ptr;
00838         }
00839 
00840       if (demangled_name != NULL)
00841         {
00842           strcpy ((*slot)->demangled, demangled_name);
00843           xfree (demangled_name);
00844         }
00845       else
00846         (*slot)->demangled[0] = '\0';
00847     }
00848 
00849   gsymbol->name = (*slot)->mangled + lookup_len - len;
00850   if ((*slot)->demangled[0] != '\0')
00851     symbol_set_demangled_name (gsymbol, (*slot)->demangled,
00852                                &per_bfd->storage_obstack);
00853   else
00854     symbol_set_demangled_name (gsymbol, NULL, &per_bfd->storage_obstack);
00855 }
00856 
00857 /* Return the source code name of a symbol.  In languages where
00858    demangling is necessary, this is the demangled name.  */
00859 
00860 const char *
00861 symbol_natural_name (const struct general_symbol_info *gsymbol)
00862 {
00863   switch (gsymbol->language)
00864     {
00865     case language_cplus:
00866     case language_d:
00867     case language_go:
00868     case language_java:
00869     case language_objc:
00870     case language_fortran:
00871       if (symbol_get_demangled_name (gsymbol) != NULL)
00872         return symbol_get_demangled_name (gsymbol);
00873       break;
00874     case language_ada:
00875       return ada_decode_symbol (gsymbol);
00876     default:
00877       break;
00878     }
00879   return gsymbol->name;
00880 }
00881 
00882 /* Return the demangled name for a symbol based on the language for
00883    that symbol.  If no demangled name exists, return NULL.  */
00884 
00885 const char *
00886 symbol_demangled_name (const struct general_symbol_info *gsymbol)
00887 {
00888   const char *dem_name = NULL;
00889 
00890   switch (gsymbol->language)
00891     {
00892     case language_cplus:
00893     case language_d:
00894     case language_go:
00895     case language_java:
00896     case language_objc:
00897     case language_fortran:
00898       dem_name = symbol_get_demangled_name (gsymbol);
00899       break;
00900     case language_ada:
00901       dem_name = ada_decode_symbol (gsymbol);
00902       break;
00903     default:
00904       break;
00905     }
00906   return dem_name;
00907 }
00908 
00909 /* Return the search name of a symbol---generally the demangled or
00910    linkage name of the symbol, depending on how it will be searched for.
00911    If there is no distinct demangled name, then returns the same value
00912    (same pointer) as SYMBOL_LINKAGE_NAME.  */
00913 
00914 const char *
00915 symbol_search_name (const struct general_symbol_info *gsymbol)
00916 {
00917   if (gsymbol->language == language_ada)
00918     return gsymbol->name;
00919   else
00920     return symbol_natural_name (gsymbol);
00921 }
00922 
00923 /* Initialize the structure fields to zero values.  */
00924 
00925 void
00926 init_sal (struct symtab_and_line *sal)
00927 {
00928   sal->pspace = NULL;
00929   sal->symtab = 0;
00930   sal->section = 0;
00931   sal->line = 0;
00932   sal->pc = 0;
00933   sal->end = 0;
00934   sal->explicit_pc = 0;
00935   sal->explicit_line = 0;
00936   sal->probe = NULL;
00937 }
00938 
00939 
00940 /* Return 1 if the two sections are the same, or if they could
00941    plausibly be copies of each other, one in an original object
00942    file and another in a separated debug file.  */
00943 
00944 int
00945 matching_obj_sections (struct obj_section *obj_first,
00946                        struct obj_section *obj_second)
00947 {
00948   asection *first = obj_first? obj_first->the_bfd_section : NULL;
00949   asection *second = obj_second? obj_second->the_bfd_section : NULL;
00950   struct objfile *obj;
00951 
00952   /* If they're the same section, then they match.  */
00953   if (first == second)
00954     return 1;
00955 
00956   /* If either is NULL, give up.  */
00957   if (first == NULL || second == NULL)
00958     return 0;
00959 
00960   /* This doesn't apply to absolute symbols.  */
00961   if (first->owner == NULL || second->owner == NULL)
00962     return 0;
00963 
00964   /* If they're in the same object file, they must be different sections.  */
00965   if (first->owner == second->owner)
00966     return 0;
00967 
00968   /* Check whether the two sections are potentially corresponding.  They must
00969      have the same size, address, and name.  We can't compare section indexes,
00970      which would be more reliable, because some sections may have been
00971      stripped.  */
00972   if (bfd_get_section_size (first) != bfd_get_section_size (second))
00973     return 0;
00974 
00975   /* In-memory addresses may start at a different offset, relativize them.  */
00976   if (bfd_get_section_vma (first->owner, first)
00977       - bfd_get_start_address (first->owner)
00978       != bfd_get_section_vma (second->owner, second)
00979          - bfd_get_start_address (second->owner))
00980     return 0;
00981 
00982   if (bfd_get_section_name (first->owner, first) == NULL
00983       || bfd_get_section_name (second->owner, second) == NULL
00984       || strcmp (bfd_get_section_name (first->owner, first),
00985                  bfd_get_section_name (second->owner, second)) != 0)
00986     return 0;
00987 
00988   /* Otherwise check that they are in corresponding objfiles.  */
00989 
00990   ALL_OBJFILES (obj)
00991     if (obj->obfd == first->owner)
00992       break;
00993   gdb_assert (obj != NULL);
00994 
00995   if (obj->separate_debug_objfile != NULL
00996       && obj->separate_debug_objfile->obfd == second->owner)
00997     return 1;
00998   if (obj->separate_debug_objfile_backlink != NULL
00999       && obj->separate_debug_objfile_backlink->obfd == second->owner)
01000     return 1;
01001 
01002   return 0;
01003 }
01004 
01005 struct symtab *
01006 find_pc_sect_symtab_via_partial (CORE_ADDR pc, struct obj_section *section)
01007 {
01008   struct objfile *objfile;
01009   struct minimal_symbol *msymbol;
01010 
01011   /* If we know that this is not a text address, return failure.  This is
01012      necessary because we loop based on texthigh and textlow, which do
01013      not include the data ranges.  */
01014   msymbol = lookup_minimal_symbol_by_pc_section (pc, section).minsym;
01015   if (msymbol
01016       && (MSYMBOL_TYPE (msymbol) == mst_data
01017           || MSYMBOL_TYPE (msymbol) == mst_bss
01018           || MSYMBOL_TYPE (msymbol) == mst_abs
01019           || MSYMBOL_TYPE (msymbol) == mst_file_data
01020           || MSYMBOL_TYPE (msymbol) == mst_file_bss))
01021     return NULL;
01022 
01023   ALL_OBJFILES (objfile)
01024   {
01025     struct symtab *result = NULL;
01026 
01027     if (objfile->sf)
01028       result = objfile->sf->qf->find_pc_sect_symtab (objfile, msymbol,
01029                                                      pc, section, 0);
01030     if (result)
01031       return result;
01032   }
01033 
01034   return NULL;
01035 }
01036 
01037 /* Debug symbols usually don't have section information.  We need to dig that
01038    out of the minimal symbols and stash that in the debug symbol.  */
01039 
01040 void
01041 fixup_section (struct general_symbol_info *ginfo,
01042                CORE_ADDR addr, struct objfile *objfile)
01043 {
01044   struct minimal_symbol *msym;
01045 
01046   /* First, check whether a minimal symbol with the same name exists
01047      and points to the same address.  The address check is required
01048      e.g. on PowerPC64, where the minimal symbol for a function will
01049      point to the function descriptor, while the debug symbol will
01050      point to the actual function code.  */
01051   msym = lookup_minimal_symbol_by_pc_name (addr, ginfo->name, objfile);
01052   if (msym)
01053     ginfo->section = SYMBOL_SECTION (msym);
01054   else
01055     {
01056       /* Static, function-local variables do appear in the linker
01057          (minimal) symbols, but are frequently given names that won't
01058          be found via lookup_minimal_symbol().  E.g., it has been
01059          observed in frv-uclinux (ELF) executables that a static,
01060          function-local variable named "foo" might appear in the
01061          linker symbols as "foo.6" or "foo.3".  Thus, there is no
01062          point in attempting to extend the lookup-by-name mechanism to
01063          handle this case due to the fact that there can be multiple
01064          names.
01065 
01066          So, instead, search the section table when lookup by name has
01067          failed.  The ``addr'' and ``endaddr'' fields may have already
01068          been relocated.  If so, the relocation offset (i.e. the
01069          ANOFFSET value) needs to be subtracted from these values when
01070          performing the comparison.  We unconditionally subtract it,
01071          because, when no relocation has been performed, the ANOFFSET
01072          value will simply be zero.
01073 
01074          The address of the symbol whose section we're fixing up HAS
01075          NOT BEEN adjusted (relocated) yet.  It can't have been since
01076          the section isn't yet known and knowing the section is
01077          necessary in order to add the correct relocation value.  In
01078          other words, we wouldn't even be in this function (attempting
01079          to compute the section) if it were already known.
01080 
01081          Note that it is possible to search the minimal symbols
01082          (subtracting the relocation value if necessary) to find the
01083          matching minimal symbol, but this is overkill and much less
01084          efficient.  It is not necessary to find the matching minimal
01085          symbol, only its section.
01086 
01087          Note that this technique (of doing a section table search)
01088          can fail when unrelocated section addresses overlap.  For
01089          this reason, we still attempt a lookup by name prior to doing
01090          a search of the section table.  */
01091 
01092       struct obj_section *s;
01093       int fallback = -1;
01094 
01095       ALL_OBJFILE_OSECTIONS (objfile, s)
01096         {
01097           int idx = s - objfile->sections;
01098           CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
01099 
01100           if (fallback == -1)
01101             fallback = idx;
01102 
01103           if (obj_section_addr (s) - offset <= addr
01104               && addr < obj_section_endaddr (s) - offset)
01105             {
01106               ginfo->section = idx;
01107               return;
01108             }
01109         }
01110 
01111       /* If we didn't find the section, assume it is in the first
01112          section.  If there is no allocated section, then it hardly
01113          matters what we pick, so just pick zero.  */
01114       if (fallback == -1)
01115         ginfo->section = 0;
01116       else
01117         ginfo->section = fallback;
01118     }
01119 }
01120 
01121 struct symbol *
01122 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
01123 {
01124   CORE_ADDR addr;
01125 
01126   if (!sym)
01127     return NULL;
01128 
01129   /* We either have an OBJFILE, or we can get at it from the sym's
01130      symtab.  Anything else is a bug.  */
01131   gdb_assert (objfile || SYMBOL_SYMTAB (sym));
01132 
01133   if (objfile == NULL)
01134     objfile = SYMBOL_SYMTAB (sym)->objfile;
01135 
01136   if (SYMBOL_OBJ_SECTION (objfile, sym))
01137     return sym;
01138 
01139   /* We should have an objfile by now.  */
01140   gdb_assert (objfile);
01141 
01142   switch (SYMBOL_CLASS (sym))
01143     {
01144     case LOC_STATIC:
01145     case LOC_LABEL:
01146       addr = SYMBOL_VALUE_ADDRESS (sym);
01147       break;
01148     case LOC_BLOCK:
01149       addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
01150       break;
01151 
01152     default:
01153       /* Nothing else will be listed in the minsyms -- no use looking
01154          it up.  */
01155       return sym;
01156     }
01157 
01158   fixup_section (&sym->ginfo, addr, objfile);
01159 
01160   return sym;
01161 }
01162 
01163 /* Compute the demangled form of NAME as used by the various symbol
01164    lookup functions.  The result is stored in *RESULT_NAME.  Returns a
01165    cleanup which can be used to clean up the result.
01166 
01167    For Ada, this function just sets *RESULT_NAME to NAME, unmodified.
01168    Normally, Ada symbol lookups are performed using the encoded name
01169    rather than the demangled name, and so it might seem to make sense
01170    for this function to return an encoded version of NAME.
01171    Unfortunately, we cannot do this, because this function is used in
01172    circumstances where it is not appropriate to try to encode NAME.
01173    For instance, when displaying the frame info, we demangle the name
01174    of each parameter, and then perform a symbol lookup inside our
01175    function using that demangled name.  In Ada, certain functions
01176    have internally-generated parameters whose name contain uppercase
01177    characters.  Encoding those name would result in those uppercase
01178    characters to become lowercase, and thus cause the symbol lookup
01179    to fail.  */
01180 
01181 struct cleanup *
01182 demangle_for_lookup (const char *name, enum language lang,
01183                      const char **result_name)
01184 {
01185   char *demangled_name = NULL;
01186   const char *modified_name = NULL;
01187   struct cleanup *cleanup = make_cleanup (null_cleanup, 0);
01188 
01189   modified_name = name;
01190 
01191   /* If we are using C++, D, Go, or Java, demangle the name before doing a
01192      lookup, so we can always binary search.  */
01193   if (lang == language_cplus)
01194     {
01195       demangled_name = gdb_demangle (name, DMGL_ANSI | DMGL_PARAMS);
01196       if (demangled_name)
01197         {
01198           modified_name = demangled_name;
01199           make_cleanup (xfree, demangled_name);
01200         }
01201       else
01202         {
01203           /* If we were given a non-mangled name, canonicalize it
01204              according to the language (so far only for C++).  */
01205           demangled_name = cp_canonicalize_string (name);
01206           if (demangled_name)
01207             {
01208               modified_name = demangled_name;
01209               make_cleanup (xfree, demangled_name);
01210             }
01211         }
01212     }
01213   else if (lang == language_java)
01214     {
01215       demangled_name = gdb_demangle (name,
01216                                      DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
01217       if (demangled_name)
01218         {
01219           modified_name = demangled_name;
01220           make_cleanup (xfree, demangled_name);
01221         }
01222     }
01223   else if (lang == language_d)
01224     {
01225       demangled_name = d_demangle (name, 0);
01226       if (demangled_name)
01227         {
01228           modified_name = demangled_name;
01229           make_cleanup (xfree, demangled_name);
01230         }
01231     }
01232   else if (lang == language_go)
01233     {
01234       demangled_name = go_demangle (name, 0);
01235       if (demangled_name)
01236         {
01237           modified_name = demangled_name;
01238           make_cleanup (xfree, demangled_name);
01239         }
01240     }
01241 
01242   *result_name = modified_name;
01243   return cleanup;
01244 }
01245 
01246 /* Find the definition for a specified symbol name NAME
01247    in domain DOMAIN, visible from lexical block BLOCK.
01248    Returns the struct symbol pointer, or zero if no symbol is found.
01249    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
01250    NAME is a field of the current implied argument `this'.  If so set
01251    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
01252    BLOCK_FOUND is set to the block in which NAME is found (in the case of
01253    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.)  */
01254 
01255 /* This function (or rather its subordinates) have a bunch of loops and
01256    it would seem to be attractive to put in some QUIT's (though I'm not really
01257    sure whether it can run long enough to be really important).  But there
01258    are a few calls for which it would appear to be bad news to quit
01259    out of here: e.g., find_proc_desc in alpha-mdebug-tdep.c.  (Note
01260    that there is C++ code below which can error(), but that probably
01261    doesn't affect these calls since they are looking for a known
01262    variable and thus can probably assume it will never hit the C++
01263    code).  */
01264 
01265 struct symbol *
01266 lookup_symbol_in_language (const char *name, const struct block *block,
01267                            const domain_enum domain, enum language lang,
01268                            struct field_of_this_result *is_a_field_of_this)
01269 {
01270   const char *modified_name;
01271   struct symbol *returnval;
01272   struct cleanup *cleanup = demangle_for_lookup (name, lang, &modified_name);
01273 
01274   returnval = lookup_symbol_aux (modified_name, block, domain, lang,
01275                                  is_a_field_of_this);
01276   do_cleanups (cleanup);
01277 
01278   return returnval;
01279 }
01280 
01281 /* Behave like lookup_symbol_in_language, but performed with the
01282    current language.  */
01283 
01284 struct symbol *
01285 lookup_symbol (const char *name, const struct block *block,
01286                domain_enum domain,
01287                struct field_of_this_result *is_a_field_of_this)
01288 {
01289   return lookup_symbol_in_language (name, block, domain,
01290                                     current_language->la_language,
01291                                     is_a_field_of_this);
01292 }
01293 
01294 /* Look up the `this' symbol for LANG in BLOCK.  Return the symbol if
01295    found, or NULL if not found.  */
01296 
01297 struct symbol *
01298 lookup_language_this (const struct language_defn *lang,
01299                       const struct block *block)
01300 {
01301   if (lang->la_name_of_this == NULL || block == NULL)
01302     return NULL;
01303 
01304   while (block)
01305     {
01306       struct symbol *sym;
01307 
01308       sym = lookup_block_symbol (block, lang->la_name_of_this, VAR_DOMAIN);
01309       if (sym != NULL)
01310         {
01311           block_found = block;
01312           return sym;
01313         }
01314       if (BLOCK_FUNCTION (block))
01315         break;
01316       block = BLOCK_SUPERBLOCK (block);
01317     }
01318 
01319   return NULL;
01320 }
01321 
01322 /* Given TYPE, a structure/union,
01323    return 1 if the component named NAME from the ultimate target
01324    structure/union is defined, otherwise, return 0.  */
01325 
01326 static int
01327 check_field (struct type *type, const char *name,
01328              struct field_of_this_result *is_a_field_of_this)
01329 {
01330   int i;
01331 
01332   /* The type may be a stub.  */
01333   CHECK_TYPEDEF (type);
01334 
01335   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
01336     {
01337       const char *t_field_name = TYPE_FIELD_NAME (type, i);
01338 
01339       if (t_field_name && (strcmp_iw (t_field_name, name) == 0))
01340         {
01341           is_a_field_of_this->type = type;
01342           is_a_field_of_this->field = &TYPE_FIELD (type, i);
01343           return 1;
01344         }
01345     }
01346 
01347   /* C++: If it was not found as a data field, then try to return it
01348      as a pointer to a method.  */
01349 
01350   for (i = TYPE_NFN_FIELDS (type) - 1; i >= 0; --i)
01351     {
01352       if (strcmp_iw (TYPE_FN_FIELDLIST_NAME (type, i), name) == 0)
01353         {
01354           is_a_field_of_this->type = type;
01355           is_a_field_of_this->fn_field = &TYPE_FN_FIELDLIST (type, i);
01356           return 1;
01357         }
01358     }
01359 
01360   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
01361     if (check_field (TYPE_BASECLASS (type, i), name, is_a_field_of_this))
01362       return 1;
01363 
01364   return 0;
01365 }
01366 
01367 /* Behave like lookup_symbol except that NAME is the natural name
01368    (e.g., demangled name) of the symbol that we're looking for.  */
01369 
01370 static struct symbol *
01371 lookup_symbol_aux (const char *name, const struct block *block,
01372                    const domain_enum domain, enum language language,
01373                    struct field_of_this_result *is_a_field_of_this)
01374 {
01375   struct symbol *sym;
01376   const struct language_defn *langdef;
01377 
01378   /* Make sure we do something sensible with is_a_field_of_this, since
01379      the callers that set this parameter to some non-null value will
01380      certainly use it later.  If we don't set it, the contents of
01381      is_a_field_of_this are undefined.  */
01382   if (is_a_field_of_this != NULL)
01383     memset (is_a_field_of_this, 0, sizeof (*is_a_field_of_this));
01384 
01385   /* Search specified block and its superiors.  Don't search
01386      STATIC_BLOCK or GLOBAL_BLOCK.  */
01387 
01388   sym = lookup_symbol_aux_local (name, block, domain, language);
01389   if (sym != NULL)
01390     return sym;
01391 
01392   /* If requested to do so by the caller and if appropriate for LANGUAGE,
01393      check to see if NAME is a field of `this'.  */
01394 
01395   langdef = language_def (language);
01396 
01397   /* Don't do this check if we are searching for a struct.  It will
01398      not be found by check_field, but will be found by other
01399      means.  */
01400   if (is_a_field_of_this != NULL && domain != STRUCT_DOMAIN)
01401     {
01402       struct symbol *sym = lookup_language_this (langdef, block);
01403 
01404       if (sym)
01405         {
01406           struct type *t = sym->type;
01407 
01408           /* I'm not really sure that type of this can ever
01409              be typedefed; just be safe.  */
01410           CHECK_TYPEDEF (t);
01411           if (TYPE_CODE (t) == TYPE_CODE_PTR
01412               || TYPE_CODE (t) == TYPE_CODE_REF)
01413             t = TYPE_TARGET_TYPE (t);
01414 
01415           if (TYPE_CODE (t) != TYPE_CODE_STRUCT
01416               && TYPE_CODE (t) != TYPE_CODE_UNION)
01417             error (_("Internal error: `%s' is not an aggregate"),
01418                    langdef->la_name_of_this);
01419 
01420           if (check_field (t, name, is_a_field_of_this))
01421             return NULL;
01422         }
01423     }
01424 
01425   /* Now do whatever is appropriate for LANGUAGE to look
01426      up static and global variables.  */
01427 
01428   sym = langdef->la_lookup_symbol_nonlocal (name, block, domain);
01429   if (sym != NULL)
01430     return sym;
01431 
01432   /* Now search all static file-level symbols.  Not strictly correct,
01433      but more useful than an error.  */
01434 
01435   return lookup_static_symbol_aux (name, domain);
01436 }
01437 
01438 /* Search all static file-level symbols for NAME from DOMAIN.  Do the symtabs
01439    first, then check the psymtabs.  If a psymtab indicates the existence of the
01440    desired name as a file-level static, then do psymtab-to-symtab conversion on
01441    the fly and return the found symbol.  */
01442 
01443 struct symbol *
01444 lookup_static_symbol_aux (const char *name, const domain_enum domain)
01445 {
01446   struct objfile *objfile;
01447   struct symbol *sym;
01448 
01449   sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, domain);
01450   if (sym != NULL)
01451     return sym;
01452 
01453   ALL_OBJFILES (objfile)
01454   {
01455     sym = lookup_symbol_aux_quick (objfile, STATIC_BLOCK, name, domain);
01456     if (sym != NULL)
01457       return sym;
01458   }
01459 
01460   return NULL;
01461 }
01462 
01463 /* Check to see if the symbol is defined in BLOCK or its superiors.
01464    Don't search STATIC_BLOCK or GLOBAL_BLOCK.  */
01465 
01466 static struct symbol *
01467 lookup_symbol_aux_local (const char *name, const struct block *block,
01468                          const domain_enum domain,
01469                          enum language language)
01470 {
01471   struct symbol *sym;
01472   const struct block *static_block = block_static_block (block);
01473   const char *scope = block_scope (block);
01474   
01475   /* Check if either no block is specified or it's a global block.  */
01476 
01477   if (static_block == NULL)
01478     return NULL;
01479 
01480   while (block != static_block)
01481     {
01482       sym = lookup_symbol_aux_block (name, block, domain);
01483       if (sym != NULL)
01484         return sym;
01485 
01486       if (language == language_cplus || language == language_fortran)
01487         {
01488           sym = cp_lookup_symbol_imports_or_template (scope, name, block,
01489                                                       domain);
01490           if (sym != NULL)
01491             return sym;
01492         }
01493 
01494       if (BLOCK_FUNCTION (block) != NULL && block_inlined_p (block))
01495         break;
01496       block = BLOCK_SUPERBLOCK (block);
01497     }
01498 
01499   /* We've reached the edge of the function without finding a result.  */
01500 
01501   return NULL;
01502 }
01503 
01504 /* Look up OBJFILE to BLOCK.  */
01505 
01506 struct objfile *
01507 lookup_objfile_from_block (const struct block *block)
01508 {
01509   struct objfile *obj;
01510   struct symtab *s;
01511 
01512   if (block == NULL)
01513     return NULL;
01514 
01515   block = block_global_block (block);
01516   /* Go through SYMTABS.  */
01517   ALL_SYMTABS (obj, s)
01518     if (block == BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK))
01519       {
01520         if (obj->separate_debug_objfile_backlink)
01521           obj = obj->separate_debug_objfile_backlink;
01522 
01523         return obj;
01524       }
01525 
01526   return NULL;
01527 }
01528 
01529 /* Look up a symbol in a block; if found, fixup the symbol, and set
01530    block_found appropriately.  */
01531 
01532 struct symbol *
01533 lookup_symbol_aux_block (const char *name, const struct block *block,
01534                          const domain_enum domain)
01535 {
01536   struct symbol *sym;
01537 
01538   sym = lookup_block_symbol (block, name, domain);
01539   if (sym)
01540     {
01541       block_found = block;
01542       return fixup_symbol_section (sym, NULL);
01543     }
01544 
01545   return NULL;
01546 }
01547 
01548 /* Check all global symbols in OBJFILE in symtabs and
01549    psymtabs.  */
01550 
01551 struct symbol *
01552 lookup_global_symbol_from_objfile (const struct objfile *main_objfile,
01553                                    const char *name,
01554                                    const domain_enum domain)
01555 {
01556   const struct objfile *objfile;
01557   struct symbol *sym;
01558   struct blockvector *bv;
01559   const struct block *block;
01560   struct symtab *s;
01561 
01562   for (objfile = main_objfile;
01563        objfile;
01564        objfile = objfile_separate_debug_iterate (main_objfile, objfile))
01565     {
01566       /* Go through symtabs.  */
01567       ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
01568         {
01569           bv = BLOCKVECTOR (s);
01570           block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
01571           sym = lookup_block_symbol (block, name, domain);
01572           if (sym)
01573             {
01574               block_found = block;
01575               return fixup_symbol_section (sym, (struct objfile *)objfile);
01576             }
01577         }
01578 
01579       sym = lookup_symbol_aux_quick ((struct objfile *) objfile, GLOBAL_BLOCK,
01580                                      name, domain);
01581       if (sym)
01582         return sym;
01583     }
01584 
01585   return NULL;
01586 }
01587 
01588 /* Check to see if the symbol is defined in one of the OBJFILE's
01589    symtabs.  BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
01590    depending on whether or not we want to search global symbols or
01591    static symbols.  */
01592 
01593 static struct symbol *
01594 lookup_symbol_aux_objfile (struct objfile *objfile, int block_index,
01595                            const char *name, const domain_enum domain)
01596 {
01597   struct symbol *sym = NULL;
01598   struct blockvector *bv;
01599   const struct block *block;
01600   struct symtab *s;
01601 
01602   ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
01603     {
01604       bv = BLOCKVECTOR (s);
01605       block = BLOCKVECTOR_BLOCK (bv, block_index);
01606       sym = lookup_block_symbol (block, name, domain);
01607       if (sym)
01608         {
01609           block_found = block;
01610           return fixup_symbol_section (sym, objfile);
01611         }
01612     }
01613 
01614   return NULL;
01615 }
01616 
01617 /* Same as lookup_symbol_aux_objfile, except that it searches all
01618    objfiles.  Return the first match found.  */
01619 
01620 static struct symbol *
01621 lookup_symbol_aux_symtabs (int block_index, const char *name,
01622                            const domain_enum domain)
01623 {
01624   struct symbol *sym;
01625   struct objfile *objfile;
01626 
01627   ALL_OBJFILES (objfile)
01628   {
01629     sym = lookup_symbol_aux_objfile (objfile, block_index, name, domain);
01630     if (sym)
01631       return sym;
01632   }
01633 
01634   return NULL;
01635 }
01636 
01637 /* Wrapper around lookup_symbol_aux_objfile for search_symbols.
01638    Look up LINKAGE_NAME in DOMAIN in the global and static blocks of OBJFILE
01639    and all related objfiles.  */
01640 
01641 static struct symbol *
01642 lookup_symbol_in_objfile_from_linkage_name (struct objfile *objfile,
01643                                             const char *linkage_name,
01644                                             domain_enum domain)
01645 {
01646   enum language lang = current_language->la_language;
01647   const char *modified_name;
01648   struct cleanup *cleanup = demangle_for_lookup (linkage_name, lang,
01649                                                  &modified_name);
01650   struct objfile *main_objfile, *cur_objfile;
01651 
01652   if (objfile->separate_debug_objfile_backlink)
01653     main_objfile = objfile->separate_debug_objfile_backlink;
01654   else
01655     main_objfile = objfile;
01656 
01657   for (cur_objfile = main_objfile;
01658        cur_objfile;
01659        cur_objfile = objfile_separate_debug_iterate (main_objfile, cur_objfile))
01660     {
01661       struct symbol *sym;
01662 
01663       sym = lookup_symbol_aux_objfile (cur_objfile, GLOBAL_BLOCK,
01664                                        modified_name, domain);
01665       if (sym == NULL)
01666         sym = lookup_symbol_aux_objfile (cur_objfile, STATIC_BLOCK,
01667                                          modified_name, domain);
01668       if (sym != NULL)
01669         {
01670           do_cleanups (cleanup);
01671           return sym;
01672         }
01673     }
01674 
01675   do_cleanups (cleanup);
01676   return NULL;
01677 }
01678 
01679 /* A helper function that throws an exception when a symbol was found
01680    in a psymtab but not in a symtab.  */
01681 
01682 static void ATTRIBUTE_NORETURN
01683 error_in_psymtab_expansion (int kind, const char *name, struct symtab *symtab)
01684 {
01685   error (_("\
01686 Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n\
01687 %s may be an inlined function, or may be a template function\n   \
01688 (if a template, try specifying an instantiation: %s<type>)."),
01689          kind == GLOBAL_BLOCK ? "global" : "static",
01690          name, symtab_to_filename_for_display (symtab), name, name);
01691 }
01692 
01693 /* A helper function for lookup_symbol_aux that interfaces with the
01694    "quick" symbol table functions.  */
01695 
01696 static struct symbol *
01697 lookup_symbol_aux_quick (struct objfile *objfile, int kind,
01698                          const char *name, const domain_enum domain)
01699 {
01700   struct symtab *symtab;
01701   struct blockvector *bv;
01702   const struct block *block;
01703   struct symbol *sym;
01704 
01705   if (!objfile->sf)
01706     return NULL;
01707   symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, domain);
01708   if (!symtab)
01709     return NULL;
01710 
01711   bv = BLOCKVECTOR (symtab);
01712   block = BLOCKVECTOR_BLOCK (bv, kind);
01713   sym = lookup_block_symbol (block, name, domain);
01714   if (!sym)
01715     error_in_psymtab_expansion (kind, name, symtab);
01716   return fixup_symbol_section (sym, objfile);
01717 }
01718 
01719 /* A default version of lookup_symbol_nonlocal for use by languages
01720    that can't think of anything better to do.  This implements the C
01721    lookup rules.  */
01722 
01723 struct symbol *
01724 basic_lookup_symbol_nonlocal (const char *name,
01725                               const struct block *block,
01726                               const domain_enum domain)
01727 {
01728   struct symbol *sym;
01729 
01730   /* NOTE: carlton/2003-05-19: The comments below were written when
01731      this (or what turned into this) was part of lookup_symbol_aux;
01732      I'm much less worried about these questions now, since these
01733      decisions have turned out well, but I leave these comments here
01734      for posterity.  */
01735 
01736   /* NOTE: carlton/2002-12-05: There is a question as to whether or
01737      not it would be appropriate to search the current global block
01738      here as well.  (That's what this code used to do before the
01739      is_a_field_of_this check was moved up.)  On the one hand, it's
01740      redundant with the lookup_symbol_aux_symtabs search that happens
01741      next.  On the other hand, if decode_line_1 is passed an argument
01742      like filename:var, then the user presumably wants 'var' to be
01743      searched for in filename.  On the third hand, there shouldn't be
01744      multiple global variables all of which are named 'var', and it's
01745      not like decode_line_1 has ever restricted its search to only
01746      global variables in a single filename.  All in all, only
01747      searching the static block here seems best: it's correct and it's
01748      cleanest.  */
01749 
01750   /* NOTE: carlton/2002-12-05: There's also a possible performance
01751      issue here: if you usually search for global symbols in the
01752      current file, then it would be slightly better to search the
01753      current global block before searching all the symtabs.  But there
01754      are other factors that have a much greater effect on performance
01755      than that one, so I don't think we should worry about that for
01756      now.  */
01757 
01758   sym = lookup_symbol_static (name, block, domain);
01759   if (sym != NULL)
01760     return sym;
01761 
01762   return lookup_symbol_global (name, block, domain);
01763 }
01764 
01765 /* Lookup a symbol in the static block associated to BLOCK, if there
01766    is one; do nothing if BLOCK is NULL or a global block.  */
01767 
01768 struct symbol *
01769 lookup_symbol_static (const char *name,
01770                       const struct block *block,
01771                       const domain_enum domain)
01772 {
01773   const struct block *static_block = block_static_block (block);
01774 
01775   if (static_block != NULL)
01776     return lookup_symbol_aux_block (name, static_block, domain);
01777   else
01778     return NULL;
01779 }
01780 
01781 /* Private data to be used with lookup_symbol_global_iterator_cb.  */
01782 
01783 struct global_sym_lookup_data
01784 {
01785   /* The name of the symbol we are searching for.  */
01786   const char *name;
01787 
01788   /* The domain to use for our search.  */
01789   domain_enum domain;
01790 
01791   /* The field where the callback should store the symbol if found.
01792      It should be initialized to NULL before the search is started.  */
01793   struct symbol *result;
01794 };
01795 
01796 /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
01797    It searches by name for a symbol in the GLOBAL_BLOCK of the given
01798    OBJFILE.  The arguments for the search are passed via CB_DATA,
01799    which in reality is a pointer to struct global_sym_lookup_data.  */
01800 
01801 static int
01802 lookup_symbol_global_iterator_cb (struct objfile *objfile,
01803                                   void *cb_data)
01804 {
01805   struct global_sym_lookup_data *data =
01806     (struct global_sym_lookup_data *) cb_data;
01807 
01808   gdb_assert (data->result == NULL);
01809 
01810   data->result = lookup_symbol_aux_objfile (objfile, GLOBAL_BLOCK,
01811                                             data->name, data->domain);
01812   if (data->result == NULL)
01813     data->result = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK,
01814                                             data->name, data->domain);
01815 
01816   /* If we found a match, tell the iterator to stop.  Otherwise,
01817      keep going.  */
01818   return (data->result != NULL);
01819 }
01820 
01821 /* Lookup a symbol in all files' global blocks (searching psymtabs if
01822    necessary).  */
01823 
01824 struct symbol *
01825 lookup_symbol_global (const char *name,
01826                       const struct block *block,
01827                       const domain_enum domain)
01828 {
01829   struct symbol *sym = NULL;
01830   struct objfile *objfile = NULL;
01831   struct global_sym_lookup_data lookup_data;
01832 
01833   /* Call library-specific lookup procedure.  */
01834   objfile = lookup_objfile_from_block (block);
01835   if (objfile != NULL)
01836     sym = solib_global_lookup (objfile, name, domain);
01837   if (sym != NULL)
01838     return sym;
01839 
01840   memset (&lookup_data, 0, sizeof (lookup_data));
01841   lookup_data.name = name;
01842   lookup_data.domain = domain;
01843   gdbarch_iterate_over_objfiles_in_search_order
01844     (objfile != NULL ? get_objfile_arch (objfile) : target_gdbarch (),
01845      lookup_symbol_global_iterator_cb, &lookup_data, objfile);
01846 
01847   return lookup_data.result;
01848 }
01849 
01850 int
01851 symbol_matches_domain (enum language symbol_language,
01852                        domain_enum symbol_domain,
01853                        domain_enum domain)
01854 {
01855   /* For C++ "struct foo { ... }" also defines a typedef for "foo".
01856      A Java class declaration also defines a typedef for the class.
01857      Similarly, any Ada type declaration implicitly defines a typedef.  */
01858   if (symbol_language == language_cplus
01859       || symbol_language == language_d
01860       || symbol_language == language_java
01861       || symbol_language == language_ada)
01862     {
01863       if ((domain == VAR_DOMAIN || domain == STRUCT_DOMAIN)
01864           && symbol_domain == STRUCT_DOMAIN)
01865         return 1;
01866     }
01867   /* For all other languages, strict match is required.  */
01868   return (symbol_domain == domain);
01869 }
01870 
01871 /* Look up a type named NAME in the struct_domain.  The type returned
01872    must not be opaque -- i.e., must have at least one field
01873    defined.  */
01874 
01875 struct type *
01876 lookup_transparent_type (const char *name)
01877 {
01878   return current_language->la_lookup_transparent_type (name);
01879 }
01880 
01881 /* A helper for basic_lookup_transparent_type that interfaces with the
01882    "quick" symbol table functions.  */
01883 
01884 static struct type *
01885 basic_lookup_transparent_type_quick (struct objfile *objfile, int kind,
01886                                      const char *name)
01887 {
01888   struct symtab *symtab;
01889   struct blockvector *bv;
01890   struct block *block;
01891   struct symbol *sym;
01892 
01893   if (!objfile->sf)
01894     return NULL;
01895   symtab = objfile->sf->qf->lookup_symbol (objfile, kind, name, STRUCT_DOMAIN);
01896   if (!symtab)
01897     return NULL;
01898 
01899   bv = BLOCKVECTOR (symtab);
01900   block = BLOCKVECTOR_BLOCK (bv, kind);
01901   sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
01902   if (!sym)
01903     error_in_psymtab_expansion (kind, name, symtab);
01904 
01905   if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
01906     return SYMBOL_TYPE (sym);
01907 
01908   return NULL;
01909 }
01910 
01911 /* The standard implementation of lookup_transparent_type.  This code
01912    was modeled on lookup_symbol -- the parts not relevant to looking
01913    up types were just left out.  In particular it's assumed here that
01914    types are available in struct_domain and only at file-static or
01915    global blocks.  */
01916 
01917 struct type *
01918 basic_lookup_transparent_type (const char *name)
01919 {
01920   struct symbol *sym;
01921   struct symtab *s = NULL;
01922   struct blockvector *bv;
01923   struct objfile *objfile;
01924   struct block *block;
01925   struct type *t;
01926 
01927   /* Now search all the global symbols.  Do the symtab's first, then
01928      check the psymtab's.  If a psymtab indicates the existence
01929      of the desired name as a global, then do psymtab-to-symtab
01930      conversion on the fly and return the found symbol.  */
01931 
01932   ALL_OBJFILES (objfile)
01933   {
01934     ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
01935       {
01936         bv = BLOCKVECTOR (s);
01937         block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
01938         sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
01939         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
01940           {
01941             return SYMBOL_TYPE (sym);
01942           }
01943       }
01944   }
01945 
01946   ALL_OBJFILES (objfile)
01947   {
01948     t = basic_lookup_transparent_type_quick (objfile, GLOBAL_BLOCK, name);
01949     if (t)
01950       return t;
01951   }
01952 
01953   /* Now search the static file-level symbols.
01954      Not strictly correct, but more useful than an error.
01955      Do the symtab's first, then
01956      check the psymtab's.  If a psymtab indicates the existence
01957      of the desired name as a file-level static, then do psymtab-to-symtab
01958      conversion on the fly and return the found symbol.  */
01959 
01960   ALL_OBJFILES (objfile)
01961   {
01962     ALL_OBJFILE_PRIMARY_SYMTABS (objfile, s)
01963       {
01964         bv = BLOCKVECTOR (s);
01965         block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
01966         sym = lookup_block_symbol (block, name, STRUCT_DOMAIN);
01967         if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
01968           {
01969             return SYMBOL_TYPE (sym);
01970           }
01971       }
01972   }
01973 
01974   ALL_OBJFILES (objfile)
01975   {
01976     t = basic_lookup_transparent_type_quick (objfile, STATIC_BLOCK, name);
01977     if (t)
01978       return t;
01979   }
01980 
01981   return (struct type *) 0;
01982 }
01983 
01984 /* Search BLOCK for symbol NAME in DOMAIN.
01985 
01986    Note that if NAME is the demangled form of a C++ symbol, we will fail
01987    to find a match during the binary search of the non-encoded names, but
01988    for now we don't worry about the slight inefficiency of looking for
01989    a match we'll never find, since it will go pretty quick.  Once the
01990    binary search terminates, we drop through and do a straight linear
01991    search on the symbols.  Each symbol which is marked as being a ObjC/C++
01992    symbol (language_cplus or language_objc set) has both the encoded and
01993    non-encoded names tested for a match.  */
01994 
01995 struct symbol *
01996 lookup_block_symbol (const struct block *block, const char *name,
01997                      const domain_enum domain)
01998 {
01999   struct block_iterator iter;
02000   struct symbol *sym;
02001 
02002   if (!BLOCK_FUNCTION (block))
02003     {
02004       for (sym = block_iter_name_first (block, name, &iter);
02005            sym != NULL;
02006            sym = block_iter_name_next (name, &iter))
02007         {
02008           if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
02009                                      SYMBOL_DOMAIN (sym), domain))
02010             return sym;
02011         }
02012       return NULL;
02013     }
02014   else
02015     {
02016       /* Note that parameter symbols do not always show up last in the
02017          list; this loop makes sure to take anything else other than
02018          parameter symbols first; it only uses parameter symbols as a
02019          last resort.  Note that this only takes up extra computation
02020          time on a match.  */
02021 
02022       struct symbol *sym_found = NULL;
02023 
02024       for (sym = block_iter_name_first (block, name, &iter);
02025            sym != NULL;
02026            sym = block_iter_name_next (name, &iter))
02027         {
02028           if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
02029                                      SYMBOL_DOMAIN (sym), domain))
02030             {
02031               sym_found = sym;
02032               if (!SYMBOL_IS_ARGUMENT (sym))
02033                 {
02034                   break;
02035                 }
02036             }
02037         }
02038       return (sym_found);       /* Will be NULL if not found.  */
02039     }
02040 }
02041 
02042 /* Iterate over the symbols named NAME, matching DOMAIN, in BLOCK.
02043    
02044    For each symbol that matches, CALLBACK is called.  The symbol and
02045    DATA are passed to the callback.
02046    
02047    If CALLBACK returns zero, the iteration ends.  Otherwise, the
02048    search continues.  */
02049 
02050 void
02051 iterate_over_symbols (const struct block *block, const char *name,
02052                       const domain_enum domain,
02053                       symbol_found_callback_ftype *callback,
02054                       void *data)
02055 {
02056   struct block_iterator iter;
02057   struct symbol *sym;
02058 
02059   for (sym = block_iter_name_first (block, name, &iter);
02060        sym != NULL;
02061        sym = block_iter_name_next (name, &iter))
02062     {
02063       if (symbol_matches_domain (SYMBOL_LANGUAGE (sym),
02064                                  SYMBOL_DOMAIN (sym), domain))
02065         {
02066           if (!callback (sym, data))
02067             return;
02068         }
02069     }
02070 }
02071 
02072 /* Find the symtab associated with PC and SECTION.  Look through the
02073    psymtabs and read in another symtab if necessary.  */
02074 
02075 struct symtab *
02076 find_pc_sect_symtab (CORE_ADDR pc, struct obj_section *section)
02077 {
02078   struct block *b;
02079   struct blockvector *bv;
02080   struct symtab *s = NULL;
02081   struct symtab *best_s = NULL;
02082   struct objfile *objfile;
02083   CORE_ADDR distance = 0;
02084   struct minimal_symbol *msymbol;
02085 
02086   /* If we know that this is not a text address, return failure.  This is
02087      necessary because we loop based on the block's high and low code
02088      addresses, which do not include the data ranges, and because
02089      we call find_pc_sect_psymtab which has a similar restriction based
02090      on the partial_symtab's texthigh and textlow.  */
02091   msymbol = lookup_minimal_symbol_by_pc_section (pc, section).minsym;
02092   if (msymbol
02093       && (MSYMBOL_TYPE (msymbol) == mst_data
02094           || MSYMBOL_TYPE (msymbol) == mst_bss
02095           || MSYMBOL_TYPE (msymbol) == mst_abs
02096           || MSYMBOL_TYPE (msymbol) == mst_file_data
02097           || MSYMBOL_TYPE (msymbol) == mst_file_bss))
02098     return NULL;
02099 
02100   /* Search all symtabs for the one whose file contains our address, and which
02101      is the smallest of all the ones containing the address.  This is designed
02102      to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
02103      and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
02104      0x1000-0x4000, but for address 0x2345 we want to return symtab b.
02105 
02106      This happens for native ecoff format, where code from included files
02107      gets its own symtab.  The symtab for the included file should have
02108      been read in already via the dependency mechanism.
02109      It might be swifter to create several symtabs with the same name
02110      like xcoff does (I'm not sure).
02111 
02112      It also happens for objfiles that have their functions reordered.
02113      For these, the symtab we are looking for is not necessarily read in.  */
02114 
02115   ALL_PRIMARY_SYMTABS (objfile, s)
02116   {
02117     bv = BLOCKVECTOR (s);
02118     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
02119 
02120     if (BLOCK_START (b) <= pc
02121         && BLOCK_END (b) > pc
02122         && (distance == 0
02123             || BLOCK_END (b) - BLOCK_START (b) < distance))
02124       {
02125         /* For an objfile that has its functions reordered,
02126            find_pc_psymtab will find the proper partial symbol table
02127            and we simply return its corresponding symtab.  */
02128         /* In order to better support objfiles that contain both
02129            stabs and coff debugging info, we continue on if a psymtab
02130            can't be found.  */
02131         if ((objfile->flags & OBJF_REORDERED) && objfile->sf)
02132           {
02133             struct symtab *result;
02134 
02135             result
02136               = objfile->sf->qf->find_pc_sect_symtab (objfile,
02137                                                       msymbol,
02138                                                       pc, section,
02139                                                       0);
02140             if (result)
02141               return result;
02142           }
02143         if (section != 0)
02144           {
02145             struct block_iterator iter;
02146             struct symbol *sym = NULL;
02147 
02148             ALL_BLOCK_SYMBOLS (b, iter, sym)
02149               {
02150                 fixup_symbol_section (sym, objfile);
02151                 if (matching_obj_sections (SYMBOL_OBJ_SECTION (objfile, sym),
02152                                            section))
02153                   break;
02154               }
02155             if (sym == NULL)
02156               continue;         /* No symbol in this symtab matches
02157                                    section.  */
02158           }
02159         distance = BLOCK_END (b) - BLOCK_START (b);
02160         best_s = s;
02161       }
02162   }
02163 
02164   if (best_s != NULL)
02165     return (best_s);
02166 
02167   /* Not found in symtabs, search the "quick" symtabs (e.g. psymtabs).  */
02168 
02169   ALL_OBJFILES (objfile)
02170   {
02171     struct symtab *result;
02172 
02173     if (!objfile->sf)
02174       continue;
02175     result = objfile->sf->qf->find_pc_sect_symtab (objfile,
02176                                                    msymbol,
02177                                                    pc, section,
02178                                                    1);
02179     if (result)
02180       return result;
02181   }
02182 
02183   return NULL;
02184 }
02185 
02186 /* Find the symtab associated with PC.  Look through the psymtabs and read
02187    in another symtab if necessary.  Backward compatibility, no section.  */
02188 
02189 struct symtab *
02190 find_pc_symtab (CORE_ADDR pc)
02191 {
02192   return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
02193 }
02194 
02195 
02196 /* Find the source file and line number for a given PC value and SECTION.
02197    Return a structure containing a symtab pointer, a line number,
02198    and a pc range for the entire source line.
02199    The value's .pc field is NOT the specified pc.
02200    NOTCURRENT nonzero means, if specified pc is on a line boundary,
02201    use the line that ends there.  Otherwise, in that case, the line
02202    that begins there is used.  */
02203 
02204 /* The big complication here is that a line may start in one file, and end just
02205    before the start of another file.  This usually occurs when you #include
02206    code in the middle of a subroutine.  To properly find the end of a line's PC
02207    range, we must search all symtabs associated with this compilation unit, and
02208    find the one whose first PC is closer than that of the next line in this
02209    symtab.  */
02210 
02211 /* If it's worth the effort, we could be using a binary search.  */
02212 
02213 struct symtab_and_line
02214 find_pc_sect_line (CORE_ADDR pc, struct obj_section *section, int notcurrent)
02215 {
02216   struct symtab *s;
02217   struct linetable *l;
02218   int len;
02219   int i;
02220   struct linetable_entry *item;
02221   struct symtab_and_line val;
02222   struct blockvector *bv;
02223   struct bound_minimal_symbol msymbol;
02224   struct minimal_symbol *mfunsym;
02225   struct objfile *objfile;
02226 
02227   /* Info on best line seen so far, and where it starts, and its file.  */
02228 
02229   struct linetable_entry *best = NULL;
02230   CORE_ADDR best_end = 0;
02231   struct symtab *best_symtab = 0;
02232 
02233   /* Store here the first line number
02234      of a file which contains the line at the smallest pc after PC.
02235      If we don't find a line whose range contains PC,
02236      we will use a line one less than this,
02237      with a range from the start of that file to the first line's pc.  */
02238   struct linetable_entry *alt = NULL;
02239 
02240   /* Info on best line seen in this file.  */
02241 
02242   struct linetable_entry *prev;
02243 
02244   /* If this pc is not from the current frame,
02245      it is the address of the end of a call instruction.
02246      Quite likely that is the start of the following statement.
02247      But what we want is the statement containing the instruction.
02248      Fudge the pc to make sure we get that.  */
02249 
02250   init_sal (&val);              /* initialize to zeroes */
02251 
02252   val.pspace = current_program_space;
02253 
02254   /* It's tempting to assume that, if we can't find debugging info for
02255      any function enclosing PC, that we shouldn't search for line
02256      number info, either.  However, GAS can emit line number info for
02257      assembly files --- very helpful when debugging hand-written
02258      assembly code.  In such a case, we'd have no debug info for the
02259      function, but we would have line info.  */
02260 
02261   if (notcurrent)
02262     pc -= 1;
02263 
02264   /* elz: added this because this function returned the wrong
02265      information if the pc belongs to a stub (import/export)
02266      to call a shlib function.  This stub would be anywhere between
02267      two functions in the target, and the line info was erroneously
02268      taken to be the one of the line before the pc.  */
02269 
02270   /* RT: Further explanation:
02271 
02272    * We have stubs (trampolines) inserted between procedures.
02273    *
02274    * Example: "shr1" exists in a shared library, and a "shr1" stub also
02275    * exists in the main image.
02276    *
02277    * In the minimal symbol table, we have a bunch of symbols
02278    * sorted by start address.  The stubs are marked as "trampoline",
02279    * the others appear as text. E.g.:
02280    *
02281    *  Minimal symbol table for main image
02282    *     main:  code for main (text symbol)
02283    *     shr1: stub  (trampoline symbol)
02284    *     foo:   code for foo (text symbol)
02285    *     ...
02286    *  Minimal symbol table for "shr1" image:
02287    *     ...
02288    *     shr1: code for shr1 (text symbol)
02289    *     ...
02290    *
02291    * So the code below is trying to detect if we are in the stub
02292    * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
02293    * and if found,  do the symbolization from the real-code address
02294    * rather than the stub address.
02295    *
02296    * Assumptions being made about the minimal symbol table:
02297    *   1. lookup_minimal_symbol_by_pc() will return a trampoline only
02298    *      if we're really in the trampoline.s If we're beyond it (say
02299    *      we're in "foo" in the above example), it'll have a closer
02300    *      symbol (the "foo" text symbol for example) and will not
02301    *      return the trampoline.
02302    *   2. lookup_minimal_symbol_text() will find a real text symbol
02303    *      corresponding to the trampoline, and whose address will
02304    *      be different than the trampoline address.  I put in a sanity
02305    *      check for the address being the same, to avoid an
02306    *      infinite recursion.
02307    */
02308   msymbol = lookup_minimal_symbol_by_pc (pc);
02309   if (msymbol.minsym != NULL)
02310     if (MSYMBOL_TYPE (msymbol.minsym) == mst_solib_trampoline)
02311       {
02312         mfunsym
02313           = lookup_minimal_symbol_text (SYMBOL_LINKAGE_NAME (msymbol.minsym),
02314                                         NULL);
02315         if (mfunsym == NULL)
02316           /* I eliminated this warning since it is coming out
02317            * in the following situation:
02318            * gdb shmain // test program with shared libraries
02319            * (gdb) break shr1  // function in shared lib
02320            * Warning: In stub for ...
02321            * In the above situation, the shared lib is not loaded yet,
02322            * so of course we can't find the real func/line info,
02323            * but the "break" still works, and the warning is annoying.
02324            * So I commented out the warning.  RT */
02325           /* warning ("In stub for %s; unable to find real function/line info",
02326              SYMBOL_LINKAGE_NAME (msymbol)); */
02327           ;
02328         /* fall through */
02329         else if (SYMBOL_VALUE_ADDRESS (mfunsym)
02330                  == SYMBOL_VALUE_ADDRESS (msymbol.minsym))
02331           /* Avoid infinite recursion */
02332           /* See above comment about why warning is commented out.  */
02333           /* warning ("In stub for %s; unable to find real function/line info",
02334              SYMBOL_LINKAGE_NAME (msymbol)); */
02335           ;
02336         /* fall through */
02337         else
02338           return find_pc_line (SYMBOL_VALUE_ADDRESS (mfunsym), 0);
02339       }
02340 
02341 
02342   s = find_pc_sect_symtab (pc, section);
02343   if (!s)
02344     {
02345       /* If no symbol information, return previous pc.  */
02346       if (notcurrent)
02347         pc++;
02348       val.pc = pc;
02349       return val;
02350     }
02351 
02352   bv = BLOCKVECTOR (s);
02353   objfile = s->objfile;
02354 
02355   /* Look at all the symtabs that share this blockvector.
02356      They all have the same apriori range, that we found was right;
02357      but they have different line tables.  */
02358 
02359   ALL_OBJFILE_SYMTABS (objfile, s)
02360     {
02361       if (BLOCKVECTOR (s) != bv)
02362         continue;
02363 
02364       /* Find the best line in this symtab.  */
02365       l = LINETABLE (s);
02366       if (!l)
02367         continue;
02368       len = l->nitems;
02369       if (len <= 0)
02370         {
02371           /* I think len can be zero if the symtab lacks line numbers
02372              (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
02373              I'm not sure which, and maybe it depends on the symbol
02374              reader).  */
02375           continue;
02376         }
02377 
02378       prev = NULL;
02379       item = l->item;           /* Get first line info.  */
02380 
02381       /* Is this file's first line closer than the first lines of other files?
02382          If so, record this file, and its first line, as best alternate.  */
02383       if (item->pc > pc && (!alt || item->pc < alt->pc))
02384         alt = item;
02385 
02386       for (i = 0; i < len; i++, item++)
02387         {
02388           /* Leave prev pointing to the linetable entry for the last line
02389              that started at or before PC.  */
02390           if (item->pc > pc)
02391             break;
02392 
02393           prev = item;
02394         }
02395 
02396       /* At this point, prev points at the line whose start addr is <= pc, and
02397          item points at the next line.  If we ran off the end of the linetable
02398          (pc >= start of the last line), then prev == item.  If pc < start of
02399          the first line, prev will not be set.  */
02400 
02401       /* Is this file's best line closer than the best in the other files?
02402          If so, record this file, and its best line, as best so far.  Don't
02403          save prev if it represents the end of a function (i.e. line number
02404          0) instead of a real line.  */
02405 
02406       if (prev && prev->line && (!best || prev->pc > best->pc))
02407         {
02408           best = prev;
02409           best_symtab = s;
02410 
02411           /* Discard BEST_END if it's before the PC of the current BEST.  */
02412           if (best_end <= best->pc)
02413             best_end = 0;
02414         }
02415 
02416       /* If another line (denoted by ITEM) is in the linetable and its
02417          PC is after BEST's PC, but before the current BEST_END, then
02418          use ITEM's PC as the new best_end.  */
02419       if (best && i < len && item->pc > best->pc
02420           && (best_end == 0 || best_end > item->pc))
02421         best_end = item->pc;
02422     }
02423 
02424   if (!best_symtab)
02425     {
02426       /* If we didn't find any line number info, just return zeros.
02427          We used to return alt->line - 1 here, but that could be
02428          anywhere; if we don't have line number info for this PC,
02429          don't make some up.  */
02430       val.pc = pc;
02431     }
02432   else if (best->line == 0)
02433     {
02434       /* If our best fit is in a range of PC's for which no line
02435          number info is available (line number is zero) then we didn't
02436          find any valid line information.  */
02437       val.pc = pc;
02438     }
02439   else
02440     {
02441       val.symtab = best_symtab;
02442       val.line = best->line;
02443       val.pc = best->pc;
02444       if (best_end && (!alt || best_end < alt->pc))
02445         val.end = best_end;
02446       else if (alt)
02447         val.end = alt->pc;
02448       else
02449         val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
02450     }
02451   val.section = section;
02452   return val;
02453 }
02454 
02455 /* Backward compatibility (no section).  */
02456 
02457 struct symtab_and_line
02458 find_pc_line (CORE_ADDR pc, int notcurrent)
02459 {
02460   struct obj_section *section;
02461 
02462   section = find_pc_overlay (pc);
02463   if (pc_in_unmapped_range (pc, section))
02464     pc = overlay_mapped_address (pc, section);
02465   return find_pc_sect_line (pc, section, notcurrent);
02466 }
02467 
02468 /* Find line number LINE in any symtab whose name is the same as
02469    SYMTAB.
02470 
02471    If found, return the symtab that contains the linetable in which it was
02472    found, set *INDEX to the index in the linetable of the best entry
02473    found, and set *EXACT_MATCH nonzero if the value returned is an
02474    exact match.
02475 
02476    If not found, return NULL.  */
02477 
02478 struct symtab *
02479 find_line_symtab (struct symtab *symtab, int line,
02480                   int *index, int *exact_match)
02481 {
02482   int exact = 0;  /* Initialized here to avoid a compiler warning.  */
02483 
02484   /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
02485      so far seen.  */
02486 
02487   int best_index;
02488   struct linetable *best_linetable;
02489   struct symtab *best_symtab;
02490 
02491   /* First try looking it up in the given symtab.  */
02492   best_linetable = LINETABLE (symtab);
02493   best_symtab = symtab;
02494   best_index = find_line_common (best_linetable, line, &exact, 0);
02495   if (best_index < 0 || !exact)
02496     {
02497       /* Didn't find an exact match.  So we better keep looking for
02498          another symtab with the same name.  In the case of xcoff,
02499          multiple csects for one source file (produced by IBM's FORTRAN
02500          compiler) produce multiple symtabs (this is unavoidable
02501          assuming csects can be at arbitrary places in memory and that
02502          the GLOBAL_BLOCK of a symtab has a begin and end address).  */
02503 
02504       /* BEST is the smallest linenumber > LINE so far seen,
02505          or 0 if none has been seen so far.
02506          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
02507       int best;
02508 
02509       struct objfile *objfile;
02510       struct symtab *s;
02511 
02512       if (best_index >= 0)
02513         best = best_linetable->item[best_index].line;
02514       else
02515         best = 0;
02516 
02517       ALL_OBJFILES (objfile)
02518       {
02519         if (objfile->sf)
02520           objfile->sf->qf->expand_symtabs_with_fullname (objfile,
02521                                                    symtab_to_fullname (symtab));
02522       }
02523 
02524       ALL_SYMTABS (objfile, s)
02525       {
02526         struct linetable *l;
02527         int ind;
02528 
02529         if (FILENAME_CMP (symtab->filename, s->filename) != 0)
02530           continue;
02531         if (FILENAME_CMP (symtab_to_fullname (symtab),
02532                           symtab_to_fullname (s)) != 0)
02533           continue;     
02534         l = LINETABLE (s);
02535         ind = find_line_common (l, line, &exact, 0);
02536         if (ind >= 0)
02537           {
02538             if (exact)
02539               {
02540                 best_index = ind;
02541                 best_linetable = l;
02542                 best_symtab = s;
02543                 goto done;
02544               }
02545             if (best == 0 || l->item[ind].line < best)
02546               {
02547                 best = l->item[ind].line;
02548                 best_index = ind;
02549                 best_linetable = l;
02550                 best_symtab = s;
02551               }
02552           }
02553       }
02554     }
02555 done:
02556   if (best_index < 0)
02557     return NULL;
02558 
02559   if (index)
02560     *index = best_index;
02561   if (exact_match)
02562     *exact_match = exact;
02563 
02564   return best_symtab;
02565 }
02566 
02567 /* Given SYMTAB, returns all the PCs function in the symtab that
02568    exactly match LINE.  Returns NULL if there are no exact matches,
02569    but updates BEST_ITEM in this case.  */
02570 
02571 VEC (CORE_ADDR) *
02572 find_pcs_for_symtab_line (struct symtab *symtab, int line,
02573                           struct linetable_entry **best_item)
02574 {
02575   int start = 0;
02576   VEC (CORE_ADDR) *result = NULL;
02577 
02578   /* First, collect all the PCs that are at this line.  */
02579   while (1)
02580     {
02581       int was_exact;
02582       int idx;
02583 
02584       idx = find_line_common (LINETABLE (symtab), line, &was_exact, start);
02585       if (idx < 0)
02586         break;
02587 
02588       if (!was_exact)
02589         {
02590           struct linetable_entry *item = &LINETABLE (symtab)->item[idx];
02591 
02592           if (*best_item == NULL || item->line < (*best_item)->line)
02593             *best_item = item;
02594 
02595           break;
02596         }
02597 
02598       VEC_safe_push (CORE_ADDR, result, LINETABLE (symtab)->item[idx].pc);
02599       start = idx + 1;
02600     }
02601 
02602   return result;
02603 }
02604 
02605 
02606 /* Set the PC value for a given source file and line number and return true.
02607    Returns zero for invalid line number (and sets the PC to 0).
02608    The source file is specified with a struct symtab.  */
02609 
02610 int
02611 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
02612 {
02613   struct linetable *l;
02614   int ind;
02615 
02616   *pc = 0;
02617   if (symtab == 0)
02618     return 0;
02619 
02620   symtab = find_line_symtab (symtab, line, &ind, NULL);
02621   if (symtab != NULL)
02622     {
02623       l = LINETABLE (symtab);
02624       *pc = l->item[ind].pc;
02625       return 1;
02626     }
02627   else
02628     return 0;
02629 }
02630 
02631 /* Find the range of pc values in a line.
02632    Store the starting pc of the line into *STARTPTR
02633    and the ending pc (start of next line) into *ENDPTR.
02634    Returns 1 to indicate success.
02635    Returns 0 if could not find the specified line.  */
02636 
02637 int
02638 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
02639                     CORE_ADDR *endptr)
02640 {
02641   CORE_ADDR startaddr;
02642   struct symtab_and_line found_sal;
02643 
02644   startaddr = sal.pc;
02645   if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
02646     return 0;
02647 
02648   /* This whole function is based on address.  For example, if line 10 has
02649      two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
02650      "info line *0x123" should say the line goes from 0x100 to 0x200
02651      and "info line *0x355" should say the line goes from 0x300 to 0x400.
02652      This also insures that we never give a range like "starts at 0x134
02653      and ends at 0x12c".  */
02654 
02655   found_sal = find_pc_sect_line (startaddr, sal.section, 0);
02656   if (found_sal.line != sal.line)
02657     {
02658       /* The specified line (sal) has zero bytes.  */
02659       *startptr = found_sal.pc;
02660       *endptr = found_sal.pc;
02661     }
02662   else
02663     {
02664       *startptr = found_sal.pc;
02665       *endptr = found_sal.end;
02666     }
02667   return 1;
02668 }
02669 
02670 /* Given a line table and a line number, return the index into the line
02671    table for the pc of the nearest line whose number is >= the specified one.
02672    Return -1 if none is found.  The value is >= 0 if it is an index.
02673    START is the index at which to start searching the line table.
02674 
02675    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
02676 
02677 static int
02678 find_line_common (struct linetable *l, int lineno,
02679                   int *exact_match, int start)
02680 {
02681   int i;
02682   int len;
02683 
02684   /* BEST is the smallest linenumber > LINENO so far seen,
02685      or 0 if none has been seen so far.
02686      BEST_INDEX identifies the item for it.  */
02687 
02688   int best_index = -1;
02689   int best = 0;
02690 
02691   *exact_match = 0;
02692 
02693   if (lineno <= 0)
02694     return -1;
02695   if (l == 0)
02696     return -1;
02697 
02698   len = l->nitems;
02699   for (i = start; i < len; i++)
02700     {
02701       struct linetable_entry *item = &(l->item[i]);
02702 
02703       if (item->line == lineno)
02704         {
02705           /* Return the first (lowest address) entry which matches.  */
02706           *exact_match = 1;
02707           return i;
02708         }
02709 
02710       if (item->line > lineno && (best == 0 || item->line < best))
02711         {
02712           best = item->line;
02713           best_index = i;
02714         }
02715     }
02716 
02717   /* If we got here, we didn't get an exact match.  */
02718   return best_index;
02719 }
02720 
02721 int
02722 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
02723 {
02724   struct symtab_and_line sal;
02725 
02726   sal = find_pc_line (pc, 0);
02727   *startptr = sal.pc;
02728   *endptr = sal.end;
02729   return sal.symtab != 0;
02730 }
02731 
02732 /* Given a function start address FUNC_ADDR and SYMTAB, find the first
02733    address for that function that has an entry in SYMTAB's line info
02734    table.  If such an entry cannot be found, return FUNC_ADDR
02735    unaltered.  */
02736 
02737 static CORE_ADDR
02738 skip_prologue_using_lineinfo (CORE_ADDR func_addr, struct symtab *symtab)
02739 {
02740   CORE_ADDR func_start, func_end;
02741   struct linetable *l;
02742   int i;
02743 
02744   /* Give up if this symbol has no lineinfo table.  */
02745   l = LINETABLE (symtab);
02746   if (l == NULL)
02747     return func_addr;
02748 
02749   /* Get the range for the function's PC values, or give up if we
02750      cannot, for some reason.  */
02751   if (!find_pc_partial_function (func_addr, NULL, &func_start, &func_end))
02752     return func_addr;
02753 
02754   /* Linetable entries are ordered by PC values, see the commentary in
02755      symtab.h where `struct linetable' is defined.  Thus, the first
02756      entry whose PC is in the range [FUNC_START..FUNC_END[ is the
02757      address we are looking for.  */
02758   for (i = 0; i < l->nitems; i++)
02759     {
02760       struct linetable_entry *item = &(l->item[i]);
02761 
02762       /* Don't use line numbers of zero, they mark special entries in
02763          the table.  See the commentary on symtab.h before the
02764          definition of struct linetable.  */
02765       if (item->line > 0 && func_start <= item->pc && item->pc < func_end)
02766         return item->pc;
02767     }
02768 
02769   return func_addr;
02770 }
02771 
02772 /* Given a function symbol SYM, find the symtab and line for the start
02773    of the function.
02774    If the argument FUNFIRSTLINE is nonzero, we want the first line
02775    of real code inside the function.  */
02776 
02777 struct symtab_and_line
02778 find_function_start_sal (struct symbol *sym, int funfirstline)
02779 {
02780   struct symtab_and_line sal;
02781 
02782   fixup_symbol_section (sym, NULL);
02783   sal = find_pc_sect_line (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)),
02784                            SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (sym), sym), 0);
02785 
02786   /* We always should have a line for the function start address.
02787      If we don't, something is odd.  Create a plain SAL refering
02788      just the PC and hope that skip_prologue_sal (if requested)
02789      can find a line number for after the prologue.  */
02790   if (sal.pc < BLOCK_START (SYMBOL_BLOCK_VALUE (sym)))
02791     {
02792       init_sal (&sal);
02793       sal.pspace = current_program_space;
02794       sal.pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
02795       sal.section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (sym), sym);
02796     }
02797 
02798   if (funfirstline)
02799     skip_prologue_sal (&sal);
02800 
02801   return sal;
02802 }
02803 
02804 /* Adjust SAL to the first instruction past the function prologue.
02805    If the PC was explicitly specified, the SAL is not changed.
02806    If the line number was explicitly specified, at most the SAL's PC
02807    is updated.  If SAL is already past the prologue, then do nothing.  */
02808 
02809 void
02810 skip_prologue_sal (struct symtab_and_line *sal)
02811 {
02812   struct symbol *sym;
02813   struct symtab_and_line start_sal;
02814   struct cleanup *old_chain;
02815   CORE_ADDR pc, saved_pc;
02816   struct obj_section *section;
02817   const char *name;
02818   struct objfile *objfile;
02819   struct gdbarch *gdbarch;
02820   struct block *b, *function_block;
02821   int force_skip, skip;
02822 
02823   /* Do not change the SAL if PC was specified explicitly.  */
02824   if (sal->explicit_pc)
02825     return;
02826 
02827   old_chain = save_current_space_and_thread ();
02828   switch_to_program_space_and_thread (sal->pspace);
02829 
02830   sym = find_pc_sect_function (sal->pc, sal->section);
02831   if (sym != NULL)
02832     {
02833       fixup_symbol_section (sym, NULL);
02834 
02835       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
02836       section = SYMBOL_OBJ_SECTION (SYMBOL_OBJFILE (sym), sym);
02837       name = SYMBOL_LINKAGE_NAME (sym);
02838       objfile = SYMBOL_SYMTAB (sym)->objfile;
02839     }
02840   else
02841     {
02842       struct bound_minimal_symbol msymbol
02843         = lookup_minimal_symbol_by_pc_section (sal->pc, sal->section);
02844 
02845       if (msymbol.minsym == NULL)
02846         {
02847           do_cleanups (old_chain);
02848           return;
02849         }
02850 
02851       objfile = msymbol.objfile;
02852       pc = SYMBOL_VALUE_ADDRESS (msymbol.minsym);
02853       section = SYMBOL_OBJ_SECTION (objfile, msymbol.minsym);
02854       name = SYMBOL_LINKAGE_NAME (msymbol.minsym);
02855     }
02856 
02857   gdbarch = get_objfile_arch (objfile);
02858 
02859   /* Process the prologue in two passes.  In the first pass try to skip the
02860      prologue (SKIP is true) and verify there is a real need for it (indicated
02861      by FORCE_SKIP).  If no such reason was found run a second pass where the
02862      prologue is not skipped (SKIP is false).  */
02863 
02864   skip = 1;
02865   force_skip = 1;
02866 
02867   /* Be conservative - allow direct PC (without skipping prologue) only if we
02868      have proven the CU (Compilation Unit) supports it.  sal->SYMTAB does not
02869      have to be set by the caller so we use SYM instead.  */
02870   if (sym && SYMBOL_SYMTAB (sym)->locations_valid)
02871     force_skip = 0;
02872 
02873   saved_pc = pc;
02874   do
02875     {
02876       pc = saved_pc;
02877 
02878       /* If the function is in an unmapped overlay, use its unmapped LMA address,
02879          so that gdbarch_skip_prologue has something unique to work on.  */
02880       if (section_is_overlay (section) && !section_is_mapped (section))
02881         pc = overlay_unmapped_address (pc, section);
02882 
02883       /* Skip "first line" of function (which is actually its prologue).  */
02884       pc += gdbarch_deprecated_function_start_offset (gdbarch);
02885       if (skip)
02886         pc = gdbarch_skip_prologue (gdbarch, pc);
02887 
02888       /* For overlays, map pc back into its mapped VMA range.  */
02889       pc = overlay_mapped_address (pc, section);
02890 
02891       /* Calculate line number.  */
02892       start_sal = find_pc_sect_line (pc, section, 0);
02893 
02894       /* Check if gdbarch_skip_prologue left us in mid-line, and the next
02895          line is still part of the same function.  */
02896       if (skip && start_sal.pc != pc
02897           && (sym ? (BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= start_sal.end
02898                      && start_sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
02899               : (lookup_minimal_symbol_by_pc_section (start_sal.end, section).minsym
02900                  == lookup_minimal_symbol_by_pc_section (pc, section).minsym)))
02901         {
02902           /* First pc of next line */
02903           pc = start_sal.end;
02904           /* Recalculate the line number (might not be N+1).  */
02905           start_sal = find_pc_sect_line (pc, section, 0);
02906         }
02907 
02908       /* On targets with executable formats that don't have a concept of
02909          constructors (ELF with .init has, PE doesn't), gcc emits a call
02910          to `__main' in `main' between the prologue and before user
02911          code.  */
02912       if (gdbarch_skip_main_prologue_p (gdbarch)
02913           && name && strcmp_iw (name, "main") == 0)
02914         {
02915           pc = gdbarch_skip_main_prologue (gdbarch, pc);
02916           /* Recalculate the line number (might not be N+1).  */
02917           start_sal = find_pc_sect_line (pc, section, 0);
02918           force_skip = 1;
02919         }
02920     }
02921   while (!force_skip && skip--);
02922 
02923   /* If we still don't have a valid source line, try to find the first
02924      PC in the lineinfo table that belongs to the same function.  This
02925      happens with COFF debug info, which does not seem to have an
02926      entry in lineinfo table for the code after the prologue which has
02927      no direct relation to source.  For example, this was found to be
02928      the case with the DJGPP target using "gcc -gcoff" when the
02929      compiler inserted code after the prologue to make sure the stack
02930      is aligned.  */
02931   if (!force_skip && sym && start_sal.symtab == NULL)
02932     {
02933       pc = skip_prologue_using_lineinfo (pc, SYMBOL_SYMTAB (sym));
02934       /* Recalculate the line number.  */
02935       start_sal = find_pc_sect_line (pc, section, 0);
02936     }
02937 
02938   do_cleanups (old_chain);
02939 
02940   /* If we're already past the prologue, leave SAL unchanged.  Otherwise
02941      forward SAL to the end of the prologue.  */
02942   if (sal->pc >= pc)
02943     return;
02944 
02945   sal->pc = pc;
02946   sal->section = section;
02947 
02948   /* Unless the explicit_line flag was set, update the SAL line
02949      and symtab to correspond to the modified PC location.  */
02950   if (sal->explicit_line)
02951     return;
02952 
02953   sal->symtab = start_sal.symtab;
02954   sal->line = start_sal.line;
02955   sal->end = start_sal.end;
02956 
02957   /* Check if we are now inside an inlined function.  If we can,
02958      use the call site of the function instead.  */
02959   b = block_for_pc_sect (sal->pc, sal->section);
02960   function_block = NULL;
02961   while (b != NULL)
02962     {
02963       if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
02964         function_block = b;
02965       else if (BLOCK_FUNCTION (b) != NULL)
02966         break;
02967       b = BLOCK_SUPERBLOCK (b);
02968     }
02969   if (function_block != NULL
02970       && SYMBOL_LINE (BLOCK_FUNCTION (function_block)) != 0)
02971     {
02972       sal->line = SYMBOL_LINE (BLOCK_FUNCTION (function_block));
02973       sal->symtab = SYMBOL_SYMTAB (BLOCK_FUNCTION (function_block));
02974     }
02975 }
02976 
02977 /* If P is of the form "operator[ \t]+..." where `...' is
02978    some legitimate operator text, return a pointer to the
02979    beginning of the substring of the operator text.
02980    Otherwise, return "".  */
02981 
02982 static char *
02983 operator_chars (char *p, char **end)
02984 {
02985   *end = "";
02986   if (strncmp (p, "operator", 8))
02987     return *end;
02988   p += 8;
02989 
02990   /* Don't get faked out by `operator' being part of a longer
02991      identifier.  */
02992   if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
02993     return *end;
02994 
02995   /* Allow some whitespace between `operator' and the operator symbol.  */
02996   while (*p == ' ' || *p == '\t')
02997     p++;
02998 
02999   /* Recognize 'operator TYPENAME'.  */
03000 
03001   if (isalpha (*p) || *p == '_' || *p == '$')
03002     {
03003       char *q = p + 1;
03004 
03005       while (isalnum (*q) || *q == '_' || *q == '$')
03006         q++;
03007       *end = q;
03008       return p;
03009     }
03010 
03011   while (*p)
03012     switch (*p)
03013       {
03014       case '\\':                        /* regexp quoting */
03015         if (p[1] == '*')
03016           {
03017             if (p[2] == '=')            /* 'operator\*=' */
03018               *end = p + 3;
03019             else                        /* 'operator\*'  */
03020               *end = p + 2;
03021             return p;
03022           }
03023         else if (p[1] == '[')
03024           {
03025             if (p[2] == ']')
03026               error (_("mismatched quoting on brackets, "
03027                        "try 'operator\\[\\]'"));
03028             else if (p[2] == '\\' && p[3] == ']')
03029               {
03030                 *end = p + 4;   /* 'operator\[\]' */
03031                 return p;
03032               }
03033             else
03034               error (_("nothing is allowed between '[' and ']'"));
03035           }
03036         else
03037           {
03038             /* Gratuitous qoute: skip it and move on.  */
03039             p++;
03040             continue;
03041           }
03042         break;
03043       case '!':
03044       case '=':
03045       case '*':
03046       case '/':
03047       case '%':
03048       case '^':
03049         if (p[1] == '=')
03050           *end = p + 2;
03051         else
03052           *end = p + 1;
03053         return p;
03054       case '<':
03055       case '>':
03056       case '+':
03057       case '-':
03058       case '&':
03059       case '|':
03060         if (p[0] == '-' && p[1] == '>')
03061           {
03062             /* Struct pointer member operator 'operator->'.  */
03063             if (p[2] == '*')
03064               {
03065                 *end = p + 3;   /* 'operator->*' */
03066                 return p;
03067               }
03068             else if (p[2] == '\\')
03069               {
03070                 *end = p + 4;   /* Hopefully 'operator->\*' */
03071                 return p;
03072               }
03073             else
03074               {
03075                 *end = p + 2;   /* 'operator->' */
03076                 return p;
03077               }
03078           }
03079         if (p[1] == '=' || p[1] == p[0])
03080           *end = p + 2;
03081         else
03082           *end = p + 1;
03083         return p;
03084       case '~':
03085       case ',':
03086         *end = p + 1;
03087         return p;
03088       case '(':
03089         if (p[1] != ')')
03090           error (_("`operator ()' must be specified "
03091                    "without whitespace in `()'"));
03092         *end = p + 2;
03093         return p;
03094       case '?':
03095         if (p[1] != ':')
03096           error (_("`operator ?:' must be specified "
03097                    "without whitespace in `?:'"));
03098         *end = p + 2;
03099         return p;
03100       case '[':
03101         if (p[1] != ']')
03102           error (_("`operator []' must be specified "
03103                    "without whitespace in `[]'"));
03104         *end = p + 2;
03105         return p;
03106       default:
03107         error (_("`operator %s' not supported"), p);
03108         break;
03109       }
03110 
03111   *end = "";
03112   return *end;
03113 }
03114 
03115 
03116 /* Cache to watch for file names already seen by filename_seen.  */
03117 
03118 struct filename_seen_cache
03119 {
03120   /* Table of files seen so far.  */
03121   htab_t tab;
03122   /* Initial size of the table.  It automagically grows from here.  */
03123 #define INITIAL_FILENAME_SEEN_CACHE_SIZE 100
03124 };
03125 
03126 /* filename_seen_cache constructor.  */
03127 
03128 static struct filename_seen_cache *
03129 create_filename_seen_cache (void)
03130 {
03131   struct filename_seen_cache *cache;
03132 
03133   cache = XNEW (struct filename_seen_cache);
03134   cache->tab = htab_create_alloc (INITIAL_FILENAME_SEEN_CACHE_SIZE,
03135                                   filename_hash, filename_eq,
03136                                   NULL, xcalloc, xfree);
03137 
03138   return cache;
03139 }
03140 
03141 /* Empty the cache, but do not delete it.  */
03142 
03143 static void
03144 clear_filename_seen_cache (struct filename_seen_cache *cache)
03145 {
03146   htab_empty (cache->tab);
03147 }
03148 
03149 /* filename_seen_cache destructor.
03150    This takes a void * argument as it is generally used as a cleanup.  */
03151 
03152 static void
03153 delete_filename_seen_cache (void *ptr)
03154 {
03155   struct filename_seen_cache *cache = ptr;
03156 
03157   htab_delete (cache->tab);
03158   xfree (cache);
03159 }
03160 
03161 /* If FILE is not already in the table of files in CACHE, return zero;
03162    otherwise return non-zero.  Optionally add FILE to the table if ADD
03163    is non-zero.
03164 
03165    NOTE: We don't manage space for FILE, we assume FILE lives as long
03166    as the caller needs.  */
03167 
03168 static int
03169 filename_seen (struct filename_seen_cache *cache, const char *file, int add)
03170 {
03171   void **slot;
03172 
03173   /* Is FILE in tab?  */
03174   slot = htab_find_slot (cache->tab, file, add ? INSERT : NO_INSERT);
03175   if (*slot != NULL)
03176     return 1;
03177 
03178   /* No; maybe add it to tab.  */
03179   if (add)
03180     *slot = (char *) file;
03181 
03182   return 0;
03183 }
03184 
03185 /* Data structure to maintain printing state for output_source_filename.  */
03186 
03187 struct output_source_filename_data
03188 {
03189   /* Cache of what we've seen so far.  */
03190   struct filename_seen_cache *filename_seen_cache;
03191 
03192   /* Flag of whether we're printing the first one.  */
03193   int first;
03194 };
03195 
03196 /* Slave routine for sources_info.  Force line breaks at ,'s.
03197    NAME is the name to print.
03198    DATA contains the state for printing and watching for duplicates.  */
03199 
03200 static void
03201 output_source_filename (const char *name,
03202                         struct output_source_filename_data *data)
03203 {
03204   /* Since a single source file can result in several partial symbol
03205      tables, we need to avoid printing it more than once.  Note: if
03206      some of the psymtabs are read in and some are not, it gets
03207      printed both under "Source files for which symbols have been
03208      read" and "Source files for which symbols will be read in on
03209      demand".  I consider this a reasonable way to deal with the
03210      situation.  I'm not sure whether this can also happen for
03211      symtabs; it doesn't hurt to check.  */
03212 
03213   /* Was NAME already seen?  */
03214   if (filename_seen (data->filename_seen_cache, name, 1))
03215     {
03216       /* Yes; don't print it again.  */
03217       return;
03218     }
03219 
03220   /* No; print it and reset *FIRST.  */
03221   if (! data->first)
03222     printf_filtered (", ");
03223   data->first = 0;
03224 
03225   wrap_here ("");
03226   fputs_filtered (name, gdb_stdout);
03227 }
03228 
03229 /* A callback for map_partial_symbol_filenames.  */
03230 
03231 static void
03232 output_partial_symbol_filename (const char *filename, const char *fullname,
03233                                 void *data)
03234 {
03235   output_source_filename (fullname ? fullname : filename, data);
03236 }
03237 
03238 static void
03239 sources_info (char *ignore, int from_tty)
03240 {
03241   struct symtab *s;
03242   struct objfile *objfile;
03243   struct output_source_filename_data data;
03244   struct cleanup *cleanups;
03245 
03246   if (!have_full_symbols () && !have_partial_symbols ())
03247     {
03248       error (_("No symbol table is loaded.  Use the \"file\" command."));
03249     }
03250 
03251   data.filename_seen_cache = create_filename_seen_cache ();
03252   cleanups = make_cleanup (delete_filename_seen_cache,
03253                            data.filename_seen_cache);
03254 
03255   printf_filtered ("Source files for which symbols have been read in:\n\n");
03256 
03257   data.first = 1;
03258   ALL_SYMTABS (objfile, s)
03259   {
03260     const char *fullname = symtab_to_fullname (s);
03261 
03262     output_source_filename (fullname, &data);
03263   }
03264   printf_filtered ("\n\n");
03265 
03266   printf_filtered ("Source files for which symbols "
03267                    "will be read in on demand:\n\n");
03268 
03269   clear_filename_seen_cache (data.filename_seen_cache);
03270   data.first = 1;
03271   map_partial_symbol_filenames (output_partial_symbol_filename, &data,
03272                                 1 /*need_fullname*/);
03273   printf_filtered ("\n");
03274 
03275   do_cleanups (cleanups);
03276 }
03277 
03278 /* Compare FILE against all the NFILES entries of FILES.  If BASENAMES is
03279    non-zero compare only lbasename of FILES.  */
03280 
03281 static int
03282 file_matches (const char *file, char *files[], int nfiles, int basenames)
03283 {
03284   int i;
03285 
03286   if (file != NULL && nfiles != 0)
03287     {
03288       for (i = 0; i < nfiles; i++)
03289         {
03290           if (compare_filenames_for_search (file, (basenames
03291                                                    ? lbasename (files[i])
03292                                                    : files[i])))
03293             return 1;
03294         }
03295     }
03296   else if (nfiles == 0)
03297     return 1;
03298   return 0;
03299 }
03300 
03301 /* Free any memory associated with a search.  */
03302 
03303 void
03304 free_search_symbols (struct symbol_search *symbols)
03305 {
03306   struct symbol_search *p;
03307   struct symbol_search *next;
03308 
03309   for (p = symbols; p != NULL; p = next)
03310     {
03311       next = p->next;
03312       xfree (p);
03313     }
03314 }
03315 
03316 static void
03317 do_free_search_symbols_cleanup (void *symbolsp)
03318 {
03319   struct symbol_search *symbols = *(struct symbol_search **) symbolsp;
03320 
03321   free_search_symbols (symbols);
03322 }
03323 
03324 struct cleanup *
03325 make_cleanup_free_search_symbols (struct symbol_search **symbolsp)
03326 {
03327   return make_cleanup (do_free_search_symbols_cleanup, symbolsp);
03328 }
03329 
03330 /* Helper function for sort_search_symbols_remove_dups and qsort.  Can only
03331    sort symbols, not minimal symbols.  */
03332 
03333 static int
03334 compare_search_syms (const void *sa, const void *sb)
03335 {
03336   struct symbol_search *sym_a = *(struct symbol_search **) sa;
03337   struct symbol_search *sym_b = *(struct symbol_search **) sb;
03338   int c;
03339 
03340   c = FILENAME_CMP (sym_a->symtab->filename, sym_b->symtab->filename);
03341   if (c != 0)
03342     return c;
03343 
03344   if (sym_a->block != sym_b->block)
03345     return sym_a->block - sym_b->block;
03346 
03347   return strcmp (SYMBOL_PRINT_NAME (sym_a->symbol),
03348                  SYMBOL_PRINT_NAME (sym_b->symbol));
03349 }
03350 
03351 /* Sort the NFOUND symbols in list FOUND and remove duplicates.
03352    The duplicates are freed, and the new list is returned in
03353    *NEW_HEAD, *NEW_TAIL.  */
03354 
03355 static void
03356 sort_search_symbols_remove_dups (struct symbol_search *found, int nfound,
03357                                  struct symbol_search **new_head,
03358                                  struct symbol_search **new_tail)
03359 {
03360   struct symbol_search **symbols, *symp, *old_next;
03361   int i, j, nunique;
03362 
03363   gdb_assert (found != NULL && nfound > 0);
03364 
03365   /* Build an array out of the list so we can easily sort them.  */
03366   symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
03367                                                * nfound);
03368   symp = found;
03369   for (i = 0; i < nfound; i++)
03370     {
03371       gdb_assert (symp != NULL);
03372       gdb_assert (symp->block >= 0 && symp->block <= 1);
03373       symbols[i] = symp;
03374       symp = symp->next;
03375     }
03376   gdb_assert (symp == NULL);
03377 
03378   qsort (symbols, nfound, sizeof (struct symbol_search *),
03379          compare_search_syms);
03380 
03381   /* Collapse out the dups.  */
03382   for (i = 1, j = 1; i < nfound; ++i)
03383     {
03384       if (compare_search_syms (&symbols[j - 1], &symbols[i]) != 0)
03385         symbols[j++] = symbols[i];
03386       else
03387         xfree (symbols[i]);
03388     }
03389   nunique = j;
03390   symbols[j - 1]->next = NULL;
03391 
03392   /* Rebuild the linked list.  */
03393   for (i = 0; i < nunique - 1; i++)
03394     symbols[i]->next = symbols[i + 1];
03395   symbols[nunique - 1]->next = NULL;
03396 
03397   *new_head = symbols[0];
03398   *new_tail = symbols[nunique - 1];
03399   xfree (symbols);
03400 }
03401 
03402 /* An object of this type is passed as the user_data to the
03403    expand_symtabs_matching method.  */
03404 struct search_symbols_data
03405 {
03406   int nfiles;
03407   char **files;
03408 
03409   /* It is true if PREG contains valid data, false otherwise.  */
03410   unsigned preg_p : 1;
03411   regex_t preg;
03412 };
03413 
03414 /* A callback for expand_symtabs_matching.  */
03415 
03416 static int
03417 search_symbols_file_matches (const char *filename, void *user_data,
03418                              int basenames)
03419 {
03420   struct search_symbols_data *data = user_data;
03421 
03422   return file_matches (filename, data->files, data->nfiles, basenames);
03423 }
03424 
03425 /* A callback for expand_symtabs_matching.  */
03426 
03427 static int
03428 search_symbols_name_matches (const char *symname, void *user_data)
03429 {
03430   struct search_symbols_data *data = user_data;
03431 
03432   return !data->preg_p || regexec (&data->preg, symname, 0, NULL, 0) == 0;
03433 }
03434 
03435 /* Search the symbol table for matches to the regular expression REGEXP,
03436    returning the results in *MATCHES.
03437 
03438    Only symbols of KIND are searched:
03439    VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
03440                       and constants (enums)
03441    FUNCTIONS_DOMAIN - search all functions
03442    TYPES_DOMAIN     - search all type names
03443    ALL_DOMAIN       - an internal error for this function
03444 
03445    free_search_symbols should be called when *MATCHES is no longer needed.
03446 
03447    Within each file the results are sorted locally; each symtab's global and
03448    static blocks are separately alphabetized.
03449    Duplicate entries are removed.  */
03450 
03451 void
03452 search_symbols (char *regexp, enum search_domain kind,
03453                 int nfiles, char *files[],
03454                 struct symbol_search **matches)
03455 {
03456   struct symtab *s;
03457   struct blockvector *bv;
03458   struct block *b;
03459   int i = 0;
03460   struct block_iterator iter;
03461   struct symbol *sym;
03462   struct objfile *objfile;
03463   struct minimal_symbol *msymbol;
03464   int found_misc = 0;
03465   static const enum minimal_symbol_type types[]
03466     = {mst_data, mst_text, mst_abs};
03467   static const enum minimal_symbol_type types2[]
03468     = {mst_bss, mst_file_text, mst_abs};
03469   static const enum minimal_symbol_type types3[]
03470     = {mst_file_data, mst_solib_trampoline, mst_abs};
03471   static const enum minimal_symbol_type types4[]
03472     = {mst_file_bss, mst_text_gnu_ifunc, mst_abs};
03473   enum minimal_symbol_type ourtype;
03474   enum minimal_symbol_type ourtype2;
03475   enum minimal_symbol_type ourtype3;
03476   enum minimal_symbol_type ourtype4;
03477   struct symbol_search *found;
03478   struct symbol_search *tail;
03479   struct search_symbols_data datum;
03480   int nfound;
03481 
03482   /* OLD_CHAIN .. RETVAL_CHAIN is always freed, RETVAL_CHAIN .. current
03483      CLEANUP_CHAIN is freed only in the case of an error.  */
03484   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
03485   struct cleanup *retval_chain;
03486 
03487   gdb_assert (kind <= TYPES_DOMAIN);
03488 
03489   ourtype = types[kind];
03490   ourtype2 = types2[kind];
03491   ourtype3 = types3[kind];
03492   ourtype4 = types4[kind];
03493 
03494   *matches = NULL;
03495   datum.preg_p = 0;
03496 
03497   if (regexp != NULL)
03498     {
03499       /* Make sure spacing is right for C++ operators.
03500          This is just a courtesy to make the matching less sensitive
03501          to how many spaces the user leaves between 'operator'
03502          and <TYPENAME> or <OPERATOR>.  */
03503       char *opend;
03504       char *opname = operator_chars (regexp, &opend);
03505       int errcode;
03506 
03507       if (*opname)
03508         {
03509           int fix = -1;         /* -1 means ok; otherwise number of
03510                                     spaces needed.  */
03511 
03512           if (isalpha (*opname) || *opname == '_' || *opname == '$')
03513             {
03514               /* There should 1 space between 'operator' and 'TYPENAME'.  */
03515               if (opname[-1] != ' ' || opname[-2] == ' ')
03516                 fix = 1;
03517             }
03518           else
03519             {
03520               /* There should 0 spaces between 'operator' and 'OPERATOR'.  */
03521               if (opname[-1] == ' ')
03522                 fix = 0;
03523             }
03524           /* If wrong number of spaces, fix it.  */
03525           if (fix >= 0)
03526             {
03527               char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
03528 
03529               sprintf (tmp, "operator%.*s%s", fix, " ", opname);
03530               regexp = tmp;
03531             }
03532         }
03533 
03534       errcode = regcomp (&datum.preg, regexp,
03535                          REG_NOSUB | (case_sensitivity == case_sensitive_off
03536                                       ? REG_ICASE : 0));
03537       if (errcode != 0)
03538         {
03539           char *err = get_regcomp_error (errcode, &datum.preg);
03540 
03541           make_cleanup (xfree, err);
03542           error (_("Invalid regexp (%s): %s"), err, regexp);
03543         }
03544       datum.preg_p = 1;
03545       make_regfree_cleanup (&datum.preg);
03546     }
03547 
03548   /* Search through the partial symtabs *first* for all symbols
03549      matching the regexp.  That way we don't have to reproduce all of
03550      the machinery below.  */
03551 
03552   datum.nfiles = nfiles;
03553   datum.files = files;
03554   ALL_OBJFILES (objfile)
03555   {
03556     if (objfile->sf)
03557       objfile->sf->qf->expand_symtabs_matching (objfile,
03558                                                 (nfiles == 0
03559                                                  ? NULL
03560                                                  : search_symbols_file_matches),
03561                                                 search_symbols_name_matches,
03562                                                 kind,
03563                                                 &datum);
03564   }
03565 
03566   /* Here, we search through the minimal symbol tables for functions
03567      and variables that match, and force their symbols to be read.
03568      This is in particular necessary for demangled variable names,
03569      which are no longer put into the partial symbol tables.
03570      The symbol will then be found during the scan of symtabs below.
03571 
03572      For functions, find_pc_symtab should succeed if we have debug info
03573      for the function, for variables we have to call
03574      lookup_symbol_in_objfile_from_linkage_name to determine if the variable
03575      has debug info.
03576      If the lookup fails, set found_misc so that we will rescan to print
03577      any matching symbols without debug info.
03578      We only search the objfile the msymbol came from, we no longer search
03579      all objfiles.  In large programs (1000s of shared libs) searching all
03580      objfiles is not worth the pain.  */
03581 
03582   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
03583     {
03584       ALL_MSYMBOLS (objfile, msymbol)
03585       {
03586         QUIT;
03587 
03588         if (msymbol->created_by_gdb)
03589           continue;
03590 
03591         if (MSYMBOL_TYPE (msymbol) == ourtype
03592             || MSYMBOL_TYPE (msymbol) == ourtype2
03593             || MSYMBOL_TYPE (msymbol) == ourtype3
03594             || MSYMBOL_TYPE (msymbol) == ourtype4)
03595           {
03596             if (!datum.preg_p
03597                 || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
03598                             NULL, 0) == 0)
03599               {
03600                 /* Note: An important side-effect of these lookup functions
03601                    is to expand the symbol table if msymbol is found, for the
03602                    benefit of the next loop on ALL_PRIMARY_SYMTABS.  */
03603                 if (kind == FUNCTIONS_DOMAIN
03604                     ? find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL
03605                     : (lookup_symbol_in_objfile_from_linkage_name
03606                        (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
03607                        == NULL))
03608                   found_misc = 1;
03609               }
03610           }
03611       }
03612     }
03613 
03614   found = NULL;
03615   tail = NULL;
03616   nfound = 0;
03617   retval_chain = make_cleanup_free_search_symbols (&found);
03618 
03619   ALL_PRIMARY_SYMTABS (objfile, s)
03620   {
03621     bv = BLOCKVECTOR (s);
03622     for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
03623       {
03624         b = BLOCKVECTOR_BLOCK (bv, i);
03625         ALL_BLOCK_SYMBOLS (b, iter, sym)
03626           {
03627             struct symtab *real_symtab = SYMBOL_SYMTAB (sym);
03628 
03629             QUIT;
03630 
03631             /* Check first sole REAL_SYMTAB->FILENAME.  It does not need to be
03632                a substring of symtab_to_fullname as it may contain "./" etc.  */
03633             if ((file_matches (real_symtab->filename, files, nfiles, 0)
03634                  || ((basenames_may_differ
03635                       || file_matches (lbasename (real_symtab->filename),
03636                                        files, nfiles, 1))
03637                      && file_matches (symtab_to_fullname (real_symtab),
03638                                       files, nfiles, 0)))
03639                 && ((!datum.preg_p
03640                      || regexec (&datum.preg, SYMBOL_NATURAL_NAME (sym), 0,
03641                                  NULL, 0) == 0)
03642                     && ((kind == VARIABLES_DOMAIN
03643                          && SYMBOL_CLASS (sym) != LOC_TYPEDEF
03644                          && SYMBOL_CLASS (sym) != LOC_UNRESOLVED
03645                          && SYMBOL_CLASS (sym) != LOC_BLOCK
03646                          /* LOC_CONST can be used for more than just enums,
03647                             e.g., c++ static const members.
03648                             We only want to skip enums here.  */
03649                          && !(SYMBOL_CLASS (sym) == LOC_CONST
03650                               && TYPE_CODE (SYMBOL_TYPE (sym))
03651                               == TYPE_CODE_ENUM))
03652                         || (kind == FUNCTIONS_DOMAIN 
03653                             && SYMBOL_CLASS (sym) == LOC_BLOCK)
03654                         || (kind == TYPES_DOMAIN
03655                             && SYMBOL_CLASS (sym) == LOC_TYPEDEF))))
03656               {
03657                 /* match */
03658                 struct symbol_search *psr = (struct symbol_search *)
03659                   xmalloc (sizeof (struct symbol_search));
03660                 psr->block = i;
03661                 psr->symtab = real_symtab;
03662                 psr->symbol = sym;
03663                 memset (&psr->msymbol, 0, sizeof (psr->msymbol));
03664                 psr->next = NULL;
03665                 if (tail == NULL)
03666                   found = psr;
03667                 else
03668                   tail->next = psr;
03669                 tail = psr;
03670                 nfound ++;
03671               }
03672           }
03673       }
03674   }
03675 
03676   if (found != NULL)
03677     {
03678       sort_search_symbols_remove_dups (found, nfound, &found, &tail);
03679       /* Note: nfound is no longer useful beyond this point.  */
03680     }
03681 
03682   /* If there are no eyes, avoid all contact.  I mean, if there are
03683      no debug symbols, then print directly from the msymbol_vector.  */
03684 
03685   if (found_misc || (nfiles == 0 && kind != FUNCTIONS_DOMAIN))
03686     {
03687       ALL_MSYMBOLS (objfile, msymbol)
03688       {
03689         QUIT;
03690 
03691         if (msymbol->created_by_gdb)
03692           continue;
03693 
03694         if (MSYMBOL_TYPE (msymbol) == ourtype
03695             || MSYMBOL_TYPE (msymbol) == ourtype2
03696             || MSYMBOL_TYPE (msymbol) == ourtype3
03697             || MSYMBOL_TYPE (msymbol) == ourtype4)
03698           {
03699             if (!datum.preg_p
03700                 || regexec (&datum.preg, SYMBOL_NATURAL_NAME (msymbol), 0,
03701                             NULL, 0) == 0)
03702               {
03703                 /* For functions we can do a quick check of whether the
03704                    symbol might be found via find_pc_symtab.  */
03705                 if (kind != FUNCTIONS_DOMAIN
03706                     || find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)) == NULL)
03707                   {
03708                     if (lookup_symbol_in_objfile_from_linkage_name
03709                         (objfile, SYMBOL_LINKAGE_NAME (msymbol), VAR_DOMAIN)
03710                         == NULL)
03711                       {
03712                         /* match */
03713                         struct symbol_search *psr = (struct symbol_search *)
03714                           xmalloc (sizeof (struct symbol_search));
03715                         psr->block = i;
03716                         psr->msymbol.minsym = msymbol;
03717                         psr->msymbol.objfile = objfile;
03718                         psr->symtab = NULL;
03719                         psr->symbol = NULL;
03720                         psr->next = NULL;
03721                         if (tail == NULL)
03722                           found = psr;
03723                         else
03724                           tail->next = psr;
03725                         tail = psr;
03726                       }
03727                   }
03728               }
03729           }
03730       }
03731     }
03732 
03733   discard_cleanups (retval_chain);
03734   do_cleanups (old_chain);
03735   *matches = found;
03736 }
03737 
03738 /* Helper function for symtab_symbol_info, this function uses
03739    the data returned from search_symbols() to print information
03740    regarding the match to gdb_stdout.  */
03741 
03742 static void
03743 print_symbol_info (enum search_domain kind,
03744                    struct symtab *s, struct symbol *sym,
03745                    int block, const char *last)
03746 {
03747   const char *s_filename = symtab_to_filename_for_display (s);
03748 
03749   if (last == NULL || filename_cmp (last, s_filename) != 0)
03750     {
03751       fputs_filtered ("\nFile ", gdb_stdout);
03752       fputs_filtered (s_filename, gdb_stdout);
03753       fputs_filtered (":\n", gdb_stdout);
03754     }
03755 
03756   if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
03757     printf_filtered ("static ");
03758 
03759   /* Typedef that is not a C++ class.  */
03760   if (kind == TYPES_DOMAIN
03761       && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
03762     typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
03763   /* variable, func, or typedef-that-is-c++-class.  */
03764   else if (kind < TYPES_DOMAIN
03765            || (kind == TYPES_DOMAIN
03766                && SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
03767     {
03768       type_print (SYMBOL_TYPE (sym),
03769                   (SYMBOL_CLASS (sym) == LOC_TYPEDEF
03770                    ? "" : SYMBOL_PRINT_NAME (sym)),
03771                   gdb_stdout, 0);
03772 
03773       printf_filtered (";\n");
03774     }
03775 }
03776 
03777 /* This help function for symtab_symbol_info() prints information
03778    for non-debugging symbols to gdb_stdout.  */
03779 
03780 static void
03781 print_msymbol_info (struct bound_minimal_symbol msymbol)
03782 {
03783   struct gdbarch *gdbarch = get_objfile_arch (msymbol.objfile);
03784   char *tmp;
03785 
03786   if (gdbarch_addr_bit (gdbarch) <= 32)
03787     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol.minsym)
03788                              & (CORE_ADDR) 0xffffffff,
03789                              8);
03790   else
03791     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol.minsym),
03792                              16);
03793   printf_filtered ("%s  %s\n",
03794                    tmp, SYMBOL_PRINT_NAME (msymbol.minsym));
03795 }
03796 
03797 /* This is the guts of the commands "info functions", "info types", and
03798    "info variables".  It calls search_symbols to find all matches and then
03799    print_[m]symbol_info to print out some useful information about the
03800    matches.  */
03801 
03802 static void
03803 symtab_symbol_info (char *regexp, enum search_domain kind, int from_tty)
03804 {
03805   static const char * const classnames[] =
03806     {"variable", "function", "type"};
03807   struct symbol_search *symbols;
03808   struct symbol_search *p;
03809   struct cleanup *old_chain;
03810   const char *last_filename = NULL;
03811   int first = 1;
03812 
03813   gdb_assert (kind <= TYPES_DOMAIN);
03814 
03815   /* Must make sure that if we're interrupted, symbols gets freed.  */
03816   search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
03817   old_chain = make_cleanup_free_search_symbols (&symbols);
03818 
03819   if (regexp != NULL)
03820     printf_filtered (_("All %ss matching regular expression \"%s\":\n"),
03821                      classnames[kind], regexp);
03822   else
03823     printf_filtered (_("All defined %ss:\n"), classnames[kind]);
03824 
03825   for (p = symbols; p != NULL; p = p->next)
03826     {
03827       QUIT;
03828 
03829       if (p->msymbol.minsym != NULL)
03830         {
03831           if (first)
03832             {
03833               printf_filtered (_("\nNon-debugging symbols:\n"));
03834               first = 0;
03835             }
03836           print_msymbol_info (p->msymbol);
03837         }
03838       else
03839         {
03840           print_symbol_info (kind,
03841                              p->symtab,
03842                              p->symbol,
03843                              p->block,
03844                              last_filename);
03845           last_filename = symtab_to_filename_for_display (p->symtab);
03846         }
03847     }
03848 
03849   do_cleanups (old_chain);
03850 }
03851 
03852 static void
03853 variables_info (char *regexp, int from_tty)
03854 {
03855   symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
03856 }
03857 
03858 static void
03859 functions_info (char *regexp, int from_tty)
03860 {
03861   symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
03862 }
03863 
03864 
03865 static void
03866 types_info (char *regexp, int from_tty)
03867 {
03868   symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
03869 }
03870 
03871 /* Breakpoint all functions matching regular expression.  */
03872 
03873 void
03874 rbreak_command_wrapper (char *regexp, int from_tty)
03875 {
03876   rbreak_command (regexp, from_tty);
03877 }
03878 
03879 /* A cleanup function that calls end_rbreak_breakpoints.  */
03880 
03881 static void
03882 do_end_rbreak_breakpoints (void *ignore)
03883 {
03884   end_rbreak_breakpoints ();
03885 }
03886 
03887 static void
03888 rbreak_command (char *regexp, int from_tty)
03889 {
03890   struct symbol_search *ss;
03891   struct symbol_search *p;
03892   struct cleanup *old_chain;
03893   char *string = NULL;
03894   int len = 0;
03895   char **files = NULL, *file_name;
03896   int nfiles = 0;
03897 
03898   if (regexp)
03899     {
03900       char *colon = strchr (regexp, ':');
03901 
03902       if (colon && *(colon + 1) != ':')
03903         {
03904           int colon_index;
03905 
03906           colon_index = colon - regexp;
03907           file_name = alloca (colon_index + 1);
03908           memcpy (file_name, regexp, colon_index);
03909           file_name[colon_index--] = 0;
03910           while (isspace (file_name[colon_index]))
03911             file_name[colon_index--] = 0; 
03912           files = &file_name;
03913           nfiles = 1;
03914           regexp = skip_spaces (colon + 1);
03915         }
03916     }
03917 
03918   search_symbols (regexp, FUNCTIONS_DOMAIN, nfiles, files, &ss);
03919   old_chain = make_cleanup_free_search_symbols (&ss);
03920   make_cleanup (free_current_contents, &string);
03921 
03922   start_rbreak_breakpoints ();
03923   make_cleanup (do_end_rbreak_breakpoints, NULL);
03924   for (p = ss; p != NULL; p = p->next)
03925     {
03926       if (p->msymbol.minsym == NULL)
03927         {
03928           const char *fullname = symtab_to_fullname (p->symtab);
03929 
03930           int newlen = (strlen (fullname)
03931                         + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
03932                         + 4);
03933 
03934           if (newlen > len)
03935             {
03936               string = xrealloc (string, newlen);
03937               len = newlen;
03938             }
03939           strcpy (string, fullname);
03940           strcat (string, ":'");
03941           strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
03942           strcat (string, "'");
03943           break_command (string, from_tty);
03944           print_symbol_info (FUNCTIONS_DOMAIN,
03945                              p->symtab,
03946                              p->symbol,
03947                              p->block,
03948                              symtab_to_filename_for_display (p->symtab));
03949         }
03950       else
03951         {
03952           int newlen = (strlen (SYMBOL_LINKAGE_NAME (p->msymbol.minsym)) + 3);
03953 
03954           if (newlen > len)
03955             {
03956               string = xrealloc (string, newlen);
03957               len = newlen;
03958             }
03959           strcpy (string, "'");
03960           strcat (string, SYMBOL_LINKAGE_NAME (p->msymbol.minsym));
03961           strcat (string, "'");
03962 
03963           break_command (string, from_tty);
03964           printf_filtered ("<function, no debug info> %s;\n",
03965                            SYMBOL_PRINT_NAME (p->msymbol.minsym));
03966         }
03967     }
03968 
03969   do_cleanups (old_chain);
03970 }
03971 
03972 
03973 /* Evaluate if NAME matches SYM_TEXT and SYM_TEXT_LEN.
03974 
03975    Either sym_text[sym_text_len] != '(' and then we search for any
03976    symbol starting with SYM_TEXT text.
03977 
03978    Otherwise sym_text[sym_text_len] == '(' and then we require symbol name to
03979    be terminated at that point.  Partial symbol tables do not have parameters
03980    information.  */
03981 
03982 static int
03983 compare_symbol_name (const char *name, const char *sym_text, int sym_text_len)
03984 {
03985   int (*ncmp) (const char *, const char *, size_t);
03986 
03987   ncmp = (case_sensitivity == case_sensitive_on ? strncmp : strncasecmp);
03988 
03989   if (ncmp (name, sym_text, sym_text_len) != 0)
03990     return 0;
03991 
03992   if (sym_text[sym_text_len] == '(')
03993     {
03994       /* User searches for `name(someth...'.  Require NAME to be terminated.
03995          Normally psymtabs and gdbindex have no parameter types so '\0' will be
03996          present but accept even parameters presence.  In this case this
03997          function is in fact strcmp_iw but whitespace skipping is not supported
03998          for tab completion.  */
03999 
04000       if (name[sym_text_len] != '\0' && name[sym_text_len] != '(')
04001         return 0;
04002     }
04003 
04004   return 1;
04005 }
04006 
04007 /* Free any memory associated with a completion list.  */
04008 
04009 static void
04010 free_completion_list (VEC (char_ptr) **list_ptr)
04011 {
04012   int i;
04013   char *p;
04014 
04015   for (i = 0; VEC_iterate (char_ptr, *list_ptr, i, p); ++i)
04016     xfree (p);
04017   VEC_free (char_ptr, *list_ptr);
04018 }
04019 
04020 /* Callback for make_cleanup.  */
04021 
04022 static void
04023 do_free_completion_list (void *list)
04024 {
04025   free_completion_list (list);
04026 }
04027 
04028 /* Helper routine for make_symbol_completion_list.  */
04029 
04030 static VEC (char_ptr) *return_val;
04031 
04032 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
04033       completion_list_add_name \
04034         (SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
04035 
04036 /*  Test to see if the symbol specified by SYMNAME (which is already
04037    demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
04038    characters.  If so, add it to the current completion list.  */
04039 
04040 static void
04041 completion_list_add_name (const char *symname,
04042                           const char *sym_text, int sym_text_len,
04043                           const char *text, const char *word)
04044 {
04045   /* Clip symbols that cannot match.  */
04046   if (!compare_symbol_name (symname, sym_text, sym_text_len))
04047     return;
04048 
04049   /* We have a match for a completion, so add SYMNAME to the current list
04050      of matches.  Note that the name is moved to freshly malloc'd space.  */
04051 
04052   {
04053     char *new;
04054 
04055     if (word == sym_text)
04056       {
04057         new = xmalloc (strlen (symname) + 5);
04058         strcpy (new, symname);
04059       }
04060     else if (word > sym_text)
04061       {
04062         /* Return some portion of symname.  */
04063         new = xmalloc (strlen (symname) + 5);
04064         strcpy (new, symname + (word - sym_text));
04065       }
04066     else
04067       {
04068         /* Return some of SYM_TEXT plus symname.  */
04069         new = xmalloc (strlen (symname) + (sym_text - word) + 5);
04070         strncpy (new, word, sym_text - word);
04071         new[sym_text - word] = '\0';
04072         strcat (new, symname);
04073       }
04074 
04075     VEC_safe_push (char_ptr, return_val, new);
04076   }
04077 }
04078 
04079 /* ObjC: In case we are completing on a selector, look as the msymbol
04080    again and feed all the selectors into the mill.  */
04081 
04082 static void
04083 completion_list_objc_symbol (struct minimal_symbol *msymbol,
04084                              const char *sym_text, int sym_text_len,
04085                              const char *text, const char *word)
04086 {
04087   static char *tmp = NULL;
04088   static unsigned int tmplen = 0;
04089 
04090   const char *method, *category, *selector;
04091   char *tmp2 = NULL;
04092 
04093   method = SYMBOL_NATURAL_NAME (msymbol);
04094 
04095   /* Is it a method?  */
04096   if ((method[0] != '-') && (method[0] != '+'))
04097     return;
04098 
04099   if (sym_text[0] == '[')
04100     /* Complete on shortened method method.  */
04101     completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);
04102 
04103   while ((strlen (method) + 1) >= tmplen)
04104     {
04105       if (tmplen == 0)
04106         tmplen = 1024;
04107       else
04108         tmplen *= 2;
04109       tmp = xrealloc (tmp, tmplen);
04110     }
04111   selector = strchr (method, ' ');
04112   if (selector != NULL)
04113     selector++;
04114 
04115   category = strchr (method, '(');
04116 
04117   if ((category != NULL) && (selector != NULL))
04118     {
04119       memcpy (tmp, method, (category - method));
04120       tmp[category - method] = ' ';
04121       memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
04122       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
04123       if (sym_text[0] == '[')
04124         completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
04125     }
04126 
04127   if (selector != NULL)
04128     {
04129       /* Complete on selector only.  */
04130       strcpy (tmp, selector);
04131       tmp2 = strchr (tmp, ']');
04132       if (tmp2 != NULL)
04133         *tmp2 = '\0';
04134 
04135       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
04136     }
04137 }
04138 
04139 /* Break the non-quoted text based on the characters which are in
04140    symbols.  FIXME: This should probably be language-specific.  */
04141 
04142 static const char *
04143 language_search_unquoted_string (const char *text, const char *p)
04144 {
04145   for (; p > text; --p)
04146     {
04147       if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
04148         continue;
04149       else
04150         {
04151           if ((current_language->la_language == language_objc))
04152             {
04153               if (p[-1] == ':')     /* Might be part of a method name.  */
04154                 continue;
04155               else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
04156                 p -= 2;             /* Beginning of a method name.  */
04157               else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
04158                 {                   /* Might be part of a method name.  */
04159                   const char *t = p;
04160 
04161                   /* Seeing a ' ' or a '(' is not conclusive evidence
04162                      that we are in the middle of a method name.  However,
04163                      finding "-[" or "+[" should be pretty un-ambiguous.
04164                      Unfortunately we have to find it now to decide.  */
04165 
04166                   while (t > text)
04167                     if (isalnum (t[-1]) || t[-1] == '_' ||
04168                         t[-1] == ' '    || t[-1] == ':' ||
04169                         t[-1] == '('    || t[-1] == ')')
04170                       --t;
04171                     else
04172                       break;
04173 
04174                   if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
04175                     p = t - 2;      /* Method name detected.  */
04176                   /* Else we leave with p unchanged.  */
04177                 }
04178             }
04179           break;
04180         }
04181     }
04182   return p;
04183 }
04184 
04185 static void
04186 completion_list_add_fields (struct symbol *sym, const char *sym_text,
04187                             int sym_text_len, const char *text,
04188                             const char *word)
04189 {
04190   if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
04191     {
04192       struct type *t = SYMBOL_TYPE (sym);
04193       enum type_code c = TYPE_CODE (t);
04194       int j;
04195 
04196       if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
04197         for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
04198           if (TYPE_FIELD_NAME (t, j))
04199             completion_list_add_name (TYPE_FIELD_NAME (t, j),
04200                                       sym_text, sym_text_len, text, word);
04201     }
04202 }
04203 
04204 /* Type of the user_data argument passed to add_macro_name or
04205    expand_partial_symbol_name.  The contents are simply whatever is
04206    needed by completion_list_add_name.  */
04207 struct add_name_data
04208 {
04209   const char *sym_text;
04210   int sym_text_len;
04211   const char *text;
04212   const char *word;
04213 };
04214 
04215 /* A callback used with macro_for_each and macro_for_each_in_scope.
04216    This adds a macro's name to the current completion list.  */
04217 
04218 static void
04219 add_macro_name (const char *name, const struct macro_definition *ignore,
04220                 struct macro_source_file *ignore2, int ignore3,
04221                 void *user_data)
04222 {
04223   struct add_name_data *datum = (struct add_name_data *) user_data;
04224 
04225   completion_list_add_name ((char *) name,
04226                             datum->sym_text, datum->sym_text_len,
04227                             datum->text, datum->word);
04228 }
04229 
04230 /* A callback for expand_partial_symbol_names.  */
04231 
04232 static int
04233 expand_partial_symbol_name (const char *name, void *user_data)
04234 {
04235   struct add_name_data *datum = (struct add_name_data *) user_data;
04236 
04237   return compare_symbol_name (name, datum->sym_text, datum->sym_text_len);
04238 }
04239 
04240 VEC (char_ptr) *
04241 default_make_symbol_completion_list_break_on (const char *text,
04242                                               const char *word,
04243                                               const char *break_on,
04244                                               enum type_code code)
04245 {
04246   /* Problem: All of the symbols have to be copied because readline
04247      frees them.  I'm not going to worry about this; hopefully there
04248      won't be that many.  */
04249 
04250   struct symbol *sym;
04251   struct symtab *s;
04252   struct minimal_symbol *msymbol;
04253   struct objfile *objfile;
04254   struct block *b;
04255   const struct block *surrounding_static_block, *surrounding_global_block;
04256   struct block_iterator iter;
04257   /* The symbol we are completing on.  Points in same buffer as text.  */
04258   const char *sym_text;
04259   /* Length of sym_text.  */
04260   int sym_text_len;
04261   struct add_name_data datum;
04262   struct cleanup *back_to;
04263 
04264   /* Now look for the symbol we are supposed to complete on.  */
04265   {
04266     const char *p;
04267     char quote_found;
04268     const char *quote_pos = NULL;
04269 
04270     /* First see if this is a quoted string.  */
04271     quote_found = '\0';
04272     for (p = text; *p != '\0'; ++p)
04273       {
04274         if (quote_found != '\0')
04275           {
04276             if (*p == quote_found)
04277               /* Found close quote.  */
04278               quote_found = '\0';
04279             else if (*p == '\\' && p[1] == quote_found)
04280               /* A backslash followed by the quote character
04281                  doesn't end the string.  */
04282               ++p;
04283           }
04284         else if (*p == '\'' || *p == '"')
04285           {
04286             quote_found = *p;
04287             quote_pos = p;
04288           }
04289       }
04290     if (quote_found == '\'')
04291       /* A string within single quotes can be a symbol, so complete on it.  */
04292       sym_text = quote_pos + 1;
04293     else if (quote_found == '"')
04294       /* A double-quoted string is never a symbol, nor does it make sense
04295          to complete it any other way.  */
04296       {
04297         return NULL;
04298       }
04299     else
04300       {
04301         /* It is not a quoted string.  Break it based on the characters
04302            which are in symbols.  */
04303         while (p > text)
04304           {
04305             if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0'
04306                 || p[-1] == ':' || strchr (break_on, p[-1]) != NULL)
04307               --p;
04308             else
04309               break;
04310           }
04311         sym_text = p;
04312       }
04313   }
04314 
04315   sym_text_len = strlen (sym_text);
04316 
04317   /* Prepare SYM_TEXT_LEN for compare_symbol_name.  */
04318 
04319   if (current_language->la_language == language_cplus
04320       || current_language->la_language == language_java
04321       || current_language->la_language == language_fortran)
04322     {
04323       /* These languages may have parameters entered by user but they are never
04324          present in the partial symbol tables.  */
04325 
04326       const char *cs = memchr (sym_text, '(', sym_text_len);
04327 
04328       if (cs)
04329         sym_text_len = cs - sym_text;
04330     }
04331   gdb_assert (sym_text[sym_text_len] == '\0' || sym_text[sym_text_len] == '(');
04332 
04333   return_val = NULL;
04334   back_to = make_cleanup (do_free_completion_list, &return_val);
04335 
04336   datum.sym_text = sym_text;
04337   datum.sym_text_len = sym_text_len;
04338   datum.text = text;
04339   datum.word = word;
04340 
04341   /* Look through the partial symtabs for all symbols which begin
04342      by matching SYM_TEXT.  Expand all CUs that you find to the list.
04343      The real names will get added by COMPLETION_LIST_ADD_SYMBOL below.  */
04344   expand_partial_symbol_names (expand_partial_symbol_name, &datum);
04345 
04346   /* At this point scan through the misc symbol vectors and add each
04347      symbol you find to the list.  Eventually we want to ignore
04348      anything that isn't a text symbol (everything else will be
04349      handled by the psymtab code above).  */
04350 
04351   if (code == TYPE_CODE_UNDEF)
04352     {
04353       ALL_MSYMBOLS (objfile, msymbol)
04354         {
04355           QUIT;
04356           COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text,
04357                                       word);
04358 
04359           completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text,
04360                                        word);
04361         }
04362     }
04363 
04364   /* Search upwards from currently selected frame (so that we can
04365      complete on local vars).  Also catch fields of types defined in
04366      this places which match our text string.  Only complete on types
04367      visible from current context.  */
04368 
04369   b = get_selected_block (0);
04370   surrounding_static_block = block_static_block (b);
04371   surrounding_global_block = block_global_block (b);
04372   if (surrounding_static_block != NULL)
04373     while (b != surrounding_static_block)
04374       {
04375         QUIT;
04376 
04377         ALL_BLOCK_SYMBOLS (b, iter, sym)
04378           {
04379             if (code == TYPE_CODE_UNDEF)
04380               {
04381                 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
04382                                             word);
04383                 completion_list_add_fields (sym, sym_text, sym_text_len, text,
04384                                             word);
04385               }
04386             else if (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
04387                      && TYPE_CODE (SYMBOL_TYPE (sym)) == code)
04388               COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text,
04389                                           word);
04390           }
04391 
04392         /* Stop when we encounter an enclosing function.  Do not stop for
04393            non-inlined functions - the locals of the enclosing function
04394            are in scope for a nested function.  */
04395         if (BLOCK_FUNCTION (b) != NULL && block_inlined_p (b))
04396           break;
04397         b = BLOCK_SUPERBLOCK (b);
04398       }
04399 
04400   /* Add fields from the file's types; symbols will be added below.  */
04401 
04402   if (code == TYPE_CODE_UNDEF)
04403     {
04404       if (surrounding_static_block != NULL)
04405         ALL_BLOCK_SYMBOLS (surrounding_static_block, iter, sym)
04406           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
04407 
04408       if (surrounding_global_block != NULL)
04409         ALL_BLOCK_SYMBOLS (surrounding_global_block, iter, sym)
04410           completion_list_add_fields (sym, sym_text, sym_text_len, text, word);
04411     }
04412 
04413   /* Go through the symtabs and check the externs and statics for
04414      symbols which match.  */
04415 
04416   ALL_PRIMARY_SYMTABS (objfile, s)
04417   {
04418     QUIT;
04419     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
04420     ALL_BLOCK_SYMBOLS (b, iter, sym)
04421       {
04422         if (code == TYPE_CODE_UNDEF
04423             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
04424                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
04425           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
04426       }
04427   }
04428 
04429   ALL_PRIMARY_SYMTABS (objfile, s)
04430   {
04431     QUIT;
04432     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
04433     ALL_BLOCK_SYMBOLS (b, iter, sym)
04434       {
04435         if (code == TYPE_CODE_UNDEF
04436             || (SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN
04437                 && TYPE_CODE (SYMBOL_TYPE (sym)) == code))
04438           COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
04439       }
04440   }
04441 
04442   /* Skip macros if we are completing a struct tag -- arguable but
04443      usually what is expected.  */
04444   if (current_language->la_macro_expansion == macro_expansion_c
04445       && code == TYPE_CODE_UNDEF)
04446     {
04447       struct macro_scope *scope;
04448 
04449       /* Add any macros visible in the default scope.  Note that this
04450          may yield the occasional wrong result, because an expression
04451          might be evaluated in a scope other than the default.  For
04452          example, if the user types "break file:line if <TAB>", the
04453          resulting expression will be evaluated at "file:line" -- but
04454          at there does not seem to be a way to detect this at
04455          completion time.  */
04456       scope = default_macro_scope ();
04457       if (scope)
04458         {
04459           macro_for_each_in_scope (scope->file, scope->line,
04460                                    add_macro_name, &datum);
04461           xfree (scope);
04462         }
04463 
04464       /* User-defined macros are always visible.  */
04465       macro_for_each (macro_user_macros, add_macro_name, &datum);
04466     }
04467 
04468   discard_cleanups (back_to);
04469   return (return_val);
04470 }
04471 
04472 VEC (char_ptr) *
04473 default_make_symbol_completion_list (const char *text, const char *word,
04474                                      enum type_code code)
04475 {
04476   return default_make_symbol_completion_list_break_on (text, word, "", code);
04477 }
04478 
04479 /* Return a vector of all symbols (regardless of class) which begin by
04480    matching TEXT.  If the answer is no symbols, then the return value
04481    is NULL.  */
04482 
04483 VEC (char_ptr) *
04484 make_symbol_completion_list (const char *text, const char *word)
04485 {
04486   return current_language->la_make_symbol_completion_list (text, word,
04487                                                            TYPE_CODE_UNDEF);
04488 }
04489 
04490 /* Like make_symbol_completion_list, but only return STRUCT_DOMAIN
04491    symbols whose type code is CODE.  */
04492 
04493 VEC (char_ptr) *
04494 make_symbol_completion_type (const char *text, const char *word,
04495                              enum type_code code)
04496 {
04497   gdb_assert (code == TYPE_CODE_UNION
04498               || code == TYPE_CODE_STRUCT
04499               || code == TYPE_CODE_CLASS
04500               || code == TYPE_CODE_ENUM);
04501   return current_language->la_make_symbol_completion_list (text, word, code);
04502 }
04503 
04504 /* Like make_symbol_completion_list, but suitable for use as a
04505    completion function.  */
04506 
04507 VEC (char_ptr) *
04508 make_symbol_completion_list_fn (struct cmd_list_element *ignore,
04509                                 const char *text, const char *word)
04510 {
04511   return make_symbol_completion_list (text, word);
04512 }
04513 
04514 /* Like make_symbol_completion_list, but returns a list of symbols
04515    defined in a source file FILE.  */
04516 
04517 VEC (char_ptr) *
04518 make_file_symbol_completion_list (const char *text, const char *word,
04519                                   const char *srcfile)
04520 {
04521   struct symbol *sym;
04522   struct symtab *s;
04523   struct block *b;
04524   struct block_iterator iter;
04525   /* The symbol we are completing on.  Points in same buffer as text.  */
04526   const char *sym_text;
04527   /* Length of sym_text.  */
04528   int sym_text_len;
04529 
04530   /* Now look for the symbol we are supposed to complete on.
04531      FIXME: This should be language-specific.  */
04532   {
04533     const char *p;
04534     char quote_found;
04535     const char *quote_pos = NULL;
04536 
04537     /* First see if this is a quoted string.  */
04538     quote_found = '\0';
04539     for (p = text; *p != '\0'; ++p)
04540       {
04541         if (quote_found != '\0')
04542           {
04543             if (*p == quote_found)
04544               /* Found close quote.  */
04545               quote_found = '\0';
04546             else if (*p == '\\' && p[1] == quote_found)
04547               /* A backslash followed by the quote character
04548                  doesn't end the string.  */
04549               ++p;
04550           }
04551         else if (*p == '\'' || *p == '"')
04552           {
04553             quote_found = *p;
04554             quote_pos = p;
04555           }
04556       }
04557     if (quote_found == '\'')
04558       /* A string within single quotes can be a symbol, so complete on it.  */
04559       sym_text = quote_pos + 1;
04560     else if (quote_found == '"')
04561       /* A double-quoted string is never a symbol, nor does it make sense
04562          to complete it any other way.  */
04563       {
04564         return NULL;
04565       }
04566     else
04567       {
04568         /* Not a quoted string.  */
04569         sym_text = language_search_unquoted_string (text, p);
04570       }
04571   }
04572 
04573   sym_text_len = strlen (sym_text);
04574 
04575   return_val = NULL;
04576 
04577   /* Find the symtab for SRCFILE (this loads it if it was not yet read
04578      in).  */
04579   s = lookup_symtab (srcfile);
04580   if (s == NULL)
04581     {
04582       /* Maybe they typed the file with leading directories, while the
04583          symbol tables record only its basename.  */
04584       const char *tail = lbasename (srcfile);
04585 
04586       if (tail > srcfile)
04587         s = lookup_symtab (tail);
04588     }
04589 
04590   /* If we have no symtab for that file, return an empty list.  */
04591   if (s == NULL)
04592     return (return_val);
04593 
04594   /* Go through this symtab and check the externs and statics for
04595      symbols which match.  */
04596 
04597   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
04598   ALL_BLOCK_SYMBOLS (b, iter, sym)
04599     {
04600       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
04601     }
04602 
04603   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
04604   ALL_BLOCK_SYMBOLS (b, iter, sym)
04605     {
04606       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
04607     }
04608 
04609   return (return_val);
04610 }
04611 
04612 /* A helper function for make_source_files_completion_list.  It adds
04613    another file name to a list of possible completions, growing the
04614    list as necessary.  */
04615 
04616 static void
04617 add_filename_to_list (const char *fname, const char *text, const char *word,
04618                       VEC (char_ptr) **list)
04619 {
04620   char *new;
04621   size_t fnlen = strlen (fname);
04622 
04623   if (word == text)
04624     {
04625       /* Return exactly fname.  */
04626       new = xmalloc (fnlen + 5);
04627       strcpy (new, fname);
04628     }
04629   else if (word > text)
04630     {
04631       /* Return some portion of fname.  */
04632       new = xmalloc (fnlen + 5);
04633       strcpy (new, fname + (word - text));
04634     }
04635   else
04636     {
04637       /* Return some of TEXT plus fname.  */
04638       new = xmalloc (fnlen + (text - word) + 5);
04639       strncpy (new, word, text - word);
04640       new[text - word] = '\0';
04641       strcat (new, fname);
04642     }
04643   VEC_safe_push (char_ptr, *list, new);
04644 }
04645 
04646 static int
04647 not_interesting_fname (const char *fname)
04648 {
04649   static const char *illegal_aliens[] = {
04650     "_globals_",        /* inserted by coff_symtab_read */
04651     NULL
04652   };
04653   int i;
04654 
04655   for (i = 0; illegal_aliens[i]; i++)
04656     {
04657       if (filename_cmp (fname, illegal_aliens[i]) == 0)
04658         return 1;
04659     }
04660   return 0;
04661 }
04662 
04663 /* An object of this type is passed as the user_data argument to
04664    map_partial_symbol_filenames.  */
04665 struct add_partial_filename_data
04666 {
04667   struct filename_seen_cache *filename_seen_cache;
04668   const char *text;
04669   const char *word;
04670   int text_len;
04671   VEC (char_ptr) **list;
04672 };
04673 
04674 /* A callback for map_partial_symbol_filenames.  */
04675 
04676 static void
04677 maybe_add_partial_symtab_filename (const char *filename, const char *fullname,
04678                                    void *user_data)
04679 {
04680   struct add_partial_filename_data *data = user_data;
04681 
04682   if (not_interesting_fname (filename))
04683     return;
04684   if (!filename_seen (data->filename_seen_cache, filename, 1)
04685       && filename_ncmp (filename, data->text, data->text_len) == 0)
04686     {
04687       /* This file matches for a completion; add it to the
04688          current list of matches.  */
04689       add_filename_to_list (filename, data->text, data->word, data->list);
04690     }
04691   else
04692     {
04693       const char *base_name = lbasename (filename);
04694 
04695       if (base_name != filename
04696           && !filename_seen (data->filename_seen_cache, base_name, 1)
04697           && filename_ncmp (base_name, data->text, data->text_len) == 0)
04698         add_filename_to_list (base_name, data->text, data->word, data->list);
04699     }
04700 }
04701 
04702 /* Return a vector of all source files whose names begin with matching
04703    TEXT.  The file names are looked up in the symbol tables of this
04704    program.  If the answer is no matchess, then the return value is
04705    NULL.  */
04706 
04707 VEC (char_ptr) *
04708 make_source_files_completion_list (const char *text, const char *word)
04709 {
04710   struct symtab *s;
04711   struct objfile *objfile;
04712   size_t text_len = strlen (text);
04713   VEC (char_ptr) *list = NULL;
04714   const char *base_name;
04715   struct add_partial_filename_data datum;
04716   struct filename_seen_cache *filename_seen_cache;
04717   struct cleanup *back_to, *cache_cleanup;
04718 
04719   if (!have_full_symbols () && !have_partial_symbols ())
04720     return list;
04721 
04722   back_to = make_cleanup (do_free_completion_list, &list);
04723 
04724   filename_seen_cache = create_filename_seen_cache ();
04725   cache_cleanup = make_cleanup (delete_filename_seen_cache,
04726                                 filename_seen_cache);
04727 
04728   ALL_SYMTABS (objfile, s)
04729     {
04730       if (not_interesting_fname (s->filename))
04731         continue;
04732       if (!filename_seen (filename_seen_cache, s->filename, 1)
04733           && filename_ncmp (s->filename, text, text_len) == 0)
04734         {
04735           /* This file matches for a completion; add it to the current
04736              list of matches.  */
04737           add_filename_to_list (s->filename, text, word, &list);
04738         }
04739       else
04740         {
04741           /* NOTE: We allow the user to type a base name when the
04742              debug info records leading directories, but not the other
04743              way around.  This is what subroutines of breakpoint
04744              command do when they parse file names.  */
04745           base_name = lbasename (s->filename);
04746           if (base_name != s->filename
04747               && !filename_seen (filename_seen_cache, base_name, 1)
04748               && filename_ncmp (base_name, text, text_len) == 0)
04749             add_filename_to_list (base_name, text, word, &list);
04750         }
04751     }
04752 
04753   datum.filename_seen_cache = filename_seen_cache;
04754   datum.text = text;
04755   datum.word = word;
04756   datum.text_len = text_len;
04757   datum.list = &list;
04758   map_partial_symbol_filenames (maybe_add_partial_symtab_filename, &datum,
04759                                 0 /*need_fullname*/);
04760 
04761   do_cleanups (cache_cleanup);
04762   discard_cleanups (back_to);
04763 
04764   return list;
04765 }
04766 
04767 /* Determine if PC is in the prologue of a function.  The prologue is the area
04768    between the first instruction of a function, and the first executable line.
04769    Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
04770 
04771    If non-zero, func_start is where we think the prologue starts, possibly
04772    by previous examination of symbol table information.  */
04773 
04774 int
04775 in_prologue (struct gdbarch *gdbarch, CORE_ADDR pc, CORE_ADDR func_start)
04776 {
04777   struct symtab_and_line sal;
04778   CORE_ADDR func_addr, func_end;
04779 
04780   /* We have several sources of information we can consult to figure
04781      this out.
04782      - Compilers usually emit line number info that marks the prologue
04783        as its own "source line".  So the ending address of that "line"
04784        is the end of the prologue.  If available, this is the most
04785        reliable method.
04786      - The minimal symbols and partial symbols, which can usually tell
04787        us the starting and ending addresses of a function.
04788      - If we know the function's start address, we can call the
04789        architecture-defined gdbarch_skip_prologue function to analyze the
04790        instruction stream and guess where the prologue ends.
04791      - Our `func_start' argument; if non-zero, this is the caller's
04792        best guess as to the function's entry point.  At the time of
04793        this writing, handle_inferior_event doesn't get this right, so
04794        it should be our last resort.  */
04795 
04796   /* Consult the partial symbol table, to find which function
04797      the PC is in.  */
04798   if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
04799     {
04800       CORE_ADDR prologue_end;
04801 
04802       /* We don't even have minsym information, so fall back to using
04803          func_start, if given.  */
04804       if (! func_start)
04805         return 1;               /* We *might* be in a prologue.  */
04806 
04807       prologue_end = gdbarch_skip_prologue (gdbarch, func_start);
04808 
04809       return func_start <= pc && pc < prologue_end;
04810     }
04811 
04812   /* If we have line number information for the function, that's
04813      usually pretty reliable.  */
04814   sal = find_pc_line (func_addr, 0);
04815 
04816   /* Now sal describes the source line at the function's entry point,
04817      which (by convention) is the prologue.  The end of that "line",
04818      sal.end, is the end of the prologue.
04819 
04820      Note that, for functions whose source code is all on a single
04821      line, the line number information doesn't always end up this way.
04822      So we must verify that our purported end-of-prologue address is
04823      *within* the function, not at its start or end.  */
04824   if (sal.line == 0
04825       || sal.end <= func_addr
04826       || func_end <= sal.end)
04827     {
04828       /* We don't have any good line number info, so use the minsym
04829          information, together with the architecture-specific prologue
04830          scanning code.  */
04831       CORE_ADDR prologue_end = gdbarch_skip_prologue (gdbarch, func_addr);
04832 
04833       return func_addr <= pc && pc < prologue_end;
04834     }
04835 
04836   /* We have line number info, and it looks good.  */
04837   return func_addr <= pc && pc < sal.end;
04838 }
04839 
04840 /* Given PC at the function's start address, attempt to find the
04841    prologue end using SAL information.  Return zero if the skip fails.
04842 
04843    A non-optimized prologue traditionally has one SAL for the function
04844    and a second for the function body.  A single line function has
04845    them both pointing at the same line.
04846 
04847    An optimized prologue is similar but the prologue may contain
04848    instructions (SALs) from the instruction body.  Need to skip those
04849    while not getting into the function body.
04850 
04851    The functions end point and an increasing SAL line are used as
04852    indicators of the prologue's endpoint.
04853 
04854    This code is based on the function refine_prologue_limit
04855    (found in ia64).  */
04856 
04857 CORE_ADDR
04858 skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr)
04859 {
04860   struct symtab_and_line prologue_sal;
04861   CORE_ADDR start_pc;
04862   CORE_ADDR end_pc;
04863   struct block *bl;
04864 
04865   /* Get an initial range for the function.  */
04866   find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
04867   start_pc += gdbarch_deprecated_function_start_offset (gdbarch);
04868 
04869   prologue_sal = find_pc_line (start_pc, 0);
04870   if (prologue_sal.line != 0)
04871     {
04872       /* For languages other than assembly, treat two consecutive line
04873          entries at the same address as a zero-instruction prologue.
04874          The GNU assembler emits separate line notes for each instruction
04875          in a multi-instruction macro, but compilers generally will not
04876          do this.  */
04877       if (prologue_sal.symtab->language != language_asm)
04878         {
04879           struct linetable *linetable = LINETABLE (prologue_sal.symtab);
04880           int idx = 0;
04881 
04882           /* Skip any earlier lines, and any end-of-sequence marker
04883              from a previous function.  */
04884           while (linetable->item[idx].pc != prologue_sal.pc
04885                  || linetable->item[idx].line == 0)
04886             idx++;
04887 
04888           if (idx+1 < linetable->nitems
04889               && linetable->item[idx+1].line != 0
04890               && linetable->item[idx+1].pc == start_pc)
04891             return start_pc;
04892         }
04893 
04894       /* If there is only one sal that covers the entire function,
04895          then it is probably a single line function, like
04896          "foo(){}".  */
04897       if (prologue_sal.end >= end_pc)
04898         return 0;
04899 
04900       while (prologue_sal.end < end_pc)
04901         {
04902           struct symtab_and_line sal;
04903 
04904           sal = find_pc_line (prologue_sal.end, 0);
04905           if (sal.line == 0)
04906             break;
04907           /* Assume that a consecutive SAL for the same (or larger)
04908              line mark the prologue -> body transition.  */
04909           if (sal.line >= prologue_sal.line)
04910             break;
04911           /* Likewise if we are in a different symtab altogether
04912              (e.g. within a file included via #include).  */
04913           if (sal.symtab != prologue_sal.symtab)
04914             break;
04915 
04916           /* The line number is smaller.  Check that it's from the
04917              same function, not something inlined.  If it's inlined,
04918              then there is no point comparing the line numbers.  */
04919           bl = block_for_pc (prologue_sal.end);
04920           while (bl)
04921             {
04922               if (block_inlined_p (bl))
04923                 break;
04924               if (BLOCK_FUNCTION (bl))
04925                 {
04926                   bl = NULL;
04927                   break;
04928                 }
04929               bl = BLOCK_SUPERBLOCK (bl);
04930             }
04931           if (bl != NULL)
04932             break;
04933 
04934           /* The case in which compiler's optimizer/scheduler has
04935              moved instructions into the prologue.  We look ahead in
04936              the function looking for address ranges whose
04937              corresponding line number is less the first one that we
04938              found for the function.  This is more conservative then
04939              refine_prologue_limit which scans a large number of SALs
04940              looking for any in the prologue.  */
04941           prologue_sal = sal;
04942         }
04943     }
04944 
04945   if (prologue_sal.end < end_pc)
04946     /* Return the end of this line, or zero if we could not find a
04947        line.  */
04948     return prologue_sal.end;
04949   else
04950     /* Don't return END_PC, which is past the end of the function.  */
04951     return prologue_sal.pc;
04952 }
04953 
04954 /* Track MAIN */
04955 static char *name_of_main;
04956 enum language language_of_main = language_unknown;
04957 
04958 void
04959 set_main_name (const char *name)
04960 {
04961   if (name_of_main != NULL)
04962     {
04963       xfree (name_of_main);
04964       name_of_main = NULL;
04965       language_of_main = language_unknown;
04966     }
04967   if (name != NULL)
04968     {
04969       name_of_main = xstrdup (name);
04970       language_of_main = language_unknown;
04971     }
04972 }
04973 
04974 /* Deduce the name of the main procedure, and set NAME_OF_MAIN
04975    accordingly.  */
04976 
04977 static void
04978 find_main_name (void)
04979 {
04980   const char *new_main_name;
04981 
04982   /* Try to see if the main procedure is in Ada.  */
04983   /* FIXME: brobecker/2005-03-07: Another way of doing this would
04984      be to add a new method in the language vector, and call this
04985      method for each language until one of them returns a non-empty
04986      name.  This would allow us to remove this hard-coded call to
04987      an Ada function.  It is not clear that this is a better approach
04988      at this point, because all methods need to be written in a way
04989      such that false positives never be returned.  For instance, it is
04990      important that a method does not return a wrong name for the main
04991      procedure if the main procedure is actually written in a different
04992      language.  It is easy to guaranty this with Ada, since we use a
04993      special symbol generated only when the main in Ada to find the name
04994      of the main procedure.  It is difficult however to see how this can
04995      be guarantied for languages such as C, for instance.  This suggests
04996      that order of call for these methods becomes important, which means
04997      a more complicated approach.  */
04998   new_main_name = ada_main_name ();
04999   if (new_main_name != NULL)
05000     {
05001       set_main_name (new_main_name);
05002       return;
05003     }
05004 
05005   new_main_name = go_main_name ();
05006   if (new_main_name != NULL)
05007     {
05008       set_main_name (new_main_name);
05009       return;
05010     }
05011 
05012   new_main_name = pascal_main_name ();
05013   if (new_main_name != NULL)
05014     {
05015       set_main_name (new_main_name);
05016       return;
05017     }
05018 
05019   /* The languages above didn't identify the name of the main procedure.
05020      Fallback to "main".  */
05021   set_main_name ("main");
05022 }
05023 
05024 char *
05025 main_name (void)
05026 {
05027   if (name_of_main == NULL)
05028     find_main_name ();
05029 
05030   return name_of_main;
05031 }
05032 
05033 /* Handle ``executable_changed'' events for the symtab module.  */
05034 
05035 static void
05036 symtab_observer_executable_changed (void)
05037 {
05038   /* NAME_OF_MAIN may no longer be the same, so reset it for now.  */
05039   set_main_name (NULL);
05040 }
05041 
05042 /* Return 1 if the supplied producer string matches the ARM RealView
05043    compiler (armcc).  */
05044 
05045 int
05046 producer_is_realview (const char *producer)
05047 {
05048   static const char *const arm_idents[] = {
05049     "ARM C Compiler, ADS",
05050     "Thumb C Compiler, ADS",
05051     "ARM C++ Compiler, ADS",
05052     "Thumb C++ Compiler, ADS",
05053     "ARM/Thumb C/C++ Compiler, RVCT",
05054     "ARM C/C++ Compiler, RVCT"
05055   };
05056   int i;
05057 
05058   if (producer == NULL)
05059     return 0;
05060 
05061   for (i = 0; i < ARRAY_SIZE (arm_idents); i++)
05062     if (strncmp (producer, arm_idents[i], strlen (arm_idents[i])) == 0)
05063       return 1;
05064 
05065   return 0;
05066 }
05067 
05068 
05069 
05070 /* The next index to hand out in response to a registration request.  */
05071 
05072 static int next_aclass_value = LOC_FINAL_VALUE;
05073 
05074 /* The maximum number of "aclass" registrations we support.  This is
05075    constant for convenience.  */
05076 #define MAX_SYMBOL_IMPLS (LOC_FINAL_VALUE + 10)
05077 
05078 /* The objects representing the various "aclass" values.  The elements
05079    from 0 up to LOC_FINAL_VALUE-1 represent themselves, and subsequent
05080    elements are those registered at gdb initialization time.  */
05081 
05082 static struct symbol_impl symbol_impl[MAX_SYMBOL_IMPLS];
05083 
05084 /* The globally visible pointer.  This is separate from 'symbol_impl'
05085    so that it can be const.  */
05086 
05087 const struct symbol_impl *symbol_impls = &symbol_impl[0];
05088 
05089 /* Make sure we saved enough room in struct symbol.  */
05090 
05091 gdb_static_assert (MAX_SYMBOL_IMPLS <= (1 << SYMBOL_ACLASS_BITS));
05092 
05093 /* Register a computed symbol type.  ACLASS must be LOC_COMPUTED.  OPS
05094    is the ops vector associated with this index.  This returns the new
05095    index, which should be used as the aclass_index field for symbols
05096    of this type.  */
05097 
05098 int
05099 register_symbol_computed_impl (enum address_class aclass,
05100                                const struct symbol_computed_ops *ops)
05101 {
05102   int result = next_aclass_value++;
05103 
05104   gdb_assert (aclass == LOC_COMPUTED);
05105   gdb_assert (result < MAX_SYMBOL_IMPLS);
05106   symbol_impl[result].aclass = aclass;
05107   symbol_impl[result].ops_computed = ops;
05108 
05109   /* Sanity check OPS.  */
05110   gdb_assert (ops != NULL);
05111   gdb_assert (ops->tracepoint_var_ref != NULL);
05112   gdb_assert (ops->describe_location != NULL);
05113   gdb_assert (ops->read_needs_frame != NULL);
05114   gdb_assert (ops->read_variable != NULL);
05115 
05116   return result;
05117 }
05118 
05119 /* Register a function with frame base type.  ACLASS must be LOC_BLOCK.
05120    OPS is the ops vector associated with this index.  This returns the
05121    new index, which should be used as the aclass_index field for symbols
05122    of this type.  */
05123 
05124 int
05125 register_symbol_block_impl (enum address_class aclass,
05126                             const struct symbol_block_ops *ops)
05127 {
05128   int result = next_aclass_value++;
05129 
05130   gdb_assert (aclass == LOC_BLOCK);
05131   gdb_assert (result < MAX_SYMBOL_IMPLS);
05132   symbol_impl[result].aclass = aclass;
05133   symbol_impl[result].ops_block = ops;
05134 
05135   /* Sanity check OPS.  */
05136   gdb_assert (ops != NULL);
05137   gdb_assert (ops->find_frame_base_location != NULL);
05138 
05139   return result;
05140 }
05141 
05142 /* Register a register symbol type.  ACLASS must be LOC_REGISTER or
05143    LOC_REGPARM_ADDR.  OPS is the register ops vector associated with
05144    this index.  This returns the new index, which should be used as
05145    the aclass_index field for symbols of this type.  */
05146 
05147 int
05148 register_symbol_register_impl (enum address_class aclass,
05149                                const struct symbol_register_ops *ops)
05150 {
05151   int result = next_aclass_value++;
05152 
05153   gdb_assert (aclass == LOC_REGISTER || aclass == LOC_REGPARM_ADDR);
05154   gdb_assert (result < MAX_SYMBOL_IMPLS);
05155   symbol_impl[result].aclass = aclass;
05156   symbol_impl[result].ops_register = ops;
05157 
05158   return result;
05159 }
05160 
05161 /* Initialize elements of 'symbol_impl' for the constants in enum
05162    address_class.  */
05163 
05164 static void
05165 initialize_ordinary_address_classes (void)
05166 {
05167   int i;
05168 
05169   for (i = 0; i < LOC_FINAL_VALUE; ++i)
05170     symbol_impl[i].aclass = i;
05171 }
05172 
05173 
05174 
05175 /* Initialize the symbol SYM.  */
05176 
05177 void
05178 initialize_symbol (struct symbol *sym)
05179 {
05180   memset (sym, 0, sizeof (*sym));
05181   SYMBOL_SECTION (sym) = -1;
05182 }
05183 
05184 /* Allocate and initialize a new 'struct symbol' on OBJFILE's
05185    obstack.  */
05186 
05187 struct symbol *
05188 allocate_symbol (struct objfile *objfile)
05189 {
05190   struct symbol *result;
05191 
05192   result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
05193   SYMBOL_SECTION (result) = -1;
05194 
05195   return result;
05196 }
05197 
05198 /* Allocate and initialize a new 'struct template_symbol' on OBJFILE's
05199    obstack.  */
05200 
05201 struct template_symbol *
05202 allocate_template_symbol (struct objfile *objfile)
05203 {
05204   struct template_symbol *result;
05205 
05206   result = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct template_symbol);
05207   SYMBOL_SECTION (&result->base) = -1;
05208 
05209   return result;
05210 }
05211 
05212 
05213 
05214 void
05215 _initialize_symtab (void)
05216 {
05217   initialize_ordinary_address_classes ();
05218 
05219   add_info ("variables", variables_info, _("\
05220 All global and static variable names, or those matching REGEXP."));
05221   if (dbx_commands)
05222     add_com ("whereis", class_info, variables_info, _("\
05223 All global and static variable names, or those matching REGEXP."));
05224 
05225   add_info ("functions", functions_info,
05226             _("All function names, or those matching REGEXP."));
05227 
05228   /* FIXME:  This command has at least the following problems:
05229      1.  It prints builtin types (in a very strange and confusing fashion).
05230      2.  It doesn't print right, e.g. with
05231      typedef struct foo *FOO
05232      type_print prints "FOO" when we want to make it (in this situation)
05233      print "struct foo *".
05234      I also think "ptype" or "whatis" is more likely to be useful (but if
05235      there is much disagreement "info types" can be fixed).  */
05236   add_info ("types", types_info,
05237             _("All type names, or those matching REGEXP."));
05238 
05239   add_info ("sources", sources_info,
05240             _("Source files in the program."));
05241 
05242   add_com ("rbreak", class_breakpoint, rbreak_command,
05243            _("Set a breakpoint for all functions matching REGEXP."));
05244 
05245   if (xdb_commands)
05246     {
05247       add_com ("lf", class_info, sources_info,
05248                _("Source files in the program"));
05249       add_com ("lg", class_info, variables_info, _("\
05250 All global and static variable names, or those matching REGEXP."));
05251     }
05252 
05253   add_setshow_enum_cmd ("multiple-symbols", no_class,
05254                         multiple_symbols_modes, &multiple_symbols_mode,
05255                         _("\
05256 Set the debugger behavior when more than one symbol are possible matches\n\
05257 in an expression."), _("\
05258 Show how the debugger handles ambiguities in expressions."), _("\
05259 Valid values are \"ask\", \"all\", \"cancel\", and the default is \"all\"."),
05260                         NULL, NULL, &setlist, &showlist);
05261 
05262   add_setshow_boolean_cmd ("basenames-may-differ", class_obscure,
05263                            &basenames_may_differ, _("\
05264 Set whether a source file may have multiple base names."), _("\
05265 Show whether a source file may have multiple base names."), _("\
05266 (A \"base name\" is the name of a file with the directory part removed.\n\
05267 Example: The base name of \"/home/user/hello.c\" is \"hello.c\".)\n\
05268 If set, GDB will canonicalize file names (e.g., expand symlinks)\n\
05269 before comparing them.  Canonicalization is an expensive operation,\n\
05270 but it allows the same file be known by more than one base name.\n\
05271 If not set (the default), all source files are assumed to have just\n\
05272 one base name, and gdb will do file name comparisons more efficiently."),
05273                            NULL, NULL,
05274                            &setlist, &showlist);
05275 
05276   add_setshow_boolean_cmd ("symtab-create", no_class, &symtab_create_debug,
05277                            _("Set debugging of symbol table creation."),
05278                            _("Show debugging of symbol table creation."), _("\
05279 When enabled, debugging messages are printed when building symbol tables."),
05280                             NULL,
05281                             NULL,
05282                             &setdebuglist, &showdebuglist);
05283 
05284   observer_attach_executable_changed (symtab_observer_executable_changed);
05285 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines