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