1 /* Symbol table lookup for the GNU debugger, GDB.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
5 Free Software Foundation, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
34 #include "call-cmds.h"
35 #include "gdb_regex.h"
36 #include "expression.h"
42 #include "filenames.h" /* for FILENAME_CMP */
46 #include "gdb_obstack.h"
49 #include <sys/types.h>
51 #include "gdb_string.h"
56 /* Prototypes for local functions */
58 static void completion_list_add_name (char *, char *, int, char *, char *);
60 static void rbreak_command (char *, int);
62 static void types_info (char *, int);
64 static void functions_info (char *, int);
66 static void variables_info (char *, int);
68 static void sources_info (char *, int);
70 static void output_source_filename (char *, int *);
72 static int find_line_common (struct linetable *, int, int *);
74 /* This one is used by linespec.c */
76 char *operator_chars (char *p, char **end);
78 static struct partial_symbol *lookup_partial_symbol (struct partial_symtab *,
82 static struct symbol *lookup_symbol_aux (const char *name,
83 const char *mangled_name,
84 const struct block *block,
85 const namespace_enum namespace,
86 int *is_a_field_of_this,
87 struct symtab **symtab);
90 struct symbol *lookup_symbol_aux_local (const char *name,
91 const char *mangled_name,
92 const struct block *block,
93 const namespace_enum namespace,
94 struct symtab **symtab,
95 const struct block **static_block);
98 struct symbol *lookup_symbol_aux_block (const char *name,
99 const char *mangled_name,
100 const struct block *block,
101 const namespace_enum namespace,
102 struct symtab **symtab);
105 struct symbol *lookup_symbol_aux_symtabs (int block_index,
107 const char *mangled_name,
108 const namespace_enum namespace,
109 struct symtab **symtab);
112 struct symbol *lookup_symbol_aux_psymtabs (int block_index,
114 const char *mangled_name,
115 const namespace_enum namespace,
116 struct symtab **symtab);
119 struct symbol *lookup_symbol_aux_minsyms (const char *name,
120 const char *mangled_name,
121 const namespace_enum namespace,
122 int *is_a_field_of_this,
123 struct symtab **symtab);
125 static struct symbol *find_active_alias (struct symbol *sym, CORE_ADDR addr);
127 /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
128 /* Signals the presence of objects compiled by HP compilers */
129 int hp_som_som_object_present = 0;
131 static void fixup_section (struct general_symbol_info *, struct objfile *);
133 static int file_matches (char *, char **, int);
135 static void print_symbol_info (namespace_enum,
136 struct symtab *, struct symbol *, int, char *);
138 static void print_msymbol_info (struct minimal_symbol *);
140 static void symtab_symbol_info (char *, namespace_enum, int);
142 static void overload_list_add_symbol (struct symbol *sym, char *oload_name);
144 void _initialize_symtab (void);
148 /* The single non-language-specific builtin type */
149 struct type *builtin_type_error;
151 /* Block in which the most recently searched-for symbol was found.
152 Might be better to make this a parameter to lookup_symbol and
155 const struct block *block_found;
157 /* Check for a symtab of a specific name; first in symtabs, then in
158 psymtabs. *If* there is no '/' in the name, a match after a '/'
159 in the symtab filename will also work. */
162 lookup_symtab (const char *name)
164 register struct symtab *s;
165 register struct partial_symtab *ps;
166 register struct objfile *objfile;
167 char *real_path = NULL;
168 char *full_path = NULL;
170 /* Here we are interested in canonicalizing an absolute path, not
171 absolutizing a relative path. */
172 if (IS_ABSOLUTE_PATH (name))
174 full_path = xfullpath (name);
175 make_cleanup (xfree, full_path);
176 real_path = gdb_realpath (name);
177 make_cleanup (xfree, real_path);
182 /* First, search for an exact match */
184 ALL_SYMTABS (objfile, s)
186 if (FILENAME_CMP (name, s->filename) == 0)
191 /* If the user gave us an absolute path, try to find the file in
192 this symtab and use its absolute path. */
194 if (full_path != NULL)
196 const char *fp = symtab_to_filename (s);
197 if (FILENAME_CMP (full_path, fp) == 0)
203 if (real_path != NULL)
205 char *rp = gdb_realpath (symtab_to_filename (s));
206 make_cleanup (xfree, rp);
207 if (FILENAME_CMP (real_path, rp) == 0)
214 /* Now, search for a matching tail (only if name doesn't have any dirs) */
216 if (lbasename (name) == name)
217 ALL_SYMTABS (objfile, s)
219 if (FILENAME_CMP (lbasename (s->filename), name) == 0)
223 /* Same search rules as above apply here, but now we look thru the
226 ps = lookup_partial_symtab (name);
231 error ("Internal: readin %s pst for `%s' found when no symtab found.",
234 s = PSYMTAB_TO_SYMTAB (ps);
239 /* At this point, we have located the psymtab for this file, but
240 the conversion to a symtab has failed. This usually happens
241 when we are looking up an include file. In this case,
242 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
243 been created. So, we need to run through the symtabs again in
244 order to find the file.
245 XXX - This is a crock, and should be fixed inside of the the
246 symbol parsing routines. */
250 /* Lookup the partial symbol table of a source file named NAME.
251 *If* there is no '/' in the name, a match after a '/'
252 in the psymtab filename will also work. */
254 struct partial_symtab *
255 lookup_partial_symtab (const char *name)
257 register struct partial_symtab *pst;
258 register struct objfile *objfile;
259 char *full_path = NULL;
260 char *real_path = NULL;
262 /* Here we are interested in canonicalizing an absolute path, not
263 absolutizing a relative path. */
264 if (IS_ABSOLUTE_PATH (name))
266 full_path = xfullpath (name);
267 make_cleanup (xfree, full_path);
268 real_path = gdb_realpath (name);
269 make_cleanup (xfree, real_path);
272 ALL_PSYMTABS (objfile, pst)
274 if (FILENAME_CMP (name, pst->filename) == 0)
279 /* If the user gave us an absolute path, try to find the file in
280 this symtab and use its absolute path. */
281 if (full_path != NULL)
283 if (pst->fullname == NULL)
284 source_full_path_of (pst->filename, &pst->fullname);
285 if (pst->fullname != NULL
286 && FILENAME_CMP (full_path, pst->fullname) == 0)
292 if (real_path != NULL)
295 if (pst->fullname == NULL)
296 source_full_path_of (pst->filename, &pst->fullname);
297 if (pst->fullname != NULL)
299 rp = gdb_realpath (pst->fullname);
300 make_cleanup (xfree, rp);
302 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
309 /* Now, search for a matching tail (only if name doesn't have any dirs) */
311 if (lbasename (name) == name)
312 ALL_PSYMTABS (objfile, pst)
314 if (FILENAME_CMP (lbasename (pst->filename), name) == 0)
321 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
322 full method name, which consist of the class name (from T), the unadorned
323 method name from METHOD_ID, and the signature for the specific overload,
324 specified by SIGNATURE_ID. Note that this function is g++ specific. */
327 gdb_mangle_name (struct type *type, int method_id, int signature_id)
329 int mangled_name_len;
331 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, method_id);
332 struct fn_field *method = &f[signature_id];
333 char *field_name = TYPE_FN_FIELDLIST_NAME (type, method_id);
334 char *physname = TYPE_FN_FIELD_PHYSNAME (f, signature_id);
335 char *newname = type_name_no_tag (type);
337 /* Does the form of physname indicate that it is the full mangled name
338 of a constructor (not just the args)? */
339 int is_full_physname_constructor;
342 int is_destructor = is_destructor_name (physname);
343 /* Need a new type prefix. */
344 char *const_prefix = method->is_const ? "C" : "";
345 char *volatile_prefix = method->is_volatile ? "V" : "";
347 int len = (newname == NULL ? 0 : strlen (newname));
349 /* Nothing to do if physname already contains a fully mangled v3 abi name
350 or an operator name. */
351 if ((physname[0] == '_' && physname[1] == 'Z')
352 || is_operator_name (field_name))
353 return xstrdup (physname);
355 is_full_physname_constructor = is_constructor_name (physname);
358 is_full_physname_constructor || (newname && STREQ (field_name, newname));
361 is_destructor = (strncmp (physname, "__dt", 4) == 0);
363 if (is_destructor || is_full_physname_constructor)
365 mangled_name = (char *) xmalloc (strlen (physname) + 1);
366 strcpy (mangled_name, physname);
372 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
374 else if (physname[0] == 't' || physname[0] == 'Q')
376 /* The physname for template and qualified methods already includes
378 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
384 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
386 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
387 + strlen (buf) + len + strlen (physname) + 1);
390 mangled_name = (char *) xmalloc (mangled_name_len);
392 mangled_name[0] = '\0';
394 strcpy (mangled_name, field_name);
396 strcat (mangled_name, buf);
397 /* If the class doesn't have a name, i.e. newname NULL, then we just
398 mangle it using 0 for the length of the class. Thus it gets mangled
399 as something starting with `::' rather than `classname::'. */
401 strcat (mangled_name, newname);
403 strcat (mangled_name, physname);
404 return (mangled_name);
408 /* Initialize the language dependent portion of a symbol
409 depending upon the language for the symbol. */
411 symbol_init_language_specific (struct general_symbol_info *gsymbol,
412 enum language language)
414 gsymbol->language = language;
415 if (gsymbol->language == language_cplus
416 || gsymbol->language == language_java)
418 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
420 else if (gsymbol->language == language_objc)
422 gsymbol->language_specific.objc_specific.demangled_name = NULL;
426 memset (&gsymbol->language_specific, 0,
427 sizeof (gsymbol->language_specific));
431 /* Functions to initialize a symbol's mangled name. */
433 /* Create the hash table used for demangled names. Each hash entry is
434 a pair of strings; one for the mangled name and one for the demangled
435 name. The entry is hashed via just the mangled name. */
438 create_demangled_names_hash (struct objfile *objfile)
440 /* Choose 256 as the starting size of the hash table, somewhat arbitrarily.
441 The hash table code will round this up to the next prime number.
442 Choosing a much larger table size wastes memory, and saves only about
443 1% in symbol reading. */
445 objfile->demangled_names_hash = htab_create_alloc_ex
446 (256, htab_hash_string, (int (*) (const void *, const void *)) streq,
447 NULL, objfile->md, xmcalloc, xmfree);
450 /* Try to determine the demangled name for a symbol, based on the
451 language of that symbol. If the language is set to language_auto,
452 it will attempt to find any demangling algorithm that works and
453 then set the language appropriately. The returned name is allocated
454 by the demangler and should be xfree'd. */
457 symbol_find_demangled_name (struct general_symbol_info *gsymbol,
460 char *demangled = NULL;
462 if (gsymbol->language == language_unknown)
463 gsymbol->language = language_auto;
464 if (gsymbol->language == language_cplus
465 || gsymbol->language == language_auto)
468 cplus_demangle (mangled, DMGL_PARAMS | DMGL_ANSI);
469 if (demangled != NULL)
471 gsymbol->language = language_cplus;
475 if (gsymbol->language == language_java)
478 cplus_demangle (mangled,
479 DMGL_PARAMS | DMGL_ANSI | DMGL_JAVA);
480 if (demangled != NULL)
482 gsymbol->language = language_java;
489 /* Set both the mangled and demangled (if any) names for GSYMBOL based on
490 NAME and LEN. The hash table corresponding to OBJFILE is used, and the
491 memory comes from that objfile's symbol_obstack. NAME is copied, so the
492 pointer can be discarded after calling this function. */
495 symbol_set_names (struct general_symbol_info *gsymbol,
496 const char *name, int len, struct objfile *objfile)
501 if (objfile->demangled_names_hash == NULL)
502 create_demangled_names_hash (objfile);
504 /* The stabs reader generally provides names that are not NULL-terminated;
505 most of the other readers don't do this, so we can just use the given
509 char *alloc_name = alloca (len + 1);
510 memcpy (alloc_name, name, len);
512 tmpname = alloc_name;
517 slot = (char **) htab_find_slot (objfile->demangled_names_hash, tmpname, INSERT);
519 /* If this name is not in the hash table, add it. */
522 char *demangled_name = symbol_find_demangled_name (gsymbol, tmpname);
523 int demangled_len = demangled_name ? strlen (demangled_name) : 0;
525 /* If there is a demangled name, place it right after the mangled name.
526 Otherwise, just place a second zero byte after the end of the mangled
528 *slot = obstack_alloc (&objfile->symbol_obstack,
529 len + demangled_len + 2);
530 memcpy (*slot, tmpname, len + 1);
533 memcpy (*slot + len + 1, demangled_name, demangled_len + 1);
534 xfree (demangled_name);
537 (*slot)[len + 1] = 0;
540 gsymbol->name = *slot;
541 if ((*slot)[len + 1])
542 gsymbol->language_specific.cplus_specific.demangled_name
545 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
548 /* Initialize the demangled name of GSYMBOL if possible. Any required space
549 to store the name is obtained from the specified obstack. The function
550 symbol_set_names, above, should be used instead where possible for more
551 efficient memory usage. */
554 symbol_init_demangled_name (struct general_symbol_info *gsymbol,
555 struct obstack *obstack)
557 char *mangled = gsymbol->name;
558 char *demangled = NULL;
560 demangled = symbol_find_demangled_name (gsymbol, mangled);
561 if (gsymbol->language == language_cplus
562 || gsymbol->language == language_java)
566 gsymbol->language_specific.cplus_specific.demangled_name
567 = obsavestring (demangled, strlen (demangled), obstack);
571 gsymbol->language_specific.cplus_specific.demangled_name = NULL;
575 /* Unknown language; just clean up quietly. */
581 /* Return the demangled name for a symbol based on the language for
582 that symbol. If no demangled name exists, return NULL. */
584 symbol_demangled_name (struct general_symbol_info *gsymbol)
586 if (gsymbol->language == language_cplus
587 || gsymbol->language == language_java)
588 return gsymbol->language_specific.cplus_specific.demangled_name;
590 else if (gsymbol->language == language_objc)
591 return gsymbol->language_specific.objc_specific.demangled_name;
597 /* Initialize the structure fields to zero values. */
599 init_sal (struct symtab_and_line *sal)
610 /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
612 struct partial_symtab *
613 find_pc_sect_psymtab (CORE_ADDR pc, asection *section)
615 register struct partial_symtab *pst;
616 register struct objfile *objfile;
617 struct minimal_symbol *msymbol;
619 /* If we know that this is not a text address, return failure. This is
620 necessary because we loop based on texthigh and textlow, which do
621 not include the data ranges. */
622 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
624 && (msymbol->type == mst_data
625 || msymbol->type == mst_bss
626 || msymbol->type == mst_abs
627 || msymbol->type == mst_file_data
628 || msymbol->type == mst_file_bss))
631 ALL_PSYMTABS (objfile, pst)
633 if (pc >= pst->textlow && pc < pst->texthigh)
635 struct partial_symtab *tpst;
637 /* An objfile that has its functions reordered might have
638 many partial symbol tables containing the PC, but
639 we want the partial symbol table that contains the
640 function containing the PC. */
641 if (!(objfile->flags & OBJF_REORDERED) &&
642 section == 0) /* can't validate section this way */
648 for (tpst = pst; tpst != NULL; tpst = tpst->next)
650 if (pc >= tpst->textlow && pc < tpst->texthigh)
652 struct partial_symbol *p;
654 p = find_pc_sect_psymbol (tpst, pc, section);
656 && SYMBOL_VALUE_ADDRESS (p)
657 == SYMBOL_VALUE_ADDRESS (msymbol))
667 /* Find which partial symtab contains PC. Return 0 if none.
668 Backward compatibility, no section */
670 struct partial_symtab *
671 find_pc_psymtab (CORE_ADDR pc)
673 return find_pc_sect_psymtab (pc, find_pc_mapped_section (pc));
676 /* Find which partial symbol within a psymtab matches PC and SECTION.
677 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
679 struct partial_symbol *
680 find_pc_sect_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc,
683 struct partial_symbol *best = NULL, *p, **pp;
687 psymtab = find_pc_sect_psymtab (pc, section);
691 /* Cope with programs that start at address 0 */
692 best_pc = (psymtab->textlow != 0) ? psymtab->textlow - 1 : 0;
694 /* Search the global symbols as well as the static symbols, so that
695 find_pc_partial_function doesn't use a minimal symbol and thus
696 cache a bad endaddr. */
697 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
698 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
699 < psymtab->n_global_syms);
703 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
704 && SYMBOL_CLASS (p) == LOC_BLOCK
705 && pc >= SYMBOL_VALUE_ADDRESS (p)
706 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
707 || (psymtab->textlow == 0
708 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
710 if (section) /* match on a specific section */
712 fixup_psymbol_section (p, psymtab->objfile);
713 if (SYMBOL_BFD_SECTION (p) != section)
716 best_pc = SYMBOL_VALUE_ADDRESS (p);
721 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
722 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
723 < psymtab->n_static_syms);
727 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
728 && SYMBOL_CLASS (p) == LOC_BLOCK
729 && pc >= SYMBOL_VALUE_ADDRESS (p)
730 && (SYMBOL_VALUE_ADDRESS (p) > best_pc
731 || (psymtab->textlow == 0
732 && best_pc == 0 && SYMBOL_VALUE_ADDRESS (p) == 0)))
734 if (section) /* match on a specific section */
736 fixup_psymbol_section (p, psymtab->objfile);
737 if (SYMBOL_BFD_SECTION (p) != section)
740 best_pc = SYMBOL_VALUE_ADDRESS (p);
748 /* Find which partial symbol within a psymtab matches PC. Return 0 if none.
749 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
751 struct partial_symbol *
752 find_pc_psymbol (struct partial_symtab *psymtab, CORE_ADDR pc)
754 return find_pc_sect_psymbol (psymtab, pc, find_pc_mapped_section (pc));
757 /* Debug symbols usually don't have section information. We need to dig that
758 out of the minimal symbols and stash that in the debug symbol. */
761 fixup_section (struct general_symbol_info *ginfo, struct objfile *objfile)
763 struct minimal_symbol *msym;
764 msym = lookup_minimal_symbol (ginfo->name, NULL, objfile);
768 ginfo->bfd_section = SYMBOL_BFD_SECTION (msym);
769 ginfo->section = SYMBOL_SECTION (msym);
774 fixup_symbol_section (struct symbol *sym, struct objfile *objfile)
779 if (SYMBOL_BFD_SECTION (sym))
782 fixup_section (&sym->ginfo, objfile);
787 struct partial_symbol *
788 fixup_psymbol_section (struct partial_symbol *psym, struct objfile *objfile)
793 if (SYMBOL_BFD_SECTION (psym))
796 fixup_section (&psym->ginfo, objfile);
801 /* Find the definition for a specified symbol name NAME
802 in namespace NAMESPACE, visible from lexical block BLOCK.
803 Returns the struct symbol pointer, or zero if no symbol is found.
804 If SYMTAB is non-NULL, store the symbol table in which the
805 symbol was found there, or NULL if not found.
806 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
807 NAME is a field of the current implied argument `this'. If so set
808 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
809 BLOCK_FOUND is set to the block in which NAME is found (in the case of
810 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
812 /* This function has a bunch of loops in it and it would seem to be
813 attractive to put in some QUIT's (though I'm not really sure
814 whether it can run long enough to be really important). But there
815 are a few calls for which it would appear to be bad news to quit
816 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c. (Note
817 that there is C++ code below which can error(), but that probably
818 doesn't affect these calls since they are looking for a known
819 variable and thus can probably assume it will never hit the C++
823 lookup_symbol (const char *name, const struct block *block,
824 const namespace_enum namespace, int *is_a_field_of_this,
825 struct symtab **symtab)
827 char *demangled_name = NULL;
828 const char *modified_name = NULL;
829 const char *mangled_name = NULL;
830 int needtofreename = 0;
831 struct symbol *returnval;
833 modified_name = name;
835 /* If we are using C++ language, demangle the name before doing a lookup, so
836 we can always binary search. */
837 if (current_language->la_language == language_cplus)
839 demangled_name = cplus_demangle (name, DMGL_ANSI | DMGL_PARAMS);
843 modified_name = demangled_name;
848 if (case_sensitivity == case_sensitive_off)
854 copy = (char *) alloca (len + 1);
855 for (i= 0; i < len; i++)
856 copy[i] = tolower (name[i]);
858 modified_name = copy;
861 returnval = lookup_symbol_aux (modified_name, mangled_name, block,
862 namespace, is_a_field_of_this, symtab);
864 xfree (demangled_name);
869 static struct symbol *
870 lookup_symbol_aux (const char *name, const char *mangled_name,
871 const struct block *block, const namespace_enum namespace,
872 int *is_a_field_of_this, struct symtab **symtab)
875 const struct block *static_block;
877 /* Search specified block and its superiors. Don't search
878 STATIC_BLOCK or GLOBAL_BLOCK. */
880 sym = lookup_symbol_aux_local (name, mangled_name, block, namespace,
881 symtab, &static_block);
886 /* NOTE: carlton/2002-11-05: At the time that this code was
887 #ifdeffed out, the value of 'block' was always NULL at this
888 point, hence the bemused comments below. */
890 /* FIXME: this code is never executed--block is always NULL at this
891 point. What is it trying to do, anyway? We already should have
892 checked the STATIC_BLOCK above (it is the superblock of top-level
893 blocks). Why is VAR_NAMESPACE special-cased? */
894 /* Don't need to mess with the psymtabs; if we have a block,
895 that file is read in. If we don't, then we deal later with
896 all the psymtab stuff that needs checking. */
897 /* Note (RT): The following never-executed code looks unnecessary to me also.
898 * If we change the code to use the original (passed-in)
899 * value of 'block', we could cause it to execute, but then what
900 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
901 * 'block' was already searched by the above code. And the STATIC_BLOCK's
902 * of *other* symtabs (those files not containing 'block' lexically)
903 * should not contain 'block' address-wise. So we wouldn't expect this
904 * code to find any 'sym''s that were not found above. I vote for
905 * deleting the following paragraph of code.
907 if (namespace == VAR_NAMESPACE && block != NULL)
910 /* Find the right symtab. */
911 ALL_SYMTABS (objfile, s)
913 bv = BLOCKVECTOR (s);
914 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
915 if (BLOCK_START (b) <= BLOCK_START (block)
916 && BLOCK_END (b) > BLOCK_START (block))
918 sym = lookup_block_symbol (b, name, mangled_name, VAR_NAMESPACE);
924 return fixup_symbol_section (sym, objfile);
931 /* C++: If requested to do so by the caller,
932 check to see if NAME is a field of `this'. */
933 if (is_a_field_of_this)
935 struct value *v = value_of_this (0);
937 *is_a_field_of_this = 0;
938 if (v && check_field (v, name))
940 *is_a_field_of_this = 1;
947 /* If there's a static block to search, search it next. */
949 /* NOTE: carlton/2002-12-05: There is a question as to whether or
950 not it would be appropriate to search the current global block
951 here as well. (That's what this code used to do before the
952 is_a_field_of_this check was moved up.) On the one hand, it's
953 redundant with the lookup_symbol_aux_symtabs search that happens
954 next. On the other hand, if decode_line_1 is passed an argument
955 like filename:var, then the user presumably wants 'var' to be
956 searched for in filename. On the third hand, there shouldn't be
957 multiple global variables all of which are named 'var', and it's
958 not like decode_line_1 has ever restricted its search to only
959 global variables in a single filename. All in all, only
960 searching the static block here seems best: it's correct and it's
963 /* NOTE: carlton/2002-12-05: There's also a possible performance
964 issue here: if you usually search for global symbols in the
965 current file, then it would be slightly better to search the
966 current global block before searching all the symtabs. But there
967 are other factors that have a much greater effect on performance
968 than that one, so I don't think we should worry about that for
971 if (static_block != NULL)
973 sym = lookup_symbol_aux_block (name, mangled_name, static_block,
979 /* Now search all global blocks. Do the symtab's first, then
980 check the psymtab's. If a psymtab indicates the existence
981 of the desired name as a global, then do psymtab-to-symtab
982 conversion on the fly and return the found symbol. */
984 sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, mangled_name,
991 /* Check for the possibility of the symbol being a function or
992 a mangled variable that is stored in one of the minimal symbol tables.
993 Eventually, all global symbols might be resolved in this way. */
995 sym = lookup_symbol_aux_minsyms (name, mangled_name,
996 namespace, is_a_field_of_this,
1004 sym = lookup_symbol_aux_psymtabs (GLOBAL_BLOCK, name, mangled_name,
1009 /* Now search all static file-level symbols. Not strictly correct,
1010 but more useful than an error. Do the symtabs first, then check
1011 the psymtabs. If a psymtab indicates the existence of the
1012 desired name as a file-level static, then do psymtab-to-symtab
1013 conversion on the fly and return the found symbol. */
1015 sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, mangled_name,
1020 sym = lookup_symbol_aux_psymtabs (STATIC_BLOCK, name, mangled_name,
1027 /* Check for the possibility of the symbol being a function or
1028 a global variable that is stored in one of the minimal symbol tables.
1029 The "minimal symbol table" is built from linker-supplied info.
1031 RT: I moved this check to last, after the complete search of
1032 the global (p)symtab's and static (p)symtab's. For HP-generated
1033 symbol tables, this check was causing a premature exit from
1034 lookup_symbol with NULL return, and thus messing up symbol lookups
1035 of things like "c::f". It seems to me a check of the minimal
1036 symbol table ought to be a last resort in any case. I'm vaguely
1037 worried about the comment below which talks about FORTRAN routines "foo_"
1038 though... is it saying we need to do the "minsym" check before
1039 the static check in this case?
1043 sym = lookup_symbol_aux_minsyms (name, mangled_name,
1044 namespace, is_a_field_of_this,
1057 /* Check to see if the symbol is defined in BLOCK or its superiors.
1058 Don't search STATIC_BLOCK or GLOBAL_BLOCK. If we don't find a
1059 match, store the address of STATIC_BLOCK in static_block. */
1061 static struct symbol *
1062 lookup_symbol_aux_local (const char *name, const char *mangled_name,
1063 const struct block *block,
1064 const namespace_enum namespace,
1065 struct symtab **symtab,
1066 const struct block **static_block)
1070 /* Check if either no block is specified or it's a global block. */
1072 if (block == NULL || BLOCK_SUPERBLOCK (block) == NULL)
1074 *static_block = NULL;
1078 while (BLOCK_SUPERBLOCK (BLOCK_SUPERBLOCK (block)) != NULL)
1080 sym = lookup_symbol_aux_block (name, mangled_name, block, namespace,
1084 block = BLOCK_SUPERBLOCK (block);
1087 /* We've reached the static block. */
1089 *static_block = block;
1093 /* Look up a symbol in a block; if found, locate its symtab, fixup the
1094 symbol, and set block_found appropriately. */
1096 static struct symbol *
1097 lookup_symbol_aux_block (const char *name, const char *mangled_name,
1098 const struct block *block,
1099 const namespace_enum namespace,
1100 struct symtab **symtab)
1103 struct objfile *objfile = NULL;
1104 struct blockvector *bv;
1106 struct symtab *s = NULL;
1108 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1111 block_found = block;
1114 /* Search the list of symtabs for one which contains the
1115 address of the start of this block. */
1116 ALL_SYMTABS (objfile, s)
1118 bv = BLOCKVECTOR (s);
1119 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1120 if (BLOCK_START (b) <= BLOCK_START (block)
1121 && BLOCK_END (b) > BLOCK_START (block))
1128 return fixup_symbol_section (sym, objfile);
1134 /* Check to see if the symbol is defined in one of the symtabs.
1135 BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1136 depending on whether or not we want to search global symbols or
1139 static struct symbol *
1140 lookup_symbol_aux_symtabs (int block_index,
1141 const char *name, const char *mangled_name,
1142 const namespace_enum namespace,
1143 struct symtab **symtab)
1146 struct objfile *objfile;
1147 struct blockvector *bv;
1148 const struct block *block;
1151 ALL_SYMTABS (objfile, s)
1153 bv = BLOCKVECTOR (s);
1154 block = BLOCKVECTOR_BLOCK (bv, block_index);
1155 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1158 block_found = block;
1161 return fixup_symbol_section (sym, objfile);
1168 /* Check to see if the symbol is defined in one of the partial
1169 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or
1170 STATIC_BLOCK, depending on whether or not we want to search global
1171 symbols or static symbols. */
1173 static struct symbol *
1174 lookup_symbol_aux_psymtabs (int block_index, const char *name,
1175 const char *mangled_name,
1176 const namespace_enum namespace,
1177 struct symtab **symtab)
1180 struct objfile *objfile;
1181 struct blockvector *bv;
1182 const struct block *block;
1183 struct partial_symtab *ps;
1185 const int psymtab_index = (block_index == GLOBAL_BLOCK ? 1 : 0);
1187 ALL_PSYMTABS (objfile, ps)
1190 && lookup_partial_symbol (ps, name, psymtab_index, namespace))
1192 s = PSYMTAB_TO_SYMTAB (ps);
1193 bv = BLOCKVECTOR (s);
1194 block = BLOCKVECTOR_BLOCK (bv, block_index);
1195 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1198 /* This shouldn't be necessary, but as a last resort try
1199 looking in the statics even though the psymtab claimed
1200 the symbol was global, or vice-versa. It's possible
1201 that the psymtab gets it wrong in some cases. */
1203 /* FIXME: carlton/2002-09-30: Should we really do that?
1204 If that happens, isn't it likely to be a GDB error, in
1205 which case we should fix the GDB error rather than
1206 silently dealing with it here? So I'd vote for
1207 removing the check for the symbol in the other
1209 block = BLOCKVECTOR_BLOCK (bv,
1210 block_index == GLOBAL_BLOCK ?
1211 STATIC_BLOCK : GLOBAL_BLOCK);
1212 sym = lookup_block_symbol (block, name, mangled_name, namespace);
1214 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>).",
1215 block_index == GLOBAL_BLOCK ? "global" : "static",
1216 name, ps->filename, name, name);
1220 return fixup_symbol_section (sym, objfile);
1227 /* Check for the possibility of the symbol being a function or a
1228 mangled variable that is stored in one of the minimal symbol
1229 tables. Eventually, all global symbols might be resolved in this
1232 /* NOTE: carlton/2002-12-05: At one point, this function was part of
1233 lookup_symbol_aux, and what are now 'return' statements within
1234 lookup_symbol_aux_minsyms returned from lookup_symbol_aux, even if
1235 sym was NULL. As far as I can tell, this was basically accidental;
1236 it didn't happen every time that msymbol was non-NULL, but only if
1237 some additional conditions held as well, and it caused problems
1238 with HP-generated symbol tables. */
1240 static struct symbol *
1241 lookup_symbol_aux_minsyms (const char *name,
1242 const char *mangled_name,
1243 const namespace_enum namespace,
1244 int *is_a_field_of_this,
1245 struct symtab **symtab)
1248 struct blockvector *bv;
1249 const struct block *block;
1250 struct minimal_symbol *msymbol;
1253 if (namespace == VAR_NAMESPACE)
1255 msymbol = lookup_minimal_symbol (name, NULL, NULL);
1257 if (msymbol != NULL)
1259 /* OK, we found a minimal symbol in spite of not finding any
1260 symbol. There are various possible explanations for
1261 this. One possibility is the symbol exists in code not
1262 compiled -g. Another possibility is that the 'psymtab'
1263 isn't doing its job. A third possibility, related to #2,
1264 is that we were confused by name-mangling. For instance,
1265 maybe the psymtab isn't doing its job because it only
1266 know about demangled names, but we were given a mangled
1269 /* We first use the address in the msymbol to try to locate
1270 the appropriate symtab. Note that find_pc_sect_symtab()
1271 has a side-effect of doing psymtab-to-symtab expansion,
1272 for the found symtab. */
1273 s = find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol),
1274 SYMBOL_BFD_SECTION (msymbol));
1277 /* This is a function which has a symtab for its address. */
1278 bv = BLOCKVECTOR (s);
1279 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1281 /* This call used to pass `SYMBOL_NAME (msymbol)' as the
1282 `name' argument to lookup_block_symbol. But the name
1283 of a minimal symbol is always mangled, so that seems
1284 to be clearly the wrong thing to pass as the
1287 lookup_block_symbol (block, name, mangled_name, namespace);
1288 /* We kept static functions in minimal symbol table as well as
1289 in static scope. We want to find them in the symbol table. */
1292 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1293 sym = lookup_block_symbol (block, name,
1294 mangled_name, namespace);
1297 /* NOTE: carlton/2002-12-04: The following comment was
1298 taken from a time when two versions of this function
1299 were part of the body of lookup_symbol_aux: this
1300 comment was taken from the version of the function
1301 that was #ifdef HPUXHPPA, and the comment was right
1302 before the 'return NULL' part of lookup_symbol_aux.
1303 (Hence the "Fall through and return 0" comment.)
1304 Elena did some digging into the situation for
1305 Fortran, and she reports:
1307 "I asked around (thanks to Jeff Knaggs), and I think
1308 the story for Fortran goes like this:
1310 "Apparently, in older Fortrans, '_' was not part of
1311 the user namespace. g77 attached a final '_' to
1312 procedure names as the exported symbols for linkage
1313 (foo_) , but the symbols went in the debug info just
1314 like 'foo'. The rationale behind this is not
1315 completely clear, and maybe it was done to other
1316 symbols as well, not just procedures." */
1318 /* If we get here with sym == 0, the symbol was
1319 found in the minimal symbol table
1320 but not in the symtab.
1321 Fall through and return 0 to use the msymbol
1322 definition of "foo_".
1323 (Note that outer code generally follows up a call
1324 to this routine with a call to lookup_minimal_symbol(),
1325 so a 0 return means we'll just flow into that other routine).
1327 This happens for Fortran "foo_" symbols,
1328 which are "foo" in the symtab.
1330 This can also happen if "asm" is used to make a
1331 regular symbol but not a debugging symbol, e.g.
1332 asm(".globl _main");
1336 if (symtab != NULL && sym != NULL)
1338 return fixup_symbol_section (sym, s->objfile);
1340 else if (MSYMBOL_TYPE (msymbol) != mst_text
1341 && MSYMBOL_TYPE (msymbol) != mst_file_text
1342 && !STREQ (name, SYMBOL_NAME (msymbol)))
1344 /* This is a mangled variable, look it up by its
1346 return lookup_symbol_aux (SYMBOL_NAME (msymbol), mangled_name,
1347 NULL, namespace, is_a_field_of_this,
1356 /* Look, in partial_symtab PST, for symbol NAME. Check the global
1357 symbols if GLOBAL, the static symbols if not */
1359 static struct partial_symbol *
1360 lookup_partial_symbol (struct partial_symtab *pst, const char *name, int global,
1361 namespace_enum namespace)
1363 struct partial_symbol *temp;
1364 struct partial_symbol **start, **psym;
1365 struct partial_symbol **top, **real_top, **bottom, **center;
1366 int length = (global ? pst->n_global_syms : pst->n_static_syms);
1367 int do_linear_search = 1;
1374 pst->objfile->global_psymbols.list + pst->globals_offset :
1375 pst->objfile->static_psymbols.list + pst->statics_offset);
1377 if (global) /* This means we can use a binary search. */
1379 do_linear_search = 0;
1381 /* Binary search. This search is guaranteed to end with center
1382 pointing at the earliest partial symbol with the correct
1383 name. At that point *all* partial symbols with that name
1384 will be checked against the correct namespace. */
1387 top = start + length - 1;
1389 while (top > bottom)
1391 center = bottom + (top - bottom) / 2;
1392 if (!(center < top))
1393 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1394 if (!do_linear_search
1395 && (SYMBOL_LANGUAGE (*center) == language_java))
1397 do_linear_search = 1;
1399 if (strcmp (SYMBOL_SOURCE_NAME (*center), name) >= 0)
1405 bottom = center + 1;
1408 if (!(top == bottom))
1409 internal_error (__FILE__, __LINE__, "failed internal consistency check");
1411 /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so
1412 we don't have to force a linear search on C++. Probably holds true
1413 for JAVA as well, no way to check.*/
1414 while (top <= real_top && SYMBOL_MATCHES_NAME (*top,name))
1416 if (SYMBOL_NAMESPACE (*top) == namespace)
1424 /* Can't use a binary search or else we found during the binary search that
1425 we should also do a linear search. */
1427 if (do_linear_search)
1429 for (psym = start; psym < start + length; psym++)
1431 if (namespace == SYMBOL_NAMESPACE (*psym))
1433 if (SYMBOL_MATCHES_NAME (*psym, name))
1444 /* Look up a type named NAME in the struct_namespace. The type returned
1445 must not be opaque -- i.e., must have at least one field defined
1447 This code was modelled on lookup_symbol -- the parts not relevant to looking
1448 up types were just left out. In particular it's assumed here that types
1449 are available in struct_namespace and only at file-static or global blocks. */
1453 lookup_transparent_type (const char *name)
1455 register struct symbol *sym;
1456 register struct symtab *s = NULL;
1457 register struct partial_symtab *ps;
1458 struct blockvector *bv;
1459 register struct objfile *objfile;
1460 register struct block *block;
1462 /* Now search all the global symbols. Do the symtab's first, then
1463 check the psymtab's. If a psymtab indicates the existence
1464 of the desired name as a global, then do psymtab-to-symtab
1465 conversion on the fly and return the found symbol. */
1467 ALL_SYMTABS (objfile, s)
1469 bv = BLOCKVECTOR (s);
1470 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1471 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1472 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1474 return SYMBOL_TYPE (sym);
1478 ALL_PSYMTABS (objfile, ps)
1480 if (!ps->readin && lookup_partial_symbol (ps, name, 1, STRUCT_NAMESPACE))
1482 s = PSYMTAB_TO_SYMTAB (ps);
1483 bv = BLOCKVECTOR (s);
1484 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1485 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1488 /* This shouldn't be necessary, but as a last resort
1489 * try looking in the statics even though the psymtab
1490 * claimed the symbol was global. It's possible that
1491 * the psymtab gets it wrong in some cases.
1493 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1494 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1496 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1497 %s may be an inlined function, or may be a template function\n\
1498 (if a template, try specifying an instantiation: %s<type>).",
1499 name, ps->filename, name, name);
1501 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1502 return SYMBOL_TYPE (sym);
1506 /* Now search the static file-level symbols.
1507 Not strictly correct, but more useful than an error.
1508 Do the symtab's first, then
1509 check the psymtab's. If a psymtab indicates the existence
1510 of the desired name as a file-level static, then do psymtab-to-symtab
1511 conversion on the fly and return the found symbol.
1514 ALL_SYMTABS (objfile, s)
1516 bv = BLOCKVECTOR (s);
1517 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1518 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1519 if (sym && !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1521 return SYMBOL_TYPE (sym);
1525 ALL_PSYMTABS (objfile, ps)
1527 if (!ps->readin && lookup_partial_symbol (ps, name, 0, STRUCT_NAMESPACE))
1529 s = PSYMTAB_TO_SYMTAB (ps);
1530 bv = BLOCKVECTOR (s);
1531 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
1532 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1535 /* This shouldn't be necessary, but as a last resort
1536 * try looking in the globals even though the psymtab
1537 * claimed the symbol was static. It's possible that
1538 * the psymtab gets it wrong in some cases.
1540 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1541 sym = lookup_block_symbol (block, name, NULL, STRUCT_NAMESPACE);
1543 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1544 %s may be an inlined function, or may be a template function\n\
1545 (if a template, try specifying an instantiation: %s<type>).",
1546 name, ps->filename, name, name);
1548 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
1549 return SYMBOL_TYPE (sym);
1552 return (struct type *) 0;
1556 /* Find the psymtab containing main(). */
1557 /* FIXME: What about languages without main() or specially linked
1558 executables that have no main() ? */
1560 struct partial_symtab *
1561 find_main_psymtab (void)
1563 register struct partial_symtab *pst;
1564 register struct objfile *objfile;
1566 ALL_PSYMTABS (objfile, pst)
1568 if (lookup_partial_symbol (pst, main_name (), 1, VAR_NAMESPACE))
1576 /* Search BLOCK for symbol NAME in NAMESPACE.
1578 Note that if NAME is the demangled form of a C++ symbol, we will fail
1579 to find a match during the binary search of the non-encoded names, but
1580 for now we don't worry about the slight inefficiency of looking for
1581 a match we'll never find, since it will go pretty quick. Once the
1582 binary search terminates, we drop through and do a straight linear
1583 search on the symbols. Each symbol which is marked as being a C++
1584 symbol (language_cplus set) has both the encoded and non-encoded names
1587 If MANGLED_NAME is non-NULL, verify that any symbol we find has this
1588 particular mangled name.
1592 lookup_block_symbol (register const struct block *block, const char *name,
1593 const char *mangled_name,
1594 const namespace_enum namespace)
1596 register int bot, top, inc;
1597 register struct symbol *sym;
1598 register struct symbol *sym_found = NULL;
1599 register int do_linear_search = 1;
1601 if (BLOCK_HASHTABLE (block))
1603 unsigned int hash_index;
1604 hash_index = msymbol_hash_iw (name);
1605 hash_index = hash_index % BLOCK_BUCKETS (block);
1606 for (sym = BLOCK_BUCKET (block, hash_index); sym; sym = sym->hash_next)
1608 if (SYMBOL_NAMESPACE (sym) == namespace
1610 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1611 : SYMBOL_MATCHES_NAME (sym, name)))
1617 /* If the blocks's symbols were sorted, start with a binary search. */
1619 if (BLOCK_SHOULD_SORT (block))
1621 /* Reset the linear search flag so if the binary search fails, we
1622 won't do the linear search once unless we find some reason to
1625 do_linear_search = 0;
1626 top = BLOCK_NSYMS (block);
1629 /* Advance BOT to not far before the first symbol whose name is NAME. */
1633 inc = (top - bot + 1);
1634 /* No need to keep binary searching for the last few bits worth. */
1639 inc = (inc >> 1) + bot;
1640 sym = BLOCK_SYM (block, inc);
1641 if (!do_linear_search && (SYMBOL_LANGUAGE (sym) == language_java))
1643 do_linear_search = 1;
1645 if (SYMBOL_SOURCE_NAME (sym)[0] < name[0])
1649 else if (SYMBOL_SOURCE_NAME (sym)[0] > name[0])
1653 else if (strcmp (SYMBOL_SOURCE_NAME (sym), name) < 0)
1663 /* Now scan forward until we run out of symbols, find one whose
1664 name is greater than NAME, or find one we want. If there is
1665 more than one symbol with the right name and namespace, we
1666 return the first one; I believe it is now impossible for us
1667 to encounter two symbols with the same name and namespace
1668 here, because blocks containing argument symbols are no
1669 longer sorted. The exception is for C++, where multiple functions
1670 (cloned constructors / destructors, in particular) can have
1671 the same demangled name. So if we have a particular
1672 mangled name to match, try to do so. */
1674 top = BLOCK_NSYMS (block);
1677 sym = BLOCK_SYM (block, bot);
1678 if (SYMBOL_NAMESPACE (sym) == namespace
1680 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1681 : SYMBOL_MATCHES_NAME (sym, name)))
1685 if (SYMBOL_SOURCE_NAME (sym)[0] > name[0])
1693 /* Here if block isn't sorted, or we fail to find a match during the
1694 binary search above. If during the binary search above, we find a
1695 symbol which is a Java symbol, then we have re-enabled the linear
1696 search flag which was reset when starting the binary search.
1698 This loop is equivalent to the loop above, but hacked greatly for speed.
1700 Note that parameter symbols do not always show up last in the
1701 list; this loop makes sure to take anything else other than
1702 parameter symbols first; it only uses parameter symbols as a
1703 last resort. Note that this only takes up extra computation
1706 if (do_linear_search)
1708 top = BLOCK_NSYMS (block);
1712 sym = BLOCK_SYM (block, bot);
1713 if (SYMBOL_NAMESPACE (sym) == namespace
1715 ? strcmp (SYMBOL_NAME (sym), mangled_name) == 0
1716 : SYMBOL_MATCHES_NAME (sym, name)))
1718 /* If SYM has aliases, then use any alias that is active
1719 at the current PC. If no alias is active at the current
1720 PC, then use the main symbol.
1722 ?!? Is checking the current pc correct? Is this routine
1723 ever called to look up a symbol from another context?
1725 FIXME: No, it's not correct. If someone sets a
1726 conditional breakpoint at an address, then the
1727 breakpoint's `struct expression' should refer to the
1728 `struct symbol' appropriate for the breakpoint's
1729 address, which may not be the PC.
1731 Even if it were never called from another context,
1732 it's totally bizarre for lookup_symbol's behavior to
1733 depend on the value of the inferior's current PC. We
1734 should pass in the appropriate PC as well as the
1735 block. The interface to lookup_symbol should change
1736 to require the caller to provide a PC. */
1738 if (SYMBOL_ALIASES (sym))
1739 sym = find_active_alias (sym, read_pc ());
1742 if (SYMBOL_CLASS (sym) != LOC_ARG &&
1743 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
1744 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
1745 SYMBOL_CLASS (sym) != LOC_REGPARM &&
1746 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
1747 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
1755 return (sym_found); /* Will be NULL if not found. */
1758 /* Given a main symbol SYM and ADDR, search through the alias
1759 list to determine if an alias is active at ADDR and return
1762 If no alias is active, then return SYM. */
1764 static struct symbol *
1765 find_active_alias (struct symbol *sym, CORE_ADDR addr)
1767 struct range_list *r;
1768 struct alias_list *aliases;
1770 /* If we have aliases, check them first. */
1771 aliases = SYMBOL_ALIASES (sym);
1775 if (!SYMBOL_RANGES (aliases->sym))
1776 return aliases->sym;
1777 for (r = SYMBOL_RANGES (aliases->sym); r; r = r->next)
1779 if (r->start <= addr && r->end > addr)
1780 return aliases->sym;
1782 aliases = aliases->next;
1785 /* Nothing found, return the main symbol. */
1790 /* Find the symtab associated with PC and SECTION. Look through the
1791 psymtabs and read in another symtab if necessary. */
1794 find_pc_sect_symtab (CORE_ADDR pc, asection *section)
1796 register struct block *b;
1797 struct blockvector *bv;
1798 register struct symtab *s = NULL;
1799 register struct symtab *best_s = NULL;
1800 register struct partial_symtab *ps;
1801 register struct objfile *objfile;
1802 CORE_ADDR distance = 0;
1803 struct minimal_symbol *msymbol;
1805 /* If we know that this is not a text address, return failure. This is
1806 necessary because we loop based on the block's high and low code
1807 addresses, which do not include the data ranges, and because
1808 we call find_pc_sect_psymtab which has a similar restriction based
1809 on the partial_symtab's texthigh and textlow. */
1810 msymbol = lookup_minimal_symbol_by_pc_section (pc, section);
1812 && (msymbol->type == mst_data
1813 || msymbol->type == mst_bss
1814 || msymbol->type == mst_abs
1815 || msymbol->type == mst_file_data
1816 || msymbol->type == mst_file_bss))
1819 /* Search all symtabs for the one whose file contains our address, and which
1820 is the smallest of all the ones containing the address. This is designed
1821 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1822 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1823 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1825 This happens for native ecoff format, where code from included files
1826 gets its own symtab. The symtab for the included file should have
1827 been read in already via the dependency mechanism.
1828 It might be swifter to create several symtabs with the same name
1829 like xcoff does (I'm not sure).
1831 It also happens for objfiles that have their functions reordered.
1832 For these, the symtab we are looking for is not necessarily read in. */
1834 ALL_SYMTABS (objfile, s)
1836 bv = BLOCKVECTOR (s);
1837 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
1839 if (BLOCK_START (b) <= pc
1840 && BLOCK_END (b) > pc
1842 || BLOCK_END (b) - BLOCK_START (b) < distance))
1844 /* For an objfile that has its functions reordered,
1845 find_pc_psymtab will find the proper partial symbol table
1846 and we simply return its corresponding symtab. */
1847 /* In order to better support objfiles that contain both
1848 stabs and coff debugging info, we continue on if a psymtab
1850 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1852 ps = find_pc_sect_psymtab (pc, section);
1854 return PSYMTAB_TO_SYMTAB (ps);
1859 struct symbol *sym = NULL;
1861 ALL_BLOCK_SYMBOLS (b, i, sym)
1863 fixup_symbol_section (sym, objfile);
1864 if (section == SYMBOL_BFD_SECTION (sym))
1867 if ((i >= BLOCK_BUCKETS (b)) && (sym == NULL))
1868 continue; /* no symbol in this symtab matches section */
1870 distance = BLOCK_END (b) - BLOCK_START (b);
1879 ps = find_pc_sect_psymtab (pc, section);
1883 /* Might want to error() here (in case symtab is corrupt and
1884 will cause a core dump), but maybe we can successfully
1885 continue, so let's not. */
1887 (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1889 s = PSYMTAB_TO_SYMTAB (ps);
1894 /* Find the symtab associated with PC. Look through the psymtabs and
1895 read in another symtab if necessary. Backward compatibility, no section */
1898 find_pc_symtab (CORE_ADDR pc)
1900 return find_pc_sect_symtab (pc, find_pc_mapped_section (pc));
1904 /* Find the source file and line number for a given PC value and SECTION.
1905 Return a structure containing a symtab pointer, a line number,
1906 and a pc range for the entire source line.
1907 The value's .pc field is NOT the specified pc.
1908 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1909 use the line that ends there. Otherwise, in that case, the line
1910 that begins there is used. */
1912 /* The big complication here is that a line may start in one file, and end just
1913 before the start of another file. This usually occurs when you #include
1914 code in the middle of a subroutine. To properly find the end of a line's PC
1915 range, we must search all symtabs associated with this compilation unit, and
1916 find the one whose first PC is closer than that of the next line in this
1919 /* If it's worth the effort, we could be using a binary search. */
1921 struct symtab_and_line
1922 find_pc_sect_line (CORE_ADDR pc, struct sec *section, int notcurrent)
1925 register struct linetable *l;
1928 register struct linetable_entry *item;
1929 struct symtab_and_line val;
1930 struct blockvector *bv;
1931 struct minimal_symbol *msymbol;
1932 struct minimal_symbol *mfunsym;
1934 /* Info on best line seen so far, and where it starts, and its file. */
1936 struct linetable_entry *best = NULL;
1937 CORE_ADDR best_end = 0;
1938 struct symtab *best_symtab = 0;
1940 /* Store here the first line number
1941 of a file which contains the line at the smallest pc after PC.
1942 If we don't find a line whose range contains PC,
1943 we will use a line one less than this,
1944 with a range from the start of that file to the first line's pc. */
1945 struct linetable_entry *alt = NULL;
1946 struct symtab *alt_symtab = 0;
1948 /* Info on best line seen in this file. */
1950 struct linetable_entry *prev;
1952 /* If this pc is not from the current frame,
1953 it is the address of the end of a call instruction.
1954 Quite likely that is the start of the following statement.
1955 But what we want is the statement containing the instruction.
1956 Fudge the pc to make sure we get that. */
1958 init_sal (&val); /* initialize to zeroes */
1960 /* It's tempting to assume that, if we can't find debugging info for
1961 any function enclosing PC, that we shouldn't search for line
1962 number info, either. However, GAS can emit line number info for
1963 assembly files --- very helpful when debugging hand-written
1964 assembly code. In such a case, we'd have no debug info for the
1965 function, but we would have line info. */
1970 /* elz: added this because this function returned the wrong
1971 information if the pc belongs to a stub (import/export)
1972 to call a shlib function. This stub would be anywhere between
1973 two functions in the target, and the line info was erroneously
1974 taken to be the one of the line before the pc.
1976 /* RT: Further explanation:
1978 * We have stubs (trampolines) inserted between procedures.
1980 * Example: "shr1" exists in a shared library, and a "shr1" stub also
1981 * exists in the main image.
1983 * In the minimal symbol table, we have a bunch of symbols
1984 * sorted by start address. The stubs are marked as "trampoline",
1985 * the others appear as text. E.g.:
1987 * Minimal symbol table for main image
1988 * main: code for main (text symbol)
1989 * shr1: stub (trampoline symbol)
1990 * foo: code for foo (text symbol)
1992 * Minimal symbol table for "shr1" image:
1994 * shr1: code for shr1 (text symbol)
1997 * So the code below is trying to detect if we are in the stub
1998 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
1999 * and if found, do the symbolization from the real-code address
2000 * rather than the stub address.
2002 * Assumptions being made about the minimal symbol table:
2003 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
2004 * if we're really in the trampoline. If we're beyond it (say
2005 * we're in "foo" in the above example), it'll have a closer
2006 * symbol (the "foo" text symbol for example) and will not
2007 * return the trampoline.
2008 * 2. lookup_minimal_symbol_text() will find a real text symbol
2009 * corresponding to the trampoline, and whose address will
2010 * be different than the trampoline address. I put in a sanity
2011 * check for the address being the same, to avoid an
2012 * infinite recursion.
2014 msymbol = lookup_minimal_symbol_by_pc (pc);
2015 if (msymbol != NULL)
2016 if (MSYMBOL_TYPE (msymbol) == mst_solib_trampoline)
2018 mfunsym = lookup_minimal_symbol_text (SYMBOL_NAME (msymbol), NULL, NULL);
2019 if (mfunsym == NULL)
2020 /* I eliminated this warning since it is coming out
2021 * in the following situation:
2022 * gdb shmain // test program with shared libraries
2023 * (gdb) break shr1 // function in shared lib
2024 * Warning: In stub for ...
2025 * In the above situation, the shared lib is not loaded yet,
2026 * so of course we can't find the real func/line info,
2027 * but the "break" still works, and the warning is annoying.
2028 * So I commented out the warning. RT */
2029 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
2031 else if (SYMBOL_VALUE (mfunsym) == SYMBOL_VALUE (msymbol))
2032 /* Avoid infinite recursion */
2033 /* See above comment about why warning is commented out */
2034 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
2037 return find_pc_line (SYMBOL_VALUE (mfunsym), 0);
2041 s = find_pc_sect_symtab (pc, section);
2044 /* if no symbol information, return previous pc */
2051 bv = BLOCKVECTOR (s);
2053 /* Look at all the symtabs that share this blockvector.
2054 They all have the same apriori range, that we found was right;
2055 but they have different line tables. */
2057 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
2059 /* Find the best line in this symtab. */
2066 /* I think len can be zero if the symtab lacks line numbers
2067 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
2068 I'm not sure which, and maybe it depends on the symbol
2074 item = l->item; /* Get first line info */
2076 /* Is this file's first line closer than the first lines of other files?
2077 If so, record this file, and its first line, as best alternate. */
2078 if (item->pc > pc && (!alt || item->pc < alt->pc))
2084 for (i = 0; i < len; i++, item++)
2086 /* Leave prev pointing to the linetable entry for the last line
2087 that started at or before PC. */
2094 /* At this point, prev points at the line whose start addr is <= pc, and
2095 item points at the next line. If we ran off the end of the linetable
2096 (pc >= start of the last line), then prev == item. If pc < start of
2097 the first line, prev will not be set. */
2099 /* Is this file's best line closer than the best in the other files?
2100 If so, record this file, and its best line, as best so far. Don't
2101 save prev if it represents the end of a function (i.e. line number
2102 0) instead of a real line. */
2104 if (prev && prev->line && (!best || prev->pc > best->pc))
2109 /* Discard BEST_END if it's before the PC of the current BEST. */
2110 if (best_end <= best->pc)
2114 /* If another line (denoted by ITEM) is in the linetable and its
2115 PC is after BEST's PC, but before the current BEST_END, then
2116 use ITEM's PC as the new best_end. */
2117 if (best && i < len && item->pc > best->pc
2118 && (best_end == 0 || best_end > item->pc))
2119 best_end = item->pc;
2125 { /* If we didn't find any line # info, just
2131 val.symtab = alt_symtab;
2132 val.line = alt->line - 1;
2134 /* Don't return line 0, that means that we didn't find the line. */
2138 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2142 else if (best->line == 0)
2144 /* If our best fit is in a range of PC's for which no line
2145 number info is available (line number is zero) then we didn't
2146 find any valid line information. */
2151 val.symtab = best_symtab;
2152 val.line = best->line;
2154 if (best_end && (!alt || best_end < alt->pc))
2159 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
2161 val.section = section;
2165 /* Backward compatibility (no section) */
2167 struct symtab_and_line
2168 find_pc_line (CORE_ADDR pc, int notcurrent)
2172 section = find_pc_overlay (pc);
2173 if (pc_in_unmapped_range (pc, section))
2174 pc = overlay_mapped_address (pc, section);
2175 return find_pc_sect_line (pc, section, notcurrent);
2178 /* Find line number LINE in any symtab whose name is the same as
2181 If found, return the symtab that contains the linetable in which it was
2182 found, set *INDEX to the index in the linetable of the best entry
2183 found, and set *EXACT_MATCH nonzero if the value returned is an
2186 If not found, return NULL. */
2189 find_line_symtab (struct symtab *symtab, int line, int *index, int *exact_match)
2193 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2197 struct linetable *best_linetable;
2198 struct symtab *best_symtab;
2200 /* First try looking it up in the given symtab. */
2201 best_linetable = LINETABLE (symtab);
2202 best_symtab = symtab;
2203 best_index = find_line_common (best_linetable, line, &exact);
2204 if (best_index < 0 || !exact)
2206 /* Didn't find an exact match. So we better keep looking for
2207 another symtab with the same name. In the case of xcoff,
2208 multiple csects for one source file (produced by IBM's FORTRAN
2209 compiler) produce multiple symtabs (this is unavoidable
2210 assuming csects can be at arbitrary places in memory and that
2211 the GLOBAL_BLOCK of a symtab has a begin and end address). */
2213 /* BEST is the smallest linenumber > LINE so far seen,
2214 or 0 if none has been seen so far.
2215 BEST_INDEX and BEST_LINETABLE identify the item for it. */
2218 struct objfile *objfile;
2221 if (best_index >= 0)
2222 best = best_linetable->item[best_index].line;
2226 ALL_SYMTABS (objfile, s)
2228 struct linetable *l;
2231 if (!STREQ (symtab->filename, s->filename))
2234 ind = find_line_common (l, line, &exact);
2244 if (best == 0 || l->item[ind].line < best)
2246 best = l->item[ind].line;
2259 *index = best_index;
2261 *exact_match = exact;
2266 /* Set the PC value for a given source file and line number and return true.
2267 Returns zero for invalid line number (and sets the PC to 0).
2268 The source file is specified with a struct symtab. */
2271 find_line_pc (struct symtab *symtab, int line, CORE_ADDR *pc)
2273 struct linetable *l;
2280 symtab = find_line_symtab (symtab, line, &ind, NULL);
2283 l = LINETABLE (symtab);
2284 *pc = l->item[ind].pc;
2291 /* Find the range of pc values in a line.
2292 Store the starting pc of the line into *STARTPTR
2293 and the ending pc (start of next line) into *ENDPTR.
2294 Returns 1 to indicate success.
2295 Returns 0 if could not find the specified line. */
2298 find_line_pc_range (struct symtab_and_line sal, CORE_ADDR *startptr,
2301 CORE_ADDR startaddr;
2302 struct symtab_and_line found_sal;
2305 if (startaddr == 0 && !find_line_pc (sal.symtab, sal.line, &startaddr))
2308 /* This whole function is based on address. For example, if line 10 has
2309 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2310 "info line *0x123" should say the line goes from 0x100 to 0x200
2311 and "info line *0x355" should say the line goes from 0x300 to 0x400.
2312 This also insures that we never give a range like "starts at 0x134
2313 and ends at 0x12c". */
2315 found_sal = find_pc_sect_line (startaddr, sal.section, 0);
2316 if (found_sal.line != sal.line)
2318 /* The specified line (sal) has zero bytes. */
2319 *startptr = found_sal.pc;
2320 *endptr = found_sal.pc;
2324 *startptr = found_sal.pc;
2325 *endptr = found_sal.end;
2330 /* Given a line table and a line number, return the index into the line
2331 table for the pc of the nearest line whose number is >= the specified one.
2332 Return -1 if none is found. The value is >= 0 if it is an index.
2334 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
2337 find_line_common (register struct linetable *l, register int lineno,
2343 /* BEST is the smallest linenumber > LINENO so far seen,
2344 or 0 if none has been seen so far.
2345 BEST_INDEX identifies the item for it. */
2347 int best_index = -1;
2356 for (i = 0; i < len; i++)
2358 register struct linetable_entry *item = &(l->item[i]);
2360 if (item->line == lineno)
2362 /* Return the first (lowest address) entry which matches. */
2367 if (item->line > lineno && (best == 0 || item->line < best))
2374 /* If we got here, we didn't get an exact match. */
2381 find_pc_line_pc_range (CORE_ADDR pc, CORE_ADDR *startptr, CORE_ADDR *endptr)
2383 struct symtab_and_line sal;
2384 sal = find_pc_line (pc, 0);
2387 return sal.symtab != 0;
2390 /* Given a function symbol SYM, find the symtab and line for the start
2392 If the argument FUNFIRSTLINE is nonzero, we want the first line
2393 of real code inside the function. */
2395 struct symtab_and_line
2396 find_function_start_sal (struct symbol *sym, int funfirstline)
2399 struct symtab_and_line sal;
2401 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
2402 fixup_symbol_section (sym, NULL);
2404 { /* skip "first line" of function (which is actually its prologue) */
2405 asection *section = SYMBOL_BFD_SECTION (sym);
2406 /* If function is in an unmapped overlay, use its unmapped LMA
2407 address, so that SKIP_PROLOGUE has something unique to work on */
2408 if (section_is_overlay (section) &&
2409 !section_is_mapped (section))
2410 pc = overlay_unmapped_address (pc, section);
2412 pc += FUNCTION_START_OFFSET;
2413 pc = SKIP_PROLOGUE (pc);
2415 /* For overlays, map pc back into its mapped VMA range */
2416 pc = overlay_mapped_address (pc, section);
2418 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2420 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2421 /* Convex: no need to suppress code on first line, if any */
2424 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2425 line is still part of the same function. */
2427 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
2428 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
2430 /* First pc of next line */
2432 /* Recalculate the line number (might not be N+1). */
2433 sal = find_pc_sect_line (pc, SYMBOL_BFD_SECTION (sym), 0);
2441 /* If P is of the form "operator[ \t]+..." where `...' is
2442 some legitimate operator text, return a pointer to the
2443 beginning of the substring of the operator text.
2444 Otherwise, return "". */
2446 operator_chars (char *p, char **end)
2449 if (strncmp (p, "operator", 8))
2453 /* Don't get faked out by `operator' being part of a longer
2455 if (isalpha (*p) || *p == '_' || *p == '$' || *p == '\0')
2458 /* Allow some whitespace between `operator' and the operator symbol. */
2459 while (*p == ' ' || *p == '\t')
2462 /* Recognize 'operator TYPENAME'. */
2464 if (isalpha (*p) || *p == '_' || *p == '$')
2466 register char *q = p + 1;
2467 while (isalnum (*q) || *q == '_' || *q == '$')
2476 case '\\': /* regexp quoting */
2479 if (p[2] == '=') /* 'operator\*=' */
2481 else /* 'operator\*' */
2485 else if (p[1] == '[')
2488 error ("mismatched quoting on brackets, try 'operator\\[\\]'");
2489 else if (p[2] == '\\' && p[3] == ']')
2491 *end = p + 4; /* 'operator\[\]' */
2495 error ("nothing is allowed between '[' and ']'");
2499 /* Gratuitous qoute: skip it and move on. */
2521 if (p[0] == '-' && p[1] == '>')
2523 /* Struct pointer member operator 'operator->'. */
2526 *end = p + 3; /* 'operator->*' */
2529 else if (p[2] == '\\')
2531 *end = p + 4; /* Hopefully 'operator->\*' */
2536 *end = p + 2; /* 'operator->' */
2540 if (p[1] == '=' || p[1] == p[0])
2551 error ("`operator ()' must be specified without whitespace in `()'");
2556 error ("`operator ?:' must be specified without whitespace in `?:'");
2561 error ("`operator []' must be specified without whitespace in `[]'");
2565 error ("`operator %s' not supported", p);
2574 /* If FILE is not already in the table of files, return zero;
2575 otherwise return non-zero. Optionally add FILE to the table if ADD
2576 is non-zero. If *FIRST is non-zero, forget the old table
2579 filename_seen (const char *file, int add, int *first)
2581 /* Table of files seen so far. */
2582 static const char **tab = NULL;
2583 /* Allocated size of tab in elements.
2584 Start with one 256-byte block (when using GNU malloc.c).
2585 24 is the malloc overhead when range checking is in effect. */
2586 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2587 /* Current size of tab in elements. */
2588 static int tab_cur_size;
2594 tab = (const char **) xmalloc (tab_alloc_size * sizeof (*tab));
2598 /* Is FILE in tab? */
2599 for (p = tab; p < tab + tab_cur_size; p++)
2600 if (strcmp (*p, file) == 0)
2603 /* No; maybe add it to tab. */
2606 if (tab_cur_size == tab_alloc_size)
2608 tab_alloc_size *= 2;
2609 tab = (const char **) xrealloc ((char *) tab,
2610 tab_alloc_size * sizeof (*tab));
2612 tab[tab_cur_size++] = file;
2618 /* Slave routine for sources_info. Force line breaks at ,'s.
2619 NAME is the name to print and *FIRST is nonzero if this is the first
2620 name printed. Set *FIRST to zero. */
2622 output_source_filename (char *name, int *first)
2624 /* Since a single source file can result in several partial symbol
2625 tables, we need to avoid printing it more than once. Note: if
2626 some of the psymtabs are read in and some are not, it gets
2627 printed both under "Source files for which symbols have been
2628 read" and "Source files for which symbols will be read in on
2629 demand". I consider this a reasonable way to deal with the
2630 situation. I'm not sure whether this can also happen for
2631 symtabs; it doesn't hurt to check. */
2633 /* Was NAME already seen? */
2634 if (filename_seen (name, 1, first))
2636 /* Yes; don't print it again. */
2639 /* No; print it and reset *FIRST. */
2646 printf_filtered (", ");
2650 fputs_filtered (name, gdb_stdout);
2654 sources_info (char *ignore, int from_tty)
2656 register struct symtab *s;
2657 register struct partial_symtab *ps;
2658 register struct objfile *objfile;
2661 if (!have_full_symbols () && !have_partial_symbols ())
2663 error ("No symbol table is loaded. Use the \"file\" command.");
2666 printf_filtered ("Source files for which symbols have been read in:\n\n");
2669 ALL_SYMTABS (objfile, s)
2671 output_source_filename (s->filename, &first);
2673 printf_filtered ("\n\n");
2675 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2678 ALL_PSYMTABS (objfile, ps)
2682 output_source_filename (ps->filename, &first);
2685 printf_filtered ("\n");
2689 file_matches (char *file, char *files[], int nfiles)
2693 if (file != NULL && nfiles != 0)
2695 for (i = 0; i < nfiles; i++)
2697 if (strcmp (files[i], lbasename (file)) == 0)
2701 else if (nfiles == 0)
2706 /* Free any memory associated with a search. */
2708 free_search_symbols (struct symbol_search *symbols)
2710 struct symbol_search *p;
2711 struct symbol_search *next;
2713 for (p = symbols; p != NULL; p = next)
2721 do_free_search_symbols_cleanup (void *symbols)
2723 free_search_symbols (symbols);
2727 make_cleanup_free_search_symbols (struct symbol_search *symbols)
2729 return make_cleanup (do_free_search_symbols_cleanup, symbols);
2732 /* Helper function for sort_search_symbols and qsort. Can only
2733 sort symbols, not minimal symbols. */
2735 compare_search_syms (const void *sa, const void *sb)
2737 struct symbol_search **sym_a = (struct symbol_search **) sa;
2738 struct symbol_search **sym_b = (struct symbol_search **) sb;
2740 return strcmp (SYMBOL_SOURCE_NAME ((*sym_a)->symbol),
2741 SYMBOL_SOURCE_NAME ((*sym_b)->symbol));
2744 /* Sort the ``nfound'' symbols in the list after prevtail. Leave
2745 prevtail where it is, but update its next pointer to point to
2746 the first of the sorted symbols. */
2747 static struct symbol_search *
2748 sort_search_symbols (struct symbol_search *prevtail, int nfound)
2750 struct symbol_search **symbols, *symp, *old_next;
2753 symbols = (struct symbol_search **) xmalloc (sizeof (struct symbol_search *)
2755 symp = prevtail->next;
2756 for (i = 0; i < nfound; i++)
2761 /* Generally NULL. */
2764 qsort (symbols, nfound, sizeof (struct symbol_search *),
2765 compare_search_syms);
2768 for (i = 0; i < nfound; i++)
2770 symp->next = symbols[i];
2773 symp->next = old_next;
2779 /* Search the symbol table for matches to the regular expression REGEXP,
2780 returning the results in *MATCHES.
2782 Only symbols of KIND are searched:
2783 FUNCTIONS_NAMESPACE - search all functions
2784 TYPES_NAMESPACE - search all type names
2785 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
2786 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
2787 and constants (enums)
2789 free_search_symbols should be called when *MATCHES is no longer needed.
2791 The results are sorted locally; each symtab's global and static blocks are
2792 separately alphabetized.
2795 search_symbols (char *regexp, namespace_enum kind, int nfiles, char *files[],
2796 struct symbol_search **matches)
2798 register struct symtab *s;
2799 register struct partial_symtab *ps;
2800 register struct blockvector *bv;
2801 struct blockvector *prev_bv = 0;
2802 register struct block *b;
2805 register struct symbol *sym;
2806 struct partial_symbol **psym;
2807 struct objfile *objfile;
2808 struct minimal_symbol *msymbol;
2811 static enum minimal_symbol_type types[]
2813 {mst_data, mst_text, mst_abs, mst_unknown};
2814 static enum minimal_symbol_type types2[]
2816 {mst_bss, mst_file_text, mst_abs, mst_unknown};
2817 static enum minimal_symbol_type types3[]
2819 {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
2820 static enum minimal_symbol_type types4[]
2822 {mst_file_bss, mst_text, mst_abs, mst_unknown};
2823 enum minimal_symbol_type ourtype;
2824 enum minimal_symbol_type ourtype2;
2825 enum minimal_symbol_type ourtype3;
2826 enum minimal_symbol_type ourtype4;
2827 struct symbol_search *sr;
2828 struct symbol_search *psr;
2829 struct symbol_search *tail;
2830 struct cleanup *old_chain = NULL;
2832 if (kind < VARIABLES_NAMESPACE)
2833 error ("must search on specific namespace");
2835 ourtype = types[(int) (kind - VARIABLES_NAMESPACE)];
2836 ourtype2 = types2[(int) (kind - VARIABLES_NAMESPACE)];
2837 ourtype3 = types3[(int) (kind - VARIABLES_NAMESPACE)];
2838 ourtype4 = types4[(int) (kind - VARIABLES_NAMESPACE)];
2840 sr = *matches = NULL;
2845 /* Make sure spacing is right for C++ operators.
2846 This is just a courtesy to make the matching less sensitive
2847 to how many spaces the user leaves between 'operator'
2848 and <TYPENAME> or <OPERATOR>. */
2850 char *opname = operator_chars (regexp, &opend);
2853 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2854 if (isalpha (*opname) || *opname == '_' || *opname == '$')
2856 /* There should 1 space between 'operator' and 'TYPENAME'. */
2857 if (opname[-1] != ' ' || opname[-2] == ' ')
2862 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2863 if (opname[-1] == ' ')
2866 /* If wrong number of spaces, fix it. */
2869 char *tmp = (char *) alloca (8 + fix + strlen (opname) + 1);
2870 sprintf (tmp, "operator%.*s%s", fix, " ", opname);
2875 if (0 != (val = re_comp (regexp)))
2876 error ("Invalid regexp (%s): %s", val, regexp);
2879 /* Search through the partial symtabs *first* for all symbols
2880 matching the regexp. That way we don't have to reproduce all of
2881 the machinery below. */
2883 ALL_PSYMTABS (objfile, ps)
2885 struct partial_symbol **bound, **gbound, **sbound;
2891 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2892 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2895 /* Go through all of the symbols stored in a partial
2896 symtab in one loop. */
2897 psym = objfile->global_psymbols.list + ps->globals_offset;
2902 if (bound == gbound && ps->n_static_syms != 0)
2904 psym = objfile->static_psymbols.list + ps->statics_offset;
2915 /* If it would match (logic taken from loop below)
2916 load the file and go on to the next one */
2917 if (file_matches (ps->filename, files, nfiles)
2918 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym))
2919 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
2920 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2921 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2922 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2923 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (*psym) == LOC_BLOCK))))
2925 PSYMTAB_TO_SYMTAB (ps);
2933 /* Here, we search through the minimal symbol tables for functions
2934 and variables that match, and force their symbols to be read.
2935 This is in particular necessary for demangled variable names,
2936 which are no longer put into the partial symbol tables.
2937 The symbol will then be found during the scan of symtabs below.
2939 For functions, find_pc_symtab should succeed if we have debug info
2940 for the function, for variables we have to call lookup_symbol
2941 to determine if the variable has debug info.
2942 If the lookup fails, set found_misc so that we will rescan to print
2943 any matching symbols without debug info.
2946 if (nfiles == 0 && (kind == VARIABLES_NAMESPACE || kind == FUNCTIONS_NAMESPACE))
2948 ALL_MSYMBOLS (objfile, msymbol)
2950 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2951 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2952 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2953 MSYMBOL_TYPE (msymbol) == ourtype4)
2955 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
2957 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
2959 if (kind == FUNCTIONS_NAMESPACE)
2967 if (SYMBOL_DEMANGLED_NAME (msymbol) != NULL)
2969 = lookup_symbol_aux_minsyms (SYMBOL_DEMANGLED_NAME
2971 SYMBOL_NAME (msymbol),
2976 = lookup_symbol_aux_minsyms (SYMBOL_NAME (msymbol),
2990 ALL_SYMTABS (objfile, s)
2992 bv = BLOCKVECTOR (s);
2993 /* Often many files share a blockvector.
2994 Scan each blockvector only once so that
2995 we don't get every symbol many times.
2996 It happens that the first symtab in the list
2997 for any given blockvector is the main file. */
2999 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
3001 struct symbol_search *prevtail = tail;
3003 b = BLOCKVECTOR_BLOCK (bv, i);
3004 ALL_BLOCK_SYMBOLS (b, j, sym)
3007 if (file_matches (s->filename, files, nfiles)
3008 && ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
3009 && ((kind == VARIABLES_NAMESPACE && SYMBOL_CLASS (sym) != LOC_TYPEDEF
3010 && SYMBOL_CLASS (sym) != LOC_BLOCK
3011 && SYMBOL_CLASS (sym) != LOC_CONST)
3012 || (kind == FUNCTIONS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK)
3013 || (kind == TYPES_NAMESPACE && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3014 || (kind == METHODS_NAMESPACE && SYMBOL_CLASS (sym) == LOC_BLOCK))))
3017 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3021 psr->msymbol = NULL;
3033 if (prevtail == NULL)
3035 struct symbol_search dummy;
3038 tail = sort_search_symbols (&dummy, nfound);
3041 old_chain = make_cleanup_free_search_symbols (sr);
3044 tail = sort_search_symbols (prevtail, nfound);
3050 /* If there are no eyes, avoid all contact. I mean, if there are
3051 no debug symbols, then print directly from the msymbol_vector. */
3053 if (found_misc || kind != FUNCTIONS_NAMESPACE)
3055 ALL_MSYMBOLS (objfile, msymbol)
3057 if (MSYMBOL_TYPE (msymbol) == ourtype ||
3058 MSYMBOL_TYPE (msymbol) == ourtype2 ||
3059 MSYMBOL_TYPE (msymbol) == ourtype3 ||
3060 MSYMBOL_TYPE (msymbol) == ourtype4)
3062 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
3064 /* Functions: Look up by address. */
3065 if (kind != FUNCTIONS_NAMESPACE ||
3066 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
3068 /* Variables/Absolutes: Look up by name */
3069 if (lookup_symbol (SYMBOL_NAME (msymbol),
3070 (struct block *) NULL, VAR_NAMESPACE,
3071 0, (struct symtab **) NULL) == NULL)
3074 psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
3076 psr->msymbol = msymbol;
3083 old_chain = make_cleanup_free_search_symbols (sr);
3097 discard_cleanups (old_chain);
3100 /* Helper function for symtab_symbol_info, this function uses
3101 the data returned from search_symbols() to print information
3102 regarding the match to gdb_stdout.
3105 print_symbol_info (namespace_enum kind, struct symtab *s, struct symbol *sym,
3106 int block, char *last)
3108 if (last == NULL || strcmp (last, s->filename) != 0)
3110 fputs_filtered ("\nFile ", gdb_stdout);
3111 fputs_filtered (s->filename, gdb_stdout);
3112 fputs_filtered (":\n", gdb_stdout);
3115 if (kind != TYPES_NAMESPACE && block == STATIC_BLOCK)
3116 printf_filtered ("static ");
3118 /* Typedef that is not a C++ class */
3119 if (kind == TYPES_NAMESPACE
3120 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
3121 typedef_print (SYMBOL_TYPE (sym), sym, gdb_stdout);
3122 /* variable, func, or typedef-that-is-c++-class */
3123 else if (kind < TYPES_NAMESPACE ||
3124 (kind == TYPES_NAMESPACE &&
3125 SYMBOL_NAMESPACE (sym) == STRUCT_NAMESPACE))
3127 type_print (SYMBOL_TYPE (sym),
3128 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
3129 ? "" : SYMBOL_SOURCE_NAME (sym)),
3132 printf_filtered (";\n");
3136 /* This help function for symtab_symbol_info() prints information
3137 for non-debugging symbols to gdb_stdout.
3140 print_msymbol_info (struct minimal_symbol *msymbol)
3144 if (TARGET_ADDR_BIT <= 32)
3145 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol)
3146 & (CORE_ADDR) 0xffffffff,
3149 tmp = local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol),
3151 printf_filtered ("%s %s\n",
3152 tmp, SYMBOL_SOURCE_NAME (msymbol));
3155 /* This is the guts of the commands "info functions", "info types", and
3156 "info variables". It calls search_symbols to find all matches and then
3157 print_[m]symbol_info to print out some useful information about the
3161 symtab_symbol_info (char *regexp, namespace_enum kind, int from_tty)
3163 static char *classnames[]
3165 {"variable", "function", "type", "method"};
3166 struct symbol_search *symbols;
3167 struct symbol_search *p;
3168 struct cleanup *old_chain;
3169 char *last_filename = NULL;
3172 /* must make sure that if we're interrupted, symbols gets freed */
3173 search_symbols (regexp, kind, 0, (char **) NULL, &symbols);
3174 old_chain = make_cleanup_free_search_symbols (symbols);
3176 printf_filtered (regexp
3177 ? "All %ss matching regular expression \"%s\":\n"
3178 : "All defined %ss:\n",
3179 classnames[(int) (kind - VARIABLES_NAMESPACE)], regexp);
3181 for (p = symbols; p != NULL; p = p->next)
3185 if (p->msymbol != NULL)
3189 printf_filtered ("\nNon-debugging symbols:\n");
3192 print_msymbol_info (p->msymbol);
3196 print_symbol_info (kind,
3201 last_filename = p->symtab->filename;
3205 do_cleanups (old_chain);
3209 variables_info (char *regexp, int from_tty)
3211 symtab_symbol_info (regexp, VARIABLES_NAMESPACE, from_tty);
3215 functions_info (char *regexp, int from_tty)
3217 symtab_symbol_info (regexp, FUNCTIONS_NAMESPACE, from_tty);
3222 types_info (char *regexp, int from_tty)
3224 symtab_symbol_info (regexp, TYPES_NAMESPACE, from_tty);
3227 /* Breakpoint all functions matching regular expression. */
3230 rbreak_command_wrapper (char *regexp, int from_tty)
3232 rbreak_command (regexp, from_tty);
3236 rbreak_command (char *regexp, int from_tty)
3238 struct symbol_search *ss;
3239 struct symbol_search *p;
3240 struct cleanup *old_chain;
3242 search_symbols (regexp, FUNCTIONS_NAMESPACE, 0, (char **) NULL, &ss);
3243 old_chain = make_cleanup_free_search_symbols (ss);
3245 for (p = ss; p != NULL; p = p->next)
3247 if (p->msymbol == NULL)
3249 char *string = (char *) alloca (strlen (p->symtab->filename)
3250 + strlen (SYMBOL_NAME (p->symbol))
3252 strcpy (string, p->symtab->filename);
3253 strcat (string, ":'");
3254 strcat (string, SYMBOL_NAME (p->symbol));
3255 strcat (string, "'");
3256 break_command (string, from_tty);
3257 print_symbol_info (FUNCTIONS_NAMESPACE,
3261 p->symtab->filename);
3265 break_command (SYMBOL_NAME (p->msymbol), from_tty);
3266 printf_filtered ("<function, no debug info> %s;\n",
3267 SYMBOL_SOURCE_NAME (p->msymbol));
3271 do_cleanups (old_chain);
3275 /* Helper routine for make_symbol_completion_list. */
3277 static int return_val_size;
3278 static int return_val_index;
3279 static char **return_val;
3281 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3283 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
3284 /* Put only the mangled name on the list. */ \
3285 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
3286 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
3287 completion_list_add_name \
3288 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
3290 completion_list_add_name \
3291 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
3294 /* Test to see if the symbol specified by SYMNAME (which is already
3295 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3296 characters. If so, add it to the current completion list. */
3299 completion_list_add_name (char *symname, char *sym_text, int sym_text_len,
3300 char *text, char *word)
3305 /* clip symbols that cannot match */
3307 if (strncmp (symname, sym_text, sym_text_len) != 0)
3312 /* We have a match for a completion, so add SYMNAME to the current list
3313 of matches. Note that the name is moved to freshly malloc'd space. */
3317 if (word == sym_text)
3319 new = xmalloc (strlen (symname) + 5);
3320 strcpy (new, symname);
3322 else if (word > sym_text)
3324 /* Return some portion of symname. */
3325 new = xmalloc (strlen (symname) + 5);
3326 strcpy (new, symname + (word - sym_text));
3330 /* Return some of SYM_TEXT plus symname. */
3331 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
3332 strncpy (new, word, sym_text - word);
3333 new[sym_text - word] = '\0';
3334 strcat (new, symname);
3337 if (return_val_index + 3 > return_val_size)
3339 newsize = (return_val_size *= 2) * sizeof (char *);
3340 return_val = (char **) xrealloc ((char *) return_val, newsize);
3342 return_val[return_val_index++] = new;
3343 return_val[return_val_index] = NULL;
3347 /* Return a NULL terminated array of all symbols (regardless of class)
3348 which begin by matching TEXT. If the answer is no symbols, then
3349 the return value is an array which contains only a NULL pointer.
3351 Problem: All of the symbols have to be copied because readline frees them.
3352 I'm not going to worry about this; hopefully there won't be that many. */
3355 make_symbol_completion_list (char *text, char *word)
3357 register struct symbol *sym;
3358 register struct symtab *s;
3359 register struct partial_symtab *ps;
3360 register struct minimal_symbol *msymbol;
3361 register struct objfile *objfile;
3362 register struct block *b, *surrounding_static_block = 0;
3364 struct partial_symbol **psym;
3365 /* The symbol we are completing on. Points in same buffer as text. */
3367 /* Length of sym_text. */
3370 /* Now look for the symbol we are supposed to complete on.
3371 FIXME: This should be language-specific. */
3375 char *quote_pos = NULL;
3377 /* First see if this is a quoted string. */
3379 for (p = text; *p != '\0'; ++p)
3381 if (quote_found != '\0')
3383 if (*p == quote_found)
3384 /* Found close quote. */
3386 else if (*p == '\\' && p[1] == quote_found)
3387 /* A backslash followed by the quote character
3388 doesn't end the string. */
3391 else if (*p == '\'' || *p == '"')
3397 if (quote_found == '\'')
3398 /* A string within single quotes can be a symbol, so complete on it. */
3399 sym_text = quote_pos + 1;
3400 else if (quote_found == '"')
3401 /* A double-quoted string is never a symbol, nor does it make sense
3402 to complete it any other way. */
3404 return_val = (char **) xmalloc (sizeof (char *));
3405 return_val[0] = NULL;
3410 /* It is not a quoted string. Break it based on the characters
3411 which are in symbols. */
3414 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3423 sym_text_len = strlen (sym_text);
3425 return_val_size = 100;
3426 return_val_index = 0;
3427 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3428 return_val[0] = NULL;
3430 /* Look through the partial symtabs for all symbols which begin
3431 by matching SYM_TEXT. Add each one that you find to the list. */
3433 ALL_PSYMTABS (objfile, ps)
3435 /* If the psymtab's been read in we'll get it when we search
3436 through the blockvector. */
3440 for (psym = objfile->global_psymbols.list + ps->globals_offset;
3441 psym < (objfile->global_psymbols.list + ps->globals_offset
3442 + ps->n_global_syms);
3445 /* If interrupted, then quit. */
3447 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3450 for (psym = objfile->static_psymbols.list + ps->statics_offset;
3451 psym < (objfile->static_psymbols.list + ps->statics_offset
3452 + ps->n_static_syms);
3456 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3460 /* At this point scan through the misc symbol vectors and add each
3461 symbol you find to the list. Eventually we want to ignore
3462 anything that isn't a text symbol (everything else will be
3463 handled by the psymtab code above). */
3465 ALL_MSYMBOLS (objfile, msymbol)
3468 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3471 /* Search upwards from currently selected frame (so that we can
3472 complete on local vars. */
3474 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
3476 if (!BLOCK_SUPERBLOCK (b))
3478 surrounding_static_block = b; /* For elmin of dups */
3481 /* Also catch fields of types defined in this places which match our
3482 text string. Only complete on types visible from current context. */
3484 ALL_BLOCK_SYMBOLS (b, i, sym)
3486 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3487 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3489 struct type *t = SYMBOL_TYPE (sym);
3490 enum type_code c = TYPE_CODE (t);
3492 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
3494 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
3496 if (TYPE_FIELD_NAME (t, j))
3498 completion_list_add_name (TYPE_FIELD_NAME (t, j),
3499 sym_text, sym_text_len, text, word);
3507 /* Go through the symtabs and check the externs and statics for
3508 symbols which match. */
3510 ALL_SYMTABS (objfile, s)
3513 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3514 ALL_BLOCK_SYMBOLS (b, i, sym)
3516 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3520 ALL_SYMTABS (objfile, s)
3523 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3524 /* Don't do this block twice. */
3525 if (b == surrounding_static_block)
3527 ALL_BLOCK_SYMBOLS (b, i, sym)
3529 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3533 return (return_val);
3536 /* Like make_symbol_completion_list, but returns a list of symbols
3537 defined in a source file FILE. */
3540 make_file_symbol_completion_list (char *text, char *word, char *srcfile)
3542 register struct symbol *sym;
3543 register struct symtab *s;
3544 register struct block *b;
3546 /* The symbol we are completing on. Points in same buffer as text. */
3548 /* Length of sym_text. */
3551 /* Now look for the symbol we are supposed to complete on.
3552 FIXME: This should be language-specific. */
3556 char *quote_pos = NULL;
3558 /* First see if this is a quoted string. */
3560 for (p = text; *p != '\0'; ++p)
3562 if (quote_found != '\0')
3564 if (*p == quote_found)
3565 /* Found close quote. */
3567 else if (*p == '\\' && p[1] == quote_found)
3568 /* A backslash followed by the quote character
3569 doesn't end the string. */
3572 else if (*p == '\'' || *p == '"')
3578 if (quote_found == '\'')
3579 /* A string within single quotes can be a symbol, so complete on it. */
3580 sym_text = quote_pos + 1;
3581 else if (quote_found == '"')
3582 /* A double-quoted string is never a symbol, nor does it make sense
3583 to complete it any other way. */
3585 return_val = (char **) xmalloc (sizeof (char *));
3586 return_val[0] = NULL;
3591 /* It is not a quoted string. Break it based on the characters
3592 which are in symbols. */
3595 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3604 sym_text_len = strlen (sym_text);
3606 return_val_size = 10;
3607 return_val_index = 0;
3608 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3609 return_val[0] = NULL;
3611 /* Find the symtab for SRCFILE (this loads it if it was not yet read
3613 s = lookup_symtab (srcfile);
3616 /* Maybe they typed the file with leading directories, while the
3617 symbol tables record only its basename. */
3618 const char *tail = lbasename (srcfile);
3621 s = lookup_symtab (tail);
3624 /* If we have no symtab for that file, return an empty list. */
3626 return (return_val);
3628 /* Go through this symtab and check the externs and statics for
3629 symbols which match. */
3631 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3632 ALL_BLOCK_SYMBOLS (b, i, sym)
3634 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3637 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3638 ALL_BLOCK_SYMBOLS (b, i, sym)
3640 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3643 return (return_val);
3646 /* A helper function for make_source_files_completion_list. It adds
3647 another file name to a list of possible completions, growing the
3648 list as necessary. */
3651 add_filename_to_list (const char *fname, char *text, char *word,
3652 char ***list, int *list_used, int *list_alloced)
3655 size_t fnlen = strlen (fname);
3657 if (*list_used + 1 >= *list_alloced)
3660 *list = (char **) xrealloc ((char *) *list,
3661 *list_alloced * sizeof (char *));
3666 /* Return exactly fname. */
3667 new = xmalloc (fnlen + 5);
3668 strcpy (new, fname);
3670 else if (word > text)
3672 /* Return some portion of fname. */
3673 new = xmalloc (fnlen + 5);
3674 strcpy (new, fname + (word - text));
3678 /* Return some of TEXT plus fname. */
3679 new = xmalloc (fnlen + (text - word) + 5);
3680 strncpy (new, word, text - word);
3681 new[text - word] = '\0';
3682 strcat (new, fname);
3684 (*list)[*list_used] = new;
3685 (*list)[++*list_used] = NULL;
3689 not_interesting_fname (const char *fname)
3691 static const char *illegal_aliens[] = {
3692 "_globals_", /* inserted by coff_symtab_read */
3697 for (i = 0; illegal_aliens[i]; i++)
3699 if (strcmp (fname, illegal_aliens[i]) == 0)
3705 /* Return a NULL terminated array of all source files whose names
3706 begin with matching TEXT. The file names are looked up in the
3707 symbol tables of this program. If the answer is no matchess, then
3708 the return value is an array which contains only a NULL pointer. */
3711 make_source_files_completion_list (char *text, char *word)
3713 register struct symtab *s;
3714 register struct partial_symtab *ps;
3715 register struct objfile *objfile;
3717 int list_alloced = 1;
3719 size_t text_len = strlen (text);
3720 char **list = (char **) xmalloc (list_alloced * sizeof (char *));
3721 const char *base_name;
3725 if (!have_full_symbols () && !have_partial_symbols ())
3728 ALL_SYMTABS (objfile, s)
3730 if (not_interesting_fname (s->filename))
3732 if (!filename_seen (s->filename, 1, &first)
3733 #if HAVE_DOS_BASED_FILE_SYSTEM
3734 && strncasecmp (s->filename, text, text_len) == 0
3736 && strncmp (s->filename, text, text_len) == 0
3740 /* This file matches for a completion; add it to the current
3742 add_filename_to_list (s->filename, text, word,
3743 &list, &list_used, &list_alloced);
3747 /* NOTE: We allow the user to type a base name when the
3748 debug info records leading directories, but not the other
3749 way around. This is what subroutines of breakpoint
3750 command do when they parse file names. */
3751 base_name = lbasename (s->filename);
3752 if (base_name != s->filename
3753 && !filename_seen (base_name, 1, &first)
3754 #if HAVE_DOS_BASED_FILE_SYSTEM
3755 && strncasecmp (base_name, text, text_len) == 0
3757 && strncmp (base_name, text, text_len) == 0
3760 add_filename_to_list (base_name, text, word,
3761 &list, &list_used, &list_alloced);
3765 ALL_PSYMTABS (objfile, ps)
3767 if (not_interesting_fname (ps->filename))
3771 if (!filename_seen (ps->filename, 1, &first)
3772 #if HAVE_DOS_BASED_FILE_SYSTEM
3773 && strncasecmp (ps->filename, text, text_len) == 0
3775 && strncmp (ps->filename, text, text_len) == 0
3779 /* This file matches for a completion; add it to the
3780 current list of matches. */
3781 add_filename_to_list (ps->filename, text, word,
3782 &list, &list_used, &list_alloced);
3787 base_name = lbasename (ps->filename);
3788 if (base_name != ps->filename
3789 && !filename_seen (base_name, 1, &first)
3790 #if HAVE_DOS_BASED_FILE_SYSTEM
3791 && strncasecmp (base_name, text, text_len) == 0
3793 && strncmp (base_name, text, text_len) == 0
3796 add_filename_to_list (base_name, text, word,
3797 &list, &list_used, &list_alloced);
3805 /* Determine if PC is in the prologue of a function. The prologue is the area
3806 between the first instruction of a function, and the first executable line.
3807 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3809 If non-zero, func_start is where we think the prologue starts, possibly
3810 by previous examination of symbol table information.
3814 in_prologue (CORE_ADDR pc, CORE_ADDR func_start)
3816 struct symtab_and_line sal;
3817 CORE_ADDR func_addr, func_end;
3819 /* We have several sources of information we can consult to figure
3821 - Compilers usually emit line number info that marks the prologue
3822 as its own "source line". So the ending address of that "line"
3823 is the end of the prologue. If available, this is the most
3825 - The minimal symbols and partial symbols, which can usually tell
3826 us the starting and ending addresses of a function.
3827 - If we know the function's start address, we can call the
3828 architecture-defined SKIP_PROLOGUE function to analyze the
3829 instruction stream and guess where the prologue ends.
3830 - Our `func_start' argument; if non-zero, this is the caller's
3831 best guess as to the function's entry point. At the time of
3832 this writing, handle_inferior_event doesn't get this right, so
3833 it should be our last resort. */
3835 /* Consult the partial symbol table, to find which function
3837 if (! find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3839 CORE_ADDR prologue_end;
3841 /* We don't even have minsym information, so fall back to using
3842 func_start, if given. */
3844 return 1; /* We *might* be in a prologue. */
3846 prologue_end = SKIP_PROLOGUE (func_start);
3848 return func_start <= pc && pc < prologue_end;
3851 /* If we have line number information for the function, that's
3852 usually pretty reliable. */
3853 sal = find_pc_line (func_addr, 0);
3855 /* Now sal describes the source line at the function's entry point,
3856 which (by convention) is the prologue. The end of that "line",
3857 sal.end, is the end of the prologue.
3859 Note that, for functions whose source code is all on a single
3860 line, the line number information doesn't always end up this way.
3861 So we must verify that our purported end-of-prologue address is
3862 *within* the function, not at its start or end. */
3864 || sal.end <= func_addr
3865 || func_end <= sal.end)
3867 /* We don't have any good line number info, so use the minsym
3868 information, together with the architecture-specific prologue
3870 CORE_ADDR prologue_end = SKIP_PROLOGUE (func_addr);
3872 return func_addr <= pc && pc < prologue_end;
3875 /* We have line number info, and it looks good. */
3876 return func_addr <= pc && pc < sal.end;
3880 /* Begin overload resolution functions */
3883 remove_params (const char *demangled_name)
3889 if (demangled_name == NULL)
3892 /* First find the end of the arg list. */
3893 argp = strrchr (demangled_name, ')');
3897 /* Back up to the beginning. */
3900 while (argp-- > demangled_name)
3904 else if (*argp == '(')
3913 internal_error (__FILE__, __LINE__,
3914 "bad demangled name %s\n", demangled_name);
3915 while (argp[-1] == ' ' && argp > demangled_name)
3918 new_name = xmalloc (argp - demangled_name + 1);
3919 memcpy (new_name, demangled_name, argp - demangled_name);
3920 new_name[argp - demangled_name] = '\0';
3924 /* Helper routine for make_symbol_completion_list. */
3926 static int sym_return_val_size;
3927 static int sym_return_val_index;
3928 static struct symbol **sym_return_val;
3930 /* Test to see if the symbol specified by SYMNAME (which is already
3931 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3932 characters. If so, add it to the current completion list. */
3935 overload_list_add_symbol (struct symbol *sym, char *oload_name)
3941 /* If there is no type information, we can't do anything, so skip */
3942 if (SYMBOL_TYPE (sym) == NULL)
3945 /* skip any symbols that we've already considered. */
3946 for (i = 0; i < sym_return_val_index; ++i)
3947 if (!strcmp (SYMBOL_NAME (sym), SYMBOL_NAME (sym_return_val[i])))
3950 /* Get the demangled name without parameters */
3951 sym_name = remove_params (SYMBOL_DEMANGLED_NAME (sym));
3955 /* skip symbols that cannot match */
3956 if (strcmp (sym_name, oload_name) != 0)
3964 /* We have a match for an overload instance, so add SYM to the current list
3965 * of overload instances */
3966 if (sym_return_val_index + 3 > sym_return_val_size)
3968 newsize = (sym_return_val_size *= 2) * sizeof (struct symbol *);
3969 sym_return_val = (struct symbol **) xrealloc ((char *) sym_return_val, newsize);
3971 sym_return_val[sym_return_val_index++] = sym;
3972 sym_return_val[sym_return_val_index] = NULL;
3975 /* Return a null-terminated list of pointers to function symbols that
3976 * match name of the supplied symbol FSYM.
3977 * This is used in finding all overloaded instances of a function name.
3978 * This has been modified from make_symbol_completion_list. */
3982 make_symbol_overload_list (struct symbol *fsym)
3984 register struct symbol *sym;
3985 register struct symtab *s;
3986 register struct partial_symtab *ps;
3987 register struct objfile *objfile;
3988 register struct block *b, *surrounding_static_block = 0;
3990 /* The name we are completing on. */
3991 char *oload_name = NULL;
3992 /* Length of name. */
3993 int oload_name_len = 0;
3995 /* Look for the symbol we are supposed to complete on. */
3997 oload_name = remove_params (SYMBOL_DEMANGLED_NAME (fsym));
4000 sym_return_val_size = 1;
4001 sym_return_val = (struct symbol **) xmalloc (2 * sizeof (struct symbol *));
4002 sym_return_val[0] = fsym;
4003 sym_return_val[1] = NULL;
4005 return sym_return_val;
4007 oload_name_len = strlen (oload_name);
4009 sym_return_val_size = 100;
4010 sym_return_val_index = 0;
4011 sym_return_val = (struct symbol **) xmalloc ((sym_return_val_size + 1) * sizeof (struct symbol *));
4012 sym_return_val[0] = NULL;
4014 /* Look through the partial symtabs for all symbols which begin
4015 by matching OLOAD_NAME. Make sure we read that symbol table in. */
4017 ALL_PSYMTABS (objfile, ps)
4019 struct partial_symbol **psym;
4021 /* If the psymtab's been read in we'll get it when we search
4022 through the blockvector. */
4026 for (psym = objfile->global_psymbols.list + ps->globals_offset;
4027 psym < (objfile->global_psymbols.list + ps->globals_offset
4028 + ps->n_global_syms);
4031 /* If interrupted, then quit. */
4033 /* This will cause the symbol table to be read if it has not yet been */
4034 s = PSYMTAB_TO_SYMTAB (ps);
4037 for (psym = objfile->static_psymbols.list + ps->statics_offset;
4038 psym < (objfile->static_psymbols.list + ps->statics_offset
4039 + ps->n_static_syms);
4043 /* This will cause the symbol table to be read if it has not yet been */
4044 s = PSYMTAB_TO_SYMTAB (ps);
4048 /* Search upwards from currently selected frame (so that we can
4049 complete on local vars. */
4051 for (b = get_selected_block (0); b != NULL; b = BLOCK_SUPERBLOCK (b))
4053 if (!BLOCK_SUPERBLOCK (b))
4055 surrounding_static_block = b; /* For elimination of dups */
4058 /* Also catch fields of types defined in this places which match our
4059 text string. Only complete on types visible from current context. */
4061 ALL_BLOCK_SYMBOLS (b, i, sym)
4063 overload_list_add_symbol (sym, oload_name);
4067 /* Go through the symtabs and check the externs and statics for
4068 symbols which match. */
4070 ALL_SYMTABS (objfile, s)
4073 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
4074 ALL_BLOCK_SYMBOLS (b, i, sym)
4076 overload_list_add_symbol (sym, oload_name);
4080 ALL_SYMTABS (objfile, s)
4083 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
4084 /* Don't do this block twice. */
4085 if (b == surrounding_static_block)
4087 ALL_BLOCK_SYMBOLS (b, i, sym)
4089 overload_list_add_symbol (sym, oload_name);
4095 return (sym_return_val);
4098 /* End of overload resolution functions */
4100 struct symtabs_and_lines
4101 decode_line_spec (char *string, int funfirstline)
4103 struct symtabs_and_lines sals;
4104 struct symtab_and_line cursal;
4107 error ("Empty line specification.");
4109 /* We use whatever is set as the current source line. We do not try
4110 and get a default or it will recursively call us! */
4111 cursal = get_current_source_symtab_and_line ();
4113 sals = decode_line_1 (&string, funfirstline,
4114 cursal.symtab, cursal.line,
4118 error ("Junk at end of line specification: %s", string);
4123 static char *name_of_main;
4126 set_main_name (const char *name)
4128 if (name_of_main != NULL)
4130 xfree (name_of_main);
4131 name_of_main = NULL;
4135 name_of_main = xstrdup (name);
4142 if (name_of_main != NULL)
4143 return name_of_main;
4150 _initialize_symtab (void)
4152 add_info ("variables", variables_info,
4153 "All global and static variable names, or those matching REGEXP.");
4155 add_com ("whereis", class_info, variables_info,
4156 "All global and static variable names, or those matching REGEXP.");
4158 add_info ("functions", functions_info,
4159 "All function names, or those matching REGEXP.");
4162 /* FIXME: This command has at least the following problems:
4163 1. It prints builtin types (in a very strange and confusing fashion).
4164 2. It doesn't print right, e.g. with
4165 typedef struct foo *FOO
4166 type_print prints "FOO" when we want to make it (in this situation)
4167 print "struct foo *".
4168 I also think "ptype" or "whatis" is more likely to be useful (but if
4169 there is much disagreement "info types" can be fixed). */
4170 add_info ("types", types_info,
4171 "All type names, or those matching REGEXP.");
4173 add_info ("sources", sources_info,
4174 "Source files in the program.");
4176 add_com ("rbreak", class_breakpoint, rbreak_command,
4177 "Set a breakpoint for all functions matching REGEXP.");
4181 add_com ("lf", class_info, sources_info, "Source files in the program");
4182 add_com ("lg", class_info, variables_info,
4183 "All global and static variable names, or those matching REGEXP.");
4186 /* Initialize the one built-in type that isn't language dependent... */
4187 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
4188 "<unknown type>", (struct objfile *) NULL);