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