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