1 /* Symbol table lookup for the GNU debugger, GDB.
2 Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
22 #include "initialize.h"
31 static int find_line_common();
32 static int lookup_misc_func();
34 /* Allocate an obstack to hold objects that should be freed
35 when we load a new symbol table.
36 This includes the symbols made by dbxread
37 and the types that are not permanent. */
39 struct obstack obstack1
;
41 struct obstack
*symbol_obstack
= &obstack1
;
43 /* These variables point to the objects
44 representing the predefined C data types. */
46 struct type
*builtin_type_void
;
47 struct type
*builtin_type_char
;
48 struct type
*builtin_type_short
;
49 struct type
*builtin_type_int
;
50 struct type
*builtin_type_long
;
51 struct type
*builtin_type_unsigned_char
;
52 struct type
*builtin_type_unsigned_short
;
53 struct type
*builtin_type_unsigned_int
;
54 struct type
*builtin_type_unsigned_long
;
55 struct type
*builtin_type_float
;
56 struct type
*builtin_type_double
;
58 /* Lookup the symbol table of a source file named NAME. */
64 register struct symtab
*s
;
67 for (s
= symtab_list
; s
; s
= s
->next
)
68 if (!strcmp (name
, s
->filename
))
71 /* If name not found as specified, see if adding ".c" helps. */
73 copy
= (char *) alloca (strlen (name
) + 3);
76 for (s
= symtab_list
; s
; s
= s
->next
)
77 if (!strcmp (copy
, s
->filename
))
83 /* Lookup a typedef or primitive type named NAME,
84 visible in lexical block BLOCK.
85 If NOERR is nonzero, return zero if NAME is not suitably defined. */
88 lookup_typename (name
, block
, noerr
)
93 register struct symbol
*sym
= lookup_symbol (name
, block
, VAR_NAMESPACE
);
94 if (sym
== 0 || SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
96 if (!strcmp (name
, "int"))
97 return builtin_type_int
;
98 if (!strcmp (name
, "long"))
99 return builtin_type_long
;
100 if (!strcmp (name
, "short"))
101 return builtin_type_short
;
102 if (!strcmp (name
, "char"))
103 return builtin_type_char
;
104 if (!strcmp (name
, "float"))
105 return builtin_type_float
;
106 if (!strcmp (name
, "double"))
107 return builtin_type_double
;
108 if (!strcmp (name
, "void"))
109 return builtin_type_void
;
113 error ("No type named %s.", name
);
115 return SYMBOL_TYPE (sym
);
119 lookup_unsigned_typename (name
)
122 if (!strcmp (name
, "int"))
123 return builtin_type_unsigned_int
;
124 if (!strcmp (name
, "long"))
125 return builtin_type_unsigned_long
;
126 if (!strcmp (name
, "short"))
127 return builtin_type_unsigned_short
;
128 if (!strcmp (name
, "char"))
129 return builtin_type_unsigned_char
;
130 error ("No type named unsigned %s.", name
);
133 /* Lookup a structure type named "struct NAME",
134 visible in lexical block BLOCK. */
137 lookup_struct (name
, block
)
141 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
143 error ("No struct type named %s.", name
);
144 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_STRUCT
)
145 error ("This context has union or enum %s, not a struct.", name
);
146 return SYMBOL_TYPE (sym
);
149 /* Lookup a union type named "union NAME",
150 visible in lexical block BLOCK. */
153 lookup_union (name
, block
)
157 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
159 error ("No union type named %s.", name
);
160 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_UNION
)
161 error ("This context has struct or enum %s, not a union.", name
);
162 return SYMBOL_TYPE (sym
);
165 /* Lookup an enum type named "enum NAME",
166 visible in lexical block BLOCK. */
169 lookup_enum (name
, block
)
173 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
175 error ("No enum type named %s.", name
);
176 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_ENUM
)
177 error ("This context has struct or union %s, not an enum.", name
);
178 return SYMBOL_TYPE (sym
);
181 /* Given a type TYPE, return a type of pointers to that type.
182 May need to construct such a type if this is the first use.
184 C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
185 to member types under control. */
188 lookup_pointer_type (type
)
191 register struct type
*ptype
= TYPE_POINTER_TYPE (type
);
192 if (ptype
) return TYPE_MAIN_VARIANT (ptype
);
194 /* This is the first time anyone wanted a pointer to a TYPE. */
195 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
196 ptype
= (struct type
*) xmalloc (sizeof (struct type
));
198 ptype
= (struct type
*) obstack_alloc (symbol_obstack
,
199 sizeof (struct type
));
201 bzero (ptype
, sizeof (struct type
));
202 TYPE_MAIN_VARIANT (ptype
) = ptype
;
203 TYPE_TARGET_TYPE (ptype
) = type
;
204 TYPE_POINTER_TYPE (type
) = ptype
;
205 /* New type is permanent if type pointed to is permanent. */
206 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
207 TYPE_FLAGS (ptype
) |= TYPE_FLAG_PERM
;
208 /* We assume the machine has only one representation for pointers! */
209 TYPE_LENGTH (ptype
) = sizeof (char *);
210 TYPE_CODE (ptype
) = TYPE_CODE_PTR
;
215 lookup_reference_type (type
)
218 register struct type
*rtype
= TYPE_REFERENCE_TYPE (type
);
219 if (rtype
) return TYPE_MAIN_VARIANT (rtype
);
221 /* This is the first time anyone wanted a pointer to a TYPE. */
222 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
223 rtype
= (struct type
*) xmalloc (sizeof (struct type
));
225 rtype
= (struct type
*) obstack_alloc (symbol_obstack
,
226 sizeof (struct type
));
228 bzero (rtype
, sizeof (struct type
));
229 TYPE_MAIN_VARIANT (rtype
) = rtype
;
230 TYPE_TARGET_TYPE (rtype
) = type
;
231 TYPE_REFERENCE_TYPE (type
) = rtype
;
232 /* New type is permanent if type pointed to is permanent. */
233 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
234 TYPE_FLAGS (rtype
) |= TYPE_FLAG_PERM
;
235 /* We assume the machine has only one representation for pointers! */
236 TYPE_LENGTH (rtype
) = sizeof (char *);
237 TYPE_CODE (rtype
) = TYPE_CODE_REF
;
241 /* Implement direct support for MEMBER_TYPE in GNU C++.
242 May need to construct such a type if this is the first use.
243 The TYPE is the type of the member. The DOMAIN is the type
244 of the aggregate that the member belongs to. */
247 lookup_member_type (domain
, type
)
248 struct type
*domain
, *type
;
250 register struct type
*mtype
= TYPE_MAIN_VARIANT (type
);
251 struct type
*main_type
;
256 if (TYPE_DOMAIN_TYPE (mtype
) == domain
)
258 mtype
= TYPE_NEXT_VARIANT (mtype
);
261 /* This is the first time anyone wanted this member type. */
262 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
263 mtype
= (struct type
*) xmalloc (sizeof (struct type
));
265 mtype
= (struct type
*) obstack_alloc (symbol_obstack
,
266 sizeof (struct type
));
268 bzero (mtype
, sizeof (struct type
));
269 if (main_type
== 0) main_type
= mtype
;
272 TYPE_NEXT_VARIANT (mtype
) = TYPE_NEXT_VARIANT (main_type
);
273 TYPE_NEXT_VARIANT (main_type
) = mtype
;
275 TYPE_MAIN_VARIANT (mtype
) = main_type
;
276 TYPE_TARGET_TYPE (mtype
) = type
;
277 TYPE_DOMAIN_TYPE (mtype
) = domain
;
278 /* New type is permanent if type pointed to is permanent. */
279 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
280 TYPE_FLAGS (mtype
) |= TYPE_FLAG_PERM
;
282 /* In practice, this is never used. */
283 TYPE_LENGTH (mtype
) = 1;
284 TYPE_CODE (mtype
) = TYPE_CODE_MEMBER
;
289 /* Given a type TYPE, return a type which has offset OFFSET,
290 via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC.
291 May need to construct such a type if none exists. */
293 lookup_basetype_type (type
, offset
, via_virtual
, via_public
)
296 int via_virtual
, via_public
;
298 register struct type
*btype
= TYPE_MAIN_VARIANT (type
);
299 struct type
*main_type
;
303 printf ("type offset non-zero in lookup_basetype_type");
310 if (/* TYPE_OFFSET (btype) == offset
311 && */ TYPE_VIA_PUBLIC (btype
) == via_public
312 && TYPE_VIA_VIRTUAL (btype
) == via_virtual
)
314 btype
= TYPE_NEXT_VARIANT (btype
);
317 /* This is the first time anyone wanted this member type. */
318 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
319 btype
= (struct type
*) xmalloc (sizeof (struct type
));
321 btype
= (struct type
*) obstack_alloc (symbol_obstack
,
322 sizeof (struct type
));
327 bzero (btype
, sizeof (struct type
));
328 TYPE_MAIN_VARIANT (btype
) = main_type
;
332 bcopy (main_type
, btype
, sizeof (struct type
));
333 TYPE_NEXT_VARIANT (main_type
) = btype
;
335 /* TYPE_OFFSET (btype) = offset; */
337 TYPE_FLAGS (btype
) |= TYPE_FLAG_VIA_PUBLIC
;
339 TYPE_FLAGS (btype
) |= TYPE_FLAG_VIA_VIRTUAL
;
340 /* New type is permanent if type pointed to is permanent. */
341 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
342 TYPE_FLAGS (btype
) |= TYPE_FLAG_PERM
;
344 /* In practice, this is never used. */
345 TYPE_LENGTH (btype
) = 1;
346 TYPE_CODE (btype
) = TYPE_CODE_STRUCT
;
351 /* Given a type TYPE, return a type of functions that return that type.
352 May need to construct such a type if this is the first use. */
355 lookup_function_type (type
, argtypes
)
357 struct type
**argtypes
;
359 register struct type
*ptype
= TYPE_FUNCTION_TYPE (type
);
360 if (ptype
) return ptype
;
362 /* This is the first time anyone wanted a function returning a TYPE. */
363 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
364 ptype
= (struct type
*) xmalloc (sizeof (struct type
));
366 ptype
= (struct type
*) obstack_alloc (symbol_obstack
,
367 sizeof (struct type
));
369 bzero (ptype
, sizeof (struct type
));
370 TYPE_TARGET_TYPE (ptype
) = type
;
371 TYPE_FUNCTION_TYPE (type
) = ptype
;
372 /* New type is permanent if type returned is permanent. */
373 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
374 TYPE_FLAGS (ptype
) |= TYPE_FLAG_PERM
;
375 TYPE_LENGTH (ptype
) = 1;
376 TYPE_CODE (ptype
) = TYPE_CODE_FUNC
;
377 TYPE_NFIELDS (ptype
) = 0;
381 /* Smash TYPE to be a type of pointers to TO_TYPE.
382 If TO_TYPE is not permanent and has no pointer-type yet,
383 record TYPE as its pointer-type. */
386 smash_to_pointer_type (type
, to_type
)
387 struct type
*type
, *to_type
;
389 bzero (type
, sizeof (struct type
));
390 TYPE_TARGET_TYPE (type
) = to_type
;
391 /* We assume the machine has only one representation for pointers! */
392 TYPE_LENGTH (type
) = sizeof (char *);
393 TYPE_CODE (type
) = TYPE_CODE_PTR
;
395 TYPE_MAIN_VARIANT (type
) = type
;
397 if (TYPE_POINTER_TYPE (to_type
) == 0
398 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
400 TYPE_POINTER_TYPE (to_type
) = type
;
404 /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE. */
407 smash_to_member_type (type
, domain
, to_type
)
408 struct type
*type
, *domain
, *to_type
;
410 bzero (type
, sizeof (struct type
));
411 TYPE_TARGET_TYPE (type
) = to_type
;
412 TYPE_DOMAIN_TYPE (type
) = domain
;
414 /* In practice, this is never needed. */
415 TYPE_LENGTH (type
) = 1;
416 TYPE_CODE (type
) = TYPE_CODE_MEMBER
;
418 TYPE_MAIN_VARIANT (type
) = lookup_member_type (domain
, to_type
);
421 /* Smash TYPE to be a type of reference to TO_TYPE.
422 If TO_TYPE is not permanent and has no pointer-type yet,
423 record TYPE as its pointer-type. */
426 smash_to_reference_type (type
, to_type
)
427 struct type
*type
, *to_type
;
429 bzero (type
, sizeof (struct type
));
430 TYPE_TARGET_TYPE (type
) = to_type
;
431 /* We assume the machine has only one representation for pointers! */
432 TYPE_LENGTH (type
) = sizeof (char *);
433 TYPE_CODE (type
) = TYPE_CODE_REF
;
435 TYPE_MAIN_VARIANT (type
) = type
;
437 if (TYPE_REFERENCE_TYPE (to_type
) == 0
438 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
440 TYPE_REFERENCE_TYPE (to_type
) = type
;
444 /* Smash TYPE to be a type of functions returning TO_TYPE.
445 If TO_TYPE is not permanent and has no function-type yet,
446 record TYPE as its function-type. */
449 smash_to_function_type (type
, to_type
)
450 struct type
*type
, *to_type
;
452 bzero (type
, sizeof (struct type
));
453 TYPE_TARGET_TYPE (type
) = to_type
;
454 TYPE_LENGTH (type
) = 1;
455 TYPE_CODE (type
) = TYPE_CODE_FUNC
;
456 TYPE_NFIELDS (type
) = 0;
458 if (TYPE_FUNCTION_TYPE (to_type
) == 0
459 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
461 TYPE_FUNCTION_TYPE (to_type
) = type
;
465 static struct symbol
*lookup_block_symbol ();
467 /* Find the definition for a specified symbol name NAME
468 in namespace NAMESPACE, visible from lexical block BLOCK.
469 Returns the struct symbol pointer, or zero if no symbol is found. */
472 lookup_symbol_1 (name
, block
, namespace)
474 register struct block
*block
;
475 enum namespace namespace;
478 register struct symbol
*sym
;
479 register struct symtab
*s
;
480 struct blockvector
*bv
;
482 /* Search specified block and its superiors. */
486 sym
= lookup_block_symbol (block
, name
, namespace);
488 block
= BLOCK_SUPERBLOCK (block
);
494 lookup_symbol_2 (name
, block
, namespace)
496 register struct block
*block
; /* ignored as parameter */
497 enum namespace namespace;
500 register struct symbol
*sym
;
501 register struct symtab
*s
;
502 struct blockvector
*bv
;
504 /* Now search all symtabs' global blocks. */
506 for (s
= symtab_list
; s
; s
= s
->next
)
508 bv
= BLOCKVECTOR (s
);
509 block
= BLOCKVECTOR_BLOCK (bv
, 0);
510 sym
= lookup_block_symbol (block
, name
, namespace);
514 /* Now search all symtabs' per-file blocks.
515 Not strictly correct, but more useful than an error. */
517 for (s
= symtab_list
; s
; s
= s
->next
)
519 bv
= BLOCKVECTOR (s
);
520 block
= BLOCKVECTOR_BLOCK (bv
, 1);
521 sym
= lookup_block_symbol (block
, name
, namespace);
528 lookup_symbol (name
, block
, namespace)
530 register struct block
*block
;
531 enum namespace namespace;
534 register struct symbol
*sym
;
535 register struct symtab
*s
;
536 struct blockvector
*bv
;
538 /* Search specified block and its superiors. */
542 sym
= lookup_block_symbol (block
, name
, namespace);
544 block
= BLOCK_SUPERBLOCK (block
);
547 /* Now search all symtabs' global blocks. */
549 for (s
= symtab_list
; s
; s
= s
->next
)
551 bv
= BLOCKVECTOR (s
);
552 block
= BLOCKVECTOR_BLOCK (bv
, 0);
553 sym
= lookup_block_symbol (block
, name
, namespace);
557 /* Now search all symtabs' per-file blocks.
558 Not strictly correct, but more useful than an error. */
560 for (s
= symtab_list
; s
; s
= s
->next
)
562 bv
= BLOCKVECTOR (s
);
563 block
= BLOCKVECTOR_BLOCK (bv
, 1);
564 sym
= lookup_block_symbol (block
, name
, namespace);
570 /* Look for a symbol in block BLOCK. */
572 static struct symbol
*
573 lookup_block_symbol (block
, name
, namespace)
574 register struct block
*block
;
576 enum namespace namespace;
578 register int bot
, top
, inc
;
579 register struct symbol
*sym
;
581 top
= BLOCK_NSYMS (block
);
584 /* If the blocks's symbols were sorted, start with a binary search. */
586 if (BLOCK_SHOULD_SORT (block
))
588 /* First, advance BOT to not far before
589 the first symbol whose name is NAME. */
593 inc
= (top
- bot
+ 1);
594 /* No need to keep binary searching for the last few bits worth. */
597 inc
= (inc
>> 1) + bot
;
598 sym
= BLOCK_SYM (block
, inc
);
599 if (SYMBOL_NAME (sym
)[0] < name
[0])
601 else if (SYMBOL_NAME (sym
)[0] > name
[0])
603 else if (strcmp (SYMBOL_NAME (sym
), name
) < 0)
609 /* Now scan forward until we run out of symbols,
610 find one whose name is greater than NAME,
612 If there is more than one symbol with the right name and namespace,
613 we return the first one. dbxread.c is careful to make sure
614 that if one is a register then it comes first. */
616 top
= BLOCK_NSYMS (block
);
619 sym
= BLOCK_SYM (block
, bot
);
620 inc
= SYMBOL_NAME (sym
)[0] - name
[0];
622 inc
= strcmp (SYMBOL_NAME (sym
), name
);
623 if (inc
== 0 && SYMBOL_NAMESPACE (sym
) == namespace)
632 /* Here if block isn't sorted.
633 This loop is equivalent to the loop above,
634 but hacked greatly for speed. */
636 top
= BLOCK_NSYMS (block
);
640 sym
= BLOCK_SYM (block
, bot
);
641 if (SYMBOL_NAME (sym
)[0] == inc
642 && !strcmp (SYMBOL_NAME (sym
), name
)
643 && SYMBOL_NAMESPACE (sym
) == namespace)
650 /* Return the symbol for the function which contains a specified
651 lexical block, described by a struct block BL. */
657 while (BLOCK_FUNCTION (bl
) == 0 && BLOCK_SUPERBLOCK (bl
) != 0)
658 bl
= BLOCK_SUPERBLOCK (bl
);
660 return BLOCK_FUNCTION (bl
);
663 /* Subroutine of find_pc_line */
665 static struct symtab
*
667 register CORE_ADDR pc
;
669 register struct block
*b
;
670 struct blockvector
*bv
;
671 register struct symtab
*s
;
673 /* Search all symtabs for one whose file contains our pc */
675 for (s
= symtab_list
; s
; s
= s
->next
)
677 bv
= BLOCKVECTOR (s
);
678 b
= BLOCKVECTOR_BLOCK (bv
, 0);
679 if (BLOCK_START (b
) <= pc
680 && BLOCK_END (b
) > pc
)
687 /* Find the source file and line number for a given PC value.
688 Return a structure containing a symtab pointer, a line number,
689 and a pc range for the entire source line.
690 The value's .pc field is NOT the specified pc.
691 NOTCURRENT nonzero means, if specified pc is on a line boundary,
692 use the line that ends there. Otherwise, in that case, the line
693 that begins there is used. */
695 struct symtab_and_line
696 find_pc_line (pc
, notcurrent
)
701 register struct linetable
*l
;
703 register int i
, item
;
705 struct symtab_and_line value
;
706 struct blockvector
*bv
;
708 /* Info on best line seen so far, and where it starts, and its file. */
711 CORE_ADDR best_pc
= 0;
712 CORE_ADDR best_end
= 0;
713 struct symtab
*best_symtab
= 0;
715 /* Store here the first line number
716 of a file which contains the line at the smallest pc after PC.
717 If we don't find a line whose range contains PC,
718 we will use a line one less than this,
719 with a range from the start of that file to the first line's pc. */
721 CORE_ADDR alt_pc
= 0;
722 struct symtab
*alt_symtab
= 0;
724 /* Info on best line seen in this file. */
729 /* Info on first line of this file. */
734 /* If this pc is not from the current frame,
735 it is the address of the end of a call instruction.
736 Quite likely that is the start of the following statement.
737 But what we want is the statement containing the instruction.
738 Fudge the pc to make sure we get that. */
740 if (notcurrent
) pc
-= 1;
742 s
= find_pc_symtab (pc
);
751 bv
= BLOCKVECTOR (s
);
753 /* Look at all the symtabs that share this blockvector.
754 They all have the same apriori range, that we found was right;
755 but they have different line tables. */
757 for (; s
&& BLOCKVECTOR (s
) == bv
; s
= s
->next
)
759 /* Find the best line in this symtab. */
764 for (i
= 0; i
< len
; i
++)
777 /* Return the last line that did not start after PC. */
788 /* Is this file's best line closer than the best in the other files?
789 If so, record this file, and its best line, as best so far. */
790 if (prev_line
>= 0 && prev_pc
> best_pc
)
793 best_line
= prev_line
;
800 /* Is this file's first line closer than the first lines of other files?
801 If so, record this file, and its first line, as best alternate. */
802 if (first_line
>= 0 && first_pc
> pc
803 && (alt_pc
== 0 || first_pc
< alt_pc
))
806 alt_line
= first_line
;
810 if (best_symtab
== 0)
812 value
.symtab
= alt_symtab
;
813 value
.line
= alt_line
- 1;
814 value
.pc
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, 0));
819 value
.symtab
= best_symtab
;
820 value
.line
= best_line
;
822 value
.end
= (best_end
? best_end
824 : BLOCK_END (BLOCKVECTOR_BLOCK (bv
, 0))));
829 /* Find the PC value for a given source file and line number.
830 Returns zero for invalid line number.
831 The source file is specified with a struct symtab. */
834 find_line_pc (symtab
, line
)
835 struct symtab
*symtab
;
838 register struct linetable
*l
;
844 l
= LINETABLE (symtab
);
845 index
= find_line_common(l
, line
, &dummy
);
846 return index
? l
->item
[index
] : 0;
849 /* Find the range of pc values in a line.
850 Store the starting pc of the line into *STARTPTR
851 and the ending pc (start of next line) into *ENDPTR.
852 Returns 1 to indicate success.
853 Returns 0 if could not find the specified line. */
856 find_line_pc_range (symtab
, thisline
, startptr
, endptr
)
857 struct symtab
*symtab
;
859 CORE_ADDR
*startptr
, *endptr
;
861 register struct linetable
*l
;
863 int exact_match
; /* did we get an exact linenumber match */
864 register CORE_ADDR prev_pc
;
870 l
= LINETABLE (symtab
);
871 index
= find_line_common (l
, thisline
, &exact_match
);
874 *startptr
= l
->item
[index
];
875 /* If we have not seen an entry for the specified line,
876 assume that means the specified line has zero bytes. */
877 if (!exact_match
|| index
== l
->nitems
-1)
880 /* Perhaps the following entry is for the following line.
882 if (l
->item
[index
+1] > 0)
883 *endptr
= l
->item
[index
+1];
885 *endptr
= find_line_pc (symtab
, thisline
+1);
892 /* Given a line table and a line number, return the index into the line
893 table for the pc of the nearest line whose number is >= the specified one.
894 Return 0 if none is found. The value is never zero is it is an index.
896 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
899 find_line_common (l
, lineno
, exact_match
)
900 register struct linetable
*l
;
907 /* BEST is the smallest linenumber > LINENO so far seen,
908 or 0 if none has been seen so far.
909 BEST_INDEX identifies the item for it. */
920 for (i
= 0; i
< len
; i
++)
922 register int item
= l
->item
[i
];
925 nextline
= - item
- 1;
929 if (nextline
== lineno
)
935 if (nextline
> lineno
&& (best
== 0 || nextline
< best
))
943 /* If we got here, we didn't get an exact match. */
950 find_pc_line_pc_range (pc
, startptr
, endptr
)
952 CORE_ADDR
*startptr
, *endptr
;
954 struct symtab_and_line sal
;
955 sal
= find_pc_line (pc
, 0);
958 return sal
.symtab
!= 0;
961 /* Parse a string that specifies a line number.
962 Pass the address of a char * variable; that variable will be
963 advanced over the characters actually parsed.
967 LINENUM -- that line number in current file. PC returned is 0.
968 FILE:LINENUM -- that line in that file. PC returned is 0.
969 FUNCTION -- line number of openbrace of that function.
970 PC returned is the start of the function.
971 FILE:FUNCTION -- likewise, but prefer functions in that file.
972 *EXPR -- line in which address EXPR appears.
974 FUNCTION may be an undebuggable function found in misc_function_vector.
976 If the argument FUNFIRSTLINE is nonzero, we want the first line
977 of real code inside a function when a function is specified.
979 DEFAULT_SYMTAB specifies the file to use if none is specified.
980 It defaults to current_source_symtab.
981 DEFAULT_LINE specifies the line number to use for relative
982 line numbers (that start with signs). Defaults to current_source_line.
984 Note that it is possible to return zero for the symtab
985 if no file is validly specified. Callers must check that.
986 Also, the line number returned may be invalid. */
988 struct symtabs_and_lines
989 decode_line_1 (argptr
, funfirstline
, default_symtab
, default_line
)
992 struct symtab
*default_symtab
;
995 struct symtabs_and_lines
decode_line_2 ();
996 struct symtabs_and_lines values
;
997 struct symtab_and_line value
;
998 register char *p
, *p1
;
999 register struct symtab
*s
;
1000 register struct symbol
*sym
;
1001 register CORE_ADDR pc
;
1004 struct symbol
*sym_class
;
1005 char *class_name
, *method_name
, *phys_name
;
1008 struct symbol
**sym_arr
;
1009 struct type
*t
, *field
;
1012 /* Defaults have defaults. */
1014 if (default_symtab
== 0)
1016 default_symtab
= current_source_symtab
;
1017 default_line
= current_source_line
;
1020 /* See if arg is *PC */
1022 if (**argptr
== '*')
1025 pc
= parse_and_eval_address_1 (argptr
);
1026 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1028 values
.sals
[0] = find_pc_line (pc
, 0);
1029 values
.sals
[0].pc
= pc
;
1033 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1037 for (p
= *argptr
; *p
; p
++)
1039 if (p
[0] == ':' || p
[0] == ' ' || p
[0] == '\t')
1042 while (p
[0] == ' ' || p
[0] == '\t') p
++;
1049 /* Extract the class name. */
1051 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
1052 copy
= (char *) alloca (p
- *argptr
+ 1);
1053 bcopy (*argptr
, copy
, p
- *argptr
);
1054 copy
[p
- *argptr
] = 0;
1056 /* Discard the class name from the arg. */
1058 while (*p
== ' ' || *p
== '\t') p
++;
1061 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
);
1064 (TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_STRUCT
1065 || TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_UNION
))
1067 /* Arg token is not digits => try it as a function name
1068 Find the next token (everything up to end or next whitespace). */
1070 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!=':') p
++;
1071 copy
= (char *) alloca (p
- *argptr
+ 1);
1072 bcopy (*argptr
, copy
, p
- *argptr
);
1073 copy
[p
- *argptr
] = '\0';
1075 /* no line number may be specified */
1076 while (*p
== ' ' || *p
== '\t') p
++;
1080 i1
= 0; /* counter for the symbol array */
1081 t
= SYMBOL_TYPE (sym_class
);
1082 sym_arr
= (struct symbol
**) alloca(TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(struct symbol
*));
1083 physnames
= (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(char*));
1085 if (destructor_name_p (copy
, t
))
1087 /* destructors are a special case. */
1088 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, 0);
1089 int len
= TYPE_FN_FIELDLIST_LENGTH (t
, 0) - 1;
1090 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, len
);
1091 physnames
[i1
] = (char *)alloca (strlen (phys_name
) + 1);
1092 strcpy (physnames
[i1
], phys_name
);
1093 sym_arr
[i1
] = lookup_symbol (phys_name
, SYMBOL_BLOCK_VALUE (sym_class
), VAR_NAMESPACE
);
1094 if (sym_arr
[i1
]) i1
++;
1098 class_name
= TYPE_NAME (t
);
1099 while (*class_name
++ != ' ');
1101 sym_class
= lookup_symbol (class_name
, 0, STRUCT_NAMESPACE
);
1102 for (method_counter
= TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class
)) - 1;
1103 method_counter
>= 0;
1107 struct fn_field
*f
=
1108 TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class
), method_counter
);
1110 method_name
= TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class
), method_counter
);
1111 if (!strcmp (copy
, method_name
))
1112 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class
), method_counter
) - 1;
1116 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1117 physnames
[i1
] = (char*) alloca (strlen (phys_name
) + 1);
1118 strcpy (physnames
[i1
], phys_name
);
1119 sym_arr
[i1
] = lookup_symbol (phys_name
, SYMBOL_BLOCK_VALUE (sym_class
), VAR_NAMESPACE
);
1120 if (sym_arr
[i1
]) i1
++;
1123 if (TYPE_N_BASECLASSES (t
))
1124 t
= TYPE_BASECLASS(t
, 1);
1132 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1134 /* Arg is the name of a function */
1135 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1138 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1140 values
.sals
[0] = find_pc_line (pc
, 0);
1141 values
.sals
[0].pc
= (values
.sals
[0].end
&& values
.sals
[0].pc
!= pc
) ? values
.sals
[0].end
: pc
;
1151 return decode_line_2 (argptr
, sym_arr
, physnames
, i1
, funfirstline
);
1154 error ("that class does not have any method named %s",copy
);
1157 error("no class, struct, or union named %s", copy
);
1161 /* Extract the file name. */
1163 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
1164 copy
= (char *) alloca (p
- *argptr
+ 1);
1165 bcopy (*argptr
, copy
, p
- *argptr
);
1166 copy
[p
- *argptr
] = 0;
1168 /* Find that file's data. */
1169 s
= lookup_symtab (copy
);
1172 if (symtab_list
== 0)
1173 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1174 error ("No source file named %s.", copy
);
1177 /* Discard the file name from the arg. */
1179 while (*p
== ' ' || *p
== '\t') p
++;
1183 /* S is specified file's symtab, or 0 if no file specified.
1184 arg no longer contains the file name. */
1186 /* Check whether arg is all digits (and sign) */
1189 if (*p
== '-' || *p
== '+') p
++;
1190 while (*p
>= '0' && *p
<= '9')
1193 if (p
!= *argptr
&& (*p
== 0 || *p
== ' ' || *p
== '\t' || *p
== ','))
1195 /* We found a token consisting of all digits -- at least one digit. */
1196 enum sign
{none
, plus
, minus
} sign
= none
;
1198 if (**argptr
== '+')
1199 sign
= plus
, (*argptr
)++;
1200 else if (**argptr
== '-')
1201 sign
= minus
, (*argptr
)++;
1202 value
.line
= atoi (*argptr
);
1209 value
.line
= default_line
+ value
.line
;
1215 value
.line
= default_line
- value
.line
;
1221 while (*p
== ' ' || *p
== '\t') p
++;
1227 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1228 values
.sals
[0] = value
;
1233 /* Arg token is not digits => try it as a function name
1234 Find the next token (everything up to end or next whitespace). */
1236 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',') p
++;
1237 copy
= (char *) alloca (p
- *argptr
+ 1);
1238 bcopy (*argptr
, copy
, p
- *argptr
);
1239 copy
[p
- *argptr
] = 0;
1240 while (*p
== ' ' || *p
== '\t') p
++;
1243 /* Look up that token as a function.
1244 If file specified, use that file's per-file block to start with. */
1246 sym
= lookup_symbol (copy
, s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), 1) : 0,
1249 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1251 /* Arg is the name of a function */
1252 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1255 value
= find_pc_line (pc
, 0);
1256 value
.pc
= (value
.end
&& value
.pc
!= pc
) ? value
.end
: pc
;
1257 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1258 values
.sals
[0] = value
;
1264 error ("%s is not a function.", copy
);
1266 if ((i
= lookup_misc_func (copy
)) < 0)
1267 error ("Function %s not defined.", copy
);
1272 value
.pc
= misc_function_vector
[i
].address
+ FUNCTION_START_OFFSET
;
1274 SKIP_PROLOGUE (value
.pc
);
1275 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1276 values
.sals
[0] = value
;
1281 if (symtab_list
== 0)
1282 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1283 error ("Function %s not defined.", copy
);
1286 struct symtabs_and_lines
1287 decode_line_spec (string
, funfirstline
)
1291 struct symtabs_and_lines sals
;
1293 error ("Empty line specification.");
1294 sals
= decode_line_1 (&string
, funfirstline
,
1295 current_source_symtab
, current_source_line
);
1297 error ("Junk at end of line specification: %s", string
);
1301 struct symtabs_and_lines
1302 decode_line_2 (argptr
, sym_arr
, physnames
, nelts
, funfirstline
)
1304 struct symbol
*sym_arr
[];
1310 struct symtabs_and_lines values
, return_values
;
1311 register CORE_ADDR pc
;
1312 char *args
, *arg1
, *read_line ();
1316 values
.sals
= (struct symtab_and_line
*) alloca (nelts
* sizeof(struct symtab_and_line
));
1317 return_values
.sals
= (struct symtab_and_line
*) malloc (nelts
* sizeof(struct symtab_and_line
));
1320 printf("[0] cancel\n[1] all\n");
1323 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
1325 /* Arg is the name of a function */
1326 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr
[i
]))
1327 + FUNCTION_START_OFFSET
;
1330 values
.sals
[i
] = find_pc_line (pc
, 0);
1331 printf("[%d] file:%s; line number:%d\n",
1332 (i
+2), values
.sals
[i
].symtab
->filename
, values
.sals
[i
].line
);
1334 else printf ("?HERE\n");
1338 if ((prompt
= getenv ("PS2")) == NULL
)
1342 printf("%s ",prompt
);
1345 args
= read_line (0);
1348 error_no_arg ("one or more choice numbers");
1356 while (*arg1
>= '0' && *arg1
<= '9') arg1
++;
1357 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
1358 error ("Arguments must be choice numbers.");
1363 error ("cancelled");
1366 bcopy (values
.sals
, return_values
.sals
, (nelts
* sizeof(struct symtab_and_line
)));
1367 return_values
.nelts
= nelts
;
1368 return return_values
;
1371 if (num
> nelts
+ 2)
1373 printf ("No choice number %d.\n", num
);
1378 if (values
.sals
[num
].pc
)
1380 return_values
.sals
[i
++] = values
.sals
[num
];
1381 values
.sals
[num
].pc
= 0;
1385 printf ("duplicate request for %d ignored.\n", num
);
1390 while (*args
== ' ' || *args
== '\t') args
++;
1392 return_values
.nelts
= i
;
1393 return return_values
;
1396 /* Return the index of misc function named NAME. */
1399 lookup_misc_func (name
)
1400 register char *name
;
1404 for (i
= 0; i
< misc_function_count
; i
++)
1405 if (!strcmp (misc_function_vector
[i
].name
, name
))
1407 return -1; /* not found */
1413 register struct symtab
*s
;
1414 register int column
= 0;
1416 if (symtab_list
== 0)
1418 printf ("No symbol table is loaded.\n");
1421 printf ("Source files for which symbol table is known:\n");
1422 for (s
= symtab_list
; s
; s
= s
->next
)
1424 if (column
!= 0 && column
+ strlen (s
->filename
) >= 70)
1429 else if (column
!= 0)
1434 printf ("%s", s
->filename
);
1435 column
+= strlen (s
->filename
);
1445 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
1446 If CLASS is zero, list all symbols except functions and type names.
1447 If CLASS is 1, list only functions.
1448 If CLASS is 2, list only type names. */
1452 if (print_count >= 21) \
1453 { printf ("--Type Return to print more--"); \
1458 static void sort_block_syms ();
1461 list_symbols (regexp
, class)
1465 register struct symtab
*s
;
1466 register struct blockvector
*bv
;
1467 struct blockvector
*prev_bv
= 0;
1468 register struct block
*b
;
1470 register struct symbol
*sym
;
1473 static char *classnames
[]
1474 = {"variable", "function", "type", "method"};
1475 int print_count
= 0;
1478 if (val
= (char *) re_comp (regexp
))
1479 error ("Invalid regexp: %s", val
);
1482 ? "All %ss matching regular expression \"%s\":\n"
1483 : "All defined %ss:\n",
1487 for (s
= symtab_list
; s
; s
= s
->next
)
1490 bv
= BLOCKVECTOR (s
);
1491 /* Often many files share a blockvector.
1492 Scan each blockvector only once so that
1493 we don't get every symbol many times.
1494 It happens that the first symtab in the list
1495 for any given blockvector is the main file. */
1497 for (i
= 0; i
< 2; i
++)
1499 b
= BLOCKVECTOR_BLOCK (bv
, i
);
1500 /* Skip the sort if this block is always sorted. */
1501 if (!BLOCK_SHOULD_SORT (b
))
1502 sort_block_syms (b
);
1503 for (j
= 0; j
< BLOCK_NSYMS (b
); j
++)
1506 sym
= BLOCK_SYM (b
, j
);
1507 if ((regexp
== 0 || re_exec (SYMBOL_NAME (sym
)))
1508 && ((class == 0 && SYMBOL_CLASS (sym
) != LOC_TYPEDEF
1509 && SYMBOL_CLASS (sym
) != LOC_BLOCK
)
1510 || (class == 1 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1511 || (class == 2 && SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
1512 || (class == 3 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)))
1516 printf ("\nFile %s:\n", s
->filename
);
1521 if (class != 2 && i
== 1)
1524 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
)
1525 printf ("typedef ");
1529 type_print (SYMBOL_TYPE (sym
),
1530 (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
1531 ? "" : SYMBOL_NAME (sym
)),
1539 type_print_base (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0, 0);
1540 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0);
1541 sprintf (buf
, " %s::", TYPE_NAME (t
));
1542 type_print_method_args (TYPE_FN_FIELD_ARGS (t
, i
), buf
, name
, stdout
);
1546 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
1547 && (TYPE_NAME ((SYMBOL_TYPE (sym
))) == 0
1548 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym
))),
1549 SYMBOL_NAME (sym
))))
1550 printf (" %s", SYMBOL_NAME (sym
));
1559 variables_info (regexp
)
1562 list_symbols (regexp
, 0);
1566 functions_info (regexp
)
1569 list_symbols (regexp
, 1);
1576 list_symbols (regexp
, 2);
1580 methods_info (regexp
)
1583 list_symbols (regexp
, 3);
1586 /* Call sort_block_syms to sort alphabetically the symbols of one block. */
1589 compare_symbols (s1
, s2
)
1590 struct symbol
**s1
, **s2
;
1592 /* Names that are less should come first. */
1593 register int namediff
= strcmp (SYMBOL_NAME (*s1
), SYMBOL_NAME (*s2
));
1594 if (namediff
!= 0) return namediff
;
1595 /* For symbols of the same name, registers should come first. */
1596 return ((SYMBOL_CLASS (*s2
) == LOC_REGISTER
)
1597 - (SYMBOL_CLASS (*s1
) == LOC_REGISTER
));
1602 register struct block
*b
;
1604 qsort (&BLOCK_SYM (b
, 0), BLOCK_NSYMS (b
),
1605 sizeof (struct symbol
*), compare_symbols
);
1608 /* Initialize the standard C scalar types. */
1612 init_type (code
, length
, uns
, name
)
1613 enum type_code code
;
1617 register struct type
*type
;
1619 type
= (struct type
*) xmalloc (sizeof (struct type
));
1620 bzero (type
, sizeof *type
);
1621 TYPE_MAIN_VARIANT (type
) = type
;
1622 TYPE_CODE (type
) = code
;
1623 TYPE_LENGTH (type
) = length
;
1624 TYPE_FLAGS (type
) = uns
? TYPE_FLAG_UNSIGNED
: 0;
1625 TYPE_FLAGS (type
) |= TYPE_FLAG_PERM
;
1626 TYPE_NFIELDS (type
) = 0;
1627 TYPE_NAME (type
) = name
;
1630 TYPE_NFN_FIELDS (type
) = 0;
1631 TYPE_N_BASECLASSES (type
) = 0;
1632 TYPE_BASECLASSES (type
) = 0;
1639 add_info ("variables", variables_info
,
1640 "All global and static variable names, or those matching REGEXP.");
1641 add_info ("functions", functions_info
,
1642 "All function names, or those matching REGEXP.");
1643 add_info ("types", types_info
,
1644 "All types names, or those matching REGEXP.");
1645 add_info ("methods", methods_info
,
1646 "All method names, or those matching REGEXP::REGEXP.\n\
1647 If the class qualifier is ommited, it is assumed to be the current scope.\n\
1648 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
1650 add_info ("sources", sources_info
,
1651 "Source files in the program.");
1653 obstack_init (symbol_obstack
);
1655 builtin_type_void
= init_type (TYPE_CODE_VOID
, 0, 0, "void");
1657 builtin_type_float
= init_type (TYPE_CODE_FLT
, sizeof (float), 0, "float");
1658 builtin_type_double
= init_type (TYPE_CODE_FLT
, sizeof (double), 0, "double");
1660 builtin_type_char
= init_type (TYPE_CODE_INT
, sizeof (char), 0, "char");
1661 builtin_type_short
= init_type (TYPE_CODE_INT
, sizeof (short), 0, "short");
1662 builtin_type_long
= init_type (TYPE_CODE_INT
, sizeof (long), 0, "long");
1663 builtin_type_int
= init_type (TYPE_CODE_INT
, sizeof (int), 0, "int");
1665 builtin_type_unsigned_char
= init_type (TYPE_CODE_INT
, sizeof (char), 1, "unsigned char");
1666 builtin_type_unsigned_short
= init_type (TYPE_CODE_INT
, sizeof (short), 1, "unsigned short");
1667 builtin_type_unsigned_long
= init_type (TYPE_CODE_INT
, sizeof (long), 1, "unsigned long");
1668 builtin_type_unsigned_int
= init_type (TYPE_CODE_INT
, sizeof (int), 1, "unsigned int");