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