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