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