xref: /openbsd-src/gnu/usr.bin/binutils/gdb/symtab.c (revision 63addd46c1e40ca0f49488ddcdc4ab598023b0c1)
1e93f7393Sniklas /* Symbol table lookup for the GNU debugger, GDB.
2b725ae77Skettenis 
3b725ae77Skettenis    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4b725ae77Skettenis    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
5e93f7393Sniklas    Free Software Foundation, Inc.
6e93f7393Sniklas 
7e93f7393Sniklas    This file is part of GDB.
8e93f7393Sniklas 
9e93f7393Sniklas    This program is free software; you can redistribute it and/or modify
10e93f7393Sniklas    it under the terms of the GNU General Public License as published by
11e93f7393Sniklas    the Free Software Foundation; either version 2 of the License, or
12e93f7393Sniklas    (at your option) any later version.
13e93f7393Sniklas 
14e93f7393Sniklas    This program is distributed in the hope that it will be useful,
15e93f7393Sniklas    but WITHOUT ANY WARRANTY; without even the implied warranty of
16e93f7393Sniklas    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17e93f7393Sniklas    GNU General Public License for more details.
18e93f7393Sniklas 
19e93f7393Sniklas    You should have received a copy of the GNU General Public License
20e93f7393Sniklas    along with this program; if not, write to the Free Software
21b725ae77Skettenis    Foundation, Inc., 59 Temple Place - Suite 330,
22b725ae77Skettenis    Boston, MA 02111-1307, USA.  */
23e93f7393Sniklas 
24e93f7393Sniklas #include "defs.h"
25e93f7393Sniklas #include "symtab.h"
26e93f7393Sniklas #include "gdbtypes.h"
27e93f7393Sniklas #include "gdbcore.h"
28e93f7393Sniklas #include "frame.h"
29e93f7393Sniklas #include "target.h"
30e93f7393Sniklas #include "value.h"
31e93f7393Sniklas #include "symfile.h"
32e93f7393Sniklas #include "objfiles.h"
33e93f7393Sniklas #include "gdbcmd.h"
34e93f7393Sniklas #include "call-cmds.h"
35b725ae77Skettenis #include "gdb_regex.h"
36e93f7393Sniklas #include "expression.h"
37e93f7393Sniklas #include "language.h"
38e93f7393Sniklas #include "demangle.h"
39b725ae77Skettenis #include "inferior.h"
40b725ae77Skettenis #include "linespec.h"
41b725ae77Skettenis #include "source.h"
42b725ae77Skettenis #include "filenames.h"		/* for FILENAME_CMP */
43b725ae77Skettenis #include "objc-lang.h"
44*63addd46Skettenis #include "ada-lang.h"
45e93f7393Sniklas 
46b725ae77Skettenis #include "hashtab.h"
47b725ae77Skettenis 
48b725ae77Skettenis #include "gdb_obstack.h"
49b725ae77Skettenis #include "block.h"
50b725ae77Skettenis #include "dictionary.h"
51e93f7393Sniklas 
52e93f7393Sniklas #include <sys/types.h>
53e93f7393Sniklas #include <fcntl.h>
54e93f7393Sniklas #include "gdb_string.h"
55e93f7393Sniklas #include "gdb_stat.h"
56e93f7393Sniklas #include <ctype.h>
57b725ae77Skettenis #include "cp-abi.h"
58e93f7393Sniklas 
59e93f7393Sniklas /* Prototypes for local functions */
60e93f7393Sniklas 
61b725ae77Skettenis static void completion_list_add_name (char *, char *, int, char *, char *);
62e93f7393Sniklas 
63b725ae77Skettenis static void rbreak_command (char *, int);
64e93f7393Sniklas 
65b725ae77Skettenis static void types_info (char *, int);
66e93f7393Sniklas 
67b725ae77Skettenis static void functions_info (char *, int);
68e93f7393Sniklas 
69b725ae77Skettenis static void variables_info (char *, int);
70e93f7393Sniklas 
71b725ae77Skettenis static void sources_info (char *, int);
72e93f7393Sniklas 
73*63addd46Skettenis static void output_source_filename (const char *, int *);
74e93f7393Sniklas 
75b725ae77Skettenis static int find_line_common (struct linetable *, int, int *);
76e93f7393Sniklas 
77b725ae77Skettenis /* This one is used by linespec.c */
78e93f7393Sniklas 
79b725ae77Skettenis char *operator_chars (char *p, char **end);
80e93f7393Sniklas 
81b725ae77Skettenis static struct symbol *lookup_symbol_aux (const char *name,
82b725ae77Skettenis 					 const char *linkage_name,
83b725ae77Skettenis 					 const struct block *block,
84b725ae77Skettenis 					 const domain_enum domain,
85b725ae77Skettenis 					 int *is_a_field_of_this,
86b725ae77Skettenis 					 struct symtab **symtab);
87e93f7393Sniklas 
88b725ae77Skettenis static
89b725ae77Skettenis struct symbol *lookup_symbol_aux_local (const char *name,
90b725ae77Skettenis 					const char *linkage_name,
91b725ae77Skettenis 					const struct block *block,
92b725ae77Skettenis 					const domain_enum domain,
93b725ae77Skettenis 					struct symtab **symtab);
94e93f7393Sniklas 
95b725ae77Skettenis static
96b725ae77Skettenis struct symbol *lookup_symbol_aux_symtabs (int block_index,
97b725ae77Skettenis 					  const char *name,
98b725ae77Skettenis 					  const char *linkage_name,
99b725ae77Skettenis 					  const domain_enum domain,
100b725ae77Skettenis 					  struct symtab **symtab);
101e93f7393Sniklas 
102b725ae77Skettenis static
103b725ae77Skettenis struct symbol *lookup_symbol_aux_psymtabs (int block_index,
104b725ae77Skettenis 					   const char *name,
105b725ae77Skettenis 					   const char *linkage_name,
106b725ae77Skettenis 					   const domain_enum domain,
107b725ae77Skettenis 					   struct symtab **symtab);
108e93f7393Sniklas 
109b725ae77Skettenis #if 0
110b725ae77Skettenis static
111b725ae77Skettenis struct symbol *lookup_symbol_aux_minsyms (const char *name,
112b725ae77Skettenis 					  const char *linkage_name,
113b725ae77Skettenis 					  const domain_enum domain,
114b725ae77Skettenis 					  int *is_a_field_of_this,
115b725ae77Skettenis 					  struct symtab **symtab);
116b725ae77Skettenis #endif
117e93f7393Sniklas 
118*63addd46Skettenis /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c.
119*63addd46Skettenis    Signals the presence of objects compiled by HP compilers.  */
120*63addd46Skettenis int deprecated_hp_som_som_object_present = 0;
121b725ae77Skettenis 
122b725ae77Skettenis static void fixup_section (struct general_symbol_info *, struct objfile *);
123b725ae77Skettenis 
124b725ae77Skettenis static int file_matches (char *, char **, int);
125b725ae77Skettenis 
126b725ae77Skettenis static void print_symbol_info (domain_enum,
127b725ae77Skettenis 			       struct symtab *, struct symbol *, int, char *);
128b725ae77Skettenis 
129b725ae77Skettenis static void print_msymbol_info (struct minimal_symbol *);
130b725ae77Skettenis 
131b725ae77Skettenis static void symtab_symbol_info (char *, domain_enum, int);
132b725ae77Skettenis 
133b725ae77Skettenis void _initialize_symtab (void);
134e93f7393Sniklas 
135e93f7393Sniklas /* */
136e93f7393Sniklas 
137e93f7393Sniklas /* The single non-language-specific builtin type */
138e93f7393Sniklas struct type *builtin_type_error;
139e93f7393Sniklas 
140e93f7393Sniklas /* Block in which the most recently searched-for symbol was found.
141e93f7393Sniklas    Might be better to make this a parameter to lookup_symbol and
142e93f7393Sniklas    value_of_this. */
143e93f7393Sniklas 
144e93f7393Sniklas const struct block *block_found;
145e93f7393Sniklas 
146e93f7393Sniklas /* Check for a symtab of a specific name; first in symtabs, then in
147e93f7393Sniklas    psymtabs.  *If* there is no '/' in the name, a match after a '/'
148e93f7393Sniklas    in the symtab filename will also work.  */
149e93f7393Sniklas 
150b725ae77Skettenis struct symtab *
lookup_symtab(const char * name)151b725ae77Skettenis lookup_symtab (const char *name)
152e93f7393Sniklas {
153b725ae77Skettenis   struct symtab *s;
154b725ae77Skettenis   struct partial_symtab *ps;
155b725ae77Skettenis   struct objfile *objfile;
156b725ae77Skettenis   char *real_path = NULL;
157b725ae77Skettenis   char *full_path = NULL;
158b725ae77Skettenis 
159b725ae77Skettenis   /* Here we are interested in canonicalizing an absolute path, not
160b725ae77Skettenis      absolutizing a relative path.  */
161b725ae77Skettenis   if (IS_ABSOLUTE_PATH (name))
162b725ae77Skettenis     {
163b725ae77Skettenis       full_path = xfullpath (name);
164b725ae77Skettenis       make_cleanup (xfree, full_path);
165b725ae77Skettenis       real_path = gdb_realpath (name);
166b725ae77Skettenis       make_cleanup (xfree, real_path);
167b725ae77Skettenis     }
168e93f7393Sniklas 
169e93f7393Sniklas got_symtab:
170e93f7393Sniklas 
171e93f7393Sniklas   /* First, search for an exact match */
172e93f7393Sniklas 
173e93f7393Sniklas   ALL_SYMTABS (objfile, s)
174b725ae77Skettenis   {
175b725ae77Skettenis     if (FILENAME_CMP (name, s->filename) == 0)
176b725ae77Skettenis       {
177e93f7393Sniklas 	return s;
178b725ae77Skettenis       }
179e93f7393Sniklas 
180b725ae77Skettenis     /* If the user gave us an absolute path, try to find the file in
181b725ae77Skettenis        this symtab and use its absolute path.  */
182b725ae77Skettenis 
183b725ae77Skettenis     if (full_path != NULL)
184b725ae77Skettenis       {
185*63addd46Skettenis         const char *fp = symtab_to_fullname (s);
186*63addd46Skettenis         if (fp != NULL && FILENAME_CMP (full_path, fp) == 0)
187b725ae77Skettenis           {
188b725ae77Skettenis             return s;
189b725ae77Skettenis           }
190b725ae77Skettenis       }
191b725ae77Skettenis 
192b725ae77Skettenis     if (real_path != NULL)
193b725ae77Skettenis       {
194*63addd46Skettenis         char *fullname = symtab_to_fullname (s);
195*63addd46Skettenis         if (fullname != NULL)
196*63addd46Skettenis           {
197*63addd46Skettenis             char *rp = gdb_realpath (fullname);
198b725ae77Skettenis             make_cleanup (xfree, rp);
199b725ae77Skettenis             if (FILENAME_CMP (real_path, rp) == 0)
200b725ae77Skettenis               {
201b725ae77Skettenis                 return s;
202b725ae77Skettenis               }
203b725ae77Skettenis           }
204b725ae77Skettenis       }
205*63addd46Skettenis   }
206e93f7393Sniklas 
207e93f7393Sniklas   /* Now, search for a matching tail (only if name doesn't have any dirs) */
208e93f7393Sniklas 
209b725ae77Skettenis   if (lbasename (name) == name)
210e93f7393Sniklas     ALL_SYMTABS (objfile, s)
211e93f7393Sniklas     {
212b725ae77Skettenis       if (FILENAME_CMP (lbasename (s->filename), name) == 0)
213e93f7393Sniklas 	return s;
214e93f7393Sniklas     }
215e93f7393Sniklas 
216e93f7393Sniklas   /* Same search rules as above apply here, but now we look thru the
217e93f7393Sniklas      psymtabs.  */
218e93f7393Sniklas 
219e93f7393Sniklas   ps = lookup_partial_symtab (name);
220e93f7393Sniklas   if (!ps)
221e93f7393Sniklas     return (NULL);
222e93f7393Sniklas 
223e93f7393Sniklas   if (ps->readin)
224e93f7393Sniklas     error ("Internal: readin %s pst for `%s' found when no symtab found.",
225e93f7393Sniklas 	   ps->filename, name);
226e93f7393Sniklas 
227e93f7393Sniklas   s = PSYMTAB_TO_SYMTAB (ps);
228e93f7393Sniklas 
229e93f7393Sniklas   if (s)
230e93f7393Sniklas     return s;
231e93f7393Sniklas 
232e93f7393Sniklas   /* At this point, we have located the psymtab for this file, but
233e93f7393Sniklas      the conversion to a symtab has failed.  This usually happens
234e93f7393Sniklas      when we are looking up an include file.  In this case,
235e93f7393Sniklas      PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
236e93f7393Sniklas      been created.  So, we need to run through the symtabs again in
237e93f7393Sniklas      order to find the file.
238e93f7393Sniklas      XXX - This is a crock, and should be fixed inside of the the
239e93f7393Sniklas      symbol parsing routines. */
240e93f7393Sniklas   goto got_symtab;
241e93f7393Sniklas }
242e93f7393Sniklas 
243e93f7393Sniklas /* Lookup the partial symbol table of a source file named NAME.
244e93f7393Sniklas    *If* there is no '/' in the name, a match after a '/'
245e93f7393Sniklas    in the psymtab filename will also work.  */
246e93f7393Sniklas 
247e93f7393Sniklas struct partial_symtab *
lookup_partial_symtab(const char * name)248b725ae77Skettenis lookup_partial_symtab (const char *name)
249e93f7393Sniklas {
250b725ae77Skettenis   struct partial_symtab *pst;
251b725ae77Skettenis   struct objfile *objfile;
252b725ae77Skettenis   char *full_path = NULL;
253b725ae77Skettenis   char *real_path = NULL;
254b725ae77Skettenis 
255b725ae77Skettenis   /* Here we are interested in canonicalizing an absolute path, not
256b725ae77Skettenis      absolutizing a relative path.  */
257b725ae77Skettenis   if (IS_ABSOLUTE_PATH (name))
258b725ae77Skettenis     {
259b725ae77Skettenis       full_path = xfullpath (name);
260b725ae77Skettenis       make_cleanup (xfree, full_path);
261b725ae77Skettenis       real_path = gdb_realpath (name);
262b725ae77Skettenis       make_cleanup (xfree, real_path);
263b725ae77Skettenis     }
264e93f7393Sniklas 
265e93f7393Sniklas   ALL_PSYMTABS (objfile, pst)
266e93f7393Sniklas   {
267b725ae77Skettenis     if (FILENAME_CMP (name, pst->filename) == 0)
268e93f7393Sniklas       {
269e93f7393Sniklas 	return (pst);
270e93f7393Sniklas       }
271b725ae77Skettenis 
272b725ae77Skettenis     /* If the user gave us an absolute path, try to find the file in
273b725ae77Skettenis        this symtab and use its absolute path.  */
274b725ae77Skettenis     if (full_path != NULL)
275b725ae77Skettenis       {
276*63addd46Skettenis 	psymtab_to_fullname (pst);
277b725ae77Skettenis 	if (pst->fullname != NULL
278b725ae77Skettenis 	    && FILENAME_CMP (full_path, pst->fullname) == 0)
279b725ae77Skettenis 	  {
280b725ae77Skettenis 	    return pst;
281b725ae77Skettenis 	  }
282b725ae77Skettenis       }
283b725ae77Skettenis 
284b725ae77Skettenis     if (real_path != NULL)
285b725ae77Skettenis       {
286b725ae77Skettenis         char *rp = NULL;
287*63addd46Skettenis 	psymtab_to_fullname (pst);
288b725ae77Skettenis         if (pst->fullname != NULL)
289b725ae77Skettenis           {
290b725ae77Skettenis             rp = gdb_realpath (pst->fullname);
291b725ae77Skettenis             make_cleanup (xfree, rp);
292b725ae77Skettenis           }
293b725ae77Skettenis 	if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
294b725ae77Skettenis 	  {
295b725ae77Skettenis 	    return pst;
296b725ae77Skettenis 	  }
297b725ae77Skettenis       }
298e93f7393Sniklas   }
299e93f7393Sniklas 
300e93f7393Sniklas   /* Now, search for a matching tail (only if name doesn't have any dirs) */
301e93f7393Sniklas 
302b725ae77Skettenis   if (lbasename (name) == name)
303e93f7393Sniklas     ALL_PSYMTABS (objfile, pst)
304e93f7393Sniklas     {
305b725ae77Skettenis       if (FILENAME_CMP (lbasename (pst->filename), name) == 0)
306e93f7393Sniklas 	return (pst);
307e93f7393Sniklas     }
308e93f7393Sniklas 
309e93f7393Sniklas   return (NULL);
310e93f7393Sniklas }
311e93f7393Sniklas 
312b725ae77Skettenis /* Mangle a GDB method stub type.  This actually reassembles the pieces of the
313b725ae77Skettenis    full method name, which consist of the class name (from T), the unadorned
314b725ae77Skettenis    method name from METHOD_ID, and the signature for the specific overload,
315b725ae77Skettenis    specified by SIGNATURE_ID.  Note that this function is g++ specific. */
316e93f7393Sniklas 
317e93f7393Sniklas char *
gdb_mangle_name(struct type * type,int method_id,int signature_id)318b725ae77Skettenis gdb_mangle_name (struct type *type, int method_id, int signature_id)
319e93f7393Sniklas {
320e93f7393Sniklas   int mangled_name_len;
321e93f7393Sniklas   char *mangled_name;
322b725ae77Skettenis   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
323b725ae77Skettenis   struct fn_field *method = &f[signature_id];
324b725ae77Skettenis   char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
325b725ae77Skettenis   char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
326e93f7393Sniklas   char *newname = type_name_no_tag (type);
327e93f7393Sniklas 
328e93f7393Sniklas   /* Does the form of physname indicate that it is the full mangled name
329e93f7393Sniklas      of a constructor (not just the args)?  */
330e93f7393Sniklas   int is_full_physname_constructor;
331e93f7393Sniklas 
332e93f7393Sniklas   int is_constructor;
333b725ae77Skettenis   int is_destructor = is_destructor_name (physname);
334e93f7393Sniklas   /* Need a new type prefix.  */
335e93f7393Sniklas   char *const_prefix = method->is_const ? "C" : "";
336e93f7393Sniklas   char *volatile_prefix = method->is_volatile ? "V" : "";
337e93f7393Sniklas   char buf[20];
338e93f7393Sniklas   int len = (newname == NULL ? 0 : strlen (newname));
339e93f7393Sniklas 
340b725ae77Skettenis   /* Nothing to do if physname already contains a fully mangled v3 abi name
341b725ae77Skettenis      or an operator name.  */
342b725ae77Skettenis   if ((physname[0] == '_' && physname[1] == 'Z')
343b725ae77Skettenis       || is_operator_name (field_name))
344b725ae77Skettenis     return xstrdup (physname);
345b725ae77Skettenis 
346b725ae77Skettenis   is_full_physname_constructor = is_constructor_name (physname);
347e93f7393Sniklas 
348e93f7393Sniklas   is_constructor =
349b725ae77Skettenis     is_full_physname_constructor || (newname && strcmp (field_name, newname) == 0);
350e93f7393Sniklas 
351e93f7393Sniklas   if (!is_destructor)
352e93f7393Sniklas     is_destructor = (strncmp (physname, "__dt", 4) == 0);
353e93f7393Sniklas 
354e93f7393Sniklas   if (is_destructor || is_full_physname_constructor)
355e93f7393Sniklas     {
356e93f7393Sniklas       mangled_name = (char *) xmalloc (strlen (physname) + 1);
357e93f7393Sniklas       strcpy (mangled_name, physname);
358e93f7393Sniklas       return mangled_name;
359e93f7393Sniklas     }
360e93f7393Sniklas 
361e93f7393Sniklas   if (len == 0)
362e93f7393Sniklas     {
363e93f7393Sniklas       sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
364e93f7393Sniklas     }
365b725ae77Skettenis   else if (physname[0] == 't' || physname[0] == 'Q')
366e93f7393Sniklas     {
367b725ae77Skettenis       /* The physname for template and qualified methods already includes
368b725ae77Skettenis          the class name.  */
369e93f7393Sniklas       sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
370e93f7393Sniklas       newname = NULL;
371e93f7393Sniklas       len = 0;
372e93f7393Sniklas     }
373e93f7393Sniklas   else
374e93f7393Sniklas     {
375e93f7393Sniklas       sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
376e93f7393Sniklas     }
377e93f7393Sniklas   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
378b725ae77Skettenis 		      + strlen (buf) + len + strlen (physname) + 1);
379e93f7393Sniklas 
380e93f7393Sniklas     {
381e93f7393Sniklas       mangled_name = (char *) xmalloc (mangled_name_len);
382e93f7393Sniklas       if (is_constructor)
383e93f7393Sniklas 	mangled_name[0] = '\0';
384e93f7393Sniklas       else
385e93f7393Sniklas 	strcpy (mangled_name, field_name);
386e93f7393Sniklas     }
387e93f7393Sniklas   strcat (mangled_name, buf);
388e93f7393Sniklas   /* If the class doesn't have a name, i.e. newname NULL, then we just
389e93f7393Sniklas      mangle it using 0 for the length of the class.  Thus it gets mangled
390e93f7393Sniklas      as something starting with `::' rather than `classname::'. */
391e93f7393Sniklas   if (newname != NULL)
392e93f7393Sniklas     strcat (mangled_name, newname);
393e93f7393Sniklas 
394e93f7393Sniklas   strcat (mangled_name, physname);
395e93f7393Sniklas   return (mangled_name);
396e93f7393Sniklas }
397e93f7393Sniklas 
398e93f7393Sniklas 
399b725ae77Skettenis /* Initialize the language dependent portion of a symbol
400b725ae77Skettenis    depending upon the language for the symbol. */
401b725ae77Skettenis void
symbol_init_language_specific(struct general_symbol_info * gsymbol,enum language language)402b725ae77Skettenis symbol_init_language_specific (struct general_symbol_info *gsymbol,
403b725ae77Skettenis 			       enum language language)
404e93f7393Sniklas {
405b725ae77Skettenis   gsymbol->language = language;
406b725ae77Skettenis   if (gsymbol->language == language_cplus
407b725ae77Skettenis       || gsymbol->language == language_java
408b725ae77Skettenis       || gsymbol->language == language_objc)
409b725ae77Skettenis     {
410b725ae77Skettenis       gsymbol->language_specific.cplus_specific.demangled_name = NULL;
411b725ae77Skettenis     }
412b725ae77Skettenis   else
413b725ae77Skettenis     {
414b725ae77Skettenis       memset (&gsymbol->language_specific, 0,
415b725ae77Skettenis 	      sizeof (gsymbol->language_specific));
416b725ae77Skettenis     }
417b725ae77Skettenis }
418b725ae77Skettenis 
419b725ae77Skettenis /* Functions to initialize a symbol's mangled name.  */
420b725ae77Skettenis 
421b725ae77Skettenis /* Create the hash table used for demangled names.  Each hash entry is
422b725ae77Skettenis    a pair of strings; one for the mangled name and one for the demangled
423b725ae77Skettenis    name.  The entry is hashed via just the mangled name.  */
424b725ae77Skettenis 
425b725ae77Skettenis static void
create_demangled_names_hash(struct objfile * objfile)426b725ae77Skettenis create_demangled_names_hash (struct objfile *objfile)
427b725ae77Skettenis {
428b725ae77Skettenis   /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
429b725ae77Skettenis      The hash table code will round this up to the next prime number.
430b725ae77Skettenis      Choosing a much larger table size wastes memory, and saves only about
431b725ae77Skettenis      1% in symbol reading.  */
432b725ae77Skettenis 
433*63addd46Skettenis   objfile->demangled_names_hash = htab_create_alloc
434b725ae77Skettenis     (256, htab_hash_string, (int (*) (const void *, const void *)) streq,
435*63addd46Skettenis      NULL, xcalloc, xfree);
436b725ae77Skettenis }
437b725ae77Skettenis 
438b725ae77Skettenis /* Try to determine the demangled name for a symbol, based on the
439b725ae77Skettenis    language of that symbol.  If the language is set to language_auto,
440b725ae77Skettenis    it will attempt to find any demangling algorithm that works and
441b725ae77Skettenis    then set the language appropriately.  The returned name is allocated
442b725ae77Skettenis    by the demangler and should be xfree'd.  */
443b725ae77Skettenis 
444b725ae77Skettenis static char *
symbol_find_demangled_name(struct general_symbol_info * gsymbol,const char * mangled)445b725ae77Skettenis symbol_find_demangled_name (struct general_symbol_info *gsymbol,
446b725ae77Skettenis 			    const char *mangled)
447b725ae77Skettenis {
448b725ae77Skettenis   char *demangled = NULL;
449b725ae77Skettenis 
450b725ae77Skettenis   if (gsymbol->language == language_unknown)
451b725ae77Skettenis     gsymbol->language = language_auto;
452b725ae77Skettenis 
453b725ae77Skettenis   if (gsymbol->language == language_objc
454b725ae77Skettenis       || gsymbol->language == language_auto)
455b725ae77Skettenis     {
456b725ae77Skettenis       demangled =
457b725ae77Skettenis 	objc_demangle (mangled, 0);
458b725ae77Skettenis       if (demangled != NULL)
459b725ae77Skettenis 	{
460b725ae77Skettenis 	  gsymbol->language = language_objc;
461b725ae77Skettenis 	  return demangled;
462b725ae77Skettenis 	}
463b725ae77Skettenis     }
464b725ae77Skettenis   if (gsymbol->language == language_cplus
465b725ae77Skettenis       || gsymbol->language == language_auto)
466b725ae77Skettenis     {
467b725ae77Skettenis       demangled =
468b725ae77Skettenis         cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
469b725ae77Skettenis       if (demangled != NULL)
470b725ae77Skettenis 	{
471b725ae77Skettenis 	  gsymbol->language = language_cplus;
472b725ae77Skettenis 	  return demangled;
473b725ae77Skettenis 	}
474b725ae77Skettenis     }
475b725ae77Skettenis   if (gsymbol->language == language_java)
476b725ae77Skettenis     {
477b725ae77Skettenis       demangled =
478b725ae77Skettenis         cplus_demangle (mangled,
479b725ae77Skettenis                         DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
480b725ae77Skettenis       if (demangled != NULL)
481b725ae77Skettenis 	{
482b725ae77Skettenis 	  gsymbol->language = language_java;
483b725ae77Skettenis 	  return demangled;
484b725ae77Skettenis 	}
485b725ae77Skettenis     }
486b725ae77Skettenis   return NULL;
487b725ae77Skettenis }
488b725ae77Skettenis 
489b725ae77Skettenis /* Set both the mangled and demangled (if any) names for GSYMBOL based
490b725ae77Skettenis    on LINKAGE_NAME and LEN.  The hash table corresponding to OBJFILE
491b725ae77Skettenis    is used, and the memory comes from that objfile's objfile_obstack.
492b725ae77Skettenis    LINKAGE_NAME is copied, so the pointer can be discarded after
493b725ae77Skettenis    calling this function.  */
494b725ae77Skettenis 
495b725ae77Skettenis /* We have to be careful when dealing with Java names: when we run
496b725ae77Skettenis    into a Java minimal symbol, we don't know it's a Java symbol, so it
497b725ae77Skettenis    gets demangled as a C++ name.  This is unfortunate, but there's not
498b725ae77Skettenis    much we can do about it: but when demangling partial symbols and
499b725ae77Skettenis    regular symbols, we'd better not reuse the wrong demangled name.
500b725ae77Skettenis    (See PR gdb/1039.)  We solve this by putting a distinctive prefix
501b725ae77Skettenis    on Java names when storing them in the hash table.  */
502b725ae77Skettenis 
503b725ae77Skettenis /* FIXME: carlton/2003-03-13: This is an unfortunate situation.  I
504b725ae77Skettenis    don't mind the Java prefix so much: different languages have
505b725ae77Skettenis    different demangling requirements, so it's only natural that we
506b725ae77Skettenis    need to keep language data around in our demangling cache.  But
507b725ae77Skettenis    it's not good that the minimal symbol has the wrong demangled name.
508b725ae77Skettenis    Unfortunately, I can't think of any easy solution to that
509b725ae77Skettenis    problem.  */
510b725ae77Skettenis 
511b725ae77Skettenis #define JAVA_PREFIX "##JAVA$$"
512b725ae77Skettenis #define JAVA_PREFIX_LEN 8
513b725ae77Skettenis 
514b725ae77Skettenis void
symbol_set_names(struct general_symbol_info * gsymbol,const char * linkage_name,int len,struct objfile * objfile)515b725ae77Skettenis symbol_set_names (struct general_symbol_info *gsymbol,
516b725ae77Skettenis 		  const char *linkage_name, int len, struct objfile *objfile)
517b725ae77Skettenis {
518b725ae77Skettenis   char **slot;
519b725ae77Skettenis   /* A 0-terminated copy of the linkage name.  */
520b725ae77Skettenis   const char *linkage_name_copy;
521b725ae77Skettenis   /* A copy of the linkage name that might have a special Java prefix
522b725ae77Skettenis      added to it, for use when looking names up in the hash table.  */
523b725ae77Skettenis   const char *lookup_name;
524b725ae77Skettenis   /* The length of lookup_name.  */
525b725ae77Skettenis   int lookup_len;
526b725ae77Skettenis 
527b725ae77Skettenis   if (objfile->demangled_names_hash == NULL)
528b725ae77Skettenis     create_demangled_names_hash (objfile);
529b725ae77Skettenis 
530b725ae77Skettenis   /* The stabs reader generally provides names that are not
531b725ae77Skettenis      NUL-terminated; most of the other readers don't do this, so we
532b725ae77Skettenis      can just use the given copy, unless we're in the Java case.  */
533b725ae77Skettenis   if (gsymbol->language == language_java)
534b725ae77Skettenis     {
535b725ae77Skettenis       char *alloc_name;
536b725ae77Skettenis       lookup_len = len + JAVA_PREFIX_LEN;
537b725ae77Skettenis 
538b725ae77Skettenis       alloc_name = alloca (lookup_len + 1);
539b725ae77Skettenis       memcpy (alloc_name, JAVA_PREFIX, JAVA_PREFIX_LEN);
540b725ae77Skettenis       memcpy (alloc_name + JAVA_PREFIX_LEN, linkage_name, len);
541b725ae77Skettenis       alloc_name[lookup_len] = '\0';
542b725ae77Skettenis 
543b725ae77Skettenis       lookup_name = alloc_name;
544b725ae77Skettenis       linkage_name_copy = alloc_name + JAVA_PREFIX_LEN;
545b725ae77Skettenis     }
546b725ae77Skettenis   else if (linkage_name[len] != '\0')
547b725ae77Skettenis     {
548b725ae77Skettenis       char *alloc_name;
549b725ae77Skettenis       lookup_len = len;
550b725ae77Skettenis 
551b725ae77Skettenis       alloc_name = alloca (lookup_len + 1);
552b725ae77Skettenis       memcpy (alloc_name, linkage_name, len);
553b725ae77Skettenis       alloc_name[lookup_len] = '\0';
554b725ae77Skettenis 
555b725ae77Skettenis       lookup_name = alloc_name;
556b725ae77Skettenis       linkage_name_copy = alloc_name;
557b725ae77Skettenis     }
558b725ae77Skettenis   else
559b725ae77Skettenis     {
560b725ae77Skettenis       lookup_len = len;
561b725ae77Skettenis       lookup_name = linkage_name;
562b725ae77Skettenis       linkage_name_copy = linkage_name;
563b725ae77Skettenis     }
564b725ae77Skettenis 
565b725ae77Skettenis   slot = (char **) htab_find_slot (objfile->demangled_names_hash,
566b725ae77Skettenis 				   lookup_name, INSERT);
567b725ae77Skettenis 
568b725ae77Skettenis   /* If this name is not in the hash table, add it.  */
569b725ae77Skettenis   if (*slot == NULL)
570b725ae77Skettenis     {
571b725ae77Skettenis       char *demangled_name = symbol_find_demangled_name (gsymbol,
572b725ae77Skettenis 							 linkage_name_copy);
573b725ae77Skettenis       int demangled_len = demangled_name ? strlen (demangled_name) : 0;
574b725ae77Skettenis 
575b725ae77Skettenis       /* If there is a demangled name, place it right after the mangled name.
576b725ae77Skettenis 	 Otherwise, just place a second zero byte after the end of the mangled
577b725ae77Skettenis 	 name.  */
578b725ae77Skettenis       *slot = obstack_alloc (&objfile->objfile_obstack,
579b725ae77Skettenis 			     lookup_len + demangled_len + 2);
580b725ae77Skettenis       memcpy (*slot, lookup_name, lookup_len + 1);
581b725ae77Skettenis       if (demangled_name != NULL)
582b725ae77Skettenis 	{
583b725ae77Skettenis 	  memcpy (*slot + lookup_len + 1, demangled_name, demangled_len + 1);
584b725ae77Skettenis 	  xfree (demangled_name);
585b725ae77Skettenis 	}
586b725ae77Skettenis       else
587b725ae77Skettenis 	(*slot)[lookup_len + 1] = '\0';
588b725ae77Skettenis     }
589b725ae77Skettenis 
590b725ae77Skettenis   gsymbol->name = *slot + lookup_len - len;
591b725ae77Skettenis   if ((*slot)[lookup_len + 1] != '\0')
592b725ae77Skettenis     gsymbol->language_specific.cplus_specific.demangled_name
593b725ae77Skettenis       = &(*slot)[lookup_len + 1];
594b725ae77Skettenis   else
595b725ae77Skettenis     gsymbol->language_specific.cplus_specific.demangled_name = NULL;
596b725ae77Skettenis }
597b725ae77Skettenis 
598b725ae77Skettenis /* Initialize the demangled name of GSYMBOL if possible.  Any required space
599b725ae77Skettenis    to store the name is obtained from the specified obstack.  The function
600b725ae77Skettenis    symbol_set_names, above, should be used instead where possible for more
601b725ae77Skettenis    efficient memory usage.  */
602b725ae77Skettenis 
603b725ae77Skettenis void
symbol_init_demangled_name(struct general_symbol_info * gsymbol,struct obstack * obstack)604b725ae77Skettenis symbol_init_demangled_name (struct general_symbol_info *gsymbol,
605b725ae77Skettenis                             struct obstack *obstack)
606b725ae77Skettenis {
607b725ae77Skettenis   char *mangled = gsymbol->name;
608b725ae77Skettenis   char *demangled = NULL;
609b725ae77Skettenis 
610b725ae77Skettenis   demangled = symbol_find_demangled_name (gsymbol, mangled);
611b725ae77Skettenis   if (gsymbol->language == language_cplus
612b725ae77Skettenis       || gsymbol->language == language_java
613b725ae77Skettenis       || gsymbol->language == language_objc)
614b725ae77Skettenis     {
615b725ae77Skettenis       if (demangled)
616b725ae77Skettenis 	{
617b725ae77Skettenis 	  gsymbol->language_specific.cplus_specific.demangled_name
618b725ae77Skettenis 	    = obsavestring (demangled, strlen (demangled), obstack);
619b725ae77Skettenis 	  xfree (demangled);
620b725ae77Skettenis 	}
621b725ae77Skettenis       else
622b725ae77Skettenis 	gsymbol->language_specific.cplus_specific.demangled_name = NULL;
623b725ae77Skettenis     }
624b725ae77Skettenis   else
625b725ae77Skettenis     {
626b725ae77Skettenis       /* Unknown language; just clean up quietly.  */
627b725ae77Skettenis       if (demangled)
628b725ae77Skettenis 	xfree (demangled);
629b725ae77Skettenis     }
630b725ae77Skettenis }
631b725ae77Skettenis 
632b725ae77Skettenis /* Return the source code name of a symbol.  In languages where
633b725ae77Skettenis    demangling is necessary, this is the demangled name.  */
634b725ae77Skettenis 
635b725ae77Skettenis char *
symbol_natural_name(const struct general_symbol_info * gsymbol)636b725ae77Skettenis symbol_natural_name (const struct general_symbol_info *gsymbol)
637b725ae77Skettenis {
638*63addd46Skettenis   switch (gsymbol->language)
639b725ae77Skettenis     {
640*63addd46Skettenis     case language_cplus:
641*63addd46Skettenis     case language_java:
642*63addd46Skettenis     case language_objc:
643*63addd46Skettenis       if (gsymbol->language_specific.cplus_specific.demangled_name != NULL)
644b725ae77Skettenis 	return gsymbol->language_specific.cplus_specific.demangled_name;
645*63addd46Skettenis       break;
646*63addd46Skettenis     case language_ada:
647*63addd46Skettenis       if (gsymbol->language_specific.cplus_specific.demangled_name != NULL)
648*63addd46Skettenis 	return gsymbol->language_specific.cplus_specific.demangled_name;
649b725ae77Skettenis       else
650*63addd46Skettenis 	return ada_decode_symbol (gsymbol);
651*63addd46Skettenis       break;
652*63addd46Skettenis     default:
653*63addd46Skettenis       break;
654b725ae77Skettenis     }
655*63addd46Skettenis   return gsymbol->name;
656b725ae77Skettenis }
657b725ae77Skettenis 
658b725ae77Skettenis /* Return the demangled name for a symbol based on the language for
659b725ae77Skettenis    that symbol.  If no demangled name exists, return NULL. */
660b725ae77Skettenis char *
symbol_demangled_name(struct general_symbol_info * gsymbol)661b725ae77Skettenis symbol_demangled_name (struct general_symbol_info *gsymbol)
662b725ae77Skettenis {
663*63addd46Skettenis   switch (gsymbol->language)
664*63addd46Skettenis     {
665*63addd46Skettenis     case language_cplus:
666*63addd46Skettenis     case language_java:
667*63addd46Skettenis     case language_objc:
668*63addd46Skettenis       if (gsymbol->language_specific.cplus_specific.demangled_name != NULL)
669b725ae77Skettenis 	return gsymbol->language_specific.cplus_specific.demangled_name;
670*63addd46Skettenis       break;
671*63addd46Skettenis     case language_ada:
672*63addd46Skettenis       if (gsymbol->language_specific.cplus_specific.demangled_name != NULL)
673*63addd46Skettenis 	return gsymbol->language_specific.cplus_specific.demangled_name;
674b725ae77Skettenis       else
675*63addd46Skettenis 	return ada_decode_symbol (gsymbol);
676*63addd46Skettenis       break;
677*63addd46Skettenis     default:
678*63addd46Skettenis       break;
679*63addd46Skettenis     }
680b725ae77Skettenis   return NULL;
681b725ae77Skettenis }
682b725ae77Skettenis 
683*63addd46Skettenis /* Return the search name of a symbol---generally the demangled or
684*63addd46Skettenis    linkage name of the symbol, depending on how it will be searched for.
685*63addd46Skettenis    If there is no distinct demangled name, then returns the same value
686*63addd46Skettenis    (same pointer) as SYMBOL_LINKAGE_NAME. */
symbol_search_name(const struct general_symbol_info * gsymbol)687*63addd46Skettenis char *symbol_search_name (const struct general_symbol_info *gsymbol) {
688*63addd46Skettenis   if (gsymbol->language == language_ada)
689*63addd46Skettenis     return gsymbol->name;
690*63addd46Skettenis   else
691*63addd46Skettenis     return symbol_natural_name (gsymbol);
692*63addd46Skettenis }
693*63addd46Skettenis 
694b725ae77Skettenis /* Initialize the structure fields to zero values.  */
695b725ae77Skettenis void
init_sal(struct symtab_and_line * sal)696b725ae77Skettenis init_sal (struct symtab_and_line *sal)
697b725ae77Skettenis {
698b725ae77Skettenis   sal->symtab = 0;
699b725ae77Skettenis   sal->section = 0;
700b725ae77Skettenis   sal->line = 0;
701b725ae77Skettenis   sal->pc = 0;
702b725ae77Skettenis   sal->end = 0;
703b725ae77Skettenis }
704b725ae77Skettenis 
705b725ae77Skettenis 
706b725ae77Skettenis 
707b725ae77Skettenis /* Find which partial symtab contains PC and SECTION.  Return 0 if
708b725ae77Skettenis    none.  We return the psymtab that contains a symbol whose address
709b725ae77Skettenis    exactly matches PC, or, if we cannot find an exact match, the
710b725ae77Skettenis    psymtab that contains a symbol whose address is closest to PC.  */
711b725ae77Skettenis struct partial_symtab *
find_pc_sect_psymtab(CORE_ADDR pc,asection * section)712b725ae77Skettenis find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
713b725ae77Skettenis {
714b725ae77Skettenis   struct partial_symtab *pst;
715b725ae77Skettenis   struct objfile *objfile;
716b725ae77Skettenis   struct minimal_symbol *msymbol;
717b725ae77Skettenis 
718b725ae77Skettenis   /* If we know that this is not a text address, return failure.  This is
719b725ae77Skettenis      necessary because we loop based on texthigh and textlow, which do
720b725ae77Skettenis      not include the data ranges.  */
721b725ae77Skettenis   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
722b725ae77Skettenis   if (msymbol
723b725ae77Skettenis       && (msymbol->type == mst_data
724b725ae77Skettenis 	  || msymbol->type == mst_bss
725b725ae77Skettenis 	  || msymbol->type == mst_abs
726b725ae77Skettenis 	  || msymbol->type == mst_file_data
727b725ae77Skettenis 	  || msymbol->type == mst_file_bss))
728b725ae77Skettenis     return NULL;
729e93f7393Sniklas 
730e93f7393Sniklas   ALL_PSYMTABS (objfile, pst)
731e93f7393Sniklas   {
732e93f7393Sniklas     if (pc >= pst->textlow && pc < pst->texthigh)
733e93f7393Sniklas       {
734e93f7393Sniklas 	struct partial_symtab *tpst;
735b725ae77Skettenis 	struct partial_symtab *best_pst = pst;
736b725ae77Skettenis 	struct partial_symbol *best_psym = NULL;
737e93f7393Sniklas 
738e93f7393Sniklas 	/* An objfile that has its functions reordered might have
739e93f7393Sniklas 	   many partial symbol tables containing the PC, but
740e93f7393Sniklas 	   we want the partial symbol table that contains the
741e93f7393Sniklas 	   function containing the PC.  */
742b725ae77Skettenis 	if (!(objfile->flags & OBJF_REORDERED) &&
743b725ae77Skettenis 	    section == 0)	/* can't validate section this way */
744e93f7393Sniklas 	  return (pst);
745e93f7393Sniklas 
746e93f7393Sniklas 	if (msymbol == NULL)
747e93f7393Sniklas 	  return (pst);
748e93f7393Sniklas 
749b725ae77Skettenis 	/* The code range of partial symtabs sometimes overlap, so, in
750b725ae77Skettenis 	   the loop below, we need to check all partial symtabs and
751b725ae77Skettenis 	   find the one that fits better for the given PC address. We
752b725ae77Skettenis 	   select the partial symtab that contains a symbol whose
753b725ae77Skettenis 	   address is closest to the PC address.  By closest we mean
754b725ae77Skettenis 	   that find_pc_sect_symbol returns the symbol with address
755b725ae77Skettenis 	   that is closest and still less than the given PC.  */
756e93f7393Sniklas 	for (tpst = pst; tpst != NULL; tpst = tpst->next)
757e93f7393Sniklas 	  {
758e93f7393Sniklas 	    if (pc >= tpst->textlow && pc < tpst->texthigh)
759e93f7393Sniklas 	      {
760e93f7393Sniklas 		struct partial_symbol *p;
761e93f7393Sniklas 
762b725ae77Skettenis 		p = find_pc_sect_psymbol (tpst, pc, section);
763e93f7393Sniklas 		if (p != NULL
764e93f7393Sniklas 		    && SYMBOL_VALUE_ADDRESS (p)
765e93f7393Sniklas 		    == SYMBOL_VALUE_ADDRESS (msymbol))
766e93f7393Sniklas 		  return (tpst);
767b725ae77Skettenis 		if (p != NULL)
768b725ae77Skettenis 		  {
769b725ae77Skettenis 		    /* We found a symbol in this partial symtab which
770b725ae77Skettenis 		       matches (or is closest to) PC, check whether it
771b725ae77Skettenis 		       is closer than our current BEST_PSYM.  Since
772b725ae77Skettenis 		       this symbol address is necessarily lower or
773b725ae77Skettenis 		       equal to PC, the symbol closer to PC is the
774b725ae77Skettenis 		       symbol which address is the highest.  */
775b725ae77Skettenis 		    /* This way we return the psymtab which contains
776b725ae77Skettenis 		       such best match symbol. This can help in cases
777b725ae77Skettenis 		       where the symbol information/debuginfo is not
778b725ae77Skettenis 		       complete, like for instance on IRIX6 with gcc,
779b725ae77Skettenis 		       where no debug info is emitted for
780b725ae77Skettenis 		       statics. (See also the nodebug.exp
781b725ae77Skettenis 		       testcase.)  */
782b725ae77Skettenis 		    if (best_psym == NULL
783b725ae77Skettenis 			|| SYMBOL_VALUE_ADDRESS (p)
784b725ae77Skettenis 			> SYMBOL_VALUE_ADDRESS (best_psym))
785b725ae77Skettenis 		      {
786b725ae77Skettenis 			best_psym = p;
787b725ae77Skettenis 			best_pst = tpst;
788e93f7393Sniklas 		      }
789e93f7393Sniklas 		  }
790b725ae77Skettenis 
791b725ae77Skettenis 	      }
792b725ae77Skettenis 	  }
793b725ae77Skettenis 	return (best_pst);
794e93f7393Sniklas       }
795e93f7393Sniklas   }
796e93f7393Sniklas   return (NULL);
797e93f7393Sniklas }
798e93f7393Sniklas 
799b725ae77Skettenis /* Find which partial symtab contains PC.  Return 0 if none.
800b725ae77Skettenis    Backward compatibility, no section */
801b725ae77Skettenis 
802b725ae77Skettenis struct partial_symtab *
find_pc_psymtab(CORE_ADDR pc)803b725ae77Skettenis find_pc_psymtab (CORE_ADDR pc)
804b725ae77Skettenis {
805b725ae77Skettenis   return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
806b725ae77Skettenis }
807b725ae77Skettenis 
808b725ae77Skettenis /* Find which partial symbol within a psymtab matches PC and SECTION.
809b725ae77Skettenis    Return 0 if none.  Check all psymtabs if PSYMTAB is 0.  */
810b725ae77Skettenis 
811e93f7393Sniklas struct partial_symbol *
find_pc_sect_psymbol(struct partial_symtab * psymtab,CORE_ADDR pc,asection * section)812b725ae77Skettenis find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
813b725ae77Skettenis 		      asection *section)
814e93f7393Sniklas {
815e93f7393Sniklas   struct partial_symbol *best = NULL, *p, **pp;
816e93f7393Sniklas   CORE_ADDR best_pc;
817e93f7393Sniklas 
818e93f7393Sniklas   if (!psymtab)
819b725ae77Skettenis     psymtab = find_pc_sect_psymtab (pc, section);
820e93f7393Sniklas   if (!psymtab)
821e93f7393Sniklas     return 0;
822e93f7393Sniklas 
823b725ae77Skettenis   /* Cope with programs that start at address 0 */
824b725ae77Skettenis   best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
825e93f7393Sniklas 
826e93f7393Sniklas   /* Search the global symbols as well as the static symbols, so that
827e93f7393Sniklas      find_pc_partial_function doesn't use a minimal symbol and thus
828e93f7393Sniklas      cache a bad endaddr.  */
829e93f7393Sniklas   for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
830e93f7393Sniklas     (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
831e93f7393Sniklas      < psymtab->n_global_syms);
832e93f7393Sniklas        pp++)
833e93f7393Sniklas     {
834e93f7393Sniklas       p = *pp;
835b725ae77Skettenis       if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
836e93f7393Sniklas 	  && SYMBOL_CLASS (p) == LOC_BLOCK
837e93f7393Sniklas 	  && pc >= SYMBOL_VALUE_ADDRESS (p)
838b725ae77Skettenis 	  && (SYMBOL_VALUE_ADDRESS (p) > best_pc
839b725ae77Skettenis 	      || (psymtab->textlow == 0
840b725ae77Skettenis 		  && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
841e93f7393Sniklas 	{
842b725ae77Skettenis 	  if (section)		/* match on a specific section */
843b725ae77Skettenis 	    {
844b725ae77Skettenis 	      fixup_psymbol_section (p, psymtab->objfile);
845b725ae77Skettenis 	      if (SYMBOL_BFD_SECTION (p) != section)
846b725ae77Skettenis 		continue;
847b725ae77Skettenis 	    }
848e93f7393Sniklas 	  best_pc = SYMBOL_VALUE_ADDRESS (p);
849e93f7393Sniklas 	  best = p;
850e93f7393Sniklas 	}
851e93f7393Sniklas     }
852b725ae77Skettenis 
853e93f7393Sniklas   for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
854e93f7393Sniklas     (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
855e93f7393Sniklas      < psymtab->n_static_syms);
856e93f7393Sniklas        pp++)
857e93f7393Sniklas     {
858e93f7393Sniklas       p = *pp;
859b725ae77Skettenis       if (SYMBOL_DOMAIN (p) == VAR_DOMAIN
860e93f7393Sniklas 	  && SYMBOL_CLASS (p) == LOC_BLOCK
861e93f7393Sniklas 	  && pc >= SYMBOL_VALUE_ADDRESS (p)
862b725ae77Skettenis 	  && (SYMBOL_VALUE_ADDRESS (p) > best_pc
863b725ae77Skettenis 	      || (psymtab->textlow == 0
864b725ae77Skettenis 		  && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
865e93f7393Sniklas 	{
866b725ae77Skettenis 	  if (section)		/* match on a specific section */
867b725ae77Skettenis 	    {
868b725ae77Skettenis 	      fixup_psymbol_section (p, psymtab->objfile);
869b725ae77Skettenis 	      if (SYMBOL_BFD_SECTION (p) != section)
870b725ae77Skettenis 		continue;
871b725ae77Skettenis 	    }
872e93f7393Sniklas 	  best_pc = SYMBOL_VALUE_ADDRESS (p);
873e93f7393Sniklas 	  best = p;
874e93f7393Sniklas 	}
875e93f7393Sniklas     }
876b725ae77Skettenis 
877e93f7393Sniklas   return best;
878e93f7393Sniklas }
879e93f7393Sniklas 
880b725ae77Skettenis /* Find which partial symbol within a psymtab matches PC.  Return 0 if none.
881b725ae77Skettenis    Check all psymtabs if PSYMTAB is 0.  Backwards compatibility, no section. */
882b725ae77Skettenis 
883b725ae77Skettenis struct partial_symbol *
find_pc_psymbol(struct partial_symtab * psymtab,CORE_ADDR pc)884b725ae77Skettenis find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
885b725ae77Skettenis {
886b725ae77Skettenis   return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
887b725ae77Skettenis }
888e93f7393Sniklas 
889b725ae77Skettenis /* Debug symbols usually don't have section information.  We need to dig that
890b725ae77Skettenis    out of the minimal symbols and stash that in the debug symbol.  */
891b725ae77Skettenis 
892b725ae77Skettenis static void
fixup_section(struct general_symbol_info * ginfo,struct objfile * objfile)893b725ae77Skettenis fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
894b725ae77Skettenis {
895b725ae77Skettenis   struct minimal_symbol *msym;
896b725ae77Skettenis   msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
897b725ae77Skettenis 
898b725ae77Skettenis   if (msym)
899b725ae77Skettenis     {
900b725ae77Skettenis       ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
901b725ae77Skettenis       ginfo->section = SYMBOL_SECTION (msym);
902b725ae77Skettenis     }
903*63addd46Skettenis   else if (objfile)
904*63addd46Skettenis     {
905*63addd46Skettenis       /* Static, function-local variables do appear in the linker
906*63addd46Skettenis 	 (minimal) symbols, but are frequently given names that won't
907*63addd46Skettenis 	 be found via lookup_minimal_symbol().  E.g., it has been
908*63addd46Skettenis 	 observed in frv-uclinux (ELF) executables that a static,
909*63addd46Skettenis 	 function-local variable named "foo" might appear in the
910*63addd46Skettenis 	 linker symbols as "foo.6" or "foo.3".  Thus, there is no
911*63addd46Skettenis 	 point in attempting to extend the lookup-by-name mechanism to
912*63addd46Skettenis 	 handle this case due to the fact that there can be multiple
913*63addd46Skettenis 	 names.
914*63addd46Skettenis 
915*63addd46Skettenis 	 So, instead, search the section table when lookup by name has
916*63addd46Skettenis 	 failed.  The ``addr'' and ``endaddr'' fields may have already
917*63addd46Skettenis 	 been relocated.  If so, the relocation offset (i.e. the
918*63addd46Skettenis 	 ANOFFSET value) needs to be subtracted from these values when
919*63addd46Skettenis 	 performing the comparison.  We unconditionally subtract it,
920*63addd46Skettenis 	 because, when no relocation has been performed, the ANOFFSET
921*63addd46Skettenis 	 value will simply be zero.
922*63addd46Skettenis 
923*63addd46Skettenis 	 The address of the symbol whose section we're fixing up HAS
924*63addd46Skettenis 	 NOT BEEN adjusted (relocated) yet.  It can't have been since
925*63addd46Skettenis 	 the section isn't yet known and knowing the section is
926*63addd46Skettenis 	 necessary in order to add the correct relocation value.  In
927*63addd46Skettenis 	 other words, we wouldn't even be in this function (attempting
928*63addd46Skettenis 	 to compute the section) if it were already known.
929*63addd46Skettenis 
930*63addd46Skettenis 	 Note that it is possible to search the minimal symbols
931*63addd46Skettenis 	 (subtracting the relocation value if necessary) to find the
932*63addd46Skettenis 	 matching minimal symbol, but this is overkill and much less
933*63addd46Skettenis 	 efficient.  It is not necessary to find the matching minimal
934*63addd46Skettenis 	 symbol, only its section.
935*63addd46Skettenis 
936*63addd46Skettenis 	 Note that this technique (of doing a section table search)
937*63addd46Skettenis 	 can fail when unrelocated section addresses overlap.  For
938*63addd46Skettenis 	 this reason, we still attempt a lookup by name prior to doing
939*63addd46Skettenis 	 a search of the section table.  */
940*63addd46Skettenis 
941*63addd46Skettenis       CORE_ADDR addr;
942*63addd46Skettenis       struct obj_section *s;
943*63addd46Skettenis 
944*63addd46Skettenis       addr = ginfo->value.address;
945*63addd46Skettenis 
946*63addd46Skettenis       ALL_OBJFILE_OSECTIONS (objfile, s)
947*63addd46Skettenis 	{
948*63addd46Skettenis 	  int idx = s->the_bfd_section->index;
949*63addd46Skettenis 	  CORE_ADDR offset = ANOFFSET (objfile->section_offsets, idx);
950*63addd46Skettenis 
951*63addd46Skettenis 	  if (s->addr - offset <= addr && addr < s->endaddr - offset)
952*63addd46Skettenis 	    {
953*63addd46Skettenis 	      ginfo->bfd_section = s->the_bfd_section;
954*63addd46Skettenis 	      ginfo->section = idx;
955*63addd46Skettenis 	      return;
956*63addd46Skettenis 	    }
957*63addd46Skettenis 	}
958*63addd46Skettenis     }
959b725ae77Skettenis }
960b725ae77Skettenis 
961b725ae77Skettenis struct symbol *
fixup_symbol_section(struct symbol * sym,struct objfile * objfile)962b725ae77Skettenis fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
963b725ae77Skettenis {
964b725ae77Skettenis   if (!sym)
965b725ae77Skettenis     return NULL;
966b725ae77Skettenis 
967b725ae77Skettenis   if (SYMBOL_BFD_SECTION (sym))
968b725ae77Skettenis     return sym;
969b725ae77Skettenis 
970b725ae77Skettenis   fixup_section (&sym->ginfo, objfile);
971b725ae77Skettenis 
972b725ae77Skettenis   return sym;
973b725ae77Skettenis }
974b725ae77Skettenis 
975b725ae77Skettenis struct partial_symbol *
fixup_psymbol_section(struct partial_symbol * psym,struct objfile * objfile)976b725ae77Skettenis fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
977b725ae77Skettenis {
978b725ae77Skettenis   if (!psym)
979b725ae77Skettenis     return NULL;
980b725ae77Skettenis 
981b725ae77Skettenis   if (SYMBOL_BFD_SECTION (psym))
982b725ae77Skettenis     return psym;
983b725ae77Skettenis 
984b725ae77Skettenis   fixup_section (&psym->ginfo, objfile);
985b725ae77Skettenis 
986b725ae77Skettenis   return psym;
987b725ae77Skettenis }
988b725ae77Skettenis 
989e93f7393Sniklas /* Find the definition for a specified symbol name NAME
990b725ae77Skettenis    in domain DOMAIN, visible from lexical block BLOCK.
991e93f7393Sniklas    Returns the struct symbol pointer, or zero if no symbol is found.
992e93f7393Sniklas    If SYMTAB is non-NULL, store the symbol table in which the
993e93f7393Sniklas    symbol was found there, or NULL if not found.
994e93f7393Sniklas    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
995e93f7393Sniklas    NAME is a field of the current implied argument `this'.  If so set
996e93f7393Sniklas    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
997e93f7393Sniklas    BLOCK_FOUND is set to the block in which NAME is found (in the case of
998e93f7393Sniklas    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
999e93f7393Sniklas 
1000e93f7393Sniklas /* This function has a bunch of loops in it and it would seem to be
1001e93f7393Sniklas    attractive to put in some QUIT's (though I'm not really sure
1002e93f7393Sniklas    whether it can run long enough to be really important).  But there
1003e93f7393Sniklas    are a few calls for which it would appear to be bad news to quit
1004b725ae77Skettenis    out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c.  (Note
1005b725ae77Skettenis    that there is C++ code below which can error(), but that probably
1006b725ae77Skettenis    doesn't affect these calls since they are looking for a known
1007b725ae77Skettenis    variable and thus can probably assume it will never hit the C++
1008b725ae77Skettenis    code).  */
1009e93f7393Sniklas 
1010e93f7393Sniklas struct symbol *
lookup_symbol(const char * name,const struct block * block,const domain_enum domain,int * is_a_field_of_this,struct symtab ** symtab)1011b725ae77Skettenis lookup_symbol (const char *name, const struct block *block,
1012b725ae77Skettenis 	       const domain_enum domain, int *is_a_field_of_this,
1013b725ae77Skettenis 	       struct symtab **symtab)
1014e93f7393Sniklas {
1015b725ae77Skettenis   char *demangled_name = NULL;
1016b725ae77Skettenis   const char *modified_name = NULL;
1017b725ae77Skettenis   const char *mangled_name = NULL;
1018b725ae77Skettenis   int needtofreename = 0;
1019b725ae77Skettenis   struct symbol *returnval;
1020b725ae77Skettenis 
1021b725ae77Skettenis   modified_name = name;
1022b725ae77Skettenis 
1023*63addd46Skettenis   /* If we are using C++ or Java, demangle the name before doing a lookup, so
1024b725ae77Skettenis      we can always binary search. */
1025b725ae77Skettenis   if (current_language->la_language == language_cplus)
1026b725ae77Skettenis     {
1027b725ae77Skettenis       demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
1028b725ae77Skettenis       if (demangled_name)
1029b725ae77Skettenis 	{
1030b725ae77Skettenis 	  mangled_name = name;
1031b725ae77Skettenis 	  modified_name = demangled_name;
1032b725ae77Skettenis 	  needtofreename = 1;
1033b725ae77Skettenis 	}
1034b725ae77Skettenis     }
1035*63addd46Skettenis   else if (current_language->la_language == language_java)
1036*63addd46Skettenis     {
1037*63addd46Skettenis       demangled_name = cplus_demangle (name,
1038*63addd46Skettenis 		      		       DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
1039*63addd46Skettenis       if (demangled_name)
1040*63addd46Skettenis 	{
1041*63addd46Skettenis 	  mangled_name = name;
1042*63addd46Skettenis 	  modified_name = demangled_name;
1043*63addd46Skettenis 	  needtofreename = 1;
1044*63addd46Skettenis 	}
1045*63addd46Skettenis     }
1046b725ae77Skettenis 
1047b725ae77Skettenis   if (case_sensitivity == case_sensitive_off)
1048b725ae77Skettenis     {
1049b725ae77Skettenis       char *copy;
1050b725ae77Skettenis       int len, i;
1051b725ae77Skettenis 
1052b725ae77Skettenis       len = strlen (name);
1053b725ae77Skettenis       copy = (char *) alloca (len + 1);
1054b725ae77Skettenis       for (i= 0; i < len; i++)
1055b725ae77Skettenis         copy[i] = tolower (name[i]);
1056b725ae77Skettenis       copy[len] = 0;
1057b725ae77Skettenis       modified_name = copy;
1058b725ae77Skettenis     }
1059b725ae77Skettenis 
1060b725ae77Skettenis   returnval = lookup_symbol_aux (modified_name, mangled_name, block,
1061b725ae77Skettenis 				 domain, is_a_field_of_this, symtab);
1062b725ae77Skettenis   if (needtofreename)
1063b725ae77Skettenis     xfree (demangled_name);
1064b725ae77Skettenis 
1065b725ae77Skettenis   return returnval;
1066b725ae77Skettenis }
1067b725ae77Skettenis 
1068b725ae77Skettenis /* Behave like lookup_symbol_aux except that NAME is the natural name
1069b725ae77Skettenis    of the symbol that we're looking for and, if LINKAGE_NAME is
1070b725ae77Skettenis    non-NULL, ensure that the symbol's linkage name matches as
1071b725ae77Skettenis    well.  */
1072b725ae77Skettenis 
1073b725ae77Skettenis static struct symbol *
lookup_symbol_aux(const char * name,const char * linkage_name,const struct block * block,const domain_enum domain,int * is_a_field_of_this,struct symtab ** symtab)1074b725ae77Skettenis lookup_symbol_aux (const char *name, const char *linkage_name,
1075b725ae77Skettenis 		   const struct block *block, const domain_enum domain,
1076b725ae77Skettenis 		   int *is_a_field_of_this, struct symtab **symtab)
1077b725ae77Skettenis {
1078b725ae77Skettenis   struct symbol *sym;
1079b725ae77Skettenis 
1080b725ae77Skettenis   /* Make sure we do something sensible with is_a_field_of_this, since
1081b725ae77Skettenis      the callers that set this parameter to some non-null value will
1082b725ae77Skettenis      certainly use it later and expect it to be either 0 or 1.
1083b725ae77Skettenis      If we don't set it, the contents of is_a_field_of_this are
1084b725ae77Skettenis      undefined.  */
1085b725ae77Skettenis   if (is_a_field_of_this != NULL)
1086b725ae77Skettenis     *is_a_field_of_this = 0;
1087b725ae77Skettenis 
1088b725ae77Skettenis   /* Search specified block and its superiors.  Don't search
1089b725ae77Skettenis      STATIC_BLOCK or GLOBAL_BLOCK.  */
1090b725ae77Skettenis 
1091b725ae77Skettenis   sym = lookup_symbol_aux_local (name, linkage_name, block, domain,
1092b725ae77Skettenis 				 symtab);
1093b725ae77Skettenis   if (sym != NULL)
1094b725ae77Skettenis     return sym;
1095b725ae77Skettenis 
1096b725ae77Skettenis   /* If requested to do so by the caller and if appropriate for the
1097b725ae77Skettenis      current language, check to see if NAME is a field of `this'. */
1098b725ae77Skettenis 
1099b725ae77Skettenis   if (current_language->la_value_of_this != NULL
1100b725ae77Skettenis       && is_a_field_of_this != NULL)
1101b725ae77Skettenis     {
1102b725ae77Skettenis       struct value *v = current_language->la_value_of_this (0);
1103b725ae77Skettenis 
1104b725ae77Skettenis       if (v && check_field (v, name))
1105b725ae77Skettenis 	{
1106b725ae77Skettenis 	  *is_a_field_of_this = 1;
1107b725ae77Skettenis 	  if (symtab != NULL)
1108b725ae77Skettenis 	    *symtab = NULL;
1109b725ae77Skettenis 	  return NULL;
1110b725ae77Skettenis 	}
1111b725ae77Skettenis     }
1112b725ae77Skettenis 
1113b725ae77Skettenis   /* Now do whatever is appropriate for the current language to look
1114b725ae77Skettenis      up static and global variables.  */
1115b725ae77Skettenis 
1116b725ae77Skettenis   sym = current_language->la_lookup_symbol_nonlocal (name, linkage_name,
1117b725ae77Skettenis 						     block, domain,
1118b725ae77Skettenis 						     symtab);
1119b725ae77Skettenis   if (sym != NULL)
1120b725ae77Skettenis     return sym;
1121b725ae77Skettenis 
1122b725ae77Skettenis   /* Now search all static file-level symbols.  Not strictly correct,
1123b725ae77Skettenis      but more useful than an error.  Do the symtabs first, then check
1124b725ae77Skettenis      the psymtabs.  If a psymtab indicates the existence of the
1125b725ae77Skettenis      desired name as a file-level static, then do psymtab-to-symtab
1126b725ae77Skettenis      conversion on the fly and return the found symbol. */
1127b725ae77Skettenis 
1128b725ae77Skettenis   sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, linkage_name,
1129b725ae77Skettenis 				   domain, symtab);
1130b725ae77Skettenis   if (sym != NULL)
1131b725ae77Skettenis     return sym;
1132b725ae77Skettenis 
1133b725ae77Skettenis   sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, linkage_name,
1134b725ae77Skettenis 				    domain, symtab);
1135b725ae77Skettenis   if (sym != NULL)
1136b725ae77Skettenis     return sym;
1137b725ae77Skettenis 
1138b725ae77Skettenis   if (symtab != NULL)
1139b725ae77Skettenis     *symtab = NULL;
1140b725ae77Skettenis   return NULL;
1141b725ae77Skettenis }
1142b725ae77Skettenis 
1143b725ae77Skettenis /* Check to see if the symbol is defined in BLOCK or its superiors.
1144b725ae77Skettenis    Don't search STATIC_BLOCK or GLOBAL_BLOCK.  */
1145b725ae77Skettenis 
1146b725ae77Skettenis static struct symbol *
lookup_symbol_aux_local(const char * name,const char * linkage_name,const struct block * block,const domain_enum domain,struct symtab ** symtab)1147b725ae77Skettenis lookup_symbol_aux_local (const char *name, const char *linkage_name,
1148b725ae77Skettenis 			 const struct block *block,
1149b725ae77Skettenis 			 const domain_enum domain,
1150b725ae77Skettenis 			 struct symtab **symtab)
1151b725ae77Skettenis {
1152b725ae77Skettenis   struct symbol *sym;
1153b725ae77Skettenis   const struct block *static_block = block_static_block (block);
1154b725ae77Skettenis 
1155b725ae77Skettenis   /* Check if either no block is specified or it's a global block.  */
1156b725ae77Skettenis 
1157b725ae77Skettenis   if (static_block == NULL)
1158b725ae77Skettenis     return NULL;
1159b725ae77Skettenis 
1160b725ae77Skettenis   while (block != static_block)
1161b725ae77Skettenis     {
1162b725ae77Skettenis       sym = lookup_symbol_aux_block (name, linkage_name, block, domain,
1163b725ae77Skettenis 				     symtab);
1164b725ae77Skettenis       if (sym != NULL)
1165b725ae77Skettenis 	return sym;
1166b725ae77Skettenis       block = BLOCK_SUPERBLOCK (block);
1167b725ae77Skettenis     }
1168b725ae77Skettenis 
1169b725ae77Skettenis   /* We've reached the static block without finding a result.  */
1170b725ae77Skettenis 
1171b725ae77Skettenis   return NULL;
1172b725ae77Skettenis }
1173b725ae77Skettenis 
1174b725ae77Skettenis /* Look up a symbol in a block; if found, locate its symtab, fixup the
1175b725ae77Skettenis    symbol, and set block_found appropriately.  */
1176b725ae77Skettenis 
1177b725ae77Skettenis struct symbol *
lookup_symbol_aux_block(const char * name,const char * linkage_name,const struct block * block,const domain_enum domain,struct symtab ** symtab)1178b725ae77Skettenis lookup_symbol_aux_block (const char *name, const char *linkage_name,
1179b725ae77Skettenis 			 const struct block *block,
1180b725ae77Skettenis 			 const domain_enum domain,
1181b725ae77Skettenis 			 struct symtab **symtab)
1182b725ae77Skettenis {
1183b725ae77Skettenis   struct symbol *sym;
1184b725ae77Skettenis   struct objfile *objfile = NULL;
1185e93f7393Sniklas   struct blockvector *bv;
1186b725ae77Skettenis   struct block *b;
1187b725ae77Skettenis   struct symtab *s = NULL;
1188e93f7393Sniklas 
1189b725ae77Skettenis   sym = lookup_block_symbol (block, name, linkage_name, domain);
1190e93f7393Sniklas   if (sym)
1191e93f7393Sniklas     {
1192e93f7393Sniklas       block_found = block;
1193e93f7393Sniklas       if (symtab != NULL)
1194e93f7393Sniklas 	{
1195e93f7393Sniklas 	  /* Search the list of symtabs for one which contains the
1196e93f7393Sniklas 	     address of the start of this block.  */
1197e93f7393Sniklas 	  ALL_SYMTABS (objfile, s)
1198e93f7393Sniklas 	    {
1199e93f7393Sniklas 	      bv = BLOCKVECTOR (s);
1200e93f7393Sniklas 	      b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1201e93f7393Sniklas 	      if (BLOCK_START (b) <= BLOCK_START (block)
1202e93f7393Sniklas 		  && BLOCK_END (b) > BLOCK_START (block))
1203e93f7393Sniklas 		goto found;
1204e93f7393Sniklas 	    }
1205e93f7393Sniklas 	found:
1206e93f7393Sniklas 	  *symtab = s;
1207e93f7393Sniklas 	}
1208e93f7393Sniklas 
1209b725ae77Skettenis       return fixup_symbol_section (sym, objfile);
1210e93f7393Sniklas     }
1211e93f7393Sniklas 
1212b725ae77Skettenis   return NULL;
1213e93f7393Sniklas }
1214e93f7393Sniklas 
1215b725ae77Skettenis /* Check to see if the symbol is defined in one of the symtabs.
1216b725ae77Skettenis    BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1217b725ae77Skettenis    depending on whether or not we want to search global symbols or
1218b725ae77Skettenis    static symbols.  */
1219e93f7393Sniklas 
1220b725ae77Skettenis static struct symbol *
lookup_symbol_aux_symtabs(int block_index,const char * name,const char * linkage_name,const domain_enum domain,struct symtab ** symtab)1221b725ae77Skettenis lookup_symbol_aux_symtabs (int block_index,
1222b725ae77Skettenis 			   const char *name, const char *linkage_name,
1223b725ae77Skettenis 			   const domain_enum domain,
1224b725ae77Skettenis 			   struct symtab **symtab)
1225e93f7393Sniklas {
1226b725ae77Skettenis   struct symbol *sym;
1227b725ae77Skettenis   struct objfile *objfile;
1228b725ae77Skettenis   struct blockvector *bv;
1229b725ae77Skettenis   const struct block *block;
1230b725ae77Skettenis   struct symtab *s;
1231e93f7393Sniklas 
1232e93f7393Sniklas   ALL_SYMTABS (objfile, s)
1233e93f7393Sniklas   {
1234e93f7393Sniklas     bv = BLOCKVECTOR (s);
1235b725ae77Skettenis     block = BLOCKVECTOR_BLOCK (bv, block_index);
1236b725ae77Skettenis     sym = lookup_block_symbol (block, name, linkage_name, domain);
1237e93f7393Sniklas     if (sym)
1238e93f7393Sniklas       {
1239e93f7393Sniklas 	block_found = block;
1240e93f7393Sniklas 	if (symtab != NULL)
1241e93f7393Sniklas 	  *symtab = s;
1242b725ae77Skettenis 	return fixup_symbol_section (sym, objfile);
1243e93f7393Sniklas       }
1244e93f7393Sniklas   }
1245e93f7393Sniklas 
1246b725ae77Skettenis   return NULL;
1247b725ae77Skettenis }
1248e93f7393Sniklas 
1249b725ae77Skettenis /* Check to see if the symbol is defined in one of the partial
1250b725ae77Skettenis    symtabs.  BLOCK_INDEX should be either GLOBAL_BLOCK or
1251b725ae77Skettenis    STATIC_BLOCK, depending on whether or not we want to search global
1252b725ae77Skettenis    symbols or static symbols.  */
1253b725ae77Skettenis 
1254b725ae77Skettenis static struct symbol *
lookup_symbol_aux_psymtabs(int block_index,const char * name,const char * linkage_name,const domain_enum domain,struct symtab ** symtab)1255b725ae77Skettenis lookup_symbol_aux_psymtabs (int block_index, const char *name,
1256b725ae77Skettenis 			    const char *linkage_name,
1257b725ae77Skettenis 			    const domain_enum domain,
1258b725ae77Skettenis 			    struct symtab **symtab)
1259b725ae77Skettenis {
1260b725ae77Skettenis   struct symbol *sym;
1261b725ae77Skettenis   struct objfile *objfile;
1262b725ae77Skettenis   struct blockvector *bv;
1263b725ae77Skettenis   const struct block *block;
1264b725ae77Skettenis   struct partial_symtab *ps;
1265b725ae77Skettenis   struct symtab *s;
1266b725ae77Skettenis   const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
1267b725ae77Skettenis 
1268b725ae77Skettenis   ALL_PSYMTABS (objfile, ps)
1269b725ae77Skettenis   {
1270b725ae77Skettenis     if (!ps->readin
1271b725ae77Skettenis 	&& lookup_partial_symbol (ps, name, linkage_name,
1272b725ae77Skettenis 				  psymtab_index, domain))
1273b725ae77Skettenis       {
1274b725ae77Skettenis 	s = PSYMTAB_TO_SYMTAB (ps);
1275b725ae77Skettenis 	bv = BLOCKVECTOR (s);
1276b725ae77Skettenis 	block = BLOCKVECTOR_BLOCK (bv, block_index);
1277b725ae77Skettenis 	sym = lookup_block_symbol (block, name, linkage_name, domain);
1278b725ae77Skettenis 	if (!sym)
1279b725ae77Skettenis 	  {
1280b725ae77Skettenis 	    /* This shouldn't be necessary, but as a last resort try
1281b725ae77Skettenis 	       looking in the statics even though the psymtab claimed
1282b725ae77Skettenis 	       the symbol was global, or vice-versa. It's possible
1283b725ae77Skettenis 	       that the psymtab gets it wrong in some cases.  */
1284b725ae77Skettenis 
1285b725ae77Skettenis 	    /* FIXME: carlton/2002-09-30: Should we really do that?
1286b725ae77Skettenis 	       If that happens, isn't it likely to be a GDB error, in
1287b725ae77Skettenis 	       which case we should fix the GDB error rather than
1288b725ae77Skettenis 	       silently dealing with it here?  So I'd vote for
1289b725ae77Skettenis 	       removing the check for the symbol in the other
1290b725ae77Skettenis 	       block.  */
1291b725ae77Skettenis 	    block = BLOCKVECTOR_BLOCK (bv,
1292b725ae77Skettenis 				       block_index == GLOBAL_BLOCK ?
1293b725ae77Skettenis 				       STATIC_BLOCK : GLOBAL_BLOCK);
1294b725ae77Skettenis 	    sym = lookup_block_symbol (block, name, linkage_name, domain);
1295b725ae77Skettenis 	    if (!sym)
1296b725ae77Skettenis 	      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>).",
1297b725ae77Skettenis 		     block_index == GLOBAL_BLOCK ? "global" : "static",
1298b725ae77Skettenis 		     name, ps->filename, name, name);
1299b725ae77Skettenis 	  }
1300b725ae77Skettenis 	if (symtab != NULL)
1301b725ae77Skettenis 	  *symtab = s;
1302b725ae77Skettenis 	return fixup_symbol_section (sym, objfile);
1303b725ae77Skettenis       }
1304b725ae77Skettenis   }
1305b725ae77Skettenis 
1306b725ae77Skettenis   return NULL;
1307b725ae77Skettenis }
1308b725ae77Skettenis 
1309b725ae77Skettenis #if 0
1310b725ae77Skettenis /* Check for the possibility of the symbol being a function or a
1311b725ae77Skettenis    mangled variable that is stored in one of the minimal symbol
1312b725ae77Skettenis    tables.  Eventually, all global symbols might be resolved in this
1313b725ae77Skettenis    way.  */
1314b725ae77Skettenis 
1315b725ae77Skettenis /* NOTE: carlton/2002-12-05: At one point, this function was part of
1316b725ae77Skettenis    lookup_symbol_aux, and what are now 'return' statements within
1317b725ae77Skettenis    lookup_symbol_aux_minsyms returned from lookup_symbol_aux, even if
1318b725ae77Skettenis    sym was NULL.  As far as I can tell, this was basically accidental;
1319b725ae77Skettenis    it didn't happen every time that msymbol was non-NULL, but only if
1320b725ae77Skettenis    some additional conditions held as well, and it caused problems
1321b725ae77Skettenis    with HP-generated symbol tables.  */
1322b725ae77Skettenis 
1323b725ae77Skettenis /* NOTE: carlton/2003-05-14: This function was once used as part of
1324b725ae77Skettenis    lookup_symbol.  It is currently unnecessary for correctness
1325b725ae77Skettenis    reasons, however, and using it doesn't seem to be any faster than
1326b725ae77Skettenis    using lookup_symbol_aux_psymtabs, so I'm commenting it out.  */
1327b725ae77Skettenis 
1328b725ae77Skettenis static struct symbol *
1329b725ae77Skettenis lookup_symbol_aux_minsyms (const char *name,
1330b725ae77Skettenis 			   const char *linkage_name,
1331b725ae77Skettenis 			   const domain_enum domain,
1332b725ae77Skettenis 			   int *is_a_field_of_this,
1333b725ae77Skettenis 			   struct symtab **symtab)
1334b725ae77Skettenis {
1335b725ae77Skettenis   struct symbol *sym;
1336b725ae77Skettenis   struct blockvector *bv;
1337b725ae77Skettenis   const struct block *block;
1338b725ae77Skettenis   struct minimal_symbol *msymbol;
1339b725ae77Skettenis   struct symtab *s;
1340b725ae77Skettenis 
1341b725ae77Skettenis   if (domain == VAR_DOMAIN)
1342e93f7393Sniklas     {
1343e93f7393Sniklas       msymbol = lookup_minimal_symbol (name, NULL, NULL);
1344b725ae77Skettenis 
1345e93f7393Sniklas       if (msymbol != NULL)
1346e93f7393Sniklas 	{
1347b725ae77Skettenis 	  /* OK, we found a minimal symbol in spite of not finding any
1348b725ae77Skettenis 	     symbol. There are various possible explanations for
1349b725ae77Skettenis 	     this. One possibility is the symbol exists in code not
1350b725ae77Skettenis 	     compiled -g. Another possibility is that the 'psymtab'
1351b725ae77Skettenis 	     isn't doing its job.  A third possibility, related to #2,
1352b725ae77Skettenis 	     is that we were confused by name-mangling. For instance,
1353b725ae77Skettenis 	     maybe the psymtab isn't doing its job because it only
1354b725ae77Skettenis 	     know about demangled names, but we were given a mangled
1355b725ae77Skettenis 	     name...  */
1356b725ae77Skettenis 
1357b725ae77Skettenis 	  /* We first use the address in the msymbol to try to locate
1358b725ae77Skettenis 	     the appropriate symtab. Note that find_pc_sect_symtab()
1359b725ae77Skettenis 	     has a side-effect of doing psymtab-to-symtab expansion,
1360b725ae77Skettenis 	     for the found symtab.  */
1361b725ae77Skettenis 	  s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
1362b725ae77Skettenis 				   SYMBOL_BFD_SECTION (msymbol));
1363e93f7393Sniklas 	  if (s != NULL)
1364e93f7393Sniklas 	    {
1365e93f7393Sniklas 	      /* This is a function which has a symtab for its address.  */
1366e93f7393Sniklas 	      bv = BLOCKVECTOR (s);
1367e93f7393Sniklas 	      block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1368b725ae77Skettenis 
1369b725ae77Skettenis 	      /* This call used to pass `SYMBOL_LINKAGE_NAME (msymbol)' as the
1370b725ae77Skettenis 	         `name' argument to lookup_block_symbol.  But the name
1371b725ae77Skettenis 	         of a minimal symbol is always mangled, so that seems
1372b725ae77Skettenis 	         to be clearly the wrong thing to pass as the
1373b725ae77Skettenis 	         unmangled name.  */
1374b725ae77Skettenis 	      sym =
1375b725ae77Skettenis 		lookup_block_symbol (block, name, linkage_name, domain);
1376e93f7393Sniklas 	      /* We kept static functions in minimal symbol table as well as
1377e93f7393Sniklas 	         in static scope. We want to find them in the symbol table. */
1378b725ae77Skettenis 	      if (!sym)
1379b725ae77Skettenis 		{
1380e93f7393Sniklas 		  block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1381b725ae77Skettenis 		  sym = lookup_block_symbol (block, name,
1382b725ae77Skettenis 					     linkage_name, domain);
1383e93f7393Sniklas 		}
1384e93f7393Sniklas 
1385b725ae77Skettenis 	      /* NOTE: carlton/2002-12-04: The following comment was
1386b725ae77Skettenis 		 taken from a time when two versions of this function
1387b725ae77Skettenis 		 were part of the body of lookup_symbol_aux: this
1388b725ae77Skettenis 		 comment was taken from the version of the function
1389b725ae77Skettenis 		 that was #ifdef HPUXHPPA, and the comment was right
1390b725ae77Skettenis 		 before the 'return NULL' part of lookup_symbol_aux.
1391b725ae77Skettenis 		 (Hence the "Fall through and return 0" comment.)
1392b725ae77Skettenis 		 Elena did some digging into the situation for
1393b725ae77Skettenis 		 Fortran, and she reports:
1394b725ae77Skettenis 
1395b725ae77Skettenis 		 "I asked around (thanks to Jeff Knaggs), and I think
1396b725ae77Skettenis 		 the story for Fortran goes like this:
1397b725ae77Skettenis 
1398b725ae77Skettenis 		 "Apparently, in older Fortrans, '_' was not part of
1399b725ae77Skettenis 		 the user namespace.  g77 attached a final '_' to
1400b725ae77Skettenis 		 procedure names as the exported symbols for linkage
1401b725ae77Skettenis 		 (foo_) , but the symbols went in the debug info just
1402b725ae77Skettenis 		 like 'foo'. The rationale behind this is not
1403b725ae77Skettenis 		 completely clear, and maybe it was done to other
1404b725ae77Skettenis 		 symbols as well, not just procedures."  */
1405b725ae77Skettenis 
1406b725ae77Skettenis 	      /* If we get here with sym == 0, the symbol was
1407b725ae77Skettenis 	         found in the minimal symbol table
1408e93f7393Sniklas 	         but not in the symtab.
1409b725ae77Skettenis 	         Fall through and return 0 to use the msymbol
1410b725ae77Skettenis 	         definition of "foo_".
1411b725ae77Skettenis 	         (Note that outer code generally follows up a call
1412b725ae77Skettenis 	         to this routine with a call to lookup_minimal_symbol(),
1413b725ae77Skettenis 	         so a 0 return means we'll just flow into that other routine).
1414e93f7393Sniklas 
1415e93f7393Sniklas 	         This happens for Fortran  "foo_" symbols,
1416e93f7393Sniklas 	         which are "foo" in the symtab.
1417e93f7393Sniklas 
1418e93f7393Sniklas 	         This can also happen if "asm" is used to make a
1419e93f7393Sniklas 	         regular symbol but not a debugging symbol, e.g.
1420e93f7393Sniklas 	         asm(".globl _main");
1421e93f7393Sniklas 	         asm("_main:");
1422e93f7393Sniklas 	       */
1423e93f7393Sniklas 
1424b725ae77Skettenis 	      if (symtab != NULL && sym != NULL)
1425e93f7393Sniklas 		*symtab = s;
1426b725ae77Skettenis 	      return fixup_symbol_section (sym, s->objfile);
1427b725ae77Skettenis 	    }
1428b725ae77Skettenis 	}
1429b725ae77Skettenis     }
1430b725ae77Skettenis 
1431b725ae77Skettenis   return NULL;
1432b725ae77Skettenis }
1433b725ae77Skettenis #endif /* 0 */
1434b725ae77Skettenis 
1435b725ae77Skettenis /* A default version of lookup_symbol_nonlocal for use by languages
1436b725ae77Skettenis    that can't think of anything better to do.  This implements the C
1437b725ae77Skettenis    lookup rules.  */
1438b725ae77Skettenis 
1439b725ae77Skettenis struct symbol *
basic_lookup_symbol_nonlocal(const char * name,const char * linkage_name,const struct block * block,const domain_enum domain,struct symtab ** symtab)1440b725ae77Skettenis basic_lookup_symbol_nonlocal (const char *name,
1441b725ae77Skettenis 			      const char *linkage_name,
1442b725ae77Skettenis 			      const struct block *block,
1443b725ae77Skettenis 			      const domain_enum domain,
1444b725ae77Skettenis 			      struct symtab **symtab)
1445b725ae77Skettenis {
1446b725ae77Skettenis   struct symbol *sym;
1447b725ae77Skettenis 
1448b725ae77Skettenis   /* NOTE: carlton/2003-05-19: The comments below were written when
1449b725ae77Skettenis      this (or what turned into this) was part of lookup_symbol_aux;
1450b725ae77Skettenis      I'm much less worried about these questions now, since these
1451b725ae77Skettenis      decisions have turned out well, but I leave these comments here
1452b725ae77Skettenis      for posterity.  */
1453b725ae77Skettenis 
1454b725ae77Skettenis   /* NOTE: carlton/2002-12-05: There is a question as to whether or
1455b725ae77Skettenis      not it would be appropriate to search the current global block
1456b725ae77Skettenis      here as well.  (That's what this code used to do before the
1457b725ae77Skettenis      is_a_field_of_this check was moved up.)  On the one hand, it's
1458b725ae77Skettenis      redundant with the lookup_symbol_aux_symtabs search that happens
1459b725ae77Skettenis      next.  On the other hand, if decode_line_1 is passed an argument
1460b725ae77Skettenis      like filename:var, then the user presumably wants 'var' to be
1461b725ae77Skettenis      searched for in filename.  On the third hand, there shouldn't be
1462b725ae77Skettenis      multiple global variables all of which are named 'var', and it's
1463b725ae77Skettenis      not like decode_line_1 has ever restricted its search to only
1464b725ae77Skettenis      global variables in a single filename.  All in all, only
1465b725ae77Skettenis      searching the static block here seems best: it's correct and it's
1466b725ae77Skettenis      cleanest.  */
1467b725ae77Skettenis 
1468b725ae77Skettenis   /* NOTE: carlton/2002-12-05: There's also a possible performance
1469b725ae77Skettenis      issue here: if you usually search for global symbols in the
1470b725ae77Skettenis      current file, then it would be slightly better to search the
1471b725ae77Skettenis      current global block before searching all the symtabs.  But there
1472b725ae77Skettenis      are other factors that have a much greater effect on performance
1473b725ae77Skettenis      than that one, so I don't think we should worry about that for
1474b725ae77Skettenis      now.  */
1475b725ae77Skettenis 
1476b725ae77Skettenis   sym = lookup_symbol_static (name, linkage_name, block, domain, symtab);
1477b725ae77Skettenis   if (sym != NULL)
1478e93f7393Sniklas     return sym;
1479b725ae77Skettenis 
1480b725ae77Skettenis   return lookup_symbol_global (name, linkage_name, domain, symtab);
1481e93f7393Sniklas }
1482e93f7393Sniklas 
1483b725ae77Skettenis /* Lookup a symbol in the static block associated to BLOCK, if there
1484b725ae77Skettenis    is one; do nothing if BLOCK is NULL or a global block.  */
1485b725ae77Skettenis 
1486b725ae77Skettenis struct symbol *
lookup_symbol_static(const char * name,const char * linkage_name,const struct block * block,const domain_enum domain,struct symtab ** symtab)1487b725ae77Skettenis lookup_symbol_static (const char *name,
1488b725ae77Skettenis 		      const char *linkage_name,
1489b725ae77Skettenis 		      const struct block *block,
1490b725ae77Skettenis 		      const domain_enum domain,
1491b725ae77Skettenis 		      struct symtab **symtab)
1492e93f7393Sniklas {
1493b725ae77Skettenis   const struct block *static_block = block_static_block (block);
1494b725ae77Skettenis 
1495b725ae77Skettenis   if (static_block != NULL)
1496b725ae77Skettenis     return lookup_symbol_aux_block (name, linkage_name, static_block,
1497b725ae77Skettenis 				    domain, symtab);
1498b725ae77Skettenis   else
1499b725ae77Skettenis     return NULL;
1500b725ae77Skettenis }
1501b725ae77Skettenis 
1502b725ae77Skettenis /* Lookup a symbol in all files' global blocks (searching psymtabs if
1503b725ae77Skettenis    necessary).  */
1504b725ae77Skettenis 
1505b725ae77Skettenis struct symbol *
lookup_symbol_global(const char * name,const char * linkage_name,const domain_enum domain,struct symtab ** symtab)1506b725ae77Skettenis lookup_symbol_global (const char *name,
1507b725ae77Skettenis 		      const char *linkage_name,
1508b725ae77Skettenis 		      const domain_enum domain,
1509b725ae77Skettenis 		      struct symtab **symtab)
1510e93f7393Sniklas {
1511b725ae77Skettenis   struct symbol *sym;
1512b725ae77Skettenis 
1513b725ae77Skettenis   sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, linkage_name,
1514b725ae77Skettenis 				   domain, symtab);
1515b725ae77Skettenis   if (sym != NULL)
1516e93f7393Sniklas     return sym;
1517b725ae77Skettenis 
1518b725ae77Skettenis   return lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, linkage_name,
1519b725ae77Skettenis 				     domain, symtab);
1520e93f7393Sniklas }
1521e93f7393Sniklas 
1522b725ae77Skettenis /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
1523b725ae77Skettenis    If LINKAGE_NAME is non-NULL, check in addition that the symbol's
1524b725ae77Skettenis    linkage name matches it.  Check the global symbols if GLOBAL, the
1525b725ae77Skettenis    static symbols if not */
1526e93f7393Sniklas 
1527b725ae77Skettenis struct partial_symbol *
lookup_partial_symbol(struct partial_symtab * pst,const char * name,const char * linkage_name,int global,domain_enum domain)1528b725ae77Skettenis lookup_partial_symbol (struct partial_symtab *pst, const char *name,
1529b725ae77Skettenis 		       const char *linkage_name, int global,
1530b725ae77Skettenis 		       domain_enum domain)
1531e93f7393Sniklas {
1532b725ae77Skettenis   struct partial_symbol *temp;
1533e93f7393Sniklas   struct partial_symbol **start, **psym;
1534b725ae77Skettenis   struct partial_symbol **top, **real_top, **bottom, **center;
1535e93f7393Sniklas   int length = (global ? pst->n_global_syms : pst->n_static_syms);
1536e93f7393Sniklas   int do_linear_search = 1;
1537e93f7393Sniklas 
1538e93f7393Sniklas   if (length == 0)
1539e93f7393Sniklas     {
1540e93f7393Sniklas       return (NULL);
1541e93f7393Sniklas     }
1542e93f7393Sniklas   start = (global ?
1543e93f7393Sniklas 	   pst->objfile->global_psymbols.list + pst->globals_offset :
1544e93f7393Sniklas 	   pst->objfile->static_psymbols.list + pst->statics_offset);
1545e93f7393Sniklas 
1546e93f7393Sniklas   if (global)			/* This means we can use a binary search. */
1547e93f7393Sniklas     {
1548e93f7393Sniklas       do_linear_search = 0;
1549e93f7393Sniklas 
1550e93f7393Sniklas       /* Binary search.  This search is guaranteed to end with center
1551b725ae77Skettenis          pointing at the earliest partial symbol whose name might be
1552b725ae77Skettenis          correct.  At that point *all* partial symbols with an
1553b725ae77Skettenis          appropriate name will be checked against the correct
1554b725ae77Skettenis          domain.  */
1555e93f7393Sniklas 
1556e93f7393Sniklas       bottom = start;
1557e93f7393Sniklas       top = start + length - 1;
1558b725ae77Skettenis       real_top = top;
1559e93f7393Sniklas       while (top > bottom)
1560e93f7393Sniklas 	{
1561e93f7393Sniklas 	  center = bottom + (top - bottom) / 2;
1562e93f7393Sniklas 	  if (!(center < top))
1563b725ae77Skettenis 	    internal_error (__FILE__, __LINE__, "failed internal consistency check");
1564b725ae77Skettenis 	  if (!do_linear_search
1565b725ae77Skettenis 	      && (SYMBOL_LANGUAGE (*center) == language_java))
1566e93f7393Sniklas 	    {
1567e93f7393Sniklas 	      do_linear_search = 1;
1568e93f7393Sniklas 	    }
1569*63addd46Skettenis 	  if (strcmp_iw_ordered (SYMBOL_SEARCH_NAME (*center), name) >= 0)
1570e93f7393Sniklas 	    {
1571e93f7393Sniklas 	      top = center;
1572e93f7393Sniklas 	    }
1573e93f7393Sniklas 	  else
1574e93f7393Sniklas 	    {
1575e93f7393Sniklas 	      bottom = center + 1;
1576e93f7393Sniklas 	    }
1577e93f7393Sniklas 	}
1578e93f7393Sniklas       if (!(top == bottom))
1579b725ae77Skettenis 	internal_error (__FILE__, __LINE__, "failed internal consistency check");
1580b725ae77Skettenis 
1581b725ae77Skettenis       while (top <= real_top
1582b725ae77Skettenis 	     && (linkage_name != NULL
1583b725ae77Skettenis 		 ? strcmp (SYMBOL_LINKAGE_NAME (*top), linkage_name) == 0
1584*63addd46Skettenis 		 : SYMBOL_MATCHES_SEARCH_NAME (*top,name)))
1585e93f7393Sniklas 	{
1586b725ae77Skettenis 	  if (SYMBOL_DOMAIN (*top) == domain)
1587e93f7393Sniklas 	    {
1588e93f7393Sniklas 		  return (*top);
1589e93f7393Sniklas 	    }
1590e93f7393Sniklas 	  top++;
1591e93f7393Sniklas 	}
1592e93f7393Sniklas     }
1593e93f7393Sniklas 
1594e93f7393Sniklas   /* Can't use a binary search or else we found during the binary search that
1595e93f7393Sniklas      we should also do a linear search. */
1596e93f7393Sniklas 
1597e93f7393Sniklas   if (do_linear_search)
1598e93f7393Sniklas     {
1599e93f7393Sniklas       for (psym = start; psym < start + length; psym++)
1600e93f7393Sniklas 	{
1601b725ae77Skettenis 	  if (domain == SYMBOL_DOMAIN (*psym))
1602e93f7393Sniklas 	    {
1603b725ae77Skettenis 	      if (linkage_name != NULL
1604b725ae77Skettenis 		  ? strcmp (SYMBOL_LINKAGE_NAME (*psym), linkage_name) == 0
1605*63addd46Skettenis 		  : SYMBOL_MATCHES_SEARCH_NAME (*psym, name))
1606e93f7393Sniklas 		{
1607e93f7393Sniklas 		  return (*psym);
1608e93f7393Sniklas 		}
1609e93f7393Sniklas 	    }
1610e93f7393Sniklas 	}
1611e93f7393Sniklas     }
1612e93f7393Sniklas 
1613e93f7393Sniklas   return (NULL);
1614e93f7393Sniklas }
1615e93f7393Sniklas 
1616b725ae77Skettenis /* Look up a type named NAME in the struct_domain.  The type returned
1617b725ae77Skettenis    must not be opaque -- i.e., must have at least one field
1618b725ae77Skettenis    defined.  */
1619b725ae77Skettenis 
1620b725ae77Skettenis struct type *
lookup_transparent_type(const char * name)1621b725ae77Skettenis lookup_transparent_type (const char *name)
1622b725ae77Skettenis {
1623b725ae77Skettenis   return current_language->la_lookup_transparent_type (name);
1624b725ae77Skettenis }
1625b725ae77Skettenis 
1626b725ae77Skettenis /* The standard implementation of lookup_transparent_type.  This code
1627b725ae77Skettenis    was modeled on lookup_symbol -- the parts not relevant to looking
1628b725ae77Skettenis    up types were just left out.  In particular it's assumed here that
1629b725ae77Skettenis    types are available in struct_domain and only at file-static or
1630b725ae77Skettenis    global blocks.  */
1631b725ae77Skettenis 
1632b725ae77Skettenis struct type *
basic_lookup_transparent_type(const char * name)1633b725ae77Skettenis basic_lookup_transparent_type (const char *name)
1634b725ae77Skettenis {
1635b725ae77Skettenis   struct symbol *sym;
1636b725ae77Skettenis   struct symtab *s = NULL;
1637b725ae77Skettenis   struct partial_symtab *ps;
1638b725ae77Skettenis   struct blockvector *bv;
1639b725ae77Skettenis   struct objfile *objfile;
1640b725ae77Skettenis   struct block *block;
1641b725ae77Skettenis 
1642b725ae77Skettenis   /* Now search all the global symbols.  Do the symtab's first, then
1643b725ae77Skettenis      check the psymtab's. If a psymtab indicates the existence
1644b725ae77Skettenis      of the desired name as a global, then do psymtab-to-symtab
1645b725ae77Skettenis      conversion on the fly and return the found symbol.  */
1646b725ae77Skettenis 
1647b725ae77Skettenis   ALL_SYMTABS (objfile, s)
1648b725ae77Skettenis   {
1649b725ae77Skettenis     bv = BLOCKVECTOR (s);
1650b725ae77Skettenis     block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1651b725ae77Skettenis     sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1652b725ae77Skettenis     if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1653b725ae77Skettenis       {
1654b725ae77Skettenis 	return SYMBOL_TYPE (sym);
1655b725ae77Skettenis       }
1656b725ae77Skettenis   }
1657b725ae77Skettenis 
1658b725ae77Skettenis   ALL_PSYMTABS (objfile, ps)
1659b725ae77Skettenis   {
1660b725ae77Skettenis     if (!ps->readin && lookup_partial_symbol (ps, name, NULL,
1661b725ae77Skettenis 					      1, STRUCT_DOMAIN))
1662b725ae77Skettenis       {
1663b725ae77Skettenis 	s = PSYMTAB_TO_SYMTAB (ps);
1664b725ae77Skettenis 	bv = BLOCKVECTOR (s);
1665b725ae77Skettenis 	block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1666b725ae77Skettenis 	sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1667b725ae77Skettenis 	if (!sym)
1668b725ae77Skettenis 	  {
1669b725ae77Skettenis 	    /* This shouldn't be necessary, but as a last resort
1670b725ae77Skettenis 	     * try looking in the statics even though the psymtab
1671b725ae77Skettenis 	     * claimed the symbol was global. It's possible that
1672b725ae77Skettenis 	     * the psymtab gets it wrong in some cases.
1673b725ae77Skettenis 	     */
1674b725ae77Skettenis 	    block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1675b725ae77Skettenis 	    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1676b725ae77Skettenis 	    if (!sym)
1677b725ae77Skettenis 	      error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1678b725ae77Skettenis %s may be an inlined function, or may be a template function\n\
1679b725ae77Skettenis (if a template, try specifying an instantiation: %s<type>).",
1680b725ae77Skettenis 		     name, ps->filename, name, name);
1681b725ae77Skettenis 	  }
1682b725ae77Skettenis 	if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1683b725ae77Skettenis 	  return SYMBOL_TYPE (sym);
1684b725ae77Skettenis       }
1685b725ae77Skettenis   }
1686b725ae77Skettenis 
1687b725ae77Skettenis   /* Now search the static file-level symbols.
1688b725ae77Skettenis      Not strictly correct, but more useful than an error.
1689b725ae77Skettenis      Do the symtab's first, then
1690b725ae77Skettenis      check the psymtab's. If a psymtab indicates the existence
1691b725ae77Skettenis      of the desired name as a file-level static, then do psymtab-to-symtab
1692b725ae77Skettenis      conversion on the fly and return the found symbol.
1693b725ae77Skettenis    */
1694b725ae77Skettenis 
1695b725ae77Skettenis   ALL_SYMTABS (objfile, s)
1696b725ae77Skettenis   {
1697b725ae77Skettenis     bv = BLOCKVECTOR (s);
1698b725ae77Skettenis     block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1699b725ae77Skettenis     sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1700b725ae77Skettenis     if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1701b725ae77Skettenis       {
1702b725ae77Skettenis 	return SYMBOL_TYPE (sym);
1703b725ae77Skettenis       }
1704b725ae77Skettenis   }
1705b725ae77Skettenis 
1706b725ae77Skettenis   ALL_PSYMTABS (objfile, ps)
1707b725ae77Skettenis   {
1708b725ae77Skettenis     if (!ps->readin && lookup_partial_symbol (ps, name, NULL, 0, STRUCT_DOMAIN))
1709b725ae77Skettenis       {
1710b725ae77Skettenis 	s = PSYMTAB_TO_SYMTAB (ps);
1711b725ae77Skettenis 	bv = BLOCKVECTOR (s);
1712b725ae77Skettenis 	block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1713b725ae77Skettenis 	sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1714b725ae77Skettenis 	if (!sym)
1715b725ae77Skettenis 	  {
1716b725ae77Skettenis 	    /* This shouldn't be necessary, but as a last resort
1717b725ae77Skettenis 	     * try looking in the globals even though the psymtab
1718b725ae77Skettenis 	     * claimed the symbol was static. It's possible that
1719b725ae77Skettenis 	     * the psymtab gets it wrong in some cases.
1720b725ae77Skettenis 	     */
1721b725ae77Skettenis 	    block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1722b725ae77Skettenis 	    sym = lookup_block_symbol (block, name, NULL, STRUCT_DOMAIN);
1723b725ae77Skettenis 	    if (!sym)
1724b725ae77Skettenis 	      error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1725b725ae77Skettenis %s may be an inlined function, or may be a template function\n\
1726b725ae77Skettenis (if a template, try specifying an instantiation: %s<type>).",
1727b725ae77Skettenis 		     name, ps->filename, name, name);
1728b725ae77Skettenis 	  }
1729b725ae77Skettenis 	if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1730b725ae77Skettenis 	  return SYMBOL_TYPE (sym);
1731b725ae77Skettenis       }
1732b725ae77Skettenis   }
1733b725ae77Skettenis   return (struct type *) 0;
1734b725ae77Skettenis }
1735b725ae77Skettenis 
1736b725ae77Skettenis 
1737e93f7393Sniklas /* Find the psymtab containing main(). */
1738e93f7393Sniklas /* FIXME:  What about languages without main() or specially linked
1739e93f7393Sniklas    executables that have no main() ? */
1740e93f7393Sniklas 
1741e93f7393Sniklas struct partial_symtab *
find_main_psymtab(void)1742b725ae77Skettenis find_main_psymtab (void)
1743e93f7393Sniklas {
1744b725ae77Skettenis   struct partial_symtab *pst;
1745b725ae77Skettenis   struct objfile *objfile;
1746e93f7393Sniklas 
1747e93f7393Sniklas   ALL_PSYMTABS (objfile, pst)
1748e93f7393Sniklas   {
1749b725ae77Skettenis     if (lookup_partial_symbol (pst, main_name (), NULL, 1, VAR_DOMAIN))
1750e93f7393Sniklas       {
1751e93f7393Sniklas 	return (pst);
1752e93f7393Sniklas       }
1753e93f7393Sniklas   }
1754e93f7393Sniklas   return (NULL);
1755e93f7393Sniklas }
1756e93f7393Sniklas 
1757b725ae77Skettenis /* Search BLOCK for symbol NAME in DOMAIN.
1758e93f7393Sniklas 
1759e93f7393Sniklas    Note that if NAME is the demangled form of a C++ symbol, we will fail
1760e93f7393Sniklas    to find a match during the binary search of the non-encoded names, but
1761e93f7393Sniklas    for now we don't worry about the slight inefficiency of looking for
1762e93f7393Sniklas    a match we'll never find, since it will go pretty quick.  Once the
1763e93f7393Sniklas    binary search terminates, we drop through and do a straight linear
1764b725ae77Skettenis    search on the symbols.  Each symbol which is marked as being a ObjC/C++
1765b725ae77Skettenis    symbol (language_cplus or language_objc set) has both the encoded and
1766b725ae77Skettenis    non-encoded names tested for a match.
1767b725ae77Skettenis 
1768b725ae77Skettenis    If LINKAGE_NAME is non-NULL, verify that any symbol we find has this
1769b725ae77Skettenis    particular mangled name.
1770b725ae77Skettenis */
1771e93f7393Sniklas 
1772e93f7393Sniklas struct symbol *
lookup_block_symbol(const struct block * block,const char * name,const char * linkage_name,const domain_enum domain)1773b725ae77Skettenis lookup_block_symbol (const struct block *block, const char *name,
1774b725ae77Skettenis 		     const char *linkage_name,
1775b725ae77Skettenis 		     const domain_enum domain)
1776e93f7393Sniklas {
1777b725ae77Skettenis   struct dict_iterator iter;
1778b725ae77Skettenis   struct symbol *sym;
1779e93f7393Sniklas 
1780b725ae77Skettenis   if (!BLOCK_FUNCTION (block))
1781e93f7393Sniklas     {
1782b725ae77Skettenis       for (sym = dict_iter_name_first (BLOCK_DICT (block), name, &iter);
1783b725ae77Skettenis 	   sym != NULL;
1784b725ae77Skettenis 	   sym = dict_iter_name_next (name, &iter))
1785e93f7393Sniklas 	{
1786b725ae77Skettenis 	  if (SYMBOL_DOMAIN (sym) == domain
1787b725ae77Skettenis 	      && (linkage_name != NULL
1788b725ae77Skettenis 		  ? strcmp (SYMBOL_LINKAGE_NAME (sym), linkage_name) == 0 : 1))
1789b725ae77Skettenis 	    return sym;
1790e93f7393Sniklas 	}
1791b725ae77Skettenis       return NULL;
1792e93f7393Sniklas     }
1793e93f7393Sniklas   else
1794e93f7393Sniklas     {
1795b725ae77Skettenis       /* Note that parameter symbols do not always show up last in the
1796e93f7393Sniklas 	 list; this loop makes sure to take anything else other than
1797e93f7393Sniklas 	 parameter symbols first; it only uses parameter symbols as a
1798e93f7393Sniklas 	 last resort.  Note that this only takes up extra computation
1799e93f7393Sniklas 	 time on a match.  */
1800e93f7393Sniklas 
1801b725ae77Skettenis       struct symbol *sym_found = NULL;
1802b725ae77Skettenis 
1803b725ae77Skettenis       for (sym = dict_iter_name_first (BLOCK_DICT (block), name, &iter);
1804b725ae77Skettenis 	   sym != NULL;
1805b725ae77Skettenis 	   sym = dict_iter_name_next (name, &iter))
1806e93f7393Sniklas 	{
1807b725ae77Skettenis 	  if (SYMBOL_DOMAIN (sym) == domain
1808b725ae77Skettenis 	      && (linkage_name != NULL
1809b725ae77Skettenis 		  ? strcmp (SYMBOL_LINKAGE_NAME (sym), linkage_name) == 0 : 1))
1810e93f7393Sniklas 	    {
1811e93f7393Sniklas 	      sym_found = sym;
1812e93f7393Sniklas 	      if (SYMBOL_CLASS (sym) != LOC_ARG &&
1813e93f7393Sniklas 		  SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1814e93f7393Sniklas 		  SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1815e93f7393Sniklas 		  SYMBOL_CLASS (sym) != LOC_REGPARM &&
1816e93f7393Sniklas 		  SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1817b725ae77Skettenis 		  SYMBOL_CLASS (sym) != LOC_BASEREG_ARG &&
1818b725ae77Skettenis 		  SYMBOL_CLASS (sym) != LOC_COMPUTED_ARG)
1819e93f7393Sniklas 		{
1820e93f7393Sniklas 		  break;
1821e93f7393Sniklas 		}
1822e93f7393Sniklas 	    }
1823e93f7393Sniklas 	}
1824e93f7393Sniklas       return (sym_found);	/* Will be NULL if not found. */
1825e93f7393Sniklas     }
1826e93f7393Sniklas }
1827e93f7393Sniklas 
1828b725ae77Skettenis /* Find the symtab associated with PC and SECTION.  Look through the
1829b725ae77Skettenis    psymtabs and read in another symtab if necessary. */
1830e93f7393Sniklas 
1831e93f7393Sniklas struct symtab *
find_pc_sect_symtab(CORE_ADDR pc,asection * section)1832b725ae77Skettenis find_pc_sect_symtab (CORE_ADDR pc, asection *section)
1833e93f7393Sniklas {
1834b725ae77Skettenis   struct block *b;
1835e93f7393Sniklas   struct blockvector *bv;
1836b725ae77Skettenis   struct symtab *s = NULL;
1837b725ae77Skettenis   struct symtab *best_s = NULL;
1838b725ae77Skettenis   struct partial_symtab *ps;
1839b725ae77Skettenis   struct objfile *objfile;
1840b725ae77Skettenis   CORE_ADDR distance = 0;
1841b725ae77Skettenis   struct minimal_symbol *msymbol;
1842b725ae77Skettenis 
1843b725ae77Skettenis   /* If we know that this is not a text address, return failure.  This is
1844b725ae77Skettenis      necessary because we loop based on the block's high and low code
1845b725ae77Skettenis      addresses, which do not include the data ranges, and because
1846b725ae77Skettenis      we call find_pc_sect_psymtab which has a similar restriction based
1847b725ae77Skettenis      on the partial_symtab's texthigh and textlow.  */
1848b725ae77Skettenis   msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1849b725ae77Skettenis   if (msymbol
1850b725ae77Skettenis       && (msymbol->type == mst_data
1851b725ae77Skettenis 	  || msymbol->type == mst_bss
1852b725ae77Skettenis 	  || msymbol->type == mst_abs
1853b725ae77Skettenis 	  || msymbol->type == mst_file_data
1854b725ae77Skettenis 	  || msymbol->type == mst_file_bss))
1855b725ae77Skettenis     return NULL;
1856e93f7393Sniklas 
1857e93f7393Sniklas   /* Search all symtabs for the one whose file contains our address, and which
1858e93f7393Sniklas      is the smallest of all the ones containing the address.  This is designed
1859e93f7393Sniklas      to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1860e93f7393Sniklas      and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
1861e93f7393Sniklas      0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1862e93f7393Sniklas 
1863e93f7393Sniklas      This happens for native ecoff format, where code from included files
1864e93f7393Sniklas      gets its own symtab. The symtab for the included file should have
1865e93f7393Sniklas      been read in already via the dependency mechanism.
1866e93f7393Sniklas      It might be swifter to create several symtabs with the same name
1867e93f7393Sniklas      like xcoff does (I'm not sure).
1868e93f7393Sniklas 
1869e93f7393Sniklas      It also happens for objfiles that have their functions reordered.
1870e93f7393Sniklas      For these, the symtab we are looking for is not necessarily read in.  */
1871e93f7393Sniklas 
1872e93f7393Sniklas   ALL_SYMTABS (objfile, s)
1873e93f7393Sniklas   {
1874e93f7393Sniklas     bv = BLOCKVECTOR (s);
1875e93f7393Sniklas     b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1876b725ae77Skettenis 
1877e93f7393Sniklas     if (BLOCK_START (b) <= pc
1878e93f7393Sniklas 	&& BLOCK_END (b) > pc
1879e93f7393Sniklas 	&& (distance == 0
1880e93f7393Sniklas 	    || BLOCK_END (b) - BLOCK_START (b) < distance))
1881e93f7393Sniklas       {
1882e93f7393Sniklas 	/* For an objfile that has its functions reordered,
1883e93f7393Sniklas 	   find_pc_psymtab will find the proper partial symbol table
1884e93f7393Sniklas 	   and we simply return its corresponding symtab.  */
1885b725ae77Skettenis 	/* In order to better support objfiles that contain both
1886b725ae77Skettenis 	   stabs and coff debugging info, we continue on if a psymtab
1887b725ae77Skettenis 	   can't be found. */
1888e93f7393Sniklas 	if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1889e93f7393Sniklas 	  {
1890b725ae77Skettenis 	    ps = find_pc_sect_psymtab (pc, section);
1891e93f7393Sniklas 	    if (ps)
1892b725ae77Skettenis 	      return PSYMTAB_TO_SYMTAB (ps);
1893b725ae77Skettenis 	  }
1894b725ae77Skettenis 	if (section != 0)
1895b725ae77Skettenis 	  {
1896b725ae77Skettenis 	    struct dict_iterator iter;
1897b725ae77Skettenis 	    struct symbol *sym = NULL;
1898b725ae77Skettenis 
1899b725ae77Skettenis 	    ALL_BLOCK_SYMBOLS (b, iter, sym)
1900b725ae77Skettenis 	      {
1901b725ae77Skettenis 		fixup_symbol_section (sym, objfile);
1902b725ae77Skettenis 		if (section == SYMBOL_BFD_SECTION (sym))
1903b725ae77Skettenis 		  break;
1904b725ae77Skettenis 	      }
1905b725ae77Skettenis 	    if (sym == NULL)
1906b725ae77Skettenis 	      continue;		/* no symbol in this symtab matches section */
1907e93f7393Sniklas 	  }
1908e93f7393Sniklas 	distance = BLOCK_END (b) - BLOCK_START (b);
1909e93f7393Sniklas 	best_s = s;
1910e93f7393Sniklas       }
1911e93f7393Sniklas   }
1912e93f7393Sniklas 
1913e93f7393Sniklas   if (best_s != NULL)
1914e93f7393Sniklas     return (best_s);
1915e93f7393Sniklas 
1916e93f7393Sniklas   s = NULL;
1917b725ae77Skettenis   ps = find_pc_sect_psymtab (pc, section);
1918e93f7393Sniklas   if (ps)
1919e93f7393Sniklas     {
1920e93f7393Sniklas       if (ps->readin)
1921e93f7393Sniklas 	/* Might want to error() here (in case symtab is corrupt and
1922e93f7393Sniklas 	   will cause a core dump), but maybe we can successfully
1923e93f7393Sniklas 	   continue, so let's not.  */
1924e93f7393Sniklas 	warning ("\
1925b725ae77Skettenis (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1926b725ae77Skettenis 		 paddr_nz (pc));
1927e93f7393Sniklas       s = PSYMTAB_TO_SYMTAB (ps);
1928e93f7393Sniklas     }
1929e93f7393Sniklas   return (s);
1930e93f7393Sniklas }
1931b725ae77Skettenis 
1932b725ae77Skettenis /* Find the symtab associated with PC.  Look through the psymtabs and
1933b725ae77Skettenis    read in another symtab if necessary.  Backward compatibility, no section */
1934b725ae77Skettenis 
1935b725ae77Skettenis struct symtab *
find_pc_symtab(CORE_ADDR pc)1936b725ae77Skettenis find_pc_symtab (CORE_ADDR pc)
1937b725ae77Skettenis {
1938b725ae77Skettenis   return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1939b725ae77Skettenis }
1940e93f7393Sniklas 
1941e93f7393Sniklas 
1942b725ae77Skettenis /* Find the source file and line number for a given PC value and SECTION.
1943e93f7393Sniklas    Return a structure containing a symtab pointer, a line number,
1944e93f7393Sniklas    and a pc range for the entire source line.
1945e93f7393Sniklas    The value's .pc field is NOT the specified pc.
1946e93f7393Sniklas    NOTCURRENT nonzero means, if specified pc is on a line boundary,
1947e93f7393Sniklas    use the line that ends there.  Otherwise, in that case, the line
1948e93f7393Sniklas    that begins there is used.  */
1949e93f7393Sniklas 
1950e93f7393Sniklas /* The big complication here is that a line may start in one file, and end just
1951e93f7393Sniklas    before the start of another file.  This usually occurs when you #include
1952e93f7393Sniklas    code in the middle of a subroutine.  To properly find the end of a line's PC
1953e93f7393Sniklas    range, we must search all symtabs associated with this compilation unit, and
1954e93f7393Sniklas    find the one whose first PC is closer than that of the next line in this
1955e93f7393Sniklas    symtab.  */
1956e93f7393Sniklas 
1957e93f7393Sniklas /* If it's worth the effort, we could be using a binary search.  */
1958e93f7393Sniklas 
1959e93f7393Sniklas struct symtab_and_line
find_pc_sect_line(CORE_ADDR pc,struct bfd_section * section,int notcurrent)1960b725ae77Skettenis find_pc_sect_line (CORE_ADDR pc, struct bfd_section *section, int notcurrent)
1961e93f7393Sniklas {
1962e93f7393Sniklas   struct symtab *s;
1963b725ae77Skettenis   struct linetable *l;
1964b725ae77Skettenis   int len;
1965b725ae77Skettenis   int i;
1966b725ae77Skettenis   struct linetable_entry *item;
1967e93f7393Sniklas   struct symtab_and_line val;
1968e93f7393Sniklas   struct blockvector *bv;
1969b725ae77Skettenis   struct minimal_symbol *msymbol;
1970b725ae77Skettenis   struct minimal_symbol *mfunsym;
1971e93f7393Sniklas 
1972e93f7393Sniklas   /* Info on best line seen so far, and where it starts, and its file.  */
1973e93f7393Sniklas 
1974e93f7393Sniklas   struct linetable_entry *best = NULL;
1975e93f7393Sniklas   CORE_ADDR best_end = 0;
1976e93f7393Sniklas   struct symtab *best_symtab = 0;
1977e93f7393Sniklas 
1978e93f7393Sniklas   /* Store here the first line number
1979e93f7393Sniklas      of a file which contains the line at the smallest pc after PC.
1980e93f7393Sniklas      If we don't find a line whose range contains PC,
1981e93f7393Sniklas      we will use a line one less than this,
1982e93f7393Sniklas      with a range from the start of that file to the first line's pc.  */
1983e93f7393Sniklas   struct linetable_entry *alt = NULL;
1984e93f7393Sniklas   struct symtab *alt_symtab = 0;
1985e93f7393Sniklas 
1986e93f7393Sniklas   /* Info on best line seen in this file.  */
1987e93f7393Sniklas 
1988e93f7393Sniklas   struct linetable_entry *prev;
1989e93f7393Sniklas 
1990e93f7393Sniklas   /* If this pc is not from the current frame,
1991e93f7393Sniklas      it is the address of the end of a call instruction.
1992e93f7393Sniklas      Quite likely that is the start of the following statement.
1993e93f7393Sniklas      But what we want is the statement containing the instruction.
1994e93f7393Sniklas      Fudge the pc to make sure we get that.  */
1995e93f7393Sniklas 
1996b725ae77Skettenis   init_sal (&val);		/* initialize to zeroes */
1997e93f7393Sniklas 
1998b725ae77Skettenis   /* It's tempting to assume that, if we can't find debugging info for
1999b725ae77Skettenis      any function enclosing PC, that we shouldn't search for line
2000b725ae77Skettenis      number info, either.  However, GAS can emit line number info for
2001b725ae77Skettenis      assembly files --- very helpful when debugging hand-written
2002b725ae77Skettenis      assembly code.  In such a case, we'd have no debug info for the
2003b725ae77Skettenis      function, but we would have line info.  */
2004b725ae77Skettenis 
2005b725ae77Skettenis   if (notcurrent)
2006b725ae77Skettenis     pc -= 1;
2007b725ae77Skettenis 
2008b725ae77Skettenis   /* elz: added this because this function returned the wrong
2009b725ae77Skettenis      information if the pc belongs to a stub (import/export)
2010b725ae77Skettenis      to call a shlib function. This stub would be anywhere between
2011b725ae77Skettenis      two functions in the target, and the line info was erroneously
2012b725ae77Skettenis      taken to be the one of the line before the pc.
2013b725ae77Skettenis    */
2014b725ae77Skettenis   /* RT: Further explanation:
2015b725ae77Skettenis 
2016b725ae77Skettenis    * We have stubs (trampolines) inserted between procedures.
2017b725ae77Skettenis    *
2018b725ae77Skettenis    * Example: "shr1" exists in a shared library, and a "shr1" stub also
2019b725ae77Skettenis    * exists in the main image.
2020b725ae77Skettenis    *
2021b725ae77Skettenis    * In the minimal symbol table, we have a bunch of symbols
2022b725ae77Skettenis    * sorted by start address. The stubs are marked as "trampoline",
2023b725ae77Skettenis    * the others appear as text. E.g.:
2024b725ae77Skettenis    *
2025b725ae77Skettenis    *  Minimal symbol table for main image
2026b725ae77Skettenis    *     main:  code for main (text symbol)
2027b725ae77Skettenis    *     shr1: stub  (trampoline symbol)
2028b725ae77Skettenis    *     foo:   code for foo (text symbol)
2029b725ae77Skettenis    *     ...
2030b725ae77Skettenis    *  Minimal symbol table for "shr1" image:
2031b725ae77Skettenis    *     ...
2032b725ae77Skettenis    *     shr1: code for shr1 (text symbol)
2033b725ae77Skettenis    *     ...
2034b725ae77Skettenis    *
2035b725ae77Skettenis    * So the code below is trying to detect if we are in the stub
2036b725ae77Skettenis    * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
2037b725ae77Skettenis    * and if found,  do the symbolization from the real-code address
2038b725ae77Skettenis    * rather than the stub address.
2039b725ae77Skettenis    *
2040b725ae77Skettenis    * Assumptions being made about the minimal symbol table:
2041b725ae77Skettenis    *   1. lookup_minimal_symbol_by_pc() will return a trampoline only
2042b725ae77Skettenis    *      if we're really in the trampoline. If we're beyond it (say
2043b725ae77Skettenis    *      we're in "foo" in the above example), it'll have a closer
2044b725ae77Skettenis    *      symbol (the "foo" text symbol for example) and will not
2045b725ae77Skettenis    *      return the trampoline.
2046b725ae77Skettenis    *   2. lookup_minimal_symbol_text() will find a real text symbol
2047b725ae77Skettenis    *      corresponding to the trampoline, and whose address will
2048b725ae77Skettenis    *      be different than the trampoline address. I put in a sanity
2049b725ae77Skettenis    *      check for the address being the same, to avoid an
2050b725ae77Skettenis    *      infinite recursion.
2051b725ae77Skettenis    */
2052b725ae77Skettenis   msymbol = lookup_minimal_symbol_by_pc (pc);
2053b725ae77Skettenis   if (msymbol != NULL)
2054b725ae77Skettenis     if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
2055b725ae77Skettenis       {
2056b725ae77Skettenis 	mfunsym = lookup_minimal_symbol_text (SYMBOL_LINKAGE_NAME (msymbol),
2057b725ae77Skettenis 					      NULL);
2058b725ae77Skettenis 	if (mfunsym == NULL)
2059b725ae77Skettenis 	  /* I eliminated this warning since it is coming out
2060b725ae77Skettenis 	   * in the following situation:
2061b725ae77Skettenis 	   * gdb shmain // test program with shared libraries
2062b725ae77Skettenis 	   * (gdb) break shr1  // function in shared lib
2063b725ae77Skettenis 	   * Warning: In stub for ...
2064b725ae77Skettenis 	   * In the above situation, the shared lib is not loaded yet,
2065b725ae77Skettenis 	   * so of course we can't find the real func/line info,
2066b725ae77Skettenis 	   * but the "break" still works, and the warning is annoying.
2067b725ae77Skettenis 	   * So I commented out the warning. RT */
2068b725ae77Skettenis 	  /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_LINKAGE_NAME (msymbol)) */ ;
2069b725ae77Skettenis 	/* fall through */
2070b725ae77Skettenis 	else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
2071b725ae77Skettenis 	  /* Avoid infinite recursion */
2072b725ae77Skettenis 	  /* See above comment about why warning is commented out */
2073b725ae77Skettenis 	  /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_LINKAGE_NAME (msymbol)) */ ;
2074b725ae77Skettenis 	/* fall through */
2075b725ae77Skettenis 	else
2076b725ae77Skettenis 	  return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
2077b725ae77Skettenis       }
2078b725ae77Skettenis 
2079b725ae77Skettenis 
2080b725ae77Skettenis   s = find_pc_sect_symtab (pc, section);
2081e93f7393Sniklas   if (!s)
2082e93f7393Sniklas     {
2083b725ae77Skettenis       /* if no symbol information, return previous pc */
2084b725ae77Skettenis       if (notcurrent)
2085b725ae77Skettenis 	pc++;
2086e93f7393Sniklas       val.pc = pc;
2087e93f7393Sniklas       return val;
2088e93f7393Sniklas     }
2089e93f7393Sniklas 
2090e93f7393Sniklas   bv = BLOCKVECTOR (s);
2091e93f7393Sniklas 
2092e93f7393Sniklas   /* Look at all the symtabs that share this blockvector.
2093e93f7393Sniklas      They all have the same apriori range, that we found was right;
2094e93f7393Sniklas      but they have different line tables.  */
2095e93f7393Sniklas 
2096e93f7393Sniklas   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
2097e93f7393Sniklas     {
2098e93f7393Sniklas       /* Find the best line in this symtab.  */
2099e93f7393Sniklas       l = LINETABLE (s);
2100e93f7393Sniklas       if (!l)
2101e93f7393Sniklas 	continue;
2102e93f7393Sniklas       len = l->nitems;
2103e93f7393Sniklas       if (len <= 0)
2104e93f7393Sniklas 	{
2105e93f7393Sniklas 	  /* I think len can be zero if the symtab lacks line numbers
2106e93f7393Sniklas 	     (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
2107e93f7393Sniklas 	     I'm not sure which, and maybe it depends on the symbol
2108e93f7393Sniklas 	     reader).  */
2109e93f7393Sniklas 	  continue;
2110e93f7393Sniklas 	}
2111e93f7393Sniklas 
2112e93f7393Sniklas       prev = NULL;
2113e93f7393Sniklas       item = l->item;		/* Get first line info */
2114e93f7393Sniklas 
2115e93f7393Sniklas       /* Is this file's first line closer than the first lines of other files?
2116e93f7393Sniklas          If so, record this file, and its first line, as best alternate.  */
2117e93f7393Sniklas       if (item->pc > pc && (!alt || item->pc < alt->pc))
2118e93f7393Sniklas 	{
2119e93f7393Sniklas 	  alt = item;
2120e93f7393Sniklas 	  alt_symtab = s;
2121e93f7393Sniklas 	}
2122e93f7393Sniklas 
2123e93f7393Sniklas       for (i = 0; i < len; i++, item++)
2124e93f7393Sniklas 	{
2125e93f7393Sniklas 	  /* Leave prev pointing to the linetable entry for the last line
2126e93f7393Sniklas 	     that started at or before PC.  */
2127e93f7393Sniklas 	  if (item->pc > pc)
2128e93f7393Sniklas 	    break;
2129e93f7393Sniklas 
2130e93f7393Sniklas 	  prev = item;
2131e93f7393Sniklas 	}
2132e93f7393Sniklas 
2133e93f7393Sniklas       /* At this point, prev points at the line whose start addr is <= pc, and
2134e93f7393Sniklas          item points at the next line.  If we ran off the end of the linetable
2135e93f7393Sniklas          (pc >= start of the last line), then prev == item.  If pc < start of
2136e93f7393Sniklas          the first line, prev will not be set.  */
2137e93f7393Sniklas 
2138e93f7393Sniklas       /* Is this file's best line closer than the best in the other files?
2139b725ae77Skettenis          If so, record this file, and its best line, as best so far.  Don't
2140b725ae77Skettenis          save prev if it represents the end of a function (i.e. line number
2141b725ae77Skettenis          0) instead of a real line.  */
2142e93f7393Sniklas 
2143b725ae77Skettenis       if (prev && prev->line && (!best || prev->pc > best->pc))
2144e93f7393Sniklas 	{
2145e93f7393Sniklas 	  best = prev;
2146e93f7393Sniklas 	  best_symtab = s;
2147b725ae77Skettenis 
2148b725ae77Skettenis 	  /* Discard BEST_END if it's before the PC of the current BEST.  */
2149b725ae77Skettenis 	  if (best_end <= best->pc)
2150b725ae77Skettenis 	    best_end = 0;
2151e93f7393Sniklas 	}
2152b725ae77Skettenis 
2153b725ae77Skettenis       /* If another line (denoted by ITEM) is in the linetable and its
2154b725ae77Skettenis          PC is after BEST's PC, but before the current BEST_END, then
2155b725ae77Skettenis 	 use ITEM's PC as the new best_end.  */
2156b725ae77Skettenis       if (best && i < len && item->pc > best->pc
2157b725ae77Skettenis           && (best_end == 0 || best_end > item->pc))
2158b725ae77Skettenis 	best_end = item->pc;
2159e93f7393Sniklas     }
2160e93f7393Sniklas 
2161e93f7393Sniklas   if (!best_symtab)
2162e93f7393Sniklas     {
2163e93f7393Sniklas       if (!alt_symtab)
2164e93f7393Sniklas 	{			/* If we didn't find any line # info, just
2165e93f7393Sniklas 				   return zeros.  */
2166e93f7393Sniklas 	  val.pc = pc;
2167e93f7393Sniklas 	}
2168e93f7393Sniklas       else
2169e93f7393Sniklas 	{
2170e93f7393Sniklas 	  val.symtab = alt_symtab;
2171e93f7393Sniklas 	  val.line = alt->line - 1;
2172e93f7393Sniklas 
2173e93f7393Sniklas 	  /* Don't return line 0, that means that we didn't find the line.  */
2174b725ae77Skettenis 	  if (val.line == 0)
2175b725ae77Skettenis 	    ++val.line;
2176e93f7393Sniklas 
2177e93f7393Sniklas 	  val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2178e93f7393Sniklas 	  val.end = alt->pc;
2179e93f7393Sniklas 	}
2180e93f7393Sniklas     }
2181b725ae77Skettenis   else if (best->line == 0)
2182b725ae77Skettenis     {
2183b725ae77Skettenis       /* If our best fit is in a range of PC's for which no line
2184b725ae77Skettenis 	 number info is available (line number is zero) then we didn't
2185b725ae77Skettenis 	 find any valid line information. */
2186b725ae77Skettenis       val.pc = pc;
2187b725ae77Skettenis     }
2188e93f7393Sniklas   else
2189e93f7393Sniklas     {
2190e93f7393Sniklas       val.symtab = best_symtab;
2191e93f7393Sniklas       val.line = best->line;
2192e93f7393Sniklas       val.pc = best->pc;
2193e93f7393Sniklas       if (best_end && (!alt || best_end < alt->pc))
2194e93f7393Sniklas 	val.end = best_end;
2195e93f7393Sniklas       else if (alt)
2196e93f7393Sniklas 	val.end = alt->pc;
2197e93f7393Sniklas       else
2198e93f7393Sniklas 	val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2199e93f7393Sniklas     }
2200b725ae77Skettenis   val.section = section;
2201e93f7393Sniklas   return val;
2202e93f7393Sniklas }
2203e93f7393Sniklas 
2204b725ae77Skettenis /* Backward compatibility (no section) */
2205b725ae77Skettenis 
2206b725ae77Skettenis struct symtab_and_line
find_pc_line(CORE_ADDR pc,int notcurrent)2207b725ae77Skettenis find_pc_line (CORE_ADDR pc, int notcurrent)
2208b725ae77Skettenis {
2209b725ae77Skettenis   asection *section;
2210b725ae77Skettenis 
2211b725ae77Skettenis   section = find_pc_overlay (pc);
2212b725ae77Skettenis   if (pc_in_unmapped_range (pc, section))
2213b725ae77Skettenis     pc = overlay_mapped_address (pc, section);
2214b725ae77Skettenis   return find_pc_sect_line (pc, section, notcurrent);
2215b725ae77Skettenis }
2216b725ae77Skettenis 
2217e93f7393Sniklas /* Find line number LINE in any symtab whose name is the same as
2218e93f7393Sniklas    SYMTAB.
2219e93f7393Sniklas 
2220b725ae77Skettenis    If found, return the symtab that contains the linetable in which it was
2221e93f7393Sniklas    found, set *INDEX to the index in the linetable of the best entry
2222e93f7393Sniklas    found, and set *EXACT_MATCH nonzero if the value returned is an
2223e93f7393Sniklas    exact match.
2224e93f7393Sniklas 
2225b725ae77Skettenis    If not found, return NULL.  */
2226e93f7393Sniklas 
2227b725ae77Skettenis struct symtab *
find_line_symtab(struct symtab * symtab,int line,int * index,int * exact_match)2228b725ae77Skettenis find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
2229e93f7393Sniklas {
2230e93f7393Sniklas   int exact;
2231e93f7393Sniklas 
2232e93f7393Sniklas   /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2233e93f7393Sniklas      so far seen.  */
2234e93f7393Sniklas 
2235e93f7393Sniklas   int best_index;
2236e93f7393Sniklas   struct linetable *best_linetable;
2237b725ae77Skettenis   struct symtab *best_symtab;
2238e93f7393Sniklas 
2239e93f7393Sniklas   /* First try looking it up in the given symtab.  */
2240e93f7393Sniklas   best_linetable = LINETABLE (symtab);
2241b725ae77Skettenis   best_symtab = symtab;
2242e93f7393Sniklas   best_index = find_line_common (best_linetable, line, &exact);
2243e93f7393Sniklas   if (best_index < 0 || !exact)
2244e93f7393Sniklas     {
2245e93f7393Sniklas       /* Didn't find an exact match.  So we better keep looking for
2246e93f7393Sniklas          another symtab with the same name.  In the case of xcoff,
2247e93f7393Sniklas          multiple csects for one source file (produced by IBM's FORTRAN
2248e93f7393Sniklas          compiler) produce multiple symtabs (this is unavoidable
2249e93f7393Sniklas          assuming csects can be at arbitrary places in memory and that
2250e93f7393Sniklas          the GLOBAL_BLOCK of a symtab has a begin and end address).  */
2251e93f7393Sniklas 
2252e93f7393Sniklas       /* BEST is the smallest linenumber > LINE so far seen,
2253e93f7393Sniklas          or 0 if none has been seen so far.
2254e93f7393Sniklas          BEST_INDEX and BEST_LINETABLE identify the item for it.  */
2255e93f7393Sniklas       int best;
2256e93f7393Sniklas 
2257e93f7393Sniklas       struct objfile *objfile;
2258e93f7393Sniklas       struct symtab *s;
2259e93f7393Sniklas 
2260e93f7393Sniklas       if (best_index >= 0)
2261e93f7393Sniklas 	best = best_linetable->item[best_index].line;
2262e93f7393Sniklas       else
2263e93f7393Sniklas 	best = 0;
2264e93f7393Sniklas 
2265e93f7393Sniklas       ALL_SYMTABS (objfile, s)
2266e93f7393Sniklas       {
2267e93f7393Sniklas 	struct linetable *l;
2268e93f7393Sniklas 	int ind;
2269e93f7393Sniklas 
2270b725ae77Skettenis 	if (strcmp (symtab->filename, s->filename) != 0)
2271e93f7393Sniklas 	  continue;
2272e93f7393Sniklas 	l = LINETABLE (s);
2273e93f7393Sniklas 	ind = find_line_common (l, line, &exact);
2274e93f7393Sniklas 	if (ind >= 0)
2275e93f7393Sniklas 	  {
2276e93f7393Sniklas 	    if (exact)
2277e93f7393Sniklas 	      {
2278e93f7393Sniklas 		best_index = ind;
2279e93f7393Sniklas 		best_linetable = l;
2280b725ae77Skettenis 		best_symtab = s;
2281e93f7393Sniklas 		goto done;
2282e93f7393Sniklas 	      }
2283e93f7393Sniklas 	    if (best == 0 || l->item[ind].line < best)
2284e93f7393Sniklas 	      {
2285e93f7393Sniklas 		best = l->item[ind].line;
2286e93f7393Sniklas 		best_index = ind;
2287e93f7393Sniklas 		best_linetable = l;
2288b725ae77Skettenis 		best_symtab = s;
2289e93f7393Sniklas 	      }
2290e93f7393Sniklas 	  }
2291e93f7393Sniklas       }
2292e93f7393Sniklas     }
2293e93f7393Sniklas done:
2294e93f7393Sniklas   if (best_index < 0)
2295b725ae77Skettenis     return NULL;
2296e93f7393Sniklas 
2297e93f7393Sniklas   if (index)
2298e93f7393Sniklas     *index = best_index;
2299e93f7393Sniklas   if (exact_match)
2300e93f7393Sniklas     *exact_match = exact;
2301b725ae77Skettenis 
2302b725ae77Skettenis   return best_symtab;
2303e93f7393Sniklas }
2304e93f7393Sniklas 
2305b725ae77Skettenis /* Set the PC value for a given source file and line number and return true.
2306b725ae77Skettenis    Returns zero for invalid line number (and sets the PC to 0).
2307e93f7393Sniklas    The source file is specified with a struct symtab.  */
2308e93f7393Sniklas 
2309b725ae77Skettenis int
find_line_pc(struct symtab * symtab,int line,CORE_ADDR * pc)2310b725ae77Skettenis find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
2311e93f7393Sniklas {
2312e93f7393Sniklas   struct linetable *l;
2313e93f7393Sniklas   int ind;
2314e93f7393Sniklas 
2315b725ae77Skettenis   *pc = 0;
2316e93f7393Sniklas   if (symtab == 0)
2317e93f7393Sniklas     return 0;
2318b725ae77Skettenis 
2319b725ae77Skettenis   symtab = find_line_symtab (symtab, line, &ind, NULL);
2320b725ae77Skettenis   if (symtab != NULL)
2321b725ae77Skettenis     {
2322b725ae77Skettenis       l = LINETABLE (symtab);
2323b725ae77Skettenis       *pc = l->item[ind].pc;
2324b725ae77Skettenis       return 1;
2325b725ae77Skettenis     }
2326e93f7393Sniklas   else
2327e93f7393Sniklas     return 0;
2328e93f7393Sniklas }
2329e93f7393Sniklas 
2330e93f7393Sniklas /* Find the range of pc values in a line.
2331e93f7393Sniklas    Store the starting pc of the line into *STARTPTR
2332e93f7393Sniklas    and the ending pc (start of next line) into *ENDPTR.
2333e93f7393Sniklas    Returns 1 to indicate success.
2334e93f7393Sniklas    Returns 0 if could not find the specified line.  */
2335e93f7393Sniklas 
2336e93f7393Sniklas int
find_line_pc_range(struct symtab_and_line sal,CORE_ADDR * startptr,CORE_ADDR * endptr)2337b725ae77Skettenis find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
2338b725ae77Skettenis 		    CORE_ADDR *endptr)
2339e93f7393Sniklas {
2340e93f7393Sniklas   CORE_ADDR startaddr;
2341e93f7393Sniklas   struct symtab_and_line found_sal;
2342e93f7393Sniklas 
2343e93f7393Sniklas   startaddr = sal.pc;
2344b725ae77Skettenis   if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
2345e93f7393Sniklas     return 0;
2346e93f7393Sniklas 
2347e93f7393Sniklas   /* This whole function is based on address.  For example, if line 10 has
2348e93f7393Sniklas      two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2349e93f7393Sniklas      "info line *0x123" should say the line goes from 0x100 to 0x200
2350e93f7393Sniklas      and "info line *0x355" should say the line goes from 0x300 to 0x400.
2351e93f7393Sniklas      This also insures that we never give a range like "starts at 0x134
2352e93f7393Sniklas      and ends at 0x12c".  */
2353e93f7393Sniklas 
2354b725ae77Skettenis   found_sal = find_pc_sect_line (startaddr, sal.section, 0);
2355e93f7393Sniklas   if (found_sal.line != sal.line)
2356e93f7393Sniklas     {
2357e93f7393Sniklas       /* The specified line (sal) has zero bytes.  */
2358e93f7393Sniklas       *startptr = found_sal.pc;
2359e93f7393Sniklas       *endptr = found_sal.pc;
2360e93f7393Sniklas     }
2361e93f7393Sniklas   else
2362e93f7393Sniklas     {
2363e93f7393Sniklas       *startptr = found_sal.pc;
2364e93f7393Sniklas       *endptr = found_sal.end;
2365e93f7393Sniklas     }
2366e93f7393Sniklas   return 1;
2367e93f7393Sniklas }
2368e93f7393Sniklas 
2369e93f7393Sniklas /* Given a line table and a line number, return the index into the line
2370e93f7393Sniklas    table for the pc of the nearest line whose number is >= the specified one.
2371e93f7393Sniklas    Return -1 if none is found.  The value is >= 0 if it is an index.
2372e93f7393Sniklas 
2373e93f7393Sniklas    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
2374e93f7393Sniklas 
2375e93f7393Sniklas static int
find_line_common(struct linetable * l,int lineno,int * exact_match)2376b725ae77Skettenis find_line_common (struct linetable *l, int lineno,
2377b725ae77Skettenis 		  int *exact_match)
2378e93f7393Sniklas {
2379b725ae77Skettenis   int i;
2380b725ae77Skettenis   int len;
2381e93f7393Sniklas 
2382e93f7393Sniklas   /* BEST is the smallest linenumber > LINENO so far seen,
2383e93f7393Sniklas      or 0 if none has been seen so far.
2384e93f7393Sniklas      BEST_INDEX identifies the item for it.  */
2385e93f7393Sniklas 
2386e93f7393Sniklas   int best_index = -1;
2387e93f7393Sniklas   int best = 0;
2388e93f7393Sniklas 
2389e93f7393Sniklas   if (lineno <= 0)
2390e93f7393Sniklas     return -1;
2391e93f7393Sniklas   if (l == 0)
2392e93f7393Sniklas     return -1;
2393e93f7393Sniklas 
2394e93f7393Sniklas   len = l->nitems;
2395e93f7393Sniklas   for (i = 0; i < len; i++)
2396e93f7393Sniklas     {
2397b725ae77Skettenis       struct linetable_entry *item = &(l->item[i]);
2398e93f7393Sniklas 
2399e93f7393Sniklas       if (item->line == lineno)
2400e93f7393Sniklas 	{
2401e93f7393Sniklas 	  /* Return the first (lowest address) entry which matches.  */
2402e93f7393Sniklas 	  *exact_match = 1;
2403e93f7393Sniklas 	  return i;
2404e93f7393Sniklas 	}
2405e93f7393Sniklas 
2406e93f7393Sniklas       if (item->line > lineno && (best == 0 || item->line < best))
2407e93f7393Sniklas 	{
2408e93f7393Sniklas 	  best = item->line;
2409e93f7393Sniklas 	  best_index = i;
2410e93f7393Sniklas 	}
2411e93f7393Sniklas     }
2412e93f7393Sniklas 
2413e93f7393Sniklas   /* If we got here, we didn't get an exact match.  */
2414e93f7393Sniklas 
2415e93f7393Sniklas   *exact_match = 0;
2416e93f7393Sniklas   return best_index;
2417e93f7393Sniklas }
2418e93f7393Sniklas 
2419e93f7393Sniklas int
find_pc_line_pc_range(CORE_ADDR pc,CORE_ADDR * startptr,CORE_ADDR * endptr)2420b725ae77Skettenis find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2421e93f7393Sniklas {
2422e93f7393Sniklas   struct symtab_and_line sal;
2423e93f7393Sniklas   sal = find_pc_line (pc, 0);
2424e93f7393Sniklas   *startptr = sal.pc;
2425e93f7393Sniklas   *endptr = sal.end;
2426e93f7393Sniklas   return sal.symtab != 0;
2427e93f7393Sniklas }
2428e93f7393Sniklas 
2429e93f7393Sniklas /* Given a function symbol SYM, find the symtab and line for the start
2430e93f7393Sniklas    of the function.
2431e93f7393Sniklas    If the argument FUNFIRSTLINE is nonzero, we want the first line
2432e93f7393Sniklas    of real code inside the function.  */
2433e93f7393Sniklas 
2434b725ae77Skettenis struct symtab_and_line
find_function_start_sal(struct symbol * sym,int funfirstline)2435b725ae77Skettenis find_function_start_sal (struct symbol *sym, int funfirstline)
2436e93f7393Sniklas {
2437e93f7393Sniklas   CORE_ADDR pc;
2438e93f7393Sniklas   struct symtab_and_line sal;
2439e93f7393Sniklas 
2440e93f7393Sniklas   pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2441b725ae77Skettenis   fixup_symbol_section (sym, NULL);
2442e93f7393Sniklas   if (funfirstline)
2443b725ae77Skettenis     {				/* skip "first line" of function (which is actually its prologue) */
2444b725ae77Skettenis       asection *section = SYMBOL_BFD_SECTION (sym);
2445b725ae77Skettenis       /* If function is in an unmapped overlay, use its unmapped LMA
2446b725ae77Skettenis          address, so that SKIP_PROLOGUE has something unique to work on */
2447b725ae77Skettenis       if (section_is_overlay (section) &&
2448b725ae77Skettenis 	  !section_is_mapped (section))
2449b725ae77Skettenis 	pc = overlay_unmapped_address (pc, section);
2450e93f7393Sniklas 
2451*63addd46Skettenis       pc += DEPRECATED_FUNCTION_START_OFFSET;
2452b725ae77Skettenis       pc = SKIP_PROLOGUE (pc);
2453b725ae77Skettenis 
2454b725ae77Skettenis       /* For overlays, map pc back into its mapped VMA range */
2455b725ae77Skettenis       pc = overlay_mapped_address (pc, section);
2456b725ae77Skettenis     }
2457b725ae77Skettenis   sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2458b725ae77Skettenis 
2459e93f7393Sniklas   /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2460e93f7393Sniklas      line is still part of the same function.  */
2461e93f7393Sniklas   if (sal.pc != pc
2462e93f7393Sniklas       && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2463e93f7393Sniklas       && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2464e93f7393Sniklas     {
2465e93f7393Sniklas       /* First pc of next line */
2466e93f7393Sniklas       pc = sal.end;
2467e93f7393Sniklas       /* Recalculate the line number (might not be N+1).  */
2468b725ae77Skettenis       sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2469e93f7393Sniklas     }
2470e93f7393Sniklas   sal.pc = pc;
2471e93f7393Sniklas 
2472e93f7393Sniklas   return sal;
2473e93f7393Sniklas }
2474b725ae77Skettenis 
2475e93f7393Sniklas /* If P is of the form "operator[ \t]+..." where `...' is
2476e93f7393Sniklas    some legitimate operator text, return a pointer to the
2477e93f7393Sniklas    beginning of the substring of the operator text.
2478e93f7393Sniklas    Otherwise, return "".  */
2479e93f7393Sniklas char *
operator_chars(char * p,char ** end)2480b725ae77Skettenis operator_chars (char *p, char **end)
2481e93f7393Sniklas {
2482e93f7393Sniklas   *end = "";
2483e93f7393Sniklas   if (strncmp (p, "operator", 8))
2484e93f7393Sniklas     return *end;
2485e93f7393Sniklas   p += 8;
2486e93f7393Sniklas 
2487e93f7393Sniklas   /* Don't get faked out by `operator' being part of a longer
2488e93f7393Sniklas      identifier.  */
2489e93f7393Sniklas   if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2490e93f7393Sniklas     return *end;
2491e93f7393Sniklas 
2492e93f7393Sniklas   /* Allow some whitespace between `operator' and the operator symbol.  */
2493e93f7393Sniklas   while (*p == ' ' || *p == '\t')
2494e93f7393Sniklas     p++;
2495e93f7393Sniklas 
2496e93f7393Sniklas   /* Recognize 'operator TYPENAME'. */
2497e93f7393Sniklas 
2498e93f7393Sniklas   if (isalpha (*p) || *p == '_' || *p == '$')
2499e93f7393Sniklas     {
2500b725ae77Skettenis       char *q = p + 1;
2501e93f7393Sniklas       while (isalnum (*q) || *q == '_' || *q == '$')
2502e93f7393Sniklas 	q++;
2503e93f7393Sniklas       *end = q;
2504e93f7393Sniklas       return p;
2505e93f7393Sniklas     }
2506e93f7393Sniklas 
2507b725ae77Skettenis   while (*p)
2508e93f7393Sniklas     switch (*p)
2509e93f7393Sniklas       {
2510b725ae77Skettenis       case '\\':			/* regexp quoting */
2511b725ae77Skettenis 	if (p[1] == '*')
2512b725ae77Skettenis 	  {
2513b725ae77Skettenis 	    if (p[2] == '=')	/* 'operator\*=' */
2514b725ae77Skettenis 	      *end = p + 3;
2515b725ae77Skettenis 	    else			/* 'operator\*'  */
2516b725ae77Skettenis 	      *end = p + 2;
2517b725ae77Skettenis 	    return p;
2518b725ae77Skettenis 	  }
2519b725ae77Skettenis 	else if (p[1] == '[')
2520b725ae77Skettenis 	  {
2521b725ae77Skettenis 	    if (p[2] == ']')
2522b725ae77Skettenis 	      error ("mismatched quoting on brackets, try 'operator\\[\\]'");
2523b725ae77Skettenis 	    else if (p[2] == '\\' && p[3] == ']')
2524b725ae77Skettenis 	      {
2525b725ae77Skettenis 		*end = p + 4;	/* 'operator\[\]' */
2526b725ae77Skettenis 		return p;
2527b725ae77Skettenis 	      }
2528b725ae77Skettenis 	    else
2529b725ae77Skettenis 	      error ("nothing is allowed between '[' and ']'");
2530b725ae77Skettenis 	  }
2531b725ae77Skettenis 	else
2532b725ae77Skettenis 	  {
2533b725ae77Skettenis 	    /* Gratuitous qoute: skip it and move on. */
2534b725ae77Skettenis 	    p++;
2535b725ae77Skettenis 	    continue;
2536b725ae77Skettenis 	  }
2537b725ae77Skettenis 	break;
2538e93f7393Sniklas       case '!':
2539e93f7393Sniklas       case '=':
2540e93f7393Sniklas       case '*':
2541e93f7393Sniklas       case '/':
2542e93f7393Sniklas       case '%':
2543e93f7393Sniklas       case '^':
2544e93f7393Sniklas 	if (p[1] == '=')
2545e93f7393Sniklas 	  *end = p + 2;
2546e93f7393Sniklas 	else
2547e93f7393Sniklas 	  *end = p + 1;
2548e93f7393Sniklas 	return p;
2549e93f7393Sniklas       case '<':
2550e93f7393Sniklas       case '>':
2551e93f7393Sniklas       case '+':
2552e93f7393Sniklas       case '-':
2553e93f7393Sniklas       case '&':
2554e93f7393Sniklas       case '|':
2555b725ae77Skettenis 	if (p[0] == '-' && p[1] == '>')
2556b725ae77Skettenis 	  {
2557b725ae77Skettenis 	    /* Struct pointer member operator 'operator->'. */
2558b725ae77Skettenis 	    if (p[2] == '*')
2559b725ae77Skettenis 	      {
2560b725ae77Skettenis 		*end = p + 3;	/* 'operator->*' */
2561b725ae77Skettenis 		return p;
2562b725ae77Skettenis 	      }
2563b725ae77Skettenis 	    else if (p[2] == '\\')
2564b725ae77Skettenis 	      {
2565b725ae77Skettenis 		*end = p + 4;	/* Hopefully 'operator->\*' */
2566b725ae77Skettenis 		return p;
2567b725ae77Skettenis 	      }
2568b725ae77Skettenis 	    else
2569b725ae77Skettenis 	      {
2570b725ae77Skettenis 		*end = p + 2;	/* 'operator->' */
2571b725ae77Skettenis 		return p;
2572b725ae77Skettenis 	      }
2573b725ae77Skettenis 	  }
2574e93f7393Sniklas 	if (p[1] == '=' || p[1] == p[0])
2575e93f7393Sniklas 	  *end = p + 2;
2576e93f7393Sniklas 	else
2577e93f7393Sniklas 	  *end = p + 1;
2578e93f7393Sniklas 	return p;
2579e93f7393Sniklas       case '~':
2580e93f7393Sniklas       case ',':
2581e93f7393Sniklas 	*end = p + 1;
2582e93f7393Sniklas 	return p;
2583e93f7393Sniklas       case '(':
2584e93f7393Sniklas 	if (p[1] != ')')
2585e93f7393Sniklas 	  error ("`operator ()' must be specified without whitespace in `()'");
2586e93f7393Sniklas 	*end = p + 2;
2587e93f7393Sniklas 	return p;
2588e93f7393Sniklas       case '?':
2589e93f7393Sniklas 	if (p[1] != ':')
2590e93f7393Sniklas 	  error ("`operator ?:' must be specified without whitespace in `?:'");
2591e93f7393Sniklas 	*end = p + 2;
2592e93f7393Sniklas 	return p;
2593e93f7393Sniklas       case '[':
2594e93f7393Sniklas 	if (p[1] != ']')
2595e93f7393Sniklas 	  error ("`operator []' must be specified without whitespace in `[]'");
2596e93f7393Sniklas 	*end = p + 2;
2597e93f7393Sniklas 	return p;
2598e93f7393Sniklas       default:
2599e93f7393Sniklas 	error ("`operator %s' not supported", p);
2600e93f7393Sniklas 	break;
2601e93f7393Sniklas       }
2602b725ae77Skettenis 
2603e93f7393Sniklas   *end = "";
2604e93f7393Sniklas   return *end;
2605e93f7393Sniklas }
2606e93f7393Sniklas 
2607b725ae77Skettenis 
2608b725ae77Skettenis /* If FILE is not already in the table of files, return zero;
2609b725ae77Skettenis    otherwise return non-zero.  Optionally add FILE to the table if ADD
2610b725ae77Skettenis    is non-zero.  If *FIRST is non-zero, forget the old table
2611b725ae77Skettenis    contents.  */
2612b725ae77Skettenis static int
filename_seen(const char * file,int add,int * first)2613b725ae77Skettenis filename_seen (const char *file, int add, int *first)
2614e93f7393Sniklas {
2615b725ae77Skettenis   /* Table of files seen so far.  */
2616b725ae77Skettenis   static const char **tab = NULL;
2617e93f7393Sniklas   /* Allocated size of tab in elements.
2618e93f7393Sniklas      Start with one 256-byte block (when using GNU malloc.c).
2619e93f7393Sniklas      24 is the malloc overhead when range checking is in effect.  */
2620e93f7393Sniklas   static int tab_alloc_size = (256 - 24) / sizeof (char *);
2621e93f7393Sniklas   /* Current size of tab in elements.  */
2622e93f7393Sniklas   static int tab_cur_size;
2623b725ae77Skettenis   const char **p;
2624e93f7393Sniklas 
2625e93f7393Sniklas   if (*first)
2626e93f7393Sniklas     {
2627e93f7393Sniklas       if (tab == NULL)
2628b725ae77Skettenis 	tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab));
2629e93f7393Sniklas       tab_cur_size = 0;
2630e93f7393Sniklas     }
2631e93f7393Sniklas 
2632b725ae77Skettenis   /* Is FILE in tab?  */
2633e93f7393Sniklas   for (p = tab; p < tab + tab_cur_size; p++)
2634b725ae77Skettenis     if (strcmp (*p, file) == 0)
2635b725ae77Skettenis       return 1;
2636b725ae77Skettenis 
2637b725ae77Skettenis   /* No; maybe add it to tab.  */
2638b725ae77Skettenis   if (add)
2639b725ae77Skettenis     {
2640e93f7393Sniklas       if (tab_cur_size == tab_alloc_size)
2641e93f7393Sniklas 	{
2642e93f7393Sniklas 	  tab_alloc_size *= 2;
2643b725ae77Skettenis 	  tab = (const char **) xrealloc ((char *) tab,
2644b725ae77Skettenis 					  tab_alloc_size * sizeof (*tab));
2645e93f7393Sniklas 	}
2646b725ae77Skettenis       tab[tab_cur_size++] = file;
2647b725ae77Skettenis     }
2648e93f7393Sniklas 
2649b725ae77Skettenis   return 0;
2650b725ae77Skettenis }
2651b725ae77Skettenis 
2652b725ae77Skettenis /* Slave routine for sources_info.  Force line breaks at ,'s.
2653b725ae77Skettenis    NAME is the name to print and *FIRST is nonzero if this is the first
2654b725ae77Skettenis    name printed.  Set *FIRST to zero.  */
2655b725ae77Skettenis static void
output_source_filename(const char * name,int * first)2656*63addd46Skettenis output_source_filename (const char *name, int *first)
2657b725ae77Skettenis {
2658b725ae77Skettenis   /* Since a single source file can result in several partial symbol
2659b725ae77Skettenis      tables, we need to avoid printing it more than once.  Note: if
2660b725ae77Skettenis      some of the psymtabs are read in and some are not, it gets
2661b725ae77Skettenis      printed both under "Source files for which symbols have been
2662b725ae77Skettenis      read" and "Source files for which symbols will be read in on
2663b725ae77Skettenis      demand".  I consider this a reasonable way to deal with the
2664b725ae77Skettenis      situation.  I'm not sure whether this can also happen for
2665b725ae77Skettenis      symtabs; it doesn't hurt to check.  */
2666b725ae77Skettenis 
2667b725ae77Skettenis   /* Was NAME already seen?  */
2668b725ae77Skettenis   if (filename_seen (name, 1, first))
2669b725ae77Skettenis     {
2670b725ae77Skettenis       /* Yes; don't print it again.  */
2671b725ae77Skettenis       return;
2672b725ae77Skettenis     }
2673b725ae77Skettenis   /* No; print it and reset *FIRST.  */
2674e93f7393Sniklas   if (*first)
2675e93f7393Sniklas     {
2676e93f7393Sniklas       *first = 0;
2677e93f7393Sniklas     }
2678e93f7393Sniklas   else
2679e93f7393Sniklas     {
2680e93f7393Sniklas       printf_filtered (", ");
2681e93f7393Sniklas     }
2682e93f7393Sniklas 
2683e93f7393Sniklas   wrap_here ("");
2684e93f7393Sniklas   fputs_filtered (name, gdb_stdout);
2685e93f7393Sniklas }
2686e93f7393Sniklas 
2687e93f7393Sniklas static void
sources_info(char * ignore,int from_tty)2688b725ae77Skettenis sources_info (char *ignore, int from_tty)
2689e93f7393Sniklas {
2690b725ae77Skettenis   struct symtab *s;
2691b725ae77Skettenis   struct partial_symtab *ps;
2692b725ae77Skettenis   struct objfile *objfile;
2693e93f7393Sniklas   int first;
2694e93f7393Sniklas 
2695e93f7393Sniklas   if (!have_full_symbols () && !have_partial_symbols ())
2696e93f7393Sniklas     {
2697b725ae77Skettenis       error ("No symbol table is loaded.  Use the \"file\" command.");
2698e93f7393Sniklas     }
2699e93f7393Sniklas 
2700e93f7393Sniklas   printf_filtered ("Source files for which symbols have been read in:\n\n");
2701e93f7393Sniklas 
2702e93f7393Sniklas   first = 1;
2703e93f7393Sniklas   ALL_SYMTABS (objfile, s)
2704e93f7393Sniklas   {
2705*63addd46Skettenis     const char *fullname = symtab_to_fullname (s);
2706*63addd46Skettenis     output_source_filename (fullname ? fullname : s->filename, &first);
2707e93f7393Sniklas   }
2708e93f7393Sniklas   printf_filtered ("\n\n");
2709e93f7393Sniklas 
2710e93f7393Sniklas   printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2711e93f7393Sniklas 
2712e93f7393Sniklas   first = 1;
2713e93f7393Sniklas   ALL_PSYMTABS (objfile, ps)
2714e93f7393Sniklas   {
2715e93f7393Sniklas     if (!ps->readin)
2716e93f7393Sniklas       {
2717*63addd46Skettenis 	const char *fullname = psymtab_to_fullname (ps);
2718*63addd46Skettenis 	output_source_filename (fullname ? fullname : ps->filename, &first);
2719e93f7393Sniklas       }
2720e93f7393Sniklas   }
2721e93f7393Sniklas   printf_filtered ("\n");
2722e93f7393Sniklas }
2723e93f7393Sniklas 
2724b725ae77Skettenis static int
file_matches(char * file,char * files[],int nfiles)2725b725ae77Skettenis file_matches (char *file, char *files[], int nfiles)
2726b725ae77Skettenis {
2727b725ae77Skettenis   int i;
2728e93f7393Sniklas 
2729b725ae77Skettenis   if (file != NULL && nfiles != 0)
2730b725ae77Skettenis     {
2731b725ae77Skettenis       for (i = 0; i < nfiles; i++)
2732b725ae77Skettenis 	{
2733b725ae77Skettenis 	  if (strcmp (files[i], lbasename (file)) == 0)
2734b725ae77Skettenis 	    return 1;
2735b725ae77Skettenis 	}
2736b725ae77Skettenis     }
2737b725ae77Skettenis   else if (nfiles == 0)
2738b725ae77Skettenis     return 1;
2739b725ae77Skettenis   return 0;
2740b725ae77Skettenis }
2741b725ae77Skettenis 
2742b725ae77Skettenis /* Free any memory associated with a search. */
2743b725ae77Skettenis void
free_search_symbols(struct symbol_search * symbols)2744b725ae77Skettenis free_search_symbols (struct symbol_search *symbols)
2745b725ae77Skettenis {
2746b725ae77Skettenis   struct symbol_search *p;
2747b725ae77Skettenis   struct symbol_search *next;
2748b725ae77Skettenis 
2749b725ae77Skettenis   for (p = symbols; p != NULL; p = next)
2750b725ae77Skettenis     {
2751b725ae77Skettenis       next = p->next;
2752b725ae77Skettenis       xfree (p);
2753b725ae77Skettenis     }
2754b725ae77Skettenis }
2755e93f7393Sniklas 
2756e93f7393Sniklas static void
do_free_search_symbols_cleanup(void * symbols)2757b725ae77Skettenis do_free_search_symbols_cleanup (void *symbols)
2758e93f7393Sniklas {
2759b725ae77Skettenis   free_search_symbols (symbols);
2760b725ae77Skettenis }
2761b725ae77Skettenis 
2762b725ae77Skettenis struct cleanup *
make_cleanup_free_search_symbols(struct symbol_search * symbols)2763b725ae77Skettenis make_cleanup_free_search_symbols (struct symbol_search *symbols)
2764b725ae77Skettenis {
2765b725ae77Skettenis   return make_cleanup (do_free_search_symbols_cleanup, symbols);
2766b725ae77Skettenis }
2767b725ae77Skettenis 
2768b725ae77Skettenis /* Helper function for sort_search_symbols and qsort.  Can only
2769b725ae77Skettenis    sort symbols, not minimal symbols.  */
2770b725ae77Skettenis static int
compare_search_syms(const void * sa,const void * sb)2771b725ae77Skettenis compare_search_syms (const void *sa, const void *sb)
2772b725ae77Skettenis {
2773b725ae77Skettenis   struct symbol_search **sym_a = (struct symbol_search **) sa;
2774b725ae77Skettenis   struct symbol_search **sym_b = (struct symbol_search **) sb;
2775b725ae77Skettenis 
2776b725ae77Skettenis   return strcmp (SYMBOL_PRINT_NAME ((*sym_a)->symbol),
2777b725ae77Skettenis 		 SYMBOL_PRINT_NAME ((*sym_b)->symbol));
2778b725ae77Skettenis }
2779b725ae77Skettenis 
2780b725ae77Skettenis /* Sort the ``nfound'' symbols in the list after prevtail.  Leave
2781b725ae77Skettenis    prevtail where it is, but update its next pointer to point to
2782b725ae77Skettenis    the first of the sorted symbols.  */
2783b725ae77Skettenis static struct symbol_search *
sort_search_symbols(struct symbol_search * prevtail,int nfound)2784b725ae77Skettenis sort_search_symbols (struct symbol_search *prevtail, int nfound)
2785b725ae77Skettenis {
2786b725ae77Skettenis   struct symbol_search **symbols, *symp, *old_next;
2787b725ae77Skettenis   int i;
2788b725ae77Skettenis 
2789b725ae77Skettenis   symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
2790b725ae77Skettenis 					       * nfound);
2791b725ae77Skettenis   symp = prevtail->next;
2792b725ae77Skettenis   for (i = 0; i < nfound; i++)
2793b725ae77Skettenis     {
2794b725ae77Skettenis       symbols[i] = symp;
2795b725ae77Skettenis       symp = symp->next;
2796b725ae77Skettenis     }
2797b725ae77Skettenis   /* Generally NULL.  */
2798b725ae77Skettenis   old_next = symp;
2799b725ae77Skettenis 
2800b725ae77Skettenis   qsort (symbols, nfound, sizeof (struct symbol_search *),
2801b725ae77Skettenis 	 compare_search_syms);
2802b725ae77Skettenis 
2803b725ae77Skettenis   symp = prevtail;
2804b725ae77Skettenis   for (i = 0; i < nfound; i++)
2805b725ae77Skettenis     {
2806b725ae77Skettenis       symp->next = symbols[i];
2807b725ae77Skettenis       symp = symp->next;
2808b725ae77Skettenis     }
2809b725ae77Skettenis   symp->next = old_next;
2810b725ae77Skettenis 
2811b725ae77Skettenis   xfree (symbols);
2812b725ae77Skettenis   return symp;
2813b725ae77Skettenis }
2814b725ae77Skettenis 
2815b725ae77Skettenis /* Search the symbol table for matches to the regular expression REGEXP,
2816b725ae77Skettenis    returning the results in *MATCHES.
2817b725ae77Skettenis 
2818b725ae77Skettenis    Only symbols of KIND are searched:
2819b725ae77Skettenis    FUNCTIONS_DOMAIN - search all functions
2820b725ae77Skettenis    TYPES_DOMAIN     - search all type names
2821b725ae77Skettenis    METHODS_DOMAIN   - search all methods NOT IMPLEMENTED
2822b725ae77Skettenis    VARIABLES_DOMAIN - search all symbols, excluding functions, type names,
2823b725ae77Skettenis    and constants (enums)
2824b725ae77Skettenis 
2825b725ae77Skettenis    free_search_symbols should be called when *MATCHES is no longer needed.
2826b725ae77Skettenis 
2827b725ae77Skettenis    The results are sorted locally; each symtab's global and static blocks are
2828b725ae77Skettenis    separately alphabetized.
2829b725ae77Skettenis  */
2830b725ae77Skettenis void
search_symbols(char * regexp,domain_enum kind,int nfiles,char * files[],struct symbol_search ** matches)2831b725ae77Skettenis search_symbols (char *regexp, domain_enum kind, int nfiles, char *files[],
2832b725ae77Skettenis 		struct symbol_search **matches)
2833b725ae77Skettenis {
2834b725ae77Skettenis   struct symtab *s;
2835b725ae77Skettenis   struct partial_symtab *ps;
2836b725ae77Skettenis   struct blockvector *bv;
2837e93f7393Sniklas   struct blockvector *prev_bv = 0;
2838b725ae77Skettenis   struct block *b;
2839b725ae77Skettenis   int i = 0;
2840b725ae77Skettenis   struct dict_iterator iter;
2841b725ae77Skettenis   struct symbol *sym;
2842e93f7393Sniklas   struct partial_symbol **psym;
2843e93f7393Sniklas   struct objfile *objfile;
2844e93f7393Sniklas   struct minimal_symbol *msymbol;
2845e93f7393Sniklas   char *val;
2846e93f7393Sniklas   int found_misc = 0;
2847e93f7393Sniklas   static enum minimal_symbol_type types[]
2848b725ae77Skettenis   =
2849b725ae77Skettenis   {mst_data, mst_text, mst_abs, mst_unknown};
2850e93f7393Sniklas   static enum minimal_symbol_type types2[]
2851b725ae77Skettenis   =
2852b725ae77Skettenis   {mst_bss, mst_file_text, mst_abs, mst_unknown};
2853e93f7393Sniklas   static enum minimal_symbol_type types3[]
2854b725ae77Skettenis   =
2855b725ae77Skettenis   {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
2856e93f7393Sniklas   static enum minimal_symbol_type types4[]
2857b725ae77Skettenis   =
2858b725ae77Skettenis   {mst_file_bss, mst_text, mst_abs, mst_unknown};
2859b725ae77Skettenis   enum minimal_symbol_type ourtype;
2860b725ae77Skettenis   enum minimal_symbol_type ourtype2;
2861b725ae77Skettenis   enum minimal_symbol_type ourtype3;
2862b725ae77Skettenis   enum minimal_symbol_type ourtype4;
2863b725ae77Skettenis   struct symbol_search *sr;
2864b725ae77Skettenis   struct symbol_search *psr;
2865b725ae77Skettenis   struct symbol_search *tail;
2866b725ae77Skettenis   struct cleanup *old_chain = NULL;
2867b725ae77Skettenis 
2868b725ae77Skettenis   if (kind < VARIABLES_DOMAIN)
2869b725ae77Skettenis     error ("must search on specific domain");
2870b725ae77Skettenis 
2871b725ae77Skettenis   ourtype = types[(int) (kind - VARIABLES_DOMAIN)];
2872b725ae77Skettenis   ourtype2 = types2[(int) (kind - VARIABLES_DOMAIN)];
2873b725ae77Skettenis   ourtype3 = types3[(int) (kind - VARIABLES_DOMAIN)];
2874b725ae77Skettenis   ourtype4 = types4[(int) (kind - VARIABLES_DOMAIN)];
2875b725ae77Skettenis 
2876b725ae77Skettenis   sr = *matches = NULL;
2877b725ae77Skettenis   tail = NULL;
2878e93f7393Sniklas 
2879e93f7393Sniklas   if (regexp != NULL)
2880e93f7393Sniklas     {
2881e93f7393Sniklas       /* Make sure spacing is right for C++ operators.
2882e93f7393Sniklas          This is just a courtesy to make the matching less sensitive
2883e93f7393Sniklas          to how many spaces the user leaves between 'operator'
2884e93f7393Sniklas          and <TYPENAME> or <OPERATOR>. */
2885e93f7393Sniklas       char *opend;
2886e93f7393Sniklas       char *opname = operator_chars (regexp, &opend);
2887e93f7393Sniklas       if (*opname)
2888e93f7393Sniklas 	{
2889e93f7393Sniklas 	  int fix = -1;		/* -1 means ok; otherwise number of spaces needed. */
2890e93f7393Sniklas 	  if (isalpha (*opname) || *opname == '_' || *opname == '$')
2891e93f7393Sniklas 	    {
2892e93f7393Sniklas 	      /* There should 1 space between 'operator' and 'TYPENAME'. */
2893e93f7393Sniklas 	      if (opname[-1] != ' ' || opname[-2] == ' ')
2894e93f7393Sniklas 		fix = 1;
2895e93f7393Sniklas 	    }
2896e93f7393Sniklas 	  else
2897e93f7393Sniklas 	    {
2898e93f7393Sniklas 	      /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2899e93f7393Sniklas 	      if (opname[-1] == ' ')
2900e93f7393Sniklas 		fix = 0;
2901e93f7393Sniklas 	    }
2902e93f7393Sniklas 	  /* If wrong number of spaces, fix it. */
2903e93f7393Sniklas 	  if (fix >= 0)
2904e93f7393Sniklas 	    {
2905b725ae77Skettenis 	      char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
2906e93f7393Sniklas 	      sprintf (tmp, "operator%.*s%s", fix, " ", opname);
2907e93f7393Sniklas 	      regexp = tmp;
2908e93f7393Sniklas 	    }
2909e93f7393Sniklas 	}
2910e93f7393Sniklas 
2911e93f7393Sniklas       if (0 != (val = re_comp (regexp)))
2912e93f7393Sniklas 	error ("Invalid regexp (%s): %s", val, regexp);
2913e93f7393Sniklas     }
2914e93f7393Sniklas 
2915e93f7393Sniklas   /* Search through the partial symtabs *first* for all symbols
2916e93f7393Sniklas      matching the regexp.  That way we don't have to reproduce all of
2917e93f7393Sniklas      the machinery below. */
2918e93f7393Sniklas 
2919e93f7393Sniklas   ALL_PSYMTABS (objfile, ps)
2920e93f7393Sniklas   {
2921e93f7393Sniklas     struct partial_symbol **bound, **gbound, **sbound;
2922e93f7393Sniklas     int keep_going = 1;
2923e93f7393Sniklas 
2924b725ae77Skettenis     if (ps->readin)
2925b725ae77Skettenis       continue;
2926e93f7393Sniklas 
2927e93f7393Sniklas     gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2928e93f7393Sniklas     sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2929e93f7393Sniklas     bound = gbound;
2930e93f7393Sniklas 
2931e93f7393Sniklas     /* Go through all of the symbols stored in a partial
2932e93f7393Sniklas        symtab in one loop. */
2933e93f7393Sniklas     psym = objfile->global_psymbols.list + ps->globals_offset;
2934e93f7393Sniklas     while (keep_going)
2935e93f7393Sniklas       {
2936e93f7393Sniklas 	if (psym >= bound)
2937e93f7393Sniklas 	  {
2938e93f7393Sniklas 	    if (bound == gbound && ps->n_static_syms != 0)
2939e93f7393Sniklas 	      {
2940e93f7393Sniklas 		psym = objfile->static_psymbols.list + ps->statics_offset;
2941e93f7393Sniklas 		bound = sbound;
2942e93f7393Sniklas 	      }
2943e93f7393Sniklas 	    else
2944e93f7393Sniklas 	      keep_going = 0;
2945e93f7393Sniklas 	    continue;
2946e93f7393Sniklas 	  }
2947e93f7393Sniklas 	else
2948e93f7393Sniklas 	  {
2949e93f7393Sniklas 	    QUIT;
2950e93f7393Sniklas 
2951e93f7393Sniklas 	    /* If it would match (logic taken from loop below)
2952e93f7393Sniklas 	       load the file and go on to the next one */
2953b725ae77Skettenis 	    if (file_matches (ps->filename, files, nfiles)
2954b725ae77Skettenis 		&& ((regexp == NULL
2955b725ae77Skettenis 		     || re_exec (SYMBOL_NATURAL_NAME (*psym)) != 0)
2956b725ae77Skettenis 		    && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
2957e93f7393Sniklas 			 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2958b725ae77Skettenis 			|| (kind == FUNCTIONS_DOMAIN && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2959b725ae77Skettenis 			|| (kind == TYPES_DOMAIN && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2960b725ae77Skettenis 			|| (kind == METHODS_DOMAIN && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
2961e93f7393Sniklas 	      {
2962e93f7393Sniklas 		PSYMTAB_TO_SYMTAB (ps);
2963e93f7393Sniklas 		keep_going = 0;
2964e93f7393Sniklas 	      }
2965e93f7393Sniklas 	  }
2966e93f7393Sniklas 	psym++;
2967e93f7393Sniklas       }
2968e93f7393Sniklas   }
2969e93f7393Sniklas 
2970e93f7393Sniklas   /* Here, we search through the minimal symbol tables for functions
2971e93f7393Sniklas      and variables that match, and force their symbols to be read.
2972e93f7393Sniklas      This is in particular necessary for demangled variable names,
2973e93f7393Sniklas      which are no longer put into the partial symbol tables.
2974e93f7393Sniklas      The symbol will then be found during the scan of symtabs below.
2975e93f7393Sniklas 
2976e93f7393Sniklas      For functions, find_pc_symtab should succeed if we have debug info
2977e93f7393Sniklas      for the function, for variables we have to call lookup_symbol
2978e93f7393Sniklas      to determine if the variable has debug info.
2979e93f7393Sniklas      If the lookup fails, set found_misc so that we will rescan to print
2980e93f7393Sniklas      any matching symbols without debug info.
2981e93f7393Sniklas    */
2982e93f7393Sniklas 
2983b725ae77Skettenis   if (nfiles == 0 && (kind == VARIABLES_DOMAIN || kind == FUNCTIONS_DOMAIN))
2984e93f7393Sniklas     {
2985e93f7393Sniklas       ALL_MSYMBOLS (objfile, msymbol)
2986e93f7393Sniklas       {
2987e93f7393Sniklas 	if (MSYMBOL_TYPE (msymbol) == ourtype ||
2988e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype2 ||
2989e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype3 ||
2990e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype4)
2991e93f7393Sniklas 	  {
2992b725ae77Skettenis 	    if (regexp == NULL
2993b725ae77Skettenis 		|| re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
2994e93f7393Sniklas 	      {
2995e93f7393Sniklas 		if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
2996e93f7393Sniklas 		  {
2997b725ae77Skettenis 		    /* FIXME: carlton/2003-02-04: Given that the
2998b725ae77Skettenis 		       semantics of lookup_symbol keeps on changing
2999b725ae77Skettenis 		       slightly, it would be a nice idea if we had a
3000b725ae77Skettenis 		       function lookup_symbol_minsym that found the
3001b725ae77Skettenis 		       symbol associated to a given minimal symbol (if
3002b725ae77Skettenis 		       any).  */
3003b725ae77Skettenis 		    if (kind == FUNCTIONS_DOMAIN
3004b725ae77Skettenis 			|| lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
3005e93f7393Sniklas 					  (struct block *) NULL,
3006b725ae77Skettenis 					  VAR_DOMAIN,
3007e93f7393Sniklas 					0, (struct symtab **) NULL) == NULL)
3008e93f7393Sniklas 		      found_misc = 1;
3009e93f7393Sniklas 		  }
3010e93f7393Sniklas 	      }
3011e93f7393Sniklas 	  }
3012e93f7393Sniklas       }
3013e93f7393Sniklas     }
3014e93f7393Sniklas 
3015e93f7393Sniklas   ALL_SYMTABS (objfile, s)
3016e93f7393Sniklas   {
3017e93f7393Sniklas     bv = BLOCKVECTOR (s);
3018e93f7393Sniklas     /* Often many files share a blockvector.
3019e93f7393Sniklas        Scan each blockvector only once so that
3020e93f7393Sniklas        we don't get every symbol many times.
3021e93f7393Sniklas        It happens that the first symtab in the list
3022e93f7393Sniklas        for any given blockvector is the main file.  */
3023e93f7393Sniklas     if (bv != prev_bv)
3024e93f7393Sniklas       for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3025e93f7393Sniklas 	{
3026b725ae77Skettenis 	  struct symbol_search *prevtail = tail;
3027b725ae77Skettenis 	  int nfound = 0;
3028e93f7393Sniklas 	  b = BLOCKVECTOR_BLOCK (bv, i);
3029b725ae77Skettenis 	  ALL_BLOCK_SYMBOLS (b, iter, sym)
3030e93f7393Sniklas 	    {
3031e93f7393Sniklas 	      QUIT;
3032b725ae77Skettenis 	      if (file_matches (s->filename, files, nfiles)
3033b725ae77Skettenis 		  && ((regexp == NULL
3034b725ae77Skettenis 		       || re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
3035b725ae77Skettenis 		      && ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3036e93f7393Sniklas 			   && SYMBOL_CLASS (sym) != LOC_BLOCK
3037e93f7393Sniklas 			   && SYMBOL_CLASS (sym) != LOC_CONST)
3038b725ae77Skettenis 			  || (kind == FUNCTIONS_DOMAIN && SYMBOL_CLASS (sym) == LOC_BLOCK)
3039b725ae77Skettenis 			  || (kind == TYPES_DOMAIN && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3040b725ae77Skettenis 			  || (kind == METHODS_DOMAIN && SYMBOL_CLASS (sym) == LOC_BLOCK))))
3041e93f7393Sniklas 		{
3042b725ae77Skettenis 		  /* match */
3043b725ae77Skettenis 		  psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3044b725ae77Skettenis 		  psr->block = i;
3045b725ae77Skettenis 		  psr->symtab = s;
3046b725ae77Skettenis 		  psr->symbol = sym;
3047b725ae77Skettenis 		  psr->msymbol = NULL;
3048b725ae77Skettenis 		  psr->next = NULL;
3049b725ae77Skettenis 		  if (tail == NULL)
3050b725ae77Skettenis 		    sr = psr;
3051b725ae77Skettenis 		  else
3052b725ae77Skettenis 		    tail->next = psr;
3053b725ae77Skettenis 		  tail = psr;
3054b725ae77Skettenis 		  nfound ++;
3055e93f7393Sniklas 		}
3056e93f7393Sniklas 	    }
3057b725ae77Skettenis 	  if (nfound > 0)
3058e93f7393Sniklas 	    {
3059b725ae77Skettenis 	      if (prevtail == NULL)
3060e93f7393Sniklas 		{
3061b725ae77Skettenis 		  struct symbol_search dummy;
3062e93f7393Sniklas 
3063b725ae77Skettenis 		  dummy.next = sr;
3064b725ae77Skettenis 		  tail = sort_search_symbols (&dummy, nfound);
3065b725ae77Skettenis 		  sr = dummy.next;
3066b725ae77Skettenis 
3067b725ae77Skettenis 		  old_chain = make_cleanup_free_search_symbols (sr);
3068e93f7393Sniklas 		}
3069e93f7393Sniklas 	      else
3070b725ae77Skettenis 		tail = sort_search_symbols (prevtail, nfound);
3071e93f7393Sniklas 	    }
3072e93f7393Sniklas 	}
3073e93f7393Sniklas     prev_bv = bv;
3074e93f7393Sniklas   }
3075e93f7393Sniklas 
3076e93f7393Sniklas   /* If there are no eyes, avoid all contact.  I mean, if there are
3077e93f7393Sniklas      no debug symbols, then print directly from the msymbol_vector.  */
3078e93f7393Sniklas 
3079b725ae77Skettenis   if (found_misc || kind != FUNCTIONS_DOMAIN)
3080e93f7393Sniklas     {
3081e93f7393Sniklas       ALL_MSYMBOLS (objfile, msymbol)
3082e93f7393Sniklas       {
3083e93f7393Sniklas 	if (MSYMBOL_TYPE (msymbol) == ourtype ||
3084e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype2 ||
3085e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype3 ||
3086e93f7393Sniklas 	    MSYMBOL_TYPE (msymbol) == ourtype4)
3087e93f7393Sniklas 	  {
3088b725ae77Skettenis 	    if (regexp == NULL
3089b725ae77Skettenis 		|| re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
3090e93f7393Sniklas 	      {
3091e93f7393Sniklas 		/* Functions:  Look up by address. */
3092b725ae77Skettenis 		if (kind != FUNCTIONS_DOMAIN ||
3093e93f7393Sniklas 		    (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
3094e93f7393Sniklas 		  {
3095e93f7393Sniklas 		    /* Variables/Absolutes:  Look up by name */
3096b725ae77Skettenis 		    if (lookup_symbol (SYMBOL_LINKAGE_NAME (msymbol),
3097b725ae77Skettenis 				       (struct block *) NULL, VAR_DOMAIN,
3098e93f7393Sniklas 				       0, (struct symtab **) NULL) == NULL)
3099e93f7393Sniklas 		      {
3100b725ae77Skettenis 			/* match */
3101b725ae77Skettenis 			psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3102b725ae77Skettenis 			psr->block = i;
3103b725ae77Skettenis 			psr->msymbol = msymbol;
3104b725ae77Skettenis 			psr->symtab = NULL;
3105b725ae77Skettenis 			psr->symbol = NULL;
3106b725ae77Skettenis 			psr->next = NULL;
3107b725ae77Skettenis 			if (tail == NULL)
3108e93f7393Sniklas 			  {
3109b725ae77Skettenis 			    sr = psr;
3110b725ae77Skettenis 			    old_chain = make_cleanup_free_search_symbols (sr);
3111e93f7393Sniklas 			  }
3112b725ae77Skettenis 			else
3113b725ae77Skettenis 			  tail->next = psr;
3114b725ae77Skettenis 			tail = psr;
3115b725ae77Skettenis 		      }
3116b725ae77Skettenis 		  }
3117b725ae77Skettenis 	      }
3118b725ae77Skettenis 	  }
3119b725ae77Skettenis       }
3120b725ae77Skettenis     }
3121b725ae77Skettenis 
3122b725ae77Skettenis   *matches = sr;
3123b725ae77Skettenis   if (sr != NULL)
3124b725ae77Skettenis     discard_cleanups (old_chain);
3125b725ae77Skettenis }
3126b725ae77Skettenis 
3127b725ae77Skettenis /* Helper function for symtab_symbol_info, this function uses
3128b725ae77Skettenis    the data returned from search_symbols() to print information
3129b725ae77Skettenis    regarding the match to gdb_stdout.
3130b725ae77Skettenis  */
3131b725ae77Skettenis static void
print_symbol_info(domain_enum kind,struct symtab * s,struct symbol * sym,int block,char * last)3132b725ae77Skettenis print_symbol_info (domain_enum kind, struct symtab *s, struct symbol *sym,
3133b725ae77Skettenis 		   int block, char *last)
3134b725ae77Skettenis {
3135b725ae77Skettenis   if (last == NULL || strcmp (last, s->filename) != 0)
3136b725ae77Skettenis     {
3137b725ae77Skettenis       fputs_filtered ("\nFile ", gdb_stdout);
3138b725ae77Skettenis       fputs_filtered (s->filename, gdb_stdout);
3139b725ae77Skettenis       fputs_filtered (":\n", gdb_stdout);
3140b725ae77Skettenis     }
3141b725ae77Skettenis 
3142b725ae77Skettenis   if (kind != TYPES_DOMAIN && block == STATIC_BLOCK)
3143b725ae77Skettenis     printf_filtered ("static ");
3144b725ae77Skettenis 
3145b725ae77Skettenis   /* Typedef that is not a C++ class */
3146b725ae77Skettenis   if (kind == TYPES_DOMAIN
3147b725ae77Skettenis       && SYMBOL_DOMAIN (sym) != STRUCT_DOMAIN)
3148b725ae77Skettenis     typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3149b725ae77Skettenis   /* variable, func, or typedef-that-is-c++-class */
3150b725ae77Skettenis   else if (kind < TYPES_DOMAIN ||
3151b725ae77Skettenis 	   (kind == TYPES_DOMAIN &&
3152b725ae77Skettenis 	    SYMBOL_DOMAIN (sym) == STRUCT_DOMAIN))
3153b725ae77Skettenis     {
3154b725ae77Skettenis       type_print (SYMBOL_TYPE (sym),
3155b725ae77Skettenis 		  (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3156b725ae77Skettenis 		   ? "" : SYMBOL_PRINT_NAME (sym)),
3157b725ae77Skettenis 		  gdb_stdout, 0);
3158b725ae77Skettenis 
3159b725ae77Skettenis       printf_filtered (";\n");
3160b725ae77Skettenis     }
3161b725ae77Skettenis }
3162b725ae77Skettenis 
3163b725ae77Skettenis /* This help function for symtab_symbol_info() prints information
3164b725ae77Skettenis    for non-debugging symbols to gdb_stdout.
3165b725ae77Skettenis  */
3166b725ae77Skettenis static void
print_msymbol_info(struct minimal_symbol * msymbol)3167b725ae77Skettenis print_msymbol_info (struct minimal_symbol *msymbol)
3168b725ae77Skettenis {
3169b725ae77Skettenis   char *tmp;
3170b725ae77Skettenis 
3171b725ae77Skettenis   if (TARGET_ADDR_BIT <= 32)
3172*63addd46Skettenis     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3173b725ae77Skettenis 			     & (CORE_ADDR) 0xffffffff,
3174*63addd46Skettenis 			     8);
3175b725ae77Skettenis   else
3176*63addd46Skettenis     tmp = hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3177*63addd46Skettenis 			     16);
3178b725ae77Skettenis   printf_filtered ("%s  %s\n",
3179b725ae77Skettenis 		   tmp, SYMBOL_PRINT_NAME (msymbol));
3180b725ae77Skettenis }
3181b725ae77Skettenis 
3182b725ae77Skettenis /* This is the guts of the commands "info functions", "info types", and
3183b725ae77Skettenis    "info variables". It calls search_symbols to find all matches and then
3184b725ae77Skettenis    print_[m]symbol_info to print out some useful information about the
3185b725ae77Skettenis    matches.
3186b725ae77Skettenis  */
3187b725ae77Skettenis static void
symtab_symbol_info(char * regexp,domain_enum kind,int from_tty)3188b725ae77Skettenis symtab_symbol_info (char *regexp, domain_enum kind, int from_tty)
3189b725ae77Skettenis {
3190b725ae77Skettenis   static char *classnames[]
3191b725ae77Skettenis   =
3192b725ae77Skettenis   {"variable", "function", "type", "method"};
3193b725ae77Skettenis   struct symbol_search *symbols;
3194b725ae77Skettenis   struct symbol_search *p;
3195b725ae77Skettenis   struct cleanup *old_chain;
3196b725ae77Skettenis   char *last_filename = NULL;
3197b725ae77Skettenis   int first = 1;
3198b725ae77Skettenis 
3199b725ae77Skettenis   /* must make sure that if we're interrupted, symbols gets freed */
3200b725ae77Skettenis   search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3201b725ae77Skettenis   old_chain = make_cleanup_free_search_symbols (symbols);
3202b725ae77Skettenis 
3203b725ae77Skettenis   printf_filtered (regexp
3204b725ae77Skettenis 		   ? "All %ss matching regular expression \"%s\":\n"
3205b725ae77Skettenis 		   : "All defined %ss:\n",
3206b725ae77Skettenis 		   classnames[(int) (kind - VARIABLES_DOMAIN)], regexp);
3207b725ae77Skettenis 
3208b725ae77Skettenis   for (p = symbols; p != NULL; p = p->next)
3209b725ae77Skettenis     {
3210b725ae77Skettenis       QUIT;
3211b725ae77Skettenis 
3212b725ae77Skettenis       if (p->msymbol != NULL)
3213b725ae77Skettenis 	{
3214b725ae77Skettenis 	  if (first)
3215e93f7393Sniklas 	    {
3216e93f7393Sniklas 	      printf_filtered ("\nNon-debugging symbols:\n");
3217b725ae77Skettenis 	      first = 0;
3218e93f7393Sniklas 	    }
3219b725ae77Skettenis 	  print_msymbol_info (p->msymbol);
3220e93f7393Sniklas 	}
3221b725ae77Skettenis       else
3222b725ae77Skettenis 	{
3223b725ae77Skettenis 	  print_symbol_info (kind,
3224b725ae77Skettenis 			     p->symtab,
3225b725ae77Skettenis 			     p->symbol,
3226b725ae77Skettenis 			     p->block,
3227b725ae77Skettenis 			     last_filename);
3228b725ae77Skettenis 	  last_filename = p->symtab->filename;
3229e93f7393Sniklas 	}
3230e93f7393Sniklas     }
3231e93f7393Sniklas 
3232b725ae77Skettenis   do_cleanups (old_chain);
3233e93f7393Sniklas }
3234e93f7393Sniklas 
3235e93f7393Sniklas static void
variables_info(char * regexp,int from_tty)3236b725ae77Skettenis variables_info (char *regexp, int from_tty)
3237e93f7393Sniklas {
3238b725ae77Skettenis   symtab_symbol_info (regexp, VARIABLES_DOMAIN, from_tty);
3239e93f7393Sniklas }
3240e93f7393Sniklas 
3241e93f7393Sniklas static void
functions_info(char * regexp,int from_tty)3242b725ae77Skettenis functions_info (char *regexp, int from_tty)
3243e93f7393Sniklas {
3244b725ae77Skettenis   symtab_symbol_info (regexp, FUNCTIONS_DOMAIN, from_tty);
3245e93f7393Sniklas }
3246e93f7393Sniklas 
3247b725ae77Skettenis 
3248e93f7393Sniklas static void
types_info(char * regexp,int from_tty)3249b725ae77Skettenis types_info (char *regexp, int from_tty)
3250e93f7393Sniklas {
3251b725ae77Skettenis   symtab_symbol_info (regexp, TYPES_DOMAIN, from_tty);
3252e93f7393Sniklas }
3253e93f7393Sniklas 
3254e93f7393Sniklas /* Breakpoint all functions matching regular expression. */
3255b725ae77Skettenis 
3256b725ae77Skettenis void
rbreak_command_wrapper(char * regexp,int from_tty)3257b725ae77Skettenis rbreak_command_wrapper (char *regexp, int from_tty)
3258b725ae77Skettenis {
3259b725ae77Skettenis   rbreak_command (regexp, from_tty);
3260b725ae77Skettenis }
3261b725ae77Skettenis 
3262e93f7393Sniklas static void
rbreak_command(char * regexp,int from_tty)3263b725ae77Skettenis rbreak_command (char *regexp, int from_tty)
3264e93f7393Sniklas {
3265b725ae77Skettenis   struct symbol_search *ss;
3266b725ae77Skettenis   struct symbol_search *p;
3267b725ae77Skettenis   struct cleanup *old_chain;
3268b725ae77Skettenis 
3269b725ae77Skettenis   search_symbols (regexp, FUNCTIONS_DOMAIN, 0, (char **) NULL, &ss);
3270b725ae77Skettenis   old_chain = make_cleanup_free_search_symbols (ss);
3271b725ae77Skettenis 
3272b725ae77Skettenis   for (p = ss; p != NULL; p = p->next)
3273b725ae77Skettenis     {
3274b725ae77Skettenis       if (p->msymbol == NULL)
3275b725ae77Skettenis 	{
3276b725ae77Skettenis 	  char *string = alloca (strlen (p->symtab->filename)
3277b725ae77Skettenis 				 + strlen (SYMBOL_LINKAGE_NAME (p->symbol))
3278b725ae77Skettenis 				 + 4);
3279b725ae77Skettenis 	  strcpy (string, p->symtab->filename);
3280b725ae77Skettenis 	  strcat (string, ":'");
3281b725ae77Skettenis 	  strcat (string, SYMBOL_LINKAGE_NAME (p->symbol));
3282b725ae77Skettenis 	  strcat (string, "'");
3283b725ae77Skettenis 	  break_command (string, from_tty);
3284b725ae77Skettenis 	  print_symbol_info (FUNCTIONS_DOMAIN,
3285b725ae77Skettenis 			     p->symtab,
3286b725ae77Skettenis 			     p->symbol,
3287b725ae77Skettenis 			     p->block,
3288b725ae77Skettenis 			     p->symtab->filename);
3289b725ae77Skettenis 	}
3290b725ae77Skettenis       else
3291b725ae77Skettenis 	{
3292b725ae77Skettenis 	  break_command (SYMBOL_LINKAGE_NAME (p->msymbol), from_tty);
3293b725ae77Skettenis 	  printf_filtered ("<function, no debug info> %s;\n",
3294b725ae77Skettenis 			   SYMBOL_PRINT_NAME (p->msymbol));
3295b725ae77Skettenis 	}
3296b725ae77Skettenis     }
3297b725ae77Skettenis 
3298b725ae77Skettenis   do_cleanups (old_chain);
3299e93f7393Sniklas }
3300e93f7393Sniklas 
3301e93f7393Sniklas 
3302e93f7393Sniklas /* Helper routine for make_symbol_completion_list.  */
3303e93f7393Sniklas 
3304e93f7393Sniklas static int return_val_size;
3305e93f7393Sniklas static int return_val_index;
3306e93f7393Sniklas static char **return_val;
3307e93f7393Sniklas 
3308e93f7393Sniklas #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3309e93f7393Sniklas       completion_list_add_name \
3310b725ae77Skettenis 	(SYMBOL_NATURAL_NAME (symbol), (sym_text), (len), (text), (word))
3311e93f7393Sniklas 
3312e93f7393Sniklas /*  Test to see if the symbol specified by SYMNAME (which is already
3313e93f7393Sniklas    demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3314e93f7393Sniklas    characters.  If so, add it to the current completion list. */
3315e93f7393Sniklas 
3316e93f7393Sniklas static void
completion_list_add_name(char * symname,char * sym_text,int sym_text_len,char * text,char * word)3317b725ae77Skettenis completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
3318b725ae77Skettenis 			  char *text, char *word)
3319e93f7393Sniklas {
3320e93f7393Sniklas   int newsize;
3321e93f7393Sniklas   int i;
3322e93f7393Sniklas 
3323e93f7393Sniklas   /* clip symbols that cannot match */
3324e93f7393Sniklas 
3325e93f7393Sniklas   if (strncmp (symname, sym_text, sym_text_len) != 0)
3326e93f7393Sniklas     {
3327e93f7393Sniklas       return;
3328e93f7393Sniklas     }
3329e93f7393Sniklas 
3330e93f7393Sniklas   /* We have a match for a completion, so add SYMNAME to the current list
3331e93f7393Sniklas      of matches. Note that the name is moved to freshly malloc'd space. */
3332e93f7393Sniklas 
3333e93f7393Sniklas   {
3334e93f7393Sniklas     char *new;
3335e93f7393Sniklas     if (word == sym_text)
3336e93f7393Sniklas       {
3337e93f7393Sniklas 	new = xmalloc (strlen (symname) + 5);
3338e93f7393Sniklas 	strcpy (new, symname);
3339e93f7393Sniklas       }
3340e93f7393Sniklas     else if (word > sym_text)
3341e93f7393Sniklas       {
3342e93f7393Sniklas 	/* Return some portion of symname.  */
3343e93f7393Sniklas 	new = xmalloc (strlen (symname) + 5);
3344e93f7393Sniklas 	strcpy (new, symname + (word - sym_text));
3345e93f7393Sniklas       }
3346e93f7393Sniklas     else
3347e93f7393Sniklas       {
3348e93f7393Sniklas 	/* Return some of SYM_TEXT plus symname.  */
3349e93f7393Sniklas 	new = xmalloc (strlen (symname) + (sym_text - word) + 5);
3350e93f7393Sniklas 	strncpy (new, word, sym_text - word);
3351e93f7393Sniklas 	new[sym_text - word] = '\0';
3352e93f7393Sniklas 	strcat (new, symname);
3353e93f7393Sniklas       }
3354e93f7393Sniklas 
3355e93f7393Sniklas     if (return_val_index + 3 > return_val_size)
3356e93f7393Sniklas       {
3357e93f7393Sniklas 	newsize = (return_val_size *= 2) * sizeof (char *);
3358e93f7393Sniklas 	return_val = (char **) xrealloc ((char *) return_val, newsize);
3359e93f7393Sniklas       }
3360e93f7393Sniklas     return_val[return_val_index++] = new;
3361e93f7393Sniklas     return_val[return_val_index] = NULL;
3362e93f7393Sniklas   }
3363e93f7393Sniklas }
3364e93f7393Sniklas 
3365b725ae77Skettenis /* ObjC: In case we are completing on a selector, look as the msymbol
3366b725ae77Skettenis    again and feed all the selectors into the mill.  */
3367b725ae77Skettenis 
3368b725ae77Skettenis static void
completion_list_objc_symbol(struct minimal_symbol * msymbol,char * sym_text,int sym_text_len,char * text,char * word)3369b725ae77Skettenis completion_list_objc_symbol (struct minimal_symbol *msymbol, char *sym_text,
3370b725ae77Skettenis 			     int sym_text_len, char *text, char *word)
3371b725ae77Skettenis {
3372b725ae77Skettenis   static char *tmp = NULL;
3373b725ae77Skettenis   static unsigned int tmplen = 0;
3374b725ae77Skettenis 
3375b725ae77Skettenis   char *method, *category, *selector;
3376b725ae77Skettenis   char *tmp2 = NULL;
3377b725ae77Skettenis 
3378b725ae77Skettenis   method = SYMBOL_NATURAL_NAME (msymbol);
3379b725ae77Skettenis 
3380b725ae77Skettenis   /* Is it a method?  */
3381b725ae77Skettenis   if ((method[0] != '-') && (method[0] != '+'))
3382b725ae77Skettenis     return;
3383b725ae77Skettenis 
3384b725ae77Skettenis   if (sym_text[0] == '[')
3385b725ae77Skettenis     /* Complete on shortened method method.  */
3386b725ae77Skettenis     completion_list_add_name (method + 1, sym_text, sym_text_len, text, word);
3387b725ae77Skettenis 
3388b725ae77Skettenis   while ((strlen (method) + 1) >= tmplen)
3389b725ae77Skettenis     {
3390b725ae77Skettenis       if (tmplen == 0)
3391b725ae77Skettenis 	tmplen = 1024;
3392b725ae77Skettenis       else
3393b725ae77Skettenis 	tmplen *= 2;
3394b725ae77Skettenis       tmp = xrealloc (tmp, tmplen);
3395b725ae77Skettenis     }
3396b725ae77Skettenis   selector = strchr (method, ' ');
3397b725ae77Skettenis   if (selector != NULL)
3398b725ae77Skettenis     selector++;
3399b725ae77Skettenis 
3400b725ae77Skettenis   category = strchr (method, '(');
3401b725ae77Skettenis 
3402b725ae77Skettenis   if ((category != NULL) && (selector != NULL))
3403b725ae77Skettenis     {
3404b725ae77Skettenis       memcpy (tmp, method, (category - method));
3405b725ae77Skettenis       tmp[category - method] = ' ';
3406b725ae77Skettenis       memcpy (tmp + (category - method) + 1, selector, strlen (selector) + 1);
3407b725ae77Skettenis       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
3408b725ae77Skettenis       if (sym_text[0] == '[')
3409b725ae77Skettenis 	completion_list_add_name (tmp + 1, sym_text, sym_text_len, text, word);
3410b725ae77Skettenis     }
3411b725ae77Skettenis 
3412b725ae77Skettenis   if (selector != NULL)
3413b725ae77Skettenis     {
3414b725ae77Skettenis       /* Complete on selector only.  */
3415b725ae77Skettenis       strcpy (tmp, selector);
3416b725ae77Skettenis       tmp2 = strchr (tmp, ']');
3417b725ae77Skettenis       if (tmp2 != NULL)
3418b725ae77Skettenis 	*tmp2 = '\0';
3419b725ae77Skettenis 
3420b725ae77Skettenis       completion_list_add_name (tmp, sym_text, sym_text_len, text, word);
3421b725ae77Skettenis     }
3422b725ae77Skettenis }
3423b725ae77Skettenis 
3424b725ae77Skettenis /* Break the non-quoted text based on the characters which are in
3425b725ae77Skettenis    symbols. FIXME: This should probably be language-specific. */
3426b725ae77Skettenis 
3427b725ae77Skettenis static char *
language_search_unquoted_string(char * text,char * p)3428b725ae77Skettenis language_search_unquoted_string (char *text, char *p)
3429b725ae77Skettenis {
3430b725ae77Skettenis   for (; p > text; --p)
3431b725ae77Skettenis     {
3432b725ae77Skettenis       if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3433b725ae77Skettenis 	continue;
3434b725ae77Skettenis       else
3435b725ae77Skettenis 	{
3436b725ae77Skettenis 	  if ((current_language->la_language == language_objc))
3437b725ae77Skettenis 	    {
3438b725ae77Skettenis 	      if (p[-1] == ':')     /* might be part of a method name */
3439b725ae77Skettenis 		continue;
3440b725ae77Skettenis 	      else if (p[-1] == '[' && (p[-2] == '-' || p[-2] == '+'))
3441b725ae77Skettenis 		p -= 2;             /* beginning of a method name */
3442b725ae77Skettenis 	      else if (p[-1] == ' ' || p[-1] == '(' || p[-1] == ')')
3443b725ae77Skettenis 		{                   /* might be part of a method name */
3444b725ae77Skettenis 		  char *t = p;
3445b725ae77Skettenis 
3446b725ae77Skettenis 		  /* Seeing a ' ' or a '(' is not conclusive evidence
3447b725ae77Skettenis 		     that we are in the middle of a method name.  However,
3448b725ae77Skettenis 		     finding "-[" or "+[" should be pretty un-ambiguous.
3449b725ae77Skettenis 		     Unfortunately we have to find it now to decide.  */
3450b725ae77Skettenis 
3451b725ae77Skettenis 		  while (t > text)
3452b725ae77Skettenis 		    if (isalnum (t[-1]) || t[-1] == '_' ||
3453b725ae77Skettenis 			t[-1] == ' '    || t[-1] == ':' ||
3454b725ae77Skettenis 			t[-1] == '('    || t[-1] == ')')
3455b725ae77Skettenis 		      --t;
3456b725ae77Skettenis 		    else
3457b725ae77Skettenis 		      break;
3458b725ae77Skettenis 
3459b725ae77Skettenis 		  if (t[-1] == '[' && (t[-2] == '-' || t[-2] == '+'))
3460b725ae77Skettenis 		    p = t - 2;      /* method name detected */
3461b725ae77Skettenis 		  /* else we leave with p unchanged */
3462b725ae77Skettenis 		}
3463b725ae77Skettenis 	    }
3464b725ae77Skettenis 	  break;
3465b725ae77Skettenis 	}
3466b725ae77Skettenis     }
3467b725ae77Skettenis   return p;
3468b725ae77Skettenis }
3469b725ae77Skettenis 
3470b725ae77Skettenis 
3471b725ae77Skettenis /* Return a NULL terminated array of all symbols (regardless of class)
3472b725ae77Skettenis    which begin by matching TEXT.  If the answer is no symbols, then
3473b725ae77Skettenis    the return value is an array which contains only a NULL pointer.
3474e93f7393Sniklas 
3475e93f7393Sniklas    Problem: All of the symbols have to be copied because readline frees them.
3476e93f7393Sniklas    I'm not going to worry about this; hopefully there won't be that many.  */
3477e93f7393Sniklas 
3478e93f7393Sniklas char **
make_symbol_completion_list(char * text,char * word)3479b725ae77Skettenis make_symbol_completion_list (char *text, char *word)
3480e93f7393Sniklas {
3481b725ae77Skettenis   struct symbol *sym;
3482b725ae77Skettenis   struct symtab *s;
3483b725ae77Skettenis   struct partial_symtab *ps;
3484b725ae77Skettenis   struct minimal_symbol *msymbol;
3485b725ae77Skettenis   struct objfile *objfile;
3486b725ae77Skettenis   struct block *b, *surrounding_static_block = 0;
3487b725ae77Skettenis   struct dict_iterator iter;
3488b725ae77Skettenis   int j;
3489e93f7393Sniklas   struct partial_symbol **psym;
3490e93f7393Sniklas   /* The symbol we are completing on.  Points in same buffer as text.  */
3491e93f7393Sniklas   char *sym_text;
3492e93f7393Sniklas   /* Length of sym_text.  */
3493e93f7393Sniklas   int sym_text_len;
3494e93f7393Sniklas 
3495e93f7393Sniklas   /* Now look for the symbol we are supposed to complete on.
3496e93f7393Sniklas      FIXME: This should be language-specific.  */
3497e93f7393Sniklas   {
3498e93f7393Sniklas     char *p;
3499e93f7393Sniklas     char quote_found;
3500e93f7393Sniklas     char *quote_pos = NULL;
3501e93f7393Sniklas 
3502e93f7393Sniklas     /* First see if this is a quoted string.  */
3503e93f7393Sniklas     quote_found = '\0';
3504e93f7393Sniklas     for (p = text; *p != '\0'; ++p)
3505e93f7393Sniklas       {
3506e93f7393Sniklas 	if (quote_found != '\0')
3507e93f7393Sniklas 	  {
3508e93f7393Sniklas 	    if (*p == quote_found)
3509e93f7393Sniklas 	      /* Found close quote.  */
3510e93f7393Sniklas 	      quote_found = '\0';
3511e93f7393Sniklas 	    else if (*p == '\\' && p[1] == quote_found)
3512e93f7393Sniklas 	      /* A backslash followed by the quote character
3513e93f7393Sniklas 	         doesn't end the string.  */
3514e93f7393Sniklas 	      ++p;
3515e93f7393Sniklas 	  }
3516e93f7393Sniklas 	else if (*p == '\'' || *p == '"')
3517e93f7393Sniklas 	  {
3518e93f7393Sniklas 	    quote_found = *p;
3519e93f7393Sniklas 	    quote_pos = p;
3520e93f7393Sniklas 	  }
3521e93f7393Sniklas       }
3522e93f7393Sniklas     if (quote_found == '\'')
3523e93f7393Sniklas       /* A string within single quotes can be a symbol, so complete on it.  */
3524e93f7393Sniklas       sym_text = quote_pos + 1;
3525e93f7393Sniklas     else if (quote_found == '"')
3526e93f7393Sniklas       /* A double-quoted string is never a symbol, nor does it make sense
3527e93f7393Sniklas          to complete it any other way.  */
3528b725ae77Skettenis       {
3529b725ae77Skettenis 	return_val = (char **) xmalloc (sizeof (char *));
3530b725ae77Skettenis 	return_val[0] = NULL;
3531b725ae77Skettenis 	return return_val;
3532b725ae77Skettenis       }
3533e93f7393Sniklas     else
3534e93f7393Sniklas       {
3535e93f7393Sniklas 	/* It is not a quoted string.  Break it based on the characters
3536e93f7393Sniklas 	   which are in symbols.  */
3537e93f7393Sniklas 	while (p > text)
3538e93f7393Sniklas 	  {
3539e93f7393Sniklas 	    if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3540e93f7393Sniklas 	      --p;
3541e93f7393Sniklas 	    else
3542e93f7393Sniklas 	      break;
3543e93f7393Sniklas 	  }
3544e93f7393Sniklas 	sym_text = p;
3545e93f7393Sniklas       }
3546e93f7393Sniklas   }
3547e93f7393Sniklas 
3548e93f7393Sniklas   sym_text_len = strlen (sym_text);
3549e93f7393Sniklas 
3550e93f7393Sniklas   return_val_size = 100;
3551e93f7393Sniklas   return_val_index = 0;
3552e93f7393Sniklas   return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3553e93f7393Sniklas   return_val[0] = NULL;
3554e93f7393Sniklas 
3555e93f7393Sniklas   /* Look through the partial symtabs for all symbols which begin
3556e93f7393Sniklas      by matching SYM_TEXT.  Add each one that you find to the list.  */
3557e93f7393Sniklas 
3558e93f7393Sniklas   ALL_PSYMTABS (objfile, ps)
3559e93f7393Sniklas   {
3560e93f7393Sniklas     /* If the psymtab's been read in we'll get it when we search
3561e93f7393Sniklas        through the blockvector.  */
3562b725ae77Skettenis     if (ps->readin)
3563b725ae77Skettenis       continue;
3564e93f7393Sniklas 
3565e93f7393Sniklas     for (psym = objfile->global_psymbols.list + ps->globals_offset;
3566e93f7393Sniklas 	 psym < (objfile->global_psymbols.list + ps->globals_offset
3567e93f7393Sniklas 		 + ps->n_global_syms);
3568e93f7393Sniklas 	 psym++)
3569e93f7393Sniklas       {
3570e93f7393Sniklas 	/* If interrupted, then quit. */
3571e93f7393Sniklas 	QUIT;
3572e93f7393Sniklas 	COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3573e93f7393Sniklas       }
3574e93f7393Sniklas 
3575e93f7393Sniklas     for (psym = objfile->static_psymbols.list + ps->statics_offset;
3576e93f7393Sniklas 	 psym < (objfile->static_psymbols.list + ps->statics_offset
3577e93f7393Sniklas 		 + ps->n_static_syms);
3578e93f7393Sniklas 	 psym++)
3579e93f7393Sniklas       {
3580e93f7393Sniklas 	QUIT;
3581e93f7393Sniklas 	COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3582e93f7393Sniklas       }
3583e93f7393Sniklas   }
3584e93f7393Sniklas 
3585e93f7393Sniklas   /* At this point scan through the misc symbol vectors and add each
3586e93f7393Sniklas      symbol you find to the list.  Eventually we want to ignore
3587e93f7393Sniklas      anything that isn't a text symbol (everything else will be
3588e93f7393Sniklas      handled by the psymtab code above).  */
3589e93f7393Sniklas 
3590e93f7393Sniklas   ALL_MSYMBOLS (objfile, msymbol)
3591e93f7393Sniklas   {
3592e93f7393Sniklas     QUIT;
3593e93f7393Sniklas     COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3594b725ae77Skettenis 
3595b725ae77Skettenis     completion_list_objc_symbol (msymbol, sym_text, sym_text_len, text, word);
3596e93f7393Sniklas   }
3597e93f7393Sniklas 
3598e93f7393Sniklas   /* Search upwards from currently selected frame (so that we can
3599e93f7393Sniklas      complete on local vars.  */
3600e93f7393Sniklas 
3601b725ae77Skettenis   for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
3602e93f7393Sniklas     {
3603e93f7393Sniklas       if (!BLOCK_SUPERBLOCK (b))
3604e93f7393Sniklas 	{
3605e93f7393Sniklas 	  surrounding_static_block = b;		/* For elmin of dups */
3606e93f7393Sniklas 	}
3607e93f7393Sniklas 
3608e93f7393Sniklas       /* Also catch fields of types defined in this places which match our
3609e93f7393Sniklas          text string.  Only complete on types visible from current context. */
3610e93f7393Sniklas 
3611b725ae77Skettenis       ALL_BLOCK_SYMBOLS (b, iter, sym)
3612e93f7393Sniklas 	{
3613b725ae77Skettenis 	  QUIT;
3614e93f7393Sniklas 	  COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3615e93f7393Sniklas 	  if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3616e93f7393Sniklas 	    {
3617e93f7393Sniklas 	      struct type *t = SYMBOL_TYPE (sym);
3618e93f7393Sniklas 	      enum type_code c = TYPE_CODE (t);
3619e93f7393Sniklas 
3620e93f7393Sniklas 	      if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
3621e93f7393Sniklas 		{
3622e93f7393Sniklas 		  for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
3623e93f7393Sniklas 		    {
3624e93f7393Sniklas 		      if (TYPE_FIELD_NAME (t, j))
3625e93f7393Sniklas 			{
3626e93f7393Sniklas 			  completion_list_add_name (TYPE_FIELD_NAME (t, j),
3627e93f7393Sniklas 					sym_text, sym_text_len, text, word);
3628e93f7393Sniklas 			}
3629e93f7393Sniklas 		    }
3630e93f7393Sniklas 		}
3631e93f7393Sniklas 	    }
3632e93f7393Sniklas 	}
3633e93f7393Sniklas     }
3634e93f7393Sniklas 
3635e93f7393Sniklas   /* Go through the symtabs and check the externs and statics for
3636e93f7393Sniklas      symbols which match.  */
3637e93f7393Sniklas 
3638e93f7393Sniklas   ALL_SYMTABS (objfile, s)
3639e93f7393Sniklas   {
3640e93f7393Sniklas     QUIT;
3641e93f7393Sniklas     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3642b725ae77Skettenis     ALL_BLOCK_SYMBOLS (b, iter, sym)
3643e93f7393Sniklas       {
3644e93f7393Sniklas 	COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3645e93f7393Sniklas       }
3646e93f7393Sniklas   }
3647e93f7393Sniklas 
3648e93f7393Sniklas   ALL_SYMTABS (objfile, s)
3649e93f7393Sniklas   {
3650e93f7393Sniklas     QUIT;
3651e93f7393Sniklas     b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3652e93f7393Sniklas     /* Don't do this block twice.  */
3653b725ae77Skettenis     if (b == surrounding_static_block)
3654b725ae77Skettenis       continue;
3655b725ae77Skettenis     ALL_BLOCK_SYMBOLS (b, iter, sym)
3656e93f7393Sniklas       {
3657e93f7393Sniklas 	COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3658e93f7393Sniklas       }
3659e93f7393Sniklas   }
3660e93f7393Sniklas 
3661e93f7393Sniklas   return (return_val);
3662e93f7393Sniklas }
3663e93f7393Sniklas 
3664b725ae77Skettenis /* Like make_symbol_completion_list, but returns a list of symbols
3665b725ae77Skettenis    defined in a source file FILE.  */
3666b725ae77Skettenis 
3667b725ae77Skettenis char **
make_file_symbol_completion_list(char * text,char * word,char * srcfile)3668b725ae77Skettenis make_file_symbol_completion_list (char *text, char *word, char *srcfile)
3669b725ae77Skettenis {
3670b725ae77Skettenis   struct symbol *sym;
3671b725ae77Skettenis   struct symtab *s;
3672b725ae77Skettenis   struct block *b;
3673b725ae77Skettenis   struct dict_iterator iter;
3674b725ae77Skettenis   /* The symbol we are completing on.  Points in same buffer as text.  */
3675b725ae77Skettenis   char *sym_text;
3676b725ae77Skettenis   /* Length of sym_text.  */
3677b725ae77Skettenis   int sym_text_len;
3678b725ae77Skettenis 
3679b725ae77Skettenis   /* Now look for the symbol we are supposed to complete on.
3680b725ae77Skettenis      FIXME: This should be language-specific.  */
3681b725ae77Skettenis   {
3682b725ae77Skettenis     char *p;
3683b725ae77Skettenis     char quote_found;
3684b725ae77Skettenis     char *quote_pos = NULL;
3685b725ae77Skettenis 
3686b725ae77Skettenis     /* First see if this is a quoted string.  */
3687b725ae77Skettenis     quote_found = '\0';
3688b725ae77Skettenis     for (p = text; *p != '\0'; ++p)
3689b725ae77Skettenis       {
3690b725ae77Skettenis 	if (quote_found != '\0')
3691b725ae77Skettenis 	  {
3692b725ae77Skettenis 	    if (*p == quote_found)
3693b725ae77Skettenis 	      /* Found close quote.  */
3694b725ae77Skettenis 	      quote_found = '\0';
3695b725ae77Skettenis 	    else if (*p == '\\' && p[1] == quote_found)
3696b725ae77Skettenis 	      /* A backslash followed by the quote character
3697b725ae77Skettenis 	         doesn't end the string.  */
3698b725ae77Skettenis 	      ++p;
3699b725ae77Skettenis 	  }
3700b725ae77Skettenis 	else if (*p == '\'' || *p == '"')
3701b725ae77Skettenis 	  {
3702b725ae77Skettenis 	    quote_found = *p;
3703b725ae77Skettenis 	    quote_pos = p;
3704b725ae77Skettenis 	  }
3705b725ae77Skettenis       }
3706b725ae77Skettenis     if (quote_found == '\'')
3707b725ae77Skettenis       /* A string within single quotes can be a symbol, so complete on it.  */
3708b725ae77Skettenis       sym_text = quote_pos + 1;
3709b725ae77Skettenis     else if (quote_found == '"')
3710b725ae77Skettenis       /* A double-quoted string is never a symbol, nor does it make sense
3711b725ae77Skettenis          to complete it any other way.  */
3712b725ae77Skettenis       {
3713b725ae77Skettenis 	return_val = (char **) xmalloc (sizeof (char *));
3714b725ae77Skettenis 	return_val[0] = NULL;
3715b725ae77Skettenis 	return return_val;
3716b725ae77Skettenis       }
3717b725ae77Skettenis     else
3718b725ae77Skettenis       {
3719b725ae77Skettenis 	/* Not a quoted string.  */
3720b725ae77Skettenis 	sym_text = language_search_unquoted_string (text, p);
3721b725ae77Skettenis       }
3722b725ae77Skettenis   }
3723b725ae77Skettenis 
3724b725ae77Skettenis   sym_text_len = strlen (sym_text);
3725b725ae77Skettenis 
3726b725ae77Skettenis   return_val_size = 10;
3727b725ae77Skettenis   return_val_index = 0;
3728b725ae77Skettenis   return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3729b725ae77Skettenis   return_val[0] = NULL;
3730b725ae77Skettenis 
3731b725ae77Skettenis   /* Find the symtab for SRCFILE (this loads it if it was not yet read
3732b725ae77Skettenis      in).  */
3733b725ae77Skettenis   s = lookup_symtab (srcfile);
3734b725ae77Skettenis   if (s == NULL)
3735b725ae77Skettenis     {
3736b725ae77Skettenis       /* Maybe they typed the file with leading directories, while the
3737b725ae77Skettenis 	 symbol tables record only its basename.  */
3738b725ae77Skettenis       const char *tail = lbasename (srcfile);
3739b725ae77Skettenis 
3740b725ae77Skettenis       if (tail > srcfile)
3741b725ae77Skettenis 	s = lookup_symtab (tail);
3742b725ae77Skettenis     }
3743b725ae77Skettenis 
3744b725ae77Skettenis   /* If we have no symtab for that file, return an empty list.  */
3745b725ae77Skettenis   if (s == NULL)
3746b725ae77Skettenis     return (return_val);
3747b725ae77Skettenis 
3748b725ae77Skettenis   /* Go through this symtab and check the externs and statics for
3749b725ae77Skettenis      symbols which match.  */
3750b725ae77Skettenis 
3751b725ae77Skettenis   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3752b725ae77Skettenis   ALL_BLOCK_SYMBOLS (b, iter, sym)
3753b725ae77Skettenis     {
3754b725ae77Skettenis       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3755b725ae77Skettenis     }
3756b725ae77Skettenis 
3757b725ae77Skettenis   b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3758b725ae77Skettenis   ALL_BLOCK_SYMBOLS (b, iter, sym)
3759b725ae77Skettenis     {
3760b725ae77Skettenis       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3761b725ae77Skettenis     }
3762b725ae77Skettenis 
3763b725ae77Skettenis   return (return_val);
3764b725ae77Skettenis }
3765b725ae77Skettenis 
3766b725ae77Skettenis /* A helper function for make_source_files_completion_list.  It adds
3767b725ae77Skettenis    another file name to a list of possible completions, growing the
3768b725ae77Skettenis    list as necessary.  */
3769b725ae77Skettenis 
3770b725ae77Skettenis static void
add_filename_to_list(const char * fname,char * text,char * word,char *** list,int * list_used,int * list_alloced)3771b725ae77Skettenis add_filename_to_list (const char *fname, char *text, char *word,
3772b725ae77Skettenis 		      char ***list, int *list_used, int *list_alloced)
3773b725ae77Skettenis {
3774b725ae77Skettenis   char *new;
3775b725ae77Skettenis   size_t fnlen = strlen (fname);
3776b725ae77Skettenis 
3777b725ae77Skettenis   if (*list_used + 1 >= *list_alloced)
3778b725ae77Skettenis     {
3779b725ae77Skettenis       *list_alloced *= 2;
3780b725ae77Skettenis       *list = (char **) xrealloc ((char *) *list,
3781b725ae77Skettenis 				  *list_alloced * sizeof (char *));
3782b725ae77Skettenis     }
3783b725ae77Skettenis 
3784b725ae77Skettenis   if (word == text)
3785b725ae77Skettenis     {
3786b725ae77Skettenis       /* Return exactly fname.  */
3787b725ae77Skettenis       new = xmalloc (fnlen + 5);
3788b725ae77Skettenis       strcpy (new, fname);
3789b725ae77Skettenis     }
3790b725ae77Skettenis   else if (word > text)
3791b725ae77Skettenis     {
3792b725ae77Skettenis       /* Return some portion of fname.  */
3793b725ae77Skettenis       new = xmalloc (fnlen + 5);
3794b725ae77Skettenis       strcpy (new, fname + (word - text));
3795b725ae77Skettenis     }
3796b725ae77Skettenis   else
3797b725ae77Skettenis     {
3798b725ae77Skettenis       /* Return some of TEXT plus fname.  */
3799b725ae77Skettenis       new = xmalloc (fnlen + (text - word) + 5);
3800b725ae77Skettenis       strncpy (new, word, text - word);
3801b725ae77Skettenis       new[text - word] = '\0';
3802b725ae77Skettenis       strcat (new, fname);
3803b725ae77Skettenis     }
3804b725ae77Skettenis   (*list)[*list_used] = new;
3805b725ae77Skettenis   (*list)[++*list_used] = NULL;
3806b725ae77Skettenis }
3807b725ae77Skettenis 
3808b725ae77Skettenis static int
not_interesting_fname(const char * fname)3809b725ae77Skettenis not_interesting_fname (const char *fname)
3810b725ae77Skettenis {
3811b725ae77Skettenis   static const char *illegal_aliens[] = {
3812b725ae77Skettenis     "_globals_",	/* inserted by coff_symtab_read */
3813b725ae77Skettenis     NULL
3814b725ae77Skettenis   };
3815b725ae77Skettenis   int i;
3816b725ae77Skettenis 
3817b725ae77Skettenis   for (i = 0; illegal_aliens[i]; i++)
3818b725ae77Skettenis     {
3819b725ae77Skettenis       if (strcmp (fname, illegal_aliens[i]) == 0)
3820b725ae77Skettenis 	return 1;
3821b725ae77Skettenis     }
3822b725ae77Skettenis   return 0;
3823b725ae77Skettenis }
3824b725ae77Skettenis 
3825b725ae77Skettenis /* Return a NULL terminated array of all source files whose names
3826b725ae77Skettenis    begin with matching TEXT.  The file names are looked up in the
3827b725ae77Skettenis    symbol tables of this program.  If the answer is no matchess, then
3828b725ae77Skettenis    the return value is an array which contains only a NULL pointer.  */
3829b725ae77Skettenis 
3830b725ae77Skettenis char **
make_source_files_completion_list(char * text,char * word)3831b725ae77Skettenis make_source_files_completion_list (char *text, char *word)
3832b725ae77Skettenis {
3833b725ae77Skettenis   struct symtab *s;
3834b725ae77Skettenis   struct partial_symtab *ps;
3835b725ae77Skettenis   struct objfile *objfile;
3836b725ae77Skettenis   int first = 1;
3837b725ae77Skettenis   int list_alloced = 1;
3838b725ae77Skettenis   int list_used = 0;
3839b725ae77Skettenis   size_t text_len = strlen (text);
3840b725ae77Skettenis   char **list = (char **) xmalloc (list_alloced * sizeof (char *));
3841b725ae77Skettenis   const char *base_name;
3842b725ae77Skettenis 
3843b725ae77Skettenis   list[0] = NULL;
3844b725ae77Skettenis 
3845b725ae77Skettenis   if (!have_full_symbols () && !have_partial_symbols ())
3846b725ae77Skettenis     return list;
3847b725ae77Skettenis 
3848b725ae77Skettenis   ALL_SYMTABS (objfile, s)
3849b725ae77Skettenis     {
3850b725ae77Skettenis       if (not_interesting_fname (s->filename))
3851b725ae77Skettenis 	continue;
3852b725ae77Skettenis       if (!filename_seen (s->filename, 1, &first)
3853b725ae77Skettenis #if HAVE_DOS_BASED_FILE_SYSTEM
3854b725ae77Skettenis 	  && strncasecmp (s->filename, text, text_len) == 0
3855b725ae77Skettenis #else
3856b725ae77Skettenis 	  && strncmp (s->filename, text, text_len) == 0
3857b725ae77Skettenis #endif
3858b725ae77Skettenis 	  )
3859b725ae77Skettenis 	{
3860b725ae77Skettenis 	  /* This file matches for a completion; add it to the current
3861b725ae77Skettenis 	     list of matches.  */
3862b725ae77Skettenis 	  add_filename_to_list (s->filename, text, word,
3863b725ae77Skettenis 				&list, &list_used, &list_alloced);
3864b725ae77Skettenis 	}
3865b725ae77Skettenis       else
3866b725ae77Skettenis 	{
3867b725ae77Skettenis 	  /* NOTE: We allow the user to type a base name when the
3868b725ae77Skettenis 	     debug info records leading directories, but not the other
3869b725ae77Skettenis 	     way around.  This is what subroutines of breakpoint
3870b725ae77Skettenis 	     command do when they parse file names.  */
3871b725ae77Skettenis 	  base_name = lbasename (s->filename);
3872b725ae77Skettenis 	  if (base_name != s->filename
3873b725ae77Skettenis 	      && !filename_seen (base_name, 1, &first)
3874b725ae77Skettenis #if HAVE_DOS_BASED_FILE_SYSTEM
3875b725ae77Skettenis 	      && strncasecmp (base_name, text, text_len) == 0
3876b725ae77Skettenis #else
3877b725ae77Skettenis 	      && strncmp (base_name, text, text_len) == 0
3878b725ae77Skettenis #endif
3879b725ae77Skettenis 	      )
3880b725ae77Skettenis 	    add_filename_to_list (base_name, text, word,
3881b725ae77Skettenis 				  &list, &list_used, &list_alloced);
3882b725ae77Skettenis 	}
3883b725ae77Skettenis     }
3884b725ae77Skettenis 
3885b725ae77Skettenis   ALL_PSYMTABS (objfile, ps)
3886b725ae77Skettenis     {
3887b725ae77Skettenis       if (not_interesting_fname (ps->filename))
3888b725ae77Skettenis 	continue;
3889b725ae77Skettenis       if (!ps->readin)
3890b725ae77Skettenis 	{
3891b725ae77Skettenis 	  if (!filename_seen (ps->filename, 1, &first)
3892b725ae77Skettenis #if HAVE_DOS_BASED_FILE_SYSTEM
3893b725ae77Skettenis 	      && strncasecmp (ps->filename, text, text_len) == 0
3894b725ae77Skettenis #else
3895b725ae77Skettenis 	      && strncmp (ps->filename, text, text_len) == 0
3896b725ae77Skettenis #endif
3897b725ae77Skettenis 	      )
3898b725ae77Skettenis 	    {
3899b725ae77Skettenis 	      /* This file matches for a completion; add it to the
3900b725ae77Skettenis 		 current list of matches.  */
3901b725ae77Skettenis 	      add_filename_to_list (ps->filename, text, word,
3902b725ae77Skettenis 				    &list, &list_used, &list_alloced);
3903b725ae77Skettenis 
3904b725ae77Skettenis 	    }
3905b725ae77Skettenis 	  else
3906b725ae77Skettenis 	    {
3907b725ae77Skettenis 	      base_name = lbasename (ps->filename);
3908b725ae77Skettenis 	      if (base_name != ps->filename
3909b725ae77Skettenis 		  && !filename_seen (base_name, 1, &first)
3910b725ae77Skettenis #if HAVE_DOS_BASED_FILE_SYSTEM
3911b725ae77Skettenis 		  && strncasecmp (base_name, text, text_len) == 0
3912b725ae77Skettenis #else
3913b725ae77Skettenis 		  && strncmp (base_name, text, text_len) == 0
3914b725ae77Skettenis #endif
3915b725ae77Skettenis 		  )
3916b725ae77Skettenis 		add_filename_to_list (base_name, text, word,
3917b725ae77Skettenis 				      &list, &list_used, &list_alloced);
3918b725ae77Skettenis 	    }
3919b725ae77Skettenis 	}
3920b725ae77Skettenis     }
3921b725ae77Skettenis 
3922b725ae77Skettenis   return list;
3923b725ae77Skettenis }
3924b725ae77Skettenis 
3925e93f7393Sniklas /* Determine if PC is in the prologue of a function.  The prologue is the area
3926e93f7393Sniklas    between the first instruction of a function, and the first executable line.
3927e93f7393Sniklas    Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3928e93f7393Sniklas 
3929b725ae77Skettenis    If non-zero, func_start is where we think the prologue starts, possibly
3930e93f7393Sniklas    by previous examination of symbol table information.
3931e93f7393Sniklas  */
3932e93f7393Sniklas 
3933e93f7393Sniklas int
in_prologue(CORE_ADDR pc,CORE_ADDR func_start)3934b725ae77Skettenis in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
3935e93f7393Sniklas {
3936e93f7393Sniklas   struct symtab_and_line sal;
3937e93f7393Sniklas   CORE_ADDR func_addr, func_end;
3938e93f7393Sniklas 
3939b725ae77Skettenis   /* We have several sources of information we can consult to figure
3940b725ae77Skettenis      this out.
3941b725ae77Skettenis      - Compilers usually emit line number info that marks the prologue
3942b725ae77Skettenis        as its own "source line".  So the ending address of that "line"
3943b725ae77Skettenis        is the end of the prologue.  If available, this is the most
3944b725ae77Skettenis        reliable method.
3945b725ae77Skettenis      - The minimal symbols and partial symbols, which can usually tell
3946b725ae77Skettenis        us the starting and ending addresses of a function.
3947b725ae77Skettenis      - If we know the function's start address, we can call the
3948b725ae77Skettenis        architecture-defined SKIP_PROLOGUE function to analyze the
3949b725ae77Skettenis        instruction stream and guess where the prologue ends.
3950b725ae77Skettenis      - Our `func_start' argument; if non-zero, this is the caller's
3951b725ae77Skettenis        best guess as to the function's entry point.  At the time of
3952b725ae77Skettenis        this writing, handle_inferior_event doesn't get this right, so
3953b725ae77Skettenis        it should be our last resort.  */
3954b725ae77Skettenis 
3955b725ae77Skettenis   /* Consult the partial symbol table, to find which function
3956b725ae77Skettenis      the PC is in.  */
3957e93f7393Sniklas   if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3958b725ae77Skettenis     {
3959b725ae77Skettenis       CORE_ADDR prologue_end;
3960e93f7393Sniklas 
3961b725ae77Skettenis       /* We don't even have minsym information, so fall back to using
3962b725ae77Skettenis          func_start, if given.  */
3963b725ae77Skettenis       if (! func_start)
3964b725ae77Skettenis 	return 1;		/* We *might* be in a prologue.  */
3965e93f7393Sniklas 
3966b725ae77Skettenis       prologue_end = SKIP_PROLOGUE (func_start);
3967e93f7393Sniklas 
3968b725ae77Skettenis       return func_start <= pc && pc < prologue_end;
3969e93f7393Sniklas     }
3970e93f7393Sniklas 
3971b725ae77Skettenis   /* If we have line number information for the function, that's
3972b725ae77Skettenis      usually pretty reliable.  */
3973b725ae77Skettenis   sal = find_pc_line (func_addr, 0);
3974b725ae77Skettenis 
3975b725ae77Skettenis   /* Now sal describes the source line at the function's entry point,
3976b725ae77Skettenis      which (by convention) is the prologue.  The end of that "line",
3977b725ae77Skettenis      sal.end, is the end of the prologue.
3978b725ae77Skettenis 
3979b725ae77Skettenis      Note that, for functions whose source code is all on a single
3980b725ae77Skettenis      line, the line number information doesn't always end up this way.
3981b725ae77Skettenis      So we must verify that our purported end-of-prologue address is
3982b725ae77Skettenis      *within* the function, not at its start or end.  */
3983b725ae77Skettenis   if (sal.line == 0
3984b725ae77Skettenis       || sal.end <= func_addr
3985b725ae77Skettenis       || func_end <= sal.end)
3986b725ae77Skettenis     {
3987b725ae77Skettenis       /* We don't have any good line number info, so use the minsym
3988b725ae77Skettenis 	 information, together with the architecture-specific prologue
3989b725ae77Skettenis 	 scanning code.  */
3990b725ae77Skettenis       CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
3991b725ae77Skettenis 
3992b725ae77Skettenis       return func_addr <= pc && pc < prologue_end;
3993b725ae77Skettenis     }
3994b725ae77Skettenis 
3995b725ae77Skettenis   /* We have line number info, and it looks good.  */
3996b725ae77Skettenis   return func_addr <= pc && pc < sal.end;
3997b725ae77Skettenis }
3998b725ae77Skettenis 
3999b725ae77Skettenis /* Given PC at the function's start address, attempt to find the
4000b725ae77Skettenis    prologue end using SAL information.  Return zero if the skip fails.
4001b725ae77Skettenis 
4002b725ae77Skettenis    A non-optimized prologue traditionally has one SAL for the function
4003b725ae77Skettenis    and a second for the function body.  A single line function has
4004b725ae77Skettenis    them both pointing at the same line.
4005b725ae77Skettenis 
4006b725ae77Skettenis    An optimized prologue is similar but the prologue may contain
4007b725ae77Skettenis    instructions (SALs) from the instruction body.  Need to skip those
4008b725ae77Skettenis    while not getting into the function body.
4009b725ae77Skettenis 
4010b725ae77Skettenis    The functions end point and an increasing SAL line are used as
4011b725ae77Skettenis    indicators of the prologue's endpoint.
4012b725ae77Skettenis 
4013b725ae77Skettenis    This code is based on the function refine_prologue_limit (versions
4014b725ae77Skettenis    found in both ia64 and ppc).  */
4015b725ae77Skettenis 
4016b725ae77Skettenis CORE_ADDR
skip_prologue_using_sal(CORE_ADDR func_addr)4017b725ae77Skettenis skip_prologue_using_sal (CORE_ADDR func_addr)
4018b725ae77Skettenis {
4019b725ae77Skettenis   struct symtab_and_line prologue_sal;
4020b725ae77Skettenis   CORE_ADDR start_pc;
4021b725ae77Skettenis   CORE_ADDR end_pc;
4022b725ae77Skettenis 
4023b725ae77Skettenis   /* Get an initial range for the function.  */
4024b725ae77Skettenis   find_pc_partial_function (func_addr, NULL, &start_pc, &end_pc);
4025*63addd46Skettenis   start_pc += DEPRECATED_FUNCTION_START_OFFSET;
4026b725ae77Skettenis 
4027b725ae77Skettenis   prologue_sal = find_pc_line (start_pc, 0);
4028b725ae77Skettenis   if (prologue_sal.line != 0)
4029b725ae77Skettenis     {
4030b725ae77Skettenis       while (prologue_sal.end < end_pc)
4031b725ae77Skettenis 	{
4032b725ae77Skettenis 	  struct symtab_and_line sal;
4033b725ae77Skettenis 
4034b725ae77Skettenis 	  sal = find_pc_line (prologue_sal.end, 0);
4035b725ae77Skettenis 	  if (sal.line == 0)
4036b725ae77Skettenis 	    break;
4037b725ae77Skettenis 	  /* Assume that a consecutive SAL for the same (or larger)
4038b725ae77Skettenis 	     line mark the prologue -> body transition.  */
4039b725ae77Skettenis 	  if (sal.line >= prologue_sal.line)
4040b725ae77Skettenis 	    break;
4041b725ae77Skettenis 	  /* The case in which compiler's optimizer/scheduler has
4042b725ae77Skettenis 	     moved instructions into the prologue.  We look ahead in
4043b725ae77Skettenis 	     the function looking for address ranges whose
4044b725ae77Skettenis 	     corresponding line number is less the first one that we
4045b725ae77Skettenis 	     found for the function.  This is more conservative then
4046b725ae77Skettenis 	     refine_prologue_limit which scans a large number of SALs
4047b725ae77Skettenis 	     looking for any in the prologue */
4048b725ae77Skettenis 	  prologue_sal = sal;
4049b725ae77Skettenis 	}
4050b725ae77Skettenis     }
4051b725ae77Skettenis   return prologue_sal.end;
4052b725ae77Skettenis }
4053e93f7393Sniklas 
4054b725ae77Skettenis struct symtabs_and_lines
decode_line_spec(char * string,int funfirstline)4055b725ae77Skettenis decode_line_spec (char *string, int funfirstline)
4056b725ae77Skettenis {
4057b725ae77Skettenis   struct symtabs_and_lines sals;
4058b725ae77Skettenis   struct symtab_and_line cursal;
4059b725ae77Skettenis 
4060b725ae77Skettenis   if (string == 0)
4061b725ae77Skettenis     error ("Empty line specification.");
4062b725ae77Skettenis 
4063b725ae77Skettenis   /* We use whatever is set as the current source line. We do not try
4064b725ae77Skettenis      and get a default  or it will recursively call us! */
4065b725ae77Skettenis   cursal = get_current_source_symtab_and_line ();
4066b725ae77Skettenis 
4067b725ae77Skettenis   sals = decode_line_1 (&string, funfirstline,
4068b725ae77Skettenis 			cursal.symtab, cursal.line,
4069b725ae77Skettenis 			(char ***) NULL, NULL);
4070b725ae77Skettenis 
4071b725ae77Skettenis   if (*string)
4072b725ae77Skettenis     error ("Junk at end of line specification: %s", string);
4073b725ae77Skettenis   return sals;
4074b725ae77Skettenis }
4075b725ae77Skettenis 
4076b725ae77Skettenis /* Track MAIN */
4077b725ae77Skettenis static char *name_of_main;
4078b725ae77Skettenis 
4079e93f7393Sniklas void
set_main_name(const char * name)4080b725ae77Skettenis set_main_name (const char *name)
4081b725ae77Skettenis {
4082b725ae77Skettenis   if (name_of_main != NULL)
4083b725ae77Skettenis     {
4084b725ae77Skettenis       xfree (name_of_main);
4085b725ae77Skettenis       name_of_main = NULL;
4086b725ae77Skettenis     }
4087b725ae77Skettenis   if (name != NULL)
4088b725ae77Skettenis     {
4089b725ae77Skettenis       name_of_main = xstrdup (name);
4090b725ae77Skettenis     }
4091b725ae77Skettenis }
4092b725ae77Skettenis 
4093b725ae77Skettenis char *
main_name(void)4094b725ae77Skettenis main_name (void)
4095b725ae77Skettenis {
4096b725ae77Skettenis   if (name_of_main != NULL)
4097b725ae77Skettenis     return name_of_main;
4098b725ae77Skettenis   else
4099b725ae77Skettenis     return "main";
4100b725ae77Skettenis }
4101b725ae77Skettenis 
4102b725ae77Skettenis 
4103b725ae77Skettenis void
_initialize_symtab(void)4104b725ae77Skettenis _initialize_symtab (void)
4105e93f7393Sniklas {
4106e93f7393Sniklas   add_info ("variables", variables_info,
4107e93f7393Sniklas 	 "All global and static variable names, or those matching REGEXP.");
4108b725ae77Skettenis   if (dbx_commands)
4109b725ae77Skettenis     add_com ("whereis", class_info, variables_info,
4110b725ae77Skettenis 	 "All global and static variable names, or those matching REGEXP.");
4111b725ae77Skettenis 
4112e93f7393Sniklas   add_info ("functions", functions_info,
4113e93f7393Sniklas 	    "All function names, or those matching REGEXP.");
4114e93f7393Sniklas 
4115b725ae77Skettenis 
4116e93f7393Sniklas   /* FIXME:  This command has at least the following problems:
4117e93f7393Sniklas      1.  It prints builtin types (in a very strange and confusing fashion).
4118e93f7393Sniklas      2.  It doesn't print right, e.g. with
4119e93f7393Sniklas      typedef struct foo *FOO
4120e93f7393Sniklas      type_print prints "FOO" when we want to make it (in this situation)
4121e93f7393Sniklas      print "struct foo *".
4122e93f7393Sniklas      I also think "ptype" or "whatis" is more likely to be useful (but if
4123e93f7393Sniklas      there is much disagreement "info types" can be fixed).  */
4124e93f7393Sniklas   add_info ("types", types_info,
4125e93f7393Sniklas 	    "All type names, or those matching REGEXP.");
4126e93f7393Sniklas 
4127e93f7393Sniklas   add_info ("sources", sources_info,
4128e93f7393Sniklas 	    "Source files in the program.");
4129e93f7393Sniklas 
4130b725ae77Skettenis   add_com ("rbreak", class_breakpoint, rbreak_command,
4131e93f7393Sniklas 	   "Set a breakpoint for all functions matching REGEXP.");
4132e93f7393Sniklas 
4133b725ae77Skettenis   if (xdb_commands)
4134b725ae77Skettenis     {
4135b725ae77Skettenis       add_com ("lf", class_info, sources_info, "Source files in the program");
4136b725ae77Skettenis       add_com ("lg", class_info, variables_info,
4137b725ae77Skettenis 	 "All global and static variable names, or those matching REGEXP.");
4138b725ae77Skettenis     }
4139b725ae77Skettenis 
4140e93f7393Sniklas   /* Initialize the one built-in type that isn't language dependent... */
4141e93f7393Sniklas   builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4142e93f7393Sniklas 				  "<unknown type>", (struct objfile *) NULL);
4143e93f7393Sniklas }
4144