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