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