A ton of changes to improve C++ debugging. See ChangeLog.
[platform/upstream/binutils.git] / gdb / symtab.c
1 /* Symbol table lookup for the GNU debugger, GDB.
2    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992
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., 675 Mass Ave, Cambridge, MA 02139, 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 "regex.h"
33 #include "expression.h"
34 #include "language.h"
35 #include "demangle.h"
36
37 #include <obstack.h>
38 #include <assert.h>
39
40 #include <sys/types.h>
41 #include <fcntl.h>
42 #include <string.h>
43 #include <sys/stat.h>
44 #include <ctype.h>
45
46 /* Prototypes for local functions */
47
48 static char *
49 expensive_mangler PARAMS ((const char *));
50
51 extern int
52 find_methods PARAMS ((struct type *, char *, char **, struct symbol **));
53
54 static void
55 completion_list_add_symbol PARAMS ((char *, char *, int));
56
57 static struct symtabs_and_lines
58 decode_line_2 PARAMS ((struct symbol *[], int, int));
59
60 static void
61 rbreak_command PARAMS ((char *, int));
62
63 static void
64 types_info PARAMS ((char *, int));
65
66 static void
67 functions_info PARAMS ((char *, int));
68
69 static void
70 variables_info PARAMS ((char *, int));
71
72 static void
73 sources_info PARAMS ((char *, int));
74
75 static void
76 list_symbols PARAMS ((char *, int, int));
77
78 static void
79 output_source_filename PARAMS ((char *, int *));
80
81 static char *
82 operator_chars PARAMS ((char *, char **));
83
84 static int
85 find_line_common PARAMS ((struct linetable *, int, int *));
86
87 static struct partial_symbol *
88 lookup_partial_symbol PARAMS ((struct partial_symtab *, const char *,
89                                int, enum namespace));
90
91 static struct partial_symbol *
92 lookup_demangled_partial_symbol PARAMS ((const struct partial_symtab *,
93                                          const char *));
94
95 static struct symbol *
96 lookup_demangled_block_symbol PARAMS ((const struct block *, const char *));
97
98 static struct symtab *
99 lookup_symtab_1 PARAMS ((char *));
100
101 /* */
102
103 /* The single non-language-specific builtin type */
104 struct type *builtin_type_error;
105
106 /* Block in which the most recently searched-for symbol was found.
107    Might be better to make this a parameter to lookup_symbol and 
108    value_of_this. */
109
110 const struct block *block_found;
111
112 char no_symtab_msg[] = "No symbol table is loaded.  Use the \"file\" command.";
113
114 /* While the C++ support is still in flux, issue a possibly helpful hint on
115    using the new command completion feature on single quoted demangled C++
116    symbols.  Remove when loose ends are cleaned up.   FIXME -fnf */
117
118 void
119 cplusplus_hint (name)
120      char *name;
121 {
122   printf ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
123   printf ("(Note leading single quote.)\n");
124 }
125
126 /* Check for a symtab of a specific name; first in symtabs, then in
127    psymtabs.  *If* there is no '/' in the name, a match after a '/'
128    in the symtab filename will also work.  */
129
130 static struct symtab *
131 lookup_symtab_1 (name)
132      char *name;
133 {
134   register struct symtab *s;
135   register struct partial_symtab *ps;
136   register char *slash;
137   register struct objfile *objfile;
138
139  got_symtab:
140
141   /* First, search for an exact match */
142
143   ALL_SYMTABS (objfile, s)
144     if (strcmp (name, s->filename) == 0)
145       return s;
146
147   slash = strchr (name, '/');
148
149   /* Now, search for a matching tail (only if name doesn't have any dirs) */
150
151   if (!slash)
152     ALL_SYMTABS (objfile, s)
153       {
154         char *p = s -> filename;
155         char *tail = strrchr (p, '/');
156
157         if (tail)
158           p = tail + 1;
159
160         if (strcmp (p, name) == 0)
161           return s;
162       }
163
164   /* Same search rules as above apply here, but now we look thru the
165      psymtabs.  */
166
167   ALL_PSYMTABS (objfile, ps)
168     if (strcmp (name, ps -> filename) == 0)
169       goto got_psymtab;
170
171   if (!slash)
172     ALL_PSYMTABS (objfile, ps)
173       {
174         char *p = ps -> filename;
175         char *tail = strrchr (p, '/');
176
177         if (tail)
178           p = tail + 1;
179
180         if (strcmp (p, name) == 0)
181           goto got_psymtab;
182       }
183
184   return (NULL);
185
186  got_psymtab:
187
188   if (ps -> readin)
189     error ("Internal: readin %s pst for `%s' found when no symtab found.",
190            ps -> filename, name);
191
192   s = PSYMTAB_TO_SYMTAB (ps);
193
194   if (s)
195     return s;
196
197   /* At this point, we have located the psymtab for this file, but
198      the conversion to a symtab has failed.  This usually happens
199      when we are looking up an include file.  In this case,
200      PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
201      been created.  So, we need to run through the symtabs again in
202      order to find the file.
203      XXX - This is a crock, and should be fixed inside of the the
204      symbol parsing routines. */
205   goto got_symtab;
206 }
207
208 /* Lookup the symbol table of a source file named NAME.  Try a couple
209    of variations if the first lookup doesn't work.  */
210
211 struct symtab *
212 lookup_symtab (name)
213      char *name;
214 {
215   register struct symtab *s;
216   register char *copy;
217
218   s = lookup_symtab_1 (name);
219   if (s) return s;
220
221   /* If name not found as specified, see if adding ".c" helps.  */
222
223   copy = (char *) alloca (strlen (name) + 3);
224   strcpy (copy, name);
225   strcat (copy, ".c");
226   s = lookup_symtab_1 (copy);
227   if (s) return s;
228
229   /* We didn't find anything; die.  */
230   return 0;
231 }
232
233 /* Lookup the partial symbol table of a source file named NAME.  This
234    only returns true on an exact match (ie. this semantics are
235    different from lookup_symtab.  */
236
237 struct partial_symtab *
238 lookup_partial_symtab (name)
239 char *name;
240 {
241   register struct partial_symtab *pst;
242   register struct objfile *objfile;
243   
244   ALL_PSYMTABS (objfile, pst)
245     {
246       if (strcmp (name, pst -> filename) == 0)
247         {
248           return (pst);
249         }
250     }
251   return (NULL);
252 }
253 \f
254 /* Demangle a GDB method stub type.  */
255 char *
256 gdb_mangle_name (type, i, j)
257      struct type *type;
258      int i, j;
259 {
260   int mangled_name_len;
261   char *mangled_name;
262   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
263   struct fn_field *method = &f[j];
264   char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
265   char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
266   char *newname = type_name_no_tag (type);
267   int is_constructor = strcmp(field_name, newname) == 0;
268   int is_destructor = is_constructor && physname[0] == '_'
269       && physname[1] == CPLUS_MARKER && physname[2] == '_';
270   /* Need a new type prefix.  */
271   char *const_prefix = method->is_const ? "C" : "";
272   char *volatile_prefix = method->is_volatile ? "V" : "";
273   char buf[20];
274 #ifndef GCC_MANGLE_BUG
275   int len = strlen (newname);
276
277   if (is_destructor)
278     {
279       mangled_name = (char*) xmalloc(strlen(physname)+1);
280       strcpy(mangled_name, physname);
281       return mangled_name;
282     }
283
284   sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
285   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
286                           + strlen (buf) + len
287                           + strlen (physname)
288                           + 1);
289
290   /* Only needed for GNU-mangled names.  ANSI-mangled names
291      work with the normal mechanisms.  */
292   if (OPNAME_PREFIX_P (field_name))
293     {
294       char *opname = cplus_mangle_opname (field_name + 3, 0);
295       if (opname == NULL)
296         error ("No mangling for \"%s\"", field_name);
297       mangled_name_len += strlen (opname);
298       mangled_name = (char *)xmalloc (mangled_name_len);
299
300       strncpy (mangled_name, field_name, 3);
301       mangled_name[3] = '\0';
302       strcat (mangled_name, opname);
303     }
304   else
305     {
306       mangled_name = (char *)xmalloc (mangled_name_len);
307       if (is_constructor)
308         mangled_name[0] = '\0';
309       else
310         strcpy (mangled_name, field_name);
311     }
312   strcat (mangled_name, buf);
313   strcat (mangled_name, newname);
314 #else
315   char *opname;
316
317   if (is_constructor)
318     {
319       buf[0] = '\0';
320     }
321   else
322     {
323       sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
324     }
325
326   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
327                       + strlen (buf) + strlen (physname) + 1);
328
329   /* Only needed for GNU-mangled names.  ANSI-mangled names
330      work with the normal mechanisms.  */
331   if (OPNAME_PREFIX_P (field_name))
332     {
333       opname = cplus_mangle_opname (field_name + 3, 0);
334       if (opname == NULL)
335         {
336           error ("No mangling for \"%s\"", field_name);
337         }
338       mangled_name_len += strlen (opname);
339       mangled_name = (char *) xmalloc (mangled_name_len);
340
341       strncpy (mangled_name, field_name, 3);
342       strcpy (mangled_name + 3, opname);
343     }
344   else
345     {
346       mangled_name = (char *) xmalloc (mangled_name_len);
347       if (is_constructor)
348         {
349           mangled_name[0] = '\0';
350         }
351       else
352         {
353           strcpy (mangled_name, field_name);
354         }
355     }
356   strcat (mangled_name, buf);
357
358 #endif
359   strcat (mangled_name, physname);
360   return (mangled_name);
361 }
362
363 \f
364 /* Find which partial symtab on contains PC.  Return 0 if none.  */
365
366 struct partial_symtab *
367 find_pc_psymtab (pc)
368      register CORE_ADDR pc;
369 {
370   register struct partial_symtab *pst;
371   register struct objfile *objfile;
372
373   ALL_PSYMTABS (objfile, pst)
374     {
375       if (pc >= pst -> textlow && pc < pst -> texthigh)
376         {
377           return (pst);
378         }
379     }
380   return (NULL);
381 }
382
383 /* Find which partial symbol within a psymtab contains PC.  Return 0
384    if none.  Check all psymtabs if PSYMTAB is 0.  */
385 struct partial_symbol *
386 find_pc_psymbol (psymtab, pc)
387      struct partial_symtab *psymtab;
388      CORE_ADDR pc;
389 {
390   struct partial_symbol *best, *p;
391   CORE_ADDR best_pc;
392   
393   if (!psymtab)
394     psymtab = find_pc_psymtab (pc);
395   if (!psymtab)
396     return 0;
397
398   best_pc = psymtab->textlow - 1;
399
400   for (p = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
401        (p - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
402         < psymtab->n_static_syms);
403        p++)
404     if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
405         && SYMBOL_CLASS (p) == LOC_BLOCK
406         && pc >= SYMBOL_VALUE_ADDRESS (p)
407         && SYMBOL_VALUE_ADDRESS (p) > best_pc)
408       {
409         best_pc = SYMBOL_VALUE_ADDRESS (p);
410         best = p;
411       }
412   if (best_pc == psymtab->textlow - 1)
413     return 0;
414   return best;
415 }
416
417 \f
418 /* Find the definition for a specified symbol name NAME
419    in namespace NAMESPACE, visible from lexical block BLOCK.
420    Returns the struct symbol pointer, or zero if no symbol is found.
421    If SYMTAB is non-NULL, store the symbol table in which the
422    symbol was found there, or NULL if not found.
423    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
424    NAME is a field of the current implied argument `this'.  If so set
425    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. 
426    BLOCK_FOUND is set to the block in which NAME is found (in the case of
427    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
428
429 struct symbol *
430 lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
431      const char *name;
432      register const struct block *block;
433      const enum namespace namespace;
434      int *is_a_field_of_this;
435      struct symtab **symtab;
436 {
437   register struct symbol *sym;
438   register struct symtab *s;
439   register struct partial_symtab *ps;
440   struct blockvector *bv;
441   register struct objfile *objfile;
442   register struct block *b;
443   register struct minimal_symbol *msymbol;
444   char *temp;
445   extern char *gdb_completer_word_break_characters;
446
447   /* If NAME contains any characters from gdb_completer_word_break_characters
448      then it is probably from a quoted name string.  So check to see if it
449      has a C++ mangled equivalent, and if so, use the mangled equivalent. */
450
451   if (strpbrk (name, gdb_completer_word_break_characters) != NULL)
452     {
453       if ((temp = expensive_mangler (name)) != NULL)
454         {
455           name = temp;
456         }
457     }
458
459   /* Search specified block and its superiors.  */
460
461   while (block != 0)
462     {
463       sym = lookup_block_symbol (block, name, namespace);
464       if (sym) 
465         {
466           block_found = block;
467           if (symtab != NULL)
468             {
469               /* Search the list of symtabs for one which contains the
470                  address of the start of this block.  */
471               ALL_SYMTABS (objfile, s)
472                 {
473                   bv = BLOCKVECTOR (s);
474                   b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
475                   if (BLOCK_START (b) <= BLOCK_START (block)
476                       && BLOCK_END (b) > BLOCK_START (block))
477                     goto found;
478                 }
479 found:
480               *symtab = s;
481             }
482
483           return (sym);
484         }
485       block = BLOCK_SUPERBLOCK (block);
486     }
487
488   /* But that doesn't do any demangling for the STATIC_BLOCK.
489      I'm not sure whether demangling is needed in the case of
490      nested function in inner blocks; if so this needs to be changed.
491      
492      Don't need to mess with the psymtabs; if we have a block,
493      that file is read in.  If we don't, then we deal later with
494      all the psymtab stuff that needs checking.  */
495   if (namespace == VAR_NAMESPACE && block != NULL)
496     {
497       struct block *b;
498       /* Find the right symtab.  */
499       ALL_SYMTABS (objfile, s)
500         {
501           bv = BLOCKVECTOR (s);
502           b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
503           if (BLOCK_START (b) <= BLOCK_START (block)
504               && BLOCK_END (b) > BLOCK_START (block))
505             {
506               sym = lookup_demangled_block_symbol (b, name);
507               if (sym)
508                 {
509                   block_found = b;
510                   if (symtab != NULL)
511                     *symtab = s;
512                   return sym;
513                 }
514             }
515         }
516     }
517
518
519   /* C++: If requested to do so by the caller, 
520      check to see if NAME is a field of `this'. */
521   if (is_a_field_of_this)
522     {
523       struct value *v = value_of_this (0);
524       
525       *is_a_field_of_this = 0;
526       if (v && check_field (v, name))
527         {
528           *is_a_field_of_this = 1;
529           if (symtab != NULL)
530             *symtab = NULL;
531           return 0;
532         }
533     }
534
535   /* Now search all global blocks.  Do the symtab's first, then
536      check the psymtab's */
537   
538   ALL_SYMTABS (objfile, s)
539     {
540       bv = BLOCKVECTOR (s);
541       block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
542       sym = lookup_block_symbol (block, name, namespace);
543       if (sym) 
544         {
545           block_found = block;
546           if (symtab != NULL)
547             *symtab = s;
548           return sym;
549         }
550     }
551
552   /* Check for the possibility of the symbol being a global function
553      that is stored in one of the minimal symbol tables.  Eventually, all
554      global symbols might be resolved in this way.  */
555   
556   if (namespace == VAR_NAMESPACE)
557     {
558       msymbol = lookup_minimal_symbol (name, (struct objfile *) NULL);
559
560       if (msymbol == NULL)
561         {
562           /* Test each minimal symbol to see if the minimal symbol's name
563              is a C++ mangled name that matches a user visible name.  */
564
565           char *demangled;
566
567           ALL_MSYMBOLS (objfile, msymbol)
568             {
569               demangled = demangle_and_match (msymbol -> name, name,
570                                               DMGL_PARAMS | DMGL_ANSI);
571               if (demangled != NULL)
572                 {
573                   free (demangled);
574                   goto found_msym;
575                 }
576             }
577           msymbol = NULL;               /* Not found */
578         }
579
580 found_msym:
581       if (msymbol != NULL)
582         {
583           s = find_pc_symtab (msymbol -> address);
584           /* If S is NULL, there are no debug symbols for this file.
585              Skip this stuff and check for matching static symbols below. */
586           if (s != NULL)
587             {
588               bv = BLOCKVECTOR (s);
589               block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
590               sym = lookup_block_symbol (block, msymbol -> name, namespace);
591               /* We kept static functions in minimal symbol table as well as
592                  in static scope. We want to find them in the symbol table. */
593                 if (!sym) {
594                   block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
595                   sym = lookup_block_symbol (block, msymbol -> name,
596                                              namespace);
597                 }
598
599               /* sym == 0 if symbol was found in the minimal symbol table
600                  but not in the symtab.
601                  Return 0 to use the msymbol definition of "foo_".
602
603                  This happens for Fortran  "foo_" symbols,
604                  which are "foo" in the symtab.
605
606                  This can also happen if "asm" is used to make a
607                  regular symbol but not a debugging symbol, e.g.
608                  asm(".globl _main");
609                  asm("_main:");
610                  */
611
612               if (symtab != NULL)
613                 *symtab = s;
614               return sym;
615             }
616         }
617     }
618       
619   ALL_PSYMTABS (objfile, ps)
620     {
621       if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
622         {
623           s = PSYMTAB_TO_SYMTAB(ps);
624           bv = BLOCKVECTOR (s);
625           block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
626           sym = lookup_block_symbol (block, name, namespace);
627           if (!sym)
628             error ("Internal: global symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
629           if (symtab != NULL)
630             *symtab = s;
631           return sym;
632         }
633     }
634
635   /* Now search all per-file blocks.
636      Not strictly correct, but more useful than an error.
637      Do the symtabs first, then check the psymtabs */
638
639   ALL_SYMTABS (objfile, s)
640     {
641       bv = BLOCKVECTOR (s);
642       block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
643       sym = lookup_block_symbol (block, name, namespace);
644       if (sym) 
645         {
646           block_found = block;
647           if (symtab != NULL)
648             *symtab = s;
649           return sym;
650         }
651     }
652
653   ALL_PSYMTABS (objfile, ps)
654     {
655       if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
656         {
657           s = PSYMTAB_TO_SYMTAB(ps);
658           bv = BLOCKVECTOR (s);
659           block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
660           sym = lookup_block_symbol (block, name, namespace);
661           if (!sym)
662             error ("Internal: static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
663           if (symtab != NULL)
664             *symtab = s;
665           return sym;
666         }
667     }
668
669   /* Now search all per-file blocks for static mangled symbols.
670      Do the symtabs first, then check the psymtabs.  */
671
672   if (namespace == VAR_NAMESPACE)
673     {
674       ALL_SYMTABS (objfile, s)
675         {
676           bv = BLOCKVECTOR (s);
677           block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
678           sym = lookup_demangled_block_symbol (block, name);
679           if (sym) 
680             {
681               block_found = block;
682               if (symtab != NULL)
683                 *symtab = s;
684               return sym;
685             }
686         }
687
688       ALL_PSYMTABS (objfile, ps)
689         {
690           if (!ps->readin && lookup_demangled_partial_symbol (ps, name))
691             {
692               s = PSYMTAB_TO_SYMTAB(ps);
693               bv = BLOCKVECTOR (s);
694               block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
695               sym = lookup_demangled_block_symbol (block, name);
696               if (!sym)
697                 error ("Internal: mangled static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
698               if (symtab != NULL)
699                 *symtab = s;
700               return sym;
701             }
702         }
703     }
704
705   if (symtab != NULL)
706     *symtab = NULL;
707   return 0;
708 }
709
710 /* Look for a static demangled symbol in block BLOCK.  */
711
712 static struct symbol *
713 lookup_demangled_block_symbol (block, name)
714      register const struct block *block;
715      const char *name;
716 {
717   register int bot, top;
718   register struct symbol *sym;
719   char *demangled;
720
721   bot = 0;
722   top = BLOCK_NSYMS (block);
723
724   while (bot < top)
725     {
726       sym = BLOCK_SYM (block, bot);
727       if (SYMBOL_NAMESPACE (sym) == VAR_NAMESPACE)
728         {
729           demangled = demangle_and_match (SYMBOL_NAME (sym), name,
730                                           DMGL_PARAMS | DMGL_ANSI);
731           if (demangled != NULL)
732             {
733               free (demangled);
734               return (sym);
735             }
736         }
737       bot++;
738     }
739
740   return (NULL);
741 }
742
743 /* Look, in partial_symtab PST, for static mangled symbol NAME. */
744
745 static struct partial_symbol *
746 lookup_demangled_partial_symbol (pst, name)
747      const struct partial_symtab *pst;
748      const char *name;
749 {
750   struct partial_symbol *start, *psym;
751   int length = pst->n_static_syms;
752   char *demangled;
753
754   if (!length)
755     return (struct partial_symbol *) 0;
756   
757   start = pst->objfile->static_psymbols.list + pst->statics_offset;
758   for (psym = start; psym < start + length; psym++)
759     {
760       if (SYMBOL_NAMESPACE (psym) == VAR_NAMESPACE)
761         {
762           demangled = demangle_and_match (SYMBOL_NAME (psym), name,
763                                           DMGL_PARAMS | DMGL_ANSI);
764           if (demangled != NULL)
765             {
766               free (demangled);
767               return (psym);
768             }
769         }
770     }
771
772   return (NULL);
773 }
774
775 /* Look, in partial_symtab PST, for symbol NAME.  Check the global
776    symbols if GLOBAL, the static symbols if not */
777
778 static struct partial_symbol *
779 lookup_partial_symbol (pst, name, global, namespace)
780      struct partial_symtab *pst;
781      const char *name;
782      int global;
783      enum namespace namespace;
784 {
785   struct partial_symbol *start, *psym;
786   int length = (global ? pst->n_global_syms : pst->n_static_syms);
787
788   if (!length)
789     return (struct partial_symbol *) 0;
790   
791   start = (global ?
792            pst->objfile->global_psymbols.list + pst->globals_offset :
793            pst->objfile->static_psymbols.list + pst->statics_offset  );
794
795   if (global)                   /* This means we can use a binary */
796                                 /* search.  */
797     {
798       struct partial_symbol *top, *bottom, *center;
799
800       /* Binary search.  This search is guaranteed to end with center
801          pointing at the earliest partial symbol with the correct
802          name.  At that point *all* partial symbols with that name
803          will be checked against the correct namespace. */
804       bottom = start;
805       top = start + length - 1;
806       while (top > bottom)
807         {
808           center = bottom + (top - bottom) / 2;
809
810           assert (center < top);
811           
812           if (strcmp (SYMBOL_NAME (center), name) >= 0)
813             top = center;
814           else
815             bottom = center + 1;
816         }
817       assert (top == bottom);
818       
819       while (!strcmp (SYMBOL_NAME (top), name))
820         {
821           if (SYMBOL_NAMESPACE (top) == namespace)
822             return top;
823           top ++;
824         }
825     }
826   else
827     {
828       /* Can't use a binary search */
829       for (psym = start; psym < start + length; psym++)
830         if (namespace == SYMBOL_NAMESPACE (psym)
831             && !strcmp (name, SYMBOL_NAME (psym)))
832           return psym;
833     }
834
835   return (struct partial_symbol *) 0;
836 }
837
838 /* Find the psymtab containing main(). */
839
840 struct partial_symtab *
841 find_main_psymtab ()
842 {
843   register struct partial_symtab *pst;
844   register struct objfile *objfile;
845
846   ALL_PSYMTABS (objfile, pst)
847     {
848       if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
849         {
850           return (pst);
851         }
852     }
853   return (NULL);
854 }
855
856 /* Look for a symbol in block BLOCK.  */
857
858 struct symbol *
859 lookup_block_symbol (block, name, namespace)
860      register const struct block *block;
861      const char *name;
862      const enum namespace namespace;
863 {
864   register int bot, top, inc;
865   register struct symbol *sym, *parameter_sym;
866
867   top = BLOCK_NSYMS (block);
868   bot = 0;
869
870   /* If the blocks's symbols were sorted, start with a binary search.  */
871
872   if (BLOCK_SHOULD_SORT (block))
873     {
874       /* First, advance BOT to not far before
875          the first symbol whose name is NAME.  */
876
877       while (1)
878         {
879           inc = (top - bot + 1);
880           /* No need to keep binary searching for the last few bits worth.  */
881           if (inc < 4)
882             break;
883           inc = (inc >> 1) + bot;
884           sym = BLOCK_SYM (block, inc);
885           if (SYMBOL_NAME (sym)[0] < name[0])
886             bot = inc;
887           else if (SYMBOL_NAME (sym)[0] > name[0])
888             top = inc;
889           else if (strcmp (SYMBOL_NAME (sym), name) < 0)
890             bot = inc;
891           else
892             top = inc;
893         }
894
895       /* Now scan forward until we run out of symbols,
896          find one whose name is greater than NAME,
897          or find one we want.
898          If there is more than one symbol with the right name and namespace,
899          we return the first one.  dbxread.c is careful to make sure
900          that if one is a register then it comes first.  */
901
902       top = BLOCK_NSYMS (block);
903       while (bot < top)
904         {
905           sym = BLOCK_SYM (block, bot);
906           inc = SYMBOL_NAME (sym)[0] - name[0];
907           if (inc == 0)
908             inc = strcmp (SYMBOL_NAME (sym), name);
909           if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
910             return sym;
911           if (inc > 0)
912             return 0;
913           bot++;
914         }
915       return 0;
916     }
917
918   /* Here if block isn't sorted.
919      This loop is equivalent to the loop above,
920      but hacked greatly for speed.
921
922      Note that parameter symbols do not always show up last in the
923      list; this loop makes sure to take anything else other than
924      parameter symbols first; it only uses parameter symbols as a
925      last resort.  Note that this only takes up extra computation
926      time on a match.  */
927
928   parameter_sym = (struct symbol *) 0;
929   top = BLOCK_NSYMS (block);
930   inc = name[0];
931   while (bot < top)
932     {
933       sym = BLOCK_SYM (block, bot);
934       if (SYMBOL_NAME (sym)[0] == inc
935           && !strcmp (SYMBOL_NAME (sym), name)
936           && SYMBOL_NAMESPACE (sym) == namespace)
937         {
938           if (SYMBOL_CLASS (sym) == LOC_ARG
939               || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
940               || SYMBOL_CLASS (sym) == LOC_REF_ARG
941               || SYMBOL_CLASS (sym) == LOC_REGPARM)
942             parameter_sym = sym;
943           else
944             return sym;
945         }
946       bot++;
947     }
948   return parameter_sym;         /* Will be 0 if not found. */
949 }
950 \f
951 /* Return the symbol for the function which contains a specified
952    lexical block, described by a struct block BL.  */
953
954 struct symbol *
955 block_function (bl)
956      struct block *bl;
957 {
958   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
959     bl = BLOCK_SUPERBLOCK (bl);
960
961   return BLOCK_FUNCTION (bl);
962 }
963
964 /* Subroutine of find_pc_line */
965
966 struct symtab *
967 find_pc_symtab (pc)
968      register CORE_ADDR pc;
969 {
970   register struct block *b;
971   struct blockvector *bv;
972   register struct symtab *s = 0;
973   register struct partial_symtab *ps;
974   register struct objfile *objfile;
975
976   /* Search all symtabs for one whose file contains our pc */
977
978   ALL_SYMTABS (objfile, s)
979     {
980       bv = BLOCKVECTOR (s);
981       b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
982       if (BLOCK_START (b) <= pc
983           && BLOCK_END (b) > pc)
984         goto found;
985     }
986
987   if (!s)
988     {
989       ps = find_pc_psymtab (pc);
990       if (ps && ps->readin)
991         {
992           printf_filtered ("(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc);
993         }
994       if (ps)
995         {
996           s = PSYMTAB_TO_SYMTAB (ps);
997         }
998     }
999
1000 found:
1001   return (s);
1002 }
1003
1004 /* Find the source file and line number for a given PC value.
1005    Return a structure containing a symtab pointer, a line number,
1006    and a pc range for the entire source line.
1007    The value's .pc field is NOT the specified pc.
1008    NOTCURRENT nonzero means, if specified pc is on a line boundary,
1009    use the line that ends there.  Otherwise, in that case, the line
1010    that begins there is used.  */
1011
1012 struct symtab_and_line
1013 find_pc_line (pc, notcurrent)
1014      CORE_ADDR pc;
1015      int notcurrent;
1016 {
1017   struct symtab *s;
1018   register struct linetable *l;
1019   register int len;
1020   register int i;
1021   register struct linetable_entry *item;
1022   struct symtab_and_line val;
1023   struct blockvector *bv;
1024
1025   /* Info on best line seen so far, and where it starts, and its file.  */
1026
1027   int best_line = 0;
1028   CORE_ADDR best_pc = 0;
1029   CORE_ADDR best_end = 0;
1030   struct symtab *best_symtab = 0;
1031
1032   /* Store here the first line number
1033      of a file which contains the line at the smallest pc after PC.
1034      If we don't find a line whose range contains PC,
1035      we will use a line one less than this,
1036      with a range from the start of that file to the first line's pc.  */
1037   int alt_line = 0;
1038   CORE_ADDR alt_pc = 0;
1039   struct symtab *alt_symtab = 0;
1040
1041   /* Info on best line seen in this file.  */
1042
1043   int prev_line;
1044   CORE_ADDR prev_pc;
1045
1046   /* Info on first line of this file.  */
1047
1048   int first_line;
1049   CORE_ADDR first_pc;
1050
1051   /* If this pc is not from the current frame,
1052      it is the address of the end of a call instruction.
1053      Quite likely that is the start of the following statement.
1054      But what we want is the statement containing the instruction.
1055      Fudge the pc to make sure we get that.  */
1056
1057   if (notcurrent) pc -= 1;
1058
1059   s = find_pc_symtab (pc);
1060   if (s == 0)
1061     {
1062       val.symtab = 0;
1063       val.line = 0;
1064       val.pc = pc;
1065       val.end = 0;
1066       return val;
1067     }
1068
1069   bv = BLOCKVECTOR (s);
1070
1071   /* Look at all the symtabs that share this blockvector.
1072      They all have the same apriori range, that we found was right;
1073      but they have different line tables.  */
1074
1075   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1076     {
1077       /* Find the best line in this symtab.  */
1078       l = LINETABLE (s);
1079       if (!l)
1080         continue;
1081       len = l->nitems;
1082       prev_line = -1;
1083       first_line = -1;
1084       for (i = 0; i < len; i++)
1085         {
1086           item = &(l->item[i]);
1087           
1088           if (first_line < 0)
1089             {
1090               first_line = item->line;
1091               first_pc = item->pc;
1092             }
1093           /* Return the last line that did not start after PC.  */
1094           if (pc >= item->pc)
1095             {
1096               prev_line = item->line;
1097               prev_pc = item->pc;
1098             }
1099           else
1100             break;
1101         }
1102
1103       /* Is this file's best line closer than the best in the other files?
1104          If so, record this file, and its best line, as best so far.  */
1105       if (prev_line >= 0 && prev_pc > best_pc)
1106         {
1107           best_pc = prev_pc;
1108           best_line = prev_line;
1109           best_symtab = s;
1110           /* If another line is in the linetable, and its PC is closer
1111              than the best_end we currently have, take it as best_end.  */
1112           if (i < len && (best_end == 0 || best_end > item->pc))
1113             best_end = item->pc;
1114         }
1115       /* Is this file's first line closer than the first lines of other files?
1116          If so, record this file, and its first line, as best alternate.  */
1117       if (first_line >= 0 && first_pc > pc
1118           && (alt_pc == 0 || first_pc < alt_pc))
1119         {
1120           alt_pc = first_pc;
1121           alt_line = first_line;
1122           alt_symtab = s;
1123         }
1124     }
1125   if (best_symtab == 0)
1126     {
1127       val.symtab = alt_symtab;
1128       val.line = alt_line - 1;
1129       val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1130       val.end = alt_pc;
1131     }
1132   else
1133     {
1134       val.symtab = best_symtab;
1135       val.line = best_line;
1136       val.pc = best_pc;
1137       if (best_end && (alt_pc == 0 || best_end < alt_pc))
1138         val.end = best_end;
1139       else if (alt_pc)
1140         val.end = alt_pc;
1141       else
1142         val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1143     }
1144   return val;
1145 }
1146 \f
1147 /* Find the PC value for a given source file and line number.
1148    Returns zero for invalid line number.
1149    The source file is specified with a struct symtab.  */
1150
1151 CORE_ADDR
1152 find_line_pc (symtab, line)
1153      struct symtab *symtab;
1154      int line;
1155 {
1156   register struct linetable *l;
1157   register int ind;
1158   int dummy;
1159
1160   if (symtab == 0)
1161     return 0;
1162   l = LINETABLE (symtab);
1163   ind = find_line_common(l, line, &dummy);
1164   return (ind >= 0) ? l->item[ind].pc : 0;
1165 }
1166
1167 /* Find the range of pc values in a line.
1168    Store the starting pc of the line into *STARTPTR
1169    and the ending pc (start of next line) into *ENDPTR.
1170    Returns 1 to indicate success.
1171    Returns 0 if could not find the specified line.  */
1172
1173 int
1174 find_line_pc_range (symtab, thisline, startptr, endptr)
1175      struct symtab *symtab;
1176      int thisline;
1177      CORE_ADDR *startptr, *endptr;
1178 {
1179   register struct linetable *l;
1180   register int ind;
1181   int exact_match;              /* did we get an exact linenumber match */
1182
1183   if (symtab == 0)
1184     return 0;
1185
1186   l = LINETABLE (symtab);
1187   ind = find_line_common (l, thisline, &exact_match);
1188   if (ind >= 0)
1189     {
1190       *startptr = l->item[ind].pc;
1191       /* If we have not seen an entry for the specified line,
1192          assume that means the specified line has zero bytes.  */
1193       if (!exact_match || ind == l->nitems-1)
1194         *endptr = *startptr;
1195       else
1196         /* Perhaps the following entry is for the following line.
1197            It's worth a try.  */
1198         if (ind+1 < l->nitems
1199          && l->item[ind+1].line == thisline + 1)
1200           *endptr = l->item[ind+1].pc;
1201         else
1202           *endptr = find_line_pc (symtab, thisline+1);
1203       return 1;
1204     }
1205
1206   return 0;
1207 }
1208
1209 /* Given a line table and a line number, return the index into the line
1210    table for the pc of the nearest line whose number is >= the specified one.
1211    Return -1 if none is found.  The value is >= 0 if it is an index.
1212
1213    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
1214
1215 static int
1216 find_line_common (l, lineno, exact_match)
1217      register struct linetable *l;
1218      register int lineno;
1219      int *exact_match;
1220 {
1221   register int i;
1222   register int len;
1223
1224   /* BEST is the smallest linenumber > LINENO so far seen,
1225      or 0 if none has been seen so far.
1226      BEST_INDEX identifies the item for it.  */
1227
1228   int best_index = -1;
1229   int best = 0;
1230
1231   if (lineno <= 0)
1232     return -1;
1233   if (l == 0)
1234     return -1;
1235
1236   len = l->nitems;
1237   for (i = 0; i < len; i++)
1238     {
1239       register struct linetable_entry *item = &(l->item[i]);
1240
1241       if (item->line == lineno)
1242         {
1243           *exact_match = 1;
1244           return i;
1245         }
1246
1247       if (item->line > lineno && (best == 0 || item->line < best))
1248         {
1249           best = item->line;
1250           best_index = i;
1251         }
1252     }
1253
1254   /* If we got here, we didn't get an exact match.  */
1255
1256   *exact_match = 0;
1257   return best_index;
1258 }
1259
1260 int
1261 find_pc_line_pc_range (pc, startptr, endptr)
1262      CORE_ADDR pc;
1263      CORE_ADDR *startptr, *endptr;
1264 {
1265   struct symtab_and_line sal;
1266   sal = find_pc_line (pc, 0);
1267   *startptr = sal.pc;
1268   *endptr = sal.end;
1269   return sal.symtab != 0;
1270 }
1271 \f
1272 struct type *
1273 find_nested_type (type, name)
1274      struct type *type;
1275      char *name;
1276 {
1277   int i;
1278   for (i = TYPE_NFIELDS (type) - 1; i >= TYPE_N_BASECLASSES (type); i--)
1279     {
1280       char *t_field_name = TYPE_FIELD_NAME (type, i);
1281
1282       if (t_field_name && !strcmp (t_field_name, name))
1283         if (TYPE_FIELD_NESTED (type, i))
1284           {
1285             return TYPE_FIELD_TYPE (type, i);
1286           }
1287     }
1288   for (i = TYPE_N_BASECLASSES (type) - 1; i >= 0; i--)
1289     {
1290       struct type * t = find_nested_type (TYPE_BASECLASS (type, i), name);
1291       if (t)
1292         return t;
1293     }
1294   return NULL;
1295 }
1296
1297 /* If P is of the form "operator[ \t]+..." where `...' is
1298    some legitimate operator text, return a pointer to the
1299    beginning of the substring of the operator text.
1300    Otherwise, return "".  */
1301 static char *
1302 operator_chars (p, end)
1303      char *p;
1304      char **end;
1305 {
1306   *end = "";
1307   if (strncmp (p, "operator", 8))
1308     return *end;
1309   p += 8;
1310
1311   /* Don't get faked out by `operator' being part of a longer
1312      identifier.  */
1313   if (isalpha(*p) || *p == '_' || *p == '$' || *p == '\0')
1314     return *end;
1315
1316   /* Allow some whitespace between `operator' and the operator symbol.  */
1317   while (*p == ' ' || *p == '\t')
1318     p++;
1319
1320   /* Recognize 'operator TYPENAME'. */
1321
1322   if (isalpha(*p) || *p == '_' || *p == '$')
1323     {
1324       register char *q = p+1;
1325       while (isalnum(*q) || *q == '_' || *q == '$')
1326         q++;
1327       *end = q;
1328       return p;
1329     }
1330
1331   switch (*p)
1332     {
1333     case '!':
1334     case '=':
1335     case '*':
1336     case '/':
1337     case '%':
1338     case '^':
1339       if (p[1] == '=')
1340         *end = p+2;
1341       else
1342         *end = p+1;
1343       return p;
1344     case '<':
1345     case '>':
1346     case '+':
1347     case '-':
1348     case '&':
1349     case '|':
1350       if (p[1] == '=' || p[1] == p[0])
1351         *end = p+2;
1352       else
1353         *end = p+1;
1354       return p;
1355     case '~':
1356     case ',':
1357       *end = p+1;
1358       return p;
1359     case '(':
1360       if (p[1] != ')')
1361         error ("`operator ()' must be specified without whitespace in `()'");
1362       *end = p+2;
1363       return p;
1364     case '?':
1365       if (p[1] != ':')
1366         error ("`operator ?:' must be specified without whitespace in `?:'");
1367       *end = p+2;
1368       return p;
1369     case '[':
1370       if (p[1] != ']')
1371         error ("`operator []' must be specified without whitespace in `[]'");
1372       *end = p+2;
1373       return p;
1374     default:
1375       error ("`operator %s' not supported", p);
1376       break;
1377     }
1378   *end = "";
1379   return *end;
1380 }
1381
1382 /* Recursive helper function for decode_line_1.
1383  * Look for methods named NAME in type T.
1384  * Return number of matches.
1385  * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1386  * These allocations seem to define "big enough":
1387  * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1388  * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1389  */
1390
1391 int
1392 find_methods (t, name, physnames, sym_arr)
1393      struct type *t;
1394      char *name;
1395      char **physnames;
1396      struct symbol **sym_arr;
1397 {
1398   int i1 = 0;
1399   int ibase;
1400   struct symbol *sym_class;
1401   char *class_name = type_name_no_tag (t);
1402   /* Ignore this class if it doesn't have a name.
1403      This prevents core dumps, but is just a workaround
1404      because we might not find the function in
1405      certain cases, such as
1406      struct D {virtual int f();}
1407      struct C : D {virtual int g();}
1408      (in this case g++ 1.35.1- does not put out a name
1409      for D as such, it defines type 19 (for example) in
1410      the same stab as C, and then does a
1411      .stabs "D:T19" and a .stabs "D:t19".
1412      Thus
1413      "break C::f" should not be looking for field f in
1414      the class named D, 
1415      but just for the field f in the baseclasses of C
1416      (no matter what their names).
1417      
1418      However, I don't know how to replace the code below
1419      that depends on knowing the name of D.  */
1420   if (class_name
1421       && (sym_class = lookup_symbol (class_name,
1422                                      (struct block *)NULL,
1423                                      STRUCT_NAMESPACE,
1424                                      (int *)NULL,
1425                                      (struct symtab **)NULL)))
1426     {
1427       int method_counter;
1428       t = SYMBOL_TYPE (sym_class);
1429       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1430            method_counter >= 0;
1431            --method_counter)
1432         {
1433           int field_counter;
1434           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1435
1436           char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1437           if (!strcmp (name, method_name))
1438             /* Find all the fields with that name.  */
1439             for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1440                  field_counter >= 0;
1441                  --field_counter)
1442               {
1443                 char *phys_name;
1444                 if (TYPE_FN_FIELD_STUB (f, field_counter))
1445                   check_stub_method (t, method_counter, field_counter);
1446                 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1447                 physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1448                 strcpy (physnames[i1], phys_name);
1449                 sym_arr[i1] = lookup_symbol (phys_name,
1450                                              SYMBOL_BLOCK_VALUE (sym_class),
1451                                              VAR_NAMESPACE,
1452                                              (int *) NULL,
1453                                              (struct symtab **) NULL);
1454                 if (sym_arr[i1]) i1++;
1455                 else
1456                   {
1457                     fputs_filtered("(Cannot find method ", stdout);
1458                     fputs_demangled(phys_name, stdout, DMGL_PARAMS);
1459                     fputs_filtered(" - possibly inlined.)\n", stdout);
1460                   }
1461               }
1462         }
1463     }
1464   /* Only search baseclasses if there is no match yet,
1465    * since names in derived classes override those in baseclasses.
1466    */
1467   if (i1)
1468     return i1;
1469   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1470     i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1471                        physnames + i1, sym_arr + i1);
1472   return i1;
1473 }
1474
1475 /* Parse a string that specifies a line number.
1476    Pass the address of a char * variable; that variable will be
1477    advanced over the characters actually parsed.
1478
1479    The string can be:
1480
1481    LINENUM -- that line number in current file.  PC returned is 0.
1482    FILE:LINENUM -- that line in that file.  PC returned is 0.
1483    FUNCTION -- line number of openbrace of that function.
1484       PC returned is the start of the function.
1485    VARIABLE -- line number of definition of that variable.
1486       PC returned is 0.
1487    FILE:FUNCTION -- likewise, but prefer functions in that file.
1488    *EXPR -- line in which address EXPR appears.
1489
1490    FUNCTION may be an undebuggable function found in minimal symbol table.
1491
1492    If the argument FUNFIRSTLINE is nonzero, we want the first line
1493    of real code inside a function when a function is specified.
1494
1495    DEFAULT_SYMTAB specifies the file to use if none is specified.
1496    It defaults to current_source_symtab.
1497    DEFAULT_LINE specifies the line number to use for relative
1498    line numbers (that start with signs).  Defaults to current_source_line.
1499
1500    Note that it is possible to return zero for the symtab
1501    if no file is validly specified.  Callers must check that.
1502    Also, the line number returned may be invalid.  */
1503
1504 struct symtabs_and_lines
1505 decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1506      char **argptr;
1507      int funfirstline;
1508      struct symtab *default_symtab;
1509      int default_line;
1510 {
1511   struct symtabs_and_lines values;
1512   struct symtab_and_line val;
1513   register char *p, *p1;
1514   char *q, *q1;
1515   register struct symtab *s;
1516
1517   register struct symbol *sym;
1518   /* The symtab that SYM was found in.  */
1519   struct symtab *sym_symtab;
1520
1521   register CORE_ADDR pc;
1522   register struct minimal_symbol *msymbol;
1523   char *copy;
1524   struct symbol *sym_class;
1525   int i1;
1526   int is_quoted;
1527   struct symbol **sym_arr;
1528   struct type *t;
1529   char **physnames;
1530   char *saved_arg = *argptr;
1531   extern char *gdb_completer_quote_characters;
1532   
1533   /* Defaults have defaults.  */
1534
1535   if (default_symtab == 0)
1536     {
1537       default_symtab = current_source_symtab;
1538       default_line = current_source_line;
1539     }
1540
1541   /* See if arg is *PC */
1542
1543   if (**argptr == '*')
1544     {
1545       if (**argptr == '*')
1546         {
1547           (*argptr)++;
1548         }
1549       pc = parse_and_eval_address_1 (argptr);
1550       values.sals = (struct symtab_and_line *)
1551         xmalloc (sizeof (struct symtab_and_line));
1552       values.nelts = 1;
1553       values.sals[0] = find_pc_line (pc, 0);
1554       values.sals[0].pc = pc;
1555       return values;
1556     }
1557
1558   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1559
1560   s = NULL;
1561   is_quoted = (strchr (gdb_completer_quote_characters, **argptr) != NULL);
1562
1563   for (p = *argptr; *p; p++)
1564     {
1565       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1566         break;
1567     }
1568   while (p[0] == ' ' || p[0] == '\t') p++;
1569
1570   if ((p[0] == ':') && !is_quoted)
1571     {
1572
1573       /*  C++  */
1574       if (p[1] ==':')
1575         {
1576           /* Extract the class name.  */
1577           p1 = p;
1578           while (p != *argptr && p[-1] == ' ') --p;
1579           copy = (char *) alloca (p - *argptr + 1);
1580           memcpy (copy, *argptr, p - *argptr);
1581           copy[p - *argptr] = 0;
1582
1583           /* Discard the class name from the arg.  */
1584           p = p1 + 2;
1585           while (*p == ' ' || *p == '\t') p++;
1586           *argptr = p;
1587
1588           sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0, 
1589                                      (struct symtab **)NULL);
1590        
1591           if (sym_class &&
1592               (   TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1593                || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1594             {
1595               /* Arg token is not digits => try it as a function name
1596                  Find the next token (everything up to end or next whitespace). */
1597               p = *argptr;
1598               while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
1599               q = operator_chars (*argptr, &q1);
1600
1601               if (q1 - q)
1602                 {
1603                   char *opname;
1604                   char *tmp = alloca (q1 - q + 1);
1605                   memcpy (tmp, q, q1 - q);
1606                   tmp[q1 - q] = '\0';
1607                   opname = cplus_mangle_opname (tmp, DMGL_ANSI);
1608                   if (opname == NULL)
1609                     {
1610                       warning ("no mangling for \"%s\"", tmp);
1611                       cplusplus_hint (saved_arg);
1612                       return_to_top_level ();
1613                     }
1614                   copy = (char*) alloca (3 + strlen(opname));
1615                   sprintf (copy, "__%s", opname);
1616                   p = q1;
1617                 }
1618               else
1619                 {
1620                   copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
1621                   memcpy (copy, *argptr, p - *argptr);
1622                   copy[p - *argptr] = '\0';
1623                 }
1624
1625               /* no line number may be specified */
1626               while (*p == ' ' || *p == '\t') p++;
1627               *argptr = p;
1628
1629               sym = 0;
1630               i1 = 0;           /*  counter for the symbol array */
1631               t = SYMBOL_TYPE (sym_class);
1632               sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1633               physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1634
1635               if (destructor_name_p (copy, t))
1636                 {
1637                   /* destructors are a special case.  */
1638                   struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1639                   int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1640                   char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1641                   physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1642                   strcpy (physnames[i1], phys_name);
1643                   sym_arr[i1] =
1644                     lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
1645                                    VAR_NAMESPACE, 0, (struct symtab **)NULL);
1646                   if (sym_arr[i1]) i1++;
1647                 }
1648               else
1649                 i1 = find_methods (t, copy, physnames, sym_arr);
1650               if (i1 == 1)
1651                 {
1652                   /* There is exactly one field with that name.  */
1653                   sym = sym_arr[0];
1654
1655                   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1656                     {
1657                       /* Arg is the name of a function */
1658                       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1659                       if (funfirstline)
1660                         SKIP_PROLOGUE (pc);
1661                       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1662                       values.nelts = 1;
1663                       values.sals[0] = find_pc_line (pc, 0);
1664                       values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1665                     }
1666                   else
1667                     {
1668                       values.nelts = 0;
1669                     }
1670                   return values;
1671                 }
1672               if (i1 > 0)
1673                 {
1674                   /* There is more than one field with that name
1675                      (overloaded).  Ask the user which one to use.  */
1676                   return decode_line_2 (sym_arr, i1, funfirstline);
1677                 }
1678               else
1679                 {
1680                   char *tmp;
1681
1682                   if (OPNAME_PREFIX_P (copy))
1683                     {
1684                       tmp = (char *)alloca (strlen (copy+3) + 9);
1685                       strcpy (tmp, "operator ");
1686                       strcat (tmp, copy+3);
1687                     }
1688                   else
1689                     tmp = copy;
1690                   if (tmp[0] == '~')
1691                     warning ("the class `%s' does not have destructor defined",
1692                              sym_class->name);
1693                   else
1694                     warning ("the class %s does not have any method named %s",
1695                              sym_class->name, tmp);
1696                   cplusplus_hint (saved_arg);
1697                   return_to_top_level ();
1698                 }
1699             }
1700           else
1701             {
1702               /* The quotes are important if copy is empty.  */
1703               warning ("can't find class, struct, or union named \"%s\"",
1704                        copy);
1705               cplusplus_hint (saved_arg);
1706               return_to_top_level ();
1707             }
1708         }
1709       /*  end of C++  */
1710
1711
1712       /* Extract the file name.  */
1713       p1 = p;
1714       while (p != *argptr && p[-1] == ' ') --p;
1715       copy = (char *) alloca (p - *argptr + 1);
1716       memcpy (copy, *argptr, p - *argptr);
1717       copy[p - *argptr] = 0;
1718
1719       /* Find that file's data.  */
1720       s = lookup_symtab (copy);
1721       if (s == 0)
1722         {
1723           if (!have_full_symbols () && !have_partial_symbols ())
1724             error (no_symtab_msg);
1725           error ("No source file named %s.", copy);
1726         }
1727
1728       /* Discard the file name from the arg.  */
1729       p = p1 + 1;
1730       while (*p == ' ' || *p == '\t') p++;
1731       *argptr = p;
1732     }
1733
1734   /* S is specified file's symtab, or 0 if no file specified.
1735      arg no longer contains the file name.  */
1736
1737   /* Check whether arg is all digits (and sign) */
1738
1739   p = *argptr;
1740   if (*p == '-' || *p == '+') p++;
1741   while (*p >= '0' && *p <= '9')
1742     p++;
1743
1744   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
1745     {
1746       /* We found a token consisting of all digits -- at least one digit.  */
1747       enum sign {none, plus, minus} sign = none;
1748
1749       /* This is where we need to make sure that we have good defaults.
1750          We must guarantee that this section of code is never executed
1751          when we are called with just a function name, since
1752          select_source_symtab calls us with such an argument  */
1753
1754       if (s == 0 && default_symtab == 0)
1755         {
1756           select_source_symtab (0);
1757           default_symtab = current_source_symtab;
1758           default_line = current_source_line;
1759         }
1760
1761       if (**argptr == '+')
1762         sign = plus, (*argptr)++;
1763       else if (**argptr == '-')
1764         sign = minus, (*argptr)++;
1765       val.line = atoi (*argptr);
1766       switch (sign)
1767         {
1768         case plus:
1769           if (p == *argptr)
1770             val.line = 5;
1771           if (s == 0)
1772             val.line = default_line + val.line;
1773           break;
1774         case minus:
1775           if (p == *argptr)
1776             val.line = 15;
1777           if (s == 0)
1778             val.line = default_line - val.line;
1779           else
1780             val.line = 1;
1781           break;
1782         case none:
1783           break;        /* No need to adjust val.line.  */
1784         }
1785
1786       while (*p == ' ' || *p == '\t') p++;
1787       *argptr = p;
1788       if (s == 0)
1789         s = default_symtab;
1790       val.symtab = s;
1791       val.pc = 0;
1792       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1793       values.sals[0] = val;
1794       values.nelts = 1;
1795       return values;
1796     }
1797
1798   /* Arg token is not digits => try it as a variable name
1799      Find the next token (everything up to end or next whitespace).  */
1800
1801   p = skip_quoted (*argptr);
1802   copy = (char *) alloca (p - *argptr + 1);
1803   memcpy (copy, *argptr, p - *argptr);
1804   copy[p - *argptr] = '\0';
1805   if ((copy[0] == copy [p - *argptr - 1])
1806       && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
1807     {
1808       char *temp;
1809       copy [p - *argptr - 1] = '\0';
1810       copy++;
1811       if ((temp = expensive_mangler (copy)) != NULL)
1812         {
1813           copy = temp;
1814         }
1815     }
1816   while (*p == ' ' || *p == '\t') p++;
1817   *argptr = p;
1818
1819   /* Look up that token as a variable.
1820      If file specified, use that file's per-file block to start with.  */
1821
1822   sym = lookup_symbol (copy,
1823                        (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
1824                         : get_selected_block ()),
1825                        VAR_NAMESPACE, 0, &sym_symtab);
1826
1827   if (sym != NULL)
1828     {
1829       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1830         {
1831           /* Arg is the name of a function */
1832           pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1833           if (funfirstline)
1834             SKIP_PROLOGUE (pc);
1835           val = find_pc_line (pc, 0);
1836 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1837           /* Convex: no need to suppress code on first line, if any */
1838           val.pc = pc;
1839 #else
1840           /* If SKIP_PROLOGUE left us in mid-line, and the next line is still
1841              part of the same function:
1842                 advance to next line, 
1843                 recalculate its line number (might not be N+1).  */
1844           if (val.pc != pc && val.end &&
1845               lookup_minimal_symbol_by_pc (pc) == lookup_minimal_symbol_by_pc (val.end)) {
1846             pc = val.end;       /* First pc of next line */
1847             val = find_pc_line (pc, 0);
1848           }
1849           val.pc = pc;
1850 #endif
1851           values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1852           values.sals[0] = val;
1853           values.nelts = 1;
1854           
1855           /* I think this is always the same as the line that
1856              we calculate above, but the general principle is
1857              "trust the symbols more than stuff like
1858              SKIP_PROLOGUE".  */
1859           if (SYMBOL_LINE (sym) != 0)
1860             values.sals[0].line = SYMBOL_LINE (sym);
1861           
1862           return values;
1863         }
1864       else if (SYMBOL_LINE (sym) != 0)
1865         {
1866           /* We know its line number.  */
1867           values.sals = (struct symtab_and_line *)
1868             xmalloc (sizeof (struct symtab_and_line));
1869           values.nelts = 1;
1870           memset (&values.sals[0], 0, sizeof (values.sals[0]));
1871           values.sals[0].symtab = sym_symtab;
1872           values.sals[0].line = SYMBOL_LINE (sym);
1873           return values;
1874         }
1875       else
1876         /* This can happen if it is compiled with a compiler which doesn't
1877            put out line numbers for variables.  */
1878         error ("Line number not known for symbol \"%s\"", copy);
1879     }
1880
1881   msymbol = lookup_minimal_symbol (copy, (struct objfile *) NULL);
1882   if (msymbol != NULL)
1883     {
1884       val.symtab = 0;
1885       val.line = 0;
1886       val.pc = msymbol -> address + FUNCTION_START_OFFSET;
1887       if (funfirstline)
1888         SKIP_PROLOGUE (val.pc);
1889       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1890       values.sals[0] = val;
1891       values.nelts = 1;
1892       return values;
1893     }
1894
1895   if (!have_full_symbols () &&
1896       !have_partial_symbols () && !have_minimal_symbols ())
1897     error (no_symtab_msg);
1898
1899   error ("Function \"%s\" not defined.", copy);
1900   return values;        /* for lint */
1901 }
1902
1903 struct symtabs_and_lines
1904 decode_line_spec (string, funfirstline)
1905      char *string;
1906      int funfirstline;
1907 {
1908   struct symtabs_and_lines sals;
1909   if (string == 0)
1910     error ("Empty line specification.");
1911   sals = decode_line_1 (&string, funfirstline,
1912                         current_source_symtab, current_source_line);
1913   if (*string)
1914     error ("Junk at end of line specification: %s", string);
1915   return sals;
1916 }
1917
1918 /* Given a list of NELTS symbols in sym_arr (with corresponding
1919    mangled names in physnames), return a list of lines to operate on
1920    (ask user if necessary).  */
1921 static struct symtabs_and_lines
1922 decode_line_2 (sym_arr, nelts, funfirstline)
1923      struct symbol *sym_arr[];
1924      int nelts;
1925      int funfirstline;
1926 {
1927   struct symtabs_and_lines values, return_values;
1928   register CORE_ADDR pc;
1929   char *args, *arg1;
1930   int i;
1931   char *prompt;
1932   char *demangled;
1933
1934   values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
1935   return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
1936
1937   i = 0;
1938   printf("[0] cancel\n[1] all\n");
1939   while (i < nelts)
1940     {
1941       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
1942         {
1943           /* Arg is the name of a function */
1944           pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i])) 
1945                + FUNCTION_START_OFFSET;
1946           if (funfirstline)
1947             SKIP_PROLOGUE (pc);
1948           values.sals[i] = find_pc_line (pc, 0);
1949           values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
1950                                values.sals[i].end                      :  pc;
1951           demangled = cplus_demangle (SYMBOL_NAME (sym_arr[i]),
1952                                       DMGL_PARAMS | DMGL_ANSI);
1953           printf("[%d] %s at %s:%d\n", (i+2),
1954                  demangled ? demangled : SYMBOL_NAME (sym_arr[i]), 
1955                  values.sals[i].symtab->filename, values.sals[i].line);
1956           if (demangled != NULL)
1957             {
1958               free (demangled);
1959             }
1960         }
1961       else printf ("?HERE\n");
1962       i++;
1963     }
1964   
1965   if ((prompt = getenv ("PS2")) == NULL)
1966     {
1967       prompt = ">";
1968     }
1969   printf("%s ",prompt);
1970   fflush(stdout);
1971
1972   args = command_line_input ((char *) NULL, 0);
1973   
1974   if (args == 0)
1975     error_no_arg ("one or more choice numbers");
1976
1977   i = 0;
1978   while (*args)
1979     {
1980       int num;
1981
1982       arg1 = args;
1983       while (*arg1 >= '0' && *arg1 <= '9') arg1++;
1984       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
1985         error ("Arguments must be choice numbers.");
1986
1987       num = atoi (args);
1988
1989       if (num == 0)
1990         error ("cancelled");
1991       else if (num == 1)
1992         {
1993           memcpy (return_values.sals, values.sals,
1994                   (nelts * sizeof(struct symtab_and_line)));
1995           return_values.nelts = nelts;
1996           return return_values;
1997         }
1998
1999       if (num > nelts + 2)
2000         {
2001           printf ("No choice number %d.\n", num);
2002         }
2003       else
2004         {
2005           num -= 2;
2006           if (values.sals[num].pc)
2007             {
2008               return_values.sals[i++] = values.sals[num];
2009               values.sals[num].pc = 0;
2010             }
2011           else
2012             {
2013               printf ("duplicate request for %d ignored.\n", num);
2014             }
2015         }
2016
2017       args = arg1;
2018       while (*args == ' ' || *args == '\t') args++;
2019     }
2020   return_values.nelts = i;
2021   return return_values;
2022 }
2023
2024 \f
2025 /* Slave routine for sources_info.  Force line breaks at ,'s.
2026    NAME is the name to print and *FIRST is nonzero if this is the first
2027    name printed.  Set *FIRST to zero.  */
2028 static void
2029 output_source_filename (name, first)
2030      char *name;
2031      int *first;
2032 {
2033   /* Table of files printed so far.  Since a single source file can
2034      result in several partial symbol tables, we need to avoid printing
2035      it more than once.  Note: if some of the psymtabs are read in and
2036      some are not, it gets printed both under "Source files for which
2037      symbols have been read" and "Source files for which symbols will
2038      be read in on demand".  I consider this a reasonable way to deal
2039      with the situation.  I'm not sure whether this can also happen for
2040      symtabs; it doesn't hurt to check.  */
2041   static char **tab = NULL;
2042   /* Allocated size of tab in elements.
2043      Start with one 256-byte block (when using GNU malloc.c).
2044      24 is the malloc overhead when range checking is in effect.  */
2045   static int tab_alloc_size = (256 - 24) / sizeof (char *);
2046   /* Current size of tab in elements.  */
2047   static int tab_cur_size;
2048
2049   char **p;
2050
2051   if (*first)
2052     {
2053       if (tab == NULL)
2054         tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2055       tab_cur_size = 0;
2056     }
2057
2058   /* Is NAME in tab?  */
2059   for (p = tab; p < tab + tab_cur_size; p++)
2060     if (strcmp (*p, name) == 0)
2061       /* Yes; don't print it again.  */
2062       return;
2063   /* No; add it to tab.  */
2064   if (tab_cur_size == tab_alloc_size)
2065     {
2066       tab_alloc_size *= 2;
2067       tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
2068     }
2069   tab[tab_cur_size++] = name;
2070
2071   if (*first)
2072     {
2073       *first = 0;
2074     }
2075   else
2076     {
2077       printf_filtered (", ");
2078     }
2079
2080   wrap_here ("");
2081   fputs_filtered (name, stdout);
2082 }  
2083
2084 static void
2085 sources_info (ignore, from_tty)
2086      char *ignore;
2087      int from_tty;
2088 {
2089   register struct symtab *s;
2090   register struct partial_symtab *ps;
2091   register struct objfile *objfile;
2092   int first;
2093   
2094   if (!have_full_symbols () && !have_partial_symbols ())
2095     {
2096       error (no_symtab_msg);
2097     }
2098   
2099   printf_filtered ("Source files for which symbols have been read in:\n\n");
2100
2101   first = 1;
2102   ALL_SYMTABS (objfile, s)
2103     {
2104       output_source_filename (s -> filename, &first);
2105     }
2106   printf_filtered ("\n\n");
2107   
2108   printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2109
2110   first = 1;
2111   ALL_PSYMTABS (objfile, ps)
2112     {
2113       if (!ps->readin)
2114         {
2115           output_source_filename (ps -> filename, &first);
2116         }
2117     }
2118   printf_filtered ("\n");
2119 }
2120
2121 static int
2122 name_match (name)
2123      char *name;
2124 {
2125   char *demangled = cplus_demangle (name, DMGL_ANSI);
2126   if (demangled != NULL)
2127     {
2128       int cond = re_exec (demangled);
2129       free (demangled);
2130       return (cond);
2131     }
2132   return (re_exec (name));
2133 }
2134 #define NAME_MATCH(NAME) name_match(NAME)
2135
2136 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2137    If CLASS is zero, list all symbols except functions, type names, and
2138                      constants (enums).
2139    If CLASS is 1, list only functions.
2140    If CLASS is 2, list only type names.
2141    If CLASS is 3, list only method names.
2142
2143    BPT is non-zero if we should set a breakpoint at the functions
2144    we find.  */
2145
2146 static void
2147 list_symbols (regexp, class, bpt)
2148      char *regexp;
2149      int class;
2150      int bpt;
2151 {
2152   register struct symtab *s;
2153   register struct partial_symtab *ps;
2154   register struct blockvector *bv;
2155   struct blockvector *prev_bv = 0;
2156   register struct block *b;
2157   register int i, j;
2158   register struct symbol *sym;
2159   struct partial_symbol *psym;
2160   struct objfile *objfile;
2161   struct minimal_symbol *msymbol;
2162   char *val;
2163   static char *classnames[]
2164     = {"variable", "function", "type", "method"};
2165   int found_in_file = 0;
2166   int found_misc = 0;
2167   static enum minimal_symbol_type types[]
2168     = {mst_data, mst_text, mst_abs, mst_unknown};
2169   static enum minimal_symbol_type types2[]
2170     = {mst_bss,  mst_text, mst_abs, mst_unknown};
2171   enum minimal_symbol_type ourtype = types[class];
2172   enum minimal_symbol_type ourtype2 = types2[class];
2173
2174   if (regexp)
2175     {
2176       /* Make sure spacing is right for C++ operators.
2177          This is just a courtesy to make the matching less sensitive
2178          to how many spaces the user leaves between 'operator'
2179          and <TYPENAME> or <OPERATOR>. */
2180       char *opend;
2181       char *opname = operator_chars (regexp, &opend);
2182       if (*opname)
2183         {
2184           int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2185           if (isalpha(*opname) || *opname == '_' || *opname == '$')
2186             {
2187               /* There should 1 space between 'operator' and 'TYPENAME'. */
2188               if (opname[-1] != ' ' || opname[-2] == ' ')
2189                 fix = 1;
2190             }
2191           else
2192             {
2193               /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2194               if (opname[-1] == ' ')
2195                 fix = 0;
2196             }
2197           /* If wrong number of spaces, fix it. */
2198           if (fix >= 0)
2199             {
2200               char *tmp = (char*) alloca(opend-opname+10);
2201               sprintf(tmp, "operator%.*s%s", fix, " ", opname);
2202               regexp = tmp;
2203             }
2204         }
2205       
2206       if (0 != (val = re_comp (regexp)))
2207         error ("Invalid regexp (%s): %s", val, regexp);
2208     }
2209
2210   /* Search through the partial symtabs *first* for all symbols
2211      matching the regexp.  That way we don't have to reproduce all of
2212      the machinery below. */
2213
2214   ALL_PSYMTABS (objfile, ps)
2215     {
2216       struct partial_symbol *bound, *gbound, *sbound;
2217       int keep_going = 1;
2218       
2219       if (ps->readin) continue;
2220       
2221       gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2222       sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2223       bound = gbound;
2224       
2225       /* Go through all of the symbols stored in a partial
2226          symtab in one loop. */
2227       psym = objfile->global_psymbols.list + ps->globals_offset;
2228       while (keep_going)
2229         {
2230           if (psym >= bound)
2231             {
2232               if (bound == gbound && ps->n_static_syms != 0)
2233                 {
2234                   psym = objfile->static_psymbols.list + ps->statics_offset;
2235                   bound = sbound;
2236                 }
2237               else
2238                 keep_going = 0;
2239               continue;
2240             }
2241           else
2242             {
2243               QUIT;
2244
2245               /* If it would match (logic taken from loop below)
2246                  load the file and go on to the next one */
2247               if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (psym)))
2248                   && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2249                        && SYMBOL_CLASS (psym) != LOC_BLOCK)
2250                       || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2251                       || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2252                       || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2253                 {
2254                   PSYMTAB_TO_SYMTAB(ps);
2255                   keep_going = 0;
2256                 }
2257             }
2258           psym++;
2259         }
2260     }
2261
2262   /* Here, we search through the minimal symbol tables for functions that
2263      match, and call find_pc_symtab on them to force their symbols to
2264      be read.  The symbol will then be found during the scan of symtabs
2265      below.  If find_pc_symtab fails, set found_misc so that we will
2266      rescan to print any matching symbols without debug info.  */
2267
2268   if (class == 1)
2269     {
2270       ALL_MSYMBOLS (objfile, msymbol)
2271         {
2272           if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2273             {
2274               if (regexp == 0 || NAME_MATCH (msymbol -> name))
2275                 {
2276                   if (0 == find_pc_symtab (msymbol -> address))
2277                     {
2278                       found_misc = 1;
2279                     }
2280                 }
2281             }
2282         }
2283     }
2284
2285   /* Printout here so as to get after the "Reading in symbols"
2286      messages which will be generated above.  */
2287   if (!bpt)
2288     printf_filtered (regexp
2289           ? "All %ss matching regular expression \"%s\":\n"
2290           : "All defined %ss:\n",
2291           classnames[class],
2292           regexp);
2293
2294   ALL_SYMTABS (objfile, s)
2295     {
2296       found_in_file = 0;
2297       bv = BLOCKVECTOR (s);
2298       /* Often many files share a blockvector.
2299          Scan each blockvector only once so that
2300          we don't get every symbol many times.
2301          It happens that the first symtab in the list
2302          for any given blockvector is the main file.  */
2303       if (bv != prev_bv)
2304         for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2305           {
2306             b = BLOCKVECTOR_BLOCK (bv, i);
2307             /* Skip the sort if this block is always sorted.  */
2308             if (!BLOCK_SHOULD_SORT (b))
2309               sort_block_syms (b);
2310             for (j = 0; j < BLOCK_NSYMS (b); j++)
2311               {
2312                 QUIT;
2313                 sym = BLOCK_SYM (b, j);
2314                 if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (sym)))
2315                     && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2316                          && SYMBOL_CLASS (sym) != LOC_BLOCK
2317                          && SYMBOL_CLASS (sym) != LOC_CONST)
2318                         || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2319                         || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2320                         || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2321                   {
2322                     if (bpt)
2323                       {
2324                         /* Set a breakpoint here, if it's a function */
2325                         if (class == 1)
2326                           break_command (SYMBOL_NAME(sym), 0);
2327                       }
2328                     else if (!found_in_file)
2329                       {
2330                         fputs_filtered ("\nFile ", stdout);
2331                         fputs_filtered (s->filename, stdout);
2332                         fputs_filtered (":\n", stdout);
2333                       }
2334                     found_in_file = 1;
2335                     
2336                     if (class != 2 && i == STATIC_BLOCK)
2337                       printf_filtered ("static ");
2338                     
2339                     /* Typedef that is not a C++ class */
2340                     if (class == 2
2341                         && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2342                       typedef_print (SYMBOL_TYPE(sym), sym, stdout);
2343                     /* variable, func, or typedef-that-is-c++-class */
2344                     else if (class < 2 || 
2345                              (class == 2 && 
2346                               SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
2347                       {
2348                         type_print (SYMBOL_TYPE (sym),
2349                                     (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2350                                      ? "" : SYMBOL_NAME (sym)),
2351                                     stdout, 0);
2352                         
2353                         printf_filtered (";\n");
2354                       }
2355                     else
2356                       {
2357 # if 0
2358 /* FIXME, why is this zapped out? */
2359                         char buf[1024];
2360                         type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0); 
2361                         type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0); 
2362                         sprintf (buf, " %s::", type_name_no_tag (t));
2363                         type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2364 # endif
2365                       }
2366                   }
2367               }
2368           }
2369       prev_bv = bv;
2370     }
2371
2372   /* If there are no eyes, avoid all contact.  I mean, if there are
2373      no debug symbols, then print directly from the msymbol_vector.  */
2374
2375   if (found_misc || class != 1)
2376     {
2377       found_in_file = 0;
2378       ALL_MSYMBOLS (objfile, msymbol)
2379         {
2380           if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2381             {
2382               if (regexp == 0 || NAME_MATCH (msymbol -> name))
2383                 {
2384                   /* Functions:  Look up by address. */
2385                   if (class != 1 ||
2386                       (0 == find_pc_symtab (msymbol -> address)))
2387                     {
2388                       /* Variables/Absolutes:  Look up by name */
2389                       if (lookup_symbol (msymbol -> name, 
2390                                          (struct block *) 0, VAR_NAMESPACE, 0,
2391                                          (struct symtab **) 0) == NULL)
2392                         {
2393                           if (!found_in_file)
2394                             {
2395                               printf_filtered ("\nNon-debugging symbols:\n");
2396                               found_in_file = 1;
2397                             }
2398                           printf_filtered ("    %08x  %s\n",
2399                                            msymbol -> address,
2400                                            msymbol -> name);
2401                         }
2402                     }
2403                 }
2404             }
2405         }
2406     }
2407 }
2408
2409 static void
2410 variables_info (regexp, from_tty)
2411      char *regexp;
2412      int from_tty;
2413 {
2414   list_symbols (regexp, 0, 0);
2415 }
2416
2417 static void
2418 functions_info (regexp, from_tty)
2419      char *regexp;
2420      int from_tty;
2421 {
2422   list_symbols (regexp, 1, 0);
2423 }
2424
2425 static void
2426 types_info (regexp, from_tty)
2427      char *regexp;
2428      int from_tty;
2429 {
2430   list_symbols (regexp, 2, 0);
2431 }
2432
2433 #if 0
2434 /* Tiemann says: "info methods was never implemented."  */
2435 static void
2436 methods_info (regexp)
2437      char *regexp;
2438 {
2439   list_symbols (regexp, 3, 0);
2440 }
2441 #endif /* 0 */
2442
2443 /* Breakpoint all functions matching regular expression. */
2444 static void
2445 rbreak_command (regexp, from_tty)
2446      char *regexp;
2447      int from_tty;
2448 {
2449   list_symbols (regexp, 1, 1);
2450 }
2451 \f
2452
2453 /* Return Nonzero if block a is lexically nested within block b,
2454    or if a and b have the same pc range.
2455    Return zero otherwise. */
2456 int
2457 contained_in (a, b)
2458      struct block *a, *b;
2459 {
2460   if (!a || !b)
2461     return 0;
2462   return BLOCK_START (a) >= BLOCK_START (b)
2463       && BLOCK_END (a)   <= BLOCK_END (b);
2464 }
2465
2466 \f
2467 /* Helper routine for make_symbol_completion_list.  */
2468
2469 static int return_val_size;
2470 static int return_val_index;
2471 static char **return_val;
2472
2473 /*  Test to see if the symbol specified by SYMNAME (or it's demangled
2474     equivalent) matches TEXT in the first TEXT_LEN characters.  If so,
2475     add it to the current completion list. */
2476
2477 static void
2478 completion_list_add_symbol (symname, text, text_len)
2479      char *symname;
2480      char *text;
2481      int text_len;
2482 {
2483   char *demangled;
2484   int newsize;
2485
2486   /* First see if SYMNAME is a C++ mangled name, and if so, use the
2487      demangled name instead, including any parameters. */
2488
2489   if ((demangled = cplus_demangle (symname, DMGL_PARAMS | DMGL_ANSI)) != NULL)
2490     {
2491       symname = demangled;
2492     }
2493
2494   /* If we have a match for a completion, then add SYMNAME to the current
2495      list of matches. Note that we always make a copy of the string, even
2496      if it is one that was returned from cplus_demangle and is already
2497      in malloc'd memory. */
2498
2499   if (strncmp (symname, text, text_len) == 0)
2500     {
2501       if (return_val_index + 3 > return_val_size)
2502         {
2503           newsize = (return_val_size *= 2) * sizeof (char *);
2504           return_val = (char **) xrealloc ((char *) return_val, newsize);
2505         }
2506       return_val[return_val_index++] = savestring (symname, strlen (symname));
2507       return_val[return_val_index] = NULL;
2508     }
2509
2510   if (demangled != NULL)
2511     {
2512       free (demangled);
2513     }
2514 }
2515
2516 /* Return a NULL terminated array of all symbols (regardless of class) which
2517    begin by matching TEXT.  If the answer is no symbols, then the return value
2518    is an array which contains only a NULL pointer.
2519
2520    Problem: All of the symbols have to be copied because readline frees them.
2521    I'm not going to worry about this; hopefully there won't be that many.  */
2522
2523 char **
2524 make_symbol_completion_list (text)
2525   char *text;
2526 {
2527   register struct symbol *sym;
2528   register struct symtab *s;
2529   register struct partial_symtab *ps;
2530   register struct minimal_symbol *msymbol;
2531   register struct objfile *objfile;
2532   register struct block *b, *surrounding_static_block = 0;
2533   register int i, j;
2534   int text_len;
2535   struct partial_symbol *psym;
2536
2537   text_len = strlen (text);
2538   return_val_size = 100;
2539   return_val_index = 0;
2540   return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
2541   return_val[0] = NULL;
2542
2543   /* Look through the partial symtabs for all symbols which begin
2544      by matching TEXT.  Add each one that you find to the list.  */
2545
2546   ALL_PSYMTABS (objfile, ps)
2547     {
2548       /* If the psymtab's been read in we'll get it when we search
2549          through the blockvector.  */
2550       if (ps->readin) continue;
2551       
2552       for (psym = objfile->global_psymbols.list + ps->globals_offset;
2553            psym < (objfile->global_psymbols.list + ps->globals_offset
2554                    + ps->n_global_syms);
2555            psym++)
2556         {
2557           /* If interrupted, then quit. */
2558           QUIT;
2559           completion_list_add_symbol (SYMBOL_NAME (psym), text, text_len);
2560         }
2561       
2562       for (psym = objfile->static_psymbols.list + ps->statics_offset;
2563            psym < (objfile->static_psymbols.list + ps->statics_offset
2564                    + ps->n_static_syms);
2565            psym++)
2566         {
2567           QUIT;
2568           completion_list_add_symbol (SYMBOL_NAME (psym), text, text_len);
2569         }
2570     }
2571
2572   /* At this point scan through the misc symbol vectors and add each
2573      symbol you find to the list.  Eventually we want to ignore
2574      anything that isn't a text symbol (everything else will be
2575      handled by the psymtab code above).  */
2576
2577   ALL_MSYMBOLS (objfile, msymbol)
2578     {
2579       QUIT;
2580       completion_list_add_symbol (msymbol -> name, text, text_len);
2581     }
2582
2583   /* Search upwards from currently selected frame (so that we can
2584      complete on local vars.  */
2585
2586   for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
2587     {
2588       if (!BLOCK_SUPERBLOCK (b))
2589         {
2590           surrounding_static_block = b;         /* For elmin of dups */
2591         }
2592       
2593       /* Also catch fields of types defined in this places which match our
2594          text string.  Only complete on types visible from current context. */
2595
2596       for (i = 0; i < BLOCK_NSYMS (b); i++)
2597         {
2598           sym = BLOCK_SYM (b, i);
2599           completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2600           if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2601             {
2602               struct type *t = SYMBOL_TYPE (sym);
2603               enum type_code c = TYPE_CODE (t);
2604
2605               if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2606                 {
2607                   for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2608                     {
2609                       if (TYPE_FIELD_NAME (t, j))
2610                         {
2611                           completion_list_add_symbol (TYPE_FIELD_NAME (t, j),
2612                                                       text, text_len);
2613                         }
2614                     }
2615                 }
2616             }
2617         }
2618     }
2619
2620   /* Go through the symtabs and check the externs and statics for
2621      symbols which match.  */
2622
2623   ALL_SYMTABS (objfile, s)
2624     {
2625       QUIT;
2626       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2627       for (i = 0; i < BLOCK_NSYMS (b); i++)
2628         {
2629           sym = BLOCK_SYM (b, i);
2630           completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2631         }
2632     }
2633
2634   ALL_SYMTABS (objfile, s)
2635     {
2636       QUIT;
2637       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2638       /* Don't do this block twice.  */
2639       if (b == surrounding_static_block) continue;
2640       for (i = 0; i < BLOCK_NSYMS (b); i++)
2641         {
2642           sym = BLOCK_SYM (b, i);
2643           completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2644         }
2645     }
2646
2647   return (return_val);
2648 }
2649
2650 \f
2651 /* Find a mangled symbol that corresponds to LOOKFOR using brute force.
2652    Basically we go munging through available symbols, demangling each one,
2653    looking for a match on the demangled result. */
2654
2655 static char *
2656 expensive_mangler (lookfor)
2657      const char *lookfor;
2658 {
2659   register struct symbol *sym;
2660   register struct symtab *s;
2661   register struct partial_symtab *ps;
2662   register struct minimal_symbol *msymbol;
2663   register struct objfile *objfile;
2664   register struct block *b, *surrounding_static_block = 0;
2665   register int i, j;
2666   struct partial_symbol *psym;
2667   char *demangled;
2668
2669   /* Look through the partial symtabs for a symbol that matches */
2670
2671   ALL_PSYMTABS (objfile, ps)
2672     {
2673       /* If the psymtab's been read in we'll get it when we search
2674          through the blockvector.  */
2675       if (ps->readin) continue;
2676       
2677       for (psym = objfile->global_psymbols.list + ps->globals_offset;
2678            psym < (objfile->global_psymbols.list + ps->globals_offset
2679                    + ps->n_global_syms);
2680            psym++)
2681         {
2682           QUIT;                 /* If interrupted, then quit. */
2683           demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2684                                           DMGL_PARAMS | DMGL_ANSI);
2685           if (demangled != NULL)
2686             {
2687               free (demangled);
2688               return (SYMBOL_NAME (psym));
2689             }
2690         }
2691       
2692       for (psym = objfile->static_psymbols.list + ps->statics_offset;
2693            psym < (objfile->static_psymbols.list + ps->statics_offset
2694                    + ps->n_static_syms);
2695            psym++)
2696         {
2697           QUIT;
2698           demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2699                                           DMGL_PARAMS | DMGL_ANSI);
2700           if (demangled != NULL)
2701             {
2702               free (demangled);
2703               return (SYMBOL_NAME (psym));
2704             }
2705         }
2706     }
2707
2708   /* Scan through the misc symbol vectors looking for a match. */
2709
2710   ALL_MSYMBOLS (objfile, msymbol)
2711     {
2712       QUIT;
2713       demangled = demangle_and_match (msymbol -> name, lookfor,
2714                                       DMGL_PARAMS | DMGL_ANSI);
2715       if (demangled != NULL)
2716         {
2717           free (demangled);
2718           return (msymbol -> name);
2719         }
2720     }
2721
2722   /* Search upwards from currently selected frame looking for a match */
2723
2724   for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2725     {
2726       if (!BLOCK_SUPERBLOCK (b))
2727         surrounding_static_block = b; /* For elmin of dups */
2728       
2729       /* Also catch fields of types defined in this places which
2730          match our text string.  Only complete on types visible
2731          from current context.  */
2732       for (i = 0; i < BLOCK_NSYMS (b); i++)
2733         {
2734           sym = BLOCK_SYM (b, i);
2735           demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2736                                           DMGL_PARAMS | DMGL_ANSI);
2737           if (demangled != NULL)
2738             {
2739               free (demangled);
2740               return (SYMBOL_NAME (sym));
2741             }
2742           if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2743             {
2744               struct type *t = SYMBOL_TYPE (sym);
2745               enum type_code c = TYPE_CODE (t);
2746
2747               if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2748                 {
2749                   for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2750                     {
2751                       if (TYPE_FIELD_NAME (t, j))
2752                         {
2753                           demangled =
2754                             demangle_and_match (TYPE_FIELD_NAME (t, j),
2755                                                 lookfor,
2756                                                 DMGL_PARAMS | DMGL_ANSI);
2757                           if (demangled != NULL)
2758                             {
2759                               free (demangled);
2760                               return (TYPE_FIELD_NAME (t, j));
2761                             }
2762                         }
2763                     }
2764                 }
2765             }
2766         }
2767     }
2768
2769   /* Go through the symtabs and check the externs and statics for
2770      symbols which match.  */
2771
2772   ALL_SYMTABS (objfile, s)
2773     {
2774       QUIT;
2775       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2776       for (i = 0; i < BLOCK_NSYMS (b); i++)
2777         {
2778           sym = BLOCK_SYM (b, i);
2779           demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2780                                           DMGL_PARAMS | DMGL_ANSI);
2781           if (demangled != NULL)
2782             {
2783               free (demangled);
2784               return (SYMBOL_NAME (sym));
2785             }
2786         }
2787     }
2788
2789   ALL_SYMTABS (objfile, s)
2790     {
2791       QUIT;
2792       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2793       /* Don't do this block twice.  */
2794       if (b == surrounding_static_block) continue;
2795       for (i = 0; i < BLOCK_NSYMS (b); i++)
2796         {
2797           sym = BLOCK_SYM (b, i);
2798           demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2799                                           DMGL_PARAMS | DMGL_ANSI);
2800           if (demangled != NULL)
2801             {
2802               free (demangled);
2803               return (SYMBOL_NAME (sym));
2804             }
2805         }
2806     }
2807
2808   return (NULL);
2809 }
2810
2811 \f
2812 #if 0
2813 /* Add the type of the symbol sym to the type of the current
2814    function whose block we are in (assumed).  The type of
2815    this current function is contained in *TYPE.
2816    
2817    This basically works as follows:  When we find a function
2818    symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2819    a pointer to its type in the global in_function_type.  Every 
2820    time we come across a parameter symbol ('p' in its name), then
2821    this procedure adds the name and type of that parameter
2822    to the function type pointed to by *TYPE.  (Which should correspond
2823    to in_function_type if it was called correctly).
2824
2825    Note that since we are modifying a type, the result of 
2826    lookup_function_type() should be memcpy()ed before calling
2827    this.  When not in strict typing mode, the expression
2828    evaluator can choose to ignore this.
2829
2830    Assumption:  All of a function's parameter symbols will
2831    appear before another function symbol is found.  The parameters 
2832    appear in the same order in the argument list as they do in the
2833    symbol table. */
2834
2835 void
2836 add_param_to_type (type,sym)
2837    struct type **type;
2838    struct symbol *sym;
2839 {
2840    int num = ++(TYPE_NFIELDS(*type));
2841
2842    if(TYPE_NFIELDS(*type)-1)
2843       TYPE_FIELDS(*type) = (struct field *)
2844           (*current_objfile->xrealloc) ((char *)(TYPE_FIELDS(*type)),
2845                                         num*sizeof(struct field));
2846    else
2847       TYPE_FIELDS(*type) = (struct field *)
2848           (*current_objfile->xmalloc) (num*sizeof(struct field));
2849    
2850    TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2851    TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2852    TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2853    TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2854 }
2855 #endif 
2856 \f
2857 void
2858 _initialize_symtab ()
2859 {
2860   add_info ("variables", variables_info,
2861             "All global and static variable names, or those matching REGEXP.");
2862   add_info ("functions", functions_info,
2863             "All function names, or those matching REGEXP.");
2864
2865   /* FIXME:  This command has at least the following problems:
2866      1.  It prints builtin types (in a very strange and confusing fashion).
2867      2.  It doesn't print right, e.g. with
2868          typedef struct foo *FOO
2869          type_print prints "FOO" when we want to make it (in this situation)
2870          print "struct foo *".
2871      I also think "ptype" or "whatis" is more likely to be useful (but if
2872      there is much disagreement "info types" can be fixed).  */
2873   add_info ("types", types_info,
2874             "All type names, or those matching REGEXP.");
2875
2876 #if 0
2877   add_info ("methods", methods_info,
2878             "All method names, or those matching REGEXP::REGEXP.\n\
2879 If the class qualifier is omitted, it is assumed to be the current scope.\n\
2880 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
2881 are listed.");
2882 #endif
2883   add_info ("sources", sources_info,
2884             "Source files in the program.");
2885
2886   add_com ("rbreak", no_class, rbreak_command,
2887             "Set a breakpoint for all functions matching REGEXP.");
2888
2889   /* Initialize the one built-in type that isn't language dependent... */
2890   builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
2891                                   "<unknown type>", (struct objfile *) NULL);
2892 }