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