* main.c (main): Read the $HOME/.gdbinit file before processing
[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 memcpy (copy, *argptr, 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 memcpy (copy, *argptr, 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 memcpy (copy, *argptr, 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 memcpy (copy, *argptr, 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 memset (&values.sals[0], 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 memcpy (return_values.sals, values.sals,
1903 (nelts * sizeof(struct symtab_and_line)));
1904 return_values.nelts = nelts;
1905 return return_values;
1906 }
1907
1908 if (num > nelts + 2)
1909 {
1910 printf ("No choice number %d.\n", num);
1911 }
1912 else
1913 {
1914 num -= 2;
1915 if (values.sals[num].pc)
1916 {
1917 return_values.sals[i++] = values.sals[num];
1918 values.sals[num].pc = 0;
1919 }
1920 else
1921 {
1922 printf ("duplicate request for %d ignored.\n", num);
1923 }
1924 }
1925
1926 args = arg1;
1927 while (*args == ' ' || *args == '\t') args++;
1928 }
1929 return_values.nelts = i;
1930 return return_values;
1931 }
1932
1933 \f
1934 /* Slave routine for sources_info. Force line breaks at ,'s.
1935 NAME is the name to print and *FIRST is nonzero if this is the first
1936 name printed. Set *FIRST to zero. */
1937 static void
1938 output_source_filename (name, first)
1939 char *name;
1940 int *first;
1941 {
1942 /* Table of files printed so far. Since a single source file can
1943 result in several partial symbol tables, we need to avoid printing
1944 it more than once. Note: if some of the psymtabs are read in and
1945 some are not, it gets printed both under "Source files for which
1946 symbols have been read" and "Source files for which symbols will
1947 be read in on demand". I consider this a reasonable way to deal
1948 with the situation. I'm not sure whether this can also happen for
1949 symtabs; it doesn't hurt to check. */
1950 static char **tab = NULL;
1951 /* Allocated size of tab in elements.
1952 Start with one 256-byte block (when using GNU malloc.c).
1953 24 is the malloc overhead when range checking is in effect. */
1954 static int tab_alloc_size = (256 - 24) / sizeof (char *);
1955 /* Current size of tab in elements. */
1956 static int tab_cur_size;
1957
1958 char **p;
1959
1960 if (*first)
1961 {
1962 if (tab == NULL)
1963 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
1964 tab_cur_size = 0;
1965 }
1966
1967 /* Is NAME in tab? */
1968 for (p = tab; p < tab + tab_cur_size; p++)
1969 if (strcmp (*p, name) == 0)
1970 /* Yes; don't print it again. */
1971 return;
1972 /* No; add it to tab. */
1973 if (tab_cur_size == tab_alloc_size)
1974 {
1975 tab_alloc_size *= 2;
1976 tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
1977 }
1978 tab[tab_cur_size++] = name;
1979
1980 if (*first)
1981 {
1982 *first = 0;
1983 }
1984 else
1985 {
1986 printf_filtered (", ");
1987 }
1988
1989 wrap_here ("");
1990 fputs_filtered (name, stdout);
1991 }
1992
1993 static void
1994 sources_info (ignore, from_tty)
1995 char *ignore;
1996 int from_tty;
1997 {
1998 register struct symtab *s;
1999 register struct partial_symtab *ps;
2000 register struct objfile *objfile;
2001 int first;
2002
2003 if (!have_full_symbols () && !have_partial_symbols ())
2004 {
2005 error (no_symtab_msg);
2006 }
2007
2008 printf_filtered ("Source files for which symbols have been read in:\n\n");
2009
2010 first = 1;
2011 ALL_SYMTABS (objfile, s)
2012 {
2013 output_source_filename (s -> filename, &first);
2014 }
2015 printf_filtered ("\n\n");
2016
2017 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2018
2019 first = 1;
2020 ALL_PSYMTABS (objfile, ps)
2021 {
2022 if (!ps->readin)
2023 {
2024 output_source_filename (ps -> filename, &first);
2025 }
2026 }
2027 printf_filtered ("\n");
2028 }
2029
2030 static int
2031 name_match (name)
2032 char *name;
2033 {
2034 char *demangled = cplus_demangle (name, 0);
2035 if (demangled != NULL)
2036 {
2037 int cond = re_exec (demangled);
2038 free (demangled);
2039 return (cond);
2040 }
2041 return (re_exec (name));
2042 }
2043 #define NAME_MATCH(NAME) name_match(NAME)
2044
2045 /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
2046 If CLASS is zero, list all symbols except functions, type names, and
2047 constants (enums).
2048 If CLASS is 1, list only functions.
2049 If CLASS is 2, list only type names.
2050 If CLASS is 3, list only method names.
2051
2052 BPT is non-zero if we should set a breakpoint at the functions
2053 we find. */
2054
2055 static void
2056 list_symbols (regexp, class, bpt)
2057 char *regexp;
2058 int class;
2059 int bpt;
2060 {
2061 register struct symtab *s;
2062 register struct partial_symtab *ps;
2063 register struct blockvector *bv;
2064 struct blockvector *prev_bv = 0;
2065 register struct block *b;
2066 register int i, j;
2067 register struct symbol *sym;
2068 struct partial_symbol *psym;
2069 struct objfile *objfile;
2070 struct minimal_symbol *msymbol;
2071 char *val;
2072 static char *classnames[]
2073 = {"variable", "function", "type", "method"};
2074 int found_in_file = 0;
2075 int found_misc = 0;
2076 static enum minimal_symbol_type types[]
2077 = {mst_data, mst_text, mst_abs, mst_unknown};
2078 static enum minimal_symbol_type types2[]
2079 = {mst_bss, mst_text, mst_abs, mst_unknown};
2080 enum minimal_symbol_type ourtype = types[class];
2081 enum minimal_symbol_type ourtype2 = types2[class];
2082
2083 if (regexp)
2084 {
2085 /* Make sure spacing is right for C++ operators.
2086 This is just a courtesy to make the matching less sensitive
2087 to how many spaces the user leaves between 'operator'
2088 and <TYPENAME> or <OPERATOR>. */
2089 char *opend;
2090 char *opname = operator_chars (regexp, &opend);
2091 if (*opname)
2092 {
2093 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2094 if (isalpha(*opname) || *opname == '_' || *opname == '$')
2095 {
2096 /* There should 1 space between 'operator' and 'TYPENAME'. */
2097 if (opname[-1] != ' ' || opname[-2] == ' ')
2098 fix = 1;
2099 }
2100 else
2101 {
2102 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2103 if (opname[-1] == ' ')
2104 fix = 0;
2105 }
2106 /* If wrong number of spaces, fix it. */
2107 if (fix >= 0)
2108 {
2109 char *tmp = (char*) alloca(opend-opname+10);
2110 sprintf(tmp, "operator%.*s%s", fix, " ", opname);
2111 regexp = tmp;
2112 }
2113 }
2114
2115 if (0 != (val = re_comp (regexp)))
2116 error ("Invalid regexp (%s): %s", val, regexp);
2117 }
2118
2119 /* Search through the partial symtabs *first* for all symbols
2120 matching the regexp. That way we don't have to reproduce all of
2121 the machinery below. */
2122
2123 ALL_PSYMTABS (objfile, ps)
2124 {
2125 struct partial_symbol *bound, *gbound, *sbound;
2126 int keep_going = 1;
2127
2128 if (ps->readin) continue;
2129
2130 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2131 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2132 bound = gbound;
2133
2134 /* Go through all of the symbols stored in a partial
2135 symtab in one loop. */
2136 psym = objfile->global_psymbols.list + ps->globals_offset;
2137 while (keep_going)
2138 {
2139 if (psym >= bound)
2140 {
2141 if (bound == gbound && ps->n_static_syms != 0)
2142 {
2143 psym = objfile->static_psymbols.list + ps->statics_offset;
2144 bound = sbound;
2145 }
2146 else
2147 keep_going = 0;
2148 continue;
2149 }
2150 else
2151 {
2152 QUIT;
2153
2154 /* If it would match (logic taken from loop below)
2155 load the file and go on to the next one */
2156 if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (psym)))
2157 && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
2158 && SYMBOL_CLASS (psym) != LOC_BLOCK)
2159 || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
2160 || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
2161 || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
2162 {
2163 PSYMTAB_TO_SYMTAB(ps);
2164 keep_going = 0;
2165 }
2166 }
2167 psym++;
2168 }
2169 }
2170
2171 /* Here, we search through the minimal symbol tables for functions that
2172 match, and call find_pc_symtab on them to force their symbols to
2173 be read. The symbol will then be found during the scan of symtabs
2174 below. If find_pc_symtab fails, set found_misc so that we will
2175 rescan to print any matching symbols without debug info. */
2176
2177 if (class == 1)
2178 {
2179 ALL_MSYMBOLS (objfile, msymbol)
2180 {
2181 if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2182 {
2183 if (regexp == 0 || NAME_MATCH (msymbol -> name))
2184 {
2185 if (0 == find_pc_symtab (msymbol -> address))
2186 {
2187 found_misc = 1;
2188 }
2189 }
2190 }
2191 }
2192 }
2193
2194 /* Printout here so as to get after the "Reading in symbols"
2195 messages which will be generated above. */
2196 if (!bpt)
2197 printf_filtered (regexp
2198 ? "All %ss matching regular expression \"%s\":\n"
2199 : "All defined %ss:\n",
2200 classnames[class],
2201 regexp);
2202
2203 ALL_SYMTABS (objfile, s)
2204 {
2205 found_in_file = 0;
2206 bv = BLOCKVECTOR (s);
2207 /* Often many files share a blockvector.
2208 Scan each blockvector only once so that
2209 we don't get every symbol many times.
2210 It happens that the first symtab in the list
2211 for any given blockvector is the main file. */
2212 if (bv != prev_bv)
2213 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2214 {
2215 b = BLOCKVECTOR_BLOCK (bv, i);
2216 /* Skip the sort if this block is always sorted. */
2217 if (!BLOCK_SHOULD_SORT (b))
2218 sort_block_syms (b);
2219 for (j = 0; j < BLOCK_NSYMS (b); j++)
2220 {
2221 QUIT;
2222 sym = BLOCK_SYM (b, j);
2223 if ((regexp == 0 || NAME_MATCH (SYMBOL_NAME (sym)))
2224 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2225 && SYMBOL_CLASS (sym) != LOC_BLOCK
2226 && SYMBOL_CLASS (sym) != LOC_CONST)
2227 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2228 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2229 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2230 {
2231 if (bpt)
2232 {
2233 /* Set a breakpoint here, if it's a function */
2234 if (class == 1)
2235 break_command (SYMBOL_NAME(sym), 0);
2236 }
2237 else if (!found_in_file)
2238 {
2239 fputs_filtered ("\nFile ", stdout);
2240 fputs_filtered (s->filename, stdout);
2241 fputs_filtered (":\n", stdout);
2242 }
2243 found_in_file = 1;
2244
2245 if (class != 2 && i == STATIC_BLOCK)
2246 printf_filtered ("static ");
2247
2248 /* Typedef that is not a C++ class */
2249 if (class == 2
2250 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2251 typedef_print (SYMBOL_TYPE(sym), sym, stdout);
2252 /* variable, func, or typedef-that-is-c++-class */
2253 else if (class < 2 ||
2254 (class == 2 &&
2255 SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
2256 {
2257 type_print (SYMBOL_TYPE (sym),
2258 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2259 ? "" : SYMBOL_NAME (sym)),
2260 stdout, 0);
2261
2262 printf_filtered (";\n");
2263 }
2264 else
2265 {
2266 # if 0
2267 /* FIXME, why is this zapped out? */
2268 char buf[1024];
2269 type_print_base (TYPE_FN_FIELD_TYPE(t, i), stdout, 0, 0);
2270 type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i), stdout, 0);
2271 sprintf (buf, " %s::", type_name_no_tag (t));
2272 type_print_method_args (TYPE_FN_FIELD_ARGS (t, i), buf, name, stdout);
2273 # endif
2274 }
2275 }
2276 }
2277 }
2278 prev_bv = bv;
2279 }
2280
2281 /* If there are no eyes, avoid all contact. I mean, if there are
2282 no debug symbols, then print directly from the msymbol_vector. */
2283
2284 if (found_misc || class != 1)
2285 {
2286 found_in_file = 0;
2287 ALL_MSYMBOLS (objfile, msymbol)
2288 {
2289 if (msymbol -> type == ourtype || msymbol -> type == ourtype2)
2290 {
2291 if (regexp == 0 || NAME_MATCH (msymbol -> name))
2292 {
2293 /* Functions: Look up by address. */
2294 if (class != 1 ||
2295 (0 == find_pc_symtab (msymbol -> address)))
2296 {
2297 /* Variables/Absolutes: Look up by name */
2298 if (lookup_symbol (msymbol -> name,
2299 (struct block *) 0, VAR_NAMESPACE, 0,
2300 (struct symtab **) 0) == NULL)
2301 {
2302 if (!found_in_file)
2303 {
2304 printf_filtered ("\nNon-debugging symbols:\n");
2305 found_in_file = 1;
2306 }
2307 printf_filtered (" %08x %s\n",
2308 msymbol -> address,
2309 msymbol -> name);
2310 }
2311 }
2312 }
2313 }
2314 }
2315 }
2316 }
2317
2318 static void
2319 variables_info (regexp, from_tty)
2320 char *regexp;
2321 int from_tty;
2322 {
2323 list_symbols (regexp, 0, 0);
2324 }
2325
2326 static void
2327 functions_info (regexp, from_tty)
2328 char *regexp;
2329 int from_tty;
2330 {
2331 list_symbols (regexp, 1, 0);
2332 }
2333
2334 static void
2335 types_info (regexp, from_tty)
2336 char *regexp;
2337 int from_tty;
2338 {
2339 list_symbols (regexp, 2, 0);
2340 }
2341
2342 #if 0
2343 /* Tiemann says: "info methods was never implemented." */
2344 static void
2345 methods_info (regexp)
2346 char *regexp;
2347 {
2348 list_symbols (regexp, 3, 0);
2349 }
2350 #endif /* 0 */
2351
2352 /* Breakpoint all functions matching regular expression. */
2353 static void
2354 rbreak_command (regexp, from_tty)
2355 char *regexp;
2356 int from_tty;
2357 {
2358 list_symbols (regexp, 1, 1);
2359 }
2360 \f
2361
2362 /* Return Nonzero if block a is lexically nested within block b,
2363 or if a and b have the same pc range.
2364 Return zero otherwise. */
2365 int
2366 contained_in (a, b)
2367 struct block *a, *b;
2368 {
2369 if (!a || !b)
2370 return 0;
2371 return BLOCK_START (a) >= BLOCK_START (b)
2372 && BLOCK_END (a) <= BLOCK_END (b);
2373 }
2374
2375 \f
2376 /* Helper routine for make_symbol_completion_list. */
2377
2378 static int return_val_size;
2379 static int return_val_index;
2380 static char **return_val;
2381
2382 /* Test to see if the symbol specified by SYMNAME (or it's demangled
2383 equivalent) matches TEXT in the first TEXT_LEN characters. If so,
2384 add it to the current completion list. */
2385
2386 static void
2387 completion_list_add_symbol (symname, text, text_len)
2388 char *symname;
2389 char *text;
2390 int text_len;
2391 {
2392 char *demangled;
2393 int newsize;
2394
2395 /* First see if SYMNAME is a C++ mangled name, and if so, use the
2396 demangled name instead, including any parameters. */
2397
2398 if ((demangled = cplus_demangle (symname, DMGL_PARAMS | DMGL_ANSI)) != NULL)
2399 {
2400 symname = demangled;
2401 }
2402
2403 /* If we have a match for a completion, then add SYMNAME to the current
2404 list of matches. Note that we always make a copy of the string, even
2405 if it is one that was returned from cplus_demangle and is already
2406 in malloc'd memory. */
2407
2408 if (strncmp (symname, text, text_len) == 0)
2409 {
2410 if (return_val_index + 3 > return_val_size)
2411 {
2412 newsize = (return_val_size *= 2) * sizeof (char *);
2413 return_val = (char **) xrealloc ((char *) return_val, newsize);
2414 }
2415 return_val[return_val_index++] = savestring (symname, strlen (symname));
2416 return_val[return_val_index] = NULL;
2417 }
2418
2419 if (demangled != NULL)
2420 {
2421 free (demangled);
2422 }
2423 }
2424
2425 /* Return a NULL terminated array of all symbols (regardless of class) which
2426 begin by matching TEXT. If the answer is no symbols, then the return value
2427 is an array which contains only a NULL pointer.
2428
2429 Problem: All of the symbols have to be copied because readline frees them.
2430 I'm not going to worry about this; hopefully there won't be that many. */
2431
2432 char **
2433 make_symbol_completion_list (text)
2434 char *text;
2435 {
2436 register struct symbol *sym;
2437 register struct symtab *s;
2438 register struct partial_symtab *ps;
2439 register struct minimal_symbol *msymbol;
2440 register struct objfile *objfile;
2441 register struct block *b, *surrounding_static_block = 0;
2442 register int i, j;
2443 int text_len;
2444 struct partial_symbol *psym;
2445
2446 text_len = strlen (text);
2447 return_val_size = 100;
2448 return_val_index = 0;
2449 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
2450 return_val[0] = NULL;
2451
2452 /* Look through the partial symtabs for all symbols which begin
2453 by matching TEXT. Add each one that you find to the list. */
2454
2455 ALL_PSYMTABS (objfile, ps)
2456 {
2457 /* If the psymtab's been read in we'll get it when we search
2458 through the blockvector. */
2459 if (ps->readin) continue;
2460
2461 for (psym = objfile->global_psymbols.list + ps->globals_offset;
2462 psym < (objfile->global_psymbols.list + ps->globals_offset
2463 + ps->n_global_syms);
2464 psym++)
2465 {
2466 /* If interrupted, then quit. */
2467 QUIT;
2468 completion_list_add_symbol (SYMBOL_NAME (psym), text, text_len);
2469 }
2470
2471 for (psym = objfile->static_psymbols.list + ps->statics_offset;
2472 psym < (objfile->static_psymbols.list + ps->statics_offset
2473 + ps->n_static_syms);
2474 psym++)
2475 {
2476 QUIT;
2477 completion_list_add_symbol (SYMBOL_NAME (psym), text, text_len);
2478 }
2479 }
2480
2481 /* At this point scan through the misc symbol vectors and add each
2482 symbol you find to the list. Eventually we want to ignore
2483 anything that isn't a text symbol (everything else will be
2484 handled by the psymtab code above). */
2485
2486 ALL_MSYMBOLS (objfile, msymbol)
2487 {
2488 QUIT;
2489 completion_list_add_symbol (msymbol -> name, text, text_len);
2490 }
2491
2492 /* Search upwards from currently selected frame (so that we can
2493 complete on local vars. */
2494
2495 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
2496 {
2497 if (!BLOCK_SUPERBLOCK (b))
2498 {
2499 surrounding_static_block = b; /* For elmin of dups */
2500 }
2501
2502 /* Also catch fields of types defined in this places which match our
2503 text string. Only complete on types visible from current context. */
2504
2505 for (i = 0; i < BLOCK_NSYMS (b); i++)
2506 {
2507 sym = BLOCK_SYM (b, i);
2508 completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2509 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2510 {
2511 struct type *t = SYMBOL_TYPE (sym);
2512 enum type_code c = TYPE_CODE (t);
2513
2514 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2515 {
2516 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2517 {
2518 if (TYPE_FIELD_NAME (t, j))
2519 {
2520 completion_list_add_symbol (TYPE_FIELD_NAME (t, j),
2521 text, text_len);
2522 }
2523 }
2524 }
2525 }
2526 }
2527 }
2528
2529 /* Go through the symtabs and check the externs and statics for
2530 symbols which match. */
2531
2532 ALL_SYMTABS (objfile, s)
2533 {
2534 QUIT;
2535 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2536 for (i = 0; i < BLOCK_NSYMS (b); i++)
2537 {
2538 sym = BLOCK_SYM (b, i);
2539 completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2540 }
2541 }
2542
2543 ALL_SYMTABS (objfile, s)
2544 {
2545 QUIT;
2546 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2547 /* Don't do this block twice. */
2548 if (b == surrounding_static_block) continue;
2549 for (i = 0; i < BLOCK_NSYMS (b); i++)
2550 {
2551 sym = BLOCK_SYM (b, i);
2552 completion_list_add_symbol (SYMBOL_NAME (sym), text, text_len);
2553 }
2554 }
2555
2556 return (return_val);
2557 }
2558
2559 \f
2560 /* Find a mangled symbol that corresponds to LOOKFOR using brute force.
2561 Basically we go munging through available symbols, demangling each one,
2562 looking for a match on the demangled result. */
2563
2564 static char *
2565 expensive_mangler (lookfor)
2566 const char *lookfor;
2567 {
2568 register struct symbol *sym;
2569 register struct symtab *s;
2570 register struct partial_symtab *ps;
2571 register struct minimal_symbol *msymbol;
2572 register struct objfile *objfile;
2573 register struct block *b, *surrounding_static_block = 0;
2574 register int i, j;
2575 struct partial_symbol *psym;
2576 char *demangled;
2577
2578 /* Look through the partial symtabs for a symbol that matches */
2579
2580 ALL_PSYMTABS (objfile, ps)
2581 {
2582 /* If the psymtab's been read in we'll get it when we search
2583 through the blockvector. */
2584 if (ps->readin) continue;
2585
2586 for (psym = objfile->global_psymbols.list + ps->globals_offset;
2587 psym < (objfile->global_psymbols.list + ps->globals_offset
2588 + ps->n_global_syms);
2589 psym++)
2590 {
2591 QUIT; /* If interrupted, then quit. */
2592 demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2593 DMGL_PARAMS | DMGL_ANSI);
2594 if (demangled != NULL)
2595 {
2596 free (demangled);
2597 return (SYMBOL_NAME (psym));
2598 }
2599 }
2600
2601 for (psym = objfile->static_psymbols.list + ps->statics_offset;
2602 psym < (objfile->static_psymbols.list + ps->statics_offset
2603 + ps->n_static_syms);
2604 psym++)
2605 {
2606 QUIT;
2607 demangled = demangle_and_match (SYMBOL_NAME (psym), lookfor,
2608 DMGL_PARAMS | DMGL_ANSI);
2609 if (demangled != NULL)
2610 {
2611 free (demangled);
2612 return (SYMBOL_NAME (psym));
2613 }
2614 }
2615 }
2616
2617 /* Scan through the misc symbol vectors looking for a match. */
2618
2619 ALL_MSYMBOLS (objfile, msymbol)
2620 {
2621 QUIT;
2622 demangled = demangle_and_match (msymbol -> name, lookfor,
2623 DMGL_PARAMS | DMGL_ANSI);
2624 if (demangled != NULL)
2625 {
2626 free (demangled);
2627 return (msymbol -> name);
2628 }
2629 }
2630
2631 /* Search upwards from currently selected frame looking for a match */
2632
2633 for (b = get_selected_block (); b; b = BLOCK_SUPERBLOCK (b))
2634 {
2635 if (!BLOCK_SUPERBLOCK (b))
2636 surrounding_static_block = b; /* For elmin of dups */
2637
2638 /* Also catch fields of types defined in this places which
2639 match our text string. Only complete on types visible
2640 from current context. */
2641 for (i = 0; i < BLOCK_NSYMS (b); i++)
2642 {
2643 sym = BLOCK_SYM (b, i);
2644 demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2645 DMGL_PARAMS | DMGL_ANSI);
2646 if (demangled != NULL)
2647 {
2648 free (demangled);
2649 return (SYMBOL_NAME (sym));
2650 }
2651 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2652 {
2653 struct type *t = SYMBOL_TYPE (sym);
2654 enum type_code c = TYPE_CODE (t);
2655
2656 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
2657 {
2658 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
2659 {
2660 if (TYPE_FIELD_NAME (t, j))
2661 {
2662 demangled =
2663 demangle_and_match (TYPE_FIELD_NAME (t, j),
2664 lookfor,
2665 DMGL_PARAMS | DMGL_ANSI);
2666 if (demangled != NULL)
2667 {
2668 free (demangled);
2669 return (TYPE_FIELD_NAME (t, j));
2670 }
2671 }
2672 }
2673 }
2674 }
2675 }
2676 }
2677
2678 /* Go through the symtabs and check the externs and statics for
2679 symbols which match. */
2680
2681 ALL_SYMTABS (objfile, s)
2682 {
2683 QUIT;
2684 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
2685 for (i = 0; i < BLOCK_NSYMS (b); i++)
2686 {
2687 sym = BLOCK_SYM (b, i);
2688 demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2689 DMGL_PARAMS | DMGL_ANSI);
2690 if (demangled != NULL)
2691 {
2692 free (demangled);
2693 return (SYMBOL_NAME (sym));
2694 }
2695 }
2696 }
2697
2698 ALL_SYMTABS (objfile, s)
2699 {
2700 QUIT;
2701 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
2702 /* Don't do this block twice. */
2703 if (b == surrounding_static_block) continue;
2704 for (i = 0; i < BLOCK_NSYMS (b); i++)
2705 {
2706 sym = BLOCK_SYM (b, i);
2707 demangled = demangle_and_match (SYMBOL_NAME (sym), lookfor,
2708 DMGL_PARAMS | DMGL_ANSI);
2709 if (demangled != NULL)
2710 {
2711 free (demangled);
2712 return (SYMBOL_NAME (sym));
2713 }
2714 }
2715 }
2716
2717 return (NULL);
2718 }
2719
2720 \f
2721 #if 0
2722 /* Add the type of the symbol sym to the type of the current
2723 function whose block we are in (assumed). The type of
2724 this current function is contained in *TYPE.
2725
2726 This basically works as follows: When we find a function
2727 symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
2728 a pointer to its type in the global in_function_type. Every
2729 time we come across a parameter symbol ('p' in its name), then
2730 this procedure adds the name and type of that parameter
2731 to the function type pointed to by *TYPE. (Which should correspond
2732 to in_function_type if it was called correctly).
2733
2734 Note that since we are modifying a type, the result of
2735 lookup_function_type() should be memcpy()ed before calling
2736 this. When not in strict typing mode, the expression
2737 evaluator can choose to ignore this.
2738
2739 Assumption: All of a function's parameter symbols will
2740 appear before another function symbol is found. The parameters
2741 appear in the same order in the argument list as they do in the
2742 symbol table. */
2743
2744 void
2745 add_param_to_type (type,sym)
2746 struct type **type;
2747 struct symbol *sym;
2748 {
2749 int num = ++(TYPE_NFIELDS(*type));
2750
2751 if(TYPE_NFIELDS(*type)-1)
2752 TYPE_FIELDS(*type) = (struct field *)
2753 (*current_objfile->xrealloc) ((char *)(TYPE_FIELDS(*type)),
2754 num*sizeof(struct field));
2755 else
2756 TYPE_FIELDS(*type) = (struct field *)
2757 (*current_objfile->xmalloc) (num*sizeof(struct field));
2758
2759 TYPE_FIELD_BITPOS(*type,num-1) = num-1;
2760 TYPE_FIELD_BITSIZE(*type,num-1) = 0;
2761 TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
2762 TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
2763 }
2764 #endif
2765 \f
2766 void
2767 _initialize_symtab ()
2768 {
2769 add_info ("variables", variables_info,
2770 "All global and static variable names, or those matching REGEXP.");
2771 add_info ("functions", functions_info,
2772 "All function names, or those matching REGEXP.");
2773
2774 /* FIXME: This command has at least the following problems:
2775 1. It prints builtin types (in a very strange and confusing fashion).
2776 2. It doesn't print right, e.g. with
2777 typedef struct foo *FOO
2778 type_print prints "FOO" when we want to make it (in this situation)
2779 print "struct foo *".
2780 I also think "ptype" or "whatis" is more likely to be useful (but if
2781 there is much disagreement "info types" can be fixed). */
2782 add_info ("types", types_info,
2783 "All type names, or those matching REGEXP.");
2784
2785 #if 0
2786 add_info ("methods", methods_info,
2787 "All method names, or those matching REGEXP::REGEXP.\n\
2788 If the class qualifier is omitted, it is assumed to be the current scope.\n\
2789 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
2790 are listed.");
2791 #endif
2792 add_info ("sources", sources_info,
2793 "Source files in the program.");
2794
2795 add_com ("rbreak", no_class, rbreak_command,
2796 "Set a breakpoint for all functions matching REGEXP.");
2797
2798 /* Initialize the one built-in type that isn't language dependent... */
2799 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
2800 "<unknown type>", (struct objfile *) NULL);
2801 }