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