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