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