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 
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 
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 
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 /* FIXME:  What about languages without main() or specially linked
840    executables that have no main() ? */
841 
842 struct partial_symtab *
843 find_main_psymtab ()
844 {
845   register struct partial_symtab *pst;
846   register struct objfile *objfile;
847 
848   ALL_PSYMTABS (objfile, pst)
849     {
850       if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
851 	{
852 	  return (pst);
853 	}
854     }
855   return (NULL);
856 }
857 
858 /* Look for a symbol in block BLOCK.  */
859 
860 struct symbol *
861 lookup_block_symbol (block, name, namespace)
862      register const struct block *block;
863      const char *name;
864      const enum namespace namespace;
865 {
866   register int bot, top, inc;
867   register struct symbol *sym, *parameter_sym;
868 
869   top = BLOCK_NSYMS (block);
870   bot = 0;
871 
872   /* If the blocks's symbols were sorted, start with a binary search.  */
873 
874   if (BLOCK_SHOULD_SORT (block))
875     {
876       /* First, advance BOT to not far before
877 	 the first symbol whose name is NAME.  */
878 
879       while (1)
880 	{
881 	  inc = (top - bot + 1);
882 	  /* No need to keep binary searching for the last few bits worth.  */
883 	  if (inc < 4)
884 	    break;
885 	  inc = (inc >> 1) + bot;
886 	  sym = BLOCK_SYM (block, inc);
887 	  if (SYMBOL_NAME (sym)[0] < name[0])
888 	    bot = inc;
889 	  else if (SYMBOL_NAME (sym)[0] > name[0])
890 	    top = inc;
891 	  else if (strcmp (SYMBOL_NAME (sym), name) < 0)
892 	    bot = inc;
893 	  else
894 	    top = inc;
895 	}
896 
897       /* Now scan forward until we run out of symbols,
898 	 find one whose name is greater than NAME,
899 	 or find one we want.
900 	 If there is more than one symbol with the right name and namespace,
901 	 we return the first one.  dbxread.c is careful to make sure
902 	 that if one is a register then it comes first.  */
903 
904       top = BLOCK_NSYMS (block);
905       while (bot < top)
906 	{
907 	  sym = BLOCK_SYM (block, bot);
908 	  inc = SYMBOL_NAME (sym)[0] - name[0];
909 	  if (inc == 0)
910 	    inc = strcmp (SYMBOL_NAME (sym), name);
911 	  if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
912 	    return sym;
913 	  if (inc > 0)
914 	    return 0;
915 	  bot++;
916 	}
917       return 0;
918     }
919 
920   /* Here if block isn't sorted.
921      This loop is equivalent to the loop above,
922      but hacked greatly for speed.
923 
924      Note that parameter symbols do not always show up last in the
925      list; this loop makes sure to take anything else other than
926      parameter symbols first; it only uses parameter symbols as a
927      last resort.  Note that this only takes up extra computation
928      time on a match.  */
929 
930   parameter_sym = (struct symbol *) 0;
931   top = BLOCK_NSYMS (block);
932   inc = name[0];
933   while (bot < top)
934     {
935       sym = BLOCK_SYM (block, bot);
936       if (SYMBOL_NAME (sym)[0] == inc
937 	  && !strcmp (SYMBOL_NAME (sym), name)
938 	  && SYMBOL_NAMESPACE (sym) == namespace)
939 	{
940 	  if (SYMBOL_CLASS (sym) == LOC_ARG
941 	      || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
942 	      || SYMBOL_CLASS (sym) == LOC_REF_ARG
943 	      || SYMBOL_CLASS (sym) == LOC_REGPARM)
944 	    parameter_sym = sym;
945 	  else
946 	    return sym;
947 	}
948       bot++;
949     }
950   return parameter_sym;		/* Will be 0 if not found. */
951 }
952 
953 /* Return the symbol for the function which contains a specified
954    lexical block, described by a struct block BL.  */
955 
956 struct symbol *
957 block_function (bl)
958      struct block *bl;
959 {
960   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
961     bl = BLOCK_SUPERBLOCK (bl);
962 
963   return BLOCK_FUNCTION (bl);
964 }
965 
966 /* Subroutine of find_pc_line */
967 
968 struct symtab *
969 find_pc_symtab (pc)
970      register CORE_ADDR pc;
971 {
972   register struct block *b;
973   struct blockvector *bv;
974   register struct symtab *s = 0;
975   register struct partial_symtab *ps;
976   register struct objfile *objfile;
977 
978   /* Search all symtabs for one whose file contains our pc */
979 
980   ALL_SYMTABS (objfile, s)
981     {
982       bv = BLOCKVECTOR (s);
983       b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
984       if (BLOCK_START (b) <= pc
985 	  && BLOCK_END (b) > pc)
986 	goto found;
987     }
988 
989   if (!s)
990     {
991       ps = find_pc_psymtab (pc);
992       if (ps && ps->readin)
993 	{
994 	  printf_filtered ("(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc);
995 	}
996       if (ps)
997 	{
998 	  s = PSYMTAB_TO_SYMTAB (ps);
999 	}
1000     }
1001 
1002 found:
1003   return (s);
1004 }
1005 
1006 /* Find the source file and line number for a given PC value.
1007    Return a structure containing a symtab pointer, a line number,
1008    and a pc range for the entire source line.
1009    The value's .pc field is NOT the specified pc.
1010    NOTCURRENT nonzero means, if specified pc is on a line boundary,
1011    use the line that ends there.  Otherwise, in that case, the line
1012    that begins there is used.  */
1013 
1014 struct symtab_and_line
1015 find_pc_line (pc, notcurrent)
1016      CORE_ADDR pc;
1017      int notcurrent;
1018 {
1019   struct symtab *s;
1020   register struct linetable *l;
1021   register int len;
1022   register int i;
1023   register struct linetable_entry *item;
1024   struct symtab_and_line val;
1025   struct blockvector *bv;
1026 
1027   /* Info on best line seen so far, and where it starts, and its file.  */
1028 
1029   int best_line = 0;
1030   CORE_ADDR best_pc = 0;
1031   CORE_ADDR best_end = 0;
1032   struct symtab *best_symtab = 0;
1033 
1034   /* Store here the first line number
1035      of a file which contains the line at the smallest pc after PC.
1036      If we don't find a line whose range contains PC,
1037      we will use a line one less than this,
1038      with a range from the start of that file to the first line's pc.  */
1039   int alt_line = 0;
1040   CORE_ADDR alt_pc = 0;
1041   struct symtab *alt_symtab = 0;
1042 
1043   /* Info on best line seen in this file.  */
1044 
1045   int prev_line;
1046   CORE_ADDR prev_pc;
1047 
1048   /* Info on first line of this file.  */
1049 
1050   int first_line;
1051   CORE_ADDR first_pc;
1052 
1053   /* If this pc is not from the current frame,
1054      it is the address of the end of a call instruction.
1055      Quite likely that is the start of the following statement.
1056      But what we want is the statement containing the instruction.
1057      Fudge the pc to make sure we get that.  */
1058 
1059   if (notcurrent) pc -= 1;
1060 
1061   s = find_pc_symtab (pc);
1062   if (s == 0)
1063     {
1064       val.symtab = 0;
1065       val.line = 0;
1066       val.pc = pc;
1067       val.end = 0;
1068       return val;
1069     }
1070 
1071   bv = BLOCKVECTOR (s);
1072 
1073   /* Look at all the symtabs that share this blockvector.
1074      They all have the same apriori range, that we found was right;
1075      but they have different line tables.  */
1076 
1077   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1078     {
1079       /* Find the best line in this symtab.  */
1080       l = LINETABLE (s);
1081       if (!l)
1082         continue;
1083       len = l->nitems;
1084       prev_line = -1;
1085       first_line = -1;
1086       for (i = 0; i < len; i++)
1087 	{
1088 	  item = &(l->item[i]);
1089 
1090 	  if (first_line < 0)
1091 	    {
1092 	      first_line = item->line;
1093 	      first_pc = item->pc;
1094 	    }
1095 	  /* Return the last line that did not start after PC.  */
1096 	  if (pc >= item->pc)
1097 	    {
1098 	      prev_line = item->line;
1099 	      prev_pc = item->pc;
1100 	    }
1101 	  else
1102 	    break;
1103 	}
1104 
1105       /* Is this file's best line closer than the best in the other files?
1106 	 If so, record this file, and its best line, as best so far.  */
1107       if (prev_line >= 0 && prev_pc > best_pc)
1108 	{
1109 	  best_pc = prev_pc;
1110 	  best_line = prev_line;
1111 	  best_symtab = s;
1112 	  /* If another line is in the linetable, and its PC is closer
1113 	     than the best_end we currently have, take it as best_end.  */
1114 	  if (i < len && (best_end == 0 || best_end > item->pc))
1115 	    best_end = item->pc;
1116 	}
1117       /* Is this file's first line closer than the first lines of other files?
1118 	 If so, record this file, and its first line, as best alternate.  */
1119       if (first_line >= 0 && first_pc > pc
1120 	  && (alt_pc == 0 || first_pc < alt_pc))
1121 	{
1122 	  alt_pc = first_pc;
1123 	  alt_line = first_line;
1124 	  alt_symtab = s;
1125 	}
1126     }
1127   if (best_symtab == 0)
1128     {
1129       val.symtab = alt_symtab;
1130       val.line = alt_line - 1;
1131       val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1132       val.end = alt_pc;
1133     }
1134   else
1135     {
1136       val.symtab = best_symtab;
1137       val.line = best_line;
1138       val.pc = best_pc;
1139       if (best_end && (alt_pc == 0 || best_end < alt_pc))
1140 	val.end = best_end;
1141       else if (alt_pc)
1142 	val.end = alt_pc;
1143       else
1144 	val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1145     }
1146   return val;
1147 }
1148 
1149 /* Find the PC value for a given source file and line number.
1150    Returns zero for invalid line number.
1151    The source file is specified with a struct symtab.  */
1152 
1153 CORE_ADDR
1154 find_line_pc (symtab, line)
1155      struct symtab *symtab;
1156      int line;
1157 {
1158   register struct linetable *l;
1159   register int ind;
1160   int dummy;
1161 
1162   if (symtab == 0)
1163     return 0;
1164   l = LINETABLE (symtab);
1165   ind = find_line_common(l, line, &dummy);
1166   return (ind >= 0) ? l->item[ind].pc : 0;
1167 }
1168 
1169 /* Find the range of pc values in a line.
1170    Store the starting pc of the line into *STARTPTR
1171    and the ending pc (start of next line) into *ENDPTR.
1172    Returns 1 to indicate success.
1173    Returns 0 if could not find the specified line.  */
1174 
1175 int
1176 find_line_pc_range (symtab, thisline, startptr, endptr)
1177      struct symtab *symtab;
1178      int thisline;
1179      CORE_ADDR *startptr, *endptr;
1180 {
1181   register struct linetable *l;
1182   register int ind;
1183   int exact_match;		/* did we get an exact linenumber match */
1184 
1185   if (symtab == 0)
1186     return 0;
1187 
1188   l = LINETABLE (symtab);
1189   ind = find_line_common (l, thisline, &exact_match);
1190   if (ind >= 0)
1191     {
1192       *startptr = l->item[ind].pc;
1193       /* If we have not seen an entry for the specified line,
1194 	 assume that means the specified line has zero bytes.  */
1195       if (!exact_match || ind == l->nitems-1)
1196 	*endptr = *startptr;
1197       else
1198 	/* Perhaps the following entry is for the following line.
1199 	   It's worth a try.  */
1200 	if (ind+1 < l->nitems
1201 	 && l->item[ind+1].line == thisline + 1)
1202 	  *endptr = l->item[ind+1].pc;
1203 	else
1204 	  *endptr = find_line_pc (symtab, thisline+1);
1205       return 1;
1206     }
1207 
1208   return 0;
1209 }
1210 
1211 /* Given a line table and a line number, return the index into the line
1212    table for the pc of the nearest line whose number is >= the specified one.
1213    Return -1 if none is found.  The value is >= 0 if it is an index.
1214 
1215    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
1216 
1217 static int
1218 find_line_common (l, lineno, exact_match)
1219      register struct linetable *l;
1220      register int lineno;
1221      int *exact_match;
1222 {
1223   register int i;
1224   register int len;
1225 
1226   /* BEST is the smallest linenumber > LINENO so far seen,
1227      or 0 if none has been seen so far.
1228      BEST_INDEX identifies the item for it.  */
1229 
1230   int best_index = -1;
1231   int best = 0;
1232 
1233   if (lineno <= 0)
1234     return -1;
1235   if (l == 0)
1236     return -1;
1237 
1238   len = l->nitems;
1239   for (i = 0; i < len; i++)
1240     {
1241       register struct linetable_entry *item = &(l->item[i]);
1242 
1243       if (item->line == lineno)
1244 	{
1245 	  *exact_match = 1;
1246 	  return i;
1247 	}
1248 
1249       if (item->line > lineno && (best == 0 || item->line < best))
1250 	{
1251 	  best = item->line;
1252 	  best_index = i;
1253 	}
1254     }
1255 
1256   /* If we got here, we didn't get an exact match.  */
1257 
1258   *exact_match = 0;
1259   return best_index;
1260 }
1261 
1262 int
1263 find_pc_line_pc_range (pc, startptr, endptr)
1264      CORE_ADDR pc;
1265      CORE_ADDR *startptr, *endptr;
1266 {
1267   struct symtab_and_line sal;
1268   sal = find_pc_line (pc, 0);
1269   *startptr = sal.pc;
1270   *endptr = sal.end;
1271   return sal.symtab != 0;
1272 }
1273 
1274 /* If P is of the form "operator[ \t]+..." where `...' is
1275    some legitimate operator text, return a pointer to the
1276    beginning of the substring of the operator text.
1277    Otherwise, return "".  */
1278 static char *
1279 operator_chars (p, end)
1280      char *p;
1281      char **end;
1282 {
1283   *end = "";
1284   if (strncmp (p, "operator", 8))
1285     return *end;
1286   p += 8;
1287 
1288   /* Don't get faked out by `operator' being part of a longer
1289      identifier.  */
1290   if (isalpha(*p) || *p == '_' || *p == '$' || *p == '\0')
1291     return *end;
1292 
1293   /* Allow some whitespace between `operator' and the operator symbol.  */
1294   while (*p == ' ' || *p == '\t')
1295     p++;
1296 
1297   /* Recognize 'operator TYPENAME'. */
1298 
1299   if (isalpha(*p) || *p == '_' || *p == '$')
1300     {
1301       register char *q = p+1;
1302       while (isalnum(*q) || *q == '_' || *q == '$')
1303 	q++;
1304       *end = q;
1305       return p;
1306     }
1307 
1308   switch (*p)
1309     {
1310     case '!':
1311     case '=':
1312     case '*':
1313     case '/':
1314     case '%':
1315     case '^':
1316       if (p[1] == '=')
1317 	*end = p+2;
1318       else
1319 	*end = p+1;
1320       return p;
1321     case '<':
1322     case '>':
1323     case '+':
1324     case '-':
1325     case '&':
1326     case '|':
1327       if (p[1] == '=' || p[1] == p[0])
1328 	*end = p+2;
1329       else
1330 	*end = p+1;
1331       return p;
1332     case '~':
1333     case ',':
1334       *end = p+1;
1335       return p;
1336     case '(':
1337       if (p[1] != ')')
1338 	error ("`operator ()' must be specified without whitespace in `()'");
1339       *end = p+2;
1340       return p;
1341     case '?':
1342       if (p[1] != ':')
1343 	error ("`operator ?:' must be specified without whitespace in `?:'");
1344       *end = p+2;
1345       return p;
1346     case '[':
1347       if (p[1] != ']')
1348 	error ("`operator []' must be specified without whitespace in `[]'");
1349       *end = p+2;
1350       return p;
1351     default:
1352       error ("`operator %s' not supported", p);
1353       break;
1354     }
1355   *end = "";
1356   return *end;
1357 }
1358 
1359 /* Recursive helper function for decode_line_1.
1360  * Look for methods named NAME in type T.
1361  * Return number of matches.
1362  * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1363  * These allocations seem to define "big enough":
1364  * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1365  * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1366  */
1367 
1368 int
1369 find_methods (t, name, physnames, sym_arr)
1370      struct type *t;
1371      char *name;
1372      char **physnames;
1373      struct symbol **sym_arr;
1374 {
1375   int i1 = 0;
1376   int ibase;
1377   struct symbol *sym_class;
1378   char *class_name = type_name_no_tag (t);
1379   /* Ignore this class if it doesn't have a name.
1380      This prevents core dumps, but is just a workaround
1381      because we might not find the function in
1382      certain cases, such as
1383      struct D {virtual int f();}
1384      struct C : D {virtual int g();}
1385      (in this case g++ 1.35.1- does not put out a name
1386      for D as such, it defines type 19 (for example) in
1387      the same stab as C, and then does a
1388      .stabs "D:T19" and a .stabs "D:t19".
1389      Thus
1390      "break C::f" should not be looking for field f in
1391      the class named D,
1392      but just for the field f in the baseclasses of C
1393      (no matter what their names).
1394 
1395      However, I don't know how to replace the code below
1396      that depends on knowing the name of D.  */
1397   if (class_name
1398       && (sym_class = lookup_symbol (class_name,
1399 				     (struct block *)NULL,
1400 				     STRUCT_NAMESPACE,
1401 				     (int *)NULL,
1402 				     (struct symtab **)NULL)))
1403     {
1404       int method_counter;
1405       t = SYMBOL_TYPE (sym_class);
1406       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1407 	   method_counter >= 0;
1408 	   --method_counter)
1409 	{
1410 	  int field_counter;
1411 	  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1412 
1413 	  char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1414 	  if (!strcmp (name, method_name))
1415 	    /* Find all the fields with that name.  */
1416 	    for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1417 		 field_counter >= 0;
1418 		 --field_counter)
1419 	      {
1420 		char *phys_name;
1421 		if (TYPE_FN_FIELD_STUB (f, field_counter))
1422 		  check_stub_method (t, method_counter, field_counter);
1423 		phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1424 		physnames[i1] = (char*) alloca (strlen (phys_name) + 1);
1425 		strcpy (physnames[i1], phys_name);
1426 		sym_arr[i1] = lookup_symbol (phys_name,
1427 					     SYMBOL_BLOCK_VALUE (sym_class),
1428 					     VAR_NAMESPACE,
1429 					     (int *) NULL,
1430 					     (struct symtab **) NULL);
1431 		if (sym_arr[i1]) i1++;
1432 		else
1433 		  {
1434 		    fputs_filtered("(Cannot find method ", stdout);
1435 		    fputs_demangled(phys_name, stdout, DMGL_PARAMS);
1436 		    fputs_filtered(" - possibly inlined.)\n", stdout);
1437 		  }
1438 	      }
1439 	}
1440     }
1441   /* Only search baseclasses if there is no match yet,
1442    * since names in derived classes override those in baseclasses.
1443    */
1444   if (i1)
1445     return i1;
1446   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1447     i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1448 		       physnames + i1, sym_arr + i1);
1449   return i1;
1450 }
1451 
1452 /* Parse a string that specifies a line number.
1453    Pass the address of a char * variable; that variable will be
1454    advanced over the characters actually parsed.
1455 
1456    The string can be:
1457 
1458    LINENUM -- that line number in current file.  PC returned is 0.
1459    FILE:LINENUM -- that line in that file.  PC returned is 0.
1460    FUNCTION -- line number of openbrace of that function.
1461       PC returned is the start of the function.
1462    VARIABLE -- line number of definition of that variable.
1463       PC returned is 0.
1464    FILE:FUNCTION -- likewise, but prefer functions in that file.
1465    *EXPR -- line in which address EXPR appears.
1466 
1467    FUNCTION may be an undebuggable function found in minimal symbol table.
1468 
1469    If the argument FUNFIRSTLINE is nonzero, we want the first line
1470    of real code inside a function when a function is specified.
1471 
1472    DEFAULT_SYMTAB specifies the file to use if none is specified.
1473    It defaults to current_source_symtab.
1474    DEFAULT_LINE specifies the line number to use for relative
1475    line numbers (that start with signs).  Defaults to current_source_line.
1476 
1477    Note that it is possible to return zero for the symtab
1478    if no file is validly specified.  Callers must check that.
1479    Also, the line number returned may be invalid.  */
1480 
1481 struct symtabs_and_lines
1482 decode_line_1 (argptr, funfirstline, default_symtab, default_line)
1483      char **argptr;
1484      int funfirstline;
1485      struct symtab *default_symtab;
1486      int default_line;
1487 {
1488   struct symtabs_and_lines values;
1489 #ifdef HPPA_COMPILER_BUG
1490   /* FIXME: The native HP 9000/700 compiler has a bug which appears
1491      when optimizing this file with target i960-vxworks.  I haven't
1492      been able to construct a simple test case.  The problem is that
1493      in the second call to SKIP_PROLOGUE below, the compiler somehow
1494      does not realize that the statement val = find_pc_line (...) will
1495      change the values of the fields of val.  It extracts the elements
1496      into registers at the top of the block, and does not update the
1497      registers after the call to find_pc_line.  You can check this by
1498      inserting a printf at the end of find_pc_line to show what values
1499      it is returning for val.pc and val.end and another printf after
1500      the call to see what values the function actually got (remember,
1501      this is compiling with cc -O, with this patch removed).  You can
1502      also examine the assembly listing: search for the second call to
1503      skip_prologue; the LDO statement before the next call to
1504      find_pc_line loads the address of the structure which
1505      find_pc_line will return; if there is a LDW just before the LDO,
1506      which fetches an element of the structure, then the compiler
1507      still has the bug.
1508 
1509      Setting val to volatile avoids the problem.  We must undef
1510      volatile, because the HPPA native compiler does not define
1511      __STDC__, although it does understand volatile, and so volatile
1512      will have been defined away in defs.h.  */
1513 #undef volatile
1514   volatile struct symtab_and_line val;
1515 #define volatile /*nothing*/
1516 #else
1517   struct symtab_and_line val;
1518 #endif
1519   register char *p, *p1;
1520   char *q, *q1;
1521   register struct symtab *s;
1522 
1523   register struct symbol *sym;
1524   /* The symtab that SYM was found in.  */
1525   struct symtab *sym_symtab;
1526 
1527   register CORE_ADDR pc;
1528   register struct minimal_symbol *msymbol;
1529   char *copy;
1530   struct symbol *sym_class;
1531   int i1;
1532   int is_quoted;
1533   struct symbol **sym_arr;
1534   struct type *t;
1535   char **physnames;
1536   char *saved_arg = *argptr;
1537   extern char *gdb_completer_quote_characters;
1538 
1539   /* Defaults have defaults.  */
1540 
1541   if (default_symtab == 0)
1542     {
1543       default_symtab = current_source_symtab;
1544       default_line = current_source_line;
1545     }
1546 
1547   /* See if arg is *PC */
1548 
1549   if (**argptr == '*')
1550     {
1551       if (**argptr == '*')
1552 	{
1553 	  (*argptr)++;
1554 	}
1555       pc = parse_and_eval_address_1 (argptr);
1556       values.sals = (struct symtab_and_line *)
1557 	xmalloc (sizeof (struct symtab_and_line));
1558       values.nelts = 1;
1559       values.sals[0] = find_pc_line (pc, 0);
1560       values.sals[0].pc = pc;
1561       return values;
1562     }
1563 
1564   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1565 
1566   s = NULL;
1567   is_quoted = (strchr (gdb_completer_quote_characters, **argptr) != NULL);
1568 
1569   for (p = *argptr; *p; p++)
1570     {
1571       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1572 	break;
1573     }
1574   while (p[0] == ' ' || p[0] == '\t') p++;
1575 
1576   if ((p[0] == ':') && !is_quoted)
1577     {
1578 
1579       /*  C++  */
1580       if (p[1] ==':')
1581 	{
1582 	  /* Extract the class name.  */
1583 	  p1 = p;
1584 	  while (p != *argptr && p[-1] == ' ') --p;
1585 	  copy = (char *) alloca (p - *argptr + 1);
1586 	  memcpy (copy, *argptr, p - *argptr);
1587 	  copy[p - *argptr] = 0;
1588 
1589 	  /* Discard the class name from the arg.  */
1590 	  p = p1 + 2;
1591 	  while (*p == ' ' || *p == '\t') p++;
1592 	  *argptr = p;
1593 
1594 	  sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
1595 				     (struct symtab **)NULL);
1596 
1597 	  if (sym_class &&
1598 	      (   TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
1599 	       || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
1600 	    {
1601 	      /* Arg token is not digits => try it as a function name
1602 		 Find the next token (everything up to end or next whitespace). */
1603 	      p = *argptr;
1604 	      while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++;
1605 	      q = operator_chars (*argptr, &q1);
1606 
1607 	      if (q1 - q)
1608 		{
1609 		  char *opname;
1610 		  char *tmp = alloca (q1 - q + 1);
1611 		  memcpy (tmp, q, q1 - q);
1612 		  tmp[q1 - q] = '\0';
1613 		  opname = cplus_mangle_opname (tmp, DMGL_ANSI);
1614 		  if (opname == NULL)
1615 		    {
1616 		      warning ("no mangling for \"%s\"", tmp);
1617 		      cplusplus_hint (saved_arg);
1618 		      return_to_top_level ();
1619 		    }
1620 		  copy = (char*) alloca (3 + strlen(opname));
1621 		  sprintf (copy, "__%s", opname);
1622 		  p = q1;
1623 		}
1624 	      else
1625 		{
1626 		  copy = (char *) alloca (p - *argptr + 1 + (q1 - q));
1627 		  memcpy (copy, *argptr, p - *argptr);
1628 		  copy[p - *argptr] = '\0';
1629 		}
1630 
1631 	      /* no line number may be specified */
1632 	      while (*p == ' ' || *p == '\t') p++;
1633 	      *argptr = p;
1634 
1635 	      sym = 0;
1636 	      i1 = 0;		/*  counter for the symbol array */
1637 	      t = SYMBOL_TYPE (sym_class);
1638 	      sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1639 	      physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1640 
1641 	      if (destructor_name_p (copy, t))
1642 		{
1643 		  /* destructors are a special case.  */
1644 		  struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
1645 		  int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
1646 		  char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
1647 		  physnames[i1] = (char *)alloca (strlen (phys_name) + 1);
1648 		  strcpy (physnames[i1], phys_name);
1649 		  sym_arr[i1] =
1650 		    lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
1651 				   VAR_NAMESPACE, 0, (struct symtab **)NULL);
1652 		  if (sym_arr[i1]) i1++;
1653 		}
1654 	      else
1655 		i1 = find_methods (t, copy, physnames, sym_arr);
1656 	      if (i1 == 1)
1657 		{
1658 		  /* There is exactly one field with that name.  */
1659 		  sym = sym_arr[0];
1660 
1661 		  if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
1662 		    {
1663 		      /* Arg is the name of a function */
1664 		      pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1665 		      if (funfirstline)
1666 			SKIP_PROLOGUE (pc);
1667 		      values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1668 		      values.nelts = 1;
1669 		      values.sals[0] = find_pc_line (pc, 0);
1670 		      values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc;
1671 		    }
1672 		  else
1673 		    {
1674 		      values.nelts = 0;
1675 		    }
1676 		  return values;
1677 		}
1678 	      if (i1 > 0)
1679 		{
1680 		  /* There is more than one field with that name
1681 		     (overloaded).  Ask the user which one to use.  */
1682 		  return decode_line_2 (sym_arr, i1, funfirstline);
1683 		}
1684 	      else
1685 		{
1686 		  char *tmp;
1687 
1688 		  if (OPNAME_PREFIX_P (copy))
1689 		    {
1690 		      tmp = (char *)alloca (strlen (copy+3) + 9);
1691 		      strcpy (tmp, "operator ");
1692 		      strcat (tmp, copy+3);
1693 		    }
1694 		  else
1695 		    tmp = copy;
1696 		  if (tmp[0] == '~')
1697 		    warning ("the class `%s' does not have destructor defined",
1698 			     sym_class->name);
1699 		  else
1700 		    warning ("the class %s does not have any method named %s",
1701 			     sym_class->name, tmp);
1702 		  cplusplus_hint (saved_arg);
1703 		  return_to_top_level ();
1704 		}
1705 	    }
1706 	  else
1707 	    {
1708 	      /* The quotes are important if copy is empty.  */
1709 	      warning ("can't find class, struct, or union named \"%s\"",
1710 		       copy);
1711 	      cplusplus_hint (saved_arg);
1712 	      return_to_top_level ();
1713 	    }
1714 	}
1715       /*  end of C++  */
1716 
1717 
1718       /* Extract the file name.  */
1719       p1 = p;
1720       while (p != *argptr && p[-1] == ' ') --p;
1721       copy = (char *) alloca (p - *argptr + 1);
1722       memcpy (copy, *argptr, p - *argptr);
1723       copy[p - *argptr] = 0;
1724 
1725       /* Find that file's data.  */
1726       s = lookup_symtab (copy);
1727       if (s == 0)
1728 	{
1729 	  if (!have_full_symbols () && !have_partial_symbols ())
1730 	    error (no_symtab_msg);
1731 	  error ("No source file named %s.", copy);
1732 	}
1733 
1734       /* Discard the file name from the arg.  */
1735       p = p1 + 1;
1736       while (*p == ' ' || *p == '\t') p++;
1737       *argptr = p;
1738     }
1739 
1740   /* S is specified file's symtab, or 0 if no file specified.
1741      arg no longer contains the file name.  */
1742 
1743   /* Check whether arg is all digits (and sign) */
1744 
1745   p = *argptr;
1746   if (*p == '-' || *p == '+') p++;
1747   while (*p >= '0' && *p <= '9')
1748     p++;
1749 
1750   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
1751     {
1752       /* We found a token consisting of all digits -- at least one digit.  */
1753       enum sign {none, plus, minus} sign = none;
1754 
1755       /* This is where we need to make sure that we have good defaults.
1756 	 We must guarantee that this section of code is never executed
1757 	 when we are called with just a function name, since
1758 	 select_source_symtab calls us with such an argument  */
1759 
1760       if (s == 0 && default_symtab == 0)
1761 	{
1762 	  select_source_symtab (0);
1763 	  default_symtab = current_source_symtab;
1764 	  default_line = current_source_line;
1765 	}
1766 
1767       if (**argptr == '+')
1768 	sign = plus, (*argptr)++;
1769       else if (**argptr == '-')
1770 	sign = minus, (*argptr)++;
1771       val.line = atoi (*argptr);
1772       switch (sign)
1773 	{
1774 	case plus:
1775 	  if (p == *argptr)
1776 	    val.line = 5;
1777 	  if (s == 0)
1778 	    val.line = default_line + val.line;
1779 	  break;
1780 	case minus:
1781 	  if (p == *argptr)
1782 	    val.line = 15;
1783 	  if (s == 0)
1784 	    val.line = default_line - val.line;
1785 	  else
1786 	    val.line = 1;
1787 	  break;
1788 	case none:
1789 	  break;	/* No need to adjust val.line.  */
1790 	}
1791 
1792       while (*p == ' ' || *p == '\t') p++;
1793       *argptr = p;
1794       if (s == 0)
1795 	s = default_symtab;
1796       val.symtab = s;
1797       val.pc = 0;
1798       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1799       values.sals[0] = val;
1800       values.nelts = 1;
1801       return values;
1802     }
1803 
1804   /* Arg token is not digits => try it as a variable name
1805      Find the next token (everything up to end or next whitespace).  */
1806 
1807   p = skip_quoted (*argptr);
1808   copy = (char *) alloca (p - *argptr + 1);
1809   memcpy (copy, *argptr, p - *argptr);
1810   copy[p - *argptr] = '\0';
1811   if ((copy[0] == copy [p - *argptr - 1])
1812       && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
1813     {
1814       char *temp;
1815       copy [p - *argptr - 1] = '\0';
1816       copy++;
1817       if ((temp = expensive_mangler (copy)) != NULL)
1818 	{
1819 	  copy = temp;
1820 	}
1821     }
1822   while (*p == ' ' || *p == '\t') p++;
1823   *argptr = p;
1824 
1825   /* Look up that token as a variable.
1826      If file specified, use that file's per-file block to start with.  */
1827 
1828   sym = lookup_symbol (copy,
1829 		       (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
1830 			: get_selected_block ()),
1831 		       VAR_NAMESPACE, 0, &sym_symtab);
1832 
1833   if (sym != NULL)
1834     {
1835       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
1836 	{
1837 	  /* Arg is the name of a function */
1838 	  pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
1839 	  if (funfirstline)
1840 	    SKIP_PROLOGUE (pc);
1841 	  val = find_pc_line (pc, 0);
1842 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1843 	  /* Convex: no need to suppress code on first line, if any */
1844 	  val.pc = pc;
1845 #else
1846 	  /* If SKIP_PROLOGUE left us in mid-line, and the next line is still
1847 	     part of the same function:
1848 		advance to next line,
1849 	        recalculate its line number (might not be N+1).  */
1850 	  if (val.pc != pc && val.end &&
1851 	      lookup_minimal_symbol_by_pc (pc) == lookup_minimal_symbol_by_pc (val.end)) {
1852 	    pc = val.end;	/* First pc of next line */
1853 	    val = find_pc_line (pc, 0);
1854 	  }
1855 	  val.pc = pc;
1856 #endif
1857 	  values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1858 	  values.sals[0] = val;
1859 	  values.nelts = 1;
1860 
1861 	  /* I think this is always the same as the line that
1862 	     we calculate above, but the general principle is
1863 	     "trust the symbols more than stuff like
1864 	     SKIP_PROLOGUE".  */
1865 	  if (SYMBOL_LINE (sym) != 0)
1866 	    values.sals[0].line = SYMBOL_LINE (sym);
1867 
1868 	  return values;
1869 	}
1870       else if (SYMBOL_LINE (sym) != 0)
1871 	{
1872 	  /* We know its line number.  */
1873 	  values.sals = (struct symtab_and_line *)
1874 	    xmalloc (sizeof (struct symtab_and_line));
1875 	  values.nelts = 1;
1876 	  memset (&values.sals[0], 0, sizeof (values.sals[0]));
1877 	  values.sals[0].symtab = sym_symtab;
1878 	  values.sals[0].line = SYMBOL_LINE (sym);
1879 	  return values;
1880 	}
1881       else
1882 	/* This can happen if it is compiled with a compiler which doesn't
1883 	   put out line numbers for variables.  */
1884 	error ("Line number not known for symbol \"%s\"", copy);
1885     }
1886 
1887   msymbol = lookup_minimal_symbol (copy, (struct objfile *) NULL);
1888   if (msymbol != NULL)
1889     {
1890       val.symtab = 0;
1891       val.line = 0;
1892       val.pc = msymbol -> address + FUNCTION_START_OFFSET;
1893       if (funfirstline)
1894 	SKIP_PROLOGUE (val.pc);
1895       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
1896       values.sals[0] = val;
1897       values.nelts = 1;
1898       return values;
1899     }
1900 
1901   if (!have_full_symbols () &&
1902       !have_partial_symbols () && !have_minimal_symbols ())
1903     error (no_symtab_msg);
1904 
1905   error ("Function \"%s\" not defined.", copy);
1906   return values;	/* for lint */
1907 }
1908 
1909 struct symtabs_and_lines
1910 decode_line_spec (string, funfirstline)
1911      char *string;
1912      int funfirstline;
1913 {
1914   struct symtabs_and_lines sals;
1915   if (string == 0)
1916     error ("Empty line specification.");
1917   sals = decode_line_1 (&string, funfirstline,
1918 			current_source_symtab, current_source_line);
1919   if (*string)
1920     error ("Junk at end of line specification: %s", string);
1921   return sals;
1922 }
1923 
1924 /* Given a list of NELTS symbols in sym_arr (with corresponding
1925    mangled names in physnames), return a list of lines to operate on
1926    (ask user if necessary).  */
1927 static struct symtabs_and_lines
1928 decode_line_2 (sym_arr, nelts, funfirstline)
1929      struct symbol *sym_arr[];
1930      int nelts;
1931      int funfirstline;
1932 {
1933   struct symtabs_and_lines values, return_values;
1934   register CORE_ADDR pc;
1935   char *args, *arg1;
1936   int i;
1937   char *prompt;
1938   char *demangled;
1939 
1940   values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
1941   return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
1942 
1943   i = 0;
1944   printf("[0] cancel\n[1] all\n");
1945   while (i < nelts)
1946     {
1947       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
1948 	{
1949 	  /* Arg is the name of a function */
1950 	  pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i]))
1951 	       + FUNCTION_START_OFFSET;
1952 	  if (funfirstline)
1953 	    SKIP_PROLOGUE (pc);
1954 	  values.sals[i] = find_pc_line (pc, 0);
1955 	  values.sals[i].pc = (values.sals[i].end && values.sals[i].pc != pc) ?
1956 			       values.sals[i].end                      :  pc;
1957 	  demangled = cplus_demangle (SYMBOL_NAME (sym_arr[i]),
1958 				      DMGL_PARAMS | DMGL_ANSI);
1959 	  printf("[%d] %s at %s:%d\n", (i+2),
1960 		 demangled ? demangled : SYMBOL_NAME (sym_arr[i]),
1961 		 values.sals[i].symtab->filename, values.sals[i].line);
1962 	  if (demangled != NULL)
1963 	    {
1964 	      free (demangled);
1965 	    }
1966 	}
1967       else printf ("?HERE\n");
1968       i++;
1969     }
1970 
1971   if ((prompt = getenv ("PS2")) == NULL)
1972     {
1973       prompt = ">";
1974     }
1975   printf("%s ",prompt);
1976   fflush(stdout);
1977 
1978   args = command_line_input ((char *) NULL, 0);
1979 
1980   if (args == 0)
1981     error_no_arg ("one or more choice numbers");
1982 
1983   i = 0;
1984   while (*args)
1985     {
1986       int num;
1987 
1988       arg1 = args;
1989       while (*arg1 >= '0' && *arg1 <= '9') arg1++;
1990       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
1991 	error ("Arguments must be choice numbers.");
1992 
1993       num = atoi (args);
1994 
1995       if (num == 0)
1996 	error ("cancelled");
1997       else if (num == 1)
1998 	{
1999 	  memcpy (return_values.sals, values.sals,
2000 		  (nelts * sizeof(struct symtab_and_line)));
2001 	  return_values.nelts = nelts;
2002 	  return return_values;
2003 	}
2004 
2005       if (num > nelts + 2)
2006 	{
2007 	  printf ("No choice number %d.\n", num);
2008 	}
2009       else
2010 	{
2011 	  num -= 2;
2012 	  if (values.sals[num].pc)
2013 	    {
2014 	      return_values.sals[i++] = values.sals[num];
2015 	      values.sals[num].pc = 0;
2016 	    }
2017 	  else
2018 	    {
2019 	      printf ("duplicate request for %d ignored.\n", num);
2020 	    }
2021 	}
2022 
2023       args = arg1;
2024       while (*args == ' ' || *args == '\t') args++;
2025     }
2026   return_values.nelts = i;
2027   return return_values;
2028 }
2029 
2030 
2031 /* Slave routine for sources_info.  Force line breaks at ,'s.
2032    NAME is the name to print and *FIRST is nonzero if this is the first
2033    name printed.  Set *FIRST to zero.  */
2034 static void
2035 output_source_filename (name, first)
2036      char *name;
2037      int *first;
2038 {
2039   /* Table of files printed so far.  Since a single source file can
2040      result in several partial symbol tables, we need to avoid printing
2041      it more than once.  Note: if some of the psymtabs are read in and
2042      some are not, it gets printed both under "Source files for which
2043      symbols have been read" and "Source files for which symbols will
2044      be read in on demand".  I consider this a reasonable way to deal
2045      with the situation.  I'm not sure whether this can also happen for
2046      symtabs; it doesn't hurt to check.  */
2047   static char **tab = NULL;
2048   /* Allocated size of tab in elements.
2049      Start with one 256-byte block (when using GNU malloc.c).
2050      24 is the malloc overhead when range checking is in effect.  */
2051   static int tab_alloc_size = (256 - 24) / sizeof (char *);
2052   /* Current size of tab in elements.  */
2053   static int tab_cur_size;
2054 
2055   char **p;
2056 
2057   if (*first)
2058     {
2059       if (tab == NULL)
2060 	tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2061       tab_cur_size = 0;
2062     }
2063 
2064   /* Is NAME in tab?  */
2065   for (p = tab; p < tab + tab_cur_size; p++)
2066     if (strcmp (*p, name) == 0)
2067       /* Yes; don't print it again.  */
2068       return;
2069   /* No; add it to tab.  */
2070   if (tab_cur_size == tab_alloc_size)
2071     {
2072       tab_alloc_size *= 2;
2073       tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
2074     }
2075   tab[tab_cur_size++] = name;
2076 
2077   if (*first)
2078     {
2079       *first = 0;
2080     }
2081   else
2082     {
2083       printf_filtered (", ");
2084     }
2085 
2086   wrap_here ("");
2087   fputs_filtered (name, stdout);
2088 }
2089 
2090 static void
2091 sources_info (ignore, from_tty)
2092      char *ignore;
2093      int from_tty;
2094 {
2095   register struct symtab *s;
2096   register struct partial_symtab *ps;
2097   register struct objfile *objfile;
2098   int first;
2099 
2100   if (!have_full_symbols () && !have_partial_symbols ())
2101     {
2102       error (no_symtab_msg);
2103     }
2104 
2105   printf_filtered ("Source files for which symbols have been read in:\n\n");
2106 
2107   first = 1;
2108   ALL_SYMTABS (objfile, s)
2109     {
2110       output_source_filename (s -> filename, &first);
2111     }
2112   printf_filtered ("\n\n");
2113 
2114   printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2115 
2116   first = 1;
2117   ALL_PSYMTABS (objfile, ps)
2118     {
2119       if (!ps->readin)
2120 	{
2121 	  output_source_filename (ps -> filename, &first);
2122 	}
2123     }
2124   printf_filtered ("\n");
2125 }
2126 
2127 static int
2128 name_match (name)
2129      char *name;
2130 {
2131   char *demangled = cplus_demangle (name, DMGL_ANSI);
2132   if (demangled != NULL)
2133     {
2134       int cond = re_exec (demangled);
2135       free (demangled);
2136       return (cond);
2137     }
2138   return (re_exec (name));
2139 }
2140 #define NAME_MATCH(NAME) name_match(NAME)
2141 
2142 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2143    If CLASS is zero, list all symbols except functions, type names, and
2144 		     constants (enums).
2145    If CLASS is 1, list only functions.
2146    If CLASS is 2, list only type names.
2147    If CLASS is 3, list only method names.
2148 
2149    BPT is non-zero if we should set a breakpoint at the functions
2150    we find.  */
2151 
2152 static void
2153 list_symbols (regexp, class, bpt)
2154      char *regexp;
2155      int class;
2156      int bpt;
2157 {
2158   register struct symtab *s;
2159   register struct partial_symtab *ps;
2160   register struct blockvector *bv;
2161   struct blockvector *prev_bv = 0;
2162   register struct block *b;
2163   register int i, j;
2164   register struct symbol *sym;
2165   struct partial_symbol *psym;
2166   struct objfile *objfile;
2167   struct minimal_symbol *msymbol;
2168   char *val;
2169   static char *classnames[]
2170     = {"variable", "function", "type", "method"};
2171   int found_in_file = 0;
2172   int found_misc = 0;
2173   static enum minimal_symbol_type types[]
2174     = {mst_data, mst_text, mst_abs, mst_unknown};
2175   static enum minimal_symbol_type types2[]
2176     = {mst_bss,  mst_text, mst_abs, mst_unknown};
2177   enum minimal_symbol_type ourtype = types[class];
2178   enum minimal_symbol_type ourtype2 = types2[class];
2179 
2180   if (regexp)
2181     {
2182       /* Make sure spacing is right for C++ operators.
2183 	 This is just a courtesy to make the matching less sensitive
2184 	 to how many spaces the user leaves between 'operator'
2185 	 and <TYPENAME> or <OPERATOR>. */
2186       char *opend;
2187       char *opname = operator_chars (regexp, &opend);
2188       if (*opname)
2189 	{
2190           int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2191 	  if (isalpha(*opname) || *opname == '_' || *opname == '$')
2192 	    {
2193 	      /* There should 1 space between 'operator' and 'TYPENAME'. */
2194 	      if (opname[-1] != ' ' || opname[-2] == ' ')
2195 	        fix = 1;
2196 	    }
2197 	  else
2198 	    {
2199 	      /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2200 	      if (opname[-1] == ' ')
2201 	        fix = 0;
2202 	    }
2203 	  /* If wrong number of spaces, fix it. */
2204 	  if (fix >= 0)
2205 	    {
2206 	      char *tmp = (char*) alloca(opend-opname+10);
2207 	      sprintf(tmp, "operator%.*s%s", fix, " ", opname);
2208 	      regexp = tmp;
2209 	    }
2210         }
2211 
2212       if (0 != (val = re_comp (regexp)))
2213 	error ("Invalid regexp (%s): %s", val, regexp);
2214     }
2215 
2216   /* Search through the partial symtabs *first* for all symbols
2217      matching the regexp.  That way we don't have to reproduce all of
2218      the machinery below. */
2219 
2220   ALL_PSYMTABS (objfile, ps)
2221     {
2222       struct partial_symbol *bound, *gbound, *sbound;
2223       int keep_going = 1;
2224 
2225       if (ps->readin) continue;
2226 
2227       gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2228       sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2229       bound = gbound;
2230 
2231       /* Go through all of the symbols stored in a partial
2232 	 symtab in one loop. */
2233       psym = objfile->global_psymbols.list + ps->globals_offset;
2234       while (keep_going)
2235 	{
2236 	  if (psym >= bound)
2237 	    {
2238 	      if (bound == gbound && ps->n_static_syms != 0)
2239 		{
2240 		  psym = objfile->static_psymbols.list + ps->statics_offset;
2241 		  bound = sbound;
2242 		}
2243 	      else
2244 		keep_going = 0;
2245 	      continue;
2246 	    }
2247 	  else
2248 	    {
2249 	      QUIT;
2250 
2251 	      /* If it would match (logic taken from loop below)
2252 		 load the file and go on to the next one */
2253 	      if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (psym)))
2254 		  && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2255 		       && SYMBOL_CLASS (psym) != LOC_BLOCK)
2256 		      || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2257 		      || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2258 		      || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2259 		{
2260 		  PSYMTAB_TO_SYMTAB(ps);
2261 		  keep_going = 0;
2262 		}
2263 	    }
2264 	  psym++;
2265 	}
2266     }
2267 
2268   /* Here, we search through the minimal symbol tables for functions that
2269      match, and call find_pc_symtab on them to force their symbols to
2270      be read.  The symbol will then be found during the scan of symtabs
2271      below.  If find_pc_symtab fails, set found_misc so that we will
2272      rescan to print any matching symbols without debug info.  */
2273 
2274   if (class == 1)
2275     {
2276       ALL_MSYMBOLS (objfile, msymbol)
2277 	{
2278 	  if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2279 	    {
2280 	      if (regexp == 0 || NAME_MATCH (msymbol -> name))
2281 		{
2282 		  if (0 == find_pc_symtab (msymbol -> address))
2283 		    {
2284 		      found_misc = 1;
2285 		    }
2286 		}
2287 	    }
2288 	}
2289     }
2290 
2291   /* Printout here so as to get after the "Reading in symbols"
2292      messages which will be generated above.  */
2293   if (!bpt)
2294     printf_filtered (regexp
2295 	  ? "All %ss matching regular expression \"%s\":\n"
2296 	  : "All defined %ss:\n",
2297 	  classnames[class],
2298 	  regexp);
2299 
2300   ALL_SYMTABS (objfile, s)
2301     {
2302       found_in_file = 0;
2303       bv = BLOCKVECTOR (s);
2304       /* Often many files share a blockvector.
2305 	 Scan each blockvector only once so that
2306 	 we don't get every symbol many times.
2307 	 It happens that the first symtab in the list
2308 	 for any given blockvector is the main file.  */
2309       if (bv != prev_bv)
2310 	for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2311 	  {
2312 	    b = BLOCKVECTOR_BLOCK (bv, i);
2313 	    /* Skip the sort if this block is always sorted.  */
2314 	    if (!BLOCK_SHOULD_SORT (b))
2315 	      sort_block_syms (b);
2316 	    for (j = 0; j < BLOCK_NSYMS (b); j++)
2317 	      {
2318 		QUIT;
2319 		sym = BLOCK_SYM (b, j);
2320 		if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (sym)))
2321 		    && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2322 			 && SYMBOL_CLASS (sym) != LOC_BLOCK
2323 			 && SYMBOL_CLASS (sym) != LOC_CONST)
2324 			|| (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2325 			|| (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2326 			|| (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2327 		  {
2328 		    if (bpt)
2329 		      {
2330 			/* Set a breakpoint here, if it's a function */
2331 			if (class == 1)
2332 			  break_command (SYMBOL_NAME(sym), 0);
2333 		      }
2334 		    else if (!found_in_file)
2335 		      {
2336 			fputs_filtered ("\nFile ", stdout);
2337 			fputs_filtered (s->filename, stdout);
2338 			fputs_filtered (":\n", stdout);
2339 		      }
2340 		    found_in_file = 1;
2341 
2342 		    if (class != 2 && i == STATIC_BLOCK)
2343 		      printf_filtered ("static ");
2344 
2345 		    /* Typedef that is not a C++ class */
2346 		    if (class == 2
2347 			&& SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2348 		      typedef_print (SYMBOL_TYPE(sym), sym, stdout);
2349 		    /* variable, func, or typedef-that-is-c++-class */
2350 		    else if (class < 2 ||
2351 			     (class == 2 &&
2352 			      SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
2353 		      {
2354 			type_print (SYMBOL_TYPE (sym),
2355 				    (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2356 				     ? "" : SYMBOL_NAME (sym)),
2357 				    stdout, 0);
2358 
2359 			printf_filtered (";\n");
2360 		      }
2361 		    else
2362 		      {
2363 # if 0
2364 /* FIXME, why is this zapped out? */
2365 			char buf[1024];
2366 			type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2367 			type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2368 			sprintf (buf, " %s::", type_name_no_tag (t));
2369 			type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2370 # endif
2371 		      }
2372 		  }
2373 	      }
2374 	  }
2375       prev_bv = bv;
2376     }
2377 
2378   /* If there are no eyes, avoid all contact.  I mean, if there are
2379      no debug symbols, then print directly from the msymbol_vector.  */
2380 
2381   if (found_misc || class != 1)
2382     {
2383       found_in_file = 0;
2384       ALL_MSYMBOLS (objfile, msymbol)
2385 	{
2386 	  if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2387 	    {
2388 	      if (regexp == 0 || NAME_MATCH (msymbol -> name))
2389 		{
2390 		  /* Functions:  Look up by address. */
2391 		  if (class != 1 ||
2392 		      (0 == find_pc_symtab (msymbol -> address)))
2393 		    {
2394 		      /* Variables/Absolutes:  Look up by name */
2395 		      if (lookup_symbol (msymbol -> name,
2396 					 (struct block *) 0, VAR_NAMESPACE, 0,
2397 					 (struct symtab **) 0) == NULL)
2398 			{
2399 			  if (!found_in_file)
2400 			    {
2401 			      printf_filtered ("\nNon-debugging symbols:\n");
2402 			      found_in_file = 1;
2403 			    }
2404 			  printf_filtered ("	%08x  %s\n",
2405 					   msymbol -> address,
2406 					   msymbol -> name);
2407 			}
2408 		    }
2409 		}
2410 	    }
2411 	}
2412     }
2413 }
2414 
2415 static void
2416 variables_info (regexp, from_tty)
2417      char *regexp;
2418      int from_tty;
2419 {
2420   list_symbols (regexp, 0, 0);
2421 }
2422 
2423 static void
2424 functions_info (regexp, from_tty)
2425      char *regexp;
2426      int from_tty;
2427 {
2428   list_symbols (regexp, 1, 0);
2429 }
2430 
2431 static void
2432 types_info (regexp, from_tty)
2433      char *regexp;
2434      int from_tty;
2435 {
2436   list_symbols (regexp, 2, 0);
2437 }
2438 
2439 #if 0
2440 /* Tiemann says: "info methods was never implemented."  */
2441 static void
2442 methods_info (regexp)
2443      char *regexp;
2444 {
2445   list_symbols (regexp, 3, 0);
2446 }
2447 #endif /* 0 */
2448 
2449 /* Breakpoint all functions matching regular expression. */
2450 static void
2451 rbreak_command (regexp, from_tty)
2452      char *regexp;
2453      int from_tty;
2454 {
2455   list_symbols (regexp, 1, 1);
2456 }
2457 
2458 
2459 /* Return Nonzero if block a is lexically nested within block b,
2460    or if a and b have the same pc range.
2461    Return zero otherwise. */
2462 int
2463 contained_in (a, b)
2464      struct block *a, *b;
2465 {
2466   if (!a || !b)
2467     return 0;
2468   return BLOCK_START (a) >= BLOCK_START (b)
2469       && BLOCK_END (a)   <= BLOCK_END (b);
2470 }
2471 
2472 
2473 /* Helper routine for make_symbol_completion_list.  */
2474 
2475 static int return_val_size;
2476 static int return_val_index;
2477 static char **return_val;
2478 
2479 static void
2480 completion_list_add_name(name)
2481      char *name;
2482 {
2483   if (return_val_index + 3 > return_val_size)
2484     return_val = (char **) xrealloc ((char *) return_val,
2485 				     (return_val_size *= 2) * sizeof (char *));
2486 
2487   return_val[return_val_index] = name;
2488   return_val[++return_val_index] = (char *)NULL;
2489 }
2490 
2491 static void
2492 completion_list_check_symbol (symname, text, text_len)
2493      char *symname;
2494      char *text;
2495      int text_len;
2496 {
2497   register char *cp;
2498 
2499   if ((cp = cplus_demangle(symname, -1)) != NULL)
2500     {
2501       if (strncmp(cp, text, text_len) == 0 && strncmp(symname, "_vt$", 4))
2502 	completion_list_add_name(cp);
2503       else
2504 	free(cp);
2505     }
2506   else if ((strncmp (symname, text, text_len) == 0))
2507     {
2508       cp = (char *)xmalloc (1 + strlen (symname));
2509       strcpy(cp, symname);
2510       completion_list_add_name(cp);
2511     }
2512 }
2513 
2514 /* Return a NULL terminated array of all symbols (regardless of class) which
2515    begin by matching TEXT.  If the answer is no symbols, then the return value
2516    is an array which contains only a NULL pointer.
2517 
2518    Problem: All of the symbols have to be copied because readline frees them.
2519    I'm not going to worry about this; hopefully there won't be that many.  */
2520 
2521 char **
2522 make_completion_list (text, wantclass)
2523   char *text;
2524   enum address_class wantclass;
2525 {
2526   register struct symbol *sym;
2527   register struct symtab *s;
2528   register struct partial_symtab *ps;
2529   register struct minimal_symbol *msymbol;
2530   register struct objfile *objfile;
2531   register struct block *b, *surrounding_static_block = 0;
2532   register int i, j;
2533   int text_len;
2534   struct partial_symbol *psym;
2535 
2536   text_len = strlen (text);
2537   return_val_size = 100;
2538   return_val_index = 0;
2539   return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
2540   return_val[0] = NULL;
2541 
2542   /* Look through the partial symtabs for all symbols which begin
2543      by matching TEXT.  Add each one that you find to the list.  */
2544 
2545   ALL_PSYMTABS (objfile, ps)
2546     {
2547       /* If the psymtab's been read in we'll get it when we search
2548 	 through the blockvector.  */
2549       if (ps->readin) continue;
2550 
2551       for (psym = objfile->global_psymbols.list + ps->globals_offset;
2552 	   psym < (objfile->global_psymbols.list + ps->globals_offset
2553 		   + ps->n_global_syms);
2554 	   psym++)
2555 	{
2556 	  /* If interrupted, then quit. */
2557 	  QUIT;
2558 	  if (wantclass && wantclass != SYMBOL_CLASS (psym))
2559 	    continue;
2560 	  completion_list_check_symbol (SYMBOL_NAME (psym), text, text_len);
2561 	}
2562 
2563       for (psym = objfile->static_psymbols.list + ps->statics_offset;
2564 	   psym < (objfile->static_psymbols.list + ps->statics_offset
2565 		   + ps->n_static_syms);
2566 	   psym++)
2567 	{
2568 	  QUIT;
2569 	  if (wantclass && wantclass != SYMBOL_CLASS (psym))
2570 	    continue;
2571 	  completion_list_check_symbol (SYMBOL_NAME (psym), text, text_len);
2572 	}
2573     }
2574 
2575   /* At this point scan through the misc symbol vectors and add each
2576      symbol you find to the list.  Eventually we want to ignore
2577      anything that isn't a text symbol (everything else will be
2578      handled by the psymtab code above).  */
2579 
2580   ALL_MSYMBOLS (objfile, msymbol)
2581     {
2582       QUIT;
2583       completion_list_check_symbol (msymbol -> name, text, text_len);
2584     }
2585 
2586   /* Search upwards from currently selected frame (so that we can
2587      complete on local vars.  */
2588 
2589   for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
2590     {
2591       if (!BLOCK_SUPERBLOCK (b))
2592 	{
2593 	  surrounding_static_block = b; 	/* For elmin of dups */
2594 	}
2595 
2596       /* Also catch fields of types defined in this places which match our
2597 	 text string.  Only complete on types visible from current context. */
2598 
2599       for (i = 0; i < BLOCK_NSYMS (b); i++)
2600 	{
2601 	  sym = BLOCK_SYM (b, i);
2602 	  if (wantclass && wantclass != SYMBOL_CLASS (sym))
2603 	    continue;
2604 	  completion_list_check_symbol (SYMBOL_NAME (sym), text, text_len);
2605 	  if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2606 	    {
2607 	      struct type *t = SYMBOL_TYPE (sym);
2608 	      enum type_code c = TYPE_CODE (t);
2609 
2610 	      if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2611 		{
2612 		  for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2613 		    {
2614 		      if (TYPE_FIELD_NAME (t, j))
2615 			{
2616 			  completion_list_check_symbol (TYPE_FIELD_NAME (t, j),
2617 						      text, text_len);
2618 			}
2619 		    }
2620 		}
2621 	    }
2622 	}
2623     }
2624 
2625   /* Go through the symtabs and check the externs and statics for
2626      symbols which match.  */
2627 
2628   ALL_SYMTABS (objfile, s)
2629     {
2630       QUIT;
2631       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2632       for (i = 0; i < BLOCK_NSYMS (b); i++)
2633 	{
2634 	  sym = BLOCK_SYM (b, i);
2635 	  if (wantclass && wantclass != SYMBOL_CLASS (sym))
2636 	    continue;
2637 	  completion_list_check_symbol (SYMBOL_NAME (sym), text, text_len);
2638 	}
2639     }
2640 
2641   ALL_SYMTABS (objfile, s)
2642     {
2643       QUIT;
2644       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2645       /* Don't do this block twice.  */
2646       if (b == surrounding_static_block) continue;
2647       for (i = 0; i < BLOCK_NSYMS (b); i++)
2648 	{
2649 	  sym = BLOCK_SYM (b, i);
2650 	  if (wantclass && wantclass != SYMBOL_CLASS (sym))
2651 	    continue;
2652 	  completion_list_check_symbol (SYMBOL_NAME (sym), text, text_len);
2653 	}
2654     }
2655 
2656   return (return_val);
2657 }
2658 
2659 char **
2660 make_function_completion_list (text)
2661   char *text;
2662 {
2663   return (make_completion_list(text, LOC_BLOCK));
2664 }
2665 
2666 char **
2667 make_symbol_completion_list (text)
2668   char *text;
2669 {
2670   return (make_completion_list(text, 0));
2671 }
2672 
2673 /* Find a mangled symbol that corresponds to LOOKFOR using brute force.
2674    Basically we go munging through available symbols, demangling each one,
2675    looking for a match on the demangled result. */
2676 
2677 static char *
2678 expensive_mangler (lookfor)
2679      const char *lookfor;
2680 {
2681   register struct symbol *sym;
2682   register struct symtab *s;
2683   register struct partial_symtab *ps;
2684   register struct minimal_symbol *msymbol;
2685   register struct objfile *objfile;
2686   register struct block *b, *surrounding_static_block = 0;
2687   register int i, j;
2688   struct partial_symbol *psym;
2689   char *demangled;
2690 
2691   /* Look through the partial symtabs for a symbol that matches */
2692 
2693   ALL_PSYMTABS (objfile, ps)
2694     {
2695       /* If the psymtab's been read in we'll get it when we search
2696 	 through the blockvector.  */
2697       if (ps->readin) continue;
2698 
2699       for (psym = objfile->global_psymbols.list + ps->globals_offset;
2700 	   psym < (objfile->global_psymbols.list + ps->globals_offset
2701 		   + ps->n_global_syms);
2702 	   psym++)
2703 	{
2704 	  QUIT;			/* If interrupted, then quit. */
2705 	  demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2706 					  DMGL_PARAMS | DMGL_ANSI);
2707 	  if (demangled != NULL)
2708 	    {
2709 	      free (demangled);
2710 	      return (SYMBOL_NAME (psym));
2711 	    }
2712 	}
2713 
2714       for (psym = objfile->static_psymbols.list + ps->statics_offset;
2715 	   psym < (objfile->static_psymbols.list + ps->statics_offset
2716 		   + ps->n_static_syms);
2717 	   psym++)
2718 	{
2719 	  QUIT;
2720 	  demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2721 					  DMGL_PARAMS | DMGL_ANSI);
2722 	  if (demangled != NULL)
2723 	    {
2724 	      free (demangled);
2725 	      return (SYMBOL_NAME (psym));
2726 	    }
2727 	}
2728     }
2729 
2730   /* Scan through the misc symbol vectors looking for a match. */
2731 
2732   ALL_MSYMBOLS (objfile, msymbol)
2733     {
2734       QUIT;
2735       demangled = demangle_and_match (msymbol -> name, lookfor,
2736 				      DMGL_PARAMS | DMGL_ANSI);
2737       if (demangled != NULL)
2738 	{
2739 	  free (demangled);
2740 	  return (msymbol -> name);
2741 	}
2742     }
2743 
2744   /* Search upwards from currently selected frame looking for a match */
2745 
2746   for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2747     {
2748       if (!BLOCK_SUPERBLOCK (b))
2749 	surrounding_static_block = b; /* For elmin of dups */
2750 
2751       /* Also catch fields of types defined in this places which
2752 	 match our text string.  Only complete on types visible
2753 	 from current context.  */
2754       for (i = 0; i < BLOCK_NSYMS (b); i++)
2755 	{
2756 	  sym = BLOCK_SYM (b, i);
2757 	  demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2758 					  DMGL_PARAMS | DMGL_ANSI);
2759 	  if (demangled != NULL)
2760 	    {
2761 	      free (demangled);
2762 	      return (SYMBOL_NAME (sym));
2763 	    }
2764 	  if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2765 	    {
2766 	      struct type *t = SYMBOL_TYPE (sym);
2767 	      enum type_code c = TYPE_CODE (t);
2768 
2769 	      if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2770 		{
2771 		  for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2772 		    {
2773 		      if (TYPE_FIELD_NAME (t, j))
2774 			{
2775 			  demangled =
2776 			    demangle_and_match (TYPE_FIELD_NAME (t, j),
2777 						lookfor,
2778 						DMGL_PARAMS | DMGL_ANSI);
2779 			  if (demangled != NULL)
2780 			    {
2781 			      free (demangled);
2782 			      return (TYPE_FIELD_NAME (t, j));
2783 			    }
2784 			}
2785 		    }
2786 		}
2787 	    }
2788 	}
2789     }
2790 
2791   /* Go through the symtabs and check the externs and statics for
2792      symbols which match.  */
2793 
2794   ALL_SYMTABS (objfile, s)
2795     {
2796       QUIT;
2797       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2798       for (i = 0; i < BLOCK_NSYMS (b); i++)
2799 	{
2800 	  sym = BLOCK_SYM (b, i);
2801 	  demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2802 					  DMGL_PARAMS | DMGL_ANSI);
2803 	  if (demangled != NULL)
2804 	    {
2805 	      free (demangled);
2806 	      return (SYMBOL_NAME (sym));
2807 	    }
2808 	}
2809     }
2810 
2811   ALL_SYMTABS (objfile, s)
2812     {
2813       QUIT;
2814       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2815       /* Don't do this block twice.  */
2816       if (b == surrounding_static_block) continue;
2817       for (i = 0; i < BLOCK_NSYMS (b); i++)
2818 	{
2819 	  sym = BLOCK_SYM (b, i);
2820 	  demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2821 					  DMGL_PARAMS | DMGL_ANSI);
2822 	  if (demangled != NULL)
2823 	    {
2824 	      free (demangled);
2825 	      return (SYMBOL_NAME (sym));
2826 	    }
2827 	}
2828     }
2829 
2830   return (NULL);
2831 }
2832 
2833 #if 0
2834 /* Add the type of the symbol sym to the type of the current
2835    function whose block we are in (assumed).  The type of
2836    this current function is contained in *TYPE.
2837 
2838    This basically works as follows:  When we find a function
2839    symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2840    a pointer to its type in the global in_function_type.  Every
2841    time we come across a parameter symbol ('p' in its name), then
2842    this procedure adds the name and type of that parameter
2843    to the function type pointed to by *TYPE.  (Which should correspond
2844    to in_function_type if it was called correctly).
2845 
2846    Note that since we are modifying a type, the result of
2847    lookup_function_type() should be memcpy()ed before calling
2848    this.  When not in strict typing mode, the expression
2849    evaluator can choose to ignore this.
2850 
2851    Assumption:  All of a function's parameter symbols will
2852    appear before another function symbol is found.  The parameters
2853    appear in the same order in the argument list as they do in the
2854    symbol table. */
2855 
2856 void
2857 add_param_to_type (type,sym)
2858    struct type **type;
2859    struct symbol *sym;
2860 {
2861    int num = ++(TYPE_NFIELDS(*type));
2862 
2863    if(TYPE_NFIELDS(*type)-1)
2864       TYPE_FIELDS(*type) = (struct field *)
2865 	  (*current_objfile->xrealloc) ((char *)(TYPE_FIELDS(*type)),
2866 					num*sizeof(struct field));
2867    else
2868       TYPE_FIELDS(*type) = (struct field *)
2869 	  (*current_objfile->xmalloc) (num*sizeof(struct field));
2870 
2871    TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2872    TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2873    TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2874    TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2875 }
2876 #endif
2877 
2878 void
2879 _initialize_symtab ()
2880 {
2881   add_info ("variables", variables_info,
2882 	    "All global and static variable names, or those matching REGEXP.");
2883   add_info ("functions", functions_info,
2884 	    "All function names, or those matching REGEXP.");
2885 
2886   /* FIXME:  This command has at least the following problems:
2887      1.  It prints builtin types (in a very strange and confusing fashion).
2888      2.  It doesn't print right, e.g. with
2889          typedef struct foo *FOO
2890 	 type_print prints "FOO" when we want to make it (in this situation)
2891 	 print "struct foo *".
2892      I also think "ptype" or "whatis" is more likely to be useful (but if
2893      there is much disagreement "info types" can be fixed).  */
2894   add_info ("types", types_info,
2895 	    "All type names, or those matching REGEXP.");
2896 
2897 #if 0
2898   add_info ("methods", methods_info,
2899 	    "All method names, or those matching REGEXP::REGEXP.\n\
2900 If the class qualifier is omitted, it is assumed to be the current scope.\n\
2901 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
2902 are listed.");
2903 #endif
2904   add_info ("sources", sources_info,
2905 	    "Source files in the program.");
2906 
2907   add_com ("rbreak", no_class, rbreak_command,
2908 	    "Set a breakpoint for all functions matching REGEXP.");
2909 
2910   /* Initialize the one built-in type that isn't language dependent... */
2911   builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
2912 				  "<unknown type>", (struct objfile *) NULL);
2913 }
2914