1 /* Symbol table lookup for the GNU debugger, GDB.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
34 #include "call-cmds.h"
35 #include "gdb_regex.h"
36 #include "expression.h"
42 #include "filenames.h" /* for FILENAME_CMP */
46 #include "gdb_obstack.h"
49 #include <sys/types.h>
51 #include "gdb_string.h"
56 /* Prototypes for local functions */
58 static void completion_list_add_name (char *, char *, int, char *, char *);
60 static void rbreak_command (char *, int);
62 static void types_info (char *, int);
64 static void functions_info (char *, int);
66 static void variables_info (char *, int);
68 static void sources_info (char *, int);
70 static void output_source_filename (char *, int *);
72 static int find_line_common (struct linetable *, int, int *);
74 /* This one is used by linespec.c */
76 char *operator_chars (char *p, char **end);
78 static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *,
83 static struct symbol *lookup_symbol_aux (const char *name,
84 const char *mangled_name,
85 const struct block *block,
86 const namespace_enum namespace,
87 int *is_a_field_of_this,
88 struct symtab **symtab);
91 struct symbol *lookup_symbol_aux_local (const char *name,
92 const char *mangled_name,
93 const struct block *block,
94 const namespace_enum namespace,
95 struct symtab **symtab,
96 const struct block **static_block);
99 struct symbol *lookup_symbol_aux_block (const char *name,
100 const char *mangled_name,
101 const struct block *block,
102 const namespace_enum namespace,
103 struct symtab **symtab);
106 struct symbol *lookup_symbol_aux_symtabs (int block_index,
108 const char *mangled_name,
109 const namespace_enum namespace,
110 struct symtab **symtab);
113 struct symbol *lookup_symbol_aux_psymtabs (int block_index,
115 const char *mangled_name,
116 const namespace_enum namespace,
117 struct symtab **symtab);
120 struct symbol *lookup_symbol_aux_minsyms (const char *name,
121 const char *mangled_name,
122 const namespace_enum namespace,
123 int *is_a_field_of_this,
124 struct symtab **symtab);
126 static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr);
128 /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
129 /* Signals the presence of objects compiled by HP compilers */
130 int hp_som_som_object_present = 0;
132 static void fixup_section (struct general_symbol_info *, struct objfile *);
134 static int file_matches (char *, char **, int);
136 static void print_symbol_info (namespace_enum,
137 struct symtab *, struct symbol *, int, char *);
139 static void print_msymbol_info (struct minimal_symbol *);
141 static void symtab_symbol_info (char *, namespace_enum, int);
143 static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
145 void _initialize_symtab (void);
149 /* The single non-language-specific builtin type */
150 struct type *builtin_type_error;
152 /* Block in which the most recently searched-for symbol was found.
153 Might be better to make this a parameter to lookup_symbol and
156 const struct block *block_found;
158 /* Check for a symtab of a specific name; first in symtabs, then in
159 psymtabs. *If* there is no '/' in the name, a match after a '/'
160 in the symtab filename will also work. */
163 lookup_symtab (const char *name)
165 register struct symtab *s;
166 register struct partial_symtab *ps;
167 register struct objfile *objfile;
168 char *real_path = NULL;
169 char *full_path = NULL;
171 /* Here we are interested in canonicalizing an absolute path, not
172 absolutizing a relative path. */
173 if (IS_ABSOLUTE_PATH (name))
175 full_path = xfullpath (name);
176 make_cleanup (xfree, full_path);
177 real_path = gdb_realpath (name);
178 make_cleanup (xfree, real_path);
183 /* First, search for an exact match */
185 ALL_SYMTABS (objfile, s)
187 if (FILENAME_CMP (name, s->filename) == 0)
192 /* If the user gave us an absolute path, try to find the file in
193 this symtab and use its absolute path. */
195 if (full_path != NULL)
197 const char *fp = symtab_to_filename (s);
198 if (FILENAME_CMP (full_path, fp) == 0)
204 if (real_path != NULL)
206 char *rp = gdb_realpath (symtab_to_filename (s));
207 make_cleanup (xfree, rp);
208 if (FILENAME_CMP (real_path, rp) == 0)
215 /* Now, search for a matching tail (only if name doesn't have any dirs) */
217 if (lbasename (name) == name)
218 ALL_SYMTABS (objfile, s)
220 if (FILENAME_CMP (lbasename (s->filename), name) == 0)
224 /* Same search rules as above apply here, but now we look thru the
227 ps = lookup_partial_symtab (name);
232 error ("Internal: readin %s pst for `%s' found when no symtab found.",
235 s = PSYMTAB_TO_SYMTAB (ps);
240 /* At this point, we have located the psymtab for this file, but
241 the conversion to a symtab has failed. This usually happens
242 when we are looking up an include file. In this case,
243 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
244 been created. So, we need to run through the symtabs again in
245 order to find the file.
246 XXX - This is a crock, and should be fixed inside of the the
247 symbol parsing routines. */
251 /* Lookup the partial symbol table of a source file named NAME.
252 *If* there is no '/' in the name, a match after a '/'
253 in the psymtab filename will also work. */
255 struct partial_symtab *
256 lookup_partial_symtab (const char *name)
258 register struct partial_symtab *pst;
259 register struct objfile *objfile;
260 char *full_path = NULL;
261 char *real_path = NULL;
263 /* Here we are interested in canonicalizing an absolute path, not
264 absolutizing a relative path. */
265 if (IS_ABSOLUTE_PATH (name))
267 full_path = xfullpath (name);
268 make_cleanup (xfree, full_path);
269 real_path = gdb_realpath (name);
270 make_cleanup (xfree, real_path);
273 ALL_PSYMTABS (objfile, pst)
275 if (FILENAME_CMP (name, pst->filename) == 0)
280 /* If the user gave us an absolute path, try to find the file in
281 this symtab and use its absolute path. */
282 if (full_path != NULL)
284 if (pst->fullname == NULL)
285 source_full_path_of (pst->filename, &pst->fullname);
286 if (pst->fullname != NULL
287 && FILENAME_CMP (full_path, pst->fullname) == 0)
293 if (real_path != NULL)
296 if (pst->fullname == NULL)
297 source_full_path_of (pst->filename, &pst->fullname);
298 if (pst->fullname != NULL)
300 rp = gdb_realpath (pst->fullname);
301 make_cleanup (xfree, rp);
303 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
310 /* Now, search for a matching tail (only if name doesn't have any dirs) */
312 if (lbasename (name) == name)
313 ALL_PSYMTABS (objfile, pst)
315 if (FILENAME_CMP (lbasename (pst->filename), name) == 0)
322 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
323 full method name, which consist of the class name (from T), the unadorned
324 method name from METHOD_ID, and the signature for the specific overload,
325 specified by SIGNATURE_ID. Note that this function is g++ specific. */
328 gdb_mangle_name (struct type *type, int method_id, int signature_id)
330 int mangled_name_len;
332 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
333 struct fn_field *method = &f[signature_id];
334 char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
335 char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
336 char *newname = type_name_no_tag (type);
338 /* Does the form of physname indicate that it is the full mangled name
339 of a constructor (not just the args)? */
340 int is_full_physname_constructor;
343 int is_destructor = is_destructor_name (physname);
344 /* Need a new type prefix. */
345 char *const_prefix = method->is_const ? "C" : "";
346 char *volatile_prefix = method->is_volatile ? "V" : "";
348 int len = (newname == NULL ? 0 : strlen (newname));
350 /* Nothing to do if physname already contains a fully mangled v3 abi name
351 or an operator name. */
352 if ((physname[0] == '_' && physname[1] == 'Z')
353 || is_operator_name (field_name))
354 return xstrdup (physname);
356 is_full_physname_constructor = is_constructor_name (physname);
359 is_full_physname_constructor || (newname && STREQ (field_name, newname));
362 is_destructor = (strncmp (physname, "__dt", 4) == 0);
364 if (is_destructor || is_full_physname_constructor)
366 mangled_name = (char *) xmalloc (strlen (physname) + 1);
367 strcpy (mangled_name, physname);
373 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
375 else if (physname[0] == 't' || physname[0] == 'Q')
377 /* The physname for template and qualified methods already includes
379 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
385 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
387 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
388 + strlen (buf) + len + strlen (physname) + 1);
391 mangled_name = (char *) xmalloc (mangled_name_len);
393 mangled_name[0] = '\0';
395 strcpy (mangled_name, field_name);
397 strcat (mangled_name, buf);
398 /* If the class doesn't have a name, i.e. newname NULL, then we just
399 mangle it using 0 for the length of the class. Thus it gets mangled
400 as something starting with `::' rather than `classname::'. */
402 strcat (mangled_name, newname);
404 strcat (mangled_name, physname);
405 return (mangled_name);
409 /* Initialize the language dependent portion of a symbol
410 depending upon the language for the symbol. */
412 symbol_init_language_specific (struct general_symbol_info *gsymbol,
413 enum language language)
415 gsymbol->language = language;
416 if (gsymbol->language == language_cplus
417 || gsymbol->language == language_java
418 || gsymbol->language == language_objc)
420 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
424 memset (&gsymbol->language_specific, 0,
425 sizeof (gsymbol->language_specific));
429 /* Functions to initialize a symbol's mangled name. */
431 /* Create the hash table used for demangled names. Each hash entry is
432 a pair of strings; one for the mangled name and one for the demangled
433 name. The entry is hashed via just the mangled name. */
436 create_demangled_names_hash (struct objfile *objfile)
438 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
439 The hash table code will round this up to the next prime number.
440 Choosing a much larger table size wastes memory, and saves only about
441 1% in symbol reading. */
443 objfile->demangled_names_hash = htab_create_alloc_ex
444 (256, htab_hash_string, (int (*) (const void *, const void *)) streq,
445 NULL, objfile->md, xmcalloc, xmfree);
448 /* Try to determine the demangled name for a symbol, based on the
449 language of that symbol. If the language is set to language_auto,
450 it will attempt to find any demangling algorithm that works and
451 then set the language appropriately. The returned name is allocated
452 by the demangler and should be xfree'd. */
455 symbol_find_demangled_name (struct general_symbol_info *gsymbol,
458 char *demangled = NULL;
460 if (gsymbol->language == language_unknown)
461 gsymbol->language = language_auto;
462 if (gsymbol->language == language_cplus
463 || gsymbol->language == language_auto)
466 cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
467 if (demangled != NULL)
469 gsymbol->language = language_cplus;
473 if (gsymbol->language == language_java)
476 cplus_demangle (mangled,
477 DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
478 if (demangled != NULL)
480 gsymbol->language = language_java;
487 /* Set both the mangled and demangled (if any) names for GSYMBOL based on
488 NAME and LEN. The hash table corresponding to OBJFILE is used, and the
489 memory comes from that objfile's symbol_obstack. NAME is copied, so the
490 pointer can be discarded after calling this function. */
493 symbol_set_names (struct general_symbol_info *gsymbol,
494 const char *name, int len, struct objfile *objfile)
499 if (objfile->demangled_names_hash == NULL)
500 create_demangled_names_hash (objfile);
502 /* The stabs reader generally provides names that are not NULL-terminated;
503 most of the other readers don't do this, so we can just use the given
507 char *alloc_name = alloca (len + 1);
508 memcpy (alloc_name, name, len);
510 tmpname = alloc_name;
515 slot = (char **) htab_find_slot (objfile->demangled_names_hash, tmpname, INSERT);
517 /* If this name is not in the hash table, add it. */
520 char *demangled_name = symbol_find_demangled_name (gsymbol, tmpname);
521 int demangled_len = demangled_name ? strlen (demangled_name) : 0;
523 /* If there is a demangled name, place it right after the mangled name.
524 Otherwise, just place a second zero byte after the end of the mangled
526 *slot = obstack_alloc (&objfile->symbol_obstack,
527 len + demangled_len + 2);
528 memcpy (*slot, tmpname, len + 1);
531 memcpy (*slot + len + 1, demangled_name, demangled_len + 1);
532 xfree (demangled_name);
535 (*slot)[len + 1] = 0;
538 gsymbol->name = *slot;
539 if ((*slot)[len + 1])
540 gsymbol->language_specific.cplus_specific.demangled_name
543 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
546 /* Initialize the demangled name of GSYMBOL if possible. Any required space
547 to store the name is obtained from the specified obstack. The function
548 symbol_set_names, above, should be used instead where possible for more
549 efficient memory usage. */
552 symbol_init_demangled_name (struct general_symbol_info *gsymbol,
553 struct obstack *obstack)
555 char *mangled = gsymbol->name;
556 char *demangled = NULL;
558 demangled = symbol_find_demangled_name (gsymbol, mangled);
559 if (gsymbol->language == language_cplus
560 || gsymbol->language == language_java)
564 gsymbol->language_specific.cplus_specific.demangled_name
565 = obsavestring (demangled, strlen (demangled), obstack);
569 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
573 /* Unknown language; just clean up quietly. */
579 /* Return the source code name of a symbol. In languages where
580 demangling is necessary, this is the demangled name. */
583 symbol_natural_name (const struct general_symbol_info *gsymbol)
585 if ((gsymbol->language == language_cplus
586 || gsymbol->language == language_java
587 || gsymbol->language == language_objc)
588 && (gsymbol->language_specific.cplus_specific.demangled_name != NULL))
590 return gsymbol->language_specific.cplus_specific.demangled_name;
594 return gsymbol->name;
598 /* Return the demangled name for a symbol based on the language for
599 that symbol. If no demangled name exists, return NULL. */
601 symbol_demangled_name (struct general_symbol_info *gsymbol)
603 if (gsymbol->language == language_cplus
604 || gsymbol->language == language_java
605 || gsymbol->language == language_objc)
606 return gsymbol->language_specific.cplus_specific.demangled_name;
612 /* Initialize the structure fields to zero values. */
614 init_sal (struct symtab_and_line *sal)
625 /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
627 struct partial_symtab *
628 find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
630 register struct partial_symtab *pst;
631 register struct objfile *objfile;
632 struct minimal_symbol *msymbol;
634 /* If we know that this is not a text address, return failure. This is
635 necessary because we loop based on texthigh and textlow, which do
636 not include the data ranges. */
637 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
639 && (msymbol->type == mst_data
640 || msymbol->type == mst_bss
641 || msymbol->type == mst_abs
642 || msymbol->type == mst_file_data
643 || msymbol->type == mst_file_bss))
646 ALL_PSYMTABS (objfile, pst)
648 if (pc >= pst->textlow && pc < pst->texthigh)
650 struct partial_symtab *tpst;
652 /* An objfile that has its functions reordered might have
653 many partial symbol tables containing the PC, but
654 we want the partial symbol table that contains the
655 function containing the PC. */
656 if (!(objfile->flags & OBJF_REORDERED) &&
657 section == 0) /* can't validate section this way */
663 for (tpst = pst; tpst != NULL; tpst = tpst->next)
665 if (pc >= tpst->textlow && pc < tpst->texthigh)
667 struct partial_symbol *p;
669 p = find_pc_sect_psymbol (tpst, pc, section);
671 && SYMBOL_VALUE_ADDRESS (p)
672 == SYMBOL_VALUE_ADDRESS (msymbol))
682 /* Find which partial symtab contains PC. Return 0 if none.
683 Backward compatibility, no section */
685 struct partial_symtab *
686 find_pc_psymtab (CORE_ADDR pc)
688 return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
691 /* Find which partial symbol within a psymtab matches PC and SECTION.
692 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
694 struct partial_symbol *
695 find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
698 struct partial_symbol *best = NULL, *p, **pp;
702 psymtab = find_pc_sect_psymtab (pc, section);
706 /* Cope with programs that start at address 0 */
707 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
709 /* Search the global symbols as well as the static symbols, so that
710 find_pc_partial_function doesn't use a minimal symbol and thus
711 cache a bad endaddr. */
712 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
713 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
714 < psymtab->n_global_syms);
718 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
719 && SYMBOL_CLASS (p) == LOC_BLOCK
720 && pc >= SYMBOL_VALUE_ADDRESS (p)
721 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
722 || (psymtab->textlow == 0
723 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
725 if (section) /* match on a specific section */
727 fixup_psymbol_section (p, psymtab->objfile);
728 if (SYMBOL_BFD_SECTION (p) != section)
731 best_pc = SYMBOL_VALUE_ADDRESS (p);
736 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
737 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
738 < psymtab->n_static_syms);
742 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
743 && SYMBOL_CLASS (p) == LOC_BLOCK
744 && pc >= SYMBOL_VALUE_ADDRESS (p)
745 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
746 || (psymtab->textlow == 0
747 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
749 if (section) /* match on a specific section */
751 fixup_psymbol_section (p, psymtab->objfile);
752 if (SYMBOL_BFD_SECTION (p) != section)
755 best_pc = SYMBOL_VALUE_ADDRESS (p);
763 /* Find which partial symbol within a psymtab matches PC. Return 0 if none.
764 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
766 struct partial_symbol *
767 find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
769 return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
772 /* Debug symbols usually don't have section information. We need to dig that
773 out of the minimal symbols and stash that in the debug symbol. */
776 fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
778 struct minimal_symbol *msym;
779 msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
783 ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
784 ginfo->section = SYMBOL_SECTION (msym);
789 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
794 if (SYMBOL_BFD_SECTION (sym))
797 fixup_section (&sym->ginfo, objfile);
802 struct partial_symbol *
803 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
808 if (SYMBOL_BFD_SECTION (psym))
811 fixup_section (&psym->ginfo, objfile);
816 /* Find the definition for a specified symbol name NAME
817 in namespace NAMESPACE, visible from lexical block BLOCK.
818 Returns the struct symbol pointer, or zero if no symbol is found.
819 If SYMTAB is non-NULL, store the symbol table in which the
820 symbol was found there, or NULL if not found.
821 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
822 NAME is a field of the current implied argument `this'. If so set
823 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
824 BLOCK_FOUND is set to the block in which NAME is found (in the case of
825 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
827 /* This function has a bunch of loops in it and it would seem to be
828 attractive to put in some QUIT's (though I'm not really sure
829 whether it can run long enough to be really important). But there
830 are a few calls for which it would appear to be bad news to quit
831 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c. (Note
832 that there is C++ code below which can error(), but that probably
833 doesn't affect these calls since they are looking for a known
834 variable and thus can probably assume it will never hit the C++
838 lookup_symbol (const char *name, const struct block *block,
839 const namespace_enum namespace, int *is_a_field_of_this,
840 struct symtab **symtab)
842 char *demangled_name = NULL;
843 const char *modified_name = NULL;
844 const char *mangled_name = NULL;
845 int needtofreename = 0;
846 struct symbol *returnval;
848 modified_name = name;
850 /* If we are using C++ language, demangle the name before doing a lookup, so
851 we can always binary search. */
852 if (current_language->la_language == language_cplus)
854 demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
858 modified_name = demangled_name;
863 if (case_sensitivity == case_sensitive_off)
869 copy = (char *) alloca (len + 1);
870 for (i= 0; i < len; i++)
871 copy[i] = tolower (name[i]);
873 modified_name = copy;
876 returnval = lookup_symbol_aux (modified_name, mangled_name, block,
877 namespace, is_a_field_of_this, symtab);
879 xfree (demangled_name);
884 static struct symbol *
885 lookup_symbol_aux (const char *name, const char *mangled_name,
886 const struct block *block, const namespace_enum namespace,
887 int *is_a_field_of_this, struct symtab **symtab)
890 const struct block *static_block;
892 /* Search specified block and its superiors. Don't search
893 STATIC_BLOCK or GLOBAL_BLOCK. */
895 sym = lookup_symbol_aux_local (name, mangled_name, block, namespace,
896 symtab, &static_block);
901 /* NOTE: carlton/2002-11-05: At the time that this code was
902 #ifdeffed out, the value of 'block' was always NULL at this
903 point, hence the bemused comments below. */
905 /* FIXME: this code is never executed--block is always NULL at this
906 point. What is it trying to do, anyway? We already should have
907 checked the STATIC_BLOCK above (it is the superblock of top-level
908 blocks). Why is VAR_NAMESPACE special-cased? */
909 /* Don't need to mess with the psymtabs; if we have a block,
910 that file is read in. If we don't, then we deal later with
911 all the psymtab stuff that needs checking. */
912 /* Note (RT): The following never-executed code looks unnecessary to me also.
913 * If we change the code to use the original (passed-in)
914 * value of 'block', we could cause it to execute, but then what
915 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
916 * 'block' was already searched by the above code. And the STATIC_BLOCK's
917 * of *other* symtabs (those files not containing 'block' lexically)
918 * should not contain 'block' address-wise. So we wouldn't expect this
919 * code to find any 'sym''s that were not found above. I vote for
920 * deleting the following paragraph of code.
922 if (namespace == VAR_NAMESPACE && block != NULL)
925 /* Find the right symtab. */
926 ALL_SYMTABS (objfile, s)
928 bv = BLOCKVECTOR (s);
929 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
930 if (BLOCK_START (b) <= BLOCK_START (block)
931 && BLOCK_END (b) > BLOCK_START (block))
933 sym = lookup_block_symbol (b, name, mangled_name, VAR_NAMESPACE);
939 return fixup_symbol_section (sym, objfile);
946 /* C++: If requested to do so by the caller,
947 check to see if NAME is a field of `this'. */
948 if (is_a_field_of_this)
950 struct value *v = value_of_this (0);
952 *is_a_field_of_this = 0;
953 if (v && check_field (v, name))
955 *is_a_field_of_this = 1;
962 /* If there's a static block to search, search it next. */
964 /* NOTE: carlton/2002-12-05: There is a question as to whether or
965 not it would be appropriate to search the current global block
966 here as well. (That's what this code used to do before the
967 is_a_field_of_this check was moved up.) On the one hand, it's
968 redundant with the lookup_symbol_aux_symtabs search that happens
969 next. On the other hand, if decode_line_1 is passed an argument
970 like filename:var, then the user presumably wants 'var' to be
971 searched for in filename. On the third hand, there shouldn't be
972 multiple global variables all of which are named 'var', and it's
973 not like decode_line_1 has ever restricted its search to only
974 global variables in a single filename. All in all, only
975 searching the static block here seems best: it's correct and it's
978 /* NOTE: carlton/2002-12-05: There's also a possible performance
979 issue here: if you usually search for global symbols in the
980 current file, then it would be slightly better to search the
981 current global block before searching all the symtabs. But there
982 are other factors that have a much greater effect on performance
983 than that one, so I don't think we should worry about that for
986 if (static_block != NULL)
988 sym = lookup_symbol_aux_block (name, mangled_name, static_block,
994 /* Now search all global blocks. Do the symtab's first, then
995 check the psymtab's. If a psymtab indicates the existence
996 of the desired name as a global, then do psymtab-to-symtab
997 conversion on the fly and return the found symbol. */
999 sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, mangled_name,
1006 /* Check for the possibility of the symbol being a function or
1007 a mangled variable that is stored in one of the minimal symbol tables.
1008 Eventually, all global symbols might be resolved in this way. */
1010 sym = lookup_symbol_aux_minsyms (name, mangled_name,
1011 namespace, is_a_field_of_this,
1019 sym = lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, mangled_name,
1024 /* Now search all static file-level symbols. Not strictly correct,
1025 but more useful than an error. Do the symtabs first, then check
1026 the psymtabs. If a psymtab indicates the existence of the
1027 desired name as a file-level static, then do psymtab-to-symtab
1028 conversion on the fly and return the found symbol. */
1030 sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, mangled_name,
1035 sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, mangled_name,
1042 /* Check for the possibility of the symbol being a function or
1043 a global variable that is stored in one of the minimal symbol tables.
1044 The "minimal symbol table" is built from linker-supplied info.
1046 RT: I moved this check to last, after the complete search of
1047 the global (p)symtab's and static (p)symtab's. For HP-generated
1048 symbol tables, this check was causing a premature exit from
1049 lookup_symbol with NULL return, and thus messing up symbol lookups
1050 of things like "c::f". It seems to me a check of the minimal
1051 symbol table ought to be a last resort in any case. I'm vaguely
1052 worried about the comment below which talks about FORTRAN routines "foo_"
1053 though... is it saying we need to do the "minsym" check before
1054 the static check in this case?
1058 sym = lookup_symbol_aux_minsyms (name, mangled_name,
1059 namespace, is_a_field_of_this,
1072 /* Check to see if the symbol is defined in BLOCK or its superiors.
1073 Don't search STATIC_BLOCK or GLOBAL_BLOCK. If we don't find a
1074 match, store the address of STATIC_BLOCK in static_block. */
1076 static struct symbol *
1077 lookup_symbol_aux_local (const char *name, const char *mangled_name,
1078 const struct block *block,
1079 const namespace_enum namespace,
1080 struct symtab **symtab,
1081 const struct block **static_block)
1085 /* Check if either no block is specified or it's a global block. */
1087 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
1089 *static_block = NULL;
1093 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
1095 sym = lookup_symbol_aux_block (name, mangled_name, block, namespace,
1099 block = BLOCK_SUPERBLOCK (block);
1102 /* We've reached the static block. */
1104 *static_block = block;
1108 /* Look up a symbol in a block; if found, locate its symtab, fixup the
1109 symbol, and set block_found appropriately. */
1111 static struct symbol *
1112 lookup_symbol_aux_block (const char *name, const char *mangled_name,
1113 const struct block *block,
1114 const namespace_enum namespace,
1115 struct symtab **symtab)
1118 struct objfile *objfile = NULL;
1119 struct blockvector *bv;
1121 struct symtab *s = NULL;
1123 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1126 block_found = block;
1129 /* Search the list of symtabs for one which contains the
1130 address of the start of this block. */
1131 ALL_SYMTABS (objfile, s)
1133 bv = BLOCKVECTOR (s);
1134 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1135 if (BLOCK_START (b) <= BLOCK_START (block)
1136 && BLOCK_END (b) > BLOCK_START (block))
1143 return fixup_symbol_section (sym, objfile);
1149 /* Check to see if the symbol is defined in one of the symtabs.
1150 BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1151 depending on whether or not we want to search global symbols or
1154 static struct symbol *
1155 lookup_symbol_aux_symtabs (int block_index,
1156 const char *name, const char *mangled_name,
1157 const namespace_enum namespace,
1158 struct symtab **symtab)
1161 struct objfile *objfile;
1162 struct blockvector *bv;
1163 const struct block *block;
1166 ALL_SYMTABS (objfile, s)
1168 bv = BLOCKVECTOR (s);
1169 block = BLOCKVECTOR_BLOCK (bv, block_index);
1170 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1173 block_found = block;
1176 return fixup_symbol_section (sym, objfile);
1183 /* Check to see if the symbol is defined in one of the partial
1184 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or
1185 STATIC_BLOCK, depending on whether or not we want to search global
1186 symbols or static symbols. */
1188 static struct symbol *
1189 lookup_symbol_aux_psymtabs (int block_index, const char *name,
1190 const char *mangled_name,
1191 const namespace_enum namespace,
1192 struct symtab **symtab)
1195 struct objfile *objfile;
1196 struct blockvector *bv;
1197 const struct block *block;
1198 struct partial_symtab *ps;
1200 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
1202 ALL_PSYMTABS (objfile, ps)
1205 && lookup_partial_symbol (ps, name, mangled_name,
1206 psymtab_index, namespace))
1208 s = PSYMTAB_TO_SYMTAB (ps);
1209 bv = BLOCKVECTOR (s);
1210 block = BLOCKVECTOR_BLOCK (bv, block_index);
1211 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1214 /* This shouldn't be necessary, but as a last resort try
1215 looking in the statics even though the psymtab claimed
1216 the symbol was global, or vice-versa. It's possible
1217 that the psymtab gets it wrong in some cases. */
1219 /* FIXME: carlton/2002-09-30: Should we really do that?
1220 If that happens, isn't it likely to be a GDB error, in
1221 which case we should fix the GDB error rather than
1222 silently dealing with it here? So I'd vote for
1223 removing the check for the symbol in the other
1225 block = BLOCKVECTOR_BLOCK (bv,
1226 block_index == GLOBAL_BLOCK ?
1227 STATIC_BLOCK : GLOBAL_BLOCK);
1228 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1230 error ("Internal: %s symbol `%s' found in %s psymtab but not in symtab.\n%s may be an inlined function, or may be a template function\n(if a template, try specifying an instantiation: %s<type>).",
1231 block_index == GLOBAL_BLOCK ? "global" : "static",
1232 name, ps->filename, name, name);
1236 return fixup_symbol_section (sym, objfile);
1243 /* Check for the possibility of the symbol being a function or a
1244 mangled variable that is stored in one of the minimal symbol
1245 tables. Eventually, all global symbols might be resolved in this
1248 /* NOTE: carlton/2002-12-05: At one point, this function was part of
1249 lookup_symbol_aux, and what are now 'return' statements within
1250 lookup_symbol_aux_minsyms returned from lookup_symbol_aux, even if
1251 sym was NULL. As far as I can tell, this was basically accidental;
1252 it didn't happen every time that msymbol was non-NULL, but only if
1253 some additional conditions held as well, and it caused problems
1254 with HP-generated symbol tables. */
1256 static struct symbol *
1257 lookup_symbol_aux_minsyms (const char *name,
1258 const char *mangled_name,
1259 const namespace_enum namespace,
1260 int *is_a_field_of_this,
1261 struct symtab **symtab)
1264 struct blockvector *bv;
1265 const struct block *block;
1266 struct minimal_symbol *msymbol;
1269 if (namespace == VAR_NAMESPACE)
1271 msymbol = lookup_minimal_symbol (name, NULL, NULL);
1273 if (msymbol != NULL)
1275 /* OK, we found a minimal symbol in spite of not finding any
1276 symbol. There are various possible explanations for
1277 this. One possibility is the symbol exists in code not
1278 compiled -g. Another possibility is that the 'psymtab'
1279 isn't doing its job. A third possibility, related to #2,
1280 is that we were confused by name-mangling. For instance,
1281 maybe the psymtab isn't doing its job because it only
1282 know about demangled names, but we were given a mangled
1285 /* We first use the address in the msymbol to try to locate
1286 the appropriate symtab. Note that find_pc_sect_symtab()
1287 has a side-effect of doing psymtab-to-symtab expansion,
1288 for the found symtab. */
1289 s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
1290 SYMBOL_BFD_SECTION (msymbol));
1293 /* This is a function which has a symtab for its address. */
1294 bv = BLOCKVECTOR (s);
1295 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1297 /* This call used to pass `DEPRECATED_SYMBOL_NAME (msymbol)' as the
1298 `name' argument to lookup_block_symbol. But the name
1299 of a minimal symbol is always mangled, so that seems
1300 to be clearly the wrong thing to pass as the
1303 lookup_block_symbol (block, name, mangled_name, namespace);
1304 /* We kept static functions in minimal symbol table as well as
1305 in static scope. We want to find them in the symbol table. */
1308 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1309 sym = lookup_block_symbol (block, name,
1310 mangled_name, namespace);
1313 /* NOTE: carlton/2002-12-04: The following comment was
1314 taken from a time when two versions of this function
1315 were part of the body of lookup_symbol_aux: this
1316 comment was taken from the version of the function
1317 that was #ifdef HPUXHPPA, and the comment was right
1318 before the 'return NULL' part of lookup_symbol_aux.
1319 (Hence the "Fall through and return 0" comment.)
1320 Elena did some digging into the situation for
1321 Fortran, and she reports:
1323 "I asked around (thanks to Jeff Knaggs), and I think
1324 the story for Fortran goes like this:
1326 "Apparently, in older Fortrans, '_' was not part of
1327 the user namespace. g77 attached a final '_' to
1328 procedure names as the exported symbols for linkage
1329 (foo_) , but the symbols went in the debug info just
1330 like 'foo'. The rationale behind this is not
1331 completely clear, and maybe it was done to other
1332 symbols as well, not just procedures." */
1334 /* If we get here with sym == 0, the symbol was
1335 found in the minimal symbol table
1336 but not in the symtab.
1337 Fall through and return 0 to use the msymbol
1338 definition of "foo_".
1339 (Note that outer code generally follows up a call
1340 to this routine with a call to lookup_minimal_symbol(),
1341 so a 0 return means we'll just flow into that other routine).
1343 This happens for Fortran "foo_" symbols,
1344 which are "foo" in the symtab.
1346 This can also happen if "asm" is used to make a
1347 regular symbol but not a debugging symbol, e.g.
1348 asm(".globl _main");
1352 if (symtab != NULL && sym != NULL)
1354 return fixup_symbol_section (sym, s->objfile);
1356 else if (MSYMBOL_TYPE (msymbol) != mst_text
1357 && MSYMBOL_TYPE (msymbol) != mst_file_text
1358 && !STREQ (name, DEPRECATED_SYMBOL_NAME (msymbol)))
1360 /* This is a mangled variable, look it up by its
1362 return lookup_symbol_aux (DEPRECATED_SYMBOL_NAME (msymbol), mangled_name,
1363 NULL, namespace, is_a_field_of_this,
1372 /* Look, in partial_symtab PST, for symbol whose natural name is NAME.
1373 If LINKAGE_NAME is non-NULL, check in addition that the symbol's
1374 linkage name matches it. Check the global symbols if GLOBAL, the
1375 static symbols if not */
1377 static struct partial_symbol *
1378 lookup_partial_symbol (struct partial_symtab *pst, const char *name,
1379 const char *linkage_name, int global,
1380 namespace_enum namespace)
1382 struct partial_symbol *temp;
1383 struct partial_symbol **start, **psym;
1384 struct partial_symbol **top, **real_top, **bottom, **center;
1385 int length = (global ? pst->n_global_syms : pst->n_static_syms);
1386 int do_linear_search = 1;
1393 pst->objfile->global_psymbols.list + pst->globals_offset :
1394 pst->objfile->static_psymbols.list + pst->statics_offset);
1396 if (global) /* This means we can use a binary search. */
1398 do_linear_search = 0;
1400 /* Binary search. This search is guaranteed to end with center
1401 pointing at the earliest partial symbol whose name might be
1402 correct. At that point *all* partial symbols with an
1403 appropriate name will be checked against the correct
1407 top = start + length - 1;
1409 while (top > bottom)
1411 center = bottom + (top - bottom) / 2;
1412 if (!(center < top))
1413 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1414 if (!do_linear_search
1415 && (SYMBOL_LANGUAGE (*center) == language_java))
1417 do_linear_search = 1;
1419 if (strcmp_iw_ordered (SYMBOL_NATURAL_NAME (*center), name) >= 0)
1425 bottom = center + 1;
1428 if (!(top == bottom))
1429 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1431 while (top <= real_top
1432 && (linkage_name != NULL
1433 ? strcmp (SYMBOL_LINKAGE_NAME (*top), linkage_name) == 0
1434 : SYMBOL_MATCHES_NATURAL_NAME (*top,name)))
1436 if (SYMBOL_NAMESPACE (*top) == namespace)
1444 /* Can't use a binary search or else we found during the binary search that
1445 we should also do a linear search. */
1447 if (do_linear_search)
1449 for (psym = start; psym < start + length; psym++)
1451 if (namespace == SYMBOL_NAMESPACE (*psym))
1453 if (linkage_name != NULL
1454 ? strcmp (SYMBOL_LINKAGE_NAME (*psym), linkage_name) == 0
1455 : SYMBOL_MATCHES_NATURAL_NAME (*psym, name))
1466 /* Look up a type named NAME in the struct_namespace. The type returned
1467 must not be opaque -- i.e., must have at least one field defined
1469 This code was modelled on lookup_symbol -- the parts not relevant to looking
1470 up types were just left out. In particular it's assumed here that types
1471 are available in struct_namespace and only at file-static or global blocks. */
1475 lookup_transparent_type (const char *name)
1477 register struct symbol *sym;
1478 register struct symtab *s = NULL;
1479 register struct partial_symtab *ps;
1480 struct blockvector *bv;
1481 register struct objfile *objfile;
1482 register struct block *block;
1484 /* Now search all the global symbols. Do the symtab's first, then
1485 check the psymtab's. If a psymtab indicates the existence
1486 of the desired name as a global, then do psymtab-to-symtab
1487 conversion on the fly and return the found symbol. */
1489 ALL_SYMTABS (objfile, s)
1491 bv = BLOCKVECTOR (s);
1492 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1493 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1494 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1496 return SYMBOL_TYPE (sym);
1500 ALL_PSYMTABS (objfile, ps)
1502 if (!ps->readin && lookup_partial_symbol (ps, name, NULL,
1503 1, STRUCT_NAMESPACE))
1505 s = PSYMTAB_TO_SYMTAB (ps);
1506 bv = BLOCKVECTOR (s);
1507 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1508 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1511 /* This shouldn't be necessary, but as a last resort
1512 * try looking in the statics even though the psymtab
1513 * claimed the symbol was global. It's possible that
1514 * the psymtab gets it wrong in some cases.
1516 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1517 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1519 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1520 %s may be an inlined function, or may be a template function\n\
1521 (if a template, try specifying an instantiation: %s<type>).",
1522 name, ps->filename, name, name);
1524 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1525 return SYMBOL_TYPE (sym);
1529 /* Now search the static file-level symbols.
1530 Not strictly correct, but more useful than an error.
1531 Do the symtab's first, then
1532 check the psymtab's. If a psymtab indicates the existence
1533 of the desired name as a file-level static, then do psymtab-to-symtab
1534 conversion on the fly and return the found symbol.
1537 ALL_SYMTABS (objfile, s)
1539 bv = BLOCKVECTOR (s);
1540 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1541 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1542 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1544 return SYMBOL_TYPE (sym);
1548 ALL_PSYMTABS (objfile, ps)
1550 if (!ps->readin && lookup_partial_symbol (ps, name, NULL, 0, STRUCT_NAMESPACE))
1552 s = PSYMTAB_TO_SYMTAB (ps);
1553 bv = BLOCKVECTOR (s);
1554 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1555 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1558 /* This shouldn't be necessary, but as a last resort
1559 * try looking in the globals even though the psymtab
1560 * claimed the symbol was static. It's possible that
1561 * the psymtab gets it wrong in some cases.
1563 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1564 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1566 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1567 %s may be an inlined function, or may be a template function\n\
1568 (if a template, try specifying an instantiation: %s<type>).",
1569 name, ps->filename, name, name);
1571 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1572 return SYMBOL_TYPE (sym);
1575 return (struct type *) 0;
1579 /* Find the psymtab containing main(). */
1580 /* FIXME: What about languages without main() or specially linked
1581 executables that have no main() ? */
1583 struct partial_symtab *
1584 find_main_psymtab (void)
1586 register struct partial_symtab *pst;
1587 register struct objfile *objfile;
1589 ALL_PSYMTABS (objfile, pst)
1591 if (lookup_partial_symbol (pst, main_name (), NULL, 1, VAR_NAMESPACE))
1599 /* Search BLOCK for symbol NAME in NAMESPACE.
1601 Note that if NAME is the demangled form of a C++ symbol, we will fail
1602 to find a match during the binary search of the non-encoded names, but
1603 for now we don't worry about the slight inefficiency of looking for
1604 a match we'll never find, since it will go pretty quick. Once the
1605 binary search terminates, we drop through and do a straight linear
1606 search on the symbols. Each symbol which is marked as being a C++
1607 symbol (language_cplus set) has both the encoded and non-encoded names
1610 If MANGLED_NAME is non-NULL, verify that any symbol we find has this
1611 particular mangled name.
1615 lookup_block_symbol (register const struct block *block, const char *name,
1616 const char *mangled_name,
1617 const namespace_enum namespace)
1619 register int bot, top, inc;
1620 register struct symbol *sym;
1621 register struct symbol *sym_found = NULL;
1622 register int do_linear_search = 1;
1624 if (BLOCK_HASHTABLE (block))
1626 unsigned int hash_index;
1627 hash_index = msymbol_hash_iw (name);
1628 hash_index = hash_index % BLOCK_BUCKETS (block);
1629 for (sym = BLOCK_BUCKET (block, hash_index); sym; sym = sym->hash_next)
1631 if (SYMBOL_NAMESPACE (sym) == namespace
1633 ? strcmp (DEPRECATED_SYMBOL_NAME (sym), mangled_name) == 0
1634 : SYMBOL_MATCHES_NATURAL_NAME (sym, name)))
1640 /* If the blocks's symbols were sorted, start with a binary search. */
1642 if (BLOCK_SHOULD_SORT (block))
1644 /* Reset the linear search flag so if the binary search fails, we
1645 won't do the linear search once unless we find some reason to
1648 do_linear_search = 0;
1649 top = BLOCK_NSYMS (block);
1652 /* Advance BOT to not far before the first symbol whose name is NAME. */
1656 inc = (top - bot + 1);
1657 /* No need to keep binary searching for the last few bits worth. */
1662 inc = (inc >> 1) + bot;
1663 sym = BLOCK_SYM (block, inc);
1664 if (!do_linear_search && (SYMBOL_LANGUAGE (sym) == language_java))
1666 do_linear_search = 1;
1668 if (SYMBOL_NATURAL_NAME (sym)[0] < name[0])
1672 else if (SYMBOL_NATURAL_NAME (sym)[0] > name[0])
1676 else if (strcmp (SYMBOL_NATURAL_NAME (sym), name) < 0)
1686 /* Now scan forward until we run out of symbols, find one whose
1687 name is greater than NAME, or find one we want. If there is
1688 more than one symbol with the right name and namespace, we
1689 return the first one; I believe it is now impossible for us
1690 to encounter two symbols with the same name and namespace
1691 here, because blocks containing argument symbols are no
1692 longer sorted. The exception is for C++, where multiple functions
1693 (cloned constructors / destructors, in particular) can have
1694 the same demangled name. So if we have a particular
1695 mangled name to match, try to do so. */
1697 top = BLOCK_NSYMS (block);
1700 sym = BLOCK_SYM (block, bot);
1701 if (SYMBOL_NAMESPACE (sym) == namespace
1703 ? strcmp (DEPRECATED_SYMBOL_NAME (sym), mangled_name) == 0
1704 : SYMBOL_MATCHES_NATURAL_NAME (sym, name)))
1708 if (SYMBOL_PRINT_NAME (sym)[0] > name[0])
1716 /* Here if block isn't sorted, or we fail to find a match during the
1717 binary search above. If during the binary search above, we find a
1718 symbol which is a Java symbol, then we have re-enabled the linear
1719 search flag which was reset when starting the binary search.
1721 This loop is equivalent to the loop above, but hacked greatly for speed.
1723 Note that parameter symbols do not always show up last in the
1724 list; this loop makes sure to take anything else other than
1725 parameter symbols first; it only uses parameter symbols as a
1726 last resort. Note that this only takes up extra computation
1729 if (do_linear_search)
1731 top = BLOCK_NSYMS (block);
1735 sym = BLOCK_SYM (block, bot);
1736 if (SYMBOL_NAMESPACE (sym) == namespace
1738 ? strcmp (DEPRECATED_SYMBOL_NAME (sym), mangled_name) == 0
1739 : SYMBOL_MATCHES_NATURAL_NAME (sym, name)))
1741 /* If SYM has aliases, then use any alias that is active
1742 at the current PC. If no alias is active at the current
1743 PC, then use the main symbol.
1745 ?!? Is checking the current pc correct? Is this routine
1746 ever called to look up a symbol from another context?
1748 FIXME: No, it's not correct. If someone sets a
1749 conditional breakpoint at an address, then the
1750 breakpoint's `struct expression' should refer to the
1751 `struct symbol' appropriate for the breakpoint's
1752 address, which may not be the PC.
1754 Even if it were never called from another context,
1755 it's totally bizarre for lookup_symbol's behavior to
1756 depend on the value of the inferior's current PC. We
1757 should pass in the appropriate PC as well as the
1758 block. The interface to lookup_symbol should change
1759 to require the caller to provide a PC. */
1761 if (SYMBOL_ALIASES (sym))
1762 sym = find_active_alias (sym, read_pc ());
1765 if (SYMBOL_CLASS (sym) != LOC_ARG &&
1766 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1767 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1768 SYMBOL_CLASS (sym) != LOC_REGPARM &&
1769 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1770 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG &&
1771 SYMBOL_CLASS (sym) != LOC_COMPUTED_ARG)
1779 return (sym_found); /* Will be NULL if not found. */
1782 /* Given a main symbol SYM and ADDR, search through the alias
1783 list to determine if an alias is active at ADDR and return
1786 If no alias is active, then return SYM. */
1788 static struct symbol *
1789 find_active_alias (struct symbol *sym, CORE_ADDR addr)
1791 struct range_list *r;
1792 struct alias_list *aliases;
1794 /* If we have aliases, check them first. */
1795 aliases = SYMBOL_ALIASES (sym);
1799 if (!SYMBOL_RANGES (aliases->sym))
1800 return aliases->sym;
1801 for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next)
1803 if (r->start <= addr && r->end > addr)
1804 return aliases->sym;
1806 aliases = aliases->next;
1809 /* Nothing found, return the main symbol. */
1814 /* Find the symtab associated with PC and SECTION. Look through the
1815 psymtabs and read in another symtab if necessary. */
1818 find_pc_sect_symtab (CORE_ADDR pc, asection *section)
1820 register struct block *b;
1821 struct blockvector *bv;
1822 register struct symtab *s = NULL;
1823 register struct symtab *best_s = NULL;
1824 register struct partial_symtab *ps;
1825 register struct objfile *objfile;
1826 CORE_ADDR distance = 0;
1827 struct minimal_symbol *msymbol;
1829 /* If we know that this is not a text address, return failure. This is
1830 necessary because we loop based on the block's high and low code
1831 addresses, which do not include the data ranges, and because
1832 we call find_pc_sect_psymtab which has a similar restriction based
1833 on the partial_symtab's texthigh and textlow. */
1834 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1836 && (msymbol->type == mst_data
1837 || msymbol->type == mst_bss
1838 || msymbol->type == mst_abs
1839 || msymbol->type == mst_file_data
1840 || msymbol->type == mst_file_bss))
1843 /* Search all symtabs for the one whose file contains our address, and which
1844 is the smallest of all the ones containing the address. This is designed
1845 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1846 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1847 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1849 This happens for native ecoff format, where code from included files
1850 gets its own symtab. The symtab for the included file should have
1851 been read in already via the dependency mechanism.
1852 It might be swifter to create several symtabs with the same name
1853 like xcoff does (I'm not sure).
1855 It also happens for objfiles that have their functions reordered.
1856 For these, the symtab we are looking for is not necessarily read in. */
1858 ALL_SYMTABS (objfile, s)
1860 bv = BLOCKVECTOR (s);
1861 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1863 if (BLOCK_START (b) <= pc
1864 && BLOCK_END (b) > pc
1866 || BLOCK_END (b) - BLOCK_START (b) < distance))
1868 /* For an objfile that has its functions reordered,
1869 find_pc_psymtab will find the proper partial symbol table
1870 and we simply return its corresponding symtab. */
1871 /* In order to better support objfiles that contain both
1872 stabs and coff debugging info, we continue on if a psymtab
1874 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1876 ps = find_pc_sect_psymtab (pc, section);
1878 return PSYMTAB_TO_SYMTAB (ps);
1883 struct symbol *sym = NULL;
1885 ALL_BLOCK_SYMBOLS (b, i, sym)
1887 fixup_symbol_section (sym, objfile);
1888 if (section == SYMBOL_BFD_SECTION (sym))
1891 if ((i >= BLOCK_BUCKETS (b)) && (sym == NULL))
1892 continue; /* no symbol in this symtab matches section */
1894 distance = BLOCK_END (b) - BLOCK_START (b);
1903 ps = find_pc_sect_psymtab (pc, section);
1907 /* Might want to error() here (in case symtab is corrupt and
1908 will cause a core dump), but maybe we can successfully
1909 continue, so let's not. */
1911 (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1913 s = PSYMTAB_TO_SYMTAB (ps);
1918 /* Find the symtab associated with PC. Look through the psymtabs and
1919 read in another symtab if necessary. Backward compatibility, no section */
1922 find_pc_symtab (CORE_ADDR pc)
1924 return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1928 /* Find the source file and line number for a given PC value and SECTION.
1929 Return a structure containing a symtab pointer, a line number,
1930 and a pc range for the entire source line.
1931 The value's .pc field is NOT the specified pc.
1932 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1933 use the line that ends there. Otherwise, in that case, the line
1934 that begins there is used. */
1936 /* The big complication here is that a line may start in one file, and end just
1937 before the start of another file. This usually occurs when you #include
1938 code in the middle of a subroutine. To properly find the end of a line's PC
1939 range, we must search all symtabs associated with this compilation unit, and
1940 find the one whose first PC is closer than that of the next line in this
1943 /* If it's worth the effort, we could be using a binary search. */
1945 struct symtab_and_line
1946 find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent)
1949 register struct linetable *l;
1952 register struct linetable_entry *item;
1953 struct symtab_and_line val;
1954 struct blockvector *bv;
1955 struct minimal_symbol *msymbol;
1956 struct minimal_symbol *mfunsym;
1958 /* Info on best line seen so far, and where it starts, and its file. */
1960 struct linetable_entry *best = NULL;
1961 CORE_ADDR best_end = 0;
1962 struct symtab *best_symtab = 0;
1964 /* Store here the first line number
1965 of a file which contains the line at the smallest pc after PC.
1966 If we don't find a line whose range contains PC,
1967 we will use a line one less than this,
1968 with a range from the start of that file to the first line's pc. */
1969 struct linetable_entry *alt = NULL;
1970 struct symtab *alt_symtab = 0;
1972 /* Info on best line seen in this file. */
1974 struct linetable_entry *prev;
1976 /* If this pc is not from the current frame,
1977 it is the address of the end of a call instruction.
1978 Quite likely that is the start of the following statement.
1979 But what we want is the statement containing the instruction.
1980 Fudge the pc to make sure we get that. */
1982 init_sal (&val); /* initialize to zeroes */
1984 /* It's tempting to assume that, if we can't find debugging info for
1985 any function enclosing PC, that we shouldn't search for line
1986 number info, either. However, GAS can emit line number info for
1987 assembly files --- very helpful when debugging hand-written
1988 assembly code. In such a case, we'd have no debug info for the
1989 function, but we would have line info. */
1994 /* elz: added this because this function returned the wrong
1995 information if the pc belongs to a stub (import/export)
1996 to call a shlib function. This stub would be anywhere between
1997 two functions in the target, and the line info was erroneously
1998 taken to be the one of the line before the pc.
2000 /* RT: Further explanation:
2002 * We have stubs (trampolines) inserted between procedures.
2004 * Example: "shr1" exists in a shared library, and a "shr1" stub also
2005 * exists in the main image.
2007 * In the minimal symbol table, we have a bunch of symbols
2008 * sorted by start address. The stubs are marked as "trampoline",
2009 * the others appear as text. E.g.:
2011 * Minimal symbol table for main image
2012 * main: code for main (text symbol)
2013 * shr1: stub (trampoline symbol)
2014 * foo: code for foo (text symbol)
2016 * Minimal symbol table for "shr1" image:
2018 * shr1: code for shr1 (text symbol)
2021 * So the code below is trying to detect if we are in the stub
2022 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
2023 * and if found, do the symbolization from the real-code address
2024 * rather than the stub address.
2026 * Assumptions being made about the minimal symbol table:
2027 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
2028 * if we're really in the trampoline. If we're beyond it (say
2029 * we're in "foo" in the above example), it'll have a closer
2030 * symbol (the "foo" text symbol for example) and will not
2031 * return the trampoline.
2032 * 2. lookup_minimal_symbol_text() will find a real text symbol
2033 * corresponding to the trampoline, and whose address will
2034 * be different than the trampoline address. I put in a sanity
2035 * check for the address being the same, to avoid an
2036 * infinite recursion.
2038 msymbol = lookup_minimal_symbol_by_pc (pc);
2039 if (msymbol != NULL)
2040 if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
2042 mfunsym = lookup_minimal_symbol_text (DEPRECATED_SYMBOL_NAME (msymbol), NULL, NULL);
2043 if (mfunsym == NULL)
2044 /* I eliminated this warning since it is coming out
2045 * in the following situation:
2046 * gdb shmain // test program with shared libraries
2047 * (gdb) break shr1 // function in shared lib
2048 * Warning: In stub for ...
2049 * In the above situation, the shared lib is not loaded yet,
2050 * so of course we can't find the real func/line info,
2051 * but the "break" still works, and the warning is annoying.
2052 * So I commented out the warning. RT */
2053 /* warning ("In stub for %s; unable to find real function/line info", DEPRECATED_SYMBOL_NAME (msymbol)) */ ;
2055 else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
2056 /* Avoid infinite recursion */
2057 /* See above comment about why warning is commented out */
2058 /* warning ("In stub for %s; unable to find real function/line info", DEPRECATED_SYMBOL_NAME (msymbol)) */ ;
2061 return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
2065 s = find_pc_sect_symtab (pc, section);
2068 /* if no symbol information, return previous pc */
2075 bv = BLOCKVECTOR (s);
2077 /* Look at all the symtabs that share this blockvector.
2078 They all have the same apriori range, that we found was right;
2079 but they have different line tables. */
2081 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
2083 /* Find the best line in this symtab. */
2090 /* I think len can be zero if the symtab lacks line numbers
2091 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
2092 I'm not sure which, and maybe it depends on the symbol
2098 item = l->item; /* Get first line info */
2100 /* Is this file's first line closer than the first lines of other files?
2101 If so, record this file, and its first line, as best alternate. */
2102 if (item->pc > pc && (!alt || item->pc < alt->pc))
2108 for (i = 0; i < len; i++, item++)
2110 /* Leave prev pointing to the linetable entry for the last line
2111 that started at or before PC. */
2118 /* At this point, prev points at the line whose start addr is <= pc, and
2119 item points at the next line. If we ran off the end of the linetable
2120 (pc >= start of the last line), then prev == item. If pc < start of
2121 the first line, prev will not be set. */
2123 /* Is this file's best line closer than the best in the other files?
2124 If so, record this file, and its best line, as best so far. Don't
2125 save prev if it represents the end of a function (i.e. line number
2126 0) instead of a real line. */
2128 if (prev && prev->line && (!best || prev->pc > best->pc))
2133 /* Discard BEST_END if it's before the PC of the current BEST. */
2134 if (best_end <= best->pc)
2138 /* If another line (denoted by ITEM) is in the linetable and its
2139 PC is after BEST's PC, but before the current BEST_END, then
2140 use ITEM's PC as the new best_end. */
2141 if (best && i < len && item->pc > best->pc
2142 && (best_end == 0 || best_end > item->pc))
2143 best_end = item->pc;
2149 { /* If we didn't find any line # info, just
2155 val.symtab = alt_symtab;
2156 val.line = alt->line - 1;
2158 /* Don't return line 0, that means that we didn't find the line. */
2162 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2166 else if (best->line == 0)
2168 /* If our best fit is in a range of PC's for which no line
2169 number info is available (line number is zero) then we didn't
2170 find any valid line information. */
2175 val.symtab = best_symtab;
2176 val.line = best->line;
2178 if (best_end && (!alt || best_end < alt->pc))
2183 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2185 val.section = section;
2189 /* Backward compatibility (no section) */
2191 struct symtab_and_line
2192 find_pc_line (CORE_ADDR pc, int notcurrent)
2196 section = find_pc_overlay (pc);
2197 if (pc_in_unmapped_range (pc, section))
2198 pc = overlay_mapped_address (pc, section);
2199 return find_pc_sect_line (pc, section, notcurrent);
2202 /* Find line number LINE in any symtab whose name is the same as
2205 If found, return the symtab that contains the linetable in which it was
2206 found, set *INDEX to the index in the linetable of the best entry
2207 found, and set *EXACT_MATCH nonzero if the value returned is an
2210 If not found, return NULL. */
2213 find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
2217 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2221 struct linetable *best_linetable;
2222 struct symtab *best_symtab;
2224 /* First try looking it up in the given symtab. */
2225 best_linetable = LINETABLE (symtab);
2226 best_symtab = symtab;
2227 best_index = find_line_common (best_linetable, line, &exact);
2228 if (best_index < 0 || !exact)
2230 /* Didn't find an exact match. So we better keep looking for
2231 another symtab with the same name. In the case of xcoff,
2232 multiple csects for one source file (produced by IBM's FORTRAN
2233 compiler) produce multiple symtabs (this is unavoidable
2234 assuming csects can be at arbitrary places in memory and that
2235 the GLOBAL_BLOCK of a symtab has a begin and end address). */
2237 /* BEST is the smallest linenumber > LINE so far seen,
2238 or 0 if none has been seen so far.
2239 BEST_INDEX and BEST_LINETABLE identify the item for it. */
2242 struct objfile *objfile;
2245 if (best_index >= 0)
2246 best = best_linetable->item[best_index].line;
2250 ALL_SYMTABS (objfile, s)
2252 struct linetable *l;
2255 if (!STREQ (symtab->filename, s->filename))
2258 ind = find_line_common (l, line, &exact);
2268 if (best == 0 || l->item[ind].line < best)
2270 best = l->item[ind].line;
2283 *index = best_index;
2285 *exact_match = exact;
2290 /* Set the PC value for a given source file and line number and return true.
2291 Returns zero for invalid line number (and sets the PC to 0).
2292 The source file is specified with a struct symtab. */
2295 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
2297 struct linetable *l;
2304 symtab = find_line_symtab (symtab, line, &ind, NULL);
2307 l = LINETABLE (symtab);
2308 *pc = l->item[ind].pc;
2315 /* Find the range of pc values in a line.
2316 Store the starting pc of the line into *STARTPTR
2317 and the ending pc (start of next line) into *ENDPTR.
2318 Returns 1 to indicate success.
2319 Returns 0 if could not find the specified line. */
2322 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
2325 CORE_ADDR startaddr;
2326 struct symtab_and_line found_sal;
2329 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
2332 /* This whole function is based on address. For example, if line 10 has
2333 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2334 "info line *0x123" should say the line goes from 0x100 to 0x200
2335 and "info line *0x355" should say the line goes from 0x300 to 0x400.
2336 This also insures that we never give a range like "starts at 0x134
2337 and ends at 0x12c". */
2339 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
2340 if (found_sal.line != sal.line)
2342 /* The specified line (sal) has zero bytes. */
2343 *startptr = found_sal.pc;
2344 *endptr = found_sal.pc;
2348 *startptr = found_sal.pc;
2349 *endptr = found_sal.end;
2354 /* Given a line table and a line number, return the index into the line
2355 table for the pc of the nearest line whose number is >= the specified one.
2356 Return -1 if none is found. The value is >= 0 if it is an index.
2358 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
2361 find_line_common (register struct linetable *l, register int lineno,
2367 /* BEST is the smallest linenumber > LINENO so far seen,
2368 or 0 if none has been seen so far.
2369 BEST_INDEX identifies the item for it. */
2371 int best_index = -1;
2380 for (i = 0; i < len; i++)
2382 register struct linetable_entry *item = &(l->item[i]);
2384 if (item->line == lineno)
2386 /* Return the first (lowest address) entry which matches. */
2391 if (item->line > lineno && (best == 0 || item->line < best))
2398 /* If we got here, we didn't get an exact match. */
2405 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2407 struct symtab_and_line sal;
2408 sal = find_pc_line (pc, 0);
2411 return sal.symtab != 0;
2414 /* Given a function symbol SYM, find the symtab and line for the start
2416 If the argument FUNFIRSTLINE is nonzero, we want the first line
2417 of real code inside the function. */
2419 struct symtab_and_line
2420 find_function_start_sal (struct symbol *sym, int funfirstline)
2423 struct symtab_and_line sal;
2425 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2426 fixup_symbol_section (sym, NULL);
2428 { /* skip "first line" of function (which is actually its prologue) */
2429 asection *section = SYMBOL_BFD_SECTION (sym);
2430 /* If function is in an unmapped overlay, use its unmapped LMA
2431 address, so that SKIP_PROLOGUE has something unique to work on */
2432 if (section_is_overlay (section) &&
2433 !section_is_mapped (section))
2434 pc = overlay_unmapped_address (pc, section);
2436 pc += FUNCTION_START_OFFSET;
2437 pc = SKIP_PROLOGUE (pc);
2439 /* For overlays, map pc back into its mapped VMA range */
2440 pc = overlay_mapped_address (pc, section);
2442 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2444 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2445 /* Convex: no need to suppress code on first line, if any */
2448 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2449 line is still part of the same function. */
2451 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2452 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2454 /* First pc of next line */
2456 /* Recalculate the line number (might not be N+1). */
2457 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2465 /* If P is of the form "operator[ \t]+..." where `...' is
2466 some legitimate operator text, return a pointer to the
2467 beginning of the substring of the operator text.
2468 Otherwise, return "". */
2470 operator_chars (char *p, char **end)
2473 if (strncmp (p, "operator", 8))
2477 /* Don't get faked out by `operator' being part of a longer
2479 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2482 /* Allow some whitespace between `operator' and the operator symbol. */
2483 while (*p == ' ' || *p == '\t')
2486 /* Recognize 'operator TYPENAME'. */
2488 if (isalpha (*p) || *p == '_' || *p == '$')
2490 register char *q = p + 1;
2491 while (isalnum (*q) || *q == '_' || *q == '$')
2500 case '\\': /* regexp quoting */
2503 if (p[2] == '=') /* 'operator\*=' */
2505 else /* 'operator\*' */
2509 else if (p[1] == '[')
2512 error ("mismatched quoting on brackets, try 'operator\\[\\]'");
2513 else if (p[2] == '\\' && p[3] == ']')
2515 *end = p + 4; /* 'operator\[\]' */
2519 error ("nothing is allowed between '[' and ']'");
2523 /* Gratuitous qoute: skip it and move on. */
2545 if (p[0] == '-' && p[1] == '>')
2547 /* Struct pointer member operator 'operator->'. */
2550 *end = p + 3; /* 'operator->*' */
2553 else if (p[2] == '\\')
2555 *end = p + 4; /* Hopefully 'operator->\*' */
2560 *end = p + 2; /* 'operator->' */
2564 if (p[1] == '=' || p[1] == p[0])
2575 error ("`operator ()' must be specified without whitespace in `()'");
2580 error ("`operator ?:' must be specified without whitespace in `?:'");
2585 error ("`operator []' must be specified without whitespace in `[]'");
2589 error ("`operator %s' not supported", p);
2598 /* If FILE is not already in the table of files, return zero;
2599 otherwise return non-zero. Optionally add FILE to the table if ADD
2600 is non-zero. If *FIRST is non-zero, forget the old table
2603 filename_seen (const char *file, int add, int *first)
2605 /* Table of files seen so far. */
2606 static const char **tab = NULL;
2607 /* Allocated size of tab in elements.
2608 Start with one 256-byte block (when using GNU malloc.c).
2609 24 is the malloc overhead when range checking is in effect. */
2610 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2611 /* Current size of tab in elements. */
2612 static int tab_cur_size;
2618 tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab));
2622 /* Is FILE in tab? */
2623 for (p = tab; p < tab + tab_cur_size; p++)
2624 if (strcmp (*p, file) == 0)
2627 /* No; maybe add it to tab. */
2630 if (tab_cur_size == tab_alloc_size)
2632 tab_alloc_size *= 2;
2633 tab = (const char **) xrealloc ((char *) tab,
2634 tab_alloc_size * sizeof (*tab));
2636 tab[tab_cur_size++] = file;
2642 /* Slave routine for sources_info. Force line breaks at ,'s.
2643 NAME is the name to print and *FIRST is nonzero if this is the first
2644 name printed. Set *FIRST to zero. */
2646 output_source_filename (char *name, int *first)
2648 /* Since a single source file can result in several partial symbol
2649 tables, we need to avoid printing it more than once. Note: if
2650 some of the psymtabs are read in and some are not, it gets
2651 printed both under "Source files for which symbols have been
2652 read" and "Source files for which symbols will be read in on
2653 demand". I consider this a reasonable way to deal with the
2654 situation. I'm not sure whether this can also happen for
2655 symtabs; it doesn't hurt to check. */
2657 /* Was NAME already seen? */
2658 if (filename_seen (name, 1, first))
2660 /* Yes; don't print it again. */
2663 /* No; print it and reset *FIRST. */
2670 printf_filtered (", ");
2674 fputs_filtered (name, gdb_stdout);
2678 sources_info (char *ignore, int from_tty)
2680 register struct symtab *s;
2681 register struct partial_symtab *ps;
2682 register struct objfile *objfile;
2685 if (!have_full_symbols () && !have_partial_symbols ())
2687 error ("No symbol table is loaded. Use the \"file\" command.");
2690 printf_filtered ("Source files for which symbols have been read in:\n\n");
2693 ALL_SYMTABS (objfile, s)
2695 output_source_filename (s->filename, &first);
2697 printf_filtered ("\n\n");
2699 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2702 ALL_PSYMTABS (objfile, ps)
2706 output_source_filename (ps->filename, &first);
2709 printf_filtered ("\n");
2713 file_matches (char *file, char *files[], int nfiles)
2717 if (file != NULL && nfiles != 0)
2719 for (i = 0; i < nfiles; i++)
2721 if (strcmp (files[i], lbasename (file)) == 0)
2725 else if (nfiles == 0)
2730 /* Free any memory associated with a search. */
2732 free_search_symbols (struct symbol_search *symbols)
2734 struct symbol_search *p;
2735 struct symbol_search *next;
2737 for (p = symbols; p != NULL; p = next)
2745 do_free_search_symbols_cleanup (void *symbols)
2747 free_search_symbols (symbols);
2751 make_cleanup_free_search_symbols (struct symbol_search *symbols)
2753 return make_cleanup (do_free_search_symbols_cleanup, symbols);
2756 /* Helper function for sort_search_symbols and qsort. Can only
2757 sort symbols, not minimal symbols. */
2759 compare_search_syms (const void *sa, const void *sb)
2761 struct symbol_search **sym_a = (struct symbol_search **) sa;
2762 struct symbol_search **sym_b = (struct symbol_search **) sb;
2764 return strcmp (SYMBOL_PRINT_NAME ((*sym_a)->symbol),
2765 SYMBOL_PRINT_NAME ((*sym_b)->symbol));
2768 /* Sort the ``nfound'' symbols in the list after prevtail. Leave
2769 prevtail where it is, but update its next pointer to point to
2770 the first of the sorted symbols. */
2771 static struct symbol_search *
2772 sort_search_symbols (struct symbol_search *prevtail, int nfound)
2774 struct symbol_search **symbols, *symp, *old_next;
2777 symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
2779 symp = prevtail->next;
2780 for (i = 0; i < nfound; i++)
2785 /* Generally NULL. */
2788 qsort (symbols, nfound, sizeof (struct symbol_search *),
2789 compare_search_syms);
2792 for (i = 0; i < nfound; i++)
2794 symp->next = symbols[i];
2797 symp->next = old_next;
2803 /* Search the symbol table for matches to the regular expression REGEXP,
2804 returning the results in *MATCHES.
2806 Only symbols of KIND are searched:
2807 FUNCTIONS_NAMESPACE - search all functions
2808 TYPES_NAMESPACE - search all type names
2809 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
2810 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
2811 and constants (enums)
2813 free_search_symbols should be called when *MATCHES is no longer needed.
2815 The results are sorted locally; each symtab's global and static blocks are
2816 separately alphabetized.
2819 search_symbols (char *regexp, namespace_enum kind, int nfiles, char *files[],
2820 struct symbol_search **matches)
2822 register struct symtab *s;
2823 register struct partial_symtab *ps;
2824 register struct blockvector *bv;
2825 struct blockvector *prev_bv = 0;
2826 register struct block *b;
2829 register struct symbol *sym;
2830 struct partial_symbol **psym;
2831 struct objfile *objfile;
2832 struct minimal_symbol *msymbol;
2835 static enum minimal_symbol_type types[]
2837 {mst_data, mst_text, mst_abs, mst_unknown};
2838 static enum minimal_symbol_type types2[]
2840 {mst_bss, mst_file_text, mst_abs, mst_unknown};
2841 static enum minimal_symbol_type types3[]
2843 {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
2844 static enum minimal_symbol_type types4[]
2846 {mst_file_bss, mst_text, mst_abs, mst_unknown};
2847 enum minimal_symbol_type ourtype;
2848 enum minimal_symbol_type ourtype2;
2849 enum minimal_symbol_type ourtype3;
2850 enum minimal_symbol_type ourtype4;
2851 struct symbol_search *sr;
2852 struct symbol_search *psr;
2853 struct symbol_search *tail;
2854 struct cleanup *old_chain = NULL;
2856 if (kind < VARIABLES_NAMESPACE)
2857 error ("must search on specific namespace");
2859 ourtype = types[(int) (kind - VARIABLES_NAMESPACE)];
2860 ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)];
2861 ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)];
2862 ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)];
2864 sr = *matches = NULL;
2869 /* Make sure spacing is right for C++ operators.
2870 This is just a courtesy to make the matching less sensitive
2871 to how many spaces the user leaves between 'operator'
2872 and <TYPENAME> or <OPERATOR>. */
2874 char *opname = operator_chars (regexp, &opend);
2877 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2878 if (isalpha (*opname) || *opname == '_' || *opname == '$')
2880 /* There should 1 space between 'operator' and 'TYPENAME'. */
2881 if (opname[-1] != ' ' || opname[-2] == ' ')
2886 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2887 if (opname[-1] == ' ')
2890 /* If wrong number of spaces, fix it. */
2893 char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
2894 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
2899 if (0 != (val = re_comp (regexp)))
2900 error ("Invalid regexp (%s): %s", val, regexp);
2903 /* Search through the partial symtabs *first* for all symbols
2904 matching the regexp. That way we don't have to reproduce all of
2905 the machinery below. */
2907 ALL_PSYMTABS (objfile, ps)
2909 struct partial_symbol **bound, **gbound, **sbound;
2915 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2916 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2919 /* Go through all of the symbols stored in a partial
2920 symtab in one loop. */
2921 psym = objfile->global_psymbols.list + ps->globals_offset;
2926 if (bound == gbound && ps->n_static_syms != 0)
2928 psym = objfile->static_psymbols.list + ps->statics_offset;
2939 /* If it would match (logic taken from loop below)
2940 load the file and go on to the next one */
2941 if (file_matches (ps->filename, files, nfiles)
2943 || re_exec (SYMBOL_NATURAL_NAME (*psym)) != 0)
2944 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
2945 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2946 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2947 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2948 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
2950 PSYMTAB_TO_SYMTAB (ps);
2958 /* Here, we search through the minimal symbol tables for functions
2959 and variables that match, and force their symbols to be read.
2960 This is in particular necessary for demangled variable names,
2961 which are no longer put into the partial symbol tables.
2962 The symbol will then be found during the scan of symtabs below.
2964 For functions, find_pc_symtab should succeed if we have debug info
2965 for the function, for variables we have to call lookup_symbol
2966 to determine if the variable has debug info.
2967 If the lookup fails, set found_misc so that we will rescan to print
2968 any matching symbols without debug info.
2971 if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE))
2973 ALL_MSYMBOLS (objfile, msymbol)
2975 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2976 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2977 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2978 MSYMBOL_TYPE (msymbol) == ourtype4)
2981 || re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
2983 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
2985 /* FIXME: carlton/2003-02-04: Given that the
2986 semantics of lookup_symbol keeps on changing
2987 slightly, it would be a nice idea if we had a
2988 function lookup_symbol_minsym that found the
2989 symbol associated to a given minimal symbol (if
2991 if (kind == FUNCTIONS_NAMESPACE
2992 || lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol),
2993 (struct block *) NULL,
2995 0, (struct symtab **) NULL) == NULL)
3003 ALL_SYMTABS (objfile, s)
3005 bv = BLOCKVECTOR (s);
3006 /* Often many files share a blockvector.
3007 Scan each blockvector only once so that
3008 we don't get every symbol many times.
3009 It happens that the first symtab in the list
3010 for any given blockvector is the main file. */
3012 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3014 struct symbol_search *prevtail = tail;
3016 b = BLOCKVECTOR_BLOCK (bv, i);
3017 ALL_BLOCK_SYMBOLS (b, j, sym)
3020 if (file_matches (s->filename, files, nfiles)
3022 || re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
3023 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3024 && SYMBOL_CLASS (sym) != LOC_BLOCK
3025 && SYMBOL_CLASS (sym) != LOC_CONST)
3026 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)
3027 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3028 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK))))
3031 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3035 psr->msymbol = NULL;
3047 if (prevtail == NULL)
3049 struct symbol_search dummy;
3052 tail = sort_search_symbols (&dummy, nfound);
3055 old_chain = make_cleanup_free_search_symbols (sr);
3058 tail = sort_search_symbols (prevtail, nfound);
3064 /* If there are no eyes, avoid all contact. I mean, if there are
3065 no debug symbols, then print directly from the msymbol_vector. */
3067 if (found_misc || kind != FUNCTIONS_NAMESPACE)
3069 ALL_MSYMBOLS (objfile, msymbol)
3071 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3072 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3073 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3074 MSYMBOL_TYPE (msymbol) == ourtype4)
3077 || re_exec (SYMBOL_NATURAL_NAME (msymbol)) != 0)
3079 /* Functions: Look up by address. */
3080 if (kind != FUNCTIONS_NAMESPACE ||
3081 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
3083 /* Variables/Absolutes: Look up by name */
3084 if (lookup_symbol (DEPRECATED_SYMBOL_NAME (msymbol),
3085 (struct block *) NULL, VAR_NAMESPACE,
3086 0, (struct symtab **) NULL) == NULL)
3089 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3091 psr->msymbol = msymbol;
3098 old_chain = make_cleanup_free_search_symbols (sr);
3112 discard_cleanups (old_chain);
3115 /* Helper function for symtab_symbol_info, this function uses
3116 the data returned from search_symbols() to print information
3117 regarding the match to gdb_stdout.
3120 print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym,
3121 int block, char *last)
3123 if (last == NULL || strcmp (last, s->filename) != 0)
3125 fputs_filtered ("\nFile ", gdb_stdout);
3126 fputs_filtered (s->filename, gdb_stdout);
3127 fputs_filtered (":\n", gdb_stdout);
3130 if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK)
3131 printf_filtered ("static ");
3133 /* Typedef that is not a C++ class */
3134 if (kind == TYPES_NAMESPACE
3135 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
3136 typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3137 /* variable, func, or typedef-that-is-c++-class */
3138 else if (kind < TYPES_NAMESPACE ||
3139 (kind == TYPES_NAMESPACE &&
3140 SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE))
3142 type_print (SYMBOL_TYPE (sym),
3143 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3144 ? "" : SYMBOL_PRINT_NAME (sym)),
3147 printf_filtered (";\n");
3151 /* This help function for symtab_symbol_info() prints information
3152 for non-debugging symbols to gdb_stdout.
3155 print_msymbol_info (struct minimal_symbol *msymbol)
3159 if (TARGET_ADDR_BIT <= 32)
3160 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3161 & (CORE_ADDR) 0xffffffff,
3164 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3166 printf_filtered ("%s %s\n",
3167 tmp, SYMBOL_PRINT_NAME (msymbol));
3170 /* This is the guts of the commands "info functions", "info types", and
3171 "info variables". It calls search_symbols to find all matches and then
3172 print_[m]symbol_info to print out some useful information about the
3176 symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty)
3178 static char *classnames[]
3180 {"variable", "function", "type", "method"};
3181 struct symbol_search *symbols;
3182 struct symbol_search *p;
3183 struct cleanup *old_chain;
3184 char *last_filename = NULL;
3187 /* must make sure that if we're interrupted, symbols gets freed */
3188 search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3189 old_chain = make_cleanup_free_search_symbols (symbols);
3191 printf_filtered (regexp
3192 ? "All %ss matching regular expression \"%s\":\n"
3193 : "All defined %ss:\n",
3194 classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp);
3196 for (p = symbols; p != NULL; p = p->next)
3200 if (p->msymbol != NULL)
3204 printf_filtered ("\nNon-debugging symbols:\n");
3207 print_msymbol_info (p->msymbol);
3211 print_symbol_info (kind,
3216 last_filename = p->symtab->filename;
3220 do_cleanups (old_chain);
3224 variables_info (char *regexp, int from_tty)
3226 symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty);
3230 functions_info (char *regexp, int from_tty)
3232 symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty);
3237 types_info (char *regexp, int from_tty)
3239 symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty);
3242 /* Breakpoint all functions matching regular expression. */
3245 rbreak_command_wrapper (char *regexp, int from_tty)
3247 rbreak_command (regexp, from_tty);
3251 rbreak_command (char *regexp, int from_tty)
3253 struct symbol_search *ss;
3254 struct symbol_search *p;
3255 struct cleanup *old_chain;
3257 search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss);
3258 old_chain = make_cleanup_free_search_symbols (ss);
3260 for (p = ss; p != NULL; p = p->next)
3262 if (p->msymbol == NULL)
3264 char *string = (char *) alloca (strlen (p->symtab->filename)
3265 + strlen (DEPRECATED_SYMBOL_NAME (p->symbol))
3267 strcpy (string, p->symtab->filename);
3268 strcat (string, ":'");
3269 strcat (string, DEPRECATED_SYMBOL_NAME (p->symbol));
3270 strcat (string, "'");
3271 break_command (string, from_tty);
3272 print_symbol_info (FUNCTIONS_NAMESPACE,
3276 p->symtab->filename);
3280 break_command (DEPRECATED_SYMBOL_NAME (p->msymbol), from_tty);
3281 printf_filtered ("<function, no debug info> %s;\n",
3282 SYMBOL_PRINT_NAME (p->msymbol));
3286 do_cleanups (old_chain);
3290 /* Helper routine for make_symbol_completion_list. */
3292 static int return_val_size;
3293 static int return_val_index;
3294 static char **return_val;
3296 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3298 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
3299 /* Put only the mangled name on the list. */ \
3300 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
3301 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
3302 completion_list_add_name \
3303 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
3305 completion_list_add_name \
3306 (DEPRECATED_SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
3309 /* Test to see if the symbol specified by SYMNAME (which is already
3310 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3311 characters. If so, add it to the current completion list. */
3314 completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
3315 char *text, char *word)
3320 /* clip symbols that cannot match */
3322 if (strncmp (symname, sym_text, sym_text_len) != 0)
3327 /* We have a match for a completion, so add SYMNAME to the current list
3328 of matches. Note that the name is moved to freshly malloc'd space. */
3332 if (word == sym_text)
3334 new = xmalloc (strlen (symname) + 5);
3335 strcpy (new, symname);
3337 else if (word > sym_text)
3339 /* Return some portion of symname. */
3340 new = xmalloc (strlen (symname) + 5);
3341 strcpy (new, symname + (word - sym_text));
3345 /* Return some of SYM_TEXT plus symname. */
3346 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
3347 strncpy (new, word, sym_text - word);
3348 new[sym_text - word] = '\0';
3349 strcat (new, symname);
3352 if (return_val_index + 3 > return_val_size)
3354 newsize = (return_val_size *= 2) * sizeof (char *);
3355 return_val = (char **) xrealloc ((char *) return_val, newsize);
3357 return_val[return_val_index++] = new;
3358 return_val[return_val_index] = NULL;
3362 /* Return a NULL terminated array of all symbols (regardless of class)
3363 which begin by matching TEXT. If the answer is no symbols, then
3364 the return value is an array which contains only a NULL pointer.
3366 Problem: All of the symbols have to be copied because readline frees them.
3367 I'm not going to worry about this; hopefully there won't be that many. */
3370 make_symbol_completion_list (char *text, char *word)
3372 register struct symbol *sym;
3373 register struct symtab *s;
3374 register struct partial_symtab *ps;
3375 register struct minimal_symbol *msymbol;
3376 register struct objfile *objfile;
3377 register struct block *b, *surrounding_static_block = 0;
3379 struct partial_symbol **psym;
3380 /* The symbol we are completing on. Points in same buffer as text. */
3382 /* Length of sym_text. */
3385 /* Now look for the symbol we are supposed to complete on.
3386 FIXME: This should be language-specific. */
3390 char *quote_pos = NULL;
3392 /* First see if this is a quoted string. */
3394 for (p = text; *p != '\0'; ++p)
3396 if (quote_found != '\0')
3398 if (*p == quote_found)
3399 /* Found close quote. */
3401 else if (*p == '\\' && p[1] == quote_found)
3402 /* A backslash followed by the quote character
3403 doesn't end the string. */
3406 else if (*p == '\'' || *p == '"')
3412 if (quote_found == '\'')
3413 /* A string within single quotes can be a symbol, so complete on it. */
3414 sym_text = quote_pos + 1;
3415 else if (quote_found == '"')
3416 /* A double-quoted string is never a symbol, nor does it make sense
3417 to complete it any other way. */
3419 return_val = (char **) xmalloc (sizeof (char *));
3420 return_val[0] = NULL;
3425 /* It is not a quoted string. Break it based on the characters
3426 which are in symbols. */
3429 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3438 sym_text_len = strlen (sym_text);
3440 return_val_size = 100;
3441 return_val_index = 0;
3442 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3443 return_val[0] = NULL;
3445 /* Look through the partial symtabs for all symbols which begin
3446 by matching SYM_TEXT. Add each one that you find to the list. */
3448 ALL_PSYMTABS (objfile, ps)
3450 /* If the psymtab's been read in we'll get it when we search
3451 through the blockvector. */
3455 for (psym = objfile->global_psymbols.list + ps->globals_offset;
3456 psym < (objfile->global_psymbols.list + ps->globals_offset
3457 + ps->n_global_syms);
3460 /* If interrupted, then quit. */
3462 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3465 for (psym = objfile->static_psymbols.list + ps->statics_offset;
3466 psym < (objfile->static_psymbols.list + ps->statics_offset
3467 + ps->n_static_syms);
3471 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3475 /* At this point scan through the misc symbol vectors and add each
3476 symbol you find to the list. Eventually we want to ignore
3477 anything that isn't a text symbol (everything else will be
3478 handled by the psymtab code above). */
3480 ALL_MSYMBOLS (objfile, msymbol)
3483 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3486 /* Search upwards from currently selected frame (so that we can
3487 complete on local vars. */
3489 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
3491 if (!BLOCK_SUPERBLOCK (b))
3493 surrounding_static_block = b; /* For elmin of dups */
3496 /* Also catch fields of types defined in this places which match our
3497 text string. Only complete on types visible from current context. */
3499 ALL_BLOCK_SYMBOLS (b, i, sym)
3501 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3502 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3504 struct type *t = SYMBOL_TYPE (sym);
3505 enum type_code c = TYPE_CODE (t);
3507 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
3509 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
3511 if (TYPE_FIELD_NAME (t, j))
3513 completion_list_add_name (TYPE_FIELD_NAME (t, j),
3514 sym_text, sym_text_len, text, word);
3522 /* Go through the symtabs and check the externs and statics for
3523 symbols which match. */
3525 ALL_SYMTABS (objfile, s)
3528 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3529 ALL_BLOCK_SYMBOLS (b, i, sym)
3531 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3535 ALL_SYMTABS (objfile, s)
3538 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3539 /* Don't do this block twice. */
3540 if (b == surrounding_static_block)
3542 ALL_BLOCK_SYMBOLS (b, i, sym)
3544 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3548 return (return_val);
3551 /* Like make_symbol_completion_list, but returns a list of symbols
3552 defined in a source file FILE. */
3555 make_file_symbol_completion_list (char *text, char *word, char *srcfile)
3557 register struct symbol *sym;
3558 register struct symtab *s;
3559 register struct block *b;
3561 /* The symbol we are completing on. Points in same buffer as text. */
3563 /* Length of sym_text. */
3566 /* Now look for the symbol we are supposed to complete on.
3567 FIXME: This should be language-specific. */
3571 char *quote_pos = NULL;
3573 /* First see if this is a quoted string. */
3575 for (p = text; *p != '\0'; ++p)
3577 if (quote_found != '\0')
3579 if (*p == quote_found)
3580 /* Found close quote. */
3582 else if (*p == '\\' && p[1] == quote_found)
3583 /* A backslash followed by the quote character
3584 doesn't end the string. */
3587 else if (*p == '\'' || *p == '"')
3593 if (quote_found == '\'')
3594 /* A string within single quotes can be a symbol, so complete on it. */
3595 sym_text = quote_pos + 1;
3596 else if (quote_found == '"')
3597 /* A double-quoted string is never a symbol, nor does it make sense
3598 to complete it any other way. */
3600 return_val = (char **) xmalloc (sizeof (char *));
3601 return_val[0] = NULL;
3606 /* It is not a quoted string. Break it based on the characters
3607 which are in symbols. */
3610 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3619 sym_text_len = strlen (sym_text);
3621 return_val_size = 10;
3622 return_val_index = 0;
3623 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3624 return_val[0] = NULL;
3626 /* Find the symtab for SRCFILE (this loads it if it was not yet read
3628 s = lookup_symtab (srcfile);
3631 /* Maybe they typed the file with leading directories, while the
3632 symbol tables record only its basename. */
3633 const char *tail = lbasename (srcfile);
3636 s = lookup_symtab (tail);
3639 /* If we have no symtab for that file, return an empty list. */
3641 return (return_val);
3643 /* Go through this symtab and check the externs and statics for
3644 symbols which match. */
3646 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3647 ALL_BLOCK_SYMBOLS (b, i, sym)
3649 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3652 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3653 ALL_BLOCK_SYMBOLS (b, i, sym)
3655 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3658 return (return_val);
3661 /* A helper function for make_source_files_completion_list. It adds
3662 another file name to a list of possible completions, growing the
3663 list as necessary. */
3666 add_filename_to_list (const char *fname, char *text, char *word,
3667 char ***list, int *list_used, int *list_alloced)
3670 size_t fnlen = strlen (fname);
3672 if (*list_used + 1 >= *list_alloced)
3675 *list = (char **) xrealloc ((char *) *list,
3676 *list_alloced * sizeof (char *));
3681 /* Return exactly fname. */
3682 new = xmalloc (fnlen + 5);
3683 strcpy (new, fname);
3685 else if (word > text)
3687 /* Return some portion of fname. */
3688 new = xmalloc (fnlen + 5);
3689 strcpy (new, fname + (word - text));
3693 /* Return some of TEXT plus fname. */
3694 new = xmalloc (fnlen + (text - word) + 5);
3695 strncpy (new, word, text - word);
3696 new[text - word] = '\0';
3697 strcat (new, fname);
3699 (*list)[*list_used] = new;
3700 (*list)[++*list_used] = NULL;
3704 not_interesting_fname (const char *fname)
3706 static const char *illegal_aliens[] = {
3707 "_globals_", /* inserted by coff_symtab_read */
3712 for (i = 0; illegal_aliens[i]; i++)
3714 if (strcmp (fname, illegal_aliens[i]) == 0)
3720 /* Return a NULL terminated array of all source files whose names
3721 begin with matching TEXT. The file names are looked up in the
3722 symbol tables of this program. If the answer is no matchess, then
3723 the return value is an array which contains only a NULL pointer. */
3726 make_source_files_completion_list (char *text, char *word)
3728 register struct symtab *s;
3729 register struct partial_symtab *ps;
3730 register struct objfile *objfile;
3732 int list_alloced = 1;
3734 size_t text_len = strlen (text);
3735 char **list = (char **) xmalloc (list_alloced * sizeof (char *));
3736 const char *base_name;
3740 if (!have_full_symbols () && !have_partial_symbols ())
3743 ALL_SYMTABS (objfile, s)
3745 if (not_interesting_fname (s->filename))
3747 if (!filename_seen (s->filename, 1, &first)
3748 #if HAVE_DOS_BASED_FILE_SYSTEM
3749 && strncasecmp (s->filename, text, text_len) == 0
3751 && strncmp (s->filename, text, text_len) == 0
3755 /* This file matches for a completion; add it to the current
3757 add_filename_to_list (s->filename, text, word,
3758 &list, &list_used, &list_alloced);
3762 /* NOTE: We allow the user to type a base name when the
3763 debug info records leading directories, but not the other
3764 way around. This is what subroutines of breakpoint
3765 command do when they parse file names. */
3766 base_name = lbasename (s->filename);
3767 if (base_name != s->filename
3768 && !filename_seen (base_name, 1, &first)
3769 #if HAVE_DOS_BASED_FILE_SYSTEM
3770 && strncasecmp (base_name, text, text_len) == 0
3772 && strncmp (base_name, text, text_len) == 0
3775 add_filename_to_list (base_name, text, word,
3776 &list, &list_used, &list_alloced);
3780 ALL_PSYMTABS (objfile, ps)
3782 if (not_interesting_fname (ps->filename))
3786 if (!filename_seen (ps->filename, 1, &first)
3787 #if HAVE_DOS_BASED_FILE_SYSTEM
3788 && strncasecmp (ps->filename, text, text_len) == 0
3790 && strncmp (ps->filename, text, text_len) == 0
3794 /* This file matches for a completion; add it to the
3795 current list of matches. */
3796 add_filename_to_list (ps->filename, text, word,
3797 &list, &list_used, &list_alloced);
3802 base_name = lbasename (ps->filename);
3803 if (base_name != ps->filename
3804 && !filename_seen (base_name, 1, &first)
3805 #if HAVE_DOS_BASED_FILE_SYSTEM
3806 && strncasecmp (base_name, text, text_len) == 0
3808 && strncmp (base_name, text, text_len) == 0
3811 add_filename_to_list (base_name, text, word,
3812 &list, &list_used, &list_alloced);
3820 /* Determine if PC is in the prologue of a function. The prologue is the area
3821 between the first instruction of a function, and the first executable line.
3822 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3824 If non-zero, func_start is where we think the prologue starts, possibly
3825 by previous examination of symbol table information.
3829 in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
3831 struct symtab_and_line sal;
3832 CORE_ADDR func_addr, func_end;
3834 /* We have several sources of information we can consult to figure
3836 - Compilers usually emit line number info that marks the prologue
3837 as its own "source line". So the ending address of that "line"
3838 is the end of the prologue. If available, this is the most
3840 - The minimal symbols and partial symbols, which can usually tell
3841 us the starting and ending addresses of a function.
3842 - If we know the function's start address, we can call the
3843 architecture-defined SKIP_PROLOGUE function to analyze the
3844 instruction stream and guess where the prologue ends.
3845 - Our `func_start' argument; if non-zero, this is the caller's
3846 best guess as to the function's entry point. At the time of
3847 this writing, handle_inferior_event doesn't get this right, so
3848 it should be our last resort. */
3850 /* Consult the partial symbol table, to find which function
3852 if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3854 CORE_ADDR prologue_end;
3856 /* We don't even have minsym information, so fall back to using
3857 func_start, if given. */
3859 return 1; /* We *might* be in a prologue. */
3861 prologue_end = SKIP_PROLOGUE (func_start);
3863 return func_start <= pc && pc < prologue_end;
3866 /* If we have line number information for the function, that's
3867 usually pretty reliable. */
3868 sal = find_pc_line (func_addr, 0);
3870 /* Now sal describes the source line at the function's entry point,
3871 which (by convention) is the prologue. The end of that "line",
3872 sal.end, is the end of the prologue.
3874 Note that, for functions whose source code is all on a single
3875 line, the line number information doesn't always end up this way.
3876 So we must verify that our purported end-of-prologue address is
3877 *within* the function, not at its start or end. */
3879 || sal.end <= func_addr
3880 || func_end <= sal.end)
3882 /* We don't have any good line number info, so use the minsym
3883 information, together with the architecture-specific prologue
3885 CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
3887 return func_addr <= pc && pc < prologue_end;
3890 /* We have line number info, and it looks good. */
3891 return func_addr <= pc && pc < sal.end;
3895 /* Begin overload resolution functions */
3898 remove_params (const char *demangled_name)
3904 if (demangled_name == NULL)
3907 /* First find the end of the arg list. */
3908 argp = strrchr (demangled_name, ')');
3912 /* Back up to the beginning. */
3915 while (argp-- > demangled_name)
3919 else if (*argp == '(')
3928 internal_error (__FILE__, __LINE__,
3929 "bad demangled name %s\n", demangled_name);
3930 while (argp[-1] == ' ' && argp > demangled_name)
3933 new_name = xmalloc (argp - demangled_name + 1);
3934 memcpy (new_name, demangled_name, argp - demangled_name);
3935 new_name[argp - demangled_name] = '\0';
3939 /* Helper routine for make_symbol_completion_list. */
3941 static int sym_return_val_size;
3942 static int sym_return_val_index;
3943 static struct symbol **sym_return_val;
3945 /* Test to see if the symbol specified by SYMNAME (which is already
3946 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3947 characters. If so, add it to the current completion list. */
3950 overload_list_add_symbol (struct symbol *sym, char *oload_name)
3956 /* If there is no type information, we can't do anything, so skip */
3957 if (SYMBOL_TYPE (sym) == NULL)
3960 /* skip any symbols that we've already considered. */
3961 for (i = 0; i < sym_return_val_index; ++i)
3962 if (!strcmp (DEPRECATED_SYMBOL_NAME (sym), DEPRECATED_SYMBOL_NAME (sym_return_val[i])))
3965 /* Get the demangled name without parameters */
3966 sym_name = remove_params (SYMBOL_DEMANGLED_NAME (sym));
3970 /* skip symbols that cannot match */
3971 if (strcmp (sym_name, oload_name) != 0)
3979 /* We have a match for an overload instance, so add SYM to the current list
3980 * of overload instances */
3981 if (sym_return_val_index + 3 > sym_return_val_size)
3983 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
3984 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
3986 sym_return_val[sym_return_val_index++] = sym;
3987 sym_return_val[sym_return_val_index] = NULL;
3990 /* Return a null-terminated list of pointers to function symbols that
3991 * match name of the supplied symbol FSYM.
3992 * This is used in finding all overloaded instances of a function name.
3993 * This has been modified from make_symbol_completion_list. */
3997 make_symbol_overload_list (struct symbol *fsym)
3999 register struct symbol *sym;
4000 register struct symtab *s;
4001 register struct partial_symtab *ps;
4002 register struct objfile *objfile;
4003 register struct block *b, *surrounding_static_block = 0;
4005 /* The name we are completing on. */
4006 char *oload_name = NULL;
4007 /* Length of name. */
4008 int oload_name_len = 0;
4010 /* Look for the symbol we are supposed to complete on. */
4012 oload_name = remove_params (SYMBOL_DEMANGLED_NAME (fsym));
4015 sym_return_val_size = 1;
4016 sym_return_val = (struct symbol **) xmalloc (2 * sizeof (struct symbol *));
4017 sym_return_val[0] = fsym;
4018 sym_return_val[1] = NULL;
4020 return sym_return_val;
4022 oload_name_len = strlen (oload_name);
4024 sym_return_val_size = 100;
4025 sym_return_val_index = 0;
4026 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
4027 sym_return_val[0] = NULL;
4029 /* Read in all partial symtabs containing a partial symbol named
4032 ALL_PSYMTABS (objfile, ps)
4034 struct partial_symbol **psym;
4036 /* If the psymtab's been read in we'll get it when we search
4037 through the blockvector. */
4041 if ((lookup_partial_symbol (ps, oload_name, NULL, 1, VAR_NAMESPACE)
4043 || (lookup_partial_symbol (ps, oload_name, NULL, 0, VAR_NAMESPACE)
4045 PSYMTAB_TO_SYMTAB (ps);
4048 /* Search upwards from currently selected frame (so that we can
4049 complete on local vars. */
4051 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
4053 if (!BLOCK_SUPERBLOCK (b))
4055 surrounding_static_block = b; /* For elimination of dups */
4058 /* Also catch fields of types defined in this places which match our
4059 text string. Only complete on types visible from current context. */
4061 ALL_BLOCK_SYMBOLS (b, i, sym)
4063 overload_list_add_symbol (sym, oload_name);
4067 /* Go through the symtabs and check the externs and statics for
4068 symbols which match. */
4070 ALL_SYMTABS (objfile, s)
4073 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4074 ALL_BLOCK_SYMBOLS (b, i, sym)
4076 overload_list_add_symbol (sym, oload_name);
4080 ALL_SYMTABS (objfile, s)
4083 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4084 /* Don't do this block twice. */
4085 if (b == surrounding_static_block)
4087 ALL_BLOCK_SYMBOLS (b, i, sym)
4089 overload_list_add_symbol (sym, oload_name);
4095 return (sym_return_val);
4098 /* End of overload resolution functions */
4100 struct symtabs_and_lines
4101 decode_line_spec (char *string, int funfirstline)
4103 struct symtabs_and_lines sals;
4104 struct symtab_and_line cursal;
4107 error ("Empty line specification.");
4109 /* We use whatever is set as the current source line. We do not try
4110 and get a default or it will recursively call us! */
4111 cursal = get_current_source_symtab_and_line ();
4113 sals = decode_line_1 (&string, funfirstline,
4114 cursal.symtab, cursal.line,
4118 error ("Junk at end of line specification: %s", string);
4123 static char *name_of_main;
4126 set_main_name (const char *name)
4128 if (name_of_main != NULL)
4130 xfree (name_of_main);
4131 name_of_main = NULL;
4135 name_of_main = xstrdup (name);
4142 if (name_of_main != NULL)
4143 return name_of_main;
4150 _initialize_symtab (void)
4152 add_info ("variables", variables_info,
4153 "All global and static variable names, or those matching REGEXP.");
4155 add_com ("whereis", class_info, variables_info,
4156 "All global and static variable names, or those matching REGEXP.");
4158 add_info ("functions", functions_info,
4159 "All function names, or those matching REGEXP.");
4162 /* FIXME: This command has at least the following problems:
4163 1. It prints builtin types (in a very strange and confusing fashion).
4164 2. It doesn't print right, e.g. with
4165 typedef struct foo *FOO
4166 type_print prints "FOO" when we want to make it (in this situation)
4167 print "struct foo *".
4168 I also think "ptype" or "whatis" is more likely to be useful (but if
4169 there is much disagreement "info types" can be fixed). */
4170 add_info ("types", types_info,
4171 "All type names, or those matching REGEXP.");
4173 add_info ("sources", sources_info,
4174 "Source files in the program.");
4176 add_com ("rbreak", class_breakpoint, rbreak_command,
4177 "Set a breakpoint for all functions matching REGEXP.");
4181 add_com ("lf", class_info, sources_info, "Source files in the program");
4182 add_com ("lg", class_info, variables_info,
4183 "All global and static variable names, or those matching REGEXP.");
4186 /* Initialize the one built-in type that isn't language dependent... */
4187 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4188 "<unknown type>", (struct objfile *) NULL);