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