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