119370Spst/* Symbol table lookup for the GNU debugger, GDB.
219370Spst
398948Sobrien   Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4130809Smarcel   1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5130809Smarcel   Free Software Foundation, Inc.
619370Spst
798948Sobrien   This file is part of GDB.
819370Spst
998948Sobrien   This program is free software; you can redistribute it and/or modify
1098948Sobrien   it under the terms of the GNU General Public License as published by
1198948Sobrien   the Free Software Foundation; either version 2 of the License, or
1298948Sobrien   (at your option) any later version.
1319370Spst
1498948Sobrien   This program is distributed in the hope that it will be useful,
1598948Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1698948Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1798948Sobrien   GNU General Public License for more details.
1819370Spst
1998948Sobrien   You should have received a copy of the GNU General Public License
2098948Sobrien   along with this program; if not, write to the Free Software
2198948Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2298948Sobrien   Boston, MA 02111-1307, USA.  */
2398948Sobrien
2419370Spst#include "defs.h"
2519370Spst#include "symtab.h"
2619370Spst#include "gdbtypes.h"
2719370Spst#include "gdbcore.h"
2819370Spst#include "frame.h"
2919370Spst#include "target.h"
3019370Spst#include "value.h"
3119370Spst#include "symfile.h"
3219370Spst#include "objfiles.h"
3319370Spst#include "gdbcmd.h"
3419370Spst#include "call-cmds.h"
3598948Sobrien#include "gdb_regex.h"
3619370Spst#include "expression.h"
3719370Spst#include "language.h"
3819370Spst#include "demangle.h"
3946283Sdfr#include "inferior.h"
4098948Sobrien#include "linespec.h"
41130809Smarcel#include "source.h"
4298948Sobrien#include "filenames.h"		/* for FILENAME_CMP */
43130809Smarcel#include "objc-lang.h"
4419370Spst
45130809Smarcel#include "hashtab.h"
4619370Spst
47130809Smarcel#include "gdb_obstack.h"
48130809Smarcel#include "block.h"
49130809Smarcel#include "dictionary.h"
50130809Smarcel
5119370Spst#include <sys/types.h>
5219370Spst#include <fcntl.h>
5319370Spst#include "gdb_string.h"
5419370Spst#include "gdb_stat.h"
5519370Spst#include <ctype.h>
5698948Sobrien#include "cp-abi.h"
5719370Spst
5819370Spst/* Prototypes for local functions */
5919370Spst
6098948Sobrienstatic void completion_list_add_name (char *, char *, int, char *, char *);
6119370Spst
6298948Sobrienstatic void rbreak_command (char *, int);
6319370Spst
6498948Sobrienstatic void types_info (char *, int);
6519370Spst
6698948Sobrienstatic void functions_info (char *, int);
6719370Spst
6898948Sobrienstatic void variables_info (char *, int);
6919370Spst
7098948Sobrienstatic void sources_info (char *, int);
7119370Spst
7298948Sobrienstatic void output_source_filename (char *, int *);
7319370Spst
7498948Sobrienstatic int find_line_common (struct linetable *, int, int *);
7519370Spst
7698948Sobrien/* This one is used by linespec.c */
7719370Spst
7898948Sobrienchar *operator_chars (char *p, char **end);
7919370Spst
80130809Smarcelstatic struct symbol *lookup_symbol_aux (const char *name,
81130809Smarcel					 const char *linkage_name,
82130809Smarcel					 const struct block *block,
83130809Smarcel					 const domain_enum domain,
84130809Smarcel					 int *is_a_field_of_this,
85130809Smarcel					 struct symtab **symtab);
8619370Spst
87130809Smarcelstatic
88130809Smarcelstruct symbol *lookup_symbol_aux_local (const char *name,
89130809Smarcel					const char *linkage_name,
90130809Smarcel					const struct block *block,
91130809Smarcel					const domain_enum domain,
92130809Smarcel					struct symtab **symtab);
9319370Spst
94130809Smarcelstatic
95130809Smarcelstruct symbol *lookup_symbol_aux_symtabs (int block_index,
96130809Smarcel					  const char *name,
97130809Smarcel					  const char *linkage_name,
98130809Smarcel					  const domain_enum domain,
99130809Smarcel					  struct symtab **symtab);
10019370Spst
101130809Smarcelstatic
102130809Smarcelstruct symbol *lookup_symbol_aux_psymtabs (int block_index,
103130809Smarcel					   const char *name,
104130809Smarcel					   const char *linkage_name,
105130809Smarcel					   const domain_enum domain,
106130809Smarcel					   struct symtab **symtab);
10719370Spst
108130809Smarcel#if 0
109130809Smarcelstatic
110130809Smarcelstruct symbol *lookup_symbol_aux_minsyms (const char *name,
111130809Smarcel					  const char *linkage_name,
112130809Smarcel					  const domain_enum domain,
113130809Smarcel					  int *is_a_field_of_this,
114130809Smarcel					  struct symtab **symtab);
115130809Smarcel#endif
116130809Smarcel
11746283Sdfr/* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
11846283Sdfr/* Signals the presence of objects compiled by HP compilers */
11946283Sdfrint hp_som_som_object_present = 0;
12046283Sdfr
12198948Sobrienstatic void fixup_section (struct general_symbol_info *, struct objfile *);
12246283Sdfr
12398948Sobrienstatic int file_matches (char *, char **, int);
12446283Sdfr
125130809Smarcelstatic void print_symbol_info (domain_enum,
12698948Sobrien			       struct symtab *, struct symbol *, int, char *);
12746283Sdfr
12898948Sobrienstatic void print_msymbol_info (struct minimal_symbol *);
12946283Sdfr
130130809Smarcelstatic void symtab_symbol_info (char *, domain_enum, int);
13146283Sdfr
13298948Sobrienvoid _initialize_symtab (void);
13398948Sobrien
13419370Spst/* */
13519370Spst
13619370Spst/* The single non-language-specific builtin type */
13719370Spststruct type *builtin_type_error;
13819370Spst
13919370Spst/* Block in which the most recently searched-for symbol was found.
14019370Spst   Might be better to make this a parameter to lookup_symbol and
14119370Spst   value_of_this. */
14219370Spst
14319370Spstconst struct block *block_found;
14419370Spst
14519370Spst/* Check for a symtab of a specific name; first in symtabs, then in
14619370Spst   psymtabs.  *If* there is no '/' in the name, a match after a '/'
14719370Spst   in the symtab filename will also work.  */
14819370Spst
14998948Sobrienstruct symtab *
15098948Sobrienlookup_symtab (const char *name)
15119370Spst{
152130809Smarcel  struct symtab *s;
153130809Smarcel  struct partial_symtab *ps;
154130809Smarcel  struct objfile *objfile;
15598948Sobrien  char *real_path = NULL;
156130809Smarcel  char *full_path = NULL;
15719370Spst
15898948Sobrien  /* Here we are interested in canonicalizing an absolute path, not
15998948Sobrien     absolutizing a relative path.  */
16098948Sobrien  if (IS_ABSOLUTE_PATH (name))
161130809Smarcel    {
162130809Smarcel      full_path = xfullpath (name);
163130809Smarcel      make_cleanup (xfree, full_path);
164130809Smarcel      real_path = gdb_realpath (name);
165130809Smarcel      make_cleanup (xfree, real_path);
166130809Smarcel    }
16719370Spst
16898948Sobriengot_symtab:
16998948Sobrien
17019370Spst  /* First, search for an exact match */
17119370Spst
17219370Spst  ALL_SYMTABS (objfile, s)
17398948Sobrien  {
17498948Sobrien    if (FILENAME_CMP (name, s->filename) == 0)
17598948Sobrien      {
17698948Sobrien	return s;
17798948Sobrien      }
178130809Smarcel
17998948Sobrien    /* If the user gave us an absolute path, try to find the file in
18098948Sobrien       this symtab and use its absolute path.  */
181130809Smarcel
182130809Smarcel    if (full_path != NULL)
183130809Smarcel      {
184130809Smarcel	const char *fp = symtab_to_filename (s);
185130809Smarcel	if (FILENAME_CMP (full_path, fp) == 0)
186130809Smarcel	  {
187130809Smarcel	    return s;
188130809Smarcel	  }
189130809Smarcel      }
190130809Smarcel
19198948Sobrien    if (real_path != NULL)
19298948Sobrien      {
193130809Smarcel	char *rp = gdb_realpath (symtab_to_filename (s));
194130809Smarcel        make_cleanup (xfree, rp);
19598948Sobrien	if (FILENAME_CMP (real_path, rp) == 0)
19698948Sobrien	  {
19798948Sobrien	    return s;
19898948Sobrien	  }
19998948Sobrien      }
20098948Sobrien  }
20119370Spst
20219370Spst  /* Now, search for a matching tail (only if name doesn't have any dirs) */
20319370Spst
20498948Sobrien  if (lbasename (name) == name)
20519370Spst    ALL_SYMTABS (objfile, s)
20698948Sobrien    {
20798948Sobrien      if (FILENAME_CMP (lbasename (s->filename), name) == 0)
20898948Sobrien	return s;
20998948Sobrien    }
21019370Spst
21119370Spst  /* Same search rules as above apply here, but now we look thru the
21219370Spst     psymtabs.  */
21319370Spst
21419370Spst  ps = lookup_partial_symtab (name);
21519370Spst  if (!ps)
21619370Spst    return (NULL);
21719370Spst
21898948Sobrien  if (ps->readin)
21919370Spst    error ("Internal: readin %s pst for `%s' found when no symtab found.",
22098948Sobrien	   ps->filename, name);
22119370Spst
22219370Spst  s = PSYMTAB_TO_SYMTAB (ps);
22319370Spst
22419370Spst  if (s)
22519370Spst    return s;
22619370Spst
22719370Spst  /* At this point, we have located the psymtab for this file, but
22819370Spst     the conversion to a symtab has failed.  This usually happens
22919370Spst     when we are looking up an include file.  In this case,
23019370Spst     PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
23119370Spst     been created.  So, we need to run through the symtabs again in
23219370Spst     order to find the file.
23319370Spst     XXX - This is a crock, and should be fixed inside of the the
23419370Spst     symbol parsing routines. */
23519370Spst  goto got_symtab;
23619370Spst}
23719370Spst
23819370Spst/* Lookup the partial symbol table of a source file named NAME.
23919370Spst   *If* there is no '/' in the name, a match after a '/'
24019370Spst   in the psymtab filename will also work.  */
24119370Spst
24219370Spststruct partial_symtab *
24398948Sobrienlookup_partial_symtab (const char *name)
24419370Spst{
245130809Smarcel  struct partial_symtab *pst;
246130809Smarcel  struct objfile *objfile;
247130809Smarcel  char *full_path = NULL;
24898948Sobrien  char *real_path = NULL;
24998948Sobrien
25098948Sobrien  /* Here we are interested in canonicalizing an absolute path, not
25198948Sobrien     absolutizing a relative path.  */
25298948Sobrien  if (IS_ABSOLUTE_PATH (name))
253130809Smarcel    {
254130809Smarcel      full_path = xfullpath (name);
255130809Smarcel      make_cleanup (xfree, full_path);
256130809Smarcel      real_path = gdb_realpath (name);
257130809Smarcel      make_cleanup (xfree, real_path);
258130809Smarcel    }
25998948Sobrien
26019370Spst  ALL_PSYMTABS (objfile, pst)
26198948Sobrien  {
26298948Sobrien    if (FILENAME_CMP (name, pst->filename) == 0)
26398948Sobrien      {
26498948Sobrien	return (pst);
26598948Sobrien      }
266130809Smarcel
26798948Sobrien    /* If the user gave us an absolute path, try to find the file in
26898948Sobrien       this symtab and use its absolute path.  */
269130809Smarcel    if (full_path != NULL)
27098948Sobrien      {
27198948Sobrien	if (pst->fullname == NULL)
27298948Sobrien	  source_full_path_of (pst->filename, &pst->fullname);
27398948Sobrien	if (pst->fullname != NULL
274130809Smarcel	    && FILENAME_CMP (full_path, pst->fullname) == 0)
27598948Sobrien	  {
27698948Sobrien	    return pst;
27798948Sobrien	  }
27898948Sobrien      }
279130809Smarcel
280130809Smarcel    if (real_path != NULL)
281130809Smarcel      {
282130809Smarcel        char *rp = NULL;
283130809Smarcel	if (pst->fullname == NULL)
284130809Smarcel	  source_full_path_of (pst->filename, &pst->fullname);
285130809Smarcel        if (pst->fullname != NULL)
286130809Smarcel          {
287130809Smarcel            rp = gdb_realpath (pst->fullname);
288130809Smarcel            make_cleanup (xfree, rp);
289130809Smarcel          }
290130809Smarcel	if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
291130809Smarcel	  {
292130809Smarcel	    return pst;
293130809Smarcel	  }
294130809Smarcel      }
29598948Sobrien  }
29619370Spst
29719370Spst  /* Now, search for a matching tail (only if name doesn't have any dirs) */
29819370Spst
29998948Sobrien  if (lbasename (name) == name)
30019370Spst    ALL_PSYMTABS (objfile, pst)
30198948Sobrien    {
30298948Sobrien      if (FILENAME_CMP (lbasename (pst->filename), name) == 0)
30398948Sobrien	return (pst);
30498948Sobrien    }
30519370Spst
30619370Spst  return (NULL);
30719370Spst}
30819370Spst
30946283Sdfr/* Mangle a GDB method stub type.  This actually reassembles the pieces of the
31046283Sdfr   full method name, which consist of the class name (from T), the unadorned
31146283Sdfr   method name from METHOD_ID, and the signature for the specific overload,
31246283Sdfr   specified by SIGNATURE_ID.  Note that this function is g++ specific. */
31319370Spst
31419370Spstchar *
31598948Sobriengdb_mangle_name (struct type *type, int method_id, int signature_id)
31619370Spst{
31719370Spst  int mangled_name_len;
31819370Spst  char *mangled_name;
31946283Sdfr  struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
32046283Sdfr  struct fn_field *method = &f[signature_id];
32146283Sdfr  char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
32246283Sdfr  char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
32319370Spst  char *newname = type_name_no_tag (type);
32419370Spst
32519370Spst  /* Does the form of physname indicate that it is the full mangled name
32619370Spst     of a constructor (not just the args)?  */
32719370Spst  int is_full_physname_constructor;
32819370Spst
32919370Spst  int is_constructor;
33098948Sobrien  int is_destructor = is_destructor_name (physname);
33119370Spst  /* Need a new type prefix.  */
33219370Spst  char *const_prefix = method->is_const ? "C" : "";
33319370Spst  char *volatile_prefix = method->is_volatile ? "V" : "";
33419370Spst  char buf[20];
33519370Spst  int len = (newname == NULL ? 0 : strlen (newname));
33619370Spst
33798948Sobrien  /* Nothing to do if physname already contains a fully mangled v3 abi name
33898948Sobrien     or an operator name.  */
33998948Sobrien  if ((physname[0] == '_' && physname[1] == 'Z')
34098948Sobrien      || is_operator_name (field_name))
34198948Sobrien    return xstrdup (physname);
34219370Spst
34398948Sobrien  is_full_physname_constructor = is_constructor_name (physname);
34498948Sobrien
34519370Spst  is_constructor =
346130809Smarcel    is_full_physname_constructor || (newname && strcmp (field_name, newname) == 0);
34719370Spst
34819370Spst  if (!is_destructor)
34998948Sobrien    is_destructor = (strncmp (physname, "__dt", 4) == 0);
35019370Spst
35119370Spst  if (is_destructor || is_full_physname_constructor)
35219370Spst    {
35398948Sobrien      mangled_name = (char *) xmalloc (strlen (physname) + 1);
35498948Sobrien      strcpy (mangled_name, physname);
35519370Spst      return mangled_name;
35619370Spst    }
35719370Spst
35819370Spst  if (len == 0)
35919370Spst    {
36019370Spst      sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
36119370Spst    }
36246283Sdfr  else if (physname[0] == 't' || physname[0] == 'Q')
36319370Spst    {
36446283Sdfr      /* The physname for template and qualified methods already includes
36598948Sobrien         the class name.  */
36619370Spst      sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
36719370Spst      newname = NULL;
36819370Spst      len = 0;
36919370Spst    }
37019370Spst  else
37119370Spst    {
37219370Spst      sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
37319370Spst    }
37419370Spst  mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
37598948Sobrien		      + strlen (buf) + len + strlen (physname) + 1);
37619370Spst
37719370Spst    {
37898948Sobrien      mangled_name = (char *) xmalloc (mangled_name_len);
37919370Spst      if (is_constructor)
38019370Spst	mangled_name[0] = '\0';
38119370Spst      else
38219370Spst	strcpy (mangled_name, field_name);
38319370Spst    }
38419370Spst  strcat (mangled_name, buf);
38519370Spst  /* If the class doesn't have a name, i.e. newname NULL, then we just
38619370Spst     mangle it using 0 for the length of the class.  Thus it gets mangled
38798948Sobrien     as something starting with `::' rather than `classname::'. */
38819370Spst  if (newname != NULL)
38919370Spst    strcat (mangled_name, newname);
39019370Spst
39119370Spst  strcat (mangled_name, physname);
39219370Spst  return (mangled_name);
39319370Spst}
394130809Smarcel
39519370Spst
396130809Smarcel/* Initialize the language dependent portion of a symbol
397130809Smarcel   depending upon the language for the symbol. */
398130809Smarcelvoid
399130809Smarcelsymbol_init_language_specific (struct general_symbol_info *gsymbol,
400130809Smarcel			       enum language language)
401130809Smarcel{
402130809Smarcel  gsymbol->language = language;
403130809Smarcel  if (gsymbol->language == language_cplus
404130809Smarcel      || gsymbol->language == language_java
405130809Smarcel      || gsymbol->language == language_objc)
406130809Smarcel    {
407130809Smarcel      gsymbol->language_specific.cplus_specific.demangled_name = NULL;
408130809Smarcel    }
409130809Smarcel  else
410130809Smarcel    {
411130809Smarcel      memset (&gsymbol->language_specific, 0,
412130809Smarcel	      sizeof (gsymbol->language_specific));
413130809Smarcel    }
414130809Smarcel}
41519370Spst
416130809Smarcel/* Functions to initialize a symbol's mangled name.  */
41798948Sobrien
418130809Smarcel/* Create the hash table used for demangled names.  Each hash entry is
419130809Smarcel   a pair of strings; one for the mangled name and one for the demangled
420130809Smarcel   name.  The entry is hashed via just the mangled name.  */
42146283Sdfr
422130809Smarcelstatic void
423130809Smarcelcreate_demangled_names_hash (struct objfile *objfile)
424130809Smarcel{
425130809Smarcel  /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
426130809Smarcel     The hash table code will round this up to the next prime number.
427130809Smarcel     Choosing a much larger table size wastes memory, and saves only about
428130809Smarcel     1% in symbol reading.  */
429130809Smarcel
430130809Smarcel  objfile->demangled_names_hash = htab_create_alloc_ex
431130809Smarcel    (256, htab_hash_string, (int (*) (const void *, const void *)) streq,
432130809Smarcel     NULL, objfile->md, xmcalloc, xmfree);
433130809Smarcel}
434130809Smarcel
435130809Smarcel/* Try to determine the demangled name for a symbol, based on the
436130809Smarcel   language of that symbol.  If the language is set to language_auto,
437130809Smarcel   it will attempt to find any demangling algorithm that works and
438130809Smarcel   then set the language appropriately.  The returned name is allocated
439130809Smarcel   by the demangler and should be xfree'd.  */
440130809Smarcel
441130809Smarcelstatic char *
442130809Smarcelsymbol_find_demangled_name (struct general_symbol_info *gsymbol,
443130809Smarcel			    const char *mangled)
444130809Smarcel{
445130809Smarcel  char *demangled = NULL;
446130809Smarcel
447130809Smarcel  if (gsymbol->language == language_unknown)
448130809Smarcel    gsymbol->language = language_auto;
449130809Smarcel
450130809Smarcel  if (gsymbol->language == language_objc
451130809Smarcel      || gsymbol->language == language_auto)
452130809Smarcel    {
453130809Smarcel      demangled =
454130809Smarcel	objc_demangle (mangled, 0);
455130809Smarcel      if (demangled != NULL)
456130809Smarcel	{
457130809Smarcel	  gsymbol->language = language_objc;
458130809Smarcel	  return demangled;
459130809Smarcel	}
460130809Smarcel    }
461130809Smarcel  if (gsymbol->language == language_cplus
462130809Smarcel      || gsymbol->language == language_auto)
463130809Smarcel    {
464130809Smarcel      demangled =
465130809Smarcel        cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
466130809Smarcel      if (demangled != NULL)
467130809Smarcel	{
468130809Smarcel	  gsymbol->language = language_cplus;
469130809Smarcel	  return demangled;
470130809Smarcel	}
471130809Smarcel    }
472130809Smarcel  if (gsymbol->language == language_java)
473130809Smarcel    {
474130809Smarcel      demangled =
475130809Smarcel        cplus_demangle (mangled,
476130809Smarcel                        DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
477130809Smarcel      if (demangled != NULL)
478130809Smarcel	{
479130809Smarcel	  gsymbol->language = language_java;
480130809Smarcel	  return demangled;
481130809Smarcel	}
482130809Smarcel    }
483130809Smarcel  return NULL;
484130809Smarcel}
485130809Smarcel
486130809Smarcel/* Set both the mangled and demangled (if any) names for GSYMBOL based
487130809Smarcel   on LINKAGE_NAME and LEN.  The hash table corresponding to OBJFILE
488130809Smarcel   is used, and the memory comes from that objfile's objfile_obstack.
489130809Smarcel   LINKAGE_NAME is copied, so the pointer can be discarded after
490130809Smarcel   calling this function.  */
491130809Smarcel
492130809Smarcel/* We have to be careful when dealing with Java names: when we run
493130809Smarcel   into a Java minimal symbol, we don't know it's a Java symbol, so it
494130809Smarcel   gets demangled as a C++ name.  This is unfortunate, but there's not
495130809Smarcel   much we can do about it: but when demangling partial symbols and
496130809Smarcel   regular symbols, we'd better not reuse the wrong demangled name.
497130809Smarcel   (See PR gdb/1039.)  We solve this by putting a distinctive prefix
498130809Smarcel   on Java names when storing them in the hash table.  */
499130809Smarcel
500130809Smarcel/* FIXME: carlton/2003-03-13: This is an unfortunate situation.  I
501130809Smarcel   don't mind the Java prefix so much: different languages have
502130809Smarcel   different demangling requirements, so it's only natural that we
503130809Smarcel   need to keep language data around in our demangling cache.  But
504130809Smarcel   it's not good that the minimal symbol has the wrong demangled name.
505130809Smarcel   Unfortunately, I can't think of any easy solution to that
506130809Smarcel   problem.  */
507130809Smarcel
508130809Smarcel#define JAVA_PREFIX "##JAVA$$"
509130809Smarcel#define JAVA_PREFIX_LEN 8
510130809Smarcel
511130809Smarcelvoid
512130809Smarcelsymbol_set_names (struct general_symbol_info *gsymbol,
513130809Smarcel		  const char *linkage_name, int len, struct objfile *objfile)
514130809Smarcel{
515130809Smarcel  char **slot;
516130809Smarcel  /* A 0-terminated copy of the linkage name.  */
517130809Smarcel  const char *linkage_name_copy;
518130809Smarcel  /* A copy of the linkage name that might have a special Java prefix
519130809Smarcel     added to it, for use when looking names up in the hash table.  */
520130809Smarcel  const char *lookup_name;
521130809Smarcel  /* The length of lookup_name.  */
522130809Smarcel  int lookup_len;
523130809Smarcel
524130809Smarcel  if (objfile->demangled_names_hash == NULL)
525130809Smarcel    create_demangled_names_hash (objfile);
526130809Smarcel
527130809Smarcel  /* The stabs reader generally provides names that are not
528130809Smarcel     NUL-terminated; most of the other readers don't do this, so we
529130809Smarcel     can just use the given copy, unless we're in the Java case.  */
530130809Smarcel  if (gsymbol->language == language_java)
531130809Smarcel    {
532130809Smarcel      char *alloc_name;
533130809Smarcel      lookup_len = len + JAVA_PREFIX_LEN;
534130809Smarcel
535130809Smarcel      alloc_name = alloca (lookup_len + 1);
536130809Smarcel      memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
537130809Smarcel      memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
538130809Smarcel      alloc_name[lookup_len] = '\0';
539130809Smarcel
540130809Smarcel      lookup_name = alloc_name;
541130809Smarcel      linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
542130809Smarcel    }
543130809Smarcel  else if (linkage_name[len] != '\0')
544130809Smarcel    {
545130809Smarcel      char *alloc_name;
546130809Smarcel      lookup_len = len;
547130809Smarcel
548130809Smarcel      alloc_name = alloca (lookup_len + 1);
549130809Smarcel      memcpy (alloc_name, linkage_name, len);
550130809Smarcel      alloc_name[lookup_len] = '\0';
551130809Smarcel
552130809Smarcel      lookup_name = alloc_name;
553130809Smarcel      linkage_name_copy = alloc_name;
554130809Smarcel    }
555130809Smarcel  else
556130809Smarcel    {
557130809Smarcel      lookup_len = len;
558130809Smarcel      lookup_name = linkage_name;
559130809Smarcel      linkage_name_copy = linkage_name;
560130809Smarcel    }
561130809Smarcel
562130809Smarcel  slot = (char **) htab_find_slot (objfile->demangled_names_hash,
563130809Smarcel				   lookup_name, INSERT);
564130809Smarcel
565130809Smarcel  /* If this name is not in the hash table, add it.  */
566130809Smarcel  if (*slot == NULL)
567130809Smarcel    {
568130809Smarcel      char *demangled_name = symbol_find_demangled_name (gsymbol,
569130809Smarcel							 linkage_name_copy);
570130809Smarcel      int demangled_len = demangled_name ? strlen (demangled_name) : 0;
571130809Smarcel
572130809Smarcel      /* If there is a demangled name, place it right after the mangled name.
573130809Smarcel	 Otherwise, just place a second zero byte after the end of the mangled
574130809Smarcel	 name.  */
575130809Smarcel      *slot = obstack_alloc (&objfile->objfile_obstack,
576130809Smarcel			     lookup_len + demangled_len + 2);
577130809Smarcel      memcpy (*slot, lookup_name, lookup_len + 1);
578130809Smarcel      if (demangled_name != NULL)
579130809Smarcel	{
580130809Smarcel	  memcpy (*slot + lookup_len + 1, demangled_name, demangled_len + 1);
581130809Smarcel	  xfree (demangled_name);
582130809Smarcel	}
583130809Smarcel      else
584130809Smarcel	(*slot)[lookup_len + 1] = '\0';
585130809Smarcel    }
586130809Smarcel
587130809Smarcel  gsymbol->name = *slot + lookup_len - len;
588130809Smarcel  if ((*slot)[lookup_len + 1] != '\0')
589130809Smarcel    gsymbol->language_specific.cplus_specific.demangled_name
590130809Smarcel      = &(*slot)[lookup_len + 1];
591130809Smarcel  else
592130809Smarcel    gsymbol->language_specific.cplus_specific.demangled_name = NULL;
593130809Smarcel}
594130809Smarcel
595130809Smarcel/* Initialize the demangled name of GSYMBOL if possible.  Any required space
596130809Smarcel   to store the name is obtained from the specified obstack.  The function
597130809Smarcel   symbol_set_names, above, should be used instead where possible for more
598130809Smarcel   efficient memory usage.  */
599130809Smarcel
600130809Smarcelvoid
601130809Smarcelsymbol_init_demangled_name (struct general_symbol_info *gsymbol,
602130809Smarcel                            struct obstack *obstack)
603130809Smarcel{
604130809Smarcel  char *mangled = gsymbol->name;
605130809Smarcel  char *demangled = NULL;
606130809Smarcel
607130809Smarcel  demangled = symbol_find_demangled_name (gsymbol, mangled);
608130809Smarcel  if (gsymbol->language == language_cplus
609130809Smarcel      || gsymbol->language == language_java
610130809Smarcel      || gsymbol->language == language_objc)
611130809Smarcel    {
612130809Smarcel      if (demangled)
613130809Smarcel	{
614130809Smarcel	  gsymbol->language_specific.cplus_specific.demangled_name
615130809Smarcel	    = obsavestring (demangled, strlen (demangled), obstack);
616130809Smarcel	  xfree (demangled);
617130809Smarcel	}
618130809Smarcel      else
619130809Smarcel	gsymbol->language_specific.cplus_specific.demangled_name = NULL;
620130809Smarcel    }
621130809Smarcel  else
622130809Smarcel    {
623130809Smarcel      /* Unknown language; just clean up quietly.  */
624130809Smarcel      if (demangled)
625130809Smarcel	xfree (demangled);
626130809Smarcel    }
627130809Smarcel}
628130809Smarcel
629130809Smarcel/* Return the source code name of a symbol.  In languages where
630130809Smarcel   demangling is necessary, this is the demangled name.  */
631130809Smarcel
632130809Smarcelchar *
633130809Smarcelsymbol_natural_name (const struct general_symbol_info *gsymbol)
634130809Smarcel{
635130809Smarcel  if ((gsymbol->language == language_cplus
636130809Smarcel       || gsymbol->language == language_java
637130809Smarcel       || gsymbol->language == language_objc)
638130809Smarcel      && (gsymbol->language_specific.cplus_specific.demangled_name != NULL))
639130809Smarcel    {
640130809Smarcel      return gsymbol->language_specific.cplus_specific.demangled_name;
641130809Smarcel    }
642130809Smarcel  else
643130809Smarcel    {
644130809Smarcel      return gsymbol->name;
645130809Smarcel    }
646130809Smarcel}
647130809Smarcel
648130809Smarcel/* Return the demangled name for a symbol based on the language for
649130809Smarcel   that symbol.  If no demangled name exists, return NULL. */
650130809Smarcelchar *
651130809Smarcelsymbol_demangled_name (struct general_symbol_info *gsymbol)
652130809Smarcel{
653130809Smarcel  if (gsymbol->language == language_cplus
654130809Smarcel      || gsymbol->language == language_java
655130809Smarcel      || gsymbol->language == language_objc)
656130809Smarcel    return gsymbol->language_specific.cplus_specific.demangled_name;
657130809Smarcel
658130809Smarcel  else
659130809Smarcel    return NULL;
660130809Smarcel}
661130809Smarcel
662130809Smarcel/* Initialize the structure fields to zero values.  */
663130809Smarcelvoid
664130809Smarcelinit_sal (struct symtab_and_line *sal)
665130809Smarcel{
666130809Smarcel  sal->symtab = 0;
667130809Smarcel  sal->section = 0;
668130809Smarcel  sal->line = 0;
669130809Smarcel  sal->pc = 0;
670130809Smarcel  sal->end = 0;
671130809Smarcel}
672130809Smarcel
673130809Smarcel
674130809Smarcel
675130809Smarcel/* Find which partial symtab contains PC and SECTION.  Return 0 if
676130809Smarcel   none.  We return the psymtab that contains a symbol whose address
677130809Smarcel   exactly matches PC, or, if we cannot find an exact match, the
678130809Smarcel   psymtab that contains a symbol whose address is closest to PC.  */
67919370Spststruct partial_symtab *
68098948Sobrienfind_pc_sect_psymtab (CORE_ADDR pc, asection *section)
68119370Spst{
682130809Smarcel  struct partial_symtab *pst;
683130809Smarcel  struct objfile *objfile;
68498948Sobrien  struct minimal_symbol *msymbol;
68519370Spst
68698948Sobrien  /* If we know that this is not a text address, return failure.  This is
68798948Sobrien     necessary because we loop based on texthigh and textlow, which do
68898948Sobrien     not include the data ranges.  */
68998948Sobrien  msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
69098948Sobrien  if (msymbol
69198948Sobrien      && (msymbol->type == mst_data
69298948Sobrien	  || msymbol->type == mst_bss
69398948Sobrien	  || msymbol->type == mst_abs
69498948Sobrien	  || msymbol->type == mst_file_data
69598948Sobrien	  || msymbol->type == mst_file_bss))
69698948Sobrien    return NULL;
69798948Sobrien
69819370Spst  ALL_PSYMTABS (objfile, pst)
69998948Sobrien  {
70098948Sobrien    if (pc >= pst->textlow && pc < pst->texthigh)
70198948Sobrien      {
70298948Sobrien	struct partial_symtab *tpst;
703130809Smarcel	struct partial_symtab *best_pst = pst;
704130809Smarcel	struct partial_symbol *best_psym = NULL;
70519370Spst
70698948Sobrien	/* An objfile that has its functions reordered might have
70798948Sobrien	   many partial symbol tables containing the PC, but
70898948Sobrien	   we want the partial symbol table that contains the
70998948Sobrien	   function containing the PC.  */
71098948Sobrien	if (!(objfile->flags & OBJF_REORDERED) &&
71198948Sobrien	    section == 0)	/* can't validate section this way */
71298948Sobrien	  return (pst);
71319370Spst
71498948Sobrien	if (msymbol == NULL)
71598948Sobrien	  return (pst);
71619370Spst
717130809Smarcel	/* The code range of partial symtabs sometimes overlap, so, in
718130809Smarcel	   the loop below, we need to check all partial symtabs and
719130809Smarcel	   find the one that fits better for the given PC address. We
720130809Smarcel	   select the partial symtab that contains a symbol whose
721130809Smarcel	   address is closest to the PC address.  By closest we mean
722130809Smarcel	   that find_pc_sect_symbol returns the symbol with address
723130809Smarcel	   that is closest and still less than the given PC.  */
72498948Sobrien	for (tpst = pst; tpst != NULL; tpst = tpst->next)
72598948Sobrien	  {
72698948Sobrien	    if (pc >= tpst->textlow && pc < tpst->texthigh)
72798948Sobrien	      {
72898948Sobrien		struct partial_symbol *p;
72919370Spst
73098948Sobrien		p = find_pc_sect_psymbol (tpst, pc, section);
73198948Sobrien		if (p != NULL
73298948Sobrien		    && SYMBOL_VALUE_ADDRESS (p)
73398948Sobrien		    == SYMBOL_VALUE_ADDRESS (msymbol))
73498948Sobrien		  return (tpst);
735130809Smarcel		if (p != NULL)
736130809Smarcel		  {
737130809Smarcel		    /* We found a symbol in this partial symtab which
738130809Smarcel		       matches (or is closest to) PC, check whether it
739130809Smarcel		       is closer than our current BEST_PSYM.  Since
740130809Smarcel		       this symbol address is necessarily lower or
741130809Smarcel		       equal to PC, the symbol closer to PC is the
742130809Smarcel		       symbol which address is the highest.  */
743130809Smarcel		    /* This way we return the psymtab which contains
744130809Smarcel		       such best match symbol. This can help in cases
745130809Smarcel		       where the symbol information/debuginfo is not
746130809Smarcel		       complete, like for instance on IRIX6 with gcc,
747130809Smarcel		       where no debug info is emitted for
748130809Smarcel		       statics. (See also the nodebug.exp
749130809Smarcel		       testcase.)  */
750130809Smarcel		    if (best_psym == NULL
751130809Smarcel			|| SYMBOL_VALUE_ADDRESS (p)
752130809Smarcel			> SYMBOL_VALUE_ADDRESS (best_psym))
753130809Smarcel		      {
754130809Smarcel			best_psym = p;
755130809Smarcel			best_pst = tpst;
756130809Smarcel		      }
757130809Smarcel		  }
758130809Smarcel
75998948Sobrien	      }
76098948Sobrien	  }
761130809Smarcel	return (best_pst);
76298948Sobrien      }
76398948Sobrien  }
76419370Spst  return (NULL);
76519370Spst}
76619370Spst
76746283Sdfr/* Find which partial symtab contains PC.  Return 0 if none.
76846283Sdfr   Backward compatibility, no section */
76946283Sdfr
77046283Sdfrstruct partial_symtab *
77198948Sobrienfind_pc_psymtab (CORE_ADDR pc)
77246283Sdfr{
77346283Sdfr  return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
77446283Sdfr}
77546283Sdfr
77646283Sdfr/* Find which partial symbol within a psymtab matches PC and SECTION.
77746283Sdfr   Return 0 if none.  Check all psymtabs if PSYMTAB is 0.  */
77846283Sdfr
77919370Spststruct partial_symbol *
78098948Sobrienfind_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
78198948Sobrien		      asection *section)
78219370Spst{
78319370Spst  struct partial_symbol *best = NULL, *p, **pp;
78419370Spst  CORE_ADDR best_pc;
78598948Sobrien
78619370Spst  if (!psymtab)
78746283Sdfr    psymtab = find_pc_sect_psymtab (pc, section);
78819370Spst  if (!psymtab)
78919370Spst    return 0;
79019370Spst
79146283Sdfr  /* Cope with programs that start at address 0 */
79246283Sdfr  best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
79319370Spst
79419370Spst  /* Search the global symbols as well as the static symbols, so that
79519370Spst     find_pc_partial_function doesn't use a minimal symbol and thus
79619370Spst     cache a bad endaddr.  */
79719370Spst  for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
79898948Sobrien    (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
79998948Sobrien     < psymtab->n_global_syms);
80019370Spst       pp++)
80119370Spst    {
80219370Spst      p = *pp;
803130809Smarcel      if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
80419370Spst	  && SYMBOL_CLASS (p) == LOC_BLOCK
80519370Spst	  && pc >= SYMBOL_VALUE_ADDRESS (p)
80646283Sdfr	  && (SYMBOL_VALUE_ADDRESS (p) > best_pc
80746283Sdfr	      || (psymtab->textlow == 0
80846283Sdfr		  && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
80919370Spst	{
81098948Sobrien	  if (section)		/* match on a specific section */
81146283Sdfr	    {
81246283Sdfr	      fixup_psymbol_section (p, psymtab->objfile);
81346283Sdfr	      if (SYMBOL_BFD_SECTION (p) != section)
81446283Sdfr		continue;
81546283Sdfr	    }
81619370Spst	  best_pc = SYMBOL_VALUE_ADDRESS (p);
81719370Spst	  best = p;
81819370Spst	}
81919370Spst    }
82046283Sdfr
82119370Spst  for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
82298948Sobrien    (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
82398948Sobrien     < psymtab->n_static_syms);
82419370Spst       pp++)
82519370Spst    {
82619370Spst      p = *pp;
827130809Smarcel      if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
82819370Spst	  && SYMBOL_CLASS (p) == LOC_BLOCK
82919370Spst	  && pc >= SYMBOL_VALUE_ADDRESS (p)
83046283Sdfr	  && (SYMBOL_VALUE_ADDRESS (p) > best_pc
83198948Sobrien	      || (psymtab->textlow == 0
83246283Sdfr		  && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
83319370Spst	{
83498948Sobrien	  if (section)		/* match on a specific section */
83546283Sdfr	    {
83646283Sdfr	      fixup_psymbol_section (p, psymtab->objfile);
83746283Sdfr	      if (SYMBOL_BFD_SECTION (p) != section)
83846283Sdfr		continue;
83946283Sdfr	    }
84019370Spst	  best_pc = SYMBOL_VALUE_ADDRESS (p);
84119370Spst	  best = p;
84219370Spst	}
84319370Spst    }
84446283Sdfr
84519370Spst  return best;
84619370Spst}
84719370Spst
84846283Sdfr/* Find which partial symbol within a psymtab matches PC.  Return 0 if none.
84946283Sdfr   Check all psymtabs if PSYMTAB is 0.  Backwards compatibility, no section. */
85046283Sdfr
85146283Sdfrstruct partial_symbol *
85298948Sobrienfind_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
85346283Sdfr{
85446283Sdfr  return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
85546283Sdfr}
85619370Spst
85746283Sdfr/* Debug symbols usually don't have section information.  We need to dig that
85846283Sdfr   out of the minimal symbols and stash that in the debug symbol.  */
85946283Sdfr
86046283Sdfrstatic void
86198948Sobrienfixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
86246283Sdfr{
86346283Sdfr  struct minimal_symbol *msym;
86446283Sdfr  msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
86546283Sdfr
86646283Sdfr  if (msym)
86798948Sobrien    {
86898948Sobrien      ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
86998948Sobrien      ginfo->section = SYMBOL_SECTION (msym);
87098948Sobrien    }
87146283Sdfr}
87246283Sdfr
87346283Sdfrstruct symbol *
87498948Sobrienfixup_symbol_section (struct symbol *sym, struct objfile *objfile)
87546283Sdfr{
87646283Sdfr  if (!sym)
87746283Sdfr    return NULL;
87846283Sdfr
87946283Sdfr  if (SYMBOL_BFD_SECTION (sym))
88046283Sdfr    return sym;
88146283Sdfr
88246283Sdfr  fixup_section (&sym->ginfo, objfile);
88346283Sdfr
88446283Sdfr  return sym;
88546283Sdfr}
88646283Sdfr
88798948Sobrienstruct partial_symbol *
88898948Sobrienfixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
88946283Sdfr{
89046283Sdfr  if (!psym)
89146283Sdfr    return NULL;
89246283Sdfr
89346283Sdfr  if (SYMBOL_BFD_SECTION (psym))
89446283Sdfr    return psym;
89546283Sdfr
89646283Sdfr  fixup_section (&psym->ginfo, objfile);
89746283Sdfr
89846283Sdfr  return psym;
89946283Sdfr}
90046283Sdfr
90119370Spst/* Find the definition for a specified symbol name NAME
902130809Smarcel   in domain DOMAIN, visible from lexical block BLOCK.
90319370Spst   Returns the struct symbol pointer, or zero if no symbol is found.
90419370Spst   If SYMTAB is non-NULL, store the symbol table in which the
90519370Spst   symbol was found there, or NULL if not found.
90619370Spst   C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
90719370Spst   NAME is a field of the current implied argument `this'.  If so set
90819370Spst   *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
90919370Spst   BLOCK_FOUND is set to the block in which NAME is found (in the case of
91019370Spst   a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
91119370Spst
91219370Spst/* This function has a bunch of loops in it and it would seem to be
91319370Spst   attractive to put in some QUIT's (though I'm not really sure
91419370Spst   whether it can run long enough to be really important).  But there
91519370Spst   are a few calls for which it would appear to be bad news to quit
916130809Smarcel   out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c.  (Note
917130809Smarcel   that there is C++ code below which can error(), but that probably
918130809Smarcel   doesn't affect these calls since they are looking for a known
919130809Smarcel   variable and thus can probably assume it will never hit the C++
920130809Smarcel   code).  */
92119370Spst
92219370Spststruct symbol *
92398948Sobrienlookup_symbol (const char *name, const struct block *block,
924130809Smarcel	       const domain_enum domain, int *is_a_field_of_this,
92598948Sobrien	       struct symtab **symtab)
92619370Spst{
927130809Smarcel  char *demangled_name = NULL;
928130809Smarcel  const char *modified_name = NULL;
929130809Smarcel  const char *mangled_name = NULL;
93098948Sobrien  int needtofreename = 0;
93198948Sobrien  struct symbol *returnval;
93298948Sobrien
933130809Smarcel  modified_name = name;
934130809Smarcel
935130809Smarcel  /* If we are using C++ language, demangle the name before doing a lookup, so
936130809Smarcel     we can always binary search. */
937130809Smarcel  if (current_language->la_language == language_cplus)
938130809Smarcel    {
939130809Smarcel      demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
940130809Smarcel      if (demangled_name)
941130809Smarcel	{
942130809Smarcel	  mangled_name = name;
943130809Smarcel	  modified_name = demangled_name;
944130809Smarcel	  needtofreename = 1;
945130809Smarcel	}
946130809Smarcel    }
947130809Smarcel
94898948Sobrien  if (case_sensitivity == case_sensitive_off)
94998948Sobrien    {
95098948Sobrien      char *copy;
95198948Sobrien      int len, i;
95298948Sobrien
95398948Sobrien      len = strlen (name);
95498948Sobrien      copy = (char *) alloca (len + 1);
95598948Sobrien      for (i= 0; i < len; i++)
95698948Sobrien        copy[i] = tolower (name[i]);
95798948Sobrien      copy[len] = 0;
95898948Sobrien      modified_name = copy;
95998948Sobrien    }
96098948Sobrien
961130809Smarcel  returnval = lookup_symbol_aux (modified_name, mangled_name, block,
962130809Smarcel				 domain, is_a_field_of_this, symtab);
96398948Sobrien  if (needtofreename)
964130809Smarcel    xfree (demangled_name);
96598948Sobrien
96698948Sobrien  return returnval;
96798948Sobrien}
96898948Sobrien
969130809Smarcel/* Behave like lookup_symbol_aux except that NAME is the natural name
970130809Smarcel   of the symbol that we're looking for and, if LINKAGE_NAME is
971130809Smarcel   non-NULL, ensure that the symbol's linkage name matches as
972130809Smarcel   well.  */
973130809Smarcel
97498948Sobrienstatic struct symbol *
975130809Smarcellookup_symbol_aux (const char *name, const char *linkage_name,
976130809Smarcel		   const struct block *block, const domain_enum domain,
977130809Smarcel		   int *is_a_field_of_this, struct symtab **symtab)
97898948Sobrien{
979130809Smarcel  struct symbol *sym;
98019370Spst
981130809Smarcel  /* Make sure we do something sensible with is_a_field_of_this, since
982130809Smarcel     the callers that set this parameter to some non-null value will
983130809Smarcel     certainly use it later and expect it to be either 0 or 1.
984130809Smarcel     If we don't set it, the contents of is_a_field_of_this are
985130809Smarcel     undefined.  */
986130809Smarcel  if (is_a_field_of_this != NULL)
987130809Smarcel    *is_a_field_of_this = 0;
98898948Sobrien
989130809Smarcel  /* Search specified block and its superiors.  Don't search
990130809Smarcel     STATIC_BLOCK or GLOBAL_BLOCK.  */
99119370Spst
992130809Smarcel  sym = lookup_symbol_aux_local (name, linkage_name, block, domain,
993130809Smarcel				 symtab);
994130809Smarcel  if (sym != NULL)
995130809Smarcel    return sym;
99619370Spst
997130809Smarcel  /* If requested to do so by the caller and if appropriate for the
998130809Smarcel     current language, check to see if NAME is a field of `this'. */
99919370Spst
1000130809Smarcel  if (current_language->la_value_of_this != NULL
1001130809Smarcel      && is_a_field_of_this != NULL)
100219370Spst    {
1003130809Smarcel      struct value *v = current_language->la_value_of_this (0);
100419370Spst
100519370Spst      if (v && check_field (v, name))
100619370Spst	{
100719370Spst	  *is_a_field_of_this = 1;
100819370Spst	  if (symtab != NULL)
100919370Spst	    *symtab = NULL;
101046283Sdfr	  return NULL;
101119370Spst	}
101219370Spst    }
101319370Spst
1014130809Smarcel  /* Now do whatever is appropriate for the current language to look
1015130809Smarcel     up static and global variables.  */
1016130809Smarcel
1017130809Smarcel  sym = current_language->la_lookup_symbol_nonlocal (name, linkage_name,
1018130809Smarcel						     block, domain,
1019130809Smarcel						     symtab);
1020130809Smarcel  if (sym != NULL)
1021130809Smarcel    return sym;
1022130809Smarcel
1023130809Smarcel  /* Now search all static file-level symbols.  Not strictly correct,
1024130809Smarcel     but more useful than an error.  Do the symtabs first, then check
1025130809Smarcel     the psymtabs.  If a psymtab indicates the existence of the
1026130809Smarcel     desired name as a file-level static, then do psymtab-to-symtab
102746283Sdfr     conversion on the fly and return the found symbol. */
102898948Sobrien
1029130809Smarcel  sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, linkage_name,
1030130809Smarcel				   domain, symtab);
1031130809Smarcel  if (sym != NULL)
1032130809Smarcel    return sym;
1033130809Smarcel
1034130809Smarcel  sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, linkage_name,
1035130809Smarcel				    domain, symtab);
1036130809Smarcel  if (sym != NULL)
1037130809Smarcel    return sym;
103819370Spst
1039130809Smarcel  if (symtab != NULL)
1040130809Smarcel    *symtab = NULL;
1041130809Smarcel  return NULL;
1042130809Smarcel}
104346283Sdfr
1044130809Smarcel/* Check to see if the symbol is defined in BLOCK or its superiors.
1045130809Smarcel   Don't search STATIC_BLOCK or GLOBAL_BLOCK.  */
104698948Sobrien
1047130809Smarcelstatic struct symbol *
1048130809Smarcellookup_symbol_aux_local (const char *name, const char *linkage_name,
1049130809Smarcel			 const struct block *block,
1050130809Smarcel			 const domain_enum domain,
1051130809Smarcel			 struct symtab **symtab)
1052130809Smarcel{
1053130809Smarcel  struct symbol *sym;
1054130809Smarcel  const struct block *static_block = block_static_block (block);
1055130809Smarcel
1056130809Smarcel  /* Check if either no block is specified or it's a global block.  */
1057130809Smarcel
1058130809Smarcel  if (static_block == NULL)
1059130809Smarcel    return NULL;
1060130809Smarcel
1061130809Smarcel  while (block != static_block)
106219370Spst    {
1063130809Smarcel      sym = lookup_symbol_aux_block (name, linkage_name, block, domain,
1064130809Smarcel				     symtab);
1065130809Smarcel      if (sym != NULL)
1066130809Smarcel	return sym;
1067130809Smarcel      block = BLOCK_SUPERBLOCK (block);
1068130809Smarcel    }
106919370Spst
1070130809Smarcel  /* We've reached the static block without finding a result.  */
107119370Spst
1072130809Smarcel  return NULL;
1073130809Smarcel}
107419370Spst
1075130809Smarcel/* Look up a symbol in a block; if found, locate its symtab, fixup the
1076130809Smarcel   symbol, and set block_found appropriately.  */
107719370Spst
1078130809Smarcelstruct symbol *
1079130809Smarcellookup_symbol_aux_block (const char *name, const char *linkage_name,
1080130809Smarcel			 const struct block *block,
1081130809Smarcel			 const domain_enum domain,
1082130809Smarcel			 struct symtab **symtab)
1083130809Smarcel{
1084130809Smarcel  struct symbol *sym;
1085130809Smarcel  struct objfile *objfile = NULL;
1086130809Smarcel  struct blockvector *bv;
1087130809Smarcel  struct block *b;
1088130809Smarcel  struct symtab *s = NULL;
1089130809Smarcel
1090130809Smarcel  sym = lookup_block_symbol (block, name, linkage_name, domain);
1091130809Smarcel  if (sym)
1092130809Smarcel    {
1093130809Smarcel      block_found = block;
1094130809Smarcel      if (symtab != NULL)
1095130809Smarcel	{
1096130809Smarcel	  /* Search the list of symtabs for one which contains the
1097130809Smarcel	     address of the start of this block.  */
1098130809Smarcel	  ALL_SYMTABS (objfile, s)
109919370Spst	    {
1100130809Smarcel	      bv = BLOCKVECTOR (s);
1101130809Smarcel	      b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1102130809Smarcel	      if (BLOCK_START (b) <= BLOCK_START (block)
1103130809Smarcel		  && BLOCK_END (b) > BLOCK_START (block))
1104130809Smarcel		goto found;
110519370Spst	    }
1106130809Smarcel	found:
1107130809Smarcel	  *symtab = s;
110819370Spst	}
1109130809Smarcel
1110130809Smarcel      return fixup_symbol_section (sym, objfile);
111119370Spst    }
111298948Sobrien
1113130809Smarcel  return NULL;
1114130809Smarcel}
111546283Sdfr
1116130809Smarcel/* Check to see if the symbol is defined in one of the symtabs.
1117130809Smarcel   BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1118130809Smarcel   depending on whether or not we want to search global symbols or
1119130809Smarcel   static symbols.  */
112019370Spst
1121130809Smarcelstatic struct symbol *
1122130809Smarcellookup_symbol_aux_symtabs (int block_index,
1123130809Smarcel			   const char *name, const char *linkage_name,
1124130809Smarcel			   const domain_enum domain,
1125130809Smarcel			   struct symtab **symtab)
1126130809Smarcel{
1127130809Smarcel  struct symbol *sym;
1128130809Smarcel  struct objfile *objfile;
1129130809Smarcel  struct blockvector *bv;
1130130809Smarcel  const struct block *block;
1131130809Smarcel  struct symtab *s;
113219370Spst
113319370Spst  ALL_SYMTABS (objfile, s)
113498948Sobrien  {
113598948Sobrien    bv = BLOCKVECTOR (s);
1136130809Smarcel    block = BLOCKVECTOR_BLOCK (bv, block_index);
1137130809Smarcel    sym = lookup_block_symbol (block, name, linkage_name, domain);
113898948Sobrien    if (sym)
113998948Sobrien      {
114098948Sobrien	block_found = block;
114198948Sobrien	if (symtab != NULL)
114298948Sobrien	  *symtab = s;
114398948Sobrien	return fixup_symbol_section (sym, objfile);
114498948Sobrien      }
114598948Sobrien  }
114619370Spst
1147130809Smarcel  return NULL;
1148130809Smarcel}
1149130809Smarcel
1150130809Smarcel/* Check to see if the symbol is defined in one of the partial
1151130809Smarcel   symtabs.  BLOCK_INDEX should be either GLOBAL_BLOCK or
1152130809Smarcel   STATIC_BLOCK, depending on whether or not we want to search global
1153130809Smarcel   symbols or static symbols.  */
1154130809Smarcel
1155130809Smarcelstatic struct symbol *
1156130809Smarcellookup_symbol_aux_psymtabs (int block_index, const char *name,
1157130809Smarcel			    const char *linkage_name,
1158130809Smarcel			    const domain_enum domain,
1159130809Smarcel			    struct symtab **symtab)
1160130809Smarcel{
1161130809Smarcel  struct symbol *sym;
1162130809Smarcel  struct objfile *objfile;
1163130809Smarcel  struct blockvector *bv;
1164130809Smarcel  const struct block *block;
1165130809Smarcel  struct partial_symtab *ps;
1166130809Smarcel  struct symtab *s;
1167130809Smarcel  const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
1168130809Smarcel
116919370Spst  ALL_PSYMTABS (objfile, ps)
117098948Sobrien  {
1171130809Smarcel    if (!ps->readin
1172130809Smarcel	&& lookup_partial_symbol (ps, name, linkage_name,
1173130809Smarcel				  psymtab_index, domain))
117498948Sobrien      {
117598948Sobrien	s = PSYMTAB_TO_SYMTAB (ps);
117698948Sobrien	bv = BLOCKVECTOR (s);
1177130809Smarcel	block = BLOCKVECTOR_BLOCK (bv, block_index);
1178130809Smarcel	sym = lookup_block_symbol (block, name, linkage_name, domain);
117998948Sobrien	if (!sym)
118098948Sobrien	  {
1181130809Smarcel	    /* This shouldn't be necessary, but as a last resort try
1182130809Smarcel	       looking in the statics even though the psymtab claimed
1183130809Smarcel	       the symbol was global, or vice-versa. It's possible
1184130809Smarcel	       that the psymtab gets it wrong in some cases.  */
1185130809Smarcel
1186130809Smarcel	    /* FIXME: carlton/2002-09-30: Should we really do that?
1187130809Smarcel	       If that happens, isn't it likely to be a GDB error, in
1188130809Smarcel	       which case we should fix the GDB error rather than
1189130809Smarcel	       silently dealing with it here?  So I'd vote for
1190130809Smarcel	       removing the check for the symbol in the other
1191130809Smarcel	       block.  */
1192130809Smarcel	    block = BLOCKVECTOR_BLOCK (bv,
1193130809Smarcel				       block_index == GLOBAL_BLOCK ?
1194130809Smarcel				       STATIC_BLOCK : GLOBAL_BLOCK);
1195130809Smarcel	    sym = lookup_block_symbol (block, name, linkage_name, domain);
119698948Sobrien	    if (!sym)
1197130809Smarcel	      error ("Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n%s may be an inlined function, or may be a template function\n(if a template, try specifying an instantiation: %s<type>).",
1198130809Smarcel		     block_index == GLOBAL_BLOCK ? "global" : "static",
119998948Sobrien		     name, ps->filename, name, name);
120098948Sobrien	  }
120198948Sobrien	if (symtab != NULL)
120298948Sobrien	  *symtab = s;
120398948Sobrien	return fixup_symbol_section (sym, objfile);
120498948Sobrien      }
120598948Sobrien  }
120619370Spst
1207130809Smarcel  return NULL;
1208130809Smarcel}
120946283Sdfr
1210130809Smarcel#if 0
1211130809Smarcel/* Check for the possibility of the symbol being a function or a
1212130809Smarcel   mangled variable that is stored in one of the minimal symbol
1213130809Smarcel   tables.  Eventually, all global symbols might be resolved in this
1214130809Smarcel   way.  */
121546283Sdfr
1216130809Smarcel/* NOTE: carlton/2002-12-05: At one point, this function was part of
1217130809Smarcel   lookup_symbol_aux, and what are now 'return' statements within
1218130809Smarcel   lookup_symbol_aux_minsyms returned from lookup_symbol_aux, even if
1219130809Smarcel   sym was NULL.  As far as I can tell, this was basically accidental;
1220130809Smarcel   it didn't happen every time that msymbol was non-NULL, but only if
1221130809Smarcel   some additional conditions held as well, and it caused problems
1222130809Smarcel   with HP-generated symbol tables.  */
122398948Sobrien
1224130809Smarcel/* NOTE: carlton/2003-05-14: This function was once used as part of
1225130809Smarcel   lookup_symbol.  It is currently unnecessary for correctness
1226130809Smarcel   reasons, however, and using it doesn't seem to be any faster than
1227130809Smarcel   using lookup_symbol_aux_psymtabs, so I'm commenting it out.  */
1228130809Smarcel
1229130809Smarcelstatic struct symbol *
1230130809Smarcellookup_symbol_aux_minsyms (const char *name,
1231130809Smarcel			   const char *linkage_name,
1232130809Smarcel			   const domain_enum domain,
1233130809Smarcel			   int *is_a_field_of_this,
1234130809Smarcel			   struct symtab **symtab)
1235130809Smarcel{
1236130809Smarcel  struct symbol *sym;
1237130809Smarcel  struct blockvector *bv;
1238130809Smarcel  const struct block *block;
1239130809Smarcel  struct minimal_symbol *msymbol;
1240130809Smarcel  struct symtab *s;
1241130809Smarcel
1242130809Smarcel  if (domain == VAR_DOMAIN)
124346283Sdfr    {
124446283Sdfr      msymbol = lookup_minimal_symbol (name, NULL, NULL);
1245130809Smarcel
124646283Sdfr      if (msymbol != NULL)
124746283Sdfr	{
1248130809Smarcel	  /* OK, we found a minimal symbol in spite of not finding any
1249130809Smarcel	     symbol. There are various possible explanations for
1250130809Smarcel	     this. One possibility is the symbol exists in code not
1251130809Smarcel	     compiled -g. Another possibility is that the 'psymtab'
1252130809Smarcel	     isn't doing its job.  A third possibility, related to #2,
1253130809Smarcel	     is that we were confused by name-mangling. For instance,
1254130809Smarcel	     maybe the psymtab isn't doing its job because it only
1255130809Smarcel	     know about demangled names, but we were given a mangled
1256130809Smarcel	     name...  */
125746283Sdfr
1258130809Smarcel	  /* We first use the address in the msymbol to try to locate
1259130809Smarcel	     the appropriate symtab. Note that find_pc_sect_symtab()
1260130809Smarcel	     has a side-effect of doing psymtab-to-symtab expansion,
1261130809Smarcel	     for the found symtab.  */
1262130809Smarcel	  s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
1263130809Smarcel				   SYMBOL_BFD_SECTION (msymbol));
126446283Sdfr	  if (s != NULL)
126546283Sdfr	    {
1266130809Smarcel	      /* This is a function which has a symtab for its address.  */
126746283Sdfr	      bv = BLOCKVECTOR (s);
126846283Sdfr	      block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1269130809Smarcel
1270130809Smarcel	      /* This call used to pass `SYMBOL_LINKAGE_NAME (msymbol)' as the
1271130809Smarcel	         `name' argument to lookup_block_symbol.  But the name
1272130809Smarcel	         of a minimal symbol is always mangled, so that seems
1273130809Smarcel	         to be clearly the wrong thing to pass as the
1274130809Smarcel	         unmangled name.  */
1275130809Smarcel	      sym =
1276130809Smarcel		lookup_block_symbol (block, name, linkage_name, domain);
127798948Sobrien	      /* We kept static functions in minimal symbol table as well as
127898948Sobrien	         in static scope. We want to find them in the symbol table. */
127998948Sobrien	      if (!sym)
128098948Sobrien		{
128146283Sdfr		  block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1282130809Smarcel		  sym = lookup_block_symbol (block, name,
1283130809Smarcel					     linkage_name, domain);
128446283Sdfr		}
128546283Sdfr
1286130809Smarcel	      /* NOTE: carlton/2002-12-04: The following comment was
1287130809Smarcel		 taken from a time when two versions of this function
1288130809Smarcel		 were part of the body of lookup_symbol_aux: this
1289130809Smarcel		 comment was taken from the version of the function
1290130809Smarcel		 that was #ifdef HPUXHPPA, and the comment was right
1291130809Smarcel		 before the 'return NULL' part of lookup_symbol_aux.
1292130809Smarcel		 (Hence the "Fall through and return 0" comment.)
1293130809Smarcel		 Elena did some digging into the situation for
1294130809Smarcel		 Fortran, and she reports:
1295130809Smarcel
1296130809Smarcel		 "I asked around (thanks to Jeff Knaggs), and I think
1297130809Smarcel		 the story for Fortran goes like this:
1298130809Smarcel
1299130809Smarcel		 "Apparently, in older Fortrans, '_' was not part of
1300130809Smarcel		 the user namespace.  g77 attached a final '_' to
1301130809Smarcel		 procedure names as the exported symbols for linkage
1302130809Smarcel		 (foo_) , but the symbols went in the debug info just
1303130809Smarcel		 like 'foo'. The rationale behind this is not
1304130809Smarcel		 completely clear, and maybe it was done to other
1305130809Smarcel		 symbols as well, not just procedures."  */
1306130809Smarcel
130746283Sdfr	      /* If we get here with sym == 0, the symbol was
130898948Sobrien	         found in the minimal symbol table
130998948Sobrien	         but not in the symtab.
131098948Sobrien	         Fall through and return 0 to use the msymbol
131198948Sobrien	         definition of "foo_".
131298948Sobrien	         (Note that outer code generally follows up a call
131398948Sobrien	         to this routine with a call to lookup_minimal_symbol(),
131498948Sobrien	         so a 0 return means we'll just flow into that other routine).
131546283Sdfr
131698948Sobrien	         This happens for Fortran  "foo_" symbols,
131798948Sobrien	         which are "foo" in the symtab.
131846283Sdfr
131998948Sobrien	         This can also happen if "asm" is used to make a
132098948Sobrien	         regular symbol but not a debugging symbol, e.g.
132198948Sobrien	         asm(".globl _main");
132298948Sobrien	         asm("_main:");
132398948Sobrien	       */
132446283Sdfr
1325130809Smarcel	      if (symtab != NULL && sym != NULL)
1326130809Smarcel		*symtab = s;
1327130809Smarcel	      return fixup_symbol_section (sym, s->objfile);
132846283Sdfr	    }
132946283Sdfr	}
133046283Sdfr    }
133146283Sdfr
1332130809Smarcel  return NULL;
1333130809Smarcel}
1334130809Smarcel#endif /* 0 */
133546283Sdfr
1336130809Smarcel/* A default version of lookup_symbol_nonlocal for use by languages
1337130809Smarcel   that can't think of anything better to do.  This implements the C
1338130809Smarcel   lookup rules.  */
1339130809Smarcel
1340130809Smarcelstruct symbol *
1341130809Smarcelbasic_lookup_symbol_nonlocal (const char *name,
1342130809Smarcel			      const char *linkage_name,
1343130809Smarcel			      const struct block *block,
1344130809Smarcel			      const domain_enum domain,
1345130809Smarcel			      struct symtab **symtab)
1346130809Smarcel{
1347130809Smarcel  struct symbol *sym;
1348130809Smarcel
1349130809Smarcel  /* NOTE: carlton/2003-05-19: The comments below were written when
1350130809Smarcel     this (or what turned into this) was part of lookup_symbol_aux;
1351130809Smarcel     I'm much less worried about these questions now, since these
1352130809Smarcel     decisions have turned out well, but I leave these comments here
1353130809Smarcel     for posterity.  */
1354130809Smarcel
1355130809Smarcel  /* NOTE: carlton/2002-12-05: There is a question as to whether or
1356130809Smarcel     not it would be appropriate to search the current global block
1357130809Smarcel     here as well.  (That's what this code used to do before the
1358130809Smarcel     is_a_field_of_this check was moved up.)  On the one hand, it's
1359130809Smarcel     redundant with the lookup_symbol_aux_symtabs search that happens
1360130809Smarcel     next.  On the other hand, if decode_line_1 is passed an argument
1361130809Smarcel     like filename:var, then the user presumably wants 'var' to be
1362130809Smarcel     searched for in filename.  On the third hand, there shouldn't be
1363130809Smarcel     multiple global variables all of which are named 'var', and it's
1364130809Smarcel     not like decode_line_1 has ever restricted its search to only
1365130809Smarcel     global variables in a single filename.  All in all, only
1366130809Smarcel     searching the static block here seems best: it's correct and it's
1367130809Smarcel     cleanest.  */
1368130809Smarcel
1369130809Smarcel  /* NOTE: carlton/2002-12-05: There's also a possible performance
1370130809Smarcel     issue here: if you usually search for global symbols in the
1371130809Smarcel     current file, then it would be slightly better to search the
1372130809Smarcel     current global block before searching all the symtabs.  But there
1373130809Smarcel     are other factors that have a much greater effect on performance
1374130809Smarcel     than that one, so I don't think we should worry about that for
1375130809Smarcel     now.  */
1376130809Smarcel
1377130809Smarcel  sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
1378130809Smarcel  if (sym != NULL)
1379130809Smarcel    return sym;
1380130809Smarcel
1381130809Smarcel  return lookup_symbol_global (name, linkage_name, domain, symtab);
138219370Spst}
138319370Spst
1384130809Smarcel/* Lookup a symbol in the static block associated to BLOCK, if there
1385130809Smarcel   is one; do nothing if BLOCK is NULL or a global block.  */
1386130809Smarcel
1387130809Smarcelstruct symbol *
1388130809Smarcellookup_symbol_static (const char *name,
1389130809Smarcel		      const char *linkage_name,
1390130809Smarcel		      const struct block *block,
1391130809Smarcel		      const domain_enum domain,
1392130809Smarcel		      struct symtab **symtab)
139319370Spst{
1394130809Smarcel  const struct block *static_block = block_static_block (block);
1395130809Smarcel
1396130809Smarcel  if (static_block != NULL)
1397130809Smarcel    return lookup_symbol_aux_block (name, linkage_name, static_block,
1398130809Smarcel				    domain, symtab);
1399130809Smarcel  else
1400130809Smarcel    return NULL;
1401130809Smarcel}
1402130809Smarcel
1403130809Smarcel/* Lookup a symbol in all files' global blocks (searching psymtabs if
1404130809Smarcel   necessary).  */
1405130809Smarcel
1406130809Smarcelstruct symbol *
1407130809Smarcellookup_symbol_global (const char *name,
1408130809Smarcel		      const char *linkage_name,
1409130809Smarcel		      const domain_enum domain,
1410130809Smarcel		      struct symtab **symtab)
1411130809Smarcel{
1412130809Smarcel  struct symbol *sym;
1413130809Smarcel
1414130809Smarcel  sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, linkage_name,
1415130809Smarcel				   domain, symtab);
1416130809Smarcel  if (sym != NULL)
1417130809Smarcel    return sym;
1418130809Smarcel
1419130809Smarcel  return lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, linkage_name,
1420130809Smarcel				     domain, symtab);
1421130809Smarcel}
1422130809Smarcel
1423130809Smarcel/* Look, in partial_symtab PST, for symbol whose natural name is NAME.
1424130809Smarcel   If LINKAGE_NAME is non-NULL, check in addition that the symbol's
1425130809Smarcel   linkage name matches it.  Check the global symbols if GLOBAL, the
1426130809Smarcel   static symbols if not */
1427130809Smarcel
1428130809Smarcelstruct partial_symbol *
1429130809Smarcellookup_partial_symbol (struct partial_symtab *pst, const char *name,
1430130809Smarcel		       const char *linkage_name, int global,
1431130809Smarcel		       domain_enum domain)
1432130809Smarcel{
143398948Sobrien  struct partial_symbol *temp;
143419370Spst  struct partial_symbol **start, **psym;
1435130809Smarcel  struct partial_symbol **top, **real_top, **bottom, **center;
143619370Spst  int length = (global ? pst->n_global_syms : pst->n_static_syms);
143719370Spst  int do_linear_search = 1;
143898948Sobrien
143919370Spst  if (length == 0)
144019370Spst    {
144119370Spst      return (NULL);
144219370Spst    }
144319370Spst  start = (global ?
144419370Spst	   pst->objfile->global_psymbols.list + pst->globals_offset :
144598948Sobrien	   pst->objfile->static_psymbols.list + pst->statics_offset);
144698948Sobrien
144798948Sobrien  if (global)			/* This means we can use a binary search. */
144819370Spst    {
144919370Spst      do_linear_search = 0;
145019370Spst
145119370Spst      /* Binary search.  This search is guaranteed to end with center
1452130809Smarcel         pointing at the earliest partial symbol whose name might be
1453130809Smarcel         correct.  At that point *all* partial symbols with an
1454130809Smarcel         appropriate name will be checked against the correct
1455130809Smarcel         domain.  */
145619370Spst
145719370Spst      bottom = start;
145819370Spst      top = start + length - 1;
1459130809Smarcel      real_top = top;
146019370Spst      while (top > bottom)
146119370Spst	{
146219370Spst	  center = bottom + (top - bottom) / 2;
146319370Spst	  if (!(center < top))
146498948Sobrien	    internal_error (__FILE__, __LINE__, "failed internal consistency check");
146546283Sdfr	  if (!do_linear_search
146698948Sobrien	      && (SYMBOL_LANGUAGE (*center) == language_java))
146719370Spst	    {
146819370Spst	      do_linear_search = 1;
146919370Spst	    }
1470130809Smarcel	  if (strcmp_iw_ordered (SYMBOL_NATURAL_NAME (*center), name) >= 0)
147119370Spst	    {
147219370Spst	      top = center;
147319370Spst	    }
147419370Spst	  else
147519370Spst	    {
147619370Spst	      bottom = center + 1;
147719370Spst	    }
147819370Spst	}
147919370Spst      if (!(top == bottom))
148098948Sobrien	internal_error (__FILE__, __LINE__, "failed internal consistency check");
148198948Sobrien
1482130809Smarcel      while (top <= real_top
1483130809Smarcel	     && (linkage_name != NULL
1484130809Smarcel		 ? strcmp (SYMBOL_LINKAGE_NAME (*top), linkage_name) == 0
1485130809Smarcel		 : SYMBOL_MATCHES_NATURAL_NAME (*top,name)))
148619370Spst	{
1487130809Smarcel	  if (SYMBOL_DOMAIN (*top) == domain)
148819370Spst	    {
148998948Sobrien		  return (*top);
149019370Spst	    }
149198948Sobrien	  top++;
149219370Spst	}
149319370Spst    }
149419370Spst
149519370Spst  /* Can't use a binary search or else we found during the binary search that
149619370Spst     we should also do a linear search. */
149719370Spst
149819370Spst  if (do_linear_search)
149998948Sobrien    {
150019370Spst      for (psym = start; psym < start + length; psym++)
150119370Spst	{
1502130809Smarcel	  if (domain == SYMBOL_DOMAIN (*psym))
150319370Spst	    {
1504130809Smarcel	      if (linkage_name != NULL
1505130809Smarcel		  ? strcmp (SYMBOL_LINKAGE_NAME (*psym), linkage_name) == 0
1506130809Smarcel		  : SYMBOL_MATCHES_NATURAL_NAME (*psym, name))
150719370Spst		{
150819370Spst		  return (*psym);
150919370Spst		}
151019370Spst	    }
151119370Spst	}
151219370Spst    }
151319370Spst
151419370Spst  return (NULL);
151519370Spst}
151619370Spst
1517130809Smarcel/* Look up a type named NAME in the struct_domain.  The type returned
1518130809Smarcel   must not be opaque -- i.e., must have at least one field
1519130809Smarcel   defined.  */
152046283Sdfr
1521130809Smarcelstruct type *
1522130809Smarcellookup_transparent_type (const char *name)
1523130809Smarcel{
1524130809Smarcel  return current_language->la_lookup_transparent_type (name);
1525130809Smarcel}
152646283Sdfr
1527130809Smarcel/* The standard implementation of lookup_transparent_type.  This code
1528130809Smarcel   was modeled on lookup_symbol -- the parts not relevant to looking
1529130809Smarcel   up types were just left out.  In particular it's assumed here that
1530130809Smarcel   types are available in struct_domain and only at file-static or
1531130809Smarcel   global blocks.  */
153246283Sdfr
153346283Sdfrstruct type *
1534130809Smarcelbasic_lookup_transparent_type (const char *name)
153546283Sdfr{
1536130809Smarcel  struct symbol *sym;
1537130809Smarcel  struct symtab *s = NULL;
1538130809Smarcel  struct partial_symtab *ps;
153946283Sdfr  struct blockvector *bv;
1540130809Smarcel  struct objfile *objfile;
1541130809Smarcel  struct block *block;
154246283Sdfr
154346283Sdfr  /* Now search all the global symbols.  Do the symtab's first, then
154446283Sdfr     check the psymtab's. If a psymtab indicates the existence
154546283Sdfr     of the desired name as a global, then do psymtab-to-symtab
154646283Sdfr     conversion on the fly and return the found symbol.  */
154798948Sobrien
154846283Sdfr  ALL_SYMTABS (objfile, s)
154998948Sobrien  {
155098948Sobrien    bv = BLOCKVECTOR (s);
155198948Sobrien    block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1552130809Smarcel    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
155398948Sobrien    if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
155498948Sobrien      {
155598948Sobrien	return SYMBOL_TYPE (sym);
155698948Sobrien      }
155798948Sobrien  }
155846283Sdfr
155946283Sdfr  ALL_PSYMTABS (objfile, ps)
156098948Sobrien  {
1561130809Smarcel    if (!ps->readin && lookup_partial_symbol (ps, name, NULL,
1562130809Smarcel					      1, STRUCT_DOMAIN))
156398948Sobrien      {
156498948Sobrien	s = PSYMTAB_TO_SYMTAB (ps);
156598948Sobrien	bv = BLOCKVECTOR (s);
156698948Sobrien	block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1567130809Smarcel	sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
156898948Sobrien	if (!sym)
156998948Sobrien	  {
157098948Sobrien	    /* This shouldn't be necessary, but as a last resort
157198948Sobrien	     * try looking in the statics even though the psymtab
157298948Sobrien	     * claimed the symbol was global. It's possible that
157398948Sobrien	     * the psymtab gets it wrong in some cases.
157498948Sobrien	     */
157598948Sobrien	    block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1576130809Smarcel	    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
157798948Sobrien	    if (!sym)
157898948Sobrien	      error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
157946283Sdfr%s may be an inlined function, or may be a template function\n\
158046283Sdfr(if a template, try specifying an instantiation: %s<type>).",
158198948Sobrien		     name, ps->filename, name, name);
158298948Sobrien	  }
158398948Sobrien	if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
158498948Sobrien	  return SYMBOL_TYPE (sym);
158598948Sobrien      }
158698948Sobrien  }
158746283Sdfr
158846283Sdfr  /* Now search the static file-level symbols.
158946283Sdfr     Not strictly correct, but more useful than an error.
159046283Sdfr     Do the symtab's first, then
159146283Sdfr     check the psymtab's. If a psymtab indicates the existence
159246283Sdfr     of the desired name as a file-level static, then do psymtab-to-symtab
159346283Sdfr     conversion on the fly and return the found symbol.
159446283Sdfr   */
159546283Sdfr
159646283Sdfr  ALL_SYMTABS (objfile, s)
159798948Sobrien  {
159898948Sobrien    bv = BLOCKVECTOR (s);
159998948Sobrien    block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1600130809Smarcel    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
160198948Sobrien    if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
160298948Sobrien      {
160398948Sobrien	return SYMBOL_TYPE (sym);
160498948Sobrien      }
160598948Sobrien  }
160646283Sdfr
160746283Sdfr  ALL_PSYMTABS (objfile, ps)
160898948Sobrien  {
1609130809Smarcel    if (!ps->readin && lookup_partial_symbol (ps, name, NULL, 0, STRUCT_DOMAIN))
161098948Sobrien      {
161198948Sobrien	s = PSYMTAB_TO_SYMTAB (ps);
161298948Sobrien	bv = BLOCKVECTOR (s);
161398948Sobrien	block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1614130809Smarcel	sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
161598948Sobrien	if (!sym)
161698948Sobrien	  {
161798948Sobrien	    /* This shouldn't be necessary, but as a last resort
161898948Sobrien	     * try looking in the globals even though the psymtab
161998948Sobrien	     * claimed the symbol was static. It's possible that
162098948Sobrien	     * the psymtab gets it wrong in some cases.
162198948Sobrien	     */
162298948Sobrien	    block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1623130809Smarcel	    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
162498948Sobrien	    if (!sym)
162598948Sobrien	      error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
162646283Sdfr%s may be an inlined function, or may be a template function\n\
162746283Sdfr(if a template, try specifying an instantiation: %s<type>).",
162898948Sobrien		     name, ps->filename, name, name);
162998948Sobrien	  }
163098948Sobrien	if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
163198948Sobrien	  return SYMBOL_TYPE (sym);
163298948Sobrien      }
163398948Sobrien  }
163446283Sdfr  return (struct type *) 0;
163546283Sdfr}
163646283Sdfr
163746283Sdfr
163819370Spst/* Find the psymtab containing main(). */
163919370Spst/* FIXME:  What about languages without main() or specially linked
164019370Spst   executables that have no main() ? */
164119370Spst
164219370Spststruct partial_symtab *
164398948Sobrienfind_main_psymtab (void)
164419370Spst{
1645130809Smarcel  struct partial_symtab *pst;
1646130809Smarcel  struct objfile *objfile;
164719370Spst
164819370Spst  ALL_PSYMTABS (objfile, pst)
164998948Sobrien  {
1650130809Smarcel    if (lookup_partial_symbol (pst, main_name (), NULL, 1, VAR_DOMAIN))
165198948Sobrien      {
165298948Sobrien	return (pst);
165398948Sobrien      }
165498948Sobrien  }
165519370Spst  return (NULL);
165619370Spst}
165719370Spst
1658130809Smarcel/* Search BLOCK for symbol NAME in DOMAIN.
165919370Spst
166019370Spst   Note that if NAME is the demangled form of a C++ symbol, we will fail
166119370Spst   to find a match during the binary search of the non-encoded names, but
166219370Spst   for now we don't worry about the slight inefficiency of looking for
166319370Spst   a match we'll never find, since it will go pretty quick.  Once the
166419370Spst   binary search terminates, we drop through and do a straight linear
1665130809Smarcel   search on the symbols.  Each symbol which is marked as being a ObjC/C++
1666130809Smarcel   symbol (language_cplus or language_objc set) has both the encoded and
1667130809Smarcel   non-encoded names tested for a match.
166819370Spst
1669130809Smarcel   If LINKAGE_NAME is non-NULL, verify that any symbol we find has this
1670130809Smarcel   particular mangled name.
1671130809Smarcel*/
1672130809Smarcel
167319370Spststruct symbol *
1674130809Smarcellookup_block_symbol (const struct block *block, const char *name,
1675130809Smarcel		     const char *linkage_name,
1676130809Smarcel		     const domain_enum domain)
167719370Spst{
1678130809Smarcel  struct dict_iterator iter;
1679130809Smarcel  struct symbol *sym;
168019370Spst
1681130809Smarcel  if (!BLOCK_FUNCTION (block))
168219370Spst    {
1683130809Smarcel      for (sym = dict_iter_name_first (BLOCK_DICT (block), name, &iter);
1684130809Smarcel	   sym != NULL;
1685130809Smarcel	   sym = dict_iter_name_next (name, &iter))
168619370Spst	{
1687130809Smarcel	  if (SYMBOL_DOMAIN (sym) == domain
1688130809Smarcel	      && (linkage_name != NULL
1689130809Smarcel		  ? strcmp (SYMBOL_LINKAGE_NAME (sym), linkage_name) == 0 : 1))
1690130809Smarcel	    return sym;
169119370Spst	}
1692130809Smarcel      return NULL;
169319370Spst    }
1694130809Smarcel  else
1695130809Smarcel    {
1696130809Smarcel      /* Note that parameter symbols do not always show up last in the
1697130809Smarcel	 list; this loop makes sure to take anything else other than
1698130809Smarcel	 parameter symbols first; it only uses parameter symbols as a
1699130809Smarcel	 last resort.  Note that this only takes up extra computation
1700130809Smarcel	 time on a match.  */
170119370Spst
1702130809Smarcel      struct symbol *sym_found = NULL;
170319370Spst
1704130809Smarcel      for (sym = dict_iter_name_first (BLOCK_DICT (block), name, &iter);
1705130809Smarcel	   sym != NULL;
1706130809Smarcel	   sym = dict_iter_name_next (name, &iter))
170719370Spst	{
1708130809Smarcel	  if (SYMBOL_DOMAIN (sym) == domain
1709130809Smarcel	      && (linkage_name != NULL
1710130809Smarcel		  ? strcmp (SYMBOL_LINKAGE_NAME (sym), linkage_name) == 0 : 1))
171119370Spst	    {
171219370Spst	      sym_found = sym;
171319370Spst	      if (SYMBOL_CLASS (sym) != LOC_ARG &&
171419370Spst		  SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
171519370Spst		  SYMBOL_CLASS (sym) != LOC_REF_ARG &&
171619370Spst		  SYMBOL_CLASS (sym) != LOC_REGPARM &&
171719370Spst		  SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1718130809Smarcel		  SYMBOL_CLASS (sym) != LOC_BASEREG_ARG &&
1719130809Smarcel		  SYMBOL_CLASS (sym) != LOC_COMPUTED_ARG)
172019370Spst		{
172119370Spst		  break;
172219370Spst		}
172319370Spst	    }
172419370Spst	}
1725130809Smarcel      return (sym_found);	/* Will be NULL if not found. */
172619370Spst    }
172719370Spst}
172819370Spst
172946283Sdfr/* Find the symtab associated with PC and SECTION.  Look through the
173046283Sdfr   psymtabs and read in another symtab if necessary. */
173119370Spst
173219370Spststruct symtab *
173398948Sobrienfind_pc_sect_symtab (CORE_ADDR pc, asection *section)
173419370Spst{
1735130809Smarcel  struct block *b;
173619370Spst  struct blockvector *bv;
1737130809Smarcel  struct symtab *s = NULL;
1738130809Smarcel  struct symtab *best_s = NULL;
1739130809Smarcel  struct partial_symtab *ps;
1740130809Smarcel  struct objfile *objfile;
174146283Sdfr  CORE_ADDR distance = 0;
174298948Sobrien  struct minimal_symbol *msymbol;
174319370Spst
174498948Sobrien  /* If we know that this is not a text address, return failure.  This is
174598948Sobrien     necessary because we loop based on the block's high and low code
174698948Sobrien     addresses, which do not include the data ranges, and because
174798948Sobrien     we call find_pc_sect_psymtab which has a similar restriction based
174898948Sobrien     on the partial_symtab's texthigh and textlow.  */
174998948Sobrien  msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
175098948Sobrien  if (msymbol
175198948Sobrien      && (msymbol->type == mst_data
175298948Sobrien	  || msymbol->type == mst_bss
175398948Sobrien	  || msymbol->type == mst_abs
175498948Sobrien	  || msymbol->type == mst_file_data
175598948Sobrien	  || msymbol->type == mst_file_bss))
175698948Sobrien    return NULL;
175798948Sobrien
175819370Spst  /* Search all symtabs for the one whose file contains our address, and which
175919370Spst     is the smallest of all the ones containing the address.  This is designed
176019370Spst     to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
176119370Spst     and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
176219370Spst     0x1000-0x4000, but for address 0x2345 we want to return symtab b.
176319370Spst
176419370Spst     This happens for native ecoff format, where code from included files
176519370Spst     gets its own symtab. The symtab for the included file should have
176619370Spst     been read in already via the dependency mechanism.
176719370Spst     It might be swifter to create several symtabs with the same name
176819370Spst     like xcoff does (I'm not sure).
176919370Spst
177019370Spst     It also happens for objfiles that have their functions reordered.
177119370Spst     For these, the symtab we are looking for is not necessarily read in.  */
177219370Spst
177319370Spst  ALL_SYMTABS (objfile, s)
177498948Sobrien  {
177598948Sobrien    bv = BLOCKVECTOR (s);
177698948Sobrien    b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
177746283Sdfr
177898948Sobrien    if (BLOCK_START (b) <= pc
177998948Sobrien	&& BLOCK_END (b) > pc
178098948Sobrien	&& (distance == 0
178198948Sobrien	    || BLOCK_END (b) - BLOCK_START (b) < distance))
178298948Sobrien      {
178398948Sobrien	/* For an objfile that has its functions reordered,
178498948Sobrien	   find_pc_psymtab will find the proper partial symbol table
178598948Sobrien	   and we simply return its corresponding symtab.  */
178698948Sobrien	/* In order to better support objfiles that contain both
178798948Sobrien	   stabs and coff debugging info, we continue on if a psymtab
178898948Sobrien	   can't be found. */
178998948Sobrien	if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
179098948Sobrien	  {
179198948Sobrien	    ps = find_pc_sect_psymtab (pc, section);
179298948Sobrien	    if (ps)
179398948Sobrien	      return PSYMTAB_TO_SYMTAB (ps);
179498948Sobrien	  }
179598948Sobrien	if (section != 0)
179698948Sobrien	  {
1797130809Smarcel	    struct dict_iterator iter;
1798130809Smarcel	    struct symbol *sym = NULL;
179946283Sdfr
1800130809Smarcel	    ALL_BLOCK_SYMBOLS (b, iter, sym)
180198948Sobrien	      {
1802130809Smarcel		fixup_symbol_section (sym, objfile);
1803130809Smarcel		if (section == SYMBOL_BFD_SECTION (sym))
180498948Sobrien		  break;
180598948Sobrien	      }
1806130809Smarcel	    if (sym == NULL)
180798948Sobrien	      continue;		/* no symbol in this symtab matches section */
180898948Sobrien	  }
180998948Sobrien	distance = BLOCK_END (b) - BLOCK_START (b);
181098948Sobrien	best_s = s;
181198948Sobrien      }
181298948Sobrien  }
181319370Spst
181419370Spst  if (best_s != NULL)
181598948Sobrien    return (best_s);
181619370Spst
181719370Spst  s = NULL;
181846283Sdfr  ps = find_pc_sect_psymtab (pc, section);
181919370Spst  if (ps)
182019370Spst    {
182119370Spst      if (ps->readin)
182219370Spst	/* Might want to error() here (in case symtab is corrupt and
182319370Spst	   will cause a core dump), but maybe we can successfully
182419370Spst	   continue, so let's not.  */
182519370Spst	warning ("\
182698948Sobrien(Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
182798948Sobrien		 paddr_nz (pc));
182819370Spst      s = PSYMTAB_TO_SYMTAB (ps);
182919370Spst    }
183019370Spst  return (s);
183119370Spst}
183246283Sdfr
183346283Sdfr/* Find the symtab associated with PC.  Look through the psymtabs and
183446283Sdfr   read in another symtab if necessary.  Backward compatibility, no section */
183546283Sdfr
183646283Sdfrstruct symtab *
183798948Sobrienfind_pc_symtab (CORE_ADDR pc)
183846283Sdfr{
183946283Sdfr  return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
184046283Sdfr}
184198948Sobrien
184246283Sdfr
184398948Sobrien/* Find the source file and line number for a given PC value and SECTION.
184419370Spst   Return a structure containing a symtab pointer, a line number,
184519370Spst   and a pc range for the entire source line.
184619370Spst   The value's .pc field is NOT the specified pc.
184719370Spst   NOTCURRENT nonzero means, if specified pc is on a line boundary,
184819370Spst   use the line that ends there.  Otherwise, in that case, the line
184919370Spst   that begins there is used.  */
185019370Spst
185119370Spst/* The big complication here is that a line may start in one file, and end just
185219370Spst   before the start of another file.  This usually occurs when you #include
185319370Spst   code in the middle of a subroutine.  To properly find the end of a line's PC
185419370Spst   range, we must search all symtabs associated with this compilation unit, and
185519370Spst   find the one whose first PC is closer than that of the next line in this
185619370Spst   symtab.  */
185719370Spst
185819370Spst/* If it's worth the effort, we could be using a binary search.  */
185919370Spst
186019370Spststruct symtab_and_line
1861130809Smarcelfind_pc_sect_line (CORE_ADDR pc, struct bfd_section *section, int notcurrent)
186219370Spst{
186319370Spst  struct symtab *s;
1864130809Smarcel  struct linetable *l;
1865130809Smarcel  int len;
1866130809Smarcel  int i;
1867130809Smarcel  struct linetable_entry *item;
186819370Spst  struct symtab_and_line val;
186919370Spst  struct blockvector *bv;
187046283Sdfr  struct minimal_symbol *msymbol;
187146283Sdfr  struct minimal_symbol *mfunsym;
187219370Spst
187319370Spst  /* Info on best line seen so far, and where it starts, and its file.  */
187419370Spst
187519370Spst  struct linetable_entry *best = NULL;
187619370Spst  CORE_ADDR best_end = 0;
187719370Spst  struct symtab *best_symtab = 0;
187819370Spst
187919370Spst  /* Store here the first line number
188019370Spst     of a file which contains the line at the smallest pc after PC.
188119370Spst     If we don't find a line whose range contains PC,
188219370Spst     we will use a line one less than this,
188319370Spst     with a range from the start of that file to the first line's pc.  */
188419370Spst  struct linetable_entry *alt = NULL;
188519370Spst  struct symtab *alt_symtab = 0;
188619370Spst
188719370Spst  /* Info on best line seen in this file.  */
188819370Spst
188919370Spst  struct linetable_entry *prev;
189019370Spst
189119370Spst  /* If this pc is not from the current frame,
189219370Spst     it is the address of the end of a call instruction.
189319370Spst     Quite likely that is the start of the following statement.
189419370Spst     But what we want is the statement containing the instruction.
189519370Spst     Fudge the pc to make sure we get that.  */
189619370Spst
1897130809Smarcel  init_sal (&val);		/* initialize to zeroes */
189819370Spst
189998948Sobrien  /* It's tempting to assume that, if we can't find debugging info for
190098948Sobrien     any function enclosing PC, that we shouldn't search for line
190198948Sobrien     number info, either.  However, GAS can emit line number info for
190298948Sobrien     assembly files --- very helpful when debugging hand-written
190398948Sobrien     assembly code.  In such a case, we'd have no debug info for the
190498948Sobrien     function, but we would have line info.  */
190598948Sobrien
190646283Sdfr  if (notcurrent)
190746283Sdfr    pc -= 1;
190846283Sdfr
190998948Sobrien  /* elz: added this because this function returned the wrong
191046283Sdfr     information if the pc belongs to a stub (import/export)
191146283Sdfr     to call a shlib function. This stub would be anywhere between
191246283Sdfr     two functions in the target, and the line info was erroneously
191346283Sdfr     taken to be the one of the line before the pc.
191498948Sobrien   */
191546283Sdfr  /* RT: Further explanation:
191698948Sobrien
191746283Sdfr   * We have stubs (trampolines) inserted between procedures.
191846283Sdfr   *
191946283Sdfr   * Example: "shr1" exists in a shared library, and a "shr1" stub also
192046283Sdfr   * exists in the main image.
192146283Sdfr   *
192246283Sdfr   * In the minimal symbol table, we have a bunch of symbols
192346283Sdfr   * sorted by start address. The stubs are marked as "trampoline",
192446283Sdfr   * the others appear as text. E.g.:
192546283Sdfr   *
192646283Sdfr   *  Minimal symbol table for main image
192746283Sdfr   *     main:  code for main (text symbol)
192846283Sdfr   *     shr1: stub  (trampoline symbol)
192946283Sdfr   *     foo:   code for foo (text symbol)
193046283Sdfr   *     ...
193146283Sdfr   *  Minimal symbol table for "shr1" image:
193246283Sdfr   *     ...
193346283Sdfr   *     shr1: code for shr1 (text symbol)
193446283Sdfr   *     ...
193546283Sdfr   *
193646283Sdfr   * So the code below is trying to detect if we are in the stub
193746283Sdfr   * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
193846283Sdfr   * and if found,  do the symbolization from the real-code address
193946283Sdfr   * rather than the stub address.
194046283Sdfr   *
194146283Sdfr   * Assumptions being made about the minimal symbol table:
194246283Sdfr   *   1. lookup_minimal_symbol_by_pc() will return a trampoline only
194346283Sdfr   *      if we're really in the trampoline. If we're beyond it (say
194446283Sdfr   *      we're in "foo" in the above example), it'll have a closer
194546283Sdfr   *      symbol (the "foo" text symbol for example) and will not
194646283Sdfr   *      return the trampoline.
194746283Sdfr   *   2. lookup_minimal_symbol_text() will find a real text symbol
194846283Sdfr   *      corresponding to the trampoline, and whose address will
194946283Sdfr   *      be different than the trampoline address. I put in a sanity
195046283Sdfr   *      check for the address being the same, to avoid an
195146283Sdfr   *      infinite recursion.
195246283Sdfr   */
195398948Sobrien  msymbol = lookup_minimal_symbol_by_pc (pc);
195498948Sobrien  if (msymbol != NULL)
195546283Sdfr    if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
195698948Sobrien      {
1957130809Smarcel	mfunsym = lookup_minimal_symbol_text (SYMBOL_LINKAGE_NAME (msymbol),
1958130809Smarcel					      NULL);
195998948Sobrien	if (mfunsym == NULL)
196098948Sobrien	  /* I eliminated this warning since it is coming out
196198948Sobrien	   * in the following situation:
196298948Sobrien	   * gdb shmain // test program with shared libraries
196398948Sobrien	   * (gdb) break shr1  // function in shared lib
196498948Sobrien	   * Warning: In stub for ...
196598948Sobrien	   * In the above situation, the shared lib is not loaded yet,
196698948Sobrien	   * so of course we can't find the real func/line info,
196798948Sobrien	   * but the "break" still works, and the warning is annoying.
196898948Sobrien	   * So I commented out the warning. RT */
1969130809Smarcel	  /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_LINKAGE_NAME (msymbol)) */ ;
197098948Sobrien	/* fall through */
197198948Sobrien	else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
197298948Sobrien	  /* Avoid infinite recursion */
197398948Sobrien	  /* See above comment about why warning is commented out */
1974130809Smarcel	  /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_LINKAGE_NAME (msymbol)) */ ;
197598948Sobrien	/* fall through */
197698948Sobrien	else
197798948Sobrien	  return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
197898948Sobrien      }
197946283Sdfr
198046283Sdfr
198146283Sdfr  s = find_pc_sect_symtab (pc, section);
198219370Spst  if (!s)
198319370Spst    {
198446283Sdfr      /* if no symbol information, return previous pc */
198546283Sdfr      if (notcurrent)
198646283Sdfr	pc++;
198719370Spst      val.pc = pc;
198819370Spst      return val;
198919370Spst    }
199019370Spst
199119370Spst  bv = BLOCKVECTOR (s);
199219370Spst
199319370Spst  /* Look at all the symtabs that share this blockvector.
199419370Spst     They all have the same apriori range, that we found was right;
199519370Spst     but they have different line tables.  */
199619370Spst
199719370Spst  for (; s && BLOCKVECTOR (s) == bv; s = s->next)
199819370Spst    {
199919370Spst      /* Find the best line in this symtab.  */
200019370Spst      l = LINETABLE (s);
200119370Spst      if (!l)
200298948Sobrien	continue;
200319370Spst      len = l->nitems;
200419370Spst      if (len <= 0)
200519370Spst	{
200619370Spst	  /* I think len can be zero if the symtab lacks line numbers
200719370Spst	     (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
200819370Spst	     I'm not sure which, and maybe it depends on the symbol
200919370Spst	     reader).  */
201019370Spst	  continue;
201119370Spst	}
201219370Spst
201319370Spst      prev = NULL;
201419370Spst      item = l->item;		/* Get first line info */
201519370Spst
201619370Spst      /* Is this file's first line closer than the first lines of other files?
201798948Sobrien         If so, record this file, and its first line, as best alternate.  */
201819370Spst      if (item->pc > pc && (!alt || item->pc < alt->pc))
201919370Spst	{
202019370Spst	  alt = item;
202119370Spst	  alt_symtab = s;
202219370Spst	}
202319370Spst
202419370Spst      for (i = 0; i < len; i++, item++)
202519370Spst	{
202646283Sdfr	  /* Leave prev pointing to the linetable entry for the last line
202746283Sdfr	     that started at or before PC.  */
202819370Spst	  if (item->pc > pc)
202919370Spst	    break;
203019370Spst
203119370Spst	  prev = item;
203219370Spst	}
203319370Spst
203419370Spst      /* At this point, prev points at the line whose start addr is <= pc, and
203598948Sobrien         item points at the next line.  If we ran off the end of the linetable
203698948Sobrien         (pc >= start of the last line), then prev == item.  If pc < start of
203798948Sobrien         the first line, prev will not be set.  */
203819370Spst
203919370Spst      /* Is this file's best line closer than the best in the other files?
2040130809Smarcel         If so, record this file, and its best line, as best so far.  Don't
2041130809Smarcel         save prev if it represents the end of a function (i.e. line number
2042130809Smarcel         0) instead of a real line.  */
204319370Spst
2044130809Smarcel      if (prev && prev->line && (!best || prev->pc > best->pc))
204519370Spst	{
204619370Spst	  best = prev;
204719370Spst	  best_symtab = s;
204898948Sobrien
204998948Sobrien	  /* Discard BEST_END if it's before the PC of the current BEST.  */
205098948Sobrien	  if (best_end <= best->pc)
205198948Sobrien	    best_end = 0;
205219370Spst	}
205398948Sobrien
205498948Sobrien      /* If another line (denoted by ITEM) is in the linetable and its
205598948Sobrien         PC is after BEST's PC, but before the current BEST_END, then
205698948Sobrien	 use ITEM's PC as the new best_end.  */
205798948Sobrien      if (best && i < len && item->pc > best->pc
205898948Sobrien          && (best_end == 0 || best_end > item->pc))
205998948Sobrien	best_end = item->pc;
206019370Spst    }
206119370Spst
206219370Spst  if (!best_symtab)
206319370Spst    {
206419370Spst      if (!alt_symtab)
206519370Spst	{			/* If we didn't find any line # info, just
206646283Sdfr				   return zeros.  */
206719370Spst	  val.pc = pc;
206819370Spst	}
206919370Spst      else
207019370Spst	{
207119370Spst	  val.symtab = alt_symtab;
207219370Spst	  val.line = alt->line - 1;
207319370Spst
207419370Spst	  /* Don't return line 0, that means that we didn't find the line.  */
207598948Sobrien	  if (val.line == 0)
207698948Sobrien	    ++val.line;
207719370Spst
207819370Spst	  val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
207919370Spst	  val.end = alt->pc;
208019370Spst	}
208119370Spst    }
208298948Sobrien  else if (best->line == 0)
208398948Sobrien    {
208498948Sobrien      /* If our best fit is in a range of PC's for which no line
208598948Sobrien	 number info is available (line number is zero) then we didn't
208698948Sobrien	 find any valid line information. */
208798948Sobrien      val.pc = pc;
208898948Sobrien    }
208919370Spst  else
209019370Spst    {
209119370Spst      val.symtab = best_symtab;
209219370Spst      val.line = best->line;
209319370Spst      val.pc = best->pc;
209419370Spst      if (best_end && (!alt || best_end < alt->pc))
209519370Spst	val.end = best_end;
209619370Spst      else if (alt)
209719370Spst	val.end = alt->pc;
209819370Spst      else
209919370Spst	val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
210019370Spst    }
210146283Sdfr  val.section = section;
210219370Spst  return val;
210319370Spst}
210446283Sdfr
210546283Sdfr/* Backward compatibility (no section) */
210646283Sdfr
210746283Sdfrstruct symtab_and_line
210898948Sobrienfind_pc_line (CORE_ADDR pc, int notcurrent)
210946283Sdfr{
211098948Sobrien  asection *section;
211146283Sdfr
211246283Sdfr  section = find_pc_overlay (pc);
211346283Sdfr  if (pc_in_unmapped_range (pc, section))
211446283Sdfr    pc = overlay_mapped_address (pc, section);
211546283Sdfr  return find_pc_sect_line (pc, section, notcurrent);
211646283Sdfr}
211719370Spst
211819370Spst/* Find line number LINE in any symtab whose name is the same as
211919370Spst   SYMTAB.
212019370Spst
212146283Sdfr   If found, return the symtab that contains the linetable in which it was
212219370Spst   found, set *INDEX to the index in the linetable of the best entry
212319370Spst   found, and set *EXACT_MATCH nonzero if the value returned is an
212419370Spst   exact match.
212519370Spst
212646283Sdfr   If not found, return NULL.  */
212719370Spst
212898948Sobrienstruct symtab *
212998948Sobrienfind_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
213019370Spst{
213119370Spst  int exact;
213219370Spst
213319370Spst  /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
213419370Spst     so far seen.  */
213519370Spst
213619370Spst  int best_index;
213719370Spst  struct linetable *best_linetable;
213846283Sdfr  struct symtab *best_symtab;
213919370Spst
214019370Spst  /* First try looking it up in the given symtab.  */
214119370Spst  best_linetable = LINETABLE (symtab);
214246283Sdfr  best_symtab = symtab;
214319370Spst  best_index = find_line_common (best_linetable, line, &exact);
214419370Spst  if (best_index < 0 || !exact)
214519370Spst    {
214619370Spst      /* Didn't find an exact match.  So we better keep looking for
214798948Sobrien         another symtab with the same name.  In the case of xcoff,
214898948Sobrien         multiple csects for one source file (produced by IBM's FORTRAN
214998948Sobrien         compiler) produce multiple symtabs (this is unavoidable
215098948Sobrien         assuming csects can be at arbitrary places in memory and that
215198948Sobrien         the GLOBAL_BLOCK of a symtab has a begin and end address).  */
215219370Spst
215319370Spst      /* BEST is the smallest linenumber > LINE so far seen,
215498948Sobrien         or 0 if none has been seen so far.
215598948Sobrien         BEST_INDEX and BEST_LINETABLE identify the item for it.  */
215619370Spst      int best;
215719370Spst
215819370Spst      struct objfile *objfile;
215919370Spst      struct symtab *s;
216019370Spst
216119370Spst      if (best_index >= 0)
216219370Spst	best = best_linetable->item[best_index].line;
216319370Spst      else
216419370Spst	best = 0;
216519370Spst
216619370Spst      ALL_SYMTABS (objfile, s)
216798948Sobrien      {
216898948Sobrien	struct linetable *l;
216998948Sobrien	int ind;
217019370Spst
2171130809Smarcel	if (strcmp (symtab->filename, s->filename) != 0)
217298948Sobrien	  continue;
217398948Sobrien	l = LINETABLE (s);
217498948Sobrien	ind = find_line_common (l, line, &exact);
217598948Sobrien	if (ind >= 0)
217698948Sobrien	  {
217798948Sobrien	    if (exact)
217898948Sobrien	      {
217998948Sobrien		best_index = ind;
218098948Sobrien		best_linetable = l;
218198948Sobrien		best_symtab = s;
218298948Sobrien		goto done;
218398948Sobrien	      }
218498948Sobrien	    if (best == 0 || l->item[ind].line < best)
218598948Sobrien	      {
218698948Sobrien		best = l->item[ind].line;
218798948Sobrien		best_index = ind;
218898948Sobrien		best_linetable = l;
218998948Sobrien		best_symtab = s;
219098948Sobrien	      }
219198948Sobrien	  }
219298948Sobrien      }
219319370Spst    }
219498948Sobriendone:
219519370Spst  if (best_index < 0)
219646283Sdfr    return NULL;
219719370Spst
219819370Spst  if (index)
219919370Spst    *index = best_index;
220019370Spst  if (exact_match)
220119370Spst    *exact_match = exact;
220246283Sdfr
220346283Sdfr  return best_symtab;
220419370Spst}
220519370Spst
220646283Sdfr/* Set the PC value for a given source file and line number and return true.
220746283Sdfr   Returns zero for invalid line number (and sets the PC to 0).
220819370Spst   The source file is specified with a struct symtab.  */
220919370Spst
221046283Sdfrint
221198948Sobrienfind_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
221219370Spst{
221319370Spst  struct linetable *l;
221419370Spst  int ind;
221519370Spst
221646283Sdfr  *pc = 0;
221719370Spst  if (symtab == 0)
221819370Spst    return 0;
221946283Sdfr
222046283Sdfr  symtab = find_line_symtab (symtab, line, &ind, NULL);
222146283Sdfr  if (symtab != NULL)
222246283Sdfr    {
222346283Sdfr      l = LINETABLE (symtab);
222446283Sdfr      *pc = l->item[ind].pc;
222546283Sdfr      return 1;
222646283Sdfr    }
222719370Spst  else
222819370Spst    return 0;
222919370Spst}
223019370Spst
223119370Spst/* Find the range of pc values in a line.
223219370Spst   Store the starting pc of the line into *STARTPTR
223319370Spst   and the ending pc (start of next line) into *ENDPTR.
223419370Spst   Returns 1 to indicate success.
223519370Spst   Returns 0 if could not find the specified line.  */
223619370Spst
223719370Spstint
223898948Sobrienfind_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
223998948Sobrien		    CORE_ADDR *endptr)
224019370Spst{
224119370Spst  CORE_ADDR startaddr;
224219370Spst  struct symtab_and_line found_sal;
224319370Spst
224419370Spst  startaddr = sal.pc;
224598948Sobrien  if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
224619370Spst    return 0;
224719370Spst
224819370Spst  /* This whole function is based on address.  For example, if line 10 has
224919370Spst     two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
225019370Spst     "info line *0x123" should say the line goes from 0x100 to 0x200
225119370Spst     and "info line *0x355" should say the line goes from 0x300 to 0x400.
225219370Spst     This also insures that we never give a range like "starts at 0x134
225319370Spst     and ends at 0x12c".  */
225419370Spst
225546283Sdfr  found_sal = find_pc_sect_line (startaddr, sal.section, 0);
225619370Spst  if (found_sal.line != sal.line)
225719370Spst    {
225819370Spst      /* The specified line (sal) has zero bytes.  */
225919370Spst      *startptr = found_sal.pc;
226019370Spst      *endptr = found_sal.pc;
226119370Spst    }
226219370Spst  else
226319370Spst    {
226419370Spst      *startptr = found_sal.pc;
226519370Spst      *endptr = found_sal.end;
226619370Spst    }
226719370Spst  return 1;
226819370Spst}
226919370Spst
227019370Spst/* Given a line table and a line number, return the index into the line
227119370Spst   table for the pc of the nearest line whose number is >= the specified one.
227219370Spst   Return -1 if none is found.  The value is >= 0 if it is an index.
227319370Spst
227419370Spst   Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
227519370Spst
227619370Spststatic int
2277130809Smarcelfind_line_common (struct linetable *l, int lineno,
227898948Sobrien		  int *exact_match)
227919370Spst{
2280130809Smarcel  int i;
2281130809Smarcel  int len;
228219370Spst
228319370Spst  /* BEST is the smallest linenumber > LINENO so far seen,
228419370Spst     or 0 if none has been seen so far.
228519370Spst     BEST_INDEX identifies the item for it.  */
228619370Spst
228719370Spst  int best_index = -1;
228819370Spst  int best = 0;
228919370Spst
229019370Spst  if (lineno <= 0)
229119370Spst    return -1;
229219370Spst  if (l == 0)
229319370Spst    return -1;
229419370Spst
229519370Spst  len = l->nitems;
229619370Spst  for (i = 0; i < len; i++)
229719370Spst    {
2298130809Smarcel      struct linetable_entry *item = &(l->item[i]);
229919370Spst
230019370Spst      if (item->line == lineno)
230119370Spst	{
230219370Spst	  /* Return the first (lowest address) entry which matches.  */
230319370Spst	  *exact_match = 1;
230419370Spst	  return i;
230519370Spst	}
230619370Spst
230719370Spst      if (item->line > lineno && (best == 0 || item->line < best))
230819370Spst	{
230919370Spst	  best = item->line;
231019370Spst	  best_index = i;
231119370Spst	}
231219370Spst    }
231319370Spst
231419370Spst  /* If we got here, we didn't get an exact match.  */
231519370Spst
231619370Spst  *exact_match = 0;
231719370Spst  return best_index;
231819370Spst}
231919370Spst
232019370Spstint
232198948Sobrienfind_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
232219370Spst{
232319370Spst  struct symtab_and_line sal;
232419370Spst  sal = find_pc_line (pc, 0);
232519370Spst  *startptr = sal.pc;
232619370Spst  *endptr = sal.end;
232719370Spst  return sal.symtab != 0;
232819370Spst}
232919370Spst
233019370Spst/* Given a function symbol SYM, find the symtab and line for the start
233119370Spst   of the function.
233219370Spst   If the argument FUNFIRSTLINE is nonzero, we want the first line
233319370Spst   of real code inside the function.  */
233419370Spst
233598948Sobrienstruct symtab_and_line
233698948Sobrienfind_function_start_sal (struct symbol *sym, int funfirstline)
233719370Spst{
233819370Spst  CORE_ADDR pc;
233919370Spst  struct symtab_and_line sal;
234019370Spst
234119370Spst  pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
234246283Sdfr  fixup_symbol_section (sym, NULL);
234319370Spst  if (funfirstline)
234498948Sobrien    {				/* skip "first line" of function (which is actually its prologue) */
234546283Sdfr      asection *section = SYMBOL_BFD_SECTION (sym);
234646283Sdfr      /* If function is in an unmapped overlay, use its unmapped LMA
234798948Sobrien         address, so that SKIP_PROLOGUE has something unique to work on */
234846283Sdfr      if (section_is_overlay (section) &&
234946283Sdfr	  !section_is_mapped (section))
235046283Sdfr	pc = overlay_unmapped_address (pc, section);
235146283Sdfr
235219370Spst      pc += FUNCTION_START_OFFSET;
235398948Sobrien      pc = SKIP_PROLOGUE (pc);
235446283Sdfr
235546283Sdfr      /* For overlays, map pc back into its mapped VMA range */
235646283Sdfr      pc = overlay_mapped_address (pc, section);
235719370Spst    }
235846283Sdfr  sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
235919370Spst
236019370Spst  /* Check if SKIP_PROLOGUE left us in mid-line, and the next
236119370Spst     line is still part of the same function.  */
236219370Spst  if (sal.pc != pc
236319370Spst      && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
236419370Spst      && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
236519370Spst    {
236619370Spst      /* First pc of next line */
236719370Spst      pc = sal.end;
236819370Spst      /* Recalculate the line number (might not be N+1).  */
236946283Sdfr      sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
237019370Spst    }
237119370Spst  sal.pc = pc;
237219370Spst
237319370Spst  return sal;
237419370Spst}
237598948Sobrien
237619370Spst/* If P is of the form "operator[ \t]+..." where `...' is
237719370Spst   some legitimate operator text, return a pointer to the
237819370Spst   beginning of the substring of the operator text.
237919370Spst   Otherwise, return "".  */
238046283Sdfrchar *
238198948Sobrienoperator_chars (char *p, char **end)
238219370Spst{
238319370Spst  *end = "";
238419370Spst  if (strncmp (p, "operator", 8))
238519370Spst    return *end;
238619370Spst  p += 8;
238719370Spst
238819370Spst  /* Don't get faked out by `operator' being part of a longer
238919370Spst     identifier.  */
239098948Sobrien  if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
239119370Spst    return *end;
239219370Spst
239319370Spst  /* Allow some whitespace between `operator' and the operator symbol.  */
239419370Spst  while (*p == ' ' || *p == '\t')
239519370Spst    p++;
239619370Spst
239719370Spst  /* Recognize 'operator TYPENAME'. */
239819370Spst
239998948Sobrien  if (isalpha (*p) || *p == '_' || *p == '$')
240019370Spst    {
2401130809Smarcel      char *q = p + 1;
240298948Sobrien      while (isalnum (*q) || *q == '_' || *q == '$')
240319370Spst	q++;
240419370Spst      *end = q;
240519370Spst      return p;
240619370Spst    }
240719370Spst
240898948Sobrien  while (*p)
240998948Sobrien    switch (*p)
241098948Sobrien      {
241198948Sobrien      case '\\':			/* regexp quoting */
241298948Sobrien	if (p[1] == '*')
241398948Sobrien	  {
241498948Sobrien	    if (p[2] == '=')	/* 'operator\*=' */
241598948Sobrien	      *end = p + 3;
241698948Sobrien	    else			/* 'operator\*'  */
241798948Sobrien	      *end = p + 2;
241898948Sobrien	    return p;
241998948Sobrien	  }
242098948Sobrien	else if (p[1] == '[')
242198948Sobrien	  {
242298948Sobrien	    if (p[2] == ']')
242398948Sobrien	      error ("mismatched quoting on brackets, try 'operator\\[\\]'");
242498948Sobrien	    else if (p[2] == '\\' && p[3] == ']')
242598948Sobrien	      {
242698948Sobrien		*end = p + 4;	/* 'operator\[\]' */
242798948Sobrien		return p;
242898948Sobrien	      }
242998948Sobrien	    else
243098948Sobrien	      error ("nothing is allowed between '[' and ']'");
243198948Sobrien	  }
243298948Sobrien	else
243398948Sobrien	  {
243498948Sobrien	    /* Gratuitous qoute: skip it and move on. */
243598948Sobrien	    p++;
243698948Sobrien	    continue;
243798948Sobrien	  }
243898948Sobrien	break;
243998948Sobrien      case '!':
244098948Sobrien      case '=':
244198948Sobrien      case '*':
244298948Sobrien      case '/':
244398948Sobrien      case '%':
244498948Sobrien      case '^':
244598948Sobrien	if (p[1] == '=')
244698948Sobrien	  *end = p + 2;
244798948Sobrien	else
244898948Sobrien	  *end = p + 1;
244998948Sobrien	return p;
245098948Sobrien      case '<':
245198948Sobrien      case '>':
245298948Sobrien      case '+':
245398948Sobrien      case '-':
245498948Sobrien      case '&':
245598948Sobrien      case '|':
245698948Sobrien	if (p[0] == '-' && p[1] == '>')
245798948Sobrien	  {
245898948Sobrien	    /* Struct pointer member operator 'operator->'. */
245998948Sobrien	    if (p[2] == '*')
246098948Sobrien	      {
246198948Sobrien		*end = p + 3;	/* 'operator->*' */
246298948Sobrien		return p;
246398948Sobrien	      }
246498948Sobrien	    else if (p[2] == '\\')
246598948Sobrien	      {
246698948Sobrien		*end = p + 4;	/* Hopefully 'operator->\*' */
246798948Sobrien		return p;
246898948Sobrien	      }
246998948Sobrien	    else
247098948Sobrien	      {
247198948Sobrien		*end = p + 2;	/* 'operator->' */
247298948Sobrien		return p;
247398948Sobrien	      }
247498948Sobrien	  }
247598948Sobrien	if (p[1] == '=' || p[1] == p[0])
247698948Sobrien	  *end = p + 2;
247798948Sobrien	else
247898948Sobrien	  *end = p + 1;
247998948Sobrien	return p;
248098948Sobrien      case '~':
248198948Sobrien      case ',':
248298948Sobrien	*end = p + 1;
248398948Sobrien	return p;
248498948Sobrien      case '(':
248598948Sobrien	if (p[1] != ')')
248698948Sobrien	  error ("`operator ()' must be specified without whitespace in `()'");
248798948Sobrien	*end = p + 2;
248898948Sobrien	return p;
248998948Sobrien      case '?':
249098948Sobrien	if (p[1] != ':')
249198948Sobrien	  error ("`operator ?:' must be specified without whitespace in `?:'");
249298948Sobrien	*end = p + 2;
249398948Sobrien	return p;
249498948Sobrien      case '[':
249598948Sobrien	if (p[1] != ']')
249698948Sobrien	  error ("`operator []' must be specified without whitespace in `[]'");
249798948Sobrien	*end = p + 2;
249898948Sobrien	return p;
249998948Sobrien      default:
250098948Sobrien	error ("`operator %s' not supported", p);
250198948Sobrien	break;
250298948Sobrien      }
250398948Sobrien
250419370Spst  *end = "";
250519370Spst  return *end;
250619370Spst}
250798948Sobrien
250819370Spst
250998948Sobrien/* If FILE is not already in the table of files, return zero;
251098948Sobrien   otherwise return non-zero.  Optionally add FILE to the table if ADD
251198948Sobrien   is non-zero.  If *FIRST is non-zero, forget the old table
251298948Sobrien   contents.  */
251319370Spststatic int
251498948Sobrienfilename_seen (const char *file, int add, int *first)
251519370Spst{
251698948Sobrien  /* Table of files seen so far.  */
251798948Sobrien  static const char **tab = NULL;
251898948Sobrien  /* Allocated size of tab in elements.
251998948Sobrien     Start with one 256-byte block (when using GNU malloc.c).
252098948Sobrien     24 is the malloc overhead when range checking is in effect.  */
252198948Sobrien  static int tab_alloc_size = (256 - 24) / sizeof (char *);
252298948Sobrien  /* Current size of tab in elements.  */
252398948Sobrien  static int tab_cur_size;
252498948Sobrien  const char **p;
252519370Spst
252698948Sobrien  if (*first)
252719370Spst    {
252898948Sobrien      if (tab == NULL)
252998948Sobrien	tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab));
253098948Sobrien      tab_cur_size = 0;
253119370Spst    }
253219370Spst
253398948Sobrien  /* Is FILE in tab?  */
253498948Sobrien  for (p = tab; p < tab + tab_cur_size; p++)
253598948Sobrien    if (strcmp (*p, file) == 0)
253698948Sobrien      return 1;
253719370Spst
253898948Sobrien  /* No; maybe add it to tab.  */
253998948Sobrien  if (add)
254019370Spst    {
254198948Sobrien      if (tab_cur_size == tab_alloc_size)
254219370Spst	{
254398948Sobrien	  tab_alloc_size *= 2;
254498948Sobrien	  tab = (const char **) xrealloc ((char *) tab,
254598948Sobrien					  tab_alloc_size * sizeof (*tab));
254646283Sdfr	}
254798948Sobrien      tab[tab_cur_size++] = file;
254819370Spst    }
254919370Spst
255098948Sobrien  return 0;
255119370Spst}
255219370Spst
255319370Spst/* Slave routine for sources_info.  Force line breaks at ,'s.
255419370Spst   NAME is the name to print and *FIRST is nonzero if this is the first
255519370Spst   name printed.  Set *FIRST to zero.  */
255619370Spststatic void
255798948Sobrienoutput_source_filename (char *name, int *first)
255819370Spst{
255998948Sobrien  /* Since a single source file can result in several partial symbol
256098948Sobrien     tables, we need to avoid printing it more than once.  Note: if
256198948Sobrien     some of the psymtabs are read in and some are not, it gets
256298948Sobrien     printed both under "Source files for which symbols have been
256398948Sobrien     read" and "Source files for which symbols will be read in on
256498948Sobrien     demand".  I consider this a reasonable way to deal with the
256598948Sobrien     situation.  I'm not sure whether this can also happen for
256619370Spst     symtabs; it doesn't hurt to check.  */
256719370Spst
256898948Sobrien  /* Was NAME already seen?  */
256998948Sobrien  if (filename_seen (name, 1, first))
257019370Spst    {
257119370Spst      /* Yes; don't print it again.  */
257219370Spst      return;
257319370Spst    }
257498948Sobrien  /* No; print it and reset *FIRST.  */
257519370Spst  if (*first)
257619370Spst    {
257719370Spst      *first = 0;
257819370Spst    }
257919370Spst  else
258019370Spst    {
258119370Spst      printf_filtered (", ");
258219370Spst    }
258319370Spst
258419370Spst  wrap_here ("");
258519370Spst  fputs_filtered (name, gdb_stdout);
258698948Sobrien}
258719370Spst
258819370Spststatic void
258998948Sobriensources_info (char *ignore, int from_tty)
259019370Spst{
2591130809Smarcel  struct symtab *s;
2592130809Smarcel  struct partial_symtab *ps;
2593130809Smarcel  struct objfile *objfile;
259419370Spst  int first;
259598948Sobrien
259619370Spst  if (!have_full_symbols () && !have_partial_symbols ())
259719370Spst    {
259898948Sobrien      error ("No symbol table is loaded.  Use the \"file\" command.");
259919370Spst    }
260098948Sobrien
260119370Spst  printf_filtered ("Source files for which symbols have been read in:\n\n");
260219370Spst
260319370Spst  first = 1;
260419370Spst  ALL_SYMTABS (objfile, s)
260598948Sobrien  {
260698948Sobrien    output_source_filename (s->filename, &first);
260798948Sobrien  }
260819370Spst  printf_filtered ("\n\n");
260998948Sobrien
261019370Spst  printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
261119370Spst
261219370Spst  first = 1;
261319370Spst  ALL_PSYMTABS (objfile, ps)
261498948Sobrien  {
261598948Sobrien    if (!ps->readin)
261698948Sobrien      {
261798948Sobrien	output_source_filename (ps->filename, &first);
261898948Sobrien      }
261998948Sobrien  }
262019370Spst  printf_filtered ("\n");
262119370Spst}
262219370Spst
262346283Sdfrstatic int
262498948Sobrienfile_matches (char *file, char *files[], int nfiles)
262546283Sdfr{
262646283Sdfr  int i;
262719370Spst
262846283Sdfr  if (file != NULL && nfiles != 0)
262946283Sdfr    {
263046283Sdfr      for (i = 0; i < nfiles; i++)
263198948Sobrien	{
263298948Sobrien	  if (strcmp (files[i], lbasename (file)) == 0)
263398948Sobrien	    return 1;
263498948Sobrien	}
263546283Sdfr    }
263646283Sdfr  else if (nfiles == 0)
263746283Sdfr    return 1;
263846283Sdfr  return 0;
263946283Sdfr}
264019370Spst
264146283Sdfr/* Free any memory associated with a search. */
264246283Sdfrvoid
264398948Sobrienfree_search_symbols (struct symbol_search *symbols)
264446283Sdfr{
264546283Sdfr  struct symbol_search *p;
264646283Sdfr  struct symbol_search *next;
264746283Sdfr
264846283Sdfr  for (p = symbols; p != NULL; p = next)
264946283Sdfr    {
265046283Sdfr      next = p->next;
265198948Sobrien      xfree (p);
265246283Sdfr    }
265346283Sdfr}
265446283Sdfr
265598948Sobrienstatic void
265698948Sobriendo_free_search_symbols_cleanup (void *symbols)
265798948Sobrien{
265898948Sobrien  free_search_symbols (symbols);
265998948Sobrien}
266098948Sobrien
266198948Sobrienstruct cleanup *
266298948Sobrienmake_cleanup_free_search_symbols (struct symbol_search *symbols)
266398948Sobrien{
266498948Sobrien  return make_cleanup (do_free_search_symbols_cleanup, symbols);
266598948Sobrien}
266698948Sobrien
266798948Sobrien/* Helper function for sort_search_symbols and qsort.  Can only
266898948Sobrien   sort symbols, not minimal symbols.  */
266998948Sobrienstatic int
267098948Sobriencompare_search_syms (const void *sa, const void *sb)
267198948Sobrien{
267298948Sobrien  struct symbol_search **sym_a = (struct symbol_search **) sa;
267398948Sobrien  struct symbol_search **sym_b = (struct symbol_search **) sb;
267498948Sobrien
2675130809Smarcel  return strcmp (SYMBOL_PRINT_NAME ((*sym_a)->symbol),
2676130809Smarcel		 SYMBOL_PRINT_NAME ((*sym_b)->symbol));
267798948Sobrien}
267898948Sobrien
267998948Sobrien/* Sort the ``nfound'' symbols in the list after prevtail.  Leave
268098948Sobrien   prevtail where it is, but update its next pointer to point to
268198948Sobrien   the first of the sorted symbols.  */
268298948Sobrienstatic struct symbol_search *
268398948Sobriensort_search_symbols (struct symbol_search *prevtail, int nfound)
268498948Sobrien{
268598948Sobrien  struct symbol_search **symbols, *symp, *old_next;
268698948Sobrien  int i;
268798948Sobrien
268898948Sobrien  symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
268998948Sobrien					       * nfound);
269098948Sobrien  symp = prevtail->next;
269198948Sobrien  for (i = 0; i < nfound; i++)
269298948Sobrien    {
269398948Sobrien      symbols[i] = symp;
269498948Sobrien      symp = symp->next;
269598948Sobrien    }
269698948Sobrien  /* Generally NULL.  */
269798948Sobrien  old_next = symp;
269898948Sobrien
269998948Sobrien  qsort (symbols, nfound, sizeof (struct symbol_search *),
270098948Sobrien	 compare_search_syms);
270198948Sobrien
270298948Sobrien  symp = prevtail;
270398948Sobrien  for (i = 0; i < nfound; i++)
270498948Sobrien    {
270598948Sobrien      symp->next = symbols[i];
270698948Sobrien      symp = symp->next;
270798948Sobrien    }
270898948Sobrien  symp->next = old_next;
270998948Sobrien
271098948Sobrien  xfree (symbols);
271198948Sobrien  return symp;
271298948Sobrien}
271398948Sobrien
271446283Sdfr/* Search the symbol table for matches to the regular expression REGEXP,
271546283Sdfr   returning the results in *MATCHES.
271646283Sdfr
271746283Sdfr   Only symbols of KIND are searched:
2718130809Smarcel   FUNCTIONS_DOMAIN - search all functions
2719130809Smarcel   TYPES_DOMAIN     - search all type names
2720130809Smarcel   METHODS_DOMAIN   - search all methods NOT IMPLEMENTED
2721130809Smarcel   VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
272298948Sobrien   and constants (enums)
272346283Sdfr
272446283Sdfr   free_search_symbols should be called when *MATCHES is no longer needed.
272598948Sobrien
272698948Sobrien   The results are sorted locally; each symtab's global and static blocks are
272798948Sobrien   separately alphabetized.
272898948Sobrien */
272946283Sdfrvoid
2730130809Smarcelsearch_symbols (char *regexp, domain_enum kind, int nfiles, char *files[],
273198948Sobrien		struct symbol_search **matches)
273219370Spst{
2733130809Smarcel  struct symtab *s;
2734130809Smarcel  struct partial_symtab *ps;
2735130809Smarcel  struct blockvector *bv;
273619370Spst  struct blockvector *prev_bv = 0;
2737130809Smarcel  struct block *b;
2738130809Smarcel  int i = 0;
2739130809Smarcel  struct dict_iterator iter;
2740130809Smarcel  struct symbol *sym;
274119370Spst  struct partial_symbol **psym;
274219370Spst  struct objfile *objfile;
274319370Spst  struct minimal_symbol *msymbol;
274419370Spst  char *val;
274519370Spst  int found_misc = 0;
274619370Spst  static enum minimal_symbol_type types[]
274798948Sobrien  =
274898948Sobrien  {mst_data, mst_text, mst_abs, mst_unknown};
274919370Spst  static enum minimal_symbol_type types2[]
275098948Sobrien  =
275198948Sobrien  {mst_bss, mst_file_text, mst_abs, mst_unknown};
275219370Spst  static enum minimal_symbol_type types3[]
275398948Sobrien  =
275498948Sobrien  {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
275519370Spst  static enum minimal_symbol_type types4[]
275698948Sobrien  =
275798948Sobrien  {mst_file_bss, mst_text, mst_abs, mst_unknown};
275846283Sdfr  enum minimal_symbol_type ourtype;
275946283Sdfr  enum minimal_symbol_type ourtype2;
276046283Sdfr  enum minimal_symbol_type ourtype3;
276146283Sdfr  enum minimal_symbol_type ourtype4;
276246283Sdfr  struct symbol_search *sr;
276346283Sdfr  struct symbol_search *psr;
276446283Sdfr  struct symbol_search *tail;
276546283Sdfr  struct cleanup *old_chain = NULL;
276619370Spst
2767130809Smarcel  if (kind < VARIABLES_DOMAIN)
2768130809Smarcel    error ("must search on specific domain");
276946283Sdfr
2770130809Smarcel  ourtype = types[(int) (kind - VARIABLES_DOMAIN)];
2771130809Smarcel  ourtype2 = types2[(int) (kind - VARIABLES_DOMAIN)];
2772130809Smarcel  ourtype3 = types3[(int) (kind - VARIABLES_DOMAIN)];
2773130809Smarcel  ourtype4 = types4[(int) (kind - VARIABLES_DOMAIN)];
277446283Sdfr
277546283Sdfr  sr = *matches = NULL;
277646283Sdfr  tail = NULL;
277746283Sdfr
277819370Spst  if (regexp != NULL)
277919370Spst    {
278019370Spst      /* Make sure spacing is right for C++ operators.
278146283Sdfr         This is just a courtesy to make the matching less sensitive
278246283Sdfr         to how many spaces the user leaves between 'operator'
278346283Sdfr         and <TYPENAME> or <OPERATOR>. */
278419370Spst      char *opend;
278519370Spst      char *opname = operator_chars (regexp, &opend);
278619370Spst      if (*opname)
278798948Sobrien	{
278898948Sobrien	  int fix = -1;		/* -1 means ok; otherwise number of spaces needed. */
278998948Sobrien	  if (isalpha (*opname) || *opname == '_' || *opname == '$')
279098948Sobrien	    {
279198948Sobrien	      /* There should 1 space between 'operator' and 'TYPENAME'. */
279298948Sobrien	      if (opname[-1] != ' ' || opname[-2] == ' ')
279398948Sobrien		fix = 1;
279498948Sobrien	    }
279598948Sobrien	  else
279698948Sobrien	    {
279798948Sobrien	      /* There should 0 spaces between 'operator' and 'OPERATOR'. */
279898948Sobrien	      if (opname[-1] == ' ')
279998948Sobrien		fix = 0;
280098948Sobrien	    }
280198948Sobrien	  /* If wrong number of spaces, fix it. */
280298948Sobrien	  if (fix >= 0)
280398948Sobrien	    {
280498948Sobrien	      char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
280598948Sobrien	      sprintf (tmp, "operator%.*s%s", fix, " ", opname);
280698948Sobrien	      regexp = tmp;
280798948Sobrien	    }
280898948Sobrien	}
280998948Sobrien
281019370Spst      if (0 != (val = re_comp (regexp)))
281198948Sobrien	error ("Invalid regexp (%s): %s", val, regexp);
281219370Spst    }
281319370Spst
281419370Spst  /* Search through the partial symtabs *first* for all symbols
281519370Spst     matching the regexp.  That way we don't have to reproduce all of
281619370Spst     the machinery below. */
281719370Spst
281819370Spst  ALL_PSYMTABS (objfile, ps)
281998948Sobrien  {
282098948Sobrien    struct partial_symbol **bound, **gbound, **sbound;
282198948Sobrien    int keep_going = 1;
282246283Sdfr
282398948Sobrien    if (ps->readin)
282498948Sobrien      continue;
282546283Sdfr
282698948Sobrien    gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
282798948Sobrien    sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
282898948Sobrien    bound = gbound;
282919370Spst
283098948Sobrien    /* Go through all of the symbols stored in a partial
283198948Sobrien       symtab in one loop. */
283298948Sobrien    psym = objfile->global_psymbols.list + ps->globals_offset;
283398948Sobrien    while (keep_going)
283498948Sobrien      {
283598948Sobrien	if (psym >= bound)
283698948Sobrien	  {
283798948Sobrien	    if (bound == gbound && ps->n_static_syms != 0)
283898948Sobrien	      {
283998948Sobrien		psym = objfile->static_psymbols.list + ps->statics_offset;
284098948Sobrien		bound = sbound;
284198948Sobrien	      }
284298948Sobrien	    else
284398948Sobrien	      keep_going = 0;
284498948Sobrien	    continue;
284598948Sobrien	  }
284698948Sobrien	else
284798948Sobrien	  {
284898948Sobrien	    QUIT;
284919370Spst
285098948Sobrien	    /* If it would match (logic taken from loop below)
285198948Sobrien	       load the file and go on to the next one */
285298948Sobrien	    if (file_matches (ps->filename, files, nfiles)
2853130809Smarcel		&& ((regexp == NULL
2854130809Smarcel		     || re_exec (SYMBOL_NATURAL_NAME (*psym)) != 0)
2855130809Smarcel		    && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
285698948Sobrien			 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2857130809Smarcel			|| (kind == FUNCTIONS_DOMAIN && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2858130809Smarcel			|| (kind == TYPES_DOMAIN && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2859130809Smarcel			|| (kind == METHODS_DOMAIN && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
286098948Sobrien	      {
286198948Sobrien		PSYMTAB_TO_SYMTAB (ps);
286298948Sobrien		keep_going = 0;
286398948Sobrien	      }
286498948Sobrien	  }
286598948Sobrien	psym++;
286698948Sobrien      }
286798948Sobrien  }
286898948Sobrien
286919370Spst  /* Here, we search through the minimal symbol tables for functions
287019370Spst     and variables that match, and force their symbols to be read.
287119370Spst     This is in particular necessary for demangled variable names,
287219370Spst     which are no longer put into the partial symbol tables.
287319370Spst     The symbol will then be found during the scan of symtabs below.
287419370Spst
287519370Spst     For functions, find_pc_symtab should succeed if we have debug info
287619370Spst     for the function, for variables we have to call lookup_symbol
287719370Spst     to determine if the variable has debug info.
287819370Spst     If the lookup fails, set found_misc so that we will rescan to print
287919370Spst     any matching symbols without debug info.
288098948Sobrien   */
288119370Spst
2882130809Smarcel  if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
288319370Spst    {
288419370Spst      ALL_MSYMBOLS (objfile, msymbol)
288598948Sobrien      {
288698948Sobrien	if (MSYMBOL_TYPE (msymbol) == ourtype ||
288798948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype2 ||
288898948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype3 ||
288998948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype4)
289098948Sobrien	  {
2891130809Smarcel	    if (regexp == NULL
2892130809Smarcel		|| re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
289398948Sobrien	      {
289498948Sobrien		if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
289598948Sobrien		  {
2896130809Smarcel		    /* FIXME: carlton/2003-02-04: Given that the
2897130809Smarcel		       semantics of lookup_symbol keeps on changing
2898130809Smarcel		       slightly, it would be a nice idea if we had a
2899130809Smarcel		       function lookup_symbol_minsym that found the
2900130809Smarcel		       symbol associated to a given minimal symbol (if
2901130809Smarcel		       any).  */
2902130809Smarcel		    if (kind == FUNCTIONS_DOMAIN
2903130809Smarcel			|| lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
290498948Sobrien					  (struct block *) NULL,
2905130809Smarcel					  VAR_DOMAIN,
290698948Sobrien					0, (struct symtab **) NULL) == NULL)
290798948Sobrien		      found_misc = 1;
290898948Sobrien		  }
290998948Sobrien	      }
291098948Sobrien	  }
291198948Sobrien      }
291219370Spst    }
291319370Spst
291419370Spst  ALL_SYMTABS (objfile, s)
291598948Sobrien  {
291698948Sobrien    bv = BLOCKVECTOR (s);
291798948Sobrien    /* Often many files share a blockvector.
291898948Sobrien       Scan each blockvector only once so that
291998948Sobrien       we don't get every symbol many times.
292098948Sobrien       It happens that the first symtab in the list
292198948Sobrien       for any given blockvector is the main file.  */
292298948Sobrien    if (bv != prev_bv)
292398948Sobrien      for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
292498948Sobrien	{
292598948Sobrien	  struct symbol_search *prevtail = tail;
292698948Sobrien	  int nfound = 0;
292798948Sobrien	  b = BLOCKVECTOR_BLOCK (bv, i);
2928130809Smarcel	  ALL_BLOCK_SYMBOLS (b, iter, sym)
292998948Sobrien	    {
293098948Sobrien	      QUIT;
293198948Sobrien	      if (file_matches (s->filename, files, nfiles)
2932130809Smarcel		  && ((regexp == NULL
2933130809Smarcel		       || re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
2934130809Smarcel		      && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (sym) != LOC_TYPEDEF
293598948Sobrien			   && SYMBOL_CLASS (sym) != LOC_BLOCK
293698948Sobrien			   && SYMBOL_CLASS (sym) != LOC_CONST)
2937130809Smarcel			  || (kind == FUNCTIONS_DOMAIN && SYMBOL_CLASS (sym) == LOC_BLOCK)
2938130809Smarcel			  || (kind == TYPES_DOMAIN && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2939130809Smarcel			  || (kind == METHODS_DOMAIN && SYMBOL_CLASS (sym) == LOC_BLOCK))))
294098948Sobrien		{
294198948Sobrien		  /* match */
294298948Sobrien		  psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
294398948Sobrien		  psr->block = i;
294498948Sobrien		  psr->symtab = s;
294598948Sobrien		  psr->symbol = sym;
294698948Sobrien		  psr->msymbol = NULL;
294798948Sobrien		  psr->next = NULL;
294898948Sobrien		  if (tail == NULL)
294998948Sobrien		    sr = psr;
295098948Sobrien		  else
295198948Sobrien		    tail->next = psr;
295298948Sobrien		  tail = psr;
295398948Sobrien		  nfound ++;
295498948Sobrien		}
295598948Sobrien	    }
295698948Sobrien	  if (nfound > 0)
295798948Sobrien	    {
295898948Sobrien	      if (prevtail == NULL)
295998948Sobrien		{
296098948Sobrien		  struct symbol_search dummy;
296119370Spst
296298948Sobrien		  dummy.next = sr;
296398948Sobrien		  tail = sort_search_symbols (&dummy, nfound);
296498948Sobrien		  sr = dummy.next;
296598948Sobrien
296698948Sobrien		  old_chain = make_cleanup_free_search_symbols (sr);
296798948Sobrien		}
296898948Sobrien	      else
296998948Sobrien		tail = sort_search_symbols (prevtail, nfound);
297098948Sobrien	    }
297198948Sobrien	}
297298948Sobrien    prev_bv = bv;
297398948Sobrien  }
297498948Sobrien
297519370Spst  /* If there are no eyes, avoid all contact.  I mean, if there are
297619370Spst     no debug symbols, then print directly from the msymbol_vector.  */
297719370Spst
2978130809Smarcel  if (found_misc || kind != FUNCTIONS_DOMAIN)
297919370Spst    {
298019370Spst      ALL_MSYMBOLS (objfile, msymbol)
298198948Sobrien      {
298298948Sobrien	if (MSYMBOL_TYPE (msymbol) == ourtype ||
298398948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype2 ||
298498948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype3 ||
298598948Sobrien	    MSYMBOL_TYPE (msymbol) == ourtype4)
298698948Sobrien	  {
2987130809Smarcel	    if (regexp == NULL
2988130809Smarcel		|| re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
298998948Sobrien	      {
299098948Sobrien		/* Functions:  Look up by address. */
2991130809Smarcel		if (kind != FUNCTIONS_DOMAIN ||
299298948Sobrien		    (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
299398948Sobrien		  {
299498948Sobrien		    /* Variables/Absolutes:  Look up by name */
2995130809Smarcel		    if (lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
2996130809Smarcel				       (struct block *) NULL, VAR_DOMAIN,
299798948Sobrien				       0, (struct symtab **) NULL) == NULL)
299898948Sobrien		      {
299998948Sobrien			/* match */
300098948Sobrien			psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
300198948Sobrien			psr->block = i;
300298948Sobrien			psr->msymbol = msymbol;
300398948Sobrien			psr->symtab = NULL;
300498948Sobrien			psr->symbol = NULL;
300598948Sobrien			psr->next = NULL;
300698948Sobrien			if (tail == NULL)
300798948Sobrien			  {
300898948Sobrien			    sr = psr;
300998948Sobrien			    old_chain = make_cleanup_free_search_symbols (sr);
301098948Sobrien			  }
301198948Sobrien			else
301298948Sobrien			  tail->next = psr;
301398948Sobrien			tail = psr;
301498948Sobrien		      }
301598948Sobrien		  }
301698948Sobrien	      }
301798948Sobrien	  }
301898948Sobrien      }
301919370Spst    }
302046283Sdfr
302146283Sdfr  *matches = sr;
302246283Sdfr  if (sr != NULL)
302346283Sdfr    discard_cleanups (old_chain);
302419370Spst}
302519370Spst
302646283Sdfr/* Helper function for symtab_symbol_info, this function uses
302746283Sdfr   the data returned from search_symbols() to print information
302846283Sdfr   regarding the match to gdb_stdout.
302998948Sobrien */
303019370Spststatic void
3031130809Smarcelprint_symbol_info (domain_enum kind, struct symtab *s, struct symbol *sym,
303298948Sobrien		   int block, char *last)
303346283Sdfr{
303446283Sdfr  if (last == NULL || strcmp (last, s->filename) != 0)
303546283Sdfr    {
303646283Sdfr      fputs_filtered ("\nFile ", gdb_stdout);
303746283Sdfr      fputs_filtered (s->filename, gdb_stdout);
303846283Sdfr      fputs_filtered (":\n", gdb_stdout);
303946283Sdfr    }
304046283Sdfr
3041130809Smarcel  if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
304246283Sdfr    printf_filtered ("static ");
304398948Sobrien
304446283Sdfr  /* Typedef that is not a C++ class */
3045130809Smarcel  if (kind == TYPES_DOMAIN
3046130809Smarcel      && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
304798948Sobrien    typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
304846283Sdfr  /* variable, func, or typedef-that-is-c++-class */
3049130809Smarcel  else if (kind < TYPES_DOMAIN ||
3050130809Smarcel	   (kind == TYPES_DOMAIN &&
3051130809Smarcel	    SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
305246283Sdfr    {
305346283Sdfr      type_print (SYMBOL_TYPE (sym),
305498948Sobrien		  (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3055130809Smarcel		   ? "" : SYMBOL_PRINT_NAME (sym)),
305698948Sobrien		  gdb_stdout, 0);
305746283Sdfr
305846283Sdfr      printf_filtered (";\n");
305946283Sdfr    }
306046283Sdfr}
306146283Sdfr
306246283Sdfr/* This help function for symtab_symbol_info() prints information
306346283Sdfr   for non-debugging symbols to gdb_stdout.
306498948Sobrien */
306546283Sdfrstatic void
306698948Sobrienprint_msymbol_info (struct minimal_symbol *msymbol)
306746283Sdfr{
306898948Sobrien  char *tmp;
306998948Sobrien
307098948Sobrien  if (TARGET_ADDR_BIT <= 32)
3071130809Smarcel    tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3072130809Smarcel				   & (CORE_ADDR) 0xffffffff,
3073130809Smarcel				   "08l");
307498948Sobrien  else
3075130809Smarcel    tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3076130809Smarcel				   "016l");
307798948Sobrien  printf_filtered ("%s  %s\n",
3078130809Smarcel		   tmp, SYMBOL_PRINT_NAME (msymbol));
307946283Sdfr}
308046283Sdfr
308146283Sdfr/* This is the guts of the commands "info functions", "info types", and
308246283Sdfr   "info variables". It calls search_symbols to find all matches and then
308346283Sdfr   print_[m]symbol_info to print out some useful information about the
308446283Sdfr   matches.
308598948Sobrien */
308646283Sdfrstatic void
3087130809Smarcelsymtab_symbol_info (char *regexp, domain_enum kind, int from_tty)
308846283Sdfr{
308946283Sdfr  static char *classnames[]
309098948Sobrien  =
309198948Sobrien  {"variable", "function", "type", "method"};
309246283Sdfr  struct symbol_search *symbols;
309346283Sdfr  struct symbol_search *p;
309446283Sdfr  struct cleanup *old_chain;
309546283Sdfr  char *last_filename = NULL;
309646283Sdfr  int first = 1;
309746283Sdfr
309846283Sdfr  /* must make sure that if we're interrupted, symbols gets freed */
309946283Sdfr  search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
310098948Sobrien  old_chain = make_cleanup_free_search_symbols (symbols);
310146283Sdfr
310246283Sdfr  printf_filtered (regexp
310398948Sobrien		   ? "All %ss matching regular expression \"%s\":\n"
310498948Sobrien		   : "All defined %ss:\n",
3105130809Smarcel		   classnames[(int) (kind - VARIABLES_DOMAIN)], regexp);
310646283Sdfr
310746283Sdfr  for (p = symbols; p != NULL; p = p->next)
310846283Sdfr    {
310946283Sdfr      QUIT;
311046283Sdfr
311146283Sdfr      if (p->msymbol != NULL)
311298948Sobrien	{
311398948Sobrien	  if (first)
311498948Sobrien	    {
311598948Sobrien	      printf_filtered ("\nNon-debugging symbols:\n");
311698948Sobrien	      first = 0;
311798948Sobrien	    }
311898948Sobrien	  print_msymbol_info (p->msymbol);
311998948Sobrien	}
312046283Sdfr      else
312198948Sobrien	{
312298948Sobrien	  print_symbol_info (kind,
312398948Sobrien			     p->symtab,
312498948Sobrien			     p->symbol,
312598948Sobrien			     p->block,
312698948Sobrien			     last_filename);
312798948Sobrien	  last_filename = p->symtab->filename;
312898948Sobrien	}
312946283Sdfr    }
313046283Sdfr
313146283Sdfr  do_cleanups (old_chain);
313246283Sdfr}
313346283Sdfr
313446283Sdfrstatic void
313598948Sobrienvariables_info (char *regexp, int from_tty)
313619370Spst{
3137130809Smarcel  symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
313819370Spst}
313919370Spst
314019370Spststatic void
314198948Sobrienfunctions_info (char *regexp, int from_tty)
314219370Spst{
3143130809Smarcel  symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
314419370Spst}
314519370Spst
314698948Sobrien
314719370Spststatic void
314898948Sobrientypes_info (char *regexp, int from_tty)
314919370Spst{
3150130809Smarcel  symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
315119370Spst}
315219370Spst
315319370Spst/* Breakpoint all functions matching regular expression. */
315498948Sobrien
315598948Sobrienvoid
315698948Sobrienrbreak_command_wrapper (char *regexp, int from_tty)
315798948Sobrien{
315898948Sobrien  rbreak_command (regexp, from_tty);
315998948Sobrien}
316098948Sobrien
316119370Spststatic void
316298948Sobrienrbreak_command (char *regexp, int from_tty)
316319370Spst{
316446283Sdfr  struct symbol_search *ss;
316546283Sdfr  struct symbol_search *p;
316646283Sdfr  struct cleanup *old_chain;
316746283Sdfr
3168130809Smarcel  search_symbols (regexp, FUNCTIONS_DOMAIN, 0, (char **) NULL, &ss);
316998948Sobrien  old_chain = make_cleanup_free_search_symbols (ss);
317046283Sdfr
317146283Sdfr  for (p = ss; p != NULL; p = p->next)
317246283Sdfr    {
317346283Sdfr      if (p->msymbol == NULL)
317498948Sobrien	{
3175130809Smarcel	  char *string = alloca (strlen (p->symtab->filename)
3176130809Smarcel				 + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
3177130809Smarcel				 + 4);
317898948Sobrien	  strcpy (string, p->symtab->filename);
317998948Sobrien	  strcat (string, ":'");
3180130809Smarcel	  strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
318198948Sobrien	  strcat (string, "'");
318298948Sobrien	  break_command (string, from_tty);
3183130809Smarcel	  print_symbol_info (FUNCTIONS_DOMAIN,
318498948Sobrien			     p->symtab,
318598948Sobrien			     p->symbol,
318698948Sobrien			     p->block,
318798948Sobrien			     p->symtab->filename);
318898948Sobrien	}
318946283Sdfr      else
319098948Sobrien	{
3191130809Smarcel	  break_command (SYMBOL_LINKAGE_NAME (p->msymbol), from_tty);
319298948Sobrien	  printf_filtered ("<function, no debug info> %s;\n",
3193130809Smarcel			   SYMBOL_PRINT_NAME (p->msymbol));
319498948Sobrien	}
319546283Sdfr    }
319646283Sdfr
319746283Sdfr  do_cleanups (old_chain);
319819370Spst}
319998948Sobrien
320046283Sdfr
320119370Spst/* Helper routine for make_symbol_completion_list.  */
320219370Spst
320319370Spststatic int return_val_size;
320419370Spststatic int return_val_index;
320519370Spststatic char **return_val;
320619370Spst
320719370Spst#define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
320819370Spst      completion_list_add_name \
3209130809Smarcel	(SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
321019370Spst
321119370Spst/*  Test to see if the symbol specified by SYMNAME (which is already
321298948Sobrien   demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
321398948Sobrien   characters.  If so, add it to the current completion list. */
321419370Spst
321519370Spststatic void
321698948Sobriencompletion_list_add_name (char *symname, char *sym_text, int sym_text_len,
321798948Sobrien			  char *text, char *word)
321819370Spst{
321919370Spst  int newsize;
322019370Spst  int i;
322119370Spst
322219370Spst  /* clip symbols that cannot match */
322319370Spst
322419370Spst  if (strncmp (symname, sym_text, sym_text_len) != 0)
322519370Spst    {
322619370Spst      return;
322719370Spst    }
322819370Spst
322919370Spst  /* We have a match for a completion, so add SYMNAME to the current list
323019370Spst     of matches. Note that the name is moved to freshly malloc'd space. */
323119370Spst
323219370Spst  {
323319370Spst    char *new;
323419370Spst    if (word == sym_text)
323519370Spst      {
323619370Spst	new = xmalloc (strlen (symname) + 5);
323719370Spst	strcpy (new, symname);
323819370Spst      }
323919370Spst    else if (word > sym_text)
324019370Spst      {
324119370Spst	/* Return some portion of symname.  */
324219370Spst	new = xmalloc (strlen (symname) + 5);
324319370Spst	strcpy (new, symname + (word - sym_text));
324419370Spst      }
324519370Spst    else
324619370Spst      {
324719370Spst	/* Return some of SYM_TEXT plus symname.  */
324819370Spst	new = xmalloc (strlen (symname) + (sym_text - word) + 5);
324919370Spst	strncpy (new, word, sym_text - word);
325019370Spst	new[sym_text - word] = '\0';
325119370Spst	strcat (new, symname);
325219370Spst      }
325319370Spst
325419370Spst    if (return_val_index + 3 > return_val_size)
325519370Spst      {
325619370Spst	newsize = (return_val_size *= 2) * sizeof (char *);
325719370Spst	return_val = (char **) xrealloc ((char *) return_val, newsize);
325819370Spst      }
325919370Spst    return_val[return_val_index++] = new;
326019370Spst    return_val[return_val_index] = NULL;
326119370Spst  }
326219370Spst}
326319370Spst
3264130809Smarcel/* ObjC: In case we are completing on a selector, look as the msymbol
3265130809Smarcel   again and feed all the selectors into the mill.  */
3266130809Smarcel
3267130809Smarcelstatic void
3268130809Smarcelcompletion_list_objc_symbol (struct minimal_symbol *msymbol, char *sym_text,
3269130809Smarcel			     int sym_text_len, char *text, char *word)
3270130809Smarcel{
3271130809Smarcel  static char *tmp = NULL;
3272130809Smarcel  static unsigned int tmplen = 0;
3273130809Smarcel
3274130809Smarcel  char *method, *category, *selector;
3275130809Smarcel  char *tmp2 = NULL;
3276130809Smarcel
3277130809Smarcel  method = SYMBOL_NATURAL_NAME (msymbol);
3278130809Smarcel
3279130809Smarcel  /* Is it a method?  */
3280130809Smarcel  if ((method[0] != '-') && (method[0] != '+'))
3281130809Smarcel    return;
3282130809Smarcel
3283130809Smarcel  if (sym_text[0] == '[')
3284130809Smarcel    /* Complete on shortened method method.  */
3285130809Smarcel    completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);
3286130809Smarcel
3287130809Smarcel  while ((strlen (method) + 1) >= tmplen)
3288130809Smarcel    {
3289130809Smarcel      if (tmplen == 0)
3290130809Smarcel	tmplen = 1024;
3291130809Smarcel      else
3292130809Smarcel	tmplen *= 2;
3293130809Smarcel      tmp = xrealloc (tmp, tmplen);
3294130809Smarcel    }
3295130809Smarcel  selector = strchr (method, ' ');
3296130809Smarcel  if (selector != NULL)
3297130809Smarcel    selector++;
3298130809Smarcel
3299130809Smarcel  category = strchr (method, '(');
3300130809Smarcel
3301130809Smarcel  if ((category != NULL) && (selector != NULL))
3302130809Smarcel    {
3303130809Smarcel      memcpy (tmp, method, (category - method));
3304130809Smarcel      tmp[category - method] = ' ';
3305130809Smarcel      memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
3306130809Smarcel      completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
3307130809Smarcel      if (sym_text[0] == '[')
3308130809Smarcel	completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
3309130809Smarcel    }
3310130809Smarcel
3311130809Smarcel  if (selector != NULL)
3312130809Smarcel    {
3313130809Smarcel      /* Complete on selector only.  */
3314130809Smarcel      strcpy (tmp, selector);
3315130809Smarcel      tmp2 = strchr (tmp, ']');
3316130809Smarcel      if (tmp2 != NULL)
3317130809Smarcel	*tmp2 = '\0';
3318130809Smarcel
3319130809Smarcel      completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
3320130809Smarcel    }
3321130809Smarcel}
3322130809Smarcel
3323130809Smarcel/* Break the non-quoted text based on the characters which are in
3324130809Smarcel   symbols. FIXME: This should probably be language-specific. */
3325130809Smarcel
3326130809Smarcelstatic char *
3327130809Smarcellanguage_search_unquoted_string (char *text, char *p)
3328130809Smarcel{
3329130809Smarcel  for (; p > text; --p)
3330130809Smarcel    {
3331130809Smarcel      if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3332130809Smarcel	continue;
3333130809Smarcel      else
3334130809Smarcel	{
3335130809Smarcel	  if ((current_language->la_language == language_objc))
3336130809Smarcel	    {
3337130809Smarcel	      if (p[-1] == ':')     /* might be part of a method name */
3338130809Smarcel		continue;
3339130809Smarcel	      else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
3340130809Smarcel		p -= 2;             /* beginning of a method name */
3341130809Smarcel	      else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
3342130809Smarcel		{                   /* might be part of a method name */
3343130809Smarcel		  char *t = p;
3344130809Smarcel
3345130809Smarcel		  /* Seeing a ' ' or a '(' is not conclusive evidence
3346130809Smarcel		     that we are in the middle of a method name.  However,
3347130809Smarcel		     finding "-[" or "+[" should be pretty un-ambiguous.
3348130809Smarcel		     Unfortunately we have to find it now to decide.  */
3349130809Smarcel
3350130809Smarcel		  while (t > text)
3351130809Smarcel		    if (isalnum (t[-1]) || t[-1] == '_' ||
3352130809Smarcel			t[-1] == ' '    || t[-1] == ':' ||
3353130809Smarcel			t[-1] == '('    || t[-1] == ')')
3354130809Smarcel		      --t;
3355130809Smarcel		    else
3356130809Smarcel		      break;
3357130809Smarcel
3358130809Smarcel		  if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
3359130809Smarcel		    p = t - 2;      /* method name detected */
3360130809Smarcel		  /* else we leave with p unchanged */
3361130809Smarcel		}
3362130809Smarcel	    }
3363130809Smarcel	  break;
3364130809Smarcel	}
3365130809Smarcel    }
3366130809Smarcel  return p;
3367130809Smarcel}
3368130809Smarcel
3369130809Smarcel
337098948Sobrien/* Return a NULL terminated array of all symbols (regardless of class)
337198948Sobrien   which begin by matching TEXT.  If the answer is no symbols, then
337298948Sobrien   the return value is an array which contains only a NULL pointer.
337319370Spst
337419370Spst   Problem: All of the symbols have to be copied because readline frees them.
337519370Spst   I'm not going to worry about this; hopefully there won't be that many.  */
337619370Spst
337719370Spstchar **
337898948Sobrienmake_symbol_completion_list (char *text, char *word)
337919370Spst{
3380130809Smarcel  struct symbol *sym;
3381130809Smarcel  struct symtab *s;
3382130809Smarcel  struct partial_symtab *ps;
3383130809Smarcel  struct minimal_symbol *msymbol;
3384130809Smarcel  struct objfile *objfile;
3385130809Smarcel  struct block *b, *surrounding_static_block = 0;
3386130809Smarcel  struct dict_iterator iter;
3387130809Smarcel  int j;
338819370Spst  struct partial_symbol **psym;
338919370Spst  /* The symbol we are completing on.  Points in same buffer as text.  */
339019370Spst  char *sym_text;
339119370Spst  /* Length of sym_text.  */
339219370Spst  int sym_text_len;
339319370Spst
339419370Spst  /* Now look for the symbol we are supposed to complete on.
339519370Spst     FIXME: This should be language-specific.  */
339619370Spst  {
339719370Spst    char *p;
339819370Spst    char quote_found;
339919370Spst    char *quote_pos = NULL;
340019370Spst
340119370Spst    /* First see if this is a quoted string.  */
340219370Spst    quote_found = '\0';
340319370Spst    for (p = text; *p != '\0'; ++p)
340419370Spst      {
340519370Spst	if (quote_found != '\0')
340619370Spst	  {
340719370Spst	    if (*p == quote_found)
340819370Spst	      /* Found close quote.  */
340919370Spst	      quote_found = '\0';
341019370Spst	    else if (*p == '\\' && p[1] == quote_found)
341119370Spst	      /* A backslash followed by the quote character
341298948Sobrien	         doesn't end the string.  */
341319370Spst	      ++p;
341419370Spst	  }
341519370Spst	else if (*p == '\'' || *p == '"')
341619370Spst	  {
341719370Spst	    quote_found = *p;
341819370Spst	    quote_pos = p;
341919370Spst	  }
342019370Spst      }
342119370Spst    if (quote_found == '\'')
342219370Spst      /* A string within single quotes can be a symbol, so complete on it.  */
342319370Spst      sym_text = quote_pos + 1;
342419370Spst    else if (quote_found == '"')
342519370Spst      /* A double-quoted string is never a symbol, nor does it make sense
342698948Sobrien         to complete it any other way.  */
342798948Sobrien      {
342898948Sobrien	return_val = (char **) xmalloc (sizeof (char *));
342998948Sobrien	return_val[0] = NULL;
343098948Sobrien	return return_val;
343198948Sobrien      }
343219370Spst    else
343319370Spst      {
343419370Spst	/* It is not a quoted string.  Break it based on the characters
343519370Spst	   which are in symbols.  */
343619370Spst	while (p > text)
343719370Spst	  {
343819370Spst	    if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
343919370Spst	      --p;
344019370Spst	    else
344119370Spst	      break;
344219370Spst	  }
344319370Spst	sym_text = p;
344419370Spst      }
344519370Spst  }
344619370Spst
344719370Spst  sym_text_len = strlen (sym_text);
344819370Spst
344919370Spst  return_val_size = 100;
345019370Spst  return_val_index = 0;
345119370Spst  return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
345219370Spst  return_val[0] = NULL;
345319370Spst
345419370Spst  /* Look through the partial symtabs for all symbols which begin
345519370Spst     by matching SYM_TEXT.  Add each one that you find to the list.  */
345619370Spst
345719370Spst  ALL_PSYMTABS (objfile, ps)
345898948Sobrien  {
345998948Sobrien    /* If the psymtab's been read in we'll get it when we search
346098948Sobrien       through the blockvector.  */
346198948Sobrien    if (ps->readin)
346298948Sobrien      continue;
346319370Spst
346498948Sobrien    for (psym = objfile->global_psymbols.list + ps->globals_offset;
346598948Sobrien	 psym < (objfile->global_psymbols.list + ps->globals_offset
346698948Sobrien		 + ps->n_global_syms);
346798948Sobrien	 psym++)
346898948Sobrien      {
346998948Sobrien	/* If interrupted, then quit. */
347098948Sobrien	QUIT;
347198948Sobrien	COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
347298948Sobrien      }
347398948Sobrien
347498948Sobrien    for (psym = objfile->static_psymbols.list + ps->statics_offset;
347598948Sobrien	 psym < (objfile->static_psymbols.list + ps->statics_offset
347698948Sobrien		 + ps->n_static_syms);
347798948Sobrien	 psym++)
347898948Sobrien      {
347998948Sobrien	QUIT;
348098948Sobrien	COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
348198948Sobrien      }
348298948Sobrien  }
348398948Sobrien
348419370Spst  /* At this point scan through the misc symbol vectors and add each
348519370Spst     symbol you find to the list.  Eventually we want to ignore
348619370Spst     anything that isn't a text symbol (everything else will be
348719370Spst     handled by the psymtab code above).  */
348819370Spst
348919370Spst  ALL_MSYMBOLS (objfile, msymbol)
349098948Sobrien  {
349198948Sobrien    QUIT;
349298948Sobrien    COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3493130809Smarcel
3494130809Smarcel    completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text, word);
349598948Sobrien  }
349619370Spst
349719370Spst  /* Search upwards from currently selected frame (so that we can
349819370Spst     complete on local vars.  */
349919370Spst
3500130809Smarcel  for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
350119370Spst    {
350219370Spst      if (!BLOCK_SUPERBLOCK (b))
350319370Spst	{
350498948Sobrien	  surrounding_static_block = b;		/* For elmin of dups */
350519370Spst	}
350698948Sobrien
350719370Spst      /* Also catch fields of types defined in this places which match our
350898948Sobrien         text string.  Only complete on types visible from current context. */
350919370Spst
3510130809Smarcel      ALL_BLOCK_SYMBOLS (b, iter, sym)
351119370Spst	{
3512130809Smarcel	  QUIT;
351319370Spst	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
351419370Spst	  if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
351519370Spst	    {
351619370Spst	      struct type *t = SYMBOL_TYPE (sym);
351719370Spst	      enum type_code c = TYPE_CODE (t);
351819370Spst
351919370Spst	      if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
352019370Spst		{
352119370Spst		  for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
352219370Spst		    {
352319370Spst		      if (TYPE_FIELD_NAME (t, j))
352419370Spst			{
352519370Spst			  completion_list_add_name (TYPE_FIELD_NAME (t, j),
352698948Sobrien					sym_text, sym_text_len, text, word);
352719370Spst			}
352819370Spst		    }
352919370Spst		}
353019370Spst	    }
353119370Spst	}
353219370Spst    }
353319370Spst
353419370Spst  /* Go through the symtabs and check the externs and statics for
353519370Spst     symbols which match.  */
353619370Spst
353719370Spst  ALL_SYMTABS (objfile, s)
353898948Sobrien  {
353998948Sobrien    QUIT;
354098948Sobrien    b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3541130809Smarcel    ALL_BLOCK_SYMBOLS (b, iter, sym)
354298948Sobrien      {
354398948Sobrien	COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
354498948Sobrien      }
354598948Sobrien  }
354698948Sobrien
354798948Sobrien  ALL_SYMTABS (objfile, s)
354898948Sobrien  {
354998948Sobrien    QUIT;
355098948Sobrien    b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
355198948Sobrien    /* Don't do this block twice.  */
355298948Sobrien    if (b == surrounding_static_block)
355398948Sobrien      continue;
3554130809Smarcel    ALL_BLOCK_SYMBOLS (b, iter, sym)
355598948Sobrien      {
355698948Sobrien	COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
355798948Sobrien      }
355898948Sobrien  }
355998948Sobrien
356098948Sobrien  return (return_val);
356198948Sobrien}
356298948Sobrien
356398948Sobrien/* Like make_symbol_completion_list, but returns a list of symbols
356498948Sobrien   defined in a source file FILE.  */
356598948Sobrien
356698948Sobrienchar **
356798948Sobrienmake_file_symbol_completion_list (char *text, char *word, char *srcfile)
356898948Sobrien{
3569130809Smarcel  struct symbol *sym;
3570130809Smarcel  struct symtab *s;
3571130809Smarcel  struct block *b;
3572130809Smarcel  struct dict_iterator iter;
357398948Sobrien  /* The symbol we are completing on.  Points in same buffer as text.  */
357498948Sobrien  char *sym_text;
357598948Sobrien  /* Length of sym_text.  */
357698948Sobrien  int sym_text_len;
357798948Sobrien
357898948Sobrien  /* Now look for the symbol we are supposed to complete on.
357998948Sobrien     FIXME: This should be language-specific.  */
358098948Sobrien  {
358198948Sobrien    char *p;
358298948Sobrien    char quote_found;
358398948Sobrien    char *quote_pos = NULL;
358498948Sobrien
358598948Sobrien    /* First see if this is a quoted string.  */
358698948Sobrien    quote_found = '\0';
358798948Sobrien    for (p = text; *p != '\0'; ++p)
358898948Sobrien      {
358998948Sobrien	if (quote_found != '\0')
359098948Sobrien	  {
359198948Sobrien	    if (*p == quote_found)
359298948Sobrien	      /* Found close quote.  */
359398948Sobrien	      quote_found = '\0';
359498948Sobrien	    else if (*p == '\\' && p[1] == quote_found)
359598948Sobrien	      /* A backslash followed by the quote character
359698948Sobrien	         doesn't end the string.  */
359798948Sobrien	      ++p;
359898948Sobrien	  }
359998948Sobrien	else if (*p == '\'' || *p == '"')
360098948Sobrien	  {
360198948Sobrien	    quote_found = *p;
360298948Sobrien	    quote_pos = p;
360398948Sobrien	  }
360498948Sobrien      }
360598948Sobrien    if (quote_found == '\'')
360698948Sobrien      /* A string within single quotes can be a symbol, so complete on it.  */
360798948Sobrien      sym_text = quote_pos + 1;
360898948Sobrien    else if (quote_found == '"')
360998948Sobrien      /* A double-quoted string is never a symbol, nor does it make sense
361098948Sobrien         to complete it any other way.  */
361198948Sobrien      {
361298948Sobrien	return_val = (char **) xmalloc (sizeof (char *));
361398948Sobrien	return_val[0] = NULL;
361498948Sobrien	return return_val;
361598948Sobrien      }
361698948Sobrien    else
361798948Sobrien      {
3618130809Smarcel	/* Not a quoted string.  */
3619130809Smarcel	sym_text = language_search_unquoted_string (text, p);
362098948Sobrien      }
362198948Sobrien  }
362298948Sobrien
362398948Sobrien  sym_text_len = strlen (sym_text);
362498948Sobrien
362598948Sobrien  return_val_size = 10;
362698948Sobrien  return_val_index = 0;
362798948Sobrien  return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
362898948Sobrien  return_val[0] = NULL;
362998948Sobrien
363098948Sobrien  /* Find the symtab for SRCFILE (this loads it if it was not yet read
363198948Sobrien     in).  */
363298948Sobrien  s = lookup_symtab (srcfile);
363398948Sobrien  if (s == NULL)
363419370Spst    {
363598948Sobrien      /* Maybe they typed the file with leading directories, while the
363698948Sobrien	 symbol tables record only its basename.  */
363798948Sobrien      const char *tail = lbasename (srcfile);
363898948Sobrien
363998948Sobrien      if (tail > srcfile)
364098948Sobrien	s = lookup_symtab (tail);
364198948Sobrien    }
364298948Sobrien
364398948Sobrien  /* If we have no symtab for that file, return an empty list.  */
364498948Sobrien  if (s == NULL)
364598948Sobrien    return (return_val);
364698948Sobrien
364798948Sobrien  /* Go through this symtab and check the externs and statics for
364898948Sobrien     symbols which match.  */
364998948Sobrien
365098948Sobrien  b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3651130809Smarcel  ALL_BLOCK_SYMBOLS (b, iter, sym)
365298948Sobrien    {
365398948Sobrien      COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
365498948Sobrien    }
365598948Sobrien
365698948Sobrien  b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3657130809Smarcel  ALL_BLOCK_SYMBOLS (b, iter, sym)
365898948Sobrien    {
365998948Sobrien      COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
366098948Sobrien    }
366198948Sobrien
366298948Sobrien  return (return_val);
366398948Sobrien}
366498948Sobrien
366598948Sobrien/* A helper function for make_source_files_completion_list.  It adds
366698948Sobrien   another file name to a list of possible completions, growing the
366798948Sobrien   list as necessary.  */
366898948Sobrien
366998948Sobrienstatic void
367098948Sobrienadd_filename_to_list (const char *fname, char *text, char *word,
367198948Sobrien		      char ***list, int *list_used, int *list_alloced)
367298948Sobrien{
367398948Sobrien  char *new;
367498948Sobrien  size_t fnlen = strlen (fname);
367598948Sobrien
367698948Sobrien  if (*list_used + 1 >= *list_alloced)
367798948Sobrien    {
367898948Sobrien      *list_alloced *= 2;
367998948Sobrien      *list = (char **) xrealloc ((char *) *list,
368098948Sobrien				  *list_alloced * sizeof (char *));
368198948Sobrien    }
368298948Sobrien
368398948Sobrien  if (word == text)
368498948Sobrien    {
368598948Sobrien      /* Return exactly fname.  */
368698948Sobrien      new = xmalloc (fnlen + 5);
368798948Sobrien      strcpy (new, fname);
368898948Sobrien    }
368998948Sobrien  else if (word > text)
369098948Sobrien    {
369198948Sobrien      /* Return some portion of fname.  */
369298948Sobrien      new = xmalloc (fnlen + 5);
369398948Sobrien      strcpy (new, fname + (word - text));
369498948Sobrien    }
369598948Sobrien  else
369698948Sobrien    {
369798948Sobrien      /* Return some of TEXT plus fname.  */
369898948Sobrien      new = xmalloc (fnlen + (text - word) + 5);
369998948Sobrien      strncpy (new, word, text - word);
370098948Sobrien      new[text - word] = '\0';
370198948Sobrien      strcat (new, fname);
370298948Sobrien    }
370398948Sobrien  (*list)[*list_used] = new;
370498948Sobrien  (*list)[++*list_used] = NULL;
370598948Sobrien}
370698948Sobrien
370798948Sobrienstatic int
370898948Sobriennot_interesting_fname (const char *fname)
370998948Sobrien{
371098948Sobrien  static const char *illegal_aliens[] = {
371198948Sobrien    "_globals_",	/* inserted by coff_symtab_read */
371298948Sobrien    NULL
371398948Sobrien  };
371498948Sobrien  int i;
371598948Sobrien
371698948Sobrien  for (i = 0; illegal_aliens[i]; i++)
371798948Sobrien    {
371898948Sobrien      if (strcmp (fname, illegal_aliens[i]) == 0)
371998948Sobrien	return 1;
372098948Sobrien    }
372198948Sobrien  return 0;
372298948Sobrien}
372398948Sobrien
372498948Sobrien/* Return a NULL terminated array of all source files whose names
372598948Sobrien   begin with matching TEXT.  The file names are looked up in the
372698948Sobrien   symbol tables of this program.  If the answer is no matchess, then
372798948Sobrien   the return value is an array which contains only a NULL pointer.  */
372898948Sobrien
372998948Sobrienchar **
373098948Sobrienmake_source_files_completion_list (char *text, char *word)
373198948Sobrien{
3732130809Smarcel  struct symtab *s;
3733130809Smarcel  struct partial_symtab *ps;
3734130809Smarcel  struct objfile *objfile;
373598948Sobrien  int first = 1;
373698948Sobrien  int list_alloced = 1;
373798948Sobrien  int list_used = 0;
373898948Sobrien  size_t text_len = strlen (text);
373998948Sobrien  char **list = (char **) xmalloc (list_alloced * sizeof (char *));
374098948Sobrien  const char *base_name;
374198948Sobrien
374298948Sobrien  list[0] = NULL;
374398948Sobrien
374498948Sobrien  if (!have_full_symbols () && !have_partial_symbols ())
374598948Sobrien    return list;
374698948Sobrien
374798948Sobrien  ALL_SYMTABS (objfile, s)
374898948Sobrien    {
374998948Sobrien      if (not_interesting_fname (s->filename))
375098948Sobrien	continue;
375198948Sobrien      if (!filename_seen (s->filename, 1, &first)
375298948Sobrien#if HAVE_DOS_BASED_FILE_SYSTEM
375398948Sobrien	  && strncasecmp (s->filename, text, text_len) == 0
375498948Sobrien#else
375598948Sobrien	  && strncmp (s->filename, text, text_len) == 0
375698948Sobrien#endif
375798948Sobrien	  )
375819370Spst	{
375998948Sobrien	  /* This file matches for a completion; add it to the current
376098948Sobrien	     list of matches.  */
376198948Sobrien	  add_filename_to_list (s->filename, text, word,
376298948Sobrien				&list, &list_used, &list_alloced);
376319370Spst	}
376498948Sobrien      else
376598948Sobrien	{
376698948Sobrien	  /* NOTE: We allow the user to type a base name when the
376798948Sobrien	     debug info records leading directories, but not the other
376898948Sobrien	     way around.  This is what subroutines of breakpoint
376998948Sobrien	     command do when they parse file names.  */
377098948Sobrien	  base_name = lbasename (s->filename);
377198948Sobrien	  if (base_name != s->filename
377298948Sobrien	      && !filename_seen (base_name, 1, &first)
377398948Sobrien#if HAVE_DOS_BASED_FILE_SYSTEM
377498948Sobrien	      && strncasecmp (base_name, text, text_len) == 0
377598948Sobrien#else
377698948Sobrien	      && strncmp (base_name, text, text_len) == 0
377798948Sobrien#endif
377898948Sobrien	      )
377998948Sobrien	    add_filename_to_list (base_name, text, word,
378098948Sobrien				  &list, &list_used, &list_alloced);
378198948Sobrien	}
378219370Spst    }
378319370Spst
378498948Sobrien  ALL_PSYMTABS (objfile, ps)
378519370Spst    {
378698948Sobrien      if (not_interesting_fname (ps->filename))
378798948Sobrien	continue;
378898948Sobrien      if (!ps->readin)
378919370Spst	{
379098948Sobrien	  if (!filename_seen (ps->filename, 1, &first)
379198948Sobrien#if HAVE_DOS_BASED_FILE_SYSTEM
379298948Sobrien	      && strncasecmp (ps->filename, text, text_len) == 0
379398948Sobrien#else
379498948Sobrien	      && strncmp (ps->filename, text, text_len) == 0
379598948Sobrien#endif
379698948Sobrien	      )
379798948Sobrien	    {
379898948Sobrien	      /* This file matches for a completion; add it to the
379998948Sobrien		 current list of matches.  */
380098948Sobrien	      add_filename_to_list (ps->filename, text, word,
380198948Sobrien				    &list, &list_used, &list_alloced);
380298948Sobrien
380398948Sobrien	    }
380498948Sobrien	  else
380598948Sobrien	    {
380698948Sobrien	      base_name = lbasename (ps->filename);
380798948Sobrien	      if (base_name != ps->filename
380898948Sobrien		  && !filename_seen (base_name, 1, &first)
380998948Sobrien#if HAVE_DOS_BASED_FILE_SYSTEM
381098948Sobrien		  && strncasecmp (base_name, text, text_len) == 0
381198948Sobrien#else
381298948Sobrien		  && strncmp (base_name, text, text_len) == 0
381398948Sobrien#endif
381498948Sobrien		  )
381598948Sobrien		add_filename_to_list (base_name, text, word,
381698948Sobrien				      &list, &list_used, &list_alloced);
381798948Sobrien	    }
381819370Spst	}
381919370Spst    }
382019370Spst
382198948Sobrien  return list;
382219370Spst}
382319370Spst
382419370Spst/* Determine if PC is in the prologue of a function.  The prologue is the area
382519370Spst   between the first instruction of a function, and the first executable line.
382619370Spst   Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
382719370Spst
382846283Sdfr   If non-zero, func_start is where we think the prologue starts, possibly
382919370Spst   by previous examination of symbol table information.
383019370Spst */
383119370Spst
383219370Spstint
383398948Sobrienin_prologue (CORE_ADDR pc, CORE_ADDR func_start)
383419370Spst{
383519370Spst  struct symtab_and_line sal;
383619370Spst  CORE_ADDR func_addr, func_end;
383719370Spst
383898948Sobrien  /* We have several sources of information we can consult to figure
383998948Sobrien     this out.
384098948Sobrien     - Compilers usually emit line number info that marks the prologue
384198948Sobrien       as its own "source line".  So the ending address of that "line"
384298948Sobrien       is the end of the prologue.  If available, this is the most
384398948Sobrien       reliable method.
384498948Sobrien     - The minimal symbols and partial symbols, which can usually tell
384598948Sobrien       us the starting and ending addresses of a function.
384698948Sobrien     - If we know the function's start address, we can call the
384798948Sobrien       architecture-defined SKIP_PROLOGUE function to analyze the
384898948Sobrien       instruction stream and guess where the prologue ends.
384998948Sobrien     - Our `func_start' argument; if non-zero, this is the caller's
385098948Sobrien       best guess as to the function's entry point.  At the time of
385198948Sobrien       this writing, handle_inferior_event doesn't get this right, so
385298948Sobrien       it should be our last resort.  */
385319370Spst
385498948Sobrien  /* Consult the partial symbol table, to find which function
385598948Sobrien     the PC is in.  */
385698948Sobrien  if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
385798948Sobrien    {
385898948Sobrien      CORE_ADDR prologue_end;
385919370Spst
386098948Sobrien      /* We don't even have minsym information, so fall back to using
386198948Sobrien         func_start, if given.  */
386298948Sobrien      if (! func_start)
386398948Sobrien	return 1;		/* We *might* be in a prologue.  */
386419370Spst
386598948Sobrien      prologue_end = SKIP_PROLOGUE (func_start);
386619370Spst
386798948Sobrien      return func_start <= pc && pc < prologue_end;
386898948Sobrien    }
386919370Spst
387098948Sobrien  /* If we have line number information for the function, that's
387198948Sobrien     usually pretty reliable.  */
387298948Sobrien  sal = find_pc_line (func_addr, 0);
387319370Spst
387498948Sobrien  /* Now sal describes the source line at the function's entry point,
387598948Sobrien     which (by convention) is the prologue.  The end of that "line",
387698948Sobrien     sal.end, is the end of the prologue.
387719370Spst
387898948Sobrien     Note that, for functions whose source code is all on a single
387998948Sobrien     line, the line number information doesn't always end up this way.
388098948Sobrien     So we must verify that our purported end-of-prologue address is
388198948Sobrien     *within* the function, not at its start or end.  */
388298948Sobrien  if (sal.line == 0
388398948Sobrien      || sal.end <= func_addr
388498948Sobrien      || func_end <= sal.end)
388598948Sobrien    {
388698948Sobrien      /* We don't have any good line number info, so use the minsym
388798948Sobrien	 information, together with the architecture-specific prologue
388898948Sobrien	 scanning code.  */
388998948Sobrien      CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
389019370Spst
389198948Sobrien      return func_addr <= pc && pc < prologue_end;
389298948Sobrien    }
389319370Spst
389498948Sobrien  /* We have line number info, and it looks good.  */
389598948Sobrien  return func_addr <= pc && pc < sal.end;
389619370Spst}
389719370Spst
3898130809Smarcel/* Given PC at the function's start address, attempt to find the
3899130809Smarcel   prologue end using SAL information.  Return zero if the skip fails.
390046283Sdfr
3901130809Smarcel   A non-optimized prologue traditionally has one SAL for the function
3902130809Smarcel   and a second for the function body.  A single line function has
3903130809Smarcel   them both pointing at the same line.
390446283Sdfr
3905130809Smarcel   An optimized prologue is similar but the prologue may contain
3906130809Smarcel   instructions (SALs) from the instruction body.  Need to skip those
3907130809Smarcel   while not getting into the function body.
390846283Sdfr
3909130809Smarcel   The functions end point and an increasing SAL line are used as
3910130809Smarcel   indicators of the prologue's endpoint.
391146283Sdfr
3912130809Smarcel   This code is based on the function refine_prologue_limit (versions
3913130809Smarcel   found in both ia64 and ppc).  */
391446283Sdfr
3915130809SmarcelCORE_ADDR
3916130809Smarcelskip_prologue_using_sal (CORE_ADDR func_addr)
391746283Sdfr{
3918130809Smarcel  struct symtab_and_line prologue_sal;
3919130809Smarcel  CORE_ADDR start_pc;
3920130809Smarcel  CORE_ADDR end_pc;
392146283Sdfr
3922130809Smarcel  /* Get an initial range for the function.  */
3923130809Smarcel  find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
3924130809Smarcel  start_pc += FUNCTION_START_OFFSET;
392546283Sdfr
3926130809Smarcel  prologue_sal = find_pc_line (start_pc, 0);
3927130809Smarcel  if (prologue_sal.line != 0)
392846283Sdfr    {
3929130809Smarcel      while (prologue_sal.end < end_pc)
393046283Sdfr	{
3931130809Smarcel	  struct symtab_and_line sal;
393298948Sobrien
3933130809Smarcel	  sal = find_pc_line (prologue_sal.end, 0);
3934130809Smarcel	  if (sal.line == 0)
3935130809Smarcel	    break;
3936130809Smarcel	  /* Assume that a consecutive SAL for the same (or larger)
3937130809Smarcel	     line mark the prologue -> body transition.  */
3938130809Smarcel	  if (sal.line >= prologue_sal.line)
3939130809Smarcel	    break;
3940130809Smarcel	  /* The case in which compiler's optimizer/scheduler has
3941130809Smarcel	     moved instructions into the prologue.  We look ahead in
3942130809Smarcel	     the function looking for address ranges whose
3943130809Smarcel	     corresponding line number is less the first one that we
3944130809Smarcel	     found for the function.  This is more conservative then
3945130809Smarcel	     refine_prologue_limit which scans a large number of SALs
3946130809Smarcel	     looking for any in the prologue */
3947130809Smarcel	  prologue_sal = sal;
394846283Sdfr	}
394946283Sdfr    }
3950130809Smarcel  return prologue_sal.end;
395146283Sdfr}
395298948Sobrien
395398948Sobrienstruct symtabs_and_lines
395498948Sobriendecode_line_spec (char *string, int funfirstline)
395598948Sobrien{
395698948Sobrien  struct symtabs_and_lines sals;
3957130809Smarcel  struct symtab_and_line cursal;
3958130809Smarcel
395998948Sobrien  if (string == 0)
396098948Sobrien    error ("Empty line specification.");
3961130809Smarcel
3962130809Smarcel  /* We use whatever is set as the current source line. We do not try
3963130809Smarcel     and get a default  or it will recursively call us! */
3964130809Smarcel  cursal = get_current_source_symtab_and_line ();
3965130809Smarcel
396698948Sobrien  sals = decode_line_1 (&string, funfirstline,
3967130809Smarcel			cursal.symtab, cursal.line,
3968130809Smarcel			(char ***) NULL, NULL);
3969130809Smarcel
397098948Sobrien  if (*string)
397198948Sobrien    error ("Junk at end of line specification: %s", string);
397298948Sobrien  return sals;
397398948Sobrien}
397446283Sdfr
397598948Sobrien/* Track MAIN */
397698948Sobrienstatic char *name_of_main;
397798948Sobrien
397819370Spstvoid
397998948Sobrienset_main_name (const char *name)
398019370Spst{
398198948Sobrien  if (name_of_main != NULL)
398298948Sobrien    {
398398948Sobrien      xfree (name_of_main);
398498948Sobrien      name_of_main = NULL;
398598948Sobrien    }
398698948Sobrien  if (name != NULL)
398798948Sobrien    {
398898948Sobrien      name_of_main = xstrdup (name);
398998948Sobrien    }
399098948Sobrien}
399198948Sobrien
399298948Sobrienchar *
399398948Sobrienmain_name (void)
399498948Sobrien{
399598948Sobrien  if (name_of_main != NULL)
399698948Sobrien    return name_of_main;
399798948Sobrien  else
399898948Sobrien    return "main";
399998948Sobrien}
400098948Sobrien
400198948Sobrien
400298948Sobrienvoid
400398948Sobrien_initialize_symtab (void)
400498948Sobrien{
400519370Spst  add_info ("variables", variables_info,
400698948Sobrien	 "All global and static variable names, or those matching REGEXP.");
400746283Sdfr  if (dbx_commands)
400898948Sobrien    add_com ("whereis", class_info, variables_info,
400998948Sobrien	 "All global and static variable names, or those matching REGEXP.");
401046283Sdfr
401119370Spst  add_info ("functions", functions_info,
401219370Spst	    "All function names, or those matching REGEXP.");
401319370Spst
401498948Sobrien
401519370Spst  /* FIXME:  This command has at least the following problems:
401619370Spst     1.  It prints builtin types (in a very strange and confusing fashion).
401719370Spst     2.  It doesn't print right, e.g. with
401898948Sobrien     typedef struct foo *FOO
401998948Sobrien     type_print prints "FOO" when we want to make it (in this situation)
402098948Sobrien     print "struct foo *".
402119370Spst     I also think "ptype" or "whatis" is more likely to be useful (but if
402219370Spst     there is much disagreement "info types" can be fixed).  */
402319370Spst  add_info ("types", types_info,
402419370Spst	    "All type names, or those matching REGEXP.");
402519370Spst
402619370Spst  add_info ("sources", sources_info,
402719370Spst	    "Source files in the program.");
402819370Spst
402946283Sdfr  add_com ("rbreak", class_breakpoint, rbreak_command,
403098948Sobrien	   "Set a breakpoint for all functions matching REGEXP.");
403119370Spst
403246283Sdfr  if (xdb_commands)
403346283Sdfr    {
403446283Sdfr      add_com ("lf", class_info, sources_info, "Source files in the program");
403546283Sdfr      add_com ("lg", class_info, variables_info,
403698948Sobrien	 "All global and static variable names, or those matching REGEXP.");
403746283Sdfr    }
403846283Sdfr
403919370Spst  /* Initialize the one built-in type that isn't language dependent... */
404019370Spst  builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
404119370Spst				  "<unknown type>", (struct objfile *) NULL);
404219370Spst}
4043