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