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