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