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