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