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