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