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