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