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 /* Allocate an obstack to hold objects that should be freed
32 when we load a new symbol table.
33 This includes the symbols made by dbxread
34 and the types that are not permanent. */
36 struct obstack obstack1
;
38 struct obstack
*symbol_obstack
= &obstack1
;
40 /* These variables point to the objects
41 representing the predefined C data types. */
43 struct type
*builtin_type_void
;
44 struct type
*builtin_type_char
;
45 struct type
*builtin_type_short
;
46 struct type
*builtin_type_int
;
47 struct type
*builtin_type_long
;
48 struct type
*builtin_type_unsigned_char
;
49 struct type
*builtin_type_unsigned_short
;
50 struct type
*builtin_type_unsigned_int
;
51 struct type
*builtin_type_unsigned_long
;
52 struct type
*builtin_type_float
;
53 struct type
*builtin_type_double
;
55 /* Lookup the symbol table of a source file named NAME. */
61 register struct symtab
*s
;
64 for (s
= symtab_list
; s
; s
= s
->next
)
65 if (!strcmp (name
, s
->filename
))
68 /* If name not found as specified, see if adding ".c" helps. */
70 copy
= (char *) alloca (strlen (name
) + 3);
73 for (s
= symtab_list
; s
; s
= s
->next
)
74 if (!strcmp (copy
, s
->filename
))
80 /* Lookup a typedef or primitive type named NAME,
81 visible in lexical block BLOCK.
82 If NOERR is nonzero, return zero if NAME is not suitably defined. */
85 lookup_typename (name
, block
, noerr
)
90 register struct symbol
*sym
= lookup_symbol (name
, block
, VAR_NAMESPACE
);
91 if (sym
== 0 || SYMBOL_CLASS (sym
) != LOC_TYPEDEF
)
93 if (!strcmp (name
, "int"))
94 return builtin_type_int
;
95 if (!strcmp (name
, "long"))
96 return builtin_type_long
;
97 if (!strcmp (name
, "short"))
98 return builtin_type_short
;
99 if (!strcmp (name
, "char"))
100 return builtin_type_char
;
101 if (!strcmp (name
, "float"))
102 return builtin_type_float
;
103 if (!strcmp (name
, "double"))
104 return builtin_type_double
;
105 if (!strcmp (name
, "void"))
106 return builtin_type_void
;
110 error ("No type named %s.", name
);
112 return SYMBOL_TYPE (sym
);
116 lookup_unsigned_typename (name
)
119 if (!strcmp (name
, "int"))
120 return builtin_type_unsigned_int
;
121 if (!strcmp (name
, "long"))
122 return builtin_type_unsigned_long
;
123 if (!strcmp (name
, "short"))
124 return builtin_type_unsigned_short
;
125 if (!strcmp (name
, "char"))
126 return builtin_type_unsigned_char
;
127 error ("No type named unsigned %s.", name
);
130 /* Lookup a structure type named "struct NAME",
131 visible in lexical block BLOCK. */
134 lookup_struct (name
, block
)
138 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
140 error ("No struct type named %s.", name
);
141 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_STRUCT
)
142 error ("This context has class, union or enum %s, not a struct.", name
);
143 return SYMBOL_TYPE (sym
);
146 /* Lookup a union type named "union NAME",
147 visible in lexical block BLOCK. */
150 lookup_union (name
, block
)
154 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
156 error ("No union type named %s.", name
);
157 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_UNION
)
158 error ("This context has class, struct or enum %s, not a union.", name
);
159 return SYMBOL_TYPE (sym
);
162 /* Lookup an enum type named "enum NAME",
163 visible in lexical block BLOCK. */
166 lookup_enum (name
, block
)
170 register struct symbol
*sym
= lookup_symbol (name
, block
, STRUCT_NAMESPACE
);
172 error ("No enum type named %s.", name
);
173 if (TYPE_CODE (SYMBOL_TYPE (sym
)) != TYPE_CODE_ENUM
)
174 error ("This context has class, struct or union %s, not an enum.", name
);
175 return SYMBOL_TYPE (sym
);
178 /* Given a type TYPE, return a type of pointers to that type.
179 May need to construct such a type if this is the first use.
181 C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer
182 to member types under control. */
185 lookup_pointer_type (type
)
188 register struct type
*ptype
= TYPE_POINTER_TYPE (type
);
189 if (ptype
) return TYPE_MAIN_VARIANT (ptype
);
191 /* This is the first time anyone wanted a pointer to a TYPE. */
192 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
193 ptype
= (struct type
*) xmalloc (sizeof (struct type
));
195 ptype
= (struct type
*) obstack_alloc (symbol_obstack
,
196 sizeof (struct type
));
198 bzero (ptype
, sizeof (struct type
));
199 TYPE_MAIN_VARIANT (ptype
) = ptype
;
200 TYPE_TARGET_TYPE (ptype
) = type
;
201 TYPE_POINTER_TYPE (type
) = ptype
;
202 /* New type is permanent if type pointed to is permanent. */
203 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
204 TYPE_FLAGS (ptype
) |= TYPE_FLAG_PERM
;
205 /* We assume the machine has only one representation for pointers! */
206 TYPE_LENGTH (ptype
) = sizeof (char *);
207 TYPE_CODE (ptype
) = TYPE_CODE_PTR
;
212 lookup_reference_type (type
)
215 register struct type
*rtype
= TYPE_REFERENCE_TYPE (type
);
216 if (rtype
) return TYPE_MAIN_VARIANT (rtype
);
218 /* This is the first time anyone wanted a pointer to a TYPE. */
219 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
220 rtype
= (struct type
*) xmalloc (sizeof (struct type
));
222 rtype
= (struct type
*) obstack_alloc (symbol_obstack
,
223 sizeof (struct type
));
225 bzero (rtype
, sizeof (struct type
));
226 TYPE_MAIN_VARIANT (rtype
) = rtype
;
227 TYPE_TARGET_TYPE (rtype
) = type
;
228 TYPE_REFERENCE_TYPE (type
) = rtype
;
229 /* New type is permanent if type pointed to is permanent. */
230 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
231 TYPE_FLAGS (rtype
) |= TYPE_FLAG_PERM
;
232 /* We assume the machine has only one representation for pointers! */
233 TYPE_LENGTH (rtype
) = sizeof (char *);
234 TYPE_CODE (rtype
) = TYPE_CODE_REF
;
239 /* Given a type TYPE, return a type of pointers to that type.
240 May need to construct such a type if this is the first use.
241 The TYPE is the type of the member. The DOMAIN is the type
242 of the aggregate that the member belongs to. */
245 lookup_member_pointer_type (type
, domain
)
246 struct type
*type
, *domain
;
248 register struct type
*ptype
= TYPE_POINTER_TYPE (type
);
249 struct type
*main_type
;
253 ptype
= TYPE_MAIN_VARIANT (ptype
);
257 if (TYPE_DOMAIN_TYPE (ptype
) == domain
)
259 ptype
= TYPE_CHAIN (ptype
);
264 main_type
= lookup_pointer_type (type
);
265 TYPE_POINTER_TYPE (type
) = main_type
;
268 /* This is the first time anyone wanted a pointer to a TYPE. */
269 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
270 ptype
= (struct type
*) xmalloc (sizeof (struct type
));
272 ptype
= (struct type
*) obstack_alloc (symbol_obstack
,
273 sizeof (struct type
));
275 bzero (ptype
, sizeof (struct type
));
276 TYPE_MAIN_VARIANT (ptype
) = main_type
;
277 TYPE_TARGET_TYPE (ptype
) = type
;
278 TYPE_DOMAIN_TYPE (ptype
) = domain
;
279 TYPE_POINTER_TYPE (type
) = ptype
;
280 /* New type is permanent if type pointed to is permanent. */
281 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
282 TYPE_FLAGS (ptype
) |= TYPE_FLAG_PERM
;
283 /* We assume the machine has only one representation for pointers! */
284 TYPE_LENGTH (ptype
) = sizeof (char *);
285 TYPE_CODE (ptype
) = TYPE_CODE_MPTR
;
287 /* Now splice in the new member pointer type. */
290 /* This type was not "smashed". */
291 TYPE_CHAIN (ptype
) = TYPE_CHAIN (main_type
);
292 TYPE_CHAIN (main_type
) = ptype
;
298 /* Given a type TYPE, return a type of functions that return that type.
299 May need to construct such a type if this is the first use. */
302 lookup_function_type (type
, argtypes
)
304 struct type
**argtypes
;
306 register struct type
*ptype
= TYPE_FUNCTION_TYPE (type
);
307 if (ptype
) return ptype
;
309 /* This is the first time anyone wanted a function returning a TYPE. */
310 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
311 ptype
= (struct type
*) xmalloc (sizeof (struct type
));
313 ptype
= (struct type
*) obstack_alloc (symbol_obstack
,
314 sizeof (struct type
));
316 bzero (ptype
, sizeof (struct type
));
317 TYPE_TARGET_TYPE (ptype
) = type
;
318 TYPE_FUNCTION_TYPE (type
) = ptype
;
319 /* New type is permanent if type returned is permanent. */
320 if (TYPE_FLAGS (type
) & TYPE_FLAG_PERM
)
321 TYPE_FLAGS (ptype
) |= TYPE_FLAG_PERM
;
322 TYPE_LENGTH (ptype
) = 1;
323 TYPE_CODE (ptype
) = TYPE_CODE_FUNC
;
324 TYPE_NFIELDS (ptype
) = 0;
328 /* Smash TYPE to be a type of pointers to TO_TYPE.
329 If TO_TYPE is not permanent and has no pointer-type yet,
330 record TYPE as its pointer-type. */
333 smash_to_pointer_type (type
, to_type
)
334 struct type
*type
, *to_type
;
336 bzero (type
, sizeof (struct type
));
337 TYPE_TARGET_TYPE (type
) = to_type
;
338 /* We assume the machine has only one representation for pointers! */
339 TYPE_LENGTH (type
) = sizeof (char *);
340 TYPE_CODE (type
) = TYPE_CODE_PTR
;
342 TYPE_MAIN_VARIANT (type
) = type
;
344 if (TYPE_POINTER_TYPE (to_type
) == 0
345 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
347 TYPE_POINTER_TYPE (to_type
) = type
;
351 /* Smash TYPE to be a type of pointers to TO_TYPE.
352 If TO_TYPE is not permanent and has no pointer-type yet,
353 record TYPE as its pointer-type.
355 TYPE is the type of the member. DOMAIN is the type of
356 the aggregate that the member belongs to. */
359 smash_to_member_pointer_type (type
, domain
, to_type
)
360 struct type
*type
, *domain
, *to_type
;
362 bzero (type
, sizeof (struct type
));
363 TYPE_TARGET_TYPE (type
) = to_type
;
364 TYPE_DOMAIN_TYPE (type
) = domain
;
365 /* We assume the machine has only one representation for pointers! */
366 TYPE_LENGTH (type
) = sizeof (char *);
367 TYPE_CODE (type
) = TYPE_CODE_MPTR
;
369 TYPE_MAIN_VARIANT (type
) = lookup_pointer_type (to_type
);
371 if (TYPE_POINTER_TYPE (to_type
) == 0
372 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
374 TYPE_POINTER_TYPE (to_type
) = type
;
378 /* Smash TYPE to be a type of reference to TO_TYPE.
379 If TO_TYPE is not permanent and has no pointer-type yet,
380 record TYPE as its pointer-type. */
383 smash_to_reference_type (type
, to_type
)
384 struct type
*type
, *to_type
;
386 bzero (type
, sizeof (struct type
));
387 TYPE_TARGET_TYPE (type
) = to_type
;
388 /* We assume the machine has only one representation for pointers! */
389 TYPE_LENGTH (type
) = sizeof (char *);
390 TYPE_CODE (type
) = TYPE_CODE_REF
;
392 TYPE_MAIN_VARIANT (type
) = type
;
394 if (TYPE_REFERENCE_TYPE (to_type
) == 0
395 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
397 TYPE_REFERENCE_TYPE (to_type
) = type
;
401 /* Smash TYPE to be a type of functions returning TO_TYPE.
402 If TO_TYPE is not permanent and has no function-type yet,
403 record TYPE as its function-type. */
406 smash_to_function_type (type
, to_type
)
407 struct type
*type
, *to_type
;
409 bzero (type
, sizeof (struct type
));
410 TYPE_TARGET_TYPE (type
) = to_type
;
411 TYPE_LENGTH (type
) = 1;
412 TYPE_CODE (type
) = TYPE_CODE_FUNC
;
413 TYPE_NFIELDS (type
) = 0;
415 if (TYPE_FUNCTION_TYPE (to_type
) == 0
416 && !(TYPE_FLAGS (type
) & TYPE_FLAG_PERM
))
418 TYPE_FUNCTION_TYPE (to_type
) = type
;
422 static struct symbol
*lookup_block_symbol ();
424 /* Find the definition for a specified symbol name NAME
425 in namespace NAMESPACE, visible from lexical block BLOCK.
426 Returns the struct symbol pointer, or zero if no symbol is found. */
429 lookup_symbol_1 (name
, block
, namespace)
431 register struct block
*block
;
432 enum namespace namespace;
435 register struct symbol
*sym
;
436 register struct symtab
*s
;
437 struct blockvector
*bv
;
439 /* Search specified block and its superiors. */
443 sym
= lookup_block_symbol (block
, name
, namespace);
445 block
= BLOCK_SUPERBLOCK (block
);
451 lookup_symbol_2 (name
, block
, namespace)
453 register struct block
*block
; /* ignored as parameter */
454 enum namespace namespace;
457 register struct symbol
*sym
;
458 register struct symtab
*s
;
459 struct blockvector
*bv
;
461 /* Now search all symtabs' global blocks. */
463 for (s
= symtab_list
; s
; s
= s
->next
)
465 bv
= BLOCKVECTOR (s
);
466 block
= BLOCKVECTOR_BLOCK (bv
, 0);
467 sym
= lookup_block_symbol (block
, name
, namespace);
471 /* Now search all symtabs' per-file blocks.
472 Not strictly correct, but more useful than an error. */
474 for (s
= symtab_list
; s
; s
= s
->next
)
476 bv
= BLOCKVECTOR (s
);
477 block
= BLOCKVECTOR_BLOCK (bv
, 1);
478 sym
= lookup_block_symbol (block
, name
, namespace);
485 lookup_symbol (name
, block
, namespace)
487 register struct block
*block
;
488 enum namespace namespace;
491 register struct symbol
*sym
;
492 register struct symtab
*s
;
493 struct blockvector
*bv
;
495 /* Search specified block and its superiors. */
499 sym
= lookup_block_symbol (block
, name
, namespace);
501 block
= BLOCK_SUPERBLOCK (block
);
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);
527 /* Look for a symbol in block BLOCK using binary search. */
529 static struct symbol
*
530 lookup_block_symbol (block
, name
, namespace)
531 register struct block
*block
;
533 enum namespace namespace;
535 register int bot
, top
, inc
;
536 register struct symbol
*sym
;
538 top
= BLOCK_NSYMS (block
);
541 /* First, advance BOT to not far before
542 the first symbol whose name is NAME. */
546 inc
= (top
- bot
+ 1);
547 /* No need to keep binary searching for the last few bits worth. */
551 sym
= BLOCK_SYM (block
, bot
+ inc
);
552 if (strcmp (SYMBOL_NAME (sym
), name
) < 0)
558 /* Now scan forward until we run out of symbols,
559 find one whose name is greater than NAME,
561 If there is more than one symbol with the right name and namespace,
562 we return the first one. dbxread.c is careful to make sure
563 that if one is a register then it comes first. */
565 top
= BLOCK_NSYMS (block
);
568 sym
= BLOCK_SYM (block
, bot
);
569 inc
= strcmp (SYMBOL_NAME (sym
), name
);
570 if (inc
== 0 && SYMBOL_NAMESPACE (sym
) == namespace)
579 /* Return the symbol for the function which contains a specified
580 lexical block, described by a struct block BL. */
586 while (BLOCK_FUNCTION (bl
) == 0 && BLOCK_SUPERBLOCK (bl
) != 0)
587 bl
= BLOCK_SUPERBLOCK (bl
);
589 return BLOCK_FUNCTION (bl
);
592 /* Subroutine of find_pc_line */
594 static struct symtab
*
596 register CORE_ADDR pc
;
598 register struct block
*b
;
599 struct blockvector
*bv
;
600 register struct symtab
*s
;
602 /* Search all symtabs for one whose file contains our pc */
604 for (s
= symtab_list
; s
; s
= s
->next
)
606 bv
= BLOCKVECTOR (s
);
607 b
= BLOCKVECTOR_BLOCK (bv
, 0);
608 if (BLOCK_START (b
) <= pc
609 && BLOCK_END (b
) > pc
)
616 /* Find the source file and line number for a given PC value.
617 Return a structure containing a symtab pointer, a line number,
618 and a pc range for the entire source line.
619 The value's .pc field is NOT the specified pc.
620 NOTCURRENT nonzero means, if specified pc is on a line boundary,
621 use the line that ends there. Otherwise, in that case, the line
622 that begins there is used. */
624 struct symtab_and_line
625 find_pc_line (pc
, notcurrent
)
630 register struct linetable
*l
;
632 register int i
, item
;
634 struct symtab_and_line value
;
635 struct blockvector
*bv
;
637 /* Info on best line seen so far, and where it starts, and its file. */
640 CORE_ADDR best_pc
= 0;
641 CORE_ADDR best_end
= 0;
642 struct symtab
*best_symtab
= 0;
644 /* Store here the first line number
645 of a file which contains the line at the smallest pc after PC.
646 If we don't find a line whose range contains PC,
647 we will use a line one less than this,
648 with a range from the start of that file to the first line's pc. */
650 CORE_ADDR alt_pc
= 0;
651 struct symtab
*alt_symtab
= 0;
653 /* Info on best line seen in this file. */
658 /* Info on first line of this file. */
663 /* If this pc is not from the current frame,
664 it is the address of the end of a call instruction.
665 Quite likely that is the start of the following statement.
666 But what we want is the statement containing the instruction.
667 Fudge the pc to make sure we get that. */
669 if (notcurrent
) pc
-= 1;
671 s
= find_pc_symtab (pc
);
680 bv
= BLOCKVECTOR (s
);
682 /* Look at all the symtabs that share this blockvector.
683 They all have the same apriori range, that we found was right;
684 but they have different line tables. */
686 for (; s
&& BLOCKVECTOR (s
) == bv
; s
= s
->next
)
688 /* Find the best line in this symtab. */
693 for (i
= 0; i
< len
; i
++)
706 /* Return the last line that did not start after PC. */
717 /* Is this file's best line closer than the best in the other files?
718 If so, record this file, and its best line, as best so far. */
719 if (prev_line
>= 0 && prev_pc
> best_pc
)
722 best_line
= prev_line
;
729 /* Is this file's first line closer than the first lines of other files?
730 If so, record this file, and its first line, as best alternate. */
731 if (first_line
>= 0 && first_pc
> pc
732 && (alt_pc
== 0 || first_pc
< alt_pc
))
735 alt_line
= first_line
;
739 if (best_symtab
== 0)
741 value
.symtab
= alt_symtab
;
742 value
.line
= alt_line
- 1;
743 value
.pc
= BLOCK_END (BLOCKVECTOR_BLOCK (bv
, 0));
748 value
.symtab
= best_symtab
;
749 value
.line
= best_line
;
751 value
.end
= (best_end
? best_end
753 : BLOCK_END (BLOCKVECTOR_BLOCK (bv
, 0))));
758 /* Find the PC value for a given source file and line number.
759 Returns zero for invalid line number.
760 The source file is specified with a struct symtab. */
763 find_line_pc (symtab
, line
)
764 struct symtab
*symtab
;
767 register struct linetable
*l
;
773 l
= LINETABLE (symtab
);
774 index
= find_line_common(l
, line
, &dummy
);
775 return index
? l
->item
[index
] : 0;
778 /* Find the range of pc values in a line.
779 Store the starting pc of the line into *STARTPTR
780 and the ending pc (start of next line) into *ENDPTR.
781 Returns 1 to indicate success.
782 Returns 0 if could not find the specified line. */
785 find_line_pc_range (symtab
, thisline
, startptr
, endptr
)
786 struct symtab
*symtab
;
788 CORE_ADDR
*startptr
, *endptr
;
790 register struct linetable
*l
;
792 int exact_match
; /* did we get an exact linenumber match */
793 register CORE_ADDR prev_pc
;
799 l
= LINETABLE (symtab
);
800 index
= find_line_common (l
, thisline
, &exact_match
);
803 *startptr
= l
->item
[index
];
804 /* If we have not seen an entry for the specified line,
805 assume that means the specified line has zero bytes. */
806 if (!exact_match
|| index
== l
->nitems
-1)
809 /* Perhaps the following entry is for the following line.
811 if (l
->item
[index
+1] > 0)
812 *endptr
= l
->item
[index
+1];
814 *endptr
= find_line_pc (symtab
, thisline
+1);
821 /* Given a line table and a line number, return the index into the line
822 table for the pc of the nearest line whose number is >= the specified one.
823 Return 0 if none is found. The value is never zero is it is an index.
825 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
828 find_line_common (l
, lineno
, exact_match
)
829 register struct linetable
*l
;
836 /* BEST is the smallest linenumber > LINENO so far seen,
837 or 0 if none has been seen so far.
838 BEST_INDEX identifies the item for it. */
849 for (i
= 0; i
< len
; i
++)
851 register int item
= l
->item
[i
];
854 nextline
= - item
- 1;
858 if (nextline
== lineno
)
864 if (nextline
> lineno
&& (best
== 0 || nextline
< best
))
872 /* If we got here, we didn't get an exact match. */
879 find_pc_line_pc_range (pc
, startptr
, endptr
)
881 CORE_ADDR
*startptr
, *endptr
;
883 struct symtab_and_line sal
;
884 sal
= find_pc_line (pc
, 0);
887 return sal
.symtab
!= 0;
890 /* Parse a string that specifies a line number.
891 Pass the address of a char * variable; that variable will be
892 advanced over the characters actually parsed.
896 LINENUM -- that line number in current file. PC returned is 0.
897 FILE:LINENUM -- that line in that file. PC returned is 0.
898 FUNCTION -- line number of openbrace of that function.
899 PC returned is the start of the function.
900 FILE:FUNCTION -- likewise, but prefer functions in that file.
901 *EXPR -- line in which address EXPR appears.
903 FUNCTION may be an undebuggable function found in misc_function_vector.
905 If the argument FUNFIRSTLINE is nonzero, we want the first line
906 of real code inside a function when a function is specified.
908 DEFAULT_SYMTAB specifies the file to use if none is specified.
909 It defaults to current_source_symtab.
910 DEFAULT_LINE specifies the line number to use for relative
911 line numbers (that start with signs). Defaults to current_source_line.
913 Note that it is possible to return zero for the symtab
914 if no file is validly specified. Callers must check that.
915 Also, the line number returned may be invalid. */
917 struct symtabs_and_lines
918 decode_line_1 (argptr
, funfirstline
, default_symtab
, default_line
)
921 struct symtab
*default_symtab
;
924 struct symtabs_and_lines
decode_line_2 ();
925 struct symtabs_and_lines values
;
926 struct symtab_and_line value
;
927 register char *p
, *p1
;
928 register struct symtab
*s
;
929 register struct symbol
*sym
;
930 register CORE_ADDR pc
;
933 struct symbol
*sym_class
;
934 char *class_name
, *method_name
, *phys_name
;
937 struct symbol
**sym_arr
;
938 struct type
*t
, *field
;
941 /* Defaults have defaults. */
943 if (default_symtab
== 0)
945 default_symtab
= current_source_symtab
;
946 default_line
= current_source_line
;
949 /* See if arg is *PC */
954 pc
= parse_and_eval_address_1 (argptr
);
955 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
957 values
.sals
[0] = find_pc_line (pc
, 0);
958 values
.sals
[0].pc
= pc
;
962 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
966 for (p
= *argptr
; *p
; p
++)
968 if (p
[0] == ':' || p
[0] == ' ' || p
[0] == '\t')
971 while (p
[0] == ' ' || p
[0] == '\t') p
++;
979 /* Extract the class name. */
981 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
982 copy
= (char *) alloca (p
- *argptr
+ 1);
983 bcopy (*argptr
, copy
, p
- *argptr
);
984 copy
[p
- *argptr
] = 0;
986 /* Discard the class name from the arg. */
988 while (*p
== ' ' || *p
== '\t') p
++;
991 sym_class
= lookup_symbol (copy
, 0, STRUCT_NAMESPACE
);
994 (TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_STRUCT
995 || TYPE_CODE (SYMBOL_TYPE (sym_class
)) == TYPE_CODE_UNION
))
997 /* Arg token is not digits => try it as a function name
998 Find the next token (everything up to end or next whitespace). */
1000 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',' && *p
!=':') p
++;
1001 copy
= (char *) alloca (p
- *argptr
+ 1);
1002 bcopy (*argptr
, copy
, p
- *argptr
);
1003 copy
[p
- *argptr
] = '\0';
1005 /* no line number may be specified */
1006 while (*p
== ' ' || *p
== '\t') p
++;
1010 i1
= 0; /* counter for the symbol array */
1011 t
= SYMBOL_TYPE (sym_class
);
1012 sym_arr
= (struct symbol
**) alloca(TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(struct symbol
*));
1013 physnames
= (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t
) * sizeof(char*));
1015 if (destructor_name_p (copy
, t
))
1017 /* destructors are a special case. */
1018 struct fn_field
*f
= TYPE_FN_FIELDLIST1 (t
, 0);
1019 int len
= TYPE_FN_FIELDLIST_LENGTH (t
, 0) - 1;
1020 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, len
);
1021 physnames
[i1
] = (char *)alloca (strlen (phys_name
) + 1);
1022 strcpy (physnames
[i1
], phys_name
);
1023 sym_arr
[i1
] = lookup_symbol (phys_name
, SYMBOL_BLOCK_VALUE (sym_class
), VAR_NAMESPACE
);
1024 if (sym_arr
[i1
]) i1
++;
1030 class_name
= TYPE_NAME (t
);
1031 while (*class_name
++ != ' ');
1033 constructor_p
= ! strcmp (class_name
, copy
);
1035 sym_class
= lookup_symbol (class_name
, 0, STRUCT_NAMESPACE
);
1036 for (method_counter
= TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class
)) - 1;
1037 method_counter
>= 0;
1041 struct fn_field
*f
=
1042 TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class
), method_counter
);
1044 method_name
= TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class
), method_counter
);
1045 if (!strcmp (copy
, method_name
))
1046 for (field_counter
= TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class
), method_counter
) - (1 + constructor_p
);
1050 phys_name
= TYPE_FN_FIELD_PHYSNAME (f
, field_counter
);
1051 physnames
[i1
] = (char*) alloca (strlen (phys_name
) + 1);
1052 strcpy (physnames
[i1
], phys_name
);
1053 sym_arr
[i1
] = lookup_symbol (phys_name
, SYMBOL_BLOCK_VALUE (sym_class
), VAR_NAMESPACE
);
1054 if (sym_arr
[i1
]) i1
++;
1057 t
= TYPE_BASECLASS(t
);
1064 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1066 /* Arg is the name of a function */
1067 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1070 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1072 values
.sals
[0] = find_pc_line (pc
, 0);
1073 values
.sals
[0].pc
= (values
.sals
[0].end
&& values
.sals
[0].pc
!= pc
) ? values
.sals
[0].end
: pc
;
1083 return decode_line_2 (argptr
, sym_arr
, physnames
, i1
, funfirstline
);
1086 error ("that class does not have any method named %s",copy
);
1089 error("no class, struct, or union named %s", copy
);
1094 /* Extract the file name. */
1096 while (p
!= *argptr
&& p
[-1] == ' ') --p
;
1097 copy
= (char *) alloca (p
- *argptr
+ 1);
1098 bcopy (*argptr
, copy
, p
- *argptr
);
1099 copy
[p
- *argptr
] = 0;
1101 /* Find that file's data. */
1102 s
= lookup_symtab (copy
);
1105 if (symtab_list
== 0)
1106 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1107 error ("No source file named %s.", copy
);
1110 /* Discard the file name from the arg. */
1112 while (*p
== ' ' || *p
== '\t') p
++;
1116 /* S is specified file's symtab, or 0 if no file specified.
1117 arg no longer contains the file name. */
1119 /* Check whether arg is all digits (and sign) */
1122 if (*p
== '-' || *p
== '+') p
++;
1123 while (*p
>= '0' && *p
<= '9')
1126 if (p
!= *argptr
&& (*p
== 0 || *p
== ' ' || *p
== '\t' || *p
== ','))
1128 /* We found a token consisting of all digits -- at least one digit. */
1129 enum sign
{none
, plus
, minus
} sign
= none
;
1131 if (**argptr
== '+')
1132 sign
= plus
, (*argptr
)++;
1133 else if (**argptr
== '-')
1134 sign
= minus
, (*argptr
)++;
1135 value
.line
= atoi (*argptr
);
1142 value
.line
= default_line
+ value
.line
;
1148 value
.line
= default_line
- value
.line
;
1154 while (*p
== ' ' || *p
== '\t') p
++;
1160 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1161 values
.sals
[0] = value
;
1166 /* Arg token is not digits => try it as a function name
1167 Find the next token (everything up to end or next whitespace). */
1169 while (*p
&& *p
!= ' ' && *p
!= '\t' && *p
!= ',') p
++;
1170 copy
= (char *) alloca (p
- *argptr
+ 1);
1171 bcopy (*argptr
, copy
, p
- *argptr
);
1172 copy
[p
- *argptr
] = 0;
1173 while (*p
== ' ' || *p
== '\t') p
++;
1176 /* Look up that token as a function.
1177 If file specified, use that file's per-file block to start with. */
1179 sym
= lookup_symbol (copy
, s
? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s
), 1) : 0,
1182 if (sym
&& SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1184 /* Arg is the name of a function */
1185 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym
)) + FUNCTION_START_OFFSET
;
1188 value
= find_pc_line (pc
, 0);
1189 value
.pc
= (value
.end
&& value
.pc
!= pc
) ? value
.end
: pc
;
1190 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1191 values
.sals
[0] = value
;
1197 error ("%s is not a function.", copy
);
1199 if ((i
= lookup_misc_func (copy
)) < 0)
1200 error ("Function %s not defined.", copy
);
1205 value
.pc
= misc_function_vector
[i
].address
+ FUNCTION_START_OFFSET
;
1207 SKIP_PROLOGUE (value
.pc
);
1208 values
.sals
= (struct symtab_and_line
*)malloc (sizeof (struct symtab_and_line
));
1209 values
.sals
[0] = value
;
1214 if (symtab_list
== 0)
1215 error ("No symbol table is loaded. Use the \"symbol-file\" command.");
1216 error ("Function %s not defined.", copy
);
1219 struct symtabs_and_lines
1220 decode_line_spec (string
, funfirstline
)
1224 struct symtabs_and_lines sals
;
1226 error ("Empty line specification.");
1227 sals
= decode_line_1 (&string
, funfirstline
,
1228 current_source_symtab
, current_source_line
);
1230 error ("Junk at end of line specification: %s", string
);
1234 struct symtabs_and_lines
1235 decode_line_2 (argptr
, sym_arr
, physnames
, nelts
, funfirstline
)
1237 struct symbol
*sym_arr
[];
1243 struct symtabs_and_lines values
, return_values
;
1244 register CORE_ADDR pc
;
1245 char *args
, *arg1
, *read_line ();
1249 values
.sals
= (struct symtab_and_line
*) alloca (nelts
* sizeof(struct symtab_and_line
));
1250 return_values
.sals
= (struct symtab_and_line
*) malloc (nelts
* sizeof(struct symtab_and_line
));
1253 printf("[0] cancel\n[1] all\n");
1256 if (sym_arr
[i
] && SYMBOL_CLASS (sym_arr
[i
]) == LOC_BLOCK
)
1258 /* Arg is the name of a function */
1259 pc
= BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr
[i
]))
1260 + FUNCTION_START_OFFSET
;
1263 values
.sals
[i
] = find_pc_line (pc
, 0);
1264 printf("[%d] file:%s; line number:%d\n",
1265 (i
+2), values
.sals
[i
].symtab
->filename
, values
.sals
[i
].line
);
1267 else printf ("?HERE\n");
1271 if ((prompt
= getenv ("PS2")) == NULL
)
1275 printf("%s ",prompt
);
1278 args
= read_line (0);
1281 error_no_arg ("one or more choice numbers");
1289 while (*arg1
>= '0' && *arg1
<= '9') arg1
++;
1290 if (*arg1
&& *arg1
!= ' ' && *arg1
!= '\t')
1291 error ("Arguments must be choice numbers.");
1296 error ("cancelled");
1299 bcopy (values
.sals
, return_values
.sals
, (nelts
* sizeof(struct symtab_and_line
)));
1300 return_values
.nelts
= nelts
;
1301 return return_values
;
1304 if (num
> nelts
+ 2)
1306 printf ("No choice number %d.\n", num
);
1311 if (values
.sals
[num
].pc
)
1313 return_values
.sals
[i
++] = values
.sals
[num
];
1314 values
.sals
[num
].pc
= 0;
1318 printf ("duplicate request for %d ignored.\n", num
);
1323 while (*args
== ' ' || *args
== '\t') args
++;
1325 return_values
.nelts
= i
;
1326 return return_values
;
1329 /* Return the index of misc function named NAME. */
1332 lookup_misc_func (name
)
1333 register char *name
;
1337 for (i
= 0; i
< misc_function_count
; i
++)
1338 if (!strcmp (misc_function_vector
[i
].name
, name
))
1340 return -1; /* not found */
1346 register struct symtab
*s
;
1347 register int column
= 0;
1349 if (symtab_list
== 0)
1351 printf ("No symbol table is loaded.\n");
1354 printf ("Source files for which symbol table is known:\n");
1355 for (s
= symtab_list
; s
; s
= s
->next
)
1357 if (column
!= 0 && column
+ strlen (s
->filename
) >= 70)
1362 else if (column
!= 0)
1367 printf ("%s", s
->filename
);
1368 column
+= strlen (s
->filename
);
1378 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
1379 If CLASS is zero, list all symbols except functions and type names.
1380 If CLASS is 1, list only functions.
1381 If CLASS is 2, list only type names. */
1385 if (print_count >= 21) \
1386 { printf ("--Type Return to print more--"); \
1392 list_symbols (regexp
, class)
1396 register struct symtab
*s
;
1397 register struct blockvector
*bv
;
1398 struct blockvector
*prev_bv
= 0;
1399 register struct block
*b
;
1401 register struct symbol
*sym
;
1404 static char *classnames
[]
1405 = {"variable", "function", "type", "method"};
1406 int print_count
= 0;
1409 if (val
= (char *) re_comp (regexp
))
1410 error ("Invalid regexp: %s", val
);
1413 ? "All %ss matching regular expression \"%s\":\n"
1414 : "All defined %ss:\n",
1418 for (s
= symtab_list
; s
; s
= s
->next
)
1421 bv
= BLOCKVECTOR (s
);
1422 /* Often many files share a blockvector.
1423 Scan each blockvector only once so that
1424 we don't get every symbol many times.
1425 It happens that the first symtab in the list
1426 for any given blockvector is the main file. */
1428 for (i
= 0; i
< 2; i
++)
1430 b
= BLOCKVECTOR_BLOCK (bv
, i
);
1431 for (j
= 0; j
< BLOCK_NSYMS (b
); j
++)
1434 sym
= BLOCK_SYM (b
, j
);
1435 if ((regexp
== 0 || re_exec (SYMBOL_NAME (sym
)))
1436 && ((class == 0 && SYMBOL_CLASS (sym
) != LOC_TYPEDEF
1437 && SYMBOL_CLASS (sym
) != LOC_BLOCK
)
1438 || (class == 1 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)
1439 || (class == 2 && SYMBOL_CLASS (sym
) == LOC_TYPEDEF
)
1440 || (class == 3 && SYMBOL_CLASS (sym
) == LOC_BLOCK
)))
1444 printf ("\nFile %s:\n", s
->filename
);
1449 if (class != 2 && i
== 1)
1452 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
)
1453 printf ("typedef ");
1457 type_print (SYMBOL_TYPE (sym
),
1458 (SYMBOL_CLASS (sym
) == LOC_TYPEDEF
1459 ? "" : SYMBOL_NAME (sym
)),
1467 type_print_base (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0, 0);
1468 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t
, i
), stdout
, 0);
1469 sprintf (buf
, " %s::", TYPE_NAME (t
));
1470 type_print_method_args (TYPE_FN_FIELD_ARGS (t
, i
), buf
, name
, stdout
);
1474 && SYMBOL_NAMESPACE (sym
) != STRUCT_NAMESPACE
1475 && (TYPE_NAME ((SYMBOL_TYPE (sym
))) == 0
1476 || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym
))),
1477 SYMBOL_NAME (sym
))))
1478 printf (" %s", SYMBOL_NAME (sym
));
1487 variables_info (regexp
)
1490 list_symbols (regexp
, 0);
1494 functions_info (regexp
)
1497 list_symbols (regexp
, 1);
1504 list_symbols (regexp
, 2);
1508 methods_info (regexp
)
1511 list_symbols (regexp
, 3);
1514 /* Initialize the standard C scalar types. */
1518 init_type (code
, length
, uns
, name
)
1519 enum type_code code
;
1523 register struct type
*type
;
1525 type
= (struct type
*) xmalloc (sizeof (struct type
));
1526 bzero (type
, sizeof *type
);
1527 TYPE_MAIN_VARIANT (type
) = type
;
1528 TYPE_CODE (type
) = code
;
1529 TYPE_LENGTH (type
) = length
;
1530 TYPE_FLAGS (type
) = uns
? TYPE_FLAG_UNSIGNED
: 0;
1531 TYPE_FLAGS (type
) |= TYPE_FLAG_PERM
;
1532 TYPE_NFIELDS (type
) = 0;
1533 TYPE_NAME (type
) = name
;
1536 TYPE_NFN_FIELDS (type
) = 0;
1537 TYPE_BASECLASS (type
) = 0;
1544 add_info ("variables", variables_info
,
1545 "All global and static variable names, or those matching REGEXP.");
1546 add_info ("functions", functions_info
,
1547 "All function names, or those matching REGEXP.");
1548 add_info ("types", types_info
,
1549 "All types names, or those matching REGEXP.");
1550 add_info ("methods", methods_info
,
1551 "All method names, or those matching REGEXP::REGEXP.\n\
1552 If the class qualifier is ommited, it is assumed to be the current scope.\n\
1553 If the first REGEXP is ommited, then all methods matching the second REGEXP\n\
1555 add_info ("sources", sources_info
,
1556 "Source files in the program.");
1558 obstack_init (symbol_obstack
);
1560 builtin_type_void
= init_type (TYPE_CODE_VOID
, 0, 0, "void");
1562 builtin_type_float
= init_type (TYPE_CODE_FLT
, sizeof (float), 0, "float");
1563 builtin_type_double
= init_type (TYPE_CODE_FLT
, sizeof (double), 0, "double");
1565 builtin_type_char
= init_type (TYPE_CODE_INT
, sizeof (char), 0, "char");
1566 builtin_type_short
= init_type (TYPE_CODE_INT
, sizeof (short), 0, "short");
1567 builtin_type_long
= init_type (TYPE_CODE_INT
, sizeof (long), 0, "long");
1568 builtin_type_int
= init_type (TYPE_CODE_INT
, sizeof (int), 0, "int");
1570 builtin_type_unsigned_char
= init_type (TYPE_CODE_INT
, sizeof (char), 1, "unsigned char");
1571 builtin_type_unsigned_short
= init_type (TYPE_CODE_INT
, sizeof (short), 1, "unsigned short");
1572 builtin_type_unsigned_long
= init_type (TYPE_CODE_INT
, sizeof (long), 1, "unsigned long");
1573 builtin_type_unsigned_int
= init_type (TYPE_CODE_INT
, sizeof (int), 1, "unsigned int");