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