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