7b4ac7e1 |
1 | /* Symbol table lookup for the GNU debugger, GDB. |
4187119d |
2 | Copyright (C) 1986, 1987, 1988, 1989 Free Software Foundation, Inc. |
7b4ac7e1 |
3 | |
4187119d |
4 | This file is part of GDB. |
7b4ac7e1 |
5 | |
4187119d |
6 | GDB 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 1, or (at your option) |
9 | any later version. |
7b4ac7e1 |
10 | |
4187119d |
11 | GDB 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. |
15 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with GDB; see the file COPYING. If not, write to |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ |
7b4ac7e1 |
19 | |
7a67dd45 |
20 | #include <stdio.h> |
7b4ac7e1 |
21 | #include "defs.h" |
7b4ac7e1 |
22 | #include "symtab.h" |
23 | #include "param.h" |
24 | |
7b4ac7e1 |
25 | #include <obstack.h> |
e91b87a3 |
26 | #include <assert.h> |
7b4ac7e1 |
27 | |
4187119d |
28 | char *index (); |
bb7592f0 |
29 | |
7b4ac7e1 |
30 | /* Allocate an obstack to hold objects that should be freed |
31 | when we load a new symbol table. |
32 | This includes the symbols made by dbxread |
33 | and the types that are not permanent. */ |
34 | |
35 | struct obstack obstack1; |
36 | |
37 | struct obstack *symbol_obstack = &obstack1; |
38 | |
e91b87a3 |
39 | /* This obstack will be used for partial_symbol objects. It can |
40 | probably actually be the same as the symbol_obstack above, but I'd |
41 | like to keep them seperate for now. If I want to later, I'll |
42 | replace one with the other. */ |
43 | |
44 | struct obstack obstack2; |
45 | |
46 | struct obstack *psymbol_obstack = &obstack2; |
47 | |
7b4ac7e1 |
48 | /* These variables point to the objects |
49 | representing the predefined C data types. */ |
50 | |
51 | struct type *builtin_type_void; |
52 | struct type *builtin_type_char; |
53 | struct type *builtin_type_short; |
54 | struct type *builtin_type_int; |
55 | struct type *builtin_type_long; |
e91b87a3 |
56 | #ifdef LONG_LONG |
57 | struct type *builtin_type_long_long; |
58 | #endif |
7b4ac7e1 |
59 | struct type *builtin_type_unsigned_char; |
60 | struct type *builtin_type_unsigned_short; |
61 | struct type *builtin_type_unsigned_int; |
62 | struct type *builtin_type_unsigned_long; |
e91b87a3 |
63 | #ifdef LONG_LONG |
64 | struct type *builtin_type_unsigned_long_long; |
65 | #endif |
7b4ac7e1 |
66 | struct type *builtin_type_float; |
67 | struct type *builtin_type_double; |
68 | |
e91b87a3 |
69 | /* Block in which the most recently searched-for symbol was found. |
70 | Might be better to make this a parameter to lookup_symbol and |
71 | value_of_this. */ |
72 | struct block *block_found; |
73 | |
74 | /* Functions */ |
75 | static int find_line_common (); |
76 | static int lookup_misc_func (); |
77 | struct partial_symtab *lookup_partial_symtab (); |
78 | struct symtab *psymtab_to_symtab (); |
79 | static struct partial_symbol *lookup_partial_symbol (); |
80 | |
4187119d |
81 | /* Check for a symtab of a specific name; first in symtabs, then in |
82 | psymtabs. *If* there is no '/' in the name, a match after a '/' |
83 | in the symtab filename will also work. */ |
7b4ac7e1 |
84 | |
4187119d |
85 | static struct symtab * |
86 | lookup_symtab_1 (name) |
7b4ac7e1 |
87 | char *name; |
88 | { |
89 | register struct symtab *s; |
e91b87a3 |
90 | register struct partial_symtab *ps; |
4187119d |
91 | register char *slash = index (name, '/'); |
92 | register int len = strlen (name); |
7b4ac7e1 |
93 | |
94 | for (s = symtab_list; s; s = s->next) |
95 | if (!strcmp (name, s->filename)) |
96 | return s; |
97 | |
4187119d |
98 | for (ps = partial_symtab_list; ps; ps = ps->next) |
99 | if (!strcmp (name, ps->filename)) |
100 | { |
101 | if (ps->readin) |
102 | fatal ("Internal: readin pst found when no symtab found."); |
103 | s = psymtab_to_symtab (ps); |
104 | return s; |
105 | } |
106 | |
107 | if (!slash) |
108 | { |
109 | for (s = symtab_list; s; s = s->next) |
110 | { |
111 | int l = strlen (s->filename); |
112 | |
113 | if (s->filename[l - len -1] == '/' |
114 | && !strcmp (s->filename + l - len, name)) |
115 | return s; |
116 | } |
117 | |
118 | for (ps = partial_symtab_list; ps; ps = ps->next) |
119 | { |
120 | int l = strlen (ps->filename); |
121 | |
122 | if (ps->filename[l - len - 1] == '/' |
123 | && !strcmp (ps->filename + l - len, name)) |
124 | { |
125 | if (ps->readin) |
126 | fatal ("Internal: readin pst found when no symtab found."); |
127 | s = psymtab_to_symtab (ps); |
128 | return s; |
129 | } |
130 | } |
131 | } |
132 | return 0; |
133 | } |
134 | |
135 | /* Lookup the symbol table of a source file named NAME. Try a couple |
136 | of variations if the first lookup doesn't work. */ |
137 | |
138 | struct symtab * |
139 | lookup_symtab (name) |
140 | char *name; |
141 | { |
142 | register struct symtab *s; |
143 | register char *copy; |
144 | |
145 | s = lookup_symtab_1 (name); |
146 | if (s) return s; |
147 | |
7b4ac7e1 |
148 | /* If name not found as specified, see if adding ".c" helps. */ |
149 | |
150 | copy = (char *) alloca (strlen (name) + 3); |
151 | strcpy (copy, name); |
152 | strcat (copy, ".c"); |
4187119d |
153 | s = lookup_symtab_1 (copy); |
154 | if (s) return s; |
e91b87a3 |
155 | |
4187119d |
156 | /* We didn't find anything; die. */ |
e91b87a3 |
157 | return 0; |
158 | } |
159 | |
4187119d |
160 | /* Lookup the partial symbol table of a source file named NAME. This |
161 | only returns true on an exact match (ie. this semantics are |
162 | different from lookup_symtab. */ |
e91b87a3 |
163 | |
164 | struct partial_symtab * |
165 | lookup_partial_symtab (name) |
166 | char *name; |
167 | { |
168 | register struct partial_symtab *s; |
169 | register char *copy; |
170 | |
171 | for (s = partial_symtab_list; s; s = s->next) |
172 | if (!strcmp (name, s->filename)) |
173 | return s; |
174 | |
7b4ac7e1 |
175 | return 0; |
176 | } |
177 | \f |
178 | /* Lookup a typedef or primitive type named NAME, |
179 | visible in lexical block BLOCK. |
180 | If NOERR is nonzero, return zero if NAME is not suitably defined. */ |
181 | |
182 | struct type * |
183 | lookup_typename (name, block, noerr) |
184 | char *name; |
185 | struct block *block; |
186 | int noerr; |
187 | { |
e91b87a3 |
188 | register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE, 0); |
7b4ac7e1 |
189 | if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF) |
190 | { |
191 | if (!strcmp (name, "int")) |
192 | return builtin_type_int; |
193 | if (!strcmp (name, "long")) |
194 | return builtin_type_long; |
195 | if (!strcmp (name, "short")) |
196 | return builtin_type_short; |
197 | if (!strcmp (name, "char")) |
198 | return builtin_type_char; |
199 | if (!strcmp (name, "float")) |
200 | return builtin_type_float; |
201 | if (!strcmp (name, "double")) |
202 | return builtin_type_double; |
203 | if (!strcmp (name, "void")) |
204 | return builtin_type_void; |
205 | |
206 | if (noerr) |
207 | return 0; |
208 | error ("No type named %s.", name); |
209 | } |
210 | return SYMBOL_TYPE (sym); |
211 | } |
212 | |
213 | struct type * |
214 | lookup_unsigned_typename (name) |
215 | char *name; |
216 | { |
217 | if (!strcmp (name, "int")) |
218 | return builtin_type_unsigned_int; |
219 | if (!strcmp (name, "long")) |
220 | return builtin_type_unsigned_long; |
221 | if (!strcmp (name, "short")) |
222 | return builtin_type_unsigned_short; |
223 | if (!strcmp (name, "char")) |
224 | return builtin_type_unsigned_char; |
225 | error ("No type named unsigned %s.", name); |
226 | } |
227 | |
228 | /* Lookup a structure type named "struct NAME", |
229 | visible in lexical block BLOCK. */ |
230 | |
231 | struct type * |
232 | lookup_struct (name, block) |
233 | char *name; |
234 | struct block *block; |
235 | { |
e91b87a3 |
236 | register struct symbol *sym |
237 | = lookup_symbol (name, block, STRUCT_NAMESPACE, 0); |
238 | |
7b4ac7e1 |
239 | if (sym == 0) |
240 | error ("No struct type named %s.", name); |
241 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT) |
e91b87a3 |
242 | error ("This context has class, union or enum %s, not a struct.", name); |
7b4ac7e1 |
243 | return SYMBOL_TYPE (sym); |
244 | } |
245 | |
246 | /* Lookup a union type named "union NAME", |
247 | visible in lexical block BLOCK. */ |
248 | |
249 | struct type * |
250 | lookup_union (name, block) |
251 | char *name; |
252 | struct block *block; |
253 | { |
e91b87a3 |
254 | register struct symbol *sym |
255 | = lookup_symbol (name, block, STRUCT_NAMESPACE, 0); |
256 | |
7b4ac7e1 |
257 | if (sym == 0) |
258 | error ("No union type named %s.", name); |
259 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION) |
e91b87a3 |
260 | error ("This context has class, struct or enum %s, not a union.", name); |
7b4ac7e1 |
261 | return SYMBOL_TYPE (sym); |
262 | } |
263 | |
264 | /* Lookup an enum type named "enum NAME", |
265 | visible in lexical block BLOCK. */ |
266 | |
267 | struct type * |
268 | lookup_enum (name, block) |
269 | char *name; |
270 | struct block *block; |
271 | { |
e91b87a3 |
272 | register struct symbol *sym |
273 | = lookup_symbol (name, block, STRUCT_NAMESPACE, 0); |
7b4ac7e1 |
274 | if (sym == 0) |
275 | error ("No enum type named %s.", name); |
276 | if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM) |
e91b87a3 |
277 | error ("This context has class, struct or union %s, not an enum.", name); |
7b4ac7e1 |
278 | return SYMBOL_TYPE (sym); |
279 | } |
280 | |
4187119d |
281 | /* Given a type TYPE, lookup the type of the component of type named |
282 | NAME. */ |
283 | |
284 | struct type * |
285 | lookup_struct_elt_type (type, name) |
286 | struct type *type; |
287 | char *name; |
288 | { |
289 | struct type *t; |
290 | int i; |
291 | char *errmsg; |
292 | |
293 | if (TYPE_CODE (type) != TYPE_CODE_STRUCT |
294 | && TYPE_CODE (type) != TYPE_CODE_UNION) |
295 | { |
296 | terminal_ours (); |
297 | fflush (stdout); |
298 | fprintf (stderr, "Type "); |
299 | type_print (type, "", stderr, -1); |
300 | fprintf (stderr, " is not a structure or union type.\n"); |
301 | return_to_top_level (); |
302 | } |
303 | |
304 | for (i = TYPE_NFIELDS (type) - 1; i >= 0; i--) |
305 | if (!strcmp (TYPE_FIELD_NAME (type, i), name)) |
306 | return TYPE_FIELD_TYPE (type, i); |
307 | |
308 | terminal_ours (); |
309 | fflush (stdout); |
310 | fprintf (stderr, "Type "); |
311 | type_print (type, "", stderr, -1); |
312 | fprintf (stderr, " has no component named %s\n", name); |
313 | return_to_top_level (); |
314 | } |
315 | |
7b4ac7e1 |
316 | /* Given a type TYPE, return a type of pointers to that type. |
bb7592f0 |
317 | May need to construct such a type if this is the first use. |
318 | |
319 | C++: use TYPE_MAIN_VARIANT and TYPE_CHAIN to keep pointer |
320 | to member types under control. */ |
7b4ac7e1 |
321 | |
322 | struct type * |
323 | lookup_pointer_type (type) |
324 | struct type *type; |
325 | { |
326 | register struct type *ptype = TYPE_POINTER_TYPE (type); |
bb7592f0 |
327 | if (ptype) return TYPE_MAIN_VARIANT (ptype); |
7b4ac7e1 |
328 | |
329 | /* This is the first time anyone wanted a pointer to a TYPE. */ |
330 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
331 | ptype = (struct type *) xmalloc (sizeof (struct type)); |
332 | else |
333 | ptype = (struct type *) obstack_alloc (symbol_obstack, |
334 | sizeof (struct type)); |
335 | |
336 | bzero (ptype, sizeof (struct type)); |
bb7592f0 |
337 | TYPE_MAIN_VARIANT (ptype) = ptype; |
7b4ac7e1 |
338 | TYPE_TARGET_TYPE (ptype) = type; |
339 | TYPE_POINTER_TYPE (type) = ptype; |
340 | /* New type is permanent if type pointed to is permanent. */ |
341 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
342 | TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM; |
343 | /* We assume the machine has only one representation for pointers! */ |
344 | TYPE_LENGTH (ptype) = sizeof (char *); |
345 | TYPE_CODE (ptype) = TYPE_CODE_PTR; |
346 | return ptype; |
347 | } |
348 | |
bb7592f0 |
349 | struct type * |
350 | lookup_reference_type (type) |
351 | struct type *type; |
352 | { |
353 | register struct type *rtype = TYPE_REFERENCE_TYPE (type); |
354 | if (rtype) return TYPE_MAIN_VARIANT (rtype); |
355 | |
356 | /* This is the first time anyone wanted a pointer to a TYPE. */ |
357 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
358 | rtype = (struct type *) xmalloc (sizeof (struct type)); |
359 | else |
360 | rtype = (struct type *) obstack_alloc (symbol_obstack, |
361 | sizeof (struct type)); |
362 | |
363 | bzero (rtype, sizeof (struct type)); |
364 | TYPE_MAIN_VARIANT (rtype) = rtype; |
365 | TYPE_TARGET_TYPE (rtype) = type; |
366 | TYPE_REFERENCE_TYPE (type) = rtype; |
367 | /* New type is permanent if type pointed to is permanent. */ |
368 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
369 | TYPE_FLAGS (rtype) |= TYPE_FLAG_PERM; |
370 | /* We assume the machine has only one representation for pointers! */ |
371 | TYPE_LENGTH (rtype) = sizeof (char *); |
372 | TYPE_CODE (rtype) = TYPE_CODE_REF; |
373 | return rtype; |
374 | } |
375 | |
e91b87a3 |
376 | |
bb7592f0 |
377 | /* Implement direct support for MEMBER_TYPE in GNU C++. |
378 | May need to construct such a type if this is the first use. |
379 | The TYPE is the type of the member. The DOMAIN is the type |
380 | of the aggregate that the member belongs to. */ |
381 | |
382 | struct type * |
e91b87a3 |
383 | lookup_member_type (type, domain) |
384 | struct type *type, *domain; |
bb7592f0 |
385 | { |
386 | register struct type *mtype = TYPE_MAIN_VARIANT (type); |
387 | struct type *main_type; |
388 | |
389 | main_type = mtype; |
390 | while (mtype) |
391 | { |
392 | if (TYPE_DOMAIN_TYPE (mtype) == domain) |
393 | return mtype; |
394 | mtype = TYPE_NEXT_VARIANT (mtype); |
395 | } |
396 | |
397 | /* This is the first time anyone wanted this member type. */ |
398 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
399 | mtype = (struct type *) xmalloc (sizeof (struct type)); |
400 | else |
401 | mtype = (struct type *) obstack_alloc (symbol_obstack, |
402 | sizeof (struct type)); |
403 | |
404 | bzero (mtype, sizeof (struct type)); |
e91b87a3 |
405 | if (main_type == 0) |
406 | main_type = mtype; |
bb7592f0 |
407 | else |
408 | { |
409 | TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type); |
410 | TYPE_NEXT_VARIANT (main_type) = mtype; |
411 | } |
412 | TYPE_MAIN_VARIANT (mtype) = main_type; |
413 | TYPE_TARGET_TYPE (mtype) = type; |
414 | TYPE_DOMAIN_TYPE (mtype) = domain; |
415 | /* New type is permanent if type pointed to is permanent. */ |
416 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
417 | TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM; |
418 | |
419 | /* In practice, this is never used. */ |
420 | TYPE_LENGTH (mtype) = 1; |
421 | TYPE_CODE (mtype) = TYPE_CODE_MEMBER; |
422 | |
e91b87a3 |
423 | #if 0 |
424 | /* Now splice in the new member pointer type. */ |
425 | if (main_type) |
426 | { |
427 | /* This type was not "smashed". */ |
428 | TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type); |
429 | TYPE_CHAIN (main_type) = mtype; |
430 | } |
431 | #endif |
432 | |
bb7592f0 |
433 | return mtype; |
434 | } |
435 | |
4187119d |
436 | struct type * |
437 | lookup_method_type (type, domain, args) |
438 | struct type *type, *domain, **args; |
439 | { |
440 | register struct type *mtype = TYPE_MAIN_VARIANT (type); |
441 | struct type *main_type; |
442 | |
443 | main_type = mtype; |
444 | while (mtype) |
445 | { |
446 | if (TYPE_DOMAIN_TYPE (mtype) == domain) |
447 | { |
448 | struct type **t1 = args; |
449 | struct type **t2 = TYPE_ARG_TYPES (mtype); |
450 | if (t2) |
451 | { |
452 | int i; |
453 | for (i = 0; t1[i] != 0 && t1[i]->code != TYPE_CODE_VOID; i++) |
454 | if (t1[i] != t2[i]) |
455 | break; |
456 | if (t1[i] == t2[i]) |
457 | return mtype; |
458 | } |
459 | } |
460 | mtype = TYPE_NEXT_VARIANT (mtype); |
461 | } |
462 | |
463 | /* This is the first time anyone wanted this member type. */ |
464 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
465 | mtype = (struct type *) xmalloc (sizeof (struct type)); |
466 | else |
467 | mtype = (struct type *) obstack_alloc (symbol_obstack, |
468 | sizeof (struct type)); |
469 | |
470 | bzero (mtype, sizeof (struct type)); |
471 | if (main_type == 0) |
472 | main_type = mtype; |
473 | else |
474 | { |
475 | TYPE_NEXT_VARIANT (mtype) = TYPE_NEXT_VARIANT (main_type); |
476 | TYPE_NEXT_VARIANT (main_type) = mtype; |
477 | } |
478 | TYPE_MAIN_VARIANT (mtype) = main_type; |
479 | TYPE_TARGET_TYPE (mtype) = type; |
480 | TYPE_DOMAIN_TYPE (mtype) = domain; |
481 | TYPE_ARG_TYPES (mtype) = args; |
482 | /* New type is permanent if type pointed to is permanent. */ |
483 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
484 | TYPE_FLAGS (mtype) |= TYPE_FLAG_PERM; |
485 | |
486 | /* In practice, this is never used. */ |
487 | TYPE_LENGTH (mtype) = 1; |
488 | TYPE_CODE (mtype) = TYPE_CODE_METHOD; |
489 | |
490 | #if 0 |
491 | /* Now splice in the new member pointer type. */ |
492 | if (main_type) |
493 | { |
494 | /* This type was not "smashed". */ |
495 | TYPE_CHAIN (mtype) = TYPE_CHAIN (main_type); |
496 | TYPE_CHAIN (main_type) = mtype; |
497 | } |
498 | #endif |
499 | |
500 | return mtype; |
501 | } |
502 | |
bb7592f0 |
503 | /* Given a type TYPE, return a type which has offset OFFSET, |
504 | via_virtual VIA_VIRTUAL, and via_public VIA_PUBLIC. |
505 | May need to construct such a type if none exists. */ |
506 | struct type * |
507 | lookup_basetype_type (type, offset, via_virtual, via_public) |
508 | struct type *type; |
509 | int offset; |
510 | int via_virtual, via_public; |
511 | { |
512 | register struct type *btype = TYPE_MAIN_VARIANT (type); |
513 | struct type *main_type; |
514 | |
515 | if (offset != 0) |
516 | { |
4187119d |
517 | printf ("Internal error: type offset non-zero in lookup_basetype_type"); |
bb7592f0 |
518 | offset = 0; |
519 | } |
520 | |
521 | main_type = btype; |
522 | while (btype) |
523 | { |
524 | if (/* TYPE_OFFSET (btype) == offset |
525 | && */ TYPE_VIA_PUBLIC (btype) == via_public |
526 | && TYPE_VIA_VIRTUAL (btype) == via_virtual) |
527 | return btype; |
528 | btype = TYPE_NEXT_VARIANT (btype); |
529 | } |
530 | |
531 | /* This is the first time anyone wanted this member type. */ |
532 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
533 | btype = (struct type *) xmalloc (sizeof (struct type)); |
534 | else |
535 | btype = (struct type *) obstack_alloc (symbol_obstack, |
536 | sizeof (struct type)); |
537 | |
538 | if (main_type == 0) |
539 | { |
540 | main_type = btype; |
541 | bzero (btype, sizeof (struct type)); |
542 | TYPE_MAIN_VARIANT (btype) = main_type; |
543 | } |
544 | else |
545 | { |
546 | bcopy (main_type, btype, sizeof (struct type)); |
547 | TYPE_NEXT_VARIANT (main_type) = btype; |
548 | } |
549 | /* TYPE_OFFSET (btype) = offset; */ |
550 | if (via_public) |
551 | TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_PUBLIC; |
552 | if (via_virtual) |
553 | TYPE_FLAGS (btype) |= TYPE_FLAG_VIA_VIRTUAL; |
554 | /* New type is permanent if type pointed to is permanent. */ |
555 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
556 | TYPE_FLAGS (btype) |= TYPE_FLAG_PERM; |
557 | |
558 | /* In practice, this is never used. */ |
559 | TYPE_LENGTH (btype) = 1; |
560 | TYPE_CODE (btype) = TYPE_CODE_STRUCT; |
561 | |
562 | return btype; |
563 | } |
564 | |
7b4ac7e1 |
565 | /* Given a type TYPE, return a type of functions that return that type. |
566 | May need to construct such a type if this is the first use. */ |
567 | |
568 | struct type * |
e91b87a3 |
569 | lookup_function_type (type) |
7b4ac7e1 |
570 | struct type *type; |
571 | { |
572 | register struct type *ptype = TYPE_FUNCTION_TYPE (type); |
573 | if (ptype) return ptype; |
574 | |
575 | /* This is the first time anyone wanted a function returning a TYPE. */ |
576 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
577 | ptype = (struct type *) xmalloc (sizeof (struct type)); |
578 | else |
579 | ptype = (struct type *) obstack_alloc (symbol_obstack, |
580 | sizeof (struct type)); |
581 | |
582 | bzero (ptype, sizeof (struct type)); |
583 | TYPE_TARGET_TYPE (ptype) = type; |
584 | TYPE_FUNCTION_TYPE (type) = ptype; |
585 | /* New type is permanent if type returned is permanent. */ |
586 | if (TYPE_FLAGS (type) & TYPE_FLAG_PERM) |
587 | TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM; |
588 | TYPE_LENGTH (ptype) = 1; |
589 | TYPE_CODE (ptype) = TYPE_CODE_FUNC; |
590 | TYPE_NFIELDS (ptype) = 0; |
591 | return ptype; |
592 | } |
4187119d |
593 | \f |
594 | /* Create an array type. Elements will be of type TYPE, and there will |
595 | be NUM of them. |
596 | |
597 | Eventually this should be extended to take two more arguments which |
598 | specify the bounds of the array and the type of the index. |
599 | It should also be changed to be a "lookup" function, with the |
600 | appropriate data structures added to the type field. |
601 | Then read array type should call here. */ |
602 | |
603 | struct type * |
604 | create_array_type (element_type, number) |
605 | struct type *element_type; |
606 | int number; |
607 | { |
608 | struct type *result_type = (struct type *) |
609 | obstack_alloc (symbol_obstack, sizeof (struct type)); |
610 | |
611 | bzero (result_type, sizeof (struct type)); |
612 | |
613 | TYPE_CODE (result_type) = TYPE_CODE_ARRAY; |
614 | TYPE_TARGET_TYPE (result_type) = element_type; |
615 | TYPE_LENGTH (result_type) = number * TYPE_LENGTH (element_type); |
616 | TYPE_NFIELDS (result_type) = 1; |
617 | TYPE_FIELDS (result_type) = |
618 | (struct field *) obstack_alloc (symbol_obstack, sizeof (struct field)); |
619 | TYPE_FIELD_TYPE (result_type, 0) = builtin_type_int; |
620 | TYPE_VPTR_FIELDNO (result_type) = -1; |
621 | |
622 | return result_type; |
623 | } |
624 | |
7b4ac7e1 |
625 | \f |
626 | /* Smash TYPE to be a type of pointers to TO_TYPE. |
627 | If TO_TYPE is not permanent and has no pointer-type yet, |
628 | record TYPE as its pointer-type. */ |
629 | |
630 | void |
631 | smash_to_pointer_type (type, to_type) |
632 | struct type *type, *to_type; |
633 | { |
4187119d |
634 | int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM); |
635 | |
7b4ac7e1 |
636 | bzero (type, sizeof (struct type)); |
637 | TYPE_TARGET_TYPE (type) = to_type; |
638 | /* We assume the machine has only one representation for pointers! */ |
639 | TYPE_LENGTH (type) = sizeof (char *); |
640 | TYPE_CODE (type) = TYPE_CODE_PTR; |
641 | |
bb7592f0 |
642 | TYPE_MAIN_VARIANT (type) = type; |
643 | |
4187119d |
644 | if (type_permanent) |
645 | TYPE_FLAGS (type) |= TYPE_FLAG_PERM; |
646 | |
7b4ac7e1 |
647 | if (TYPE_POINTER_TYPE (to_type) == 0 |
4187119d |
648 | && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM) |
649 | || type_permanent)) |
7b4ac7e1 |
650 | { |
651 | TYPE_POINTER_TYPE (to_type) = type; |
652 | } |
653 | } |
654 | |
bb7592f0 |
655 | /* Smash TYPE to be a type of members of DOMAIN with type TO_TYPE. */ |
656 | |
657 | void |
658 | smash_to_member_type (type, domain, to_type) |
659 | struct type *type, *domain, *to_type; |
660 | { |
661 | bzero (type, sizeof (struct type)); |
662 | TYPE_TARGET_TYPE (type) = to_type; |
663 | TYPE_DOMAIN_TYPE (type) = domain; |
664 | |
665 | /* In practice, this is never needed. */ |
666 | TYPE_LENGTH (type) = 1; |
667 | TYPE_CODE (type) = TYPE_CODE_MEMBER; |
668 | |
669 | TYPE_MAIN_VARIANT (type) = lookup_member_type (domain, to_type); |
670 | } |
671 | |
4187119d |
672 | /* Smash TYPE to be a type of method of DOMAIN with type TO_TYPE. */ |
673 | |
674 | void |
675 | smash_to_method_type (type, domain, to_type, args) |
676 | struct type *type, *domain, *to_type, **args; |
677 | { |
678 | bzero (type, sizeof (struct type)); |
679 | TYPE_TARGET_TYPE (type) = to_type; |
680 | TYPE_DOMAIN_TYPE (type) = domain; |
681 | TYPE_ARG_TYPES (type) = args; |
682 | |
683 | /* In practice, this is never needed. */ |
684 | TYPE_LENGTH (type) = 1; |
685 | TYPE_CODE (type) = TYPE_CODE_METHOD; |
686 | |
687 | TYPE_MAIN_VARIANT (type) = lookup_method_type (domain, to_type, args); |
688 | } |
689 | |
bb7592f0 |
690 | /* Smash TYPE to be a type of reference to TO_TYPE. |
691 | If TO_TYPE is not permanent and has no pointer-type yet, |
692 | record TYPE as its pointer-type. */ |
693 | |
694 | void |
695 | smash_to_reference_type (type, to_type) |
696 | struct type *type, *to_type; |
697 | { |
4187119d |
698 | int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM); |
699 | |
bb7592f0 |
700 | bzero (type, sizeof (struct type)); |
701 | TYPE_TARGET_TYPE (type) = to_type; |
702 | /* We assume the machine has only one representation for pointers! */ |
703 | TYPE_LENGTH (type) = sizeof (char *); |
704 | TYPE_CODE (type) = TYPE_CODE_REF; |
705 | |
706 | TYPE_MAIN_VARIANT (type) = type; |
707 | |
4187119d |
708 | if (type_permanent) |
709 | TYPE_FLAGS (type) |= TYPE_FLAG_PERM; |
710 | |
bb7592f0 |
711 | if (TYPE_REFERENCE_TYPE (to_type) == 0 |
4187119d |
712 | && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM) |
713 | || type_permanent)) |
bb7592f0 |
714 | { |
715 | TYPE_REFERENCE_TYPE (to_type) = type; |
716 | } |
717 | } |
718 | |
7b4ac7e1 |
719 | /* Smash TYPE to be a type of functions returning TO_TYPE. |
720 | If TO_TYPE is not permanent and has no function-type yet, |
721 | record TYPE as its function-type. */ |
722 | |
723 | void |
724 | smash_to_function_type (type, to_type) |
725 | struct type *type, *to_type; |
726 | { |
4187119d |
727 | int type_permanent = (TYPE_FLAGS (type) & TYPE_FLAG_PERM); |
728 | |
7b4ac7e1 |
729 | bzero (type, sizeof (struct type)); |
730 | TYPE_TARGET_TYPE (type) = to_type; |
731 | TYPE_LENGTH (type) = 1; |
732 | TYPE_CODE (type) = TYPE_CODE_FUNC; |
733 | TYPE_NFIELDS (type) = 0; |
734 | |
4187119d |
735 | if (type_permanent) |
736 | TYPE_FLAGS (type) |= TYPE_FLAG_PERM; |
737 | |
7b4ac7e1 |
738 | if (TYPE_FUNCTION_TYPE (to_type) == 0 |
4187119d |
739 | && (!(TYPE_FLAGS (to_type) & TYPE_FLAG_PERM) |
740 | || type_permanent)) |
7b4ac7e1 |
741 | { |
742 | TYPE_FUNCTION_TYPE (to_type) = type; |
743 | } |
744 | } |
4187119d |
745 | \f |
746 | /* Find which partial symtab on the partial_symtab_list contains |
747 | PC. Return 0 if none. */ |
748 | |
749 | struct partial_symtab * |
750 | find_pc_psymtab (pc) |
751 | register CORE_ADDR pc; |
752 | { |
753 | register struct partial_symtab *ps; |
754 | |
755 | for (ps = partial_symtab_list; ps; ps = ps->next) |
756 | if (pc >= ps->textlow && pc < ps->texthigh) |
757 | return ps; |
758 | |
759 | return 0; |
760 | } |
761 | |
762 | /* Find which partial symbol within a psymtab contains PC. Return 0 |
763 | if none. Check all psymtabs if PSYMTAB is 0. */ |
764 | struct partial_symbol * |
765 | find_pc_psymbol (psymtab, pc) |
766 | struct partial_symtab *psymtab; |
767 | CORE_ADDR pc; |
768 | { |
769 | struct partial_symbol *best, *p; |
770 | int best_pc; |
771 | |
772 | if (!psymtab) |
773 | psymtab = find_pc_psymtab (pc); |
774 | if (!psymtab) |
775 | return 0; |
776 | |
777 | best_pc = psymtab->textlow - 1; |
778 | |
779 | for (p = static_psymbols.list + psymtab->statics_offset; |
780 | (p - (static_psymbols.list + psymtab->statics_offset) |
781 | < psymtab->n_static_syms); |
782 | p++) |
783 | if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE |
784 | && SYMBOL_CLASS (p) == LOC_BLOCK |
785 | && pc >= SYMBOL_VALUE (p) |
786 | && SYMBOL_VALUE (p) > best_pc) |
787 | { |
788 | best_pc = SYMBOL_VALUE (p); |
789 | best = p; |
790 | } |
791 | if (best_pc == psymtab->textlow - 1) |
792 | return 0; |
793 | return best; |
794 | } |
795 | |
7b4ac7e1 |
796 | \f |
797 | static struct symbol *lookup_block_symbol (); |
798 | |
799 | /* Find the definition for a specified symbol name NAME |
800 | in namespace NAMESPACE, visible from lexical block BLOCK. |
e91b87a3 |
801 | Returns the struct symbol pointer, or zero if no symbol is found. |
802 | C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if |
803 | NAME is a field of the current implied argument `this'. If so set |
804 | *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. |
805 | BLOCK_FOUND is set to the block in which NAME is found (in the case of |
806 | a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */ |
7b4ac7e1 |
807 | |
bb7592f0 |
808 | struct symbol * |
e91b87a3 |
809 | lookup_symbol (name, block, namespace, is_a_field_of_this) |
bb7592f0 |
810 | char *name; |
811 | register struct block *block; |
812 | enum namespace namespace; |
e91b87a3 |
813 | int *is_a_field_of_this; |
bb7592f0 |
814 | { |
815 | register int i, n; |
816 | register struct symbol *sym; |
817 | register struct symtab *s; |
e91b87a3 |
818 | register struct partial_symtab *ps; |
bb7592f0 |
819 | struct blockvector *bv; |
820 | |
821 | /* Search specified block and its superiors. */ |
822 | |
823 | while (block != 0) |
824 | { |
825 | sym = lookup_block_symbol (block, name, namespace); |
e91b87a3 |
826 | if (sym) |
827 | { |
828 | block_found = block; |
829 | return sym; |
830 | } |
bb7592f0 |
831 | block = BLOCK_SUPERBLOCK (block); |
832 | } |
bb7592f0 |
833 | |
e91b87a3 |
834 | /* C++: If requested to do so by the caller, |
835 | check to see if NAME is a field of `this'. */ |
836 | if (is_a_field_of_this) |
837 | { |
838 | int v = (int) value_of_this (0); |
839 | |
840 | *is_a_field_of_this = 0; |
841 | if (v && check_field (v, name)) |
842 | { |
843 | *is_a_field_of_this = 1; |
844 | return 0; |
845 | } |
846 | } |
bb7592f0 |
847 | |
e91b87a3 |
848 | /* Now search all global blocks. Do the symtab's first, then |
849 | check the psymtab's */ |
bb7592f0 |
850 | |
851 | for (s = symtab_list; s; s = s->next) |
852 | { |
853 | bv = BLOCKVECTOR (s); |
854 | block = BLOCKVECTOR_BLOCK (bv, 0); |
855 | sym = lookup_block_symbol (block, name, namespace); |
e91b87a3 |
856 | if (sym) |
857 | { |
858 | block_found = block; |
859 | return sym; |
860 | } |
bb7592f0 |
861 | } |
862 | |
4187119d |
863 | /* Check for the possibility of the symbol being a global function |
864 | that is stored on the misc function vector. Eventually, all |
865 | global symbols might be resolved in this way. */ |
866 | |
867 | if (namespace == VAR_NAMESPACE) |
868 | { |
869 | int index = lookup_misc_func (name); |
870 | |
871 | if (index != -1) |
872 | { |
873 | ps = find_pc_psymtab (misc_function_vector[index].address); |
874 | if (ps && !ps->readin) |
875 | { |
876 | s = psymtab_to_symtab (ps); |
877 | bv = BLOCKVECTOR (s); |
878 | block = BLOCKVECTOR_BLOCK (bv, 0); |
879 | sym = lookup_block_symbol (block, name, namespace); |
880 | /* sym == 0 if symbol was found in the psymtab but not |
881 | in the symtab. |
882 | Return 0 to use the misc_function definition of "foo_". |
883 | |
884 | This happens for Fortran "foo_" symbols, |
885 | which are "foo" in the symtab. |
886 | |
887 | This can also happen if "asm" is used to make a |
888 | regular symbol but not a debugging symbol, e.g. |
889 | asm(".globl _main"); |
890 | asm("_main:"); |
891 | */ |
892 | |
893 | return sym; |
894 | } |
895 | } |
896 | } |
897 | |
e91b87a3 |
898 | for (ps = partial_symtab_list; ps; ps = ps->next) |
4187119d |
899 | if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace)) |
e91b87a3 |
900 | { |
901 | s = psymtab_to_symtab(ps); |
902 | bv = BLOCKVECTOR (s); |
903 | block = BLOCKVECTOR_BLOCK (bv, 0); |
904 | sym = lookup_block_symbol (block, name, namespace); |
905 | if (!sym) |
906 | fatal ("Internal: global symbol found in psymtab but not in symtab"); |
907 | return sym; |
908 | } |
909 | |
910 | /* Now search all per-file blocks. |
911 | Not strictly correct, but more useful than an error. |
912 | Do the symtabs first, then check the psymtabs */ |
bb7592f0 |
913 | |
914 | for (s = symtab_list; s; s = s->next) |
915 | { |
916 | bv = BLOCKVECTOR (s); |
917 | block = BLOCKVECTOR_BLOCK (bv, 1); |
918 | sym = lookup_block_symbol (block, name, namespace); |
e91b87a3 |
919 | if (sym) |
920 | { |
921 | block_found = block; |
922 | return sym; |
923 | } |
bb7592f0 |
924 | } |
e91b87a3 |
925 | |
926 | for (ps = partial_symtab_list; ps; ps = ps->next) |
4187119d |
927 | if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace)) |
e91b87a3 |
928 | { |
929 | s = psymtab_to_symtab(ps); |
930 | bv = BLOCKVECTOR (s); |
931 | block = BLOCKVECTOR_BLOCK (bv, 1); |
932 | sym = lookup_block_symbol (block, name, namespace); |
933 | if (!sym) |
934 | fatal ("Internal: static symbol found in psymtab but not in symtab"); |
935 | return sym; |
936 | } |
937 | |
bb7592f0 |
938 | return 0; |
939 | } |
940 | |
e91b87a3 |
941 | /* Look, in partial_symtab PST, for symbol NAME. Check the global |
942 | symbols if GLOBAL, the static symbols if not */ |
943 | |
944 | static struct partial_symbol * |
945 | lookup_partial_symbol (pst, name, global, namespace) |
946 | struct partial_symtab *pst; |
7b4ac7e1 |
947 | char *name; |
e91b87a3 |
948 | int global; |
7b4ac7e1 |
949 | enum namespace namespace; |
950 | { |
e91b87a3 |
951 | struct partial_symbol *start, *psym; |
952 | int length = (global ? pst->n_global_syms : pst->n_static_syms); |
7b4ac7e1 |
953 | |
e91b87a3 |
954 | start = (global ? |
4187119d |
955 | global_psymbols.list + pst->globals_offset : |
956 | static_psymbols.list + pst->statics_offset ); |
7b4ac7e1 |
957 | |
e91b87a3 |
958 | if (!length) |
959 | return (struct partial_symbol *) 0; |
960 | |
961 | if (global) /* This means we can use a binary */ |
962 | /* search. */ |
7b4ac7e1 |
963 | { |
e91b87a3 |
964 | struct partial_symbol *top, *bottom, *center; |
965 | |
966 | /* Binary search. This search is guarranteed to end with center |
967 | pointing at the earliest partial symbol with the correct |
968 | name. At that point *all* partial symbols with that name |
969 | will be checked against the correct namespace. */ |
970 | bottom = start; |
971 | top = start + length - 1; |
972 | while (top > bottom) |
973 | { |
974 | center = bottom + (top - bottom) / 2; |
7b4ac7e1 |
975 | |
e91b87a3 |
976 | assert (center < top); |
977 | |
978 | if (strcmp (SYMBOL_NAME (center), name) >= 0) |
979 | top = center; |
980 | else |
981 | bottom = center + 1; |
982 | } |
983 | assert (top == bottom); |
984 | |
985 | while (!strcmp (SYMBOL_NAME (top), name)) |
986 | { |
987 | if (SYMBOL_NAMESPACE (top) == namespace) |
988 | return top; |
989 | top ++; |
990 | } |
7b4ac7e1 |
991 | } |
e91b87a3 |
992 | else |
7b4ac7e1 |
993 | { |
e91b87a3 |
994 | /* Can't use a binary search */ |
995 | for (psym = start; psym < start + length; psym++) |
996 | if (namespace == SYMBOL_NAMESPACE (psym) |
997 | && !strcmp (name, SYMBOL_NAME (psym))) |
998 | return psym; |
7b4ac7e1 |
999 | } |
e91b87a3 |
1000 | |
1001 | return (struct partial_symbol *) 0; |
7b4ac7e1 |
1002 | } |
1003 | |
3bf57d21 |
1004 | /* Look for a symbol in block BLOCK. */ |
7b4ac7e1 |
1005 | |
1006 | static struct symbol * |
1007 | lookup_block_symbol (block, name, namespace) |
1008 | register struct block *block; |
1009 | char *name; |
1010 | enum namespace namespace; |
1011 | { |
1012 | register int bot, top, inc; |
e91b87a3 |
1013 | register struct symbol *sym, *parameter_sym; |
7b4ac7e1 |
1014 | |
1015 | top = BLOCK_NSYMS (block); |
1016 | bot = 0; |
1017 | |
3bf57d21 |
1018 | /* If the blocks's symbols were sorted, start with a binary search. */ |
7b4ac7e1 |
1019 | |
3bf57d21 |
1020 | if (BLOCK_SHOULD_SORT (block)) |
7b4ac7e1 |
1021 | { |
3bf57d21 |
1022 | /* First, advance BOT to not far before |
1023 | the first symbol whose name is NAME. */ |
1024 | |
1025 | while (1) |
1026 | { |
1027 | inc = (top - bot + 1); |
1028 | /* No need to keep binary searching for the last few bits worth. */ |
1029 | if (inc < 4) |
1030 | break; |
1031 | inc = (inc >> 1) + bot; |
1032 | sym = BLOCK_SYM (block, inc); |
1033 | if (SYMBOL_NAME (sym)[0] < name[0]) |
1034 | bot = inc; |
1035 | else if (SYMBOL_NAME (sym)[0] > name[0]) |
1036 | top = inc; |
1037 | else if (strcmp (SYMBOL_NAME (sym), name) < 0) |
1038 | bot = inc; |
1039 | else |
1040 | top = inc; |
1041 | } |
1042 | |
1043 | /* Now scan forward until we run out of symbols, |
1044 | find one whose name is greater than NAME, |
1045 | or find one we want. |
1046 | If there is more than one symbol with the right name and namespace, |
1047 | we return the first one. dbxread.c is careful to make sure |
1048 | that if one is a register then it comes first. */ |
1049 | |
1050 | top = BLOCK_NSYMS (block); |
1051 | while (bot < top) |
1052 | { |
1053 | sym = BLOCK_SYM (block, bot); |
1054 | inc = SYMBOL_NAME (sym)[0] - name[0]; |
1055 | if (inc == 0) |
1056 | inc = strcmp (SYMBOL_NAME (sym), name); |
1057 | if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace) |
1058 | return sym; |
1059 | if (inc > 0) |
1060 | return 0; |
1061 | bot++; |
1062 | } |
1063 | return 0; |
7b4ac7e1 |
1064 | } |
1065 | |
3bf57d21 |
1066 | /* Here if block isn't sorted. |
1067 | This loop is equivalent to the loop above, |
e91b87a3 |
1068 | but hacked greatly for speed. |
1069 | |
1070 | Note that parameter symbols do not always show up last in the |
1071 | list; this loop makes sure to take anything else other than |
1072 | parameter symbols first; it only uses parameter symbols as a |
1073 | last resort. Note that this only takes up extra computation |
1074 | time on a match. */ |
7b4ac7e1 |
1075 | |
e91b87a3 |
1076 | parameter_sym = (struct symbol *) 0; |
7b4ac7e1 |
1077 | top = BLOCK_NSYMS (block); |
3bf57d21 |
1078 | inc = name[0]; |
7b4ac7e1 |
1079 | while (bot < top) |
1080 | { |
1081 | sym = BLOCK_SYM (block, bot); |
3bf57d21 |
1082 | if (SYMBOL_NAME (sym)[0] == inc |
1083 | && !strcmp (SYMBOL_NAME (sym), name) |
1084 | && SYMBOL_NAMESPACE (sym) == namespace) |
e91b87a3 |
1085 | { |
4187119d |
1086 | if (SYMBOL_CLASS (sym) == LOC_ARG |
1087 | || SYMBOL_CLASS (sym) == LOC_REF_ARG |
1088 | || SYMBOL_CLASS (sym) == LOC_REGPARM) |
e91b87a3 |
1089 | parameter_sym = sym; |
1090 | else |
1091 | return sym; |
1092 | } |
7b4ac7e1 |
1093 | bot++; |
1094 | } |
e91b87a3 |
1095 | return parameter_sym; /* Will be 0 if not found. */ |
7b4ac7e1 |
1096 | } |
1097 | \f |
1098 | /* Return the symbol for the function which contains a specified |
1099 | lexical block, described by a struct block BL. */ |
1100 | |
1101 | struct symbol * |
1102 | block_function (bl) |
1103 | struct block *bl; |
1104 | { |
1105 | while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0) |
1106 | bl = BLOCK_SUPERBLOCK (bl); |
1107 | |
1108 | return BLOCK_FUNCTION (bl); |
1109 | } |
1110 | |
1111 | /* Subroutine of find_pc_line */ |
1112 | |
4187119d |
1113 | struct symtab * |
7b4ac7e1 |
1114 | find_pc_symtab (pc) |
1115 | register CORE_ADDR pc; |
1116 | { |
1117 | register struct block *b; |
1118 | struct blockvector *bv; |
1119 | register struct symtab *s; |
e91b87a3 |
1120 | register struct partial_symtab *ps; |
7b4ac7e1 |
1121 | |
1122 | /* Search all symtabs for one whose file contains our pc */ |
1123 | |
1124 | for (s = symtab_list; s; s = s->next) |
1125 | { |
1126 | bv = BLOCKVECTOR (s); |
1127 | b = BLOCKVECTOR_BLOCK (bv, 0); |
1128 | if (BLOCK_START (b) <= pc |
1129 | && BLOCK_END (b) > pc) |
1130 | break; |
1131 | } |
1132 | |
e91b87a3 |
1133 | if (!s) |
4187119d |
1134 | { |
1135 | ps = find_pc_psymtab (pc); |
1136 | if (ps && ps->readin) |
1137 | fatal ("Internal error: pc in read in psymtab, but not in symtab."); |
1138 | |
1139 | if (ps) |
1140 | s = psymtab_to_symtab (ps); |
1141 | } |
e91b87a3 |
1142 | |
7b4ac7e1 |
1143 | return s; |
1144 | } |
1145 | |
1146 | /* Find the source file and line number for a given PC value. |
1147 | Return a structure containing a symtab pointer, a line number, |
1148 | and a pc range for the entire source line. |
1149 | The value's .pc field is NOT the specified pc. |
1150 | NOTCURRENT nonzero means, if specified pc is on a line boundary, |
1151 | use the line that ends there. Otherwise, in that case, the line |
1152 | that begins there is used. */ |
1153 | |
1154 | struct symtab_and_line |
1155 | find_pc_line (pc, notcurrent) |
1156 | CORE_ADDR pc; |
1157 | int notcurrent; |
1158 | { |
1159 | struct symtab *s; |
1160 | register struct linetable *l; |
1161 | register int len; |
e91b87a3 |
1162 | register int i; |
1163 | register struct linetable_entry *item; |
7b4ac7e1 |
1164 | struct symtab_and_line value; |
1165 | struct blockvector *bv; |
1166 | |
1167 | /* Info on best line seen so far, and where it starts, and its file. */ |
1168 | |
1169 | int best_line = 0; |
1170 | CORE_ADDR best_pc = 0; |
1171 | CORE_ADDR best_end = 0; |
1172 | struct symtab *best_symtab = 0; |
1173 | |
1174 | /* Store here the first line number |
1175 | of a file which contains the line at the smallest pc after PC. |
1176 | If we don't find a line whose range contains PC, |
1177 | we will use a line one less than this, |
1178 | with a range from the start of that file to the first line's pc. */ |
1179 | int alt_line = 0; |
1180 | CORE_ADDR alt_pc = 0; |
1181 | struct symtab *alt_symtab = 0; |
1182 | |
1183 | /* Info on best line seen in this file. */ |
1184 | |
1185 | int prev_line; |
1186 | CORE_ADDR prev_pc; |
1187 | |
1188 | /* Info on first line of this file. */ |
1189 | |
1190 | int first_line; |
1191 | CORE_ADDR first_pc; |
1192 | |
1193 | /* If this pc is not from the current frame, |
1194 | it is the address of the end of a call instruction. |
1195 | Quite likely that is the start of the following statement. |
1196 | But what we want is the statement containing the instruction. |
1197 | Fudge the pc to make sure we get that. */ |
1198 | |
1199 | if (notcurrent) pc -= 1; |
1200 | |
1201 | s = find_pc_symtab (pc); |
1202 | if (s == 0) |
1203 | { |
1204 | value.symtab = 0; |
1205 | value.line = 0; |
1206 | value.pc = pc; |
e91b87a3 |
1207 | value.end = 0; |
7b4ac7e1 |
1208 | return value; |
1209 | } |
1210 | |
1211 | bv = BLOCKVECTOR (s); |
1212 | |
1213 | /* Look at all the symtabs that share this blockvector. |
1214 | They all have the same apriori range, that we found was right; |
1215 | but they have different line tables. */ |
1216 | |
1217 | for (; s && BLOCKVECTOR (s) == bv; s = s->next) |
1218 | { |
1219 | /* Find the best line in this symtab. */ |
1220 | l = LINETABLE (s); |
1221 | len = l->nitems; |
1222 | prev_line = -1; |
1223 | first_line = -1; |
1224 | for (i = 0; i < len; i++) |
1225 | { |
e91b87a3 |
1226 | item = &(l->item[i]); |
1227 | |
1228 | if (first_line < 0) |
7b4ac7e1 |
1229 | { |
e91b87a3 |
1230 | first_line = item->line; |
1231 | first_pc = item->pc; |
1232 | } |
1233 | /* Return the last line that did not start after PC. */ |
1234 | if (pc >= item->pc) |
1235 | { |
1236 | prev_line = item->line; |
1237 | prev_pc = item->pc; |
7b4ac7e1 |
1238 | } |
e91b87a3 |
1239 | else |
1240 | break; |
7b4ac7e1 |
1241 | } |
1242 | |
1243 | /* Is this file's best line closer than the best in the other files? |
1244 | If so, record this file, and its best line, as best so far. */ |
1245 | if (prev_line >= 0 && prev_pc > best_pc) |
1246 | { |
1247 | best_pc = prev_pc; |
1248 | best_line = prev_line; |
1249 | best_symtab = s; |
1250 | if (i < len) |
e91b87a3 |
1251 | best_end = item->pc; |
7b4ac7e1 |
1252 | else |
1253 | best_end = 0; |
1254 | } |
1255 | /* Is this file's first line closer than the first lines of other files? |
1256 | If so, record this file, and its first line, as best alternate. */ |
1257 | if (first_line >= 0 && first_pc > pc |
1258 | && (alt_pc == 0 || first_pc < alt_pc)) |
1259 | { |
1260 | alt_pc = first_pc; |
1261 | alt_line = first_line; |
1262 | alt_symtab = s; |
1263 | } |
1264 | } |
1265 | if (best_symtab == 0) |
1266 | { |
1267 | value.symtab = alt_symtab; |
1268 | value.line = alt_line - 1; |
1269 | value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0)); |
1270 | value.end = alt_pc; |
1271 | } |
1272 | else |
1273 | { |
1274 | value.symtab = best_symtab; |
1275 | value.line = best_line; |
1276 | value.pc = best_pc; |
1277 | value.end = (best_end ? best_end |
1278 | : (alt_pc ? alt_pc |
1279 | : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0)))); |
1280 | } |
1281 | return value; |
1282 | } |
632ea0cc |
1283 | \f |
1284 | /* Find the PC value for a given source file and line number. |
1285 | Returns zero for invalid line number. |
1286 | The source file is specified with a struct symtab. */ |
1287 | |
1288 | CORE_ADDR |
1289 | find_line_pc (symtab, line) |
1290 | struct symtab *symtab; |
1291 | int line; |
1292 | { |
1293 | register struct linetable *l; |
1294 | register int index; |
1295 | int dummy; |
1296 | |
1297 | if (symtab == 0) |
1298 | return 0; |
1299 | l = LINETABLE (symtab); |
1300 | index = find_line_common(l, line, &dummy); |
e91b87a3 |
1301 | return index ? l->item[index].pc : 0; |
632ea0cc |
1302 | } |
7b4ac7e1 |
1303 | |
1304 | /* Find the range of pc values in a line. |
1305 | Store the starting pc of the line into *STARTPTR |
1306 | and the ending pc (start of next line) into *ENDPTR. |
1307 | Returns 1 to indicate success. |
1308 | Returns 0 if could not find the specified line. */ |
1309 | |
1310 | int |
1311 | find_line_pc_range (symtab, thisline, startptr, endptr) |
1312 | struct symtab *symtab; |
1313 | int thisline; |
1314 | CORE_ADDR *startptr, *endptr; |
1315 | { |
1316 | register struct linetable *l; |
632ea0cc |
1317 | register int index; |
1318 | int exact_match; /* did we get an exact linenumber match */ |
7b4ac7e1 |
1319 | register CORE_ADDR prev_pc; |
1320 | CORE_ADDR last_pc; |
1321 | |
1322 | if (symtab == 0) |
1323 | return 0; |
1324 | |
1325 | l = LINETABLE (symtab); |
632ea0cc |
1326 | index = find_line_common (l, thisline, &exact_match); |
1327 | if (index) |
7b4ac7e1 |
1328 | { |
e91b87a3 |
1329 | *startptr = l->item[index].pc; |
632ea0cc |
1330 | /* If we have not seen an entry for the specified line, |
1331 | assume that means the specified line has zero bytes. */ |
1332 | if (!exact_match || index == l->nitems-1) |
1333 | *endptr = *startptr; |
7b4ac7e1 |
1334 | else |
632ea0cc |
1335 | /* Perhaps the following entry is for the following line. |
1336 | It's worth a try. */ |
e91b87a3 |
1337 | if (l->item[index+1].line == thisline + 1) |
1338 | *endptr = l->item[index+1].pc; |
632ea0cc |
1339 | else |
1340 | *endptr = find_line_pc (symtab, thisline+1); |
7b4ac7e1 |
1341 | return 1; |
1342 | } |
1343 | |
1344 | return 0; |
1345 | } |
1346 | |
632ea0cc |
1347 | /* Given a line table and a line number, return the index into the line |
1348 | table for the pc of the nearest line whose number is >= the specified one. |
1349 | Return 0 if none is found. The value is never zero is it is an index. |
7b4ac7e1 |
1350 | |
632ea0cc |
1351 | Set *EXACT_MATCH nonzero if the value returned is an exact match. */ |
1352 | |
1353 | static int |
1354 | find_line_common (l, lineno, exact_match) |
1355 | register struct linetable *l; |
1356 | register int lineno; |
1357 | int *exact_match; |
7b4ac7e1 |
1358 | { |
7b4ac7e1 |
1359 | register int i; |
632ea0cc |
1360 | register int len; |
1361 | |
1362 | /* BEST is the smallest linenumber > LINENO so far seen, |
1363 | or 0 if none has been seen so far. |
1364 | BEST_INDEX identifies the item for it. */ |
7b4ac7e1 |
1365 | |
632ea0cc |
1366 | int best_index = 0; |
1367 | int best = 0; |
1368 | |
1369 | int nextline = -1; |
1370 | |
1371 | if (lineno <= 0) |
7b4ac7e1 |
1372 | return 0; |
1373 | |
7b4ac7e1 |
1374 | len = l->nitems; |
1375 | for (i = 0; i < len; i++) |
1376 | { |
e91b87a3 |
1377 | register struct linetable_entry *item = &(l->item[i]); |
632ea0cc |
1378 | |
e91b87a3 |
1379 | if (item->line == lineno) |
7b4ac7e1 |
1380 | { |
e91b87a3 |
1381 | *exact_match = 1; |
1382 | return i; |
1383 | } |
632ea0cc |
1384 | |
e91b87a3 |
1385 | if (item->line > lineno && (best == 0 || item->line < best)) |
1386 | { |
1387 | best = item->line; |
1388 | best_index = i; |
7b4ac7e1 |
1389 | } |
1390 | } |
632ea0cc |
1391 | |
1392 | /* If we got here, we didn't get an exact match. */ |
1393 | |
1394 | *exact_match = 0; |
1395 | return best_index; |
7b4ac7e1 |
1396 | } |
1397 | |
1398 | int |
1399 | find_pc_line_pc_range (pc, startptr, endptr) |
1400 | CORE_ADDR pc; |
1401 | CORE_ADDR *startptr, *endptr; |
1402 | { |
1403 | struct symtab_and_line sal; |
1404 | sal = find_pc_line (pc, 0); |
1405 | *startptr = sal.pc; |
1406 | *endptr = sal.end; |
1407 | return sal.symtab != 0; |
1408 | } |
1409 | \f |
1410 | /* Parse a string that specifies a line number. |
1411 | Pass the address of a char * variable; that variable will be |
1412 | advanced over the characters actually parsed. |
1413 | |
1414 | The string can be: |
1415 | |
1416 | LINENUM -- that line number in current file. PC returned is 0. |
1417 | FILE:LINENUM -- that line in that file. PC returned is 0. |
1418 | FUNCTION -- line number of openbrace of that function. |
1419 | PC returned is the start of the function. |
1420 | FILE:FUNCTION -- likewise, but prefer functions in that file. |
1421 | *EXPR -- line in which address EXPR appears. |
1422 | |
1423 | FUNCTION may be an undebuggable function found in misc_function_vector. |
1424 | |
1425 | If the argument FUNFIRSTLINE is nonzero, we want the first line |
1426 | of real code inside a function when a function is specified. |
1427 | |
1428 | DEFAULT_SYMTAB specifies the file to use if none is specified. |
1429 | It defaults to current_source_symtab. |
1430 | DEFAULT_LINE specifies the line number to use for relative |
1431 | line numbers (that start with signs). Defaults to current_source_line. |
1432 | |
1433 | Note that it is possible to return zero for the symtab |
1434 | if no file is validly specified. Callers must check that. |
1435 | Also, the line number returned may be invalid. */ |
1436 | |
bb7592f0 |
1437 | struct symtabs_and_lines |
7b4ac7e1 |
1438 | decode_line_1 (argptr, funfirstline, default_symtab, default_line) |
1439 | char **argptr; |
1440 | int funfirstline; |
1441 | struct symtab *default_symtab; |
1442 | int default_line; |
1443 | { |
bb7592f0 |
1444 | struct symtabs_and_lines decode_line_2 (); |
1445 | struct symtabs_and_lines values; |
7b4ac7e1 |
1446 | struct symtab_and_line value; |
1447 | register char *p, *p1; |
1448 | register struct symtab *s; |
1449 | register struct symbol *sym; |
1450 | register CORE_ADDR pc; |
1451 | register int i; |
1452 | char *copy; |
bb7592f0 |
1453 | struct symbol *sym_class; |
1454 | char *class_name, *method_name, *phys_name; |
1455 | int method_counter; |
1456 | int i1; |
1457 | struct symbol **sym_arr; |
1458 | struct type *t, *field; |
1459 | char **physnames; |
e91b87a3 |
1460 | |
7b4ac7e1 |
1461 | /* Defaults have defaults. */ |
1462 | |
1463 | if (default_symtab == 0) |
1464 | { |
1465 | default_symtab = current_source_symtab; |
1466 | default_line = current_source_line; |
1467 | } |
1468 | |
1469 | /* See if arg is *PC */ |
1470 | |
1471 | if (**argptr == '*') |
1472 | { |
1473 | (*argptr)++; |
1474 | pc = parse_and_eval_address_1 (argptr); |
4187119d |
1475 | values.sals = (struct symtab_and_line *) |
1476 | malloc (sizeof (struct symtab_and_line)); |
bb7592f0 |
1477 | values.nelts = 1; |
1478 | values.sals[0] = find_pc_line (pc, 0); |
1479 | values.sals[0].pc = pc; |
1480 | return values; |
7b4ac7e1 |
1481 | } |
1482 | |
1483 | /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */ |
1484 | |
1485 | s = 0; |
1486 | |
1487 | for (p = *argptr; *p; p++) |
1488 | { |
1489 | if (p[0] == ':' || p[0] == ' ' || p[0] == '\t') |
1490 | break; |
1491 | } |
1492 | while (p[0] == ' ' || p[0] == '\t') p++; |
1493 | |
1494 | if (p[0] == ':') |
1495 | { |
e91b87a3 |
1496 | |
bb7592f0 |
1497 | /* C++ */ |
1498 | if (p[1] ==':') |
1499 | { |
1500 | /* Extract the class name. */ |
1501 | p1 = p; |
1502 | while (p != *argptr && p[-1] == ' ') --p; |
1503 | copy = (char *) alloca (p - *argptr + 1); |
1504 | bcopy (*argptr, copy, p - *argptr); |
1505 | copy[p - *argptr] = 0; |
1506 | |
1507 | /* Discard the class name from the arg. */ |
1508 | p = p1 + 2; |
1509 | while (*p == ' ' || *p == '\t') p++; |
1510 | *argptr = p; |
1511 | |
e91b87a3 |
1512 | sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0); |
bb7592f0 |
1513 | |
1514 | if (sym_class && |
1515 | (TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT |
1516 | || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION)) |
1517 | { |
1518 | /* Arg token is not digits => try it as a function name |
1519 | Find the next token (everything up to end or next whitespace). */ |
1520 | p = *argptr; |
1521 | while (*p && *p != ' ' && *p != '\t' && *p != ',' && *p !=':') p++; |
1522 | copy = (char *) alloca (p - *argptr + 1); |
1523 | bcopy (*argptr, copy, p - *argptr); |
1524 | copy[p - *argptr] = '\0'; |
1525 | |
1526 | /* no line number may be specified */ |
1527 | while (*p == ' ' || *p == '\t') p++; |
1528 | *argptr = p; |
1529 | |
1530 | sym = 0; |
1531 | i1 = 0; /* counter for the symbol array */ |
1532 | t = SYMBOL_TYPE (sym_class); |
1533 | sym_arr = (struct symbol **) alloca(TYPE_NFN_FIELDS_TOTAL (t) * sizeof(struct symbol*)); |
1534 | physnames = (char **) alloca (TYPE_NFN_FIELDS_TOTAL (t) * sizeof(char*)); |
1535 | |
1536 | if (destructor_name_p (copy, t)) |
1537 | { |
1538 | /* destructors are a special case. */ |
1539 | struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0); |
1540 | int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1; |
1541 | phys_name = TYPE_FN_FIELD_PHYSNAME (f, len); |
1542 | physnames[i1] = (char *)alloca (strlen (phys_name) + 1); |
1543 | strcpy (physnames[i1], phys_name); |
e91b87a3 |
1544 | sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE, 0); |
bb7592f0 |
1545 | if (sym_arr[i1]) i1++; |
1546 | } |
1547 | else while (t) |
1548 | { |
1549 | class_name = TYPE_NAME (t); |
4187119d |
1550 | /* Ignore this class if it doesn't have a name. |
1551 | This prevents core dumps, but is just a workaround |
1552 | because we might not find the function in |
1553 | certain cases, such as |
1554 | struct D {virtual int f();} |
1555 | struct C : D {virtual int g();} |
1556 | (in this case g++ 1.35.1- does not put out a name |
1557 | for D as such, it defines type 19 (for example) in |
1558 | the same stab as C, and then does a |
1559 | .stabs "D:T19" and a .stabs "D:t19". |
1560 | Thus |
1561 | "break C::f" should not be looking for field f in |
1562 | the class named D, |
1563 | but just for the field f in the baseclasses of C |
1564 | (no matter what their names). |
1565 | |
1566 | However, I don't know how to replace the code below |
1567 | that depends on knowing the name of D. */ |
1568 | if (class_name) |
bb7592f0 |
1569 | { |
1c997a4a |
1570 | /* We just want the class name. In the context |
1571 | of C++, stripping off "struct " is always |
1572 | sensible. */ |
1573 | if (strncmp("struct ", class_name, 7) == 0) |
1574 | class_name += 7; |
1575 | if (strncmp("union ", class_name, 6) == 0) |
1576 | class_name += 6; |
4187119d |
1577 | |
1578 | sym_class = lookup_symbol (class_name, 0, STRUCT_NAMESPACE, 0); |
1579 | for (method_counter = TYPE_NFN_FIELDS (SYMBOL_TYPE (sym_class)) - 1; |
1580 | method_counter >= 0; |
1581 | --method_counter) |
1582 | { |
1583 | int field_counter; |
1584 | struct fn_field *f = |
1585 | TYPE_FN_FIELDLIST1 (SYMBOL_TYPE (sym_class), method_counter); |
1586 | |
1587 | method_name = TYPE_FN_FIELDLIST_NAME (SYMBOL_TYPE (sym_class), method_counter); |
1588 | if (!strcmp (copy, method_name)) |
1589 | /* Find all the fields with that name. */ |
1590 | for (field_counter = TYPE_FN_FIELDLIST_LENGTH (SYMBOL_TYPE (sym_class), method_counter) - 1; |
1591 | field_counter >= 0; |
1592 | --field_counter) |
1593 | { |
1594 | phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter); |
1595 | physnames[i1] = (char*) alloca (strlen (phys_name) + 1); |
1596 | strcpy (physnames[i1], phys_name); |
1597 | sym_arr[i1] = lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class), VAR_NAMESPACE, 0); |
1598 | if (sym_arr[i1]) i1++; |
1599 | } |
1600 | } |
bb7592f0 |
1601 | } |
1602 | if (TYPE_N_BASECLASSES (t)) |
1603 | t = TYPE_BASECLASS(t, 1); |
e91b87a3 |
1604 | else |
1605 | break; |
bb7592f0 |
1606 | } |
1607 | |
1608 | if (i1 == 1) |
1609 | { |
4187119d |
1610 | /* There is exactly one field with that name. */ |
bb7592f0 |
1611 | sym = sym_arr[0]; |
1612 | |
1613 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) |
1614 | { |
1615 | /* Arg is the name of a function */ |
1616 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET; |
1617 | if (funfirstline) |
1618 | SKIP_PROLOGUE (pc); |
1619 | values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line)); |
1620 | values.nelts = 1; |
1621 | values.sals[0] = find_pc_line (pc, 0); |
1622 | values.sals[0].pc = (values.sals[0].end && values.sals[0].pc != pc) ? values.sals[0].end : pc; |
1623 | } |
1624 | else |
1625 | { |
1626 | values.nelts = 0; |
1627 | } |
1628 | return values; |
1629 | } |
1630 | if (i1 > 0) |
1631 | { |
4187119d |
1632 | /* There is more than one field with that name |
1633 | (overloaded). Ask the user which one to use. */ |
e91b87a3 |
1634 | return decode_line_2 (argptr, sym_arr, physnames, |
1635 | i1, funfirstline); |
bb7592f0 |
1636 | } |
1637 | else |
1638 | error ("that class does not have any method named %s",copy); |
1639 | } |
1640 | else |
1641 | error("no class, struct, or union named %s", copy ); |
1642 | } |
1643 | /* end of C++ */ |
1644 | |
e91b87a3 |
1645 | |
7b4ac7e1 |
1646 | /* Extract the file name. */ |
1647 | p1 = p; |
1648 | while (p != *argptr && p[-1] == ' ') --p; |
1649 | copy = (char *) alloca (p - *argptr + 1); |
1650 | bcopy (*argptr, copy, p - *argptr); |
1651 | copy[p - *argptr] = 0; |
1652 | |
1653 | /* Find that file's data. */ |
1654 | s = lookup_symtab (copy); |
1655 | if (s == 0) |
1656 | { |
e91b87a3 |
1657 | if (symtab_list == 0 && partial_symtab_list == 0) |
7b4ac7e1 |
1658 | error ("No symbol table is loaded. Use the \"symbol-file\" command."); |
1659 | error ("No source file named %s.", copy); |
1660 | } |
1661 | |
1662 | /* Discard the file name from the arg. */ |
1663 | p = p1 + 1; |
1664 | while (*p == ' ' || *p == '\t') p++; |
1665 | *argptr = p; |
1666 | } |
1667 | |
1668 | /* S is specified file's symtab, or 0 if no file specified. |
1669 | arg no longer contains the file name. */ |
1670 | |
1671 | /* Check whether arg is all digits (and sign) */ |
1672 | |
1673 | p = *argptr; |
1674 | if (*p == '-' || *p == '+') p++; |
1675 | while (*p >= '0' && *p <= '9') |
1676 | p++; |
1677 | |
1678 | if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ',')) |
1679 | { |
1680 | /* We found a token consisting of all digits -- at least one digit. */ |
1681 | enum sign {none, plus, minus} sign = none; |
1682 | |
e91b87a3 |
1683 | /* This is where we need to make sure that we have good defaults. |
1684 | We must guarrantee that this section of code is never executed |
1685 | when we are called with just a function name, since |
1686 | select_source_symtab calls us with such an argument */ |
1687 | |
1688 | if (s == 0 && default_symtab == 0) |
1689 | { |
1690 | if (symtab_list == 0 && partial_symtab_list == 0) |
1691 | error ("No symbol table is loaded. Use the \"symbol-file\" command."); |
4187119d |
1692 | select_source_symtab (0); |
e91b87a3 |
1693 | default_symtab = current_source_symtab; |
1694 | default_line = current_source_line; |
1695 | } |
1696 | |
7b4ac7e1 |
1697 | if (**argptr == '+') |
1698 | sign = plus, (*argptr)++; |
1699 | else if (**argptr == '-') |
1700 | sign = minus, (*argptr)++; |
1701 | value.line = atoi (*argptr); |
1702 | switch (sign) |
1703 | { |
1704 | case plus: |
1705 | if (p == *argptr) |
1706 | value.line = 5; |
1707 | if (s == 0) |
1708 | value.line = default_line + value.line; |
1709 | break; |
1710 | case minus: |
1711 | if (p == *argptr) |
1712 | value.line = 15; |
1713 | if (s == 0) |
1714 | value.line = default_line - value.line; |
1715 | else |
1716 | value.line = 1; |
1717 | break; |
1718 | } |
1719 | |
1720 | while (*p == ' ' || *p == '\t') p++; |
1721 | *argptr = p; |
1722 | if (s == 0) |
1723 | s = default_symtab; |
1724 | value.symtab = s; |
1725 | value.pc = 0; |
bb7592f0 |
1726 | values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line)); |
1727 | values.sals[0] = value; |
1728 | values.nelts = 1; |
1729 | return values; |
7b4ac7e1 |
1730 | } |
1731 | |
1732 | /* Arg token is not digits => try it as a function name |
1733 | Find the next token (everything up to end or next whitespace). */ |
1734 | p = *argptr; |
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++; |
1740 | *argptr = p; |
1741 | |
1742 | /* Look up that token as a function. |
1743 | If file specified, use that file's per-file block to start with. */ |
1744 | |
1745 | sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0, |
e91b87a3 |
1746 | VAR_NAMESPACE, 0); |
7b4ac7e1 |
1747 | |
1748 | if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK) |
1749 | { |
1750 | /* Arg is the name of a function */ |
1751 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET; |
1752 | if (funfirstline) |
1753 | SKIP_PROLOGUE (pc); |
1754 | value = find_pc_line (pc, 0); |
4187119d |
1755 | #ifdef PROLOGUE_FIRSTLINE_OVERLAP |
e91b87a3 |
1756 | /* Convex: no need to suppress code on first line, if any */ |
1757 | value.pc = pc; |
1758 | #else |
7b4ac7e1 |
1759 | value.pc = (value.end && value.pc != pc) ? value.end : pc; |
e91b87a3 |
1760 | #endif |
bb7592f0 |
1761 | values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line)); |
1762 | values.sals[0] = value; |
1763 | values.nelts = 1; |
1764 | return values; |
7b4ac7e1 |
1765 | } |
1766 | |
1767 | if (sym) |
1768 | error ("%s is not a function.", copy); |
1769 | |
632ea0cc |
1770 | if ((i = lookup_misc_func (copy)) < 0) |
1771 | error ("Function %s not defined.", copy); |
1772 | else |
1773 | { |
1774 | value.symtab = 0; |
1775 | value.line = 0; |
1776 | value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET; |
1777 | if (funfirstline) |
1778 | SKIP_PROLOGUE (value.pc); |
bb7592f0 |
1779 | values.sals = (struct symtab_and_line *)malloc (sizeof (struct symtab_and_line)); |
1780 | values.sals[0] = value; |
1781 | values.nelts = 1; |
1782 | return values; |
632ea0cc |
1783 | } |
7b4ac7e1 |
1784 | |
e91b87a3 |
1785 | if (symtab_list == 0 && partial_symtab_list == 0) |
7b4ac7e1 |
1786 | error ("No symbol table is loaded. Use the \"symbol-file\" command."); |
1787 | error ("Function %s not defined.", copy); |
1788 | } |
1789 | |
bb7592f0 |
1790 | struct symtabs_and_lines |
7b4ac7e1 |
1791 | decode_line_spec (string, funfirstline) |
1792 | char *string; |
1793 | int funfirstline; |
1794 | { |
bb7592f0 |
1795 | struct symtabs_and_lines sals; |
7b4ac7e1 |
1796 | if (string == 0) |
1797 | error ("Empty line specification."); |
bb7592f0 |
1798 | sals = decode_line_1 (&string, funfirstline, |
1799 | current_source_symtab, current_source_line); |
7b4ac7e1 |
1800 | if (*string) |
1801 | error ("Junk at end of line specification: %s", string); |
bb7592f0 |
1802 | return sals; |
1803 | } |
1804 | |
7a67dd45 |
1805 | /* Given a list of NELTS symbols in sym_arr (with corresponding |
1806 | mangled names in physnames), return a list of lines to operate on |
1807 | (ask user if necessary). */ |
bb7592f0 |
1808 | struct symtabs_and_lines |
1809 | decode_line_2 (argptr, sym_arr, physnames, nelts, funfirstline) |
1810 | char **argptr; |
1811 | struct symbol *sym_arr[]; |
1812 | char *physnames[]; |
1813 | int nelts; |
1814 | int funfirstline; |
1815 | { |
1816 | char *getenv(); |
1817 | struct symtabs_and_lines values, return_values; |
1818 | register CORE_ADDR pc; |
4187119d |
1819 | char *args, *arg1, *command_line_input (); |
bb7592f0 |
1820 | int i; |
1821 | char *prompt; |
1822 | |
1823 | values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line)); |
1824 | return_values.sals = (struct symtab_and_line *) malloc (nelts * sizeof(struct symtab_and_line)); |
1825 | |
1826 | i = 0; |
1827 | printf("[0] cancel\n[1] all\n"); |
1828 | while (i < nelts) |
1829 | { |
1830 | if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK) |
1831 | { |
1832 | /* Arg is the name of a function */ |
1833 | pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym_arr[i])) |
1834 | + FUNCTION_START_OFFSET; |
1835 | if (funfirstline) |
1836 | SKIP_PROLOGUE (pc); |
1837 | values.sals[i] = find_pc_line (pc, 0); |
1838 | printf("[%d] file:%s; line number:%d\n", |
1839 | (i+2), values.sals[i].symtab->filename, values.sals[i].line); |
1840 | } |
1841 | else printf ("?HERE\n"); |
1842 | i++; |
1843 | } |
1844 | |
1845 | if ((prompt = getenv ("PS2")) == NULL) |
1846 | { |
1847 | prompt = ">"; |
1848 | } |
1849 | printf("%s ",prompt); |
1850 | fflush(stdout); |
1851 | |
4187119d |
1852 | args = command_line_input (0, 0); |
bb7592f0 |
1853 | |
1854 | if (args == 0) |
1855 | error_no_arg ("one or more choice numbers"); |
1856 | |
1857 | i = 0; |
1858 | while (*args) |
1859 | { |
1860 | int num; |
1861 | |
1862 | arg1 = args; |
1863 | while (*arg1 >= '0' && *arg1 <= '9') arg1++; |
1864 | if (*arg1 && *arg1 != ' ' && *arg1 != '\t') |
1865 | error ("Arguments must be choice numbers."); |
1866 | |
1867 | num = atoi (args); |
1868 | |
1869 | if (num == 0) |
1870 | error ("cancelled"); |
1871 | else if (num == 1) |
1872 | { |
1873 | bcopy (values.sals, return_values.sals, (nelts * sizeof(struct symtab_and_line))); |
1874 | return_values.nelts = nelts; |
1875 | return return_values; |
1876 | } |
1877 | |
1878 | if (num > nelts + 2) |
1879 | { |
1880 | printf ("No choice number %d.\n", num); |
1881 | } |
1882 | else |
1883 | { |
1884 | num -= 2; |
1885 | if (values.sals[num].pc) |
1886 | { |
1887 | return_values.sals[i++] = values.sals[num]; |
1888 | values.sals[num].pc = 0; |
1889 | } |
1890 | else |
1891 | { |
1892 | printf ("duplicate request for %d ignored.\n", num); |
1893 | } |
1894 | } |
1895 | |
1896 | args = arg1; |
1897 | while (*args == ' ' || *args == '\t') args++; |
1898 | } |
1899 | return_values.nelts = i; |
1900 | return return_values; |
632ea0cc |
1901 | } |
1902 | |
1903 | /* Return the index of misc function named NAME. */ |
1904 | |
e91b87a3 |
1905 | static int |
632ea0cc |
1906 | lookup_misc_func (name) |
1907 | register char *name; |
1908 | { |
1909 | register int i; |
1910 | |
1911 | for (i = 0; i < misc_function_count; i++) |
1912 | if (!strcmp (misc_function_vector[i].name, name)) |
1913 | return i; |
1914 | return -1; /* not found */ |
7b4ac7e1 |
1915 | } |
1916 | \f |
e91b87a3 |
1917 | /* |
4187119d |
1918 | * Slave routine for sources_info. Force line breaks at ,'s. |
e91b87a3 |
1919 | */ |
e91b87a3 |
1920 | static void |
1921 | output_source_filename (name, next) |
1922 | char *name; |
1923 | int next; |
1924 | { |
1925 | static int column = 0; |
1926 | |
1927 | if (column != 0 && column + strlen (name) >= 70) |
1928 | { |
4187119d |
1929 | printf_filtered ("\n"); |
e91b87a3 |
1930 | column = 0; |
e91b87a3 |
1931 | } |
1932 | else if (column != 0) |
1933 | { |
4187119d |
1934 | printf_filtered (" "); |
e91b87a3 |
1935 | column++; |
1936 | } |
4187119d |
1937 | printf_filtered ("%s", name); |
e91b87a3 |
1938 | column += strlen (name); |
1939 | if (next) |
1940 | { |
4187119d |
1941 | printf_filtered (","); |
e91b87a3 |
1942 | column++; |
1943 | } |
1944 | |
1945 | if (!next) column = 0; |
1946 | } |
1947 | |
7b4ac7e1 |
1948 | static void |
1949 | sources_info () |
1950 | { |
1951 | register struct symtab *s; |
e91b87a3 |
1952 | register struct partial_symtab *ps; |
7b4ac7e1 |
1953 | register int column = 0; |
1954 | |
e91b87a3 |
1955 | if (symtab_list == 0 && partial_symtab_list == 0) |
7b4ac7e1 |
1956 | { |
1957 | printf ("No symbol table is loaded.\n"); |
1958 | return; |
1959 | } |
e91b87a3 |
1960 | |
4187119d |
1961 | printf_filtered ("Source files for which symbols have been read in:\n\n"); |
1962 | |
7b4ac7e1 |
1963 | for (s = symtab_list; s; s = s->next) |
e91b87a3 |
1964 | output_source_filename (s->filename, s->next); |
4187119d |
1965 | printf_filtered ("\n\n"); |
e91b87a3 |
1966 | |
4187119d |
1967 | printf_filtered ("Source files for which symbols will be read in on demand:\n\n"); |
1968 | |
e91b87a3 |
1969 | for (ps = partial_symtab_list; ps; ps = ps->next) |
4187119d |
1970 | if (!ps->readin) |
1971 | output_source_filename (ps->filename, ps->next); |
1972 | printf_filtered ("\n"); |
7b4ac7e1 |
1973 | } |
1974 | |
1975 | /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP. |
1976 | If CLASS is zero, list all symbols except functions and type names. |
1977 | If CLASS is 1, list only functions. |
1978 | If CLASS is 2, list only type names. */ |
1979 | |
3bf57d21 |
1980 | static void sort_block_syms (); |
1981 | |
7b4ac7e1 |
1982 | static void |
1983 | list_symbols (regexp, class) |
1984 | char *regexp; |
1985 | int class; |
1986 | { |
1987 | register struct symtab *s; |
e91b87a3 |
1988 | register struct partial_symtab *ps; |
7b4ac7e1 |
1989 | register struct blockvector *bv; |
1990 | struct blockvector *prev_bv = 0; |
1991 | register struct block *b; |
1992 | register int i, j; |
1993 | register struct symbol *sym; |
e91b87a3 |
1994 | struct partial_symbol *psym; |
632ea0cc |
1995 | char *val; |
7b4ac7e1 |
1996 | static char *classnames[] |
bb7592f0 |
1997 | = {"variable", "function", "type", "method"}; |
7b4ac7e1 |
1998 | int print_count = 0; |
e91b87a3 |
1999 | int found_in_file = 0; |
632ea0cc |
2000 | |
2001 | if (regexp) |
7b4ac7e1 |
2002 | if (val = (char *) re_comp (regexp)) |
2003 | error ("Invalid regexp: %s", val); |
7b4ac7e1 |
2004 | |
e91b87a3 |
2005 | /* Search through the partial_symtab_list *first* for all symbols |
2006 | matching the regexp. That way we don't have to reproduce all of |
2007 | the machinery below. */ |
2008 | for (ps = partial_symtab_list; ps; ps = ps->next) |
2009 | { |
2010 | struct partial_symbol *bound, *gbound, *sbound; |
2011 | int keep_going = 1; |
2012 | |
4187119d |
2013 | if (ps->readin) continue; |
2014 | |
2015 | gbound = global_psymbols.list + ps->globals_offset + ps->n_global_syms; |
2016 | sbound = static_psymbols.list + ps->statics_offset + ps->n_static_syms; |
e91b87a3 |
2017 | bound = gbound; |
2018 | |
2019 | /* Go through all of the symbols stored in a partial |
2020 | symtab in one loop. */ |
4187119d |
2021 | psym = global_psymbols.list + ps->globals_offset; |
e91b87a3 |
2022 | while (keep_going) |
2023 | { |
2024 | if (psym >= bound) |
2025 | { |
2026 | if (bound == gbound && ps->n_static_syms != 0) |
2027 | { |
4187119d |
2028 | psym = static_psymbols.list + ps->statics_offset; |
e91b87a3 |
2029 | bound = sbound; |
2030 | } |
2031 | else |
2032 | keep_going = 0; |
2033 | } |
2034 | else |
2035 | { |
2036 | QUIT; |
2037 | |
2038 | /* If it would match (logic taken from loop below) |
2039 | load the file and go on to the next one */ |
2040 | if ((regexp == 0 || re_exec (SYMBOL_NAME (psym))) |
2041 | && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF |
2042 | && SYMBOL_CLASS (psym) != LOC_BLOCK) |
2043 | || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK) |
2044 | || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF) |
2045 | || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK))) |
2046 | { |
2047 | psymtab_to_symtab(ps); |
2048 | keep_going = 0; |
2049 | } |
2050 | } |
2051 | psym++; |
2052 | } |
2053 | } |
2054 | |
4187119d |
2055 | /* Printout here so as to get after the "Reading in symbols" |
2056 | messages which will be generated above. */ |
2057 | printf_filtered (regexp |
2058 | ? "All %ss matching regular expression \"%s\":\n" |
2059 | : "All defined %ss:\n", |
2060 | classnames[class], |
2061 | regexp); |
2062 | |
2063 | /* Here, *if* the class is correct (function only, right now), we |
2064 | should search through the misc function vector for symbols that |
2065 | match and call find_pc_psymtab on them. If find_pc_psymtab returns |
2066 | 0, don't worry about it (already read in or no debugging info). */ |
2067 | |
2068 | if (class == 1) |
2069 | { |
2070 | for (i = 0; i < misc_function_count; i++) |
2071 | if (regexp == 0 || re_exec (misc_function_vector[i].name)) |
2072 | { |
2073 | ps = find_pc_psymtab (misc_function_vector[i].address); |
2074 | if (ps && !ps->readin) |
2075 | psymtab_to_symtab (ps); |
2076 | } |
2077 | } |
2078 | |
7b4ac7e1 |
2079 | for (s = symtab_list; s; s = s->next) |
2080 | { |
2081 | found_in_file = 0; |
2082 | bv = BLOCKVECTOR (s); |
2083 | /* Often many files share a blockvector. |
2084 | Scan each blockvector only once so that |
2085 | we don't get every symbol many times. |
2086 | It happens that the first symtab in the list |
2087 | for any given blockvector is the main file. */ |
2088 | if (bv != prev_bv) |
2089 | for (i = 0; i < 2; i++) |
2090 | { |
2091 | b = BLOCKVECTOR_BLOCK (bv, i); |
3bf57d21 |
2092 | /* Skip the sort if this block is always sorted. */ |
2093 | if (!BLOCK_SHOULD_SORT (b)) |
2094 | sort_block_syms (b); |
7b4ac7e1 |
2095 | for (j = 0; j < BLOCK_NSYMS (b); j++) |
2096 | { |
2097 | QUIT; |
2098 | sym = BLOCK_SYM (b, j); |
632ea0cc |
2099 | if ((regexp == 0 || re_exec (SYMBOL_NAME (sym))) |
2100 | && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF |
7b4ac7e1 |
2101 | && SYMBOL_CLASS (sym) != LOC_BLOCK) |
2102 | || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK) |
bb7592f0 |
2103 | || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
2104 | || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK))) |
7b4ac7e1 |
2105 | { |
2106 | if (!found_in_file) |
2107 | { |
4187119d |
2108 | printf_filtered ("\nFile %s:\n", s->filename); |
7b4ac7e1 |
2109 | print_count += 2; |
2110 | } |
2111 | found_in_file = 1; |
7b4ac7e1 |
2112 | if (class != 2 && i == 1) |
4187119d |
2113 | printf_filtered ("static "); |
7b4ac7e1 |
2114 | if (class == 2 |
2115 | && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE) |
4187119d |
2116 | printf_filtered ("typedef "); |
7b4ac7e1 |
2117 | |
bb7592f0 |
2118 | if (class < 3) |
2119 | { |
2120 | type_print (SYMBOL_TYPE (sym), |
2121 | (SYMBOL_CLASS (sym) == LOC_TYPEDEF |
2122 | ? "" : SYMBOL_NAME (sym)), |
2123 | stdout, 0); |
4187119d |
2124 | |
2125 | if (class == 2 |
2126 | && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE |
2127 | && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0 |
2128 | || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))), |
2129 | SYMBOL_NAME (sym)))) |
2130 | printf_filtered (" %s", SYMBOL_NAME (sym)); |
2131 | |
2132 | printf_filtered (";\n"); |
bb7592f0 |
2133 | } |
2134 | else |
2135 | { |
bb7592f0 |
2136 | # if 0 |
7a67dd45 |
2137 | char buf[1024]; |
bb7592f0 |
2138 | type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0); |
2139 | type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0); |
2140 | sprintf (buf, " %s::", TYPE_NAME (t)); |
2141 | type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout); |
2142 | # endif |
2143 | } |
7b4ac7e1 |
2144 | } |
2145 | } |
2146 | } |
2147 | prev_bv = bv; |
2148 | } |
7b4ac7e1 |
2149 | } |
2150 | |
2151 | static void |
2152 | variables_info (regexp) |
2153 | char *regexp; |
2154 | { |
2155 | list_symbols (regexp, 0); |
2156 | } |
2157 | |
2158 | static void |
2159 | functions_info (regexp) |
2160 | char *regexp; |
2161 | { |
2162 | list_symbols (regexp, 1); |
2163 | } |
2164 | |
2165 | static void |
2166 | types_info (regexp) |
2167 | char *regexp; |
2168 | { |
2169 | list_symbols (regexp, 2); |
2170 | } |
bb7592f0 |
2171 | |
7a67dd45 |
2172 | #if 0 |
2173 | /* Tiemann says: "info methods was never implemented." */ |
bb7592f0 |
2174 | static void |
2175 | methods_info (regexp) |
2176 | char *regexp; |
2177 | { |
2178 | list_symbols (regexp, 3); |
2179 | } |
7a67dd45 |
2180 | #endif /* 0 */ |
3bf57d21 |
2181 | \f |
2182 | /* Call sort_block_syms to sort alphabetically the symbols of one block. */ |
2183 | |
2184 | static int |
2185 | compare_symbols (s1, s2) |
2186 | struct symbol **s1, **s2; |
2187 | { |
2188 | /* Names that are less should come first. */ |
2189 | register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2)); |
2190 | if (namediff != 0) return namediff; |
2191 | /* For symbols of the same name, registers should come first. */ |
2192 | return ((SYMBOL_CLASS (*s2) == LOC_REGISTER) |
2193 | - (SYMBOL_CLASS (*s1) == LOC_REGISTER)); |
2194 | } |
632ea0cc |
2195 | |
2196 | static void |
3bf57d21 |
2197 | sort_block_syms (b) |
2198 | register struct block *b; |
632ea0cc |
2199 | { |
3bf57d21 |
2200 | qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b), |
2201 | sizeof (struct symbol *), compare_symbols); |
632ea0cc |
2202 | } |
7b4ac7e1 |
2203 | \f |
2204 | /* Initialize the standard C scalar types. */ |
2205 | |
2206 | static |
2207 | struct type * |
2208 | init_type (code, length, uns, name) |
2209 | enum type_code code; |
2210 | int length, uns; |
2211 | char *name; |
2212 | { |
2213 | register struct type *type; |
2214 | |
2215 | type = (struct type *) xmalloc (sizeof (struct type)); |
2216 | bzero (type, sizeof *type); |
bb7592f0 |
2217 | TYPE_MAIN_VARIANT (type) = type; |
7b4ac7e1 |
2218 | TYPE_CODE (type) = code; |
2219 | TYPE_LENGTH (type) = length; |
2220 | TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0; |
2221 | TYPE_FLAGS (type) |= TYPE_FLAG_PERM; |
2222 | TYPE_NFIELDS (type) = 0; |
2223 | TYPE_NAME (type) = name; |
2224 | |
bb7592f0 |
2225 | /* C++ fancies. */ |
2226 | TYPE_NFN_FIELDS (type) = 0; |
2227 | TYPE_N_BASECLASSES (type) = 0; |
2228 | TYPE_BASECLASSES (type) = 0; |
7b4ac7e1 |
2229 | return type; |
2230 | } |
2231 | |
e91b87a3 |
2232 | /* Return Nonzero if block a is lexically nested within block b, |
2233 | or if a and b have the same pc range. |
2234 | Return zero otherwise. */ |
2235 | int |
2236 | contained_in (a, b) |
2237 | struct block *a, *b; |
2238 | { |
2239 | if (!a || !b) |
2240 | return 0; |
2241 | return a->startaddr >= b->startaddr && a->endaddr <= b->endaddr; |
2242 | } |
2243 | |
4187119d |
2244 | \f |
2245 | /* Helper routine for make_symbol_completion_list. */ |
2246 | |
2247 | int return_val_size, return_val_index; |
2248 | char **return_val; |
2249 | |
2250 | void |
2251 | completion_list_add_symbol (symname) |
2252 | char *symname; |
2253 | { |
2254 | if (return_val_index + 3 > return_val_size) |
2255 | return_val = |
2256 | (char **)xrealloc (return_val, |
2257 | (return_val_size *= 2) * sizeof (char *)); |
2258 | |
2259 | return_val[return_val_index] = |
2260 | (char *)xmalloc (1 + strlen (symname)); |
2261 | |
2262 | strcpy (return_val[return_val_index], symname); |
2263 | |
2264 | return_val[++return_val_index] = (char *)NULL; |
2265 | } |
2266 | |
2267 | /* Return a NULL terminated array of all symbols (regardless of class) which |
2268 | begin by matching TEXT. If the answer is no symbols, then the return value |
2269 | is an array which contains only a NULL pointer. |
2270 | |
2271 | Problem: All of the symbols have to be copied because readline |
2272 | frees them. I'm not going to worry about this; hopefully there |
2273 | won't be that many. */ |
2274 | |
2275 | char ** |
2276 | make_symbol_completion_list (text) |
2277 | char *text; |
2278 | { |
2279 | register struct symtab *s; |
2280 | register struct partial_symtab *ps; |
2281 | register struct blockvector *bv; |
2282 | struct blockvector *prev_bv = 0; |
2283 | register struct block *b, *surrounding_static_block; |
2284 | extern struct block *get_selected_block (); |
2285 | register int i, j; |
2286 | register struct symbol *sym; |
2287 | struct partial_symbol *psym; |
2288 | |
2289 | int text_len = strlen (text); |
2290 | return_val_size = 100; |
2291 | return_val_index = 0; |
2292 | return_val = |
2293 | (char **)xmalloc ((1 + return_val_size) *sizeof (char *)); |
2294 | return_val[0] = (char *)NULL; |
2295 | |
2296 | /* Look through the partial symtabs for all symbols which begin |
2297 | by matching TEXT. Add each one that you find to the list. */ |
2298 | |
2299 | for (ps = partial_symtab_list; ps; ps = ps->next) |
2300 | { |
2301 | /* If the psymtab's been read in we'll get it when we search |
2302 | through the blockvector. */ |
2303 | if (ps->readin) continue; |
2304 | |
2305 | for (psym = global_psymbols.list + ps->globals_offset; |
2306 | psym < (global_psymbols.list + ps->globals_offset |
2307 | + ps->n_global_syms); |
2308 | psym++) |
2309 | { |
2310 | QUIT; /* If interrupted, then quit. */ |
2311 | if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0)) |
2312 | completion_list_add_symbol (SYMBOL_NAME (psym)); |
2313 | } |
2314 | |
2315 | for (psym = static_psymbols.list + ps->statics_offset; |
2316 | psym < (static_psymbols.list + ps->statics_offset |
2317 | + ps->n_static_syms); |
2318 | psym++) |
2319 | { |
2320 | QUIT; |
2321 | if ((strncmp (SYMBOL_NAME (psym), text, text_len) == 0)) |
2322 | completion_list_add_symbol (SYMBOL_NAME (psym)); |
2323 | } |
2324 | } |
2325 | |
2326 | /* At this point scan through the misc function vector and add each |
2327 | symbol you find to the list. Eventually we want to ignore |
2328 | anything that isn't a text symbol (everything else will be |
2329 | handled by the psymtab code above). */ |
2330 | |
2331 | for (i = 0; i < misc_function_count; i++) |
2332 | if (!strncmp (text, misc_function_vector[i].name, text_len)) |
2333 | completion_list_add_symbol (misc_function_vector[i].name); |
2334 | |
2335 | /* Search upwards from currently selected frame (so that we can |
2336 | complete on local vars. */ |
2337 | for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b)) |
2338 | { |
2339 | if (!BLOCK_SUPERBLOCK (b)) |
2340 | surrounding_static_block = b; /* For elmin of dups */ |
2341 | |
2342 | /* Also catch fields of types defined in this places which |
2343 | match our text string. Only complete on types visible |
2344 | from current context. */ |
2345 | for (i = 0; i < BLOCK_NSYMS (b); i++) |
2346 | { |
2347 | register struct symbol *sym = BLOCK_SYM (b, i); |
2348 | |
2349 | if (!strncmp (SYMBOL_NAME (sym), text, text_len)) |
2350 | completion_list_add_symbol (SYMBOL_NAME (sym)); |
2351 | |
2352 | if (SYMBOL_CLASS (sym) == LOC_TYPEDEF) |
2353 | { |
2354 | struct type *t = SYMBOL_TYPE (sym); |
2355 | enum type_code c = TYPE_CODE (t); |
2356 | |
2357 | if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT) |
2358 | for (j = 0; j < TYPE_NFIELDS (t); j++) |
2359 | if (TYPE_FIELD_NAME (t, j) && |
2360 | !strncmp (TYPE_FIELD_NAME (t, j), text, text_len)) |
2361 | completion_list_add_symbol (TYPE_FIELD_NAME (t, j)); |
2362 | } |
2363 | } |
2364 | } |
2365 | |
2366 | /* Go through the symtabs and check the externs and statics for |
2367 | symbols which match. */ |
2368 | |
2369 | for (s = symtab_list; s; s = s->next) |
2370 | { |
2371 | struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 0); |
2372 | |
2373 | for (i = 0; i < BLOCK_NSYMS (b); i++) |
2374 | if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len)) |
2375 | completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i))); |
2376 | } |
2377 | |
2378 | for (s = symtab_list; s; s = s->next) |
2379 | { |
2380 | struct block *b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1); |
2381 | |
2382 | /* Don't do this block twice. */ |
2383 | if (b == surrounding_static_block) continue; |
2384 | |
2385 | for (i = 0; i < BLOCK_NSYMS (b); i++) |
2386 | if (!strncmp (SYMBOL_NAME (BLOCK_SYM (b, i)), text, text_len)) |
2387 | completion_list_add_symbol (SYMBOL_NAME (BLOCK_SYM (b, i))); |
2388 | } |
2389 | |
2390 | return (return_val); |
2391 | } |
2392 | \f |
e91b87a3 |
2393 | void |
2394 | _initialize_symtab () |
7b4ac7e1 |
2395 | { |
2396 | add_info ("variables", variables_info, |
2397 | "All global and static variable names, or those matching REGEXP."); |
2398 | add_info ("functions", functions_info, |
2399 | "All function names, or those matching REGEXP."); |
2400 | add_info ("types", types_info, |
2401 | "All types names, or those matching REGEXP."); |
7a67dd45 |
2402 | #if 0 |
bb7592f0 |
2403 | add_info ("methods", methods_info, |
2404 | "All method names, or those matching REGEXP::REGEXP.\n\ |
2405 | If the class qualifier is ommited, it is assumed to be the current scope.\n\ |
2406 | If the first REGEXP is ommited, then all methods matching the second REGEXP\n\ |
2407 | are listed."); |
7a67dd45 |
2408 | #endif |
7b4ac7e1 |
2409 | add_info ("sources", sources_info, |
2410 | "Source files in the program."); |
2411 | |
2412 | obstack_init (symbol_obstack); |
e91b87a3 |
2413 | obstack_init (psymbol_obstack); |
7b4ac7e1 |
2414 | |
4187119d |
2415 | builtin_type_void = init_type (TYPE_CODE_VOID, 1, 0, "void"); |
7b4ac7e1 |
2416 | |
2417 | builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float"); |
2418 | builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double"); |
2419 | |
2420 | builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char"); |
2421 | builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short"); |
2422 | builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long"); |
2423 | builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int"); |
2424 | |
2425 | builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char"); |
2426 | builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short"); |
2427 | builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long"); |
2428 | builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int"); |
e91b87a3 |
2429 | #ifdef LONG_LONG |
2430 | builtin_type_long_long = |
2431 | init_type (TYPE_CODE_INT, sizeof (long long), 0, "long long"); |
2432 | builtin_type_unsigned_long_long = |
2433 | init_type (TYPE_CODE_INT, sizeof (long long), 1, "unsigned long long"); |
2434 | #endif |
7b4ac7e1 |
2435 | } |
2436 | |