* c-exp.y (qualified_name): Replace explicit check for valid
[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, 1993, 1994, 1995, 1996
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "gnu-regex.h"
33 #include "expression.h"
34 #include "language.h"
35 #include "demangle.h"
36
37 #include "obstack.h"
38
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include "gdb_string.h"
42 #include "gdb_stat.h"
43 #include <ctype.h>
44
45 /* Prototypes for local functions */
46
47 extern int
48 find_methods PARAMS ((struct type *, char *, struct symbol **));
49
50 static void
51 completion_list_add_name PARAMS ((char *, char *, int, char *, char *));
52
53 static void
54 build_canonical_line_spec PARAMS ((struct symtab_and_line *, char *, char ***));
55
56 static struct symtabs_and_lines
57 decode_line_2 PARAMS ((struct symbol *[], int, int, char ***));
58
59 static void
60 rbreak_command PARAMS ((char *, int));
61
62 static void
63 types_info PARAMS ((char *, int));
64
65 static void
66 functions_info PARAMS ((char *, int));
67
68 static void
69 variables_info PARAMS ((char *, int));
70
71 static void
72 sources_info PARAMS ((char *, int));
73
74 static void
75 list_symbols PARAMS ((char *, int, int, int));
76
77 static void
78 output_source_filename PARAMS ((char *, int *));
79
80 char *
81 operator_chars PARAMS ((char *, char **));
82
83 static int find_line_common PARAMS ((struct linetable *, int, int *));
84
85 static struct partial_symbol *
86 lookup_partial_symbol PARAMS ((struct partial_symtab *, const char *,
87 int, namespace_enum));
88
89 static struct symtab *
90 lookup_symtab_1 PARAMS ((char *));
91
92 static void
93 cplusplus_hint PARAMS ((char *));
94
95 /* */
96
97 /* The single non-language-specific builtin type */
98 struct type *builtin_type_error;
99
100 /* Block in which the most recently searched-for symbol was found.
101 Might be better to make this a parameter to lookup_symbol and
102 value_of_this. */
103
104 const struct block *block_found;
105
106 char no_symtab_msg[] = "No symbol table is loaded. Use the \"file\" command.";
107
108 /* While the C++ support is still in flux, issue a possibly helpful hint on
109 using the new command completion feature on single quoted demangled C++
110 symbols. Remove when loose ends are cleaned up. FIXME -fnf */
111
112 static void
113 cplusplus_hint (name)
114 char *name;
115 {
116 while (*name == '\'')
117 name++;
118 printf_filtered ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
119 printf_filtered ("(Note leading single quote.)\n");
120 }
121
122 /* Check for a symtab of a specific name; first in symtabs, then in
123 psymtabs. *If* there is no '/' in the name, a match after a '/'
124 in the symtab filename will also work. */
125
126 static struct symtab *
127 lookup_symtab_1 (name)
128 char *name;
129 {
130 register struct symtab *s;
131 register struct partial_symtab *ps;
132 register char *slash;
133 register struct objfile *objfile;
134
135 got_symtab:
136
137 /* First, search for an exact match */
138
139 ALL_SYMTABS (objfile, s)
140 if (STREQ (name, s->filename))
141 return s;
142
143 slash = strchr (name, '/');
144
145 /* Now, search for a matching tail (only if name doesn't have any dirs) */
146
147 if (!slash)
148 ALL_SYMTABS (objfile, s)
149 {
150 char *p = s -> filename;
151 char *tail = strrchr (p, '/');
152
153 if (tail)
154 p = tail + 1;
155
156 if (STREQ (p, name))
157 return s;
158 }
159
160 /* Same search rules as above apply here, but now we look thru the
161 psymtabs. */
162
163 ps = lookup_partial_symtab (name);
164 if (!ps)
165 return (NULL);
166
167 if (ps -> readin)
168 error ("Internal: readin %s pst for `%s' found when no symtab found.",
169 ps -> filename, name);
170
171 s = PSYMTAB_TO_SYMTAB (ps);
172
173 if (s)
174 return s;
175
176 /* At this point, we have located the psymtab for this file, but
177 the conversion to a symtab has failed. This usually happens
178 when we are looking up an include file. In this case,
179 PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
180 been created. So, we need to run through the symtabs again in
181 order to find the file.
182 XXX - This is a crock, and should be fixed inside of the the
183 symbol parsing routines. */
184 goto got_symtab;
185 }
186
187 /* Lookup the symbol table of a source file named NAME. Try a couple
188 of variations if the first lookup doesn't work. */
189
190 struct symtab *
191 lookup_symtab (name)
192 char *name;
193 {
194 register struct symtab *s;
195 #if 0
196 register char *copy;
197 #endif
198
199 s = lookup_symtab_1 (name);
200 if (s) return s;
201
202 #if 0
203 /* This screws c-exp.y:yylex if there is both a type "tree" and a symtab
204 "tree.c". */
205
206 /* If name not found as specified, see if adding ".c" helps. */
207 /* Why is this? Is it just a user convenience? (If so, it's pretty
208 questionable in the presence of C++, FORTRAN, etc.). It's not in
209 the GDB manual. */
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 #endif /* 0 */
217
218 /* We didn't find anything; die. */
219 return 0;
220 }
221
222 /* Lookup the partial symbol table of a source file named NAME.
223 *If* there is no '/' in the name, a match after a '/'
224 in the psymtab filename will also work. */
225
226 struct partial_symtab *
227 lookup_partial_symtab (name)
228 char *name;
229 {
230 register struct partial_symtab *pst;
231 register struct objfile *objfile;
232
233 ALL_PSYMTABS (objfile, pst)
234 {
235 if (STREQ (name, pst -> filename))
236 {
237 return (pst);
238 }
239 }
240
241 /* Now, search for a matching tail (only if name doesn't have any dirs) */
242
243 if (!strchr (name, '/'))
244 ALL_PSYMTABS (objfile, pst)
245 {
246 char *p = pst -> filename;
247 char *tail = strrchr (p, '/');
248
249 if (tail)
250 p = tail + 1;
251
252 if (STREQ (p, name))
253 return (pst);
254 }
255
256 return (NULL);
257 }
258 \f
259 /* Demangle a GDB method stub type.
260 Note that this function is g++ specific. */
261
262 char *
263 gdb_mangle_name (type, i, j)
264 struct type *type;
265 int i, j;
266 {
267 int mangled_name_len;
268 char *mangled_name;
269 struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
270 struct fn_field *method = &f[j];
271 char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
272 char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
273 char *newname = type_name_no_tag (type);
274
275 /* Does the form of physname indicate that it is the full mangled name
276 of a constructor (not just the args)? */
277 int is_full_physname_constructor;
278
279 int is_constructor;
280 int is_destructor = DESTRUCTOR_PREFIX_P (physname);
281 /* Need a new type prefix. */
282 char *const_prefix = method->is_const ? "C" : "";
283 char *volatile_prefix = method->is_volatile ? "V" : "";
284 char buf[20];
285 int len = (newname == NULL ? 0 : strlen (newname));
286
287 is_full_physname_constructor =
288 ((physname[0]=='_' && physname[1]=='_' &&
289 (isdigit(physname[2]) || physname[2]=='Q' || physname[2]=='t'))
290 || (strncmp(physname, "__ct", 4) == 0));
291
292 is_constructor =
293 is_full_physname_constructor || (newname && STREQ(field_name, newname));
294
295 if (!is_destructor)
296 is_destructor = (strncmp(physname, "__dt", 4) == 0);
297
298 if (is_destructor || is_full_physname_constructor)
299 {
300 mangled_name = (char*) xmalloc(strlen(physname)+1);
301 strcpy(mangled_name, physname);
302 return mangled_name;
303 }
304
305 if (len == 0)
306 {
307 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
308 if (strcmp(buf, "__") == 0)
309 buf[0] = '\0';
310 }
311 else if (newname != NULL && strchr (newname, '<') != NULL)
312 {
313 /* Template methods are fully mangled. */
314 sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
315 newname = NULL;
316 len = 0;
317 }
318 else
319 {
320 sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
321 }
322 mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
323 + strlen (buf) + len
324 + strlen (physname)
325 + 1);
326
327 /* Only needed for GNU-mangled names. ANSI-mangled names
328 work with the normal mechanisms. */
329 if (OPNAME_PREFIX_P (field_name))
330 {
331 const char *opname = cplus_mangle_opname (field_name + 3, 0);
332 if (opname == NULL)
333 error ("No mangling for \"%s\"", field_name);
334 mangled_name_len += strlen (opname);
335 mangled_name = (char *)xmalloc (mangled_name_len);
336
337 strncpy (mangled_name, field_name, 3);
338 mangled_name[3] = '\0';
339 strcat (mangled_name, opname);
340 }
341 else
342 {
343 mangled_name = (char *)xmalloc (mangled_name_len);
344 if (is_constructor)
345 mangled_name[0] = '\0';
346 else
347 strcpy (mangled_name, field_name);
348 }
349 strcat (mangled_name, buf);
350 /* If the class doesn't have a name, i.e. newname NULL, then we just
351 mangle it using 0 for the length of the class. Thus it gets mangled
352 as something starting with `::' rather than `classname::'. */
353 if (newname != NULL)
354 strcat (mangled_name, newname);
355
356 strcat (mangled_name, physname);
357 return (mangled_name);
358 }
359
360 \f
361 /* Find which partial symtab on contains PC. Return 0 if none. */
362
363 struct partial_symtab *
364 find_pc_psymtab (pc)
365 register CORE_ADDR pc;
366 {
367 register struct partial_symtab *pst;
368 register struct objfile *objfile;
369
370 ALL_PSYMTABS (objfile, pst)
371 {
372 if (pc >= pst->textlow && pc < pst->texthigh)
373 {
374 struct minimal_symbol *msymbol;
375 struct partial_symtab *tpst;
376
377 /* An objfile that has its functions reordered might have
378 many partial symbol tables containing the PC, but
379 we want the partial symbol table that contains the
380 function containing the PC. */
381 if (!(objfile->flags & OBJF_REORDERED))
382 return (pst);
383
384 msymbol = lookup_minimal_symbol_by_pc (pc);
385 if (msymbol == NULL)
386 return (pst);
387
388 for (tpst = pst; tpst != NULL; tpst = tpst->next)
389 {
390 if (pc >= tpst->textlow && pc < tpst->texthigh)
391 {
392 struct partial_symbol *p;
393
394 p = find_pc_psymbol (tpst, pc);
395 if (p != NULL
396 && SYMBOL_VALUE_ADDRESS(p)
397 == SYMBOL_VALUE_ADDRESS (msymbol))
398 return (tpst);
399 }
400 }
401 return (pst);
402 }
403 }
404 return (NULL);
405 }
406
407 /* Find which partial symbol within a psymtab contains PC. Return 0
408 if none. Check all psymtabs if PSYMTAB is 0. */
409 struct partial_symbol *
410 find_pc_psymbol (psymtab, pc)
411 struct partial_symtab *psymtab;
412 CORE_ADDR pc;
413 {
414 struct partial_symbol *best = NULL, *p, **pp;
415 CORE_ADDR best_pc;
416
417 if (!psymtab)
418 psymtab = find_pc_psymtab (pc);
419 if (!psymtab)
420 return 0;
421
422 best_pc = psymtab->textlow - 1;
423
424 /* Search the global symbols as well as the static symbols, so that
425 find_pc_partial_function doesn't use a minimal symbol and thus
426 cache a bad endaddr. */
427 for (pp = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
428 (pp - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
429 < psymtab->n_global_syms);
430 pp++)
431 {
432 p = *pp;
433 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
434 && SYMBOL_CLASS (p) == LOC_BLOCK
435 && pc >= SYMBOL_VALUE_ADDRESS (p)
436 && SYMBOL_VALUE_ADDRESS (p) > best_pc)
437 {
438 best_pc = SYMBOL_VALUE_ADDRESS (p);
439 best = p;
440 }
441 }
442 for (pp = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
443 (pp - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
444 < psymtab->n_static_syms);
445 pp++)
446 {
447 p = *pp;
448 if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
449 && SYMBOL_CLASS (p) == LOC_BLOCK
450 && pc >= SYMBOL_VALUE_ADDRESS (p)
451 && SYMBOL_VALUE_ADDRESS (p) > best_pc)
452 {
453 best_pc = SYMBOL_VALUE_ADDRESS (p);
454 best = p;
455 }
456 }
457 if (best_pc == psymtab->textlow - 1)
458 return 0;
459 return best;
460 }
461
462 \f
463 /* Find the definition for a specified symbol name NAME
464 in namespace NAMESPACE, visible from lexical block BLOCK.
465 Returns the struct symbol pointer, or zero if no symbol is found.
466 If SYMTAB is non-NULL, store the symbol table in which the
467 symbol was found there, or NULL if not found.
468 C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
469 NAME is a field of the current implied argument `this'. If so set
470 *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero.
471 BLOCK_FOUND is set to the block in which NAME is found (in the case of
472 a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
473
474 /* This function has a bunch of loops in it and it would seem to be
475 attractive to put in some QUIT's (though I'm not really sure
476 whether it can run long enough to be really important). But there
477 are a few calls for which it would appear to be bad news to quit
478 out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
479 nindy_frame_chain_valid in nindy-tdep.c. (Note that there is C++
480 code below which can error(), but that probably doesn't affect
481 these calls since they are looking for a known variable and thus
482 can probably assume it will never hit the C++ code). */
483
484 struct symbol *
485 lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
486 const char *name;
487 register const struct block *block;
488 const namespace_enum namespace;
489 int *is_a_field_of_this;
490 struct symtab **symtab;
491 {
492 register struct symbol *sym;
493 register struct symtab *s = NULL;
494 register struct partial_symtab *ps;
495 struct blockvector *bv;
496 register struct objfile *objfile;
497 register struct block *b;
498 register struct minimal_symbol *msymbol;
499
500 /* Search specified block and its superiors. */
501
502 while (block != 0)
503 {
504 sym = lookup_block_symbol (block, name, namespace);
505 if (sym)
506 {
507 block_found = block;
508 if (symtab != NULL)
509 {
510 /* Search the list of symtabs for one which contains the
511 address of the start of this block. */
512 ALL_SYMTABS (objfile, s)
513 {
514 bv = BLOCKVECTOR (s);
515 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
516 if (BLOCK_START (b) <= BLOCK_START (block)
517 && BLOCK_END (b) > BLOCK_START (block))
518 goto found;
519 }
520 found:
521 *symtab = s;
522 }
523
524 return (sym);
525 }
526 block = BLOCK_SUPERBLOCK (block);
527 }
528
529 /* FIXME: this code is never executed--block is always NULL at this
530 point. What is it trying to do, anyway? We already should have
531 checked the STATIC_BLOCK above (it is the superblock of top-level
532 blocks). Why is VAR_NAMESPACE special-cased? */
533 /* Don't need to mess with the psymtabs; if we have a block,
534 that file is read in. If we don't, then we deal later with
535 all the psymtab stuff that needs checking. */
536 if (namespace == VAR_NAMESPACE && block != NULL)
537 {
538 struct block *b;
539 /* Find the right symtab. */
540 ALL_SYMTABS (objfile, s)
541 {
542 bv = BLOCKVECTOR (s);
543 b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
544 if (BLOCK_START (b) <= BLOCK_START (block)
545 && BLOCK_END (b) > BLOCK_START (block))
546 {
547 sym = lookup_block_symbol (b, name, VAR_NAMESPACE);
548 if (sym)
549 {
550 block_found = b;
551 if (symtab != NULL)
552 *symtab = s;
553 return sym;
554 }
555 }
556 }
557 }
558
559
560 /* C++: If requested to do so by the caller,
561 check to see if NAME is a field of `this'. */
562 if (is_a_field_of_this)
563 {
564 struct value *v = value_of_this (0);
565
566 *is_a_field_of_this = 0;
567 if (v && check_field (v, name))
568 {
569 *is_a_field_of_this = 1;
570 if (symtab != NULL)
571 *symtab = NULL;
572 return 0;
573 }
574 }
575
576 /* Now search all global blocks. Do the symtab's first, then
577 check the psymtab's */
578
579 ALL_SYMTABS (objfile, s)
580 {
581 bv = BLOCKVECTOR (s);
582 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
583 sym = lookup_block_symbol (block, name, namespace);
584 if (sym)
585 {
586 block_found = block;
587 if (symtab != NULL)
588 *symtab = s;
589 return sym;
590 }
591 }
592
593 /* Check for the possibility of the symbol being a function or
594 a mangled variable that is stored in one of the minimal symbol tables.
595 Eventually, all global symbols might be resolved in this way. */
596
597 if (namespace == VAR_NAMESPACE)
598 {
599 msymbol = lookup_minimal_symbol (name, NULL, NULL);
600 if (msymbol != NULL)
601 {
602 s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
603 if (s != NULL)
604 {
605 /* This is a function which has a symtab for its address. */
606 bv = BLOCKVECTOR (s);
607 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
608 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
609 namespace);
610 /* We kept static functions in minimal symbol table as well as
611 in static scope. We want to find them in the symbol table. */
612 if (!sym) {
613 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
614 sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
615 namespace);
616 }
617
618 /* sym == 0 if symbol was found in the minimal symbol table
619 but not in the symtab.
620 Return 0 to use the msymbol definition of "foo_".
621
622 This happens for Fortran "foo_" symbols,
623 which are "foo" in the symtab.
624
625 This can also happen if "asm" is used to make a
626 regular symbol but not a debugging symbol, e.g.
627 asm(".globl _main");
628 asm("_main:");
629 */
630
631 if (symtab != NULL)
632 *symtab = s;
633 return sym;
634 }
635 else if (MSYMBOL_TYPE (msymbol) != mst_text
636 && MSYMBOL_TYPE (msymbol) != mst_file_text
637 && !STREQ (name, SYMBOL_NAME (msymbol)))
638 {
639 /* This is a mangled variable, look it up by its
640 mangled name. */
641 return lookup_symbol (SYMBOL_NAME (msymbol), block,
642 namespace, is_a_field_of_this, symtab);
643 }
644 /* There are no debug symbols for this file, or we are looking
645 for an unmangled variable.
646 Try to find a matching static symbol below. */
647 }
648 }
649
650 ALL_PSYMTABS (objfile, ps)
651 {
652 if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
653 {
654 s = PSYMTAB_TO_SYMTAB(ps);
655 bv = BLOCKVECTOR (s);
656 block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
657 sym = lookup_block_symbol (block, name, namespace);
658 if (!sym)
659 error ("Internal: global symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
660 if (symtab != NULL)
661 *symtab = s;
662 return sym;
663 }
664 }
665
666 /* Now search all per-file blocks.
667 Not strictly correct, but more useful than an error.
668 Do the symtabs first, then check the psymtabs */
669
670 ALL_SYMTABS (objfile, s)
671 {
672 bv = BLOCKVECTOR (s);
673 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
674 sym = lookup_block_symbol (block, name, namespace);
675 if (sym)
676 {
677 block_found = block;
678 if (symtab != NULL)
679 *symtab = s;
680 return sym;
681 }
682 }
683
684 ALL_PSYMTABS (objfile, ps)
685 {
686 if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
687 {
688 s = PSYMTAB_TO_SYMTAB(ps);
689 bv = BLOCKVECTOR (s);
690 block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
691 sym = lookup_block_symbol (block, name, namespace);
692 if (!sym)
693 error ("Internal: static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
694 if (symtab != NULL)
695 *symtab = s;
696 return sym;
697 }
698 }
699
700 if (symtab != NULL)
701 *symtab = NULL;
702 return 0;
703 }
704
705 /* Look, in partial_symtab PST, for symbol NAME. Check the global
706 symbols if GLOBAL, the static symbols if not */
707
708 static struct partial_symbol *
709 lookup_partial_symbol (pst, name, global, namespace)
710 struct partial_symtab *pst;
711 const char *name;
712 int global;
713 namespace_enum namespace;
714 {
715 struct partial_symbol **start, **psym;
716 struct partial_symbol **top, **bottom, **center;
717 int length = (global ? pst->n_global_syms : pst->n_static_syms);
718 int do_linear_search = 1;
719
720 if (length == 0)
721 {
722 return (NULL);
723 }
724
725 start = (global ?
726 pst->objfile->global_psymbols.list + pst->globals_offset :
727 pst->objfile->static_psymbols.list + pst->statics_offset );
728
729 if (global) /* This means we can use a binary search. */
730 {
731 do_linear_search = 0;
732
733 /* Binary search. This search is guaranteed to end with center
734 pointing at the earliest partial symbol with the correct
735 name. At that point *all* partial symbols with that name
736 will be checked against the correct namespace. */
737
738 bottom = start;
739 top = start + length - 1;
740 while (top > bottom)
741 {
742 center = bottom + (top - bottom) / 2;
743 if (!(center < top))
744 abort ();
745 if (!do_linear_search && SYMBOL_LANGUAGE (*center) == language_cplus)
746 {
747 do_linear_search = 1;
748 }
749 if (STRCMP (SYMBOL_NAME (*center), name) >= 0)
750 {
751 top = center;
752 }
753 else
754 {
755 bottom = center + 1;
756 }
757 }
758 if (!(top == bottom))
759 abort ();
760 while (STREQ (SYMBOL_NAME (*top), name))
761 {
762 if (SYMBOL_NAMESPACE (*top) == namespace)
763 {
764 return (*top);
765 }
766 top ++;
767 }
768 }
769
770 /* Can't use a binary search or else we found during the binary search that
771 we should also do a linear search. */
772
773 if (do_linear_search)
774 {
775 for (psym = start; psym < start + length; psym++)
776 {
777 if (namespace == SYMBOL_NAMESPACE (*psym))
778 {
779 if (SYMBOL_MATCHES_NAME (*psym, name))
780 {
781 return (*psym);
782 }
783 }
784 }
785 }
786
787 return (NULL);
788 }
789
790 /* Find the psymtab containing main(). */
791 /* FIXME: What about languages without main() or specially linked
792 executables that have no main() ? */
793
794 struct partial_symtab *
795 find_main_psymtab ()
796 {
797 register struct partial_symtab *pst;
798 register struct objfile *objfile;
799
800 ALL_PSYMTABS (objfile, pst)
801 {
802 if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
803 {
804 return (pst);
805 }
806 }
807 return (NULL);
808 }
809
810 /* Search BLOCK for symbol NAME in NAMESPACE.
811
812 Note that if NAME is the demangled form of a C++ symbol, we will fail
813 to find a match during the binary search of the non-encoded names, but
814 for now we don't worry about the slight inefficiency of looking for
815 a match we'll never find, since it will go pretty quick. Once the
816 binary search terminates, we drop through and do a straight linear
817 search on the symbols. Each symbol which is marked as being a C++
818 symbol (language_cplus set) has both the encoded and non-encoded names
819 tested for a match. */
820
821 struct symbol *
822 lookup_block_symbol (block, name, namespace)
823 register const struct block *block;
824 const char *name;
825 const namespace_enum namespace;
826 {
827 register int bot, top, inc;
828 register struct symbol *sym;
829 register struct symbol *sym_found = NULL;
830 register int do_linear_search = 1;
831
832 /* If the blocks's symbols were sorted, start with a binary search. */
833
834 if (BLOCK_SHOULD_SORT (block))
835 {
836 /* Reset the linear search flag so if the binary search fails, we
837 won't do the linear search once unless we find some reason to
838 do so, such as finding a C++ symbol during the binary search.
839 Note that for C++ modules, ALL the symbols in a block should
840 end up marked as C++ symbols. */
841
842 do_linear_search = 0;
843 top = BLOCK_NSYMS (block);
844 bot = 0;
845
846 /* Advance BOT to not far before the first symbol whose name is NAME. */
847
848 while (1)
849 {
850 inc = (top - bot + 1);
851 /* No need to keep binary searching for the last few bits worth. */
852 if (inc < 4)
853 {
854 break;
855 }
856 inc = (inc >> 1) + bot;
857 sym = BLOCK_SYM (block, inc);
858 if (!do_linear_search && SYMBOL_LANGUAGE (sym) == language_cplus)
859 {
860 do_linear_search = 1;
861 }
862 if (SYMBOL_NAME (sym)[0] < name[0])
863 {
864 bot = inc;
865 }
866 else if (SYMBOL_NAME (sym)[0] > name[0])
867 {
868 top = inc;
869 }
870 else if (STRCMP (SYMBOL_NAME (sym), name) < 0)
871 {
872 bot = inc;
873 }
874 else
875 {
876 top = inc;
877 }
878 }
879
880 /* Now scan forward until we run out of symbols, find one whose
881 name is greater than NAME, or find one we want. If there is
882 more than one symbol with the right name and namespace, we
883 return the first one; I believe it is now impossible for us
884 to encounter two symbols with the same name and namespace
885 here, because blocks containing argument symbols are no
886 longer sorted. */
887
888 top = BLOCK_NSYMS (block);
889 while (bot < top)
890 {
891 sym = BLOCK_SYM (block, bot);
892 inc = SYMBOL_NAME (sym)[0] - name[0];
893 if (inc == 0)
894 {
895 inc = STRCMP (SYMBOL_NAME (sym), name);
896 }
897 if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
898 {
899 return (sym);
900 }
901 if (inc > 0)
902 {
903 break;
904 }
905 bot++;
906 }
907 }
908
909 /* Here if block isn't sorted, or we fail to find a match during the
910 binary search above. If during the binary search above, we find a
911 symbol which is a C++ symbol, then we have re-enabled the linear
912 search flag which was reset when starting the binary search.
913
914 This loop is equivalent to the loop above, but hacked greatly for speed.
915
916 Note that parameter symbols do not always show up last in the
917 list; this loop makes sure to take anything else other than
918 parameter symbols first; it only uses parameter symbols as a
919 last resort. Note that this only takes up extra computation
920 time on a match. */
921
922 if (do_linear_search)
923 {
924 top = BLOCK_NSYMS (block);
925 bot = 0;
926 while (bot < top)
927 {
928 sym = BLOCK_SYM (block, bot);
929 if (SYMBOL_NAMESPACE (sym) == namespace &&
930 SYMBOL_MATCHES_NAME (sym, name))
931 {
932 sym_found = sym;
933 if (SYMBOL_CLASS (sym) != LOC_ARG &&
934 SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
935 SYMBOL_CLASS (sym) != LOC_REF_ARG &&
936 SYMBOL_CLASS (sym) != LOC_REGPARM &&
937 SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
938 SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
939 {
940 break;
941 }
942 }
943 bot++;
944 }
945 }
946 return (sym_found); /* Will be NULL if not found. */
947 }
948
949 \f
950 /* Return the symbol for the function which contains a specified
951 lexical block, described by a struct block BL. */
952
953 struct symbol *
954 block_function (bl)
955 struct block *bl;
956 {
957 while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
958 bl = BLOCK_SUPERBLOCK (bl);
959
960 return BLOCK_FUNCTION (bl);
961 }
962
963 /* Find the symtab associated with PC. Look through the psymtabs and read in
964 another symtab if necessary. */
965
966 struct symtab *
967 find_pc_symtab (pc)
968 register CORE_ADDR pc;
969 {
970 register struct block *b;
971 struct blockvector *bv;
972 register struct symtab *s = NULL;
973 register struct symtab *best_s = NULL;
974 register struct partial_symtab *ps;
975 register struct objfile *objfile;
976 int distance = 0;
977
978 /* Search all symtabs for the one whose file contains our address, and which
979 is the smallest of all the ones containing the address. This is designed
980 to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
981 and symtab b is at 0x2000-0x3000. So the GLOBAL_BLOCK for a is from
982 0x1000-0x4000, but for address 0x2345 we want to return symtab b.
983
984 This happens for native ecoff format, where code from included files
985 gets its own symtab. The symtab for the included file should have
986 been read in already via the dependency mechanism.
987 It might be swifter to create several symtabs with the same name
988 like xcoff does (I'm not sure).
989
990 It also happens for objfiles that have their functions reordered.
991 For these, the symtab we are looking for is not necessarily read in. */
992
993 ALL_SYMTABS (objfile, s)
994 {
995 bv = BLOCKVECTOR (s);
996 b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
997 if (BLOCK_START (b) <= pc
998 && BLOCK_END (b) > pc
999 && (distance == 0
1000 || BLOCK_END (b) - BLOCK_START (b) < distance))
1001 {
1002 /* For an objfile that has its functions reordered,
1003 find_pc_psymtab will find the proper partial symbol table
1004 and we simply return its corresponding symtab. */
1005 if ((objfile->flags & OBJF_REORDERED) && objfile->psymtabs)
1006 {
1007 ps = find_pc_psymtab (pc);
1008 if (ps)
1009 s = PSYMTAB_TO_SYMTAB (ps);
1010 else
1011 s = NULL;
1012 return (s);
1013 }
1014 distance = BLOCK_END (b) - BLOCK_START (b);
1015 best_s = s;
1016 }
1017 }
1018
1019 if (best_s != NULL)
1020 return(best_s);
1021
1022 s = NULL;
1023 ps = find_pc_psymtab (pc);
1024 if (ps)
1025 {
1026 if (ps->readin)
1027 /* Might want to error() here (in case symtab is corrupt and
1028 will cause a core dump), but maybe we can successfully
1029 continue, so let's not. */
1030 /* FIXME-32x64: assumes pc fits in a long */
1031 warning ("\
1032 (Internal error: pc 0x%lx in read in psymtab, but not in symtab.)\n",
1033 (unsigned long) pc);
1034 s = PSYMTAB_TO_SYMTAB (ps);
1035 }
1036 return (s);
1037 }
1038 \f
1039 #if 0
1040
1041 /* Find the closest symbol value (of any sort -- function or variable)
1042 for a given address value. Slow but complete. (currently unused,
1043 mainly because it is too slow. We could fix it if each symtab and
1044 psymtab had contained in it the addresses ranges of each of its
1045 sections, which also would be required to make things like "info
1046 line *0x2345" cause psymtabs to be converted to symtabs). */
1047
1048 struct symbol *
1049 find_addr_symbol (addr, symtabp, symaddrp)
1050 CORE_ADDR addr;
1051 struct symtab **symtabp;
1052 CORE_ADDR *symaddrp;
1053 {
1054 struct symtab *symtab, *best_symtab;
1055 struct objfile *objfile;
1056 register int bot, top;
1057 register struct symbol *sym;
1058 register CORE_ADDR sym_addr;
1059 struct block *block;
1060 int blocknum;
1061
1062 /* Info on best symbol seen so far */
1063
1064 register CORE_ADDR best_sym_addr = 0;
1065 struct symbol *best_sym = 0;
1066
1067 /* FIXME -- we should pull in all the psymtabs, too! */
1068 ALL_SYMTABS (objfile, symtab)
1069 {
1070 /* Search the global and static blocks in this symtab for
1071 the closest symbol-address to the desired address. */
1072
1073 for (blocknum = GLOBAL_BLOCK; blocknum <= STATIC_BLOCK; blocknum++)
1074 {
1075 QUIT;
1076 block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), blocknum);
1077 top = BLOCK_NSYMS (block);
1078 for (bot = 0; bot < top; bot++)
1079 {
1080 sym = BLOCK_SYM (block, bot);
1081 switch (SYMBOL_CLASS (sym))
1082 {
1083 case LOC_STATIC:
1084 case LOC_LABEL:
1085 sym_addr = SYMBOL_VALUE_ADDRESS (sym);
1086 break;
1087
1088 case LOC_BLOCK:
1089 sym_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1090 break;
1091
1092 default:
1093 continue;
1094 }
1095
1096 if (sym_addr <= addr)
1097 if (sym_addr > best_sym_addr)
1098 {
1099 /* Quit if we found an exact match. */
1100 best_sym = sym;
1101 best_sym_addr = sym_addr;
1102 best_symtab = symtab;
1103 if (sym_addr == addr)
1104 goto done;
1105 }
1106 }
1107 }
1108 }
1109
1110 done:
1111 if (symtabp)
1112 *symtabp = best_symtab;
1113 if (symaddrp)
1114 *symaddrp = best_sym_addr;
1115 return best_sym;
1116 }
1117 #endif /* 0 */
1118
1119 /* Find the source file and line number for a given PC value.
1120 Return a structure containing a symtab pointer, a line number,
1121 and a pc range for the entire source line.
1122 The value's .pc field is NOT the specified pc.
1123 NOTCURRENT nonzero means, if specified pc is on a line boundary,
1124 use the line that ends there. Otherwise, in that case, the line
1125 that begins there is used. */
1126
1127 /* The big complication here is that a line may start in one file, and end just
1128 before the start of another file. This usually occurs when you #include
1129 code in the middle of a subroutine. To properly find the end of a line's PC
1130 range, we must search all symtabs associated with this compilation unit, and
1131 find the one whose first PC is closer than that of the next line in this
1132 symtab. */
1133
1134 /* If it's worth the effort, we could be using a binary search. */
1135
1136 struct symtab_and_line
1137 find_pc_line (pc, notcurrent)
1138 CORE_ADDR pc;
1139 int notcurrent;
1140 {
1141 struct symtab *s;
1142 register struct linetable *l;
1143 register int len;
1144 register int i;
1145 register struct linetable_entry *item;
1146 struct symtab_and_line val;
1147 struct blockvector *bv;
1148
1149 /* Info on best line seen so far, and where it starts, and its file. */
1150
1151 struct linetable_entry *best = NULL;
1152 CORE_ADDR best_end = 0;
1153 struct symtab *best_symtab = 0;
1154
1155 /* Store here the first line number
1156 of a file which contains the line at the smallest pc after PC.
1157 If we don't find a line whose range contains PC,
1158 we will use a line one less than this,
1159 with a range from the start of that file to the first line's pc. */
1160 struct linetable_entry *alt = NULL;
1161 struct symtab *alt_symtab = 0;
1162
1163 /* Info on best line seen in this file. */
1164
1165 struct linetable_entry *prev;
1166
1167 /* If this pc is not from the current frame,
1168 it is the address of the end of a call instruction.
1169 Quite likely that is the start of the following statement.
1170 But what we want is the statement containing the instruction.
1171 Fudge the pc to make sure we get that. */
1172
1173 if (notcurrent) pc -= 1;
1174
1175 s = find_pc_symtab (pc);
1176 if (!s)
1177 {
1178 val.symtab = 0;
1179 val.line = 0;
1180 val.pc = pc;
1181 val.end = 0;
1182 return val;
1183 }
1184
1185 bv = BLOCKVECTOR (s);
1186
1187 /* Look at all the symtabs that share this blockvector.
1188 They all have the same apriori range, that we found was right;
1189 but they have different line tables. */
1190
1191 for (; s && BLOCKVECTOR (s) == bv; s = s->next)
1192 {
1193 /* Find the best line in this symtab. */
1194 l = LINETABLE (s);
1195 if (!l)
1196 continue;
1197 len = l->nitems;
1198 if (len <= 0)
1199 {
1200 /* I think len can be zero if the symtab lacks line numbers
1201 (e.g. gcc -g1). (Either that or the LINETABLE is NULL;
1202 I'm not sure which, and maybe it depends on the symbol
1203 reader). */
1204 continue;
1205 }
1206
1207 prev = NULL;
1208 item = l->item; /* Get first line info */
1209
1210 /* Is this file's first line closer than the first lines of other files?
1211 If so, record this file, and its first line, as best alternate. */
1212 if (item->pc > pc && (!alt || item->pc < alt->pc))
1213 {
1214 alt = item;
1215 alt_symtab = s;
1216 }
1217
1218 for (i = 0; i < len; i++, item++)
1219 {
1220 /* Leave prev pointing to the linetable entry for the last line
1221 that started at or before PC. */
1222 if (item->pc > pc)
1223 break;
1224
1225 prev = item;
1226 }
1227
1228 /* At this point, prev points at the line whose start addr is <= pc, and
1229 item points at the next line. If we ran off the end of the linetable
1230 (pc >= start of the last line), then prev == item. If pc < start of
1231 the first line, prev will not be set. */
1232
1233 /* Is this file's best line closer than the best in the other files?
1234 If so, record this file, and its best line, as best so far. */
1235
1236 if (prev && (!best || prev->pc > best->pc))
1237 {
1238 best = prev;
1239 best_symtab = s;
1240 /* If another line is in the linetable, and its PC is closer
1241 than the best_end we currently have, take it as best_end. */
1242 if (i < len && (best_end == 0 || best_end > item->pc))
1243 best_end = item->pc;
1244 }
1245 }
1246
1247 if (!best_symtab)
1248 {
1249 if (!alt_symtab)
1250 { /* If we didn't find any line # info, just
1251 return zeros. */
1252 val.symtab = 0;
1253 val.line = 0;
1254 val.pc = pc;
1255 val.end = 0;
1256 }
1257 else
1258 {
1259 val.symtab = alt_symtab;
1260 val.line = alt->line - 1;
1261
1262 /* Don't return line 0, that means that we didn't find the line. */
1263 if (val.line == 0) ++val.line;
1264
1265 val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1266 val.end = alt->pc;
1267 }
1268 }
1269 else
1270 {
1271 val.symtab = best_symtab;
1272 val.line = best->line;
1273 val.pc = best->pc;
1274 if (best_end && (!alt || best_end < alt->pc))
1275 val.end = best_end;
1276 else if (alt)
1277 val.end = alt->pc;
1278 else
1279 val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
1280 }
1281 return val;
1282 }
1283 \f
1284 static int find_line_symtab PARAMS ((struct symtab *, int, struct linetable **,
1285 int *, int *));
1286
1287 /* Find line number LINE in any symtab whose name is the same as
1288 SYMTAB.
1289
1290 If found, return 1, set *LINETABLE to the linetable in which it was
1291 found, set *INDEX to the index in the linetable of the best entry
1292 found, and set *EXACT_MATCH nonzero if the value returned is an
1293 exact match.
1294
1295 If not found, return 0. */
1296
1297 static int
1298 find_line_symtab (symtab, line, linetable, index, exact_match)
1299 struct symtab *symtab;
1300 int line;
1301 struct linetable **linetable;
1302 int *index;
1303 int *exact_match;
1304 {
1305 int exact;
1306
1307 /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
1308 so far seen. */
1309
1310 int best_index;
1311 struct linetable *best_linetable;
1312
1313 /* First try looking it up in the given symtab. */
1314 best_linetable = LINETABLE (symtab);
1315 best_index = find_line_common (best_linetable, line, &exact);
1316 if (best_index < 0 || !exact)
1317 {
1318 /* Didn't find an exact match. So we better keep looking for
1319 another symtab with the same name. In the case of xcoff,
1320 multiple csects for one source file (produced by IBM's FORTRAN
1321 compiler) produce multiple symtabs (this is unavoidable
1322 assuming csects can be at arbitrary places in memory and that
1323 the GLOBAL_BLOCK of a symtab has a begin and end address). */
1324
1325 /* BEST is the smallest linenumber > LINE so far seen,
1326 or 0 if none has been seen so far.
1327 BEST_INDEX and BEST_LINETABLE identify the item for it. */
1328 int best;
1329
1330 struct objfile *objfile;
1331 struct symtab *s;
1332
1333 if (best_index >= 0)
1334 best = best_linetable->item[best_index].line;
1335 else
1336 best = 0;
1337
1338 ALL_SYMTABS (objfile, s)
1339 {
1340 struct linetable *l;
1341 int ind;
1342
1343 if (!STREQ (symtab->filename, s->filename))
1344 continue;
1345 l = LINETABLE (s);
1346 ind = find_line_common (l, line, &exact);
1347 if (ind >= 0)
1348 {
1349 if (exact)
1350 {
1351 best_index = ind;
1352 best_linetable = l;
1353 goto done;
1354 }
1355 if (best == 0 || l->item[ind].line < best)
1356 {
1357 best = l->item[ind].line;
1358 best_index = ind;
1359 best_linetable = l;
1360 }
1361 }
1362 }
1363 }
1364 done:
1365 if (best_index < 0)
1366 return 0;
1367
1368 if (index)
1369 *index = best_index;
1370 if (linetable)
1371 *linetable = best_linetable;
1372 if (exact_match)
1373 *exact_match = exact;
1374 return 1;
1375 }
1376 \f
1377 /* Find the PC value for a given source file and line number.
1378 Returns zero for invalid line number.
1379 The source file is specified with a struct symtab. */
1380
1381 CORE_ADDR
1382 find_line_pc (symtab, line)
1383 struct symtab *symtab;
1384 int line;
1385 {
1386 struct linetable *l;
1387 int ind;
1388
1389 if (symtab == 0)
1390 return 0;
1391 if (find_line_symtab (symtab, line, &l, &ind, NULL))
1392 return l->item[ind].pc;
1393 else
1394 return 0;
1395 }
1396
1397 /* Find the range of pc values in a line.
1398 Store the starting pc of the line into *STARTPTR
1399 and the ending pc (start of next line) into *ENDPTR.
1400 Returns 1 to indicate success.
1401 Returns 0 if could not find the specified line. */
1402
1403 int
1404 find_line_pc_range (sal, startptr, endptr)
1405 struct symtab_and_line sal;
1406 CORE_ADDR *startptr, *endptr;
1407 {
1408 CORE_ADDR startaddr;
1409 struct symtab_and_line found_sal;
1410
1411 startaddr = sal.pc;
1412 if (startaddr == 0)
1413 {
1414 startaddr = find_line_pc (sal.symtab, sal.line);
1415 }
1416 if (startaddr == 0)
1417 return 0;
1418
1419 /* This whole function is based on address. For example, if line 10 has
1420 two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
1421 "info line *0x123" should say the line goes from 0x100 to 0x200
1422 and "info line *0x355" should say the line goes from 0x300 to 0x400.
1423 This also insures that we never give a range like "starts at 0x134
1424 and ends at 0x12c". */
1425
1426 found_sal = find_pc_line (startaddr, 0);
1427 if (found_sal.line != sal.line)
1428 {
1429 /* The specified line (sal) has zero bytes. */
1430 *startptr = found_sal.pc;
1431 *endptr = found_sal.pc;
1432 }
1433 else
1434 {
1435 *startptr = found_sal.pc;
1436 *endptr = found_sal.end;
1437 }
1438 return 1;
1439 }
1440
1441 /* Given a line table and a line number, return the index into the line
1442 table for the pc of the nearest line whose number is >= the specified one.
1443 Return -1 if none is found. The value is >= 0 if it is an index.
1444
1445 Set *EXACT_MATCH nonzero if the value returned is an exact match. */
1446
1447 static int
1448 find_line_common (l, lineno, exact_match)
1449 register struct linetable *l;
1450 register int lineno;
1451 int *exact_match;
1452 {
1453 register int i;
1454 register int len;
1455
1456 /* BEST is the smallest linenumber > LINENO so far seen,
1457 or 0 if none has been seen so far.
1458 BEST_INDEX identifies the item for it. */
1459
1460 int best_index = -1;
1461 int best = 0;
1462
1463 if (lineno <= 0)
1464 return -1;
1465 if (l == 0)
1466 return -1;
1467
1468 len = l->nitems;
1469 for (i = 0; i < len; i++)
1470 {
1471 register struct linetable_entry *item = &(l->item[i]);
1472
1473 if (item->line == lineno)
1474 {
1475 /* Return the first (lowest address) entry which matches. */
1476 *exact_match = 1;
1477 return i;
1478 }
1479
1480 if (item->line > lineno && (best == 0 || item->line < best))
1481 {
1482 best = item->line;
1483 best_index = i;
1484 }
1485 }
1486
1487 /* If we got here, we didn't get an exact match. */
1488
1489 *exact_match = 0;
1490 return best_index;
1491 }
1492
1493 int
1494 find_pc_line_pc_range (pc, startptr, endptr)
1495 CORE_ADDR pc;
1496 CORE_ADDR *startptr, *endptr;
1497 {
1498 struct symtab_and_line sal;
1499 sal = find_pc_line (pc, 0);
1500 *startptr = sal.pc;
1501 *endptr = sal.end;
1502 return sal.symtab != 0;
1503 }
1504
1505 /* Given a function symbol SYM, find the symtab and line for the start
1506 of the function.
1507 If the argument FUNFIRSTLINE is nonzero, we want the first line
1508 of real code inside the function. */
1509
1510 static struct symtab_and_line
1511 find_function_start_sal PARAMS ((struct symbol *sym, int));
1512
1513 static struct symtab_and_line
1514 find_function_start_sal (sym, funfirstline)
1515 struct symbol *sym;
1516 int funfirstline;
1517 {
1518 CORE_ADDR pc;
1519 struct symtab_and_line sal;
1520
1521 pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
1522 if (funfirstline)
1523 {
1524 pc += FUNCTION_START_OFFSET;
1525 SKIP_PROLOGUE (pc);
1526 }
1527 sal = find_pc_line (pc, 0);
1528
1529 #ifdef PROLOGUE_FIRSTLINE_OVERLAP
1530 /* Convex: no need to suppress code on first line, if any */
1531 sal.pc = pc;
1532 #else
1533 /* Check if SKIP_PROLOGUE left us in mid-line, and the next
1534 line is still part of the same function. */
1535 if (sal.pc != pc
1536 && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
1537 && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
1538 {
1539 /* First pc of next line */
1540 pc = sal.end;
1541 /* Recalculate the line number (might not be N+1). */
1542 sal = find_pc_line (pc, 0);
1543 }
1544 sal.pc = pc;
1545 #endif
1546
1547 return sal;
1548 }
1549 \f
1550 /* If P is of the form "operator[ \t]+..." where `...' is
1551 some legitimate operator text, return a pointer to the
1552 beginning of the substring of the operator text.
1553 Otherwise, return "". */
1554 char *
1555 operator_chars (p, end)
1556 char *p;
1557 char **end;
1558 {
1559 *end = "";
1560 if (strncmp (p, "operator", 8))
1561 return *end;
1562 p += 8;
1563
1564 /* Don't get faked out by `operator' being part of a longer
1565 identifier. */
1566 if (isalpha(*p) || *p == '_' || *p == '$' || *p == '\0')
1567 return *end;
1568
1569 /* Allow some whitespace between `operator' and the operator symbol. */
1570 while (*p == ' ' || *p == '\t')
1571 p++;
1572
1573 /* Recognize 'operator TYPENAME'. */
1574
1575 if (isalpha(*p) || *p == '_' || *p == '$')
1576 {
1577 register char *q = p+1;
1578 while (isalnum(*q) || *q == '_' || *q == '$')
1579 q++;
1580 *end = q;
1581 return p;
1582 }
1583
1584 switch (*p)
1585 {
1586 case '!':
1587 case '=':
1588 case '*':
1589 case '/':
1590 case '%':
1591 case '^':
1592 if (p[1] == '=')
1593 *end = p+2;
1594 else
1595 *end = p+1;
1596 return p;
1597 case '<':
1598 case '>':
1599 case '+':
1600 case '-':
1601 case '&':
1602 case '|':
1603 if (p[1] == '=' || p[1] == p[0])
1604 *end = p+2;
1605 else
1606 *end = p+1;
1607 return p;
1608 case '~':
1609 case ',':
1610 *end = p+1;
1611 return p;
1612 case '(':
1613 if (p[1] != ')')
1614 error ("`operator ()' must be specified without whitespace in `()'");
1615 *end = p+2;
1616 return p;
1617 case '?':
1618 if (p[1] != ':')
1619 error ("`operator ?:' must be specified without whitespace in `?:'");
1620 *end = p+2;
1621 return p;
1622 case '[':
1623 if (p[1] != ']')
1624 error ("`operator []' must be specified without whitespace in `[]'");
1625 *end = p+2;
1626 return p;
1627 default:
1628 error ("`operator %s' not supported", p);
1629 break;
1630 }
1631 *end = "";
1632 return *end;
1633 }
1634
1635 /* Return the number of methods described for TYPE, including the
1636 methods from types it derives from. This can't be done in the symbol
1637 reader because the type of the baseclass might still be stubbed
1638 when the definition of the derived class is parsed. */
1639
1640 static int total_number_of_methods PARAMS ((struct type *type));
1641
1642 static int
1643 total_number_of_methods (type)
1644 struct type *type;
1645 {
1646 int n;
1647 int count;
1648
1649 CHECK_TYPEDEF (type);
1650 count = TYPE_NFN_FIELDS_TOTAL (type);
1651
1652 for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
1653 count += total_number_of_methods (TYPE_BASECLASS (type, n));
1654
1655 return count;
1656 }
1657
1658 /* Recursive helper function for decode_line_1.
1659 Look for methods named NAME in type T.
1660 Return number of matches.
1661 Put matches in SYM_ARR, which should have been allocated with
1662 a size of total_number_of_methods (T) * sizeof (struct symbol *).
1663 Note that this function is g++ specific. */
1664
1665 int
1666 find_methods (t, name, sym_arr)
1667 struct type *t;
1668 char *name;
1669 struct symbol **sym_arr;
1670 {
1671 int i1 = 0;
1672 int ibase;
1673 struct symbol *sym_class;
1674 char *class_name = type_name_no_tag (t);
1675 /* Ignore this class if it doesn't have a name. This is ugly, but
1676 unless we figure out how to get the physname without the name of
1677 the class, then the loop can't do any good. */
1678 if (class_name
1679 && (sym_class = lookup_symbol (class_name,
1680 (struct block *)NULL,
1681 STRUCT_NAMESPACE,
1682 (int *)NULL,
1683 (struct symtab **)NULL)))
1684 {
1685 int method_counter;
1686 /* FIXME: Shouldn't this just be CHECK_TYPEDEF (t)? */
1687 t = SYMBOL_TYPE (sym_class);
1688 for (method_counter = TYPE_NFN_FIELDS (t) - 1;
1689 method_counter >= 0;
1690 --method_counter)
1691 {
1692 int field_counter;
1693 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
1694 char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
1695 char dem_opname[64];
1696
1697 if (strncmp(method_name, "__", 2)==0 ||
1698 strncmp(method_name, "op", 2)==0 ||
1699 strncmp(method_name, "type", 4)==0 )
1700 {
1701 if (cplus_demangle_opname(method_name, dem_opname, DMGL_ANSI))
1702 method_name = dem_opname;
1703 else if (cplus_demangle_opname(method_name, dem_opname, 0))
1704 method_name = dem_opname;
1705 }
1706 if (STREQ (name, method_name))
1707 /* Find all the fields with that name. */
1708 for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
1709 field_counter >= 0;
1710 --field_counter)
1711 {
1712 char *phys_name;
1713 if (TYPE_FN_FIELD_STUB (f, field_counter))
1714 check_stub_method (t, method_counter, field_counter);
1715 phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
1716 /* Destructor is handled by caller, dont add it to the list */
1717 if (DESTRUCTOR_PREFIX_P (phys_name))
1718 continue;
1719
1720 sym_arr[i1] = lookup_symbol (phys_name,
1721 NULL, VAR_NAMESPACE,
1722 (int *) NULL,
1723 (struct symtab **) NULL);
1724 if (sym_arr[i1])
1725 i1++;
1726 else
1727 {
1728 fputs_filtered("(Cannot find method ", gdb_stdout);
1729 fprintf_symbol_filtered (gdb_stdout, phys_name,
1730 language_cplus,
1731 DMGL_PARAMS | DMGL_ANSI);
1732 fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
1733 }
1734 }
1735 }
1736 }
1737
1738 /* Only search baseclasses if there is no match yet, since names in
1739 derived classes override those in baseclasses.
1740
1741 FIXME: The above is not true; it is only true of member functions
1742 if they have the same number of arguments (??? - section 13.1 of the
1743 ARM says the function members are not in the same scope but doesn't
1744 really spell out the rules in a way I understand. In any case, if
1745 the number of arguments differ this is a case in which we can overload
1746 rather than hiding without any problem, and gcc 2.4.5 does overload
1747 rather than hiding in this case). */
1748
1749 if (i1)
1750 return i1;
1751 for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
1752 i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
1753 sym_arr + i1);
1754 return i1;
1755 }
1756
1757 /* Helper function for decode_line_1.
1758 Build a canonical line spec in CANONICAL if it is non-NULL and if
1759 the SAL has a symtab.
1760 If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
1761 If SYMNAME is NULL the line number from SAL is used and the canonical
1762 line spec is `filename:linenum'. */
1763
1764 static void
1765 build_canonical_line_spec (sal, symname, canonical)
1766 struct symtab_and_line *sal;
1767 char *symname;
1768 char ***canonical;
1769 {
1770 char **canonical_arr;
1771 char *canonical_name;
1772 char *filename;
1773 struct symtab *s = sal->symtab;
1774
1775 if (s == (struct symtab *)NULL
1776 || s->filename == (char *)NULL
1777 || canonical == (char ***)NULL)
1778 return;
1779
1780 canonical_arr = (char **) xmalloc (sizeof (char *));
1781 *canonical = canonical_arr;
1782
1783 filename = s->filename;
1784 if (symname != NULL)
1785 {
1786 canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
1787 sprintf (canonical_name, "%s:%s", filename, symname);
1788 }
1789 else
1790 {
1791 canonical_name = xmalloc (strlen (filename) + 30);
1792 sprintf (canonical_name, "%s:%d", filename, sal->line);
1793 }
1794 canonical_arr[0] = canonical_name;
1795 }
1796
1797 /* Parse a string that specifies a line number.
1798 Pass the address of a char * variable; that variable will be
1799 advanced over the characters actually parsed.
1800
1801 The string can be:
1802
1803 LINENUM -- that line number in current file. PC returned is 0.
1804 FILE:LINENUM -- that line in that file. PC returned is 0.
1805 FUNCTION -- line number of openbrace of that function.
1806 PC returned is the start of the function.
1807 VARIABLE -- line number of definition of that variable.
1808 PC returned is 0.
1809 FILE:FUNCTION -- likewise, but prefer functions in that file.
1810 *EXPR -- line in which address EXPR appears.
1811
1812 FUNCTION may be an undebuggable function found in minimal symbol table.
1813
1814 If the argument FUNFIRSTLINE is nonzero, we want the first line
1815 of real code inside a function when a function is specified, and it is
1816 not OK to specify a variable or type to get its line number.
1817
1818 DEFAULT_SYMTAB specifies the file to use if none is specified.
1819 It defaults to current_source_symtab.
1820 DEFAULT_LINE specifies the line number to use for relative
1821 line numbers (that start with signs). Defaults to current_source_line.
1822 If CANONICAL is non-NULL, store an array of strings containing the canonical
1823 line specs there if necessary. Currently overloaded member functions and
1824 line numbers or static functions without a filename yield a canonical
1825 line spec. The array and the line spec strings are allocated on the heap,
1826 it is the callers responsibility to free them.
1827
1828 Note that it is possible to return zero for the symtab
1829 if no file is validly specified. Callers must check that.
1830 Also, the line number returned may be invalid. */
1831
1832 /* We allow single quotes in various places. This is a hideous
1833 kludge, which exists because the completer can't yet deal with the
1834 lack of single quotes. FIXME: write a linespec_completer which we
1835 can use as appropriate instead of make_symbol_completion_list. */
1836
1837 struct symtabs_and_lines
1838 decode_line_1 (argptr, funfirstline, default_symtab, default_line, canonical)
1839 char **argptr;
1840 int funfirstline;
1841 struct symtab *default_symtab;
1842 int default_line;
1843 char ***canonical;
1844 {
1845 struct symtabs_and_lines values;
1846 #ifdef HPPA_COMPILER_BUG
1847 /* FIXME: The native HP 9000/700 compiler has a bug which appears
1848 when optimizing this file with target i960-vxworks. I haven't
1849 been able to construct a simple test case. The problem is that
1850 in the second call to SKIP_PROLOGUE below, the compiler somehow
1851 does not realize that the statement val = find_pc_line (...) will
1852 change the values of the fields of val. It extracts the elements
1853 into registers at the top of the block, and does not update the
1854 registers after the call to find_pc_line. You can check this by
1855 inserting a printf at the end of find_pc_line to show what values
1856 it is returning for val.pc and val.end and another printf after
1857 the call to see what values the function actually got (remember,
1858 this is compiling with cc -O, with this patch removed). You can
1859 also examine the assembly listing: search for the second call to
1860 skip_prologue; the LDO statement before the next call to
1861 find_pc_line loads the address of the structure which
1862 find_pc_line will return; if there is a LDW just before the LDO,
1863 which fetches an element of the structure, then the compiler
1864 still has the bug.
1865
1866 Setting val to volatile avoids the problem. We must undef
1867 volatile, because the HPPA native compiler does not define
1868 __STDC__, although it does understand volatile, and so volatile
1869 will have been defined away in defs.h. */
1870 #undef volatile
1871 volatile struct symtab_and_line val;
1872 #define volatile /*nothing*/
1873 #else
1874 struct symtab_and_line val;
1875 #endif
1876 register char *p, *p1;
1877 char *q, *pp;
1878 #if 0
1879 char *q1;
1880 #endif
1881 register struct symtab *s;
1882
1883 register struct symbol *sym;
1884 /* The symtab that SYM was found in. */
1885 struct symtab *sym_symtab;
1886
1887 register CORE_ADDR pc;
1888 register struct minimal_symbol *msymbol;
1889 char *copy;
1890 struct symbol *sym_class;
1891 int i1;
1892 int is_quoted, has_parens;
1893 struct symbol **sym_arr;
1894 struct type *t;
1895 char *saved_arg = *argptr;
1896 extern char *gdb_completer_quote_characters;
1897
1898 /* Defaults have defaults. */
1899
1900 if (default_symtab == 0)
1901 {
1902 default_symtab = current_source_symtab;
1903 default_line = current_source_line;
1904 }
1905
1906 /* See if arg is *PC */
1907
1908 if (**argptr == '*')
1909 {
1910 (*argptr)++;
1911 pc = parse_and_eval_address_1 (argptr);
1912 values.sals = (struct symtab_and_line *)
1913 xmalloc (sizeof (struct symtab_and_line));
1914 values.nelts = 1;
1915 values.sals[0] = find_pc_line (pc, 0);
1916 values.sals[0].pc = pc;
1917 return values;
1918 }
1919
1920 /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
1921
1922 s = NULL;
1923 is_quoted = (**argptr
1924 && strchr (gdb_completer_quote_characters, **argptr) != NULL);
1925 has_parens = ((pp = strchr (*argptr, '(')) != NULL
1926 && (pp = strchr (pp, ')')) != NULL);
1927
1928 for (p = *argptr; *p; p++)
1929 {
1930 if (p[0] == '<')
1931 {
1932 while(++p && *p != '>');
1933 if (!p)
1934 {
1935 error ("non-matching '<' and '>' in command");
1936 }
1937 }
1938 if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
1939 break;
1940 }
1941 while (p[0] == ' ' || p[0] == '\t') p++;
1942
1943 if ((p[0] == ':') && !has_parens)
1944 {
1945
1946 /* C++ */
1947 if (is_quoted) *argptr = *argptr+1;
1948 if (p[1] ==':')
1949 {
1950 /* Extract the class name. */
1951 p1 = p;
1952 while (p != *argptr && p[-1] == ' ') --p;
1953 copy = (char *) alloca (p - *argptr + 1);
1954 memcpy (copy, *argptr, p - *argptr);
1955 copy[p - *argptr] = 0;
1956
1957 /* Discard the class name from the arg. */
1958 p = p1 + 2;
1959 while (*p == ' ' || *p == '\t') p++;
1960 *argptr = p;
1961
1962 sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0,
1963 (struct symtab **)NULL);
1964
1965 if (sym_class &&
1966 (t = check_typedef (SYMBOL_TYPE (sym_class)),
1967 (TYPE_CODE (t) == TYPE_CODE_STRUCT
1968 || TYPE_CODE (t) == TYPE_CODE_UNION)))
1969 {
1970 /* Arg token is not digits => try it as a function name
1971 Find the next token(everything up to end or next blank). */
1972 if (**argptr
1973 && strchr (gdb_completer_quote_characters, **argptr) != NULL)
1974 {
1975 p = skip_quoted(*argptr);
1976 *argptr = *argptr + 1;
1977 }
1978 else
1979 {
1980 p = *argptr;
1981 while (*p && *p!=' ' && *p!='\t' && *p!=',' && *p!=':') p++;
1982 }
1983 /*
1984 q = operator_chars (*argptr, &q1);
1985 if (q1 - q)
1986 {
1987 char *opname;
1988 char *tmp = alloca (q1 - q + 1);
1989 memcpy (tmp, q, q1 - q);
1990 tmp[q1 - q] = '\0';
1991 opname = cplus_mangle_opname (tmp, DMGL_ANSI);
1992 if (opname == NULL)
1993 {
1994 error_begin ();
1995 printf_filtered ("no mangling for \"%s\"\n", tmp);
1996 cplusplus_hint (saved_arg);
1997 return_to_top_level (RETURN_ERROR);
1998 }
1999 copy = (char*) alloca (3 + strlen(opname));
2000 sprintf (copy, "__%s", opname);
2001 p = q1;
2002 }
2003 else
2004 */
2005 {
2006 copy = (char *) alloca (p - *argptr + 1 );
2007 memcpy (copy, *argptr, p - *argptr);
2008 copy[p - *argptr] = '\0';
2009 if (p != *argptr
2010 && copy[p - *argptr - 1]
2011 && strchr (gdb_completer_quote_characters,
2012 copy[p - *argptr - 1]) != NULL)
2013 copy[p - *argptr - 1] = '\0';
2014 }
2015
2016 /* no line number may be specified */
2017 while (*p == ' ' || *p == '\t') p++;
2018 *argptr = p;
2019
2020 sym = 0;
2021 i1 = 0; /* counter for the symbol array */
2022 sym_arr = (struct symbol **) alloca(total_number_of_methods (t)
2023 * sizeof(struct symbol *));
2024
2025 if (destructor_name_p (copy, t))
2026 {
2027 /* Destructors are a special case. */
2028 int m_index, f_index;
2029
2030 if (get_destructor_fn_field (t, &m_index, &f_index))
2031 {
2032 struct fn_field *f = TYPE_FN_FIELDLIST1 (t, m_index);
2033
2034 sym_arr[i1] =
2035 lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, f_index),
2036 NULL, VAR_NAMESPACE, (int *) NULL,
2037 (struct symtab **)NULL);
2038 if (sym_arr[i1])
2039 i1++;
2040 }
2041 }
2042 else
2043 i1 = find_methods (t, copy, sym_arr);
2044 if (i1 == 1)
2045 {
2046 /* There is exactly one field with that name. */
2047 sym = sym_arr[0];
2048
2049 if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
2050 {
2051 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2052 values.nelts = 1;
2053 values.sals[0] = find_function_start_sal (sym,
2054 funfirstline);
2055 }
2056 else
2057 {
2058 values.nelts = 0;
2059 }
2060 return values;
2061 }
2062 if (i1 > 0)
2063 {
2064 /* There is more than one field with that name
2065 (overloaded). Ask the user which one to use. */
2066 return decode_line_2 (sym_arr, i1, funfirstline, canonical);
2067 }
2068 else
2069 {
2070 char *tmp;
2071
2072 if (OPNAME_PREFIX_P (copy))
2073 {
2074 tmp = (char *)alloca (strlen (copy+3) + 9);
2075 strcpy (tmp, "operator ");
2076 strcat (tmp, copy+3);
2077 }
2078 else
2079 tmp = copy;
2080 error_begin ();
2081 if (tmp[0] == '~')
2082 printf_filtered
2083 ("the class `%s' does not have destructor defined\n",
2084 SYMBOL_SOURCE_NAME(sym_class));
2085 else
2086 printf_filtered
2087 ("the class %s does not have any method named %s\n",
2088 SYMBOL_SOURCE_NAME(sym_class), tmp);
2089 cplusplus_hint (saved_arg);
2090 return_to_top_level (RETURN_ERROR);
2091 }
2092 }
2093 else
2094 {
2095 error_begin ();
2096 /* The quotes are important if copy is empty. */
2097 printf_filtered
2098 ("can't find class, struct, or union named \"%s\"\n", copy);
2099 cplusplus_hint (saved_arg);
2100 return_to_top_level (RETURN_ERROR);
2101 }
2102 }
2103 /* end of C++ */
2104
2105
2106 /* Extract the file name. */
2107 p1 = p;
2108 while (p != *argptr && p[-1] == ' ') --p;
2109 copy = (char *) alloca (p - *argptr + 1);
2110 memcpy (copy, *argptr, p - *argptr);
2111 copy[p - *argptr] = 0;
2112
2113 /* Find that file's data. */
2114 s = lookup_symtab (copy);
2115 if (s == 0)
2116 {
2117 if (!have_full_symbols () && !have_partial_symbols ())
2118 error (no_symtab_msg);
2119 error ("No source file named %s.", copy);
2120 }
2121
2122 /* Discard the file name from the arg. */
2123 p = p1 + 1;
2124 while (*p == ' ' || *p == '\t') p++;
2125 *argptr = p;
2126 }
2127
2128 /* S is specified file's symtab, or 0 if no file specified.
2129 arg no longer contains the file name. */
2130
2131 /* Check whether arg is all digits (and sign) */
2132
2133 q = *argptr;
2134 if (*q == '-' || *q == '+') q++;
2135 while (*q >= '0' && *q <= '9')
2136 q++;
2137
2138 if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
2139 {
2140 /* We found a token consisting of all digits -- at least one digit. */
2141 enum sign {none, plus, minus} sign = none;
2142
2143 /* We might need a canonical line spec if no file was specified. */
2144 int need_canonical = (s == 0) ? 1 : 0;
2145
2146 /* This is where we need to make sure that we have good defaults.
2147 We must guarantee that this section of code is never executed
2148 when we are called with just a function name, since
2149 select_source_symtab calls us with such an argument */
2150
2151 if (s == 0 && default_symtab == 0)
2152 {
2153 select_source_symtab (0);
2154 default_symtab = current_source_symtab;
2155 default_line = current_source_line;
2156 }
2157
2158 if (**argptr == '+')
2159 sign = plus, (*argptr)++;
2160 else if (**argptr == '-')
2161 sign = minus, (*argptr)++;
2162 val.line = atoi (*argptr);
2163 switch (sign)
2164 {
2165 case plus:
2166 if (q == *argptr)
2167 val.line = 5;
2168 if (s == 0)
2169 val.line = default_line + val.line;
2170 break;
2171 case minus:
2172 if (q == *argptr)
2173 val.line = 15;
2174 if (s == 0)
2175 val.line = default_line - val.line;
2176 else
2177 val.line = 1;
2178 break;
2179 case none:
2180 break; /* No need to adjust val.line. */
2181 }
2182
2183 while (*q == ' ' || *q == '\t') q++;
2184 *argptr = q;
2185 if (s == 0)
2186 s = default_symtab;
2187 val.symtab = s;
2188 val.pc = 0;
2189 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2190 values.sals[0] = val;
2191 values.nelts = 1;
2192 if (need_canonical)
2193 build_canonical_line_spec (values.sals, NULL, canonical);
2194 return values;
2195 }
2196
2197 /* Arg token is not digits => try it as a variable name
2198 Find the next token (everything up to end or next whitespace). */
2199
2200 if (**argptr == '$') /* Convenience variable */
2201 p = skip_quoted (*argptr + 1);
2202 else if (is_quoted)
2203 {
2204 p = skip_quoted (*argptr);
2205 if (p[-1] != '\'')
2206 error ("Unmatched single quote.");
2207 }
2208 else if (has_parens)
2209 {
2210 p = pp+1;
2211 }
2212 else
2213 {
2214 p = skip_quoted(*argptr);
2215 }
2216
2217 copy = (char *) alloca (p - *argptr + 1);
2218 memcpy (copy, *argptr, p - *argptr);
2219 copy[p - *argptr] = '\0';
2220 if (p != *argptr
2221 && copy[0]
2222 && copy[0] == copy [p - *argptr - 1]
2223 && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
2224 {
2225 copy [p - *argptr - 1] = '\0';
2226 copy++;
2227 }
2228 while (*p == ' ' || *p == '\t') p++;
2229 *argptr = p;
2230
2231 /* See if it's a convenience variable */
2232
2233 if (*copy == '$')
2234 {
2235 value_ptr valx;
2236 int need_canonical = (s == 0) ? 1 : 0;
2237
2238 valx = value_of_internalvar (lookup_internalvar (copy + 1));
2239 if (TYPE_CODE (VALUE_TYPE (valx)) != TYPE_CODE_INT)
2240 error ("Convenience variables used in line specs must have integer values.");
2241
2242 val.symtab = s ? s : default_symtab;
2243 val.line = value_as_long (valx);
2244 val.pc = 0;
2245
2246 values.sals = (struct symtab_and_line *)xmalloc (sizeof val);
2247 values.sals[0] = val;
2248 values.nelts = 1;
2249
2250 if (need_canonical)
2251 build_canonical_line_spec (values.sals, NULL, canonical);
2252
2253 return values;
2254 }
2255
2256
2257 /* Look up that token as a variable.
2258 If file specified, use that file's per-file block to start with. */
2259
2260 sym = lookup_symbol (copy,
2261 (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
2262 : get_selected_block ()),
2263 VAR_NAMESPACE, 0, &sym_symtab);
2264
2265 if (sym != NULL)
2266 {
2267 if (SYMBOL_CLASS (sym) == LOC_BLOCK)
2268 {
2269 /* Arg is the name of a function */
2270 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2271 values.sals[0] = find_function_start_sal (sym, funfirstline);
2272 values.nelts = 1;
2273
2274 /* Don't use the SYMBOL_LINE; if used at all it points to
2275 the line containing the parameters or thereabouts, not
2276 the first line of code. */
2277
2278 /* We might need a canonical line spec if it is a static
2279 function. */
2280 if (s == 0)
2281 {
2282 struct blockvector *bv = BLOCKVECTOR (sym_symtab);
2283 struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
2284 if (lookup_block_symbol (b, copy, VAR_NAMESPACE) != NULL)
2285 build_canonical_line_spec (values.sals, copy, canonical);
2286 }
2287 return values;
2288 }
2289 else
2290 {
2291 if (funfirstline)
2292 error ("\"%s\" is not a function", copy);
2293 else if (SYMBOL_LINE (sym) != 0)
2294 {
2295 /* We know its line number. */
2296 values.sals = (struct symtab_and_line *)
2297 xmalloc (sizeof (struct symtab_and_line));
2298 values.nelts = 1;
2299 memset (&values.sals[0], 0, sizeof (values.sals[0]));
2300 values.sals[0].symtab = sym_symtab;
2301 values.sals[0].line = SYMBOL_LINE (sym);
2302 return values;
2303 }
2304 else
2305 /* This can happen if it is compiled with a compiler which doesn't
2306 put out line numbers for variables. */
2307 /* FIXME: Shouldn't we just set .line and .symtab to zero
2308 and return? For example, "info line foo" could print
2309 the address. */
2310 error ("Line number not known for symbol \"%s\"", copy);
2311 }
2312 }
2313
2314 msymbol = lookup_minimal_symbol (copy, NULL, NULL);
2315 if (msymbol != NULL)
2316 {
2317 val.symtab = 0;
2318 val.line = 0;
2319 val.pc = SYMBOL_VALUE_ADDRESS (msymbol);
2320 if (funfirstline)
2321 {
2322 val.pc += FUNCTION_START_OFFSET;
2323 SKIP_PROLOGUE (val.pc);
2324 }
2325 values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
2326 values.sals[0] = val;
2327 values.nelts = 1;
2328 return values;
2329 }
2330
2331 if (!have_full_symbols () &&
2332 !have_partial_symbols () && !have_minimal_symbols ())
2333 error (no_symtab_msg);
2334
2335 error ("Function \"%s\" not defined.", copy);
2336 return values; /* for lint */
2337 }
2338
2339 struct symtabs_and_lines
2340 decode_line_spec (string, funfirstline)
2341 char *string;
2342 int funfirstline;
2343 {
2344 struct symtabs_and_lines sals;
2345 if (string == 0)
2346 error ("Empty line specification.");
2347 sals = decode_line_1 (&string, funfirstline,
2348 current_source_symtab, current_source_line,
2349 (char ***)NULL);
2350 if (*string)
2351 error ("Junk at end of line specification: %s", string);
2352 return sals;
2353 }
2354
2355 /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
2356 operate on (ask user if necessary).
2357 If CANONICAL is non-NULL return a corresponding array of mangled names
2358 as canonical line specs there. */
2359
2360 static struct symtabs_and_lines
2361 decode_line_2 (sym_arr, nelts, funfirstline, canonical)
2362 struct symbol *sym_arr[];
2363 int nelts;
2364 int funfirstline;
2365 char ***canonical;
2366 {
2367 struct symtabs_and_lines values, return_values;
2368 char *args, *arg1;
2369 int i;
2370 char *prompt;
2371 char *symname;
2372 struct cleanup *old_chain;
2373 char **canonical_arr = (char **)NULL;
2374
2375 values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
2376 return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
2377 old_chain = make_cleanup (free, return_values.sals);
2378
2379 if (canonical)
2380 {
2381 canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
2382 make_cleanup (free, canonical_arr);
2383 memset (canonical_arr, 0, nelts * sizeof (char *));
2384 *canonical = canonical_arr;
2385 }
2386
2387 i = 0;
2388 printf_unfiltered("[0] cancel\n[1] all\n");
2389 while (i < nelts)
2390 {
2391 if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
2392 {
2393 values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
2394 printf_unfiltered ("[%d] %s at %s:%d\n",
2395 (i+2),
2396 SYMBOL_SOURCE_NAME (sym_arr[i]),
2397 values.sals[i].symtab->filename,
2398 values.sals[i].line);
2399 }
2400 else
2401 printf_unfiltered ("?HERE\n");
2402 i++;
2403 }
2404
2405 if ((prompt = getenv ("PS2")) == NULL)
2406 {
2407 prompt = ">";
2408 }
2409 printf_unfiltered("%s ",prompt);
2410 gdb_flush(gdb_stdout);
2411
2412 args = command_line_input ((char *) NULL, 0, "overload-choice");
2413
2414 if (args == 0 || *args == 0)
2415 error_no_arg ("one or more choice numbers");
2416
2417 i = 0;
2418 while (*args)
2419 {
2420 int num;
2421
2422 arg1 = args;
2423 while (*arg1 >= '0' && *arg1 <= '9') arg1++;
2424 if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
2425 error ("Arguments must be choice numbers.");
2426
2427 num = atoi (args);
2428
2429 if (num == 0)
2430 error ("cancelled");
2431 else if (num == 1)
2432 {
2433 if (canonical_arr)
2434 {
2435 for (i = 0; i < nelts; i++)
2436 {
2437 if (canonical_arr[i] == NULL)
2438 {
2439 symname = SYMBOL_NAME (sym_arr[i]);
2440 canonical_arr[i] = savestring (symname, strlen (symname));
2441 }
2442 }
2443 }
2444 memcpy (return_values.sals, values.sals,
2445 (nelts * sizeof(struct symtab_and_line)));
2446 return_values.nelts = nelts;
2447 discard_cleanups (old_chain);
2448 return return_values;
2449 }
2450
2451 if (num > nelts + 2)
2452 {
2453 printf_unfiltered ("No choice number %d.\n", num);
2454 }
2455 else
2456 {
2457 num -= 2;
2458 if (values.sals[num].pc)
2459 {
2460 if (canonical_arr)
2461 {
2462 symname = SYMBOL_NAME (sym_arr[num]);
2463 make_cleanup (free, symname);
2464 canonical_arr[i] = savestring (symname, strlen (symname));
2465 }
2466 return_values.sals[i++] = values.sals[num];
2467 values.sals[num].pc = 0;
2468 }
2469 else
2470 {
2471 printf_unfiltered ("duplicate request for %d ignored.\n", num);
2472 }
2473 }
2474
2475 args = arg1;
2476 while (*args == ' ' || *args == '\t') args++;
2477 }
2478 return_values.nelts = i;
2479 discard_cleanups (old_chain);
2480 return return_values;
2481 }
2482
2483 \f
2484 /* Slave routine for sources_info. Force line breaks at ,'s.
2485 NAME is the name to print and *FIRST is nonzero if this is the first
2486 name printed. Set *FIRST to zero. */
2487 static void
2488 output_source_filename (name, first)
2489 char *name;
2490 int *first;
2491 {
2492 /* Table of files printed so far. Since a single source file can
2493 result in several partial symbol tables, we need to avoid printing
2494 it more than once. Note: if some of the psymtabs are read in and
2495 some are not, it gets printed both under "Source files for which
2496 symbols have been read" and "Source files for which symbols will
2497 be read in on demand". I consider this a reasonable way to deal
2498 with the situation. I'm not sure whether this can also happen for
2499 symtabs; it doesn't hurt to check. */
2500 static char **tab = NULL;
2501 /* Allocated size of tab in elements.
2502 Start with one 256-byte block (when using GNU malloc.c).
2503 24 is the malloc overhead when range checking is in effect. */
2504 static int tab_alloc_size = (256 - 24) / sizeof (char *);
2505 /* Current size of tab in elements. */
2506 static int tab_cur_size;
2507
2508 char **p;
2509
2510 if (*first)
2511 {
2512 if (tab == NULL)
2513 tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
2514 tab_cur_size = 0;
2515 }
2516
2517 /* Is NAME in tab? */
2518 for (p = tab; p < tab + tab_cur_size; p++)
2519 if (STREQ (*p, name))
2520 /* Yes; don't print it again. */
2521 return;
2522 /* No; add it to tab. */
2523 if (tab_cur_size == tab_alloc_size)
2524 {
2525 tab_alloc_size *= 2;
2526 tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
2527 }
2528 tab[tab_cur_size++] = name;
2529
2530 if (*first)
2531 {
2532 *first = 0;
2533 }
2534 else
2535 {
2536 printf_filtered (", ");
2537 }
2538
2539 wrap_here ("");
2540 fputs_filtered (name, gdb_stdout);
2541 }
2542
2543 static void
2544 sources_info (ignore, from_tty)
2545 char *ignore;
2546 int from_tty;
2547 {
2548 register struct symtab *s;
2549 register struct partial_symtab *ps;
2550 register struct objfile *objfile;
2551 int first;
2552
2553 if (!have_full_symbols () && !have_partial_symbols ())
2554 {
2555 error (no_symtab_msg);
2556 }
2557
2558 printf_filtered ("Source files for which symbols have been read in:\n\n");
2559
2560 first = 1;
2561 ALL_SYMTABS (objfile, s)
2562 {
2563 output_source_filename (s -> filename, &first);
2564 }
2565 printf_filtered ("\n\n");
2566
2567 printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
2568
2569 first = 1;
2570 ALL_PSYMTABS (objfile, ps)
2571 {
2572 if (!ps->readin)
2573 {
2574 output_source_filename (ps -> filename, &first);
2575 }
2576 }
2577 printf_filtered ("\n");
2578 }
2579
2580 /* List all symbols (if REGEXP is NULL) or all symbols matching REGEXP.
2581 If CLASS is zero, list all symbols except functions, type names, and
2582 constants (enums).
2583 If CLASS is 1, list only functions.
2584 If CLASS is 2, list only type names.
2585 If CLASS is 3, list only method names.
2586
2587 BPT is non-zero if we should set a breakpoint at the functions
2588 we find. */
2589
2590 static void
2591 list_symbols (regexp, class, bpt, from_tty)
2592 char *regexp;
2593 int class;
2594 int bpt;
2595 int from_tty;
2596 {
2597 register struct symtab *s;
2598 register struct partial_symtab *ps;
2599 register struct blockvector *bv;
2600 struct blockvector *prev_bv = 0;
2601 register struct block *b;
2602 register int i, j;
2603 register struct symbol *sym;
2604 struct partial_symbol **psym;
2605 struct objfile *objfile;
2606 struct minimal_symbol *msymbol;
2607 char *val;
2608 static char *classnames[]
2609 = {"variable", "function", "type", "method"};
2610 int found_in_file = 0;
2611 int found_misc = 0;
2612 static enum minimal_symbol_type types[]
2613 = {mst_data, mst_text, mst_abs, mst_unknown};
2614 static enum minimal_symbol_type types2[]
2615 = {mst_bss, mst_file_text, mst_abs, mst_unknown};
2616 static enum minimal_symbol_type types3[]
2617 = {mst_file_data, mst_solib_trampoline, mst_abs, mst_unknown};
2618 static enum minimal_symbol_type types4[]
2619 = {mst_file_bss, mst_text, mst_abs, mst_unknown};
2620 enum minimal_symbol_type ourtype = types[class];
2621 enum minimal_symbol_type ourtype2 = types2[class];
2622 enum minimal_symbol_type ourtype3 = types3[class];
2623 enum minimal_symbol_type ourtype4 = types4[class];
2624
2625 if (regexp != NULL)
2626 {
2627 /* Make sure spacing is right for C++ operators.
2628 This is just a courtesy to make the matching less sensitive
2629 to how many spaces the user leaves between 'operator'
2630 and <TYPENAME> or <OPERATOR>. */
2631 char *opend;
2632 char *opname = operator_chars (regexp, &opend);
2633 if (*opname)
2634 {
2635 int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
2636 if (isalpha(*opname) || *opname == '_' || *opname == '$')
2637 {
2638 /* There should 1 space between 'operator' and 'TYPENAME'. */
2639 if (opname[-1] != ' ' || opname[-2] == ' ')
2640 fix = 1;
2641 }
2642 else
2643 {
2644 /* There should 0 spaces between 'operator' and 'OPERATOR'. */
2645 if (opname[-1] == ' ')
2646 fix = 0;
2647 }
2648 /* If wrong number of spaces, fix it. */
2649 if (fix >= 0)
2650 {
2651 char *tmp = (char*) alloca(opend-opname+10);
2652 sprintf(tmp, "operator%.*s%s", fix, " ", opname);
2653 regexp = tmp;
2654 }
2655 }
2656
2657 if (0 != (val = re_comp (regexp)))
2658 error ("Invalid regexp (%s): %s", val, regexp);
2659 }
2660
2661 /* Search through the partial symtabs *first* for all symbols
2662 matching the regexp. That way we don't have to reproduce all of
2663 the machinery below. */
2664
2665 ALL_PSYMTABS (objfile, ps)
2666 {
2667 struct partial_symbol **bound, **gbound, **sbound;
2668 int keep_going = 1;
2669
2670 if (ps->readin) continue;
2671
2672 gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
2673 sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
2674 bound = gbound;
2675
2676 /* Go through all of the symbols stored in a partial
2677 symtab in one loop. */
2678 psym = objfile->global_psymbols.list + ps->globals_offset;
2679 while (keep_going)
2680 {
2681 if (psym >= bound)
2682 {
2683 if (bound == gbound && ps->n_static_syms != 0)
2684 {
2685 psym = objfile->static_psymbols.list + ps->statics_offset;
2686 bound = sbound;
2687 }
2688 else
2689 keep_going = 0;
2690 continue;
2691 }
2692 else
2693 {
2694 QUIT;
2695
2696 /* If it would match (logic taken from loop below)
2697 load the file and go on to the next one */
2698 if ((regexp == NULL || SYMBOL_MATCHES_REGEXP (*psym))
2699 && ((class == 0 && SYMBOL_CLASS (*psym) != LOC_TYPEDEF
2700 && SYMBOL_CLASS (*psym) != LOC_BLOCK)
2701 || (class == 1 && SYMBOL_CLASS (*psym) == LOC_BLOCK)
2702 || (class == 2 && SYMBOL_CLASS (*psym) == LOC_TYPEDEF)
2703 || (class == 3 && SYMBOL_CLASS (*psym) == LOC_BLOCK)))
2704 {
2705 PSYMTAB_TO_SYMTAB(ps);
2706 keep_going = 0;
2707 }
2708 }
2709 psym++;
2710 }
2711 }
2712
2713 /* Here, we search through the minimal symbol tables for functions
2714 and variables that match, and force their symbols to be read.
2715 This is in particular necessary for demangled variable names,
2716 which are no longer put into the partial symbol tables.
2717 The symbol will then be found during the scan of symtabs below.
2718
2719 For functions, find_pc_symtab should succeed if we have debug info
2720 for the function, for variables we have to call lookup_symbol
2721 to determine if the variable has debug info.
2722 If the lookup fails, set found_misc so that we will rescan to print
2723 any matching symbols without debug info.
2724 */
2725
2726 if (class == 0 || class == 1)
2727 {
2728 ALL_MSYMBOLS (objfile, msymbol)
2729 {
2730 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2731 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2732 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2733 MSYMBOL_TYPE (msymbol) == ourtype4)
2734 {
2735 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
2736 {
2737 if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
2738 {
2739 if (class == 1
2740 || lookup_symbol (SYMBOL_NAME (msymbol),
2741 (struct block *) NULL,
2742 VAR_NAMESPACE,
2743 0, (struct symtab **) NULL) == NULL)
2744 found_misc = 1;
2745 }
2746 }
2747 }
2748 }
2749 }
2750
2751 /* Printout here so as to get after the "Reading in symbols"
2752 messages which will be generated above. */
2753 if (!bpt)
2754 printf_filtered (regexp
2755 ? "All %ss matching regular expression \"%s\":\n"
2756 : "All defined %ss:\n",
2757 classnames[class],
2758 regexp);
2759
2760 ALL_SYMTABS (objfile, s)
2761 {
2762 found_in_file = 0;
2763 bv = BLOCKVECTOR (s);
2764 /* Often many files share a blockvector.
2765 Scan each blockvector only once so that
2766 we don't get every symbol many times.
2767 It happens that the first symtab in the list
2768 for any given blockvector is the main file. */
2769 if (bv != prev_bv)
2770 for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
2771 {
2772 b = BLOCKVECTOR_BLOCK (bv, i);
2773 /* Skip the sort if this block is always sorted. */
2774 if (!BLOCK_SHOULD_SORT (b))
2775 sort_block_syms (b);
2776 for (j = 0; j < BLOCK_NSYMS (b); j++)
2777 {
2778 QUIT;
2779 sym = BLOCK_SYM (b, j);
2780 if ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
2781 && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
2782 && SYMBOL_CLASS (sym) != LOC_BLOCK
2783 && SYMBOL_CLASS (sym) != LOC_CONST)
2784 || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
2785 || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
2786 || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
2787 {
2788 if (bpt)
2789 {
2790 /* Set a breakpoint here, if it's a function */
2791 if (class == 1)
2792 {
2793 /* There may be more than one function with the
2794 same name but in different files. In order to
2795 set breakpoints on all of them, we must give
2796 both the file name and the function name to
2797 break_command.
2798 Quoting the symbol name gets rid of problems
2799 with mangled symbol names that contain
2800 CPLUS_MARKER characters. */
2801 char *string =
2802 (char *) alloca (strlen (s->filename)
2803 + strlen (SYMBOL_NAME(sym))
2804 + 4);
2805 strcpy (string, s->filename);
2806 strcat (string, ":'");
2807 strcat (string, SYMBOL_NAME(sym));
2808 strcat (string, "'");
2809 break_command (string, from_tty);
2810 }
2811 }
2812 else if (!found_in_file)
2813 {
2814 fputs_filtered ("\nFile ", gdb_stdout);
2815 fputs_filtered (s->filename, gdb_stdout);
2816 fputs_filtered (":\n", gdb_stdout);
2817 }
2818 found_in_file = 1;
2819
2820 if (class != 2 && i == STATIC_BLOCK)
2821 printf_filtered ("static ");
2822
2823 /* Typedef that is not a C++ class */
2824 if (class == 2
2825 && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
2826 c_typedef_print (SYMBOL_TYPE(sym), sym, gdb_stdout);
2827 /* variable, func, or typedef-that-is-c++-class */
2828 else if (class < 2 ||
2829 (class == 2 &&
2830 SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
2831 {
2832 type_print (SYMBOL_TYPE (sym),
2833 (SYMBOL_CLASS (sym) == LOC_TYPEDEF
2834 ? "" : SYMBOL_SOURCE_NAME (sym)),
2835 gdb_stdout, 0);
2836
2837 printf_filtered (";\n");
2838 }
2839 else
2840 {
2841 # if 0
2842 /* Tiemann says: "info methods was never implemented." */
2843 char *demangled_name;
2844 c_type_print_base (TYPE_FN_FIELD_TYPE(t, i),
2845 gdb_stdout, 0, 0);
2846 c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i),
2847 gdb_stdout, 0);
2848 if (TYPE_FN_FIELD_STUB (t, i))
2849 check_stub_method (TYPE_DOMAIN_TYPE (type), j, i);
2850 demangled_name =
2851 cplus_demangle (TYPE_FN_FIELD_PHYSNAME (t, i),
2852 DMGL_ANSI | DMGL_PARAMS);
2853 if (demangled_name == NULL)
2854 fprintf_filtered (stream, "<badly mangled name %s>",
2855 TYPE_FN_FIELD_PHYSNAME (t, i));
2856 else
2857 {
2858 fputs_filtered (demangled_name, stream);
2859 free (demangled_name);
2860 }
2861 # endif
2862 }
2863 }
2864 }
2865 }
2866 prev_bv = bv;
2867 }
2868
2869 /* If there are no eyes, avoid all contact. I mean, if there are
2870 no debug symbols, then print directly from the msymbol_vector. */
2871
2872 if (found_misc || class != 1)
2873 {
2874 found_in_file = 0;
2875 ALL_MSYMBOLS (objfile, msymbol)
2876 {
2877 if (MSYMBOL_TYPE (msymbol) == ourtype ||
2878 MSYMBOL_TYPE (msymbol) == ourtype2 ||
2879 MSYMBOL_TYPE (msymbol) == ourtype3 ||
2880 MSYMBOL_TYPE (msymbol) == ourtype4)
2881 {
2882 if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
2883 {
2884 /* Functions: Look up by address. */
2885 if (class != 1 ||
2886 (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
2887 {
2888 /* Variables/Absolutes: Look up by name */
2889 if (lookup_symbol (SYMBOL_NAME (msymbol),
2890 (struct block *) NULL, VAR_NAMESPACE,
2891 0, (struct symtab **) NULL) == NULL)
2892 {
2893 if (bpt)
2894 {
2895 break_command (SYMBOL_NAME (msymbol), from_tty);
2896 printf_filtered ("<function, no debug info> %s;\n",
2897 SYMBOL_SOURCE_NAME (msymbol));
2898 continue;
2899 }
2900 if (!found_in_file)
2901 {
2902 printf_filtered ("\nNon-debugging symbols:\n");
2903 found_in_file = 1;
2904 }
2905 printf_filtered (" %08lx %s\n",
2906 (unsigned long) SYMBOL_VALUE_ADDRESS (msymbol),
2907 SYMBOL_SOURCE_NAME (msymbol));
2908 }
2909 }
2910 }
2911 }
2912 }
2913 }
2914 }
2915
2916 static void
2917 variables_info (regexp, from_tty)
2918 char *regexp;
2919 int from_tty;
2920 {
2921 list_symbols (regexp, 0, 0, from_tty);
2922 }
2923
2924 static void
2925 functions_info (regexp, from_tty)
2926 char *regexp;
2927 int from_tty;
2928 {
2929 list_symbols (regexp, 1, 0, from_tty);
2930 }
2931
2932 static void
2933 types_info (regexp, from_tty)
2934 char *regexp;
2935 int from_tty;
2936 {
2937 list_symbols (regexp, 2, 0, from_tty);
2938 }
2939
2940 #if 0
2941 /* Tiemann says: "info methods was never implemented." */
2942 static void
2943 methods_info (regexp)
2944 char *regexp;
2945 {
2946 list_symbols (regexp, 3, 0, from_tty);
2947 }
2948 #endif /* 0 */
2949
2950 /* Breakpoint all functions matching regular expression. */
2951 static void
2952 rbreak_command (regexp, from_tty)
2953 char *regexp;
2954 int from_tty;
2955 {
2956 list_symbols (regexp, 1, 1, from_tty);
2957 }
2958 \f
2959
2960 /* Return Nonzero if block a is lexically nested within block b,
2961 or if a and b have the same pc range.
2962 Return zero otherwise. */
2963 int
2964 contained_in (a, b)
2965 struct block *a, *b;
2966 {
2967 if (!a || !b)
2968 return 0;
2969 return BLOCK_START (a) >= BLOCK_START (b)
2970 && BLOCK_END (a) <= BLOCK_END (b);
2971 }
2972
2973 \f
2974 /* Helper routine for make_symbol_completion_list. */
2975
2976 static int return_val_size;
2977 static int return_val_index;
2978 static char **return_val;
2979
2980 #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
2981 do { \
2982 if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
2983 /* Put only the mangled name on the list. */ \
2984 /* Advantage: "b foo<TAB>" completes to "b foo(int, int)" */ \
2985 /* Disadvantage: "b foo__i<TAB>" doesn't complete. */ \
2986 completion_list_add_name \
2987 (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
2988 else \
2989 completion_list_add_name \
2990 (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
2991 } while (0)
2992
2993 /* Test to see if the symbol specified by SYMNAME (which is already
2994 demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
2995 characters. If so, add it to the current completion list. */
2996
2997 static void
2998 completion_list_add_name (symname, sym_text, sym_text_len, text, word)
2999 char *symname;
3000 char *sym_text;
3001 int sym_text_len;
3002 char *text;
3003 char *word;
3004 {
3005 int newsize;
3006 int i;
3007
3008 /* clip symbols that cannot match */
3009
3010 if (strncmp (symname, sym_text, sym_text_len) != 0)
3011 {
3012 return;
3013 }
3014
3015 /* Clip any symbol names that we've already considered. (This is a
3016 time optimization) */
3017
3018 for (i = 0; i < return_val_index; ++i)
3019 {
3020 if (STREQ (symname, return_val[i]))
3021 {
3022 return;
3023 }
3024 }
3025
3026 /* We have a match for a completion, so add SYMNAME to the current list
3027 of matches. Note that the name is moved to freshly malloc'd space. */
3028
3029 {
3030 char *new;
3031 if (word == sym_text)
3032 {
3033 new = xmalloc (strlen (symname) + 5);
3034 strcpy (new, symname);
3035 }
3036 else if (word > sym_text)
3037 {
3038 /* Return some portion of symname. */
3039 new = xmalloc (strlen (symname) + 5);
3040 strcpy (new, symname + (word - sym_text));
3041 }
3042 else
3043 {
3044 /* Return some of SYM_TEXT plus symname. */
3045 new = xmalloc (strlen (symname) + (sym_text - word) + 5);
3046 strncpy (new, word, sym_text - word);
3047 new[sym_text - word] = '\0';
3048 strcat (new, symname);
3049 }
3050
3051 /* Recheck for duplicates if we intend to add a modified symbol. */
3052 if (word != sym_text)
3053 {
3054 for (i = 0; i < return_val_index; ++i)
3055 {
3056 if (STREQ (new, return_val[i]))
3057 {
3058 free (new);
3059 return;
3060 }
3061 }
3062 }
3063
3064 if (return_val_index + 3 > return_val_size)
3065 {
3066 newsize = (return_val_size *= 2) * sizeof (char *);
3067 return_val = (char **) xrealloc ((char *) return_val, newsize);
3068 }
3069 return_val[return_val_index++] = new;
3070 return_val[return_val_index] = NULL;
3071 }
3072 }
3073
3074 /* Return a NULL terminated array of all symbols (regardless of class) which
3075 begin by matching TEXT. If the answer is no symbols, then the return value
3076 is an array which contains only a NULL pointer.
3077
3078 Problem: All of the symbols have to be copied because readline frees them.
3079 I'm not going to worry about this; hopefully there won't be that many. */
3080
3081 char **
3082 make_symbol_completion_list (text, word)
3083 char *text;
3084 char *word;
3085 {
3086 register struct symbol *sym;
3087 register struct symtab *s;
3088 register struct partial_symtab *ps;
3089 register struct minimal_symbol *msymbol;
3090 register struct objfile *objfile;
3091 register struct block *b, *surrounding_static_block = 0;
3092 register int i, j;
3093 struct partial_symbol **psym;
3094 /* The symbol we are completing on. Points in same buffer as text. */
3095 char *sym_text;
3096 /* Length of sym_text. */
3097 int sym_text_len;
3098
3099 /* Now look for the symbol we are supposed to complete on.
3100 FIXME: This should be language-specific. */
3101 {
3102 char *p;
3103 char quote_found;
3104 char *quote_pos = NULL;
3105
3106 /* First see if this is a quoted string. */
3107 quote_found = '\0';
3108 for (p = text; *p != '\0'; ++p)
3109 {
3110 if (quote_found != '\0')
3111 {
3112 if (*p == quote_found)
3113 /* Found close quote. */
3114 quote_found = '\0';
3115 else if (*p == '\\' && p[1] == quote_found)
3116 /* A backslash followed by the quote character
3117 doesn't end the string. */
3118 ++p;
3119 }
3120 else if (*p == '\'' || *p == '"')
3121 {
3122 quote_found = *p;
3123 quote_pos = p;
3124 }
3125 }
3126 if (quote_found == '\'')
3127 /* A string within single quotes can be a symbol, so complete on it. */
3128 sym_text = quote_pos + 1;
3129 else if (quote_found == '"')
3130 /* A double-quoted string is never a symbol, nor does it make sense
3131 to complete it any other way. */
3132 return NULL;
3133 else
3134 {
3135 /* It is not a quoted string. Break it based on the characters
3136 which are in symbols. */
3137 while (p > text)
3138 {
3139 if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
3140 --p;
3141 else
3142 break;
3143 }
3144 sym_text = p;
3145 }
3146 }
3147
3148 sym_text_len = strlen (sym_text);
3149
3150 return_val_size = 100;
3151 return_val_index = 0;
3152 return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
3153 return_val[0] = NULL;
3154
3155 /* Look through the partial symtabs for all symbols which begin
3156 by matching SYM_TEXT. Add each one that you find to the list. */
3157
3158 ALL_PSYMTABS (objfile, ps)
3159 {
3160 /* If the psymtab's been read in we'll get it when we search
3161 through the blockvector. */
3162 if (ps->readin) continue;
3163
3164 for (psym = objfile->global_psymbols.list + ps->globals_offset;
3165 psym < (objfile->global_psymbols.list + ps->globals_offset
3166 + ps->n_global_syms);
3167 psym++)
3168 {
3169 /* If interrupted, then quit. */
3170 QUIT;
3171 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3172 }
3173
3174 for (psym = objfile->static_psymbols.list + ps->statics_offset;
3175 psym < (objfile->static_psymbols.list + ps->statics_offset
3176 + ps->n_static_syms);
3177 psym++)
3178 {
3179 QUIT;
3180 COMPLETION_LIST_ADD_SYMBOL (*psym, sym_text, sym_text_len, text, word);
3181 }
3182 }
3183
3184 /* At this point scan through the misc symbol vectors and add each
3185 symbol you find to the list. Eventually we want to ignore
3186 anything that isn't a text symbol (everything else will be
3187 handled by the psymtab code above). */
3188
3189 ALL_MSYMBOLS (objfile, msymbol)
3190 {
3191 QUIT;
3192 COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
3193 }
3194
3195 /* Search upwards from currently selected frame (so that we can
3196 complete on local vars. */
3197
3198 for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
3199 {
3200 if (!BLOCK_SUPERBLOCK (b))
3201 {
3202 surrounding_static_block = b; /* For elmin of dups */
3203 }
3204
3205 /* Also catch fields of types defined in this places which match our
3206 text string. Only complete on types visible from current context. */
3207
3208 for (i = 0; i < BLOCK_NSYMS (b); i++)
3209 {
3210 sym = BLOCK_SYM (b, i);
3211 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3212 if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
3213 {
3214 struct type *t = SYMBOL_TYPE (sym);
3215 enum type_code c = TYPE_CODE (t);
3216
3217 if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
3218 {
3219 for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
3220 {
3221 if (TYPE_FIELD_NAME (t, j))
3222 {
3223 completion_list_add_name (TYPE_FIELD_NAME (t, j),
3224 sym_text, sym_text_len, text, word);
3225 }
3226 }
3227 }
3228 }
3229 }
3230 }
3231
3232 /* Go through the symtabs and check the externs and statics for
3233 symbols which match. */
3234
3235 ALL_SYMTABS (objfile, s)
3236 {
3237 QUIT;
3238 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
3239 for (i = 0; i < BLOCK_NSYMS (b); i++)
3240 {
3241 sym = BLOCK_SYM (b, i);
3242 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3243 }
3244 }
3245
3246 ALL_SYMTABS (objfile, s)
3247 {
3248 QUIT;
3249 b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
3250 /* Don't do this block twice. */
3251 if (b == surrounding_static_block) continue;
3252 for (i = 0; i < BLOCK_NSYMS (b); i++)
3253 {
3254 sym = BLOCK_SYM (b, i);
3255 COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
3256 }
3257 }
3258
3259 return (return_val);
3260 }
3261
3262 /* Determine if PC is in the prologue of a function. The prologue is the area
3263 between the first instruction of a function, and the first executable line.
3264 Returns 1 if PC *might* be in prologue, 0 if definately *not* in prologue.
3265
3266 If non-zero, func_start is where we thing the prologue starts, possibly
3267 by previous examination of symbol table information.
3268 */
3269
3270 int
3271 in_prologue (pc, func_start)
3272 CORE_ADDR pc;
3273 CORE_ADDR func_start;
3274 {
3275 struct symtab_and_line sal;
3276 CORE_ADDR func_addr, func_end;
3277
3278 if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
3279 goto nosyms; /* Might be in prologue */
3280
3281 sal = find_pc_line (func_addr, 0);
3282
3283 if (sal.line == 0)
3284 goto nosyms;
3285
3286 if (sal.end > func_addr
3287 && sal.end <= func_end) /* Is prologue in function? */
3288 return pc < sal.end; /* Yes, is pc in prologue? */
3289
3290 /* The line after the prologue seems to be outside the function. In this
3291 case, tell the caller to find the prologue the hard way. */
3292
3293 return 1;
3294
3295 /* Come here when symtabs don't contain line # info. In this case, it is
3296 likely that the user has stepped into a library function w/o symbols, or
3297 is doing a stepi/nexti through code without symbols. */
3298
3299 nosyms:
3300
3301 /* If func_start is zero (meaning unknown) then we don't know whether pc is
3302 in the prologue or not. I.E. it might be. */
3303
3304 if (!func_start) return 1;
3305
3306 /* We need to call the target-specific prologue skipping functions with the
3307 function's start address because PC may be pointing at an instruction that
3308 could be mistakenly considered part of the prologue. */
3309
3310 SKIP_PROLOGUE (func_start);
3311
3312 return pc < func_start;
3313 }
3314
3315 \f
3316 void
3317 _initialize_symtab ()
3318 {
3319 add_info ("variables", variables_info,
3320 "All global and static variable names, or those matching REGEXP.");
3321 add_info ("functions", functions_info,
3322 "All function names, or those matching REGEXP.");
3323
3324 /* FIXME: This command has at least the following problems:
3325 1. It prints builtin types (in a very strange and confusing fashion).
3326 2. It doesn't print right, e.g. with
3327 typedef struct foo *FOO
3328 type_print prints "FOO" when we want to make it (in this situation)
3329 print "struct foo *".
3330 I also think "ptype" or "whatis" is more likely to be useful (but if
3331 there is much disagreement "info types" can be fixed). */
3332 add_info ("types", types_info,
3333 "All type names, or those matching REGEXP.");
3334
3335 #if 0
3336 add_info ("methods", methods_info,
3337 "All method names, or those matching REGEXP::REGEXP.\n\
3338 If the class qualifier is omitted, it is assumed to be the current scope.\n\
3339 If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
3340 are listed.");
3341 #endif
3342 add_info ("sources", sources_info,
3343 "Source files in the program.");
3344
3345 add_com ("rbreak", no_class, rbreak_command,
3346 "Set a breakpoint for all functions matching REGEXP.");
3347
3348 /* Initialize the one built-in type that isn't language dependent... */
3349 builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
3350 "<unknown type>", (struct objfile *) NULL);
3351 }
This page took 0.098645 seconds and 4 git commands to generate.