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