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