1 /* Symbol table lookup for the GNU debugger, GDB.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
30 #include "expression.h"
36 #include <sys/types.h>
42 /* Prototypes for local functions */
45 find_methods
PARAMS ((struct type
*, char *, char **, struct symbol
**));
48 completion_list_add_symbol
PARAMS ((char *));
50 static struct symtabs_and_lines
51 decode_line_2
PARAMS ((struct symbol
*[], int, int));
54 rbreak_command
PARAMS ((char *));
57 types_info
PARAMS ((char *));
60 functions_info
PARAMS ((char *));
63 variables_info
PARAMS ((char *));
66 list_symbols
PARAMS ((char *, int, int));
69 sources_info
PARAMS ((void));
72 output_source_filename
PARAMS ((char *, int *));
75 operator_chars
PARAMS ((char *, char **));
78 find_line_common
PARAMS ((struct linetable
*, int, int *));
80 static struct partial_symbol
*
81 lookup_partial_symbol
PARAMS ((struct partial_symtab
*, const char *,
82 int, enum namespace));
84 static struct partial_symbol
*
85 lookup_demangled_partial_symbol
PARAMS ((const struct partial_symtab
*,
88 static struct symbol
*
89 lookup_demangled_block_symbol
PARAMS ((const struct block
*, const char *));
91 static struct symtab
*
92 lookup_symtab_1
PARAMS ((char *));
96 /* The single non-language-specific builtin type */
97 struct type
*builtin_type_error
;
99 /* Block in which the most recently searched-for symbol was found.
100 Might be better to make this a parameter to lookup_symbol and
103 const struct block
*block_found
;
105 char no_symtab_msg
[] = "No symbol table is loaded. Use the \"file\" command.";
107 /* Check for a symtab of a specific name; first in symtabs, then in
108 psymtabs. *If* there is no '/' in the name, a match after a '/'
109 in the symtab filename will also work. */
111 static struct symtab
*
112 lookup_symtab_1 (name
)
115 register struct symtab
*s
;
116 register struct partial_symtab
*ps
;
117 register char *slash
= strchr (name
, '/');
118 register int len
= strlen (name
);
119 register struct objfile
*objfile
;
122 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
124 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
126 if (strcmp (name
, s
->filename
) == 0)
132 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
134 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
->next
)
136 if (strcmp (name
, ps
-> filename
) == 0)
140 error ("Internal: readin pst for `%s' found when no symtab found.", name
);
142 return (PSYMTAB_TO_SYMTAB (ps
));
148 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
150 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
152 int l
= strlen (s
->filename
);
154 if (s
->filename
[l
- len
-1] == '/'
155 && (strcmp (s
->filename
+ l
- len
, name
) == 0))
162 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
164 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
166 int l
= strlen (ps
-> filename
);
168 if (ps
-> filename
[l
- len
- 1] == '/'
169 && (strcmp (ps
->filename
+ l
- len
, name
) == 0))
173 error ("Internal: readin pst for `%s' found when no symtab found.", name
);
175 return (PSYMTAB_TO_SYMTAB (ps
));
183 /* Lookup the symbol table of a source file named NAME. Try a couple
184 of variations if the first lookup doesn't work. */
190 register struct symtab
*s
;
193 s
= lookup_symtab_1 (name
);
196 /* If name not found as specified, see if adding ".c" helps. */
198 copy
= (char *) alloca (strlen (name
) + 3);
201 s
= lookup_symtab_1 (copy
);
204 /* We didn't find anything; die. */
208 /* Lookup the partial symbol table of a source file named NAME. This
209 only returns true on an exact match (ie. this semantics are
210 different from lookup_symtab. */
212 struct partial_symtab
*
213 lookup_partial_symtab (name
)
216 register struct partial_symtab
*pst
;
217 register struct objfile
*objfile
;
219 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
221 for (pst
= objfile
-> psymtabs
; pst
!= NULL
; pst
= pst
-> next
)
223 if (strcmp (name
, pst
-> filename
) == 0)
232 /* Demangle a GDB method stub type. */
234 gdb_mangle_name (type
, i
, j
)
238 int mangled_name_len
;
240 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (type
, i
);
241 struct fn_field
*method
= &f
[j
];
242 char *field_name
= TYPE_FN_FIELDLIST_NAME (type
, i
);
243 int is_constructor
= strcmp(field_name
, TYPE_NAME (type
)) == 0;
245 /* Need a new type prefix. */
246 char *const_prefix
= method
->is_const
? "C" : "";
247 char *volatile_prefix
= method
->is_volatile
? "V" : "";
248 char *newname
= type_name_no_tag (type
);
250 int len
= strlen (newname
);
252 sprintf (buf
, "__%s%s%d", const_prefix
, volatile_prefix
, len
);
253 mangled_name_len
= ((is_constructor
? 0 : strlen (field_name
))
255 + strlen (TYPE_FN_FIELD_PHYSNAME (f
, j
))
258 /* Only needed for GNU-mangled names. ANSI-mangled names
259 work with the normal mechanisms. */
260 if (OPNAME_PREFIX_P (field_name
))
262 char *opname
= cplus_mangle_opname (field_name
+ 3, 0);
264 error ("No mangling for \"%s\"", field_name
);
265 mangled_name_len
+= strlen (opname
);
266 mangled_name
= (char *)xmalloc (mangled_name_len
);
268 strncpy (mangled_name
, field_name
, 3);
269 mangled_name
[3] = '\0';
270 strcat (mangled_name
, opname
);
274 mangled_name
= (char *)xmalloc (mangled_name_len
);
276 mangled_name
[0] = '\0';
278 strcpy (mangled_name
, field_name
);
280 strcat (mangled_name
, buf
);
281 strcat (mangled_name
, newname
);
282 strcat (mangled_name
, TYPE_FN_FIELD_PHYSNAME (f
, j
));
288 /* Find which partial symtab on contains PC. Return 0 if none. */
290 struct partial_symtab
*
292 register CORE_ADDR pc
;
294 register struct partial_symtab
*pst
;
295 register struct objfile
*objfile
;
297 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
299 for (pst
= objfile
-> psymtabs
; pst
!= NULL
; pst
= pst
-> next
)
301 if (pc
>= pst
-> textlow
&& pc
< pst
-> texthigh
)
310 /* Find which partial symbol within a psymtab contains PC. Return 0
311 if none. Check all psymtabs if PSYMTAB is 0. */
312 struct partial_symbol
*
313 find_pc_psymbol (psymtab
, pc
)
314 struct partial_symtab
*psymtab
;
317 struct partial_symbol
*best
, *p
;
321 psymtab
= find_pc_psymtab (pc
);
325 best_pc
= psymtab
->textlow
- 1;
327 for (p
= psymtab
->objfile
->static_psymbols
.list
+ psymtab
->statics_offset
;
328 (p
- (psymtab
->objfile
->static_psymbols
.list
+ psymtab
->statics_offset
)
329 < psymtab
->n_static_syms
);
331 if (SYMBOL_NAMESPACE (p
) == VAR_NAMESPACE
332 && SYMBOL_CLASS (p
) == LOC_BLOCK
333 && pc
>= SYMBOL_VALUE_ADDRESS (p
)
334 && SYMBOL_VALUE_ADDRESS (p
) > best_pc
)
336 best_pc
= SYMBOL_VALUE_ADDRESS (p
);
339 if (best_pc
== psymtab
->textlow
- 1)
345 /* Function called via iterate_over_msymbols() that tests a minimal symbol
346 to see if the minimal symbol's name is a C++ mangled name that matches
347 a user visible name. The user visible name (pname) is passed as arg1
348 and the number of leading characters that must match in both the mangled
349 name and the user name (matchcount) is passed as arg2. Returns a pointer
350 to the minimal symbol if it matches, NULL otherwise. */
353 cplus_mangled_symbol (objfile
, msymbol
, arg1
, arg2
, arg3
)
354 struct objfile
*objfile
;
355 struct minimal_symbol
*msymbol
;
360 char *pname
= (char *) arg1
;
361 int matchcount
= (int) arg2
;
363 struct minimal_symbol
*foundit
= NULL
;
365 if (strncmp (msymbol
-> name
, pname
, matchcount
) == 0)
367 if ((demangled
= cplus_demangle (msymbol
-> name
, -1)) != NULL
)
369 if (strcmp (demangled
, pname
) == 0)
376 return ((PTR
) foundit
);
379 /* Find the definition for a specified symbol name NAME
380 in namespace NAMESPACE, visible from lexical block BLOCK.
381 Returns the struct symbol pointer, or zero if no symbol is found.
382 If SYMTAB is non-NULL, store the symbol table in which the
383 symbol was found there, or NULL if not found.
384 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
385 NAME is a field of the current implied argument `this'. If so set
386 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
387 BLOCK_FOUND is set to the block in which NAME is found (in the case of
388 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
391 lookup_symbol (name
, block
, namespace, is_a_field_of_this
, symtab
)
393 register const struct block
*block
;
394 const enum namespace namespace;
395 int *is_a_field_of_this
;
396 struct symtab
**symtab
;
398 register struct symbol
*sym
;
399 register struct symtab
*s
;
400 register struct partial_symtab
*ps
;
401 struct blockvector
*bv
;
402 register struct objfile
*objfile
;
403 register struct block
*b
;
405 register struct minimal_symbol
*msymbol
;
407 /* Search specified block and its superiors. */
411 sym
= lookup_block_symbol (block
, name
, namespace);
417 /* Search the list of symtabs for one which contains the
418 address of the start of this block. */
419 for (found
= 0, objfile
= object_files
;
420 !found
&& objfile
!= NULL
;
421 objfile
= objfile
-> next
)
423 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
425 bv
= BLOCKVECTOR (s
);
426 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
427 if (BLOCK_START (b
) <= BLOCK_START (block
)
428 && BLOCK_END (b
) > BLOCK_START (block
))
440 block
= BLOCK_SUPERBLOCK (block
);
443 /* But that doesn't do any demangling for the STATIC_BLOCK.
444 I'm not sure whether demangling is needed in the case of
445 nested function in inner blocks; if so this needs to be changed.
447 Don't need to mess with the psymtabs; if we have a block,
448 that file is read in. If we don't, then we deal later with
449 all the psymtab stuff that needs checking. */
450 if (namespace == VAR_NAMESPACE
&& block
!= NULL
)
453 /* Find the right symtab. */
454 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
456 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
458 bv
= BLOCKVECTOR (s
);
459 b
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
460 if (BLOCK_START (b
) <= BLOCK_START (block
)
461 && BLOCK_END (b
) > BLOCK_START (block
))
463 sym
= lookup_demangled_block_symbol (b
, name
);
477 /* C++: If requested to do so by the caller,
478 check to see if NAME is a field of `this'. */
479 if (is_a_field_of_this
)
481 struct value
*v
= value_of_this (0);
483 *is_a_field_of_this
= 0;
484 if (v
&& check_field (v
, name
))
486 *is_a_field_of_this
= 1;
493 /* Now search all global blocks. Do the symtab's first, then
494 check the psymtab's */
496 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
498 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
500 bv
= BLOCKVECTOR (s
);
501 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
502 sym
= lookup_block_symbol (block
, name
, namespace);
513 /* Check for the possibility of the symbol being a global function
514 that is stored in one of the minimal symbol tables. Eventually, all
515 global symbols might be resolved in this way. */
517 if (namespace == VAR_NAMESPACE
)
519 msymbol
= lookup_minimal_symbol (name
, (struct objfile
*) NULL
);
522 /* Look for a mangled C++ name for NAME. */
523 msymbol
= (struct minimal_symbol
*)
524 iterate_over_msymbols (cplus_mangled_symbol
, (PTR
) name
,
525 (PTR
) strlen (name
), (PTR
) NULL
);
529 s
= find_pc_symtab (msymbol
-> address
);
530 /* If S is NULL, there are no debug symbols for this file.
531 Skip this stuff and check for matching static symbols below. */
534 bv
= BLOCKVECTOR (s
);
535 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
536 sym
= lookup_block_symbol (block
, msymbol
-> name
, namespace);
537 /* We kept static functions in minimal symbol table as well as
538 in static scope. We want to find them in the symbol table. */
540 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
541 sym
= lookup_block_symbol (block
, msymbol
-> name
,
545 /* sym == 0 if symbol was found in the minimal symbol table
546 but not in the symtab.
547 Return 0 to use the msymbol definition of "foo_".
549 This happens for Fortran "foo_" symbols,
550 which are "foo" in the symtab.
552 This can also happen if "asm" is used to make a
553 regular symbol but not a debugging symbol, e.g.
565 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
567 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
->next
)
569 if (!ps
->readin
&& lookup_partial_symbol (ps
, name
, 1, namespace))
571 s
= PSYMTAB_TO_SYMTAB(ps
);
572 bv
= BLOCKVECTOR (s
);
573 block
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
574 sym
= lookup_block_symbol (block
, name
, namespace);
576 error ("Internal: global symbol `%s' found in psymtab but not in symtab", name
);
584 /* Now search all per-file blocks.
585 Not strictly correct, but more useful than an error.
586 Do the symtabs first, then check the psymtabs */
588 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
590 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
592 bv
= BLOCKVECTOR (s
);
593 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
594 sym
= lookup_block_symbol (block
, name
, namespace);
605 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
607 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
609 if (!ps
->readin
&& lookup_partial_symbol (ps
, name
, 0, namespace))
611 s
= PSYMTAB_TO_SYMTAB(ps
);
612 bv
= BLOCKVECTOR (s
);
613 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
614 sym
= lookup_block_symbol (block
, name
, namespace);
616 error ("Internal: static symbol `%s' found in psymtab but not in symtab", name
);
624 /* Now search all per-file blocks for static mangled symbols.
625 Do the symtabs first, then check the psymtabs. */
627 if (namespace == VAR_NAMESPACE
)
629 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
631 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
633 bv
= BLOCKVECTOR (s
);
634 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
635 sym
= lookup_demangled_block_symbol (block
, name
);
646 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
648 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
650 if (!ps
->readin
&& lookup_demangled_partial_symbol (ps
, name
))
652 s
= PSYMTAB_TO_SYMTAB(ps
);
653 bv
= BLOCKVECTOR (s
);
654 block
= BLOCKVECTOR_BLOCK (bv
, STATIC_BLOCK
);
655 sym
= lookup_demangled_block_symbol (block
, name
);
657 error ("Internal: mangled static symbol `%s' found in psymtab but not in symtab", name
);
671 /* Look for a static demangled symbol in block BLOCK. */
673 static struct symbol
*
674 lookup_demangled_block_symbol (block
, name
)
675 register const struct block
*block
;
678 register int bot
, top
, inc
;
679 register struct symbol
*sym
;
682 top
= BLOCK_NSYMS (block
);
687 sym
= BLOCK_SYM (block
, bot
);
688 if (SYMBOL_NAME (sym
)[0] == inc
689 && SYMBOL_NAMESPACE (sym
) == VAR_NAMESPACE
)
691 char *demangled
= cplus_demangle(SYMBOL_NAME (sym
), -1);
692 if (demangled
!= NULL
)
694 int cond
= strcmp (demangled
, name
);
706 /* Look, in partial_symtab PST, for static mangled symbol NAME. */
708 static struct partial_symbol
*
709 lookup_demangled_partial_symbol (pst
, name
)
710 const struct partial_symtab
*pst
;
713 struct partial_symbol
*start
, *psym
;
714 int length
= pst
->n_static_syms
;
715 register int inc
= name
[0];
718 return (struct partial_symbol
*) 0;
720 start
= pst
->objfile
->static_psymbols
.list
+ pst
->statics_offset
;
721 for (psym
= start
; psym
< start
+ length
; psym
++)
723 if (SYMBOL_NAME (psym
)[0] == inc
724 && SYMBOL_NAMESPACE (psym
) == VAR_NAMESPACE
)
726 char *demangled
= cplus_demangle(SYMBOL_NAME (psym
), -1);
727 if (demangled
!= NULL
)
729 int cond
= strcmp (demangled
, name
);
737 return (struct partial_symbol
*) 0;
740 /* Look, in partial_symtab PST, for symbol NAME. Check the global
741 symbols if GLOBAL, the static symbols if not */
743 static struct partial_symbol
*
744 lookup_partial_symbol (pst
, name
, global
, namespace)
745 struct partial_symtab
*pst
;
748 enum namespace namespace;
750 struct partial_symbol
*start
, *psym
;
751 int length
= (global
? pst
->n_global_syms
: pst
->n_static_syms
);
754 return (struct partial_symbol
*) 0;
757 pst
->objfile
->global_psymbols
.list
+ pst
->globals_offset
:
758 pst
->objfile
->static_psymbols
.list
+ pst
->statics_offset
);
760 if (global
) /* This means we can use a binary */
763 struct partial_symbol
*top
, *bottom
, *center
;
765 /* Binary search. This search is guaranteed to end with center
766 pointing at the earliest partial symbol with the correct
767 name. At that point *all* partial symbols with that name
768 will be checked against the correct namespace. */
770 top
= start
+ length
- 1;
773 center
= bottom
+ (top
- bottom
) / 2;
775 assert (center
< top
);
777 if (strcmp (SYMBOL_NAME (center
), name
) >= 0)
782 assert (top
== bottom
);
784 while (!strcmp (SYMBOL_NAME (top
), name
))
786 if (SYMBOL_NAMESPACE (top
) == namespace)
793 /* Can't use a binary search */
794 for (psym
= start
; psym
< start
+ length
; psym
++)
795 if (namespace == SYMBOL_NAMESPACE (psym
)
796 && !strcmp (name
, SYMBOL_NAME (psym
)))
800 return (struct partial_symbol
*) 0;
803 /* Find the psymtab containing main(). */
805 struct partial_symtab
*
808 register struct partial_symtab
*pst
;
809 register struct objfile
*objfile
;
811 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
813 for (pst
= objfile
-> psymtabs
; pst
; pst
= pst
->next
)
815 if (lookup_partial_symbol (pst
, "main", 1, VAR_NAMESPACE
))
824 /* Look for a symbol in block BLOCK. */
827 lookup_block_symbol (block
, name
, namespace)
828 register const struct block
*block
;
830 const enum namespace namespace;
832 register int bot
, top
, inc
;
833 register struct symbol
*sym
, *parameter_sym
;
835 top
= BLOCK_NSYMS (block
);
838 /* If the blocks's symbols were sorted, start with a binary search. */
840 if (BLOCK_SHOULD_SORT (block
))
842 /* First, advance BOT to not far before
843 the first symbol whose name is NAME. */
847 inc
= (top
- bot
+ 1);
848 /* No need to keep binary searching for the last few bits worth. */
851 inc
= (inc
>> 1) + bot
;
852 sym
= BLOCK_SYM (block
, inc
);
853 if (SYMBOL_NAME (sym
)[0] < name
[0])
855 else if (SYMBOL_NAME (sym
)[0] > name
[0])
857 else if (strcmp (SYMBOL_NAME (sym
), name
) < 0)
863 /* Now scan forward until we run out of symbols,
864 find one whose name is greater than NAME,
866 If there is more than one symbol with the right name and namespace,
867 we return the first one. dbxread.c is careful to make sure
868 that if one is a register then it comes first. */
870 top
= BLOCK_NSYMS (block
);
873 sym
= BLOCK_SYM (block
, bot
);
874 inc
= SYMBOL_NAME (sym
)[0] - name
[0];
876 inc
= strcmp (SYMBOL_NAME (sym
), name
);
877 if (inc
== 0 && SYMBOL_NAMESPACE (sym
) == namespace)
886 /* Here if block isn't sorted.
887 This loop is equivalent to the loop above,
888 but hacked greatly for speed.
890 Note that parameter symbols do not always show up last in the
891 list; this loop makes sure to take anything else other than
892 parameter symbols first; it only uses parameter symbols as a
893 last resort. Note that this only takes up extra computation
896 parameter_sym
= (struct symbol
*) 0;
897 top
= BLOCK_NSYMS (block
);
901 sym
= BLOCK_SYM (block
, bot
);
902 if (SYMBOL_NAME (sym
)[0] == inc
903 && !strcmp (SYMBOL_NAME (sym
), name
)
904 && SYMBOL_NAMESPACE (sym
) == namespace)
906 if (SYMBOL_CLASS (sym
) == LOC_ARG
907 || SYMBOL_CLASS (sym
) == LOC_LOCAL_ARG
908 || SYMBOL_CLASS (sym
) == LOC_REF_ARG
909 || SYMBOL_CLASS (sym
) == LOC_REGPARM
)
916 return parameter_sym
; /* Will be 0 if not found. */
919 /* Return the symbol for the function which contains a specified
920 lexical block, described by a struct block BL. */
926 while (BLOCK_FUNCTION (bl
) == 0 && BLOCK_SUPERBLOCK (bl
) != 0)
927 bl
= BLOCK_SUPERBLOCK (bl
);
929 return BLOCK_FUNCTION (bl
);
932 /* Subroutine of find_pc_line */
936 register CORE_ADDR pc
;
938 register struct block
*b
;
939 struct blockvector
*bv
;
940 register struct symtab
*s
= 0;
941 register struct partial_symtab
*ps
;
942 register struct objfile
*objfile
;
945 /* Search all symtabs for one whose file contains our pc */
947 for (found
= 0, objfile
= object_files
;
948 !found
&& objfile
!= NULL
;
949 objfile
= objfile
-> next
)
951 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
953 bv
= BLOCKVECTOR (s
);
954 b
= BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
);
955 if (BLOCK_START (b
) <= pc
956 && BLOCK_END (b
) > pc
)
966 ps
= find_pc_psymtab (pc
);
967 if (ps
&& ps
->readin
)
969 printf_filtered ("(Internal error: pc 0x%x in read in psymtab, but not in symtab.)\n", pc
);
973 s
= PSYMTAB_TO_SYMTAB (ps
);
980 /* Find the source file and line number for a given PC value.
981 Return a structure containing a symtab pointer, a line number,
982 and a pc range for the entire source line.
983 The value's .pc field is NOT the specified pc.
984 NOTCURRENT nonzero means, if specified pc is on a line boundary,
985 use the line that ends there. Otherwise, in that case, the line
986 that begins there is used. */
988 struct symtab_and_line
989 find_pc_line (pc
, notcurrent
)
994 register struct linetable
*l
;
997 register struct linetable_entry
*item
;
998 struct symtab_and_line val
;
999 struct blockvector
*bv
;
1001 /* Info on best line seen so far, and where it starts, and its file. */
1004 CORE_ADDR best_pc
= 0;
1005 CORE_ADDR best_end
= 0;
1006 struct symtab
*best_symtab
= 0;
1008 /* Store here the first line number
1009 of a file which contains the line at the smallest pc after PC.
1010 If we don't find a line whose range contains PC,
1011 we will use a line one less than this,
1012 with a range from the start of that file to the first line's pc. */
1014 CORE_ADDR alt_pc
= 0;
1015 struct symtab
*alt_symtab
= 0;
1017 /* Info on best line seen in this file. */
1022 /* Info on first line of this file. */
1027 /* If this pc is not from the current frame,
1028 it is the address of the end of a call instruction.
1029 Quite likely that is the start of the following statement.
1030 But what we want is the statement containing the instruction.
1031 Fudge the pc to make sure we get that. */
1033 if (notcurrent
) pc
-= 1;
1035 s
= find_pc_symtab (pc
);
1045 bv
= BLOCKVECTOR (s
);
1047 /* Look at all the symtabs that share this blockvector.
1048 They all have the same apriori range, that we found was right;
1049 but they have different line tables. */
1051 for (; s
&& BLOCKVECTOR (s
) == bv
; s
= s
->next
)
1053 /* Find the best line in this symtab. */
1060 for (i
= 0; i
< len
; i
++)
1062 item
= &(l
->item
[i
]);
1066 first_line
= item
->line
;
1067 first_pc
= item
->pc
;
1069 /* Return the last line that did not start after PC. */
1072 prev_line
= item
->line
;
1079 /* Is this file's best line closer than the best in the other files?
1080 If so, record this file, and its best line, as best so far. */
1081 if (prev_line
>= 0 && prev_pc
> best_pc
)
1084 best_line
= prev_line
;
1086 /* If another line is in the linetable, and its PC is closer
1087 than the best_end we currently have, take it as best_end. */
1088 if (i
< len
&& (best_end
== 0 || best_end
> item
->pc
))
1089 best_end
= item
->pc
;
1091 /* Is this file's first line closer than the first lines of other files?
1092 If so, record this file, and its first line, as best alternate. */
1093 if (first_line
>= 0 && first_pc
> pc
1094 && (alt_pc
== 0 || first_pc
< alt_pc
))
1097 alt_line
= first_line
;
1101 if (best_symtab
== 0)
1103 val
.symtab
= alt_symtab
;
1104 val
.line
= alt_line
- 1;
1105 val
.pc
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
));
1110 val
.symtab
= best_symtab
;
1111 val
.line
= best_line
;
1113 if (best_end
&& (alt_pc
== 0 || best_end
< alt_pc
))
1118 val
.end
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, GLOBAL_BLOCK
));
1123 /* Find the PC value for a given source file and line number.
1124 Returns zero for invalid line number.
1125 The source file is specified with a struct symtab. */
1128 find_line_pc (symtab
, line
)
1129 struct symtab
*symtab
;
1132 register struct linetable
*l
;
1138 l
= LINETABLE (symtab
);
1139 ind
= find_line_common(l
, line
, &dummy
);
1140 return (ind
>= 0) ? l
->item
[ind
].pc
: 0;
1143 /* Find the range of pc values in a line.
1144 Store the starting pc of the line into *STARTPTR
1145 and the ending pc (start of next line) into *ENDPTR.
1146 Returns 1 to indicate success.
1147 Returns 0 if could not find the specified line. */
1150 find_line_pc_range (symtab
, thisline
, startptr
, endptr
)
1151 struct symtab
*symtab
;
1153 CORE_ADDR
*startptr
, *endptr
;
1155 register struct linetable
*l
;
1157 int exact_match
; /* did we get an exact linenumber match */
1162 l
= LINETABLE (symtab
);
1163 ind
= find_line_common (l
, thisline
, &exact_match
);
1166 *startptr
= l
->item
[ind
].pc
;
1167 /* If we have not seen an entry for the specified line,
1168 assume that means the specified line has zero bytes. */
1169 if (!exact_match
|| ind
== l
->nitems
-1)
1170 *endptr
= *startptr
;
1172 /* Perhaps the following entry is for the following line.
1173 It's worth a try. */
1174 if (ind
+1 < l
->nitems
1175 && l
->item
[ind
+1].line
== thisline
+ 1)
1176 *endptr
= l
->item
[ind
+1].pc
;
1178 *endptr
= find_line_pc (symtab
, thisline
+1);
1185 /* Given a line table and a line number, return the index into the line
1186 table for the pc of the nearest line whose number is >= the specified one.
1187 Return -1 if none is found. The value is >= 0 if it is an index.
1189 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1192 find_line_common (l
, lineno
, exact_match
)
1193 register struct linetable
*l
;
1194 register int lineno
;
1200 /* BEST is the smallest linenumber > LINENO so far seen,
1201 or 0 if none has been seen so far.
1202 BEST_INDEX identifies the item for it. */
1204 int best_index
= -1;
1213 for (i
= 0; i
< len
; i
++)
1215 register struct linetable_entry
*item
= &(l
->item
[i
]);
1217 if (item
->line
== lineno
)
1223 if (item
->line
> lineno
&& (best
== 0 || item
->line
< best
))
1230 /* If we got here, we didn't get an exact match. */
1237 find_pc_line_pc_range (pc
, startptr
, endptr
)
1239 CORE_ADDR
*startptr
, *endptr
;
1241 struct symtab_and_line sal
;
1242 sal
= find_pc_line (pc
, 0);
1245 return sal
.symtab
!= 0;
1248 /* If P is of the form "operator[ \t]+..." where `...' is
1249 some legitimate operator text, return a pointer to the
1250 beginning of the substring of the operator text.
1251 Otherwise, return "". */
1253 operator_chars (p
, end
)
1258 if (strncmp (p
, "operator", 8))
1262 /* Don't get faked out by `operator' being part of a longer
1264 if (isalpha(*p
) || *p
== '_' || *p
== '$' || *p
== '\0')
1267 /* Allow some whitespace between `operator' and the operator symbol. */
1268 while (*p
== ' ' || *p
== '\t')
1271 /* Recognize 'operator TYPENAME'. */
1273 if (isalpha(*p
) || *p
== '_' || *p
== '$')
1275 register char *q
= p
+1;
1276 while (isalnum(*q
) || *q
== '_' || *q
== '$')
1301 if (p
[1] == '=' || p
[1] == p
[0])
1312 error ("`operator ()' must be specified without whitespace in `()'");
1317 error ("`operator ?:' must be specified without whitespace in `?:'");
1322 error ("`operator []' must be specified without whitespace in `[]'");
1326 error ("`operator %s' not supported", p
);
1333 /* Recursive helper function for decode_line_1.
1334 * Look for methods named NAME in type T.
1335 * Return number of matches.
1336 * Put matches in PHYSNAMES and SYM_ARR (which better be big enough!).
1337 * These allocations seem to define "big enough":
1338 * sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*));
1339 * physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*));
1343 find_methods (t
, name
, physnames
, sym_arr
)
1347 struct symbol
**sym_arr
;
1351 struct symbol
*sym_class
;
1352 char *class_name
= type_name_no_tag (t
);
1353 /* Ignore this class if it doesn't have a name.
1354 This prevents core dumps, but is just a workaround
1355 because we might not find the function in
1356 certain cases, such as
1357 struct D {virtual int f();}
1358 struct C : D {virtual int g();}
1359 (in this case g++ 1.35.1- does not put out a name
1360 for D as such, it defines type 19 (for example) in
1361 the same stab as C, and then does a
1362 .stabs "D:T19" and a .stabs "D:t19".
1364 "break C::f" should not be looking for field f in
1366 but just for the field f in the baseclasses of C
1367 (no matter what their names).
1369 However, I don't know how to replace the code below
1370 that depends on knowing the name of D. */
1372 && (sym_class
= lookup_symbol (class_name
,
1373 (struct block
*)NULL
,
1376 (struct symtab
**)NULL
)))
1379 t
= SYMBOL_TYPE (sym_class
);
1380 for (method_counter
= TYPE_NFN_FIELDS (t
) - 1;
1381 method_counter
>= 0;
1385 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, method_counter
);
1387 char *method_name
= TYPE_FN_FIELDLIST_NAME (t
, method_counter
);
1388 if (!strcmp (name
, method_name
))
1389 /* Find all the fields with that name. */
1390 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (t
, method_counter
) - 1;
1395 if (TYPE_FN_FIELD_STUB (f
, field_counter
))
1396 check_stub_method (t
, method_counter
, field_counter
);
1397 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1398 physnames
[i1
] = (char*) alloca (strlen (phys_name
) + 1);
1399 strcpy (physnames
[i1
], phys_name
);
1400 sym_arr
[i1
] = lookup_symbol (phys_name
,
1401 SYMBOL_BLOCK_VALUE (sym_class
),
1404 (struct symtab
**) NULL
);
1405 if (sym_arr
[i1
]) i1
++;
1408 fputs_filtered("(Cannot find method ", stdout
);
1409 fputs_demangled(phys_name
, stdout
, 0);
1410 fputs_filtered(" - possibly inlined.)\n", stdout
);
1415 /* Only search baseclasses if there is no match yet,
1416 * since names in derived classes override those in baseclasses.
1420 for (ibase
= 0; ibase
< TYPE_N_BASECLASSES (t
); ibase
++)
1421 i1
+= find_methods(TYPE_BASECLASS(t
, ibase
), name
,
1422 physnames
+ i1
, sym_arr
+ i1
);
1426 /* Parse a string that specifies a line number.
1427 Pass the address of a char * variable; that variable will be
1428 advanced over the characters actually parsed.
1432 LINENUM -- that line number in current file. PC returned is 0.
1433 FILE:LINENUM -- that line in that file. PC returned is 0.
1434 FUNCTION -- line number of openbrace of that function.
1435 PC returned is the start of the function.
1436 VARIABLE -- line number of definition of that variable.
1438 FILE:FUNCTION -- likewise, but prefer functions in that file.
1439 *EXPR -- line in which address EXPR appears.
1441 FUNCTION may be an undebuggable function found in minimal symbol table.
1443 If the argument FUNFIRSTLINE is nonzero, we want the first line
1444 of real code inside a function when a function is specified.
1446 DEFAULT_SYMTAB specifies the file to use if none is specified.
1447 It defaults to current_source_symtab.
1448 DEFAULT_LINE specifies the line number to use for relative
1449 line numbers (that start with signs). Defaults to current_source_line.
1451 Note that it is possible to return zero for the symtab
1452 if no file is validly specified. Callers must check that.
1453 Also, the line number returned may be invalid. */
1455 struct symtabs_and_lines
1456 decode_line_1 (argptr
, funfirstline
, default_symtab
, default_line
)
1459 struct symtab
*default_symtab
;
1462 struct symtabs_and_lines values
;
1463 struct symtab_and_line val
;
1464 register char *p
, *p1
;
1466 register struct symtab
*s
;
1468 register struct symbol
*sym
;
1469 /* The symtab that SYM was found in. */
1470 struct symtab
*sym_symtab
;
1472 register CORE_ADDR pc
;
1473 register struct minimal_symbol
*msymbol
;
1475 struct symbol
*sym_class
;
1477 struct symbol
**sym_arr
;
1481 /* Defaults have defaults. */
1483 if (default_symtab
== 0)
1485 default_symtab
= current_source_symtab
;
1486 default_line
= current_source_line
;
1489 /* See if arg is *PC */
1491 if (**argptr
== '*')
1494 pc
= parse_and_eval_address_1 (argptr
);
1495 values
.sals
= (struct symtab_and_line
*)
1496 xmalloc (sizeof (struct symtab_and_line
));
1498 values
.sals
[0] = find_pc_line (pc
, 0);
1499 values
.sals
[0].pc
= pc
;
1503 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1507 for (p
= *argptr
; *p
; p
++)
1509 if (p
[0] == ':' || p
[0] == ' ' || p
[0] == '\t')
1512 while (p
[0] == ' ' || p
[0] == '\t') p
++;
1520 /* Extract the class name. */
1522 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
1523 copy
= (char *) alloca (p
- *argptr
+ 1);
1524 bcopy (*argptr
, copy
, p
- *argptr
);
1525 copy
[p
- *argptr
] = 0;
1527 /* Discard the class name from the arg. */
1529 while (*p
== ' ' || *p
== '\t') p
++;
1532 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
, 0,
1533 (struct symtab
**)NULL
);
1536 ( TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_STRUCT
1537 || TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_UNION
))
1539 /* Arg token is not digits => try it as a function name
1540 Find the next token (everything up to end or next whitespace). */
1542 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!=':') p
++;
1543 q
= operator_chars (*argptr
, &q1
);
1548 char *tmp
= alloca (q1
- q
+ 1);
1549 memcpy (tmp
, q
, q1
- q
);
1551 opname
= cplus_mangle_opname (tmp
, 1);
1553 error ("No mangling for \"%s\"", tmp
);
1554 copy
= (char*) alloca (3 + strlen(opname
));
1555 sprintf (copy
, "__%s", opname
);
1560 copy
= (char *) alloca (p
- *argptr
+ 1 + (q1
- q
));
1561 bcopy (*argptr
, copy
, p
- *argptr
);
1562 copy
[p
- *argptr
] = '\0';
1565 /* no line number may be specified */
1566 while (*p
== ' ' || *p
== '\t') p
++;
1570 i1
= 0; /* counter for the symbol array */
1571 t
= SYMBOL_TYPE (sym_class
);
1572 sym_arr
= (struct symbol
**) alloca(TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(struct symbol
*));
1573 physnames
= (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(char*));
1575 if (destructor_name_p (copy
, t
))
1577 /* destructors are a special case. */
1578 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, 0);
1579 int len
= TYPE_FN_FIELDLIST_LENGTH (t
, 0) - 1;
1580 char *phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, len
);
1581 physnames
[i1
] = (char *)alloca (strlen (phys_name
) + 1);
1582 strcpy (physnames
[i1
], phys_name
);
1584 lookup_symbol (phys_name
, SYMBOL_BLOCK_VALUE (sym_class
),
1585 VAR_NAMESPACE
, 0, (struct symtab
**)NULL
);
1586 if (sym_arr
[i1
]) i1
++;
1589 i1
= find_methods (t
, copy
, physnames
, sym_arr
);
1592 /* There is exactly one field with that name. */
1595 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1597 /* Arg is the name of a function */
1598 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1601 values
.sals
= (struct symtab_and_line
*)xmalloc (sizeof (struct symtab_and_line
));
1603 values
.sals
[0] = find_pc_line (pc
, 0);
1604 values
.sals
[0].pc
= (values
.sals
[0].end
&& values
.sals
[0].pc
!= pc
) ? values
.sals
[0].end
: pc
;
1614 /* There is more than one field with that name
1615 (overloaded). Ask the user which one to use. */
1616 return decode_line_2 (sym_arr
, i1
, funfirstline
);
1622 if (OPNAME_PREFIX_P (copy
))
1624 tmp
= (char *)alloca (strlen (copy
+3) + 9);
1625 strcpy (tmp
, "operator ");
1626 strcat (tmp
, copy
+3);
1631 error ("The class `%s' does not have destructor defined",
1634 error ("The class %s does not have any method named %s",
1635 sym_class
->name
, tmp
);
1639 /* The quotes are important if copy is empty. */
1640 error("No class, struct, or union named \"%s\"", copy
);
1645 /* Extract the file name. */
1647 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
1648 copy
= (char *) alloca (p
- *argptr
+ 1);
1649 bcopy (*argptr
, copy
, p
- *argptr
);
1650 copy
[p
- *argptr
] = 0;
1652 /* Find that file's data. */
1653 s
= lookup_symtab (copy
);
1656 if (!have_full_symbols () && !have_partial_symbols ())
1657 error (no_symtab_msg
);
1658 error ("No source file named %s.", copy
);
1661 /* Discard the file name from the arg. */
1663 while (*p
== ' ' || *p
== '\t') p
++;
1667 /* S is specified file's symtab, or 0 if no file specified.
1668 arg no longer contains the file name. */
1670 /* Check whether arg is all digits (and sign) */
1673 if (*p
== '-' || *p
== '+') p
++;
1674 while (*p
>= '0' && *p
<= '9')
1677 if (p
!= *argptr
&& (*p
== 0 || *p
== ' ' || *p
== '\t' || *p
== ','))
1679 /* We found a token consisting of all digits -- at least one digit. */
1680 enum sign
{none
, plus
, minus
} sign
= none
;
1682 /* This is where we need to make sure that we have good defaults.
1683 We must guarantee that this section of code is never executed
1684 when we are called with just a function name, since
1685 select_source_symtab calls us with such an argument */
1687 if (s
== 0 && default_symtab
== 0)
1689 select_source_symtab (0);
1690 default_symtab
= current_source_symtab
;
1691 default_line
= current_source_line
;
1694 if (**argptr
== '+')
1695 sign
= plus
, (*argptr
)++;
1696 else if (**argptr
== '-')
1697 sign
= minus
, (*argptr
)++;
1698 val
.line
= atoi (*argptr
);
1705 val
.line
= default_line
+ val
.line
;
1711 val
.line
= default_line
- val
.line
;
1716 break; /* No need to adjust val.line. */
1719 while (*p
== ' ' || *p
== '\t') p
++;
1725 values
.sals
= (struct symtab_and_line
*)xmalloc (sizeof (struct symtab_and_line
));
1726 values
.sals
[0] = val
;
1731 /* Arg token is not digits => try it as a variable name
1732 Find the next token (everything up to end or next whitespace). */
1735 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',') p
++;
1736 copy
= (char *) alloca (p
- *argptr
+ 1);
1737 bcopy (*argptr
, copy
, p
- *argptr
);
1738 copy
[p
- *argptr
] = 0;
1739 while (*p
== ' ' || *p
== '\t') p
++;
1742 /* Look up that token as a variable.
1743 If file specified, use that file's per-file block to start with. */
1745 sym
= lookup_symbol (copy
,
1746 (s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
)
1747 : get_selected_block ()),
1748 VAR_NAMESPACE
, 0, &sym_symtab
);
1752 if (SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1754 /* Arg is the name of a function */
1755 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1758 val
= find_pc_line (pc
, 0);
1759 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1760 /* Convex: no need to suppress code on first line, if any */
1763 /* If SKIP_PROLOGUE left us in mid-line, and the next line is still
1764 part of the same function:
1765 advance to next line,
1766 recalculate its line number (might not be N+1). */
1767 if (val
.pc
!= pc
&& val
.end
&&
1768 lookup_minimal_symbol_by_pc (pc
) == lookup_minimal_symbol_by_pc (val
.end
)) {
1769 pc
= val
.end
; /* First pc of next line */
1770 val
= find_pc_line (pc
, 0);
1774 values
.sals
= (struct symtab_and_line
*)xmalloc (sizeof (struct symtab_and_line
));
1775 values
.sals
[0] = val
;
1778 /* I think this is always the same as the line that
1779 we calculate above, but the general principle is
1780 "trust the symbols more than stuff like
1782 if (SYMBOL_LINE (sym
) != 0)
1783 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1787 else if (SYMBOL_LINE (sym
) != 0)
1789 /* We know its line number. */
1790 values
.sals
= (struct symtab_and_line
*)
1791 xmalloc (sizeof (struct symtab_and_line
));
1793 bzero (&values
.sals
[0], sizeof (values
.sals
[0]));
1794 values
.sals
[0].symtab
= sym_symtab
;
1795 values
.sals
[0].line
= SYMBOL_LINE (sym
);
1799 /* This can happen if it is compiled with a compiler which doesn't
1800 put out line numbers for variables. */
1801 error ("Line number not known for symbol \"%s\"", copy
);
1804 msymbol
= lookup_minimal_symbol (copy
, (struct objfile
*) NULL
);
1805 if (msymbol
!= NULL
)
1809 val
.pc
= msymbol
-> address
+ FUNCTION_START_OFFSET
;
1811 SKIP_PROLOGUE (val
.pc
);
1812 values
.sals
= (struct symtab_and_line
*)xmalloc (sizeof (struct symtab_and_line
));
1813 values
.sals
[0] = val
;
1818 if (!have_full_symbols () &&
1819 !have_partial_symbols () && !have_minimal_symbols ())
1820 error (no_symtab_msg
);
1822 error ("Function %s not defined.", copy
);
1823 return values
; /* for lint */
1826 struct symtabs_and_lines
1827 decode_line_spec (string
, funfirstline
)
1831 struct symtabs_and_lines sals
;
1833 error ("Empty line specification.");
1834 sals
= decode_line_1 (&string
, funfirstline
,
1835 current_source_symtab
, current_source_line
);
1837 error ("Junk at end of line specification: %s", string
);
1841 /* Given a list of NELTS symbols in sym_arr (with corresponding
1842 mangled names in physnames), return a list of lines to operate on
1843 (ask user if necessary). */
1844 static struct symtabs_and_lines
1845 decode_line_2 (sym_arr
, nelts
, funfirstline
)
1846 struct symbol
*sym_arr
[];
1850 struct symtabs_and_lines values
, return_values
;
1851 register CORE_ADDR pc
;
1856 values
.sals
= (struct symtab_and_line
*) alloca (nelts
* sizeof(struct symtab_and_line
));
1857 return_values
.sals
= (struct symtab_and_line
*) xmalloc (nelts
* sizeof(struct symtab_and_line
));
1860 printf("[0] cancel\n[1] all\n");
1863 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
1865 /* Arg is the name of a function */
1866 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr
[i
]))
1867 + FUNCTION_START_OFFSET
;
1870 values
.sals
[i
] = find_pc_line (pc
, 0);
1871 values
.sals
[i
].pc
= (values
.sals
[i
].end
&& values
.sals
[i
].pc
!= pc
) ?
1872 values
.sals
[i
].end
: pc
;
1873 printf("[%d] file:%s; line number:%d\n",
1874 (i
+2), values
.sals
[i
].symtab
->filename
, values
.sals
[i
].line
);
1876 else printf ("?HERE\n");
1880 if ((prompt
= getenv ("PS2")) == NULL
)
1884 printf("%s ",prompt
);
1887 args
= command_line_input ((char *) NULL
, 0);
1890 error_no_arg ("one or more choice numbers");
1898 while (*arg1
>= '0' && *arg1
<= '9') arg1
++;
1899 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
1900 error ("Arguments must be choice numbers.");
1905 error ("cancelled");
1908 bcopy (values
.sals
, return_values
.sals
, (nelts
* sizeof(struct symtab_and_line
)));
1909 return_values
.nelts
= nelts
;
1910 return return_values
;
1913 if (num
> nelts
+ 2)
1915 printf ("No choice number %d.\n", num
);
1920 if (values
.sals
[num
].pc
)
1922 return_values
.sals
[i
++] = values
.sals
[num
];
1923 values
.sals
[num
].pc
= 0;
1927 printf ("duplicate request for %d ignored.\n", num
);
1932 while (*args
== ' ' || *args
== '\t') args
++;
1934 return_values
.nelts
= i
;
1935 return return_values
;
1939 /* Slave routine for sources_info. Force line breaks at ,'s.
1940 NAME is the name to print and *FIRST is nonzero if this is the first
1941 name printed. Set *FIRST to zero. */
1943 output_source_filename (name
, first
)
1947 static unsigned int column
;
1948 /* Table of files printed so far. Since a single source file can
1949 result in several partial symbol tables, we need to avoid printing
1950 it more than once. Note: if some of the psymtabs are read in and
1951 some are not, it gets printed both under "Source files for which
1952 symbols have been read" and "Source files for which symbols will
1953 be read in on demand". I consider this a reasonable way to deal
1954 with the situation. I'm not sure whether this can also happen for
1955 symtabs; it doesn't hurt to check. */
1956 static char **tab
= NULL
;
1957 /* Allocated size of tab in elements.
1958 Start with one 256-byte block (when using GNU malloc.c).
1959 24 is the malloc overhead when range checking is in effect. */
1960 static int tab_alloc_size
= (256 - 24) / sizeof (char *);
1961 /* Current size of tab in elements. */
1962 static int tab_cur_size
;
1969 tab
= (char **) xmalloc (tab_alloc_size
* sizeof (*tab
));
1973 /* Is NAME in tab? */
1974 for (p
= tab
; p
< tab
+ tab_cur_size
; p
++)
1975 if (strcmp (*p
, name
) == 0)
1976 /* Yes; don't print it again. */
1978 /* No; add it to tab. */
1979 if (tab_cur_size
== tab_alloc_size
)
1981 tab_alloc_size
*= 2;
1982 tab
= (char **) xrealloc ((char *) tab
, tab_alloc_size
* sizeof (*tab
));
1984 tab
[tab_cur_size
++] = name
;
1993 printf_filtered (",");
1997 if (column
!= 0 && column
+ strlen (name
) >= 70)
1999 printf_filtered ("\n");
2002 else if (column
!= 0)
2004 printf_filtered (" ");
2007 fputs_filtered (name
, stdout
);
2008 column
+= strlen (name
);
2014 register struct symtab
*s
;
2015 register struct partial_symtab
*ps
;
2016 register struct objfile
*objfile
;
2019 if (!have_full_symbols () && !have_partial_symbols ())
2021 error (no_symtab_msg
);
2024 printf_filtered ("Source files for which symbols have been read in:\n\n");
2027 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2029 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
2031 output_source_filename (s
-> filename
, &first
);
2034 printf_filtered ("\n\n");
2036 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2039 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2041 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
2045 output_source_filename (ps
-> filename
, &first
);
2049 printf_filtered ("\n");
2056 char *demangled
= cplus_demangle(name
, -1);
2057 if (demangled
!= NULL
)
2059 int cond
= re_exec (demangled
);
2063 return re_exec(name
);
2065 #define NAME_MATCH(NAME) name_match(NAME)
2067 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2068 If CLASS is zero, list all symbols except functions and type names.
2069 If CLASS is 1, list only functions.
2070 If CLASS is 2, list only type names.
2071 If CLASS is 3, list only method names.
2073 BPT is non-zero if we should set a breakpoint at the functions
2077 list_symbols (regexp
, class, bpt
)
2082 register struct symtab
*s
;
2083 register struct partial_symtab
*ps
;
2084 register struct blockvector
*bv
;
2085 struct blockvector
*prev_bv
= 0;
2086 register struct block
*b
;
2088 register struct symbol
*sym
;
2089 struct partial_symbol
*psym
;
2090 struct objfile
*objfile
;
2091 struct minimal_symbol
*msymbol
;
2094 static char *classnames
[]
2095 = {"variable", "function", "type", "method"};
2096 int found_in_file
= 0;
2098 static enum minimal_symbol_type types
[]
2099 = {mst_data
, mst_text
, mst_abs
, mst_unknown
};
2100 static enum minimal_symbol_type types2
[]
2101 = {mst_bss
, mst_text
, mst_abs
, mst_unknown
};
2102 enum minimal_symbol_type ourtype
= types
[class];
2103 enum minimal_symbol_type ourtype2
= types2
[class];
2108 /* Make sure spacing is right for C++ operators.
2109 This is just a courtesy to make the matching less sensitive
2110 to how many spaces the user leaves between 'operator'
2111 and <TYPENAME> or <OPERATOR>. */
2113 char *opname
= operator_chars (regexp
, &opend
);
2116 int fix
= -1; /* -1 means ok; otherwise number of spaces needed. */
2117 if (isalpha(*opname
) || *opname
== '_' || *opname
== '$')
2119 /* There should 1 space between 'operator' and 'TYPENAME'. */
2120 if (opname
[-1] != ' ' || opname
[-2] == ' ')
2125 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2126 if (opname
[-1] == ' ')
2129 /* If wrong number of spaces, fix it. */
2132 char *tmp
= (char*) alloca(opend
-opname
+10);
2133 sprintf(tmp
, "operator%.*s%s", fix
, " ", opname
);
2138 if (0 != (val
= re_comp (regexp
)))
2139 error ("Invalid regexp (%s): %s", val
, regexp
);
2142 /* Search through the partial symtabs *first* for all symbols
2143 matching the regexp. That way we don't have to reproduce all of
2144 the machinery below. */
2146 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2148 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
2150 struct partial_symbol
*bound
, *gbound
, *sbound
;
2153 if (ps
->readin
) continue;
2155 gbound
= objfile
->global_psymbols
.list
+ ps
->globals_offset
+ ps
->n_global_syms
;
2156 sbound
= objfile
->static_psymbols
.list
+ ps
->statics_offset
+ ps
->n_static_syms
;
2159 /* Go through all of the symbols stored in a partial
2160 symtab in one loop. */
2161 psym
= objfile
->global_psymbols
.list
+ ps
->globals_offset
;
2166 if (bound
== gbound
&& ps
->n_static_syms
!= 0)
2168 psym
= objfile
->static_psymbols
.list
+ ps
->statics_offset
;
2179 /* If it would match (logic taken from loop below)
2180 load the file and go on to the next one */
2181 if ((regexp
== 0 || NAME_MATCH (SYMBOL_NAME (psym
)))
2182 && ((class == 0 && SYMBOL_CLASS (psym
) != LOC_TYPEDEF
2183 && SYMBOL_CLASS (psym
) != LOC_BLOCK
)
2184 || (class == 1 && SYMBOL_CLASS (psym
) == LOC_BLOCK
)
2185 || (class == 2 && SYMBOL_CLASS (psym
) == LOC_TYPEDEF
)
2186 || (class == 3 && SYMBOL_CLASS (psym
) == LOC_BLOCK
)))
2188 (void) PSYMTAB_TO_SYMTAB(ps
);
2197 /* Here, we search through the minimal symbol tables for functions that
2198 match, and call find_pc_symtab on them to force their symbols to
2199 be read. The symbol will then be found during the scan of symtabs
2200 below. If find_pc_symtab fails, set found_misc so that we will
2201 rescan to print any matching symbols without debug info. */
2205 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2207 for (msymbol
= objfile
-> msymbols
;
2208 msymbol
-> name
!= NULL
; msymbol
++)
2210 if (msymbol
-> type
== ourtype
|| msymbol
-> type
== ourtype2
)
2212 if (regexp
== 0 || NAME_MATCH (msymbol
-> name
))
2214 if (0 == find_pc_symtab (msymbol
-> address
))
2224 /* Printout here so as to get after the "Reading in symbols"
2225 messages which will be generated above. */
2227 printf_filtered (regexp
2228 ? "All %ss matching regular expression \"%s\":\n"
2229 : "All defined %ss:\n",
2233 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2235 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
2238 bv
= BLOCKVECTOR (s
);
2239 /* Often many files share a blockvector.
2240 Scan each blockvector only once so that
2241 we don't get every symbol many times.
2242 It happens that the first symtab in the list
2243 for any given blockvector is the main file. */
2245 for (i
= GLOBAL_BLOCK
; i
<= STATIC_BLOCK
; i
++)
2247 b
= BLOCKVECTOR_BLOCK (bv
, i
);
2248 /* Skip the sort if this block is always sorted. */
2249 if (!BLOCK_SHOULD_SORT (b
))
2250 sort_block_syms (b
);
2251 for (j
= 0; j
< BLOCK_NSYMS (b
); j
++)
2254 sym
= BLOCK_SYM (b
, j
);
2255 if ((regexp
== 0 || NAME_MATCH (SYMBOL_NAME (sym
)))
2256 && ((class == 0 && SYMBOL_CLASS (sym
) != LOC_TYPEDEF
2257 && SYMBOL_CLASS (sym
) != LOC_BLOCK
)
2258 || (class == 1 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)
2259 || (class == 2 && SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
2260 || (class == 3 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)))
2264 /* Set a breakpoint here, if it's a function */
2266 break_command (SYMBOL_NAME(sym
), 0);
2268 else if (!found_in_file
)
2270 fputs_filtered ("\nFile ", stdout
);
2271 fputs_filtered (s
->filename
, stdout
);
2272 fputs_filtered (":\n", stdout
);
2276 if (class != 2 && i
== STATIC_BLOCK
)
2277 printf_filtered ("static ");
2279 /* Typedef that is not a C++ class */
2281 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
)
2282 typedef_print (SYMBOL_TYPE(sym
), sym
, stdout
);
2283 /* variable, func, or typedef-that-is-c++-class */
2284 else if (class < 2 ||
2286 SYMBOL_NAMESPACE(sym
) == STRUCT_NAMESPACE
))
2288 type_print (SYMBOL_TYPE (sym
),
2289 (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
2290 ? "" : SYMBOL_NAME (sym
)),
2293 printf_filtered (";\n");
2299 type_print_base (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0, 0);
2300 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0);
2301 sprintf (buf
, " %s::", type_name_no_tag (t
));
2302 type_print_method_args (TYPE_FN_FIELD_ARGS (t
, i
), buf
, name
, stdout
);
2312 /* If there are no eyes, avoid all contact. I mean, if there are
2313 no debug symbols, then print directly from the msymbol_vector. */
2315 if (found_misc
|| class != 1)
2318 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2320 for (msymbol
= objfile
-> msymbols
;
2321 msymbol
-> name
!= NULL
; msymbol
++)
2323 if (msymbol
-> type
== ourtype
|| msymbol
-> type
== ourtype2
)
2325 if (regexp
== 0 || NAME_MATCH (msymbol
-> name
))
2327 /* Functions: Look up by address. */
2329 (find_pc_symtab (msymbol
-> address
) != NULL
))
2331 /* Variables/Absolutes: Look up by name */
2332 if (lookup_symbol (msymbol
-> name
,
2333 (struct block
*) 0, VAR_NAMESPACE
, 0,
2334 (struct symtab
**) 0) == NULL
)
2338 printf_filtered ("\nNon-debugging symbols:\n");
2341 printf_filtered (" %08x %s\n",
2354 variables_info (regexp
)
2357 list_symbols (regexp
, 0, 0);
2361 functions_info (regexp
)
2364 list_symbols (regexp
, 1, 0);
2371 list_symbols (regexp
, 2, 0);
2375 /* Tiemann says: "info methods was never implemented." */
2377 methods_info (regexp
)
2380 list_symbols (regexp
, 3, 0);
2384 /* Breakpoint all functions matching regular expression. */
2386 rbreak_command (regexp
)
2389 list_symbols (regexp
, 1, 1);
2393 /* Return Nonzero if block a is lexically nested within block b,
2394 or if a and b have the same pc range.
2395 Return zero otherwise. */
2398 struct block
*a
, *b
;
2402 return BLOCK_START (a
) >= BLOCK_START (b
)
2403 && BLOCK_END (a
) <= BLOCK_END (b
);
2407 /* Helper routine for make_symbol_completion_list. */
2409 int return_val_size
, return_val_index
;
2413 completion_list_add_symbol (symname
)
2416 if (return_val_index
+ 3 > return_val_size
)
2417 return_val
= (char **) xrealloc ((char *) return_val
,
2418 (return_val_size
*= 2) * sizeof (char *));
2420 return_val
[return_val_index
] =
2421 (char *)xmalloc (1 + strlen (symname
));
2423 strcpy (return_val
[return_val_index
], symname
);
2425 return_val
[++return_val_index
] = (char *)NULL
;
2428 /* Return a NULL terminated array of all symbols (regardless of class) which
2429 begin by matching TEXT. If the answer is no symbols, then the return value
2430 is an array which contains only a NULL pointer.
2432 Problem: All of the symbols have to be copied because readline
2433 frees them. I'm not going to worry about this; hopefully there
2434 won't be that many. */
2437 make_symbol_completion_list (text
)
2440 register struct symtab
*s
;
2441 register struct partial_symtab
*ps
;
2442 register struct minimal_symbol
*msymbol
;
2443 register struct objfile
*objfile
;
2444 register struct block
*b
, *surrounding_static_block
= 0;
2446 struct partial_symbol
*psym
;
2448 int text_len
= strlen (text
);
2449 return_val_size
= 100;
2450 return_val_index
= 0;
2452 (char **)xmalloc ((1 + return_val_size
) *sizeof (char *));
2453 return_val
[0] = (char *)NULL
;
2455 /* Look through the partial symtabs for all symbols which begin
2456 by matching TEXT. Add each one that you find to the list. */
2458 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2460 for (ps
= objfile
-> psymtabs
; ps
!= NULL
; ps
= ps
-> next
)
2462 /* If the psymtab's been read in we'll get it when we search
2463 through the blockvector. */
2464 if (ps
->readin
) continue;
2466 for (psym
= objfile
->global_psymbols
.list
+ ps
->globals_offset
;
2467 psym
< (objfile
->global_psymbols
.list
+ ps
->globals_offset
2468 + ps
->n_global_syms
);
2471 QUIT
; /* If interrupted, then quit. */
2472 if ((strncmp (SYMBOL_NAME (psym
), text
, text_len
) == 0))
2473 completion_list_add_symbol (SYMBOL_NAME (psym
));
2476 for (psym
= objfile
->static_psymbols
.list
+ ps
->statics_offset
;
2477 psym
< (objfile
->static_psymbols
.list
+ ps
->statics_offset
2478 + ps
->n_static_syms
);
2482 if ((strncmp (SYMBOL_NAME (psym
), text
, text_len
) == 0))
2483 completion_list_add_symbol (SYMBOL_NAME (psym
));
2488 /* At this point scan through the misc symbol vectors and add each
2489 symbol you find to the list. Eventually we want to ignore
2490 anything that isn't a text symbol (everything else will be
2491 handled by the psymtab code above). */
2493 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2495 for (msymbol
= objfile
-> msymbols
;
2496 msymbol
->name
!= NULL
; msymbol
++)
2498 if (strncmp (text
, msymbol
-> name
, text_len
) == 0)
2500 completion_list_add_symbol (msymbol
-> name
);
2505 /* Search upwards from currently selected frame (so that we can
2506 complete on local vars. */
2507 for (b
= get_selected_block (); b
; b
= BLOCK_SUPERBLOCK (b
))
2509 if (!BLOCK_SUPERBLOCK (b
))
2510 surrounding_static_block
= b
; /* For elmin of dups */
2512 /* Also catch fields of types defined in this places which
2513 match our text string. Only complete on types visible
2514 from current context. */
2515 for (i
= 0; i
< BLOCK_NSYMS (b
); i
++)
2517 register struct symbol
*sym
= BLOCK_SYM (b
, i
);
2519 if (!strncmp (SYMBOL_NAME (sym
), text
, text_len
))
2520 completion_list_add_symbol (SYMBOL_NAME (sym
));
2522 if (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
2524 struct type
*t
= SYMBOL_TYPE (sym
);
2525 enum type_code c
= TYPE_CODE (t
);
2527 if (c
== TYPE_CODE_UNION
|| c
== TYPE_CODE_STRUCT
)
2528 for (j
= TYPE_N_BASECLASSES (t
); j
< TYPE_NFIELDS (t
); j
++)
2529 if (TYPE_FIELD_NAME (t
, j
) &&
2530 !strncmp (TYPE_FIELD_NAME (t
, j
), text
, text_len
))
2531 completion_list_add_symbol (TYPE_FIELD_NAME (t
, j
));
2536 /* Go through the symtabs and check the externs and statics for
2537 symbols which match. */
2539 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2541 for (s
= objfile
->symtabs
; s
!= NULL
; s
= s
-> next
)
2543 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), GLOBAL_BLOCK
);
2545 for (i
= 0; i
< BLOCK_NSYMS (b
); i
++)
2546 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b
, i
)), text
, text_len
))
2547 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b
, i
)));
2551 for (objfile
= object_files
; objfile
!= NULL
; objfile
= objfile
-> next
)
2553 for (s
= objfile
-> symtabs
; s
!= NULL
; s
= s
-> next
)
2555 b
= BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), STATIC_BLOCK
);
2557 /* Don't do this block twice. */
2558 if (b
== surrounding_static_block
) continue;
2560 for (i
= 0; i
< BLOCK_NSYMS (b
); i
++)
2561 if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b
, i
)), text
, text_len
))
2562 completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b
, i
)));
2566 return (return_val
);
2570 /* Add the type of the symbol sym to the type of the current
2571 function whose block we are in (assumed). The type of
2572 this current function is contained in *TYPE.
2574 This basically works as follows: When we find a function
2575 symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2576 a pointer to its type in the global in_function_type. Every
2577 time we come across a parameter symbol ('p' in its name), then
2578 this procedure adds the name and type of that parameter
2579 to the function type pointed to by *TYPE. (Which should correspond
2580 to in_function_type if it was called correctly).
2582 Note that since we are modifying a type, the result of
2583 lookup_function_type() should be bcopy()ed before calling
2584 this. When not in strict typing mode, the expression
2585 evaluator can choose to ignore this.
2587 Assumption: All of a function's parameter symbols will
2588 appear before another function symbol is found. The parameters
2589 appear in the same order in the argument list as they do in the
2593 add_param_to_type (type
,sym
)
2597 int num
= ++(TYPE_NFIELDS(*type
));
2599 if(TYPE_NFIELDS(*type
)-1)
2600 TYPE_FIELDS(*type
) = (struct field
*)
2601 (*current_objfile
->xrealloc
) ((char *)(TYPE_FIELDS(*type
)),
2602 num
*sizeof(struct field
));
2604 TYPE_FIELDS(*type
) = (struct field
*)
2605 (*current_objfile
->xmalloc
) (num
*sizeof(struct field
));
2607 TYPE_FIELD_BITPOS(*type
,num
-1) = num
-1;
2608 TYPE_FIELD_BITSIZE(*type
,num
-1) = 0;
2609 TYPE_FIELD_TYPE(*type
,num
-1) = SYMBOL_TYPE(sym
);
2610 TYPE_FIELD_NAME(*type
,num
-1) = SYMBOL_NAME(sym
);
2615 _initialize_symtab ()
2617 add_info ("variables", variables_info
,
2618 "All global and static variable names, or those matching REGEXP.");
2619 add_info ("functions", functions_info
,
2620 "All function names, or those matching REGEXP.");
2622 /* FIXME: This command has at least the following problems:
2623 1. It prints builtin types (in a very strange and confusing fashion).
2624 2. It doesn't print right, e.g. with
2625 typedef struct foo *FOO
2626 type_print prints "FOO" when we want to make it (in this situation)
2627 print "struct foo *".
2628 I also think "ptype" or "whatis" is more likely to be useful (but if
2629 there is much disagreement "info types" can be fixed). */
2630 add_info ("types", types_info
,
2631 "All type names, or those matching REGEXP.");
2634 add_info ("methods", methods_info
,
2635 "All method names, or those matching REGEXP::REGEXP.\n\
2636 If the class qualifier is ommited, it is assumed to be the current scope.\n\
2637 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
2640 add_info ("sources", sources_info
,
2641 "Source files in the program.");
2643 add_com ("rbreak", no_class
, rbreak_command
,
2644 "Set a breakpoint for all functions matching REGEXP.");
2646 /* Initialize the one built-in type that isn't language dependent... */
2647 builtin_type_error
= init_type (TYPE_CODE_ERROR
, 0, 0,
2648 "<unknown type>", (struct objfile
*) NULL
);