1 /* Symbol table lookup for the GNU debugger, GDB.
3 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
4 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
34 #include "call-cmds.h"
35 #include "gdb_regex.h"
36 #include "expression.h"
42 #include "filenames.h" /* for FILENAME_CMP */
44 #include "gdb_obstack.h"
46 #include <sys/types.h>
48 #include "gdb_string.h"
53 /* Prototypes for local functions */
55 static void completion_list_add_name (char *, char *, int, char *, char *);
57 static void rbreak_command (char *, int);
59 static void types_info (char *, int);
61 static void functions_info (char *, int);
63 static void variables_info (char *, int);
65 static void sources_info (char *, int);
67 static void output_source_filename (char *, int *);
69 static int find_line_common (struct linetable
*, int, int *);
71 /* This one is used by linespec.c */
73 char *operator_chars (char *p
, char **end
);
75 static struct partial_symbol
*lookup_partial_symbol (struct partial_symtab
*,
79 static struct symbol
*lookup_symbol_aux (const char *name
,
80 const char *mangled_name
,
81 const struct block
*block
,
82 const namespace_enum
namespace,
83 int *is_a_field_of_this
,
84 struct symtab
**symtab
);
86 static struct symbol
*lookup_symbol_aux_local (const char *name
,
87 const char *mangled_name
,
88 const struct block
*block
,
89 const namespace_enum
namespace,
90 struct symtab
**symtab
);
93 struct symbol
*lookup_symbol_aux_block (const char *name
,
94 const char *mangled_name
,
95 const struct block
*block
,
96 const namespace_enum
namespace,
97 struct symtab
**symtab
);
100 struct symbol
*lookup_symbol_aux_symtabs (int block_index
,
102 const char *mangled_name
,
103 const namespace_enum
namespace,
104 struct symtab
**symtab
);
107 struct symbol
*lookup_symbol_aux_psymtabs (int block_index
,
109 const char *mangled_name
,
110 const namespace_enum
namespace,
111 struct symtab
**symtab
);
114 struct symbol
*lookup_symbol_aux_minsyms (const char *name
,
115 const char *mangled_name
,
116 const namespace_enum
namespace,
117 int *is_a_field_of_this
,
118 struct symtab
**symtab
,
121 static struct symbol
*find_active_alias (struct symbol
*sym
, CORE_ADDR addr
);
123 /* This flag is used in hppa-tdep.c, and set in hp-symtab-read.c */
124 /* Signals the presence of objects compiled by HP compilers */
125 int hp_som_som_object_present
= 0;
127 static void fixup_section (struct general_symbol_info
*, struct objfile
*);
129 static int file_matches (char *, char **, int);
131 static void print_symbol_info (namespace_enum
,
132 struct symtab
*, struct symbol
*, int, char *);
134 static void print_msymbol_info (struct minimal_symbol
*);
136 static void symtab_symbol_info (char *, namespace_enum
, int);
138 static void overload_list_add_symbol (struct symbol
*sym
, char *oload_name
);
140 void _initialize_symtab (void);
144 /* The single non-language-specific builtin type */
145 struct type
*builtin_type_error
;
147 /* Block in which the most recently searched-for symbol was found.
148 Might be better to make this a parameter to lookup_symbol and
151 const struct block
*block_found
;
153 /* Check for a symtab of a specific name; first in symtabs, then in
154 psymtabs. *If* there is no '/' in the name, a match after a '/'
155 in the symtab filename will also work. */
158 lookup_symtab (const char *name
)
160 register struct symtab
*s
;
161 register struct partial_symtab
*ps
;
162 register struct objfile
*objfile
;
163 char *real_path
= NULL
;
164 char *full_path
= NULL
;
166 /* Here we are interested in canonicalizing an absolute path, not
167 absolutizing a relative path. */
168 if (IS_ABSOLUTE_PATH (name
))
170 full_path
= xfullpath (name
);
171 make_cleanup (xfree
, full_path
);
172 real_path
= gdb_realpath (name
);
173 make_cleanup (xfree
, real_path
);
178 /* First, search for an exact match */
180 ALL_SYMTABS (objfile
, s
)
182 if (FILENAME_CMP (name
, s
->filename
) == 0)
187 /* If the user gave us an absolute path, try to find the file in
188 this symtab and use its absolute path. */
190 if (full_path
!= NULL
)
192 const char *fp
= symtab_to_filename (s
);
193 if (FILENAME_CMP (full_path
, fp
) == 0)
199 if (real_path
!= NULL
)
201 char *rp
= gdb_realpath (symtab_to_filename (s
));
202 make_cleanup (xfree
, rp
);
203 if (FILENAME_CMP (real_path
, rp
) == 0)
210 /* Now, search for a matching tail (only if name doesn't have any dirs) */
212 if (lbasename (name
) == name
)
213 ALL_SYMTABS (objfile
, s
)
215 if (FILENAME_CMP (lbasename (s
->filename
), name
) == 0)
219 /* Same search rules as above apply here, but now we look thru the
222 ps
= lookup_partial_symtab (name
);
227 error ("Internal: readin %s pst for `%s' found when no symtab found.",
230 s
= PSYMTAB_TO_SYMTAB (ps
);
235 /* At this point, we have located the psymtab for this file, but
236 the conversion to a symtab has failed. This usually happens
237 when we are looking up an include file. In this case,
238 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
239 been created. So, we need to run through the symtabs again in
240 order to find the file.
241 XXX - This is a crock, and should be fixed inside of the the
242 symbol parsing routines. */
246 /* Lookup the partial symbol table of a source file named NAME.
247 *If* there is no '/' in the name, a match after a '/'
248 in the psymtab filename will also work. */
250 struct partial_symtab
*
251 lookup_partial_symtab (const char *name
)
253 register struct partial_symtab
*pst
;
254 register struct objfile
*objfile
;
255 char *full_path
= NULL
;
256 char *real_path
= NULL
;
258 /* Here we are interested in canonicalizing an absolute path, not
259 absolutizing a relative path. */
260 if (IS_ABSOLUTE_PATH (name
))
262 full_path
= xfullpath (name
);
263 make_cleanup (xfree
, full_path
);
264 real_path
= gdb_realpath (name
);
265 make_cleanup (xfree
, real_path
);
268 ALL_PSYMTABS (objfile
, pst
)
270 if (FILENAME_CMP (name
, pst
->filename
) == 0)
275 /* If the user gave us an absolute path, try to find the file in
276 this symtab and use its absolute path. */
277 if (full_path
!= NULL
)
279 if (pst
->fullname
== NULL
)
280 source_full_path_of (pst
->filename
, &pst
->fullname
);
281 if (pst
->fullname
!= NULL
282 && FILENAME_CMP (full_path
, pst
->fullname
) == 0)
288 if (real_path
!= NULL
)
291 if (pst
->fullname
== NULL
)
292 source_full_path_of (pst
->filename
, &pst
->fullname
);
293 if (pst
->fullname
!= NULL
)
295 rp
= gdb_realpath (pst
->fullname
);
296 make_cleanup (xfree
, rp
);
298 if (rp
!= NULL
&& FILENAME_CMP (real_path
, rp
) == 0)
305 /* Now, search for a matching tail (only if name doesn't have any dirs) */
307 if (lbasename (name
) == name
)
308 ALL_PSYMTABS (objfile
, pst
)
310 if (FILENAME_CMP (lbasename (pst
->filename
), name
) == 0)
317 /* Mangle a GDB method stub type. This actually reassembles the pieces of the
318 full method name, which consist of the class name (from T), the unadorned
319 method name from METHOD_ID, and the signature for the specific overload,
320 specified by SIGNATURE_ID. Note that this function is g++ specific. */
323 gdb_mangle_name (struct type
*type
, int method_id
, int signature_id
)
325 int mangled_name_len
;
327 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, method_id
);
328 struct fn_field
*method
= &f
[signature_id
];
329 char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, method_id
);
330 char *physname
= TYPE_FN_FIELD_PHYSNAME (f
, signature_id
);
331 char *newname
= type_name_no_tag (type
);
333 /* Does the form of physname indicate that it is the full mangled name
334 of a constructor (not just the args)? */
335 int is_full_physname_constructor
;
338 int is_destructor
= is_destructor_name (physname
);
339 /* Need a new type prefix. */
340 char *const_prefix
= method
->is_const
? "C" : "";
341 char *volatile_prefix
= method
->is_volatile
? "V" : "";
343 int len
= (newname
== NULL
? 0 : strlen (newname
));
345 /* Nothing to do if physname already contains a fully mangled v3 abi name
346 or an operator name. */
347 if ((physname
[0] == '_' && physname
[1] == 'Z')
348 || is_operator_name (field_name
))
349 return xstrdup (physname
);
351 is_full_physname_constructor
= is_constructor_name (physname
);
354 is_full_physname_constructor
|| (newname
&& STREQ (field_name
, newname
));
357 is_destructor
= (strncmp (physname
, "__dt", 4) == 0);
359 if (is_destructor
|| is_full_physname_constructor
)
361 mangled_name
= (char *) xmalloc (strlen (physname
) + 1);
362 strcpy (mangled_name
, physname
);
368 sprintf (buf
, "__%s%s", const_prefix
, volatile_prefix
);
370 else if (physname
[0] == 't' || physname
[0] == 'Q')
372 /* The physname for template and qualified methods already includes
374 sprintf (buf
, "__%s%s", const_prefix
, volatile_prefix
);
380 sprintf (buf
, "__%s%s%d", const_prefix
, volatile_prefix
, len
);
382 mangled_name_len
= ((is_constructor
? 0 : strlen (field_name
))
383 + strlen (buf
) + len
+ strlen (physname
) + 1);
386 mangled_name
= (char *) xmalloc (mangled_name_len
);
388 mangled_name
[0] = '\0';
390 strcpy (mangled_name
, field_name
);
392 strcat (mangled_name
, buf
);
393 /* If the class doesn't have a name, i.e. newname NULL, then we just
394 mangle it using 0 for the length of the class. Thus it gets mangled
395 as something starting with `::' rather than `classname::'. */
397 strcat (mangled_name
, newname
);
399 strcat (mangled_name
, physname
);
400 return (mangled_name
);
404 /* Initialize the language dependent portion of a symbol
405 depending upon the language for the symbol. */
407 symbol_init_language_specific (struct general_symbol_info
*gsymbol
,
408 enum language language
)
410 gsymbol
->language
= language
;
411 if (gsymbol
->language
== language_cplus
412 || gsymbol
->language
== language_java
)
414 gsymbol
->language_specific
.cplus_specific
.demangled_name
= NULL
;
416 else if (gsymbol
->language
== language_objc
)
418 gsymbol
->language_specific
.objc_specific
.demangled_name
= NULL
;
420 /* OBSOLETE else if (SYMBOL_LANGUAGE (symbol) == language_chill) */
422 /* OBSOLETE SYMBOL_CHILL_DEMANGLED_NAME (symbol) = NULL; */
426 memset (&gsymbol
->language_specific
, 0,
427 sizeof (gsymbol
->language_specific
));
431 /* Initialize a symbol's mangled name. */
433 /* Try to initialize the demangled name for a symbol, based on the
434 language of that symbol. If the language is set to language_auto,
435 it will attempt to find any demangling algorithm that works and
436 then set the language appropriately. If no demangling of any kind
437 is found, the language is set back to language_unknown, so we can
438 avoid doing this work again the next time we encounter the symbol.
439 Any required space to store the name is obtained from the specified
443 symbol_init_demangled_name (struct general_symbol_info
*gsymbol
,
444 struct obstack
*obstack
)
446 char *mangled
= gsymbol
->name
;
447 char *demangled
= NULL
;
449 if (gsymbol
->language
== language_unknown
)
450 gsymbol
->language
= language_auto
;
451 if (gsymbol
->language
== language_cplus
452 || gsymbol
->language
== language_auto
)
455 cplus_demangle (gsymbol
->name
, DMGL_PARAMS
| DMGL_ANSI
);
456 if (demangled
!= NULL
)
458 gsymbol
->language
= language_cplus
;
459 gsymbol
->language_specific
.cplus_specific
.demangled_name
=
460 obsavestring (demangled
, strlen (demangled
), obstack
);
465 gsymbol
->language_specific
.cplus_specific
.demangled_name
= NULL
;
468 if (gsymbol
->language
== language_java
)
471 cplus_demangle (gsymbol
->name
,
472 DMGL_PARAMS
| DMGL_ANSI
| DMGL_JAVA
);
473 if (demangled
!= NULL
)
475 gsymbol
->language
= language_java
;
476 gsymbol
->language_specific
.cplus_specific
.demangled_name
=
477 obsavestring (demangled
, strlen (demangled
), obstack
);
482 gsymbol
->language_specific
.cplus_specific
.demangled_name
= NULL
;
486 /* OBSOLETE if (demangled == NULL */
487 /* OBSOLETE && (gsymbol->language == language_chill */
488 /* OBSOLETE || gsymbol->language == language_auto)) */
490 /* OBSOLETE demangled = */
491 /* OBSOLETE chill_demangle (gsymbol->name); */
492 /* OBSOLETE if (demangled != NULL) */
494 /* OBSOLETE gsymbol->language = language_chill; */
495 /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = */
496 /* OBSOLETE obsavestring (demangled, strlen (demangled), obstack); */
497 /* OBSOLETE xfree (demangled); */
501 /* OBSOLETE gsymbol->language_specific.chill_specific.demangled_name = NULL; */
507 /* Return the demangled name for a symbol based on the language for
508 that symbol. If no demangled name exists, return NULL. */
510 symbol_demangled_name (struct general_symbol_info
*gsymbol
)
512 if (gsymbol
->language
== language_cplus
513 || gsymbol
->language
== language_java
)
514 return gsymbol
->language_specific
.cplus_specific
.demangled_name
;
516 else if (gsymbol
->language
== language_objc
)
517 return gsymbol
->language_specific
.objc_specific
.demangled_name
;
522 /* OBSOLETE (SYMBOL_LANGUAGE (symbol) == language_chill */
523 /* OBSOLETE ? SYMBOL_CHILL_DEMANGLED_NAME (symbol) */
526 /* Initialize the structure fields to zero values. */
528 init_sal (struct symtab_and_line
*sal
)
539 /* Find which partial symtab on contains PC and SECTION. Return 0 if none. */
541 struct partial_symtab
*
542 find_pc_sect_psymtab (CORE_ADDR pc
, asection
*section
)
544 register struct partial_symtab
*pst
;
545 register struct objfile
*objfile
;
546 struct minimal_symbol
*msymbol
;
548 /* If we know that this is not a text address, return failure. This is
549 necessary because we loop based on texthigh and textlow, which do
550 not include the data ranges. */
551 msymbol
= lookup_minimal_symbol_by_pc_section (pc
, section
);
553 && (msymbol
->type
== mst_data
554 || msymbol
->type
== mst_bss
555 || msymbol
->type
== mst_abs
556 || msymbol
->type
== mst_file_data
557 || msymbol
->type
== mst_file_bss
))
560 ALL_PSYMTABS (objfile
, pst
)
562 if (pc
>= pst
->textlow
&& pc
< pst
->texthigh
)
564 struct partial_symtab
*tpst
;
566 /* An objfile that has its functions reordered might have
567 many partial symbol tables containing the PC, but
568 we want the partial symbol table that contains the
569 function containing the PC. */
570 if (!(objfile
->flags
& OBJF_REORDERED
) &&
571 section
== 0) /* can't validate section this way */
577 for (tpst
= pst
; tpst
!= NULL
; tpst
= tpst
->next
)
579 if (pc
>= tpst
->textlow
&& pc
< tpst
->texthigh
)
581 struct partial_symbol
*p
;
583 p
= find_pc_sect_psymbol (tpst
, pc
, section
);
585 && SYMBOL_VALUE_ADDRESS (p
)
586 == SYMBOL_VALUE_ADDRESS (msymbol
))
596 /* Find which partial symtab contains PC. Return 0 if none.
597 Backward compatibility, no section */
599 struct partial_symtab
*
600 find_pc_psymtab (CORE_ADDR pc
)
602 return find_pc_sect_psymtab (pc
, find_pc_mapped_section (pc
));
605 /* Find which partial symbol within a psymtab matches PC and SECTION.
606 Return 0 if none. Check all psymtabs if PSYMTAB is 0. */
608 struct partial_symbol
*
609 find_pc_sect_psymbol (struct partial_symtab
*psymtab
, CORE_ADDR pc
,
612 struct partial_symbol
*best
= NULL
, *p
, **pp
;
616 psymtab
= find_pc_sect_psymtab (pc
, section
);
620 /* Cope with programs that start at address 0 */
621 best_pc
= (psymtab
->textlow
!= 0) ? psymtab
->textlow
- 1 : 0;
623 /* Search the global symbols as well as the static symbols, so that
624 find_pc_partial_function doesn't use a minimal symbol and thus
625 cache a bad endaddr. */
626 for (pp
= psymtab
->objfile
->global_psymbols
.list
+ psymtab
->globals_offset
;
627 (pp
- (psymtab
->objfile
->global_psymbols
.list
+ psymtab
->globals_offset
)
628 < psymtab
->n_global_syms
);
632 if (SYMBOL_NAMESPACE (p
) == VAR_NAMESPACE
633 && SYMBOL_CLASS (p
) == LOC_BLOCK
634 && pc
>= SYMBOL_VALUE_ADDRESS (p
)
635 && (SYMBOL_VALUE_ADDRESS (p
) > best_pc
636 || (psymtab
->textlow
== 0
637 && best_pc
== 0 && SYMBOL_VALUE_ADDRESS (p
) == 0)))
639 if (section
) /* match on a specific section */
641 fixup_psymbol_section (p
, psymtab
->objfile
);
642 if (SYMBOL_BFD_SECTION (p
) != section
)
645 best_pc
= SYMBOL_VALUE_ADDRESS (p
);
650 for (pp
= psymtab
->objfile
->static_psymbols
.list
+ psymtab
->statics_offset
;
651 (pp
- (psymtab
->objfile
->static_psymbols
.list
+ psymtab
->statics_offset
)
652 < psymtab
->n_static_syms
);
656 if (SYMBOL_NAMESPACE (p
) == VAR_NAMESPACE
657 && SYMBOL_CLASS (p
) == LOC_BLOCK
658 && pc
>= SYMBOL_VALUE_ADDRESS (p
)
659 && (SYMBOL_VALUE_ADDRESS (p
) > best_pc
660 || (psymtab
->textlow
== 0
661 && best_pc
== 0 && SYMBOL_VALUE_ADDRESS (p
) == 0)))
663 if (section
) /* match on a specific section */
665 fixup_psymbol_section (p
, psymtab
->objfile
);
666 if (SYMBOL_BFD_SECTION (p
) != section
)
669 best_pc
= SYMBOL_VALUE_ADDRESS (p
);
677 /* Find which partial symbol within a psymtab matches PC. Return 0 if none.
678 Check all psymtabs if PSYMTAB is 0. Backwards compatibility, no section. */
680 struct partial_symbol
*
681 find_pc_psymbol (struct partial_symtab
*psymtab
, CORE_ADDR pc
)
683 return find_pc_sect_psymbol (psymtab
, pc
, find_pc_mapped_section (pc
));
686 /* Debug symbols usually don't have section information. We need to dig that
687 out of the minimal symbols and stash that in the debug symbol. */
690 fixup_section (struct general_symbol_info
*ginfo
, struct objfile
*objfile
)
692 struct minimal_symbol
*msym
;
693 msym
= lookup_minimal_symbol (ginfo
->name
, NULL
, objfile
);
697 ginfo
->bfd_section
= SYMBOL_BFD_SECTION (msym
);
698 ginfo
->section
= SYMBOL_SECTION (msym
);
703 fixup_symbol_section (struct symbol
*sym
, struct objfile
*objfile
)
708 if (SYMBOL_BFD_SECTION (sym
))
711 fixup_section (&sym
->ginfo
, objfile
);
716 struct partial_symbol
*
717 fixup_psymbol_section (struct partial_symbol
*psym
, struct objfile
*objfile
)
722 if (SYMBOL_BFD_SECTION (psym
))
725 fixup_section (&psym
->ginfo
, objfile
);
730 /* Find the definition for a specified symbol name NAME
731 in namespace NAMESPACE, visible from lexical block BLOCK.
732 Returns the struct symbol pointer, or zero if no symbol is found.
733 If SYMTAB is non-NULL, store the symbol table in which the
734 symbol was found there, or NULL if not found.
735 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
736 NAME is a field of the current implied argument `this'. If so set
737 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
738 BLOCK_FOUND is set to the block in which NAME is found (in the case of
739 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
741 /* This function has a bunch of loops in it and it would seem to be
742 attractive to put in some QUIT's (though I'm not really sure
743 whether it can run long enough to be really important). But there
744 are a few calls for which it would appear to be bad news to quit
745 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
746 nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++
747 code below which can error(), but that probably doesn't affect
748 these calls since they are looking for a known variable and thus
749 can probably assume it will never hit the C++ code). */
752 lookup_symbol (const char *name
, const struct block
*block
,
753 const namespace_enum
namespace, int *is_a_field_of_this
,
754 struct symtab
**symtab
)
756 char *demangled_name
= NULL
;
757 const char *modified_name
= NULL
;
758 const char *mangled_name
= NULL
;
759 int needtofreename
= 0;
760 struct symbol
*returnval
;
762 modified_name
= name
;
764 /* If we are using C++ language, demangle the name before doing a lookup, so
765 we can always binary search. */
766 if (current_language
->la_language
== language_cplus
)
768 demangled_name
= cplus_demangle (name
, DMGL_ANSI
| DMGL_PARAMS
);
772 modified_name
= demangled_name
;
777 if (case_sensitivity
== case_sensitive_off
)
783 copy
= (char *) alloca (len
+ 1);
784 for (i
= 0; i
< len
; i
++)
785 copy
[i
] = tolower (name
[i
]);
787 modified_name
= copy
;
790 returnval
= lookup_symbol_aux (modified_name
, mangled_name
, block
,
791 namespace, is_a_field_of_this
, symtab
);
793 xfree (demangled_name
);
798 static struct symbol
*
799 lookup_symbol_aux (const char *name
, const char *mangled_name
,
800 const struct block
*block
, const namespace_enum
namespace,
801 int *is_a_field_of_this
, struct symtab
**symtab
)
805 /* FIXME: carlton/2002-11-05: This variable is here so that
806 lookup_symbol_aux will sometimes return NULL after receiving a
807 NULL return value from lookup_symbol_aux_minsyms, without
808 proceeding on to the partial symtab and static variable tests. I
809 suspect that that's a bad idea. */
813 /* Search specified block and its superiors. */
815 sym
= lookup_symbol_aux_local (name
, mangled_name
, block
, namespace,
821 /* NOTE: carlton/2002-11-05: At the time that this code was
822 #ifdeffed out, the value of 'block' was always NULL at this
823 point, hence the bemused comments below. */
825 /* FIXME: this code is never executed--block is always NULL at this
826 point. What is it trying to do, anyway? We already should have
827 checked the STATIC_BLOCK above (it is the superblock of top-level
828 blocks). Why is VAR_NAMESPACE special-cased? */
829 /* Don't need to mess with the psymtabs; if we have a block,
830 that file is read in. If we don't, then we deal later with
831 all the psymtab stuff that needs checking. */
832 /* Note (RT): The following never-executed code looks unnecessary to me also.
833 * If we change the code to use the original (passed-in)
834 * value of 'block', we could cause it to execute, but then what
835 * would it do? The STATIC_BLOCK of the symtab containing the passed-in
836 * 'block' was already searched by the above code. And the STATIC_BLOCK's
837 * of *other* symtabs (those files not containing 'block' lexically)
838 * should not contain 'block' address-wise. So we wouldn't expect this
839 * code to find any 'sym''s that were not found above. I vote for
840 * deleting the following paragraph of code.
842 if (namespace == VAR_NAMESPACE
&& block
!= NULL
)
845 /* Find the right symtab. */
846 ALL_SYMTABS (objfile
, s
)
848 bv
= BLOCKVECTOR (s
);
849 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
850 if (BLOCK_START (b
) <= BLOCK_START (block
)
851 && BLOCK_END (b
) > BLOCK_START (block
))
853 sym
= lookup_block_symbol (b
, name
, mangled_name
, VAR_NAMESPACE
);
859 return fixup_symbol_section (sym
, objfile
);
866 /* C++: If requested to do so by the caller,
867 check to see if NAME is a field of `this'. */
868 if (is_a_field_of_this
)
870 struct value
*v
= value_of_this (0);
872 *is_a_field_of_this
= 0;
873 if (v
&& check_field (v
, name
))
875 *is_a_field_of_this
= 1;
882 /* Now search all global blocks. Do the symtab's first, then
883 check the psymtab's. If a psymtab indicates the existence
884 of the desired name as a global, then do psymtab-to-symtab
885 conversion on the fly and return the found symbol. */
887 sym
= lookup_symbol_aux_symtabs (GLOBAL_BLOCK
, name
, mangled_name
,
894 /* Check for the possibility of the symbol being a function or
895 a mangled variable that is stored in one of the minimal symbol tables.
896 Eventually, all global symbols might be resolved in this way. */
900 sym
= lookup_symbol_aux_minsyms (name
, mangled_name
,
901 namespace, is_a_field_of_this
,
902 symtab
, &force_return
);
904 if (sym
!= NULL
|| force_return
== 1)
909 sym
= lookup_symbol_aux_psymtabs (GLOBAL_BLOCK
, name
, mangled_name
,
914 /* Now search all static file-level symbols. Not strictly correct,
915 but more useful than an error. Do the symtabs first, then check
916 the psymtabs. If a psymtab indicates the existence of the
917 desired name as a file-level static, then do psymtab-to-symtab
918 conversion on the fly and return the found symbol. */
920 sym
= lookup_symbol_aux_symtabs (STATIC_BLOCK
, name
, mangled_name
,
925 sym
= lookup_symbol_aux_psymtabs (STATIC_BLOCK
, name
, mangled_name
,
932 /* Check for the possibility of the symbol being a function or
933 a global variable that is stored in one of the minimal symbol tables.
934 The "minimal symbol table" is built from linker-supplied info.
936 RT: I moved this check to last, after the complete search of
937 the global (p)symtab's and static (p)symtab's. For HP-generated
938 symbol tables, this check was causing a premature exit from
939 lookup_symbol with NULL return, and thus messing up symbol lookups
940 of things like "c::f". It seems to me a check of the minimal
941 symbol table ought to be a last resort in any case. I'm vaguely
942 worried about the comment below which talks about FORTRAN routines "foo_"
943 though... is it saying we need to do the "minsym" check before
944 the static check in this case?
950 sym
= lookup_symbol_aux_minsyms (name
, mangled_name
,
951 namespace, is_a_field_of_this
,
952 symtab
, &force_return
);
954 if (sym
!= NULL
|| force_return
== 1)
964 /* Check to see if the symbol is defined in BLOCK or its
967 static struct symbol
*
968 lookup_symbol_aux_local (const char *name
, const char *mangled_name
,
969 const struct block
*block
,
970 const namespace_enum
namespace,
971 struct symtab
**symtab
)
977 sym
= lookup_symbol_aux_block (name
, mangled_name
, block
, namespace,
981 block
= BLOCK_SUPERBLOCK (block
);
987 /* Look up a symbol in a block; if found, locate its symtab, fixup the
988 symbol, and set block_found appropriately. */
990 static struct symbol
*
991 lookup_symbol_aux_block (const char *name
, const char *mangled_name
,
992 const struct block
*block
,
993 const namespace_enum
namespace,
994 struct symtab
**symtab
)
997 struct objfile
*objfile
= NULL
;
998 struct blockvector
*bv
;
1000 struct symtab
*s
= NULL
;
1002 sym
= lookup_block_symbol (block
, name
, mangled_name
, namespace);
1005 block_found
= block
;
1008 /* Search the list of symtabs for one which contains the
1009 address of the start of this block. */
1010 ALL_SYMTABS (objfile
, s
)
1012 bv
= BLOCKVECTOR (s
);
1013 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1014 if (BLOCK_START (b
) <= BLOCK_START (block
)
1015 && BLOCK_END (b
) > BLOCK_START (block
))
1022 return fixup_symbol_section (sym
, objfile
);
1028 /* Check to see if the symbol is defined in one of the symtabs.
1029 BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
1030 depending on whether or not we want to search global symbols or
1033 static struct symbol
*
1034 lookup_symbol_aux_symtabs (int block_index
,
1035 const char *name
, const char *mangled_name
,
1036 const namespace_enum
namespace,
1037 struct symtab
**symtab
)
1040 struct objfile
*objfile
;
1041 struct blockvector
*bv
;
1042 const struct block
*block
;
1045 ALL_SYMTABS (objfile
, s
)
1047 bv
= BLOCKVECTOR (s
);
1048 block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
1049 sym
= lookup_block_symbol (block
, name
, mangled_name
, namespace);
1052 block_found
= block
;
1055 return fixup_symbol_section (sym
, objfile
);
1062 /* Check to see if the symbol is defined in one of the partial
1063 symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or
1064 STATIC_BLOCK, depending on whether or not we want to search global
1065 symbols or static symbols. */
1067 static struct symbol
*
1068 lookup_symbol_aux_psymtabs (int block_index
, const char *name
,
1069 const char *mangled_name
,
1070 const namespace_enum
namespace,
1071 struct symtab
**symtab
)
1074 struct objfile
*objfile
;
1075 struct blockvector
*bv
;
1076 const struct block
*block
;
1077 struct partial_symtab
*ps
;
1079 const int psymtab_index
= (block_index
== GLOBAL_BLOCK
? 1 : 0);
1081 ALL_PSYMTABS (objfile
, ps
)
1084 && lookup_partial_symbol (ps
, name
, psymtab_index
, namespace))
1086 s
= PSYMTAB_TO_SYMTAB (ps
);
1087 bv
= BLOCKVECTOR (s
);
1088 block
= BLOCKVECTOR_BLOCK (bv
, block_index
);
1089 sym
= lookup_block_symbol (block
, name
, mangled_name
, namespace);
1092 /* This shouldn't be necessary, but as a last resort try
1093 looking in the statics even though the psymtab claimed
1094 the symbol was global, or vice-versa. It's possible
1095 that the psymtab gets it wrong in some cases. */
1097 /* FIXME: carlton/2002-09-30: Should we really do that?
1098 If that happens, isn't it likely to be a GDB error, in
1099 which case we should fix the GDB error rather than
1100 silently dealing with it here? So I'd vote for
1101 removing the check for the symbol in the other
1103 block
= BLOCKVECTOR_BLOCK (bv
,
1104 block_index
== GLOBAL_BLOCK
?
1105 STATIC_BLOCK
: GLOBAL_BLOCK
);
1106 sym
= lookup_block_symbol (block
, name
, mangled_name
, namespace);
1108 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>).",
1109 block_index
== GLOBAL_BLOCK
? "global" : "static",
1110 name
, ps
->filename
, name
, name
);
1114 return fixup_symbol_section (sym
, objfile
);
1121 /* Check for the possibility of the symbol being a function or a
1122 mangled variable that is stored in one of the minimal symbol
1123 tables. Eventually, all global symbols might be resolved in this
1126 static struct symbol
*
1127 lookup_symbol_aux_minsyms (const char *name
,
1128 const char *mangled_name
,
1129 const namespace_enum
namespace,
1130 int *is_a_field_of_this
,
1131 struct symtab
**symtab
,
1135 struct blockvector
*bv
;
1136 const struct block
*block
;
1137 struct minimal_symbol
*msymbol
;
1140 if (namespace == VAR_NAMESPACE
)
1142 msymbol
= lookup_minimal_symbol (name
, NULL
, NULL
);
1144 if (msymbol
!= NULL
)
1146 /* OK, we found a minimal symbol in spite of not finding any
1147 symbol. There are various possible explanations for
1148 this. One possibility is the symbol exists in code not
1149 compiled -g. Another possibility is that the 'psymtab'
1150 isn't doing its job. A third possibility, related to #2,
1151 is that we were confused by name-mangling. For instance,
1152 maybe the psymtab isn't doing its job because it only
1153 know about demangled names, but we were given a mangled
1156 /* We first use the address in the msymbol to try to locate
1157 the appropriate symtab. Note that find_pc_sect_symtab()
1158 has a side-effect of doing psymtab-to-symtab expansion,
1159 for the found symtab. */
1160 s
= find_pc_sect_symtab (SYMBOL_VALUE_ADDRESS (msymbol
),
1161 SYMBOL_BFD_SECTION (msymbol
));
1164 /* This is a function which has a symtab for its address. */
1165 bv
= BLOCKVECTOR (s
);
1166 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1168 /* This call used to pass `SYMBOL_NAME (msymbol)' as the
1169 `name' argument to lookup_block_symbol. But the name
1170 of a minimal symbol is always mangled, so that seems
1171 to be clearly the wrong thing to pass as the
1174 lookup_block_symbol (block
, name
, mangled_name
, namespace);
1175 /* We kept static functions in minimal symbol table as well as
1176 in static scope. We want to find them in the symbol table. */
1179 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1180 sym
= lookup_block_symbol (block
, name
,
1181 mangled_name
, namespace);
1184 /* NOTE: carlton/2002-12-04: The following comment was
1185 taken from a time when two versions of this function
1186 were part of the body of lookup_symbol_aux: this
1187 comment was taken from the version of the function
1188 that was #ifdef HPUXHPPA, and the comment was right
1189 before the 'return NULL' part of lookup_symbol_aux.
1190 (Hence the "Fall through and return 0" comment.)
1191 Elena did some digging into the situation for
1192 Fortran, and she reports:
1194 "I asked around (thanks to Jeff Knaggs), and I think
1195 the story for Fortran goes like this:
1197 "Apparently, in older Fortrans, '_' was not part of
1198 the user namespace. g77 attached a final '_' to
1199 procedure names as the exported symbols for linkage
1200 (foo_) , but the symbols went in the debug info just
1201 like 'foo'. The rationale behind this is not
1202 completely clear, and maybe it was done to other
1203 symbols as well, not just procedures." */
1205 /* If we get here with sym == 0, the symbol was
1206 found in the minimal symbol table
1207 but not in the symtab.
1208 Fall through and return 0 to use the msymbol
1209 definition of "foo_".
1210 (Note that outer code generally follows up a call
1211 to this routine with a call to lookup_minimal_symbol(),
1212 so a 0 return means we'll just flow into that other routine).
1214 This happens for Fortran "foo_" symbols,
1215 which are "foo" in the symtab.
1217 This can also happen if "asm" is used to make a
1218 regular symbol but not a debugging symbol, e.g.
1219 asm(".globl _main");
1223 if (symtab
!= NULL
&& sym
!= NULL
)
1226 return fixup_symbol_section (sym
, s
->objfile
);
1228 else if (MSYMBOL_TYPE (msymbol
) != mst_text
1229 && MSYMBOL_TYPE (msymbol
) != mst_file_text
1230 && !STREQ (name
, SYMBOL_NAME (msymbol
)))
1232 /* This is a mangled variable, look it up by its
1235 return lookup_symbol_aux (SYMBOL_NAME (msymbol
), mangled_name
,
1236 NULL
, namespace, is_a_field_of_this
,
1245 /* Look, in partial_symtab PST, for symbol NAME. Check the global
1246 symbols if GLOBAL, the static symbols if not */
1248 static struct partial_symbol
*
1249 lookup_partial_symbol (struct partial_symtab
*pst
, const char *name
, int global
,
1250 namespace_enum
namespace)
1252 struct partial_symbol
*temp
;
1253 struct partial_symbol
**start
, **psym
;
1254 struct partial_symbol
**top
, **bottom
, **center
;
1255 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
1256 int do_linear_search
= 1;
1263 pst
->objfile
->global_psymbols
.list
+ pst
->globals_offset
:
1264 pst
->objfile
->static_psymbols
.list
+ pst
->statics_offset
);
1266 if (global
) /* This means we can use a binary search. */
1268 do_linear_search
= 0;
1270 /* Binary search. This search is guaranteed to end with center
1271 pointing at the earliest partial symbol with the correct
1272 name. At that point *all* partial symbols with that name
1273 will be checked against the correct namespace. */
1276 top
= start
+ length
- 1;
1277 while (top
> bottom
)
1279 center
= bottom
+ (top
- bottom
) / 2;
1280 if (!(center
< top
))
1281 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1282 if (!do_linear_search
1283 && (SYMBOL_LANGUAGE (*center
) == language_java
))
1285 do_linear_search
= 1;
1287 if (strcmp (SYMBOL_SOURCE_NAME (*center
), name
) >= 0)
1293 bottom
= center
+ 1;
1296 if (!(top
== bottom
))
1297 internal_error (__FILE__
, __LINE__
, "failed internal consistency check");
1299 /* djb - 2000-06-03 - Use SYMBOL_MATCHES_NAME, not a strcmp, so
1300 we don't have to force a linear search on C++. Probably holds true
1301 for JAVA as well, no way to check.*/
1302 while (SYMBOL_MATCHES_NAME (*top
,name
))
1304 if (SYMBOL_NAMESPACE (*top
) == namespace)
1312 /* Can't use a binary search or else we found during the binary search that
1313 we should also do a linear search. */
1315 if (do_linear_search
)
1317 for (psym
= start
; psym
< start
+ length
; psym
++)
1319 if (namespace == SYMBOL_NAMESPACE (*psym
))
1321 if (SYMBOL_MATCHES_NAME (*psym
, name
))
1332 /* Look up a type named NAME in the struct_namespace. The type returned
1333 must not be opaque -- i.e., must have at least one field defined
1335 This code was modelled on lookup_symbol -- the parts not relevant to looking
1336 up types were just left out. In particular it's assumed here that types
1337 are available in struct_namespace and only at file-static or global blocks. */
1341 lookup_transparent_type (const char *name
)
1343 register struct symbol
*sym
;
1344 register struct symtab
*s
= NULL
;
1345 register struct partial_symtab
*ps
;
1346 struct blockvector
*bv
;
1347 register struct objfile
*objfile
;
1348 register struct block
*block
;
1350 /* Now search all the global symbols. Do the symtab's first, then
1351 check the psymtab's. If a psymtab indicates the existence
1352 of the desired name as a global, then do psymtab-to-symtab
1353 conversion on the fly and return the found symbol. */
1355 ALL_SYMTABS (objfile
, s
)
1357 bv
= BLOCKVECTOR (s
);
1358 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1359 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1360 if (sym
&& !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
1362 return SYMBOL_TYPE (sym
);
1366 ALL_PSYMTABS (objfile
, ps
)
1368 if (!ps
->readin
&& lookup_partial_symbol (ps
, name
, 1, STRUCT_NAMESPACE
))
1370 s
= PSYMTAB_TO_SYMTAB (ps
);
1371 bv
= BLOCKVECTOR (s
);
1372 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1373 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1376 /* This shouldn't be necessary, but as a last resort
1377 * try looking in the statics even though the psymtab
1378 * claimed the symbol was global. It's possible that
1379 * the psymtab gets it wrong in some cases.
1381 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1382 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1384 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab.\n\
1385 %s may be an inlined function, or may be a template function\n\
1386 (if a template, try specifying an instantiation: %s<type>).",
1387 name
, ps
->filename
, name
, name
);
1389 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
1390 return SYMBOL_TYPE (sym
);
1394 /* Now search the static file-level symbols.
1395 Not strictly correct, but more useful than an error.
1396 Do the symtab's first, then
1397 check the psymtab's. If a psymtab indicates the existence
1398 of the desired name as a file-level static, then do psymtab-to-symtab
1399 conversion on the fly and return the found symbol.
1402 ALL_SYMTABS (objfile
, s
)
1404 bv
= BLOCKVECTOR (s
);
1405 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1406 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1407 if (sym
&& !TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
1409 return SYMBOL_TYPE (sym
);
1413 ALL_PSYMTABS (objfile
, ps
)
1415 if (!ps
->readin
&& lookup_partial_symbol (ps
, name
, 0, STRUCT_NAMESPACE
))
1417 s
= PSYMTAB_TO_SYMTAB (ps
);
1418 bv
= BLOCKVECTOR (s
);
1419 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
1420 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1423 /* This shouldn't be necessary, but as a last resort
1424 * try looking in the globals even though the psymtab
1425 * claimed the symbol was static. It's possible that
1426 * the psymtab gets it wrong in some cases.
1428 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1429 sym
= lookup_block_symbol (block
, name
, NULL
, STRUCT_NAMESPACE
);
1431 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab.\n\
1432 %s may be an inlined function, or may be a template function\n\
1433 (if a template, try specifying an instantiation: %s<type>).",
1434 name
, ps
->filename
, name
, name
);
1436 if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym
)))
1437 return SYMBOL_TYPE (sym
);
1440 return (struct type
*) 0;
1444 /* Find the psymtab containing main(). */
1445 /* FIXME: What about languages without main() or specially linked
1446 executables that have no main() ? */
1448 struct partial_symtab
*
1449 find_main_psymtab (void)
1451 register struct partial_symtab
*pst
;
1452 register struct objfile
*objfile
;
1454 ALL_PSYMTABS (objfile
, pst
)
1456 if (lookup_partial_symbol (pst
, main_name (), 1, VAR_NAMESPACE
))
1464 /* Search BLOCK for symbol NAME in NAMESPACE.
1466 Note that if NAME is the demangled form of a C++ symbol, we will fail
1467 to find a match during the binary search of the non-encoded names, but
1468 for now we don't worry about the slight inefficiency of looking for
1469 a match we'll never find, since it will go pretty quick. Once the
1470 binary search terminates, we drop through and do a straight linear
1471 search on the symbols. Each symbol which is marked as being a C++
1472 symbol (language_cplus set) has both the encoded and non-encoded names
1475 If MANGLED_NAME is non-NULL, verify that any symbol we find has this
1476 particular mangled name.
1480 lookup_block_symbol (register const struct block
*block
, const char *name
,
1481 const char *mangled_name
,
1482 const namespace_enum
namespace)
1484 register int bot
, top
, inc
;
1485 register struct symbol
*sym
;
1486 register struct symbol
*sym_found
= NULL
;
1487 register int do_linear_search
= 1;
1489 if (BLOCK_HASHTABLE (block
))
1491 unsigned int hash_index
;
1492 hash_index
= msymbol_hash_iw (name
);
1493 hash_index
= hash_index
% BLOCK_BUCKETS (block
);
1494 for (sym
= BLOCK_BUCKET (block
, hash_index
); sym
; sym
= sym
->hash_next
)
1496 if (SYMBOL_NAMESPACE (sym
) == namespace
1498 ? strcmp (SYMBOL_NAME (sym
), mangled_name
) == 0
1499 : SYMBOL_MATCHES_NAME (sym
, name
)))
1505 /* If the blocks's symbols were sorted, start with a binary search. */
1507 if (BLOCK_SHOULD_SORT (block
))
1509 /* Reset the linear search flag so if the binary search fails, we
1510 won't do the linear search once unless we find some reason to
1513 do_linear_search
= 0;
1514 top
= BLOCK_NSYMS (block
);
1517 /* Advance BOT to not far before the first symbol whose name is NAME. */
1521 inc
= (top
- bot
+ 1);
1522 /* No need to keep binary searching for the last few bits worth. */
1527 inc
= (inc
>> 1) + bot
;
1528 sym
= BLOCK_SYM (block
, inc
);
1529 if (!do_linear_search
&& (SYMBOL_LANGUAGE (sym
) == language_java
))
1531 do_linear_search
= 1;
1533 if (SYMBOL_SOURCE_NAME (sym
)[0] < name
[0])
1537 else if (SYMBOL_SOURCE_NAME (sym
)[0] > name
[0])
1541 else if (strcmp (SYMBOL_SOURCE_NAME (sym
), name
) < 0)
1551 /* Now scan forward until we run out of symbols, find one whose
1552 name is greater than NAME, or find one we want. If there is
1553 more than one symbol with the right name and namespace, we
1554 return the first one; I believe it is now impossible for us
1555 to encounter two symbols with the same name and namespace
1556 here, because blocks containing argument symbols are no
1557 longer sorted. The exception is for C++, where multiple functions
1558 (cloned constructors / destructors, in particular) can have
1559 the same demangled name. So if we have a particular
1560 mangled name to match, try to do so. */
1562 top
= BLOCK_NSYMS (block
);
1565 sym
= BLOCK_SYM (block
, bot
);
1566 if (SYMBOL_NAMESPACE (sym
) == namespace
1568 ? strcmp (SYMBOL_NAME (sym
), mangled_name
) == 0
1569 : SYMBOL_MATCHES_NAME (sym
, name
)))
1573 if (SYMBOL_SOURCE_NAME (sym
)[0] > name
[0])
1581 /* Here if block isn't sorted, or we fail to find a match during the
1582 binary search above. If during the binary search above, we find a
1583 symbol which is a Java symbol, then we have re-enabled the linear
1584 search flag which was reset when starting the binary search.
1586 This loop is equivalent to the loop above, but hacked greatly for speed.
1588 Note that parameter symbols do not always show up last in the
1589 list; this loop makes sure to take anything else other than
1590 parameter symbols first; it only uses parameter symbols as a
1591 last resort. Note that this only takes up extra computation
1594 if (do_linear_search
)
1596 top
= BLOCK_NSYMS (block
);
1600 sym
= BLOCK_SYM (block
, bot
);
1601 if (SYMBOL_NAMESPACE (sym
) == namespace
1603 ? strcmp (SYMBOL_NAME (sym
), mangled_name
) == 0
1604 : SYMBOL_MATCHES_NAME (sym
, name
)))
1606 /* If SYM has aliases, then use any alias that is active
1607 at the current PC. If no alias is active at the current
1608 PC, then use the main symbol.
1610 ?!? Is checking the current pc correct? Is this routine
1611 ever called to look up a symbol from another context?
1613 FIXME: No, it's not correct. If someone sets a
1614 conditional breakpoint at an address, then the
1615 breakpoint's `struct expression' should refer to the
1616 `struct symbol' appropriate for the breakpoint's
1617 address, which may not be the PC.
1619 Even if it were never called from another context,
1620 it's totally bizarre for lookup_symbol's behavior to
1621 depend on the value of the inferior's current PC. We
1622 should pass in the appropriate PC as well as the
1623 block. The interface to lookup_symbol should change
1624 to require the caller to provide a PC. */
1626 if (SYMBOL_ALIASES (sym
))
1627 sym
= find_active_alias (sym
, read_pc ());
1630 if (SYMBOL_CLASS (sym
) != LOC_ARG
&&
1631 SYMBOL_CLASS (sym
) != LOC_LOCAL_ARG
&&
1632 SYMBOL_CLASS (sym
) != LOC_REF_ARG
&&
1633 SYMBOL_CLASS (sym
) != LOC_REGPARM
&&
1634 SYMBOL_CLASS (sym
) != LOC_REGPARM_ADDR
&&
1635 SYMBOL_CLASS (sym
) != LOC_BASEREG_ARG
)
1643 return (sym_found
); /* Will be NULL if not found. */
1646 /* Given a main symbol SYM and ADDR, search through the alias
1647 list to determine if an alias is active at ADDR and return
1650 If no alias is active, then return SYM. */
1652 static struct symbol
*
1653 find_active_alias (struct symbol
*sym
, CORE_ADDR addr
)
1655 struct range_list
*r
;
1656 struct alias_list
*aliases
;
1658 /* If we have aliases, check them first. */
1659 aliases
= SYMBOL_ALIASES (sym
);
1663 if (!SYMBOL_RANGES (aliases
->sym
))
1664 return aliases
->sym
;
1665 for (r
= SYMBOL_RANGES (aliases
->sym
); r
; r
= r
->next
)
1667 if (r
->start
<= addr
&& r
->end
> addr
)
1668 return aliases
->sym
;
1670 aliases
= aliases
->next
;
1673 /* Nothing found, return the main symbol. */
1678 /* Return the symbol for the function which contains a specified
1679 lexical block, described by a struct block BL. */
1682 block_function (struct block
*bl
)
1684 while (BLOCK_FUNCTION (bl
) == 0 && BLOCK_SUPERBLOCK (bl
) != 0)
1685 bl
= BLOCK_SUPERBLOCK (bl
);
1687 return BLOCK_FUNCTION (bl
);
1690 /* Find the symtab associated with PC and SECTION. Look through the
1691 psymtabs and read in another symtab if necessary. */
1694 find_pc_sect_symtab (CORE_ADDR pc
, asection
*section
)
1696 register struct block
*b
;
1697 struct blockvector
*bv
;
1698 register struct symtab
*s
= NULL
;
1699 register struct symtab
*best_s
= NULL
;
1700 register struct partial_symtab
*ps
;
1701 register struct objfile
*objfile
;
1702 CORE_ADDR distance
= 0;
1703 struct minimal_symbol
*msymbol
;
1705 /* If we know that this is not a text address, return failure. This is
1706 necessary because we loop based on the block's high and low code
1707 addresses, which do not include the data ranges, and because
1708 we call find_pc_sect_psymtab which has a similar restriction based
1709 on the partial_symtab's texthigh and textlow. */
1710 msymbol
= lookup_minimal_symbol_by_pc_section (pc
, section
);
1712 && (msymbol
->type
== mst_data
1713 || msymbol
->type
== mst_bss
1714 || msymbol
->type
== mst_abs
1715 || msymbol
->type
== mst_file_data
1716 || msymbol
->type
== mst_file_bss
))
1719 /* Search all symtabs for the one whose file contains our address, and which
1720 is the smallest of all the ones containing the address. This is designed
1721 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
1722 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
1723 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
1725 This happens for native ecoff format, where code from included files
1726 gets its own symtab. The symtab for the included file should have
1727 been read in already via the dependency mechanism.
1728 It might be swifter to create several symtabs with the same name
1729 like xcoff does (I'm not sure).
1731 It also happens for objfiles that have their functions reordered.
1732 For these, the symtab we are looking for is not necessarily read in. */
1734 ALL_SYMTABS (objfile
, s
)
1736 bv
= BLOCKVECTOR (s
);
1737 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
1739 if (BLOCK_START (b
) <= pc
1740 && BLOCK_END (b
) > pc
1742 || BLOCK_END (b
) - BLOCK_START (b
) < distance
))
1744 /* For an objfile that has its functions reordered,
1745 find_pc_psymtab will find the proper partial symbol table
1746 and we simply return its corresponding symtab. */
1747 /* In order to better support objfiles that contain both
1748 stabs and coff debugging info, we continue on if a psymtab
1750 if ((objfile
->flags
& OBJF_REORDERED
) && objfile
->psymtabs
)
1752 ps
= find_pc_sect_psymtab (pc
, section
);
1754 return PSYMTAB_TO_SYMTAB (ps
);
1759 struct symbol
*sym
= NULL
;
1761 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
1763 fixup_symbol_section (sym
, objfile
);
1764 if (section
== SYMBOL_BFD_SECTION (sym
))
1767 if ((i
>= BLOCK_BUCKETS (b
)) && (sym
== NULL
))
1768 continue; /* no symbol in this symtab matches section */
1770 distance
= BLOCK_END (b
) - BLOCK_START (b
);
1779 ps
= find_pc_sect_psymtab (pc
, section
);
1783 /* Might want to error() here (in case symtab is corrupt and
1784 will cause a core dump), but maybe we can successfully
1785 continue, so let's not. */
1787 (Internal error: pc 0x%s in read in psymtab, but not in symtab.)\n",
1789 s
= PSYMTAB_TO_SYMTAB (ps
);
1794 /* Find the symtab associated with PC. Look through the psymtabs and
1795 read in another symtab if necessary. Backward compatibility, no section */
1798 find_pc_symtab (CORE_ADDR pc
)
1800 return find_pc_sect_symtab (pc
, find_pc_mapped_section (pc
));
1804 /* Find the source file and line number for a given PC value and SECTION.
1805 Return a structure containing a symtab pointer, a line number,
1806 and a pc range for the entire source line.
1807 The value's .pc field is NOT the specified pc.
1808 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1809 use the line that ends there. Otherwise, in that case, the line
1810 that begins there is used. */
1812 /* The big complication here is that a line may start in one file, and end just
1813 before the start of another file. This usually occurs when you #include
1814 code in the middle of a subroutine. To properly find the end of a line's PC
1815 range, we must search all symtabs associated with this compilation unit, and
1816 find the one whose first PC is closer than that of the next line in this
1819 /* If it's worth the effort, we could be using a binary search. */
1821 struct symtab_and_line
1822 find_pc_sect_line (CORE_ADDR pc
, struct sec
*section
, int notcurrent
)
1825 register struct linetable
*l
;
1828 register struct linetable_entry
*item
;
1829 struct symtab_and_line val
;
1830 struct blockvector
*bv
;
1831 struct minimal_symbol
*msymbol
;
1832 struct minimal_symbol
*mfunsym
;
1834 /* Info on best line seen so far, and where it starts, and its file. */
1836 struct linetable_entry
*best
= NULL
;
1837 CORE_ADDR best_end
= 0;
1838 struct symtab
*best_symtab
= 0;
1840 /* Store here the first line number
1841 of a file which contains the line at the smallest pc after PC.
1842 If we don't find a line whose range contains PC,
1843 we will use a line one less than this,
1844 with a range from the start of that file to the first line's pc. */
1845 struct linetable_entry
*alt
= NULL
;
1846 struct symtab
*alt_symtab
= 0;
1848 /* Info on best line seen in this file. */
1850 struct linetable_entry
*prev
;
1852 /* If this pc is not from the current frame,
1853 it is the address of the end of a call instruction.
1854 Quite likely that is the start of the following statement.
1855 But what we want is the statement containing the instruction.
1856 Fudge the pc to make sure we get that. */
1858 init_sal (&val
); /* initialize to zeroes */
1860 /* It's tempting to assume that, if we can't find debugging info for
1861 any function enclosing PC, that we shouldn't search for line
1862 number info, either. However, GAS can emit line number info for
1863 assembly files --- very helpful when debugging hand-written
1864 assembly code. In such a case, we'd have no debug info for the
1865 function, but we would have line info. */
1870 /* elz: added this because this function returned the wrong
1871 information if the pc belongs to a stub (import/export)
1872 to call a shlib function. This stub would be anywhere between
1873 two functions in the target, and the line info was erroneously
1874 taken to be the one of the line before the pc.
1876 /* RT: Further explanation:
1878 * We have stubs (trampolines) inserted between procedures.
1880 * Example: "shr1" exists in a shared library, and a "shr1" stub also
1881 * exists in the main image.
1883 * In the minimal symbol table, we have a bunch of symbols
1884 * sorted by start address. The stubs are marked as "trampoline",
1885 * the others appear as text. E.g.:
1887 * Minimal symbol table for main image
1888 * main: code for main (text symbol)
1889 * shr1: stub (trampoline symbol)
1890 * foo: code for foo (text symbol)
1892 * Minimal symbol table for "shr1" image:
1894 * shr1: code for shr1 (text symbol)
1897 * So the code below is trying to detect if we are in the stub
1898 * ("shr1" stub), and if so, find the real code ("shr1" trampoline),
1899 * and if found, do the symbolization from the real-code address
1900 * rather than the stub address.
1902 * Assumptions being made about the minimal symbol table:
1903 * 1. lookup_minimal_symbol_by_pc() will return a trampoline only
1904 * if we're really in the trampoline. If we're beyond it (say
1905 * we're in "foo" in the above example), it'll have a closer
1906 * symbol (the "foo" text symbol for example) and will not
1907 * return the trampoline.
1908 * 2. lookup_minimal_symbol_text() will find a real text symbol
1909 * corresponding to the trampoline, and whose address will
1910 * be different than the trampoline address. I put in a sanity
1911 * check for the address being the same, to avoid an
1912 * infinite recursion.
1914 msymbol
= lookup_minimal_symbol_by_pc (pc
);
1915 if (msymbol
!= NULL
)
1916 if (MSYMBOL_TYPE (msymbol
) == mst_solib_trampoline
)
1918 mfunsym
= lookup_minimal_symbol_text (SYMBOL_NAME (msymbol
), NULL
, NULL
);
1919 if (mfunsym
== NULL
)
1920 /* I eliminated this warning since it is coming out
1921 * in the following situation:
1922 * gdb shmain // test program with shared libraries
1923 * (gdb) break shr1 // function in shared lib
1924 * Warning: In stub for ...
1925 * In the above situation, the shared lib is not loaded yet,
1926 * so of course we can't find the real func/line info,
1927 * but the "break" still works, and the warning is annoying.
1928 * So I commented out the warning. RT */
1929 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1931 else if (SYMBOL_VALUE (mfunsym
) == SYMBOL_VALUE (msymbol
))
1932 /* Avoid infinite recursion */
1933 /* See above comment about why warning is commented out */
1934 /* warning ("In stub for %s; unable to find real function/line info", SYMBOL_NAME(msymbol)) */ ;
1937 return find_pc_line (SYMBOL_VALUE (mfunsym
), 0);
1941 s
= find_pc_sect_symtab (pc
, section
);
1944 /* if no symbol information, return previous pc */
1951 bv
= BLOCKVECTOR (s
);
1953 /* Look at all the symtabs that share this blockvector.
1954 They all have the same apriori range, that we found was right;
1955 but they have different line tables. */
1957 for (; s
&& BLOCKVECTOR (s
) == bv
; s
= s
->next
)
1959 /* Find the best line in this symtab. */
1966 /* I think len can be zero if the symtab lacks line numbers
1967 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
1968 I'm not sure which, and maybe it depends on the symbol
1974 item
= l
->item
; /* Get first line info */
1976 /* Is this file's first line closer than the first lines of other files?
1977 If so, record this file, and its first line, as best alternate. */
1978 if (item
->pc
> pc
&& (!alt
|| item
->pc
< alt
->pc
))
1984 for (i
= 0; i
< len
; i
++, item
++)
1986 /* Leave prev pointing to the linetable entry for the last line
1987 that started at or before PC. */
1994 /* At this point, prev points at the line whose start addr is <= pc, and
1995 item points at the next line. If we ran off the end of the linetable
1996 (pc >= start of the last line), then prev == item. If pc < start of
1997 the first line, prev will not be set. */
1999 /* Is this file's best line closer than the best in the other files?
2000 If so, record this file, and its best line, as best so far. */
2002 if (prev
&& (!best
|| prev
->pc
> best
->pc
))
2007 /* Discard BEST_END if it's before the PC of the current BEST. */
2008 if (best_end
<= best
->pc
)
2012 /* If another line (denoted by ITEM) is in the linetable and its
2013 PC is after BEST's PC, but before the current BEST_END, then
2014 use ITEM's PC as the new best_end. */
2015 if (best
&& i
< len
&& item
->pc
> best
->pc
2016 && (best_end
== 0 || best_end
> item
->pc
))
2017 best_end
= item
->pc
;
2023 { /* If we didn't find any line # info, just
2029 val
.symtab
= alt_symtab
;
2030 val
.line
= alt
->line
- 1;
2032 /* Don't return line 0, that means that we didn't find the line. */
2036 val
.pc
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
));
2040 else if (best
->line
== 0)
2042 /* If our best fit is in a range of PC's for which no line
2043 number info is available (line number is zero) then we didn't
2044 find any valid line information. */
2049 val
.symtab
= best_symtab
;
2050 val
.line
= best
->line
;
2052 if (best_end
&& (!alt
|| best_end
< alt
->pc
))
2057 val
.end
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
));
2059 val
.section
= section
;
2063 /* Backward compatibility (no section) */
2065 struct symtab_and_line
2066 find_pc_line (CORE_ADDR pc
, int notcurrent
)
2070 section
= find_pc_overlay (pc
);
2071 if (pc_in_unmapped_range (pc
, section
))
2072 pc
= overlay_mapped_address (pc
, section
);
2073 return find_pc_sect_line (pc
, section
, notcurrent
);
2076 /* Find line number LINE in any symtab whose name is the same as
2079 If found, return the symtab that contains the linetable in which it was
2080 found, set *INDEX to the index in the linetable of the best entry
2081 found, and set *EXACT_MATCH nonzero if the value returned is an
2084 If not found, return NULL. */
2087 find_line_symtab (struct symtab
*symtab
, int line
, int *index
, int *exact_match
)
2091 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
2095 struct linetable
*best_linetable
;
2096 struct symtab
*best_symtab
;
2098 /* First try looking it up in the given symtab. */
2099 best_linetable
= LINETABLE (symtab
);
2100 best_symtab
= symtab
;
2101 best_index
= find_line_common (best_linetable
, line
, &exact
);
2102 if (best_index
< 0 || !exact
)
2104 /* Didn't find an exact match. So we better keep looking for
2105 another symtab with the same name. In the case of xcoff,
2106 multiple csects for one source file (produced by IBM's FORTRAN
2107 compiler) produce multiple symtabs (this is unavoidable
2108 assuming csects can be at arbitrary places in memory and that
2109 the GLOBAL_BLOCK of a symtab has a begin and end address). */
2111 /* BEST is the smallest linenumber > LINE so far seen,
2112 or 0 if none has been seen so far.
2113 BEST_INDEX and BEST_LINETABLE identify the item for it. */
2116 struct objfile
*objfile
;
2119 if (best_index
>= 0)
2120 best
= best_linetable
->item
[best_index
].line
;
2124 ALL_SYMTABS (objfile
, s
)
2126 struct linetable
*l
;
2129 if (!STREQ (symtab
->filename
, s
->filename
))
2132 ind
= find_line_common (l
, line
, &exact
);
2142 if (best
== 0 || l
->item
[ind
].line
< best
)
2144 best
= l
->item
[ind
].line
;
2157 *index
= best_index
;
2159 *exact_match
= exact
;
2164 /* Set the PC value for a given source file and line number and return true.
2165 Returns zero for invalid line number (and sets the PC to 0).
2166 The source file is specified with a struct symtab. */
2169 find_line_pc (struct symtab
*symtab
, int line
, CORE_ADDR
*pc
)
2171 struct linetable
*l
;
2178 symtab
= find_line_symtab (symtab
, line
, &ind
, NULL
);
2181 l
= LINETABLE (symtab
);
2182 *pc
= l
->item
[ind
].pc
;
2189 /* Find the range of pc values in a line.
2190 Store the starting pc of the line into *STARTPTR
2191 and the ending pc (start of next line) into *ENDPTR.
2192 Returns 1 to indicate success.
2193 Returns 0 if could not find the specified line. */
2196 find_line_pc_range (struct symtab_and_line sal
, CORE_ADDR
*startptr
,
2199 CORE_ADDR startaddr
;
2200 struct symtab_and_line found_sal
;
2203 if (startaddr
== 0 && !find_line_pc (sal
.symtab
, sal
.line
, &startaddr
))
2206 /* This whole function is based on address. For example, if line 10 has
2207 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
2208 "info line *0x123" should say the line goes from 0x100 to 0x200
2209 and "info line *0x355" should say the line goes from 0x300 to 0x400.
2210 This also insures that we never give a range like "starts at 0x134
2211 and ends at 0x12c". */
2213 found_sal
= find_pc_sect_line (startaddr
, sal
.section
, 0);
2214 if (found_sal
.line
!= sal
.line
)
2216 /* The specified line (sal) has zero bytes. */
2217 *startptr
= found_sal
.pc
;
2218 *endptr
= found_sal
.pc
;
2222 *startptr
= found_sal
.pc
;
2223 *endptr
= found_sal
.end
;
2228 /* Given a line table and a line number, return the index into the line
2229 table for the pc of the nearest line whose number is >= the specified one.
2230 Return -1 if none is found. The value is >= 0 if it is an index.
2232 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
2235 find_line_common (register struct linetable
*l
, register int lineno
,
2241 /* BEST is the smallest linenumber > LINENO so far seen,
2242 or 0 if none has been seen so far.
2243 BEST_INDEX identifies the item for it. */
2245 int best_index
= -1;
2254 for (i
= 0; i
< len
; i
++)
2256 register struct linetable_entry
*item
= &(l
->item
[i
]);
2258 if (item
->line
== lineno
)
2260 /* Return the first (lowest address) entry which matches. */
2265 if (item
->line
> lineno
&& (best
== 0 || item
->line
< best
))
2272 /* If we got here, we didn't get an exact match. */
2279 find_pc_line_pc_range (CORE_ADDR pc
, CORE_ADDR
*startptr
, CORE_ADDR
*endptr
)
2281 struct symtab_and_line sal
;
2282 sal
= find_pc_line (pc
, 0);
2285 return sal
.symtab
!= 0;
2288 /* Given a function symbol SYM, find the symtab and line for the start
2290 If the argument FUNFIRSTLINE is nonzero, we want the first line
2291 of real code inside the function. */
2293 struct symtab_and_line
2294 find_function_start_sal (struct symbol
*sym
, int funfirstline
)
2297 struct symtab_and_line sal
;
2299 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
));
2300 fixup_symbol_section (sym
, NULL
);
2302 { /* skip "first line" of function (which is actually its prologue) */
2303 asection
*section
= SYMBOL_BFD_SECTION (sym
);
2304 /* If function is in an unmapped overlay, use its unmapped LMA
2305 address, so that SKIP_PROLOGUE has something unique to work on */
2306 if (section_is_overlay (section
) &&
2307 !section_is_mapped (section
))
2308 pc
= overlay_unmapped_address (pc
, section
);
2310 pc
+= FUNCTION_START_OFFSET
;
2311 pc
= SKIP_PROLOGUE (pc
);
2313 /* For overlays, map pc back into its mapped VMA range */
2314 pc
= overlay_mapped_address (pc
, section
);
2316 sal
= find_pc_sect_line (pc
, SYMBOL_BFD_SECTION (sym
), 0);
2318 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
2319 /* Convex: no need to suppress code on first line, if any */
2322 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
2323 line is still part of the same function. */
2325 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) <= sal
.end
2326 && sal
.end
< BLOCK_END (SYMBOL_BLOCK_VALUE (sym
)))
2328 /* First pc of next line */
2330 /* Recalculate the line number (might not be N+1). */
2331 sal
= find_pc_sect_line (pc
, SYMBOL_BFD_SECTION (sym
), 0);
2339 /* If P is of the form "operator[ \t]+..." where `...' is
2340 some legitimate operator text, return a pointer to the
2341 beginning of the substring of the operator text.
2342 Otherwise, return "". */
2344 operator_chars (char *p
, char **end
)
2347 if (strncmp (p
, "operator", 8))
2351 /* Don't get faked out by `operator' being part of a longer
2353 if (isalpha (*p
) || *p
== '_' || *p
== '$' || *p
== '\0')
2356 /* Allow some whitespace between `operator' and the operator symbol. */
2357 while (*p
== ' ' || *p
== '\t')
2360 /* Recognize 'operator TYPENAME'. */
2362 if (isalpha (*p
) || *p
== '_' || *p
== '$')
2364 register char *q
= p
+ 1;
2365 while (isalnum (*q
) || *q
== '_' || *q
== '$')
2374 case '\\': /* regexp quoting */
2377 if (p
[2] == '=') /* 'operator\*=' */
2379 else /* 'operator\*' */
2383 else if (p
[1] == '[')
2386 error ("mismatched quoting on brackets, try 'operator\\[\\]'");
2387 else if (p
[2] == '\\' && p
[3] == ']')
2389 *end
= p
+ 4; /* 'operator\[\]' */
2393 error ("nothing is allowed between '[' and ']'");
2397 /* Gratuitous qoute: skip it and move on. */
2419 if (p
[0] == '-' && p
[1] == '>')
2421 /* Struct pointer member operator 'operator->'. */
2424 *end
= p
+ 3; /* 'operator->*' */
2427 else if (p
[2] == '\\')
2429 *end
= p
+ 4; /* Hopefully 'operator->\*' */
2434 *end
= p
+ 2; /* 'operator->' */
2438 if (p
[1] == '=' || p
[1] == p
[0])
2449 error ("`operator ()' must be specified without whitespace in `()'");
2454 error ("`operator ?:' must be specified without whitespace in `?:'");
2459 error ("`operator []' must be specified without whitespace in `[]'");
2463 error ("`operator %s' not supported", p
);
2472 /* If FILE is not already in the table of files, return zero;
2473 otherwise return non-zero. Optionally add FILE to the table if ADD
2474 is non-zero. If *FIRST is non-zero, forget the old table
2477 filename_seen (const char *file
, int add
, int *first
)
2479 /* Table of files seen so far. */
2480 static const char **tab
= NULL
;
2481 /* Allocated size of tab in elements.
2482 Start with one 256-byte block (when using GNU malloc.c).
2483 24 is the malloc overhead when range checking is in effect. */
2484 static int tab_alloc_size
= (256 - 24) / sizeof (char *);
2485 /* Current size of tab in elements. */
2486 static int tab_cur_size
;
2492 tab
= (const char **) xmalloc (tab_alloc_size
* sizeof (*tab
));
2496 /* Is FILE in tab? */
2497 for (p
= tab
; p
< tab
+ tab_cur_size
; p
++)
2498 if (strcmp (*p
, file
) == 0)
2501 /* No; maybe add it to tab. */
2504 if (tab_cur_size
== tab_alloc_size
)
2506 tab_alloc_size
*= 2;
2507 tab
= (const char **) xrealloc ((char *) tab
,
2508 tab_alloc_size
* sizeof (*tab
));
2510 tab
[tab_cur_size
++] = file
;
2516 /* Slave routine for sources_info. Force line breaks at ,'s.
2517 NAME is the name to print and *FIRST is nonzero if this is the first
2518 name printed. Set *FIRST to zero. */
2520 output_source_filename (char *name
, int *first
)
2522 /* Since a single source file can result in several partial symbol
2523 tables, we need to avoid printing it more than once. Note: if
2524 some of the psymtabs are read in and some are not, it gets
2525 printed both under "Source files for which symbols have been
2526 read" and "Source files for which symbols will be read in on
2527 demand". I consider this a reasonable way to deal with the
2528 situation. I'm not sure whether this can also happen for
2529 symtabs; it doesn't hurt to check. */
2531 /* Was NAME already seen? */
2532 if (filename_seen (name
, 1, first
))
2534 /* Yes; don't print it again. */
2537 /* No; print it and reset *FIRST. */
2544 printf_filtered (", ");
2548 fputs_filtered (name
, gdb_stdout
);
2552 sources_info (char *ignore
, int from_tty
)
2554 register struct symtab
*s
;
2555 register struct partial_symtab
*ps
;
2556 register struct objfile
*objfile
;
2559 if (!have_full_symbols () && !have_partial_symbols ())
2561 error ("No symbol table is loaded. Use the \"file\" command.");
2564 printf_filtered ("Source files for which symbols have been read in:\n\n");
2567 ALL_SYMTABS (objfile
, s
)
2569 output_source_filename (s
->filename
, &first
);
2571 printf_filtered ("\n\n");
2573 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2576 ALL_PSYMTABS (objfile
, ps
)
2580 output_source_filename (ps
->filename
, &first
);
2583 printf_filtered ("\n");
2587 file_matches (char *file
, char *files
[], int nfiles
)
2591 if (file
!= NULL
&& nfiles
!= 0)
2593 for (i
= 0; i
< nfiles
; i
++)
2595 if (strcmp (files
[i
], lbasename (file
)) == 0)
2599 else if (nfiles
== 0)
2604 /* Free any memory associated with a search. */
2606 free_search_symbols (struct symbol_search
*symbols
)
2608 struct symbol_search
*p
;
2609 struct symbol_search
*next
;
2611 for (p
= symbols
; p
!= NULL
; p
= next
)
2619 do_free_search_symbols_cleanup (void *symbols
)
2621 free_search_symbols (symbols
);
2625 make_cleanup_free_search_symbols (struct symbol_search
*symbols
)
2627 return make_cleanup (do_free_search_symbols_cleanup
, symbols
);
2630 /* Helper function for sort_search_symbols and qsort. Can only
2631 sort symbols, not minimal symbols. */
2633 compare_search_syms (const void *sa
, const void *sb
)
2635 struct symbol_search
**sym_a
= (struct symbol_search
**) sa
;
2636 struct symbol_search
**sym_b
= (struct symbol_search
**) sb
;
2638 return strcmp (SYMBOL_SOURCE_NAME ((*sym_a
)->symbol
),
2639 SYMBOL_SOURCE_NAME ((*sym_b
)->symbol
));
2642 /* Sort the ``nfound'' symbols in the list after prevtail. Leave
2643 prevtail where it is, but update its next pointer to point to
2644 the first of the sorted symbols. */
2645 static struct symbol_search
*
2646 sort_search_symbols (struct symbol_search
*prevtail
, int nfound
)
2648 struct symbol_search
**symbols
, *symp
, *old_next
;
2651 symbols
= (struct symbol_search
**) xmalloc (sizeof (struct symbol_search
*)
2653 symp
= prevtail
->next
;
2654 for (i
= 0; i
< nfound
; i
++)
2659 /* Generally NULL. */
2662 qsort (symbols
, nfound
, sizeof (struct symbol_search
*),
2663 compare_search_syms
);
2666 for (i
= 0; i
< nfound
; i
++)
2668 symp
->next
= symbols
[i
];
2671 symp
->next
= old_next
;
2677 /* Search the symbol table for matches to the regular expression REGEXP,
2678 returning the results in *MATCHES.
2680 Only symbols of KIND are searched:
2681 FUNCTIONS_NAMESPACE - search all functions
2682 TYPES_NAMESPACE - search all type names
2683 METHODS_NAMESPACE - search all methods NOT IMPLEMENTED
2684 VARIABLES_NAMESPACE - search all symbols, excluding functions, type names,
2685 and constants (enums)
2687 free_search_symbols should be called when *MATCHES is no longer needed.
2689 The results are sorted locally; each symtab's global and static blocks are
2690 separately alphabetized.
2693 search_symbols (char *regexp
, namespace_enum kind
, int nfiles
, char *files
[],
2694 struct symbol_search
**matches
)
2696 register struct symtab
*s
;
2697 register struct partial_symtab
*ps
;
2698 register struct blockvector
*bv
;
2699 struct blockvector
*prev_bv
= 0;
2700 register struct block
*b
;
2703 register struct symbol
*sym
;
2704 struct partial_symbol
**psym
;
2705 struct objfile
*objfile
;
2706 struct minimal_symbol
*msymbol
;
2709 static enum minimal_symbol_type types
[]
2711 {mst_data
, mst_text
, mst_abs
, mst_unknown
};
2712 static enum minimal_symbol_type types2
[]
2714 {mst_bss
, mst_file_text
, mst_abs
, mst_unknown
};
2715 static enum minimal_symbol_type types3
[]
2717 {mst_file_data
, mst_solib_trampoline
, mst_abs
, mst_unknown
};
2718 static enum minimal_symbol_type types4
[]
2720 {mst_file_bss
, mst_text
, mst_abs
, mst_unknown
};
2721 enum minimal_symbol_type ourtype
;
2722 enum minimal_symbol_type ourtype2
;
2723 enum minimal_symbol_type ourtype3
;
2724 enum minimal_symbol_type ourtype4
;
2725 struct symbol_search
*sr
;
2726 struct symbol_search
*psr
;
2727 struct symbol_search
*tail
;
2728 struct cleanup
*old_chain
= NULL
;
2730 if (kind
< VARIABLES_NAMESPACE
)
2731 error ("must search on specific namespace");
2733 ourtype
= types
[(int) (kind
- VARIABLES_NAMESPACE
)];
2734 ourtype2
= types2
[(int) (kind
- VARIABLES_NAMESPACE
)];
2735 ourtype3
= types3
[(int) (kind
- VARIABLES_NAMESPACE
)];
2736 ourtype4
= types4
[(int) (kind
- VARIABLES_NAMESPACE
)];
2738 sr
= *matches
= NULL
;
2743 /* Make sure spacing is right for C++ operators.
2744 This is just a courtesy to make the matching less sensitive
2745 to how many spaces the user leaves between 'operator'
2746 and <TYPENAME> or <OPERATOR>. */
2748 char *opname
= operator_chars (regexp
, &opend
);
2751 int fix
= -1; /* -1 means ok; otherwise number of spaces needed. */
2752 if (isalpha (*opname
) || *opname
== '_' || *opname
== '$')
2754 /* There should 1 space between 'operator' and 'TYPENAME'. */
2755 if (opname
[-1] != ' ' || opname
[-2] == ' ')
2760 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2761 if (opname
[-1] == ' ')
2764 /* If wrong number of spaces, fix it. */
2767 char *tmp
= (char *) alloca (8 + fix
+ strlen (opname
) + 1);
2768 sprintf (tmp
, "operator%.*s%s", fix
, " ", opname
);
2773 if (0 != (val
= re_comp (regexp
)))
2774 error ("Invalid regexp (%s): %s", val
, regexp
);
2777 /* Search through the partial symtabs *first* for all symbols
2778 matching the regexp. That way we don't have to reproduce all of
2779 the machinery below. */
2781 ALL_PSYMTABS (objfile
, ps
)
2783 struct partial_symbol
**bound
, **gbound
, **sbound
;
2789 gbound
= objfile
->global_psymbols
.list
+ ps
->globals_offset
+ ps
->n_global_syms
;
2790 sbound
= objfile
->static_psymbols
.list
+ ps
->statics_offset
+ ps
->n_static_syms
;
2793 /* Go through all of the symbols stored in a partial
2794 symtab in one loop. */
2795 psym
= objfile
->global_psymbols
.list
+ ps
->globals_offset
;
2800 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
2802 psym
= objfile
->static_psymbols
.list
+ ps
->statics_offset
;
2813 /* If it would match (logic taken from loop below)
2814 load the file and go on to the next one */
2815 if (file_matches (ps
->filename
, files
, nfiles
)
2816 && ((regexp
== NULL
|| SYMBOL_MATCHES_REGEXP (*psym
))
2817 && ((kind
== VARIABLES_NAMESPACE
&& SYMBOL_CLASS (*psym
) != LOC_TYPEDEF
2818 && SYMBOL_CLASS (*psym
) != LOC_BLOCK
)
2819 || (kind
== FUNCTIONS_NAMESPACE
&& SYMBOL_CLASS (*psym
) == LOC_BLOCK
)
2820 || (kind
== TYPES_NAMESPACE
&& SYMBOL_CLASS (*psym
) == LOC_TYPEDEF
)
2821 || (kind
== METHODS_NAMESPACE
&& SYMBOL_CLASS (*psym
) == LOC_BLOCK
))))
2823 PSYMTAB_TO_SYMTAB (ps
);
2831 /* Here, we search through the minimal symbol tables for functions
2832 and variables that match, and force their symbols to be read.
2833 This is in particular necessary for demangled variable names,
2834 which are no longer put into the partial symbol tables.
2835 The symbol will then be found during the scan of symtabs below.
2837 For functions, find_pc_symtab should succeed if we have debug info
2838 for the function, for variables we have to call lookup_symbol
2839 to determine if the variable has debug info.
2840 If the lookup fails, set found_misc so that we will rescan to print
2841 any matching symbols without debug info.
2844 if (nfiles
== 0 && (kind
== VARIABLES_NAMESPACE
|| kind
== FUNCTIONS_NAMESPACE
))
2846 ALL_MSYMBOLS (objfile
, msymbol
)
2848 if (MSYMBOL_TYPE (msymbol
) == ourtype
||
2849 MSYMBOL_TYPE (msymbol
) == ourtype2
||
2850 MSYMBOL_TYPE (msymbol
) == ourtype3
||
2851 MSYMBOL_TYPE (msymbol
) == ourtype4
)
2853 if (regexp
== NULL
|| SYMBOL_MATCHES_REGEXP (msymbol
))
2855 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol
)))
2857 if (kind
== FUNCTIONS_NAMESPACE
2858 || lookup_symbol (SYMBOL_NAME (msymbol
),
2859 (struct block
*) NULL
,
2861 0, (struct symtab
**) NULL
) == NULL
)
2869 ALL_SYMTABS (objfile
, s
)
2871 bv
= BLOCKVECTOR (s
);
2872 /* Often many files share a blockvector.
2873 Scan each blockvector only once so that
2874 we don't get every symbol many times.
2875 It happens that the first symtab in the list
2876 for any given blockvector is the main file. */
2878 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
2880 struct symbol_search
*prevtail
= tail
;
2882 b
= BLOCKVECTOR_BLOCK (bv
, i
);
2883 ALL_BLOCK_SYMBOLS (b
, j
, sym
)
2886 if (file_matches (s
->filename
, files
, nfiles
)
2887 && ((regexp
== NULL
|| SYMBOL_MATCHES_REGEXP (sym
))
2888 && ((kind
== VARIABLES_NAMESPACE
&& SYMBOL_CLASS (sym
) != LOC_TYPEDEF
2889 && SYMBOL_CLASS (sym
) != LOC_BLOCK
2890 && SYMBOL_CLASS (sym
) != LOC_CONST
)
2891 || (kind
== FUNCTIONS_NAMESPACE
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
2892 || (kind
== TYPES_NAMESPACE
&& SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
2893 || (kind
== METHODS_NAMESPACE
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
))))
2896 psr
= (struct symbol_search
*) xmalloc (sizeof (struct symbol_search
));
2900 psr
->msymbol
= NULL
;
2912 if (prevtail
== NULL
)
2914 struct symbol_search dummy
;
2917 tail
= sort_search_symbols (&dummy
, nfound
);
2920 old_chain
= make_cleanup_free_search_symbols (sr
);
2923 tail
= sort_search_symbols (prevtail
, nfound
);
2929 /* If there are no eyes, avoid all contact. I mean, if there are
2930 no debug symbols, then print directly from the msymbol_vector. */
2932 if (found_misc
|| kind
!= FUNCTIONS_NAMESPACE
)
2934 ALL_MSYMBOLS (objfile
, msymbol
)
2936 if (MSYMBOL_TYPE (msymbol
) == ourtype
||
2937 MSYMBOL_TYPE (msymbol
) == ourtype2
||
2938 MSYMBOL_TYPE (msymbol
) == ourtype3
||
2939 MSYMBOL_TYPE (msymbol
) == ourtype4
)
2941 if (regexp
== NULL
|| SYMBOL_MATCHES_REGEXP (msymbol
))
2943 /* Functions: Look up by address. */
2944 if (kind
!= FUNCTIONS_NAMESPACE
||
2945 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol
))))
2947 /* Variables/Absolutes: Look up by name */
2948 if (lookup_symbol (SYMBOL_NAME (msymbol
),
2949 (struct block
*) NULL
, VAR_NAMESPACE
,
2950 0, (struct symtab
**) NULL
) == NULL
)
2953 psr
= (struct symbol_search
*) xmalloc (sizeof (struct symbol_search
));
2955 psr
->msymbol
= msymbol
;
2962 old_chain
= make_cleanup_free_search_symbols (sr
);
2976 discard_cleanups (old_chain
);
2979 /* Helper function for symtab_symbol_info, this function uses
2980 the data returned from search_symbols() to print information
2981 regarding the match to gdb_stdout.
2984 print_symbol_info (namespace_enum kind
, struct symtab
*s
, struct symbol
*sym
,
2985 int block
, char *last
)
2987 if (last
== NULL
|| strcmp (last
, s
->filename
) != 0)
2989 fputs_filtered ("\nFile ", gdb_stdout
);
2990 fputs_filtered (s
->filename
, gdb_stdout
);
2991 fputs_filtered (":\n", gdb_stdout
);
2994 if (kind
!= TYPES_NAMESPACE
&& block
== STATIC_BLOCK
)
2995 printf_filtered ("static ");
2997 /* Typedef that is not a C++ class */
2998 if (kind
== TYPES_NAMESPACE
2999 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
)
3000 typedef_print (SYMBOL_TYPE (sym
), sym
, gdb_stdout
);
3001 /* variable, func, or typedef-that-is-c++-class */
3002 else if (kind
< TYPES_NAMESPACE
||
3003 (kind
== TYPES_NAMESPACE
&&
3004 SYMBOL_NAMESPACE (sym
) == STRUCT_NAMESPACE
))
3006 type_print (SYMBOL_TYPE (sym
),
3007 (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
3008 ? "" : SYMBOL_SOURCE_NAME (sym
)),
3011 printf_filtered (";\n");
3015 /* This help function for symtab_symbol_info() prints information
3016 for non-debugging symbols to gdb_stdout.
3019 print_msymbol_info (struct minimal_symbol
*msymbol
)
3023 if (TARGET_ADDR_BIT
<= 32)
3024 tmp
= local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol
)
3025 & (CORE_ADDR
) 0xffffffff,
3028 tmp
= local_hex_string_custom (SYMBOL_VALUE_ADDRESS (msymbol
),
3030 printf_filtered ("%s %s\n",
3031 tmp
, SYMBOL_SOURCE_NAME (msymbol
));
3034 /* This is the guts of the commands "info functions", "info types", and
3035 "info variables". It calls search_symbols to find all matches and then
3036 print_[m]symbol_info to print out some useful information about the
3040 symtab_symbol_info (char *regexp
, namespace_enum kind
, int from_tty
)
3042 static char *classnames
[]
3044 {"variable", "function", "type", "method"};
3045 struct symbol_search
*symbols
;
3046 struct symbol_search
*p
;
3047 struct cleanup
*old_chain
;
3048 char *last_filename
= NULL
;
3051 /* must make sure that if we're interrupted, symbols gets freed */
3052 search_symbols (regexp
, kind
, 0, (char **) NULL
, &symbols
);
3053 old_chain
= make_cleanup_free_search_symbols (symbols
);
3055 printf_filtered (regexp
3056 ? "All %ss matching regular expression \"%s\":\n"
3057 : "All defined %ss:\n",
3058 classnames
[(int) (kind
- VARIABLES_NAMESPACE
)], regexp
);
3060 for (p
= symbols
; p
!= NULL
; p
= p
->next
)
3064 if (p
->msymbol
!= NULL
)
3068 printf_filtered ("\nNon-debugging symbols:\n");
3071 print_msymbol_info (p
->msymbol
);
3075 print_symbol_info (kind
,
3080 last_filename
= p
->symtab
->filename
;
3084 do_cleanups (old_chain
);
3088 variables_info (char *regexp
, int from_tty
)
3090 symtab_symbol_info (regexp
, VARIABLES_NAMESPACE
, from_tty
);
3094 functions_info (char *regexp
, int from_tty
)
3096 symtab_symbol_info (regexp
, FUNCTIONS_NAMESPACE
, from_tty
);
3101 types_info (char *regexp
, int from_tty
)
3103 symtab_symbol_info (regexp
, TYPES_NAMESPACE
, from_tty
);
3106 /* Breakpoint all functions matching regular expression. */
3109 rbreak_command_wrapper (char *regexp
, int from_tty
)
3111 rbreak_command (regexp
, from_tty
);
3115 rbreak_command (char *regexp
, int from_tty
)
3117 struct symbol_search
*ss
;
3118 struct symbol_search
*p
;
3119 struct cleanup
*old_chain
;
3121 search_symbols (regexp
, FUNCTIONS_NAMESPACE
, 0, (char **) NULL
, &ss
);
3122 old_chain
= make_cleanup_free_search_symbols (ss
);
3124 for (p
= ss
; p
!= NULL
; p
= p
->next
)
3126 if (p
->msymbol
== NULL
)
3128 char *string
= (char *) alloca (strlen (p
->symtab
->filename
)
3129 + strlen (SYMBOL_NAME (p
->symbol
))
3131 strcpy (string
, p
->symtab
->filename
);
3132 strcat (string
, ":'");
3133 strcat (string
, SYMBOL_NAME (p
->symbol
));
3134 strcat (string
, "'");
3135 break_command (string
, from_tty
);
3136 print_symbol_info (FUNCTIONS_NAMESPACE
,
3140 p
->symtab
->filename
);
3144 break_command (SYMBOL_NAME (p
->msymbol
), from_tty
);
3145 printf_filtered ("<function, no debug info> %s;\n",
3146 SYMBOL_SOURCE_NAME (p
->msymbol
));
3150 do_cleanups (old_chain
);
3154 /* Return Nonzero if block a is lexically nested within block b,
3155 or if a and b have the same pc range.
3156 Return zero otherwise. */
3158 contained_in (struct block
*a
, struct block
*b
)
3162 return BLOCK_START (a
) >= BLOCK_START (b
)
3163 && BLOCK_END (a
) <= BLOCK_END (b
);
3167 /* Helper routine for make_symbol_completion_list. */
3169 static int return_val_size
;
3170 static int return_val_index
;
3171 static char **return_val
;
3173 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
3175 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
3176 /* Put only the mangled name on the list. */ \
3177 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
3178 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
3179 completion_list_add_name \
3180 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
3182 completion_list_add_name \
3183 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
3186 /* Test to see if the symbol specified by SYMNAME (which is already
3187 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3188 characters. If so, add it to the current completion list. */
3191 completion_list_add_name (char *symname
, char *sym_text
, int sym_text_len
,
3192 char *text
, char *word
)
3197 /* clip symbols that cannot match */
3199 if (strncmp (symname
, sym_text
, sym_text_len
) != 0)
3204 /* We have a match for a completion, so add SYMNAME to the current list
3205 of matches. Note that the name is moved to freshly malloc'd space. */
3209 if (word
== sym_text
)
3211 new = xmalloc (strlen (symname
) + 5);
3212 strcpy (new, symname
);
3214 else if (word
> sym_text
)
3216 /* Return some portion of symname. */
3217 new = xmalloc (strlen (symname
) + 5);
3218 strcpy (new, symname
+ (word
- sym_text
));
3222 /* Return some of SYM_TEXT plus symname. */
3223 new = xmalloc (strlen (symname
) + (sym_text
- word
) + 5);
3224 strncpy (new, word
, sym_text
- word
);
3225 new[sym_text
- word
] = '\0';
3226 strcat (new, symname
);
3229 if (return_val_index
+ 3 > return_val_size
)
3231 newsize
= (return_val_size
*= 2) * sizeof (char *);
3232 return_val
= (char **) xrealloc ((char *) return_val
, newsize
);
3234 return_val
[return_val_index
++] = new;
3235 return_val
[return_val_index
] = NULL
;
3239 /* Return a NULL terminated array of all symbols (regardless of class)
3240 which begin by matching TEXT. If the answer is no symbols, then
3241 the return value is an array which contains only a NULL pointer.
3243 Problem: All of the symbols have to be copied because readline frees them.
3244 I'm not going to worry about this; hopefully there won't be that many. */
3247 make_symbol_completion_list (char *text
, char *word
)
3249 register struct symbol
*sym
;
3250 register struct symtab
*s
;
3251 register struct partial_symtab
*ps
;
3252 register struct minimal_symbol
*msymbol
;
3253 register struct objfile
*objfile
;
3254 register struct block
*b
, *surrounding_static_block
= 0;
3256 struct partial_symbol
**psym
;
3257 /* The symbol we are completing on. Points in same buffer as text. */
3259 /* Length of sym_text. */
3262 /* Now look for the symbol we are supposed to complete on.
3263 FIXME: This should be language-specific. */
3267 char *quote_pos
= NULL
;
3269 /* First see if this is a quoted string. */
3271 for (p
= text
; *p
!= '\0'; ++p
)
3273 if (quote_found
!= '\0')
3275 if (*p
== quote_found
)
3276 /* Found close quote. */
3278 else if (*p
== '\\' && p
[1] == quote_found
)
3279 /* A backslash followed by the quote character
3280 doesn't end the string. */
3283 else if (*p
== '\'' || *p
== '"')
3289 if (quote_found
== '\'')
3290 /* A string within single quotes can be a symbol, so complete on it. */
3291 sym_text
= quote_pos
+ 1;
3292 else if (quote_found
== '"')
3293 /* A double-quoted string is never a symbol, nor does it make sense
3294 to complete it any other way. */
3296 return_val
= (char **) xmalloc (sizeof (char *));
3297 return_val
[0] = NULL
;
3302 /* It is not a quoted string. Break it based on the characters
3303 which are in symbols. */
3306 if (isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0')
3315 sym_text_len
= strlen (sym_text
);
3317 return_val_size
= 100;
3318 return_val_index
= 0;
3319 return_val
= (char **) xmalloc ((return_val_size
+ 1) * sizeof (char *));
3320 return_val
[0] = NULL
;
3322 /* Look through the partial symtabs for all symbols which begin
3323 by matching SYM_TEXT. Add each one that you find to the list. */
3325 ALL_PSYMTABS (objfile
, ps
)
3327 /* If the psymtab's been read in we'll get it when we search
3328 through the blockvector. */
3332 for (psym
= objfile
->global_psymbols
.list
+ ps
->globals_offset
;
3333 psym
< (objfile
->global_psymbols
.list
+ ps
->globals_offset
3334 + ps
->n_global_syms
);
3337 /* If interrupted, then quit. */
3339 COMPLETION_LIST_ADD_SYMBOL (*psym
, sym_text
, sym_text_len
, text
, word
);
3342 for (psym
= objfile
->static_psymbols
.list
+ ps
->statics_offset
;
3343 psym
< (objfile
->static_psymbols
.list
+ ps
->statics_offset
3344 + ps
->n_static_syms
);
3348 COMPLETION_LIST_ADD_SYMBOL (*psym
, sym_text
, sym_text_len
, text
, word
);
3352 /* At this point scan through the misc symbol vectors and add each
3353 symbol you find to the list. Eventually we want to ignore
3354 anything that isn't a text symbol (everything else will be
3355 handled by the psymtab code above). */
3357 ALL_MSYMBOLS (objfile
, msymbol
)
3360 COMPLETION_LIST_ADD_SYMBOL (msymbol
, sym_text
, sym_text_len
, text
, word
);
3363 /* Search upwards from currently selected frame (so that we can
3364 complete on local vars. */
3366 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
3368 if (!BLOCK_SUPERBLOCK (b
))
3370 surrounding_static_block
= b
; /* For elmin of dups */
3373 /* Also catch fields of types defined in this places which match our
3374 text string. Only complete on types visible from current context. */
3376 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3378 COMPLETION_LIST_ADD_SYMBOL (sym
, sym_text
, sym_text_len
, text
, word
);
3379 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
3381 struct type
*t
= SYMBOL_TYPE (sym
);
3382 enum type_code c
= TYPE_CODE (t
);
3384 if (c
== TYPE_CODE_UNION
|| c
== TYPE_CODE_STRUCT
)
3386 for (j
= TYPE_N_BASECLASSES (t
); j
< TYPE_NFIELDS (t
); j
++)
3388 if (TYPE_FIELD_NAME (t
, j
))
3390 completion_list_add_name (TYPE_FIELD_NAME (t
, j
),
3391 sym_text
, sym_text_len
, text
, word
);
3399 /* Go through the symtabs and check the externs and statics for
3400 symbols which match. */
3402 ALL_SYMTABS (objfile
, s
)
3405 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
3406 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3408 COMPLETION_LIST_ADD_SYMBOL (sym
, sym_text
, sym_text_len
, text
, word
);
3412 ALL_SYMTABS (objfile
, s
)
3415 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
3416 /* Don't do this block twice. */
3417 if (b
== surrounding_static_block
)
3419 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3421 COMPLETION_LIST_ADD_SYMBOL (sym
, sym_text
, sym_text_len
, text
, word
);
3425 return (return_val
);
3428 /* Like make_symbol_completion_list, but returns a list of symbols
3429 defined in a source file FILE. */
3432 make_file_symbol_completion_list (char *text
, char *word
, char *srcfile
)
3434 register struct symbol
*sym
;
3435 register struct symtab
*s
;
3436 register struct block
*b
;
3438 /* The symbol we are completing on. Points in same buffer as text. */
3440 /* Length of sym_text. */
3443 /* Now look for the symbol we are supposed to complete on.
3444 FIXME: This should be language-specific. */
3448 char *quote_pos
= NULL
;
3450 /* First see if this is a quoted string. */
3452 for (p
= text
; *p
!= '\0'; ++p
)
3454 if (quote_found
!= '\0')
3456 if (*p
== quote_found
)
3457 /* Found close quote. */
3459 else if (*p
== '\\' && p
[1] == quote_found
)
3460 /* A backslash followed by the quote character
3461 doesn't end the string. */
3464 else if (*p
== '\'' || *p
== '"')
3470 if (quote_found
== '\'')
3471 /* A string within single quotes can be a symbol, so complete on it. */
3472 sym_text
= quote_pos
+ 1;
3473 else if (quote_found
== '"')
3474 /* A double-quoted string is never a symbol, nor does it make sense
3475 to complete it any other way. */
3477 return_val
= (char **) xmalloc (sizeof (char *));
3478 return_val
[0] = NULL
;
3483 /* It is not a quoted string. Break it based on the characters
3484 which are in symbols. */
3487 if (isalnum (p
[-1]) || p
[-1] == '_' || p
[-1] == '\0')
3496 sym_text_len
= strlen (sym_text
);
3498 return_val_size
= 10;
3499 return_val_index
= 0;
3500 return_val
= (char **) xmalloc ((return_val_size
+ 1) * sizeof (char *));
3501 return_val
[0] = NULL
;
3503 /* Find the symtab for SRCFILE (this loads it if it was not yet read
3505 s
= lookup_symtab (srcfile
);
3508 /* Maybe they typed the file with leading directories, while the
3509 symbol tables record only its basename. */
3510 const char *tail
= lbasename (srcfile
);
3513 s
= lookup_symtab (tail
);
3516 /* If we have no symtab for that file, return an empty list. */
3518 return (return_val
);
3520 /* Go through this symtab and check the externs and statics for
3521 symbols which match. */
3523 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
3524 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3526 COMPLETION_LIST_ADD_SYMBOL (sym
, sym_text
, sym_text_len
, text
, word
);
3529 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
3530 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3532 COMPLETION_LIST_ADD_SYMBOL (sym
, sym_text
, sym_text_len
, text
, word
);
3535 return (return_val
);
3538 /* A helper function for make_source_files_completion_list. It adds
3539 another file name to a list of possible completions, growing the
3540 list as necessary. */
3543 add_filename_to_list (const char *fname
, char *text
, char *word
,
3544 char ***list
, int *list_used
, int *list_alloced
)
3547 size_t fnlen
= strlen (fname
);
3549 if (*list_used
+ 1 >= *list_alloced
)
3552 *list
= (char **) xrealloc ((char *) *list
,
3553 *list_alloced
* sizeof (char *));
3558 /* Return exactly fname. */
3559 new = xmalloc (fnlen
+ 5);
3560 strcpy (new, fname
);
3562 else if (word
> text
)
3564 /* Return some portion of fname. */
3565 new = xmalloc (fnlen
+ 5);
3566 strcpy (new, fname
+ (word
- text
));
3570 /* Return some of TEXT plus fname. */
3571 new = xmalloc (fnlen
+ (text
- word
) + 5);
3572 strncpy (new, word
, text
- word
);
3573 new[text
- word
] = '\0';
3574 strcat (new, fname
);
3576 (*list
)[*list_used
] = new;
3577 (*list
)[++*list_used
] = NULL
;
3581 not_interesting_fname (const char *fname
)
3583 static const char *illegal_aliens
[] = {
3584 "_globals_", /* inserted by coff_symtab_read */
3589 for (i
= 0; illegal_aliens
[i
]; i
++)
3591 if (strcmp (fname
, illegal_aliens
[i
]) == 0)
3597 /* Return a NULL terminated array of all source files whose names
3598 begin with matching TEXT. The file names are looked up in the
3599 symbol tables of this program. If the answer is no matchess, then
3600 the return value is an array which contains only a NULL pointer. */
3603 make_source_files_completion_list (char *text
, char *word
)
3605 register struct symtab
*s
;
3606 register struct partial_symtab
*ps
;
3607 register struct objfile
*objfile
;
3609 int list_alloced
= 1;
3611 size_t text_len
= strlen (text
);
3612 char **list
= (char **) xmalloc (list_alloced
* sizeof (char *));
3613 const char *base_name
;
3617 if (!have_full_symbols () && !have_partial_symbols ())
3620 ALL_SYMTABS (objfile
, s
)
3622 if (not_interesting_fname (s
->filename
))
3624 if (!filename_seen (s
->filename
, 1, &first
)
3625 #if HAVE_DOS_BASED_FILE_SYSTEM
3626 && strncasecmp (s
->filename
, text
, text_len
) == 0
3628 && strncmp (s
->filename
, text
, text_len
) == 0
3632 /* This file matches for a completion; add it to the current
3634 add_filename_to_list (s
->filename
, text
, word
,
3635 &list
, &list_used
, &list_alloced
);
3639 /* NOTE: We allow the user to type a base name when the
3640 debug info records leading directories, but not the other
3641 way around. This is what subroutines of breakpoint
3642 command do when they parse file names. */
3643 base_name
= lbasename (s
->filename
);
3644 if (base_name
!= s
->filename
3645 && !filename_seen (base_name
, 1, &first
)
3646 #if HAVE_DOS_BASED_FILE_SYSTEM
3647 && strncasecmp (base_name
, text
, text_len
) == 0
3649 && strncmp (base_name
, text
, text_len
) == 0
3652 add_filename_to_list (base_name
, text
, word
,
3653 &list
, &list_used
, &list_alloced
);
3657 ALL_PSYMTABS (objfile
, ps
)
3659 if (not_interesting_fname (ps
->filename
))
3663 if (!filename_seen (ps
->filename
, 1, &first
)
3664 #if HAVE_DOS_BASED_FILE_SYSTEM
3665 && strncasecmp (ps
->filename
, text
, text_len
) == 0
3667 && strncmp (ps
->filename
, text
, text_len
) == 0
3671 /* This file matches for a completion; add it to the
3672 current list of matches. */
3673 add_filename_to_list (ps
->filename
, text
, word
,
3674 &list
, &list_used
, &list_alloced
);
3679 base_name
= lbasename (ps
->filename
);
3680 if (base_name
!= ps
->filename
3681 && !filename_seen (base_name
, 1, &first
)
3682 #if HAVE_DOS_BASED_FILE_SYSTEM
3683 && strncasecmp (base_name
, text
, text_len
) == 0
3685 && strncmp (base_name
, text
, text_len
) == 0
3688 add_filename_to_list (base_name
, text
, word
,
3689 &list
, &list_used
, &list_alloced
);
3697 /* Determine if PC is in the prologue of a function. The prologue is the area
3698 between the first instruction of a function, and the first executable line.
3699 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3701 If non-zero, func_start is where we think the prologue starts, possibly
3702 by previous examination of symbol table information.
3706 in_prologue (CORE_ADDR pc
, CORE_ADDR func_start
)
3708 struct symtab_and_line sal
;
3709 CORE_ADDR func_addr
, func_end
;
3711 /* We have several sources of information we can consult to figure
3713 - Compilers usually emit line number info that marks the prologue
3714 as its own "source line". So the ending address of that "line"
3715 is the end of the prologue. If available, this is the most
3717 - The minimal symbols and partial symbols, which can usually tell
3718 us the starting and ending addresses of a function.
3719 - If we know the function's start address, we can call the
3720 architecture-defined SKIP_PROLOGUE function to analyze the
3721 instruction stream and guess where the prologue ends.
3722 - Our `func_start' argument; if non-zero, this is the caller's
3723 best guess as to the function's entry point. At the time of
3724 this writing, handle_inferior_event doesn't get this right, so
3725 it should be our last resort. */
3727 /* Consult the partial symbol table, to find which function
3729 if (! find_pc_partial_function (pc
, NULL
, &func_addr
, &func_end
))
3731 CORE_ADDR prologue_end
;
3733 /* We don't even have minsym information, so fall back to using
3734 func_start, if given. */
3736 return 1; /* We *might* be in a prologue. */
3738 prologue_end
= SKIP_PROLOGUE (func_start
);
3740 return func_start
<= pc
&& pc
< prologue_end
;
3743 /* If we have line number information for the function, that's
3744 usually pretty reliable. */
3745 sal
= find_pc_line (func_addr
, 0);
3747 /* Now sal describes the source line at the function's entry point,
3748 which (by convention) is the prologue. The end of that "line",
3749 sal.end, is the end of the prologue.
3751 Note that, for functions whose source code is all on a single
3752 line, the line number information doesn't always end up this way.
3753 So we must verify that our purported end-of-prologue address is
3754 *within* the function, not at its start or end. */
3756 || sal
.end
<= func_addr
3757 || func_end
<= sal
.end
)
3759 /* We don't have any good line number info, so use the minsym
3760 information, together with the architecture-specific prologue
3762 CORE_ADDR prologue_end
= SKIP_PROLOGUE (func_addr
);
3764 return func_addr
<= pc
&& pc
< prologue_end
;
3767 /* We have line number info, and it looks good. */
3768 return func_addr
<= pc
&& pc
< sal
.end
;
3772 /* Begin overload resolution functions */
3775 remove_params (const char *demangled_name
)
3781 if (demangled_name
== NULL
)
3784 /* First find the end of the arg list. */
3785 argp
= strrchr (demangled_name
, ')');
3789 /* Back up to the beginning. */
3792 while (argp
-- > demangled_name
)
3796 else if (*argp
== '(')
3805 internal_error (__FILE__
, __LINE__
,
3806 "bad demangled name %s\n", demangled_name
);
3807 while (argp
[-1] == ' ' && argp
> demangled_name
)
3810 new_name
= xmalloc (argp
- demangled_name
+ 1);
3811 memcpy (new_name
, demangled_name
, argp
- demangled_name
);
3812 new_name
[argp
- demangled_name
] = '\0';
3816 /* Helper routine for make_symbol_completion_list. */
3818 static int sym_return_val_size
;
3819 static int sym_return_val_index
;
3820 static struct symbol
**sym_return_val
;
3822 /* Test to see if the symbol specified by SYMNAME (which is already
3823 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
3824 characters. If so, add it to the current completion list. */
3827 overload_list_add_symbol (struct symbol
*sym
, char *oload_name
)
3833 /* If there is no type information, we can't do anything, so skip */
3834 if (SYMBOL_TYPE (sym
) == NULL
)
3837 /* skip any symbols that we've already considered. */
3838 for (i
= 0; i
< sym_return_val_index
; ++i
)
3839 if (!strcmp (SYMBOL_NAME (sym
), SYMBOL_NAME (sym_return_val
[i
])))
3842 /* Get the demangled name without parameters */
3843 sym_name
= remove_params (SYMBOL_DEMANGLED_NAME (sym
));
3847 /* skip symbols that cannot match */
3848 if (strcmp (sym_name
, oload_name
) != 0)
3856 /* We have a match for an overload instance, so add SYM to the current list
3857 * of overload instances */
3858 if (sym_return_val_index
+ 3 > sym_return_val_size
)
3860 newsize
= (sym_return_val_size
*= 2) * sizeof (struct symbol
*);
3861 sym_return_val
= (struct symbol
**) xrealloc ((char *) sym_return_val
, newsize
);
3863 sym_return_val
[sym_return_val_index
++] = sym
;
3864 sym_return_val
[sym_return_val_index
] = NULL
;
3867 /* Return a null-terminated list of pointers to function symbols that
3868 * match name of the supplied symbol FSYM.
3869 * This is used in finding all overloaded instances of a function name.
3870 * This has been modified from make_symbol_completion_list. */
3874 make_symbol_overload_list (struct symbol
*fsym
)
3876 register struct symbol
*sym
;
3877 register struct symtab
*s
;
3878 register struct partial_symtab
*ps
;
3879 register struct objfile
*objfile
;
3880 register struct block
*b
, *surrounding_static_block
= 0;
3882 /* The name we are completing on. */
3883 char *oload_name
= NULL
;
3884 /* Length of name. */
3885 int oload_name_len
= 0;
3887 /* Look for the symbol we are supposed to complete on. */
3889 oload_name
= remove_params (SYMBOL_DEMANGLED_NAME (fsym
));
3892 sym_return_val_size
= 1;
3893 sym_return_val
= (struct symbol
**) xmalloc (2 * sizeof (struct symbol
*));
3894 sym_return_val
[0] = fsym
;
3895 sym_return_val
[1] = NULL
;
3897 return sym_return_val
;
3899 oload_name_len
= strlen (oload_name
);
3901 sym_return_val_size
= 100;
3902 sym_return_val_index
= 0;
3903 sym_return_val
= (struct symbol
**) xmalloc ((sym_return_val_size
+ 1) * sizeof (struct symbol
*));
3904 sym_return_val
[0] = NULL
;
3906 /* Look through the partial symtabs for all symbols which begin
3907 by matching OLOAD_NAME. Make sure we read that symbol table in. */
3909 ALL_PSYMTABS (objfile
, ps
)
3911 struct partial_symbol
**psym
;
3913 /* If the psymtab's been read in we'll get it when we search
3914 through the blockvector. */
3918 for (psym
= objfile
->global_psymbols
.list
+ ps
->globals_offset
;
3919 psym
< (objfile
->global_psymbols
.list
+ ps
->globals_offset
3920 + ps
->n_global_syms
);
3923 /* If interrupted, then quit. */
3925 /* This will cause the symbol table to be read if it has not yet been */
3926 s
= PSYMTAB_TO_SYMTAB (ps
);
3929 for (psym
= objfile
->static_psymbols
.list
+ ps
->statics_offset
;
3930 psym
< (objfile
->static_psymbols
.list
+ ps
->statics_offset
3931 + ps
->n_static_syms
);
3935 /* This will cause the symbol table to be read if it has not yet been */
3936 s
= PSYMTAB_TO_SYMTAB (ps
);
3940 /* Search upwards from currently selected frame (so that we can
3941 complete on local vars. */
3943 for (b
= get_selected_block (0); b
!= NULL
; b
= BLOCK_SUPERBLOCK (b
))
3945 if (!BLOCK_SUPERBLOCK (b
))
3947 surrounding_static_block
= b
; /* For elimination of dups */
3950 /* Also catch fields of types defined in this places which match our
3951 text string. Only complete on types visible from current context. */
3953 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3955 overload_list_add_symbol (sym
, oload_name
);
3959 /* Go through the symtabs and check the externs and statics for
3960 symbols which match. */
3962 ALL_SYMTABS (objfile
, s
)
3965 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
3966 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3968 overload_list_add_symbol (sym
, oload_name
);
3972 ALL_SYMTABS (objfile
, s
)
3975 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
3976 /* Don't do this block twice. */
3977 if (b
== surrounding_static_block
)
3979 ALL_BLOCK_SYMBOLS (b
, i
, sym
)
3981 overload_list_add_symbol (sym
, oload_name
);
3987 return (sym_return_val
);
3990 /* End of overload resolution functions */
3992 struct symtabs_and_lines
3993 decode_line_spec (char *string
, int funfirstline
)
3995 struct symtabs_and_lines sals
;
3996 struct symtab_and_line cursal
;
3999 error ("Empty line specification.");
4001 /* We use whatever is set as the current source line. We do not try
4002 and get a default or it will recursively call us! */
4003 cursal
= get_current_source_symtab_and_line ();
4005 sals
= decode_line_1 (&string
, funfirstline
,
4006 cursal
.symtab
, cursal
.line
,
4010 error ("Junk at end of line specification: %s", string
);
4015 static char *name_of_main
;
4018 set_main_name (const char *name
)
4020 if (name_of_main
!= NULL
)
4022 xfree (name_of_main
);
4023 name_of_main
= NULL
;
4027 name_of_main
= xstrdup (name
);
4034 if (name_of_main
!= NULL
)
4035 return name_of_main
;
4042 _initialize_symtab (void)
4044 add_info ("variables", variables_info
,
4045 "All global and static variable names, or those matching REGEXP.");
4047 add_com ("whereis", class_info
, variables_info
,
4048 "All global and static variable names, or those matching REGEXP.");
4050 add_info ("functions", functions_info
,
4051 "All function names, or those matching REGEXP.");
4054 /* FIXME: This command has at least the following problems:
4055 1. It prints builtin types (in a very strange and confusing fashion).
4056 2. It doesn't print right, e.g. with
4057 typedef struct foo *FOO
4058 type_print prints "FOO" when we want to make it (in this situation)
4059 print "struct foo *".
4060 I also think "ptype" or "whatis" is more likely to be useful (but if
4061 there is much disagreement "info types" can be fixed). */
4062 add_info ("types", types_info
,
4063 "All type names, or those matching REGEXP.");
4065 add_info ("sources", sources_info
,
4066 "Source files in the program.");
4068 add_com ("rbreak", class_breakpoint
, rbreak_command
,
4069 "Set a breakpoint for all functions matching REGEXP.");
4073 add_com ("lf", class_info
, sources_info
, "Source files in the program");
4074 add_com ("lg", class_info
, variables_info
,
4075 "All global and static variable names, or those matching REGEXP.");
4078 /* Initialize the one built-in type that isn't language dependent... */
4079 builtin_type_error
= init_type (TYPE_CODE_ERROR
, 0, 0,
4080 "<unknown type>", (struct objfile
*) NULL
);