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 Free Software
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 */
44 #include "gdb_obstack.h"
46 #include <sys/types.h>
48 #include "gdb_string.h"
53 /* Prototypes for local functions */
55 static void completion_list_add_name (char *, char *, int, char *, char *);
57 static void rbreak_command (char *, int);
59 static void types_info (char *, int);
61 static void functions_info (char *, int);
63 static void variables_info (char *, int);
65 static void sources_info (char *, int);
67 static void output_source_filename (char *, int *);
69 static int find_line_common (struct linetable *, int, int *);
71 /* This one is used by linespec.c */
73 char *operator_chars (char *p, char **end);
75 static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *,
79 static struct symbol *lookup_symbol_aux (const char *name,
80 const char *mangled_name,
81 const struct block *block,
82 const namespace_enum namespace,
83 int *is_a_field_of_this,
84 struct symtab **symtab);
86 static struct symbol *lookup_symbol_aux_local (const char *name,
87 const char *mangled_name,
88 const struct block *block,
89 const namespace_enum namespace,
90 struct symtab **symtab);
93 struct symbol *lookup_symbol_aux_symtabs (int block_index,
95 const char *mangled_name,
96 const namespace_enum namespace,
97 struct symtab **symtab);
100 struct symbol *lookup_symbol_aux_psymtabs (int block_index,
102 const char *mangled_name,
103 const namespace_enum namespace,
104 struct symtab **symtab);
106 static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr);
108 /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
109 /* Signals the presence of objects compiled by HP compilers */
110 int hp_som_som_object_present = 0;
112 static void fixup_section (struct general_symbol_info *, struct objfile *);
114 static int file_matches (char *, char **, int);
116 static void print_symbol_info (namespace_enum,
117 struct symtab *, struct symbol *, int, char *);
119 static void print_msymbol_info (struct minimal_symbol *);
121 static void symtab_symbol_info (char *, namespace_enum, int);
123 static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
125 void _initialize_symtab (void);
129 /* The single non-language-specific builtin type */
130 struct type *builtin_type_error;
132 /* Block in which the most recently searched-for symbol was found.
133 Might be better to make this a parameter to lookup_symbol and
136 const struct block *block_found;
138 /* Check for a symtab of a specific name; first in symtabs, then in
139 psymtabs. *If* there is no '/' in the name, a match after a '/'
140 in the symtab filename will also work. */
143 lookup_symtab (const char *name)
145 register struct symtab *s;
146 register struct partial_symtab *ps;
147 register struct objfile *objfile;
148 char *real_path = NULL;
149 char *full_path = NULL;
151 /* Here we are interested in canonicalizing an absolute path, not
152 absolutizing a relative path. */
153 if (IS_ABSOLUTE_PATH (name))
155 full_path = xfullpath (name);
156 make_cleanup (xfree, full_path);
157 real_path = gdb_realpath (name);
158 make_cleanup (xfree, real_path);
163 /* First, search for an exact match */
165 ALL_SYMTABS (objfile, s)
167 if (FILENAME_CMP (name, s->filename) == 0)
172 /* If the user gave us an absolute path, try to find the file in
173 this symtab and use its absolute path. */
175 if (full_path != NULL)
177 const char *fp = symtab_to_filename (s);
178 if (FILENAME_CMP (full_path, fp) == 0)
184 if (real_path != NULL)
186 char *rp = gdb_realpath (symtab_to_filename (s));
187 make_cleanup (xfree, rp);
188 if (FILENAME_CMP (real_path, rp) == 0)
195 /* Now, search for a matching tail (only if name doesn't have any dirs) */
197 if (lbasename (name) == name)
198 ALL_SYMTABS (objfile, s)
200 if (FILENAME_CMP (lbasename (s->filename), name) == 0)
204 /* Same search rules as above apply here, but now we look thru the
207 ps = lookup_partial_symtab (name);
212 error ("Internal: readin %s pst for `%s' found when no symtab found.",
215 s = PSYMTAB_TO_SYMTAB (ps);
220 /* At this point, we have located the psymtab for this file, but
221 the conversion to a symtab has failed. This usually happens
222 when we are looking up an include file. In this case,
223 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
224 been created. So, we need to run through the symtabs again in
225 order to find the file.
226 XXX - This is a crock, and should be fixed inside of the the
227 symbol parsing routines. */
231 /* Lookup the partial symbol table of a source file named NAME.
232 *If* there is no '/' in the name, a match after a '/'
233 in the psymtab filename will also work. */
235 struct partial_symtab *
236 lookup_partial_symtab (const char *name)
238 register struct partial_symtab *pst;
239 register struct objfile *objfile;
240 char *full_path = NULL;
241 char *real_path = NULL;
243 /* Here we are interested in canonicalizing an absolute path, not
244 absolutizing a relative path. */
245 if (IS_ABSOLUTE_PATH (name))
247 full_path = xfullpath (name);
248 make_cleanup (xfree, full_path);
249 real_path = gdb_realpath (name);
250 make_cleanup (xfree, real_path);
253 ALL_PSYMTABS (objfile, pst)
255 if (FILENAME_CMP (name, pst->filename) == 0)
260 /* If the user gave us an absolute path, try to find the file in
261 this symtab and use its absolute path. */
262 if (full_path != NULL)
264 if (pst->fullname == NULL)
265 source_full_path_of (pst->filename, &pst->fullname);
266 if (pst->fullname != NULL
267 && FILENAME_CMP (full_path, pst->fullname) == 0)
273 if (real_path != NULL)
276 if (pst->fullname == NULL)
277 source_full_path_of (pst->filename, &pst->fullname);
278 if (pst->fullname != NULL)
280 rp = gdb_realpath (pst->fullname);
281 make_cleanup (xfree, rp);
283 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
290 /* Now, search for a matching tail (only if name doesn't have any dirs) */
292 if (lbasename (name) == name)
293 ALL_PSYMTABS (objfile, pst)
295 if (FILENAME_CMP (lbasename (pst->filename), name) == 0)
302 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
303 full method name, which consist of the class name (from T), the unadorned
304 method name from METHOD_ID, and the signature for the specific overload,
305 specified by SIGNATURE_ID. Note that this function is g++ specific. */
308 gdb_mangle_name (struct type *type, int method_id, int signature_id)
310 int mangled_name_len;
312 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
313 struct fn_field *method = &f[signature_id];
314 char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
315 char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
316 char *newname = type_name_no_tag (type);
318 /* Does the form of physname indicate that it is the full mangled name
319 of a constructor (not just the args)? */
320 int is_full_physname_constructor;
323 int is_destructor = is_destructor_name (physname);
324 /* Need a new type prefix. */
325 char *const_prefix = method->is_const ? "C" : "";
326 char *volatile_prefix = method->is_volatile ? "V" : "";
328 int len = (newname == NULL ? 0 : strlen (newname));
330 /* Nothing to do if physname already contains a fully mangled v3 abi name
331 or an operator name. */
332 if ((physname[0] == '_' && physname[1] == 'Z')
333 || is_operator_name (field_name))
334 return xstrdup (physname);
336 is_full_physname_constructor = is_constructor_name (physname);
339 is_full_physname_constructor || (newname && STREQ (field_name, newname));
342 is_destructor = (strncmp (physname, "__dt", 4) == 0);
344 if (is_destructor || is_full_physname_constructor)
346 mangled_name = (char *) xmalloc (strlen (physname) + 1);
347 strcpy (mangled_name, physname);
353 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
355 else if (physname[0] == 't' || physname[0] == 'Q')
357 /* The physname for template and qualified methods already includes
359 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
365 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
367 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
368 + strlen (buf) + len + strlen (physname) + 1);
371 mangled_name = (char *) xmalloc (mangled_name_len);
373 mangled_name[0] = '\0';
375 strcpy (mangled_name, field_name);
377 strcat (mangled_name, buf);
378 /* If the class doesn't have a name, i.e. newname NULL, then we just
379 mangle it using 0 for the length of the class. Thus it gets mangled
380 as something starting with `::' rather than `classname::'. */
382 strcat (mangled_name, newname);
384 strcat (mangled_name, physname);
385 return (mangled_name);
389 /* Initialize the language dependent portion of a symbol
390 depending upon the language for the symbol. */
392 symbol_init_language_specific (struct general_symbol_info *gsymbol,
393 enum language language)
395 gsymbol->language = language;
396 if (gsymbol->language == language_cplus
397 || gsymbol->language == language_java)
399 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
401 else if (gsymbol->language == language_objc)
403 gsymbol->language_specific.objc_specific.demangled_name = NULL;
405 /* OBSOLETE else if (SYMBOL_LANGUAGE (symbol) == language_chill) */
407 /* OBSOLETE SYMBOL_CHILL_DEMANGLED_NAME (symbol) = NULL; */
411 memset (&gsymbol->language_specific, 0,
412 sizeof (gsymbol->language_specific));
416 /* Initialize a symbol's mangled name. */
418 /* Try to initialize the demangled name for a symbol, based on the
419 language of that symbol. If the language is set to language_auto,
420 it will attempt to find any demangling algorithm that works and
421 then set the language appropriately. If no demangling of any kind
422 is found, the language is set back to language_unknown, so we can
423 avoid doing this work again the next time we encounter the symbol.
424 Any required space to store the name is obtained from the specified
428 symbol_init_demangled_name (struct general_symbol_info *gsymbol,
429 struct obstack *obstack)
431 char *mangled = gsymbol->name;
432 char *demangled = NULL;
434 if (gsymbol->language == language_unknown)
435 gsymbol->language = language_auto;
436 if (gsymbol->language == language_cplus
437 || gsymbol->language == language_auto)
440 cplus_demangle (gsymbol->name, DMGL_PARAMS | DMGL_ANSI);
441 if (demangled != NULL)
443 gsymbol->language = language_cplus;
444 gsymbol->language_specific.cplus_specific.demangled_name =
445 obsavestring (demangled, strlen (demangled), obstack);
450 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
453 if (gsymbol->language == language_java)
456 cplus_demangle (gsymbol->name,
457 DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
458 if (demangled != NULL)
460 gsymbol->language = language_java;
461 gsymbol->language_specific.cplus_specific.demangled_name =
462 obsavestring (demangled, strlen (demangled), obstack);
467 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
471 /* OBSOLETE if (demangled == NULL */
472 /* OBSOLETE && (gsymbol->language == language_chill */
473 /* OBSOLETE || gsymbol->language == language_auto)) */
475 /* OBSOLETE demangled = */
476 /* OBSOLETE chill_demangle (gsymbol->name); */
477 /* OBSOLETE if (demangled != NULL) */
479 /* OBSOLETE gsymbol->language = language_chill; */
480 /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = */
481 /* OBSOLETE obsavestring (demangled, strlen (demangled), obstack); */
482 /* OBSOLETE xfree (demangled); */
486 /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = NULL; */
492 /* Return the demangled name for a symbol based on the language for
493 that symbol. If no demangled name exists, return NULL. */
495 symbol_demangled_name (struct general_symbol_info *gsymbol)
497 if (gsymbol->language == language_cplus
498 || gsymbol->language == language_java)
499 return gsymbol->language_specific.cplus_specific.demangled_name;
501 else if (gsymbol->language == language_objc)
502 return gsymbol->language_specific.objc_specific.demangled_name;
507 /* OBSOLETE (SYMBOL_LANGUAGE (symbol) == language_chill */
508 /* OBSOLETE ? SYMBOL_CHILL_DEMANGLED_NAME (symbol) */
511 /* Initialize the structure fields to zero values. */
513 init_sal (struct symtab_and_line *sal)
524 /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
526 struct partial_symtab *
527 find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
529 register struct partial_symtab *pst;
530 register struct objfile *objfile;
531 struct minimal_symbol *msymbol;
533 /* If we know that this is not a text address, return failure. This is
534 necessary because we loop based on texthigh and textlow, which do
535 not include the data ranges. */
536 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
538 && (msymbol->type == mst_data
539 || msymbol->type == mst_bss
540 || msymbol->type == mst_abs
541 || msymbol->type == mst_file_data
542 || msymbol->type == mst_file_bss))
545 ALL_PSYMTABS (objfile, pst)
547 if (pc >= pst->textlow && pc < pst->texthigh)
549 struct partial_symtab *tpst;
551 /* An objfile that has its functions reordered might have
552 many partial symbol tables containing the PC, but
553 we want the partial symbol table that contains the
554 function containing the PC. */
555 if (!(objfile->flags & OBJF_REORDERED) &&
556 section == 0) /* can't validate section this way */
562 for (tpst = pst; tpst != NULL; tpst = tpst->next)
564 if (pc >= tpst->textlow && pc < tpst->texthigh)
566 struct partial_symbol *p;
568 p = find_pc_sect_psymbol (tpst, pc, section);
570 && SYMBOL_VALUE_ADDRESS (p)
571 == SYMBOL_VALUE_ADDRESS (msymbol))
581 /* Find which partial symtab contains PC. Return 0 if none.
582 Backward compatibility, no section */
584 struct partial_symtab *
585 find_pc_psymtab (CORE_ADDR pc)
587 return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
590 /* Find which partial symbol within a psymtab matches PC and SECTION.
591 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
593 struct partial_symbol *
594 find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
597 struct partial_symbol *best = NULL, *p, **pp;
601 psymtab = find_pc_sect_psymtab (pc, section);
605 /* Cope with programs that start at address 0 */
606 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
608 /* Search the global symbols as well as the static symbols, so that
609 find_pc_partial_function doesn't use a minimal symbol and thus
610 cache a bad endaddr. */
611 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
612 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
613 < psymtab->n_global_syms);
617 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
618 && SYMBOL_CLASS (p) == LOC_BLOCK
619 && pc >= SYMBOL_VALUE_ADDRESS (p)
620 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
621 || (psymtab->textlow == 0
622 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
624 if (section) /* match on a specific section */
626 fixup_psymbol_section (p, psymtab->objfile);
627 if (SYMBOL_BFD_SECTION (p) != section)
630 best_pc = SYMBOL_VALUE_ADDRESS (p);
635 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
636 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
637 < psymtab->n_static_syms);
641 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
642 && SYMBOL_CLASS (p) == LOC_BLOCK
643 && pc >= SYMBOL_VALUE_ADDRESS (p)
644 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
645 || (psymtab->textlow == 0
646 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
648 if (section) /* match on a specific section */
650 fixup_psymbol_section (p, psymtab->objfile);
651 if (SYMBOL_BFD_SECTION (p) != section)
654 best_pc = SYMBOL_VALUE_ADDRESS (p);
662 /* Find which partial symbol within a psymtab matches PC. Return 0 if none.
663 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
665 struct partial_symbol *
666 find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
668 return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
671 /* Debug symbols usually don't have section information. We need to dig that
672 out of the minimal symbols and stash that in the debug symbol. */
675 fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
677 struct minimal_symbol *msym;
678 msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
682 ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
683 ginfo->section = SYMBOL_SECTION (msym);
688 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
693 if (SYMBOL_BFD_SECTION (sym))
696 fixup_section (&sym->ginfo, objfile);
701 struct partial_symbol *
702 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
707 if (SYMBOL_BFD_SECTION (psym))
710 fixup_section (&psym->ginfo, objfile);
715 /* Find the definition for a specified symbol name NAME
716 in namespace NAMESPACE, visible from lexical block BLOCK.
717 Returns the struct symbol pointer, or zero if no symbol is found.
718 If SYMTAB is non-NULL, store the symbol table in which the
719 symbol was found there, or NULL if not found.
720 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
721 NAME is a field of the current implied argument `this'. If so set
722 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
723 BLOCK_FOUND is set to the block in which NAME is found (in the case of
724 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
726 /* This function has a bunch of loops in it and it would seem to be
727 attractive to put in some QUIT's (though I'm not really sure
728 whether it can run long enough to be really important). But there
729 are a few calls for which it would appear to be bad news to quit
730 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
731 nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++
732 code below which can error(), but that probably doesn't affect
733 these calls since they are looking for a known variable and thus
734 can probably assume it will never hit the C++ code). */
737 lookup_symbol (const char *name, const struct block *block,
738 const namespace_enum namespace, int *is_a_field_of_this,
739 struct symtab **symtab)
741 char *demangled_name = NULL;
742 const char *modified_name = NULL;
743 const char *mangled_name = NULL;
744 int needtofreename = 0;
745 struct symbol *returnval;
747 modified_name = name;
749 /* If we are using C++ language, demangle the name before doing a lookup, so
750 we can always binary search. */
751 if (current_language->la_language == language_cplus)
753 demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
757 modified_name = demangled_name;
762 if (case_sensitivity == case_sensitive_off)
768 copy = (char *) alloca (len + 1);
769 for (i= 0; i < len; i++)
770 copy[i] = tolower (name[i]);
772 modified_name = copy;
775 returnval = lookup_symbol_aux (modified_name, mangled_name, block,
776 namespace, is_a_field_of_this, symtab);
778 xfree (demangled_name);
783 static struct symbol *
784 lookup_symbol_aux (const char *name, const char *mangled_name,
785 const struct block *block, const namespace_enum namespace,
786 int *is_a_field_of_this, struct symtab **symtab)
789 struct symtab *s = NULL;
790 struct blockvector *bv;
791 struct minimal_symbol *msymbol;
793 /* Search specified block and its superiors. */
795 sym = lookup_symbol_aux_local (name, mangled_name, block, namespace,
801 /* NOTE: carlton/2002-11-05: At the time that this code was
802 #ifdeffed out, the value of 'block' was always NULL at this
803 point, hence the bemused comments below. */
805 /* FIXME: this code is never executed--block is always NULL at this
806 point. What is it trying to do, anyway? We already should have
807 checked the STATIC_BLOCK above (it is the superblock of top-level
808 blocks). Why is VAR_NAMESPACE special-cased? */
809 /* Don't need to mess with the psymtabs; if we have a block,
810 that file is read in. If we don't, then we deal later with
811 all the psymtab stuff that needs checking. */
812 /* Note (RT): The following never-executed code looks unnecessary to me also.
813 * If we change the code to use the original (passed-in)
814 * value of 'block', we could cause it to execute, but then what
815 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
816 * 'block' was already searched by the above code. And the STATIC_BLOCK's
817 * of *other* symtabs (those files not containing 'block' lexically)
818 * should not contain 'block' address-wise. So we wouldn't expect this
819 * code to find any 'sym''s that were not found above. I vote for
820 * deleting the following paragraph of code.
822 if (namespace == VAR_NAMESPACE && block != NULL)
825 /* Find the right symtab. */
826 ALL_SYMTABS (objfile, s)
828 bv = BLOCKVECTOR (s);
829 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
830 if (BLOCK_START (b) <= BLOCK_START (block)
831 && BLOCK_END (b) > BLOCK_START (block))
833 sym = lookup_block_symbol (b, name, mangled_name, VAR_NAMESPACE);
839 return fixup_symbol_section (sym, objfile);
846 /* C++: If requested to do so by the caller,
847 check to see if NAME is a field of `this'. */
848 if (is_a_field_of_this)
850 struct value *v = value_of_this (0);
852 *is_a_field_of_this = 0;
853 if (v && check_field (v, name))
855 *is_a_field_of_this = 1;
862 /* Now search all global blocks. Do the symtab's first, then
863 check the psymtab's. If a psymtab indicates the existence
864 of the desired name as a global, then do psymtab-to-symtab
865 conversion on the fly and return the found symbol. */
867 sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, mangled_name,
874 /* Check for the possibility of the symbol being a function or
875 a mangled variable that is stored in one of the minimal symbol tables.
876 Eventually, all global symbols might be resolved in this way. */
878 if (namespace == VAR_NAMESPACE)
880 msymbol = lookup_minimal_symbol (name, NULL, NULL);
883 s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
884 SYMBOL_BFD_SECTION (msymbol));
887 /* This is a function which has a symtab for its address. */
888 bv = BLOCKVECTOR (s);
889 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
891 /* This call used to pass `SYMBOL_NAME (msymbol)' as the
892 `name' argument to lookup_block_symbol. But the name
893 of a minimal symbol is always mangled, so that seems
894 to be clearly the wrong thing to pass as the
896 sym = lookup_block_symbol (block, name, mangled_name, namespace);
897 /* We kept static functions in minimal symbol table as well as
898 in static scope. We want to find them in the symbol table. */
901 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
902 sym = lookup_block_symbol (block, name,
903 mangled_name, namespace);
906 /* sym == 0 if symbol was found in the minimal symbol table
907 but not in the symtab.
908 Return 0 to use the msymbol definition of "foo_".
910 This happens for Fortran "foo_" symbols,
911 which are "foo" in the symtab.
913 This can also happen if "asm" is used to make a
914 regular symbol but not a debugging symbol, e.g.
921 return fixup_symbol_section (sym, s->objfile);
923 else if (MSYMBOL_TYPE (msymbol) != mst_text
924 && MSYMBOL_TYPE (msymbol) != mst_file_text
925 && !STREQ (name, SYMBOL_NAME (msymbol)))
927 /* This is a mangled variable, look it up by its
929 return lookup_symbol_aux (SYMBOL_NAME (msymbol), mangled_name, NULL,
930 namespace, is_a_field_of_this, symtab);
932 /* There are no debug symbols for this file, or we are looking
933 for an unmangled variable.
934 Try to find a matching static symbol below. */
940 sym = lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, mangled_name,
945 /* Now search all static file-level symbols. Not strictly correct,
946 but more useful than an error. Do the symtabs first, then check
947 the psymtabs. If a psymtab indicates the existence of the
948 desired name as a file-level static, then do psymtab-to-symtab
949 conversion on the fly and return the found symbol. */
951 sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, mangled_name,
956 sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, mangled_name,
963 /* Check for the possibility of the symbol being a function or
964 a global variable that is stored in one of the minimal symbol tables.
965 The "minimal symbol table" is built from linker-supplied info.
967 RT: I moved this check to last, after the complete search of
968 the global (p)symtab's and static (p)symtab's. For HP-generated
969 symbol tables, this check was causing a premature exit from
970 lookup_symbol with NULL return, and thus messing up symbol lookups
971 of things like "c::f". It seems to me a check of the minimal
972 symbol table ought to be a last resort in any case. I'm vaguely
973 worried about the comment below which talks about FORTRAN routines "foo_"
974 though... is it saying we need to do the "minsym" check before
975 the static check in this case?
978 if (namespace == VAR_NAMESPACE)
980 msymbol = lookup_minimal_symbol (name, NULL, NULL);
983 /* OK, we found a minimal symbol in spite of not
984 * finding any symbol. There are various possible
985 * explanations for this. One possibility is the symbol
986 * exists in code not compiled -g. Another possibility
987 * is that the 'psymtab' isn't doing its job.
988 * A third possibility, related to #2, is that we were confused
989 * by name-mangling. For instance, maybe the psymtab isn't
990 * doing its job because it only know about demangled
991 * names, but we were given a mangled name...
994 /* We first use the address in the msymbol to try to
995 * locate the appropriate symtab. Note that find_pc_symtab()
996 * has a side-effect of doing psymtab-to-symtab expansion,
997 * for the found symtab.
999 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
1002 bv = BLOCKVECTOR (s);
1003 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1004 /* This call used to pass `SYMBOL_NAME (msymbol)' as the
1005 `name' argument to lookup_block_symbol. But the name
1006 of a minimal symbol is always mangled, so that seems
1007 to be clearly the wrong thing to pass as the
1009 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1010 /* We kept static functions in minimal symbol table as well as
1011 in static scope. We want to find them in the symbol table. */
1014 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1015 sym = lookup_block_symbol (block, name,
1016 mangled_name, namespace);
1018 /* If we found one, return it */
1026 /* If we get here with sym == 0, the symbol was
1027 found in the minimal symbol table
1028 but not in the symtab.
1029 Fall through and return 0 to use the msymbol
1030 definition of "foo_".
1031 (Note that outer code generally follows up a call
1032 to this routine with a call to lookup_minimal_symbol(),
1033 so a 0 return means we'll just flow into that other routine).
1035 This happens for Fortran "foo_" symbols,
1036 which are "foo" in the symtab.
1038 This can also happen if "asm" is used to make a
1039 regular symbol but not a debugging symbol, e.g.
1040 asm(".globl _main");
1045 /* If the lookup-by-address fails, try repeating the
1046 * entire lookup process with the symbol name from
1047 * the msymbol (if different from the original symbol name).
1049 else if (MSYMBOL_TYPE (msymbol) != mst_text
1050 && MSYMBOL_TYPE (msymbol) != mst_file_text
1051 && !STREQ (name, SYMBOL_NAME (msymbol)))
1053 return lookup_symbol_aux (SYMBOL_NAME (msymbol), mangled_name,
1054 NULL, namespace, is_a_field_of_this,
1067 /* Check to see if the symbol is defined in BLOCK or its
1070 static struct symbol *
1071 lookup_symbol_aux_local (const char *name, const char *mangled_name,
1072 const struct block *block,
1073 const namespace_enum namespace,
1074 struct symtab **symtab)
1077 struct objfile *objfile = NULL;
1078 struct blockvector *bv;
1080 struct symtab *s = NULL;
1084 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1087 block_found = block;
1090 /* Search the list of symtabs for one which contains the
1091 address of the start of this block. */
1092 ALL_SYMTABS (objfile, s)
1094 bv = BLOCKVECTOR (s);
1095 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1096 if (BLOCK_START (b) <= BLOCK_START (block)
1097 && BLOCK_END (b) > BLOCK_START (block))
1104 return fixup_symbol_section (sym, objfile);
1106 block = BLOCK_SUPERBLOCK (block);
1112 /* Check to see if the symbol is defined in one of the symtabs.
1113 BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1114 depending on whether or not we want to search global symbols or
1117 static struct symbol *
1118 lookup_symbol_aux_symtabs (int block_index,
1119 const char *name, const char *mangled_name,
1120 const namespace_enum namespace,
1121 struct symtab **symtab)
1124 struct objfile *objfile;
1125 struct blockvector *bv;
1126 const struct block *block;
1129 ALL_SYMTABS (objfile, s)
1131 bv = BLOCKVECTOR (s);
1132 block = BLOCKVECTOR_BLOCK (bv, block_index);
1133 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1136 block_found = block;
1139 return fixup_symbol_section (sym, objfile);
1146 /* Check to see if the symbol is defined in one of the partial
1147 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or
1148 STATIC_BLOCK, depending on whether or not we want to search global
1149 symbols or static symbols. */
1151 static struct symbol *
1152 lookup_symbol_aux_psymtabs (int block_index, const char *name,
1153 const char *mangled_name,
1154 const namespace_enum namespace,
1155 struct symtab **symtab)
1158 struct objfile *objfile;
1159 struct blockvector *bv;
1160 const struct block *block;
1161 struct partial_symtab *ps;
1163 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
1165 ALL_PSYMTABS (objfile, ps)
1168 && lookup_partial_symbol (ps, name, psymtab_index, namespace))
1170 s = PSYMTAB_TO_SYMTAB (ps);
1171 bv = BLOCKVECTOR (s);
1172 block = BLOCKVECTOR_BLOCK (bv, block_index);
1173 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1176 /* This shouldn't be necessary, but as a last resort try
1177 looking in the statics even though the psymtab claimed
1178 the symbol was global, or vice-versa. It's possible
1179 that the psymtab gets it wrong in some cases. */
1181 /* FIXME: carlton/2002-09-30: Should we really do that?
1182 If that happens, isn't it likely to be a GDB error, in
1183 which case we should fix the GDB error rather than
1184 silently dealing with it here? So I'd vote for
1185 removing the check for the symbol in the other
1187 block = BLOCKVECTOR_BLOCK (bv,
1188 block_index == GLOBAL_BLOCK ?
1189 STATIC_BLOCK : GLOBAL_BLOCK);
1190 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1192 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>).",
1193 block_index == GLOBAL_BLOCK ? "global" : "static",
1194 name, ps->filename, name, name);
1198 return fixup_symbol_section (sym, objfile);
1205 /* Look, in partial_symtab PST, for symbol NAME. Check the global
1206 symbols if GLOBAL, the static symbols if not */
1208 static struct partial_symbol *
1209 lookup_partial_symbol (struct partial_symtab *pst, const char *name, int global,
1210 namespace_enum namespace)
1212 struct partial_symbol *temp;
1213 struct partial_symbol **start, **psym;
1214 struct partial_symbol **top, **bottom, **center;
1215 int length = (global ? pst->n_global_syms : pst->n_static_syms);
1216 int do_linear_search = 1;
1223 pst->objfile->global_psymbols.list + pst->globals_offset :
1224 pst->objfile->static_psymbols.list + pst->statics_offset);
1226 if (global) /* This means we can use a binary search. */
1228 do_linear_search = 0;
1230 /* Binary search. This search is guaranteed to end with center
1231 pointing at the earliest partial symbol with the correct
1232 name. At that point *all* partial symbols with that name
1233 will be checked against the correct namespace. */
1236 top = start + length - 1;
1237 while (top > bottom)
1239 center = bottom + (top - bottom) / 2;
1240 if (!(center < top))
1241 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1242 if (!do_linear_search
1243 && (SYMBOL_LANGUAGE (*center) == language_java))
1245 do_linear_search = 1;
1247 if (strcmp (SYMBOL_SOURCE_NAME (*center), name) >= 0)
1253 bottom = center + 1;
1256 if (!(top == bottom))
1257 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1259 /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so
1260 we don't have to force a linear search on C++. Probably holds true
1261 for JAVA as well, no way to check.*/
1262 while (SYMBOL_MATCHES_NAME (*top,name))
1264 if (SYMBOL_NAMESPACE (*top) == namespace)
1272 /* Can't use a binary search or else we found during the binary search that
1273 we should also do a linear search. */
1275 if (do_linear_search)
1277 for (psym = start; psym < start + length; psym++)
1279 if (namespace == SYMBOL_NAMESPACE (*psym))
1281 if (SYMBOL_MATCHES_NAME (*psym, name))
1292 /* Look up a type named NAME in the struct_namespace. The type returned
1293 must not be opaque -- i.e., must have at least one field defined
1295 This code was modelled on lookup_symbol -- the parts not relevant to looking
1296 up types were just left out. In particular it's assumed here that types
1297 are available in struct_namespace and only at file-static or global blocks. */
1301 lookup_transparent_type (const char *name)
1303 register struct symbol *sym;
1304 register struct symtab *s = NULL;
1305 register struct partial_symtab *ps;
1306 struct blockvector *bv;
1307 register struct objfile *objfile;
1308 register struct block *block;
1310 /* Now search all the global symbols. Do the symtab's first, then
1311 check the psymtab's. If a psymtab indicates the existence
1312 of the desired name as a global, then do psymtab-to-symtab
1313 conversion on the fly and return the found symbol. */
1315 ALL_SYMTABS (objfile, s)
1317 bv = BLOCKVECTOR (s);
1318 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1319 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1320 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1322 return SYMBOL_TYPE (sym);
1326 ALL_PSYMTABS (objfile, ps)
1328 if (!ps->readin && lookup_partial_symbol (ps, name, 1, STRUCT_NAMESPACE))
1330 s = PSYMTAB_TO_SYMTAB (ps);
1331 bv = BLOCKVECTOR (s);
1332 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1333 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1336 /* This shouldn't be necessary, but as a last resort
1337 * try looking in the statics even though the psymtab
1338 * claimed the symbol was global. It's possible that
1339 * the psymtab gets it wrong in some cases.
1341 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1342 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1344 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1345 %s may be an inlined function, or may be a template function\n\
1346 (if a template, try specifying an instantiation: %s<type>).",
1347 name, ps->filename, name, name);
1349 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1350 return SYMBOL_TYPE (sym);
1354 /* Now search the static file-level symbols.
1355 Not strictly correct, but more useful than an error.
1356 Do the symtab's first, then
1357 check the psymtab's. If a psymtab indicates the existence
1358 of the desired name as a file-level static, then do psymtab-to-symtab
1359 conversion on the fly and return the found symbol.
1362 ALL_SYMTABS (objfile, s)
1364 bv = BLOCKVECTOR (s);
1365 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1366 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1367 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1369 return SYMBOL_TYPE (sym);
1373 ALL_PSYMTABS (objfile, ps)
1375 if (!ps->readin && lookup_partial_symbol (ps, name, 0, STRUCT_NAMESPACE))
1377 s = PSYMTAB_TO_SYMTAB (ps);
1378 bv = BLOCKVECTOR (s);
1379 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1380 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1383 /* This shouldn't be necessary, but as a last resort
1384 * try looking in the globals even though the psymtab
1385 * claimed the symbol was static. It's possible that
1386 * the psymtab gets it wrong in some cases.
1388 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1389 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1391 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1392 %s may be an inlined function, or may be a template function\n\
1393 (if a template, try specifying an instantiation: %s<type>).",
1394 name, ps->filename, name, name);
1396 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1397 return SYMBOL_TYPE (sym);
1400 return (struct type *) 0;
1404 /* Find the psymtab containing main(). */
1405 /* FIXME: What about languages without main() or specially linked
1406 executables that have no main() ? */
1408 struct partial_symtab *
1409 find_main_psymtab (void)
1411 register struct partial_symtab *pst;
1412 register struct objfile *objfile;
1414 ALL_PSYMTABS (objfile, pst)
1416 if (lookup_partial_symbol (pst, main_name (), 1, VAR_NAMESPACE))
1424 /* Search BLOCK for symbol NAME in NAMESPACE.
1426 Note that if NAME is the demangled form of a C++ symbol, we will fail
1427 to find a match during the binary search of the non-encoded names, but
1428 for now we don't worry about the slight inefficiency of looking for
1429 a match we'll never find, since it will go pretty quick. Once the
1430 binary search terminates, we drop through and do a straight linear
1431 search on the symbols. Each symbol which is marked as being a C++
1432 symbol (language_cplus set) has both the encoded and non-encoded names
1435 If MANGLED_NAME is non-NULL, verify that any symbol we find has this
1436 particular mangled name.
1440 lookup_block_symbol (register const struct block *block, const char *name,
1441 const char *mangled_name,
1442 const namespace_enum namespace)
1444 register int bot, top, inc;
1445 register struct symbol *sym;
1446 register struct symbol *sym_found = NULL;
1447 register int do_linear_search = 1;
1449 if (BLOCK_HASHTABLE (block))
1451 unsigned int hash_index;
1452 hash_index = msymbol_hash_iw (name);
1453 hash_index = hash_index % BLOCK_BUCKETS (block);
1454 for (sym = BLOCK_BUCKET (block, hash_index); sym; sym = sym->hash_next)
1456 if (SYMBOL_NAMESPACE (sym) == namespace
1458 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1459 : SYMBOL_MATCHES_NAME (sym, name)))
1465 /* If the blocks's symbols were sorted, start with a binary search. */
1467 if (BLOCK_SHOULD_SORT (block))
1469 /* Reset the linear search flag so if the binary search fails, we
1470 won't do the linear search once unless we find some reason to
1473 do_linear_search = 0;
1474 top = BLOCK_NSYMS (block);
1477 /* Advance BOT to not far before the first symbol whose name is NAME. */
1481 inc = (top - bot + 1);
1482 /* No need to keep binary searching for the last few bits worth. */
1487 inc = (inc >> 1) + bot;
1488 sym = BLOCK_SYM (block, inc);
1489 if (!do_linear_search && (SYMBOL_LANGUAGE (sym) == language_java))
1491 do_linear_search = 1;
1493 if (SYMBOL_SOURCE_NAME (sym)[0] < name[0])
1497 else if (SYMBOL_SOURCE_NAME (sym)[0] > name[0])
1501 else if (strcmp (SYMBOL_SOURCE_NAME (sym), name) < 0)
1511 /* Now scan forward until we run out of symbols, find one whose
1512 name is greater than NAME, or find one we want. If there is
1513 more than one symbol with the right name and namespace, we
1514 return the first one; I believe it is now impossible for us
1515 to encounter two symbols with the same name and namespace
1516 here, because blocks containing argument symbols are no
1517 longer sorted. The exception is for C++, where multiple functions
1518 (cloned constructors / destructors, in particular) can have
1519 the same demangled name. So if we have a particular
1520 mangled name to match, try to do so. */
1522 top = BLOCK_NSYMS (block);
1525 sym = BLOCK_SYM (block, bot);
1526 if (SYMBOL_NAMESPACE (sym) == namespace
1528 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1529 : SYMBOL_MATCHES_NAME (sym, name)))
1533 if (SYMBOL_SOURCE_NAME (sym)[0] > name[0])
1541 /* Here if block isn't sorted, or we fail to find a match during the
1542 binary search above. If during the binary search above, we find a
1543 symbol which is a Java symbol, then we have re-enabled the linear
1544 search flag which was reset when starting the binary search.
1546 This loop is equivalent to the loop above, but hacked greatly for speed.
1548 Note that parameter symbols do not always show up last in the
1549 list; this loop makes sure to take anything else other than
1550 parameter symbols first; it only uses parameter symbols as a
1551 last resort. Note that this only takes up extra computation
1554 if (do_linear_search)
1556 top = BLOCK_NSYMS (block);
1560 sym = BLOCK_SYM (block, bot);
1561 if (SYMBOL_NAMESPACE (sym) == namespace
1563 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1564 : SYMBOL_MATCHES_NAME (sym, name)))
1566 /* If SYM has aliases, then use any alias that is active
1567 at the current PC. If no alias is active at the current
1568 PC, then use the main symbol.
1570 ?!? Is checking the current pc correct? Is this routine
1571 ever called to look up a symbol from another context?
1573 FIXME: No, it's not correct. If someone sets a
1574 conditional breakpoint at an address, then the
1575 breakpoint's `struct expression' should refer to the
1576 `struct symbol' appropriate for the breakpoint's
1577 address, which may not be the PC.
1579 Even if it were never called from another context,
1580 it's totally bizarre for lookup_symbol's behavior to
1581 depend on the value of the inferior's current PC. We
1582 should pass in the appropriate PC as well as the
1583 block. The interface to lookup_symbol should change
1584 to require the caller to provide a PC. */
1586 if (SYMBOL_ALIASES (sym))
1587 sym = find_active_alias (sym, read_pc ());
1590 if (SYMBOL_CLASS (sym) != LOC_ARG &&
1591 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1592 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1593 SYMBOL_CLASS (sym) != LOC_REGPARM &&
1594 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1595 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
1603 return (sym_found); /* Will be NULL if not found. */
1606 /* Given a main symbol SYM and ADDR, search through the alias
1607 list to determine if an alias is active at ADDR and return
1610 If no alias is active, then return SYM. */
1612 static struct symbol *
1613 find_active_alias (struct symbol *sym, CORE_ADDR addr)
1615 struct range_list *r;
1616 struct alias_list *aliases;
1618 /* If we have aliases, check them first. */
1619 aliases = SYMBOL_ALIASES (sym);
1623 if (!SYMBOL_RANGES (aliases->sym))
1624 return aliases->sym;
1625 for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next)
1627 if (r->start <= addr && r->end > addr)
1628 return aliases->sym;
1630 aliases = aliases->next;
1633 /* Nothing found, return the main symbol. */
1638 /* Return the symbol for the function which contains a specified
1639 lexical block, described by a struct block BL. */
1642 block_function (struct block *bl)
1644 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
1645 bl = BLOCK_SUPERBLOCK (bl);
1647 return BLOCK_FUNCTION (bl);
1650 /* Find the symtab associated with PC and SECTION. Look through the
1651 psymtabs and read in another symtab if necessary. */
1654 find_pc_sect_symtab (CORE_ADDR pc, asection *section)
1656 register struct block *b;
1657 struct blockvector *bv;
1658 register struct symtab *s = NULL;
1659 register struct symtab *best_s = NULL;
1660 register struct partial_symtab *ps;
1661 register struct objfile *objfile;
1662 CORE_ADDR distance = 0;
1663 struct minimal_symbol *msymbol;
1665 /* If we know that this is not a text address, return failure. This is
1666 necessary because we loop based on the block's high and low code
1667 addresses, which do not include the data ranges, and because
1668 we call find_pc_sect_psymtab which has a similar restriction based
1669 on the partial_symtab's texthigh and textlow. */
1670 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1672 && (msymbol->type == mst_data
1673 || msymbol->type == mst_bss
1674 || msymbol->type == mst_abs
1675 || msymbol->type == mst_file_data
1676 || msymbol->type == mst_file_bss))
1679 /* Search all symtabs for the one whose file contains our address, and which
1680 is the smallest of all the ones containing the address. This is designed
1681 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1682 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1683 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1685 This happens for native ecoff format, where code from included files
1686 gets its own symtab. The symtab for the included file should have
1687 been read in already via the dependency mechanism.
1688 It might be swifter to create several symtabs with the same name
1689 like xcoff does (I'm not sure).
1691 It also happens for objfiles that have their functions reordered.
1692 For these, the symtab we are looking for is not necessarily read in. */
1694 ALL_SYMTABS (objfile, s)
1696 bv = BLOCKVECTOR (s);
1697 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1699 if (BLOCK_START (b) <= pc
1700 && BLOCK_END (b) > pc
1702 || BLOCK_END (b) - BLOCK_START (b) < distance))
1704 /* For an objfile that has its functions reordered,
1705 find_pc_psymtab will find the proper partial symbol table
1706 and we simply return its corresponding symtab. */
1707 /* In order to better support objfiles that contain both
1708 stabs and coff debugging info, we continue on if a psymtab
1710 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1712 ps = find_pc_sect_psymtab (pc, section);
1714 return PSYMTAB_TO_SYMTAB (ps);
1719 struct symbol *sym = NULL;
1721 ALL_BLOCK_SYMBOLS (b, i, sym)
1723 fixup_symbol_section (sym, objfile);
1724 if (section == SYMBOL_BFD_SECTION (sym))
1727 if ((i >= BLOCK_BUCKETS (b)) && (sym == NULL))
1728 continue; /* no symbol in this symtab matches section */
1730 distance = BLOCK_END (b) - BLOCK_START (b);
1739 ps = find_pc_sect_psymtab (pc, section);
1743 /* Might want to error() here (in case symtab is corrupt and
1744 will cause a core dump), but maybe we can successfully
1745 continue, so let's not. */
1747 (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1749 s = PSYMTAB_TO_SYMTAB (ps);
1754 /* Find the symtab associated with PC. Look through the psymtabs and
1755 read in another symtab if necessary. Backward compatibility, no section */
1758 find_pc_symtab (CORE_ADDR pc)
1760 return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1764 /* Find the source file and line number for a given PC value and SECTION.
1765 Return a structure containing a symtab pointer, a line number,
1766 and a pc range for the entire source line.
1767 The value's .pc field is NOT the specified pc.
1768 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1769 use the line that ends there. Otherwise, in that case, the line
1770 that begins there is used. */
1772 /* The big complication here is that a line may start in one file, and end just
1773 before the start of another file. This usually occurs when you #include
1774 code in the middle of a subroutine. To properly find the end of a line's PC
1775 range, we must search all symtabs associated with this compilation unit, and
1776 find the one whose first PC is closer than that of the next line in this
1779 /* If it's worth the effort, we could be using a binary search. */
1781 struct symtab_and_line
1782 find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent)
1785 register struct linetable *l;
1788 register struct linetable_entry *item;
1789 struct symtab_and_line val;
1790 struct blockvector *bv;
1791 struct minimal_symbol *msymbol;
1792 struct minimal_symbol *mfunsym;
1794 /* Info on best line seen so far, and where it starts, and its file. */
1796 struct linetable_entry *best = NULL;
1797 CORE_ADDR best_end = 0;
1798 struct symtab *best_symtab = 0;
1800 /* Store here the first line number
1801 of a file which contains the line at the smallest pc after PC.
1802 If we don't find a line whose range contains PC,
1803 we will use a line one less than this,
1804 with a range from the start of that file to the first line's pc. */
1805 struct linetable_entry *alt = NULL;
1806 struct symtab *alt_symtab = 0;
1808 /* Info on best line seen in this file. */
1810 struct linetable_entry *prev;
1812 /* If this pc is not from the current frame,
1813 it is the address of the end of a call instruction.
1814 Quite likely that is the start of the following statement.
1815 But what we want is the statement containing the instruction.
1816 Fudge the pc to make sure we get that. */
1818 init_sal (&val); /* initialize to zeroes */
1820 /* It's tempting to assume that, if we can't find debugging info for
1821 any function enclosing PC, that we shouldn't search for line
1822 number info, either. However, GAS can emit line number info for
1823 assembly files --- very helpful when debugging hand-written
1824 assembly code. In such a case, we'd have no debug info for the
1825 function, but we would have line info. */
1830 /* elz: added this because this function returned the wrong
1831 information if the pc belongs to a stub (import/export)
1832 to call a shlib function. This stub would be anywhere between
1833 two functions in the target, and the line info was erroneously
1834 taken to be the one of the line before the pc.
1836 /* RT: Further explanation:
1838 * We have stubs (trampolines) inserted between procedures.
1840 * Example: "shr1" exists in a shared library, and a "shr1" stub also
1841 * exists in the main image.
1843 * In the minimal symbol table, we have a bunch of symbols
1844 * sorted by start address. The stubs are marked as "trampoline",
1845 * the others appear as text. E.g.:
1847 * Minimal symbol table for main image
1848 * main: code for main (text symbol)
1849 * shr1: stub (trampoline symbol)
1850 * foo: code for foo (text symbol)
1852 * Minimal symbol table for "shr1" image:
1854 * shr1: code for shr1 (text symbol)
1857 * So the code below is trying to detect if we are in the stub
1858 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
1859 * and if found, do the symbolization from the real-code address
1860 * rather than the stub address.
1862 * Assumptions being made about the minimal symbol table:
1863 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
1864 * if we're really in the trampoline. If we're beyond it (say
1865 * we're in "foo" in the above example), it'll have a closer
1866 * symbol (the "foo" text symbol for example) and will not
1867 * return the trampoline.
1868 * 2. lookup_minimal_symbol_text() will find a real text symbol
1869 * corresponding to the trampoline, and whose address will
1870 * be different than the trampoline address. I put in a sanity
1871 * check for the address being the same, to avoid an
1872 * infinite recursion.
1874 msymbol = lookup_minimal_symbol_by_pc (pc);
1875 if (msymbol != NULL)
1876 if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
1878 mfunsym = lookup_minimal_symbol_text (SYMBOL_NAME (msymbol), NULL, NULL);
1879 if (mfunsym == NULL)
1880 /* I eliminated this warning since it is coming out
1881 * in the following situation:
1882 * gdb shmain // test program with shared libraries
1883 * (gdb) break shr1 // function in shared lib
1884 * Warning: In stub for ...
1885 * In the above situation, the shared lib is not loaded yet,
1886 * so of course we can't find the real func/line info,
1887 * but the "break" still works, and the warning is annoying.
1888 * So I commented out the warning. RT */
1889 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1891 else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
1892 /* Avoid infinite recursion */
1893 /* See above comment about why warning is commented out */
1894 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1897 return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
1901 s = find_pc_sect_symtab (pc, section);
1904 /* if no symbol information, return previous pc */
1911 bv = BLOCKVECTOR (s);
1913 /* Look at all the symtabs that share this blockvector.
1914 They all have the same apriori range, that we found was right;
1915 but they have different line tables. */
1917 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1919 /* Find the best line in this symtab. */
1926 /* I think len can be zero if the symtab lacks line numbers
1927 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
1928 I'm not sure which, and maybe it depends on the symbol
1934 item = l->item; /* Get first line info */
1936 /* Is this file's first line closer than the first lines of other files?
1937 If so, record this file, and its first line, as best alternate. */
1938 if (item->pc > pc && (!alt || item->pc < alt->pc))
1944 for (i = 0; i < len; i++, item++)
1946 /* Leave prev pointing to the linetable entry for the last line
1947 that started at or before PC. */
1954 /* At this point, prev points at the line whose start addr is <= pc, and
1955 item points at the next line. If we ran off the end of the linetable
1956 (pc >= start of the last line), then prev == item. If pc < start of
1957 the first line, prev will not be set. */
1959 /* Is this file's best line closer than the best in the other files?
1960 If so, record this file, and its best line, as best so far. */
1962 if (prev && (!best || prev->pc > best->pc))
1967 /* Discard BEST_END if it's before the PC of the current BEST. */
1968 if (best_end <= best->pc)
1972 /* If another line (denoted by ITEM) is in the linetable and its
1973 PC is after BEST's PC, but before the current BEST_END, then
1974 use ITEM's PC as the new best_end. */
1975 if (best && i < len && item->pc > best->pc
1976 && (best_end == 0 || best_end > item->pc))
1977 best_end = item->pc;
1983 { /* If we didn't find any line # info, just
1989 val.symtab = alt_symtab;
1990 val.line = alt->line - 1;
1992 /* Don't return line 0, that means that we didn't find the line. */
1996 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2000 else if (best->line == 0)
2002 /* If our best fit is in a range of PC's for which no line
2003 number info is available (line number is zero) then we didn't
2004 find any valid line information. */
2009 val.symtab = best_symtab;
2010 val.line = best->line;
2012 if (best_end && (!alt || best_end < alt->pc))
2017 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2019 val.section = section;
2023 /* Backward compatibility (no section) */
2025 struct symtab_and_line
2026 find_pc_line (CORE_ADDR pc, int notcurrent)
2030 section = find_pc_overlay (pc);
2031 if (pc_in_unmapped_range (pc, section))
2032 pc = overlay_mapped_address (pc, section);
2033 return find_pc_sect_line (pc, section, notcurrent);
2036 /* Find line number LINE in any symtab whose name is the same as
2039 If found, return the symtab that contains the linetable in which it was
2040 found, set *INDEX to the index in the linetable of the best entry
2041 found, and set *EXACT_MATCH nonzero if the value returned is an
2044 If not found, return NULL. */
2047 find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
2051 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2055 struct linetable *best_linetable;
2056 struct symtab *best_symtab;
2058 /* First try looking it up in the given symtab. */
2059 best_linetable = LINETABLE (symtab);
2060 best_symtab = symtab;
2061 best_index = find_line_common (best_linetable, line, &exact);
2062 if (best_index < 0 || !exact)
2064 /* Didn't find an exact match. So we better keep looking for
2065 another symtab with the same name. In the case of xcoff,
2066 multiple csects for one source file (produced by IBM's FORTRAN
2067 compiler) produce multiple symtabs (this is unavoidable
2068 assuming csects can be at arbitrary places in memory and that
2069 the GLOBAL_BLOCK of a symtab has a begin and end address). */
2071 /* BEST is the smallest linenumber > LINE so far seen,
2072 or 0 if none has been seen so far.
2073 BEST_INDEX and BEST_LINETABLE identify the item for it. */
2076 struct objfile *objfile;
2079 if (best_index >= 0)
2080 best = best_linetable->item[best_index].line;
2084 ALL_SYMTABS (objfile, s)
2086 struct linetable *l;
2089 if (!STREQ (symtab->filename, s->filename))
2092 ind = find_line_common (l, line, &exact);
2102 if (best == 0 || l->item[ind].line < best)
2104 best = l->item[ind].line;
2117 *index = best_index;
2119 *exact_match = exact;
2124 /* Set the PC value for a given source file and line number and return true.
2125 Returns zero for invalid line number (and sets the PC to 0).
2126 The source file is specified with a struct symtab. */
2129 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
2131 struct linetable *l;
2138 symtab = find_line_symtab (symtab, line, &ind, NULL);
2141 l = LINETABLE (symtab);
2142 *pc = l->item[ind].pc;
2149 /* Find the range of pc values in a line.
2150 Store the starting pc of the line into *STARTPTR
2151 and the ending pc (start of next line) into *ENDPTR.
2152 Returns 1 to indicate success.
2153 Returns 0 if could not find the specified line. */
2156 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
2159 CORE_ADDR startaddr;
2160 struct symtab_and_line found_sal;
2163 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
2166 /* This whole function is based on address. For example, if line 10 has
2167 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2168 "info line *0x123" should say the line goes from 0x100 to 0x200
2169 and "info line *0x355" should say the line goes from 0x300 to 0x400.
2170 This also insures that we never give a range like "starts at 0x134
2171 and ends at 0x12c". */
2173 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
2174 if (found_sal.line != sal.line)
2176 /* The specified line (sal) has zero bytes. */
2177 *startptr = found_sal.pc;
2178 *endptr = found_sal.pc;
2182 *startptr = found_sal.pc;
2183 *endptr = found_sal.end;
2188 /* Given a line table and a line number, return the index into the line
2189 table for the pc of the nearest line whose number is >= the specified one.
2190 Return -1 if none is found. The value is >= 0 if it is an index.
2192 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
2195 find_line_common (register struct linetable *l, register int lineno,
2201 /* BEST is the smallest linenumber > LINENO so far seen,
2202 or 0 if none has been seen so far.
2203 BEST_INDEX identifies the item for it. */
2205 int best_index = -1;
2214 for (i = 0; i < len; i++)
2216 register struct linetable_entry *item = &(l->item[i]);
2218 if (item->line == lineno)
2220 /* Return the first (lowest address) entry which matches. */
2225 if (item->line > lineno && (best == 0 || item->line < best))
2232 /* If we got here, we didn't get an exact match. */
2239 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2241 struct symtab_and_line sal;
2242 sal = find_pc_line (pc, 0);
2245 return sal.symtab != 0;
2248 /* Given a function symbol SYM, find the symtab and line for the start
2250 If the argument FUNFIRSTLINE is nonzero, we want the first line
2251 of real code inside the function. */
2253 struct symtab_and_line
2254 find_function_start_sal (struct symbol *sym, int funfirstline)
2257 struct symtab_and_line sal;
2259 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2260 fixup_symbol_section (sym, NULL);
2262 { /* skip "first line" of function (which is actually its prologue) */
2263 asection *section = SYMBOL_BFD_SECTION (sym);
2264 /* If function is in an unmapped overlay, use its unmapped LMA
2265 address, so that SKIP_PROLOGUE has something unique to work on */
2266 if (section_is_overlay (section) &&
2267 !section_is_mapped (section))
2268 pc = overlay_unmapped_address (pc, section);
2270 pc += FUNCTION_START_OFFSET;
2271 pc = SKIP_PROLOGUE (pc);
2273 /* For overlays, map pc back into its mapped VMA range */
2274 pc = overlay_mapped_address (pc, section);
2276 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2278 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2279 /* Convex: no need to suppress code on first line, if any */
2282 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2283 line is still part of the same function. */
2285 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2286 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2288 /* First pc of next line */
2290 /* Recalculate the line number (might not be N+1). */
2291 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2299 /* If P is of the form "operator[ \t]+..." where `...' is
2300 some legitimate operator text, return a pointer to the
2301 beginning of the substring of the operator text.
2302 Otherwise, return "". */
2304 operator_chars (char *p, char **end)
2307 if (strncmp (p, "operator", 8))
2311 /* Don't get faked out by `operator' being part of a longer
2313 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2316 /* Allow some whitespace between `operator' and the operator symbol. */
2317 while (*p == ' ' || *p == '\t')
2320 /* Recognize 'operator TYPENAME'. */
2322 if (isalpha (*p) || *p == '_' || *p == '$')
2324 register char *q = p + 1;
2325 while (isalnum (*q) || *q == '_' || *q == '$')
2334 case '\\': /* regexp quoting */
2337 if (p[2] == '=') /* 'operator\*=' */
2339 else /* 'operator\*' */
2343 else if (p[1] == '[')
2346 error ("mismatched quoting on brackets, try 'operator\\[\\]'");
2347 else if (p[2] == '\\' && p[3] == ']')
2349 *end = p + 4; /* 'operator\[\]' */
2353 error ("nothing is allowed between '[' and ']'");
2357 /* Gratuitous qoute: skip it and move on. */
2379 if (p[0] == '-' && p[1] == '>')
2381 /* Struct pointer member operator 'operator->'. */
2384 *end = p + 3; /* 'operator->*' */
2387 else if (p[2] == '\\')
2389 *end = p + 4; /* Hopefully 'operator->\*' */
2394 *end = p + 2; /* 'operator->' */
2398 if (p[1] == '=' || p[1] == p[0])
2409 error ("`operator ()' must be specified without whitespace in `()'");
2414 error ("`operator ?:' must be specified without whitespace in `?:'");
2419 error ("`operator []' must be specified without whitespace in `[]'");
2423 error ("`operator %s' not supported", p);
2432 /* If FILE is not already in the table of files, return zero;
2433 otherwise return non-zero. Optionally add FILE to the table if ADD
2434 is non-zero. If *FIRST is non-zero, forget the old table
2437 filename_seen (const char *file, int add, int *first)
2439 /* Table of files seen so far. */
2440 static const char **tab = NULL;
2441 /* Allocated size of tab in elements.
2442 Start with one 256-byte block (when using GNU malloc.c).
2443 24 is the malloc overhead when range checking is in effect. */
2444 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2445 /* Current size of tab in elements. */
2446 static int tab_cur_size;
2452 tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab));
2456 /* Is FILE in tab? */
2457 for (p = tab; p < tab + tab_cur_size; p++)
2458 if (strcmp (*p, file) == 0)
2461 /* No; maybe add it to tab. */
2464 if (tab_cur_size == tab_alloc_size)
2466 tab_alloc_size *= 2;
2467 tab = (const char **) xrealloc ((char *) tab,
2468 tab_alloc_size * sizeof (*tab));
2470 tab[tab_cur_size++] = file;
2476 /* Slave routine for sources_info. Force line breaks at ,'s.
2477 NAME is the name to print and *FIRST is nonzero if this is the first
2478 name printed. Set *FIRST to zero. */
2480 output_source_filename (char *name, int *first)
2482 /* Since a single source file can result in several partial symbol
2483 tables, we need to avoid printing it more than once. Note: if
2484 some of the psymtabs are read in and some are not, it gets
2485 printed both under "Source files for which symbols have been
2486 read" and "Source files for which symbols will be read in on
2487 demand". I consider this a reasonable way to deal with the
2488 situation. I'm not sure whether this can also happen for
2489 symtabs; it doesn't hurt to check. */
2491 /* Was NAME already seen? */
2492 if (filename_seen (name, 1, first))
2494 /* Yes; don't print it again. */
2497 /* No; print it and reset *FIRST. */
2504 printf_filtered (", ");
2508 fputs_filtered (name, gdb_stdout);
2512 sources_info (char *ignore, int from_tty)
2514 register struct symtab *s;
2515 register struct partial_symtab *ps;
2516 register struct objfile *objfile;
2519 if (!have_full_symbols () && !have_partial_symbols ())
2521 error ("No symbol table is loaded. Use the \"file\" command.");
2524 printf_filtered ("Source files for which symbols have been read in:\n\n");
2527 ALL_SYMTABS (objfile, s)
2529 output_source_filename (s->filename, &first);
2531 printf_filtered ("\n\n");
2533 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2536 ALL_PSYMTABS (objfile, ps)
2540 output_source_filename (ps->filename, &first);
2543 printf_filtered ("\n");
2547 file_matches (char *file, char *files[], int nfiles)
2551 if (file != NULL && nfiles != 0)
2553 for (i = 0; i < nfiles; i++)
2555 if (strcmp (files[i], lbasename (file)) == 0)
2559 else if (nfiles == 0)
2564 /* Free any memory associated with a search. */
2566 free_search_symbols (struct symbol_search *symbols)
2568 struct symbol_search *p;
2569 struct symbol_search *next;
2571 for (p = symbols; p != NULL; p = next)
2579 do_free_search_symbols_cleanup (void *symbols)
2581 free_search_symbols (symbols);
2585 make_cleanup_free_search_symbols (struct symbol_search *symbols)
2587 return make_cleanup (do_free_search_symbols_cleanup, symbols);
2590 /* Helper function for sort_search_symbols and qsort. Can only
2591 sort symbols, not minimal symbols. */
2593 compare_search_syms (const void *sa, const void *sb)
2595 struct symbol_search **sym_a = (struct symbol_search **) sa;
2596 struct symbol_search **sym_b = (struct symbol_search **) sb;
2598 return strcmp (SYMBOL_SOURCE_NAME ((*sym_a)->symbol),
2599 SYMBOL_SOURCE_NAME ((*sym_b)->symbol));
2602 /* Sort the ``nfound'' symbols in the list after prevtail. Leave
2603 prevtail where it is, but update its next pointer to point to
2604 the first of the sorted symbols. */
2605 static struct symbol_search *
2606 sort_search_symbols (struct symbol_search *prevtail, int nfound)
2608 struct symbol_search **symbols, *symp, *old_next;
2611 symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
2613 symp = prevtail->next;
2614 for (i = 0; i < nfound; i++)
2619 /* Generally NULL. */
2622 qsort (symbols, nfound, sizeof (struct symbol_search *),
2623 compare_search_syms);
2626 for (i = 0; i < nfound; i++)
2628 symp->next = symbols[i];
2631 symp->next = old_next;
2637 /* Search the symbol table for matches to the regular expression REGEXP,
2638 returning the results in *MATCHES.
2640 Only symbols of KIND are searched:
2641 FUNCTIONS_NAMESPACE - search all functions
2642 TYPES_NAMESPACE - search all type names
2643 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
2644 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
2645 and constants (enums)
2647 free_search_symbols should be called when *MATCHES is no longer needed.
2649 The results are sorted locally; each symtab's global and static blocks are
2650 separately alphabetized.
2653 search_symbols (char *regexp, namespace_enum kind, int nfiles, char *files[],
2654 struct symbol_search **matches)
2656 register struct symtab *s;
2657 register struct partial_symtab *ps;
2658 register struct blockvector *bv;
2659 struct blockvector *prev_bv = 0;
2660 register struct block *b;
2663 register struct symbol *sym;
2664 struct partial_symbol **psym;
2665 struct objfile *objfile;
2666 struct minimal_symbol *msymbol;
2669 static enum minimal_symbol_type types[]
2671 {mst_data, mst_text, mst_abs, mst_unknown};
2672 static enum minimal_symbol_type types2[]
2674 {mst_bss, mst_file_text, mst_abs, mst_unknown};
2675 static enum minimal_symbol_type types3[]
2677 {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
2678 static enum minimal_symbol_type types4[]
2680 {mst_file_bss, mst_text, mst_abs, mst_unknown};
2681 enum minimal_symbol_type ourtype;
2682 enum minimal_symbol_type ourtype2;
2683 enum minimal_symbol_type ourtype3;
2684 enum minimal_symbol_type ourtype4;
2685 struct symbol_search *sr;
2686 struct symbol_search *psr;
2687 struct symbol_search *tail;
2688 struct cleanup *old_chain = NULL;
2690 if (kind < VARIABLES_NAMESPACE)
2691 error ("must search on specific namespace");
2693 ourtype = types[(int) (kind - VARIABLES_NAMESPACE)];
2694 ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)];
2695 ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)];
2696 ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)];
2698 sr = *matches = NULL;
2703 /* Make sure spacing is right for C++ operators.
2704 This is just a courtesy to make the matching less sensitive
2705 to how many spaces the user leaves between 'operator'
2706 and <TYPENAME> or <OPERATOR>. */
2708 char *opname = operator_chars (regexp, &opend);
2711 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2712 if (isalpha (*opname) || *opname == '_' || *opname == '$')
2714 /* There should 1 space between 'operator' and 'TYPENAME'. */
2715 if (opname[-1] != ' ' || opname[-2] == ' ')
2720 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2721 if (opname[-1] == ' ')
2724 /* If wrong number of spaces, fix it. */
2727 char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
2728 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
2733 if (0 != (val = re_comp (regexp)))
2734 error ("Invalid regexp (%s): %s", val, regexp);
2737 /* Search through the partial symtabs *first* for all symbols
2738 matching the regexp. That way we don't have to reproduce all of
2739 the machinery below. */
2741 ALL_PSYMTABS (objfile, ps)
2743 struct partial_symbol **bound, **gbound, **sbound;
2749 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2750 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2753 /* Go through all of the symbols stored in a partial
2754 symtab in one loop. */
2755 psym = objfile->global_psymbols.list + ps->globals_offset;
2760 if (bound == gbound && ps->n_static_syms != 0)
2762 psym = objfile->static_psymbols.list + ps->statics_offset;
2773 /* If it would match (logic taken from loop below)
2774 load the file and go on to the next one */
2775 if (file_matches (ps->filename, files, nfiles)
2776 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym))
2777 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
2778 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2779 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2780 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2781 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
2783 PSYMTAB_TO_SYMTAB (ps);
2791 /* Here, we search through the minimal symbol tables for functions
2792 and variables that match, and force their symbols to be read.
2793 This is in particular necessary for demangled variable names,
2794 which are no longer put into the partial symbol tables.
2795 The symbol will then be found during the scan of symtabs below.
2797 For functions, find_pc_symtab should succeed if we have debug info
2798 for the function, for variables we have to call lookup_symbol
2799 to determine if the variable has debug info.
2800 If the lookup fails, set found_misc so that we will rescan to print
2801 any matching symbols without debug info.
2804 if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE))
2806 ALL_MSYMBOLS (objfile, msymbol)
2808 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2809 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2810 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2811 MSYMBOL_TYPE (msymbol) == ourtype4)
2813 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
2815 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
2817 if (kind == FUNCTIONS_NAMESPACE
2818 || lookup_symbol (SYMBOL_NAME (msymbol),
2819 (struct block *) NULL,
2821 0, (struct symtab **) NULL) == NULL)
2829 ALL_SYMTABS (objfile, s)
2831 bv = BLOCKVECTOR (s);
2832 /* Often many files share a blockvector.
2833 Scan each blockvector only once so that
2834 we don't get every symbol many times.
2835 It happens that the first symtab in the list
2836 for any given blockvector is the main file. */
2838 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2840 struct symbol_search *prevtail = tail;
2842 b = BLOCKVECTOR_BLOCK (bv, i);
2843 ALL_BLOCK_SYMBOLS (b, j, sym)
2846 if (file_matches (s->filename, files, nfiles)
2847 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
2848 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2849 && SYMBOL_CLASS (sym) != LOC_BLOCK
2850 && SYMBOL_CLASS (sym) != LOC_CONST)
2851 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)
2852 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2853 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK))))
2856 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
2860 psr->msymbol = NULL;
2872 if (prevtail == NULL)
2874 struct symbol_search dummy;
2877 tail = sort_search_symbols (&dummy, nfound);
2880 old_chain = make_cleanup_free_search_symbols (sr);
2883 tail = sort_search_symbols (prevtail, nfound);
2889 /* If there are no eyes, avoid all contact. I mean, if there are
2890 no debug symbols, then print directly from the msymbol_vector. */
2892 if (found_misc || kind != FUNCTIONS_NAMESPACE)
2894 ALL_MSYMBOLS (objfile, msymbol)
2896 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2897 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2898 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2899 MSYMBOL_TYPE (msymbol) == ourtype4)
2901 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
2903 /* Functions: Look up by address. */
2904 if (kind != FUNCTIONS_NAMESPACE ||
2905 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
2907 /* Variables/Absolutes: Look up by name */
2908 if (lookup_symbol (SYMBOL_NAME (msymbol),
2909 (struct block *) NULL, VAR_NAMESPACE,
2910 0, (struct symtab **) NULL) == NULL)
2913 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
2915 psr->msymbol = msymbol;
2922 old_chain = make_cleanup_free_search_symbols (sr);
2936 discard_cleanups (old_chain);
2939 /* Helper function for symtab_symbol_info, this function uses
2940 the data returned from search_symbols() to print information
2941 regarding the match to gdb_stdout.
2944 print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym,
2945 int block, char *last)
2947 if (last == NULL || strcmp (last, s->filename) != 0)
2949 fputs_filtered ("\nFile ", gdb_stdout);
2950 fputs_filtered (s->filename, gdb_stdout);
2951 fputs_filtered (":\n", gdb_stdout);
2954 if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK)
2955 printf_filtered ("static ");
2957 /* Typedef that is not a C++ class */
2958 if (kind == TYPES_NAMESPACE
2959 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2960 typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
2961 /* variable, func, or typedef-that-is-c++-class */
2962 else if (kind < TYPES_NAMESPACE ||
2963 (kind == TYPES_NAMESPACE &&
2964 SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE))
2966 type_print (SYMBOL_TYPE (sym),
2967 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2968 ? "" : SYMBOL_SOURCE_NAME (sym)),
2971 printf_filtered (";\n");
2976 /* Tiemann says: "info methods was never implemented." */
2977 char *demangled_name;
2978 c_type_print_base (TYPE_FN_FIELD_TYPE (t, block),
2980 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE (t, block),
2982 if (TYPE_FN_FIELD_STUB (t, block))
2983 check_stub_method (TYPE_DOMAIN_TYPE (type), j, block);
2985 cplus_demangle (TYPE_FN_FIELD_PHYSNAME (t, block),
2986 DMGL_ANSI | DMGL_PARAMS);
2987 if (demangled_name == NULL)
2988 fprintf_filtered (stream, "<badly mangled name %s>",
2989 TYPE_FN_FIELD_PHYSNAME (t, block));
2992 fputs_filtered (demangled_name, stream);
2993 xfree (demangled_name);
2999 /* This help function for symtab_symbol_info() prints information
3000 for non-debugging symbols to gdb_stdout.
3003 print_msymbol_info (struct minimal_symbol *msymbol)
3007 if (TARGET_ADDR_BIT <= 32)
3008 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3009 & (CORE_ADDR) 0xffffffff,
3012 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3014 printf_filtered ("%s %s\n",
3015 tmp, SYMBOL_SOURCE_NAME (msymbol));
3018 /* This is the guts of the commands "info functions", "info types", and
3019 "info variables". It calls search_symbols to find all matches and then
3020 print_[m]symbol_info to print out some useful information about the
3024 symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty)
3026 static char *classnames[]
3028 {"variable", "function", "type", "method"};
3029 struct symbol_search *symbols;
3030 struct symbol_search *p;
3031 struct cleanup *old_chain;
3032 char *last_filename = NULL;
3035 /* must make sure that if we're interrupted, symbols gets freed */
3036 search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3037 old_chain = make_cleanup_free_search_symbols (symbols);
3039 printf_filtered (regexp
3040 ? "All %ss matching regular expression \"%s\":\n"
3041 : "All defined %ss:\n",
3042 classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp);
3044 for (p = symbols; p != NULL; p = p->next)
3048 if (p->msymbol != NULL)
3052 printf_filtered ("\nNon-debugging symbols:\n");
3055 print_msymbol_info (p->msymbol);
3059 print_symbol_info (kind,
3064 last_filename = p->symtab->filename;
3068 do_cleanups (old_chain);
3072 variables_info (char *regexp, int from_tty)
3074 symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty);
3078 functions_info (char *regexp, int from_tty)
3080 symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty);
3085 types_info (char *regexp, int from_tty)
3087 symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty);
3091 /* Tiemann says: "info methods was never implemented." */
3093 methods_info (char *regexp)
3095 symtab_symbol_info (regexp, METHODS_NAMESPACE, 0, from_tty);
3099 /* Breakpoint all functions matching regular expression. */
3102 rbreak_command_wrapper (char *regexp, int from_tty)
3104 rbreak_command (regexp, from_tty);
3108 rbreak_command (char *regexp, int from_tty)
3110 struct symbol_search *ss;
3111 struct symbol_search *p;
3112 struct cleanup *old_chain;
3114 search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss);
3115 old_chain = make_cleanup_free_search_symbols (ss);
3117 for (p = ss; p != NULL; p = p->next)
3119 if (p->msymbol == NULL)
3121 char *string = (char *) alloca (strlen (p->symtab->filename)
3122 + strlen (SYMBOL_NAME (p->symbol))
3124 strcpy (string, p->symtab->filename);
3125 strcat (string, ":'");
3126 strcat (string, SYMBOL_NAME (p->symbol));
3127 strcat (string, "'");
3128 break_command (string, from_tty);
3129 print_symbol_info (FUNCTIONS_NAMESPACE,
3133 p->symtab->filename);
3137 break_command (SYMBOL_NAME (p->msymbol), from_tty);
3138 printf_filtered ("<function, no debug info> %s;\n",
3139 SYMBOL_SOURCE_NAME (p->msymbol));
3143 do_cleanups (old_chain);
3147 /* Return Nonzero if block a is lexically nested within block b,
3148 or if a and b have the same pc range.
3149 Return zero otherwise. */
3151 contained_in (struct block *a, struct block *b)
3155 return BLOCK_START (a) >= BLOCK_START (b)
3156 && BLOCK_END (a) <= BLOCK_END (b);
3160 /* Helper routine for make_symbol_completion_list. */
3162 static int return_val_size;
3163 static int return_val_index;
3164 static char **return_val;
3166 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3168 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
3169 /* Put only the mangled name on the list. */ \
3170 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
3171 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
3172 completion_list_add_name \
3173 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
3175 completion_list_add_name \
3176 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
3179 /* Test to see if the symbol specified by SYMNAME (which is already
3180 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3181 characters. If so, add it to the current completion list. */
3184 completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
3185 char *text, char *word)
3190 /* clip symbols that cannot match */
3192 if (strncmp (symname, sym_text, sym_text_len) != 0)
3197 /* We have a match for a completion, so add SYMNAME to the current list
3198 of matches. Note that the name is moved to freshly malloc'd space. */
3202 if (word == sym_text)
3204 new = xmalloc (strlen (symname) + 5);
3205 strcpy (new, symname);
3207 else if (word > sym_text)
3209 /* Return some portion of symname. */
3210 new = xmalloc (strlen (symname) + 5);
3211 strcpy (new, symname + (word - sym_text));
3215 /* Return some of SYM_TEXT plus symname. */
3216 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
3217 strncpy (new, word, sym_text - word);
3218 new[sym_text - word] = '\0';
3219 strcat (new, symname);
3222 if (return_val_index + 3 > return_val_size)
3224 newsize = (return_val_size *= 2) * sizeof (char *);
3225 return_val = (char **) xrealloc ((char *) return_val, newsize);
3227 return_val[return_val_index++] = new;
3228 return_val[return_val_index] = NULL;
3232 /* Return a NULL terminated array of all symbols (regardless of class)
3233 which begin by matching TEXT. If the answer is no symbols, then
3234 the return value is an array which contains only a NULL pointer.
3236 Problem: All of the symbols have to be copied because readline frees them.
3237 I'm not going to worry about this; hopefully there won't be that many. */
3240 make_symbol_completion_list (char *text, char *word)
3242 register struct symbol *sym;
3243 register struct symtab *s;
3244 register struct partial_symtab *ps;
3245 register struct minimal_symbol *msymbol;
3246 register struct objfile *objfile;
3247 register struct block *b, *surrounding_static_block = 0;
3249 struct partial_symbol **psym;
3250 /* The symbol we are completing on. Points in same buffer as text. */
3252 /* Length of sym_text. */
3255 /* Now look for the symbol we are supposed to complete on.
3256 FIXME: This should be language-specific. */
3260 char *quote_pos = NULL;
3262 /* First see if this is a quoted string. */
3264 for (p = text; *p != '\0'; ++p)
3266 if (quote_found != '\0')
3268 if (*p == quote_found)
3269 /* Found close quote. */
3271 else if (*p == '\\' && p[1] == quote_found)
3272 /* A backslash followed by the quote character
3273 doesn't end the string. */
3276 else if (*p == '\'' || *p == '"')
3282 if (quote_found == '\'')
3283 /* A string within single quotes can be a symbol, so complete on it. */
3284 sym_text = quote_pos + 1;
3285 else if (quote_found == '"')
3286 /* A double-quoted string is never a symbol, nor does it make sense
3287 to complete it any other way. */
3289 return_val = (char **) xmalloc (sizeof (char *));
3290 return_val[0] = NULL;
3295 /* It is not a quoted string. Break it based on the characters
3296 which are in symbols. */
3299 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3308 sym_text_len = strlen (sym_text);
3310 return_val_size = 100;
3311 return_val_index = 0;
3312 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3313 return_val[0] = NULL;
3315 /* Look through the partial symtabs for all symbols which begin
3316 by matching SYM_TEXT. Add each one that you find to the list. */
3318 ALL_PSYMTABS (objfile, ps)
3320 /* If the psymtab's been read in we'll get it when we search
3321 through the blockvector. */
3325 for (psym = objfile->global_psymbols.list + ps->globals_offset;
3326 psym < (objfile->global_psymbols.list + ps->globals_offset
3327 + ps->n_global_syms);
3330 /* If interrupted, then quit. */
3332 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3335 for (psym = objfile->static_psymbols.list + ps->statics_offset;
3336 psym < (objfile->static_psymbols.list + ps->statics_offset
3337 + ps->n_static_syms);
3341 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3345 /* At this point scan through the misc symbol vectors and add each
3346 symbol you find to the list. Eventually we want to ignore
3347 anything that isn't a text symbol (everything else will be
3348 handled by the psymtab code above). */
3350 ALL_MSYMBOLS (objfile, msymbol)
3353 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3356 /* Search upwards from currently selected frame (so that we can
3357 complete on local vars. */
3359 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
3361 if (!BLOCK_SUPERBLOCK (b))
3363 surrounding_static_block = b; /* For elmin of dups */
3366 /* Also catch fields of types defined in this places which match our
3367 text string. Only complete on types visible from current context. */
3369 ALL_BLOCK_SYMBOLS (b, i, sym)
3371 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3372 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3374 struct type *t = SYMBOL_TYPE (sym);
3375 enum type_code c = TYPE_CODE (t);
3377 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
3379 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
3381 if (TYPE_FIELD_NAME (t, j))
3383 completion_list_add_name (TYPE_FIELD_NAME (t, j),
3384 sym_text, sym_text_len, text, word);
3392 /* Go through the symtabs and check the externs and statics for
3393 symbols which match. */
3395 ALL_SYMTABS (objfile, s)
3398 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3399 ALL_BLOCK_SYMBOLS (b, i, sym)
3401 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3405 ALL_SYMTABS (objfile, s)
3408 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3409 /* Don't do this block twice. */
3410 if (b == surrounding_static_block)
3412 ALL_BLOCK_SYMBOLS (b, i, sym)
3414 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3418 return (return_val);
3421 /* Like make_symbol_completion_list, but returns a list of symbols
3422 defined in a source file FILE. */
3425 make_file_symbol_completion_list (char *text, char *word, char *srcfile)
3427 register struct symbol *sym;
3428 register struct symtab *s;
3429 register struct block *b;
3431 /* The symbol we are completing on. Points in same buffer as text. */
3433 /* Length of sym_text. */
3436 /* Now look for the symbol we are supposed to complete on.
3437 FIXME: This should be language-specific. */
3441 char *quote_pos = NULL;
3443 /* First see if this is a quoted string. */
3445 for (p = text; *p != '\0'; ++p)
3447 if (quote_found != '\0')
3449 if (*p == quote_found)
3450 /* Found close quote. */
3452 else if (*p == '\\' && p[1] == quote_found)
3453 /* A backslash followed by the quote character
3454 doesn't end the string. */
3457 else if (*p == '\'' || *p == '"')
3463 if (quote_found == '\'')
3464 /* A string within single quotes can be a symbol, so complete on it. */
3465 sym_text = quote_pos + 1;
3466 else if (quote_found == '"')
3467 /* A double-quoted string is never a symbol, nor does it make sense
3468 to complete it any other way. */
3470 return_val = (char **) xmalloc (sizeof (char *));
3471 return_val[0] = NULL;
3476 /* It is not a quoted string. Break it based on the characters
3477 which are in symbols. */
3480 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3489 sym_text_len = strlen (sym_text);
3491 return_val_size = 10;
3492 return_val_index = 0;
3493 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3494 return_val[0] = NULL;
3496 /* Find the symtab for SRCFILE (this loads it if it was not yet read
3498 s = lookup_symtab (srcfile);
3501 /* Maybe they typed the file with leading directories, while the
3502 symbol tables record only its basename. */
3503 const char *tail = lbasename (srcfile);
3506 s = lookup_symtab (tail);
3509 /* If we have no symtab for that file, return an empty list. */
3511 return (return_val);
3513 /* Go through this symtab and check the externs and statics for
3514 symbols which match. */
3516 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3517 ALL_BLOCK_SYMBOLS (b, i, sym)
3519 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3522 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3523 ALL_BLOCK_SYMBOLS (b, i, sym)
3525 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3528 return (return_val);
3531 /* A helper function for make_source_files_completion_list. It adds
3532 another file name to a list of possible completions, growing the
3533 list as necessary. */
3536 add_filename_to_list (const char *fname, char *text, char *word,
3537 char ***list, int *list_used, int *list_alloced)
3540 size_t fnlen = strlen (fname);
3542 if (*list_used + 1 >= *list_alloced)
3545 *list = (char **) xrealloc ((char *) *list,
3546 *list_alloced * sizeof (char *));
3551 /* Return exactly fname. */
3552 new = xmalloc (fnlen + 5);
3553 strcpy (new, fname);
3555 else if (word > text)
3557 /* Return some portion of fname. */
3558 new = xmalloc (fnlen + 5);
3559 strcpy (new, fname + (word - text));
3563 /* Return some of TEXT plus fname. */
3564 new = xmalloc (fnlen + (text - word) + 5);
3565 strncpy (new, word, text - word);
3566 new[text - word] = '\0';
3567 strcat (new, fname);
3569 (*list)[*list_used] = new;
3570 (*list)[++*list_used] = NULL;
3574 not_interesting_fname (const char *fname)
3576 static const char *illegal_aliens[] = {
3577 "_globals_", /* inserted by coff_symtab_read */
3582 for (i = 0; illegal_aliens[i]; i++)
3584 if (strcmp (fname, illegal_aliens[i]) == 0)
3590 /* Return a NULL terminated array of all source files whose names
3591 begin with matching TEXT. The file names are looked up in the
3592 symbol tables of this program. If the answer is no matchess, then
3593 the return value is an array which contains only a NULL pointer. */
3596 make_source_files_completion_list (char *text, char *word)
3598 register struct symtab *s;
3599 register struct partial_symtab *ps;
3600 register struct objfile *objfile;
3602 int list_alloced = 1;
3604 size_t text_len = strlen (text);
3605 char **list = (char **) xmalloc (list_alloced * sizeof (char *));
3606 const char *base_name;
3610 if (!have_full_symbols () && !have_partial_symbols ())
3613 ALL_SYMTABS (objfile, s)
3615 if (not_interesting_fname (s->filename))
3617 if (!filename_seen (s->filename, 1, &first)
3618 #if HAVE_DOS_BASED_FILE_SYSTEM
3619 && strncasecmp (s->filename, text, text_len) == 0
3621 && strncmp (s->filename, text, text_len) == 0
3625 /* This file matches for a completion; add it to the current
3627 add_filename_to_list (s->filename, text, word,
3628 &list, &list_used, &list_alloced);
3632 /* NOTE: We allow the user to type a base name when the
3633 debug info records leading directories, but not the other
3634 way around. This is what subroutines of breakpoint
3635 command do when they parse file names. */
3636 base_name = lbasename (s->filename);
3637 if (base_name != s->filename
3638 && !filename_seen (base_name, 1, &first)
3639 #if HAVE_DOS_BASED_FILE_SYSTEM
3640 && strncasecmp (base_name, text, text_len) == 0
3642 && strncmp (base_name, text, text_len) == 0
3645 add_filename_to_list (base_name, text, word,
3646 &list, &list_used, &list_alloced);
3650 ALL_PSYMTABS (objfile, ps)
3652 if (not_interesting_fname (ps->filename))
3656 if (!filename_seen (ps->filename, 1, &first)
3657 #if HAVE_DOS_BASED_FILE_SYSTEM
3658 && strncasecmp (ps->filename, text, text_len) == 0
3660 && strncmp (ps->filename, text, text_len) == 0
3664 /* This file matches for a completion; add it to the
3665 current list of matches. */
3666 add_filename_to_list (ps->filename, text, word,
3667 &list, &list_used, &list_alloced);
3672 base_name = lbasename (ps->filename);
3673 if (base_name != ps->filename
3674 && !filename_seen (base_name, 1, &first)
3675 #if HAVE_DOS_BASED_FILE_SYSTEM
3676 && strncasecmp (base_name, text, text_len) == 0
3678 && strncmp (base_name, text, text_len) == 0
3681 add_filename_to_list (base_name, text, word,
3682 &list, &list_used, &list_alloced);
3690 /* Determine if PC is in the prologue of a function. The prologue is the area
3691 between the first instruction of a function, and the first executable line.
3692 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3694 If non-zero, func_start is where we think the prologue starts, possibly
3695 by previous examination of symbol table information.
3699 in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
3701 struct symtab_and_line sal;
3702 CORE_ADDR func_addr, func_end;
3704 /* We have several sources of information we can consult to figure
3706 - Compilers usually emit line number info that marks the prologue
3707 as its own "source line". So the ending address of that "line"
3708 is the end of the prologue. If available, this is the most
3710 - The minimal symbols and partial symbols, which can usually tell
3711 us the starting and ending addresses of a function.
3712 - If we know the function's start address, we can call the
3713 architecture-defined SKIP_PROLOGUE function to analyze the
3714 instruction stream and guess where the prologue ends.
3715 - Our `func_start' argument; if non-zero, this is the caller's
3716 best guess as to the function's entry point. At the time of
3717 this writing, handle_inferior_event doesn't get this right, so
3718 it should be our last resort. */
3720 /* Consult the partial symbol table, to find which function
3722 if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3724 CORE_ADDR prologue_end;
3726 /* We don't even have minsym information, so fall back to using
3727 func_start, if given. */
3729 return 1; /* We *might* be in a prologue. */
3731 prologue_end = SKIP_PROLOGUE (func_start);
3733 return func_start <= pc && pc < prologue_end;
3736 /* If we have line number information for the function, that's
3737 usually pretty reliable. */
3738 sal = find_pc_line (func_addr, 0);
3740 /* Now sal describes the source line at the function's entry point,
3741 which (by convention) is the prologue. The end of that "line",
3742 sal.end, is the end of the prologue.
3744 Note that, for functions whose source code is all on a single
3745 line, the line number information doesn't always end up this way.
3746 So we must verify that our purported end-of-prologue address is
3747 *within* the function, not at its start or end. */
3749 || sal.end <= func_addr
3750 || func_end <= sal.end)
3752 /* We don't have any good line number info, so use the minsym
3753 information, together with the architecture-specific prologue
3755 CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
3757 return func_addr <= pc && pc < prologue_end;
3760 /* We have line number info, and it looks good. */
3761 return func_addr <= pc && pc < sal.end;
3765 /* Begin overload resolution functions */
3768 remove_params (const char *demangled_name)
3774 if (demangled_name == NULL)
3777 /* First find the end of the arg list. */
3778 argp = strrchr (demangled_name, ')');
3782 /* Back up to the beginning. */
3785 while (argp-- > demangled_name)
3789 else if (*argp == '(')
3798 internal_error (__FILE__, __LINE__,
3799 "bad demangled name %s\n", demangled_name);
3800 while (argp[-1] == ' ' && argp > demangled_name)
3803 new_name = xmalloc (argp - demangled_name + 1);
3804 memcpy (new_name, demangled_name, argp - demangled_name);
3805 new_name[argp - demangled_name] = '\0';
3809 /* Helper routine for make_symbol_completion_list. */
3811 static int sym_return_val_size;
3812 static int sym_return_val_index;
3813 static struct symbol **sym_return_val;
3815 /* Test to see if the symbol specified by SYMNAME (which is already
3816 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3817 characters. If so, add it to the current completion list. */
3820 overload_list_add_symbol (struct symbol *sym, char *oload_name)
3826 /* If there is no type information, we can't do anything, so skip */
3827 if (SYMBOL_TYPE (sym) == NULL)
3830 /* skip any symbols that we've already considered. */
3831 for (i = 0; i < sym_return_val_index; ++i)
3832 if (!strcmp (SYMBOL_NAME (sym), SYMBOL_NAME (sym_return_val[i])))
3835 /* Get the demangled name without parameters */
3836 sym_name = remove_params (SYMBOL_DEMANGLED_NAME (sym));
3840 /* skip symbols that cannot match */
3841 if (strcmp (sym_name, oload_name) != 0)
3849 /* We have a match for an overload instance, so add SYM to the current list
3850 * of overload instances */
3851 if (sym_return_val_index + 3 > sym_return_val_size)
3853 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
3854 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
3856 sym_return_val[sym_return_val_index++] = sym;
3857 sym_return_val[sym_return_val_index] = NULL;
3860 /* Return a null-terminated list of pointers to function symbols that
3861 * match name of the supplied symbol FSYM.
3862 * This is used in finding all overloaded instances of a function name.
3863 * This has been modified from make_symbol_completion_list. */
3867 make_symbol_overload_list (struct symbol *fsym)
3869 register struct symbol *sym;
3870 register struct symtab *s;
3871 register struct partial_symtab *ps;
3872 register struct objfile *objfile;
3873 register struct block *b, *surrounding_static_block = 0;
3875 /* The name we are completing on. */
3876 char *oload_name = NULL;
3877 /* Length of name. */
3878 int oload_name_len = 0;
3880 /* Look for the symbol we are supposed to complete on. */
3882 oload_name = remove_params (SYMBOL_DEMANGLED_NAME (fsym));
3885 sym_return_val_size = 1;
3886 sym_return_val = (struct symbol **) xmalloc (2 * sizeof (struct symbol *));
3887 sym_return_val[0] = fsym;
3888 sym_return_val[1] = NULL;
3890 return sym_return_val;
3892 oload_name_len = strlen (oload_name);
3894 sym_return_val_size = 100;
3895 sym_return_val_index = 0;
3896 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
3897 sym_return_val[0] = NULL;
3899 /* Look through the partial symtabs for all symbols which begin
3900 by matching OLOAD_NAME. Make sure we read that symbol table in. */
3902 ALL_PSYMTABS (objfile, ps)
3904 struct partial_symbol **psym;
3906 /* If the psymtab's been read in we'll get it when we search
3907 through the blockvector. */
3911 for (psym = objfile->global_psymbols.list + ps->globals_offset;
3912 psym < (objfile->global_psymbols.list + ps->globals_offset
3913 + ps->n_global_syms);
3916 /* If interrupted, then quit. */
3918 /* This will cause the symbol table to be read if it has not yet been */
3919 s = PSYMTAB_TO_SYMTAB (ps);
3922 for (psym = objfile->static_psymbols.list + ps->statics_offset;
3923 psym < (objfile->static_psymbols.list + ps->statics_offset
3924 + ps->n_static_syms);
3928 /* This will cause the symbol table to be read if it has not yet been */
3929 s = PSYMTAB_TO_SYMTAB (ps);
3933 /* Search upwards from currently selected frame (so that we can
3934 complete on local vars. */
3936 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
3938 if (!BLOCK_SUPERBLOCK (b))
3940 surrounding_static_block = b; /* For elimination of dups */
3943 /* Also catch fields of types defined in this places which match our
3944 text string. Only complete on types visible from current context. */
3946 ALL_BLOCK_SYMBOLS (b, i, sym)
3948 overload_list_add_symbol (sym, oload_name);
3952 /* Go through the symtabs and check the externs and statics for
3953 symbols which match. */
3955 ALL_SYMTABS (objfile, s)
3958 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3959 ALL_BLOCK_SYMBOLS (b, i, sym)
3961 overload_list_add_symbol (sym, oload_name);
3965 ALL_SYMTABS (objfile, s)
3968 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3969 /* Don't do this block twice. */
3970 if (b == surrounding_static_block)
3972 ALL_BLOCK_SYMBOLS (b, i, sym)
3974 overload_list_add_symbol (sym, oload_name);
3980 return (sym_return_val);
3983 /* End of overload resolution functions */
3985 struct symtabs_and_lines
3986 decode_line_spec (char *string, int funfirstline)
3988 struct symtabs_and_lines sals;
3989 struct symtab_and_line cursal;
3992 error ("Empty line specification.");
3994 /* We use whatever is set as the current source line. We do not try
3995 and get a default or it will recursively call us! */
3996 cursal = get_current_source_symtab_and_line ();
3998 sals = decode_line_1 (&string, funfirstline,
3999 cursal.symtab, cursal.line,
4003 error ("Junk at end of line specification: %s", string);
4008 static char *name_of_main;
4011 set_main_name (const char *name)
4013 if (name_of_main != NULL)
4015 xfree (name_of_main);
4016 name_of_main = NULL;
4020 name_of_main = xstrdup (name);
4027 if (name_of_main != NULL)
4028 return name_of_main;
4035 _initialize_symtab (void)
4037 add_info ("variables", variables_info,
4038 "All global and static variable names, or those matching REGEXP.");
4040 add_com ("whereis", class_info, variables_info,
4041 "All global and static variable names, or those matching REGEXP.");
4043 add_info ("functions", functions_info,
4044 "All function names, or those matching REGEXP.");
4047 /* FIXME: This command has at least the following problems:
4048 1. It prints builtin types (in a very strange and confusing fashion).
4049 2. It doesn't print right, e.g. with
4050 typedef struct foo *FOO
4051 type_print prints "FOO" when we want to make it (in this situation)
4052 print "struct foo *".
4053 I also think "ptype" or "whatis" is more likely to be useful (but if
4054 there is much disagreement "info types" can be fixed). */
4055 add_info ("types", types_info,
4056 "All type names, or those matching REGEXP.");
4059 add_info ("methods", methods_info,
4060 "All method names, or those matching REGEXP::REGEXP.\n\
4061 If the class qualifier is omitted, it is assumed to be the current scope.\n\
4062 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
4065 add_info ("sources", sources_info,
4066 "Source files in the program.");
4068 add_com ("rbreak", class_breakpoint, rbreak_command,
4069 "Set a breakpoint for all functions matching REGEXP.");
4073 add_com ("lf", class_info, sources_info, "Source files in the program");
4074 add_com ("lg", class_info, variables_info,
4075 "All global and static variable names, or those matching REGEXP.");
4078 /* Initialize the one built-in type that isn't language dependent... */
4079 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4080 "<unknown type>", (struct objfile *) NULL);