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